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. 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. 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 prepartion 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. if ( (result = usbat_multiple_write(us,
  1357. registers, data, 7)) != USB_STOR_TRANSPORT_GOOD) {
  1358. return result;
  1359. }
  1360. /*
  1361. * Write the 12-byte command header.
  1362. *
  1363. * If the command is BLANK then set the timer for 75 minutes.
  1364. * Otherwise set it for 10 minutes.
  1365. *
  1366. * NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW
  1367. * AT SPEED 4 IS UNRELIABLE!!!
  1368. */
  1369. if ((result = usbat_write_block(us,
  1370. USBAT_ATA, srb->cmnd, 12,
  1371. (srb->cmnd[0]==GPCMD_BLANK ? 75 : 10), 0) !=
  1372. USB_STOR_TRANSPORT_GOOD)) {
  1373. return result;
  1374. }
  1375. /* If there is response data to be read in then do it here. */
  1376. if (len != 0 && (srb->sc_data_direction == DMA_FROM_DEVICE)) {
  1377. /* How many bytes to read in? Check cylL register */
  1378. if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
  1379. USB_STOR_XFER_GOOD) {
  1380. return USB_STOR_TRANSPORT_ERROR;
  1381. }
  1382. if (len > 0xFF) { /* need to read cylH also */
  1383. len = *status;
  1384. if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
  1385. USB_STOR_XFER_GOOD) {
  1386. return USB_STOR_TRANSPORT_ERROR;
  1387. }
  1388. len += ((unsigned int) *status)<<8;
  1389. }
  1390. else
  1391. len = *status;
  1392. result = usbat_read_block(us, scsi_sglist(srb), len,
  1393. scsi_sg_count(srb));
  1394. }
  1395. return result;
  1396. }
  1397. /*
  1398. * Transport for USBAT02-based CompactFlash and similar storage devices
  1399. */
  1400. static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us)
  1401. {
  1402. int rc;
  1403. struct usbat_info *info = (struct usbat_info *) (us->extra);
  1404. unsigned long block, blocks;
  1405. unsigned char *ptr = us->iobuf;
  1406. static unsigned char inquiry_response[36] = {
  1407. 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
  1408. };
  1409. if (srb->cmnd[0] == INQUIRY) {
  1410. US_DEBUGP("usbat_flash_transport: INQUIRY. Returning bogus response.\n");
  1411. memcpy(ptr, inquiry_response, sizeof(inquiry_response));
  1412. fill_inquiry_response(us, ptr, 36);
  1413. return USB_STOR_TRANSPORT_GOOD;
  1414. }
  1415. if (srb->cmnd[0] == READ_CAPACITY) {
  1416. rc = usbat_flash_check_media(us, info);
  1417. if (rc != USB_STOR_TRANSPORT_GOOD)
  1418. return rc;
  1419. rc = usbat_flash_get_sector_count(us, info);
  1420. if (rc != USB_STOR_TRANSPORT_GOOD)
  1421. return rc;
  1422. /* hard coded 512 byte sectors as per ATA spec */
  1423. info->ssize = 0x200;
  1424. US_DEBUGP("usbat_flash_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
  1425. info->sectors, info->ssize);
  1426. /*
  1427. * build the reply
  1428. * note: must return the sector number of the last sector,
  1429. * *not* the total number of sectors
  1430. */
  1431. ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
  1432. ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
  1433. usb_stor_set_xfer_buf(ptr, 8, srb);
  1434. return USB_STOR_TRANSPORT_GOOD;
  1435. }
  1436. if (srb->cmnd[0] == MODE_SELECT_10) {
  1437. US_DEBUGP("usbat_flash_transport: Gah! MODE_SELECT_10.\n");
  1438. return USB_STOR_TRANSPORT_ERROR;
  1439. }
  1440. if (srb->cmnd[0] == READ_10) {
  1441. block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
  1442. ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
  1443. blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
  1444. US_DEBUGP("usbat_flash_transport: READ_10: read block 0x%04lx count %ld\n", 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. US_DEBUGP("usbat_flash_transport: READ_12: read block 0x%04lx count %ld\n", block, blocks);
  1456. return usbat_flash_read_data(us, info, block, blocks);
  1457. }
  1458. if (srb->cmnd[0] == WRITE_10) {
  1459. block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
  1460. ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
  1461. blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
  1462. US_DEBUGP("usbat_flash_transport: WRITE_10: write block 0x%04lx count %ld\n", block, blocks);
  1463. return usbat_flash_write_data(us, info, block, blocks);
  1464. }
  1465. if (srb->cmnd[0] == WRITE_12) {
  1466. /*
  1467. * I don't think we'll ever see a WRITE_12 but support it anyway
  1468. */
  1469. block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
  1470. ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
  1471. blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
  1472. ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
  1473. US_DEBUGP("usbat_flash_transport: WRITE_12: write block 0x%04lx count %ld\n", block, blocks);
  1474. return usbat_flash_write_data(us, info, block, blocks);
  1475. }
  1476. if (srb->cmnd[0] == TEST_UNIT_READY) {
  1477. US_DEBUGP("usbat_flash_transport: TEST_UNIT_READY.\n");
  1478. rc = usbat_flash_check_media(us, info);
  1479. if (rc != USB_STOR_TRANSPORT_GOOD)
  1480. return rc;
  1481. return usbat_check_status(us);
  1482. }
  1483. if (srb->cmnd[0] == REQUEST_SENSE) {
  1484. US_DEBUGP("usbat_flash_transport: REQUEST_SENSE.\n");
  1485. memset(ptr, 0, 18);
  1486. ptr[0] = 0xF0;
  1487. ptr[2] = info->sense_key;
  1488. ptr[7] = 11;
  1489. ptr[12] = info->sense_asc;
  1490. ptr[13] = info->sense_ascq;
  1491. usb_stor_set_xfer_buf(ptr, 18, srb);
  1492. return USB_STOR_TRANSPORT_GOOD;
  1493. }
  1494. if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
  1495. /*
  1496. * sure. whatever. not like we can stop the user from popping
  1497. * the media out of the device (no locking doors, etc)
  1498. */
  1499. return USB_STOR_TRANSPORT_GOOD;
  1500. }
  1501. US_DEBUGP("usbat_flash_transport: Gah! Unknown command: %d (0x%x)\n",
  1502. srb->cmnd[0], srb->cmnd[0]);
  1503. info->sense_key = 0x05;
  1504. info->sense_asc = 0x20;
  1505. info->sense_ascq = 0x00;
  1506. return USB_STOR_TRANSPORT_FAILED;
  1507. }
  1508. static int init_usbat_cd(struct us_data *us)
  1509. {
  1510. return init_usbat(us, USBAT_DEV_HP8200);
  1511. }
  1512. static int init_usbat_flash(struct us_data *us)
  1513. {
  1514. return init_usbat(us, USBAT_DEV_FLASH);
  1515. }
  1516. static int usbat_probe(struct usb_interface *intf,
  1517. const struct usb_device_id *id)
  1518. {
  1519. struct us_data *us;
  1520. int result;
  1521. result = usb_stor_probe1(&us, intf, id,
  1522. (id - usbat_usb_ids) + usbat_unusual_dev_list);
  1523. if (result)
  1524. return result;
  1525. /* The actual transport will be determined later by the
  1526. * initialization routine; this is just a placeholder.
  1527. */
  1528. us->transport_name = "Shuttle USBAT";
  1529. us->transport = usbat_flash_transport;
  1530. us->transport_reset = usb_stor_CB_reset;
  1531. us->max_lun = 1;
  1532. result = usb_stor_probe2(us);
  1533. return result;
  1534. }
  1535. static struct usb_driver usbat_driver = {
  1536. .name = "ums-usbat",
  1537. .probe = usbat_probe,
  1538. .disconnect = usb_stor_disconnect,
  1539. .suspend = usb_stor_suspend,
  1540. .resume = usb_stor_resume,
  1541. .reset_resume = usb_stor_reset_resume,
  1542. .pre_reset = usb_stor_pre_reset,
  1543. .post_reset = usb_stor_post_reset,
  1544. .id_table = usbat_usb_ids,
  1545. .soft_unbind = 1,
  1546. };
  1547. static int __init usbat_init(void)
  1548. {
  1549. return usb_register(&usbat_driver);
  1550. }
  1551. static void __exit usbat_exit(void)
  1552. {
  1553. usb_deregister(&usbat_driver);
  1554. }
  1555. module_init(usbat_init);
  1556. module_exit(usbat_exit);