jumpshot.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /* Driver for Lexar "Jumpshot" Compact Flash reader
  2. *
  3. * jumpshot driver v0.1:
  4. *
  5. * First release
  6. *
  7. * Current development and maintenance by:
  8. * (c) 2000 Jimmie Mayfield (mayfield+usb@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. * Developed with the assistance of:
  19. *
  20. * (C) 2002 Alan Stern <stern@rowland.org>
  21. *
  22. * This program is free software; you can redistribute it and/or modify it
  23. * under the terms of the GNU General Public License as published by the
  24. * Free Software Foundation; either version 2, or (at your option) any
  25. * later version.
  26. *
  27. * This program is distributed in the hope that it will be useful, but
  28. * WITHOUT ANY WARRANTY; without even the implied warranty of
  29. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  30. * General Public License for more details.
  31. *
  32. * You should have received a copy of the GNU General Public License along
  33. * with this program; if not, write to the Free Software Foundation, Inc.,
  34. * 675 Mass Ave, Cambridge, MA 02139, USA.
  35. */
  36. /*
  37. * This driver attempts to support the Lexar Jumpshot USB CompactFlash
  38. * reader. Like many other USB CompactFlash readers, the Jumpshot contains
  39. * a USB-to-ATA chip.
  40. *
  41. * This driver supports reading and writing. If you're truly paranoid,
  42. * however, you can force the driver into a write-protected state by setting
  43. * the WP enable bits in jumpshot_handle_mode_sense. See the comments
  44. * in that routine.
  45. */
  46. #include <linux/errno.h>
  47. #include <linux/slab.h>
  48. #include <scsi/scsi.h>
  49. #include <scsi/scsi_cmnd.h>
  50. #include "usb.h"
  51. #include "transport.h"
  52. #include "protocol.h"
  53. #include "debug.h"
  54. #include "jumpshot.h"
  55. static inline int jumpshot_bulk_read(struct us_data *us,
  56. unsigned char *data,
  57. unsigned int len)
  58. {
  59. if (len == 0)
  60. return USB_STOR_XFER_GOOD;
  61. US_DEBUGP("jumpshot_bulk_read: len = %d\n", len);
  62. return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  63. data, len, NULL);
  64. }
  65. static inline int jumpshot_bulk_write(struct us_data *us,
  66. unsigned char *data,
  67. unsigned int len)
  68. {
  69. if (len == 0)
  70. return USB_STOR_XFER_GOOD;
  71. US_DEBUGP("jumpshot_bulk_write: len = %d\n", len);
  72. return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  73. data, len, NULL);
  74. }
  75. static int jumpshot_get_status(struct us_data *us)
  76. {
  77. int rc;
  78. if (!us)
  79. return USB_STOR_TRANSPORT_ERROR;
  80. // send the setup
  81. rc = usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
  82. 0, 0xA0, 0, 7, us->iobuf, 1);
  83. if (rc != USB_STOR_XFER_GOOD)
  84. return USB_STOR_TRANSPORT_ERROR;
  85. if (us->iobuf[0] != 0x50) {
  86. US_DEBUGP("jumpshot_get_status: 0x%2x\n",
  87. us->iobuf[0]);
  88. return USB_STOR_TRANSPORT_ERROR;
  89. }
  90. return USB_STOR_TRANSPORT_GOOD;
  91. }
  92. static int jumpshot_read_data(struct us_data *us,
  93. struct jumpshot_info *info,
  94. u32 sector,
  95. u32 sectors)
  96. {
  97. unsigned char *command = us->iobuf;
  98. unsigned char *buffer;
  99. unsigned char thistime;
  100. unsigned int totallen, alloclen;
  101. int len, result;
  102. unsigned int sg_offset = 0;
  103. struct scatterlist *sg = NULL;
  104. // we're working in LBA mode. according to the ATA spec,
  105. // we can support up to 28-bit addressing. I don't know if Jumpshot
  106. // supports beyond 24-bit addressing. It's kind of hard to test
  107. // since it requires > 8GB CF card.
  108. if (sector > 0x0FFFFFFF)
  109. return USB_STOR_TRANSPORT_ERROR;
  110. totallen = sectors * info->ssize;
  111. // Since we don't read more than 64 KB at a time, we have to create
  112. // a bounce buffer and move the data a piece at a time between the
  113. // bounce buffer and the actual transfer buffer.
  114. alloclen = min(totallen, 65536u);
  115. buffer = kmalloc(alloclen, GFP_NOIO);
  116. if (buffer == NULL)
  117. return USB_STOR_TRANSPORT_ERROR;
  118. do {
  119. // loop, never allocate or transfer more than 64k at once
  120. // (min(128k, 255*info->ssize) is the real limit)
  121. len = min(totallen, alloclen);
  122. thistime = (len / info->ssize) & 0xff;
  123. command[0] = 0;
  124. command[1] = thistime;
  125. command[2] = sector & 0xFF;
  126. command[3] = (sector >> 8) & 0xFF;
  127. command[4] = (sector >> 16) & 0xFF;
  128. command[5] = 0xE0 | ((sector >> 24) & 0x0F);
  129. command[6] = 0x20;
  130. // send the setup + command
  131. result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
  132. 0, 0x20, 0, 1, command, 7);
  133. if (result != USB_STOR_XFER_GOOD)
  134. goto leave;
  135. // read the result
  136. result = jumpshot_bulk_read(us, buffer, len);
  137. if (result != USB_STOR_XFER_GOOD)
  138. goto leave;
  139. US_DEBUGP("jumpshot_read_data: %d bytes\n", len);
  140. // Store the data in the transfer buffer
  141. usb_stor_access_xfer_buf(buffer, len, us->srb,
  142. &sg, &sg_offset, TO_XFER_BUF);
  143. sector += thistime;
  144. totallen -= len;
  145. } while (totallen > 0);
  146. kfree(buffer);
  147. return USB_STOR_TRANSPORT_GOOD;
  148. leave:
  149. kfree(buffer);
  150. return USB_STOR_TRANSPORT_ERROR;
  151. }
  152. static int jumpshot_write_data(struct us_data *us,
  153. struct jumpshot_info *info,
  154. u32 sector,
  155. u32 sectors)
  156. {
  157. unsigned char *command = us->iobuf;
  158. unsigned char *buffer;
  159. unsigned char thistime;
  160. unsigned int totallen, alloclen;
  161. int len, result, waitcount;
  162. unsigned int sg_offset = 0;
  163. struct scatterlist *sg = NULL;
  164. // we're working in LBA mode. according to the ATA spec,
  165. // we can support up to 28-bit addressing. I don't know if Jumpshot
  166. // supports beyond 24-bit addressing. It's kind of hard to test
  167. // since it requires > 8GB CF card.
  168. //
  169. if (sector > 0x0FFFFFFF)
  170. return USB_STOR_TRANSPORT_ERROR;
  171. totallen = sectors * info->ssize;
  172. // Since we don't write more than 64 KB at a time, we have to create
  173. // a bounce buffer and move the data a piece at a time between the
  174. // bounce buffer and the actual transfer buffer.
  175. alloclen = min(totallen, 65536u);
  176. buffer = kmalloc(alloclen, GFP_NOIO);
  177. if (buffer == NULL)
  178. return USB_STOR_TRANSPORT_ERROR;
  179. do {
  180. // loop, never allocate or transfer more than 64k at once
  181. // (min(128k, 255*info->ssize) is the real limit)
  182. len = min(totallen, alloclen);
  183. thistime = (len / info->ssize) & 0xff;
  184. // Get the data from the transfer buffer
  185. usb_stor_access_xfer_buf(buffer, len, us->srb,
  186. &sg, &sg_offset, FROM_XFER_BUF);
  187. command[0] = 0;
  188. command[1] = thistime;
  189. command[2] = sector & 0xFF;
  190. command[3] = (sector >> 8) & 0xFF;
  191. command[4] = (sector >> 16) & 0xFF;
  192. command[5] = 0xE0 | ((sector >> 24) & 0x0F);
  193. command[6] = 0x30;
  194. // send the setup + command
  195. result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
  196. 0, 0x20, 0, 1, command, 7);
  197. if (result != USB_STOR_XFER_GOOD)
  198. goto leave;
  199. // send the data
  200. result = jumpshot_bulk_write(us, buffer, len);
  201. if (result != USB_STOR_XFER_GOOD)
  202. goto leave;
  203. // read the result. apparently the bulk write can complete
  204. // before the jumpshot drive is finished writing. so we loop
  205. // here until we get a good return code
  206. waitcount = 0;
  207. do {
  208. result = jumpshot_get_status(us);
  209. if (result != USB_STOR_TRANSPORT_GOOD) {
  210. // I have not experimented to find the smallest value.
  211. //
  212. msleep(50);
  213. }
  214. } while ((result != USB_STOR_TRANSPORT_GOOD) && (waitcount < 10));
  215. if (result != USB_STOR_TRANSPORT_GOOD)
  216. US_DEBUGP("jumpshot_write_data: Gah! Waitcount = 10. Bad write!?\n");
  217. sector += thistime;
  218. totallen -= len;
  219. } while (totallen > 0);
  220. kfree(buffer);
  221. return result;
  222. leave:
  223. kfree(buffer);
  224. return USB_STOR_TRANSPORT_ERROR;
  225. }
  226. static int jumpshot_id_device(struct us_data *us,
  227. struct jumpshot_info *info)
  228. {
  229. unsigned char *command = us->iobuf;
  230. unsigned char *reply;
  231. int rc;
  232. if (!us || !info)
  233. return USB_STOR_TRANSPORT_ERROR;
  234. command[0] = 0xE0;
  235. command[1] = 0xEC;
  236. reply = kmalloc(512, GFP_NOIO);
  237. if (!reply)
  238. return USB_STOR_TRANSPORT_ERROR;
  239. // send the setup
  240. rc = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
  241. 0, 0x20, 0, 6, command, 2);
  242. if (rc != USB_STOR_XFER_GOOD) {
  243. US_DEBUGP("jumpshot_id_device: Gah! "
  244. "send_control for read_capacity failed\n");
  245. rc = USB_STOR_TRANSPORT_ERROR;
  246. goto leave;
  247. }
  248. // read the reply
  249. rc = jumpshot_bulk_read(us, reply, 512);
  250. if (rc != USB_STOR_XFER_GOOD) {
  251. rc = USB_STOR_TRANSPORT_ERROR;
  252. goto leave;
  253. }
  254. info->sectors = ((u32)(reply[117]) << 24) |
  255. ((u32)(reply[116]) << 16) |
  256. ((u32)(reply[115]) << 8) |
  257. ((u32)(reply[114]) );
  258. rc = USB_STOR_TRANSPORT_GOOD;
  259. leave:
  260. kfree(reply);
  261. return rc;
  262. }
  263. static int jumpshot_handle_mode_sense(struct us_data *us,
  264. struct scsi_cmnd * srb,
  265. int sense_6)
  266. {
  267. static unsigned char rw_err_page[12] = {
  268. 0x1, 0xA, 0x21, 1, 0, 0, 0, 0, 1, 0, 0, 0
  269. };
  270. static unsigned char cache_page[12] = {
  271. 0x8, 0xA, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0
  272. };
  273. static unsigned char rbac_page[12] = {
  274. 0x1B, 0xA, 0, 0x81, 0, 0, 0, 0, 0, 0, 0, 0
  275. };
  276. static unsigned char timer_page[8] = {
  277. 0x1C, 0x6, 0, 0, 0, 0
  278. };
  279. unsigned char pc, page_code;
  280. unsigned int i = 0;
  281. struct jumpshot_info *info = (struct jumpshot_info *) (us->extra);
  282. unsigned char *ptr = us->iobuf;
  283. pc = srb->cmnd[2] >> 6;
  284. page_code = srb->cmnd[2] & 0x3F;
  285. switch (pc) {
  286. case 0x0:
  287. US_DEBUGP("jumpshot_handle_mode_sense: Current values\n");
  288. break;
  289. case 0x1:
  290. US_DEBUGP("jumpshot_handle_mode_sense: Changeable values\n");
  291. break;
  292. case 0x2:
  293. US_DEBUGP("jumpshot_handle_mode_sense: Default values\n");
  294. break;
  295. case 0x3:
  296. US_DEBUGP("jumpshot_handle_mode_sense: Saves values\n");
  297. break;
  298. }
  299. memset(ptr, 0, 8);
  300. if (sense_6) {
  301. ptr[2] = 0x00; // WP enable: 0x80
  302. i = 4;
  303. } else {
  304. ptr[3] = 0x00; // WP enable: 0x80
  305. i = 8;
  306. }
  307. switch (page_code) {
  308. case 0x0:
  309. // vendor-specific mode
  310. info->sense_key = 0x05;
  311. info->sense_asc = 0x24;
  312. info->sense_ascq = 0x00;
  313. return USB_STOR_TRANSPORT_FAILED;
  314. case 0x1:
  315. memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
  316. i += sizeof(rw_err_page);
  317. break;
  318. case 0x8:
  319. memcpy(ptr + i, cache_page, sizeof(cache_page));
  320. i += sizeof(cache_page);
  321. break;
  322. case 0x1B:
  323. memcpy(ptr + i, rbac_page, sizeof(rbac_page));
  324. i += sizeof(rbac_page);
  325. break;
  326. case 0x1C:
  327. memcpy(ptr + i, timer_page, sizeof(timer_page));
  328. i += sizeof(timer_page);
  329. break;
  330. case 0x3F:
  331. memcpy(ptr + i, timer_page, sizeof(timer_page));
  332. i += sizeof(timer_page);
  333. memcpy(ptr + i, rbac_page, sizeof(rbac_page));
  334. i += sizeof(rbac_page);
  335. memcpy(ptr + i, cache_page, sizeof(cache_page));
  336. i += sizeof(cache_page);
  337. memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
  338. i += sizeof(rw_err_page);
  339. break;
  340. }
  341. if (sense_6)
  342. ptr[0] = i - 1;
  343. else
  344. ((__be16 *) ptr)[0] = cpu_to_be16(i - 2);
  345. usb_stor_set_xfer_buf(ptr, i, srb);
  346. return USB_STOR_TRANSPORT_GOOD;
  347. }
  348. static void jumpshot_info_destructor(void *extra)
  349. {
  350. // this routine is a placeholder...
  351. // currently, we don't allocate any extra blocks so we're okay
  352. }
  353. // Transport for the Lexar 'Jumpshot'
  354. //
  355. int jumpshot_transport(struct scsi_cmnd * srb, struct us_data *us)
  356. {
  357. struct jumpshot_info *info;
  358. int rc;
  359. unsigned long block, blocks;
  360. unsigned char *ptr = us->iobuf;
  361. static unsigned char inquiry_response[8] = {
  362. 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
  363. };
  364. if (!us->extra) {
  365. us->extra = kzalloc(sizeof(struct jumpshot_info), GFP_NOIO);
  366. if (!us->extra) {
  367. US_DEBUGP("jumpshot_transport: Gah! Can't allocate storage for jumpshot info struct!\n");
  368. return USB_STOR_TRANSPORT_ERROR;
  369. }
  370. us->extra_destructor = jumpshot_info_destructor;
  371. }
  372. info = (struct jumpshot_info *) (us->extra);
  373. if (srb->cmnd[0] == INQUIRY) {
  374. US_DEBUGP("jumpshot_transport: INQUIRY. Returning bogus response.\n");
  375. memcpy(ptr, inquiry_response, sizeof(inquiry_response));
  376. fill_inquiry_response(us, ptr, 36);
  377. return USB_STOR_TRANSPORT_GOOD;
  378. }
  379. if (srb->cmnd[0] == READ_CAPACITY) {
  380. info->ssize = 0x200; // hard coded 512 byte sectors as per ATA spec
  381. rc = jumpshot_get_status(us);
  382. if (rc != USB_STOR_TRANSPORT_GOOD)
  383. return rc;
  384. rc = jumpshot_id_device(us, info);
  385. if (rc != USB_STOR_TRANSPORT_GOOD)
  386. return rc;
  387. US_DEBUGP("jumpshot_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
  388. info->sectors, info->ssize);
  389. // build the reply
  390. //
  391. ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
  392. ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
  393. usb_stor_set_xfer_buf(ptr, 8, srb);
  394. return USB_STOR_TRANSPORT_GOOD;
  395. }
  396. if (srb->cmnd[0] == MODE_SELECT_10) {
  397. US_DEBUGP("jumpshot_transport: Gah! MODE_SELECT_10.\n");
  398. return USB_STOR_TRANSPORT_ERROR;
  399. }
  400. if (srb->cmnd[0] == READ_10) {
  401. block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
  402. ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
  403. blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
  404. US_DEBUGP("jumpshot_transport: READ_10: read block 0x%04lx count %ld\n", block, blocks);
  405. return jumpshot_read_data(us, info, block, blocks);
  406. }
  407. if (srb->cmnd[0] == READ_12) {
  408. // I don't think we'll ever see a READ_12 but support it anyway...
  409. //
  410. block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
  411. ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
  412. blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
  413. ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
  414. US_DEBUGP("jumpshot_transport: READ_12: read block 0x%04lx count %ld\n", block, blocks);
  415. return jumpshot_read_data(us, info, block, blocks);
  416. }
  417. if (srb->cmnd[0] == WRITE_10) {
  418. block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
  419. ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
  420. blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
  421. US_DEBUGP("jumpshot_transport: WRITE_10: write block 0x%04lx count %ld\n", block, blocks);
  422. return jumpshot_write_data(us, info, block, blocks);
  423. }
  424. if (srb->cmnd[0] == WRITE_12) {
  425. // I don't think we'll ever see a WRITE_12 but support it anyway...
  426. //
  427. block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
  428. ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
  429. blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
  430. ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
  431. US_DEBUGP("jumpshot_transport: WRITE_12: write block 0x%04lx count %ld\n", block, blocks);
  432. return jumpshot_write_data(us, info, block, blocks);
  433. }
  434. if (srb->cmnd[0] == TEST_UNIT_READY) {
  435. US_DEBUGP("jumpshot_transport: TEST_UNIT_READY.\n");
  436. return jumpshot_get_status(us);
  437. }
  438. if (srb->cmnd[0] == REQUEST_SENSE) {
  439. US_DEBUGP("jumpshot_transport: REQUEST_SENSE.\n");
  440. memset(ptr, 0, 18);
  441. ptr[0] = 0xF0;
  442. ptr[2] = info->sense_key;
  443. ptr[7] = 11;
  444. ptr[12] = info->sense_asc;
  445. ptr[13] = info->sense_ascq;
  446. usb_stor_set_xfer_buf(ptr, 18, srb);
  447. return USB_STOR_TRANSPORT_GOOD;
  448. }
  449. if (srb->cmnd[0] == MODE_SENSE) {
  450. US_DEBUGP("jumpshot_transport: MODE_SENSE_6 detected\n");
  451. return jumpshot_handle_mode_sense(us, srb, 1);
  452. }
  453. if (srb->cmnd[0] == MODE_SENSE_10) {
  454. US_DEBUGP("jumpshot_transport: MODE_SENSE_10 detected\n");
  455. return jumpshot_handle_mode_sense(us, srb, 0);
  456. }
  457. if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
  458. // sure. whatever. not like we can stop the user from popping
  459. // the media out of the device (no locking doors, etc)
  460. //
  461. return USB_STOR_TRANSPORT_GOOD;
  462. }
  463. if (srb->cmnd[0] == START_STOP) {
  464. /* this is used by sd.c'check_scsidisk_media_change to detect
  465. media change */
  466. US_DEBUGP("jumpshot_transport: START_STOP.\n");
  467. /* the first jumpshot_id_device after a media change returns
  468. an error (determined experimentally) */
  469. rc = jumpshot_id_device(us, info);
  470. if (rc == USB_STOR_TRANSPORT_GOOD) {
  471. info->sense_key = NO_SENSE;
  472. srb->result = SUCCESS;
  473. } else {
  474. info->sense_key = UNIT_ATTENTION;
  475. srb->result = SAM_STAT_CHECK_CONDITION;
  476. }
  477. return rc;
  478. }
  479. US_DEBUGP("jumpshot_transport: Gah! Unknown command: %d (0x%x)\n",
  480. srb->cmnd[0], srb->cmnd[0]);
  481. info->sense_key = 0x05;
  482. info->sense_asc = 0x20;
  483. info->sense_ascq = 0x00;
  484. return USB_STOR_TRANSPORT_FAILED;
  485. }