mod_host.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480
  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. * +--------+ | | other device requested
  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. * @ : uep requested free pipe, but all have been used.
  63. * now it is waiting for free pipe
  64. */
  65. /*
  66. * struct
  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; /* attached pipe */
  78. struct usbhsh_device *udev; /* attached udev */
  79. struct usb_host_endpoint *ep;
  80. struct list_head ep_list; /* list to usbhsh_device */
  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. u32 port_stat; /* USB_PORT_STAT_xxx */
  89. struct completion setup_ack_done;
  90. };
  91. static const char usbhsh_hcd_name[] = "renesas_usbhs host";
  92. /*
  93. * macro
  94. */
  95. #define usbhsh_priv_to_hpriv(priv) \
  96. container_of(usbhs_mod_get(priv, USBHS_HOST), struct usbhsh_hpriv, mod)
  97. #define __usbhsh_for_each_udev(start, pos, h, i) \
  98. for (i = start, pos = (h)->udev + i; \
  99. i < USBHSH_DEVICE_MAX; \
  100. i++, pos = (h)->udev + i)
  101. #define usbhsh_for_each_udev(pos, hpriv, i) \
  102. __usbhsh_for_each_udev(1, pos, hpriv, i)
  103. #define usbhsh_for_each_udev_with_dev0(pos, hpriv, i) \
  104. __usbhsh_for_each_udev(0, pos, hpriv, i)
  105. #define usbhsh_hcd_to_hpriv(h) (struct usbhsh_hpriv *)((h)->hcd_priv)
  106. #define usbhsh_hcd_to_dev(h) ((h)->self.controller)
  107. #define usbhsh_hpriv_to_priv(h) ((h)->mod.priv)
  108. #define usbhsh_hpriv_to_dcp(h) ((h)->dcp)
  109. #define usbhsh_hpriv_to_hcd(h) \
  110. container_of((void *)h, struct usb_hcd, hcd_priv)
  111. #define usbhsh_ep_to_uep(u) ((u)->hcpriv)
  112. #define usbhsh_uep_to_pipe(u) ((u)->pipe)
  113. #define usbhsh_uep_to_udev(u) ((u)->udev)
  114. #define usbhsh_uep_to_ep(u) ((u)->ep)
  115. #define usbhsh_urb_to_ureq(u) ((u)->hcpriv)
  116. #define usbhsh_urb_to_usbv(u) ((u)->dev)
  117. #define usbhsh_usbv_to_udev(d) dev_get_drvdata(&(d)->dev)
  118. #define usbhsh_udev_to_usbv(h) ((h)->usbv)
  119. #define usbhsh_udev_is_used(h) usbhsh_udev_to_usbv(h)
  120. #define usbhsh_pipe_to_uep(p) ((p)->mod_private)
  121. #define usbhsh_device_parent(d) (usbhsh_usbv_to_udev((d)->usbv->parent))
  122. #define usbhsh_device_hubport(d) ((d)->usbv->portnum)
  123. #define usbhsh_device_number(h, d) ((int)((d) - (h)->udev))
  124. #define usbhsh_device_nth(h, d) ((h)->udev + d)
  125. #define usbhsh_device0(h) usbhsh_device_nth(h, 0)
  126. #define usbhsh_port_stat_init(h) ((h)->port_stat = 0)
  127. #define usbhsh_port_stat_set(h, s) ((h)->port_stat |= (s))
  128. #define usbhsh_port_stat_clear(h, s) ((h)->port_stat &= ~(s))
  129. #define usbhsh_port_stat_get(h) ((h)->port_stat)
  130. #define usbhsh_pkt_to_ureq(p) \
  131. container_of((void *)p, struct usbhsh_request, pkt)
  132. /*
  133. * req alloc/free
  134. */
  135. static struct usbhsh_request *usbhsh_ureq_alloc(struct usbhsh_hpriv *hpriv,
  136. struct urb *urb,
  137. gfp_t mem_flags)
  138. {
  139. struct usbhsh_request *ureq;
  140. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  141. struct device *dev = usbhs_priv_to_dev(priv);
  142. ureq = kzalloc(sizeof(struct usbhsh_request), mem_flags);
  143. if (!ureq) {
  144. dev_err(dev, "ureq alloc fail\n");
  145. return NULL;
  146. }
  147. usbhs_pkt_init(&ureq->pkt);
  148. ureq->urb = urb;
  149. usbhsh_urb_to_ureq(urb) = ureq;
  150. return ureq;
  151. }
  152. static void usbhsh_ureq_free(struct usbhsh_hpriv *hpriv,
  153. struct usbhsh_request *ureq)
  154. {
  155. usbhsh_urb_to_ureq(ureq->urb) = NULL;
  156. ureq->urb = NULL;
  157. kfree(ureq);
  158. }
  159. /*
  160. * pipe control
  161. */
  162. static void usbhsh_endpoint_sequence_save(struct usbhsh_hpriv *hpriv,
  163. struct urb *urb,
  164. struct usbhs_pkt *pkt)
  165. {
  166. int len = urb->actual_length;
  167. int maxp = usb_endpoint_maxp(&urb->ep->desc);
  168. int t = 0;
  169. /* DCP is out of sequence control */
  170. if (usb_pipecontrol(urb->pipe))
  171. return;
  172. /*
  173. * renesas_usbhs pipe has a limitation in a number.
  174. * So, driver should re-use the limited pipe for each device/endpoint.
  175. * DATA0/1 sequence should be saved for it.
  176. * see [image of mod_host]
  177. * [HARDWARE LIMITATION]
  178. */
  179. /*
  180. * next sequence depends on actual_length
  181. *
  182. * ex) actual_length = 1147, maxp = 512
  183. * data0 : 512
  184. * data1 : 512
  185. * data0 : 123
  186. * data1 is the next sequence
  187. */
  188. t = len / maxp;
  189. if (len % maxp)
  190. t++;
  191. if (pkt->zero)
  192. t++;
  193. t %= 2;
  194. if (t)
  195. usb_dotoggle(urb->dev,
  196. usb_pipeendpoint(urb->pipe),
  197. usb_pipeout(urb->pipe));
  198. }
  199. static struct usbhsh_device *usbhsh_device_get(struct usbhsh_hpriv *hpriv,
  200. struct urb *urb);
  201. static int usbhsh_pipe_attach(struct usbhsh_hpriv *hpriv,
  202. struct urb *urb)
  203. {
  204. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  205. struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep);
  206. struct usbhsh_device *udev = usbhsh_device_get(hpriv, urb);
  207. struct usbhs_pipe *pipe;
  208. struct usb_endpoint_descriptor *desc = &urb->ep->desc;
  209. struct device *dev = usbhs_priv_to_dev(priv);
  210. unsigned long flags;
  211. int dir_in_req = !!usb_pipein(urb->pipe);
  212. int is_dcp = usb_endpoint_xfer_control(desc);
  213. int i, dir_in;
  214. int ret = -EBUSY;
  215. /******************** spin lock ********************/
  216. usbhs_lock(priv, flags);
  217. if (unlikely(usbhsh_uep_to_pipe(uep))) {
  218. dev_err(dev, "uep already has pipe\n");
  219. goto usbhsh_pipe_attach_done;
  220. }
  221. usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
  222. /* check pipe type */
  223. if (!usbhs_pipe_type_is(pipe, usb_endpoint_type(desc)))
  224. continue;
  225. /* check pipe direction if normal pipe */
  226. if (!is_dcp) {
  227. dir_in = !!usbhs_pipe_is_dir_in(pipe);
  228. if (0 != (dir_in - dir_in_req))
  229. continue;
  230. }
  231. /* check pipe is free */
  232. if (usbhsh_pipe_to_uep(pipe))
  233. continue;
  234. /*
  235. * attach pipe to uep
  236. *
  237. * usbhs_pipe_config_update() should be called after
  238. * usbhs_set_device_config()
  239. * see
  240. * DCPMAXP/PIPEMAXP
  241. */
  242. usbhsh_uep_to_pipe(uep) = pipe;
  243. usbhsh_pipe_to_uep(pipe) = uep;
  244. usbhs_pipe_config_update(pipe,
  245. usbhsh_device_number(hpriv, udev),
  246. usb_endpoint_num(desc),
  247. usb_endpoint_maxp(desc));
  248. dev_dbg(dev, "%s [%d-%d(%s:%s)]\n", __func__,
  249. usbhsh_device_number(hpriv, udev),
  250. usb_endpoint_num(desc),
  251. usbhs_pipe_name(pipe),
  252. dir_in_req ? "in" : "out");
  253. ret = 0;
  254. break;
  255. }
  256. usbhsh_pipe_attach_done:
  257. usbhs_unlock(priv, flags);
  258. /******************** spin unlock ******************/
  259. return ret;
  260. }
  261. static void usbhsh_pipe_detach(struct usbhsh_hpriv *hpriv,
  262. struct usbhsh_ep *uep)
  263. {
  264. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  265. struct usbhs_pipe *pipe;
  266. struct device *dev = usbhs_priv_to_dev(priv);
  267. unsigned long flags;
  268. /******************** spin lock ********************/
  269. usbhs_lock(priv, flags);
  270. pipe = usbhsh_uep_to_pipe(uep);
  271. if (unlikely(!pipe)) {
  272. dev_err(dev, "uep doens't have pipe\n");
  273. } else {
  274. struct usb_host_endpoint *ep = usbhsh_uep_to_ep(uep);
  275. struct usbhsh_device *udev = usbhsh_uep_to_udev(uep);
  276. /* detach pipe from uep */
  277. usbhsh_uep_to_pipe(uep) = NULL;
  278. usbhsh_pipe_to_uep(pipe) = NULL;
  279. dev_dbg(dev, "%s [%d-%d(%s)]\n", __func__,
  280. usbhsh_device_number(hpriv, udev),
  281. usb_endpoint_num(&ep->desc),
  282. usbhs_pipe_name(pipe));
  283. }
  284. usbhs_unlock(priv, flags);
  285. /******************** spin unlock ******************/
  286. }
  287. /*
  288. * endpoint control
  289. */
  290. static int usbhsh_endpoint_attach(struct usbhsh_hpriv *hpriv,
  291. struct urb *urb,
  292. gfp_t mem_flags)
  293. {
  294. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  295. struct usbhsh_device *udev = usbhsh_device_get(hpriv, urb);
  296. struct usb_host_endpoint *ep = urb->ep;
  297. struct usbhsh_ep *uep;
  298. struct device *dev = usbhs_priv_to_dev(priv);
  299. struct usb_endpoint_descriptor *desc = &ep->desc;
  300. unsigned long flags;
  301. uep = kzalloc(sizeof(struct usbhsh_ep), mem_flags);
  302. if (!uep) {
  303. dev_err(dev, "usbhsh_ep alloc fail\n");
  304. return -ENOMEM;
  305. }
  306. /******************** spin lock ********************/
  307. usbhs_lock(priv, flags);
  308. /*
  309. * init endpoint
  310. */
  311. INIT_LIST_HEAD(&uep->ep_list);
  312. list_add_tail(&uep->ep_list, &udev->ep_list_head);
  313. usbhsh_uep_to_udev(uep) = udev;
  314. usbhsh_uep_to_ep(uep) = ep;
  315. usbhsh_ep_to_uep(ep) = uep;
  316. usbhs_unlock(priv, flags);
  317. /******************** spin unlock ******************/
  318. dev_dbg(dev, "%s [%d-%d]\n", __func__,
  319. usbhsh_device_number(hpriv, udev),
  320. usb_endpoint_num(desc));
  321. return 0;
  322. }
  323. static void usbhsh_endpoint_detach(struct usbhsh_hpriv *hpriv,
  324. struct usb_host_endpoint *ep)
  325. {
  326. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  327. struct device *dev = usbhs_priv_to_dev(priv);
  328. struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep);
  329. unsigned long flags;
  330. if (!uep)
  331. return;
  332. dev_dbg(dev, "%s [%d-%d]\n", __func__,
  333. usbhsh_device_number(hpriv, usbhsh_uep_to_udev(uep)),
  334. usb_endpoint_num(&ep->desc));
  335. if (usbhsh_uep_to_pipe(uep))
  336. usbhsh_pipe_detach(hpriv, uep);
  337. /******************** spin lock ********************/
  338. usbhs_lock(priv, flags);
  339. /* remove this endpoint from udev */
  340. list_del_init(&uep->ep_list);
  341. usbhsh_uep_to_udev(uep) = NULL;
  342. usbhsh_uep_to_ep(uep) = NULL;
  343. usbhsh_ep_to_uep(ep) = NULL;
  344. usbhs_unlock(priv, flags);
  345. /******************** spin unlock ******************/
  346. kfree(uep);
  347. }
  348. static void usbhsh_endpoint_detach_all(struct usbhsh_hpriv *hpriv,
  349. struct usbhsh_device *udev)
  350. {
  351. struct usbhsh_ep *uep, *next;
  352. list_for_each_entry_safe(uep, next, &udev->ep_list_head, ep_list)
  353. usbhsh_endpoint_detach(hpriv, usbhsh_uep_to_ep(uep));
  354. }
  355. /*
  356. * device control
  357. */
  358. static int usbhsh_connected_to_rhdev(struct usb_hcd *hcd,
  359. struct usbhsh_device *udev)
  360. {
  361. struct usb_device *usbv = usbhsh_udev_to_usbv(udev);
  362. return hcd->self.root_hub == usbv->parent;
  363. }
  364. static int usbhsh_device_has_endpoint(struct usbhsh_device *udev)
  365. {
  366. return !list_empty(&udev->ep_list_head);
  367. }
  368. static struct usbhsh_device *usbhsh_device_get(struct usbhsh_hpriv *hpriv,
  369. struct urb *urb)
  370. {
  371. struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
  372. struct usbhsh_device *udev = usbhsh_usbv_to_udev(usbv);
  373. /* usbhsh_device_attach() is still not called */
  374. if (!udev)
  375. return NULL;
  376. /* if it is device0, return it */
  377. if (0 == usb_pipedevice(urb->pipe))
  378. return usbhsh_device0(hpriv);
  379. /* return attached device */
  380. return udev;
  381. }
  382. static struct usbhsh_device *usbhsh_device_attach(struct usbhsh_hpriv *hpriv,
  383. struct urb *urb)
  384. {
  385. struct usbhsh_device *udev = NULL;
  386. struct usbhsh_device *udev0 = usbhsh_device0(hpriv);
  387. struct usbhsh_device *pos;
  388. struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
  389. struct device *dev = usbhsh_hcd_to_dev(hcd);
  390. struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
  391. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  392. unsigned long flags;
  393. u16 upphub, hubport;
  394. int i;
  395. /*
  396. * This function should be called only while urb is pointing to device0.
  397. * It will attach unused usbhsh_device to urb (usbv),
  398. * and initialize device0.
  399. * You can use usbhsh_device_get() to get "current" udev,
  400. * and usbhsh_usbv_to_udev() is for "attached" udev.
  401. */
  402. if (0 != usb_pipedevice(urb->pipe)) {
  403. dev_err(dev, "%s fail: urb isn't pointing device0\n", __func__);
  404. return NULL;
  405. }
  406. /******************** spin lock ********************/
  407. usbhs_lock(priv, flags);
  408. /*
  409. * find unused device
  410. */
  411. usbhsh_for_each_udev(pos, hpriv, i) {
  412. if (usbhsh_udev_is_used(pos))
  413. continue;
  414. udev = pos;
  415. break;
  416. }
  417. if (udev) {
  418. /*
  419. * usbhsh_usbv_to_udev()
  420. * usbhsh_udev_to_usbv()
  421. * will be enable
  422. */
  423. dev_set_drvdata(&usbv->dev, udev);
  424. udev->usbv = usbv;
  425. }
  426. usbhs_unlock(priv, flags);
  427. /******************** spin unlock ******************/
  428. if (!udev) {
  429. dev_err(dev, "no free usbhsh_device\n");
  430. return NULL;
  431. }
  432. if (usbhsh_device_has_endpoint(udev)) {
  433. dev_warn(dev, "udev have old endpoint\n");
  434. usbhsh_endpoint_detach_all(hpriv, udev);
  435. }
  436. if (usbhsh_device_has_endpoint(udev0)) {
  437. dev_warn(dev, "udev0 have old endpoint\n");
  438. usbhsh_endpoint_detach_all(hpriv, udev0);
  439. }
  440. /* uep will be attached */
  441. INIT_LIST_HEAD(&udev0->ep_list_head);
  442. INIT_LIST_HEAD(&udev->ep_list_head);
  443. /*
  444. * set device0 config
  445. */
  446. usbhs_set_device_config(priv,
  447. 0, 0, 0, usbv->speed);
  448. /*
  449. * set new device config
  450. */
  451. upphub = 0;
  452. hubport = 0;
  453. if (!usbhsh_connected_to_rhdev(hcd, udev)) {
  454. /* if udev is not connected to rhdev, it means parent is Hub */
  455. struct usbhsh_device *parent = usbhsh_device_parent(udev);
  456. upphub = usbhsh_device_number(hpriv, parent);
  457. hubport = usbhsh_device_hubport(udev);
  458. dev_dbg(dev, "%s connecte to Hub [%d:%d](%p)\n", __func__,
  459. upphub, hubport, parent);
  460. }
  461. usbhs_set_device_config(priv,
  462. usbhsh_device_number(hpriv, udev),
  463. upphub, hubport, usbv->speed);
  464. dev_dbg(dev, "%s [%d](%p)\n", __func__,
  465. usbhsh_device_number(hpriv, udev), udev);
  466. return udev;
  467. }
  468. static void usbhsh_device_detach(struct usbhsh_hpriv *hpriv,
  469. struct usbhsh_device *udev)
  470. {
  471. struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
  472. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  473. struct device *dev = usbhsh_hcd_to_dev(hcd);
  474. struct usb_device *usbv = usbhsh_udev_to_usbv(udev);
  475. unsigned long flags;
  476. dev_dbg(dev, "%s [%d](%p)\n", __func__,
  477. usbhsh_device_number(hpriv, udev), udev);
  478. if (usbhsh_device_has_endpoint(udev)) {
  479. dev_warn(dev, "udev still have endpoint\n");
  480. usbhsh_endpoint_detach_all(hpriv, udev);
  481. }
  482. /*
  483. * There is nothing to do if it is device0.
  484. * see
  485. * usbhsh_device_attach()
  486. * usbhsh_device_get()
  487. */
  488. if (0 == usbhsh_device_number(hpriv, udev))
  489. return;
  490. /******************** spin lock ********************/
  491. usbhs_lock(priv, flags);
  492. /*
  493. * usbhsh_usbv_to_udev()
  494. * usbhsh_udev_to_usbv()
  495. * will be disable
  496. */
  497. dev_set_drvdata(&usbv->dev, NULL);
  498. udev->usbv = NULL;
  499. usbhs_unlock(priv, flags);
  500. /******************** spin unlock ******************/
  501. }
  502. /*
  503. * queue push/pop
  504. */
  505. static void usbhsh_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt)
  506. {
  507. struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt);
  508. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  509. struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
  510. struct urb *urb = ureq->urb;
  511. struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep);
  512. struct device *dev = usbhs_priv_to_dev(priv);
  513. dev_dbg(dev, "%s\n", __func__);
  514. if (!urb) {
  515. dev_warn(dev, "pkt doesn't have urb\n");
  516. return;
  517. }
  518. urb->actual_length = pkt->actual;
  519. usbhsh_ureq_free(hpriv, ureq);
  520. usbhsh_endpoint_sequence_save(hpriv, urb, pkt);
  521. usbhsh_pipe_detach(hpriv, uep);
  522. usb_hcd_unlink_urb_from_ep(hcd, urb);
  523. usb_hcd_giveback_urb(hcd, urb, 0);
  524. }
  525. static int usbhsh_queue_push(struct usb_hcd *hcd,
  526. struct urb *urb,
  527. gfp_t mem_flags)
  528. {
  529. struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
  530. struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep);
  531. struct usbhs_pipe *pipe = usbhsh_uep_to_pipe(uep);
  532. struct device *dev = usbhsh_hcd_to_dev(hcd);
  533. struct usbhsh_request *ureq;
  534. void *buf;
  535. int len, sequence;
  536. if (usb_pipeisoc(urb->pipe)) {
  537. dev_err(dev, "pipe iso is not supported now\n");
  538. return -EIO;
  539. }
  540. /* this ureq will be freed on usbhsh_queue_done() */
  541. ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
  542. if (unlikely(!ureq)) {
  543. dev_err(dev, "ureq alloc fail\n");
  544. return -ENOMEM;
  545. }
  546. if (usb_pipein(urb->pipe))
  547. pipe->handler = &usbhs_fifo_pio_pop_handler;
  548. else
  549. pipe->handler = &usbhs_fifo_pio_push_handler;
  550. buf = (void *)(urb->transfer_buffer + urb->actual_length);
  551. len = urb->transfer_buffer_length - urb->actual_length;
  552. sequence = usb_gettoggle(urb->dev,
  553. usb_pipeendpoint(urb->pipe),
  554. usb_pipeout(urb->pipe));
  555. dev_dbg(dev, "%s\n", __func__);
  556. usbhs_pkt_push(pipe, &ureq->pkt, usbhsh_queue_done,
  557. buf, len, (urb->transfer_flags & URB_ZERO_PACKET),
  558. sequence);
  559. usbhs_pkt_start(pipe);
  560. return 0;
  561. }
  562. /*
  563. * DCP setup stage
  564. */
  565. static int usbhsh_is_request_address(struct urb *urb)
  566. {
  567. struct usb_ctrlrequest *req;
  568. req = (struct usb_ctrlrequest *)urb->setup_packet;
  569. if ((DeviceOutRequest == req->bRequestType << 8) &&
  570. (USB_REQ_SET_ADDRESS == req->bRequest))
  571. return 1;
  572. else
  573. return 0;
  574. }
  575. static void usbhsh_setup_stage_packet_push(struct usbhsh_hpriv *hpriv,
  576. struct urb *urb,
  577. struct usbhs_pipe *pipe)
  578. {
  579. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  580. struct usb_ctrlrequest req;
  581. struct device *dev = usbhs_priv_to_dev(priv);
  582. /*
  583. * wait setup packet ACK
  584. * see
  585. * usbhsh_irq_setup_ack()
  586. * usbhsh_irq_setup_err()
  587. */
  588. init_completion(&hpriv->setup_ack_done);
  589. /* copy original request */
  590. memcpy(&req, urb->setup_packet, sizeof(struct usb_ctrlrequest));
  591. /*
  592. * renesas_usbhs can not use original usb address.
  593. * see HARDWARE LIMITATION.
  594. * modify usb address here to use attached device.
  595. * see usbhsh_device_attach()
  596. */
  597. if (usbhsh_is_request_address(urb)) {
  598. struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
  599. struct usbhsh_device *udev = usbhsh_usbv_to_udev(usbv);
  600. /* udev is a attached device */
  601. req.wValue = usbhsh_device_number(hpriv, udev);
  602. dev_dbg(dev, "create new address - %d\n", req.wValue);
  603. }
  604. /* set request */
  605. usbhs_usbreq_set_val(priv, &req);
  606. /*
  607. * wait setup packet ACK
  608. */
  609. wait_for_completion(&hpriv->setup_ack_done);
  610. dev_dbg(dev, "%s done\n", __func__);
  611. }
  612. /*
  613. * DCP data stage
  614. */
  615. static void usbhsh_data_stage_packet_done(struct usbhs_priv *priv,
  616. struct usbhs_pkt *pkt)
  617. {
  618. struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt);
  619. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  620. /* this ureq was connected to urb when usbhsh_urb_enqueue() */
  621. usbhsh_ureq_free(hpriv, ureq);
  622. }
  623. static int usbhsh_data_stage_packet_push(struct usbhsh_hpriv *hpriv,
  624. struct urb *urb,
  625. struct usbhs_pipe *pipe,
  626. gfp_t mem_flags)
  627. {
  628. struct usbhsh_request *ureq;
  629. /* this ureq will be freed on usbhsh_data_stage_packet_done() */
  630. ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
  631. if (unlikely(!ureq))
  632. return -ENOMEM;
  633. if (usb_pipein(urb->pipe))
  634. pipe->handler = &usbhs_dcp_data_stage_in_handler;
  635. else
  636. pipe->handler = &usbhs_dcp_data_stage_out_handler;
  637. usbhs_pkt_push(pipe, &ureq->pkt,
  638. usbhsh_data_stage_packet_done,
  639. urb->transfer_buffer,
  640. urb->transfer_buffer_length,
  641. (urb->transfer_flags & URB_ZERO_PACKET),
  642. -1);
  643. return 0;
  644. }
  645. /*
  646. * DCP status stage
  647. */
  648. static int usbhsh_status_stage_packet_push(struct usbhsh_hpriv *hpriv,
  649. struct urb *urb,
  650. struct usbhs_pipe *pipe,
  651. gfp_t mem_flags)
  652. {
  653. struct usbhsh_request *ureq;
  654. /* This ureq will be freed on usbhsh_queue_done() */
  655. ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
  656. if (unlikely(!ureq))
  657. return -ENOMEM;
  658. if (usb_pipein(urb->pipe))
  659. pipe->handler = &usbhs_dcp_status_stage_in_handler;
  660. else
  661. pipe->handler = &usbhs_dcp_status_stage_out_handler;
  662. usbhs_pkt_push(pipe, &ureq->pkt,
  663. usbhsh_queue_done,
  664. NULL,
  665. urb->transfer_buffer_length,
  666. 0, -1);
  667. return 0;
  668. }
  669. static int usbhsh_dcp_queue_push(struct usb_hcd *hcd,
  670. struct urb *urb,
  671. gfp_t mflags)
  672. {
  673. struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
  674. struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep);
  675. struct usbhs_pipe *pipe = usbhsh_uep_to_pipe(uep);
  676. struct device *dev = usbhsh_hcd_to_dev(hcd);
  677. int ret;
  678. dev_dbg(dev, "%s\n", __func__);
  679. /*
  680. * setup stage
  681. *
  682. * usbhsh_send_setup_stage_packet() wait SACK/SIGN
  683. */
  684. usbhsh_setup_stage_packet_push(hpriv, urb, pipe);
  685. /*
  686. * data stage
  687. *
  688. * It is pushed only when urb has buffer.
  689. */
  690. if (urb->transfer_buffer_length) {
  691. ret = usbhsh_data_stage_packet_push(hpriv, urb, pipe, mflags);
  692. if (ret < 0) {
  693. dev_err(dev, "data stage failed\n");
  694. return ret;
  695. }
  696. }
  697. /*
  698. * status stage
  699. */
  700. ret = usbhsh_status_stage_packet_push(hpriv, urb, pipe, mflags);
  701. if (ret < 0) {
  702. dev_err(dev, "status stage failed\n");
  703. return ret;
  704. }
  705. /*
  706. * start pushed packets
  707. */
  708. usbhs_pkt_start(pipe);
  709. return 0;
  710. }
  711. /*
  712. * dma map functions
  713. */
  714. static int usbhsh_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
  715. {
  716. return 0;
  717. }
  718. /*
  719. * for hc_driver
  720. */
  721. static int usbhsh_host_start(struct usb_hcd *hcd)
  722. {
  723. return 0;
  724. }
  725. static void usbhsh_host_stop(struct usb_hcd *hcd)
  726. {
  727. }
  728. static int usbhsh_urb_enqueue(struct usb_hcd *hcd,
  729. struct urb *urb,
  730. gfp_t mem_flags)
  731. {
  732. struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
  733. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  734. struct device *dev = usbhs_priv_to_dev(priv);
  735. struct usb_host_endpoint *ep = urb->ep;
  736. struct usbhsh_device *new_udev = NULL;
  737. int is_dir_in = usb_pipein(urb->pipe);
  738. int i;
  739. int ret;
  740. dev_dbg(dev, "%s (%s)\n", __func__, is_dir_in ? "in" : "out");
  741. ret = usb_hcd_link_urb_to_ep(hcd, urb);
  742. if (ret)
  743. goto usbhsh_urb_enqueue_error_not_linked;
  744. /*
  745. * attach udev if needed
  746. * see [image of mod_host]
  747. */
  748. if (!usbhsh_device_get(hpriv, urb)) {
  749. new_udev = usbhsh_device_attach(hpriv, urb);
  750. if (!new_udev) {
  751. ret = -EIO;
  752. goto usbhsh_urb_enqueue_error_not_linked;
  753. }
  754. }
  755. /*
  756. * attach endpoint if needed
  757. * see [image of mod_host]
  758. */
  759. if (!usbhsh_ep_to_uep(ep)) {
  760. ret = usbhsh_endpoint_attach(hpriv, urb, mem_flags);
  761. if (ret < 0)
  762. goto usbhsh_urb_enqueue_error_free_device;
  763. }
  764. /*
  765. * attach pipe to endpoint
  766. * see [image of mod_host]
  767. */
  768. for (i = 0; i < 1024; i++) {
  769. ret = usbhsh_pipe_attach(hpriv, urb);
  770. if (ret < 0)
  771. msleep(100);
  772. else
  773. break;
  774. }
  775. if (ret < 0)
  776. goto usbhsh_urb_enqueue_error_free_endpoint;
  777. /*
  778. * push packet
  779. */
  780. if (usb_pipecontrol(urb->pipe))
  781. ret = usbhsh_dcp_queue_push(hcd, urb, mem_flags);
  782. else
  783. ret = usbhsh_queue_push(hcd, urb, mem_flags);
  784. return ret;
  785. usbhsh_urb_enqueue_error_free_endpoint:
  786. usbhsh_endpoint_detach(hpriv, ep);
  787. usbhsh_urb_enqueue_error_free_device:
  788. if (new_udev)
  789. usbhsh_device_detach(hpriv, new_udev);
  790. usbhsh_urb_enqueue_error_not_linked:
  791. dev_dbg(dev, "%s error\n", __func__);
  792. return ret;
  793. }
  794. static int usbhsh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  795. {
  796. struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
  797. struct usbhsh_request *ureq = usbhsh_urb_to_ureq(urb);
  798. if (ureq) {
  799. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  800. struct usbhs_pkt *pkt = &ureq->pkt;
  801. usbhs_pkt_pop(pkt->pipe, pkt);
  802. usbhsh_queue_done(priv, pkt);
  803. }
  804. return 0;
  805. }
  806. static void usbhsh_endpoint_disable(struct usb_hcd *hcd,
  807. struct usb_host_endpoint *ep)
  808. {
  809. struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep);
  810. struct usbhsh_device *udev;
  811. struct usbhsh_hpriv *hpriv;
  812. /*
  813. * this function might be called manytimes by same hcd/ep
  814. * in-endpoint == out-endpoint if ep == dcp.
  815. */
  816. if (!uep)
  817. return;
  818. udev = usbhsh_uep_to_udev(uep);
  819. hpriv = usbhsh_hcd_to_hpriv(hcd);
  820. usbhsh_endpoint_detach(hpriv, ep);
  821. /*
  822. * if there is no endpoint,
  823. * free device
  824. */
  825. if (!usbhsh_device_has_endpoint(udev))
  826. usbhsh_device_detach(hpriv, udev);
  827. }
  828. static int usbhsh_hub_status_data(struct usb_hcd *hcd, char *buf)
  829. {
  830. struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
  831. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  832. struct device *dev = usbhs_priv_to_dev(priv);
  833. int roothub_id = 1; /* only 1 root hub */
  834. /*
  835. * does port stat was changed ?
  836. * check USB_PORT_STAT_C_xxx << 16
  837. */
  838. if (usbhsh_port_stat_get(hpriv) & 0xFFFF0000)
  839. *buf = (1 << roothub_id);
  840. else
  841. *buf = 0;
  842. dev_dbg(dev, "%s (%02x)\n", __func__, *buf);
  843. return !!(*buf);
  844. }
  845. static int __usbhsh_hub_hub_feature(struct usbhsh_hpriv *hpriv,
  846. u16 typeReq, u16 wValue,
  847. u16 wIndex, char *buf, u16 wLength)
  848. {
  849. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  850. struct device *dev = usbhs_priv_to_dev(priv);
  851. switch (wValue) {
  852. case C_HUB_OVER_CURRENT:
  853. case C_HUB_LOCAL_POWER:
  854. dev_dbg(dev, "%s :: C_HUB_xx\n", __func__);
  855. return 0;
  856. }
  857. return -EPIPE;
  858. }
  859. static int __usbhsh_hub_port_feature(struct usbhsh_hpriv *hpriv,
  860. u16 typeReq, u16 wValue,
  861. u16 wIndex, char *buf, u16 wLength)
  862. {
  863. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  864. struct device *dev = usbhs_priv_to_dev(priv);
  865. int enable = (typeReq == SetPortFeature);
  866. int speed, i, timeout = 128;
  867. int roothub_id = 1; /* only 1 root hub */
  868. /* common error */
  869. if (wIndex > roothub_id || wLength != 0)
  870. return -EPIPE;
  871. /* check wValue */
  872. switch (wValue) {
  873. case USB_PORT_FEAT_POWER:
  874. usbhs_vbus_ctrl(priv, enable);
  875. dev_dbg(dev, "%s :: USB_PORT_FEAT_POWER\n", __func__);
  876. break;
  877. case USB_PORT_FEAT_ENABLE:
  878. case USB_PORT_FEAT_SUSPEND:
  879. case USB_PORT_FEAT_C_ENABLE:
  880. case USB_PORT_FEAT_C_SUSPEND:
  881. case USB_PORT_FEAT_C_CONNECTION:
  882. case USB_PORT_FEAT_C_OVER_CURRENT:
  883. case USB_PORT_FEAT_C_RESET:
  884. dev_dbg(dev, "%s :: USB_PORT_FEAT_xxx\n", __func__);
  885. break;
  886. case USB_PORT_FEAT_RESET:
  887. if (!enable)
  888. break;
  889. usbhsh_port_stat_clear(hpriv,
  890. USB_PORT_STAT_HIGH_SPEED |
  891. USB_PORT_STAT_LOW_SPEED);
  892. usbhs_bus_send_reset(priv);
  893. msleep(20);
  894. usbhs_bus_send_sof_enable(priv);
  895. for (i = 0; i < timeout ; i++) {
  896. switch (usbhs_bus_get_speed(priv)) {
  897. case USB_SPEED_LOW:
  898. speed = USB_PORT_STAT_LOW_SPEED;
  899. goto got_usb_bus_speed;
  900. case USB_SPEED_HIGH:
  901. speed = USB_PORT_STAT_HIGH_SPEED;
  902. goto got_usb_bus_speed;
  903. case USB_SPEED_FULL:
  904. speed = 0;
  905. goto got_usb_bus_speed;
  906. }
  907. msleep(20);
  908. }
  909. return -EPIPE;
  910. got_usb_bus_speed:
  911. usbhsh_port_stat_set(hpriv, speed);
  912. usbhsh_port_stat_set(hpriv, USB_PORT_STAT_ENABLE);
  913. dev_dbg(dev, "%s :: USB_PORT_FEAT_RESET (speed = %d)\n",
  914. __func__, speed);
  915. /* status change is not needed */
  916. return 0;
  917. default:
  918. return -EPIPE;
  919. }
  920. /* set/clear status */
  921. if (enable)
  922. usbhsh_port_stat_set(hpriv, (1 << wValue));
  923. else
  924. usbhsh_port_stat_clear(hpriv, (1 << wValue));
  925. return 0;
  926. }
  927. static int __usbhsh_hub_get_status(struct usbhsh_hpriv *hpriv,
  928. u16 typeReq, u16 wValue,
  929. u16 wIndex, char *buf, u16 wLength)
  930. {
  931. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  932. struct usb_hub_descriptor *desc = (struct usb_hub_descriptor *)buf;
  933. struct device *dev = usbhs_priv_to_dev(priv);
  934. int roothub_id = 1; /* only 1 root hub */
  935. switch (typeReq) {
  936. case GetHubStatus:
  937. dev_dbg(dev, "%s :: GetHubStatus\n", __func__);
  938. *buf = 0x00;
  939. break;
  940. case GetPortStatus:
  941. if (wIndex != roothub_id)
  942. return -EPIPE;
  943. dev_dbg(dev, "%s :: GetPortStatus\n", __func__);
  944. *(__le32 *)buf = cpu_to_le32(usbhsh_port_stat_get(hpriv));
  945. break;
  946. case GetHubDescriptor:
  947. desc->bDescriptorType = 0x29;
  948. desc->bHubContrCurrent = 0;
  949. desc->bNbrPorts = roothub_id;
  950. desc->bDescLength = 9;
  951. desc->bPwrOn2PwrGood = 0;
  952. desc->wHubCharacteristics = cpu_to_le16(0x0011);
  953. desc->u.hs.DeviceRemovable[0] = (roothub_id << 1);
  954. desc->u.hs.DeviceRemovable[1] = ~0;
  955. dev_dbg(dev, "%s :: GetHubDescriptor\n", __func__);
  956. break;
  957. }
  958. return 0;
  959. }
  960. static int usbhsh_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  961. u16 wIndex, char *buf, u16 wLength)
  962. {
  963. struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
  964. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  965. struct device *dev = usbhs_priv_to_dev(priv);
  966. int ret = -EPIPE;
  967. switch (typeReq) {
  968. /* Hub Feature */
  969. case ClearHubFeature:
  970. case SetHubFeature:
  971. ret = __usbhsh_hub_hub_feature(hpriv, typeReq,
  972. wValue, wIndex, buf, wLength);
  973. break;
  974. /* Port Feature */
  975. case SetPortFeature:
  976. case ClearPortFeature:
  977. ret = __usbhsh_hub_port_feature(hpriv, typeReq,
  978. wValue, wIndex, buf, wLength);
  979. break;
  980. /* Get status */
  981. case GetHubStatus:
  982. case GetPortStatus:
  983. case GetHubDescriptor:
  984. ret = __usbhsh_hub_get_status(hpriv, typeReq,
  985. wValue, wIndex, buf, wLength);
  986. break;
  987. }
  988. dev_dbg(dev, "typeReq = %x, ret = %d, port_stat = %x\n",
  989. typeReq, ret, usbhsh_port_stat_get(hpriv));
  990. return ret;
  991. }
  992. static struct hc_driver usbhsh_driver = {
  993. .description = usbhsh_hcd_name,
  994. .hcd_priv_size = sizeof(struct usbhsh_hpriv),
  995. /*
  996. * generic hardware linkage
  997. */
  998. .flags = HCD_USB2,
  999. .start = usbhsh_host_start,
  1000. .stop = usbhsh_host_stop,
  1001. /*
  1002. * managing i/o requests and associated device resources
  1003. */
  1004. .urb_enqueue = usbhsh_urb_enqueue,
  1005. .urb_dequeue = usbhsh_urb_dequeue,
  1006. .endpoint_disable = usbhsh_endpoint_disable,
  1007. /*
  1008. * root hub
  1009. */
  1010. .hub_status_data = usbhsh_hub_status_data,
  1011. .hub_control = usbhsh_hub_control,
  1012. };
  1013. /*
  1014. * interrupt functions
  1015. */
  1016. static int usbhsh_irq_attch(struct usbhs_priv *priv,
  1017. struct usbhs_irq_state *irq_state)
  1018. {
  1019. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  1020. struct device *dev = usbhs_priv_to_dev(priv);
  1021. dev_dbg(dev, "device attached\n");
  1022. usbhsh_port_stat_set(hpriv, USB_PORT_STAT_CONNECTION);
  1023. usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16);
  1024. /*
  1025. * attch interrupt might happen infinitely on some device
  1026. * (on self power USB hub ?)
  1027. * disable it here.
  1028. */
  1029. hpriv->mod.irq_attch = NULL;
  1030. usbhs_irq_callback_update(priv, &hpriv->mod);
  1031. return 0;
  1032. }
  1033. static int usbhsh_irq_dtch(struct usbhs_priv *priv,
  1034. struct usbhs_irq_state *irq_state)
  1035. {
  1036. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  1037. struct device *dev = usbhs_priv_to_dev(priv);
  1038. dev_dbg(dev, "device detached\n");
  1039. usbhsh_port_stat_clear(hpriv, USB_PORT_STAT_CONNECTION);
  1040. usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16);
  1041. /*
  1042. * enable attch interrupt again
  1043. */
  1044. hpriv->mod.irq_attch = usbhsh_irq_attch;
  1045. usbhs_irq_callback_update(priv, &hpriv->mod);
  1046. return 0;
  1047. }
  1048. static int usbhsh_irq_setup_ack(struct usbhs_priv *priv,
  1049. struct usbhs_irq_state *irq_state)
  1050. {
  1051. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  1052. struct device *dev = usbhs_priv_to_dev(priv);
  1053. dev_dbg(dev, "setup packet OK\n");
  1054. complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */
  1055. return 0;
  1056. }
  1057. static int usbhsh_irq_setup_err(struct usbhs_priv *priv,
  1058. struct usbhs_irq_state *irq_state)
  1059. {
  1060. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  1061. struct device *dev = usbhs_priv_to_dev(priv);
  1062. dev_dbg(dev, "setup packet Err\n");
  1063. complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */
  1064. return 0;
  1065. }
  1066. /*
  1067. * module start/stop
  1068. */
  1069. static void usbhsh_pipe_init_for_host(struct usbhs_priv *priv)
  1070. {
  1071. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  1072. struct usbhs_pipe *pipe;
  1073. u32 *pipe_type = usbhs_get_dparam(priv, pipe_type);
  1074. int pipe_size = usbhs_get_dparam(priv, pipe_size);
  1075. int old_type, dir_in, i;
  1076. /* init all pipe */
  1077. old_type = USB_ENDPOINT_XFER_CONTROL;
  1078. for (i = 0; i < pipe_size; i++) {
  1079. /*
  1080. * data "output" will be finished as soon as possible,
  1081. * but there is no guaranty at data "input" case.
  1082. *
  1083. * "input" needs "standby" pipe.
  1084. * So, "input" direction pipe > "output" direction pipe
  1085. * is good idea.
  1086. *
  1087. * 1st USB_ENDPOINT_XFER_xxx will be output direction,
  1088. * and the other will be input direction here.
  1089. *
  1090. * ex)
  1091. * ...
  1092. * USB_ENDPOINT_XFER_ISOC -> dir out
  1093. * USB_ENDPOINT_XFER_ISOC -> dir in
  1094. * USB_ENDPOINT_XFER_BULK -> dir out
  1095. * USB_ENDPOINT_XFER_BULK -> dir in
  1096. * USB_ENDPOINT_XFER_BULK -> dir in
  1097. * ...
  1098. */
  1099. dir_in = (pipe_type[i] == old_type);
  1100. old_type = pipe_type[i];
  1101. if (USB_ENDPOINT_XFER_CONTROL == pipe_type[i]) {
  1102. pipe = usbhs_dcp_malloc(priv);
  1103. usbhsh_hpriv_to_dcp(hpriv) = pipe;
  1104. } else {
  1105. pipe = usbhs_pipe_malloc(priv,
  1106. pipe_type[i],
  1107. dir_in);
  1108. }
  1109. pipe->mod_private = NULL;
  1110. }
  1111. }
  1112. static int usbhsh_start(struct usbhs_priv *priv)
  1113. {
  1114. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  1115. struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
  1116. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  1117. struct device *dev = usbhs_priv_to_dev(priv);
  1118. int ret;
  1119. /* add hcd */
  1120. ret = usb_add_hcd(hcd, 0, 0);
  1121. if (ret < 0)
  1122. return 0;
  1123. /*
  1124. * pipe initialize and enable DCP
  1125. */
  1126. usbhs_pipe_init(priv,
  1127. usbhsh_dma_map_ctrl);
  1128. usbhs_fifo_init(priv);
  1129. usbhsh_pipe_init_for_host(priv);
  1130. /*
  1131. * system config enble
  1132. * - HI speed
  1133. * - host
  1134. * - usb module
  1135. */
  1136. usbhs_sys_host_ctrl(priv, 1);
  1137. /*
  1138. * enable irq callback
  1139. */
  1140. mod->irq_attch = usbhsh_irq_attch;
  1141. mod->irq_dtch = usbhsh_irq_dtch;
  1142. mod->irq_sack = usbhsh_irq_setup_ack;
  1143. mod->irq_sign = usbhsh_irq_setup_err;
  1144. usbhs_irq_callback_update(priv, mod);
  1145. dev_dbg(dev, "start host\n");
  1146. return ret;
  1147. }
  1148. static int usbhsh_stop(struct usbhs_priv *priv)
  1149. {
  1150. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  1151. struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
  1152. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  1153. struct device *dev = usbhs_priv_to_dev(priv);
  1154. /*
  1155. * disable irq callback
  1156. */
  1157. mod->irq_attch = NULL;
  1158. mod->irq_dtch = NULL;
  1159. mod->irq_sack = NULL;
  1160. mod->irq_sign = NULL;
  1161. usbhs_irq_callback_update(priv, mod);
  1162. usb_remove_hcd(hcd);
  1163. /* disable sys */
  1164. usbhs_sys_host_ctrl(priv, 0);
  1165. dev_dbg(dev, "quit host\n");
  1166. return 0;
  1167. }
  1168. int usbhs_mod_host_probe(struct usbhs_priv *priv)
  1169. {
  1170. struct usbhsh_hpriv *hpriv;
  1171. struct usb_hcd *hcd;
  1172. struct usbhsh_device *udev;
  1173. struct device *dev = usbhs_priv_to_dev(priv);
  1174. int i;
  1175. /* initialize hcd */
  1176. hcd = usb_create_hcd(&usbhsh_driver, dev, usbhsh_hcd_name);
  1177. if (!hcd) {
  1178. dev_err(dev, "Failed to create hcd\n");
  1179. return -ENOMEM;
  1180. }
  1181. /*
  1182. * CAUTION
  1183. *
  1184. * There is no guarantee that it is possible to access usb module here.
  1185. * Don't accesses to it.
  1186. * The accesse will be enable after "usbhsh_start"
  1187. */
  1188. hpriv = usbhsh_hcd_to_hpriv(hcd);
  1189. /*
  1190. * register itself
  1191. */
  1192. usbhs_mod_register(priv, &hpriv->mod, USBHS_HOST);
  1193. /* init hpriv */
  1194. hpriv->mod.name = "host";
  1195. hpriv->mod.start = usbhsh_start;
  1196. hpriv->mod.stop = usbhsh_stop;
  1197. usbhsh_port_stat_init(hpriv);
  1198. /* init all device */
  1199. usbhsh_for_each_udev_with_dev0(udev, hpriv, i) {
  1200. udev->usbv = NULL;
  1201. INIT_LIST_HEAD(&udev->ep_list_head);
  1202. }
  1203. dev_info(dev, "host probed\n");
  1204. return 0;
  1205. }
  1206. int usbhs_mod_host_remove(struct usbhs_priv *priv)
  1207. {
  1208. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  1209. struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
  1210. usb_put_hcd(hcd);
  1211. return 0;
  1212. }