mod_gadget.c 28 KB

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