sddr55.c 25 KB

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