mod_host.c 31 KB

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