sddr09.c 45 KB

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