mod_host.c 37 KB

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