sddr55.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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)|(USB_US_TYPE_STOR<<24) }
  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. US_DEBUGP("Result for send_command in status %d\n",
  126. result);
  127. if (result != USB_STOR_XFER_GOOD) {
  128. set_sense_info (4, 0, 0); /* hardware error */
  129. return USB_STOR_TRANSPORT_ERROR;
  130. }
  131. result = sddr55_bulk_transport(us,
  132. DMA_FROM_DEVICE, status, 4);
  133. /* expect to get short transfer if no card fitted */
  134. if (result == USB_STOR_XFER_SHORT || result == USB_STOR_XFER_STALLED) {
  135. /* had a short transfer, no card inserted, free map memory */
  136. kfree(info->lba_to_pba);
  137. kfree(info->pba_to_lba);
  138. info->lba_to_pba = NULL;
  139. info->pba_to_lba = NULL;
  140. info->fatal_error = 0;
  141. info->force_read_only = 0;
  142. set_sense_info (2, 0x3a, 0); /* not ready, medium not present */
  143. return USB_STOR_TRANSPORT_FAILED;
  144. }
  145. if (result != USB_STOR_XFER_GOOD) {
  146. set_sense_info (4, 0, 0); /* hardware error */
  147. return USB_STOR_TRANSPORT_FAILED;
  148. }
  149. /* check write protect status */
  150. info->read_only = (status[0] & 0x20);
  151. /* now read status */
  152. result = sddr55_bulk_transport(us,
  153. DMA_FROM_DEVICE, status, 2);
  154. if (result != USB_STOR_XFER_GOOD) {
  155. set_sense_info (4, 0, 0); /* hardware error */
  156. }
  157. return (result == USB_STOR_XFER_GOOD ?
  158. USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_FAILED);
  159. }
  160. static int sddr55_read_data(struct us_data *us,
  161. unsigned int lba,
  162. unsigned int page,
  163. unsigned short sectors) {
  164. int result = USB_STOR_TRANSPORT_GOOD;
  165. unsigned char *command = us->iobuf;
  166. unsigned char *status = us->iobuf;
  167. struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
  168. unsigned char *buffer;
  169. unsigned int pba;
  170. unsigned long address;
  171. unsigned short pages;
  172. unsigned int len, offset;
  173. struct scatterlist *sg;
  174. // Since we only read in one block at a time, we have to create
  175. // a bounce buffer and move the data a piece at a time between the
  176. // bounce buffer and the actual transfer buffer.
  177. len = min((unsigned int) sectors, (unsigned int) info->blocksize >>
  178. info->smallpageshift) * PAGESIZE;
  179. buffer = kmalloc(len, GFP_NOIO);
  180. if (buffer == NULL)
  181. return USB_STOR_TRANSPORT_ERROR; /* out of memory */
  182. offset = 0;
  183. sg = NULL;
  184. while (sectors>0) {
  185. /* have we got to end? */
  186. if (lba >= info->max_log_blks)
  187. break;
  188. pba = info->lba_to_pba[lba];
  189. // Read as many sectors as possible in this block
  190. pages = min((unsigned int) sectors << info->smallpageshift,
  191. info->blocksize - page);
  192. len = pages << info->pageshift;
  193. US_DEBUGP("Read %02X pages, from PBA %04X"
  194. " (LBA %04X) page %02X\n",
  195. pages, pba, lba, page);
  196. if (pba == NOT_ALLOCATED) {
  197. /* no pba for this lba, fill with zeroes */
  198. memset (buffer, 0, len);
  199. } else {
  200. address = (pba << info->blockshift) + page;
  201. command[0] = 0;
  202. command[1] = LSB_of(address>>16);
  203. command[2] = LSB_of(address>>8);
  204. command[3] = LSB_of(address);
  205. command[4] = 0;
  206. command[5] = 0xB0;
  207. command[6] = LSB_of(pages << (1 - info->smallpageshift));
  208. command[7] = 0x85;
  209. /* send command */
  210. result = sddr55_bulk_transport(us,
  211. DMA_TO_DEVICE, command, 8);
  212. US_DEBUGP("Result for send_command in read_data %d\n",
  213. result);
  214. if (result != USB_STOR_XFER_GOOD) {
  215. result = USB_STOR_TRANSPORT_ERROR;
  216. goto leave;
  217. }
  218. /* read data */
  219. result = sddr55_bulk_transport(us,
  220. DMA_FROM_DEVICE, buffer, len);
  221. if (result != USB_STOR_XFER_GOOD) {
  222. result = USB_STOR_TRANSPORT_ERROR;
  223. goto leave;
  224. }
  225. /* now read status */
  226. result = sddr55_bulk_transport(us,
  227. DMA_FROM_DEVICE, status, 2);
  228. if (result != USB_STOR_XFER_GOOD) {
  229. result = USB_STOR_TRANSPORT_ERROR;
  230. goto leave;
  231. }
  232. /* check status for error */
  233. if (status[0] == 0xff && status[1] == 0x4) {
  234. set_sense_info (3, 0x11, 0);
  235. result = USB_STOR_TRANSPORT_FAILED;
  236. goto leave;
  237. }
  238. }
  239. // Store the data in the transfer buffer
  240. usb_stor_access_xfer_buf(buffer, len, us->srb,
  241. &sg, &offset, TO_XFER_BUF);
  242. page = 0;
  243. lba++;
  244. sectors -= pages >> info->smallpageshift;
  245. }
  246. result = USB_STOR_TRANSPORT_GOOD;
  247. leave:
  248. kfree(buffer);
  249. return result;
  250. }
  251. static int sddr55_write_data(struct us_data *us,
  252. unsigned int lba,
  253. unsigned int page,
  254. unsigned short sectors) {
  255. int result = USB_STOR_TRANSPORT_GOOD;
  256. unsigned char *command = us->iobuf;
  257. unsigned char *status = us->iobuf;
  258. struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
  259. unsigned char *buffer;
  260. unsigned int pba;
  261. unsigned int new_pba;
  262. unsigned long address;
  263. unsigned short pages;
  264. int i;
  265. unsigned int len, offset;
  266. struct scatterlist *sg;
  267. /* check if we are allowed to write */
  268. if (info->read_only || info->force_read_only) {
  269. set_sense_info (7, 0x27, 0); /* read only */
  270. return USB_STOR_TRANSPORT_FAILED;
  271. }
  272. // Since we only write one block at a time, we have to create
  273. // a bounce buffer and move the data a piece at a time between the
  274. // bounce buffer and the actual transfer buffer.
  275. len = min((unsigned int) sectors, (unsigned int) info->blocksize >>
  276. info->smallpageshift) * PAGESIZE;
  277. buffer = kmalloc(len, GFP_NOIO);
  278. if (buffer == NULL)
  279. return USB_STOR_TRANSPORT_ERROR;
  280. offset = 0;
  281. sg = NULL;
  282. while (sectors > 0) {
  283. /* have we got to end? */
  284. if (lba >= info->max_log_blks)
  285. break;
  286. pba = info->lba_to_pba[lba];
  287. // Write as many sectors as possible in this block
  288. pages = min((unsigned int) sectors << info->smallpageshift,
  289. info->blocksize - page);
  290. len = pages << info->pageshift;
  291. // Get the data from the transfer buffer
  292. usb_stor_access_xfer_buf(buffer, len, us->srb,
  293. &sg, &offset, FROM_XFER_BUF);
  294. US_DEBUGP("Write %02X pages, to PBA %04X"
  295. " (LBA %04X) page %02X\n",
  296. pages, pba, lba, page);
  297. command[4] = 0;
  298. if (pba == NOT_ALLOCATED) {
  299. /* no pba allocated for this lba, find a free pba to use */
  300. int max_pba = (info->max_log_blks / 250 ) * 256;
  301. int found_count = 0;
  302. int found_pba = -1;
  303. /* set pba to first block in zone lba is in */
  304. pba = (lba / 1000) * 1024;
  305. US_DEBUGP("No PBA for LBA %04X\n",lba);
  306. if (max_pba > 1024)
  307. max_pba = 1024;
  308. /*
  309. * Scan through the map looking for an unused block
  310. * leave 16 unused blocks at start (or as many as
  311. * possible) since the sddr55 seems to reuse a used
  312. * block when it shouldn't if we don't leave space.
  313. */
  314. for (i = 0; i < max_pba; i++, pba++) {
  315. if (info->pba_to_lba[pba] == UNUSED_BLOCK) {
  316. found_pba = pba;
  317. if (found_count++ > 16)
  318. break;
  319. }
  320. }
  321. pba = found_pba;
  322. if (pba == -1) {
  323. /* oh dear */
  324. US_DEBUGP("Couldn't find unallocated block\n");
  325. set_sense_info (3, 0x31, 0); /* medium error */
  326. result = USB_STOR_TRANSPORT_FAILED;
  327. goto leave;
  328. }
  329. US_DEBUGP("Allocating PBA %04X for LBA %04X\n", pba, lba);
  330. /* set writing to unallocated block flag */
  331. command[4] = 0x40;
  332. }
  333. address = (pba << info->blockshift) + page;
  334. command[1] = LSB_of(address>>16);
  335. command[2] = LSB_of(address>>8);
  336. command[3] = LSB_of(address);
  337. /* set the lba into the command, modulo 1000 */
  338. command[0] = LSB_of(lba % 1000);
  339. command[6] = MSB_of(lba % 1000);
  340. command[4] |= LSB_of(pages >> info->smallpageshift);
  341. command[5] = 0xB0;
  342. command[7] = 0x86;
  343. /* send command */
  344. result = sddr55_bulk_transport(us,
  345. DMA_TO_DEVICE, command, 8);
  346. if (result != USB_STOR_XFER_GOOD) {
  347. US_DEBUGP("Result for send_command 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. /* send the data */
  355. result = sddr55_bulk_transport(us,
  356. DMA_TO_DEVICE, buffer, len);
  357. if (result != USB_STOR_XFER_GOOD) {
  358. US_DEBUGP("Result for send_data 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. /* now read status */
  366. result = sddr55_bulk_transport(us, DMA_FROM_DEVICE, status, 6);
  367. if (result != USB_STOR_XFER_GOOD) {
  368. US_DEBUGP("Result for get_status in write_data %d\n",
  369. result);
  370. /* set_sense_info is superfluous here? */
  371. set_sense_info (3, 0x3, 0);/* peripheral write error */
  372. result = USB_STOR_TRANSPORT_FAILED;
  373. goto leave;
  374. }
  375. new_pba = (status[3] + (status[4] << 8) + (status[5] << 16))
  376. >> info->blockshift;
  377. /* check status for error */
  378. if (status[0] == 0xff && status[1] == 0x4) {
  379. info->pba_to_lba[new_pba] = BAD_BLOCK;
  380. set_sense_info (3, 0x0c, 0);
  381. result = USB_STOR_TRANSPORT_FAILED;
  382. goto leave;
  383. }
  384. US_DEBUGP("Updating maps for LBA %04X: old PBA %04X, new PBA %04X\n",
  385. lba, pba, new_pba);
  386. /* update the lba<->pba maps, note new_pba might be the same as pba */
  387. info->lba_to_pba[lba] = new_pba;
  388. info->pba_to_lba[pba] = UNUSED_BLOCK;
  389. /* check that new_pba wasn't already being used */
  390. if (info->pba_to_lba[new_pba] != UNUSED_BLOCK) {
  391. printk(KERN_ERR "sddr55 error: new PBA %04X already in use for LBA %04X\n",
  392. new_pba, info->pba_to_lba[new_pba]);
  393. info->fatal_error = 1;
  394. set_sense_info (3, 0x31, 0);
  395. result = USB_STOR_TRANSPORT_FAILED;
  396. goto leave;
  397. }
  398. /* update the pba<->lba maps for new_pba */
  399. info->pba_to_lba[new_pba] = lba % 1000;
  400. page = 0;
  401. lba++;
  402. sectors -= pages >> info->smallpageshift;
  403. }
  404. result = USB_STOR_TRANSPORT_GOOD;
  405. leave:
  406. kfree(buffer);
  407. return result;
  408. }
  409. static int sddr55_read_deviceID(struct us_data *us,
  410. unsigned char *manufacturerID,
  411. unsigned char *deviceID) {
  412. int result;
  413. unsigned char *command = us->iobuf;
  414. unsigned char *content = us->iobuf;
  415. memset(command, 0, 8);
  416. command[5] = 0xB0;
  417. command[7] = 0x84;
  418. result = sddr55_bulk_transport(us, DMA_TO_DEVICE, command, 8);
  419. US_DEBUGP("Result of send_control for device ID is %d\n",
  420. result);
  421. if (result != USB_STOR_XFER_GOOD)
  422. return USB_STOR_TRANSPORT_ERROR;
  423. result = sddr55_bulk_transport(us,
  424. DMA_FROM_DEVICE, content, 4);
  425. if (result != USB_STOR_XFER_GOOD)
  426. return USB_STOR_TRANSPORT_ERROR;
  427. *manufacturerID = content[0];
  428. *deviceID = content[1];
  429. if (content[0] != 0xff) {
  430. result = sddr55_bulk_transport(us,
  431. DMA_FROM_DEVICE, content, 2);
  432. }
  433. return USB_STOR_TRANSPORT_GOOD;
  434. }
  435. static int sddr55_reset(struct us_data *us)
  436. {
  437. return 0;
  438. }
  439. static unsigned long sddr55_get_capacity(struct us_data *us) {
  440. unsigned char uninitialized_var(manufacturerID);
  441. unsigned char uninitialized_var(deviceID);
  442. int result;
  443. struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
  444. US_DEBUGP("Reading capacity...\n");
  445. result = sddr55_read_deviceID(us,
  446. &manufacturerID,
  447. &deviceID);
  448. US_DEBUGP("Result of read_deviceID is %d\n",
  449. result);
  450. if (result != USB_STOR_XFER_GOOD)
  451. return 0;
  452. US_DEBUGP("Device ID = %02X\n", deviceID);
  453. US_DEBUGP("Manuf ID = %02X\n", manufacturerID);
  454. info->pageshift = 9;
  455. info->smallpageshift = 0;
  456. info->blocksize = 16;
  457. info->blockshift = 4;
  458. info->blockmask = 15;
  459. switch (deviceID) {
  460. case 0x6e: // 1MB
  461. case 0xe8:
  462. case 0xec:
  463. info->pageshift = 8;
  464. info->smallpageshift = 1;
  465. return 0x00100000;
  466. case 0xea: // 2MB
  467. case 0x64:
  468. info->pageshift = 8;
  469. info->smallpageshift = 1;
  470. case 0x5d: // 5d is a ROM card with pagesize 512.
  471. return 0x00200000;
  472. case 0xe3: // 4MB
  473. case 0xe5:
  474. case 0x6b:
  475. case 0xd5:
  476. return 0x00400000;
  477. case 0xe6: // 8MB
  478. case 0xd6:
  479. return 0x00800000;
  480. case 0x73: // 16MB
  481. info->blocksize = 32;
  482. info->blockshift = 5;
  483. info->blockmask = 31;
  484. return 0x01000000;
  485. case 0x75: // 32MB
  486. info->blocksize = 32;
  487. info->blockshift = 5;
  488. info->blockmask = 31;
  489. return 0x02000000;
  490. case 0x76: // 64MB
  491. info->blocksize = 32;
  492. info->blockshift = 5;
  493. info->blockmask = 31;
  494. return 0x04000000;
  495. case 0x79: // 128MB
  496. info->blocksize = 32;
  497. info->blockshift = 5;
  498. info->blockmask = 31;
  499. return 0x08000000;
  500. default: // unknown
  501. return 0;
  502. }
  503. }
  504. static int sddr55_read_map(struct us_data *us) {
  505. struct sddr55_card_info *info = (struct sddr55_card_info *)(us->extra);
  506. int numblocks;
  507. unsigned char *buffer;
  508. unsigned char *command = us->iobuf;
  509. int i;
  510. unsigned short lba;
  511. unsigned short max_lba;
  512. int result;
  513. if (!info->capacity)
  514. return -1;
  515. numblocks = info->capacity >> (info->blockshift + info->pageshift);
  516. buffer = kmalloc( numblocks * 2, GFP_NOIO );
  517. if (!buffer)
  518. return -1;
  519. memset(command, 0, 8);
  520. command[5] = 0xB0;
  521. command[6] = numblocks * 2 / 256;
  522. command[7] = 0x8A;
  523. result = sddr55_bulk_transport(us, DMA_TO_DEVICE, command, 8);
  524. if ( result != USB_STOR_XFER_GOOD) {
  525. kfree (buffer);
  526. return -1;
  527. }
  528. result = sddr55_bulk_transport(us, DMA_FROM_DEVICE, buffer, numblocks * 2);
  529. if ( result != USB_STOR_XFER_GOOD) {
  530. kfree (buffer);
  531. return -1;
  532. }
  533. result = sddr55_bulk_transport(us, DMA_FROM_DEVICE, command, 2);
  534. if ( result != USB_STOR_XFER_GOOD) {
  535. kfree (buffer);
  536. return -1;
  537. }
  538. kfree(info->lba_to_pba);
  539. kfree(info->pba_to_lba);
  540. info->lba_to_pba = kmalloc(numblocks*sizeof(int), GFP_NOIO);
  541. info->pba_to_lba = kmalloc(numblocks*sizeof(int), GFP_NOIO);
  542. if (info->lba_to_pba == NULL || info->pba_to_lba == NULL) {
  543. kfree(info->lba_to_pba);
  544. kfree(info->pba_to_lba);
  545. info->lba_to_pba = NULL;
  546. info->pba_to_lba = NULL;
  547. kfree(buffer);
  548. return -1;
  549. }
  550. memset(info->lba_to_pba, 0xff, numblocks*sizeof(int));
  551. memset(info->pba_to_lba, 0xff, numblocks*sizeof(int));
  552. /* set maximum lba */
  553. max_lba = info->max_log_blks;
  554. if (max_lba > 1000)
  555. max_lba = 1000;
  556. // Each block is 64 bytes of control data, so block i is located in
  557. // scatterlist block i*64/128k = i*(2^6)*(2^-17) = i*(2^-11)
  558. for (i=0; i<numblocks; i++) {
  559. int zone = i / 1024;
  560. lba = short_pack(buffer[i * 2], buffer[i * 2 + 1]);
  561. /* Every 1024 physical blocks ("zone"), the LBA numbers
  562. * go back to zero, but are within a higher
  563. * block of LBA's. Also, there is a maximum of
  564. * 1000 LBA's per zone. In other words, in PBA
  565. * 1024-2047 you will find LBA 0-999 which are
  566. * really LBA 1000-1999. Yes, this wastes 24
  567. * physical blocks per zone. Go figure.
  568. * These devices can have blocks go bad, so there
  569. * are 24 spare blocks to use when blocks do go bad.
  570. */
  571. /* SDDR55 returns 0xffff for a bad block, and 0x400 for the
  572. * CIS block. (Is this true for cards 8MB or less??)
  573. * Record these in the physical to logical map
  574. */
  575. info->pba_to_lba[i] = lba;
  576. if (lba >= max_lba) {
  577. continue;
  578. }
  579. if (info->lba_to_pba[lba + zone * 1000] != NOT_ALLOCATED &&
  580. !info->force_read_only) {
  581. printk(KERN_WARNING
  582. "sddr55: map inconsistency at LBA %04X\n",
  583. lba + zone * 1000);
  584. info->force_read_only = 1;
  585. }
  586. if (lba<0x10 || (lba>=0x3E0 && lba<0x3EF))
  587. US_DEBUGP("LBA %04X <-> PBA %04X\n", lba, i);
  588. info->lba_to_pba[lba + zone * 1000] = i;
  589. }
  590. kfree(buffer);
  591. return 0;
  592. }
  593. static void sddr55_card_info_destructor(void *extra) {
  594. struct sddr55_card_info *info = (struct sddr55_card_info *)extra;
  595. if (!extra)
  596. return;
  597. kfree(info->lba_to_pba);
  598. kfree(info->pba_to_lba);
  599. }
  600. /*
  601. * Transport for the Sandisk SDDR-55
  602. */
  603. static int sddr55_transport(struct scsi_cmnd *srb, struct us_data *us)
  604. {
  605. int result;
  606. static unsigned char inquiry_response[8] = {
  607. 0x00, 0x80, 0x00, 0x02, 0x1F, 0x00, 0x00, 0x00
  608. };
  609. // write-protected for now, no block descriptor support
  610. static unsigned char mode_page_01[20] = {
  611. 0x0, 0x12, 0x00, 0x80, 0x0, 0x0, 0x0, 0x0,
  612. 0x01, 0x0A,
  613. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  614. };
  615. unsigned char *ptr = us->iobuf;
  616. unsigned long capacity;
  617. unsigned int lba;
  618. unsigned int pba;
  619. unsigned int page;
  620. unsigned short pages;
  621. struct sddr55_card_info *info;
  622. if (!us->extra) {
  623. us->extra = kzalloc(
  624. sizeof(struct sddr55_card_info), GFP_NOIO);
  625. if (!us->extra)
  626. return USB_STOR_TRANSPORT_ERROR;
  627. us->extra_destructor = sddr55_card_info_destructor;
  628. }
  629. info = (struct sddr55_card_info *)(us->extra);
  630. if (srb->cmnd[0] == REQUEST_SENSE) {
  631. US_DEBUGP("SDDR55: request sense %02x/%02x/%02x\n", info->sense_data[2], info->sense_data[12], 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. US_DEBUGP(
  694. "SDDR55: Dummy up request for mode page 1\n");
  695. return USB_STOR_TRANSPORT_GOOD;
  696. } else if ( (srb->cmnd[2] & 0x3F) == 0x3F ) {
  697. US_DEBUGP(
  698. "SDDR55: Dummy up request for all mode pages\n");
  699. return USB_STOR_TRANSPORT_GOOD;
  700. }
  701. set_sense_info (5, 0x24, 0); /* invalid field in command */
  702. return USB_STOR_TRANSPORT_FAILED;
  703. }
  704. if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
  705. US_DEBUGP(
  706. "SDDR55: %s medium removal. Not that I can do"
  707. " anything about it...\n",
  708. (srb->cmnd[4]&0x03) ? "Prevent" : "Allow");
  709. return USB_STOR_TRANSPORT_GOOD;
  710. }
  711. if (srb->cmnd[0] == READ_10 || srb->cmnd[0] == WRITE_10) {
  712. page = short_pack(srb->cmnd[3], srb->cmnd[2]);
  713. page <<= 16;
  714. page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
  715. pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
  716. page <<= info->smallpageshift;
  717. // convert page to block and page-within-block
  718. lba = page >> info->blockshift;
  719. page = page & info->blockmask;
  720. // locate physical block corresponding to logical block
  721. if (lba >= info->max_log_blks) {
  722. US_DEBUGP("Error: Requested LBA %04X exceeds maximum "
  723. "block %04X\n", lba, info->max_log_blks-1);
  724. set_sense_info (5, 0x24, 0); /* invalid field in command */
  725. return USB_STOR_TRANSPORT_FAILED;
  726. }
  727. pba = info->lba_to_pba[lba];
  728. if (srb->cmnd[0] == WRITE_10) {
  729. US_DEBUGP("WRITE_10: write block %04X (LBA %04X) page %01X"
  730. " pages %d\n",
  731. pba, lba, page, pages);
  732. return sddr55_write_data(us, lba, page, pages);
  733. } else {
  734. US_DEBUGP("READ_10: read block %04X (LBA %04X) page %01X"
  735. " 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 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. if (result)
  757. return result;
  758. us->transport_name = "SDDR55";
  759. us->transport = sddr55_transport;
  760. us->transport_reset = sddr55_reset;
  761. us->max_lun = 0;
  762. result = usb_stor_probe2(us);
  763. return result;
  764. }
  765. static struct usb_driver sddr55_driver = {
  766. .name = "ums-sddr55",
  767. .probe = sddr55_probe,
  768. .disconnect = usb_stor_disconnect,
  769. .suspend = usb_stor_suspend,
  770. .resume = usb_stor_resume,
  771. .reset_resume = usb_stor_reset_resume,
  772. .pre_reset = usb_stor_pre_reset,
  773. .post_reset = usb_stor_post_reset,
  774. .id_table = sddr55_usb_ids,
  775. .soft_unbind = 1,
  776. .no_dynamic_id = 1,
  777. };
  778. module_usb_driver(sddr55_driver);