sddr55.c 25 KB

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