mod_host.c 31 KB

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