Matthew Dharm | e80b0fa | 2005-12-04 22:02:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Driver for Alauda-based card readers |
| 3 | * |
| 4 | * Current development and maintenance by: |
| 5 | * (c) 2005 Daniel Drake <dsd@gentoo.org> |
| 6 | * |
| 7 | * The 'Alauda' is a chip manufacturered by RATOC for OEM use. |
| 8 | * |
| 9 | * Alauda implements a vendor-specific command set to access two media reader |
| 10 | * ports (XD, SmartMedia). This driver converts SCSI commands to the commands |
| 11 | * which are accepted by these devices. |
| 12 | * |
| 13 | * The driver was developed through reverse-engineering, with the help of the |
| 14 | * sddr09 driver which has many similarities, and with some help from the |
| 15 | * (very old) vendor-supplied GPL sma03 driver. |
| 16 | * |
| 17 | * For protocol info, see http://alauda.sourceforge.net |
| 18 | * |
| 19 | * This program is free software; you can redistribute it and/or modify it |
| 20 | * under the terms of the GNU General Public License as published by the |
| 21 | * Free Software Foundation; either version 2, or (at your option) any |
| 22 | * later version. |
| 23 | * |
| 24 | * This program is distributed in the hope that it will be useful, but |
| 25 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 26 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 27 | * General Public License for more details. |
| 28 | * |
| 29 | * You should have received a copy of the GNU General Public License along |
| 30 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 31 | * 675 Mass Ave, Cambridge, MA 02139, USA. |
| 32 | */ |
| 33 | |
Alan Stern | a74bba3 | 2009-02-12 14:48:22 -0500 | [diff] [blame] | 34 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 35 | #include <linux/slab.h> |
Alan Stern | a74bba3 | 2009-02-12 14:48:22 -0500 | [diff] [blame] | 36 | |
Matthew Dharm | e80b0fa | 2005-12-04 22:02:44 -0800 | [diff] [blame] | 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" |
Alan Stern | a74bba3 | 2009-02-12 14:48:22 -0500 | [diff] [blame] | 45 | |
Maciej Grela | 4246b06 | 2009-02-28 12:39:20 -0800 | [diff] [blame] | 46 | MODULE_DESCRIPTION("Driver for Alauda-based card readers"); |
| 47 | MODULE_AUTHOR("Daniel Drake <dsd@gentoo.org>"); |
| 48 | MODULE_LICENSE("GPL"); |
| 49 | |
Alan Stern | a74bba3 | 2009-02-12 14:48:22 -0500 | [diff] [blame] | 50 | /* |
| 51 | * Status bytes |
| 52 | */ |
| 53 | #define ALAUDA_STATUS_ERROR 0x01 |
| 54 | #define ALAUDA_STATUS_READY 0x40 |
| 55 | |
| 56 | /* |
| 57 | * Control opcodes (for request field) |
| 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 | * Bulk command identity (byte 0) |
| 68 | */ |
| 69 | #define ALAUDA_BULK_CMD 0x40 |
| 70 | |
| 71 | /* |
| 72 | * Bulk opcodes (byte 1) |
| 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 | * Port to operate on (byte 8) |
| 83 | */ |
| 84 | #define ALAUDA_PORT_XD 0x00 |
| 85 | #define ALAUDA_PORT_SM 0x01 |
| 86 | |
| 87 | /* |
| 88 | * LBA and PBA are unsigned ints. Special values. |
| 89 | */ |
| 90 | #define UNDEF 0xffff |
| 91 | #define SPARE 0xfffe |
| 92 | #define UNUSABLE 0xfffd |
| 93 | |
| 94 | struct alauda_media_info { |
| 95 | unsigned long capacity; /* total media size in bytes */ |
| 96 | unsigned int pagesize; /* page size in bytes */ |
| 97 | unsigned int blocksize; /* number of pages per block */ |
| 98 | unsigned int uzonesize; /* number of usable blocks per zone */ |
| 99 | unsigned int zonesize; /* number of blocks per zone */ |
| 100 | unsigned int blockmask; /* mask to get page from address */ |
| 101 | |
| 102 | unsigned char pageshift; |
| 103 | unsigned char blockshift; |
| 104 | unsigned char zoneshift; |
| 105 | |
| 106 | u16 **lba_to_pba; /* logical to physical block map */ |
| 107 | u16 **pba_to_lba; /* physical to logical block map */ |
| 108 | }; |
| 109 | |
| 110 | struct alauda_info { |
| 111 | struct alauda_media_info port[2]; |
| 112 | int wr_ep; /* endpoint to write data out of */ |
| 113 | |
| 114 | unsigned char sense_key; |
| 115 | unsigned long sense_asc; /* additional sense code */ |
| 116 | unsigned long sense_ascq; /* additional sense code qualifier */ |
| 117 | }; |
Matthew Dharm | e80b0fa | 2005-12-04 22:02:44 -0800 | [diff] [blame] | 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 | |
Alan Stern | a74bba3 | 2009-02-12 14:48:22 -0500 | [diff] [blame] | 130 | static int init_alauda(struct us_data *us); |
| 131 | |
| 132 | |
| 133 | /* |
| 134 | * The table of devices |
| 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), \ |
Sebastian Andrzej Siewior | f61870e | 2012-08-28 22:37:13 +0200 | [diff] [blame] | 140 | .driver_info = (flags) } |
Alan Stern | a74bba3 | 2009-02-12 14:48:22 -0500 | [diff] [blame] | 141 | |
Felipe Balbi | 3358be9 | 2011-11-15 09:53:28 +0200 | [diff] [blame] | 142 | static struct usb_device_id alauda_usb_ids[] = { |
Alan Stern | a74bba3 | 2009-02-12 14:48:22 -0500 | [diff] [blame] | 143 | # include "unusual_alauda.h" |
| 144 | { } /* Terminating entry */ |
| 145 | }; |
| 146 | MODULE_DEVICE_TABLE(usb, alauda_usb_ids); |
| 147 | |
| 148 | #undef UNUSUAL_DEV |
| 149 | |
| 150 | /* |
| 151 | * The flags table |
| 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 | |
| 164 | static struct us_unusual_dev alauda_unusual_dev_list[] = { |
| 165 | # include "unusual_alauda.h" |
| 166 | { } /* Terminating entry */ |
| 167 | }; |
| 168 | |
| 169 | #undef UNUSUAL_DEV |
| 170 | |
| 171 | |
Matthew Dharm | e80b0fa | 2005-12-04 22:02:44 -0800 | [diff] [blame] | 172 | /* |
| 173 | * Media handling |
| 174 | */ |
| 175 | |
| 176 | struct alauda_card_info { |
| 177 | unsigned char id; /* id byte */ |
| 178 | unsigned char chipshift; /* 1<<cs bytes total capacity */ |
| 179 | unsigned char pageshift; /* 1<<ps bytes in a page */ |
| 180 | unsigned char blockshift; /* 1<<bs pages per block */ |
| 181 | unsigned char zoneshift; /* 1<<zs blocks per zone */ |
| 182 | }; |
| 183 | |
| 184 | static struct alauda_card_info alauda_card_ids[] = { |
| 185 | /* NAND flash */ |
| 186 | { 0x6e, 20, 8, 4, 8}, /* 1 MB */ |
| 187 | { 0xe8, 20, 8, 4, 8}, /* 1 MB */ |
| 188 | { 0xec, 20, 8, 4, 8}, /* 1 MB */ |
| 189 | { 0x64, 21, 8, 4, 9}, /* 2 MB */ |
| 190 | { 0xea, 21, 8, 4, 9}, /* 2 MB */ |
| 191 | { 0x6b, 22, 9, 4, 9}, /* 4 MB */ |
| 192 | { 0xe3, 22, 9, 4, 9}, /* 4 MB */ |
| 193 | { 0xe5, 22, 9, 4, 9}, /* 4 MB */ |
| 194 | { 0xe6, 23, 9, 4, 10}, /* 8 MB */ |
| 195 | { 0x73, 24, 9, 5, 10}, /* 16 MB */ |
| 196 | { 0x75, 25, 9, 5, 10}, /* 32 MB */ |
| 197 | { 0x76, 26, 9, 5, 10}, /* 64 MB */ |
| 198 | { 0x79, 27, 9, 5, 10}, /* 128 MB */ |
| 199 | { 0x71, 28, 9, 5, 10}, /* 256 MB */ |
| 200 | |
| 201 | /* MASK ROM */ |
| 202 | { 0x5d, 21, 9, 4, 8}, /* 2 MB */ |
| 203 | { 0xd5, 22, 9, 4, 9}, /* 4 MB */ |
| 204 | { 0xd6, 23, 9, 4, 10}, /* 8 MB */ |
| 205 | { 0x57, 24, 9, 4, 11}, /* 16 MB */ |
| 206 | { 0x58, 25, 9, 4, 12}, /* 32 MB */ |
| 207 | { 0,} |
| 208 | }; |
| 209 | |
| 210 | static 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 | * ECC computation. |
| 221 | */ |
| 222 | |
| 223 | static unsigned char parity[256]; |
| 224 | static unsigned char ecc2[256]; |
| 225 | |
| 226 | static 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 | /* compute 3-byte ecc on 256 bytes */ |
| 250 | static void nand_compute_ecc(unsigned char *data, unsigned char *ecc) { |
| 251 | int i, j, a; |
| 252 | unsigned char par, bit, bits[8]; |
| 253 | |
| 254 | par = 0; |
| 255 | for (j = 0; j < 8; j++) |
| 256 | bits[j] = 0; |
| 257 | |
| 258 | /* collect 16 checksum bits */ |
| 259 | for (i = 0; i < 256; i++) { |
| 260 | par ^= data[i]; |
| 261 | bit = parity[data[i]]; |
| 262 | for (j = 0; j < 8; j++) |
| 263 | if ((i & (1<<j)) == 0) |
| 264 | bits[j] ^= bit; |
| 265 | } |
| 266 | |
| 267 | /* put 4+4+4 = 12 bits in the ecc */ |
| 268 | a = (bits[3] << 6) + (bits[2] << 4) + (bits[1] << 2) + bits[0]; |
| 269 | ecc[0] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0)); |
| 270 | |
| 271 | a = (bits[7] << 6) + (bits[6] << 4) + (bits[5] << 2) + bits[4]; |
| 272 | ecc[1] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0)); |
| 273 | |
| 274 | ecc[2] = ecc2[par]; |
| 275 | } |
| 276 | |
| 277 | static int nand_compare_ecc(unsigned char *data, unsigned char *ecc) { |
| 278 | return (data[0] == ecc[0] && data[1] == ecc[1] && data[2] == ecc[2]); |
| 279 | } |
| 280 | |
| 281 | static void nand_store_ecc(unsigned char *data, unsigned char *ecc) { |
| 282 | memcpy(data, ecc, 3); |
| 283 | } |
| 284 | |
| 285 | /* |
| 286 | * Alauda driver |
| 287 | */ |
| 288 | |
| 289 | /* |
| 290 | * Forget our PBA <---> LBA mappings for a particular port |
| 291 | */ |
| 292 | static void alauda_free_maps (struct alauda_media_info *media_info) |
| 293 | { |
| 294 | unsigned int shift = media_info->zoneshift |
| 295 | + media_info->blockshift + media_info->pageshift; |
| 296 | unsigned int num_zones = media_info->capacity >> shift; |
| 297 | unsigned int i; |
| 298 | |
| 299 | if (media_info->lba_to_pba != NULL) |
| 300 | for (i = 0; i < num_zones; i++) { |
| 301 | kfree(media_info->lba_to_pba[i]); |
| 302 | media_info->lba_to_pba[i] = NULL; |
| 303 | } |
| 304 | |
| 305 | if (media_info->pba_to_lba != NULL) |
| 306 | for (i = 0; i < num_zones; i++) { |
| 307 | kfree(media_info->pba_to_lba[i]); |
| 308 | media_info->pba_to_lba[i] = NULL; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | /* |
| 313 | * Returns 2 bytes of status data |
| 314 | * The first byte describes media status, and second byte describes door status |
| 315 | */ |
| 316 | static int alauda_get_media_status(struct us_data *us, unsigned char *data) |
| 317 | { |
| 318 | int rc; |
| 319 | unsigned char command; |
| 320 | |
| 321 | if (MEDIA_PORT(us) == ALAUDA_PORT_XD) |
| 322 | command = ALAUDA_GET_XD_MEDIA_STATUS; |
| 323 | else |
| 324 | command = ALAUDA_GET_SM_MEDIA_STATUS; |
| 325 | |
| 326 | rc = usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe, |
| 327 | command, 0xc0, 0, 1, data, 2); |
| 328 | |
| 329 | US_DEBUGP("alauda_get_media_status: Media status %02X %02X\n", |
| 330 | data[0], data[1]); |
| 331 | |
| 332 | return rc; |
| 333 | } |
| 334 | |
| 335 | /* |
| 336 | * Clears the "media was changed" bit so that we know when it changes again |
| 337 | * in the future. |
| 338 | */ |
| 339 | static int alauda_ack_media(struct us_data *us) |
| 340 | { |
| 341 | unsigned char command; |
| 342 | |
| 343 | if (MEDIA_PORT(us) == ALAUDA_PORT_XD) |
| 344 | command = ALAUDA_ACK_XD_MEDIA_CHANGE; |
| 345 | else |
| 346 | command = ALAUDA_ACK_SM_MEDIA_CHANGE; |
| 347 | |
| 348 | return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe, |
| 349 | command, 0x40, 0, 1, NULL, 0); |
| 350 | } |
| 351 | |
| 352 | /* |
| 353 | * Retrieves a 4-byte media signature, which indicates manufacturer, capacity, |
| 354 | * and some other details. |
| 355 | */ |
| 356 | static int alauda_get_media_signature(struct us_data *us, unsigned char *data) |
| 357 | { |
| 358 | unsigned char command; |
| 359 | |
| 360 | if (MEDIA_PORT(us) == ALAUDA_PORT_XD) |
| 361 | command = ALAUDA_GET_XD_MEDIA_SIG; |
| 362 | else |
| 363 | command = ALAUDA_GET_SM_MEDIA_SIG; |
| 364 | |
| 365 | return usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe, |
| 366 | command, 0xc0, 0, 0, data, 4); |
| 367 | } |
| 368 | |
| 369 | /* |
| 370 | * Resets the media status (but not the whole device?) |
| 371 | */ |
| 372 | static int alauda_reset_media(struct us_data *us) |
| 373 | { |
| 374 | unsigned char *command = us->iobuf; |
| 375 | |
| 376 | memset(command, 0, 9); |
| 377 | command[0] = ALAUDA_BULK_CMD; |
| 378 | command[1] = ALAUDA_BULK_RESET_MEDIA; |
| 379 | command[8] = MEDIA_PORT(us); |
| 380 | |
| 381 | return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, |
| 382 | command, 9, NULL); |
| 383 | } |
| 384 | |
| 385 | /* |
| 386 | * Examines the media and deduces capacity, etc. |
| 387 | */ |
| 388 | static int alauda_init_media(struct us_data *us) |
| 389 | { |
| 390 | unsigned char *data = us->iobuf; |
| 391 | int ready = 0; |
| 392 | struct alauda_card_info *media_info; |
| 393 | unsigned int num_zones; |
| 394 | |
| 395 | while (ready == 0) { |
| 396 | msleep(20); |
| 397 | |
| 398 | if (alauda_get_media_status(us, data) != USB_STOR_XFER_GOOD) |
| 399 | return USB_STOR_TRANSPORT_ERROR; |
| 400 | |
| 401 | if (data[0] & 0x10) |
| 402 | ready = 1; |
| 403 | } |
| 404 | |
| 405 | US_DEBUGP("alauda_init_media: We are ready for action!\n"); |
| 406 | |
| 407 | if (alauda_ack_media(us) != USB_STOR_XFER_GOOD) |
| 408 | return USB_STOR_TRANSPORT_ERROR; |
| 409 | |
| 410 | msleep(10); |
| 411 | |
| 412 | if (alauda_get_media_status(us, data) != USB_STOR_XFER_GOOD) |
| 413 | return USB_STOR_TRANSPORT_ERROR; |
| 414 | |
| 415 | if (data[0] != 0x14) { |
| 416 | US_DEBUGP("alauda_init_media: Media not ready after ack\n"); |
| 417 | return USB_STOR_TRANSPORT_ERROR; |
| 418 | } |
| 419 | |
| 420 | if (alauda_get_media_signature(us, data) != USB_STOR_XFER_GOOD) |
| 421 | return USB_STOR_TRANSPORT_ERROR; |
| 422 | |
| 423 | US_DEBUGP("alauda_init_media: Media signature: %02X %02X %02X %02X\n", |
| 424 | data[0], data[1], data[2], data[3]); |
| 425 | media_info = alauda_card_find_id(data[1]); |
| 426 | if (media_info == NULL) { |
Frank Seidel | 6f8aa65 | 2009-02-05 16:16:24 +0100 | [diff] [blame] | 427 | printk(KERN_WARNING |
| 428 | "alauda_init_media: Unrecognised media signature: " |
Matthew Dharm | e80b0fa | 2005-12-04 22:02:44 -0800 | [diff] [blame] | 429 | "%02X %02X %02X %02X\n", |
| 430 | data[0], data[1], data[2], data[3]); |
| 431 | return USB_STOR_TRANSPORT_ERROR; |
| 432 | } |
| 433 | |
| 434 | MEDIA_INFO(us).capacity = 1 << media_info->chipshift; |
| 435 | US_DEBUGP("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 | * Examines the media status and does the right thing when the media has gone, |
| 462 | * appeared, or changed. |
| 463 | */ |
| 464 | static 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 | /* Check for no media or door open */ |
| 473 | if ((status[0] & 0x80) || ((status[0] & 0x1F) == 0x10) |
| 474 | || ((status[1] & 0x01) == 0)) { |
| 475 | US_DEBUGP("alauda_check_media: 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 | /* Check for media change */ |
| 484 | if (status[0] & 0x08) { |
| 485 | US_DEBUGP("alauda_check_media: 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 | * Checks the status from the 2nd status register |
| 500 | * Returns 3 bytes of status data, only the first is known |
| 501 | */ |
| 502 | static 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 | US_DEBUGP("alauda_check_status2: %02X %02X %02X\n", data[0], data[1], data[2]); |
| 522 | if (data[0] & ALAUDA_STATUS_ERROR) |
| 523 | return USB_STOR_XFER_ERROR; |
| 524 | |
| 525 | return USB_STOR_XFER_GOOD; |
| 526 | } |
| 527 | |
| 528 | /* |
| 529 | * Gets the redundancy data for the first page of a PBA |
| 530 | * Returns 16 bytes. |
| 531 | */ |
| 532 | static 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 | * Finds the first unused PBA in a zone |
| 551 | * Returns the absolute PBA of an unused PBA, or 0 if none found. |
| 552 | */ |
| 553 | static 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 | * Reads the redundancy data for all PBA's in a zone |
| 568 | * Produces lba <--> pba mappings |
| 569 | */ |
| 570 | static 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 | US_DEBUGP("alauda_read_map: Mapping blocks for zone %d\n", zone); |
| 588 | |
| 589 | /* 1024 PBA's per zone */ |
| 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 | /* special PBAs have control field 0^16 */ |
| 603 | for (j = 0; j < 16; j++) |
| 604 | if (data[j] != 0) |
| 605 | goto nonz; |
| 606 | pba_to_lba[i] = UNUSABLE; |
| 607 | US_DEBUGP("alauda_read_map: PBA %d has no logical mapping\n", blocknum); |
| 608 | continue; |
| 609 | |
| 610 | nonz: |
| 611 | /* unwritten PBAs have control field FF^16 */ |
| 612 | for (j = 0; j < 16; j++) |
| 613 | if (data[j] != 0xff) |
| 614 | goto nonff; |
| 615 | continue; |
| 616 | |
| 617 | nonff: |
| 618 | /* normal PBAs start with six FFs */ |
| 619 | if (j < 6) { |
| 620 | US_DEBUGP("alauda_read_map: PBA %d has no logical mapping: " |
| 621 | "reserved area = %02X%02X%02X%02X " |
| 622 | "data status %02X block status %02X\n", |
| 623 | blocknum, data[0], data[1], data[2], data[3], |
| 624 | data[4], data[5]); |
| 625 | pba_to_lba[i] = UNUSABLE; |
| 626 | continue; |
| 627 | } |
| 628 | |
| 629 | if ((data[6] >> 4) != 0x01) { |
| 630 | US_DEBUGP("alauda_read_map: PBA %d has invalid address " |
| 631 | "field %02X%02X/%02X%02X\n", |
| 632 | blocknum, data[6], data[7], data[11], data[12]); |
| 633 | pba_to_lba[i] = UNUSABLE; |
| 634 | continue; |
| 635 | } |
| 636 | |
| 637 | /* check even parity */ |
| 638 | if (parity[data[6] ^ data[7]]) { |
Frank Seidel | 6f8aa65 | 2009-02-05 16:16:24 +0100 | [diff] [blame] | 639 | printk(KERN_WARNING |
| 640 | "alauda_read_map: Bad parity in LBA for block %d" |
Matthew Dharm | e80b0fa | 2005-12-04 22:02:44 -0800 | [diff] [blame] | 641 | " (%02X %02X)\n", i, data[6], data[7]); |
| 642 | pba_to_lba[i] = UNUSABLE; |
| 643 | continue; |
| 644 | } |
| 645 | |
| 646 | lba_offset = short_pack(data[7], data[6]); |
| 647 | lba_offset = (lba_offset & 0x07FF) >> 1; |
| 648 | lba_real = lba_offset + zone_base_lba; |
| 649 | |
| 650 | /* |
| 651 | * Every 1024 physical blocks ("zone"), the LBA numbers |
| 652 | * go back to zero, but are within a higher block of LBA's. |
| 653 | * Also, there is a maximum of 1000 LBA's per zone. |
| 654 | * In other words, in PBA 1024-2047 you will find LBA 0-999 |
| 655 | * which are really LBA 1000-1999. This allows for 24 bad |
| 656 | * or special physical blocks per zone. |
| 657 | */ |
| 658 | |
| 659 | if (lba_offset >= uzonesize) { |
Frank Seidel | 6f8aa65 | 2009-02-05 16:16:24 +0100 | [diff] [blame] | 660 | printk(KERN_WARNING |
| 661 | "alauda_read_map: Bad low LBA %d for block %d\n", |
Matthew Dharm | e80b0fa | 2005-12-04 22:02:44 -0800 | [diff] [blame] | 662 | lba_real, blocknum); |
| 663 | continue; |
| 664 | } |
| 665 | |
| 666 | if (lba_to_pba[lba_offset] != UNDEF) { |
Frank Seidel | 6f8aa65 | 2009-02-05 16:16:24 +0100 | [diff] [blame] | 667 | printk(KERN_WARNING |
| 668 | "alauda_read_map: " |
| 669 | "LBA %d seen for PBA %d and %d\n", |
Matthew Dharm | e80b0fa | 2005-12-04 22:02:44 -0800 | [diff] [blame] | 670 | lba_real, lba_to_pba[lba_offset], blocknum); |
| 671 | continue; |
| 672 | } |
| 673 | |
| 674 | pba_to_lba[i] = lba_real; |
| 675 | lba_to_pba[lba_offset] = blocknum; |
| 676 | continue; |
| 677 | } |
| 678 | |
| 679 | MEDIA_INFO(us).lba_to_pba[zone] = lba_to_pba; |
| 680 | MEDIA_INFO(us).pba_to_lba[zone] = pba_to_lba; |
| 681 | result = 0; |
| 682 | goto out; |
| 683 | |
| 684 | error: |
| 685 | kfree(lba_to_pba); |
| 686 | kfree(pba_to_lba); |
| 687 | out: |
| 688 | return result; |
| 689 | } |
| 690 | |
| 691 | /* |
| 692 | * Checks to see whether we have already mapped a certain zone |
| 693 | * If we haven't, the map is generated |
| 694 | */ |
| 695 | static void alauda_ensure_map_for_zone(struct us_data *us, unsigned int zone) |
| 696 | { |
| 697 | if (MEDIA_INFO(us).lba_to_pba[zone] == NULL |
| 698 | || MEDIA_INFO(us).pba_to_lba[zone] == NULL) |
| 699 | alauda_read_map(us, zone); |
| 700 | } |
| 701 | |
| 702 | /* |
| 703 | * Erases an entire block |
| 704 | */ |
| 705 | static int alauda_erase_block(struct us_data *us, u16 pba) |
| 706 | { |
| 707 | int rc; |
| 708 | unsigned char command[] = { |
| 709 | ALAUDA_BULK_CMD, ALAUDA_BULK_ERASE_BLOCK, PBA_HI(pba), |
| 710 | PBA_ZONE(pba), 0, PBA_LO(pba), 0x02, 0, MEDIA_PORT(us) |
| 711 | }; |
| 712 | unsigned char buf[2]; |
| 713 | |
| 714 | US_DEBUGP("alauda_erase_block: Erasing PBA %d\n", pba); |
| 715 | |
| 716 | rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, |
| 717 | command, 9, NULL); |
| 718 | if (rc != USB_STOR_XFER_GOOD) |
| 719 | return rc; |
| 720 | |
| 721 | rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, |
| 722 | buf, 2, NULL); |
| 723 | if (rc != USB_STOR_XFER_GOOD) |
| 724 | return rc; |
| 725 | |
| 726 | US_DEBUGP("alauda_erase_block: Erase result: %02X %02X\n", |
| 727 | buf[0], buf[1]); |
| 728 | return rc; |
| 729 | } |
| 730 | |
| 731 | /* |
| 732 | * Reads data from a certain offset page inside a PBA, including interleaved |
| 733 | * redundancy data. Returns (pagesize+64)*pages bytes in data. |
| 734 | */ |
| 735 | static int alauda_read_block_raw(struct us_data *us, u16 pba, |
| 736 | unsigned int page, unsigned int pages, unsigned char *data) |
| 737 | { |
| 738 | int rc; |
| 739 | unsigned char command[] = { |
| 740 | ALAUDA_BULK_CMD, ALAUDA_BULK_READ_BLOCK, PBA_HI(pba), |
| 741 | PBA_ZONE(pba), 0, PBA_LO(pba) + page, pages, 0, MEDIA_PORT(us) |
| 742 | }; |
| 743 | |
| 744 | US_DEBUGP("alauda_read_block: pba %d page %d count %d\n", |
| 745 | pba, page, pages); |
| 746 | |
| 747 | rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, |
| 748 | command, 9, NULL); |
| 749 | if (rc != USB_STOR_XFER_GOOD) |
| 750 | return rc; |
| 751 | |
| 752 | return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, |
| 753 | data, (MEDIA_INFO(us).pagesize + 64) * pages, NULL); |
| 754 | } |
| 755 | |
| 756 | /* |
| 757 | * Reads data from a certain offset page inside a PBA, excluding redundancy |
| 758 | * data. Returns pagesize*pages bytes in data. Note that data must be big enough |
| 759 | * to hold (pagesize+64)*pages bytes of data, but you can ignore those 'extra' |
| 760 | * trailing bytes outside this function. |
| 761 | */ |
| 762 | static int alauda_read_block(struct us_data *us, u16 pba, |
| 763 | unsigned int page, unsigned int pages, unsigned char *data) |
| 764 | { |
| 765 | int i, rc; |
| 766 | unsigned int pagesize = MEDIA_INFO(us).pagesize; |
| 767 | |
| 768 | rc = alauda_read_block_raw(us, pba, page, pages, data); |
| 769 | if (rc != USB_STOR_XFER_GOOD) |
| 770 | return rc; |
| 771 | |
| 772 | /* Cut out the redundancy data */ |
| 773 | for (i = 0; i < pages; i++) { |
| 774 | int dest_offset = i * pagesize; |
| 775 | int src_offset = i * (pagesize + 64); |
| 776 | memmove(data + dest_offset, data + src_offset, pagesize); |
| 777 | } |
| 778 | |
| 779 | return rc; |
| 780 | } |
| 781 | |
| 782 | /* |
| 783 | * Writes an entire block of data and checks status after write. |
| 784 | * Redundancy data must be already included in data. Data should be |
| 785 | * (pagesize+64)*blocksize bytes in length. |
| 786 | */ |
| 787 | static int alauda_write_block(struct us_data *us, u16 pba, unsigned char *data) |
| 788 | { |
| 789 | int rc; |
| 790 | struct alauda_info *info = (struct alauda_info *) us->extra; |
| 791 | unsigned char command[] = { |
| 792 | ALAUDA_BULK_CMD, ALAUDA_BULK_WRITE_BLOCK, PBA_HI(pba), |
| 793 | PBA_ZONE(pba), 0, PBA_LO(pba), 32, 0, MEDIA_PORT(us) |
| 794 | }; |
| 795 | |
| 796 | US_DEBUGP("alauda_write_block: pba %d\n", pba); |
| 797 | |
| 798 | rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, |
| 799 | command, 9, NULL); |
| 800 | if (rc != USB_STOR_XFER_GOOD) |
| 801 | return rc; |
| 802 | |
| 803 | rc = usb_stor_bulk_transfer_buf(us, info->wr_ep, data, |
| 804 | (MEDIA_INFO(us).pagesize + 64) * MEDIA_INFO(us).blocksize, |
| 805 | NULL); |
| 806 | if (rc != USB_STOR_XFER_GOOD) |
| 807 | return rc; |
| 808 | |
| 809 | return alauda_check_status2(us); |
| 810 | } |
| 811 | |
| 812 | /* |
| 813 | * Write some data to a specific LBA. |
| 814 | */ |
| 815 | static int alauda_write_lba(struct us_data *us, u16 lba, |
| 816 | unsigned int page, unsigned int pages, |
| 817 | unsigned char *ptr, unsigned char *blockbuffer) |
| 818 | { |
| 819 | u16 pba, lbap, new_pba; |
| 820 | unsigned char *bptr, *cptr, *xptr; |
| 821 | unsigned char ecc[3]; |
| 822 | int i, result; |
| 823 | unsigned int uzonesize = MEDIA_INFO(us).uzonesize; |
| 824 | unsigned int zonesize = MEDIA_INFO(us).zonesize; |
| 825 | unsigned int pagesize = MEDIA_INFO(us).pagesize; |
| 826 | unsigned int blocksize = MEDIA_INFO(us).blocksize; |
| 827 | unsigned int lba_offset = lba % uzonesize; |
| 828 | unsigned int new_pba_offset; |
| 829 | unsigned int zone = lba / uzonesize; |
| 830 | |
| 831 | alauda_ensure_map_for_zone(us, zone); |
| 832 | |
| 833 | pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset]; |
| 834 | if (pba == 1) { |
| 835 | /* Maybe it is impossible to write to PBA 1. |
| 836 | Fake success, but don't do anything. */ |
Frank Seidel | 6f8aa65 | 2009-02-05 16:16:24 +0100 | [diff] [blame] | 837 | printk(KERN_WARNING |
| 838 | "alauda_write_lba: avoid writing to pba 1\n"); |
Matthew Dharm | e80b0fa | 2005-12-04 22:02:44 -0800 | [diff] [blame] | 839 | return USB_STOR_TRANSPORT_GOOD; |
| 840 | } |
| 841 | |
| 842 | new_pba = alauda_find_unused_pba(&MEDIA_INFO(us), zone); |
| 843 | if (!new_pba) { |
Frank Seidel | 6f8aa65 | 2009-02-05 16:16:24 +0100 | [diff] [blame] | 844 | printk(KERN_WARNING |
| 845 | "alauda_write_lba: Out of unused blocks\n"); |
Matthew Dharm | e80b0fa | 2005-12-04 22:02:44 -0800 | [diff] [blame] | 846 | return USB_STOR_TRANSPORT_ERROR; |
| 847 | } |
| 848 | |
| 849 | /* read old contents */ |
| 850 | if (pba != UNDEF) { |
| 851 | result = alauda_read_block_raw(us, pba, 0, |
| 852 | blocksize, blockbuffer); |
| 853 | if (result != USB_STOR_XFER_GOOD) |
| 854 | return result; |
| 855 | } else { |
| 856 | memset(blockbuffer, 0, blocksize * (pagesize + 64)); |
| 857 | } |
| 858 | |
| 859 | lbap = (lba_offset << 1) | 0x1000; |
| 860 | if (parity[MSB_of(lbap) ^ LSB_of(lbap)]) |
| 861 | lbap ^= 1; |
| 862 | |
| 863 | /* check old contents and fill lba */ |
| 864 | for (i = 0; i < blocksize; i++) { |
| 865 | bptr = blockbuffer + (i * (pagesize + 64)); |
| 866 | cptr = bptr + pagesize; |
| 867 | nand_compute_ecc(bptr, ecc); |
| 868 | if (!nand_compare_ecc(cptr+13, ecc)) { |
| 869 | US_DEBUGP("Warning: bad ecc in page %d- of pba %d\n", |
| 870 | i, pba); |
| 871 | nand_store_ecc(cptr+13, ecc); |
| 872 | } |
| 873 | nand_compute_ecc(bptr + (pagesize / 2), ecc); |
| 874 | if (!nand_compare_ecc(cptr+8, ecc)) { |
| 875 | US_DEBUGP("Warning: bad ecc in page %d+ of pba %d\n", |
| 876 | i, pba); |
| 877 | nand_store_ecc(cptr+8, ecc); |
| 878 | } |
| 879 | cptr[6] = cptr[11] = MSB_of(lbap); |
| 880 | cptr[7] = cptr[12] = LSB_of(lbap); |
| 881 | } |
| 882 | |
| 883 | /* copy in new stuff and compute ECC */ |
| 884 | xptr = ptr; |
| 885 | for (i = page; i < page+pages; i++) { |
| 886 | bptr = blockbuffer + (i * (pagesize + 64)); |
| 887 | cptr = bptr + pagesize; |
| 888 | memcpy(bptr, xptr, pagesize); |
| 889 | xptr += pagesize; |
| 890 | nand_compute_ecc(bptr, ecc); |
| 891 | nand_store_ecc(cptr+13, ecc); |
| 892 | nand_compute_ecc(bptr + (pagesize / 2), ecc); |
| 893 | nand_store_ecc(cptr+8, ecc); |
| 894 | } |
| 895 | |
| 896 | result = alauda_write_block(us, new_pba, blockbuffer); |
| 897 | if (result != USB_STOR_XFER_GOOD) |
| 898 | return result; |
| 899 | |
| 900 | new_pba_offset = new_pba - (zone * zonesize); |
| 901 | MEDIA_INFO(us).pba_to_lba[zone][new_pba_offset] = lba; |
| 902 | MEDIA_INFO(us).lba_to_pba[zone][lba_offset] = new_pba; |
| 903 | US_DEBUGP("alauda_write_lba: Remapped LBA %d to PBA %d\n", |
| 904 | lba, new_pba); |
| 905 | |
| 906 | if (pba != UNDEF) { |
| 907 | unsigned int pba_offset = pba - (zone * zonesize); |
| 908 | result = alauda_erase_block(us, pba); |
| 909 | if (result != USB_STOR_XFER_GOOD) |
| 910 | return result; |
| 911 | MEDIA_INFO(us).pba_to_lba[zone][pba_offset] = UNDEF; |
| 912 | } |
| 913 | |
| 914 | return USB_STOR_TRANSPORT_GOOD; |
| 915 | } |
| 916 | |
| 917 | /* |
| 918 | * Read data from a specific sector address |
| 919 | */ |
| 920 | static int alauda_read_data(struct us_data *us, unsigned long address, |
| 921 | unsigned int sectors) |
| 922 | { |
| 923 | unsigned char *buffer; |
| 924 | u16 lba, max_lba; |
Jens Axboe | 1f6f31a | 2007-05-11 12:33:09 +0200 | [diff] [blame] | 925 | unsigned int page, len, offset; |
Matthew Dharm | e80b0fa | 2005-12-04 22:02:44 -0800 | [diff] [blame] | 926 | unsigned int blockshift = MEDIA_INFO(us).blockshift; |
| 927 | unsigned int pageshift = MEDIA_INFO(us).pageshift; |
| 928 | unsigned int blocksize = MEDIA_INFO(us).blocksize; |
| 929 | unsigned int pagesize = MEDIA_INFO(us).pagesize; |
| 930 | unsigned int uzonesize = MEDIA_INFO(us).uzonesize; |
Jens Axboe | 1f6f31a | 2007-05-11 12:33:09 +0200 | [diff] [blame] | 931 | struct scatterlist *sg; |
Matthew Dharm | e80b0fa | 2005-12-04 22:02:44 -0800 | [diff] [blame] | 932 | int result; |
| 933 | |
| 934 | /* |
| 935 | * Since we only read in one block at a time, we have to create |
| 936 | * a bounce buffer and move the data a piece at a time between the |
| 937 | * bounce buffer and the actual transfer buffer. |
| 938 | * We make this buffer big enough to hold temporary redundancy data, |
| 939 | * which we use when reading the data blocks. |
| 940 | */ |
| 941 | |
| 942 | len = min(sectors, blocksize) * (pagesize + 64); |
| 943 | buffer = kmalloc(len, GFP_NOIO); |
| 944 | if (buffer == NULL) { |
Frank Seidel | 6f8aa65 | 2009-02-05 16:16:24 +0100 | [diff] [blame] | 945 | printk(KERN_WARNING "alauda_read_data: Out of memory\n"); |
Matthew Dharm | e80b0fa | 2005-12-04 22:02:44 -0800 | [diff] [blame] | 946 | return USB_STOR_TRANSPORT_ERROR; |
| 947 | } |
| 948 | |
| 949 | /* Figure out the initial LBA and page */ |
| 950 | lba = address >> blockshift; |
| 951 | page = (address & MEDIA_INFO(us).blockmask); |
| 952 | max_lba = MEDIA_INFO(us).capacity >> (blockshift + pageshift); |
| 953 | |
| 954 | result = USB_STOR_TRANSPORT_GOOD; |
Jens Axboe | 1f6f31a | 2007-05-11 12:33:09 +0200 | [diff] [blame] | 955 | offset = 0; |
| 956 | sg = NULL; |
Matthew Dharm | e80b0fa | 2005-12-04 22:02:44 -0800 | [diff] [blame] | 957 | |
| 958 | while (sectors > 0) { |
| 959 | unsigned int zone = lba / uzonesize; /* integer division */ |
| 960 | unsigned int lba_offset = lba - (zone * uzonesize); |
| 961 | unsigned int pages; |
| 962 | u16 pba; |
| 963 | alauda_ensure_map_for_zone(us, zone); |
| 964 | |
| 965 | /* Not overflowing capacity? */ |
| 966 | if (lba >= max_lba) { |
| 967 | US_DEBUGP("Error: Requested lba %u exceeds " |
| 968 | "maximum %u\n", lba, max_lba); |
| 969 | result = USB_STOR_TRANSPORT_ERROR; |
| 970 | break; |
| 971 | } |
| 972 | |
| 973 | /* Find number of pages we can read in this block */ |
| 974 | pages = min(sectors, blocksize - page); |
| 975 | len = pages << pageshift; |
| 976 | |
| 977 | /* Find where this lba lives on disk */ |
| 978 | pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset]; |
| 979 | |
| 980 | if (pba == UNDEF) { /* this lba was never written */ |
| 981 | US_DEBUGP("Read %d zero pages (LBA %d) page %d\n", |
| 982 | pages, lba, page); |
| 983 | |
| 984 | /* This is not really an error. It just means |
| 985 | that the block has never been written. |
| 986 | Instead of returning USB_STOR_TRANSPORT_ERROR |
| 987 | it is better to return all zero data. */ |
| 988 | |
| 989 | memset(buffer, 0, len); |
| 990 | } else { |
| 991 | US_DEBUGP("Read %d pages, from PBA %d" |
| 992 | " (LBA %d) page %d\n", |
| 993 | pages, pba, lba, page); |
| 994 | |
| 995 | result = alauda_read_block(us, pba, page, pages, buffer); |
| 996 | if (result != USB_STOR_TRANSPORT_GOOD) |
| 997 | break; |
| 998 | } |
| 999 | |
| 1000 | /* Store the data in the transfer buffer */ |
| 1001 | usb_stor_access_xfer_buf(buffer, len, us->srb, |
Jens Axboe | 1f6f31a | 2007-05-11 12:33:09 +0200 | [diff] [blame] | 1002 | &sg, &offset, TO_XFER_BUF); |
Matthew Dharm | e80b0fa | 2005-12-04 22:02:44 -0800 | [diff] [blame] | 1003 | |
| 1004 | page = 0; |
| 1005 | lba++; |
| 1006 | sectors -= pages; |
| 1007 | } |
| 1008 | |
| 1009 | kfree(buffer); |
| 1010 | return result; |
| 1011 | } |
| 1012 | |
| 1013 | /* |
| 1014 | * Write data to a specific sector address |
| 1015 | */ |
| 1016 | static int alauda_write_data(struct us_data *us, unsigned long address, |
| 1017 | unsigned int sectors) |
| 1018 | { |
| 1019 | unsigned char *buffer, *blockbuffer; |
Jens Axboe | 1f6f31a | 2007-05-11 12:33:09 +0200 | [diff] [blame] | 1020 | unsigned int page, len, offset; |
Matthew Dharm | e80b0fa | 2005-12-04 22:02:44 -0800 | [diff] [blame] | 1021 | unsigned int blockshift = MEDIA_INFO(us).blockshift; |
| 1022 | unsigned int pageshift = MEDIA_INFO(us).pageshift; |
| 1023 | unsigned int blocksize = MEDIA_INFO(us).blocksize; |
| 1024 | unsigned int pagesize = MEDIA_INFO(us).pagesize; |
Jens Axboe | 1f6f31a | 2007-05-11 12:33:09 +0200 | [diff] [blame] | 1025 | struct scatterlist *sg; |
Matthew Dharm | e80b0fa | 2005-12-04 22:02:44 -0800 | [diff] [blame] | 1026 | u16 lba, max_lba; |
| 1027 | int result; |
| 1028 | |
| 1029 | /* |
| 1030 | * Since we don't write the user data directly to the device, |
| 1031 | * we have to create a bounce buffer and move the data a piece |
| 1032 | * at a time between the bounce buffer and the actual transfer buffer. |
| 1033 | */ |
| 1034 | |
| 1035 | len = min(sectors, blocksize) * pagesize; |
| 1036 | buffer = kmalloc(len, GFP_NOIO); |
| 1037 | if (buffer == NULL) { |
Frank Seidel | 6f8aa65 | 2009-02-05 16:16:24 +0100 | [diff] [blame] | 1038 | printk(KERN_WARNING "alauda_write_data: Out of memory\n"); |
Matthew Dharm | e80b0fa | 2005-12-04 22:02:44 -0800 | [diff] [blame] | 1039 | return USB_STOR_TRANSPORT_ERROR; |
| 1040 | } |
| 1041 | |
| 1042 | /* |
| 1043 | * We also need a temporary block buffer, where we read in the old data, |
| 1044 | * overwrite parts with the new data, and manipulate the redundancy data |
| 1045 | */ |
| 1046 | blockbuffer = kmalloc((pagesize + 64) * blocksize, GFP_NOIO); |
| 1047 | if (blockbuffer == NULL) { |
Frank Seidel | 6f8aa65 | 2009-02-05 16:16:24 +0100 | [diff] [blame] | 1048 | printk(KERN_WARNING "alauda_write_data: Out of memory\n"); |
Matthew Dharm | e80b0fa | 2005-12-04 22:02:44 -0800 | [diff] [blame] | 1049 | kfree(buffer); |
| 1050 | return USB_STOR_TRANSPORT_ERROR; |
| 1051 | } |
| 1052 | |
| 1053 | /* Figure out the initial LBA and page */ |
| 1054 | lba = address >> blockshift; |
| 1055 | page = (address & MEDIA_INFO(us).blockmask); |
| 1056 | max_lba = MEDIA_INFO(us).capacity >> (pageshift + blockshift); |
| 1057 | |
| 1058 | result = USB_STOR_TRANSPORT_GOOD; |
Jens Axboe | 1f6f31a | 2007-05-11 12:33:09 +0200 | [diff] [blame] | 1059 | offset = 0; |
| 1060 | sg = NULL; |
Matthew Dharm | e80b0fa | 2005-12-04 22:02:44 -0800 | [diff] [blame] | 1061 | |
| 1062 | while (sectors > 0) { |
| 1063 | /* Write as many sectors as possible in this block */ |
| 1064 | unsigned int pages = min(sectors, blocksize - page); |
| 1065 | len = pages << pageshift; |
| 1066 | |
| 1067 | /* Not overflowing capacity? */ |
| 1068 | if (lba >= max_lba) { |
| 1069 | US_DEBUGP("alauda_write_data: Requested lba %u exceeds " |
| 1070 | "maximum %u\n", lba, max_lba); |
| 1071 | result = USB_STOR_TRANSPORT_ERROR; |
| 1072 | break; |
| 1073 | } |
| 1074 | |
| 1075 | /* Get the data from the transfer buffer */ |
| 1076 | usb_stor_access_xfer_buf(buffer, len, us->srb, |
Jens Axboe | 1f6f31a | 2007-05-11 12:33:09 +0200 | [diff] [blame] | 1077 | &sg, &offset, FROM_XFER_BUF); |
Matthew Dharm | e80b0fa | 2005-12-04 22:02:44 -0800 | [diff] [blame] | 1078 | |
| 1079 | result = alauda_write_lba(us, lba, page, pages, buffer, |
| 1080 | blockbuffer); |
| 1081 | if (result != USB_STOR_TRANSPORT_GOOD) |
| 1082 | break; |
| 1083 | |
| 1084 | page = 0; |
| 1085 | lba++; |
| 1086 | sectors -= pages; |
| 1087 | } |
| 1088 | |
| 1089 | kfree(buffer); |
| 1090 | kfree(blockbuffer); |
| 1091 | return result; |
| 1092 | } |
| 1093 | |
| 1094 | /* |
| 1095 | * Our interface with the rest of the world |
| 1096 | */ |
| 1097 | |
| 1098 | static void alauda_info_destructor(void *extra) |
| 1099 | { |
| 1100 | struct alauda_info *info = (struct alauda_info *) extra; |
| 1101 | int port; |
| 1102 | |
| 1103 | if (!info) |
| 1104 | return; |
| 1105 | |
| 1106 | for (port = 0; port < 2; port++) { |
| 1107 | struct alauda_media_info *media_info = &info->port[port]; |
| 1108 | |
| 1109 | alauda_free_maps(media_info); |
| 1110 | kfree(media_info->lba_to_pba); |
| 1111 | kfree(media_info->pba_to_lba); |
| 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | /* |
| 1116 | * Initialize alauda_info struct and find the data-write endpoint |
| 1117 | */ |
Alan Stern | a74bba3 | 2009-02-12 14:48:22 -0500 | [diff] [blame] | 1118 | static int init_alauda(struct us_data *us) |
Matthew Dharm | e80b0fa | 2005-12-04 22:02:44 -0800 | [diff] [blame] | 1119 | { |
| 1120 | struct alauda_info *info; |
| 1121 | struct usb_host_interface *altsetting = us->pusb_intf->cur_altsetting; |
| 1122 | nand_init_ecc(); |
| 1123 | |
| 1124 | us->extra = kzalloc(sizeof(struct alauda_info), GFP_NOIO); |
| 1125 | if (!us->extra) { |
| 1126 | US_DEBUGP("init_alauda: Gah! Can't allocate storage for" |
| 1127 | "alauda info struct!\n"); |
| 1128 | return USB_STOR_TRANSPORT_ERROR; |
| 1129 | } |
| 1130 | info = (struct alauda_info *) us->extra; |
| 1131 | us->extra_destructor = alauda_info_destructor; |
| 1132 | |
| 1133 | info->wr_ep = usb_sndbulkpipe(us->pusb_dev, |
| 1134 | altsetting->endpoint[0].desc.bEndpointAddress |
| 1135 | & USB_ENDPOINT_NUMBER_MASK); |
| 1136 | |
| 1137 | return USB_STOR_TRANSPORT_GOOD; |
| 1138 | } |
| 1139 | |
Alan Stern | a74bba3 | 2009-02-12 14:48:22 -0500 | [diff] [blame] | 1140 | static int alauda_transport(struct scsi_cmnd *srb, struct us_data *us) |
Matthew Dharm | e80b0fa | 2005-12-04 22:02:44 -0800 | [diff] [blame] | 1141 | { |
| 1142 | int rc; |
| 1143 | struct alauda_info *info = (struct alauda_info *) us->extra; |
| 1144 | unsigned char *ptr = us->iobuf; |
| 1145 | static unsigned char inquiry_response[36] = { |
| 1146 | 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00 |
| 1147 | }; |
| 1148 | |
| 1149 | if (srb->cmnd[0] == INQUIRY) { |
| 1150 | US_DEBUGP("alauda_transport: INQUIRY. " |
| 1151 | "Returning bogus response.\n"); |
| 1152 | memcpy(ptr, inquiry_response, sizeof(inquiry_response)); |
| 1153 | fill_inquiry_response(us, ptr, 36); |
| 1154 | return USB_STOR_TRANSPORT_GOOD; |
| 1155 | } |
| 1156 | |
| 1157 | if (srb->cmnd[0] == TEST_UNIT_READY) { |
| 1158 | US_DEBUGP("alauda_transport: TEST_UNIT_READY.\n"); |
| 1159 | return alauda_check_media(us); |
| 1160 | } |
| 1161 | |
| 1162 | if (srb->cmnd[0] == READ_CAPACITY) { |
| 1163 | unsigned int num_zones; |
| 1164 | unsigned long capacity; |
| 1165 | |
| 1166 | rc = alauda_check_media(us); |
| 1167 | if (rc != USB_STOR_TRANSPORT_GOOD) |
| 1168 | return rc; |
| 1169 | |
| 1170 | num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift |
| 1171 | + MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift); |
| 1172 | |
| 1173 | capacity = num_zones * MEDIA_INFO(us).uzonesize |
| 1174 | * MEDIA_INFO(us).blocksize; |
| 1175 | |
| 1176 | /* Report capacity and page size */ |
| 1177 | ((__be32 *) ptr)[0] = cpu_to_be32(capacity - 1); |
| 1178 | ((__be32 *) ptr)[1] = cpu_to_be32(512); |
| 1179 | |
| 1180 | usb_stor_set_xfer_buf(ptr, 8, srb); |
| 1181 | return USB_STOR_TRANSPORT_GOOD; |
| 1182 | } |
| 1183 | |
| 1184 | if (srb->cmnd[0] == READ_10) { |
| 1185 | unsigned int page, pages; |
| 1186 | |
| 1187 | rc = alauda_check_media(us); |
| 1188 | if (rc != USB_STOR_TRANSPORT_GOOD) |
| 1189 | return rc; |
| 1190 | |
| 1191 | page = short_pack(srb->cmnd[3], srb->cmnd[2]); |
| 1192 | page <<= 16; |
| 1193 | page |= short_pack(srb->cmnd[5], srb->cmnd[4]); |
| 1194 | pages = short_pack(srb->cmnd[8], srb->cmnd[7]); |
| 1195 | |
| 1196 | US_DEBUGP("alauda_transport: READ_10: page %d pagect %d\n", |
| 1197 | page, pages); |
| 1198 | |
| 1199 | return alauda_read_data(us, page, pages); |
| 1200 | } |
| 1201 | |
| 1202 | if (srb->cmnd[0] == WRITE_10) { |
| 1203 | unsigned int page, pages; |
| 1204 | |
| 1205 | rc = alauda_check_media(us); |
| 1206 | if (rc != USB_STOR_TRANSPORT_GOOD) |
| 1207 | return rc; |
| 1208 | |
| 1209 | page = short_pack(srb->cmnd[3], srb->cmnd[2]); |
| 1210 | page <<= 16; |
| 1211 | page |= short_pack(srb->cmnd[5], srb->cmnd[4]); |
| 1212 | pages = short_pack(srb->cmnd[8], srb->cmnd[7]); |
| 1213 | |
| 1214 | US_DEBUGP("alauda_transport: WRITE_10: page %d pagect %d\n", |
| 1215 | page, pages); |
| 1216 | |
| 1217 | return alauda_write_data(us, page, pages); |
| 1218 | } |
| 1219 | |
| 1220 | if (srb->cmnd[0] == REQUEST_SENSE) { |
| 1221 | US_DEBUGP("alauda_transport: REQUEST_SENSE.\n"); |
| 1222 | |
| 1223 | memset(ptr, 0, 18); |
| 1224 | ptr[0] = 0xF0; |
| 1225 | ptr[2] = info->sense_key; |
| 1226 | ptr[7] = 11; |
| 1227 | ptr[12] = info->sense_asc; |
| 1228 | ptr[13] = info->sense_ascq; |
| 1229 | usb_stor_set_xfer_buf(ptr, 18, srb); |
| 1230 | |
| 1231 | return USB_STOR_TRANSPORT_GOOD; |
| 1232 | } |
| 1233 | |
| 1234 | if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) { |
| 1235 | /* sure. whatever. not like we can stop the user from popping |
| 1236 | the media out of the device (no locking doors, etc) */ |
| 1237 | return USB_STOR_TRANSPORT_GOOD; |
| 1238 | } |
| 1239 | |
| 1240 | US_DEBUGP("alauda_transport: Gah! Unknown command: %d (0x%x)\n", |
| 1241 | srb->cmnd[0], srb->cmnd[0]); |
| 1242 | info->sense_key = 0x05; |
| 1243 | info->sense_asc = 0x20; |
| 1244 | info->sense_ascq = 0x00; |
| 1245 | return USB_STOR_TRANSPORT_FAILED; |
| 1246 | } |
| 1247 | |
Alan Stern | a74bba3 | 2009-02-12 14:48:22 -0500 | [diff] [blame] | 1248 | static int alauda_probe(struct usb_interface *intf, |
| 1249 | const struct usb_device_id *id) |
| 1250 | { |
| 1251 | struct us_data *us; |
| 1252 | int result; |
| 1253 | |
| 1254 | result = usb_stor_probe1(&us, intf, id, |
| 1255 | (id - alauda_usb_ids) + alauda_unusual_dev_list); |
| 1256 | if (result) |
| 1257 | return result; |
| 1258 | |
| 1259 | us->transport_name = "Alauda Control/Bulk"; |
| 1260 | us->transport = alauda_transport; |
| 1261 | us->transport_reset = usb_stor_Bulk_reset; |
| 1262 | us->max_lun = 1; |
| 1263 | |
| 1264 | result = usb_stor_probe2(us); |
| 1265 | return result; |
| 1266 | } |
| 1267 | |
| 1268 | static struct usb_driver alauda_driver = { |
| 1269 | .name = "ums-alauda", |
| 1270 | .probe = alauda_probe, |
| 1271 | .disconnect = usb_stor_disconnect, |
| 1272 | .suspend = usb_stor_suspend, |
| 1273 | .resume = usb_stor_resume, |
| 1274 | .reset_resume = usb_stor_reset_resume, |
| 1275 | .pre_reset = usb_stor_pre_reset, |
| 1276 | .post_reset = usb_stor_post_reset, |
| 1277 | .id_table = alauda_usb_ids, |
| 1278 | .soft_unbind = 1, |
Huajun Li | e73b2db | 2012-01-14 10:15:21 +0800 | [diff] [blame] | 1279 | .no_dynamic_id = 1, |
Alan Stern | a74bba3 | 2009-02-12 14:48:22 -0500 | [diff] [blame] | 1280 | }; |
| 1281 | |
Greg Kroah-Hartman | 65db430 | 2011-11-18 09:34:02 -0800 | [diff] [blame] | 1282 | module_usb_driver(alauda_driver); |