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 * This driver don't use spin_try_lock
125 * to avoid warning of CONFIG_DEBUG_SPINLOCK
127 static spinlock_t *usbhsg_trylock(struct usbhsg_gpriv *gpriv,
128 unsigned long *flags)
130 spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
132 /* check spin lock status
133 * to avoid deadlock/nest */
134 if (spin_is_locked(lock))
137 spin_lock_irqsave(lock, *flags);
142 static void usbhsg_unlock(spinlock_t *lock, unsigned long *flags)
147 spin_unlock_irqrestore(lock, *flags);
153 static void usbhsg_queue_push(struct usbhsg_uep *uep,
154 struct usbhsg_request *ureq)
156 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
157 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
158 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
161 ********* assume under spin lock *********
163 list_del_init(&ureq->node);
164 list_add_tail(&ureq->node, &uep->list);
165 ureq->req.actual = 0;
166 ureq->req.status = -EINPROGRESS;
168 dev_dbg(dev, "pipe %d : queue push (%d)\n",
169 usbhs_pipe_number(pipe),
173 static struct usbhsg_request *usbhsg_queue_get(struct usbhsg_uep *uep)
176 ********* assume under spin lock *********
178 if (list_empty(&uep->list))
181 return list_entry(uep->list.next, struct usbhsg_request, node);
184 #define usbhsg_queue_prepare(uep) __usbhsg_queue_handler(uep, 1);
185 #define usbhsg_queue_handle(uep) __usbhsg_queue_handler(uep, 0);
186 static int __usbhsg_queue_handler(struct usbhsg_uep *uep, int prepare)
188 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
189 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
190 struct usbhsg_request *ureq;
196 dev_err(dev, "no handler function\n");
201 * CAUTION [*queue handler*]
203 * This function will be called for start/restart queue operation.
204 * OTOH the most much worry for USB driver is spinlock nest.
206 * - usb_ep_ops :: queue
207 * - usb_request :: complete
209 * But the caller of this function need not care about spinlock.
210 * This function is using usbhsg_trylock for it.
211 * if "is_locked" is 1, this mean this function lock it.
212 * but if it is 0, this mean it is already under spin lock.
214 * CAUTION [*endpoint queue*]
215 * CAUTION [*request complete*]
218 /****************** spin try lock *******************/
219 lock = usbhsg_trylock(gpriv, &flags);
221 ureq = usbhsg_queue_get(uep);
224 ret = uep->handler->prepare(uep, ureq);
226 ret = uep->handler->try_run(uep, ureq);
228 usbhsg_unlock(lock, &flags);
229 /******************** spin unlock ******************/
234 static void usbhsg_queue_pop(struct usbhsg_uep *uep,
235 struct usbhsg_request *ureq,
238 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
239 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
240 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
243 ********* assume under spin lock *********
247 * CAUTION [*request complete*]
249 * There is a possibility not to be called in correct order
250 * if "complete" is called without spinlock.
252 * So, this function assume it is under spinlock,
253 * and call usb_request :: complete.
255 * But this "complete" will push next usb_request.
256 * It mean "usb_ep_ops :: queue" which is using spinlock is called
259 * To avoid dead-lock, this driver is using usbhsg_trylock.
260 * CAUTION [*endpoint queue*]
261 * CAUTION [*queue handler*]
264 dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
266 list_del_init(&ureq->node);
268 ureq->req.status = status;
269 ureq->req.complete(&uep->ep, &ureq->req);
273 usbhsg_queue_prepare(uep);
277 * irq enable/disable function
279 #define usbhsg_irq_callback_ctrl(uep, status, enable) \
281 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); \
282 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); \
283 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); \
284 struct usbhs_mod *mod = usbhs_mod_get_current(priv); \
288 mod->irq_##status |= (1 << usbhs_pipe_number(pipe)); \
290 mod->irq_##status &= ~(1 << usbhs_pipe_number(pipe)); \
291 usbhs_irq_callback_update(priv, mod); \
294 static void usbhsg_irq_empty_ctrl(struct usbhsg_uep *uep, int enable)
296 usbhsg_irq_callback_ctrl(uep, bempsts, enable);
299 static void usbhsg_irq_ready_ctrl(struct usbhsg_uep *uep, int enable)
301 usbhsg_irq_callback_ctrl(uep, brdysts, enable);
307 static int usbhsg_try_run_ctrl_stage_end(struct usbhsg_uep *uep,
308 struct usbhsg_request *ureq)
310 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
313 ********* assume under spin lock *********
316 usbhs_dcp_control_transfer_done(pipe);
317 usbhsg_queue_pop(uep, ureq, 0);
322 static int usbhsg_try_run_send_packet(struct usbhsg_uep *uep,
323 struct usbhsg_request *ureq)
325 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
326 struct usb_request *req = &ureq->req;
327 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
328 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
336 ********* assume under spin lock *********
339 maxp = usbhs_pipe_get_maxpacket(pipe);
340 buf = req->buf + req->actual;
341 remainder = req->length - req->actual;
343 send = usbhs_fifo_write(pipe, buf, remainder);
346 * send < 0 : pipe busy
347 * send = 0 : send zero packet
348 * send > 0 : send data
355 /* send all packet ? */
356 if (send < remainder)
357 is_done = 0; /* there are remainder data */
358 else if (send < maxp)
359 is_done = 1; /* short packet */
361 is_done = !req->zero; /* send zero packet ? */
363 dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n",
364 usbhs_pipe_number(pipe),
365 remainder, send, is_done, req->zero);
368 * enable interrupt and send again in irq handler
369 * if it still have remainder data which should be sent.
372 uep->handler->irq_mask(uep, enable);
375 * usbhs_fifo_enable execute
376 * - after callback_update,
377 * - before queue_pop / stage_end
379 usbhs_fifo_enable(pipe);
382 * all data were sent ?
385 /* it care below call in
387 if (usbhsg_is_dcp(uep))
388 usbhs_dcp_control_transfer_done(pipe);
390 usbhsg_queue_pop(uep, ureq, 0);
396 static int usbhsg_prepare_send_packet(struct usbhsg_uep *uep,
397 struct usbhsg_request *ureq)
399 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
402 ********* assume under spin lock *********
405 usbhs_fifo_prepare_write(pipe);
406 usbhsg_try_run_send_packet(uep, ureq);
411 static int usbhsg_try_run_receive_packet(struct usbhsg_uep *uep,
412 struct usbhsg_request *ureq)
414 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
415 struct usb_request *req = &ureq->req;
416 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
417 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
424 ********* assume under spin lock *********
427 maxp = usbhs_pipe_get_maxpacket(pipe);
428 buf = req->buf + req->actual;
429 remainder = req->length - req->actual;
431 recv = usbhs_fifo_read(pipe, buf, remainder);
433 * recv < 0 : pipe busy
434 * recv >= 0 : receive data
441 /* update parameters */
444 if ((recv == remainder) || /* receive all data */
445 (recv < maxp)) /* short packet */
448 dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n",
449 usbhs_pipe_number(pipe),
450 remainder, recv, is_done, req->zero);
452 /* read all data ? */
456 uep->handler->irq_mask(uep, disable);
457 usbhs_fifo_disable(pipe);
458 usbhsg_queue_pop(uep, ureq, 0);
464 static int usbhsg_prepare_receive_packet(struct usbhsg_uep *uep,
465 struct usbhsg_request *ureq)
467 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
472 ********* assume under spin lock *********
475 ret = usbhs_fifo_prepare_read(pipe);
480 * data will be read in interrupt handler
482 uep->handler->irq_mask(uep, enable);
487 static struct usbhsg_pipe_handle usbhsg_handler_send_by_empty = {
488 .prepare = usbhsg_prepare_send_packet,
489 .try_run = usbhsg_try_run_send_packet,
490 .irq_mask = usbhsg_irq_empty_ctrl,
493 static struct usbhsg_pipe_handle usbhsg_handler_send_by_ready = {
494 .prepare = usbhsg_prepare_send_packet,
495 .try_run = usbhsg_try_run_send_packet,
496 .irq_mask = usbhsg_irq_ready_ctrl,
499 static struct usbhsg_pipe_handle usbhsg_handler_recv_by_ready = {
500 .prepare = usbhsg_prepare_receive_packet,
501 .try_run = usbhsg_try_run_receive_packet,
502 .irq_mask = usbhsg_irq_ready_ctrl,
505 static struct usbhsg_pipe_handle usbhsg_handler_ctrl_stage_end = {
506 .prepare = usbhsg_try_run_ctrl_stage_end,
507 .try_run = usbhsg_try_run_ctrl_stage_end,
511 * DCP pipe can NOT use "ready interrupt" for "send"
512 * it should use "empty" interrupt.
514 * "Operation" - "Interrupt Function" - "BRDY Interrupt"
516 * on the other hand, normal pipe can use "ready interrupt" for "send"
517 * even though it is single/double buffer
519 #define usbhsg_handler_send_ctrl usbhsg_handler_send_by_empty
520 #define usbhsg_handler_recv_ctrl usbhsg_handler_recv_by_ready
522 #define usbhsg_handler_send_packet usbhsg_handler_send_by_ready
523 #define usbhsg_handler_recv_packet usbhsg_handler_recv_by_ready
526 * USB_TYPE_STANDARD / clear feature functions
528 static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
529 struct usbhsg_uep *uep,
530 struct usb_ctrlrequest *ctrl)
532 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
533 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
534 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
536 usbhs_dcp_control_transfer_done(pipe);
541 static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
542 struct usbhsg_uep *uep,
543 struct usb_ctrlrequest *ctrl)
545 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
546 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
548 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
549 usbhs_fifo_disable(pipe);
550 usbhs_pipe_clear_sequence(pipe);
551 usbhs_fifo_enable(pipe);
554 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
556 usbhsg_queue_prepare(uep);
561 struct usbhsg_recip_handle req_clear_feature = {
562 .name = "clear feature",
563 .device = usbhsg_recip_handler_std_control_done,
564 .interface = usbhsg_recip_handler_std_control_done,
565 .endpoint = usbhsg_recip_handler_std_clear_endpoint,
571 static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
572 struct usbhsg_recip_handle *handler,
573 struct usb_ctrlrequest *ctrl)
575 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
576 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
577 struct usbhsg_uep *uep;
578 int recip = ctrl->bRequestType & USB_RECIP_MASK;
579 int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
581 int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
582 struct usb_ctrlrequest *ctrl);
585 uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
588 case USB_RECIP_DEVICE:
590 func = handler->device;
592 case USB_RECIP_INTERFACE:
594 func = handler->interface;
596 case USB_RECIP_ENDPOINT:
598 func = handler->endpoint;
601 dev_warn(dev, "unsupported RECIP(%d)\n", recip);
607 dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
608 ret = func(priv, uep, ctrl);
617 * it will be called from usbhs_interrupt
619 static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
620 struct usbhs_irq_state *irq_state)
622 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
623 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
625 gpriv->gadget.speed = usbhs_status_get_usb_speed(irq_state);
627 dev_dbg(dev, "state = %x : speed : %d\n",
628 usbhs_status_get_device_state(irq_state),
629 gpriv->gadget.speed);
634 static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
635 struct usbhs_irq_state *irq_state)
637 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
638 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
639 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
640 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
641 struct usb_ctrlrequest ctrl;
642 struct usbhsg_recip_handle *recip_handler = NULL;
643 int stage = usbhs_status_get_ctrl_stage(irq_state);
646 dev_dbg(dev, "stage = %d\n", stage);
652 * - "Interrupt Function"
653 * - "Control Transfer Stage Transition Interrupt"
654 * - Fig. "Control Transfer Stage Transitions"
658 case READ_DATA_STAGE:
659 dcp->handler = &usbhsg_handler_send_ctrl;
661 case WRITE_DATA_STAGE:
662 dcp->handler = &usbhsg_handler_recv_ctrl;
664 case NODATA_STATUS_STAGE:
665 dcp->handler = &usbhsg_handler_ctrl_stage_end;
674 usbhs_usbreq_get_val(priv, &ctrl);
676 switch (ctrl.bRequestType & USB_TYPE_MASK) {
677 case USB_TYPE_STANDARD:
678 switch (ctrl.bRequest) {
679 case USB_REQ_CLEAR_FEATURE:
680 recip_handler = &req_clear_feature;
686 * setup stage / run recip
689 ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
691 ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
694 usbhs_fifo_stall(pipe);
699 static int usbhsg_irq_empty(struct usbhs_priv *priv,
700 struct usbhs_irq_state *irq_state)
702 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
703 struct usbhsg_uep *uep;
704 struct usbhs_pipe *pipe;
705 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
708 if (!irq_state->bempsts) {
709 dev_err(dev, "debug %s !!\n", __func__);
713 dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
716 * search interrupted "pipe"
719 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
720 if (!(irq_state->bempsts & (1 << i)))
723 uep = usbhsg_pipe_to_uep(pipe);
724 ret = usbhsg_queue_handle(uep);
726 dev_err(dev, "send error %d : %d\n", i, ret);
732 static int usbhsg_irq_ready(struct usbhs_priv *priv,
733 struct usbhs_irq_state *irq_state)
735 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
736 struct usbhsg_uep *uep;
737 struct usbhs_pipe *pipe;
738 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
741 if (!irq_state->brdysts) {
742 dev_err(dev, "debug %s !!\n", __func__);
746 dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
749 * search interrupted "pipe"
752 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
753 if (!(irq_state->brdysts & (1 << i)))
756 uep = usbhsg_pipe_to_uep(pipe);
757 ret = usbhsg_queue_handle(uep);
759 dev_err(dev, "receive error %d : %d\n", i, ret);
770 static int usbhsg_dcp_enable(struct usbhsg_uep *uep)
772 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
773 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
774 struct usbhs_pipe *pipe;
777 ********* assume under spin lock *********
780 pipe = usbhs_dcp_malloc(priv);
785 uep->pipe->mod_private = uep;
786 INIT_LIST_HEAD(&uep->list);
791 #define usbhsg_dcp_disable usbhsg_pipe_disable
792 static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
794 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
795 struct usbhsg_request *ureq;
799 ********* assume under spin lock *********
802 usbhs_fifo_disable(pipe);
807 usbhsg_irq_empty_ctrl(uep, disable);
808 usbhsg_irq_ready_ctrl(uep, disable);
811 ureq = usbhsg_queue_get(uep);
815 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
821 static void usbhsg_uep_init(struct usbhsg_gpriv *gpriv)
824 struct usbhsg_uep *uep;
826 usbhsg_for_each_uep_with_dcp(uep, gpriv, i)
835 static int usbhsg_ep_enable(struct usb_ep *ep,
836 const struct usb_endpoint_descriptor *desc)
838 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
839 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
840 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
841 struct usbhs_pipe *pipe;
847 * if it already have pipe,
853 /******************** spin lock ********************/
854 lock = usbhsg_trylock(gpriv, &flags);
856 pipe = usbhs_pipe_malloc(priv, desc);
859 pipe->mod_private = uep;
860 INIT_LIST_HEAD(&uep->list);
862 if (usb_endpoint_dir_in(desc))
863 uep->handler = &usbhsg_handler_send_packet;
865 uep->handler = &usbhsg_handler_recv_packet;
870 usbhsg_unlock(lock, &flags);
871 /******************** spin unlock ******************/
876 static int usbhsg_ep_disable(struct usb_ep *ep)
878 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
879 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
884 /******************** spin lock ********************/
885 lock = usbhsg_trylock(gpriv, &flags);
887 ret = usbhsg_pipe_disable(uep);
889 usbhsg_unlock(lock, &flags);
890 /******************** spin unlock ******************/
895 static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
898 struct usbhsg_request *ureq;
900 ureq = kzalloc(sizeof *ureq, gfp_flags);
904 INIT_LIST_HEAD(&ureq->node);
908 static void usbhsg_ep_free_request(struct usb_ep *ep,
909 struct usb_request *req)
911 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
913 WARN_ON(!list_empty(&ureq->node));
917 static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
920 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
921 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
922 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
923 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
929 * CAUTION [*endpoint queue*]
931 * This function will be called from usb_request :: complete
932 * or usb driver timing.
933 * If this function is called from usb_request :: complete,
934 * it is already under spinlock on this driver.
935 * but it is called frm usb driver, this function should call spinlock.
937 * This function is using usbshg_trylock to solve this issue.
938 * if "is_locked" is 1, this mean this function lock it.
939 * but if it is 0, this mean it is already under spin lock.
941 * CAUTION [*queue handler*]
942 * CAUTION [*request complete*]
945 /******************** spin lock ********************/
946 lock = usbhsg_trylock(gpriv, &flags);
949 if (usbhsg_is_not_connected(gpriv) ||
950 unlikely(!gpriv->driver) ||
954 usbhsg_queue_push(uep, ureq);
956 usbhsg_unlock(lock, &flags);
957 /******************** spin unlock ******************/
959 usbhsg_queue_prepare(uep);
964 static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
966 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
967 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
968 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
974 * CAUTION [*queue handler*]
975 * CAUTION [*endpoint queue*]
976 * CAUTION [*request complete*]
979 /******************** spin lock ********************/
980 lock = usbhsg_trylock(gpriv, &flags);
982 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
984 usbhsg_unlock(lock, &flags);
985 /******************** spin unlock ******************/
990 static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
992 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
993 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
994 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
995 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
1002 * CAUTION [*queue handler*]
1003 * CAUTION [*endpoint queue*]
1004 * CAUTION [*request complete*]
1007 /******************** spin lock ********************/
1008 lock = usbhsg_trylock(gpriv, &flags);
1009 if (!usbhsg_queue_get(uep)) {
1011 dev_dbg(dev, "set halt %d (pipe %d)\n",
1012 halt, usbhs_pipe_number(pipe));
1015 usbhs_fifo_stall(pipe);
1017 usbhs_fifo_disable(pipe);
1020 usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
1022 usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
1027 usbhsg_unlock(lock, &flags);
1028 /******************** spin unlock ******************/
1033 static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
1035 return __usbhsg_ep_set_halt_wedge(ep, value, 0);
1038 static int usbhsg_ep_set_wedge(struct usb_ep *ep)
1040 return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
1043 static struct usb_ep_ops usbhsg_ep_ops = {
1044 .enable = usbhsg_ep_enable,
1045 .disable = usbhsg_ep_disable,
1047 .alloc_request = usbhsg_ep_alloc_request,
1048 .free_request = usbhsg_ep_free_request,
1050 .queue = usbhsg_ep_queue,
1051 .dequeue = usbhsg_ep_dequeue,
1053 .set_halt = usbhsg_ep_set_halt,
1054 .set_wedge = usbhsg_ep_set_wedge,
1058 * usb module start/end
1060 static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
1062 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1063 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
1064 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1065 struct device *dev = usbhs_priv_to_dev(priv);
1067 unsigned long flags;
1069 /******************** spin lock ********************/
1070 lock = usbhsg_trylock(gpriv, &flags);
1073 * enable interrupt and systems if ready
1075 usbhsg_status_set(gpriv, status);
1076 if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
1077 usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
1078 goto usbhsg_try_start_unlock;
1080 dev_dbg(dev, "start gadget\n");
1083 * pipe initialize and enable DCP
1085 usbhs_pipe_init(priv);
1086 usbhsg_uep_init(gpriv);
1087 usbhsg_dcp_enable(dcp);
1090 * system config enble
1095 usbhs_sys_hispeed_ctrl(priv, 1);
1096 usbhs_sys_function_ctrl(priv, 1);
1097 usbhs_sys_usb_ctrl(priv, 1);
1100 * enable irq callback
1102 mod->irq_dev_state = usbhsg_irq_dev_state;
1103 mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
1104 mod->irq_empty = usbhsg_irq_empty;
1105 mod->irq_ready = usbhsg_irq_ready;
1106 mod->irq_bempsts = 0;
1107 mod->irq_brdysts = 0;
1108 usbhs_irq_callback_update(priv, mod);
1110 usbhsg_try_start_unlock:
1111 usbhsg_unlock(lock, &flags);
1112 /******************** spin unlock ********************/
1117 static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
1119 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1120 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1121 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
1122 struct device *dev = usbhs_priv_to_dev(priv);
1124 unsigned long flags;
1126 /******************** spin lock ********************/
1127 lock = usbhsg_trylock(gpriv, &flags);
1130 * disable interrupt and systems if 1st try
1132 usbhsg_status_clr(gpriv, status);
1133 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
1134 !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
1135 goto usbhsg_try_stop_unlock;
1137 /* disable all irq */
1138 mod->irq_dev_state = NULL;
1139 mod->irq_ctrl_stage = NULL;
1140 mod->irq_empty = NULL;
1141 mod->irq_ready = NULL;
1142 mod->irq_bempsts = 0;
1143 mod->irq_brdysts = 0;
1144 usbhs_irq_callback_update(priv, mod);
1146 usbhsg_dcp_disable(dcp);
1148 gpriv->gadget.speed = USB_SPEED_UNKNOWN;
1151 usbhs_sys_hispeed_ctrl(priv, 0);
1152 usbhs_sys_function_ctrl(priv, 0);
1153 usbhs_sys_usb_ctrl(priv, 0);
1155 usbhsg_unlock(lock, &flags);
1156 /******************** spin unlock ********************/
1158 if (gpriv->driver &&
1159 gpriv->driver->disconnect)
1160 gpriv->driver->disconnect(&gpriv->gadget);
1162 dev_dbg(dev, "stop gadget\n");
1166 usbhsg_try_stop_unlock:
1167 usbhsg_unlock(lock, &flags);
1174 * linux usb function
1177 struct usbhsg_gpriv *the_controller;
1178 int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1179 int (*bind)(struct usb_gadget *))
1181 struct usbhsg_gpriv *gpriv = the_controller;
1182 struct usbhs_priv *priv;
1189 driver->speed != USB_SPEED_HIGH)
1196 dev = usbhsg_gpriv_to_dev(gpriv);
1197 priv = usbhsg_gpriv_to_priv(gpriv);
1199 /* first hook up the driver ... */
1200 gpriv->driver = driver;
1201 gpriv->gadget.dev.driver = &driver->driver;
1203 ret = device_add(&gpriv->gadget.dev);
1205 dev_err(dev, "device_add error %d\n", ret);
1209 ret = bind(&gpriv->gadget);
1211 dev_err(dev, "bind to driver %s error %d\n",
1212 driver->driver.name, ret);
1216 dev_dbg(dev, "bind %s\n", driver->driver.name);
1218 return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
1221 device_del(&gpriv->gadget.dev);
1223 gpriv->driver = NULL;
1224 gpriv->gadget.dev.driver = NULL;
1228 EXPORT_SYMBOL(usb_gadget_probe_driver);
1230 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1232 struct usbhsg_gpriv *gpriv = the_controller;
1233 struct usbhs_priv *priv;
1234 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
1241 driver != gpriv->driver)
1244 dev = usbhsg_gpriv_to_dev(gpriv);
1245 priv = usbhsg_gpriv_to_priv(gpriv);
1247 usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
1248 device_del(&gpriv->gadget.dev);
1249 gpriv->driver = NULL;
1251 if (driver->disconnect)
1252 driver->disconnect(&gpriv->gadget);
1254 driver->unbind(&gpriv->gadget);
1255 dev_dbg(dev, "unbind %s\n", driver->driver.name);
1259 EXPORT_SYMBOL(usb_gadget_unregister_driver);
1264 static int usbhsg_get_frame(struct usb_gadget *gadget)
1266 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
1267 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
1269 return usbhs_frame_get_num(priv);
1272 static struct usb_gadget_ops usbhsg_gadget_ops = {
1273 .get_frame = usbhsg_get_frame,
1276 static int usbhsg_start(struct usbhs_priv *priv)
1278 return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
1281 static int usbhsg_stop(struct usbhs_priv *priv)
1283 return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
1286 int __devinit usbhs_mod_gadget_probe(struct usbhs_priv *priv)
1288 struct usbhsg_gpriv *gpriv;
1289 struct usbhsg_uep *uep;
1290 struct device *dev = usbhs_priv_to_dev(priv);
1291 int pipe_size = usbhs_get_dparam(priv, pipe_size);
1294 gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
1296 dev_err(dev, "Could not allocate gadget priv\n");
1300 uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
1302 dev_err(dev, "Could not allocate ep\n");
1303 goto usbhs_mod_gadget_probe_err_gpriv;
1309 * There is no guarantee that it is possible to access usb module here.
1310 * Don't accesses to it.
1311 * The accesse will be enable after "usbhsg_start"
1317 usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
1320 gpriv->mod.name = "gadget";
1321 gpriv->mod.start = usbhsg_start;
1322 gpriv->mod.stop = usbhsg_stop;
1324 gpriv->uep_size = pipe_size;
1325 usbhsg_status_init(gpriv);
1330 device_initialize(&gpriv->gadget.dev);
1331 dev_set_name(&gpriv->gadget.dev, "gadget");
1332 gpriv->gadget.dev.parent = dev;
1333 gpriv->gadget.name = "renesas_usbhs_udc";
1334 gpriv->gadget.ops = &usbhsg_gadget_ops;
1335 gpriv->gadget.is_dualspeed = 1;
1337 INIT_LIST_HEAD(&gpriv->gadget.ep_list);
1342 usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
1344 snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
1346 uep->ep.name = uep->ep_name;
1347 uep->ep.ops = &usbhsg_ep_ops;
1348 INIT_LIST_HEAD(&uep->ep.ep_list);
1349 INIT_LIST_HEAD(&uep->list);
1352 if (usbhsg_is_dcp(uep)) {
1353 gpriv->gadget.ep0 = &uep->ep;
1354 uep->ep.maxpacket = 64;
1356 /* init normal pipe */
1358 uep->ep.maxpacket = 512;
1359 list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
1363 the_controller = gpriv;
1365 dev_info(dev, "gadget probed\n");
1369 usbhs_mod_gadget_probe_err_gpriv:
1375 void __devexit usbhs_mod_gadget_remove(struct usbhs_priv *priv)
1377 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);