blob: 5407411e30e0628d9b175f0396a6ed281dfe152b [file] [log] [blame]
Matthew Dharme80b0fa2005-12-04 22:02:44 -08001/*
2 * Driver for Alauda-based card readers
3 *
4 * Current development and maintenance by:
5 * (c) 2005 Daniel Drake <dsd@gentoo.org>
6 *
7 * The 'Alauda' is a chip manufacturered by RATOC for OEM use.
8 *
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.
12 *
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.
16 *
17 * For protocol info, see http://alauda.sourceforge.net
18 *
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.
23 *
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.
28 *
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.
32 */
33
34#include <scsi/scsi.h>
35#include <scsi/scsi_cmnd.h>
36#include <scsi/scsi_device.h>
37
38#include "usb.h"
39#include "transport.h"
40#include "protocol.h"
41#include "debug.h"
42#include "alauda.h"
43
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)
47
48#define MEDIA_PORT(us) us->srb->device->lun
49#define MEDIA_INFO(us) ((struct alauda_info *)us->extra)->port[MEDIA_PORT(us)]
50
51#define PBA_LO(pba) ((pba & 0xF) << 5)
52#define PBA_HI(pba) (pba >> 3)
53#define PBA_ZONE(pba) (pba >> 11)
54
55/*
56 * Media handling
57 */
58
59struct 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 */
65};
66
67static 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 */
83
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,}
91};
92
93static struct alauda_card_info *alauda_card_find_id(unsigned char id) {
94 int i;
95
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;
100}
101
102/*
103 * ECC computation.
104 */
105
106static unsigned char parity[256];
107static unsigned char ecc2[256];
108
109static void nand_init_ecc(void) {
110 int i, j, a;
111
112 parity[0] = 0;
113 for (i = 1; i < 256; i++)
114 parity[i] = (parity[i&(i-1)] ^ 1);
115
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;
126 }
127 }
128 ecc2[i] = ~(a ^ (a<<1) ^ (parity[i] ? 0xa8 : 0));
129 }
130}
131
132/* compute 3-byte ecc on 256 bytes */
133static void nand_compute_ecc(unsigned char *data, unsigned char *ecc) {
134 int i, j, a;
135 unsigned char par, bit, bits[8];
136
137 par = 0;
138 for (j = 0; j < 8; j++)
139 bits[j] = 0;
140
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;
148 }
149
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));
153
154 a = (bits[7] << 6) + (bits[6] << 4) + (bits[5] << 2) + bits[4];
155 ecc[1] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
156
157 ecc[2] = ecc2[par];
158}
159
160static int nand_compare_ecc(unsigned char *data, unsigned char *ecc) {
161 return (data[0] == ecc[0] && data[1] == ecc[1] && data[2] == ecc[2]);
162}
163
164static void nand_store_ecc(unsigned char *data, unsigned char *ecc) {
165 memcpy(data, ecc, 3);
166}
167
168/*
169 * Alauda driver
170 */
171
172/*
173 * Forget our PBA <---> LBA mappings for a particular port
174 */
175static void alauda_free_maps (struct alauda_media_info *media_info)
176{
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;
181
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;
186 }
187
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;
192 }
193}
194
195/*
196 * Returns 2 bytes of status data
197 * The first byte describes media status, and second byte describes door status
198 */
199static int alauda_get_media_status(struct us_data *us, unsigned char *data)
200{
201 int rc;
202 unsigned char command;
203
204 if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
205 command = ALAUDA_GET_XD_MEDIA_STATUS;
206 else
207 command = ALAUDA_GET_SM_MEDIA_STATUS;
208
209 rc = usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
210 command, 0xc0, 0, 1, data, 2);
211
212 US_DEBUGP("alauda_get_media_status: Media status %02X %02X\n",
213 data[0], data[1]);
214
215 return rc;
216}
217
218/*
219 * Clears the "media was changed" bit so that we know when it changes again
220 * in the future.
221 */
222static int alauda_ack_media(struct us_data *us)
223{
224 unsigned char command;
225
226 if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
227 command = ALAUDA_ACK_XD_MEDIA_CHANGE;
228 else
229 command = ALAUDA_ACK_SM_MEDIA_CHANGE;
230
231 return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
232 command, 0x40, 0, 1, NULL, 0);
233}
234
235/*
236 * Retrieves a 4-byte media signature, which indicates manufacturer, capacity,
237 * and some other details.
238 */
239static int alauda_get_media_signature(struct us_data *us, unsigned char *data)
240{
241 unsigned char command;
242
243 if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
244 command = ALAUDA_GET_XD_MEDIA_SIG;
245 else
246 command = ALAUDA_GET_SM_MEDIA_SIG;
247
248 return usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
249 command, 0xc0, 0, 0, data, 4);
250}
251
252/*
253 * Resets the media status (but not the whole device?)
254 */
255static int alauda_reset_media(struct us_data *us)
256{
257 unsigned char *command = us->iobuf;
258
259 memset(command, 0, 9);
260 command[0] = ALAUDA_BULK_CMD;
261 command[1] = ALAUDA_BULK_RESET_MEDIA;
262 command[8] = MEDIA_PORT(us);
263
264 return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
265 command, 9, NULL);
266}
267
268/*
269 * Examines the media and deduces capacity, etc.
270 */
271static int alauda_init_media(struct us_data *us)
272{
273 unsigned char *data = us->iobuf;
274 int ready = 0;
275 struct alauda_card_info *media_info;
276 unsigned int num_zones;
277
278 while (ready == 0) {
279 msleep(20);
280
281 if (alauda_get_media_status(us, data) != USB_STOR_XFER_GOOD)
282 return USB_STOR_TRANSPORT_ERROR;
283
284 if (data[0] & 0x10)
285 ready = 1;
286 }
287
288 US_DEBUGP("alauda_init_media: We are ready for action!\n");
289
290 if (alauda_ack_media(us) != USB_STOR_XFER_GOOD)
291 return USB_STOR_TRANSPORT_ERROR;
292
293 msleep(10);
294
295 if (alauda_get_media_status(us, data) != USB_STOR_XFER_GOOD)
296 return USB_STOR_TRANSPORT_ERROR;
297
298 if (data[0] != 0x14) {
299 US_DEBUGP("alauda_init_media: Media not ready after ack\n");
300 return USB_STOR_TRANSPORT_ERROR;
301 }
302
303 if (alauda_get_media_signature(us, data) != USB_STOR_XFER_GOOD)
304 return USB_STOR_TRANSPORT_ERROR;
305
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) {
Frank Seidel6f8aa652009-02-05 16:16:24 +0100310 printk(KERN_WARNING
311 "alauda_init_media: Unrecognised media signature: "
Matthew Dharme80b0fa2005-12-04 22:02:44 -0800312 "%02X %02X %02X %02X\n",
313 data[0], data[1], data[2], data[3]);
314 return USB_STOR_TRANSPORT_ERROR;
315 }
316
317 MEDIA_INFO(us).capacity = 1 << media_info->chipshift;
318 US_DEBUGP("Found media with capacity: %ldMB\n",
319 MEDIA_INFO(us).capacity >> 20);
320
321 MEDIA_INFO(us).pageshift = media_info->pageshift;
322 MEDIA_INFO(us).blockshift = media_info->blockshift;
323 MEDIA_INFO(us).zoneshift = media_info->zoneshift;
324
325 MEDIA_INFO(us).pagesize = 1 << media_info->pageshift;
326 MEDIA_INFO(us).blocksize = 1 << media_info->blockshift;
327 MEDIA_INFO(us).zonesize = 1 << media_info->zoneshift;
328
329 MEDIA_INFO(us).uzonesize = ((1 << media_info->zoneshift) / 128) * 125;
330 MEDIA_INFO(us).blockmask = MEDIA_INFO(us).blocksize - 1;
331
332 num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift
333 + MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift);
334 MEDIA_INFO(us).pba_to_lba = kcalloc(num_zones, sizeof(u16*), GFP_NOIO);
335 MEDIA_INFO(us).lba_to_pba = kcalloc(num_zones, sizeof(u16*), GFP_NOIO);
336
337 if (alauda_reset_media(us) != USB_STOR_XFER_GOOD)
338 return USB_STOR_TRANSPORT_ERROR;
339
340 return USB_STOR_TRANSPORT_GOOD;
341}
342
343/*
344 * Examines the media status and does the right thing when the media has gone,
345 * appeared, or changed.
346 */
347static int alauda_check_media(struct us_data *us)
348{
349 struct alauda_info *info = (struct alauda_info *) us->extra;
350 unsigned char status[2];
351 int rc;
352
353 rc = alauda_get_media_status(us, status);
354
355 /* Check for no media or door open */
356 if ((status[0] & 0x80) || ((status[0] & 0x1F) == 0x10)
357 || ((status[1] & 0x01) == 0)) {
358 US_DEBUGP("alauda_check_media: No media, or door open\n");
359 alauda_free_maps(&MEDIA_INFO(us));
360 info->sense_key = 0x02;
361 info->sense_asc = 0x3A;
362 info->sense_ascq = 0x00;
363 return USB_STOR_TRANSPORT_FAILED;
364 }
365
366 /* Check for media change */
367 if (status[0] & 0x08) {
368 US_DEBUGP("alauda_check_media: Media change detected\n");
369 alauda_free_maps(&MEDIA_INFO(us));
370 alauda_init_media(us);
371
372 info->sense_key = UNIT_ATTENTION;
373 info->sense_asc = 0x28;
374 info->sense_ascq = 0x00;
375 return USB_STOR_TRANSPORT_FAILED;
376 }
377
378 return USB_STOR_TRANSPORT_GOOD;
379}
380
381/*
382 * Checks the status from the 2nd status register
383 * Returns 3 bytes of status data, only the first is known
384 */
385static int alauda_check_status2(struct us_data *us)
386{
387 int rc;
388 unsigned char command[] = {
389 ALAUDA_BULK_CMD, ALAUDA_BULK_GET_STATUS2,
390 0, 0, 0, 0, 3, 0, MEDIA_PORT(us)
391 };
392 unsigned char data[3];
393
394 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
395 command, 9, NULL);
396 if (rc != USB_STOR_XFER_GOOD)
397 return rc;
398
399 rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
400 data, 3, NULL);
401 if (rc != USB_STOR_XFER_GOOD)
402 return rc;
403
404 US_DEBUGP("alauda_check_status2: %02X %02X %02X\n", data[0], data[1], data[2]);
405 if (data[0] & ALAUDA_STATUS_ERROR)
406 return USB_STOR_XFER_ERROR;
407
408 return USB_STOR_XFER_GOOD;
409}
410
411/*
412 * Gets the redundancy data for the first page of a PBA
413 * Returns 16 bytes.
414 */
415static int alauda_get_redu_data(struct us_data *us, u16 pba, unsigned char *data)
416{
417 int rc;
418 unsigned char command[] = {
419 ALAUDA_BULK_CMD, ALAUDA_BULK_GET_REDU_DATA,
420 PBA_HI(pba), PBA_ZONE(pba), 0, PBA_LO(pba), 0, 0, MEDIA_PORT(us)
421 };
422
423 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
424 command, 9, NULL);
425 if (rc != USB_STOR_XFER_GOOD)
426 return rc;
427
428 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
429 data, 16, NULL);
430}
431
432/*
433 * Finds the first unused PBA in a zone
434 * Returns the absolute PBA of an unused PBA, or 0 if none found.
435 */
436static u16 alauda_find_unused_pba(struct alauda_media_info *info,
437 unsigned int zone)
438{
439 u16 *pba_to_lba = info->pba_to_lba[zone];
440 unsigned int i;
441
442 for (i = 0; i < info->zonesize; i++)
443 if (pba_to_lba[i] == UNDEF)
444 return (zone << info->zoneshift) + i;
445
446 return 0;
447}
448
449/*
450 * Reads the redundancy data for all PBA's in a zone
451 * Produces lba <--> pba mappings
452 */
453static int alauda_read_map(struct us_data *us, unsigned int zone)
454{
455 unsigned char *data = us->iobuf;
456 int result;
457 int i, j;
458 unsigned int zonesize = MEDIA_INFO(us).zonesize;
459 unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
460 unsigned int lba_offset, lba_real, blocknum;
461 unsigned int zone_base_lba = zone * uzonesize;
462 unsigned int zone_base_pba = zone * zonesize;
463 u16 *lba_to_pba = kcalloc(zonesize, sizeof(u16), GFP_NOIO);
464 u16 *pba_to_lba = kcalloc(zonesize, sizeof(u16), GFP_NOIO);
465 if (lba_to_pba == NULL || pba_to_lba == NULL) {
466 result = USB_STOR_TRANSPORT_ERROR;
467 goto error;
468 }
469
470 US_DEBUGP("alauda_read_map: Mapping blocks for zone %d\n", zone);
471
472 /* 1024 PBA's per zone */
473 for (i = 0; i < zonesize; i++)
474 lba_to_pba[i] = pba_to_lba[i] = UNDEF;
475
476 for (i = 0; i < zonesize; i++) {
477 blocknum = zone_base_pba + i;
478
479 result = alauda_get_redu_data(us, blocknum, data);
480 if (result != USB_STOR_XFER_GOOD) {
481 result = USB_STOR_TRANSPORT_ERROR;
482 goto error;
483 }
484
485 /* special PBAs have control field 0^16 */
486 for (j = 0; j < 16; j++)
487 if (data[j] != 0)
488 goto nonz;
489 pba_to_lba[i] = UNUSABLE;
490 US_DEBUGP("alauda_read_map: PBA %d has no logical mapping\n", blocknum);
491 continue;
492
493 nonz:
494 /* unwritten PBAs have control field FF^16 */
495 for (j = 0; j < 16; j++)
496 if (data[j] != 0xff)
497 goto nonff;
498 continue;
499
500 nonff:
501 /* normal PBAs start with six FFs */
502 if (j < 6) {
503 US_DEBUGP("alauda_read_map: PBA %d has no logical mapping: "
504 "reserved area = %02X%02X%02X%02X "
505 "data status %02X block status %02X\n",
506 blocknum, data[0], data[1], data[2], data[3],
507 data[4], data[5]);
508 pba_to_lba[i] = UNUSABLE;
509 continue;
510 }
511
512 if ((data[6] >> 4) != 0x01) {
513 US_DEBUGP("alauda_read_map: PBA %d has invalid address "
514 "field %02X%02X/%02X%02X\n",
515 blocknum, data[6], data[7], data[11], data[12]);
516 pba_to_lba[i] = UNUSABLE;
517 continue;
518 }
519
520 /* check even parity */
521 if (parity[data[6] ^ data[7]]) {
Frank Seidel6f8aa652009-02-05 16:16:24 +0100522 printk(KERN_WARNING
523 "alauda_read_map: Bad parity in LBA for block %d"
Matthew Dharme80b0fa2005-12-04 22:02:44 -0800524 " (%02X %02X)\n", i, data[6], data[7]);
525 pba_to_lba[i] = UNUSABLE;
526 continue;
527 }
528
529 lba_offset = short_pack(data[7], data[6]);
530 lba_offset = (lba_offset & 0x07FF) >> 1;
531 lba_real = lba_offset + zone_base_lba;
532
533 /*
534 * Every 1024 physical blocks ("zone"), the LBA numbers
535 * go back to zero, but are within a higher block of LBA's.
536 * Also, there is a maximum of 1000 LBA's per zone.
537 * In other words, in PBA 1024-2047 you will find LBA 0-999
538 * which are really LBA 1000-1999. This allows for 24 bad
539 * or special physical blocks per zone.
540 */
541
542 if (lba_offset >= uzonesize) {
Frank Seidel6f8aa652009-02-05 16:16:24 +0100543 printk(KERN_WARNING
544 "alauda_read_map: Bad low LBA %d for block %d\n",
Matthew Dharme80b0fa2005-12-04 22:02:44 -0800545 lba_real, blocknum);
546 continue;
547 }
548
549 if (lba_to_pba[lba_offset] != UNDEF) {
Frank Seidel6f8aa652009-02-05 16:16:24 +0100550 printk(KERN_WARNING
551 "alauda_read_map: "
552 "LBA %d seen for PBA %d and %d\n",
Matthew Dharme80b0fa2005-12-04 22:02:44 -0800553 lba_real, lba_to_pba[lba_offset], blocknum);
554 continue;
555 }
556
557 pba_to_lba[i] = lba_real;
558 lba_to_pba[lba_offset] = blocknum;
559 continue;
560 }
561
562 MEDIA_INFO(us).lba_to_pba[zone] = lba_to_pba;
563 MEDIA_INFO(us).pba_to_lba[zone] = pba_to_lba;
564 result = 0;
565 goto out;
566
567error:
568 kfree(lba_to_pba);
569 kfree(pba_to_lba);
570out:
571 return result;
572}
573
574/*
575 * Checks to see whether we have already mapped a certain zone
576 * If we haven't, the map is generated
577 */
578static void alauda_ensure_map_for_zone(struct us_data *us, unsigned int zone)
579{
580 if (MEDIA_INFO(us).lba_to_pba[zone] == NULL
581 || MEDIA_INFO(us).pba_to_lba[zone] == NULL)
582 alauda_read_map(us, zone);
583}
584
585/*
586 * Erases an entire block
587 */
588static int alauda_erase_block(struct us_data *us, u16 pba)
589{
590 int rc;
591 unsigned char command[] = {
592 ALAUDA_BULK_CMD, ALAUDA_BULK_ERASE_BLOCK, PBA_HI(pba),
593 PBA_ZONE(pba), 0, PBA_LO(pba), 0x02, 0, MEDIA_PORT(us)
594 };
595 unsigned char buf[2];
596
597 US_DEBUGP("alauda_erase_block: Erasing PBA %d\n", pba);
598
599 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
600 command, 9, NULL);
601 if (rc != USB_STOR_XFER_GOOD)
602 return rc;
603
604 rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
605 buf, 2, NULL);
606 if (rc != USB_STOR_XFER_GOOD)
607 return rc;
608
609 US_DEBUGP("alauda_erase_block: Erase result: %02X %02X\n",
610 buf[0], buf[1]);
611 return rc;
612}
613
614/*
615 * Reads data from a certain offset page inside a PBA, including interleaved
616 * redundancy data. Returns (pagesize+64)*pages bytes in data.
617 */
618static int alauda_read_block_raw(struct us_data *us, u16 pba,
619 unsigned int page, unsigned int pages, unsigned char *data)
620{
621 int rc;
622 unsigned char command[] = {
623 ALAUDA_BULK_CMD, ALAUDA_BULK_READ_BLOCK, PBA_HI(pba),
624 PBA_ZONE(pba), 0, PBA_LO(pba) + page, pages, 0, MEDIA_PORT(us)
625 };
626
627 US_DEBUGP("alauda_read_block: pba %d page %d count %d\n",
628 pba, page, pages);
629
630 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
631 command, 9, NULL);
632 if (rc != USB_STOR_XFER_GOOD)
633 return rc;
634
635 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
636 data, (MEDIA_INFO(us).pagesize + 64) * pages, NULL);
637}
638
639/*
640 * Reads data from a certain offset page inside a PBA, excluding redundancy
641 * data. Returns pagesize*pages bytes in data. Note that data must be big enough
642 * to hold (pagesize+64)*pages bytes of data, but you can ignore those 'extra'
643 * trailing bytes outside this function.
644 */
645static int alauda_read_block(struct us_data *us, u16 pba,
646 unsigned int page, unsigned int pages, unsigned char *data)
647{
648 int i, rc;
649 unsigned int pagesize = MEDIA_INFO(us).pagesize;
650
651 rc = alauda_read_block_raw(us, pba, page, pages, data);
652 if (rc != USB_STOR_XFER_GOOD)
653 return rc;
654
655 /* Cut out the redundancy data */
656 for (i = 0; i < pages; i++) {
657 int dest_offset = i * pagesize;
658 int src_offset = i * (pagesize + 64);
659 memmove(data + dest_offset, data + src_offset, pagesize);
660 }
661
662 return rc;
663}
664
665/*
666 * Writes an entire block of data and checks status after write.
667 * Redundancy data must be already included in data. Data should be
668 * (pagesize+64)*blocksize bytes in length.
669 */
670static int alauda_write_block(struct us_data *us, u16 pba, unsigned char *data)
671{
672 int rc;
673 struct alauda_info *info = (struct alauda_info *) us->extra;
674 unsigned char command[] = {
675 ALAUDA_BULK_CMD, ALAUDA_BULK_WRITE_BLOCK, PBA_HI(pba),
676 PBA_ZONE(pba), 0, PBA_LO(pba), 32, 0, MEDIA_PORT(us)
677 };
678
679 US_DEBUGP("alauda_write_block: pba %d\n", pba);
680
681 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
682 command, 9, NULL);
683 if (rc != USB_STOR_XFER_GOOD)
684 return rc;
685
686 rc = usb_stor_bulk_transfer_buf(us, info->wr_ep, data,
687 (MEDIA_INFO(us).pagesize + 64) * MEDIA_INFO(us).blocksize,
688 NULL);
689 if (rc != USB_STOR_XFER_GOOD)
690 return rc;
691
692 return alauda_check_status2(us);
693}
694
695/*
696 * Write some data to a specific LBA.
697 */
698static int alauda_write_lba(struct us_data *us, u16 lba,
699 unsigned int page, unsigned int pages,
700 unsigned char *ptr, unsigned char *blockbuffer)
701{
702 u16 pba, lbap, new_pba;
703 unsigned char *bptr, *cptr, *xptr;
704 unsigned char ecc[3];
705 int i, result;
706 unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
707 unsigned int zonesize = MEDIA_INFO(us).zonesize;
708 unsigned int pagesize = MEDIA_INFO(us).pagesize;
709 unsigned int blocksize = MEDIA_INFO(us).blocksize;
710 unsigned int lba_offset = lba % uzonesize;
711 unsigned int new_pba_offset;
712 unsigned int zone = lba / uzonesize;
713
714 alauda_ensure_map_for_zone(us, zone);
715
716 pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
717 if (pba == 1) {
718 /* Maybe it is impossible to write to PBA 1.
719 Fake success, but don't do anything. */
Frank Seidel6f8aa652009-02-05 16:16:24 +0100720 printk(KERN_WARNING
721 "alauda_write_lba: avoid writing to pba 1\n");
Matthew Dharme80b0fa2005-12-04 22:02:44 -0800722 return USB_STOR_TRANSPORT_GOOD;
723 }
724
725 new_pba = alauda_find_unused_pba(&MEDIA_INFO(us), zone);
726 if (!new_pba) {
Frank Seidel6f8aa652009-02-05 16:16:24 +0100727 printk(KERN_WARNING
728 "alauda_write_lba: Out of unused blocks\n");
Matthew Dharme80b0fa2005-12-04 22:02:44 -0800729 return USB_STOR_TRANSPORT_ERROR;
730 }
731
732 /* read old contents */
733 if (pba != UNDEF) {
734 result = alauda_read_block_raw(us, pba, 0,
735 blocksize, blockbuffer);
736 if (result != USB_STOR_XFER_GOOD)
737 return result;
738 } else {
739 memset(blockbuffer, 0, blocksize * (pagesize + 64));
740 }
741
742 lbap = (lba_offset << 1) | 0x1000;
743 if (parity[MSB_of(lbap) ^ LSB_of(lbap)])
744 lbap ^= 1;
745
746 /* check old contents and fill lba */
747 for (i = 0; i < blocksize; i++) {
748 bptr = blockbuffer + (i * (pagesize + 64));
749 cptr = bptr + pagesize;
750 nand_compute_ecc(bptr, ecc);
751 if (!nand_compare_ecc(cptr+13, ecc)) {
752 US_DEBUGP("Warning: bad ecc in page %d- of pba %d\n",
753 i, pba);
754 nand_store_ecc(cptr+13, ecc);
755 }
756 nand_compute_ecc(bptr + (pagesize / 2), ecc);
757 if (!nand_compare_ecc(cptr+8, ecc)) {
758 US_DEBUGP("Warning: bad ecc in page %d+ of pba %d\n",
759 i, pba);
760 nand_store_ecc(cptr+8, ecc);
761 }
762 cptr[6] = cptr[11] = MSB_of(lbap);
763 cptr[7] = cptr[12] = LSB_of(lbap);
764 }
765
766 /* copy in new stuff and compute ECC */
767 xptr = ptr;
768 for (i = page; i < page+pages; i++) {
769 bptr = blockbuffer + (i * (pagesize + 64));
770 cptr = bptr + pagesize;
771 memcpy(bptr, xptr, pagesize);
772 xptr += pagesize;
773 nand_compute_ecc(bptr, ecc);
774 nand_store_ecc(cptr+13, ecc);
775 nand_compute_ecc(bptr + (pagesize / 2), ecc);
776 nand_store_ecc(cptr+8, ecc);
777 }
778
779 result = alauda_write_block(us, new_pba, blockbuffer);
780 if (result != USB_STOR_XFER_GOOD)
781 return result;
782
783 new_pba_offset = new_pba - (zone * zonesize);
784 MEDIA_INFO(us).pba_to_lba[zone][new_pba_offset] = lba;
785 MEDIA_INFO(us).lba_to_pba[zone][lba_offset] = new_pba;
786 US_DEBUGP("alauda_write_lba: Remapped LBA %d to PBA %d\n",
787 lba, new_pba);
788
789 if (pba != UNDEF) {
790 unsigned int pba_offset = pba - (zone * zonesize);
791 result = alauda_erase_block(us, pba);
792 if (result != USB_STOR_XFER_GOOD)
793 return result;
794 MEDIA_INFO(us).pba_to_lba[zone][pba_offset] = UNDEF;
795 }
796
797 return USB_STOR_TRANSPORT_GOOD;
798}
799
800/*
801 * Read data from a specific sector address
802 */
803static int alauda_read_data(struct us_data *us, unsigned long address,
804 unsigned int sectors)
805{
806 unsigned char *buffer;
807 u16 lba, max_lba;
Jens Axboe1f6f31a2007-05-11 12:33:09 +0200808 unsigned int page, len, offset;
Matthew Dharme80b0fa2005-12-04 22:02:44 -0800809 unsigned int blockshift = MEDIA_INFO(us).blockshift;
810 unsigned int pageshift = MEDIA_INFO(us).pageshift;
811 unsigned int blocksize = MEDIA_INFO(us).blocksize;
812 unsigned int pagesize = MEDIA_INFO(us).pagesize;
813 unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
Jens Axboe1f6f31a2007-05-11 12:33:09 +0200814 struct scatterlist *sg;
Matthew Dharme80b0fa2005-12-04 22:02:44 -0800815 int result;
816
817 /*
818 * Since we only read in one block at a time, we have to create
819 * a bounce buffer and move the data a piece at a time between the
820 * bounce buffer and the actual transfer buffer.
821 * We make this buffer big enough to hold temporary redundancy data,
822 * which we use when reading the data blocks.
823 */
824
825 len = min(sectors, blocksize) * (pagesize + 64);
826 buffer = kmalloc(len, GFP_NOIO);
827 if (buffer == NULL) {
Frank Seidel6f8aa652009-02-05 16:16:24 +0100828 printk(KERN_WARNING "alauda_read_data: Out of memory\n");
Matthew Dharme80b0fa2005-12-04 22:02:44 -0800829 return USB_STOR_TRANSPORT_ERROR;
830 }
831
832 /* Figure out the initial LBA and page */
833 lba = address >> blockshift;
834 page = (address & MEDIA_INFO(us).blockmask);
835 max_lba = MEDIA_INFO(us).capacity >> (blockshift + pageshift);
836
837 result = USB_STOR_TRANSPORT_GOOD;
Jens Axboe1f6f31a2007-05-11 12:33:09 +0200838 offset = 0;
839 sg = NULL;
Matthew Dharme80b0fa2005-12-04 22:02:44 -0800840
841 while (sectors > 0) {
842 unsigned int zone = lba / uzonesize; /* integer division */
843 unsigned int lba_offset = lba - (zone * uzonesize);
844 unsigned int pages;
845 u16 pba;
846 alauda_ensure_map_for_zone(us, zone);
847
848 /* Not overflowing capacity? */
849 if (lba >= max_lba) {
850 US_DEBUGP("Error: Requested lba %u exceeds "
851 "maximum %u\n", lba, max_lba);
852 result = USB_STOR_TRANSPORT_ERROR;
853 break;
854 }
855
856 /* Find number of pages we can read in this block */
857 pages = min(sectors, blocksize - page);
858 len = pages << pageshift;
859
860 /* Find where this lba lives on disk */
861 pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
862
863 if (pba == UNDEF) { /* this lba was never written */
864 US_DEBUGP("Read %d zero pages (LBA %d) page %d\n",
865 pages, lba, page);
866
867 /* This is not really an error. It just means
868 that the block has never been written.
869 Instead of returning USB_STOR_TRANSPORT_ERROR
870 it is better to return all zero data. */
871
872 memset(buffer, 0, len);
873 } else {
874 US_DEBUGP("Read %d pages, from PBA %d"
875 " (LBA %d) page %d\n",
876 pages, pba, lba, page);
877
878 result = alauda_read_block(us, pba, page, pages, buffer);
879 if (result != USB_STOR_TRANSPORT_GOOD)
880 break;
881 }
882
883 /* Store the data in the transfer buffer */
884 usb_stor_access_xfer_buf(buffer, len, us->srb,
Jens Axboe1f6f31a2007-05-11 12:33:09 +0200885 &sg, &offset, TO_XFER_BUF);
Matthew Dharme80b0fa2005-12-04 22:02:44 -0800886
887 page = 0;
888 lba++;
889 sectors -= pages;
890 }
891
892 kfree(buffer);
893 return result;
894}
895
896/*
897 * Write data to a specific sector address
898 */
899static int alauda_write_data(struct us_data *us, unsigned long address,
900 unsigned int sectors)
901{
902 unsigned char *buffer, *blockbuffer;
Jens Axboe1f6f31a2007-05-11 12:33:09 +0200903 unsigned int page, len, offset;
Matthew Dharme80b0fa2005-12-04 22:02:44 -0800904 unsigned int blockshift = MEDIA_INFO(us).blockshift;
905 unsigned int pageshift = MEDIA_INFO(us).pageshift;
906 unsigned int blocksize = MEDIA_INFO(us).blocksize;
907 unsigned int pagesize = MEDIA_INFO(us).pagesize;
Jens Axboe1f6f31a2007-05-11 12:33:09 +0200908 struct scatterlist *sg;
Matthew Dharme80b0fa2005-12-04 22:02:44 -0800909 u16 lba, max_lba;
910 int result;
911
912 /*
913 * Since we don't write the user data directly to the device,
914 * we have to create a bounce buffer and move the data a piece
915 * at a time between the bounce buffer and the actual transfer buffer.
916 */
917
918 len = min(sectors, blocksize) * pagesize;
919 buffer = kmalloc(len, GFP_NOIO);
920 if (buffer == NULL) {
Frank Seidel6f8aa652009-02-05 16:16:24 +0100921 printk(KERN_WARNING "alauda_write_data: Out of memory\n");
Matthew Dharme80b0fa2005-12-04 22:02:44 -0800922 return USB_STOR_TRANSPORT_ERROR;
923 }
924
925 /*
926 * We also need a temporary block buffer, where we read in the old data,
927 * overwrite parts with the new data, and manipulate the redundancy data
928 */
929 blockbuffer = kmalloc((pagesize + 64) * blocksize, GFP_NOIO);
930 if (blockbuffer == NULL) {
Frank Seidel6f8aa652009-02-05 16:16:24 +0100931 printk(KERN_WARNING "alauda_write_data: Out of memory\n");
Matthew Dharme80b0fa2005-12-04 22:02:44 -0800932 kfree(buffer);
933 return USB_STOR_TRANSPORT_ERROR;
934 }
935
936 /* Figure out the initial LBA and page */
937 lba = address >> blockshift;
938 page = (address & MEDIA_INFO(us).blockmask);
939 max_lba = MEDIA_INFO(us).capacity >> (pageshift + blockshift);
940
941 result = USB_STOR_TRANSPORT_GOOD;
Jens Axboe1f6f31a2007-05-11 12:33:09 +0200942 offset = 0;
943 sg = NULL;
Matthew Dharme80b0fa2005-12-04 22:02:44 -0800944
945 while (sectors > 0) {
946 /* Write as many sectors as possible in this block */
947 unsigned int pages = min(sectors, blocksize - page);
948 len = pages << pageshift;
949
950 /* Not overflowing capacity? */
951 if (lba >= max_lba) {
952 US_DEBUGP("alauda_write_data: Requested lba %u exceeds "
953 "maximum %u\n", lba, max_lba);
954 result = USB_STOR_TRANSPORT_ERROR;
955 break;
956 }
957
958 /* Get the data from the transfer buffer */
959 usb_stor_access_xfer_buf(buffer, len, us->srb,
Jens Axboe1f6f31a2007-05-11 12:33:09 +0200960 &sg, &offset, FROM_XFER_BUF);
Matthew Dharme80b0fa2005-12-04 22:02:44 -0800961
962 result = alauda_write_lba(us, lba, page, pages, buffer,
963 blockbuffer);
964 if (result != USB_STOR_TRANSPORT_GOOD)
965 break;
966
967 page = 0;
968 lba++;
969 sectors -= pages;
970 }
971
972 kfree(buffer);
973 kfree(blockbuffer);
974 return result;
975}
976
977/*
978 * Our interface with the rest of the world
979 */
980
981static void alauda_info_destructor(void *extra)
982{
983 struct alauda_info *info = (struct alauda_info *) extra;
984 int port;
985
986 if (!info)
987 return;
988
989 for (port = 0; port < 2; port++) {
990 struct alauda_media_info *media_info = &info->port[port];
991
992 alauda_free_maps(media_info);
993 kfree(media_info->lba_to_pba);
994 kfree(media_info->pba_to_lba);
995 }
996}
997
998/*
999 * Initialize alauda_info struct and find the data-write endpoint
1000 */
1001int init_alauda(struct us_data *us)
1002{
1003 struct alauda_info *info;
1004 struct usb_host_interface *altsetting = us->pusb_intf->cur_altsetting;
1005 nand_init_ecc();
1006
1007 us->extra = kzalloc(sizeof(struct alauda_info), GFP_NOIO);
1008 if (!us->extra) {
1009 US_DEBUGP("init_alauda: Gah! Can't allocate storage for"
1010 "alauda info struct!\n");
1011 return USB_STOR_TRANSPORT_ERROR;
1012 }
1013 info = (struct alauda_info *) us->extra;
1014 us->extra_destructor = alauda_info_destructor;
1015
1016 info->wr_ep = usb_sndbulkpipe(us->pusb_dev,
1017 altsetting->endpoint[0].desc.bEndpointAddress
1018 & USB_ENDPOINT_NUMBER_MASK);
1019
1020 return USB_STOR_TRANSPORT_GOOD;
1021}
1022
1023int alauda_transport(struct scsi_cmnd *srb, struct us_data *us)
1024{
1025 int rc;
1026 struct alauda_info *info = (struct alauda_info *) us->extra;
1027 unsigned char *ptr = us->iobuf;
1028 static unsigned char inquiry_response[36] = {
1029 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1030 };
1031
1032 if (srb->cmnd[0] == INQUIRY) {
1033 US_DEBUGP("alauda_transport: INQUIRY. "
1034 "Returning bogus response.\n");
1035 memcpy(ptr, inquiry_response, sizeof(inquiry_response));
1036 fill_inquiry_response(us, ptr, 36);
1037 return USB_STOR_TRANSPORT_GOOD;
1038 }
1039
1040 if (srb->cmnd[0] == TEST_UNIT_READY) {
1041 US_DEBUGP("alauda_transport: TEST_UNIT_READY.\n");
1042 return alauda_check_media(us);
1043 }
1044
1045 if (srb->cmnd[0] == READ_CAPACITY) {
1046 unsigned int num_zones;
1047 unsigned long capacity;
1048
1049 rc = alauda_check_media(us);
1050 if (rc != USB_STOR_TRANSPORT_GOOD)
1051 return rc;
1052
1053 num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift
1054 + MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift);
1055
1056 capacity = num_zones * MEDIA_INFO(us).uzonesize
1057 * MEDIA_INFO(us).blocksize;
1058
1059 /* Report capacity and page size */
1060 ((__be32 *) ptr)[0] = cpu_to_be32(capacity - 1);
1061 ((__be32 *) ptr)[1] = cpu_to_be32(512);
1062
1063 usb_stor_set_xfer_buf(ptr, 8, srb);
1064 return USB_STOR_TRANSPORT_GOOD;
1065 }
1066
1067 if (srb->cmnd[0] == READ_10) {
1068 unsigned int page, pages;
1069
1070 rc = alauda_check_media(us);
1071 if (rc != USB_STOR_TRANSPORT_GOOD)
1072 return rc;
1073
1074 page = short_pack(srb->cmnd[3], srb->cmnd[2]);
1075 page <<= 16;
1076 page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
1077 pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
1078
1079 US_DEBUGP("alauda_transport: READ_10: page %d pagect %d\n",
1080 page, pages);
1081
1082 return alauda_read_data(us, page, pages);
1083 }
1084
1085 if (srb->cmnd[0] == WRITE_10) {
1086 unsigned int page, pages;
1087
1088 rc = alauda_check_media(us);
1089 if (rc != USB_STOR_TRANSPORT_GOOD)
1090 return rc;
1091
1092 page = short_pack(srb->cmnd[3], srb->cmnd[2]);
1093 page <<= 16;
1094 page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
1095 pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
1096
1097 US_DEBUGP("alauda_transport: WRITE_10: page %d pagect %d\n",
1098 page, pages);
1099
1100 return alauda_write_data(us, page, pages);
1101 }
1102
1103 if (srb->cmnd[0] == REQUEST_SENSE) {
1104 US_DEBUGP("alauda_transport: REQUEST_SENSE.\n");
1105
1106 memset(ptr, 0, 18);
1107 ptr[0] = 0xF0;
1108 ptr[2] = info->sense_key;
1109 ptr[7] = 11;
1110 ptr[12] = info->sense_asc;
1111 ptr[13] = info->sense_ascq;
1112 usb_stor_set_xfer_buf(ptr, 18, srb);
1113
1114 return USB_STOR_TRANSPORT_GOOD;
1115 }
1116
1117 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
1118 /* sure. whatever. not like we can stop the user from popping
1119 the media out of the device (no locking doors, etc) */
1120 return USB_STOR_TRANSPORT_GOOD;
1121 }
1122
1123 US_DEBUGP("alauda_transport: Gah! Unknown command: %d (0x%x)\n",
1124 srb->cmnd[0], srb->cmnd[0]);
1125 info->sense_key = 0x05;
1126 info->sense_asc = 0x20;
1127 info->sense_ascq = 0x00;
1128 return USB_STOR_TRANSPORT_FAILED;
1129}
1130