Merge tag 'usb-for-v3.19' of git://git.kernel.org/pub/scm/linux/kernel/git/balbi...
[deliverable/linux.git] / drivers / usb / renesas_usbhs / mod_gadget.c
CommitLineData
2f98382d
KM
1/*
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 */
dfbb7f4f 17#include <linux/delay.h>
d128a259 18#include <linux/dma-mapping.h>
2f98382d
KM
19#include <linux/io.h>
20#include <linux/module.h>
21#include <linux/platform_device.h>
22#include <linux/usb/ch9.h>
23#include <linux/usb/gadget.h>
24#include "common.h"
25
26/*
27 * struct
28 */
29struct usbhsg_request {
30 struct usb_request req;
4bd04811 31 struct usbhs_pkt pkt;
2f98382d
KM
32};
33
34#define EP_NAME_SIZE 8
35struct usbhsg_gpriv;
2f98382d
KM
36struct usbhsg_uep {
37 struct usb_ep ep;
38 struct usbhs_pipe *pipe;
2f98382d
KM
39
40 char ep_name[EP_NAME_SIZE];
41
42 struct usbhsg_gpriv *gpriv;
2f98382d
KM
43};
44
45struct usbhsg_gpriv {
46 struct usb_gadget gadget;
47 struct usbhs_mod mod;
48
49 struct usbhsg_uep *uep;
50 int uep_size;
51
52 struct usb_gadget_driver *driver;
53
54 u32 status;
55#define USBHSG_STATUS_STARTED (1 << 0)
56#define USBHSG_STATUS_REGISTERD (1 << 1)
57#define USBHSG_STATUS_WEDGE (1 << 2)
cac402dd 58#define USBHSG_STATUS_SELF_POWERED (1 << 3)
04a5def3 59#define USBHSG_STATUS_SOFT_CONNECT (1 << 4)
2f98382d
KM
60};
61
2f98382d
KM
62struct usbhsg_recip_handle {
63 char *name;
64 int (*device)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
65 struct usb_ctrlrequest *ctrl);
66 int (*interface)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
67 struct usb_ctrlrequest *ctrl);
68 int (*endpoint)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
69 struct usb_ctrlrequest *ctrl);
70};
71
72/*
73 * macro
74 */
75#define usbhsg_priv_to_gpriv(priv) \
76 container_of( \
77 usbhs_mod_get(priv, USBHS_GADGET), \
78 struct usbhsg_gpriv, mod)
79
80#define __usbhsg_for_each_uep(start, pos, g, i) \
925403f4
KM
81 for ((i) = start; \
82 ((i) < (g)->uep_size) && ((pos) = (g)->uep + (i)); \
83 (i)++)
2f98382d
KM
84
85#define usbhsg_for_each_uep(pos, gpriv, i) \
86 __usbhsg_for_each_uep(1, pos, gpriv, i)
87
88#define usbhsg_for_each_uep_with_dcp(pos, gpriv, i) \
89 __usbhsg_for_each_uep(0, pos, gpriv, i)
90
91#define usbhsg_gadget_to_gpriv(g)\
92 container_of(g, struct usbhsg_gpriv, gadget)
93
94#define usbhsg_req_to_ureq(r)\
95 container_of(r, struct usbhsg_request, req)
96
97#define usbhsg_ep_to_uep(e) container_of(e, struct usbhsg_uep, ep)
2f98382d
KM
98#define usbhsg_gpriv_to_dev(gp) usbhs_priv_to_dev((gp)->mod.priv)
99#define usbhsg_gpriv_to_priv(gp) ((gp)->mod.priv)
100#define usbhsg_gpriv_to_dcp(gp) ((gp)->uep)
101#define usbhsg_gpriv_to_nth_uep(gp, i) ((gp)->uep + i)
102#define usbhsg_uep_to_gpriv(u) ((u)->gpriv)
103#define usbhsg_uep_to_pipe(u) ((u)->pipe)
104#define usbhsg_pipe_to_uep(p) ((p)->mod_private)
105#define usbhsg_is_dcp(u) ((u) == usbhsg_gpriv_to_dcp((u)->gpriv))
106
4bd04811
KM
107#define usbhsg_ureq_to_pkt(u) (&(u)->pkt)
108#define usbhsg_pkt_to_ureq(i) \
109 container_of(i, struct usbhsg_request, pkt)
110
2f98382d
KM
111#define usbhsg_is_not_connected(gp) ((gp)->gadget.speed == USB_SPEED_UNKNOWN)
112
113/* status */
114#define usbhsg_status_init(gp) do {(gp)->status = 0; } while (0)
115#define usbhsg_status_set(gp, b) (gp->status |= b)
116#define usbhsg_status_clr(gp, b) (gp->status &= ~b)
117#define usbhsg_status_has(gp, b) (gp->status & b)
118
119/*
233f519d 120 * queue push/pop
2f98382d 121 */
2f98382d
KM
122static void usbhsg_queue_pop(struct usbhsg_uep *uep,
123 struct usbhsg_request *ureq,
124 int status)
125{
126 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
127 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
128 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
2f98382d
KM
129
130 dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
131
2f98382d 132 ureq->req.status = status;
304f7e5e 133 usb_gadget_giveback_request(&uep->ep, &ureq->req);
2f98382d
KM
134}
135
2cc97197 136static void usbhsg_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt)
2f98382d 137{
4bd04811
KM
138 struct usbhs_pipe *pipe = pkt->pipe;
139 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
140 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
2f98382d 141
659d4954 142 ureq->req.actual = pkt->actual;
2f98382d 143
659d4954 144 usbhsg_queue_pop(uep, ureq, 0);
4bd04811
KM
145}
146
b331872b
KM
147static void usbhsg_queue_push(struct usbhsg_uep *uep,
148 struct usbhsg_request *ureq)
149{
150 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
151 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
152 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
153 struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
154 struct usb_request *req = &ureq->req;
155
156 req->actual = 0;
157 req->status = -EINPROGRESS;
158 usbhs_pkt_push(pipe, pkt, usbhsg_queue_done,
3edeee38 159 req->buf, req->length, req->zero, -1);
654c35ab 160 usbhs_pkt_start(pipe);
b331872b
KM
161
162 dev_dbg(dev, "pipe %d : queue push (%d)\n",
163 usbhs_pipe_number(pipe),
164 req->length);
165}
166
233f519d
KM
167/*
168 * dma map/unmap
169 */
ade78f9f 170static int usbhsg_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
e73a9891
KM
171{
172 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
173 struct usb_request *req = &ureq->req;
e73a9891
KM
174 struct usbhs_pipe *pipe = pkt->pipe;
175 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
176 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
e73a9891 177 enum dma_data_direction dir;
ade78f9f 178 int ret = 0;
e73a9891 179
ade78f9f 180 dir = usbhs_pipe_is_dir_host(pipe);
e73a9891 181
ade78f9f
FB
182 if (map) {
183 /* it can not use scatter/gather */
184 WARN_ON(req->num_sgs);
185
186 ret = usb_gadget_map_request(&gpriv->gadget, req, dir);
187 if (ret < 0)
188 return ret;
189
190 pkt->dma = req->dma;
191 } else {
192 usb_gadget_unmap_request(&gpriv->gadget, req, dir);
193 }
194
195 return ret;
e73a9891
KM
196}
197
2f98382d
KM
198/*
199 * USB_TYPE_STANDARD / clear feature functions
200 */
201static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
202 struct usbhsg_uep *uep,
203 struct usb_ctrlrequest *ctrl)
204{
205 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
206 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
207 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
208
209 usbhs_dcp_control_transfer_done(pipe);
210
211 return 0;
212}
213
214static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
215 struct usbhsg_uep *uep,
216 struct usb_ctrlrequest *ctrl)
217{
218 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
219 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
220
221 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
e8d548d5 222 usbhs_pipe_disable(pipe);
6e6db82b 223 usbhs_pipe_sequence_data0(pipe);
e8d548d5 224 usbhs_pipe_enable(pipe);
2f98382d
KM
225 }
226
227 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
228
25fa7079
KM
229 usbhs_pkt_start(pipe);
230
2f98382d
KM
231 return 0;
232}
233
225da3e3 234static struct usbhsg_recip_handle req_clear_feature = {
2f98382d
KM
235 .name = "clear feature",
236 .device = usbhsg_recip_handler_std_control_done,
237 .interface = usbhsg_recip_handler_std_control_done,
238 .endpoint = usbhsg_recip_handler_std_clear_endpoint,
239};
240
ced6e09e
KM
241/*
242 * USB_TYPE_STANDARD / set feature functions
243 */
dfbb7f4f
KM
244static int usbhsg_recip_handler_std_set_device(struct usbhs_priv *priv,
245 struct usbhsg_uep *uep,
246 struct usb_ctrlrequest *ctrl)
247{
248 switch (le16_to_cpu(ctrl->wValue)) {
249 case USB_DEVICE_TEST_MODE:
250 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
251 udelay(100);
252 usbhs_sys_set_test_mode(priv, le16_to_cpu(ctrl->wIndex >> 8));
253 break;
254 default:
255 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
256 break;
257 }
258
259 return 0;
260}
261
ced6e09e
KM
262static int usbhsg_recip_handler_std_set_endpoint(struct usbhs_priv *priv,
263 struct usbhsg_uep *uep,
264 struct usb_ctrlrequest *ctrl)
265{
266 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
267
268 usbhs_pipe_stall(pipe);
269
270 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
271
272 return 0;
273}
274
225da3e3 275static struct usbhsg_recip_handle req_set_feature = {
ced6e09e 276 .name = "set feature",
dfbb7f4f 277 .device = usbhsg_recip_handler_std_set_device,
ced6e09e
KM
278 .interface = usbhsg_recip_handler_std_control_done,
279 .endpoint = usbhsg_recip_handler_std_set_endpoint,
280};
281
17f7f769
KM
282/*
283 * USB_TYPE_STANDARD / get status functions
284 */
285static void __usbhsg_recip_send_complete(struct usb_ep *ep,
286 struct usb_request *req)
287{
288 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
289
290 /* free allocated recip-buffer/usb_request */
291 kfree(ureq->pkt.buf);
292 usb_ep_free_request(ep, req);
293}
294
295static void __usbhsg_recip_send_status(struct usbhsg_gpriv *gpriv,
296 unsigned short status)
297{
298 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
299 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
300 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
301 struct usb_request *req;
302 unsigned short *buf;
303
304 /* alloc new usb_request for recip */
305 req = usb_ep_alloc_request(&dcp->ep, GFP_ATOMIC);
306 if (!req) {
307 dev_err(dev, "recip request allocation fail\n");
308 return;
309 }
310
311 /* alloc recip data buffer */
312 buf = kmalloc(sizeof(*buf), GFP_ATOMIC);
313 if (!buf) {
314 usb_ep_free_request(&dcp->ep, req);
315 dev_err(dev, "recip data allocation fail\n");
316 return;
317 }
318
319 /* recip data is status */
320 *buf = cpu_to_le16(status);
321
322 /* allocated usb_request/buffer will be freed */
323 req->complete = __usbhsg_recip_send_complete;
324 req->buf = buf;
325 req->length = sizeof(*buf);
326 req->zero = 0;
327
328 /* push packet */
329 pipe->handler = &usbhs_fifo_pio_push_handler;
330 usbhsg_queue_push(dcp, usbhsg_req_to_ureq(req));
331}
332
333static int usbhsg_recip_handler_std_get_device(struct usbhs_priv *priv,
334 struct usbhsg_uep *uep,
335 struct usb_ctrlrequest *ctrl)
336{
337 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
cac402dd
SY
338 unsigned short status = 0;
339
340 if (usbhsg_status_has(gpriv, USBHSG_STATUS_SELF_POWERED))
341 status = 1 << USB_DEVICE_SELF_POWERED;
17f7f769
KM
342
343 __usbhsg_recip_send_status(gpriv, status);
344
345 return 0;
346}
347
348static int usbhsg_recip_handler_std_get_interface(struct usbhs_priv *priv,
349 struct usbhsg_uep *uep,
350 struct usb_ctrlrequest *ctrl)
351{
352 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
353 unsigned short status = 0;
354
355 __usbhsg_recip_send_status(gpriv, status);
356
357 return 0;
358}
359
360static int usbhsg_recip_handler_std_get_endpoint(struct usbhs_priv *priv,
361 struct usbhsg_uep *uep,
362 struct usb_ctrlrequest *ctrl)
363{
364 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
365 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
366 unsigned short status = 0;
367
368 if (usbhs_pipe_is_stall(pipe))
369 status = 1 << USB_ENDPOINT_HALT;
370
371 __usbhsg_recip_send_status(gpriv, status);
372
373 return 0;
374}
375
225da3e3 376static struct usbhsg_recip_handle req_get_status = {
17f7f769
KM
377 .name = "get status",
378 .device = usbhsg_recip_handler_std_get_device,
379 .interface = usbhsg_recip_handler_std_get_interface,
380 .endpoint = usbhsg_recip_handler_std_get_endpoint,
381};
382
2f98382d
KM
383/*
384 * USB_TYPE handler
385 */
386static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
387 struct usbhsg_recip_handle *handler,
388 struct usb_ctrlrequest *ctrl)
389{
390 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
391 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
392 struct usbhsg_uep *uep;
0432eed0 393 struct usbhs_pipe *pipe;
2f98382d
KM
394 int recip = ctrl->bRequestType & USB_RECIP_MASK;
395 int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
7983bc74 396 int ret = 0;
2f98382d
KM
397 int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
398 struct usb_ctrlrequest *ctrl);
399 char *msg;
400
401 uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
0432eed0
KM
402 pipe = usbhsg_uep_to_pipe(uep);
403 if (!pipe) {
9a28b7bd 404 dev_err(dev, "wrong recip request\n");
25fa7079 405 return -EINVAL;
9a28b7bd 406 }
2f98382d
KM
407
408 switch (recip) {
409 case USB_RECIP_DEVICE:
410 msg = "DEVICE";
411 func = handler->device;
412 break;
413 case USB_RECIP_INTERFACE:
414 msg = "INTERFACE";
415 func = handler->interface;
416 break;
417 case USB_RECIP_ENDPOINT:
418 msg = "ENDPOINT";
419 func = handler->endpoint;
420 break;
421 default:
422 dev_warn(dev, "unsupported RECIP(%d)\n", recip);
423 func = NULL;
424 ret = -EINVAL;
425 }
426
427 if (func) {
428 dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
429 ret = func(priv, uep, ctrl);
430 }
431
432 return ret;
433}
434
435/*
436 * irq functions
437 *
438 * it will be called from usbhs_interrupt
439 */
440static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
441 struct usbhs_irq_state *irq_state)
442{
443 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
444 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
445
75587f52 446 gpriv->gadget.speed = usbhs_bus_get_speed(priv);
2f98382d
KM
447
448 dev_dbg(dev, "state = %x : speed : %d\n",
449 usbhs_status_get_device_state(irq_state),
450 gpriv->gadget.speed);
451
452 return 0;
453}
454
455static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
456 struct usbhs_irq_state *irq_state)
457{
458 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
459 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
460 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
461 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
462 struct usb_ctrlrequest ctrl;
463 struct usbhsg_recip_handle *recip_handler = NULL;
464 int stage = usbhs_status_get_ctrl_stage(irq_state);
465 int ret = 0;
466
467 dev_dbg(dev, "stage = %d\n", stage);
468
469 /*
470 * see Manual
471 *
472 * "Operation"
473 * - "Interrupt Function"
474 * - "Control Transfer Stage Transition Interrupt"
475 * - Fig. "Control Transfer Stage Transitions"
476 */
477
478 switch (stage) {
479 case READ_DATA_STAGE:
0c6ef985 480 pipe->handler = &usbhs_fifo_pio_push_handler;
2f98382d
KM
481 break;
482 case WRITE_DATA_STAGE:
0c6ef985 483 pipe->handler = &usbhs_fifo_pio_pop_handler;
2f98382d
KM
484 break;
485 case NODATA_STATUS_STAGE:
0c6ef985 486 pipe->handler = &usbhs_ctrl_stage_end_handler;
2f98382d 487 break;
4ef35b10
YS
488 case READ_STATUS_STAGE:
489 case WRITE_STATUS_STAGE:
490 usbhs_dcp_control_transfer_done(pipe);
2f98382d
KM
491 default:
492 return ret;
493 }
494
495 /*
496 * get usb request
497 */
498 usbhs_usbreq_get_val(priv, &ctrl);
499
500 switch (ctrl.bRequestType & USB_TYPE_MASK) {
501 case USB_TYPE_STANDARD:
502 switch (ctrl.bRequest) {
503 case USB_REQ_CLEAR_FEATURE:
504 recip_handler = &req_clear_feature;
505 break;
ced6e09e
KM
506 case USB_REQ_SET_FEATURE:
507 recip_handler = &req_set_feature;
508 break;
17f7f769
KM
509 case USB_REQ_GET_STATUS:
510 recip_handler = &req_get_status;
511 break;
2f98382d
KM
512 }
513 }
514
515 /*
516 * setup stage / run recip
517 */
518 if (recip_handler)
519 ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
520 else
521 ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
522
523 if (ret < 0)
e8d548d5 524 usbhs_pipe_stall(pipe);
2f98382d
KM
525
526 return ret;
527}
528
2f98382d
KM
529/*
530 *
531 * usb_dcp_ops
532 *
533 */
2f98382d
KM
534static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
535{
536 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
8a2c225d 537 struct usbhs_pkt *pkt;
2f98382d 538
2f98382d 539 while (1) {
97664a20 540 pkt = usbhs_pkt_pop(pipe, NULL);
8a2c225d 541 if (!pkt)
2f98382d 542 break;
91b158f4
KM
543
544 usbhsg_queue_pop(uep, usbhsg_pkt_to_ureq(pkt), -ECONNRESET);
2f98382d
KM
545 }
546
91b158f4
KM
547 usbhs_pipe_disable(pipe);
548
2f98382d
KM
549 return 0;
550}
551
552/*
553 *
554 * usb_ep_ops
555 *
556 */
557static int usbhsg_ep_enable(struct usb_ep *ep,
558 const struct usb_endpoint_descriptor *desc)
559{
560 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
561 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
562 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
563 struct usbhs_pipe *pipe;
2f98382d
KM
564 int ret = -EIO;
565
409ba9e7
KM
566 /*
567 * if it already have pipe,
568 * nothing to do
569 */
08e6c611
KM
570 if (uep->pipe) {
571 usbhs_pipe_clear(uep->pipe);
6e6db82b 572 usbhs_pipe_sequence_data0(uep->pipe);
409ba9e7 573 return 0;
08e6c611 574 }
409ba9e7 575
f5aa889f
KM
576 pipe = usbhs_pipe_malloc(priv,
577 usb_endpoint_type(desc),
578 usb_endpoint_dir_in(desc));
2f98382d
KM
579 if (pipe) {
580 uep->pipe = pipe;
581 pipe->mod_private = uep;
2f98382d 582
f5aa889f 583 /* set epnum / maxp */
bc6fbf59 584 usbhs_pipe_config_update(pipe, 0,
f5aa889f
KM
585 usb_endpoint_num(desc),
586 usb_endpoint_maxp(desc));
587
233f519d
KM
588 /*
589 * usbhs_fifo_dma_push/pop_handler try to
590 * use dmaengine if possible.
591 * It will use pio handler if impossible.
592 */
2f98382d 593 if (usb_endpoint_dir_in(desc))
0c6ef985 594 pipe->handler = &usbhs_fifo_dma_push_handler;
2f98382d 595 else
0c6ef985 596 pipe->handler = &usbhs_fifo_dma_pop_handler;
2f98382d
KM
597
598 ret = 0;
599 }
cb96632c 600
2f98382d
KM
601 return ret;
602}
603
604static int usbhsg_ep_disable(struct usb_ep *ep)
605{
606 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
dfb87b8b 607 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
2f98382d 608
11432050
KM
609 if (!pipe)
610 return -EINVAL;
611
d9fa298f 612 usbhsg_pipe_disable(uep);
dfb87b8b 613 usbhs_pipe_free(pipe);
d9fa298f
KM
614
615 uep->pipe->mod_private = NULL;
616 uep->pipe = NULL;
617
618 return 0;
2f98382d
KM
619}
620
621static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
622 gfp_t gfp_flags)
623{
624 struct usbhsg_request *ureq;
625
626 ureq = kzalloc(sizeof *ureq, gfp_flags);
627 if (!ureq)
628 return NULL;
629
6acb95d4
KM
630 usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
631
2f98382d
KM
632 return &ureq->req;
633}
634
635static void usbhsg_ep_free_request(struct usb_ep *ep,
636 struct usb_request *req)
637{
638 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
639
6acb95d4 640 WARN_ON(!list_empty(&ureq->pkt.node));
2f98382d
KM
641 kfree(ureq);
642}
643
644static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
645 gfp_t gfp_flags)
646{
647 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
648 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
649 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
650 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
2f98382d
KM
651
652 /* param check */
653 if (usbhsg_is_not_connected(gpriv) ||
654 unlikely(!gpriv->driver) ||
655 unlikely(!pipe))
97664a20 656 return -ESHUTDOWN;
2f98382d 657
97664a20 658 usbhsg_queue_push(uep, ureq);
2f98382d 659
97664a20 660 return 0;
2f98382d
KM
661}
662
663static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
664{
665 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
666 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
97664a20 667 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
2f98382d 668
97664a20 669 usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq));
2f98382d
KM
670 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
671
2f98382d
KM
672 return 0;
673}
674
675static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
676{
677 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
678 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
679 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
97664a20 680 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
2f98382d 681 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
2f98382d 682 unsigned long flags;
2f98382d 683
97664a20 684 usbhsg_pipe_disable(uep);
2f98382d 685
97664a20
KM
686 dev_dbg(dev, "set halt %d (pipe %d)\n",
687 halt, usbhs_pipe_number(pipe));
2f98382d 688
97664a20
KM
689 /******************** spin lock ********************/
690 usbhs_lock(priv, flags);
2f98382d 691
97664a20
KM
692 if (halt)
693 usbhs_pipe_stall(pipe);
694 else
695 usbhs_pipe_disable(pipe);
2f98382d 696
97664a20
KM
697 if (halt && wedge)
698 usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
699 else
700 usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
2f98382d 701
97664a20 702 usbhs_unlock(priv, flags);
2f98382d
KM
703 /******************** spin unlock ******************/
704
97664a20 705 return 0;
2f98382d
KM
706}
707
708static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
709{
710 return __usbhsg_ep_set_halt_wedge(ep, value, 0);
711}
712
713static int usbhsg_ep_set_wedge(struct usb_ep *ep)
714{
715 return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
716}
717
718static struct usb_ep_ops usbhsg_ep_ops = {
719 .enable = usbhsg_ep_enable,
720 .disable = usbhsg_ep_disable,
721
722 .alloc_request = usbhsg_ep_alloc_request,
723 .free_request = usbhsg_ep_free_request,
724
725 .queue = usbhsg_ep_queue,
726 .dequeue = usbhsg_ep_dequeue,
727
728 .set_halt = usbhsg_ep_set_halt,
729 .set_wedge = usbhsg_ep_set_wedge,
730};
731
04a5def3
TK
732/*
733 * pullup control
734 */
735static int usbhsg_can_pullup(struct usbhs_priv *priv)
736{
737 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
738
739 return gpriv->driver &&
740 usbhsg_status_has(gpriv, USBHSG_STATUS_SOFT_CONNECT);
741}
742
743static void usbhsg_update_pullup(struct usbhs_priv *priv)
744{
745 if (usbhsg_can_pullup(priv))
746 usbhs_sys_function_pullup(priv, 1);
747 else
748 usbhs_sys_function_pullup(priv, 0);
749}
750
2f98382d
KM
751/*
752 * usb module start/end
753 */
754static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
755{
756 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
757 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
758 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
759 struct device *dev = usbhs_priv_to_dev(priv);
2f98382d 760 unsigned long flags;
97664a20 761 int ret = 0;
2f98382d
KM
762
763 /******************** spin lock ********************/
97664a20 764 usbhs_lock(priv, flags);
2f98382d 765
2f98382d
KM
766 usbhsg_status_set(gpriv, status);
767 if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
768 usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
97664a20
KM
769 ret = -1; /* not ready */
770
771 usbhs_unlock(priv, flags);
772 /******************** spin unlock ********************/
773
774 if (ret < 0)
775 return 0; /* not ready is not error */
2f98382d 776
97664a20
KM
777 /*
778 * enable interrupt and systems if ready
779 */
2f98382d
KM
780 dev_dbg(dev, "start gadget\n");
781
782 /*
783 * pipe initialize and enable DCP
784 */
cdeb7943 785 usbhs_fifo_init(priv);
4bd04811 786 usbhs_pipe_init(priv,
e73a9891 787 usbhsg_dma_map_ctrl);
97664a20 788
d9fa298f 789 /* dcp init instead of usbhsg_ep_enable() */
97664a20
KM
790 dcp->pipe = usbhs_dcp_malloc(priv);
791 dcp->pipe->mod_private = dcp;
bc6fbf59 792 usbhs_pipe_config_update(dcp->pipe, 0, 0, 64);
2f98382d
KM
793
794 /*
795 * system config enble
796 * - HI speed
797 * - function
798 * - usb module
799 */
2f98382d 800 usbhs_sys_function_ctrl(priv, 1);
04a5def3 801 usbhsg_update_pullup(priv);
2f98382d
KM
802
803 /*
804 * enable irq callback
805 */
806 mod->irq_dev_state = usbhsg_irq_dev_state;
807 mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
2f98382d
KM
808 usbhs_irq_callback_update(priv, mod);
809
2f98382d
KM
810 return 0;
811}
812
813static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
814{
815 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
816 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
817 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
818 struct device *dev = usbhs_priv_to_dev(priv);
2f98382d 819 unsigned long flags;
97664a20 820 int ret = 0;
2f98382d
KM
821
822 /******************** spin lock ********************/
97664a20 823 usbhs_lock(priv, flags);
2f98382d 824
2f98382d
KM
825 usbhsg_status_clr(gpriv, status);
826 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
827 !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
97664a20
KM
828 ret = -1; /* already done */
829
830 usbhs_unlock(priv, flags);
831 /******************** spin unlock ********************/
832
833 if (ret < 0)
834 return 0; /* already done is not error */
2f98382d 835
97664a20
KM
836 /*
837 * disable interrupt and systems if 1st try
838 */
dad67397
KM
839 usbhs_fifo_quit(priv);
840
2f98382d
KM
841 /* disable all irq */
842 mod->irq_dev_state = NULL;
843 mod->irq_ctrl_stage = NULL;
2f98382d
KM
844 usbhs_irq_callback_update(priv, mod);
845
2f98382d
KM
846 gpriv->gadget.speed = USB_SPEED_UNKNOWN;
847
848 /* disable sys */
dfbb7f4f 849 usbhs_sys_set_test_mode(priv, 0);
2f98382d 850 usbhs_sys_function_ctrl(priv, 0);
2f98382d 851
d9fa298f 852 usbhsg_ep_disable(&dcp->ep);
2f98382d 853
2f98382d
KM
854 dev_dbg(dev, "stop gadget\n");
855
2f98382d
KM
856 return 0;
857}
858
859/*
860 *
861 * linux usb function
862 *
863 */
af1d7056
FB
864static int usbhsg_gadget_start(struct usb_gadget *gadget,
865 struct usb_gadget_driver *driver)
2f98382d 866{
af1d7056 867 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
f8eff0a0 868 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
2f98382d 869
af1d7056 870 if (!driver ||
2f98382d 871 !driver->setup ||
7177aed4 872 driver->max_speed < USB_SPEED_FULL)
2f98382d 873 return -EINVAL;
3b872188 874
2f98382d
KM
875 /* first hook up the driver ... */
876 gpriv->driver = driver;
2f98382d 877
2f98382d 878 return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
2f98382d 879}
2f98382d 880
22835b80 881static int usbhsg_gadget_stop(struct usb_gadget *gadget)
2f98382d 882{
af1d7056 883 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
f8eff0a0 884 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
2f98382d 885
2f98382d 886 usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
2f98382d
KM
887 gpriv->driver = NULL;
888
2f98382d
KM
889 return 0;
890}
2f98382d
KM
891
892/*
893 * usb gadget ops
894 */
895static int usbhsg_get_frame(struct usb_gadget *gadget)
896{
897 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
898 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
899
900 return usbhs_frame_get_num(priv);
901}
902
4cd2f599 903static int usbhsg_pullup(struct usb_gadget *gadget, int is_on)
904{
905 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
906 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
04a5def3 907 unsigned long flags;
4cd2f599 908
04a5def3
TK
909 usbhs_lock(priv, flags);
910 if (is_on)
911 usbhsg_status_set(gpriv, USBHSG_STATUS_SOFT_CONNECT);
912 else
913 usbhsg_status_clr(gpriv, USBHSG_STATUS_SOFT_CONNECT);
914 usbhsg_update_pullup(priv);
915 usbhs_unlock(priv, flags);
4cd2f599 916
917 return 0;
918}
919
cac402dd
SY
920static int usbhsg_set_selfpowered(struct usb_gadget *gadget, int is_self)
921{
922 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
923
924 if (is_self)
925 usbhsg_status_set(gpriv, USBHSG_STATUS_SELF_POWERED);
926 else
927 usbhsg_status_clr(gpriv, USBHSG_STATUS_SELF_POWERED);
928
929 return 0;
930}
931
eeef4587 932static const struct usb_gadget_ops usbhsg_gadget_ops = {
2f98382d 933 .get_frame = usbhsg_get_frame,
cac402dd 934 .set_selfpowered = usbhsg_set_selfpowered,
af1d7056
FB
935 .udc_start = usbhsg_gadget_start,
936 .udc_stop = usbhsg_gadget_stop,
4cd2f599 937 .pullup = usbhsg_pullup,
2f98382d
KM
938};
939
940static int usbhsg_start(struct usbhs_priv *priv)
941{
942 return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
943}
944
945static int usbhsg_stop(struct usbhs_priv *priv)
946{
8885a897
KM
947 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
948
949 /* cable disconnect */
950 if (gpriv->driver &&
951 gpriv->driver->disconnect)
952 gpriv->driver->disconnect(&gpriv->gadget);
953
2f98382d
KM
954 return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
955}
956
b7a8d17d 957int usbhs_mod_gadget_probe(struct usbhs_priv *priv)
2f98382d
KM
958{
959 struct usbhsg_gpriv *gpriv;
960 struct usbhsg_uep *uep;
961 struct device *dev = usbhs_priv_to_dev(priv);
962 int pipe_size = usbhs_get_dparam(priv, pipe_size);
963 int i;
0f91349b 964 int ret;
2f98382d
KM
965
966 gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
967 if (!gpriv) {
968 dev_err(dev, "Could not allocate gadget priv\n");
969 return -ENOMEM;
970 }
971
972 uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
973 if (!uep) {
974 dev_err(dev, "Could not allocate ep\n");
0f91349b 975 ret = -ENOMEM;
2f98382d
KM
976 goto usbhs_mod_gadget_probe_err_gpriv;
977 }
978
979 /*
980 * CAUTION
981 *
982 * There is no guarantee that it is possible to access usb module here.
983 * Don't accesses to it.
984 * The accesse will be enable after "usbhsg_start"
985 */
986
987 /*
988 * register itself
989 */
990 usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
991
992 /* init gpriv */
993 gpriv->mod.name = "gadget";
994 gpriv->mod.start = usbhsg_start;
995 gpriv->mod.stop = usbhsg_stop;
996 gpriv->uep = uep;
997 gpriv->uep_size = pipe_size;
998 usbhsg_status_init(gpriv);
999
1000 /*
1001 * init gadget
1002 */
2f98382d
KM
1003 gpriv->gadget.dev.parent = dev;
1004 gpriv->gadget.name = "renesas_usbhs_udc";
1005 gpriv->gadget.ops = &usbhsg_gadget_ops;
d327ab5b 1006 gpriv->gadget.max_speed = USB_SPEED_HIGH;
2f98382d
KM
1007
1008 INIT_LIST_HEAD(&gpriv->gadget.ep_list);
1009
1010 /*
1011 * init usb_ep
1012 */
1013 usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
1014 uep->gpriv = gpriv;
58482945 1015 uep->pipe = NULL;
2f98382d
KM
1016 snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
1017
1018 uep->ep.name = uep->ep_name;
1019 uep->ep.ops = &usbhsg_ep_ops;
1020 INIT_LIST_HEAD(&uep->ep.ep_list);
2f98382d
KM
1021
1022 /* init DCP */
1023 if (usbhsg_is_dcp(uep)) {
1024 gpriv->gadget.ep0 = &uep->ep;
e117e742 1025 usb_ep_set_maxpacket_limit(&uep->ep, 64);
2f98382d
KM
1026 }
1027 /* init normal pipe */
1028 else {
e117e742 1029 usb_ep_set_maxpacket_limit(&uep->ep, 512);
2f98382d
KM
1030 list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
1031 }
1032 }
1033
0f91349b
SAS
1034 ret = usb_add_gadget_udc(dev, &gpriv->gadget);
1035 if (ret)
0972ef71 1036 goto err_add_udc;
0f91349b
SAS
1037
1038
2f98382d
KM
1039 dev_info(dev, "gadget probed\n");
1040
1041 return 0;
f8eff0a0 1042
0f91349b
SAS
1043err_add_udc:
1044 kfree(gpriv->uep);
2f98382d
KM
1045
1046usbhs_mod_gadget_probe_err_gpriv:
1047 kfree(gpriv);
1048
0f91349b 1049 return ret;
2f98382d
KM
1050}
1051
b7a8d17d 1052void usbhs_mod_gadget_remove(struct usbhs_priv *priv)
2f98382d
KM
1053{
1054 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1055
0f91349b 1056 usb_del_gadget_udc(&gpriv->gadget);
3b872188 1057
3af51ac9 1058 kfree(gpriv->uep);
2f98382d
KM
1059 kfree(gpriv);
1060}
This page took 0.2719 seconds and 5 git commands to generate.