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