alauda.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  1. /*
  2. * Driver for Alauda-based card readers
  3. *
  4. * Current development and maintenance by:
  5. * (c) 2005 Daniel Drake <dsd@gentoo.org>
  6. *
  7. * The 'Alauda' is a chip manufacturered by RATOC for OEM use.
  8. *
  9. * Alauda implements a vendor-specific command set to access two media reader
  10. * ports (XD, SmartMedia). This driver converts SCSI commands to the commands
  11. * which are accepted by these devices.
  12. *
  13. * The driver was developed through reverse-engineering, with the help of the
  14. * sddr09 driver which has many similarities, and with some help from the
  15. * (very old) vendor-supplied GPL sma03 driver.
  16. *
  17. * For protocol info, see http://alauda.sourceforge.net
  18. *
  19. * This program is free software; you can redistribute it and/or modify it
  20. * under the terms of the GNU General Public License as published by the
  21. * Free Software Foundation; either version 2, or (at your option) any
  22. * later version.
  23. *
  24. * This program is distributed in the hope that it will be useful, but
  25. * WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. * General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License along
  30. * with this program; if not, write to the Free Software Foundation, Inc.,
  31. * 675 Mass Ave, Cambridge, MA 02139, USA.
  32. */
  33. #include <linux/module.h>
  34. #include <linux/slab.h>
  35. #include <scsi/scsi.h>
  36. #include <scsi/scsi_cmnd.h>
  37. #include <scsi/scsi_device.h>
  38. #include "usb.h"
  39. #include "transport.h"
  40. #include "protocol.h"
  41. #include "debug.h"
  42. MODULE_DESCRIPTION("Driver for Alauda-based card readers");
  43. MODULE_AUTHOR("Daniel Drake <dsd@gentoo.org>");
  44. MODULE_LICENSE("GPL");
  45. /*
  46. * Status bytes
  47. */
  48. #define ALAUDA_STATUS_ERROR 0x01
  49. #define ALAUDA_STATUS_READY 0x40
  50. /*
  51. * Control opcodes (for request field)
  52. */
  53. #define ALAUDA_GET_XD_MEDIA_STATUS 0x08
  54. #define ALAUDA_GET_SM_MEDIA_STATUS 0x98
  55. #define ALAUDA_ACK_XD_MEDIA_CHANGE 0x0a
  56. #define ALAUDA_ACK_SM_MEDIA_CHANGE 0x9a
  57. #define ALAUDA_GET_XD_MEDIA_SIG 0x86
  58. #define ALAUDA_GET_SM_MEDIA_SIG 0x96
  59. /*
  60. * Bulk command identity (byte 0)
  61. */
  62. #define ALAUDA_BULK_CMD 0x40
  63. /*
  64. * Bulk opcodes (byte 1)
  65. */
  66. #define ALAUDA_BULK_GET_REDU_DATA 0x85
  67. #define ALAUDA_BULK_READ_BLOCK 0x94
  68. #define ALAUDA_BULK_ERASE_BLOCK 0xa3
  69. #define ALAUDA_BULK_WRITE_BLOCK 0xb4
  70. #define ALAUDA_BULK_GET_STATUS2 0xb7
  71. #define ALAUDA_BULK_RESET_MEDIA 0xe0
  72. /*
  73. * Port to operate on (byte 8)
  74. */
  75. #define ALAUDA_PORT_XD 0x00
  76. #define ALAUDA_PORT_SM 0x01
  77. /*
  78. * LBA and PBA are unsigned ints. Special values.
  79. */
  80. #define UNDEF 0xffff
  81. #define SPARE 0xfffe
  82. #define UNUSABLE 0xfffd
  83. struct alauda_media_info {
  84. unsigned long capacity; /* total media size in bytes */
  85. unsigned int pagesize; /* page size in bytes */
  86. unsigned int blocksize; /* number of pages per block */
  87. unsigned int uzonesize; /* number of usable blocks per zone */
  88. unsigned int zonesize; /* number of blocks per zone */
  89. unsigned int blockmask; /* mask to get page from address */
  90. unsigned char pageshift;
  91. unsigned char blockshift;
  92. unsigned char zoneshift;
  93. u16 **lba_to_pba; /* logical to physical block map */
  94. u16 **pba_to_lba; /* physical to logical block map */
  95. };
  96. struct alauda_info {
  97. struct alauda_media_info port[2];
  98. int wr_ep; /* endpoint to write data out of */
  99. unsigned char sense_key;
  100. unsigned long sense_asc; /* additional sense code */
  101. unsigned long sense_ascq; /* additional sense code qualifier */
  102. };
  103. #define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) )
  104. #define LSB_of(s) ((s)&0xFF)
  105. #define MSB_of(s) ((s)>>8)
  106. #define MEDIA_PORT(us) us->srb->device->lun
  107. #define MEDIA_INFO(us) ((struct alauda_info *)us->extra)->port[MEDIA_PORT(us)]
  108. #define PBA_LO(pba) ((pba & 0xF) << 5)
  109. #define PBA_HI(pba) (pba >> 3)
  110. #define PBA_ZONE(pba) (pba >> 11)
  111. static int init_alauda(struct us_data *us);
  112. /*
  113. * The table of devices
  114. */
  115. #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
  116. vendorName, productName, useProtocol, useTransport, \
  117. initFunction, flags) \
  118. { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
  119. .driver_info = (flags) }
  120. static struct usb_device_id alauda_usb_ids[] = {
  121. # include "unusual_alauda.h"
  122. { } /* Terminating entry */
  123. };
  124. MODULE_DEVICE_TABLE(usb, alauda_usb_ids);
  125. #undef UNUSUAL_DEV
  126. /*
  127. * The flags table
  128. */
  129. #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
  130. vendor_name, product_name, use_protocol, use_transport, \
  131. init_function, Flags) \
  132. { \
  133. .vendorName = vendor_name, \
  134. .productName = product_name, \
  135. .useProtocol = use_protocol, \
  136. .useTransport = use_transport, \
  137. .initFunction = init_function, \
  138. }
  139. static struct us_unusual_dev alauda_unusual_dev_list[] = {
  140. # include "unusual_alauda.h"
  141. { } /* Terminating entry */
  142. };
  143. #undef UNUSUAL_DEV
  144. /*
  145. * Media handling
  146. */
  147. struct alauda_card_info {
  148. unsigned char id; /* id byte */
  149. unsigned char chipshift; /* 1<<cs bytes total capacity */
  150. unsigned char pageshift; /* 1<<ps bytes in a page */
  151. unsigned char blockshift; /* 1<<bs pages per block */
  152. unsigned char zoneshift; /* 1<<zs blocks per zone */
  153. };
  154. static struct alauda_card_info alauda_card_ids[] = {
  155. /* NAND flash */
  156. { 0x6e, 20, 8, 4, 8}, /* 1 MB */
  157. { 0xe8, 20, 8, 4, 8}, /* 1 MB */
  158. { 0xec, 20, 8, 4, 8}, /* 1 MB */
  159. { 0x64, 21, 8, 4, 9}, /* 2 MB */
  160. { 0xea, 21, 8, 4, 9}, /* 2 MB */
  161. { 0x6b, 22, 9, 4, 9}, /* 4 MB */
  162. { 0xe3, 22, 9, 4, 9}, /* 4 MB */
  163. { 0xe5, 22, 9, 4, 9}, /* 4 MB */
  164. { 0xe6, 23, 9, 4, 10}, /* 8 MB */
  165. { 0x73, 24, 9, 5, 10}, /* 16 MB */
  166. { 0x75, 25, 9, 5, 10}, /* 32 MB */
  167. { 0x76, 26, 9, 5, 10}, /* 64 MB */
  168. { 0x79, 27, 9, 5, 10}, /* 128 MB */
  169. { 0x71, 28, 9, 5, 10}, /* 256 MB */
  170. /* MASK ROM */
  171. { 0x5d, 21, 9, 4, 8}, /* 2 MB */
  172. { 0xd5, 22, 9, 4, 9}, /* 4 MB */
  173. { 0xd6, 23, 9, 4, 10}, /* 8 MB */
  174. { 0x57, 24, 9, 4, 11}, /* 16 MB */
  175. { 0x58, 25, 9, 4, 12}, /* 32 MB */
  176. { 0,}
  177. };
  178. static struct alauda_card_info *alauda_card_find_id(unsigned char id)
  179. {
  180. int i;
  181. for (i = 0; alauda_card_ids[i].id != 0; i++)
  182. if (alauda_card_ids[i].id == id)
  183. return &(alauda_card_ids[i]);
  184. return NULL;
  185. }
  186. /*
  187. * ECC computation.
  188. */
  189. static unsigned char parity[256];
  190. static unsigned char ecc2[256];
  191. static void nand_init_ecc(void)
  192. {
  193. int i, j, a;
  194. parity[0] = 0;
  195. for (i = 1; i < 256; i++)
  196. parity[i] = (parity[i&(i-1)] ^ 1);
  197. for (i = 0; i < 256; i++) {
  198. a = 0;
  199. for (j = 0; j < 8; j++) {
  200. if (i & (1<<j)) {
  201. if ((j & 1) == 0)
  202. a ^= 0x04;
  203. if ((j & 2) == 0)
  204. a ^= 0x10;
  205. if ((j & 4) == 0)
  206. a ^= 0x40;
  207. }
  208. }
  209. ecc2[i] = ~(a ^ (a<<1) ^ (parity[i] ? 0xa8 : 0));
  210. }
  211. }
  212. /* compute 3-byte ecc on 256 bytes */
  213. static void nand_compute_ecc(unsigned char *data, unsigned char *ecc)
  214. {
  215. int i, j, a;
  216. unsigned char par = 0, bit, bits[8] = {0};
  217. /* collect 16 checksum bits */
  218. for (i = 0; i < 256; i++) {
  219. par ^= data[i];
  220. bit = parity[data[i]];
  221. for (j = 0; j < 8; j++)
  222. if ((i & (1<<j)) == 0)
  223. bits[j] ^= bit;
  224. }
  225. /* put 4+4+4 = 12 bits in the ecc */
  226. a = (bits[3] << 6) + (bits[2] << 4) + (bits[1] << 2) + bits[0];
  227. ecc[0] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
  228. a = (bits[7] << 6) + (bits[6] << 4) + (bits[5] << 2) + bits[4];
  229. ecc[1] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
  230. ecc[2] = ecc2[par];
  231. }
  232. static int nand_compare_ecc(unsigned char *data, unsigned char *ecc)
  233. {
  234. return (data[0] == ecc[0] && data[1] == ecc[1] && data[2] == ecc[2]);
  235. }
  236. static void nand_store_ecc(unsigned char *data, unsigned char *ecc)
  237. {
  238. memcpy(data, ecc, 3);
  239. }
  240. /*
  241. * Alauda driver
  242. */
  243. /*
  244. * Forget our PBA <---> LBA mappings for a particular port
  245. */
  246. static void alauda_free_maps (struct alauda_media_info *media_info)
  247. {
  248. unsigned int shift = media_info->zoneshift
  249. + media_info->blockshift + media_info->pageshift;
  250. unsigned int num_zones = media_info->capacity >> shift;
  251. unsigned int i;
  252. if (media_info->lba_to_pba != NULL)
  253. for (i = 0; i < num_zones; i++) {
  254. kfree(media_info->lba_to_pba[i]);
  255. media_info->lba_to_pba[i] = NULL;
  256. }
  257. if (media_info->pba_to_lba != NULL)
  258. for (i = 0; i < num_zones; i++) {
  259. kfree(media_info->pba_to_lba[i]);
  260. media_info->pba_to_lba[i] = NULL;
  261. }
  262. }
  263. /*
  264. * Returns 2 bytes of status data
  265. * The first byte describes media status, and second byte describes door status
  266. */
  267. static int alauda_get_media_status(struct us_data *us, unsigned char *data)
  268. {
  269. int rc;
  270. unsigned char command;
  271. if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
  272. command = ALAUDA_GET_XD_MEDIA_STATUS;
  273. else
  274. command = ALAUDA_GET_SM_MEDIA_STATUS;
  275. rc = usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
  276. command, 0xc0, 0, 1, data, 2);
  277. usb_stor_dbg(us, "Media status %02X %02X\n", data[0], data[1]);
  278. return rc;
  279. }
  280. /*
  281. * Clears the "media was changed" bit so that we know when it changes again
  282. * in the future.
  283. */
  284. static int alauda_ack_media(struct us_data *us)
  285. {
  286. unsigned char command;
  287. if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
  288. command = ALAUDA_ACK_XD_MEDIA_CHANGE;
  289. else
  290. command = ALAUDA_ACK_SM_MEDIA_CHANGE;
  291. return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
  292. command, 0x40, 0, 1, NULL, 0);
  293. }
  294. /*
  295. * Retrieves a 4-byte media signature, which indicates manufacturer, capacity,
  296. * and some other details.
  297. */
  298. static int alauda_get_media_signature(struct us_data *us, unsigned char *data)
  299. {
  300. unsigned char command;
  301. if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
  302. command = ALAUDA_GET_XD_MEDIA_SIG;
  303. else
  304. command = ALAUDA_GET_SM_MEDIA_SIG;
  305. return usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
  306. command, 0xc0, 0, 0, data, 4);
  307. }
  308. /*
  309. * Resets the media status (but not the whole device?)
  310. */
  311. static int alauda_reset_media(struct us_data *us)
  312. {
  313. unsigned char *command = us->iobuf;
  314. memset(command, 0, 9);
  315. command[0] = ALAUDA_BULK_CMD;
  316. command[1] = ALAUDA_BULK_RESET_MEDIA;
  317. command[8] = MEDIA_PORT(us);
  318. return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  319. command, 9, NULL);
  320. }
  321. /*
  322. * Examines the media and deduces capacity, etc.
  323. */
  324. static int alauda_init_media(struct us_data *us)
  325. {
  326. unsigned char *data = us->iobuf;
  327. int ready = 0;
  328. struct alauda_card_info *media_info;
  329. unsigned int num_zones;
  330. while (ready == 0) {
  331. msleep(20);
  332. if (alauda_get_media_status(us, data) != USB_STOR_XFER_GOOD)
  333. return USB_STOR_TRANSPORT_ERROR;
  334. if (data[0] & 0x10)
  335. ready = 1;
  336. }
  337. usb_stor_dbg(us, "We are ready for action!\n");
  338. if (alauda_ack_media(us) != USB_STOR_XFER_GOOD)
  339. return USB_STOR_TRANSPORT_ERROR;
  340. msleep(10);
  341. if (alauda_get_media_status(us, data) != USB_STOR_XFER_GOOD)
  342. return USB_STOR_TRANSPORT_ERROR;
  343. if (data[0] != 0x14) {
  344. usb_stor_dbg(us, "Media not ready after ack\n");
  345. return USB_STOR_TRANSPORT_ERROR;
  346. }
  347. if (alauda_get_media_signature(us, data) != USB_STOR_XFER_GOOD)
  348. return USB_STOR_TRANSPORT_ERROR;
  349. usb_stor_dbg(us, "Media signature: %4ph\n", data);
  350. media_info = alauda_card_find_id(data[1]);
  351. if (media_info == NULL) {
  352. pr_warn("alauda_init_media: Unrecognised media signature: %4ph\n",
  353. data);
  354. return USB_STOR_TRANSPORT_ERROR;
  355. }
  356. MEDIA_INFO(us).capacity = 1 << media_info->chipshift;
  357. usb_stor_dbg(us, "Found media with capacity: %ldMB\n",
  358. MEDIA_INFO(us).capacity >> 20);
  359. MEDIA_INFO(us).pageshift = media_info->pageshift;
  360. MEDIA_INFO(us).blockshift = media_info->blockshift;
  361. MEDIA_INFO(us).zoneshift = media_info->zoneshift;
  362. MEDIA_INFO(us).pagesize = 1 << media_info->pageshift;
  363. MEDIA_INFO(us).blocksize = 1 << media_info->blockshift;
  364. MEDIA_INFO(us).zonesize = 1 << media_info->zoneshift;
  365. MEDIA_INFO(us).uzonesize = ((1 << media_info->zoneshift) / 128) * 125;
  366. MEDIA_INFO(us).blockmask = MEDIA_INFO(us).blocksize - 1;
  367. num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift
  368. + MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift);
  369. MEDIA_INFO(us).pba_to_lba = kcalloc(num_zones, sizeof(u16*), GFP_NOIO);
  370. MEDIA_INFO(us).lba_to_pba = kcalloc(num_zones, sizeof(u16*), GFP_NOIO);
  371. if (alauda_reset_media(us) != USB_STOR_XFER_GOOD)
  372. return USB_STOR_TRANSPORT_ERROR;
  373. return USB_STOR_TRANSPORT_GOOD;
  374. }
  375. /*
  376. * Examines the media status and does the right thing when the media has gone,
  377. * appeared, or changed.
  378. */
  379. static int alauda_check_media(struct us_data *us)
  380. {
  381. struct alauda_info *info = (struct alauda_info *) us->extra;
  382. unsigned char status[2];
  383. int rc;
  384. rc = alauda_get_media_status(us, status);
  385. /* Check for no media or door open */
  386. if ((status[0] & 0x80) || ((status[0] & 0x1F) == 0x10)
  387. || ((status[1] & 0x01) == 0)) {
  388. usb_stor_dbg(us, "No media, or door open\n");
  389. alauda_free_maps(&MEDIA_INFO(us));
  390. info->sense_key = 0x02;
  391. info->sense_asc = 0x3A;
  392. info->sense_ascq = 0x00;
  393. return USB_STOR_TRANSPORT_FAILED;
  394. }
  395. /* Check for media change */
  396. if (status[0] & 0x08) {
  397. usb_stor_dbg(us, "Media change detected\n");
  398. alauda_free_maps(&MEDIA_INFO(us));
  399. alauda_init_media(us);
  400. info->sense_key = UNIT_ATTENTION;
  401. info->sense_asc = 0x28;
  402. info->sense_ascq = 0x00;
  403. return USB_STOR_TRANSPORT_FAILED;
  404. }
  405. return USB_STOR_TRANSPORT_GOOD;
  406. }
  407. /*
  408. * Checks the status from the 2nd status register
  409. * Returns 3 bytes of status data, only the first is known
  410. */
  411. static int alauda_check_status2(struct us_data *us)
  412. {
  413. int rc;
  414. unsigned char command[] = {
  415. ALAUDA_BULK_CMD, ALAUDA_BULK_GET_STATUS2,
  416. 0, 0, 0, 0, 3, 0, MEDIA_PORT(us)
  417. };
  418. unsigned char data[3];
  419. rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  420. command, 9, NULL);
  421. if (rc != USB_STOR_XFER_GOOD)
  422. return rc;
  423. rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  424. data, 3, NULL);
  425. if (rc != USB_STOR_XFER_GOOD)
  426. return rc;
  427. usb_stor_dbg(us, "%3ph\n", data);
  428. if (data[0] & ALAUDA_STATUS_ERROR)
  429. return USB_STOR_XFER_ERROR;
  430. return USB_STOR_XFER_GOOD;
  431. }
  432. /*
  433. * Gets the redundancy data for the first page of a PBA
  434. * Returns 16 bytes.
  435. */
  436. static int alauda_get_redu_data(struct us_data *us, u16 pba, unsigned char *data)
  437. {
  438. int rc;
  439. unsigned char command[] = {
  440. ALAUDA_BULK_CMD, ALAUDA_BULK_GET_REDU_DATA,
  441. PBA_HI(pba), PBA_ZONE(pba), 0, PBA_LO(pba), 0, 0, MEDIA_PORT(us)
  442. };
  443. rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  444. command, 9, NULL);
  445. if (rc != USB_STOR_XFER_GOOD)
  446. return rc;
  447. return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  448. data, 16, NULL);
  449. }
  450. /*
  451. * Finds the first unused PBA in a zone
  452. * Returns the absolute PBA of an unused PBA, or 0 if none found.
  453. */
  454. static u16 alauda_find_unused_pba(struct alauda_media_info *info,
  455. unsigned int zone)
  456. {
  457. u16 *pba_to_lba = info->pba_to_lba[zone];
  458. unsigned int i;
  459. for (i = 0; i < info->zonesize; i++)
  460. if (pba_to_lba[i] == UNDEF)
  461. return (zone << info->zoneshift) + i;
  462. return 0;
  463. }
  464. /*
  465. * Reads the redundancy data for all PBA's in a zone
  466. * Produces lba <--> pba mappings
  467. */
  468. static int alauda_read_map(struct us_data *us, unsigned int zone)
  469. {
  470. unsigned char *data = us->iobuf;
  471. int result;
  472. int i, j;
  473. unsigned int zonesize = MEDIA_INFO(us).zonesize;
  474. unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
  475. unsigned int lba_offset, lba_real, blocknum;
  476. unsigned int zone_base_lba = zone * uzonesize;
  477. unsigned int zone_base_pba = zone * zonesize;
  478. u16 *lba_to_pba = kcalloc(zonesize, sizeof(u16), GFP_NOIO);
  479. u16 *pba_to_lba = kcalloc(zonesize, sizeof(u16), GFP_NOIO);
  480. if (lba_to_pba == NULL || pba_to_lba == NULL) {
  481. result = USB_STOR_TRANSPORT_ERROR;
  482. goto error;
  483. }
  484. usb_stor_dbg(us, "Mapping blocks for zone %d\n", zone);
  485. /* 1024 PBA's per zone */
  486. for (i = 0; i < zonesize; i++)
  487. lba_to_pba[i] = pba_to_lba[i] = UNDEF;
  488. for (i = 0; i < zonesize; i++) {
  489. blocknum = zone_base_pba + i;
  490. result = alauda_get_redu_data(us, blocknum, data);
  491. if (result != USB_STOR_XFER_GOOD) {
  492. result = USB_STOR_TRANSPORT_ERROR;
  493. goto error;
  494. }
  495. /* special PBAs have control field 0^16 */
  496. for (j = 0; j < 16; j++)
  497. if (data[j] != 0)
  498. goto nonz;
  499. pba_to_lba[i] = UNUSABLE;
  500. usb_stor_dbg(us, "PBA %d has no logical mapping\n", blocknum);
  501. continue;
  502. nonz:
  503. /* unwritten PBAs have control field FF^16 */
  504. for (j = 0; j < 16; j++)
  505. if (data[j] != 0xff)
  506. goto nonff;
  507. continue;
  508. nonff:
  509. /* normal PBAs start with six FFs */
  510. if (j < 6) {
  511. usb_stor_dbg(us, "PBA %d has no logical mapping: reserved area = %02X%02X%02X%02X data status %02X block status %02X\n",
  512. blocknum,
  513. data[0], data[1], data[2], data[3],
  514. data[4], data[5]);
  515. pba_to_lba[i] = UNUSABLE;
  516. continue;
  517. }
  518. if ((data[6] >> 4) != 0x01) {
  519. usb_stor_dbg(us, "PBA %d has invalid address field %02X%02X/%02X%02X\n",
  520. blocknum, data[6], data[7],
  521. data[11], data[12]);
  522. pba_to_lba[i] = UNUSABLE;
  523. continue;
  524. }
  525. /* check even parity */
  526. if (parity[data[6] ^ data[7]]) {
  527. printk(KERN_WARNING
  528. "alauda_read_map: Bad parity in LBA for block %d"
  529. " (%02X %02X)\n", i, data[6], data[7]);
  530. pba_to_lba[i] = UNUSABLE;
  531. continue;
  532. }
  533. lba_offset = short_pack(data[7], data[6]);
  534. lba_offset = (lba_offset & 0x07FF) >> 1;
  535. lba_real = lba_offset + zone_base_lba;
  536. /*
  537. * Every 1024 physical blocks ("zone"), the LBA numbers
  538. * go back to zero, but are within a higher block of LBA's.
  539. * Also, there is a maximum of 1000 LBA's per zone.
  540. * In other words, in PBA 1024-2047 you will find LBA 0-999
  541. * which are really LBA 1000-1999. This allows for 24 bad
  542. * or special physical blocks per zone.
  543. */
  544. if (lba_offset >= uzonesize) {
  545. printk(KERN_WARNING
  546. "alauda_read_map: Bad low LBA %d for block %d\n",
  547. lba_real, blocknum);
  548. continue;
  549. }
  550. if (lba_to_pba[lba_offset] != UNDEF) {
  551. printk(KERN_WARNING
  552. "alauda_read_map: "
  553. "LBA %d seen for PBA %d and %d\n",
  554. lba_real, lba_to_pba[lba_offset], blocknum);
  555. continue;
  556. }
  557. pba_to_lba[i] = lba_real;
  558. lba_to_pba[lba_offset] = blocknum;
  559. continue;
  560. }
  561. MEDIA_INFO(us).lba_to_pba[zone] = lba_to_pba;
  562. MEDIA_INFO(us).pba_to_lba[zone] = pba_to_lba;
  563. result = 0;
  564. goto out;
  565. error:
  566. kfree(lba_to_pba);
  567. kfree(pba_to_lba);
  568. out:
  569. return result;
  570. }
  571. /*
  572. * Checks to see whether we have already mapped a certain zone
  573. * If we haven't, the map is generated
  574. */
  575. static void alauda_ensure_map_for_zone(struct us_data *us, unsigned int zone)
  576. {
  577. if (MEDIA_INFO(us).lba_to_pba[zone] == NULL
  578. || MEDIA_INFO(us).pba_to_lba[zone] == NULL)
  579. alauda_read_map(us, zone);
  580. }
  581. /*
  582. * Erases an entire block
  583. */
  584. static int alauda_erase_block(struct us_data *us, u16 pba)
  585. {
  586. int rc;
  587. unsigned char command[] = {
  588. ALAUDA_BULK_CMD, ALAUDA_BULK_ERASE_BLOCK, PBA_HI(pba),
  589. PBA_ZONE(pba), 0, PBA_LO(pba), 0x02, 0, MEDIA_PORT(us)
  590. };
  591. unsigned char buf[2];
  592. usb_stor_dbg(us, "Erasing PBA %d\n", pba);
  593. rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  594. command, 9, NULL);
  595. if (rc != USB_STOR_XFER_GOOD)
  596. return rc;
  597. rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  598. buf, 2, NULL);
  599. if (rc != USB_STOR_XFER_GOOD)
  600. return rc;
  601. usb_stor_dbg(us, "Erase result: %02X %02X\n", buf[0], buf[1]);
  602. return rc;
  603. }
  604. /*
  605. * Reads data from a certain offset page inside a PBA, including interleaved
  606. * redundancy data. Returns (pagesize+64)*pages bytes in data.
  607. */
  608. static int alauda_read_block_raw(struct us_data *us, u16 pba,
  609. unsigned int page, unsigned int pages, unsigned char *data)
  610. {
  611. int rc;
  612. unsigned char command[] = {
  613. ALAUDA_BULK_CMD, ALAUDA_BULK_READ_BLOCK, PBA_HI(pba),
  614. PBA_ZONE(pba), 0, PBA_LO(pba) + page, pages, 0, MEDIA_PORT(us)
  615. };
  616. usb_stor_dbg(us, "pba %d page %d count %d\n", pba, page, pages);
  617. rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  618. command, 9, NULL);
  619. if (rc != USB_STOR_XFER_GOOD)
  620. return rc;
  621. return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  622. data, (MEDIA_INFO(us).pagesize + 64) * pages, NULL);
  623. }
  624. /*
  625. * Reads data from a certain offset page inside a PBA, excluding redundancy
  626. * data. Returns pagesize*pages bytes in data. Note that data must be big enough
  627. * to hold (pagesize+64)*pages bytes of data, but you can ignore those 'extra'
  628. * trailing bytes outside this function.
  629. */
  630. static int alauda_read_block(struct us_data *us, u16 pba,
  631. unsigned int page, unsigned int pages, unsigned char *data)
  632. {
  633. int i, rc;
  634. unsigned int pagesize = MEDIA_INFO(us).pagesize;
  635. rc = alauda_read_block_raw(us, pba, page, pages, data);
  636. if (rc != USB_STOR_XFER_GOOD)
  637. return rc;
  638. /* Cut out the redundancy data */
  639. for (i = 0; i < pages; i++) {
  640. int dest_offset = i * pagesize;
  641. int src_offset = i * (pagesize + 64);
  642. memmove(data + dest_offset, data + src_offset, pagesize);
  643. }
  644. return rc;
  645. }
  646. /*
  647. * Writes an entire block of data and checks status after write.
  648. * Redundancy data must be already included in data. Data should be
  649. * (pagesize+64)*blocksize bytes in length.
  650. */
  651. static int alauda_write_block(struct us_data *us, u16 pba, unsigned char *data)
  652. {
  653. int rc;
  654. struct alauda_info *info = (struct alauda_info *) us->extra;
  655. unsigned char command[] = {
  656. ALAUDA_BULK_CMD, ALAUDA_BULK_WRITE_BLOCK, PBA_HI(pba),
  657. PBA_ZONE(pba), 0, PBA_LO(pba), 32, 0, MEDIA_PORT(us)
  658. };
  659. usb_stor_dbg(us, "pba %d\n", pba);
  660. rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  661. command, 9, NULL);
  662. if (rc != USB_STOR_XFER_GOOD)
  663. return rc;
  664. rc = usb_stor_bulk_transfer_buf(us, info->wr_ep, data,
  665. (MEDIA_INFO(us).pagesize + 64) * MEDIA_INFO(us).blocksize,
  666. NULL);
  667. if (rc != USB_STOR_XFER_GOOD)
  668. return rc;
  669. return alauda_check_status2(us);
  670. }
  671. /*
  672. * Write some data to a specific LBA.
  673. */
  674. static int alauda_write_lba(struct us_data *us, u16 lba,
  675. unsigned int page, unsigned int pages,
  676. unsigned char *ptr, unsigned char *blockbuffer)
  677. {
  678. u16 pba, lbap, new_pba;
  679. unsigned char *bptr, *cptr, *xptr;
  680. unsigned char ecc[3];
  681. int i, result;
  682. unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
  683. unsigned int zonesize = MEDIA_INFO(us).zonesize;
  684. unsigned int pagesize = MEDIA_INFO(us).pagesize;
  685. unsigned int blocksize = MEDIA_INFO(us).blocksize;
  686. unsigned int lba_offset = lba % uzonesize;
  687. unsigned int new_pba_offset;
  688. unsigned int zone = lba / uzonesize;
  689. alauda_ensure_map_for_zone(us, zone);
  690. pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
  691. if (pba == 1) {
  692. /* Maybe it is impossible to write to PBA 1.
  693. Fake success, but don't do anything. */
  694. printk(KERN_WARNING
  695. "alauda_write_lba: avoid writing to pba 1\n");
  696. return USB_STOR_TRANSPORT_GOOD;
  697. }
  698. new_pba = alauda_find_unused_pba(&MEDIA_INFO(us), zone);
  699. if (!new_pba) {
  700. printk(KERN_WARNING
  701. "alauda_write_lba: Out of unused blocks\n");
  702. return USB_STOR_TRANSPORT_ERROR;
  703. }
  704. /* read old contents */
  705. if (pba != UNDEF) {
  706. result = alauda_read_block_raw(us, pba, 0,
  707. blocksize, blockbuffer);
  708. if (result != USB_STOR_XFER_GOOD)
  709. return result;
  710. } else {
  711. memset(blockbuffer, 0, blocksize * (pagesize + 64));
  712. }
  713. lbap = (lba_offset << 1) | 0x1000;
  714. if (parity[MSB_of(lbap) ^ LSB_of(lbap)])
  715. lbap ^= 1;
  716. /* check old contents and fill lba */
  717. for (i = 0; i < blocksize; i++) {
  718. bptr = blockbuffer + (i * (pagesize + 64));
  719. cptr = bptr + pagesize;
  720. nand_compute_ecc(bptr, ecc);
  721. if (!nand_compare_ecc(cptr+13, ecc)) {
  722. usb_stor_dbg(us, "Warning: bad ecc in page %d- of pba %d\n",
  723. i, pba);
  724. nand_store_ecc(cptr+13, ecc);
  725. }
  726. nand_compute_ecc(bptr + (pagesize / 2), ecc);
  727. if (!nand_compare_ecc(cptr+8, ecc)) {
  728. usb_stor_dbg(us, "Warning: bad ecc in page %d+ of pba %d\n",
  729. i, pba);
  730. nand_store_ecc(cptr+8, ecc);
  731. }
  732. cptr[6] = cptr[11] = MSB_of(lbap);
  733. cptr[7] = cptr[12] = LSB_of(lbap);
  734. }
  735. /* copy in new stuff and compute ECC */
  736. xptr = ptr;
  737. for (i = page; i < page+pages; i++) {
  738. bptr = blockbuffer + (i * (pagesize + 64));
  739. cptr = bptr + pagesize;
  740. memcpy(bptr, xptr, pagesize);
  741. xptr += pagesize;
  742. nand_compute_ecc(bptr, ecc);
  743. nand_store_ecc(cptr+13, ecc);
  744. nand_compute_ecc(bptr + (pagesize / 2), ecc);
  745. nand_store_ecc(cptr+8, ecc);
  746. }
  747. result = alauda_write_block(us, new_pba, blockbuffer);
  748. if (result != USB_STOR_XFER_GOOD)
  749. return result;
  750. new_pba_offset = new_pba - (zone * zonesize);
  751. MEDIA_INFO(us).pba_to_lba[zone][new_pba_offset] = lba;
  752. MEDIA_INFO(us).lba_to_pba[zone][lba_offset] = new_pba;
  753. usb_stor_dbg(us, "Remapped LBA %d to PBA %d\n", lba, new_pba);
  754. if (pba != UNDEF) {
  755. unsigned int pba_offset = pba - (zone * zonesize);
  756. result = alauda_erase_block(us, pba);
  757. if (result != USB_STOR_XFER_GOOD)
  758. return result;
  759. MEDIA_INFO(us).pba_to_lba[zone][pba_offset] = UNDEF;
  760. }
  761. return USB_STOR_TRANSPORT_GOOD;
  762. }
  763. /*
  764. * Read data from a specific sector address
  765. */
  766. static int alauda_read_data(struct us_data *us, unsigned long address,
  767. unsigned int sectors)
  768. {
  769. unsigned char *buffer;
  770. u16 lba, max_lba;
  771. unsigned int page, len, offset;
  772. unsigned int blockshift = MEDIA_INFO(us).blockshift;
  773. unsigned int pageshift = MEDIA_INFO(us).pageshift;
  774. unsigned int blocksize = MEDIA_INFO(us).blocksize;
  775. unsigned int pagesize = MEDIA_INFO(us).pagesize;
  776. unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
  777. struct scatterlist *sg;
  778. int result;
  779. /*
  780. * Since we only read in one block at a time, we have to create
  781. * a bounce buffer and move the data a piece at a time between the
  782. * bounce buffer and the actual transfer buffer.
  783. * We make this buffer big enough to hold temporary redundancy data,
  784. * which we use when reading the data blocks.
  785. */
  786. len = min(sectors, blocksize) * (pagesize + 64);
  787. buffer = kmalloc(len, GFP_NOIO);
  788. if (buffer == NULL) {
  789. printk(KERN_WARNING "alauda_read_data: Out of memory\n");
  790. return USB_STOR_TRANSPORT_ERROR;
  791. }
  792. /* Figure out the initial LBA and page */
  793. lba = address >> blockshift;
  794. page = (address & MEDIA_INFO(us).blockmask);
  795. max_lba = MEDIA_INFO(us).capacity >> (blockshift + pageshift);
  796. result = USB_STOR_TRANSPORT_GOOD;
  797. offset = 0;
  798. sg = NULL;
  799. while (sectors > 0) {
  800. unsigned int zone = lba / uzonesize; /* integer division */
  801. unsigned int lba_offset = lba - (zone * uzonesize);
  802. unsigned int pages;
  803. u16 pba;
  804. alauda_ensure_map_for_zone(us, zone);
  805. /* Not overflowing capacity? */
  806. if (lba >= max_lba) {
  807. usb_stor_dbg(us, "Error: Requested lba %u exceeds maximum %u\n",
  808. lba, max_lba);
  809. result = USB_STOR_TRANSPORT_ERROR;
  810. break;
  811. }
  812. /* Find number of pages we can read in this block */
  813. pages = min(sectors, blocksize - page);
  814. len = pages << pageshift;
  815. /* Find where this lba lives on disk */
  816. pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
  817. if (pba == UNDEF) { /* this lba was never written */
  818. usb_stor_dbg(us, "Read %d zero pages (LBA %d) page %d\n",
  819. pages, lba, page);
  820. /* This is not really an error. It just means
  821. that the block has never been written.
  822. Instead of returning USB_STOR_TRANSPORT_ERROR
  823. it is better to return all zero data. */
  824. memset(buffer, 0, len);
  825. } else {
  826. usb_stor_dbg(us, "Read %d pages, from PBA %d (LBA %d) page %d\n",
  827. pages, pba, lba, page);
  828. result = alauda_read_block(us, pba, page, pages, buffer);
  829. if (result != USB_STOR_TRANSPORT_GOOD)
  830. break;
  831. }
  832. /* Store the data in the transfer buffer */
  833. usb_stor_access_xfer_buf(buffer, len, us->srb,
  834. &sg, &offset, TO_XFER_BUF);
  835. page = 0;
  836. lba++;
  837. sectors -= pages;
  838. }
  839. kfree(buffer);
  840. return result;
  841. }
  842. /*
  843. * Write data to a specific sector address
  844. */
  845. static int alauda_write_data(struct us_data *us, unsigned long address,
  846. unsigned int sectors)
  847. {
  848. unsigned char *buffer, *blockbuffer;
  849. unsigned int page, len, offset;
  850. unsigned int blockshift = MEDIA_INFO(us).blockshift;
  851. unsigned int pageshift = MEDIA_INFO(us).pageshift;
  852. unsigned int blocksize = MEDIA_INFO(us).blocksize;
  853. unsigned int pagesize = MEDIA_INFO(us).pagesize;
  854. struct scatterlist *sg;
  855. u16 lba, max_lba;
  856. int result;
  857. /*
  858. * Since we don't write the user data directly to the device,
  859. * we have to create a bounce buffer and move the data a piece
  860. * at a time between the bounce buffer and the actual transfer buffer.
  861. */
  862. len = min(sectors, blocksize) * pagesize;
  863. buffer = kmalloc(len, GFP_NOIO);
  864. if (buffer == NULL) {
  865. printk(KERN_WARNING "alauda_write_data: Out of memory\n");
  866. return USB_STOR_TRANSPORT_ERROR;
  867. }
  868. /*
  869. * We also need a temporary block buffer, where we read in the old data,
  870. * overwrite parts with the new data, and manipulate the redundancy data
  871. */
  872. blockbuffer = kmalloc((pagesize + 64) * blocksize, GFP_NOIO);
  873. if (blockbuffer == NULL) {
  874. printk(KERN_WARNING "alauda_write_data: Out of memory\n");
  875. kfree(buffer);
  876. return USB_STOR_TRANSPORT_ERROR;
  877. }
  878. /* Figure out the initial LBA and page */
  879. lba = address >> blockshift;
  880. page = (address & MEDIA_INFO(us).blockmask);
  881. max_lba = MEDIA_INFO(us).capacity >> (pageshift + blockshift);
  882. result = USB_STOR_TRANSPORT_GOOD;
  883. offset = 0;
  884. sg = NULL;
  885. while (sectors > 0) {
  886. /* Write as many sectors as possible in this block */
  887. unsigned int pages = min(sectors, blocksize - page);
  888. len = pages << pageshift;
  889. /* Not overflowing capacity? */
  890. if (lba >= max_lba) {
  891. usb_stor_dbg(us, "Requested lba %u exceeds maximum %u\n",
  892. lba, max_lba);
  893. result = USB_STOR_TRANSPORT_ERROR;
  894. break;
  895. }
  896. /* Get the data from the transfer buffer */
  897. usb_stor_access_xfer_buf(buffer, len, us->srb,
  898. &sg, &offset, FROM_XFER_BUF);
  899. result = alauda_write_lba(us, lba, page, pages, buffer,
  900. blockbuffer);
  901. if (result != USB_STOR_TRANSPORT_GOOD)
  902. break;
  903. page = 0;
  904. lba++;
  905. sectors -= pages;
  906. }
  907. kfree(buffer);
  908. kfree(blockbuffer);
  909. return result;
  910. }
  911. /*
  912. * Our interface with the rest of the world
  913. */
  914. static void alauda_info_destructor(void *extra)
  915. {
  916. struct alauda_info *info = (struct alauda_info *) extra;
  917. int port;
  918. if (!info)
  919. return;
  920. for (port = 0; port < 2; port++) {
  921. struct alauda_media_info *media_info = &info->port[port];
  922. alauda_free_maps(media_info);
  923. kfree(media_info->lba_to_pba);
  924. kfree(media_info->pba_to_lba);
  925. }
  926. }
  927. /*
  928. * Initialize alauda_info struct and find the data-write endpoint
  929. */
  930. static int init_alauda(struct us_data *us)
  931. {
  932. struct alauda_info *info;
  933. struct usb_host_interface *altsetting = us->pusb_intf->cur_altsetting;
  934. nand_init_ecc();
  935. us->extra = kzalloc(sizeof(struct alauda_info), GFP_NOIO);
  936. if (!us->extra)
  937. return USB_STOR_TRANSPORT_ERROR;
  938. info = (struct alauda_info *) us->extra;
  939. us->extra_destructor = alauda_info_destructor;
  940. info->wr_ep = usb_sndbulkpipe(us->pusb_dev,
  941. altsetting->endpoint[0].desc.bEndpointAddress
  942. & USB_ENDPOINT_NUMBER_MASK);
  943. return USB_STOR_TRANSPORT_GOOD;
  944. }
  945. static int alauda_transport(struct scsi_cmnd *srb, struct us_data *us)
  946. {
  947. int rc;
  948. struct alauda_info *info = (struct alauda_info *) us->extra;
  949. unsigned char *ptr = us->iobuf;
  950. static unsigned char inquiry_response[36] = {
  951. 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
  952. };
  953. if (srb->cmnd[0] == INQUIRY) {
  954. usb_stor_dbg(us, "INQUIRY - Returning bogus response\n");
  955. memcpy(ptr, inquiry_response, sizeof(inquiry_response));
  956. fill_inquiry_response(us, ptr, 36);
  957. return USB_STOR_TRANSPORT_GOOD;
  958. }
  959. if (srb->cmnd[0] == TEST_UNIT_READY) {
  960. usb_stor_dbg(us, "TEST_UNIT_READY\n");
  961. return alauda_check_media(us);
  962. }
  963. if (srb->cmnd[0] == READ_CAPACITY) {
  964. unsigned int num_zones;
  965. unsigned long capacity;
  966. rc = alauda_check_media(us);
  967. if (rc != USB_STOR_TRANSPORT_GOOD)
  968. return rc;
  969. num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift
  970. + MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift);
  971. capacity = num_zones * MEDIA_INFO(us).uzonesize
  972. * MEDIA_INFO(us).blocksize;
  973. /* Report capacity and page size */
  974. ((__be32 *) ptr)[0] = cpu_to_be32(capacity - 1);
  975. ((__be32 *) ptr)[1] = cpu_to_be32(512);
  976. usb_stor_set_xfer_buf(ptr, 8, srb);
  977. return USB_STOR_TRANSPORT_GOOD;
  978. }
  979. if (srb->cmnd[0] == READ_10) {
  980. unsigned int page, pages;
  981. rc = alauda_check_media(us);
  982. if (rc != USB_STOR_TRANSPORT_GOOD)
  983. return rc;
  984. page = short_pack(srb->cmnd[3], srb->cmnd[2]);
  985. page <<= 16;
  986. page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
  987. pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
  988. usb_stor_dbg(us, "READ_10: page %d pagect %d\n", page, pages);
  989. return alauda_read_data(us, page, pages);
  990. }
  991. if (srb->cmnd[0] == WRITE_10) {
  992. unsigned int page, pages;
  993. rc = alauda_check_media(us);
  994. if (rc != USB_STOR_TRANSPORT_GOOD)
  995. return rc;
  996. page = short_pack(srb->cmnd[3], srb->cmnd[2]);
  997. page <<= 16;
  998. page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
  999. pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
  1000. usb_stor_dbg(us, "WRITE_10: page %d pagect %d\n", page, pages);
  1001. return alauda_write_data(us, page, pages);
  1002. }
  1003. if (srb->cmnd[0] == REQUEST_SENSE) {
  1004. usb_stor_dbg(us, "REQUEST_SENSE\n");
  1005. memset(ptr, 0, 18);
  1006. ptr[0] = 0xF0;
  1007. ptr[2] = info->sense_key;
  1008. ptr[7] = 11;
  1009. ptr[12] = info->sense_asc;
  1010. ptr[13] = info->sense_ascq;
  1011. usb_stor_set_xfer_buf(ptr, 18, srb);
  1012. return USB_STOR_TRANSPORT_GOOD;
  1013. }
  1014. if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
  1015. /* sure. whatever. not like we can stop the user from popping
  1016. the media out of the device (no locking doors, etc) */
  1017. return USB_STOR_TRANSPORT_GOOD;
  1018. }
  1019. usb_stor_dbg(us, "Gah! Unknown command: %d (0x%x)\n",
  1020. srb->cmnd[0], srb->cmnd[0]);
  1021. info->sense_key = 0x05;
  1022. info->sense_asc = 0x20;
  1023. info->sense_ascq = 0x00;
  1024. return USB_STOR_TRANSPORT_FAILED;
  1025. }
  1026. static int alauda_probe(struct usb_interface *intf,
  1027. const struct usb_device_id *id)
  1028. {
  1029. struct us_data *us;
  1030. int result;
  1031. result = usb_stor_probe1(&us, intf, id,
  1032. (id - alauda_usb_ids) + alauda_unusual_dev_list);
  1033. if (result)
  1034. return result;
  1035. us->transport_name = "Alauda Control/Bulk";
  1036. us->transport = alauda_transport;
  1037. us->transport_reset = usb_stor_Bulk_reset;
  1038. us->max_lun = 1;
  1039. result = usb_stor_probe2(us);
  1040. return result;
  1041. }
  1042. static struct usb_driver alauda_driver = {
  1043. .name = "ums-alauda",
  1044. .probe = alauda_probe,
  1045. .disconnect = usb_stor_disconnect,
  1046. .suspend = usb_stor_suspend,
  1047. .resume = usb_stor_resume,
  1048. .reset_resume = usb_stor_reset_resume,
  1049. .pre_reset = usb_stor_pre_reset,
  1050. .post_reset = usb_stor_post_reset,
  1051. .id_table = alauda_usb_ids,
  1052. .soft_unbind = 1,
  1053. .no_dynamic_id = 1,
  1054. };
  1055. module_usb_driver(alauda_driver);