datafab.c 20 KB

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