shuttle_usbat.c 45 KB

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