shuttle_usbat.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739
  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_idx = 0, sg_offset = 0;
  852. result = usbat_flash_check_media(us, info);
  853. if (result != USB_STOR_TRANSPORT_GOOD)
  854. return result;
  855. /*
  856. * we're working in LBA mode. according to the ATA spec,
  857. * we can support up to 28-bit addressing. I don't know if Jumpshot
  858. * supports beyond 24-bit addressing. It's kind of hard to test
  859. * since it requires > 8GB CF card.
  860. */
  861. if (sector > 0x0FFFFFFF)
  862. return USB_STOR_TRANSPORT_ERROR;
  863. totallen = sectors * info->ssize;
  864. /*
  865. * Since we don't read more than 64 KB at a time, we have to create
  866. * a bounce buffer and move the data a piece at a time between the
  867. * bounce buffer and the actual transfer buffer.
  868. */
  869. alloclen = min(totallen, 65536u);
  870. buffer = kmalloc(alloclen, GFP_NOIO);
  871. if (buffer == NULL)
  872. return USB_STOR_TRANSPORT_ERROR;
  873. do {
  874. /*
  875. * loop, never allocate or transfer more than 64k at once
  876. * (min(128k, 255*info->ssize) is the real limit)
  877. */
  878. len = min(totallen, alloclen);
  879. thistime = (len / info->ssize) & 0xff;
  880. /* ATA command 0x20 (READ SECTORS) */
  881. usbat_pack_ata_sector_cmd(command, thistime, sector, 0x20);
  882. /* Write/execute ATA read command */
  883. result = usbat_multiple_write(us, registers, command, 7);
  884. if (result != USB_STOR_TRANSPORT_GOOD)
  885. goto leave;
  886. /* Read the data we just requested */
  887. result = usbat_read_blocks(us, buffer, len, 0);
  888. if (result != USB_STOR_TRANSPORT_GOOD)
  889. goto leave;
  890. US_DEBUGP("usbat_flash_read_data: %d bytes\n", len);
  891. /* Store the data in the transfer buffer */
  892. usb_stor_access_xfer_buf(buffer, len, us->srb,
  893. &sg_idx, &sg_offset, TO_XFER_BUF);
  894. sector += thistime;
  895. totallen -= len;
  896. } while (totallen > 0);
  897. kfree(buffer);
  898. return USB_STOR_TRANSPORT_GOOD;
  899. leave:
  900. kfree(buffer);
  901. return USB_STOR_TRANSPORT_ERROR;
  902. }
  903. /*
  904. * Write data to device
  905. */
  906. static int usbat_flash_write_data(struct us_data *us,
  907. struct usbat_info *info,
  908. u32 sector,
  909. u32 sectors)
  910. {
  911. unsigned char registers[7] = {
  912. USBAT_ATA_FEATURES,
  913. USBAT_ATA_SECCNT,
  914. USBAT_ATA_SECNUM,
  915. USBAT_ATA_LBA_ME,
  916. USBAT_ATA_LBA_HI,
  917. USBAT_ATA_DEVICE,
  918. USBAT_ATA_STATUS,
  919. };
  920. unsigned char command[7];
  921. unsigned char *buffer;
  922. unsigned char thistime;
  923. unsigned int totallen, alloclen;
  924. int len, result;
  925. unsigned int sg_idx = 0, sg_offset = 0;
  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_idx, &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_segment = 0;
  990. unsigned int sg_offset = 0;
  991. US_DEBUGP("handle_read10: transfersize %d\n",
  992. srb->transfersize);
  993. if (srb->request_bufflen < 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. srb->request_buffer,
  1000. srb->request_bufflen, srb->use_sg, 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 = srb->request_bufflen/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, srb->request_bufflen);
  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. sg_segment = 0; /* for keeping track of where we are in */
  1038. sg_offset = 0; /* the scatter/gather list */
  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_segment, &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. }