mod_host.c 34 KB

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