datafab.c 20 KB

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