datafab.c 19 KB

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