sddr09.c 45 KB

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