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