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>
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/usb/ch9.h>
23 #include <linux/usb/gadget.h>
29 struct usbhsg_request
{
30 struct usb_request req
;
34 #define EP_NAME_SIZE 8
38 struct usbhs_pipe
*pipe
;
40 char ep_name
[EP_NAME_SIZE
];
42 struct usbhsg_gpriv
*gpriv
;
46 struct usb_gadget gadget
;
49 struct usbhsg_uep
*uep
;
52 struct usb_gadget_driver
*driver
;
55 #define USBHSG_STATUS_STARTED (1 << 0)
56 #define USBHSG_STATUS_REGISTERD (1 << 1)
57 #define USBHSG_STATUS_WEDGE (1 << 2)
58 #define USBHSG_STATUS_SELF_POWERED (1 << 3)
59 #define USBHSG_STATUS_SOFT_CONNECT (1 << 4)
62 struct usbhsg_recip_handle
{
64 int (*device
)(struct usbhs_priv
*priv
, struct usbhsg_uep
*uep
,
65 struct usb_ctrlrequest
*ctrl
);
66 int (*interface
)(struct usbhs_priv
*priv
, struct usbhsg_uep
*uep
,
67 struct usb_ctrlrequest
*ctrl
);
68 int (*endpoint
)(struct usbhs_priv
*priv
, struct usbhsg_uep
*uep
,
69 struct usb_ctrlrequest
*ctrl
);
75 #define usbhsg_priv_to_gpriv(priv) \
77 usbhs_mod_get(priv, USBHS_GADGET), \
78 struct usbhsg_gpriv, mod)
80 #define __usbhsg_for_each_uep(start, pos, g, i) \
82 ((i) < (g)->uep_size) && ((pos) = (g)->uep + (i)); \
85 #define usbhsg_for_each_uep(pos, gpriv, i) \
86 __usbhsg_for_each_uep(1, pos, gpriv, i)
88 #define usbhsg_for_each_uep_with_dcp(pos, gpriv, i) \
89 __usbhsg_for_each_uep(0, pos, gpriv, i)
91 #define usbhsg_gadget_to_gpriv(g)\
92 container_of(g, struct usbhsg_gpriv, gadget)
94 #define usbhsg_req_to_ureq(r)\
95 container_of(r, struct usbhsg_request, req)
97 #define usbhsg_ep_to_uep(e) container_of(e, struct usbhsg_uep, ep)
98 #define usbhsg_gpriv_to_dev(gp) usbhs_priv_to_dev((gp)->mod.priv)
99 #define usbhsg_gpriv_to_priv(gp) ((gp)->mod.priv)
100 #define usbhsg_gpriv_to_dcp(gp) ((gp)->uep)
101 #define usbhsg_gpriv_to_nth_uep(gp, i) ((gp)->uep + i)
102 #define usbhsg_uep_to_gpriv(u) ((u)->gpriv)
103 #define usbhsg_uep_to_pipe(u) ((u)->pipe)
104 #define usbhsg_pipe_to_uep(p) ((p)->mod_private)
105 #define usbhsg_is_dcp(u) ((u) == usbhsg_gpriv_to_dcp((u)->gpriv))
107 #define usbhsg_ureq_to_pkt(u) (&(u)->pkt)
108 #define usbhsg_pkt_to_ureq(i) \
109 container_of(i, struct usbhsg_request, pkt)
111 #define usbhsg_is_not_connected(gp) ((gp)->gadget.speed == USB_SPEED_UNKNOWN)
114 #define usbhsg_status_init(gp) do {(gp)->status = 0; } while (0)
115 #define usbhsg_status_set(gp, b) (gp->status |= b)
116 #define usbhsg_status_clr(gp, b) (gp->status &= ~b)
117 #define usbhsg_status_has(gp, b) (gp->status & b)
122 static void __usbhsg_queue_pop(struct usbhsg_uep
*uep
,
123 struct usbhsg_request
*ureq
,
126 struct usbhsg_gpriv
*gpriv
= usbhsg_uep_to_gpriv(uep
);
127 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(uep
);
128 struct device
*dev
= usbhsg_gpriv_to_dev(gpriv
);
129 struct usbhs_priv
*priv
= usbhsg_gpriv_to_priv(gpriv
);
131 dev_dbg(dev
, "pipe %d : queue pop\n", usbhs_pipe_number(pipe
));
133 ureq
->req
.status
= status
;
134 spin_unlock(usbhs_priv_to_lock(priv
));
135 usb_gadget_giveback_request(&uep
->ep
, &ureq
->req
);
136 spin_lock(usbhs_priv_to_lock(priv
));
139 static void usbhsg_queue_pop(struct usbhsg_uep
*uep
,
140 struct usbhsg_request
*ureq
,
143 struct usbhsg_gpriv
*gpriv
= usbhsg_uep_to_gpriv(uep
);
144 struct usbhs_priv
*priv
= usbhsg_gpriv_to_priv(gpriv
);
147 usbhs_lock(priv
, flags
);
148 __usbhsg_queue_pop(uep
, ureq
, status
);
149 usbhs_unlock(priv
, flags
);
152 static void usbhsg_queue_done(struct usbhs_priv
*priv
, 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 void usbhsg_queue_push(struct usbhsg_uep
*uep
,
164 struct usbhsg_request
*ureq
)
166 struct usbhsg_gpriv
*gpriv
= usbhsg_uep_to_gpriv(uep
);
167 struct device
*dev
= usbhsg_gpriv_to_dev(gpriv
);
168 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(uep
);
169 struct usbhs_pkt
*pkt
= usbhsg_ureq_to_pkt(ureq
);
170 struct usb_request
*req
= &ureq
->req
;
173 req
->status
= -EINPROGRESS
;
174 usbhs_pkt_push(pipe
, pkt
, usbhsg_queue_done
,
175 req
->buf
, req
->length
, req
->zero
, -1);
176 usbhs_pkt_start(pipe
);
178 dev_dbg(dev
, "pipe %d : queue push (%d)\n",
179 usbhs_pipe_number(pipe
),
186 static int usbhsg_dma_map_ctrl(struct usbhs_pkt
*pkt
, int map
)
188 struct usbhsg_request
*ureq
= usbhsg_pkt_to_ureq(pkt
);
189 struct usb_request
*req
= &ureq
->req
;
190 struct usbhs_pipe
*pipe
= pkt
->pipe
;
191 struct usbhsg_uep
*uep
= usbhsg_pipe_to_uep(pipe
);
192 struct usbhsg_gpriv
*gpriv
= usbhsg_uep_to_gpriv(uep
);
193 enum dma_data_direction dir
;
196 dir
= usbhs_pipe_is_dir_host(pipe
);
199 /* it can not use scatter/gather */
200 WARN_ON(req
->num_sgs
);
202 ret
= usb_gadget_map_request(&gpriv
->gadget
, req
, dir
);
208 usb_gadget_unmap_request(&gpriv
->gadget
, req
, dir
);
215 * USB_TYPE_STANDARD / clear feature functions
217 static int usbhsg_recip_handler_std_control_done(struct usbhs_priv
*priv
,
218 struct usbhsg_uep
*uep
,
219 struct usb_ctrlrequest
*ctrl
)
221 struct usbhsg_gpriv
*gpriv
= usbhsg_priv_to_gpriv(priv
);
222 struct usbhsg_uep
*dcp
= usbhsg_gpriv_to_dcp(gpriv
);
223 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(dcp
);
225 usbhs_dcp_control_transfer_done(pipe
);
230 static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv
*priv
,
231 struct usbhsg_uep
*uep
,
232 struct usb_ctrlrequest
*ctrl
)
234 struct usbhsg_gpriv
*gpriv
= usbhsg_uep_to_gpriv(uep
);
235 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(uep
);
237 if (!usbhsg_status_has(gpriv
, USBHSG_STATUS_WEDGE
)) {
238 usbhs_pipe_disable(pipe
);
239 usbhs_pipe_sequence_data0(pipe
);
240 usbhs_pipe_enable(pipe
);
243 usbhsg_recip_handler_std_control_done(priv
, uep
, ctrl
);
245 usbhs_pkt_start(pipe
);
250 static struct usbhsg_recip_handle req_clear_feature
= {
251 .name
= "clear feature",
252 .device
= usbhsg_recip_handler_std_control_done
,
253 .interface
= usbhsg_recip_handler_std_control_done
,
254 .endpoint
= usbhsg_recip_handler_std_clear_endpoint
,
258 * USB_TYPE_STANDARD / set feature functions
260 static int usbhsg_recip_handler_std_set_device(struct usbhs_priv
*priv
,
261 struct usbhsg_uep
*uep
,
262 struct usb_ctrlrequest
*ctrl
)
264 switch (le16_to_cpu(ctrl
->wValue
)) {
265 case USB_DEVICE_TEST_MODE
:
266 usbhsg_recip_handler_std_control_done(priv
, uep
, ctrl
);
268 usbhs_sys_set_test_mode(priv
, le16_to_cpu(ctrl
->wIndex
>> 8));
271 usbhsg_recip_handler_std_control_done(priv
, uep
, ctrl
);
278 static int usbhsg_recip_handler_std_set_endpoint(struct usbhs_priv
*priv
,
279 struct usbhsg_uep
*uep
,
280 struct usb_ctrlrequest
*ctrl
)
282 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(uep
);
284 usbhs_pipe_stall(pipe
);
286 usbhsg_recip_handler_std_control_done(priv
, uep
, ctrl
);
291 static struct usbhsg_recip_handle req_set_feature
= {
292 .name
= "set feature",
293 .device
= usbhsg_recip_handler_std_set_device
,
294 .interface
= usbhsg_recip_handler_std_control_done
,
295 .endpoint
= usbhsg_recip_handler_std_set_endpoint
,
299 * USB_TYPE_STANDARD / get status functions
301 static void __usbhsg_recip_send_complete(struct usb_ep
*ep
,
302 struct usb_request
*req
)
304 struct usbhsg_request
*ureq
= usbhsg_req_to_ureq(req
);
306 /* free allocated recip-buffer/usb_request */
307 kfree(ureq
->pkt
.buf
);
308 usb_ep_free_request(ep
, req
);
311 static void __usbhsg_recip_send_status(struct usbhsg_gpriv
*gpriv
,
312 unsigned short status
)
314 struct usbhsg_uep
*dcp
= usbhsg_gpriv_to_dcp(gpriv
);
315 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(dcp
);
316 struct device
*dev
= usbhsg_gpriv_to_dev(gpriv
);
317 struct usb_request
*req
;
320 /* alloc new usb_request for recip */
321 req
= usb_ep_alloc_request(&dcp
->ep
, GFP_ATOMIC
);
323 dev_err(dev
, "recip request allocation fail\n");
327 /* alloc recip data buffer */
328 buf
= kmalloc(sizeof(*buf
), GFP_ATOMIC
);
330 usb_ep_free_request(&dcp
->ep
, req
);
331 dev_err(dev
, "recip data allocation fail\n");
335 /* recip data is status */
336 *buf
= cpu_to_le16(status
);
338 /* allocated usb_request/buffer will be freed */
339 req
->complete
= __usbhsg_recip_send_complete
;
341 req
->length
= sizeof(*buf
);
345 pipe
->handler
= &usbhs_fifo_pio_push_handler
;
346 usbhsg_queue_push(dcp
, usbhsg_req_to_ureq(req
));
349 static int usbhsg_recip_handler_std_get_device(struct usbhs_priv
*priv
,
350 struct usbhsg_uep
*uep
,
351 struct usb_ctrlrequest
*ctrl
)
353 struct usbhsg_gpriv
*gpriv
= usbhsg_uep_to_gpriv(uep
);
354 unsigned short status
= 0;
356 if (usbhsg_status_has(gpriv
, USBHSG_STATUS_SELF_POWERED
))
357 status
= 1 << USB_DEVICE_SELF_POWERED
;
359 __usbhsg_recip_send_status(gpriv
, status
);
364 static int usbhsg_recip_handler_std_get_interface(struct usbhs_priv
*priv
,
365 struct usbhsg_uep
*uep
,
366 struct usb_ctrlrequest
*ctrl
)
368 struct usbhsg_gpriv
*gpriv
= usbhsg_uep_to_gpriv(uep
);
369 unsigned short status
= 0;
371 __usbhsg_recip_send_status(gpriv
, status
);
376 static int usbhsg_recip_handler_std_get_endpoint(struct usbhs_priv
*priv
,
377 struct usbhsg_uep
*uep
,
378 struct usb_ctrlrequest
*ctrl
)
380 struct usbhsg_gpriv
*gpriv
= usbhsg_uep_to_gpriv(uep
);
381 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(uep
);
382 unsigned short status
= 0;
384 if (usbhs_pipe_is_stall(pipe
))
385 status
= 1 << USB_ENDPOINT_HALT
;
387 __usbhsg_recip_send_status(gpriv
, status
);
392 static struct usbhsg_recip_handle req_get_status
= {
393 .name
= "get status",
394 .device
= usbhsg_recip_handler_std_get_device
,
395 .interface
= usbhsg_recip_handler_std_get_interface
,
396 .endpoint
= usbhsg_recip_handler_std_get_endpoint
,
402 static int usbhsg_recip_run_handle(struct usbhs_priv
*priv
,
403 struct usbhsg_recip_handle
*handler
,
404 struct usb_ctrlrequest
*ctrl
)
406 struct usbhsg_gpriv
*gpriv
= usbhsg_priv_to_gpriv(priv
);
407 struct device
*dev
= usbhsg_gpriv_to_dev(gpriv
);
408 struct usbhsg_uep
*uep
;
409 struct usbhs_pipe
*pipe
;
410 int recip
= ctrl
->bRequestType
& USB_RECIP_MASK
;
411 int nth
= le16_to_cpu(ctrl
->wIndex
) & USB_ENDPOINT_NUMBER_MASK
;
413 int (*func
)(struct usbhs_priv
*priv
, struct usbhsg_uep
*uep
,
414 struct usb_ctrlrequest
*ctrl
);
417 uep
= usbhsg_gpriv_to_nth_uep(gpriv
, nth
);
418 pipe
= usbhsg_uep_to_pipe(uep
);
420 dev_err(dev
, "wrong recip request\n");
425 case USB_RECIP_DEVICE
:
427 func
= handler
->device
;
429 case USB_RECIP_INTERFACE
:
431 func
= handler
->interface
;
433 case USB_RECIP_ENDPOINT
:
435 func
= handler
->endpoint
;
438 dev_warn(dev
, "unsupported RECIP(%d)\n", recip
);
444 dev_dbg(dev
, "%s (pipe %d :%s)\n", handler
->name
, nth
, msg
);
445 ret
= func(priv
, uep
, ctrl
);
454 * it will be called from usbhs_interrupt
456 static int usbhsg_irq_dev_state(struct usbhs_priv
*priv
,
457 struct usbhs_irq_state
*irq_state
)
459 struct usbhsg_gpriv
*gpriv
= usbhsg_priv_to_gpriv(priv
);
460 struct device
*dev
= usbhsg_gpriv_to_dev(gpriv
);
462 gpriv
->gadget
.speed
= usbhs_bus_get_speed(priv
);
464 dev_dbg(dev
, "state = %x : speed : %d\n",
465 usbhs_status_get_device_state(irq_state
),
466 gpriv
->gadget
.speed
);
471 static int usbhsg_irq_ctrl_stage(struct usbhs_priv
*priv
,
472 struct usbhs_irq_state
*irq_state
)
474 struct usbhsg_gpriv
*gpriv
= usbhsg_priv_to_gpriv(priv
);
475 struct usbhsg_uep
*dcp
= usbhsg_gpriv_to_dcp(gpriv
);
476 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(dcp
);
477 struct device
*dev
= usbhsg_gpriv_to_dev(gpriv
);
478 struct usb_ctrlrequest ctrl
;
479 struct usbhsg_recip_handle
*recip_handler
= NULL
;
480 int stage
= usbhs_status_get_ctrl_stage(irq_state
);
483 dev_dbg(dev
, "stage = %d\n", stage
);
489 * - "Interrupt Function"
490 * - "Control Transfer Stage Transition Interrupt"
491 * - Fig. "Control Transfer Stage Transitions"
495 case READ_DATA_STAGE
:
496 pipe
->handler
= &usbhs_fifo_pio_push_handler
;
498 case WRITE_DATA_STAGE
:
499 pipe
->handler
= &usbhs_fifo_pio_pop_handler
;
501 case NODATA_STATUS_STAGE
:
502 pipe
->handler
= &usbhs_ctrl_stage_end_handler
;
504 case READ_STATUS_STAGE
:
505 case WRITE_STATUS_STAGE
:
506 usbhs_dcp_control_transfer_done(pipe
);
514 usbhs_usbreq_get_val(priv
, &ctrl
);
516 switch (ctrl
.bRequestType
& USB_TYPE_MASK
) {
517 case USB_TYPE_STANDARD
:
518 switch (ctrl
.bRequest
) {
519 case USB_REQ_CLEAR_FEATURE
:
520 recip_handler
= &req_clear_feature
;
522 case USB_REQ_SET_FEATURE
:
523 recip_handler
= &req_set_feature
;
525 case USB_REQ_GET_STATUS
:
526 recip_handler
= &req_get_status
;
532 * setup stage / run recip
535 ret
= usbhsg_recip_run_handle(priv
, recip_handler
, &ctrl
);
537 ret
= gpriv
->driver
->setup(&gpriv
->gadget
, &ctrl
);
540 usbhs_pipe_stall(pipe
);
550 static int usbhsg_pipe_disable(struct usbhsg_uep
*uep
)
552 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(uep
);
553 struct usbhs_pkt
*pkt
;
556 pkt
= usbhs_pkt_pop(pipe
, NULL
);
560 usbhsg_queue_pop(uep
, usbhsg_pkt_to_ureq(pkt
), -ECONNRESET
);
563 usbhs_pipe_disable(pipe
);
573 static int usbhsg_ep_enable(struct usb_ep
*ep
,
574 const struct usb_endpoint_descriptor
*desc
)
576 struct usbhsg_uep
*uep
= usbhsg_ep_to_uep(ep
);
577 struct usbhsg_gpriv
*gpriv
= usbhsg_uep_to_gpriv(uep
);
578 struct usbhs_priv
*priv
= usbhsg_gpriv_to_priv(gpriv
);
579 struct usbhs_pipe
*pipe
;
583 * if it already have pipe,
587 usbhs_pipe_clear(uep
->pipe
);
588 usbhs_pipe_sequence_data0(uep
->pipe
);
592 pipe
= usbhs_pipe_malloc(priv
,
593 usb_endpoint_type(desc
),
594 usb_endpoint_dir_in(desc
));
597 pipe
->mod_private
= uep
;
599 /* set epnum / maxp */
600 usbhs_pipe_config_update(pipe
, 0,
601 usb_endpoint_num(desc
),
602 usb_endpoint_maxp(desc
));
605 * usbhs_fifo_dma_push/pop_handler try to
606 * use dmaengine if possible.
607 * It will use pio handler if impossible.
609 if (usb_endpoint_dir_in(desc
))
610 pipe
->handler
= &usbhs_fifo_dma_push_handler
;
612 pipe
->handler
= &usbhs_fifo_dma_pop_handler
;
620 static int usbhsg_ep_disable(struct usb_ep
*ep
)
622 struct usbhsg_uep
*uep
= usbhsg_ep_to_uep(ep
);
623 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(uep
);
628 usbhsg_pipe_disable(uep
);
629 usbhs_pipe_free(pipe
);
631 uep
->pipe
->mod_private
= NULL
;
637 static struct usb_request
*usbhsg_ep_alloc_request(struct usb_ep
*ep
,
640 struct usbhsg_request
*ureq
;
642 ureq
= kzalloc(sizeof *ureq
, gfp_flags
);
646 usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq
));
651 static void usbhsg_ep_free_request(struct usb_ep
*ep
,
652 struct usb_request
*req
)
654 struct usbhsg_request
*ureq
= usbhsg_req_to_ureq(req
);
656 WARN_ON(!list_empty(&ureq
->pkt
.node
));
660 static int usbhsg_ep_queue(struct usb_ep
*ep
, struct usb_request
*req
,
663 struct usbhsg_uep
*uep
= usbhsg_ep_to_uep(ep
);
664 struct usbhsg_gpriv
*gpriv
= usbhsg_uep_to_gpriv(uep
);
665 struct usbhsg_request
*ureq
= usbhsg_req_to_ureq(req
);
666 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(uep
);
669 if (usbhsg_is_not_connected(gpriv
) ||
670 unlikely(!gpriv
->driver
) ||
674 usbhsg_queue_push(uep
, ureq
);
679 static int usbhsg_ep_dequeue(struct usb_ep
*ep
, struct usb_request
*req
)
681 struct usbhsg_uep
*uep
= usbhsg_ep_to_uep(ep
);
682 struct usbhsg_request
*ureq
= usbhsg_req_to_ureq(req
);
683 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(uep
);
685 usbhs_pkt_pop(pipe
, usbhsg_ureq_to_pkt(ureq
));
686 usbhsg_queue_pop(uep
, ureq
, -ECONNRESET
);
691 static int __usbhsg_ep_set_halt_wedge(struct usb_ep
*ep
, int halt
, int wedge
)
693 struct usbhsg_uep
*uep
= usbhsg_ep_to_uep(ep
);
694 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(uep
);
695 struct usbhsg_gpriv
*gpriv
= usbhsg_uep_to_gpriv(uep
);
696 struct usbhs_priv
*priv
= usbhsg_gpriv_to_priv(gpriv
);
697 struct device
*dev
= usbhsg_gpriv_to_dev(gpriv
);
700 usbhsg_pipe_disable(uep
);
702 dev_dbg(dev
, "set halt %d (pipe %d)\n",
703 halt
, usbhs_pipe_number(pipe
));
705 /******************** spin lock ********************/
706 usbhs_lock(priv
, flags
);
709 usbhs_pipe_stall(pipe
);
711 usbhs_pipe_disable(pipe
);
714 usbhsg_status_set(gpriv
, USBHSG_STATUS_WEDGE
);
716 usbhsg_status_clr(gpriv
, USBHSG_STATUS_WEDGE
);
718 usbhs_unlock(priv
, flags
);
719 /******************** spin unlock ******************/
724 static int usbhsg_ep_set_halt(struct usb_ep
*ep
, int value
)
726 return __usbhsg_ep_set_halt_wedge(ep
, value
, 0);
729 static int usbhsg_ep_set_wedge(struct usb_ep
*ep
)
731 return __usbhsg_ep_set_halt_wedge(ep
, 1, 1);
734 static struct usb_ep_ops usbhsg_ep_ops
= {
735 .enable
= usbhsg_ep_enable
,
736 .disable
= usbhsg_ep_disable
,
738 .alloc_request
= usbhsg_ep_alloc_request
,
739 .free_request
= usbhsg_ep_free_request
,
741 .queue
= usbhsg_ep_queue
,
742 .dequeue
= usbhsg_ep_dequeue
,
744 .set_halt
= usbhsg_ep_set_halt
,
745 .set_wedge
= usbhsg_ep_set_wedge
,
751 static int usbhsg_can_pullup(struct usbhs_priv
*priv
)
753 struct usbhsg_gpriv
*gpriv
= usbhsg_priv_to_gpriv(priv
);
755 return gpriv
->driver
&&
756 usbhsg_status_has(gpriv
, USBHSG_STATUS_SOFT_CONNECT
);
759 static void usbhsg_update_pullup(struct usbhs_priv
*priv
)
761 if (usbhsg_can_pullup(priv
))
762 usbhs_sys_function_pullup(priv
, 1);
764 usbhs_sys_function_pullup(priv
, 0);
768 * usb module start/end
770 static int usbhsg_try_start(struct usbhs_priv
*priv
, u32 status
)
772 struct usbhsg_gpriv
*gpriv
= usbhsg_priv_to_gpriv(priv
);
773 struct usbhsg_uep
*dcp
= usbhsg_gpriv_to_dcp(gpriv
);
774 struct usbhs_mod
*mod
= usbhs_mod_get_current(priv
);
775 struct device
*dev
= usbhs_priv_to_dev(priv
);
779 /******************** spin lock ********************/
780 usbhs_lock(priv
, flags
);
782 usbhsg_status_set(gpriv
, status
);
783 if (!(usbhsg_status_has(gpriv
, USBHSG_STATUS_STARTED
) &&
784 usbhsg_status_has(gpriv
, USBHSG_STATUS_REGISTERD
)))
785 ret
= -1; /* not ready */
787 usbhs_unlock(priv
, flags
);
788 /******************** spin unlock ********************/
791 return 0; /* not ready is not error */
794 * enable interrupt and systems if ready
796 dev_dbg(dev
, "start gadget\n");
799 * pipe initialize and enable DCP
801 usbhs_fifo_init(priv
);
802 usbhs_pipe_init(priv
,
803 usbhsg_dma_map_ctrl
);
805 /* dcp init instead of usbhsg_ep_enable() */
806 dcp
->pipe
= usbhs_dcp_malloc(priv
);
807 dcp
->pipe
->mod_private
= dcp
;
808 usbhs_pipe_config_update(dcp
->pipe
, 0, 0, 64);
811 * system config enble
816 usbhs_sys_function_ctrl(priv
, 1);
817 usbhsg_update_pullup(priv
);
820 * enable irq callback
822 mod
->irq_dev_state
= usbhsg_irq_dev_state
;
823 mod
->irq_ctrl_stage
= usbhsg_irq_ctrl_stage
;
824 usbhs_irq_callback_update(priv
, mod
);
829 static int usbhsg_try_stop(struct usbhs_priv
*priv
, u32 status
)
831 struct usbhsg_gpriv
*gpriv
= usbhsg_priv_to_gpriv(priv
);
832 struct usbhs_mod
*mod
= usbhs_mod_get_current(priv
);
833 struct usbhsg_uep
*dcp
= usbhsg_gpriv_to_dcp(gpriv
);
834 struct device
*dev
= usbhs_priv_to_dev(priv
);
838 /******************** spin lock ********************/
839 usbhs_lock(priv
, flags
);
841 usbhsg_status_clr(gpriv
, status
);
842 if (!usbhsg_status_has(gpriv
, USBHSG_STATUS_STARTED
) &&
843 !usbhsg_status_has(gpriv
, USBHSG_STATUS_REGISTERD
))
844 ret
= -1; /* already done */
846 usbhs_unlock(priv
, flags
);
847 /******************** spin unlock ********************/
850 return 0; /* already done is not error */
853 * disable interrupt and systems if 1st try
855 usbhs_fifo_quit(priv
);
857 /* disable all irq */
858 mod
->irq_dev_state
= NULL
;
859 mod
->irq_ctrl_stage
= NULL
;
860 usbhs_irq_callback_update(priv
, mod
);
862 gpriv
->gadget
.speed
= USB_SPEED_UNKNOWN
;
865 usbhs_sys_set_test_mode(priv
, 0);
866 usbhs_sys_function_ctrl(priv
, 0);
868 usbhsg_ep_disable(&dcp
->ep
);
870 dev_dbg(dev
, "stop gadget\n");
880 static int usbhsg_gadget_start(struct usb_gadget
*gadget
,
881 struct usb_gadget_driver
*driver
)
883 struct usbhsg_gpriv
*gpriv
= usbhsg_gadget_to_gpriv(gadget
);
884 struct usbhs_priv
*priv
= usbhsg_gpriv_to_priv(gpriv
);
888 driver
->max_speed
< USB_SPEED_FULL
)
891 /* first hook up the driver ... */
892 gpriv
->driver
= driver
;
894 return usbhsg_try_start(priv
, USBHSG_STATUS_REGISTERD
);
897 static int usbhsg_gadget_stop(struct usb_gadget
*gadget
)
899 struct usbhsg_gpriv
*gpriv
= usbhsg_gadget_to_gpriv(gadget
);
900 struct usbhs_priv
*priv
= usbhsg_gpriv_to_priv(gpriv
);
902 usbhsg_try_stop(priv
, USBHSG_STATUS_REGISTERD
);
903 gpriv
->driver
= NULL
;
911 static int usbhsg_get_frame(struct usb_gadget
*gadget
)
913 struct usbhsg_gpriv
*gpriv
= usbhsg_gadget_to_gpriv(gadget
);
914 struct usbhs_priv
*priv
= usbhsg_gpriv_to_priv(gpriv
);
916 return usbhs_frame_get_num(priv
);
919 static int usbhsg_pullup(struct usb_gadget
*gadget
, int is_on
)
921 struct usbhsg_gpriv
*gpriv
= usbhsg_gadget_to_gpriv(gadget
);
922 struct usbhs_priv
*priv
= usbhsg_gpriv_to_priv(gpriv
);
925 usbhs_lock(priv
, flags
);
927 usbhsg_status_set(gpriv
, USBHSG_STATUS_SOFT_CONNECT
);
929 usbhsg_status_clr(gpriv
, USBHSG_STATUS_SOFT_CONNECT
);
930 usbhsg_update_pullup(priv
);
931 usbhs_unlock(priv
, flags
);
936 static int usbhsg_set_selfpowered(struct usb_gadget
*gadget
, int is_self
)
938 struct usbhsg_gpriv
*gpriv
= usbhsg_gadget_to_gpriv(gadget
);
941 usbhsg_status_set(gpriv
, USBHSG_STATUS_SELF_POWERED
);
943 usbhsg_status_clr(gpriv
, USBHSG_STATUS_SELF_POWERED
);
945 gadget
->is_selfpowered
= (is_self
!= 0);
950 static const struct usb_gadget_ops usbhsg_gadget_ops
= {
951 .get_frame
= usbhsg_get_frame
,
952 .set_selfpowered
= usbhsg_set_selfpowered
,
953 .udc_start
= usbhsg_gadget_start
,
954 .udc_stop
= usbhsg_gadget_stop
,
955 .pullup
= usbhsg_pullup
,
958 static int usbhsg_start(struct usbhs_priv
*priv
)
960 return usbhsg_try_start(priv
, USBHSG_STATUS_STARTED
);
963 static int usbhsg_stop(struct usbhs_priv
*priv
)
965 struct usbhsg_gpriv
*gpriv
= usbhsg_priv_to_gpriv(priv
);
967 /* cable disconnect */
969 gpriv
->driver
->disconnect
)
970 gpriv
->driver
->disconnect(&gpriv
->gadget
);
972 return usbhsg_try_stop(priv
, USBHSG_STATUS_STARTED
);
975 int usbhs_mod_gadget_probe(struct usbhs_priv
*priv
)
977 struct usbhsg_gpriv
*gpriv
;
978 struct usbhsg_uep
*uep
;
979 struct device
*dev
= usbhs_priv_to_dev(priv
);
980 int pipe_size
= usbhs_get_dparam(priv
, pipe_size
);
984 gpriv
= kzalloc(sizeof(struct usbhsg_gpriv
), GFP_KERNEL
);
986 dev_err(dev
, "Could not allocate gadget priv\n");
990 uep
= kzalloc(sizeof(struct usbhsg_uep
) * pipe_size
, GFP_KERNEL
);
992 dev_err(dev
, "Could not allocate ep\n");
994 goto usbhs_mod_gadget_probe_err_gpriv
;
1000 * There is no guarantee that it is possible to access usb module here.
1001 * Don't accesses to it.
1002 * The accesse will be enable after "usbhsg_start"
1008 usbhs_mod_register(priv
, &gpriv
->mod
, USBHS_GADGET
);
1011 gpriv
->mod
.name
= "gadget";
1012 gpriv
->mod
.start
= usbhsg_start
;
1013 gpriv
->mod
.stop
= usbhsg_stop
;
1015 gpriv
->uep_size
= pipe_size
;
1016 usbhsg_status_init(gpriv
);
1021 gpriv
->gadget
.dev
.parent
= dev
;
1022 gpriv
->gadget
.name
= "renesas_usbhs_udc";
1023 gpriv
->gadget
.ops
= &usbhsg_gadget_ops
;
1024 gpriv
->gadget
.max_speed
= USB_SPEED_HIGH
;
1026 INIT_LIST_HEAD(&gpriv
->gadget
.ep_list
);
1031 usbhsg_for_each_uep_with_dcp(uep
, gpriv
, i
) {
1034 snprintf(uep
->ep_name
, EP_NAME_SIZE
, "ep%d", i
);
1036 uep
->ep
.name
= uep
->ep_name
;
1037 uep
->ep
.ops
= &usbhsg_ep_ops
;
1038 INIT_LIST_HEAD(&uep
->ep
.ep_list
);
1041 if (usbhsg_is_dcp(uep
)) {
1042 gpriv
->gadget
.ep0
= &uep
->ep
;
1043 usb_ep_set_maxpacket_limit(&uep
->ep
, 64);
1045 /* init normal pipe */
1047 usb_ep_set_maxpacket_limit(&uep
->ep
, 512);
1048 list_add_tail(&uep
->ep
.ep_list
, &gpriv
->gadget
.ep_list
);
1052 ret
= usb_add_gadget_udc(dev
, &gpriv
->gadget
);
1057 dev_info(dev
, "gadget probed\n");
1064 usbhs_mod_gadget_probe_err_gpriv
:
1070 void usbhs_mod_gadget_remove(struct usbhs_priv
*priv
)
1072 struct usbhsg_gpriv
*gpriv
= usbhsg_priv_to_gpriv(priv
);
1074 usb_del_gadget_udc(&gpriv
->gadget
);