alauda.c 32 KB

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