UsbBooter.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095
  1. #include <string.h>
  2. #include <ctype.h>
  3. #include <stdlib.h>
  4. #include "amt630h.h"
  5. #include "typedef.h"
  6. #include "uart.h"
  7. #include "timer.h"
  8. #include "list.h"
  9. #include "scsi.h"
  10. #include "usb.h"
  11. #include "dwc2.h"
  12. #include "fs/ff.h"
  13. #include "fs/diskio.h"
  14. #define USB_BUFSIZ 512
  15. #define HUB_SHORT_RESET_TIME 20
  16. #define HUB_LONG_RESET_TIME 200
  17. #define PORT_OVERCURRENT_MAX_SCAN_COUNT 3
  18. #ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
  19. #define CONFIG_USB_MAX_CONTROLLER_COUNT 1
  20. #endif
  21. /* Part types */
  22. #define PART_TYPE_UNKNOWN 0x00
  23. #define PART_TYPE_MAC 0x01
  24. #define PART_TYPE_DOS 0x02
  25. #define PART_TYPE_ISO 0x03
  26. #define PART_TYPE_AMIGA 0x04
  27. #define PART_TYPE_EFI 0x05
  28. /* device types */
  29. #define DEV_TYPE_UNKNOWN 0xff /* not connected */
  30. #define DEV_TYPE_HARDDISK 0x00 /* harddisk */
  31. #define DEV_TYPE_TAPE 0x01 /* Tape */
  32. #define DEV_TYPE_CDROM 0x05 /* CD-ROM */
  33. #define DEV_TYPE_OPDISK 0x07 /* optical disk */
  34. #define PART_NAME_LEN 32
  35. #define PART_TYPE_LEN 32
  36. #define MAX_SEARCH_PARTITIONS 64
  37. typedef unsigned long lbaint_t;
  38. typedef struct {
  39. u8 b[16];
  40. } efi_guid_t;
  41. /* Interface types: */
  42. enum if_type {
  43. IF_TYPE_UNKNOWN = 0,
  44. IF_TYPE_IDE,
  45. IF_TYPE_SCSI,
  46. IF_TYPE_ATAPI,
  47. IF_TYPE_USB,
  48. IF_TYPE_DOC,
  49. IF_TYPE_MMC,
  50. IF_TYPE_SD,
  51. IF_TYPE_SATA,
  52. IF_TYPE_HOST,
  53. IF_TYPE_NVME,
  54. IF_TYPE_EFI,
  55. IF_TYPE_COUNT, /* Number of interface types */
  56. };
  57. #define BLK_VEN_SIZE 40
  58. #define BLK_PRD_SIZE 20
  59. #define BLK_REV_SIZE 8
  60. /*
  61. * Identifies the partition table type (ie. MBR vs GPT GUID) signature
  62. */
  63. enum sig_type {
  64. SIG_TYPE_NONE,
  65. SIG_TYPE_MBR,
  66. SIG_TYPE_GUID,
  67. SIG_TYPE_COUNT /* Number of signature types */
  68. };
  69. /*
  70. * With driver model (CONFIG_BLK) this is uclass platform data, accessible
  71. * with dev_get_uclass_platdata(dev)
  72. */
  73. struct blk_desc {
  74. /*
  75. * TODO: With driver model we should be able to use the parent
  76. * device's uclass instead.
  77. */
  78. enum if_type if_type; /* type of the interface */
  79. int devnum; /* device number */
  80. unsigned char part_type; /* partition type */
  81. unsigned char target; /* target SCSI ID */
  82. unsigned char lun; /* target LUN */
  83. unsigned char hwpart; /* HW partition, e.g. for eMMC */
  84. unsigned char type; /* device type */
  85. unsigned char removable; /* removable device */
  86. lbaint_t lba; /* number of blocks */
  87. unsigned long blksz; /* block size */
  88. int log2blksz; /* for convenience: log2(blksz) */
  89. char vendor[BLK_VEN_SIZE + 1]; /* device vendor string */
  90. char product[BLK_PRD_SIZE + 1]; /* device product number */
  91. char revision[BLK_REV_SIZE + 1]; /* firmware revision */
  92. enum sig_type sig_type; /* Partition table signature type */
  93. union {
  94. u32 mbr_sig; /* MBR integer signature */
  95. efi_guid_t guid_sig; /* GPT GUID Signature */
  96. };
  97. unsigned long (*block_read)(struct blk_desc *block_dev,
  98. lbaint_t start,
  99. lbaint_t blkcnt,
  100. void *buffer);
  101. unsigned long (*block_write)(struct blk_desc *block_dev,
  102. lbaint_t start,
  103. lbaint_t blkcnt,
  104. const void *buffer);
  105. unsigned long (*block_erase)(struct blk_desc *block_dev,
  106. lbaint_t start,
  107. lbaint_t blkcnt);
  108. void *priv; /* driver private struct pointer */
  109. };
  110. static int asynch_allowed;
  111. char usb_started; /* flag for the started/stopped USB status */
  112. static struct usb_device usb_dev[USB_MAX_DEVICE];
  113. static int dev_index;
  114. struct usb_device_scan {
  115. struct list_head list;
  116. struct usb_device *dev; /* USB hub device to scan */
  117. struct usb_hub_device *hub; /* USB hub struct */
  118. int port; /* USB port to scan */
  119. };
  120. static LIST_HEAD(usb_scan_list);
  121. static struct usb_hub_device hub_dev[USB_MAX_HUB];
  122. static int usb_hub_index;
  123. static int usb_stor_curr_dev = -1; /* current device */
  124. /* direction table -- this indicates the direction of the data
  125. * transfer for each command code -- a 1 indicates input
  126. */
  127. static const unsigned char us_direction[256/8] = {
  128. 0x28, 0x81, 0x14, 0x14, 0x20, 0x01, 0x90, 0x77,
  129. 0x0C, 0x20, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
  130. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,
  131. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  132. };
  133. #define US_DIRECTION(x) ((us_direction[x>>3] >> (x & 7)) & 1)
  134. #pragma pack(ARCH_DMA_MINALIGN)
  135. static struct scsi_cmd usb_ccb;
  136. static u32 CBWTag;
  137. static int usb_max_devs; /* number of highest available usb device */
  138. static struct blk_desc usb_dev_desc[USB_MAX_STOR_DEV];
  139. struct us_data;
  140. typedef int (*trans_cmnd)(struct scsi_cmd *cb, struct us_data *data);
  141. typedef int (*trans_reset)(struct us_data *data);
  142. #define USB_READY (1 << 0)
  143. struct us_data {
  144. struct usb_device *pusb_dev; /* this usb_device */
  145. unsigned int flags; /* from filter initially */
  146. unsigned char ifnum; /* interface number */
  147. unsigned char ep_in; /* in endpoint */
  148. unsigned char ep_out; /* out ....... */
  149. unsigned char ep_int; /* interrupt . */
  150. unsigned char subclass; /* as in overview */
  151. unsigned char protocol; /* .............. */
  152. unsigned char attention_done; /* force attn on first cmd */
  153. unsigned short ip_data; /* interrupt data */
  154. int action; /* what to do */
  155. int ip_wanted; /* needed */
  156. int *irq_handle; /* for USB int requests */
  157. unsigned int irqpipe; /* pipe for release_irq */
  158. unsigned char irqmaxp; /* max packed for irq Pipe */
  159. unsigned char irqinterval; /* Intervall for IRQ Pipe */
  160. struct scsi_cmd *srb; /* current srb */
  161. trans_reset transport_reset; /* reset routine */
  162. trans_cmnd transport; /* transport routine */
  163. unsigned short max_xfer_blk; /* maximum transfer blocks */
  164. };
  165. static struct us_data usb_stor[USB_MAX_STOR_DEV];
  166. #define USB_STOR_TRANSPORT_GOOD 0
  167. #define USB_STOR_TRANSPORT_FAILED -1
  168. #define USB_STOR_TRANSPORT_ERROR -2
  169. void usb_hub_reset(void)
  170. {
  171. usb_hub_index = 0;
  172. /* Zero out global hub_dev in case its re-used again */
  173. memset(hub_dev, 0, sizeof(hub_dev));
  174. }
  175. static struct usb_hub_device *usb_hub_allocate(void)
  176. {
  177. if (usb_hub_index < USB_MAX_HUB)
  178. return &hub_dev[usb_hub_index++];
  179. return NULL;
  180. }
  181. /*-------------------------------------------------------------------
  182. * Max Packet stuff
  183. */
  184. /*
  185. * returns the max packet size, depending on the pipe direction and
  186. * the configurations values
  187. */
  188. int usb_maxpacket(struct usb_device *dev, unsigned long pipe)
  189. {
  190. /* direction is out -> use emaxpacket out */
  191. if ((pipe & USB_DIR_IN) == 0)
  192. return dev->epmaxpacketout[((pipe>>15) & 0xf)];
  193. else
  194. return dev->epmaxpacketin[((pipe>>15) & 0xf)];
  195. }
  196. /*
  197. * The routine usb_set_maxpacket_ep() is extracted from the loop of routine
  198. * usb_set_maxpacket(), because the optimizer of GCC 4.x chokes on this routine
  199. * when it is inlined in 1 single routine. What happens is that the register r3
  200. * is used as loop-count 'i', but gets overwritten later on.
  201. * This is clearly a compiler bug, but it is easier to workaround it here than
  202. * to update the compiler (Occurs with at least several GCC 4.{1,2},x
  203. * CodeSourcery compilers like e.g. 2007q3, 2008q1, 2008q3 lite editions on ARM)
  204. *
  205. * NOTE: Similar behaviour was observed with GCC4.6 on ARMv5.
  206. */
  207. static void usb_set_maxpacket_ep(struct usb_device *dev, int if_idx, int ep_idx)
  208. {
  209. int b;
  210. struct usb_endpoint_descriptor *ep;
  211. u16 ep_wMaxPacketSize = 0;
  212. unsigned char *tmp;
  213. (void)ep_wMaxPacketSize;
  214. ep = &dev->config.if_desc[if_idx].ep_desc[ep_idx];
  215. b = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
  216. tmp = (unsigned char*)&ep->wMaxPacketSize;
  217. ep_wMaxPacketSize = (tmp[1] << 8) | tmp[0];//get_unaligned(&ep->wMaxPacketSize);
  218. if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
  219. USB_ENDPOINT_XFER_CONTROL) {
  220. /* Control => bidirectional */
  221. dev->epmaxpacketout[b] = ep_wMaxPacketSize;
  222. dev->epmaxpacketin[b] = ep_wMaxPacketSize;
  223. /* debug("##Control EP epmaxpacketout/in[%d] = %d\n",
  224. b, dev->epmaxpacketin[b]); */
  225. } else {
  226. if ((ep->bEndpointAddress & 0x80) == 0) {
  227. /* OUT Endpoint */
  228. if (ep_wMaxPacketSize > dev->epmaxpacketout[b]) {
  229. dev->epmaxpacketout[b] = ep_wMaxPacketSize;
  230. /* debug("##EP epmaxpacketout[%d] = %d\n",
  231. b, dev->epmaxpacketout[b]); */
  232. }
  233. } else {
  234. /* IN Endpoint */
  235. if (ep_wMaxPacketSize > dev->epmaxpacketin[b]) {
  236. dev->epmaxpacketin[b] = ep_wMaxPacketSize;
  237. /* debug("##EP epmaxpacketin[%d] = %d\n",
  238. b, dev->epmaxpacketin[b]); */
  239. }
  240. } /* if out */
  241. } /* if control */
  242. }
  243. /*
  244. * set the max packed value of all endpoints in the given configuration
  245. */
  246. static int usb_set_maxpacket(struct usb_device *dev)
  247. {
  248. int i, ii;
  249. for (i = 0; i < dev->config.desc.bNumInterfaces; i++)
  250. for (ii = 0; ii < dev->config.if_desc[i].desc.bNumEndpoints; ii++)
  251. usb_set_maxpacket_ep(dev, i, ii);
  252. return 0;
  253. }
  254. /*-------------------------------------------------------------------
  255. * Message wrappers.
  256. *
  257. */
  258. /*
  259. * submits an Interrupt Message
  260. */
  261. int usb_submit_int_msg(struct usb_device *dev, unsigned long pipe,
  262. void *buffer, int transfer_len, int interval)
  263. {
  264. return submit_int_msg(dev, pipe, buffer, transfer_len, interval);
  265. }
  266. /*
  267. * submits a control message and waits for comletion (at least timeout * 1ms)
  268. * If timeout is 0, we don't wait for completion (used as example to set and
  269. * clear keyboards LEDs). For data transfers, (storage transfers) we don't
  270. * allow control messages with 0 timeout, by previousely resetting the flag
  271. * asynch_allowed (usb_disable_asynch(1)).
  272. * returns the transferred length if OK or -1 if error. The transferred length
  273. * and the current status are stored in the dev->act_len and dev->status.
  274. */
  275. int usb_control_msg(struct usb_device *dev, unsigned int pipe,
  276. unsigned char request, unsigned char requesttype,
  277. unsigned short value, unsigned short index,
  278. void *data, unsigned short size, int timeout)
  279. {
  280. ALLOC_CACHE_ALIGN_BUFFER(struct devrequest, setup_packet, 1);
  281. int err;
  282. if ((timeout == 0) && (!asynch_allowed)) {
  283. /* request for a asynch control pipe is not allowed */
  284. return -EINVAL;
  285. }
  286. /* set setup command */
  287. setup_packet->requesttype = requesttype;
  288. setup_packet->request = request;
  289. setup_packet->value = value;
  290. setup_packet->index = index;
  291. setup_packet->length = size;
  292. /* debug("usb_control_msg: request: 0x%X, requesttype: 0x%X, " \
  293. "value 0x%X index 0x%X length 0x%X\n",
  294. request, requesttype, value, index, size); */
  295. dev->status = USB_ST_NOT_PROC; /*not yet processed */
  296. err = submit_control_msg(dev, pipe, data, size, setup_packet);
  297. if (err < 0)
  298. return err;
  299. if (timeout == 0)
  300. return (int)size;
  301. /*
  302. * Wait for status to update until timeout expires, USB driver
  303. * interrupt handler may set the status when the USB operation has
  304. * been completed.
  305. */
  306. while (timeout--) {
  307. if (!(dev->status & USB_ST_NOT_PROC))
  308. break;
  309. mdelay(1);
  310. }
  311. if (dev->status)
  312. return -1;
  313. return dev->act_len;
  314. }
  315. /*-------------------------------------------------------------------
  316. * submits bulk message, and waits for completion. returns 0 if Ok or
  317. * negative if Error.
  318. * synchronous behavior
  319. */
  320. int usb_bulk_msg(struct usb_device *dev, unsigned int pipe,
  321. void *data, int len, int *actual_length, int timeout)
  322. {
  323. if (len < 0)
  324. return -EINVAL;
  325. dev->status = USB_ST_NOT_PROC; /*not yet processed */
  326. if (submit_bulk_msg(dev, pipe, data, len) < 0)
  327. return -EIO;
  328. while (timeout--) {
  329. if (!(dev->status & USB_ST_NOT_PROC))
  330. break;
  331. mdelay(1);
  332. }
  333. *actual_length = dev->act_len;
  334. if (dev->status == 0)
  335. return 0;
  336. else
  337. return -EIO;
  338. }
  339. /*******************************************************************************
  340. * Parse the config, located in buffer, and fills the dev->config structure.
  341. * Note that all little/big endian swapping are done automatically.
  342. * (wTotalLength has already been swapped and sanitized when it was read.)
  343. */
  344. static int usb_parse_config(struct usb_device *dev,
  345. unsigned char *buffer, int cfgno)
  346. {
  347. struct usb_descriptor_header *head;
  348. int index, ifno, epno, curr_if_num;
  349. u16 ep_wMaxPacketSize = 0;
  350. unsigned char *tmp;
  351. struct usb_interface *if_desc = NULL;
  352. (void)ep_wMaxPacketSize;
  353. ifno = -1;
  354. epno = -1;
  355. curr_if_num = -1;
  356. dev->configno = cfgno;
  357. head = (struct usb_descriptor_header *) &buffer[0];
  358. if (head->bDescriptorType != USB_DT_CONFIG) {
  359. /* printf(" ERROR: NOT USB_CONFIG_DESC %x\n",
  360. head->bDescriptorType); */
  361. return -EINVAL;
  362. }
  363. if (head->bLength != USB_DT_CONFIG_SIZE) {
  364. //printf("ERROR: Invalid USB CFG length (%d)\n", head->bLength);
  365. return -EINVAL;
  366. }
  367. memcpy(&dev->config, head, USB_DT_CONFIG_SIZE);
  368. dev->config.no_of_if = 0;
  369. index = dev->config.desc.bLength;
  370. /* Ok the first entry must be a configuration entry,
  371. * now process the others */
  372. head = (struct usb_descriptor_header *) &buffer[index];
  373. while (index + 1 < dev->config.desc.wTotalLength && head->bLength) {
  374. switch (head->bDescriptorType) {
  375. case USB_DT_INTERFACE:
  376. if (head->bLength != USB_DT_INTERFACE_SIZE) {
  377. /* printf("ERROR: Invalid USB IF length (%d)\n",
  378. head->bLength); */
  379. break;
  380. }
  381. if (index + USB_DT_INTERFACE_SIZE >
  382. dev->config.desc.wTotalLength) {
  383. //puts("USB IF descriptor overflowed buffer!\n");
  384. break;
  385. }
  386. if (((struct usb_interface_descriptor *) \
  387. head)->bInterfaceNumber != curr_if_num) {
  388. /* this is a new interface, copy new desc */
  389. ifno = dev->config.no_of_if;
  390. if (ifno >= USB_MAXINTERFACES) {
  391. //puts("Too many USB interfaces!\n");
  392. /* try to go on with what we have */
  393. return -EINVAL;
  394. }
  395. if_desc = &dev->config.if_desc[ifno];
  396. dev->config.no_of_if++;
  397. memcpy(if_desc, head,
  398. USB_DT_INTERFACE_SIZE);
  399. if_desc->no_of_ep = 0;
  400. if_desc->num_altsetting = 1;
  401. curr_if_num =
  402. if_desc->desc.bInterfaceNumber;
  403. } else {
  404. /* found alternate setting for the interface */
  405. if (ifno >= 0) {
  406. if_desc = &dev->config.if_desc[ifno];
  407. if_desc->num_altsetting++;
  408. }
  409. }
  410. break;
  411. case USB_DT_ENDPOINT:
  412. if (head->bLength != USB_DT_ENDPOINT_SIZE &&
  413. head->bLength != USB_DT_ENDPOINT_AUDIO_SIZE) {
  414. /* printf("ERROR: Invalid USB EP length (%d)\n",
  415. head->bLength); */
  416. break;
  417. }
  418. if (index + head->bLength >
  419. dev->config.desc.wTotalLength) {
  420. //puts("USB EP descriptor overflowed buffer!\n");
  421. break;
  422. }
  423. if (ifno < 0) {
  424. //puts("Endpoint descriptor out of order!\n");
  425. break;
  426. }
  427. epno = dev->config.if_desc[ifno].no_of_ep;
  428. if_desc = &dev->config.if_desc[ifno];
  429. if (epno >= USB_MAXENDPOINTS) {
  430. /* printf("Interface %d has too many endpoints!\n",
  431. if_desc->desc.bInterfaceNumber); */
  432. return -EINVAL;
  433. }
  434. /* found an endpoint */
  435. if_desc->no_of_ep++;
  436. memcpy(&if_desc->ep_desc[epno], head,
  437. USB_DT_ENDPOINT_SIZE);
  438. /* ep_wMaxPacketSize = get_unaligned(&dev->config.\
  439. if_desc[ifno].\
  440. ep_desc[epno].\
  441. wMaxPacketSize); */
  442. tmp = (unsigned char*)&dev->config.if_desc[ifno].ep_desc[epno].wMaxPacketSize;
  443. ep_wMaxPacketSize = (tmp[1] << 8) | tmp[0];
  444. /* put_unaligned(le16_to_cpu(ep_wMaxPacketSize),
  445. &dev->config.\
  446. if_desc[ifno].\
  447. ep_desc[epno].\
  448. wMaxPacketSize); */
  449. //debug("if %d, ep %d\n", ifno, epno);
  450. break;
  451. case USB_DT_SS_ENDPOINT_COMP:
  452. if (head->bLength != USB_DT_SS_EP_COMP_SIZE) {
  453. /* printf("ERROR: Invalid USB EPC length (%d)\n",
  454. head->bLength); */
  455. break;
  456. }
  457. if (index + USB_DT_SS_EP_COMP_SIZE >
  458. dev->config.desc.wTotalLength) {
  459. //puts("USB EPC descriptor overflowed buffer!\n");
  460. break;
  461. }
  462. if (ifno < 0 || epno < 0) {
  463. //puts("EPC descriptor out of order!\n");
  464. break;
  465. }
  466. if_desc = &dev->config.if_desc[ifno];
  467. memcpy(&if_desc->ss_ep_comp_desc[epno], head,
  468. USB_DT_SS_EP_COMP_SIZE);
  469. break;
  470. default:
  471. if (head->bLength == 0)
  472. return -EINVAL;
  473. /* debug("unknown Description Type : %x\n",
  474. head->bDescriptorType); */
  475. #ifdef DEBUG
  476. {
  477. unsigned char *ch = (unsigned char *)head;
  478. int i;
  479. for (i = 0; i < head->bLength; i++)
  480. debug("%02X ", *ch++);
  481. debug("\n\n\n");
  482. }
  483. #endif
  484. break;
  485. }
  486. index += head->bLength;
  487. head = (struct usb_descriptor_header *)&buffer[index];
  488. }
  489. return 0;
  490. }
  491. /********************************************************************
  492. * get string index in buffer
  493. */
  494. static int usb_get_string(struct usb_device *dev, unsigned short langid,
  495. unsigned char index, void *buf, int size)
  496. {
  497. int i;
  498. int result;
  499. for (i = 0; i < 3; ++i) {
  500. /* some devices are flaky */
  501. result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  502. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
  503. (USB_DT_STRING << 8) + index, langid, buf, size,
  504. USB_CNTL_TIMEOUT);
  505. if (result > 0)
  506. break;
  507. }
  508. return result;
  509. }
  510. static void usb_try_string_workarounds(unsigned char *buf, int *length)
  511. {
  512. int newlength, oldlength = *length;
  513. for (newlength = 2; newlength + 1 < oldlength; newlength += 2)
  514. if (!isprint(buf[newlength]) || buf[newlength + 1])
  515. break;
  516. if (newlength > 2) {
  517. buf[0] = newlength;
  518. *length = newlength;
  519. }
  520. }
  521. static int usb_string_sub(struct usb_device *dev, unsigned int langid,
  522. unsigned int index, unsigned char *buf)
  523. {
  524. int rc;
  525. /* Try to read the string descriptor by asking for the maximum
  526. * possible number of bytes */
  527. rc = usb_get_string(dev, langid, index, buf, 255);
  528. /* If that failed try to read the descriptor length, then
  529. * ask for just that many bytes */
  530. if (rc < 2) {
  531. rc = usb_get_string(dev, langid, index, buf, 2);
  532. if (rc == 2)
  533. rc = usb_get_string(dev, langid, index, buf, buf[0]);
  534. }
  535. if (rc >= 2) {
  536. if (!buf[0] && !buf[1])
  537. usb_try_string_workarounds(buf, &rc);
  538. /* There might be extra junk at the end of the descriptor */
  539. if (buf[0] < rc)
  540. rc = buf[0];
  541. rc = rc - (rc & 1); /* force a multiple of two */
  542. }
  543. if (rc < 2)
  544. rc = -EINVAL;
  545. return rc;
  546. }
  547. /********************************************************************
  548. * usb_string:
  549. * Get string index and translate it to ascii.
  550. * returns string length (> 0) or error (< 0)
  551. */
  552. int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
  553. {
  554. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, mybuf, USB_BUFSIZ);
  555. unsigned char *tbuf;
  556. int err;
  557. unsigned int u, idx;
  558. if (size <= 0 || !buf || !index)
  559. return -EINVAL;
  560. buf[0] = 0;
  561. tbuf = &mybuf[0];
  562. /* get langid for strings if it's not yet known */
  563. if (!dev->have_langid) {
  564. err = usb_string_sub(dev, 0, 0, tbuf);
  565. if (err < 0) {
  566. /* debug("error getting string descriptor 0 " \
  567. "(error=%lx)\n", dev->status); */
  568. return -EIO;
  569. } else if (tbuf[0] < 4) {
  570. //debug("string descriptor 0 too short\n");
  571. return -EIO;
  572. } else {
  573. dev->have_langid = -1;
  574. dev->string_langid = tbuf[2] | (tbuf[3] << 8);
  575. /* always use the first langid listed */
  576. /* debug("USB device number %d default " \
  577. "language ID 0x%x\n",
  578. dev->devnum, dev->string_langid); */
  579. }
  580. }
  581. err = usb_string_sub(dev, dev->string_langid, index, tbuf);
  582. if (err < 0)
  583. return err;
  584. size--; /* leave room for trailing NULL char in output buffer */
  585. for (idx = 0, u = 2; u < err; u += 2) {
  586. if (idx >= size)
  587. break;
  588. if (tbuf[u+1]) /* high byte */
  589. buf[idx++] = '?'; /* non-ASCII character */
  590. else
  591. buf[idx++] = tbuf[u];
  592. }
  593. buf[idx] = 0;
  594. err = idx;
  595. return err;
  596. }
  597. struct usb_device *usb_get_dev_index(int index)
  598. {
  599. if (usb_dev[index].devnum == -1)
  600. return NULL;
  601. else
  602. return &usb_dev[index];
  603. }
  604. void usb_free_device(void *controller)
  605. {
  606. dev_index--;
  607. //debug("Freeing device node: %d\n", dev_index);
  608. memset(&usb_dev[dev_index], 0, sizeof(struct usb_device));
  609. usb_dev[dev_index].devnum = -1;
  610. }
  611. int usb_alloc_device(struct usb_device *udev)
  612. {
  613. return 0;
  614. }
  615. /***********************************************************************
  616. * Clears an endpoint
  617. * endp: endpoint number in bits 0-3;
  618. * direction flag in bit 7 (1 = IN, 0 = OUT)
  619. */
  620. int usb_clear_halt(struct usb_device *dev, int pipe)
  621. {
  622. int result;
  623. int endp = usb_pipeendpoint(pipe)|(usb_pipein(pipe)<<7);
  624. result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  625. USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT, 0,
  626. endp, NULL, 0, USB_CNTL_TIMEOUT * 3);
  627. /* don't clear if failed */
  628. if (result < 0)
  629. return result;
  630. /*
  631. * NOTE: we do not get status and verify reset was successful
  632. * as some devices are reported to lock up upon this check..
  633. */
  634. usb_endpoint_running(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
  635. /* toggle is reset on clear */
  636. usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), 0);
  637. return 0;
  638. }
  639. /**********************************************************************
  640. * get_descriptor type
  641. */
  642. static int usb_get_descriptor(struct usb_device *dev, unsigned char type,
  643. unsigned char index, void *buf, int size)
  644. {
  645. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  646. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
  647. (type << 8) + index, 0, buf, size,
  648. USB_CNTL_TIMEOUT);
  649. }
  650. /**********************************************************************
  651. * gets len of configuration cfgno
  652. */
  653. int usb_get_configuration_len(struct usb_device *dev, int cfgno)
  654. {
  655. int result;
  656. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, 9);
  657. struct usb_config_descriptor *config;
  658. config = (struct usb_config_descriptor *)&buffer[0];
  659. result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno, buffer, 9);
  660. if (result < 9) {
  661. if (result < 0)
  662. ;/* printf("unable to get descriptor, error %lX\n",
  663. dev->status); */
  664. else
  665. ;/* printf("config descriptor too short " \
  666. "(expected %i, got %i)\n", 9, result); */
  667. return -EIO;
  668. }
  669. return config->wTotalLength;
  670. }
  671. /**********************************************************************
  672. * gets configuration cfgno and store it in the buffer
  673. */
  674. int usb_get_configuration_no(struct usb_device *dev, int cfgno,
  675. unsigned char *buffer, int length)
  676. {
  677. int result;
  678. struct usb_config_descriptor *config;
  679. config = (struct usb_config_descriptor *)&buffer[0];
  680. result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno, buffer, length);
  681. /* debug("get_conf_no %d Result %d, wLength %d\n", cfgno, result,
  682. le16_to_cpu(config->wTotalLength)); */
  683. config->wTotalLength = result; /* validated, with CPU byte order */
  684. return result;
  685. }
  686. static int get_descriptor_len(struct usb_device *dev, int len, int expect_len)
  687. {
  688. struct usb_device_descriptor *desc;
  689. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, tmpbuf, USB_BUFSIZ);
  690. int err;
  691. desc = (struct usb_device_descriptor *)tmpbuf;
  692. err = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, len);
  693. if (err < expect_len) {
  694. if (err < 0) {
  695. /* printf("unable to get device descriptor (error=%d)\n",
  696. err); */
  697. return err;
  698. } else {
  699. /* printf("USB device descriptor short read (expected %i, got %i)\n",
  700. expect_len, err); */
  701. return -EIO;
  702. }
  703. }
  704. memcpy(&dev->descriptor, tmpbuf, sizeof(dev->descriptor));
  705. return 0;
  706. }
  707. /********************************************************************
  708. * set address of a device to the value in dev->devnum.
  709. * This can only be done by addressing the device via the default address (0)
  710. */
  711. static int usb_set_address(struct usb_device *dev)
  712. {
  713. //debug("set address %d\n", dev->devnum);
  714. return usb_control_msg(dev, usb_snddefctrl(dev), USB_REQ_SET_ADDRESS,
  715. 0, (dev->devnum), 0, NULL, 0, USB_CNTL_TIMEOUT);
  716. }
  717. /********************************************************************
  718. * set interface number to interface
  719. */
  720. int usb_set_interface(struct usb_device *dev, int interface, int alternate)
  721. {
  722. struct usb_interface *if_face = NULL;
  723. int ret, i;
  724. for (i = 0; i < dev->config.desc.bNumInterfaces; i++) {
  725. if (dev->config.if_desc[i].desc.bInterfaceNumber == interface) {
  726. if_face = &dev->config.if_desc[i];
  727. break;
  728. }
  729. }
  730. if (!if_face) {
  731. //printf("selecting invalid interface %d", interface);
  732. return -EINVAL;
  733. }
  734. /*
  735. * We should return now for devices with only one alternate setting.
  736. * According to 9.4.10 of the Universal Serial Bus Specification
  737. * Revision 2.0 such devices can return with a STALL. This results in
  738. * some USB sticks timeouting during initialization and then being
  739. * unusable in U-Boot.
  740. */
  741. if (if_face->num_altsetting == 1)
  742. return 0;
  743. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  744. USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE,
  745. alternate, interface, NULL, 0,
  746. USB_CNTL_TIMEOUT * 5);
  747. if (ret < 0)
  748. return ret;
  749. return 0;
  750. }
  751. /********************************************************************
  752. * set configuration number to configuration
  753. */
  754. static int usb_set_configuration(struct usb_device *dev, int configuration)
  755. {
  756. int res;
  757. //debug("set configuration %d\n", configuration);
  758. /* set setup command */
  759. res = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  760. USB_REQ_SET_CONFIGURATION, 0,
  761. configuration, 0,
  762. NULL, 0, USB_CNTL_TIMEOUT);
  763. if (res == 0) {
  764. dev->toggle[0] = 0;
  765. dev->toggle[1] = 0;
  766. return 0;
  767. } else
  768. return -EIO;
  769. }
  770. static int usb_setup_descriptor(struct usb_device *dev, bool do_read)
  771. {
  772. /*
  773. * This is a Windows scheme of initialization sequence, with double
  774. * reset of the device (Linux uses the same sequence)
  775. * Some equipment is said to work only with such init sequence; this
  776. * patch is based on the work by Alan Stern:
  777. * http://sourceforge.net/mailarchive/forum.php?
  778. * thread_id=5729457&forum_id=5398
  779. */
  780. /*
  781. * send 64-byte GET-DEVICE-DESCRIPTOR request. Since the descriptor is
  782. * only 18 bytes long, this will terminate with a short packet. But if
  783. * the maxpacket size is 8 or 16 the device may be waiting to transmit
  784. * some more, or keeps on retransmitting the 8 byte header.
  785. */
  786. if (dev->speed == USB_SPEED_LOW) {
  787. dev->descriptor.bMaxPacketSize0 = 8;
  788. dev->maxpacketsize = PACKET_SIZE_8;
  789. } else {
  790. dev->descriptor.bMaxPacketSize0 = 64;
  791. dev->maxpacketsize = PACKET_SIZE_64;
  792. }
  793. dev->epmaxpacketin[0] = dev->descriptor.bMaxPacketSize0;
  794. dev->epmaxpacketout[0] = dev->descriptor.bMaxPacketSize0;
  795. if (do_read && dev->speed == USB_SPEED_FULL) {
  796. int err;
  797. /*
  798. * Validate we've received only at least 8 bytes, not that
  799. * we've received the entire descriptor. The reasoning is:
  800. * - The code only uses fields in the first 8 bytes, so
  801. * that's all we need to have fetched at this stage.
  802. * - The smallest maxpacket size is 8 bytes. Before we know
  803. * the actual maxpacket the device uses, the USB controller
  804. * may only accept a single packet. Consequently we are only
  805. * guaranteed to receive 1 packet (at least 8 bytes) even in
  806. * a non-error case.
  807. *
  808. * At least the DWC2 controller needs to be programmed with
  809. * the number of packets in addition to the number of bytes.
  810. * A request for 64 bytes of data with the maxpacket guessed
  811. * as 64 (above) yields a request for 1 packet.
  812. */
  813. err = get_descriptor_len(dev, 64, 8);
  814. if (err)
  815. return err;
  816. }
  817. dev->epmaxpacketin[0] = dev->descriptor.bMaxPacketSize0;
  818. dev->epmaxpacketout[0] = dev->descriptor.bMaxPacketSize0;
  819. switch (dev->descriptor.bMaxPacketSize0) {
  820. case 8:
  821. dev->maxpacketsize = PACKET_SIZE_8;
  822. break;
  823. case 16:
  824. dev->maxpacketsize = PACKET_SIZE_16;
  825. break;
  826. case 32:
  827. dev->maxpacketsize = PACKET_SIZE_32;
  828. break;
  829. case 64:
  830. dev->maxpacketsize = PACKET_SIZE_64;
  831. break;
  832. default:
  833. //printf("%s: invalid max packet size\n", __func__);
  834. return -EIO;
  835. }
  836. return 0;
  837. }
  838. int usb_alloc_new_device(void *controller, struct usb_device **devp)
  839. {
  840. int i;
  841. if (dev_index == USB_MAX_DEVICE) {
  842. SendUartString("ERROR, too many USB Devices\n");
  843. return -ENOSPC;
  844. }
  845. /* default Address is 0, real addresses start with 1 */
  846. usb_dev[dev_index].devnum = dev_index + 1;
  847. usb_dev[dev_index].maxchild = 0;
  848. for (i = 0; i < USB_MAXCHILDREN; i++)
  849. usb_dev[dev_index].children[i] = NULL;
  850. usb_dev[dev_index].parent = NULL;
  851. usb_dev[dev_index].controller = controller;
  852. dev_index++;
  853. *devp = &usb_dev[dev_index - 1];
  854. return 0;
  855. }
  856. static int usb_prepare_device(struct usb_device *dev, int addr, bool do_read,
  857. struct usb_device *parent)
  858. {
  859. int err;
  860. /*
  861. * Allocate usb 3.0 device context.
  862. * USB 3.0 (xHCI) protocol tries to allocate device slot
  863. * and related data structures first. This call does that.
  864. * Refer to sec 4.3.2 in xHCI spec rev1.0
  865. */
  866. err = usb_alloc_device(dev);
  867. if (err) {
  868. //printf("Cannot allocate device context to get SLOT_ID\n");
  869. return err;
  870. }
  871. err = usb_setup_descriptor(dev, do_read);
  872. if (err)
  873. return err;
  874. /* err = usb_hub_port_reset(dev, parent);
  875. if (err)
  876. return err; */
  877. dev->devnum = addr;
  878. err = usb_set_address(dev); /* set address */
  879. if (err < 0) {
  880. /* printf("\n USB device not accepting new address " \
  881. "(error=%lX)\n", dev->status); */
  882. return err;
  883. }
  884. mdelay(10); /* Let the SET_ADDRESS settle */
  885. /*
  886. * If we haven't read device descriptor before, read it here
  887. * after device is assigned an address. This is only applicable
  888. * to xHCI so far.
  889. */
  890. if (!do_read) {
  891. err = usb_setup_descriptor(dev, true);
  892. if (err)
  893. return err;
  894. }
  895. return 0;
  896. }
  897. int usb_select_config(struct usb_device *dev)
  898. {
  899. unsigned char *rawtmpbuf = NULL, *tmpbuf = NULL;
  900. int err;
  901. err = get_descriptor_len(dev, USB_DT_DEVICE_SIZE, USB_DT_DEVICE_SIZE);
  902. if (err)
  903. return err;
  904. /*
  905. * Kingston DT Ultimate 32GB USB 3.0 seems to be extremely sensitive
  906. * about this first Get Descriptor request. If there are any other
  907. * requests in the first microframe, the stick crashes. Wait about
  908. * one microframe duration here (1mS for USB 1.x , 125uS for USB 2.0).
  909. */
  910. mdelay(1);
  911. /* only support for one config for now */
  912. err = usb_get_configuration_len(dev, 0);
  913. if (err >= 0) {
  914. rawtmpbuf = (unsigned char *)malloc(err + ARCH_DMA_MINALIGN - 1);
  915. if (!rawtmpbuf)
  916. err = -ENOMEM;
  917. else {
  918. tmpbuf = (unsigned char*)ALIGN((uintptr_t)rawtmpbuf, ARCH_DMA_MINALIGN);
  919. err = usb_get_configuration_no(dev, 0, tmpbuf, err);
  920. }
  921. }
  922. if (err < 0) {
  923. /* printf("usb_new_device: Cannot read configuration, " \
  924. "skipping device %04x:%04x\n",
  925. dev->descriptor.idVendor, dev->descriptor.idProduct); */
  926. if (rawtmpbuf)
  927. free(rawtmpbuf);
  928. return err;
  929. }
  930. usb_parse_config(dev, tmpbuf, 0);
  931. free(rawtmpbuf);
  932. usb_set_maxpacket(dev);
  933. /*
  934. * we set the default configuration here
  935. * This seems premature. If the driver wants a different configuration
  936. * it will need to select itself.
  937. */
  938. err = usb_set_configuration(dev, dev->config.desc.bConfigurationValue);
  939. if (err < 0) {
  940. /* printf("failed to set default configuration " \
  941. "len %d, status %lX\n", dev->act_len, dev->status); */
  942. return err;
  943. }
  944. /*
  945. * Wait until the Set Configuration request gets processed by the
  946. * device. This is required by at least SanDisk Cruzer Pop USB 2.0
  947. * and Kingston DT Ultimate 32GB USB 3.0 on DWC2 OTG controller.
  948. */
  949. mdelay(10);
  950. /* debug("new device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
  951. dev->descriptor.iManufacturer, dev->descriptor.iProduct,
  952. dev->descriptor.iSerialNumber); */
  953. memset(dev->mf, 0, sizeof(dev->mf));
  954. memset(dev->prod, 0, sizeof(dev->prod));
  955. memset(dev->serial, 0, sizeof(dev->serial));
  956. if (dev->descriptor.iManufacturer)
  957. usb_string(dev, dev->descriptor.iManufacturer,
  958. dev->mf, sizeof(dev->mf));
  959. if (dev->descriptor.iProduct)
  960. usb_string(dev, dev->descriptor.iProduct,
  961. dev->prod, sizeof(dev->prod));
  962. if (dev->descriptor.iSerialNumber)
  963. usb_string(dev, dev->descriptor.iSerialNumber,
  964. dev->serial, sizeof(dev->serial));
  965. //debug("Manufacturer %s\n", dev->mf);
  966. //debug("Product %s\n", dev->prod);
  967. //debug("SerialNumber %s\n", dev->serial);
  968. return 0;
  969. }
  970. int usb_setup_device(struct usb_device *dev, bool do_read,
  971. struct usb_device *parent)
  972. {
  973. int addr;
  974. int ret;
  975. /* We still haven't set the Address yet */
  976. addr = dev->devnum;
  977. dev->devnum = 0;
  978. ret = usb_prepare_device(dev, addr, do_read, parent);
  979. if (ret)
  980. return ret;
  981. ret = usb_select_config(dev);
  982. return ret;
  983. }
  984. bool usb_device_has_child_on_port(struct usb_device *parent, int port)
  985. {
  986. return parent->children[port] != NULL;
  987. }
  988. static inline bool usb_hub_is_superspeed(struct usb_device *hdev)
  989. {
  990. return hdev->descriptor.bDeviceProtocol == 3;
  991. }
  992. static int usb_get_hub_descriptor(struct usb_device *dev, void *data, int size)
  993. {
  994. unsigned short dtype = USB_DT_HUB;
  995. if (usb_hub_is_superspeed(dev))
  996. dtype = USB_DT_SS_HUB;
  997. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  998. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
  999. dtype << 8, 0, data, size, USB_CNTL_TIMEOUT);
  1000. }
  1001. static struct usb_hub_device *usb_get_hub_device(struct usb_device *dev)
  1002. {
  1003. struct usb_hub_device *hub;
  1004. /* "allocate" Hub device */
  1005. hub = usb_hub_allocate();
  1006. return hub;
  1007. }
  1008. static int usb_get_hub_status(struct usb_device *dev, void *data)
  1009. {
  1010. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  1011. USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
  1012. data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
  1013. }
  1014. static int usb_clear_port_feature(struct usb_device *dev, int port, int feature)
  1015. {
  1016. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  1017. USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature,
  1018. port, NULL, 0, USB_CNTL_TIMEOUT);
  1019. }
  1020. static int usb_set_port_feature(struct usb_device *dev, int port, int feature)
  1021. {
  1022. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  1023. USB_REQ_SET_FEATURE, USB_RT_PORT, feature,
  1024. port, NULL, 0, USB_CNTL_TIMEOUT);
  1025. }
  1026. int usb_get_port_status(struct usb_device *dev, int port, void *data)
  1027. {
  1028. int ret;
  1029. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  1030. USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port,
  1031. data, sizeof(struct usb_port_status), USB_CNTL_TIMEOUT);
  1032. return ret;
  1033. }
  1034. static void usb_hub_power_on(struct usb_hub_device *hub)
  1035. {
  1036. int i;
  1037. struct usb_device *dev;
  1038. unsigned pgood_delay = hub->desc.bPwrOn2PwrGood * 2;
  1039. //const char *env;
  1040. dev = hub->pusb_dev;
  1041. //debug("enabling power on all ports\n");
  1042. for (i = 0; i < dev->maxchild; i++) {
  1043. usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
  1044. //debug("port %d returns %lX\n", i + 1, dev->status);
  1045. }
  1046. /*
  1047. * Wait for power to become stable,
  1048. * plus spec-defined max time for device to connect
  1049. * but allow this time to be increased via env variable as some
  1050. * devices break the spec and require longer warm-up times
  1051. */
  1052. /* env = env_get("usb_pgood_delay");
  1053. if (env)
  1054. pgood_delay = max(pgood_delay,
  1055. (unsigned)simple_strtol(env, NULL, 0));
  1056. debug("pgood_delay=%dms\n", pgood_delay); */
  1057. /*
  1058. * Do a minimum delay of the larger value of 100ms or pgood_delay
  1059. * so that the power can stablize before the devices are queried
  1060. */
  1061. hub->query_delay = get_timer(0) + max(100, (int)pgood_delay);
  1062. /*
  1063. * Record the power-on timeout here. The max. delay (timeout)
  1064. * will be done based on this value in the USB port loop in
  1065. * usb_hub_configure() later.
  1066. */
  1067. hub->connect_timeout = hub->query_delay + 1000;
  1068. /* debug("devnum=%d poweron: query_delay=%d connect_timeout=%d\n",
  1069. dev->devnum, max(100, (int)pgood_delay),
  1070. max(100, (int)pgood_delay) + 1000); */
  1071. }
  1072. #define MAX_TRIES 5
  1073. /**
  1074. * usb_hub_port_reset() - reset a port given its usb_device pointer
  1075. *
  1076. * Reset a hub port and see if a device is present on that port, providing
  1077. * sufficient time for it to show itself. The port status is returned.
  1078. *
  1079. * @dev: USB device to reset
  1080. * @port: Port number to reset (note ports are numbered from 0 here)
  1081. * @portstat: Returns port status
  1082. */
  1083. static int usb_hub_port_reset(struct usb_device *dev, int port,
  1084. unsigned short *portstat)
  1085. {
  1086. int err, tries;
  1087. ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
  1088. unsigned short portstatus, portchange;
  1089. int delay = HUB_SHORT_RESET_TIME; /* start with short reset delay */
  1090. //debug("%s: resetting port %d...\n", __func__, port + 1);
  1091. for (tries = 0; tries < MAX_TRIES; tries++) {
  1092. err = usb_set_port_feature(dev, port + 1, USB_PORT_FEAT_RESET);
  1093. if (err < 0)
  1094. return err;
  1095. mdelay(delay);
  1096. if (usb_get_port_status(dev, port + 1, portsts) < 0) {
  1097. /* debug("get_port_status failed status %lX\n",
  1098. dev->status); */
  1099. return -1;
  1100. }
  1101. portstatus = le16_to_cpu(portsts->wPortStatus);
  1102. portchange = le16_to_cpu(portsts->wPortChange);
  1103. (void)portchange;
  1104. /* debug("portstatus %x, change %x, %s\n", portstatus, portchange,
  1105. portspeed(portstatus));
  1106. debug("STAT_C_CONNECTION = %d STAT_CONNECTION = %d" \
  1107. " USB_PORT_STAT_ENABLE %d\n",
  1108. (portchange & USB_PORT_STAT_C_CONNECTION) ? 1 : 0,
  1109. (portstatus & USB_PORT_STAT_CONNECTION) ? 1 : 0,
  1110. (portstatus & USB_PORT_STAT_ENABLE) ? 1 : 0); */
  1111. /*
  1112. * Perhaps we should check for the following here:
  1113. * - C_CONNECTION hasn't been set.
  1114. * - CONNECTION is still set.
  1115. *
  1116. * Doing so would ensure that the device is still connected
  1117. * to the bus, and hasn't been unplugged or replaced while the
  1118. * USB bus reset was going on.
  1119. *
  1120. * However, if we do that, then (at least) a San Disk Ultra
  1121. * USB 3.0 16GB device fails to reset on (at least) an NVIDIA
  1122. * Tegra Jetson TK1 board. For some reason, the device appears
  1123. * to briefly drop off the bus when this second bus reset is
  1124. * executed, yet if we retry this loop, it'll eventually come
  1125. * back after another reset or two.
  1126. */
  1127. if (portstatus & USB_PORT_STAT_ENABLE)
  1128. break;
  1129. /* Switch to long reset delay for the next round */
  1130. delay = HUB_LONG_RESET_TIME;
  1131. }
  1132. if (tries == MAX_TRIES) {
  1133. /* debug("Cannot enable port %i after %i retries, " \
  1134. "disabling port.\n", port + 1, MAX_TRIES);
  1135. debug("Maybe the USB cable is bad?\n"); */
  1136. return -1;
  1137. }
  1138. usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_RESET);
  1139. *portstat = portstatus;
  1140. return 0;
  1141. }
  1142. int usb_new_device(struct usb_device *dev);
  1143. int usb_hub_port_connect_change(struct usb_device *dev, int port)
  1144. {
  1145. ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
  1146. unsigned short portstatus;
  1147. int ret, speed;
  1148. /* Check status */
  1149. ret = usb_get_port_status(dev, port + 1, portsts);
  1150. if (ret < 0) {
  1151. //debug("get_port_status failed\n");
  1152. return ret;
  1153. }
  1154. portstatus = portsts->wPortStatus;
  1155. /* debug("portstatus %x, change %x, %s\n",
  1156. portstatus,
  1157. le16_to_cpu(portsts->wPortChange),
  1158. portspeed(portstatus)); */
  1159. /* Clear the connection change status */
  1160. usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_CONNECTION);
  1161. /* Disconnect any existing devices under this port */
  1162. if (((!(portstatus & USB_PORT_STAT_CONNECTION)) &&
  1163. (!(portstatus & USB_PORT_STAT_ENABLE))) ||
  1164. usb_device_has_child_on_port(dev, port)) {
  1165. //debug("usb_disconnect(&hub->children[port]);\n");
  1166. /* Return now if nothing is connected */
  1167. if (!(portstatus & USB_PORT_STAT_CONNECTION))
  1168. return -ENOTCONN;
  1169. }
  1170. /* Reset the port */
  1171. ret = usb_hub_port_reset(dev, port, &portstatus);
  1172. if (ret < 0) {
  1173. if (ret != -ENXIO)
  1174. ;//printf("cannot reset port %i!?\n", port + 1);
  1175. return ret;
  1176. }
  1177. switch (portstatus & USB_PORT_STAT_SPEED_MASK) {
  1178. case USB_PORT_STAT_SUPER_SPEED:
  1179. speed = USB_SPEED_SUPER;
  1180. break;
  1181. case USB_PORT_STAT_HIGH_SPEED:
  1182. speed = USB_SPEED_HIGH;
  1183. break;
  1184. case USB_PORT_STAT_LOW_SPEED:
  1185. speed = USB_SPEED_LOW;
  1186. break;
  1187. default:
  1188. speed = USB_SPEED_FULL;
  1189. break;
  1190. }
  1191. struct usb_device *usb;
  1192. ret = usb_alloc_new_device(dev->controller, &usb);
  1193. if (ret) {
  1194. //printf("cannot create new device: ret=%d", ret);
  1195. return ret;
  1196. }
  1197. dev->children[port] = usb;
  1198. usb->speed = speed;
  1199. usb->parent = dev;
  1200. usb->portnr = port + 1;
  1201. /* Run it through the hoops (find a driver, etc) */
  1202. ret = usb_new_device(usb);
  1203. if (ret < 0) {
  1204. /* Woops, disable the port */
  1205. usb_free_device(dev->controller);
  1206. dev->children[port] = NULL;
  1207. }
  1208. if (ret < 0) {
  1209. //debug("hub: disabling port %d\n", port + 1);
  1210. usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_ENABLE);
  1211. }
  1212. return ret;
  1213. }
  1214. static int usb_scan_port(struct usb_device_scan *usb_scan)
  1215. {
  1216. ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
  1217. unsigned short portstatus;
  1218. unsigned short portchange;
  1219. struct usb_device *dev;
  1220. struct usb_hub_device *hub;
  1221. int ret = 0;
  1222. int i;
  1223. dev = usb_scan->dev;
  1224. hub = usb_scan->hub;
  1225. i = usb_scan->port;
  1226. /*
  1227. * Don't talk to the device before the query delay is expired.
  1228. * This is needed for voltages to stabalize.
  1229. */
  1230. if (get_timer(0) < hub->query_delay)
  1231. return 0;
  1232. ret = usb_get_port_status(dev, i + 1, portsts);
  1233. if (ret < 0) {
  1234. //debug("get_port_status failed\n");
  1235. if (get_timer(0) >= hub->connect_timeout) {
  1236. /* debug("devnum=%d port=%d: timeout\n",
  1237. dev->devnum, i + 1); */
  1238. /* Remove this device from scanning list */
  1239. list_del(&usb_scan->list);
  1240. free(usb_scan);
  1241. return 0;
  1242. }
  1243. return 0;
  1244. }
  1245. portstatus = portsts->wPortStatus;
  1246. portchange = portsts->wPortChange;
  1247. //debug("Port %d Status %X Change %X\n", i + 1, portstatus, portchange);
  1248. /*
  1249. * No connection change happened, wait a bit more.
  1250. *
  1251. * For some situation, the hub reports no connection change but a
  1252. * device is connected to the port (eg: CCS bit is set but CSC is not
  1253. * in the PORTSC register of a root hub), ignore such case.
  1254. */
  1255. if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
  1256. !(portstatus & USB_PORT_STAT_CONNECTION)) {
  1257. if (get_timer(0) >= hub->connect_timeout) {
  1258. /* debug("devnum=%d port=%d: timeout\n",
  1259. dev->devnum, i + 1); */
  1260. /* Remove this device from scanning list */
  1261. list_del(&usb_scan->list);
  1262. free(usb_scan);
  1263. return 0;
  1264. }
  1265. return 0;
  1266. }
  1267. if (portchange & USB_PORT_STAT_C_RESET) {
  1268. //debug("port %d reset change\n", i + 1);
  1269. usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_RESET);
  1270. }
  1271. if ((portchange & USB_SS_PORT_STAT_C_BH_RESET) &&
  1272. usb_hub_is_superspeed(dev)) {
  1273. //debug("port %d BH reset change\n", i + 1);
  1274. usb_clear_port_feature(dev, i + 1, USB_SS_PORT_FEAT_C_BH_RESET);
  1275. }
  1276. /* A new USB device is ready at this point */
  1277. //debug("devnum=%d port=%d: USB dev found\n", dev->devnum, i + 1);
  1278. usb_hub_port_connect_change(dev, i);
  1279. if (portchange & USB_PORT_STAT_C_ENABLE) {
  1280. //debug("port %d enable change, status %x\n", i + 1, portstatus);
  1281. usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_ENABLE);
  1282. /*
  1283. * The following hack causes a ghost device problem
  1284. * to Faraday EHCI
  1285. */
  1286. /*
  1287. * EM interference sometimes causes bad shielded USB
  1288. * devices to be shutdown by the hub, this hack enables
  1289. * them again. Works at least with mouse driver
  1290. */
  1291. if (!(portstatus & USB_PORT_STAT_ENABLE) &&
  1292. (portstatus & USB_PORT_STAT_CONNECTION) &&
  1293. usb_device_has_child_on_port(dev, i)) {
  1294. /* debug("already running port %i disabled by hub (EMI?), re-enabling...\n",
  1295. i + 1); */
  1296. usb_hub_port_connect_change(dev, i);
  1297. }
  1298. }
  1299. if (portstatus & USB_PORT_STAT_SUSPEND) {
  1300. //debug("port %d suspend change\n", i + 1);
  1301. usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_SUSPEND);
  1302. }
  1303. if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
  1304. //debug("port %d over-current change\n", i + 1);
  1305. usb_clear_port_feature(dev, i + 1,
  1306. USB_PORT_FEAT_C_OVER_CURRENT);
  1307. /* Only power-on this one port */
  1308. usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
  1309. hub->overcurrent_count[i]++;
  1310. /*
  1311. * If the max-scan-count is not reached, return without removing
  1312. * the device from scan-list. This will re-issue a new scan.
  1313. */
  1314. if (hub->overcurrent_count[i] <=
  1315. PORT_OVERCURRENT_MAX_SCAN_COUNT)
  1316. return 0;
  1317. /* Otherwise the device will get removed */
  1318. /* printf("Port %d over-current occurred %d times\n", i + 1,
  1319. hub->overcurrent_count[i]); */
  1320. }
  1321. /*
  1322. * We're done with this device, so let's remove this device from
  1323. * scanning list
  1324. */
  1325. list_del(&usb_scan->list);
  1326. free(usb_scan);
  1327. return 0;
  1328. }
  1329. static int usb_device_list_scan(void)
  1330. {
  1331. struct usb_device_scan *usb_scan;
  1332. struct usb_device_scan *tmp;
  1333. static int running;
  1334. int ret = 0;
  1335. /* Only run this loop once for each controller */
  1336. if (running)
  1337. return 0;
  1338. running = 1;
  1339. while (1) {
  1340. /* We're done, once the list is empty again */
  1341. if (list_empty(&usb_scan_list))
  1342. goto out;
  1343. for (usb_scan = (struct usb_device_scan*)((&usb_scan_list)->next),
  1344. tmp = (struct usb_device_scan*)(usb_scan->list.next);
  1345. &usb_scan->list != &usb_scan_list;
  1346. usb_scan = tmp, tmp = (struct usb_device_scan*)(tmp->list.next)) {
  1347. int ret;
  1348. /* Scan this port */
  1349. ret = usb_scan_port(usb_scan);
  1350. if (ret)
  1351. goto out;
  1352. }
  1353. }
  1354. out:
  1355. /*
  1356. * This USB controller has finished scanning all its connected
  1357. * USB devices. Set "running" back to 0, so that other USB controllers
  1358. * will scan their devices too.
  1359. */
  1360. running = 0;
  1361. return ret;
  1362. }
  1363. static int usb_hub_configure(struct usb_device *dev)
  1364. {
  1365. int i, length;
  1366. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, USB_BUFSIZ);
  1367. unsigned char *bitmap;
  1368. short hubCharacteristics;
  1369. struct usb_hub_descriptor *descriptor;
  1370. struct usb_hub_device *hub;
  1371. //struct usb_hub_status *hubsts;
  1372. unsigned char *tmp;
  1373. unsigned short tmpval;
  1374. int ret;
  1375. hub = usb_get_hub_device(dev);
  1376. if (hub == NULL)
  1377. return -ENOMEM;
  1378. hub->pusb_dev = dev;
  1379. /* Get the the hub descriptor */
  1380. ret = usb_get_hub_descriptor(dev, buffer, 4);
  1381. if (ret < 0) {
  1382. /* debug("usb_hub_configure: failed to get hub " \
  1383. "descriptor, giving up %lX\n", dev->status); */
  1384. return ret;
  1385. }
  1386. descriptor = (struct usb_hub_descriptor *)buffer;
  1387. length = min(descriptor->bLength,
  1388. sizeof(struct usb_hub_descriptor));
  1389. ret = usb_get_hub_descriptor(dev, buffer, length);
  1390. if (ret < 0) {
  1391. /* debug("usb_hub_configure: failed to get hub " \
  1392. "descriptor 2nd giving up %lX\n", dev->status); */
  1393. return ret;
  1394. }
  1395. memcpy((unsigned char *)&hub->desc, buffer, length);
  1396. /* adjust 16bit values */
  1397. /* put_unaligned(le16_to_cpu(get_unaligned(
  1398. &descriptor->wHubCharacteristics)),
  1399. &hub->desc.wHubCharacteristics); */
  1400. tmp = (unsigned char*)&descriptor->wHubCharacteristics;
  1401. tmpval = (tmp[1] << 8) | tmp[0];
  1402. tmp = (unsigned char*)&hub->desc.wHubCharacteristics;
  1403. tmp[0] = tmpval & 0xff;
  1404. tmp[1] = (tmpval >> 8) & 0xff;
  1405. /* set the bitmap */
  1406. bitmap = (unsigned char *)&hub->desc.u.hs.DeviceRemovable[0];
  1407. /* devices not removable by default */
  1408. memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8);
  1409. bitmap = (unsigned char *)&hub->desc.u.hs.PortPowerCtrlMask[0];
  1410. memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8); /* PowerMask = 1B */
  1411. for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
  1412. hub->desc.u.hs.DeviceRemovable[i] =
  1413. descriptor->u.hs.DeviceRemovable[i];
  1414. for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
  1415. hub->desc.u.hs.PortPowerCtrlMask[i] =
  1416. descriptor->u.hs.PortPowerCtrlMask[i];
  1417. dev->maxchild = descriptor->bNbrPorts;
  1418. //debug("%d ports detected\n", dev->maxchild);
  1419. tmp = (unsigned char*)&hub->desc.wHubCharacteristics;
  1420. hubCharacteristics = (tmp[1] << 8) | tmp[0];//get_unaligned(&hub->desc.wHubCharacteristics);
  1421. switch (hubCharacteristics & HUB_CHAR_LPSM) {
  1422. case 0x00:
  1423. //debug("ganged power switching\n");
  1424. break;
  1425. case 0x01:
  1426. //debug("individual port power switching\n");
  1427. break;
  1428. case 0x02:
  1429. case 0x03:
  1430. //debug("unknown reserved power switching mode\n");
  1431. break;
  1432. }
  1433. if (hubCharacteristics & HUB_CHAR_COMPOUND)
  1434. ;//debug("part of a compound device\n");
  1435. else
  1436. ;//debug("standalone hub\n");
  1437. switch (hubCharacteristics & HUB_CHAR_OCPM) {
  1438. case 0x00:
  1439. //debug("global over-current protection\n");
  1440. break;
  1441. case 0x08:
  1442. //debug("individual port over-current protection\n");
  1443. break;
  1444. case 0x10:
  1445. case 0x18:
  1446. //debug("no over-current protection\n");
  1447. break;
  1448. }
  1449. switch (dev->descriptor.bDeviceProtocol) {
  1450. case USB_HUB_PR_FS:
  1451. break;
  1452. case USB_HUB_PR_HS_SINGLE_TT:
  1453. //debug("Single TT\n");
  1454. break;
  1455. case USB_HUB_PR_HS_MULTI_TT:
  1456. ret = usb_set_interface(dev, 0, 1);
  1457. if (ret == 0) {
  1458. //debug("TT per port\n");
  1459. hub->tt.multi = true;
  1460. } else {
  1461. ;//debug("Using single TT (err %d)\n", ret);
  1462. }
  1463. break;
  1464. case USB_HUB_PR_SS:
  1465. /* USB 3.0 hubs don't have a TT */
  1466. break;
  1467. default:
  1468. /* debug("Unrecognized hub protocol %d\n",
  1469. dev->descriptor.bDeviceProtocol); */
  1470. break;
  1471. }
  1472. /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
  1473. switch (hubCharacteristics & HUB_CHAR_TTTT) {
  1474. case HUB_TTTT_8_BITS:
  1475. if (dev->descriptor.bDeviceProtocol != 0) {
  1476. hub->tt.think_time = 666;
  1477. /* debug("TT requires at most %d FS bit times (%d ns)\n",
  1478. 8, hub->tt.think_time); */
  1479. }
  1480. break;
  1481. case HUB_TTTT_16_BITS:
  1482. hub->tt.think_time = 666 * 2;
  1483. /* debug("TT requires at most %d FS bit times (%d ns)\n",
  1484. 16, hub->tt.think_time); */
  1485. break;
  1486. case HUB_TTTT_24_BITS:
  1487. hub->tt.think_time = 666 * 3;
  1488. /* debug("TT requires at most %d FS bit times (%d ns)\n",
  1489. 24, hub->tt.think_time); */
  1490. break;
  1491. case HUB_TTTT_32_BITS:
  1492. hub->tt.think_time = 666 * 4;
  1493. /* debug("TT requires at most %d FS bit times (%d ns)\n",
  1494. 32, hub->tt.think_time); */
  1495. break;
  1496. }
  1497. /* debug("power on to power good time: %dms\n",
  1498. descriptor->bPwrOn2PwrGood * 2);
  1499. debug("hub controller current requirement: %dmA\n",
  1500. descriptor->bHubContrCurrent);
  1501. for (i = 0; i < dev->maxchild; i++)
  1502. debug("port %d is%s removable\n", i + 1,
  1503. hub->desc.u.hs.DeviceRemovable[(i + 1) / 8] & \
  1504. (1 << ((i + 1) % 8)) ? " not" : ""); */
  1505. if (sizeof(struct usb_hub_status) > USB_BUFSIZ) {
  1506. /* debug("usb_hub_configure: failed to get Status - " \
  1507. "too long: %d\n", descriptor->bLength); */
  1508. return -EFBIG;
  1509. }
  1510. ret = usb_get_hub_status(dev, buffer);
  1511. if (ret < 0) {
  1512. /* debug("usb_hub_configure: failed to get Status %lX\n",
  1513. dev->status); */
  1514. return ret;
  1515. }
  1516. /* hubsts = (struct usb_hub_status *)buffer;
  1517. debug("get_hub_status returned status %X, change %X\n",
  1518. le16_to_cpu(hubsts->wHubStatus),
  1519. le16_to_cpu(hubsts->wHubChange));
  1520. debug("local power source is %s\n",
  1521. (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_LOCAL_POWER) ? \
  1522. "lost (inactive)" : "good");
  1523. debug("%sover-current condition exists\n",
  1524. (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_OVERCURRENT) ? \
  1525. "" : "no "); */
  1526. usb_hub_power_on(hub);
  1527. /*
  1528. * Reset any devices that may be in a bad state when applying
  1529. * the power. This is a __weak function. Resetting of the devices
  1530. * should occur in the board file of the device.
  1531. */
  1532. /* for (i = 0; i < dev->maxchild; i++)
  1533. usb_hub_reset_devices(hub, i + 1); */
  1534. /*
  1535. * Only add the connected USB devices, including potential hubs,
  1536. * to a scanning list. This list will get scanned and devices that
  1537. * are detected (either via port connected or via port timeout)
  1538. * will get removed from this list. Scanning of the devices on this
  1539. * list will continue until all devices are removed.
  1540. */
  1541. for (i = 0; i < dev->maxchild; i++) {
  1542. struct usb_device_scan *usb_scan;
  1543. usb_scan = calloc(1, sizeof(*usb_scan));
  1544. if (!usb_scan) {
  1545. //printf("Can't allocate memory for USB device!\n");
  1546. return -ENOMEM;
  1547. }
  1548. usb_scan->dev = dev;
  1549. usb_scan->hub = hub;
  1550. usb_scan->port = i;
  1551. list_add_tail(&usb_scan->list, &usb_scan_list);
  1552. }
  1553. /*
  1554. * And now call the scanning code which loops over the generated list
  1555. */
  1556. ret = usb_device_list_scan();
  1557. return ret;
  1558. }
  1559. static int usb_hub_check(struct usb_device *dev, int ifnum)
  1560. {
  1561. struct usb_interface *iface;
  1562. struct usb_endpoint_descriptor *ep = NULL;
  1563. iface = &dev->config.if_desc[ifnum];
  1564. /* Is it a hub? */
  1565. if (iface->desc.bInterfaceClass != USB_CLASS_HUB)
  1566. goto err;
  1567. /* Some hubs have a subclass of 1, which AFAICT according to the */
  1568. /* specs is not defined, but it works */
  1569. if ((iface->desc.bInterfaceSubClass != 0) &&
  1570. (iface->desc.bInterfaceSubClass != 1))
  1571. goto err;
  1572. /* Multiple endpoints? What kind of mutant ninja-hub is this? */
  1573. if (iface->desc.bNumEndpoints != 1)
  1574. goto err;
  1575. ep = &iface->ep_desc[0];
  1576. /* Output endpoint? Curiousier and curiousier.. */
  1577. if (!(ep->bEndpointAddress & USB_DIR_IN))
  1578. goto err;
  1579. /* If it's not an interrupt endpoint, we'd better punt! */
  1580. if ((ep->bmAttributes & 3) != 3)
  1581. goto err;
  1582. /* We found a hub */
  1583. //debug("USB hub found\n");
  1584. return 0;
  1585. err:
  1586. /* debug("USB hub not found: bInterfaceClass=%d, bInterfaceSubClass=%d, bNumEndpoints=%d\n",
  1587. iface->desc.bInterfaceClass, iface->desc.bInterfaceSubClass,
  1588. iface->desc.bNumEndpoints);
  1589. if (ep) {
  1590. debug(" bEndpointAddress=%#x, bmAttributes=%d",
  1591. ep->bEndpointAddress, ep->bmAttributes);
  1592. } */
  1593. return -ENOENT;
  1594. }
  1595. int usb_hub_probe(struct usb_device *dev, int ifnum)
  1596. {
  1597. int ret;
  1598. ret = usb_hub_check(dev, ifnum);
  1599. if (ret)
  1600. return 0;
  1601. ret = usb_hub_configure(dev);
  1602. return ret;
  1603. }
  1604. int usb_new_device(struct usb_device *dev)
  1605. {
  1606. bool do_read = true;
  1607. int err;
  1608. err = usb_setup_device(dev, do_read, dev->parent);
  1609. if (err)
  1610. return err;
  1611. /* Now probe if the device is a hub */
  1612. err = usb_hub_probe(dev, 0);
  1613. if (err < 0)
  1614. return err;
  1615. return 0;
  1616. }
  1617. /***************************************************************************
  1618. * Init USB Device
  1619. */
  1620. int usb_init(void)
  1621. {
  1622. void *ctrl;
  1623. struct usb_device *dev;
  1624. int i, start_index = 0;
  1625. int controllers_initialized = 0;
  1626. int ret;
  1627. dev_index = 0;
  1628. asynch_allowed = 1;
  1629. usb_hub_reset();
  1630. /* first make all devices unknown */
  1631. for (i = 0; i < USB_MAX_DEVICE; i++) {
  1632. memset(&usb_dev[i], 0, sizeof(struct usb_device));
  1633. usb_dev[i].devnum = -1;
  1634. }
  1635. /* init low_level USB */
  1636. for (i = 0; i < CONFIG_USB_MAX_CONTROLLER_COUNT; i++) {
  1637. /* init low_level USB */
  1638. ret = usb_lowlevel_init(i, USB_INIT_HOST, &ctrl);
  1639. if (ret == -ENODEV) { /* No such device. */
  1640. SendUartString("Port not available.\n");
  1641. controllers_initialized++;
  1642. continue;
  1643. }
  1644. if (ret) { /* Other error. */
  1645. SendUartString("lowlevel init failed\n");
  1646. continue;
  1647. }
  1648. /*
  1649. * lowlevel init is OK, now scan the bus for devices
  1650. * i.e. search HUBs and configure them
  1651. */
  1652. controllers_initialized++;
  1653. start_index = dev_index;
  1654. ret = usb_alloc_new_device(ctrl, &dev);
  1655. if (ret)
  1656. break;
  1657. /*
  1658. * device 0 is always present
  1659. * (root hub, so let it analyze)
  1660. */
  1661. ret = usb_new_device(dev);
  1662. if (ret)
  1663. usb_free_device(dev->controller);
  1664. if (start_index == dev_index) {
  1665. SendUartString("No USB Device found\n");
  1666. continue;
  1667. } else {
  1668. SendUartString("USB Device(s) found\n");
  1669. }
  1670. usb_started = 1;
  1671. }
  1672. /* if we were not able to find at least one working bus, bail out */
  1673. if (controllers_initialized == 0)
  1674. SendUartString("USB error: all controllers failed lowlevel init\n");
  1675. return usb_started ? 0 : -ENODEV;
  1676. }
  1677. /*
  1678. * disables the asynch behaviour of the control message. This is used for data
  1679. * transfers that uses the exclusiv access to the control and bulk messages.
  1680. * Returns the old value so it can be restored later.
  1681. */
  1682. int usb_disable_asynch(int disable)
  1683. {
  1684. int old_value = asynch_allowed;
  1685. asynch_allowed = !disable;
  1686. return old_value;
  1687. }
  1688. /***********************************************************************
  1689. * Data transfer routines
  1690. ***********************************************************************/
  1691. static int us_one_transfer(struct us_data *us, int pipe, char *buf, int length)
  1692. {
  1693. int max_size;
  1694. int this_xfer;
  1695. int result;
  1696. int partial;
  1697. int maxtry;
  1698. int stat;
  1699. /* determine the maximum packet size for these transfers */
  1700. max_size = usb_maxpacket(us->pusb_dev, pipe) * 16;
  1701. /* while we have data left to transfer */
  1702. while (length) {
  1703. /* calculate how long this will be -- maximum or a remainder */
  1704. this_xfer = length > max_size ? max_size : length;
  1705. length -= this_xfer;
  1706. /* setup the retry counter */
  1707. maxtry = 10;
  1708. /* set up the transfer loop */
  1709. do {
  1710. /* transfer the data */
  1711. /* debug("Bulk xfer 0x%lx(%d) try #%d\n",
  1712. (ulong)map_to_sysmem(buf), this_xfer,
  1713. 11 - maxtry); */
  1714. result = usb_bulk_msg(us->pusb_dev, pipe, buf,
  1715. this_xfer, &partial,
  1716. USB_CNTL_TIMEOUT * 5);
  1717. /* debug("bulk_msg returned %d xferred %d/%d\n",
  1718. result, partial, this_xfer); */
  1719. if (us->pusb_dev->status != 0) {
  1720. /* if we stall, we need to clear it before
  1721. * we go on
  1722. */
  1723. if (us->pusb_dev->status & USB_ST_STALLED) {
  1724. /* debug("stalled ->clearing endpoint" \
  1725. "halt for pipe 0x%x\n", pipe); */
  1726. stat = us->pusb_dev->status;
  1727. usb_clear_halt(us->pusb_dev, pipe);
  1728. us->pusb_dev->status = stat;
  1729. if (this_xfer == partial) {
  1730. /* debug("bulk transferred" \
  1731. "with error %lX," \
  1732. " but data ok\n",
  1733. us->pusb_dev->status); */
  1734. return 0;
  1735. }
  1736. else
  1737. return result;
  1738. }
  1739. if (us->pusb_dev->status & USB_ST_NAK_REC) {
  1740. //debug("Device NAKed bulk_msg\n");
  1741. return result;
  1742. }
  1743. //debug("bulk transferred with error");
  1744. if (this_xfer == partial) {
  1745. /* debug(" %ld, but data ok\n",
  1746. us->pusb_dev->status); */
  1747. return 0;
  1748. }
  1749. /* if our try counter reaches 0, bail out */
  1750. /* debug(" %ld, data %d\n",
  1751. us->pusb_dev->status, partial); */
  1752. if (!maxtry--)
  1753. return result;
  1754. }
  1755. /* update to show what data was transferred */
  1756. this_xfer -= partial;
  1757. buf += partial;
  1758. /* continue until this transfer is done */
  1759. } while (this_xfer);
  1760. }
  1761. /* if we get here, we're done and successful */
  1762. return 0;
  1763. }
  1764. static unsigned int usb_get_max_lun(struct us_data *us)
  1765. {
  1766. int len;
  1767. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, result, 1);
  1768. len = usb_control_msg(us->pusb_dev,
  1769. usb_rcvctrlpipe(us->pusb_dev, 0),
  1770. US_BBB_GET_MAX_LUN,
  1771. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  1772. 0, us->ifnum,
  1773. result, sizeof(char),
  1774. USB_CNTL_TIMEOUT * 5);
  1775. //debug("Get Max LUN -> len = %i, result = %i\n", len, (int) *result);
  1776. return (len > 0) ? *result : 0;
  1777. }
  1778. static int usb_stor_BBB_reset(struct us_data *us)
  1779. {
  1780. int result;
  1781. unsigned int pipe;
  1782. /*
  1783. * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
  1784. *
  1785. * For Reset Recovery the host shall issue in the following order:
  1786. * a) a Bulk-Only Mass Storage Reset
  1787. * b) a Clear Feature HALT to the Bulk-In endpoint
  1788. * c) a Clear Feature HALT to the Bulk-Out endpoint
  1789. *
  1790. * This is done in 3 steps.
  1791. *
  1792. * If the reset doesn't succeed, the device should be port reset.
  1793. *
  1794. * This comment stolen from FreeBSD's /sys/dev/usb/umass.c.
  1795. */
  1796. //debug("BBB_reset\n");
  1797. result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
  1798. US_BBB_RESET,
  1799. USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  1800. 0, us->ifnum, NULL, 0, USB_CNTL_TIMEOUT * 5);
  1801. if ((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
  1802. //debug("RESET:stall\n");
  1803. return -1;
  1804. }
  1805. /* long wait for reset */
  1806. mdelay(150);
  1807. /* debug("BBB_reset result %d: status %lX reset\n",
  1808. result, us->pusb_dev->status); */
  1809. pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
  1810. result = usb_clear_halt(us->pusb_dev, pipe);
  1811. /* long wait for reset */
  1812. mdelay(150);
  1813. /* debug("BBB_reset result %d: status %lX clearing IN endpoint\n",
  1814. result, us->pusb_dev->status); */
  1815. /* long wait for reset */
  1816. pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
  1817. result = usb_clear_halt(us->pusb_dev, pipe);
  1818. mdelay(150);
  1819. /* debug("BBB_reset result %d: status %lX clearing OUT endpoint\n",
  1820. result, us->pusb_dev->status);
  1821. debug("BBB_reset done\n"); */
  1822. return 0;
  1823. }
  1824. /* FIXME: this reset function doesn't really reset the port, and it
  1825. * should. Actually it should probably do what it's doing here, and
  1826. * reset the port physically
  1827. */
  1828. static int usb_stor_CB_reset(struct us_data *us)
  1829. {
  1830. unsigned char cmd[12];
  1831. int result;
  1832. //debug("CB_reset\n");
  1833. memset(cmd, 0xff, sizeof(cmd));
  1834. cmd[0] = SCSI_SEND_DIAG;
  1835. cmd[1] = 4;
  1836. result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
  1837. US_CBI_ADSC,
  1838. USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  1839. 0, us->ifnum, cmd, sizeof(cmd),
  1840. USB_CNTL_TIMEOUT * 5);
  1841. (void)result;
  1842. /* long wait for reset */
  1843. mdelay(1500);
  1844. /* debug("CB_reset result %d: status %lX clearing endpoint halt\n",
  1845. result, us->pusb_dev->status); */
  1846. usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_in));
  1847. usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_out));
  1848. //debug("CB_reset done\n");
  1849. return 0;
  1850. }
  1851. /*
  1852. * Set up the command for a BBB device. Note that the actual SCSI
  1853. * command is copied into cbw.CBWCDB.
  1854. */
  1855. static int usb_stor_BBB_comdat(struct scsi_cmd *srb, struct us_data *us)
  1856. {
  1857. int result;
  1858. int actlen;
  1859. int dir_in;
  1860. unsigned int pipe;
  1861. ALLOC_CACHE_ALIGN_BUFFER(struct umass_bbb_cbw, cbw, 1);
  1862. dir_in = US_DIRECTION(srb->cmd[0]);
  1863. /* sanity checks */
  1864. if (!(srb->cmdlen <= CBWCDBLENGTH)) {
  1865. //debug("usb_stor_BBB_comdat:cmdlen too large\n");
  1866. return -1;
  1867. }
  1868. /* always OUT to the ep */
  1869. pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
  1870. cbw->dCBWSignature = cpu_to_le32(CBWSIGNATURE);
  1871. cbw->dCBWTag = cpu_to_le32(CBWTag++);
  1872. cbw->dCBWDataTransferLength = cpu_to_le32(srb->datalen);
  1873. cbw->bCBWFlags = (dir_in ? CBWFLAGS_IN : CBWFLAGS_OUT);
  1874. cbw->bCBWLUN = srb->lun;
  1875. cbw->bCDBLength = srb->cmdlen;
  1876. /* copy the command data into the CBW command data buffer */
  1877. /* DST SRC LEN!!! */
  1878. memcpy(cbw->CBWCDB, srb->cmd, srb->cmdlen);
  1879. result = usb_bulk_msg(us->pusb_dev, pipe, cbw, UMASS_BBB_CBW_SIZE,
  1880. &actlen, USB_CNTL_TIMEOUT * 5);
  1881. if (result < 0)
  1882. ;//debug("usb_stor_BBB_comdat:usb_bulk_msg error\n");
  1883. return result;
  1884. }
  1885. /* FIXME: we also need a CBI_command which sets up the completion
  1886. * interrupt, and waits for it
  1887. */
  1888. static int usb_stor_CB_comdat(struct scsi_cmd *srb, struct us_data *us)
  1889. {
  1890. int result = 0;
  1891. int dir_in, retry;
  1892. unsigned int pipe;
  1893. unsigned long status;
  1894. retry = 5;
  1895. dir_in = US_DIRECTION(srb->cmd[0]);
  1896. if (dir_in)
  1897. pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
  1898. else
  1899. pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
  1900. while (retry--) {
  1901. //debug("CBI gets a command: Try %d\n", 5 - retry);
  1902. /* let's send the command via the control pipe */
  1903. result = usb_control_msg(us->pusb_dev,
  1904. usb_sndctrlpipe(us->pusb_dev , 0),
  1905. US_CBI_ADSC,
  1906. USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  1907. 0, us->ifnum,
  1908. srb->cmd, srb->cmdlen,
  1909. USB_CNTL_TIMEOUT * 5);
  1910. /* debug("CB_transport: control msg returned %d, status %lX\n",
  1911. result, us->pusb_dev->status); */
  1912. /* check the return code for the command */
  1913. if (result < 0) {
  1914. if (us->pusb_dev->status & USB_ST_STALLED) {
  1915. status = us->pusb_dev->status;
  1916. /* debug(" stall during command found," \
  1917. " clear pipe\n"); */
  1918. usb_clear_halt(us->pusb_dev,
  1919. usb_sndctrlpipe(us->pusb_dev, 0));
  1920. us->pusb_dev->status = status;
  1921. }
  1922. /* debug(" error during command %02X" \
  1923. " Stat = %lX\n", srb->cmd[0],
  1924. us->pusb_dev->status); */
  1925. return result;
  1926. }
  1927. /* transfer the data payload for this command, if one exists*/
  1928. /* debug("CB_transport: control msg returned %d," \
  1929. " direction is %s to go 0x%lx\n", result,
  1930. dir_in ? "IN" : "OUT", srb->datalen); */
  1931. if (srb->datalen) {
  1932. result = us_one_transfer(us, pipe, (char *)srb->pdata,
  1933. srb->datalen);
  1934. /* debug("CBI attempted to transfer data," \
  1935. " result is %d status %lX, len %d\n",
  1936. result, us->pusb_dev->status,
  1937. us->pusb_dev->act_len); */
  1938. if (!(us->pusb_dev->status & USB_ST_NAK_REC))
  1939. break;
  1940. } /* if (srb->datalen) */
  1941. else
  1942. break;
  1943. }
  1944. /* return result */
  1945. return result;
  1946. }
  1947. static int usb_stor_CBI_get_status(struct scsi_cmd *srb, struct us_data *us)
  1948. {
  1949. int timeout;
  1950. us->ip_wanted = 1;
  1951. submit_int_msg(us->pusb_dev, us->irqpipe,
  1952. (void *) &us->ip_data, us->irqmaxp, us->irqinterval);
  1953. timeout = 1000;
  1954. while (timeout--) {
  1955. if (us->ip_wanted == 0)
  1956. break;
  1957. mdelay(10);
  1958. }
  1959. if (us->ip_wanted) {
  1960. //printf(" Did not get interrupt on CBI\n");
  1961. us->ip_wanted = 0;
  1962. return USB_STOR_TRANSPORT_ERROR;
  1963. }
  1964. /* debug("Got interrupt data 0x%x, transferred %d status 0x%lX\n",
  1965. us->ip_data, us->pusb_dev->irq_act_len,
  1966. us->pusb_dev->irq_status); */
  1967. /* UFI gives us ASC and ASCQ, like a request sense */
  1968. if (us->subclass == US_SC_UFI) {
  1969. if (srb->cmd[0] == SCSI_REQ_SENSE ||
  1970. srb->cmd[0] == SCSI_INQUIRY)
  1971. return USB_STOR_TRANSPORT_GOOD; /* Good */
  1972. else if (us->ip_data)
  1973. return USB_STOR_TRANSPORT_FAILED;
  1974. else
  1975. return USB_STOR_TRANSPORT_GOOD;
  1976. }
  1977. /* otherwise, we interpret the data normally */
  1978. switch (us->ip_data) {
  1979. case 0x0001:
  1980. return USB_STOR_TRANSPORT_GOOD;
  1981. case 0x0002:
  1982. return USB_STOR_TRANSPORT_FAILED;
  1983. default:
  1984. return USB_STOR_TRANSPORT_ERROR;
  1985. } /* switch */
  1986. //return USB_STOR_TRANSPORT_ERROR;
  1987. }
  1988. #define USB_TRANSPORT_UNKNOWN_RETRY 5
  1989. #define USB_TRANSPORT_NOT_READY_RETRY 10
  1990. /* clear a stall on an endpoint - special for BBB devices */
  1991. static int usb_stor_BBB_clear_endpt_stall(struct us_data *us, u8 endpt)
  1992. {
  1993. /* ENDPOINT_HALT = 0, so set value to 0 */
  1994. return usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
  1995. USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT, 0,
  1996. endpt, NULL, 0, USB_CNTL_TIMEOUT * 5);
  1997. }
  1998. static int usb_stor_BBB_transport(struct scsi_cmd *srb, struct us_data *us)
  1999. {
  2000. int result, retry;
  2001. int dir_in;
  2002. int actlen, data_actlen;
  2003. unsigned int pipe, pipein, pipeout;
  2004. ALLOC_CACHE_ALIGN_BUFFER(struct umass_bbb_csw, csw, 1);
  2005. dir_in = US_DIRECTION(srb->cmd[0]);
  2006. /* COMMAND phase */
  2007. //debug("COMMAND phase\n");
  2008. result = usb_stor_BBB_comdat(srb, us);
  2009. if (result < 0) {
  2010. /* debug("failed to send CBW status %ld\n",
  2011. us->pusb_dev->status); */
  2012. usb_stor_BBB_reset(us);
  2013. return USB_STOR_TRANSPORT_FAILED;
  2014. }
  2015. if (!(us->flags & USB_READY))
  2016. mdelay(5);
  2017. pipein = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
  2018. pipeout = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
  2019. /* DATA phase + error handling */
  2020. data_actlen = 0;
  2021. /* no data, go immediately to the STATUS phase */
  2022. if (srb->datalen == 0)
  2023. goto st;
  2024. //debug("DATA phase\n");
  2025. if (dir_in)
  2026. pipe = pipein;
  2027. else
  2028. pipe = pipeout;
  2029. result = usb_bulk_msg(us->pusb_dev, pipe, srb->pdata, srb->datalen,
  2030. &data_actlen, USB_CNTL_TIMEOUT * 5);
  2031. /* special handling of STALL in DATA phase */
  2032. if ((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
  2033. //debug("DATA:stall\n");
  2034. /* clear the STALL on the endpoint */
  2035. result = usb_stor_BBB_clear_endpt_stall(us,
  2036. dir_in ? us->ep_in : us->ep_out);
  2037. if (result >= 0)
  2038. /* continue on to STATUS phase */
  2039. goto st;
  2040. }
  2041. if (result < 0) {
  2042. /* debug("usb_bulk_msg error status %ld\n",
  2043. us->pusb_dev->status); */
  2044. usb_stor_BBB_reset(us);
  2045. return USB_STOR_TRANSPORT_FAILED;
  2046. }
  2047. /* STATUS phase + error handling */
  2048. st:
  2049. retry = 0;
  2050. again:
  2051. //debug("STATUS phase\n");
  2052. result = usb_bulk_msg(us->pusb_dev, pipein, csw, UMASS_BBB_CSW_SIZE,
  2053. &actlen, USB_CNTL_TIMEOUT*5);
  2054. /* special handling of STALL in STATUS phase */
  2055. if ((result < 0) && (retry < 1) &&
  2056. (us->pusb_dev->status & USB_ST_STALLED)) {
  2057. //debug("STATUS:stall\n");
  2058. /* clear the STALL on the endpoint */
  2059. result = usb_stor_BBB_clear_endpt_stall(us, us->ep_in);
  2060. if (result >= 0 && (retry++ < 1))
  2061. /* do a retry */
  2062. goto again;
  2063. }
  2064. if (result < 0) {
  2065. /* debug("usb_bulk_msg error status %ld\n",
  2066. us->pusb_dev->status); */
  2067. usb_stor_BBB_reset(us);
  2068. return USB_STOR_TRANSPORT_FAILED;
  2069. }
  2070. /* misuse pipe to get the residue */
  2071. pipe = le32_to_cpu(csw->dCSWDataResidue);
  2072. if (pipe == 0 && srb->datalen != 0 && srb->datalen - data_actlen != 0)
  2073. pipe = srb->datalen - data_actlen;
  2074. if (CSWSIGNATURE != le32_to_cpu(csw->dCSWSignature)) {
  2075. //debug("!CSWSIGNATURE\n");
  2076. usb_stor_BBB_reset(us);
  2077. return USB_STOR_TRANSPORT_FAILED;
  2078. } else if ((CBWTag - 1) != le32_to_cpu(csw->dCSWTag)) {
  2079. //debug("!Tag\n");
  2080. usb_stor_BBB_reset(us);
  2081. return USB_STOR_TRANSPORT_FAILED;
  2082. } else if (csw->bCSWStatus > CSWSTATUS_PHASE) {
  2083. //debug(">PHASE\n");
  2084. usb_stor_BBB_reset(us);
  2085. return USB_STOR_TRANSPORT_FAILED;
  2086. } else if (csw->bCSWStatus == CSWSTATUS_PHASE) {
  2087. //debug("=PHASE\n");
  2088. usb_stor_BBB_reset(us);
  2089. return USB_STOR_TRANSPORT_FAILED;
  2090. } else if (data_actlen > srb->datalen) {
  2091. /* debug("transferred %dB instead of %ldB\n",
  2092. data_actlen, srb->datalen); */
  2093. return USB_STOR_TRANSPORT_FAILED;
  2094. } else if (csw->bCSWStatus == CSWSTATUS_FAILED) {
  2095. //debug("FAILED\n");
  2096. return USB_STOR_TRANSPORT_FAILED;
  2097. }
  2098. return result;
  2099. }
  2100. static int usb_stor_CB_transport(struct scsi_cmd *srb, struct us_data *us)
  2101. {
  2102. int result, status;
  2103. struct scsi_cmd *psrb;
  2104. struct scsi_cmd reqsrb;
  2105. int retry, notready;
  2106. psrb = &reqsrb;
  2107. status = USB_STOR_TRANSPORT_GOOD;
  2108. retry = 0;
  2109. notready = 0;
  2110. /* issue the command */
  2111. do_retry:
  2112. result = usb_stor_CB_comdat(srb, us);
  2113. /* debug("command / Data returned %d, status %lX\n",
  2114. result, us->pusb_dev->status); */
  2115. /* if this is an CBI Protocol, get IRQ */
  2116. if (us->protocol == US_PR_CBI) {
  2117. status = usb_stor_CBI_get_status(srb, us);
  2118. /* if the status is error, report it */
  2119. if (status == USB_STOR_TRANSPORT_ERROR) {
  2120. //debug(" USB CBI Command Error\n");
  2121. return status;
  2122. }
  2123. srb->sense_buf[12] = (unsigned char)(us->ip_data >> 8);
  2124. srb->sense_buf[13] = (unsigned char)(us->ip_data & 0xff);
  2125. if (!us->ip_data) {
  2126. /* if the status is good, report it */
  2127. if (status == USB_STOR_TRANSPORT_GOOD) {
  2128. //debug(" USB CBI Command Good\n");
  2129. return status;
  2130. }
  2131. }
  2132. }
  2133. /* do we have to issue an auto request? */
  2134. /* HERE we have to check the result */
  2135. if ((result < 0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
  2136. //debug("ERROR %lX\n", us->pusb_dev->status);
  2137. us->transport_reset(us);
  2138. return USB_STOR_TRANSPORT_ERROR;
  2139. }
  2140. if ((us->protocol == US_PR_CBI) &&
  2141. ((srb->cmd[0] == SCSI_REQ_SENSE) ||
  2142. (srb->cmd[0] == SCSI_INQUIRY))) {
  2143. /* do not issue an autorequest after request sense */
  2144. //debug("No auto request and good\n");
  2145. return USB_STOR_TRANSPORT_GOOD;
  2146. }
  2147. /* issue an request_sense */
  2148. memset(&psrb->cmd[0], 0, 12);
  2149. psrb->cmd[0] = SCSI_REQ_SENSE;
  2150. psrb->cmd[1] = srb->lun << 5;
  2151. psrb->cmd[4] = 18;
  2152. psrb->datalen = 18;
  2153. psrb->pdata = &srb->sense_buf[0];
  2154. psrb->cmdlen = 12;
  2155. /* issue the command */
  2156. result = usb_stor_CB_comdat(psrb, us);
  2157. //debug("auto request returned %d\n", result);
  2158. /* if this is an CBI Protocol, get IRQ */
  2159. if (us->protocol == US_PR_CBI)
  2160. status = usb_stor_CBI_get_status(psrb, us);
  2161. if ((result < 0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
  2162. /* debug(" AUTO REQUEST ERROR %ld\n",
  2163. us->pusb_dev->status); */
  2164. return USB_STOR_TRANSPORT_ERROR;
  2165. }
  2166. /* debug("autorequest returned 0x%02X 0x%02X 0x%02X 0x%02X\n",
  2167. srb->sense_buf[0], srb->sense_buf[2],
  2168. srb->sense_buf[12], srb->sense_buf[13]); */
  2169. /* Check the auto request result */
  2170. if ((srb->sense_buf[2] == 0) &&
  2171. (srb->sense_buf[12] == 0) &&
  2172. (srb->sense_buf[13] == 0)) {
  2173. /* ok, no sense */
  2174. return USB_STOR_TRANSPORT_GOOD;
  2175. }
  2176. /* Check the auto request result */
  2177. switch (srb->sense_buf[2]) {
  2178. case 0x01:
  2179. /* Recovered Error */
  2180. return USB_STOR_TRANSPORT_GOOD;
  2181. break;
  2182. case 0x02:
  2183. /* Not Ready */
  2184. if (notready++ > USB_TRANSPORT_NOT_READY_RETRY) {
  2185. /* printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X"
  2186. " 0x%02X (NOT READY)\n", srb->cmd[0],
  2187. srb->sense_buf[0], srb->sense_buf[2],
  2188. srb->sense_buf[12], srb->sense_buf[13]); */
  2189. return USB_STOR_TRANSPORT_FAILED;
  2190. } else {
  2191. mdelay(100);
  2192. goto do_retry;
  2193. }
  2194. break;
  2195. default:
  2196. if (retry++ > USB_TRANSPORT_UNKNOWN_RETRY) {
  2197. /* printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X"
  2198. " 0x%02X\n", srb->cmd[0], srb->sense_buf[0],
  2199. srb->sense_buf[2], srb->sense_buf[12],
  2200. srb->sense_buf[13]); */
  2201. return USB_STOR_TRANSPORT_FAILED;
  2202. } else
  2203. goto do_retry;
  2204. break;
  2205. }
  2206. //return USB_STOR_TRANSPORT_FAILED;
  2207. }
  2208. static int usb_stor_irq(struct usb_device *dev)
  2209. {
  2210. struct us_data *us;
  2211. us = (struct us_data *)dev->privptr;
  2212. if (us->ip_wanted)
  2213. us->ip_wanted = 0;
  2214. return 0;
  2215. }
  2216. static void usb_stor_set_max_xfer_blk(struct usb_device *udev,
  2217. struct us_data *us)
  2218. {
  2219. unsigned short blk = 20;
  2220. us->max_xfer_blk = blk;
  2221. }
  2222. static int usb_inquiry(struct scsi_cmd *srb, struct us_data *ss)
  2223. {
  2224. int retry, i;
  2225. retry = 5;
  2226. do {
  2227. memset(&srb->cmd[0], 0, 12);
  2228. srb->cmd[0] = SCSI_INQUIRY;
  2229. srb->cmd[1] = srb->lun << 5;
  2230. srb->cmd[4] = 36;
  2231. srb->datalen = 36;
  2232. srb->cmdlen = 12;
  2233. i = ss->transport(srb, ss);
  2234. //debug("inquiry returns %d\n", i);
  2235. if (i == 0)
  2236. break;
  2237. } while (--retry);
  2238. if (!retry) {
  2239. //printf("error in inquiry\n");
  2240. return -1;
  2241. }
  2242. return 0;
  2243. }
  2244. static int usb_request_sense(struct scsi_cmd *srb, struct us_data *ss)
  2245. {
  2246. char *ptr;
  2247. ptr = (char *)srb->pdata;
  2248. memset(&srb->cmd[0], 0, 12);
  2249. srb->cmd[0] = SCSI_REQ_SENSE;
  2250. srb->cmd[1] = srb->lun << 5;
  2251. srb->cmd[4] = 18;
  2252. srb->datalen = 18;
  2253. srb->pdata = &srb->sense_buf[0];
  2254. srb->cmdlen = 12;
  2255. ss->transport(srb, ss);
  2256. /* debug("Request Sense returned %02X %02X %02X\n",
  2257. srb->sense_buf[2], srb->sense_buf[12],
  2258. srb->sense_buf[13]); */
  2259. srb->pdata = (unsigned char *)ptr;
  2260. return 0;
  2261. }
  2262. static int usb_test_unit_ready(struct scsi_cmd *srb, struct us_data *ss)
  2263. {
  2264. int retries = 10;
  2265. do {
  2266. memset(&srb->cmd[0], 0, 12);
  2267. srb->cmd[0] = SCSI_TST_U_RDY;
  2268. srb->cmd[1] = srb->lun << 5;
  2269. srb->datalen = 0;
  2270. srb->cmdlen = 12;
  2271. if (ss->transport(srb, ss) == USB_STOR_TRANSPORT_GOOD) {
  2272. ss->flags |= USB_READY;
  2273. return 0;
  2274. }
  2275. usb_request_sense(srb, ss);
  2276. /*
  2277. * Check the Key Code Qualifier, if it matches
  2278. * "Not Ready - medium not present"
  2279. * (the sense Key equals 0x2 and the ASC is 0x3a)
  2280. * return immediately as the medium being absent won't change
  2281. * unless there is a user action.
  2282. */
  2283. if ((srb->sense_buf[2] == 0x02) &&
  2284. (srb->sense_buf[12] == 0x3a))
  2285. return -1;
  2286. mdelay(100);
  2287. } while (retries--);
  2288. return -1;
  2289. }
  2290. static int usb_read_capacity(struct scsi_cmd *srb, struct us_data *ss)
  2291. {
  2292. int retry;
  2293. /* XXX retries */
  2294. retry = 3;
  2295. do {
  2296. memset(&srb->cmd[0], 0, 12);
  2297. srb->cmd[0] = SCSI_RD_CAPAC;
  2298. srb->cmd[1] = srb->lun << 5;
  2299. srb->datalen = 8;
  2300. srb->cmdlen = 12;
  2301. if (ss->transport(srb, ss) == USB_STOR_TRANSPORT_GOOD)
  2302. return 0;
  2303. } while (retry--);
  2304. return -1;
  2305. }
  2306. static int usb_read_10(struct scsi_cmd *srb, struct us_data *ss,
  2307. unsigned long start, unsigned short blocks)
  2308. {
  2309. memset(&srb->cmd[0], 0, 12);
  2310. srb->cmd[0] = SCSI_READ10;
  2311. srb->cmd[1] = srb->lun << 5;
  2312. srb->cmd[2] = ((unsigned char) (start >> 24)) & 0xff;
  2313. srb->cmd[3] = ((unsigned char) (start >> 16)) & 0xff;
  2314. srb->cmd[4] = ((unsigned char) (start >> 8)) & 0xff;
  2315. srb->cmd[5] = ((unsigned char) (start)) & 0xff;
  2316. srb->cmd[7] = ((unsigned char) (blocks >> 8)) & 0xff;
  2317. srb->cmd[8] = (unsigned char) blocks & 0xff;
  2318. srb->cmdlen = 12;
  2319. //debug("read10: start %lx blocks %x\n", start, blocks);
  2320. return ss->transport(srb, ss);
  2321. }
  2322. /* Probe to see if a new device is actually a Storage device */
  2323. int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,
  2324. struct us_data *ss)
  2325. {
  2326. struct usb_interface *iface;
  2327. int i;
  2328. struct usb_endpoint_descriptor *ep_desc;
  2329. unsigned int flags = 0;
  2330. /* let's examine the device now */
  2331. iface = &dev->config.if_desc[ifnum];
  2332. if (dev->descriptor.bDeviceClass != 0 ||
  2333. iface->desc.bInterfaceClass != USB_CLASS_MASS_STORAGE ||
  2334. iface->desc.bInterfaceSubClass < US_SC_MIN ||
  2335. iface->desc.bInterfaceSubClass > US_SC_MAX) {
  2336. //SendUartString("Not mass storage\n");
  2337. /* if it's not a mass storage, we go no further */
  2338. return 0;
  2339. }
  2340. memset(ss, 0, sizeof(struct us_data));
  2341. /* At this point, we know we've got a live one */
  2342. //debug("\n\nUSB Mass Storage device detected\n");
  2343. /* Initialize the us_data structure with some useful info */
  2344. ss->flags = flags;
  2345. ss->ifnum = ifnum;
  2346. ss->pusb_dev = dev;
  2347. ss->attention_done = 0;
  2348. ss->subclass = iface->desc.bInterfaceSubClass;
  2349. ss->protocol = iface->desc.bInterfaceProtocol;
  2350. /* set the handler pointers based on the protocol */
  2351. //debug("Transport: ");
  2352. switch (ss->protocol) {
  2353. case US_PR_CB:
  2354. //debug("Control/Bulk\n");
  2355. ss->transport = usb_stor_CB_transport;
  2356. ss->transport_reset = usb_stor_CB_reset;
  2357. break;
  2358. case US_PR_CBI:
  2359. //debug("Control/Bulk/Interrupt\n");
  2360. ss->transport = usb_stor_CB_transport;
  2361. ss->transport_reset = usb_stor_CB_reset;
  2362. break;
  2363. case US_PR_BULK:
  2364. //debug("Bulk/Bulk/Bulk\n");
  2365. ss->transport = usb_stor_BBB_transport;
  2366. ss->transport_reset = usb_stor_BBB_reset;
  2367. break;
  2368. default:
  2369. //printf("USB Storage Transport unknown / not yet implemented\n");
  2370. return 0;
  2371. break;
  2372. }
  2373. /*
  2374. * We are expecting a minimum of 2 endpoints - in and out (bulk).
  2375. * An optional interrupt is OK (necessary for CBI protocol).
  2376. * We will ignore any others.
  2377. */
  2378. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2379. ep_desc = &iface->ep_desc[i];
  2380. /* is it an BULK endpoint? */
  2381. if ((ep_desc->bmAttributes &
  2382. USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) {
  2383. if (ep_desc->bEndpointAddress & USB_DIR_IN)
  2384. ss->ep_in = ep_desc->bEndpointAddress &
  2385. USB_ENDPOINT_NUMBER_MASK;
  2386. else
  2387. ss->ep_out =
  2388. ep_desc->bEndpointAddress &
  2389. USB_ENDPOINT_NUMBER_MASK;
  2390. }
  2391. /* is it an interrupt endpoint? */
  2392. if ((ep_desc->bmAttributes &
  2393. USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) {
  2394. ss->ep_int = ep_desc->bEndpointAddress &
  2395. USB_ENDPOINT_NUMBER_MASK;
  2396. ss->irqinterval = ep_desc->bInterval;
  2397. }
  2398. }
  2399. /* debug("Endpoints In %d Out %d Int %d\n",
  2400. ss->ep_in, ss->ep_out, ss->ep_int); */
  2401. /* Do some basic sanity checks, and bail if we find a problem */
  2402. if (usb_set_interface(dev, iface->desc.bInterfaceNumber, 0) ||
  2403. !ss->ep_in || !ss->ep_out ||
  2404. (ss->protocol == US_PR_CBI && ss->ep_int == 0)) {
  2405. //debug("Problems with device\n");
  2406. return 0;
  2407. }
  2408. /* set class specific stuff */
  2409. /* We only handle certain protocols. Currently, these are
  2410. * the only ones.
  2411. * The SFF8070 accepts the requests used in u-boot
  2412. */
  2413. if (ss->subclass != US_SC_UFI && ss->subclass != US_SC_SCSI &&
  2414. ss->subclass != US_SC_8070) {
  2415. //printf("Sorry, protocol %d not yet supported.\n", ss->subclass);
  2416. return 0;
  2417. }
  2418. if (ss->ep_int) {
  2419. /* we had found an interrupt endpoint, prepare irq pipe
  2420. * set up the IRQ pipe and handler
  2421. */
  2422. ss->irqinterval = (ss->irqinterval > 0) ? ss->irqinterval : 255;
  2423. ss->irqpipe = usb_rcvintpipe(ss->pusb_dev, ss->ep_int);
  2424. ss->irqmaxp = usb_maxpacket(dev, ss->irqpipe);
  2425. dev->irq_handle = usb_stor_irq;
  2426. }
  2427. /* Set the maximum transfer size per host controller setting */
  2428. usb_stor_set_max_xfer_blk(dev, ss);
  2429. dev->privptr = (void *)ss;
  2430. return 1;
  2431. }
  2432. int usb_stor_get_info(struct usb_device *dev, struct us_data *ss,
  2433. struct blk_desc *dev_desc)
  2434. {
  2435. unsigned char perq, modi;
  2436. ALLOC_CACHE_ALIGN_BUFFER(u32, cap, 2);
  2437. ALLOC_CACHE_ALIGN_BUFFER(u8, usb_stor_buf, 36);
  2438. u32 capacity, blksz;
  2439. struct scsi_cmd *pccb = &usb_ccb;
  2440. pccb->pdata = usb_stor_buf;
  2441. dev_desc->target = dev->devnum;
  2442. pccb->lun = dev_desc->lun;
  2443. //debug(" address %d\n", dev_desc->target);
  2444. if (usb_inquiry(pccb, ss)) {
  2445. //debug("%s: usb_inquiry() failed\n", __func__);
  2446. return -1;
  2447. }
  2448. perq = usb_stor_buf[0];
  2449. modi = usb_stor_buf[1];
  2450. /*
  2451. * Skip unknown devices (0x1f) and enclosure service devices (0x0d),
  2452. * they would not respond to test_unit_ready .
  2453. */
  2454. if (((perq & 0x1f) == 0x1f) || ((perq & 0x1f) == 0x0d)) {
  2455. //debug("%s: unknown/unsupported device\n", __func__);
  2456. return 0;
  2457. }
  2458. if ((modi&0x80) == 0x80) {
  2459. /* drive is removable */
  2460. dev_desc->removable = 1;
  2461. }
  2462. memcpy(dev_desc->vendor, (const void *)&usb_stor_buf[8], 8);
  2463. memcpy(dev_desc->product, (const void *)&usb_stor_buf[16], 16);
  2464. memcpy(dev_desc->revision, (const void *)&usb_stor_buf[32], 4);
  2465. dev_desc->vendor[8] = 0;
  2466. dev_desc->product[16] = 0;
  2467. dev_desc->revision[4] = 0;
  2468. /* debug("ISO Vers %X, Response Data %X\n", usb_stor_buf[2],
  2469. usb_stor_buf[3]); */
  2470. if (usb_test_unit_ready(pccb, ss)) {
  2471. /* printf("Device NOT ready\n"
  2472. " Request Sense returned %02X %02X %02X\n",
  2473. pccb->sense_buf[2], pccb->sense_buf[12],
  2474. pccb->sense_buf[13]); */
  2475. if (dev_desc->removable == 1)
  2476. dev_desc->type = perq;
  2477. return 0;
  2478. }
  2479. pccb->pdata = (unsigned char *)cap;
  2480. memset(pccb->pdata, 0, 8);
  2481. if (usb_read_capacity(pccb, ss) != 0) {
  2482. //printf("READ_CAP ERROR\n");
  2483. cap[0] = 2880;
  2484. cap[1] = 0x200;
  2485. }
  2486. ss->flags &= ~USB_READY;
  2487. //debug("Read Capacity returns: 0x%08x, 0x%08x\n", cap[0], cap[1]);
  2488. #if 0
  2489. if (cap[0] > (0x200000 * 10)) /* greater than 10 GByte */
  2490. cap[0] >>= 16;
  2491. cap[0] = cpu_to_be32(cap[0]);
  2492. cap[1] = cpu_to_be32(cap[1]);
  2493. #endif
  2494. capacity = be32_to_cpu(cap[0]) + 1;
  2495. blksz = be32_to_cpu(cap[1]);
  2496. dev_desc->lba = capacity;
  2497. dev_desc->blksz = blksz;
  2498. dev_desc->log2blksz = LOG2(dev_desc->blksz);
  2499. dev_desc->type = perq;
  2500. return 1;
  2501. }
  2502. static unsigned long usb_stor_read(struct blk_desc *block_dev, lbaint_t blknr,
  2503. lbaint_t blkcnt, void *buffer)
  2504. {
  2505. lbaint_t start, blks;
  2506. uintptr_t buf_addr;
  2507. unsigned short smallblks;
  2508. struct usb_device *udev;
  2509. struct us_data *ss;
  2510. int retry;
  2511. struct scsi_cmd *srb = &usb_ccb;
  2512. if (blkcnt == 0)
  2513. return 0;
  2514. /* Setup device */
  2515. //debug("\nusb_read: udev %d\n", block_dev->devnum);
  2516. udev = usb_dev_desc[block_dev->devnum].priv;
  2517. if (!udev) {
  2518. //debug("%s: No device\n", __func__);
  2519. return 0;
  2520. }
  2521. ss = (struct us_data *)udev->privptr;
  2522. usb_disable_asynch(1); /* asynch transfer not allowed */
  2523. srb->lun = block_dev->lun;
  2524. buf_addr = (uintptr_t)buffer;
  2525. start = blknr;
  2526. blks = blkcnt;
  2527. /* debug("\nusb_read: dev %d startblk " LBAF ", blccnt " LBAF " buffer %"
  2528. PRIxPTR "\n", block_dev->devnum, start, blks, buf_addr); */
  2529. do {
  2530. /* XXX need some comment here */
  2531. retry = 2;
  2532. srb->pdata = (unsigned char *)buf_addr;
  2533. if (blks > ss->max_xfer_blk)
  2534. smallblks = ss->max_xfer_blk;
  2535. else
  2536. smallblks = (unsigned short) blks;
  2537. retry_it:
  2538. /* if (smallblks == ss->max_xfer_blk)
  2539. usb_show_progress(); */
  2540. srb->datalen = block_dev->blksz * smallblks;
  2541. srb->pdata = (unsigned char *)buf_addr;
  2542. if (usb_read_10(srb, ss, start, smallblks)) {
  2543. //debug("Read ERROR\n");
  2544. usb_request_sense(srb, ss);
  2545. if (retry--)
  2546. goto retry_it;
  2547. blkcnt -= blks;
  2548. break;
  2549. }
  2550. start += smallblks;
  2551. blks -= smallblks;
  2552. buf_addr += srb->datalen;
  2553. } while (blks != 0);
  2554. ss->flags &= ~USB_READY;
  2555. /* debug("usb_read: end startblk " LBAF
  2556. ", blccnt %x buffer %" PRIxPTR "\n",
  2557. start, smallblks, buf_addr); */
  2558. usb_disable_asynch(0); /* asynch transfer allowed */
  2559. /* if (blkcnt >= ss->max_xfer_blk)
  2560. debug("\n"); */
  2561. return blkcnt;
  2562. }
  2563. void usb_find_usb2_hub_address_port(struct usb_device *udev,
  2564. uint8_t *hub_address, uint8_t *hub_port)
  2565. {
  2566. /* Find out the nearest parent which is high speed */
  2567. while (udev->parent->parent != NULL)
  2568. if (udev->parent->speed != USB_SPEED_HIGH) {
  2569. udev = udev->parent;
  2570. } else {
  2571. *hub_address = udev->parent->devnum;
  2572. *hub_port = udev->portnr;
  2573. return;
  2574. }
  2575. *hub_address = 0;
  2576. *hub_port = 0;
  2577. }
  2578. static int usb_stor_probe_device(struct usb_device *udev)
  2579. {
  2580. int lun, max_lun;
  2581. int start;
  2582. if (udev == NULL)
  2583. return -ENOENT; /* no more devices available */
  2584. //debug("\n\nProbing for storage\n");
  2585. /* We don't have space to even probe if we hit the maximum */
  2586. if (usb_max_devs == USB_MAX_STOR_DEV) {
  2587. /* printf("max USB Storage Device reached: %d stopping\n",
  2588. usb_max_devs); */
  2589. return -ENOSPC;
  2590. }
  2591. if (!usb_storage_probe(udev, 0, &usb_stor[usb_max_devs]))
  2592. return 0;
  2593. /*
  2594. * OK, it's a storage device. Iterate over its LUNs and populate
  2595. * usb_dev_desc'
  2596. */
  2597. start = usb_max_devs;
  2598. max_lun = usb_get_max_lun(&usb_stor[usb_max_devs]);
  2599. for (lun = 0; lun <= max_lun && usb_max_devs < USB_MAX_STOR_DEV;
  2600. lun++) {
  2601. struct blk_desc *blkdev;
  2602. blkdev = &usb_dev_desc[usb_max_devs];
  2603. memset(blkdev, '\0', sizeof(struct blk_desc));
  2604. blkdev->if_type = IF_TYPE_USB;
  2605. blkdev->devnum = usb_max_devs;
  2606. blkdev->part_type = PART_TYPE_UNKNOWN;
  2607. blkdev->target = 0xff;
  2608. blkdev->type = DEV_TYPE_UNKNOWN;
  2609. blkdev->block_read = usb_stor_read;
  2610. //blkdev->block_write = usb_stor_write;
  2611. blkdev->lun = lun;
  2612. blkdev->priv = udev;
  2613. if (usb_stor_get_info(udev, &usb_stor[start],
  2614. &usb_dev_desc[usb_max_devs]) == 1) {
  2615. //debug("partype: %d\n", blkdev->part_type);
  2616. //part_init(blkdev);
  2617. //debug("partype: %d\n", blkdev->part_type);
  2618. usb_max_devs++;
  2619. //debug("%s: Found device %p\n", __func__, udev);
  2620. }
  2621. }
  2622. return 0;
  2623. }
  2624. void usb_stor_reset(void)
  2625. {
  2626. usb_max_devs = 0;
  2627. }
  2628. /*******************************************************************************
  2629. * scan the usb and reports device info
  2630. * to the user if mode = 1
  2631. * returns current device or -1 if no
  2632. */
  2633. int usb_stor_scan(int mode)
  2634. {
  2635. unsigned char i;
  2636. if (mode == 1)
  2637. SendUartString("\rScanning usb for storage devices... ");
  2638. usb_disable_asynch(1); /* asynch transfer not allowed */
  2639. usb_stor_reset();
  2640. for (i = 0; i < USB_MAX_DEVICE; i++) {
  2641. struct usb_device *dev;
  2642. dev = usb_get_dev_index(i); /* get device */
  2643. //debug("i=%d\n", i);
  2644. if (usb_stor_probe_device(dev))
  2645. break;
  2646. } /* for */
  2647. usb_disable_asynch(0); /* asynch transfer allowed */
  2648. SendUartString("\rStorage Device(s) found\r\n");
  2649. if (usb_max_devs > 0)
  2650. return 0;
  2651. return -1;
  2652. }
  2653. extern void UpdateFromMedia(int drv);
  2654. void updateFromUSB(void)
  2655. {
  2656. /* set usb0 host mode */
  2657. rSYS_ANA_CFG &= ~(1 << 25);
  2658. rSYS_ANA_CFG |= (1 << 24);
  2659. udelay(100);
  2660. /* soft reset phy */
  2661. rSYS_SOFT_RST &= ~(1 << 30);
  2662. udelay(100);
  2663. rSYS_SOFT_RST |= (1 << 30);
  2664. udelay(1000);
  2665. /* soft reset controller */
  2666. rSYS_SOFT_RST &= ~(1 << 3);
  2667. udelay(100);
  2668. rSYS_SOFT_RST |= (1 << 3);
  2669. udelay(100);
  2670. if (usb_init() < 0) {
  2671. SendUartString("usb_init fail.\n");
  2672. return;
  2673. }
  2674. usb_stor_curr_dev = usb_stor_scan(1);
  2675. if (usb_stor_curr_dev < 0) {
  2676. SendUartString("usb_stor_scan fail.\n");
  2677. return;
  2678. }
  2679. SendUartString("usb_stor_scan ok.\n");
  2680. UpdateFromMedia(USB);
  2681. }
  2682. int USB_disk_initialize(void)
  2683. {
  2684. return 0;
  2685. }
  2686. int USB_disk_read(void *buff, DWORD sector, BYTE count)
  2687. {
  2688. struct blk_desc *dev_desc = &usb_dev_desc[0];
  2689. return dev_desc->block_read(dev_desc, sector, count, buff);
  2690. }
  2691. int USB_disk_ioctl(BYTE ctrl, void *buff)
  2692. {
  2693. struct blk_desc *dev_desc = &usb_dev_desc[0];
  2694. switch(ctrl)
  2695. {
  2696. case CTRL_SYNC:
  2697. break;
  2698. case GET_SECTOR_COUNT:
  2699. *(DWORD*)buff = dev_desc->lba;
  2700. break;
  2701. case GET_BLOCK_SIZE:
  2702. *(DWORD*)buff = 1;
  2703. break;
  2704. default:
  2705. return -1;
  2706. }
  2707. return 0;
  2708. }