Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* Driver for Datafab USB Compact Flash reader |
1da177e4 LT |
2 | * |
3 | * datafab driver v0.1: | |
4 | * | |
5 | * First release | |
6 | * | |
7 | * Current development and maintenance by: | |
8 | * (c) 2000 Jimmie Mayfield (mayfield+datafab@sackheads.org) | |
9 | * | |
10 | * Many thanks to Robert Baruch for the SanDisk SmartMedia reader driver | |
11 | * which I used as a template for this driver. | |
12 | * | |
13 | * Some bugfixes and scatter-gather code by Gregory P. Smith | |
14 | * (greg-usb@electricrain.com) | |
15 | * | |
16 | * Fix for media change by Joerg Schneider (js@joergschneider.com) | |
17 | * | |
18 | * Other contributors: | |
19 | * (c) 2002 Alan Stern <stern@rowland.org> | |
20 | * | |
21 | * This program is free software; you can redistribute it and/or modify it | |
22 | * under the terms of the GNU General Public License as published by the | |
23 | * Free Software Foundation; either version 2, or (at your option) any | |
24 | * later version. | |
25 | * | |
26 | * This program is distributed in the hope that it will be useful, but | |
27 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
28 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
29 | * General Public License for more details. | |
30 | * | |
31 | * You should have received a copy of the GNU General Public License along | |
32 | * with this program; if not, write to the Free Software Foundation, Inc., | |
33 | * 675 Mass Ave, Cambridge, MA 02139, USA. | |
34 | */ | |
35 | ||
36 | /* | |
37 | * This driver attempts to support USB CompactFlash reader/writer devices | |
38 | * based on Datafab USB-to-ATA chips. It was specifically developed for the | |
39 | * Datafab MDCFE-B USB CompactFlash reader but has since been found to work | |
40 | * with a variety of Datafab-based devices from a number of manufacturers. | |
41 | * I've received a report of this driver working with a Datafab-based | |
42 | * SmartMedia device though please be aware that I'm personally unable to | |
43 | * test SmartMedia support. | |
44 | * | |
45 | * This driver supports reading and writing. If you're truly paranoid, | |
46 | * however, you can force the driver into a write-protected state by setting | |
47 | * the WP enable bits in datafab_handle_mode_sense(). See the comments | |
48 | * in that routine. | |
49 | */ | |
50 | ||
1da177e4 | 51 | #include <linux/errno.h> |
2cbbf357 | 52 | #include <linux/module.h> |
1da177e4 LT |
53 | #include <linux/slab.h> |
54 | ||
55 | #include <scsi/scsi.h> | |
56 | #include <scsi/scsi_cmnd.h> | |
57 | ||
58 | #include "usb.h" | |
59 | #include "transport.h" | |
60 | #include "protocol.h" | |
61 | #include "debug.h" | |
2cbbf357 | 62 | |
4246b06a MG |
63 | MODULE_DESCRIPTION("Driver for Datafab USB Compact Flash reader"); |
64 | MODULE_AUTHOR("Jimmie Mayfield <mayfield+datafab@sackheads.org>"); | |
65 | MODULE_LICENSE("GPL"); | |
66 | ||
2cbbf357 AS |
67 | struct datafab_info { |
68 | unsigned long sectors; /* total sector count */ | |
69 | unsigned long ssize; /* sector size in bytes */ | |
70 | signed char lun; /* used for dual-slot readers */ | |
71 | ||
72 | /* the following aren't used yet */ | |
73 | unsigned char sense_key; | |
74 | unsigned long sense_asc; /* additional sense code */ | |
75 | unsigned long sense_ascq; /* additional sense code qualifier */ | |
76 | }; | |
1da177e4 LT |
77 | |
78 | static int datafab_determine_lun(struct us_data *us, | |
79 | struct datafab_info *info); | |
80 | ||
81 | ||
2cbbf357 AS |
82 | /* |
83 | * The table of devices | |
84 | */ | |
85 | #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \ | |
86 | vendorName, productName, useProtocol, useTransport, \ | |
87 | initFunction, flags) \ | |
88 | { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \ | |
89 | .driver_info = (flags)|(USB_US_TYPE_STOR<<24) } | |
90 | ||
b03379f7 | 91 | static struct usb_device_id datafab_usb_ids[] = { |
2cbbf357 AS |
92 | # include "unusual_datafab.h" |
93 | { } /* Terminating entry */ | |
94 | }; | |
95 | MODULE_DEVICE_TABLE(usb, datafab_usb_ids); | |
96 | ||
97 | #undef UNUSUAL_DEV | |
98 | ||
99 | /* | |
100 | * The flags table | |
101 | */ | |
102 | #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \ | |
103 | vendor_name, product_name, use_protocol, use_transport, \ | |
104 | init_function, Flags) \ | |
105 | { \ | |
106 | .vendorName = vendor_name, \ | |
107 | .productName = product_name, \ | |
108 | .useProtocol = use_protocol, \ | |
109 | .useTransport = use_transport, \ | |
110 | .initFunction = init_function, \ | |
111 | } | |
112 | ||
113 | static struct us_unusual_dev datafab_unusual_dev_list[] = { | |
114 | # include "unusual_datafab.h" | |
115 | { } /* Terminating entry */ | |
116 | }; | |
117 | ||
118 | #undef UNUSUAL_DEV | |
119 | ||
120 | ||
1da177e4 LT |
121 | static inline int |
122 | datafab_bulk_read(struct us_data *us, unsigned char *data, unsigned int len) { | |
123 | if (len == 0) | |
124 | return USB_STOR_XFER_GOOD; | |
125 | ||
126 | US_DEBUGP("datafab_bulk_read: len = %d\n", len); | |
127 | return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, | |
128 | data, len, NULL); | |
129 | } | |
130 | ||
131 | ||
132 | static inline int | |
133 | datafab_bulk_write(struct us_data *us, unsigned char *data, unsigned int len) { | |
134 | if (len == 0) | |
135 | return USB_STOR_XFER_GOOD; | |
136 | ||
137 | US_DEBUGP("datafab_bulk_write: len = %d\n", len); | |
138 | return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, | |
139 | data, len, NULL); | |
140 | } | |
141 | ||
142 | ||
143 | static int datafab_read_data(struct us_data *us, | |
144 | struct datafab_info *info, | |
145 | u32 sector, | |
146 | u32 sectors) | |
147 | { | |
148 | unsigned char *command = us->iobuf; | |
149 | unsigned char *buffer; | |
150 | unsigned char thistime; | |
151 | unsigned int totallen, alloclen; | |
152 | int len, result; | |
1f6f31a0 JA |
153 | unsigned int sg_offset = 0; |
154 | struct scatterlist *sg = NULL; | |
1da177e4 LT |
155 | |
156 | // we're working in LBA mode. according to the ATA spec, | |
157 | // we can support up to 28-bit addressing. I don't know if Datafab | |
158 | // supports beyond 24-bit addressing. It's kind of hard to test | |
159 | // since it requires > 8GB CF card. | |
160 | // | |
161 | if (sectors > 0x0FFFFFFF) | |
162 | return USB_STOR_TRANSPORT_ERROR; | |
163 | ||
164 | if (info->lun == -1) { | |
165 | result = datafab_determine_lun(us, info); | |
166 | if (result != USB_STOR_TRANSPORT_GOOD) | |
167 | return result; | |
168 | } | |
169 | ||
170 | totallen = sectors * info->ssize; | |
171 | ||
172 | // Since we don't read more than 64 KB at a time, we have to create | |
173 | // a bounce buffer and move the data a piece at a time between the | |
174 | // bounce buffer and the actual transfer buffer. | |
175 | ||
176 | alloclen = min(totallen, 65536u); | |
177 | buffer = kmalloc(alloclen, GFP_NOIO); | |
178 | if (buffer == NULL) | |
179 | return USB_STOR_TRANSPORT_ERROR; | |
180 | ||
181 | do { | |
182 | // loop, never allocate or transfer more than 64k at once | |
183 | // (min(128k, 255*info->ssize) is the real limit) | |
184 | ||
185 | len = min(totallen, alloclen); | |
186 | thistime = (len / info->ssize) & 0xff; | |
187 | ||
188 | command[0] = 0; | |
189 | command[1] = thistime; | |
190 | command[2] = sector & 0xFF; | |
191 | command[3] = (sector >> 8) & 0xFF; | |
192 | command[4] = (sector >> 16) & 0xFF; | |
193 | ||
194 | command[5] = 0xE0 + (info->lun << 4); | |
195 | command[5] |= (sector >> 24) & 0x0F; | |
196 | command[6] = 0x20; | |
197 | command[7] = 0x01; | |
198 | ||
199 | // send the read command | |
200 | result = datafab_bulk_write(us, command, 8); | |
201 | if (result != USB_STOR_XFER_GOOD) | |
202 | goto leave; | |
203 | ||
204 | // read the result | |
205 | result = datafab_bulk_read(us, buffer, len); | |
206 | if (result != USB_STOR_XFER_GOOD) | |
207 | goto leave; | |
208 | ||
209 | // Store the data in the transfer buffer | |
210 | usb_stor_access_xfer_buf(buffer, len, us->srb, | |
1f6f31a0 | 211 | &sg, &sg_offset, TO_XFER_BUF); |
1da177e4 LT |
212 | |
213 | sector += thistime; | |
214 | totallen -= len; | |
215 | } while (totallen > 0); | |
216 | ||
217 | kfree(buffer); | |
218 | return USB_STOR_TRANSPORT_GOOD; | |
219 | ||
220 | leave: | |
221 | kfree(buffer); | |
222 | return USB_STOR_TRANSPORT_ERROR; | |
223 | } | |
224 | ||
225 | ||
226 | static int datafab_write_data(struct us_data *us, | |
227 | struct datafab_info *info, | |
228 | u32 sector, | |
229 | u32 sectors) | |
230 | { | |
231 | unsigned char *command = us->iobuf; | |
232 | unsigned char *reply = us->iobuf; | |
233 | unsigned char *buffer; | |
234 | unsigned char thistime; | |
235 | unsigned int totallen, alloclen; | |
236 | int len, result; | |
1f6f31a0 JA |
237 | unsigned int sg_offset = 0; |
238 | struct scatterlist *sg = NULL; | |
1da177e4 LT |
239 | |
240 | // we're working in LBA mode. according to the ATA spec, | |
241 | // we can support up to 28-bit addressing. I don't know if Datafab | |
242 | // supports beyond 24-bit addressing. It's kind of hard to test | |
243 | // since it requires > 8GB CF card. | |
244 | // | |
245 | if (sectors > 0x0FFFFFFF) | |
246 | return USB_STOR_TRANSPORT_ERROR; | |
247 | ||
248 | if (info->lun == -1) { | |
249 | result = datafab_determine_lun(us, info); | |
250 | if (result != USB_STOR_TRANSPORT_GOOD) | |
251 | return result; | |
252 | } | |
253 | ||
254 | totallen = sectors * info->ssize; | |
255 | ||
256 | // Since we don't write more than 64 KB at a time, we have to create | |
257 | // a bounce buffer and move the data a piece at a time between the | |
258 | // bounce buffer and the actual transfer buffer. | |
259 | ||
260 | alloclen = min(totallen, 65536u); | |
261 | buffer = kmalloc(alloclen, GFP_NOIO); | |
262 | if (buffer == NULL) | |
263 | return USB_STOR_TRANSPORT_ERROR; | |
264 | ||
265 | do { | |
266 | // loop, never allocate or transfer more than 64k at once | |
267 | // (min(128k, 255*info->ssize) is the real limit) | |
268 | ||
269 | len = min(totallen, alloclen); | |
270 | thistime = (len / info->ssize) & 0xff; | |
271 | ||
272 | // Get the data from the transfer buffer | |
273 | usb_stor_access_xfer_buf(buffer, len, us->srb, | |
1f6f31a0 | 274 | &sg, &sg_offset, FROM_XFER_BUF); |
1da177e4 LT |
275 | |
276 | command[0] = 0; | |
277 | command[1] = thistime; | |
278 | command[2] = sector & 0xFF; | |
279 | command[3] = (sector >> 8) & 0xFF; | |
280 | command[4] = (sector >> 16) & 0xFF; | |
281 | ||
282 | command[5] = 0xE0 + (info->lun << 4); | |
283 | command[5] |= (sector >> 24) & 0x0F; | |
284 | command[6] = 0x30; | |
285 | command[7] = 0x02; | |
286 | ||
287 | // send the command | |
288 | result = datafab_bulk_write(us, command, 8); | |
289 | if (result != USB_STOR_XFER_GOOD) | |
290 | goto leave; | |
291 | ||
292 | // send the data | |
293 | result = datafab_bulk_write(us, buffer, len); | |
294 | if (result != USB_STOR_XFER_GOOD) | |
295 | goto leave; | |
296 | ||
297 | // read the result | |
298 | result = datafab_bulk_read(us, reply, 2); | |
299 | if (result != USB_STOR_XFER_GOOD) | |
300 | goto leave; | |
301 | ||
302 | if (reply[0] != 0x50 && reply[1] != 0) { | |
303 | US_DEBUGP("datafab_write_data: Gah! " | |
304 | "write return code: %02x %02x\n", | |
305 | reply[0], reply[1]); | |
306 | result = USB_STOR_TRANSPORT_ERROR; | |
307 | goto leave; | |
308 | } | |
309 | ||
310 | sector += thistime; | |
311 | totallen -= len; | |
312 | } while (totallen > 0); | |
313 | ||
314 | kfree(buffer); | |
315 | return USB_STOR_TRANSPORT_GOOD; | |
316 | ||
317 | leave: | |
318 | kfree(buffer); | |
319 | return USB_STOR_TRANSPORT_ERROR; | |
320 | } | |
321 | ||
322 | ||
323 | static int datafab_determine_lun(struct us_data *us, | |
324 | struct datafab_info *info) | |
325 | { | |
326 | // Dual-slot readers can be thought of as dual-LUN devices. | |
327 | // We need to determine which card slot is being used. | |
328 | // We'll send an IDENTIFY DEVICE command and see which LUN responds... | |
329 | // | |
330 | // There might be a better way of doing this? | |
331 | ||
332 | static unsigned char scommand[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 }; | |
333 | unsigned char *command = us->iobuf; | |
334 | unsigned char *buf; | |
335 | int count = 0, rc; | |
336 | ||
64aebe73 | 337 | if (!info) |
1da177e4 LT |
338 | return USB_STOR_TRANSPORT_ERROR; |
339 | ||
340 | memcpy(command, scommand, 8); | |
341 | buf = kmalloc(512, GFP_NOIO); | |
342 | if (!buf) | |
343 | return USB_STOR_TRANSPORT_ERROR; | |
344 | ||
345 | US_DEBUGP("datafab_determine_lun: locating...\n"); | |
346 | ||
347 | // we'll try 3 times before giving up... | |
348 | // | |
349 | while (count++ < 3) { | |
350 | command[5] = 0xa0; | |
351 | ||
352 | rc = datafab_bulk_write(us, command, 8); | |
353 | if (rc != USB_STOR_XFER_GOOD) { | |
354 | rc = USB_STOR_TRANSPORT_ERROR; | |
355 | goto leave; | |
356 | } | |
357 | ||
358 | rc = datafab_bulk_read(us, buf, 512); | |
359 | if (rc == USB_STOR_XFER_GOOD) { | |
360 | info->lun = 0; | |
361 | rc = USB_STOR_TRANSPORT_GOOD; | |
362 | goto leave; | |
363 | } | |
364 | ||
365 | command[5] = 0xb0; | |
366 | ||
367 | rc = datafab_bulk_write(us, command, 8); | |
368 | if (rc != USB_STOR_XFER_GOOD) { | |
369 | rc = USB_STOR_TRANSPORT_ERROR; | |
370 | goto leave; | |
371 | } | |
372 | ||
373 | rc = datafab_bulk_read(us, buf, 512); | |
374 | if (rc == USB_STOR_XFER_GOOD) { | |
375 | info->lun = 1; | |
376 | rc = USB_STOR_TRANSPORT_GOOD; | |
377 | goto leave; | |
378 | } | |
379 | ||
380 | msleep(20); | |
381 | } | |
382 | ||
383 | rc = USB_STOR_TRANSPORT_ERROR; | |
384 | ||
385 | leave: | |
386 | kfree(buf); | |
387 | return rc; | |
388 | } | |
389 | ||
390 | static int datafab_id_device(struct us_data *us, | |
391 | struct datafab_info *info) | |
392 | { | |
393 | // this is a variation of the ATA "IDENTIFY DEVICE" command...according | |
394 | // to the ATA spec, 'Sector Count' isn't used but the Windows driver | |
395 | // sets this bit so we do too... | |
396 | // | |
397 | static unsigned char scommand[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 }; | |
398 | unsigned char *command = us->iobuf; | |
399 | unsigned char *reply; | |
400 | int rc; | |
401 | ||
64aebe73 | 402 | if (!info) |
1da177e4 LT |
403 | return USB_STOR_TRANSPORT_ERROR; |
404 | ||
405 | if (info->lun == -1) { | |
406 | rc = datafab_determine_lun(us, info); | |
407 | if (rc != USB_STOR_TRANSPORT_GOOD) | |
408 | return rc; | |
409 | } | |
410 | ||
411 | memcpy(command, scommand, 8); | |
412 | reply = kmalloc(512, GFP_NOIO); | |
413 | if (!reply) | |
414 | return USB_STOR_TRANSPORT_ERROR; | |
415 | ||
416 | command[5] += (info->lun << 4); | |
417 | ||
418 | rc = datafab_bulk_write(us, command, 8); | |
419 | if (rc != USB_STOR_XFER_GOOD) { | |
420 | rc = USB_STOR_TRANSPORT_ERROR; | |
421 | goto leave; | |
422 | } | |
423 | ||
424 | // we'll go ahead and extract the media capacity while we're here... | |
425 | // | |
426 | rc = datafab_bulk_read(us, reply, 512); | |
427 | if (rc == USB_STOR_XFER_GOOD) { | |
428 | // capacity is at word offset 57-58 | |
429 | // | |
430 | info->sectors = ((u32)(reply[117]) << 24) | | |
431 | ((u32)(reply[116]) << 16) | | |
432 | ((u32)(reply[115]) << 8) | | |
433 | ((u32)(reply[114]) ); | |
434 | rc = USB_STOR_TRANSPORT_GOOD; | |
435 | goto leave; | |
436 | } | |
437 | ||
438 | rc = USB_STOR_TRANSPORT_ERROR; | |
439 | ||
440 | leave: | |
441 | kfree(reply); | |
442 | return rc; | |
443 | } | |
444 | ||
445 | ||
446 | static int datafab_handle_mode_sense(struct us_data *us, | |
447 | struct scsi_cmnd * srb, | |
448 | int sense_6) | |
449 | { | |
450 | static unsigned char rw_err_page[12] = { | |
451 | 0x1, 0xA, 0x21, 1, 0, 0, 0, 0, 1, 0, 0, 0 | |
452 | }; | |
453 | static unsigned char cache_page[12] = { | |
454 | 0x8, 0xA, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0 | |
455 | }; | |
456 | static unsigned char rbac_page[12] = { | |
457 | 0x1B, 0xA, 0, 0x81, 0, 0, 0, 0, 0, 0, 0, 0 | |
458 | }; | |
459 | static unsigned char timer_page[8] = { | |
460 | 0x1C, 0x6, 0, 0, 0, 0 | |
461 | }; | |
462 | unsigned char pc, page_code; | |
463 | unsigned int i = 0; | |
464 | struct datafab_info *info = (struct datafab_info *) (us->extra); | |
465 | unsigned char *ptr = us->iobuf; | |
466 | ||
467 | // most of this stuff is just a hack to get things working. the | |
468 | // datafab reader doesn't present a SCSI interface so we | |
469 | // fudge the SCSI commands... | |
470 | // | |
471 | ||
472 | pc = srb->cmnd[2] >> 6; | |
473 | page_code = srb->cmnd[2] & 0x3F; | |
474 | ||
475 | switch (pc) { | |
476 | case 0x0: | |
477 | US_DEBUGP("datafab_handle_mode_sense: Current values\n"); | |
478 | break; | |
479 | case 0x1: | |
480 | US_DEBUGP("datafab_handle_mode_sense: Changeable values\n"); | |
481 | break; | |
482 | case 0x2: | |
483 | US_DEBUGP("datafab_handle_mode_sense: Default values\n"); | |
484 | break; | |
485 | case 0x3: | |
486 | US_DEBUGP("datafab_handle_mode_sense: Saves values\n"); | |
487 | break; | |
488 | } | |
489 | ||
490 | memset(ptr, 0, 8); | |
491 | if (sense_6) { | |
492 | ptr[2] = 0x00; // WP enable: 0x80 | |
493 | i = 4; | |
494 | } else { | |
495 | ptr[3] = 0x00; // WP enable: 0x80 | |
496 | i = 8; | |
497 | } | |
498 | ||
499 | switch (page_code) { | |
500 | default: | |
501 | // vendor-specific mode | |
502 | info->sense_key = 0x05; | |
503 | info->sense_asc = 0x24; | |
504 | info->sense_ascq = 0x00; | |
505 | return USB_STOR_TRANSPORT_FAILED; | |
506 | ||
507 | case 0x1: | |
508 | memcpy(ptr + i, rw_err_page, sizeof(rw_err_page)); | |
509 | i += sizeof(rw_err_page); | |
510 | break; | |
511 | ||
512 | case 0x8: | |
513 | memcpy(ptr + i, cache_page, sizeof(cache_page)); | |
514 | i += sizeof(cache_page); | |
515 | break; | |
516 | ||
517 | case 0x1B: | |
518 | memcpy(ptr + i, rbac_page, sizeof(rbac_page)); | |
519 | i += sizeof(rbac_page); | |
520 | break; | |
521 | ||
522 | case 0x1C: | |
523 | memcpy(ptr + i, timer_page, sizeof(timer_page)); | |
524 | i += sizeof(timer_page); | |
525 | break; | |
526 | ||
527 | case 0x3F: // retrieve all pages | |
528 | memcpy(ptr + i, timer_page, sizeof(timer_page)); | |
529 | i += sizeof(timer_page); | |
530 | memcpy(ptr + i, rbac_page, sizeof(rbac_page)); | |
531 | i += sizeof(rbac_page); | |
532 | memcpy(ptr + i, cache_page, sizeof(cache_page)); | |
533 | i += sizeof(cache_page); | |
534 | memcpy(ptr + i, rw_err_page, sizeof(rw_err_page)); | |
535 | i += sizeof(rw_err_page); | |
536 | break; | |
537 | } | |
538 | ||
539 | if (sense_6) | |
540 | ptr[0] = i - 1; | |
541 | else | |
542 | ((__be16 *) ptr)[0] = cpu_to_be16(i - 2); | |
543 | usb_stor_set_xfer_buf(ptr, i, srb); | |
544 | ||
545 | return USB_STOR_TRANSPORT_GOOD; | |
546 | } | |
547 | ||
548 | static void datafab_info_destructor(void *extra) | |
549 | { | |
550 | // this routine is a placeholder... | |
551 | // currently, we don't allocate any extra memory so we're okay | |
552 | } | |
553 | ||
554 | ||
555 | // Transport for the Datafab MDCFE-B | |
556 | // | |
2cbbf357 | 557 | static int datafab_transport(struct scsi_cmnd *srb, struct us_data *us) |
1da177e4 LT |
558 | { |
559 | struct datafab_info *info; | |
560 | int rc; | |
561 | unsigned long block, blocks; | |
562 | unsigned char *ptr = us->iobuf; | |
563 | static unsigned char inquiry_reply[8] = { | |
564 | 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00 | |
565 | }; | |
566 | ||
567 | if (!us->extra) { | |
887c2560 | 568 | us->extra = kzalloc(sizeof(struct datafab_info), GFP_NOIO); |
1da177e4 LT |
569 | if (!us->extra) { |
570 | US_DEBUGP("datafab_transport: Gah! " | |
571 | "Can't allocate storage for Datafab info struct!\n"); | |
572 | return USB_STOR_TRANSPORT_ERROR; | |
573 | } | |
1da177e4 LT |
574 | us->extra_destructor = datafab_info_destructor; |
575 | ((struct datafab_info *)us->extra)->lun = -1; | |
576 | } | |
577 | ||
578 | info = (struct datafab_info *) (us->extra); | |
579 | ||
580 | if (srb->cmnd[0] == INQUIRY) { | |
581 | US_DEBUGP("datafab_transport: INQUIRY. Returning bogus response"); | |
582 | memcpy(ptr, inquiry_reply, sizeof(inquiry_reply)); | |
583 | fill_inquiry_response(us, ptr, 36); | |
584 | return USB_STOR_TRANSPORT_GOOD; | |
585 | } | |
586 | ||
587 | if (srb->cmnd[0] == READ_CAPACITY) { | |
588 | info->ssize = 0x200; // hard coded 512 byte sectors as per ATA spec | |
589 | rc = datafab_id_device(us, info); | |
590 | if (rc != USB_STOR_TRANSPORT_GOOD) | |
591 | return rc; | |
592 | ||
593 | US_DEBUGP("datafab_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n", | |
594 | info->sectors, info->ssize); | |
595 | ||
596 | // build the reply | |
597 | // we need the last sector, not the number of sectors | |
598 | ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1); | |
599 | ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize); | |
600 | usb_stor_set_xfer_buf(ptr, 8, srb); | |
601 | ||
602 | return USB_STOR_TRANSPORT_GOOD; | |
603 | } | |
604 | ||
605 | if (srb->cmnd[0] == MODE_SELECT_10) { | |
606 | US_DEBUGP("datafab_transport: Gah! MODE_SELECT_10.\n"); | |
607 | return USB_STOR_TRANSPORT_ERROR; | |
608 | } | |
609 | ||
610 | // don't bother implementing READ_6 or WRITE_6. | |
611 | // | |
612 | if (srb->cmnd[0] == READ_10) { | |
613 | block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) | | |
614 | ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5])); | |
615 | ||
616 | blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8])); | |
617 | ||
618 | US_DEBUGP("datafab_transport: READ_10: read block 0x%04lx count %ld\n", block, blocks); | |
619 | return datafab_read_data(us, info, block, blocks); | |
620 | } | |
621 | ||
622 | if (srb->cmnd[0] == READ_12) { | |
623 | // we'll probably never see a READ_12 but we'll do it anyway... | |
624 | // | |
625 | block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) | | |
626 | ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5])); | |
627 | ||
628 | blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) | | |
629 | ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9])); | |
630 | ||
631 | US_DEBUGP("datafab_transport: READ_12: read block 0x%04lx count %ld\n", block, blocks); | |
632 | return datafab_read_data(us, info, block, blocks); | |
633 | } | |
634 | ||
635 | if (srb->cmnd[0] == WRITE_10) { | |
636 | block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) | | |
637 | ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5])); | |
638 | ||
639 | blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8])); | |
640 | ||
641 | US_DEBUGP("datafab_transport: WRITE_10: write block 0x%04lx count %ld\n", block, blocks); | |
642 | return datafab_write_data(us, info, block, blocks); | |
643 | } | |
644 | ||
645 | if (srb->cmnd[0] == WRITE_12) { | |
646 | // we'll probably never see a WRITE_12 but we'll do it anyway... | |
647 | // | |
648 | block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) | | |
649 | ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5])); | |
650 | ||
651 | blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) | | |
652 | ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9])); | |
653 | ||
654 | US_DEBUGP("datafab_transport: WRITE_12: write block 0x%04lx count %ld\n", block, blocks); | |
655 | return datafab_write_data(us, info, block, blocks); | |
656 | } | |
657 | ||
658 | if (srb->cmnd[0] == TEST_UNIT_READY) { | |
659 | US_DEBUGP("datafab_transport: TEST_UNIT_READY.\n"); | |
660 | return datafab_id_device(us, info); | |
661 | } | |
662 | ||
663 | if (srb->cmnd[0] == REQUEST_SENSE) { | |
664 | US_DEBUGP("datafab_transport: REQUEST_SENSE. Returning faked response\n"); | |
665 | ||
666 | // this response is pretty bogus right now. eventually if necessary | |
667 | // we can set the correct sense data. so far though it hasn't been | |
668 | // necessary | |
669 | // | |
670 | memset(ptr, 0, 18); | |
671 | ptr[0] = 0xF0; | |
672 | ptr[2] = info->sense_key; | |
673 | ptr[7] = 11; | |
674 | ptr[12] = info->sense_asc; | |
675 | ptr[13] = info->sense_ascq; | |
676 | usb_stor_set_xfer_buf(ptr, 18, srb); | |
677 | ||
678 | return USB_STOR_TRANSPORT_GOOD; | |
679 | } | |
680 | ||
681 | if (srb->cmnd[0] == MODE_SENSE) { | |
682 | US_DEBUGP("datafab_transport: MODE_SENSE_6 detected\n"); | |
683 | return datafab_handle_mode_sense(us, srb, 1); | |
684 | } | |
685 | ||
686 | if (srb->cmnd[0] == MODE_SENSE_10) { | |
687 | US_DEBUGP("datafab_transport: MODE_SENSE_10 detected\n"); | |
688 | return datafab_handle_mode_sense(us, srb, 0); | |
689 | } | |
690 | ||
691 | if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) { | |
692 | // sure. whatever. not like we can stop the user from | |
693 | // popping the media out of the device (no locking doors, etc) | |
694 | // | |
695 | return USB_STOR_TRANSPORT_GOOD; | |
696 | } | |
697 | ||
698 | if (srb->cmnd[0] == START_STOP) { | |
699 | /* this is used by sd.c'check_scsidisk_media_change to detect | |
700 | media change */ | |
701 | US_DEBUGP("datafab_transport: START_STOP.\n"); | |
702 | /* the first datafab_id_device after a media change returns | |
703 | an error (determined experimentally) */ | |
704 | rc = datafab_id_device(us, info); | |
705 | if (rc == USB_STOR_TRANSPORT_GOOD) { | |
706 | info->sense_key = NO_SENSE; | |
707 | srb->result = SUCCESS; | |
708 | } else { | |
709 | info->sense_key = UNIT_ATTENTION; | |
710 | srb->result = SAM_STAT_CHECK_CONDITION; | |
711 | } | |
712 | return rc; | |
713 | } | |
714 | ||
715 | US_DEBUGP("datafab_transport: Gah! Unknown command: %d (0x%x)\n", | |
716 | srb->cmnd[0], srb->cmnd[0]); | |
717 | info->sense_key = 0x05; | |
718 | info->sense_asc = 0x20; | |
719 | info->sense_ascq = 0x00; | |
720 | return USB_STOR_TRANSPORT_FAILED; | |
721 | } | |
2cbbf357 AS |
722 | |
723 | static int datafab_probe(struct usb_interface *intf, | |
724 | const struct usb_device_id *id) | |
725 | { | |
726 | struct us_data *us; | |
727 | int result; | |
728 | ||
729 | result = usb_stor_probe1(&us, intf, id, | |
730 | (id - datafab_usb_ids) + datafab_unusual_dev_list); | |
731 | if (result) | |
732 | return result; | |
733 | ||
734 | us->transport_name = "Datafab Bulk-Only"; | |
735 | us->transport = datafab_transport; | |
736 | us->transport_reset = usb_stor_Bulk_reset; | |
737 | us->max_lun = 1; | |
738 | ||
739 | result = usb_stor_probe2(us); | |
740 | return result; | |
741 | } | |
742 | ||
743 | static struct usb_driver datafab_driver = { | |
744 | .name = "ums-datafab", | |
745 | .probe = datafab_probe, | |
746 | .disconnect = usb_stor_disconnect, | |
747 | .suspend = usb_stor_suspend, | |
748 | .resume = usb_stor_resume, | |
749 | .reset_resume = usb_stor_reset_resume, | |
750 | .pre_reset = usb_stor_pre_reset, | |
751 | .post_reset = usb_stor_post_reset, | |
752 | .id_table = datafab_usb_ids, | |
753 | .soft_unbind = 1, | |
754 | }; | |
755 | ||
756 | static int __init datafab_init(void) | |
757 | { | |
758 | return usb_register(&datafab_driver); | |
759 | } | |
760 | ||
761 | static void __exit datafab_exit(void) | |
762 | { | |
763 | usb_deregister(&datafab_driver); | |
764 | } | |
765 | ||
766 | module_init(datafab_init); | |
767 | module_exit(datafab_exit); |