/drivers/usb/renesas_usbhs/mod_gadget.c

https://bitbucket.org/cyanogenmod/android_kernel_asus_tf300t · C · 976 lines · 649 code · 186 blank · 141 comment · 53 complexity · af090e913977dd896f8da2fb0d21d9e9 MD5 · raw file

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