alauda.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271
  1. /*
  2. * Driver for Alauda-based card readers
  3. *
  4. * Current development and maintenance by:
  5. * (c) 2005 Daniel Drake <dsd@gentoo.org>
  6. *
  7. * The 'Alauda' is a chip manufacturered by RATOC for OEM use.
  8. *
  9. * Alauda implements a vendor-specific command set to access two media reader
  10. * ports (XD, SmartMedia). This driver converts SCSI commands to the commands
  11. * which are accepted by these devices.
  12. *
  13. * The driver was developed through reverse-engineering, with the help of the
  14. * sddr09 driver which has many similarities, and with some help from the
  15. * (very old) vendor-supplied GPL sma03 driver.
  16. *
  17. * For protocol info, see http://alauda.sourceforge.net
  18. *
  19. * This program is free software; you can redistribute it and/or modify it
  20. * under the terms of the GNU General Public License as published by the
  21. * Free Software Foundation; either version 2, or (at your option) any
  22. * later version.
  23. *
  24. * This program is distributed in the hope that it will be useful, but
  25. * WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. * General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License along
  30. * with this program; if not, write to the Free Software Foundation, Inc.,
  31. * 675 Mass Ave, Cambridge, MA 02139, USA.
  32. */
  33. #include <linux/module.h>
  34. #include <linux/slab.h>
  35. #include <scsi/scsi.h>
  36. #include <scsi/scsi_cmnd.h>
  37. #include <scsi/scsi_device.h>
  38. #include "usb.h"
  39. #include "transport.h"
  40. #include "protocol.h"
  41. #include "debug.h"
  42. MODULE_DESCRIPTION("Driver for Alauda-based card readers");
  43. MODULE_AUTHOR("Daniel Drake <dsd@gentoo.org>");
  44. MODULE_LICENSE("GPL");
  45. /*
  46. * Status bytes
  47. */
  48. #define ALAUDA_STATUS_ERROR 0x01
  49. #define ALAUDA_STATUS_READY 0x40
  50. /*
  51. * Control opcodes (for request field)
  52. */
  53. #define ALAUDA_GET_XD_MEDIA_STATUS 0x08
  54. #define ALAUDA_GET_SM_MEDIA_STATUS 0x98
  55. #define ALAUDA_ACK_XD_MEDIA_CHANGE 0x0a
  56. #define ALAUDA_ACK_SM_MEDIA_CHANGE 0x9a
  57. #define ALAUDA_GET_XD_MEDIA_SIG 0x86
  58. #define ALAUDA_GET_SM_MEDIA_SIG 0x96
  59. /*
  60. * Bulk command identity (byte 0)
  61. */
  62. #define ALAUDA_BULK_CMD 0x40
  63. /*
  64. * Bulk opcodes (byte 1)
  65. */
  66. #define ALAUDA_BULK_GET_REDU_DATA 0x85
  67. #define ALAUDA_BULK_READ_BLOCK 0x94
  68. #define ALAUDA_BULK_ERASE_BLOCK 0xa3
  69. #define ALAUDA_BULK_WRITE_BLOCK 0xb4
  70. #define ALAUDA_BULK_GET_STATUS2 0xb7
  71. #define ALAUDA_BULK_RESET_MEDIA 0xe0
  72. /*
  73. * Port to operate on (byte 8)
  74. */
  75. #define ALAUDA_PORT_XD 0x00
  76. #define ALAUDA_PORT_SM 0x01
  77. /*
  78. * LBA and PBA are unsigned ints. Special values.
  79. */
  80. #define UNDEF 0xffff
  81. #define SPARE 0xfffe
  82. #define UNUSABLE 0xfffd
  83. struct alauda_media_info {
  84. unsigned long capacity; /* total media size in bytes */
  85. unsigned int pagesize; /* page size in bytes */
  86. unsigned int blocksize; /* number of pages per block */
  87. unsigned int uzonesize; /* number of usable blocks per zone */
  88. unsigned int zonesize; /* number of blocks per zone */
  89. unsigned int blockmask; /* mask to get page from address */
  90. unsigned char pageshift;
  91. unsigned char blockshift;
  92. unsigned char zoneshift;
  93. u16 **lba_to_pba; /* logical to physical block map */
  94. u16 **pba_to_lba; /* physical to logical block map */
  95. };
  96. struct alauda_info {
  97. struct alauda_media_info port[2];
  98. int wr_ep; /* endpoint to write data out of */
  99. unsigned char sense_key;
  100. unsigned long sense_asc; /* additional sense code */
  101. unsigned long sense_ascq; /* additional sense code qualifier */
  102. };
  103. #define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) )
  104. #define LSB_of(s) ((s)&0xFF)
  105. #define MSB_of(s) ((s)>>8)
  106. #define MEDIA_PORT(us) us->srb->device->lun
  107. #define MEDIA_INFO(us) ((struct alauda_info *)us->extra)->port[MEDIA_PORT(us)]
  108. #define PBA_LO(pba) ((pba & 0xF) << 5)
  109. #define PBA_HI(pba) (pba >> 3)
  110. #define PBA_ZONE(pba) (pba >> 11)
  111. static int init_alauda(struct us_data *us);
  112. /*
  113. * The table of devices
  114. */
  115. #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
  116. vendorName, productName, useProtocol, useTransport, \
  117. initFunction, flags) \
  118. { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
  119. .driver_info = (flags) }
  120. static struct usb_device_id alauda_usb_ids[] = {
  121. # include "unusual_alauda.h"
  122. { } /* Terminating entry */
  123. };
  124. MODULE_DEVICE_TABLE(usb, alauda_usb_ids);
  125. #undef UNUSUAL_DEV
  126. /*
  127. * The flags table
  128. */
  129. #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
  130. vendor_name, product_name, use_protocol, use_transport, \
  131. init_function, Flags) \
  132. { \
  133. .vendorName = vendor_name, \
  134. .productName = product_name, \
  135. .useProtocol = use_protocol, \
  136. .useTransport = use_transport, \
  137. .initFunction = init_function, \
  138. }
  139. static struct us_unusual_dev alauda_unusual_dev_list[] = {
  140. # include "unusual_alauda.h"
  141. { } /* Terminating entry */
  142. };
  143. #undef UNUSUAL_DEV
  144. /*
  145. * Media handling
  146. */
  147. struct alauda_card_info {
  148. unsigned char id; /* id byte */
  149. unsigned char chipshift; /* 1<<cs bytes total capacity */
  150. unsigned char pageshift; /* 1<<ps bytes in a page */
  151. unsigned char blockshift; /* 1<<bs pages per block */
  152. unsigned char zoneshift; /* 1<<zs blocks per zone */
  153. };
  154. static struct alauda_card_info alauda_card_ids[] = {
  155. /* NAND flash */
  156. { 0x6e, 20, 8, 4, 8}, /* 1 MB */
  157. { 0xe8, 20, 8, 4, 8}, /* 1 MB */
  158. { 0xec, 20, 8, 4, 8}, /* 1 MB */
  159. { 0x64, 21, 8, 4, 9}, /* 2 MB */
  160. { 0xea, 21, 8, 4, 9}, /* 2 MB */
  161. { 0x6b, 22, 9, 4, 9}, /* 4 MB */
  162. { 0xe3, 22, 9, 4, 9}, /* 4 MB */
  163. { 0xe5, 22, 9, 4, 9}, /* 4 MB */
  164. { 0xe6, 23, 9, 4, 10}, /* 8 MB */
  165. { 0x73, 24, 9, 5, 10}, /* 16 MB */
  166. { 0x75, 25, 9, 5, 10}, /* 32 MB */
  167. { 0x76, 26, 9, 5, 10}, /* 64 MB */
  168. { 0x79, 27, 9, 5, 10}, /* 128 MB */
  169. { 0x71, 28, 9, 5, 10}, /* 256 MB */
  170. /* MASK ROM */
  171. { 0x5d, 21, 9, 4, 8}, /* 2 MB */
  172. { 0xd5, 22, 9, 4, 9}, /* 4 MB */
  173. { 0xd6, 23, 9, 4, 10}, /* 8 MB */
  174. { 0x57, 24, 9, 4, 11}, /* 16 MB */
  175. { 0x58, 25, 9, 4, 12}, /* 32 MB */
  176. { 0,}
  177. };
  178. static struct alauda_card_info *alauda_card_find_id(unsigned char id) {
  179. int i;
  180. for (i = 0; alauda_card_ids[i].id != 0; i++)
  181. if (alauda_card_ids[i].id == id)
  182. return &(alauda_card_ids[i]);
  183. return NULL;
  184. }
  185. /*
  186. * ECC computation.
  187. */
  188. static unsigned char parity[256];
  189. static unsigned char ecc2[256];
  190. static void nand_init_ecc(void) {
  191. int i, j, a;
  192. parity[0] = 0;
  193. for (i = 1; i < 256; i++)
  194. parity[i] = (parity[i&(i-1)] ^ 1);
  195. for (i = 0; i < 256; i++) {
  196. a = 0;
  197. for (j = 0; j < 8; j++) {
  198. if (i & (1<<j)) {
  199. if ((j & 1) == 0)
  200. a ^= 0x04;
  201. if ((j & 2) == 0)
  202. a ^= 0x10;
  203. if ((j & 4) == 0)
  204. a ^= 0x40;
  205. }
  206. }
  207. ecc2[i] = ~(a ^ (a<<1) ^ (parity[i] ? 0xa8 : 0));
  208. }
  209. }
  210. /* compute 3-byte ecc on 256 bytes */
  211. static void nand_compute_ecc(unsigned char *data, unsigned char *ecc) {
  212. int i, j, a;
  213. unsigned char par, bit, bits[8];
  214. par = 0;
  215. for (j = 0; j < 8; j++)
  216. bits[j] = 0;
  217. /* collect 16 checksum bits */
  218. for (i = 0; i < 256; i++) {
  219. par ^= data[i];
  220. bit = parity[data[i]];
  221. for (j = 0; j < 8; j++)
  222. if ((i & (1<<j)) == 0)
  223. bits[j] ^= bit;
  224. }
  225. /* put 4+4+4 = 12 bits in the ecc */
  226. a = (bits[3] << 6) + (bits[2] << 4) + (bits[1] << 2) + bits[0];
  227. ecc[0] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
  228. a = (bits[7] << 6) + (bits[6] << 4) + (bits[5] << 2) + bits[4];
  229. ecc[1] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
  230. ecc[2] = ecc2[par];
  231. }
  232. static int nand_compare_ecc(unsigned char *data, unsigned char *ecc) {
  233. return (data[0] == ecc[0] && data[1] == ecc[1] && data[2] == ecc[2]);
  234. }
  235. static void nand_store_ecc(unsigned char *data, unsigned char *ecc) {
  236. memcpy(data, ecc, 3);
  237. }
  238. /*
  239. * Alauda driver
  240. */
  241. /*
  242. * Forget our PBA <---> LBA mappings for a particular port
  243. */
  244. static void alauda_free_maps (struct alauda_media_info *media_info)
  245. {
  246. unsigned int shift = media_info->zoneshift
  247. + media_info->blockshift + media_info->pageshift;
  248. unsigned int num_zones = media_info->capacity >> shift;
  249. unsigned int i;
  250. if (media_info->lba_to_pba != NULL)
  251. for (i = 0; i < num_zones; i++) {
  252. kfree(media_info->lba_to_pba[i]);
  253. media_info->lba_to_pba[i] = NULL;
  254. }
  255. if (media_info->pba_to_lba != NULL)
  256. for (i = 0; i < num_zones; i++) {
  257. kfree(media_info->pba_to_lba[i]);
  258. media_info->pba_to_lba[i] = NULL;
  259. }
  260. }
  261. /*
  262. * Returns 2 bytes of status data
  263. * The first byte describes media status, and second byte describes door status
  264. */
  265. static int alauda_get_media_status(struct us_data *us, unsigned char *data)
  266. {
  267. int rc;
  268. unsigned char command;
  269. if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
  270. command = ALAUDA_GET_XD_MEDIA_STATUS;
  271. else
  272. command = ALAUDA_GET_SM_MEDIA_STATUS;
  273. rc = usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
  274. command, 0xc0, 0, 1, data, 2);
  275. usb_stor_dbg(us, "Media status %02X %02X\n", 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. usb_stor_dbg(us, "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. usb_stor_dbg(us, "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. usb_stor_dbg(us, "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. usb_stor_dbg(us, "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. usb_stor_dbg(us, "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. usb_stor_dbg(us, "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. usb_stor_dbg(us, "%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. usb_stor_dbg(us, "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. usb_stor_dbg(us, "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. usb_stor_dbg(us, "PBA %d has no logical mapping: reserved area = %02X%02X%02X%02X data status %02X block status %02X\n",
  513. blocknum,
  514. data[0], data[1], data[2], data[3],
  515. data[4], data[5]);
  516. pba_to_lba[i] = UNUSABLE;
  517. continue;
  518. }
  519. if ((data[6] >> 4) != 0x01) {
  520. usb_stor_dbg(us, "PBA %d has invalid address field %02X%02X/%02X%02X\n",
  521. blocknum, data[6], data[7],
  522. data[11], data[12]);
  523. pba_to_lba[i] = UNUSABLE;
  524. continue;
  525. }
  526. /* check even parity */
  527. if (parity[data[6] ^ data[7]]) {
  528. printk(KERN_WARNING
  529. "alauda_read_map: Bad parity in LBA for block %d"
  530. " (%02X %02X)\n", i, data[6], data[7]);
  531. pba_to_lba[i] = UNUSABLE;
  532. continue;
  533. }
  534. lba_offset = short_pack(data[7], data[6]);
  535. lba_offset = (lba_offset & 0x07FF) >> 1;
  536. lba_real = lba_offset + zone_base_lba;
  537. /*
  538. * Every 1024 physical blocks ("zone"), the LBA numbers
  539. * go back to zero, but are within a higher block of LBA's.
  540. * Also, there is a maximum of 1000 LBA's per zone.
  541. * In other words, in PBA 1024-2047 you will find LBA 0-999
  542. * which are really LBA 1000-1999. This allows for 24 bad
  543. * or special physical blocks per zone.
  544. */
  545. if (lba_offset >= uzonesize) {
  546. printk(KERN_WARNING
  547. "alauda_read_map: Bad low LBA %d for block %d\n",
  548. lba_real, blocknum);
  549. continue;
  550. }
  551. if (lba_to_pba[lba_offset] != UNDEF) {
  552. printk(KERN_WARNING
  553. "alauda_read_map: "
  554. "LBA %d seen for PBA %d and %d\n",
  555. lba_real, lba_to_pba[lba_offset], blocknum);
  556. continue;
  557. }
  558. pba_to_lba[i] = lba_real;
  559. lba_to_pba[lba_offset] = blocknum;
  560. continue;
  561. }
  562. MEDIA_INFO(us).lba_to_pba[zone] = lba_to_pba;
  563. MEDIA_INFO(us).pba_to_lba[zone] = pba_to_lba;
  564. result = 0;
  565. goto out;
  566. error:
  567. kfree(lba_to_pba);
  568. kfree(pba_to_lba);
  569. out:
  570. return result;
  571. }
  572. /*
  573. * Checks to see whether we have already mapped a certain zone
  574. * If we haven't, the map is generated
  575. */
  576. static void alauda_ensure_map_for_zone(struct us_data *us, unsigned int zone)
  577. {
  578. if (MEDIA_INFO(us).lba_to_pba[zone] == NULL
  579. || MEDIA_INFO(us).pba_to_lba[zone] == NULL)
  580. alauda_read_map(us, zone);
  581. }
  582. /*
  583. * Erases an entire block
  584. */
  585. static int alauda_erase_block(struct us_data *us, u16 pba)
  586. {
  587. int rc;
  588. unsigned char command[] = {
  589. ALAUDA_BULK_CMD, ALAUDA_BULK_ERASE_BLOCK, PBA_HI(pba),
  590. PBA_ZONE(pba), 0, PBA_LO(pba), 0x02, 0, MEDIA_PORT(us)
  591. };
  592. unsigned char buf[2];
  593. usb_stor_dbg(us, "Erasing PBA %d\n", pba);
  594. rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  595. command, 9, NULL);
  596. if (rc != USB_STOR_XFER_GOOD)
  597. return rc;
  598. rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  599. buf, 2, NULL);
  600. if (rc != USB_STOR_XFER_GOOD)
  601. return rc;
  602. usb_stor_dbg(us, "Erase result: %02X %02X\n", buf[0], buf[1]);
  603. return rc;
  604. }
  605. /*
  606. * Reads data from a certain offset page inside a PBA, including interleaved
  607. * redundancy data. Returns (pagesize+64)*pages bytes in data.
  608. */
  609. static int alauda_read_block_raw(struct us_data *us, u16 pba,
  610. unsigned int page, unsigned int pages, unsigned char *data)
  611. {
  612. int rc;
  613. unsigned char command[] = {
  614. ALAUDA_BULK_CMD, ALAUDA_BULK_READ_BLOCK, PBA_HI(pba),
  615. PBA_ZONE(pba), 0, PBA_LO(pba) + page, pages, 0, MEDIA_PORT(us)
  616. };
  617. usb_stor_dbg(us, "pba %d page %d count %d\n", pba, page, pages);
  618. rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  619. command, 9, NULL);
  620. if (rc != USB_STOR_XFER_GOOD)
  621. return rc;
  622. return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  623. data, (MEDIA_INFO(us).pagesize + 64) * pages, NULL);
  624. }
  625. /*
  626. * Reads data from a certain offset page inside a PBA, excluding redundancy
  627. * data. Returns pagesize*pages bytes in data. Note that data must be big enough
  628. * to hold (pagesize+64)*pages bytes of data, but you can ignore those 'extra'
  629. * trailing bytes outside this function.
  630. */
  631. static int alauda_read_block(struct us_data *us, u16 pba,
  632. unsigned int page, unsigned int pages, unsigned char *data)
  633. {
  634. int i, rc;
  635. unsigned int pagesize = MEDIA_INFO(us).pagesize;
  636. rc = alauda_read_block_raw(us, pba, page, pages, data);
  637. if (rc != USB_STOR_XFER_GOOD)
  638. return rc;
  639. /* Cut out the redundancy data */
  640. for (i = 0; i < pages; i++) {
  641. int dest_offset = i * pagesize;
  642. int src_offset = i * (pagesize + 64);
  643. memmove(data + dest_offset, data + src_offset, pagesize);
  644. }
  645. return rc;
  646. }
  647. /*
  648. * Writes an entire block of data and checks status after write.
  649. * Redundancy data must be already included in data. Data should be
  650. * (pagesize+64)*blocksize bytes in length.
  651. */
  652. static int alauda_write_block(struct us_data *us, u16 pba, unsigned char *data)
  653. {
  654. int rc;
  655. struct alauda_info *info = (struct alauda_info *) us->extra;
  656. unsigned char command[] = {
  657. ALAUDA_BULK_CMD, ALAUDA_BULK_WRITE_BLOCK, PBA_HI(pba),
  658. PBA_ZONE(pba), 0, PBA_LO(pba), 32, 0, MEDIA_PORT(us)
  659. };
  660. usb_stor_dbg(us, "pba %d\n", pba);
  661. rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  662. command, 9, NULL);
  663. if (rc != USB_STOR_XFER_GOOD)
  664. return rc;
  665. rc = usb_stor_bulk_transfer_buf(us, info->wr_ep, data,
  666. (MEDIA_INFO(us).pagesize + 64) * MEDIA_INFO(us).blocksize,
  667. NULL);
  668. if (rc != USB_STOR_XFER_GOOD)
  669. return rc;
  670. return alauda_check_status2(us);
  671. }
  672. /*
  673. * Write some data to a specific LBA.
  674. */
  675. static int alauda_write_lba(struct us_data *us, u16 lba,
  676. unsigned int page, unsigned int pages,
  677. unsigned char *ptr, unsigned char *blockbuffer)
  678. {
  679. u16 pba, lbap, new_pba;
  680. unsigned char *bptr, *cptr, *xptr;
  681. unsigned char ecc[3];
  682. int i, result;
  683. unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
  684. unsigned int zonesize = MEDIA_INFO(us).zonesize;
  685. unsigned int pagesize = MEDIA_INFO(us).pagesize;
  686. unsigned int blocksize = MEDIA_INFO(us).blocksize;
  687. unsigned int lba_offset = lba % uzonesize;
  688. unsigned int new_pba_offset;
  689. unsigned int zone = lba / uzonesize;
  690. alauda_ensure_map_for_zone(us, zone);
  691. pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
  692. if (pba == 1) {
  693. /* Maybe it is impossible to write to PBA 1.
  694. Fake success, but don't do anything. */
  695. printk(KERN_WARNING
  696. "alauda_write_lba: avoid writing to pba 1\n");
  697. return USB_STOR_TRANSPORT_GOOD;
  698. }
  699. new_pba = alauda_find_unused_pba(&MEDIA_INFO(us), zone);
  700. if (!new_pba) {
  701. printk(KERN_WARNING
  702. "alauda_write_lba: Out of unused blocks\n");
  703. return USB_STOR_TRANSPORT_ERROR;
  704. }
  705. /* read old contents */
  706. if (pba != UNDEF) {
  707. result = alauda_read_block_raw(us, pba, 0,
  708. blocksize, blockbuffer);
  709. if (result != USB_STOR_XFER_GOOD)
  710. return result;
  711. } else {
  712. memset(blockbuffer, 0, blocksize * (pagesize + 64));
  713. }
  714. lbap = (lba_offset << 1) | 0x1000;
  715. if (parity[MSB_of(lbap) ^ LSB_of(lbap)])
  716. lbap ^= 1;
  717. /* check old contents and fill lba */
  718. for (i = 0; i < blocksize; i++) {
  719. bptr = blockbuffer + (i * (pagesize + 64));
  720. cptr = bptr + pagesize;
  721. nand_compute_ecc(bptr, ecc);
  722. if (!nand_compare_ecc(cptr+13, ecc)) {
  723. usb_stor_dbg(us, "Warning: bad ecc in page %d- of pba %d\n",
  724. i, pba);
  725. nand_store_ecc(cptr+13, ecc);
  726. }
  727. nand_compute_ecc(bptr + (pagesize / 2), ecc);
  728. if (!nand_compare_ecc(cptr+8, ecc)) {
  729. usb_stor_dbg(us, "Warning: bad ecc in page %d+ of pba %d\n",
  730. i, pba);
  731. nand_store_ecc(cptr+8, ecc);
  732. }
  733. cptr[6] = cptr[11] = MSB_of(lbap);
  734. cptr[7] = cptr[12] = LSB_of(lbap);
  735. }
  736. /* copy in new stuff and compute ECC */
  737. xptr = ptr;
  738. for (i = page; i < page+pages; i++) {
  739. bptr = blockbuffer + (i * (pagesize + 64));
  740. cptr = bptr + pagesize;
  741. memcpy(bptr, xptr, pagesize);
  742. xptr += pagesize;
  743. nand_compute_ecc(bptr, ecc);
  744. nand_store_ecc(cptr+13, ecc);
  745. nand_compute_ecc(bptr + (pagesize / 2), ecc);
  746. nand_store_ecc(cptr+8, ecc);
  747. }
  748. result = alauda_write_block(us, new_pba, blockbuffer);
  749. if (result != USB_STOR_XFER_GOOD)
  750. return result;
  751. new_pba_offset = new_pba - (zone * zonesize);
  752. MEDIA_INFO(us).pba_to_lba[zone][new_pba_offset] = lba;
  753. MEDIA_INFO(us).lba_to_pba[zone][lba_offset] = new_pba;
  754. usb_stor_dbg(us, "Remapped LBA %d to PBA %d\n", lba, new_pba);
  755. if (pba != UNDEF) {
  756. unsigned int pba_offset = pba - (zone * zonesize);
  757. result = alauda_erase_block(us, pba);
  758. if (result != USB_STOR_XFER_GOOD)
  759. return result;
  760. MEDIA_INFO(us).pba_to_lba[zone][pba_offset] = UNDEF;
  761. }
  762. return USB_STOR_TRANSPORT_GOOD;
  763. }
  764. /*
  765. * Read data from a specific sector address
  766. */
  767. static int alauda_read_data(struct us_data *us, unsigned long address,
  768. unsigned int sectors)
  769. {
  770. unsigned char *buffer;
  771. u16 lba, max_lba;
  772. unsigned int page, len, offset;
  773. unsigned int blockshift = MEDIA_INFO(us).blockshift;
  774. unsigned int pageshift = MEDIA_INFO(us).pageshift;
  775. unsigned int blocksize = MEDIA_INFO(us).blocksize;
  776. unsigned int pagesize = MEDIA_INFO(us).pagesize;
  777. unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
  778. struct scatterlist *sg;
  779. int result;
  780. /*
  781. * Since we only read in one block at a time, we have to create
  782. * a bounce buffer and move the data a piece at a time between the
  783. * bounce buffer and the actual transfer buffer.
  784. * We make this buffer big enough to hold temporary redundancy data,
  785. * which we use when reading the data blocks.
  786. */
  787. len = min(sectors, blocksize) * (pagesize + 64);
  788. buffer = kmalloc(len, GFP_NOIO);
  789. if (buffer == NULL) {
  790. printk(KERN_WARNING "alauda_read_data: Out of memory\n");
  791. return USB_STOR_TRANSPORT_ERROR;
  792. }
  793. /* Figure out the initial LBA and page */
  794. lba = address >> blockshift;
  795. page = (address & MEDIA_INFO(us).blockmask);
  796. max_lba = MEDIA_INFO(us).capacity >> (blockshift + pageshift);
  797. result = USB_STOR_TRANSPORT_GOOD;
  798. offset = 0;
  799. sg = NULL;
  800. while (sectors > 0) {
  801. unsigned int zone = lba / uzonesize; /* integer division */
  802. unsigned int lba_offset = lba - (zone * uzonesize);
  803. unsigned int pages;
  804. u16 pba;
  805. alauda_ensure_map_for_zone(us, zone);
  806. /* Not overflowing capacity? */
  807. if (lba >= max_lba) {
  808. usb_stor_dbg(us, "Error: Requested lba %u exceeds maximum %u\n",
  809. lba, max_lba);
  810. result = USB_STOR_TRANSPORT_ERROR;
  811. break;
  812. }
  813. /* Find number of pages we can read in this block */
  814. pages = min(sectors, blocksize - page);
  815. len = pages << pageshift;
  816. /* Find where this lba lives on disk */
  817. pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
  818. if (pba == UNDEF) { /* this lba was never written */
  819. usb_stor_dbg(us, "Read %d zero pages (LBA %d) page %d\n",
  820. pages, lba, page);
  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. memset(buffer, 0, len);
  826. } else {
  827. usb_stor_dbg(us, "Read %d pages, from PBA %d (LBA %d) page %d\n",
  828. pages, pba, lba, page);
  829. result = alauda_read_block(us, pba, page, pages, buffer);
  830. if (result != USB_STOR_TRANSPORT_GOOD)
  831. break;
  832. }
  833. /* Store the data in the transfer buffer */
  834. usb_stor_access_xfer_buf(buffer, len, us->srb,
  835. &sg, &offset, TO_XFER_BUF);
  836. page = 0;
  837. lba++;
  838. sectors -= pages;
  839. }
  840. kfree(buffer);
  841. return result;
  842. }
  843. /*
  844. * Write data to a specific sector address
  845. */
  846. static int alauda_write_data(struct us_data *us, unsigned long address,
  847. unsigned int sectors)
  848. {
  849. unsigned char *buffer, *blockbuffer;
  850. unsigned int page, len, offset;
  851. unsigned int blockshift = MEDIA_INFO(us).blockshift;
  852. unsigned int pageshift = MEDIA_INFO(us).pageshift;
  853. unsigned int blocksize = MEDIA_INFO(us).blocksize;
  854. unsigned int pagesize = MEDIA_INFO(us).pagesize;
  855. struct scatterlist *sg;
  856. u16 lba, max_lba;
  857. int result;
  858. /*
  859. * Since we don't write the user data directly to the device,
  860. * we have to create a bounce buffer and move the data a piece
  861. * at a time between the bounce buffer and the actual transfer buffer.
  862. */
  863. len = min(sectors, blocksize) * pagesize;
  864. buffer = kmalloc(len, GFP_NOIO);
  865. if (buffer == NULL) {
  866. printk(KERN_WARNING "alauda_write_data: Out of memory\n");
  867. return USB_STOR_TRANSPORT_ERROR;
  868. }
  869. /*
  870. * We also need a temporary block buffer, where we read in the old data,
  871. * overwrite parts with the new data, and manipulate the redundancy data
  872. */
  873. blockbuffer = kmalloc((pagesize + 64) * blocksize, GFP_NOIO);
  874. if (blockbuffer == NULL) {
  875. printk(KERN_WARNING "alauda_write_data: Out of memory\n");
  876. kfree(buffer);
  877. return USB_STOR_TRANSPORT_ERROR;
  878. }
  879. /* Figure out the initial LBA and page */
  880. lba = address >> blockshift;
  881. page = (address & MEDIA_INFO(us).blockmask);
  882. max_lba = MEDIA_INFO(us).capacity >> (pageshift + blockshift);
  883. result = USB_STOR_TRANSPORT_GOOD;
  884. offset = 0;
  885. sg = NULL;
  886. while (sectors > 0) {
  887. /* Write as many sectors as possible in this block */
  888. unsigned int pages = min(sectors, blocksize - page);
  889. len = pages << pageshift;
  890. /* Not overflowing capacity? */
  891. if (lba >= max_lba) {
  892. usb_stor_dbg(us, "Requested lba %u exceeds maximum %u\n",
  893. lba, max_lba);
  894. result = USB_STOR_TRANSPORT_ERROR;
  895. break;
  896. }
  897. /* Get the data from the transfer buffer */
  898. usb_stor_access_xfer_buf(buffer, len, us->srb,
  899. &sg, &offset, FROM_XFER_BUF);
  900. result = alauda_write_lba(us, lba, page, pages, buffer,
  901. blockbuffer);
  902. if (result != USB_STOR_TRANSPORT_GOOD)
  903. break;
  904. page = 0;
  905. lba++;
  906. sectors -= pages;
  907. }
  908. kfree(buffer);
  909. kfree(blockbuffer);
  910. return result;
  911. }
  912. /*
  913. * Our interface with the rest of the world
  914. */
  915. static void alauda_info_destructor(void *extra)
  916. {
  917. struct alauda_info *info = (struct alauda_info *) extra;
  918. int port;
  919. if (!info)
  920. return;
  921. for (port = 0; port < 2; port++) {
  922. struct alauda_media_info *media_info = &info->port[port];
  923. alauda_free_maps(media_info);
  924. kfree(media_info->lba_to_pba);
  925. kfree(media_info->pba_to_lba);
  926. }
  927. }
  928. /*
  929. * Initialize alauda_info struct and find the data-write endpoint
  930. */
  931. static int init_alauda(struct us_data *us)
  932. {
  933. struct alauda_info *info;
  934. struct usb_host_interface *altsetting = us->pusb_intf->cur_altsetting;
  935. nand_init_ecc();
  936. us->extra = kzalloc(sizeof(struct alauda_info), GFP_NOIO);
  937. if (!us->extra)
  938. return USB_STOR_TRANSPORT_ERROR;
  939. info = (struct alauda_info *) us->extra;
  940. us->extra_destructor = alauda_info_destructor;
  941. info->wr_ep = usb_sndbulkpipe(us->pusb_dev,
  942. altsetting->endpoint[0].desc.bEndpointAddress
  943. & USB_ENDPOINT_NUMBER_MASK);
  944. return USB_STOR_TRANSPORT_GOOD;
  945. }
  946. static int alauda_transport(struct scsi_cmnd *srb, struct us_data *us)
  947. {
  948. int rc;
  949. struct alauda_info *info = (struct alauda_info *) us->extra;
  950. unsigned char *ptr = us->iobuf;
  951. static unsigned char inquiry_response[36] = {
  952. 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
  953. };
  954. if (srb->cmnd[0] == INQUIRY) {
  955. usb_stor_dbg(us, "INQUIRY - Returning bogus response\n");
  956. memcpy(ptr, inquiry_response, sizeof(inquiry_response));
  957. fill_inquiry_response(us, ptr, 36);
  958. return USB_STOR_TRANSPORT_GOOD;
  959. }
  960. if (srb->cmnd[0] == TEST_UNIT_READY) {
  961. usb_stor_dbg(us, "TEST_UNIT_READY\n");
  962. return alauda_check_media(us);
  963. }
  964. if (srb->cmnd[0] == READ_CAPACITY) {
  965. unsigned int num_zones;
  966. unsigned long capacity;
  967. rc = alauda_check_media(us);
  968. if (rc != USB_STOR_TRANSPORT_GOOD)
  969. return rc;
  970. num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift
  971. + MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift);
  972. capacity = num_zones * MEDIA_INFO(us).uzonesize
  973. * MEDIA_INFO(us).blocksize;
  974. /* Report capacity and page size */
  975. ((__be32 *) ptr)[0] = cpu_to_be32(capacity - 1);
  976. ((__be32 *) ptr)[1] = cpu_to_be32(512);
  977. usb_stor_set_xfer_buf(ptr, 8, srb);
  978. return USB_STOR_TRANSPORT_GOOD;
  979. }
  980. if (srb->cmnd[0] == READ_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, "READ_10: page %d pagect %d\n", page, pages);
  990. return alauda_read_data(us, page, pages);
  991. }
  992. if (srb->cmnd[0] == WRITE_10) {
  993. unsigned int page, pages;
  994. rc = alauda_check_media(us);
  995. if (rc != USB_STOR_TRANSPORT_GOOD)
  996. return rc;
  997. page = short_pack(srb->cmnd[3], srb->cmnd[2]);
  998. page <<= 16;
  999. page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
  1000. pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
  1001. usb_stor_dbg(us, "WRITE_10: page %d pagect %d\n", page, pages);
  1002. return alauda_write_data(us, page, pages);
  1003. }
  1004. if (srb->cmnd[0] == REQUEST_SENSE) {
  1005. usb_stor_dbg(us, "REQUEST_SENSE\n");
  1006. memset(ptr, 0, 18);
  1007. ptr[0] = 0xF0;
  1008. ptr[2] = info->sense_key;
  1009. ptr[7] = 11;
  1010. ptr[12] = info->sense_asc;
  1011. ptr[13] = info->sense_ascq;
  1012. usb_stor_set_xfer_buf(ptr, 18, srb);
  1013. return USB_STOR_TRANSPORT_GOOD;
  1014. }
  1015. if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
  1016. /* sure. whatever. not like we can stop the user from popping
  1017. the media out of the device (no locking doors, etc) */
  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 int alauda_probe(struct usb_interface *intf,
  1028. const struct usb_device_id *id)
  1029. {
  1030. struct us_data *us;
  1031. int result;
  1032. result = usb_stor_probe1(&us, intf, id,
  1033. (id - alauda_usb_ids) + alauda_unusual_dev_list);
  1034. if (result)
  1035. return result;
  1036. us->transport_name = "Alauda Control/Bulk";
  1037. us->transport = alauda_transport;
  1038. us->transport_reset = usb_stor_Bulk_reset;
  1039. us->max_lun = 1;
  1040. result = usb_stor_probe2(us);
  1041. return result;
  1042. }
  1043. static struct usb_driver alauda_driver = {
  1044. .name = "ums-alauda",
  1045. .probe = alauda_probe,
  1046. .disconnect = usb_stor_disconnect,
  1047. .suspend = usb_stor_suspend,
  1048. .resume = usb_stor_resume,
  1049. .reset_resume = usb_stor_reset_resume,
  1050. .pre_reset = usb_stor_pre_reset,
  1051. .post_reset = usb_stor_post_reset,
  1052. .id_table = alauda_usb_ids,
  1053. .soft_unbind = 1,
  1054. .no_dynamic_id = 1,
  1055. };
  1056. module_usb_driver(alauda_driver);