shuttle_usbat.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738
  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. unsigned char *data,
  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, data, 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. unsigned char *data,
  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, data, 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. unsigned char *content,
  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, content, 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. unsigned char *content,
  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, content, 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. unsigned char *content,
  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, content, 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. unsigned char *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. unsigned char *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 (srb->request_bufflen < 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. srb->request_buffer,
  1002. srb->request_bufflen, srb->use_sg, 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 = srb->request_bufflen/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, srb->request_bufflen);
  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 != srb->request_bufflen) {
  1040. if (len > srb->request_bufflen - transferred)
  1041. len = srb->request_bufflen - 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 != srb->request_bufflen */
  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. char string[64];
  1196. len = srb->request_bufflen;
  1197. /* Send A0 (ATA PACKET COMMAND).
  1198. Note: I guess we're never going to get any of the ATA
  1199. commands... just ATA Packet Commands.
  1200. */
  1201. registers[0] = USBAT_ATA_FEATURES;
  1202. registers[1] = USBAT_ATA_SECCNT;
  1203. registers[2] = USBAT_ATA_SECNUM;
  1204. registers[3] = USBAT_ATA_LBA_ME;
  1205. registers[4] = USBAT_ATA_LBA_HI;
  1206. registers[5] = USBAT_ATA_DEVICE;
  1207. registers[6] = USBAT_ATA_CMD;
  1208. data[0] = 0x00;
  1209. data[1] = 0x00;
  1210. data[2] = 0x00;
  1211. data[3] = len&0xFF; /* (cylL) = expected length (L) */
  1212. data[4] = (len>>8)&0xFF; /* (cylH) = expected length (H) */
  1213. data[5] = 0xB0; /* (device sel) = slave */
  1214. data[6] = 0xA0; /* (command) = ATA PACKET COMMAND */
  1215. for (i=7; i<19; i++) {
  1216. registers[i] = 0x10;
  1217. data[i] = (i-7 >= srb->cmd_len) ? 0 : srb->cmnd[i-7];
  1218. }
  1219. result = usbat_get_status(us, status);
  1220. US_DEBUGP("Status = %02X\n", *status);
  1221. if (result != USB_STOR_XFER_GOOD)
  1222. return USB_STOR_TRANSPORT_ERROR;
  1223. if (srb->cmnd[0] == TEST_UNIT_READY)
  1224. transferred = 0;
  1225. if (srb->sc_data_direction == DMA_TO_DEVICE) {
  1226. result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
  1227. registers, data, 19,
  1228. USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
  1229. (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
  1230. DMA_TO_DEVICE,
  1231. srb->request_buffer,
  1232. len, srb->use_sg, 10);
  1233. if (result == USB_STOR_TRANSPORT_GOOD) {
  1234. transferred += len;
  1235. US_DEBUGP("Wrote %08X bytes\n", transferred);
  1236. }
  1237. return result;
  1238. } else if (srb->cmnd[0] == READ_10 ||
  1239. srb->cmnd[0] == GPCMD_READ_CD) {
  1240. return usbat_hp8200e_handle_read10(us, registers, data, srb);
  1241. }
  1242. if (len > 0xFFFF) {
  1243. US_DEBUGP("Error: len = %08X... what do I do now?\n",
  1244. len);
  1245. return USB_STOR_TRANSPORT_ERROR;
  1246. }
  1247. if ( (result = usbat_multiple_write(us,
  1248. registers, data, 7)) != USB_STOR_TRANSPORT_GOOD) {
  1249. return result;
  1250. }
  1251. /*
  1252. * Write the 12-byte command header.
  1253. *
  1254. * If the command is BLANK then set the timer for 75 minutes.
  1255. * Otherwise set it for 10 minutes.
  1256. *
  1257. * NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW
  1258. * AT SPEED 4 IS UNRELIABLE!!!
  1259. */
  1260. if ((result = usbat_write_block(us,
  1261. USBAT_ATA, srb->cmnd, 12,
  1262. (srb->cmnd[0]==GPCMD_BLANK ? 75 : 10), 0) !=
  1263. USB_STOR_TRANSPORT_GOOD)) {
  1264. return result;
  1265. }
  1266. /* If there is response data to be read in then do it here. */
  1267. if (len != 0 && (srb->sc_data_direction == DMA_FROM_DEVICE)) {
  1268. /* How many bytes to read in? Check cylL register */
  1269. if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
  1270. USB_STOR_XFER_GOOD) {
  1271. return USB_STOR_TRANSPORT_ERROR;
  1272. }
  1273. if (len > 0xFF) { /* need to read cylH also */
  1274. len = *status;
  1275. if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
  1276. USB_STOR_XFER_GOOD) {
  1277. return USB_STOR_TRANSPORT_ERROR;
  1278. }
  1279. len += ((unsigned int) *status)<<8;
  1280. }
  1281. else
  1282. len = *status;
  1283. result = usbat_read_block(us, srb->request_buffer, len, srb->use_sg);
  1284. /* Debug-print the first 32 bytes of the transfer */
  1285. if (!srb->use_sg) {
  1286. string[0] = 0;
  1287. for (i=0; i<len && i<32; i++) {
  1288. sprintf(string+strlen(string), "%02X ",
  1289. ((unsigned char *)srb->request_buffer)[i]);
  1290. if ((i%16)==15) {
  1291. US_DEBUGP("%s\n", string);
  1292. string[0] = 0;
  1293. }
  1294. }
  1295. if (string[0]!=0)
  1296. US_DEBUGP("%s\n", string);
  1297. }
  1298. }
  1299. return result;
  1300. }
  1301. /*
  1302. * Transport for USBAT02-based CompactFlash and similar storage devices
  1303. */
  1304. static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us)
  1305. {
  1306. int rc;
  1307. struct usbat_info *info = (struct usbat_info *) (us->extra);
  1308. unsigned long block, blocks;
  1309. unsigned char *ptr = us->iobuf;
  1310. static unsigned char inquiry_response[36] = {
  1311. 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
  1312. };
  1313. if (srb->cmnd[0] == INQUIRY) {
  1314. US_DEBUGP("usbat_flash_transport: INQUIRY. Returning bogus response.\n");
  1315. memcpy(ptr, inquiry_response, sizeof(inquiry_response));
  1316. fill_inquiry_response(us, ptr, 36);
  1317. return USB_STOR_TRANSPORT_GOOD;
  1318. }
  1319. if (srb->cmnd[0] == READ_CAPACITY) {
  1320. rc = usbat_flash_check_media(us, info);
  1321. if (rc != USB_STOR_TRANSPORT_GOOD)
  1322. return rc;
  1323. rc = usbat_flash_get_sector_count(us, info);
  1324. if (rc != USB_STOR_TRANSPORT_GOOD)
  1325. return rc;
  1326. /* hard coded 512 byte sectors as per ATA spec */
  1327. info->ssize = 0x200;
  1328. US_DEBUGP("usbat_flash_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
  1329. info->sectors, info->ssize);
  1330. /*
  1331. * build the reply
  1332. * note: must return the sector number of the last sector,
  1333. * *not* the total number of sectors
  1334. */
  1335. ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
  1336. ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
  1337. usb_stor_set_xfer_buf(ptr, 8, srb);
  1338. return USB_STOR_TRANSPORT_GOOD;
  1339. }
  1340. if (srb->cmnd[0] == MODE_SELECT_10) {
  1341. US_DEBUGP("usbat_flash_transport: Gah! MODE_SELECT_10.\n");
  1342. return USB_STOR_TRANSPORT_ERROR;
  1343. }
  1344. if (srb->cmnd[0] == READ_10) {
  1345. block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
  1346. ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
  1347. blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
  1348. US_DEBUGP("usbat_flash_transport: READ_10: read block 0x%04lx count %ld\n", block, blocks);
  1349. return usbat_flash_read_data(us, info, block, blocks);
  1350. }
  1351. if (srb->cmnd[0] == READ_12) {
  1352. /*
  1353. * I don't think we'll ever see a READ_12 but support it anyway
  1354. */
  1355. block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
  1356. ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
  1357. blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
  1358. ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
  1359. US_DEBUGP("usbat_flash_transport: READ_12: read block 0x%04lx count %ld\n", block, blocks);
  1360. return usbat_flash_read_data(us, info, block, blocks);
  1361. }
  1362. if (srb->cmnd[0] == WRITE_10) {
  1363. block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
  1364. ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
  1365. blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
  1366. US_DEBUGP("usbat_flash_transport: WRITE_10: write block 0x%04lx count %ld\n", block, blocks);
  1367. return usbat_flash_write_data(us, info, block, blocks);
  1368. }
  1369. if (srb->cmnd[0] == WRITE_12) {
  1370. /*
  1371. * I don't think we'll ever see a WRITE_12 but support it anyway
  1372. */
  1373. block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
  1374. ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
  1375. blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
  1376. ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
  1377. US_DEBUGP("usbat_flash_transport: WRITE_12: write block 0x%04lx count %ld\n", block, blocks);
  1378. return usbat_flash_write_data(us, info, block, blocks);
  1379. }
  1380. if (srb->cmnd[0] == TEST_UNIT_READY) {
  1381. US_DEBUGP("usbat_flash_transport: TEST_UNIT_READY.\n");
  1382. rc = usbat_flash_check_media(us, info);
  1383. if (rc != USB_STOR_TRANSPORT_GOOD)
  1384. return rc;
  1385. return usbat_check_status(us);
  1386. }
  1387. if (srb->cmnd[0] == REQUEST_SENSE) {
  1388. US_DEBUGP("usbat_flash_transport: REQUEST_SENSE.\n");
  1389. memset(ptr, 0, 18);
  1390. ptr[0] = 0xF0;
  1391. ptr[2] = info->sense_key;
  1392. ptr[7] = 11;
  1393. ptr[12] = info->sense_asc;
  1394. ptr[13] = info->sense_ascq;
  1395. usb_stor_set_xfer_buf(ptr, 18, srb);
  1396. return USB_STOR_TRANSPORT_GOOD;
  1397. }
  1398. if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
  1399. /*
  1400. * sure. whatever. not like we can stop the user from popping
  1401. * the media out of the device (no locking doors, etc)
  1402. */
  1403. return USB_STOR_TRANSPORT_GOOD;
  1404. }
  1405. US_DEBUGP("usbat_flash_transport: Gah! Unknown command: %d (0x%x)\n",
  1406. srb->cmnd[0], srb->cmnd[0]);
  1407. info->sense_key = 0x05;
  1408. info->sense_asc = 0x20;
  1409. info->sense_ascq = 0x00;
  1410. return USB_STOR_TRANSPORT_FAILED;
  1411. }
  1412. int init_usbat_cd(struct us_data *us)
  1413. {
  1414. return init_usbat(us, USBAT_DEV_HP8200);
  1415. }
  1416. int init_usbat_flash(struct us_data *us)
  1417. {
  1418. return init_usbat(us, USBAT_DEV_FLASH);
  1419. }
  1420. int init_usbat_probe(struct us_data *us)
  1421. {
  1422. return init_usbat(us, 0);
  1423. }
  1424. /*
  1425. * Default transport function. Attempts to detect which transport function
  1426. * should be called, makes it the new default, and calls it.
  1427. *
  1428. * This function should never be called. Our usbat_init() function detects the
  1429. * device type and changes the us->transport ptr to the transport function
  1430. * relevant to the device.
  1431. * However, we'll support this impossible(?) case anyway.
  1432. */
  1433. int usbat_transport(struct scsi_cmnd *srb, struct us_data *us)
  1434. {
  1435. struct usbat_info *info = (struct usbat_info*) (us->extra);
  1436. if (usbat_set_transport(us, info, 0))
  1437. return USB_STOR_TRANSPORT_ERROR;
  1438. return us->transport(srb, us);
  1439. }