shuttle_usbat.c 49 KB

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