sddr55.c 25 KB

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