alauda.c 34 KB

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