mod_gadget.c 22 KB

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