alauda.c 32 KB

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