mod_gadget.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/usb/ch9.h>
  21. #include <linux/usb/gadget.h>
  22. #include "common.h"
  23. /*
  24. * struct
  25. */
  26. struct usbhsg_request {
  27. struct usb_request req;
  28. struct usbhs_pkt pkt;
  29. };
  30. #define EP_NAME_SIZE 8
  31. struct usbhsg_gpriv;
  32. struct usbhsg_uep {
  33. struct usb_ep ep;
  34. struct usbhs_pipe *pipe;
  35. char ep_name[EP_NAME_SIZE];
  36. struct usbhsg_gpriv *gpriv;
  37. struct usbhs_pkt_handle *handler;
  38. };
  39. struct usbhsg_gpriv {
  40. struct usb_gadget gadget;
  41. struct usbhs_mod mod;
  42. struct usbhsg_uep *uep;
  43. int uep_size;
  44. struct usb_gadget_driver *driver;
  45. u32 status;
  46. #define USBHSG_STATUS_STARTED (1 << 0)
  47. #define USBHSG_STATUS_REGISTERD (1 << 1)
  48. #define USBHSG_STATUS_WEDGE (1 << 2)
  49. };
  50. struct usbhsg_recip_handle {
  51. char *name;
  52. int (*device)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
  53. struct usb_ctrlrequest *ctrl);
  54. int (*interface)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
  55. struct usb_ctrlrequest *ctrl);
  56. int (*endpoint)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
  57. struct usb_ctrlrequest *ctrl);
  58. };
  59. /*
  60. * macro
  61. */
  62. #define usbhsg_priv_to_gpriv(priv) \
  63. container_of( \
  64. usbhs_mod_get(priv, USBHS_GADGET), \
  65. struct usbhsg_gpriv, mod)
  66. #define __usbhsg_for_each_uep(start, pos, g, i) \
  67. for (i = start, pos = (g)->uep; \
  68. i < (g)->uep_size; \
  69. i++, pos = (g)->uep + i)
  70. #define usbhsg_for_each_uep(pos, gpriv, i) \
  71. __usbhsg_for_each_uep(1, pos, gpriv, i)
  72. #define usbhsg_for_each_uep_with_dcp(pos, gpriv, i) \
  73. __usbhsg_for_each_uep(0, pos, gpriv, i)
  74. #define usbhsg_gadget_to_gpriv(g)\
  75. container_of(g, struct usbhsg_gpriv, gadget)
  76. #define usbhsg_req_to_ureq(r)\
  77. container_of(r, struct usbhsg_request, req)
  78. #define usbhsg_ep_to_uep(e) container_of(e, struct usbhsg_uep, ep)
  79. #define usbhsg_gpriv_to_dev(gp) usbhs_priv_to_dev((gp)->mod.priv)
  80. #define usbhsg_gpriv_to_priv(gp) ((gp)->mod.priv)
  81. #define usbhsg_gpriv_to_dcp(gp) ((gp)->uep)
  82. #define usbhsg_gpriv_to_nth_uep(gp, i) ((gp)->uep + i)
  83. #define usbhsg_uep_to_gpriv(u) ((u)->gpriv)
  84. #define usbhsg_uep_to_pipe(u) ((u)->pipe)
  85. #define usbhsg_pipe_to_uep(p) ((p)->mod_private)
  86. #define usbhsg_is_dcp(u) ((u) == usbhsg_gpriv_to_dcp((u)->gpriv))
  87. #define usbhsg_ureq_to_pkt(u) (&(u)->pkt)
  88. #define usbhsg_pkt_to_ureq(i) \
  89. container_of(i, struct usbhsg_request, pkt)
  90. #define usbhsg_is_not_connected(gp) ((gp)->gadget.speed == USB_SPEED_UNKNOWN)
  91. /* status */
  92. #define usbhsg_status_init(gp) do {(gp)->status = 0; } while (0)
  93. #define usbhsg_status_set(gp, b) (gp->status |= b)
  94. #define usbhsg_status_clr(gp, b) (gp->status &= ~b)
  95. #define usbhsg_status_has(gp, b) (gp->status & b)
  96. /*
  97. * list push/pop
  98. */
  99. static void usbhsg_queue_push(struct usbhsg_uep *uep,
  100. struct usbhsg_request *ureq)
  101. {
  102. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  103. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  104. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  105. struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
  106. struct usb_request *req = &ureq->req;
  107. usbhs_pkt_push(pipe, pkt, uep->handler,
  108. req->buf, req->length, req->zero);
  109. req->actual = 0;
  110. req->status = -EINPROGRESS;
  111. dev_dbg(dev, "pipe %d : queue push (%d)\n",
  112. usbhs_pipe_number(pipe),
  113. req->length);
  114. }
  115. static void usbhsg_queue_start(struct usbhsg_uep *uep)
  116. {
  117. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  118. usbhs_pkt_start(pipe);
  119. }
  120. static void usbhsg_queue_pop(struct usbhsg_uep *uep,
  121. struct usbhsg_request *ureq,
  122. int status)
  123. {
  124. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  125. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  126. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  127. dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
  128. ureq->req.status = status;
  129. ureq->req.complete(&uep->ep, &ureq->req);
  130. /* more request ? */
  131. if (0 == status)
  132. usbhsg_queue_start(uep);
  133. }
  134. static void usbhsg_queue_done(struct usbhs_pkt *pkt)
  135. {
  136. struct usbhs_pipe *pipe = pkt->pipe;
  137. struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
  138. struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
  139. ureq->req.actual = pkt->actual;
  140. usbhsg_queue_pop(uep, ureq, 0);
  141. }
  142. /*
  143. * USB_TYPE_STANDARD / clear feature functions
  144. */
  145. static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
  146. struct usbhsg_uep *uep,
  147. struct usb_ctrlrequest *ctrl)
  148. {
  149. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  150. struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
  151. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
  152. usbhs_dcp_control_transfer_done(pipe);
  153. return 0;
  154. }
  155. static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
  156. struct usbhsg_uep *uep,
  157. struct usb_ctrlrequest *ctrl)
  158. {
  159. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  160. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  161. if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
  162. usbhs_pipe_disable(pipe);
  163. usbhs_pipe_clear_sequence(pipe);
  164. usbhs_pipe_enable(pipe);
  165. }
  166. usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
  167. return 0;
  168. }
  169. struct usbhsg_recip_handle req_clear_feature = {
  170. .name = "clear feature",
  171. .device = usbhsg_recip_handler_std_control_done,
  172. .interface = usbhsg_recip_handler_std_control_done,
  173. .endpoint = usbhsg_recip_handler_std_clear_endpoint,
  174. };
  175. /*
  176. * USB_TYPE handler
  177. */
  178. static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
  179. struct usbhsg_recip_handle *handler,
  180. struct usb_ctrlrequest *ctrl)
  181. {
  182. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  183. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  184. struct usbhsg_uep *uep;
  185. int recip = ctrl->bRequestType & USB_RECIP_MASK;
  186. int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
  187. int ret;
  188. int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
  189. struct usb_ctrlrequest *ctrl);
  190. char *msg;
  191. uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
  192. if (!usbhsg_uep_to_pipe(uep)) {
  193. dev_err(dev, "wrong recip request\n");
  194. ret = -EINVAL;
  195. goto usbhsg_recip_run_handle_end;
  196. }
  197. switch (recip) {
  198. case USB_RECIP_DEVICE:
  199. msg = "DEVICE";
  200. func = handler->device;
  201. break;
  202. case USB_RECIP_INTERFACE:
  203. msg = "INTERFACE";
  204. func = handler->interface;
  205. break;
  206. case USB_RECIP_ENDPOINT:
  207. msg = "ENDPOINT";
  208. func = handler->endpoint;
  209. break;
  210. default:
  211. dev_warn(dev, "unsupported RECIP(%d)\n", recip);
  212. func = NULL;
  213. ret = -EINVAL;
  214. }
  215. if (func) {
  216. unsigned long flags;
  217. dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
  218. /******************** spin lock ********************/
  219. usbhs_lock(priv, flags);
  220. ret = func(priv, uep, ctrl);
  221. usbhs_unlock(priv, flags);
  222. /******************** spin unlock ******************/
  223. }
  224. usbhsg_recip_run_handle_end:
  225. usbhsg_queue_start(uep);
  226. return ret;
  227. }
  228. /*
  229. * irq functions
  230. *
  231. * it will be called from usbhs_interrupt
  232. */
  233. static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
  234. struct usbhs_irq_state *irq_state)
  235. {
  236. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  237. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  238. gpriv->gadget.speed = usbhs_status_get_usb_speed(irq_state);
  239. dev_dbg(dev, "state = %x : speed : %d\n",
  240. usbhs_status_get_device_state(irq_state),
  241. gpriv->gadget.speed);
  242. return 0;
  243. }
  244. static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
  245. struct usbhs_irq_state *irq_state)
  246. {
  247. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  248. struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
  249. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
  250. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  251. struct usb_ctrlrequest ctrl;
  252. struct usbhsg_recip_handle *recip_handler = NULL;
  253. int stage = usbhs_status_get_ctrl_stage(irq_state);
  254. int ret = 0;
  255. dev_dbg(dev, "stage = %d\n", stage);
  256. /*
  257. * see Manual
  258. *
  259. * "Operation"
  260. * - "Interrupt Function"
  261. * - "Control Transfer Stage Transition Interrupt"
  262. * - Fig. "Control Transfer Stage Transitions"
  263. */
  264. switch (stage) {
  265. case READ_DATA_STAGE:
  266. dcp->handler = &usbhs_fifo_push_handler;
  267. break;
  268. case WRITE_DATA_STAGE:
  269. dcp->handler = &usbhs_fifo_pop_handler;
  270. break;
  271. case NODATA_STATUS_STAGE:
  272. dcp->handler = &usbhs_ctrl_stage_end_handler;
  273. break;
  274. default:
  275. return ret;
  276. }
  277. /*
  278. * get usb request
  279. */
  280. usbhs_usbreq_get_val(priv, &ctrl);
  281. switch (ctrl.bRequestType & USB_TYPE_MASK) {
  282. case USB_TYPE_STANDARD:
  283. switch (ctrl.bRequest) {
  284. case USB_REQ_CLEAR_FEATURE:
  285. recip_handler = &req_clear_feature;
  286. break;
  287. }
  288. }
  289. /*
  290. * setup stage / run recip
  291. */
  292. if (recip_handler)
  293. ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
  294. else
  295. ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
  296. if (ret < 0)
  297. usbhs_pipe_stall(pipe);
  298. return ret;
  299. }
  300. /*
  301. *
  302. * usb_dcp_ops
  303. *
  304. */
  305. static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
  306. {
  307. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  308. struct usbhs_pkt *pkt;
  309. usbhs_pipe_disable(pipe);
  310. while (1) {
  311. pkt = usbhs_pkt_pop(pipe, NULL);
  312. if (!pkt)
  313. break;
  314. }
  315. return 0;
  316. }
  317. static void usbhsg_uep_init(struct usbhsg_gpriv *gpriv)
  318. {
  319. int i;
  320. struct usbhsg_uep *uep;
  321. usbhsg_for_each_uep_with_dcp(uep, gpriv, i)
  322. uep->pipe = NULL;
  323. }
  324. /*
  325. *
  326. * usb_ep_ops
  327. *
  328. */
  329. static int usbhsg_ep_enable(struct usb_ep *ep,
  330. const struct usb_endpoint_descriptor *desc)
  331. {
  332. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  333. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  334. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  335. struct usbhs_pipe *pipe;
  336. int ret = -EIO;
  337. /*
  338. * if it already have pipe,
  339. * nothing to do
  340. */
  341. if (uep->pipe)
  342. return 0;
  343. pipe = usbhs_pipe_malloc(priv, desc);
  344. if (pipe) {
  345. uep->pipe = pipe;
  346. pipe->mod_private = uep;
  347. if (usb_endpoint_dir_in(desc))
  348. uep->handler = &usbhs_fifo_push_handler;
  349. else
  350. uep->handler = &usbhs_fifo_pop_handler;
  351. ret = 0;
  352. }
  353. return ret;
  354. }
  355. static int usbhsg_ep_disable(struct usb_ep *ep)
  356. {
  357. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  358. return usbhsg_pipe_disable(uep);
  359. }
  360. static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
  361. gfp_t gfp_flags)
  362. {
  363. struct usbhsg_request *ureq;
  364. ureq = kzalloc(sizeof *ureq, gfp_flags);
  365. if (!ureq)
  366. return NULL;
  367. usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
  368. return &ureq->req;
  369. }
  370. static void usbhsg_ep_free_request(struct usb_ep *ep,
  371. struct usb_request *req)
  372. {
  373. struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
  374. WARN_ON(!list_empty(&ureq->pkt.node));
  375. kfree(ureq);
  376. }
  377. static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
  378. gfp_t gfp_flags)
  379. {
  380. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  381. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  382. struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
  383. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  384. /* param check */
  385. if (usbhsg_is_not_connected(gpriv) ||
  386. unlikely(!gpriv->driver) ||
  387. unlikely(!pipe))
  388. return -ESHUTDOWN;
  389. usbhsg_queue_push(uep, ureq);
  390. usbhsg_queue_start(uep);
  391. return 0;
  392. }
  393. static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
  394. {
  395. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  396. struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
  397. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  398. usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq));
  399. usbhsg_queue_pop(uep, ureq, -ECONNRESET);
  400. return 0;
  401. }
  402. static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
  403. {
  404. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  405. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  406. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  407. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  408. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  409. unsigned long flags;
  410. usbhsg_pipe_disable(uep);
  411. dev_dbg(dev, "set halt %d (pipe %d)\n",
  412. halt, usbhs_pipe_number(pipe));
  413. /******************** spin lock ********************/
  414. usbhs_lock(priv, flags);
  415. if (halt)
  416. usbhs_pipe_stall(pipe);
  417. else
  418. usbhs_pipe_disable(pipe);
  419. if (halt && wedge)
  420. usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
  421. else
  422. usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
  423. usbhs_unlock(priv, flags);
  424. /******************** spin unlock ******************/
  425. return 0;
  426. }
  427. static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
  428. {
  429. return __usbhsg_ep_set_halt_wedge(ep, value, 0);
  430. }
  431. static int usbhsg_ep_set_wedge(struct usb_ep *ep)
  432. {
  433. return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
  434. }
  435. static struct usb_ep_ops usbhsg_ep_ops = {
  436. .enable = usbhsg_ep_enable,
  437. .disable = usbhsg_ep_disable,
  438. .alloc_request = usbhsg_ep_alloc_request,
  439. .free_request = usbhsg_ep_free_request,
  440. .queue = usbhsg_ep_queue,
  441. .dequeue = usbhsg_ep_dequeue,
  442. .set_halt = usbhsg_ep_set_halt,
  443. .set_wedge = usbhsg_ep_set_wedge,
  444. };
  445. /*
  446. * usb module start/end
  447. */
  448. static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
  449. {
  450. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  451. struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
  452. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  453. struct device *dev = usbhs_priv_to_dev(priv);
  454. unsigned long flags;
  455. int ret = 0;
  456. /******************** spin lock ********************/
  457. usbhs_lock(priv, flags);
  458. usbhsg_status_set(gpriv, status);
  459. if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
  460. usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
  461. ret = -1; /* not ready */
  462. usbhs_unlock(priv, flags);
  463. /******************** spin unlock ********************/
  464. if (ret < 0)
  465. return 0; /* not ready is not error */
  466. /*
  467. * enable interrupt and systems if ready
  468. */
  469. dev_dbg(dev, "start gadget\n");
  470. /*
  471. * pipe initialize and enable DCP
  472. */
  473. usbhs_pipe_init(priv,
  474. usbhsg_queue_done);
  475. usbhs_fifo_init(priv);
  476. usbhsg_uep_init(gpriv);
  477. /* dcp init */
  478. dcp->pipe = usbhs_dcp_malloc(priv);
  479. dcp->pipe->mod_private = dcp;
  480. /*
  481. * system config enble
  482. * - HI speed
  483. * - function
  484. * - usb module
  485. */
  486. usbhs_sys_hispeed_ctrl(priv, 1);
  487. usbhs_sys_function_ctrl(priv, 1);
  488. usbhs_sys_usb_ctrl(priv, 1);
  489. /*
  490. * enable irq callback
  491. */
  492. mod->irq_dev_state = usbhsg_irq_dev_state;
  493. mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
  494. usbhs_irq_callback_update(priv, mod);
  495. return 0;
  496. }
  497. static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
  498. {
  499. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  500. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  501. struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
  502. struct device *dev = usbhs_priv_to_dev(priv);
  503. unsigned long flags;
  504. int ret = 0;
  505. /******************** spin lock ********************/
  506. usbhs_lock(priv, flags);
  507. usbhsg_status_clr(gpriv, status);
  508. if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
  509. !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
  510. ret = -1; /* already done */
  511. usbhs_unlock(priv, flags);
  512. /******************** spin unlock ********************/
  513. if (ret < 0)
  514. return 0; /* already done is not error */
  515. /*
  516. * disable interrupt and systems if 1st try
  517. */
  518. usbhs_fifo_quit(priv);
  519. /* disable all irq */
  520. mod->irq_dev_state = NULL;
  521. mod->irq_ctrl_stage = NULL;
  522. usbhs_irq_callback_update(priv, mod);
  523. gpriv->gadget.speed = USB_SPEED_UNKNOWN;
  524. /* disable sys */
  525. usbhs_sys_hispeed_ctrl(priv, 0);
  526. usbhs_sys_function_ctrl(priv, 0);
  527. usbhs_sys_usb_ctrl(priv, 0);
  528. usbhsg_pipe_disable(dcp);
  529. if (gpriv->driver &&
  530. gpriv->driver->disconnect)
  531. gpriv->driver->disconnect(&gpriv->gadget);
  532. dev_dbg(dev, "stop gadget\n");
  533. return 0;
  534. }
  535. /*
  536. *
  537. * linux usb function
  538. *
  539. */
  540. struct usbhsg_gpriv *the_controller;
  541. int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
  542. int (*bind)(struct usb_gadget *))
  543. {
  544. struct usbhsg_gpriv *gpriv = the_controller;
  545. struct usbhs_priv *priv;
  546. struct device *dev;
  547. int ret;
  548. if (!bind ||
  549. !driver ||
  550. !driver->setup ||
  551. driver->speed != USB_SPEED_HIGH)
  552. return -EINVAL;
  553. if (!gpriv)
  554. return -ENODEV;
  555. if (gpriv->driver)
  556. return -EBUSY;
  557. dev = usbhsg_gpriv_to_dev(gpriv);
  558. priv = usbhsg_gpriv_to_priv(gpriv);
  559. /* first hook up the driver ... */
  560. gpriv->driver = driver;
  561. gpriv->gadget.dev.driver = &driver->driver;
  562. ret = device_add(&gpriv->gadget.dev);
  563. if (ret) {
  564. dev_err(dev, "device_add error %d\n", ret);
  565. goto add_fail;
  566. }
  567. ret = bind(&gpriv->gadget);
  568. if (ret) {
  569. dev_err(dev, "bind to driver %s error %d\n",
  570. driver->driver.name, ret);
  571. goto bind_fail;
  572. }
  573. dev_dbg(dev, "bind %s\n", driver->driver.name);
  574. return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
  575. bind_fail:
  576. device_del(&gpriv->gadget.dev);
  577. add_fail:
  578. gpriv->driver = NULL;
  579. gpriv->gadget.dev.driver = NULL;
  580. return ret;
  581. }
  582. EXPORT_SYMBOL(usb_gadget_probe_driver);
  583. int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  584. {
  585. struct usbhsg_gpriv *gpriv = the_controller;
  586. struct usbhs_priv *priv;
  587. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  588. if (!gpriv)
  589. return -ENODEV;
  590. if (!driver ||
  591. !driver->unbind ||
  592. driver != gpriv->driver)
  593. return -EINVAL;
  594. dev = usbhsg_gpriv_to_dev(gpriv);
  595. priv = usbhsg_gpriv_to_priv(gpriv);
  596. usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
  597. device_del(&gpriv->gadget.dev);
  598. gpriv->driver = NULL;
  599. if (driver->disconnect)
  600. driver->disconnect(&gpriv->gadget);
  601. driver->unbind(&gpriv->gadget);
  602. dev_dbg(dev, "unbind %s\n", driver->driver.name);
  603. return 0;
  604. }
  605. EXPORT_SYMBOL(usb_gadget_unregister_driver);
  606. /*
  607. * usb gadget ops
  608. */
  609. static int usbhsg_get_frame(struct usb_gadget *gadget)
  610. {
  611. struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
  612. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  613. return usbhs_frame_get_num(priv);
  614. }
  615. static struct usb_gadget_ops usbhsg_gadget_ops = {
  616. .get_frame = usbhsg_get_frame,
  617. };
  618. static int usbhsg_start(struct usbhs_priv *priv)
  619. {
  620. return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
  621. }
  622. static int usbhsg_stop(struct usbhs_priv *priv)
  623. {
  624. return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
  625. }
  626. int __devinit usbhs_mod_gadget_probe(struct usbhs_priv *priv)
  627. {
  628. struct usbhsg_gpriv *gpriv;
  629. struct usbhsg_uep *uep;
  630. struct device *dev = usbhs_priv_to_dev(priv);
  631. int pipe_size = usbhs_get_dparam(priv, pipe_size);
  632. int i;
  633. gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
  634. if (!gpriv) {
  635. dev_err(dev, "Could not allocate gadget priv\n");
  636. return -ENOMEM;
  637. }
  638. uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
  639. if (!uep) {
  640. dev_err(dev, "Could not allocate ep\n");
  641. goto usbhs_mod_gadget_probe_err_gpriv;
  642. }
  643. /*
  644. * CAUTION
  645. *
  646. * There is no guarantee that it is possible to access usb module here.
  647. * Don't accesses to it.
  648. * The accesse will be enable after "usbhsg_start"
  649. */
  650. /*
  651. * register itself
  652. */
  653. usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
  654. /* init gpriv */
  655. gpriv->mod.name = "gadget";
  656. gpriv->mod.start = usbhsg_start;
  657. gpriv->mod.stop = usbhsg_stop;
  658. gpriv->uep = uep;
  659. gpriv->uep_size = pipe_size;
  660. usbhsg_status_init(gpriv);
  661. /*
  662. * init gadget
  663. */
  664. device_initialize(&gpriv->gadget.dev);
  665. dev_set_name(&gpriv->gadget.dev, "gadget");
  666. gpriv->gadget.dev.parent = dev;
  667. gpriv->gadget.name = "renesas_usbhs_udc";
  668. gpriv->gadget.ops = &usbhsg_gadget_ops;
  669. gpriv->gadget.is_dualspeed = 1;
  670. INIT_LIST_HEAD(&gpriv->gadget.ep_list);
  671. /*
  672. * init usb_ep
  673. */
  674. usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
  675. uep->gpriv = gpriv;
  676. snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
  677. uep->ep.name = uep->ep_name;
  678. uep->ep.ops = &usbhsg_ep_ops;
  679. INIT_LIST_HEAD(&uep->ep.ep_list);
  680. /* init DCP */
  681. if (usbhsg_is_dcp(uep)) {
  682. gpriv->gadget.ep0 = &uep->ep;
  683. uep->ep.maxpacket = 64;
  684. }
  685. /* init normal pipe */
  686. else {
  687. uep->ep.maxpacket = 512;
  688. list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
  689. }
  690. }
  691. the_controller = gpriv;
  692. dev_info(dev, "gadget probed\n");
  693. return 0;
  694. usbhs_mod_gadget_probe_err_gpriv:
  695. kfree(gpriv);
  696. return -ENOMEM;
  697. }
  698. void __devexit usbhs_mod_gadget_remove(struct usbhs_priv *priv)
  699. {
  700. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  701. kfree(gpriv);
  702. }