sddr09.c 41 KB

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