jumpshot.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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_idx = 0, sg_offset = 0;
  105. // we're working in LBA mode. according to the ATA spec,
  106. // we can support up to 28-bit addressing. I don't know if Jumpshot
  107. // supports beyond 24-bit addressing. It's kind of hard to test
  108. // since it requires > 8GB CF card.
  109. if (sector > 0x0FFFFFFF)
  110. return USB_STOR_TRANSPORT_ERROR;
  111. totallen = sectors * info->ssize;
  112. // Since we don't read more than 64 KB at a time, we have to create
  113. // a bounce buffer and move the data a piece at a time between the
  114. // bounce buffer and the actual transfer buffer.
  115. alloclen = min(totallen, 65536u);
  116. buffer = kmalloc(alloclen, GFP_NOIO);
  117. if (buffer == NULL)
  118. return USB_STOR_TRANSPORT_ERROR;
  119. do {
  120. // loop, never allocate or transfer more than 64k at once
  121. // (min(128k, 255*info->ssize) is the real limit)
  122. len = min(totallen, alloclen);
  123. thistime = (len / info->ssize) & 0xff;
  124. command[0] = 0;
  125. command[1] = thistime;
  126. command[2] = sector & 0xFF;
  127. command[3] = (sector >> 8) & 0xFF;
  128. command[4] = (sector >> 16) & 0xFF;
  129. command[5] = 0xE0 | ((sector >> 24) & 0x0F);
  130. command[6] = 0x20;
  131. // send the setup + command
  132. result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
  133. 0, 0x20, 0, 1, command, 7);
  134. if (result != USB_STOR_XFER_GOOD)
  135. goto leave;
  136. // read the result
  137. result = jumpshot_bulk_read(us, buffer, len);
  138. if (result != USB_STOR_XFER_GOOD)
  139. goto leave;
  140. US_DEBUGP("jumpshot_read_data: %d bytes\n", len);
  141. // Store the data in the transfer buffer
  142. usb_stor_access_xfer_buf(buffer, len, us->srb,
  143. &sg_idx, &sg_offset, TO_XFER_BUF);
  144. sector += thistime;
  145. totallen -= len;
  146. } while (totallen > 0);
  147. kfree(buffer);
  148. return USB_STOR_TRANSPORT_GOOD;
  149. leave:
  150. kfree(buffer);
  151. return USB_STOR_TRANSPORT_ERROR;
  152. }
  153. static int jumpshot_write_data(struct us_data *us,
  154. struct jumpshot_info *info,
  155. u32 sector,
  156. u32 sectors)
  157. {
  158. unsigned char *command = us->iobuf;
  159. unsigned char *buffer;
  160. unsigned char thistime;
  161. unsigned int totallen, alloclen;
  162. int len, result, waitcount;
  163. unsigned int sg_idx = 0, sg_offset = 0;
  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_idx, &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. }