Merge 3.0-rc2 into usb-linus as it's needed by some USB patches
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / usb / renesas_usbhs / mod_gadget.c
blob46e247ad14f34497e13c2b4007d28e99c256a9f0
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/io.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/usb/ch9.h>
21 #include <linux/usb/gadget.h>
22 #include "common.h"
25 * struct
27 struct usbhsg_request {
28 struct usb_request req;
29 struct usbhs_pkt pkt;
32 #define EP_NAME_SIZE 8
33 struct usbhsg_gpriv;
34 struct usbhsg_uep {
35 struct usb_ep ep;
36 struct usbhs_pipe *pipe;
38 char ep_name[EP_NAME_SIZE];
40 struct usbhsg_gpriv *gpriv;
41 struct usbhs_pkt_handle *handler;
44 struct usbhsg_gpriv {
45 struct usb_gadget gadget;
46 struct usbhs_mod mod;
48 struct usbhsg_uep *uep;
49 int uep_size;
51 struct usb_gadget_driver *driver;
53 u32 status;
54 #define USBHSG_STATUS_STARTED (1 << 0)
55 #define USBHSG_STATUS_REGISTERD (1 << 1)
56 #define USBHSG_STATUS_WEDGE (1 << 2)
59 struct usbhsg_recip_handle {
60 char *name;
61 int (*device)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
62 struct usb_ctrlrequest *ctrl);
63 int (*interface)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
64 struct usb_ctrlrequest *ctrl);
65 int (*endpoint)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
66 struct usb_ctrlrequest *ctrl);
70 * macro
72 #define usbhsg_priv_to_gpriv(priv) \
73 container_of( \
74 usbhs_mod_get(priv, USBHS_GADGET), \
75 struct usbhsg_gpriv, mod)
77 #define __usbhsg_for_each_uep(start, pos, g, i) \
78 for (i = start, pos = (g)->uep; \
79 i < (g)->uep_size; \
80 i++, pos = (g)->uep + i)
82 #define usbhsg_for_each_uep(pos, gpriv, i) \
83 __usbhsg_for_each_uep(1, pos, gpriv, i)
85 #define usbhsg_for_each_uep_with_dcp(pos, gpriv, i) \
86 __usbhsg_for_each_uep(0, pos, gpriv, i)
88 #define usbhsg_gadget_to_gpriv(g)\
89 container_of(g, struct usbhsg_gpriv, gadget)
91 #define usbhsg_req_to_ureq(r)\
92 container_of(r, struct usbhsg_request, req)
94 #define usbhsg_ep_to_uep(e) container_of(e, struct usbhsg_uep, ep)
95 #define usbhsg_gpriv_to_dev(gp) usbhs_priv_to_dev((gp)->mod.priv)
96 #define usbhsg_gpriv_to_priv(gp) ((gp)->mod.priv)
97 #define usbhsg_gpriv_to_dcp(gp) ((gp)->uep)
98 #define usbhsg_gpriv_to_nth_uep(gp, i) ((gp)->uep + i)
99 #define usbhsg_uep_to_gpriv(u) ((u)->gpriv)
100 #define usbhsg_uep_to_pipe(u) ((u)->pipe)
101 #define usbhsg_pipe_to_uep(p) ((p)->mod_private)
102 #define usbhsg_is_dcp(u) ((u) == usbhsg_gpriv_to_dcp((u)->gpriv))
104 #define usbhsg_ureq_to_pkt(u) (&(u)->pkt)
105 #define usbhsg_pkt_to_ureq(i) \
106 container_of(i, struct usbhsg_request, pkt)
108 #define usbhsg_is_not_connected(gp) ((gp)->gadget.speed == USB_SPEED_UNKNOWN)
110 /* status */
111 #define usbhsg_status_init(gp) do {(gp)->status = 0; } while (0)
112 #define usbhsg_status_set(gp, b) (gp->status |= b)
113 #define usbhsg_status_clr(gp, b) (gp->status &= ~b)
114 #define usbhsg_status_has(gp, b) (gp->status & b)
117 * list push/pop
119 static void usbhsg_queue_push(struct usbhsg_uep *uep,
120 struct usbhsg_request *ureq)
122 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
123 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
124 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
125 struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
126 struct usb_request *req = &ureq->req;
128 req->actual = 0;
129 req->status = -EINPROGRESS;
130 usbhs_pkt_push(pipe, pkt, uep->handler,
131 req->buf, req->length, req->zero);
133 dev_dbg(dev, "pipe %d : queue push (%d)\n",
134 usbhs_pipe_number(pipe),
135 req->length);
138 static void usbhsg_queue_pop(struct usbhsg_uep *uep,
139 struct usbhsg_request *ureq,
140 int status)
142 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
143 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
144 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
146 dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
148 ureq->req.status = status;
149 ureq->req.complete(&uep->ep, &ureq->req);
152 static void usbhsg_queue_done(struct usbhs_pkt *pkt)
154 struct usbhs_pipe *pipe = pkt->pipe;
155 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
156 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
158 ureq->req.actual = pkt->actual;
160 usbhsg_queue_pop(uep, ureq, 0);
163 static int usbhsg_dma_map(struct device *dev,
164 struct usbhs_pkt *pkt,
165 enum dma_data_direction dir)
167 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
168 struct usb_request *req = &ureq->req;
170 if (pkt->dma != DMA_ADDR_INVALID) {
171 dev_err(dev, "dma is already mapped\n");
172 return -EIO;
175 if (req->dma == DMA_ADDR_INVALID) {
176 pkt->dma = dma_map_single(dev, pkt->buf, pkt->length, dir);
177 } else {
178 dma_sync_single_for_device(dev, req->dma, req->length, dir);
179 pkt->dma = req->dma;
182 if (dma_mapping_error(dev, pkt->dma)) {
183 dev_err(dev, "dma mapping error %x\n", pkt->dma);
184 return -EIO;
187 return 0;
190 static int usbhsg_dma_unmap(struct device *dev,
191 struct usbhs_pkt *pkt,
192 enum dma_data_direction dir)
194 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
195 struct usb_request *req = &ureq->req;
197 if (pkt->dma == DMA_ADDR_INVALID) {
198 dev_err(dev, "dma is not mapped\n");
199 return -EIO;
202 if (req->dma == DMA_ADDR_INVALID)
203 dma_unmap_single(dev, pkt->dma, pkt->length, dir);
204 else
205 dma_sync_single_for_cpu(dev, req->dma, req->length, dir);
207 pkt->dma = DMA_ADDR_INVALID;
209 return 0;
212 static int usbhsg_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
214 struct usbhs_pipe *pipe = pkt->pipe;
215 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
216 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
217 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
218 enum dma_data_direction dir;
220 dir = usbhs_pipe_is_dir_in(pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
222 if (map)
223 return usbhsg_dma_map(dev, pkt, dir);
224 else
225 return usbhsg_dma_unmap(dev, pkt, dir);
229 * USB_TYPE_STANDARD / clear feature functions
231 static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
232 struct usbhsg_uep *uep,
233 struct usb_ctrlrequest *ctrl)
235 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
236 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
237 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
239 usbhs_dcp_control_transfer_done(pipe);
241 return 0;
244 static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
245 struct usbhsg_uep *uep,
246 struct usb_ctrlrequest *ctrl)
248 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
249 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
251 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
252 usbhs_pipe_disable(pipe);
253 usbhs_pipe_clear_sequence(pipe);
254 usbhs_pipe_enable(pipe);
257 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
259 return 0;
262 struct usbhsg_recip_handle req_clear_feature = {
263 .name = "clear feature",
264 .device = usbhsg_recip_handler_std_control_done,
265 .interface = usbhsg_recip_handler_std_control_done,
266 .endpoint = usbhsg_recip_handler_std_clear_endpoint,
270 * USB_TYPE handler
272 static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
273 struct usbhsg_recip_handle *handler,
274 struct usb_ctrlrequest *ctrl)
276 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
277 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
278 struct usbhsg_uep *uep;
279 struct usbhs_pipe *pipe;
280 int recip = ctrl->bRequestType & USB_RECIP_MASK;
281 int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
282 int ret;
283 int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
284 struct usb_ctrlrequest *ctrl);
285 char *msg;
287 uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
288 pipe = usbhsg_uep_to_pipe(uep);
289 if (!pipe) {
290 dev_err(dev, "wrong recip request\n");
291 ret = -EINVAL;
292 goto usbhsg_recip_run_handle_end;
295 switch (recip) {
296 case USB_RECIP_DEVICE:
297 msg = "DEVICE";
298 func = handler->device;
299 break;
300 case USB_RECIP_INTERFACE:
301 msg = "INTERFACE";
302 func = handler->interface;
303 break;
304 case USB_RECIP_ENDPOINT:
305 msg = "ENDPOINT";
306 func = handler->endpoint;
307 break;
308 default:
309 dev_warn(dev, "unsupported RECIP(%d)\n", recip);
310 func = NULL;
311 ret = -EINVAL;
314 if (func) {
315 unsigned long flags;
317 dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
319 /******************** spin lock ********************/
320 usbhs_lock(priv, flags);
321 ret = func(priv, uep, ctrl);
322 usbhs_unlock(priv, flags);
323 /******************** spin unlock ******************/
326 usbhsg_recip_run_handle_end:
327 usbhs_pkt_start(pipe);
329 return ret;
333 * irq functions
335 * it will be called from usbhs_interrupt
337 static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
338 struct usbhs_irq_state *irq_state)
340 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
341 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
343 gpriv->gadget.speed = usbhs_status_get_usb_speed(irq_state);
345 dev_dbg(dev, "state = %x : speed : %d\n",
346 usbhs_status_get_device_state(irq_state),
347 gpriv->gadget.speed);
349 return 0;
352 static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
353 struct usbhs_irq_state *irq_state)
355 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
356 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
357 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
358 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
359 struct usb_ctrlrequest ctrl;
360 struct usbhsg_recip_handle *recip_handler = NULL;
361 int stage = usbhs_status_get_ctrl_stage(irq_state);
362 int ret = 0;
364 dev_dbg(dev, "stage = %d\n", stage);
367 * see Manual
369 * "Operation"
370 * - "Interrupt Function"
371 * - "Control Transfer Stage Transition Interrupt"
372 * - Fig. "Control Transfer Stage Transitions"
375 switch (stage) {
376 case READ_DATA_STAGE:
377 dcp->handler = &usbhs_fifo_pio_push_handler;
378 break;
379 case WRITE_DATA_STAGE:
380 dcp->handler = &usbhs_fifo_pio_pop_handler;
381 break;
382 case NODATA_STATUS_STAGE:
383 dcp->handler = &usbhs_ctrl_stage_end_handler;
384 break;
385 default:
386 return ret;
390 * get usb request
392 usbhs_usbreq_get_val(priv, &ctrl);
394 switch (ctrl.bRequestType & USB_TYPE_MASK) {
395 case USB_TYPE_STANDARD:
396 switch (ctrl.bRequest) {
397 case USB_REQ_CLEAR_FEATURE:
398 recip_handler = &req_clear_feature;
399 break;
404 * setup stage / run recip
406 if (recip_handler)
407 ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
408 else
409 ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
411 if (ret < 0)
412 usbhs_pipe_stall(pipe);
414 return ret;
419 * usb_dcp_ops
422 static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
424 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
425 struct usbhs_pkt *pkt;
427 usbhs_pipe_disable(pipe);
429 while (1) {
430 pkt = usbhs_pkt_pop(pipe, NULL);
431 if (!pkt)
432 break;
435 return 0;
438 static void usbhsg_uep_init(struct usbhsg_gpriv *gpriv)
440 int i;
441 struct usbhsg_uep *uep;
443 usbhsg_for_each_uep_with_dcp(uep, gpriv, i)
444 uep->pipe = NULL;
449 * usb_ep_ops
452 static int usbhsg_ep_enable(struct usb_ep *ep,
453 const struct usb_endpoint_descriptor *desc)
455 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
456 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
457 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
458 struct usbhs_pipe *pipe;
459 int ret = -EIO;
462 * if it already have pipe,
463 * nothing to do
465 if (uep->pipe)
466 return 0;
468 pipe = usbhs_pipe_malloc(priv, desc);
469 if (pipe) {
470 uep->pipe = pipe;
471 pipe->mod_private = uep;
473 if (usb_endpoint_dir_in(desc))
474 uep->handler = &usbhs_fifo_pio_push_handler;
475 else
476 uep->handler = &usbhs_fifo_pio_pop_handler;
478 ret = 0;
481 return ret;
484 static int usbhsg_ep_disable(struct usb_ep *ep)
486 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
488 return usbhsg_pipe_disable(uep);
491 static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
492 gfp_t gfp_flags)
494 struct usbhsg_request *ureq;
496 ureq = kzalloc(sizeof *ureq, gfp_flags);
497 if (!ureq)
498 return NULL;
500 usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
502 ureq->req.dma = DMA_ADDR_INVALID;
504 return &ureq->req;
507 static void usbhsg_ep_free_request(struct usb_ep *ep,
508 struct usb_request *req)
510 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
512 WARN_ON(!list_empty(&ureq->pkt.node));
513 kfree(ureq);
516 static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
517 gfp_t gfp_flags)
519 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
520 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
521 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
522 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
524 /* param check */
525 if (usbhsg_is_not_connected(gpriv) ||
526 unlikely(!gpriv->driver) ||
527 unlikely(!pipe))
528 return -ESHUTDOWN;
530 usbhsg_queue_push(uep, ureq);
532 return 0;
535 static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
537 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
538 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
539 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
541 usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq));
542 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
544 return 0;
547 static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
549 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
550 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
551 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
552 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
553 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
554 unsigned long flags;
556 usbhsg_pipe_disable(uep);
558 dev_dbg(dev, "set halt %d (pipe %d)\n",
559 halt, usbhs_pipe_number(pipe));
561 /******************** spin lock ********************/
562 usbhs_lock(priv, flags);
564 if (halt)
565 usbhs_pipe_stall(pipe);
566 else
567 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);
574 usbhs_unlock(priv, flags);
575 /******************** spin unlock ******************/
577 return 0;
580 static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
582 return __usbhsg_ep_set_halt_wedge(ep, value, 0);
585 static int usbhsg_ep_set_wedge(struct usb_ep *ep)
587 return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
590 static struct usb_ep_ops usbhsg_ep_ops = {
591 .enable = usbhsg_ep_enable,
592 .disable = usbhsg_ep_disable,
594 .alloc_request = usbhsg_ep_alloc_request,
595 .free_request = usbhsg_ep_free_request,
597 .queue = usbhsg_ep_queue,
598 .dequeue = usbhsg_ep_dequeue,
600 .set_halt = usbhsg_ep_set_halt,
601 .set_wedge = usbhsg_ep_set_wedge,
605 * usb module start/end
607 static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
609 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
610 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
611 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
612 struct device *dev = usbhs_priv_to_dev(priv);
613 unsigned long flags;
614 int ret = 0;
616 /******************** spin lock ********************/
617 usbhs_lock(priv, flags);
619 usbhsg_status_set(gpriv, status);
620 if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
621 usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
622 ret = -1; /* not ready */
624 usbhs_unlock(priv, flags);
625 /******************** spin unlock ********************/
627 if (ret < 0)
628 return 0; /* not ready is not error */
631 * enable interrupt and systems if ready
633 dev_dbg(dev, "start gadget\n");
636 * pipe initialize and enable DCP
638 usbhs_pipe_init(priv,
639 usbhsg_queue_done,
640 usbhsg_dma_map_ctrl);
641 usbhs_fifo_init(priv);
642 usbhsg_uep_init(gpriv);
644 /* dcp init */
645 dcp->pipe = usbhs_dcp_malloc(priv);
646 dcp->pipe->mod_private = dcp;
649 * system config enble
650 * - HI speed
651 * - function
652 * - usb module
654 usbhs_sys_hispeed_ctrl(priv, 1);
655 usbhs_sys_function_ctrl(priv, 1);
656 usbhs_sys_usb_ctrl(priv, 1);
659 * enable irq callback
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);
665 return 0;
668 static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
670 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
671 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
672 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
673 struct device *dev = usbhs_priv_to_dev(priv);
674 unsigned long flags;
675 int ret = 0;
677 /******************** spin lock ********************/
678 usbhs_lock(priv, flags);
680 usbhsg_status_clr(gpriv, status);
681 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
682 !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
683 ret = -1; /* already done */
685 usbhs_unlock(priv, flags);
686 /******************** spin unlock ********************/
688 if (ret < 0)
689 return 0; /* already done is not error */
692 * disable interrupt and systems if 1st try
694 usbhs_fifo_quit(priv);
696 /* disable all irq */
697 mod->irq_dev_state = NULL;
698 mod->irq_ctrl_stage = NULL;
699 usbhs_irq_callback_update(priv, mod);
701 gpriv->gadget.speed = USB_SPEED_UNKNOWN;
703 /* disable sys */
704 usbhs_sys_hispeed_ctrl(priv, 0);
705 usbhs_sys_function_ctrl(priv, 0);
706 usbhs_sys_usb_ctrl(priv, 0);
708 usbhsg_pipe_disable(dcp);
710 if (gpriv->driver &&
711 gpriv->driver->disconnect)
712 gpriv->driver->disconnect(&gpriv->gadget);
714 dev_dbg(dev, "stop gadget\n");
716 return 0;
721 * linux usb function
724 struct usbhsg_gpriv *the_controller;
725 int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
726 int (*bind)(struct usb_gadget *))
728 struct usbhsg_gpriv *gpriv = the_controller;
729 struct usbhs_priv *priv;
730 struct device *dev;
731 int ret;
733 if (!bind ||
734 !driver ||
735 !driver->setup ||
736 driver->speed != USB_SPEED_HIGH)
737 return -EINVAL;
738 if (!gpriv)
739 return -ENODEV;
740 if (gpriv->driver)
741 return -EBUSY;
743 dev = usbhsg_gpriv_to_dev(gpriv);
744 priv = usbhsg_gpriv_to_priv(gpriv);
746 /* first hook up the driver ... */
747 gpriv->driver = driver;
748 gpriv->gadget.dev.driver = &driver->driver;
750 ret = device_add(&gpriv->gadget.dev);
751 if (ret) {
752 dev_err(dev, "device_add error %d\n", ret);
753 goto add_fail;
756 ret = bind(&gpriv->gadget);
757 if (ret) {
758 dev_err(dev, "bind to driver %s error %d\n",
759 driver->driver.name, ret);
760 goto bind_fail;
763 dev_dbg(dev, "bind %s\n", driver->driver.name);
765 return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
767 bind_fail:
768 device_del(&gpriv->gadget.dev);
769 add_fail:
770 gpriv->driver = NULL;
771 gpriv->gadget.dev.driver = NULL;
773 return ret;
775 EXPORT_SYMBOL(usb_gadget_probe_driver);
777 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
779 struct usbhsg_gpriv *gpriv = the_controller;
780 struct usbhs_priv *priv;
781 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
783 if (!gpriv)
784 return -ENODEV;
786 if (!driver ||
787 !driver->unbind ||
788 driver != gpriv->driver)
789 return -EINVAL;
791 dev = usbhsg_gpriv_to_dev(gpriv);
792 priv = usbhsg_gpriv_to_priv(gpriv);
794 usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
795 device_del(&gpriv->gadget.dev);
796 gpriv->driver = NULL;
798 if (driver->disconnect)
799 driver->disconnect(&gpriv->gadget);
801 driver->unbind(&gpriv->gadget);
802 dev_dbg(dev, "unbind %s\n", driver->driver.name);
804 return 0;
806 EXPORT_SYMBOL(usb_gadget_unregister_driver);
809 * usb gadget ops
811 static int usbhsg_get_frame(struct usb_gadget *gadget)
813 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
814 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
816 return usbhs_frame_get_num(priv);
819 static struct usb_gadget_ops usbhsg_gadget_ops = {
820 .get_frame = usbhsg_get_frame,
823 static int usbhsg_start(struct usbhs_priv *priv)
825 return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
828 static int usbhsg_stop(struct usbhs_priv *priv)
830 return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
833 int __devinit usbhs_mod_gadget_probe(struct usbhs_priv *priv)
835 struct usbhsg_gpriv *gpriv;
836 struct usbhsg_uep *uep;
837 struct device *dev = usbhs_priv_to_dev(priv);
838 int pipe_size = usbhs_get_dparam(priv, pipe_size);
839 int i;
841 gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
842 if (!gpriv) {
843 dev_err(dev, "Could not allocate gadget priv\n");
844 return -ENOMEM;
847 uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
848 if (!uep) {
849 dev_err(dev, "Could not allocate ep\n");
850 goto usbhs_mod_gadget_probe_err_gpriv;
854 * CAUTION
856 * There is no guarantee that it is possible to access usb module here.
857 * Don't accesses to it.
858 * The accesse will be enable after "usbhsg_start"
862 * register itself
864 usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
866 /* init gpriv */
867 gpriv->mod.name = "gadget";
868 gpriv->mod.start = usbhsg_start;
869 gpriv->mod.stop = usbhsg_stop;
870 gpriv->uep = uep;
871 gpriv->uep_size = pipe_size;
872 usbhsg_status_init(gpriv);
875 * init gadget
877 device_initialize(&gpriv->gadget.dev);
878 dev_set_name(&gpriv->gadget.dev, "gadget");
879 gpriv->gadget.dev.parent = dev;
880 gpriv->gadget.name = "renesas_usbhs_udc";
881 gpriv->gadget.ops = &usbhsg_gadget_ops;
882 gpriv->gadget.is_dualspeed = 1;
884 INIT_LIST_HEAD(&gpriv->gadget.ep_list);
887 * init usb_ep
889 usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
890 uep->gpriv = gpriv;
891 snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
893 uep->ep.name = uep->ep_name;
894 uep->ep.ops = &usbhsg_ep_ops;
895 INIT_LIST_HEAD(&uep->ep.ep_list);
897 /* init DCP */
898 if (usbhsg_is_dcp(uep)) {
899 gpriv->gadget.ep0 = &uep->ep;
900 uep->ep.maxpacket = 64;
902 /* init normal pipe */
903 else {
904 uep->ep.maxpacket = 512;
905 list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
909 the_controller = gpriv;
911 dev_info(dev, "gadget probed\n");
913 return 0;
915 usbhs_mod_gadget_probe_err_gpriv:
916 kfree(gpriv);
918 return -ENOMEM;
921 void __devexit usbhs_mod_gadget_remove(struct usbhs_priv *priv)
923 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
925 kfree(gpriv->uep);
926 kfree(gpriv);