sddr55.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. /* Driver for SanDisk SDDR-55 SmartMedia reader
  2. *
  3. * SDDR55 driver v0.1:
  4. *
  5. * First release
  6. *
  7. * Current development and maintenance by:
  8. * (c) 2002 Simon Munton
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2, or (at your option) any
  13. * later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 675 Mass Ave, Cambridge, MA 02139, USA.
  23. */
  24. #include <linux/jiffies.h>
  25. #include <linux/errno.h>
  26. #include <linux/module.h>
  27. #include <linux/slab.h>
  28. #include <scsi/scsi.h>
  29. #include <scsi/scsi_cmnd.h>
  30. #include "usb.h"
  31. #include "transport.h"
  32. #include "protocol.h"
  33. #include "debug.h"
  34. #include "scsiglue.h"
  35. #define DRV_NAME "ums-sddr55"
  36. MODULE_DESCRIPTION("Driver for SanDisk SDDR-55 SmartMedia reader");
  37. MODULE_AUTHOR("Simon Munton");
  38. MODULE_LICENSE("GPL");
  39. /*
  40. * The table of devices
  41. */
  42. #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
  43. vendorName, productName, useProtocol, useTransport, \
  44. initFunction, flags) \
  45. { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
  46. .driver_info = (flags) }
  47. static struct usb_device_id sddr55_usb_ids[] = {
  48. # include "unusual_sddr55.h"
  49. { } /* Terminating entry */
  50. };
  51. MODULE_DEVICE_TABLE(usb, sddr55_usb_ids);
  52. #undef UNUSUAL_DEV
  53. /*
  54. * The flags table
  55. */
  56. #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
  57. vendor_name, product_name, use_protocol, use_transport, \
  58. init_function, Flags) \
  59. { \
  60. .vendorName = vendor_name, \
  61. .productName = product_name, \
  62. .useProtocol = use_protocol, \
  63. .useTransport = use_transport, \
  64. .initFunction = init_function, \
  65. }
  66. static struct us_unusual_dev sddr55_unusual_dev_list[] = {
  67. # include "unusual_sddr55.h"
  68. { } /* Terminating entry */
  69. };
  70. #undef UNUSUAL_DEV
  71. #define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) )
  72. #define LSB_of(s) ((s)&0xFF)
  73. #define MSB_of(s) ((s)>>8)
  74. #define PAGESIZE 512
  75. #define set_sense_info(sk, asc, ascq) \
  76. do { \
  77. info->sense_data[2] = sk; \
  78. info->sense_data[12] = asc; \
  79. info->sense_data[13] = ascq; \
  80. } while (0)
  81. struct sddr55_card_info {
  82. unsigned long capacity; /* Size of card in bytes */
  83. int max_log_blks; /* maximum number of logical blocks */
  84. int pageshift; /* log2 of pagesize */
  85. int smallpageshift; /* 1 if pagesize == 256 */
  86. int blocksize; /* Size of block in pages */
  87. int blockshift; /* log2 of blocksize */
  88. int blockmask; /* 2^blockshift - 1 */
  89. int read_only; /* non zero if card is write protected */
  90. int force_read_only; /* non zero if we find a map error*/
  91. int *lba_to_pba; /* logical to physical map */
  92. int *pba_to_lba; /* physical to logical map */
  93. int fatal_error; /* set if we detect something nasty */
  94. unsigned long last_access; /* number of jiffies since we last talked to device */
  95. unsigned char sense_data[18];
  96. };
  97. #define NOT_ALLOCATED 0xffffffff
  98. #define BAD_BLOCK 0xffff
  99. #define CIS_BLOCK 0x400
  100. #define UNUSED_BLOCK 0x3ff
  101. static int
  102. sddr55_bulk_transport(struct us_data *us, int direction,
  103. unsigned char *data, unsigned int len) {
  104. struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
  105. unsigned int pipe = (direction == DMA_FROM_DEVICE) ?
  106. us->recv_bulk_pipe : us->send_bulk_pipe;
  107. if (!len)
  108. return USB_STOR_XFER_GOOD;
  109. info->last_access = jiffies;
  110. return usb_stor_bulk_transfer_buf(us, pipe, data, len, NULL);
  111. }
  112. /* check if card inserted, if there is, update read_only status
  113. * return non zero if no card
  114. */
  115. static int sddr55_status(struct us_data *us)
  116. {
  117. int result;
  118. unsigned char *command = us->iobuf;
  119. unsigned char *status = us->iobuf;
  120. struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
  121. /* send command */
  122. memset(command, 0, 8);
  123. command[5] = 0xB0;
  124. command[7] = 0x80;
  125. result = sddr55_bulk_transport(us,
  126. DMA_TO_DEVICE, command, 8);
  127. usb_stor_dbg(us, "Result for send_command in status %d\n", result);
  128. if (result != USB_STOR_XFER_GOOD) {
  129. set_sense_info (4, 0, 0); /* hardware error */
  130. return USB_STOR_TRANSPORT_ERROR;
  131. }
  132. result = sddr55_bulk_transport(us,
  133. DMA_FROM_DEVICE, status, 4);
  134. /* expect to get short transfer if no card fitted */
  135. if (result == USB_STOR_XFER_SHORT || result == USB_STOR_XFER_STALLED) {
  136. /* had a short transfer, no card inserted, free map memory */
  137. kfree(info->lba_to_pba);
  138. kfree(info->pba_to_lba);
  139. info->lba_to_pba = NULL;
  140. info->pba_to_lba = NULL;
  141. info->fatal_error = 0;
  142. info->force_read_only = 0;
  143. set_sense_info (2, 0x3a, 0); /* not ready, medium not present */
  144. return USB_STOR_TRANSPORT_FAILED;
  145. }
  146. if (result != USB_STOR_XFER_GOOD) {
  147. set_sense_info (4, 0, 0); /* hardware error */
  148. return USB_STOR_TRANSPORT_FAILED;
  149. }
  150. /* check write protect status */
  151. info->read_only = (status[0] & 0x20);
  152. /* now read status */
  153. result = sddr55_bulk_transport(us,
  154. DMA_FROM_DEVICE, status, 2);
  155. if (result != USB_STOR_XFER_GOOD) {
  156. set_sense_info (4, 0, 0); /* hardware error */
  157. }
  158. return (result == USB_STOR_XFER_GOOD ?
  159. USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_FAILED);
  160. }
  161. static int sddr55_read_data(struct us_data *us,
  162. unsigned int lba,
  163. unsigned int page,
  164. unsigned short sectors) {
  165. int result = USB_STOR_TRANSPORT_GOOD;
  166. unsigned char *command = us->iobuf;
  167. unsigned char *status = us->iobuf;
  168. struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
  169. unsigned char *buffer;
  170. unsigned int pba;
  171. unsigned long address;
  172. unsigned short pages;
  173. unsigned int len, offset;
  174. struct scatterlist *sg;
  175. // Since we only read in one block at a time, we have to create
  176. // a bounce buffer and move the data a piece at a time between the
  177. // bounce buffer and the actual transfer buffer.
  178. len = min((unsigned int) sectors, (unsigned int) info->blocksize >>
  179. info->smallpageshift) * PAGESIZE;
  180. buffer = kmalloc(len, GFP_NOIO);
  181. if (buffer == NULL)
  182. return USB_STOR_TRANSPORT_ERROR; /* out of memory */
  183. offset = 0;
  184. sg = NULL;
  185. while (sectors>0) {
  186. /* have we got to end? */
  187. if (lba >= info->max_log_blks)
  188. break;
  189. pba = info->lba_to_pba[lba];
  190. // Read as many sectors as possible in this block
  191. pages = min((unsigned int) sectors << info->smallpageshift,
  192. info->blocksize - page);
  193. len = pages << info->pageshift;
  194. usb_stor_dbg(us, "Read %02X pages, from PBA %04X (LBA %04X) page %02X\n",
  195. pages, pba, lba, page);
  196. if (pba == NOT_ALLOCATED) {
  197. /* no pba for this lba, fill with zeroes */
  198. memset (buffer, 0, len);
  199. } else {
  200. address = (pba << info->blockshift) + page;
  201. command[0] = 0;
  202. command[1] = LSB_of(address>>16);
  203. command[2] = LSB_of(address>>8);
  204. command[3] = LSB_of(address);
  205. command[4] = 0;
  206. command[5] = 0xB0;
  207. command[6] = LSB_of(pages << (1 - info->smallpageshift));
  208. command[7] = 0x85;
  209. /* send command */
  210. result = sddr55_bulk_transport(us,
  211. DMA_TO_DEVICE, command, 8);
  212. usb_stor_dbg(us, "Result for send_command in read_data %d\n",
  213. result);
  214. if (result != USB_STOR_XFER_GOOD) {
  215. result = USB_STOR_TRANSPORT_ERROR;
  216. goto leave;
  217. }
  218. /* read data */
  219. result = sddr55_bulk_transport(us,
  220. DMA_FROM_DEVICE, buffer, len);
  221. if (result != USB_STOR_XFER_GOOD) {
  222. result = USB_STOR_TRANSPORT_ERROR;
  223. goto leave;
  224. }
  225. /* now read status */
  226. result = sddr55_bulk_transport(us,
  227. DMA_FROM_DEVICE, status, 2);
  228. if (result != USB_STOR_XFER_GOOD) {
  229. result = USB_STOR_TRANSPORT_ERROR;
  230. goto leave;
  231. }
  232. /* check status for error */
  233. if (status[0] == 0xff && status[1] == 0x4) {
  234. set_sense_info (3, 0x11, 0);
  235. result = USB_STOR_TRANSPORT_FAILED;
  236. goto leave;
  237. }
  238. }
  239. // Store the data in the transfer buffer
  240. usb_stor_access_xfer_buf(buffer, len, us->srb,
  241. &sg, &offset, TO_XFER_BUF);
  242. page = 0;
  243. lba++;
  244. sectors -= pages >> info->smallpageshift;
  245. }
  246. result = USB_STOR_TRANSPORT_GOOD;
  247. leave:
  248. kfree(buffer);
  249. return result;
  250. }
  251. static int sddr55_write_data(struct us_data *us,
  252. unsigned int lba,
  253. unsigned int page,
  254. unsigned short sectors) {
  255. int result = USB_STOR_TRANSPORT_GOOD;
  256. unsigned char *command = us->iobuf;
  257. unsigned char *status = us->iobuf;
  258. struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
  259. unsigned char *buffer;
  260. unsigned int pba;
  261. unsigned int new_pba;
  262. unsigned long address;
  263. unsigned short pages;
  264. int i;
  265. unsigned int len, offset;
  266. struct scatterlist *sg;
  267. /* check if we are allowed to write */
  268. if (info->read_only || info->force_read_only) {
  269. set_sense_info (7, 0x27, 0); /* read only */
  270. return USB_STOR_TRANSPORT_FAILED;
  271. }
  272. // Since we only write one block at a time, we have to create
  273. // a bounce buffer and move the data a piece at a time between the
  274. // bounce buffer and the actual transfer buffer.
  275. len = min((unsigned int) sectors, (unsigned int) info->blocksize >>
  276. info->smallpageshift) * PAGESIZE;
  277. buffer = kmalloc(len, GFP_NOIO);
  278. if (buffer == NULL)
  279. return USB_STOR_TRANSPORT_ERROR;
  280. offset = 0;
  281. sg = NULL;
  282. while (sectors > 0) {
  283. /* have we got to end? */
  284. if (lba >= info->max_log_blks)
  285. break;
  286. pba = info->lba_to_pba[lba];
  287. // Write as many sectors as possible in this block
  288. pages = min((unsigned int) sectors << info->smallpageshift,
  289. info->blocksize - page);
  290. len = pages << info->pageshift;
  291. // Get the data from the transfer buffer
  292. usb_stor_access_xfer_buf(buffer, len, us->srb,
  293. &sg, &offset, FROM_XFER_BUF);
  294. usb_stor_dbg(us, "Write %02X pages, to PBA %04X (LBA %04X) page %02X\n",
  295. pages, pba, lba, page);
  296. command[4] = 0;
  297. if (pba == NOT_ALLOCATED) {
  298. /* no pba allocated for this lba, find a free pba to use */
  299. int max_pba = (info->max_log_blks / 250 ) * 256;
  300. int found_count = 0;
  301. int found_pba = -1;
  302. /* set pba to first block in zone lba is in */
  303. pba = (lba / 1000) * 1024;
  304. usb_stor_dbg(us, "No PBA for LBA %04X\n", lba);
  305. if (max_pba > 1024)
  306. max_pba = 1024;
  307. /*
  308. * Scan through the map looking for an unused block
  309. * leave 16 unused blocks at start (or as many as
  310. * possible) since the sddr55 seems to reuse a used
  311. * block when it shouldn't if we don't leave space.
  312. */
  313. for (i = 0; i < max_pba; i++, pba++) {
  314. if (info->pba_to_lba[pba] == UNUSED_BLOCK) {
  315. found_pba = pba;
  316. if (found_count++ > 16)
  317. break;
  318. }
  319. }
  320. pba = found_pba;
  321. if (pba == -1) {
  322. /* oh dear */
  323. usb_stor_dbg(us, "Couldn't find unallocated block\n");
  324. set_sense_info (3, 0x31, 0); /* medium error */
  325. result = USB_STOR_TRANSPORT_FAILED;
  326. goto leave;
  327. }
  328. usb_stor_dbg(us, "Allocating PBA %04X for LBA %04X\n",
  329. pba, lba);
  330. /* set writing to unallocated block flag */
  331. command[4] = 0x40;
  332. }
  333. address = (pba << info->blockshift) + page;
  334. command[1] = LSB_of(address>>16);
  335. command[2] = LSB_of(address>>8);
  336. command[3] = LSB_of(address);
  337. /* set the lba into the command, modulo 1000 */
  338. command[0] = LSB_of(lba % 1000);
  339. command[6] = MSB_of(lba % 1000);
  340. command[4] |= LSB_of(pages >> info->smallpageshift);
  341. command[5] = 0xB0;
  342. command[7] = 0x86;
  343. /* send command */
  344. result = sddr55_bulk_transport(us,
  345. DMA_TO_DEVICE, command, 8);
  346. if (result != USB_STOR_XFER_GOOD) {
  347. usb_stor_dbg(us, "Result for send_command in write_data %d\n",
  348. result);
  349. /* set_sense_info is superfluous here? */
  350. set_sense_info (3, 0x3, 0);/* peripheral write error */
  351. result = USB_STOR_TRANSPORT_FAILED;
  352. goto leave;
  353. }
  354. /* send the data */
  355. result = sddr55_bulk_transport(us,
  356. DMA_TO_DEVICE, buffer, len);
  357. if (result != USB_STOR_XFER_GOOD) {
  358. usb_stor_dbg(us, "Result for send_data in write_data %d\n",
  359. result);
  360. /* set_sense_info is superfluous here? */
  361. set_sense_info (3, 0x3, 0);/* peripheral write error */
  362. result = USB_STOR_TRANSPORT_FAILED;
  363. goto leave;
  364. }
  365. /* now read status */
  366. result = sddr55_bulk_transport(us, DMA_FROM_DEVICE, status, 6);
  367. if (result != USB_STOR_XFER_GOOD) {
  368. usb_stor_dbg(us, "Result for get_status in write_data %d\n",
  369. result);
  370. /* set_sense_info is superfluous here? */
  371. set_sense_info (3, 0x3, 0);/* peripheral write error */
  372. result = USB_STOR_TRANSPORT_FAILED;
  373. goto leave;
  374. }
  375. new_pba = (status[3] + (status[4] << 8) + (status[5] << 16))
  376. >> info->blockshift;
  377. /* check status for error */
  378. if (status[0] == 0xff && status[1] == 0x4) {
  379. info->pba_to_lba[new_pba] = BAD_BLOCK;
  380. set_sense_info (3, 0x0c, 0);
  381. result = USB_STOR_TRANSPORT_FAILED;
  382. goto leave;
  383. }
  384. usb_stor_dbg(us, "Updating maps for LBA %04X: old PBA %04X, new PBA %04X\n",
  385. lba, pba, new_pba);
  386. /* update the lba<->pba maps, note new_pba might be the same as pba */
  387. info->lba_to_pba[lba] = new_pba;
  388. info->pba_to_lba[pba] = UNUSED_BLOCK;
  389. /* check that new_pba wasn't already being used */
  390. if (info->pba_to_lba[new_pba] != UNUSED_BLOCK) {
  391. printk(KERN_ERR "sddr55 error: new PBA %04X already in use for LBA %04X\n",
  392. new_pba, info->pba_to_lba[new_pba]);
  393. info->fatal_error = 1;
  394. set_sense_info (3, 0x31, 0);
  395. result = USB_STOR_TRANSPORT_FAILED;
  396. goto leave;
  397. }
  398. /* update the pba<->lba maps for new_pba */
  399. info->pba_to_lba[new_pba] = lba % 1000;
  400. page = 0;
  401. lba++;
  402. sectors -= pages >> info->smallpageshift;
  403. }
  404. result = USB_STOR_TRANSPORT_GOOD;
  405. leave:
  406. kfree(buffer);
  407. return result;
  408. }
  409. static int sddr55_read_deviceID(struct us_data *us,
  410. unsigned char *manufacturerID,
  411. unsigned char *deviceID) {
  412. int result;
  413. unsigned char *command = us->iobuf;
  414. unsigned char *content = us->iobuf;
  415. memset(command, 0, 8);
  416. command[5] = 0xB0;
  417. command[7] = 0x84;
  418. result = sddr55_bulk_transport(us, DMA_TO_DEVICE, command, 8);
  419. usb_stor_dbg(us, "Result of send_control for device ID is %d\n",
  420. result);
  421. if (result != USB_STOR_XFER_GOOD)
  422. return USB_STOR_TRANSPORT_ERROR;
  423. result = sddr55_bulk_transport(us,
  424. DMA_FROM_DEVICE, content, 4);
  425. if (result != USB_STOR_XFER_GOOD)
  426. return USB_STOR_TRANSPORT_ERROR;
  427. *manufacturerID = content[0];
  428. *deviceID = content[1];
  429. if (content[0] != 0xff) {
  430. result = sddr55_bulk_transport(us,
  431. DMA_FROM_DEVICE, content, 2);
  432. }
  433. return USB_STOR_TRANSPORT_GOOD;
  434. }
  435. static int sddr55_reset(struct us_data *us)
  436. {
  437. return 0;
  438. }
  439. static unsigned long sddr55_get_capacity(struct us_data *us) {
  440. unsigned char uninitialized_var(manufacturerID);
  441. unsigned char uninitialized_var(deviceID);
  442. int result;
  443. struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
  444. usb_stor_dbg(us, "Reading capacity...\n");
  445. result = sddr55_read_deviceID(us,
  446. &manufacturerID,
  447. &deviceID);
  448. usb_stor_dbg(us, "Result of read_deviceID is %d\n", result);
  449. if (result != USB_STOR_XFER_GOOD)
  450. return 0;
  451. usb_stor_dbg(us, "Device ID = %02X\n", deviceID);
  452. usb_stor_dbg(us, "Manuf ID = %02X\n", manufacturerID);
  453. info->pageshift = 9;
  454. info->smallpageshift = 0;
  455. info->blocksize = 16;
  456. info->blockshift = 4;
  457. info->blockmask = 15;
  458. switch (deviceID) {
  459. case 0x6e: // 1MB
  460. case 0xe8:
  461. case 0xec:
  462. info->pageshift = 8;
  463. info->smallpageshift = 1;
  464. return 0x00100000;
  465. case 0xea: // 2MB
  466. case 0x64:
  467. info->pageshift = 8;
  468. info->smallpageshift = 1;
  469. case 0x5d: // 5d is a ROM card with pagesize 512.
  470. return 0x00200000;
  471. case 0xe3: // 4MB
  472. case 0xe5:
  473. case 0x6b:
  474. case 0xd5:
  475. return 0x00400000;
  476. case 0xe6: // 8MB
  477. case 0xd6:
  478. return 0x00800000;
  479. case 0x73: // 16MB
  480. info->blocksize = 32;
  481. info->blockshift = 5;
  482. info->blockmask = 31;
  483. return 0x01000000;
  484. case 0x75: // 32MB
  485. info->blocksize = 32;
  486. info->blockshift = 5;
  487. info->blockmask = 31;
  488. return 0x02000000;
  489. case 0x76: // 64MB
  490. info->blocksize = 32;
  491. info->blockshift = 5;
  492. info->blockmask = 31;
  493. return 0x04000000;
  494. case 0x79: // 128MB
  495. info->blocksize = 32;
  496. info->blockshift = 5;
  497. info->blockmask = 31;
  498. return 0x08000000;
  499. default: // unknown
  500. return 0;
  501. }
  502. }
  503. static int sddr55_read_map(struct us_data *us) {
  504. struct sddr55_card_info *info = (struct sddr55_card_info *)(us->extra);
  505. int numblocks;
  506. unsigned char *buffer;
  507. unsigned char *command = us->iobuf;
  508. int i;
  509. unsigned short lba;
  510. unsigned short max_lba;
  511. int result;
  512. if (!info->capacity)
  513. return -1;
  514. numblocks = info->capacity >> (info->blockshift + info->pageshift);
  515. buffer = kmalloc( numblocks * 2, GFP_NOIO );
  516. if (!buffer)
  517. return -1;
  518. memset(command, 0, 8);
  519. command[5] = 0xB0;
  520. command[6] = numblocks * 2 / 256;
  521. command[7] = 0x8A;
  522. result = sddr55_bulk_transport(us, DMA_TO_DEVICE, command, 8);
  523. if ( result != USB_STOR_XFER_GOOD) {
  524. kfree (buffer);
  525. return -1;
  526. }
  527. result = sddr55_bulk_transport(us, DMA_FROM_DEVICE, buffer, numblocks * 2);
  528. if ( result != USB_STOR_XFER_GOOD) {
  529. kfree (buffer);
  530. return -1;
  531. }
  532. result = sddr55_bulk_transport(us, DMA_FROM_DEVICE, command, 2);
  533. if ( result != USB_STOR_XFER_GOOD) {
  534. kfree (buffer);
  535. return -1;
  536. }
  537. kfree(info->lba_to_pba);
  538. kfree(info->pba_to_lba);
  539. info->lba_to_pba = kmalloc(numblocks*sizeof(int), GFP_NOIO);
  540. info->pba_to_lba = kmalloc(numblocks*sizeof(int), GFP_NOIO);
  541. if (info->lba_to_pba == NULL || info->pba_to_lba == NULL) {
  542. kfree(info->lba_to_pba);
  543. kfree(info->pba_to_lba);
  544. info->lba_to_pba = NULL;
  545. info->pba_to_lba = NULL;
  546. kfree(buffer);
  547. return -1;
  548. }
  549. memset(info->lba_to_pba, 0xff, numblocks*sizeof(int));
  550. memset(info->pba_to_lba, 0xff, numblocks*sizeof(int));
  551. /* set maximum lba */
  552. max_lba = info->max_log_blks;
  553. if (max_lba > 1000)
  554. max_lba = 1000;
  555. // Each block is 64 bytes of control data, so block i is located in
  556. // scatterlist block i*64/128k = i*(2^6)*(2^-17) = i*(2^-11)
  557. for (i=0; i<numblocks; i++) {
  558. int zone = i / 1024;
  559. lba = short_pack(buffer[i * 2], buffer[i * 2 + 1]);
  560. /* Every 1024 physical blocks ("zone"), the LBA numbers
  561. * go back to zero, but are within a higher
  562. * block of LBA's. Also, there is a maximum of
  563. * 1000 LBA's per zone. In other words, in PBA
  564. * 1024-2047 you will find LBA 0-999 which are
  565. * really LBA 1000-1999. Yes, this wastes 24
  566. * physical blocks per zone. Go figure.
  567. * These devices can have blocks go bad, so there
  568. * are 24 spare blocks to use when blocks do go bad.
  569. */
  570. /* SDDR55 returns 0xffff for a bad block, and 0x400 for the
  571. * CIS block. (Is this true for cards 8MB or less??)
  572. * Record these in the physical to logical map
  573. */
  574. info->pba_to_lba[i] = lba;
  575. if (lba >= max_lba) {
  576. continue;
  577. }
  578. if (info->lba_to_pba[lba + zone * 1000] != NOT_ALLOCATED &&
  579. !info->force_read_only) {
  580. printk(KERN_WARNING
  581. "sddr55: map inconsistency at LBA %04X\n",
  582. lba + zone * 1000);
  583. info->force_read_only = 1;
  584. }
  585. if (lba<0x10 || (lba>=0x3E0 && lba<0x3EF))
  586. usb_stor_dbg(us, "LBA %04X <-> PBA %04X\n", lba, i);
  587. info->lba_to_pba[lba + zone * 1000] = i;
  588. }
  589. kfree(buffer);
  590. return 0;
  591. }
  592. static void sddr55_card_info_destructor(void *extra) {
  593. struct sddr55_card_info *info = (struct sddr55_card_info *)extra;
  594. if (!extra)
  595. return;
  596. kfree(info->lba_to_pba);
  597. kfree(info->pba_to_lba);
  598. }
  599. /*
  600. * Transport for the Sandisk SDDR-55
  601. */
  602. static int sddr55_transport(struct scsi_cmnd *srb, struct us_data *us)
  603. {
  604. int result;
  605. static unsigned char inquiry_response[8] = {
  606. 0x00, 0x80, 0x00, 0x02, 0x1F, 0x00, 0x00, 0x00
  607. };
  608. // write-protected for now, no block descriptor support
  609. static unsigned char mode_page_01[20] = {
  610. 0x0, 0x12, 0x00, 0x80, 0x0, 0x0, 0x0, 0x0,
  611. 0x01, 0x0A,
  612. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  613. };
  614. unsigned char *ptr = us->iobuf;
  615. unsigned long capacity;
  616. unsigned int lba;
  617. unsigned int pba;
  618. unsigned int page;
  619. unsigned short pages;
  620. struct sddr55_card_info *info;
  621. if (!us->extra) {
  622. us->extra = kzalloc(
  623. sizeof(struct sddr55_card_info), GFP_NOIO);
  624. if (!us->extra)
  625. return USB_STOR_TRANSPORT_ERROR;
  626. us->extra_destructor = sddr55_card_info_destructor;
  627. }
  628. info = (struct sddr55_card_info *)(us->extra);
  629. if (srb->cmnd[0] == REQUEST_SENSE) {
  630. usb_stor_dbg(us, "request sense %02x/%02x/%02x\n",
  631. info->sense_data[2],
  632. info->sense_data[12],
  633. info->sense_data[13]);
  634. memcpy (ptr, info->sense_data, sizeof info->sense_data);
  635. ptr[0] = 0x70;
  636. ptr[7] = 11;
  637. usb_stor_set_xfer_buf (ptr, sizeof info->sense_data, srb);
  638. memset (info->sense_data, 0, sizeof info->sense_data);
  639. return USB_STOR_TRANSPORT_GOOD;
  640. }
  641. memset (info->sense_data, 0, sizeof info->sense_data);
  642. /* Dummy up a response for INQUIRY since SDDR55 doesn't
  643. respond to INQUIRY commands */
  644. if (srb->cmnd[0] == INQUIRY) {
  645. memcpy(ptr, inquiry_response, 8);
  646. fill_inquiry_response(us, ptr, 36);
  647. return USB_STOR_TRANSPORT_GOOD;
  648. }
  649. /* only check card status if the map isn't allocated, ie no card seen yet
  650. * or if it's been over half a second since we last accessed it
  651. */
  652. if (info->lba_to_pba == NULL || time_after(jiffies, info->last_access + HZ/2)) {
  653. /* check to see if a card is fitted */
  654. result = sddr55_status (us);
  655. if (result) {
  656. result = sddr55_status (us);
  657. if (!result) {
  658. set_sense_info (6, 0x28, 0); /* new media, set unit attention, not ready to ready */
  659. }
  660. return USB_STOR_TRANSPORT_FAILED;
  661. }
  662. }
  663. /* if we detected a problem with the map when writing,
  664. don't allow any more access */
  665. if (info->fatal_error) {
  666. set_sense_info (3, 0x31, 0);
  667. return USB_STOR_TRANSPORT_FAILED;
  668. }
  669. if (srb->cmnd[0] == READ_CAPACITY) {
  670. capacity = sddr55_get_capacity(us);
  671. if (!capacity) {
  672. set_sense_info (3, 0x30, 0); /* incompatible medium */
  673. return USB_STOR_TRANSPORT_FAILED;
  674. }
  675. info->capacity = capacity;
  676. /* figure out the maximum logical block number, allowing for
  677. * the fact that only 250 out of every 256 are used */
  678. info->max_log_blks = ((info->capacity >> (info->pageshift + info->blockshift)) / 256) * 250;
  679. /* Last page in the card, adjust as we only use 250 out of
  680. * every 256 pages */
  681. capacity = (capacity / 256) * 250;
  682. capacity /= PAGESIZE;
  683. capacity--;
  684. ((__be32 *) ptr)[0] = cpu_to_be32(capacity);
  685. ((__be32 *) ptr)[1] = cpu_to_be32(PAGESIZE);
  686. usb_stor_set_xfer_buf(ptr, 8, srb);
  687. sddr55_read_map(us);
  688. return USB_STOR_TRANSPORT_GOOD;
  689. }
  690. if (srb->cmnd[0] == MODE_SENSE_10) {
  691. memcpy(ptr, mode_page_01, sizeof mode_page_01);
  692. ptr[3] = (info->read_only || info->force_read_only) ? 0x80 : 0;
  693. usb_stor_set_xfer_buf(ptr, sizeof(mode_page_01), srb);
  694. if ( (srb->cmnd[2] & 0x3F) == 0x01 ) {
  695. usb_stor_dbg(us, "Dummy up request for mode page 1\n");
  696. return USB_STOR_TRANSPORT_GOOD;
  697. } else if ( (srb->cmnd[2] & 0x3F) == 0x3F ) {
  698. usb_stor_dbg(us, "Dummy up request for all mode pages\n");
  699. return USB_STOR_TRANSPORT_GOOD;
  700. }
  701. set_sense_info (5, 0x24, 0); /* invalid field in command */
  702. return USB_STOR_TRANSPORT_FAILED;
  703. }
  704. if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
  705. usb_stor_dbg(us, "%s medium removal. Not that I can do anything about it...\n",
  706. (srb->cmnd[4]&0x03) ? "Prevent" : "Allow");
  707. return USB_STOR_TRANSPORT_GOOD;
  708. }
  709. if (srb->cmnd[0] == READ_10 || srb->cmnd[0] == WRITE_10) {
  710. page = short_pack(srb->cmnd[3], srb->cmnd[2]);
  711. page <<= 16;
  712. page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
  713. pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
  714. page <<= info->smallpageshift;
  715. // convert page to block and page-within-block
  716. lba = page >> info->blockshift;
  717. page = page & info->blockmask;
  718. // locate physical block corresponding to logical block
  719. if (lba >= info->max_log_blks) {
  720. usb_stor_dbg(us, "Error: Requested LBA %04X exceeds maximum block %04X\n",
  721. lba, info->max_log_blks - 1);
  722. set_sense_info (5, 0x24, 0); /* invalid field in command */
  723. return USB_STOR_TRANSPORT_FAILED;
  724. }
  725. pba = info->lba_to_pba[lba];
  726. if (srb->cmnd[0] == WRITE_10) {
  727. usb_stor_dbg(us, "WRITE_10: write block %04X (LBA %04X) page %01X pages %d\n",
  728. pba, lba, page, pages);
  729. return sddr55_write_data(us, lba, page, pages);
  730. } else {
  731. usb_stor_dbg(us, "READ_10: read block %04X (LBA %04X) page %01X pages %d\n",
  732. pba, lba, page, pages);
  733. return sddr55_read_data(us, lba, page, pages);
  734. }
  735. }
  736. if (srb->cmnd[0] == TEST_UNIT_READY) {
  737. return USB_STOR_TRANSPORT_GOOD;
  738. }
  739. if (srb->cmnd[0] == START_STOP) {
  740. return USB_STOR_TRANSPORT_GOOD;
  741. }
  742. set_sense_info (5, 0x20, 0); /* illegal command */
  743. return USB_STOR_TRANSPORT_FAILED; // FIXME: sense buffer?
  744. }
  745. static struct scsi_host_template sddr55_host_template;
  746. static int sddr55_probe(struct usb_interface *intf,
  747. const struct usb_device_id *id)
  748. {
  749. struct us_data *us;
  750. int result;
  751. result = usb_stor_probe1(&us, intf, id,
  752. (id - sddr55_usb_ids) + sddr55_unusual_dev_list,
  753. &sddr55_host_template);
  754. if (result)
  755. return result;
  756. us->transport_name = "SDDR55";
  757. us->transport = sddr55_transport;
  758. us->transport_reset = sddr55_reset;
  759. us->max_lun = 0;
  760. result = usb_stor_probe2(us);
  761. return result;
  762. }
  763. static struct usb_driver sddr55_driver = {
  764. .name = DRV_NAME,
  765. .probe = sddr55_probe,
  766. .disconnect = usb_stor_disconnect,
  767. .suspend = usb_stor_suspend,
  768. .resume = usb_stor_resume,
  769. .reset_resume = usb_stor_reset_resume,
  770. .pre_reset = usb_stor_pre_reset,
  771. .post_reset = usb_stor_post_reset,
  772. .id_table = sddr55_usb_ids,
  773. .soft_unbind = 1,
  774. .no_dynamic_id = 1,
  775. };
  776. module_usb_stor_driver(sddr55_driver, sddr55_host_template, DRV_NAME);