mod_gadget.c 28 KB

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