shuttle_usbat.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679
  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. msleep(25);
  717. /*
  718. * In attempt to distinguish between HP CDRW's and Flash readers, we now
  719. * execute the IDENTIFY PACKET DEVICE command. On ATA devices (i.e. flash
  720. * readers), this command should fail with error. On ATAPI devices (i.e.
  721. * CDROM drives), it should succeed.
  722. */
  723. rc = usbat_write(us, USBAT_ATA, USBAT_ATA_CMD, 0xA1);
  724. if (rc != USB_STOR_XFER_GOOD)
  725. return USB_STOR_TRANSPORT_ERROR;
  726. rc = usbat_get_status(us, &status);
  727. if (rc != USB_STOR_XFER_GOOD)
  728. return USB_STOR_TRANSPORT_ERROR;
  729. // Check for error bit
  730. if (status & 0x01) {
  731. // Device is a CompactFlash reader/writer
  732. US_DEBUGP("usbat_identify_device: Detected Flash reader/writer\n");
  733. info->devicetype = USBAT_DEV_FLASH;
  734. } else {
  735. // Device is HP 8200
  736. US_DEBUGP("usbat_identify_device: Detected HP8200 CDRW\n");
  737. info->devicetype = USBAT_DEV_HP8200;
  738. }
  739. return USB_STOR_TRANSPORT_GOOD;
  740. }
  741. /*
  742. * Set the transport function based on the device type
  743. */
  744. static int usbat_set_transport(struct us_data *us,
  745. struct usbat_info *info)
  746. {
  747. int rc;
  748. if (!info->devicetype) {
  749. rc = usbat_identify_device(us, info);
  750. if (rc != USB_STOR_TRANSPORT_GOOD) {
  751. US_DEBUGP("usbat_set_transport: Could not identify device\n");
  752. return 1;
  753. }
  754. }
  755. if (usbat_get_device_type(us) == USBAT_DEV_HP8200)
  756. us->transport = usbat_hp8200e_transport;
  757. else if (usbat_get_device_type(us) == USBAT_DEV_FLASH)
  758. us->transport = usbat_flash_transport;
  759. return 0;
  760. }
  761. /*
  762. * Read the media capacity
  763. */
  764. static int usbat_flash_get_sector_count(struct us_data *us,
  765. struct usbat_info *info)
  766. {
  767. unsigned char registers[3] = {
  768. USBAT_ATA_SECCNT,
  769. USBAT_ATA_DEVICE,
  770. USBAT_ATA_CMD,
  771. };
  772. unsigned char command[3] = { 0x01, 0xA0, 0xEC };
  773. unsigned char *reply;
  774. unsigned char status;
  775. int rc;
  776. if (!us || !info)
  777. return USB_STOR_TRANSPORT_ERROR;
  778. reply = kmalloc(512, GFP_NOIO);
  779. if (!reply)
  780. return USB_STOR_TRANSPORT_ERROR;
  781. // ATAPI command : IDENTIFY DEVICE
  782. rc = usbat_multiple_write(us, registers, command, 3);
  783. if (rc != USB_STOR_XFER_GOOD) {
  784. US_DEBUGP("usbat_flash_get_sector_count: Gah! identify_device failed\n");
  785. rc = USB_STOR_TRANSPORT_ERROR;
  786. goto leave;
  787. }
  788. // Read device status
  789. if (usbat_get_status(us, &status) != USB_STOR_XFER_GOOD) {
  790. rc = USB_STOR_TRANSPORT_ERROR;
  791. goto leave;
  792. }
  793. msleep(100);
  794. // Read the device identification data
  795. rc = usbat_read_block(us, reply, 512);
  796. if (rc != USB_STOR_TRANSPORT_GOOD)
  797. goto leave;
  798. info->sectors = ((u32)(reply[117]) << 24) |
  799. ((u32)(reply[116]) << 16) |
  800. ((u32)(reply[115]) << 8) |
  801. ((u32)(reply[114]) );
  802. rc = USB_STOR_TRANSPORT_GOOD;
  803. leave:
  804. kfree(reply);
  805. return rc;
  806. }
  807. /*
  808. * Read data from device
  809. */
  810. static int usbat_flash_read_data(struct us_data *us,
  811. struct usbat_info *info,
  812. u32 sector,
  813. u32 sectors)
  814. {
  815. unsigned char registers[7] = {
  816. USBAT_ATA_FEATURES,
  817. USBAT_ATA_SECCNT,
  818. USBAT_ATA_SECNUM,
  819. USBAT_ATA_LBA_ME,
  820. USBAT_ATA_LBA_HI,
  821. USBAT_ATA_DEVICE,
  822. USBAT_ATA_STATUS,
  823. };
  824. unsigned char command[7];
  825. unsigned char *buffer;
  826. unsigned char thistime;
  827. unsigned int totallen, alloclen;
  828. int len, result;
  829. unsigned int sg_idx = 0, sg_offset = 0;
  830. result = usbat_flash_check_media(us, info);
  831. if (result != USB_STOR_TRANSPORT_GOOD)
  832. return result;
  833. // we're working in LBA mode. according to the ATA spec,
  834. // we can support up to 28-bit addressing. I don't know if Jumpshot
  835. // supports beyond 24-bit addressing. It's kind of hard to test
  836. // since it requires > 8GB CF card.
  837. if (sector > 0x0FFFFFFF)
  838. return USB_STOR_TRANSPORT_ERROR;
  839. totallen = sectors * info->ssize;
  840. // Since we don't read more than 64 KB at a time, we have to create
  841. // a bounce buffer and move the data a piece at a time between the
  842. // bounce buffer and the actual transfer buffer.
  843. alloclen = min(totallen, 65536u);
  844. buffer = kmalloc(alloclen, GFP_NOIO);
  845. if (buffer == NULL)
  846. return USB_STOR_TRANSPORT_ERROR;
  847. do {
  848. // loop, never allocate or transfer more than 64k at once
  849. // (min(128k, 255*info->ssize) is the real limit)
  850. len = min(totallen, alloclen);
  851. thistime = (len / info->ssize) & 0xff;
  852. // ATAPI command 0x20 (READ SECTORS)
  853. usbat_pack_atapi_sector_cmd(command, thistime, sector, 0x20);
  854. // Write/execute ATAPI read command
  855. result = usbat_multiple_write(us, registers, command, 7);
  856. if (result != USB_STOR_TRANSPORT_GOOD)
  857. goto leave;
  858. // Read the data we just requested
  859. result = usbat_read_blocks(us, buffer, len);
  860. if (result != USB_STOR_TRANSPORT_GOOD)
  861. goto leave;
  862. US_DEBUGP("usbat_flash_read_data: %d bytes\n", len);
  863. // Store the data in the transfer buffer
  864. usb_stor_access_xfer_buf(buffer, len, us->srb,
  865. &sg_idx, &sg_offset, TO_XFER_BUF);
  866. sector += thistime;
  867. totallen -= len;
  868. } while (totallen > 0);
  869. kfree(buffer);
  870. return USB_STOR_TRANSPORT_GOOD;
  871. leave:
  872. kfree(buffer);
  873. return USB_STOR_TRANSPORT_ERROR;
  874. }
  875. /*
  876. * Write data to device
  877. */
  878. static int usbat_flash_write_data(struct us_data *us,
  879. struct usbat_info *info,
  880. u32 sector,
  881. u32 sectors)
  882. {
  883. unsigned char registers[7] = {
  884. USBAT_ATA_FEATURES,
  885. USBAT_ATA_SECCNT,
  886. USBAT_ATA_SECNUM,
  887. USBAT_ATA_LBA_ME,
  888. USBAT_ATA_LBA_HI,
  889. USBAT_ATA_DEVICE,
  890. USBAT_ATA_STATUS,
  891. };
  892. unsigned char command[7];
  893. unsigned char *buffer;
  894. unsigned char thistime;
  895. unsigned int totallen, alloclen;
  896. int len, result;
  897. unsigned int sg_idx = 0, sg_offset = 0;
  898. result = usbat_flash_check_media(us, info);
  899. if (result != USB_STOR_TRANSPORT_GOOD)
  900. return result;
  901. // we're working in LBA mode. according to the ATA spec,
  902. // we can support up to 28-bit addressing. I don't know if Jumpshot
  903. // supports beyond 24-bit addressing. It's kind of hard to test
  904. // since it requires > 8GB CF card.
  905. if (sector > 0x0FFFFFFF)
  906. return USB_STOR_TRANSPORT_ERROR;
  907. totallen = sectors * info->ssize;
  908. // Since we don't write more than 64 KB at a time, we have to create
  909. // a bounce buffer and move the data a piece at a time between the
  910. // bounce buffer and the actual transfer buffer.
  911. alloclen = min(totallen, 65536u);
  912. buffer = kmalloc(alloclen, GFP_NOIO);
  913. if (buffer == NULL)
  914. return USB_STOR_TRANSPORT_ERROR;
  915. do {
  916. // loop, never allocate or transfer more than 64k at once
  917. // (min(128k, 255*info->ssize) is the real limit)
  918. len = min(totallen, alloclen);
  919. thistime = (len / info->ssize) & 0xff;
  920. // Get the data from the transfer buffer
  921. usb_stor_access_xfer_buf(buffer, len, us->srb,
  922. &sg_idx, &sg_offset, FROM_XFER_BUF);
  923. // ATAPI command 0x30 (WRITE SECTORS)
  924. usbat_pack_atapi_sector_cmd(command, thistime, sector, 0x30);
  925. // Write/execute ATAPI write command
  926. result = usbat_multiple_write(us, registers, command, 7);
  927. if (result != USB_STOR_TRANSPORT_GOOD)
  928. goto leave;
  929. // Write the data
  930. result = usbat_write_blocks(us, buffer, len);
  931. if (result != USB_STOR_TRANSPORT_GOOD)
  932. goto leave;
  933. sector += thistime;
  934. totallen -= len;
  935. } while (totallen > 0);
  936. kfree(buffer);
  937. return result;
  938. leave:
  939. kfree(buffer);
  940. return USB_STOR_TRANSPORT_ERROR;
  941. }
  942. /*
  943. * Squeeze a potentially huge (> 65535 byte) read10 command into
  944. * a little ( <= 65535 byte) ATAPI pipe
  945. */
  946. static int usbat_hp8200e_handle_read10(struct us_data *us,
  947. unsigned char *registers,
  948. unsigned char *data,
  949. struct scsi_cmnd *srb)
  950. {
  951. int result = USB_STOR_TRANSPORT_GOOD;
  952. unsigned char *buffer;
  953. unsigned int len;
  954. unsigned int sector;
  955. unsigned int sg_segment = 0;
  956. unsigned int sg_offset = 0;
  957. US_DEBUGP("handle_read10: transfersize %d\n",
  958. srb->transfersize);
  959. if (srb->request_bufflen < 0x10000) {
  960. result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
  961. registers, data, 19,
  962. USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
  963. (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
  964. DMA_FROM_DEVICE,
  965. srb->request_buffer,
  966. srb->request_bufflen, srb->use_sg, 1);
  967. return result;
  968. }
  969. /*
  970. * Since we're requesting more data than we can handle in
  971. * a single read command (max is 64k-1), we will perform
  972. * multiple reads, but each read must be in multiples of
  973. * a sector. Luckily the sector size is in srb->transfersize
  974. * (see linux/drivers/scsi/sr.c).
  975. */
  976. if (data[7+0] == GPCMD_READ_CD) {
  977. len = short_pack(data[7+9], data[7+8]);
  978. len <<= 16;
  979. len |= data[7+7];
  980. US_DEBUGP("handle_read10: GPCMD_READ_CD: len %d\n", len);
  981. srb->transfersize = srb->request_bufflen/len;
  982. }
  983. if (!srb->transfersize) {
  984. srb->transfersize = 2048; /* A guess */
  985. US_DEBUGP("handle_read10: transfersize 0, forcing %d\n",
  986. srb->transfersize);
  987. }
  988. // Since we only read in one block at a time, we have to create
  989. // a bounce buffer and move the data a piece at a time between the
  990. // bounce buffer and the actual transfer buffer.
  991. len = (65535/srb->transfersize) * srb->transfersize;
  992. US_DEBUGP("Max read is %d bytes\n", len);
  993. len = min(len, srb->request_bufflen);
  994. buffer = kmalloc(len, GFP_NOIO);
  995. if (buffer == NULL) // bloody hell!
  996. return USB_STOR_TRANSPORT_FAILED;
  997. sector = short_pack(data[7+3], data[7+2]);
  998. sector <<= 16;
  999. sector |= short_pack(data[7+5], data[7+4]);
  1000. transferred = 0;
  1001. sg_segment = 0; // for keeping track of where we are in
  1002. sg_offset = 0; // the scatter/gather list
  1003. while (transferred != srb->request_bufflen) {
  1004. if (len > srb->request_bufflen - transferred)
  1005. len = srb->request_bufflen - transferred;
  1006. data[3] = len&0xFF; // (cylL) = expected length (L)
  1007. data[4] = (len>>8)&0xFF; // (cylH) = expected length (H)
  1008. // Fix up the SCSI command sector and num sectors
  1009. data[7+2] = MSB_of(sector>>16); // SCSI command sector
  1010. data[7+3] = LSB_of(sector>>16);
  1011. data[7+4] = MSB_of(sector&0xFFFF);
  1012. data[7+5] = LSB_of(sector&0xFFFF);
  1013. if (data[7+0] == GPCMD_READ_CD)
  1014. data[7+6] = 0;
  1015. data[7+7] = MSB_of(len / srb->transfersize); // SCSI command
  1016. data[7+8] = LSB_of(len / srb->transfersize); // num sectors
  1017. result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
  1018. registers, data, 19,
  1019. USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
  1020. (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
  1021. DMA_FROM_DEVICE,
  1022. buffer,
  1023. len, 0, 1);
  1024. if (result != USB_STOR_TRANSPORT_GOOD)
  1025. break;
  1026. // Store the data in the transfer buffer
  1027. usb_stor_access_xfer_buf(buffer, len, srb,
  1028. &sg_segment, &sg_offset, TO_XFER_BUF);
  1029. // Update the amount transferred and the sector number
  1030. transferred += len;
  1031. sector += len / srb->transfersize;
  1032. } // while transferred != srb->request_bufflen
  1033. kfree(buffer);
  1034. return result;
  1035. }
  1036. static int usbat_select_and_test_registers(struct us_data *us)
  1037. {
  1038. int selector;
  1039. unsigned char *status = us->iobuf;
  1040. // try device = master, then device = slave.
  1041. for (selector = 0xA0; selector <= 0xB0; selector += 0x10) {
  1042. if (usbat_write(us, USBAT_ATA, USBAT_ATA_DEVICE, selector) !=
  1043. USB_STOR_XFER_GOOD)
  1044. return USB_STOR_TRANSPORT_ERROR;
  1045. if (usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status) !=
  1046. USB_STOR_XFER_GOOD)
  1047. return USB_STOR_TRANSPORT_ERROR;
  1048. if (usbat_read(us, USBAT_ATA, USBAT_ATA_DEVICE, status) !=
  1049. USB_STOR_XFER_GOOD)
  1050. return USB_STOR_TRANSPORT_ERROR;
  1051. if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
  1052. USB_STOR_XFER_GOOD)
  1053. return USB_STOR_TRANSPORT_ERROR;
  1054. if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
  1055. USB_STOR_XFER_GOOD)
  1056. return USB_STOR_TRANSPORT_ERROR;
  1057. if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_ME, 0x55) !=
  1058. USB_STOR_XFER_GOOD)
  1059. return USB_STOR_TRANSPORT_ERROR;
  1060. if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_HI, 0xAA) !=
  1061. USB_STOR_XFER_GOOD)
  1062. return USB_STOR_TRANSPORT_ERROR;
  1063. if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
  1064. USB_STOR_XFER_GOOD)
  1065. return USB_STOR_TRANSPORT_ERROR;
  1066. if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
  1067. USB_STOR_XFER_GOOD)
  1068. return USB_STOR_TRANSPORT_ERROR;
  1069. }
  1070. return USB_STOR_TRANSPORT_GOOD;
  1071. }
  1072. /*
  1073. * Initialize the USBAT processor and the storage device
  1074. */
  1075. int init_usbat(struct us_data *us)
  1076. {
  1077. int rc;
  1078. struct usbat_info *info;
  1079. unsigned char subcountH = USBAT_ATA_LBA_HI;
  1080. unsigned char subcountL = USBAT_ATA_LBA_ME;
  1081. unsigned char *status = us->iobuf;
  1082. us->extra = kmalloc(sizeof(struct usbat_info), GFP_NOIO);
  1083. if (!us->extra) {
  1084. US_DEBUGP("init_usbat: Gah! Can't allocate storage for usbat info struct!\n");
  1085. return 1;
  1086. }
  1087. memset(us->extra, 0, sizeof(struct usbat_info));
  1088. info = (struct usbat_info *) (us->extra);
  1089. // Enable peripheral control signals
  1090. rc = usbat_write_user_io(us,
  1091. USBAT_UIO_OE1 | USBAT_UIO_OE0,
  1092. USBAT_UIO_EPAD | USBAT_UIO_1);
  1093. if (rc != USB_STOR_XFER_GOOD)
  1094. return USB_STOR_TRANSPORT_ERROR;
  1095. US_DEBUGP("INIT 1\n");
  1096. msleep(2000);
  1097. rc = usbat_read_user_io(us, status);
  1098. if (rc != USB_STOR_TRANSPORT_GOOD)
  1099. return rc;
  1100. US_DEBUGP("INIT 2\n");
  1101. rc = usbat_read_user_io(us, status);
  1102. if (rc != USB_STOR_XFER_GOOD)
  1103. return USB_STOR_TRANSPORT_ERROR;
  1104. rc = usbat_read_user_io(us, status);
  1105. if (rc != USB_STOR_XFER_GOOD)
  1106. return USB_STOR_TRANSPORT_ERROR;
  1107. US_DEBUGP("INIT 3\n");
  1108. rc = usbat_select_and_test_registers(us);
  1109. if (rc != USB_STOR_TRANSPORT_GOOD)
  1110. return rc;
  1111. US_DEBUGP("INIT 4\n");
  1112. rc = usbat_read_user_io(us, status);
  1113. if (rc != USB_STOR_XFER_GOOD)
  1114. return USB_STOR_TRANSPORT_ERROR;
  1115. US_DEBUGP("INIT 5\n");
  1116. // Enable peripheral control signals and card detect
  1117. rc = usbat_device_enable_cdt(us);
  1118. if (rc != USB_STOR_TRANSPORT_GOOD)
  1119. return rc;
  1120. US_DEBUGP("INIT 6\n");
  1121. rc = usbat_read_user_io(us, status);
  1122. if (rc != USB_STOR_XFER_GOOD)
  1123. return USB_STOR_TRANSPORT_ERROR;
  1124. US_DEBUGP("INIT 7\n");
  1125. msleep(1400);
  1126. rc = usbat_read_user_io(us, status);
  1127. if (rc != USB_STOR_XFER_GOOD)
  1128. return USB_STOR_TRANSPORT_ERROR;
  1129. US_DEBUGP("INIT 8\n");
  1130. rc = usbat_select_and_test_registers(us);
  1131. if (rc != USB_STOR_TRANSPORT_GOOD)
  1132. return rc;
  1133. US_DEBUGP("INIT 9\n");
  1134. // At this point, we need to detect which device we are using
  1135. if (usbat_set_transport(us, info))
  1136. return USB_STOR_TRANSPORT_ERROR;
  1137. US_DEBUGP("INIT 10\n");
  1138. if (usbat_get_device_type(us) == USBAT_DEV_FLASH) {
  1139. subcountH = 0x02;
  1140. subcountL = 0x00;
  1141. }
  1142. rc = usbat_set_shuttle_features(us, (USBAT_FEAT_ETEN | USBAT_FEAT_ET2 | USBAT_FEAT_ET1),
  1143. 0x00, 0x88, 0x08, subcountH, subcountL);
  1144. if (rc != USB_STOR_XFER_GOOD)
  1145. return USB_STOR_TRANSPORT_ERROR;
  1146. US_DEBUGP("INIT 11\n");
  1147. return USB_STOR_TRANSPORT_GOOD;
  1148. }
  1149. /*
  1150. * Transport for the HP 8200e
  1151. */
  1152. static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us)
  1153. {
  1154. int result;
  1155. unsigned char *status = us->iobuf;
  1156. unsigned char registers[32];
  1157. unsigned char data[32];
  1158. unsigned int len;
  1159. int i;
  1160. char string[64];
  1161. len = srb->request_bufflen;
  1162. /* Send A0 (ATA PACKET COMMAND).
  1163. Note: I guess we're never going to get any of the ATA
  1164. commands... just ATA Packet Commands.
  1165. */
  1166. registers[0] = USBAT_ATA_FEATURES;
  1167. registers[1] = USBAT_ATA_SECCNT;
  1168. registers[2] = USBAT_ATA_SECNUM;
  1169. registers[3] = USBAT_ATA_LBA_ME;
  1170. registers[4] = USBAT_ATA_LBA_HI;
  1171. registers[5] = USBAT_ATA_DEVICE;
  1172. registers[6] = USBAT_ATA_CMD;
  1173. data[0] = 0x00;
  1174. data[1] = 0x00;
  1175. data[2] = 0x00;
  1176. data[3] = len&0xFF; // (cylL) = expected length (L)
  1177. data[4] = (len>>8)&0xFF; // (cylH) = expected length (H)
  1178. data[5] = 0xB0; // (device sel) = slave
  1179. data[6] = 0xA0; // (command) = ATA PACKET COMMAND
  1180. for (i=7; i<19; i++) {
  1181. registers[i] = 0x10;
  1182. data[i] = (i-7 >= srb->cmd_len) ? 0 : srb->cmnd[i-7];
  1183. }
  1184. result = usbat_get_status(us, status);
  1185. US_DEBUGP("Status = %02X\n", *status);
  1186. if (result != USB_STOR_XFER_GOOD)
  1187. return USB_STOR_TRANSPORT_ERROR;
  1188. if (srb->cmnd[0] == TEST_UNIT_READY)
  1189. transferred = 0;
  1190. if (srb->sc_data_direction == DMA_TO_DEVICE) {
  1191. result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
  1192. registers, data, 19,
  1193. USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
  1194. (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
  1195. DMA_TO_DEVICE,
  1196. srb->request_buffer,
  1197. len, srb->use_sg, 10);
  1198. if (result == USB_STOR_TRANSPORT_GOOD) {
  1199. transferred += len;
  1200. US_DEBUGP("Wrote %08X bytes\n", transferred);
  1201. }
  1202. return result;
  1203. } else if (srb->cmnd[0] == READ_10 ||
  1204. srb->cmnd[0] == GPCMD_READ_CD) {
  1205. return usbat_hp8200e_handle_read10(us, registers, data, srb);
  1206. }
  1207. if (len > 0xFFFF) {
  1208. US_DEBUGP("Error: len = %08X... what do I do now?\n",
  1209. len);
  1210. return USB_STOR_TRANSPORT_ERROR;
  1211. }
  1212. if ( (result = usbat_multiple_write(us,
  1213. registers, data, 7)) != USB_STOR_TRANSPORT_GOOD) {
  1214. return result;
  1215. }
  1216. // Write the 12-byte command header.
  1217. // If the command is BLANK then set the timer for 75 minutes.
  1218. // Otherwise set it for 10 minutes.
  1219. // NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW
  1220. // AT SPEED 4 IS UNRELIABLE!!!
  1221. if ( (result = usbat_write_block(us,
  1222. USBAT_ATA, srb->cmnd, 12,
  1223. srb->cmnd[0]==GPCMD_BLANK ? 75 : 10)) !=
  1224. USB_STOR_TRANSPORT_GOOD) {
  1225. return result;
  1226. }
  1227. // If there is response data to be read in
  1228. // then do it here.
  1229. if (len != 0 && (srb->sc_data_direction == DMA_FROM_DEVICE)) {
  1230. // How many bytes to read in? Check cylL register
  1231. if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
  1232. USB_STOR_XFER_GOOD) {
  1233. return USB_STOR_TRANSPORT_ERROR;
  1234. }
  1235. if (len > 0xFF) { // need to read cylH also
  1236. len = *status;
  1237. if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
  1238. USB_STOR_XFER_GOOD) {
  1239. return USB_STOR_TRANSPORT_ERROR;
  1240. }
  1241. len += ((unsigned int) *status)<<8;
  1242. }
  1243. else
  1244. len = *status;
  1245. result = usbat_read_block(us, srb->request_buffer, len);
  1246. /* Debug-print the first 32 bytes of the transfer */
  1247. if (!srb->use_sg) {
  1248. string[0] = 0;
  1249. for (i=0; i<len && i<32; i++) {
  1250. sprintf(string+strlen(string), "%02X ",
  1251. ((unsigned char *)srb->request_buffer)[i]);
  1252. if ((i%16)==15) {
  1253. US_DEBUGP("%s\n", string);
  1254. string[0] = 0;
  1255. }
  1256. }
  1257. if (string[0]!=0)
  1258. US_DEBUGP("%s\n", string);
  1259. }
  1260. }
  1261. return result;
  1262. }
  1263. /*
  1264. * Transport for USBAT02-based CompactFlash and similar storage devices
  1265. */
  1266. static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us)
  1267. {
  1268. int rc;
  1269. struct usbat_info *info = (struct usbat_info *) (us->extra);
  1270. unsigned long block, blocks;
  1271. unsigned char *ptr = us->iobuf;
  1272. static unsigned char inquiry_response[36] = {
  1273. 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
  1274. };
  1275. if (srb->cmnd[0] == INQUIRY) {
  1276. US_DEBUGP("usbat_flash_transport: INQUIRY. Returning bogus response.\n");
  1277. memcpy(ptr, inquiry_response, sizeof(inquiry_response));
  1278. fill_inquiry_response(us, ptr, 36);
  1279. return USB_STOR_TRANSPORT_GOOD;
  1280. }
  1281. if (srb->cmnd[0] == READ_CAPACITY) {
  1282. rc = usbat_flash_check_media(us, info);
  1283. if (rc != USB_STOR_TRANSPORT_GOOD)
  1284. return rc;
  1285. rc = usbat_flash_get_sector_count(us, info);
  1286. if (rc != USB_STOR_TRANSPORT_GOOD)
  1287. return rc;
  1288. info->ssize = 0x200; // hard coded 512 byte sectors as per ATA spec
  1289. US_DEBUGP("usbat_flash_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
  1290. info->sectors, info->ssize);
  1291. // build the reply
  1292. // note: must return the sector number of the last sector,
  1293. // *not* the total number of sectors
  1294. ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
  1295. ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
  1296. usb_stor_set_xfer_buf(ptr, 8, srb);
  1297. return USB_STOR_TRANSPORT_GOOD;
  1298. }
  1299. if (srb->cmnd[0] == MODE_SELECT_10) {
  1300. US_DEBUGP("usbat_flash_transport: Gah! MODE_SELECT_10.\n");
  1301. return USB_STOR_TRANSPORT_ERROR;
  1302. }
  1303. if (srb->cmnd[0] == READ_10) {
  1304. block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
  1305. ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
  1306. blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
  1307. US_DEBUGP("usbat_flash_transport: READ_10: read block 0x%04lx count %ld\n", block, blocks);
  1308. return usbat_flash_read_data(us, info, block, blocks);
  1309. }
  1310. if (srb->cmnd[0] == READ_12) {
  1311. // I don't think we'll ever see a READ_12 but support it anyway...
  1312. block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
  1313. ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
  1314. blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
  1315. ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
  1316. US_DEBUGP("usbat_flash_transport: READ_12: read block 0x%04lx count %ld\n", block, blocks);
  1317. return usbat_flash_read_data(us, info, block, blocks);
  1318. }
  1319. if (srb->cmnd[0] == WRITE_10) {
  1320. block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
  1321. ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
  1322. blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
  1323. US_DEBUGP("usbat_flash_transport: WRITE_10: write block 0x%04lx count %ld\n", block, blocks);
  1324. return usbat_flash_write_data(us, info, block, blocks);
  1325. }
  1326. if (srb->cmnd[0] == WRITE_12) {
  1327. // I don't think we'll ever see a WRITE_12 but support it anyway...
  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[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
  1331. ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
  1332. US_DEBUGP("usbat_flash_transport: WRITE_12: write block 0x%04lx count %ld\n", block, blocks);
  1333. return usbat_flash_write_data(us, info, block, blocks);
  1334. }
  1335. if (srb->cmnd[0] == TEST_UNIT_READY) {
  1336. US_DEBUGP("usbat_flash_transport: TEST_UNIT_READY.\n");
  1337. rc = usbat_flash_check_media(us, info);
  1338. if (rc != USB_STOR_TRANSPORT_GOOD)
  1339. return rc;
  1340. return usbat_check_status(us);
  1341. }
  1342. if (srb->cmnd[0] == REQUEST_SENSE) {
  1343. US_DEBUGP("usbat_flash_transport: REQUEST_SENSE.\n");
  1344. memset(ptr, 0, 18);
  1345. ptr[0] = 0xF0;
  1346. ptr[2] = info->sense_key;
  1347. ptr[7] = 11;
  1348. ptr[12] = info->sense_asc;
  1349. ptr[13] = info->sense_ascq;
  1350. usb_stor_set_xfer_buf(ptr, 18, srb);
  1351. return USB_STOR_TRANSPORT_GOOD;
  1352. }
  1353. if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
  1354. // sure. whatever. not like we can stop the user from popping
  1355. // the media out of the device (no locking doors, etc)
  1356. return USB_STOR_TRANSPORT_GOOD;
  1357. }
  1358. US_DEBUGP("usbat_flash_transport: Gah! Unknown command: %d (0x%x)\n",
  1359. srb->cmnd[0], srb->cmnd[0]);
  1360. info->sense_key = 0x05;
  1361. info->sense_asc = 0x20;
  1362. info->sense_ascq = 0x00;
  1363. return USB_STOR_TRANSPORT_FAILED;
  1364. }
  1365. /*
  1366. * Default transport function. Attempts to detect which transport function
  1367. * should be called, makes it the new default, and calls it.
  1368. *
  1369. * This function should never be called. Our usbat_init() function detects the
  1370. * device type and changes the us->transport ptr to the transport function
  1371. * relevant to the device.
  1372. * However, we'll support this impossible(?) case anyway.
  1373. */
  1374. int usbat_transport(struct scsi_cmnd *srb, struct us_data *us)
  1375. {
  1376. struct usbat_info *info = (struct usbat_info*) (us->extra);
  1377. if (usbat_set_transport(us, info))
  1378. return USB_STOR_TRANSPORT_ERROR;
  1379. return us->transport(srb, us);
  1380. }