alauda.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  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. int i;
  180. for (i = 0; alauda_card_ids[i].id != 0; i++)
  181. if (alauda_card_ids[i].id == id)
  182. return &(alauda_card_ids[i]);
  183. return NULL;
  184. }
  185. /*
  186. * ECC computation.
  187. */
  188. static unsigned char parity[256];
  189. static unsigned char ecc2[256];
  190. static void nand_init_ecc(void) {
  191. int i, j, a;
  192. parity[0] = 0;
  193. for (i = 1; i < 256; i++)
  194. parity[i] = (parity[i&(i-1)] ^ 1);
  195. for (i = 0; i < 256; i++) {
  196. a = 0;
  197. for (j = 0; j < 8; j++) {
  198. if (i & (1<<j)) {
  199. if ((j & 1) == 0)
  200. a ^= 0x04;
  201. if ((j & 2) == 0)
  202. a ^= 0x10;
  203. if ((j & 4) == 0)
  204. a ^= 0x40;
  205. }
  206. }
  207. ecc2[i] = ~(a ^ (a<<1) ^ (parity[i] ? 0xa8 : 0));
  208. }
  209. }
  210. /* compute 3-byte ecc on 256 bytes */
  211. static void nand_compute_ecc(unsigned char *data, unsigned char *ecc) {
  212. int i, j, a;
  213. unsigned char par = 0, bit, bits[8] = {0};
  214. /* collect 16 checksum bits */
  215. for (i = 0; i < 256; i++) {
  216. par ^= data[i];
  217. bit = parity[data[i]];
  218. for (j = 0; j < 8; j++)
  219. if ((i & (1<<j)) == 0)
  220. bits[j] ^= bit;
  221. }
  222. /* put 4+4+4 = 12 bits in the ecc */
  223. a = (bits[3] << 6) + (bits[2] << 4) + (bits[1] << 2) + bits[0];
  224. ecc[0] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
  225. a = (bits[7] << 6) + (bits[6] << 4) + (bits[5] << 2) + bits[4];
  226. ecc[1] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
  227. ecc[2] = ecc2[par];
  228. }
  229. static int nand_compare_ecc(unsigned char *data, unsigned char *ecc) {
  230. return (data[0] == ecc[0] && data[1] == ecc[1] && data[2] == ecc[2]);
  231. }
  232. static void nand_store_ecc(unsigned char *data, unsigned char *ecc) {
  233. memcpy(data, ecc, 3);
  234. }
  235. /*
  236. * Alauda driver
  237. */
  238. /*
  239. * Forget our PBA <---> LBA mappings for a particular port
  240. */
  241. static void alauda_free_maps (struct alauda_media_info *media_info)
  242. {
  243. unsigned int shift = media_info->zoneshift
  244. + media_info->blockshift + media_info->pageshift;
  245. unsigned int num_zones = media_info->capacity >> shift;
  246. unsigned int i;
  247. if (media_info->lba_to_pba != NULL)
  248. for (i = 0; i < num_zones; i++) {
  249. kfree(media_info->lba_to_pba[i]);
  250. media_info->lba_to_pba[i] = NULL;
  251. }
  252. if (media_info->pba_to_lba != NULL)
  253. for (i = 0; i < num_zones; i++) {
  254. kfree(media_info->pba_to_lba[i]);
  255. media_info->pba_to_lba[i] = NULL;
  256. }
  257. }
  258. /*
  259. * Returns 2 bytes of status data
  260. * The first byte describes media status, and second byte describes door status
  261. */
  262. static int alauda_get_media_status(struct us_data *us, unsigned char *data)
  263. {
  264. int rc;
  265. unsigned char command;
  266. if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
  267. command = ALAUDA_GET_XD_MEDIA_STATUS;
  268. else
  269. command = ALAUDA_GET_SM_MEDIA_STATUS;
  270. rc = usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
  271. command, 0xc0, 0, 1, data, 2);
  272. usb_stor_dbg(us, "Media status %02X %02X\n", 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. usb_stor_dbg(us, "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. usb_stor_dbg(us, "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. usb_stor_dbg(us, "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. usb_stor_dbg(us, "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. usb_stor_dbg(us, "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. usb_stor_dbg(us, "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. usb_stor_dbg(us, "%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. usb_stor_dbg(us, "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. usb_stor_dbg(us, "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. usb_stor_dbg(us, "PBA %d has no logical mapping: reserved area = %02X%02X%02X%02X data status %02X block status %02X\n",
  510. blocknum,
  511. data[0], data[1], data[2], data[3],
  512. data[4], data[5]);
  513. pba_to_lba[i] = UNUSABLE;
  514. continue;
  515. }
  516. if ((data[6] >> 4) != 0x01) {
  517. usb_stor_dbg(us, "PBA %d has invalid address field %02X%02X/%02X%02X\n",
  518. blocknum, data[6], data[7],
  519. data[11], data[12]);
  520. pba_to_lba[i] = UNUSABLE;
  521. continue;
  522. }
  523. /* check even parity */
  524. if (parity[data[6] ^ data[7]]) {
  525. printk(KERN_WARNING
  526. "alauda_read_map: Bad parity in LBA for block %d"
  527. " (%02X %02X)\n", i, data[6], data[7]);
  528. pba_to_lba[i] = UNUSABLE;
  529. continue;
  530. }
  531. lba_offset = short_pack(data[7], data[6]);
  532. lba_offset = (lba_offset & 0x07FF) >> 1;
  533. lba_real = lba_offset + zone_base_lba;
  534. /*
  535. * Every 1024 physical blocks ("zone"), the LBA numbers
  536. * go back to zero, but are within a higher block of LBA's.
  537. * Also, there is a maximum of 1000 LBA's per zone.
  538. * In other words, in PBA 1024-2047 you will find LBA 0-999
  539. * which are really LBA 1000-1999. This allows for 24 bad
  540. * or special physical blocks per zone.
  541. */
  542. if (lba_offset >= uzonesize) {
  543. printk(KERN_WARNING
  544. "alauda_read_map: Bad low LBA %d for block %d\n",
  545. lba_real, blocknum);
  546. continue;
  547. }
  548. if (lba_to_pba[lba_offset] != UNDEF) {
  549. printk(KERN_WARNING
  550. "alauda_read_map: "
  551. "LBA %d seen for PBA %d and %d\n",
  552. lba_real, lba_to_pba[lba_offset], blocknum);
  553. continue;
  554. }
  555. pba_to_lba[i] = lba_real;
  556. lba_to_pba[lba_offset] = blocknum;
  557. continue;
  558. }
  559. MEDIA_INFO(us).lba_to_pba[zone] = lba_to_pba;
  560. MEDIA_INFO(us).pba_to_lba[zone] = pba_to_lba;
  561. result = 0;
  562. goto out;
  563. error:
  564. kfree(lba_to_pba);
  565. kfree(pba_to_lba);
  566. out:
  567. return result;
  568. }
  569. /*
  570. * Checks to see whether we have already mapped a certain zone
  571. * If we haven't, the map is generated
  572. */
  573. static void alauda_ensure_map_for_zone(struct us_data *us, unsigned int zone)
  574. {
  575. if (MEDIA_INFO(us).lba_to_pba[zone] == NULL
  576. || MEDIA_INFO(us).pba_to_lba[zone] == NULL)
  577. alauda_read_map(us, zone);
  578. }
  579. /*
  580. * Erases an entire block
  581. */
  582. static int alauda_erase_block(struct us_data *us, u16 pba)
  583. {
  584. int rc;
  585. unsigned char command[] = {
  586. ALAUDA_BULK_CMD, ALAUDA_BULK_ERASE_BLOCK, PBA_HI(pba),
  587. PBA_ZONE(pba), 0, PBA_LO(pba), 0x02, 0, MEDIA_PORT(us)
  588. };
  589. unsigned char buf[2];
  590. usb_stor_dbg(us, "Erasing PBA %d\n", pba);
  591. rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  592. command, 9, NULL);
  593. if (rc != USB_STOR_XFER_GOOD)
  594. return rc;
  595. rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  596. buf, 2, NULL);
  597. if (rc != USB_STOR_XFER_GOOD)
  598. return rc;
  599. usb_stor_dbg(us, "Erase result: %02X %02X\n", buf[0], buf[1]);
  600. return rc;
  601. }
  602. /*
  603. * Reads data from a certain offset page inside a PBA, including interleaved
  604. * redundancy data. Returns (pagesize+64)*pages bytes in data.
  605. */
  606. static int alauda_read_block_raw(struct us_data *us, u16 pba,
  607. unsigned int page, unsigned int pages, unsigned char *data)
  608. {
  609. int rc;
  610. unsigned char command[] = {
  611. ALAUDA_BULK_CMD, ALAUDA_BULK_READ_BLOCK, PBA_HI(pba),
  612. PBA_ZONE(pba), 0, PBA_LO(pba) + page, pages, 0, MEDIA_PORT(us)
  613. };
  614. usb_stor_dbg(us, "pba %d page %d count %d\n", pba, page, pages);
  615. rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  616. command, 9, NULL);
  617. if (rc != USB_STOR_XFER_GOOD)
  618. return rc;
  619. return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  620. data, (MEDIA_INFO(us).pagesize + 64) * pages, NULL);
  621. }
  622. /*
  623. * Reads data from a certain offset page inside a PBA, excluding redundancy
  624. * data. Returns pagesize*pages bytes in data. Note that data must be big enough
  625. * to hold (pagesize+64)*pages bytes of data, but you can ignore those 'extra'
  626. * trailing bytes outside this function.
  627. */
  628. static int alauda_read_block(struct us_data *us, u16 pba,
  629. unsigned int page, unsigned int pages, unsigned char *data)
  630. {
  631. int i, rc;
  632. unsigned int pagesize = MEDIA_INFO(us).pagesize;
  633. rc = alauda_read_block_raw(us, pba, page, pages, data);
  634. if (rc != USB_STOR_XFER_GOOD)
  635. return rc;
  636. /* Cut out the redundancy data */
  637. for (i = 0; i < pages; i++) {
  638. int dest_offset = i * pagesize;
  639. int src_offset = i * (pagesize + 64);
  640. memmove(data + dest_offset, data + src_offset, pagesize);
  641. }
  642. return rc;
  643. }
  644. /*
  645. * Writes an entire block of data and checks status after write.
  646. * Redundancy data must be already included in data. Data should be
  647. * (pagesize+64)*blocksize bytes in length.
  648. */
  649. static int alauda_write_block(struct us_data *us, u16 pba, unsigned char *data)
  650. {
  651. int rc;
  652. struct alauda_info *info = (struct alauda_info *) us->extra;
  653. unsigned char command[] = {
  654. ALAUDA_BULK_CMD, ALAUDA_BULK_WRITE_BLOCK, PBA_HI(pba),
  655. PBA_ZONE(pba), 0, PBA_LO(pba), 32, 0, MEDIA_PORT(us)
  656. };
  657. usb_stor_dbg(us, "pba %d\n", pba);
  658. rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  659. command, 9, NULL);
  660. if (rc != USB_STOR_XFER_GOOD)
  661. return rc;
  662. rc = usb_stor_bulk_transfer_buf(us, info->wr_ep, data,
  663. (MEDIA_INFO(us).pagesize + 64) * MEDIA_INFO(us).blocksize,
  664. NULL);
  665. if (rc != USB_STOR_XFER_GOOD)
  666. return rc;
  667. return alauda_check_status2(us);
  668. }
  669. /*
  670. * Write some data to a specific LBA.
  671. */
  672. static int alauda_write_lba(struct us_data *us, u16 lba,
  673. unsigned int page, unsigned int pages,
  674. unsigned char *ptr, unsigned char *blockbuffer)
  675. {
  676. u16 pba, lbap, new_pba;
  677. unsigned char *bptr, *cptr, *xptr;
  678. unsigned char ecc[3];
  679. int i, result;
  680. unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
  681. unsigned int zonesize = MEDIA_INFO(us).zonesize;
  682. unsigned int pagesize = MEDIA_INFO(us).pagesize;
  683. unsigned int blocksize = MEDIA_INFO(us).blocksize;
  684. unsigned int lba_offset = lba % uzonesize;
  685. unsigned int new_pba_offset;
  686. unsigned int zone = lba / uzonesize;
  687. alauda_ensure_map_for_zone(us, zone);
  688. pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
  689. if (pba == 1) {
  690. /* Maybe it is impossible to write to PBA 1.
  691. Fake success, but don't do anything. */
  692. printk(KERN_WARNING
  693. "alauda_write_lba: avoid writing to pba 1\n");
  694. return USB_STOR_TRANSPORT_GOOD;
  695. }
  696. new_pba = alauda_find_unused_pba(&MEDIA_INFO(us), zone);
  697. if (!new_pba) {
  698. printk(KERN_WARNING
  699. "alauda_write_lba: Out of unused blocks\n");
  700. return USB_STOR_TRANSPORT_ERROR;
  701. }
  702. /* read old contents */
  703. if (pba != UNDEF) {
  704. result = alauda_read_block_raw(us, pba, 0,
  705. blocksize, blockbuffer);
  706. if (result != USB_STOR_XFER_GOOD)
  707. return result;
  708. } else {
  709. memset(blockbuffer, 0, blocksize * (pagesize + 64));
  710. }
  711. lbap = (lba_offset << 1) | 0x1000;
  712. if (parity[MSB_of(lbap) ^ LSB_of(lbap)])
  713. lbap ^= 1;
  714. /* check old contents and fill lba */
  715. for (i = 0; i < blocksize; i++) {
  716. bptr = blockbuffer + (i * (pagesize + 64));
  717. cptr = bptr + pagesize;
  718. nand_compute_ecc(bptr, ecc);
  719. if (!nand_compare_ecc(cptr+13, ecc)) {
  720. usb_stor_dbg(us, "Warning: bad ecc in page %d- of pba %d\n",
  721. i, pba);
  722. nand_store_ecc(cptr+13, ecc);
  723. }
  724. nand_compute_ecc(bptr + (pagesize / 2), ecc);
  725. if (!nand_compare_ecc(cptr+8, ecc)) {
  726. usb_stor_dbg(us, "Warning: bad ecc in page %d+ of pba %d\n",
  727. i, pba);
  728. nand_store_ecc(cptr+8, ecc);
  729. }
  730. cptr[6] = cptr[11] = MSB_of(lbap);
  731. cptr[7] = cptr[12] = LSB_of(lbap);
  732. }
  733. /* copy in new stuff and compute ECC */
  734. xptr = ptr;
  735. for (i = page; i < page+pages; i++) {
  736. bptr = blockbuffer + (i * (pagesize + 64));
  737. cptr = bptr + pagesize;
  738. memcpy(bptr, xptr, pagesize);
  739. xptr += pagesize;
  740. nand_compute_ecc(bptr, ecc);
  741. nand_store_ecc(cptr+13, ecc);
  742. nand_compute_ecc(bptr + (pagesize / 2), ecc);
  743. nand_store_ecc(cptr+8, ecc);
  744. }
  745. result = alauda_write_block(us, new_pba, blockbuffer);
  746. if (result != USB_STOR_XFER_GOOD)
  747. return result;
  748. new_pba_offset = new_pba - (zone * zonesize);
  749. MEDIA_INFO(us).pba_to_lba[zone][new_pba_offset] = lba;
  750. MEDIA_INFO(us).lba_to_pba[zone][lba_offset] = new_pba;
  751. usb_stor_dbg(us, "Remapped LBA %d to PBA %d\n", lba, new_pba);
  752. if (pba != UNDEF) {
  753. unsigned int pba_offset = pba - (zone * zonesize);
  754. result = alauda_erase_block(us, pba);
  755. if (result != USB_STOR_XFER_GOOD)
  756. return result;
  757. MEDIA_INFO(us).pba_to_lba[zone][pba_offset] = UNDEF;
  758. }
  759. return USB_STOR_TRANSPORT_GOOD;
  760. }
  761. /*
  762. * Read data from a specific sector address
  763. */
  764. static int alauda_read_data(struct us_data *us, unsigned long address,
  765. unsigned int sectors)
  766. {
  767. unsigned char *buffer;
  768. u16 lba, max_lba;
  769. unsigned int page, len, offset;
  770. unsigned int blockshift = MEDIA_INFO(us).blockshift;
  771. unsigned int pageshift = MEDIA_INFO(us).pageshift;
  772. unsigned int blocksize = MEDIA_INFO(us).blocksize;
  773. unsigned int pagesize = MEDIA_INFO(us).pagesize;
  774. unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
  775. struct scatterlist *sg;
  776. int result;
  777. /*
  778. * Since we only read in one block at a time, we have to create
  779. * a bounce buffer and move the data a piece at a time between the
  780. * bounce buffer and the actual transfer buffer.
  781. * We make this buffer big enough to hold temporary redundancy data,
  782. * which we use when reading the data blocks.
  783. */
  784. len = min(sectors, blocksize) * (pagesize + 64);
  785. buffer = kmalloc(len, GFP_NOIO);
  786. if (buffer == NULL) {
  787. printk(KERN_WARNING "alauda_read_data: Out of memory\n");
  788. return USB_STOR_TRANSPORT_ERROR;
  789. }
  790. /* Figure out the initial LBA and page */
  791. lba = address >> blockshift;
  792. page = (address & MEDIA_INFO(us).blockmask);
  793. max_lba = MEDIA_INFO(us).capacity >> (blockshift + pageshift);
  794. result = USB_STOR_TRANSPORT_GOOD;
  795. offset = 0;
  796. sg = NULL;
  797. while (sectors > 0) {
  798. unsigned int zone = lba / uzonesize; /* integer division */
  799. unsigned int lba_offset = lba - (zone * uzonesize);
  800. unsigned int pages;
  801. u16 pba;
  802. alauda_ensure_map_for_zone(us, zone);
  803. /* Not overflowing capacity? */
  804. if (lba >= max_lba) {
  805. usb_stor_dbg(us, "Error: Requested lba %u exceeds maximum %u\n",
  806. lba, max_lba);
  807. result = USB_STOR_TRANSPORT_ERROR;
  808. break;
  809. }
  810. /* Find number of pages we can read in this block */
  811. pages = min(sectors, blocksize - page);
  812. len = pages << pageshift;
  813. /* Find where this lba lives on disk */
  814. pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
  815. if (pba == UNDEF) { /* this lba was never written */
  816. usb_stor_dbg(us, "Read %d zero pages (LBA %d) page %d\n",
  817. pages, lba, page);
  818. /* This is not really an error. It just means
  819. that the block has never been written.
  820. Instead of returning USB_STOR_TRANSPORT_ERROR
  821. it is better to return all zero data. */
  822. memset(buffer, 0, len);
  823. } else {
  824. usb_stor_dbg(us, "Read %d pages, from PBA %d (LBA %d) page %d\n",
  825. pages, pba, lba, page);
  826. result = alauda_read_block(us, pba, page, pages, buffer);
  827. if (result != USB_STOR_TRANSPORT_GOOD)
  828. break;
  829. }
  830. /* Store the data in the transfer buffer */
  831. usb_stor_access_xfer_buf(buffer, len, us->srb,
  832. &sg, &offset, TO_XFER_BUF);
  833. page = 0;
  834. lba++;
  835. sectors -= pages;
  836. }
  837. kfree(buffer);
  838. return result;
  839. }
  840. /*
  841. * Write data to a specific sector address
  842. */
  843. static int alauda_write_data(struct us_data *us, unsigned long address,
  844. unsigned int sectors)
  845. {
  846. unsigned char *buffer, *blockbuffer;
  847. unsigned int page, len, offset;
  848. unsigned int blockshift = MEDIA_INFO(us).blockshift;
  849. unsigned int pageshift = MEDIA_INFO(us).pageshift;
  850. unsigned int blocksize = MEDIA_INFO(us).blocksize;
  851. unsigned int pagesize = MEDIA_INFO(us).pagesize;
  852. struct scatterlist *sg;
  853. u16 lba, max_lba;
  854. int result;
  855. /*
  856. * Since we don't write the user data directly to the device,
  857. * we have to create a bounce buffer and move the data a piece
  858. * at a time between the bounce buffer and the actual transfer buffer.
  859. */
  860. len = min(sectors, blocksize) * pagesize;
  861. buffer = kmalloc(len, GFP_NOIO);
  862. if (buffer == NULL) {
  863. printk(KERN_WARNING "alauda_write_data: Out of memory\n");
  864. return USB_STOR_TRANSPORT_ERROR;
  865. }
  866. /*
  867. * We also need a temporary block buffer, where we read in the old data,
  868. * overwrite parts with the new data, and manipulate the redundancy data
  869. */
  870. blockbuffer = kmalloc((pagesize + 64) * blocksize, GFP_NOIO);
  871. if (blockbuffer == NULL) {
  872. printk(KERN_WARNING "alauda_write_data: Out of memory\n");
  873. kfree(buffer);
  874. return USB_STOR_TRANSPORT_ERROR;
  875. }
  876. /* Figure out the initial LBA and page */
  877. lba = address >> blockshift;
  878. page = (address & MEDIA_INFO(us).blockmask);
  879. max_lba = MEDIA_INFO(us).capacity >> (pageshift + blockshift);
  880. result = USB_STOR_TRANSPORT_GOOD;
  881. offset = 0;
  882. sg = NULL;
  883. while (sectors > 0) {
  884. /* Write as many sectors as possible in this block */
  885. unsigned int pages = min(sectors, blocksize - page);
  886. len = pages << pageshift;
  887. /* Not overflowing capacity? */
  888. if (lba >= max_lba) {
  889. usb_stor_dbg(us, "Requested lba %u exceeds maximum %u\n",
  890. lba, max_lba);
  891. result = USB_STOR_TRANSPORT_ERROR;
  892. break;
  893. }
  894. /* Get the data from the transfer buffer */
  895. usb_stor_access_xfer_buf(buffer, len, us->srb,
  896. &sg, &offset, FROM_XFER_BUF);
  897. result = alauda_write_lba(us, lba, page, pages, buffer,
  898. blockbuffer);
  899. if (result != USB_STOR_TRANSPORT_GOOD)
  900. break;
  901. page = 0;
  902. lba++;
  903. sectors -= pages;
  904. }
  905. kfree(buffer);
  906. kfree(blockbuffer);
  907. return result;
  908. }
  909. /*
  910. * Our interface with the rest of the world
  911. */
  912. static void alauda_info_destructor(void *extra)
  913. {
  914. struct alauda_info *info = (struct alauda_info *) extra;
  915. int port;
  916. if (!info)
  917. return;
  918. for (port = 0; port < 2; port++) {
  919. struct alauda_media_info *media_info = &info->port[port];
  920. alauda_free_maps(media_info);
  921. kfree(media_info->lba_to_pba);
  922. kfree(media_info->pba_to_lba);
  923. }
  924. }
  925. /*
  926. * Initialize alauda_info struct and find the data-write endpoint
  927. */
  928. static int init_alauda(struct us_data *us)
  929. {
  930. struct alauda_info *info;
  931. struct usb_host_interface *altsetting = us->pusb_intf->cur_altsetting;
  932. nand_init_ecc();
  933. us->extra = kzalloc(sizeof(struct alauda_info), GFP_NOIO);
  934. if (!us->extra)
  935. return USB_STOR_TRANSPORT_ERROR;
  936. info = (struct alauda_info *) us->extra;
  937. us->extra_destructor = alauda_info_destructor;
  938. info->wr_ep = usb_sndbulkpipe(us->pusb_dev,
  939. altsetting->endpoint[0].desc.bEndpointAddress
  940. & USB_ENDPOINT_NUMBER_MASK);
  941. return USB_STOR_TRANSPORT_GOOD;
  942. }
  943. static int alauda_transport(struct scsi_cmnd *srb, struct us_data *us)
  944. {
  945. int rc;
  946. struct alauda_info *info = (struct alauda_info *) us->extra;
  947. unsigned char *ptr = us->iobuf;
  948. static unsigned char inquiry_response[36] = {
  949. 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
  950. };
  951. if (srb->cmnd[0] == INQUIRY) {
  952. usb_stor_dbg(us, "INQUIRY - Returning bogus response\n");
  953. memcpy(ptr, inquiry_response, sizeof(inquiry_response));
  954. fill_inquiry_response(us, ptr, 36);
  955. return USB_STOR_TRANSPORT_GOOD;
  956. }
  957. if (srb->cmnd[0] == TEST_UNIT_READY) {
  958. usb_stor_dbg(us, "TEST_UNIT_READY\n");
  959. return alauda_check_media(us);
  960. }
  961. if (srb->cmnd[0] == READ_CAPACITY) {
  962. unsigned int num_zones;
  963. unsigned long capacity;
  964. rc = alauda_check_media(us);
  965. if (rc != USB_STOR_TRANSPORT_GOOD)
  966. return rc;
  967. num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift
  968. + MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift);
  969. capacity = num_zones * MEDIA_INFO(us).uzonesize
  970. * MEDIA_INFO(us).blocksize;
  971. /* Report capacity and page size */
  972. ((__be32 *) ptr)[0] = cpu_to_be32(capacity - 1);
  973. ((__be32 *) ptr)[1] = cpu_to_be32(512);
  974. usb_stor_set_xfer_buf(ptr, 8, srb);
  975. return USB_STOR_TRANSPORT_GOOD;
  976. }
  977. if (srb->cmnd[0] == READ_10) {
  978. unsigned int page, pages;
  979. rc = alauda_check_media(us);
  980. if (rc != USB_STOR_TRANSPORT_GOOD)
  981. return rc;
  982. page = short_pack(srb->cmnd[3], srb->cmnd[2]);
  983. page <<= 16;
  984. page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
  985. pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
  986. usb_stor_dbg(us, "READ_10: page %d pagect %d\n", page, pages);
  987. return alauda_read_data(us, page, pages);
  988. }
  989. if (srb->cmnd[0] == WRITE_10) {
  990. unsigned int page, pages;
  991. rc = alauda_check_media(us);
  992. if (rc != USB_STOR_TRANSPORT_GOOD)
  993. return rc;
  994. page = short_pack(srb->cmnd[3], srb->cmnd[2]);
  995. page <<= 16;
  996. page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
  997. pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
  998. usb_stor_dbg(us, "WRITE_10: page %d pagect %d\n", page, pages);
  999. return alauda_write_data(us, page, pages);
  1000. }
  1001. if (srb->cmnd[0] == REQUEST_SENSE) {
  1002. usb_stor_dbg(us, "REQUEST_SENSE\n");
  1003. memset(ptr, 0, 18);
  1004. ptr[0] = 0xF0;
  1005. ptr[2] = info->sense_key;
  1006. ptr[7] = 11;
  1007. ptr[12] = info->sense_asc;
  1008. ptr[13] = info->sense_ascq;
  1009. usb_stor_set_xfer_buf(ptr, 18, srb);
  1010. return USB_STOR_TRANSPORT_GOOD;
  1011. }
  1012. if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
  1013. /* sure. whatever. not like we can stop the user from popping
  1014. the media out of the device (no locking doors, etc) */
  1015. return USB_STOR_TRANSPORT_GOOD;
  1016. }
  1017. usb_stor_dbg(us, "Gah! Unknown command: %d (0x%x)\n",
  1018. srb->cmnd[0], srb->cmnd[0]);
  1019. info->sense_key = 0x05;
  1020. info->sense_asc = 0x20;
  1021. info->sense_ascq = 0x00;
  1022. return USB_STOR_TRANSPORT_FAILED;
  1023. }
  1024. static int alauda_probe(struct usb_interface *intf,
  1025. const struct usb_device_id *id)
  1026. {
  1027. struct us_data *us;
  1028. int result;
  1029. result = usb_stor_probe1(&us, intf, id,
  1030. (id - alauda_usb_ids) + alauda_unusual_dev_list);
  1031. if (result)
  1032. return result;
  1033. us->transport_name = "Alauda Control/Bulk";
  1034. us->transport = alauda_transport;
  1035. us->transport_reset = usb_stor_Bulk_reset;
  1036. us->max_lun = 1;
  1037. result = usb_stor_probe2(us);
  1038. return result;
  1039. }
  1040. static struct usb_driver alauda_driver = {
  1041. .name = "ums-alauda",
  1042. .probe = alauda_probe,
  1043. .disconnect = usb_stor_disconnect,
  1044. .suspend = usb_stor_suspend,
  1045. .resume = usb_stor_resume,
  1046. .reset_resume = usb_stor_reset_resume,
  1047. .pre_reset = usb_stor_pre_reset,
  1048. .post_reset = usb_stor_post_reset,
  1049. .id_table = alauda_usb_ids,
  1050. .soft_unbind = 1,
  1051. .no_dynamic_id = 1,
  1052. };
  1053. module_usb_driver(alauda_driver);