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
;
29 struct list_head node
;
32 #define EP_NAME_SIZE 8
34 struct usbhsg_pipe_handle
;
37 struct usbhs_pipe
*pipe
;
38 struct list_head list
;
40 char ep_name
[EP_NAME_SIZE
];
42 struct usbhsg_gpriv
*gpriv
;
43 struct usbhsg_pipe_handle
*handler
;
47 struct usb_gadget gadget
;
50 struct usbhsg_uep
*uep
;
53 struct usb_gadget_driver
*driver
;
56 #define USBHSG_STATUS_STARTED (1 << 0)
57 #define USBHSG_STATUS_REGISTERD (1 << 1)
58 #define USBHSG_STATUS_WEDGE (1 << 2)
61 struct usbhsg_pipe_handle
{
62 int (*prepare
)(struct usbhsg_uep
*uep
, struct usbhsg_request
*ureq
);
63 int (*try_run
)(struct usbhsg_uep
*uep
, struct usbhsg_request
*ureq
);
64 void (*irq_mask
)(struct usbhsg_uep
*uep
, int enable
);
67 struct usbhsg_recip_handle
{
69 int (*device
)(struct usbhs_priv
*priv
, struct usbhsg_uep
*uep
,
70 struct usb_ctrlrequest
*ctrl
);
71 int (*interface
)(struct usbhs_priv
*priv
, struct usbhsg_uep
*uep
,
72 struct usb_ctrlrequest
*ctrl
);
73 int (*endpoint
)(struct usbhs_priv
*priv
, struct usbhsg_uep
*uep
,
74 struct usb_ctrlrequest
*ctrl
);
80 #define usbhsg_priv_to_gpriv(priv) \
82 usbhs_mod_get(priv, USBHS_GADGET), \
83 struct usbhsg_gpriv, mod)
85 #define __usbhsg_for_each_uep(start, pos, g, i) \
86 for (i = start, pos = (g)->uep; \
88 i++, pos = (g)->uep + i)
90 #define usbhsg_for_each_uep(pos, gpriv, i) \
91 __usbhsg_for_each_uep(1, pos, gpriv, i)
93 #define usbhsg_for_each_uep_with_dcp(pos, gpriv, i) \
94 __usbhsg_for_each_uep(0, pos, gpriv, i)
96 #define usbhsg_gadget_to_gpriv(g)\
97 container_of(g, struct usbhsg_gpriv, gadget)
99 #define usbhsg_req_to_ureq(r)\
100 container_of(r, struct usbhsg_request, req)
102 #define usbhsg_ep_to_uep(e) container_of(e, struct usbhsg_uep, ep)
103 #define usbhsg_gpriv_to_lock(gp) usbhs_priv_to_lock((gp)->mod.priv)
104 #define usbhsg_gpriv_to_dev(gp) usbhs_priv_to_dev((gp)->mod.priv)
105 #define usbhsg_gpriv_to_priv(gp) ((gp)->mod.priv)
106 #define usbhsg_gpriv_to_dcp(gp) ((gp)->uep)
107 #define usbhsg_gpriv_to_nth_uep(gp, i) ((gp)->uep + i)
108 #define usbhsg_uep_to_gpriv(u) ((u)->gpriv)
109 #define usbhsg_uep_to_pipe(u) ((u)->pipe)
110 #define usbhsg_pipe_to_uep(p) ((p)->mod_private)
111 #define usbhsg_is_dcp(u) ((u) == usbhsg_gpriv_to_dcp((u)->gpriv))
113 #define usbhsg_is_not_connected(gp) ((gp)->gadget.speed == USB_SPEED_UNKNOWN)
116 #define usbhsg_status_init(gp) do {(gp)->status = 0; } while (0)
117 #define usbhsg_status_set(gp, b) (gp->status |= b)
118 #define usbhsg_status_clr(gp, b) (gp->status &= ~b)
119 #define usbhsg_status_has(gp, b) (gp->status & b)
124 static void usbhsg_queue_push(struct usbhsg_uep
*uep
,
125 struct usbhsg_request
*ureq
)
127 struct usbhsg_gpriv
*gpriv
= usbhsg_uep_to_gpriv(uep
);
128 struct device
*dev
= usbhsg_gpriv_to_dev(gpriv
);
129 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(uep
);
132 ********* assume under spin lock *********
134 list_del_init(&ureq
->node
);
135 list_add_tail(&ureq
->node
, &uep
->list
);
136 ureq
->req
.actual
= 0;
137 ureq
->req
.status
= -EINPROGRESS
;
139 dev_dbg(dev
, "pipe %d : queue push (%d)\n",
140 usbhs_pipe_number(pipe
),
144 static struct usbhsg_request
*usbhsg_queue_get(struct usbhsg_uep
*uep
)
147 ********* assume under spin lock *********
149 if (list_empty(&uep
->list
))
152 return list_entry(uep
->list
.next
, struct usbhsg_request
, node
);
155 #define usbhsg_queue_prepare(uep) __usbhsg_queue_handler(uep, 1);
156 #define usbhsg_queue_handle(uep) __usbhsg_queue_handler(uep, 0);
157 static int __usbhsg_queue_handler(struct usbhsg_uep
*uep
, int prepare
)
159 struct usbhsg_gpriv
*gpriv
= usbhsg_uep_to_gpriv(uep
);
160 struct device
*dev
= usbhsg_gpriv_to_dev(gpriv
);
161 struct usbhsg_request
*ureq
;
162 spinlock_t
*lock
= usbhsg_gpriv_to_lock(gpriv
);
168 dev_err(dev
, "no handler function\n");
173 * CAUTION [*queue handler*]
175 * This function will be called for start/restart queue operation.
176 * OTOH the most much worry for USB driver is spinlock nest.
178 * - usb_ep_ops :: queue
179 * - usb_request :: complete
181 * But the caller of this function need not care about spinlock.
182 * This function is using spin_trylock_irqsave for it.
183 * if "is_locked" is 1, this mean this function lock it.
184 * but if it is 0, this mean it is already under spin lock.
186 * CAUTION [*endpoint queue*]
187 * CAUTION [*request complete*]
190 /****************** spin try lock *******************/
191 is_locked
= spin_trylock_irqsave(lock
, flags
);
192 ureq
= usbhsg_queue_get(uep
);
195 ret
= uep
->handler
->prepare(uep
, ureq
);
197 ret
= uep
->handler
->try_run(uep
, ureq
);
200 spin_unlock_irqrestore(lock
, flags
);
201 /******************** spin unlock ******************/
206 static void usbhsg_queue_pop(struct usbhsg_uep
*uep
,
207 struct usbhsg_request
*ureq
,
210 struct usbhsg_gpriv
*gpriv
= usbhsg_uep_to_gpriv(uep
);
211 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(uep
);
212 struct device
*dev
= usbhsg_gpriv_to_dev(gpriv
);
215 ********* assume under spin lock *********
219 * CAUTION [*request complete*]
221 * There is a possibility not to be called in correct order
222 * if "complete" is called without spinlock.
224 * So, this function assume it is under spinlock,
225 * and call usb_request :: complete.
227 * But this "complete" will push next usb_request.
228 * It mean "usb_ep_ops :: queue" which is using spinlock is called
231 * To avoid dead-lock, this driver is using spin_trylock.
232 * CAUTION [*endpoint queue*]
233 * CAUTION [*queue handler*]
236 dev_dbg(dev
, "pipe %d : queue pop\n", usbhs_pipe_number(pipe
));
238 list_del_init(&ureq
->node
);
240 ureq
->req
.status
= status
;
241 ureq
->req
.complete(&uep
->ep
, &ureq
->req
);
245 usbhsg_queue_prepare(uep
);
249 * irq enable/disable function
251 #define usbhsg_irq_callback_ctrl(uep, status, enable) \
253 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); \
254 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); \
255 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); \
256 struct usbhs_mod *mod = usbhs_mod_get_current(priv); \
260 mod->irq_##status |= (1 << usbhs_pipe_number(pipe)); \
262 mod->irq_##status &= ~(1 << usbhs_pipe_number(pipe)); \
263 usbhs_irq_callback_update(priv, mod); \
266 static void usbhsg_irq_empty_ctrl(struct usbhsg_uep
*uep
, int enable
)
268 usbhsg_irq_callback_ctrl(uep
, bempsts
, enable
);
271 static void usbhsg_irq_ready_ctrl(struct usbhsg_uep
*uep
, int enable
)
273 usbhsg_irq_callback_ctrl(uep
, brdysts
, enable
);
279 static int usbhsg_try_run_ctrl_stage_end(struct usbhsg_uep
*uep
,
280 struct usbhsg_request
*ureq
)
282 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(uep
);
285 ********* assume under spin lock *********
288 usbhs_dcp_control_transfer_done(pipe
);
289 usbhsg_queue_pop(uep
, ureq
, 0);
294 static int usbhsg_try_run_send_packet(struct usbhsg_uep
*uep
,
295 struct usbhsg_request
*ureq
)
297 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(uep
);
298 struct usb_request
*req
= &ureq
->req
;
299 struct usbhsg_gpriv
*gpriv
= usbhsg_uep_to_gpriv(uep
);
300 struct device
*dev
= usbhsg_gpriv_to_dev(gpriv
);
308 ********* assume under spin lock *********
311 maxp
= usbhs_pipe_get_maxpacket(pipe
);
312 buf
= req
->buf
+ req
->actual
;
313 remainder
= req
->length
- req
->actual
;
315 send
= usbhs_fifo_write(pipe
, buf
, remainder
);
318 * send < 0 : pipe busy
319 * send = 0 : send zero packet
320 * send > 0 : send data
327 /* send all packet ? */
328 if (send
< remainder
)
329 is_done
= 0; /* there are remainder data */
330 else if (send
< maxp
)
331 is_done
= 1; /* short packet */
333 is_done
= !req
->zero
; /* send zero packet ? */
335 dev_dbg(dev
, " send %d (%d/ %d/ %d/ %d)\n",
336 usbhs_pipe_number(pipe
),
337 remainder
, send
, is_done
, req
->zero
);
340 * enable interrupt and send again in irq handler
341 * if it still have remainder data which should be sent.
344 uep
->handler
->irq_mask(uep
, enable
);
347 * usbhs_fifo_enable execute
348 * - after callback_update,
349 * - before queue_pop / stage_end
351 usbhs_fifo_enable(pipe
);
354 * all data were sent ?
357 /* it care below call in
359 if (usbhsg_is_dcp(uep
))
360 usbhs_dcp_control_transfer_done(pipe
);
362 usbhsg_queue_pop(uep
, ureq
, 0);
368 static int usbhsg_prepare_send_packet(struct usbhsg_uep
*uep
,
369 struct usbhsg_request
*ureq
)
371 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(uep
);
374 ********* assume under spin lock *********
377 usbhs_fifo_prepare_write(pipe
);
378 usbhsg_try_run_send_packet(uep
, ureq
);
383 static int usbhsg_try_run_receive_packet(struct usbhsg_uep
*uep
,
384 struct usbhsg_request
*ureq
)
386 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(uep
);
387 struct usb_request
*req
= &ureq
->req
;
388 struct usbhsg_gpriv
*gpriv
= usbhsg_uep_to_gpriv(uep
);
389 struct device
*dev
= usbhsg_gpriv_to_dev(gpriv
);
396 ********* assume under spin lock *********
399 maxp
= usbhs_pipe_get_maxpacket(pipe
);
400 buf
= req
->buf
+ req
->actual
;
401 remainder
= req
->length
- req
->actual
;
403 recv
= usbhs_fifo_read(pipe
, buf
, remainder
);
405 * recv < 0 : pipe busy
406 * recv >= 0 : receive data
413 /* update parameters */
416 if ((recv
== remainder
) || /* receive all data */
417 (recv
< maxp
)) /* short packet */
420 dev_dbg(dev
, " recv %d (%d/ %d/ %d/ %d)\n",
421 usbhs_pipe_number(pipe
),
422 remainder
, recv
, is_done
, req
->zero
);
424 /* read all data ? */
428 uep
->handler
->irq_mask(uep
, disable
);
429 usbhs_fifo_disable(pipe
);
430 usbhsg_queue_pop(uep
, ureq
, 0);
436 static int usbhsg_prepare_receive_packet(struct usbhsg_uep
*uep
,
437 struct usbhsg_request
*ureq
)
439 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(uep
);
444 ********* assume under spin lock *********
447 ret
= usbhs_fifo_prepare_read(pipe
);
452 * data will be read in interrupt handler
454 uep
->handler
->irq_mask(uep
, enable
);
459 static struct usbhsg_pipe_handle usbhsg_handler_send_by_empty
= {
460 .prepare
= usbhsg_prepare_send_packet
,
461 .try_run
= usbhsg_try_run_send_packet
,
462 .irq_mask
= usbhsg_irq_empty_ctrl
,
465 static struct usbhsg_pipe_handle usbhsg_handler_send_by_ready
= {
466 .prepare
= usbhsg_prepare_send_packet
,
467 .try_run
= usbhsg_try_run_send_packet
,
468 .irq_mask
= usbhsg_irq_ready_ctrl
,
471 static struct usbhsg_pipe_handle usbhsg_handler_recv_by_ready
= {
472 .prepare
= usbhsg_prepare_receive_packet
,
473 .try_run
= usbhsg_try_run_receive_packet
,
474 .irq_mask
= usbhsg_irq_ready_ctrl
,
477 static struct usbhsg_pipe_handle usbhsg_handler_ctrl_stage_end
= {
478 .prepare
= usbhsg_try_run_ctrl_stage_end
,
479 .try_run
= usbhsg_try_run_ctrl_stage_end
,
483 * DCP pipe can NOT use "ready interrupt" for "send"
484 * it should use "empty" interrupt.
486 * "Operation" - "Interrupt Function" - "BRDY Interrupt"
488 * on the other hand, normal pipe can use "ready interrupt" for "send"
489 * even though it is single/double buffer
491 #define usbhsg_handler_send_ctrl usbhsg_handler_send_by_empty
492 #define usbhsg_handler_recv_ctrl usbhsg_handler_recv_by_ready
494 #define usbhsg_handler_send_packet usbhsg_handler_send_by_ready
495 #define usbhsg_handler_recv_packet usbhsg_handler_recv_by_ready
498 * USB_TYPE_STANDARD / clear feature functions
500 static int usbhsg_recip_handler_std_control_done(struct usbhs_priv
*priv
,
501 struct usbhsg_uep
*uep
,
502 struct usb_ctrlrequest
*ctrl
)
504 struct usbhsg_gpriv
*gpriv
= usbhsg_priv_to_gpriv(priv
);
505 struct usbhsg_uep
*dcp
= usbhsg_gpriv_to_dcp(gpriv
);
506 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(dcp
);
508 usbhs_dcp_control_transfer_done(pipe
);
513 static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv
*priv
,
514 struct usbhsg_uep
*uep
,
515 struct usb_ctrlrequest
*ctrl
)
517 struct usbhsg_gpriv
*gpriv
= usbhsg_uep_to_gpriv(uep
);
518 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(uep
);
520 if (!usbhsg_status_has(gpriv
, USBHSG_STATUS_WEDGE
)) {
521 usbhs_fifo_disable(pipe
);
522 usbhs_pipe_clear_sequence(pipe
);
523 usbhs_fifo_enable(pipe
);
526 usbhsg_recip_handler_std_control_done(priv
, uep
, ctrl
);
528 usbhsg_queue_prepare(uep
);
533 struct usbhsg_recip_handle req_clear_feature
= {
534 .name
= "clear feature",
535 .device
= usbhsg_recip_handler_std_control_done
,
536 .interface
= usbhsg_recip_handler_std_control_done
,
537 .endpoint
= usbhsg_recip_handler_std_clear_endpoint
,
543 static int usbhsg_recip_run_handle(struct usbhs_priv
*priv
,
544 struct usbhsg_recip_handle
*handler
,
545 struct usb_ctrlrequest
*ctrl
)
547 struct usbhsg_gpriv
*gpriv
= usbhsg_priv_to_gpriv(priv
);
548 struct device
*dev
= usbhsg_gpriv_to_dev(gpriv
);
549 struct usbhsg_uep
*uep
;
550 int recip
= ctrl
->bRequestType
& USB_RECIP_MASK
;
551 int nth
= le16_to_cpu(ctrl
->wIndex
) & USB_ENDPOINT_NUMBER_MASK
;
553 int (*func
)(struct usbhs_priv
*priv
, struct usbhsg_uep
*uep
,
554 struct usb_ctrlrequest
*ctrl
);
557 uep
= usbhsg_gpriv_to_nth_uep(gpriv
, nth
);
560 case USB_RECIP_DEVICE
:
562 func
= handler
->device
;
564 case USB_RECIP_INTERFACE
:
566 func
= handler
->interface
;
568 case USB_RECIP_ENDPOINT
:
570 func
= handler
->endpoint
;
573 dev_warn(dev
, "unsupported RECIP(%d)\n", recip
);
579 dev_dbg(dev
, "%s (pipe %d :%s)\n", handler
->name
, nth
, msg
);
580 ret
= func(priv
, uep
, ctrl
);
589 * it will be called from usbhs_interrupt
591 static int usbhsg_irq_dev_state(struct usbhs_priv
*priv
,
592 struct usbhs_irq_state
*irq_state
)
594 struct usbhsg_gpriv
*gpriv
= usbhsg_priv_to_gpriv(priv
);
595 struct device
*dev
= usbhsg_gpriv_to_dev(gpriv
);
597 gpriv
->gadget
.speed
= usbhs_status_get_usb_speed(irq_state
);
599 dev_dbg(dev
, "state = %x : speed : %d\n",
600 usbhs_status_get_device_state(irq_state
),
601 gpriv
->gadget
.speed
);
606 static int usbhsg_irq_ctrl_stage(struct usbhs_priv
*priv
,
607 struct usbhs_irq_state
*irq_state
)
609 struct usbhsg_gpriv
*gpriv
= usbhsg_priv_to_gpriv(priv
);
610 struct usbhsg_uep
*dcp
= usbhsg_gpriv_to_dcp(gpriv
);
611 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(dcp
);
612 struct device
*dev
= usbhsg_gpriv_to_dev(gpriv
);
613 struct usb_ctrlrequest ctrl
;
614 struct usbhsg_recip_handle
*recip_handler
= NULL
;
615 int stage
= usbhs_status_get_ctrl_stage(irq_state
);
618 dev_dbg(dev
, "stage = %d\n", stage
);
624 * - "Interrupt Function"
625 * - "Control Transfer Stage Transition Interrupt"
626 * - Fig. "Control Transfer Stage Transitions"
630 case READ_DATA_STAGE
:
631 dcp
->handler
= &usbhsg_handler_send_ctrl
;
633 case WRITE_DATA_STAGE
:
634 dcp
->handler
= &usbhsg_handler_recv_ctrl
;
636 case NODATA_STATUS_STAGE
:
637 dcp
->handler
= &usbhsg_handler_ctrl_stage_end
;
646 usbhs_usbreq_get_val(priv
, &ctrl
);
648 switch (ctrl
.bRequestType
& USB_TYPE_MASK
) {
649 case USB_TYPE_STANDARD
:
650 switch (ctrl
.bRequest
) {
651 case USB_REQ_CLEAR_FEATURE
:
652 recip_handler
= &req_clear_feature
;
658 * setup stage / run recip
661 ret
= usbhsg_recip_run_handle(priv
, recip_handler
, &ctrl
);
663 ret
= gpriv
->driver
->setup(&gpriv
->gadget
, &ctrl
);
666 usbhs_fifo_stall(pipe
);
671 static int usbhsg_irq_empty(struct usbhs_priv
*priv
,
672 struct usbhs_irq_state
*irq_state
)
674 struct usbhsg_gpriv
*gpriv
= usbhsg_priv_to_gpriv(priv
);
675 struct usbhsg_uep
*uep
;
676 struct usbhs_pipe
*pipe
;
677 struct device
*dev
= usbhsg_gpriv_to_dev(gpriv
);
680 if (!irq_state
->bempsts
) {
681 dev_err(dev
, "debug %s !!\n", __func__
);
685 dev_dbg(dev
, "irq empty [0x%04x]\n", irq_state
->bempsts
);
688 * search interrupted "pipe"
691 usbhs_for_each_pipe_with_dcp(pipe
, priv
, i
) {
692 if (!(irq_state
->bempsts
& (1 << i
)))
695 uep
= usbhsg_pipe_to_uep(pipe
);
696 ret
= usbhsg_queue_handle(uep
);
698 dev_err(dev
, "send error %d : %d\n", i
, ret
);
704 static int usbhsg_irq_ready(struct usbhs_priv
*priv
,
705 struct usbhs_irq_state
*irq_state
)
707 struct usbhsg_gpriv
*gpriv
= usbhsg_priv_to_gpriv(priv
);
708 struct usbhsg_uep
*uep
;
709 struct usbhs_pipe
*pipe
;
710 struct device
*dev
= usbhsg_gpriv_to_dev(gpriv
);
713 if (!irq_state
->brdysts
) {
714 dev_err(dev
, "debug %s !!\n", __func__
);
718 dev_dbg(dev
, "irq ready [0x%04x]\n", irq_state
->brdysts
);
721 * search interrupted "pipe"
724 usbhs_for_each_pipe_with_dcp(pipe
, priv
, i
) {
725 if (!(irq_state
->brdysts
& (1 << i
)))
728 uep
= usbhsg_pipe_to_uep(pipe
);
729 ret
= usbhsg_queue_handle(uep
);
731 dev_err(dev
, "receive error %d : %d\n", i
, ret
);
742 static int usbhsg_dcp_enable(struct usbhsg_uep
*uep
)
744 struct usbhsg_gpriv
*gpriv
= usbhsg_uep_to_gpriv(uep
);
745 struct usbhs_priv
*priv
= usbhsg_gpriv_to_priv(gpriv
);
746 struct usbhs_pipe
*pipe
;
749 ********* assume under spin lock *********
752 pipe
= usbhs_dcp_malloc(priv
);
757 uep
->pipe
->mod_private
= uep
;
758 INIT_LIST_HEAD(&uep
->list
);
763 #define usbhsg_dcp_disable usbhsg_pipe_disable
764 static int usbhsg_pipe_disable(struct usbhsg_uep
*uep
)
766 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(uep
);
767 struct usbhsg_request
*ureq
;
771 ********* assume under spin lock *********
774 usbhs_fifo_disable(pipe
);
779 usbhsg_irq_empty_ctrl(uep
, disable
);
780 usbhsg_irq_ready_ctrl(uep
, disable
);
783 ureq
= usbhsg_queue_get(uep
);
787 usbhsg_queue_pop(uep
, ureq
, -ECONNRESET
);
790 uep
->pipe
->mod_private
= NULL
;
801 static int usbhsg_ep_enable(struct usb_ep
*ep
,
802 const struct usb_endpoint_descriptor
*desc
)
804 struct usbhsg_uep
*uep
= usbhsg_ep_to_uep(ep
);
805 struct usbhsg_gpriv
*gpriv
= usbhsg_uep_to_gpriv(uep
);
806 struct usbhs_priv
*priv
= usbhsg_gpriv_to_priv(gpriv
);
807 struct usbhs_pipe
*pipe
;
808 spinlock_t
*lock
= usbhsg_gpriv_to_lock(gpriv
);
812 /******************** spin lock ********************/
813 spin_lock_irqsave(lock
, flags
);
815 pipe
= usbhs_pipe_malloc(priv
, desc
);
818 pipe
->mod_private
= uep
;
819 INIT_LIST_HEAD(&uep
->list
);
821 if (usb_endpoint_dir_in(desc
))
822 uep
->handler
= &usbhsg_handler_send_packet
;
824 uep
->handler
= &usbhsg_handler_recv_packet
;
828 spin_unlock_irqrestore(lock
, flags
);
829 /******************** spin unlock ******************/
834 static int usbhsg_ep_disable(struct usb_ep
*ep
)
836 struct usbhsg_uep
*uep
= usbhsg_ep_to_uep(ep
);
837 struct usbhsg_gpriv
*gpriv
= usbhsg_uep_to_gpriv(uep
);
838 spinlock_t
*lock
= usbhsg_gpriv_to_lock(gpriv
);
842 /******************** spin lock ********************/
843 spin_lock_irqsave(lock
, flags
);
844 ret
= usbhsg_pipe_disable(uep
);
845 spin_unlock_irqrestore(lock
, flags
);
846 /******************** spin unlock ******************/
851 static struct usb_request
*usbhsg_ep_alloc_request(struct usb_ep
*ep
,
854 struct usbhsg_request
*ureq
;
856 ureq
= kzalloc(sizeof *ureq
, gfp_flags
);
860 INIT_LIST_HEAD(&ureq
->node
);
864 static void usbhsg_ep_free_request(struct usb_ep
*ep
,
865 struct usb_request
*req
)
867 struct usbhsg_request
*ureq
= usbhsg_req_to_ureq(req
);
869 WARN_ON(!list_empty(&ureq
->node
));
873 static int usbhsg_ep_queue(struct usb_ep
*ep
, struct usb_request
*req
,
876 struct usbhsg_uep
*uep
= usbhsg_ep_to_uep(ep
);
877 struct usbhsg_gpriv
*gpriv
= usbhsg_uep_to_gpriv(uep
);
878 struct usbhsg_request
*ureq
= usbhsg_req_to_ureq(req
);
879 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(uep
);
880 spinlock_t
*lock
= usbhsg_gpriv_to_lock(gpriv
);
886 * CAUTION [*endpoint queue*]
888 * This function will be called from usb_request :: complete
889 * or usb driver timing.
890 * If this function is called from usb_request :: complete,
891 * it is already under spinlock on this driver.
892 * but it is called frm usb driver, this function should call spinlock.
894 * This function is using spin_trylock_irqsave to solve this issue.
895 * if "is_locked" is 1, this mean this function lock it.
896 * but if it is 0, this mean it is already under spin lock.
898 * CAUTION [*queue handler*]
899 * CAUTION [*request complete*]
902 /******************** spin lock ********************/
903 is_locked
= spin_trylock_irqsave(lock
, flags
);
906 if (usbhsg_is_not_connected(gpriv
) ||
907 unlikely(!gpriv
->driver
) ||
911 usbhsg_queue_push(uep
, ureq
);
914 spin_unlock_irqrestore(lock
, flags
);
915 /******************** spin unlock ******************/
917 usbhsg_queue_prepare(uep
);
922 static int usbhsg_ep_dequeue(struct usb_ep
*ep
, struct usb_request
*req
)
924 struct usbhsg_uep
*uep
= usbhsg_ep_to_uep(ep
);
925 struct usbhsg_request
*ureq
= usbhsg_req_to_ureq(req
);
926 struct usbhsg_gpriv
*gpriv
= usbhsg_uep_to_gpriv(uep
);
927 spinlock_t
*lock
= usbhsg_gpriv_to_lock(gpriv
);
933 * CAUTION [*queue handler*]
934 * CAUTION [*endpoint queue*]
935 * CAUTION [*request complete*]
938 /******************** spin lock ********************/
939 is_locked
= spin_trylock_irqsave(lock
, flags
);
941 usbhsg_queue_pop(uep
, ureq
, -ECONNRESET
);
944 spin_unlock_irqrestore(lock
, flags
);
945 /******************** spin unlock ******************/
950 static int __usbhsg_ep_set_halt_wedge(struct usb_ep
*ep
, int halt
, int wedge
)
952 struct usbhsg_uep
*uep
= usbhsg_ep_to_uep(ep
);
953 struct usbhs_pipe
*pipe
= usbhsg_uep_to_pipe(uep
);
954 struct usbhsg_gpriv
*gpriv
= usbhsg_uep_to_gpriv(uep
);
955 struct device
*dev
= usbhsg_gpriv_to_dev(gpriv
);
956 spinlock_t
*lock
= usbhsg_gpriv_to_lock(gpriv
);
963 * CAUTION [*queue handler*]
964 * CAUTION [*endpoint queue*]
965 * CAUTION [*request complete*]
968 /******************** spin lock ********************/
969 is_locked
= spin_trylock_irqsave(lock
, flags
);
970 if (!usbhsg_queue_get(uep
)) {
972 dev_dbg(dev
, "set halt %d (pipe %d)\n",
973 halt
, usbhs_pipe_number(pipe
));
976 usbhs_fifo_stall(pipe
);
978 usbhs_fifo_disable(pipe
);
981 usbhsg_status_set(gpriv
, USBHSG_STATUS_WEDGE
);
983 usbhsg_status_clr(gpriv
, USBHSG_STATUS_WEDGE
);
989 spin_unlock_irqrestore(lock
, flags
);
990 /******************** spin unlock ******************/
995 static int usbhsg_ep_set_halt(struct usb_ep
*ep
, int value
)
997 return __usbhsg_ep_set_halt_wedge(ep
, value
, 0);
1000 static int usbhsg_ep_set_wedge(struct usb_ep
*ep
)
1002 return __usbhsg_ep_set_halt_wedge(ep
, 1, 1);
1005 static struct usb_ep_ops usbhsg_ep_ops
= {
1006 .enable
= usbhsg_ep_enable
,
1007 .disable
= usbhsg_ep_disable
,
1009 .alloc_request
= usbhsg_ep_alloc_request
,
1010 .free_request
= usbhsg_ep_free_request
,
1012 .queue
= usbhsg_ep_queue
,
1013 .dequeue
= usbhsg_ep_dequeue
,
1015 .set_halt
= usbhsg_ep_set_halt
,
1016 .set_wedge
= usbhsg_ep_set_wedge
,
1020 * usb module start/end
1022 static int usbhsg_try_start(struct usbhs_priv
*priv
, u32 status
)
1024 struct usbhsg_gpriv
*gpriv
= usbhsg_priv_to_gpriv(priv
);
1025 struct usbhsg_uep
*dcp
= usbhsg_gpriv_to_dcp(gpriv
);
1026 struct usbhs_mod
*mod
= usbhs_mod_get_current(priv
);
1027 struct device
*dev
= usbhs_priv_to_dev(priv
);
1028 spinlock_t
*lock
= usbhsg_gpriv_to_lock(gpriv
);
1029 unsigned long flags
;
1031 /******************** spin lock ********************/
1032 spin_lock_irqsave(lock
, flags
);
1035 * enable interrupt and systems if ready
1037 usbhsg_status_set(gpriv
, status
);
1038 if (!(usbhsg_status_has(gpriv
, USBHSG_STATUS_STARTED
) &&
1039 usbhsg_status_has(gpriv
, USBHSG_STATUS_REGISTERD
)))
1040 goto usbhsg_try_start_unlock
;
1042 dev_dbg(dev
, "start gadget\n");
1045 * pipe initialize and enable DCP
1047 usbhs_pipe_init(priv
);
1048 usbhsg_dcp_enable(dcp
);
1051 * system config enble
1056 usbhs_sys_hispeed_ctrl(priv
, 1);
1057 usbhs_sys_function_ctrl(priv
, 1);
1058 usbhs_sys_usb_ctrl(priv
, 1);
1061 * enable irq callback
1063 mod
->irq_dev_state
= usbhsg_irq_dev_state
;
1064 mod
->irq_ctrl_stage
= usbhsg_irq_ctrl_stage
;
1065 mod
->irq_empty
= usbhsg_irq_empty
;
1066 mod
->irq_ready
= usbhsg_irq_ready
;
1067 mod
->irq_bempsts
= 0;
1068 mod
->irq_brdysts
= 0;
1069 usbhs_irq_callback_update(priv
, mod
);
1071 usbhsg_try_start_unlock
:
1072 spin_unlock_irqrestore(lock
, flags
);
1073 /******************** spin unlock ********************/
1078 static int usbhsg_try_stop(struct usbhs_priv
*priv
, u32 status
)
1080 struct usbhsg_gpriv
*gpriv
= usbhsg_priv_to_gpriv(priv
);
1081 struct usbhs_mod
*mod
= usbhs_mod_get_current(priv
);
1082 struct usbhsg_uep
*dcp
= usbhsg_gpriv_to_dcp(gpriv
);
1083 struct device
*dev
= usbhs_priv_to_dev(priv
);
1084 spinlock_t
*lock
= usbhsg_gpriv_to_lock(gpriv
);
1085 unsigned long flags
;
1087 /******************** spin lock ********************/
1088 spin_lock_irqsave(lock
, flags
);
1091 * disable interrupt and systems if 1st try
1093 usbhsg_status_clr(gpriv
, status
);
1094 if (!usbhsg_status_has(gpriv
, USBHSG_STATUS_STARTED
) &&
1095 !usbhsg_status_has(gpriv
, USBHSG_STATUS_REGISTERD
))
1096 goto usbhsg_try_stop_unlock
;
1098 /* disable all irq */
1099 mod
->irq_dev_state
= NULL
;
1100 mod
->irq_ctrl_stage
= NULL
;
1101 mod
->irq_empty
= NULL
;
1102 mod
->irq_ready
= NULL
;
1103 mod
->irq_bempsts
= 0;
1104 mod
->irq_brdysts
= 0;
1105 usbhs_irq_callback_update(priv
, mod
);
1107 usbhsg_dcp_disable(dcp
);
1109 gpriv
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1112 usbhs_sys_hispeed_ctrl(priv
, 0);
1113 usbhs_sys_function_ctrl(priv
, 0);
1114 usbhs_sys_usb_ctrl(priv
, 0);
1116 spin_unlock_irqrestore(lock
, flags
);
1117 /******************** spin unlock ********************/
1119 if (gpriv
->driver
&&
1120 gpriv
->driver
->disconnect
)
1121 gpriv
->driver
->disconnect(&gpriv
->gadget
);
1123 dev_dbg(dev
, "stop gadget\n");
1127 usbhsg_try_stop_unlock
:
1128 spin_unlock_irqrestore(lock
, flags
);
1135 * linux usb function
1138 struct usbhsg_gpriv
*the_controller
;
1139 int usb_gadget_probe_driver(struct usb_gadget_driver
*driver
,
1140 int (*bind
)(struct usb_gadget
*))
1142 struct usbhsg_gpriv
*gpriv
= the_controller
;
1143 struct usbhs_priv
*priv
;
1150 driver
->speed
!= USB_SPEED_HIGH
)
1157 dev
= usbhsg_gpriv_to_dev(gpriv
);
1158 priv
= usbhsg_gpriv_to_priv(gpriv
);
1160 /* first hook up the driver ... */
1161 gpriv
->driver
= driver
;
1162 gpriv
->gadget
.dev
.driver
= &driver
->driver
;
1164 ret
= device_add(&gpriv
->gadget
.dev
);
1166 dev_err(dev
, "device_add error %d\n", ret
);
1170 ret
= bind(&gpriv
->gadget
);
1172 dev_err(dev
, "bind to driver %s error %d\n",
1173 driver
->driver
.name
, ret
);
1177 dev_dbg(dev
, "bind %s\n", driver
->driver
.name
);
1179 return usbhsg_try_start(priv
, USBHSG_STATUS_REGISTERD
);
1182 device_del(&gpriv
->gadget
.dev
);
1184 gpriv
->driver
= NULL
;
1185 gpriv
->gadget
.dev
.driver
= NULL
;
1189 EXPORT_SYMBOL(usb_gadget_probe_driver
);
1191 int usb_gadget_unregister_driver(struct usb_gadget_driver
*driver
)
1193 struct usbhsg_gpriv
*gpriv
= the_controller
;
1194 struct usbhs_priv
*priv
;
1195 struct device
*dev
= usbhsg_gpriv_to_dev(gpriv
);
1202 driver
!= gpriv
->driver
)
1205 dev
= usbhsg_gpriv_to_dev(gpriv
);
1206 priv
= usbhsg_gpriv_to_priv(gpriv
);
1208 usbhsg_try_stop(priv
, USBHSG_STATUS_REGISTERD
);
1209 device_del(&gpriv
->gadget
.dev
);
1210 gpriv
->driver
= NULL
;
1212 if (driver
->disconnect
)
1213 driver
->disconnect(&gpriv
->gadget
);
1215 driver
->unbind(&gpriv
->gadget
);
1216 dev_dbg(dev
, "unbind %s\n", driver
->driver
.name
);
1220 EXPORT_SYMBOL(usb_gadget_unregister_driver
);
1225 static int usbhsg_get_frame(struct usb_gadget
*gadget
)
1227 struct usbhsg_gpriv
*gpriv
= usbhsg_gadget_to_gpriv(gadget
);
1228 struct usbhs_priv
*priv
= usbhsg_gpriv_to_priv(gpriv
);
1230 return usbhs_frame_get_num(priv
);
1233 static struct usb_gadget_ops usbhsg_gadget_ops
= {
1234 .get_frame
= usbhsg_get_frame
,
1237 static int usbhsg_start(struct usbhs_priv
*priv
)
1239 return usbhsg_try_start(priv
, USBHSG_STATUS_STARTED
);
1242 static int usbhsg_stop(struct usbhs_priv
*priv
)
1244 return usbhsg_try_stop(priv
, USBHSG_STATUS_STARTED
);
1247 int __devinit
usbhs_mod_gadget_probe(struct usbhs_priv
*priv
)
1249 struct usbhsg_gpriv
*gpriv
;
1250 struct usbhsg_uep
*uep
;
1251 struct device
*dev
= usbhs_priv_to_dev(priv
);
1252 int pipe_size
= usbhs_get_dparam(priv
, pipe_size
);
1255 gpriv
= kzalloc(sizeof(struct usbhsg_gpriv
), GFP_KERNEL
);
1257 dev_err(dev
, "Could not allocate gadget priv\n");
1261 uep
= kzalloc(sizeof(struct usbhsg_uep
) * pipe_size
, GFP_KERNEL
);
1263 dev_err(dev
, "Could not allocate ep\n");
1264 goto usbhs_mod_gadget_probe_err_gpriv
;
1270 * There is no guarantee that it is possible to access usb module here.
1271 * Don't accesses to it.
1272 * The accesse will be enable after "usbhsg_start"
1278 usbhs_mod_register(priv
, &gpriv
->mod
, USBHS_GADGET
);
1281 gpriv
->mod
.name
= "gadget";
1282 gpriv
->mod
.start
= usbhsg_start
;
1283 gpriv
->mod
.stop
= usbhsg_stop
;
1285 gpriv
->uep_size
= pipe_size
;
1286 usbhsg_status_init(gpriv
);
1291 device_initialize(&gpriv
->gadget
.dev
);
1292 dev_set_name(&gpriv
->gadget
.dev
, "gadget");
1293 gpriv
->gadget
.dev
.parent
= dev
;
1294 gpriv
->gadget
.name
= "renesas_usbhs_udc";
1295 gpriv
->gadget
.ops
= &usbhsg_gadget_ops
;
1296 gpriv
->gadget
.is_dualspeed
= 1;
1298 INIT_LIST_HEAD(&gpriv
->gadget
.ep_list
);
1303 usbhsg_for_each_uep_with_dcp(uep
, gpriv
, i
) {
1305 snprintf(uep
->ep_name
, EP_NAME_SIZE
, "ep%d", i
);
1307 uep
->ep
.name
= uep
->ep_name
;
1308 uep
->ep
.ops
= &usbhsg_ep_ops
;
1309 INIT_LIST_HEAD(&uep
->ep
.ep_list
);
1310 INIT_LIST_HEAD(&uep
->list
);
1313 if (usbhsg_is_dcp(uep
)) {
1314 gpriv
->gadget
.ep0
= &uep
->ep
;
1315 uep
->ep
.maxpacket
= 64;
1317 /* init normal pipe */
1319 uep
->ep
.maxpacket
= 512;
1320 list_add_tail(&uep
->ep
.ep_list
, &gpriv
->gadget
.ep_list
);
1324 the_controller
= gpriv
;
1326 dev_info(dev
, "gadget probed\n");
1330 usbhs_mod_gadget_probe_err_gpriv
:
1336 void __devexit
usbhs_mod_gadget_remove(struct usbhs_priv
*priv
)
1338 struct usbhsg_gpriv
*gpriv
= usbhsg_priv_to_gpriv(priv
);