mod_gadget.c 22 KB

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