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