jumpshot.c 16 KB

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