mod_gadget.c 25 KB

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