mod_host.c 37 KB

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