datafab.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. /* Driver for Datafab USB Compact Flash reader
  2. *
  3. * $Id: datafab.c,v 1.7 2002/02/25 00:40:13 mdharm Exp $
  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. * This program is free software; you can redistribute it and/or modify it
  24. * under the terms of the GNU General Public License as published by the
  25. * Free Software Foundation; either version 2, or (at your option) any
  26. * later version.
  27. *
  28. * This program is distributed in the hope that it will be useful, but
  29. * WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  31. * General Public License for more details.
  32. *
  33. * You should have received a copy of the GNU General Public License along
  34. * with this program; if not, write to the Free Software Foundation, Inc.,
  35. * 675 Mass Ave, Cambridge, MA 02139, USA.
  36. */
  37. /*
  38. * This driver attempts to support USB CompactFlash reader/writer devices
  39. * based on Datafab USB-to-ATA chips. It was specifically developed for the
  40. * Datafab MDCFE-B USB CompactFlash reader but has since been found to work
  41. * with a variety of Datafab-based devices from a number of manufacturers.
  42. * I've received a report of this driver working with a Datafab-based
  43. * SmartMedia device though please be aware that I'm personally unable to
  44. * test SmartMedia support.
  45. *
  46. * This driver supports reading and writing. If you're truly paranoid,
  47. * however, you can force the driver into a write-protected state by setting
  48. * the WP enable bits in datafab_handle_mode_sense(). See the comments
  49. * in that routine.
  50. */
  51. #include <linux/sched.h>
  52. #include <linux/errno.h>
  53. #include <linux/slab.h>
  54. #include <scsi/scsi.h>
  55. #include <scsi/scsi_cmnd.h>
  56. #include "usb.h"
  57. #include "transport.h"
  58. #include "protocol.h"
  59. #include "debug.h"
  60. #include "datafab.h"
  61. static int datafab_determine_lun(struct us_data *us,
  62. struct datafab_info *info);
  63. static inline int
  64. datafab_bulk_read(struct us_data *us, unsigned char *data, unsigned int len) {
  65. if (len == 0)
  66. return USB_STOR_XFER_GOOD;
  67. US_DEBUGP("datafab_bulk_read: len = %d\n", len);
  68. return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  69. data, len, NULL);
  70. }
  71. static inline int
  72. datafab_bulk_write(struct us_data *us, unsigned char *data, unsigned int len) {
  73. if (len == 0)
  74. return USB_STOR_XFER_GOOD;
  75. US_DEBUGP("datafab_bulk_write: len = %d\n", len);
  76. return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  77. data, len, NULL);
  78. }
  79. static int datafab_read_data(struct us_data *us,
  80. struct datafab_info *info,
  81. u32 sector,
  82. u32 sectors)
  83. {
  84. unsigned char *command = us->iobuf;
  85. unsigned char *buffer;
  86. unsigned char thistime;
  87. unsigned int totallen, alloclen;
  88. int len, result;
  89. unsigned int sg_idx = 0, sg_offset = 0;
  90. // we're working in LBA mode. according to the ATA spec,
  91. // we can support up to 28-bit addressing. I don't know if Datafab
  92. // supports beyond 24-bit addressing. It's kind of hard to test
  93. // since it requires > 8GB CF card.
  94. //
  95. if (sectors > 0x0FFFFFFF)
  96. return USB_STOR_TRANSPORT_ERROR;
  97. if (info->lun == -1) {
  98. result = datafab_determine_lun(us, info);
  99. if (result != USB_STOR_TRANSPORT_GOOD)
  100. return result;
  101. }
  102. totallen = sectors * info->ssize;
  103. // Since we don't read more than 64 KB at a time, we have to create
  104. // a bounce buffer and move the data a piece at a time between the
  105. // bounce buffer and the actual transfer buffer.
  106. alloclen = min(totallen, 65536u);
  107. buffer = kmalloc(alloclen, GFP_NOIO);
  108. if (buffer == NULL)
  109. return USB_STOR_TRANSPORT_ERROR;
  110. do {
  111. // loop, never allocate or transfer more than 64k at once
  112. // (min(128k, 255*info->ssize) is the real limit)
  113. len = min(totallen, alloclen);
  114. thistime = (len / info->ssize) & 0xff;
  115. command[0] = 0;
  116. command[1] = thistime;
  117. command[2] = sector & 0xFF;
  118. command[3] = (sector >> 8) & 0xFF;
  119. command[4] = (sector >> 16) & 0xFF;
  120. command[5] = 0xE0 + (info->lun << 4);
  121. command[5] |= (sector >> 24) & 0x0F;
  122. command[6] = 0x20;
  123. command[7] = 0x01;
  124. // send the read command
  125. result = datafab_bulk_write(us, command, 8);
  126. if (result != USB_STOR_XFER_GOOD)
  127. goto leave;
  128. // read the result
  129. result = datafab_bulk_read(us, buffer, len);
  130. if (result != USB_STOR_XFER_GOOD)
  131. goto leave;
  132. // Store the data in the transfer buffer
  133. usb_stor_access_xfer_buf(buffer, len, us->srb,
  134. &sg_idx, &sg_offset, TO_XFER_BUF);
  135. sector += thistime;
  136. totallen -= len;
  137. } while (totallen > 0);
  138. kfree(buffer);
  139. return USB_STOR_TRANSPORT_GOOD;
  140. leave:
  141. kfree(buffer);
  142. return USB_STOR_TRANSPORT_ERROR;
  143. }
  144. static int datafab_write_data(struct us_data *us,
  145. struct datafab_info *info,
  146. u32 sector,
  147. u32 sectors)
  148. {
  149. unsigned char *command = us->iobuf;
  150. unsigned char *reply = us->iobuf;
  151. unsigned char *buffer;
  152. unsigned char thistime;
  153. unsigned int totallen, alloclen;
  154. int len, result;
  155. unsigned int sg_idx = 0, sg_offset = 0;
  156. // we're working in LBA mode. according to the ATA spec,
  157. // we can support up to 28-bit addressing. I don't know if Datafab
  158. // supports beyond 24-bit addressing. It's kind of hard to test
  159. // since it requires > 8GB CF card.
  160. //
  161. if (sectors > 0x0FFFFFFF)
  162. return USB_STOR_TRANSPORT_ERROR;
  163. if (info->lun == -1) {
  164. result = datafab_determine_lun(us, info);
  165. if (result != USB_STOR_TRANSPORT_GOOD)
  166. return result;
  167. }
  168. totallen = sectors * info->ssize;
  169. // Since we don't write more than 64 KB at a time, we have to create
  170. // a bounce buffer and move the data a piece at a time between the
  171. // bounce buffer and the actual transfer buffer.
  172. alloclen = min(totallen, 65536u);
  173. buffer = kmalloc(alloclen, GFP_NOIO);
  174. if (buffer == NULL)
  175. return USB_STOR_TRANSPORT_ERROR;
  176. do {
  177. // loop, never allocate or transfer more than 64k at once
  178. // (min(128k, 255*info->ssize) is the real limit)
  179. len = min(totallen, alloclen);
  180. thistime = (len / info->ssize) & 0xff;
  181. // Get the data from the transfer buffer
  182. usb_stor_access_xfer_buf(buffer, len, us->srb,
  183. &sg_idx, &sg_offset, FROM_XFER_BUF);
  184. command[0] = 0;
  185. command[1] = thistime;
  186. command[2] = sector & 0xFF;
  187. command[3] = (sector >> 8) & 0xFF;
  188. command[4] = (sector >> 16) & 0xFF;
  189. command[5] = 0xE0 + (info->lun << 4);
  190. command[5] |= (sector >> 24) & 0x0F;
  191. command[6] = 0x30;
  192. command[7] = 0x02;
  193. // send the command
  194. result = datafab_bulk_write(us, command, 8);
  195. if (result != USB_STOR_XFER_GOOD)
  196. goto leave;
  197. // send the data
  198. result = datafab_bulk_write(us, buffer, len);
  199. if (result != USB_STOR_XFER_GOOD)
  200. goto leave;
  201. // read the result
  202. result = datafab_bulk_read(us, reply, 2);
  203. if (result != USB_STOR_XFER_GOOD)
  204. goto leave;
  205. if (reply[0] != 0x50 && reply[1] != 0) {
  206. US_DEBUGP("datafab_write_data: Gah! "
  207. "write return code: %02x %02x\n",
  208. reply[0], reply[1]);
  209. result = USB_STOR_TRANSPORT_ERROR;
  210. goto leave;
  211. }
  212. sector += thistime;
  213. totallen -= len;
  214. } while (totallen > 0);
  215. kfree(buffer);
  216. return USB_STOR_TRANSPORT_GOOD;
  217. leave:
  218. kfree(buffer);
  219. return USB_STOR_TRANSPORT_ERROR;
  220. }
  221. static int datafab_determine_lun(struct us_data *us,
  222. struct datafab_info *info)
  223. {
  224. // Dual-slot readers can be thought of as dual-LUN devices.
  225. // We need to determine which card slot is being used.
  226. // We'll send an IDENTIFY DEVICE command and see which LUN responds...
  227. //
  228. // There might be a better way of doing this?
  229. static unsigned char scommand[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 };
  230. unsigned char *command = us->iobuf;
  231. unsigned char *buf;
  232. int count = 0, rc;
  233. if (!us || !info)
  234. return USB_STOR_TRANSPORT_ERROR;
  235. memcpy(command, scommand, 8);
  236. buf = kmalloc(512, GFP_NOIO);
  237. if (!buf)
  238. return USB_STOR_TRANSPORT_ERROR;
  239. US_DEBUGP("datafab_determine_lun: locating...\n");
  240. // we'll try 3 times before giving up...
  241. //
  242. while (count++ < 3) {
  243. command[5] = 0xa0;
  244. rc = datafab_bulk_write(us, command, 8);
  245. if (rc != USB_STOR_XFER_GOOD) {
  246. rc = USB_STOR_TRANSPORT_ERROR;
  247. goto leave;
  248. }
  249. rc = datafab_bulk_read(us, buf, 512);
  250. if (rc == USB_STOR_XFER_GOOD) {
  251. info->lun = 0;
  252. rc = USB_STOR_TRANSPORT_GOOD;
  253. goto leave;
  254. }
  255. command[5] = 0xb0;
  256. rc = datafab_bulk_write(us, command, 8);
  257. if (rc != USB_STOR_XFER_GOOD) {
  258. rc = USB_STOR_TRANSPORT_ERROR;
  259. goto leave;
  260. }
  261. rc = datafab_bulk_read(us, buf, 512);
  262. if (rc == USB_STOR_XFER_GOOD) {
  263. info->lun = 1;
  264. rc = USB_STOR_TRANSPORT_GOOD;
  265. goto leave;
  266. }
  267. msleep(20);
  268. }
  269. rc = USB_STOR_TRANSPORT_ERROR;
  270. leave:
  271. kfree(buf);
  272. return rc;
  273. }
  274. static int datafab_id_device(struct us_data *us,
  275. struct datafab_info *info)
  276. {
  277. // this is a variation of the ATA "IDENTIFY DEVICE" command...according
  278. // to the ATA spec, 'Sector Count' isn't used but the Windows driver
  279. // sets this bit so we do too...
  280. //
  281. static unsigned char scommand[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 };
  282. unsigned char *command = us->iobuf;
  283. unsigned char *reply;
  284. int rc;
  285. if (!us || !info)
  286. return USB_STOR_TRANSPORT_ERROR;
  287. if (info->lun == -1) {
  288. rc = datafab_determine_lun(us, info);
  289. if (rc != USB_STOR_TRANSPORT_GOOD)
  290. return rc;
  291. }
  292. memcpy(command, scommand, 8);
  293. reply = kmalloc(512, GFP_NOIO);
  294. if (!reply)
  295. return USB_STOR_TRANSPORT_ERROR;
  296. command[5] += (info->lun << 4);
  297. rc = datafab_bulk_write(us, command, 8);
  298. if (rc != USB_STOR_XFER_GOOD) {
  299. rc = USB_STOR_TRANSPORT_ERROR;
  300. goto leave;
  301. }
  302. // we'll go ahead and extract the media capacity while we're here...
  303. //
  304. rc = datafab_bulk_read(us, reply, 512);
  305. if (rc == USB_STOR_XFER_GOOD) {
  306. // capacity is at word offset 57-58
  307. //
  308. info->sectors = ((u32)(reply[117]) << 24) |
  309. ((u32)(reply[116]) << 16) |
  310. ((u32)(reply[115]) << 8) |
  311. ((u32)(reply[114]) );
  312. rc = USB_STOR_TRANSPORT_GOOD;
  313. goto leave;
  314. }
  315. rc = USB_STOR_TRANSPORT_ERROR;
  316. leave:
  317. kfree(reply);
  318. return rc;
  319. }
  320. static int datafab_handle_mode_sense(struct us_data *us,
  321. struct scsi_cmnd * srb,
  322. int sense_6)
  323. {
  324. static unsigned char rw_err_page[12] = {
  325. 0x1, 0xA, 0x21, 1, 0, 0, 0, 0, 1, 0, 0, 0
  326. };
  327. static unsigned char cache_page[12] = {
  328. 0x8, 0xA, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0
  329. };
  330. static unsigned char rbac_page[12] = {
  331. 0x1B, 0xA, 0, 0x81, 0, 0, 0, 0, 0, 0, 0, 0
  332. };
  333. static unsigned char timer_page[8] = {
  334. 0x1C, 0x6, 0, 0, 0, 0
  335. };
  336. unsigned char pc, page_code;
  337. unsigned int i = 0;
  338. struct datafab_info *info = (struct datafab_info *) (us->extra);
  339. unsigned char *ptr = us->iobuf;
  340. // most of this stuff is just a hack to get things working. the
  341. // datafab reader doesn't present a SCSI interface so we
  342. // fudge the SCSI commands...
  343. //
  344. pc = srb->cmnd[2] >> 6;
  345. page_code = srb->cmnd[2] & 0x3F;
  346. switch (pc) {
  347. case 0x0:
  348. US_DEBUGP("datafab_handle_mode_sense: Current values\n");
  349. break;
  350. case 0x1:
  351. US_DEBUGP("datafab_handle_mode_sense: Changeable values\n");
  352. break;
  353. case 0x2:
  354. US_DEBUGP("datafab_handle_mode_sense: Default values\n");
  355. break;
  356. case 0x3:
  357. US_DEBUGP("datafab_handle_mode_sense: Saves values\n");
  358. break;
  359. }
  360. memset(ptr, 0, 8);
  361. if (sense_6) {
  362. ptr[2] = 0x00; // WP enable: 0x80
  363. i = 4;
  364. } else {
  365. ptr[3] = 0x00; // WP enable: 0x80
  366. i = 8;
  367. }
  368. switch (page_code) {
  369. default:
  370. // vendor-specific mode
  371. info->sense_key = 0x05;
  372. info->sense_asc = 0x24;
  373. info->sense_ascq = 0x00;
  374. return USB_STOR_TRANSPORT_FAILED;
  375. case 0x1:
  376. memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
  377. i += sizeof(rw_err_page);
  378. break;
  379. case 0x8:
  380. memcpy(ptr + i, cache_page, sizeof(cache_page));
  381. i += sizeof(cache_page);
  382. break;
  383. case 0x1B:
  384. memcpy(ptr + i, rbac_page, sizeof(rbac_page));
  385. i += sizeof(rbac_page);
  386. break;
  387. case 0x1C:
  388. memcpy(ptr + i, timer_page, sizeof(timer_page));
  389. i += sizeof(timer_page);
  390. break;
  391. case 0x3F: // retrieve all pages
  392. memcpy(ptr + i, timer_page, sizeof(timer_page));
  393. i += sizeof(timer_page);
  394. memcpy(ptr + i, rbac_page, sizeof(rbac_page));
  395. i += sizeof(rbac_page);
  396. memcpy(ptr + i, cache_page, sizeof(cache_page));
  397. i += sizeof(cache_page);
  398. memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
  399. i += sizeof(rw_err_page);
  400. break;
  401. }
  402. if (sense_6)
  403. ptr[0] = i - 1;
  404. else
  405. ((__be16 *) ptr)[0] = cpu_to_be16(i - 2);
  406. usb_stor_set_xfer_buf(ptr, i, srb);
  407. return USB_STOR_TRANSPORT_GOOD;
  408. }
  409. static void datafab_info_destructor(void *extra)
  410. {
  411. // this routine is a placeholder...
  412. // currently, we don't allocate any extra memory so we're okay
  413. }
  414. // Transport for the Datafab MDCFE-B
  415. //
  416. int datafab_transport(struct scsi_cmnd * srb, struct us_data *us)
  417. {
  418. struct datafab_info *info;
  419. int rc;
  420. unsigned long block, blocks;
  421. unsigned char *ptr = us->iobuf;
  422. static unsigned char inquiry_reply[8] = {
  423. 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
  424. };
  425. if (!us->extra) {
  426. us->extra = kmalloc(sizeof(struct datafab_info), GFP_NOIO);
  427. if (!us->extra) {
  428. US_DEBUGP("datafab_transport: Gah! "
  429. "Can't allocate storage for Datafab info struct!\n");
  430. return USB_STOR_TRANSPORT_ERROR;
  431. }
  432. memset(us->extra, 0, sizeof(struct datafab_info));
  433. us->extra_destructor = datafab_info_destructor;
  434. ((struct datafab_info *)us->extra)->lun = -1;
  435. }
  436. info = (struct datafab_info *) (us->extra);
  437. if (srb->cmnd[0] == INQUIRY) {
  438. US_DEBUGP("datafab_transport: INQUIRY. Returning bogus response");
  439. memcpy(ptr, inquiry_reply, sizeof(inquiry_reply));
  440. fill_inquiry_response(us, ptr, 36);
  441. return USB_STOR_TRANSPORT_GOOD;
  442. }
  443. if (srb->cmnd[0] == READ_CAPACITY) {
  444. info->ssize = 0x200; // hard coded 512 byte sectors as per ATA spec
  445. rc = datafab_id_device(us, info);
  446. if (rc != USB_STOR_TRANSPORT_GOOD)
  447. return rc;
  448. US_DEBUGP("datafab_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
  449. info->sectors, info->ssize);
  450. // build the reply
  451. // we need the last sector, not the number of sectors
  452. ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
  453. ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
  454. usb_stor_set_xfer_buf(ptr, 8, srb);
  455. return USB_STOR_TRANSPORT_GOOD;
  456. }
  457. if (srb->cmnd[0] == MODE_SELECT_10) {
  458. US_DEBUGP("datafab_transport: Gah! MODE_SELECT_10.\n");
  459. return USB_STOR_TRANSPORT_ERROR;
  460. }
  461. // don't bother implementing READ_6 or WRITE_6.
  462. //
  463. if (srb->cmnd[0] == READ_10) {
  464. block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
  465. ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
  466. blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
  467. US_DEBUGP("datafab_transport: READ_10: read block 0x%04lx count %ld\n", block, blocks);
  468. return datafab_read_data(us, info, block, blocks);
  469. }
  470. if (srb->cmnd[0] == READ_12) {
  471. // we'll probably never see a READ_12 but we'll do it anyway...
  472. //
  473. block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
  474. ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
  475. blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
  476. ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
  477. US_DEBUGP("datafab_transport: READ_12: read block 0x%04lx count %ld\n", block, blocks);
  478. return datafab_read_data(us, info, block, blocks);
  479. }
  480. if (srb->cmnd[0] == WRITE_10) {
  481. block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
  482. ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
  483. blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
  484. US_DEBUGP("datafab_transport: WRITE_10: write block 0x%04lx count %ld\n", block, blocks);
  485. return datafab_write_data(us, info, block, blocks);
  486. }
  487. if (srb->cmnd[0] == WRITE_12) {
  488. // we'll probably never see a WRITE_12 but we'll do it anyway...
  489. //
  490. block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
  491. ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
  492. blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
  493. ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
  494. US_DEBUGP("datafab_transport: WRITE_12: write block 0x%04lx count %ld\n", block, blocks);
  495. return datafab_write_data(us, info, block, blocks);
  496. }
  497. if (srb->cmnd[0] == TEST_UNIT_READY) {
  498. US_DEBUGP("datafab_transport: TEST_UNIT_READY.\n");
  499. return datafab_id_device(us, info);
  500. }
  501. if (srb->cmnd[0] == REQUEST_SENSE) {
  502. US_DEBUGP("datafab_transport: REQUEST_SENSE. Returning faked response\n");
  503. // this response is pretty bogus right now. eventually if necessary
  504. // we can set the correct sense data. so far though it hasn't been
  505. // necessary
  506. //
  507. memset(ptr, 0, 18);
  508. ptr[0] = 0xF0;
  509. ptr[2] = info->sense_key;
  510. ptr[7] = 11;
  511. ptr[12] = info->sense_asc;
  512. ptr[13] = info->sense_ascq;
  513. usb_stor_set_xfer_buf(ptr, 18, srb);
  514. return USB_STOR_TRANSPORT_GOOD;
  515. }
  516. if (srb->cmnd[0] == MODE_SENSE) {
  517. US_DEBUGP("datafab_transport: MODE_SENSE_6 detected\n");
  518. return datafab_handle_mode_sense(us, srb, 1);
  519. }
  520. if (srb->cmnd[0] == MODE_SENSE_10) {
  521. US_DEBUGP("datafab_transport: MODE_SENSE_10 detected\n");
  522. return datafab_handle_mode_sense(us, srb, 0);
  523. }
  524. if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
  525. // sure. whatever. not like we can stop the user from
  526. // popping the media out of the device (no locking doors, etc)
  527. //
  528. return USB_STOR_TRANSPORT_GOOD;
  529. }
  530. if (srb->cmnd[0] == START_STOP) {
  531. /* this is used by sd.c'check_scsidisk_media_change to detect
  532. media change */
  533. US_DEBUGP("datafab_transport: START_STOP.\n");
  534. /* the first datafab_id_device after a media change returns
  535. an error (determined experimentally) */
  536. rc = datafab_id_device(us, info);
  537. if (rc == USB_STOR_TRANSPORT_GOOD) {
  538. info->sense_key = NO_SENSE;
  539. srb->result = SUCCESS;
  540. } else {
  541. info->sense_key = UNIT_ATTENTION;
  542. srb->result = SAM_STAT_CHECK_CONDITION;
  543. }
  544. return rc;
  545. }
  546. US_DEBUGP("datafab_transport: Gah! Unknown command: %d (0x%x)\n",
  547. srb->cmnd[0], srb->cmnd[0]);
  548. info->sense_key = 0x05;
  549. info->sense_asc = 0x20;
  550. info->sense_ascq = 0x00;
  551. return USB_STOR_TRANSPORT_FAILED;
  552. }