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