mod_host.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  1. // SPDX-License-Identifier: GPL-1.0+
  2. /*
  3. * Renesas USB driver
  4. *
  5. * Copyright (C) 2011 Renesas Solutions Corp.
  6. * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  7. */
  8. #include <linux/io.h>
  9. #include <linux/list.h>
  10. #include <linux/module.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/slab.h>
  13. #include <linux/usb.h>
  14. #include <linux/usb/hcd.h>
  15. #include "common.h"
  16. /*
  17. *** HARDWARE LIMITATION ***
  18. *
  19. * 1) renesas_usbhs has a limited number of controllable devices.
  20. * it can control only 9 devices in generally.
  21. * see DEVADDn / DCPMAXP / PIPEMAXP.
  22. *
  23. * 2) renesas_usbhs pipe number is limited.
  24. * the pipe will be re-used for each devices.
  25. * so, software should control DATA0/1 sequence of each devices.
  26. */
  27. /*
  28. * image of mod_host
  29. *
  30. * +--------+
  31. * | udev 0 | --> it is used when set address
  32. * +--------+
  33. *
  34. * +--------+ pipes are reused for each uep.
  35. * | udev 1 |-+- [uep 0 (dcp) ] --+ pipe will be switched when
  36. * +--------+ | | other device requested
  37. * +- [uep 1 (bulk)] --|---+ +--------------+
  38. * | +--------------> | pipe0 (dcp) |
  39. * +- [uep 2 (bulk)] -@ | +--------------+
  40. * | | pipe1 (isoc) |
  41. * +--------+ | +--------------+
  42. * | udev 2 |-+- [uep 0 (dcp) ] -@ +----------> | pipe2 (bulk) |
  43. * +--------+ | +--------------+
  44. * +- [uep 1 (int) ] ----+ +------> | pipe3 (bulk) |
  45. * | | +--------------+
  46. * +--------+ +-----|------> | pipe4 (int) |
  47. * | udev 3 |-+- [uep 0 (dcp) ] -@ | +--------------+
  48. * +--------+ | | | .... |
  49. * +- [uep 1 (bulk)] -@ | | .... |
  50. * | |
  51. * +- [uep 2 (bulk)]-----------+
  52. *
  53. * @ : uep requested free pipe, but all have been used.
  54. * now it is waiting for free pipe
  55. */
  56. /*
  57. * struct
  58. */
  59. struct usbhsh_request {
  60. struct urb *urb;
  61. struct usbhs_pkt pkt;
  62. };
  63. struct usbhsh_device {
  64. struct usb_device *usbv;
  65. struct list_head ep_list_head; /* list of usbhsh_ep */
  66. };
  67. struct usbhsh_ep {
  68. struct usbhs_pipe *pipe; /* attached pipe */
  69. struct usbhsh_device *udev; /* attached udev */
  70. struct usb_host_endpoint *ep;
  71. struct list_head ep_list; /* list to usbhsh_device */
  72. unsigned int counter; /* pipe attach counter */
  73. };
  74. #define USBHSH_DEVICE_MAX 10 /* see DEVADDn / DCPMAXP / PIPEMAXP */
  75. #define USBHSH_PORT_MAX 7 /* see DEVADDn :: HUBPORT */
  76. struct usbhsh_hpriv {
  77. struct usbhs_mod mod;
  78. struct usbhs_pipe *dcp;
  79. struct usbhsh_device udev[USBHSH_DEVICE_MAX];
  80. u32 port_stat; /* USB_PORT_STAT_xxx */
  81. struct completion setup_ack_done;
  82. };
  83. static const char usbhsh_hcd_name[] = "renesas_usbhs host";
  84. /*
  85. * macro
  86. */
  87. #define usbhsh_priv_to_hpriv(priv) \
  88. container_of(usbhs_mod_get(priv, USBHS_HOST), struct usbhsh_hpriv, mod)
  89. #define __usbhsh_for_each_udev(start, pos, h, i) \
  90. for ((i) = start; \
  91. ((i) < USBHSH_DEVICE_MAX) && ((pos) = (h)->udev + (i)); \
  92. (i)++)
  93. #define usbhsh_for_each_udev(pos, hpriv, i) \
  94. __usbhsh_for_each_udev(1, pos, hpriv, i)
  95. #define usbhsh_for_each_udev_with_dev0(pos, hpriv, i) \
  96. __usbhsh_for_each_udev(0, pos, hpriv, i)
  97. #define usbhsh_hcd_to_hpriv(h) (struct usbhsh_hpriv *)((h)->hcd_priv)
  98. #define usbhsh_hcd_to_dev(h) ((h)->self.controller)
  99. #define usbhsh_hpriv_to_priv(h) ((h)->mod.priv)
  100. #define usbhsh_hpriv_to_dcp(h) ((h)->dcp)
  101. #define usbhsh_hpriv_to_hcd(h) \
  102. container_of((void *)h, struct usb_hcd, hcd_priv)
  103. #define usbhsh_ep_to_uep(u) ((u)->hcpriv)
  104. #define usbhsh_uep_to_pipe(u) ((u)->pipe)
  105. #define usbhsh_uep_to_udev(u) ((u)->udev)
  106. #define usbhsh_uep_to_ep(u) ((u)->ep)
  107. #define usbhsh_urb_to_ureq(u) ((u)->hcpriv)
  108. #define usbhsh_urb_to_usbv(u) ((u)->dev)
  109. #define usbhsh_usbv_to_udev(d) dev_get_drvdata(&(d)->dev)
  110. #define usbhsh_udev_to_usbv(h) ((h)->usbv)
  111. #define usbhsh_udev_is_used(h) usbhsh_udev_to_usbv(h)
  112. #define usbhsh_pipe_to_uep(p) ((p)->mod_private)
  113. #define usbhsh_device_parent(d) (usbhsh_usbv_to_udev((d)->usbv->parent))
  114. #define usbhsh_device_hubport(d) ((d)->usbv->portnum)
  115. #define usbhsh_device_number(h, d) ((int)((d) - (h)->udev))
  116. #define usbhsh_device_nth(h, d) ((h)->udev + d)
  117. #define usbhsh_device0(h) usbhsh_device_nth(h, 0)
  118. #define usbhsh_port_stat_init(h) ((h)->port_stat = 0)
  119. #define usbhsh_port_stat_set(h, s) ((h)->port_stat |= (s))
  120. #define usbhsh_port_stat_clear(h, s) ((h)->port_stat &= ~(s))
  121. #define usbhsh_port_stat_get(h) ((h)->port_stat)
  122. #define usbhsh_pkt_to_ureq(p) \
  123. container_of((void *)p, struct usbhsh_request, pkt)
  124. /*
  125. * req alloc/free
  126. */
  127. static struct usbhsh_request *usbhsh_ureq_alloc(struct usbhsh_hpriv *hpriv,
  128. struct urb *urb,
  129. gfp_t mem_flags)
  130. {
  131. struct usbhsh_request *ureq;
  132. ureq = kzalloc(sizeof(struct usbhsh_request), mem_flags);
  133. if (!ureq)
  134. return NULL;
  135. usbhs_pkt_init(&ureq->pkt);
  136. ureq->urb = urb;
  137. usbhsh_urb_to_ureq(urb) = ureq;
  138. return ureq;
  139. }
  140. static void usbhsh_ureq_free(struct usbhsh_hpriv *hpriv,
  141. struct usbhsh_request *ureq)
  142. {
  143. usbhsh_urb_to_ureq(ureq->urb) = NULL;
  144. ureq->urb = NULL;
  145. kfree(ureq);
  146. }
  147. /*
  148. * status
  149. */
  150. static int usbhsh_is_running(struct usbhsh_hpriv *hpriv)
  151. {
  152. /*
  153. * we can decide some device is attached or not
  154. * by checking mod.irq_attch
  155. * see
  156. * usbhsh_irq_attch()
  157. * usbhsh_irq_dtch()
  158. */
  159. return (hpriv->mod.irq_attch == NULL);
  160. }
  161. /*
  162. * pipe control
  163. */
  164. static void usbhsh_endpoint_sequence_save(struct usbhsh_hpriv *hpriv,
  165. struct urb *urb,
  166. struct usbhs_pkt *pkt)
  167. {
  168. int len = urb->actual_length;
  169. int maxp = usb_endpoint_maxp(&urb->ep->desc);
  170. int t = 0;
  171. /* DCP is out of sequence control */
  172. if (usb_pipecontrol(urb->pipe))
  173. return;
  174. /*
  175. * renesas_usbhs pipe has a limitation in a number.
  176. * So, driver should re-use the limited pipe for each device/endpoint.
  177. * DATA0/1 sequence should be saved for it.
  178. * see [image of mod_host]
  179. * [HARDWARE LIMITATION]
  180. */
  181. /*
  182. * next sequence depends on actual_length
  183. *
  184. * ex) actual_length = 1147, maxp = 512
  185. * data0 : 512
  186. * data1 : 512
  187. * data0 : 123
  188. * data1 is the next sequence
  189. */
  190. t = len / maxp;
  191. if (len % maxp)
  192. t++;
  193. if (pkt->zero)
  194. t++;
  195. t %= 2;
  196. if (t)
  197. usb_dotoggle(urb->dev,
  198. usb_pipeendpoint(urb->pipe),
  199. usb_pipeout(urb->pipe));
  200. }
  201. static struct usbhsh_device *usbhsh_device_get(struct usbhsh_hpriv *hpriv,
  202. struct urb *urb);
  203. static int usbhsh_pipe_attach(struct usbhsh_hpriv *hpriv,
  204. struct urb *urb)
  205. {
  206. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  207. struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep);
  208. struct usbhsh_device *udev = usbhsh_device_get(hpriv, urb);
  209. struct usbhs_pipe *pipe;
  210. struct usb_endpoint_descriptor *desc = &urb->ep->desc;
  211. struct device *dev = usbhs_priv_to_dev(priv);
  212. unsigned long flags;
  213. int dir_in_req = !!usb_pipein(urb->pipe);
  214. int is_dcp = usb_endpoint_xfer_control(desc);
  215. int i, dir_in;
  216. int ret = -EBUSY;
  217. /******************** spin lock ********************/
  218. usbhs_lock(priv, flags);
  219. /*
  220. * if uep has been attached to pipe,
  221. * reuse it
  222. */
  223. if (usbhsh_uep_to_pipe(uep)) {
  224. ret = 0;
  225. goto usbhsh_pipe_attach_done;
  226. }
  227. usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
  228. /* check pipe type */
  229. if (!usbhs_pipe_type_is(pipe, usb_endpoint_type(desc)))
  230. continue;
  231. /* check pipe direction if normal pipe */
  232. if (!is_dcp) {
  233. dir_in = !!usbhs_pipe_is_dir_in(pipe);
  234. if (0 != (dir_in - dir_in_req))
  235. continue;
  236. }
  237. /* check pipe is free */
  238. if (usbhsh_pipe_to_uep(pipe))
  239. continue;
  240. /*
  241. * attach pipe to uep
  242. *
  243. * usbhs_pipe_config_update() should be called after
  244. * usbhs_set_device_config()
  245. * see
  246. * DCPMAXP/PIPEMAXP
  247. */
  248. usbhsh_uep_to_pipe(uep) = pipe;
  249. usbhsh_pipe_to_uep(pipe) = uep;
  250. usbhs_pipe_config_update(pipe,
  251. usbhsh_device_number(hpriv, udev),
  252. usb_endpoint_num(desc),
  253. usb_endpoint_maxp(desc));
  254. dev_dbg(dev, "%s [%d-%d(%s:%s)]\n", __func__,
  255. usbhsh_device_number(hpriv, udev),
  256. usb_endpoint_num(desc),
  257. usbhs_pipe_name(pipe),
  258. dir_in_req ? "in" : "out");
  259. ret = 0;
  260. break;
  261. }
  262. usbhsh_pipe_attach_done:
  263. if (0 == ret)
  264. uep->counter++;
  265. usbhs_unlock(priv, flags);
  266. /******************** spin unlock ******************/
  267. return ret;
  268. }
  269. static void usbhsh_pipe_detach(struct usbhsh_hpriv *hpriv,
  270. struct usbhsh_ep *uep)
  271. {
  272. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  273. struct usbhs_pipe *pipe;
  274. struct device *dev = usbhs_priv_to_dev(priv);
  275. unsigned long flags;
  276. if (unlikely(!uep)) {
  277. dev_err(dev, "no uep\n");
  278. return;
  279. }
  280. /******************** spin lock ********************/
  281. usbhs_lock(priv, flags);
  282. pipe = usbhsh_uep_to_pipe(uep);
  283. if (unlikely(!pipe)) {
  284. dev_err(dev, "uep doens't have pipe\n");
  285. } else if (1 == uep->counter--) { /* last user */
  286. struct usb_host_endpoint *ep = usbhsh_uep_to_ep(uep);
  287. struct usbhsh_device *udev = usbhsh_uep_to_udev(uep);
  288. /* detach pipe from uep */
  289. usbhsh_uep_to_pipe(uep) = NULL;
  290. usbhsh_pipe_to_uep(pipe) = NULL;
  291. dev_dbg(dev, "%s [%d-%d(%s)]\n", __func__,
  292. usbhsh_device_number(hpriv, udev),
  293. usb_endpoint_num(&ep->desc),
  294. usbhs_pipe_name(pipe));
  295. }
  296. usbhs_unlock(priv, flags);
  297. /******************** spin unlock ******************/
  298. }
  299. /*
  300. * endpoint control
  301. */
  302. static int usbhsh_endpoint_attach(struct usbhsh_hpriv *hpriv,
  303. struct urb *urb,
  304. gfp_t mem_flags)
  305. {
  306. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  307. struct usbhsh_device *udev = usbhsh_device_get(hpriv, urb);
  308. struct usb_host_endpoint *ep = urb->ep;
  309. struct usbhsh_ep *uep;
  310. struct device *dev = usbhs_priv_to_dev(priv);
  311. struct usb_endpoint_descriptor *desc = &ep->desc;
  312. unsigned long flags;
  313. uep = kzalloc(sizeof(struct usbhsh_ep), mem_flags);
  314. if (!uep)
  315. return -ENOMEM;
  316. /******************** spin lock ********************/
  317. usbhs_lock(priv, flags);
  318. /*
  319. * init endpoint
  320. */
  321. uep->counter = 0;
  322. INIT_LIST_HEAD(&uep->ep_list);
  323. list_add_tail(&uep->ep_list, &udev->ep_list_head);
  324. usbhsh_uep_to_udev(uep) = udev;
  325. usbhsh_uep_to_ep(uep) = ep;
  326. usbhsh_ep_to_uep(ep) = uep;
  327. usbhs_unlock(priv, flags);
  328. /******************** spin unlock ******************/
  329. dev_dbg(dev, "%s [%d-%d]\n", __func__,
  330. usbhsh_device_number(hpriv, udev),
  331. usb_endpoint_num(desc));
  332. return 0;
  333. }
  334. static void usbhsh_endpoint_detach(struct usbhsh_hpriv *hpriv,
  335. struct usb_host_endpoint *ep)
  336. {
  337. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  338. struct device *dev = usbhs_priv_to_dev(priv);
  339. struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep);
  340. unsigned long flags;
  341. if (!uep)
  342. return;
  343. dev_dbg(dev, "%s [%d-%d]\n", __func__,
  344. usbhsh_device_number(hpriv, usbhsh_uep_to_udev(uep)),
  345. usb_endpoint_num(&ep->desc));
  346. if (usbhsh_uep_to_pipe(uep))
  347. usbhsh_pipe_detach(hpriv, uep);
  348. /******************** spin lock ********************/
  349. usbhs_lock(priv, flags);
  350. /* remove this endpoint from udev */
  351. list_del_init(&uep->ep_list);
  352. usbhsh_uep_to_udev(uep) = NULL;
  353. usbhsh_uep_to_ep(uep) = NULL;
  354. usbhsh_ep_to_uep(ep) = NULL;
  355. usbhs_unlock(priv, flags);
  356. /******************** spin unlock ******************/
  357. kfree(uep);
  358. }
  359. static void usbhsh_endpoint_detach_all(struct usbhsh_hpriv *hpriv,
  360. struct usbhsh_device *udev)
  361. {
  362. struct usbhsh_ep *uep, *next;
  363. list_for_each_entry_safe(uep, next, &udev->ep_list_head, ep_list)
  364. usbhsh_endpoint_detach(hpriv, usbhsh_uep_to_ep(uep));
  365. }
  366. /*
  367. * device control
  368. */
  369. static int usbhsh_connected_to_rhdev(struct usb_hcd *hcd,
  370. struct usbhsh_device *udev)
  371. {
  372. struct usb_device *usbv = usbhsh_udev_to_usbv(udev);
  373. return hcd->self.root_hub == usbv->parent;
  374. }
  375. static int usbhsh_device_has_endpoint(struct usbhsh_device *udev)
  376. {
  377. return !list_empty(&udev->ep_list_head);
  378. }
  379. static struct usbhsh_device *usbhsh_device_get(struct usbhsh_hpriv *hpriv,
  380. struct urb *urb)
  381. {
  382. struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
  383. struct usbhsh_device *udev = usbhsh_usbv_to_udev(usbv);
  384. /* usbhsh_device_attach() is still not called */
  385. if (!udev)
  386. return NULL;
  387. /* if it is device0, return it */
  388. if (0 == usb_pipedevice(urb->pipe))
  389. return usbhsh_device0(hpriv);
  390. /* return attached device */
  391. return udev;
  392. }
  393. static struct usbhsh_device *usbhsh_device_attach(struct usbhsh_hpriv *hpriv,
  394. struct urb *urb)
  395. {
  396. struct usbhsh_device *udev = NULL;
  397. struct usbhsh_device *udev0 = usbhsh_device0(hpriv);
  398. struct usbhsh_device *pos;
  399. struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
  400. struct device *dev = usbhsh_hcd_to_dev(hcd);
  401. struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
  402. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  403. unsigned long flags;
  404. u16 upphub, hubport;
  405. int i;
  406. /*
  407. * This function should be called only while urb is pointing to device0.
  408. * It will attach unused usbhsh_device to urb (usbv),
  409. * and initialize device0.
  410. * You can use usbhsh_device_get() to get "current" udev,
  411. * and usbhsh_usbv_to_udev() is for "attached" udev.
  412. */
  413. if (0 != usb_pipedevice(urb->pipe)) {
  414. dev_err(dev, "%s fail: urb isn't pointing device0\n", __func__);
  415. return NULL;
  416. }
  417. /******************** spin lock ********************/
  418. usbhs_lock(priv, flags);
  419. /*
  420. * find unused device
  421. */
  422. usbhsh_for_each_udev(pos, hpriv, i) {
  423. if (usbhsh_udev_is_used(pos))
  424. continue;
  425. udev = pos;
  426. break;
  427. }
  428. if (udev) {
  429. /*
  430. * usbhsh_usbv_to_udev()
  431. * usbhsh_udev_to_usbv()
  432. * will be enable
  433. */
  434. dev_set_drvdata(&usbv->dev, udev);
  435. udev->usbv = usbv;
  436. }
  437. usbhs_unlock(priv, flags);
  438. /******************** spin unlock ******************/
  439. if (!udev) {
  440. dev_err(dev, "no free usbhsh_device\n");
  441. return NULL;
  442. }
  443. if (usbhsh_device_has_endpoint(udev)) {
  444. dev_warn(dev, "udev have old endpoint\n");
  445. usbhsh_endpoint_detach_all(hpriv, udev);
  446. }
  447. if (usbhsh_device_has_endpoint(udev0)) {
  448. dev_warn(dev, "udev0 have old endpoint\n");
  449. usbhsh_endpoint_detach_all(hpriv, udev0);
  450. }
  451. /* uep will be attached */
  452. INIT_LIST_HEAD(&udev0->ep_list_head);
  453. INIT_LIST_HEAD(&udev->ep_list_head);
  454. /*
  455. * set device0 config
  456. */
  457. usbhs_set_device_config(priv,
  458. 0, 0, 0, usbv->speed);
  459. /*
  460. * set new device config
  461. */
  462. upphub = 0;
  463. hubport = 0;
  464. if (!usbhsh_connected_to_rhdev(hcd, udev)) {
  465. /* if udev is not connected to rhdev, it means parent is Hub */
  466. struct usbhsh_device *parent = usbhsh_device_parent(udev);
  467. upphub = usbhsh_device_number(hpriv, parent);
  468. hubport = usbhsh_device_hubport(udev);
  469. dev_dbg(dev, "%s connected to Hub [%d:%d](%p)\n", __func__,
  470. upphub, hubport, parent);
  471. }
  472. usbhs_set_device_config(priv,
  473. usbhsh_device_number(hpriv, udev),
  474. upphub, hubport, usbv->speed);
  475. dev_dbg(dev, "%s [%d](%p)\n", __func__,
  476. usbhsh_device_number(hpriv, udev), udev);
  477. return udev;
  478. }
  479. static void usbhsh_device_detach(struct usbhsh_hpriv *hpriv,
  480. struct usbhsh_device *udev)
  481. {
  482. struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
  483. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  484. struct device *dev = usbhsh_hcd_to_dev(hcd);
  485. struct usb_device *usbv = usbhsh_udev_to_usbv(udev);
  486. unsigned long flags;
  487. dev_dbg(dev, "%s [%d](%p)\n", __func__,
  488. usbhsh_device_number(hpriv, udev), udev);
  489. if (usbhsh_device_has_endpoint(udev)) {
  490. dev_warn(dev, "udev still have endpoint\n");
  491. usbhsh_endpoint_detach_all(hpriv, udev);
  492. }
  493. /*
  494. * There is nothing to do if it is device0.
  495. * see
  496. * usbhsh_device_attach()
  497. * usbhsh_device_get()
  498. */
  499. if (0 == usbhsh_device_number(hpriv, udev))
  500. return;
  501. /******************** spin lock ********************/
  502. usbhs_lock(priv, flags);
  503. /*
  504. * usbhsh_usbv_to_udev()
  505. * usbhsh_udev_to_usbv()
  506. * will be disable
  507. */
  508. dev_set_drvdata(&usbv->dev, NULL);
  509. udev->usbv = NULL;
  510. usbhs_unlock(priv, flags);
  511. /******************** spin unlock ******************/
  512. }
  513. /*
  514. * queue push/pop
  515. */
  516. static void usbhsh_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt)
  517. {
  518. struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt);
  519. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  520. struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
  521. struct urb *urb = ureq->urb;
  522. struct device *dev = usbhs_priv_to_dev(priv);
  523. int status = 0;
  524. dev_dbg(dev, "%s\n", __func__);
  525. if (!urb) {
  526. dev_warn(dev, "pkt doesn't have urb\n");
  527. return;
  528. }
  529. if (!usbhsh_is_running(hpriv))
  530. status = -ESHUTDOWN;
  531. urb->actual_length = pkt->actual;
  532. usbhsh_endpoint_sequence_save(hpriv, urb, pkt);
  533. usbhsh_ureq_free(hpriv, ureq);
  534. usbhsh_pipe_detach(hpriv, usbhsh_ep_to_uep(urb->ep));
  535. usb_hcd_unlink_urb_from_ep(hcd, urb);
  536. usb_hcd_giveback_urb(hcd, urb, status);
  537. }
  538. static int usbhsh_queue_push(struct usb_hcd *hcd,
  539. struct urb *urb,
  540. gfp_t mem_flags)
  541. {
  542. struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
  543. struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep);
  544. struct usbhs_pipe *pipe = usbhsh_uep_to_pipe(uep);
  545. struct device *dev = usbhsh_hcd_to_dev(hcd);
  546. struct usbhsh_request *ureq;
  547. void *buf;
  548. int len, sequence;
  549. if (usb_pipeisoc(urb->pipe)) {
  550. dev_err(dev, "pipe iso is not supported now\n");
  551. return -EIO;
  552. }
  553. /* this ureq will be freed on usbhsh_queue_done() */
  554. ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
  555. if (unlikely(!ureq)) {
  556. dev_err(dev, "ureq alloc fail\n");
  557. return -ENOMEM;
  558. }
  559. if (usb_pipein(urb->pipe))
  560. pipe->handler = &usbhs_fifo_dma_pop_handler;
  561. else
  562. pipe->handler = &usbhs_fifo_dma_push_handler;
  563. buf = (void *)(urb->transfer_buffer + urb->actual_length);
  564. len = urb->transfer_buffer_length - urb->actual_length;
  565. sequence = usb_gettoggle(urb->dev,
  566. usb_pipeendpoint(urb->pipe),
  567. usb_pipeout(urb->pipe));
  568. dev_dbg(dev, "%s\n", __func__);
  569. usbhs_pkt_push(pipe, &ureq->pkt, usbhsh_queue_done,
  570. buf, len, (urb->transfer_flags & URB_ZERO_PACKET),
  571. sequence);
  572. usbhs_pkt_start(pipe);
  573. return 0;
  574. }
  575. static void usbhsh_queue_force_pop(struct usbhs_priv *priv,
  576. struct usbhs_pipe *pipe)
  577. {
  578. struct usbhs_pkt *pkt;
  579. while (1) {
  580. pkt = usbhs_pkt_pop(pipe, NULL);
  581. if (!pkt)
  582. break;
  583. /*
  584. * if all packet are gone, usbhsh_endpoint_disable()
  585. * will be called.
  586. * then, attached device/endpoint/pipe will be detached
  587. */
  588. usbhsh_queue_done(priv, pkt);
  589. }
  590. }
  591. static void usbhsh_queue_force_pop_all(struct usbhs_priv *priv)
  592. {
  593. struct usbhs_pipe *pos;
  594. int i;
  595. usbhs_for_each_pipe_with_dcp(pos, priv, i)
  596. usbhsh_queue_force_pop(priv, pos);
  597. }
  598. /*
  599. * DCP setup stage
  600. */
  601. static int usbhsh_is_request_address(struct urb *urb)
  602. {
  603. struct usb_ctrlrequest *req;
  604. req = (struct usb_ctrlrequest *)urb->setup_packet;
  605. if ((DeviceOutRequest == req->bRequestType << 8) &&
  606. (USB_REQ_SET_ADDRESS == req->bRequest))
  607. return 1;
  608. else
  609. return 0;
  610. }
  611. static void usbhsh_setup_stage_packet_push(struct usbhsh_hpriv *hpriv,
  612. struct urb *urb,
  613. struct usbhs_pipe *pipe)
  614. {
  615. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  616. struct usb_ctrlrequest req;
  617. struct device *dev = usbhs_priv_to_dev(priv);
  618. /*
  619. * wait setup packet ACK
  620. * see
  621. * usbhsh_irq_setup_ack()
  622. * usbhsh_irq_setup_err()
  623. */
  624. init_completion(&hpriv->setup_ack_done);
  625. /* copy original request */
  626. memcpy(&req, urb->setup_packet, sizeof(struct usb_ctrlrequest));
  627. /*
  628. * renesas_usbhs can not use original usb address.
  629. * see HARDWARE LIMITATION.
  630. * modify usb address here to use attached device.
  631. * see usbhsh_device_attach()
  632. */
  633. if (usbhsh_is_request_address(urb)) {
  634. struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
  635. struct usbhsh_device *udev = usbhsh_usbv_to_udev(usbv);
  636. /* udev is a attached device */
  637. req.wValue = usbhsh_device_number(hpriv, udev);
  638. dev_dbg(dev, "create new address - %d\n", req.wValue);
  639. }
  640. /* set request */
  641. usbhs_usbreq_set_val(priv, &req);
  642. /*
  643. * wait setup packet ACK
  644. */
  645. wait_for_completion(&hpriv->setup_ack_done);
  646. dev_dbg(dev, "%s done\n", __func__);
  647. }
  648. /*
  649. * DCP data stage
  650. */
  651. static void usbhsh_data_stage_packet_done(struct usbhs_priv *priv,
  652. struct usbhs_pkt *pkt)
  653. {
  654. struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt);
  655. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  656. /* this ureq was connected to urb when usbhsh_urb_enqueue() */
  657. usbhsh_ureq_free(hpriv, ureq);
  658. }
  659. static int usbhsh_data_stage_packet_push(struct usbhsh_hpriv *hpriv,
  660. struct urb *urb,
  661. struct usbhs_pipe *pipe,
  662. gfp_t mem_flags)
  663. {
  664. struct usbhsh_request *ureq;
  665. /* this ureq will be freed on usbhsh_data_stage_packet_done() */
  666. ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
  667. if (unlikely(!ureq))
  668. return -ENOMEM;
  669. if (usb_pipein(urb->pipe))
  670. pipe->handler = &usbhs_dcp_data_stage_in_handler;
  671. else
  672. pipe->handler = &usbhs_dcp_data_stage_out_handler;
  673. usbhs_pkt_push(pipe, &ureq->pkt,
  674. usbhsh_data_stage_packet_done,
  675. urb->transfer_buffer,
  676. urb->transfer_buffer_length,
  677. (urb->transfer_flags & URB_ZERO_PACKET),
  678. -1);
  679. return 0;
  680. }
  681. /*
  682. * DCP status stage
  683. */
  684. static int usbhsh_status_stage_packet_push(struct usbhsh_hpriv *hpriv,
  685. struct urb *urb,
  686. struct usbhs_pipe *pipe,
  687. gfp_t mem_flags)
  688. {
  689. struct usbhsh_request *ureq;
  690. /* This ureq will be freed on usbhsh_queue_done() */
  691. ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
  692. if (unlikely(!ureq))
  693. return -ENOMEM;
  694. if (usb_pipein(urb->pipe))
  695. pipe->handler = &usbhs_dcp_status_stage_in_handler;
  696. else
  697. pipe->handler = &usbhs_dcp_status_stage_out_handler;
  698. usbhs_pkt_push(pipe, &ureq->pkt,
  699. usbhsh_queue_done,
  700. NULL,
  701. urb->transfer_buffer_length,
  702. 0, -1);
  703. return 0;
  704. }
  705. static int usbhsh_dcp_queue_push(struct usb_hcd *hcd,
  706. struct urb *urb,
  707. gfp_t mflags)
  708. {
  709. struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
  710. struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep);
  711. struct usbhs_pipe *pipe = usbhsh_uep_to_pipe(uep);
  712. struct device *dev = usbhsh_hcd_to_dev(hcd);
  713. int ret;
  714. dev_dbg(dev, "%s\n", __func__);
  715. /*
  716. * setup stage
  717. *
  718. * usbhsh_send_setup_stage_packet() wait SACK/SIGN
  719. */
  720. usbhsh_setup_stage_packet_push(hpriv, urb, pipe);
  721. /*
  722. * data stage
  723. *
  724. * It is pushed only when urb has buffer.
  725. */
  726. if (urb->transfer_buffer_length) {
  727. ret = usbhsh_data_stage_packet_push(hpriv, urb, pipe, mflags);
  728. if (ret < 0) {
  729. dev_err(dev, "data stage failed\n");
  730. return ret;
  731. }
  732. }
  733. /*
  734. * status stage
  735. */
  736. ret = usbhsh_status_stage_packet_push(hpriv, urb, pipe, mflags);
  737. if (ret < 0) {
  738. dev_err(dev, "status stage failed\n");
  739. return ret;
  740. }
  741. /*
  742. * start pushed packets
  743. */
  744. usbhs_pkt_start(pipe);
  745. return 0;
  746. }
  747. /*
  748. * dma map functions
  749. */
  750. static int usbhsh_dma_map_ctrl(struct device *dma_dev, struct usbhs_pkt *pkt,
  751. int map)
  752. {
  753. if (map) {
  754. struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt);
  755. struct urb *urb = ureq->urb;
  756. /* it can not use scatter/gather */
  757. if (urb->num_sgs)
  758. return -EINVAL;
  759. pkt->dma = urb->transfer_dma;
  760. if (!pkt->dma)
  761. return -EINVAL;
  762. }
  763. return 0;
  764. }
  765. /*
  766. * for hc_driver
  767. */
  768. static int usbhsh_host_start(struct usb_hcd *hcd)
  769. {
  770. return 0;
  771. }
  772. static void usbhsh_host_stop(struct usb_hcd *hcd)
  773. {
  774. }
  775. static int usbhsh_urb_enqueue(struct usb_hcd *hcd,
  776. struct urb *urb,
  777. gfp_t mem_flags)
  778. {
  779. struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
  780. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  781. struct device *dev = usbhs_priv_to_dev(priv);
  782. struct usb_host_endpoint *ep = urb->ep;
  783. struct usbhsh_device *new_udev = NULL;
  784. int is_dir_in = usb_pipein(urb->pipe);
  785. int ret;
  786. dev_dbg(dev, "%s (%s)\n", __func__, is_dir_in ? "in" : "out");
  787. if (!usbhsh_is_running(hpriv)) {
  788. ret = -EIO;
  789. dev_err(dev, "host is not running\n");
  790. goto usbhsh_urb_enqueue_error_not_linked;
  791. }
  792. ret = usb_hcd_link_urb_to_ep(hcd, urb);
  793. if (ret) {
  794. dev_err(dev, "urb link failed\n");
  795. goto usbhsh_urb_enqueue_error_not_linked;
  796. }
  797. /*
  798. * attach udev if needed
  799. * see [image of mod_host]
  800. */
  801. if (!usbhsh_device_get(hpriv, urb)) {
  802. new_udev = usbhsh_device_attach(hpriv, urb);
  803. if (!new_udev) {
  804. ret = -EIO;
  805. dev_err(dev, "device attach failed\n");
  806. goto usbhsh_urb_enqueue_error_not_linked;
  807. }
  808. }
  809. /*
  810. * attach endpoint if needed
  811. * see [image of mod_host]
  812. */
  813. if (!usbhsh_ep_to_uep(ep)) {
  814. ret = usbhsh_endpoint_attach(hpriv, urb, mem_flags);
  815. if (ret < 0) {
  816. dev_err(dev, "endpoint attach failed\n");
  817. goto usbhsh_urb_enqueue_error_free_device;
  818. }
  819. }
  820. /*
  821. * attach pipe to endpoint
  822. * see [image of mod_host]
  823. */
  824. ret = usbhsh_pipe_attach(hpriv, urb);
  825. if (ret < 0) {
  826. dev_err(dev, "pipe attach failed\n");
  827. goto usbhsh_urb_enqueue_error_free_endpoint;
  828. }
  829. /*
  830. * push packet
  831. */
  832. if (usb_pipecontrol(urb->pipe))
  833. ret = usbhsh_dcp_queue_push(hcd, urb, mem_flags);
  834. else
  835. ret = usbhsh_queue_push(hcd, urb, mem_flags);
  836. return ret;
  837. usbhsh_urb_enqueue_error_free_endpoint:
  838. usbhsh_endpoint_detach(hpriv, ep);
  839. usbhsh_urb_enqueue_error_free_device:
  840. if (new_udev)
  841. usbhsh_device_detach(hpriv, new_udev);
  842. usbhsh_urb_enqueue_error_not_linked:
  843. dev_dbg(dev, "%s error\n", __func__);
  844. return ret;
  845. }
  846. static int usbhsh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  847. {
  848. struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
  849. struct usbhsh_request *ureq = usbhsh_urb_to_ureq(urb);
  850. if (ureq) {
  851. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  852. struct usbhs_pkt *pkt = &ureq->pkt;
  853. usbhs_pkt_pop(pkt->pipe, pkt);
  854. usbhsh_queue_done(priv, pkt);
  855. }
  856. return 0;
  857. }
  858. static void usbhsh_endpoint_disable(struct usb_hcd *hcd,
  859. struct usb_host_endpoint *ep)
  860. {
  861. struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep);
  862. struct usbhsh_device *udev;
  863. struct usbhsh_hpriv *hpriv;
  864. /*
  865. * this function might be called manytimes by same hcd/ep
  866. * in-endpoint == out-endpoint if ep == dcp.
  867. */
  868. if (!uep)
  869. return;
  870. udev = usbhsh_uep_to_udev(uep);
  871. hpriv = usbhsh_hcd_to_hpriv(hcd);
  872. usbhsh_endpoint_detach(hpriv, ep);
  873. /*
  874. * if there is no endpoint,
  875. * free device
  876. */
  877. if (!usbhsh_device_has_endpoint(udev))
  878. usbhsh_device_detach(hpriv, udev);
  879. }
  880. static int usbhsh_hub_status_data(struct usb_hcd *hcd, char *buf)
  881. {
  882. struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
  883. int roothub_id = 1; /* only 1 root hub */
  884. /*
  885. * does port stat was changed ?
  886. * check USB_PORT_STAT_C_xxx << 16
  887. */
  888. if (usbhsh_port_stat_get(hpriv) & 0xFFFF0000)
  889. *buf = (1 << roothub_id);
  890. else
  891. *buf = 0;
  892. return !!(*buf);
  893. }
  894. static int __usbhsh_hub_hub_feature(struct usbhsh_hpriv *hpriv,
  895. u16 typeReq, u16 wValue,
  896. u16 wIndex, char *buf, u16 wLength)
  897. {
  898. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  899. struct device *dev = usbhs_priv_to_dev(priv);
  900. switch (wValue) {
  901. case C_HUB_OVER_CURRENT:
  902. case C_HUB_LOCAL_POWER:
  903. dev_dbg(dev, "%s :: C_HUB_xx\n", __func__);
  904. return 0;
  905. }
  906. return -EPIPE;
  907. }
  908. static int __usbhsh_hub_port_feature(struct usbhsh_hpriv *hpriv,
  909. u16 typeReq, u16 wValue,
  910. u16 wIndex, char *buf, u16 wLength)
  911. {
  912. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  913. struct device *dev = usbhs_priv_to_dev(priv);
  914. int enable = (typeReq == SetPortFeature);
  915. int speed, i, timeout = 128;
  916. int roothub_id = 1; /* only 1 root hub */
  917. /* common error */
  918. if (wIndex > roothub_id || wLength != 0)
  919. return -EPIPE;
  920. /* check wValue */
  921. switch (wValue) {
  922. case USB_PORT_FEAT_POWER:
  923. usbhs_vbus_ctrl(priv, enable);
  924. dev_dbg(dev, "%s :: USB_PORT_FEAT_POWER\n", __func__);
  925. break;
  926. case USB_PORT_FEAT_ENABLE:
  927. case USB_PORT_FEAT_SUSPEND:
  928. case USB_PORT_FEAT_C_ENABLE:
  929. case USB_PORT_FEAT_C_SUSPEND:
  930. case USB_PORT_FEAT_C_CONNECTION:
  931. case USB_PORT_FEAT_C_OVER_CURRENT:
  932. case USB_PORT_FEAT_C_RESET:
  933. dev_dbg(dev, "%s :: USB_PORT_FEAT_xxx\n", __func__);
  934. break;
  935. case USB_PORT_FEAT_RESET:
  936. if (!enable)
  937. break;
  938. usbhsh_port_stat_clear(hpriv,
  939. USB_PORT_STAT_HIGH_SPEED |
  940. USB_PORT_STAT_LOW_SPEED);
  941. usbhsh_queue_force_pop_all(priv);
  942. usbhs_bus_send_reset(priv);
  943. msleep(20);
  944. usbhs_bus_send_sof_enable(priv);
  945. for (i = 0; i < timeout ; i++) {
  946. switch (usbhs_bus_get_speed(priv)) {
  947. case USB_SPEED_LOW:
  948. speed = USB_PORT_STAT_LOW_SPEED;
  949. goto got_usb_bus_speed;
  950. case USB_SPEED_HIGH:
  951. speed = USB_PORT_STAT_HIGH_SPEED;
  952. goto got_usb_bus_speed;
  953. case USB_SPEED_FULL:
  954. speed = 0;
  955. goto got_usb_bus_speed;
  956. }
  957. msleep(20);
  958. }
  959. return -EPIPE;
  960. got_usb_bus_speed:
  961. usbhsh_port_stat_set(hpriv, speed);
  962. usbhsh_port_stat_set(hpriv, USB_PORT_STAT_ENABLE);
  963. dev_dbg(dev, "%s :: USB_PORT_FEAT_RESET (speed = %d)\n",
  964. __func__, speed);
  965. /* status change is not needed */
  966. return 0;
  967. default:
  968. return -EPIPE;
  969. }
  970. /* set/clear status */
  971. if (enable)
  972. usbhsh_port_stat_set(hpriv, (1 << wValue));
  973. else
  974. usbhsh_port_stat_clear(hpriv, (1 << wValue));
  975. return 0;
  976. }
  977. static int __usbhsh_hub_get_status(struct usbhsh_hpriv *hpriv,
  978. u16 typeReq, u16 wValue,
  979. u16 wIndex, char *buf, u16 wLength)
  980. {
  981. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  982. struct usb_hub_descriptor *desc = (struct usb_hub_descriptor *)buf;
  983. struct device *dev = usbhs_priv_to_dev(priv);
  984. int roothub_id = 1; /* only 1 root hub */
  985. switch (typeReq) {
  986. case GetHubStatus:
  987. dev_dbg(dev, "%s :: GetHubStatus\n", __func__);
  988. *buf = 0x00;
  989. break;
  990. case GetPortStatus:
  991. if (wIndex != roothub_id)
  992. return -EPIPE;
  993. dev_dbg(dev, "%s :: GetPortStatus\n", __func__);
  994. *(__le32 *)buf = cpu_to_le32(usbhsh_port_stat_get(hpriv));
  995. break;
  996. case GetHubDescriptor:
  997. desc->bDescriptorType = USB_DT_HUB;
  998. desc->bHubContrCurrent = 0;
  999. desc->bNbrPorts = roothub_id;
  1000. desc->bDescLength = 9;
  1001. desc->bPwrOn2PwrGood = 0;
  1002. desc->wHubCharacteristics =
  1003. cpu_to_le16(HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_NO_OCPM);
  1004. desc->u.hs.DeviceRemovable[0] = (roothub_id << 1);
  1005. desc->u.hs.DeviceRemovable[1] = ~0;
  1006. dev_dbg(dev, "%s :: GetHubDescriptor\n", __func__);
  1007. break;
  1008. }
  1009. return 0;
  1010. }
  1011. static int usbhsh_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  1012. u16 wIndex, char *buf, u16 wLength)
  1013. {
  1014. struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
  1015. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  1016. struct device *dev = usbhs_priv_to_dev(priv);
  1017. int ret = -EPIPE;
  1018. switch (typeReq) {
  1019. /* Hub Feature */
  1020. case ClearHubFeature:
  1021. case SetHubFeature:
  1022. ret = __usbhsh_hub_hub_feature(hpriv, typeReq,
  1023. wValue, wIndex, buf, wLength);
  1024. break;
  1025. /* Port Feature */
  1026. case SetPortFeature:
  1027. case ClearPortFeature:
  1028. ret = __usbhsh_hub_port_feature(hpriv, typeReq,
  1029. wValue, wIndex, buf, wLength);
  1030. break;
  1031. /* Get status */
  1032. case GetHubStatus:
  1033. case GetPortStatus:
  1034. case GetHubDescriptor:
  1035. ret = __usbhsh_hub_get_status(hpriv, typeReq,
  1036. wValue, wIndex, buf, wLength);
  1037. break;
  1038. }
  1039. dev_dbg(dev, "typeReq = %x, ret = %d, port_stat = %x\n",
  1040. typeReq, ret, usbhsh_port_stat_get(hpriv));
  1041. return ret;
  1042. }
  1043. static int usbhsh_bus_nop(struct usb_hcd *hcd)
  1044. {
  1045. /* nothing to do */
  1046. return 0;
  1047. }
  1048. static const struct hc_driver usbhsh_driver = {
  1049. .description = usbhsh_hcd_name,
  1050. .hcd_priv_size = sizeof(struct usbhsh_hpriv),
  1051. /*
  1052. * generic hardware linkage
  1053. */
  1054. .flags = HCD_USB2,
  1055. .start = usbhsh_host_start,
  1056. .stop = usbhsh_host_stop,
  1057. /*
  1058. * managing i/o requests and associated device resources
  1059. */
  1060. .urb_enqueue = usbhsh_urb_enqueue,
  1061. .urb_dequeue = usbhsh_urb_dequeue,
  1062. .endpoint_disable = usbhsh_endpoint_disable,
  1063. /*
  1064. * root hub
  1065. */
  1066. .hub_status_data = usbhsh_hub_status_data,
  1067. .hub_control = usbhsh_hub_control,
  1068. .bus_suspend = usbhsh_bus_nop,
  1069. .bus_resume = usbhsh_bus_nop,
  1070. };
  1071. /*
  1072. * interrupt functions
  1073. */
  1074. static int usbhsh_irq_attch(struct usbhs_priv *priv,
  1075. struct usbhs_irq_state *irq_state)
  1076. {
  1077. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  1078. struct device *dev = usbhs_priv_to_dev(priv);
  1079. dev_dbg(dev, "device attached\n");
  1080. usbhsh_port_stat_set(hpriv, USB_PORT_STAT_CONNECTION);
  1081. usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16);
  1082. /*
  1083. * attch interrupt might happen infinitely on some device
  1084. * (on self power USB hub ?)
  1085. * disable it here.
  1086. *
  1087. * usbhsh_is_running() becomes effective
  1088. * according to this process.
  1089. * see
  1090. * usbhsh_is_running()
  1091. * usbhsh_urb_enqueue()
  1092. */
  1093. hpriv->mod.irq_attch = NULL;
  1094. usbhs_irq_callback_update(priv, &hpriv->mod);
  1095. return 0;
  1096. }
  1097. static int usbhsh_irq_dtch(struct usbhs_priv *priv,
  1098. struct usbhs_irq_state *irq_state)
  1099. {
  1100. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  1101. struct device *dev = usbhs_priv_to_dev(priv);
  1102. dev_dbg(dev, "device detached\n");
  1103. usbhsh_port_stat_clear(hpriv, USB_PORT_STAT_CONNECTION);
  1104. usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16);
  1105. /*
  1106. * enable attch interrupt again
  1107. *
  1108. * usbhsh_is_running() becomes invalid
  1109. * according to this process.
  1110. * see
  1111. * usbhsh_is_running()
  1112. * usbhsh_urb_enqueue()
  1113. */
  1114. hpriv->mod.irq_attch = usbhsh_irq_attch;
  1115. usbhs_irq_callback_update(priv, &hpriv->mod);
  1116. /*
  1117. * usbhsh_queue_force_pop_all() should be called
  1118. * after usbhsh_is_running() becomes invalid.
  1119. */
  1120. usbhsh_queue_force_pop_all(priv);
  1121. return 0;
  1122. }
  1123. static int usbhsh_irq_setup_ack(struct usbhs_priv *priv,
  1124. struct usbhs_irq_state *irq_state)
  1125. {
  1126. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  1127. struct device *dev = usbhs_priv_to_dev(priv);
  1128. dev_dbg(dev, "setup packet OK\n");
  1129. complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */
  1130. return 0;
  1131. }
  1132. static int usbhsh_irq_setup_err(struct usbhs_priv *priv,
  1133. struct usbhs_irq_state *irq_state)
  1134. {
  1135. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  1136. struct device *dev = usbhs_priv_to_dev(priv);
  1137. dev_dbg(dev, "setup packet Err\n");
  1138. complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */
  1139. return 0;
  1140. }
  1141. /*
  1142. * module start/stop
  1143. */
  1144. static void usbhsh_pipe_init_for_host(struct usbhs_priv *priv)
  1145. {
  1146. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  1147. struct usbhs_pipe *pipe;
  1148. struct renesas_usbhs_driver_pipe_config *pipe_configs =
  1149. usbhs_get_dparam(priv, pipe_configs);
  1150. int pipe_size = usbhs_get_dparam(priv, pipe_size);
  1151. int old_type, dir_in, i;
  1152. /* init all pipe */
  1153. old_type = USB_ENDPOINT_XFER_CONTROL;
  1154. for (i = 0; i < pipe_size; i++) {
  1155. /*
  1156. * data "output" will be finished as soon as possible,
  1157. * but there is no guaranty at data "input" case.
  1158. *
  1159. * "input" needs "standby" pipe.
  1160. * So, "input" direction pipe > "output" direction pipe
  1161. * is good idea.
  1162. *
  1163. * 1st USB_ENDPOINT_XFER_xxx will be output direction,
  1164. * and the other will be input direction here.
  1165. *
  1166. * ex)
  1167. * ...
  1168. * USB_ENDPOINT_XFER_ISOC -> dir out
  1169. * USB_ENDPOINT_XFER_ISOC -> dir in
  1170. * USB_ENDPOINT_XFER_BULK -> dir out
  1171. * USB_ENDPOINT_XFER_BULK -> dir in
  1172. * USB_ENDPOINT_XFER_BULK -> dir in
  1173. * ...
  1174. */
  1175. dir_in = (pipe_configs[i].type == old_type);
  1176. old_type = pipe_configs[i].type;
  1177. if (USB_ENDPOINT_XFER_CONTROL == pipe_configs[i].type) {
  1178. pipe = usbhs_dcp_malloc(priv);
  1179. usbhsh_hpriv_to_dcp(hpriv) = pipe;
  1180. } else {
  1181. pipe = usbhs_pipe_malloc(priv,
  1182. pipe_configs[i].type,
  1183. dir_in);
  1184. }
  1185. pipe->mod_private = NULL;
  1186. }
  1187. }
  1188. static int usbhsh_start(struct usbhs_priv *priv)
  1189. {
  1190. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  1191. struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
  1192. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  1193. struct device *dev = usbhs_priv_to_dev(priv);
  1194. int ret;
  1195. /* add hcd */
  1196. ret = usb_add_hcd(hcd, 0, 0);
  1197. if (ret < 0)
  1198. return 0;
  1199. device_wakeup_enable(hcd->self.controller);
  1200. /*
  1201. * pipe initialize and enable DCP
  1202. */
  1203. usbhs_fifo_init(priv);
  1204. usbhs_pipe_init(priv,
  1205. usbhsh_dma_map_ctrl);
  1206. usbhsh_pipe_init_for_host(priv);
  1207. /*
  1208. * system config enble
  1209. * - HI speed
  1210. * - host
  1211. * - usb module
  1212. */
  1213. usbhs_sys_host_ctrl(priv, 1);
  1214. /*
  1215. * enable irq callback
  1216. */
  1217. mod->irq_attch = usbhsh_irq_attch;
  1218. mod->irq_dtch = usbhsh_irq_dtch;
  1219. mod->irq_sack = usbhsh_irq_setup_ack;
  1220. mod->irq_sign = usbhsh_irq_setup_err;
  1221. usbhs_irq_callback_update(priv, mod);
  1222. dev_dbg(dev, "start host\n");
  1223. return ret;
  1224. }
  1225. static int usbhsh_stop(struct usbhs_priv *priv)
  1226. {
  1227. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  1228. struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
  1229. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  1230. struct device *dev = usbhs_priv_to_dev(priv);
  1231. /*
  1232. * disable irq callback
  1233. */
  1234. mod->irq_attch = NULL;
  1235. mod->irq_dtch = NULL;
  1236. mod->irq_sack = NULL;
  1237. mod->irq_sign = NULL;
  1238. usbhs_irq_callback_update(priv, mod);
  1239. usb_remove_hcd(hcd);
  1240. /* disable sys */
  1241. usbhs_sys_host_ctrl(priv, 0);
  1242. dev_dbg(dev, "quit host\n");
  1243. return 0;
  1244. }
  1245. int usbhs_mod_host_probe(struct usbhs_priv *priv)
  1246. {
  1247. struct usbhsh_hpriv *hpriv;
  1248. struct usb_hcd *hcd;
  1249. struct usbhsh_device *udev;
  1250. struct device *dev = usbhs_priv_to_dev(priv);
  1251. int i;
  1252. /* initialize hcd */
  1253. hcd = usb_create_hcd(&usbhsh_driver, dev, usbhsh_hcd_name);
  1254. if (!hcd) {
  1255. dev_err(dev, "Failed to create hcd\n");
  1256. return -ENOMEM;
  1257. }
  1258. hcd->has_tt = 1; /* for low/full speed */
  1259. /*
  1260. * CAUTION
  1261. *
  1262. * There is no guarantee that it is possible to access usb module here.
  1263. * Don't accesses to it.
  1264. * The accesse will be enable after "usbhsh_start"
  1265. */
  1266. hpriv = usbhsh_hcd_to_hpriv(hcd);
  1267. /*
  1268. * register itself
  1269. */
  1270. usbhs_mod_register(priv, &hpriv->mod, USBHS_HOST);
  1271. /* init hpriv */
  1272. hpriv->mod.name = "host";
  1273. hpriv->mod.start = usbhsh_start;
  1274. hpriv->mod.stop = usbhsh_stop;
  1275. usbhsh_port_stat_init(hpriv);
  1276. /* init all device */
  1277. usbhsh_for_each_udev_with_dev0(udev, hpriv, i) {
  1278. udev->usbv = NULL;
  1279. INIT_LIST_HEAD(&udev->ep_list_head);
  1280. }
  1281. dev_info(dev, "host probed\n");
  1282. return 0;
  1283. }
  1284. int usbhs_mod_host_remove(struct usbhs_priv *priv)
  1285. {
  1286. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  1287. struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
  1288. usb_put_hcd(hcd);
  1289. return 0;
  1290. }