1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver for Alauda-based card readers
5 * Current development and maintenance by:
6 * (c) 2005 Daniel Drake <dsd@gentoo.org>
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");
37 MODULE_AUTHOR("Daniel Drake <dsd@gentoo.org>");
38 MODULE_LICENSE("GPL");
43 #define ALAUDA_STATUS_ERROR 0x01
44 #define ALAUDA_STATUS_READY 0x40
47 * Control opcodes (for request field)
49 #define ALAUDA_GET_XD_MEDIA_STATUS 0x08
50 #define ALAUDA_GET_SM_MEDIA_STATUS 0x98
51 #define ALAUDA_ACK_XD_MEDIA_CHANGE 0x0a
52 #define ALAUDA_ACK_SM_MEDIA_CHANGE 0x9a
53 #define ALAUDA_GET_XD_MEDIA_SIG 0x86
54 #define ALAUDA_GET_SM_MEDIA_SIG 0x96
57 * Bulk command identity (byte 0)
59 #define ALAUDA_BULK_CMD 0x40
62 * Bulk opcodes (byte 1)
64 #define ALAUDA_BULK_GET_REDU_DATA 0x85
65 #define ALAUDA_BULK_READ_BLOCK 0x94
66 #define ALAUDA_BULK_ERASE_BLOCK 0xa3
67 #define ALAUDA_BULK_WRITE_BLOCK 0xb4
68 #define ALAUDA_BULK_GET_STATUS2 0xb7
69 #define ALAUDA_BULK_RESET_MEDIA 0xe0
72 * Port to operate on (byte 8)
74 #define ALAUDA_PORT_XD 0x00
75 #define ALAUDA_PORT_SM 0x01
78 * LBA and PBA are unsigned ints. Special values.
82 #define UNUSABLE 0xfffd
84 struct alauda_media_info
{
85 unsigned long capacity
; /* total media size in bytes */
86 unsigned int pagesize
; /* page size in bytes */
87 unsigned int blocksize
; /* number of pages per block */
88 unsigned int uzonesize
; /* number of usable blocks per zone */
89 unsigned int zonesize
; /* number of blocks per zone */
90 unsigned int blockmask
; /* mask to get page from address */
92 unsigned char pageshift
;
93 unsigned char blockshift
;
94 unsigned char zoneshift
;
96 u16
**lba_to_pba
; /* logical to physical block map */
97 u16
**pba_to_lba
; /* physical to logical block map */
101 struct alauda_media_info port
[2];
102 int wr_ep
; /* endpoint to write data out of */
104 unsigned char sense_key
;
105 unsigned long sense_asc
; /* additional sense code */
106 unsigned long sense_ascq
; /* additional sense code qualifier */
109 #define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) )
110 #define LSB_of(s) ((s)&0xFF)
111 #define MSB_of(s) ((s)>>8)
113 #define MEDIA_PORT(us) us->srb->device->lun
114 #define MEDIA_INFO(us) ((struct alauda_info *)us->extra)->port[MEDIA_PORT(us)]
116 #define PBA_LO(pba) ((pba & 0xF) << 5)
117 #define PBA_HI(pba) (pba >> 3)
118 #define PBA_ZONE(pba) (pba >> 11)
120 static int init_alauda(struct us_data
*us
);
124 * The table of devices
126 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
127 vendorName, productName, useProtocol, useTransport, \
128 initFunction, flags) \
129 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
130 .driver_info = (flags) }
132 static struct usb_device_id alauda_usb_ids
[] = {
133 # include "unusual_alauda.h"
134 { } /* Terminating entry */
136 MODULE_DEVICE_TABLE(usb
, alauda_usb_ids
);
143 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
144 vendor_name, product_name, use_protocol, use_transport, \
145 init_function, Flags) \
147 .vendorName = vendor_name, \
148 .productName = product_name, \
149 .useProtocol = use_protocol, \
150 .useTransport = use_transport, \
151 .initFunction = init_function, \
154 static struct us_unusual_dev alauda_unusual_dev_list
[] = {
155 # include "unusual_alauda.h"
156 { } /* Terminating entry */
166 struct alauda_card_info
{
167 unsigned char id
; /* id byte */
168 unsigned char chipshift
; /* 1<<cs bytes total capacity */
169 unsigned char pageshift
; /* 1<<ps bytes in a page */
170 unsigned char blockshift
; /* 1<<bs pages per block */
171 unsigned char zoneshift
; /* 1<<zs blocks per zone */
174 static struct alauda_card_info alauda_card_ids
[] = {
176 { 0x6e, 20, 8, 4, 8}, /* 1 MB */
177 { 0xe8, 20, 8, 4, 8}, /* 1 MB */
178 { 0xec, 20, 8, 4, 8}, /* 1 MB */
179 { 0x64, 21, 8, 4, 9}, /* 2 MB */
180 { 0xea, 21, 8, 4, 9}, /* 2 MB */
181 { 0x6b, 22, 9, 4, 9}, /* 4 MB */
182 { 0xe3, 22, 9, 4, 9}, /* 4 MB */
183 { 0xe5, 22, 9, 4, 9}, /* 4 MB */
184 { 0xe6, 23, 9, 4, 10}, /* 8 MB */
185 { 0x73, 24, 9, 5, 10}, /* 16 MB */
186 { 0x75, 25, 9, 5, 10}, /* 32 MB */
187 { 0x76, 26, 9, 5, 10}, /* 64 MB */
188 { 0x79, 27, 9, 5, 10}, /* 128 MB */
189 { 0x71, 28, 9, 5, 10}, /* 256 MB */
192 { 0x5d, 21, 9, 4, 8}, /* 2 MB */
193 { 0xd5, 22, 9, 4, 9}, /* 4 MB */
194 { 0xd6, 23, 9, 4, 10}, /* 8 MB */
195 { 0x57, 24, 9, 4, 11}, /* 16 MB */
196 { 0x58, 25, 9, 4, 12}, /* 32 MB */
200 static struct alauda_card_info
*alauda_card_find_id(unsigned char id
)
204 for (i
= 0; alauda_card_ids
[i
].id
!= 0; i
++)
205 if (alauda_card_ids
[i
].id
== id
)
206 return &(alauda_card_ids
[i
]);
214 static unsigned char parity
[256];
215 static unsigned char ecc2
[256];
217 static void nand_init_ecc(void)
222 for (i
= 1; i
< 256; i
++)
223 parity
[i
] = (parity
[i
&(i
-1)] ^ 1);
225 for (i
= 0; i
< 256; i
++) {
227 for (j
= 0; j
< 8; j
++) {
237 ecc2
[i
] = ~(a
^ (a
<<1) ^ (parity
[i
] ? 0xa8 : 0));
241 /* compute 3-byte ecc on 256 bytes */
242 static void nand_compute_ecc(unsigned char *data
, unsigned char *ecc
)
245 unsigned char par
= 0, bit
, bits
[8] = {0};
247 /* collect 16 checksum bits */
248 for (i
= 0; i
< 256; i
++) {
250 bit
= parity
[data
[i
]];
251 for (j
= 0; j
< 8; j
++)
252 if ((i
& (1<<j
)) == 0)
256 /* put 4+4+4 = 12 bits in the ecc */
257 a
= (bits
[3] << 6) + (bits
[2] << 4) + (bits
[1] << 2) + bits
[0];
258 ecc
[0] = ~(a
^ (a
<<1) ^ (parity
[par
] ? 0xaa : 0));
260 a
= (bits
[7] << 6) + (bits
[6] << 4) + (bits
[5] << 2) + bits
[4];
261 ecc
[1] = ~(a
^ (a
<<1) ^ (parity
[par
] ? 0xaa : 0));
266 static int nand_compare_ecc(unsigned char *data
, unsigned char *ecc
)
268 return (data
[0] == ecc
[0] && data
[1] == ecc
[1] && data
[2] == ecc
[2]);
271 static void nand_store_ecc(unsigned char *data
, unsigned char *ecc
)
273 memcpy(data
, ecc
, 3);
281 * Forget our PBA <---> LBA mappings for a particular port
283 static void alauda_free_maps (struct alauda_media_info
*media_info
)
285 unsigned int shift
= media_info
->zoneshift
286 + media_info
->blockshift
+ media_info
->pageshift
;
287 unsigned int num_zones
= media_info
->capacity
>> shift
;
290 if (media_info
->lba_to_pba
!= NULL
)
291 for (i
= 0; i
< num_zones
; i
++) {
292 kfree(media_info
->lba_to_pba
[i
]);
293 media_info
->lba_to_pba
[i
] = NULL
;
296 if (media_info
->pba_to_lba
!= NULL
)
297 for (i
= 0; i
< num_zones
; i
++) {
298 kfree(media_info
->pba_to_lba
[i
]);
299 media_info
->pba_to_lba
[i
] = NULL
;
304 * Returns 2 bytes of status data
305 * The first byte describes media status, and second byte describes door status
307 static int alauda_get_media_status(struct us_data
*us
, unsigned char *data
)
310 unsigned char command
;
312 if (MEDIA_PORT(us
) == ALAUDA_PORT_XD
)
313 command
= ALAUDA_GET_XD_MEDIA_STATUS
;
315 command
= ALAUDA_GET_SM_MEDIA_STATUS
;
317 rc
= usb_stor_ctrl_transfer(us
, us
->recv_ctrl_pipe
,
318 command
, 0xc0, 0, 1, data
, 2);
320 usb_stor_dbg(us
, "Media status %02X %02X\n", data
[0], data
[1]);
326 * Clears the "media was changed" bit so that we know when it changes again
329 static int alauda_ack_media(struct us_data
*us
)
331 unsigned char command
;
333 if (MEDIA_PORT(us
) == ALAUDA_PORT_XD
)
334 command
= ALAUDA_ACK_XD_MEDIA_CHANGE
;
336 command
= ALAUDA_ACK_SM_MEDIA_CHANGE
;
338 return usb_stor_ctrl_transfer(us
, us
->send_ctrl_pipe
,
339 command
, 0x40, 0, 1, NULL
, 0);
343 * Retrieves a 4-byte media signature, which indicates manufacturer, capacity,
344 * and some other details.
346 static int alauda_get_media_signature(struct us_data
*us
, unsigned char *data
)
348 unsigned char command
;
350 if (MEDIA_PORT(us
) == ALAUDA_PORT_XD
)
351 command
= ALAUDA_GET_XD_MEDIA_SIG
;
353 command
= ALAUDA_GET_SM_MEDIA_SIG
;
355 return usb_stor_ctrl_transfer(us
, us
->recv_ctrl_pipe
,
356 command
, 0xc0, 0, 0, data
, 4);
360 * Resets the media status (but not the whole device?)
362 static int alauda_reset_media(struct us_data
*us
)
364 unsigned char *command
= us
->iobuf
;
366 memset(command
, 0, 9);
367 command
[0] = ALAUDA_BULK_CMD
;
368 command
[1] = ALAUDA_BULK_RESET_MEDIA
;
369 command
[8] = MEDIA_PORT(us
);
371 return usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
,
376 * Examines the media and deduces capacity, etc.
378 static int alauda_init_media(struct us_data
*us
)
380 unsigned char *data
= us
->iobuf
;
382 struct alauda_card_info
*media_info
;
383 unsigned int num_zones
;
388 if (alauda_get_media_status(us
, data
) != USB_STOR_XFER_GOOD
)
389 return USB_STOR_TRANSPORT_ERROR
;
395 usb_stor_dbg(us
, "We are ready for action!\n");
397 if (alauda_ack_media(us
) != USB_STOR_XFER_GOOD
)
398 return USB_STOR_TRANSPORT_ERROR
;
402 if (alauda_get_media_status(us
, data
) != USB_STOR_XFER_GOOD
)
403 return USB_STOR_TRANSPORT_ERROR
;
405 if (data
[0] != 0x14) {
406 usb_stor_dbg(us
, "Media not ready after ack\n");
407 return USB_STOR_TRANSPORT_ERROR
;
410 if (alauda_get_media_signature(us
, data
) != USB_STOR_XFER_GOOD
)
411 return USB_STOR_TRANSPORT_ERROR
;
413 usb_stor_dbg(us
, "Media signature: %4ph\n", data
);
414 media_info
= alauda_card_find_id(data
[1]);
415 if (media_info
== NULL
) {
416 pr_warn("alauda_init_media: Unrecognised media signature: %4ph\n",
418 return USB_STOR_TRANSPORT_ERROR
;
421 MEDIA_INFO(us
).capacity
= 1 << media_info
->chipshift
;
422 usb_stor_dbg(us
, "Found media with capacity: %ldMB\n",
423 MEDIA_INFO(us
).capacity
>> 20);
425 MEDIA_INFO(us
).pageshift
= media_info
->pageshift
;
426 MEDIA_INFO(us
).blockshift
= media_info
->blockshift
;
427 MEDIA_INFO(us
).zoneshift
= media_info
->zoneshift
;
429 MEDIA_INFO(us
).pagesize
= 1 << media_info
->pageshift
;
430 MEDIA_INFO(us
).blocksize
= 1 << media_info
->blockshift
;
431 MEDIA_INFO(us
).zonesize
= 1 << media_info
->zoneshift
;
433 MEDIA_INFO(us
).uzonesize
= ((1 << media_info
->zoneshift
) / 128) * 125;
434 MEDIA_INFO(us
).blockmask
= MEDIA_INFO(us
).blocksize
- 1;
436 num_zones
= MEDIA_INFO(us
).capacity
>> (MEDIA_INFO(us
).zoneshift
437 + MEDIA_INFO(us
).blockshift
+ MEDIA_INFO(us
).pageshift
);
438 MEDIA_INFO(us
).pba_to_lba
= kcalloc(num_zones
, sizeof(u16
*), GFP_NOIO
);
439 MEDIA_INFO(us
).lba_to_pba
= kcalloc(num_zones
, sizeof(u16
*), GFP_NOIO
);
441 if (alauda_reset_media(us
) != USB_STOR_XFER_GOOD
)
442 return USB_STOR_TRANSPORT_ERROR
;
444 return USB_STOR_TRANSPORT_GOOD
;
448 * Examines the media status and does the right thing when the media has gone,
449 * appeared, or changed.
451 static int alauda_check_media(struct us_data
*us
)
453 struct alauda_info
*info
= (struct alauda_info
*) us
->extra
;
454 unsigned char status
[2];
457 rc
= alauda_get_media_status(us
, status
);
459 /* Check for no media or door open */
460 if ((status
[0] & 0x80) || ((status
[0] & 0x1F) == 0x10)
461 || ((status
[1] & 0x01) == 0)) {
462 usb_stor_dbg(us
, "No media, or door open\n");
463 alauda_free_maps(&MEDIA_INFO(us
));
464 info
->sense_key
= 0x02;
465 info
->sense_asc
= 0x3A;
466 info
->sense_ascq
= 0x00;
467 return USB_STOR_TRANSPORT_FAILED
;
470 /* Check for media change */
471 if (status
[0] & 0x08) {
472 usb_stor_dbg(us
, "Media change detected\n");
473 alauda_free_maps(&MEDIA_INFO(us
));
474 alauda_init_media(us
);
476 info
->sense_key
= UNIT_ATTENTION
;
477 info
->sense_asc
= 0x28;
478 info
->sense_ascq
= 0x00;
479 return USB_STOR_TRANSPORT_FAILED
;
482 return USB_STOR_TRANSPORT_GOOD
;
486 * Checks the status from the 2nd status register
487 * Returns 3 bytes of status data, only the first is known
489 static int alauda_check_status2(struct us_data
*us
)
492 unsigned char command
[] = {
493 ALAUDA_BULK_CMD
, ALAUDA_BULK_GET_STATUS2
,
494 0, 0, 0, 0, 3, 0, MEDIA_PORT(us
)
496 unsigned char data
[3];
498 rc
= usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
,
500 if (rc
!= USB_STOR_XFER_GOOD
)
503 rc
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
,
505 if (rc
!= USB_STOR_XFER_GOOD
)
508 usb_stor_dbg(us
, "%3ph\n", data
);
509 if (data
[0] & ALAUDA_STATUS_ERROR
)
510 return USB_STOR_XFER_ERROR
;
512 return USB_STOR_XFER_GOOD
;
516 * Gets the redundancy data for the first page of a PBA
519 static int alauda_get_redu_data(struct us_data
*us
, u16 pba
, unsigned char *data
)
522 unsigned char command
[] = {
523 ALAUDA_BULK_CMD
, ALAUDA_BULK_GET_REDU_DATA
,
524 PBA_HI(pba
), PBA_ZONE(pba
), 0, PBA_LO(pba
), 0, 0, MEDIA_PORT(us
)
527 rc
= usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
,
529 if (rc
!= USB_STOR_XFER_GOOD
)
532 return usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
,
537 * Finds the first unused PBA in a zone
538 * Returns the absolute PBA of an unused PBA, or 0 if none found.
540 static u16
alauda_find_unused_pba(struct alauda_media_info
*info
,
543 u16
*pba_to_lba
= info
->pba_to_lba
[zone
];
546 for (i
= 0; i
< info
->zonesize
; i
++)
547 if (pba_to_lba
[i
] == UNDEF
)
548 return (zone
<< info
->zoneshift
) + i
;
554 * Reads the redundancy data for all PBA's in a zone
555 * Produces lba <--> pba mappings
557 static int alauda_read_map(struct us_data
*us
, unsigned int zone
)
559 unsigned char *data
= us
->iobuf
;
562 unsigned int zonesize
= MEDIA_INFO(us
).zonesize
;
563 unsigned int uzonesize
= MEDIA_INFO(us
).uzonesize
;
564 unsigned int lba_offset
, lba_real
, blocknum
;
565 unsigned int zone_base_lba
= zone
* uzonesize
;
566 unsigned int zone_base_pba
= zone
* zonesize
;
567 u16
*lba_to_pba
= kcalloc(zonesize
, sizeof(u16
), GFP_NOIO
);
568 u16
*pba_to_lba
= kcalloc(zonesize
, sizeof(u16
), GFP_NOIO
);
569 if (lba_to_pba
== NULL
|| pba_to_lba
== NULL
) {
570 result
= USB_STOR_TRANSPORT_ERROR
;
574 usb_stor_dbg(us
, "Mapping blocks for zone %d\n", zone
);
576 /* 1024 PBA's per zone */
577 for (i
= 0; i
< zonesize
; i
++)
578 lba_to_pba
[i
] = pba_to_lba
[i
] = UNDEF
;
580 for (i
= 0; i
< zonesize
; i
++) {
581 blocknum
= zone_base_pba
+ i
;
583 result
= alauda_get_redu_data(us
, blocknum
, data
);
584 if (result
!= USB_STOR_XFER_GOOD
) {
585 result
= USB_STOR_TRANSPORT_ERROR
;
589 /* special PBAs have control field 0^16 */
590 for (j
= 0; j
< 16; j
++)
593 pba_to_lba
[i
] = UNUSABLE
;
594 usb_stor_dbg(us
, "PBA %d has no logical mapping\n", blocknum
);
598 /* unwritten PBAs have control field FF^16 */
599 for (j
= 0; j
< 16; j
++)
605 /* normal PBAs start with six FFs */
607 usb_stor_dbg(us
, "PBA %d has no logical mapping: reserved area = %02X%02X%02X%02X data status %02X block status %02X\n",
609 data
[0], data
[1], data
[2], data
[3],
611 pba_to_lba
[i
] = UNUSABLE
;
615 if ((data
[6] >> 4) != 0x01) {
616 usb_stor_dbg(us
, "PBA %d has invalid address field %02X%02X/%02X%02X\n",
617 blocknum
, data
[6], data
[7],
619 pba_to_lba
[i
] = UNUSABLE
;
623 /* check even parity */
624 if (parity
[data
[6] ^ data
[7]]) {
626 "alauda_read_map: Bad parity in LBA for block %d"
627 " (%02X %02X)\n", i
, data
[6], data
[7]);
628 pba_to_lba
[i
] = UNUSABLE
;
632 lba_offset
= short_pack(data
[7], data
[6]);
633 lba_offset
= (lba_offset
& 0x07FF) >> 1;
634 lba_real
= lba_offset
+ zone_base_lba
;
637 * Every 1024 physical blocks ("zone"), the LBA numbers
638 * go back to zero, but are within a higher block of LBA's.
639 * Also, there is a maximum of 1000 LBA's per zone.
640 * In other words, in PBA 1024-2047 you will find LBA 0-999
641 * which are really LBA 1000-1999. This allows for 24 bad
642 * or special physical blocks per zone.
645 if (lba_offset
>= uzonesize
) {
647 "alauda_read_map: Bad low LBA %d for block %d\n",
652 if (lba_to_pba
[lba_offset
] != UNDEF
) {
655 "LBA %d seen for PBA %d and %d\n",
656 lba_real
, lba_to_pba
[lba_offset
], blocknum
);
660 pba_to_lba
[i
] = lba_real
;
661 lba_to_pba
[lba_offset
] = blocknum
;
665 MEDIA_INFO(us
).lba_to_pba
[zone
] = lba_to_pba
;
666 MEDIA_INFO(us
).pba_to_lba
[zone
] = pba_to_lba
;
678 * Checks to see whether we have already mapped a certain zone
679 * If we haven't, the map is generated
681 static void alauda_ensure_map_for_zone(struct us_data
*us
, unsigned int zone
)
683 if (MEDIA_INFO(us
).lba_to_pba
[zone
] == NULL
684 || MEDIA_INFO(us
).pba_to_lba
[zone
] == NULL
)
685 alauda_read_map(us
, zone
);
689 * Erases an entire block
691 static int alauda_erase_block(struct us_data
*us
, u16 pba
)
694 unsigned char command
[] = {
695 ALAUDA_BULK_CMD
, ALAUDA_BULK_ERASE_BLOCK
, PBA_HI(pba
),
696 PBA_ZONE(pba
), 0, PBA_LO(pba
), 0x02, 0, MEDIA_PORT(us
)
698 unsigned char buf
[2];
700 usb_stor_dbg(us
, "Erasing PBA %d\n", pba
);
702 rc
= usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
,
704 if (rc
!= USB_STOR_XFER_GOOD
)
707 rc
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
,
709 if (rc
!= USB_STOR_XFER_GOOD
)
712 usb_stor_dbg(us
, "Erase result: %02X %02X\n", buf
[0], buf
[1]);
717 * Reads data from a certain offset page inside a PBA, including interleaved
718 * redundancy data. Returns (pagesize+64)*pages bytes in data.
720 static int alauda_read_block_raw(struct us_data
*us
, u16 pba
,
721 unsigned int page
, unsigned int pages
, unsigned char *data
)
724 unsigned char command
[] = {
725 ALAUDA_BULK_CMD
, ALAUDA_BULK_READ_BLOCK
, PBA_HI(pba
),
726 PBA_ZONE(pba
), 0, PBA_LO(pba
) + page
, pages
, 0, MEDIA_PORT(us
)
729 usb_stor_dbg(us
, "pba %d page %d count %d\n", pba
, page
, pages
);
731 rc
= usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
,
733 if (rc
!= USB_STOR_XFER_GOOD
)
736 return usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
,
737 data
, (MEDIA_INFO(us
).pagesize
+ 64) * pages
, NULL
);
741 * Reads data from a certain offset page inside a PBA, excluding redundancy
742 * data. Returns pagesize*pages bytes in data. Note that data must be big enough
743 * to hold (pagesize+64)*pages bytes of data, but you can ignore those 'extra'
744 * trailing bytes outside this function.
746 static int alauda_read_block(struct us_data
*us
, u16 pba
,
747 unsigned int page
, unsigned int pages
, unsigned char *data
)
750 unsigned int pagesize
= MEDIA_INFO(us
).pagesize
;
752 rc
= alauda_read_block_raw(us
, pba
, page
, pages
, data
);
753 if (rc
!= USB_STOR_XFER_GOOD
)
756 /* Cut out the redundancy data */
757 for (i
= 0; i
< pages
; i
++) {
758 int dest_offset
= i
* pagesize
;
759 int src_offset
= i
* (pagesize
+ 64);
760 memmove(data
+ dest_offset
, data
+ src_offset
, pagesize
);
767 * Writes an entire block of data and checks status after write.
768 * Redundancy data must be already included in data. Data should be
769 * (pagesize+64)*blocksize bytes in length.
771 static int alauda_write_block(struct us_data
*us
, u16 pba
, unsigned char *data
)
774 struct alauda_info
*info
= (struct alauda_info
*) us
->extra
;
775 unsigned char command
[] = {
776 ALAUDA_BULK_CMD
, ALAUDA_BULK_WRITE_BLOCK
, PBA_HI(pba
),
777 PBA_ZONE(pba
), 0, PBA_LO(pba
), 32, 0, MEDIA_PORT(us
)
780 usb_stor_dbg(us
, "pba %d\n", pba
);
782 rc
= usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
,
784 if (rc
!= USB_STOR_XFER_GOOD
)
787 rc
= usb_stor_bulk_transfer_buf(us
, info
->wr_ep
, data
,
788 (MEDIA_INFO(us
).pagesize
+ 64) * MEDIA_INFO(us
).blocksize
,
790 if (rc
!= USB_STOR_XFER_GOOD
)
793 return alauda_check_status2(us
);
797 * Write some data to a specific LBA.
799 static int alauda_write_lba(struct us_data
*us
, u16 lba
,
800 unsigned int page
, unsigned int pages
,
801 unsigned char *ptr
, unsigned char *blockbuffer
)
803 u16 pba
, lbap
, new_pba
;
804 unsigned char *bptr
, *cptr
, *xptr
;
805 unsigned char ecc
[3];
807 unsigned int uzonesize
= MEDIA_INFO(us
).uzonesize
;
808 unsigned int zonesize
= MEDIA_INFO(us
).zonesize
;
809 unsigned int pagesize
= MEDIA_INFO(us
).pagesize
;
810 unsigned int blocksize
= MEDIA_INFO(us
).blocksize
;
811 unsigned int lba_offset
= lba
% uzonesize
;
812 unsigned int new_pba_offset
;
813 unsigned int zone
= lba
/ uzonesize
;
815 alauda_ensure_map_for_zone(us
, zone
);
817 pba
= MEDIA_INFO(us
).lba_to_pba
[zone
][lba_offset
];
820 * Maybe it is impossible to write to PBA 1.
821 * Fake success, but don't do anything.
824 "alauda_write_lba: avoid writing to pba 1\n");
825 return USB_STOR_TRANSPORT_GOOD
;
828 new_pba
= alauda_find_unused_pba(&MEDIA_INFO(us
), zone
);
831 "alauda_write_lba: Out of unused blocks\n");
832 return USB_STOR_TRANSPORT_ERROR
;
835 /* read old contents */
837 result
= alauda_read_block_raw(us
, pba
, 0,
838 blocksize
, blockbuffer
);
839 if (result
!= USB_STOR_XFER_GOOD
)
842 memset(blockbuffer
, 0, blocksize
* (pagesize
+ 64));
845 lbap
= (lba_offset
<< 1) | 0x1000;
846 if (parity
[MSB_of(lbap
) ^ LSB_of(lbap
)])
849 /* check old contents and fill lba */
850 for (i
= 0; i
< blocksize
; i
++) {
851 bptr
= blockbuffer
+ (i
* (pagesize
+ 64));
852 cptr
= bptr
+ pagesize
;
853 nand_compute_ecc(bptr
, ecc
);
854 if (!nand_compare_ecc(cptr
+13, ecc
)) {
855 usb_stor_dbg(us
, "Warning: bad ecc in page %d- of pba %d\n",
857 nand_store_ecc(cptr
+13, ecc
);
859 nand_compute_ecc(bptr
+ (pagesize
/ 2), ecc
);
860 if (!nand_compare_ecc(cptr
+8, ecc
)) {
861 usb_stor_dbg(us
, "Warning: bad ecc in page %d+ of pba %d\n",
863 nand_store_ecc(cptr
+8, ecc
);
865 cptr
[6] = cptr
[11] = MSB_of(lbap
);
866 cptr
[7] = cptr
[12] = LSB_of(lbap
);
869 /* copy in new stuff and compute ECC */
871 for (i
= page
; i
< page
+pages
; i
++) {
872 bptr
= blockbuffer
+ (i
* (pagesize
+ 64));
873 cptr
= bptr
+ pagesize
;
874 memcpy(bptr
, xptr
, pagesize
);
876 nand_compute_ecc(bptr
, ecc
);
877 nand_store_ecc(cptr
+13, ecc
);
878 nand_compute_ecc(bptr
+ (pagesize
/ 2), ecc
);
879 nand_store_ecc(cptr
+8, ecc
);
882 result
= alauda_write_block(us
, new_pba
, blockbuffer
);
883 if (result
!= USB_STOR_XFER_GOOD
)
886 new_pba_offset
= new_pba
- (zone
* zonesize
);
887 MEDIA_INFO(us
).pba_to_lba
[zone
][new_pba_offset
] = lba
;
888 MEDIA_INFO(us
).lba_to_pba
[zone
][lba_offset
] = new_pba
;
889 usb_stor_dbg(us
, "Remapped LBA %d to PBA %d\n", lba
, new_pba
);
892 unsigned int pba_offset
= pba
- (zone
* zonesize
);
893 result
= alauda_erase_block(us
, pba
);
894 if (result
!= USB_STOR_XFER_GOOD
)
896 MEDIA_INFO(us
).pba_to_lba
[zone
][pba_offset
] = UNDEF
;
899 return USB_STOR_TRANSPORT_GOOD
;
903 * Read data from a specific sector address
905 static int alauda_read_data(struct us_data
*us
, unsigned long address
,
906 unsigned int sectors
)
908 unsigned char *buffer
;
910 unsigned int page
, len
, offset
;
911 unsigned int blockshift
= MEDIA_INFO(us
).blockshift
;
912 unsigned int pageshift
= MEDIA_INFO(us
).pageshift
;
913 unsigned int blocksize
= MEDIA_INFO(us
).blocksize
;
914 unsigned int pagesize
= MEDIA_INFO(us
).pagesize
;
915 unsigned int uzonesize
= MEDIA_INFO(us
).uzonesize
;
916 struct scatterlist
*sg
;
920 * Since we only read in one block at a time, we have to create
921 * a bounce buffer and move the data a piece at a time between the
922 * bounce buffer and the actual transfer buffer.
923 * We make this buffer big enough to hold temporary redundancy data,
924 * which we use when reading the data blocks.
927 len
= min(sectors
, blocksize
) * (pagesize
+ 64);
928 buffer
= kmalloc(len
, GFP_NOIO
);
930 return USB_STOR_TRANSPORT_ERROR
;
932 /* Figure out the initial LBA and page */
933 lba
= address
>> blockshift
;
934 page
= (address
& MEDIA_INFO(us
).blockmask
);
935 max_lba
= MEDIA_INFO(us
).capacity
>> (blockshift
+ pageshift
);
937 result
= USB_STOR_TRANSPORT_GOOD
;
941 while (sectors
> 0) {
942 unsigned int zone
= lba
/ uzonesize
; /* integer division */
943 unsigned int lba_offset
= lba
- (zone
* uzonesize
);
946 alauda_ensure_map_for_zone(us
, zone
);
948 /* Not overflowing capacity? */
949 if (lba
>= max_lba
) {
950 usb_stor_dbg(us
, "Error: Requested lba %u exceeds maximum %u\n",
952 result
= USB_STOR_TRANSPORT_ERROR
;
956 /* Find number of pages we can read in this block */
957 pages
= min(sectors
, blocksize
- page
);
958 len
= pages
<< pageshift
;
960 /* Find where this lba lives on disk */
961 pba
= MEDIA_INFO(us
).lba_to_pba
[zone
][lba_offset
];
963 if (pba
== UNDEF
) { /* this lba was never written */
964 usb_stor_dbg(us
, "Read %d zero pages (LBA %d) page %d\n",
968 * This is not really an error. It just means
969 * that the block has never been written.
970 * Instead of returning USB_STOR_TRANSPORT_ERROR
971 * it is better to return all zero data.
974 memset(buffer
, 0, len
);
976 usb_stor_dbg(us
, "Read %d pages, from PBA %d (LBA %d) page %d\n",
977 pages
, pba
, lba
, page
);
979 result
= alauda_read_block(us
, pba
, page
, pages
, buffer
);
980 if (result
!= USB_STOR_TRANSPORT_GOOD
)
984 /* Store the data in the transfer buffer */
985 usb_stor_access_xfer_buf(buffer
, len
, us
->srb
,
986 &sg
, &offset
, TO_XFER_BUF
);
998 * Write data to a specific sector address
1000 static int alauda_write_data(struct us_data
*us
, unsigned long address
,
1001 unsigned int sectors
)
1003 unsigned char *buffer
, *blockbuffer
;
1004 unsigned int page
, len
, offset
;
1005 unsigned int blockshift
= MEDIA_INFO(us
).blockshift
;
1006 unsigned int pageshift
= MEDIA_INFO(us
).pageshift
;
1007 unsigned int blocksize
= MEDIA_INFO(us
).blocksize
;
1008 unsigned int pagesize
= MEDIA_INFO(us
).pagesize
;
1009 struct scatterlist
*sg
;
1014 * Since we don't write the user data directly to the device,
1015 * we have to create a bounce buffer and move the data a piece
1016 * at a time between the bounce buffer and the actual transfer buffer.
1019 len
= min(sectors
, blocksize
) * pagesize
;
1020 buffer
= kmalloc(len
, GFP_NOIO
);
1022 return USB_STOR_TRANSPORT_ERROR
;
1025 * We also need a temporary block buffer, where we read in the old data,
1026 * overwrite parts with the new data, and manipulate the redundancy data
1028 blockbuffer
= kmalloc_array(pagesize
+ 64, blocksize
, GFP_NOIO
);
1031 return USB_STOR_TRANSPORT_ERROR
;
1034 /* Figure out the initial LBA and page */
1035 lba
= address
>> blockshift
;
1036 page
= (address
& MEDIA_INFO(us
).blockmask
);
1037 max_lba
= MEDIA_INFO(us
).capacity
>> (pageshift
+ blockshift
);
1039 result
= USB_STOR_TRANSPORT_GOOD
;
1043 while (sectors
> 0) {
1044 /* Write as many sectors as possible in this block */
1045 unsigned int pages
= min(sectors
, blocksize
- page
);
1046 len
= pages
<< pageshift
;
1048 /* Not overflowing capacity? */
1049 if (lba
>= max_lba
) {
1050 usb_stor_dbg(us
, "Requested lba %u exceeds maximum %u\n",
1052 result
= USB_STOR_TRANSPORT_ERROR
;
1056 /* Get the data from the transfer buffer */
1057 usb_stor_access_xfer_buf(buffer
, len
, us
->srb
,
1058 &sg
, &offset
, FROM_XFER_BUF
);
1060 result
= alauda_write_lba(us
, lba
, page
, pages
, buffer
,
1062 if (result
!= USB_STOR_TRANSPORT_GOOD
)
1076 * Our interface with the rest of the world
1079 static void alauda_info_destructor(void *extra
)
1081 struct alauda_info
*info
= (struct alauda_info
*) extra
;
1087 for (port
= 0; port
< 2; port
++) {
1088 struct alauda_media_info
*media_info
= &info
->port
[port
];
1090 alauda_free_maps(media_info
);
1091 kfree(media_info
->lba_to_pba
);
1092 kfree(media_info
->pba_to_lba
);
1097 * Initialize alauda_info struct and find the data-write endpoint
1099 static int init_alauda(struct us_data
*us
)
1101 struct alauda_info
*info
;
1102 struct usb_host_interface
*altsetting
= us
->pusb_intf
->cur_altsetting
;
1105 us
->extra
= kzalloc(sizeof(struct alauda_info
), GFP_NOIO
);
1107 return USB_STOR_TRANSPORT_ERROR
;
1109 info
= (struct alauda_info
*) us
->extra
;
1110 us
->extra_destructor
= alauda_info_destructor
;
1112 info
->wr_ep
= usb_sndbulkpipe(us
->pusb_dev
,
1113 altsetting
->endpoint
[0].desc
.bEndpointAddress
1114 & USB_ENDPOINT_NUMBER_MASK
);
1116 return USB_STOR_TRANSPORT_GOOD
;
1119 static int alauda_transport(struct scsi_cmnd
*srb
, struct us_data
*us
)
1122 struct alauda_info
*info
= (struct alauda_info
*) us
->extra
;
1123 unsigned char *ptr
= us
->iobuf
;
1124 static unsigned char inquiry_response
[36] = {
1125 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1128 if (srb
->cmnd
[0] == INQUIRY
) {
1129 usb_stor_dbg(us
, "INQUIRY - Returning bogus response\n");
1130 memcpy(ptr
, inquiry_response
, sizeof(inquiry_response
));
1131 fill_inquiry_response(us
, ptr
, 36);
1132 return USB_STOR_TRANSPORT_GOOD
;
1135 if (srb
->cmnd
[0] == TEST_UNIT_READY
) {
1136 usb_stor_dbg(us
, "TEST_UNIT_READY\n");
1137 return alauda_check_media(us
);
1140 if (srb
->cmnd
[0] == READ_CAPACITY
) {
1141 unsigned int num_zones
;
1142 unsigned long capacity
;
1144 rc
= alauda_check_media(us
);
1145 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
1148 num_zones
= MEDIA_INFO(us
).capacity
>> (MEDIA_INFO(us
).zoneshift
1149 + MEDIA_INFO(us
).blockshift
+ MEDIA_INFO(us
).pageshift
);
1151 capacity
= num_zones
* MEDIA_INFO(us
).uzonesize
1152 * MEDIA_INFO(us
).blocksize
;
1154 /* Report capacity and page size */
1155 ((__be32
*) ptr
)[0] = cpu_to_be32(capacity
- 1);
1156 ((__be32
*) ptr
)[1] = cpu_to_be32(512);
1158 usb_stor_set_xfer_buf(ptr
, 8, srb
);
1159 return USB_STOR_TRANSPORT_GOOD
;
1162 if (srb
->cmnd
[0] == READ_10
) {
1163 unsigned int page
, pages
;
1165 rc
= alauda_check_media(us
);
1166 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
1169 page
= short_pack(srb
->cmnd
[3], srb
->cmnd
[2]);
1171 page
|= short_pack(srb
->cmnd
[5], srb
->cmnd
[4]);
1172 pages
= short_pack(srb
->cmnd
[8], srb
->cmnd
[7]);
1174 usb_stor_dbg(us
, "READ_10: page %d pagect %d\n", page
, pages
);
1176 return alauda_read_data(us
, page
, pages
);
1179 if (srb
->cmnd
[0] == WRITE_10
) {
1180 unsigned int page
, pages
;
1182 rc
= alauda_check_media(us
);
1183 if (rc
!= USB_STOR_TRANSPORT_GOOD
)
1186 page
= short_pack(srb
->cmnd
[3], srb
->cmnd
[2]);
1188 page
|= short_pack(srb
->cmnd
[5], srb
->cmnd
[4]);
1189 pages
= short_pack(srb
->cmnd
[8], srb
->cmnd
[7]);
1191 usb_stor_dbg(us
, "WRITE_10: page %d pagect %d\n", page
, pages
);
1193 return alauda_write_data(us
, page
, pages
);
1196 if (srb
->cmnd
[0] == REQUEST_SENSE
) {
1197 usb_stor_dbg(us
, "REQUEST_SENSE\n");
1201 ptr
[2] = info
->sense_key
;
1203 ptr
[12] = info
->sense_asc
;
1204 ptr
[13] = info
->sense_ascq
;
1205 usb_stor_set_xfer_buf(ptr
, 18, srb
);
1207 return USB_STOR_TRANSPORT_GOOD
;
1210 if (srb
->cmnd
[0] == ALLOW_MEDIUM_REMOVAL
) {
1212 * sure. whatever. not like we can stop the user from popping
1213 * the media out of the device (no locking doors, etc)
1215 return USB_STOR_TRANSPORT_GOOD
;
1218 usb_stor_dbg(us
, "Gah! Unknown command: %d (0x%x)\n",
1219 srb
->cmnd
[0], srb
->cmnd
[0]);
1220 info
->sense_key
= 0x05;
1221 info
->sense_asc
= 0x20;
1222 info
->sense_ascq
= 0x00;
1223 return USB_STOR_TRANSPORT_FAILED
;
1226 static struct scsi_host_template alauda_host_template
;
1228 static int alauda_probe(struct usb_interface
*intf
,
1229 const struct usb_device_id
*id
)
1234 result
= usb_stor_probe1(&us
, intf
, id
,
1235 (id
- alauda_usb_ids
) + alauda_unusual_dev_list
,
1236 &alauda_host_template
);
1240 us
->transport_name
= "Alauda Control/Bulk";
1241 us
->transport
= alauda_transport
;
1242 us
->transport_reset
= usb_stor_Bulk_reset
;
1245 result
= usb_stor_probe2(us
);
1249 static struct usb_driver alauda_driver
= {
1251 .probe
= alauda_probe
,
1252 .disconnect
= usb_stor_disconnect
,
1253 .suspend
= usb_stor_suspend
,
1254 .resume
= usb_stor_resume
,
1255 .reset_resume
= usb_stor_reset_resume
,
1256 .pre_reset
= usb_stor_pre_reset
,
1257 .post_reset
= usb_stor_post_reset
,
1258 .id_table
= alauda_usb_ids
,
1263 module_usb_stor_driver(alauda_driver
, alauda_host_template
, DRV_NAME
);