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_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)
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)
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
;
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
),
138 static void usbhsg_queue_pop(struct usbhsg_uep
*uep
,
139 struct usbhsg_request
*ureq
,
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");
175 if (req
->dma
== DMA_ADDR_INVALID
) {
176 pkt
->dma
= dma_map_single(dev
, pkt
->buf
, pkt
->length
, dir
);
178 dma_sync_single_for_device(dev
, req
->dma
, req
->length
, dir
);
182 if (dma_mapping_error(dev
, pkt
->dma
)) {
183 dev_err(dev
, "dma mapping error %x\n", pkt
->dma
);
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");
202 if (req
->dma
== DMA_ADDR_INVALID
)
203 dma_unmap_single(dev
, pkt
->dma
, pkt
->length
, dir
);
205 dma_sync_single_for_cpu(dev
, req
->dma
, req
->length
, dir
);
207 pkt
->dma
= DMA_ADDR_INVALID
;
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
;
223 return usbhsg_dma_map(dev
, pkt
, dir
);
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
);
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
);
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
,
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
;
283 int (*func
)(struct usbhs_priv
*priv
, struct usbhsg_uep
*uep
,
284 struct usb_ctrlrequest
*ctrl
);
287 uep
= usbhsg_gpriv_to_nth_uep(gpriv
, nth
);
288 pipe
= usbhsg_uep_to_pipe(uep
);
290 dev_err(dev
, "wrong recip request\n");
292 goto usbhsg_recip_run_handle_end
;
296 case USB_RECIP_DEVICE
:
298 func
= handler
->device
;
300 case USB_RECIP_INTERFACE
:
302 func
= handler
->interface
;
304 case USB_RECIP_ENDPOINT
:
306 func
= handler
->endpoint
;
309 dev_warn(dev
, "unsupported RECIP(%d)\n", recip
);
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
);
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
);
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
);
364 dev_dbg(dev
, "stage = %d\n", stage
);
370 * - "Interrupt Function"
371 * - "Control Transfer Stage Transition Interrupt"
372 * - Fig. "Control Transfer Stage Transitions"
376 case READ_DATA_STAGE
:
377 dcp
->handler
= &usbhs_fifo_pio_push_handler
;
379 case WRITE_DATA_STAGE
:
380 dcp
->handler
= &usbhs_fifo_pio_pop_handler
;
382 case NODATA_STATUS_STAGE
:
383 dcp
->handler
= &usbhs_ctrl_stage_end_handler
;
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
;
404 * setup stage / run recip
407 ret
= usbhsg_recip_run_handle(priv
, recip_handler
, &ctrl
);
409 ret
= gpriv
->driver
->setup(&gpriv
->gadget
, &ctrl
);
412 usbhs_pipe_stall(pipe
);
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
);
430 pkt
= usbhs_pkt_pop(pipe
, NULL
);
438 static void usbhsg_uep_init(struct usbhsg_gpriv
*gpriv
)
441 struct usbhsg_uep
*uep
;
443 usbhsg_for_each_uep_with_dcp(uep
, gpriv
, i
)
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
;
462 * if it already have pipe,
468 pipe
= usbhs_pipe_malloc(priv
, desc
);
471 pipe
->mod_private
= uep
;
473 if (usb_endpoint_dir_in(desc
))
474 uep
->handler
= &usbhs_fifo_pio_push_handler
;
476 uep
->handler
= &usbhs_fifo_pio_pop_handler
;
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
,
494 struct usbhsg_request
*ureq
;
496 ureq
= kzalloc(sizeof *ureq
, gfp_flags
);
500 usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq
));
502 ureq
->req
.dma
= DMA_ADDR_INVALID
;
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
));
516 static int usbhsg_ep_queue(struct usb_ep
*ep
, struct usb_request
*req
,
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
);
525 if (usbhsg_is_not_connected(gpriv
) ||
526 unlikely(!gpriv
->driver
) ||
530 usbhsg_queue_push(uep
, ureq
);
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
);
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
);
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
);
565 usbhs_pipe_stall(pipe
);
567 usbhs_pipe_disable(pipe
);
570 usbhsg_status_set(gpriv
, USBHSG_STATUS_WEDGE
);
572 usbhsg_status_clr(gpriv
, USBHSG_STATUS_WEDGE
);
574 usbhs_unlock(priv
, flags
);
575 /******************** spin unlock ******************/
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
);
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 ********************/
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
,
640 usbhsg_dma_map_ctrl
);
641 usbhs_fifo_init(priv
);
642 usbhsg_uep_init(gpriv
);
645 dcp
->pipe
= usbhs_dcp_malloc(priv
);
646 dcp
->pipe
->mod_private
= dcp
;
649 * system config enble
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
);
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
);
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 ********************/
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
;
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
);
711 gpriv
->driver
->disconnect
)
712 gpriv
->driver
->disconnect(&gpriv
->gadget
);
714 dev_dbg(dev
, "stop gadget\n");
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
;
736 driver
->speed
!= USB_SPEED_HIGH
)
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
);
752 dev_err(dev
, "device_add error %d\n", ret
);
756 ret
= bind(&gpriv
->gadget
);
758 dev_err(dev
, "bind to driver %s error %d\n",
759 driver
->driver
.name
, ret
);
763 dev_dbg(dev
, "bind %s\n", driver
->driver
.name
);
765 return usbhsg_try_start(priv
, USBHSG_STATUS_REGISTERD
);
768 device_del(&gpriv
->gadget
.dev
);
770 gpriv
->driver
= NULL
;
771 gpriv
->gadget
.dev
.driver
= NULL
;
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
);
788 driver
!= gpriv
->driver
)
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
);
806 EXPORT_SYMBOL(usb_gadget_unregister_driver
);
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
);
841 gpriv
= kzalloc(sizeof(struct usbhsg_gpriv
), GFP_KERNEL
);
843 dev_err(dev
, "Could not allocate gadget priv\n");
847 uep
= kzalloc(sizeof(struct usbhsg_uep
) * pipe_size
, GFP_KERNEL
);
849 dev_err(dev
, "Could not allocate ep\n");
850 goto usbhs_mod_gadget_probe_err_gpriv
;
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"
864 usbhs_mod_register(priv
, &gpriv
->mod
, USBHS_GADGET
);
867 gpriv
->mod
.name
= "gadget";
868 gpriv
->mod
.start
= usbhsg_start
;
869 gpriv
->mod
.stop
= usbhsg_stop
;
871 gpriv
->uep_size
= pipe_size
;
872 usbhsg_status_init(gpriv
);
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
);
889 usbhsg_for_each_uep_with_dcp(uep
, gpriv
, i
) {
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
);
898 if (usbhsg_is_dcp(uep
)) {
899 gpriv
->gadget
.ep0
= &uep
->ep
;
900 uep
->ep
.maxpacket
= 64;
902 /* init normal pipe */
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");
915 usbhs_mod_gadget_probe_err_gpriv
:
921 void __devexit
usbhs_mod_gadget_remove(struct usbhs_priv
*priv
)
923 struct usbhsg_gpriv
*gpriv
= usbhsg_priv_to_gpriv(priv
);