shuttle_usbat.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870
  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)|(USB_US_TYPE_STOR<<24) }
  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. US_DEBUGP("usbat_bulk_read: 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. US_DEBUGP("usbat_bulk_write: 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. US_DEBUGP("usbat_get_status: 0x%02X\n", (unsigned short) (*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. US_DEBUGP("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. US_DEBUGP("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. US_DEBUGP("Redoing %s\n",
  565. direction==DMA_TO_DEVICE ? "write" : "read");
  566. } else if (result != USB_STOR_XFER_GOOD)
  567. return USB_STOR_TRANSPORT_ERROR;
  568. else
  569. return usbat_wait_not_busy(us, minutes);
  570. }
  571. US_DEBUGP("Bummer! %s bulk data 20 times failed.\n",
  572. direction==DMA_TO_DEVICE ? "Writing" : "Reading");
  573. return USB_STOR_TRANSPORT_FAILED;
  574. }
  575. /*
  576. * Write to multiple registers:
  577. * Allows us to write specific data to any registers. The data to be written
  578. * gets packed in this sequence: reg0, data0, reg1, data1, ..., regN, dataN
  579. * which gets sent through bulk out.
  580. * Not designed for large transfers of data!
  581. */
  582. static int usbat_multiple_write(struct us_data *us,
  583. unsigned char *registers,
  584. unsigned char *data_out,
  585. unsigned short num_registers)
  586. {
  587. int i, result;
  588. unsigned char *data = us->iobuf;
  589. unsigned char *command = us->iobuf;
  590. BUG_ON(num_registers > US_IOBUF_SIZE/2);
  591. /* Write to multiple registers, ATA access */
  592. command[0] = 0x40;
  593. command[1] = USBAT_ATA | USBAT_CMD_WRITE_REGS;
  594. /* No relevance */
  595. command[2] = 0;
  596. command[3] = 0;
  597. command[4] = 0;
  598. command[5] = 0;
  599. /* Number of bytes to be transferred (incl. addresses and data) */
  600. command[6] = LSB_of(num_registers*2);
  601. command[7] = MSB_of(num_registers*2);
  602. /* The setup command */
  603. result = usbat_execute_command(us, command, 8);
  604. if (result != USB_STOR_XFER_GOOD)
  605. return USB_STOR_TRANSPORT_ERROR;
  606. /* Create the reg/data, reg/data sequence */
  607. for (i=0; i<num_registers; i++) {
  608. data[i<<1] = registers[i];
  609. data[1+(i<<1)] = data_out[i];
  610. }
  611. /* Send the data */
  612. result = usbat_bulk_write(us, data, num_registers*2, 0);
  613. if (result != USB_STOR_XFER_GOOD)
  614. return USB_STOR_TRANSPORT_ERROR;
  615. if (usbat_get_device_type(us) == USBAT_DEV_HP8200)
  616. return usbat_wait_not_busy(us, 0);
  617. else
  618. return USB_STOR_TRANSPORT_GOOD;
  619. }
  620. /*
  621. * Conditionally read blocks from device:
  622. * Allows us to read blocks from a specific data register, based upon the
  623. * condition that a status register can be successfully masked with a status
  624. * qualifier. If this condition is not initially met, the read will wait
  625. * up until a maximum amount of time has elapsed, as specified by timeout.
  626. * The read will start when the condition is met, otherwise the command aborts.
  627. *
  628. * The qualifier defined here is not the value that is masked, it defines
  629. * conditions for the write to take place. The actual masked qualifier (and
  630. * other related details) are defined beforehand with _set_shuttle_features().
  631. */
  632. static int usbat_read_blocks(struct us_data *us,
  633. void* buffer,
  634. int len,
  635. int use_sg)
  636. {
  637. int result;
  638. unsigned char *command = us->iobuf;
  639. command[0] = 0xC0;
  640. command[1] = USBAT_ATA | USBAT_CMD_COND_READ_BLOCK;
  641. command[2] = USBAT_ATA_DATA;
  642. command[3] = USBAT_ATA_STATUS;
  643. command[4] = 0xFD; /* Timeout (ms); */
  644. command[5] = USBAT_QUAL_FCQ;
  645. command[6] = LSB_of(len);
  646. command[7] = MSB_of(len);
  647. /* Multiple block read setup command */
  648. result = usbat_execute_command(us, command, 8);
  649. if (result != USB_STOR_XFER_GOOD)
  650. return USB_STOR_TRANSPORT_FAILED;
  651. /* Read the blocks we just asked for */
  652. result = usbat_bulk_read(us, buffer, len, use_sg);
  653. if (result != USB_STOR_XFER_GOOD)
  654. return USB_STOR_TRANSPORT_FAILED;
  655. return USB_STOR_TRANSPORT_GOOD;
  656. }
  657. /*
  658. * Conditionally write blocks to device:
  659. * Allows us to write blocks to a specific data register, based upon the
  660. * condition that a status register can be successfully masked with a status
  661. * qualifier. If this condition is not initially met, the write will wait
  662. * up until a maximum amount of time has elapsed, as specified by timeout.
  663. * The read will start when the condition is met, otherwise the command aborts.
  664. *
  665. * The qualifier defined here is not the value that is masked, it defines
  666. * conditions for the write to take place. The actual masked qualifier (and
  667. * other related details) are defined beforehand with _set_shuttle_features().
  668. */
  669. static int usbat_write_blocks(struct us_data *us,
  670. void* buffer,
  671. int len,
  672. int use_sg)
  673. {
  674. int result;
  675. unsigned char *command = us->iobuf;
  676. command[0] = 0x40;
  677. command[1] = USBAT_ATA | USBAT_CMD_COND_WRITE_BLOCK;
  678. command[2] = USBAT_ATA_DATA;
  679. command[3] = USBAT_ATA_STATUS;
  680. command[4] = 0xFD; /* Timeout (ms) */
  681. command[5] = USBAT_QUAL_FCQ;
  682. command[6] = LSB_of(len);
  683. command[7] = MSB_of(len);
  684. /* Multiple block write setup command */
  685. result = usbat_execute_command(us, command, 8);
  686. if (result != USB_STOR_XFER_GOOD)
  687. return USB_STOR_TRANSPORT_FAILED;
  688. /* Write the data */
  689. result = usbat_bulk_write(us, buffer, len, use_sg);
  690. if (result != USB_STOR_XFER_GOOD)
  691. return USB_STOR_TRANSPORT_FAILED;
  692. return USB_STOR_TRANSPORT_GOOD;
  693. }
  694. /*
  695. * Read the User IO register
  696. */
  697. static int usbat_read_user_io(struct us_data *us, unsigned char *data_flags)
  698. {
  699. int result;
  700. result = usb_stor_ctrl_transfer(us,
  701. us->recv_ctrl_pipe,
  702. USBAT_CMD_UIO,
  703. 0xC0,
  704. 0,
  705. 0,
  706. data_flags,
  707. USBAT_UIO_READ);
  708. US_DEBUGP("usbat_read_user_io: UIO register reads %02X\n", (unsigned short) (*data_flags));
  709. return result;
  710. }
  711. /*
  712. * Write to the User IO register
  713. */
  714. static int usbat_write_user_io(struct us_data *us,
  715. unsigned char enable_flags,
  716. unsigned char data_flags)
  717. {
  718. return usb_stor_ctrl_transfer(us,
  719. us->send_ctrl_pipe,
  720. USBAT_CMD_UIO,
  721. 0x40,
  722. short_pack(enable_flags, data_flags),
  723. 0,
  724. NULL,
  725. USBAT_UIO_WRITE);
  726. }
  727. /*
  728. * Reset the device
  729. * Often needed on media change.
  730. */
  731. static int usbat_device_reset(struct us_data *us)
  732. {
  733. int rc;
  734. /*
  735. * Reset peripheral, enable peripheral control signals
  736. * (bring reset signal up)
  737. */
  738. rc = usbat_write_user_io(us,
  739. USBAT_UIO_DRVRST | USBAT_UIO_OE1 | USBAT_UIO_OE0,
  740. USBAT_UIO_EPAD | USBAT_UIO_1);
  741. if (rc != USB_STOR_XFER_GOOD)
  742. return USB_STOR_TRANSPORT_ERROR;
  743. /*
  744. * Enable peripheral control signals
  745. * (bring reset signal down)
  746. */
  747. rc = usbat_write_user_io(us,
  748. USBAT_UIO_OE1 | USBAT_UIO_OE0,
  749. USBAT_UIO_EPAD | USBAT_UIO_1);
  750. if (rc != USB_STOR_XFER_GOOD)
  751. return USB_STOR_TRANSPORT_ERROR;
  752. return USB_STOR_TRANSPORT_GOOD;
  753. }
  754. /*
  755. * Enable card detect
  756. */
  757. static int usbat_device_enable_cdt(struct us_data *us)
  758. {
  759. int rc;
  760. /* Enable peripheral control signals and card detect */
  761. rc = usbat_write_user_io(us,
  762. USBAT_UIO_ACKD | USBAT_UIO_OE1 | USBAT_UIO_OE0,
  763. USBAT_UIO_EPAD | USBAT_UIO_1);
  764. if (rc != USB_STOR_XFER_GOOD)
  765. return USB_STOR_TRANSPORT_ERROR;
  766. return USB_STOR_TRANSPORT_GOOD;
  767. }
  768. /*
  769. * Determine if media is present.
  770. */
  771. static int usbat_flash_check_media_present(unsigned char *uio)
  772. {
  773. if (*uio & USBAT_UIO_UI0) {
  774. US_DEBUGP("usbat_flash_check_media_present: no media detected\n");
  775. return USBAT_FLASH_MEDIA_NONE;
  776. }
  777. return USBAT_FLASH_MEDIA_CF;
  778. }
  779. /*
  780. * Determine if media has changed since last operation
  781. */
  782. static int usbat_flash_check_media_changed(unsigned char *uio)
  783. {
  784. if (*uio & USBAT_UIO_0) {
  785. US_DEBUGP("usbat_flash_check_media_changed: media change detected\n");
  786. return USBAT_FLASH_MEDIA_CHANGED;
  787. }
  788. return USBAT_FLASH_MEDIA_SAME;
  789. }
  790. /*
  791. * Check for media change / no media and handle the situation appropriately
  792. */
  793. static int usbat_flash_check_media(struct us_data *us,
  794. struct usbat_info *info)
  795. {
  796. int rc;
  797. unsigned char *uio = us->iobuf;
  798. rc = usbat_read_user_io(us, uio);
  799. if (rc != USB_STOR_XFER_GOOD)
  800. return USB_STOR_TRANSPORT_ERROR;
  801. /* Check for media existence */
  802. rc = usbat_flash_check_media_present(uio);
  803. if (rc == USBAT_FLASH_MEDIA_NONE) {
  804. info->sense_key = 0x02;
  805. info->sense_asc = 0x3A;
  806. info->sense_ascq = 0x00;
  807. return USB_STOR_TRANSPORT_FAILED;
  808. }
  809. /* Check for media change */
  810. rc = usbat_flash_check_media_changed(uio);
  811. if (rc == USBAT_FLASH_MEDIA_CHANGED) {
  812. /* Reset and re-enable card detect */
  813. rc = usbat_device_reset(us);
  814. if (rc != USB_STOR_TRANSPORT_GOOD)
  815. return rc;
  816. rc = usbat_device_enable_cdt(us);
  817. if (rc != USB_STOR_TRANSPORT_GOOD)
  818. return rc;
  819. msleep(50);
  820. rc = usbat_read_user_io(us, uio);
  821. if (rc != USB_STOR_XFER_GOOD)
  822. return USB_STOR_TRANSPORT_ERROR;
  823. info->sense_key = UNIT_ATTENTION;
  824. info->sense_asc = 0x28;
  825. info->sense_ascq = 0x00;
  826. return USB_STOR_TRANSPORT_FAILED;
  827. }
  828. return USB_STOR_TRANSPORT_GOOD;
  829. }
  830. /*
  831. * Determine whether we are controlling a flash-based reader/writer,
  832. * or a HP8200-based CD drive.
  833. * Sets transport functions as appropriate.
  834. */
  835. static int usbat_identify_device(struct us_data *us,
  836. struct usbat_info *info)
  837. {
  838. int rc;
  839. unsigned char status;
  840. if (!us || !info)
  841. return USB_STOR_TRANSPORT_ERROR;
  842. rc = usbat_device_reset(us);
  843. if (rc != USB_STOR_TRANSPORT_GOOD)
  844. return rc;
  845. msleep(500);
  846. /*
  847. * In attempt to distinguish between HP CDRW's and Flash readers, we now
  848. * execute the IDENTIFY PACKET DEVICE command. On ATA devices (i.e. flash
  849. * readers), this command should fail with error. On ATAPI devices (i.e.
  850. * CDROM drives), it should succeed.
  851. */
  852. rc = usbat_write(us, USBAT_ATA, USBAT_ATA_CMD, 0xA1);
  853. if (rc != USB_STOR_XFER_GOOD)
  854. return USB_STOR_TRANSPORT_ERROR;
  855. rc = usbat_get_status(us, &status);
  856. if (rc != USB_STOR_XFER_GOOD)
  857. return USB_STOR_TRANSPORT_ERROR;
  858. /* Check for error bit, or if the command 'fell through' */
  859. if (status == 0xA1 || !(status & 0x01)) {
  860. /* Device is HP 8200 */
  861. US_DEBUGP("usbat_identify_device: Detected HP8200 CDRW\n");
  862. info->devicetype = USBAT_DEV_HP8200;
  863. } else {
  864. /* Device is a CompactFlash reader/writer */
  865. US_DEBUGP("usbat_identify_device: Detected Flash reader/writer\n");
  866. info->devicetype = USBAT_DEV_FLASH;
  867. }
  868. return USB_STOR_TRANSPORT_GOOD;
  869. }
  870. /*
  871. * Set the transport function based on the device type
  872. */
  873. static int usbat_set_transport(struct us_data *us,
  874. struct usbat_info *info,
  875. int devicetype)
  876. {
  877. if (!info->devicetype)
  878. info->devicetype = devicetype;
  879. if (!info->devicetype)
  880. usbat_identify_device(us, info);
  881. switch (info->devicetype) {
  882. default:
  883. return USB_STOR_TRANSPORT_ERROR;
  884. case USBAT_DEV_HP8200:
  885. us->transport = usbat_hp8200e_transport;
  886. break;
  887. case USBAT_DEV_FLASH:
  888. us->transport = usbat_flash_transport;
  889. break;
  890. }
  891. return 0;
  892. }
  893. /*
  894. * Read the media capacity
  895. */
  896. static int usbat_flash_get_sector_count(struct us_data *us,
  897. struct usbat_info *info)
  898. {
  899. unsigned char registers[3] = {
  900. USBAT_ATA_SECCNT,
  901. USBAT_ATA_DEVICE,
  902. USBAT_ATA_CMD,
  903. };
  904. unsigned char command[3] = { 0x01, 0xA0, 0xEC };
  905. unsigned char *reply;
  906. unsigned char status;
  907. int rc;
  908. if (!us || !info)
  909. return USB_STOR_TRANSPORT_ERROR;
  910. reply = kmalloc(512, GFP_NOIO);
  911. if (!reply)
  912. return USB_STOR_TRANSPORT_ERROR;
  913. /* ATA command : IDENTIFY DEVICE */
  914. rc = usbat_multiple_write(us, registers, command, 3);
  915. if (rc != USB_STOR_XFER_GOOD) {
  916. US_DEBUGP("usbat_flash_get_sector_count: Gah! identify_device failed\n");
  917. rc = USB_STOR_TRANSPORT_ERROR;
  918. goto leave;
  919. }
  920. /* Read device status */
  921. if (usbat_get_status(us, &status) != USB_STOR_XFER_GOOD) {
  922. rc = USB_STOR_TRANSPORT_ERROR;
  923. goto leave;
  924. }
  925. msleep(100);
  926. /* Read the device identification data */
  927. rc = usbat_read_block(us, reply, 512, 0);
  928. if (rc != USB_STOR_TRANSPORT_GOOD)
  929. goto leave;
  930. info->sectors = ((u32)(reply[117]) << 24) |
  931. ((u32)(reply[116]) << 16) |
  932. ((u32)(reply[115]) << 8) |
  933. ((u32)(reply[114]) );
  934. rc = USB_STOR_TRANSPORT_GOOD;
  935. leave:
  936. kfree(reply);
  937. return rc;
  938. }
  939. /*
  940. * Read data from device
  941. */
  942. static int usbat_flash_read_data(struct us_data *us,
  943. struct usbat_info *info,
  944. u32 sector,
  945. u32 sectors)
  946. {
  947. unsigned char registers[7] = {
  948. USBAT_ATA_FEATURES,
  949. USBAT_ATA_SECCNT,
  950. USBAT_ATA_SECNUM,
  951. USBAT_ATA_LBA_ME,
  952. USBAT_ATA_LBA_HI,
  953. USBAT_ATA_DEVICE,
  954. USBAT_ATA_STATUS,
  955. };
  956. unsigned char command[7];
  957. unsigned char *buffer;
  958. unsigned char thistime;
  959. unsigned int totallen, alloclen;
  960. int len, result;
  961. unsigned int sg_offset = 0;
  962. struct scatterlist *sg = NULL;
  963. result = usbat_flash_check_media(us, info);
  964. if (result != USB_STOR_TRANSPORT_GOOD)
  965. return result;
  966. /*
  967. * we're working in LBA mode. according to the ATA spec,
  968. * we can support up to 28-bit addressing. I don't know if Jumpshot
  969. * supports beyond 24-bit addressing. It's kind of hard to test
  970. * since it requires > 8GB CF card.
  971. */
  972. if (sector > 0x0FFFFFFF)
  973. return USB_STOR_TRANSPORT_ERROR;
  974. totallen = sectors * info->ssize;
  975. /*
  976. * Since we don't read more than 64 KB at a time, we have to create
  977. * a bounce buffer and move the data a piece at a time between the
  978. * bounce buffer and the actual transfer buffer.
  979. */
  980. alloclen = min(totallen, 65536u);
  981. buffer = kmalloc(alloclen, GFP_NOIO);
  982. if (buffer == NULL)
  983. return USB_STOR_TRANSPORT_ERROR;
  984. do {
  985. /*
  986. * loop, never allocate or transfer more than 64k at once
  987. * (min(128k, 255*info->ssize) is the real limit)
  988. */
  989. len = min(totallen, alloclen);
  990. thistime = (len / info->ssize) & 0xff;
  991. /* ATA command 0x20 (READ SECTORS) */
  992. usbat_pack_ata_sector_cmd(command, thistime, sector, 0x20);
  993. /* Write/execute ATA read command */
  994. result = usbat_multiple_write(us, registers, command, 7);
  995. if (result != USB_STOR_TRANSPORT_GOOD)
  996. goto leave;
  997. /* Read the data we just requested */
  998. result = usbat_read_blocks(us, buffer, len, 0);
  999. if (result != USB_STOR_TRANSPORT_GOOD)
  1000. goto leave;
  1001. US_DEBUGP("usbat_flash_read_data: %d bytes\n", len);
  1002. /* Store the data in the transfer buffer */
  1003. usb_stor_access_xfer_buf(buffer, len, us->srb,
  1004. &sg, &sg_offset, TO_XFER_BUF);
  1005. sector += thistime;
  1006. totallen -= len;
  1007. } while (totallen > 0);
  1008. kfree(buffer);
  1009. return USB_STOR_TRANSPORT_GOOD;
  1010. leave:
  1011. kfree(buffer);
  1012. return USB_STOR_TRANSPORT_ERROR;
  1013. }
  1014. /*
  1015. * Write data to device
  1016. */
  1017. static int usbat_flash_write_data(struct us_data *us,
  1018. struct usbat_info *info,
  1019. u32 sector,
  1020. u32 sectors)
  1021. {
  1022. unsigned char registers[7] = {
  1023. USBAT_ATA_FEATURES,
  1024. USBAT_ATA_SECCNT,
  1025. USBAT_ATA_SECNUM,
  1026. USBAT_ATA_LBA_ME,
  1027. USBAT_ATA_LBA_HI,
  1028. USBAT_ATA_DEVICE,
  1029. USBAT_ATA_STATUS,
  1030. };
  1031. unsigned char command[7];
  1032. unsigned char *buffer;
  1033. unsigned char thistime;
  1034. unsigned int totallen, alloclen;
  1035. int len, result;
  1036. unsigned int sg_offset = 0;
  1037. struct scatterlist *sg = NULL;
  1038. result = usbat_flash_check_media(us, info);
  1039. if (result != USB_STOR_TRANSPORT_GOOD)
  1040. return result;
  1041. /*
  1042. * we're working in LBA mode. according to the ATA spec,
  1043. * we can support up to 28-bit addressing. I don't know if the device
  1044. * supports beyond 24-bit addressing. It's kind of hard to test
  1045. * since it requires > 8GB media.
  1046. */
  1047. if (sector > 0x0FFFFFFF)
  1048. return USB_STOR_TRANSPORT_ERROR;
  1049. totallen = sectors * info->ssize;
  1050. /*
  1051. * Since we don't write more than 64 KB at a time, we have to create
  1052. * a bounce buffer and move the data a piece at a time between the
  1053. * bounce buffer and the actual transfer buffer.
  1054. */
  1055. alloclen = min(totallen, 65536u);
  1056. buffer = kmalloc(alloclen, GFP_NOIO);
  1057. if (buffer == NULL)
  1058. return USB_STOR_TRANSPORT_ERROR;
  1059. do {
  1060. /*
  1061. * loop, never allocate or transfer more than 64k at once
  1062. * (min(128k, 255*info->ssize) is the real limit)
  1063. */
  1064. len = min(totallen, alloclen);
  1065. thistime = (len / info->ssize) & 0xff;
  1066. /* Get the data from the transfer buffer */
  1067. usb_stor_access_xfer_buf(buffer, len, us->srb,
  1068. &sg, &sg_offset, FROM_XFER_BUF);
  1069. /* ATA command 0x30 (WRITE SECTORS) */
  1070. usbat_pack_ata_sector_cmd(command, thistime, sector, 0x30);
  1071. /* Write/execute ATA write command */
  1072. result = usbat_multiple_write(us, registers, command, 7);
  1073. if (result != USB_STOR_TRANSPORT_GOOD)
  1074. goto leave;
  1075. /* Write the data */
  1076. result = usbat_write_blocks(us, buffer, len, 0);
  1077. if (result != USB_STOR_TRANSPORT_GOOD)
  1078. goto leave;
  1079. sector += thistime;
  1080. totallen -= len;
  1081. } while (totallen > 0);
  1082. kfree(buffer);
  1083. return result;
  1084. leave:
  1085. kfree(buffer);
  1086. return USB_STOR_TRANSPORT_ERROR;
  1087. }
  1088. /*
  1089. * Squeeze a potentially huge (> 65535 byte) read10 command into
  1090. * a little ( <= 65535 byte) ATAPI pipe
  1091. */
  1092. static int usbat_hp8200e_handle_read10(struct us_data *us,
  1093. unsigned char *registers,
  1094. unsigned char *data,
  1095. struct scsi_cmnd *srb)
  1096. {
  1097. int result = USB_STOR_TRANSPORT_GOOD;
  1098. unsigned char *buffer;
  1099. unsigned int len;
  1100. unsigned int sector;
  1101. unsigned int sg_offset = 0;
  1102. struct scatterlist *sg = NULL;
  1103. US_DEBUGP("handle_read10: transfersize %d\n",
  1104. srb->transfersize);
  1105. if (scsi_bufflen(srb) < 0x10000) {
  1106. result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
  1107. registers, data, 19,
  1108. USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
  1109. (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
  1110. DMA_FROM_DEVICE,
  1111. scsi_sglist(srb),
  1112. scsi_bufflen(srb), scsi_sg_count(srb), 1);
  1113. return result;
  1114. }
  1115. /*
  1116. * Since we're requesting more data than we can handle in
  1117. * a single read command (max is 64k-1), we will perform
  1118. * multiple reads, but each read must be in multiples of
  1119. * a sector. Luckily the sector size is in srb->transfersize
  1120. * (see linux/drivers/scsi/sr.c).
  1121. */
  1122. if (data[7+0] == GPCMD_READ_CD) {
  1123. len = short_pack(data[7+9], data[7+8]);
  1124. len <<= 16;
  1125. len |= data[7+7];
  1126. US_DEBUGP("handle_read10: GPCMD_READ_CD: len %d\n", len);
  1127. srb->transfersize = scsi_bufflen(srb)/len;
  1128. }
  1129. if (!srb->transfersize) {
  1130. srb->transfersize = 2048; /* A guess */
  1131. US_DEBUGP("handle_read10: transfersize 0, forcing %d\n",
  1132. srb->transfersize);
  1133. }
  1134. /*
  1135. * Since we only read in one block at a time, we have to create
  1136. * a bounce buffer and move the data a piece at a time between the
  1137. * bounce buffer and the actual transfer buffer.
  1138. */
  1139. len = (65535/srb->transfersize) * srb->transfersize;
  1140. US_DEBUGP("Max read is %d bytes\n", len);
  1141. len = min(len, scsi_bufflen(srb));
  1142. buffer = kmalloc(len, GFP_NOIO);
  1143. if (buffer == NULL) /* bloody hell! */
  1144. return USB_STOR_TRANSPORT_FAILED;
  1145. sector = short_pack(data[7+3], data[7+2]);
  1146. sector <<= 16;
  1147. sector |= short_pack(data[7+5], data[7+4]);
  1148. transferred = 0;
  1149. while (transferred != scsi_bufflen(srb)) {
  1150. if (len > scsi_bufflen(srb) - transferred)
  1151. len = scsi_bufflen(srb) - transferred;
  1152. data[3] = len&0xFF; /* (cylL) = expected length (L) */
  1153. data[4] = (len>>8)&0xFF; /* (cylH) = expected length (H) */
  1154. /* Fix up the SCSI command sector and num sectors */
  1155. data[7+2] = MSB_of(sector>>16); /* SCSI command sector */
  1156. data[7+3] = LSB_of(sector>>16);
  1157. data[7+4] = MSB_of(sector&0xFFFF);
  1158. data[7+5] = LSB_of(sector&0xFFFF);
  1159. if (data[7+0] == GPCMD_READ_CD)
  1160. data[7+6] = 0;
  1161. data[7+7] = MSB_of(len / srb->transfersize); /* SCSI command */
  1162. data[7+8] = LSB_of(len / srb->transfersize); /* num sectors */
  1163. result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
  1164. registers, data, 19,
  1165. USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
  1166. (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
  1167. DMA_FROM_DEVICE,
  1168. buffer,
  1169. len, 0, 1);
  1170. if (result != USB_STOR_TRANSPORT_GOOD)
  1171. break;
  1172. /* Store the data in the transfer buffer */
  1173. usb_stor_access_xfer_buf(buffer, len, srb,
  1174. &sg, &sg_offset, TO_XFER_BUF);
  1175. /* Update the amount transferred and the sector number */
  1176. transferred += len;
  1177. sector += len / srb->transfersize;
  1178. } /* while transferred != scsi_bufflen(srb) */
  1179. kfree(buffer);
  1180. return result;
  1181. }
  1182. static int usbat_select_and_test_registers(struct us_data *us)
  1183. {
  1184. int selector;
  1185. unsigned char *status = us->iobuf;
  1186. /* try device = master, then device = slave. */
  1187. for (selector = 0xA0; selector <= 0xB0; selector += 0x10) {
  1188. if (usbat_write(us, USBAT_ATA, USBAT_ATA_DEVICE, selector) !=
  1189. USB_STOR_XFER_GOOD)
  1190. return USB_STOR_TRANSPORT_ERROR;
  1191. if (usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status) !=
  1192. USB_STOR_XFER_GOOD)
  1193. return USB_STOR_TRANSPORT_ERROR;
  1194. if (usbat_read(us, USBAT_ATA, USBAT_ATA_DEVICE, status) !=
  1195. USB_STOR_XFER_GOOD)
  1196. return USB_STOR_TRANSPORT_ERROR;
  1197. if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
  1198. USB_STOR_XFER_GOOD)
  1199. return USB_STOR_TRANSPORT_ERROR;
  1200. if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
  1201. USB_STOR_XFER_GOOD)
  1202. return USB_STOR_TRANSPORT_ERROR;
  1203. if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_ME, 0x55) !=
  1204. USB_STOR_XFER_GOOD)
  1205. return USB_STOR_TRANSPORT_ERROR;
  1206. if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_HI, 0xAA) !=
  1207. USB_STOR_XFER_GOOD)
  1208. return USB_STOR_TRANSPORT_ERROR;
  1209. if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
  1210. USB_STOR_XFER_GOOD)
  1211. return USB_STOR_TRANSPORT_ERROR;
  1212. if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
  1213. USB_STOR_XFER_GOOD)
  1214. return USB_STOR_TRANSPORT_ERROR;
  1215. }
  1216. return USB_STOR_TRANSPORT_GOOD;
  1217. }
  1218. /*
  1219. * Initialize the USBAT processor and the storage device
  1220. */
  1221. static int init_usbat(struct us_data *us, int devicetype)
  1222. {
  1223. int rc;
  1224. struct usbat_info *info;
  1225. unsigned char subcountH = USBAT_ATA_LBA_HI;
  1226. unsigned char subcountL = USBAT_ATA_LBA_ME;
  1227. unsigned char *status = us->iobuf;
  1228. us->extra = kzalloc(sizeof(struct usbat_info), GFP_NOIO);
  1229. if (!us->extra) {
  1230. US_DEBUGP("init_usbat: Gah! Can't allocate storage for usbat info struct!\n");
  1231. return 1;
  1232. }
  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. US_DEBUGP("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. US_DEBUGP("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. US_DEBUGP("INIT 3\n");
  1253. rc = usbat_select_and_test_registers(us);
  1254. if (rc != USB_STOR_TRANSPORT_GOOD)
  1255. return rc;
  1256. US_DEBUGP("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. US_DEBUGP("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. US_DEBUGP("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. US_DEBUGP("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. US_DEBUGP("INIT 8\n");
  1275. rc = usbat_select_and_test_registers(us);
  1276. if (rc != USB_STOR_TRANSPORT_GOOD)
  1277. return rc;
  1278. US_DEBUGP("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. US_DEBUGP("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. US_DEBUGP("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. US_DEBUGP("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. US_DEBUGP("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. US_DEBUGP("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. US_DEBUGP("usbat_flash_transport: 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. US_DEBUGP("usbat_flash_transport: 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. US_DEBUGP("usbat_flash_transport: 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. US_DEBUGP("usbat_flash_transport: READ_10: read block 0x%04lx count %ld\n", block, blocks);
  1442. return usbat_flash_read_data(us, info, block, blocks);
  1443. }
  1444. if (srb->cmnd[0] == READ_12) {
  1445. /*
  1446. * I don't think we'll ever see a READ_12 but support it anyway
  1447. */
  1448. block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
  1449. ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
  1450. blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
  1451. ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
  1452. US_DEBUGP("usbat_flash_transport: READ_12: read block 0x%04lx count %ld\n", block, blocks);
  1453. return usbat_flash_read_data(us, info, block, blocks);
  1454. }
  1455. if (srb->cmnd[0] == WRITE_10) {
  1456. block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
  1457. ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
  1458. blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
  1459. US_DEBUGP("usbat_flash_transport: WRITE_10: write block 0x%04lx count %ld\n", block, blocks);
  1460. return usbat_flash_write_data(us, info, block, blocks);
  1461. }
  1462. if (srb->cmnd[0] == WRITE_12) {
  1463. /*
  1464. * I don't think we'll ever see a WRITE_12 but support it anyway
  1465. */
  1466. block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
  1467. ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
  1468. blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
  1469. ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
  1470. US_DEBUGP("usbat_flash_transport: WRITE_12: write block 0x%04lx count %ld\n", block, blocks);
  1471. return usbat_flash_write_data(us, info, block, blocks);
  1472. }
  1473. if (srb->cmnd[0] == TEST_UNIT_READY) {
  1474. US_DEBUGP("usbat_flash_transport: TEST_UNIT_READY.\n");
  1475. rc = usbat_flash_check_media(us, info);
  1476. if (rc != USB_STOR_TRANSPORT_GOOD)
  1477. return rc;
  1478. return usbat_check_status(us);
  1479. }
  1480. if (srb->cmnd[0] == REQUEST_SENSE) {
  1481. US_DEBUGP("usbat_flash_transport: REQUEST_SENSE.\n");
  1482. memset(ptr, 0, 18);
  1483. ptr[0] = 0xF0;
  1484. ptr[2] = info->sense_key;
  1485. ptr[7] = 11;
  1486. ptr[12] = info->sense_asc;
  1487. ptr[13] = info->sense_ascq;
  1488. usb_stor_set_xfer_buf(ptr, 18, srb);
  1489. return USB_STOR_TRANSPORT_GOOD;
  1490. }
  1491. if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
  1492. /*
  1493. * sure. whatever. not like we can stop the user from popping
  1494. * the media out of the device (no locking doors, etc)
  1495. */
  1496. return USB_STOR_TRANSPORT_GOOD;
  1497. }
  1498. US_DEBUGP("usbat_flash_transport: Gah! Unknown command: %d (0x%x)\n",
  1499. srb->cmnd[0], srb->cmnd[0]);
  1500. info->sense_key = 0x05;
  1501. info->sense_asc = 0x20;
  1502. info->sense_ascq = 0x00;
  1503. return USB_STOR_TRANSPORT_FAILED;
  1504. }
  1505. static int init_usbat_cd(struct us_data *us)
  1506. {
  1507. return init_usbat(us, USBAT_DEV_HP8200);
  1508. }
  1509. static int init_usbat_flash(struct us_data *us)
  1510. {
  1511. return init_usbat(us, USBAT_DEV_FLASH);
  1512. }
  1513. static int usbat_probe(struct usb_interface *intf,
  1514. const struct usb_device_id *id)
  1515. {
  1516. struct us_data *us;
  1517. int result;
  1518. result = usb_stor_probe1(&us, intf, id,
  1519. (id - usbat_usb_ids) + usbat_unusual_dev_list);
  1520. if (result)
  1521. return result;
  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 = "ums-usbat",
  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_driver(usbat_driver);