sddr09.c 45 KB

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