sddr55.c 24 KB

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