mod_gadget.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  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 device *dma_dev, struct usbhs_pkt *pkt,
  164. int map)
  165. {
  166. struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
  167. struct usb_request *req = &ureq->req;
  168. struct usbhs_pipe *pipe = pkt->pipe;
  169. enum dma_data_direction dir;
  170. int ret = 0;
  171. dir = usbhs_pipe_is_dir_host(pipe);
  172. if (map) {
  173. /* it can not use scatter/gather */
  174. WARN_ON(req->num_sgs);
  175. ret = usb_gadget_map_request_by_dev(dma_dev, req, dir);
  176. if (ret < 0)
  177. return ret;
  178. pkt->dma = req->dma;
  179. } else {
  180. usb_gadget_unmap_request_by_dev(dma_dev, req, dir);
  181. }
  182. return ret;
  183. }
  184. /*
  185. * USB_TYPE_STANDARD / clear feature functions
  186. */
  187. static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
  188. struct usbhsg_uep *uep,
  189. struct usb_ctrlrequest *ctrl)
  190. {
  191. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  192. struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
  193. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
  194. usbhs_dcp_control_transfer_done(pipe);
  195. return 0;
  196. }
  197. static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
  198. struct usbhsg_uep *uep,
  199. struct usb_ctrlrequest *ctrl)
  200. {
  201. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  202. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  203. if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
  204. usbhs_pipe_disable(pipe);
  205. usbhs_pipe_sequence_data0(pipe);
  206. usbhs_pipe_enable(pipe);
  207. }
  208. usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
  209. usbhs_pkt_start(pipe);
  210. return 0;
  211. }
  212. static struct usbhsg_recip_handle req_clear_feature = {
  213. .name = "clear feature",
  214. .device = usbhsg_recip_handler_std_control_done,
  215. .interface = usbhsg_recip_handler_std_control_done,
  216. .endpoint = usbhsg_recip_handler_std_clear_endpoint,
  217. };
  218. /*
  219. * USB_TYPE_STANDARD / set feature functions
  220. */
  221. static int usbhsg_recip_handler_std_set_device(struct usbhs_priv *priv,
  222. struct usbhsg_uep *uep,
  223. struct usb_ctrlrequest *ctrl)
  224. {
  225. switch (le16_to_cpu(ctrl->wValue)) {
  226. case USB_DEVICE_TEST_MODE:
  227. usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
  228. udelay(100);
  229. usbhs_sys_set_test_mode(priv, le16_to_cpu(ctrl->wIndex >> 8));
  230. break;
  231. default:
  232. usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
  233. break;
  234. }
  235. return 0;
  236. }
  237. static int usbhsg_recip_handler_std_set_endpoint(struct usbhs_priv *priv,
  238. struct usbhsg_uep *uep,
  239. struct usb_ctrlrequest *ctrl)
  240. {
  241. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  242. usbhs_pipe_stall(pipe);
  243. usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
  244. return 0;
  245. }
  246. static struct usbhsg_recip_handle req_set_feature = {
  247. .name = "set feature",
  248. .device = usbhsg_recip_handler_std_set_device,
  249. .interface = usbhsg_recip_handler_std_control_done,
  250. .endpoint = usbhsg_recip_handler_std_set_endpoint,
  251. };
  252. /*
  253. * USB_TYPE_STANDARD / get status functions
  254. */
  255. static void __usbhsg_recip_send_complete(struct usb_ep *ep,
  256. struct usb_request *req)
  257. {
  258. struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
  259. /* free allocated recip-buffer/usb_request */
  260. kfree(ureq->pkt.buf);
  261. usb_ep_free_request(ep, req);
  262. }
  263. static void __usbhsg_recip_send_status(struct usbhsg_gpriv *gpriv,
  264. unsigned short status)
  265. {
  266. struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
  267. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
  268. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  269. struct usb_request *req;
  270. unsigned short *buf;
  271. /* alloc new usb_request for recip */
  272. req = usb_ep_alloc_request(&dcp->ep, GFP_ATOMIC);
  273. if (!req) {
  274. dev_err(dev, "recip request allocation fail\n");
  275. return;
  276. }
  277. /* alloc recip data buffer */
  278. buf = kmalloc(sizeof(*buf), GFP_ATOMIC);
  279. if (!buf) {
  280. usb_ep_free_request(&dcp->ep, req);
  281. dev_err(dev, "recip data allocation fail\n");
  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 = usbhsg_uep_to_pipe(uep);
  536. if (!pipe)
  537. return -EINVAL;
  538. usbhsg_pipe_disable(uep);
  539. usbhs_pipe_free(pipe);
  540. uep->pipe->mod_private = NULL;
  541. uep->pipe = NULL;
  542. return 0;
  543. }
  544. static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
  545. gfp_t gfp_flags)
  546. {
  547. struct usbhsg_request *ureq;
  548. ureq = kzalloc(sizeof *ureq, gfp_flags);
  549. if (!ureq)
  550. return NULL;
  551. usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
  552. return &ureq->req;
  553. }
  554. static void usbhsg_ep_free_request(struct usb_ep *ep,
  555. struct usb_request *req)
  556. {
  557. struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
  558. WARN_ON(!list_empty(&ureq->pkt.node));
  559. kfree(ureq);
  560. }
  561. static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
  562. gfp_t gfp_flags)
  563. {
  564. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  565. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  566. struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
  567. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  568. /* param check */
  569. if (usbhsg_is_not_connected(gpriv) ||
  570. unlikely(!gpriv->driver) ||
  571. unlikely(!pipe))
  572. return -ESHUTDOWN;
  573. usbhsg_queue_push(uep, ureq);
  574. return 0;
  575. }
  576. static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
  577. {
  578. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  579. struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
  580. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  581. if (pipe)
  582. usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq));
  583. /*
  584. * To dequeue a request, this driver should call the usbhsg_queue_pop()
  585. * even if the pipe is NULL.
  586. */
  587. usbhsg_queue_pop(uep, ureq, -ECONNRESET);
  588. return 0;
  589. }
  590. static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
  591. {
  592. struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
  593. struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
  594. struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
  595. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  596. struct device *dev = usbhsg_gpriv_to_dev(gpriv);
  597. unsigned long flags;
  598. usbhsg_pipe_disable(uep);
  599. dev_dbg(dev, "set halt %d (pipe %d)\n",
  600. halt, usbhs_pipe_number(pipe));
  601. /******************** spin lock ********************/
  602. usbhs_lock(priv, flags);
  603. if (halt)
  604. usbhs_pipe_stall(pipe);
  605. else
  606. usbhs_pipe_disable(pipe);
  607. if (halt && wedge)
  608. usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
  609. else
  610. usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
  611. usbhs_unlock(priv, flags);
  612. /******************** spin unlock ******************/
  613. return 0;
  614. }
  615. static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
  616. {
  617. return __usbhsg_ep_set_halt_wedge(ep, value, 0);
  618. }
  619. static int usbhsg_ep_set_wedge(struct usb_ep *ep)
  620. {
  621. return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
  622. }
  623. static struct usb_ep_ops usbhsg_ep_ops = {
  624. .enable = usbhsg_ep_enable,
  625. .disable = usbhsg_ep_disable,
  626. .alloc_request = usbhsg_ep_alloc_request,
  627. .free_request = usbhsg_ep_free_request,
  628. .queue = usbhsg_ep_queue,
  629. .dequeue = usbhsg_ep_dequeue,
  630. .set_halt = usbhsg_ep_set_halt,
  631. .set_wedge = usbhsg_ep_set_wedge,
  632. };
  633. /*
  634. * pullup control
  635. */
  636. static int usbhsg_can_pullup(struct usbhs_priv *priv)
  637. {
  638. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  639. return gpriv->driver &&
  640. usbhsg_status_has(gpriv, USBHSG_STATUS_SOFT_CONNECT);
  641. }
  642. static void usbhsg_update_pullup(struct usbhs_priv *priv)
  643. {
  644. if (usbhsg_can_pullup(priv))
  645. usbhs_sys_function_pullup(priv, 1);
  646. else
  647. usbhs_sys_function_pullup(priv, 0);
  648. }
  649. /*
  650. * usb module start/end
  651. */
  652. static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
  653. {
  654. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  655. struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
  656. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  657. struct device *dev = usbhs_priv_to_dev(priv);
  658. unsigned long flags;
  659. int ret = 0;
  660. /******************** spin lock ********************/
  661. usbhs_lock(priv, flags);
  662. usbhsg_status_set(gpriv, status);
  663. if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
  664. usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
  665. ret = -1; /* not ready */
  666. usbhs_unlock(priv, flags);
  667. /******************** spin unlock ********************/
  668. if (ret < 0)
  669. return 0; /* not ready is not error */
  670. /*
  671. * enable interrupt and systems if ready
  672. */
  673. dev_dbg(dev, "start gadget\n");
  674. /*
  675. * pipe initialize and enable DCP
  676. */
  677. usbhs_fifo_init(priv);
  678. usbhs_pipe_init(priv,
  679. usbhsg_dma_map_ctrl);
  680. /* dcp init instead of usbhsg_ep_enable() */
  681. dcp->pipe = usbhs_dcp_malloc(priv);
  682. dcp->pipe->mod_private = dcp;
  683. usbhs_pipe_config_update(dcp->pipe, 0, 0, 64);
  684. /*
  685. * system config enble
  686. * - HI speed
  687. * - function
  688. * - usb module
  689. */
  690. usbhs_sys_function_ctrl(priv, 1);
  691. usbhsg_update_pullup(priv);
  692. /*
  693. * enable irq callback
  694. */
  695. mod->irq_dev_state = usbhsg_irq_dev_state;
  696. mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
  697. usbhs_irq_callback_update(priv, mod);
  698. return 0;
  699. }
  700. static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
  701. {
  702. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  703. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  704. struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
  705. struct device *dev = usbhs_priv_to_dev(priv);
  706. unsigned long flags;
  707. int ret = 0;
  708. /******************** spin lock ********************/
  709. usbhs_lock(priv, flags);
  710. usbhsg_status_clr(gpriv, status);
  711. if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
  712. !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
  713. ret = -1; /* already done */
  714. usbhs_unlock(priv, flags);
  715. /******************** spin unlock ********************/
  716. if (ret < 0)
  717. return 0; /* already done is not error */
  718. /*
  719. * disable interrupt and systems if 1st try
  720. */
  721. usbhs_fifo_quit(priv);
  722. /* disable all irq */
  723. mod->irq_dev_state = NULL;
  724. mod->irq_ctrl_stage = NULL;
  725. usbhs_irq_callback_update(priv, mod);
  726. gpriv->gadget.speed = USB_SPEED_UNKNOWN;
  727. /* disable sys */
  728. usbhs_sys_set_test_mode(priv, 0);
  729. usbhs_sys_function_ctrl(priv, 0);
  730. usbhsg_ep_disable(&dcp->ep);
  731. dev_dbg(dev, "stop gadget\n");
  732. return 0;
  733. }
  734. /*
  735. * VBUS provided by the PHY
  736. */
  737. static int usbhsm_phy_get_vbus(struct platform_device *pdev)
  738. {
  739. struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
  740. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  741. return gpriv->vbus_active;
  742. }
  743. static void usbhs_mod_phy_mode(struct usbhs_priv *priv)
  744. {
  745. struct usbhs_mod_info *info = &priv->mod_info;
  746. info->irq_vbus = NULL;
  747. priv->pfunc.get_vbus = usbhsm_phy_get_vbus;
  748. usbhs_irq_callback_update(priv, NULL);
  749. }
  750. /*
  751. *
  752. * linux usb function
  753. *
  754. */
  755. static int usbhsg_gadget_start(struct usb_gadget *gadget,
  756. struct usb_gadget_driver *driver)
  757. {
  758. struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
  759. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  760. struct device *dev = usbhs_priv_to_dev(priv);
  761. int ret;
  762. if (!driver ||
  763. !driver->setup ||
  764. driver->max_speed < USB_SPEED_FULL)
  765. return -EINVAL;
  766. /* connect to bus through transceiver */
  767. if (!IS_ERR_OR_NULL(gpriv->transceiver)) {
  768. ret = otg_set_peripheral(gpriv->transceiver->otg,
  769. &gpriv->gadget);
  770. if (ret) {
  771. dev_err(dev, "%s: can't bind to transceiver\n",
  772. gpriv->gadget.name);
  773. return ret;
  774. }
  775. /* get vbus using phy versions */
  776. usbhs_mod_phy_mode(priv);
  777. }
  778. /* first hook up the driver ... */
  779. gpriv->driver = driver;
  780. return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
  781. }
  782. static int usbhsg_gadget_stop(struct usb_gadget *gadget)
  783. {
  784. struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
  785. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  786. usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
  787. if (!IS_ERR_OR_NULL(gpriv->transceiver))
  788. otg_set_peripheral(gpriv->transceiver->otg, NULL);
  789. gpriv->driver = NULL;
  790. return 0;
  791. }
  792. /*
  793. * usb gadget ops
  794. */
  795. static int usbhsg_get_frame(struct usb_gadget *gadget)
  796. {
  797. struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
  798. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  799. return usbhs_frame_get_num(priv);
  800. }
  801. static int usbhsg_pullup(struct usb_gadget *gadget, int is_on)
  802. {
  803. struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
  804. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  805. unsigned long flags;
  806. usbhs_lock(priv, flags);
  807. if (is_on)
  808. usbhsg_status_set(gpriv, USBHSG_STATUS_SOFT_CONNECT);
  809. else
  810. usbhsg_status_clr(gpriv, USBHSG_STATUS_SOFT_CONNECT);
  811. usbhsg_update_pullup(priv);
  812. usbhs_unlock(priv, flags);
  813. return 0;
  814. }
  815. static int usbhsg_set_selfpowered(struct usb_gadget *gadget, int is_self)
  816. {
  817. struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
  818. if (is_self)
  819. usbhsg_status_set(gpriv, USBHSG_STATUS_SELF_POWERED);
  820. else
  821. usbhsg_status_clr(gpriv, USBHSG_STATUS_SELF_POWERED);
  822. gadget->is_selfpowered = (is_self != 0);
  823. return 0;
  824. }
  825. static int usbhsg_vbus_session(struct usb_gadget *gadget, int is_active)
  826. {
  827. struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
  828. struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
  829. struct platform_device *pdev = usbhs_priv_to_pdev(priv);
  830. gpriv->vbus_active = !!is_active;
  831. renesas_usbhs_call_notify_hotplug(pdev);
  832. return 0;
  833. }
  834. static const struct usb_gadget_ops usbhsg_gadget_ops = {
  835. .get_frame = usbhsg_get_frame,
  836. .set_selfpowered = usbhsg_set_selfpowered,
  837. .udc_start = usbhsg_gadget_start,
  838. .udc_stop = usbhsg_gadget_stop,
  839. .pullup = usbhsg_pullup,
  840. .vbus_session = usbhsg_vbus_session,
  841. };
  842. static int usbhsg_start(struct usbhs_priv *priv)
  843. {
  844. return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
  845. }
  846. static int usbhsg_stop(struct usbhs_priv *priv)
  847. {
  848. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  849. /* cable disconnect */
  850. if (gpriv->driver &&
  851. gpriv->driver->disconnect)
  852. gpriv->driver->disconnect(&gpriv->gadget);
  853. return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
  854. }
  855. int usbhs_mod_gadget_probe(struct usbhs_priv *priv)
  856. {
  857. struct usbhsg_gpriv *gpriv;
  858. struct usbhsg_uep *uep;
  859. struct device *dev = usbhs_priv_to_dev(priv);
  860. struct renesas_usbhs_driver_pipe_config *pipe_configs =
  861. usbhs_get_dparam(priv, pipe_configs);
  862. int pipe_size = usbhs_get_dparam(priv, pipe_size);
  863. int i;
  864. int ret;
  865. gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
  866. if (!gpriv) {
  867. dev_err(dev, "Could not allocate gadget priv\n");
  868. return -ENOMEM;
  869. }
  870. uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
  871. if (!uep) {
  872. dev_err(dev, "Could not allocate ep\n");
  873. ret = -ENOMEM;
  874. goto usbhs_mod_gadget_probe_err_gpriv;
  875. }
  876. gpriv->transceiver = usb_get_phy(USB_PHY_TYPE_UNDEFINED);
  877. dev_info(dev, "%stransceiver found\n",
  878. gpriv->transceiver ? "" : "no ");
  879. /*
  880. * CAUTION
  881. *
  882. * There is no guarantee that it is possible to access usb module here.
  883. * Don't accesses to it.
  884. * The accesse will be enable after "usbhsg_start"
  885. */
  886. /*
  887. * register itself
  888. */
  889. usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
  890. /* init gpriv */
  891. gpriv->mod.name = "gadget";
  892. gpriv->mod.start = usbhsg_start;
  893. gpriv->mod.stop = usbhsg_stop;
  894. gpriv->uep = uep;
  895. gpriv->uep_size = pipe_size;
  896. usbhsg_status_init(gpriv);
  897. /*
  898. * init gadget
  899. */
  900. gpriv->gadget.dev.parent = dev;
  901. gpriv->gadget.name = "renesas_usbhs_udc";
  902. gpriv->gadget.ops = &usbhsg_gadget_ops;
  903. gpriv->gadget.max_speed = USB_SPEED_HIGH;
  904. INIT_LIST_HEAD(&gpriv->gadget.ep_list);
  905. /*
  906. * init usb_ep
  907. */
  908. usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
  909. uep->gpriv = gpriv;
  910. uep->pipe = NULL;
  911. snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
  912. uep->ep.name = uep->ep_name;
  913. uep->ep.ops = &usbhsg_ep_ops;
  914. INIT_LIST_HEAD(&uep->ep.ep_list);
  915. /* init DCP */
  916. if (usbhsg_is_dcp(uep)) {
  917. gpriv->gadget.ep0 = &uep->ep;
  918. usb_ep_set_maxpacket_limit(&uep->ep, 64);
  919. uep->ep.caps.type_control = true;
  920. } else {
  921. /* init normal pipe */
  922. if (pipe_configs[i].type == USB_ENDPOINT_XFER_ISOC)
  923. uep->ep.caps.type_iso = true;
  924. if (pipe_configs[i].type == USB_ENDPOINT_XFER_BULK)
  925. uep->ep.caps.type_bulk = true;
  926. if (pipe_configs[i].type == USB_ENDPOINT_XFER_INT)
  927. uep->ep.caps.type_int = true;
  928. usb_ep_set_maxpacket_limit(&uep->ep,
  929. pipe_configs[i].bufsize);
  930. list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
  931. }
  932. uep->ep.caps.dir_in = true;
  933. uep->ep.caps.dir_out = true;
  934. }
  935. ret = usb_add_gadget_udc(dev, &gpriv->gadget);
  936. if (ret)
  937. goto err_add_udc;
  938. dev_info(dev, "gadget probed\n");
  939. return 0;
  940. err_add_udc:
  941. kfree(gpriv->uep);
  942. usbhs_mod_gadget_probe_err_gpriv:
  943. kfree(gpriv);
  944. return ret;
  945. }
  946. void usbhs_mod_gadget_remove(struct usbhs_priv *priv)
  947. {
  948. struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
  949. usb_del_gadget_udc(&gpriv->gadget);
  950. kfree(gpriv->uep);
  951. kfree(gpriv);
  952. }