1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver for Alauda-based card readers
5 * Current development and maintenance by:
8 * The 'Alauda' is a chip manufacturered by RATOC for OEM use.
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.
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.
18 * For protocol info, see http://alauda.sourceforge.net
21 #include <linux/module.h>
22 #include <linux/slab.h>
24 #include <scsi/scsi.h>
25 #include <scsi/scsi_cmnd.h>
26 #include <scsi/scsi_device.h>
29 #include "transport.h"
34 #define DRV_NAME "ums-alauda"
36 MODULE_DESCRIPTION("Driver for Alauda-based card readers");
38 MODULE_LICENSE("GPL");
39 MODULE_IMPORT_NS("USB_STORAGE");
44 #define ALAUDA_STATUS_ERROR 0x01
45 #define ALAUDA_STATUS_READY 0x40
48 * Control opcodes (for request field)
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
58 * Bulk command identity (byte 0)
60 #define ALAUDA_BULK_CMD 0x40
63 * Bulk opcodes (byte 1)
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
73 * Port to operate on (byte 8)
75 #define ALAUDA_PORT_XD 0x00
76 #define ALAUDA_PORT_SM 0x01
79 * LBA and PBA are unsigned ints. Special values.
83 #define UNUSABLE 0xfffd
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 */
93 unsigned char pageshift;
94 unsigned char blockshift;
95 unsigned char zoneshift;
97 u16 **lba_to_pba; /* logical to physical block map */
98 u16 **pba_to_lba; /* physical to logical block map */
102 struct alauda_media_info port[2];
103 int wr_ep; /* endpoint to write data out of */
105 unsigned char sense_key;
106 unsigned long sense_asc; /* additional sense code */
107 unsigned long sense_ascq; /* additional sense code qualifier */
109 bool media_initialized;
112 #define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) )
113 #define LSB_of(s) ((s)&0xFF)
114 #define MSB_of(s) ((s)>>8)
116 #define MEDIA_PORT(us) us->srb->device->lun
117 #define MEDIA_INFO(us) ((struct alauda_info *)us->extra)->port[MEDIA_PORT(us)]
119 #define PBA_LO(pba) ((pba & 0xF) << 5)
120 #define PBA_HI(pba) (pba >> 3)
121 #define PBA_ZONE(pba) (pba >> 11)
123 static int init_alauda(struct us_data *us);
127 * The table of devices
129 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
130 vendorName, productName, useProtocol, useTransport, \
131 initFunction, flags) \
132 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
133 .driver_info = (flags) }
135 static const struct usb_device_id alauda_usb_ids[] = {
136 # include "unusual_alauda.h"
137 { } /* Terminating entry */
139 MODULE_DEVICE_TABLE(usb, alauda_usb_ids);
146 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
147 vendor_name, product_name, use_protocol, use_transport, \
148 init_function, Flags) \
150 .vendorName = vendor_name, \
151 .productName = product_name, \
152 .useProtocol = use_protocol, \
153 .useTransport = use_transport, \
154 .initFunction = init_function, \
157 static const struct us_unusual_dev alauda_unusual_dev_list[] = {
158 # include "unusual_alauda.h"
159 { } /* Terminating entry */
169 struct alauda_card_info {
170 unsigned char id; /* id byte */
171 unsigned char chipshift; /* 1<<cs bytes total capacity */
172 unsigned char pageshift; /* 1<<ps bytes in a page */
173 unsigned char blockshift; /* 1<<bs pages per block */
174 unsigned char zoneshift; /* 1<<zs blocks per zone */
177 static struct alauda_card_info alauda_card_ids[] = {
179 { 0x6e, 20, 8, 4, 8}, /* 1 MB */
180 { 0xe8, 20, 8, 4, 8}, /* 1 MB */
181 { 0xec, 20, 8, 4, 8}, /* 1 MB */
182 { 0x64, 21, 8, 4, 9}, /* 2 MB */
183 { 0xea, 21, 8, 4, 9}, /* 2 MB */
184 { 0x6b, 22, 9, 4, 9}, /* 4 MB */
185 { 0xe3, 22, 9, 4, 9}, /* 4 MB */
186 { 0xe5, 22, 9, 4, 9}, /* 4 MB */
187 { 0xe6, 23, 9, 4, 10}, /* 8 MB */
188 { 0x73, 24, 9, 5, 10}, /* 16 MB */
189 { 0x75, 25, 9, 5, 10}, /* 32 MB */
190 { 0x76, 26, 9, 5, 10}, /* 64 MB */
191 { 0x79, 27, 9, 5, 10}, /* 128 MB */
192 { 0x71, 28, 9, 5, 10}, /* 256 MB */
195 { 0x5d, 21, 9, 4, 8}, /* 2 MB */
196 { 0xd5, 22, 9, 4, 9}, /* 4 MB */
197 { 0xd6, 23, 9, 4, 10}, /* 8 MB */
198 { 0x57, 24, 9, 4, 11}, /* 16 MB */
199 { 0x58, 25, 9, 4, 12}, /* 32 MB */
203 static struct alauda_card_info *alauda_card_find_id(unsigned char id)
207 for (i = 0; alauda_card_ids[i].id != 0; i++)
208 if (alauda_card_ids[i].id == id)
209 return &(alauda_card_ids[i]);
217 static unsigned char parity[256];
218 static unsigned char ecc2[256];
220 static void nand_init_ecc(void)
225 for (i = 1; i < 256; i++)
226 parity[i] = (parity[i&(i-1)] ^ 1);
228 for (i = 0; i < 256; i++) {
230 for (j = 0; j < 8; j++) {
240 ecc2[i] = ~(a ^ (a<<1) ^ (parity[i] ? 0xa8 : 0));
244 /* compute 3-byte ecc on 256 bytes */
245 static void nand_compute_ecc(unsigned char *data, unsigned char *ecc)
248 unsigned char par = 0, bit, bits[8] = {0};
250 /* collect 16 checksum bits */
251 for (i = 0; i < 256; i++) {
253 bit = parity[data[i]];
254 for (j = 0; j < 8; j++)
255 if ((i & (1<<j)) == 0)
259 /* put 4+4+4 = 12 bits in the ecc */
260 a = (bits[3] << 6) + (bits[2] << 4) + (bits[1] << 2) + bits[0];
261 ecc[0] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
263 a = (bits[7] << 6) + (bits[6] << 4) + (bits[5] << 2) + bits[4];
264 ecc[1] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
269 static int nand_compare_ecc(unsigned char *data, unsigned char *ecc)
271 return (data[0] == ecc[0] && data[1] == ecc[1] && data[2] == ecc[2]);
274 static void nand_store_ecc(unsigned char *data, unsigned char *ecc)
276 memcpy(data, ecc, 3);
284 * Forget our PBA <---> LBA mappings for a particular port
286 static void alauda_free_maps (struct alauda_media_info *media_info)
288 unsigned int shift = media_info->zoneshift
289 + media_info->blockshift + media_info->pageshift;
290 unsigned int num_zones = media_info->capacity >> shift;
293 if (media_info->lba_to_pba != NULL)
294 for (i = 0; i < num_zones; i++) {
295 kfree(media_info->lba_to_pba[i]);
296 media_info->lba_to_pba[i] = NULL;
299 if (media_info->pba_to_lba != NULL)
300 for (i = 0; i < num_zones; i++) {
301 kfree(media_info->pba_to_lba[i]);
302 media_info->pba_to_lba[i] = NULL;
307 * Returns 2 bytes of status data
308 * The first byte describes media status, and second byte describes door status
310 static int alauda_get_media_status(struct us_data *us, unsigned char *data)
313 unsigned char command;
315 if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
316 command = ALAUDA_GET_XD_MEDIA_STATUS;
318 command = ALAUDA_GET_SM_MEDIA_STATUS;
320 rc = usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
321 command, 0xc0, 0, 1, data, 2);
323 if (rc == USB_STOR_XFER_GOOD)
324 usb_stor_dbg(us, "Media status %02X %02X\n", data[0], data[1]);
330 * Clears the "media was changed" bit so that we know when it changes again
333 static int alauda_ack_media(struct us_data *us)
335 unsigned char command;
337 if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
338 command = ALAUDA_ACK_XD_MEDIA_CHANGE;
340 command = ALAUDA_ACK_SM_MEDIA_CHANGE;
342 return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
343 command, 0x40, 0, 1, NULL, 0);
347 * Retrieves a 4-byte media signature, which indicates manufacturer, capacity,
348 * and some other details.
350 static int alauda_get_media_signature(struct us_data *us, unsigned char *data)
352 unsigned char command;
354 if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
355 command = ALAUDA_GET_XD_MEDIA_SIG;
357 command = ALAUDA_GET_SM_MEDIA_SIG;
359 return usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
360 command, 0xc0, 0, 0, data, 4);
364 * Resets the media status (but not the whole device?)
366 static int alauda_reset_media(struct us_data *us)
368 unsigned char *command = us->iobuf;
370 memset(command, 0, 9);
371 command[0] = ALAUDA_BULK_CMD;
372 command[1] = ALAUDA_BULK_RESET_MEDIA;
373 command[8] = MEDIA_PORT(us);
375 return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
380 * Examines the media and deduces capacity, etc.
382 static int alauda_init_media(struct us_data *us)
384 unsigned char *data = us->iobuf;
386 struct alauda_card_info *media_info;
387 unsigned int num_zones;
392 if (alauda_get_media_status(us, data) != USB_STOR_XFER_GOOD)
393 return USB_STOR_TRANSPORT_ERROR;
399 usb_stor_dbg(us, "We are ready for action!\n");
401 if (alauda_ack_media(us) != USB_STOR_XFER_GOOD)
402 return USB_STOR_TRANSPORT_ERROR;
406 if (alauda_get_media_status(us, data) != USB_STOR_XFER_GOOD)
407 return USB_STOR_TRANSPORT_ERROR;
409 if (data[0] != 0x14) {
410 usb_stor_dbg(us, "Media not ready after ack\n");
411 return USB_STOR_TRANSPORT_ERROR;
414 if (alauda_get_media_signature(us, data) != USB_STOR_XFER_GOOD)
415 return USB_STOR_TRANSPORT_ERROR;
417 usb_stor_dbg(us, "Media signature: %4ph\n", data);
418 media_info = alauda_card_find_id(data[1]);
419 if (media_info == NULL) {
420 pr_warn("alauda_init_media: Unrecognised media signature: %4ph\n",
422 return USB_STOR_TRANSPORT_ERROR;
425 MEDIA_INFO(us).capacity = 1 << media_info->chipshift;
426 usb_stor_dbg(us, "Found media with capacity: %ldMB\n",
427 MEDIA_INFO(us).capacity >> 20);
429 MEDIA_INFO(us).pageshift = media_info->pageshift;
430 MEDIA_INFO(us).blockshift = media_info->blockshift;
431 MEDIA_INFO(us).zoneshift = media_info->zoneshift;
433 MEDIA_INFO(us).pagesize = 1 << media_info->pageshift;
434 MEDIA_INFO(us).blocksize = 1 << media_info->blockshift;
435 MEDIA_INFO(us).zonesize = 1 << media_info->zoneshift;
437 MEDIA_INFO(us).uzonesize = ((1 << media_info->zoneshift) / 128) * 125;
438 MEDIA_INFO(us).blockmask = MEDIA_INFO(us).blocksize - 1;
440 num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift
441 + MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift);
442 MEDIA_INFO(us).pba_to_lba = kcalloc(num_zones, sizeof(u16*), GFP_NOIO);
443 MEDIA_INFO(us).lba_to_pba = kcalloc(num_zones, sizeof(u16*), GFP_NOIO);
444 if (MEDIA_INFO(us).pba_to_lba == NULL || MEDIA_INFO(us).lba_to_pba == NULL)
445 return USB_STOR_TRANSPORT_ERROR;
447 if (alauda_reset_media(us) != USB_STOR_XFER_GOOD)
448 return USB_STOR_TRANSPORT_ERROR;
450 return USB_STOR_TRANSPORT_GOOD;
454 * Examines the media status and does the right thing when the media has gone,
455 * appeared, or changed.
457 static int alauda_check_media(struct us_data *us)
459 struct alauda_info *info = (struct alauda_info *) us->extra;
460 unsigned char *status = us->iobuf;
463 rc = alauda_get_media_status(us, status);
464 if (rc != USB_STOR_XFER_GOOD) {
465 status[0] = 0xF0; /* Pretend there's no media */
469 /* Check for no media or door open */
470 if ((status[0] & 0x80) || ((status[0] & 0x1F) == 0x10)
471 || ((status[1] & 0x01) == 0)) {
472 usb_stor_dbg(us, "No media, or door open\n");
473 alauda_free_maps(&MEDIA_INFO(us));
474 info->sense_key = 0x02;
475 info->sense_asc = 0x3A;
476 info->sense_ascq = 0x00;
477 return USB_STOR_TRANSPORT_FAILED;
480 /* Check for media change */
481 if (status[0] & 0x08 || !info->media_initialized) {
482 usb_stor_dbg(us, "Media change detected\n");
483 alauda_free_maps(&MEDIA_INFO(us));
484 rc = alauda_init_media(us);
485 if (rc == USB_STOR_TRANSPORT_GOOD)
486 info->media_initialized = true;
487 info->sense_key = UNIT_ATTENTION;
488 info->sense_asc = 0x28;
489 info->sense_ascq = 0x00;
490 return USB_STOR_TRANSPORT_FAILED;
493 return USB_STOR_TRANSPORT_GOOD;
497 * Checks the status from the 2nd status register
498 * Returns 3 bytes of status data, only the first is known
500 static int alauda_check_status2(struct us_data *us)
503 unsigned char command[] = {
504 ALAUDA_BULK_CMD, ALAUDA_BULK_GET_STATUS2,
505 0, 0, 0, 0, 3, 0, MEDIA_PORT(us)
507 unsigned char data[3];
509 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
511 if (rc != USB_STOR_XFER_GOOD)
514 rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
516 if (rc != USB_STOR_XFER_GOOD)
519 usb_stor_dbg(us, "%3ph\n", data);
520 if (data[0] & ALAUDA_STATUS_ERROR)
521 return USB_STOR_XFER_ERROR;
523 return USB_STOR_XFER_GOOD;
527 * Gets the redundancy data for the first page of a PBA
530 static int alauda_get_redu_data(struct us_data *us, u16 pba, unsigned char *data)
533 unsigned char command[] = {
534 ALAUDA_BULK_CMD, ALAUDA_BULK_GET_REDU_DATA,
535 PBA_HI(pba), PBA_ZONE(pba), 0, PBA_LO(pba), 0, 0, MEDIA_PORT(us)
538 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
540 if (rc != USB_STOR_XFER_GOOD)
543 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
548 * Finds the first unused PBA in a zone
549 * Returns the absolute PBA of an unused PBA, or 0 if none found.
551 static u16 alauda_find_unused_pba(struct alauda_media_info *info,
554 u16 *pba_to_lba = info->pba_to_lba[zone];
557 for (i = 0; i < info->zonesize; i++)
558 if (pba_to_lba[i] == UNDEF)
559 return (zone << info->zoneshift) + i;
565 * Reads the redundancy data for all PBA's in a zone
566 * Produces lba <--> pba mappings
568 static int alauda_read_map(struct us_data *us, unsigned int zone)
570 unsigned char *data = us->iobuf;
573 unsigned int zonesize = MEDIA_INFO(us).zonesize;
574 unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
575 unsigned int lba_offset, lba_real, blocknum;
576 unsigned int zone_base_lba = zone * uzonesize;
577 unsigned int zone_base_pba = zone * zonesize;
578 u16 *lba_to_pba = kcalloc(zonesize, sizeof(u16), GFP_NOIO);
579 u16 *pba_to_lba = kcalloc(zonesize, sizeof(u16), GFP_NOIO);
580 if (lba_to_pba == NULL || pba_to_lba == NULL) {
581 result = USB_STOR_TRANSPORT_ERROR;
585 usb_stor_dbg(us, "Mapping blocks for zone %d\n", zone);
587 /* 1024 PBA's per zone */
588 for (i = 0; i < zonesize; i++)
589 lba_to_pba[i] = pba_to_lba[i] = UNDEF;
591 for (i = 0; i < zonesize; i++) {
592 blocknum = zone_base_pba + i;
594 result = alauda_get_redu_data(us, blocknum, data);
595 if (result != USB_STOR_XFER_GOOD) {
596 result = USB_STOR_TRANSPORT_ERROR;
600 /* special PBAs have control field 0^16 */
601 for (j = 0; j < 16; j++)
604 pba_to_lba[i] = UNUSABLE;
605 usb_stor_dbg(us, "PBA %d has no logical mapping\n", blocknum);
609 /* unwritten PBAs have control field FF^16 */
610 for (j = 0; j < 16; j++)
616 /* normal PBAs start with six FFs */
618 usb_stor_dbg(us, "PBA %d has no logical mapping: reserved area = %02X%02X%02X%02X data status %02X block status %02X\n",
620 data[0], data[1], data[2], data[3],
622 pba_to_lba[i] = UNUSABLE;
626 if ((data[6] >> 4) != 0x01) {
627 usb_stor_dbg(us, "PBA %d has invalid address field %02X%02X/%02X%02X\n",
628 blocknum, data[6], data[7],
630 pba_to_lba[i] = UNUSABLE;
634 /* check even parity */
635 if (parity[data[6] ^ data[7]]) {
637 "alauda_read_map: Bad parity in LBA for block %d"
638 " (%02X %02X)\n", i, data[6], data[7]);
639 pba_to_lba[i] = UNUSABLE;
643 lba_offset = short_pack(data[7], data[6]);
644 lba_offset = (lba_offset & 0x07FF) >> 1;
645 lba_real = lba_offset + zone_base_lba;
648 * Every 1024 physical blocks ("zone"), the LBA numbers
649 * go back to zero, but are within a higher block of LBA's.
650 * Also, there is a maximum of 1000 LBA's per zone.
651 * In other words, in PBA 1024-2047 you will find LBA 0-999
652 * which are really LBA 1000-1999. This allows for 24 bad
653 * or special physical blocks per zone.
656 if (lba_offset >= uzonesize) {
658 "alauda_read_map: Bad low LBA %d for block %d\n",
663 if (lba_to_pba[lba_offset] != UNDEF) {
666 "LBA %d seen for PBA %d and %d\n",
667 lba_real, lba_to_pba[lba_offset], blocknum);
671 pba_to_lba[i] = lba_real;
672 lba_to_pba[lba_offset] = blocknum;
676 MEDIA_INFO(us).lba_to_pba[zone] = lba_to_pba;
677 MEDIA_INFO(us).pba_to_lba[zone] = pba_to_lba;
689 * Checks to see whether we have already mapped a certain zone
690 * If we haven't, the map is generated
692 static void alauda_ensure_map_for_zone(struct us_data *us, unsigned int zone)
694 if (MEDIA_INFO(us).lba_to_pba[zone] == NULL
695 || MEDIA_INFO(us).pba_to_lba[zone] == NULL)
696 alauda_read_map(us, zone);
700 * Erases an entire block
702 static int alauda_erase_block(struct us_data *us, u16 pba)
705 unsigned char command[] = {
706 ALAUDA_BULK_CMD, ALAUDA_BULK_ERASE_BLOCK, PBA_HI(pba),
707 PBA_ZONE(pba), 0, PBA_LO(pba), 0x02, 0, MEDIA_PORT(us)
709 unsigned char buf[2];
711 usb_stor_dbg(us, "Erasing PBA %d\n", pba);
713 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
715 if (rc != USB_STOR_XFER_GOOD)
718 rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
720 if (rc != USB_STOR_XFER_GOOD)
723 usb_stor_dbg(us, "Erase result: %02X %02X\n", buf[0], buf[1]);
728 * Reads data from a certain offset page inside a PBA, including interleaved
729 * redundancy data. Returns (pagesize+64)*pages bytes in data.
731 static int alauda_read_block_raw(struct us_data *us, u16 pba,
732 unsigned int page, unsigned int pages, unsigned char *data)
735 unsigned char command[] = {
736 ALAUDA_BULK_CMD, ALAUDA_BULK_READ_BLOCK, PBA_HI(pba),
737 PBA_ZONE(pba), 0, PBA_LO(pba) + page, pages, 0, MEDIA_PORT(us)
740 usb_stor_dbg(us, "pba %d page %d count %d\n", pba, page, pages);
742 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
744 if (rc != USB_STOR_XFER_GOOD)
747 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
748 data, (MEDIA_INFO(us).pagesize + 64) * pages, NULL);
752 * Reads data from a certain offset page inside a PBA, excluding redundancy
753 * data. Returns pagesize*pages bytes in data. Note that data must be big enough
754 * to hold (pagesize+64)*pages bytes of data, but you can ignore those 'extra'
755 * trailing bytes outside this function.
757 static int alauda_read_block(struct us_data *us, u16 pba,
758 unsigned int page, unsigned int pages, unsigned char *data)
761 unsigned int pagesize = MEDIA_INFO(us).pagesize;
763 rc = alauda_read_block_raw(us, pba, page, pages, data);
764 if (rc != USB_STOR_XFER_GOOD)
767 /* Cut out the redundancy data */
768 for (i = 0; i < pages; i++) {
769 int dest_offset = i * pagesize;
770 int src_offset = i * (pagesize + 64);
771 memmove(data + dest_offset, data + src_offset, pagesize);
778 * Writes an entire block of data and checks status after write.
779 * Redundancy data must be already included in data. Data should be
780 * (pagesize+64)*blocksize bytes in length.
782 static int alauda_write_block(struct us_data *us, u16 pba, unsigned char *data)
785 struct alauda_info *info = (struct alauda_info *) us->extra;
786 unsigned char command[] = {
787 ALAUDA_BULK_CMD, ALAUDA_BULK_WRITE_BLOCK, PBA_HI(pba),
788 PBA_ZONE(pba), 0, PBA_LO(pba), 32, 0, MEDIA_PORT(us)
791 usb_stor_dbg(us, "pba %d\n", pba);
793 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
795 if (rc != USB_STOR_XFER_GOOD)
798 rc = usb_stor_bulk_transfer_buf(us, info->wr_ep, data,
799 (MEDIA_INFO(us).pagesize + 64) * MEDIA_INFO(us).blocksize,
801 if (rc != USB_STOR_XFER_GOOD)
804 return alauda_check_status2(us);
808 * Write some data to a specific LBA.
810 static int alauda_write_lba(struct us_data *us, u16 lba,
811 unsigned int page, unsigned int pages,
812 unsigned char *ptr, unsigned char *blockbuffer)
814 u16 pba, lbap, new_pba;
815 unsigned char *bptr, *cptr, *xptr;
816 unsigned char ecc[3];
818 unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
819 unsigned int zonesize = MEDIA_INFO(us).zonesize;
820 unsigned int pagesize = MEDIA_INFO(us).pagesize;
821 unsigned int blocksize = MEDIA_INFO(us).blocksize;
822 unsigned int lba_offset = lba % uzonesize;
823 unsigned int new_pba_offset;
824 unsigned int zone = lba / uzonesize;
826 alauda_ensure_map_for_zone(us, zone);
828 pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
831 * Maybe it is impossible to write to PBA 1.
832 * Fake success, but don't do anything.
835 "alauda_write_lba: avoid writing to pba 1\n");
836 return USB_STOR_TRANSPORT_GOOD;
839 new_pba = alauda_find_unused_pba(&MEDIA_INFO(us), zone);
842 "alauda_write_lba: Out of unused blocks\n");
843 return USB_STOR_TRANSPORT_ERROR;
846 /* read old contents */
848 result = alauda_read_block_raw(us, pba, 0,
849 blocksize, blockbuffer);
850 if (result != USB_STOR_XFER_GOOD)
853 memset(blockbuffer, 0, blocksize * (pagesize + 64));
856 lbap = (lba_offset << 1) | 0x1000;
857 if (parity[MSB_of(lbap) ^ LSB_of(lbap)])
860 /* check old contents and fill lba */
861 for (i = 0; i < blocksize; i++) {
862 bptr = blockbuffer + (i * (pagesize + 64));
863 cptr = bptr + pagesize;
864 nand_compute_ecc(bptr, ecc);
865 if (!nand_compare_ecc(cptr+13, ecc)) {
866 usb_stor_dbg(us, "Warning: bad ecc in page %d- of pba %d\n",
868 nand_store_ecc(cptr+13, ecc);
870 nand_compute_ecc(bptr + (pagesize / 2), ecc);
871 if (!nand_compare_ecc(cptr+8, ecc)) {
872 usb_stor_dbg(us, "Warning: bad ecc in page %d+ of pba %d\n",
874 nand_store_ecc(cptr+8, ecc);
876 cptr[6] = cptr[11] = MSB_of(lbap);
877 cptr[7] = cptr[12] = LSB_of(lbap);
880 /* copy in new stuff and compute ECC */
882 for (i = page; i < page+pages; i++) {
883 bptr = blockbuffer + (i * (pagesize + 64));
884 cptr = bptr + pagesize;
885 memcpy(bptr, xptr, pagesize);
887 nand_compute_ecc(bptr, ecc);
888 nand_store_ecc(cptr+13, ecc);
889 nand_compute_ecc(bptr + (pagesize / 2), ecc);
890 nand_store_ecc(cptr+8, ecc);
893 result = alauda_write_block(us, new_pba, blockbuffer);
894 if (result != USB_STOR_XFER_GOOD)
897 new_pba_offset = new_pba - (zone * zonesize);
898 MEDIA_INFO(us).pba_to_lba[zone][new_pba_offset] = lba;
899 MEDIA_INFO(us).lba_to_pba[zone][lba_offset] = new_pba;
900 usb_stor_dbg(us, "Remapped LBA %d to PBA %d\n", lba, new_pba);
903 unsigned int pba_offset = pba - (zone * zonesize);
904 result = alauda_erase_block(us, pba);
905 if (result != USB_STOR_XFER_GOOD)
907 MEDIA_INFO(us).pba_to_lba[zone][pba_offset] = UNDEF;
910 return USB_STOR_TRANSPORT_GOOD;
914 * Read data from a specific sector address
916 static int alauda_read_data(struct us_data *us, unsigned long address,
917 unsigned int sectors)
919 unsigned char *buffer;
921 unsigned int page, len, offset;
922 unsigned int blockshift = MEDIA_INFO(us).blockshift;
923 unsigned int pageshift = MEDIA_INFO(us).pageshift;
924 unsigned int blocksize = MEDIA_INFO(us).blocksize;
925 unsigned int pagesize = MEDIA_INFO(us).pagesize;
926 unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
927 struct scatterlist *sg;
931 * Since we only read in one block at a time, we have to create
932 * a bounce buffer and move the data a piece at a time between the
933 * bounce buffer and the actual transfer buffer.
934 * We make this buffer big enough to hold temporary redundancy data,
935 * which we use when reading the data blocks.
938 len = min(sectors, blocksize) * (pagesize + 64);
939 buffer = kmalloc(len, GFP_NOIO);
941 return USB_STOR_TRANSPORT_ERROR;
943 /* Figure out the initial LBA and page */
944 lba = address >> blockshift;
945 page = (address & MEDIA_INFO(us).blockmask);
946 max_lba = MEDIA_INFO(us).capacity >> (blockshift + pageshift);
948 result = USB_STOR_TRANSPORT_GOOD;
952 while (sectors > 0) {
953 unsigned int zone = lba / uzonesize; /* integer division */
954 unsigned int lba_offset = lba - (zone * uzonesize);
957 alauda_ensure_map_for_zone(us, zone);
959 /* Not overflowing capacity? */
960 if (lba >= max_lba) {
961 usb_stor_dbg(us, "Error: Requested lba %u exceeds maximum %u\n",
963 result = USB_STOR_TRANSPORT_ERROR;
967 /* Find number of pages we can read in this block */
968 pages = min(sectors, blocksize - page);
969 len = pages << pageshift;
971 /* Find where this lba lives on disk */
972 pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
974 if (pba == UNDEF) { /* this lba was never written */
975 usb_stor_dbg(us, "Read %d zero pages (LBA %d) page %d\n",
979 * This is not really an error. It just means
980 * that the block has never been written.
981 * Instead of returning USB_STOR_TRANSPORT_ERROR
982 * it is better to return all zero data.
985 memset(buffer, 0, len);
987 usb_stor_dbg(us, "Read %d pages, from PBA %d (LBA %d) page %d\n",
988 pages, pba, lba, page);
990 result = alauda_read_block(us, pba, page, pages, buffer);
991 if (result != USB_STOR_TRANSPORT_GOOD)
995 /* Store the data in the transfer buffer */
996 usb_stor_access_xfer_buf(buffer, len, us->srb,
997 &sg, &offset, TO_XFER_BUF);
1009 * Write data to a specific sector address
1011 static int alauda_write_data(struct us_data *us, unsigned long address,
1012 unsigned int sectors)
1014 unsigned char *buffer, *blockbuffer;
1015 unsigned int page, len, offset;
1016 unsigned int blockshift = MEDIA_INFO(us).blockshift;
1017 unsigned int pageshift = MEDIA_INFO(us).pageshift;
1018 unsigned int blocksize = MEDIA_INFO(us).blocksize;
1019 unsigned int pagesize = MEDIA_INFO(us).pagesize;
1020 struct scatterlist *sg;
1025 * Since we don't write the user data directly to the device,
1026 * we have to create a bounce buffer and move the data a piece
1027 * at a time between the bounce buffer and the actual transfer buffer.
1030 len = min(sectors, blocksize) * pagesize;
1031 buffer = kmalloc(len, GFP_NOIO);
1033 return USB_STOR_TRANSPORT_ERROR;
1036 * We also need a temporary block buffer, where we read in the old data,
1037 * overwrite parts with the new data, and manipulate the redundancy data
1039 blockbuffer = kmalloc_array(pagesize + 64, blocksize, GFP_NOIO);
1042 return USB_STOR_TRANSPORT_ERROR;
1045 /* Figure out the initial LBA and page */
1046 lba = address >> blockshift;
1047 page = (address & MEDIA_INFO(us).blockmask);
1048 max_lba = MEDIA_INFO(us).capacity >> (pageshift + blockshift);
1050 result = USB_STOR_TRANSPORT_GOOD;
1054 while (sectors > 0) {
1055 /* Write as many sectors as possible in this block */
1056 unsigned int pages = min(sectors, blocksize - page);
1057 len = pages << pageshift;
1059 /* Not overflowing capacity? */
1060 if (lba >= max_lba) {
1061 usb_stor_dbg(us, "Requested lba %u exceeds maximum %u\n",
1063 result = USB_STOR_TRANSPORT_ERROR;
1067 /* Get the data from the transfer buffer */
1068 usb_stor_access_xfer_buf(buffer, len, us->srb,
1069 &sg, &offset, FROM_XFER_BUF);
1071 result = alauda_write_lba(us, lba, page, pages, buffer,
1073 if (result != USB_STOR_TRANSPORT_GOOD)
1087 * Our interface with the rest of the world
1090 static void alauda_info_destructor(void *extra)
1092 struct alauda_info *info = (struct alauda_info *) extra;
1098 for (port = 0; port < 2; port++) {
1099 struct alauda_media_info *media_info = &info->port[port];
1101 alauda_free_maps(media_info);
1102 kfree(media_info->lba_to_pba);
1103 kfree(media_info->pba_to_lba);
1108 * Initialize alauda_info struct and find the data-write endpoint
1110 static int init_alauda(struct us_data *us)
1112 struct alauda_info *info;
1113 struct usb_host_interface *altsetting = us->pusb_intf->cur_altsetting;
1116 us->extra = kzalloc(sizeof(struct alauda_info), GFP_NOIO);
1120 info = (struct alauda_info *) us->extra;
1121 us->extra_destructor = alauda_info_destructor;
1123 info->wr_ep = usb_sndbulkpipe(us->pusb_dev,
1124 altsetting->endpoint[0].desc.bEndpointAddress
1125 & USB_ENDPOINT_NUMBER_MASK);
1130 static int alauda_transport(struct scsi_cmnd *srb, struct us_data *us)
1133 struct alauda_info *info = (struct alauda_info *) us->extra;
1134 unsigned char *ptr = us->iobuf;
1135 static unsigned char inquiry_response[36] = {
1136 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1139 if (srb->cmnd[0] == INQUIRY) {
1140 usb_stor_dbg(us, "INQUIRY - Returning bogus response\n");
1141 memcpy(ptr, inquiry_response, sizeof(inquiry_response));
1142 fill_inquiry_response(us, ptr, 36);
1143 return USB_STOR_TRANSPORT_GOOD;
1146 if (srb->cmnd[0] == TEST_UNIT_READY) {
1147 usb_stor_dbg(us, "TEST_UNIT_READY\n");
1148 return alauda_check_media(us);
1151 if (srb->cmnd[0] == READ_CAPACITY) {
1152 unsigned int num_zones;
1153 unsigned long capacity;
1155 rc = alauda_check_media(us);
1156 if (rc != USB_STOR_TRANSPORT_GOOD)
1159 num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift
1160 + MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift);
1162 capacity = num_zones * MEDIA_INFO(us).uzonesize
1163 * MEDIA_INFO(us).blocksize;
1165 /* Report capacity and page size */
1166 ((__be32 *) ptr)[0] = cpu_to_be32(capacity - 1);
1167 ((__be32 *) ptr)[1] = cpu_to_be32(512);
1169 usb_stor_set_xfer_buf(ptr, 8, srb);
1170 return USB_STOR_TRANSPORT_GOOD;
1173 if (srb->cmnd[0] == READ_10) {
1174 unsigned int page, pages;
1176 rc = alauda_check_media(us);
1177 if (rc != USB_STOR_TRANSPORT_GOOD)
1180 page = short_pack(srb->cmnd[3], srb->cmnd[2]);
1182 page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
1183 pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
1185 usb_stor_dbg(us, "READ_10: page %d pagect %d\n", page, pages);
1187 return alauda_read_data(us, page, pages);
1190 if (srb->cmnd[0] == WRITE_10) {
1191 unsigned int page, pages;
1193 rc = alauda_check_media(us);
1194 if (rc != USB_STOR_TRANSPORT_GOOD)
1197 page = short_pack(srb->cmnd[3], srb->cmnd[2]);
1199 page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
1200 pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
1202 usb_stor_dbg(us, "WRITE_10: page %d pagect %d\n", page, pages);
1204 return alauda_write_data(us, page, pages);
1207 if (srb->cmnd[0] == REQUEST_SENSE) {
1208 usb_stor_dbg(us, "REQUEST_SENSE\n");
1212 ptr[2] = info->sense_key;
1214 ptr[12] = info->sense_asc;
1215 ptr[13] = info->sense_ascq;
1216 usb_stor_set_xfer_buf(ptr, 18, srb);
1218 return USB_STOR_TRANSPORT_GOOD;
1221 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
1223 * sure. whatever. not like we can stop the user from popping
1224 * the media out of the device (no locking doors, etc)
1226 return USB_STOR_TRANSPORT_GOOD;
1229 usb_stor_dbg(us, "Gah! Unknown command: %d (0x%x)\n",
1230 srb->cmnd[0], srb->cmnd[0]);
1231 info->sense_key = 0x05;
1232 info->sense_asc = 0x20;
1233 info->sense_ascq = 0x00;
1234 return USB_STOR_TRANSPORT_FAILED;
1237 static struct scsi_host_template alauda_host_template;
1239 static int alauda_probe(struct usb_interface *intf,
1240 const struct usb_device_id *id)
1245 result = usb_stor_probe1(&us, intf, id,
1246 (id - alauda_usb_ids) + alauda_unusual_dev_list,
1247 &alauda_host_template);
1251 us->transport_name = "Alauda Control/Bulk";
1252 us->transport = alauda_transport;
1253 us->transport_reset = usb_stor_Bulk_reset;
1256 result = usb_stor_probe2(us);
1260 static struct usb_driver alauda_driver = {
1262 .probe = alauda_probe,
1263 .disconnect = usb_stor_disconnect,
1264 .suspend = usb_stor_suspend,
1265 .resume = usb_stor_resume,
1266 .reset_resume = usb_stor_reset_resume,
1267 .pre_reset = usb_stor_pre_reset,
1268 .post_reset = usb_stor_post_reset,
1269 .id_table = alauda_usb_ids,
1274 module_usb_stor_driver(alauda_driver, alauda_host_template, DRV_NAME);