alauda.c 34 KB

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