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
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/usb/ch9.h>
21 #include <linux/usb/gadget.h>
27 struct usbhsg_request {
28 struct usb_request req;
32 #define EP_NAME_SIZE 8
36 struct usbhs_pipe *pipe;
38 char ep_name[EP_NAME_SIZE];
40 struct usbhsg_gpriv *gpriv;
41 struct usbhs_pkt_handle *handler;
45 struct usb_gadget gadget;
48 struct usbhsg_uep *uep;
51 struct usb_gadget_driver *driver;
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 {
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);
72 #define usbhsg_priv_to_gpriv(priv) \
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; \
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_lock(gp) usbhs_priv_to_lock((gp)->mod.priv)
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)
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)
120 * This driver don't use spin_try_lock
121 * to avoid warning of CONFIG_DEBUG_SPINLOCK
123 static spinlock_t *usbhsg_trylock(struct usbhsg_gpriv *gpriv,
124 unsigned long *flags)
126 spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
128 /* check spin lock status
129 * to avoid deadlock/nest */
130 if (spin_is_locked(lock))
133 spin_lock_irqsave(lock, *flags);
138 static void usbhsg_unlock(spinlock_t *lock, unsigned long *flags)
143 spin_unlock_irqrestore(lock, *flags);
149 static void usbhsg_queue_push(struct usbhsg_uep *uep,
150 struct usbhsg_request *ureq)
152 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
153 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
154 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
155 struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
156 struct usb_request *req = &ureq->req;
159 ********* assume under spin lock *********
161 usbhs_pkt_push(pipe, pkt, uep->handler,
162 req->buf, req->length, req->zero);
164 req->status = -EINPROGRESS;
166 dev_dbg(dev, "pipe %d : queue push (%d)\n",
167 usbhs_pipe_number(pipe),
171 static int usbhsg_queue_start(struct usbhsg_uep *uep)
173 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
174 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
175 struct usbhs_pkt *pkt;
181 * CAUTION [*queue handler*]
183 * This function will be called for start/restart queue operation.
184 * OTOH the most much worry for USB driver is spinlock nest.
186 * - usb_ep_ops :: queue
187 * - usb_request :: complete
189 * But the caller of this function need not care about spinlock.
190 * This function is using usbhsg_trylock for it.
191 * if "is_locked" is 1, this mean this function lock it.
192 * but if it is 0, this mean it is already under spin lock.
194 * CAUTION [*endpoint queue*]
195 * CAUTION [*request complete*]
198 /****************** spin try lock *******************/
199 lock = usbhsg_trylock(gpriv, &flags);
201 pkt = usbhs_pkt_get(pipe);
203 ret = usbhs_pkt_start(pkt);
205 usbhsg_unlock(lock, &flags);
206 /******************** spin unlock ******************/
211 static void usbhsg_queue_pop(struct usbhsg_uep *uep,
212 struct usbhsg_request *ureq,
215 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
216 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
217 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
218 struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
221 ********* assume under spin lock *********
225 * CAUTION [*request complete*]
227 * There is a possibility not to be called in correct order
228 * if "complete" is called without spinlock.
230 * So, this function assume it is under spinlock,
231 * and call usb_request :: complete.
233 * But this "complete" will push next usb_request.
234 * It mean "usb_ep_ops :: queue" which is using spinlock is called
237 * To avoid dead-lock, this driver is using usbhsg_trylock.
238 * CAUTION [*endpoint queue*]
239 * CAUTION [*queue handler*]
242 dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
246 ureq->req.status = status;
247 ureq->req.complete(&uep->ep, &ureq->req);
251 usbhsg_queue_start(uep);
254 static void usbhsg_queue_done(struct usbhs_pkt *pkt)
256 struct usbhs_pipe *pipe = pkt->pipe;
257 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
258 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
260 ureq->req.actual = pkt->actual;
262 usbhsg_queue_pop(uep, ureq, 0);
266 * USB_TYPE_STANDARD / clear feature functions
268 static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
269 struct usbhsg_uep *uep,
270 struct usb_ctrlrequest *ctrl)
272 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
273 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
274 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
276 usbhs_dcp_control_transfer_done(pipe);
281 static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
282 struct usbhsg_uep *uep,
283 struct usb_ctrlrequest *ctrl)
285 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
286 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
288 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
289 usbhs_pipe_disable(pipe);
290 usbhs_pipe_clear_sequence(pipe);
291 usbhs_pipe_enable(pipe);
294 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
296 usbhsg_queue_start(uep);
301 struct usbhsg_recip_handle req_clear_feature = {
302 .name = "clear feature",
303 .device = usbhsg_recip_handler_std_control_done,
304 .interface = usbhsg_recip_handler_std_control_done,
305 .endpoint = usbhsg_recip_handler_std_clear_endpoint,
311 static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
312 struct usbhsg_recip_handle *handler,
313 struct usb_ctrlrequest *ctrl)
315 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
316 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
317 struct usbhsg_uep *uep;
318 int recip = ctrl->bRequestType & USB_RECIP_MASK;
319 int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
321 int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
322 struct usb_ctrlrequest *ctrl);
325 uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
326 if (!usbhsg_uep_to_pipe(uep)) {
327 dev_err(dev, "wrong recip request\n");
332 case USB_RECIP_DEVICE:
334 func = handler->device;
336 case USB_RECIP_INTERFACE:
338 func = handler->interface;
340 case USB_RECIP_ENDPOINT:
342 func = handler->endpoint;
345 dev_warn(dev, "unsupported RECIP(%d)\n", recip);
351 dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
352 ret = func(priv, uep, ctrl);
361 * it will be called from usbhs_interrupt
363 static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
364 struct usbhs_irq_state *irq_state)
366 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
367 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
369 gpriv->gadget.speed = usbhs_status_get_usb_speed(irq_state);
371 dev_dbg(dev, "state = %x : speed : %d\n",
372 usbhs_status_get_device_state(irq_state),
373 gpriv->gadget.speed);
378 static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
379 struct usbhs_irq_state *irq_state)
381 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
382 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
383 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
384 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
385 struct usb_ctrlrequest ctrl;
386 struct usbhsg_recip_handle *recip_handler = NULL;
387 int stage = usbhs_status_get_ctrl_stage(irq_state);
390 dev_dbg(dev, "stage = %d\n", stage);
396 * - "Interrupt Function"
397 * - "Control Transfer Stage Transition Interrupt"
398 * - Fig. "Control Transfer Stage Transitions"
402 case READ_DATA_STAGE:
403 dcp->handler = &usbhs_fifo_push_handler;
405 case WRITE_DATA_STAGE:
406 dcp->handler = &usbhs_fifo_pop_handler;
408 case NODATA_STATUS_STAGE:
409 dcp->handler = &usbhs_ctrl_stage_end_handler;
418 usbhs_usbreq_get_val(priv, &ctrl);
420 switch (ctrl.bRequestType & USB_TYPE_MASK) {
421 case USB_TYPE_STANDARD:
422 switch (ctrl.bRequest) {
423 case USB_REQ_CLEAR_FEATURE:
424 recip_handler = &req_clear_feature;
430 * setup stage / run recip
433 ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
435 ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
438 usbhs_pipe_stall(pipe);
448 static int usbhsg_dcp_enable(struct usbhsg_uep *uep)
450 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
451 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
452 struct usbhs_pipe *pipe;
455 ********* assume under spin lock *********
458 pipe = usbhs_dcp_malloc(priv);
463 uep->pipe->mod_private = uep;
468 #define usbhsg_dcp_disable usbhsg_pipe_disable
469 static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
471 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
472 struct usbhs_pkt *pkt;
475 ********* assume under spin lock *********
478 usbhs_pipe_disable(pipe);
481 pkt = usbhs_pkt_get(pipe);
491 static void usbhsg_uep_init(struct usbhsg_gpriv *gpriv)
494 struct usbhsg_uep *uep;
496 usbhsg_for_each_uep_with_dcp(uep, gpriv, i)
505 static int usbhsg_ep_enable(struct usb_ep *ep,
506 const struct usb_endpoint_descriptor *desc)
508 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
509 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
510 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
511 struct usbhs_pipe *pipe;
517 * if it already have pipe,
523 /******************** spin lock ********************/
524 lock = usbhsg_trylock(gpriv, &flags);
526 pipe = usbhs_pipe_malloc(priv, desc);
529 pipe->mod_private = uep;
531 if (usb_endpoint_dir_in(desc))
532 uep->handler = &usbhs_fifo_push_handler;
534 uep->handler = &usbhs_fifo_pop_handler;
539 usbhsg_unlock(lock, &flags);
540 /******************** spin unlock ******************/
545 static int usbhsg_ep_disable(struct usb_ep *ep)
547 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
548 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
553 /******************** spin lock ********************/
554 lock = usbhsg_trylock(gpriv, &flags);
556 ret = usbhsg_pipe_disable(uep);
558 usbhsg_unlock(lock, &flags);
559 /******************** spin unlock ******************/
564 static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
567 struct usbhsg_request *ureq;
569 ureq = kzalloc(sizeof *ureq, gfp_flags);
573 usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
578 static void usbhsg_ep_free_request(struct usb_ep *ep,
579 struct usb_request *req)
581 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
583 WARN_ON(!list_empty(&ureq->pkt.node));
587 static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
590 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
591 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
592 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
593 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
599 * CAUTION [*endpoint queue*]
601 * This function will be called from usb_request :: complete
602 * or usb driver timing.
603 * If this function is called from usb_request :: complete,
604 * it is already under spinlock on this driver.
605 * but it is called frm usb driver, this function should call spinlock.
607 * This function is using usbshg_trylock to solve this issue.
608 * if "is_locked" is 1, this mean this function lock it.
609 * but if it is 0, this mean it is already under spin lock.
611 * CAUTION [*queue handler*]
612 * CAUTION [*request complete*]
615 /******************** spin lock ********************/
616 lock = usbhsg_trylock(gpriv, &flags);
619 if (usbhsg_is_not_connected(gpriv) ||
620 unlikely(!gpriv->driver) ||
624 usbhsg_queue_push(uep, ureq);
626 usbhsg_unlock(lock, &flags);
627 /******************** spin unlock ******************/
629 usbhsg_queue_start(uep);
634 static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
636 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
637 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
638 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
644 * CAUTION [*queue handler*]
645 * CAUTION [*endpoint queue*]
646 * CAUTION [*request complete*]
649 /******************** spin lock ********************/
650 lock = usbhsg_trylock(gpriv, &flags);
652 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
654 usbhsg_unlock(lock, &flags);
655 /******************** spin unlock ******************/
660 static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
662 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
663 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
664 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
665 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
672 * CAUTION [*queue handler*]
673 * CAUTION [*endpoint queue*]
674 * CAUTION [*request complete*]
677 /******************** spin lock ********************/
678 lock = usbhsg_trylock(gpriv, &flags);
679 if (!usbhs_pkt_get(pipe)) {
681 dev_dbg(dev, "set halt %d (pipe %d)\n",
682 halt, usbhs_pipe_number(pipe));
685 usbhs_pipe_stall(pipe);
687 usbhs_pipe_disable(pipe);
690 usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
692 usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
697 usbhsg_unlock(lock, &flags);
698 /******************** spin unlock ******************/
703 static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
705 return __usbhsg_ep_set_halt_wedge(ep, value, 0);
708 static int usbhsg_ep_set_wedge(struct usb_ep *ep)
710 return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
713 static struct usb_ep_ops usbhsg_ep_ops = {
714 .enable = usbhsg_ep_enable,
715 .disable = usbhsg_ep_disable,
717 .alloc_request = usbhsg_ep_alloc_request,
718 .free_request = usbhsg_ep_free_request,
720 .queue = usbhsg_ep_queue,
721 .dequeue = usbhsg_ep_dequeue,
723 .set_halt = usbhsg_ep_set_halt,
724 .set_wedge = usbhsg_ep_set_wedge,
728 * usb module start/end
730 static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
732 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
733 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
734 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
735 struct device *dev = usbhs_priv_to_dev(priv);
739 /******************** spin lock ********************/
740 lock = usbhsg_trylock(gpriv, &flags);
743 * enable interrupt and systems if ready
745 usbhsg_status_set(gpriv, status);
746 if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
747 usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
748 goto usbhsg_try_start_unlock;
750 dev_dbg(dev, "start gadget\n");
753 * pipe initialize and enable DCP
755 usbhs_pipe_init(priv,
757 usbhs_fifo_init(priv);
758 usbhsg_uep_init(gpriv);
759 usbhsg_dcp_enable(dcp);
762 * system config enble
767 usbhs_sys_hispeed_ctrl(priv, 1);
768 usbhs_sys_function_ctrl(priv, 1);
769 usbhs_sys_usb_ctrl(priv, 1);
772 * enable irq callback
774 mod->irq_dev_state = usbhsg_irq_dev_state;
775 mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
776 usbhs_irq_callback_update(priv, mod);
778 usbhsg_try_start_unlock:
779 usbhsg_unlock(lock, &flags);
780 /******************** spin unlock ********************/
785 static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
787 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
788 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
789 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
790 struct device *dev = usbhs_priv_to_dev(priv);
794 /******************** spin lock ********************/
795 lock = usbhsg_trylock(gpriv, &flags);
798 * disable interrupt and systems if 1st try
800 usbhsg_status_clr(gpriv, status);
801 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
802 !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
803 goto usbhsg_try_stop_unlock;
805 usbhs_fifo_quit(priv);
807 /* disable all irq */
808 mod->irq_dev_state = NULL;
809 mod->irq_ctrl_stage = NULL;
810 usbhs_irq_callback_update(priv, mod);
812 usbhsg_dcp_disable(dcp);
814 gpriv->gadget.speed = USB_SPEED_UNKNOWN;
817 usbhs_sys_hispeed_ctrl(priv, 0);
818 usbhs_sys_function_ctrl(priv, 0);
819 usbhs_sys_usb_ctrl(priv, 0);
821 usbhsg_unlock(lock, &flags);
822 /******************** spin unlock ********************/
825 gpriv->driver->disconnect)
826 gpriv->driver->disconnect(&gpriv->gadget);
828 dev_dbg(dev, "stop gadget\n");
832 usbhsg_try_stop_unlock:
833 usbhsg_unlock(lock, &flags);
843 struct usbhsg_gpriv *the_controller;
844 int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
845 int (*bind)(struct usb_gadget *))
847 struct usbhsg_gpriv *gpriv = the_controller;
848 struct usbhs_priv *priv;
855 driver->speed != USB_SPEED_HIGH)
862 dev = usbhsg_gpriv_to_dev(gpriv);
863 priv = usbhsg_gpriv_to_priv(gpriv);
865 /* first hook up the driver ... */
866 gpriv->driver = driver;
867 gpriv->gadget.dev.driver = &driver->driver;
869 ret = device_add(&gpriv->gadget.dev);
871 dev_err(dev, "device_add error %d\n", ret);
875 ret = bind(&gpriv->gadget);
877 dev_err(dev, "bind to driver %s error %d\n",
878 driver->driver.name, ret);
882 dev_dbg(dev, "bind %s\n", driver->driver.name);
884 return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
887 device_del(&gpriv->gadget.dev);
889 gpriv->driver = NULL;
890 gpriv->gadget.dev.driver = NULL;
894 EXPORT_SYMBOL(usb_gadget_probe_driver);
896 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
898 struct usbhsg_gpriv *gpriv = the_controller;
899 struct usbhs_priv *priv;
900 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
907 driver != gpriv->driver)
910 dev = usbhsg_gpriv_to_dev(gpriv);
911 priv = usbhsg_gpriv_to_priv(gpriv);
913 usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
914 device_del(&gpriv->gadget.dev);
915 gpriv->driver = NULL;
917 if (driver->disconnect)
918 driver->disconnect(&gpriv->gadget);
920 driver->unbind(&gpriv->gadget);
921 dev_dbg(dev, "unbind %s\n", driver->driver.name);
925 EXPORT_SYMBOL(usb_gadget_unregister_driver);
930 static int usbhsg_get_frame(struct usb_gadget *gadget)
932 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
933 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
935 return usbhs_frame_get_num(priv);
938 static struct usb_gadget_ops usbhsg_gadget_ops = {
939 .get_frame = usbhsg_get_frame,
942 static int usbhsg_start(struct usbhs_priv *priv)
944 return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
947 static int usbhsg_stop(struct usbhs_priv *priv)
949 return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
952 int __devinit usbhs_mod_gadget_probe(struct usbhs_priv *priv)
954 struct usbhsg_gpriv *gpriv;
955 struct usbhsg_uep *uep;
956 struct device *dev = usbhs_priv_to_dev(priv);
957 int pipe_size = usbhs_get_dparam(priv, pipe_size);
960 gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
962 dev_err(dev, "Could not allocate gadget priv\n");
966 uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
968 dev_err(dev, "Could not allocate ep\n");
969 goto usbhs_mod_gadget_probe_err_gpriv;
975 * There is no guarantee that it is possible to access usb module here.
976 * Don't accesses to it.
977 * The accesse will be enable after "usbhsg_start"
983 usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
986 gpriv->mod.name = "gadget";
987 gpriv->mod.start = usbhsg_start;
988 gpriv->mod.stop = usbhsg_stop;
990 gpriv->uep_size = pipe_size;
991 usbhsg_status_init(gpriv);
996 device_initialize(&gpriv->gadget.dev);
997 dev_set_name(&gpriv->gadget.dev, "gadget");
998 gpriv->gadget.dev.parent = dev;
999 gpriv->gadget.name = "renesas_usbhs_udc";
1000 gpriv->gadget.ops = &usbhsg_gadget_ops;
1001 gpriv->gadget.is_dualspeed = 1;
1003 INIT_LIST_HEAD(&gpriv->gadget.ep_list);
1008 usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
1010 snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
1012 uep->ep.name = uep->ep_name;
1013 uep->ep.ops = &usbhsg_ep_ops;
1014 INIT_LIST_HEAD(&uep->ep.ep_list);
1017 if (usbhsg_is_dcp(uep)) {
1018 gpriv->gadget.ep0 = &uep->ep;
1019 uep->ep.maxpacket = 64;
1021 /* init normal pipe */
1023 uep->ep.maxpacket = 512;
1024 list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
1028 the_controller = gpriv;
1030 dev_info(dev, "gadget probed\n");
1034 usbhs_mod_gadget_probe_err_gpriv:
1040 void __devexit usbhs_mod_gadget_remove(struct usbhs_priv *priv)
1042 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);