mod_host.c 37 KB

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