alauda.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  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: %4ph\n", data);
  345. media_info = alauda_card_find_id(data[1]);
  346. if (media_info == NULL) {
  347. pr_warn("alauda_init_media: Unrecognised media signature: %4ph\n",
  348. data);
  349. return USB_STOR_TRANSPORT_ERROR;
  350. }
  351. MEDIA_INFO(us).capacity = 1 << media_info->chipshift;
  352. usb_stor_dbg(us, "Found media with capacity: %ldMB\n",
  353. MEDIA_INFO(us).capacity >> 20);
  354. MEDIA_INFO(us).pageshift = media_info->pageshift;
  355. MEDIA_INFO(us).blockshift = media_info->blockshift;
  356. MEDIA_INFO(us).zoneshift = media_info->zoneshift;
  357. MEDIA_INFO(us).pagesize = 1 << media_info->pageshift;
  358. MEDIA_INFO(us).blocksize = 1 << media_info->blockshift;
  359. MEDIA_INFO(us).zonesize = 1 << media_info->zoneshift;
  360. MEDIA_INFO(us).uzonesize = ((1 << media_info->zoneshift) / 128) * 125;
  361. MEDIA_INFO(us).blockmask = MEDIA_INFO(us).blocksize - 1;
  362. num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift
  363. + MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift);
  364. MEDIA_INFO(us).pba_to_lba = kcalloc(num_zones, sizeof(u16*), GFP_NOIO);
  365. MEDIA_INFO(us).lba_to_pba = kcalloc(num_zones, sizeof(u16*), GFP_NOIO);
  366. if (alauda_reset_media(us) != USB_STOR_XFER_GOOD)
  367. return USB_STOR_TRANSPORT_ERROR;
  368. return USB_STOR_TRANSPORT_GOOD;
  369. }
  370. /*
  371. * Examines the media status and does the right thing when the media has gone,
  372. * appeared, or changed.
  373. */
  374. static int alauda_check_media(struct us_data *us)
  375. {
  376. struct alauda_info *info = (struct alauda_info *) us->extra;
  377. unsigned char status[2];
  378. int rc;
  379. rc = alauda_get_media_status(us, status);
  380. /* Check for no media or door open */
  381. if ((status[0] & 0x80) || ((status[0] & 0x1F) == 0x10)
  382. || ((status[1] & 0x01) == 0)) {
  383. usb_stor_dbg(us, "No media, or door open\n");
  384. alauda_free_maps(&MEDIA_INFO(us));
  385. info->sense_key = 0x02;
  386. info->sense_asc = 0x3A;
  387. info->sense_ascq = 0x00;
  388. return USB_STOR_TRANSPORT_FAILED;
  389. }
  390. /* Check for media change */
  391. if (status[0] & 0x08) {
  392. usb_stor_dbg(us, "Media change detected\n");
  393. alauda_free_maps(&MEDIA_INFO(us));
  394. alauda_init_media(us);
  395. info->sense_key = UNIT_ATTENTION;
  396. info->sense_asc = 0x28;
  397. info->sense_ascq = 0x00;
  398. return USB_STOR_TRANSPORT_FAILED;
  399. }
  400. return USB_STOR_TRANSPORT_GOOD;
  401. }
  402. /*
  403. * Checks the status from the 2nd status register
  404. * Returns 3 bytes of status data, only the first is known
  405. */
  406. static int alauda_check_status2(struct us_data *us)
  407. {
  408. int rc;
  409. unsigned char command[] = {
  410. ALAUDA_BULK_CMD, ALAUDA_BULK_GET_STATUS2,
  411. 0, 0, 0, 0, 3, 0, MEDIA_PORT(us)
  412. };
  413. unsigned char data[3];
  414. rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  415. command, 9, NULL);
  416. if (rc != USB_STOR_XFER_GOOD)
  417. return rc;
  418. rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  419. data, 3, NULL);
  420. if (rc != USB_STOR_XFER_GOOD)
  421. return rc;
  422. usb_stor_dbg(us, "%3ph\n", data);
  423. if (data[0] & ALAUDA_STATUS_ERROR)
  424. return USB_STOR_XFER_ERROR;
  425. return USB_STOR_XFER_GOOD;
  426. }
  427. /*
  428. * Gets the redundancy data for the first page of a PBA
  429. * Returns 16 bytes.
  430. */
  431. static int alauda_get_redu_data(struct us_data *us, u16 pba, unsigned char *data)
  432. {
  433. int rc;
  434. unsigned char command[] = {
  435. ALAUDA_BULK_CMD, ALAUDA_BULK_GET_REDU_DATA,
  436. PBA_HI(pba), PBA_ZONE(pba), 0, PBA_LO(pba), 0, 0, MEDIA_PORT(us)
  437. };
  438. rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  439. command, 9, NULL);
  440. if (rc != USB_STOR_XFER_GOOD)
  441. return rc;
  442. return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  443. data, 16, NULL);
  444. }
  445. /*
  446. * Finds the first unused PBA in a zone
  447. * Returns the absolute PBA of an unused PBA, or 0 if none found.
  448. */
  449. static u16 alauda_find_unused_pba(struct alauda_media_info *info,
  450. unsigned int zone)
  451. {
  452. u16 *pba_to_lba = info->pba_to_lba[zone];
  453. unsigned int i;
  454. for (i = 0; i < info->zonesize; i++)
  455. if (pba_to_lba[i] == UNDEF)
  456. return (zone << info->zoneshift) + i;
  457. return 0;
  458. }
  459. /*
  460. * Reads the redundancy data for all PBA's in a zone
  461. * Produces lba <--> pba mappings
  462. */
  463. static int alauda_read_map(struct us_data *us, unsigned int zone)
  464. {
  465. unsigned char *data = us->iobuf;
  466. int result;
  467. int i, j;
  468. unsigned int zonesize = MEDIA_INFO(us).zonesize;
  469. unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
  470. unsigned int lba_offset, lba_real, blocknum;
  471. unsigned int zone_base_lba = zone * uzonesize;
  472. unsigned int zone_base_pba = zone * zonesize;
  473. u16 *lba_to_pba = kcalloc(zonesize, sizeof(u16), GFP_NOIO);
  474. u16 *pba_to_lba = kcalloc(zonesize, sizeof(u16), GFP_NOIO);
  475. if (lba_to_pba == NULL || pba_to_lba == NULL) {
  476. result = USB_STOR_TRANSPORT_ERROR;
  477. goto error;
  478. }
  479. usb_stor_dbg(us, "Mapping blocks for zone %d\n", zone);
  480. /* 1024 PBA's per zone */
  481. for (i = 0; i < zonesize; i++)
  482. lba_to_pba[i] = pba_to_lba[i] = UNDEF;
  483. for (i = 0; i < zonesize; i++) {
  484. blocknum = zone_base_pba + i;
  485. result = alauda_get_redu_data(us, blocknum, data);
  486. if (result != USB_STOR_XFER_GOOD) {
  487. result = USB_STOR_TRANSPORT_ERROR;
  488. goto error;
  489. }
  490. /* special PBAs have control field 0^16 */
  491. for (j = 0; j < 16; j++)
  492. if (data[j] != 0)
  493. goto nonz;
  494. pba_to_lba[i] = UNUSABLE;
  495. usb_stor_dbg(us, "PBA %d has no logical mapping\n", blocknum);
  496. continue;
  497. nonz:
  498. /* unwritten PBAs have control field FF^16 */
  499. for (j = 0; j < 16; j++)
  500. if (data[j] != 0xff)
  501. goto nonff;
  502. continue;
  503. nonff:
  504. /* normal PBAs start with six FFs */
  505. if (j < 6) {
  506. usb_stor_dbg(us, "PBA %d has no logical mapping: reserved area = %02X%02X%02X%02X data status %02X block status %02X\n",
  507. blocknum,
  508. data[0], data[1], data[2], data[3],
  509. data[4], data[5]);
  510. pba_to_lba[i] = UNUSABLE;
  511. continue;
  512. }
  513. if ((data[6] >> 4) != 0x01) {
  514. usb_stor_dbg(us, "PBA %d has invalid address field %02X%02X/%02X%02X\n",
  515. blocknum, data[6], data[7],
  516. data[11], data[12]);
  517. pba_to_lba[i] = UNUSABLE;
  518. continue;
  519. }
  520. /* check even parity */
  521. if (parity[data[6] ^ data[7]]) {
  522. printk(KERN_WARNING
  523. "alauda_read_map: Bad parity in LBA for block %d"
  524. " (%02X %02X)\n", i, data[6], data[7]);
  525. pba_to_lba[i] = UNUSABLE;
  526. continue;
  527. }
  528. lba_offset = short_pack(data[7], data[6]);
  529. lba_offset = (lba_offset & 0x07FF) >> 1;
  530. lba_real = lba_offset + zone_base_lba;
  531. /*
  532. * Every 1024 physical blocks ("zone"), the LBA numbers
  533. * go back to zero, but are within a higher block of LBA's.
  534. * Also, there is a maximum of 1000 LBA's per zone.
  535. * In other words, in PBA 1024-2047 you will find LBA 0-999
  536. * which are really LBA 1000-1999. This allows for 24 bad
  537. * or special physical blocks per zone.
  538. */
  539. if (lba_offset >= uzonesize) {
  540. printk(KERN_WARNING
  541. "alauda_read_map: Bad low LBA %d for block %d\n",
  542. lba_real, blocknum);
  543. continue;
  544. }
  545. if (lba_to_pba[lba_offset] != UNDEF) {
  546. printk(KERN_WARNING
  547. "alauda_read_map: "
  548. "LBA %d seen for PBA %d and %d\n",
  549. lba_real, lba_to_pba[lba_offset], blocknum);
  550. continue;
  551. }
  552. pba_to_lba[i] = lba_real;
  553. lba_to_pba[lba_offset] = blocknum;
  554. continue;
  555. }
  556. MEDIA_INFO(us).lba_to_pba[zone] = lba_to_pba;
  557. MEDIA_INFO(us).pba_to_lba[zone] = pba_to_lba;
  558. result = 0;
  559. goto out;
  560. error:
  561. kfree(lba_to_pba);
  562. kfree(pba_to_lba);
  563. out:
  564. return result;
  565. }
  566. /*
  567. * Checks to see whether we have already mapped a certain zone
  568. * If we haven't, the map is generated
  569. */
  570. static void alauda_ensure_map_for_zone(struct us_data *us, unsigned int zone)
  571. {
  572. if (MEDIA_INFO(us).lba_to_pba[zone] == NULL
  573. || MEDIA_INFO(us).pba_to_lba[zone] == NULL)
  574. alauda_read_map(us, zone);
  575. }
  576. /*
  577. * Erases an entire block
  578. */
  579. static int alauda_erase_block(struct us_data *us, u16 pba)
  580. {
  581. int rc;
  582. unsigned char command[] = {
  583. ALAUDA_BULK_CMD, ALAUDA_BULK_ERASE_BLOCK, PBA_HI(pba),
  584. PBA_ZONE(pba), 0, PBA_LO(pba), 0x02, 0, MEDIA_PORT(us)
  585. };
  586. unsigned char buf[2];
  587. usb_stor_dbg(us, "Erasing PBA %d\n", pba);
  588. rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  589. command, 9, NULL);
  590. if (rc != USB_STOR_XFER_GOOD)
  591. return rc;
  592. rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  593. buf, 2, NULL);
  594. if (rc != USB_STOR_XFER_GOOD)
  595. return rc;
  596. usb_stor_dbg(us, "Erase result: %02X %02X\n", buf[0], buf[1]);
  597. return rc;
  598. }
  599. /*
  600. * Reads data from a certain offset page inside a PBA, including interleaved
  601. * redundancy data. Returns (pagesize+64)*pages bytes in data.
  602. */
  603. static int alauda_read_block_raw(struct us_data *us, u16 pba,
  604. unsigned int page, unsigned int pages, unsigned char *data)
  605. {
  606. int rc;
  607. unsigned char command[] = {
  608. ALAUDA_BULK_CMD, ALAUDA_BULK_READ_BLOCK, PBA_HI(pba),
  609. PBA_ZONE(pba), 0, PBA_LO(pba) + page, pages, 0, MEDIA_PORT(us)
  610. };
  611. usb_stor_dbg(us, "pba %d page %d count %d\n", pba, page, pages);
  612. rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  613. command, 9, NULL);
  614. if (rc != USB_STOR_XFER_GOOD)
  615. return rc;
  616. return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  617. data, (MEDIA_INFO(us).pagesize + 64) * pages, NULL);
  618. }
  619. /*
  620. * Reads data from a certain offset page inside a PBA, excluding redundancy
  621. * data. Returns pagesize*pages bytes in data. Note that data must be big enough
  622. * to hold (pagesize+64)*pages bytes of data, but you can ignore those 'extra'
  623. * trailing bytes outside this function.
  624. */
  625. static int alauda_read_block(struct us_data *us, u16 pba,
  626. unsigned int page, unsigned int pages, unsigned char *data)
  627. {
  628. int i, rc;
  629. unsigned int pagesize = MEDIA_INFO(us).pagesize;
  630. rc = alauda_read_block_raw(us, pba, page, pages, data);
  631. if (rc != USB_STOR_XFER_GOOD)
  632. return rc;
  633. /* Cut out the redundancy data */
  634. for (i = 0; i < pages; i++) {
  635. int dest_offset = i * pagesize;
  636. int src_offset = i * (pagesize + 64);
  637. memmove(data + dest_offset, data + src_offset, pagesize);
  638. }
  639. return rc;
  640. }
  641. /*
  642. * Writes an entire block of data and checks status after write.
  643. * Redundancy data must be already included in data. Data should be
  644. * (pagesize+64)*blocksize bytes in length.
  645. */
  646. static int alauda_write_block(struct us_data *us, u16 pba, unsigned char *data)
  647. {
  648. int rc;
  649. struct alauda_info *info = (struct alauda_info *) us->extra;
  650. unsigned char command[] = {
  651. ALAUDA_BULK_CMD, ALAUDA_BULK_WRITE_BLOCK, PBA_HI(pba),
  652. PBA_ZONE(pba), 0, PBA_LO(pba), 32, 0, MEDIA_PORT(us)
  653. };
  654. usb_stor_dbg(us, "pba %d\n", pba);
  655. rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  656. command, 9, NULL);
  657. if (rc != USB_STOR_XFER_GOOD)
  658. return rc;
  659. rc = usb_stor_bulk_transfer_buf(us, info->wr_ep, data,
  660. (MEDIA_INFO(us).pagesize + 64) * MEDIA_INFO(us).blocksize,
  661. NULL);
  662. if (rc != USB_STOR_XFER_GOOD)
  663. return rc;
  664. return alauda_check_status2(us);
  665. }
  666. /*
  667. * Write some data to a specific LBA.
  668. */
  669. static int alauda_write_lba(struct us_data *us, u16 lba,
  670. unsigned int page, unsigned int pages,
  671. unsigned char *ptr, unsigned char *blockbuffer)
  672. {
  673. u16 pba, lbap, new_pba;
  674. unsigned char *bptr, *cptr, *xptr;
  675. unsigned char ecc[3];
  676. int i, result;
  677. unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
  678. unsigned int zonesize = MEDIA_INFO(us).zonesize;
  679. unsigned int pagesize = MEDIA_INFO(us).pagesize;
  680. unsigned int blocksize = MEDIA_INFO(us).blocksize;
  681. unsigned int lba_offset = lba % uzonesize;
  682. unsigned int new_pba_offset;
  683. unsigned int zone = lba / uzonesize;
  684. alauda_ensure_map_for_zone(us, zone);
  685. pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
  686. if (pba == 1) {
  687. /* Maybe it is impossible to write to PBA 1.
  688. Fake success, but don't do anything. */
  689. printk(KERN_WARNING
  690. "alauda_write_lba: avoid writing to pba 1\n");
  691. return USB_STOR_TRANSPORT_GOOD;
  692. }
  693. new_pba = alauda_find_unused_pba(&MEDIA_INFO(us), zone);
  694. if (!new_pba) {
  695. printk(KERN_WARNING
  696. "alauda_write_lba: Out of unused blocks\n");
  697. return USB_STOR_TRANSPORT_ERROR;
  698. }
  699. /* read old contents */
  700. if (pba != UNDEF) {
  701. result = alauda_read_block_raw(us, pba, 0,
  702. blocksize, blockbuffer);
  703. if (result != USB_STOR_XFER_GOOD)
  704. return result;
  705. } else {
  706. memset(blockbuffer, 0, blocksize * (pagesize + 64));
  707. }
  708. lbap = (lba_offset << 1) | 0x1000;
  709. if (parity[MSB_of(lbap) ^ LSB_of(lbap)])
  710. lbap ^= 1;
  711. /* check old contents and fill lba */
  712. for (i = 0; i < blocksize; i++) {
  713. bptr = blockbuffer + (i * (pagesize + 64));
  714. cptr = bptr + pagesize;
  715. nand_compute_ecc(bptr, ecc);
  716. if (!nand_compare_ecc(cptr+13, ecc)) {
  717. usb_stor_dbg(us, "Warning: bad ecc in page %d- of pba %d\n",
  718. i, pba);
  719. nand_store_ecc(cptr+13, ecc);
  720. }
  721. nand_compute_ecc(bptr + (pagesize / 2), ecc);
  722. if (!nand_compare_ecc(cptr+8, ecc)) {
  723. usb_stor_dbg(us, "Warning: bad ecc in page %d+ of pba %d\n",
  724. i, pba);
  725. nand_store_ecc(cptr+8, ecc);
  726. }
  727. cptr[6] = cptr[11] = MSB_of(lbap);
  728. cptr[7] = cptr[12] = LSB_of(lbap);
  729. }
  730. /* copy in new stuff and compute ECC */
  731. xptr = ptr;
  732. for (i = page; i < page+pages; i++) {
  733. bptr = blockbuffer + (i * (pagesize + 64));
  734. cptr = bptr + pagesize;
  735. memcpy(bptr, xptr, pagesize);
  736. xptr += pagesize;
  737. nand_compute_ecc(bptr, ecc);
  738. nand_store_ecc(cptr+13, ecc);
  739. nand_compute_ecc(bptr + (pagesize / 2), ecc);
  740. nand_store_ecc(cptr+8, ecc);
  741. }
  742. result = alauda_write_block(us, new_pba, blockbuffer);
  743. if (result != USB_STOR_XFER_GOOD)
  744. return result;
  745. new_pba_offset = new_pba - (zone * zonesize);
  746. MEDIA_INFO(us).pba_to_lba[zone][new_pba_offset] = lba;
  747. MEDIA_INFO(us).lba_to_pba[zone][lba_offset] = new_pba;
  748. usb_stor_dbg(us, "Remapped LBA %d to PBA %d\n", lba, new_pba);
  749. if (pba != UNDEF) {
  750. unsigned int pba_offset = pba - (zone * zonesize);
  751. result = alauda_erase_block(us, pba);
  752. if (result != USB_STOR_XFER_GOOD)
  753. return result;
  754. MEDIA_INFO(us).pba_to_lba[zone][pba_offset] = UNDEF;
  755. }
  756. return USB_STOR_TRANSPORT_GOOD;
  757. }
  758. /*
  759. * Read data from a specific sector address
  760. */
  761. static int alauda_read_data(struct us_data *us, unsigned long address,
  762. unsigned int sectors)
  763. {
  764. unsigned char *buffer;
  765. u16 lba, max_lba;
  766. unsigned int page, len, offset;
  767. unsigned int blockshift = MEDIA_INFO(us).blockshift;
  768. unsigned int pageshift = MEDIA_INFO(us).pageshift;
  769. unsigned int blocksize = MEDIA_INFO(us).blocksize;
  770. unsigned int pagesize = MEDIA_INFO(us).pagesize;
  771. unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
  772. struct scatterlist *sg;
  773. int result;
  774. /*
  775. * Since we only read in one block at a time, we have to create
  776. * a bounce buffer and move the data a piece at a time between the
  777. * bounce buffer and the actual transfer buffer.
  778. * We make this buffer big enough to hold temporary redundancy data,
  779. * which we use when reading the data blocks.
  780. */
  781. len = min(sectors, blocksize) * (pagesize + 64);
  782. buffer = kmalloc(len, GFP_NOIO);
  783. if (buffer == NULL) {
  784. printk(KERN_WARNING "alauda_read_data: Out of memory\n");
  785. return USB_STOR_TRANSPORT_ERROR;
  786. }
  787. /* Figure out the initial LBA and page */
  788. lba = address >> blockshift;
  789. page = (address & MEDIA_INFO(us).blockmask);
  790. max_lba = MEDIA_INFO(us).capacity >> (blockshift + pageshift);
  791. result = USB_STOR_TRANSPORT_GOOD;
  792. offset = 0;
  793. sg = NULL;
  794. while (sectors > 0) {
  795. unsigned int zone = lba / uzonesize; /* integer division */
  796. unsigned int lba_offset = lba - (zone * uzonesize);
  797. unsigned int pages;
  798. u16 pba;
  799. alauda_ensure_map_for_zone(us, zone);
  800. /* Not overflowing capacity? */
  801. if (lba >= max_lba) {
  802. usb_stor_dbg(us, "Error: Requested lba %u exceeds maximum %u\n",
  803. lba, max_lba);
  804. result = USB_STOR_TRANSPORT_ERROR;
  805. break;
  806. }
  807. /* Find number of pages we can read in this block */
  808. pages = min(sectors, blocksize - page);
  809. len = pages << pageshift;
  810. /* Find where this lba lives on disk */
  811. pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
  812. if (pba == UNDEF) { /* this lba was never written */
  813. usb_stor_dbg(us, "Read %d zero pages (LBA %d) page %d\n",
  814. pages, lba, page);
  815. /* This is not really an error. It just means
  816. that the block has never been written.
  817. Instead of returning USB_STOR_TRANSPORT_ERROR
  818. it is better to return all zero data. */
  819. memset(buffer, 0, len);
  820. } else {
  821. usb_stor_dbg(us, "Read %d pages, from PBA %d (LBA %d) page %d\n",
  822. pages, pba, lba, page);
  823. result = alauda_read_block(us, pba, page, pages, buffer);
  824. if (result != USB_STOR_TRANSPORT_GOOD)
  825. break;
  826. }
  827. /* Store the data in the transfer buffer */
  828. usb_stor_access_xfer_buf(buffer, len, us->srb,
  829. &sg, &offset, TO_XFER_BUF);
  830. page = 0;
  831. lba++;
  832. sectors -= pages;
  833. }
  834. kfree(buffer);
  835. return result;
  836. }
  837. /*
  838. * Write data to a specific sector address
  839. */
  840. static int alauda_write_data(struct us_data *us, unsigned long address,
  841. unsigned int sectors)
  842. {
  843. unsigned char *buffer, *blockbuffer;
  844. unsigned int page, len, offset;
  845. unsigned int blockshift = MEDIA_INFO(us).blockshift;
  846. unsigned int pageshift = MEDIA_INFO(us).pageshift;
  847. unsigned int blocksize = MEDIA_INFO(us).blocksize;
  848. unsigned int pagesize = MEDIA_INFO(us).pagesize;
  849. struct scatterlist *sg;
  850. u16 lba, max_lba;
  851. int result;
  852. /*
  853. * Since we don't write the user data directly to the device,
  854. * we have to create a bounce buffer and move the data a piece
  855. * at a time between the bounce buffer and the actual transfer buffer.
  856. */
  857. len = min(sectors, blocksize) * pagesize;
  858. buffer = kmalloc(len, GFP_NOIO);
  859. if (buffer == NULL) {
  860. printk(KERN_WARNING "alauda_write_data: Out of memory\n");
  861. return USB_STOR_TRANSPORT_ERROR;
  862. }
  863. /*
  864. * We also need a temporary block buffer, where we read in the old data,
  865. * overwrite parts with the new data, and manipulate the redundancy data
  866. */
  867. blockbuffer = kmalloc((pagesize + 64) * blocksize, GFP_NOIO);
  868. if (blockbuffer == NULL) {
  869. printk(KERN_WARNING "alauda_write_data: Out of memory\n");
  870. kfree(buffer);
  871. return USB_STOR_TRANSPORT_ERROR;
  872. }
  873. /* Figure out the initial LBA and page */
  874. lba = address >> blockshift;
  875. page = (address & MEDIA_INFO(us).blockmask);
  876. max_lba = MEDIA_INFO(us).capacity >> (pageshift + blockshift);
  877. result = USB_STOR_TRANSPORT_GOOD;
  878. offset = 0;
  879. sg = NULL;
  880. while (sectors > 0) {
  881. /* Write as many sectors as possible in this block */
  882. unsigned int pages = min(sectors, blocksize - page);
  883. len = pages << pageshift;
  884. /* Not overflowing capacity? */
  885. if (lba >= max_lba) {
  886. usb_stor_dbg(us, "Requested lba %u exceeds maximum %u\n",
  887. lba, max_lba);
  888. result = USB_STOR_TRANSPORT_ERROR;
  889. break;
  890. }
  891. /* Get the data from the transfer buffer */
  892. usb_stor_access_xfer_buf(buffer, len, us->srb,
  893. &sg, &offset, FROM_XFER_BUF);
  894. result = alauda_write_lba(us, lba, page, pages, buffer,
  895. blockbuffer);
  896. if (result != USB_STOR_TRANSPORT_GOOD)
  897. break;
  898. page = 0;
  899. lba++;
  900. sectors -= pages;
  901. }
  902. kfree(buffer);
  903. kfree(blockbuffer);
  904. return result;
  905. }
  906. /*
  907. * Our interface with the rest of the world
  908. */
  909. static void alauda_info_destructor(void *extra)
  910. {
  911. struct alauda_info *info = (struct alauda_info *) extra;
  912. int port;
  913. if (!info)
  914. return;
  915. for (port = 0; port < 2; port++) {
  916. struct alauda_media_info *media_info = &info->port[port];
  917. alauda_free_maps(media_info);
  918. kfree(media_info->lba_to_pba);
  919. kfree(media_info->pba_to_lba);
  920. }
  921. }
  922. /*
  923. * Initialize alauda_info struct and find the data-write endpoint
  924. */
  925. static int init_alauda(struct us_data *us)
  926. {
  927. struct alauda_info *info;
  928. struct usb_host_interface *altsetting = us->pusb_intf->cur_altsetting;
  929. nand_init_ecc();
  930. us->extra = kzalloc(sizeof(struct alauda_info), GFP_NOIO);
  931. if (!us->extra)
  932. return USB_STOR_TRANSPORT_ERROR;
  933. info = (struct alauda_info *) us->extra;
  934. us->extra_destructor = alauda_info_destructor;
  935. info->wr_ep = usb_sndbulkpipe(us->pusb_dev,
  936. altsetting->endpoint[0].desc.bEndpointAddress
  937. & USB_ENDPOINT_NUMBER_MASK);
  938. return USB_STOR_TRANSPORT_GOOD;
  939. }
  940. static int alauda_transport(struct scsi_cmnd *srb, struct us_data *us)
  941. {
  942. int rc;
  943. struct alauda_info *info = (struct alauda_info *) us->extra;
  944. unsigned char *ptr = us->iobuf;
  945. static unsigned char inquiry_response[36] = {
  946. 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
  947. };
  948. if (srb->cmnd[0] == INQUIRY) {
  949. usb_stor_dbg(us, "INQUIRY - Returning bogus response\n");
  950. memcpy(ptr, inquiry_response, sizeof(inquiry_response));
  951. fill_inquiry_response(us, ptr, 36);
  952. return USB_STOR_TRANSPORT_GOOD;
  953. }
  954. if (srb->cmnd[0] == TEST_UNIT_READY) {
  955. usb_stor_dbg(us, "TEST_UNIT_READY\n");
  956. return alauda_check_media(us);
  957. }
  958. if (srb->cmnd[0] == READ_CAPACITY) {
  959. unsigned int num_zones;
  960. unsigned long capacity;
  961. rc = alauda_check_media(us);
  962. if (rc != USB_STOR_TRANSPORT_GOOD)
  963. return rc;
  964. num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift
  965. + MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift);
  966. capacity = num_zones * MEDIA_INFO(us).uzonesize
  967. * MEDIA_INFO(us).blocksize;
  968. /* Report capacity and page size */
  969. ((__be32 *) ptr)[0] = cpu_to_be32(capacity - 1);
  970. ((__be32 *) ptr)[1] = cpu_to_be32(512);
  971. usb_stor_set_xfer_buf(ptr, 8, srb);
  972. return USB_STOR_TRANSPORT_GOOD;
  973. }
  974. if (srb->cmnd[0] == READ_10) {
  975. unsigned int page, pages;
  976. rc = alauda_check_media(us);
  977. if (rc != USB_STOR_TRANSPORT_GOOD)
  978. return rc;
  979. page = short_pack(srb->cmnd[3], srb->cmnd[2]);
  980. page <<= 16;
  981. page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
  982. pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
  983. usb_stor_dbg(us, "READ_10: page %d pagect %d\n", page, pages);
  984. return alauda_read_data(us, page, pages);
  985. }
  986. if (srb->cmnd[0] == WRITE_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. usb_stor_dbg(us, "WRITE_10: page %d pagect %d\n", page, pages);
  996. return alauda_write_data(us, page, pages);
  997. }
  998. if (srb->cmnd[0] == REQUEST_SENSE) {
  999. usb_stor_dbg(us, "REQUEST_SENSE\n");
  1000. memset(ptr, 0, 18);
  1001. ptr[0] = 0xF0;
  1002. ptr[2] = info->sense_key;
  1003. ptr[7] = 11;
  1004. ptr[12] = info->sense_asc;
  1005. ptr[13] = info->sense_ascq;
  1006. usb_stor_set_xfer_buf(ptr, 18, srb);
  1007. return USB_STOR_TRANSPORT_GOOD;
  1008. }
  1009. if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
  1010. /* sure. whatever. not like we can stop the user from popping
  1011. the media out of the device (no locking doors, etc) */
  1012. return USB_STOR_TRANSPORT_GOOD;
  1013. }
  1014. usb_stor_dbg(us, "Gah! Unknown command: %d (0x%x)\n",
  1015. srb->cmnd[0], srb->cmnd[0]);
  1016. info->sense_key = 0x05;
  1017. info->sense_asc = 0x20;
  1018. info->sense_ascq = 0x00;
  1019. return USB_STOR_TRANSPORT_FAILED;
  1020. }
  1021. static int alauda_probe(struct usb_interface *intf,
  1022. const struct usb_device_id *id)
  1023. {
  1024. struct us_data *us;
  1025. int result;
  1026. result = usb_stor_probe1(&us, intf, id,
  1027. (id - alauda_usb_ids) + alauda_unusual_dev_list);
  1028. if (result)
  1029. return result;
  1030. us->transport_name = "Alauda Control/Bulk";
  1031. us->transport = alauda_transport;
  1032. us->transport_reset = usb_stor_Bulk_reset;
  1033. us->max_lun = 1;
  1034. result = usb_stor_probe2(us);
  1035. return result;
  1036. }
  1037. static struct usb_driver alauda_driver = {
  1038. .name = "ums-alauda",
  1039. .probe = alauda_probe,
  1040. .disconnect = usb_stor_disconnect,
  1041. .suspend = usb_stor_suspend,
  1042. .resume = usb_stor_resume,
  1043. .reset_resume = usb_stor_reset_resume,
  1044. .pre_reset = usb_stor_pre_reset,
  1045. .post_reset = usb_stor_post_reset,
  1046. .id_table = alauda_usb_ids,
  1047. .soft_unbind = 1,
  1048. .no_dynamic_id = 1,
  1049. };
  1050. module_usb_driver(alauda_driver);