alauda.c 33 KB

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