shuttle_usbat.c 45 KB

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