sddr55.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  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, index, offset;
  140. // Since we only read in one block at a time, we have to create
  141. // a bounce buffer and move the data a piece at a time between the
  142. // bounce buffer and the actual transfer buffer.
  143. len = min((unsigned int) sectors, (unsigned int) info->blocksize >>
  144. info->smallpageshift) * PAGESIZE;
  145. buffer = kmalloc(len, GFP_NOIO);
  146. if (buffer == NULL)
  147. return USB_STOR_TRANSPORT_ERROR; /* out of memory */
  148. index = offset = 0;
  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. &index, &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, index, offset;
  231. /* check if we are allowed to write */
  232. if (info->read_only || info->force_read_only) {
  233. set_sense_info (7, 0x27, 0); /* read only */
  234. return USB_STOR_TRANSPORT_FAILED;
  235. }
  236. // Since we only write one block at a time, we have to create
  237. // a bounce buffer and move the data a piece at a time between the
  238. // bounce buffer and the actual transfer buffer.
  239. len = min((unsigned int) sectors, (unsigned int) info->blocksize >>
  240. info->smallpageshift) * PAGESIZE;
  241. buffer = kmalloc(len, GFP_NOIO);
  242. if (buffer == NULL)
  243. return USB_STOR_TRANSPORT_ERROR;
  244. index = offset = 0;
  245. while (sectors > 0) {
  246. /* have we got to end? */
  247. if (lba >= info->max_log_blks)
  248. break;
  249. pba = info->lba_to_pba[lba];
  250. // Write as many sectors as possible in this block
  251. pages = min((unsigned int) sectors << info->smallpageshift,
  252. info->blocksize - page);
  253. len = pages << info->pageshift;
  254. // Get the data from the transfer buffer
  255. usb_stor_access_xfer_buf(buffer, len, us->srb,
  256. &index, &offset, FROM_XFER_BUF);
  257. US_DEBUGP("Write %02X pages, to PBA %04X"
  258. " (LBA %04X) page %02X\n",
  259. pages, pba, lba, page);
  260. command[4] = 0;
  261. if (pba == NOT_ALLOCATED) {
  262. /* no pba allocated for this lba, find a free pba to use */
  263. int max_pba = (info->max_log_blks / 250 ) * 256;
  264. int found_count = 0;
  265. int found_pba = -1;
  266. /* set pba to first block in zone lba is in */
  267. pba = (lba / 1000) * 1024;
  268. US_DEBUGP("No PBA for LBA %04X\n",lba);
  269. if (max_pba > 1024)
  270. max_pba = 1024;
  271. /*
  272. * Scan through the map looking for an unused block
  273. * leave 16 unused blocks at start (or as many as
  274. * possible) since the sddr55 seems to reuse a used
  275. * block when it shouldn't if we don't leave space.
  276. */
  277. for (i = 0; i < max_pba; i++, pba++) {
  278. if (info->pba_to_lba[pba] == UNUSED_BLOCK) {
  279. found_pba = pba;
  280. if (found_count++ > 16)
  281. break;
  282. }
  283. }
  284. pba = found_pba;
  285. if (pba == -1) {
  286. /* oh dear */
  287. US_DEBUGP("Couldn't find unallocated block\n");
  288. set_sense_info (3, 0x31, 0); /* medium error */
  289. result = USB_STOR_TRANSPORT_FAILED;
  290. goto leave;
  291. }
  292. US_DEBUGP("Allocating PBA %04X for LBA %04X\n", pba, lba);
  293. /* set writing to unallocated block flag */
  294. command[4] = 0x40;
  295. }
  296. address = (pba << info->blockshift) + page;
  297. command[1] = LSB_of(address>>16);
  298. command[2] = LSB_of(address>>8);
  299. command[3] = LSB_of(address);
  300. /* set the lba into the command, modulo 1000 */
  301. command[0] = LSB_of(lba % 1000);
  302. command[6] = MSB_of(lba % 1000);
  303. command[4] |= LSB_of(pages >> info->smallpageshift);
  304. command[5] = 0xB0;
  305. command[7] = 0x86;
  306. /* send command */
  307. result = sddr55_bulk_transport(us,
  308. DMA_TO_DEVICE, command, 8);
  309. if (result != USB_STOR_XFER_GOOD) {
  310. US_DEBUGP("Result for send_command in write_data %d\n",
  311. result);
  312. /* set_sense_info is superfluous here? */
  313. set_sense_info (3, 0x3, 0);/* peripheral write error */
  314. result = USB_STOR_TRANSPORT_FAILED;
  315. goto leave;
  316. }
  317. /* send the data */
  318. result = sddr55_bulk_transport(us,
  319. DMA_TO_DEVICE, buffer, len);
  320. if (result != USB_STOR_XFER_GOOD) {
  321. US_DEBUGP("Result for send_data in write_data %d\n",
  322. result);
  323. /* set_sense_info is superfluous here? */
  324. set_sense_info (3, 0x3, 0);/* peripheral write error */
  325. result = USB_STOR_TRANSPORT_FAILED;
  326. goto leave;
  327. }
  328. /* now read status */
  329. result = sddr55_bulk_transport(us, DMA_FROM_DEVICE, status, 6);
  330. if (result != USB_STOR_XFER_GOOD) {
  331. US_DEBUGP("Result for get_status in write_data %d\n",
  332. result);
  333. /* set_sense_info is superfluous here? */
  334. set_sense_info (3, 0x3, 0);/* peripheral write error */
  335. result = USB_STOR_TRANSPORT_FAILED;
  336. goto leave;
  337. }
  338. new_pba = (status[3] + (status[4] << 8) + (status[5] << 16))
  339. >> info->blockshift;
  340. /* check status for error */
  341. if (status[0] == 0xff && status[1] == 0x4) {
  342. info->pba_to_lba[new_pba] = BAD_BLOCK;
  343. set_sense_info (3, 0x0c, 0);
  344. result = USB_STOR_TRANSPORT_FAILED;
  345. goto leave;
  346. }
  347. US_DEBUGP("Updating maps for LBA %04X: old PBA %04X, new PBA %04X\n",
  348. lba, pba, new_pba);
  349. /* update the lba<->pba maps, note new_pba might be the same as pba */
  350. info->lba_to_pba[lba] = new_pba;
  351. info->pba_to_lba[pba] = UNUSED_BLOCK;
  352. /* check that new_pba wasn't already being used */
  353. if (info->pba_to_lba[new_pba] != UNUSED_BLOCK) {
  354. printk(KERN_ERR "sddr55 error: new PBA %04X already in use for LBA %04X\n",
  355. new_pba, info->pba_to_lba[new_pba]);
  356. info->fatal_error = 1;
  357. set_sense_info (3, 0x31, 0);
  358. result = USB_STOR_TRANSPORT_FAILED;
  359. goto leave;
  360. }
  361. /* update the pba<->lba maps for new_pba */
  362. info->pba_to_lba[new_pba] = lba % 1000;
  363. page = 0;
  364. lba++;
  365. sectors -= pages >> info->smallpageshift;
  366. }
  367. result = USB_STOR_TRANSPORT_GOOD;
  368. leave:
  369. kfree(buffer);
  370. return result;
  371. }
  372. static int sddr55_read_deviceID(struct us_data *us,
  373. unsigned char *manufacturerID,
  374. unsigned char *deviceID) {
  375. int result;
  376. unsigned char *command = us->iobuf;
  377. unsigned char *content = us->iobuf;
  378. memset(command, 0, 8);
  379. command[5] = 0xB0;
  380. command[7] = 0x84;
  381. result = sddr55_bulk_transport(us, DMA_TO_DEVICE, command, 8);
  382. US_DEBUGP("Result of send_control for device ID is %d\n",
  383. result);
  384. if (result != USB_STOR_XFER_GOOD)
  385. return USB_STOR_TRANSPORT_ERROR;
  386. result = sddr55_bulk_transport(us,
  387. DMA_FROM_DEVICE, content, 4);
  388. if (result != USB_STOR_XFER_GOOD)
  389. return USB_STOR_TRANSPORT_ERROR;
  390. *manufacturerID = content[0];
  391. *deviceID = content[1];
  392. if (content[0] != 0xff) {
  393. result = sddr55_bulk_transport(us,
  394. DMA_FROM_DEVICE, content, 2);
  395. }
  396. return USB_STOR_TRANSPORT_GOOD;
  397. }
  398. int sddr55_reset(struct us_data *us) {
  399. return 0;
  400. }
  401. static unsigned long sddr55_get_capacity(struct us_data *us) {
  402. unsigned char manufacturerID;
  403. unsigned char deviceID;
  404. int result;
  405. struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
  406. US_DEBUGP("Reading capacity...\n");
  407. result = sddr55_read_deviceID(us,
  408. &manufacturerID,
  409. &deviceID);
  410. US_DEBUGP("Result of read_deviceID is %d\n",
  411. result);
  412. if (result != USB_STOR_XFER_GOOD)
  413. return 0;
  414. US_DEBUGP("Device ID = %02X\n", deviceID);
  415. US_DEBUGP("Manuf ID = %02X\n", manufacturerID);
  416. info->pageshift = 9;
  417. info->smallpageshift = 0;
  418. info->blocksize = 16;
  419. info->blockshift = 4;
  420. info->blockmask = 15;
  421. switch (deviceID) {
  422. case 0x6e: // 1MB
  423. case 0xe8:
  424. case 0xec:
  425. info->pageshift = 8;
  426. info->smallpageshift = 1;
  427. return 0x00100000;
  428. case 0xea: // 2MB
  429. case 0x64:
  430. info->pageshift = 8;
  431. info->smallpageshift = 1;
  432. case 0x5d: // 5d is a ROM card with pagesize 512.
  433. return 0x00200000;
  434. case 0xe3: // 4MB
  435. case 0xe5:
  436. case 0x6b:
  437. case 0xd5:
  438. return 0x00400000;
  439. case 0xe6: // 8MB
  440. case 0xd6:
  441. return 0x00800000;
  442. case 0x73: // 16MB
  443. info->blocksize = 32;
  444. info->blockshift = 5;
  445. info->blockmask = 31;
  446. return 0x01000000;
  447. case 0x75: // 32MB
  448. info->blocksize = 32;
  449. info->blockshift = 5;
  450. info->blockmask = 31;
  451. return 0x02000000;
  452. case 0x76: // 64MB
  453. info->blocksize = 32;
  454. info->blockshift = 5;
  455. info->blockmask = 31;
  456. return 0x04000000;
  457. case 0x79: // 128MB
  458. info->blocksize = 32;
  459. info->blockshift = 5;
  460. info->blockmask = 31;
  461. return 0x08000000;
  462. default: // unknown
  463. return 0;
  464. }
  465. }
  466. static int sddr55_read_map(struct us_data *us) {
  467. struct sddr55_card_info *info = (struct sddr55_card_info *)(us->extra);
  468. int numblocks;
  469. unsigned char *buffer;
  470. unsigned char *command = us->iobuf;
  471. int i;
  472. unsigned short lba;
  473. unsigned short max_lba;
  474. int result;
  475. if (!info->capacity)
  476. return -1;
  477. numblocks = info->capacity >> (info->blockshift + info->pageshift);
  478. buffer = kmalloc( numblocks * 2, GFP_NOIO );
  479. if (!buffer)
  480. return -1;
  481. memset(command, 0, 8);
  482. command[5] = 0xB0;
  483. command[6] = numblocks * 2 / 256;
  484. command[7] = 0x8A;
  485. result = sddr55_bulk_transport(us, DMA_TO_DEVICE, command, 8);
  486. if ( result != USB_STOR_XFER_GOOD) {
  487. kfree (buffer);
  488. return -1;
  489. }
  490. result = sddr55_bulk_transport(us, DMA_FROM_DEVICE, buffer, numblocks * 2);
  491. if ( result != USB_STOR_XFER_GOOD) {
  492. kfree (buffer);
  493. return -1;
  494. }
  495. result = sddr55_bulk_transport(us, DMA_FROM_DEVICE, command, 2);
  496. if ( result != USB_STOR_XFER_GOOD) {
  497. kfree (buffer);
  498. return -1;
  499. }
  500. kfree(info->lba_to_pba);
  501. kfree(info->pba_to_lba);
  502. info->lba_to_pba = kmalloc(numblocks*sizeof(int), GFP_NOIO);
  503. info->pba_to_lba = kmalloc(numblocks*sizeof(int), GFP_NOIO);
  504. if (info->lba_to_pba == NULL || info->pba_to_lba == NULL) {
  505. kfree(info->lba_to_pba);
  506. kfree(info->pba_to_lba);
  507. info->lba_to_pba = NULL;
  508. info->pba_to_lba = NULL;
  509. kfree(buffer);
  510. return -1;
  511. }
  512. memset(info->lba_to_pba, 0xff, numblocks*sizeof(int));
  513. memset(info->pba_to_lba, 0xff, numblocks*sizeof(int));
  514. /* set maximum lba */
  515. max_lba = info->max_log_blks;
  516. if (max_lba > 1000)
  517. max_lba = 1000;
  518. // Each block is 64 bytes of control data, so block i is located in
  519. // scatterlist block i*64/128k = i*(2^6)*(2^-17) = i*(2^-11)
  520. for (i=0; i<numblocks; i++) {
  521. int zone = i / 1024;
  522. lba = short_pack(buffer[i * 2], buffer[i * 2 + 1]);
  523. /* Every 1024 physical blocks ("zone"), the LBA numbers
  524. * go back to zero, but are within a higher
  525. * block of LBA's. Also, there is a maximum of
  526. * 1000 LBA's per zone. In other words, in PBA
  527. * 1024-2047 you will find LBA 0-999 which are
  528. * really LBA 1000-1999. Yes, this wastes 24
  529. * physical blocks per zone. Go figure.
  530. * These devices can have blocks go bad, so there
  531. * are 24 spare blocks to use when blocks do go bad.
  532. */
  533. /* SDDR55 returns 0xffff for a bad block, and 0x400 for the
  534. * CIS block. (Is this true for cards 8MB or less??)
  535. * Record these in the physical to logical map
  536. */
  537. info->pba_to_lba[i] = lba;
  538. if (lba >= max_lba) {
  539. continue;
  540. }
  541. if (info->lba_to_pba[lba + zone * 1000] != NOT_ALLOCATED &&
  542. !info->force_read_only) {
  543. printk("sddr55: map inconsistency at LBA %04X\n", lba + zone * 1000);
  544. info->force_read_only = 1;
  545. }
  546. if (lba<0x10 || (lba>=0x3E0 && lba<0x3EF))
  547. US_DEBUGP("LBA %04X <-> PBA %04X\n", lba, i);
  548. info->lba_to_pba[lba + zone * 1000] = i;
  549. }
  550. kfree(buffer);
  551. return 0;
  552. }
  553. static void sddr55_card_info_destructor(void *extra) {
  554. struct sddr55_card_info *info = (struct sddr55_card_info *)extra;
  555. if (!extra)
  556. return;
  557. kfree(info->lba_to_pba);
  558. kfree(info->pba_to_lba);
  559. }
  560. /*
  561. * Transport for the Sandisk SDDR-55
  562. */
  563. int sddr55_transport(struct scsi_cmnd *srb, struct us_data *us)
  564. {
  565. int result;
  566. static unsigned char inquiry_response[8] = {
  567. 0x00, 0x80, 0x00, 0x02, 0x1F, 0x00, 0x00, 0x00
  568. };
  569. // write-protected for now, no block descriptor support
  570. static unsigned char mode_page_01[20] = {
  571. 0x0, 0x12, 0x00, 0x80, 0x0, 0x0, 0x0, 0x0,
  572. 0x01, 0x0A,
  573. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  574. };
  575. unsigned char *ptr = us->iobuf;
  576. unsigned long capacity;
  577. unsigned int lba;
  578. unsigned int pba;
  579. unsigned int page;
  580. unsigned short pages;
  581. struct sddr55_card_info *info;
  582. if (!us->extra) {
  583. us->extra = kmalloc(
  584. sizeof(struct sddr55_card_info), GFP_NOIO);
  585. if (!us->extra)
  586. return USB_STOR_TRANSPORT_ERROR;
  587. memset(us->extra, 0, sizeof(struct sddr55_card_info));
  588. us->extra_destructor = sddr55_card_info_destructor;
  589. }
  590. info = (struct sddr55_card_info *)(us->extra);
  591. if (srb->cmnd[0] == REQUEST_SENSE) {
  592. US_DEBUGP("SDDR55: request sense %02x/%02x/%02x\n", info->sense_data[2], info->sense_data[12], info->sense_data[13]);
  593. memcpy (ptr, info->sense_data, sizeof info->sense_data);
  594. ptr[0] = 0x70;
  595. ptr[7] = 11;
  596. usb_stor_set_xfer_buf (ptr, sizeof info->sense_data, srb);
  597. memset (info->sense_data, 0, sizeof info->sense_data);
  598. return USB_STOR_TRANSPORT_GOOD;
  599. }
  600. memset (info->sense_data, 0, sizeof info->sense_data);
  601. /* Dummy up a response for INQUIRY since SDDR55 doesn't
  602. respond to INQUIRY commands */
  603. if (srb->cmnd[0] == INQUIRY) {
  604. memcpy(ptr, inquiry_response, 8);
  605. fill_inquiry_response(us, ptr, 36);
  606. return USB_STOR_TRANSPORT_GOOD;
  607. }
  608. /* only check card status if the map isn't allocated, ie no card seen yet
  609. * or if it's been over half a second since we last accessed it
  610. */
  611. if (info->lba_to_pba == NULL || time_after(jiffies, info->last_access + HZ/2)) {
  612. /* check to see if a card is fitted */
  613. result = sddr55_status (us);
  614. if (result) {
  615. result = sddr55_status (us);
  616. if (!result) {
  617. set_sense_info (6, 0x28, 0); /* new media, set unit attention, not ready to ready */
  618. }
  619. return USB_STOR_TRANSPORT_FAILED;
  620. }
  621. }
  622. /* if we detected a problem with the map when writing,
  623. don't allow any more access */
  624. if (info->fatal_error) {
  625. set_sense_info (3, 0x31, 0);
  626. return USB_STOR_TRANSPORT_FAILED;
  627. }
  628. if (srb->cmnd[0] == READ_CAPACITY) {
  629. capacity = sddr55_get_capacity(us);
  630. if (!capacity) {
  631. set_sense_info (3, 0x30, 0); /* incompatible medium */
  632. return USB_STOR_TRANSPORT_FAILED;
  633. }
  634. info->capacity = capacity;
  635. /* figure out the maximum logical block number, allowing for
  636. * the fact that only 250 out of every 256 are used */
  637. info->max_log_blks = ((info->capacity >> (info->pageshift + info->blockshift)) / 256) * 250;
  638. /* Last page in the card, adjust as we only use 250 out of
  639. * every 256 pages */
  640. capacity = (capacity / 256) * 250;
  641. capacity /= PAGESIZE;
  642. capacity--;
  643. ((__be32 *) ptr)[0] = cpu_to_be32(capacity);
  644. ((__be32 *) ptr)[1] = cpu_to_be32(PAGESIZE);
  645. usb_stor_set_xfer_buf(ptr, 8, srb);
  646. sddr55_read_map(us);
  647. return USB_STOR_TRANSPORT_GOOD;
  648. }
  649. if (srb->cmnd[0] == MODE_SENSE_10) {
  650. memcpy(ptr, mode_page_01, sizeof mode_page_01);
  651. ptr[3] = (info->read_only || info->force_read_only) ? 0x80 : 0;
  652. usb_stor_set_xfer_buf(ptr, sizeof(mode_page_01), srb);
  653. if ( (srb->cmnd[2] & 0x3F) == 0x01 ) {
  654. US_DEBUGP(
  655. "SDDR55: Dummy up request for mode page 1\n");
  656. return USB_STOR_TRANSPORT_GOOD;
  657. } else if ( (srb->cmnd[2] & 0x3F) == 0x3F ) {
  658. US_DEBUGP(
  659. "SDDR55: Dummy up request for all mode pages\n");
  660. return USB_STOR_TRANSPORT_GOOD;
  661. }
  662. set_sense_info (5, 0x24, 0); /* invalid field in command */
  663. return USB_STOR_TRANSPORT_FAILED;
  664. }
  665. if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
  666. US_DEBUGP(
  667. "SDDR55: %s medium removal. Not that I can do"
  668. " anything about it...\n",
  669. (srb->cmnd[4]&0x03) ? "Prevent" : "Allow");
  670. return USB_STOR_TRANSPORT_GOOD;
  671. }
  672. if (srb->cmnd[0] == READ_10 || srb->cmnd[0] == WRITE_10) {
  673. page = short_pack(srb->cmnd[3], srb->cmnd[2]);
  674. page <<= 16;
  675. page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
  676. pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
  677. page <<= info->smallpageshift;
  678. // convert page to block and page-within-block
  679. lba = page >> info->blockshift;
  680. page = page & info->blockmask;
  681. // locate physical block corresponding to logical block
  682. if (lba >= info->max_log_blks) {
  683. US_DEBUGP("Error: Requested LBA %04X exceeds maximum "
  684. "block %04X\n", lba, info->max_log_blks-1);
  685. set_sense_info (5, 0x24, 0); /* invalid field in command */
  686. return USB_STOR_TRANSPORT_FAILED;
  687. }
  688. pba = info->lba_to_pba[lba];
  689. if (srb->cmnd[0] == WRITE_10) {
  690. US_DEBUGP("WRITE_10: write block %04X (LBA %04X) page %01X"
  691. " pages %d\n",
  692. pba, lba, page, pages);
  693. return sddr55_write_data(us, lba, page, pages);
  694. } else {
  695. US_DEBUGP("READ_10: read block %04X (LBA %04X) page %01X"
  696. " pages %d\n",
  697. pba, lba, page, pages);
  698. return sddr55_read_data(us, lba, page, pages);
  699. }
  700. }
  701. if (srb->cmnd[0] == TEST_UNIT_READY) {
  702. return USB_STOR_TRANSPORT_GOOD;
  703. }
  704. if (srb->cmnd[0] == START_STOP) {
  705. return USB_STOR_TRANSPORT_GOOD;
  706. }
  707. set_sense_info (5, 0x20, 0); /* illegal command */
  708. return USB_STOR_TRANSPORT_FAILED; // FIXME: sense buffer?
  709. }