mod_host.c 37 KB

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