spi-topcliff-pch: add recovery processing in case wait-event timeout
[zen-stable.git] / drivers / usb / renesas_usbhs / mod_gadget.c
blobcc72a8f15913a51956b2094c5f8b90c3613e0441
1 /*
2 * Renesas USB driver
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>
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"
27 * struct
29 struct usbhsg_request {
30 struct usb_request req;
31 struct usbhs_pkt pkt;
34 #define EP_NAME_SIZE 8
35 struct usbhsg_gpriv;
36 struct usbhsg_uep {
37 struct usb_ep ep;
38 struct usbhs_pipe *pipe;
40 char ep_name[EP_NAME_SIZE];
42 struct usbhsg_gpriv *gpriv;
45 struct usbhsg_gpriv {
46 struct usb_gadget gadget;
47 struct usbhs_mod mod;
49 struct usbhsg_uep *uep;
50 int uep_size;
52 struct usb_gadget_driver *driver;
54 u32 status;
55 #define USBHSG_STATUS_STARTED (1 << 0)
56 #define USBHSG_STATUS_REGISTERD (1 << 1)
57 #define USBHSG_STATUS_WEDGE (1 << 2)
60 struct usbhsg_recip_handle {
61 char *name;
62 int (*device)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
63 struct usb_ctrlrequest *ctrl);
64 int (*interface)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
65 struct usb_ctrlrequest *ctrl);
66 int (*endpoint)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
67 struct usb_ctrlrequest *ctrl);
71 * macro
73 #define usbhsg_priv_to_gpriv(priv) \
74 container_of( \
75 usbhs_mod_get(priv, USBHS_GADGET), \
76 struct usbhsg_gpriv, mod)
78 #define __usbhsg_for_each_uep(start, pos, g, i) \
79 for (i = start, pos = (g)->uep + i; \
80 i < (g)->uep_size; \
81 i++, pos = (g)->uep + i)
83 #define usbhsg_for_each_uep(pos, gpriv, i) \
84 __usbhsg_for_each_uep(1, pos, gpriv, i)
86 #define usbhsg_for_each_uep_with_dcp(pos, gpriv, i) \
87 __usbhsg_for_each_uep(0, pos, gpriv, i)
89 #define usbhsg_gadget_to_gpriv(g)\
90 container_of(g, struct usbhsg_gpriv, gadget)
92 #define usbhsg_req_to_ureq(r)\
93 container_of(r, struct usbhsg_request, req)
95 #define usbhsg_ep_to_uep(e) container_of(e, struct usbhsg_uep, ep)
96 #define usbhsg_gpriv_to_dev(gp) usbhs_priv_to_dev((gp)->mod.priv)
97 #define usbhsg_gpriv_to_priv(gp) ((gp)->mod.priv)
98 #define usbhsg_gpriv_to_dcp(gp) ((gp)->uep)
99 #define usbhsg_gpriv_to_nth_uep(gp, i) ((gp)->uep + i)
100 #define usbhsg_uep_to_gpriv(u) ((u)->gpriv)
101 #define usbhsg_uep_to_pipe(u) ((u)->pipe)
102 #define usbhsg_pipe_to_uep(p) ((p)->mod_private)
103 #define usbhsg_is_dcp(u) ((u) == usbhsg_gpriv_to_dcp((u)->gpriv))
105 #define usbhsg_ureq_to_pkt(u) (&(u)->pkt)
106 #define usbhsg_pkt_to_ureq(i) \
107 container_of(i, struct usbhsg_request, pkt)
109 #define usbhsg_is_not_connected(gp) ((gp)->gadget.speed == USB_SPEED_UNKNOWN)
111 /* status */
112 #define usbhsg_status_init(gp) do {(gp)->status = 0; } while (0)
113 #define usbhsg_status_set(gp, b) (gp->status |= b)
114 #define usbhsg_status_clr(gp, b) (gp->status &= ~b)
115 #define usbhsg_status_has(gp, b) (gp->status & b)
118 * queue push/pop
120 static void usbhsg_queue_pop(struct usbhsg_uep *uep,
121 struct usbhsg_request *ureq,
122 int status)
124 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
125 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
126 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
128 dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
130 ureq->req.status = status;
131 ureq->req.complete(&uep->ep, &ureq->req);
134 static void usbhsg_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt)
136 struct usbhs_pipe *pipe = pkt->pipe;
137 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
138 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
140 ureq->req.actual = pkt->actual;
142 usbhsg_queue_pop(uep, ureq, 0);
145 static void usbhsg_queue_push(struct usbhsg_uep *uep,
146 struct usbhsg_request *ureq)
148 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
149 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
150 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
151 struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
152 struct usb_request *req = &ureq->req;
154 req->actual = 0;
155 req->status = -EINPROGRESS;
156 usbhs_pkt_push(pipe, pkt, usbhsg_queue_done,
157 req->buf, req->length, req->zero, -1);
158 usbhs_pkt_start(pipe);
160 dev_dbg(dev, "pipe %d : queue push (%d)\n",
161 usbhs_pipe_number(pipe),
162 req->length);
166 * dma map/unmap
168 static int usbhsg_dma_map(struct device *dev,
169 struct usbhs_pkt *pkt,
170 enum dma_data_direction dir)
172 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
173 struct usb_request *req = &ureq->req;
175 if (pkt->dma != DMA_ADDR_INVALID) {
176 dev_err(dev, "dma is already mapped\n");
177 return -EIO;
180 if (req->dma == DMA_ADDR_INVALID) {
181 pkt->dma = dma_map_single(dev, pkt->buf, pkt->length, dir);
182 } else {
183 dma_sync_single_for_device(dev, req->dma, req->length, dir);
184 pkt->dma = req->dma;
187 if (dma_mapping_error(dev, pkt->dma)) {
188 dev_err(dev, "dma mapping error %llx\n", (u64)pkt->dma);
189 return -EIO;
192 return 0;
195 static int usbhsg_dma_unmap(struct device *dev,
196 struct usbhs_pkt *pkt,
197 enum dma_data_direction dir)
199 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
200 struct usb_request *req = &ureq->req;
202 if (pkt->dma == DMA_ADDR_INVALID) {
203 dev_err(dev, "dma is not mapped\n");
204 return -EIO;
207 if (req->dma == DMA_ADDR_INVALID)
208 dma_unmap_single(dev, pkt->dma, pkt->length, dir);
209 else
210 dma_sync_single_for_cpu(dev, req->dma, req->length, dir);
212 pkt->dma = DMA_ADDR_INVALID;
214 return 0;
217 static int usbhsg_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
219 struct usbhs_pipe *pipe = pkt->pipe;
220 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
221 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
222 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
223 enum dma_data_direction dir;
225 dir = usbhs_pipe_is_dir_in(pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
227 if (map)
228 return usbhsg_dma_map(dev, pkt, dir);
229 else
230 return usbhsg_dma_unmap(dev, pkt, dir);
234 * USB_TYPE_STANDARD / clear feature functions
236 static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
237 struct usbhsg_uep *uep,
238 struct usb_ctrlrequest *ctrl)
240 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
241 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
242 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
244 usbhs_dcp_control_transfer_done(pipe);
246 return 0;
249 static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
250 struct usbhsg_uep *uep,
251 struct usb_ctrlrequest *ctrl)
253 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
254 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
256 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
257 usbhs_pipe_disable(pipe);
258 usbhs_pipe_sequence_data0(pipe);
259 usbhs_pipe_enable(pipe);
262 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
264 usbhs_pkt_start(pipe);
266 return 0;
269 struct usbhsg_recip_handle req_clear_feature = {
270 .name = "clear feature",
271 .device = usbhsg_recip_handler_std_control_done,
272 .interface = usbhsg_recip_handler_std_control_done,
273 .endpoint = usbhsg_recip_handler_std_clear_endpoint,
277 * USB_TYPE_STANDARD / set feature functions
279 static int usbhsg_recip_handler_std_set_device(struct usbhs_priv *priv,
280 struct usbhsg_uep *uep,
281 struct usb_ctrlrequest *ctrl)
283 switch (le16_to_cpu(ctrl->wValue)) {
284 case USB_DEVICE_TEST_MODE:
285 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
286 udelay(100);
287 usbhs_sys_set_test_mode(priv, le16_to_cpu(ctrl->wIndex >> 8));
288 break;
289 default:
290 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
291 break;
294 return 0;
297 static int usbhsg_recip_handler_std_set_endpoint(struct usbhs_priv *priv,
298 struct usbhsg_uep *uep,
299 struct usb_ctrlrequest *ctrl)
301 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
303 usbhs_pipe_stall(pipe);
305 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
307 return 0;
310 struct usbhsg_recip_handle req_set_feature = {
311 .name = "set feature",
312 .device = usbhsg_recip_handler_std_set_device,
313 .interface = usbhsg_recip_handler_std_control_done,
314 .endpoint = usbhsg_recip_handler_std_set_endpoint,
318 * USB_TYPE_STANDARD / get status functions
320 static void __usbhsg_recip_send_complete(struct usb_ep *ep,
321 struct usb_request *req)
323 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
325 /* free allocated recip-buffer/usb_request */
326 kfree(ureq->pkt.buf);
327 usb_ep_free_request(ep, req);
330 static void __usbhsg_recip_send_status(struct usbhsg_gpriv *gpriv,
331 unsigned short status)
333 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
334 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
335 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
336 struct usb_request *req;
337 unsigned short *buf;
339 /* alloc new usb_request for recip */
340 req = usb_ep_alloc_request(&dcp->ep, GFP_ATOMIC);
341 if (!req) {
342 dev_err(dev, "recip request allocation fail\n");
343 return;
346 /* alloc recip data buffer */
347 buf = kmalloc(sizeof(*buf), GFP_ATOMIC);
348 if (!buf) {
349 usb_ep_free_request(&dcp->ep, req);
350 dev_err(dev, "recip data allocation fail\n");
351 return;
354 /* recip data is status */
355 *buf = cpu_to_le16(status);
357 /* allocated usb_request/buffer will be freed */
358 req->complete = __usbhsg_recip_send_complete;
359 req->buf = buf;
360 req->length = sizeof(*buf);
361 req->zero = 0;
363 /* push packet */
364 pipe->handler = &usbhs_fifo_pio_push_handler;
365 usbhsg_queue_push(dcp, usbhsg_req_to_ureq(req));
368 static int usbhsg_recip_handler_std_get_device(struct usbhs_priv *priv,
369 struct usbhsg_uep *uep,
370 struct usb_ctrlrequest *ctrl)
372 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
373 unsigned short status = 1 << USB_DEVICE_SELF_POWERED;
375 __usbhsg_recip_send_status(gpriv, status);
377 return 0;
380 static int usbhsg_recip_handler_std_get_interface(struct usbhs_priv *priv,
381 struct usbhsg_uep *uep,
382 struct usb_ctrlrequest *ctrl)
384 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
385 unsigned short status = 0;
387 __usbhsg_recip_send_status(gpriv, status);
389 return 0;
392 static int usbhsg_recip_handler_std_get_endpoint(struct usbhs_priv *priv,
393 struct usbhsg_uep *uep,
394 struct usb_ctrlrequest *ctrl)
396 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
397 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
398 unsigned short status = 0;
400 if (usbhs_pipe_is_stall(pipe))
401 status = 1 << USB_ENDPOINT_HALT;
403 __usbhsg_recip_send_status(gpriv, status);
405 return 0;
408 struct usbhsg_recip_handle req_get_status = {
409 .name = "get status",
410 .device = usbhsg_recip_handler_std_get_device,
411 .interface = usbhsg_recip_handler_std_get_interface,
412 .endpoint = usbhsg_recip_handler_std_get_endpoint,
416 * USB_TYPE handler
418 static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
419 struct usbhsg_recip_handle *handler,
420 struct usb_ctrlrequest *ctrl)
422 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
423 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
424 struct usbhsg_uep *uep;
425 struct usbhs_pipe *pipe;
426 int recip = ctrl->bRequestType & USB_RECIP_MASK;
427 int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
428 int ret = 0;
429 int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
430 struct usb_ctrlrequest *ctrl);
431 char *msg;
433 uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
434 pipe = usbhsg_uep_to_pipe(uep);
435 if (!pipe) {
436 dev_err(dev, "wrong recip request\n");
437 return -EINVAL;
440 switch (recip) {
441 case USB_RECIP_DEVICE:
442 msg = "DEVICE";
443 func = handler->device;
444 break;
445 case USB_RECIP_INTERFACE:
446 msg = "INTERFACE";
447 func = handler->interface;
448 break;
449 case USB_RECIP_ENDPOINT:
450 msg = "ENDPOINT";
451 func = handler->endpoint;
452 break;
453 default:
454 dev_warn(dev, "unsupported RECIP(%d)\n", recip);
455 func = NULL;
456 ret = -EINVAL;
459 if (func) {
460 dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
461 ret = func(priv, uep, ctrl);
464 return ret;
468 * irq functions
470 * it will be called from usbhs_interrupt
472 static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
473 struct usbhs_irq_state *irq_state)
475 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
476 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
478 gpriv->gadget.speed = usbhs_bus_get_speed(priv);
480 dev_dbg(dev, "state = %x : speed : %d\n",
481 usbhs_status_get_device_state(irq_state),
482 gpriv->gadget.speed);
484 return 0;
487 static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
488 struct usbhs_irq_state *irq_state)
490 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
491 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
492 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
493 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
494 struct usb_ctrlrequest ctrl;
495 struct usbhsg_recip_handle *recip_handler = NULL;
496 int stage = usbhs_status_get_ctrl_stage(irq_state);
497 int ret = 0;
499 dev_dbg(dev, "stage = %d\n", stage);
502 * see Manual
504 * "Operation"
505 * - "Interrupt Function"
506 * - "Control Transfer Stage Transition Interrupt"
507 * - Fig. "Control Transfer Stage Transitions"
510 switch (stage) {
511 case READ_DATA_STAGE:
512 pipe->handler = &usbhs_fifo_pio_push_handler;
513 break;
514 case WRITE_DATA_STAGE:
515 pipe->handler = &usbhs_fifo_pio_pop_handler;
516 break;
517 case NODATA_STATUS_STAGE:
518 pipe->handler = &usbhs_ctrl_stage_end_handler;
519 break;
520 default:
521 return ret;
525 * get usb request
527 usbhs_usbreq_get_val(priv, &ctrl);
529 switch (ctrl.bRequestType & USB_TYPE_MASK) {
530 case USB_TYPE_STANDARD:
531 switch (ctrl.bRequest) {
532 case USB_REQ_CLEAR_FEATURE:
533 recip_handler = &req_clear_feature;
534 break;
535 case USB_REQ_SET_FEATURE:
536 recip_handler = &req_set_feature;
537 break;
538 case USB_REQ_GET_STATUS:
539 recip_handler = &req_get_status;
540 break;
545 * setup stage / run recip
547 if (recip_handler)
548 ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
549 else
550 ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
552 if (ret < 0)
553 usbhs_pipe_stall(pipe);
555 return ret;
560 * usb_dcp_ops
563 static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
565 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
566 struct usbhs_pkt *pkt;
568 while (1) {
569 pkt = usbhs_pkt_pop(pipe, NULL);
570 if (!pkt)
571 break;
573 usbhsg_queue_pop(uep, usbhsg_pkt_to_ureq(pkt), -ECONNRESET);
576 usbhs_pipe_disable(pipe);
578 return 0;
581 static void usbhsg_uep_init(struct usbhsg_gpriv *gpriv)
583 int i;
584 struct usbhsg_uep *uep;
586 usbhsg_for_each_uep_with_dcp(uep, gpriv, i)
587 uep->pipe = NULL;
592 * usb_ep_ops
595 static int usbhsg_ep_enable(struct usb_ep *ep,
596 const struct usb_endpoint_descriptor *desc)
598 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
599 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
600 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
601 struct usbhs_pipe *pipe;
602 int ret = -EIO;
605 * if it already have pipe,
606 * nothing to do
608 if (uep->pipe) {
609 usbhs_pipe_clear(uep->pipe);
610 usbhs_pipe_sequence_data0(uep->pipe);
611 return 0;
614 pipe = usbhs_pipe_malloc(priv,
615 usb_endpoint_type(desc),
616 usb_endpoint_dir_in(desc));
617 if (pipe) {
618 uep->pipe = pipe;
619 pipe->mod_private = uep;
621 /* set epnum / maxp */
622 usbhs_pipe_config_update(pipe, 0,
623 usb_endpoint_num(desc),
624 usb_endpoint_maxp(desc));
627 * usbhs_fifo_dma_push/pop_handler try to
628 * use dmaengine if possible.
629 * It will use pio handler if impossible.
631 if (usb_endpoint_dir_in(desc))
632 pipe->handler = &usbhs_fifo_dma_push_handler;
633 else
634 pipe->handler = &usbhs_fifo_dma_pop_handler;
636 ret = 0;
639 return ret;
642 static int usbhsg_ep_disable(struct usb_ep *ep)
644 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
646 return usbhsg_pipe_disable(uep);
649 static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
650 gfp_t gfp_flags)
652 struct usbhsg_request *ureq;
654 ureq = kzalloc(sizeof *ureq, gfp_flags);
655 if (!ureq)
656 return NULL;
658 usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
660 ureq->req.dma = DMA_ADDR_INVALID;
662 return &ureq->req;
665 static void usbhsg_ep_free_request(struct usb_ep *ep,
666 struct usb_request *req)
668 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
670 WARN_ON(!list_empty(&ureq->pkt.node));
671 kfree(ureq);
674 static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
675 gfp_t gfp_flags)
677 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
678 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
679 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
680 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
682 /* param check */
683 if (usbhsg_is_not_connected(gpriv) ||
684 unlikely(!gpriv->driver) ||
685 unlikely(!pipe))
686 return -ESHUTDOWN;
688 usbhsg_queue_push(uep, ureq);
690 return 0;
693 static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
695 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
696 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
697 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
699 usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq));
700 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
702 return 0;
705 static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
707 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
708 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
709 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
710 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
711 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
712 unsigned long flags;
714 usbhsg_pipe_disable(uep);
716 dev_dbg(dev, "set halt %d (pipe %d)\n",
717 halt, usbhs_pipe_number(pipe));
719 /******************** spin lock ********************/
720 usbhs_lock(priv, flags);
722 if (halt)
723 usbhs_pipe_stall(pipe);
724 else
725 usbhs_pipe_disable(pipe);
727 if (halt && wedge)
728 usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
729 else
730 usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
732 usbhs_unlock(priv, flags);
733 /******************** spin unlock ******************/
735 return 0;
738 static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
740 return __usbhsg_ep_set_halt_wedge(ep, value, 0);
743 static int usbhsg_ep_set_wedge(struct usb_ep *ep)
745 return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
748 static struct usb_ep_ops usbhsg_ep_ops = {
749 .enable = usbhsg_ep_enable,
750 .disable = usbhsg_ep_disable,
752 .alloc_request = usbhsg_ep_alloc_request,
753 .free_request = usbhsg_ep_free_request,
755 .queue = usbhsg_ep_queue,
756 .dequeue = usbhsg_ep_dequeue,
758 .set_halt = usbhsg_ep_set_halt,
759 .set_wedge = usbhsg_ep_set_wedge,
763 * usb module start/end
765 static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
767 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
768 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
769 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
770 struct device *dev = usbhs_priv_to_dev(priv);
771 unsigned long flags;
772 int ret = 0;
774 /******************** spin lock ********************/
775 usbhs_lock(priv, flags);
777 usbhsg_status_set(gpriv, status);
778 if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
779 usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
780 ret = -1; /* not ready */
782 usbhs_unlock(priv, flags);
783 /******************** spin unlock ********************/
785 if (ret < 0)
786 return 0; /* not ready is not error */
789 * enable interrupt and systems if ready
791 dev_dbg(dev, "start gadget\n");
794 * pipe initialize and enable DCP
796 usbhs_pipe_init(priv,
797 usbhsg_dma_map_ctrl);
798 usbhs_fifo_init(priv);
799 usbhsg_uep_init(gpriv);
801 /* dcp init */
802 dcp->pipe = usbhs_dcp_malloc(priv);
803 dcp->pipe->mod_private = dcp;
804 usbhs_pipe_config_update(dcp->pipe, 0, 0, 64);
807 * system config enble
808 * - HI speed
809 * - function
810 * - usb module
812 usbhs_sys_function_ctrl(priv, 1);
815 * enable irq callback
817 mod->irq_dev_state = usbhsg_irq_dev_state;
818 mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
819 usbhs_irq_callback_update(priv, mod);
821 return 0;
824 static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
826 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
827 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
828 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
829 struct device *dev = usbhs_priv_to_dev(priv);
830 unsigned long flags;
831 int ret = 0;
833 /******************** spin lock ********************/
834 usbhs_lock(priv, flags);
836 usbhsg_status_clr(gpriv, status);
837 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
838 !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
839 ret = -1; /* already done */
841 usbhs_unlock(priv, flags);
842 /******************** spin unlock ********************/
844 if (ret < 0)
845 return 0; /* already done is not error */
848 * disable interrupt and systems if 1st try
850 usbhs_fifo_quit(priv);
852 /* disable all irq */
853 mod->irq_dev_state = NULL;
854 mod->irq_ctrl_stage = NULL;
855 usbhs_irq_callback_update(priv, mod);
857 gpriv->gadget.speed = USB_SPEED_UNKNOWN;
859 /* disable sys */
860 usbhs_sys_set_test_mode(priv, 0);
861 usbhs_sys_function_ctrl(priv, 0);
863 usbhsg_pipe_disable(dcp);
865 dev_dbg(dev, "stop gadget\n");
867 return 0;
872 * linux usb function
875 static int usbhsg_gadget_start(struct usb_gadget *gadget,
876 struct usb_gadget_driver *driver)
878 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
879 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
881 if (!driver ||
882 !driver->setup ||
883 driver->max_speed < USB_SPEED_FULL)
884 return -EINVAL;
886 /* first hook up the driver ... */
887 gpriv->driver = driver;
888 gpriv->gadget.dev.driver = &driver->driver;
890 return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
893 static int usbhsg_gadget_stop(struct usb_gadget *gadget,
894 struct usb_gadget_driver *driver)
896 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
897 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
899 if (!driver ||
900 !driver->unbind)
901 return -EINVAL;
903 usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
904 gpriv->gadget.dev.driver = NULL;
905 gpriv->driver = NULL;
907 return 0;
911 * usb gadget ops
913 static int usbhsg_get_frame(struct usb_gadget *gadget)
915 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
916 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
918 return usbhs_frame_get_num(priv);
921 static struct usb_gadget_ops usbhsg_gadget_ops = {
922 .get_frame = usbhsg_get_frame,
923 .udc_start = usbhsg_gadget_start,
924 .udc_stop = usbhsg_gadget_stop,
927 static int usbhsg_start(struct usbhs_priv *priv)
929 return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
932 static int usbhsg_stop(struct usbhs_priv *priv)
934 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
936 /* cable disconnect */
937 if (gpriv->driver &&
938 gpriv->driver->disconnect)
939 gpriv->driver->disconnect(&gpriv->gadget);
941 return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
944 static void usbhs_mod_gadget_release(struct device *pdev)
946 /* do nothing */
949 int usbhs_mod_gadget_probe(struct usbhs_priv *priv)
951 struct usbhsg_gpriv *gpriv;
952 struct usbhsg_uep *uep;
953 struct device *dev = usbhs_priv_to_dev(priv);
954 int pipe_size = usbhs_get_dparam(priv, pipe_size);
955 int i;
956 int ret;
958 gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
959 if (!gpriv) {
960 dev_err(dev, "Could not allocate gadget priv\n");
961 return -ENOMEM;
964 uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
965 if (!uep) {
966 dev_err(dev, "Could not allocate ep\n");
967 ret = -ENOMEM;
968 goto usbhs_mod_gadget_probe_err_gpriv;
972 * CAUTION
974 * There is no guarantee that it is possible to access usb module here.
975 * Don't accesses to it.
976 * The accesse will be enable after "usbhsg_start"
980 * register itself
982 usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
984 /* init gpriv */
985 gpriv->mod.name = "gadget";
986 gpriv->mod.start = usbhsg_start;
987 gpriv->mod.stop = usbhsg_stop;
988 gpriv->uep = uep;
989 gpriv->uep_size = pipe_size;
990 usbhsg_status_init(gpriv);
993 * init gadget
995 dev_set_name(&gpriv->gadget.dev, "gadget");
996 gpriv->gadget.dev.parent = dev;
997 gpriv->gadget.dev.release = usbhs_mod_gadget_release;
998 gpriv->gadget.name = "renesas_usbhs_udc";
999 gpriv->gadget.ops = &usbhsg_gadget_ops;
1000 gpriv->gadget.max_speed = USB_SPEED_HIGH;
1001 ret = device_register(&gpriv->gadget.dev);
1002 if (ret < 0)
1003 goto err_add_udc;
1005 INIT_LIST_HEAD(&gpriv->gadget.ep_list);
1008 * init usb_ep
1010 usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
1011 uep->gpriv = gpriv;
1012 snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
1014 uep->ep.name = uep->ep_name;
1015 uep->ep.ops = &usbhsg_ep_ops;
1016 INIT_LIST_HEAD(&uep->ep.ep_list);
1018 /* init DCP */
1019 if (usbhsg_is_dcp(uep)) {
1020 gpriv->gadget.ep0 = &uep->ep;
1021 uep->ep.maxpacket = 64;
1023 /* init normal pipe */
1024 else {
1025 uep->ep.maxpacket = 512;
1026 list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
1030 ret = usb_add_gadget_udc(dev, &gpriv->gadget);
1031 if (ret)
1032 goto err_register;
1035 dev_info(dev, "gadget probed\n");
1037 return 0;
1039 err_register:
1040 device_unregister(&gpriv->gadget.dev);
1041 err_add_udc:
1042 kfree(gpriv->uep);
1044 usbhs_mod_gadget_probe_err_gpriv:
1045 kfree(gpriv);
1047 return ret;
1050 void usbhs_mod_gadget_remove(struct usbhs_priv *priv)
1052 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1054 usb_del_gadget_udc(&gpriv->gadget);
1056 device_unregister(&gpriv->gadget.dev);
1058 kfree(gpriv->uep);
1059 kfree(gpriv);