alauda.c 33 KB

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