mod_gadget.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  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/delay.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/io.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/usb/ch9.h>
  23. #include <linux/usb/gadget.h>
  24. #include <linux/usb/otg.h>
  25. #include "common.h"
  26. /*
  27. * struct
  28. */
  29. struct usbhsg_request {
  30. struct usb_request req;
  31. struct usbhs_pkt pkt;
  32. };
  33. #define EP_NAME_SIZE 8
  34. struct usbhsg_gpriv;
  35. struct usbhsg_uep {
  36. struct usb_ep ep;
  37. struct usbhs_pipe *pipe;
  38. spinlock_t lock; /* protect the pipe */
  39. char ep_name[EP_NAME_SIZE];
  40. struct usbhsg_gpriv *gpriv;
  41. };
  42. struct usbhsg_gpriv {
  43. struct usb_gadget gadget;
  44. struct usbhs_mod mod;
  45. struct usbhsg_uep *uep;
  46. int uep_size;
  47. struct usb_gadget_driver *driver;
  48. struct usb_phy *transceiver;
  49. bool vbus_active;
  50. u32 status;
  51. #define USBHSG_STATUS_STARTED (1 << 0)
  52. #define USBHSG_STATUS_REGISTERD (1 << 1)
  53. #define USBHSG_STATUS_WEDGE (1 << 2)
  54. #define USBHSG_STATUS_SELF_POWERED (1 << 3)
  55. #define USBHSG_STATUS_SOFT_CONNECT (1 << 4)
  56. };
  57. struct usbhsg_recip_handle {
  58. char *name;
  59. int (*device)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
  60. struct usb_ctrlrequest *ctrl);
  61. int (*interface)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
  62. struct usb_ctrlrequest *ctrl);
  63. int (*endpoint)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
  64. struct usb_ctrlrequest *ctrl);
  65. };
  66. /*
  67. * macro
  68. */
  69. #define usbhsg_priv_to_gpriv(priv) \
  70. container_of( \
  71. usbhs_mod_get(priv, USBHS_GADGET), \
  72. struct usbhsg_gpriv, mod)
  73. #define __usbhsg_for_each_uep(start, pos, g, i) \
  74. for ((i) = start; \
  75. ((i) < (g)->uep_size) && ((pos) = (g)->uep + (i)); \
  76. (i)++)
  77. #define usbhsg_for_each_uep(pos, gpriv, i) \
  78. __usbhsg_for_each_uep(1, pos, gpriv, i)
  79. #define usbhsg_for_each_uep_with_dcp(pos, gpriv, i) \
  80. __usbhsg_for_each_uep(0, pos, gpriv, i)
  81. #define usbhsg_gadget_to_gpriv(g)\
  82. container_of(g, struct usbhsg_gpriv, gadget)
  83. #define usbhsg_req_to_ureq(r)\
  84. container_of(r, struct usbhsg_request, req)
  85. #define usbhsg_ep_to_uep(e) container_of(e, struct usbhsg_uep, ep)
  86. #define usbhsg_gpriv_to_dev(gp) usbhs_priv_to_dev((gp)->mod.priv)
  87. #define usbhsg_gpriv_to_priv(gp) ((gp)->mod.priv)
  88. #define usbhsg_gpriv_to_dcp(gp) ((gp)->uep)
  89. #define usbhsg_gpriv_to_nth_uep(gp, i) ((gp)->uep + i)
  90. #define usbhsg_uep_to_gpriv(u) ((u)->gpriv)
  91. #define usbhsg_uep_to_pipe(u) ((u)->pipe)
  92. #define usbhsg_pipe_to_uep(p) ((p)->mod_private)
  93. #define usbhsg_is_dcp(u) ((u) == usbhsg_gpriv_to_dcp((u)->gpriv))
  94. #define usbhsg_ureq_to_pkt(u) (&(u)->pkt)
  95. #define usbhsg_pkt_to_ureq(i) \
  96. container_of(i, struct usbhsg_request, pkt)
  97. #define usbhsg_is_not_connected(gp) ((gp)->gadget.speed == USB_SPEED_UNKNOWN)
  98. /* status */
  99. #define usbhsg_status_init(gp) do {(gp)->status = 0; } while (0)
  100. #define usbhsg_status_set(gp, b) (gp->status |= b)
  101. #define usbhsg_status_clr(gp, b) (gp->status &= ~b)
  102. #define usbhsg_status_has(gp, b) (gp->status & b)
  103. /*
  104. * queue push/pop
  105. */
  106. static void __usbhsg_queue_pop(struct usbhsg_uep *uep,
  107. struct usbhsg_request *ureq,
  108. int status)
  109. {
  110. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  111. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  112. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  113. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  114. if (pipe)
  115. dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
  116. ureq->req.status = status;
  117. spin_unlock(usbhs_priv_to_lock(priv));
  118. usb_gadget_giveback_request(&uep->ep, &ureq->req);
  119. spin_lock(usbhs_priv_to_lock(priv));
  120. }
  121. static void usbhsg_queue_pop(struct usbhsg_uep *uep,
  122. struct usbhsg_request *ureq,
  123. int status)
  124. {
  125. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  126. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  127. unsigned long flags;
  128. usbhs_lock(priv, flags);
  129. __usbhsg_queue_pop(uep, ureq, status);
  130. usbhs_unlock(priv, flags);
  131. }
  132. static void usbhsg_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt)
  133. {
  134. struct usbhs_pipe *pipe = pkt->pipe;
  135. struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
  136. struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
  137. unsigned long flags;
  138. ureq->req.actual = pkt->actual;
  139. usbhs_lock(priv, flags);
  140. if (uep)
  141. __usbhsg_queue_pop(uep, ureq, 0);
  142. usbhs_unlock(priv, flags);
  143. }
  144. static void usbhsg_queue_push(struct usbhsg_uep *uep,
  145. struct usbhsg_request *ureq)
  146. {
  147. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  148. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  149. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  150. struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
  151. struct usb_request *req = &ureq->req;
  152. req->actual = 0;
  153. req->status = -EINPROGRESS;
  154. usbhs_pkt_push(pipe, pkt, usbhsg_queue_done,
  155. req->buf, req->length, req->zero, -1);
  156. usbhs_pkt_start(pipe);
  157. dev_dbg(dev, "pipe %d : queue push (%d)\n",
  158. usbhs_pipe_number(pipe),
  159. req->length);
  160. }
  161. /*
  162. * dma map/unmap
  163. */
  164. static int usbhsg_dma_map_ctrl(struct device *dma_dev, struct usbhs_pkt *pkt,
  165. int map)
  166. {
  167. struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
  168. struct usb_request *req = &ureq->req;
  169. struct usbhs_pipe *pipe = pkt->pipe;
  170. enum dma_data_direction dir;
  171. int ret = 0;
  172. dir = usbhs_pipe_is_dir_host(pipe);
  173. if (map) {
  174. /* it can not use scatter/gather */
  175. WARN_ON(req->num_sgs);
  176. ret = usb_gadget_map_request_by_dev(dma_dev, req, dir);
  177. if (ret < 0)
  178. return ret;
  179. pkt->dma = req->dma;
  180. } else {
  181. usb_gadget_unmap_request_by_dev(dma_dev, req, dir);
  182. }
  183. return ret;
  184. }
  185. /*
  186. * USB_TYPE_STANDARD / clear feature functions
  187. */
  188. static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
  189. struct usbhsg_uep *uep,
  190. struct usb_ctrlrequest *ctrl)
  191. {
  192. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  193. struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
  194. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
  195. usbhs_dcp_control_transfer_done(pipe);
  196. return 0;
  197. }
  198. static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
  199. struct usbhsg_uep *uep,
  200. struct usb_ctrlrequest *ctrl)
  201. {
  202. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  203. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  204. if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
  205. usbhs_pipe_disable(pipe);
  206. usbhs_pipe_sequence_data0(pipe);
  207. usbhs_pipe_enable(pipe);
  208. }
  209. usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
  210. usbhs_pkt_start(pipe);
  211. return 0;
  212. }
  213. static struct usbhsg_recip_handle req_clear_feature = {
  214. .name = "clear feature",
  215. .device = usbhsg_recip_handler_std_control_done,
  216. .interface = usbhsg_recip_handler_std_control_done,
  217. .endpoint = usbhsg_recip_handler_std_clear_endpoint,
  218. };
  219. /*
  220. * USB_TYPE_STANDARD / set feature functions
  221. */
  222. static int usbhsg_recip_handler_std_set_device(struct usbhs_priv *priv,
  223. struct usbhsg_uep *uep,
  224. struct usb_ctrlrequest *ctrl)
  225. {
  226. switch (le16_to_cpu(ctrl->wValue)) {
  227. case USB_DEVICE_TEST_MODE:
  228. usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
  229. udelay(100);
  230. usbhs_sys_set_test_mode(priv, le16_to_cpu(ctrl->wIndex >> 8));
  231. break;
  232. default:
  233. usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
  234. break;
  235. }
  236. return 0;
  237. }
  238. static int usbhsg_recip_handler_std_set_endpoint(struct usbhs_priv *priv,
  239. struct usbhsg_uep *uep,
  240. struct usb_ctrlrequest *ctrl)
  241. {
  242. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  243. usbhs_pipe_stall(pipe);
  244. usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
  245. return 0;
  246. }
  247. static struct usbhsg_recip_handle req_set_feature = {
  248. .name = "set feature",
  249. .device = usbhsg_recip_handler_std_set_device,
  250. .interface = usbhsg_recip_handler_std_control_done,
  251. .endpoint = usbhsg_recip_handler_std_set_endpoint,
  252. };
  253. /*
  254. * USB_TYPE_STANDARD / get status functions
  255. */
  256. static void __usbhsg_recip_send_complete(struct usb_ep *ep,
  257. struct usb_request *req)
  258. {
  259. struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
  260. /* free allocated recip-buffer/usb_request */
  261. kfree(ureq->pkt.buf);
  262. usb_ep_free_request(ep, req);
  263. }
  264. static void __usbhsg_recip_send_status(struct usbhsg_gpriv *gpriv,
  265. unsigned short status)
  266. {
  267. struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
  268. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
  269. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  270. struct usb_request *req;
  271. unsigned short *buf;
  272. /* alloc new usb_request for recip */
  273. req = usb_ep_alloc_request(&dcp->ep, GFP_ATOMIC);
  274. if (!req) {
  275. dev_err(dev, "recip request allocation fail\n");
  276. return;
  277. }
  278. /* alloc recip data buffer */
  279. buf = kmalloc(sizeof(*buf), GFP_ATOMIC);
  280. if (!buf) {
  281. usb_ep_free_request(&dcp->ep, req);
  282. return;
  283. }
  284. /* recip data is status */
  285. *buf = cpu_to_le16(status);
  286. /* allocated usb_request/buffer will be freed */
  287. req->complete = __usbhsg_recip_send_complete;
  288. req->buf = buf;
  289. req->length = sizeof(*buf);
  290. req->zero = 0;
  291. /* push packet */
  292. pipe->handler = &usbhs_fifo_pio_push_handler;
  293. usbhsg_queue_push(dcp, usbhsg_req_to_ureq(req));
  294. }
  295. static int usbhsg_recip_handler_std_get_device(struct usbhs_priv *priv,
  296. struct usbhsg_uep *uep,
  297. struct usb_ctrlrequest *ctrl)
  298. {
  299. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  300. unsigned short status = 0;
  301. if (usbhsg_status_has(gpriv, USBHSG_STATUS_SELF_POWERED))
  302. status = 1 << USB_DEVICE_SELF_POWERED;
  303. __usbhsg_recip_send_status(gpriv, status);
  304. return 0;
  305. }
  306. static int usbhsg_recip_handler_std_get_interface(struct usbhs_priv *priv,
  307. struct usbhsg_uep *uep,
  308. struct usb_ctrlrequest *ctrl)
  309. {
  310. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  311. unsigned short status = 0;
  312. __usbhsg_recip_send_status(gpriv, status);
  313. return 0;
  314. }
  315. static int usbhsg_recip_handler_std_get_endpoint(struct usbhs_priv *priv,
  316. struct usbhsg_uep *uep,
  317. struct usb_ctrlrequest *ctrl)
  318. {
  319. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  320. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  321. unsigned short status = 0;
  322. if (usbhs_pipe_is_stall(pipe))
  323. status = 1 << USB_ENDPOINT_HALT;
  324. __usbhsg_recip_send_status(gpriv, status);
  325. return 0;
  326. }
  327. static struct usbhsg_recip_handle req_get_status = {
  328. .name = "get status",
  329. .device = usbhsg_recip_handler_std_get_device,
  330. .interface = usbhsg_recip_handler_std_get_interface,
  331. .endpoint = usbhsg_recip_handler_std_get_endpoint,
  332. };
  333. /*
  334. * USB_TYPE handler
  335. */
  336. static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
  337. struct usbhsg_recip_handle *handler,
  338. struct usb_ctrlrequest *ctrl)
  339. {
  340. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  341. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  342. struct usbhsg_uep *uep;
  343. struct usbhs_pipe *pipe;
  344. int recip = ctrl->bRequestType & USB_RECIP_MASK;
  345. int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
  346. int ret = 0;
  347. int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
  348. struct usb_ctrlrequest *ctrl);
  349. char *msg;
  350. uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
  351. pipe = usbhsg_uep_to_pipe(uep);
  352. if (!pipe) {
  353. dev_err(dev, "wrong recip request\n");
  354. return -EINVAL;
  355. }
  356. switch (recip) {
  357. case USB_RECIP_DEVICE:
  358. msg = "DEVICE";
  359. func = handler->device;
  360. break;
  361. case USB_RECIP_INTERFACE:
  362. msg = "INTERFACE";
  363. func = handler->interface;
  364. break;
  365. case USB_RECIP_ENDPOINT:
  366. msg = "ENDPOINT";
  367. func = handler->endpoint;
  368. break;
  369. default:
  370. dev_warn(dev, "unsupported RECIP(%d)\n", recip);
  371. func = NULL;
  372. ret = -EINVAL;
  373. }
  374. if (func) {
  375. dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
  376. ret = func(priv, uep, ctrl);
  377. }
  378. return ret;
  379. }
  380. /*
  381. * irq functions
  382. *
  383. * it will be called from usbhs_interrupt
  384. */
  385. static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
  386. struct usbhs_irq_state *irq_state)
  387. {
  388. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  389. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  390. gpriv->gadget.speed = usbhs_bus_get_speed(priv);
  391. dev_dbg(dev, "state = %x : speed : %d\n",
  392. usbhs_status_get_device_state(irq_state),
  393. gpriv->gadget.speed);
  394. return 0;
  395. }
  396. static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
  397. struct usbhs_irq_state *irq_state)
  398. {
  399. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  400. struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
  401. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
  402. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  403. struct usb_ctrlrequest ctrl;
  404. struct usbhsg_recip_handle *recip_handler = NULL;
  405. int stage = usbhs_status_get_ctrl_stage(irq_state);
  406. int ret = 0;
  407. dev_dbg(dev, "stage = %d\n", stage);
  408. /*
  409. * see Manual
  410. *
  411. * "Operation"
  412. * - "Interrupt Function"
  413. * - "Control Transfer Stage Transition Interrupt"
  414. * - Fig. "Control Transfer Stage Transitions"
  415. */
  416. switch (stage) {
  417. case READ_DATA_STAGE:
  418. pipe->handler = &usbhs_fifo_pio_push_handler;
  419. break;
  420. case WRITE_DATA_STAGE:
  421. pipe->handler = &usbhs_fifo_pio_pop_handler;
  422. break;
  423. case NODATA_STATUS_STAGE:
  424. pipe->handler = &usbhs_ctrl_stage_end_handler;
  425. break;
  426. case READ_STATUS_STAGE:
  427. case WRITE_STATUS_STAGE:
  428. usbhs_dcp_control_transfer_done(pipe);
  429. default:
  430. return ret;
  431. }
  432. /*
  433. * get usb request
  434. */
  435. usbhs_usbreq_get_val(priv, &ctrl);
  436. switch (ctrl.bRequestType & USB_TYPE_MASK) {
  437. case USB_TYPE_STANDARD:
  438. switch (ctrl.bRequest) {
  439. case USB_REQ_CLEAR_FEATURE:
  440. recip_handler = &req_clear_feature;
  441. break;
  442. case USB_REQ_SET_FEATURE:
  443. recip_handler = &req_set_feature;
  444. break;
  445. case USB_REQ_GET_STATUS:
  446. recip_handler = &req_get_status;
  447. break;
  448. }
  449. }
  450. /*
  451. * setup stage / run recip
  452. */
  453. if (recip_handler)
  454. ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
  455. else
  456. ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
  457. if (ret < 0)
  458. usbhs_pipe_stall(pipe);
  459. return ret;
  460. }
  461. /*
  462. *
  463. * usb_dcp_ops
  464. *
  465. */
  466. static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
  467. {
  468. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  469. struct usbhs_pkt *pkt;
  470. while (1) {
  471. pkt = usbhs_pkt_pop(pipe, NULL);
  472. if (!pkt)
  473. break;
  474. usbhsg_queue_pop(uep, usbhsg_pkt_to_ureq(pkt), -ESHUTDOWN);
  475. }
  476. usbhs_pipe_disable(pipe);
  477. return 0;
  478. }
  479. /*
  480. *
  481. * usb_ep_ops
  482. *
  483. */
  484. static int usbhsg_ep_enable(struct usb_ep *ep,
  485. const struct usb_endpoint_descriptor *desc)
  486. {
  487. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  488. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  489. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  490. struct usbhs_pipe *pipe;
  491. int ret = -EIO;
  492. unsigned long flags;
  493. usbhs_lock(priv, flags);
  494. /*
  495. * if it already have pipe,
  496. * nothing to do
  497. */
  498. if (uep->pipe) {
  499. usbhs_pipe_clear(uep->pipe);
  500. usbhs_pipe_sequence_data0(uep->pipe);
  501. ret = 0;
  502. goto usbhsg_ep_enable_end;
  503. }
  504. pipe = usbhs_pipe_malloc(priv,
  505. usb_endpoint_type(desc),
  506. usb_endpoint_dir_in(desc));
  507. if (pipe) {
  508. uep->pipe = pipe;
  509. pipe->mod_private = uep;
  510. /* set epnum / maxp */
  511. usbhs_pipe_config_update(pipe, 0,
  512. usb_endpoint_num(desc),
  513. usb_endpoint_maxp(desc));
  514. /*
  515. * usbhs_fifo_dma_push/pop_handler try to
  516. * use dmaengine if possible.
  517. * It will use pio handler if impossible.
  518. */
  519. if (usb_endpoint_dir_in(desc)) {
  520. pipe->handler = &usbhs_fifo_dma_push_handler;
  521. } else {
  522. pipe->handler = &usbhs_fifo_dma_pop_handler;
  523. usbhs_xxxsts_clear(priv, BRDYSTS,
  524. usbhs_pipe_number(pipe));
  525. }
  526. ret = 0;
  527. }
  528. usbhsg_ep_enable_end:
  529. usbhs_unlock(priv, flags);
  530. return ret;
  531. }
  532. static int usbhsg_ep_disable(struct usb_ep *ep)
  533. {
  534. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  535. struct usbhs_pipe *pipe;
  536. unsigned long flags;
  537. spin_lock_irqsave(&uep->lock, flags);
  538. pipe = usbhsg_uep_to_pipe(uep);
  539. if (!pipe)
  540. goto out;
  541. usbhsg_pipe_disable(uep);
  542. usbhs_pipe_free(pipe);
  543. uep->pipe->mod_private = NULL;
  544. uep->pipe = NULL;
  545. out:
  546. spin_unlock_irqrestore(&uep->lock, flags);
  547. return 0;
  548. }
  549. static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
  550. gfp_t gfp_flags)
  551. {
  552. struct usbhsg_request *ureq;
  553. ureq = kzalloc(sizeof *ureq, gfp_flags);
  554. if (!ureq)
  555. return NULL;
  556. usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
  557. return &ureq->req;
  558. }
  559. static void usbhsg_ep_free_request(struct usb_ep *ep,
  560. struct usb_request *req)
  561. {
  562. struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
  563. WARN_ON(!list_empty(&ureq->pkt.node));
  564. kfree(ureq);
  565. }
  566. static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
  567. gfp_t gfp_flags)
  568. {
  569. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  570. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  571. struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
  572. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  573. /* param check */
  574. if (usbhsg_is_not_connected(gpriv) ||
  575. unlikely(!gpriv->driver) ||
  576. unlikely(!pipe))
  577. return -ESHUTDOWN;
  578. usbhsg_queue_push(uep, ureq);
  579. return 0;
  580. }
  581. static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
  582. {
  583. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  584. struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
  585. struct usbhs_pipe *pipe;
  586. unsigned long flags;
  587. spin_lock_irqsave(&uep->lock, flags);
  588. pipe = usbhsg_uep_to_pipe(uep);
  589. if (pipe)
  590. usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq));
  591. /*
  592. * To dequeue a request, this driver should call the usbhsg_queue_pop()
  593. * even if the pipe is NULL.
  594. */
  595. usbhsg_queue_pop(uep, ureq, -ECONNRESET);
  596. spin_unlock_irqrestore(&uep->lock, flags);
  597. return 0;
  598. }
  599. static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
  600. {
  601. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  602. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  603. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  604. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  605. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  606. unsigned long flags;
  607. usbhsg_pipe_disable(uep);
  608. dev_dbg(dev, "set halt %d (pipe %d)\n",
  609. halt, usbhs_pipe_number(pipe));
  610. /******************** spin lock ********************/
  611. usbhs_lock(priv, flags);
  612. if (halt)
  613. usbhs_pipe_stall(pipe);
  614. else
  615. usbhs_pipe_disable(pipe);
  616. if (halt && wedge)
  617. usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
  618. else
  619. usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
  620. usbhs_unlock(priv, flags);
  621. /******************** spin unlock ******************/
  622. return 0;
  623. }
  624. static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
  625. {
  626. return __usbhsg_ep_set_halt_wedge(ep, value, 0);
  627. }
  628. static int usbhsg_ep_set_wedge(struct usb_ep *ep)
  629. {
  630. return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
  631. }
  632. static const struct usb_ep_ops usbhsg_ep_ops = {
  633. .enable = usbhsg_ep_enable,
  634. .disable = usbhsg_ep_disable,
  635. .alloc_request = usbhsg_ep_alloc_request,
  636. .free_request = usbhsg_ep_free_request,
  637. .queue = usbhsg_ep_queue,
  638. .dequeue = usbhsg_ep_dequeue,
  639. .set_halt = usbhsg_ep_set_halt,
  640. .set_wedge = usbhsg_ep_set_wedge,
  641. };
  642. /*
  643. * pullup control
  644. */
  645. static int usbhsg_can_pullup(struct usbhs_priv *priv)
  646. {
  647. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  648. return gpriv->driver &&
  649. usbhsg_status_has(gpriv, USBHSG_STATUS_SOFT_CONNECT);
  650. }
  651. static void usbhsg_update_pullup(struct usbhs_priv *priv)
  652. {
  653. if (usbhsg_can_pullup(priv))
  654. usbhs_sys_function_pullup(priv, 1);
  655. else
  656. usbhs_sys_function_pullup(priv, 0);
  657. }
  658. /*
  659. * usb module start/end
  660. */
  661. static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
  662. {
  663. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  664. struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
  665. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  666. struct device *dev = usbhs_priv_to_dev(priv);
  667. unsigned long flags;
  668. int ret = 0;
  669. /******************** spin lock ********************/
  670. usbhs_lock(priv, flags);
  671. usbhsg_status_set(gpriv, status);
  672. if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
  673. usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
  674. ret = -1; /* not ready */
  675. usbhs_unlock(priv, flags);
  676. /******************** spin unlock ********************/
  677. if (ret < 0)
  678. return 0; /* not ready is not error */
  679. /*
  680. * enable interrupt and systems if ready
  681. */
  682. dev_dbg(dev, "start gadget\n");
  683. /*
  684. * pipe initialize and enable DCP
  685. */
  686. usbhs_fifo_init(priv);
  687. usbhs_pipe_init(priv,
  688. usbhsg_dma_map_ctrl);
  689. /* dcp init instead of usbhsg_ep_enable() */
  690. dcp->pipe = usbhs_dcp_malloc(priv);
  691. dcp->pipe->mod_private = dcp;
  692. usbhs_pipe_config_update(dcp->pipe, 0, 0, 64);
  693. /*
  694. * system config enble
  695. * - HI speed
  696. * - function
  697. * - usb module
  698. */
  699. usbhs_sys_function_ctrl(priv, 1);
  700. usbhsg_update_pullup(priv);
  701. /*
  702. * enable irq callback
  703. */
  704. mod->irq_dev_state = usbhsg_irq_dev_state;
  705. mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
  706. usbhs_irq_callback_update(priv, mod);
  707. return 0;
  708. }
  709. static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
  710. {
  711. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  712. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  713. struct usbhsg_uep *uep;
  714. struct device *dev = usbhs_priv_to_dev(priv);
  715. unsigned long flags;
  716. int ret = 0, i;
  717. /******************** spin lock ********************/
  718. usbhs_lock(priv, flags);
  719. usbhsg_status_clr(gpriv, status);
  720. if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
  721. !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
  722. ret = -1; /* already done */
  723. usbhs_unlock(priv, flags);
  724. /******************** spin unlock ********************/
  725. if (ret < 0)
  726. return 0; /* already done is not error */
  727. /*
  728. * disable interrupt and systems if 1st try
  729. */
  730. usbhs_fifo_quit(priv);
  731. /* disable all irq */
  732. mod->irq_dev_state = NULL;
  733. mod->irq_ctrl_stage = NULL;
  734. usbhs_irq_callback_update(priv, mod);
  735. gpriv->gadget.speed = USB_SPEED_UNKNOWN;
  736. /* disable sys */
  737. usbhs_sys_set_test_mode(priv, 0);
  738. usbhs_sys_function_ctrl(priv, 0);
  739. /* disable all eps */
  740. usbhsg_for_each_uep_with_dcp(uep, gpriv, i)
  741. usbhsg_ep_disable(&uep->ep);
  742. dev_dbg(dev, "stop gadget\n");
  743. return 0;
  744. }
  745. /*
  746. * VBUS provided by the PHY
  747. */
  748. static int usbhsm_phy_get_vbus(struct platform_device *pdev)
  749. {
  750. struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
  751. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  752. return gpriv->vbus_active;
  753. }
  754. static void usbhs_mod_phy_mode(struct usbhs_priv *priv)
  755. {
  756. struct usbhs_mod_info *info = &priv->mod_info;
  757. info->irq_vbus = NULL;
  758. priv->pfunc.get_vbus = usbhsm_phy_get_vbus;
  759. usbhs_irq_callback_update(priv, NULL);
  760. }
  761. /*
  762. *
  763. * linux usb function
  764. *
  765. */
  766. static int usbhsg_gadget_start(struct usb_gadget *gadget,
  767. struct usb_gadget_driver *driver)
  768. {
  769. struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
  770. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  771. struct device *dev = usbhs_priv_to_dev(priv);
  772. int ret;
  773. if (!driver ||
  774. !driver->setup ||
  775. driver->max_speed < USB_SPEED_FULL)
  776. return -EINVAL;
  777. /* connect to bus through transceiver */
  778. if (!IS_ERR_OR_NULL(gpriv->transceiver)) {
  779. ret = otg_set_peripheral(gpriv->transceiver->otg,
  780. &gpriv->gadget);
  781. if (ret) {
  782. dev_err(dev, "%s: can't bind to transceiver\n",
  783. gpriv->gadget.name);
  784. return ret;
  785. }
  786. /* get vbus using phy versions */
  787. usbhs_mod_phy_mode(priv);
  788. }
  789. /* first hook up the driver ... */
  790. gpriv->driver = driver;
  791. return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
  792. }
  793. static int usbhsg_gadget_stop(struct usb_gadget *gadget)
  794. {
  795. struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
  796. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  797. usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
  798. if (!IS_ERR_OR_NULL(gpriv->transceiver))
  799. otg_set_peripheral(gpriv->transceiver->otg, NULL);
  800. gpriv->driver = NULL;
  801. return 0;
  802. }
  803. /*
  804. * usb gadget ops
  805. */
  806. static int usbhsg_get_frame(struct usb_gadget *gadget)
  807. {
  808. struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
  809. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  810. return usbhs_frame_get_num(priv);
  811. }
  812. static int usbhsg_pullup(struct usb_gadget *gadget, int is_on)
  813. {
  814. struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
  815. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  816. unsigned long flags;
  817. usbhs_lock(priv, flags);
  818. if (is_on)
  819. usbhsg_status_set(gpriv, USBHSG_STATUS_SOFT_CONNECT);
  820. else
  821. usbhsg_status_clr(gpriv, USBHSG_STATUS_SOFT_CONNECT);
  822. usbhsg_update_pullup(priv);
  823. usbhs_unlock(priv, flags);
  824. return 0;
  825. }
  826. static int usbhsg_set_selfpowered(struct usb_gadget *gadget, int is_self)
  827. {
  828. struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
  829. if (is_self)
  830. usbhsg_status_set(gpriv, USBHSG_STATUS_SELF_POWERED);
  831. else
  832. usbhsg_status_clr(gpriv, USBHSG_STATUS_SELF_POWERED);
  833. gadget->is_selfpowered = (is_self != 0);
  834. return 0;
  835. }
  836. static int usbhsg_vbus_session(struct usb_gadget *gadget, int is_active)
  837. {
  838. struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
  839. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  840. struct platform_device *pdev = usbhs_priv_to_pdev(priv);
  841. gpriv->vbus_active = !!is_active;
  842. renesas_usbhs_call_notify_hotplug(pdev);
  843. return 0;
  844. }
  845. static const struct usb_gadget_ops usbhsg_gadget_ops = {
  846. .get_frame = usbhsg_get_frame,
  847. .set_selfpowered = usbhsg_set_selfpowered,
  848. .udc_start = usbhsg_gadget_start,
  849. .udc_stop = usbhsg_gadget_stop,
  850. .pullup = usbhsg_pullup,
  851. .vbus_session = usbhsg_vbus_session,
  852. };
  853. static int usbhsg_start(struct usbhs_priv *priv)
  854. {
  855. return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
  856. }
  857. static int usbhsg_stop(struct usbhs_priv *priv)
  858. {
  859. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  860. /* cable disconnect */
  861. if (gpriv->driver &&
  862. gpriv->driver->disconnect)
  863. gpriv->driver->disconnect(&gpriv->gadget);
  864. return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
  865. }
  866. int usbhs_mod_gadget_probe(struct usbhs_priv *priv)
  867. {
  868. struct usbhsg_gpriv *gpriv;
  869. struct usbhsg_uep *uep;
  870. struct device *dev = usbhs_priv_to_dev(priv);
  871. struct renesas_usbhs_driver_pipe_config *pipe_configs =
  872. usbhs_get_dparam(priv, pipe_configs);
  873. int pipe_size = usbhs_get_dparam(priv, pipe_size);
  874. int i;
  875. int ret;
  876. gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
  877. if (!gpriv)
  878. return -ENOMEM;
  879. uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
  880. if (!uep) {
  881. ret = -ENOMEM;
  882. goto usbhs_mod_gadget_probe_err_gpriv;
  883. }
  884. gpriv->transceiver = usb_get_phy(USB_PHY_TYPE_UNDEFINED);
  885. dev_info(dev, "%stransceiver found\n",
  886. !IS_ERR(gpriv->transceiver) ? "" : "no ");
  887. /*
  888. * CAUTION
  889. *
  890. * There is no guarantee that it is possible to access usb module here.
  891. * Don't accesses to it.
  892. * The accesse will be enable after "usbhsg_start"
  893. */
  894. /*
  895. * register itself
  896. */
  897. usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
  898. /* init gpriv */
  899. gpriv->mod.name = "gadget";
  900. gpriv->mod.start = usbhsg_start;
  901. gpriv->mod.stop = usbhsg_stop;
  902. gpriv->uep = uep;
  903. gpriv->uep_size = pipe_size;
  904. usbhsg_status_init(gpriv);
  905. /*
  906. * init gadget
  907. */
  908. gpriv->gadget.dev.parent = dev;
  909. gpriv->gadget.name = "renesas_usbhs_udc";
  910. gpriv->gadget.ops = &usbhsg_gadget_ops;
  911. gpriv->gadget.max_speed = USB_SPEED_HIGH;
  912. gpriv->gadget.quirk_avoids_skb_reserve = usbhs_get_dparam(priv,
  913. has_usb_dmac);
  914. INIT_LIST_HEAD(&gpriv->gadget.ep_list);
  915. /*
  916. * init usb_ep
  917. */
  918. usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
  919. uep->gpriv = gpriv;
  920. uep->pipe = NULL;
  921. snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
  922. uep->ep.name = uep->ep_name;
  923. uep->ep.ops = &usbhsg_ep_ops;
  924. INIT_LIST_HEAD(&uep->ep.ep_list);
  925. spin_lock_init(&uep->lock);
  926. /* init DCP */
  927. if (usbhsg_is_dcp(uep)) {
  928. gpriv->gadget.ep0 = &uep->ep;
  929. usb_ep_set_maxpacket_limit(&uep->ep, 64);
  930. uep->ep.caps.type_control = true;
  931. } else {
  932. /* init normal pipe */
  933. if (pipe_configs[i].type == USB_ENDPOINT_XFER_ISOC)
  934. uep->ep.caps.type_iso = true;
  935. if (pipe_configs[i].type == USB_ENDPOINT_XFER_BULK)
  936. uep->ep.caps.type_bulk = true;
  937. if (pipe_configs[i].type == USB_ENDPOINT_XFER_INT)
  938. uep->ep.caps.type_int = true;
  939. usb_ep_set_maxpacket_limit(&uep->ep,
  940. pipe_configs[i].bufsize);
  941. list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
  942. }
  943. uep->ep.caps.dir_in = true;
  944. uep->ep.caps.dir_out = true;
  945. }
  946. ret = usb_add_gadget_udc(dev, &gpriv->gadget);
  947. if (ret)
  948. goto err_add_udc;
  949. dev_info(dev, "gadget probed\n");
  950. return 0;
  951. err_add_udc:
  952. kfree(gpriv->uep);
  953. usbhs_mod_gadget_probe_err_gpriv:
  954. kfree(gpriv);
  955. return ret;
  956. }
  957. void usbhs_mod_gadget_remove(struct usbhs_priv *priv)
  958. {
  959. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  960. usb_del_gadget_udc(&gpriv->gadget);
  961. kfree(gpriv->uep);
  962. kfree(gpriv);
  963. }