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