0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021 #include <linux/module.h>
0022 #include <linux/slab.h>
0023
0024 #include <scsi/scsi.h>
0025 #include <scsi/scsi_cmnd.h>
0026 #include <scsi/scsi_device.h>
0027
0028 #include "usb.h"
0029 #include "transport.h"
0030 #include "protocol.h"
0031 #include "debug.h"
0032 #include "scsiglue.h"
0033
0034 #define DRV_NAME "ums-alauda"
0035
0036 MODULE_DESCRIPTION("Driver for Alauda-based card readers");
0037 MODULE_AUTHOR("Daniel Drake <dsd@gentoo.org>");
0038 MODULE_LICENSE("GPL");
0039 MODULE_IMPORT_NS(USB_STORAGE);
0040
0041
0042
0043
0044 #define ALAUDA_STATUS_ERROR 0x01
0045 #define ALAUDA_STATUS_READY 0x40
0046
0047
0048
0049
0050 #define ALAUDA_GET_XD_MEDIA_STATUS 0x08
0051 #define ALAUDA_GET_SM_MEDIA_STATUS 0x98
0052 #define ALAUDA_ACK_XD_MEDIA_CHANGE 0x0a
0053 #define ALAUDA_ACK_SM_MEDIA_CHANGE 0x9a
0054 #define ALAUDA_GET_XD_MEDIA_SIG 0x86
0055 #define ALAUDA_GET_SM_MEDIA_SIG 0x96
0056
0057
0058
0059
0060 #define ALAUDA_BULK_CMD 0x40
0061
0062
0063
0064
0065 #define ALAUDA_BULK_GET_REDU_DATA 0x85
0066 #define ALAUDA_BULK_READ_BLOCK 0x94
0067 #define ALAUDA_BULK_ERASE_BLOCK 0xa3
0068 #define ALAUDA_BULK_WRITE_BLOCK 0xb4
0069 #define ALAUDA_BULK_GET_STATUS2 0xb7
0070 #define ALAUDA_BULK_RESET_MEDIA 0xe0
0071
0072
0073
0074
0075 #define ALAUDA_PORT_XD 0x00
0076 #define ALAUDA_PORT_SM 0x01
0077
0078
0079
0080
0081 #define UNDEF 0xffff
0082 #define SPARE 0xfffe
0083 #define UNUSABLE 0xfffd
0084
0085 struct alauda_media_info {
0086 unsigned long capacity;
0087 unsigned int pagesize;
0088 unsigned int blocksize;
0089 unsigned int uzonesize;
0090 unsigned int zonesize;
0091 unsigned int blockmask;
0092
0093 unsigned char pageshift;
0094 unsigned char blockshift;
0095 unsigned char zoneshift;
0096
0097 u16 **lba_to_pba;
0098 u16 **pba_to_lba;
0099 };
0100
0101 struct alauda_info {
0102 struct alauda_media_info port[2];
0103 int wr_ep;
0104
0105 unsigned char sense_key;
0106 unsigned long sense_asc;
0107 unsigned long sense_ascq;
0108 };
0109
0110 #define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) )
0111 #define LSB_of(s) ((s)&0xFF)
0112 #define MSB_of(s) ((s)>>8)
0113
0114 #define MEDIA_PORT(us) us->srb->device->lun
0115 #define MEDIA_INFO(us) ((struct alauda_info *)us->extra)->port[MEDIA_PORT(us)]
0116
0117 #define PBA_LO(pba) ((pba & 0xF) << 5)
0118 #define PBA_HI(pba) (pba >> 3)
0119 #define PBA_ZONE(pba) (pba >> 11)
0120
0121 static int init_alauda(struct us_data *us);
0122
0123
0124
0125
0126
0127 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
0128 vendorName, productName, useProtocol, useTransport, \
0129 initFunction, flags) \
0130 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
0131 .driver_info = (flags) }
0132
0133 static struct usb_device_id alauda_usb_ids[] = {
0134 # include "unusual_alauda.h"
0135 { }
0136 };
0137 MODULE_DEVICE_TABLE(usb, alauda_usb_ids);
0138
0139 #undef UNUSUAL_DEV
0140
0141
0142
0143
0144 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
0145 vendor_name, product_name, use_protocol, use_transport, \
0146 init_function, Flags) \
0147 { \
0148 .vendorName = vendor_name, \
0149 .productName = product_name, \
0150 .useProtocol = use_protocol, \
0151 .useTransport = use_transport, \
0152 .initFunction = init_function, \
0153 }
0154
0155 static struct us_unusual_dev alauda_unusual_dev_list[] = {
0156 # include "unusual_alauda.h"
0157 { }
0158 };
0159
0160 #undef UNUSUAL_DEV
0161
0162
0163
0164
0165
0166
0167 struct alauda_card_info {
0168 unsigned char id;
0169 unsigned char chipshift;
0170 unsigned char pageshift;
0171 unsigned char blockshift;
0172 unsigned char zoneshift;
0173 };
0174
0175 static struct alauda_card_info alauda_card_ids[] = {
0176
0177 { 0x6e, 20, 8, 4, 8},
0178 { 0xe8, 20, 8, 4, 8},
0179 { 0xec, 20, 8, 4, 8},
0180 { 0x64, 21, 8, 4, 9},
0181 { 0xea, 21, 8, 4, 9},
0182 { 0x6b, 22, 9, 4, 9},
0183 { 0xe3, 22, 9, 4, 9},
0184 { 0xe5, 22, 9, 4, 9},
0185 { 0xe6, 23, 9, 4, 10},
0186 { 0x73, 24, 9, 5, 10},
0187 { 0x75, 25, 9, 5, 10},
0188 { 0x76, 26, 9, 5, 10},
0189 { 0x79, 27, 9, 5, 10},
0190 { 0x71, 28, 9, 5, 10},
0191
0192
0193 { 0x5d, 21, 9, 4, 8},
0194 { 0xd5, 22, 9, 4, 9},
0195 { 0xd6, 23, 9, 4, 10},
0196 { 0x57, 24, 9, 4, 11},
0197 { 0x58, 25, 9, 4, 12},
0198 { 0,}
0199 };
0200
0201 static struct alauda_card_info *alauda_card_find_id(unsigned char id)
0202 {
0203 int i;
0204
0205 for (i = 0; alauda_card_ids[i].id != 0; i++)
0206 if (alauda_card_ids[i].id == id)
0207 return &(alauda_card_ids[i]);
0208 return NULL;
0209 }
0210
0211
0212
0213
0214
0215 static unsigned char parity[256];
0216 static unsigned char ecc2[256];
0217
0218 static void nand_init_ecc(void)
0219 {
0220 int i, j, a;
0221
0222 parity[0] = 0;
0223 for (i = 1; i < 256; i++)
0224 parity[i] = (parity[i&(i-1)] ^ 1);
0225
0226 for (i = 0; i < 256; i++) {
0227 a = 0;
0228 for (j = 0; j < 8; j++) {
0229 if (i & (1<<j)) {
0230 if ((j & 1) == 0)
0231 a ^= 0x04;
0232 if ((j & 2) == 0)
0233 a ^= 0x10;
0234 if ((j & 4) == 0)
0235 a ^= 0x40;
0236 }
0237 }
0238 ecc2[i] = ~(a ^ (a<<1) ^ (parity[i] ? 0xa8 : 0));
0239 }
0240 }
0241
0242
0243 static void nand_compute_ecc(unsigned char *data, unsigned char *ecc)
0244 {
0245 int i, j, a;
0246 unsigned char par = 0, bit, bits[8] = {0};
0247
0248
0249 for (i = 0; i < 256; i++) {
0250 par ^= data[i];
0251 bit = parity[data[i]];
0252 for (j = 0; j < 8; j++)
0253 if ((i & (1<<j)) == 0)
0254 bits[j] ^= bit;
0255 }
0256
0257
0258 a = (bits[3] << 6) + (bits[2] << 4) + (bits[1] << 2) + bits[0];
0259 ecc[0] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
0260
0261 a = (bits[7] << 6) + (bits[6] << 4) + (bits[5] << 2) + bits[4];
0262 ecc[1] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
0263
0264 ecc[2] = ecc2[par];
0265 }
0266
0267 static int nand_compare_ecc(unsigned char *data, unsigned char *ecc)
0268 {
0269 return (data[0] == ecc[0] && data[1] == ecc[1] && data[2] == ecc[2]);
0270 }
0271
0272 static void nand_store_ecc(unsigned char *data, unsigned char *ecc)
0273 {
0274 memcpy(data, ecc, 3);
0275 }
0276
0277
0278
0279
0280
0281
0282
0283
0284 static void alauda_free_maps (struct alauda_media_info *media_info)
0285 {
0286 unsigned int shift = media_info->zoneshift
0287 + media_info->blockshift + media_info->pageshift;
0288 unsigned int num_zones = media_info->capacity >> shift;
0289 unsigned int i;
0290
0291 if (media_info->lba_to_pba != NULL)
0292 for (i = 0; i < num_zones; i++) {
0293 kfree(media_info->lba_to_pba[i]);
0294 media_info->lba_to_pba[i] = NULL;
0295 }
0296
0297 if (media_info->pba_to_lba != NULL)
0298 for (i = 0; i < num_zones; i++) {
0299 kfree(media_info->pba_to_lba[i]);
0300 media_info->pba_to_lba[i] = NULL;
0301 }
0302 }
0303
0304
0305
0306
0307
0308 static int alauda_get_media_status(struct us_data *us, unsigned char *data)
0309 {
0310 int rc;
0311 unsigned char command;
0312
0313 if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
0314 command = ALAUDA_GET_XD_MEDIA_STATUS;
0315 else
0316 command = ALAUDA_GET_SM_MEDIA_STATUS;
0317
0318 rc = usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
0319 command, 0xc0, 0, 1, data, 2);
0320
0321 usb_stor_dbg(us, "Media status %02X %02X\n", data[0], data[1]);
0322
0323 return rc;
0324 }
0325
0326
0327
0328
0329
0330 static int alauda_ack_media(struct us_data *us)
0331 {
0332 unsigned char command;
0333
0334 if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
0335 command = ALAUDA_ACK_XD_MEDIA_CHANGE;
0336 else
0337 command = ALAUDA_ACK_SM_MEDIA_CHANGE;
0338
0339 return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
0340 command, 0x40, 0, 1, NULL, 0);
0341 }
0342
0343
0344
0345
0346
0347 static int alauda_get_media_signature(struct us_data *us, unsigned char *data)
0348 {
0349 unsigned char command;
0350
0351 if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
0352 command = ALAUDA_GET_XD_MEDIA_SIG;
0353 else
0354 command = ALAUDA_GET_SM_MEDIA_SIG;
0355
0356 return usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
0357 command, 0xc0, 0, 0, data, 4);
0358 }
0359
0360
0361
0362
0363 static int alauda_reset_media(struct us_data *us)
0364 {
0365 unsigned char *command = us->iobuf;
0366
0367 memset(command, 0, 9);
0368 command[0] = ALAUDA_BULK_CMD;
0369 command[1] = ALAUDA_BULK_RESET_MEDIA;
0370 command[8] = MEDIA_PORT(us);
0371
0372 return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
0373 command, 9, NULL);
0374 }
0375
0376
0377
0378
0379 static int alauda_init_media(struct us_data *us)
0380 {
0381 unsigned char *data = us->iobuf;
0382 int ready = 0;
0383 struct alauda_card_info *media_info;
0384 unsigned int num_zones;
0385
0386 while (ready == 0) {
0387 msleep(20);
0388
0389 if (alauda_get_media_status(us, data) != USB_STOR_XFER_GOOD)
0390 return USB_STOR_TRANSPORT_ERROR;
0391
0392 if (data[0] & 0x10)
0393 ready = 1;
0394 }
0395
0396 usb_stor_dbg(us, "We are ready for action!\n");
0397
0398 if (alauda_ack_media(us) != USB_STOR_XFER_GOOD)
0399 return USB_STOR_TRANSPORT_ERROR;
0400
0401 msleep(10);
0402
0403 if (alauda_get_media_status(us, data) != USB_STOR_XFER_GOOD)
0404 return USB_STOR_TRANSPORT_ERROR;
0405
0406 if (data[0] != 0x14) {
0407 usb_stor_dbg(us, "Media not ready after ack\n");
0408 return USB_STOR_TRANSPORT_ERROR;
0409 }
0410
0411 if (alauda_get_media_signature(us, data) != USB_STOR_XFER_GOOD)
0412 return USB_STOR_TRANSPORT_ERROR;
0413
0414 usb_stor_dbg(us, "Media signature: %4ph\n", data);
0415 media_info = alauda_card_find_id(data[1]);
0416 if (media_info == NULL) {
0417 pr_warn("alauda_init_media: Unrecognised media signature: %4ph\n",
0418 data);
0419 return USB_STOR_TRANSPORT_ERROR;
0420 }
0421
0422 MEDIA_INFO(us).capacity = 1 << media_info->chipshift;
0423 usb_stor_dbg(us, "Found media with capacity: %ldMB\n",
0424 MEDIA_INFO(us).capacity >> 20);
0425
0426 MEDIA_INFO(us).pageshift = media_info->pageshift;
0427 MEDIA_INFO(us).blockshift = media_info->blockshift;
0428 MEDIA_INFO(us).zoneshift = media_info->zoneshift;
0429
0430 MEDIA_INFO(us).pagesize = 1 << media_info->pageshift;
0431 MEDIA_INFO(us).blocksize = 1 << media_info->blockshift;
0432 MEDIA_INFO(us).zonesize = 1 << media_info->zoneshift;
0433
0434 MEDIA_INFO(us).uzonesize = ((1 << media_info->zoneshift) / 128) * 125;
0435 MEDIA_INFO(us).blockmask = MEDIA_INFO(us).blocksize - 1;
0436
0437 num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift
0438 + MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift);
0439 MEDIA_INFO(us).pba_to_lba = kcalloc(num_zones, sizeof(u16*), GFP_NOIO);
0440 MEDIA_INFO(us).lba_to_pba = kcalloc(num_zones, sizeof(u16*), GFP_NOIO);
0441
0442 if (alauda_reset_media(us) != USB_STOR_XFER_GOOD)
0443 return USB_STOR_TRANSPORT_ERROR;
0444
0445 return USB_STOR_TRANSPORT_GOOD;
0446 }
0447
0448
0449
0450
0451
0452 static int alauda_check_media(struct us_data *us)
0453 {
0454 struct alauda_info *info = (struct alauda_info *) us->extra;
0455 unsigned char status[2];
0456
0457 alauda_get_media_status(us, status);
0458
0459
0460 if ((status[0] & 0x80) || ((status[0] & 0x1F) == 0x10)
0461 || ((status[1] & 0x01) == 0)) {
0462 usb_stor_dbg(us, "No media, or door open\n");
0463 alauda_free_maps(&MEDIA_INFO(us));
0464 info->sense_key = 0x02;
0465 info->sense_asc = 0x3A;
0466 info->sense_ascq = 0x00;
0467 return USB_STOR_TRANSPORT_FAILED;
0468 }
0469
0470
0471 if (status[0] & 0x08) {
0472 usb_stor_dbg(us, "Media change detected\n");
0473 alauda_free_maps(&MEDIA_INFO(us));
0474 alauda_init_media(us);
0475
0476 info->sense_key = UNIT_ATTENTION;
0477 info->sense_asc = 0x28;
0478 info->sense_ascq = 0x00;
0479 return USB_STOR_TRANSPORT_FAILED;
0480 }
0481
0482 return USB_STOR_TRANSPORT_GOOD;
0483 }
0484
0485
0486
0487
0488
0489 static int alauda_check_status2(struct us_data *us)
0490 {
0491 int rc;
0492 unsigned char command[] = {
0493 ALAUDA_BULK_CMD, ALAUDA_BULK_GET_STATUS2,
0494 0, 0, 0, 0, 3, 0, MEDIA_PORT(us)
0495 };
0496 unsigned char data[3];
0497
0498 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
0499 command, 9, NULL);
0500 if (rc != USB_STOR_XFER_GOOD)
0501 return rc;
0502
0503 rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
0504 data, 3, NULL);
0505 if (rc != USB_STOR_XFER_GOOD)
0506 return rc;
0507
0508 usb_stor_dbg(us, "%3ph\n", data);
0509 if (data[0] & ALAUDA_STATUS_ERROR)
0510 return USB_STOR_XFER_ERROR;
0511
0512 return USB_STOR_XFER_GOOD;
0513 }
0514
0515
0516
0517
0518
0519 static int alauda_get_redu_data(struct us_data *us, u16 pba, unsigned char *data)
0520 {
0521 int rc;
0522 unsigned char command[] = {
0523 ALAUDA_BULK_CMD, ALAUDA_BULK_GET_REDU_DATA,
0524 PBA_HI(pba), PBA_ZONE(pba), 0, PBA_LO(pba), 0, 0, MEDIA_PORT(us)
0525 };
0526
0527 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
0528 command, 9, NULL);
0529 if (rc != USB_STOR_XFER_GOOD)
0530 return rc;
0531
0532 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
0533 data, 16, NULL);
0534 }
0535
0536
0537
0538
0539
0540 static u16 alauda_find_unused_pba(struct alauda_media_info *info,
0541 unsigned int zone)
0542 {
0543 u16 *pba_to_lba = info->pba_to_lba[zone];
0544 unsigned int i;
0545
0546 for (i = 0; i < info->zonesize; i++)
0547 if (pba_to_lba[i] == UNDEF)
0548 return (zone << info->zoneshift) + i;
0549
0550 return 0;
0551 }
0552
0553
0554
0555
0556
0557 static int alauda_read_map(struct us_data *us, unsigned int zone)
0558 {
0559 unsigned char *data = us->iobuf;
0560 int result;
0561 int i, j;
0562 unsigned int zonesize = MEDIA_INFO(us).zonesize;
0563 unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
0564 unsigned int lba_offset, lba_real, blocknum;
0565 unsigned int zone_base_lba = zone * uzonesize;
0566 unsigned int zone_base_pba = zone * zonesize;
0567 u16 *lba_to_pba = kcalloc(zonesize, sizeof(u16), GFP_NOIO);
0568 u16 *pba_to_lba = kcalloc(zonesize, sizeof(u16), GFP_NOIO);
0569 if (lba_to_pba == NULL || pba_to_lba == NULL) {
0570 result = USB_STOR_TRANSPORT_ERROR;
0571 goto error;
0572 }
0573
0574 usb_stor_dbg(us, "Mapping blocks for zone %d\n", zone);
0575
0576
0577 for (i = 0; i < zonesize; i++)
0578 lba_to_pba[i] = pba_to_lba[i] = UNDEF;
0579
0580 for (i = 0; i < zonesize; i++) {
0581 blocknum = zone_base_pba + i;
0582
0583 result = alauda_get_redu_data(us, blocknum, data);
0584 if (result != USB_STOR_XFER_GOOD) {
0585 result = USB_STOR_TRANSPORT_ERROR;
0586 goto error;
0587 }
0588
0589
0590 for (j = 0; j < 16; j++)
0591 if (data[j] != 0)
0592 goto nonz;
0593 pba_to_lba[i] = UNUSABLE;
0594 usb_stor_dbg(us, "PBA %d has no logical mapping\n", blocknum);
0595 continue;
0596
0597 nonz:
0598
0599 for (j = 0; j < 16; j++)
0600 if (data[j] != 0xff)
0601 goto nonff;
0602 continue;
0603
0604 nonff:
0605
0606 if (j < 6) {
0607 usb_stor_dbg(us, "PBA %d has no logical mapping: reserved area = %02X%02X%02X%02X data status %02X block status %02X\n",
0608 blocknum,
0609 data[0], data[1], data[2], data[3],
0610 data[4], data[5]);
0611 pba_to_lba[i] = UNUSABLE;
0612 continue;
0613 }
0614
0615 if ((data[6] >> 4) != 0x01) {
0616 usb_stor_dbg(us, "PBA %d has invalid address field %02X%02X/%02X%02X\n",
0617 blocknum, data[6], data[7],
0618 data[11], data[12]);
0619 pba_to_lba[i] = UNUSABLE;
0620 continue;
0621 }
0622
0623
0624 if (parity[data[6] ^ data[7]]) {
0625 printk(KERN_WARNING
0626 "alauda_read_map: Bad parity in LBA for block %d"
0627 " (%02X %02X)\n", i, data[6], data[7]);
0628 pba_to_lba[i] = UNUSABLE;
0629 continue;
0630 }
0631
0632 lba_offset = short_pack(data[7], data[6]);
0633 lba_offset = (lba_offset & 0x07FF) >> 1;
0634 lba_real = lba_offset + zone_base_lba;
0635
0636
0637
0638
0639
0640
0641
0642
0643
0644
0645 if (lba_offset >= uzonesize) {
0646 printk(KERN_WARNING
0647 "alauda_read_map: Bad low LBA %d for block %d\n",
0648 lba_real, blocknum);
0649 continue;
0650 }
0651
0652 if (lba_to_pba[lba_offset] != UNDEF) {
0653 printk(KERN_WARNING
0654 "alauda_read_map: "
0655 "LBA %d seen for PBA %d and %d\n",
0656 lba_real, lba_to_pba[lba_offset], blocknum);
0657 continue;
0658 }
0659
0660 pba_to_lba[i] = lba_real;
0661 lba_to_pba[lba_offset] = blocknum;
0662 continue;
0663 }
0664
0665 MEDIA_INFO(us).lba_to_pba[zone] = lba_to_pba;
0666 MEDIA_INFO(us).pba_to_lba[zone] = pba_to_lba;
0667 result = 0;
0668 goto out;
0669
0670 error:
0671 kfree(lba_to_pba);
0672 kfree(pba_to_lba);
0673 out:
0674 return result;
0675 }
0676
0677
0678
0679
0680
0681 static void alauda_ensure_map_for_zone(struct us_data *us, unsigned int zone)
0682 {
0683 if (MEDIA_INFO(us).lba_to_pba[zone] == NULL
0684 || MEDIA_INFO(us).pba_to_lba[zone] == NULL)
0685 alauda_read_map(us, zone);
0686 }
0687
0688
0689
0690
0691 static int alauda_erase_block(struct us_data *us, u16 pba)
0692 {
0693 int rc;
0694 unsigned char command[] = {
0695 ALAUDA_BULK_CMD, ALAUDA_BULK_ERASE_BLOCK, PBA_HI(pba),
0696 PBA_ZONE(pba), 0, PBA_LO(pba), 0x02, 0, MEDIA_PORT(us)
0697 };
0698 unsigned char buf[2];
0699
0700 usb_stor_dbg(us, "Erasing PBA %d\n", pba);
0701
0702 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
0703 command, 9, NULL);
0704 if (rc != USB_STOR_XFER_GOOD)
0705 return rc;
0706
0707 rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
0708 buf, 2, NULL);
0709 if (rc != USB_STOR_XFER_GOOD)
0710 return rc;
0711
0712 usb_stor_dbg(us, "Erase result: %02X %02X\n", buf[0], buf[1]);
0713 return rc;
0714 }
0715
0716
0717
0718
0719
0720 static int alauda_read_block_raw(struct us_data *us, u16 pba,
0721 unsigned int page, unsigned int pages, unsigned char *data)
0722 {
0723 int rc;
0724 unsigned char command[] = {
0725 ALAUDA_BULK_CMD, ALAUDA_BULK_READ_BLOCK, PBA_HI(pba),
0726 PBA_ZONE(pba), 0, PBA_LO(pba) + page, pages, 0, MEDIA_PORT(us)
0727 };
0728
0729 usb_stor_dbg(us, "pba %d page %d count %d\n", pba, page, pages);
0730
0731 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
0732 command, 9, NULL);
0733 if (rc != USB_STOR_XFER_GOOD)
0734 return rc;
0735
0736 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
0737 data, (MEDIA_INFO(us).pagesize + 64) * pages, NULL);
0738 }
0739
0740
0741
0742
0743
0744
0745
0746 static int alauda_read_block(struct us_data *us, u16 pba,
0747 unsigned int page, unsigned int pages, unsigned char *data)
0748 {
0749 int i, rc;
0750 unsigned int pagesize = MEDIA_INFO(us).pagesize;
0751
0752 rc = alauda_read_block_raw(us, pba, page, pages, data);
0753 if (rc != USB_STOR_XFER_GOOD)
0754 return rc;
0755
0756
0757 for (i = 0; i < pages; i++) {
0758 int dest_offset = i * pagesize;
0759 int src_offset = i * (pagesize + 64);
0760 memmove(data + dest_offset, data + src_offset, pagesize);
0761 }
0762
0763 return rc;
0764 }
0765
0766
0767
0768
0769
0770
0771 static int alauda_write_block(struct us_data *us, u16 pba, unsigned char *data)
0772 {
0773 int rc;
0774 struct alauda_info *info = (struct alauda_info *) us->extra;
0775 unsigned char command[] = {
0776 ALAUDA_BULK_CMD, ALAUDA_BULK_WRITE_BLOCK, PBA_HI(pba),
0777 PBA_ZONE(pba), 0, PBA_LO(pba), 32, 0, MEDIA_PORT(us)
0778 };
0779
0780 usb_stor_dbg(us, "pba %d\n", pba);
0781
0782 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
0783 command, 9, NULL);
0784 if (rc != USB_STOR_XFER_GOOD)
0785 return rc;
0786
0787 rc = usb_stor_bulk_transfer_buf(us, info->wr_ep, data,
0788 (MEDIA_INFO(us).pagesize + 64) * MEDIA_INFO(us).blocksize,
0789 NULL);
0790 if (rc != USB_STOR_XFER_GOOD)
0791 return rc;
0792
0793 return alauda_check_status2(us);
0794 }
0795
0796
0797
0798
0799 static int alauda_write_lba(struct us_data *us, u16 lba,
0800 unsigned int page, unsigned int pages,
0801 unsigned char *ptr, unsigned char *blockbuffer)
0802 {
0803 u16 pba, lbap, new_pba;
0804 unsigned char *bptr, *cptr, *xptr;
0805 unsigned char ecc[3];
0806 int i, result;
0807 unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
0808 unsigned int zonesize = MEDIA_INFO(us).zonesize;
0809 unsigned int pagesize = MEDIA_INFO(us).pagesize;
0810 unsigned int blocksize = MEDIA_INFO(us).blocksize;
0811 unsigned int lba_offset = lba % uzonesize;
0812 unsigned int new_pba_offset;
0813 unsigned int zone = lba / uzonesize;
0814
0815 alauda_ensure_map_for_zone(us, zone);
0816
0817 pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
0818 if (pba == 1) {
0819
0820
0821
0822
0823 printk(KERN_WARNING
0824 "alauda_write_lba: avoid writing to pba 1\n");
0825 return USB_STOR_TRANSPORT_GOOD;
0826 }
0827
0828 new_pba = alauda_find_unused_pba(&MEDIA_INFO(us), zone);
0829 if (!new_pba) {
0830 printk(KERN_WARNING
0831 "alauda_write_lba: Out of unused blocks\n");
0832 return USB_STOR_TRANSPORT_ERROR;
0833 }
0834
0835
0836 if (pba != UNDEF) {
0837 result = alauda_read_block_raw(us, pba, 0,
0838 blocksize, blockbuffer);
0839 if (result != USB_STOR_XFER_GOOD)
0840 return result;
0841 } else {
0842 memset(blockbuffer, 0, blocksize * (pagesize + 64));
0843 }
0844
0845 lbap = (lba_offset << 1) | 0x1000;
0846 if (parity[MSB_of(lbap) ^ LSB_of(lbap)])
0847 lbap ^= 1;
0848
0849
0850 for (i = 0; i < blocksize; i++) {
0851 bptr = blockbuffer + (i * (pagesize + 64));
0852 cptr = bptr + pagesize;
0853 nand_compute_ecc(bptr, ecc);
0854 if (!nand_compare_ecc(cptr+13, ecc)) {
0855 usb_stor_dbg(us, "Warning: bad ecc in page %d- of pba %d\n",
0856 i, pba);
0857 nand_store_ecc(cptr+13, ecc);
0858 }
0859 nand_compute_ecc(bptr + (pagesize / 2), ecc);
0860 if (!nand_compare_ecc(cptr+8, ecc)) {
0861 usb_stor_dbg(us, "Warning: bad ecc in page %d+ of pba %d\n",
0862 i, pba);
0863 nand_store_ecc(cptr+8, ecc);
0864 }
0865 cptr[6] = cptr[11] = MSB_of(lbap);
0866 cptr[7] = cptr[12] = LSB_of(lbap);
0867 }
0868
0869
0870 xptr = ptr;
0871 for (i = page; i < page+pages; i++) {
0872 bptr = blockbuffer + (i * (pagesize + 64));
0873 cptr = bptr + pagesize;
0874 memcpy(bptr, xptr, pagesize);
0875 xptr += pagesize;
0876 nand_compute_ecc(bptr, ecc);
0877 nand_store_ecc(cptr+13, ecc);
0878 nand_compute_ecc(bptr + (pagesize / 2), ecc);
0879 nand_store_ecc(cptr+8, ecc);
0880 }
0881
0882 result = alauda_write_block(us, new_pba, blockbuffer);
0883 if (result != USB_STOR_XFER_GOOD)
0884 return result;
0885
0886 new_pba_offset = new_pba - (zone * zonesize);
0887 MEDIA_INFO(us).pba_to_lba[zone][new_pba_offset] = lba;
0888 MEDIA_INFO(us).lba_to_pba[zone][lba_offset] = new_pba;
0889 usb_stor_dbg(us, "Remapped LBA %d to PBA %d\n", lba, new_pba);
0890
0891 if (pba != UNDEF) {
0892 unsigned int pba_offset = pba - (zone * zonesize);
0893 result = alauda_erase_block(us, pba);
0894 if (result != USB_STOR_XFER_GOOD)
0895 return result;
0896 MEDIA_INFO(us).pba_to_lba[zone][pba_offset] = UNDEF;
0897 }
0898
0899 return USB_STOR_TRANSPORT_GOOD;
0900 }
0901
0902
0903
0904
0905 static int alauda_read_data(struct us_data *us, unsigned long address,
0906 unsigned int sectors)
0907 {
0908 unsigned char *buffer;
0909 u16 lba, max_lba;
0910 unsigned int page, len, offset;
0911 unsigned int blockshift = MEDIA_INFO(us).blockshift;
0912 unsigned int pageshift = MEDIA_INFO(us).pageshift;
0913 unsigned int blocksize = MEDIA_INFO(us).blocksize;
0914 unsigned int pagesize = MEDIA_INFO(us).pagesize;
0915 unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
0916 struct scatterlist *sg;
0917 int result;
0918
0919
0920
0921
0922
0923
0924
0925
0926
0927 len = min(sectors, blocksize) * (pagesize + 64);
0928 buffer = kmalloc(len, GFP_NOIO);
0929 if (!buffer)
0930 return USB_STOR_TRANSPORT_ERROR;
0931
0932
0933 lba = address >> blockshift;
0934 page = (address & MEDIA_INFO(us).blockmask);
0935 max_lba = MEDIA_INFO(us).capacity >> (blockshift + pageshift);
0936
0937 result = USB_STOR_TRANSPORT_GOOD;
0938 offset = 0;
0939 sg = NULL;
0940
0941 while (sectors > 0) {
0942 unsigned int zone = lba / uzonesize;
0943 unsigned int lba_offset = lba - (zone * uzonesize);
0944 unsigned int pages;
0945 u16 pba;
0946 alauda_ensure_map_for_zone(us, zone);
0947
0948
0949 if (lba >= max_lba) {
0950 usb_stor_dbg(us, "Error: Requested lba %u exceeds maximum %u\n",
0951 lba, max_lba);
0952 result = USB_STOR_TRANSPORT_ERROR;
0953 break;
0954 }
0955
0956
0957 pages = min(sectors, blocksize - page);
0958 len = pages << pageshift;
0959
0960
0961 pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
0962
0963 if (pba == UNDEF) {
0964 usb_stor_dbg(us, "Read %d zero pages (LBA %d) page %d\n",
0965 pages, lba, page);
0966
0967
0968
0969
0970
0971
0972
0973
0974 memset(buffer, 0, len);
0975 } else {
0976 usb_stor_dbg(us, "Read %d pages, from PBA %d (LBA %d) page %d\n",
0977 pages, pba, lba, page);
0978
0979 result = alauda_read_block(us, pba, page, pages, buffer);
0980 if (result != USB_STOR_TRANSPORT_GOOD)
0981 break;
0982 }
0983
0984
0985 usb_stor_access_xfer_buf(buffer, len, us->srb,
0986 &sg, &offset, TO_XFER_BUF);
0987
0988 page = 0;
0989 lba++;
0990 sectors -= pages;
0991 }
0992
0993 kfree(buffer);
0994 return result;
0995 }
0996
0997
0998
0999
1000 static int alauda_write_data(struct us_data *us, unsigned long address,
1001 unsigned int sectors)
1002 {
1003 unsigned char *buffer, *blockbuffer;
1004 unsigned int page, len, offset;
1005 unsigned int blockshift = MEDIA_INFO(us).blockshift;
1006 unsigned int pageshift = MEDIA_INFO(us).pageshift;
1007 unsigned int blocksize = MEDIA_INFO(us).blocksize;
1008 unsigned int pagesize = MEDIA_INFO(us).pagesize;
1009 struct scatterlist *sg;
1010 u16 lba, max_lba;
1011 int result;
1012
1013
1014
1015
1016
1017
1018
1019 len = min(sectors, blocksize) * pagesize;
1020 buffer = kmalloc(len, GFP_NOIO);
1021 if (!buffer)
1022 return USB_STOR_TRANSPORT_ERROR;
1023
1024
1025
1026
1027
1028 blockbuffer = kmalloc_array(pagesize + 64, blocksize, GFP_NOIO);
1029 if (!blockbuffer) {
1030 kfree(buffer);
1031 return USB_STOR_TRANSPORT_ERROR;
1032 }
1033
1034
1035 lba = address >> blockshift;
1036 page = (address & MEDIA_INFO(us).blockmask);
1037 max_lba = MEDIA_INFO(us).capacity >> (pageshift + blockshift);
1038
1039 result = USB_STOR_TRANSPORT_GOOD;
1040 offset = 0;
1041 sg = NULL;
1042
1043 while (sectors > 0) {
1044
1045 unsigned int pages = min(sectors, blocksize - page);
1046 len = pages << pageshift;
1047
1048
1049 if (lba >= max_lba) {
1050 usb_stor_dbg(us, "Requested lba %u exceeds maximum %u\n",
1051 lba, max_lba);
1052 result = USB_STOR_TRANSPORT_ERROR;
1053 break;
1054 }
1055
1056
1057 usb_stor_access_xfer_buf(buffer, len, us->srb,
1058 &sg, &offset, FROM_XFER_BUF);
1059
1060 result = alauda_write_lba(us, lba, page, pages, buffer,
1061 blockbuffer);
1062 if (result != USB_STOR_TRANSPORT_GOOD)
1063 break;
1064
1065 page = 0;
1066 lba++;
1067 sectors -= pages;
1068 }
1069
1070 kfree(buffer);
1071 kfree(blockbuffer);
1072 return result;
1073 }
1074
1075
1076
1077
1078
1079 static void alauda_info_destructor(void *extra)
1080 {
1081 struct alauda_info *info = (struct alauda_info *) extra;
1082 int port;
1083
1084 if (!info)
1085 return;
1086
1087 for (port = 0; port < 2; port++) {
1088 struct alauda_media_info *media_info = &info->port[port];
1089
1090 alauda_free_maps(media_info);
1091 kfree(media_info->lba_to_pba);
1092 kfree(media_info->pba_to_lba);
1093 }
1094 }
1095
1096
1097
1098
1099 static int init_alauda(struct us_data *us)
1100 {
1101 struct alauda_info *info;
1102 struct usb_host_interface *altsetting = us->pusb_intf->cur_altsetting;
1103 nand_init_ecc();
1104
1105 us->extra = kzalloc(sizeof(struct alauda_info), GFP_NOIO);
1106 if (!us->extra)
1107 return -ENOMEM;
1108
1109 info = (struct alauda_info *) us->extra;
1110 us->extra_destructor = alauda_info_destructor;
1111
1112 info->wr_ep = usb_sndbulkpipe(us->pusb_dev,
1113 altsetting->endpoint[0].desc.bEndpointAddress
1114 & USB_ENDPOINT_NUMBER_MASK);
1115
1116 return 0;
1117 }
1118
1119 static int alauda_transport(struct scsi_cmnd *srb, struct us_data *us)
1120 {
1121 int rc;
1122 struct alauda_info *info = (struct alauda_info *) us->extra;
1123 unsigned char *ptr = us->iobuf;
1124 static unsigned char inquiry_response[36] = {
1125 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1126 };
1127
1128 if (srb->cmnd[0] == INQUIRY) {
1129 usb_stor_dbg(us, "INQUIRY - Returning bogus response\n");
1130 memcpy(ptr, inquiry_response, sizeof(inquiry_response));
1131 fill_inquiry_response(us, ptr, 36);
1132 return USB_STOR_TRANSPORT_GOOD;
1133 }
1134
1135 if (srb->cmnd[0] == TEST_UNIT_READY) {
1136 usb_stor_dbg(us, "TEST_UNIT_READY\n");
1137 return alauda_check_media(us);
1138 }
1139
1140 if (srb->cmnd[0] == READ_CAPACITY) {
1141 unsigned int num_zones;
1142 unsigned long capacity;
1143
1144 rc = alauda_check_media(us);
1145 if (rc != USB_STOR_TRANSPORT_GOOD)
1146 return rc;
1147
1148 num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift
1149 + MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift);
1150
1151 capacity = num_zones * MEDIA_INFO(us).uzonesize
1152 * MEDIA_INFO(us).blocksize;
1153
1154
1155 ((__be32 *) ptr)[0] = cpu_to_be32(capacity - 1);
1156 ((__be32 *) ptr)[1] = cpu_to_be32(512);
1157
1158 usb_stor_set_xfer_buf(ptr, 8, srb);
1159 return USB_STOR_TRANSPORT_GOOD;
1160 }
1161
1162 if (srb->cmnd[0] == READ_10) {
1163 unsigned int page, pages;
1164
1165 rc = alauda_check_media(us);
1166 if (rc != USB_STOR_TRANSPORT_GOOD)
1167 return rc;
1168
1169 page = short_pack(srb->cmnd[3], srb->cmnd[2]);
1170 page <<= 16;
1171 page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
1172 pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
1173
1174 usb_stor_dbg(us, "READ_10: page %d pagect %d\n", page, pages);
1175
1176 return alauda_read_data(us, page, pages);
1177 }
1178
1179 if (srb->cmnd[0] == WRITE_10) {
1180 unsigned int page, pages;
1181
1182 rc = alauda_check_media(us);
1183 if (rc != USB_STOR_TRANSPORT_GOOD)
1184 return rc;
1185
1186 page = short_pack(srb->cmnd[3], srb->cmnd[2]);
1187 page <<= 16;
1188 page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
1189 pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
1190
1191 usb_stor_dbg(us, "WRITE_10: page %d pagect %d\n", page, pages);
1192
1193 return alauda_write_data(us, page, pages);
1194 }
1195
1196 if (srb->cmnd[0] == REQUEST_SENSE) {
1197 usb_stor_dbg(us, "REQUEST_SENSE\n");
1198
1199 memset(ptr, 0, 18);
1200 ptr[0] = 0xF0;
1201 ptr[2] = info->sense_key;
1202 ptr[7] = 11;
1203 ptr[12] = info->sense_asc;
1204 ptr[13] = info->sense_ascq;
1205 usb_stor_set_xfer_buf(ptr, 18, srb);
1206
1207 return USB_STOR_TRANSPORT_GOOD;
1208 }
1209
1210 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
1211
1212
1213
1214
1215 return USB_STOR_TRANSPORT_GOOD;
1216 }
1217
1218 usb_stor_dbg(us, "Gah! Unknown command: %d (0x%x)\n",
1219 srb->cmnd[0], srb->cmnd[0]);
1220 info->sense_key = 0x05;
1221 info->sense_asc = 0x20;
1222 info->sense_ascq = 0x00;
1223 return USB_STOR_TRANSPORT_FAILED;
1224 }
1225
1226 static struct scsi_host_template alauda_host_template;
1227
1228 static int alauda_probe(struct usb_interface *intf,
1229 const struct usb_device_id *id)
1230 {
1231 struct us_data *us;
1232 int result;
1233
1234 result = usb_stor_probe1(&us, intf, id,
1235 (id - alauda_usb_ids) + alauda_unusual_dev_list,
1236 &alauda_host_template);
1237 if (result)
1238 return result;
1239
1240 us->transport_name = "Alauda Control/Bulk";
1241 us->transport = alauda_transport;
1242 us->transport_reset = usb_stor_Bulk_reset;
1243 us->max_lun = 1;
1244
1245 result = usb_stor_probe2(us);
1246 return result;
1247 }
1248
1249 static struct usb_driver alauda_driver = {
1250 .name = DRV_NAME,
1251 .probe = alauda_probe,
1252 .disconnect = usb_stor_disconnect,
1253 .suspend = usb_stor_suspend,
1254 .resume = usb_stor_resume,
1255 .reset_resume = usb_stor_reset_resume,
1256 .pre_reset = usb_stor_pre_reset,
1257 .post_reset = usb_stor_post_reset,
1258 .id_table = alauda_usb_ids,
1259 .soft_unbind = 1,
1260 .no_dynamic_id = 1,
1261 };
1262
1263 module_usb_stor_driver(alauda_driver, alauda_host_template, DRV_NAME);