datafab.c 20 KB

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