mod_gadget.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. /*
  2. * Renesas USB driver
  3. *
  4. * Copyright (C) 2011 Renesas Solutions Corp.
  5. * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. *
  16. */
  17. #include <linux/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 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_bus_get_speed(priv);
  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. pipe->handler = &usbhs_fifo_pio_push_handler;
  326. break;
  327. case WRITE_DATA_STAGE:
  328. pipe->handler = &usbhs_fifo_pio_pop_handler;
  329. break;
  330. case NODATA_STATUS_STAGE:
  331. pipe->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_sequence_data0(uep->pipe);
  403. return 0;
  404. }
  405. pipe = usbhs_pipe_malloc(priv,
  406. usb_endpoint_type(desc),
  407. usb_endpoint_dir_in(desc));
  408. if (pipe) {
  409. uep->pipe = pipe;
  410. pipe->mod_private = uep;
  411. /* set epnum / maxp */
  412. usbhs_pipe_config_update(pipe, 0,
  413. usb_endpoint_num(desc),
  414. usb_endpoint_maxp(desc));
  415. /*
  416. * usbhs_fifo_dma_push/pop_handler try to
  417. * use dmaengine if possible.
  418. * It will use pio handler if impossible.
  419. */
  420. if (usb_endpoint_dir_in(desc))
  421. pipe->handler = &usbhs_fifo_dma_push_handler;
  422. else
  423. pipe->handler = &usbhs_fifo_dma_pop_handler;
  424. ret = 0;
  425. }
  426. return ret;
  427. }
  428. static int usbhsg_ep_disable(struct usb_ep *ep)
  429. {
  430. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  431. return usbhsg_pipe_disable(uep);
  432. }
  433. static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
  434. gfp_t gfp_flags)
  435. {
  436. struct usbhsg_request *ureq;
  437. ureq = kzalloc(sizeof *ureq, gfp_flags);
  438. if (!ureq)
  439. return NULL;
  440. usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
  441. ureq->req.dma = DMA_ADDR_INVALID;
  442. return &ureq->req;
  443. }
  444. static void usbhsg_ep_free_request(struct usb_ep *ep,
  445. struct usb_request *req)
  446. {
  447. struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
  448. WARN_ON(!list_empty(&ureq->pkt.node));
  449. kfree(ureq);
  450. }
  451. static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
  452. gfp_t gfp_flags)
  453. {
  454. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  455. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  456. struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
  457. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  458. /* param check */
  459. if (usbhsg_is_not_connected(gpriv) ||
  460. unlikely(!gpriv->driver) ||
  461. unlikely(!pipe))
  462. return -ESHUTDOWN;
  463. usbhsg_queue_push(uep, ureq);
  464. return 0;
  465. }
  466. static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
  467. {
  468. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  469. struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
  470. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  471. usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq));
  472. usbhsg_queue_pop(uep, ureq, -ECONNRESET);
  473. return 0;
  474. }
  475. static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
  476. {
  477. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  478. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  479. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  480. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  481. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  482. unsigned long flags;
  483. usbhsg_pipe_disable(uep);
  484. dev_dbg(dev, "set halt %d (pipe %d)\n",
  485. halt, usbhs_pipe_number(pipe));
  486. /******************** spin lock ********************/
  487. usbhs_lock(priv, flags);
  488. if (halt)
  489. usbhs_pipe_stall(pipe);
  490. else
  491. usbhs_pipe_disable(pipe);
  492. if (halt && wedge)
  493. usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
  494. else
  495. usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
  496. usbhs_unlock(priv, flags);
  497. /******************** spin unlock ******************/
  498. return 0;
  499. }
  500. static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
  501. {
  502. return __usbhsg_ep_set_halt_wedge(ep, value, 0);
  503. }
  504. static int usbhsg_ep_set_wedge(struct usb_ep *ep)
  505. {
  506. return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
  507. }
  508. static struct usb_ep_ops usbhsg_ep_ops = {
  509. .enable = usbhsg_ep_enable,
  510. .disable = usbhsg_ep_disable,
  511. .alloc_request = usbhsg_ep_alloc_request,
  512. .free_request = usbhsg_ep_free_request,
  513. .queue = usbhsg_ep_queue,
  514. .dequeue = usbhsg_ep_dequeue,
  515. .set_halt = usbhsg_ep_set_halt,
  516. .set_wedge = usbhsg_ep_set_wedge,
  517. };
  518. /*
  519. * usb module start/end
  520. */
  521. static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
  522. {
  523. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  524. struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
  525. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  526. struct device *dev = usbhs_priv_to_dev(priv);
  527. unsigned long flags;
  528. int ret = 0;
  529. /******************** spin lock ********************/
  530. usbhs_lock(priv, flags);
  531. usbhsg_status_set(gpriv, status);
  532. if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
  533. usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
  534. ret = -1; /* not ready */
  535. usbhs_unlock(priv, flags);
  536. /******************** spin unlock ********************/
  537. if (ret < 0)
  538. return 0; /* not ready is not error */
  539. /*
  540. * enable interrupt and systems if ready
  541. */
  542. dev_dbg(dev, "start gadget\n");
  543. /*
  544. * pipe initialize and enable DCP
  545. */
  546. usbhs_pipe_init(priv,
  547. usbhsg_dma_map_ctrl);
  548. usbhs_fifo_init(priv);
  549. usbhsg_uep_init(gpriv);
  550. /* dcp init */
  551. dcp->pipe = usbhs_dcp_malloc(priv);
  552. dcp->pipe->mod_private = dcp;
  553. usbhs_pipe_config_update(dcp->pipe, 0, 0, 64);
  554. /*
  555. * system config enble
  556. * - HI speed
  557. * - function
  558. * - usb module
  559. */
  560. usbhs_sys_hispeed_ctrl(priv, 1);
  561. usbhs_sys_function_ctrl(priv, 1);
  562. usbhs_sys_usb_ctrl(priv, 1);
  563. /*
  564. * enable irq callback
  565. */
  566. mod->irq_dev_state = usbhsg_irq_dev_state;
  567. mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
  568. usbhs_irq_callback_update(priv, mod);
  569. return 0;
  570. }
  571. static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
  572. {
  573. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  574. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  575. struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
  576. struct device *dev = usbhs_priv_to_dev(priv);
  577. unsigned long flags;
  578. int ret = 0;
  579. /******************** spin lock ********************/
  580. usbhs_lock(priv, flags);
  581. usbhsg_status_clr(gpriv, status);
  582. if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
  583. !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
  584. ret = -1; /* already done */
  585. usbhs_unlock(priv, flags);
  586. /******************** spin unlock ********************/
  587. if (ret < 0)
  588. return 0; /* already done is not error */
  589. /*
  590. * disable interrupt and systems if 1st try
  591. */
  592. usbhs_fifo_quit(priv);
  593. /* disable all irq */
  594. mod->irq_dev_state = NULL;
  595. mod->irq_ctrl_stage = NULL;
  596. usbhs_irq_callback_update(priv, mod);
  597. gpriv->gadget.speed = USB_SPEED_UNKNOWN;
  598. /* disable sys */
  599. usbhs_sys_hispeed_ctrl(priv, 0);
  600. usbhs_sys_function_ctrl(priv, 0);
  601. usbhs_sys_usb_ctrl(priv, 0);
  602. usbhsg_pipe_disable(dcp);
  603. dev_dbg(dev, "stop gadget\n");
  604. return 0;
  605. }
  606. /*
  607. *
  608. * linux usb function
  609. *
  610. */
  611. static int usbhsg_gadget_start(struct usb_gadget *gadget,
  612. struct usb_gadget_driver *driver)
  613. {
  614. struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
  615. struct usbhs_priv *priv;
  616. struct device *dev;
  617. int ret;
  618. if (!driver ||
  619. !driver->setup ||
  620. driver->speed != USB_SPEED_HIGH)
  621. return -EINVAL;
  622. dev = usbhsg_gpriv_to_dev(gpriv);
  623. priv = usbhsg_gpriv_to_priv(gpriv);
  624. /* first hook up the driver ... */
  625. gpriv->driver = driver;
  626. gpriv->gadget.dev.driver = &driver->driver;
  627. ret = device_add(&gpriv->gadget.dev);
  628. if (ret) {
  629. dev_err(dev, "device_add error %d\n", ret);
  630. goto add_fail;
  631. }
  632. return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
  633. add_fail:
  634. gpriv->driver = NULL;
  635. gpriv->gadget.dev.driver = NULL;
  636. return ret;
  637. }
  638. static int usbhsg_gadget_stop(struct usb_gadget *gadget,
  639. struct usb_gadget_driver *driver)
  640. {
  641. struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
  642. struct usbhs_priv *priv;
  643. struct device *dev;
  644. if (!driver ||
  645. !driver->unbind)
  646. return -EINVAL;
  647. dev = usbhsg_gpriv_to_dev(gpriv);
  648. priv = usbhsg_gpriv_to_priv(gpriv);
  649. usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
  650. device_del(&gpriv->gadget.dev);
  651. gpriv->driver = NULL;
  652. return 0;
  653. }
  654. /*
  655. * usb gadget ops
  656. */
  657. static int usbhsg_get_frame(struct usb_gadget *gadget)
  658. {
  659. struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
  660. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  661. return usbhs_frame_get_num(priv);
  662. }
  663. static struct usb_gadget_ops usbhsg_gadget_ops = {
  664. .get_frame = usbhsg_get_frame,
  665. .udc_start = usbhsg_gadget_start,
  666. .udc_stop = usbhsg_gadget_stop,
  667. };
  668. static int usbhsg_start(struct usbhs_priv *priv)
  669. {
  670. return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
  671. }
  672. static int usbhsg_stop(struct usbhs_priv *priv)
  673. {
  674. return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
  675. }
  676. int __devinit usbhs_mod_gadget_probe(struct usbhs_priv *priv)
  677. {
  678. struct usbhsg_gpriv *gpriv;
  679. struct usbhsg_uep *uep;
  680. struct device *dev = usbhs_priv_to_dev(priv);
  681. int pipe_size = usbhs_get_dparam(priv, pipe_size);
  682. int i;
  683. int ret;
  684. gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
  685. if (!gpriv) {
  686. dev_err(dev, "Could not allocate gadget priv\n");
  687. return -ENOMEM;
  688. }
  689. uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
  690. if (!uep) {
  691. dev_err(dev, "Could not allocate ep\n");
  692. ret = -ENOMEM;
  693. goto usbhs_mod_gadget_probe_err_gpriv;
  694. }
  695. /*
  696. * CAUTION
  697. *
  698. * There is no guarantee that it is possible to access usb module here.
  699. * Don't accesses to it.
  700. * The accesse will be enable after "usbhsg_start"
  701. */
  702. /*
  703. * register itself
  704. */
  705. usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
  706. /* init gpriv */
  707. gpriv->mod.name = "gadget";
  708. gpriv->mod.start = usbhsg_start;
  709. gpriv->mod.stop = usbhsg_stop;
  710. gpriv->uep = uep;
  711. gpriv->uep_size = pipe_size;
  712. usbhsg_status_init(gpriv);
  713. /*
  714. * init gadget
  715. */
  716. device_initialize(&gpriv->gadget.dev);
  717. dev_set_name(&gpriv->gadget.dev, "gadget");
  718. gpriv->gadget.dev.parent = dev;
  719. gpriv->gadget.name = "renesas_usbhs_udc";
  720. gpriv->gadget.ops = &usbhsg_gadget_ops;
  721. gpriv->gadget.is_dualspeed = 1;
  722. INIT_LIST_HEAD(&gpriv->gadget.ep_list);
  723. /*
  724. * init usb_ep
  725. */
  726. usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
  727. uep->gpriv = gpriv;
  728. snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
  729. uep->ep.name = uep->ep_name;
  730. uep->ep.ops = &usbhsg_ep_ops;
  731. INIT_LIST_HEAD(&uep->ep.ep_list);
  732. /* init DCP */
  733. if (usbhsg_is_dcp(uep)) {
  734. gpriv->gadget.ep0 = &uep->ep;
  735. uep->ep.maxpacket = 64;
  736. }
  737. /* init normal pipe */
  738. else {
  739. uep->ep.maxpacket = 512;
  740. list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
  741. }
  742. }
  743. usbhsg_controller_register(gpriv);
  744. ret = usb_add_gadget_udc(dev, &gpriv->gadget);
  745. if (ret)
  746. goto err_add_udc;
  747. dev_info(dev, "gadget probed\n");
  748. return 0;
  749. err_add_udc:
  750. kfree(gpriv->uep);
  751. usbhs_mod_gadget_probe_err_gpriv:
  752. kfree(gpriv);
  753. return ret;
  754. }
  755. void __devexit usbhs_mod_gadget_remove(struct usbhs_priv *priv)
  756. {
  757. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  758. usb_del_gadget_udc(&gpriv->gadget);
  759. usbhsg_controller_unregister(gpriv);
  760. kfree(gpriv->uep);
  761. kfree(gpriv);
  762. }