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