mod_host.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587
  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; \
  100. ((i) < USBHSH_DEVICE_MAX) && ((pos) = (h)->udev + (i)); \
  101. (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 = USB_DT_HUB;
  1012. desc->bHubContrCurrent = 0;
  1013. desc->bNbrPorts = roothub_id;
  1014. desc->bDescLength = 9;
  1015. desc->bPwrOn2PwrGood = 0;
  1016. desc->wHubCharacteristics =
  1017. cpu_to_le16(HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_NO_OCPM);
  1018. desc->u.hs.DeviceRemovable[0] = (roothub_id << 1);
  1019. desc->u.hs.DeviceRemovable[1] = ~0;
  1020. dev_dbg(dev, "%s :: GetHubDescriptor\n", __func__);
  1021. break;
  1022. }
  1023. return 0;
  1024. }
  1025. static int usbhsh_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  1026. u16 wIndex, char *buf, u16 wLength)
  1027. {
  1028. struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
  1029. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  1030. struct device *dev = usbhs_priv_to_dev(priv);
  1031. int ret = -EPIPE;
  1032. switch (typeReq) {
  1033. /* Hub Feature */
  1034. case ClearHubFeature:
  1035. case SetHubFeature:
  1036. ret = __usbhsh_hub_hub_feature(hpriv, typeReq,
  1037. wValue, wIndex, buf, wLength);
  1038. break;
  1039. /* Port Feature */
  1040. case SetPortFeature:
  1041. case ClearPortFeature:
  1042. ret = __usbhsh_hub_port_feature(hpriv, typeReq,
  1043. wValue, wIndex, buf, wLength);
  1044. break;
  1045. /* Get status */
  1046. case GetHubStatus:
  1047. case GetPortStatus:
  1048. case GetHubDescriptor:
  1049. ret = __usbhsh_hub_get_status(hpriv, typeReq,
  1050. wValue, wIndex, buf, wLength);
  1051. break;
  1052. }
  1053. dev_dbg(dev, "typeReq = %x, ret = %d, port_stat = %x\n",
  1054. typeReq, ret, usbhsh_port_stat_get(hpriv));
  1055. return ret;
  1056. }
  1057. static int usbhsh_bus_nop(struct usb_hcd *hcd)
  1058. {
  1059. /* nothing to do */
  1060. return 0;
  1061. }
  1062. static struct hc_driver usbhsh_driver = {
  1063. .description = usbhsh_hcd_name,
  1064. .hcd_priv_size = sizeof(struct usbhsh_hpriv),
  1065. /*
  1066. * generic hardware linkage
  1067. */
  1068. .flags = HCD_USB2,
  1069. .start = usbhsh_host_start,
  1070. .stop = usbhsh_host_stop,
  1071. /*
  1072. * managing i/o requests and associated device resources
  1073. */
  1074. .urb_enqueue = usbhsh_urb_enqueue,
  1075. .urb_dequeue = usbhsh_urb_dequeue,
  1076. .endpoint_disable = usbhsh_endpoint_disable,
  1077. /*
  1078. * root hub
  1079. */
  1080. .hub_status_data = usbhsh_hub_status_data,
  1081. .hub_control = usbhsh_hub_control,
  1082. .bus_suspend = usbhsh_bus_nop,
  1083. .bus_resume = usbhsh_bus_nop,
  1084. };
  1085. /*
  1086. * interrupt functions
  1087. */
  1088. static int usbhsh_irq_attch(struct usbhs_priv *priv,
  1089. struct usbhs_irq_state *irq_state)
  1090. {
  1091. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  1092. struct device *dev = usbhs_priv_to_dev(priv);
  1093. dev_dbg(dev, "device attached\n");
  1094. usbhsh_port_stat_set(hpriv, USB_PORT_STAT_CONNECTION);
  1095. usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16);
  1096. /*
  1097. * attch interrupt might happen infinitely on some device
  1098. * (on self power USB hub ?)
  1099. * disable it here.
  1100. *
  1101. * usbhsh_is_running() becomes effective
  1102. * according to this process.
  1103. * see
  1104. * usbhsh_is_running()
  1105. * usbhsh_urb_enqueue()
  1106. */
  1107. hpriv->mod.irq_attch = NULL;
  1108. usbhs_irq_callback_update(priv, &hpriv->mod);
  1109. return 0;
  1110. }
  1111. static int usbhsh_irq_dtch(struct usbhs_priv *priv,
  1112. struct usbhs_irq_state *irq_state)
  1113. {
  1114. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  1115. struct device *dev = usbhs_priv_to_dev(priv);
  1116. dev_dbg(dev, "device detached\n");
  1117. usbhsh_port_stat_clear(hpriv, USB_PORT_STAT_CONNECTION);
  1118. usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16);
  1119. /*
  1120. * enable attch interrupt again
  1121. *
  1122. * usbhsh_is_running() becomes invalid
  1123. * according to this process.
  1124. * see
  1125. * usbhsh_is_running()
  1126. * usbhsh_urb_enqueue()
  1127. */
  1128. hpriv->mod.irq_attch = usbhsh_irq_attch;
  1129. usbhs_irq_callback_update(priv, &hpriv->mod);
  1130. /*
  1131. * usbhsh_queue_force_pop_all() should be called
  1132. * after usbhsh_is_running() becomes invalid.
  1133. */
  1134. usbhsh_queue_force_pop_all(priv);
  1135. return 0;
  1136. }
  1137. static int usbhsh_irq_setup_ack(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 OK\n");
  1143. complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */
  1144. return 0;
  1145. }
  1146. static int usbhsh_irq_setup_err(struct usbhs_priv *priv,
  1147. struct usbhs_irq_state *irq_state)
  1148. {
  1149. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  1150. struct device *dev = usbhs_priv_to_dev(priv);
  1151. dev_dbg(dev, "setup packet Err\n");
  1152. complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */
  1153. return 0;
  1154. }
  1155. /*
  1156. * module start/stop
  1157. */
  1158. static void usbhsh_pipe_init_for_host(struct usbhs_priv *priv)
  1159. {
  1160. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  1161. struct usbhs_pipe *pipe;
  1162. u32 *pipe_type = usbhs_get_dparam(priv, pipe_type);
  1163. int pipe_size = usbhs_get_dparam(priv, pipe_size);
  1164. int old_type, dir_in, i;
  1165. /* init all pipe */
  1166. old_type = USB_ENDPOINT_XFER_CONTROL;
  1167. for (i = 0; i < pipe_size; i++) {
  1168. /*
  1169. * data "output" will be finished as soon as possible,
  1170. * but there is no guaranty at data "input" case.
  1171. *
  1172. * "input" needs "standby" pipe.
  1173. * So, "input" direction pipe > "output" direction pipe
  1174. * is good idea.
  1175. *
  1176. * 1st USB_ENDPOINT_XFER_xxx will be output direction,
  1177. * and the other will be input direction here.
  1178. *
  1179. * ex)
  1180. * ...
  1181. * USB_ENDPOINT_XFER_ISOC -> dir out
  1182. * USB_ENDPOINT_XFER_ISOC -> dir in
  1183. * USB_ENDPOINT_XFER_BULK -> dir out
  1184. * USB_ENDPOINT_XFER_BULK -> dir in
  1185. * USB_ENDPOINT_XFER_BULK -> dir in
  1186. * ...
  1187. */
  1188. dir_in = (pipe_type[i] == old_type);
  1189. old_type = pipe_type[i];
  1190. if (USB_ENDPOINT_XFER_CONTROL == pipe_type[i]) {
  1191. pipe = usbhs_dcp_malloc(priv);
  1192. usbhsh_hpriv_to_dcp(hpriv) = pipe;
  1193. } else {
  1194. pipe = usbhs_pipe_malloc(priv,
  1195. pipe_type[i],
  1196. dir_in);
  1197. }
  1198. pipe->mod_private = NULL;
  1199. }
  1200. }
  1201. static int usbhsh_start(struct usbhs_priv *priv)
  1202. {
  1203. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  1204. struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
  1205. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  1206. struct device *dev = usbhs_priv_to_dev(priv);
  1207. int ret;
  1208. /* add hcd */
  1209. ret = usb_add_hcd(hcd, 0, 0);
  1210. if (ret < 0)
  1211. return 0;
  1212. device_wakeup_enable(hcd->self.controller);
  1213. /*
  1214. * pipe initialize and enable DCP
  1215. */
  1216. usbhs_fifo_init(priv);
  1217. usbhs_pipe_init(priv,
  1218. usbhsh_dma_map_ctrl);
  1219. usbhsh_pipe_init_for_host(priv);
  1220. /*
  1221. * system config enble
  1222. * - HI speed
  1223. * - host
  1224. * - usb module
  1225. */
  1226. usbhs_sys_host_ctrl(priv, 1);
  1227. /*
  1228. * enable irq callback
  1229. */
  1230. mod->irq_attch = usbhsh_irq_attch;
  1231. mod->irq_dtch = usbhsh_irq_dtch;
  1232. mod->irq_sack = usbhsh_irq_setup_ack;
  1233. mod->irq_sign = usbhsh_irq_setup_err;
  1234. usbhs_irq_callback_update(priv, mod);
  1235. dev_dbg(dev, "start host\n");
  1236. return ret;
  1237. }
  1238. static int usbhsh_stop(struct usbhs_priv *priv)
  1239. {
  1240. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  1241. struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
  1242. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  1243. struct device *dev = usbhs_priv_to_dev(priv);
  1244. /*
  1245. * disable irq callback
  1246. */
  1247. mod->irq_attch = NULL;
  1248. mod->irq_dtch = NULL;
  1249. mod->irq_sack = NULL;
  1250. mod->irq_sign = NULL;
  1251. usbhs_irq_callback_update(priv, mod);
  1252. usb_remove_hcd(hcd);
  1253. /* disable sys */
  1254. usbhs_sys_host_ctrl(priv, 0);
  1255. dev_dbg(dev, "quit host\n");
  1256. return 0;
  1257. }
  1258. int usbhs_mod_host_probe(struct usbhs_priv *priv)
  1259. {
  1260. struct usbhsh_hpriv *hpriv;
  1261. struct usb_hcd *hcd;
  1262. struct usbhsh_device *udev;
  1263. struct device *dev = usbhs_priv_to_dev(priv);
  1264. int i;
  1265. /* initialize hcd */
  1266. hcd = usb_create_hcd(&usbhsh_driver, dev, usbhsh_hcd_name);
  1267. if (!hcd) {
  1268. dev_err(dev, "Failed to create hcd\n");
  1269. return -ENOMEM;
  1270. }
  1271. hcd->has_tt = 1; /* for low/full speed */
  1272. /*
  1273. * CAUTION
  1274. *
  1275. * There is no guarantee that it is possible to access usb module here.
  1276. * Don't accesses to it.
  1277. * The accesse will be enable after "usbhsh_start"
  1278. */
  1279. hpriv = usbhsh_hcd_to_hpriv(hcd);
  1280. /*
  1281. * register itself
  1282. */
  1283. usbhs_mod_register(priv, &hpriv->mod, USBHS_HOST);
  1284. /* init hpriv */
  1285. hpriv->mod.name = "host";
  1286. hpriv->mod.start = usbhsh_start;
  1287. hpriv->mod.stop = usbhsh_stop;
  1288. usbhsh_port_stat_init(hpriv);
  1289. /* init all device */
  1290. usbhsh_for_each_udev_with_dev0(udev, hpriv, i) {
  1291. udev->usbv = NULL;
  1292. INIT_LIST_HEAD(&udev->ep_list_head);
  1293. }
  1294. dev_info(dev, "host probed\n");
  1295. return 0;
  1296. }
  1297. int usbhs_mod_host_remove(struct usbhs_priv *priv)
  1298. {
  1299. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  1300. struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
  1301. usb_put_hcd(hcd);
  1302. return 0;
  1303. }