shuttle_usbat.c 45 KB

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