datafab.c 19 KB

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