| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* Driver for SanDisk SDDR-55 SmartMedia reader | 
|  | 2 | * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * SDDR55 driver v0.1: | 
|  | 4 | * | 
|  | 5 | * First release | 
|  | 6 | * | 
|  | 7 | * Current development and maintenance by: | 
|  | 8 | *   (c) 2002 Simon Munton | 
|  | 9 | * | 
|  | 10 | * This program is free software; you can redistribute it and/or modify it | 
|  | 11 | * under the terms of the GNU General Public License as published by the | 
|  | 12 | * Free Software Foundation; either version 2, or (at your option) any | 
|  | 13 | * later version. | 
|  | 14 | * | 
|  | 15 | * This program is distributed in the hope that it will be useful, but | 
|  | 16 | * WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|  | 18 | * General Public License for more details. | 
|  | 19 | * | 
|  | 20 | * You should have received a copy of the GNU General Public License along | 
|  | 21 | * with this program; if not, write to the Free Software Foundation, Inc., | 
|  | 22 | * 675 Mass Ave, Cambridge, MA 02139, USA. | 
|  | 23 | */ | 
|  | 24 |  | 
|  | 25 | #include <linux/jiffies.h> | 
|  | 26 | #include <linux/errno.h> | 
| Alan Stern | 70fcc00 | 2009-02-12 14:47:59 -0500 | [diff] [blame] | 27 | #include <linux/module.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/slab.h> | 
|  | 29 |  | 
|  | 30 | #include <scsi/scsi.h> | 
|  | 31 | #include <scsi/scsi_cmnd.h> | 
|  | 32 |  | 
|  | 33 | #include "usb.h" | 
|  | 34 | #include "transport.h" | 
|  | 35 | #include "protocol.h" | 
|  | 36 | #include "debug.h" | 
| Alan Stern | 70fcc00 | 2009-02-12 14:47:59 -0500 | [diff] [blame] | 37 |  | 
| Maciej Grela | 4246b06 | 2009-02-28 12:39:20 -0800 | [diff] [blame] | 38 | MODULE_DESCRIPTION("Driver for SanDisk SDDR-55 SmartMedia reader"); | 
|  | 39 | MODULE_AUTHOR("Simon Munton"); | 
|  | 40 | MODULE_LICENSE("GPL"); | 
| Alan Stern | 70fcc00 | 2009-02-12 14:47:59 -0500 | [diff] [blame] | 41 |  | 
|  | 42 | /* | 
|  | 43 | * The table of devices | 
|  | 44 | */ | 
|  | 45 | #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \ | 
|  | 46 | vendorName, productName, useProtocol, useTransport, \ | 
|  | 47 | initFunction, flags) \ | 
|  | 48 | { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \ | 
|  | 49 | .driver_info = (flags)|(USB_US_TYPE_STOR<<24) } | 
|  | 50 |  | 
|  | 51 | struct usb_device_id sddr55_usb_ids[] = { | 
|  | 52 | #	include "unusual_sddr55.h" | 
|  | 53 | { }		/* Terminating entry */ | 
|  | 54 | }; | 
|  | 55 | MODULE_DEVICE_TABLE(usb, sddr55_usb_ids); | 
|  | 56 |  | 
|  | 57 | #undef UNUSUAL_DEV | 
|  | 58 |  | 
|  | 59 | /* | 
|  | 60 | * The flags table | 
|  | 61 | */ | 
|  | 62 | #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \ | 
|  | 63 | vendor_name, product_name, use_protocol, use_transport, \ | 
|  | 64 | init_function, Flags) \ | 
|  | 65 | { \ | 
|  | 66 | .vendorName = vendor_name,	\ | 
|  | 67 | .productName = product_name,	\ | 
|  | 68 | .useProtocol = use_protocol,	\ | 
|  | 69 | .useTransport = use_transport,	\ | 
|  | 70 | .initFunction = init_function,	\ | 
|  | 71 | } | 
|  | 72 |  | 
|  | 73 | static struct us_unusual_dev sddr55_unusual_dev_list[] = { | 
|  | 74 | #	include "unusual_sddr55.h" | 
|  | 75 | { }		/* Terminating entry */ | 
|  | 76 | }; | 
|  | 77 |  | 
|  | 78 | #undef UNUSUAL_DEV | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 |  | 
|  | 80 |  | 
|  | 81 | #define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) ) | 
|  | 82 | #define LSB_of(s) ((s)&0xFF) | 
|  | 83 | #define MSB_of(s) ((s)>>8) | 
|  | 84 | #define PAGESIZE  512 | 
|  | 85 |  | 
|  | 86 | #define set_sense_info(sk, asc, ascq)	\ | 
|  | 87 | do {				\ | 
|  | 88 | info->sense_data[2] = sk;	\ | 
|  | 89 | info->sense_data[12] = asc;	\ | 
|  | 90 | info->sense_data[13] = ascq;	\ | 
|  | 91 | } while (0) | 
|  | 92 |  | 
|  | 93 |  | 
|  | 94 | struct sddr55_card_info { | 
|  | 95 | unsigned long	capacity;	/* Size of card in bytes */ | 
|  | 96 | int		max_log_blks;	/* maximum number of logical blocks */ | 
|  | 97 | int		pageshift;	/* log2 of pagesize */ | 
|  | 98 | int		smallpageshift;	/* 1 if pagesize == 256 */ | 
|  | 99 | int		blocksize;	/* Size of block in pages */ | 
|  | 100 | int		blockshift;	/* log2 of blocksize */ | 
|  | 101 | int		blockmask;	/* 2^blockshift - 1 */ | 
|  | 102 | int		read_only;	/* non zero if card is write protected */ | 
|  | 103 | int		force_read_only;	/* non zero if we find a map error*/ | 
|  | 104 | int		*lba_to_pba;	/* logical to physical map */ | 
|  | 105 | int		*pba_to_lba;	/* physical to logical map */ | 
|  | 106 | int		fatal_error;	/* set if we detect something nasty */ | 
|  | 107 | unsigned long 	last_access;	/* number of jiffies since we last talked to device */ | 
|  | 108 | unsigned char   sense_data[18]; | 
|  | 109 | }; | 
|  | 110 |  | 
|  | 111 |  | 
|  | 112 | #define NOT_ALLOCATED		0xffffffff | 
|  | 113 | #define BAD_BLOCK		0xffff | 
|  | 114 | #define CIS_BLOCK		0x400 | 
|  | 115 | #define UNUSED_BLOCK		0x3ff | 
|  | 116 |  | 
|  | 117 | static int | 
|  | 118 | sddr55_bulk_transport(struct us_data *us, int direction, | 
|  | 119 | unsigned char *data, unsigned int len) { | 
|  | 120 | struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra; | 
|  | 121 | unsigned int pipe = (direction == DMA_FROM_DEVICE) ? | 
|  | 122 | us->recv_bulk_pipe : us->send_bulk_pipe; | 
|  | 123 |  | 
|  | 124 | if (!len) | 
|  | 125 | return USB_STOR_XFER_GOOD; | 
|  | 126 | info->last_access = jiffies; | 
|  | 127 | return usb_stor_bulk_transfer_buf(us, pipe, data, len, NULL); | 
|  | 128 | } | 
|  | 129 |  | 
|  | 130 | /* check if card inserted, if there is, update read_only status | 
|  | 131 | * return non zero if no card | 
|  | 132 | */ | 
|  | 133 |  | 
|  | 134 | static int sddr55_status(struct us_data *us) | 
|  | 135 | { | 
|  | 136 | int result; | 
|  | 137 | unsigned char *command = us->iobuf; | 
|  | 138 | unsigned char *status = us->iobuf; | 
|  | 139 | struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra; | 
|  | 140 |  | 
|  | 141 | /* send command */ | 
|  | 142 | memset(command, 0, 8); | 
|  | 143 | command[5] = 0xB0; | 
|  | 144 | command[7] = 0x80; | 
|  | 145 | result = sddr55_bulk_transport(us, | 
|  | 146 | DMA_TO_DEVICE, command, 8); | 
|  | 147 |  | 
|  | 148 | US_DEBUGP("Result for send_command in status %d\n", | 
|  | 149 | result); | 
|  | 150 |  | 
|  | 151 | if (result != USB_STOR_XFER_GOOD) { | 
|  | 152 | set_sense_info (4, 0, 0);	/* hardware error */ | 
|  | 153 | return USB_STOR_TRANSPORT_ERROR; | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | result = sddr55_bulk_transport(us, | 
|  | 157 | DMA_FROM_DEVICE, status,	4); | 
|  | 158 |  | 
|  | 159 | /* expect to get short transfer if no card fitted */ | 
|  | 160 | if (result == USB_STOR_XFER_SHORT || result == USB_STOR_XFER_STALLED) { | 
|  | 161 | /* had a short transfer, no card inserted, free map memory */ | 
| Jesper Juhl | 1bc3c9e | 2005-04-18 17:39:34 -0700 | [diff] [blame] | 162 | kfree(info->lba_to_pba); | 
|  | 163 | kfree(info->pba_to_lba); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | info->lba_to_pba = NULL; | 
|  | 165 | info->pba_to_lba = NULL; | 
|  | 166 |  | 
|  | 167 | info->fatal_error = 0; | 
|  | 168 | info->force_read_only = 0; | 
|  | 169 |  | 
|  | 170 | set_sense_info (2, 0x3a, 0);	/* not ready, medium not present */ | 
|  | 171 | return USB_STOR_TRANSPORT_FAILED; | 
|  | 172 | } | 
|  | 173 |  | 
|  | 174 | if (result != USB_STOR_XFER_GOOD) { | 
|  | 175 | set_sense_info (4, 0, 0);	/* hardware error */ | 
|  | 176 | return USB_STOR_TRANSPORT_FAILED; | 
|  | 177 | } | 
|  | 178 |  | 
|  | 179 | /* check write protect status */ | 
|  | 180 | info->read_only = (status[0] & 0x20); | 
|  | 181 |  | 
|  | 182 | /* now read status */ | 
|  | 183 | result = sddr55_bulk_transport(us, | 
|  | 184 | DMA_FROM_DEVICE, status,	2); | 
|  | 185 |  | 
|  | 186 | if (result != USB_STOR_XFER_GOOD) { | 
|  | 187 | set_sense_info (4, 0, 0);	/* hardware error */ | 
|  | 188 | } | 
|  | 189 |  | 
|  | 190 | return (result == USB_STOR_XFER_GOOD ? | 
|  | 191 | USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_FAILED); | 
|  | 192 | } | 
|  | 193 |  | 
|  | 194 |  | 
|  | 195 | static int sddr55_read_data(struct us_data *us, | 
|  | 196 | unsigned int lba, | 
|  | 197 | unsigned int page, | 
|  | 198 | unsigned short sectors) { | 
|  | 199 |  | 
|  | 200 | int result = USB_STOR_TRANSPORT_GOOD; | 
|  | 201 | unsigned char *command = us->iobuf; | 
|  | 202 | unsigned char *status = us->iobuf; | 
|  | 203 | struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra; | 
|  | 204 | unsigned char *buffer; | 
|  | 205 |  | 
|  | 206 | unsigned int pba; | 
|  | 207 | unsigned long address; | 
|  | 208 |  | 
|  | 209 | unsigned short pages; | 
| Jens Axboe | 1f6f31a | 2007-05-11 12:33:09 +0200 | [diff] [blame] | 210 | unsigned int len, offset; | 
|  | 211 | struct scatterlist *sg; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 |  | 
|  | 213 | // Since we only read in one block at a time, we have to create | 
|  | 214 | // a bounce buffer and move the data a piece at a time between the | 
|  | 215 | // bounce buffer and the actual transfer buffer. | 
|  | 216 |  | 
|  | 217 | len = min((unsigned int) sectors, (unsigned int) info->blocksize >> | 
|  | 218 | info->smallpageshift) * PAGESIZE; | 
|  | 219 | buffer = kmalloc(len, GFP_NOIO); | 
|  | 220 | if (buffer == NULL) | 
|  | 221 | return USB_STOR_TRANSPORT_ERROR; /* out of memory */ | 
| Jens Axboe | 1f6f31a | 2007-05-11 12:33:09 +0200 | [diff] [blame] | 222 | offset = 0; | 
|  | 223 | sg = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 |  | 
|  | 225 | while (sectors>0) { | 
|  | 226 |  | 
|  | 227 | /* have we got to end? */ | 
|  | 228 | if (lba >= info->max_log_blks) | 
|  | 229 | break; | 
|  | 230 |  | 
|  | 231 | pba = info->lba_to_pba[lba]; | 
|  | 232 |  | 
|  | 233 | // Read as many sectors as possible in this block | 
|  | 234 |  | 
|  | 235 | pages = min((unsigned int) sectors << info->smallpageshift, | 
|  | 236 | info->blocksize - page); | 
|  | 237 | len = pages << info->pageshift; | 
|  | 238 |  | 
|  | 239 | US_DEBUGP("Read %02X pages, from PBA %04X" | 
|  | 240 | " (LBA %04X) page %02X\n", | 
|  | 241 | pages, pba, lba, page); | 
|  | 242 |  | 
|  | 243 | if (pba == NOT_ALLOCATED) { | 
|  | 244 | /* no pba for this lba, fill with zeroes */ | 
|  | 245 | memset (buffer, 0, len); | 
|  | 246 | } else { | 
|  | 247 |  | 
|  | 248 | address = (pba << info->blockshift) + page; | 
|  | 249 |  | 
|  | 250 | command[0] = 0; | 
|  | 251 | command[1] = LSB_of(address>>16); | 
|  | 252 | command[2] = LSB_of(address>>8); | 
|  | 253 | command[3] = LSB_of(address); | 
|  | 254 |  | 
|  | 255 | command[4] = 0; | 
|  | 256 | command[5] = 0xB0; | 
|  | 257 | command[6] = LSB_of(pages << (1 - info->smallpageshift)); | 
|  | 258 | command[7] = 0x85; | 
|  | 259 |  | 
|  | 260 | /* send command */ | 
|  | 261 | result = sddr55_bulk_transport(us, | 
|  | 262 | DMA_TO_DEVICE, command, 8); | 
|  | 263 |  | 
|  | 264 | US_DEBUGP("Result for send_command in read_data %d\n", | 
|  | 265 | result); | 
|  | 266 |  | 
|  | 267 | if (result != USB_STOR_XFER_GOOD) { | 
|  | 268 | result = USB_STOR_TRANSPORT_ERROR; | 
|  | 269 | goto leave; | 
|  | 270 | } | 
|  | 271 |  | 
|  | 272 | /* read data */ | 
|  | 273 | result = sddr55_bulk_transport(us, | 
|  | 274 | DMA_FROM_DEVICE, buffer, len); | 
|  | 275 |  | 
|  | 276 | if (result != USB_STOR_XFER_GOOD) { | 
|  | 277 | result = USB_STOR_TRANSPORT_ERROR; | 
|  | 278 | goto leave; | 
|  | 279 | } | 
|  | 280 |  | 
|  | 281 | /* now read status */ | 
|  | 282 | result = sddr55_bulk_transport(us, | 
|  | 283 | DMA_FROM_DEVICE, status, 2); | 
|  | 284 |  | 
|  | 285 | if (result != USB_STOR_XFER_GOOD) { | 
|  | 286 | result = USB_STOR_TRANSPORT_ERROR; | 
|  | 287 | goto leave; | 
|  | 288 | } | 
|  | 289 |  | 
|  | 290 | /* check status for error */ | 
|  | 291 | if (status[0] == 0xff && status[1] == 0x4) { | 
|  | 292 | set_sense_info (3, 0x11, 0); | 
|  | 293 | result = USB_STOR_TRANSPORT_FAILED; | 
|  | 294 | goto leave; | 
|  | 295 | } | 
|  | 296 | } | 
|  | 297 |  | 
|  | 298 | // Store the data in the transfer buffer | 
|  | 299 | usb_stor_access_xfer_buf(buffer, len, us->srb, | 
| Jens Axboe | 1f6f31a | 2007-05-11 12:33:09 +0200 | [diff] [blame] | 300 | &sg, &offset, TO_XFER_BUF); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 |  | 
|  | 302 | page = 0; | 
|  | 303 | lba++; | 
|  | 304 | sectors -= pages >> info->smallpageshift; | 
|  | 305 | } | 
|  | 306 |  | 
|  | 307 | result = USB_STOR_TRANSPORT_GOOD; | 
|  | 308 |  | 
|  | 309 | leave: | 
|  | 310 | kfree(buffer); | 
|  | 311 |  | 
|  | 312 | return result; | 
|  | 313 | } | 
|  | 314 |  | 
|  | 315 | static int sddr55_write_data(struct us_data *us, | 
|  | 316 | unsigned int lba, | 
|  | 317 | unsigned int page, | 
|  | 318 | unsigned short sectors) { | 
|  | 319 |  | 
|  | 320 | int result = USB_STOR_TRANSPORT_GOOD; | 
|  | 321 | unsigned char *command = us->iobuf; | 
|  | 322 | unsigned char *status = us->iobuf; | 
|  | 323 | struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra; | 
|  | 324 | unsigned char *buffer; | 
|  | 325 |  | 
|  | 326 | unsigned int pba; | 
|  | 327 | unsigned int new_pba; | 
|  | 328 | unsigned long address; | 
|  | 329 |  | 
|  | 330 | unsigned short pages; | 
|  | 331 | int i; | 
| Jens Axboe | 1f6f31a | 2007-05-11 12:33:09 +0200 | [diff] [blame] | 332 | unsigned int len, offset; | 
|  | 333 | struct scatterlist *sg; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 |  | 
|  | 335 | /* check if we are allowed to write */ | 
|  | 336 | if (info->read_only || info->force_read_only) { | 
|  | 337 | set_sense_info (7, 0x27, 0);	/* read only */ | 
|  | 338 | return USB_STOR_TRANSPORT_FAILED; | 
|  | 339 | } | 
|  | 340 |  | 
|  | 341 | // Since we only write one block at a time, we have to create | 
|  | 342 | // a bounce buffer and move the data a piece at a time between the | 
|  | 343 | // bounce buffer and the actual transfer buffer. | 
|  | 344 |  | 
|  | 345 | len = min((unsigned int) sectors, (unsigned int) info->blocksize >> | 
|  | 346 | info->smallpageshift) * PAGESIZE; | 
|  | 347 | buffer = kmalloc(len, GFP_NOIO); | 
|  | 348 | if (buffer == NULL) | 
|  | 349 | return USB_STOR_TRANSPORT_ERROR; | 
| Jens Axboe | 1f6f31a | 2007-05-11 12:33:09 +0200 | [diff] [blame] | 350 | offset = 0; | 
|  | 351 | sg = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 |  | 
|  | 353 | while (sectors > 0) { | 
|  | 354 |  | 
|  | 355 | /* have we got to end? */ | 
|  | 356 | if (lba >= info->max_log_blks) | 
|  | 357 | break; | 
|  | 358 |  | 
|  | 359 | pba = info->lba_to_pba[lba]; | 
|  | 360 |  | 
|  | 361 | // Write as many sectors as possible in this block | 
|  | 362 |  | 
|  | 363 | pages = min((unsigned int) sectors << info->smallpageshift, | 
|  | 364 | info->blocksize - page); | 
|  | 365 | len = pages << info->pageshift; | 
|  | 366 |  | 
|  | 367 | // Get the data from the transfer buffer | 
|  | 368 | usb_stor_access_xfer_buf(buffer, len, us->srb, | 
| Jens Axboe | 1f6f31a | 2007-05-11 12:33:09 +0200 | [diff] [blame] | 369 | &sg, &offset, FROM_XFER_BUF); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 |  | 
|  | 371 | US_DEBUGP("Write %02X pages, to PBA %04X" | 
|  | 372 | " (LBA %04X) page %02X\n", | 
|  | 373 | pages, pba, lba, page); | 
|  | 374 |  | 
|  | 375 | command[4] = 0; | 
|  | 376 |  | 
|  | 377 | if (pba == NOT_ALLOCATED) { | 
|  | 378 | /* no pba allocated for this lba, find a free pba to use */ | 
|  | 379 |  | 
|  | 380 | int max_pba = (info->max_log_blks / 250 ) * 256; | 
|  | 381 | int found_count = 0; | 
|  | 382 | int found_pba = -1; | 
|  | 383 |  | 
|  | 384 | /* set pba to first block in zone lba is in */ | 
|  | 385 | pba = (lba / 1000) * 1024; | 
|  | 386 |  | 
|  | 387 | US_DEBUGP("No PBA for LBA %04X\n",lba); | 
|  | 388 |  | 
|  | 389 | if (max_pba > 1024) | 
|  | 390 | max_pba = 1024; | 
|  | 391 |  | 
|  | 392 | /* | 
|  | 393 | * Scan through the map looking for an unused block | 
|  | 394 | * leave 16 unused blocks at start (or as many as | 
|  | 395 | * possible) since the sddr55 seems to reuse a used | 
|  | 396 | * block when it shouldn't if we don't leave space. | 
|  | 397 | */ | 
|  | 398 | for (i = 0; i < max_pba; i++, pba++) { | 
|  | 399 | if (info->pba_to_lba[pba] == UNUSED_BLOCK) { | 
|  | 400 | found_pba = pba; | 
|  | 401 | if (found_count++ > 16) | 
|  | 402 | break; | 
|  | 403 | } | 
|  | 404 | } | 
|  | 405 |  | 
|  | 406 | pba = found_pba; | 
|  | 407 |  | 
|  | 408 | if (pba == -1) { | 
|  | 409 | /* oh dear */ | 
|  | 410 | US_DEBUGP("Couldn't find unallocated block\n"); | 
|  | 411 |  | 
|  | 412 | set_sense_info (3, 0x31, 0);	/* medium error */ | 
|  | 413 | result = USB_STOR_TRANSPORT_FAILED; | 
|  | 414 | goto leave; | 
|  | 415 | } | 
|  | 416 |  | 
|  | 417 | US_DEBUGP("Allocating PBA %04X for LBA %04X\n", pba, lba); | 
|  | 418 |  | 
|  | 419 | /* set writing to unallocated block flag */ | 
|  | 420 | command[4] = 0x40; | 
|  | 421 | } | 
|  | 422 |  | 
|  | 423 | address = (pba << info->blockshift) + page; | 
|  | 424 |  | 
|  | 425 | command[1] = LSB_of(address>>16); | 
|  | 426 | command[2] = LSB_of(address>>8); | 
|  | 427 | command[3] = LSB_of(address); | 
|  | 428 |  | 
|  | 429 | /* set the lba into the command, modulo 1000 */ | 
|  | 430 | command[0] = LSB_of(lba % 1000); | 
|  | 431 | command[6] = MSB_of(lba % 1000); | 
|  | 432 |  | 
|  | 433 | command[4] |= LSB_of(pages >> info->smallpageshift); | 
|  | 434 | command[5] = 0xB0; | 
|  | 435 | command[7] = 0x86; | 
|  | 436 |  | 
|  | 437 | /* send command */ | 
|  | 438 | result = sddr55_bulk_transport(us, | 
|  | 439 | DMA_TO_DEVICE, command, 8); | 
|  | 440 |  | 
|  | 441 | if (result != USB_STOR_XFER_GOOD) { | 
|  | 442 | US_DEBUGP("Result for send_command in write_data %d\n", | 
|  | 443 | result); | 
|  | 444 |  | 
|  | 445 | /* set_sense_info is superfluous here? */ | 
|  | 446 | set_sense_info (3, 0x3, 0);/* peripheral write error */ | 
|  | 447 | result = USB_STOR_TRANSPORT_FAILED; | 
|  | 448 | goto leave; | 
|  | 449 | } | 
|  | 450 |  | 
|  | 451 | /* send the data */ | 
|  | 452 | result = sddr55_bulk_transport(us, | 
|  | 453 | DMA_TO_DEVICE, buffer, len); | 
|  | 454 |  | 
|  | 455 | if (result != USB_STOR_XFER_GOOD) { | 
|  | 456 | US_DEBUGP("Result for send_data in write_data %d\n", | 
|  | 457 | result); | 
|  | 458 |  | 
|  | 459 | /* set_sense_info is superfluous here? */ | 
|  | 460 | set_sense_info (3, 0x3, 0);/* peripheral write error */ | 
|  | 461 | result = USB_STOR_TRANSPORT_FAILED; | 
|  | 462 | goto leave; | 
|  | 463 | } | 
|  | 464 |  | 
|  | 465 | /* now read status */ | 
|  | 466 | result = sddr55_bulk_transport(us, DMA_FROM_DEVICE, status, 6); | 
|  | 467 |  | 
|  | 468 | if (result != USB_STOR_XFER_GOOD) { | 
|  | 469 | US_DEBUGP("Result for get_status in write_data %d\n", | 
|  | 470 | result); | 
|  | 471 |  | 
|  | 472 | /* set_sense_info is superfluous here? */ | 
|  | 473 | set_sense_info (3, 0x3, 0);/* peripheral write error */ | 
|  | 474 | result = USB_STOR_TRANSPORT_FAILED; | 
|  | 475 | goto leave; | 
|  | 476 | } | 
|  | 477 |  | 
|  | 478 | new_pba = (status[3] + (status[4] << 8) + (status[5] << 16)) | 
|  | 479 | >> info->blockshift; | 
|  | 480 |  | 
|  | 481 | /* check status for error */ | 
|  | 482 | if (status[0] == 0xff && status[1] == 0x4) { | 
|  | 483 | info->pba_to_lba[new_pba] = BAD_BLOCK; | 
|  | 484 |  | 
|  | 485 | set_sense_info (3, 0x0c, 0); | 
|  | 486 | result = USB_STOR_TRANSPORT_FAILED; | 
|  | 487 | goto leave; | 
|  | 488 | } | 
|  | 489 |  | 
|  | 490 | US_DEBUGP("Updating maps for LBA %04X: old PBA %04X, new PBA %04X\n", | 
|  | 491 | lba, pba, new_pba); | 
|  | 492 |  | 
|  | 493 | /* update the lba<->pba maps, note new_pba might be the same as pba */ | 
|  | 494 | info->lba_to_pba[lba] = new_pba; | 
|  | 495 | info->pba_to_lba[pba] = UNUSED_BLOCK; | 
|  | 496 |  | 
|  | 497 | /* check that new_pba wasn't already being used */ | 
|  | 498 | if (info->pba_to_lba[new_pba] != UNUSED_BLOCK) { | 
|  | 499 | printk(KERN_ERR "sddr55 error: new PBA %04X already in use for LBA %04X\n", | 
|  | 500 | new_pba, info->pba_to_lba[new_pba]); | 
|  | 501 | info->fatal_error = 1; | 
|  | 502 | set_sense_info (3, 0x31, 0); | 
|  | 503 | result = USB_STOR_TRANSPORT_FAILED; | 
|  | 504 | goto leave; | 
|  | 505 | } | 
|  | 506 |  | 
|  | 507 | /* update the pba<->lba maps for new_pba */ | 
|  | 508 | info->pba_to_lba[new_pba] = lba % 1000; | 
|  | 509 |  | 
|  | 510 | page = 0; | 
|  | 511 | lba++; | 
|  | 512 | sectors -= pages >> info->smallpageshift; | 
|  | 513 | } | 
|  | 514 | result = USB_STOR_TRANSPORT_GOOD; | 
|  | 515 |  | 
|  | 516 | leave: | 
|  | 517 | kfree(buffer); | 
|  | 518 | return result; | 
|  | 519 | } | 
|  | 520 |  | 
|  | 521 | static int sddr55_read_deviceID(struct us_data *us, | 
|  | 522 | unsigned char *manufacturerID, | 
|  | 523 | unsigned char *deviceID) { | 
|  | 524 |  | 
|  | 525 | int result; | 
|  | 526 | unsigned char *command = us->iobuf; | 
|  | 527 | unsigned char *content = us->iobuf; | 
|  | 528 |  | 
|  | 529 | memset(command, 0, 8); | 
|  | 530 | command[5] = 0xB0; | 
|  | 531 | command[7] = 0x84; | 
|  | 532 | result = sddr55_bulk_transport(us, DMA_TO_DEVICE, command, 8); | 
|  | 533 |  | 
|  | 534 | US_DEBUGP("Result of send_control for device ID is %d\n", | 
|  | 535 | result); | 
|  | 536 |  | 
|  | 537 | if (result != USB_STOR_XFER_GOOD) | 
|  | 538 | return USB_STOR_TRANSPORT_ERROR; | 
|  | 539 |  | 
|  | 540 | result = sddr55_bulk_transport(us, | 
|  | 541 | DMA_FROM_DEVICE, content, 4); | 
|  | 542 |  | 
|  | 543 | if (result != USB_STOR_XFER_GOOD) | 
|  | 544 | return USB_STOR_TRANSPORT_ERROR; | 
|  | 545 |  | 
|  | 546 | *manufacturerID = content[0]; | 
|  | 547 | *deviceID = content[1]; | 
|  | 548 |  | 
|  | 549 | if (content[0] != 0xff)	{ | 
|  | 550 | result = sddr55_bulk_transport(us, | 
|  | 551 | DMA_FROM_DEVICE, content, 2); | 
|  | 552 | } | 
|  | 553 |  | 
|  | 554 | return USB_STOR_TRANSPORT_GOOD; | 
|  | 555 | } | 
|  | 556 |  | 
|  | 557 |  | 
| Alan Stern | 70fcc00 | 2009-02-12 14:47:59 -0500 | [diff] [blame] | 558 | static int sddr55_reset(struct us_data *us) | 
|  | 559 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | return 0; | 
|  | 561 | } | 
|  | 562 |  | 
|  | 563 |  | 
|  | 564 | static unsigned long sddr55_get_capacity(struct us_data *us) { | 
|  | 565 |  | 
| Andrew Morton | 8a20acc | 2008-03-04 15:25:08 -0800 | [diff] [blame] | 566 | unsigned char uninitialized_var(manufacturerID); | 
|  | 567 | unsigned char uninitialized_var(deviceID); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | int result; | 
|  | 569 | struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra; | 
|  | 570 |  | 
|  | 571 | US_DEBUGP("Reading capacity...\n"); | 
|  | 572 |  | 
|  | 573 | result = sddr55_read_deviceID(us, | 
|  | 574 | &manufacturerID, | 
|  | 575 | &deviceID); | 
|  | 576 |  | 
|  | 577 | US_DEBUGP("Result of read_deviceID is %d\n", | 
|  | 578 | result); | 
|  | 579 |  | 
|  | 580 | if (result != USB_STOR_XFER_GOOD) | 
|  | 581 | return 0; | 
|  | 582 |  | 
|  | 583 | US_DEBUGP("Device ID = %02X\n", deviceID); | 
|  | 584 | US_DEBUGP("Manuf  ID = %02X\n", manufacturerID); | 
|  | 585 |  | 
|  | 586 | info->pageshift = 9; | 
|  | 587 | info->smallpageshift = 0; | 
|  | 588 | info->blocksize = 16; | 
|  | 589 | info->blockshift = 4; | 
|  | 590 | info->blockmask = 15; | 
|  | 591 |  | 
|  | 592 | switch (deviceID) { | 
|  | 593 |  | 
|  | 594 | case 0x6e: // 1MB | 
|  | 595 | case 0xe8: | 
|  | 596 | case 0xec: | 
|  | 597 | info->pageshift = 8; | 
|  | 598 | info->smallpageshift = 1; | 
|  | 599 | return 0x00100000; | 
|  | 600 |  | 
|  | 601 | case 0xea: // 2MB | 
|  | 602 | case 0x64: | 
|  | 603 | info->pageshift = 8; | 
|  | 604 | info->smallpageshift = 1; | 
|  | 605 | case 0x5d: // 5d is a ROM card with pagesize 512. | 
|  | 606 | return 0x00200000; | 
|  | 607 |  | 
|  | 608 | case 0xe3: // 4MB | 
|  | 609 | case 0xe5: | 
|  | 610 | case 0x6b: | 
|  | 611 | case 0xd5: | 
|  | 612 | return 0x00400000; | 
|  | 613 |  | 
|  | 614 | case 0xe6: // 8MB | 
|  | 615 | case 0xd6: | 
|  | 616 | return 0x00800000; | 
|  | 617 |  | 
|  | 618 | case 0x73: // 16MB | 
|  | 619 | info->blocksize = 32; | 
|  | 620 | info->blockshift = 5; | 
|  | 621 | info->blockmask = 31; | 
|  | 622 | return 0x01000000; | 
|  | 623 |  | 
|  | 624 | case 0x75: // 32MB | 
|  | 625 | info->blocksize = 32; | 
|  | 626 | info->blockshift = 5; | 
|  | 627 | info->blockmask = 31; | 
|  | 628 | return 0x02000000; | 
|  | 629 |  | 
|  | 630 | case 0x76: // 64MB | 
|  | 631 | info->blocksize = 32; | 
|  | 632 | info->blockshift = 5; | 
|  | 633 | info->blockmask = 31; | 
|  | 634 | return 0x04000000; | 
|  | 635 |  | 
|  | 636 | case 0x79: // 128MB | 
|  | 637 | info->blocksize = 32; | 
|  | 638 | info->blockshift = 5; | 
|  | 639 | info->blockmask = 31; | 
|  | 640 | return 0x08000000; | 
|  | 641 |  | 
|  | 642 | default: // unknown | 
|  | 643 | return 0; | 
|  | 644 |  | 
|  | 645 | } | 
|  | 646 | } | 
|  | 647 |  | 
|  | 648 | static int sddr55_read_map(struct us_data *us) { | 
|  | 649 |  | 
|  | 650 | struct sddr55_card_info *info = (struct sddr55_card_info *)(us->extra); | 
|  | 651 | int numblocks; | 
|  | 652 | unsigned char *buffer; | 
|  | 653 | unsigned char *command = us->iobuf; | 
|  | 654 | int i; | 
|  | 655 | unsigned short lba; | 
|  | 656 | unsigned short max_lba; | 
|  | 657 | int result; | 
|  | 658 |  | 
|  | 659 | if (!info->capacity) | 
|  | 660 | return -1; | 
|  | 661 |  | 
|  | 662 | numblocks = info->capacity >> (info->blockshift + info->pageshift); | 
|  | 663 |  | 
|  | 664 | buffer = kmalloc( numblocks * 2, GFP_NOIO ); | 
|  | 665 |  | 
|  | 666 | if (!buffer) | 
|  | 667 | return -1; | 
|  | 668 |  | 
|  | 669 | memset(command, 0, 8); | 
|  | 670 | command[5] = 0xB0; | 
|  | 671 | command[6] = numblocks * 2 / 256; | 
|  | 672 | command[7] = 0x8A; | 
|  | 673 |  | 
|  | 674 | result = sddr55_bulk_transport(us, DMA_TO_DEVICE, command, 8); | 
|  | 675 |  | 
|  | 676 | if ( result != USB_STOR_XFER_GOOD) { | 
|  | 677 | kfree (buffer); | 
|  | 678 | return -1; | 
|  | 679 | } | 
|  | 680 |  | 
|  | 681 | result = sddr55_bulk_transport(us, DMA_FROM_DEVICE, buffer, numblocks * 2); | 
|  | 682 |  | 
|  | 683 | if ( result != USB_STOR_XFER_GOOD) { | 
|  | 684 | kfree (buffer); | 
|  | 685 | return -1; | 
|  | 686 | } | 
|  | 687 |  | 
|  | 688 | result = sddr55_bulk_transport(us, DMA_FROM_DEVICE, command, 2); | 
|  | 689 |  | 
|  | 690 | if ( result != USB_STOR_XFER_GOOD) { | 
|  | 691 | kfree (buffer); | 
|  | 692 | return -1; | 
|  | 693 | } | 
|  | 694 |  | 
| Jesper Juhl | 1bc3c9e | 2005-04-18 17:39:34 -0700 | [diff] [blame] | 695 | kfree(info->lba_to_pba); | 
|  | 696 | kfree(info->pba_to_lba); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | info->lba_to_pba = kmalloc(numblocks*sizeof(int), GFP_NOIO); | 
|  | 698 | info->pba_to_lba = kmalloc(numblocks*sizeof(int), GFP_NOIO); | 
|  | 699 |  | 
|  | 700 | if (info->lba_to_pba == NULL || info->pba_to_lba == NULL) { | 
| Jesper Juhl | 1bc3c9e | 2005-04-18 17:39:34 -0700 | [diff] [blame] | 701 | kfree(info->lba_to_pba); | 
|  | 702 | kfree(info->pba_to_lba); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | info->lba_to_pba = NULL; | 
|  | 704 | info->pba_to_lba = NULL; | 
|  | 705 | kfree(buffer); | 
|  | 706 | return -1; | 
|  | 707 | } | 
|  | 708 |  | 
|  | 709 | memset(info->lba_to_pba, 0xff, numblocks*sizeof(int)); | 
|  | 710 | memset(info->pba_to_lba, 0xff, numblocks*sizeof(int)); | 
|  | 711 |  | 
|  | 712 | /* set maximum lba */ | 
|  | 713 | max_lba = info->max_log_blks; | 
|  | 714 | if (max_lba > 1000) | 
|  | 715 | max_lba = 1000; | 
|  | 716 |  | 
|  | 717 | // Each block is 64 bytes of control data, so block i is located in | 
|  | 718 | // scatterlist block i*64/128k = i*(2^6)*(2^-17) = i*(2^-11) | 
|  | 719 |  | 
|  | 720 | for (i=0; i<numblocks; i++) { | 
|  | 721 | int zone = i / 1024; | 
|  | 722 |  | 
|  | 723 | lba = short_pack(buffer[i * 2], buffer[i * 2 + 1]); | 
|  | 724 |  | 
|  | 725 | /* Every 1024 physical blocks ("zone"), the LBA numbers | 
|  | 726 | * go back to zero, but are within a higher | 
|  | 727 | * block of LBA's. Also, there is a maximum of | 
|  | 728 | * 1000 LBA's per zone. In other words, in PBA | 
|  | 729 | * 1024-2047 you will find LBA 0-999 which are | 
|  | 730 | * really LBA 1000-1999. Yes, this wastes 24 | 
|  | 731 | * physical blocks per zone. Go figure. | 
|  | 732 | * These devices can have blocks go bad, so there | 
|  | 733 | * are 24 spare blocks to use when blocks do go bad. | 
|  | 734 | */ | 
|  | 735 |  | 
|  | 736 | /* SDDR55 returns 0xffff for a bad block, and 0x400 for the | 
|  | 737 | * CIS block. (Is this true for cards 8MB or less??) | 
|  | 738 | * Record these in the physical to logical map | 
|  | 739 | */ | 
|  | 740 |  | 
|  | 741 | info->pba_to_lba[i] = lba; | 
|  | 742 |  | 
|  | 743 | if (lba >= max_lba) { | 
|  | 744 | continue; | 
|  | 745 | } | 
|  | 746 |  | 
|  | 747 | if (info->lba_to_pba[lba + zone * 1000] != NOT_ALLOCATED && | 
|  | 748 | !info->force_read_only) { | 
| Frank Seidel | 6f8aa65 | 2009-02-05 16:16:24 +0100 | [diff] [blame] | 749 | printk(KERN_WARNING | 
|  | 750 | "sddr55: map inconsistency at LBA %04X\n", | 
|  | 751 | lba + zone * 1000); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | info->force_read_only = 1; | 
|  | 753 | } | 
|  | 754 |  | 
|  | 755 | if (lba<0x10 || (lba>=0x3E0 && lba<0x3EF)) | 
|  | 756 | US_DEBUGP("LBA %04X <-> PBA %04X\n", lba, i); | 
|  | 757 |  | 
|  | 758 | info->lba_to_pba[lba + zone * 1000] = i; | 
|  | 759 | } | 
|  | 760 |  | 
|  | 761 | kfree(buffer); | 
|  | 762 | return 0; | 
|  | 763 | } | 
|  | 764 |  | 
|  | 765 |  | 
|  | 766 | static void sddr55_card_info_destructor(void *extra) { | 
|  | 767 | struct sddr55_card_info *info = (struct sddr55_card_info *)extra; | 
|  | 768 |  | 
|  | 769 | if (!extra) | 
|  | 770 | return; | 
|  | 771 |  | 
| Jesper Juhl | 1bc3c9e | 2005-04-18 17:39:34 -0700 | [diff] [blame] | 772 | kfree(info->lba_to_pba); | 
|  | 773 | kfree(info->pba_to_lba); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | } | 
|  | 775 |  | 
|  | 776 |  | 
|  | 777 | /* | 
|  | 778 | * Transport for the Sandisk SDDR-55 | 
|  | 779 | */ | 
| Alan Stern | 70fcc00 | 2009-02-12 14:47:59 -0500 | [diff] [blame] | 780 | static int sddr55_transport(struct scsi_cmnd *srb, struct us_data *us) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | { | 
|  | 782 | int result; | 
|  | 783 | static unsigned char inquiry_response[8] = { | 
|  | 784 | 0x00, 0x80, 0x00, 0x02, 0x1F, 0x00, 0x00, 0x00 | 
|  | 785 | }; | 
|  | 786 | // write-protected for now, no block descriptor support | 
|  | 787 | static unsigned char mode_page_01[20] = { | 
|  | 788 | 0x0, 0x12, 0x00, 0x80, 0x0, 0x0, 0x0, 0x0, | 
|  | 789 | 0x01, 0x0A, | 
|  | 790 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 | 
|  | 791 | }; | 
|  | 792 | unsigned char *ptr = us->iobuf; | 
|  | 793 | unsigned long capacity; | 
|  | 794 | unsigned int lba; | 
|  | 795 | unsigned int pba; | 
|  | 796 | unsigned int page; | 
|  | 797 | unsigned short pages; | 
|  | 798 | struct sddr55_card_info *info; | 
|  | 799 |  | 
|  | 800 | if (!us->extra) { | 
| Oliver Neukum | 887c256 | 2006-01-08 12:33:45 +0100 | [diff] [blame] | 801 | us->extra = kzalloc( | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | sizeof(struct sddr55_card_info), GFP_NOIO); | 
|  | 803 | if (!us->extra) | 
|  | 804 | return USB_STOR_TRANSPORT_ERROR; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | us->extra_destructor = sddr55_card_info_destructor; | 
|  | 806 | } | 
|  | 807 |  | 
|  | 808 | info = (struct sddr55_card_info *)(us->extra); | 
|  | 809 |  | 
|  | 810 | if (srb->cmnd[0] == REQUEST_SENSE) { | 
|  | 811 | US_DEBUGP("SDDR55: request sense %02x/%02x/%02x\n", info->sense_data[2], info->sense_data[12], info->sense_data[13]); | 
|  | 812 |  | 
|  | 813 | memcpy (ptr, info->sense_data, sizeof info->sense_data); | 
|  | 814 | ptr[0] = 0x70; | 
|  | 815 | ptr[7] = 11; | 
|  | 816 | usb_stor_set_xfer_buf (ptr, sizeof info->sense_data, srb); | 
|  | 817 | memset (info->sense_data, 0, sizeof info->sense_data); | 
|  | 818 |  | 
|  | 819 | return USB_STOR_TRANSPORT_GOOD; | 
|  | 820 | } | 
|  | 821 |  | 
|  | 822 | memset (info->sense_data, 0, sizeof info->sense_data); | 
|  | 823 |  | 
|  | 824 | /* Dummy up a response for INQUIRY since SDDR55 doesn't | 
|  | 825 | respond to INQUIRY commands */ | 
|  | 826 |  | 
|  | 827 | if (srb->cmnd[0] == INQUIRY) { | 
|  | 828 | memcpy(ptr, inquiry_response, 8); | 
|  | 829 | fill_inquiry_response(us, ptr, 36); | 
|  | 830 | return USB_STOR_TRANSPORT_GOOD; | 
|  | 831 | } | 
|  | 832 |  | 
|  | 833 | /* only check card status if the map isn't allocated, ie no card seen yet | 
|  | 834 | * or if it's been over half a second since we last accessed it | 
|  | 835 | */ | 
|  | 836 | if (info->lba_to_pba == NULL || time_after(jiffies, info->last_access + HZ/2)) { | 
|  | 837 |  | 
|  | 838 | /* check to see if a card is fitted */ | 
|  | 839 | result = sddr55_status (us); | 
|  | 840 | if (result) { | 
|  | 841 | result = sddr55_status (us); | 
|  | 842 | if (!result) { | 
|  | 843 | set_sense_info (6, 0x28, 0);	/* new media, set unit attention, not ready to ready */ | 
|  | 844 | } | 
|  | 845 | return USB_STOR_TRANSPORT_FAILED; | 
|  | 846 | } | 
|  | 847 | } | 
|  | 848 |  | 
|  | 849 | /* if we detected a problem with the map when writing, | 
|  | 850 | don't allow any more access */ | 
|  | 851 | if (info->fatal_error) { | 
|  | 852 |  | 
|  | 853 | set_sense_info (3, 0x31, 0); | 
|  | 854 | return USB_STOR_TRANSPORT_FAILED; | 
|  | 855 | } | 
|  | 856 |  | 
|  | 857 | if (srb->cmnd[0] == READ_CAPACITY) { | 
|  | 858 |  | 
|  | 859 | capacity = sddr55_get_capacity(us); | 
|  | 860 |  | 
|  | 861 | if (!capacity) { | 
|  | 862 | set_sense_info (3, 0x30, 0); /* incompatible medium */ | 
|  | 863 | return USB_STOR_TRANSPORT_FAILED; | 
|  | 864 | } | 
|  | 865 |  | 
|  | 866 | info->capacity = capacity; | 
|  | 867 |  | 
|  | 868 | /* figure out the maximum logical block number, allowing for | 
|  | 869 | * the fact that only 250 out of every 256 are used */ | 
|  | 870 | info->max_log_blks = ((info->capacity >> (info->pageshift + info->blockshift)) / 256) * 250; | 
|  | 871 |  | 
|  | 872 | /* Last page in the card, adjust as we only use 250 out of | 
|  | 873 | * every 256 pages */ | 
|  | 874 | capacity = (capacity / 256) * 250; | 
|  | 875 |  | 
|  | 876 | capacity /= PAGESIZE; | 
|  | 877 | capacity--; | 
|  | 878 |  | 
|  | 879 | ((__be32 *) ptr)[0] = cpu_to_be32(capacity); | 
|  | 880 | ((__be32 *) ptr)[1] = cpu_to_be32(PAGESIZE); | 
|  | 881 | usb_stor_set_xfer_buf(ptr, 8, srb); | 
|  | 882 |  | 
|  | 883 | sddr55_read_map(us); | 
|  | 884 |  | 
|  | 885 | return USB_STOR_TRANSPORT_GOOD; | 
|  | 886 | } | 
|  | 887 |  | 
|  | 888 | if (srb->cmnd[0] == MODE_SENSE_10) { | 
|  | 889 |  | 
|  | 890 | memcpy(ptr, mode_page_01, sizeof mode_page_01); | 
|  | 891 | ptr[3] = (info->read_only || info->force_read_only) ? 0x80 : 0; | 
|  | 892 | usb_stor_set_xfer_buf(ptr, sizeof(mode_page_01), srb); | 
|  | 893 |  | 
|  | 894 | if ( (srb->cmnd[2] & 0x3F) == 0x01 ) { | 
|  | 895 | US_DEBUGP( | 
|  | 896 | "SDDR55: Dummy up request for mode page 1\n"); | 
|  | 897 | return USB_STOR_TRANSPORT_GOOD; | 
|  | 898 |  | 
|  | 899 | } else if ( (srb->cmnd[2] & 0x3F) == 0x3F ) { | 
|  | 900 | US_DEBUGP( | 
|  | 901 | "SDDR55: Dummy up request for all mode pages\n"); | 
|  | 902 | return USB_STOR_TRANSPORT_GOOD; | 
|  | 903 | } | 
|  | 904 |  | 
|  | 905 | set_sense_info (5, 0x24, 0);	/* invalid field in command */ | 
|  | 906 | return USB_STOR_TRANSPORT_FAILED; | 
|  | 907 | } | 
|  | 908 |  | 
|  | 909 | if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) { | 
|  | 910 |  | 
|  | 911 | US_DEBUGP( | 
|  | 912 | "SDDR55: %s medium removal. Not that I can do" | 
|  | 913 | " anything about it...\n", | 
|  | 914 | (srb->cmnd[4]&0x03) ? "Prevent" : "Allow"); | 
|  | 915 |  | 
|  | 916 | return USB_STOR_TRANSPORT_GOOD; | 
|  | 917 |  | 
|  | 918 | } | 
|  | 919 |  | 
|  | 920 | if (srb->cmnd[0] == READ_10 || srb->cmnd[0] == WRITE_10) { | 
|  | 921 |  | 
|  | 922 | page = short_pack(srb->cmnd[3], srb->cmnd[2]); | 
|  | 923 | page <<= 16; | 
|  | 924 | page |= short_pack(srb->cmnd[5], srb->cmnd[4]); | 
|  | 925 | pages = short_pack(srb->cmnd[8], srb->cmnd[7]); | 
|  | 926 |  | 
|  | 927 | page <<= info->smallpageshift; | 
|  | 928 |  | 
|  | 929 | // convert page to block and page-within-block | 
|  | 930 |  | 
|  | 931 | lba = page >> info->blockshift; | 
|  | 932 | page = page & info->blockmask; | 
|  | 933 |  | 
|  | 934 | // locate physical block corresponding to logical block | 
|  | 935 |  | 
|  | 936 | if (lba >= info->max_log_blks) { | 
|  | 937 |  | 
|  | 938 | US_DEBUGP("Error: Requested LBA %04X exceeds maximum " | 
|  | 939 | "block %04X\n", lba, info->max_log_blks-1); | 
|  | 940 |  | 
|  | 941 | set_sense_info (5, 0x24, 0);	/* invalid field in command */ | 
|  | 942 |  | 
|  | 943 | return USB_STOR_TRANSPORT_FAILED; | 
|  | 944 | } | 
|  | 945 |  | 
|  | 946 | pba = info->lba_to_pba[lba]; | 
|  | 947 |  | 
|  | 948 | if (srb->cmnd[0] == WRITE_10) { | 
|  | 949 | US_DEBUGP("WRITE_10: write block %04X (LBA %04X) page %01X" | 
|  | 950 | " pages %d\n", | 
|  | 951 | pba, lba, page, pages); | 
|  | 952 |  | 
|  | 953 | return sddr55_write_data(us, lba, page, pages); | 
|  | 954 | } else { | 
|  | 955 | US_DEBUGP("READ_10: read block %04X (LBA %04X) page %01X" | 
|  | 956 | " pages %d\n", | 
|  | 957 | pba, lba, page, pages); | 
|  | 958 |  | 
|  | 959 | return sddr55_read_data(us, lba, page, pages); | 
|  | 960 | } | 
|  | 961 | } | 
|  | 962 |  | 
|  | 963 |  | 
|  | 964 | if (srb->cmnd[0] == TEST_UNIT_READY) { | 
|  | 965 | return USB_STOR_TRANSPORT_GOOD; | 
|  | 966 | } | 
|  | 967 |  | 
|  | 968 | if (srb->cmnd[0] == START_STOP) { | 
|  | 969 | return USB_STOR_TRANSPORT_GOOD; | 
|  | 970 | } | 
|  | 971 |  | 
|  | 972 | set_sense_info (5, 0x20, 0);	/* illegal command */ | 
|  | 973 |  | 
|  | 974 | return USB_STOR_TRANSPORT_FAILED; // FIXME: sense buffer? | 
|  | 975 | } | 
|  | 976 |  | 
| Alan Stern | 70fcc00 | 2009-02-12 14:47:59 -0500 | [diff] [blame] | 977 |  | 
|  | 978 | static int sddr55_probe(struct usb_interface *intf, | 
|  | 979 | const struct usb_device_id *id) | 
|  | 980 | { | 
|  | 981 | struct us_data *us; | 
|  | 982 | int result; | 
|  | 983 |  | 
|  | 984 | result = usb_stor_probe1(&us, intf, id, | 
|  | 985 | (id - sddr55_usb_ids) + sddr55_unusual_dev_list); | 
|  | 986 | if (result) | 
|  | 987 | return result; | 
|  | 988 |  | 
|  | 989 | us->transport_name = "SDDR55"; | 
|  | 990 | us->transport = sddr55_transport; | 
|  | 991 | us->transport_reset = sddr55_reset; | 
|  | 992 | us->max_lun = 0; | 
|  | 993 |  | 
|  | 994 | result = usb_stor_probe2(us); | 
|  | 995 | return result; | 
|  | 996 | } | 
|  | 997 |  | 
|  | 998 | static struct usb_driver sddr55_driver = { | 
|  | 999 | .name =		"ums-sddr55", | 
|  | 1000 | .probe =	sddr55_probe, | 
|  | 1001 | .disconnect =	usb_stor_disconnect, | 
|  | 1002 | .suspend =	usb_stor_suspend, | 
|  | 1003 | .resume =	usb_stor_resume, | 
|  | 1004 | .reset_resume =	usb_stor_reset_resume, | 
|  | 1005 | .pre_reset =	usb_stor_pre_reset, | 
|  | 1006 | .post_reset =	usb_stor_post_reset, | 
|  | 1007 | .id_table =	sddr55_usb_ids, | 
|  | 1008 | .soft_unbind =	1, | 
|  | 1009 | }; | 
|  | 1010 |  | 
|  | 1011 | static int __init sddr55_init(void) | 
|  | 1012 | { | 
|  | 1013 | return usb_register(&sddr55_driver); | 
|  | 1014 | } | 
|  | 1015 |  | 
|  | 1016 | static void __exit sddr55_exit(void) | 
|  | 1017 | { | 
|  | 1018 | usb_deregister(&sddr55_driver); | 
|  | 1019 | } | 
|  | 1020 |  | 
|  | 1021 | module_init(sddr55_init); | 
|  | 1022 | module_exit(sddr55_exit); |