shuttle_usbat.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874
  1. /* Driver for SCM Microsystems (a.k.a. Shuttle) USB-ATAPI cable
  2. *
  3. * Current development and maintenance by:
  4. * (c) 2000, 2001 Robert Baruch (autophile@starband.net)
  5. * (c) 2004, 2005 Daniel Drake <dsd@gentoo.org>
  6. *
  7. * Developed with the assistance of:
  8. * (c) 2002 Alan Stern <stern@rowland.org>
  9. *
  10. * Flash support based on earlier work by:
  11. * (c) 2002 Thomas Kreiling <usbdev@sm04.de>
  12. *
  13. * Many originally ATAPI devices were slightly modified to meet the USB
  14. * market by using some kind of translation from ATAPI to USB on the host,
  15. * and the peripheral would translate from USB back to ATAPI.
  16. *
  17. * SCM Microsystems (www.scmmicro.com) makes a device, sold to OEM's only,
  18. * which does the USB-to-ATAPI conversion. By obtaining the data sheet on
  19. * their device under nondisclosure agreement, I have been able to write
  20. * this driver for Linux.
  21. *
  22. * The chip used in the device can also be used for EPP and ISA translation
  23. * as well. This driver is only guaranteed to work with the ATAPI
  24. * translation.
  25. *
  26. * See the Kconfig help text for a list of devices known to be supported by
  27. * this driver.
  28. *
  29. * This program is free software; you can redistribute it and/or modify it
  30. * under the terms of the GNU General Public License as published by the
  31. * Free Software Foundation; either version 2, or (at your option) any
  32. * later version.
  33. *
  34. * This program is distributed in the hope that it will be useful, but
  35. * WITHOUT ANY WARRANTY; without even the implied warranty of
  36. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  37. * General Public License for more details.
  38. *
  39. * You should have received a copy of the GNU General Public License along
  40. * with this program; if not, write to the Free Software Foundation, Inc.,
  41. * 675 Mass Ave, Cambridge, MA 02139, USA.
  42. */
  43. #include <linux/errno.h>
  44. #include <linux/module.h>
  45. #include <linux/slab.h>
  46. #include <linux/cdrom.h>
  47. #include <scsi/scsi.h>
  48. #include <scsi/scsi_cmnd.h>
  49. #include "usb.h"
  50. #include "transport.h"
  51. #include "protocol.h"
  52. #include "debug.h"
  53. MODULE_DESCRIPTION("Driver for SCM Microsystems (a.k.a. Shuttle) USB-ATAPI cable");
  54. MODULE_AUTHOR("Daniel Drake <dsd@gentoo.org>, Robert Baruch <autophile@starband.net>");
  55. MODULE_LICENSE("GPL");
  56. /* Supported device types */
  57. #define USBAT_DEV_HP8200 0x01
  58. #define USBAT_DEV_FLASH 0x02
  59. #define USBAT_EPP_PORT 0x10
  60. #define USBAT_EPP_REGISTER 0x30
  61. #define USBAT_ATA 0x40
  62. #define USBAT_ISA 0x50
  63. /* Commands (need to be logically OR'd with an access type */
  64. #define USBAT_CMD_READ_REG 0x00
  65. #define USBAT_CMD_WRITE_REG 0x01
  66. #define USBAT_CMD_READ_BLOCK 0x02
  67. #define USBAT_CMD_WRITE_BLOCK 0x03
  68. #define USBAT_CMD_COND_READ_BLOCK 0x04
  69. #define USBAT_CMD_COND_WRITE_BLOCK 0x05
  70. #define USBAT_CMD_WRITE_REGS 0x07
  71. /* Commands (these don't need an access type) */
  72. #define USBAT_CMD_EXEC_CMD 0x80
  73. #define USBAT_CMD_SET_FEAT 0x81
  74. #define USBAT_CMD_UIO 0x82
  75. /* Methods of accessing UIO register */
  76. #define USBAT_UIO_READ 1
  77. #define USBAT_UIO_WRITE 0
  78. /* Qualifier bits */
  79. #define USBAT_QUAL_FCQ 0x20 /* full compare */
  80. #define USBAT_QUAL_ALQ 0x10 /* auto load subcount */
  81. /* USBAT Flash Media status types */
  82. #define USBAT_FLASH_MEDIA_NONE 0
  83. #define USBAT_FLASH_MEDIA_CF 1
  84. /* USBAT Flash Media change types */
  85. #define USBAT_FLASH_MEDIA_SAME 0
  86. #define USBAT_FLASH_MEDIA_CHANGED 1
  87. /* USBAT ATA registers */
  88. #define USBAT_ATA_DATA 0x10 /* read/write data (R/W) */
  89. #define USBAT_ATA_FEATURES 0x11 /* set features (W) */
  90. #define USBAT_ATA_ERROR 0x11 /* error (R) */
  91. #define USBAT_ATA_SECCNT 0x12 /* sector count (R/W) */
  92. #define USBAT_ATA_SECNUM 0x13 /* sector number (R/W) */
  93. #define USBAT_ATA_LBA_ME 0x14 /* cylinder low (R/W) */
  94. #define USBAT_ATA_LBA_HI 0x15 /* cylinder high (R/W) */
  95. #define USBAT_ATA_DEVICE 0x16 /* head/device selection (R/W) */
  96. #define USBAT_ATA_STATUS 0x17 /* device status (R) */
  97. #define USBAT_ATA_CMD 0x17 /* device command (W) */
  98. #define USBAT_ATA_ALTSTATUS 0x0E /* status (no clear IRQ) (R) */
  99. /* USBAT User I/O Data registers */
  100. #define USBAT_UIO_EPAD 0x80 /* Enable Peripheral Control Signals */
  101. #define USBAT_UIO_CDT 0x40 /* Card Detect (Read Only) */
  102. /* CDT = ACKD & !UI1 & !UI0 */
  103. #define USBAT_UIO_1 0x20 /* I/O 1 */
  104. #define USBAT_UIO_0 0x10 /* I/O 0 */
  105. #define USBAT_UIO_EPP_ATA 0x08 /* 1=EPP mode, 0=ATA mode */
  106. #define USBAT_UIO_UI1 0x04 /* Input 1 */
  107. #define USBAT_UIO_UI0 0x02 /* Input 0 */
  108. #define USBAT_UIO_INTR_ACK 0x01 /* Interrupt (ATA/ISA)/Acknowledge (EPP) */
  109. /* USBAT User I/O Enable registers */
  110. #define USBAT_UIO_DRVRST 0x80 /* Reset Peripheral */
  111. #define USBAT_UIO_ACKD 0x40 /* Enable Card Detect */
  112. #define USBAT_UIO_OE1 0x20 /* I/O 1 set=output/clr=input */
  113. /* If ACKD=1, set OE1 to 1 also. */
  114. #define USBAT_UIO_OE0 0x10 /* I/O 0 set=output/clr=input */
  115. #define USBAT_UIO_ADPRST 0x01 /* Reset SCM chip */
  116. /* USBAT Features */
  117. #define USBAT_FEAT_ETEN 0x80 /* External trigger enable */
  118. #define USBAT_FEAT_U1 0x08
  119. #define USBAT_FEAT_U0 0x04
  120. #define USBAT_FEAT_ET1 0x02
  121. #define USBAT_FEAT_ET2 0x01
  122. struct usbat_info {
  123. int devicetype;
  124. /* Used for Flash readers only */
  125. unsigned long sectors; /* total sector count */
  126. unsigned long ssize; /* sector size in bytes */
  127. unsigned char sense_key;
  128. unsigned long sense_asc; /* additional sense code */
  129. unsigned long sense_ascq; /* additional sense code qualifier */
  130. };
  131. #define short_pack(LSB,MSB) ( ((u16)(LSB)) | ( ((u16)(MSB))<<8 ) )
  132. #define LSB_of(s) ((s)&0xFF)
  133. #define MSB_of(s) ((s)>>8)
  134. static int transferred = 0;
  135. static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us);
  136. static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us);
  137. static int init_usbat_cd(struct us_data *us);
  138. static int init_usbat_flash(struct us_data *us);
  139. /*
  140. * The table of devices
  141. */
  142. #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
  143. vendorName, productName, useProtocol, useTransport, \
  144. initFunction, flags) \
  145. { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
  146. .driver_info = (flags) }
  147. static struct usb_device_id usbat_usb_ids[] = {
  148. # include "unusual_usbat.h"
  149. { } /* Terminating entry */
  150. };
  151. MODULE_DEVICE_TABLE(usb, usbat_usb_ids);
  152. #undef UNUSUAL_DEV
  153. /*
  154. * The flags table
  155. */
  156. #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
  157. vendor_name, product_name, use_protocol, use_transport, \
  158. init_function, Flags) \
  159. { \
  160. .vendorName = vendor_name, \
  161. .productName = product_name, \
  162. .useProtocol = use_protocol, \
  163. .useTransport = use_transport, \
  164. .initFunction = init_function, \
  165. }
  166. static struct us_unusual_dev usbat_unusual_dev_list[] = {
  167. # include "unusual_usbat.h"
  168. { } /* Terminating entry */
  169. };
  170. #undef UNUSUAL_DEV
  171. /*
  172. * Convenience function to produce an ATA read/write sectors command
  173. * Use cmd=0x20 for read, cmd=0x30 for write
  174. */
  175. static void usbat_pack_ata_sector_cmd(unsigned char *buf,
  176. unsigned char thistime,
  177. u32 sector, unsigned char cmd)
  178. {
  179. buf[0] = 0;
  180. buf[1] = thistime;
  181. buf[2] = sector & 0xFF;
  182. buf[3] = (sector >> 8) & 0xFF;
  183. buf[4] = (sector >> 16) & 0xFF;
  184. buf[5] = 0xE0 | ((sector >> 24) & 0x0F);
  185. buf[6] = cmd;
  186. }
  187. /*
  188. * Convenience function to get the device type (flash or hp8200)
  189. */
  190. static int usbat_get_device_type(struct us_data *us)
  191. {
  192. return ((struct usbat_info*)us->extra)->devicetype;
  193. }
  194. /*
  195. * Read a register from the device
  196. */
  197. static int usbat_read(struct us_data *us,
  198. unsigned char access,
  199. unsigned char reg,
  200. unsigned char *content)
  201. {
  202. return usb_stor_ctrl_transfer(us,
  203. us->recv_ctrl_pipe,
  204. access | USBAT_CMD_READ_REG,
  205. 0xC0,
  206. (u16)reg,
  207. 0,
  208. content,
  209. 1);
  210. }
  211. /*
  212. * Write to a register on the device
  213. */
  214. static int usbat_write(struct us_data *us,
  215. unsigned char access,
  216. unsigned char reg,
  217. unsigned char content)
  218. {
  219. return usb_stor_ctrl_transfer(us,
  220. us->send_ctrl_pipe,
  221. access | USBAT_CMD_WRITE_REG,
  222. 0x40,
  223. short_pack(reg, content),
  224. 0,
  225. NULL,
  226. 0);
  227. }
  228. /*
  229. * Convenience function to perform a bulk read
  230. */
  231. static int usbat_bulk_read(struct us_data *us,
  232. void* buf,
  233. unsigned int len,
  234. int use_sg)
  235. {
  236. if (len == 0)
  237. return USB_STOR_XFER_GOOD;
  238. usb_stor_dbg(us, "len = %d\n", len);
  239. return usb_stor_bulk_transfer_sg(us, us->recv_bulk_pipe, buf, len, use_sg, NULL);
  240. }
  241. /*
  242. * Convenience function to perform a bulk write
  243. */
  244. static int usbat_bulk_write(struct us_data *us,
  245. void* buf,
  246. unsigned int len,
  247. int use_sg)
  248. {
  249. if (len == 0)
  250. return USB_STOR_XFER_GOOD;
  251. usb_stor_dbg(us, "len = %d\n", len);
  252. return usb_stor_bulk_transfer_sg(us, us->send_bulk_pipe, buf, len, use_sg, NULL);
  253. }
  254. /*
  255. * Some USBAT-specific commands can only be executed over a command transport
  256. * This transport allows one (len=8) or two (len=16) vendor-specific commands
  257. * to be executed.
  258. */
  259. static int usbat_execute_command(struct us_data *us,
  260. unsigned char *commands,
  261. unsigned int len)
  262. {
  263. return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
  264. USBAT_CMD_EXEC_CMD, 0x40, 0, 0,
  265. commands, len);
  266. }
  267. /*
  268. * Read the status register
  269. */
  270. static int usbat_get_status(struct us_data *us, unsigned char *status)
  271. {
  272. int rc;
  273. rc = usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status);
  274. usb_stor_dbg(us, "0x%02X\n", *status);
  275. return rc;
  276. }
  277. /*
  278. * Check the device status
  279. */
  280. static int usbat_check_status(struct us_data *us)
  281. {
  282. unsigned char *reply = us->iobuf;
  283. int rc;
  284. rc = usbat_get_status(us, reply);
  285. if (rc != USB_STOR_XFER_GOOD)
  286. return USB_STOR_TRANSPORT_FAILED;
  287. /* error/check condition (0x51 is ok) */
  288. if (*reply & 0x01 && *reply != 0x51)
  289. return USB_STOR_TRANSPORT_FAILED;
  290. /* device fault */
  291. if (*reply & 0x20)
  292. return USB_STOR_TRANSPORT_FAILED;
  293. return USB_STOR_TRANSPORT_GOOD;
  294. }
  295. /*
  296. * Stores critical information in internal registers in preparation for the execution
  297. * of a conditional usbat_read_blocks or usbat_write_blocks call.
  298. */
  299. static int usbat_set_shuttle_features(struct us_data *us,
  300. unsigned char external_trigger,
  301. unsigned char epp_control,
  302. unsigned char mask_byte,
  303. unsigned char test_pattern,
  304. unsigned char subcountH,
  305. unsigned char subcountL)
  306. {
  307. unsigned char *command = us->iobuf;
  308. command[0] = 0x40;
  309. command[1] = USBAT_CMD_SET_FEAT;
  310. /*
  311. * The only bit relevant to ATA access is bit 6
  312. * which defines 8 bit data access (set) or 16 bit (unset)
  313. */
  314. command[2] = epp_control;
  315. /*
  316. * If FCQ is set in the qualifier (defined in R/W cmd), then bits U0, U1,
  317. * ET1 and ET2 define an external event to be checked for on event of a
  318. * _read_blocks or _write_blocks operation. The read/write will not take
  319. * place unless the defined trigger signal is active.
  320. */
  321. command[3] = external_trigger;
  322. /*
  323. * The resultant byte of the mask operation (see mask_byte) is compared for
  324. * equivalence with this test pattern. If equal, the read/write will take
  325. * place.
  326. */
  327. command[4] = test_pattern;
  328. /*
  329. * This value is logically ANDed with the status register field specified
  330. * in the read/write command.
  331. */
  332. command[5] = mask_byte;
  333. /*
  334. * If ALQ is set in the qualifier, this field contains the address of the
  335. * registers where the byte count should be read for transferring the data.
  336. * If ALQ is not set, then this field contains the number of bytes to be
  337. * transferred.
  338. */
  339. command[6] = subcountL;
  340. command[7] = subcountH;
  341. return usbat_execute_command(us, command, 8);
  342. }
  343. /*
  344. * Block, waiting for an ATA device to become not busy or to report
  345. * an error condition.
  346. */
  347. static int usbat_wait_not_busy(struct us_data *us, int minutes)
  348. {
  349. int i;
  350. int result;
  351. unsigned char *status = us->iobuf;
  352. /* Synchronizing cache on a CDR could take a heck of a long time,
  353. * but probably not more than 10 minutes or so. On the other hand,
  354. * doing a full blank on a CDRW at speed 1 will take about 75
  355. * minutes!
  356. */
  357. for (i=0; i<1200+minutes*60; i++) {
  358. result = usbat_get_status(us, status);
  359. if (result!=USB_STOR_XFER_GOOD)
  360. return USB_STOR_TRANSPORT_ERROR;
  361. if (*status & 0x01) { /* check condition */
  362. result = usbat_read(us, USBAT_ATA, 0x10, status);
  363. return USB_STOR_TRANSPORT_FAILED;
  364. }
  365. if (*status & 0x20) /* device fault */
  366. return USB_STOR_TRANSPORT_FAILED;
  367. if ((*status & 0x80)==0x00) { /* not busy */
  368. usb_stor_dbg(us, "Waited not busy for %d steps\n", i);
  369. return USB_STOR_TRANSPORT_GOOD;
  370. }
  371. if (i<500)
  372. msleep(10); /* 5 seconds */
  373. else if (i<700)
  374. msleep(50); /* 10 seconds */
  375. else if (i<1200)
  376. msleep(100); /* 50 seconds */
  377. else
  378. msleep(1000); /* X minutes */
  379. }
  380. usb_stor_dbg(us, "Waited not busy for %d minutes, timing out\n",
  381. minutes);
  382. return USB_STOR_TRANSPORT_FAILED;
  383. }
  384. /*
  385. * Read block data from the data register
  386. */
  387. static int usbat_read_block(struct us_data *us,
  388. void* buf,
  389. unsigned short len,
  390. int use_sg)
  391. {
  392. int result;
  393. unsigned char *command = us->iobuf;
  394. if (!len)
  395. return USB_STOR_TRANSPORT_GOOD;
  396. command[0] = 0xC0;
  397. command[1] = USBAT_ATA | USBAT_CMD_READ_BLOCK;
  398. command[2] = USBAT_ATA_DATA;
  399. command[3] = 0;
  400. command[4] = 0;
  401. command[5] = 0;
  402. command[6] = LSB_of(len);
  403. command[7] = MSB_of(len);
  404. result = usbat_execute_command(us, command, 8);
  405. if (result != USB_STOR_XFER_GOOD)
  406. return USB_STOR_TRANSPORT_ERROR;
  407. result = usbat_bulk_read(us, buf, len, use_sg);
  408. return (result == USB_STOR_XFER_GOOD ?
  409. USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_ERROR);
  410. }
  411. /*
  412. * Write block data via the data register
  413. */
  414. static int usbat_write_block(struct us_data *us,
  415. unsigned char access,
  416. void* buf,
  417. unsigned short len,
  418. int minutes,
  419. int use_sg)
  420. {
  421. int result;
  422. unsigned char *command = us->iobuf;
  423. if (!len)
  424. return USB_STOR_TRANSPORT_GOOD;
  425. command[0] = 0x40;
  426. command[1] = access | USBAT_CMD_WRITE_BLOCK;
  427. command[2] = USBAT_ATA_DATA;
  428. command[3] = 0;
  429. command[4] = 0;
  430. command[5] = 0;
  431. command[6] = LSB_of(len);
  432. command[7] = MSB_of(len);
  433. result = usbat_execute_command(us, command, 8);
  434. if (result != USB_STOR_XFER_GOOD)
  435. return USB_STOR_TRANSPORT_ERROR;
  436. result = usbat_bulk_write(us, buf, len, use_sg);
  437. if (result != USB_STOR_XFER_GOOD)
  438. return USB_STOR_TRANSPORT_ERROR;
  439. return usbat_wait_not_busy(us, minutes);
  440. }
  441. /*
  442. * Process read and write requests
  443. */
  444. static int usbat_hp8200e_rw_block_test(struct us_data *us,
  445. unsigned char access,
  446. unsigned char *registers,
  447. unsigned char *data_out,
  448. unsigned short num_registers,
  449. unsigned char data_reg,
  450. unsigned char status_reg,
  451. unsigned char timeout,
  452. unsigned char qualifier,
  453. int direction,
  454. void *buf,
  455. unsigned short len,
  456. int use_sg,
  457. int minutes)
  458. {
  459. int result;
  460. unsigned int pipe = (direction == DMA_FROM_DEVICE) ?
  461. us->recv_bulk_pipe : us->send_bulk_pipe;
  462. unsigned char *command = us->iobuf;
  463. int i, j;
  464. int cmdlen;
  465. unsigned char *data = us->iobuf;
  466. unsigned char *status = us->iobuf;
  467. BUG_ON(num_registers > US_IOBUF_SIZE/2);
  468. for (i=0; i<20; i++) {
  469. /*
  470. * The first time we send the full command, which consists
  471. * of downloading the SCSI command followed by downloading
  472. * the data via a write-and-test. Any other time we only
  473. * send the command to download the data -- the SCSI command
  474. * is still 'active' in some sense in the device.
  475. *
  476. * We're only going to try sending the data 10 times. After
  477. * that, we just return a failure.
  478. */
  479. if (i==0) {
  480. cmdlen = 16;
  481. /*
  482. * Write to multiple registers
  483. * Not really sure the 0x07, 0x17, 0xfc, 0xe7 is
  484. * necessary here, but that's what came out of the
  485. * trace every single time.
  486. */
  487. command[0] = 0x40;
  488. command[1] = access | USBAT_CMD_WRITE_REGS;
  489. command[2] = 0x07;
  490. command[3] = 0x17;
  491. command[4] = 0xFC;
  492. command[5] = 0xE7;
  493. command[6] = LSB_of(num_registers*2);
  494. command[7] = MSB_of(num_registers*2);
  495. } else
  496. cmdlen = 8;
  497. /* Conditionally read or write blocks */
  498. command[cmdlen-8] = (direction==DMA_TO_DEVICE ? 0x40 : 0xC0);
  499. command[cmdlen-7] = access |
  500. (direction==DMA_TO_DEVICE ?
  501. USBAT_CMD_COND_WRITE_BLOCK : USBAT_CMD_COND_READ_BLOCK);
  502. command[cmdlen-6] = data_reg;
  503. command[cmdlen-5] = status_reg;
  504. command[cmdlen-4] = timeout;
  505. command[cmdlen-3] = qualifier;
  506. command[cmdlen-2] = LSB_of(len);
  507. command[cmdlen-1] = MSB_of(len);
  508. result = usbat_execute_command(us, command, cmdlen);
  509. if (result != USB_STOR_XFER_GOOD)
  510. return USB_STOR_TRANSPORT_ERROR;
  511. if (i==0) {
  512. for (j=0; j<num_registers; j++) {
  513. data[j<<1] = registers[j];
  514. data[1+(j<<1)] = data_out[j];
  515. }
  516. result = usbat_bulk_write(us, data, num_registers*2, 0);
  517. if (result != USB_STOR_XFER_GOOD)
  518. return USB_STOR_TRANSPORT_ERROR;
  519. }
  520. result = usb_stor_bulk_transfer_sg(us,
  521. pipe, buf, len, use_sg, NULL);
  522. /*
  523. * If we get a stall on the bulk download, we'll retry
  524. * the bulk download -- but not the SCSI command because
  525. * in some sense the SCSI command is still 'active' and
  526. * waiting for the data. Don't ask me why this should be;
  527. * I'm only following what the Windoze driver did.
  528. *
  529. * Note that a stall for the test-and-read/write command means
  530. * that the test failed. In this case we're testing to make
  531. * sure that the device is error-free
  532. * (i.e. bit 0 -- CHK -- of status is 0). The most likely
  533. * hypothesis is that the USBAT chip somehow knows what
  534. * the device will accept, but doesn't give the device any
  535. * data until all data is received. Thus, the device would
  536. * still be waiting for the first byte of data if a stall
  537. * occurs, even if the stall implies that some data was
  538. * transferred.
  539. */
  540. if (result == USB_STOR_XFER_SHORT ||
  541. result == USB_STOR_XFER_STALLED) {
  542. /*
  543. * If we're reading and we stalled, then clear
  544. * the bulk output pipe only the first time.
  545. */
  546. if (direction==DMA_FROM_DEVICE && i==0) {
  547. if (usb_stor_clear_halt(us,
  548. us->send_bulk_pipe) < 0)
  549. return USB_STOR_TRANSPORT_ERROR;
  550. }
  551. /*
  552. * Read status: is the device angry, or just busy?
  553. */
  554. result = usbat_read(us, USBAT_ATA,
  555. direction==DMA_TO_DEVICE ?
  556. USBAT_ATA_STATUS : USBAT_ATA_ALTSTATUS,
  557. status);
  558. if (result!=USB_STOR_XFER_GOOD)
  559. return USB_STOR_TRANSPORT_ERROR;
  560. if (*status & 0x01) /* check condition */
  561. return USB_STOR_TRANSPORT_FAILED;
  562. if (*status & 0x20) /* device fault */
  563. return USB_STOR_TRANSPORT_FAILED;
  564. usb_stor_dbg(us, "Redoing %s\n",
  565. direction == DMA_TO_DEVICE
  566. ? "write" : "read");
  567. } else if (result != USB_STOR_XFER_GOOD)
  568. return USB_STOR_TRANSPORT_ERROR;
  569. else
  570. return usbat_wait_not_busy(us, minutes);
  571. }
  572. usb_stor_dbg(us, "Bummer! %s bulk data 20 times failed\n",
  573. direction == DMA_TO_DEVICE ? "Writing" : "Reading");
  574. return USB_STOR_TRANSPORT_FAILED;
  575. }
  576. /*
  577. * Write to multiple registers:
  578. * Allows us to write specific data to any registers. The data to be written
  579. * gets packed in this sequence: reg0, data0, reg1, data1, ..., regN, dataN
  580. * which gets sent through bulk out.
  581. * Not designed for large transfers of data!
  582. */
  583. static int usbat_multiple_write(struct us_data *us,
  584. unsigned char *registers,
  585. unsigned char *data_out,
  586. unsigned short num_registers)
  587. {
  588. int i, result;
  589. unsigned char *data = us->iobuf;
  590. unsigned char *command = us->iobuf;
  591. BUG_ON(num_registers > US_IOBUF_SIZE/2);
  592. /* Write to multiple registers, ATA access */
  593. command[0] = 0x40;
  594. command[1] = USBAT_ATA | USBAT_CMD_WRITE_REGS;
  595. /* No relevance */
  596. command[2] = 0;
  597. command[3] = 0;
  598. command[4] = 0;
  599. command[5] = 0;
  600. /* Number of bytes to be transferred (incl. addresses and data) */
  601. command[6] = LSB_of(num_registers*2);
  602. command[7] = MSB_of(num_registers*2);
  603. /* The setup command */
  604. result = usbat_execute_command(us, command, 8);
  605. if (result != USB_STOR_XFER_GOOD)
  606. return USB_STOR_TRANSPORT_ERROR;
  607. /* Create the reg/data, reg/data sequence */
  608. for (i=0; i<num_registers; i++) {
  609. data[i<<1] = registers[i];
  610. data[1+(i<<1)] = data_out[i];
  611. }
  612. /* Send the data */
  613. result = usbat_bulk_write(us, data, num_registers*2, 0);
  614. if (result != USB_STOR_XFER_GOOD)
  615. return USB_STOR_TRANSPORT_ERROR;
  616. if (usbat_get_device_type(us) == USBAT_DEV_HP8200)
  617. return usbat_wait_not_busy(us, 0);
  618. else
  619. return USB_STOR_TRANSPORT_GOOD;
  620. }
  621. /*
  622. * Conditionally read blocks from device:
  623. * Allows us to read blocks from a specific data register, based upon the
  624. * condition that a status register can be successfully masked with a status
  625. * qualifier. If this condition is not initially met, the read will wait
  626. * up until a maximum amount of time has elapsed, as specified by timeout.
  627. * The read will start when the condition is met, otherwise the command aborts.
  628. *
  629. * The qualifier defined here is not the value that is masked, it defines
  630. * conditions for the write to take place. The actual masked qualifier (and
  631. * other related details) are defined beforehand with _set_shuttle_features().
  632. */
  633. static int usbat_read_blocks(struct us_data *us,
  634. void* buffer,
  635. int len,
  636. int use_sg)
  637. {
  638. int result;
  639. unsigned char *command = us->iobuf;
  640. command[0] = 0xC0;
  641. command[1] = USBAT_ATA | USBAT_CMD_COND_READ_BLOCK;
  642. command[2] = USBAT_ATA_DATA;
  643. command[3] = USBAT_ATA_STATUS;
  644. command[4] = 0xFD; /* Timeout (ms); */
  645. command[5] = USBAT_QUAL_FCQ;
  646. command[6] = LSB_of(len);
  647. command[7] = MSB_of(len);
  648. /* Multiple block read setup command */
  649. result = usbat_execute_command(us, command, 8);
  650. if (result != USB_STOR_XFER_GOOD)
  651. return USB_STOR_TRANSPORT_FAILED;
  652. /* Read the blocks we just asked for */
  653. result = usbat_bulk_read(us, buffer, len, use_sg);
  654. if (result != USB_STOR_XFER_GOOD)
  655. return USB_STOR_TRANSPORT_FAILED;
  656. return USB_STOR_TRANSPORT_GOOD;
  657. }
  658. /*
  659. * Conditionally write blocks to device:
  660. * Allows us to write blocks to a specific data register, based upon the
  661. * condition that a status register can be successfully masked with a status
  662. * qualifier. If this condition is not initially met, the write will wait
  663. * up until a maximum amount of time has elapsed, as specified by timeout.
  664. * The read will start when the condition is met, otherwise the command aborts.
  665. *
  666. * The qualifier defined here is not the value that is masked, it defines
  667. * conditions for the write to take place. The actual masked qualifier (and
  668. * other related details) are defined beforehand with _set_shuttle_features().
  669. */
  670. static int usbat_write_blocks(struct us_data *us,
  671. void* buffer,
  672. int len,
  673. int use_sg)
  674. {
  675. int result;
  676. unsigned char *command = us->iobuf;
  677. command[0] = 0x40;
  678. command[1] = USBAT_ATA | USBAT_CMD_COND_WRITE_BLOCK;
  679. command[2] = USBAT_ATA_DATA;
  680. command[3] = USBAT_ATA_STATUS;
  681. command[4] = 0xFD; /* Timeout (ms) */
  682. command[5] = USBAT_QUAL_FCQ;
  683. command[6] = LSB_of(len);
  684. command[7] = MSB_of(len);
  685. /* Multiple block write setup command */
  686. result = usbat_execute_command(us, command, 8);
  687. if (result != USB_STOR_XFER_GOOD)
  688. return USB_STOR_TRANSPORT_FAILED;
  689. /* Write the data */
  690. result = usbat_bulk_write(us, buffer, len, use_sg);
  691. if (result != USB_STOR_XFER_GOOD)
  692. return USB_STOR_TRANSPORT_FAILED;
  693. return USB_STOR_TRANSPORT_GOOD;
  694. }
  695. /*
  696. * Read the User IO register
  697. */
  698. static int usbat_read_user_io(struct us_data *us, unsigned char *data_flags)
  699. {
  700. int result;
  701. result = usb_stor_ctrl_transfer(us,
  702. us->recv_ctrl_pipe,
  703. USBAT_CMD_UIO,
  704. 0xC0,
  705. 0,
  706. 0,
  707. data_flags,
  708. USBAT_UIO_READ);
  709. usb_stor_dbg(us, "UIO register reads %02X\n", *data_flags);
  710. return result;
  711. }
  712. /*
  713. * Write to the User IO register
  714. */
  715. static int usbat_write_user_io(struct us_data *us,
  716. unsigned char enable_flags,
  717. unsigned char data_flags)
  718. {
  719. return usb_stor_ctrl_transfer(us,
  720. us->send_ctrl_pipe,
  721. USBAT_CMD_UIO,
  722. 0x40,
  723. short_pack(enable_flags, data_flags),
  724. 0,
  725. NULL,
  726. USBAT_UIO_WRITE);
  727. }
  728. /*
  729. * Reset the device
  730. * Often needed on media change.
  731. */
  732. static int usbat_device_reset(struct us_data *us)
  733. {
  734. int rc;
  735. /*
  736. * Reset peripheral, enable peripheral control signals
  737. * (bring reset signal up)
  738. */
  739. rc = usbat_write_user_io(us,
  740. USBAT_UIO_DRVRST | USBAT_UIO_OE1 | USBAT_UIO_OE0,
  741. USBAT_UIO_EPAD | USBAT_UIO_1);
  742. if (rc != USB_STOR_XFER_GOOD)
  743. return USB_STOR_TRANSPORT_ERROR;
  744. /*
  745. * Enable peripheral control signals
  746. * (bring reset signal down)
  747. */
  748. rc = usbat_write_user_io(us,
  749. USBAT_UIO_OE1 | USBAT_UIO_OE0,
  750. USBAT_UIO_EPAD | USBAT_UIO_1);
  751. if (rc != USB_STOR_XFER_GOOD)
  752. return USB_STOR_TRANSPORT_ERROR;
  753. return USB_STOR_TRANSPORT_GOOD;
  754. }
  755. /*
  756. * Enable card detect
  757. */
  758. static int usbat_device_enable_cdt(struct us_data *us)
  759. {
  760. int rc;
  761. /* Enable peripheral control signals and card detect */
  762. rc = usbat_write_user_io(us,
  763. USBAT_UIO_ACKD | USBAT_UIO_OE1 | USBAT_UIO_OE0,
  764. USBAT_UIO_EPAD | USBAT_UIO_1);
  765. if (rc != USB_STOR_XFER_GOOD)
  766. return USB_STOR_TRANSPORT_ERROR;
  767. return USB_STOR_TRANSPORT_GOOD;
  768. }
  769. /*
  770. * Determine if media is present.
  771. */
  772. static int usbat_flash_check_media_present(struct us_data *us,
  773. unsigned char *uio)
  774. {
  775. if (*uio & USBAT_UIO_UI0) {
  776. usb_stor_dbg(us, "no media detected\n");
  777. return USBAT_FLASH_MEDIA_NONE;
  778. }
  779. return USBAT_FLASH_MEDIA_CF;
  780. }
  781. /*
  782. * Determine if media has changed since last operation
  783. */
  784. static int usbat_flash_check_media_changed(struct us_data *us,
  785. unsigned char *uio)
  786. {
  787. if (*uio & USBAT_UIO_0) {
  788. usb_stor_dbg(us, "media change detected\n");
  789. return USBAT_FLASH_MEDIA_CHANGED;
  790. }
  791. return USBAT_FLASH_MEDIA_SAME;
  792. }
  793. /*
  794. * Check for media change / no media and handle the situation appropriately
  795. */
  796. static int usbat_flash_check_media(struct us_data *us,
  797. struct usbat_info *info)
  798. {
  799. int rc;
  800. unsigned char *uio = us->iobuf;
  801. rc = usbat_read_user_io(us, uio);
  802. if (rc != USB_STOR_XFER_GOOD)
  803. return USB_STOR_TRANSPORT_ERROR;
  804. /* Check for media existence */
  805. rc = usbat_flash_check_media_present(us, uio);
  806. if (rc == USBAT_FLASH_MEDIA_NONE) {
  807. info->sense_key = 0x02;
  808. info->sense_asc = 0x3A;
  809. info->sense_ascq = 0x00;
  810. return USB_STOR_TRANSPORT_FAILED;
  811. }
  812. /* Check for media change */
  813. rc = usbat_flash_check_media_changed(us, uio);
  814. if (rc == USBAT_FLASH_MEDIA_CHANGED) {
  815. /* Reset and re-enable card detect */
  816. rc = usbat_device_reset(us);
  817. if (rc != USB_STOR_TRANSPORT_GOOD)
  818. return rc;
  819. rc = usbat_device_enable_cdt(us);
  820. if (rc != USB_STOR_TRANSPORT_GOOD)
  821. return rc;
  822. msleep(50);
  823. rc = usbat_read_user_io(us, uio);
  824. if (rc != USB_STOR_XFER_GOOD)
  825. return USB_STOR_TRANSPORT_ERROR;
  826. info->sense_key = UNIT_ATTENTION;
  827. info->sense_asc = 0x28;
  828. info->sense_ascq = 0x00;
  829. return USB_STOR_TRANSPORT_FAILED;
  830. }
  831. return USB_STOR_TRANSPORT_GOOD;
  832. }
  833. /*
  834. * Determine whether we are controlling a flash-based reader/writer,
  835. * or a HP8200-based CD drive.
  836. * Sets transport functions as appropriate.
  837. */
  838. static int usbat_identify_device(struct us_data *us,
  839. struct usbat_info *info)
  840. {
  841. int rc;
  842. unsigned char status;
  843. if (!us || !info)
  844. return USB_STOR_TRANSPORT_ERROR;
  845. rc = usbat_device_reset(us);
  846. if (rc != USB_STOR_TRANSPORT_GOOD)
  847. return rc;
  848. msleep(500);
  849. /*
  850. * In attempt to distinguish between HP CDRW's and Flash readers, we now
  851. * execute the IDENTIFY PACKET DEVICE command. On ATA devices (i.e. flash
  852. * readers), this command should fail with error. On ATAPI devices (i.e.
  853. * CDROM drives), it should succeed.
  854. */
  855. rc = usbat_write(us, USBAT_ATA, USBAT_ATA_CMD, 0xA1);
  856. if (rc != USB_STOR_XFER_GOOD)
  857. return USB_STOR_TRANSPORT_ERROR;
  858. rc = usbat_get_status(us, &status);
  859. if (rc != USB_STOR_XFER_GOOD)
  860. return USB_STOR_TRANSPORT_ERROR;
  861. /* Check for error bit, or if the command 'fell through' */
  862. if (status == 0xA1 || !(status & 0x01)) {
  863. /* Device is HP 8200 */
  864. usb_stor_dbg(us, "Detected HP8200 CDRW\n");
  865. info->devicetype = USBAT_DEV_HP8200;
  866. } else {
  867. /* Device is a CompactFlash reader/writer */
  868. usb_stor_dbg(us, "Detected Flash reader/writer\n");
  869. info->devicetype = USBAT_DEV_FLASH;
  870. }
  871. return USB_STOR_TRANSPORT_GOOD;
  872. }
  873. /*
  874. * Set the transport function based on the device type
  875. */
  876. static int usbat_set_transport(struct us_data *us,
  877. struct usbat_info *info,
  878. int devicetype)
  879. {
  880. if (!info->devicetype)
  881. info->devicetype = devicetype;
  882. if (!info->devicetype)
  883. usbat_identify_device(us, info);
  884. switch (info->devicetype) {
  885. default:
  886. return USB_STOR_TRANSPORT_ERROR;
  887. case USBAT_DEV_HP8200:
  888. us->transport = usbat_hp8200e_transport;
  889. break;
  890. case USBAT_DEV_FLASH:
  891. us->transport = usbat_flash_transport;
  892. break;
  893. }
  894. return 0;
  895. }
  896. /*
  897. * Read the media capacity
  898. */
  899. static int usbat_flash_get_sector_count(struct us_data *us,
  900. struct usbat_info *info)
  901. {
  902. unsigned char registers[3] = {
  903. USBAT_ATA_SECCNT,
  904. USBAT_ATA_DEVICE,
  905. USBAT_ATA_CMD,
  906. };
  907. unsigned char command[3] = { 0x01, 0xA0, 0xEC };
  908. unsigned char *reply;
  909. unsigned char status;
  910. int rc;
  911. if (!us || !info)
  912. return USB_STOR_TRANSPORT_ERROR;
  913. reply = kmalloc(512, GFP_NOIO);
  914. if (!reply)
  915. return USB_STOR_TRANSPORT_ERROR;
  916. /* ATA command : IDENTIFY DEVICE */
  917. rc = usbat_multiple_write(us, registers, command, 3);
  918. if (rc != USB_STOR_XFER_GOOD) {
  919. usb_stor_dbg(us, "Gah! identify_device failed\n");
  920. rc = USB_STOR_TRANSPORT_ERROR;
  921. goto leave;
  922. }
  923. /* Read device status */
  924. if (usbat_get_status(us, &status) != USB_STOR_XFER_GOOD) {
  925. rc = USB_STOR_TRANSPORT_ERROR;
  926. goto leave;
  927. }
  928. msleep(100);
  929. /* Read the device identification data */
  930. rc = usbat_read_block(us, reply, 512, 0);
  931. if (rc != USB_STOR_TRANSPORT_GOOD)
  932. goto leave;
  933. info->sectors = ((u32)(reply[117]) << 24) |
  934. ((u32)(reply[116]) << 16) |
  935. ((u32)(reply[115]) << 8) |
  936. ((u32)(reply[114]) );
  937. rc = USB_STOR_TRANSPORT_GOOD;
  938. leave:
  939. kfree(reply);
  940. return rc;
  941. }
  942. /*
  943. * Read data from device
  944. */
  945. static int usbat_flash_read_data(struct us_data *us,
  946. struct usbat_info *info,
  947. u32 sector,
  948. u32 sectors)
  949. {
  950. unsigned char registers[7] = {
  951. USBAT_ATA_FEATURES,
  952. USBAT_ATA_SECCNT,
  953. USBAT_ATA_SECNUM,
  954. USBAT_ATA_LBA_ME,
  955. USBAT_ATA_LBA_HI,
  956. USBAT_ATA_DEVICE,
  957. USBAT_ATA_STATUS,
  958. };
  959. unsigned char command[7];
  960. unsigned char *buffer;
  961. unsigned char thistime;
  962. unsigned int totallen, alloclen;
  963. int len, result;
  964. unsigned int sg_offset = 0;
  965. struct scatterlist *sg = NULL;
  966. result = usbat_flash_check_media(us, info);
  967. if (result != USB_STOR_TRANSPORT_GOOD)
  968. return result;
  969. /*
  970. * we're working in LBA mode. according to the ATA spec,
  971. * we can support up to 28-bit addressing. I don't know if Jumpshot
  972. * supports beyond 24-bit addressing. It's kind of hard to test
  973. * since it requires > 8GB CF card.
  974. */
  975. if (sector > 0x0FFFFFFF)
  976. return USB_STOR_TRANSPORT_ERROR;
  977. totallen = sectors * info->ssize;
  978. /*
  979. * Since we don't read more than 64 KB at a time, we have to create
  980. * a bounce buffer and move the data a piece at a time between the
  981. * bounce buffer and the actual transfer buffer.
  982. */
  983. alloclen = min(totallen, 65536u);
  984. buffer = kmalloc(alloclen, GFP_NOIO);
  985. if (buffer == NULL)
  986. return USB_STOR_TRANSPORT_ERROR;
  987. do {
  988. /*
  989. * loop, never allocate or transfer more than 64k at once
  990. * (min(128k, 255*info->ssize) is the real limit)
  991. */
  992. len = min(totallen, alloclen);
  993. thistime = (len / info->ssize) & 0xff;
  994. /* ATA command 0x20 (READ SECTORS) */
  995. usbat_pack_ata_sector_cmd(command, thistime, sector, 0x20);
  996. /* Write/execute ATA read command */
  997. result = usbat_multiple_write(us, registers, command, 7);
  998. if (result != USB_STOR_TRANSPORT_GOOD)
  999. goto leave;
  1000. /* Read the data we just requested */
  1001. result = usbat_read_blocks(us, buffer, len, 0);
  1002. if (result != USB_STOR_TRANSPORT_GOOD)
  1003. goto leave;
  1004. usb_stor_dbg(us, "%d bytes\n", len);
  1005. /* Store the data in the transfer buffer */
  1006. usb_stor_access_xfer_buf(buffer, len, us->srb,
  1007. &sg, &sg_offset, TO_XFER_BUF);
  1008. sector += thistime;
  1009. totallen -= len;
  1010. } while (totallen > 0);
  1011. kfree(buffer);
  1012. return USB_STOR_TRANSPORT_GOOD;
  1013. leave:
  1014. kfree(buffer);
  1015. return USB_STOR_TRANSPORT_ERROR;
  1016. }
  1017. /*
  1018. * Write data to device
  1019. */
  1020. static int usbat_flash_write_data(struct us_data *us,
  1021. struct usbat_info *info,
  1022. u32 sector,
  1023. u32 sectors)
  1024. {
  1025. unsigned char registers[7] = {
  1026. USBAT_ATA_FEATURES,
  1027. USBAT_ATA_SECCNT,
  1028. USBAT_ATA_SECNUM,
  1029. USBAT_ATA_LBA_ME,
  1030. USBAT_ATA_LBA_HI,
  1031. USBAT_ATA_DEVICE,
  1032. USBAT_ATA_STATUS,
  1033. };
  1034. unsigned char command[7];
  1035. unsigned char *buffer;
  1036. unsigned char thistime;
  1037. unsigned int totallen, alloclen;
  1038. int len, result;
  1039. unsigned int sg_offset = 0;
  1040. struct scatterlist *sg = NULL;
  1041. result = usbat_flash_check_media(us, info);
  1042. if (result != USB_STOR_TRANSPORT_GOOD)
  1043. return result;
  1044. /*
  1045. * we're working in LBA mode. according to the ATA spec,
  1046. * we can support up to 28-bit addressing. I don't know if the device
  1047. * supports beyond 24-bit addressing. It's kind of hard to test
  1048. * since it requires > 8GB media.
  1049. */
  1050. if (sector > 0x0FFFFFFF)
  1051. return USB_STOR_TRANSPORT_ERROR;
  1052. totallen = sectors * info->ssize;
  1053. /*
  1054. * Since we don't write more than 64 KB at a time, we have to create
  1055. * a bounce buffer and move the data a piece at a time between the
  1056. * bounce buffer and the actual transfer buffer.
  1057. */
  1058. alloclen = min(totallen, 65536u);
  1059. buffer = kmalloc(alloclen, GFP_NOIO);
  1060. if (buffer == NULL)
  1061. return USB_STOR_TRANSPORT_ERROR;
  1062. do {
  1063. /*
  1064. * loop, never allocate or transfer more than 64k at once
  1065. * (min(128k, 255*info->ssize) is the real limit)
  1066. */
  1067. len = min(totallen, alloclen);
  1068. thistime = (len / info->ssize) & 0xff;
  1069. /* Get the data from the transfer buffer */
  1070. usb_stor_access_xfer_buf(buffer, len, us->srb,
  1071. &sg, &sg_offset, FROM_XFER_BUF);
  1072. /* ATA command 0x30 (WRITE SECTORS) */
  1073. usbat_pack_ata_sector_cmd(command, thistime, sector, 0x30);
  1074. /* Write/execute ATA write command */
  1075. result = usbat_multiple_write(us, registers, command, 7);
  1076. if (result != USB_STOR_TRANSPORT_GOOD)
  1077. goto leave;
  1078. /* Write the data */
  1079. result = usbat_write_blocks(us, buffer, len, 0);
  1080. if (result != USB_STOR_TRANSPORT_GOOD)
  1081. goto leave;
  1082. sector += thistime;
  1083. totallen -= len;
  1084. } while (totallen > 0);
  1085. kfree(buffer);
  1086. return result;
  1087. leave:
  1088. kfree(buffer);
  1089. return USB_STOR_TRANSPORT_ERROR;
  1090. }
  1091. /*
  1092. * Squeeze a potentially huge (> 65535 byte) read10 command into
  1093. * a little ( <= 65535 byte) ATAPI pipe
  1094. */
  1095. static int usbat_hp8200e_handle_read10(struct us_data *us,
  1096. unsigned char *registers,
  1097. unsigned char *data,
  1098. struct scsi_cmnd *srb)
  1099. {
  1100. int result = USB_STOR_TRANSPORT_GOOD;
  1101. unsigned char *buffer;
  1102. unsigned int len;
  1103. unsigned int sector;
  1104. unsigned int sg_offset = 0;
  1105. struct scatterlist *sg = NULL;
  1106. usb_stor_dbg(us, "transfersize %d\n", srb->transfersize);
  1107. if (scsi_bufflen(srb) < 0x10000) {
  1108. result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
  1109. registers, data, 19,
  1110. USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
  1111. (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
  1112. DMA_FROM_DEVICE,
  1113. scsi_sglist(srb),
  1114. scsi_bufflen(srb), scsi_sg_count(srb), 1);
  1115. return result;
  1116. }
  1117. /*
  1118. * Since we're requesting more data than we can handle in
  1119. * a single read command (max is 64k-1), we will perform
  1120. * multiple reads, but each read must be in multiples of
  1121. * a sector. Luckily the sector size is in srb->transfersize
  1122. * (see linux/drivers/scsi/sr.c).
  1123. */
  1124. if (data[7+0] == GPCMD_READ_CD) {
  1125. len = short_pack(data[7+9], data[7+8]);
  1126. len <<= 16;
  1127. len |= data[7+7];
  1128. usb_stor_dbg(us, "GPCMD_READ_CD: len %d\n", len);
  1129. srb->transfersize = scsi_bufflen(srb)/len;
  1130. }
  1131. if (!srb->transfersize) {
  1132. srb->transfersize = 2048; /* A guess */
  1133. usb_stor_dbg(us, "transfersize 0, forcing %d\n",
  1134. srb->transfersize);
  1135. }
  1136. /*
  1137. * Since we only read in one block at a time, we have to create
  1138. * a bounce buffer and move the data a piece at a time between the
  1139. * bounce buffer and the actual transfer buffer.
  1140. */
  1141. len = (65535/srb->transfersize) * srb->transfersize;
  1142. usb_stor_dbg(us, "Max read is %d bytes\n", len);
  1143. len = min(len, scsi_bufflen(srb));
  1144. buffer = kmalloc(len, GFP_NOIO);
  1145. if (buffer == NULL) /* bloody hell! */
  1146. return USB_STOR_TRANSPORT_FAILED;
  1147. sector = short_pack(data[7+3], data[7+2]);
  1148. sector <<= 16;
  1149. sector |= short_pack(data[7+5], data[7+4]);
  1150. transferred = 0;
  1151. while (transferred != scsi_bufflen(srb)) {
  1152. if (len > scsi_bufflen(srb) - transferred)
  1153. len = scsi_bufflen(srb) - transferred;
  1154. data[3] = len&0xFF; /* (cylL) = expected length (L) */
  1155. data[4] = (len>>8)&0xFF; /* (cylH) = expected length (H) */
  1156. /* Fix up the SCSI command sector and num sectors */
  1157. data[7+2] = MSB_of(sector>>16); /* SCSI command sector */
  1158. data[7+3] = LSB_of(sector>>16);
  1159. data[7+4] = MSB_of(sector&0xFFFF);
  1160. data[7+5] = LSB_of(sector&0xFFFF);
  1161. if (data[7+0] == GPCMD_READ_CD)
  1162. data[7+6] = 0;
  1163. data[7+7] = MSB_of(len / srb->transfersize); /* SCSI command */
  1164. data[7+8] = LSB_of(len / srb->transfersize); /* num sectors */
  1165. result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
  1166. registers, data, 19,
  1167. USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
  1168. (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
  1169. DMA_FROM_DEVICE,
  1170. buffer,
  1171. len, 0, 1);
  1172. if (result != USB_STOR_TRANSPORT_GOOD)
  1173. break;
  1174. /* Store the data in the transfer buffer */
  1175. usb_stor_access_xfer_buf(buffer, len, srb,
  1176. &sg, &sg_offset, TO_XFER_BUF);
  1177. /* Update the amount transferred and the sector number */
  1178. transferred += len;
  1179. sector += len / srb->transfersize;
  1180. } /* while transferred != scsi_bufflen(srb) */
  1181. kfree(buffer);
  1182. return result;
  1183. }
  1184. static int usbat_select_and_test_registers(struct us_data *us)
  1185. {
  1186. int selector;
  1187. unsigned char *status = us->iobuf;
  1188. /* try device = master, then device = slave. */
  1189. for (selector = 0xA0; selector <= 0xB0; selector += 0x10) {
  1190. if (usbat_write(us, USBAT_ATA, USBAT_ATA_DEVICE, selector) !=
  1191. USB_STOR_XFER_GOOD)
  1192. return USB_STOR_TRANSPORT_ERROR;
  1193. if (usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status) !=
  1194. USB_STOR_XFER_GOOD)
  1195. return USB_STOR_TRANSPORT_ERROR;
  1196. if (usbat_read(us, USBAT_ATA, USBAT_ATA_DEVICE, status) !=
  1197. USB_STOR_XFER_GOOD)
  1198. return USB_STOR_TRANSPORT_ERROR;
  1199. if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
  1200. USB_STOR_XFER_GOOD)
  1201. return USB_STOR_TRANSPORT_ERROR;
  1202. if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
  1203. USB_STOR_XFER_GOOD)
  1204. return USB_STOR_TRANSPORT_ERROR;
  1205. if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_ME, 0x55) !=
  1206. USB_STOR_XFER_GOOD)
  1207. return USB_STOR_TRANSPORT_ERROR;
  1208. if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_HI, 0xAA) !=
  1209. USB_STOR_XFER_GOOD)
  1210. return USB_STOR_TRANSPORT_ERROR;
  1211. if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
  1212. USB_STOR_XFER_GOOD)
  1213. return USB_STOR_TRANSPORT_ERROR;
  1214. if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
  1215. USB_STOR_XFER_GOOD)
  1216. return USB_STOR_TRANSPORT_ERROR;
  1217. }
  1218. return USB_STOR_TRANSPORT_GOOD;
  1219. }
  1220. /*
  1221. * Initialize the USBAT processor and the storage device
  1222. */
  1223. static int init_usbat(struct us_data *us, int devicetype)
  1224. {
  1225. int rc;
  1226. struct usbat_info *info;
  1227. unsigned char subcountH = USBAT_ATA_LBA_HI;
  1228. unsigned char subcountL = USBAT_ATA_LBA_ME;
  1229. unsigned char *status = us->iobuf;
  1230. us->extra = kzalloc(sizeof(struct usbat_info), GFP_NOIO);
  1231. if (!us->extra)
  1232. return 1;
  1233. info = (struct usbat_info *) (us->extra);
  1234. /* Enable peripheral control signals */
  1235. rc = usbat_write_user_io(us,
  1236. USBAT_UIO_OE1 | USBAT_UIO_OE0,
  1237. USBAT_UIO_EPAD | USBAT_UIO_1);
  1238. if (rc != USB_STOR_XFER_GOOD)
  1239. return USB_STOR_TRANSPORT_ERROR;
  1240. usb_stor_dbg(us, "INIT 1\n");
  1241. msleep(2000);
  1242. rc = usbat_read_user_io(us, status);
  1243. if (rc != USB_STOR_TRANSPORT_GOOD)
  1244. return rc;
  1245. usb_stor_dbg(us, "INIT 2\n");
  1246. rc = usbat_read_user_io(us, status);
  1247. if (rc != USB_STOR_XFER_GOOD)
  1248. return USB_STOR_TRANSPORT_ERROR;
  1249. rc = usbat_read_user_io(us, status);
  1250. if (rc != USB_STOR_XFER_GOOD)
  1251. return USB_STOR_TRANSPORT_ERROR;
  1252. usb_stor_dbg(us, "INIT 3\n");
  1253. rc = usbat_select_and_test_registers(us);
  1254. if (rc != USB_STOR_TRANSPORT_GOOD)
  1255. return rc;
  1256. usb_stor_dbg(us, "INIT 4\n");
  1257. rc = usbat_read_user_io(us, status);
  1258. if (rc != USB_STOR_XFER_GOOD)
  1259. return USB_STOR_TRANSPORT_ERROR;
  1260. usb_stor_dbg(us, "INIT 5\n");
  1261. /* Enable peripheral control signals and card detect */
  1262. rc = usbat_device_enable_cdt(us);
  1263. if (rc != USB_STOR_TRANSPORT_GOOD)
  1264. return rc;
  1265. usb_stor_dbg(us, "INIT 6\n");
  1266. rc = usbat_read_user_io(us, status);
  1267. if (rc != USB_STOR_XFER_GOOD)
  1268. return USB_STOR_TRANSPORT_ERROR;
  1269. usb_stor_dbg(us, "INIT 7\n");
  1270. msleep(1400);
  1271. rc = usbat_read_user_io(us, status);
  1272. if (rc != USB_STOR_XFER_GOOD)
  1273. return USB_STOR_TRANSPORT_ERROR;
  1274. usb_stor_dbg(us, "INIT 8\n");
  1275. rc = usbat_select_and_test_registers(us);
  1276. if (rc != USB_STOR_TRANSPORT_GOOD)
  1277. return rc;
  1278. usb_stor_dbg(us, "INIT 9\n");
  1279. /* At this point, we need to detect which device we are using */
  1280. if (usbat_set_transport(us, info, devicetype))
  1281. return USB_STOR_TRANSPORT_ERROR;
  1282. usb_stor_dbg(us, "INIT 10\n");
  1283. if (usbat_get_device_type(us) == USBAT_DEV_FLASH) {
  1284. subcountH = 0x02;
  1285. subcountL = 0x00;
  1286. }
  1287. rc = usbat_set_shuttle_features(us, (USBAT_FEAT_ETEN | USBAT_FEAT_ET2 | USBAT_FEAT_ET1),
  1288. 0x00, 0x88, 0x08, subcountH, subcountL);
  1289. if (rc != USB_STOR_XFER_GOOD)
  1290. return USB_STOR_TRANSPORT_ERROR;
  1291. usb_stor_dbg(us, "INIT 11\n");
  1292. return USB_STOR_TRANSPORT_GOOD;
  1293. }
  1294. /*
  1295. * Transport for the HP 8200e
  1296. */
  1297. static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us)
  1298. {
  1299. int result;
  1300. unsigned char *status = us->iobuf;
  1301. unsigned char registers[32];
  1302. unsigned char data[32];
  1303. unsigned int len;
  1304. int i;
  1305. len = scsi_bufflen(srb);
  1306. /* Send A0 (ATA PACKET COMMAND).
  1307. Note: I guess we're never going to get any of the ATA
  1308. commands... just ATA Packet Commands.
  1309. */
  1310. registers[0] = USBAT_ATA_FEATURES;
  1311. registers[1] = USBAT_ATA_SECCNT;
  1312. registers[2] = USBAT_ATA_SECNUM;
  1313. registers[3] = USBAT_ATA_LBA_ME;
  1314. registers[4] = USBAT_ATA_LBA_HI;
  1315. registers[5] = USBAT_ATA_DEVICE;
  1316. registers[6] = USBAT_ATA_CMD;
  1317. data[0] = 0x00;
  1318. data[1] = 0x00;
  1319. data[2] = 0x00;
  1320. data[3] = len&0xFF; /* (cylL) = expected length (L) */
  1321. data[4] = (len>>8)&0xFF; /* (cylH) = expected length (H) */
  1322. data[5] = 0xB0; /* (device sel) = slave */
  1323. data[6] = 0xA0; /* (command) = ATA PACKET COMMAND */
  1324. for (i=7; i<19; i++) {
  1325. registers[i] = 0x10;
  1326. data[i] = (i-7 >= srb->cmd_len) ? 0 : srb->cmnd[i-7];
  1327. }
  1328. result = usbat_get_status(us, status);
  1329. usb_stor_dbg(us, "Status = %02X\n", *status);
  1330. if (result != USB_STOR_XFER_GOOD)
  1331. return USB_STOR_TRANSPORT_ERROR;
  1332. if (srb->cmnd[0] == TEST_UNIT_READY)
  1333. transferred = 0;
  1334. if (srb->sc_data_direction == DMA_TO_DEVICE) {
  1335. result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
  1336. registers, data, 19,
  1337. USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
  1338. (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
  1339. DMA_TO_DEVICE,
  1340. scsi_sglist(srb),
  1341. len, scsi_sg_count(srb), 10);
  1342. if (result == USB_STOR_TRANSPORT_GOOD) {
  1343. transferred += len;
  1344. usb_stor_dbg(us, "Wrote %08X bytes\n", transferred);
  1345. }
  1346. return result;
  1347. } else if (srb->cmnd[0] == READ_10 ||
  1348. srb->cmnd[0] == GPCMD_READ_CD) {
  1349. return usbat_hp8200e_handle_read10(us, registers, data, srb);
  1350. }
  1351. if (len > 0xFFFF) {
  1352. usb_stor_dbg(us, "Error: len = %08X... what do I do now?\n",
  1353. len);
  1354. return USB_STOR_TRANSPORT_ERROR;
  1355. }
  1356. result = usbat_multiple_write(us, registers, data, 7);
  1357. if (result != USB_STOR_TRANSPORT_GOOD)
  1358. return result;
  1359. /*
  1360. * Write the 12-byte command header.
  1361. *
  1362. * If the command is BLANK then set the timer for 75 minutes.
  1363. * Otherwise set it for 10 minutes.
  1364. *
  1365. * NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW
  1366. * AT SPEED 4 IS UNRELIABLE!!!
  1367. */
  1368. result = usbat_write_block(us, USBAT_ATA, srb->cmnd, 12,
  1369. srb->cmnd[0] == GPCMD_BLANK ? 75 : 10, 0);
  1370. if (result != USB_STOR_TRANSPORT_GOOD)
  1371. return result;
  1372. /* If there is response data to be read in then do it here. */
  1373. if (len != 0 && (srb->sc_data_direction == DMA_FROM_DEVICE)) {
  1374. /* How many bytes to read in? Check cylL register */
  1375. if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
  1376. USB_STOR_XFER_GOOD) {
  1377. return USB_STOR_TRANSPORT_ERROR;
  1378. }
  1379. if (len > 0xFF) { /* need to read cylH also */
  1380. len = *status;
  1381. if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
  1382. USB_STOR_XFER_GOOD) {
  1383. return USB_STOR_TRANSPORT_ERROR;
  1384. }
  1385. len += ((unsigned int) *status)<<8;
  1386. }
  1387. else
  1388. len = *status;
  1389. result = usbat_read_block(us, scsi_sglist(srb), len,
  1390. scsi_sg_count(srb));
  1391. }
  1392. return result;
  1393. }
  1394. /*
  1395. * Transport for USBAT02-based CompactFlash and similar storage devices
  1396. */
  1397. static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us)
  1398. {
  1399. int rc;
  1400. struct usbat_info *info = (struct usbat_info *) (us->extra);
  1401. unsigned long block, blocks;
  1402. unsigned char *ptr = us->iobuf;
  1403. static unsigned char inquiry_response[36] = {
  1404. 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
  1405. };
  1406. if (srb->cmnd[0] == INQUIRY) {
  1407. usb_stor_dbg(us, "INQUIRY - Returning bogus response\n");
  1408. memcpy(ptr, inquiry_response, sizeof(inquiry_response));
  1409. fill_inquiry_response(us, ptr, 36);
  1410. return USB_STOR_TRANSPORT_GOOD;
  1411. }
  1412. if (srb->cmnd[0] == READ_CAPACITY) {
  1413. rc = usbat_flash_check_media(us, info);
  1414. if (rc != USB_STOR_TRANSPORT_GOOD)
  1415. return rc;
  1416. rc = usbat_flash_get_sector_count(us, info);
  1417. if (rc != USB_STOR_TRANSPORT_GOOD)
  1418. return rc;
  1419. /* hard coded 512 byte sectors as per ATA spec */
  1420. info->ssize = 0x200;
  1421. usb_stor_dbg(us, "READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
  1422. info->sectors, info->ssize);
  1423. /*
  1424. * build the reply
  1425. * note: must return the sector number of the last sector,
  1426. * *not* the total number of sectors
  1427. */
  1428. ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
  1429. ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
  1430. usb_stor_set_xfer_buf(ptr, 8, srb);
  1431. return USB_STOR_TRANSPORT_GOOD;
  1432. }
  1433. if (srb->cmnd[0] == MODE_SELECT_10) {
  1434. usb_stor_dbg(us, "Gah! MODE_SELECT_10\n");
  1435. return USB_STOR_TRANSPORT_ERROR;
  1436. }
  1437. if (srb->cmnd[0] == READ_10) {
  1438. block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
  1439. ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
  1440. blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
  1441. usb_stor_dbg(us, "READ_10: read block 0x%04lx count %ld\n",
  1442. block, blocks);
  1443. return usbat_flash_read_data(us, info, block, blocks);
  1444. }
  1445. if (srb->cmnd[0] == READ_12) {
  1446. /*
  1447. * I don't think we'll ever see a READ_12 but support it anyway
  1448. */
  1449. block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
  1450. ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
  1451. blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
  1452. ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
  1453. usb_stor_dbg(us, "READ_12: read block 0x%04lx count %ld\n",
  1454. block, blocks);
  1455. return usbat_flash_read_data(us, info, block, blocks);
  1456. }
  1457. if (srb->cmnd[0] == WRITE_10) {
  1458. block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
  1459. ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
  1460. blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
  1461. usb_stor_dbg(us, "WRITE_10: write block 0x%04lx count %ld\n",
  1462. block, blocks);
  1463. return usbat_flash_write_data(us, info, block, blocks);
  1464. }
  1465. if (srb->cmnd[0] == WRITE_12) {
  1466. /*
  1467. * I don't think we'll ever see a WRITE_12 but support it anyway
  1468. */
  1469. block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
  1470. ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
  1471. blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
  1472. ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
  1473. usb_stor_dbg(us, "WRITE_12: write block 0x%04lx count %ld\n",
  1474. block, blocks);
  1475. return usbat_flash_write_data(us, info, block, blocks);
  1476. }
  1477. if (srb->cmnd[0] == TEST_UNIT_READY) {
  1478. usb_stor_dbg(us, "TEST_UNIT_READY\n");
  1479. rc = usbat_flash_check_media(us, info);
  1480. if (rc != USB_STOR_TRANSPORT_GOOD)
  1481. return rc;
  1482. return usbat_check_status(us);
  1483. }
  1484. if (srb->cmnd[0] == REQUEST_SENSE) {
  1485. usb_stor_dbg(us, "REQUEST_SENSE\n");
  1486. memset(ptr, 0, 18);
  1487. ptr[0] = 0xF0;
  1488. ptr[2] = info->sense_key;
  1489. ptr[7] = 11;
  1490. ptr[12] = info->sense_asc;
  1491. ptr[13] = info->sense_ascq;
  1492. usb_stor_set_xfer_buf(ptr, 18, srb);
  1493. return USB_STOR_TRANSPORT_GOOD;
  1494. }
  1495. if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
  1496. /*
  1497. * sure. whatever. not like we can stop the user from popping
  1498. * the media out of the device (no locking doors, etc)
  1499. */
  1500. return USB_STOR_TRANSPORT_GOOD;
  1501. }
  1502. usb_stor_dbg(us, "Gah! Unknown command: %d (0x%x)\n",
  1503. srb->cmnd[0], srb->cmnd[0]);
  1504. info->sense_key = 0x05;
  1505. info->sense_asc = 0x20;
  1506. info->sense_ascq = 0x00;
  1507. return USB_STOR_TRANSPORT_FAILED;
  1508. }
  1509. static int init_usbat_cd(struct us_data *us)
  1510. {
  1511. return init_usbat(us, USBAT_DEV_HP8200);
  1512. }
  1513. static int init_usbat_flash(struct us_data *us)
  1514. {
  1515. return init_usbat(us, USBAT_DEV_FLASH);
  1516. }
  1517. static int usbat_probe(struct usb_interface *intf,
  1518. const struct usb_device_id *id)
  1519. {
  1520. struct us_data *us;
  1521. int result;
  1522. result = usb_stor_probe1(&us, intf, id,
  1523. (id - usbat_usb_ids) + usbat_unusual_dev_list);
  1524. if (result)
  1525. return result;
  1526. /* The actual transport will be determined later by the
  1527. * initialization routine; this is just a placeholder.
  1528. */
  1529. us->transport_name = "Shuttle USBAT";
  1530. us->transport = usbat_flash_transport;
  1531. us->transport_reset = usb_stor_CB_reset;
  1532. us->max_lun = 1;
  1533. result = usb_stor_probe2(us);
  1534. return result;
  1535. }
  1536. static struct usb_driver usbat_driver = {
  1537. .name = "ums-usbat",
  1538. .probe = usbat_probe,
  1539. .disconnect = usb_stor_disconnect,
  1540. .suspend = usb_stor_suspend,
  1541. .resume = usb_stor_resume,
  1542. .reset_resume = usb_stor_reset_resume,
  1543. .pre_reset = usb_stor_pre_reset,
  1544. .post_reset = usb_stor_post_reset,
  1545. .id_table = usbat_usb_ids,
  1546. .soft_unbind = 1,
  1547. .no_dynamic_id = 1,
  1548. };
  1549. module_usb_driver(usbat_driver);