1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34#include <linux/module.h>
35#include <linux/slab.h>
36
37#include <scsi/scsi.h>
38#include <scsi/scsi_cmnd.h>
39#include <scsi/scsi_device.h>
40
41#include "usb.h"
42#include "transport.h"
43#include "protocol.h"
44#include "debug.h"
45
46MODULE_DESCRIPTION("Driver for Alauda-based card readers");
47MODULE_AUTHOR("Daniel Drake <dsd@gentoo.org>");
48MODULE_LICENSE("GPL");
49
50
51
52
53#define ALAUDA_STATUS_ERROR 0x01
54#define ALAUDA_STATUS_READY 0x40
55
56
57
58
59#define ALAUDA_GET_XD_MEDIA_STATUS 0x08
60#define ALAUDA_GET_SM_MEDIA_STATUS 0x98
61#define ALAUDA_ACK_XD_MEDIA_CHANGE 0x0a
62#define ALAUDA_ACK_SM_MEDIA_CHANGE 0x9a
63#define ALAUDA_GET_XD_MEDIA_SIG 0x86
64#define ALAUDA_GET_SM_MEDIA_SIG 0x96
65
66
67
68
69#define ALAUDA_BULK_CMD 0x40
70
71
72
73
74#define ALAUDA_BULK_GET_REDU_DATA 0x85
75#define ALAUDA_BULK_READ_BLOCK 0x94
76#define ALAUDA_BULK_ERASE_BLOCK 0xa3
77#define ALAUDA_BULK_WRITE_BLOCK 0xb4
78#define ALAUDA_BULK_GET_STATUS2 0xb7
79#define ALAUDA_BULK_RESET_MEDIA 0xe0
80
81
82
83
84#define ALAUDA_PORT_XD 0x00
85#define ALAUDA_PORT_SM 0x01
86
87
88
89
90#define UNDEF 0xffff
91#define SPARE 0xfffe
92#define UNUSABLE 0xfffd
93
94struct alauda_media_info {
95 unsigned long capacity;
96 unsigned int pagesize;
97 unsigned int blocksize;
98 unsigned int uzonesize;
99 unsigned int zonesize;
100 unsigned int blockmask;
101
102 unsigned char pageshift;
103 unsigned char blockshift;
104 unsigned char zoneshift;
105
106 u16 **lba_to_pba;
107 u16 **pba_to_lba;
108};
109
110struct alauda_info {
111 struct alauda_media_info port[2];
112 int wr_ep;
113
114 unsigned char sense_key;
115 unsigned long sense_asc;
116 unsigned long sense_ascq;
117};
118
119#define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) )
120#define LSB_of(s) ((s)&0xFF)
121#define MSB_of(s) ((s)>>8)
122
123#define MEDIA_PORT(us) us->srb->device->lun
124#define MEDIA_INFO(us) ((struct alauda_info *)us->extra)->port[MEDIA_PORT(us)]
125
126#define PBA_LO(pba) ((pba & 0xF) << 5)
127#define PBA_HI(pba) (pba >> 3)
128#define PBA_ZONE(pba) (pba >> 11)
129
130static int init_alauda(struct us_data *us);
131
132
133
134
135
136#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
137 vendorName, productName, useProtocol, useTransport, \
138 initFunction, flags) \
139{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
140 .driver_info = (flags) }
141
142static struct usb_device_id alauda_usb_ids[] = {
143# include "unusual_alauda.h"
144 { }
145};
146MODULE_DEVICE_TABLE(usb, alauda_usb_ids);
147
148#undef UNUSUAL_DEV
149
150
151
152
153#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
154 vendor_name, product_name, use_protocol, use_transport, \
155 init_function, Flags) \
156{ \
157 .vendorName = vendor_name, \
158 .productName = product_name, \
159 .useProtocol = use_protocol, \
160 .useTransport = use_transport, \
161 .initFunction = init_function, \
162}
163
164static struct us_unusual_dev alauda_unusual_dev_list[] = {
165# include "unusual_alauda.h"
166 { }
167};
168
169#undef UNUSUAL_DEV
170
171
172
173
174
175
176struct alauda_card_info {
177 unsigned char id;
178 unsigned char chipshift;
179 unsigned char pageshift;
180 unsigned char blockshift;
181 unsigned char zoneshift;
182};
183
184static struct alauda_card_info alauda_card_ids[] = {
185
186 { 0x6e, 20, 8, 4, 8},
187 { 0xe8, 20, 8, 4, 8},
188 { 0xec, 20, 8, 4, 8},
189 { 0x64, 21, 8, 4, 9},
190 { 0xea, 21, 8, 4, 9},
191 { 0x6b, 22, 9, 4, 9},
192 { 0xe3, 22, 9, 4, 9},
193 { 0xe5, 22, 9, 4, 9},
194 { 0xe6, 23, 9, 4, 10},
195 { 0x73, 24, 9, 5, 10},
196 { 0x75, 25, 9, 5, 10},
197 { 0x76, 26, 9, 5, 10},
198 { 0x79, 27, 9, 5, 10},
199 { 0x71, 28, 9, 5, 10},
200
201
202 { 0x5d, 21, 9, 4, 8},
203 { 0xd5, 22, 9, 4, 9},
204 { 0xd6, 23, 9, 4, 10},
205 { 0x57, 24, 9, 4, 11},
206 { 0x58, 25, 9, 4, 12},
207 { 0,}
208};
209
210static struct alauda_card_info *alauda_card_find_id(unsigned char id) {
211 int i;
212
213 for (i = 0; alauda_card_ids[i].id != 0; i++)
214 if (alauda_card_ids[i].id == id)
215 return &(alauda_card_ids[i]);
216 return NULL;
217}
218
219
220
221
222
223static unsigned char parity[256];
224static unsigned char ecc2[256];
225
226static void nand_init_ecc(void) {
227 int i, j, a;
228
229 parity[0] = 0;
230 for (i = 1; i < 256; i++)
231 parity[i] = (parity[i&(i-1)] ^ 1);
232
233 for (i = 0; i < 256; i++) {
234 a = 0;
235 for (j = 0; j < 8; j++) {
236 if (i & (1<<j)) {
237 if ((j & 1) == 0)
238 a ^= 0x04;
239 if ((j & 2) == 0)
240 a ^= 0x10;
241 if ((j & 4) == 0)
242 a ^= 0x40;
243 }
244 }
245 ecc2[i] = ~(a ^ (a<<1) ^ (parity[i] ? 0xa8 : 0));
246 }
247}
248
249
250static void nand_compute_ecc(unsigned char *data, unsigned char *ecc) {
251 int i, j, a;
252 unsigned char par = 0, bit, bits[8] = {0};
253
254
255 for (i = 0; i < 256; i++) {
256 par ^= data[i];
257 bit = parity[data[i]];
258 for (j = 0; j < 8; j++)
259 if ((i & (1<<j)) == 0)
260 bits[j] ^= bit;
261 }
262
263
264 a = (bits[3] << 6) + (bits[2] << 4) + (bits[1] << 2) + bits[0];
265 ecc[0] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
266
267 a = (bits[7] << 6) + (bits[6] << 4) + (bits[5] << 2) + bits[4];
268 ecc[1] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
269
270 ecc[2] = ecc2[par];
271}
272
273static int nand_compare_ecc(unsigned char *data, unsigned char *ecc) {
274 return (data[0] == ecc[0] && data[1] == ecc[1] && data[2] == ecc[2]);
275}
276
277static void nand_store_ecc(unsigned char *data, unsigned char *ecc) {
278 memcpy(data, ecc, 3);
279}
280
281
282
283
284
285
286
287
288static void alauda_free_maps (struct alauda_media_info *media_info)
289{
290 unsigned int shift = media_info->zoneshift
291 + media_info->blockshift + media_info->pageshift;
292 unsigned int num_zones = media_info->capacity >> shift;
293 unsigned int i;
294
295 if (media_info->lba_to_pba != NULL)
296 for (i = 0; i < num_zones; i++) {
297 kfree(media_info->lba_to_pba[i]);
298 media_info->lba_to_pba[i] = NULL;
299 }
300
301 if (media_info->pba_to_lba != NULL)
302 for (i = 0; i < num_zones; i++) {
303 kfree(media_info->pba_to_lba[i]);
304 media_info->pba_to_lba[i] = NULL;
305 }
306}
307
308
309
310
311
312static int alauda_get_media_status(struct us_data *us, unsigned char *data)
313{
314 int rc;
315 unsigned char command;
316
317 if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
318 command = ALAUDA_GET_XD_MEDIA_STATUS;
319 else
320 command = ALAUDA_GET_SM_MEDIA_STATUS;
321
322 rc = usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
323 command, 0xc0, 0, 1, data, 2);
324
325 usb_stor_dbg(us, "Media status %02X %02X\n", data[0], data[1]);
326
327 return rc;
328}
329
330
331
332
333
334static int alauda_ack_media(struct us_data *us)
335{
336 unsigned char command;
337
338 if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
339 command = ALAUDA_ACK_XD_MEDIA_CHANGE;
340 else
341 command = ALAUDA_ACK_SM_MEDIA_CHANGE;
342
343 return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
344 command, 0x40, 0, 1, NULL, 0);
345}
346
347
348
349
350
351static int alauda_get_media_signature(struct us_data *us, unsigned char *data)
352{
353 unsigned char command;
354
355 if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
356 command = ALAUDA_GET_XD_MEDIA_SIG;
357 else
358 command = ALAUDA_GET_SM_MEDIA_SIG;
359
360 return usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
361 command, 0xc0, 0, 0, data, 4);
362}
363
364
365
366
367static int alauda_reset_media(struct us_data *us)
368{
369 unsigned char *command = us->iobuf;
370
371 memset(command, 0, 9);
372 command[0] = ALAUDA_BULK_CMD;
373 command[1] = ALAUDA_BULK_RESET_MEDIA;
374 command[8] = MEDIA_PORT(us);
375
376 return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
377 command, 9, NULL);
378}
379
380
381
382
383static int alauda_init_media(struct us_data *us)
384{
385 unsigned char *data = us->iobuf;
386 int ready = 0;
387 struct alauda_card_info *media_info;
388 unsigned int num_zones;
389
390 while (ready == 0) {
391 msleep(20);
392
393 if (alauda_get_media_status(us, data) != USB_STOR_XFER_GOOD)
394 return USB_STOR_TRANSPORT_ERROR;
395
396 if (data[0] & 0x10)
397 ready = 1;
398 }
399
400 usb_stor_dbg(us, "We are ready for action!\n");
401
402 if (alauda_ack_media(us) != USB_STOR_XFER_GOOD)
403 return USB_STOR_TRANSPORT_ERROR;
404
405 msleep(10);
406
407 if (alauda_get_media_status(us, data) != USB_STOR_XFER_GOOD)
408 return USB_STOR_TRANSPORT_ERROR;
409
410 if (data[0] != 0x14) {
411 usb_stor_dbg(us, "Media not ready after ack\n");
412 return USB_STOR_TRANSPORT_ERROR;
413 }
414
415 if (alauda_get_media_signature(us, data) != USB_STOR_XFER_GOOD)
416 return USB_STOR_TRANSPORT_ERROR;
417
418 usb_stor_dbg(us, "Media signature: %02X %02X %02X %02X\n",
419 data[0], data[1], data[2], data[3]);
420 media_info = alauda_card_find_id(data[1]);
421 if (media_info == NULL) {
422 printk(KERN_WARNING
423 "alauda_init_media: Unrecognised media signature: "
424 "%02X %02X %02X %02X\n",
425 data[0], data[1], data[2], data[3]);
426 return USB_STOR_TRANSPORT_ERROR;
427 }
428
429 MEDIA_INFO(us).capacity = 1 << media_info->chipshift;
430 usb_stor_dbg(us, "Found media with capacity: %ldMB\n",
431 MEDIA_INFO(us).capacity >> 20);
432
433 MEDIA_INFO(us).pageshift = media_info->pageshift;
434 MEDIA_INFO(us).blockshift = media_info->blockshift;
435 MEDIA_INFO(us).zoneshift = media_info->zoneshift;
436
437 MEDIA_INFO(us).pagesize = 1 << media_info->pageshift;
438 MEDIA_INFO(us).blocksize = 1 << media_info->blockshift;
439 MEDIA_INFO(us).zonesize = 1 << media_info->zoneshift;
440
441 MEDIA_INFO(us).uzonesize = ((1 << media_info->zoneshift) / 128) * 125;
442 MEDIA_INFO(us).blockmask = MEDIA_INFO(us).blocksize - 1;
443
444 num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift
445 + MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift);
446 MEDIA_INFO(us).pba_to_lba = kcalloc(num_zones, sizeof(u16*), GFP_NOIO);
447 MEDIA_INFO(us).lba_to_pba = kcalloc(num_zones, sizeof(u16*), GFP_NOIO);
448
449 if (alauda_reset_media(us) != USB_STOR_XFER_GOOD)
450 return USB_STOR_TRANSPORT_ERROR;
451
452 return USB_STOR_TRANSPORT_GOOD;
453}
454
455
456
457
458
459static int alauda_check_media(struct us_data *us)
460{
461 struct alauda_info *info = (struct alauda_info *) us->extra;
462 unsigned char status[2];
463 int rc;
464
465 rc = alauda_get_media_status(us, status);
466
467
468 if ((status[0] & 0x80) || ((status[0] & 0x1F) == 0x10)
469 || ((status[1] & 0x01) == 0)) {
470 usb_stor_dbg(us, "No media, or door open\n");
471 alauda_free_maps(&MEDIA_INFO(us));
472 info->sense_key = 0x02;
473 info->sense_asc = 0x3A;
474 info->sense_ascq = 0x00;
475 return USB_STOR_TRANSPORT_FAILED;
476 }
477
478
479 if (status[0] & 0x08) {
480 usb_stor_dbg(us, "Media change detected\n");
481 alauda_free_maps(&MEDIA_INFO(us));
482 alauda_init_media(us);
483
484 info->sense_key = UNIT_ATTENTION;
485 info->sense_asc = 0x28;
486 info->sense_ascq = 0x00;
487 return USB_STOR_TRANSPORT_FAILED;
488 }
489
490 return USB_STOR_TRANSPORT_GOOD;
491}
492
493
494
495
496
497static int alauda_check_status2(struct us_data *us)
498{
499 int rc;
500 unsigned char command[] = {
501 ALAUDA_BULK_CMD, ALAUDA_BULK_GET_STATUS2,
502 0, 0, 0, 0, 3, 0, MEDIA_PORT(us)
503 };
504 unsigned char data[3];
505
506 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
507 command, 9, NULL);
508 if (rc != USB_STOR_XFER_GOOD)
509 return rc;
510
511 rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
512 data, 3, NULL);
513 if (rc != USB_STOR_XFER_GOOD)
514 return rc;
515
516 usb_stor_dbg(us, "%02X %02X %02X\n", data[0], data[1], data[2]);
517 if (data[0] & ALAUDA_STATUS_ERROR)
518 return USB_STOR_XFER_ERROR;
519
520 return USB_STOR_XFER_GOOD;
521}
522
523
524
525
526
527static int alauda_get_redu_data(struct us_data *us, u16 pba, unsigned char *data)
528{
529 int rc;
530 unsigned char command[] = {
531 ALAUDA_BULK_CMD, ALAUDA_BULK_GET_REDU_DATA,
532 PBA_HI(pba), PBA_ZONE(pba), 0, PBA_LO(pba), 0, 0, MEDIA_PORT(us)
533 };
534
535 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
536 command, 9, NULL);
537 if (rc != USB_STOR_XFER_GOOD)
538 return rc;
539
540 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
541 data, 16, NULL);
542}
543
544
545
546
547
548static u16 alauda_find_unused_pba(struct alauda_media_info *info,
549 unsigned int zone)
550{
551 u16 *pba_to_lba = info->pba_to_lba[zone];
552 unsigned int i;
553
554 for (i = 0; i < info->zonesize; i++)
555 if (pba_to_lba[i] == UNDEF)
556 return (zone << info->zoneshift) + i;
557
558 return 0;
559}
560
561
562
563
564
565static int alauda_read_map(struct us_data *us, unsigned int zone)
566{
567 unsigned char *data = us->iobuf;
568 int result;
569 int i, j;
570 unsigned int zonesize = MEDIA_INFO(us).zonesize;
571 unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
572 unsigned int lba_offset, lba_real, blocknum;
573 unsigned int zone_base_lba = zone * uzonesize;
574 unsigned int zone_base_pba = zone * zonesize;
575 u16 *lba_to_pba = kcalloc(zonesize, sizeof(u16), GFP_NOIO);
576 u16 *pba_to_lba = kcalloc(zonesize, sizeof(u16), GFP_NOIO);
577 if (lba_to_pba == NULL || pba_to_lba == NULL) {
578 result = USB_STOR_TRANSPORT_ERROR;
579 goto error;
580 }
581
582 usb_stor_dbg(us, "Mapping blocks for zone %d\n", zone);
583
584
585 for (i = 0; i < zonesize; i++)
586 lba_to_pba[i] = pba_to_lba[i] = UNDEF;
587
588 for (i = 0; i < zonesize; i++) {
589 blocknum = zone_base_pba + i;
590
591 result = alauda_get_redu_data(us, blocknum, data);
592 if (result != USB_STOR_XFER_GOOD) {
593 result = USB_STOR_TRANSPORT_ERROR;
594 goto error;
595 }
596
597
598 for (j = 0; j < 16; j++)
599 if (data[j] != 0)
600 goto nonz;
601 pba_to_lba[i] = UNUSABLE;
602 usb_stor_dbg(us, "PBA %d has no logical mapping\n", blocknum);
603 continue;
604
605 nonz:
606
607 for (j = 0; j < 16; j++)
608 if (data[j] != 0xff)
609 goto nonff;
610 continue;
611
612 nonff:
613
614 if (j < 6) {
615 usb_stor_dbg(us, "PBA %d has no logical mapping: reserved area = %02X%02X%02X%02X data status %02X block status %02X\n",
616 blocknum,
617 data[0], data[1], data[2], data[3],
618 data[4], data[5]);
619 pba_to_lba[i] = UNUSABLE;
620 continue;
621 }
622
623 if ((data[6] >> 4) != 0x01) {
624 usb_stor_dbg(us, "PBA %d has invalid address field %02X%02X/%02X%02X\n",
625 blocknum, data[6], data[7],
626 data[11], data[12]);
627 pba_to_lba[i] = UNUSABLE;
628 continue;
629 }
630
631
632 if (parity[data[6] ^ data[7]]) {
633 printk(KERN_WARNING
634 "alauda_read_map: Bad parity in LBA for block %d"
635 " (%02X %02X)\n", i, data[6], data[7]);
636 pba_to_lba[i] = UNUSABLE;
637 continue;
638 }
639
640 lba_offset = short_pack(data[7], data[6]);
641 lba_offset = (lba_offset & 0x07FF) >> 1;
642 lba_real = lba_offset + zone_base_lba;
643
644
645
646
647
648
649
650
651
652
653 if (lba_offset >= uzonesize) {
654 printk(KERN_WARNING
655 "alauda_read_map: Bad low LBA %d for block %d\n",
656 lba_real, blocknum);
657 continue;
658 }
659
660 if (lba_to_pba[lba_offset] != UNDEF) {
661 printk(KERN_WARNING
662 "alauda_read_map: "
663 "LBA %d seen for PBA %d and %d\n",
664 lba_real, lba_to_pba[lba_offset], blocknum);
665 continue;
666 }
667
668 pba_to_lba[i] = lba_real;
669 lba_to_pba[lba_offset] = blocknum;
670 continue;
671 }
672
673 MEDIA_INFO(us).lba_to_pba[zone] = lba_to_pba;
674 MEDIA_INFO(us).pba_to_lba[zone] = pba_to_lba;
675 result = 0;
676 goto out;
677
678error:
679 kfree(lba_to_pba);
680 kfree(pba_to_lba);
681out:
682 return result;
683}
684
685
686
687
688
689static void alauda_ensure_map_for_zone(struct us_data *us, unsigned int zone)
690{
691 if (MEDIA_INFO(us).lba_to_pba[zone] == NULL
692 || MEDIA_INFO(us).pba_to_lba[zone] == NULL)
693 alauda_read_map(us, zone);
694}
695
696
697
698
699static int alauda_erase_block(struct us_data *us, u16 pba)
700{
701 int rc;
702 unsigned char command[] = {
703 ALAUDA_BULK_CMD, ALAUDA_BULK_ERASE_BLOCK, PBA_HI(pba),
704 PBA_ZONE(pba), 0, PBA_LO(pba), 0x02, 0, MEDIA_PORT(us)
705 };
706 unsigned char buf[2];
707
708 usb_stor_dbg(us, "Erasing PBA %d\n", pba);
709
710 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
711 command, 9, NULL);
712 if (rc != USB_STOR_XFER_GOOD)
713 return rc;
714
715 rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
716 buf, 2, NULL);
717 if (rc != USB_STOR_XFER_GOOD)
718 return rc;
719
720 usb_stor_dbg(us, "Erase result: %02X %02X\n", buf[0], buf[1]);
721 return rc;
722}
723
724
725
726
727
728static int alauda_read_block_raw(struct us_data *us, u16 pba,
729 unsigned int page, unsigned int pages, unsigned char *data)
730{
731 int rc;
732 unsigned char command[] = {
733 ALAUDA_BULK_CMD, ALAUDA_BULK_READ_BLOCK, PBA_HI(pba),
734 PBA_ZONE(pba), 0, PBA_LO(pba) + page, pages, 0, MEDIA_PORT(us)
735 };
736
737 usb_stor_dbg(us, "pba %d page %d count %d\n", pba, page, pages);
738
739 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
740 command, 9, NULL);
741 if (rc != USB_STOR_XFER_GOOD)
742 return rc;
743
744 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
745 data, (MEDIA_INFO(us).pagesize + 64) * pages, NULL);
746}
747
748
749
750
751
752
753
754static int alauda_read_block(struct us_data *us, u16 pba,
755 unsigned int page, unsigned int pages, unsigned char *data)
756{
757 int i, rc;
758 unsigned int pagesize = MEDIA_INFO(us).pagesize;
759
760 rc = alauda_read_block_raw(us, pba, page, pages, data);
761 if (rc != USB_STOR_XFER_GOOD)
762 return rc;
763
764
765 for (i = 0; i < pages; i++) {
766 int dest_offset = i * pagesize;
767 int src_offset = i * (pagesize + 64);
768 memmove(data + dest_offset, data + src_offset, pagesize);
769 }
770
771 return rc;
772}
773
774
775
776
777
778
779static int alauda_write_block(struct us_data *us, u16 pba, unsigned char *data)
780{
781 int rc;
782 struct alauda_info *info = (struct alauda_info *) us->extra;
783 unsigned char command[] = {
784 ALAUDA_BULK_CMD, ALAUDA_BULK_WRITE_BLOCK, PBA_HI(pba),
785 PBA_ZONE(pba), 0, PBA_LO(pba), 32, 0, MEDIA_PORT(us)
786 };
787
788 usb_stor_dbg(us, "pba %d\n", pba);
789
790 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
791 command, 9, NULL);
792 if (rc != USB_STOR_XFER_GOOD)
793 return rc;
794
795 rc = usb_stor_bulk_transfer_buf(us, info->wr_ep, data,
796 (MEDIA_INFO(us).pagesize + 64) * MEDIA_INFO(us).blocksize,
797 NULL);
798 if (rc != USB_STOR_XFER_GOOD)
799 return rc;
800
801 return alauda_check_status2(us);
802}
803
804
805
806
807static int alauda_write_lba(struct us_data *us, u16 lba,
808 unsigned int page, unsigned int pages,
809 unsigned char *ptr, unsigned char *blockbuffer)
810{
811 u16 pba, lbap, new_pba;
812 unsigned char *bptr, *cptr, *xptr;
813 unsigned char ecc[3];
814 int i, result;
815 unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
816 unsigned int zonesize = MEDIA_INFO(us).zonesize;
817 unsigned int pagesize = MEDIA_INFO(us).pagesize;
818 unsigned int blocksize = MEDIA_INFO(us).blocksize;
819 unsigned int lba_offset = lba % uzonesize;
820 unsigned int new_pba_offset;
821 unsigned int zone = lba / uzonesize;
822
823 alauda_ensure_map_for_zone(us, zone);
824
825 pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
826 if (pba == 1) {
827
828
829 printk(KERN_WARNING
830 "alauda_write_lba: avoid writing to pba 1\n");
831 return USB_STOR_TRANSPORT_GOOD;
832 }
833
834 new_pba = alauda_find_unused_pba(&MEDIA_INFO(us), zone);
835 if (!new_pba) {
836 printk(KERN_WARNING
837 "alauda_write_lba: Out of unused blocks\n");
838 return USB_STOR_TRANSPORT_ERROR;
839 }
840
841
842 if (pba != UNDEF) {
843 result = alauda_read_block_raw(us, pba, 0,
844 blocksize, blockbuffer);
845 if (result != USB_STOR_XFER_GOOD)
846 return result;
847 } else {
848 memset(blockbuffer, 0, blocksize * (pagesize + 64));
849 }
850
851 lbap = (lba_offset << 1) | 0x1000;
852 if (parity[MSB_of(lbap) ^ LSB_of(lbap)])
853 lbap ^= 1;
854
855
856 for (i = 0; i < blocksize; i++) {
857 bptr = blockbuffer + (i * (pagesize + 64));
858 cptr = bptr + pagesize;
859 nand_compute_ecc(bptr, ecc);
860 if (!nand_compare_ecc(cptr+13, ecc)) {
861 usb_stor_dbg(us, "Warning: bad ecc in page %d- of pba %d\n",
862 i, pba);
863 nand_store_ecc(cptr+13, ecc);
864 }
865 nand_compute_ecc(bptr + (pagesize / 2), ecc);
866 if (!nand_compare_ecc(cptr+8, ecc)) {
867 usb_stor_dbg(us, "Warning: bad ecc in page %d+ of pba %d\n",
868 i, pba);
869 nand_store_ecc(cptr+8, ecc);
870 }
871 cptr[6] = cptr[11] = MSB_of(lbap);
872 cptr[7] = cptr[12] = LSB_of(lbap);
873 }
874
875
876 xptr = ptr;
877 for (i = page; i < page+pages; i++) {
878 bptr = blockbuffer + (i * (pagesize + 64));
879 cptr = bptr + pagesize;
880 memcpy(bptr, xptr, pagesize);
881 xptr += pagesize;
882 nand_compute_ecc(bptr, ecc);
883 nand_store_ecc(cptr+13, ecc);
884 nand_compute_ecc(bptr + (pagesize / 2), ecc);
885 nand_store_ecc(cptr+8, ecc);
886 }
887
888 result = alauda_write_block(us, new_pba, blockbuffer);
889 if (result != USB_STOR_XFER_GOOD)
890 return result;
891
892 new_pba_offset = new_pba - (zone * zonesize);
893 MEDIA_INFO(us).pba_to_lba[zone][new_pba_offset] = lba;
894 MEDIA_INFO(us).lba_to_pba[zone][lba_offset] = new_pba;
895 usb_stor_dbg(us, "Remapped LBA %d to PBA %d\n", lba, new_pba);
896
897 if (pba != UNDEF) {
898 unsigned int pba_offset = pba - (zone * zonesize);
899 result = alauda_erase_block(us, pba);
900 if (result != USB_STOR_XFER_GOOD)
901 return result;
902 MEDIA_INFO(us).pba_to_lba[zone][pba_offset] = UNDEF;
903 }
904
905 return USB_STOR_TRANSPORT_GOOD;
906}
907
908
909
910
911static int alauda_read_data(struct us_data *us, unsigned long address,
912 unsigned int sectors)
913{
914 unsigned char *buffer;
915 u16 lba, max_lba;
916 unsigned int page, len, offset;
917 unsigned int blockshift = MEDIA_INFO(us).blockshift;
918 unsigned int pageshift = MEDIA_INFO(us).pageshift;
919 unsigned int blocksize = MEDIA_INFO(us).blocksize;
920 unsigned int pagesize = MEDIA_INFO(us).pagesize;
921 unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
922 struct scatterlist *sg;
923 int result;
924
925
926
927
928
929
930
931
932
933 len = min(sectors, blocksize) * (pagesize + 64);
934 buffer = kmalloc(len, GFP_NOIO);
935 if (buffer == NULL) {
936 printk(KERN_WARNING "alauda_read_data: Out of memory\n");
937 return USB_STOR_TRANSPORT_ERROR;
938 }
939
940
941 lba = address >> blockshift;
942 page = (address & MEDIA_INFO(us).blockmask);
943 max_lba = MEDIA_INFO(us).capacity >> (blockshift + pageshift);
944
945 result = USB_STOR_TRANSPORT_GOOD;
946 offset = 0;
947 sg = NULL;
948
949 while (sectors > 0) {
950 unsigned int zone = lba / uzonesize;
951 unsigned int lba_offset = lba - (zone * uzonesize);
952 unsigned int pages;
953 u16 pba;
954 alauda_ensure_map_for_zone(us, zone);
955
956
957 if (lba >= max_lba) {
958 usb_stor_dbg(us, "Error: Requested lba %u exceeds maximum %u\n",
959 lba, max_lba);
960 result = USB_STOR_TRANSPORT_ERROR;
961 break;
962 }
963
964
965 pages = min(sectors, blocksize - page);
966 len = pages << pageshift;
967
968
969 pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
970
971 if (pba == UNDEF) {
972 usb_stor_dbg(us, "Read %d zero pages (LBA %d) page %d\n",
973 pages, lba, page);
974
975
976
977
978
979
980 memset(buffer, 0, len);
981 } else {
982 usb_stor_dbg(us, "Read %d pages, from PBA %d (LBA %d) page %d\n",
983 pages, pba, lba, page);
984
985 result = alauda_read_block(us, pba, page, pages, buffer);
986 if (result != USB_STOR_TRANSPORT_GOOD)
987 break;
988 }
989
990
991 usb_stor_access_xfer_buf(buffer, len, us->srb,
992 &sg, &offset, TO_XFER_BUF);
993
994 page = 0;
995 lba++;
996 sectors -= pages;
997 }
998
999 kfree(buffer);
1000 return result;
1001}
1002
1003
1004
1005
1006static int alauda_write_data(struct us_data *us, unsigned long address,
1007 unsigned int sectors)
1008{
1009 unsigned char *buffer, *blockbuffer;
1010 unsigned int page, len, offset;
1011 unsigned int blockshift = MEDIA_INFO(us).blockshift;
1012 unsigned int pageshift = MEDIA_INFO(us).pageshift;
1013 unsigned int blocksize = MEDIA_INFO(us).blocksize;
1014 unsigned int pagesize = MEDIA_INFO(us).pagesize;
1015 struct scatterlist *sg;
1016 u16 lba, max_lba;
1017 int result;
1018
1019
1020
1021
1022
1023
1024
1025 len = min(sectors, blocksize) * pagesize;
1026 buffer = kmalloc(len, GFP_NOIO);
1027 if (buffer == NULL) {
1028 printk(KERN_WARNING "alauda_write_data: Out of memory\n");
1029 return USB_STOR_TRANSPORT_ERROR;
1030 }
1031
1032
1033
1034
1035
1036 blockbuffer = kmalloc((pagesize + 64) * blocksize, GFP_NOIO);
1037 if (blockbuffer == NULL) {
1038 printk(KERN_WARNING "alauda_write_data: Out of memory\n");
1039 kfree(buffer);
1040 return USB_STOR_TRANSPORT_ERROR;
1041 }
1042
1043
1044 lba = address >> blockshift;
1045 page = (address & MEDIA_INFO(us).blockmask);
1046 max_lba = MEDIA_INFO(us).capacity >> (pageshift + blockshift);
1047
1048 result = USB_STOR_TRANSPORT_GOOD;
1049 offset = 0;
1050 sg = NULL;
1051
1052 while (sectors > 0) {
1053
1054 unsigned int pages = min(sectors, blocksize - page);
1055 len = pages << pageshift;
1056
1057
1058 if (lba >= max_lba) {
1059 usb_stor_dbg(us, "Requested lba %u exceeds maximum %u\n",
1060 lba, max_lba);
1061 result = USB_STOR_TRANSPORT_ERROR;
1062 break;
1063 }
1064
1065
1066 usb_stor_access_xfer_buf(buffer, len, us->srb,
1067 &sg, &offset, FROM_XFER_BUF);
1068
1069 result = alauda_write_lba(us, lba, page, pages, buffer,
1070 blockbuffer);
1071 if (result != USB_STOR_TRANSPORT_GOOD)
1072 break;
1073
1074 page = 0;
1075 lba++;
1076 sectors -= pages;
1077 }
1078
1079 kfree(buffer);
1080 kfree(blockbuffer);
1081 return result;
1082}
1083
1084
1085
1086
1087
1088static void alauda_info_destructor(void *extra)
1089{
1090 struct alauda_info *info = (struct alauda_info *) extra;
1091 int port;
1092
1093 if (!info)
1094 return;
1095
1096 for (port = 0; port < 2; port++) {
1097 struct alauda_media_info *media_info = &info->port[port];
1098
1099 alauda_free_maps(media_info);
1100 kfree(media_info->lba_to_pba);
1101 kfree(media_info->pba_to_lba);
1102 }
1103}
1104
1105
1106
1107
1108static int init_alauda(struct us_data *us)
1109{
1110 struct alauda_info *info;
1111 struct usb_host_interface *altsetting = us->pusb_intf->cur_altsetting;
1112 nand_init_ecc();
1113
1114 us->extra = kzalloc(sizeof(struct alauda_info), GFP_NOIO);
1115 if (!us->extra)
1116 return USB_STOR_TRANSPORT_ERROR;
1117
1118 info = (struct alauda_info *) us->extra;
1119 us->extra_destructor = alauda_info_destructor;
1120
1121 info->wr_ep = usb_sndbulkpipe(us->pusb_dev,
1122 altsetting->endpoint[0].desc.bEndpointAddress
1123 & USB_ENDPOINT_NUMBER_MASK);
1124
1125 return USB_STOR_TRANSPORT_GOOD;
1126}
1127
1128static int alauda_transport(struct scsi_cmnd *srb, struct us_data *us)
1129{
1130 int rc;
1131 struct alauda_info *info = (struct alauda_info *) us->extra;
1132 unsigned char *ptr = us->iobuf;
1133 static unsigned char inquiry_response[36] = {
1134 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1135 };
1136
1137 if (srb->cmnd[0] == INQUIRY) {
1138 usb_stor_dbg(us, "INQUIRY - Returning bogus response\n");
1139 memcpy(ptr, inquiry_response, sizeof(inquiry_response));
1140 fill_inquiry_response(us, ptr, 36);
1141 return USB_STOR_TRANSPORT_GOOD;
1142 }
1143
1144 if (srb->cmnd[0] == TEST_UNIT_READY) {
1145 usb_stor_dbg(us, "TEST_UNIT_READY\n");
1146 return alauda_check_media(us);
1147 }
1148
1149 if (srb->cmnd[0] == READ_CAPACITY) {
1150 unsigned int num_zones;
1151 unsigned long capacity;
1152
1153 rc = alauda_check_media(us);
1154 if (rc != USB_STOR_TRANSPORT_GOOD)
1155 return rc;
1156
1157 num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift
1158 + MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift);
1159
1160 capacity = num_zones * MEDIA_INFO(us).uzonesize
1161 * MEDIA_INFO(us).blocksize;
1162
1163
1164 ((__be32 *) ptr)[0] = cpu_to_be32(capacity - 1);
1165 ((__be32 *) ptr)[1] = cpu_to_be32(512);
1166
1167 usb_stor_set_xfer_buf(ptr, 8, srb);
1168 return USB_STOR_TRANSPORT_GOOD;
1169 }
1170
1171 if (srb->cmnd[0] == READ_10) {
1172 unsigned int page, pages;
1173
1174 rc = alauda_check_media(us);
1175 if (rc != USB_STOR_TRANSPORT_GOOD)
1176 return rc;
1177
1178 page = short_pack(srb->cmnd[3], srb->cmnd[2]);
1179 page <<= 16;
1180 page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
1181 pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
1182
1183 usb_stor_dbg(us, "READ_10: page %d pagect %d\n", page, pages);
1184
1185 return alauda_read_data(us, page, pages);
1186 }
1187
1188 if (srb->cmnd[0] == WRITE_10) {
1189 unsigned int page, pages;
1190
1191 rc = alauda_check_media(us);
1192 if (rc != USB_STOR_TRANSPORT_GOOD)
1193 return rc;
1194
1195 page = short_pack(srb->cmnd[3], srb->cmnd[2]);
1196 page <<= 16;
1197 page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
1198 pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
1199
1200 usb_stor_dbg(us, "WRITE_10: page %d pagect %d\n", page, pages);
1201
1202 return alauda_write_data(us, page, pages);
1203 }
1204
1205 if (srb->cmnd[0] == REQUEST_SENSE) {
1206 usb_stor_dbg(us, "REQUEST_SENSE\n");
1207
1208 memset(ptr, 0, 18);
1209 ptr[0] = 0xF0;
1210 ptr[2] = info->sense_key;
1211 ptr[7] = 11;
1212 ptr[12] = info->sense_asc;
1213 ptr[13] = info->sense_ascq;
1214 usb_stor_set_xfer_buf(ptr, 18, srb);
1215
1216 return USB_STOR_TRANSPORT_GOOD;
1217 }
1218
1219 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
1220
1221
1222 return USB_STOR_TRANSPORT_GOOD;
1223 }
1224
1225 usb_stor_dbg(us, "Gah! Unknown command: %d (0x%x)\n",
1226 srb->cmnd[0], srb->cmnd[0]);
1227 info->sense_key = 0x05;
1228 info->sense_asc = 0x20;
1229 info->sense_ascq = 0x00;
1230 return USB_STOR_TRANSPORT_FAILED;
1231}
1232
1233static int alauda_probe(struct usb_interface *intf,
1234 const struct usb_device_id *id)
1235{
1236 struct us_data *us;
1237 int result;
1238
1239 result = usb_stor_probe1(&us, intf, id,
1240 (id - alauda_usb_ids) + alauda_unusual_dev_list);
1241 if (result)
1242 return result;
1243
1244 us->transport_name = "Alauda Control/Bulk";
1245 us->transport = alauda_transport;
1246 us->transport_reset = usb_stor_Bulk_reset;
1247 us->max_lun = 1;
1248
1249 result = usb_stor_probe2(us);
1250 return result;
1251}
1252
1253static struct usb_driver alauda_driver = {
1254 .name = "ums-alauda",
1255 .probe = alauda_probe,
1256 .disconnect = usb_stor_disconnect,
1257 .suspend = usb_stor_suspend,
1258 .resume = usb_stor_resume,
1259 .reset_resume = usb_stor_reset_resume,
1260 .pre_reset = usb_stor_pre_reset,
1261 .post_reset = usb_stor_post_reset,
1262 .id_table = alauda_usb_ids,
1263 .soft_unbind = 1,
1264 .no_dynamic_id = 1,
1265};
1266
1267module_usb_driver(alauda_driver);
1268