1 /*
2  * MTD driver for Alauda chips
3  *
4  * Copyright (C) 2007 Joern Engel <joern@logfs.org>
5  *
6  * Based on drivers/usb/usb-skeleton.c which is:
7  * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
8  * and on drivers/usb/storage/alauda.c, which is:
9  *   (c) 2005 Daniel Drake <dsd@gentoo.org>
10  *
11  * Idea and initial work by Arnd Bergmann <arnd@arndb.de>
12  */
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/module.h>
18 #include <linux/kref.h>
19 #include <linux/usb.h>
20 #include <linux/mutex.h>
21 #include <linux/mtd/mtd.h>
22 #include <linux/mtd/nand_ecc.h>
23 
24 /* Control commands */
25 #define ALAUDA_GET_XD_MEDIA_STATUS	0x08
26 #define ALAUDA_ACK_XD_MEDIA_CHANGE	0x0a
27 #define ALAUDA_GET_XD_MEDIA_SIG		0x86
28 
29 /* Common prefix */
30 #define ALAUDA_BULK_CMD			0x40
31 
32 /* The two ports */
33 #define ALAUDA_PORT_XD			0x00
34 #define ALAUDA_PORT_SM			0x01
35 
36 /* Bulk commands */
37 #define ALAUDA_BULK_READ_PAGE		0x84
38 #define ALAUDA_BULK_READ_OOB		0x85 /* don't use, there's a chip bug */
39 #define ALAUDA_BULK_READ_BLOCK		0x94
40 #define ALAUDA_BULK_ERASE_BLOCK		0xa3
41 #define ALAUDA_BULK_WRITE_PAGE		0xa4
42 #define ALAUDA_BULK_WRITE_BLOCK		0xb4
43 #define ALAUDA_BULK_RESET_MEDIA		0xe0
44 
45 /* Address shifting */
46 #define PBA_LO(pba) ((pba & 0xF) << 5)
47 #define PBA_HI(pba) (pba >> 3)
48 #define PBA_ZONE(pba) (pba >> 11)
49 
50 #define TIMEOUT HZ
51 
52 static const struct usb_device_id alauda_table[] = {
53 	{ USB_DEVICE(0x0584, 0x0008) },	/* Fujifilm DPC-R1 */
54 	{ USB_DEVICE(0x07b4, 0x010a) },	/* Olympus MAUSB-10 */
55 	{ }
56 };
57 MODULE_DEVICE_TABLE(usb, alauda_table);
58 
59 struct alauda_card {
60 	u8	id;		/* id byte */
61 	u8	chipshift;	/* 1<<chipshift total size */
62 	u8	pageshift;	/* 1<<pageshift page size */
63 	u8	blockshift;	/* 1<<blockshift block size */
64 };
65 
66 struct alauda {
67 	struct usb_device	*dev;
68 	struct usb_interface	*interface;
69 	struct mtd_info		*mtd;
70 	struct alauda_card	*card;
71 	struct mutex		card_mutex;
72 	u32			pagemask;
73 	u32			bytemask;
74 	u32			blockmask;
75 	unsigned int		write_out;
76 	unsigned int		bulk_in;
77 	unsigned int		bulk_out;
78 	u8			port;
79 	struct kref		kref;
80 };
81 
82 static struct alauda_card alauda_card_ids[] = {
83 	/* NAND flash */
84 	{ 0x6e, 20, 8, 12},	/* 1 MB */
85 	{ 0xe8, 20, 8, 12},	/* 1 MB */
86 	{ 0xec, 20, 8, 12},	/* 1 MB */
87 	{ 0x64, 21, 8, 12},	/* 2 MB */
88 	{ 0xea, 21, 8, 12},	/* 2 MB */
89 	{ 0x6b, 22, 9, 13},	/* 4 MB */
90 	{ 0xe3, 22, 9, 13},	/* 4 MB */
91 	{ 0xe5, 22, 9, 13},	/* 4 MB */
92 	{ 0xe6, 23, 9, 13},	/* 8 MB */
93 	{ 0x73, 24, 9, 14},	/* 16 MB */
94 	{ 0x75, 25, 9, 14},	/* 32 MB */
95 	{ 0x76, 26, 9, 14},	/* 64 MB */
96 	{ 0x79, 27, 9, 14},	/* 128 MB */
97 	{ 0x71, 28, 9, 14},	/* 256 MB */
98 
99 	/* MASK ROM */
100 	{ 0x5d, 21, 9, 13},	/* 2 MB */
101 	{ 0xd5, 22, 9, 13},	/* 4 MB */
102 	{ 0xd6, 23, 9, 13},	/* 8 MB */
103 	{ 0x57, 24, 9, 13},	/* 16 MB */
104 	{ 0x58, 25, 9, 13},	/* 32 MB */
105 	{ }
106 };
107 
get_card(u8 id)108 static struct alauda_card *get_card(u8 id)
109 {
110 	struct alauda_card *card;
111 
112 	for (card = alauda_card_ids; card->id; card++)
113 		if (card->id == id)
114 			return card;
115 	return NULL;
116 }
117 
alauda_delete(struct kref * kref)118 static void alauda_delete(struct kref *kref)
119 {
120 	struct alauda *al = container_of(kref, struct alauda, kref);
121 
122 	if (al->mtd) {
123 		del_mtd_device(al->mtd);
124 		kfree(al->mtd);
125 	}
126 	usb_put_dev(al->dev);
127 	kfree(al);
128 }
129 
alauda_get_media_status(struct alauda * al,void * buf)130 static int alauda_get_media_status(struct alauda *al, void *buf)
131 {
132 	int ret;
133 
134 	mutex_lock(&al->card_mutex);
135 	ret = usb_control_msg(al->dev, usb_rcvctrlpipe(al->dev, 0),
136 			ALAUDA_GET_XD_MEDIA_STATUS, 0xc0, 0, 1, buf, 2, HZ);
137 	mutex_unlock(&al->card_mutex);
138 	return ret;
139 }
140 
alauda_ack_media(struct alauda * al)141 static int alauda_ack_media(struct alauda *al)
142 {
143 	int ret;
144 
145 	mutex_lock(&al->card_mutex);
146 	ret = usb_control_msg(al->dev, usb_sndctrlpipe(al->dev, 0),
147 			ALAUDA_ACK_XD_MEDIA_CHANGE, 0x40, 0, 1, NULL, 0, HZ);
148 	mutex_unlock(&al->card_mutex);
149 	return ret;
150 }
151 
alauda_get_media_signatures(struct alauda * al,void * buf)152 static int alauda_get_media_signatures(struct alauda *al, void *buf)
153 {
154 	int ret;
155 
156 	mutex_lock(&al->card_mutex);
157 	ret = usb_control_msg(al->dev, usb_rcvctrlpipe(al->dev, 0),
158 			ALAUDA_GET_XD_MEDIA_SIG, 0xc0, 0, 0, buf, 4, HZ);
159 	mutex_unlock(&al->card_mutex);
160 	return ret;
161 }
162 
alauda_reset(struct alauda * al)163 static void alauda_reset(struct alauda *al)
164 {
165 	u8 command[] = {
166 		ALAUDA_BULK_CMD, ALAUDA_BULK_RESET_MEDIA, 0, 0,
167 		0, 0, 0, 0, al->port
168 	};
169 	mutex_lock(&al->card_mutex);
170 	usb_bulk_msg(al->dev, al->bulk_out, command, 9, NULL, HZ);
171 	mutex_unlock(&al->card_mutex);
172 }
173 
correct_data(void * buf,void * read_ecc,int * corrected,int * uncorrected)174 static void correct_data(void *buf, void *read_ecc,
175 		int *corrected, int *uncorrected)
176 {
177 	u8 calc_ecc[3];
178 	int err;
179 
180 	nand_calculate_ecc(NULL, buf, calc_ecc);
181 	err = nand_correct_data(NULL, buf, read_ecc, calc_ecc);
182 	if (err) {
183 		if (err > 0)
184 			(*corrected)++;
185 		else
186 			(*uncorrected)++;
187 	}
188 }
189 
190 struct alauda_sg_request {
191 	struct urb *urb[3];
192 	struct completion comp;
193 };
194 
alauda_complete(struct urb * urb)195 static void alauda_complete(struct urb *urb)
196 {
197 	struct completion *comp = urb->context;
198 
199 	if (comp)
200 		complete(comp);
201 }
202 
__alauda_read_page(struct mtd_info * mtd,loff_t from,void * buf,void * oob)203 static int __alauda_read_page(struct mtd_info *mtd, loff_t from, void *buf,
204 		void *oob)
205 {
206 	struct alauda_sg_request sg;
207 	struct alauda *al = mtd->priv;
208 	u32 pba = from >> al->card->blockshift;
209 	u32 page = (from >> al->card->pageshift) & al->pagemask;
210 	u8 command[] = {
211 		ALAUDA_BULK_CMD, ALAUDA_BULK_READ_PAGE, PBA_HI(pba),
212 		PBA_ZONE(pba), 0, PBA_LO(pba) + page, 1, 0, al->port
213 	};
214 	int i, err;
215 
216 	for (i=0; i<3; i++)
217 		sg.urb[i] = NULL;
218 
219 	err = -ENOMEM;
220 	for (i=0; i<3; i++) {
221 		sg.urb[i] = usb_alloc_urb(0, GFP_NOIO);
222 		if (!sg.urb[i])
223 			goto out;
224 	}
225 	init_completion(&sg.comp);
226 	usb_fill_bulk_urb(sg.urb[0], al->dev, al->bulk_out, command, 9,
227 			alauda_complete, NULL);
228 	usb_fill_bulk_urb(sg.urb[1], al->dev, al->bulk_in, buf, mtd->writesize,
229 			alauda_complete, NULL);
230 	usb_fill_bulk_urb(sg.urb[2], al->dev, al->bulk_in, oob, 16,
231 			alauda_complete, &sg.comp);
232 
233 	mutex_lock(&al->card_mutex);
234 	for (i=0; i<3; i++) {
235 		err = usb_submit_urb(sg.urb[i], GFP_NOIO);
236 		if (err)
237 			goto cancel;
238 	}
239 	if (!wait_for_completion_timeout(&sg.comp, TIMEOUT)) {
240 		err = -ETIMEDOUT;
241 cancel:
242 		for (i=0; i<3; i++) {
243 			usb_kill_urb(sg.urb[i]);
244 		}
245 	}
246 	mutex_unlock(&al->card_mutex);
247 
248 out:
249 	usb_free_urb(sg.urb[0]);
250 	usb_free_urb(sg.urb[1]);
251 	usb_free_urb(sg.urb[2]);
252 	return err;
253 }
254 
alauda_read_page(struct mtd_info * mtd,loff_t from,void * buf,u8 * oob,int * corrected,int * uncorrected)255 static int alauda_read_page(struct mtd_info *mtd, loff_t from,
256 		void *buf, u8 *oob, int *corrected, int *uncorrected)
257 {
258 	int err;
259 
260 	err = __alauda_read_page(mtd, from, buf, oob);
261 	if (err)
262 		return err;
263 	correct_data(buf, oob+13, corrected, uncorrected);
264 	correct_data(buf+256, oob+8, corrected, uncorrected);
265 	return 0;
266 }
267 
alauda_write_page(struct mtd_info * mtd,loff_t to,void * buf,void * oob)268 static int alauda_write_page(struct mtd_info *mtd, loff_t to, void *buf,
269 		void *oob)
270 {
271 	struct alauda_sg_request sg;
272 	struct alauda *al = mtd->priv;
273 	u32 pba = to >> al->card->blockshift;
274 	u32 page = (to >> al->card->pageshift) & al->pagemask;
275 	u8 command[] = {
276 		ALAUDA_BULK_CMD, ALAUDA_BULK_WRITE_PAGE, PBA_HI(pba),
277 		PBA_ZONE(pba), 0, PBA_LO(pba) + page, 32, 0, al->port
278 	};
279 	int i, err;
280 
281 	for (i=0; i<3; i++)
282 		sg.urb[i] = NULL;
283 
284 	err = -ENOMEM;
285 	for (i=0; i<3; i++) {
286 		sg.urb[i] = usb_alloc_urb(0, GFP_NOIO);
287 		if (!sg.urb[i])
288 			goto out;
289 	}
290 	init_completion(&sg.comp);
291 	usb_fill_bulk_urb(sg.urb[0], al->dev, al->bulk_out, command, 9,
292 			alauda_complete, NULL);
293 	usb_fill_bulk_urb(sg.urb[1], al->dev, al->write_out, buf,mtd->writesize,
294 			alauda_complete, NULL);
295 	usb_fill_bulk_urb(sg.urb[2], al->dev, al->write_out, oob, 16,
296 			alauda_complete, &sg.comp);
297 
298 	mutex_lock(&al->card_mutex);
299 	for (i=0; i<3; i++) {
300 		err = usb_submit_urb(sg.urb[i], GFP_NOIO);
301 		if (err)
302 			goto cancel;
303 	}
304 	if (!wait_for_completion_timeout(&sg.comp, TIMEOUT)) {
305 		err = -ETIMEDOUT;
306 cancel:
307 		for (i=0; i<3; i++) {
308 			usb_kill_urb(sg.urb[i]);
309 		}
310 	}
311 	mutex_unlock(&al->card_mutex);
312 
313 out:
314 	usb_free_urb(sg.urb[0]);
315 	usb_free_urb(sg.urb[1]);
316 	usb_free_urb(sg.urb[2]);
317 	return err;
318 }
319 
alauda_erase_block(struct mtd_info * mtd,loff_t ofs)320 static int alauda_erase_block(struct mtd_info *mtd, loff_t ofs)
321 {
322 	struct alauda_sg_request sg;
323 	struct alauda *al = mtd->priv;
324 	u32 pba = ofs >> al->card->blockshift;
325 	u8 command[] = {
326 		ALAUDA_BULK_CMD, ALAUDA_BULK_ERASE_BLOCK, PBA_HI(pba),
327 		PBA_ZONE(pba), 0, PBA_LO(pba), 0x02, 0, al->port
328 	};
329 	u8 buf[2];
330 	int i, err;
331 
332 	for (i=0; i<2; i++)
333 		sg.urb[i] = NULL;
334 
335 	err = -ENOMEM;
336 	for (i=0; i<2; i++) {
337 		sg.urb[i] = usb_alloc_urb(0, GFP_NOIO);
338 		if (!sg.urb[i])
339 			goto out;
340 	}
341 	init_completion(&sg.comp);
342 	usb_fill_bulk_urb(sg.urb[0], al->dev, al->bulk_out, command, 9,
343 			alauda_complete, NULL);
344 	usb_fill_bulk_urb(sg.urb[1], al->dev, al->bulk_in, buf, 2,
345 			alauda_complete, &sg.comp);
346 
347 	mutex_lock(&al->card_mutex);
348 	for (i=0; i<2; i++) {
349 		err = usb_submit_urb(sg.urb[i], GFP_NOIO);
350 		if (err)
351 			goto cancel;
352 	}
353 	if (!wait_for_completion_timeout(&sg.comp, TIMEOUT)) {
354 		err = -ETIMEDOUT;
355 cancel:
356 		for (i=0; i<2; i++) {
357 			usb_kill_urb(sg.urb[i]);
358 		}
359 	}
360 	mutex_unlock(&al->card_mutex);
361 
362 out:
363 	usb_free_urb(sg.urb[0]);
364 	usb_free_urb(sg.urb[1]);
365 	return err;
366 }
367 
alauda_read_oob(struct mtd_info * mtd,loff_t from,void * oob)368 static int alauda_read_oob(struct mtd_info *mtd, loff_t from, void *oob)
369 {
370 	static u8 ignore_buf[512]; /* write only */
371 
372 	return __alauda_read_page(mtd, from, ignore_buf, oob);
373 }
374 
alauda_isbad(struct mtd_info * mtd,loff_t ofs)375 static int alauda_isbad(struct mtd_info *mtd, loff_t ofs)
376 {
377 	u8 oob[16];
378 	int err;
379 
380 	err = alauda_read_oob(mtd, ofs, oob);
381 	if (err)
382 		return err;
383 
384 	/* A block is marked bad if two or more bits are zero */
385 	return hweight8(oob[5]) >= 7 ? 0 : 1;
386 }
387 
alauda_bounce_read(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,u_char * buf)388 static int alauda_bounce_read(struct mtd_info *mtd, loff_t from, size_t len,
389 		size_t *retlen, u_char *buf)
390 {
391 	struct alauda *al = mtd->priv;
392 	void *bounce_buf;
393 	int err, corrected=0, uncorrected=0;
394 
395 	bounce_buf = kmalloc(mtd->writesize, GFP_KERNEL);
396 	if (!bounce_buf)
397 		return -ENOMEM;
398 
399 	*retlen = len;
400 	while (len) {
401 		u8 oob[16];
402 		size_t byte = from & al->bytemask;
403 		size_t cplen = min(len, mtd->writesize - byte);
404 
405 		err = alauda_read_page(mtd, from, bounce_buf, oob,
406 				&corrected, &uncorrected);
407 		if (err)
408 			goto out;
409 
410 		memcpy(buf, bounce_buf + byte, cplen);
411 		buf += cplen;
412 		from += cplen;
413 		len -= cplen;
414 	}
415 	err = 0;
416 	if (corrected)
417 		err = -EUCLEAN;
418 	if (uncorrected)
419 		err = -EBADMSG;
420 out:
421 	kfree(bounce_buf);
422 	return err;
423 }
424 
alauda_read(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,u_char * buf)425 static int alauda_read(struct mtd_info *mtd, loff_t from, size_t len,
426 		size_t *retlen, u_char *buf)
427 {
428 	struct alauda *al = mtd->priv;
429 	int err, corrected=0, uncorrected=0;
430 
431 	if ((from & al->bytemask) || (len & al->bytemask))
432 		return alauda_bounce_read(mtd, from, len, retlen, buf);
433 
434 	*retlen = len;
435 	while (len) {
436 		u8 oob[16];
437 
438 		err = alauda_read_page(mtd, from, buf, oob,
439 				&corrected, &uncorrected);
440 		if (err)
441 			return err;
442 
443 		buf += mtd->writesize;
444 		from += mtd->writesize;
445 		len -= mtd->writesize;
446 	}
447 	err = 0;
448 	if (corrected)
449 		err = -EUCLEAN;
450 	if (uncorrected)
451 		err = -EBADMSG;
452 	return err;
453 }
454 
alauda_write(struct mtd_info * mtd,loff_t to,size_t len,size_t * retlen,const u_char * buf)455 static int alauda_write(struct mtd_info *mtd, loff_t to, size_t len,
456 		size_t *retlen, const u_char *buf)
457 {
458 	struct alauda *al = mtd->priv;
459 	int err;
460 
461 	if ((to & al->bytemask) || (len & al->bytemask))
462 		return -EINVAL;
463 
464 	*retlen = len;
465 	while (len) {
466 		u32 page = (to >> al->card->pageshift) & al->pagemask;
467 		u8 oob[16] = {	'h', 'e', 'l', 'l', 'o', 0xff, 0xff, 0xff,
468 				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
469 
470 		/* don't write to bad blocks */
471 		if (page == 0) {
472 			err = alauda_isbad(mtd, to);
473 			if (err) {
474 				return -EIO;
475 			}
476 		}
477 		nand_calculate_ecc(mtd, buf, &oob[13]);
478 		nand_calculate_ecc(mtd, buf+256, &oob[8]);
479 
480 		err = alauda_write_page(mtd, to, (void*)buf, oob);
481 		if (err)
482 			return err;
483 
484 		buf += mtd->writesize;
485 		to += mtd->writesize;
486 		len -= mtd->writesize;
487 	}
488 	return 0;
489 }
490 
__alauda_erase(struct mtd_info * mtd,struct erase_info * instr)491 static int __alauda_erase(struct mtd_info *mtd, struct erase_info *instr)
492 {
493 	struct alauda *al = mtd->priv;
494 	u32 ofs = instr->addr;
495 	u32 len = instr->len;
496 	int err;
497 
498 	if ((ofs & al->blockmask) || (len & al->blockmask))
499 		return -EINVAL;
500 
501 	while (len) {
502 		/* don't erase bad blocks */
503 		err = alauda_isbad(mtd, ofs);
504 		if (err > 0)
505 			err = -EIO;
506 		if (err < 0)
507 			return err;
508 
509 		err = alauda_erase_block(mtd, ofs);
510 		if (err < 0)
511 			return err;
512 
513 		ofs += mtd->erasesize;
514 		len -= mtd->erasesize;
515 	}
516 	return 0;
517 }
518 
alauda_erase(struct mtd_info * mtd,struct erase_info * instr)519 static int alauda_erase(struct mtd_info *mtd, struct erase_info *instr)
520 {
521 	int err;
522 
523 	err = __alauda_erase(mtd, instr);
524 	instr->state = err ? MTD_ERASE_FAILED : MTD_ERASE_DONE;
525 	mtd_erase_callback(instr);
526 	return err;
527 }
528 
alauda_init_media(struct alauda * al)529 static int alauda_init_media(struct alauda *al)
530 {
531 	u8 buf[4], *b0=buf, *b1=buf+1;
532 	struct alauda_card *card;
533 	struct mtd_info *mtd;
534 	int err;
535 
536 	mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
537 	if (!mtd)
538 		return -ENOMEM;
539 
540 	for (;;) {
541 		err = alauda_get_media_status(al, buf);
542 		if (err < 0)
543 			goto error;
544 		if (*b0 & 0x10)
545 			break;
546 		msleep(20);
547 	}
548 
549 	err = alauda_ack_media(al);
550 	if (err)
551 		goto error;
552 
553 	msleep(10);
554 
555 	err = alauda_get_media_status(al, buf);
556 	if (err < 0)
557 		goto error;
558 
559 	if (*b0 != 0x14) {
560 		/* media not ready */
561 		err = -EIO;
562 		goto error;
563 	}
564 	err = alauda_get_media_signatures(al, buf);
565 	if (err < 0)
566 		goto error;
567 
568 	card = get_card(*b1);
569 	if (!card) {
570 		printk(KERN_ERR"Alauda: unknown card id %02x\n", *b1);
571 		err = -EIO;
572 		goto error;
573 	}
574 	printk(KERN_INFO"pagesize=%x\nerasesize=%x\nsize=%xMiB\n",
575 			1<<card->pageshift, 1<<card->blockshift,
576 			1<<(card->chipshift-20));
577 	al->card = card;
578 	al->pagemask = (1 << (card->blockshift - card->pageshift)) - 1;
579 	al->bytemask = (1 << card->pageshift) - 1;
580 	al->blockmask = (1 << card->blockshift) - 1;
581 
582 	mtd->name = "alauda";
583 	mtd->size = 1<<card->chipshift;
584 	mtd->erasesize = 1<<card->blockshift;
585 	mtd->writesize = 1<<card->pageshift;
586 	mtd->type = MTD_NANDFLASH;
587 	mtd->flags = MTD_CAP_NANDFLASH;
588 	mtd->read = alauda_read;
589 	mtd->write = alauda_write;
590 	mtd->erase = alauda_erase;
591 	mtd->block_isbad = alauda_isbad;
592 	mtd->priv = al;
593 	mtd->owner = THIS_MODULE;
594 
595 	err = add_mtd_device(mtd);
596 	if (err) {
597 		err = -ENFILE;
598 		goto error;
599 	}
600 
601 	al->mtd = mtd;
602 	alauda_reset(al); /* no clue whether this is necessary */
603 	return 0;
604 error:
605 	kfree(mtd);
606 	return err;
607 }
608 
alauda_check_media(struct alauda * al)609 static int alauda_check_media(struct alauda *al)
610 {
611 	u8 buf[2], *b0 = buf, *b1 = buf+1;
612 	int err;
613 
614 	err = alauda_get_media_status(al, buf);
615 	if (err < 0)
616 		return err;
617 
618 	if ((*b1 & 0x01) == 0) {
619 		/* door open */
620 		return -EIO;
621 	}
622 	if ((*b0 & 0x80) || ((*b0 & 0x1F) == 0x10)) {
623 		/* no media ? */
624 		return -EIO;
625 	}
626 	if (*b0 & 0x08) {
627 		/* media change ? */
628 		return alauda_init_media(al);
629 	}
630 	return 0;
631 }
632 
alauda_probe(struct usb_interface * interface,const struct usb_device_id * id)633 static int alauda_probe(struct usb_interface *interface,
634 		const struct usb_device_id *id)
635 {
636 	struct alauda *al;
637 	struct usb_host_interface *iface;
638 	struct usb_endpoint_descriptor *ep,
639 			*ep_in=NULL, *ep_out=NULL, *ep_wr=NULL;
640 	int i, err = -ENOMEM;
641 
642 	al = kzalloc(2*sizeof(*al), GFP_KERNEL);
643 	if (!al)
644 		goto error;
645 
646 	kref_init(&al->kref);
647 	usb_set_intfdata(interface, al);
648 
649 	al->dev = usb_get_dev(interface_to_usbdev(interface));
650 	al->interface = interface;
651 
652 	iface = interface->cur_altsetting;
653 	for (i = 0; i < iface->desc.bNumEndpoints; ++i) {
654 		ep = &iface->endpoint[i].desc;
655 
656 		if (usb_endpoint_is_bulk_in(ep)) {
657 			ep_in = ep;
658 		} else if (usb_endpoint_is_bulk_out(ep)) {
659 			if (i==0)
660 				ep_wr = ep;
661 			else
662 				ep_out = ep;
663 		}
664 	}
665 	err = -EIO;
666 	if (!ep_wr || !ep_in || !ep_out)
667 		goto error;
668 
669 	al->write_out = usb_sndbulkpipe(al->dev,
670 			usb_endpoint_num(ep_wr));
671 	al->bulk_in = usb_rcvbulkpipe(al->dev,
672 			usb_endpoint_num(ep_in));
673 	al->bulk_out = usb_sndbulkpipe(al->dev,
674 			usb_endpoint_num(ep_out));
675 
676 	/* second device is identical up to now */
677 	memcpy(al+1, al, sizeof(*al));
678 
679 	mutex_init(&al[0].card_mutex);
680 	mutex_init(&al[1].card_mutex);
681 
682 	al[0].port = ALAUDA_PORT_XD;
683 	al[1].port = ALAUDA_PORT_SM;
684 
685 	dev_info(&interface->dev, "alauda probed\n");
686 	alauda_check_media(al);
687 	alauda_check_media(al+1);
688 
689 	return 0;
690 
691 error:
692 	if (al)
693 		kref_put(&al->kref, alauda_delete);
694 	return err;
695 }
696 
alauda_disconnect(struct usb_interface * interface)697 static void alauda_disconnect(struct usb_interface *interface)
698 {
699 	struct alauda *al;
700 
701 	al = usb_get_intfdata(interface);
702 	usb_set_intfdata(interface, NULL);
703 
704 	/* FIXME: prevent more I/O from starting */
705 
706 	/* decrement our usage count */
707 	if (al)
708 		kref_put(&al->kref, alauda_delete);
709 
710 	dev_info(&interface->dev, "alauda gone");
711 }
712 
713 static struct usb_driver alauda_driver = {
714 	.name =		"alauda",
715 	.probe =	alauda_probe,
716 	.disconnect =	alauda_disconnect,
717 	.id_table =	alauda_table,
718 };
719 
alauda_init(void)720 static int __init alauda_init(void)
721 {
722 	return usb_register(&alauda_driver);
723 }
724 
alauda_exit(void)725 static void __exit alauda_exit(void)
726 {
727 	usb_deregister(&alauda_driver);
728 }
729 
730 module_init(alauda_init);
731 module_exit(alauda_exit);
732 
733 MODULE_LICENSE("GPL");
734