shuttle_usbat.c 48 KB

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