blob: d027c80ab3599bd58a3cdadb0c389fdc76904a99 [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/*
Kuninori Morimotocb966322011-04-21 14:10:08 +0900122 * usbhsg_trylock
123 *
124 * This driver don't use spin_try_lock
125 * to avoid warning of CONFIG_DEBUG_SPINLOCK
126 */
127static spinlock_t *usbhsg_trylock(struct usbhsg_gpriv *gpriv,
128 unsigned long *flags)
129{
130 spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
131
132 /* check spin lock status
133 * to avoid deadlock/nest */
134 if (spin_is_locked(lock))
135 return NULL;
136
137 spin_lock_irqsave(lock, *flags);
138
139 return lock;
140}
141
142static void usbhsg_unlock(spinlock_t *lock, unsigned long *flags)
143{
144 if (!lock)
145 return;
146
147 spin_unlock_irqrestore(lock, *flags);
148}
149
150/*
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900151 * list push/pop
152 */
153static void usbhsg_queue_push(struct usbhsg_uep *uep,
154 struct usbhsg_request *ureq)
155{
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);
159
160 /*
161 ********* assume under spin lock *********
162 */
163 list_del_init(&ureq->node);
164 list_add_tail(&ureq->node, &uep->list);
165 ureq->req.actual = 0;
166 ureq->req.status = -EINPROGRESS;
167
168 dev_dbg(dev, "pipe %d : queue push (%d)\n",
169 usbhs_pipe_number(pipe),
170 ureq->req.length);
171}
172
173static struct usbhsg_request *usbhsg_queue_get(struct usbhsg_uep *uep)
174{
175 /*
176 ********* assume under spin lock *********
177 */
178 if (list_empty(&uep->list))
179 return NULL;
180
181 return list_entry(uep->list.next, struct usbhsg_request, node);
182}
183
184#define usbhsg_queue_prepare(uep) __usbhsg_queue_handler(uep, 1);
185#define usbhsg_queue_handle(uep) __usbhsg_queue_handler(uep, 0);
186static int __usbhsg_queue_handler(struct usbhsg_uep *uep, int prepare)
187{
188 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
189 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
190 struct usbhsg_request *ureq;
Kuninori Morimotocb966322011-04-21 14:10:08 +0900191 spinlock_t *lock;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900192 unsigned long flags;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900193 int ret = 0;
194
195 if (!uep->handler) {
196 dev_err(dev, "no handler function\n");
197 return -EIO;
198 }
199
200 /*
201 * CAUTION [*queue handler*]
202 *
203 * This function will be called for start/restart queue operation.
204 * OTOH the most much worry for USB driver is spinlock nest.
205 * Specially it are
206 * - usb_ep_ops :: queue
207 * - usb_request :: complete
208 *
209 * But the caller of this function need not care about spinlock.
Kuninori Morimotocb966322011-04-21 14:10:08 +0900210 * This function is using usbhsg_trylock for it.
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900211 * 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.
213 * see also
214 * CAUTION [*endpoint queue*]
215 * CAUTION [*request complete*]
216 */
217
218 /****************** spin try lock *******************/
Kuninori Morimotocb966322011-04-21 14:10:08 +0900219 lock = usbhsg_trylock(gpriv, &flags);
220
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900221 ureq = usbhsg_queue_get(uep);
222 if (ureq) {
223 if (prepare)
224 ret = uep->handler->prepare(uep, ureq);
225 else
226 ret = uep->handler->try_run(uep, ureq);
227 }
Kuninori Morimotocb966322011-04-21 14:10:08 +0900228 usbhsg_unlock(lock, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900229 /******************** spin unlock ******************/
230
231 return ret;
232}
233
234static void usbhsg_queue_pop(struct usbhsg_uep *uep,
235 struct usbhsg_request *ureq,
236 int status)
237{
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);
241
242 /*
243 ********* assume under spin lock *********
244 */
245
246 /*
247 * CAUTION [*request complete*]
248 *
249 * There is a possibility not to be called in correct order
250 * if "complete" is called without spinlock.
251 *
252 * So, this function assume it is under spinlock,
253 * and call usb_request :: complete.
254 *
255 * But this "complete" will push next usb_request.
256 * It mean "usb_ep_ops :: queue" which is using spinlock is called
257 * under spinlock.
258 *
Kuninori Morimotocb966322011-04-21 14:10:08 +0900259 * To avoid dead-lock, this driver is using usbhsg_trylock.
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900260 * CAUTION [*endpoint queue*]
261 * CAUTION [*queue handler*]
262 */
263
264 dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
265
266 list_del_init(&ureq->node);
267
268 ureq->req.status = status;
269 ureq->req.complete(&uep->ep, &ureq->req);
270
271 /* more request ? */
272 if (0 == status)
273 usbhsg_queue_prepare(uep);
274}
275
276/*
277 * irq enable/disable function
278 */
279#define usbhsg_irq_callback_ctrl(uep, status, enable) \
280 ({ \
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); \
285 if (!mod) \
286 return; \
287 if (enable) \
288 mod->irq_##status |= (1 << usbhs_pipe_number(pipe)); \
289 else \
290 mod->irq_##status &= ~(1 << usbhs_pipe_number(pipe)); \
291 usbhs_irq_callback_update(priv, mod); \
292 })
293
294static void usbhsg_irq_empty_ctrl(struct usbhsg_uep *uep, int enable)
295{
296 usbhsg_irq_callback_ctrl(uep, bempsts, enable);
297}
298
299static void usbhsg_irq_ready_ctrl(struct usbhsg_uep *uep, int enable)
300{
301 usbhsg_irq_callback_ctrl(uep, brdysts, enable);
302}
303
304/*
305 * handler function
306 */
307static int usbhsg_try_run_ctrl_stage_end(struct usbhsg_uep *uep,
308 struct usbhsg_request *ureq)
309{
310 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
311
312 /*
313 ********* assume under spin lock *********
314 */
315
316 usbhs_dcp_control_transfer_done(pipe);
317 usbhsg_queue_pop(uep, ureq, 0);
318
319 return 0;
320}
321
322static int usbhsg_try_run_send_packet(struct usbhsg_uep *uep,
323 struct usbhsg_request *ureq)
324{
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);
329 void *buf;
330 int remainder, send;
331 int is_done = 0;
332 int enable;
333 int maxp;
334
335 /*
336 ********* assume under spin lock *********
337 */
338
339 maxp = usbhs_pipe_get_maxpacket(pipe);
340 buf = req->buf + req->actual;
341 remainder = req->length - req->actual;
342
343 send = usbhs_fifo_write(pipe, buf, remainder);
344
345 /*
346 * send < 0 : pipe busy
347 * send = 0 : send zero packet
348 * send > 0 : send data
349 *
350 * send <= max_packet
351 */
352 if (send > 0)
353 req->actual += send;
354
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 */
360 else
361 is_done = !req->zero; /* send zero packet ? */
362
363 dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n",
364 usbhs_pipe_number(pipe),
365 remainder, send, is_done, req->zero);
366
367 /*
368 * enable interrupt and send again in irq handler
369 * if it still have remainder data which should be sent.
370 */
371 enable = !is_done;
372 uep->handler->irq_mask(uep, enable);
373
374 /*
375 * usbhs_fifo_enable execute
376 * - after callback_update,
377 * - before queue_pop / stage_end
378 */
379 usbhs_fifo_enable(pipe);
380
381 /*
382 * all data were sent ?
383 */
384 if (is_done) {
385 /* it care below call in
386 "function mode" */
387 if (usbhsg_is_dcp(uep))
388 usbhs_dcp_control_transfer_done(pipe);
389
390 usbhsg_queue_pop(uep, ureq, 0);
391 }
392
393 return 0;
394}
395
396static int usbhsg_prepare_send_packet(struct usbhsg_uep *uep,
397 struct usbhsg_request *ureq)
398{
399 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
400
401 /*
402 ********* assume under spin lock *********
403 */
404
405 usbhs_fifo_prepare_write(pipe);
406 usbhsg_try_run_send_packet(uep, ureq);
407
408 return 0;
409}
410
411static int usbhsg_try_run_receive_packet(struct usbhsg_uep *uep,
412 struct usbhsg_request *ureq)
413{
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);
418 void *buf;
419 int maxp;
420 int remainder, recv;
421 int is_done = 0;
422
423 /*
424 ********* assume under spin lock *********
425 */
426
427 maxp = usbhs_pipe_get_maxpacket(pipe);
428 buf = req->buf + req->actual;
429 remainder = req->length - req->actual;
430
431 recv = usbhs_fifo_read(pipe, buf, remainder);
432 /*
433 * recv < 0 : pipe busy
434 * recv >= 0 : receive data
435 *
436 * recv <= max_packet
437 */
438 if (recv < 0)
439 return -EBUSY;
440
441 /* update parameters */
442 req->actual += recv;
443
444 if ((recv == remainder) || /* receive all data */
445 (recv < maxp)) /* short packet */
446 is_done = 1;
447
448 dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n",
449 usbhs_pipe_number(pipe),
450 remainder, recv, is_done, req->zero);
451
452 /* read all data ? */
453 if (is_done) {
454 int disable = 0;
455
456 uep->handler->irq_mask(uep, disable);
457 usbhs_fifo_disable(pipe);
458 usbhsg_queue_pop(uep, ureq, 0);
459 }
460
461 return 0;
462}
463
464static int usbhsg_prepare_receive_packet(struct usbhsg_uep *uep,
465 struct usbhsg_request *ureq)
466{
467 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
468 int enable = 1;
469 int ret;
470
471 /*
472 ********* assume under spin lock *********
473 */
474
475 ret = usbhs_fifo_prepare_read(pipe);
476 if (ret < 0)
477 return ret;
478
479 /*
480 * data will be read in interrupt handler
481 */
482 uep->handler->irq_mask(uep, enable);
483
484 return ret;
485}
486
487static 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,
491};
492
493static 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,
497};
498
499static 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,
503};
504
505static 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,
508};
509
510/*
511 * DCP pipe can NOT use "ready interrupt" for "send"
512 * it should use "empty" interrupt.
513 * see
514 * "Operation" - "Interrupt Function" - "BRDY Interrupt"
515 *
516 * on the other hand, normal pipe can use "ready interrupt" for "send"
517 * even though it is single/double buffer
518 */
519#define usbhsg_handler_send_ctrl usbhsg_handler_send_by_empty
520#define usbhsg_handler_recv_ctrl usbhsg_handler_recv_by_ready
521
522#define usbhsg_handler_send_packet usbhsg_handler_send_by_ready
523#define usbhsg_handler_recv_packet usbhsg_handler_recv_by_ready
524
525/*
526 * USB_TYPE_STANDARD / clear feature functions
527 */
528static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
529 struct usbhsg_uep *uep,
530 struct usb_ctrlrequest *ctrl)
531{
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);
535
536 usbhs_dcp_control_transfer_done(pipe);
537
538 return 0;
539}
540
541static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
542 struct usbhsg_uep *uep,
543 struct usb_ctrlrequest *ctrl)
544{
545 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
546 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
547
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);
552 }
553
554 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
555
556 usbhsg_queue_prepare(uep);
557
558 return 0;
559}
560
561struct 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,
566};
567
568/*
569 * USB_TYPE handler
570 */
571static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
572 struct usbhsg_recip_handle *handler,
573 struct usb_ctrlrequest *ctrl)
574{
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;
580 int ret;
581 int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
582 struct usb_ctrlrequest *ctrl);
583 char *msg;
584
585 uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
586
587 switch (recip) {
588 case USB_RECIP_DEVICE:
589 msg = "DEVICE";
590 func = handler->device;
591 break;
592 case USB_RECIP_INTERFACE:
593 msg = "INTERFACE";
594 func = handler->interface;
595 break;
596 case USB_RECIP_ENDPOINT:
597 msg = "ENDPOINT";
598 func = handler->endpoint;
599 break;
600 default:
601 dev_warn(dev, "unsupported RECIP(%d)\n", recip);
602 func = NULL;
603 ret = -EINVAL;
604 }
605
606 if (func) {
607 dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
608 ret = func(priv, uep, ctrl);
609 }
610
611 return ret;
612}
613
614/*
615 * irq functions
616 *
617 * it will be called from usbhs_interrupt
618 */
619static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
620 struct usbhs_irq_state *irq_state)
621{
622 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
623 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
624
625 gpriv->gadget.speed = usbhs_status_get_usb_speed(irq_state);
626
627 dev_dbg(dev, "state = %x : speed : %d\n",
628 usbhs_status_get_device_state(irq_state),
629 gpriv->gadget.speed);
630
631 return 0;
632}
633
634static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
635 struct usbhs_irq_state *irq_state)
636{
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);
644 int ret = 0;
645
646 dev_dbg(dev, "stage = %d\n", stage);
647
648 /*
649 * see Manual
650 *
651 * "Operation"
652 * - "Interrupt Function"
653 * - "Control Transfer Stage Transition Interrupt"
654 * - Fig. "Control Transfer Stage Transitions"
655 */
656
657 switch (stage) {
658 case READ_DATA_STAGE:
659 dcp->handler = &usbhsg_handler_send_ctrl;
660 break;
661 case WRITE_DATA_STAGE:
662 dcp->handler = &usbhsg_handler_recv_ctrl;
663 break;
664 case NODATA_STATUS_STAGE:
665 dcp->handler = &usbhsg_handler_ctrl_stage_end;
666 break;
667 default:
668 return ret;
669 }
670
671 /*
672 * get usb request
673 */
674 usbhs_usbreq_get_val(priv, &ctrl);
675
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;
681 break;
682 }
683 }
684
685 /*
686 * setup stage / run recip
687 */
688 if (recip_handler)
689 ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
690 else
691 ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
692
693 if (ret < 0)
694 usbhs_fifo_stall(pipe);
695
696 return ret;
697}
698
699static int usbhsg_irq_empty(struct usbhs_priv *priv,
700 struct usbhs_irq_state *irq_state)
701{
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);
706 int i, ret;
707
708 if (!irq_state->bempsts) {
709 dev_err(dev, "debug %s !!\n", __func__);
710 return -EIO;
711 }
712
713 dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
714
715 /*
716 * search interrupted "pipe"
717 * not "uep".
718 */
719 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
720 if (!(irq_state->bempsts & (1 << i)))
721 continue;
722
723 uep = usbhsg_pipe_to_uep(pipe);
724 ret = usbhsg_queue_handle(uep);
725 if (ret < 0)
726 dev_err(dev, "send error %d : %d\n", i, ret);
727 }
728
729 return 0;
730}
731
732static int usbhsg_irq_ready(struct usbhs_priv *priv,
733 struct usbhs_irq_state *irq_state)
734{
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);
739 int i, ret;
740
741 if (!irq_state->brdysts) {
742 dev_err(dev, "debug %s !!\n", __func__);
743 return -EIO;
744 }
745
746 dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
747
748 /*
749 * search interrupted "pipe"
750 * not "uep".
751 */
752 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
753 if (!(irq_state->brdysts & (1 << i)))
754 continue;
755
756 uep = usbhsg_pipe_to_uep(pipe);
757 ret = usbhsg_queue_handle(uep);
758 if (ret < 0)
759 dev_err(dev, "receive error %d : %d\n", i, ret);
760 }
761
762 return 0;
763}
764
765/*
766 *
767 * usb_dcp_ops
768 *
769 */
770static int usbhsg_dcp_enable(struct usbhsg_uep *uep)
771{
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;
775
776 /*
777 ********* assume under spin lock *********
778 */
779
780 pipe = usbhs_dcp_malloc(priv);
781 if (!pipe)
782 return -EIO;
783
784 uep->pipe = pipe;
785 uep->pipe->mod_private = uep;
786 INIT_LIST_HEAD(&uep->list);
787
788 return 0;
789}
790
791#define usbhsg_dcp_disable usbhsg_pipe_disable
792static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
793{
794 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
795 struct usbhsg_request *ureq;
796 int disable = 0;
797
798 /*
799 ********* assume under spin lock *********
800 */
801
802 usbhs_fifo_disable(pipe);
803
804 /*
805 * disable pipe irq
806 */
807 usbhsg_irq_empty_ctrl(uep, disable);
808 usbhsg_irq_ready_ctrl(uep, disable);
809
810 while (1) {
811 ureq = usbhsg_queue_get(uep);
812 if (!ureq)
813 break;
814
815 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
816 }
817
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900818 return 0;
819}
820
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900821static void usbhsg_uep_init(struct usbhsg_gpriv *gpriv)
822{
823 int i;
824 struct usbhsg_uep *uep;
825
826 usbhsg_for_each_uep_with_dcp(uep, gpriv, i)
827 uep->pipe = NULL;
828}
829
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900830/*
831 *
832 * usb_ep_ops
833 *
834 */
835static int usbhsg_ep_enable(struct usb_ep *ep,
836 const struct usb_endpoint_descriptor *desc)
837{
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;
Kuninori Morimotocb966322011-04-21 14:10:08 +0900842 spinlock_t *lock;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900843 unsigned long flags;
844 int ret = -EIO;
845
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900846 /*
847 * if it already have pipe,
848 * nothing to do
849 */
850 if (uep->pipe)
851 return 0;
852
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900853 /******************** spin lock ********************/
Kuninori Morimotocb966322011-04-21 14:10:08 +0900854 lock = usbhsg_trylock(gpriv, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900855
856 pipe = usbhs_pipe_malloc(priv, desc);
857 if (pipe) {
858 uep->pipe = pipe;
859 pipe->mod_private = uep;
860 INIT_LIST_HEAD(&uep->list);
861
862 if (usb_endpoint_dir_in(desc))
863 uep->handler = &usbhsg_handler_send_packet;
864 else
865 uep->handler = &usbhsg_handler_recv_packet;
866
867 ret = 0;
868 }
Kuninori Morimotocb966322011-04-21 14:10:08 +0900869
870 usbhsg_unlock(lock, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900871 /******************** spin unlock ******************/
872
873 return ret;
874}
875
876static int usbhsg_ep_disable(struct usb_ep *ep)
877{
878 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
879 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
Kuninori Morimotocb966322011-04-21 14:10:08 +0900880 spinlock_t *lock;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900881 unsigned long flags;
882 int ret;
883
884 /******************** spin lock ********************/
Kuninori Morimotocb966322011-04-21 14:10:08 +0900885 lock = usbhsg_trylock(gpriv, &flags);
886
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900887 ret = usbhsg_pipe_disable(uep);
Kuninori Morimotocb966322011-04-21 14:10:08 +0900888
889 usbhsg_unlock(lock, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900890 /******************** spin unlock ******************/
891
892 return ret;
893}
894
895static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
896 gfp_t gfp_flags)
897{
898 struct usbhsg_request *ureq;
899
900 ureq = kzalloc(sizeof *ureq, gfp_flags);
901 if (!ureq)
902 return NULL;
903
904 INIT_LIST_HEAD(&ureq->node);
905 return &ureq->req;
906}
907
908static void usbhsg_ep_free_request(struct usb_ep *ep,
909 struct usb_request *req)
910{
911 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
912
913 WARN_ON(!list_empty(&ureq->node));
914 kfree(ureq);
915}
916
917static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
918 gfp_t gfp_flags)
919{
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);
Kuninori Morimotocb966322011-04-21 14:10:08 +0900924 spinlock_t *lock;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900925 unsigned long flags;
926 int ret = 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900927
928 /*
929 * CAUTION [*endpoint queue*]
930 *
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.
936 *
Kuninori Morimotocb966322011-04-21 14:10:08 +0900937 * This function is using usbshg_trylock to solve this issue.
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900938 * 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.
940 * see also
941 * CAUTION [*queue handler*]
942 * CAUTION [*request complete*]
943 */
944
945 /******************** spin lock ********************/
Kuninori Morimotocb966322011-04-21 14:10:08 +0900946 lock = usbhsg_trylock(gpriv, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900947
948 /* param check */
949 if (usbhsg_is_not_connected(gpriv) ||
950 unlikely(!gpriv->driver) ||
951 unlikely(!pipe))
952 ret = -ESHUTDOWN;
953 else
954 usbhsg_queue_push(uep, ureq);
955
Kuninori Morimotocb966322011-04-21 14:10:08 +0900956 usbhsg_unlock(lock, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900957 /******************** spin unlock ******************/
958
959 usbhsg_queue_prepare(uep);
960
961 return ret;
962}
963
964static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
965{
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);
Kuninori Morimotocb966322011-04-21 14:10:08 +0900969 spinlock_t *lock;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900970 unsigned long flags;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900971
972 /*
973 * see
974 * CAUTION [*queue handler*]
975 * CAUTION [*endpoint queue*]
976 * CAUTION [*request complete*]
977 */
978
979 /******************** spin lock ********************/
Kuninori Morimotocb966322011-04-21 14:10:08 +0900980 lock = usbhsg_trylock(gpriv, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900981
982 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
983
Kuninori Morimotocb966322011-04-21 14:10:08 +0900984 usbhsg_unlock(lock, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900985 /******************** spin unlock ******************/
986
987 return 0;
988}
989
990static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
991{
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);
Kuninori Morimotocb966322011-04-21 14:10:08 +0900996 spinlock_t *lock;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900997 unsigned long flags;
998 int ret = -EAGAIN;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900999
1000 /*
1001 * see
1002 * CAUTION [*queue handler*]
1003 * CAUTION [*endpoint queue*]
1004 * CAUTION [*request complete*]
1005 */
1006
1007 /******************** spin lock ********************/
Kuninori Morimotocb966322011-04-21 14:10:08 +09001008 lock = usbhsg_trylock(gpriv, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001009 if (!usbhsg_queue_get(uep)) {
1010
1011 dev_dbg(dev, "set halt %d (pipe %d)\n",
1012 halt, usbhs_pipe_number(pipe));
1013
1014 if (halt)
1015 usbhs_fifo_stall(pipe);
1016 else
1017 usbhs_fifo_disable(pipe);
1018
1019 if (halt && wedge)
1020 usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
1021 else
1022 usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
1023
1024 ret = 0;
1025 }
1026
Kuninori Morimotocb966322011-04-21 14:10:08 +09001027 usbhsg_unlock(lock, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001028 /******************** spin unlock ******************/
1029
1030 return ret;
1031}
1032
1033static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
1034{
1035 return __usbhsg_ep_set_halt_wedge(ep, value, 0);
1036}
1037
1038static int usbhsg_ep_set_wedge(struct usb_ep *ep)
1039{
1040 return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
1041}
1042
1043static struct usb_ep_ops usbhsg_ep_ops = {
1044 .enable = usbhsg_ep_enable,
1045 .disable = usbhsg_ep_disable,
1046
1047 .alloc_request = usbhsg_ep_alloc_request,
1048 .free_request = usbhsg_ep_free_request,
1049
1050 .queue = usbhsg_ep_queue,
1051 .dequeue = usbhsg_ep_dequeue,
1052
1053 .set_halt = usbhsg_ep_set_halt,
1054 .set_wedge = usbhsg_ep_set_wedge,
1055};
1056
1057/*
1058 * usb module start/end
1059 */
1060static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
1061{
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);
Kuninori Morimotocb966322011-04-21 14:10:08 +09001066 spinlock_t *lock;
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001067 unsigned long flags;
1068
1069 /******************** spin lock ********************/
Kuninori Morimotocb966322011-04-21 14:10:08 +09001070 lock = usbhsg_trylock(gpriv, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001071
1072 /*
1073 * enable interrupt and systems if ready
1074 */
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;
1079
1080 dev_dbg(dev, "start gadget\n");
1081
1082 /*
1083 * pipe initialize and enable DCP
1084 */
1085 usbhs_pipe_init(priv);
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +09001086 usbhsg_uep_init(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001087 usbhsg_dcp_enable(dcp);
1088
1089 /*
1090 * system config enble
1091 * - HI speed
1092 * - function
1093 * - usb module
1094 */
1095 usbhs_sys_hispeed_ctrl(priv, 1);
1096 usbhs_sys_function_ctrl(priv, 1);
1097 usbhs_sys_usb_ctrl(priv, 1);
1098
1099 /*
1100 * enable irq callback
1101 */
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);
1109
1110usbhsg_try_start_unlock:
Kuninori Morimotocb966322011-04-21 14:10:08 +09001111 usbhsg_unlock(lock, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001112 /******************** spin unlock ********************/
1113
1114 return 0;
1115}
1116
1117static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
1118{
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);
Kuninori Morimotocb966322011-04-21 14:10:08 +09001123 spinlock_t *lock;
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001124 unsigned long flags;
1125
1126 /******************** spin lock ********************/
Kuninori Morimotocb966322011-04-21 14:10:08 +09001127 lock = usbhsg_trylock(gpriv, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001128
1129 /*
1130 * disable interrupt and systems if 1st try
1131 */
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;
1136
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);
1145
1146 usbhsg_dcp_disable(dcp);
1147
1148 gpriv->gadget.speed = USB_SPEED_UNKNOWN;
1149
1150 /* disable sys */
1151 usbhs_sys_hispeed_ctrl(priv, 0);
1152 usbhs_sys_function_ctrl(priv, 0);
1153 usbhs_sys_usb_ctrl(priv, 0);
1154
Kuninori Morimotocb966322011-04-21 14:10:08 +09001155 usbhsg_unlock(lock, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001156 /******************** spin unlock ********************/
1157
1158 if (gpriv->driver &&
1159 gpriv->driver->disconnect)
1160 gpriv->driver->disconnect(&gpriv->gadget);
1161
1162 dev_dbg(dev, "stop gadget\n");
1163
1164 return 0;
1165
1166usbhsg_try_stop_unlock:
Kuninori Morimotocb966322011-04-21 14:10:08 +09001167 usbhsg_unlock(lock, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001168
1169 return 0;
1170}
1171
1172/*
1173 *
1174 * linux usb function
1175 *
1176 */
1177struct usbhsg_gpriv *the_controller;
1178int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1179 int (*bind)(struct usb_gadget *))
1180{
1181 struct usbhsg_gpriv *gpriv = the_controller;
1182 struct usbhs_priv *priv;
1183 struct device *dev;
1184 int ret;
1185
1186 if (!bind ||
1187 !driver ||
1188 !driver->setup ||
1189 driver->speed != USB_SPEED_HIGH)
1190 return -EINVAL;
1191 if (!gpriv)
1192 return -ENODEV;
1193 if (gpriv->driver)
1194 return -EBUSY;
1195
1196 dev = usbhsg_gpriv_to_dev(gpriv);
1197 priv = usbhsg_gpriv_to_priv(gpriv);
1198
1199 /* first hook up the driver ... */
1200 gpriv->driver = driver;
1201 gpriv->gadget.dev.driver = &driver->driver;
1202
1203 ret = device_add(&gpriv->gadget.dev);
1204 if (ret) {
1205 dev_err(dev, "device_add error %d\n", ret);
1206 goto add_fail;
1207 }
1208
1209 ret = bind(&gpriv->gadget);
1210 if (ret) {
1211 dev_err(dev, "bind to driver %s error %d\n",
1212 driver->driver.name, ret);
1213 goto bind_fail;
1214 }
1215
1216 dev_dbg(dev, "bind %s\n", driver->driver.name);
1217
1218 return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
1219
1220bind_fail:
1221 device_del(&gpriv->gadget.dev);
1222add_fail:
1223 gpriv->driver = NULL;
1224 gpriv->gadget.dev.driver = NULL;
1225
1226 return ret;
1227}
1228EXPORT_SYMBOL(usb_gadget_probe_driver);
1229
1230int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1231{
1232 struct usbhsg_gpriv *gpriv = the_controller;
1233 struct usbhs_priv *priv;
1234 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
1235
1236 if (!gpriv)
1237 return -ENODEV;
1238
1239 if (!driver ||
1240 !driver->unbind ||
1241 driver != gpriv->driver)
1242 return -EINVAL;
1243
1244 dev = usbhsg_gpriv_to_dev(gpriv);
1245 priv = usbhsg_gpriv_to_priv(gpriv);
1246
1247 usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
1248 device_del(&gpriv->gadget.dev);
1249 gpriv->driver = NULL;
1250
1251 if (driver->disconnect)
1252 driver->disconnect(&gpriv->gadget);
1253
1254 driver->unbind(&gpriv->gadget);
1255 dev_dbg(dev, "unbind %s\n", driver->driver.name);
1256
1257 return 0;
1258}
1259EXPORT_SYMBOL(usb_gadget_unregister_driver);
1260
1261/*
1262 * usb gadget ops
1263 */
1264static int usbhsg_get_frame(struct usb_gadget *gadget)
1265{
1266 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
1267 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
1268
1269 return usbhs_frame_get_num(priv);
1270}
1271
1272static struct usb_gadget_ops usbhsg_gadget_ops = {
1273 .get_frame = usbhsg_get_frame,
1274};
1275
1276static int usbhsg_start(struct usbhs_priv *priv)
1277{
1278 return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
1279}
1280
1281static int usbhsg_stop(struct usbhs_priv *priv)
1282{
1283 return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
1284}
1285
1286int __devinit usbhs_mod_gadget_probe(struct usbhs_priv *priv)
1287{
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);
1292 int i;
1293
1294 gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
1295 if (!gpriv) {
1296 dev_err(dev, "Could not allocate gadget priv\n");
1297 return -ENOMEM;
1298 }
1299
1300 uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
1301 if (!uep) {
1302 dev_err(dev, "Could not allocate ep\n");
1303 goto usbhs_mod_gadget_probe_err_gpriv;
1304 }
1305
1306 /*
1307 * CAUTION
1308 *
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"
1312 */
1313
1314 /*
1315 * register itself
1316 */
1317 usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
1318
1319 /* init gpriv */
1320 gpriv->mod.name = "gadget";
1321 gpriv->mod.start = usbhsg_start;
1322 gpriv->mod.stop = usbhsg_stop;
1323 gpriv->uep = uep;
1324 gpriv->uep_size = pipe_size;
1325 usbhsg_status_init(gpriv);
1326
1327 /*
1328 * init gadget
1329 */
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;
1336
1337 INIT_LIST_HEAD(&gpriv->gadget.ep_list);
1338
1339 /*
1340 * init usb_ep
1341 */
1342 usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
1343 uep->gpriv = gpriv;
1344 snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
1345
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);
1350
1351 /* init DCP */
1352 if (usbhsg_is_dcp(uep)) {
1353 gpriv->gadget.ep0 = &uep->ep;
1354 uep->ep.maxpacket = 64;
1355 }
1356 /* init normal pipe */
1357 else {
1358 uep->ep.maxpacket = 512;
1359 list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
1360 }
1361 }
1362
1363 the_controller = gpriv;
1364
1365 dev_info(dev, "gadget probed\n");
1366
1367 return 0;
1368
1369usbhs_mod_gadget_probe_err_gpriv:
1370 kfree(gpriv);
1371
1372 return -ENOMEM;
1373}
1374
1375void __devexit usbhs_mod_gadget_remove(struct usbhs_priv *priv)
1376{
1377 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1378
1379 kfree(gpriv);
1380}