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