shuttle_usbat.c 44 KB

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