alauda.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  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)|(USB_US_TYPE_STOR<<24) }
  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. US_DEBUGP("alauda_get_media_status: Media status %02X %02X\n",
  276. data[0], data[1]);
  277. return rc;
  278. }
  279. /*
  280. * Clears the "media was changed" bit so that we know when it changes again
  281. * in the future.
  282. */
  283. static int alauda_ack_media(struct us_data *us)
  284. {
  285. unsigned char command;
  286. if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
  287. command = ALAUDA_ACK_XD_MEDIA_CHANGE;
  288. else
  289. command = ALAUDA_ACK_SM_MEDIA_CHANGE;
  290. return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
  291. command, 0x40, 0, 1, NULL, 0);
  292. }
  293. /*
  294. * Retrieves a 4-byte media signature, which indicates manufacturer, capacity,
  295. * and some other details.
  296. */
  297. static int alauda_get_media_signature(struct us_data *us, unsigned char *data)
  298. {
  299. unsigned char command;
  300. if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
  301. command = ALAUDA_GET_XD_MEDIA_SIG;
  302. else
  303. command = ALAUDA_GET_SM_MEDIA_SIG;
  304. return usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
  305. command, 0xc0, 0, 0, data, 4);
  306. }
  307. /*
  308. * Resets the media status (but not the whole device?)
  309. */
  310. static int alauda_reset_media(struct us_data *us)
  311. {
  312. unsigned char *command = us->iobuf;
  313. memset(command, 0, 9);
  314. command[0] = ALAUDA_BULK_CMD;
  315. command[1] = ALAUDA_BULK_RESET_MEDIA;
  316. command[8] = MEDIA_PORT(us);
  317. return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  318. command, 9, NULL);
  319. }
  320. /*
  321. * Examines the media and deduces capacity, etc.
  322. */
  323. static int alauda_init_media(struct us_data *us)
  324. {
  325. unsigned char *data = us->iobuf;
  326. int ready = 0;
  327. struct alauda_card_info *media_info;
  328. unsigned int num_zones;
  329. while (ready == 0) {
  330. msleep(20);
  331. if (alauda_get_media_status(us, data) != USB_STOR_XFER_GOOD)
  332. return USB_STOR_TRANSPORT_ERROR;
  333. if (data[0] & 0x10)
  334. ready = 1;
  335. }
  336. US_DEBUGP("alauda_init_media: We are ready for action!\n");
  337. if (alauda_ack_media(us) != USB_STOR_XFER_GOOD)
  338. return USB_STOR_TRANSPORT_ERROR;
  339. msleep(10);
  340. if (alauda_get_media_status(us, data) != USB_STOR_XFER_GOOD)
  341. return USB_STOR_TRANSPORT_ERROR;
  342. if (data[0] != 0x14) {
  343. US_DEBUGP("alauda_init_media: Media not ready after ack\n");
  344. return USB_STOR_TRANSPORT_ERROR;
  345. }
  346. if (alauda_get_media_signature(us, data) != USB_STOR_XFER_GOOD)
  347. return USB_STOR_TRANSPORT_ERROR;
  348. US_DEBUGP("alauda_init_media: Media signature: %02X %02X %02X %02X\n",
  349. data[0], data[1], data[2], data[3]);
  350. media_info = alauda_card_find_id(data[1]);
  351. if (media_info == NULL) {
  352. printk(KERN_WARNING
  353. "alauda_init_media: Unrecognised media signature: "
  354. "%02X %02X %02X %02X\n",
  355. data[0], data[1], data[2], data[3]);
  356. return USB_STOR_TRANSPORT_ERROR;
  357. }
  358. MEDIA_INFO(us).capacity = 1 << media_info->chipshift;
  359. US_DEBUGP("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. US_DEBUGP("alauda_check_media: 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. US_DEBUGP("alauda_check_media: 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. US_DEBUGP("alauda_check_status2: %02X %02X %02X\n", data[0], data[1], data[2]);
  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. US_DEBUGP("alauda_read_map: 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. US_DEBUGP("alauda_read_map: 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. US_DEBUGP("alauda_read_map: PBA %d has no logical mapping: "
  514. "reserved area = %02X%02X%02X%02X "
  515. "data status %02X block status %02X\n",
  516. blocknum, data[0], data[1], data[2], data[3],
  517. data[4], data[5]);
  518. pba_to_lba[i] = UNUSABLE;
  519. continue;
  520. }
  521. if ((data[6] >> 4) != 0x01) {
  522. US_DEBUGP("alauda_read_map: PBA %d has invalid address "
  523. "field %02X%02X/%02X%02X\n",
  524. blocknum, data[6], data[7], data[11], data[12]);
  525. pba_to_lba[i] = UNUSABLE;
  526. continue;
  527. }
  528. /* check even parity */
  529. if (parity[data[6] ^ data[7]]) {
  530. printk(KERN_WARNING
  531. "alauda_read_map: Bad parity in LBA for block %d"
  532. " (%02X %02X)\n", i, data[6], data[7]);
  533. pba_to_lba[i] = UNUSABLE;
  534. continue;
  535. }
  536. lba_offset = short_pack(data[7], data[6]);
  537. lba_offset = (lba_offset & 0x07FF) >> 1;
  538. lba_real = lba_offset + zone_base_lba;
  539. /*
  540. * Every 1024 physical blocks ("zone"), the LBA numbers
  541. * go back to zero, but are within a higher block of LBA's.
  542. * Also, there is a maximum of 1000 LBA's per zone.
  543. * In other words, in PBA 1024-2047 you will find LBA 0-999
  544. * which are really LBA 1000-1999. This allows for 24 bad
  545. * or special physical blocks per zone.
  546. */
  547. if (lba_offset >= uzonesize) {
  548. printk(KERN_WARNING
  549. "alauda_read_map: Bad low LBA %d for block %d\n",
  550. lba_real, blocknum);
  551. continue;
  552. }
  553. if (lba_to_pba[lba_offset] != UNDEF) {
  554. printk(KERN_WARNING
  555. "alauda_read_map: "
  556. "LBA %d seen for PBA %d and %d\n",
  557. lba_real, lba_to_pba[lba_offset], blocknum);
  558. continue;
  559. }
  560. pba_to_lba[i] = lba_real;
  561. lba_to_pba[lba_offset] = blocknum;
  562. continue;
  563. }
  564. MEDIA_INFO(us).lba_to_pba[zone] = lba_to_pba;
  565. MEDIA_INFO(us).pba_to_lba[zone] = pba_to_lba;
  566. result = 0;
  567. goto out;
  568. error:
  569. kfree(lba_to_pba);
  570. kfree(pba_to_lba);
  571. out:
  572. return result;
  573. }
  574. /*
  575. * Checks to see whether we have already mapped a certain zone
  576. * If we haven't, the map is generated
  577. */
  578. static void alauda_ensure_map_for_zone(struct us_data *us, unsigned int zone)
  579. {
  580. if (MEDIA_INFO(us).lba_to_pba[zone] == NULL
  581. || MEDIA_INFO(us).pba_to_lba[zone] == NULL)
  582. alauda_read_map(us, zone);
  583. }
  584. /*
  585. * Erases an entire block
  586. */
  587. static int alauda_erase_block(struct us_data *us, u16 pba)
  588. {
  589. int rc;
  590. unsigned char command[] = {
  591. ALAUDA_BULK_CMD, ALAUDA_BULK_ERASE_BLOCK, PBA_HI(pba),
  592. PBA_ZONE(pba), 0, PBA_LO(pba), 0x02, 0, MEDIA_PORT(us)
  593. };
  594. unsigned char buf[2];
  595. US_DEBUGP("alauda_erase_block: Erasing PBA %d\n", pba);
  596. rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  597. command, 9, NULL);
  598. if (rc != USB_STOR_XFER_GOOD)
  599. return rc;
  600. rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  601. buf, 2, NULL);
  602. if (rc != USB_STOR_XFER_GOOD)
  603. return rc;
  604. US_DEBUGP("alauda_erase_block: Erase result: %02X %02X\n",
  605. buf[0], buf[1]);
  606. return rc;
  607. }
  608. /*
  609. * Reads data from a certain offset page inside a PBA, including interleaved
  610. * redundancy data. Returns (pagesize+64)*pages bytes in data.
  611. */
  612. static int alauda_read_block_raw(struct us_data *us, u16 pba,
  613. unsigned int page, unsigned int pages, unsigned char *data)
  614. {
  615. int rc;
  616. unsigned char command[] = {
  617. ALAUDA_BULK_CMD, ALAUDA_BULK_READ_BLOCK, PBA_HI(pba),
  618. PBA_ZONE(pba), 0, PBA_LO(pba) + page, pages, 0, MEDIA_PORT(us)
  619. };
  620. US_DEBUGP("alauda_read_block: pba %d page %d count %d\n",
  621. pba, page, pages);
  622. rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  623. command, 9, NULL);
  624. if (rc != USB_STOR_XFER_GOOD)
  625. return rc;
  626. return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  627. data, (MEDIA_INFO(us).pagesize + 64) * pages, NULL);
  628. }
  629. /*
  630. * Reads data from a certain offset page inside a PBA, excluding redundancy
  631. * data. Returns pagesize*pages bytes in data. Note that data must be big enough
  632. * to hold (pagesize+64)*pages bytes of data, but you can ignore those 'extra'
  633. * trailing bytes outside this function.
  634. */
  635. static int alauda_read_block(struct us_data *us, u16 pba,
  636. unsigned int page, unsigned int pages, unsigned char *data)
  637. {
  638. int i, rc;
  639. unsigned int pagesize = MEDIA_INFO(us).pagesize;
  640. rc = alauda_read_block_raw(us, pba, page, pages, data);
  641. if (rc != USB_STOR_XFER_GOOD)
  642. return rc;
  643. /* Cut out the redundancy data */
  644. for (i = 0; i < pages; i++) {
  645. int dest_offset = i * pagesize;
  646. int src_offset = i * (pagesize + 64);
  647. memmove(data + dest_offset, data + src_offset, pagesize);
  648. }
  649. return rc;
  650. }
  651. /*
  652. * Writes an entire block of data and checks status after write.
  653. * Redundancy data must be already included in data. Data should be
  654. * (pagesize+64)*blocksize bytes in length.
  655. */
  656. static int alauda_write_block(struct us_data *us, u16 pba, unsigned char *data)
  657. {
  658. int rc;
  659. struct alauda_info *info = (struct alauda_info *) us->extra;
  660. unsigned char command[] = {
  661. ALAUDA_BULK_CMD, ALAUDA_BULK_WRITE_BLOCK, PBA_HI(pba),
  662. PBA_ZONE(pba), 0, PBA_LO(pba), 32, 0, MEDIA_PORT(us)
  663. };
  664. US_DEBUGP("alauda_write_block: pba %d\n", pba);
  665. rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  666. command, 9, NULL);
  667. if (rc != USB_STOR_XFER_GOOD)
  668. return rc;
  669. rc = usb_stor_bulk_transfer_buf(us, info->wr_ep, data,
  670. (MEDIA_INFO(us).pagesize + 64) * MEDIA_INFO(us).blocksize,
  671. NULL);
  672. if (rc != USB_STOR_XFER_GOOD)
  673. return rc;
  674. return alauda_check_status2(us);
  675. }
  676. /*
  677. * Write some data to a specific LBA.
  678. */
  679. static int alauda_write_lba(struct us_data *us, u16 lba,
  680. unsigned int page, unsigned int pages,
  681. unsigned char *ptr, unsigned char *blockbuffer)
  682. {
  683. u16 pba, lbap, new_pba;
  684. unsigned char *bptr, *cptr, *xptr;
  685. unsigned char ecc[3];
  686. int i, result;
  687. unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
  688. unsigned int zonesize = MEDIA_INFO(us).zonesize;
  689. unsigned int pagesize = MEDIA_INFO(us).pagesize;
  690. unsigned int blocksize = MEDIA_INFO(us).blocksize;
  691. unsigned int lba_offset = lba % uzonesize;
  692. unsigned int new_pba_offset;
  693. unsigned int zone = lba / uzonesize;
  694. alauda_ensure_map_for_zone(us, zone);
  695. pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
  696. if (pba == 1) {
  697. /* Maybe it is impossible to write to PBA 1.
  698. Fake success, but don't do anything. */
  699. printk(KERN_WARNING
  700. "alauda_write_lba: avoid writing to pba 1\n");
  701. return USB_STOR_TRANSPORT_GOOD;
  702. }
  703. new_pba = alauda_find_unused_pba(&MEDIA_INFO(us), zone);
  704. if (!new_pba) {
  705. printk(KERN_WARNING
  706. "alauda_write_lba: Out of unused blocks\n");
  707. return USB_STOR_TRANSPORT_ERROR;
  708. }
  709. /* read old contents */
  710. if (pba != UNDEF) {
  711. result = alauda_read_block_raw(us, pba, 0,
  712. blocksize, blockbuffer);
  713. if (result != USB_STOR_XFER_GOOD)
  714. return result;
  715. } else {
  716. memset(blockbuffer, 0, blocksize * (pagesize + 64));
  717. }
  718. lbap = (lba_offset << 1) | 0x1000;
  719. if (parity[MSB_of(lbap) ^ LSB_of(lbap)])
  720. lbap ^= 1;
  721. /* check old contents and fill lba */
  722. for (i = 0; i < blocksize; i++) {
  723. bptr = blockbuffer + (i * (pagesize + 64));
  724. cptr = bptr + pagesize;
  725. nand_compute_ecc(bptr, ecc);
  726. if (!nand_compare_ecc(cptr+13, ecc)) {
  727. US_DEBUGP("Warning: bad ecc in page %d- of pba %d\n",
  728. i, pba);
  729. nand_store_ecc(cptr+13, ecc);
  730. }
  731. nand_compute_ecc(bptr + (pagesize / 2), ecc);
  732. if (!nand_compare_ecc(cptr+8, ecc)) {
  733. US_DEBUGP("Warning: bad ecc in page %d+ of pba %d\n",
  734. i, pba);
  735. nand_store_ecc(cptr+8, ecc);
  736. }
  737. cptr[6] = cptr[11] = MSB_of(lbap);
  738. cptr[7] = cptr[12] = LSB_of(lbap);
  739. }
  740. /* copy in new stuff and compute ECC */
  741. xptr = ptr;
  742. for (i = page; i < page+pages; i++) {
  743. bptr = blockbuffer + (i * (pagesize + 64));
  744. cptr = bptr + pagesize;
  745. memcpy(bptr, xptr, pagesize);
  746. xptr += pagesize;
  747. nand_compute_ecc(bptr, ecc);
  748. nand_store_ecc(cptr+13, ecc);
  749. nand_compute_ecc(bptr + (pagesize / 2), ecc);
  750. nand_store_ecc(cptr+8, ecc);
  751. }
  752. result = alauda_write_block(us, new_pba, blockbuffer);
  753. if (result != USB_STOR_XFER_GOOD)
  754. return result;
  755. new_pba_offset = new_pba - (zone * zonesize);
  756. MEDIA_INFO(us).pba_to_lba[zone][new_pba_offset] = lba;
  757. MEDIA_INFO(us).lba_to_pba[zone][lba_offset] = new_pba;
  758. US_DEBUGP("alauda_write_lba: Remapped LBA %d to PBA %d\n",
  759. lba, new_pba);
  760. if (pba != UNDEF) {
  761. unsigned int pba_offset = pba - (zone * zonesize);
  762. result = alauda_erase_block(us, pba);
  763. if (result != USB_STOR_XFER_GOOD)
  764. return result;
  765. MEDIA_INFO(us).pba_to_lba[zone][pba_offset] = UNDEF;
  766. }
  767. return USB_STOR_TRANSPORT_GOOD;
  768. }
  769. /*
  770. * Read data from a specific sector address
  771. */
  772. static int alauda_read_data(struct us_data *us, unsigned long address,
  773. unsigned int sectors)
  774. {
  775. unsigned char *buffer;
  776. u16 lba, max_lba;
  777. unsigned int page, len, offset;
  778. unsigned int blockshift = MEDIA_INFO(us).blockshift;
  779. unsigned int pageshift = MEDIA_INFO(us).pageshift;
  780. unsigned int blocksize = MEDIA_INFO(us).blocksize;
  781. unsigned int pagesize = MEDIA_INFO(us).pagesize;
  782. unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
  783. struct scatterlist *sg;
  784. int result;
  785. /*
  786. * Since we only read in one block at a time, we have to create
  787. * a bounce buffer and move the data a piece at a time between the
  788. * bounce buffer and the actual transfer buffer.
  789. * We make this buffer big enough to hold temporary redundancy data,
  790. * which we use when reading the data blocks.
  791. */
  792. len = min(sectors, blocksize) * (pagesize + 64);
  793. buffer = kmalloc(len, GFP_NOIO);
  794. if (buffer == NULL) {
  795. printk(KERN_WARNING "alauda_read_data: Out of memory\n");
  796. return USB_STOR_TRANSPORT_ERROR;
  797. }
  798. /* Figure out the initial LBA and page */
  799. lba = address >> blockshift;
  800. page = (address & MEDIA_INFO(us).blockmask);
  801. max_lba = MEDIA_INFO(us).capacity >> (blockshift + pageshift);
  802. result = USB_STOR_TRANSPORT_GOOD;
  803. offset = 0;
  804. sg = NULL;
  805. while (sectors > 0) {
  806. unsigned int zone = lba / uzonesize; /* integer division */
  807. unsigned int lba_offset = lba - (zone * uzonesize);
  808. unsigned int pages;
  809. u16 pba;
  810. alauda_ensure_map_for_zone(us, zone);
  811. /* Not overflowing capacity? */
  812. if (lba >= max_lba) {
  813. US_DEBUGP("Error: Requested lba %u exceeds "
  814. "maximum %u\n", lba, max_lba);
  815. result = USB_STOR_TRANSPORT_ERROR;
  816. break;
  817. }
  818. /* Find number of pages we can read in this block */
  819. pages = min(sectors, blocksize - page);
  820. len = pages << pageshift;
  821. /* Find where this lba lives on disk */
  822. pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
  823. if (pba == UNDEF) { /* this lba was never written */
  824. US_DEBUGP("Read %d zero pages (LBA %d) page %d\n",
  825. pages, lba, page);
  826. /* This is not really an error. It just means
  827. that the block has never been written.
  828. Instead of returning USB_STOR_TRANSPORT_ERROR
  829. it is better to return all zero data. */
  830. memset(buffer, 0, len);
  831. } else {
  832. US_DEBUGP("Read %d pages, from PBA %d"
  833. " (LBA %d) page %d\n",
  834. pages, pba, lba, page);
  835. result = alauda_read_block(us, pba, page, pages, buffer);
  836. if (result != USB_STOR_TRANSPORT_GOOD)
  837. break;
  838. }
  839. /* Store the data in the transfer buffer */
  840. usb_stor_access_xfer_buf(buffer, len, us->srb,
  841. &sg, &offset, TO_XFER_BUF);
  842. page = 0;
  843. lba++;
  844. sectors -= pages;
  845. }
  846. kfree(buffer);
  847. return result;
  848. }
  849. /*
  850. * Write data to a specific sector address
  851. */
  852. static int alauda_write_data(struct us_data *us, unsigned long address,
  853. unsigned int sectors)
  854. {
  855. unsigned char *buffer, *blockbuffer;
  856. unsigned int page, len, offset;
  857. unsigned int blockshift = MEDIA_INFO(us).blockshift;
  858. unsigned int pageshift = MEDIA_INFO(us).pageshift;
  859. unsigned int blocksize = MEDIA_INFO(us).blocksize;
  860. unsigned int pagesize = MEDIA_INFO(us).pagesize;
  861. struct scatterlist *sg;
  862. u16 lba, max_lba;
  863. int result;
  864. /*
  865. * Since we don't write the user data directly to the device,
  866. * we have to create a bounce buffer and move the data a piece
  867. * at a time between the bounce buffer and the actual transfer buffer.
  868. */
  869. len = min(sectors, blocksize) * pagesize;
  870. buffer = kmalloc(len, GFP_NOIO);
  871. if (buffer == NULL) {
  872. printk(KERN_WARNING "alauda_write_data: Out of memory\n");
  873. return USB_STOR_TRANSPORT_ERROR;
  874. }
  875. /*
  876. * We also need a temporary block buffer, where we read in the old data,
  877. * overwrite parts with the new data, and manipulate the redundancy data
  878. */
  879. blockbuffer = kmalloc((pagesize + 64) * blocksize, GFP_NOIO);
  880. if (blockbuffer == NULL) {
  881. printk(KERN_WARNING "alauda_write_data: Out of memory\n");
  882. kfree(buffer);
  883. return USB_STOR_TRANSPORT_ERROR;
  884. }
  885. /* Figure out the initial LBA and page */
  886. lba = address >> blockshift;
  887. page = (address & MEDIA_INFO(us).blockmask);
  888. max_lba = MEDIA_INFO(us).capacity >> (pageshift + blockshift);
  889. result = USB_STOR_TRANSPORT_GOOD;
  890. offset = 0;
  891. sg = NULL;
  892. while (sectors > 0) {
  893. /* Write as many sectors as possible in this block */
  894. unsigned int pages = min(sectors, blocksize - page);
  895. len = pages << pageshift;
  896. /* Not overflowing capacity? */
  897. if (lba >= max_lba) {
  898. US_DEBUGP("alauda_write_data: Requested lba %u exceeds "
  899. "maximum %u\n", lba, max_lba);
  900. result = USB_STOR_TRANSPORT_ERROR;
  901. break;
  902. }
  903. /* Get the data from the transfer buffer */
  904. usb_stor_access_xfer_buf(buffer, len, us->srb,
  905. &sg, &offset, FROM_XFER_BUF);
  906. result = alauda_write_lba(us, lba, page, pages, buffer,
  907. blockbuffer);
  908. if (result != USB_STOR_TRANSPORT_GOOD)
  909. break;
  910. page = 0;
  911. lba++;
  912. sectors -= pages;
  913. }
  914. kfree(buffer);
  915. kfree(blockbuffer);
  916. return result;
  917. }
  918. /*
  919. * Our interface with the rest of the world
  920. */
  921. static void alauda_info_destructor(void *extra)
  922. {
  923. struct alauda_info *info = (struct alauda_info *) extra;
  924. int port;
  925. if (!info)
  926. return;
  927. for (port = 0; port < 2; port++) {
  928. struct alauda_media_info *media_info = &info->port[port];
  929. alauda_free_maps(media_info);
  930. kfree(media_info->lba_to_pba);
  931. kfree(media_info->pba_to_lba);
  932. }
  933. }
  934. /*
  935. * Initialize alauda_info struct and find the data-write endpoint
  936. */
  937. static int init_alauda(struct us_data *us)
  938. {
  939. struct alauda_info *info;
  940. struct usb_host_interface *altsetting = us->pusb_intf->cur_altsetting;
  941. nand_init_ecc();
  942. us->extra = kzalloc(sizeof(struct alauda_info), GFP_NOIO);
  943. if (!us->extra) {
  944. US_DEBUGP("init_alauda: Gah! Can't allocate storage for"
  945. "alauda info struct!\n");
  946. return USB_STOR_TRANSPORT_ERROR;
  947. }
  948. info = (struct alauda_info *) us->extra;
  949. us->extra_destructor = alauda_info_destructor;
  950. info->wr_ep = usb_sndbulkpipe(us->pusb_dev,
  951. altsetting->endpoint[0].desc.bEndpointAddress
  952. & USB_ENDPOINT_NUMBER_MASK);
  953. return USB_STOR_TRANSPORT_GOOD;
  954. }
  955. static int alauda_transport(struct scsi_cmnd *srb, struct us_data *us)
  956. {
  957. int rc;
  958. struct alauda_info *info = (struct alauda_info *) us->extra;
  959. unsigned char *ptr = us->iobuf;
  960. static unsigned char inquiry_response[36] = {
  961. 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
  962. };
  963. if (srb->cmnd[0] == INQUIRY) {
  964. US_DEBUGP("alauda_transport: INQUIRY. "
  965. "Returning bogus response.\n");
  966. memcpy(ptr, inquiry_response, sizeof(inquiry_response));
  967. fill_inquiry_response(us, ptr, 36);
  968. return USB_STOR_TRANSPORT_GOOD;
  969. }
  970. if (srb->cmnd[0] == TEST_UNIT_READY) {
  971. US_DEBUGP("alauda_transport: TEST_UNIT_READY.\n");
  972. return alauda_check_media(us);
  973. }
  974. if (srb->cmnd[0] == READ_CAPACITY) {
  975. unsigned int num_zones;
  976. unsigned long capacity;
  977. rc = alauda_check_media(us);
  978. if (rc != USB_STOR_TRANSPORT_GOOD)
  979. return rc;
  980. num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift
  981. + MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift);
  982. capacity = num_zones * MEDIA_INFO(us).uzonesize
  983. * MEDIA_INFO(us).blocksize;
  984. /* Report capacity and page size */
  985. ((__be32 *) ptr)[0] = cpu_to_be32(capacity - 1);
  986. ((__be32 *) ptr)[1] = cpu_to_be32(512);
  987. usb_stor_set_xfer_buf(ptr, 8, srb);
  988. return USB_STOR_TRANSPORT_GOOD;
  989. }
  990. if (srb->cmnd[0] == READ_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. US_DEBUGP("alauda_transport: READ_10: page %d pagect %d\n",
  1000. page, pages);
  1001. return alauda_read_data(us, page, pages);
  1002. }
  1003. if (srb->cmnd[0] == WRITE_10) {
  1004. unsigned int page, pages;
  1005. rc = alauda_check_media(us);
  1006. if (rc != USB_STOR_TRANSPORT_GOOD)
  1007. return rc;
  1008. page = short_pack(srb->cmnd[3], srb->cmnd[2]);
  1009. page <<= 16;
  1010. page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
  1011. pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
  1012. US_DEBUGP("alauda_transport: WRITE_10: page %d pagect %d\n",
  1013. page, pages);
  1014. return alauda_write_data(us, page, pages);
  1015. }
  1016. if (srb->cmnd[0] == REQUEST_SENSE) {
  1017. US_DEBUGP("alauda_transport: REQUEST_SENSE.\n");
  1018. memset(ptr, 0, 18);
  1019. ptr[0] = 0xF0;
  1020. ptr[2] = info->sense_key;
  1021. ptr[7] = 11;
  1022. ptr[12] = info->sense_asc;
  1023. ptr[13] = info->sense_ascq;
  1024. usb_stor_set_xfer_buf(ptr, 18, srb);
  1025. return USB_STOR_TRANSPORT_GOOD;
  1026. }
  1027. if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
  1028. /* sure. whatever. not like we can stop the user from popping
  1029. the media out of the device (no locking doors, etc) */
  1030. return USB_STOR_TRANSPORT_GOOD;
  1031. }
  1032. US_DEBUGP("alauda_transport: Gah! Unknown command: %d (0x%x)\n",
  1033. srb->cmnd[0], srb->cmnd[0]);
  1034. info->sense_key = 0x05;
  1035. info->sense_asc = 0x20;
  1036. info->sense_ascq = 0x00;
  1037. return USB_STOR_TRANSPORT_FAILED;
  1038. }
  1039. static int alauda_probe(struct usb_interface *intf,
  1040. const struct usb_device_id *id)
  1041. {
  1042. struct us_data *us;
  1043. int result;
  1044. result = usb_stor_probe1(&us, intf, id,
  1045. (id - alauda_usb_ids) + alauda_unusual_dev_list);
  1046. if (result)
  1047. return result;
  1048. us->transport_name = "Alauda Control/Bulk";
  1049. us->transport = alauda_transport;
  1050. us->transport_reset = usb_stor_Bulk_reset;
  1051. us->max_lun = 1;
  1052. result = usb_stor_probe2(us);
  1053. return result;
  1054. }
  1055. static struct usb_driver alauda_driver = {
  1056. .name = "ums-alauda",
  1057. .probe = alauda_probe,
  1058. .disconnect = usb_stor_disconnect,
  1059. .suspend = usb_stor_suspend,
  1060. .resume = usb_stor_resume,
  1061. .reset_resume = usb_stor_reset_resume,
  1062. .pre_reset = usb_stor_pre_reset,
  1063. .post_reset = usb_stor_post_reset,
  1064. .id_table = alauda_usb_ids,
  1065. .soft_unbind = 1,
  1066. .no_dynamic_id = 1,
  1067. };
  1068. module_usb_driver(alauda_driver);