sddr55.c 23 KB

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