ia64/kvm: compilation fix. export account_system_vtime.
[pv_ops_mirror.git] / drivers / usb / storage / alauda.c
blob8d3711a7ff0655aa519811a295b6b57c2b8b0253
1 /*
2 * Driver for Alauda-based card readers
4 * Current development and maintenance by:
5 * (c) 2005 Daniel Drake <dsd@gentoo.org>
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
22 * later version.
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 <scsi/scsi.h>
35 #include <scsi/scsi_cmnd.h>
36 #include <scsi/scsi_device.h>
38 #include "usb.h"
39 #include "transport.h"
40 #include "protocol.h"
41 #include "debug.h"
42 #include "alauda.h"
44 #define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) )
45 #define LSB_of(s) ((s)&0xFF)
46 #define MSB_of(s) ((s)>>8)
48 #define MEDIA_PORT(us) us->srb->device->lun
49 #define MEDIA_INFO(us) ((struct alauda_info *)us->extra)->port[MEDIA_PORT(us)]
51 #define PBA_LO(pba) ((pba & 0xF) << 5)
52 #define PBA_HI(pba) (pba >> 3)
53 #define PBA_ZONE(pba) (pba >> 11)
56 * Media handling
59 struct alauda_card_info {
60 unsigned char id; /* id byte */
61 unsigned char chipshift; /* 1<<cs bytes total capacity */
62 unsigned char pageshift; /* 1<<ps bytes in a page */
63 unsigned char blockshift; /* 1<<bs pages per block */
64 unsigned char zoneshift; /* 1<<zs blocks per zone */
67 static struct alauda_card_info alauda_card_ids[] = {
68 /* NAND flash */
69 { 0x6e, 20, 8, 4, 8}, /* 1 MB */
70 { 0xe8, 20, 8, 4, 8}, /* 1 MB */
71 { 0xec, 20, 8, 4, 8}, /* 1 MB */
72 { 0x64, 21, 8, 4, 9}, /* 2 MB */
73 { 0xea, 21, 8, 4, 9}, /* 2 MB */
74 { 0x6b, 22, 9, 4, 9}, /* 4 MB */
75 { 0xe3, 22, 9, 4, 9}, /* 4 MB */
76 { 0xe5, 22, 9, 4, 9}, /* 4 MB */
77 { 0xe6, 23, 9, 4, 10}, /* 8 MB */
78 { 0x73, 24, 9, 5, 10}, /* 16 MB */
79 { 0x75, 25, 9, 5, 10}, /* 32 MB */
80 { 0x76, 26, 9, 5, 10}, /* 64 MB */
81 { 0x79, 27, 9, 5, 10}, /* 128 MB */
82 { 0x71, 28, 9, 5, 10}, /* 256 MB */
84 /* MASK ROM */
85 { 0x5d, 21, 9, 4, 8}, /* 2 MB */
86 { 0xd5, 22, 9, 4, 9}, /* 4 MB */
87 { 0xd6, 23, 9, 4, 10}, /* 8 MB */
88 { 0x57, 24, 9, 4, 11}, /* 16 MB */
89 { 0x58, 25, 9, 4, 12}, /* 32 MB */
90 { 0,}
93 static struct alauda_card_info *alauda_card_find_id(unsigned char id) {
94 int i;
96 for (i = 0; alauda_card_ids[i].id != 0; i++)
97 if (alauda_card_ids[i].id == id)
98 return &(alauda_card_ids[i]);
99 return NULL;
103 * ECC computation.
106 static unsigned char parity[256];
107 static unsigned char ecc2[256];
109 static void nand_init_ecc(void) {
110 int i, j, a;
112 parity[0] = 0;
113 for (i = 1; i < 256; i++)
114 parity[i] = (parity[i&(i-1)] ^ 1);
116 for (i = 0; i < 256; i++) {
117 a = 0;
118 for (j = 0; j < 8; j++) {
119 if (i & (1<<j)) {
120 if ((j & 1) == 0)
121 a ^= 0x04;
122 if ((j & 2) == 0)
123 a ^= 0x10;
124 if ((j & 4) == 0)
125 a ^= 0x40;
128 ecc2[i] = ~(a ^ (a<<1) ^ (parity[i] ? 0xa8 : 0));
132 /* compute 3-byte ecc on 256 bytes */
133 static void nand_compute_ecc(unsigned char *data, unsigned char *ecc) {
134 int i, j, a;
135 unsigned char par, bit, bits[8];
137 par = 0;
138 for (j = 0; j < 8; j++)
139 bits[j] = 0;
141 /* collect 16 checksum bits */
142 for (i = 0; i < 256; i++) {
143 par ^= data[i];
144 bit = parity[data[i]];
145 for (j = 0; j < 8; j++)
146 if ((i & (1<<j)) == 0)
147 bits[j] ^= bit;
150 /* put 4+4+4 = 12 bits in the ecc */
151 a = (bits[3] << 6) + (bits[2] << 4) + (bits[1] << 2) + bits[0];
152 ecc[0] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
154 a = (bits[7] << 6) + (bits[6] << 4) + (bits[5] << 2) + bits[4];
155 ecc[1] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
157 ecc[2] = ecc2[par];
160 static int nand_compare_ecc(unsigned char *data, unsigned char *ecc) {
161 return (data[0] == ecc[0] && data[1] == ecc[1] && data[2] == ecc[2]);
164 static void nand_store_ecc(unsigned char *data, unsigned char *ecc) {
165 memcpy(data, ecc, 3);
169 * Alauda driver
173 * Forget our PBA <---> LBA mappings for a particular port
175 static void alauda_free_maps (struct alauda_media_info *media_info)
177 unsigned int shift = media_info->zoneshift
178 + media_info->blockshift + media_info->pageshift;
179 unsigned int num_zones = media_info->capacity >> shift;
180 unsigned int i;
182 if (media_info->lba_to_pba != NULL)
183 for (i = 0; i < num_zones; i++) {
184 kfree(media_info->lba_to_pba[i]);
185 media_info->lba_to_pba[i] = NULL;
188 if (media_info->pba_to_lba != NULL)
189 for (i = 0; i < num_zones; i++) {
190 kfree(media_info->pba_to_lba[i]);
191 media_info->pba_to_lba[i] = NULL;
196 * Returns 2 bytes of status data
197 * The first byte describes media status, and second byte describes door status
199 static int alauda_get_media_status(struct us_data *us, unsigned char *data)
201 int rc;
202 unsigned char command;
204 if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
205 command = ALAUDA_GET_XD_MEDIA_STATUS;
206 else
207 command = ALAUDA_GET_SM_MEDIA_STATUS;
209 rc = usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
210 command, 0xc0, 0, 1, data, 2);
212 US_DEBUGP("alauda_get_media_status: Media status %02X %02X\n",
213 data[0], data[1]);
215 return rc;
219 * Clears the "media was changed" bit so that we know when it changes again
220 * in the future.
222 static int alauda_ack_media(struct us_data *us)
224 unsigned char command;
226 if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
227 command = ALAUDA_ACK_XD_MEDIA_CHANGE;
228 else
229 command = ALAUDA_ACK_SM_MEDIA_CHANGE;
231 return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
232 command, 0x40, 0, 1, NULL, 0);
236 * Retrieves a 4-byte media signature, which indicates manufacturer, capacity,
237 * and some other details.
239 static int alauda_get_media_signature(struct us_data *us, unsigned char *data)
241 unsigned char command;
243 if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
244 command = ALAUDA_GET_XD_MEDIA_SIG;
245 else
246 command = ALAUDA_GET_SM_MEDIA_SIG;
248 return usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
249 command, 0xc0, 0, 0, data, 4);
253 * Resets the media status (but not the whole device?)
255 static int alauda_reset_media(struct us_data *us)
257 unsigned char *command = us->iobuf;
259 memset(command, 0, 9);
260 command[0] = ALAUDA_BULK_CMD;
261 command[1] = ALAUDA_BULK_RESET_MEDIA;
262 command[8] = MEDIA_PORT(us);
264 return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
265 command, 9, NULL);
269 * Examines the media and deduces capacity, etc.
271 static int alauda_init_media(struct us_data *us)
273 unsigned char *data = us->iobuf;
274 int ready = 0;
275 struct alauda_card_info *media_info;
276 unsigned int num_zones;
278 while (ready == 0) {
279 msleep(20);
281 if (alauda_get_media_status(us, data) != USB_STOR_XFER_GOOD)
282 return USB_STOR_TRANSPORT_ERROR;
284 if (data[0] & 0x10)
285 ready = 1;
288 US_DEBUGP("alauda_init_media: We are ready for action!\n");
290 if (alauda_ack_media(us) != USB_STOR_XFER_GOOD)
291 return USB_STOR_TRANSPORT_ERROR;
293 msleep(10);
295 if (alauda_get_media_status(us, data) != USB_STOR_XFER_GOOD)
296 return USB_STOR_TRANSPORT_ERROR;
298 if (data[0] != 0x14) {
299 US_DEBUGP("alauda_init_media: Media not ready after ack\n");
300 return USB_STOR_TRANSPORT_ERROR;
303 if (alauda_get_media_signature(us, data) != USB_STOR_XFER_GOOD)
304 return USB_STOR_TRANSPORT_ERROR;
306 US_DEBUGP("alauda_init_media: Media signature: %02X %02X %02X %02X\n",
307 data[0], data[1], data[2], data[3]);
308 media_info = alauda_card_find_id(data[1]);
309 if (media_info == NULL) {
310 printk("alauda_init_media: Unrecognised media signature: "
311 "%02X %02X %02X %02X\n",
312 data[0], data[1], data[2], data[3]);
313 return USB_STOR_TRANSPORT_ERROR;
316 MEDIA_INFO(us).capacity = 1 << media_info->chipshift;
317 US_DEBUGP("Found media with capacity: %ldMB\n",
318 MEDIA_INFO(us).capacity >> 20);
320 MEDIA_INFO(us).pageshift = media_info->pageshift;
321 MEDIA_INFO(us).blockshift = media_info->blockshift;
322 MEDIA_INFO(us).zoneshift = media_info->zoneshift;
324 MEDIA_INFO(us).pagesize = 1 << media_info->pageshift;
325 MEDIA_INFO(us).blocksize = 1 << media_info->blockshift;
326 MEDIA_INFO(us).zonesize = 1 << media_info->zoneshift;
328 MEDIA_INFO(us).uzonesize = ((1 << media_info->zoneshift) / 128) * 125;
329 MEDIA_INFO(us).blockmask = MEDIA_INFO(us).blocksize - 1;
331 num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift
332 + MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift);
333 MEDIA_INFO(us).pba_to_lba = kcalloc(num_zones, sizeof(u16*), GFP_NOIO);
334 MEDIA_INFO(us).lba_to_pba = kcalloc(num_zones, sizeof(u16*), GFP_NOIO);
336 if (alauda_reset_media(us) != USB_STOR_XFER_GOOD)
337 return USB_STOR_TRANSPORT_ERROR;
339 return USB_STOR_TRANSPORT_GOOD;
343 * Examines the media status and does the right thing when the media has gone,
344 * appeared, or changed.
346 static int alauda_check_media(struct us_data *us)
348 struct alauda_info *info = (struct alauda_info *) us->extra;
349 unsigned char status[2];
350 int rc;
352 rc = alauda_get_media_status(us, status);
354 /* Check for no media or door open */
355 if ((status[0] & 0x80) || ((status[0] & 0x1F) == 0x10)
356 || ((status[1] & 0x01) == 0)) {
357 US_DEBUGP("alauda_check_media: No media, or door open\n");
358 alauda_free_maps(&MEDIA_INFO(us));
359 info->sense_key = 0x02;
360 info->sense_asc = 0x3A;
361 info->sense_ascq = 0x00;
362 return USB_STOR_TRANSPORT_FAILED;
365 /* Check for media change */
366 if (status[0] & 0x08) {
367 US_DEBUGP("alauda_check_media: Media change detected\n");
368 alauda_free_maps(&MEDIA_INFO(us));
369 alauda_init_media(us);
371 info->sense_key = UNIT_ATTENTION;
372 info->sense_asc = 0x28;
373 info->sense_ascq = 0x00;
374 return USB_STOR_TRANSPORT_FAILED;
377 return USB_STOR_TRANSPORT_GOOD;
381 * Checks the status from the 2nd status register
382 * Returns 3 bytes of status data, only the first is known
384 static int alauda_check_status2(struct us_data *us)
386 int rc;
387 unsigned char command[] = {
388 ALAUDA_BULK_CMD, ALAUDA_BULK_GET_STATUS2,
389 0, 0, 0, 0, 3, 0, MEDIA_PORT(us)
391 unsigned char data[3];
393 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
394 command, 9, NULL);
395 if (rc != USB_STOR_XFER_GOOD)
396 return rc;
398 rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
399 data, 3, NULL);
400 if (rc != USB_STOR_XFER_GOOD)
401 return rc;
403 US_DEBUGP("alauda_check_status2: %02X %02X %02X\n", data[0], data[1], data[2]);
404 if (data[0] & ALAUDA_STATUS_ERROR)
405 return USB_STOR_XFER_ERROR;
407 return USB_STOR_XFER_GOOD;
411 * Gets the redundancy data for the first page of a PBA
412 * Returns 16 bytes.
414 static int alauda_get_redu_data(struct us_data *us, u16 pba, unsigned char *data)
416 int rc;
417 unsigned char command[] = {
418 ALAUDA_BULK_CMD, ALAUDA_BULK_GET_REDU_DATA,
419 PBA_HI(pba), PBA_ZONE(pba), 0, PBA_LO(pba), 0, 0, MEDIA_PORT(us)
422 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
423 command, 9, NULL);
424 if (rc != USB_STOR_XFER_GOOD)
425 return rc;
427 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
428 data, 16, NULL);
432 * Finds the first unused PBA in a zone
433 * Returns the absolute PBA of an unused PBA, or 0 if none found.
435 static u16 alauda_find_unused_pba(struct alauda_media_info *info,
436 unsigned int zone)
438 u16 *pba_to_lba = info->pba_to_lba[zone];
439 unsigned int i;
441 for (i = 0; i < info->zonesize; i++)
442 if (pba_to_lba[i] == UNDEF)
443 return (zone << info->zoneshift) + i;
445 return 0;
449 * Reads the redundancy data for all PBA's in a zone
450 * Produces lba <--> pba mappings
452 static int alauda_read_map(struct us_data *us, unsigned int zone)
454 unsigned char *data = us->iobuf;
455 int result;
456 int i, j;
457 unsigned int zonesize = MEDIA_INFO(us).zonesize;
458 unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
459 unsigned int lba_offset, lba_real, blocknum;
460 unsigned int zone_base_lba = zone * uzonesize;
461 unsigned int zone_base_pba = zone * zonesize;
462 u16 *lba_to_pba = kcalloc(zonesize, sizeof(u16), GFP_NOIO);
463 u16 *pba_to_lba = kcalloc(zonesize, sizeof(u16), GFP_NOIO);
464 if (lba_to_pba == NULL || pba_to_lba == NULL) {
465 result = USB_STOR_TRANSPORT_ERROR;
466 goto error;
469 US_DEBUGP("alauda_read_map: Mapping blocks for zone %d\n", zone);
471 /* 1024 PBA's per zone */
472 for (i = 0; i < zonesize; i++)
473 lba_to_pba[i] = pba_to_lba[i] = UNDEF;
475 for (i = 0; i < zonesize; i++) {
476 blocknum = zone_base_pba + i;
478 result = alauda_get_redu_data(us, blocknum, data);
479 if (result != USB_STOR_XFER_GOOD) {
480 result = USB_STOR_TRANSPORT_ERROR;
481 goto error;
484 /* special PBAs have control field 0^16 */
485 for (j = 0; j < 16; j++)
486 if (data[j] != 0)
487 goto nonz;
488 pba_to_lba[i] = UNUSABLE;
489 US_DEBUGP("alauda_read_map: PBA %d has no logical mapping\n", blocknum);
490 continue;
492 nonz:
493 /* unwritten PBAs have control field FF^16 */
494 for (j = 0; j < 16; j++)
495 if (data[j] != 0xff)
496 goto nonff;
497 continue;
499 nonff:
500 /* normal PBAs start with six FFs */
501 if (j < 6) {
502 US_DEBUGP("alauda_read_map: PBA %d has no logical mapping: "
503 "reserved area = %02X%02X%02X%02X "
504 "data status %02X block status %02X\n",
505 blocknum, data[0], data[1], data[2], data[3],
506 data[4], data[5]);
507 pba_to_lba[i] = UNUSABLE;
508 continue;
511 if ((data[6] >> 4) != 0x01) {
512 US_DEBUGP("alauda_read_map: PBA %d has invalid address "
513 "field %02X%02X/%02X%02X\n",
514 blocknum, data[6], data[7], data[11], data[12]);
515 pba_to_lba[i] = UNUSABLE;
516 continue;
519 /* check even parity */
520 if (parity[data[6] ^ data[7]]) {
521 printk("alauda_read_map: Bad parity in LBA for block %d"
522 " (%02X %02X)\n", i, data[6], data[7]);
523 pba_to_lba[i] = UNUSABLE;
524 continue;
527 lba_offset = short_pack(data[7], data[6]);
528 lba_offset = (lba_offset & 0x07FF) >> 1;
529 lba_real = lba_offset + zone_base_lba;
532 * Every 1024 physical blocks ("zone"), the LBA numbers
533 * go back to zero, but are within a higher block of LBA's.
534 * Also, there is a maximum of 1000 LBA's per zone.
535 * In other words, in PBA 1024-2047 you will find LBA 0-999
536 * which are really LBA 1000-1999. This allows for 24 bad
537 * or special physical blocks per zone.
540 if (lba_offset >= uzonesize) {
541 printk("alauda_read_map: Bad low LBA %d for block %d\n",
542 lba_real, blocknum);
543 continue;
546 if (lba_to_pba[lba_offset] != UNDEF) {
547 printk("alauda_read_map: LBA %d seen for PBA %d and %d\n",
548 lba_real, lba_to_pba[lba_offset], blocknum);
549 continue;
552 pba_to_lba[i] = lba_real;
553 lba_to_pba[lba_offset] = blocknum;
554 continue;
557 MEDIA_INFO(us).lba_to_pba[zone] = lba_to_pba;
558 MEDIA_INFO(us).pba_to_lba[zone] = pba_to_lba;
559 result = 0;
560 goto out;
562 error:
563 kfree(lba_to_pba);
564 kfree(pba_to_lba);
565 out:
566 return result;
570 * Checks to see whether we have already mapped a certain zone
571 * If we haven't, the map is generated
573 static void alauda_ensure_map_for_zone(struct us_data *us, unsigned int zone)
575 if (MEDIA_INFO(us).lba_to_pba[zone] == NULL
576 || MEDIA_INFO(us).pba_to_lba[zone] == NULL)
577 alauda_read_map(us, zone);
581 * Erases an entire block
583 static int alauda_erase_block(struct us_data *us, u16 pba)
585 int rc;
586 unsigned char command[] = {
587 ALAUDA_BULK_CMD, ALAUDA_BULK_ERASE_BLOCK, PBA_HI(pba),
588 PBA_ZONE(pba), 0, PBA_LO(pba), 0x02, 0, MEDIA_PORT(us)
590 unsigned char buf[2];
592 US_DEBUGP("alauda_erase_block: Erasing PBA %d\n", pba);
594 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
595 command, 9, NULL);
596 if (rc != USB_STOR_XFER_GOOD)
597 return rc;
599 rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
600 buf, 2, NULL);
601 if (rc != USB_STOR_XFER_GOOD)
602 return rc;
604 US_DEBUGP("alauda_erase_block: Erase result: %02X %02X\n",
605 buf[0], buf[1]);
606 return rc;
610 * Reads data from a certain offset page inside a PBA, including interleaved
611 * redundancy data. Returns (pagesize+64)*pages bytes in data.
613 static int alauda_read_block_raw(struct us_data *us, u16 pba,
614 unsigned int page, unsigned int pages, unsigned char *data)
616 int rc;
617 unsigned char command[] = {
618 ALAUDA_BULK_CMD, ALAUDA_BULK_READ_BLOCK, PBA_HI(pba),
619 PBA_ZONE(pba), 0, PBA_LO(pba) + page, pages, 0, MEDIA_PORT(us)
622 US_DEBUGP("alauda_read_block: pba %d page %d count %d\n",
623 pba, page, pages);
625 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
626 command, 9, NULL);
627 if (rc != USB_STOR_XFER_GOOD)
628 return rc;
630 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
631 data, (MEDIA_INFO(us).pagesize + 64) * pages, NULL);
635 * Reads data from a certain offset page inside a PBA, excluding redundancy
636 * data. Returns pagesize*pages bytes in data. Note that data must be big enough
637 * to hold (pagesize+64)*pages bytes of data, but you can ignore those 'extra'
638 * trailing bytes outside this function.
640 static int alauda_read_block(struct us_data *us, u16 pba,
641 unsigned int page, unsigned int pages, unsigned char *data)
643 int i, rc;
644 unsigned int pagesize = MEDIA_INFO(us).pagesize;
646 rc = alauda_read_block_raw(us, pba, page, pages, data);
647 if (rc != USB_STOR_XFER_GOOD)
648 return rc;
650 /* Cut out the redundancy data */
651 for (i = 0; i < pages; i++) {
652 int dest_offset = i * pagesize;
653 int src_offset = i * (pagesize + 64);
654 memmove(data + dest_offset, data + src_offset, pagesize);
657 return rc;
661 * Writes an entire block of data and checks status after write.
662 * Redundancy data must be already included in data. Data should be
663 * (pagesize+64)*blocksize bytes in length.
665 static int alauda_write_block(struct us_data *us, u16 pba, unsigned char *data)
667 int rc;
668 struct alauda_info *info = (struct alauda_info *) us->extra;
669 unsigned char command[] = {
670 ALAUDA_BULK_CMD, ALAUDA_BULK_WRITE_BLOCK, PBA_HI(pba),
671 PBA_ZONE(pba), 0, PBA_LO(pba), 32, 0, MEDIA_PORT(us)
674 US_DEBUGP("alauda_write_block: pba %d\n", pba);
676 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
677 command, 9, NULL);
678 if (rc != USB_STOR_XFER_GOOD)
679 return rc;
681 rc = usb_stor_bulk_transfer_buf(us, info->wr_ep, data,
682 (MEDIA_INFO(us).pagesize + 64) * MEDIA_INFO(us).blocksize,
683 NULL);
684 if (rc != USB_STOR_XFER_GOOD)
685 return rc;
687 return alauda_check_status2(us);
691 * Write some data to a specific LBA.
693 static int alauda_write_lba(struct us_data *us, u16 lba,
694 unsigned int page, unsigned int pages,
695 unsigned char *ptr, unsigned char *blockbuffer)
697 u16 pba, lbap, new_pba;
698 unsigned char *bptr, *cptr, *xptr;
699 unsigned char ecc[3];
700 int i, result;
701 unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
702 unsigned int zonesize = MEDIA_INFO(us).zonesize;
703 unsigned int pagesize = MEDIA_INFO(us).pagesize;
704 unsigned int blocksize = MEDIA_INFO(us).blocksize;
705 unsigned int lba_offset = lba % uzonesize;
706 unsigned int new_pba_offset;
707 unsigned int zone = lba / uzonesize;
709 alauda_ensure_map_for_zone(us, zone);
711 pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
712 if (pba == 1) {
713 /* Maybe it is impossible to write to PBA 1.
714 Fake success, but don't do anything. */
715 printk("alauda_write_lba: avoid writing to pba 1\n");
716 return USB_STOR_TRANSPORT_GOOD;
719 new_pba = alauda_find_unused_pba(&MEDIA_INFO(us), zone);
720 if (!new_pba) {
721 printk("alauda_write_lba: Out of unused blocks\n");
722 return USB_STOR_TRANSPORT_ERROR;
725 /* read old contents */
726 if (pba != UNDEF) {
727 result = alauda_read_block_raw(us, pba, 0,
728 blocksize, blockbuffer);
729 if (result != USB_STOR_XFER_GOOD)
730 return result;
731 } else {
732 memset(blockbuffer, 0, blocksize * (pagesize + 64));
735 lbap = (lba_offset << 1) | 0x1000;
736 if (parity[MSB_of(lbap) ^ LSB_of(lbap)])
737 lbap ^= 1;
739 /* check old contents and fill lba */
740 for (i = 0; i < blocksize; i++) {
741 bptr = blockbuffer + (i * (pagesize + 64));
742 cptr = bptr + pagesize;
743 nand_compute_ecc(bptr, ecc);
744 if (!nand_compare_ecc(cptr+13, ecc)) {
745 US_DEBUGP("Warning: bad ecc in page %d- of pba %d\n",
746 i, pba);
747 nand_store_ecc(cptr+13, ecc);
749 nand_compute_ecc(bptr + (pagesize / 2), ecc);
750 if (!nand_compare_ecc(cptr+8, ecc)) {
751 US_DEBUGP("Warning: bad ecc in page %d+ of pba %d\n",
752 i, pba);
753 nand_store_ecc(cptr+8, ecc);
755 cptr[6] = cptr[11] = MSB_of(lbap);
756 cptr[7] = cptr[12] = LSB_of(lbap);
759 /* copy in new stuff and compute ECC */
760 xptr = ptr;
761 for (i = page; i < page+pages; i++) {
762 bptr = blockbuffer + (i * (pagesize + 64));
763 cptr = bptr + pagesize;
764 memcpy(bptr, xptr, pagesize);
765 xptr += pagesize;
766 nand_compute_ecc(bptr, ecc);
767 nand_store_ecc(cptr+13, ecc);
768 nand_compute_ecc(bptr + (pagesize / 2), ecc);
769 nand_store_ecc(cptr+8, ecc);
772 result = alauda_write_block(us, new_pba, blockbuffer);
773 if (result != USB_STOR_XFER_GOOD)
774 return result;
776 new_pba_offset = new_pba - (zone * zonesize);
777 MEDIA_INFO(us).pba_to_lba[zone][new_pba_offset] = lba;
778 MEDIA_INFO(us).lba_to_pba[zone][lba_offset] = new_pba;
779 US_DEBUGP("alauda_write_lba: Remapped LBA %d to PBA %d\n",
780 lba, new_pba);
782 if (pba != UNDEF) {
783 unsigned int pba_offset = pba - (zone * zonesize);
784 result = alauda_erase_block(us, pba);
785 if (result != USB_STOR_XFER_GOOD)
786 return result;
787 MEDIA_INFO(us).pba_to_lba[zone][pba_offset] = UNDEF;
790 return USB_STOR_TRANSPORT_GOOD;
794 * Read data from a specific sector address
796 static int alauda_read_data(struct us_data *us, unsigned long address,
797 unsigned int sectors)
799 unsigned char *buffer;
800 u16 lba, max_lba;
801 unsigned int page, len, offset;
802 unsigned int blockshift = MEDIA_INFO(us).blockshift;
803 unsigned int pageshift = MEDIA_INFO(us).pageshift;
804 unsigned int blocksize = MEDIA_INFO(us).blocksize;
805 unsigned int pagesize = MEDIA_INFO(us).pagesize;
806 unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
807 struct scatterlist *sg;
808 int result;
811 * Since we only read in one block at a time, we have to create
812 * a bounce buffer and move the data a piece at a time between the
813 * bounce buffer and the actual transfer buffer.
814 * We make this buffer big enough to hold temporary redundancy data,
815 * which we use when reading the data blocks.
818 len = min(sectors, blocksize) * (pagesize + 64);
819 buffer = kmalloc(len, GFP_NOIO);
820 if (buffer == NULL) {
821 printk("alauda_read_data: Out of memory\n");
822 return USB_STOR_TRANSPORT_ERROR;
825 /* Figure out the initial LBA and page */
826 lba = address >> blockshift;
827 page = (address & MEDIA_INFO(us).blockmask);
828 max_lba = MEDIA_INFO(us).capacity >> (blockshift + pageshift);
830 result = USB_STOR_TRANSPORT_GOOD;
831 offset = 0;
832 sg = NULL;
834 while (sectors > 0) {
835 unsigned int zone = lba / uzonesize; /* integer division */
836 unsigned int lba_offset = lba - (zone * uzonesize);
837 unsigned int pages;
838 u16 pba;
839 alauda_ensure_map_for_zone(us, zone);
841 /* Not overflowing capacity? */
842 if (lba >= max_lba) {
843 US_DEBUGP("Error: Requested lba %u exceeds "
844 "maximum %u\n", lba, max_lba);
845 result = USB_STOR_TRANSPORT_ERROR;
846 break;
849 /* Find number of pages we can read in this block */
850 pages = min(sectors, blocksize - page);
851 len = pages << pageshift;
853 /* Find where this lba lives on disk */
854 pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
856 if (pba == UNDEF) { /* this lba was never written */
857 US_DEBUGP("Read %d zero pages (LBA %d) page %d\n",
858 pages, lba, page);
860 /* This is not really an error. It just means
861 that the block has never been written.
862 Instead of returning USB_STOR_TRANSPORT_ERROR
863 it is better to return all zero data. */
865 memset(buffer, 0, len);
866 } else {
867 US_DEBUGP("Read %d pages, from PBA %d"
868 " (LBA %d) page %d\n",
869 pages, pba, lba, page);
871 result = alauda_read_block(us, pba, page, pages, buffer);
872 if (result != USB_STOR_TRANSPORT_GOOD)
873 break;
876 /* Store the data in the transfer buffer */
877 usb_stor_access_xfer_buf(buffer, len, us->srb,
878 &sg, &offset, TO_XFER_BUF);
880 page = 0;
881 lba++;
882 sectors -= pages;
885 kfree(buffer);
886 return result;
890 * Write data to a specific sector address
892 static int alauda_write_data(struct us_data *us, unsigned long address,
893 unsigned int sectors)
895 unsigned char *buffer, *blockbuffer;
896 unsigned int page, len, offset;
897 unsigned int blockshift = MEDIA_INFO(us).blockshift;
898 unsigned int pageshift = MEDIA_INFO(us).pageshift;
899 unsigned int blocksize = MEDIA_INFO(us).blocksize;
900 unsigned int pagesize = MEDIA_INFO(us).pagesize;
901 struct scatterlist *sg;
902 u16 lba, max_lba;
903 int result;
906 * Since we don't write the user data directly to the device,
907 * we have to create a bounce buffer and move the data a piece
908 * at a time between the bounce buffer and the actual transfer buffer.
911 len = min(sectors, blocksize) * pagesize;
912 buffer = kmalloc(len, GFP_NOIO);
913 if (buffer == NULL) {
914 printk("alauda_write_data: Out of memory\n");
915 return USB_STOR_TRANSPORT_ERROR;
919 * We also need a temporary block buffer, where we read in the old data,
920 * overwrite parts with the new data, and manipulate the redundancy data
922 blockbuffer = kmalloc((pagesize + 64) * blocksize, GFP_NOIO);
923 if (blockbuffer == NULL) {
924 printk("alauda_write_data: Out of memory\n");
925 kfree(buffer);
926 return USB_STOR_TRANSPORT_ERROR;
929 /* Figure out the initial LBA and page */
930 lba = address >> blockshift;
931 page = (address & MEDIA_INFO(us).blockmask);
932 max_lba = MEDIA_INFO(us).capacity >> (pageshift + blockshift);
934 result = USB_STOR_TRANSPORT_GOOD;
935 offset = 0;
936 sg = NULL;
938 while (sectors > 0) {
939 /* Write as many sectors as possible in this block */
940 unsigned int pages = min(sectors, blocksize - page);
941 len = pages << pageshift;
943 /* Not overflowing capacity? */
944 if (lba >= max_lba) {
945 US_DEBUGP("alauda_write_data: Requested lba %u exceeds "
946 "maximum %u\n", lba, max_lba);
947 result = USB_STOR_TRANSPORT_ERROR;
948 break;
951 /* Get the data from the transfer buffer */
952 usb_stor_access_xfer_buf(buffer, len, us->srb,
953 &sg, &offset, FROM_XFER_BUF);
955 result = alauda_write_lba(us, lba, page, pages, buffer,
956 blockbuffer);
957 if (result != USB_STOR_TRANSPORT_GOOD)
958 break;
960 page = 0;
961 lba++;
962 sectors -= pages;
965 kfree(buffer);
966 kfree(blockbuffer);
967 return result;
971 * Our interface with the rest of the world
974 static void alauda_info_destructor(void *extra)
976 struct alauda_info *info = (struct alauda_info *) extra;
977 int port;
979 if (!info)
980 return;
982 for (port = 0; port < 2; port++) {
983 struct alauda_media_info *media_info = &info->port[port];
985 alauda_free_maps(media_info);
986 kfree(media_info->lba_to_pba);
987 kfree(media_info->pba_to_lba);
992 * Initialize alauda_info struct and find the data-write endpoint
994 int init_alauda(struct us_data *us)
996 struct alauda_info *info;
997 struct usb_host_interface *altsetting = us->pusb_intf->cur_altsetting;
998 nand_init_ecc();
1000 us->extra = kzalloc(sizeof(struct alauda_info), GFP_NOIO);
1001 if (!us->extra) {
1002 US_DEBUGP("init_alauda: Gah! Can't allocate storage for"
1003 "alauda info struct!\n");
1004 return USB_STOR_TRANSPORT_ERROR;
1006 info = (struct alauda_info *) us->extra;
1007 us->extra_destructor = alauda_info_destructor;
1009 info->wr_ep = usb_sndbulkpipe(us->pusb_dev,
1010 altsetting->endpoint[0].desc.bEndpointAddress
1011 & USB_ENDPOINT_NUMBER_MASK);
1013 return USB_STOR_TRANSPORT_GOOD;
1016 int alauda_transport(struct scsi_cmnd *srb, struct us_data *us)
1018 int rc;
1019 struct alauda_info *info = (struct alauda_info *) us->extra;
1020 unsigned char *ptr = us->iobuf;
1021 static unsigned char inquiry_response[36] = {
1022 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1025 if (srb->cmnd[0] == INQUIRY) {
1026 US_DEBUGP("alauda_transport: INQUIRY. "
1027 "Returning bogus response.\n");
1028 memcpy(ptr, inquiry_response, sizeof(inquiry_response));
1029 fill_inquiry_response(us, ptr, 36);
1030 return USB_STOR_TRANSPORT_GOOD;
1033 if (srb->cmnd[0] == TEST_UNIT_READY) {
1034 US_DEBUGP("alauda_transport: TEST_UNIT_READY.\n");
1035 return alauda_check_media(us);
1038 if (srb->cmnd[0] == READ_CAPACITY) {
1039 unsigned int num_zones;
1040 unsigned long capacity;
1042 rc = alauda_check_media(us);
1043 if (rc != USB_STOR_TRANSPORT_GOOD)
1044 return rc;
1046 num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift
1047 + MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift);
1049 capacity = num_zones * MEDIA_INFO(us).uzonesize
1050 * MEDIA_INFO(us).blocksize;
1052 /* Report capacity and page size */
1053 ((__be32 *) ptr)[0] = cpu_to_be32(capacity - 1);
1054 ((__be32 *) ptr)[1] = cpu_to_be32(512);
1056 usb_stor_set_xfer_buf(ptr, 8, srb);
1057 return USB_STOR_TRANSPORT_GOOD;
1060 if (srb->cmnd[0] == READ_10) {
1061 unsigned int page, pages;
1063 rc = alauda_check_media(us);
1064 if (rc != USB_STOR_TRANSPORT_GOOD)
1065 return rc;
1067 page = short_pack(srb->cmnd[3], srb->cmnd[2]);
1068 page <<= 16;
1069 page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
1070 pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
1072 US_DEBUGP("alauda_transport: READ_10: page %d pagect %d\n",
1073 page, pages);
1075 return alauda_read_data(us, page, pages);
1078 if (srb->cmnd[0] == WRITE_10) {
1079 unsigned int page, pages;
1081 rc = alauda_check_media(us);
1082 if (rc != USB_STOR_TRANSPORT_GOOD)
1083 return rc;
1085 page = short_pack(srb->cmnd[3], srb->cmnd[2]);
1086 page <<= 16;
1087 page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
1088 pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
1090 US_DEBUGP("alauda_transport: WRITE_10: page %d pagect %d\n",
1091 page, pages);
1093 return alauda_write_data(us, page, pages);
1096 if (srb->cmnd[0] == REQUEST_SENSE) {
1097 US_DEBUGP("alauda_transport: REQUEST_SENSE.\n");
1099 memset(ptr, 0, 18);
1100 ptr[0] = 0xF0;
1101 ptr[2] = info->sense_key;
1102 ptr[7] = 11;
1103 ptr[12] = info->sense_asc;
1104 ptr[13] = info->sense_ascq;
1105 usb_stor_set_xfer_buf(ptr, 18, srb);
1107 return USB_STOR_TRANSPORT_GOOD;
1110 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
1111 /* sure. whatever. not like we can stop the user from popping
1112 the media out of the device (no locking doors, etc) */
1113 return USB_STOR_TRANSPORT_GOOD;
1116 US_DEBUGP("alauda_transport: Gah! Unknown command: %d (0x%x)\n",
1117 srb->cmnd[0], srb->cmnd[0]);
1118 info->sense_key = 0x05;
1119 info->sense_asc = 0x20;
1120 info->sense_ascq = 0x00;
1121 return USB_STOR_TRANSPORT_FAILED;