usb: renesas_usbhs: don't re-allocation pipe buffer
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / usb / renesas_usbhs / mod_gadget.c
blob8c721d86bac8aeb83ff4436695c2c8436b059847
1 /*
2 * Renesas USB driver
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/io.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/usb/ch9.h>
21 #include <linux/usb/gadget.h>
22 #include "common.h"
25 * struct
27 struct usbhsg_request {
28 struct usb_request req;
29 struct list_head node;
32 #define EP_NAME_SIZE 8
33 struct usbhsg_gpriv;
34 struct usbhsg_pipe_handle;
35 struct usbhsg_uep {
36 struct usb_ep ep;
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;
46 struct usbhsg_gpriv {
47 struct usb_gadget gadget;
48 struct usbhs_mod mod;
50 struct usbhsg_uep *uep;
51 int uep_size;
53 struct usb_gadget_driver *driver;
55 u32 status;
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 {
68 char *name;
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);
78 * macro
80 #define usbhsg_priv_to_gpriv(priv) \
81 container_of( \
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; \
87 i < (g)->uep_size; \
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)
115 /* status */
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)
122 * list push/pop
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),
141 ureq->req.length);
144 static struct usbhsg_request *usbhsg_queue_get(struct usbhsg_uep *uep)
147 ********* assume under spin lock *********
149 if (list_empty(&uep->list))
150 return NULL;
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);
163 unsigned long flags;
164 int is_locked;
165 int ret = 0;
167 if (!uep->handler) {
168 dev_err(dev, "no handler function\n");
169 return -EIO;
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.
177 * Specially it are
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.
185 * see also
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);
193 if (ureq) {
194 if (prepare)
195 ret = uep->handler->prepare(uep, ureq);
196 else
197 ret = uep->handler->try_run(uep, ureq);
199 if (is_locked)
200 spin_unlock_irqrestore(lock, flags);
201 /******************** spin unlock ******************/
203 return ret;
206 static void usbhsg_queue_pop(struct usbhsg_uep *uep,
207 struct usbhsg_request *ureq,
208 int status)
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
229 * under spinlock.
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);
243 /* more request ? */
244 if (0 == status)
245 usbhsg_queue_prepare(uep);
249 * irq enable/disable function
251 #define usbhsg_irq_callback_ctrl(uep, status, enable) \
252 ({ \
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); \
257 if (!mod) \
258 return; \
259 if (enable) \
260 mod->irq_##status |= (1 << usbhs_pipe_number(pipe)); \
261 else \
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);
277 * handler function
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);
291 return 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);
301 void *buf;
302 int remainder, send;
303 int is_done = 0;
304 int enable;
305 int maxp;
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
322 * send <= max_packet
324 if (send > 0)
325 req->actual += send;
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 */
332 else
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.
343 enable = !is_done;
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 ?
356 if (is_done) {
357 /* it care below call in
358 "function mode" */
359 if (usbhsg_is_dcp(uep))
360 usbhs_dcp_control_transfer_done(pipe);
362 usbhsg_queue_pop(uep, ureq, 0);
365 return 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);
380 return 0;
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);
390 void *buf;
391 int maxp;
392 int remainder, recv;
393 int is_done = 0;
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
408 * recv <= max_packet
410 if (recv < 0)
411 return -EBUSY;
413 /* update parameters */
414 req->actual += recv;
416 if ((recv == remainder) || /* receive all data */
417 (recv < maxp)) /* short packet */
418 is_done = 1;
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 ? */
425 if (is_done) {
426 int disable = 0;
428 uep->handler->irq_mask(uep, disable);
429 usbhs_fifo_disable(pipe);
430 usbhsg_queue_pop(uep, ureq, 0);
433 return 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);
440 int enable = 1;
441 int ret;
444 ********* assume under spin lock *********
447 ret = usbhs_fifo_prepare_read(pipe);
448 if (ret < 0)
449 return ret;
452 * data will be read in interrupt handler
454 uep->handler->irq_mask(uep, enable);
456 return ret;
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.
485 * see
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);
510 return 0;
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);
530 return 0;
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,
541 * USB_TYPE handler
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;
552 int ret;
553 int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
554 struct usb_ctrlrequest *ctrl);
555 char *msg;
557 uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
559 switch (recip) {
560 case USB_RECIP_DEVICE:
561 msg = "DEVICE";
562 func = handler->device;
563 break;
564 case USB_RECIP_INTERFACE:
565 msg = "INTERFACE";
566 func = handler->interface;
567 break;
568 case USB_RECIP_ENDPOINT:
569 msg = "ENDPOINT";
570 func = handler->endpoint;
571 break;
572 default:
573 dev_warn(dev, "unsupported RECIP(%d)\n", recip);
574 func = NULL;
575 ret = -EINVAL;
578 if (func) {
579 dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
580 ret = func(priv, uep, ctrl);
583 return ret;
587 * irq functions
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);
603 return 0;
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);
616 int ret = 0;
618 dev_dbg(dev, "stage = %d\n", stage);
621 * see Manual
623 * "Operation"
624 * - "Interrupt Function"
625 * - "Control Transfer Stage Transition Interrupt"
626 * - Fig. "Control Transfer Stage Transitions"
629 switch (stage) {
630 case READ_DATA_STAGE:
631 dcp->handler = &usbhsg_handler_send_ctrl;
632 break;
633 case WRITE_DATA_STAGE:
634 dcp->handler = &usbhsg_handler_recv_ctrl;
635 break;
636 case NODATA_STATUS_STAGE:
637 dcp->handler = &usbhsg_handler_ctrl_stage_end;
638 break;
639 default:
640 return ret;
644 * get usb request
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;
653 break;
658 * setup stage / run recip
660 if (recip_handler)
661 ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
662 else
663 ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
665 if (ret < 0)
666 usbhs_fifo_stall(pipe);
668 return ret;
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);
678 int i, ret;
680 if (!irq_state->bempsts) {
681 dev_err(dev, "debug %s !!\n", __func__);
682 return -EIO;
685 dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
688 * search interrupted "pipe"
689 * not "uep".
691 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
692 if (!(irq_state->bempsts & (1 << i)))
693 continue;
695 uep = usbhsg_pipe_to_uep(pipe);
696 ret = usbhsg_queue_handle(uep);
697 if (ret < 0)
698 dev_err(dev, "send error %d : %d\n", i, ret);
701 return 0;
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);
711 int i, ret;
713 if (!irq_state->brdysts) {
714 dev_err(dev, "debug %s !!\n", __func__);
715 return -EIO;
718 dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
721 * search interrupted "pipe"
722 * not "uep".
724 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
725 if (!(irq_state->brdysts & (1 << i)))
726 continue;
728 uep = usbhsg_pipe_to_uep(pipe);
729 ret = usbhsg_queue_handle(uep);
730 if (ret < 0)
731 dev_err(dev, "receive error %d : %d\n", i, ret);
734 return 0;
739 * usb_dcp_ops
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);
753 if (!pipe)
754 return -EIO;
756 uep->pipe = pipe;
757 uep->pipe->mod_private = uep;
758 INIT_LIST_HEAD(&uep->list);
760 return 0;
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;
768 int disable = 0;
771 ********* assume under spin lock *********
774 usbhs_fifo_disable(pipe);
777 * disable pipe irq
779 usbhsg_irq_empty_ctrl(uep, disable);
780 usbhsg_irq_ready_ctrl(uep, disable);
782 while (1) {
783 ureq = usbhsg_queue_get(uep);
784 if (!ureq)
785 break;
787 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
790 return 0;
793 static void usbhsg_uep_init(struct usbhsg_gpriv *gpriv)
795 int i;
796 struct usbhsg_uep *uep;
798 usbhsg_for_each_uep_with_dcp(uep, gpriv, i)
799 uep->pipe = NULL;
804 * usb_ep_ops
807 static int usbhsg_ep_enable(struct usb_ep *ep,
808 const struct usb_endpoint_descriptor *desc)
810 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
811 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
812 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
813 struct usbhs_pipe *pipe;
814 spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
815 unsigned long flags;
816 int ret = -EIO;
819 * if it already have pipe,
820 * nothing to do
822 if (uep->pipe)
823 return 0;
825 /******************** spin lock ********************/
826 spin_lock_irqsave(lock, flags);
828 pipe = usbhs_pipe_malloc(priv, desc);
829 if (pipe) {
830 uep->pipe = pipe;
831 pipe->mod_private = uep;
832 INIT_LIST_HEAD(&uep->list);
834 if (usb_endpoint_dir_in(desc))
835 uep->handler = &usbhsg_handler_send_packet;
836 else
837 uep->handler = &usbhsg_handler_recv_packet;
839 ret = 0;
841 spin_unlock_irqrestore(lock, flags);
842 /******************** spin unlock ******************/
844 return ret;
847 static int usbhsg_ep_disable(struct usb_ep *ep)
849 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
850 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
851 spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
852 unsigned long flags;
853 int ret;
855 /******************** spin lock ********************/
856 spin_lock_irqsave(lock, flags);
857 ret = usbhsg_pipe_disable(uep);
858 spin_unlock_irqrestore(lock, flags);
859 /******************** spin unlock ******************/
861 return ret;
864 static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
865 gfp_t gfp_flags)
867 struct usbhsg_request *ureq;
869 ureq = kzalloc(sizeof *ureq, gfp_flags);
870 if (!ureq)
871 return NULL;
873 INIT_LIST_HEAD(&ureq->node);
874 return &ureq->req;
877 static void usbhsg_ep_free_request(struct usb_ep *ep,
878 struct usb_request *req)
880 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
882 WARN_ON(!list_empty(&ureq->node));
883 kfree(ureq);
886 static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
887 gfp_t gfp_flags)
889 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
890 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
891 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
892 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
893 spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
894 unsigned long flags;
895 int ret = 0;
896 int is_locked;
899 * CAUTION [*endpoint queue*]
901 * This function will be called from usb_request :: complete
902 * or usb driver timing.
903 * If this function is called from usb_request :: complete,
904 * it is already under spinlock on this driver.
905 * but it is called frm usb driver, this function should call spinlock.
907 * This function is using spin_trylock_irqsave to solve this issue.
908 * if "is_locked" is 1, this mean this function lock it.
909 * but if it is 0, this mean it is already under spin lock.
910 * see also
911 * CAUTION [*queue handler*]
912 * CAUTION [*request complete*]
915 /******************** spin lock ********************/
916 is_locked = spin_trylock_irqsave(lock, flags);
918 /* param check */
919 if (usbhsg_is_not_connected(gpriv) ||
920 unlikely(!gpriv->driver) ||
921 unlikely(!pipe))
922 ret = -ESHUTDOWN;
923 else
924 usbhsg_queue_push(uep, ureq);
926 if (is_locked)
927 spin_unlock_irqrestore(lock, flags);
928 /******************** spin unlock ******************/
930 usbhsg_queue_prepare(uep);
932 return ret;
935 static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
937 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
938 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
939 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
940 spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
941 unsigned long flags;
942 int is_locked;
945 * see
946 * CAUTION [*queue handler*]
947 * CAUTION [*endpoint queue*]
948 * CAUTION [*request complete*]
951 /******************** spin lock ********************/
952 is_locked = spin_trylock_irqsave(lock, flags);
954 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
956 if (is_locked)
957 spin_unlock_irqrestore(lock, flags);
958 /******************** spin unlock ******************/
960 return 0;
963 static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
965 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
966 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
967 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
968 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
969 spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
970 unsigned long flags;
971 int ret = -EAGAIN;
972 int is_locked;
975 * see
976 * CAUTION [*queue handler*]
977 * CAUTION [*endpoint queue*]
978 * CAUTION [*request complete*]
981 /******************** spin lock ********************/
982 is_locked = spin_trylock_irqsave(lock, flags);
983 if (!usbhsg_queue_get(uep)) {
985 dev_dbg(dev, "set halt %d (pipe %d)\n",
986 halt, usbhs_pipe_number(pipe));
988 if (halt)
989 usbhs_fifo_stall(pipe);
990 else
991 usbhs_fifo_disable(pipe);
993 if (halt && wedge)
994 usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
995 else
996 usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
998 ret = 0;
1001 if (is_locked)
1002 spin_unlock_irqrestore(lock, flags);
1003 /******************** spin unlock ******************/
1005 return ret;
1008 static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
1010 return __usbhsg_ep_set_halt_wedge(ep, value, 0);
1013 static int usbhsg_ep_set_wedge(struct usb_ep *ep)
1015 return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
1018 static struct usb_ep_ops usbhsg_ep_ops = {
1019 .enable = usbhsg_ep_enable,
1020 .disable = usbhsg_ep_disable,
1022 .alloc_request = usbhsg_ep_alloc_request,
1023 .free_request = usbhsg_ep_free_request,
1025 .queue = usbhsg_ep_queue,
1026 .dequeue = usbhsg_ep_dequeue,
1028 .set_halt = usbhsg_ep_set_halt,
1029 .set_wedge = usbhsg_ep_set_wedge,
1033 * usb module start/end
1035 static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
1037 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1038 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
1039 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1040 struct device *dev = usbhs_priv_to_dev(priv);
1041 spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
1042 unsigned long flags;
1044 /******************** spin lock ********************/
1045 spin_lock_irqsave(lock, flags);
1048 * enable interrupt and systems if ready
1050 usbhsg_status_set(gpriv, status);
1051 if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
1052 usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
1053 goto usbhsg_try_start_unlock;
1055 dev_dbg(dev, "start gadget\n");
1058 * pipe initialize and enable DCP
1060 usbhs_pipe_init(priv);
1061 usbhsg_uep_init(gpriv);
1062 usbhsg_dcp_enable(dcp);
1065 * system config enble
1066 * - HI speed
1067 * - function
1068 * - usb module
1070 usbhs_sys_hispeed_ctrl(priv, 1);
1071 usbhs_sys_function_ctrl(priv, 1);
1072 usbhs_sys_usb_ctrl(priv, 1);
1075 * enable irq callback
1077 mod->irq_dev_state = usbhsg_irq_dev_state;
1078 mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
1079 mod->irq_empty = usbhsg_irq_empty;
1080 mod->irq_ready = usbhsg_irq_ready;
1081 mod->irq_bempsts = 0;
1082 mod->irq_brdysts = 0;
1083 usbhs_irq_callback_update(priv, mod);
1085 usbhsg_try_start_unlock:
1086 spin_unlock_irqrestore(lock, flags);
1087 /******************** spin unlock ********************/
1089 return 0;
1092 static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
1094 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1095 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1096 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
1097 struct device *dev = usbhs_priv_to_dev(priv);
1098 spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
1099 unsigned long flags;
1101 /******************** spin lock ********************/
1102 spin_lock_irqsave(lock, flags);
1105 * disable interrupt and systems if 1st try
1107 usbhsg_status_clr(gpriv, status);
1108 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
1109 !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
1110 goto usbhsg_try_stop_unlock;
1112 /* disable all irq */
1113 mod->irq_dev_state = NULL;
1114 mod->irq_ctrl_stage = NULL;
1115 mod->irq_empty = NULL;
1116 mod->irq_ready = NULL;
1117 mod->irq_bempsts = 0;
1118 mod->irq_brdysts = 0;
1119 usbhs_irq_callback_update(priv, mod);
1121 usbhsg_dcp_disable(dcp);
1123 gpriv->gadget.speed = USB_SPEED_UNKNOWN;
1125 /* disable sys */
1126 usbhs_sys_hispeed_ctrl(priv, 0);
1127 usbhs_sys_function_ctrl(priv, 0);
1128 usbhs_sys_usb_ctrl(priv, 0);
1130 spin_unlock_irqrestore(lock, flags);
1131 /******************** spin unlock ********************/
1133 if (gpriv->driver &&
1134 gpriv->driver->disconnect)
1135 gpriv->driver->disconnect(&gpriv->gadget);
1137 dev_dbg(dev, "stop gadget\n");
1139 return 0;
1141 usbhsg_try_stop_unlock:
1142 spin_unlock_irqrestore(lock, flags);
1144 return 0;
1149 * linux usb function
1152 struct usbhsg_gpriv *the_controller;
1153 int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1154 int (*bind)(struct usb_gadget *))
1156 struct usbhsg_gpriv *gpriv = the_controller;
1157 struct usbhs_priv *priv;
1158 struct device *dev;
1159 int ret;
1161 if (!bind ||
1162 !driver ||
1163 !driver->setup ||
1164 driver->speed != USB_SPEED_HIGH)
1165 return -EINVAL;
1166 if (!gpriv)
1167 return -ENODEV;
1168 if (gpriv->driver)
1169 return -EBUSY;
1171 dev = usbhsg_gpriv_to_dev(gpriv);
1172 priv = usbhsg_gpriv_to_priv(gpriv);
1174 /* first hook up the driver ... */
1175 gpriv->driver = driver;
1176 gpriv->gadget.dev.driver = &driver->driver;
1178 ret = device_add(&gpriv->gadget.dev);
1179 if (ret) {
1180 dev_err(dev, "device_add error %d\n", ret);
1181 goto add_fail;
1184 ret = bind(&gpriv->gadget);
1185 if (ret) {
1186 dev_err(dev, "bind to driver %s error %d\n",
1187 driver->driver.name, ret);
1188 goto bind_fail;
1191 dev_dbg(dev, "bind %s\n", driver->driver.name);
1193 return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
1195 bind_fail:
1196 device_del(&gpriv->gadget.dev);
1197 add_fail:
1198 gpriv->driver = NULL;
1199 gpriv->gadget.dev.driver = NULL;
1201 return ret;
1203 EXPORT_SYMBOL(usb_gadget_probe_driver);
1205 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1207 struct usbhsg_gpriv *gpriv = the_controller;
1208 struct usbhs_priv *priv;
1209 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
1211 if (!gpriv)
1212 return -ENODEV;
1214 if (!driver ||
1215 !driver->unbind ||
1216 driver != gpriv->driver)
1217 return -EINVAL;
1219 dev = usbhsg_gpriv_to_dev(gpriv);
1220 priv = usbhsg_gpriv_to_priv(gpriv);
1222 usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
1223 device_del(&gpriv->gadget.dev);
1224 gpriv->driver = NULL;
1226 if (driver->disconnect)
1227 driver->disconnect(&gpriv->gadget);
1229 driver->unbind(&gpriv->gadget);
1230 dev_dbg(dev, "unbind %s\n", driver->driver.name);
1232 return 0;
1234 EXPORT_SYMBOL(usb_gadget_unregister_driver);
1237 * usb gadget ops
1239 static int usbhsg_get_frame(struct usb_gadget *gadget)
1241 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
1242 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
1244 return usbhs_frame_get_num(priv);
1247 static struct usb_gadget_ops usbhsg_gadget_ops = {
1248 .get_frame = usbhsg_get_frame,
1251 static int usbhsg_start(struct usbhs_priv *priv)
1253 return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
1256 static int usbhsg_stop(struct usbhs_priv *priv)
1258 return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
1261 int __devinit usbhs_mod_gadget_probe(struct usbhs_priv *priv)
1263 struct usbhsg_gpriv *gpriv;
1264 struct usbhsg_uep *uep;
1265 struct device *dev = usbhs_priv_to_dev(priv);
1266 int pipe_size = usbhs_get_dparam(priv, pipe_size);
1267 int i;
1269 gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
1270 if (!gpriv) {
1271 dev_err(dev, "Could not allocate gadget priv\n");
1272 return -ENOMEM;
1275 uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
1276 if (!uep) {
1277 dev_err(dev, "Could not allocate ep\n");
1278 goto usbhs_mod_gadget_probe_err_gpriv;
1282 * CAUTION
1284 * There is no guarantee that it is possible to access usb module here.
1285 * Don't accesses to it.
1286 * The accesse will be enable after "usbhsg_start"
1290 * register itself
1292 usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
1294 /* init gpriv */
1295 gpriv->mod.name = "gadget";
1296 gpriv->mod.start = usbhsg_start;
1297 gpriv->mod.stop = usbhsg_stop;
1298 gpriv->uep = uep;
1299 gpriv->uep_size = pipe_size;
1300 usbhsg_status_init(gpriv);
1303 * init gadget
1305 device_initialize(&gpriv->gadget.dev);
1306 dev_set_name(&gpriv->gadget.dev, "gadget");
1307 gpriv->gadget.dev.parent = dev;
1308 gpriv->gadget.name = "renesas_usbhs_udc";
1309 gpriv->gadget.ops = &usbhsg_gadget_ops;
1310 gpriv->gadget.is_dualspeed = 1;
1312 INIT_LIST_HEAD(&gpriv->gadget.ep_list);
1315 * init usb_ep
1317 usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
1318 uep->gpriv = gpriv;
1319 snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
1321 uep->ep.name = uep->ep_name;
1322 uep->ep.ops = &usbhsg_ep_ops;
1323 INIT_LIST_HEAD(&uep->ep.ep_list);
1324 INIT_LIST_HEAD(&uep->list);
1326 /* init DCP */
1327 if (usbhsg_is_dcp(uep)) {
1328 gpriv->gadget.ep0 = &uep->ep;
1329 uep->ep.maxpacket = 64;
1331 /* init normal pipe */
1332 else {
1333 uep->ep.maxpacket = 512;
1334 list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
1338 the_controller = gpriv;
1340 dev_info(dev, "gadget probed\n");
1342 return 0;
1344 usbhs_mod_gadget_probe_err_gpriv:
1345 kfree(gpriv);
1347 return -ENOMEM;
1350 void __devexit usbhs_mod_gadget_remove(struct usbhs_priv *priv)
1352 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1354 kfree(gpriv);