mod_host.c 31 KB

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