shuttle_usbat.c 45 KB

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