alauda.c 33 KB

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