mod_gadget.c 20 KB

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