sddr09.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802
  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)
  658. return -ENOMEM;
  659. // This could be made much more efficient by checking for
  660. // contiguous LBA's. Another exercise left to the student.
  661. result = 0;
  662. offset = 0;
  663. sg = NULL;
  664. while (sectors > 0) {
  665. /* Find number of pages we can read in this block */
  666. pages = min(sectors, info->blocksize - page);
  667. len = pages << info->pageshift;
  668. /* Not overflowing capacity? */
  669. if (lba >= maxlba) {
  670. usb_stor_dbg(us, "Error: Requested lba %u exceeds maximum %u\n",
  671. lba, maxlba);
  672. result = -EIO;
  673. break;
  674. }
  675. /* Find where this lba lives on disk */
  676. pba = info->lba_to_pba[lba];
  677. if (pba == UNDEF) { /* this lba was never written */
  678. usb_stor_dbg(us, "Read %d zero pages (LBA %d) page %d\n",
  679. pages, lba, page);
  680. /*
  681. * This is not really an error. It just means
  682. * that the block has never been written.
  683. * Instead of returning an error
  684. * it is better to return all zero data.
  685. */
  686. memset(buffer, 0, len);
  687. } else {
  688. usb_stor_dbg(us, "Read %d pages, from PBA %d (LBA %d) page %d\n",
  689. pages, pba, lba, page);
  690. address = ((pba << info->blockshift) + page) <<
  691. info->pageshift;
  692. result = sddr09_read20(us, address>>1,
  693. pages, info->pageshift, buffer, 0);
  694. if (result)
  695. break;
  696. }
  697. // Store the data in the transfer buffer
  698. usb_stor_access_xfer_buf(buffer, len, us->srb,
  699. &sg, &offset, TO_XFER_BUF);
  700. page = 0;
  701. lba++;
  702. sectors -= pages;
  703. }
  704. kfree(buffer);
  705. return result;
  706. }
  707. static unsigned int
  708. sddr09_find_unused_pba(struct sddr09_card_info *info, unsigned int lba) {
  709. static unsigned int lastpba = 1;
  710. int zonestart, end, i;
  711. zonestart = (lba/1000) << 10;
  712. end = info->capacity >> (info->blockshift + info->pageshift);
  713. end -= zonestart;
  714. if (end > 1024)
  715. end = 1024;
  716. for (i = lastpba+1; i < end; i++) {
  717. if (info->pba_to_lba[zonestart+i] == UNDEF) {
  718. lastpba = i;
  719. return zonestart+i;
  720. }
  721. }
  722. for (i = 0; i <= lastpba; i++) {
  723. if (info->pba_to_lba[zonestart+i] == UNDEF) {
  724. lastpba = i;
  725. return zonestart+i;
  726. }
  727. }
  728. return 0;
  729. }
  730. static int
  731. sddr09_write_lba(struct us_data *us, unsigned int lba,
  732. unsigned int page, unsigned int pages,
  733. unsigned char *ptr, unsigned char *blockbuffer) {
  734. struct sddr09_card_info *info = (struct sddr09_card_info *) us->extra;
  735. unsigned long address;
  736. unsigned int pba, lbap;
  737. unsigned int pagelen;
  738. unsigned char *bptr, *cptr, *xptr;
  739. unsigned char ecc[3];
  740. int i, result;
  741. lbap = ((lba % 1000) << 1) | 0x1000;
  742. if (parity[MSB_of(lbap) ^ LSB_of(lbap)])
  743. lbap ^= 1;
  744. pba = info->lba_to_pba[lba];
  745. if (pba == UNDEF) {
  746. pba = sddr09_find_unused_pba(info, lba);
  747. if (!pba) {
  748. printk(KERN_WARNING
  749. "sddr09_write_lba: Out of unused blocks\n");
  750. return -ENOSPC;
  751. }
  752. info->pba_to_lba[pba] = lba;
  753. info->lba_to_pba[lba] = pba;
  754. }
  755. if (pba == 1) {
  756. /*
  757. * Maybe it is impossible to write to PBA 1.
  758. * Fake success, but don't do anything.
  759. */
  760. printk(KERN_WARNING "sddr09: avoid writing to pba 1\n");
  761. return 0;
  762. }
  763. pagelen = (1 << info->pageshift) + (1 << CONTROL_SHIFT);
  764. /* read old contents */
  765. address = (pba << (info->pageshift + info->blockshift));
  766. result = sddr09_read22(us, address>>1, info->blocksize,
  767. info->pageshift, blockbuffer, 0);
  768. if (result)
  769. return result;
  770. /* check old contents and fill lba */
  771. for (i = 0; i < info->blocksize; i++) {
  772. bptr = blockbuffer + i*pagelen;
  773. cptr = bptr + info->pagesize;
  774. nand_compute_ecc(bptr, ecc);
  775. if (!nand_compare_ecc(cptr+13, ecc)) {
  776. usb_stor_dbg(us, "Warning: bad ecc in page %d- of pba %d\n",
  777. i, pba);
  778. nand_store_ecc(cptr+13, ecc);
  779. }
  780. nand_compute_ecc(bptr+(info->pagesize / 2), ecc);
  781. if (!nand_compare_ecc(cptr+8, ecc)) {
  782. usb_stor_dbg(us, "Warning: bad ecc in page %d+ of pba %d\n",
  783. i, pba);
  784. nand_store_ecc(cptr+8, ecc);
  785. }
  786. cptr[6] = cptr[11] = MSB_of(lbap);
  787. cptr[7] = cptr[12] = LSB_of(lbap);
  788. }
  789. /* copy in new stuff and compute ECC */
  790. xptr = ptr;
  791. for (i = page; i < page+pages; i++) {
  792. bptr = blockbuffer + i*pagelen;
  793. cptr = bptr + info->pagesize;
  794. memcpy(bptr, xptr, info->pagesize);
  795. xptr += info->pagesize;
  796. nand_compute_ecc(bptr, ecc);
  797. nand_store_ecc(cptr+13, ecc);
  798. nand_compute_ecc(bptr+(info->pagesize / 2), ecc);
  799. nand_store_ecc(cptr+8, ecc);
  800. }
  801. usb_stor_dbg(us, "Rewrite PBA %d (LBA %d)\n", pba, lba);
  802. result = sddr09_write_inplace(us, address>>1, info->blocksize,
  803. info->pageshift, blockbuffer, 0);
  804. usb_stor_dbg(us, "sddr09_write_inplace returns %d\n", result);
  805. #if 0
  806. {
  807. unsigned char status = 0;
  808. int result2 = sddr09_read_status(us, &status);
  809. if (result2)
  810. usb_stor_dbg(us, "cannot read status\n");
  811. else if (status != 0xc0)
  812. usb_stor_dbg(us, "status after write: 0x%x\n", status);
  813. }
  814. #endif
  815. #if 0
  816. {
  817. int result2 = sddr09_test_unit_ready(us);
  818. }
  819. #endif
  820. return result;
  821. }
  822. static int
  823. sddr09_write_data(struct us_data *us,
  824. unsigned long address,
  825. unsigned int sectors) {
  826. struct sddr09_card_info *info = (struct sddr09_card_info *) us->extra;
  827. unsigned int lba, maxlba, page, pages;
  828. unsigned int pagelen, blocklen;
  829. unsigned char *blockbuffer;
  830. unsigned char *buffer;
  831. unsigned int len, offset;
  832. struct scatterlist *sg;
  833. int result;
  834. /* Figure out the initial LBA and page */
  835. lba = address >> info->blockshift;
  836. page = (address & info->blockmask);
  837. maxlba = info->capacity >> (info->pageshift + info->blockshift);
  838. if (lba >= maxlba)
  839. return -EIO;
  840. /*
  841. * blockbuffer is used for reading in the old data, overwriting
  842. * with the new data, and performing ECC calculations
  843. */
  844. /*
  845. * TODO: instead of doing kmalloc/kfree for each write,
  846. * add a bufferpointer to the info structure
  847. */
  848. pagelen = (1 << info->pageshift) + (1 << CONTROL_SHIFT);
  849. blocklen = (pagelen << info->blockshift);
  850. blockbuffer = kmalloc(blocklen, GFP_NOIO);
  851. if (!blockbuffer)
  852. return -ENOMEM;
  853. /*
  854. * Since we don't write the user data directly to the device,
  855. * we have to create a bounce buffer and move the data a piece
  856. * at a time between the bounce buffer and the actual transfer buffer.
  857. */
  858. len = min(sectors, (unsigned int) info->blocksize) * info->pagesize;
  859. buffer = kmalloc(len, GFP_NOIO);
  860. if (!buffer) {
  861. kfree(blockbuffer);
  862. return -ENOMEM;
  863. }
  864. result = 0;
  865. offset = 0;
  866. sg = NULL;
  867. while (sectors > 0) {
  868. /* Write as many sectors as possible in this block */
  869. pages = min(sectors, info->blocksize - page);
  870. len = (pages << info->pageshift);
  871. /* Not overflowing capacity? */
  872. if (lba >= maxlba) {
  873. usb_stor_dbg(us, "Error: Requested lba %u exceeds maximum %u\n",
  874. lba, maxlba);
  875. result = -EIO;
  876. break;
  877. }
  878. /* Get the data from the transfer buffer */
  879. usb_stor_access_xfer_buf(buffer, len, us->srb,
  880. &sg, &offset, FROM_XFER_BUF);
  881. result = sddr09_write_lba(us, lba, page, pages,
  882. buffer, blockbuffer);
  883. if (result)
  884. break;
  885. page = 0;
  886. lba++;
  887. sectors -= pages;
  888. }
  889. kfree(buffer);
  890. kfree(blockbuffer);
  891. return result;
  892. }
  893. static int
  894. sddr09_read_control(struct us_data *us,
  895. unsigned long address,
  896. unsigned int blocks,
  897. unsigned char *content,
  898. int use_sg) {
  899. usb_stor_dbg(us, "Read control address %lu, blocks %d\n",
  900. address, blocks);
  901. return sddr09_read21(us, address, blocks,
  902. CONTROL_SHIFT, content, use_sg);
  903. }
  904. /*
  905. * Read Device ID Command: 12 bytes.
  906. * byte 0: opcode: ED
  907. *
  908. * Returns 2 bytes: Manufacturer ID and Device ID.
  909. * On more recent cards 3 bytes: the third byte is an option code A5
  910. * signifying that the secret command to read an 128-bit ID is available.
  911. * On still more recent cards 4 bytes: the fourth byte C0 means that
  912. * a second read ID cmd is available.
  913. */
  914. static int
  915. sddr09_read_deviceID(struct us_data *us, unsigned char *deviceID) {
  916. unsigned char *command = us->iobuf;
  917. unsigned char *content = us->iobuf;
  918. int result, i;
  919. memset(command, 0, 12);
  920. command[0] = 0xED;
  921. command[1] = LUNBITS;
  922. result = sddr09_send_scsi_command(us, command, 12);
  923. if (result)
  924. return result;
  925. result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  926. content, 64, NULL);
  927. for (i = 0; i < 4; i++)
  928. deviceID[i] = content[i];
  929. return (result == USB_STOR_XFER_GOOD ? 0 : -EIO);
  930. }
  931. static int
  932. sddr09_get_wp(struct us_data *us, struct sddr09_card_info *info) {
  933. int result;
  934. unsigned char status;
  935. const char *wp_fmt;
  936. result = sddr09_read_status(us, &status);
  937. if (result) {
  938. usb_stor_dbg(us, "read_status fails\n");
  939. return result;
  940. }
  941. if ((status & 0x80) == 0) {
  942. info->flags |= SDDR09_WP; /* write protected */
  943. wp_fmt = " WP";
  944. } else {
  945. wp_fmt = "";
  946. }
  947. usb_stor_dbg(us, "status 0x%02X%s%s%s%s\n", status, wp_fmt,
  948. status & 0x40 ? " Ready" : "",
  949. status & LUNBITS ? " Suspended" : "",
  950. status & 0x01 ? " Error" : "");
  951. return 0;
  952. }
  953. #if 0
  954. /*
  955. * Reset Command: 12 bytes.
  956. * byte 0: opcode: EB
  957. */
  958. static int
  959. sddr09_reset(struct us_data *us) {
  960. unsigned char *command = us->iobuf;
  961. memset(command, 0, 12);
  962. command[0] = 0xEB;
  963. command[1] = LUNBITS;
  964. return sddr09_send_scsi_command(us, command, 12);
  965. }
  966. #endif
  967. static struct nand_flash_dev *
  968. sddr09_get_cardinfo(struct us_data *us, unsigned char flags) {
  969. struct nand_flash_dev *cardinfo;
  970. unsigned char deviceID[4];
  971. char blurbtxt[256];
  972. int result;
  973. usb_stor_dbg(us, "Reading capacity...\n");
  974. result = sddr09_read_deviceID(us, deviceID);
  975. if (result) {
  976. usb_stor_dbg(us, "Result of read_deviceID is %d\n", result);
  977. printk(KERN_WARNING "sddr09: could not read card info\n");
  978. return NULL;
  979. }
  980. sprintf(blurbtxt, "sddr09: Found Flash card, ID = %4ph", deviceID);
  981. /* Byte 0 is the manufacturer */
  982. sprintf(blurbtxt + strlen(blurbtxt),
  983. ": Manuf. %s",
  984. nand_flash_manufacturer(deviceID[0]));
  985. /* Byte 1 is the device type */
  986. cardinfo = nand_find_id(deviceID[1]);
  987. if (cardinfo) {
  988. /*
  989. * MB or MiB? It is neither. A 16 MB card has
  990. * 17301504 raw bytes, of which 16384000 are
  991. * usable for user data.
  992. */
  993. sprintf(blurbtxt + strlen(blurbtxt),
  994. ", %d MB", 1<<(cardinfo->chipshift - 20));
  995. } else {
  996. sprintf(blurbtxt + strlen(blurbtxt),
  997. ", type unrecognized");
  998. }
  999. /* Byte 2 is code to signal availability of 128-bit ID */
  1000. if (deviceID[2] == 0xa5) {
  1001. sprintf(blurbtxt + strlen(blurbtxt),
  1002. ", 128-bit ID");
  1003. }
  1004. /* Byte 3 announces the availability of another read ID command */
  1005. if (deviceID[3] == 0xc0) {
  1006. sprintf(blurbtxt + strlen(blurbtxt),
  1007. ", extra cmd");
  1008. }
  1009. if (flags & SDDR09_WP)
  1010. sprintf(blurbtxt + strlen(blurbtxt),
  1011. ", WP");
  1012. printk(KERN_WARNING "%s\n", blurbtxt);
  1013. return cardinfo;
  1014. }
  1015. static int
  1016. sddr09_read_map(struct us_data *us) {
  1017. struct sddr09_card_info *info = (struct sddr09_card_info *) us->extra;
  1018. int numblocks, alloc_len, alloc_blocks;
  1019. int i, j, result;
  1020. unsigned char *buffer, *buffer_end, *ptr;
  1021. unsigned int lba, lbact;
  1022. if (!info->capacity)
  1023. return -1;
  1024. /*
  1025. * size of a block is 1 << (blockshift + pageshift) bytes
  1026. * divide into the total capacity to get the number of blocks
  1027. */
  1028. numblocks = info->capacity >> (info->blockshift + info->pageshift);
  1029. /*
  1030. * read 64 bytes for every block (actually 1 << CONTROL_SHIFT)
  1031. * but only use a 64 KB buffer
  1032. * buffer size used must be a multiple of (1 << CONTROL_SHIFT)
  1033. */
  1034. #define SDDR09_READ_MAP_BUFSZ 65536
  1035. alloc_blocks = min(numblocks, SDDR09_READ_MAP_BUFSZ >> CONTROL_SHIFT);
  1036. alloc_len = (alloc_blocks << CONTROL_SHIFT);
  1037. buffer = kmalloc(alloc_len, GFP_NOIO);
  1038. if (!buffer) {
  1039. result = -1;
  1040. goto done;
  1041. }
  1042. buffer_end = buffer + alloc_len;
  1043. #undef SDDR09_READ_MAP_BUFSZ
  1044. kfree(info->lba_to_pba);
  1045. kfree(info->pba_to_lba);
  1046. info->lba_to_pba = kmalloc(numblocks*sizeof(int), GFP_NOIO);
  1047. info->pba_to_lba = kmalloc(numblocks*sizeof(int), GFP_NOIO);
  1048. if (info->lba_to_pba == NULL || info->pba_to_lba == NULL) {
  1049. printk(KERN_WARNING "sddr09_read_map: out of memory\n");
  1050. result = -1;
  1051. goto done;
  1052. }
  1053. for (i = 0; i < numblocks; i++)
  1054. info->lba_to_pba[i] = info->pba_to_lba[i] = UNDEF;
  1055. /*
  1056. * Define lba-pba translation table
  1057. */
  1058. ptr = buffer_end;
  1059. for (i = 0; i < numblocks; i++) {
  1060. ptr += (1 << CONTROL_SHIFT);
  1061. if (ptr >= buffer_end) {
  1062. unsigned long address;
  1063. address = i << (info->pageshift + info->blockshift);
  1064. result = sddr09_read_control(
  1065. us, address>>1,
  1066. min(alloc_blocks, numblocks - i),
  1067. buffer, 0);
  1068. if (result) {
  1069. result = -1;
  1070. goto done;
  1071. }
  1072. ptr = buffer;
  1073. }
  1074. if (i == 0 || i == 1) {
  1075. info->pba_to_lba[i] = UNUSABLE;
  1076. continue;
  1077. }
  1078. /* special PBAs have control field 0^16 */
  1079. for (j = 0; j < 16; j++)
  1080. if (ptr[j] != 0)
  1081. goto nonz;
  1082. info->pba_to_lba[i] = UNUSABLE;
  1083. printk(KERN_WARNING "sddr09: PBA %d has no logical mapping\n",
  1084. i);
  1085. continue;
  1086. nonz:
  1087. /* unwritten PBAs have control field FF^16 */
  1088. for (j = 0; j < 16; j++)
  1089. if (ptr[j] != 0xff)
  1090. goto nonff;
  1091. continue;
  1092. nonff:
  1093. /* normal PBAs start with six FFs */
  1094. if (j < 6) {
  1095. printk(KERN_WARNING
  1096. "sddr09: PBA %d has no logical mapping: "
  1097. "reserved area = %02X%02X%02X%02X "
  1098. "data status %02X block status %02X\n",
  1099. i, ptr[0], ptr[1], ptr[2], ptr[3],
  1100. ptr[4], ptr[5]);
  1101. info->pba_to_lba[i] = UNUSABLE;
  1102. continue;
  1103. }
  1104. if ((ptr[6] >> 4) != 0x01) {
  1105. printk(KERN_WARNING
  1106. "sddr09: PBA %d has invalid address field "
  1107. "%02X%02X/%02X%02X\n",
  1108. i, ptr[6], ptr[7], ptr[11], ptr[12]);
  1109. info->pba_to_lba[i] = UNUSABLE;
  1110. continue;
  1111. }
  1112. /* check even parity */
  1113. if (parity[ptr[6] ^ ptr[7]]) {
  1114. printk(KERN_WARNING
  1115. "sddr09: Bad parity in LBA for block %d"
  1116. " (%02X %02X)\n", i, ptr[6], ptr[7]);
  1117. info->pba_to_lba[i] = UNUSABLE;
  1118. continue;
  1119. }
  1120. lba = short_pack(ptr[7], ptr[6]);
  1121. lba = (lba & 0x07FF) >> 1;
  1122. /*
  1123. * Every 1024 physical blocks ("zone"), the LBA numbers
  1124. * go back to zero, but are within a higher block of LBA's.
  1125. * Also, there is a maximum of 1000 LBA's per zone.
  1126. * In other words, in PBA 1024-2047 you will find LBA 0-999
  1127. * which are really LBA 1000-1999. This allows for 24 bad
  1128. * or special physical blocks per zone.
  1129. */
  1130. if (lba >= 1000) {
  1131. printk(KERN_WARNING
  1132. "sddr09: Bad low LBA %d for block %d\n",
  1133. lba, i);
  1134. goto possibly_erase;
  1135. }
  1136. lba += 1000*(i/0x400);
  1137. if (info->lba_to_pba[lba] != UNDEF) {
  1138. printk(KERN_WARNING
  1139. "sddr09: LBA %d seen for PBA %d and %d\n",
  1140. lba, info->lba_to_pba[lba], i);
  1141. goto possibly_erase;
  1142. }
  1143. info->pba_to_lba[i] = lba;
  1144. info->lba_to_pba[lba] = i;
  1145. continue;
  1146. possibly_erase:
  1147. if (erase_bad_lba_entries) {
  1148. unsigned long address;
  1149. address = (i << (info->pageshift + info->blockshift));
  1150. sddr09_erase(us, address>>1);
  1151. info->pba_to_lba[i] = UNDEF;
  1152. } else
  1153. info->pba_to_lba[i] = UNUSABLE;
  1154. }
  1155. /*
  1156. * Approximate capacity. This is not entirely correct yet,
  1157. * since a zone with less than 1000 usable pages leads to
  1158. * missing LBAs. Especially if it is the last zone, some
  1159. * LBAs can be past capacity.
  1160. */
  1161. lbact = 0;
  1162. for (i = 0; i < numblocks; i += 1024) {
  1163. int ct = 0;
  1164. for (j = 0; j < 1024 && i+j < numblocks; j++) {
  1165. if (info->pba_to_lba[i+j] != UNUSABLE) {
  1166. if (ct >= 1000)
  1167. info->pba_to_lba[i+j] = SPARE;
  1168. else
  1169. ct++;
  1170. }
  1171. }
  1172. lbact += ct;
  1173. }
  1174. info->lbact = lbact;
  1175. usb_stor_dbg(us, "Found %d LBA's\n", lbact);
  1176. result = 0;
  1177. done:
  1178. if (result != 0) {
  1179. kfree(info->lba_to_pba);
  1180. kfree(info->pba_to_lba);
  1181. info->lba_to_pba = NULL;
  1182. info->pba_to_lba = NULL;
  1183. }
  1184. kfree(buffer);
  1185. return result;
  1186. }
  1187. static void
  1188. sddr09_card_info_destructor(void *extra) {
  1189. struct sddr09_card_info *info = (struct sddr09_card_info *)extra;
  1190. if (!info)
  1191. return;
  1192. kfree(info->lba_to_pba);
  1193. kfree(info->pba_to_lba);
  1194. }
  1195. static int
  1196. sddr09_common_init(struct us_data *us) {
  1197. int result;
  1198. /* set the configuration -- STALL is an acceptable response here */
  1199. if (us->pusb_dev->actconfig->desc.bConfigurationValue != 1) {
  1200. usb_stor_dbg(us, "active config #%d != 1 ??\n",
  1201. us->pusb_dev->actconfig->desc.bConfigurationValue);
  1202. return -EINVAL;
  1203. }
  1204. result = usb_reset_configuration(us->pusb_dev);
  1205. usb_stor_dbg(us, "Result of usb_reset_configuration is %d\n", result);
  1206. if (result == -EPIPE) {
  1207. usb_stor_dbg(us, "-- stall on control interface\n");
  1208. } else if (result != 0) {
  1209. /* it's not a stall, but another error -- time to bail */
  1210. usb_stor_dbg(us, "-- Unknown error. Rejecting device\n");
  1211. return -EINVAL;
  1212. }
  1213. us->extra = kzalloc(sizeof(struct sddr09_card_info), GFP_NOIO);
  1214. if (!us->extra)
  1215. return -ENOMEM;
  1216. us->extra_destructor = sddr09_card_info_destructor;
  1217. nand_init_ecc();
  1218. return 0;
  1219. }
  1220. /*
  1221. * This is needed at a very early stage. If this is not listed in the
  1222. * unusual devices list but called from here then LUN 0 of the combo reader
  1223. * is not recognized. But I do not know what precisely these calls do.
  1224. */
  1225. static int
  1226. usb_stor_sddr09_dpcm_init(struct us_data *us) {
  1227. int result;
  1228. unsigned char *data = us->iobuf;
  1229. result = sddr09_common_init(us);
  1230. if (result)
  1231. return result;
  1232. result = sddr09_send_command(us, 0x01, USB_DIR_IN, data, 2);
  1233. if (result) {
  1234. usb_stor_dbg(us, "send_command fails\n");
  1235. return result;
  1236. }
  1237. usb_stor_dbg(us, "%02X %02X\n", data[0], data[1]);
  1238. // get 07 02
  1239. result = sddr09_send_command(us, 0x08, USB_DIR_IN, data, 2);
  1240. if (result) {
  1241. usb_stor_dbg(us, "2nd send_command fails\n");
  1242. return result;
  1243. }
  1244. usb_stor_dbg(us, "%02X %02X\n", data[0], data[1]);
  1245. // get 07 00
  1246. result = sddr09_request_sense(us, data, 18);
  1247. if (result == 0 && data[2] != 0) {
  1248. int j;
  1249. for (j=0; j<18; j++)
  1250. printk(" %02X", data[j]);
  1251. printk("\n");
  1252. // get 70 00 00 00 00 00 00 * 00 00 00 00 00 00
  1253. // 70: current command
  1254. // sense key 0, sense code 0, extd sense code 0
  1255. // additional transfer length * = sizeof(data) - 7
  1256. // Or: 70 00 06 00 00 00 00 0b 00 00 00 00 28 00 00 00 00 00
  1257. // sense key 06, sense code 28: unit attention,
  1258. // not ready to ready transition
  1259. }
  1260. // test unit ready
  1261. return 0; /* not result */
  1262. }
  1263. /*
  1264. * Transport for the Microtech DPCM-USB
  1265. */
  1266. static int dpcm_transport(struct scsi_cmnd *srb, struct us_data *us)
  1267. {
  1268. int ret;
  1269. usb_stor_dbg(us, "LUN=%d\n", (u8)srb->device->lun);
  1270. switch (srb->device->lun) {
  1271. case 0:
  1272. /*
  1273. * LUN 0 corresponds to the CompactFlash card reader.
  1274. */
  1275. ret = usb_stor_CB_transport(srb, us);
  1276. break;
  1277. case 1:
  1278. /*
  1279. * LUN 1 corresponds to the SmartMedia card reader.
  1280. */
  1281. /*
  1282. * Set the LUN to 0 (just in case).
  1283. */
  1284. srb->device->lun = 0;
  1285. ret = sddr09_transport(srb, us);
  1286. srb->device->lun = 1;
  1287. break;
  1288. default:
  1289. usb_stor_dbg(us, "Invalid LUN %d\n", (u8)srb->device->lun);
  1290. ret = USB_STOR_TRANSPORT_ERROR;
  1291. break;
  1292. }
  1293. return ret;
  1294. }
  1295. /*
  1296. * Transport for the Sandisk SDDR-09
  1297. */
  1298. static int sddr09_transport(struct scsi_cmnd *srb, struct us_data *us)
  1299. {
  1300. static unsigned char sensekey = 0, sensecode = 0;
  1301. static unsigned char havefakesense = 0;
  1302. int result, i;
  1303. unsigned char *ptr = us->iobuf;
  1304. unsigned long capacity;
  1305. unsigned int page, pages;
  1306. struct sddr09_card_info *info;
  1307. static unsigned char inquiry_response[8] = {
  1308. 0x00, 0x80, 0x00, 0x02, 0x1F, 0x00, 0x00, 0x00
  1309. };
  1310. /* note: no block descriptor support */
  1311. static unsigned char mode_page_01[19] = {
  1312. 0x00, 0x0F, 0x00, 0x0, 0x0, 0x0, 0x00,
  1313. 0x01, 0x0A,
  1314. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1315. };
  1316. info = (struct sddr09_card_info *)us->extra;
  1317. if (srb->cmnd[0] == REQUEST_SENSE && havefakesense) {
  1318. /* for a faked command, we have to follow with a faked sense */
  1319. memset(ptr, 0, 18);
  1320. ptr[0] = 0x70;
  1321. ptr[2] = sensekey;
  1322. ptr[7] = 11;
  1323. ptr[12] = sensecode;
  1324. usb_stor_set_xfer_buf(ptr, 18, srb);
  1325. sensekey = sensecode = havefakesense = 0;
  1326. return USB_STOR_TRANSPORT_GOOD;
  1327. }
  1328. havefakesense = 1;
  1329. /*
  1330. * Dummy up a response for INQUIRY since SDDR09 doesn't
  1331. * respond to INQUIRY commands
  1332. */
  1333. if (srb->cmnd[0] == INQUIRY) {
  1334. memcpy(ptr, inquiry_response, 8);
  1335. fill_inquiry_response(us, ptr, 36);
  1336. return USB_STOR_TRANSPORT_GOOD;
  1337. }
  1338. if (srb->cmnd[0] == READ_CAPACITY) {
  1339. struct nand_flash_dev *cardinfo;
  1340. sddr09_get_wp(us, info); /* read WP bit */
  1341. cardinfo = sddr09_get_cardinfo(us, info->flags);
  1342. if (!cardinfo) {
  1343. /* probably no media */
  1344. init_error:
  1345. sensekey = 0x02; /* not ready */
  1346. sensecode = 0x3a; /* medium not present */
  1347. return USB_STOR_TRANSPORT_FAILED;
  1348. }
  1349. info->capacity = (1 << cardinfo->chipshift);
  1350. info->pageshift = cardinfo->pageshift;
  1351. info->pagesize = (1 << info->pageshift);
  1352. info->blockshift = cardinfo->blockshift;
  1353. info->blocksize = (1 << info->blockshift);
  1354. info->blockmask = info->blocksize - 1;
  1355. // map initialization, must follow get_cardinfo()
  1356. if (sddr09_read_map(us)) {
  1357. /* probably out of memory */
  1358. goto init_error;
  1359. }
  1360. // Report capacity
  1361. capacity = (info->lbact << info->blockshift) - 1;
  1362. ((__be32 *) ptr)[0] = cpu_to_be32(capacity);
  1363. // Report page size
  1364. ((__be32 *) ptr)[1] = cpu_to_be32(info->pagesize);
  1365. usb_stor_set_xfer_buf(ptr, 8, srb);
  1366. return USB_STOR_TRANSPORT_GOOD;
  1367. }
  1368. if (srb->cmnd[0] == MODE_SENSE_10) {
  1369. int modepage = (srb->cmnd[2] & 0x3F);
  1370. /*
  1371. * They ask for the Read/Write error recovery page,
  1372. * or for all pages.
  1373. */
  1374. /* %% We should check DBD %% */
  1375. if (modepage == 0x01 || modepage == 0x3F) {
  1376. usb_stor_dbg(us, "Dummy up request for mode page 0x%x\n",
  1377. modepage);
  1378. memcpy(ptr, mode_page_01, sizeof(mode_page_01));
  1379. ((__be16*)ptr)[0] = cpu_to_be16(sizeof(mode_page_01) - 2);
  1380. ptr[3] = (info->flags & SDDR09_WP) ? 0x80 : 0;
  1381. usb_stor_set_xfer_buf(ptr, sizeof(mode_page_01), srb);
  1382. return USB_STOR_TRANSPORT_GOOD;
  1383. }
  1384. sensekey = 0x05; /* illegal request */
  1385. sensecode = 0x24; /* invalid field in CDB */
  1386. return USB_STOR_TRANSPORT_FAILED;
  1387. }
  1388. if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL)
  1389. return USB_STOR_TRANSPORT_GOOD;
  1390. havefakesense = 0;
  1391. if (srb->cmnd[0] == READ_10) {
  1392. page = short_pack(srb->cmnd[3], srb->cmnd[2]);
  1393. page <<= 16;
  1394. page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
  1395. pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
  1396. usb_stor_dbg(us, "READ_10: read page %d pagect %d\n",
  1397. page, pages);
  1398. result = sddr09_read_data(us, page, pages);
  1399. return (result == 0 ? USB_STOR_TRANSPORT_GOOD :
  1400. USB_STOR_TRANSPORT_ERROR);
  1401. }
  1402. if (srb->cmnd[0] == WRITE_10) {
  1403. page = short_pack(srb->cmnd[3], srb->cmnd[2]);
  1404. page <<= 16;
  1405. page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
  1406. pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
  1407. usb_stor_dbg(us, "WRITE_10: write page %d pagect %d\n",
  1408. page, pages);
  1409. result = sddr09_write_data(us, page, pages);
  1410. return (result == 0 ? USB_STOR_TRANSPORT_GOOD :
  1411. USB_STOR_TRANSPORT_ERROR);
  1412. }
  1413. /*
  1414. * catch-all for all other commands, except
  1415. * pass TEST_UNIT_READY and REQUEST_SENSE through
  1416. */
  1417. if (srb->cmnd[0] != TEST_UNIT_READY &&
  1418. srb->cmnd[0] != REQUEST_SENSE) {
  1419. sensekey = 0x05; /* illegal request */
  1420. sensecode = 0x20; /* invalid command */
  1421. havefakesense = 1;
  1422. return USB_STOR_TRANSPORT_FAILED;
  1423. }
  1424. for (; srb->cmd_len<12; srb->cmd_len++)
  1425. srb->cmnd[srb->cmd_len] = 0;
  1426. srb->cmnd[1] = LUNBITS;
  1427. ptr[0] = 0;
  1428. for (i=0; i<12; i++)
  1429. sprintf(ptr+strlen(ptr), "%02X ", srb->cmnd[i]);
  1430. usb_stor_dbg(us, "Send control for command %s\n", ptr);
  1431. result = sddr09_send_scsi_command(us, srb->cmnd, 12);
  1432. if (result) {
  1433. usb_stor_dbg(us, "sddr09_send_scsi_command returns %d\n",
  1434. result);
  1435. return USB_STOR_TRANSPORT_ERROR;
  1436. }
  1437. if (scsi_bufflen(srb) == 0)
  1438. return USB_STOR_TRANSPORT_GOOD;
  1439. if (srb->sc_data_direction == DMA_TO_DEVICE ||
  1440. srb->sc_data_direction == DMA_FROM_DEVICE) {
  1441. unsigned int pipe = (srb->sc_data_direction == DMA_TO_DEVICE)
  1442. ? us->send_bulk_pipe : us->recv_bulk_pipe;
  1443. usb_stor_dbg(us, "%s %d bytes\n",
  1444. (srb->sc_data_direction == DMA_TO_DEVICE) ?
  1445. "sending" : "receiving",
  1446. scsi_bufflen(srb));
  1447. result = usb_stor_bulk_srb(us, pipe, srb);
  1448. return (result == USB_STOR_XFER_GOOD ?
  1449. USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_ERROR);
  1450. }
  1451. return USB_STOR_TRANSPORT_GOOD;
  1452. }
  1453. /*
  1454. * Initialization routine for the sddr09 subdriver
  1455. */
  1456. static int
  1457. usb_stor_sddr09_init(struct us_data *us) {
  1458. return sddr09_common_init(us);
  1459. }
  1460. static struct scsi_host_template sddr09_host_template;
  1461. static int sddr09_probe(struct usb_interface *intf,
  1462. const struct usb_device_id *id)
  1463. {
  1464. struct us_data *us;
  1465. int result;
  1466. result = usb_stor_probe1(&us, intf, id,
  1467. (id - sddr09_usb_ids) + sddr09_unusual_dev_list,
  1468. &sddr09_host_template);
  1469. if (result)
  1470. return result;
  1471. if (us->protocol == USB_PR_DPCM_USB) {
  1472. us->transport_name = "Control/Bulk-EUSB/SDDR09";
  1473. us->transport = dpcm_transport;
  1474. us->transport_reset = usb_stor_CB_reset;
  1475. us->max_lun = 1;
  1476. } else {
  1477. us->transport_name = "EUSB/SDDR09";
  1478. us->transport = sddr09_transport;
  1479. us->transport_reset = usb_stor_CB_reset;
  1480. us->max_lun = 0;
  1481. }
  1482. result = usb_stor_probe2(us);
  1483. return result;
  1484. }
  1485. static struct usb_driver sddr09_driver = {
  1486. .name = DRV_NAME,
  1487. .probe = sddr09_probe,
  1488. .disconnect = usb_stor_disconnect,
  1489. .suspend = usb_stor_suspend,
  1490. .resume = usb_stor_resume,
  1491. .reset_resume = usb_stor_reset_resume,
  1492. .pre_reset = usb_stor_pre_reset,
  1493. .post_reset = usb_stor_post_reset,
  1494. .id_table = sddr09_usb_ids,
  1495. .soft_unbind = 1,
  1496. .no_dynamic_id = 1,
  1497. };
  1498. module_usb_stor_driver(sddr09_driver, sddr09_host_template, DRV_NAME);