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)
60 struct usbhsg_recip_handle
{
62 int (*device
)(struct usbhs_priv
*priv
, struct usbhsg_uep
*uep
,
63 struct usb_ctrlrequest
*ctrl
);
64 int (*interface
)(struct usbhs_priv
*priv
, struct usbhsg_uep
*uep
,
65 struct usb_ctrlrequest
*ctrl
);
66 int (*endpoint
)(struct usbhs_priv
*priv
, struct usbhsg_uep
*uep
,
67 struct usb_ctrlrequest
*ctrl
);
73 #define usbhsg_priv_to_gpriv(priv) \
75 usbhs_mod_get(priv, USBHS_GADGET), \
76 struct usbhsg_gpriv, mod)
78 #define __usbhsg_for_each_uep(start, pos, g, i) \
79 for (i = start, pos = (g)->uep + i; \
81 i++, pos = (g)->uep + i)
83 #define usbhsg_for_each_uep(pos, gpriv, i) \
84 __usbhsg_for_each_uep(1, pos, gpriv, i)
86 #define usbhsg_for_each_uep_with_dcp(pos, gpriv, i) \
87 __usbhsg_for_each_uep(0, pos, gpriv, i)
89 #define usbhsg_gadget_to_gpriv(g)\
90 container_of(g, struct usbhsg_gpriv, gadget)
92 #define usbhsg_req_to_ureq(r)\
93 container_of(r, struct usbhsg_request, req)
95 #define usbhsg_ep_to_uep(e) container_of(e, struct usbhsg_uep, ep)
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 static void usbhsg_queue_pop(struct usbhsg_uep
*uep
,
121 struct usbhsg_request
*ureq
,
124 struct usbhsg_gpriv
*gpriv
= usbhsg_uep_to_gpriv(uep
);
125 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(uep
);
126 struct device
*dev
= usbhsg_gpriv_to_dev(gpriv
);
128 dev_dbg(dev
, "pipe %d : queue pop\n", usbhs_pipe_number(pipe
));
130 ureq
->req
.status
= status
;
131 ureq
->req
.complete(&uep
->ep
, &ureq
->req
);
134 static void usbhsg_queue_done(struct usbhs_priv
*priv
, struct usbhs_pkt
*pkt
)
136 struct usbhs_pipe
*pipe
= pkt
->pipe
;
137 struct usbhsg_uep
*uep
= usbhsg_pipe_to_uep(pipe
);
138 struct usbhsg_request
*ureq
= usbhsg_pkt_to_ureq(pkt
);
140 ureq
->req
.actual
= pkt
->actual
;
142 usbhsg_queue_pop(uep
, ureq
, 0);
145 static void usbhsg_queue_push(struct usbhsg_uep
*uep
,
146 struct usbhsg_request
*ureq
)
148 struct usbhsg_gpriv
*gpriv
= usbhsg_uep_to_gpriv(uep
);
149 struct device
*dev
= usbhsg_gpriv_to_dev(gpriv
);
150 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(uep
);
151 struct usbhs_pkt
*pkt
= usbhsg_ureq_to_pkt(ureq
);
152 struct usb_request
*req
= &ureq
->req
;
155 req
->status
= -EINPROGRESS
;
156 usbhs_pkt_push(pipe
, pkt
, usbhsg_queue_done
,
157 req
->buf
, req
->length
, req
->zero
, -1);
158 usbhs_pkt_start(pipe
);
160 dev_dbg(dev
, "pipe %d : queue push (%d)\n",
161 usbhs_pipe_number(pipe
),
168 static int usbhsg_dma_map_ctrl(struct usbhs_pkt
*pkt
, int map
)
170 struct usbhsg_request
*ureq
= usbhsg_pkt_to_ureq(pkt
);
171 struct usb_request
*req
= &ureq
->req
;
172 struct usbhs_pipe
*pipe
= pkt
->pipe
;
173 struct usbhsg_uep
*uep
= usbhsg_pipe_to_uep(pipe
);
174 struct usbhsg_gpriv
*gpriv
= usbhsg_uep_to_gpriv(uep
);
175 enum dma_data_direction dir
;
178 dir
= usbhs_pipe_is_dir_host(pipe
);
181 /* it can not use scatter/gather */
182 WARN_ON(req
->num_sgs
);
184 ret
= usb_gadget_map_request(&gpriv
->gadget
, req
, dir
);
190 usb_gadget_unmap_request(&gpriv
->gadget
, req
, dir
);
197 * USB_TYPE_STANDARD / clear feature functions
199 static int usbhsg_recip_handler_std_control_done(struct usbhs_priv
*priv
,
200 struct usbhsg_uep
*uep
,
201 struct usb_ctrlrequest
*ctrl
)
203 struct usbhsg_gpriv
*gpriv
= usbhsg_priv_to_gpriv(priv
);
204 struct usbhsg_uep
*dcp
= usbhsg_gpriv_to_dcp(gpriv
);
205 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(dcp
);
207 usbhs_dcp_control_transfer_done(pipe
);
212 static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv
*priv
,
213 struct usbhsg_uep
*uep
,
214 struct usb_ctrlrequest
*ctrl
)
216 struct usbhsg_gpriv
*gpriv
= usbhsg_uep_to_gpriv(uep
);
217 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(uep
);
219 if (!usbhsg_status_has(gpriv
, USBHSG_STATUS_WEDGE
)) {
220 usbhs_pipe_disable(pipe
);
221 usbhs_pipe_sequence_data0(pipe
);
222 usbhs_pipe_enable(pipe
);
225 usbhsg_recip_handler_std_control_done(priv
, uep
, ctrl
);
227 usbhs_pkt_start(pipe
);
232 struct usbhsg_recip_handle req_clear_feature
= {
233 .name
= "clear feature",
234 .device
= usbhsg_recip_handler_std_control_done
,
235 .interface
= usbhsg_recip_handler_std_control_done
,
236 .endpoint
= usbhsg_recip_handler_std_clear_endpoint
,
240 * USB_TYPE_STANDARD / set feature functions
242 static int usbhsg_recip_handler_std_set_device(struct usbhs_priv
*priv
,
243 struct usbhsg_uep
*uep
,
244 struct usb_ctrlrequest
*ctrl
)
246 switch (le16_to_cpu(ctrl
->wValue
)) {
247 case USB_DEVICE_TEST_MODE
:
248 usbhsg_recip_handler_std_control_done(priv
, uep
, ctrl
);
250 usbhs_sys_set_test_mode(priv
, le16_to_cpu(ctrl
->wIndex
>> 8));
253 usbhsg_recip_handler_std_control_done(priv
, uep
, ctrl
);
260 static int usbhsg_recip_handler_std_set_endpoint(struct usbhs_priv
*priv
,
261 struct usbhsg_uep
*uep
,
262 struct usb_ctrlrequest
*ctrl
)
264 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(uep
);
266 usbhs_pipe_stall(pipe
);
268 usbhsg_recip_handler_std_control_done(priv
, uep
, ctrl
);
273 struct usbhsg_recip_handle req_set_feature
= {
274 .name
= "set feature",
275 .device
= usbhsg_recip_handler_std_set_device
,
276 .interface
= usbhsg_recip_handler_std_control_done
,
277 .endpoint
= usbhsg_recip_handler_std_set_endpoint
,
281 * USB_TYPE_STANDARD / get status functions
283 static void __usbhsg_recip_send_complete(struct usb_ep
*ep
,
284 struct usb_request
*req
)
286 struct usbhsg_request
*ureq
= usbhsg_req_to_ureq(req
);
288 /* free allocated recip-buffer/usb_request */
289 kfree(ureq
->pkt
.buf
);
290 usb_ep_free_request(ep
, req
);
293 static void __usbhsg_recip_send_status(struct usbhsg_gpriv
*gpriv
,
294 unsigned short status
)
296 struct usbhsg_uep
*dcp
= usbhsg_gpriv_to_dcp(gpriv
);
297 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(dcp
);
298 struct device
*dev
= usbhsg_gpriv_to_dev(gpriv
);
299 struct usb_request
*req
;
302 /* alloc new usb_request for recip */
303 req
= usb_ep_alloc_request(&dcp
->ep
, GFP_ATOMIC
);
305 dev_err(dev
, "recip request allocation fail\n");
309 /* alloc recip data buffer */
310 buf
= kmalloc(sizeof(*buf
), GFP_ATOMIC
);
312 usb_ep_free_request(&dcp
->ep
, req
);
313 dev_err(dev
, "recip data allocation fail\n");
317 /* recip data is status */
318 *buf
= cpu_to_le16(status
);
320 /* allocated usb_request/buffer will be freed */
321 req
->complete
= __usbhsg_recip_send_complete
;
323 req
->length
= sizeof(*buf
);
327 pipe
->handler
= &usbhs_fifo_pio_push_handler
;
328 usbhsg_queue_push(dcp
, usbhsg_req_to_ureq(req
));
331 static int usbhsg_recip_handler_std_get_device(struct usbhs_priv
*priv
,
332 struct usbhsg_uep
*uep
,
333 struct usb_ctrlrequest
*ctrl
)
335 struct usbhsg_gpriv
*gpriv
= usbhsg_uep_to_gpriv(uep
);
336 unsigned short status
= 1 << USB_DEVICE_SELF_POWERED
;
338 __usbhsg_recip_send_status(gpriv
, status
);
343 static int usbhsg_recip_handler_std_get_interface(struct usbhs_priv
*priv
,
344 struct usbhsg_uep
*uep
,
345 struct usb_ctrlrequest
*ctrl
)
347 struct usbhsg_gpriv
*gpriv
= usbhsg_uep_to_gpriv(uep
);
348 unsigned short status
= 0;
350 __usbhsg_recip_send_status(gpriv
, status
);
355 static int usbhsg_recip_handler_std_get_endpoint(struct usbhs_priv
*priv
,
356 struct usbhsg_uep
*uep
,
357 struct usb_ctrlrequest
*ctrl
)
359 struct usbhsg_gpriv
*gpriv
= usbhsg_uep_to_gpriv(uep
);
360 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(uep
);
361 unsigned short status
= 0;
363 if (usbhs_pipe_is_stall(pipe
))
364 status
= 1 << USB_ENDPOINT_HALT
;
366 __usbhsg_recip_send_status(gpriv
, status
);
371 struct usbhsg_recip_handle req_get_status
= {
372 .name
= "get status",
373 .device
= usbhsg_recip_handler_std_get_device
,
374 .interface
= usbhsg_recip_handler_std_get_interface
,
375 .endpoint
= usbhsg_recip_handler_std_get_endpoint
,
381 static int usbhsg_recip_run_handle(struct usbhs_priv
*priv
,
382 struct usbhsg_recip_handle
*handler
,
383 struct usb_ctrlrequest
*ctrl
)
385 struct usbhsg_gpriv
*gpriv
= usbhsg_priv_to_gpriv(priv
);
386 struct device
*dev
= usbhsg_gpriv_to_dev(gpriv
);
387 struct usbhsg_uep
*uep
;
388 struct usbhs_pipe
*pipe
;
389 int recip
= ctrl
->bRequestType
& USB_RECIP_MASK
;
390 int nth
= le16_to_cpu(ctrl
->wIndex
) & USB_ENDPOINT_NUMBER_MASK
;
392 int (*func
)(struct usbhs_priv
*priv
, struct usbhsg_uep
*uep
,
393 struct usb_ctrlrequest
*ctrl
);
396 uep
= usbhsg_gpriv_to_nth_uep(gpriv
, nth
);
397 pipe
= usbhsg_uep_to_pipe(uep
);
399 dev_err(dev
, "wrong recip request\n");
404 case USB_RECIP_DEVICE
:
406 func
= handler
->device
;
408 case USB_RECIP_INTERFACE
:
410 func
= handler
->interface
;
412 case USB_RECIP_ENDPOINT
:
414 func
= handler
->endpoint
;
417 dev_warn(dev
, "unsupported RECIP(%d)\n", recip
);
423 dev_dbg(dev
, "%s (pipe %d :%s)\n", handler
->name
, nth
, msg
);
424 ret
= func(priv
, uep
, ctrl
);
433 * it will be called from usbhs_interrupt
435 static int usbhsg_irq_dev_state(struct usbhs_priv
*priv
,
436 struct usbhs_irq_state
*irq_state
)
438 struct usbhsg_gpriv
*gpriv
= usbhsg_priv_to_gpriv(priv
);
439 struct device
*dev
= usbhsg_gpriv_to_dev(gpriv
);
441 gpriv
->gadget
.speed
= usbhs_bus_get_speed(priv
);
443 dev_dbg(dev
, "state = %x : speed : %d\n",
444 usbhs_status_get_device_state(irq_state
),
445 gpriv
->gadget
.speed
);
450 static int usbhsg_irq_ctrl_stage(struct usbhs_priv
*priv
,
451 struct usbhs_irq_state
*irq_state
)
453 struct usbhsg_gpriv
*gpriv
= usbhsg_priv_to_gpriv(priv
);
454 struct usbhsg_uep
*dcp
= usbhsg_gpriv_to_dcp(gpriv
);
455 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(dcp
);
456 struct device
*dev
= usbhsg_gpriv_to_dev(gpriv
);
457 struct usb_ctrlrequest ctrl
;
458 struct usbhsg_recip_handle
*recip_handler
= NULL
;
459 int stage
= usbhs_status_get_ctrl_stage(irq_state
);
462 dev_dbg(dev
, "stage = %d\n", stage
);
468 * - "Interrupt Function"
469 * - "Control Transfer Stage Transition Interrupt"
470 * - Fig. "Control Transfer Stage Transitions"
474 case READ_DATA_STAGE
:
475 pipe
->handler
= &usbhs_fifo_pio_push_handler
;
477 case WRITE_DATA_STAGE
:
478 pipe
->handler
= &usbhs_fifo_pio_pop_handler
;
480 case NODATA_STATUS_STAGE
:
481 pipe
->handler
= &usbhs_ctrl_stage_end_handler
;
490 usbhs_usbreq_get_val(priv
, &ctrl
);
492 switch (ctrl
.bRequestType
& USB_TYPE_MASK
) {
493 case USB_TYPE_STANDARD
:
494 switch (ctrl
.bRequest
) {
495 case USB_REQ_CLEAR_FEATURE
:
496 recip_handler
= &req_clear_feature
;
498 case USB_REQ_SET_FEATURE
:
499 recip_handler
= &req_set_feature
;
501 case USB_REQ_GET_STATUS
:
502 recip_handler
= &req_get_status
;
508 * setup stage / run recip
511 ret
= usbhsg_recip_run_handle(priv
, recip_handler
, &ctrl
);
513 ret
= gpriv
->driver
->setup(&gpriv
->gadget
, &ctrl
);
516 usbhs_pipe_stall(pipe
);
526 static int usbhsg_pipe_disable(struct usbhsg_uep
*uep
)
528 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(uep
);
529 struct usbhs_pkt
*pkt
;
532 pkt
= usbhs_pkt_pop(pipe
, NULL
);
536 usbhsg_queue_pop(uep
, usbhsg_pkt_to_ureq(pkt
), -ECONNRESET
);
539 usbhs_pipe_disable(pipe
);
544 static void usbhsg_uep_init(struct usbhsg_gpriv
*gpriv
)
547 struct usbhsg_uep
*uep
;
549 usbhsg_for_each_uep_with_dcp(uep
, gpriv
, i
)
558 static int usbhsg_ep_enable(struct usb_ep
*ep
,
559 const struct usb_endpoint_descriptor
*desc
)
561 struct usbhsg_uep
*uep
= usbhsg_ep_to_uep(ep
);
562 struct usbhsg_gpriv
*gpriv
= usbhsg_uep_to_gpriv(uep
);
563 struct usbhs_priv
*priv
= usbhsg_gpriv_to_priv(gpriv
);
564 struct usbhs_pipe
*pipe
;
568 * if it already have pipe,
572 usbhs_pipe_clear(uep
->pipe
);
573 usbhs_pipe_sequence_data0(uep
->pipe
);
577 pipe
= usbhs_pipe_malloc(priv
,
578 usb_endpoint_type(desc
),
579 usb_endpoint_dir_in(desc
));
582 pipe
->mod_private
= uep
;
584 /* set epnum / maxp */
585 usbhs_pipe_config_update(pipe
, 0,
586 usb_endpoint_num(desc
),
587 usb_endpoint_maxp(desc
));
590 * usbhs_fifo_dma_push/pop_handler try to
591 * use dmaengine if possible.
592 * It will use pio handler if impossible.
594 if (usb_endpoint_dir_in(desc
))
595 pipe
->handler
= &usbhs_fifo_dma_push_handler
;
597 pipe
->handler
= &usbhs_fifo_dma_pop_handler
;
605 static int usbhsg_ep_disable(struct usb_ep
*ep
)
607 struct usbhsg_uep
*uep
= usbhsg_ep_to_uep(ep
);
609 return usbhsg_pipe_disable(uep
);
612 static struct usb_request
*usbhsg_ep_alloc_request(struct usb_ep
*ep
,
615 struct usbhsg_request
*ureq
;
617 ureq
= kzalloc(sizeof *ureq
, gfp_flags
);
621 usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq
));
626 static void usbhsg_ep_free_request(struct usb_ep
*ep
,
627 struct usb_request
*req
)
629 struct usbhsg_request
*ureq
= usbhsg_req_to_ureq(req
);
631 WARN_ON(!list_empty(&ureq
->pkt
.node
));
635 static int usbhsg_ep_queue(struct usb_ep
*ep
, struct usb_request
*req
,
638 struct usbhsg_uep
*uep
= usbhsg_ep_to_uep(ep
);
639 struct usbhsg_gpriv
*gpriv
= usbhsg_uep_to_gpriv(uep
);
640 struct usbhsg_request
*ureq
= usbhsg_req_to_ureq(req
);
641 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(uep
);
644 if (usbhsg_is_not_connected(gpriv
) ||
645 unlikely(!gpriv
->driver
) ||
649 usbhsg_queue_push(uep
, ureq
);
654 static int usbhsg_ep_dequeue(struct usb_ep
*ep
, struct usb_request
*req
)
656 struct usbhsg_uep
*uep
= usbhsg_ep_to_uep(ep
);
657 struct usbhsg_request
*ureq
= usbhsg_req_to_ureq(req
);
658 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(uep
);
660 usbhs_pkt_pop(pipe
, usbhsg_ureq_to_pkt(ureq
));
661 usbhsg_queue_pop(uep
, ureq
, -ECONNRESET
);
666 static int __usbhsg_ep_set_halt_wedge(struct usb_ep
*ep
, int halt
, int wedge
)
668 struct usbhsg_uep
*uep
= usbhsg_ep_to_uep(ep
);
669 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(uep
);
670 struct usbhsg_gpriv
*gpriv
= usbhsg_uep_to_gpriv(uep
);
671 struct usbhs_priv
*priv
= usbhsg_gpriv_to_priv(gpriv
);
672 struct device
*dev
= usbhsg_gpriv_to_dev(gpriv
);
675 usbhsg_pipe_disable(uep
);
677 dev_dbg(dev
, "set halt %d (pipe %d)\n",
678 halt
, usbhs_pipe_number(pipe
));
680 /******************** spin lock ********************/
681 usbhs_lock(priv
, flags
);
684 usbhs_pipe_stall(pipe
);
686 usbhs_pipe_disable(pipe
);
689 usbhsg_status_set(gpriv
, USBHSG_STATUS_WEDGE
);
691 usbhsg_status_clr(gpriv
, USBHSG_STATUS_WEDGE
);
693 usbhs_unlock(priv
, flags
);
694 /******************** spin unlock ******************/
699 static int usbhsg_ep_set_halt(struct usb_ep
*ep
, int value
)
701 return __usbhsg_ep_set_halt_wedge(ep
, value
, 0);
704 static int usbhsg_ep_set_wedge(struct usb_ep
*ep
)
706 return __usbhsg_ep_set_halt_wedge(ep
, 1, 1);
709 static struct usb_ep_ops usbhsg_ep_ops
= {
710 .enable
= usbhsg_ep_enable
,
711 .disable
= usbhsg_ep_disable
,
713 .alloc_request
= usbhsg_ep_alloc_request
,
714 .free_request
= usbhsg_ep_free_request
,
716 .queue
= usbhsg_ep_queue
,
717 .dequeue
= usbhsg_ep_dequeue
,
719 .set_halt
= usbhsg_ep_set_halt
,
720 .set_wedge
= usbhsg_ep_set_wedge
,
724 * usb module start/end
726 static int usbhsg_try_start(struct usbhs_priv
*priv
, u32 status
)
728 struct usbhsg_gpriv
*gpriv
= usbhsg_priv_to_gpriv(priv
);
729 struct usbhsg_uep
*dcp
= usbhsg_gpriv_to_dcp(gpriv
);
730 struct usbhs_mod
*mod
= usbhs_mod_get_current(priv
);
731 struct device
*dev
= usbhs_priv_to_dev(priv
);
735 /******************** spin lock ********************/
736 usbhs_lock(priv
, flags
);
738 usbhsg_status_set(gpriv
, status
);
739 if (!(usbhsg_status_has(gpriv
, USBHSG_STATUS_STARTED
) &&
740 usbhsg_status_has(gpriv
, USBHSG_STATUS_REGISTERD
)))
741 ret
= -1; /* not ready */
743 usbhs_unlock(priv
, flags
);
744 /******************** spin unlock ********************/
747 return 0; /* not ready is not error */
750 * enable interrupt and systems if ready
752 dev_dbg(dev
, "start gadget\n");
755 * pipe initialize and enable DCP
757 usbhs_pipe_init(priv
,
758 usbhsg_dma_map_ctrl
);
759 usbhs_fifo_init(priv
);
760 usbhsg_uep_init(gpriv
);
763 dcp
->pipe
= usbhs_dcp_malloc(priv
);
764 dcp
->pipe
->mod_private
= dcp
;
765 usbhs_pipe_config_update(dcp
->pipe
, 0, 0, 64);
768 * system config enble
773 usbhs_sys_function_ctrl(priv
, 1);
776 * enable irq callback
778 mod
->irq_dev_state
= usbhsg_irq_dev_state
;
779 mod
->irq_ctrl_stage
= usbhsg_irq_ctrl_stage
;
780 usbhs_irq_callback_update(priv
, mod
);
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 usbhs_lock(priv
, flags
);
797 usbhsg_status_clr(gpriv
, status
);
798 if (!usbhsg_status_has(gpriv
, USBHSG_STATUS_STARTED
) &&
799 !usbhsg_status_has(gpriv
, USBHSG_STATUS_REGISTERD
))
800 ret
= -1; /* already done */
802 usbhs_unlock(priv
, flags
);
803 /******************** spin unlock ********************/
806 return 0; /* already done is not error */
809 * disable interrupt and systems if 1st try
811 usbhs_fifo_quit(priv
);
813 /* disable all irq */
814 mod
->irq_dev_state
= NULL
;
815 mod
->irq_ctrl_stage
= NULL
;
816 usbhs_irq_callback_update(priv
, mod
);
818 gpriv
->gadget
.speed
= USB_SPEED_UNKNOWN
;
821 usbhs_sys_set_test_mode(priv
, 0);
822 usbhs_sys_function_ctrl(priv
, 0);
824 usbhsg_pipe_disable(dcp
);
826 dev_dbg(dev
, "stop gadget\n");
836 static int usbhsg_gadget_start(struct usb_gadget
*gadget
,
837 struct usb_gadget_driver
*driver
)
839 struct usbhsg_gpriv
*gpriv
= usbhsg_gadget_to_gpriv(gadget
);
840 struct usbhs_priv
*priv
= usbhsg_gpriv_to_priv(gpriv
);
844 driver
->max_speed
< USB_SPEED_FULL
)
847 /* first hook up the driver ... */
848 gpriv
->driver
= driver
;
849 gpriv
->gadget
.dev
.driver
= &driver
->driver
;
851 return usbhsg_try_start(priv
, USBHSG_STATUS_REGISTERD
);
854 static int usbhsg_gadget_stop(struct usb_gadget
*gadget
,
855 struct usb_gadget_driver
*driver
)
857 struct usbhsg_gpriv
*gpriv
= usbhsg_gadget_to_gpriv(gadget
);
858 struct usbhs_priv
*priv
= usbhsg_gpriv_to_priv(gpriv
);
864 usbhsg_try_stop(priv
, USBHSG_STATUS_REGISTERD
);
865 gpriv
->gadget
.dev
.driver
= NULL
;
866 gpriv
->driver
= NULL
;
874 static int usbhsg_get_frame(struct usb_gadget
*gadget
)
876 struct usbhsg_gpriv
*gpriv
= usbhsg_gadget_to_gpriv(gadget
);
877 struct usbhs_priv
*priv
= usbhsg_gpriv_to_priv(gpriv
);
879 return usbhs_frame_get_num(priv
);
882 static struct usb_gadget_ops usbhsg_gadget_ops
= {
883 .get_frame
= usbhsg_get_frame
,
884 .udc_start
= usbhsg_gadget_start
,
885 .udc_stop
= usbhsg_gadget_stop
,
888 static int usbhsg_start(struct usbhs_priv
*priv
)
890 return usbhsg_try_start(priv
, USBHSG_STATUS_STARTED
);
893 static int usbhsg_stop(struct usbhs_priv
*priv
)
895 struct usbhsg_gpriv
*gpriv
= usbhsg_priv_to_gpriv(priv
);
897 /* cable disconnect */
899 gpriv
->driver
->disconnect
)
900 gpriv
->driver
->disconnect(&gpriv
->gadget
);
902 return usbhsg_try_stop(priv
, USBHSG_STATUS_STARTED
);
905 static void usbhs_mod_gadget_release(struct device
*pdev
)
910 int usbhs_mod_gadget_probe(struct usbhs_priv
*priv
)
912 struct usbhsg_gpriv
*gpriv
;
913 struct usbhsg_uep
*uep
;
914 struct device
*dev
= usbhs_priv_to_dev(priv
);
915 int pipe_size
= usbhs_get_dparam(priv
, pipe_size
);
919 gpriv
= kzalloc(sizeof(struct usbhsg_gpriv
), GFP_KERNEL
);
921 dev_err(dev
, "Could not allocate gadget priv\n");
925 uep
= kzalloc(sizeof(struct usbhsg_uep
) * pipe_size
, GFP_KERNEL
);
927 dev_err(dev
, "Could not allocate ep\n");
929 goto usbhs_mod_gadget_probe_err_gpriv
;
935 * There is no guarantee that it is possible to access usb module here.
936 * Don't accesses to it.
937 * The accesse will be enable after "usbhsg_start"
943 usbhs_mod_register(priv
, &gpriv
->mod
, USBHS_GADGET
);
946 gpriv
->mod
.name
= "gadget";
947 gpriv
->mod
.start
= usbhsg_start
;
948 gpriv
->mod
.stop
= usbhsg_stop
;
950 gpriv
->uep_size
= pipe_size
;
951 usbhsg_status_init(gpriv
);
956 dev_set_name(&gpriv
->gadget
.dev
, "gadget");
957 gpriv
->gadget
.dev
.parent
= dev
;
958 gpriv
->gadget
.dev
.release
= usbhs_mod_gadget_release
;
959 gpriv
->gadget
.name
= "renesas_usbhs_udc";
960 gpriv
->gadget
.ops
= &usbhsg_gadget_ops
;
961 gpriv
->gadget
.max_speed
= USB_SPEED_HIGH
;
962 ret
= device_register(&gpriv
->gadget
.dev
);
966 INIT_LIST_HEAD(&gpriv
->gadget
.ep_list
);
971 usbhsg_for_each_uep_with_dcp(uep
, gpriv
, i
) {
973 snprintf(uep
->ep_name
, EP_NAME_SIZE
, "ep%d", i
);
975 uep
->ep
.name
= uep
->ep_name
;
976 uep
->ep
.ops
= &usbhsg_ep_ops
;
977 INIT_LIST_HEAD(&uep
->ep
.ep_list
);
980 if (usbhsg_is_dcp(uep
)) {
981 gpriv
->gadget
.ep0
= &uep
->ep
;
982 uep
->ep
.maxpacket
= 64;
984 /* init normal pipe */
986 uep
->ep
.maxpacket
= 512;
987 list_add_tail(&uep
->ep
.ep_list
, &gpriv
->gadget
.ep_list
);
991 ret
= usb_add_gadget_udc(dev
, &gpriv
->gadget
);
996 dev_info(dev
, "gadget probed\n");
1001 device_unregister(&gpriv
->gadget
.dev
);
1005 usbhs_mod_gadget_probe_err_gpriv
:
1011 void usbhs_mod_gadget_remove(struct usbhs_priv
*priv
)
1013 struct usbhsg_gpriv
*gpriv
= usbhsg_priv_to_gpriv(priv
);
1015 usb_del_gadget_udc(&gpriv
->gadget
);
1017 device_unregister(&gpriv
->gadget
.dev
);