shuttle_usbat.c 45 KB

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