blob: 8c721d86bac8aeb83ff4436695c2c8436b059847 [file] [log] [blame]
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001/*
2 * Renesas USB driver
3 *
4 * Copyright (C) 2011 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6 *
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.
11 *
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
15 *
16 */
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"
23
24/*
25 * struct
26 */
27struct usbhsg_request {
28 struct usb_request req;
29 struct list_head node;
30};
31
32#define EP_NAME_SIZE 8
33struct usbhsg_gpriv;
34struct usbhsg_pipe_handle;
35struct usbhsg_uep {
36 struct usb_ep ep;
37 struct usbhs_pipe *pipe;
38 struct list_head list;
39
40 char ep_name[EP_NAME_SIZE];
41
42 struct usbhsg_gpriv *gpriv;
43 struct usbhsg_pipe_handle *handler;
44};
45
46struct usbhsg_gpriv {
47 struct usb_gadget gadget;
48 struct usbhs_mod mod;
49
50 struct usbhsg_uep *uep;
51 int uep_size;
52
53 struct usb_gadget_driver *driver;
54
55 u32 status;
56#define USBHSG_STATUS_STARTED (1 << 0)
57#define USBHSG_STATUS_REGISTERD (1 << 1)
58#define USBHSG_STATUS_WEDGE (1 << 2)
59};
60
61struct 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);
65};
66
67struct 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);
75};
76
77/*
78 * macro
79 */
80#define usbhsg_priv_to_gpriv(priv) \
81 container_of( \
82 usbhs_mod_get(priv, USBHS_GADGET), \
83 struct usbhsg_gpriv, mod)
84
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)
89
90#define usbhsg_for_each_uep(pos, gpriv, i) \
91 __usbhsg_for_each_uep(1, pos, gpriv, i)
92
93#define usbhsg_for_each_uep_with_dcp(pos, gpriv, i) \
94 __usbhsg_for_each_uep(0, pos, gpriv, i)
95
96#define usbhsg_gadget_to_gpriv(g)\
97 container_of(g, struct usbhsg_gpriv, gadget)
98
99#define usbhsg_req_to_ureq(r)\
100 container_of(r, struct usbhsg_request, req)
101
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))
112
113#define usbhsg_is_not_connected(gp) ((gp)->gadget.speed == USB_SPEED_UNKNOWN)
114
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)
120
121/*
122 * list push/pop
123 */
124static void usbhsg_queue_push(struct usbhsg_uep *uep,
125 struct usbhsg_request *ureq)
126{
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);
130
131 /*
132 ********* assume under spin lock *********
133 */
134 list_del_init(&ureq->node);
135 list_add_tail(&ureq->node, &uep->list);
136 ureq->req.actual = 0;
137 ureq->req.status = -EINPROGRESS;
138
139 dev_dbg(dev, "pipe %d : queue push (%d)\n",
140 usbhs_pipe_number(pipe),
141 ureq->req.length);
142}
143
144static struct usbhsg_request *usbhsg_queue_get(struct usbhsg_uep *uep)
145{
146 /*
147 ********* assume under spin lock *********
148 */
149 if (list_empty(&uep->list))
150 return NULL;
151
152 return list_entry(uep->list.next, struct usbhsg_request, node);
153}
154
155#define usbhsg_queue_prepare(uep) __usbhsg_queue_handler(uep, 1);
156#define usbhsg_queue_handle(uep) __usbhsg_queue_handler(uep, 0);
157static int __usbhsg_queue_handler(struct usbhsg_uep *uep, int prepare)
158{
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;
166
167 if (!uep->handler) {
168 dev_err(dev, "no handler function\n");
169 return -EIO;
170 }
171
172 /*
173 * CAUTION [*queue handler*]
174 *
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
180 *
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*]
188 */
189
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);
198 }
199 if (is_locked)
200 spin_unlock_irqrestore(lock, flags);
201 /******************** spin unlock ******************/
202
203 return ret;
204}
205
206static void usbhsg_queue_pop(struct usbhsg_uep *uep,
207 struct usbhsg_request *ureq,
208 int status)
209{
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);
213
214 /*
215 ********* assume under spin lock *********
216 */
217
218 /*
219 * CAUTION [*request complete*]
220 *
221 * There is a possibility not to be called in correct order
222 * if "complete" is called without spinlock.
223 *
224 * So, this function assume it is under spinlock,
225 * and call usb_request :: complete.
226 *
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.
230 *
231 * To avoid dead-lock, this driver is using spin_trylock.
232 * CAUTION [*endpoint queue*]
233 * CAUTION [*queue handler*]
234 */
235
236 dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
237
238 list_del_init(&ureq->node);
239
240 ureq->req.status = status;
241 ureq->req.complete(&uep->ep, &ureq->req);
242
243 /* more request ? */
244 if (0 == status)
245 usbhsg_queue_prepare(uep);
246}
247
248/*
249 * irq enable/disable function
250 */
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); \
264 })
265
266static void usbhsg_irq_empty_ctrl(struct usbhsg_uep *uep, int enable)
267{
268 usbhsg_irq_callback_ctrl(uep, bempsts, enable);
269}
270
271static void usbhsg_irq_ready_ctrl(struct usbhsg_uep *uep, int enable)
272{
273 usbhsg_irq_callback_ctrl(uep, brdysts, enable);
274}
275
276/*
277 * handler function
278 */
279static int usbhsg_try_run_ctrl_stage_end(struct usbhsg_uep *uep,
280 struct usbhsg_request *ureq)
281{
282 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
283
284 /*
285 ********* assume under spin lock *********
286 */
287
288 usbhs_dcp_control_transfer_done(pipe);
289 usbhsg_queue_pop(uep, ureq, 0);
290
291 return 0;
292}
293
294static int usbhsg_try_run_send_packet(struct usbhsg_uep *uep,
295 struct usbhsg_request *ureq)
296{
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;
306
307 /*
308 ********* assume under spin lock *********
309 */
310
311 maxp = usbhs_pipe_get_maxpacket(pipe);
312 buf = req->buf + req->actual;
313 remainder = req->length - req->actual;
314
315 send = usbhs_fifo_write(pipe, buf, remainder);
316
317 /*
318 * send < 0 : pipe busy
319 * send = 0 : send zero packet
320 * send > 0 : send data
321 *
322 * send <= max_packet
323 */
324 if (send > 0)
325 req->actual += send;
326
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 ? */
334
335 dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n",
336 usbhs_pipe_number(pipe),
337 remainder, send, is_done, req->zero);
338
339 /*
340 * enable interrupt and send again in irq handler
341 * if it still have remainder data which should be sent.
342 */
343 enable = !is_done;
344 uep->handler->irq_mask(uep, enable);
345
346 /*
347 * usbhs_fifo_enable execute
348 * - after callback_update,
349 * - before queue_pop / stage_end
350 */
351 usbhs_fifo_enable(pipe);
352
353 /*
354 * all data were sent ?
355 */
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);
361
362 usbhsg_queue_pop(uep, ureq, 0);
363 }
364
365 return 0;
366}
367
368static int usbhsg_prepare_send_packet(struct usbhsg_uep *uep,
369 struct usbhsg_request *ureq)
370{
371 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
372
373 /*
374 ********* assume under spin lock *********
375 */
376
377 usbhs_fifo_prepare_write(pipe);
378 usbhsg_try_run_send_packet(uep, ureq);
379
380 return 0;
381}
382
383static int usbhsg_try_run_receive_packet(struct usbhsg_uep *uep,
384 struct usbhsg_request *ureq)
385{
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;
394
395 /*
396 ********* assume under spin lock *********
397 */
398
399 maxp = usbhs_pipe_get_maxpacket(pipe);
400 buf = req->buf + req->actual;
401 remainder = req->length - req->actual;
402
403 recv = usbhs_fifo_read(pipe, buf, remainder);
404 /*
405 * recv < 0 : pipe busy
406 * recv >= 0 : receive data
407 *
408 * recv <= max_packet
409 */
410 if (recv < 0)
411 return -EBUSY;
412
413 /* update parameters */
414 req->actual += recv;
415
416 if ((recv == remainder) || /* receive all data */
417 (recv < maxp)) /* short packet */
418 is_done = 1;
419
420 dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n",
421 usbhs_pipe_number(pipe),
422 remainder, recv, is_done, req->zero);
423
424 /* read all data ? */
425 if (is_done) {
426 int disable = 0;
427
428 uep->handler->irq_mask(uep, disable);
429 usbhs_fifo_disable(pipe);
430 usbhsg_queue_pop(uep, ureq, 0);
431 }
432
433 return 0;
434}
435
436static int usbhsg_prepare_receive_packet(struct usbhsg_uep *uep,
437 struct usbhsg_request *ureq)
438{
439 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
440 int enable = 1;
441 int ret;
442
443 /*
444 ********* assume under spin lock *********
445 */
446
447 ret = usbhs_fifo_prepare_read(pipe);
448 if (ret < 0)
449 return ret;
450
451 /*
452 * data will be read in interrupt handler
453 */
454 uep->handler->irq_mask(uep, enable);
455
456 return ret;
457}
458
459static 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,
463};
464
465static 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,
469};
470
471static 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,
475};
476
477static 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,
480};
481
482/*
483 * DCP pipe can NOT use "ready interrupt" for "send"
484 * it should use "empty" interrupt.
485 * see
486 * "Operation" - "Interrupt Function" - "BRDY Interrupt"
487 *
488 * on the other hand, normal pipe can use "ready interrupt" for "send"
489 * even though it is single/double buffer
490 */
491#define usbhsg_handler_send_ctrl usbhsg_handler_send_by_empty
492#define usbhsg_handler_recv_ctrl usbhsg_handler_recv_by_ready
493
494#define usbhsg_handler_send_packet usbhsg_handler_send_by_ready
495#define usbhsg_handler_recv_packet usbhsg_handler_recv_by_ready
496
497/*
498 * USB_TYPE_STANDARD / clear feature functions
499 */
500static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
501 struct usbhsg_uep *uep,
502 struct usb_ctrlrequest *ctrl)
503{
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);
507
508 usbhs_dcp_control_transfer_done(pipe);
509
510 return 0;
511}
512
513static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
514 struct usbhsg_uep *uep,
515 struct usb_ctrlrequest *ctrl)
516{
517 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
518 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
519
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);
524 }
525
526 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
527
528 usbhsg_queue_prepare(uep);
529
530 return 0;
531}
532
533struct 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,
538};
539
540/*
541 * USB_TYPE handler
542 */
543static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
544 struct usbhsg_recip_handle *handler,
545 struct usb_ctrlrequest *ctrl)
546{
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;
556
557 uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
558
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;
576 }
577
578 if (func) {
579 dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
580 ret = func(priv, uep, ctrl);
581 }
582
583 return ret;
584}
585
586/*
587 * irq functions
588 *
589 * it will be called from usbhs_interrupt
590 */
591static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
592 struct usbhs_irq_state *irq_state)
593{
594 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
595 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
596
597 gpriv->gadget.speed = usbhs_status_get_usb_speed(irq_state);
598
599 dev_dbg(dev, "state = %x : speed : %d\n",
600 usbhs_status_get_device_state(irq_state),
601 gpriv->gadget.speed);
602
603 return 0;
604}
605
606static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
607 struct usbhs_irq_state *irq_state)
608{
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;
617
618 dev_dbg(dev, "stage = %d\n", stage);
619
620 /*
621 * see Manual
622 *
623 * "Operation"
624 * - "Interrupt Function"
625 * - "Control Transfer Stage Transition Interrupt"
626 * - Fig. "Control Transfer Stage Transitions"
627 */
628
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;
641 }
642
643 /*
644 * get usb request
645 */
646 usbhs_usbreq_get_val(priv, &ctrl);
647
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;
654 }
655 }
656
657 /*
658 * setup stage / run recip
659 */
660 if (recip_handler)
661 ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
662 else
663 ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
664
665 if (ret < 0)
666 usbhs_fifo_stall(pipe);
667
668 return ret;
669}
670
671static int usbhsg_irq_empty(struct usbhs_priv *priv,
672 struct usbhs_irq_state *irq_state)
673{
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;
679
680 if (!irq_state->bempsts) {
681 dev_err(dev, "debug %s !!\n", __func__);
682 return -EIO;
683 }
684
685 dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
686
687 /*
688 * search interrupted "pipe"
689 * not "uep".
690 */
691 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
692 if (!(irq_state->bempsts & (1 << i)))
693 continue;
694
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);
699 }
700
701 return 0;
702}
703
704static int usbhsg_irq_ready(struct usbhs_priv *priv,
705 struct usbhs_irq_state *irq_state)
706{
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;
712
713 if (!irq_state->brdysts) {
714 dev_err(dev, "debug %s !!\n", __func__);
715 return -EIO;
716 }
717
718 dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
719
720 /*
721 * search interrupted "pipe"
722 * not "uep".
723 */
724 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
725 if (!(irq_state->brdysts & (1 << i)))
726 continue;
727
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);
732 }
733
734 return 0;
735}
736
737/*
738 *
739 * usb_dcp_ops
740 *
741 */
742static int usbhsg_dcp_enable(struct usbhsg_uep *uep)
743{
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;
747
748 /*
749 ********* assume under spin lock *********
750 */
751
752 pipe = usbhs_dcp_malloc(priv);
753 if (!pipe)
754 return -EIO;
755
756 uep->pipe = pipe;
757 uep->pipe->mod_private = uep;
758 INIT_LIST_HEAD(&uep->list);
759
760 return 0;
761}
762
763#define usbhsg_dcp_disable usbhsg_pipe_disable
764static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
765{
766 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
767 struct usbhsg_request *ureq;
768 int disable = 0;
769
770 /*
771 ********* assume under spin lock *********
772 */
773
774 usbhs_fifo_disable(pipe);
775
776 /*
777 * disable pipe irq
778 */
779 usbhsg_irq_empty_ctrl(uep, disable);
780 usbhsg_irq_ready_ctrl(uep, disable);
781
782 while (1) {
783 ureq = usbhsg_queue_get(uep);
784 if (!ureq)
785 break;
786
787 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
788 }
789
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900790 return 0;
791}
792
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900793static void usbhsg_uep_init(struct usbhsg_gpriv *gpriv)
794{
795 int i;
796 struct usbhsg_uep *uep;
797
798 usbhsg_for_each_uep_with_dcp(uep, gpriv, i)
799 uep->pipe = NULL;
800}
801
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900802/*
803 *
804 * usb_ep_ops
805 *
806 */
807static int usbhsg_ep_enable(struct usb_ep *ep,
808 const struct usb_endpoint_descriptor *desc)
809{
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;
817
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900818 /*
819 * if it already have pipe,
820 * nothing to do
821 */
822 if (uep->pipe)
823 return 0;
824
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900825 /******************** spin lock ********************/
826 spin_lock_irqsave(lock, flags);
827
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);
833
834 if (usb_endpoint_dir_in(desc))
835 uep->handler = &usbhsg_handler_send_packet;
836 else
837 uep->handler = &usbhsg_handler_recv_packet;
838
839 ret = 0;
840 }
841 spin_unlock_irqrestore(lock, flags);
842 /******************** spin unlock ******************/
843
844 return ret;
845}
846
847static int usbhsg_ep_disable(struct usb_ep *ep)
848{
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;
854
855 /******************** spin lock ********************/
856 spin_lock_irqsave(lock, flags);
857 ret = usbhsg_pipe_disable(uep);
858 spin_unlock_irqrestore(lock, flags);
859 /******************** spin unlock ******************/
860
861 return ret;
862}
863
864static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
865 gfp_t gfp_flags)
866{
867 struct usbhsg_request *ureq;
868
869 ureq = kzalloc(sizeof *ureq, gfp_flags);
870 if (!ureq)
871 return NULL;
872
873 INIT_LIST_HEAD(&ureq->node);
874 return &ureq->req;
875}
876
877static void usbhsg_ep_free_request(struct usb_ep *ep,
878 struct usb_request *req)
879{
880 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
881
882 WARN_ON(!list_empty(&ureq->node));
883 kfree(ureq);
884}
885
886static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
887 gfp_t gfp_flags)
888{
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;
897
898 /*
899 * CAUTION [*endpoint queue*]
900 *
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.
906 *
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*]
913 */
914
915 /******************** spin lock ********************/
916 is_locked = spin_trylock_irqsave(lock, flags);
917
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);
925
926 if (is_locked)
927 spin_unlock_irqrestore(lock, flags);
928 /******************** spin unlock ******************/
929
930 usbhsg_queue_prepare(uep);
931
932 return ret;
933}
934
935static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
936{
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;
943
944 /*
945 * see
946 * CAUTION [*queue handler*]
947 * CAUTION [*endpoint queue*]
948 * CAUTION [*request complete*]
949 */
950
951 /******************** spin lock ********************/
952 is_locked = spin_trylock_irqsave(lock, flags);
953
954 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
955
956 if (is_locked)
957 spin_unlock_irqrestore(lock, flags);
958 /******************** spin unlock ******************/
959
960 return 0;
961}
962
963static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
964{
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;
973
974 /*
975 * see
976 * CAUTION [*queue handler*]
977 * CAUTION [*endpoint queue*]
978 * CAUTION [*request complete*]
979 */
980
981 /******************** spin lock ********************/
982 is_locked = spin_trylock_irqsave(lock, flags);
983 if (!usbhsg_queue_get(uep)) {
984
985 dev_dbg(dev, "set halt %d (pipe %d)\n",
986 halt, usbhs_pipe_number(pipe));
987
988 if (halt)
989 usbhs_fifo_stall(pipe);
990 else
991 usbhs_fifo_disable(pipe);
992
993 if (halt && wedge)
994 usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
995 else
996 usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
997
998 ret = 0;
999 }
1000
1001 if (is_locked)
1002 spin_unlock_irqrestore(lock, flags);
1003 /******************** spin unlock ******************/
1004
1005 return ret;
1006}
1007
1008static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
1009{
1010 return __usbhsg_ep_set_halt_wedge(ep, value, 0);
1011}
1012
1013static int usbhsg_ep_set_wedge(struct usb_ep *ep)
1014{
1015 return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
1016}
1017
1018static struct usb_ep_ops usbhsg_ep_ops = {
1019 .enable = usbhsg_ep_enable,
1020 .disable = usbhsg_ep_disable,
1021
1022 .alloc_request = usbhsg_ep_alloc_request,
1023 .free_request = usbhsg_ep_free_request,
1024
1025 .queue = usbhsg_ep_queue,
1026 .dequeue = usbhsg_ep_dequeue,
1027
1028 .set_halt = usbhsg_ep_set_halt,
1029 .set_wedge = usbhsg_ep_set_wedge,
1030};
1031
1032/*
1033 * usb module start/end
1034 */
1035static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
1036{
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;
1043
1044 /******************** spin lock ********************/
1045 spin_lock_irqsave(lock, flags);
1046
1047 /*
1048 * enable interrupt and systems if ready
1049 */
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;
1054
1055 dev_dbg(dev, "start gadget\n");
1056
1057 /*
1058 * pipe initialize and enable DCP
1059 */
1060 usbhs_pipe_init(priv);
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +09001061 usbhsg_uep_init(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001062 usbhsg_dcp_enable(dcp);
1063
1064 /*
1065 * system config enble
1066 * - HI speed
1067 * - function
1068 * - usb module
1069 */
1070 usbhs_sys_hispeed_ctrl(priv, 1);
1071 usbhs_sys_function_ctrl(priv, 1);
1072 usbhs_sys_usb_ctrl(priv, 1);
1073
1074 /*
1075 * enable irq callback
1076 */
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);
1084
1085usbhsg_try_start_unlock:
1086 spin_unlock_irqrestore(lock, flags);
1087 /******************** spin unlock ********************/
1088
1089 return 0;
1090}
1091
1092static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
1093{
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;
1100
1101 /******************** spin lock ********************/
1102 spin_lock_irqsave(lock, flags);
1103
1104 /*
1105 * disable interrupt and systems if 1st try
1106 */
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;
1111
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);
1120
1121 usbhsg_dcp_disable(dcp);
1122
1123 gpriv->gadget.speed = USB_SPEED_UNKNOWN;
1124
1125 /* disable sys */
1126 usbhs_sys_hispeed_ctrl(priv, 0);
1127 usbhs_sys_function_ctrl(priv, 0);
1128 usbhs_sys_usb_ctrl(priv, 0);
1129
1130 spin_unlock_irqrestore(lock, flags);
1131 /******************** spin unlock ********************/
1132
1133 if (gpriv->driver &&
1134 gpriv->driver->disconnect)
1135 gpriv->driver->disconnect(&gpriv->gadget);
1136
1137 dev_dbg(dev, "stop gadget\n");
1138
1139 return 0;
1140
1141usbhsg_try_stop_unlock:
1142 spin_unlock_irqrestore(lock, flags);
1143
1144 return 0;
1145}
1146
1147/*
1148 *
1149 * linux usb function
1150 *
1151 */
1152struct usbhsg_gpriv *the_controller;
1153int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1154 int (*bind)(struct usb_gadget *))
1155{
1156 struct usbhsg_gpriv *gpriv = the_controller;
1157 struct usbhs_priv *priv;
1158 struct device *dev;
1159 int ret;
1160
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;
1170
1171 dev = usbhsg_gpriv_to_dev(gpriv);
1172 priv = usbhsg_gpriv_to_priv(gpriv);
1173
1174 /* first hook up the driver ... */
1175 gpriv->driver = driver;
1176 gpriv->gadget.dev.driver = &driver->driver;
1177
1178 ret = device_add(&gpriv->gadget.dev);
1179 if (ret) {
1180 dev_err(dev, "device_add error %d\n", ret);
1181 goto add_fail;
1182 }
1183
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;
1189 }
1190
1191 dev_dbg(dev, "bind %s\n", driver->driver.name);
1192
1193 return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
1194
1195bind_fail:
1196 device_del(&gpriv->gadget.dev);
1197add_fail:
1198 gpriv->driver = NULL;
1199 gpriv->gadget.dev.driver = NULL;
1200
1201 return ret;
1202}
1203EXPORT_SYMBOL(usb_gadget_probe_driver);
1204
1205int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1206{
1207 struct usbhsg_gpriv *gpriv = the_controller;
1208 struct usbhs_priv *priv;
1209 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
1210
1211 if (!gpriv)
1212 return -ENODEV;
1213
1214 if (!driver ||
1215 !driver->unbind ||
1216 driver != gpriv->driver)
1217 return -EINVAL;
1218
1219 dev = usbhsg_gpriv_to_dev(gpriv);
1220 priv = usbhsg_gpriv_to_priv(gpriv);
1221
1222 usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
1223 device_del(&gpriv->gadget.dev);
1224 gpriv->driver = NULL;
1225
1226 if (driver->disconnect)
1227 driver->disconnect(&gpriv->gadget);
1228
1229 driver->unbind(&gpriv->gadget);
1230 dev_dbg(dev, "unbind %s\n", driver->driver.name);
1231
1232 return 0;
1233}
1234EXPORT_SYMBOL(usb_gadget_unregister_driver);
1235
1236/*
1237 * usb gadget ops
1238 */
1239static int usbhsg_get_frame(struct usb_gadget *gadget)
1240{
1241 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
1242 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
1243
1244 return usbhs_frame_get_num(priv);
1245}
1246
1247static struct usb_gadget_ops usbhsg_gadget_ops = {
1248 .get_frame = usbhsg_get_frame,
1249};
1250
1251static int usbhsg_start(struct usbhs_priv *priv)
1252{
1253 return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
1254}
1255
1256static int usbhsg_stop(struct usbhs_priv *priv)
1257{
1258 return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
1259}
1260
1261int __devinit usbhs_mod_gadget_probe(struct usbhs_priv *priv)
1262{
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;
1268
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;
1273 }
1274
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;
1279 }
1280
1281 /*
1282 * CAUTION
1283 *
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"
1287 */
1288
1289 /*
1290 * register itself
1291 */
1292 usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
1293
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);
1301
1302 /*
1303 * init gadget
1304 */
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;
1311
1312 INIT_LIST_HEAD(&gpriv->gadget.ep_list);
1313
1314 /*
1315 * init usb_ep
1316 */
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);
1320
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);
1325
1326 /* init DCP */
1327 if (usbhsg_is_dcp(uep)) {
1328 gpriv->gadget.ep0 = &uep->ep;
1329 uep->ep.maxpacket = 64;
1330 }
1331 /* init normal pipe */
1332 else {
1333 uep->ep.maxpacket = 512;
1334 list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
1335 }
1336 }
1337
1338 the_controller = gpriv;
1339
1340 dev_info(dev, "gadget probed\n");
1341
1342 return 0;
1343
1344usbhs_mod_gadget_probe_err_gpriv:
1345 kfree(gpriv);
1346
1347 return -ENOMEM;
1348}
1349
1350void __devexit usbhs_mod_gadget_remove(struct usbhs_priv *priv)
1351{
1352 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1353
1354 kfree(gpriv);
1355}