sddr09.c 44 KB

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