mod_host.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512
  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 usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep);
  526. struct device *dev = usbhs_priv_to_dev(priv);
  527. dev_dbg(dev, "%s\n", __func__);
  528. if (!urb) {
  529. dev_warn(dev, "pkt doesn't have urb\n");
  530. return;
  531. }
  532. urb->actual_length = pkt->actual;
  533. usbhsh_ureq_free(hpriv, ureq);
  534. usbhsh_endpoint_sequence_save(hpriv, urb, pkt);
  535. usbhsh_pipe_detach(hpriv, uep);
  536. usb_hcd_unlink_urb_from_ep(hcd, urb);
  537. usb_hcd_giveback_urb(hcd, urb, 0);
  538. }
  539. static int usbhsh_queue_push(struct usb_hcd *hcd,
  540. struct urb *urb,
  541. gfp_t mem_flags)
  542. {
  543. struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
  544. struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep);
  545. struct usbhs_pipe *pipe = usbhsh_uep_to_pipe(uep);
  546. struct device *dev = usbhsh_hcd_to_dev(hcd);
  547. struct usbhsh_request *ureq;
  548. void *buf;
  549. int len, sequence;
  550. if (usb_pipeisoc(urb->pipe)) {
  551. dev_err(dev, "pipe iso is not supported now\n");
  552. return -EIO;
  553. }
  554. /* this ureq will be freed on usbhsh_queue_done() */
  555. ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
  556. if (unlikely(!ureq)) {
  557. dev_err(dev, "ureq alloc fail\n");
  558. return -ENOMEM;
  559. }
  560. if (usb_pipein(urb->pipe))
  561. pipe->handler = &usbhs_fifo_pio_pop_handler;
  562. else
  563. pipe->handler = &usbhs_fifo_pio_push_handler;
  564. buf = (void *)(urb->transfer_buffer + urb->actual_length);
  565. len = urb->transfer_buffer_length - urb->actual_length;
  566. sequence = usb_gettoggle(urb->dev,
  567. usb_pipeendpoint(urb->pipe),
  568. usb_pipeout(urb->pipe));
  569. dev_dbg(dev, "%s\n", __func__);
  570. usbhs_pkt_push(pipe, &ureq->pkt, usbhsh_queue_done,
  571. buf, len, (urb->transfer_flags & URB_ZERO_PACKET),
  572. sequence);
  573. usbhs_pkt_start(pipe);
  574. return 0;
  575. }
  576. /*
  577. * DCP setup stage
  578. */
  579. static int usbhsh_is_request_address(struct urb *urb)
  580. {
  581. struct usb_ctrlrequest *req;
  582. req = (struct usb_ctrlrequest *)urb->setup_packet;
  583. if ((DeviceOutRequest == req->bRequestType << 8) &&
  584. (USB_REQ_SET_ADDRESS == req->bRequest))
  585. return 1;
  586. else
  587. return 0;
  588. }
  589. static void usbhsh_setup_stage_packet_push(struct usbhsh_hpriv *hpriv,
  590. struct urb *urb,
  591. struct usbhs_pipe *pipe)
  592. {
  593. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  594. struct usb_ctrlrequest req;
  595. struct device *dev = usbhs_priv_to_dev(priv);
  596. /*
  597. * wait setup packet ACK
  598. * see
  599. * usbhsh_irq_setup_ack()
  600. * usbhsh_irq_setup_err()
  601. */
  602. init_completion(&hpriv->setup_ack_done);
  603. /* copy original request */
  604. memcpy(&req, urb->setup_packet, sizeof(struct usb_ctrlrequest));
  605. /*
  606. * renesas_usbhs can not use original usb address.
  607. * see HARDWARE LIMITATION.
  608. * modify usb address here to use attached device.
  609. * see usbhsh_device_attach()
  610. */
  611. if (usbhsh_is_request_address(urb)) {
  612. struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
  613. struct usbhsh_device *udev = usbhsh_usbv_to_udev(usbv);
  614. /* udev is a attached device */
  615. req.wValue = usbhsh_device_number(hpriv, udev);
  616. dev_dbg(dev, "create new address - %d\n", req.wValue);
  617. }
  618. /* set request */
  619. usbhs_usbreq_set_val(priv, &req);
  620. /*
  621. * wait setup packet ACK
  622. */
  623. wait_for_completion(&hpriv->setup_ack_done);
  624. dev_dbg(dev, "%s done\n", __func__);
  625. }
  626. /*
  627. * DCP data stage
  628. */
  629. static void usbhsh_data_stage_packet_done(struct usbhs_priv *priv,
  630. struct usbhs_pkt *pkt)
  631. {
  632. struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt);
  633. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  634. /* this ureq was connected to urb when usbhsh_urb_enqueue() */
  635. usbhsh_ureq_free(hpriv, ureq);
  636. }
  637. static int usbhsh_data_stage_packet_push(struct usbhsh_hpriv *hpriv,
  638. struct urb *urb,
  639. struct usbhs_pipe *pipe,
  640. gfp_t mem_flags)
  641. {
  642. struct usbhsh_request *ureq;
  643. /* this ureq will be freed on usbhsh_data_stage_packet_done() */
  644. ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
  645. if (unlikely(!ureq))
  646. return -ENOMEM;
  647. if (usb_pipein(urb->pipe))
  648. pipe->handler = &usbhs_dcp_data_stage_in_handler;
  649. else
  650. pipe->handler = &usbhs_dcp_data_stage_out_handler;
  651. usbhs_pkt_push(pipe, &ureq->pkt,
  652. usbhsh_data_stage_packet_done,
  653. urb->transfer_buffer,
  654. urb->transfer_buffer_length,
  655. (urb->transfer_flags & URB_ZERO_PACKET),
  656. -1);
  657. return 0;
  658. }
  659. /*
  660. * DCP status stage
  661. */
  662. static int usbhsh_status_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_queue_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_status_stage_in_handler;
  674. else
  675. pipe->handler = &usbhs_dcp_status_stage_out_handler;
  676. usbhs_pkt_push(pipe, &ureq->pkt,
  677. usbhsh_queue_done,
  678. NULL,
  679. urb->transfer_buffer_length,
  680. 0, -1);
  681. return 0;
  682. }
  683. static int usbhsh_dcp_queue_push(struct usb_hcd *hcd,
  684. struct urb *urb,
  685. gfp_t mflags)
  686. {
  687. struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
  688. struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep);
  689. struct usbhs_pipe *pipe = usbhsh_uep_to_pipe(uep);
  690. struct device *dev = usbhsh_hcd_to_dev(hcd);
  691. int ret;
  692. dev_dbg(dev, "%s\n", __func__);
  693. /*
  694. * setup stage
  695. *
  696. * usbhsh_send_setup_stage_packet() wait SACK/SIGN
  697. */
  698. usbhsh_setup_stage_packet_push(hpriv, urb, pipe);
  699. /*
  700. * data stage
  701. *
  702. * It is pushed only when urb has buffer.
  703. */
  704. if (urb->transfer_buffer_length) {
  705. ret = usbhsh_data_stage_packet_push(hpriv, urb, pipe, mflags);
  706. if (ret < 0) {
  707. dev_err(dev, "data stage failed\n");
  708. return ret;
  709. }
  710. }
  711. /*
  712. * status stage
  713. */
  714. ret = usbhsh_status_stage_packet_push(hpriv, urb, pipe, mflags);
  715. if (ret < 0) {
  716. dev_err(dev, "status stage failed\n");
  717. return ret;
  718. }
  719. /*
  720. * start pushed packets
  721. */
  722. usbhs_pkt_start(pipe);
  723. return 0;
  724. }
  725. /*
  726. * dma map functions
  727. */
  728. static int usbhsh_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
  729. {
  730. return 0;
  731. }
  732. /*
  733. * for hc_driver
  734. */
  735. static int usbhsh_host_start(struct usb_hcd *hcd)
  736. {
  737. return 0;
  738. }
  739. static void usbhsh_host_stop(struct usb_hcd *hcd)
  740. {
  741. }
  742. static int usbhsh_urb_enqueue(struct usb_hcd *hcd,
  743. struct urb *urb,
  744. gfp_t mem_flags)
  745. {
  746. struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
  747. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  748. struct device *dev = usbhs_priv_to_dev(priv);
  749. struct usb_host_endpoint *ep = urb->ep;
  750. struct usbhsh_device *new_udev = NULL;
  751. int is_dir_in = usb_pipein(urb->pipe);
  752. int i;
  753. int ret;
  754. dev_dbg(dev, "%s (%s)\n", __func__, is_dir_in ? "in" : "out");
  755. if (!usbhsh_is_running(hpriv)) {
  756. ret = -EIO;
  757. goto usbhsh_urb_enqueue_error_not_linked;
  758. }
  759. ret = usb_hcd_link_urb_to_ep(hcd, urb);
  760. if (ret)
  761. goto usbhsh_urb_enqueue_error_not_linked;
  762. /*
  763. * attach udev if needed
  764. * see [image of mod_host]
  765. */
  766. if (!usbhsh_device_get(hpriv, urb)) {
  767. new_udev = usbhsh_device_attach(hpriv, urb);
  768. if (!new_udev) {
  769. ret = -EIO;
  770. goto usbhsh_urb_enqueue_error_not_linked;
  771. }
  772. }
  773. /*
  774. * attach endpoint if needed
  775. * see [image of mod_host]
  776. */
  777. if (!usbhsh_ep_to_uep(ep)) {
  778. ret = usbhsh_endpoint_attach(hpriv, urb, mem_flags);
  779. if (ret < 0)
  780. goto usbhsh_urb_enqueue_error_free_device;
  781. }
  782. /*
  783. * attach pipe to endpoint
  784. * see [image of mod_host]
  785. */
  786. for (i = 0; i < 1024; i++) {
  787. ret = usbhsh_pipe_attach(hpriv, urb);
  788. if (ret < 0)
  789. msleep(100);
  790. else
  791. break;
  792. }
  793. if (ret < 0)
  794. goto usbhsh_urb_enqueue_error_free_endpoint;
  795. /*
  796. * push packet
  797. */
  798. if (usb_pipecontrol(urb->pipe))
  799. ret = usbhsh_dcp_queue_push(hcd, urb, mem_flags);
  800. else
  801. ret = usbhsh_queue_push(hcd, urb, mem_flags);
  802. return ret;
  803. usbhsh_urb_enqueue_error_free_endpoint:
  804. usbhsh_endpoint_detach(hpriv, ep);
  805. usbhsh_urb_enqueue_error_free_device:
  806. if (new_udev)
  807. usbhsh_device_detach(hpriv, new_udev);
  808. usbhsh_urb_enqueue_error_not_linked:
  809. dev_dbg(dev, "%s error\n", __func__);
  810. return ret;
  811. }
  812. static int usbhsh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  813. {
  814. struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
  815. struct usbhsh_request *ureq = usbhsh_urb_to_ureq(urb);
  816. if (ureq) {
  817. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  818. struct usbhs_pkt *pkt = &ureq->pkt;
  819. usbhs_pkt_pop(pkt->pipe, pkt);
  820. usbhsh_queue_done(priv, pkt);
  821. }
  822. return 0;
  823. }
  824. static void usbhsh_endpoint_disable(struct usb_hcd *hcd,
  825. struct usb_host_endpoint *ep)
  826. {
  827. struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep);
  828. struct usbhsh_device *udev;
  829. struct usbhsh_hpriv *hpriv;
  830. /*
  831. * this function might be called manytimes by same hcd/ep
  832. * in-endpoint == out-endpoint if ep == dcp.
  833. */
  834. if (!uep)
  835. return;
  836. udev = usbhsh_uep_to_udev(uep);
  837. hpriv = usbhsh_hcd_to_hpriv(hcd);
  838. usbhsh_endpoint_detach(hpriv, ep);
  839. /*
  840. * if there is no endpoint,
  841. * free device
  842. */
  843. if (!usbhsh_device_has_endpoint(udev))
  844. usbhsh_device_detach(hpriv, udev);
  845. }
  846. static int usbhsh_hub_status_data(struct usb_hcd *hcd, char *buf)
  847. {
  848. struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
  849. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  850. struct device *dev = usbhs_priv_to_dev(priv);
  851. int roothub_id = 1; /* only 1 root hub */
  852. /*
  853. * does port stat was changed ?
  854. * check USB_PORT_STAT_C_xxx << 16
  855. */
  856. if (usbhsh_port_stat_get(hpriv) & 0xFFFF0000)
  857. *buf = (1 << roothub_id);
  858. else
  859. *buf = 0;
  860. dev_dbg(dev, "%s (%02x)\n", __func__, *buf);
  861. return !!(*buf);
  862. }
  863. static int __usbhsh_hub_hub_feature(struct usbhsh_hpriv *hpriv,
  864. u16 typeReq, u16 wValue,
  865. u16 wIndex, char *buf, u16 wLength)
  866. {
  867. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  868. struct device *dev = usbhs_priv_to_dev(priv);
  869. switch (wValue) {
  870. case C_HUB_OVER_CURRENT:
  871. case C_HUB_LOCAL_POWER:
  872. dev_dbg(dev, "%s :: C_HUB_xx\n", __func__);
  873. return 0;
  874. }
  875. return -EPIPE;
  876. }
  877. static int __usbhsh_hub_port_feature(struct usbhsh_hpriv *hpriv,
  878. u16 typeReq, u16 wValue,
  879. u16 wIndex, char *buf, u16 wLength)
  880. {
  881. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  882. struct device *dev = usbhs_priv_to_dev(priv);
  883. int enable = (typeReq == SetPortFeature);
  884. int speed, i, timeout = 128;
  885. int roothub_id = 1; /* only 1 root hub */
  886. /* common error */
  887. if (wIndex > roothub_id || wLength != 0)
  888. return -EPIPE;
  889. /* check wValue */
  890. switch (wValue) {
  891. case USB_PORT_FEAT_POWER:
  892. usbhs_vbus_ctrl(priv, enable);
  893. dev_dbg(dev, "%s :: USB_PORT_FEAT_POWER\n", __func__);
  894. break;
  895. case USB_PORT_FEAT_ENABLE:
  896. case USB_PORT_FEAT_SUSPEND:
  897. case USB_PORT_FEAT_C_ENABLE:
  898. case USB_PORT_FEAT_C_SUSPEND:
  899. case USB_PORT_FEAT_C_CONNECTION:
  900. case USB_PORT_FEAT_C_OVER_CURRENT:
  901. case USB_PORT_FEAT_C_RESET:
  902. dev_dbg(dev, "%s :: USB_PORT_FEAT_xxx\n", __func__);
  903. break;
  904. case USB_PORT_FEAT_RESET:
  905. if (!enable)
  906. break;
  907. usbhsh_port_stat_clear(hpriv,
  908. USB_PORT_STAT_HIGH_SPEED |
  909. USB_PORT_STAT_LOW_SPEED);
  910. usbhs_bus_send_reset(priv);
  911. msleep(20);
  912. usbhs_bus_send_sof_enable(priv);
  913. for (i = 0; i < timeout ; i++) {
  914. switch (usbhs_bus_get_speed(priv)) {
  915. case USB_SPEED_LOW:
  916. speed = USB_PORT_STAT_LOW_SPEED;
  917. goto got_usb_bus_speed;
  918. case USB_SPEED_HIGH:
  919. speed = USB_PORT_STAT_HIGH_SPEED;
  920. goto got_usb_bus_speed;
  921. case USB_SPEED_FULL:
  922. speed = 0;
  923. goto got_usb_bus_speed;
  924. }
  925. msleep(20);
  926. }
  927. return -EPIPE;
  928. got_usb_bus_speed:
  929. usbhsh_port_stat_set(hpriv, speed);
  930. usbhsh_port_stat_set(hpriv, USB_PORT_STAT_ENABLE);
  931. dev_dbg(dev, "%s :: USB_PORT_FEAT_RESET (speed = %d)\n",
  932. __func__, speed);
  933. /* status change is not needed */
  934. return 0;
  935. default:
  936. return -EPIPE;
  937. }
  938. /* set/clear status */
  939. if (enable)
  940. usbhsh_port_stat_set(hpriv, (1 << wValue));
  941. else
  942. usbhsh_port_stat_clear(hpriv, (1 << wValue));
  943. return 0;
  944. }
  945. static int __usbhsh_hub_get_status(struct usbhsh_hpriv *hpriv,
  946. u16 typeReq, u16 wValue,
  947. u16 wIndex, char *buf, u16 wLength)
  948. {
  949. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  950. struct usb_hub_descriptor *desc = (struct usb_hub_descriptor *)buf;
  951. struct device *dev = usbhs_priv_to_dev(priv);
  952. int roothub_id = 1; /* only 1 root hub */
  953. switch (typeReq) {
  954. case GetHubStatus:
  955. dev_dbg(dev, "%s :: GetHubStatus\n", __func__);
  956. *buf = 0x00;
  957. break;
  958. case GetPortStatus:
  959. if (wIndex != roothub_id)
  960. return -EPIPE;
  961. dev_dbg(dev, "%s :: GetPortStatus\n", __func__);
  962. *(__le32 *)buf = cpu_to_le32(usbhsh_port_stat_get(hpriv));
  963. break;
  964. case GetHubDescriptor:
  965. desc->bDescriptorType = 0x29;
  966. desc->bHubContrCurrent = 0;
  967. desc->bNbrPorts = roothub_id;
  968. desc->bDescLength = 9;
  969. desc->bPwrOn2PwrGood = 0;
  970. desc->wHubCharacteristics = cpu_to_le16(0x0011);
  971. desc->u.hs.DeviceRemovable[0] = (roothub_id << 1);
  972. desc->u.hs.DeviceRemovable[1] = ~0;
  973. dev_dbg(dev, "%s :: GetHubDescriptor\n", __func__);
  974. break;
  975. }
  976. return 0;
  977. }
  978. static int usbhsh_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  979. u16 wIndex, char *buf, u16 wLength)
  980. {
  981. struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
  982. struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
  983. struct device *dev = usbhs_priv_to_dev(priv);
  984. int ret = -EPIPE;
  985. switch (typeReq) {
  986. /* Hub Feature */
  987. case ClearHubFeature:
  988. case SetHubFeature:
  989. ret = __usbhsh_hub_hub_feature(hpriv, typeReq,
  990. wValue, wIndex, buf, wLength);
  991. break;
  992. /* Port Feature */
  993. case SetPortFeature:
  994. case ClearPortFeature:
  995. ret = __usbhsh_hub_port_feature(hpriv, typeReq,
  996. wValue, wIndex, buf, wLength);
  997. break;
  998. /* Get status */
  999. case GetHubStatus:
  1000. case GetPortStatus:
  1001. case GetHubDescriptor:
  1002. ret = __usbhsh_hub_get_status(hpriv, typeReq,
  1003. wValue, wIndex, buf, wLength);
  1004. break;
  1005. }
  1006. dev_dbg(dev, "typeReq = %x, ret = %d, port_stat = %x\n",
  1007. typeReq, ret, usbhsh_port_stat_get(hpriv));
  1008. return ret;
  1009. }
  1010. static struct hc_driver usbhsh_driver = {
  1011. .description = usbhsh_hcd_name,
  1012. .hcd_priv_size = sizeof(struct usbhsh_hpriv),
  1013. /*
  1014. * generic hardware linkage
  1015. */
  1016. .flags = HCD_USB2,
  1017. .start = usbhsh_host_start,
  1018. .stop = usbhsh_host_stop,
  1019. /*
  1020. * managing i/o requests and associated device resources
  1021. */
  1022. .urb_enqueue = usbhsh_urb_enqueue,
  1023. .urb_dequeue = usbhsh_urb_dequeue,
  1024. .endpoint_disable = usbhsh_endpoint_disable,
  1025. /*
  1026. * root hub
  1027. */
  1028. .hub_status_data = usbhsh_hub_status_data,
  1029. .hub_control = usbhsh_hub_control,
  1030. };
  1031. /*
  1032. * interrupt functions
  1033. */
  1034. static int usbhsh_irq_attch(struct usbhs_priv *priv,
  1035. struct usbhs_irq_state *irq_state)
  1036. {
  1037. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  1038. struct device *dev = usbhs_priv_to_dev(priv);
  1039. dev_dbg(dev, "device attached\n");
  1040. usbhsh_port_stat_set(hpriv, USB_PORT_STAT_CONNECTION);
  1041. usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16);
  1042. /*
  1043. * attch interrupt might happen infinitely on some device
  1044. * (on self power USB hub ?)
  1045. * disable it here.
  1046. *
  1047. * usbhsh_is_running() becomes effective
  1048. * according to this process.
  1049. * see
  1050. * usbhsh_is_running()
  1051. * usbhsh_urb_enqueue()
  1052. */
  1053. hpriv->mod.irq_attch = NULL;
  1054. usbhs_irq_callback_update(priv, &hpriv->mod);
  1055. return 0;
  1056. }
  1057. static int usbhsh_irq_dtch(struct usbhs_priv *priv,
  1058. struct usbhs_irq_state *irq_state)
  1059. {
  1060. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  1061. struct device *dev = usbhs_priv_to_dev(priv);
  1062. dev_dbg(dev, "device detached\n");
  1063. usbhsh_port_stat_clear(hpriv, USB_PORT_STAT_CONNECTION);
  1064. usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16);
  1065. /*
  1066. * enable attch interrupt again
  1067. *
  1068. * usbhsh_is_running() becomes invalid
  1069. * according to this process.
  1070. * see
  1071. * usbhsh_is_running()
  1072. * usbhsh_urb_enqueue()
  1073. */
  1074. hpriv->mod.irq_attch = usbhsh_irq_attch;
  1075. usbhs_irq_callback_update(priv, &hpriv->mod);
  1076. return 0;
  1077. }
  1078. static int usbhsh_irq_setup_ack(struct usbhs_priv *priv,
  1079. struct usbhs_irq_state *irq_state)
  1080. {
  1081. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  1082. struct device *dev = usbhs_priv_to_dev(priv);
  1083. dev_dbg(dev, "setup packet OK\n");
  1084. complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */
  1085. return 0;
  1086. }
  1087. static int usbhsh_irq_setup_err(struct usbhs_priv *priv,
  1088. struct usbhs_irq_state *irq_state)
  1089. {
  1090. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  1091. struct device *dev = usbhs_priv_to_dev(priv);
  1092. dev_dbg(dev, "setup packet Err\n");
  1093. complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */
  1094. return 0;
  1095. }
  1096. /*
  1097. * module start/stop
  1098. */
  1099. static void usbhsh_pipe_init_for_host(struct usbhs_priv *priv)
  1100. {
  1101. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  1102. struct usbhs_pipe *pipe;
  1103. u32 *pipe_type = usbhs_get_dparam(priv, pipe_type);
  1104. int pipe_size = usbhs_get_dparam(priv, pipe_size);
  1105. int old_type, dir_in, i;
  1106. /* init all pipe */
  1107. old_type = USB_ENDPOINT_XFER_CONTROL;
  1108. for (i = 0; i < pipe_size; i++) {
  1109. /*
  1110. * data "output" will be finished as soon as possible,
  1111. * but there is no guaranty at data "input" case.
  1112. *
  1113. * "input" needs "standby" pipe.
  1114. * So, "input" direction pipe > "output" direction pipe
  1115. * is good idea.
  1116. *
  1117. * 1st USB_ENDPOINT_XFER_xxx will be output direction,
  1118. * and the other will be input direction here.
  1119. *
  1120. * ex)
  1121. * ...
  1122. * USB_ENDPOINT_XFER_ISOC -> dir out
  1123. * USB_ENDPOINT_XFER_ISOC -> dir in
  1124. * USB_ENDPOINT_XFER_BULK -> dir out
  1125. * USB_ENDPOINT_XFER_BULK -> dir in
  1126. * USB_ENDPOINT_XFER_BULK -> dir in
  1127. * ...
  1128. */
  1129. dir_in = (pipe_type[i] == old_type);
  1130. old_type = pipe_type[i];
  1131. if (USB_ENDPOINT_XFER_CONTROL == pipe_type[i]) {
  1132. pipe = usbhs_dcp_malloc(priv);
  1133. usbhsh_hpriv_to_dcp(hpriv) = pipe;
  1134. } else {
  1135. pipe = usbhs_pipe_malloc(priv,
  1136. pipe_type[i],
  1137. dir_in);
  1138. }
  1139. pipe->mod_private = NULL;
  1140. }
  1141. }
  1142. static int usbhsh_start(struct usbhs_priv *priv)
  1143. {
  1144. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  1145. struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
  1146. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  1147. struct device *dev = usbhs_priv_to_dev(priv);
  1148. int ret;
  1149. /* add hcd */
  1150. ret = usb_add_hcd(hcd, 0, 0);
  1151. if (ret < 0)
  1152. return 0;
  1153. /*
  1154. * pipe initialize and enable DCP
  1155. */
  1156. usbhs_pipe_init(priv,
  1157. usbhsh_dma_map_ctrl);
  1158. usbhs_fifo_init(priv);
  1159. usbhsh_pipe_init_for_host(priv);
  1160. /*
  1161. * system config enble
  1162. * - HI speed
  1163. * - host
  1164. * - usb module
  1165. */
  1166. usbhs_sys_host_ctrl(priv, 1);
  1167. /*
  1168. * enable irq callback
  1169. */
  1170. mod->irq_attch = usbhsh_irq_attch;
  1171. mod->irq_dtch = usbhsh_irq_dtch;
  1172. mod->irq_sack = usbhsh_irq_setup_ack;
  1173. mod->irq_sign = usbhsh_irq_setup_err;
  1174. usbhs_irq_callback_update(priv, mod);
  1175. dev_dbg(dev, "start host\n");
  1176. return ret;
  1177. }
  1178. static int usbhsh_stop(struct usbhs_priv *priv)
  1179. {
  1180. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  1181. struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
  1182. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  1183. struct device *dev = usbhs_priv_to_dev(priv);
  1184. /*
  1185. * disable irq callback
  1186. */
  1187. mod->irq_attch = NULL;
  1188. mod->irq_dtch = NULL;
  1189. mod->irq_sack = NULL;
  1190. mod->irq_sign = NULL;
  1191. usbhs_irq_callback_update(priv, mod);
  1192. usb_remove_hcd(hcd);
  1193. /* disable sys */
  1194. usbhs_sys_host_ctrl(priv, 0);
  1195. dev_dbg(dev, "quit host\n");
  1196. return 0;
  1197. }
  1198. int usbhs_mod_host_probe(struct usbhs_priv *priv)
  1199. {
  1200. struct usbhsh_hpriv *hpriv;
  1201. struct usb_hcd *hcd;
  1202. struct usbhsh_device *udev;
  1203. struct device *dev = usbhs_priv_to_dev(priv);
  1204. int i;
  1205. /* initialize hcd */
  1206. hcd = usb_create_hcd(&usbhsh_driver, dev, usbhsh_hcd_name);
  1207. if (!hcd) {
  1208. dev_err(dev, "Failed to create hcd\n");
  1209. return -ENOMEM;
  1210. }
  1211. /*
  1212. * CAUTION
  1213. *
  1214. * There is no guarantee that it is possible to access usb module here.
  1215. * Don't accesses to it.
  1216. * The accesse will be enable after "usbhsh_start"
  1217. */
  1218. hpriv = usbhsh_hcd_to_hpriv(hcd);
  1219. /*
  1220. * register itself
  1221. */
  1222. usbhs_mod_register(priv, &hpriv->mod, USBHS_HOST);
  1223. /* init hpriv */
  1224. hpriv->mod.name = "host";
  1225. hpriv->mod.start = usbhsh_start;
  1226. hpriv->mod.stop = usbhsh_stop;
  1227. usbhsh_port_stat_init(hpriv);
  1228. /* init all device */
  1229. usbhsh_for_each_udev_with_dev0(udev, hpriv, i) {
  1230. udev->usbv = NULL;
  1231. INIT_LIST_HEAD(&udev->ep_list_head);
  1232. }
  1233. dev_info(dev, "host probed\n");
  1234. return 0;
  1235. }
  1236. int usbhs_mod_host_remove(struct usbhs_priv *priv)
  1237. {
  1238. struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
  1239. struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
  1240. usb_put_hcd(hcd);
  1241. return 0;
  1242. }