blob: 9f5e9fbfaae5e3d248a35bd5302208dcb117d2fb [file] [log] [blame]
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001/*
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 */
Kuninori Morimotodfbb7f42011-11-24 17:28:35 -080017#include <linux/delay.h>
Kuninori Morimotod128a2592011-08-03 21:41:26 -070018#include <linux/dma-mapping.h>
Kuninori Morimoto2f983822011-04-05 11:40:54 +090019#include <linux/io.h>
20#include <linux/module.h>
21#include <linux/platform_device.h>
22#include <linux/usb/ch9.h>
23#include <linux/usb/gadget.h>
24#include "common.h"
25
26/*
27 * struct
28 */
29struct usbhsg_request {
30 struct usb_request req;
Kuninori Morimoto4bd04812011-06-06 14:18:07 +090031 struct usbhs_pkt pkt;
Kuninori Morimoto2f983822011-04-05 11:40:54 +090032};
33
34#define EP_NAME_SIZE 8
35struct usbhsg_gpriv;
Kuninori Morimoto2f983822011-04-05 11:40:54 +090036struct usbhsg_uep {
37 struct usb_ep ep;
38 struct usbhs_pipe *pipe;
Kuninori Morimoto2f983822011-04-05 11:40:54 +090039
40 char ep_name[EP_NAME_SIZE];
41
42 struct usbhsg_gpriv *gpriv;
Kuninori Morimoto2f983822011-04-05 11:40:54 +090043};
44
45struct usbhsg_gpriv {
46 struct usb_gadget gadget;
47 struct usbhs_mod mod;
48
49 struct usbhsg_uep *uep;
50 int uep_size;
51
52 struct usb_gadget_driver *driver;
53
54 u32 status;
55#define USBHSG_STATUS_STARTED (1 << 0)
56#define USBHSG_STATUS_REGISTERD (1 << 1)
57#define USBHSG_STATUS_WEDGE (1 << 2)
Shimoda, Yoshihirocac402d2012-03-16 13:10:15 +090058#define USBHSG_STATUS_SELF_POWERED (1 << 3)
Kuninori Morimoto2f983822011-04-05 11:40:54 +090059};
60
Kuninori Morimoto2f983822011-04-05 11:40:54 +090061struct 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) \
Kuninori Morimoto925403f2013-07-11 22:32:31 -070080 for ((i) = start; \
81 ((i) < (g)->uep_size) && ((pos) = (g)->uep + (i)); \
82 (i)++)
Kuninori Morimoto2f983822011-04-05 11:40:54 +090083
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)
Kuninori Morimoto2f983822011-04-05 11:40:54 +090097#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
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900106#define usbhsg_ureq_to_pkt(u) (&(u)->pkt)
107#define usbhsg_pkt_to_ureq(i) \
108 container_of(i, struct usbhsg_request, pkt)
109
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900110#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/*
Kuninori Morimoto233f5192011-07-07 00:23:24 -0700119 * queue push/pop
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900120 */
Yoshihiro Shimodaccf7f202015-03-16 16:36:48 +0900121static void __usbhsg_queue_pop(struct usbhsg_uep *uep,
122 struct usbhsg_request *ureq,
123 int status)
124{
125 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
126 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
127 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
128 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
129
130 dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
131
132 ureq->req.status = status;
133 spin_unlock(usbhs_priv_to_lock(priv));
134 usb_gadget_giveback_request(&uep->ep, &ureq->req);
135 spin_lock(usbhs_priv_to_lock(priv));
136}
137
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900138static void usbhsg_queue_pop(struct usbhsg_uep *uep,
139 struct usbhsg_request *ureq,
140 int status)
141{
142 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
Yoshihiro Shimodaccf7f202015-03-16 16:36:48 +0900143 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
144 unsigned long flags;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900145
Yoshihiro Shimodaccf7f202015-03-16 16:36:48 +0900146 usbhs_lock(priv, flags);
147 __usbhsg_queue_pop(uep, ureq, status);
148 usbhs_unlock(priv, flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900149}
150
Kuninori Morimoto2cc97192011-10-10 22:03:39 -0700151static void usbhsg_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900152{
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900153 struct usbhs_pipe *pipe = pkt->pipe;
154 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
155 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
Yoshihiro Shimoda09cbe7d2016-04-04 20:40:20 +0900156 unsigned long flags;
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900157
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900158 ureq->req.actual = pkt->actual;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900159
Yoshihiro Shimoda09cbe7d2016-04-04 20:40:20 +0900160 usbhs_lock(priv, flags);
161 if (uep)
162 __usbhsg_queue_pop(uep, ureq, 0);
163 usbhs_unlock(priv, flags);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900164}
165
Kuninori Morimotob3318722011-10-10 22:04:41 -0700166static void usbhsg_queue_push(struct usbhsg_uep *uep,
167 struct usbhsg_request *ureq)
168{
169 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
170 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
171 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
172 struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
173 struct usb_request *req = &ureq->req;
174
175 req->actual = 0;
176 req->status = -EINPROGRESS;
177 usbhs_pkt_push(pipe, pkt, usbhsg_queue_done,
Kuninori Morimoto3edeee32011-12-08 18:28:54 -0800178 req->buf, req->length, req->zero, -1);
Kuninori Morimoto654c35a2011-10-10 22:04:53 -0700179 usbhs_pkt_start(pipe);
Kuninori Morimotob3318722011-10-10 22:04:41 -0700180
181 dev_dbg(dev, "pipe %d : queue push (%d)\n",
182 usbhs_pipe_number(pipe),
183 req->length);
184}
185
Kuninori Morimoto233f5192011-07-07 00:23:24 -0700186/*
187 * dma map/unmap
188 */
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900189static int usbhsg_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
190{
Felipe Balbiade78f92011-12-19 11:57:16 +0200191 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
192 struct usb_request *req = &ureq->req;
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900193 struct usbhs_pipe *pipe = pkt->pipe;
194 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
195 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900196 enum dma_data_direction dir;
Felipe Balbiade78f92011-12-19 11:57:16 +0200197 int ret = 0;
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900198
Felipe Balbiade78f92011-12-19 11:57:16 +0200199 dir = usbhs_pipe_is_dir_host(pipe);
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900200
Felipe Balbiade78f92011-12-19 11:57:16 +0200201 if (map) {
202 /* it can not use scatter/gather */
203 WARN_ON(req->num_sgs);
204
205 ret = usb_gadget_map_request(&gpriv->gadget, req, dir);
206 if (ret < 0)
207 return ret;
208
209 pkt->dma = req->dma;
210 } else {
211 usb_gadget_unmap_request(&gpriv->gadget, req, dir);
212 }
213
214 return ret;
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900215}
216
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900217/*
218 * USB_TYPE_STANDARD / clear feature functions
219 */
220static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
221 struct usbhsg_uep *uep,
222 struct usb_ctrlrequest *ctrl)
223{
224 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
225 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
226 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
227
228 usbhs_dcp_control_transfer_done(pipe);
229
230 return 0;
231}
232
233static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
234 struct usbhsg_uep *uep,
235 struct usb_ctrlrequest *ctrl)
236{
237 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
238 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
239
240 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900241 usbhs_pipe_disable(pipe);
Kuninori Morimoto6e6db822011-10-10 22:05:30 -0700242 usbhs_pipe_sequence_data0(pipe);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900243 usbhs_pipe_enable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900244 }
245
246 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
247
Kuninori Morimoto25fa7072011-11-24 17:28:17 -0800248 usbhs_pkt_start(pipe);
249
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900250 return 0;
251}
252
Kuninori Morimoto225da3e2013-03-31 18:34:43 -0700253static struct usbhsg_recip_handle req_clear_feature = {
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900254 .name = "clear feature",
255 .device = usbhsg_recip_handler_std_control_done,
256 .interface = usbhsg_recip_handler_std_control_done,
257 .endpoint = usbhsg_recip_handler_std_clear_endpoint,
258};
259
260/*
Kuninori Morimotoced6e092011-11-24 17:27:50 -0800261 * USB_TYPE_STANDARD / set feature functions
262 */
Kuninori Morimotodfbb7f42011-11-24 17:28:35 -0800263static int usbhsg_recip_handler_std_set_device(struct usbhs_priv *priv,
264 struct usbhsg_uep *uep,
265 struct usb_ctrlrequest *ctrl)
266{
267 switch (le16_to_cpu(ctrl->wValue)) {
268 case USB_DEVICE_TEST_MODE:
269 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
270 udelay(100);
271 usbhs_sys_set_test_mode(priv, le16_to_cpu(ctrl->wIndex >> 8));
272 break;
273 default:
274 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
275 break;
276 }
277
278 return 0;
279}
280
Kuninori Morimotoced6e092011-11-24 17:27:50 -0800281static int usbhsg_recip_handler_std_set_endpoint(struct usbhs_priv *priv,
282 struct usbhsg_uep *uep,
283 struct usb_ctrlrequest *ctrl)
284{
285 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
286
287 usbhs_pipe_stall(pipe);
288
289 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
290
291 return 0;
292}
293
Kuninori Morimoto225da3e2013-03-31 18:34:43 -0700294static struct usbhsg_recip_handle req_set_feature = {
Kuninori Morimotoced6e092011-11-24 17:27:50 -0800295 .name = "set feature",
Kuninori Morimotodfbb7f42011-11-24 17:28:35 -0800296 .device = usbhsg_recip_handler_std_set_device,
Kuninori Morimotoced6e092011-11-24 17:27:50 -0800297 .interface = usbhsg_recip_handler_std_control_done,
298 .endpoint = usbhsg_recip_handler_std_set_endpoint,
299};
300
301/*
Kuninori Morimoto17f7f762011-11-24 17:28:04 -0800302 * USB_TYPE_STANDARD / get status functions
303 */
304static void __usbhsg_recip_send_complete(struct usb_ep *ep,
305 struct usb_request *req)
306{
307 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
308
309 /* free allocated recip-buffer/usb_request */
310 kfree(ureq->pkt.buf);
311 usb_ep_free_request(ep, req);
312}
313
314static void __usbhsg_recip_send_status(struct usbhsg_gpriv *gpriv,
315 unsigned short status)
316{
317 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
318 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
319 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
320 struct usb_request *req;
321 unsigned short *buf;
322
323 /* alloc new usb_request for recip */
324 req = usb_ep_alloc_request(&dcp->ep, GFP_ATOMIC);
325 if (!req) {
326 dev_err(dev, "recip request allocation fail\n");
327 return;
328 }
329
330 /* alloc recip data buffer */
331 buf = kmalloc(sizeof(*buf), GFP_ATOMIC);
332 if (!buf) {
333 usb_ep_free_request(&dcp->ep, req);
334 dev_err(dev, "recip data allocation fail\n");
335 return;
336 }
337
338 /* recip data is status */
339 *buf = cpu_to_le16(status);
340
341 /* allocated usb_request/buffer will be freed */
342 req->complete = __usbhsg_recip_send_complete;
343 req->buf = buf;
344 req->length = sizeof(*buf);
345 req->zero = 0;
346
347 /* push packet */
348 pipe->handler = &usbhs_fifo_pio_push_handler;
349 usbhsg_queue_push(dcp, usbhsg_req_to_ureq(req));
350}
351
352static int usbhsg_recip_handler_std_get_device(struct usbhs_priv *priv,
353 struct usbhsg_uep *uep,
354 struct usb_ctrlrequest *ctrl)
355{
356 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
Shimoda, Yoshihirocac402d2012-03-16 13:10:15 +0900357 unsigned short status = 0;
358
359 if (usbhsg_status_has(gpriv, USBHSG_STATUS_SELF_POWERED))
360 status = 1 << USB_DEVICE_SELF_POWERED;
Kuninori Morimoto17f7f762011-11-24 17:28:04 -0800361
362 __usbhsg_recip_send_status(gpriv, status);
363
364 return 0;
365}
366
367static int usbhsg_recip_handler_std_get_interface(struct usbhs_priv *priv,
368 struct usbhsg_uep *uep,
369 struct usb_ctrlrequest *ctrl)
370{
371 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
372 unsigned short status = 0;
373
374 __usbhsg_recip_send_status(gpriv, status);
375
376 return 0;
377}
378
379static int usbhsg_recip_handler_std_get_endpoint(struct usbhs_priv *priv,
380 struct usbhsg_uep *uep,
381 struct usb_ctrlrequest *ctrl)
382{
383 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
384 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
385 unsigned short status = 0;
386
387 if (usbhs_pipe_is_stall(pipe))
388 status = 1 << USB_ENDPOINT_HALT;
389
390 __usbhsg_recip_send_status(gpriv, status);
391
392 return 0;
393}
394
Kuninori Morimoto225da3e2013-03-31 18:34:43 -0700395static struct usbhsg_recip_handle req_get_status = {
Kuninori Morimoto17f7f762011-11-24 17:28:04 -0800396 .name = "get status",
397 .device = usbhsg_recip_handler_std_get_device,
398 .interface = usbhsg_recip_handler_std_get_interface,
399 .endpoint = usbhsg_recip_handler_std_get_endpoint,
400};
401
402/*
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900403 * USB_TYPE handler
404 */
405static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
406 struct usbhsg_recip_handle *handler,
407 struct usb_ctrlrequest *ctrl)
408{
409 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
410 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
411 struct usbhsg_uep *uep;
Kuninori Morimoto0432eed2011-06-06 14:18:54 +0900412 struct usbhs_pipe *pipe;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900413 int recip = ctrl->bRequestType & USB_RECIP_MASK;
414 int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
Jesper Juhl7983bc72012-01-16 22:42:10 +0100415 int ret = 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900416 int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
417 struct usb_ctrlrequest *ctrl);
418 char *msg;
419
420 uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
Kuninori Morimoto0432eed2011-06-06 14:18:54 +0900421 pipe = usbhsg_uep_to_pipe(uep);
422 if (!pipe) {
Kuninori Morimoto9a28b7b2011-04-21 14:10:12 +0900423 dev_err(dev, "wrong recip request\n");
Kuninori Morimoto25fa7072011-11-24 17:28:17 -0800424 return -EINVAL;
Kuninori Morimoto9a28b7b2011-04-21 14:10:12 +0900425 }
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900426
427 switch (recip) {
428 case USB_RECIP_DEVICE:
429 msg = "DEVICE";
430 func = handler->device;
431 break;
432 case USB_RECIP_INTERFACE:
433 msg = "INTERFACE";
434 func = handler->interface;
435 break;
436 case USB_RECIP_ENDPOINT:
437 msg = "ENDPOINT";
438 func = handler->endpoint;
439 break;
440 default:
441 dev_warn(dev, "unsupported RECIP(%d)\n", recip);
442 func = NULL;
443 ret = -EINVAL;
444 }
445
446 if (func) {
447 dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
448 ret = func(priv, uep, ctrl);
449 }
450
451 return ret;
452}
453
454/*
455 * irq functions
456 *
457 * it will be called from usbhs_interrupt
458 */
459static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
460 struct usbhs_irq_state *irq_state)
461{
462 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
463 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
464
Kuninori Morimoto75587f52011-10-10 22:01:51 -0700465 gpriv->gadget.speed = usbhs_bus_get_speed(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900466
467 dev_dbg(dev, "state = %x : speed : %d\n",
468 usbhs_status_get_device_state(irq_state),
469 gpriv->gadget.speed);
470
471 return 0;
472}
473
474static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
475 struct usbhs_irq_state *irq_state)
476{
477 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
478 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
479 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
480 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
481 struct usb_ctrlrequest ctrl;
482 struct usbhsg_recip_handle *recip_handler = NULL;
483 int stage = usbhs_status_get_ctrl_stage(irq_state);
484 int ret = 0;
485
486 dev_dbg(dev, "stage = %d\n", stage);
487
488 /*
489 * see Manual
490 *
491 * "Operation"
492 * - "Interrupt Function"
493 * - "Control Transfer Stage Transition Interrupt"
494 * - Fig. "Control Transfer Stage Transitions"
495 */
496
497 switch (stage) {
498 case READ_DATA_STAGE:
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700499 pipe->handler = &usbhs_fifo_pio_push_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900500 break;
501 case WRITE_DATA_STAGE:
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700502 pipe->handler = &usbhs_fifo_pio_pop_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900503 break;
504 case NODATA_STATUS_STAGE:
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700505 pipe->handler = &usbhs_ctrl_stage_end_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900506 break;
507 default:
508 return ret;
509 }
510
511 /*
512 * get usb request
513 */
514 usbhs_usbreq_get_val(priv, &ctrl);
515
516 switch (ctrl.bRequestType & USB_TYPE_MASK) {
517 case USB_TYPE_STANDARD:
518 switch (ctrl.bRequest) {
519 case USB_REQ_CLEAR_FEATURE:
520 recip_handler = &req_clear_feature;
521 break;
Kuninori Morimotoced6e092011-11-24 17:27:50 -0800522 case USB_REQ_SET_FEATURE:
523 recip_handler = &req_set_feature;
524 break;
Kuninori Morimoto17f7f762011-11-24 17:28:04 -0800525 case USB_REQ_GET_STATUS:
526 recip_handler = &req_get_status;
527 break;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900528 }
529 }
530
531 /*
532 * setup stage / run recip
533 */
534 if (recip_handler)
535 ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
536 else
537 ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
538
539 if (ret < 0)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900540 usbhs_pipe_stall(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900541
542 return ret;
543}
544
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900545/*
546 *
547 * usb_dcp_ops
548 *
549 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900550static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
551{
552 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto8a2c2252011-06-06 14:18:33 +0900553 struct usbhs_pkt *pkt;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900554
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900555 while (1) {
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900556 pkt = usbhs_pkt_pop(pipe, NULL);
Kuninori Morimoto8a2c2252011-06-06 14:18:33 +0900557 if (!pkt)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900558 break;
Kuninori Morimoto91b158f2011-11-24 17:28:26 -0800559
560 usbhsg_queue_pop(uep, usbhsg_pkt_to_ureq(pkt), -ECONNRESET);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900561 }
562
Kuninori Morimoto91b158f2011-11-24 17:28:26 -0800563 usbhs_pipe_disable(pipe);
564
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900565 return 0;
566}
567
568/*
569 *
570 * usb_ep_ops
571 *
572 */
573static int usbhsg_ep_enable(struct usb_ep *ep,
574 const struct usb_endpoint_descriptor *desc)
575{
576 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
577 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
578 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
579 struct usbhs_pipe *pipe;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900580 int ret = -EIO;
Yoshihiro Shimoda7d989fc2016-06-08 16:32:50 +0900581 unsigned long flags;
582
583 usbhs_lock(priv, flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900584
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900585 /*
586 * if it already have pipe,
587 * nothing to do
588 */
Kuninori Morimoto08e6c612011-06-09 16:48:25 +0900589 if (uep->pipe) {
590 usbhs_pipe_clear(uep->pipe);
Kuninori Morimoto6e6db822011-10-10 22:05:30 -0700591 usbhs_pipe_sequence_data0(uep->pipe);
Yoshihiro Shimoda7d989fc2016-06-08 16:32:50 +0900592 ret = 0;
593 goto usbhsg_ep_enable_end;
Kuninori Morimoto08e6c612011-06-09 16:48:25 +0900594 }
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900595
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700596 pipe = usbhs_pipe_malloc(priv,
597 usb_endpoint_type(desc),
598 usb_endpoint_dir_in(desc));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900599 if (pipe) {
600 uep->pipe = pipe;
601 pipe->mod_private = uep;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900602
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700603 /* set epnum / maxp */
Kuninori Morimotobc6fbf52011-10-10 22:04:00 -0700604 usbhs_pipe_config_update(pipe, 0,
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700605 usb_endpoint_num(desc),
606 usb_endpoint_maxp(desc));
607
Kuninori Morimoto233f5192011-07-07 00:23:24 -0700608 /*
609 * usbhs_fifo_dma_push/pop_handler try to
610 * use dmaengine if possible.
611 * It will use pio handler if impossible.
612 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900613 if (usb_endpoint_dir_in(desc))
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700614 pipe->handler = &usbhs_fifo_dma_push_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900615 else
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700616 pipe->handler = &usbhs_fifo_dma_pop_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900617
618 ret = 0;
619 }
Kuninori Morimotocb966322011-04-21 14:10:08 +0900620
Yoshihiro Shimoda7d989fc2016-06-08 16:32:50 +0900621usbhsg_ep_enable_end:
622 usbhs_unlock(priv, flags);
623
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900624 return ret;
625}
626
627static int usbhsg_ep_disable(struct usb_ep *ep)
628{
629 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
Yoshihiro Shimodadfb87b82014-07-09 20:30:13 +0900630 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900631
Kazuya Mizuguchie2eb9802014-11-04 10:05:42 +0900632 if (!pipe)
633 return -EINVAL;
634
Kuninori Morimotod9fa2982012-12-10 22:44:07 -0800635 usbhsg_pipe_disable(uep);
Yoshihiro Shimodadfb87b82014-07-09 20:30:13 +0900636 usbhs_pipe_free(pipe);
Kuninori Morimotod9fa2982012-12-10 22:44:07 -0800637
638 uep->pipe->mod_private = NULL;
639 uep->pipe = NULL;
640
641 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900642}
643
644static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
645 gfp_t gfp_flags)
646{
647 struct usbhsg_request *ureq;
648
649 ureq = kzalloc(sizeof *ureq, gfp_flags);
650 if (!ureq)
651 return NULL;
652
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900653 usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
654
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900655 return &ureq->req;
656}
657
658static void usbhsg_ep_free_request(struct usb_ep *ep,
659 struct usb_request *req)
660{
661 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
662
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900663 WARN_ON(!list_empty(&ureq->pkt.node));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900664 kfree(ureq);
665}
666
667static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
668 gfp_t gfp_flags)
669{
670 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
671 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
672 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
673 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900674
675 /* param check */
676 if (usbhsg_is_not_connected(gpriv) ||
677 unlikely(!gpriv->driver) ||
678 unlikely(!pipe))
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900679 return -ESHUTDOWN;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900680
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900681 usbhsg_queue_push(uep, ureq);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900682
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900683 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900684}
685
686static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
687{
688 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
689 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900690 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900691
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900692 usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900693 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
694
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900695 return 0;
696}
697
698static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
699{
700 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
701 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
702 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900703 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900704 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900705 unsigned long flags;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900706
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900707 usbhsg_pipe_disable(uep);
708
709 dev_dbg(dev, "set halt %d (pipe %d)\n",
710 halt, usbhs_pipe_number(pipe));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900711
712 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900713 usbhs_lock(priv, flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900714
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900715 if (halt)
716 usbhs_pipe_stall(pipe);
717 else
718 usbhs_pipe_disable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900719
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900720 if (halt && wedge)
721 usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
722 else
723 usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900724
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900725 usbhs_unlock(priv, flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900726 /******************** spin unlock ******************/
727
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900728 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900729}
730
731static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
732{
733 return __usbhsg_ep_set_halt_wedge(ep, value, 0);
734}
735
736static int usbhsg_ep_set_wedge(struct usb_ep *ep)
737{
738 return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
739}
740
741static struct usb_ep_ops usbhsg_ep_ops = {
742 .enable = usbhsg_ep_enable,
743 .disable = usbhsg_ep_disable,
744
745 .alloc_request = usbhsg_ep_alloc_request,
746 .free_request = usbhsg_ep_free_request,
747
748 .queue = usbhsg_ep_queue,
749 .dequeue = usbhsg_ep_dequeue,
750
751 .set_halt = usbhsg_ep_set_halt,
752 .set_wedge = usbhsg_ep_set_wedge,
753};
754
755/*
756 * usb module start/end
757 */
758static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
759{
760 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
761 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
762 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
763 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900764 unsigned long flags;
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900765 int ret = 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900766
767 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900768 usbhs_lock(priv, flags);
769
770 usbhsg_status_set(gpriv, status);
771 if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
772 usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
773 ret = -1; /* not ready */
774
775 usbhs_unlock(priv, flags);
776 /******************** spin unlock ********************/
777
778 if (ret < 0)
779 return 0; /* not ready is not error */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900780
781 /*
782 * enable interrupt and systems if ready
783 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900784 dev_dbg(dev, "start gadget\n");
785
786 /*
787 * pipe initialize and enable DCP
788 */
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900789 usbhs_pipe_init(priv,
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900790 usbhsg_dma_map_ctrl);
Kuninori Morimotodad67392011-06-06 14:18:28 +0900791 usbhs_fifo_init(priv);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900792
Kuninori Morimotod9fa2982012-12-10 22:44:07 -0800793 /* dcp init instead of usbhsg_ep_enable() */
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900794 dcp->pipe = usbhs_dcp_malloc(priv);
795 dcp->pipe->mod_private = dcp;
Kuninori Morimotobc6fbf52011-10-10 22:04:00 -0700796 usbhs_pipe_config_update(dcp->pipe, 0, 0, 64);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900797
798 /*
799 * system config enble
800 * - HI speed
801 * - function
802 * - usb module
803 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900804 usbhs_sys_function_ctrl(priv, 1);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900805
806 /*
807 * enable irq callback
808 */
809 mod->irq_dev_state = usbhsg_irq_dev_state;
810 mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900811 usbhs_irq_callback_update(priv, mod);
812
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900813 return 0;
814}
815
816static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
817{
818 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
819 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
820 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
821 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900822 unsigned long flags;
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900823 int ret = 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900824
825 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900826 usbhs_lock(priv, flags);
827
828 usbhsg_status_clr(gpriv, status);
829 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
830 !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
831 ret = -1; /* already done */
832
833 usbhs_unlock(priv, flags);
834 /******************** spin unlock ********************/
835
836 if (ret < 0)
837 return 0; /* already done is not error */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900838
839 /*
840 * disable interrupt and systems if 1st try
841 */
Kuninori Morimotodad67392011-06-06 14:18:28 +0900842 usbhs_fifo_quit(priv);
843
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900844 /* disable all irq */
845 mod->irq_dev_state = NULL;
846 mod->irq_ctrl_stage = NULL;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900847 usbhs_irq_callback_update(priv, mod);
848
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900849 gpriv->gadget.speed = USB_SPEED_UNKNOWN;
850
851 /* disable sys */
Kuninori Morimotodfbb7f42011-11-24 17:28:35 -0800852 usbhs_sys_set_test_mode(priv, 0);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900853 usbhs_sys_function_ctrl(priv, 0);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900854
Kuninori Morimotod9fa2982012-12-10 22:44:07 -0800855 usbhsg_ep_disable(&dcp->ep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900856
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900857 dev_dbg(dev, "stop gadget\n");
858
859 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900860}
861
862/*
863 *
864 * linux usb function
865 *
866 */
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300867static int usbhsg_gadget_start(struct usb_gadget *gadget,
868 struct usb_gadget_driver *driver)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900869{
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300870 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -0800871 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900872
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300873 if (!driver ||
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900874 !driver->setup ||
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100875 driver->max_speed < USB_SPEED_FULL)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900876 return -EINVAL;
Kuninori Morimoto3b872182011-07-03 17:42:35 -0700877
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900878 /* first hook up the driver ... */
879 gpriv->driver = driver;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900880
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900881 return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900882}
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900883
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300884static int usbhsg_gadget_stop(struct usb_gadget *gadget,
885 struct usb_gadget_driver *driver)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900886{
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300887 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -0800888 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900889
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900890 usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900891 gpriv->driver = NULL;
892
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900893 return 0;
894}
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900895
896/*
897 * usb gadget ops
898 */
899static int usbhsg_get_frame(struct usb_gadget *gadget)
900{
901 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
902 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
903
904 return usbhs_frame_get_num(priv);
905}
906
kuninori.morimoto.gx@renesas.com4cd2f592012-10-15 23:24:19 -0700907static int usbhsg_pullup(struct usb_gadget *gadget, int is_on)
908{
909 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
910 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
911
912 usbhs_sys_function_pullup(priv, is_on);
913
914 return 0;
915}
916
Shimoda, Yoshihirocac402d2012-03-16 13:10:15 +0900917static int usbhsg_set_selfpowered(struct usb_gadget *gadget, int is_self)
918{
919 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
920
921 if (is_self)
922 usbhsg_status_set(gpriv, USBHSG_STATUS_SELF_POWERED);
923 else
924 usbhsg_status_clr(gpriv, USBHSG_STATUS_SELF_POWERED);
925
926 return 0;
927}
928
Felipe Balbieeef4582013-01-24 17:58:16 +0200929static const struct usb_gadget_ops usbhsg_gadget_ops = {
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900930 .get_frame = usbhsg_get_frame,
Shimoda, Yoshihirocac402d2012-03-16 13:10:15 +0900931 .set_selfpowered = usbhsg_set_selfpowered,
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300932 .udc_start = usbhsg_gadget_start,
933 .udc_stop = usbhsg_gadget_stop,
kuninori.morimoto.gx@renesas.com4cd2f592012-10-15 23:24:19 -0700934 .pullup = usbhsg_pullup,
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900935};
936
937static int usbhsg_start(struct usbhs_priv *priv)
938{
939 return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
940}
941
942static int usbhsg_stop(struct usbhs_priv *priv)
943{
Kuninori Morimoto8885a892011-11-17 18:19:38 -0800944 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
945
946 /* cable disconnect */
947 if (gpriv->driver &&
948 gpriv->driver->disconnect)
949 gpriv->driver->disconnect(&gpriv->gadget);
950
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900951 return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
952}
953
Kuninori Morimotob7a8d172011-10-26 19:33:49 -0700954int usbhs_mod_gadget_probe(struct usbhs_priv *priv)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900955{
956 struct usbhsg_gpriv *gpriv;
957 struct usbhsg_uep *uep;
958 struct device *dev = usbhs_priv_to_dev(priv);
959 int pipe_size = usbhs_get_dparam(priv, pipe_size);
960 int i;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300961 int ret;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900962
963 gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
964 if (!gpriv) {
965 dev_err(dev, "Could not allocate gadget priv\n");
966 return -ENOMEM;
967 }
968
969 uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
970 if (!uep) {
971 dev_err(dev, "Could not allocate ep\n");
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300972 ret = -ENOMEM;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900973 goto usbhs_mod_gadget_probe_err_gpriv;
974 }
975
976 /*
977 * CAUTION
978 *
979 * There is no guarantee that it is possible to access usb module here.
980 * Don't accesses to it.
981 * The accesse will be enable after "usbhsg_start"
982 */
983
984 /*
985 * register itself
986 */
987 usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
988
989 /* init gpriv */
990 gpriv->mod.name = "gadget";
991 gpriv->mod.start = usbhsg_start;
992 gpriv->mod.stop = usbhsg_stop;
993 gpriv->uep = uep;
994 gpriv->uep_size = pipe_size;
995 usbhsg_status_init(gpriv);
996
997 /*
998 * init gadget
999 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001000 gpriv->gadget.dev.parent = dev;
1001 gpriv->gadget.name = "renesas_usbhs_udc";
1002 gpriv->gadget.ops = &usbhsg_gadget_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01001003 gpriv->gadget.max_speed = USB_SPEED_HIGH;
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001004
1005 INIT_LIST_HEAD(&gpriv->gadget.ep_list);
1006
1007 /*
1008 * init usb_ep
1009 */
1010 usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
1011 uep->gpriv = gpriv;
Kuninori Morimoto58482942012-12-10 22:43:45 -08001012 uep->pipe = NULL;
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001013 snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
1014
1015 uep->ep.name = uep->ep_name;
1016 uep->ep.ops = &usbhsg_ep_ops;
1017 INIT_LIST_HEAD(&uep->ep.ep_list);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001018
1019 /* init DCP */
1020 if (usbhsg_is_dcp(uep)) {
1021 gpriv->gadget.ep0 = &uep->ep;
Robert Baldygae117e742013-12-13 12:23:38 +01001022 usb_ep_set_maxpacket_limit(&uep->ep, 64);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001023 }
1024 /* init normal pipe */
1025 else {
Robert Baldygae117e742013-12-13 12:23:38 +01001026 usb_ep_set_maxpacket_limit(&uep->ep, 512);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001027 list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
1028 }
1029 }
1030
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001031 ret = usb_add_gadget_udc(dev, &gpriv->gadget);
1032 if (ret)
Felipe Balbi0972ef72013-01-24 17:08:01 +02001033 goto err_add_udc;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001034
1035
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001036 dev_info(dev, "gadget probed\n");
1037
1038 return 0;
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -08001039
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001040err_add_udc:
1041 kfree(gpriv->uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001042
1043usbhs_mod_gadget_probe_err_gpriv:
1044 kfree(gpriv);
1045
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001046 return ret;
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001047}
1048
Kuninori Morimotob7a8d172011-10-26 19:33:49 -07001049void usbhs_mod_gadget_remove(struct usbhs_priv *priv)
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001050{
1051 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1052
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001053 usb_del_gadget_udc(&gpriv->gadget);
Kuninori Morimoto3b872182011-07-03 17:42:35 -07001054
Sebastian Andrzej Siewior3af51ac2011-06-03 19:50:48 +02001055 kfree(gpriv->uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001056 kfree(gpriv);
1057}