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/sched.h>
  49. #include <linux/errno.h>
  50. #include <linux/slab.h>
  51. #include <scsi/scsi.h>
  52. #include <scsi/scsi_cmnd.h>
  53. #include "usb.h"
  54. #include "transport.h"
  55. #include "protocol.h"
  56. #include "debug.h"
  57. #include "jumpshot.h"
  58. static inline int jumpshot_bulk_read(struct us_data *us,
  59. unsigned char *data,
  60. unsigned int len)
  61. {
  62. if (len == 0)
  63. return USB_STOR_XFER_GOOD;
  64. US_DEBUGP("jumpshot_bulk_read: len = %d\n", len);
  65. return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  66. data, len, NULL);
  67. }
  68. static inline int jumpshot_bulk_write(struct us_data *us,
  69. unsigned char *data,
  70. unsigned int len)
  71. {
  72. if (len == 0)
  73. return USB_STOR_XFER_GOOD;
  74. US_DEBUGP("jumpshot_bulk_write: len = %d\n", len);
  75. return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  76. data, len, NULL);
  77. }
  78. static int jumpshot_get_status(struct us_data *us)
  79. {
  80. int rc;
  81. if (!us)
  82. return USB_STOR_TRANSPORT_ERROR;
  83. // send the setup
  84. rc = usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
  85. 0, 0xA0, 0, 7, us->iobuf, 1);
  86. if (rc != USB_STOR_XFER_GOOD)
  87. return USB_STOR_TRANSPORT_ERROR;
  88. if (us->iobuf[0] != 0x50) {
  89. US_DEBUGP("jumpshot_get_status: 0x%2x\n",
  90. us->iobuf[0]);
  91. return USB_STOR_TRANSPORT_ERROR;
  92. }
  93. return USB_STOR_TRANSPORT_GOOD;
  94. }
  95. static int jumpshot_read_data(struct us_data *us,
  96. struct jumpshot_info *info,
  97. u32 sector,
  98. u32 sectors)
  99. {
  100. unsigned char *command = us->iobuf;
  101. unsigned char *buffer;
  102. unsigned char thistime;
  103. unsigned int totallen, alloclen;
  104. int len, result;
  105. unsigned int sg_idx = 0, sg_offset = 0;
  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_idx, &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_idx = 0, sg_offset = 0;
  165. // we're working in LBA mode. according to the ATA spec,
  166. // we can support up to 28-bit addressing. I don't know if Jumpshot
  167. // supports beyond 24-bit addressing. It's kind of hard to test
  168. // since it requires > 8GB CF card.
  169. //
  170. if (sector > 0x0FFFFFFF)
  171. return USB_STOR_TRANSPORT_ERROR;
  172. totallen = sectors * info->ssize;
  173. // Since we don't write more than 64 KB at a time, we have to create
  174. // a bounce buffer and move the data a piece at a time between the
  175. // bounce buffer and the actual transfer buffer.
  176. alloclen = min(totallen, 65536u);
  177. buffer = kmalloc(alloclen, GFP_NOIO);
  178. if (buffer == NULL)
  179. return USB_STOR_TRANSPORT_ERROR;
  180. do {
  181. // loop, never allocate or transfer more than 64k at once
  182. // (min(128k, 255*info->ssize) is the real limit)
  183. len = min(totallen, alloclen);
  184. thistime = (len / info->ssize) & 0xff;
  185. // Get the data from the transfer buffer
  186. usb_stor_access_xfer_buf(buffer, len, us->srb,
  187. &sg_idx, &sg_offset, FROM_XFER_BUF);
  188. command[0] = 0;
  189. command[1] = thistime;
  190. command[2] = sector & 0xFF;
  191. command[3] = (sector >> 8) & 0xFF;
  192. command[4] = (sector >> 16) & 0xFF;
  193. command[5] = 0xE0 | ((sector >> 24) & 0x0F);
  194. command[6] = 0x30;
  195. // send the setup + command
  196. result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
  197. 0, 0x20, 0, 1, command, 7);
  198. if (result != USB_STOR_XFER_GOOD)
  199. goto leave;
  200. // send the data
  201. result = jumpshot_bulk_write(us, buffer, len);
  202. if (result != USB_STOR_XFER_GOOD)
  203. goto leave;
  204. // read the result. apparently the bulk write can complete
  205. // before the jumpshot drive is finished writing. so we loop
  206. // here until we get a good return code
  207. waitcount = 0;
  208. do {
  209. result = jumpshot_get_status(us);
  210. if (result != USB_STOR_TRANSPORT_GOOD) {
  211. // I have not experimented to find the smallest value.
  212. //
  213. msleep(50);
  214. }
  215. } while ((result != USB_STOR_TRANSPORT_GOOD) && (waitcount < 10));
  216. if (result != USB_STOR_TRANSPORT_GOOD)
  217. US_DEBUGP("jumpshot_write_data: Gah! Waitcount = 10. Bad write!?\n");
  218. sector += thistime;
  219. totallen -= len;
  220. } while (totallen > 0);
  221. kfree(buffer);
  222. return result;
  223. leave:
  224. kfree(buffer);
  225. return USB_STOR_TRANSPORT_ERROR;
  226. }
  227. static int jumpshot_id_device(struct us_data *us,
  228. struct jumpshot_info *info)
  229. {
  230. unsigned char *command = us->iobuf;
  231. unsigned char *reply;
  232. int rc;
  233. if (!us || !info)
  234. return USB_STOR_TRANSPORT_ERROR;
  235. command[0] = 0xE0;
  236. command[1] = 0xEC;
  237. reply = kmalloc(512, GFP_NOIO);
  238. if (!reply)
  239. return USB_STOR_TRANSPORT_ERROR;
  240. // send the setup
  241. rc = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
  242. 0, 0x20, 0, 6, command, 2);
  243. if (rc != USB_STOR_XFER_GOOD) {
  244. US_DEBUGP("jumpshot_id_device: Gah! "
  245. "send_control for read_capacity failed\n");
  246. rc = USB_STOR_TRANSPORT_ERROR;
  247. goto leave;
  248. }
  249. // read the reply
  250. rc = jumpshot_bulk_read(us, reply, 512);
  251. if (rc != USB_STOR_XFER_GOOD) {
  252. rc = USB_STOR_TRANSPORT_ERROR;
  253. goto leave;
  254. }
  255. info->sectors = ((u32)(reply[117]) << 24) |
  256. ((u32)(reply[116]) << 16) |
  257. ((u32)(reply[115]) << 8) |
  258. ((u32)(reply[114]) );
  259. rc = USB_STOR_TRANSPORT_GOOD;
  260. leave:
  261. kfree(reply);
  262. return rc;
  263. }
  264. static int jumpshot_handle_mode_sense(struct us_data *us,
  265. struct scsi_cmnd * srb,
  266. int sense_6)
  267. {
  268. static unsigned char rw_err_page[12] = {
  269. 0x1, 0xA, 0x21, 1, 0, 0, 0, 0, 1, 0, 0, 0
  270. };
  271. static unsigned char cache_page[12] = {
  272. 0x8, 0xA, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0
  273. };
  274. static unsigned char rbac_page[12] = {
  275. 0x1B, 0xA, 0, 0x81, 0, 0, 0, 0, 0, 0, 0, 0
  276. };
  277. static unsigned char timer_page[8] = {
  278. 0x1C, 0x6, 0, 0, 0, 0
  279. };
  280. unsigned char pc, page_code;
  281. unsigned int i = 0;
  282. struct jumpshot_info *info = (struct jumpshot_info *) (us->extra);
  283. unsigned char *ptr = us->iobuf;
  284. pc = srb->cmnd[2] >> 6;
  285. page_code = srb->cmnd[2] & 0x3F;
  286. switch (pc) {
  287. case 0x0:
  288. US_DEBUGP("jumpshot_handle_mode_sense: Current values\n");
  289. break;
  290. case 0x1:
  291. US_DEBUGP("jumpshot_handle_mode_sense: Changeable values\n");
  292. break;
  293. case 0x2:
  294. US_DEBUGP("jumpshot_handle_mode_sense: Default values\n");
  295. break;
  296. case 0x3:
  297. US_DEBUGP("jumpshot_handle_mode_sense: Saves values\n");
  298. break;
  299. }
  300. memset(ptr, 0, 8);
  301. if (sense_6) {
  302. ptr[2] = 0x00; // WP enable: 0x80
  303. i = 4;
  304. } else {
  305. ptr[3] = 0x00; // WP enable: 0x80
  306. i = 8;
  307. }
  308. switch (page_code) {
  309. case 0x0:
  310. // vendor-specific mode
  311. info->sense_key = 0x05;
  312. info->sense_asc = 0x24;
  313. info->sense_ascq = 0x00;
  314. return USB_STOR_TRANSPORT_FAILED;
  315. case 0x1:
  316. memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
  317. i += sizeof(rw_err_page);
  318. break;
  319. case 0x8:
  320. memcpy(ptr + i, cache_page, sizeof(cache_page));
  321. i += sizeof(cache_page);
  322. break;
  323. case 0x1B:
  324. memcpy(ptr + i, rbac_page, sizeof(rbac_page));
  325. i += sizeof(rbac_page);
  326. break;
  327. case 0x1C:
  328. memcpy(ptr + i, timer_page, sizeof(timer_page));
  329. i += sizeof(timer_page);
  330. break;
  331. case 0x3F:
  332. memcpy(ptr + i, timer_page, sizeof(timer_page));
  333. i += sizeof(timer_page);
  334. memcpy(ptr + i, rbac_page, sizeof(rbac_page));
  335. i += sizeof(rbac_page);
  336. memcpy(ptr + i, cache_page, sizeof(cache_page));
  337. i += sizeof(cache_page);
  338. memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
  339. i += sizeof(rw_err_page);
  340. break;
  341. }
  342. if (sense_6)
  343. ptr[0] = i - 1;
  344. else
  345. ((__be16 *) ptr)[0] = cpu_to_be16(i - 2);
  346. usb_stor_set_xfer_buf(ptr, i, srb);
  347. return USB_STOR_TRANSPORT_GOOD;
  348. }
  349. static void jumpshot_info_destructor(void *extra)
  350. {
  351. // this routine is a placeholder...
  352. // currently, we don't allocate any extra blocks so we're okay
  353. }
  354. // Transport for the Lexar 'Jumpshot'
  355. //
  356. int jumpshot_transport(struct scsi_cmnd * srb, struct us_data *us)
  357. {
  358. struct jumpshot_info *info;
  359. int rc;
  360. unsigned long block, blocks;
  361. unsigned char *ptr = us->iobuf;
  362. static unsigned char inquiry_response[8] = {
  363. 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
  364. };
  365. if (!us->extra) {
  366. us->extra = kmalloc(sizeof(struct jumpshot_info), GFP_NOIO);
  367. if (!us->extra) {
  368. US_DEBUGP("jumpshot_transport: Gah! Can't allocate storage for jumpshot info struct!\n");
  369. return USB_STOR_TRANSPORT_ERROR;
  370. }
  371. memset(us->extra, 0, sizeof(struct jumpshot_info));
  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. }