sddr09.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for SanDisk SDDR-09 SmartMedia reader
  4. *
  5. * (c) 2000, 2001 Robert Baruch (autophile@starband.net)
  6. * (c) 2002 Andries Brouwer (aeb@cwi.nl)
  7. * Developed with the assistance of:
  8. * (c) 2002 Alan Stern <stern@rowland.org>
  9. *
  10. * The SanDisk SDDR-09 SmartMedia reader uses the Shuttle EUSB-01 chip.
  11. * This chip is a programmable USB controller. In the SDDR-09, it has
  12. * been programmed to obey a certain limited set of SCSI commands.
  13. * This driver translates the "real" SCSI commands to the SDDR-09 SCSI
  14. * commands.
  15. */
  16. /*
  17. * Known vendor commands: 12 bytes, first byte is opcode
  18. *
  19. * E7: read scatter gather
  20. * E8: read
  21. * E9: write
  22. * EA: erase
  23. * EB: reset
  24. * EC: read status
  25. * ED: read ID
  26. * EE: write CIS (?)
  27. * EF: compute checksum (?)
  28. */
  29. #include <linux/errno.h>
  30. #include <linux/module.h>
  31. #include <linux/slab.h>
  32. #include <scsi/scsi.h>
  33. #include <scsi/scsi_cmnd.h>
  34. #include <scsi/scsi_device.h>
  35. #include "usb.h"
  36. #include "transport.h"
  37. #include "protocol.h"
  38. #include "debug.h"
  39. #include "scsiglue.h"
  40. #define DRV_NAME "ums-sddr09"
  41. MODULE_DESCRIPTION("Driver for SanDisk SDDR-09 SmartMedia reader");
  42. MODULE_AUTHOR("Andries Brouwer <aeb@cwi.nl>, Robert Baruch <autophile@starband.net>");
  43. MODULE_LICENSE("GPL");
  44. static int usb_stor_sddr09_dpcm_init(struct us_data *us);
  45. static int sddr09_transport(struct scsi_cmnd *srb, struct us_data *us);
  46. static int usb_stor_sddr09_init(struct us_data *us);
  47. /*
  48. * The table of devices
  49. */
  50. #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
  51. vendorName, productName, useProtocol, useTransport, \
  52. initFunction, flags) \
  53. { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
  54. .driver_info = (flags) }
  55. static struct usb_device_id sddr09_usb_ids[] = {
  56. # include "unusual_sddr09.h"
  57. { } /* Terminating entry */
  58. };
  59. MODULE_DEVICE_TABLE(usb, sddr09_usb_ids);
  60. #undef UNUSUAL_DEV
  61. /*
  62. * The flags table
  63. */
  64. #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
  65. vendor_name, product_name, use_protocol, use_transport, \
  66. init_function, Flags) \
  67. { \
  68. .vendorName = vendor_name, \
  69. .productName = product_name, \
  70. .useProtocol = use_protocol, \
  71. .useTransport = use_transport, \
  72. .initFunction = init_function, \
  73. }
  74. static struct us_unusual_dev sddr09_unusual_dev_list[] = {
  75. # include "unusual_sddr09.h"
  76. { } /* Terminating entry */
  77. };
  78. #undef UNUSUAL_DEV
  79. #define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) )
  80. #define LSB_of(s) ((s)&0xFF)
  81. #define MSB_of(s) ((s)>>8)
  82. /*
  83. * First some stuff that does not belong here:
  84. * data on SmartMedia and other cards, completely
  85. * unrelated to this driver.
  86. * Similar stuff occurs in <linux/mtd/nand_ids.h>.
  87. */
  88. struct nand_flash_dev {
  89. int model_id;
  90. int chipshift; /* 1<<cs bytes total capacity */
  91. char pageshift; /* 1<<ps bytes in a page */
  92. char blockshift; /* 1<<bs pages in an erase block */
  93. char zoneshift; /* 1<<zs blocks in a zone */
  94. /* # of logical blocks is 125/128 of this */
  95. char pageadrlen; /* length of an address in bytes - 1 */
  96. };
  97. /*
  98. * NAND Flash Manufacturer ID Codes
  99. */
  100. #define NAND_MFR_AMD 0x01
  101. #define NAND_MFR_NATSEMI 0x8f
  102. #define NAND_MFR_TOSHIBA 0x98
  103. #define NAND_MFR_SAMSUNG 0xec
  104. static inline char *nand_flash_manufacturer(int manuf_id) {
  105. switch(manuf_id) {
  106. case NAND_MFR_AMD:
  107. return "AMD";
  108. case NAND_MFR_NATSEMI:
  109. return "NATSEMI";
  110. case NAND_MFR_TOSHIBA:
  111. return "Toshiba";
  112. case NAND_MFR_SAMSUNG:
  113. return "Samsung";
  114. default:
  115. return "unknown";
  116. }
  117. }
  118. /*
  119. * It looks like it is unnecessary to attach manufacturer to the
  120. * remaining data: SSFDC prescribes manufacturer-independent id codes.
  121. *
  122. * 256 MB NAND flash has a 5-byte ID with 2nd byte 0xaa, 0xba, 0xca or 0xda.
  123. */
  124. static struct nand_flash_dev nand_flash_ids[] = {
  125. /* NAND flash */
  126. { 0x6e, 20, 8, 4, 8, 2}, /* 1 MB */
  127. { 0xe8, 20, 8, 4, 8, 2}, /* 1 MB */
  128. { 0xec, 20, 8, 4, 8, 2}, /* 1 MB */
  129. { 0x64, 21, 8, 4, 9, 2}, /* 2 MB */
  130. { 0xea, 21, 8, 4, 9, 2}, /* 2 MB */
  131. { 0x6b, 22, 9, 4, 9, 2}, /* 4 MB */
  132. { 0xe3, 22, 9, 4, 9, 2}, /* 4 MB */
  133. { 0xe5, 22, 9, 4, 9, 2}, /* 4 MB */
  134. { 0xe6, 23, 9, 4, 10, 2}, /* 8 MB */
  135. { 0x73, 24, 9, 5, 10, 2}, /* 16 MB */
  136. { 0x75, 25, 9, 5, 10, 2}, /* 32 MB */
  137. { 0x76, 26, 9, 5, 10, 3}, /* 64 MB */
  138. { 0x79, 27, 9, 5, 10, 3}, /* 128 MB */
  139. /* MASK ROM */
  140. { 0x5d, 21, 9, 4, 8, 2}, /* 2 MB */
  141. { 0xd5, 22, 9, 4, 9, 2}, /* 4 MB */
  142. { 0xd6, 23, 9, 4, 10, 2}, /* 8 MB */
  143. { 0x57, 24, 9, 4, 11, 2}, /* 16 MB */
  144. { 0x58, 25, 9, 4, 12, 2}, /* 32 MB */
  145. { 0,}
  146. };
  147. static struct nand_flash_dev *
  148. nand_find_id(unsigned char id) {
  149. int i;
  150. for (i = 0; i < ARRAY_SIZE(nand_flash_ids); i++)
  151. if (nand_flash_ids[i].model_id == id)
  152. return &(nand_flash_ids[i]);
  153. return NULL;
  154. }
  155. /*
  156. * ECC computation.
  157. */
  158. static unsigned char parity[256];
  159. static unsigned char ecc2[256];
  160. static void nand_init_ecc(void) {
  161. int i, j, a;
  162. parity[0] = 0;
  163. for (i = 1; i < 256; i++)
  164. parity[i] = (parity[i&(i-1)] ^ 1);
  165. for (i = 0; i < 256; i++) {
  166. a = 0;
  167. for (j = 0; j < 8; j++) {
  168. if (i & (1<<j)) {
  169. if ((j & 1) == 0)
  170. a ^= 0x04;
  171. if ((j & 2) == 0)
  172. a ^= 0x10;
  173. if ((j & 4) == 0)
  174. a ^= 0x40;
  175. }
  176. }
  177. ecc2[i] = ~(a ^ (a<<1) ^ (parity[i] ? 0xa8 : 0));
  178. }
  179. }
  180. /* compute 3-byte ecc on 256 bytes */
  181. static void nand_compute_ecc(unsigned char *data, unsigned char *ecc) {
  182. int i, j, a;
  183. unsigned char par = 0, bit, bits[8] = {0};
  184. /* collect 16 checksum bits */
  185. for (i = 0; i < 256; i++) {
  186. par ^= data[i];
  187. bit = parity[data[i]];
  188. for (j = 0; j < 8; j++)
  189. if ((i & (1<<j)) == 0)
  190. bits[j] ^= bit;
  191. }
  192. /* put 4+4+4 = 12 bits in the ecc */
  193. a = (bits[3] << 6) + (bits[2] << 4) + (bits[1] << 2) + bits[0];
  194. ecc[0] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
  195. a = (bits[7] << 6) + (bits[6] << 4) + (bits[5] << 2) + bits[4];
  196. ecc[1] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
  197. ecc[2] = ecc2[par];
  198. }
  199. static int nand_compare_ecc(unsigned char *data, unsigned char *ecc) {
  200. return (data[0] == ecc[0] && data[1] == ecc[1] && data[2] == ecc[2]);
  201. }
  202. static void nand_store_ecc(unsigned char *data, unsigned char *ecc) {
  203. memcpy(data, ecc, 3);
  204. }
  205. /*
  206. * The actual driver starts here.
  207. */
  208. struct sddr09_card_info {
  209. unsigned long capacity; /* Size of card in bytes */
  210. int pagesize; /* Size of page in bytes */
  211. int pageshift; /* log2 of pagesize */
  212. int blocksize; /* Size of block in pages */
  213. int blockshift; /* log2 of blocksize */
  214. int blockmask; /* 2^blockshift - 1 */
  215. int *lba_to_pba; /* logical to physical map */
  216. int *pba_to_lba; /* physical to logical map */
  217. int lbact; /* number of available pages */
  218. int flags;
  219. #define SDDR09_WP 1 /* write protected */
  220. };
  221. /*
  222. * On my 16MB card, control blocks have size 64 (16 real control bytes,
  223. * and 48 junk bytes). In reality of course the card uses 16 control bytes,
  224. * so the reader makes up the remaining 48. Don't know whether these numbers
  225. * depend on the card. For now a constant.
  226. */
  227. #define CONTROL_SHIFT 6
  228. /*
  229. * On my Combo CF/SM reader, the SM reader has LUN 1.
  230. * (and things fail with LUN 0).
  231. * It seems LUN is irrelevant for others.
  232. */
  233. #define LUN 1
  234. #define LUNBITS (LUN << 5)
  235. /*
  236. * LBA and PBA are unsigned ints. Special values.
  237. */
  238. #define UNDEF 0xffffffff
  239. #define SPARE 0xfffffffe
  240. #define UNUSABLE 0xfffffffd
  241. static const int erase_bad_lba_entries = 0;
  242. /* send vendor interface command (0x41) */
  243. /* called for requests 0, 1, 8 */
  244. static int
  245. sddr09_send_command(struct us_data *us,
  246. unsigned char request,
  247. unsigned char direction,
  248. unsigned char *xfer_data,
  249. unsigned int xfer_len) {
  250. unsigned int pipe;
  251. unsigned char requesttype = (0x41 | direction);
  252. int rc;
  253. // Get the receive or send control pipe number
  254. if (direction == USB_DIR_IN)
  255. pipe = us->recv_ctrl_pipe;
  256. else
  257. pipe = us->send_ctrl_pipe;
  258. rc = usb_stor_ctrl_transfer(us, pipe, request, requesttype,
  259. 0, 0, xfer_data, xfer_len);
  260. switch (rc) {
  261. case USB_STOR_XFER_GOOD: return 0;
  262. case USB_STOR_XFER_STALLED: return -EPIPE;
  263. default: return -EIO;
  264. }
  265. }
  266. static int
  267. sddr09_send_scsi_command(struct us_data *us,
  268. unsigned char *command,
  269. unsigned int command_len) {
  270. return sddr09_send_command(us, 0, USB_DIR_OUT, command, command_len);
  271. }
  272. #if 0
  273. /*
  274. * Test Unit Ready Command: 12 bytes.
  275. * byte 0: opcode: 00
  276. */
  277. static int
  278. sddr09_test_unit_ready(struct us_data *us) {
  279. unsigned char *command = us->iobuf;
  280. int result;
  281. memset(command, 0, 6);
  282. command[1] = LUNBITS;
  283. result = sddr09_send_scsi_command(us, command, 6);
  284. usb_stor_dbg(us, "sddr09_test_unit_ready returns %d\n", result);
  285. return result;
  286. }
  287. #endif
  288. /*
  289. * Request Sense Command: 12 bytes.
  290. * byte 0: opcode: 03
  291. * byte 4: data length
  292. */
  293. static int
  294. sddr09_request_sense(struct us_data *us, unsigned char *sensebuf, int buflen) {
  295. unsigned char *command = us->iobuf;
  296. int result;
  297. memset(command, 0, 12);
  298. command[0] = 0x03;
  299. command[1] = LUNBITS;
  300. command[4] = buflen;
  301. result = sddr09_send_scsi_command(us, command, 12);
  302. if (result)
  303. return result;
  304. result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  305. sensebuf, buflen, NULL);
  306. return (result == USB_STOR_XFER_GOOD ? 0 : -EIO);
  307. }
  308. /*
  309. * Read Command: 12 bytes.
  310. * byte 0: opcode: E8
  311. * byte 1: last two bits: 00: read data, 01: read blockwise control,
  312. * 10: read both, 11: read pagewise control.
  313. * It turns out we need values 20, 21, 22, 23 here (LUN 1).
  314. * bytes 2-5: address (interpretation depends on byte 1, see below)
  315. * bytes 10-11: count (idem)
  316. *
  317. * A page has 512 data bytes and 64 control bytes (16 control and 48 junk).
  318. * A read data command gets data in 512-byte pages.
  319. * A read control command gets control in 64-byte chunks.
  320. * A read both command gets data+control in 576-byte chunks.
  321. *
  322. * Blocks are groups of 32 pages, and read blockwise control jumps to the
  323. * next block, while read pagewise control jumps to the next page after
  324. * reading a group of 64 control bytes.
  325. * [Here 512 = 1<<pageshift, 32 = 1<<blockshift, 64 is constant?]
  326. *
  327. * (1 MB and 2 MB cards are a bit different, but I have only a 16 MB card.)
  328. */
  329. static int
  330. sddr09_readX(struct us_data *us, int x, unsigned long fromaddress,
  331. int nr_of_pages, int bulklen, unsigned char *buf,
  332. int use_sg) {
  333. unsigned char *command = us->iobuf;
  334. int result;
  335. command[0] = 0xE8;
  336. command[1] = LUNBITS | x;
  337. command[2] = MSB_of(fromaddress>>16);
  338. command[3] = LSB_of(fromaddress>>16);
  339. command[4] = MSB_of(fromaddress & 0xFFFF);
  340. command[5] = LSB_of(fromaddress & 0xFFFF);
  341. command[6] = 0;
  342. command[7] = 0;
  343. command[8] = 0;
  344. command[9] = 0;
  345. command[10] = MSB_of(nr_of_pages);
  346. command[11] = LSB_of(nr_of_pages);
  347. result = sddr09_send_scsi_command(us, command, 12);
  348. if (result) {
  349. usb_stor_dbg(us, "Result for send_control in sddr09_read2%d %d\n",
  350. x, result);
  351. return result;
  352. }
  353. result = usb_stor_bulk_transfer_sg(us, us->recv_bulk_pipe,
  354. buf, bulklen, use_sg, NULL);
  355. if (result != USB_STOR_XFER_GOOD) {
  356. usb_stor_dbg(us, "Result for bulk_transfer in sddr09_read2%d %d\n",
  357. x, result);
  358. return -EIO;
  359. }
  360. return 0;
  361. }
  362. /*
  363. * Read Data
  364. *
  365. * fromaddress counts data shorts:
  366. * increasing it by 256 shifts the bytestream by 512 bytes;
  367. * the last 8 bits are ignored.
  368. *
  369. * nr_of_pages counts pages of size (1 << pageshift).
  370. */
  371. static int
  372. sddr09_read20(struct us_data *us, unsigned long fromaddress,
  373. int nr_of_pages, int pageshift, unsigned char *buf, int use_sg) {
  374. int bulklen = nr_of_pages << pageshift;
  375. /* The last 8 bits of fromaddress are ignored. */
  376. return sddr09_readX(us, 0, fromaddress, nr_of_pages, bulklen,
  377. buf, use_sg);
  378. }
  379. /*
  380. * Read Blockwise Control
  381. *
  382. * fromaddress gives the starting position (as in read data;
  383. * the last 8 bits are ignored); increasing it by 32*256 shifts
  384. * the output stream by 64 bytes.
  385. *
  386. * count counts control groups of size (1 << controlshift).
  387. * For me, controlshift = 6. Is this constant?
  388. *
  389. * After getting one control group, jump to the next block
  390. * (fromaddress += 8192).
  391. */
  392. static int
  393. sddr09_read21(struct us_data *us, unsigned long fromaddress,
  394. int count, int controlshift, unsigned char *buf, int use_sg) {
  395. int bulklen = (count << controlshift);
  396. return sddr09_readX(us, 1, fromaddress, count, bulklen,
  397. buf, use_sg);
  398. }
  399. /*
  400. * Read both Data and Control
  401. *
  402. * fromaddress counts data shorts, ignoring control:
  403. * increasing it by 256 shifts the bytestream by 576 = 512+64 bytes;
  404. * the last 8 bits are ignored.
  405. *
  406. * nr_of_pages counts pages of size (1 << pageshift) + (1 << controlshift).
  407. */
  408. static int
  409. sddr09_read22(struct us_data *us, unsigned long fromaddress,
  410. int nr_of_pages, int pageshift, unsigned char *buf, int use_sg) {
  411. int bulklen = (nr_of_pages << pageshift) + (nr_of_pages << CONTROL_SHIFT);
  412. usb_stor_dbg(us, "reading %d pages, %d bytes\n", nr_of_pages, bulklen);
  413. return sddr09_readX(us, 2, fromaddress, nr_of_pages, bulklen,
  414. buf, use_sg);
  415. }
  416. #if 0
  417. /*
  418. * Read Pagewise Control
  419. *
  420. * fromaddress gives the starting position (as in read data;
  421. * the last 8 bits are ignored); increasing it by 256 shifts
  422. * the output stream by 64 bytes.
  423. *
  424. * count counts control groups of size (1 << controlshift).
  425. * For me, controlshift = 6. Is this constant?
  426. *
  427. * After getting one control group, jump to the next page
  428. * (fromaddress += 256).
  429. */
  430. static int
  431. sddr09_read23(struct us_data *us, unsigned long fromaddress,
  432. int count, int controlshift, unsigned char *buf, int use_sg) {
  433. int bulklen = (count << controlshift);
  434. return sddr09_readX(us, 3, fromaddress, count, bulklen,
  435. buf, use_sg);
  436. }
  437. #endif
  438. /*
  439. * Erase Command: 12 bytes.
  440. * byte 0: opcode: EA
  441. * bytes 6-9: erase address (big-endian, counting shorts, sector aligned).
  442. *
  443. * Always precisely one block is erased; bytes 2-5 and 10-11 are ignored.
  444. * The byte address being erased is 2*Eaddress.
  445. * The CIS cannot be erased.
  446. */
  447. static int
  448. sddr09_erase(struct us_data *us, unsigned long Eaddress) {
  449. unsigned char *command = us->iobuf;
  450. int result;
  451. usb_stor_dbg(us, "erase address %lu\n", Eaddress);
  452. memset(command, 0, 12);
  453. command[0] = 0xEA;
  454. command[1] = LUNBITS;
  455. command[6] = MSB_of(Eaddress>>16);
  456. command[7] = LSB_of(Eaddress>>16);
  457. command[8] = MSB_of(Eaddress & 0xFFFF);
  458. command[9] = LSB_of(Eaddress & 0xFFFF);
  459. result = sddr09_send_scsi_command(us, command, 12);
  460. if (result)
  461. usb_stor_dbg(us, "Result for send_control in sddr09_erase %d\n",
  462. result);
  463. return result;
  464. }
  465. /*
  466. * Write CIS Command: 12 bytes.
  467. * byte 0: opcode: EE
  468. * bytes 2-5: write address in shorts
  469. * bytes 10-11: sector count
  470. *
  471. * This writes at the indicated address. Don't know how it differs
  472. * from E9. Maybe it does not erase? However, it will also write to
  473. * the CIS.
  474. *
  475. * When two such commands on the same page follow each other directly,
  476. * the second one is not done.
  477. */
  478. /*
  479. * Write Command: 12 bytes.
  480. * byte 0: opcode: E9
  481. * bytes 2-5: write address (big-endian, counting shorts, sector aligned).
  482. * bytes 6-9: erase address (big-endian, counting shorts, sector aligned).
  483. * bytes 10-11: sector count (big-endian, in 512-byte sectors).
  484. *
  485. * If write address equals erase address, the erase is done first,
  486. * otherwise the write is done first. When erase address equals zero
  487. * no erase is done?
  488. */
  489. static int
  490. sddr09_writeX(struct us_data *us,
  491. unsigned long Waddress, unsigned long Eaddress,
  492. int nr_of_pages, int bulklen, unsigned char *buf, int use_sg) {
  493. unsigned char *command = us->iobuf;
  494. int result;
  495. command[0] = 0xE9;
  496. command[1] = LUNBITS;
  497. command[2] = MSB_of(Waddress>>16);
  498. command[3] = LSB_of(Waddress>>16);
  499. command[4] = MSB_of(Waddress & 0xFFFF);
  500. command[5] = LSB_of(Waddress & 0xFFFF);
  501. command[6] = MSB_of(Eaddress>>16);
  502. command[7] = LSB_of(Eaddress>>16);
  503. command[8] = MSB_of(Eaddress & 0xFFFF);
  504. command[9] = LSB_of(Eaddress & 0xFFFF);
  505. command[10] = MSB_of(nr_of_pages);
  506. command[11] = LSB_of(nr_of_pages);
  507. result = sddr09_send_scsi_command(us, command, 12);
  508. if (result) {
  509. usb_stor_dbg(us, "Result for send_control in sddr09_writeX %d\n",
  510. result);
  511. return result;
  512. }
  513. result = usb_stor_bulk_transfer_sg(us, us->send_bulk_pipe,
  514. buf, bulklen, use_sg, NULL);
  515. if (result != USB_STOR_XFER_GOOD) {
  516. usb_stor_dbg(us, "Result for bulk_transfer in sddr09_writeX %d\n",
  517. result);
  518. return -EIO;
  519. }
  520. return 0;
  521. }
  522. /* erase address, write same address */
  523. static int
  524. sddr09_write_inplace(struct us_data *us, unsigned long address,
  525. int nr_of_pages, int pageshift, unsigned char *buf,
  526. int use_sg) {
  527. int bulklen = (nr_of_pages << pageshift) + (nr_of_pages << CONTROL_SHIFT);
  528. return sddr09_writeX(us, address, address, nr_of_pages, bulklen,
  529. buf, use_sg);
  530. }
  531. #if 0
  532. /*
  533. * Read Scatter Gather Command: 3+4n bytes.
  534. * byte 0: opcode E7
  535. * byte 2: n
  536. * bytes 4i-1,4i,4i+1: page address
  537. * byte 4i+2: page count
  538. * (i=1..n)
  539. *
  540. * This reads several pages from the card to a single memory buffer.
  541. * The last two bits of byte 1 have the same meaning as for E8.
  542. */
  543. static int
  544. sddr09_read_sg_test_only(struct us_data *us) {
  545. unsigned char *command = us->iobuf;
  546. int result, bulklen, nsg, ct;
  547. unsigned char *buf;
  548. unsigned long address;
  549. nsg = bulklen = 0;
  550. command[0] = 0xE7;
  551. command[1] = LUNBITS;
  552. command[2] = 0;
  553. address = 040000; ct = 1;
  554. nsg++;
  555. bulklen += (ct << 9);
  556. command[4*nsg+2] = ct;
  557. command[4*nsg+1] = ((address >> 9) & 0xFF);
  558. command[4*nsg+0] = ((address >> 17) & 0xFF);
  559. command[4*nsg-1] = ((address >> 25) & 0xFF);
  560. address = 0340000; ct = 1;
  561. nsg++;
  562. bulklen += (ct << 9);
  563. command[4*nsg+2] = ct;
  564. command[4*nsg+1] = ((address >> 9) & 0xFF);
  565. command[4*nsg+0] = ((address >> 17) & 0xFF);
  566. command[4*nsg-1] = ((address >> 25) & 0xFF);
  567. address = 01000000; ct = 2;
  568. nsg++;
  569. bulklen += (ct << 9);
  570. command[4*nsg+2] = ct;
  571. command[4*nsg+1] = ((address >> 9) & 0xFF);
  572. command[4*nsg+0] = ((address >> 17) & 0xFF);
  573. command[4*nsg-1] = ((address >> 25) & 0xFF);
  574. command[2] = nsg;
  575. result = sddr09_send_scsi_command(us, command, 4*nsg+3);
  576. if (result) {
  577. usb_stor_dbg(us, "Result for send_control in sddr09_read_sg %d\n",
  578. result);
  579. return result;
  580. }
  581. buf = kmalloc(bulklen, GFP_NOIO);
  582. if (!buf)
  583. return -ENOMEM;
  584. result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  585. buf, bulklen, NULL);
  586. kfree(buf);
  587. if (result != USB_STOR_XFER_GOOD) {
  588. usb_stor_dbg(us, "Result for bulk_transfer in sddr09_read_sg %d\n",
  589. result);
  590. return -EIO;
  591. }
  592. return 0;
  593. }
  594. #endif
  595. /*
  596. * Read Status Command: 12 bytes.
  597. * byte 0: opcode: EC
  598. *
  599. * Returns 64 bytes, all zero except for the first.
  600. * bit 0: 1: Error
  601. * bit 5: 1: Suspended
  602. * bit 6: 1: Ready
  603. * bit 7: 1: Not write-protected
  604. */
  605. static int
  606. sddr09_read_status(struct us_data *us, unsigned char *status) {
  607. unsigned char *command = us->iobuf;
  608. unsigned char *data = us->iobuf;
  609. int result;
  610. usb_stor_dbg(us, "Reading status...\n");
  611. memset(command, 0, 12);
  612. command[0] = 0xEC;
  613. command[1] = LUNBITS;
  614. result = sddr09_send_scsi_command(us, command, 12);
  615. if (result)
  616. return result;
  617. result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  618. data, 64, NULL);
  619. *status = data[0];
  620. return (result == USB_STOR_XFER_GOOD ? 0 : -EIO);
  621. }
  622. static int
  623. sddr09_read_data(struct us_data *us,
  624. unsigned long address,
  625. unsigned int sectors) {
  626. struct sddr09_card_info *info = (struct sddr09_card_info *) us->extra;
  627. unsigned char *buffer;
  628. unsigned int lba, maxlba, pba;
  629. unsigned int page, pages;
  630. unsigned int len, offset;
  631. struct scatterlist *sg;
  632. int result;
  633. // Figure out the initial LBA and page
  634. lba = address >> info->blockshift;
  635. page = (address & info->blockmask);
  636. maxlba = info->capacity >> (info->pageshift + info->blockshift);
  637. if (lba >= maxlba)
  638. return -EIO;
  639. // Since we only read in one block at a time, we have to create
  640. // a bounce buffer and move the data a piece at a time between the
  641. // bounce buffer and the actual transfer buffer.
  642. len = min(sectors, (unsigned int) info->blocksize) * info->pagesize;
  643. buffer = kmalloc(len, GFP_NOIO);
  644. if (!buffer)
  645. return -ENOMEM;
  646. // This could be made much more efficient by checking for
  647. // contiguous LBA's. Another exercise left to the student.
  648. result = 0;
  649. offset = 0;
  650. sg = NULL;
  651. while (sectors > 0) {
  652. /* Find number of pages we can read in this block */
  653. pages = min(sectors, info->blocksize - page);
  654. len = pages << info->pageshift;
  655. /* Not overflowing capacity? */
  656. if (lba >= maxlba) {
  657. usb_stor_dbg(us, "Error: Requested lba %u exceeds maximum %u\n",
  658. lba, maxlba);
  659. result = -EIO;
  660. break;
  661. }
  662. /* Find where this lba lives on disk */
  663. pba = info->lba_to_pba[lba];
  664. if (pba == UNDEF) { /* this lba was never written */
  665. usb_stor_dbg(us, "Read %d zero pages (LBA %d) page %d\n",
  666. pages, lba, page);
  667. /*
  668. * This is not really an error. It just means
  669. * that the block has never been written.
  670. * Instead of returning an error
  671. * it is better to return all zero data.
  672. */
  673. memset(buffer, 0, len);
  674. } else {
  675. usb_stor_dbg(us, "Read %d pages, from PBA %d (LBA %d) page %d\n",
  676. pages, pba, lba, page);
  677. address = ((pba << info->blockshift) + page) <<
  678. info->pageshift;
  679. result = sddr09_read20(us, address>>1,
  680. pages, info->pageshift, buffer, 0);
  681. if (result)
  682. break;
  683. }
  684. // Store the data in the transfer buffer
  685. usb_stor_access_xfer_buf(buffer, len, us->srb,
  686. &sg, &offset, TO_XFER_BUF);
  687. page = 0;
  688. lba++;
  689. sectors -= pages;
  690. }
  691. kfree(buffer);
  692. return result;
  693. }
  694. static unsigned int
  695. sddr09_find_unused_pba(struct sddr09_card_info *info, unsigned int lba) {
  696. static unsigned int lastpba = 1;
  697. int zonestart, end, i;
  698. zonestart = (lba/1000) << 10;
  699. end = info->capacity >> (info->blockshift + info->pageshift);
  700. end -= zonestart;
  701. if (end > 1024)
  702. end = 1024;
  703. for (i = lastpba+1; i < end; i++) {
  704. if (info->pba_to_lba[zonestart+i] == UNDEF) {
  705. lastpba = i;
  706. return zonestart+i;
  707. }
  708. }
  709. for (i = 0; i <= lastpba; i++) {
  710. if (info->pba_to_lba[zonestart+i] == UNDEF) {
  711. lastpba = i;
  712. return zonestart+i;
  713. }
  714. }
  715. return 0;
  716. }
  717. static int
  718. sddr09_write_lba(struct us_data *us, unsigned int lba,
  719. unsigned int page, unsigned int pages,
  720. unsigned char *ptr, unsigned char *blockbuffer) {
  721. struct sddr09_card_info *info = (struct sddr09_card_info *) us->extra;
  722. unsigned long address;
  723. unsigned int pba, lbap;
  724. unsigned int pagelen;
  725. unsigned char *bptr, *cptr, *xptr;
  726. unsigned char ecc[3];
  727. int i, result;
  728. lbap = ((lba % 1000) << 1) | 0x1000;
  729. if (parity[MSB_of(lbap) ^ LSB_of(lbap)])
  730. lbap ^= 1;
  731. pba = info->lba_to_pba[lba];
  732. if (pba == UNDEF) {
  733. pba = sddr09_find_unused_pba(info, lba);
  734. if (!pba) {
  735. printk(KERN_WARNING
  736. "sddr09_write_lba: Out of unused blocks\n");
  737. return -ENOSPC;
  738. }
  739. info->pba_to_lba[pba] = lba;
  740. info->lba_to_pba[lba] = pba;
  741. }
  742. if (pba == 1) {
  743. /*
  744. * Maybe it is impossible to write to PBA 1.
  745. * Fake success, but don't do anything.
  746. */
  747. printk(KERN_WARNING "sddr09: avoid writing to pba 1\n");
  748. return 0;
  749. }
  750. pagelen = (1 << info->pageshift) + (1 << CONTROL_SHIFT);
  751. /* read old contents */
  752. address = (pba << (info->pageshift + info->blockshift));
  753. result = sddr09_read22(us, address>>1, info->blocksize,
  754. info->pageshift, blockbuffer, 0);
  755. if (result)
  756. return result;
  757. /* check old contents and fill lba */
  758. for (i = 0; i < info->blocksize; i++) {
  759. bptr = blockbuffer + i*pagelen;
  760. cptr = bptr + info->pagesize;
  761. nand_compute_ecc(bptr, ecc);
  762. if (!nand_compare_ecc(cptr+13, ecc)) {
  763. usb_stor_dbg(us, "Warning: bad ecc in page %d- of pba %d\n",
  764. i, pba);
  765. nand_store_ecc(cptr+13, ecc);
  766. }
  767. nand_compute_ecc(bptr+(info->pagesize / 2), ecc);
  768. if (!nand_compare_ecc(cptr+8, ecc)) {
  769. usb_stor_dbg(us, "Warning: bad ecc in page %d+ of pba %d\n",
  770. i, pba);
  771. nand_store_ecc(cptr+8, ecc);
  772. }
  773. cptr[6] = cptr[11] = MSB_of(lbap);
  774. cptr[7] = cptr[12] = LSB_of(lbap);
  775. }
  776. /* copy in new stuff and compute ECC */
  777. xptr = ptr;
  778. for (i = page; i < page+pages; i++) {
  779. bptr = blockbuffer + i*pagelen;
  780. cptr = bptr + info->pagesize;
  781. memcpy(bptr, xptr, info->pagesize);
  782. xptr += info->pagesize;
  783. nand_compute_ecc(bptr, ecc);
  784. nand_store_ecc(cptr+13, ecc);
  785. nand_compute_ecc(bptr+(info->pagesize / 2), ecc);
  786. nand_store_ecc(cptr+8, ecc);
  787. }
  788. usb_stor_dbg(us, "Rewrite PBA %d (LBA %d)\n", pba, lba);
  789. result = sddr09_write_inplace(us, address>>1, info->blocksize,
  790. info->pageshift, blockbuffer, 0);
  791. usb_stor_dbg(us, "sddr09_write_inplace returns %d\n", result);
  792. #if 0
  793. {
  794. unsigned char status = 0;
  795. int result2 = sddr09_read_status(us, &status);
  796. if (result2)
  797. usb_stor_dbg(us, "cannot read status\n");
  798. else if (status != 0xc0)
  799. usb_stor_dbg(us, "status after write: 0x%x\n", status);
  800. }
  801. #endif
  802. #if 0
  803. {
  804. int result2 = sddr09_test_unit_ready(us);
  805. }
  806. #endif
  807. return result;
  808. }
  809. static int
  810. sddr09_write_data(struct us_data *us,
  811. unsigned long address,
  812. unsigned int sectors) {
  813. struct sddr09_card_info *info = (struct sddr09_card_info *) us->extra;
  814. unsigned int lba, maxlba, page, pages;
  815. unsigned int pagelen, blocklen;
  816. unsigned char *blockbuffer;
  817. unsigned char *buffer;
  818. unsigned int len, offset;
  819. struct scatterlist *sg;
  820. int result;
  821. /* Figure out the initial LBA and page */
  822. lba = address >> info->blockshift;
  823. page = (address & info->blockmask);
  824. maxlba = info->capacity >> (info->pageshift + info->blockshift);
  825. if (lba >= maxlba)
  826. return -EIO;
  827. /*
  828. * blockbuffer is used for reading in the old data, overwriting
  829. * with the new data, and performing ECC calculations
  830. */
  831. /*
  832. * TODO: instead of doing kmalloc/kfree for each write,
  833. * add a bufferpointer to the info structure
  834. */
  835. pagelen = (1 << info->pageshift) + (1 << CONTROL_SHIFT);
  836. blocklen = (pagelen << info->blockshift);
  837. blockbuffer = kmalloc(blocklen, GFP_NOIO);
  838. if (!blockbuffer)
  839. return -ENOMEM;
  840. /*
  841. * Since we don't write the user data directly to the device,
  842. * we have to create a bounce buffer and move the data a piece
  843. * at a time between the bounce buffer and the actual transfer buffer.
  844. */
  845. len = min(sectors, (unsigned int) info->blocksize) * info->pagesize;
  846. buffer = kmalloc(len, GFP_NOIO);
  847. if (!buffer) {
  848. kfree(blockbuffer);
  849. return -ENOMEM;
  850. }
  851. result = 0;
  852. offset = 0;
  853. sg = NULL;
  854. while (sectors > 0) {
  855. /* Write as many sectors as possible in this block */
  856. pages = min(sectors, info->blocksize - page);
  857. len = (pages << info->pageshift);
  858. /* Not overflowing capacity? */
  859. if (lba >= maxlba) {
  860. usb_stor_dbg(us, "Error: Requested lba %u exceeds maximum %u\n",
  861. lba, maxlba);
  862. result = -EIO;
  863. break;
  864. }
  865. /* Get the data from the transfer buffer */
  866. usb_stor_access_xfer_buf(buffer, len, us->srb,
  867. &sg, &offset, FROM_XFER_BUF);
  868. result = sddr09_write_lba(us, lba, page, pages,
  869. buffer, blockbuffer);
  870. if (result)
  871. break;
  872. page = 0;
  873. lba++;
  874. sectors -= pages;
  875. }
  876. kfree(buffer);
  877. kfree(blockbuffer);
  878. return result;
  879. }
  880. static int
  881. sddr09_read_control(struct us_data *us,
  882. unsigned long address,
  883. unsigned int blocks,
  884. unsigned char *content,
  885. int use_sg) {
  886. usb_stor_dbg(us, "Read control address %lu, blocks %d\n",
  887. address, blocks);
  888. return sddr09_read21(us, address, blocks,
  889. CONTROL_SHIFT, content, use_sg);
  890. }
  891. /*
  892. * Read Device ID Command: 12 bytes.
  893. * byte 0: opcode: ED
  894. *
  895. * Returns 2 bytes: Manufacturer ID and Device ID.
  896. * On more recent cards 3 bytes: the third byte is an option code A5
  897. * signifying that the secret command to read an 128-bit ID is available.
  898. * On still more recent cards 4 bytes: the fourth byte C0 means that
  899. * a second read ID cmd is available.
  900. */
  901. static int
  902. sddr09_read_deviceID(struct us_data *us, unsigned char *deviceID) {
  903. unsigned char *command = us->iobuf;
  904. unsigned char *content = us->iobuf;
  905. int result, i;
  906. memset(command, 0, 12);
  907. command[0] = 0xED;
  908. command[1] = LUNBITS;
  909. result = sddr09_send_scsi_command(us, command, 12);
  910. if (result)
  911. return result;
  912. result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  913. content, 64, NULL);
  914. for (i = 0; i < 4; i++)
  915. deviceID[i] = content[i];
  916. return (result == USB_STOR_XFER_GOOD ? 0 : -EIO);
  917. }
  918. static int
  919. sddr09_get_wp(struct us_data *us, struct sddr09_card_info *info) {
  920. int result;
  921. unsigned char status;
  922. const char *wp_fmt;
  923. result = sddr09_read_status(us, &status);
  924. if (result) {
  925. usb_stor_dbg(us, "read_status fails\n");
  926. return result;
  927. }
  928. if ((status & 0x80) == 0) {
  929. info->flags |= SDDR09_WP; /* write protected */
  930. wp_fmt = " WP";
  931. } else {
  932. wp_fmt = "";
  933. }
  934. usb_stor_dbg(us, "status 0x%02X%s%s%s%s\n", status, wp_fmt,
  935. status & 0x40 ? " Ready" : "",
  936. status & LUNBITS ? " Suspended" : "",
  937. status & 0x01 ? " Error" : "");
  938. return 0;
  939. }
  940. #if 0
  941. /*
  942. * Reset Command: 12 bytes.
  943. * byte 0: opcode: EB
  944. */
  945. static int
  946. sddr09_reset(struct us_data *us) {
  947. unsigned char *command = us->iobuf;
  948. memset(command, 0, 12);
  949. command[0] = 0xEB;
  950. command[1] = LUNBITS;
  951. return sddr09_send_scsi_command(us, command, 12);
  952. }
  953. #endif
  954. static struct nand_flash_dev *
  955. sddr09_get_cardinfo(struct us_data *us, unsigned char flags) {
  956. struct nand_flash_dev *cardinfo;
  957. unsigned char deviceID[4];
  958. char blurbtxt[256];
  959. int result;
  960. usb_stor_dbg(us, "Reading capacity...\n");
  961. result = sddr09_read_deviceID(us, deviceID);
  962. if (result) {
  963. usb_stor_dbg(us, "Result of read_deviceID is %d\n", result);
  964. printk(KERN_WARNING "sddr09: could not read card info\n");
  965. return NULL;
  966. }
  967. sprintf(blurbtxt, "sddr09: Found Flash card, ID = %4ph", deviceID);
  968. /* Byte 0 is the manufacturer */
  969. sprintf(blurbtxt + strlen(blurbtxt),
  970. ": Manuf. %s",
  971. nand_flash_manufacturer(deviceID[0]));
  972. /* Byte 1 is the device type */
  973. cardinfo = nand_find_id(deviceID[1]);
  974. if (cardinfo) {
  975. /*
  976. * MB or MiB? It is neither. A 16 MB card has
  977. * 17301504 raw bytes, of which 16384000 are
  978. * usable for user data.
  979. */
  980. sprintf(blurbtxt + strlen(blurbtxt),
  981. ", %d MB", 1<<(cardinfo->chipshift - 20));
  982. } else {
  983. sprintf(blurbtxt + strlen(blurbtxt),
  984. ", type unrecognized");
  985. }
  986. /* Byte 2 is code to signal availability of 128-bit ID */
  987. if (deviceID[2] == 0xa5) {
  988. sprintf(blurbtxt + strlen(blurbtxt),
  989. ", 128-bit ID");
  990. }
  991. /* Byte 3 announces the availability of another read ID command */
  992. if (deviceID[3] == 0xc0) {
  993. sprintf(blurbtxt + strlen(blurbtxt),
  994. ", extra cmd");
  995. }
  996. if (flags & SDDR09_WP)
  997. sprintf(blurbtxt + strlen(blurbtxt),
  998. ", WP");
  999. printk(KERN_WARNING "%s\n", blurbtxt);
  1000. return cardinfo;
  1001. }
  1002. static int
  1003. sddr09_read_map(struct us_data *us) {
  1004. struct sddr09_card_info *info = (struct sddr09_card_info *) us->extra;
  1005. int numblocks, alloc_len, alloc_blocks;
  1006. int i, j, result;
  1007. unsigned char *buffer, *buffer_end, *ptr;
  1008. unsigned int lba, lbact;
  1009. if (!info->capacity)
  1010. return -1;
  1011. /*
  1012. * size of a block is 1 << (blockshift + pageshift) bytes
  1013. * divide into the total capacity to get the number of blocks
  1014. */
  1015. numblocks = info->capacity >> (info->blockshift + info->pageshift);
  1016. /*
  1017. * read 64 bytes for every block (actually 1 << CONTROL_SHIFT)
  1018. * but only use a 64 KB buffer
  1019. * buffer size used must be a multiple of (1 << CONTROL_SHIFT)
  1020. */
  1021. #define SDDR09_READ_MAP_BUFSZ 65536
  1022. alloc_blocks = min(numblocks, SDDR09_READ_MAP_BUFSZ >> CONTROL_SHIFT);
  1023. alloc_len = (alloc_blocks << CONTROL_SHIFT);
  1024. buffer = kmalloc(alloc_len, GFP_NOIO);
  1025. if (!buffer) {
  1026. result = -1;
  1027. goto done;
  1028. }
  1029. buffer_end = buffer + alloc_len;
  1030. #undef SDDR09_READ_MAP_BUFSZ
  1031. kfree(info->lba_to_pba);
  1032. kfree(info->pba_to_lba);
  1033. info->lba_to_pba = kmalloc_array(numblocks, sizeof(int), GFP_NOIO);
  1034. info->pba_to_lba = kmalloc_array(numblocks, sizeof(int), GFP_NOIO);
  1035. if (info->lba_to_pba == NULL || info->pba_to_lba == NULL) {
  1036. printk(KERN_WARNING "sddr09_read_map: out of memory\n");
  1037. result = -1;
  1038. goto done;
  1039. }
  1040. for (i = 0; i < numblocks; i++)
  1041. info->lba_to_pba[i] = info->pba_to_lba[i] = UNDEF;
  1042. /*
  1043. * Define lba-pba translation table
  1044. */
  1045. ptr = buffer_end;
  1046. for (i = 0; i < numblocks; i++) {
  1047. ptr += (1 << CONTROL_SHIFT);
  1048. if (ptr >= buffer_end) {
  1049. unsigned long address;
  1050. address = i << (info->pageshift + info->blockshift);
  1051. result = sddr09_read_control(
  1052. us, address>>1,
  1053. min(alloc_blocks, numblocks - i),
  1054. buffer, 0);
  1055. if (result) {
  1056. result = -1;
  1057. goto done;
  1058. }
  1059. ptr = buffer;
  1060. }
  1061. if (i == 0 || i == 1) {
  1062. info->pba_to_lba[i] = UNUSABLE;
  1063. continue;
  1064. }
  1065. /* special PBAs have control field 0^16 */
  1066. for (j = 0; j < 16; j++)
  1067. if (ptr[j] != 0)
  1068. goto nonz;
  1069. info->pba_to_lba[i] = UNUSABLE;
  1070. printk(KERN_WARNING "sddr09: PBA %d has no logical mapping\n",
  1071. i);
  1072. continue;
  1073. nonz:
  1074. /* unwritten PBAs have control field FF^16 */
  1075. for (j = 0; j < 16; j++)
  1076. if (ptr[j] != 0xff)
  1077. goto nonff;
  1078. continue;
  1079. nonff:
  1080. /* normal PBAs start with six FFs */
  1081. if (j < 6) {
  1082. printk(KERN_WARNING
  1083. "sddr09: PBA %d has no logical mapping: "
  1084. "reserved area = %02X%02X%02X%02X "
  1085. "data status %02X block status %02X\n",
  1086. i, ptr[0], ptr[1], ptr[2], ptr[3],
  1087. ptr[4], ptr[5]);
  1088. info->pba_to_lba[i] = UNUSABLE;
  1089. continue;
  1090. }
  1091. if ((ptr[6] >> 4) != 0x01) {
  1092. printk(KERN_WARNING
  1093. "sddr09: PBA %d has invalid address field "
  1094. "%02X%02X/%02X%02X\n",
  1095. i, ptr[6], ptr[7], ptr[11], ptr[12]);
  1096. info->pba_to_lba[i] = UNUSABLE;
  1097. continue;
  1098. }
  1099. /* check even parity */
  1100. if (parity[ptr[6] ^ ptr[7]]) {
  1101. printk(KERN_WARNING
  1102. "sddr09: Bad parity in LBA for block %d"
  1103. " (%02X %02X)\n", i, ptr[6], ptr[7]);
  1104. info->pba_to_lba[i] = UNUSABLE;
  1105. continue;
  1106. }
  1107. lba = short_pack(ptr[7], ptr[6]);
  1108. lba = (lba & 0x07FF) >> 1;
  1109. /*
  1110. * Every 1024 physical blocks ("zone"), the LBA numbers
  1111. * go back to zero, but are within a higher block of LBA's.
  1112. * Also, there is a maximum of 1000 LBA's per zone.
  1113. * In other words, in PBA 1024-2047 you will find LBA 0-999
  1114. * which are really LBA 1000-1999. This allows for 24 bad
  1115. * or special physical blocks per zone.
  1116. */
  1117. if (lba >= 1000) {
  1118. printk(KERN_WARNING
  1119. "sddr09: Bad low LBA %d for block %d\n",
  1120. lba, i);
  1121. goto possibly_erase;
  1122. }
  1123. lba += 1000*(i/0x400);
  1124. if (info->lba_to_pba[lba] != UNDEF) {
  1125. printk(KERN_WARNING
  1126. "sddr09: LBA %d seen for PBA %d and %d\n",
  1127. lba, info->lba_to_pba[lba], i);
  1128. goto possibly_erase;
  1129. }
  1130. info->pba_to_lba[i] = lba;
  1131. info->lba_to_pba[lba] = i;
  1132. continue;
  1133. possibly_erase:
  1134. if (erase_bad_lba_entries) {
  1135. unsigned long address;
  1136. address = (i << (info->pageshift + info->blockshift));
  1137. sddr09_erase(us, address>>1);
  1138. info->pba_to_lba[i] = UNDEF;
  1139. } else
  1140. info->pba_to_lba[i] = UNUSABLE;
  1141. }
  1142. /*
  1143. * Approximate capacity. This is not entirely correct yet,
  1144. * since a zone with less than 1000 usable pages leads to
  1145. * missing LBAs. Especially if it is the last zone, some
  1146. * LBAs can be past capacity.
  1147. */
  1148. lbact = 0;
  1149. for (i = 0; i < numblocks; i += 1024) {
  1150. int ct = 0;
  1151. for (j = 0; j < 1024 && i+j < numblocks; j++) {
  1152. if (info->pba_to_lba[i+j] != UNUSABLE) {
  1153. if (ct >= 1000)
  1154. info->pba_to_lba[i+j] = SPARE;
  1155. else
  1156. ct++;
  1157. }
  1158. }
  1159. lbact += ct;
  1160. }
  1161. info->lbact = lbact;
  1162. usb_stor_dbg(us, "Found %d LBA's\n", lbact);
  1163. result = 0;
  1164. done:
  1165. if (result != 0) {
  1166. kfree(info->lba_to_pba);
  1167. kfree(info->pba_to_lba);
  1168. info->lba_to_pba = NULL;
  1169. info->pba_to_lba = NULL;
  1170. }
  1171. kfree(buffer);
  1172. return result;
  1173. }
  1174. static void
  1175. sddr09_card_info_destructor(void *extra) {
  1176. struct sddr09_card_info *info = (struct sddr09_card_info *)extra;
  1177. if (!info)
  1178. return;
  1179. kfree(info->lba_to_pba);
  1180. kfree(info->pba_to_lba);
  1181. }
  1182. static int
  1183. sddr09_common_init(struct us_data *us) {
  1184. int result;
  1185. /* set the configuration -- STALL is an acceptable response here */
  1186. if (us->pusb_dev->actconfig->desc.bConfigurationValue != 1) {
  1187. usb_stor_dbg(us, "active config #%d != 1 ??\n",
  1188. us->pusb_dev->actconfig->desc.bConfigurationValue);
  1189. return -EINVAL;
  1190. }
  1191. result = usb_reset_configuration(us->pusb_dev);
  1192. usb_stor_dbg(us, "Result of usb_reset_configuration is %d\n", result);
  1193. if (result == -EPIPE) {
  1194. usb_stor_dbg(us, "-- stall on control interface\n");
  1195. } else if (result != 0) {
  1196. /* it's not a stall, but another error -- time to bail */
  1197. usb_stor_dbg(us, "-- Unknown error. Rejecting device\n");
  1198. return -EINVAL;
  1199. }
  1200. us->extra = kzalloc(sizeof(struct sddr09_card_info), GFP_NOIO);
  1201. if (!us->extra)
  1202. return -ENOMEM;
  1203. us->extra_destructor = sddr09_card_info_destructor;
  1204. nand_init_ecc();
  1205. return 0;
  1206. }
  1207. /*
  1208. * This is needed at a very early stage. If this is not listed in the
  1209. * unusual devices list but called from here then LUN 0 of the combo reader
  1210. * is not recognized. But I do not know what precisely these calls do.
  1211. */
  1212. static int
  1213. usb_stor_sddr09_dpcm_init(struct us_data *us) {
  1214. int result;
  1215. unsigned char *data = us->iobuf;
  1216. result = sddr09_common_init(us);
  1217. if (result)
  1218. return result;
  1219. result = sddr09_send_command(us, 0x01, USB_DIR_IN, data, 2);
  1220. if (result) {
  1221. usb_stor_dbg(us, "send_command fails\n");
  1222. return result;
  1223. }
  1224. usb_stor_dbg(us, "%02X %02X\n", data[0], data[1]);
  1225. // get 07 02
  1226. result = sddr09_send_command(us, 0x08, USB_DIR_IN, data, 2);
  1227. if (result) {
  1228. usb_stor_dbg(us, "2nd send_command fails\n");
  1229. return result;
  1230. }
  1231. usb_stor_dbg(us, "%02X %02X\n", data[0], data[1]);
  1232. // get 07 00
  1233. result = sddr09_request_sense(us, data, 18);
  1234. if (result == 0 && data[2] != 0) {
  1235. int j;
  1236. for (j=0; j<18; j++)
  1237. printk(" %02X", data[j]);
  1238. printk("\n");
  1239. // get 70 00 00 00 00 00 00 * 00 00 00 00 00 00
  1240. // 70: current command
  1241. // sense key 0, sense code 0, extd sense code 0
  1242. // additional transfer length * = sizeof(data) - 7
  1243. // Or: 70 00 06 00 00 00 00 0b 00 00 00 00 28 00 00 00 00 00
  1244. // sense key 06, sense code 28: unit attention,
  1245. // not ready to ready transition
  1246. }
  1247. // test unit ready
  1248. return 0; /* not result */
  1249. }
  1250. /*
  1251. * Transport for the Microtech DPCM-USB
  1252. */
  1253. static int dpcm_transport(struct scsi_cmnd *srb, struct us_data *us)
  1254. {
  1255. int ret;
  1256. usb_stor_dbg(us, "LUN=%d\n", (u8)srb->device->lun);
  1257. switch (srb->device->lun) {
  1258. case 0:
  1259. /*
  1260. * LUN 0 corresponds to the CompactFlash card reader.
  1261. */
  1262. ret = usb_stor_CB_transport(srb, us);
  1263. break;
  1264. case 1:
  1265. /*
  1266. * LUN 1 corresponds to the SmartMedia card reader.
  1267. */
  1268. /*
  1269. * Set the LUN to 0 (just in case).
  1270. */
  1271. srb->device->lun = 0;
  1272. ret = sddr09_transport(srb, us);
  1273. srb->device->lun = 1;
  1274. break;
  1275. default:
  1276. usb_stor_dbg(us, "Invalid LUN %d\n", (u8)srb->device->lun);
  1277. ret = USB_STOR_TRANSPORT_ERROR;
  1278. break;
  1279. }
  1280. return ret;
  1281. }
  1282. /*
  1283. * Transport for the Sandisk SDDR-09
  1284. */
  1285. static int sddr09_transport(struct scsi_cmnd *srb, struct us_data *us)
  1286. {
  1287. static unsigned char sensekey = 0, sensecode = 0;
  1288. static unsigned char havefakesense = 0;
  1289. int result, i;
  1290. unsigned char *ptr = us->iobuf;
  1291. unsigned long capacity;
  1292. unsigned int page, pages;
  1293. struct sddr09_card_info *info;
  1294. static unsigned char inquiry_response[8] = {
  1295. 0x00, 0x80, 0x00, 0x02, 0x1F, 0x00, 0x00, 0x00
  1296. };
  1297. /* note: no block descriptor support */
  1298. static unsigned char mode_page_01[19] = {
  1299. 0x00, 0x0F, 0x00, 0x0, 0x0, 0x0, 0x00,
  1300. 0x01, 0x0A,
  1301. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1302. };
  1303. info = (struct sddr09_card_info *)us->extra;
  1304. if (srb->cmnd[0] == REQUEST_SENSE && havefakesense) {
  1305. /* for a faked command, we have to follow with a faked sense */
  1306. memset(ptr, 0, 18);
  1307. ptr[0] = 0x70;
  1308. ptr[2] = sensekey;
  1309. ptr[7] = 11;
  1310. ptr[12] = sensecode;
  1311. usb_stor_set_xfer_buf(ptr, 18, srb);
  1312. sensekey = sensecode = havefakesense = 0;
  1313. return USB_STOR_TRANSPORT_GOOD;
  1314. }
  1315. havefakesense = 1;
  1316. /*
  1317. * Dummy up a response for INQUIRY since SDDR09 doesn't
  1318. * respond to INQUIRY commands
  1319. */
  1320. if (srb->cmnd[0] == INQUIRY) {
  1321. memcpy(ptr, inquiry_response, 8);
  1322. fill_inquiry_response(us, ptr, 36);
  1323. return USB_STOR_TRANSPORT_GOOD;
  1324. }
  1325. if (srb->cmnd[0] == READ_CAPACITY) {
  1326. struct nand_flash_dev *cardinfo;
  1327. sddr09_get_wp(us, info); /* read WP bit */
  1328. cardinfo = sddr09_get_cardinfo(us, info->flags);
  1329. if (!cardinfo) {
  1330. /* probably no media */
  1331. init_error:
  1332. sensekey = 0x02; /* not ready */
  1333. sensecode = 0x3a; /* medium not present */
  1334. return USB_STOR_TRANSPORT_FAILED;
  1335. }
  1336. info->capacity = (1 << cardinfo->chipshift);
  1337. info->pageshift = cardinfo->pageshift;
  1338. info->pagesize = (1 << info->pageshift);
  1339. info->blockshift = cardinfo->blockshift;
  1340. info->blocksize = (1 << info->blockshift);
  1341. info->blockmask = info->blocksize - 1;
  1342. // map initialization, must follow get_cardinfo()
  1343. if (sddr09_read_map(us)) {
  1344. /* probably out of memory */
  1345. goto init_error;
  1346. }
  1347. // Report capacity
  1348. capacity = (info->lbact << info->blockshift) - 1;
  1349. ((__be32 *) ptr)[0] = cpu_to_be32(capacity);
  1350. // Report page size
  1351. ((__be32 *) ptr)[1] = cpu_to_be32(info->pagesize);
  1352. usb_stor_set_xfer_buf(ptr, 8, srb);
  1353. return USB_STOR_TRANSPORT_GOOD;
  1354. }
  1355. if (srb->cmnd[0] == MODE_SENSE_10) {
  1356. int modepage = (srb->cmnd[2] & 0x3F);
  1357. /*
  1358. * They ask for the Read/Write error recovery page,
  1359. * or for all pages.
  1360. */
  1361. /* %% We should check DBD %% */
  1362. if (modepage == 0x01 || modepage == 0x3F) {
  1363. usb_stor_dbg(us, "Dummy up request for mode page 0x%x\n",
  1364. modepage);
  1365. memcpy(ptr, mode_page_01, sizeof(mode_page_01));
  1366. ((__be16*)ptr)[0] = cpu_to_be16(sizeof(mode_page_01) - 2);
  1367. ptr[3] = (info->flags & SDDR09_WP) ? 0x80 : 0;
  1368. usb_stor_set_xfer_buf(ptr, sizeof(mode_page_01), srb);
  1369. return USB_STOR_TRANSPORT_GOOD;
  1370. }
  1371. sensekey = 0x05; /* illegal request */
  1372. sensecode = 0x24; /* invalid field in CDB */
  1373. return USB_STOR_TRANSPORT_FAILED;
  1374. }
  1375. if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL)
  1376. return USB_STOR_TRANSPORT_GOOD;
  1377. havefakesense = 0;
  1378. if (srb->cmnd[0] == READ_10) {
  1379. page = short_pack(srb->cmnd[3], srb->cmnd[2]);
  1380. page <<= 16;
  1381. page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
  1382. pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
  1383. usb_stor_dbg(us, "READ_10: read page %d pagect %d\n",
  1384. page, pages);
  1385. result = sddr09_read_data(us, page, pages);
  1386. return (result == 0 ? USB_STOR_TRANSPORT_GOOD :
  1387. USB_STOR_TRANSPORT_ERROR);
  1388. }
  1389. if (srb->cmnd[0] == WRITE_10) {
  1390. page = short_pack(srb->cmnd[3], srb->cmnd[2]);
  1391. page <<= 16;
  1392. page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
  1393. pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
  1394. usb_stor_dbg(us, "WRITE_10: write page %d pagect %d\n",
  1395. page, pages);
  1396. result = sddr09_write_data(us, page, pages);
  1397. return (result == 0 ? USB_STOR_TRANSPORT_GOOD :
  1398. USB_STOR_TRANSPORT_ERROR);
  1399. }
  1400. /*
  1401. * catch-all for all other commands, except
  1402. * pass TEST_UNIT_READY and REQUEST_SENSE through
  1403. */
  1404. if (srb->cmnd[0] != TEST_UNIT_READY &&
  1405. srb->cmnd[0] != REQUEST_SENSE) {
  1406. sensekey = 0x05; /* illegal request */
  1407. sensecode = 0x20; /* invalid command */
  1408. havefakesense = 1;
  1409. return USB_STOR_TRANSPORT_FAILED;
  1410. }
  1411. for (; srb->cmd_len<12; srb->cmd_len++)
  1412. srb->cmnd[srb->cmd_len] = 0;
  1413. srb->cmnd[1] = LUNBITS;
  1414. ptr[0] = 0;
  1415. for (i=0; i<12; i++)
  1416. sprintf(ptr+strlen(ptr), "%02X ", srb->cmnd[i]);
  1417. usb_stor_dbg(us, "Send control for command %s\n", ptr);
  1418. result = sddr09_send_scsi_command(us, srb->cmnd, 12);
  1419. if (result) {
  1420. usb_stor_dbg(us, "sddr09_send_scsi_command returns %d\n",
  1421. result);
  1422. return USB_STOR_TRANSPORT_ERROR;
  1423. }
  1424. if (scsi_bufflen(srb) == 0)
  1425. return USB_STOR_TRANSPORT_GOOD;
  1426. if (srb->sc_data_direction == DMA_TO_DEVICE ||
  1427. srb->sc_data_direction == DMA_FROM_DEVICE) {
  1428. unsigned int pipe = (srb->sc_data_direction == DMA_TO_DEVICE)
  1429. ? us->send_bulk_pipe : us->recv_bulk_pipe;
  1430. usb_stor_dbg(us, "%s %d bytes\n",
  1431. (srb->sc_data_direction == DMA_TO_DEVICE) ?
  1432. "sending" : "receiving",
  1433. scsi_bufflen(srb));
  1434. result = usb_stor_bulk_srb(us, pipe, srb);
  1435. return (result == USB_STOR_XFER_GOOD ?
  1436. USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_ERROR);
  1437. }
  1438. return USB_STOR_TRANSPORT_GOOD;
  1439. }
  1440. /*
  1441. * Initialization routine for the sddr09 subdriver
  1442. */
  1443. static int
  1444. usb_stor_sddr09_init(struct us_data *us) {
  1445. return sddr09_common_init(us);
  1446. }
  1447. static struct scsi_host_template sddr09_host_template;
  1448. static int sddr09_probe(struct usb_interface *intf,
  1449. const struct usb_device_id *id)
  1450. {
  1451. struct us_data *us;
  1452. int result;
  1453. result = usb_stor_probe1(&us, intf, id,
  1454. (id - sddr09_usb_ids) + sddr09_unusual_dev_list,
  1455. &sddr09_host_template);
  1456. if (result)
  1457. return result;
  1458. if (us->protocol == USB_PR_DPCM_USB) {
  1459. us->transport_name = "Control/Bulk-EUSB/SDDR09";
  1460. us->transport = dpcm_transport;
  1461. us->transport_reset = usb_stor_CB_reset;
  1462. us->max_lun = 1;
  1463. } else {
  1464. us->transport_name = "EUSB/SDDR09";
  1465. us->transport = sddr09_transport;
  1466. us->transport_reset = usb_stor_CB_reset;
  1467. us->max_lun = 0;
  1468. }
  1469. result = usb_stor_probe2(us);
  1470. return result;
  1471. }
  1472. static struct usb_driver sddr09_driver = {
  1473. .name = DRV_NAME,
  1474. .probe = sddr09_probe,
  1475. .disconnect = usb_stor_disconnect,
  1476. .suspend = usb_stor_suspend,
  1477. .resume = usb_stor_resume,
  1478. .reset_resume = usb_stor_reset_resume,
  1479. .pre_reset = usb_stor_pre_reset,
  1480. .post_reset = usb_stor_post_reset,
  1481. .id_table = sddr09_usb_ids,
  1482. .soft_unbind = 1,
  1483. .no_dynamic_id = 1,
  1484. };
  1485. module_usb_stor_driver(sddr09_driver, sddr09_host_template, DRV_NAME);