]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/usb/renesas_usbhs/mod_gadget.c
Merge remote-tracking branches 'regulator/topic/devm' and 'regulator/topic/stub'...
[mirror_ubuntu-bionic-kernel.git] / drivers / usb / renesas_usbhs / mod_gadget.c
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 */
17 #include <linux/delay.h>
18 #include <linux/dma-mapping.h>
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 */
29 struct usbhsg_request {
30 struct usb_request req;
31 struct usbhs_pkt pkt;
32 };
33
34 #define EP_NAME_SIZE 8
35 struct usbhsg_gpriv;
36 struct usbhsg_uep {
37 struct usb_ep ep;
38 struct usbhs_pipe *pipe;
39
40 char ep_name[EP_NAME_SIZE];
41
42 struct usbhsg_gpriv *gpriv;
43 };
44
45 struct 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
60 struct 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) \
79 for (i = start, pos = (g)->uep + i; \
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)
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
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
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 /*
118 * queue push/pop
119 */
120 static 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);
127
128 dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
129
130 ureq->req.status = status;
131 ureq->req.complete(&uep->ep, &ureq->req);
132 }
133
134 static void usbhsg_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt)
135 {
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);
139
140 ureq->req.actual = pkt->actual;
141
142 usbhsg_queue_pop(uep, ureq, 0);
143 }
144
145 static 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,
157 req->buf, req->length, req->zero, -1);
158 usbhs_pkt_start(pipe);
159
160 dev_dbg(dev, "pipe %d : queue push (%d)\n",
161 usbhs_pipe_number(pipe),
162 req->length);
163 }
164
165 /*
166 * dma map/unmap
167 */
168 static 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)) {
188 dev_err(dev, "dma mapping error %llx\n", (u64)pkt->dma);
189 return -EIO;
190 }
191
192 return 0;
193 }
194
195 static 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
217 static 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
233 /*
234 * USB_TYPE_STANDARD / clear feature functions
235 */
236 static 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
249 static 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)) {
257 usbhs_pipe_disable(pipe);
258 usbhs_pipe_sequence_data0(pipe);
259 usbhs_pipe_enable(pipe);
260 }
261
262 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
263
264 usbhs_pkt_start(pipe);
265
266 return 0;
267 }
268
269 struct 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
276 /*
277 * USB_TYPE_STANDARD / set feature functions
278 */
279 static 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
297 static 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
310 struct usbhsg_recip_handle req_set_feature = {
311 .name = "set feature",
312 .device = usbhsg_recip_handler_std_set_device,
313 .interface = usbhsg_recip_handler_std_control_done,
314 .endpoint = usbhsg_recip_handler_std_set_endpoint,
315 };
316
317 /*
318 * USB_TYPE_STANDARD / get status functions
319 */
320 static 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
330 static 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
368 static 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
380 static 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
392 static 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
408 struct 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
415 /*
416 * USB_TYPE handler
417 */
418 static 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;
425 struct usbhs_pipe *pipe;
426 int recip = ctrl->bRequestType & USB_RECIP_MASK;
427 int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
428 int ret = 0;
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);
434 pipe = usbhsg_uep_to_pipe(uep);
435 if (!pipe) {
436 dev_err(dev, "wrong recip request\n");
437 return -EINVAL;
438 }
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 */
472 static 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
478 gpriv->gadget.speed = usbhs_bus_get_speed(priv);
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
487 static 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:
512 pipe->handler = &usbhs_fifo_pio_push_handler;
513 break;
514 case WRITE_DATA_STAGE:
515 pipe->handler = &usbhs_fifo_pio_pop_handler;
516 break;
517 case NODATA_STATUS_STAGE:
518 pipe->handler = &usbhs_ctrl_stage_end_handler;
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;
535 case USB_REQ_SET_FEATURE:
536 recip_handler = &req_set_feature;
537 break;
538 case USB_REQ_GET_STATUS:
539 recip_handler = &req_get_status;
540 break;
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)
553 usbhs_pipe_stall(pipe);
554
555 return ret;
556 }
557
558 /*
559 *
560 * usb_dcp_ops
561 *
562 */
563 static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
564 {
565 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
566 struct usbhs_pkt *pkt;
567
568 while (1) {
569 pkt = usbhs_pkt_pop(pipe, NULL);
570 if (!pkt)
571 break;
572
573 usbhsg_queue_pop(uep, usbhsg_pkt_to_ureq(pkt), -ECONNRESET);
574 }
575
576 usbhs_pipe_disable(pipe);
577
578 return 0;
579 }
580
581 static 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
590 /*
591 *
592 * usb_ep_ops
593 *
594 */
595 static 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;
602 int ret = -EIO;
603
604 /*
605 * if it already have pipe,
606 * nothing to do
607 */
608 if (uep->pipe) {
609 usbhs_pipe_clear(uep->pipe);
610 usbhs_pipe_sequence_data0(uep->pipe);
611 return 0;
612 }
613
614 pipe = usbhs_pipe_malloc(priv,
615 usb_endpoint_type(desc),
616 usb_endpoint_dir_in(desc));
617 if (pipe) {
618 uep->pipe = pipe;
619 pipe->mod_private = uep;
620
621 /* set epnum / maxp */
622 usbhs_pipe_config_update(pipe, 0,
623 usb_endpoint_num(desc),
624 usb_endpoint_maxp(desc));
625
626 /*
627 * usbhs_fifo_dma_push/pop_handler try to
628 * use dmaengine if possible.
629 * It will use pio handler if impossible.
630 */
631 if (usb_endpoint_dir_in(desc))
632 pipe->handler = &usbhs_fifo_dma_push_handler;
633 else
634 pipe->handler = &usbhs_fifo_dma_pop_handler;
635
636 ret = 0;
637 }
638
639 return ret;
640 }
641
642 static int usbhsg_ep_disable(struct usb_ep *ep)
643 {
644 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
645
646 return usbhsg_pipe_disable(uep);
647 }
648
649 static 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
658 usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
659
660 ureq->req.dma = DMA_ADDR_INVALID;
661
662 return &ureq->req;
663 }
664
665 static 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
670 WARN_ON(!list_empty(&ureq->pkt.node));
671 kfree(ureq);
672 }
673
674 static 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);
681
682 /* param check */
683 if (usbhsg_is_not_connected(gpriv) ||
684 unlikely(!gpriv->driver) ||
685 unlikely(!pipe))
686 return -ESHUTDOWN;
687
688 usbhsg_queue_push(uep, ureq);
689
690 return 0;
691 }
692
693 static 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);
697 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
698
699 usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq));
700 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
701
702 return 0;
703 }
704
705 static 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);
710 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
711 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
712 unsigned long flags;
713
714 usbhsg_pipe_disable(uep);
715
716 dev_dbg(dev, "set halt %d (pipe %d)\n",
717 halt, usbhs_pipe_number(pipe));
718
719 /******************** spin lock ********************/
720 usbhs_lock(priv, flags);
721
722 if (halt)
723 usbhs_pipe_stall(pipe);
724 else
725 usbhs_pipe_disable(pipe);
726
727 if (halt && wedge)
728 usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
729 else
730 usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
731
732 usbhs_unlock(priv, flags);
733 /******************** spin unlock ******************/
734
735 return 0;
736 }
737
738 static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
739 {
740 return __usbhsg_ep_set_halt_wedge(ep, value, 0);
741 }
742
743 static int usbhsg_ep_set_wedge(struct usb_ep *ep)
744 {
745 return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
746 }
747
748 static 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 */
765 static 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);
771 unsigned long flags;
772 int ret = 0;
773
774 /******************** spin lock ********************/
775 usbhs_lock(priv, flags);
776
777 usbhsg_status_set(gpriv, status);
778 if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
779 usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
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 */
787
788 /*
789 * enable interrupt and systems if ready
790 */
791 dev_dbg(dev, "start gadget\n");
792
793 /*
794 * pipe initialize and enable DCP
795 */
796 usbhs_pipe_init(priv,
797 usbhsg_dma_map_ctrl);
798 usbhs_fifo_init(priv);
799 usbhsg_uep_init(gpriv);
800
801 /* dcp init */
802 dcp->pipe = usbhs_dcp_malloc(priv);
803 dcp->pipe->mod_private = dcp;
804 usbhs_pipe_config_update(dcp->pipe, 0, 0, 64);
805
806 /*
807 * system config enble
808 * - HI speed
809 * - function
810 * - usb module
811 */
812 usbhs_sys_function_ctrl(priv, 1);
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;
819 usbhs_irq_callback_update(priv, mod);
820
821 return 0;
822 }
823
824 static 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);
830 unsigned long flags;
831 int ret = 0;
832
833 /******************** spin lock ********************/
834 usbhs_lock(priv, flags);
835
836 usbhsg_status_clr(gpriv, status);
837 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
838 !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
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 */
846
847 /*
848 * disable interrupt and systems if 1st try
849 */
850 usbhs_fifo_quit(priv);
851
852 /* disable all irq */
853 mod->irq_dev_state = NULL;
854 mod->irq_ctrl_stage = NULL;
855 usbhs_irq_callback_update(priv, mod);
856
857 gpriv->gadget.speed = USB_SPEED_UNKNOWN;
858
859 /* disable sys */
860 usbhs_sys_set_test_mode(priv, 0);
861 usbhs_sys_function_ctrl(priv, 0);
862
863 usbhsg_pipe_disable(dcp);
864
865 dev_dbg(dev, "stop gadget\n");
866
867 return 0;
868 }
869
870 /*
871 *
872 * linux usb function
873 *
874 */
875 static int usbhsg_gadget_start(struct usb_gadget *gadget,
876 struct usb_gadget_driver *driver)
877 {
878 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
879 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
880
881 if (!driver ||
882 !driver->setup ||
883 driver->max_speed < USB_SPEED_FULL)
884 return -EINVAL;
885
886 /* first hook up the driver ... */
887 gpriv->driver = driver;
888 gpriv->gadget.dev.driver = &driver->driver;
889
890 return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
891 }
892
893 static int usbhsg_gadget_stop(struct usb_gadget *gadget,
894 struct usb_gadget_driver *driver)
895 {
896 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
897 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
898
899 if (!driver ||
900 !driver->unbind)
901 return -EINVAL;
902
903 usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
904 gpriv->gadget.dev.driver = NULL;
905 gpriv->driver = NULL;
906
907 return 0;
908 }
909
910 /*
911 * usb gadget ops
912 */
913 static 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
921 static struct usb_gadget_ops usbhsg_gadget_ops = {
922 .get_frame = usbhsg_get_frame,
923 .udc_start = usbhsg_gadget_start,
924 .udc_stop = usbhsg_gadget_stop,
925 };
926
927 static int usbhsg_start(struct usbhs_priv *priv)
928 {
929 return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
930 }
931
932 static int usbhsg_stop(struct usbhs_priv *priv)
933 {
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
941 return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
942 }
943
944 int usbhs_mod_gadget_probe(struct usbhs_priv *priv)
945 {
946 struct usbhsg_gpriv *gpriv;
947 struct usbhsg_uep *uep;
948 struct device *dev = usbhs_priv_to_dev(priv);
949 int pipe_size = usbhs_get_dparam(priv, pipe_size);
950 int i;
951 int ret;
952
953 gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
954 if (!gpriv) {
955 dev_err(dev, "Could not allocate gadget priv\n");
956 return -ENOMEM;
957 }
958
959 uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
960 if (!uep) {
961 dev_err(dev, "Could not allocate ep\n");
962 ret = -ENOMEM;
963 goto usbhs_mod_gadget_probe_err_gpriv;
964 }
965
966 /*
967 * CAUTION
968 *
969 * There is no guarantee that it is possible to access usb module here.
970 * Don't accesses to it.
971 * The accesse will be enable after "usbhsg_start"
972 */
973
974 /*
975 * register itself
976 */
977 usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
978
979 /* init gpriv */
980 gpriv->mod.name = "gadget";
981 gpriv->mod.start = usbhsg_start;
982 gpriv->mod.stop = usbhsg_stop;
983 gpriv->uep = uep;
984 gpriv->uep_size = pipe_size;
985 usbhsg_status_init(gpriv);
986
987 /*
988 * init gadget
989 */
990 dev_set_name(&gpriv->gadget.dev, "gadget");
991 gpriv->gadget.dev.parent = dev;
992 gpriv->gadget.name = "renesas_usbhs_udc";
993 gpriv->gadget.ops = &usbhsg_gadget_ops;
994 gpriv->gadget.max_speed = USB_SPEED_HIGH;
995 ret = device_register(&gpriv->gadget.dev);
996 if (ret < 0)
997 goto err_add_udc;
998
999 INIT_LIST_HEAD(&gpriv->gadget.ep_list);
1000
1001 /*
1002 * init usb_ep
1003 */
1004 usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
1005 uep->gpriv = gpriv;
1006 snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
1007
1008 uep->ep.name = uep->ep_name;
1009 uep->ep.ops = &usbhsg_ep_ops;
1010 INIT_LIST_HEAD(&uep->ep.ep_list);
1011
1012 /* init DCP */
1013 if (usbhsg_is_dcp(uep)) {
1014 gpriv->gadget.ep0 = &uep->ep;
1015 uep->ep.maxpacket = 64;
1016 }
1017 /* init normal pipe */
1018 else {
1019 uep->ep.maxpacket = 512;
1020 list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
1021 }
1022 }
1023
1024 ret = usb_add_gadget_udc(dev, &gpriv->gadget);
1025 if (ret)
1026 goto err_register;
1027
1028
1029 dev_info(dev, "gadget probed\n");
1030
1031 return 0;
1032
1033 err_register:
1034 device_unregister(&gpriv->gadget.dev);
1035 err_add_udc:
1036 kfree(gpriv->uep);
1037
1038 usbhs_mod_gadget_probe_err_gpriv:
1039 kfree(gpriv);
1040
1041 return ret;
1042 }
1043
1044 void usbhs_mod_gadget_remove(struct usbhs_priv *priv)
1045 {
1046 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1047
1048 usb_del_gadget_udc(&gpriv->gadget);
1049
1050 device_unregister(&gpriv->gadget.dev);
1051
1052 kfree(gpriv->uep);
1053 kfree(gpriv);
1054 }