mod_host.c 31 KB

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