mod_gadget.c 27 KB

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