4 * Copyright (C) 2011 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
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.
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
17 #include <linux/delay.h>
18 #include <linux/dma-mapping.h>
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/usb/ch9.h>
23 #include <linux/usb/gadget.h>
29 struct usbhsg_request {
30 struct usb_request req;
34 #define EP_NAME_SIZE 8
38 struct usbhs_pipe *pipe;
40 char ep_name[EP_NAME_SIZE];
42 struct usbhsg_gpriv *gpriv;
46 struct usb_gadget gadget;
49 struct usbhsg_uep *uep;
52 struct usb_gadget_driver *driver;
55 #define USBHSG_STATUS_STARTED (1 << 0)
56 #define USBHSG_STATUS_REGISTERD (1 << 1)
57 #define USBHSG_STATUS_WEDGE (1 << 2)
58 #define USBHSG_STATUS_SELF_POWERED (1 << 3)
59 #define USBHSG_STATUS_SOFT_CONNECT (1 << 4)
62 struct usbhsg_recip_handle {
64 int (*device)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
65 struct usb_ctrlrequest *ctrl);
66 int (*interface)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
67 struct usb_ctrlrequest *ctrl);
68 int (*endpoint)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
69 struct usb_ctrlrequest *ctrl);
75 #define usbhsg_priv_to_gpriv(priv) \
77 usbhs_mod_get(priv, USBHS_GADGET), \
78 struct usbhsg_gpriv, mod)
80 #define __usbhsg_for_each_uep(start, pos, g, i) \
82 ((i) < (g)->uep_size) && ((pos) = (g)->uep + (i)); \
85 #define usbhsg_for_each_uep(pos, gpriv, i) \
86 __usbhsg_for_each_uep(1, pos, gpriv, i)
88 #define usbhsg_for_each_uep_with_dcp(pos, gpriv, i) \
89 __usbhsg_for_each_uep(0, pos, gpriv, i)
91 #define usbhsg_gadget_to_gpriv(g)\
92 container_of(g, struct usbhsg_gpriv, gadget)
94 #define usbhsg_req_to_ureq(r)\
95 container_of(r, struct usbhsg_request, req)
97 #define usbhsg_ep_to_uep(e) container_of(e, struct usbhsg_uep, ep)
98 #define usbhsg_gpriv_to_dev(gp) usbhs_priv_to_dev((gp)->mod.priv)
99 #define usbhsg_gpriv_to_priv(gp) ((gp)->mod.priv)
100 #define usbhsg_gpriv_to_dcp(gp) ((gp)->uep)
101 #define usbhsg_gpriv_to_nth_uep(gp, i) ((gp)->uep + i)
102 #define usbhsg_uep_to_gpriv(u) ((u)->gpriv)
103 #define usbhsg_uep_to_pipe(u) ((u)->pipe)
104 #define usbhsg_pipe_to_uep(p) ((p)->mod_private)
105 #define usbhsg_is_dcp(u) ((u) == usbhsg_gpriv_to_dcp((u)->gpriv))
107 #define usbhsg_ureq_to_pkt(u) (&(u)->pkt)
108 #define usbhsg_pkt_to_ureq(i) \
109 container_of(i, struct usbhsg_request, pkt)
111 #define usbhsg_is_not_connected(gp) ((gp)->gadget.speed == USB_SPEED_UNKNOWN)
114 #define usbhsg_status_init(gp) do {(gp)->status = 0; } while (0)
115 #define usbhsg_status_set(gp, b) (gp->status |= b)
116 #define usbhsg_status_clr(gp, b) (gp->status &= ~b)
117 #define usbhsg_status_has(gp, b) (gp->status & b)
122 static void usbhsg_queue_pop(struct usbhsg_uep *uep,
123 struct usbhsg_request *ureq,
126 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
127 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
128 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
130 dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
132 ureq->req.status = status;
133 usb_gadget_giveback_request(&uep->ep, &ureq->req);
136 static void usbhsg_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt)
138 struct usbhs_pipe *pipe = pkt->pipe;
139 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
140 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
142 ureq->req.actual = pkt->actual;
144 usbhsg_queue_pop(uep, ureq, 0);
147 static void usbhsg_queue_push(struct usbhsg_uep *uep,
148 struct usbhsg_request *ureq)
150 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
151 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
152 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
153 struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
154 struct usb_request *req = &ureq->req;
157 req->status = -EINPROGRESS;
158 usbhs_pkt_push(pipe, pkt, usbhsg_queue_done,
159 req->buf, req->length, req->zero, -1);
160 usbhs_pkt_start(pipe);
162 dev_dbg(dev, "pipe %d : queue push (%d)\n",
163 usbhs_pipe_number(pipe),
170 static int usbhsg_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
172 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
173 struct usb_request *req = &ureq->req;
174 struct usbhs_pipe *pipe = pkt->pipe;
175 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
176 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
177 enum dma_data_direction dir;
180 dir = usbhs_pipe_is_dir_host(pipe);
183 /* it can not use scatter/gather */
184 WARN_ON(req->num_sgs);
186 ret = usb_gadget_map_request(&gpriv->gadget, req, dir);
192 usb_gadget_unmap_request(&gpriv->gadget, req, dir);
199 * USB_TYPE_STANDARD / clear feature functions
201 static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
202 struct usbhsg_uep *uep,
203 struct usb_ctrlrequest *ctrl)
205 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
206 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
207 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
209 usbhs_dcp_control_transfer_done(pipe);
214 static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
215 struct usbhsg_uep *uep,
216 struct usb_ctrlrequest *ctrl)
218 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
219 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
221 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
222 usbhs_pipe_disable(pipe);
223 usbhs_pipe_sequence_data0(pipe);
224 usbhs_pipe_enable(pipe);
227 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
229 usbhs_pkt_start(pipe);
234 static struct usbhsg_recip_handle req_clear_feature = {
235 .name = "clear feature",
236 .device = usbhsg_recip_handler_std_control_done,
237 .interface = usbhsg_recip_handler_std_control_done,
238 .endpoint = usbhsg_recip_handler_std_clear_endpoint,
242 * USB_TYPE_STANDARD / set feature functions
244 static int usbhsg_recip_handler_std_set_device(struct usbhs_priv *priv,
245 struct usbhsg_uep *uep,
246 struct usb_ctrlrequest *ctrl)
248 switch (le16_to_cpu(ctrl->wValue)) {
249 case USB_DEVICE_TEST_MODE:
250 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
252 usbhs_sys_set_test_mode(priv, le16_to_cpu(ctrl->wIndex >> 8));
255 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
262 static int usbhsg_recip_handler_std_set_endpoint(struct usbhs_priv *priv,
263 struct usbhsg_uep *uep,
264 struct usb_ctrlrequest *ctrl)
266 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
268 usbhs_pipe_stall(pipe);
270 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
275 static struct usbhsg_recip_handle req_set_feature = {
276 .name = "set feature",
277 .device = usbhsg_recip_handler_std_set_device,
278 .interface = usbhsg_recip_handler_std_control_done,
279 .endpoint = usbhsg_recip_handler_std_set_endpoint,
283 * USB_TYPE_STANDARD / get status functions
285 static void __usbhsg_recip_send_complete(struct usb_ep *ep,
286 struct usb_request *req)
288 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
290 /* free allocated recip-buffer/usb_request */
291 kfree(ureq->pkt.buf);
292 usb_ep_free_request(ep, req);
295 static void __usbhsg_recip_send_status(struct usbhsg_gpriv *gpriv,
296 unsigned short status)
298 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
299 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
300 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
301 struct usb_request *req;
304 /* alloc new usb_request for recip */
305 req = usb_ep_alloc_request(&dcp->ep, GFP_ATOMIC);
307 dev_err(dev, "recip request allocation fail\n");
311 /* alloc recip data buffer */
312 buf = kmalloc(sizeof(*buf), GFP_ATOMIC);
314 usb_ep_free_request(&dcp->ep, req);
315 dev_err(dev, "recip data allocation fail\n");
319 /* recip data is status */
320 *buf = cpu_to_le16(status);
322 /* allocated usb_request/buffer will be freed */
323 req->complete = __usbhsg_recip_send_complete;
325 req->length = sizeof(*buf);
329 pipe->handler = &usbhs_fifo_pio_push_handler;
330 usbhsg_queue_push(dcp, usbhsg_req_to_ureq(req));
333 static int usbhsg_recip_handler_std_get_device(struct usbhs_priv *priv,
334 struct usbhsg_uep *uep,
335 struct usb_ctrlrequest *ctrl)
337 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
338 unsigned short status = 0;
340 if (usbhsg_status_has(gpriv, USBHSG_STATUS_SELF_POWERED))
341 status = 1 << USB_DEVICE_SELF_POWERED;
343 __usbhsg_recip_send_status(gpriv, status);
348 static int usbhsg_recip_handler_std_get_interface(struct usbhs_priv *priv,
349 struct usbhsg_uep *uep,
350 struct usb_ctrlrequest *ctrl)
352 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
353 unsigned short status = 0;
355 __usbhsg_recip_send_status(gpriv, status);
360 static int usbhsg_recip_handler_std_get_endpoint(struct usbhs_priv *priv,
361 struct usbhsg_uep *uep,
362 struct usb_ctrlrequest *ctrl)
364 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
365 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
366 unsigned short status = 0;
368 if (usbhs_pipe_is_stall(pipe))
369 status = 1 << USB_ENDPOINT_HALT;
371 __usbhsg_recip_send_status(gpriv, status);
376 static struct usbhsg_recip_handle req_get_status = {
377 .name = "get status",
378 .device = usbhsg_recip_handler_std_get_device,
379 .interface = usbhsg_recip_handler_std_get_interface,
380 .endpoint = usbhsg_recip_handler_std_get_endpoint,
386 static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
387 struct usbhsg_recip_handle *handler,
388 struct usb_ctrlrequest *ctrl)
390 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
391 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
392 struct usbhsg_uep *uep;
393 struct usbhs_pipe *pipe;
394 int recip = ctrl->bRequestType & USB_RECIP_MASK;
395 int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
397 int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
398 struct usb_ctrlrequest *ctrl);
401 uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
402 pipe = usbhsg_uep_to_pipe(uep);
404 dev_err(dev, "wrong recip request\n");
409 case USB_RECIP_DEVICE:
411 func = handler->device;
413 case USB_RECIP_INTERFACE:
415 func = handler->interface;
417 case USB_RECIP_ENDPOINT:
419 func = handler->endpoint;
422 dev_warn(dev, "unsupported RECIP(%d)\n", recip);
428 dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
429 ret = func(priv, uep, ctrl);
438 * it will be called from usbhs_interrupt
440 static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
441 struct usbhs_irq_state *irq_state)
443 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
444 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
446 gpriv->gadget.speed = usbhs_bus_get_speed(priv);
448 dev_dbg(dev, "state = %x : speed : %d\n",
449 usbhs_status_get_device_state(irq_state),
450 gpriv->gadget.speed);
455 static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
456 struct usbhs_irq_state *irq_state)
458 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
459 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
460 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
461 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
462 struct usb_ctrlrequest ctrl;
463 struct usbhsg_recip_handle *recip_handler = NULL;
464 int stage = usbhs_status_get_ctrl_stage(irq_state);
467 dev_dbg(dev, "stage = %d\n", stage);
473 * - "Interrupt Function"
474 * - "Control Transfer Stage Transition Interrupt"
475 * - Fig. "Control Transfer Stage Transitions"
479 case READ_DATA_STAGE:
480 pipe->handler = &usbhs_fifo_pio_push_handler;
482 case WRITE_DATA_STAGE:
483 pipe->handler = &usbhs_fifo_pio_pop_handler;
485 case NODATA_STATUS_STAGE:
486 pipe->handler = &usbhs_ctrl_stage_end_handler;
488 case READ_STATUS_STAGE:
489 case WRITE_STATUS_STAGE:
490 usbhs_dcp_control_transfer_done(pipe);
498 usbhs_usbreq_get_val(priv, &ctrl);
500 switch (ctrl.bRequestType & USB_TYPE_MASK) {
501 case USB_TYPE_STANDARD:
502 switch (ctrl.bRequest) {
503 case USB_REQ_CLEAR_FEATURE:
504 recip_handler = &req_clear_feature;
506 case USB_REQ_SET_FEATURE:
507 recip_handler = &req_set_feature;
509 case USB_REQ_GET_STATUS:
510 recip_handler = &req_get_status;
516 * setup stage / run recip
519 ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
521 ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
524 usbhs_pipe_stall(pipe);
534 static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
536 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
537 struct usbhs_pkt *pkt;
540 pkt = usbhs_pkt_pop(pipe, NULL);
544 usbhsg_queue_pop(uep, usbhsg_pkt_to_ureq(pkt), -ECONNRESET);
547 usbhs_pipe_disable(pipe);
557 static int usbhsg_ep_enable(struct usb_ep *ep,
558 const struct usb_endpoint_descriptor *desc)
560 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
561 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
562 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
563 struct usbhs_pipe *pipe;
567 * if it already have pipe,
571 usbhs_pipe_clear(uep->pipe);
572 usbhs_pipe_sequence_data0(uep->pipe);
576 pipe = usbhs_pipe_malloc(priv,
577 usb_endpoint_type(desc),
578 usb_endpoint_dir_in(desc));
581 pipe->mod_private = uep;
583 /* set epnum / maxp */
584 usbhs_pipe_config_update(pipe, 0,
585 usb_endpoint_num(desc),
586 usb_endpoint_maxp(desc));
589 * usbhs_fifo_dma_push/pop_handler try to
590 * use dmaengine if possible.
591 * It will use pio handler if impossible.
593 if (usb_endpoint_dir_in(desc))
594 pipe->handler = &usbhs_fifo_dma_push_handler;
596 pipe->handler = &usbhs_fifo_dma_pop_handler;
604 static int usbhsg_ep_disable(struct usb_ep *ep)
606 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
607 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
612 usbhsg_pipe_disable(uep);
613 usbhs_pipe_free(pipe);
615 uep->pipe->mod_private = NULL;
621 static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
624 struct usbhsg_request *ureq;
626 ureq = kzalloc(sizeof *ureq, gfp_flags);
630 usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
635 static void usbhsg_ep_free_request(struct usb_ep *ep,
636 struct usb_request *req)
638 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
640 WARN_ON(!list_empty(&ureq->pkt.node));
644 static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
647 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
648 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
649 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
650 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
653 if (usbhsg_is_not_connected(gpriv) ||
654 unlikely(!gpriv->driver) ||
658 usbhsg_queue_push(uep, ureq);
663 static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
665 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
666 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
667 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
669 usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq));
670 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
675 static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
677 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
678 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
679 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
680 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
681 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
684 usbhsg_pipe_disable(uep);
686 dev_dbg(dev, "set halt %d (pipe %d)\n",
687 halt, usbhs_pipe_number(pipe));
689 /******************** spin lock ********************/
690 usbhs_lock(priv, flags);
693 usbhs_pipe_stall(pipe);
695 usbhs_pipe_disable(pipe);
698 usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
700 usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
702 usbhs_unlock(priv, flags);
703 /******************** spin unlock ******************/
708 static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
710 return __usbhsg_ep_set_halt_wedge(ep, value, 0);
713 static int usbhsg_ep_set_wedge(struct usb_ep *ep)
715 return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
718 static struct usb_ep_ops usbhsg_ep_ops = {
719 .enable = usbhsg_ep_enable,
720 .disable = usbhsg_ep_disable,
722 .alloc_request = usbhsg_ep_alloc_request,
723 .free_request = usbhsg_ep_free_request,
725 .queue = usbhsg_ep_queue,
726 .dequeue = usbhsg_ep_dequeue,
728 .set_halt = usbhsg_ep_set_halt,
729 .set_wedge = usbhsg_ep_set_wedge,
735 static int usbhsg_can_pullup(struct usbhs_priv *priv)
737 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
739 return gpriv->driver &&
740 usbhsg_status_has(gpriv, USBHSG_STATUS_SOFT_CONNECT);
743 static void usbhsg_update_pullup(struct usbhs_priv *priv)
745 if (usbhsg_can_pullup(priv))
746 usbhs_sys_function_pullup(priv, 1);
748 usbhs_sys_function_pullup(priv, 0);
752 * usb module start/end
754 static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
756 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
757 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
758 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
759 struct device *dev = usbhs_priv_to_dev(priv);
763 /******************** spin lock ********************/
764 usbhs_lock(priv, flags);
766 usbhsg_status_set(gpriv, status);
767 if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
768 usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
769 ret = -1; /* not ready */
771 usbhs_unlock(priv, flags);
772 /******************** spin unlock ********************/
775 return 0; /* not ready is not error */
778 * enable interrupt and systems if ready
780 dev_dbg(dev, "start gadget\n");
783 * pipe initialize and enable DCP
785 usbhs_fifo_init(priv);
786 usbhs_pipe_init(priv,
787 usbhsg_dma_map_ctrl);
789 /* dcp init instead of usbhsg_ep_enable() */
790 dcp->pipe = usbhs_dcp_malloc(priv);
791 dcp->pipe->mod_private = dcp;
792 usbhs_pipe_config_update(dcp->pipe, 0, 0, 64);
795 * system config enble
800 usbhs_sys_function_ctrl(priv, 1);
801 usbhsg_update_pullup(priv);
804 * enable irq callback
806 mod->irq_dev_state = usbhsg_irq_dev_state;
807 mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
808 usbhs_irq_callback_update(priv, mod);
813 static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
815 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
816 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
817 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
818 struct device *dev = usbhs_priv_to_dev(priv);
822 /******************** spin lock ********************/
823 usbhs_lock(priv, flags);
825 usbhsg_status_clr(gpriv, status);
826 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
827 !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
828 ret = -1; /* already done */
830 usbhs_unlock(priv, flags);
831 /******************** spin unlock ********************/
834 return 0; /* already done is not error */
837 * disable interrupt and systems if 1st try
839 usbhs_fifo_quit(priv);
841 /* disable all irq */
842 mod->irq_dev_state = NULL;
843 mod->irq_ctrl_stage = NULL;
844 usbhs_irq_callback_update(priv, mod);
846 gpriv->gadget.speed = USB_SPEED_UNKNOWN;
849 usbhs_sys_set_test_mode(priv, 0);
850 usbhs_sys_function_ctrl(priv, 0);
852 usbhsg_ep_disable(&dcp->ep);
854 dev_dbg(dev, "stop gadget\n");
864 static int usbhsg_gadget_start(struct usb_gadget *gadget,
865 struct usb_gadget_driver *driver)
867 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
868 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
872 driver->max_speed < USB_SPEED_FULL)
875 /* first hook up the driver ... */
876 gpriv->driver = driver;
878 return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
881 static int usbhsg_gadget_stop(struct usb_gadget *gadget)
883 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
884 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
886 usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
887 gpriv->driver = NULL;
895 static int usbhsg_get_frame(struct usb_gadget *gadget)
897 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
898 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
900 return usbhs_frame_get_num(priv);
903 static int usbhsg_pullup(struct usb_gadget *gadget, int is_on)
905 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
906 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
909 usbhs_lock(priv, flags);
911 usbhsg_status_set(gpriv, USBHSG_STATUS_SOFT_CONNECT);
913 usbhsg_status_clr(gpriv, USBHSG_STATUS_SOFT_CONNECT);
914 usbhsg_update_pullup(priv);
915 usbhs_unlock(priv, flags);
920 static int usbhsg_set_selfpowered(struct usb_gadget *gadget, int is_self)
922 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
925 usbhsg_status_set(gpriv, USBHSG_STATUS_SELF_POWERED);
927 usbhsg_status_clr(gpriv, USBHSG_STATUS_SELF_POWERED);
932 static const struct usb_gadget_ops usbhsg_gadget_ops = {
933 .get_frame = usbhsg_get_frame,
934 .set_selfpowered = usbhsg_set_selfpowered,
935 .udc_start = usbhsg_gadget_start,
936 .udc_stop = usbhsg_gadget_stop,
937 .pullup = usbhsg_pullup,
940 static int usbhsg_start(struct usbhs_priv *priv)
942 return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
945 static int usbhsg_stop(struct usbhs_priv *priv)
947 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
949 /* cable disconnect */
951 gpriv->driver->disconnect)
952 gpriv->driver->disconnect(&gpriv->gadget);
954 return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
957 int usbhs_mod_gadget_probe(struct usbhs_priv *priv)
959 struct usbhsg_gpriv *gpriv;
960 struct usbhsg_uep *uep;
961 struct device *dev = usbhs_priv_to_dev(priv);
962 int pipe_size = usbhs_get_dparam(priv, pipe_size);
966 gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
968 dev_err(dev, "Could not allocate gadget priv\n");
972 uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
974 dev_err(dev, "Could not allocate ep\n");
976 goto usbhs_mod_gadget_probe_err_gpriv;
982 * There is no guarantee that it is possible to access usb module here.
983 * Don't accesses to it.
984 * The accesse will be enable after "usbhsg_start"
990 usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
993 gpriv->mod.name = "gadget";
994 gpriv->mod.start = usbhsg_start;
995 gpriv->mod.stop = usbhsg_stop;
997 gpriv->uep_size = pipe_size;
998 usbhsg_status_init(gpriv);
1003 gpriv->gadget.dev.parent = dev;
1004 gpriv->gadget.name = "renesas_usbhs_udc";
1005 gpriv->gadget.ops = &usbhsg_gadget_ops;
1006 gpriv->gadget.max_speed = USB_SPEED_HIGH;
1008 INIT_LIST_HEAD(&gpriv->gadget.ep_list);
1013 usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
1016 snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
1018 uep->ep.name = uep->ep_name;
1019 uep->ep.ops = &usbhsg_ep_ops;
1020 INIT_LIST_HEAD(&uep->ep.ep_list);
1023 if (usbhsg_is_dcp(uep)) {
1024 gpriv->gadget.ep0 = &uep->ep;
1025 usb_ep_set_maxpacket_limit(&uep->ep, 64);
1027 /* init normal pipe */
1029 usb_ep_set_maxpacket_limit(&uep->ep, 512);
1030 list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
1034 ret = usb_add_gadget_udc(dev, &gpriv->gadget);
1039 dev_info(dev, "gadget probed\n");
1046 usbhs_mod_gadget_probe_err_gpriv:
1052 void usbhs_mod_gadget_remove(struct usbhs_priv *priv)
1054 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1056 usb_del_gadget_udc(&gpriv->gadget);