sddr09.c 45 KB

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