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 struct usbhsg_request *usbhsg_queue_get(struct usbhsg_uep *uep)
173 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
174 struct usbhs_pkt *pkt = usbhs_pkt_get(pipe);
177 ********* assume under spin lock *********
182 return usbhsg_pkt_to_ureq(pkt);
185 static int usbhsg_queue_start(struct usbhsg_uep *uep)
187 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
188 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
189 struct usbhs_pkt *pkt;
195 * CAUTION [*queue handler*]
197 * This function will be called for start/restart queue operation.
198 * OTOH the most much worry for USB driver is spinlock nest.
200 * - usb_ep_ops :: queue
201 * - usb_request :: complete
203 * But the caller of this function need not care about spinlock.
204 * This function is using usbhsg_trylock for it.
205 * if "is_locked" is 1, this mean this function lock it.
206 * but if it is 0, this mean it is already under spin lock.
208 * CAUTION [*endpoint queue*]
209 * CAUTION [*request complete*]
212 /****************** spin try lock *******************/
213 lock = usbhsg_trylock(gpriv, &flags);
215 pkt = usbhs_pkt_get(pipe);
217 ret = usbhs_pkt_start(pkt);
219 usbhsg_unlock(lock, &flags);
220 /******************** spin unlock ******************/
225 static void usbhsg_queue_pop(struct usbhsg_uep *uep,
226 struct usbhsg_request *ureq,
229 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
230 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
231 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
232 struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
235 ********* assume under spin lock *********
239 * CAUTION [*request complete*]
241 * There is a possibility not to be called in correct order
242 * if "complete" is called without spinlock.
244 * So, this function assume it is under spinlock,
245 * and call usb_request :: complete.
247 * But this "complete" will push next usb_request.
248 * It mean "usb_ep_ops :: queue" which is using spinlock is called
251 * To avoid dead-lock, this driver is using usbhsg_trylock.
252 * CAUTION [*endpoint queue*]
253 * CAUTION [*queue handler*]
256 dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
260 ureq->req.status = status;
261 ureq->req.complete(&uep->ep, &ureq->req);
265 usbhsg_queue_start(uep);
268 static void usbhsg_queue_done(struct usbhs_pkt *pkt)
270 struct usbhs_pipe *pipe = pkt->pipe;
271 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
272 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
274 ureq->req.actual = pkt->actual;
276 usbhsg_queue_pop(uep, ureq, 0);
280 * USB_TYPE_STANDARD / clear feature functions
282 static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
283 struct usbhsg_uep *uep,
284 struct usb_ctrlrequest *ctrl)
286 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
287 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
288 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
290 usbhs_dcp_control_transfer_done(pipe);
295 static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
296 struct usbhsg_uep *uep,
297 struct usb_ctrlrequest *ctrl)
299 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
300 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
302 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
303 usbhs_pipe_disable(pipe);
304 usbhs_pipe_clear_sequence(pipe);
305 usbhs_pipe_enable(pipe);
308 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
310 usbhsg_queue_start(uep);
315 struct usbhsg_recip_handle req_clear_feature = {
316 .name = "clear feature",
317 .device = usbhsg_recip_handler_std_control_done,
318 .interface = usbhsg_recip_handler_std_control_done,
319 .endpoint = usbhsg_recip_handler_std_clear_endpoint,
325 static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
326 struct usbhsg_recip_handle *handler,
327 struct usb_ctrlrequest *ctrl)
329 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
330 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
331 struct usbhsg_uep *uep;
332 int recip = ctrl->bRequestType & USB_RECIP_MASK;
333 int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
335 int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
336 struct usb_ctrlrequest *ctrl);
339 uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
340 if (!usbhsg_uep_to_pipe(uep)) {
341 dev_err(dev, "wrong recip request\n");
346 case USB_RECIP_DEVICE:
348 func = handler->device;
350 case USB_RECIP_INTERFACE:
352 func = handler->interface;
354 case USB_RECIP_ENDPOINT:
356 func = handler->endpoint;
359 dev_warn(dev, "unsupported RECIP(%d)\n", recip);
365 dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
366 ret = func(priv, uep, ctrl);
375 * it will be called from usbhs_interrupt
377 static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
378 struct usbhs_irq_state *irq_state)
380 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
381 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
383 gpriv->gadget.speed = usbhs_status_get_usb_speed(irq_state);
385 dev_dbg(dev, "state = %x : speed : %d\n",
386 usbhs_status_get_device_state(irq_state),
387 gpriv->gadget.speed);
392 static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
393 struct usbhs_irq_state *irq_state)
395 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
396 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
397 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
398 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
399 struct usb_ctrlrequest ctrl;
400 struct usbhsg_recip_handle *recip_handler = NULL;
401 int stage = usbhs_status_get_ctrl_stage(irq_state);
404 dev_dbg(dev, "stage = %d\n", stage);
410 * - "Interrupt Function"
411 * - "Control Transfer Stage Transition Interrupt"
412 * - Fig. "Control Transfer Stage Transitions"
416 case READ_DATA_STAGE:
417 dcp->handler = &usbhs_fifo_push_handler;
419 case WRITE_DATA_STAGE:
420 dcp->handler = &usbhs_fifo_pop_handler;
422 case NODATA_STATUS_STAGE:
423 dcp->handler = &usbhs_ctrl_stage_end_handler;
432 usbhs_usbreq_get_val(priv, &ctrl);
434 switch (ctrl.bRequestType & USB_TYPE_MASK) {
435 case USB_TYPE_STANDARD:
436 switch (ctrl.bRequest) {
437 case USB_REQ_CLEAR_FEATURE:
438 recip_handler = &req_clear_feature;
444 * setup stage / run recip
447 ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
449 ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
452 usbhs_pipe_stall(pipe);
462 static int usbhsg_dcp_enable(struct usbhsg_uep *uep)
464 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
465 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
466 struct usbhs_pipe *pipe;
469 ********* assume under spin lock *********
472 pipe = usbhs_dcp_malloc(priv);
477 uep->pipe->mod_private = uep;
482 #define usbhsg_dcp_disable usbhsg_pipe_disable
483 static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
485 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
486 struct usbhsg_request *ureq;
489 ********* assume under spin lock *********
492 usbhs_pipe_disable(pipe);
495 ureq = usbhsg_queue_get(uep);
499 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
505 static void usbhsg_uep_init(struct usbhsg_gpriv *gpriv)
508 struct usbhsg_uep *uep;
510 usbhsg_for_each_uep_with_dcp(uep, gpriv, i)
519 static int usbhsg_ep_enable(struct usb_ep *ep,
520 const struct usb_endpoint_descriptor *desc)
522 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
523 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
524 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
525 struct usbhs_pipe *pipe;
531 * if it already have pipe,
537 /******************** spin lock ********************/
538 lock = usbhsg_trylock(gpriv, &flags);
540 pipe = usbhs_pipe_malloc(priv, desc);
543 pipe->mod_private = uep;
545 if (usb_endpoint_dir_in(desc))
546 uep->handler = &usbhs_fifo_push_handler;
548 uep->handler = &usbhs_fifo_pop_handler;
553 usbhsg_unlock(lock, &flags);
554 /******************** spin unlock ******************/
559 static int usbhsg_ep_disable(struct usb_ep *ep)
561 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
562 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
567 /******************** spin lock ********************/
568 lock = usbhsg_trylock(gpriv, &flags);
570 ret = usbhsg_pipe_disable(uep);
572 usbhsg_unlock(lock, &flags);
573 /******************** spin unlock ******************/
578 static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
581 struct usbhsg_request *ureq;
583 ureq = kzalloc(sizeof *ureq, gfp_flags);
587 usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
592 static void usbhsg_ep_free_request(struct usb_ep *ep,
593 struct usb_request *req)
595 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
597 WARN_ON(!list_empty(&ureq->pkt.node));
601 static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
604 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
605 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
606 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
607 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
613 * CAUTION [*endpoint queue*]
615 * This function will be called from usb_request :: complete
616 * or usb driver timing.
617 * If this function is called from usb_request :: complete,
618 * it is already under spinlock on this driver.
619 * but it is called frm usb driver, this function should call spinlock.
621 * This function is using usbshg_trylock to solve this issue.
622 * if "is_locked" is 1, this mean this function lock it.
623 * but if it is 0, this mean it is already under spin lock.
625 * CAUTION [*queue handler*]
626 * CAUTION [*request complete*]
629 /******************** spin lock ********************/
630 lock = usbhsg_trylock(gpriv, &flags);
633 if (usbhsg_is_not_connected(gpriv) ||
634 unlikely(!gpriv->driver) ||
638 usbhsg_queue_push(uep, ureq);
640 usbhsg_unlock(lock, &flags);
641 /******************** spin unlock ******************/
643 usbhsg_queue_start(uep);
648 static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
650 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
651 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
652 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
658 * CAUTION [*queue handler*]
659 * CAUTION [*endpoint queue*]
660 * CAUTION [*request complete*]
663 /******************** spin lock ********************/
664 lock = usbhsg_trylock(gpriv, &flags);
666 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
668 usbhsg_unlock(lock, &flags);
669 /******************** spin unlock ******************/
674 static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
676 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
677 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
678 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
679 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
686 * CAUTION [*queue handler*]
687 * CAUTION [*endpoint queue*]
688 * CAUTION [*request complete*]
691 /******************** spin lock ********************/
692 lock = usbhsg_trylock(gpriv, &flags);
693 if (!usbhsg_queue_get(uep)) {
695 dev_dbg(dev, "set halt %d (pipe %d)\n",
696 halt, usbhs_pipe_number(pipe));
699 usbhs_pipe_stall(pipe);
701 usbhs_pipe_disable(pipe);
704 usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
706 usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
711 usbhsg_unlock(lock, &flags);
712 /******************** spin unlock ******************/
717 static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
719 return __usbhsg_ep_set_halt_wedge(ep, value, 0);
722 static int usbhsg_ep_set_wedge(struct usb_ep *ep)
724 return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
727 static struct usb_ep_ops usbhsg_ep_ops = {
728 .enable = usbhsg_ep_enable,
729 .disable = usbhsg_ep_disable,
731 .alloc_request = usbhsg_ep_alloc_request,
732 .free_request = usbhsg_ep_free_request,
734 .queue = usbhsg_ep_queue,
735 .dequeue = usbhsg_ep_dequeue,
737 .set_halt = usbhsg_ep_set_halt,
738 .set_wedge = usbhsg_ep_set_wedge,
742 * usb module start/end
744 static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
746 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
747 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
748 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
749 struct device *dev = usbhs_priv_to_dev(priv);
753 /******************** spin lock ********************/
754 lock = usbhsg_trylock(gpriv, &flags);
757 * enable interrupt and systems if ready
759 usbhsg_status_set(gpriv, status);
760 if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
761 usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
762 goto usbhsg_try_start_unlock;
764 dev_dbg(dev, "start gadget\n");
767 * pipe initialize and enable DCP
769 usbhs_pipe_init(priv,
771 usbhs_fifo_init(priv);
772 usbhsg_uep_init(gpriv);
773 usbhsg_dcp_enable(dcp);
776 * system config enble
781 usbhs_sys_hispeed_ctrl(priv, 1);
782 usbhs_sys_function_ctrl(priv, 1);
783 usbhs_sys_usb_ctrl(priv, 1);
786 * enable irq callback
788 mod->irq_dev_state = usbhsg_irq_dev_state;
789 mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
790 usbhs_irq_callback_update(priv, mod);
792 usbhsg_try_start_unlock:
793 usbhsg_unlock(lock, &flags);
794 /******************** spin unlock ********************/
799 static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
801 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
802 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
803 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
804 struct device *dev = usbhs_priv_to_dev(priv);
808 /******************** spin lock ********************/
809 lock = usbhsg_trylock(gpriv, &flags);
812 * disable interrupt and systems if 1st try
814 usbhsg_status_clr(gpriv, status);
815 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
816 !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
817 goto usbhsg_try_stop_unlock;
819 usbhs_fifo_quit(priv);
821 /* disable all irq */
822 mod->irq_dev_state = NULL;
823 mod->irq_ctrl_stage = NULL;
824 usbhs_irq_callback_update(priv, mod);
826 usbhsg_dcp_disable(dcp);
828 gpriv->gadget.speed = USB_SPEED_UNKNOWN;
831 usbhs_sys_hispeed_ctrl(priv, 0);
832 usbhs_sys_function_ctrl(priv, 0);
833 usbhs_sys_usb_ctrl(priv, 0);
835 usbhsg_unlock(lock, &flags);
836 /******************** spin unlock ********************/
839 gpriv->driver->disconnect)
840 gpriv->driver->disconnect(&gpriv->gadget);
842 dev_dbg(dev, "stop gadget\n");
846 usbhsg_try_stop_unlock:
847 usbhsg_unlock(lock, &flags);
857 struct usbhsg_gpriv *the_controller;
858 int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
859 int (*bind)(struct usb_gadget *))
861 struct usbhsg_gpriv *gpriv = the_controller;
862 struct usbhs_priv *priv;
869 driver->speed != USB_SPEED_HIGH)
876 dev = usbhsg_gpriv_to_dev(gpriv);
877 priv = usbhsg_gpriv_to_priv(gpriv);
879 /* first hook up the driver ... */
880 gpriv->driver = driver;
881 gpriv->gadget.dev.driver = &driver->driver;
883 ret = device_add(&gpriv->gadget.dev);
885 dev_err(dev, "device_add error %d\n", ret);
889 ret = bind(&gpriv->gadget);
891 dev_err(dev, "bind to driver %s error %d\n",
892 driver->driver.name, ret);
896 dev_dbg(dev, "bind %s\n", driver->driver.name);
898 return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
901 device_del(&gpriv->gadget.dev);
903 gpriv->driver = NULL;
904 gpriv->gadget.dev.driver = NULL;
908 EXPORT_SYMBOL(usb_gadget_probe_driver);
910 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
912 struct usbhsg_gpriv *gpriv = the_controller;
913 struct usbhs_priv *priv;
914 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
921 driver != gpriv->driver)
924 dev = usbhsg_gpriv_to_dev(gpriv);
925 priv = usbhsg_gpriv_to_priv(gpriv);
927 usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
928 device_del(&gpriv->gadget.dev);
929 gpriv->driver = NULL;
931 if (driver->disconnect)
932 driver->disconnect(&gpriv->gadget);
934 driver->unbind(&gpriv->gadget);
935 dev_dbg(dev, "unbind %s\n", driver->driver.name);
939 EXPORT_SYMBOL(usb_gadget_unregister_driver);
944 static int usbhsg_get_frame(struct usb_gadget *gadget)
946 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
947 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
949 return usbhs_frame_get_num(priv);
952 static struct usb_gadget_ops usbhsg_gadget_ops = {
953 .get_frame = usbhsg_get_frame,
956 static int usbhsg_start(struct usbhs_priv *priv)
958 return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
961 static int usbhsg_stop(struct usbhs_priv *priv)
963 return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
966 int __devinit usbhs_mod_gadget_probe(struct usbhs_priv *priv)
968 struct usbhsg_gpriv *gpriv;
969 struct usbhsg_uep *uep;
970 struct device *dev = usbhs_priv_to_dev(priv);
971 int pipe_size = usbhs_get_dparam(priv, pipe_size);
974 gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
976 dev_err(dev, "Could not allocate gadget priv\n");
980 uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
982 dev_err(dev, "Could not allocate ep\n");
983 goto usbhs_mod_gadget_probe_err_gpriv;
989 * There is no guarantee that it is possible to access usb module here.
990 * Don't accesses to it.
991 * The accesse will be enable after "usbhsg_start"
997 usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
1000 gpriv->mod.name = "gadget";
1001 gpriv->mod.start = usbhsg_start;
1002 gpriv->mod.stop = usbhsg_stop;
1004 gpriv->uep_size = pipe_size;
1005 usbhsg_status_init(gpriv);
1010 device_initialize(&gpriv->gadget.dev);
1011 dev_set_name(&gpriv->gadget.dev, "gadget");
1012 gpriv->gadget.dev.parent = dev;
1013 gpriv->gadget.name = "renesas_usbhs_udc";
1014 gpriv->gadget.ops = &usbhsg_gadget_ops;
1015 gpriv->gadget.is_dualspeed = 1;
1017 INIT_LIST_HEAD(&gpriv->gadget.ep_list);
1022 usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
1024 snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
1026 uep->ep.name = uep->ep_name;
1027 uep->ep.ops = &usbhsg_ep_ops;
1028 INIT_LIST_HEAD(&uep->ep.ep_list);
1031 if (usbhsg_is_dcp(uep)) {
1032 gpriv->gadget.ep0 = &uep->ep;
1033 uep->ep.maxpacket = 64;
1035 /* init normal pipe */
1037 uep->ep.maxpacket = 512;
1038 list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
1042 the_controller = gpriv;
1044 dev_info(dev, "gadget probed\n");
1048 usbhs_mod_gadget_probe_err_gpriv:
1054 void __devexit usbhs_mod_gadget_remove(struct usbhs_priv *priv)
1056 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);