2 * Driver for Alauda-based card readers
4 * Current development and maintenance by:
7 * The 'Alauda' is a chip manufacturered by RATOC for OEM use.
9 * Alauda implements a vendor-specific command set to access two media reader
10 * ports (XD, SmartMedia). This driver converts SCSI commands to the commands
11 * which are accepted by these devices.
13 * The driver was developed through reverse-engineering, with the help of the
14 * sddr09 driver which has many similarities, and with some help from the
15 * (very old) vendor-supplied GPL sma03 driver.
17 * For protocol info, see http://alauda.sourceforge.net
19 * This program is free software; you can redistribute it and/or modify it
20 * under the terms of the GNU General Public License as published by the
21 * Free Software Foundation; either version 2, or (at your option) any
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
29 * You should have received a copy of the GNU General Public License along
30 * with this program; if not, write to the Free Software Foundation, Inc.,
31 * 675 Mass Ave, Cambridge, MA 02139, USA.
34 #include <linux/module.h>
35 #include <linux/slab.h>
37 #include <scsi/scsi.h>
38 #include <scsi/scsi_cmnd.h>
39 #include <scsi/scsi_device.h>
42 #include "transport.h"
46 MODULE_DESCRIPTION("Driver for Alauda-based card readers");
48 MODULE_LICENSE("GPL");
53 #define ALAUDA_STATUS_ERROR 0x01
54 #define ALAUDA_STATUS_READY 0x40
57 * Control opcodes (for request field)
59 #define ALAUDA_GET_XD_MEDIA_STATUS 0x08
60 #define ALAUDA_GET_SM_MEDIA_STATUS 0x98
61 #define ALAUDA_ACK_XD_MEDIA_CHANGE 0x0a
62 #define ALAUDA_ACK_SM_MEDIA_CHANGE 0x9a
63 #define ALAUDA_GET_XD_MEDIA_SIG 0x86
64 #define ALAUDA_GET_SM_MEDIA_SIG 0x96
67 * Bulk command identity (byte 0)
69 #define ALAUDA_BULK_CMD 0x40
72 * Bulk opcodes (byte 1)
74 #define ALAUDA_BULK_GET_REDU_DATA 0x85
75 #define ALAUDA_BULK_READ_BLOCK 0x94
76 #define ALAUDA_BULK_ERASE_BLOCK 0xa3
77 #define ALAUDA_BULK_WRITE_BLOCK 0xb4
78 #define ALAUDA_BULK_GET_STATUS2 0xb7
79 #define ALAUDA_BULK_RESET_MEDIA 0xe0
82 * Port to operate on (byte 8)
84 #define ALAUDA_PORT_XD 0x00
85 #define ALAUDA_PORT_SM 0x01
88 * LBA and PBA are unsigned ints. Special values.
92 #define UNUSABLE 0xfffd
94 struct alauda_media_info {
95 unsigned long capacity; /* total media size in bytes */
96 unsigned int pagesize; /* page size in bytes */
97 unsigned int blocksize; /* number of pages per block */
98 unsigned int uzonesize; /* number of usable blocks per zone */
99 unsigned int zonesize; /* number of blocks per zone */
100 unsigned int blockmask; /* mask to get page from address */
102 unsigned char pageshift;
103 unsigned char blockshift;
104 unsigned char zoneshift;
106 u16 **lba_to_pba; /* logical to physical block map */
107 u16 **pba_to_lba; /* physical to logical block map */
111 struct alauda_media_info port[2];
112 int wr_ep; /* endpoint to write data out of */
114 unsigned char sense_key;
115 unsigned long sense_asc; /* additional sense code */
116 unsigned long sense_ascq; /* additional sense code qualifier */
119 #define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) )
120 #define LSB_of(s) ((s)&0xFF)
121 #define MSB_of(s) ((s)>>8)
123 #define MEDIA_PORT(us) us->srb->device->lun
124 #define MEDIA_INFO(us) ((struct alauda_info *)us->extra)->port[MEDIA_PORT(us)]
126 #define PBA_LO(pba) ((pba & 0xF) << 5)
127 #define PBA_HI(pba) (pba >> 3)
128 #define PBA_ZONE(pba) (pba >> 11)
130 static int init_alauda(struct us_data *us);
134 * The table of devices
136 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
137 vendorName, productName, useProtocol, useTransport, \
138 initFunction, flags) \
139 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
140 .driver_info = (flags) }
142 static struct usb_device_id alauda_usb_ids[] = {
143 # include "unusual_alauda.h"
144 { } /* Terminating entry */
146 MODULE_DEVICE_TABLE(usb, alauda_usb_ids);
153 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
154 vendor_name, product_name, use_protocol, use_transport, \
155 init_function, Flags) \
157 .vendorName = vendor_name, \
158 .productName = product_name, \
159 .useProtocol = use_protocol, \
160 .useTransport = use_transport, \
161 .initFunction = init_function, \
164 static struct us_unusual_dev alauda_unusual_dev_list[] = {
165 # include "unusual_alauda.h"
166 { } /* Terminating entry */
176 struct alauda_card_info {
177 unsigned char id; /* id byte */
178 unsigned char chipshift; /* 1<<cs bytes total capacity */
179 unsigned char pageshift; /* 1<<ps bytes in a page */
180 unsigned char blockshift; /* 1<<bs pages per block */
181 unsigned char zoneshift; /* 1<<zs blocks per zone */
184 static struct alauda_card_info alauda_card_ids[] = {
186 { 0x6e, 20, 8, 4, 8}, /* 1 MB */
187 { 0xe8, 20, 8, 4, 8}, /* 1 MB */
188 { 0xec, 20, 8, 4, 8}, /* 1 MB */
189 { 0x64, 21, 8, 4, 9}, /* 2 MB */
190 { 0xea, 21, 8, 4, 9}, /* 2 MB */
191 { 0x6b, 22, 9, 4, 9}, /* 4 MB */
192 { 0xe3, 22, 9, 4, 9}, /* 4 MB */
193 { 0xe5, 22, 9, 4, 9}, /* 4 MB */
194 { 0xe6, 23, 9, 4, 10}, /* 8 MB */
195 { 0x73, 24, 9, 5, 10}, /* 16 MB */
196 { 0x75, 25, 9, 5, 10}, /* 32 MB */
197 { 0x76, 26, 9, 5, 10}, /* 64 MB */
198 { 0x79, 27, 9, 5, 10}, /* 128 MB */
199 { 0x71, 28, 9, 5, 10}, /* 256 MB */
202 { 0x5d, 21, 9, 4, 8}, /* 2 MB */
203 { 0xd5, 22, 9, 4, 9}, /* 4 MB */
204 { 0xd6, 23, 9, 4, 10}, /* 8 MB */
205 { 0x57, 24, 9, 4, 11}, /* 16 MB */
206 { 0x58, 25, 9, 4, 12}, /* 32 MB */
210 static struct alauda_card_info *alauda_card_find_id(unsigned char id)
214 for (i = 0; alauda_card_ids[i].id != 0; i++)
215 if (alauda_card_ids[i].id == id)
216 return &(alauda_card_ids[i]);
224 static unsigned char parity[256];
225 static unsigned char ecc2[256];
227 static void nand_init_ecc(void)
232 for (i = 1; i < 256; i++)
233 parity[i] = (parity[i&(i-1)] ^ 1);
235 for (i = 0; i < 256; i++) {
237 for (j = 0; j < 8; j++) {
247 ecc2[i] = ~(a ^ (a<<1) ^ (parity[i] ? 0xa8 : 0));
251 /* compute 3-byte ecc on 256 bytes */
252 static void nand_compute_ecc(unsigned char *data, unsigned char *ecc)
255 unsigned char par = 0, bit, bits[8] = {0};
257 /* collect 16 checksum bits */
258 for (i = 0; i < 256; i++) {
260 bit = parity[data[i]];
261 for (j = 0; j < 8; j++)
262 if ((i & (1<<j)) == 0)
266 /* put 4+4+4 = 12 bits in the ecc */
267 a = (bits[3] << 6) + (bits[2] << 4) + (bits[1] << 2) + bits[0];
268 ecc[0] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
270 a = (bits[7] << 6) + (bits[6] << 4) + (bits[5] << 2) + bits[4];
271 ecc[1] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
276 static int nand_compare_ecc(unsigned char *data, unsigned char *ecc)
278 return (data[0] == ecc[0] && data[1] == ecc[1] && data[2] == ecc[2]);
281 static void nand_store_ecc(unsigned char *data, unsigned char *ecc)
283 memcpy(data, ecc, 3);
291 * Forget our PBA <---> LBA mappings for a particular port
293 static void alauda_free_maps (struct alauda_media_info *media_info)
295 unsigned int shift = media_info->zoneshift
296 + media_info->blockshift + media_info->pageshift;
297 unsigned int num_zones = media_info->capacity >> shift;
300 if (media_info->lba_to_pba != NULL)
301 for (i = 0; i < num_zones; i++) {
302 kfree(media_info->lba_to_pba[i]);
303 media_info->lba_to_pba[i] = NULL;
306 if (media_info->pba_to_lba != NULL)
307 for (i = 0; i < num_zones; i++) {
308 kfree(media_info->pba_to_lba[i]);
309 media_info->pba_to_lba[i] = NULL;
314 * Returns 2 bytes of status data
315 * The first byte describes media status, and second byte describes door status
317 static int alauda_get_media_status(struct us_data *us, unsigned char *data)
320 unsigned char command;
322 if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
323 command = ALAUDA_GET_XD_MEDIA_STATUS;
325 command = ALAUDA_GET_SM_MEDIA_STATUS;
327 rc = usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
328 command, 0xc0, 0, 1, data, 2);
330 usb_stor_dbg(us, "Media status %02X %02X\n", data[0], data[1]);
336 * Clears the "media was changed" bit so that we know when it changes again
339 static int alauda_ack_media(struct us_data *us)
341 unsigned char command;
343 if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
344 command = ALAUDA_ACK_XD_MEDIA_CHANGE;
346 command = ALAUDA_ACK_SM_MEDIA_CHANGE;
348 return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
349 command, 0x40, 0, 1, NULL, 0);
353 * Retrieves a 4-byte media signature, which indicates manufacturer, capacity,
354 * and some other details.
356 static int alauda_get_media_signature(struct us_data *us, unsigned char *data)
358 unsigned char command;
360 if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
361 command = ALAUDA_GET_XD_MEDIA_SIG;
363 command = ALAUDA_GET_SM_MEDIA_SIG;
365 return usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
366 command, 0xc0, 0, 0, data, 4);
370 * Resets the media status (but not the whole device?)
372 static int alauda_reset_media(struct us_data *us)
374 unsigned char *command = us->iobuf;
376 memset(command, 0, 9);
377 command[0] = ALAUDA_BULK_CMD;
378 command[1] = ALAUDA_BULK_RESET_MEDIA;
379 command[8] = MEDIA_PORT(us);
381 return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
386 * Examines the media and deduces capacity, etc.
388 static int alauda_init_media(struct us_data *us)
390 unsigned char *data = us->iobuf;
392 struct alauda_card_info *media_info;
393 unsigned int num_zones;
398 if (alauda_get_media_status(us, data) != USB_STOR_XFER_GOOD)
399 return USB_STOR_TRANSPORT_ERROR;
405 usb_stor_dbg(us, "We are ready for action!\n");
407 if (alauda_ack_media(us) != USB_STOR_XFER_GOOD)
408 return USB_STOR_TRANSPORT_ERROR;
412 if (alauda_get_media_status(us, data) != USB_STOR_XFER_GOOD)
413 return USB_STOR_TRANSPORT_ERROR;
415 if (data[0] != 0x14) {
416 usb_stor_dbg(us, "Media not ready after ack\n");
417 return USB_STOR_TRANSPORT_ERROR;
420 if (alauda_get_media_signature(us, data) != USB_STOR_XFER_GOOD)
421 return USB_STOR_TRANSPORT_ERROR;
423 usb_stor_dbg(us, "Media signature: %4ph\n", data);
424 media_info = alauda_card_find_id(data[1]);
425 if (media_info == NULL) {
426 pr_warn("alauda_init_media: Unrecognised media signature: %4ph\n",
428 return USB_STOR_TRANSPORT_ERROR;
431 MEDIA_INFO(us).capacity = 1 << media_info->chipshift;
432 usb_stor_dbg(us, "Found media with capacity: %ldMB\n",
433 MEDIA_INFO(us).capacity >> 20);
435 MEDIA_INFO(us).pageshift = media_info->pageshift;
436 MEDIA_INFO(us).blockshift = media_info->blockshift;
437 MEDIA_INFO(us).zoneshift = media_info->zoneshift;
439 MEDIA_INFO(us).pagesize = 1 << media_info->pageshift;
440 MEDIA_INFO(us).blocksize = 1 << media_info->blockshift;
441 MEDIA_INFO(us).zonesize = 1 << media_info->zoneshift;
443 MEDIA_INFO(us).uzonesize = ((1 << media_info->zoneshift) / 128) * 125;
444 MEDIA_INFO(us).blockmask = MEDIA_INFO(us).blocksize - 1;
446 num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift
447 + MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift);
448 MEDIA_INFO(us).pba_to_lba = kcalloc(num_zones, sizeof(u16*), GFP_NOIO);
449 MEDIA_INFO(us).lba_to_pba = kcalloc(num_zones, sizeof(u16*), GFP_NOIO);
451 if (alauda_reset_media(us) != USB_STOR_XFER_GOOD)
452 return USB_STOR_TRANSPORT_ERROR;
454 return USB_STOR_TRANSPORT_GOOD;
458 * Examines the media status and does the right thing when the media has gone,
459 * appeared, or changed.
461 static int alauda_check_media(struct us_data *us)
463 struct alauda_info *info = (struct alauda_info *) us->extra;
464 unsigned char status[2];
467 rc = alauda_get_media_status(us, status);
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) {
482 usb_stor_dbg(us, "Media change detected\n");
483 alauda_free_maps(&MEDIA_INFO(us));
484 alauda_init_media(us);
486 info->sense_key = UNIT_ATTENTION;
487 info->sense_asc = 0x28;
488 info->sense_ascq = 0x00;
489 return USB_STOR_TRANSPORT_FAILED;
492 return USB_STOR_TRANSPORT_GOOD;
496 * Checks the status from the 2nd status register
497 * Returns 3 bytes of status data, only the first is known
499 static int alauda_check_status2(struct us_data *us)
502 unsigned char command[] = {
503 ALAUDA_BULK_CMD, ALAUDA_BULK_GET_STATUS2,
504 0, 0, 0, 0, 3, 0, MEDIA_PORT(us)
506 unsigned char data[3];
508 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
510 if (rc != USB_STOR_XFER_GOOD)
513 rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
515 if (rc != USB_STOR_XFER_GOOD)
518 usb_stor_dbg(us, "%3ph\n", data);
519 if (data[0] & ALAUDA_STATUS_ERROR)
520 return USB_STOR_XFER_ERROR;
522 return USB_STOR_XFER_GOOD;
526 * Gets the redundancy data for the first page of a PBA
529 static int alauda_get_redu_data(struct us_data *us, u16 pba, unsigned char *data)
532 unsigned char command[] = {
533 ALAUDA_BULK_CMD, ALAUDA_BULK_GET_REDU_DATA,
534 PBA_HI(pba), PBA_ZONE(pba), 0, PBA_LO(pba), 0, 0, MEDIA_PORT(us)
537 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
539 if (rc != USB_STOR_XFER_GOOD)
542 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
547 * Finds the first unused PBA in a zone
548 * Returns the absolute PBA of an unused PBA, or 0 if none found.
550 static u16 alauda_find_unused_pba(struct alauda_media_info *info,
553 u16 *pba_to_lba = info->pba_to_lba[zone];
556 for (i = 0; i < info->zonesize; i++)
557 if (pba_to_lba[i] == UNDEF)
558 return (zone << info->zoneshift) + i;
564 * Reads the redundancy data for all PBA's in a zone
565 * Produces lba <--> pba mappings
567 static int alauda_read_map(struct us_data *us, unsigned int zone)
569 unsigned char *data = us->iobuf;
572 unsigned int zonesize = MEDIA_INFO(us).zonesize;
573 unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
574 unsigned int lba_offset, lba_real, blocknum;
575 unsigned int zone_base_lba = zone * uzonesize;
576 unsigned int zone_base_pba = zone * zonesize;
577 u16 *lba_to_pba = kcalloc(zonesize, sizeof(u16), GFP_NOIO);
578 u16 *pba_to_lba = kcalloc(zonesize, sizeof(u16), GFP_NOIO);
579 if (lba_to_pba == NULL || pba_to_lba == NULL) {
580 result = USB_STOR_TRANSPORT_ERROR;
584 usb_stor_dbg(us, "Mapping blocks for zone %d\n", zone);
586 /* 1024 PBA's per zone */
587 for (i = 0; i < zonesize; i++)
588 lba_to_pba[i] = pba_to_lba[i] = UNDEF;
590 for (i = 0; i < zonesize; i++) {
591 blocknum = zone_base_pba + i;
593 result = alauda_get_redu_data(us, blocknum, data);
594 if (result != USB_STOR_XFER_GOOD) {
595 result = USB_STOR_TRANSPORT_ERROR;
599 /* special PBAs have control field 0^16 */
600 for (j = 0; j < 16; j++)
603 pba_to_lba[i] = UNUSABLE;
604 usb_stor_dbg(us, "PBA %d has no logical mapping\n", blocknum);
608 /* unwritten PBAs have control field FF^16 */
609 for (j = 0; j < 16; j++)
615 /* normal PBAs start with six FFs */
617 usb_stor_dbg(us, "PBA %d has no logical mapping: reserved area = %02X%02X%02X%02X data status %02X block status %02X\n",
619 data[0], data[1], data[2], data[3],
621 pba_to_lba[i] = UNUSABLE;
625 if ((data[6] >> 4) != 0x01) {
626 usb_stor_dbg(us, "PBA %d has invalid address field %02X%02X/%02X%02X\n",
627 blocknum, data[6], data[7],
629 pba_to_lba[i] = UNUSABLE;
633 /* check even parity */
634 if (parity[data[6] ^ data[7]]) {
636 "alauda_read_map: Bad parity in LBA for block %d"
637 " (%02X %02X)\n", i, data[6], data[7]);
638 pba_to_lba[i] = UNUSABLE;
642 lba_offset = short_pack(data[7], data[6]);
643 lba_offset = (lba_offset & 0x07FF) >> 1;
644 lba_real = lba_offset + zone_base_lba;
647 * Every 1024 physical blocks ("zone"), the LBA numbers
648 * go back to zero, but are within a higher block of LBA's.
649 * Also, there is a maximum of 1000 LBA's per zone.
650 * In other words, in PBA 1024-2047 you will find LBA 0-999
651 * which are really LBA 1000-1999. This allows for 24 bad
652 * or special physical blocks per zone.
655 if (lba_offset >= uzonesize) {
657 "alauda_read_map: Bad low LBA %d for block %d\n",
662 if (lba_to_pba[lba_offset] != UNDEF) {
665 "LBA %d seen for PBA %d and %d\n",
666 lba_real, lba_to_pba[lba_offset], blocknum);
670 pba_to_lba[i] = lba_real;
671 lba_to_pba[lba_offset] = blocknum;
675 MEDIA_INFO(us).lba_to_pba[zone] = lba_to_pba;
676 MEDIA_INFO(us).pba_to_lba[zone] = pba_to_lba;
688 * Checks to see whether we have already mapped a certain zone
689 * If we haven't, the map is generated
691 static void alauda_ensure_map_for_zone(struct us_data *us, unsigned int zone)
693 if (MEDIA_INFO(us).lba_to_pba[zone] == NULL
694 || MEDIA_INFO(us).pba_to_lba[zone] == NULL)
695 alauda_read_map(us, zone);
699 * Erases an entire block
701 static int alauda_erase_block(struct us_data *us, u16 pba)
704 unsigned char command[] = {
705 ALAUDA_BULK_CMD, ALAUDA_BULK_ERASE_BLOCK, PBA_HI(pba),
706 PBA_ZONE(pba), 0, PBA_LO(pba), 0x02, 0, MEDIA_PORT(us)
708 unsigned char buf[2];
710 usb_stor_dbg(us, "Erasing PBA %d\n", pba);
712 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
714 if (rc != USB_STOR_XFER_GOOD)
717 rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
719 if (rc != USB_STOR_XFER_GOOD)
722 usb_stor_dbg(us, "Erase result: %02X %02X\n", buf[0], buf[1]);
727 * Reads data from a certain offset page inside a PBA, including interleaved
728 * redundancy data. Returns (pagesize+64)*pages bytes in data.
730 static int alauda_read_block_raw(struct us_data *us, u16 pba,
731 unsigned int page, unsigned int pages, unsigned char *data)
734 unsigned char command[] = {
735 ALAUDA_BULK_CMD, ALAUDA_BULK_READ_BLOCK, PBA_HI(pba),
736 PBA_ZONE(pba), 0, PBA_LO(pba) + page, pages, 0, MEDIA_PORT(us)
739 usb_stor_dbg(us, "pba %d page %d count %d\n", pba, page, pages);
741 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
743 if (rc != USB_STOR_XFER_GOOD)
746 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
747 data, (MEDIA_INFO(us).pagesize + 64) * pages, NULL);
751 * Reads data from a certain offset page inside a PBA, excluding redundancy
752 * data. Returns pagesize*pages bytes in data. Note that data must be big enough
753 * to hold (pagesize+64)*pages bytes of data, but you can ignore those 'extra'
754 * trailing bytes outside this function.
756 static int alauda_read_block(struct us_data *us, u16 pba,
757 unsigned int page, unsigned int pages, unsigned char *data)
760 unsigned int pagesize = MEDIA_INFO(us).pagesize;
762 rc = alauda_read_block_raw(us, pba, page, pages, data);
763 if (rc != USB_STOR_XFER_GOOD)
766 /* Cut out the redundancy data */
767 for (i = 0; i < pages; i++) {
768 int dest_offset = i * pagesize;
769 int src_offset = i * (pagesize + 64);
770 memmove(data + dest_offset, data + src_offset, pagesize);
777 * Writes an entire block of data and checks status after write.
778 * Redundancy data must be already included in data. Data should be
779 * (pagesize+64)*blocksize bytes in length.
781 static int alauda_write_block(struct us_data *us, u16 pba, unsigned char *data)
784 struct alauda_info *info = (struct alauda_info *) us->extra;
785 unsigned char command[] = {
786 ALAUDA_BULK_CMD, ALAUDA_BULK_WRITE_BLOCK, PBA_HI(pba),
787 PBA_ZONE(pba), 0, PBA_LO(pba), 32, 0, MEDIA_PORT(us)
790 usb_stor_dbg(us, "pba %d\n", pba);
792 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
794 if (rc != USB_STOR_XFER_GOOD)
797 rc = usb_stor_bulk_transfer_buf(us, info->wr_ep, data,
798 (MEDIA_INFO(us).pagesize + 64) * MEDIA_INFO(us).blocksize,
800 if (rc != USB_STOR_XFER_GOOD)
803 return alauda_check_status2(us);
807 * Write some data to a specific LBA.
809 static int alauda_write_lba(struct us_data *us, u16 lba,
810 unsigned int page, unsigned int pages,
811 unsigned char *ptr, unsigned char *blockbuffer)
813 u16 pba, lbap, new_pba;
814 unsigned char *bptr, *cptr, *xptr;
815 unsigned char ecc[3];
817 unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
818 unsigned int zonesize = MEDIA_INFO(us).zonesize;
819 unsigned int pagesize = MEDIA_INFO(us).pagesize;
820 unsigned int blocksize = MEDIA_INFO(us).blocksize;
821 unsigned int lba_offset = lba % uzonesize;
822 unsigned int new_pba_offset;
823 unsigned int zone = lba / uzonesize;
825 alauda_ensure_map_for_zone(us, zone);
827 pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
829 /* Maybe it is impossible to write to PBA 1.
830 Fake success, but don't do anything. */
832 "alauda_write_lba: avoid writing to pba 1\n");
833 return USB_STOR_TRANSPORT_GOOD;
836 new_pba = alauda_find_unused_pba(&MEDIA_INFO(us), zone);
839 "alauda_write_lba: Out of unused blocks\n");
840 return USB_STOR_TRANSPORT_ERROR;
843 /* read old contents */
845 result = alauda_read_block_raw(us, pba, 0,
846 blocksize, blockbuffer);
847 if (result != USB_STOR_XFER_GOOD)
850 memset(blockbuffer, 0, blocksize * (pagesize + 64));
853 lbap = (lba_offset << 1) | 0x1000;
854 if (parity[MSB_of(lbap) ^ LSB_of(lbap)])
857 /* check old contents and fill lba */
858 for (i = 0; i < blocksize; i++) {
859 bptr = blockbuffer + (i * (pagesize + 64));
860 cptr = bptr + pagesize;
861 nand_compute_ecc(bptr, ecc);
862 if (!nand_compare_ecc(cptr+13, ecc)) {
863 usb_stor_dbg(us, "Warning: bad ecc in page %d- of pba %d\n",
865 nand_store_ecc(cptr+13, ecc);
867 nand_compute_ecc(bptr + (pagesize / 2), ecc);
868 if (!nand_compare_ecc(cptr+8, ecc)) {
869 usb_stor_dbg(us, "Warning: bad ecc in page %d+ of pba %d\n",
871 nand_store_ecc(cptr+8, ecc);
873 cptr[6] = cptr[11] = MSB_of(lbap);
874 cptr[7] = cptr[12] = LSB_of(lbap);
877 /* copy in new stuff and compute ECC */
879 for (i = page; i < page+pages; i++) {
880 bptr = blockbuffer + (i * (pagesize + 64));
881 cptr = bptr + pagesize;
882 memcpy(bptr, xptr, pagesize);
884 nand_compute_ecc(bptr, ecc);
885 nand_store_ecc(cptr+13, ecc);
886 nand_compute_ecc(bptr + (pagesize / 2), ecc);
887 nand_store_ecc(cptr+8, ecc);
890 result = alauda_write_block(us, new_pba, blockbuffer);
891 if (result != USB_STOR_XFER_GOOD)
894 new_pba_offset = new_pba - (zone * zonesize);
895 MEDIA_INFO(us).pba_to_lba[zone][new_pba_offset] = lba;
896 MEDIA_INFO(us).lba_to_pba[zone][lba_offset] = new_pba;
897 usb_stor_dbg(us, "Remapped LBA %d to PBA %d\n", lba, new_pba);
900 unsigned int pba_offset = pba - (zone * zonesize);
901 result = alauda_erase_block(us, pba);
902 if (result != USB_STOR_XFER_GOOD)
904 MEDIA_INFO(us).pba_to_lba[zone][pba_offset] = UNDEF;
907 return USB_STOR_TRANSPORT_GOOD;
911 * Read data from a specific sector address
913 static int alauda_read_data(struct us_data *us, unsigned long address,
914 unsigned int sectors)
916 unsigned char *buffer;
918 unsigned int page, len, offset;
919 unsigned int blockshift = MEDIA_INFO(us).blockshift;
920 unsigned int pageshift = MEDIA_INFO(us).pageshift;
921 unsigned int blocksize = MEDIA_INFO(us).blocksize;
922 unsigned int pagesize = MEDIA_INFO(us).pagesize;
923 unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
924 struct scatterlist *sg;
928 * Since we only read in one block at a time, we have to create
929 * a bounce buffer and move the data a piece at a time between the
930 * bounce buffer and the actual transfer buffer.
931 * We make this buffer big enough to hold temporary redundancy data,
932 * which we use when reading the data blocks.
935 len = min(sectors, blocksize) * (pagesize + 64);
936 buffer = kmalloc(len, GFP_NOIO);
937 if (buffer == NULL) {
938 printk(KERN_WARNING "alauda_read_data: Out of memory\n");
939 return USB_STOR_TRANSPORT_ERROR;
942 /* Figure out the initial LBA and page */
943 lba = address >> blockshift;
944 page = (address & MEDIA_INFO(us).blockmask);
945 max_lba = MEDIA_INFO(us).capacity >> (blockshift + pageshift);
947 result = USB_STOR_TRANSPORT_GOOD;
951 while (sectors > 0) {
952 unsigned int zone = lba / uzonesize; /* integer division */
953 unsigned int lba_offset = lba - (zone * uzonesize);
956 alauda_ensure_map_for_zone(us, zone);
958 /* Not overflowing capacity? */
959 if (lba >= max_lba) {
960 usb_stor_dbg(us, "Error: Requested lba %u exceeds maximum %u\n",
962 result = USB_STOR_TRANSPORT_ERROR;
966 /* Find number of pages we can read in this block */
967 pages = min(sectors, blocksize - page);
968 len = pages << pageshift;
970 /* Find where this lba lives on disk */
971 pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
973 if (pba == UNDEF) { /* this lba was never written */
974 usb_stor_dbg(us, "Read %d zero pages (LBA %d) page %d\n",
977 /* This is not really an error. It just means
978 that the block has never been written.
979 Instead of returning USB_STOR_TRANSPORT_ERROR
980 it is better to return all zero data. */
982 memset(buffer, 0, len);
984 usb_stor_dbg(us, "Read %d pages, from PBA %d (LBA %d) page %d\n",
985 pages, pba, lba, page);
987 result = alauda_read_block(us, pba, page, pages, buffer);
988 if (result != USB_STOR_TRANSPORT_GOOD)
992 /* Store the data in the transfer buffer */
993 usb_stor_access_xfer_buf(buffer, len, us->srb,
994 &sg, &offset, TO_XFER_BUF);
1006 * Write data to a specific sector address
1008 static int alauda_write_data(struct us_data *us, unsigned long address,
1009 unsigned int sectors)
1011 unsigned char *buffer, *blockbuffer;
1012 unsigned int page, len, offset;
1013 unsigned int blockshift = MEDIA_INFO(us).blockshift;
1014 unsigned int pageshift = MEDIA_INFO(us).pageshift;
1015 unsigned int blocksize = MEDIA_INFO(us).blocksize;
1016 unsigned int pagesize = MEDIA_INFO(us).pagesize;
1017 struct scatterlist *sg;
1022 * Since we don't write the user data directly to the device,
1023 * we have to create a bounce buffer and move the data a piece
1024 * at a time between the bounce buffer and the actual transfer buffer.
1027 len = min(sectors, blocksize) * pagesize;
1028 buffer = kmalloc(len, GFP_NOIO);
1029 if (buffer == NULL) {
1030 printk(KERN_WARNING "alauda_write_data: Out of memory\n");
1031 return USB_STOR_TRANSPORT_ERROR;
1035 * We also need a temporary block buffer, where we read in the old data,
1036 * overwrite parts with the new data, and manipulate the redundancy data
1038 blockbuffer = kmalloc((pagesize + 64) * blocksize, GFP_NOIO);
1039 if (blockbuffer == NULL) {
1040 printk(KERN_WARNING "alauda_write_data: Out of memory\n");
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);
1118 return USB_STOR_TRANSPORT_ERROR;
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);
1127 return USB_STOR_TRANSPORT_GOOD;
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) {
1222 /* sure. whatever. not like we can stop the user from popping
1223 the media out of the device (no locking doors, etc) */
1224 return USB_STOR_TRANSPORT_GOOD;
1227 usb_stor_dbg(us, "Gah! Unknown command: %d (0x%x)\n",
1228 srb->cmnd[0], srb->cmnd[0]);
1229 info->sense_key = 0x05;
1230 info->sense_asc = 0x20;
1231 info->sense_ascq = 0x00;
1232 return USB_STOR_TRANSPORT_FAILED;
1235 static int alauda_probe(struct usb_interface *intf,
1236 const struct usb_device_id *id)
1241 result = usb_stor_probe1(&us, intf, id,
1242 (id - alauda_usb_ids) + alauda_unusual_dev_list);
1246 us->transport_name = "Alauda Control/Bulk";
1247 us->transport = alauda_transport;
1248 us->transport_reset = usb_stor_Bulk_reset;
1251 result = usb_stor_probe2(us);
1255 static struct usb_driver alauda_driver = {
1256 .name = "ums-alauda",
1257 .probe = alauda_probe,
1258 .disconnect = usb_stor_disconnect,
1259 .suspend = usb_stor_suspend,
1260 .resume = usb_stor_resume,
1261 .reset_resume = usb_stor_reset_resume,
1262 .pre_reset = usb_stor_pre_reset,
1263 .post_reset = usb_stor_post_reset,
1264 .id_table = alauda_usb_ids,
1269 module_usb_driver(alauda_driver);