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