sddr55.c 23 KB

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