mod_host.c 31 KB

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