mod_gadget.c 22 KB

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