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