1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Driver for Alauda-based card readers
4  *
5  * Current development and maintenance by:
6  *   (c) 2005 Daniel Drake <dsd@gentoo.org>
7  *
8  * The 'Alauda' is a chip manufacturered by RATOC for OEM use.
9  *
10  * Alauda implements a vendor-specific command set to access two media reader
11  * ports (XD, SmartMedia). This driver converts SCSI commands to the commands
12  * which are accepted by these devices.
13  *
14  * The driver was developed through reverse-engineering, with the help of the
15  * sddr09 driver which has many similarities, and with some help from the
16  * (very old) vendor-supplied GPL sma03 driver.
17  *
18  * For protocol info, see http://alauda.sourceforge.net
19  */
20 
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 
24 #include <scsi/scsi.h>
25 #include <scsi/scsi_cmnd.h>
26 #include <scsi/scsi_device.h>
27 
28 #include "usb.h"
29 #include "transport.h"
30 #include "protocol.h"
31 #include "debug.h"
32 #include "scsiglue.h"
33 
34 #define DRV_NAME "ums-alauda"
35 
36 MODULE_DESCRIPTION("Driver for Alauda-based card readers");
37 MODULE_AUTHOR("Daniel Drake <dsd@gentoo.org>");
38 MODULE_LICENSE("GPL");
39 MODULE_IMPORT_NS(USB_STORAGE);
40 
41 /*
42  * Status bytes
43  */
44 #define ALAUDA_STATUS_ERROR		0x01
45 #define ALAUDA_STATUS_READY		0x40
46 
47 /*
48  * Control opcodes (for request field)
49  */
50 #define ALAUDA_GET_XD_MEDIA_STATUS	0x08
51 #define ALAUDA_GET_SM_MEDIA_STATUS	0x98
52 #define ALAUDA_ACK_XD_MEDIA_CHANGE	0x0a
53 #define ALAUDA_ACK_SM_MEDIA_CHANGE	0x9a
54 #define ALAUDA_GET_XD_MEDIA_SIG		0x86
55 #define ALAUDA_GET_SM_MEDIA_SIG		0x96
56 
57 /*
58  * Bulk command identity (byte 0)
59  */
60 #define ALAUDA_BULK_CMD			0x40
61 
62 /*
63  * Bulk opcodes (byte 1)
64  */
65 #define ALAUDA_BULK_GET_REDU_DATA	0x85
66 #define ALAUDA_BULK_READ_BLOCK		0x94
67 #define ALAUDA_BULK_ERASE_BLOCK		0xa3
68 #define ALAUDA_BULK_WRITE_BLOCK		0xb4
69 #define ALAUDA_BULK_GET_STATUS2		0xb7
70 #define ALAUDA_BULK_RESET_MEDIA		0xe0
71 
72 /*
73  * Port to operate on (byte 8)
74  */
75 #define ALAUDA_PORT_XD			0x00
76 #define ALAUDA_PORT_SM			0x01
77 
78 /*
79  * LBA and PBA are unsigned ints. Special values.
80  */
81 #define UNDEF    0xffff
82 #define SPARE    0xfffe
83 #define UNUSABLE 0xfffd
84 
85 struct alauda_media_info {
86 	unsigned long capacity;		/* total media size in bytes */
87 	unsigned int pagesize;		/* page size in bytes */
88 	unsigned int blocksize;		/* number of pages per block */
89 	unsigned int uzonesize;		/* number of usable blocks per zone */
90 	unsigned int zonesize;		/* number of blocks per zone */
91 	unsigned int blockmask;		/* mask to get page from address */
92 
93 	unsigned char pageshift;
94 	unsigned char blockshift;
95 	unsigned char zoneshift;
96 
97 	u16 **lba_to_pba;		/* logical to physical block map */
98 	u16 **pba_to_lba;		/* physical to logical block map */
99 };
100 
101 struct alauda_info {
102 	struct alauda_media_info port[2];
103 	int wr_ep;			/* endpoint to write data out of */
104 
105 	unsigned char sense_key;
106 	unsigned long sense_asc;	/* additional sense code */
107 	unsigned long sense_ascq;	/* additional sense code qualifier */
108 };
109 
110 #define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) )
111 #define LSB_of(s) ((s)&0xFF)
112 #define MSB_of(s) ((s)>>8)
113 
114 #define MEDIA_PORT(us) us->srb->device->lun
115 #define MEDIA_INFO(us) ((struct alauda_info *)us->extra)->port[MEDIA_PORT(us)]
116 
117 #define PBA_LO(pba) ((pba & 0xF) << 5)
118 #define PBA_HI(pba) (pba >> 3)
119 #define PBA_ZONE(pba) (pba >> 11)
120 
121 static int init_alauda(struct us_data *us);
122 
123 
124 /*
125  * The table of devices
126  */
127 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
128 		    vendorName, productName, useProtocol, useTransport, \
129 		    initFunction, flags) \
130 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
131   .driver_info = (flags) }
132 
133 static struct usb_device_id alauda_usb_ids[] = {
134 #	include "unusual_alauda.h"
135 	{ }		/* Terminating entry */
136 };
137 MODULE_DEVICE_TABLE(usb, alauda_usb_ids);
138 
139 #undef UNUSUAL_DEV
140 
141 /*
142  * The flags table
143  */
144 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
145 		    vendor_name, product_name, use_protocol, use_transport, \
146 		    init_function, Flags) \
147 { \
148 	.vendorName = vendor_name,	\
149 	.productName = product_name,	\
150 	.useProtocol = use_protocol,	\
151 	.useTransport = use_transport,	\
152 	.initFunction = init_function,	\
153 }
154 
155 static struct us_unusual_dev alauda_unusual_dev_list[] = {
156 #	include "unusual_alauda.h"
157 	{ }		/* Terminating entry */
158 };
159 
160 #undef UNUSUAL_DEV
161 
162 
163 /*
164  * Media handling
165  */
166 
167 struct alauda_card_info {
168 	unsigned char id;		/* id byte */
169 	unsigned char chipshift;	/* 1<<cs bytes total capacity */
170 	unsigned char pageshift;	/* 1<<ps bytes in a page */
171 	unsigned char blockshift;	/* 1<<bs pages per block */
172 	unsigned char zoneshift;	/* 1<<zs blocks per zone */
173 };
174 
175 static struct alauda_card_info alauda_card_ids[] = {
176 	/* NAND flash */
177 	{ 0x6e, 20, 8, 4, 8},	/* 1 MB */
178 	{ 0xe8, 20, 8, 4, 8},	/* 1 MB */
179 	{ 0xec, 20, 8, 4, 8},	/* 1 MB */
180 	{ 0x64, 21, 8, 4, 9}, 	/* 2 MB */
181 	{ 0xea, 21, 8, 4, 9},	/* 2 MB */
182 	{ 0x6b, 22, 9, 4, 9},	/* 4 MB */
183 	{ 0xe3, 22, 9, 4, 9},	/* 4 MB */
184 	{ 0xe5, 22, 9, 4, 9},	/* 4 MB */
185 	{ 0xe6, 23, 9, 4, 10},	/* 8 MB */
186 	{ 0x73, 24, 9, 5, 10},	/* 16 MB */
187 	{ 0x75, 25, 9, 5, 10},	/* 32 MB */
188 	{ 0x76, 26, 9, 5, 10},	/* 64 MB */
189 	{ 0x79, 27, 9, 5, 10},	/* 128 MB */
190 	{ 0x71, 28, 9, 5, 10},	/* 256 MB */
191 
192 	/* MASK ROM */
193 	{ 0x5d, 21, 9, 4, 8},	/* 2 MB */
194 	{ 0xd5, 22, 9, 4, 9},	/* 4 MB */
195 	{ 0xd6, 23, 9, 4, 10},	/* 8 MB */
196 	{ 0x57, 24, 9, 4, 11},	/* 16 MB */
197 	{ 0x58, 25, 9, 4, 12},	/* 32 MB */
198 	{ 0,}
199 };
200 
alauda_card_find_id(unsigned char id)201 static struct alauda_card_info *alauda_card_find_id(unsigned char id)
202 {
203 	int i;
204 
205 	for (i = 0; alauda_card_ids[i].id != 0; i++)
206 		if (alauda_card_ids[i].id == id)
207 			return &(alauda_card_ids[i]);
208 	return NULL;
209 }
210 
211 /*
212  * ECC computation.
213  */
214 
215 static unsigned char parity[256];
216 static unsigned char ecc2[256];
217 
nand_init_ecc(void)218 static void nand_init_ecc(void)
219 {
220 	int i, j, a;
221 
222 	parity[0] = 0;
223 	for (i = 1; i < 256; i++)
224 		parity[i] = (parity[i&(i-1)] ^ 1);
225 
226 	for (i = 0; i < 256; i++) {
227 		a = 0;
228 		for (j = 0; j < 8; j++) {
229 			if (i & (1<<j)) {
230 				if ((j & 1) == 0)
231 					a ^= 0x04;
232 				if ((j & 2) == 0)
233 					a ^= 0x10;
234 				if ((j & 4) == 0)
235 					a ^= 0x40;
236 			}
237 		}
238 		ecc2[i] = ~(a ^ (a<<1) ^ (parity[i] ? 0xa8 : 0));
239 	}
240 }
241 
242 /* compute 3-byte ecc on 256 bytes */
nand_compute_ecc(unsigned char * data,unsigned char * ecc)243 static void nand_compute_ecc(unsigned char *data, unsigned char *ecc)
244 {
245 	int i, j, a;
246 	unsigned char par = 0, bit, bits[8] = {0};
247 
248 	/* collect 16 checksum bits */
249 	for (i = 0; i < 256; i++) {
250 		par ^= data[i];
251 		bit = parity[data[i]];
252 		for (j = 0; j < 8; j++)
253 			if ((i & (1<<j)) == 0)
254 				bits[j] ^= bit;
255 	}
256 
257 	/* put 4+4+4 = 12 bits in the ecc */
258 	a = (bits[3] << 6) + (bits[2] << 4) + (bits[1] << 2) + bits[0];
259 	ecc[0] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
260 
261 	a = (bits[7] << 6) + (bits[6] << 4) + (bits[5] << 2) + bits[4];
262 	ecc[1] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
263 
264 	ecc[2] = ecc2[par];
265 }
266 
nand_compare_ecc(unsigned char * data,unsigned char * ecc)267 static int nand_compare_ecc(unsigned char *data, unsigned char *ecc)
268 {
269 	return (data[0] == ecc[0] && data[1] == ecc[1] && data[2] == ecc[2]);
270 }
271 
nand_store_ecc(unsigned char * data,unsigned char * ecc)272 static void nand_store_ecc(unsigned char *data, unsigned char *ecc)
273 {
274 	memcpy(data, ecc, 3);
275 }
276 
277 /*
278  * Alauda driver
279  */
280 
281 /*
282  * Forget our PBA <---> LBA mappings for a particular port
283  */
alauda_free_maps(struct alauda_media_info * media_info)284 static void alauda_free_maps (struct alauda_media_info *media_info)
285 {
286 	unsigned int shift = media_info->zoneshift
287 		+ media_info->blockshift + media_info->pageshift;
288 	unsigned int num_zones = media_info->capacity >> shift;
289 	unsigned int i;
290 
291 	if (media_info->lba_to_pba != NULL)
292 		for (i = 0; i < num_zones; i++) {
293 			kfree(media_info->lba_to_pba[i]);
294 			media_info->lba_to_pba[i] = NULL;
295 		}
296 
297 	if (media_info->pba_to_lba != NULL)
298 		for (i = 0; i < num_zones; i++) {
299 			kfree(media_info->pba_to_lba[i]);
300 			media_info->pba_to_lba[i] = NULL;
301 		}
302 }
303 
304 /*
305  * Returns 2 bytes of status data
306  * The first byte describes media status, and second byte describes door status
307  */
alauda_get_media_status(struct us_data * us,unsigned char * data)308 static int alauda_get_media_status(struct us_data *us, unsigned char *data)
309 {
310 	int rc;
311 	unsigned char command;
312 
313 	if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
314 		command = ALAUDA_GET_XD_MEDIA_STATUS;
315 	else
316 		command = ALAUDA_GET_SM_MEDIA_STATUS;
317 
318 	rc = usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
319 		command, 0xc0, 0, 1, data, 2);
320 
321 	usb_stor_dbg(us, "Media status %02X %02X\n", data[0], data[1]);
322 
323 	return rc;
324 }
325 
326 /*
327  * Clears the "media was changed" bit so that we know when it changes again
328  * in the future.
329  */
alauda_ack_media(struct us_data * us)330 static int alauda_ack_media(struct us_data *us)
331 {
332 	unsigned char command;
333 
334 	if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
335 		command = ALAUDA_ACK_XD_MEDIA_CHANGE;
336 	else
337 		command = ALAUDA_ACK_SM_MEDIA_CHANGE;
338 
339 	return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
340 		command, 0x40, 0, 1, NULL, 0);
341 }
342 
343 /*
344  * Retrieves a 4-byte media signature, which indicates manufacturer, capacity,
345  * and some other details.
346  */
alauda_get_media_signature(struct us_data * us,unsigned char * data)347 static int alauda_get_media_signature(struct us_data *us, unsigned char *data)
348 {
349 	unsigned char command;
350 
351 	if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
352 		command = ALAUDA_GET_XD_MEDIA_SIG;
353 	else
354 		command = ALAUDA_GET_SM_MEDIA_SIG;
355 
356 	return usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
357 		command, 0xc0, 0, 0, data, 4);
358 }
359 
360 /*
361  * Resets the media status (but not the whole device?)
362  */
alauda_reset_media(struct us_data * us)363 static int alauda_reset_media(struct us_data *us)
364 {
365 	unsigned char *command = us->iobuf;
366 
367 	memset(command, 0, 9);
368 	command[0] = ALAUDA_BULK_CMD;
369 	command[1] = ALAUDA_BULK_RESET_MEDIA;
370 	command[8] = MEDIA_PORT(us);
371 
372 	return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
373 		command, 9, NULL);
374 }
375 
376 /*
377  * Examines the media and deduces capacity, etc.
378  */
alauda_init_media(struct us_data * us)379 static int alauda_init_media(struct us_data *us)
380 {
381 	unsigned char *data = us->iobuf;
382 	int ready = 0;
383 	struct alauda_card_info *media_info;
384 	unsigned int num_zones;
385 
386 	while (ready == 0) {
387 		msleep(20);
388 
389 		if (alauda_get_media_status(us, data) != USB_STOR_XFER_GOOD)
390 			return USB_STOR_TRANSPORT_ERROR;
391 
392 		if (data[0] & 0x10)
393 			ready = 1;
394 	}
395 
396 	usb_stor_dbg(us, "We are ready for action!\n");
397 
398 	if (alauda_ack_media(us) != USB_STOR_XFER_GOOD)
399 		return USB_STOR_TRANSPORT_ERROR;
400 
401 	msleep(10);
402 
403 	if (alauda_get_media_status(us, data) != USB_STOR_XFER_GOOD)
404 		return USB_STOR_TRANSPORT_ERROR;
405 
406 	if (data[0] != 0x14) {
407 		usb_stor_dbg(us, "Media not ready after ack\n");
408 		return USB_STOR_TRANSPORT_ERROR;
409 	}
410 
411 	if (alauda_get_media_signature(us, data) != USB_STOR_XFER_GOOD)
412 		return USB_STOR_TRANSPORT_ERROR;
413 
414 	usb_stor_dbg(us, "Media signature: %4ph\n", data);
415 	media_info = alauda_card_find_id(data[1]);
416 	if (media_info == NULL) {
417 		pr_warn("alauda_init_media: Unrecognised media signature: %4ph\n",
418 			data);
419 		return USB_STOR_TRANSPORT_ERROR;
420 	}
421 
422 	MEDIA_INFO(us).capacity = 1 << media_info->chipshift;
423 	usb_stor_dbg(us, "Found media with capacity: %ldMB\n",
424 		     MEDIA_INFO(us).capacity >> 20);
425 
426 	MEDIA_INFO(us).pageshift = media_info->pageshift;
427 	MEDIA_INFO(us).blockshift = media_info->blockshift;
428 	MEDIA_INFO(us).zoneshift = media_info->zoneshift;
429 
430 	MEDIA_INFO(us).pagesize = 1 << media_info->pageshift;
431 	MEDIA_INFO(us).blocksize = 1 << media_info->blockshift;
432 	MEDIA_INFO(us).zonesize = 1 << media_info->zoneshift;
433 
434 	MEDIA_INFO(us).uzonesize = ((1 << media_info->zoneshift) / 128) * 125;
435 	MEDIA_INFO(us).blockmask = MEDIA_INFO(us).blocksize - 1;
436 
437 	num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift
438 		+ MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift);
439 	MEDIA_INFO(us).pba_to_lba = kcalloc(num_zones, sizeof(u16*), GFP_NOIO);
440 	MEDIA_INFO(us).lba_to_pba = kcalloc(num_zones, sizeof(u16*), GFP_NOIO);
441 	if (MEDIA_INFO(us).pba_to_lba == NULL || MEDIA_INFO(us).lba_to_pba == NULL)
442 		return USB_STOR_TRANSPORT_ERROR;
443 
444 	if (alauda_reset_media(us) != USB_STOR_XFER_GOOD)
445 		return USB_STOR_TRANSPORT_ERROR;
446 
447 	return USB_STOR_TRANSPORT_GOOD;
448 }
449 
450 /*
451  * Examines the media status and does the right thing when the media has gone,
452  * appeared, or changed.
453  */
alauda_check_media(struct us_data * us)454 static int alauda_check_media(struct us_data *us)
455 {
456 	struct alauda_info *info = (struct alauda_info *) us->extra;
457 	unsigned char status[2];
458 
459 	alauda_get_media_status(us, status);
460 
461 	/* Check for no media or door open */
462 	if ((status[0] & 0x80) || ((status[0] & 0x1F) == 0x10)
463 		|| ((status[1] & 0x01) == 0)) {
464 		usb_stor_dbg(us, "No media, or door open\n");
465 		alauda_free_maps(&MEDIA_INFO(us));
466 		info->sense_key = 0x02;
467 		info->sense_asc = 0x3A;
468 		info->sense_ascq = 0x00;
469 		return USB_STOR_TRANSPORT_FAILED;
470 	}
471 
472 	/* Check for media change */
473 	if (status[0] & 0x08) {
474 		usb_stor_dbg(us, "Media change detected\n");
475 		alauda_free_maps(&MEDIA_INFO(us));
476 		alauda_init_media(us);
477 
478 		info->sense_key = UNIT_ATTENTION;
479 		info->sense_asc = 0x28;
480 		info->sense_ascq = 0x00;
481 		return USB_STOR_TRANSPORT_FAILED;
482 	}
483 
484 	return USB_STOR_TRANSPORT_GOOD;
485 }
486 
487 /*
488  * Checks the status from the 2nd status register
489  * Returns 3 bytes of status data, only the first is known
490  */
alauda_check_status2(struct us_data * us)491 static int alauda_check_status2(struct us_data *us)
492 {
493 	int rc;
494 	unsigned char command[] = {
495 		ALAUDA_BULK_CMD, ALAUDA_BULK_GET_STATUS2,
496 		0, 0, 0, 0, 3, 0, MEDIA_PORT(us)
497 	};
498 	unsigned char data[3];
499 
500 	rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
501 		command, 9, NULL);
502 	if (rc != USB_STOR_XFER_GOOD)
503 		return rc;
504 
505 	rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
506 		data, 3, NULL);
507 	if (rc != USB_STOR_XFER_GOOD)
508 		return rc;
509 
510 	usb_stor_dbg(us, "%3ph\n", data);
511 	if (data[0] & ALAUDA_STATUS_ERROR)
512 		return USB_STOR_XFER_ERROR;
513 
514 	return USB_STOR_XFER_GOOD;
515 }
516 
517 /*
518  * Gets the redundancy data for the first page of a PBA
519  * Returns 16 bytes.
520  */
alauda_get_redu_data(struct us_data * us,u16 pba,unsigned char * data)521 static int alauda_get_redu_data(struct us_data *us, u16 pba, unsigned char *data)
522 {
523 	int rc;
524 	unsigned char command[] = {
525 		ALAUDA_BULK_CMD, ALAUDA_BULK_GET_REDU_DATA,
526 		PBA_HI(pba), PBA_ZONE(pba), 0, PBA_LO(pba), 0, 0, MEDIA_PORT(us)
527 	};
528 
529 	rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
530 		command, 9, NULL);
531 	if (rc != USB_STOR_XFER_GOOD)
532 		return rc;
533 
534 	return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
535 		data, 16, NULL);
536 }
537 
538 /*
539  * Finds the first unused PBA in a zone
540  * Returns the absolute PBA of an unused PBA, or 0 if none found.
541  */
alauda_find_unused_pba(struct alauda_media_info * info,unsigned int zone)542 static u16 alauda_find_unused_pba(struct alauda_media_info *info,
543 	unsigned int zone)
544 {
545 	u16 *pba_to_lba = info->pba_to_lba[zone];
546 	unsigned int i;
547 
548 	for (i = 0; i < info->zonesize; i++)
549 		if (pba_to_lba[i] == UNDEF)
550 			return (zone << info->zoneshift) + i;
551 
552 	return 0;
553 }
554 
555 /*
556  * Reads the redundancy data for all PBA's in a zone
557  * Produces lba <--> pba mappings
558  */
alauda_read_map(struct us_data * us,unsigned int zone)559 static int alauda_read_map(struct us_data *us, unsigned int zone)
560 {
561 	unsigned char *data = us->iobuf;
562 	int result;
563 	int i, j;
564 	unsigned int zonesize = MEDIA_INFO(us).zonesize;
565 	unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
566 	unsigned int lba_offset, lba_real, blocknum;
567 	unsigned int zone_base_lba = zone * uzonesize;
568 	unsigned int zone_base_pba = zone * zonesize;
569 	u16 *lba_to_pba = kcalloc(zonesize, sizeof(u16), GFP_NOIO);
570 	u16 *pba_to_lba = kcalloc(zonesize, sizeof(u16), GFP_NOIO);
571 	if (lba_to_pba == NULL || pba_to_lba == NULL) {
572 		result = USB_STOR_TRANSPORT_ERROR;
573 		goto error;
574 	}
575 
576 	usb_stor_dbg(us, "Mapping blocks for zone %d\n", zone);
577 
578 	/* 1024 PBA's per zone */
579 	for (i = 0; i < zonesize; i++)
580 		lba_to_pba[i] = pba_to_lba[i] = UNDEF;
581 
582 	for (i = 0; i < zonesize; i++) {
583 		blocknum = zone_base_pba + i;
584 
585 		result = alauda_get_redu_data(us, blocknum, data);
586 		if (result != USB_STOR_XFER_GOOD) {
587 			result = USB_STOR_TRANSPORT_ERROR;
588 			goto error;
589 		}
590 
591 		/* special PBAs have control field 0^16 */
592 		for (j = 0; j < 16; j++)
593 			if (data[j] != 0)
594 				goto nonz;
595 		pba_to_lba[i] = UNUSABLE;
596 		usb_stor_dbg(us, "PBA %d has no logical mapping\n", blocknum);
597 		continue;
598 
599 	nonz:
600 		/* unwritten PBAs have control field FF^16 */
601 		for (j = 0; j < 16; j++)
602 			if (data[j] != 0xff)
603 				goto nonff;
604 		continue;
605 
606 	nonff:
607 		/* normal PBAs start with six FFs */
608 		if (j < 6) {
609 			usb_stor_dbg(us, "PBA %d has no logical mapping: reserved area = %02X%02X%02X%02X data status %02X block status %02X\n",
610 				     blocknum,
611 				     data[0], data[1], data[2], data[3],
612 				     data[4], data[5]);
613 			pba_to_lba[i] = UNUSABLE;
614 			continue;
615 		}
616 
617 		if ((data[6] >> 4) != 0x01) {
618 			usb_stor_dbg(us, "PBA %d has invalid address field %02X%02X/%02X%02X\n",
619 				     blocknum, data[6], data[7],
620 				     data[11], data[12]);
621 			pba_to_lba[i] = UNUSABLE;
622 			continue;
623 		}
624 
625 		/* check even parity */
626 		if (parity[data[6] ^ data[7]]) {
627 			printk(KERN_WARNING
628 			       "alauda_read_map: Bad parity in LBA for block %d"
629 			       " (%02X %02X)\n", i, data[6], data[7]);
630 			pba_to_lba[i] = UNUSABLE;
631 			continue;
632 		}
633 
634 		lba_offset = short_pack(data[7], data[6]);
635 		lba_offset = (lba_offset & 0x07FF) >> 1;
636 		lba_real = lba_offset + zone_base_lba;
637 
638 		/*
639 		 * Every 1024 physical blocks ("zone"), the LBA numbers
640 		 * go back to zero, but are within a higher block of LBA's.
641 		 * Also, there is a maximum of 1000 LBA's per zone.
642 		 * In other words, in PBA 1024-2047 you will find LBA 0-999
643 		 * which are really LBA 1000-1999. This allows for 24 bad
644 		 * or special physical blocks per zone.
645 		 */
646 
647 		if (lba_offset >= uzonesize) {
648 			printk(KERN_WARNING
649 			       "alauda_read_map: Bad low LBA %d for block %d\n",
650 			       lba_real, blocknum);
651 			continue;
652 		}
653 
654 		if (lba_to_pba[lba_offset] != UNDEF) {
655 			printk(KERN_WARNING
656 			       "alauda_read_map: "
657 			       "LBA %d seen for PBA %d and %d\n",
658 			       lba_real, lba_to_pba[lba_offset], blocknum);
659 			continue;
660 		}
661 
662 		pba_to_lba[i] = lba_real;
663 		lba_to_pba[lba_offset] = blocknum;
664 		continue;
665 	}
666 
667 	MEDIA_INFO(us).lba_to_pba[zone] = lba_to_pba;
668 	MEDIA_INFO(us).pba_to_lba[zone] = pba_to_lba;
669 	result = 0;
670 	goto out;
671 
672 error:
673 	kfree(lba_to_pba);
674 	kfree(pba_to_lba);
675 out:
676 	return result;
677 }
678 
679 /*
680  * Checks to see whether we have already mapped a certain zone
681  * If we haven't, the map is generated
682  */
alauda_ensure_map_for_zone(struct us_data * us,unsigned int zone)683 static void alauda_ensure_map_for_zone(struct us_data *us, unsigned int zone)
684 {
685 	if (MEDIA_INFO(us).lba_to_pba[zone] == NULL
686 		|| MEDIA_INFO(us).pba_to_lba[zone] == NULL)
687 		alauda_read_map(us, zone);
688 }
689 
690 /*
691  * Erases an entire block
692  */
alauda_erase_block(struct us_data * us,u16 pba)693 static int alauda_erase_block(struct us_data *us, u16 pba)
694 {
695 	int rc;
696 	unsigned char command[] = {
697 		ALAUDA_BULK_CMD, ALAUDA_BULK_ERASE_BLOCK, PBA_HI(pba),
698 		PBA_ZONE(pba), 0, PBA_LO(pba), 0x02, 0, MEDIA_PORT(us)
699 	};
700 	unsigned char buf[2];
701 
702 	usb_stor_dbg(us, "Erasing PBA %d\n", pba);
703 
704 	rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
705 		command, 9, NULL);
706 	if (rc != USB_STOR_XFER_GOOD)
707 		return rc;
708 
709 	rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
710 		buf, 2, NULL);
711 	if (rc != USB_STOR_XFER_GOOD)
712 		return rc;
713 
714 	usb_stor_dbg(us, "Erase result: %02X %02X\n", buf[0], buf[1]);
715 	return rc;
716 }
717 
718 /*
719  * Reads data from a certain offset page inside a PBA, including interleaved
720  * redundancy data. Returns (pagesize+64)*pages bytes in data.
721  */
alauda_read_block_raw(struct us_data * us,u16 pba,unsigned int page,unsigned int pages,unsigned char * data)722 static int alauda_read_block_raw(struct us_data *us, u16 pba,
723 		unsigned int page, unsigned int pages, unsigned char *data)
724 {
725 	int rc;
726 	unsigned char command[] = {
727 		ALAUDA_BULK_CMD, ALAUDA_BULK_READ_BLOCK, PBA_HI(pba),
728 		PBA_ZONE(pba), 0, PBA_LO(pba) + page, pages, 0, MEDIA_PORT(us)
729 	};
730 
731 	usb_stor_dbg(us, "pba %d page %d count %d\n", pba, page, pages);
732 
733 	rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
734 		command, 9, NULL);
735 	if (rc != USB_STOR_XFER_GOOD)
736 		return rc;
737 
738 	return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
739 		data, (MEDIA_INFO(us).pagesize + 64) * pages, NULL);
740 }
741 
742 /*
743  * Reads data from a certain offset page inside a PBA, excluding redundancy
744  * data. Returns pagesize*pages bytes in data. Note that data must be big enough
745  * to hold (pagesize+64)*pages bytes of data, but you can ignore those 'extra'
746  * trailing bytes outside this function.
747  */
alauda_read_block(struct us_data * us,u16 pba,unsigned int page,unsigned int pages,unsigned char * data)748 static int alauda_read_block(struct us_data *us, u16 pba,
749 		unsigned int page, unsigned int pages, unsigned char *data)
750 {
751 	int i, rc;
752 	unsigned int pagesize = MEDIA_INFO(us).pagesize;
753 
754 	rc = alauda_read_block_raw(us, pba, page, pages, data);
755 	if (rc != USB_STOR_XFER_GOOD)
756 		return rc;
757 
758 	/* Cut out the redundancy data */
759 	for (i = 0; i < pages; i++) {
760 		int dest_offset = i * pagesize;
761 		int src_offset = i * (pagesize + 64);
762 		memmove(data + dest_offset, data + src_offset, pagesize);
763 	}
764 
765 	return rc;
766 }
767 
768 /*
769  * Writes an entire block of data and checks status after write.
770  * Redundancy data must be already included in data. Data should be
771  * (pagesize+64)*blocksize bytes in length.
772  */
alauda_write_block(struct us_data * us,u16 pba,unsigned char * data)773 static int alauda_write_block(struct us_data *us, u16 pba, unsigned char *data)
774 {
775 	int rc;
776 	struct alauda_info *info = (struct alauda_info *) us->extra;
777 	unsigned char command[] = {
778 		ALAUDA_BULK_CMD, ALAUDA_BULK_WRITE_BLOCK, PBA_HI(pba),
779 		PBA_ZONE(pba), 0, PBA_LO(pba), 32, 0, MEDIA_PORT(us)
780 	};
781 
782 	usb_stor_dbg(us, "pba %d\n", pba);
783 
784 	rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
785 		command, 9, NULL);
786 	if (rc != USB_STOR_XFER_GOOD)
787 		return rc;
788 
789 	rc = usb_stor_bulk_transfer_buf(us, info->wr_ep, data,
790 		(MEDIA_INFO(us).pagesize + 64) * MEDIA_INFO(us).blocksize,
791 		NULL);
792 	if (rc != USB_STOR_XFER_GOOD)
793 		return rc;
794 
795 	return alauda_check_status2(us);
796 }
797 
798 /*
799  * Write some data to a specific LBA.
800  */
alauda_write_lba(struct us_data * us,u16 lba,unsigned int page,unsigned int pages,unsigned char * ptr,unsigned char * blockbuffer)801 static int alauda_write_lba(struct us_data *us, u16 lba,
802 		 unsigned int page, unsigned int pages,
803 		 unsigned char *ptr, unsigned char *blockbuffer)
804 {
805 	u16 pba, lbap, new_pba;
806 	unsigned char *bptr, *cptr, *xptr;
807 	unsigned char ecc[3];
808 	int i, result;
809 	unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
810 	unsigned int zonesize = MEDIA_INFO(us).zonesize;
811 	unsigned int pagesize = MEDIA_INFO(us).pagesize;
812 	unsigned int blocksize = MEDIA_INFO(us).blocksize;
813 	unsigned int lba_offset = lba % uzonesize;
814 	unsigned int new_pba_offset;
815 	unsigned int zone = lba / uzonesize;
816 
817 	alauda_ensure_map_for_zone(us, zone);
818 
819 	pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
820 	if (pba == 1) {
821 		/*
822 		 * Maybe it is impossible to write to PBA 1.
823 		 * Fake success, but don't do anything.
824 		 */
825 		printk(KERN_WARNING
826 		       "alauda_write_lba: avoid writing to pba 1\n");
827 		return USB_STOR_TRANSPORT_GOOD;
828 	}
829 
830 	new_pba = alauda_find_unused_pba(&MEDIA_INFO(us), zone);
831 	if (!new_pba) {
832 		printk(KERN_WARNING
833 		       "alauda_write_lba: Out of unused blocks\n");
834 		return USB_STOR_TRANSPORT_ERROR;
835 	}
836 
837 	/* read old contents */
838 	if (pba != UNDEF) {
839 		result = alauda_read_block_raw(us, pba, 0,
840 			blocksize, blockbuffer);
841 		if (result != USB_STOR_XFER_GOOD)
842 			return result;
843 	} else {
844 		memset(blockbuffer, 0, blocksize * (pagesize + 64));
845 	}
846 
847 	lbap = (lba_offset << 1) | 0x1000;
848 	if (parity[MSB_of(lbap) ^ LSB_of(lbap)])
849 		lbap ^= 1;
850 
851 	/* check old contents and fill lba */
852 	for (i = 0; i < blocksize; i++) {
853 		bptr = blockbuffer + (i * (pagesize + 64));
854 		cptr = bptr + pagesize;
855 		nand_compute_ecc(bptr, ecc);
856 		if (!nand_compare_ecc(cptr+13, ecc)) {
857 			usb_stor_dbg(us, "Warning: bad ecc in page %d- of pba %d\n",
858 				     i, pba);
859 			nand_store_ecc(cptr+13, ecc);
860 		}
861 		nand_compute_ecc(bptr + (pagesize / 2), ecc);
862 		if (!nand_compare_ecc(cptr+8, ecc)) {
863 			usb_stor_dbg(us, "Warning: bad ecc in page %d+ of pba %d\n",
864 				     i, pba);
865 			nand_store_ecc(cptr+8, ecc);
866 		}
867 		cptr[6] = cptr[11] = MSB_of(lbap);
868 		cptr[7] = cptr[12] = LSB_of(lbap);
869 	}
870 
871 	/* copy in new stuff and compute ECC */
872 	xptr = ptr;
873 	for (i = page; i < page+pages; i++) {
874 		bptr = blockbuffer + (i * (pagesize + 64));
875 		cptr = bptr + pagesize;
876 		memcpy(bptr, xptr, pagesize);
877 		xptr += pagesize;
878 		nand_compute_ecc(bptr, ecc);
879 		nand_store_ecc(cptr+13, ecc);
880 		nand_compute_ecc(bptr + (pagesize / 2), ecc);
881 		nand_store_ecc(cptr+8, ecc);
882 	}
883 
884 	result = alauda_write_block(us, new_pba, blockbuffer);
885 	if (result != USB_STOR_XFER_GOOD)
886 		return result;
887 
888 	new_pba_offset = new_pba - (zone * zonesize);
889 	MEDIA_INFO(us).pba_to_lba[zone][new_pba_offset] = lba;
890 	MEDIA_INFO(us).lba_to_pba[zone][lba_offset] = new_pba;
891 	usb_stor_dbg(us, "Remapped LBA %d to PBA %d\n", lba, new_pba);
892 
893 	if (pba != UNDEF) {
894 		unsigned int pba_offset = pba - (zone * zonesize);
895 		result = alauda_erase_block(us, pba);
896 		if (result != USB_STOR_XFER_GOOD)
897 			return result;
898 		MEDIA_INFO(us).pba_to_lba[zone][pba_offset] = UNDEF;
899 	}
900 
901 	return USB_STOR_TRANSPORT_GOOD;
902 }
903 
904 /*
905  * Read data from a specific sector address
906  */
alauda_read_data(struct us_data * us,unsigned long address,unsigned int sectors)907 static int alauda_read_data(struct us_data *us, unsigned long address,
908 		unsigned int sectors)
909 {
910 	unsigned char *buffer;
911 	u16 lba, max_lba;
912 	unsigned int page, len, offset;
913 	unsigned int blockshift = MEDIA_INFO(us).blockshift;
914 	unsigned int pageshift = MEDIA_INFO(us).pageshift;
915 	unsigned int blocksize = MEDIA_INFO(us).blocksize;
916 	unsigned int pagesize = MEDIA_INFO(us).pagesize;
917 	unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
918 	struct scatterlist *sg;
919 	int result;
920 
921 	/*
922 	 * Since we only read in one block at a time, we have to create
923 	 * a bounce buffer and move the data a piece at a time between the
924 	 * bounce buffer and the actual transfer buffer.
925 	 * We make this buffer big enough to hold temporary redundancy data,
926 	 * which we use when reading the data blocks.
927 	 */
928 
929 	len = min(sectors, blocksize) * (pagesize + 64);
930 	buffer = kmalloc(len, GFP_NOIO);
931 	if (!buffer)
932 		return USB_STOR_TRANSPORT_ERROR;
933 
934 	/* Figure out the initial LBA and page */
935 	lba = address >> blockshift;
936 	page = (address & MEDIA_INFO(us).blockmask);
937 	max_lba = MEDIA_INFO(us).capacity >> (blockshift + pageshift);
938 
939 	result = USB_STOR_TRANSPORT_GOOD;
940 	offset = 0;
941 	sg = NULL;
942 
943 	while (sectors > 0) {
944 		unsigned int zone = lba / uzonesize; /* integer division */
945 		unsigned int lba_offset = lba - (zone * uzonesize);
946 		unsigned int pages;
947 		u16 pba;
948 		alauda_ensure_map_for_zone(us, zone);
949 
950 		/* Not overflowing capacity? */
951 		if (lba >= max_lba) {
952 			usb_stor_dbg(us, "Error: Requested lba %u exceeds maximum %u\n",
953 				     lba, max_lba);
954 			result = USB_STOR_TRANSPORT_ERROR;
955 			break;
956 		}
957 
958 		/* Find number of pages we can read in this block */
959 		pages = min(sectors, blocksize - page);
960 		len = pages << pageshift;
961 
962 		/* Find where this lba lives on disk */
963 		pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
964 
965 		if (pba == UNDEF) {	/* this lba was never written */
966 			usb_stor_dbg(us, "Read %d zero pages (LBA %d) page %d\n",
967 				     pages, lba, page);
968 
969 			/*
970 			 * This is not really an error. It just means
971 			 * that the block has never been written.
972 			 * Instead of returning USB_STOR_TRANSPORT_ERROR
973 			 * it is better to return all zero data.
974 			 */
975 
976 			memset(buffer, 0, len);
977 		} else {
978 			usb_stor_dbg(us, "Read %d pages, from PBA %d (LBA %d) page %d\n",
979 				     pages, pba, lba, page);
980 
981 			result = alauda_read_block(us, pba, page, pages, buffer);
982 			if (result != USB_STOR_TRANSPORT_GOOD)
983 				break;
984 		}
985 
986 		/* Store the data in the transfer buffer */
987 		usb_stor_access_xfer_buf(buffer, len, us->srb,
988 				&sg, &offset, TO_XFER_BUF);
989 
990 		page = 0;
991 		lba++;
992 		sectors -= pages;
993 	}
994 
995 	kfree(buffer);
996 	return result;
997 }
998 
999 /*
1000  * Write data to a specific sector address
1001  */
alauda_write_data(struct us_data * us,unsigned long address,unsigned int sectors)1002 static int alauda_write_data(struct us_data *us, unsigned long address,
1003 		unsigned int sectors)
1004 {
1005 	unsigned char *buffer, *blockbuffer;
1006 	unsigned int page, len, offset;
1007 	unsigned int blockshift = MEDIA_INFO(us).blockshift;
1008 	unsigned int pageshift = MEDIA_INFO(us).pageshift;
1009 	unsigned int blocksize = MEDIA_INFO(us).blocksize;
1010 	unsigned int pagesize = MEDIA_INFO(us).pagesize;
1011 	struct scatterlist *sg;
1012 	u16 lba, max_lba;
1013 	int result;
1014 
1015 	/*
1016 	 * Since we don't write the user data directly to the device,
1017 	 * we have to create a bounce buffer and move the data a piece
1018 	 * at a time between the bounce buffer and the actual transfer buffer.
1019 	 */
1020 
1021 	len = min(sectors, blocksize) * pagesize;
1022 	buffer = kmalloc(len, GFP_NOIO);
1023 	if (!buffer)
1024 		return USB_STOR_TRANSPORT_ERROR;
1025 
1026 	/*
1027 	 * We also need a temporary block buffer, where we read in the old data,
1028 	 * overwrite parts with the new data, and manipulate the redundancy data
1029 	 */
1030 	blockbuffer = kmalloc_array(pagesize + 64, blocksize, GFP_NOIO);
1031 	if (!blockbuffer) {
1032 		kfree(buffer);
1033 		return USB_STOR_TRANSPORT_ERROR;
1034 	}
1035 
1036 	/* Figure out the initial LBA and page */
1037 	lba = address >> blockshift;
1038 	page = (address & MEDIA_INFO(us).blockmask);
1039 	max_lba = MEDIA_INFO(us).capacity >> (pageshift + blockshift);
1040 
1041 	result = USB_STOR_TRANSPORT_GOOD;
1042 	offset = 0;
1043 	sg = NULL;
1044 
1045 	while (sectors > 0) {
1046 		/* Write as many sectors as possible in this block */
1047 		unsigned int pages = min(sectors, blocksize - page);
1048 		len = pages << pageshift;
1049 
1050 		/* Not overflowing capacity? */
1051 		if (lba >= max_lba) {
1052 			usb_stor_dbg(us, "Requested lba %u exceeds maximum %u\n",
1053 				     lba, max_lba);
1054 			result = USB_STOR_TRANSPORT_ERROR;
1055 			break;
1056 		}
1057 
1058 		/* Get the data from the transfer buffer */
1059 		usb_stor_access_xfer_buf(buffer, len, us->srb,
1060 				&sg, &offset, FROM_XFER_BUF);
1061 
1062 		result = alauda_write_lba(us, lba, page, pages, buffer,
1063 			blockbuffer);
1064 		if (result != USB_STOR_TRANSPORT_GOOD)
1065 			break;
1066 
1067 		page = 0;
1068 		lba++;
1069 		sectors -= pages;
1070 	}
1071 
1072 	kfree(buffer);
1073 	kfree(blockbuffer);
1074 	return result;
1075 }
1076 
1077 /*
1078  * Our interface with the rest of the world
1079  */
1080 
alauda_info_destructor(void * extra)1081 static void alauda_info_destructor(void *extra)
1082 {
1083 	struct alauda_info *info = (struct alauda_info *) extra;
1084 	int port;
1085 
1086 	if (!info)
1087 		return;
1088 
1089 	for (port = 0; port < 2; port++) {
1090 		struct alauda_media_info *media_info = &info->port[port];
1091 
1092 		alauda_free_maps(media_info);
1093 		kfree(media_info->lba_to_pba);
1094 		kfree(media_info->pba_to_lba);
1095 	}
1096 }
1097 
1098 /*
1099  * Initialize alauda_info struct and find the data-write endpoint
1100  */
init_alauda(struct us_data * us)1101 static int init_alauda(struct us_data *us)
1102 {
1103 	struct alauda_info *info;
1104 	struct usb_host_interface *altsetting = us->pusb_intf->cur_altsetting;
1105 	nand_init_ecc();
1106 
1107 	us->extra = kzalloc(sizeof(struct alauda_info), GFP_NOIO);
1108 	if (!us->extra)
1109 		return -ENOMEM;
1110 
1111 	info = (struct alauda_info *) us->extra;
1112 	us->extra_destructor = alauda_info_destructor;
1113 
1114 	info->wr_ep = usb_sndbulkpipe(us->pusb_dev,
1115 		altsetting->endpoint[0].desc.bEndpointAddress
1116 		& USB_ENDPOINT_NUMBER_MASK);
1117 
1118 	return 0;
1119 }
1120 
alauda_transport(struct scsi_cmnd * srb,struct us_data * us)1121 static int alauda_transport(struct scsi_cmnd *srb, struct us_data *us)
1122 {
1123 	int rc;
1124 	struct alauda_info *info = (struct alauda_info *) us->extra;
1125 	unsigned char *ptr = us->iobuf;
1126 	static unsigned char inquiry_response[36] = {
1127 		0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1128 	};
1129 
1130 	if (srb->cmnd[0] == INQUIRY) {
1131 		usb_stor_dbg(us, "INQUIRY - Returning bogus response\n");
1132 		memcpy(ptr, inquiry_response, sizeof(inquiry_response));
1133 		fill_inquiry_response(us, ptr, 36);
1134 		return USB_STOR_TRANSPORT_GOOD;
1135 	}
1136 
1137 	if (srb->cmnd[0] == TEST_UNIT_READY) {
1138 		usb_stor_dbg(us, "TEST_UNIT_READY\n");
1139 		return alauda_check_media(us);
1140 	}
1141 
1142 	if (srb->cmnd[0] == READ_CAPACITY) {
1143 		unsigned int num_zones;
1144 		unsigned long capacity;
1145 
1146 		rc = alauda_check_media(us);
1147 		if (rc != USB_STOR_TRANSPORT_GOOD)
1148 			return rc;
1149 
1150 		num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift
1151 			+ MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift);
1152 
1153 		capacity = num_zones * MEDIA_INFO(us).uzonesize
1154 			* MEDIA_INFO(us).blocksize;
1155 
1156 		/* Report capacity and page size */
1157 		((__be32 *) ptr)[0] = cpu_to_be32(capacity - 1);
1158 		((__be32 *) ptr)[1] = cpu_to_be32(512);
1159 
1160 		usb_stor_set_xfer_buf(ptr, 8, srb);
1161 		return USB_STOR_TRANSPORT_GOOD;
1162 	}
1163 
1164 	if (srb->cmnd[0] == READ_10) {
1165 		unsigned int page, pages;
1166 
1167 		rc = alauda_check_media(us);
1168 		if (rc != USB_STOR_TRANSPORT_GOOD)
1169 			return rc;
1170 
1171 		page = short_pack(srb->cmnd[3], srb->cmnd[2]);
1172 		page <<= 16;
1173 		page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
1174 		pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
1175 
1176 		usb_stor_dbg(us, "READ_10: page %d pagect %d\n", page, pages);
1177 
1178 		return alauda_read_data(us, page, pages);
1179 	}
1180 
1181 	if (srb->cmnd[0] == WRITE_10) {
1182 		unsigned int page, pages;
1183 
1184 		rc = alauda_check_media(us);
1185 		if (rc != USB_STOR_TRANSPORT_GOOD)
1186 			return rc;
1187 
1188 		page = short_pack(srb->cmnd[3], srb->cmnd[2]);
1189 		page <<= 16;
1190 		page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
1191 		pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
1192 
1193 		usb_stor_dbg(us, "WRITE_10: page %d pagect %d\n", page, pages);
1194 
1195 		return alauda_write_data(us, page, pages);
1196 	}
1197 
1198 	if (srb->cmnd[0] == REQUEST_SENSE) {
1199 		usb_stor_dbg(us, "REQUEST_SENSE\n");
1200 
1201 		memset(ptr, 0, 18);
1202 		ptr[0] = 0xF0;
1203 		ptr[2] = info->sense_key;
1204 		ptr[7] = 11;
1205 		ptr[12] = info->sense_asc;
1206 		ptr[13] = info->sense_ascq;
1207 		usb_stor_set_xfer_buf(ptr, 18, srb);
1208 
1209 		return USB_STOR_TRANSPORT_GOOD;
1210 	}
1211 
1212 	if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
1213 		/*
1214 		 * sure.  whatever.  not like we can stop the user from popping
1215 		 * the media out of the device (no locking doors, etc)
1216 		 */
1217 		return USB_STOR_TRANSPORT_GOOD;
1218 	}
1219 
1220 	usb_stor_dbg(us, "Gah! Unknown command: %d (0x%x)\n",
1221 		     srb->cmnd[0], srb->cmnd[0]);
1222 	info->sense_key = 0x05;
1223 	info->sense_asc = 0x20;
1224 	info->sense_ascq = 0x00;
1225 	return USB_STOR_TRANSPORT_FAILED;
1226 }
1227 
1228 static struct scsi_host_template alauda_host_template;
1229 
alauda_probe(struct usb_interface * intf,const struct usb_device_id * id)1230 static int alauda_probe(struct usb_interface *intf,
1231 			 const struct usb_device_id *id)
1232 {
1233 	struct us_data *us;
1234 	int result;
1235 
1236 	result = usb_stor_probe1(&us, intf, id,
1237 			(id - alauda_usb_ids) + alauda_unusual_dev_list,
1238 			&alauda_host_template);
1239 	if (result)
1240 		return result;
1241 
1242 	us->transport_name  = "Alauda Control/Bulk";
1243 	us->transport = alauda_transport;
1244 	us->transport_reset = usb_stor_Bulk_reset;
1245 	us->max_lun = 1;
1246 
1247 	result = usb_stor_probe2(us);
1248 	return result;
1249 }
1250 
1251 static struct usb_driver alauda_driver = {
1252 	.name =		DRV_NAME,
1253 	.probe =	alauda_probe,
1254 	.disconnect =	usb_stor_disconnect,
1255 	.suspend =	usb_stor_suspend,
1256 	.resume =	usb_stor_resume,
1257 	.reset_resume =	usb_stor_reset_resume,
1258 	.pre_reset =	usb_stor_pre_reset,
1259 	.post_reset =	usb_stor_post_reset,
1260 	.id_table =	alauda_usb_ids,
1261 	.soft_unbind =	1,
1262 	.no_dynamic_id = 1,
1263 };
1264 
1265 module_usb_stor_driver(alauda_driver, alauda_host_template, DRV_NAME);
1266