mod_host.c 31 KB

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