blob: 8697e6efcabf92df1ace5b62defa9adcf954ea5c [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)
Takeshi Kihara04a5def2014-11-04 10:05:43 +090059#define USBHSG_STATUS_SOFT_CONNECT (1 << 4)
Kuninori Morimoto2f983822011-04-05 11:40:54 +090060};
61
Kuninori Morimoto2f983822011-04-05 11:40:54 +090062struct usbhsg_recip_handle {
63 char *name;
64 int (*device)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
65 struct usb_ctrlrequest *ctrl);
66 int (*interface)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
67 struct usb_ctrlrequest *ctrl);
68 int (*endpoint)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
69 struct usb_ctrlrequest *ctrl);
70};
71
72/*
73 * macro
74 */
75#define usbhsg_priv_to_gpriv(priv) \
76 container_of( \
77 usbhs_mod_get(priv, USBHS_GADGET), \
78 struct usbhsg_gpriv, mod)
79
80#define __usbhsg_for_each_uep(start, pos, g, i) \
Kuninori Morimoto925403f2013-07-11 22:32:31 -070081 for ((i) = start; \
82 ((i) < (g)->uep_size) && ((pos) = (g)->uep + (i)); \
83 (i)++)
Kuninori Morimoto2f983822011-04-05 11:40:54 +090084
85#define usbhsg_for_each_uep(pos, gpriv, i) \
86 __usbhsg_for_each_uep(1, pos, gpriv, i)
87
88#define usbhsg_for_each_uep_with_dcp(pos, gpriv, i) \
89 __usbhsg_for_each_uep(0, pos, gpriv, i)
90
91#define usbhsg_gadget_to_gpriv(g)\
92 container_of(g, struct usbhsg_gpriv, gadget)
93
94#define usbhsg_req_to_ureq(r)\
95 container_of(r, struct usbhsg_request, req)
96
97#define usbhsg_ep_to_uep(e) container_of(e, struct usbhsg_uep, ep)
Kuninori Morimoto2f983822011-04-05 11:40:54 +090098#define usbhsg_gpriv_to_dev(gp) usbhs_priv_to_dev((gp)->mod.priv)
99#define usbhsg_gpriv_to_priv(gp) ((gp)->mod.priv)
100#define usbhsg_gpriv_to_dcp(gp) ((gp)->uep)
101#define usbhsg_gpriv_to_nth_uep(gp, i) ((gp)->uep + i)
102#define usbhsg_uep_to_gpriv(u) ((u)->gpriv)
103#define usbhsg_uep_to_pipe(u) ((u)->pipe)
104#define usbhsg_pipe_to_uep(p) ((p)->mod_private)
105#define usbhsg_is_dcp(u) ((u) == usbhsg_gpriv_to_dcp((u)->gpriv))
106
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900107#define usbhsg_ureq_to_pkt(u) (&(u)->pkt)
108#define usbhsg_pkt_to_ureq(i) \
109 container_of(i, struct usbhsg_request, pkt)
110
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900111#define usbhsg_is_not_connected(gp) ((gp)->gadget.speed == USB_SPEED_UNKNOWN)
112
113/* status */
114#define usbhsg_status_init(gp) do {(gp)->status = 0; } while (0)
115#define usbhsg_status_set(gp, b) (gp->status |= b)
116#define usbhsg_status_clr(gp, b) (gp->status &= ~b)
117#define usbhsg_status_has(gp, b) (gp->status & b)
118
119/*
Kuninori Morimoto233f5192011-07-07 00:23:24 -0700120 * queue push/pop
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900121 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900122static void usbhsg_queue_pop(struct usbhsg_uep *uep,
123 struct usbhsg_request *ureq,
124 int status)
125{
126 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
127 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
128 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900129
130 dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
131
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900132 ureq->req.status = status;
Michal Sojka304f7e52014-09-24 22:43:19 +0200133 usb_gadget_giveback_request(&uep->ep, &ureq->req);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900134}
135
Kuninori Morimoto2cc97192011-10-10 22:03:39 -0700136static void usbhsg_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900137{
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900138 struct usbhs_pipe *pipe = pkt->pipe;
139 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
140 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900141
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900142 ureq->req.actual = pkt->actual;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900143
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900144 usbhsg_queue_pop(uep, ureq, 0);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900145}
146
Kuninori Morimotob3318722011-10-10 22:04:41 -0700147static void usbhsg_queue_push(struct usbhsg_uep *uep,
148 struct usbhsg_request *ureq)
149{
150 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
151 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
152 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
153 struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
154 struct usb_request *req = &ureq->req;
155
156 req->actual = 0;
157 req->status = -EINPROGRESS;
158 usbhs_pkt_push(pipe, pkt, usbhsg_queue_done,
Kuninori Morimoto3edeee32011-12-08 18:28:54 -0800159 req->buf, req->length, req->zero, -1);
Kuninori Morimoto654c35a2011-10-10 22:04:53 -0700160 usbhs_pkt_start(pipe);
Kuninori Morimotob3318722011-10-10 22:04:41 -0700161
162 dev_dbg(dev, "pipe %d : queue push (%d)\n",
163 usbhs_pipe_number(pipe),
164 req->length);
165}
166
Kuninori Morimoto233f5192011-07-07 00:23:24 -0700167/*
168 * dma map/unmap
169 */
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900170static int usbhsg_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
171{
Felipe Balbiade78f92011-12-19 11:57:16 +0200172 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
173 struct usb_request *req = &ureq->req;
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900174 struct usbhs_pipe *pipe = pkt->pipe;
175 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
176 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900177 enum dma_data_direction dir;
Felipe Balbiade78f92011-12-19 11:57:16 +0200178 int ret = 0;
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900179
Felipe Balbiade78f92011-12-19 11:57:16 +0200180 dir = usbhs_pipe_is_dir_host(pipe);
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900181
Felipe Balbiade78f92011-12-19 11:57:16 +0200182 if (map) {
183 /* it can not use scatter/gather */
184 WARN_ON(req->num_sgs);
185
186 ret = usb_gadget_map_request(&gpriv->gadget, req, dir);
187 if (ret < 0)
188 return ret;
189
190 pkt->dma = req->dma;
191 } else {
192 usb_gadget_unmap_request(&gpriv->gadget, req, dir);
193 }
194
195 return ret;
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900196}
197
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900198/*
199 * USB_TYPE_STANDARD / clear feature functions
200 */
201static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
202 struct usbhsg_uep *uep,
203 struct usb_ctrlrequest *ctrl)
204{
205 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
206 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
207 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
208
209 usbhs_dcp_control_transfer_done(pipe);
210
211 return 0;
212}
213
214static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
215 struct usbhsg_uep *uep,
216 struct usb_ctrlrequest *ctrl)
217{
218 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
219 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
220
221 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900222 usbhs_pipe_disable(pipe);
Kuninori Morimoto6e6db822011-10-10 22:05:30 -0700223 usbhs_pipe_sequence_data0(pipe);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900224 usbhs_pipe_enable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900225 }
226
227 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
228
Kuninori Morimoto25fa7072011-11-24 17:28:17 -0800229 usbhs_pkt_start(pipe);
230
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900231 return 0;
232}
233
Kuninori Morimoto225da3e2013-03-31 18:34:43 -0700234static struct usbhsg_recip_handle req_clear_feature = {
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900235 .name = "clear feature",
236 .device = usbhsg_recip_handler_std_control_done,
237 .interface = usbhsg_recip_handler_std_control_done,
238 .endpoint = usbhsg_recip_handler_std_clear_endpoint,
239};
240
241/*
Kuninori Morimotoced6e092011-11-24 17:27:50 -0800242 * USB_TYPE_STANDARD / set feature functions
243 */
Kuninori Morimotodfbb7f42011-11-24 17:28:35 -0800244static int usbhsg_recip_handler_std_set_device(struct usbhs_priv *priv,
245 struct usbhsg_uep *uep,
246 struct usb_ctrlrequest *ctrl)
247{
248 switch (le16_to_cpu(ctrl->wValue)) {
249 case USB_DEVICE_TEST_MODE:
250 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
251 udelay(100);
252 usbhs_sys_set_test_mode(priv, le16_to_cpu(ctrl->wIndex >> 8));
253 break;
254 default:
255 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
256 break;
257 }
258
259 return 0;
260}
261
Kuninori Morimotoced6e092011-11-24 17:27:50 -0800262static int usbhsg_recip_handler_std_set_endpoint(struct usbhs_priv *priv,
263 struct usbhsg_uep *uep,
264 struct usb_ctrlrequest *ctrl)
265{
266 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
267
268 usbhs_pipe_stall(pipe);
269
270 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
271
272 return 0;
273}
274
Kuninori Morimoto225da3e2013-03-31 18:34:43 -0700275static struct usbhsg_recip_handle req_set_feature = {
Kuninori Morimotoced6e092011-11-24 17:27:50 -0800276 .name = "set feature",
Kuninori Morimotodfbb7f42011-11-24 17:28:35 -0800277 .device = usbhsg_recip_handler_std_set_device,
Kuninori Morimotoced6e092011-11-24 17:27:50 -0800278 .interface = usbhsg_recip_handler_std_control_done,
279 .endpoint = usbhsg_recip_handler_std_set_endpoint,
280};
281
282/*
Kuninori Morimoto17f7f762011-11-24 17:28:04 -0800283 * USB_TYPE_STANDARD / get status functions
284 */
285static void __usbhsg_recip_send_complete(struct usb_ep *ep,
286 struct usb_request *req)
287{
288 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
289
290 /* free allocated recip-buffer/usb_request */
291 kfree(ureq->pkt.buf);
292 usb_ep_free_request(ep, req);
293}
294
295static void __usbhsg_recip_send_status(struct usbhsg_gpriv *gpriv,
296 unsigned short status)
297{
298 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
299 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
300 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
301 struct usb_request *req;
302 unsigned short *buf;
303
304 /* alloc new usb_request for recip */
305 req = usb_ep_alloc_request(&dcp->ep, GFP_ATOMIC);
306 if (!req) {
307 dev_err(dev, "recip request allocation fail\n");
308 return;
309 }
310
311 /* alloc recip data buffer */
312 buf = kmalloc(sizeof(*buf), GFP_ATOMIC);
313 if (!buf) {
314 usb_ep_free_request(&dcp->ep, req);
315 dev_err(dev, "recip data allocation fail\n");
316 return;
317 }
318
319 /* recip data is status */
320 *buf = cpu_to_le16(status);
321
322 /* allocated usb_request/buffer will be freed */
323 req->complete = __usbhsg_recip_send_complete;
324 req->buf = buf;
325 req->length = sizeof(*buf);
326 req->zero = 0;
327
328 /* push packet */
329 pipe->handler = &usbhs_fifo_pio_push_handler;
330 usbhsg_queue_push(dcp, usbhsg_req_to_ureq(req));
331}
332
333static int usbhsg_recip_handler_std_get_device(struct usbhs_priv *priv,
334 struct usbhsg_uep *uep,
335 struct usb_ctrlrequest *ctrl)
336{
337 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
Shimoda, Yoshihirocac402d2012-03-16 13:10:15 +0900338 unsigned short status = 0;
339
340 if (usbhsg_status_has(gpriv, USBHSG_STATUS_SELF_POWERED))
341 status = 1 << USB_DEVICE_SELF_POWERED;
Kuninori Morimoto17f7f762011-11-24 17:28:04 -0800342
343 __usbhsg_recip_send_status(gpriv, status);
344
345 return 0;
346}
347
348static int usbhsg_recip_handler_std_get_interface(struct usbhs_priv *priv,
349 struct usbhsg_uep *uep,
350 struct usb_ctrlrequest *ctrl)
351{
352 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
353 unsigned short status = 0;
354
355 __usbhsg_recip_send_status(gpriv, status);
356
357 return 0;
358}
359
360static int usbhsg_recip_handler_std_get_endpoint(struct usbhs_priv *priv,
361 struct usbhsg_uep *uep,
362 struct usb_ctrlrequest *ctrl)
363{
364 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
365 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
366 unsigned short status = 0;
367
368 if (usbhs_pipe_is_stall(pipe))
369 status = 1 << USB_ENDPOINT_HALT;
370
371 __usbhsg_recip_send_status(gpriv, status);
372
373 return 0;
374}
375
Kuninori Morimoto225da3e2013-03-31 18:34:43 -0700376static struct usbhsg_recip_handle req_get_status = {
Kuninori Morimoto17f7f762011-11-24 17:28:04 -0800377 .name = "get status",
378 .device = usbhsg_recip_handler_std_get_device,
379 .interface = usbhsg_recip_handler_std_get_interface,
380 .endpoint = usbhsg_recip_handler_std_get_endpoint,
381};
382
383/*
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900384 * USB_TYPE handler
385 */
386static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
387 struct usbhsg_recip_handle *handler,
388 struct usb_ctrlrequest *ctrl)
389{
390 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
391 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
392 struct usbhsg_uep *uep;
Kuninori Morimoto0432eed2011-06-06 14:18:54 +0900393 struct usbhs_pipe *pipe;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900394 int recip = ctrl->bRequestType & USB_RECIP_MASK;
395 int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
Jesper Juhl7983bc72012-01-16 22:42:10 +0100396 int ret = 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900397 int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
398 struct usb_ctrlrequest *ctrl);
399 char *msg;
400
401 uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
Kuninori Morimoto0432eed2011-06-06 14:18:54 +0900402 pipe = usbhsg_uep_to_pipe(uep);
403 if (!pipe) {
Kuninori Morimoto9a28b7b2011-04-21 14:10:12 +0900404 dev_err(dev, "wrong recip request\n");
Kuninori Morimoto25fa7072011-11-24 17:28:17 -0800405 return -EINVAL;
Kuninori Morimoto9a28b7b2011-04-21 14:10:12 +0900406 }
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900407
408 switch (recip) {
409 case USB_RECIP_DEVICE:
410 msg = "DEVICE";
411 func = handler->device;
412 break;
413 case USB_RECIP_INTERFACE:
414 msg = "INTERFACE";
415 func = handler->interface;
416 break;
417 case USB_RECIP_ENDPOINT:
418 msg = "ENDPOINT";
419 func = handler->endpoint;
420 break;
421 default:
422 dev_warn(dev, "unsupported RECIP(%d)\n", recip);
423 func = NULL;
424 ret = -EINVAL;
425 }
426
427 if (func) {
428 dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
429 ret = func(priv, uep, ctrl);
430 }
431
432 return ret;
433}
434
435/*
436 * irq functions
437 *
438 * it will be called from usbhs_interrupt
439 */
440static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
441 struct usbhs_irq_state *irq_state)
442{
443 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
444 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
445
Kuninori Morimoto75587f52011-10-10 22:01:51 -0700446 gpriv->gadget.speed = usbhs_bus_get_speed(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900447
448 dev_dbg(dev, "state = %x : speed : %d\n",
449 usbhs_status_get_device_state(irq_state),
450 gpriv->gadget.speed);
451
452 return 0;
453}
454
455static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
456 struct usbhs_irq_state *irq_state)
457{
458 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
459 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
460 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
461 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
462 struct usb_ctrlrequest ctrl;
463 struct usbhsg_recip_handle *recip_handler = NULL;
464 int stage = usbhs_status_get_ctrl_stage(irq_state);
465 int ret = 0;
466
467 dev_dbg(dev, "stage = %d\n", stage);
468
469 /*
470 * see Manual
471 *
472 * "Operation"
473 * - "Interrupt Function"
474 * - "Control Transfer Stage Transition Interrupt"
475 * - Fig. "Control Transfer Stage Transitions"
476 */
477
478 switch (stage) {
479 case READ_DATA_STAGE:
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700480 pipe->handler = &usbhs_fifo_pio_push_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900481 break;
482 case WRITE_DATA_STAGE:
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700483 pipe->handler = &usbhs_fifo_pio_pop_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900484 break;
485 case NODATA_STATUS_STAGE:
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700486 pipe->handler = &usbhs_ctrl_stage_end_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900487 break;
Yoshihiro Shimoda4ef35b12014-11-04 10:05:44 +0900488 case READ_STATUS_STAGE:
489 case WRITE_STATUS_STAGE:
490 usbhs_dcp_control_transfer_done(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900491 default:
492 return ret;
493 }
494
495 /*
496 * get usb request
497 */
498 usbhs_usbreq_get_val(priv, &ctrl);
499
500 switch (ctrl.bRequestType & USB_TYPE_MASK) {
501 case USB_TYPE_STANDARD:
502 switch (ctrl.bRequest) {
503 case USB_REQ_CLEAR_FEATURE:
504 recip_handler = &req_clear_feature;
505 break;
Kuninori Morimotoced6e092011-11-24 17:27:50 -0800506 case USB_REQ_SET_FEATURE:
507 recip_handler = &req_set_feature;
508 break;
Kuninori Morimoto17f7f762011-11-24 17:28:04 -0800509 case USB_REQ_GET_STATUS:
510 recip_handler = &req_get_status;
511 break;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900512 }
513 }
514
515 /*
516 * setup stage / run recip
517 */
518 if (recip_handler)
519 ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
520 else
521 ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
522
523 if (ret < 0)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900524 usbhs_pipe_stall(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900525
526 return ret;
527}
528
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900529/*
530 *
531 * usb_dcp_ops
532 *
533 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900534static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
535{
536 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto8a2c2252011-06-06 14:18:33 +0900537 struct usbhs_pkt *pkt;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900538
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900539 while (1) {
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900540 pkt = usbhs_pkt_pop(pipe, NULL);
Kuninori Morimoto8a2c2252011-06-06 14:18:33 +0900541 if (!pkt)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900542 break;
Kuninori Morimoto91b158f2011-11-24 17:28:26 -0800543
544 usbhsg_queue_pop(uep, usbhsg_pkt_to_ureq(pkt), -ECONNRESET);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900545 }
546
Kuninori Morimoto91b158f2011-11-24 17:28:26 -0800547 usbhs_pipe_disable(pipe);
548
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900549 return 0;
550}
551
552/*
553 *
554 * usb_ep_ops
555 *
556 */
557static int usbhsg_ep_enable(struct usb_ep *ep,
558 const struct usb_endpoint_descriptor *desc)
559{
560 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
561 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
562 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
563 struct usbhs_pipe *pipe;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900564 int ret = -EIO;
565
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900566 /*
567 * if it already have pipe,
568 * nothing to do
569 */
Kuninori Morimoto08e6c612011-06-09 16:48:25 +0900570 if (uep->pipe) {
571 usbhs_pipe_clear(uep->pipe);
Kuninori Morimoto6e6db822011-10-10 22:05:30 -0700572 usbhs_pipe_sequence_data0(uep->pipe);
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900573 return 0;
Kuninori Morimoto08e6c612011-06-09 16:48:25 +0900574 }
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900575
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700576 pipe = usbhs_pipe_malloc(priv,
577 usb_endpoint_type(desc),
578 usb_endpoint_dir_in(desc));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900579 if (pipe) {
580 uep->pipe = pipe;
581 pipe->mod_private = uep;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900582
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700583 /* set epnum / maxp */
Kuninori Morimotobc6fbf52011-10-10 22:04:00 -0700584 usbhs_pipe_config_update(pipe, 0,
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700585 usb_endpoint_num(desc),
586 usb_endpoint_maxp(desc));
587
Kuninori Morimoto233f5192011-07-07 00:23:24 -0700588 /*
589 * usbhs_fifo_dma_push/pop_handler try to
590 * use dmaengine if possible.
591 * It will use pio handler if impossible.
592 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900593 if (usb_endpoint_dir_in(desc))
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700594 pipe->handler = &usbhs_fifo_dma_push_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900595 else
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700596 pipe->handler = &usbhs_fifo_dma_pop_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900597
598 ret = 0;
599 }
Kuninori Morimotocb966322011-04-21 14:10:08 +0900600
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900601 return ret;
602}
603
604static int usbhsg_ep_disable(struct usb_ep *ep)
605{
606 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
Yoshihiro Shimodadfb87b82014-07-09 20:30:13 +0900607 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900608
Kazuya Mizuguchi11432052014-11-04 10:05:42 +0900609 if (!pipe)
610 return -EINVAL;
611
Kuninori Morimotod9fa2982012-12-10 22:44:07 -0800612 usbhsg_pipe_disable(uep);
Yoshihiro Shimodadfb87b82014-07-09 20:30:13 +0900613 usbhs_pipe_free(pipe);
Kuninori Morimotod9fa2982012-12-10 22:44:07 -0800614
615 uep->pipe->mod_private = NULL;
616 uep->pipe = NULL;
617
618 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900619}
620
621static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
622 gfp_t gfp_flags)
623{
624 struct usbhsg_request *ureq;
625
626 ureq = kzalloc(sizeof *ureq, gfp_flags);
627 if (!ureq)
628 return NULL;
629
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900630 usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
631
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900632 return &ureq->req;
633}
634
635static void usbhsg_ep_free_request(struct usb_ep *ep,
636 struct usb_request *req)
637{
638 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
639
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900640 WARN_ON(!list_empty(&ureq->pkt.node));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900641 kfree(ureq);
642}
643
644static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
645 gfp_t gfp_flags)
646{
647 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
648 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
649 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
650 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900651
652 /* param check */
653 if (usbhsg_is_not_connected(gpriv) ||
654 unlikely(!gpriv->driver) ||
655 unlikely(!pipe))
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900656 return -ESHUTDOWN;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900657
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900658 usbhsg_queue_push(uep, ureq);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900659
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900660 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900661}
662
663static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
664{
665 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
666 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900667 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900668
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900669 usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900670 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
671
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900672 return 0;
673}
674
675static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
676{
677 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
678 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
679 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900680 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900681 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900682 unsigned long flags;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900683
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900684 usbhsg_pipe_disable(uep);
685
686 dev_dbg(dev, "set halt %d (pipe %d)\n",
687 halt, usbhs_pipe_number(pipe));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900688
689 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900690 usbhs_lock(priv, flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900691
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900692 if (halt)
693 usbhs_pipe_stall(pipe);
694 else
695 usbhs_pipe_disable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900696
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900697 if (halt && wedge)
698 usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
699 else
700 usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900701
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900702 usbhs_unlock(priv, flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900703 /******************** spin unlock ******************/
704
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900705 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900706}
707
708static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
709{
710 return __usbhsg_ep_set_halt_wedge(ep, value, 0);
711}
712
713static int usbhsg_ep_set_wedge(struct usb_ep *ep)
714{
715 return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
716}
717
718static struct usb_ep_ops usbhsg_ep_ops = {
719 .enable = usbhsg_ep_enable,
720 .disable = usbhsg_ep_disable,
721
722 .alloc_request = usbhsg_ep_alloc_request,
723 .free_request = usbhsg_ep_free_request,
724
725 .queue = usbhsg_ep_queue,
726 .dequeue = usbhsg_ep_dequeue,
727
728 .set_halt = usbhsg_ep_set_halt,
729 .set_wedge = usbhsg_ep_set_wedge,
730};
731
732/*
Takeshi Kihara04a5def2014-11-04 10:05:43 +0900733 * pullup control
734 */
735static int usbhsg_can_pullup(struct usbhs_priv *priv)
736{
737 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
738
739 return gpriv->driver &&
740 usbhsg_status_has(gpriv, USBHSG_STATUS_SOFT_CONNECT);
741}
742
743static void usbhsg_update_pullup(struct usbhs_priv *priv)
744{
745 if (usbhsg_can_pullup(priv))
746 usbhs_sys_function_pullup(priv, 1);
747 else
748 usbhs_sys_function_pullup(priv, 0);
749}
750
751/*
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900752 * usb module start/end
753 */
754static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
755{
756 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
757 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
758 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
759 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900760 unsigned long flags;
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900761 int ret = 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900762
763 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900764 usbhs_lock(priv, flags);
765
766 usbhsg_status_set(gpriv, status);
767 if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
768 usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
769 ret = -1; /* not ready */
770
771 usbhs_unlock(priv, flags);
772 /******************** spin unlock ********************/
773
774 if (ret < 0)
775 return 0; /* not ready is not error */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900776
777 /*
778 * enable interrupt and systems if ready
779 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900780 dev_dbg(dev, "start gadget\n");
781
782 /*
783 * pipe initialize and enable DCP
784 */
Yoshihiro Shimodacdeb7942014-11-04 10:05:45 +0900785 usbhs_fifo_init(priv);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900786 usbhs_pipe_init(priv,
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900787 usbhsg_dma_map_ctrl);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900788
Kuninori Morimotod9fa2982012-12-10 22:44:07 -0800789 /* dcp init instead of usbhsg_ep_enable() */
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900790 dcp->pipe = usbhs_dcp_malloc(priv);
791 dcp->pipe->mod_private = dcp;
Kuninori Morimotobc6fbf52011-10-10 22:04:00 -0700792 usbhs_pipe_config_update(dcp->pipe, 0, 0, 64);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900793
794 /*
795 * system config enble
796 * - HI speed
797 * - function
798 * - usb module
799 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900800 usbhs_sys_function_ctrl(priv, 1);
Takeshi Kihara04a5def2014-11-04 10:05:43 +0900801 usbhsg_update_pullup(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900802
803 /*
804 * enable irq callback
805 */
806 mod->irq_dev_state = usbhsg_irq_dev_state;
807 mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900808 usbhs_irq_callback_update(priv, mod);
809
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900810 return 0;
811}
812
813static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
814{
815 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
816 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
817 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
818 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900819 unsigned long flags;
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900820 int ret = 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900821
822 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900823 usbhs_lock(priv, flags);
824
825 usbhsg_status_clr(gpriv, status);
826 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
827 !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
828 ret = -1; /* already done */
829
830 usbhs_unlock(priv, flags);
831 /******************** spin unlock ********************/
832
833 if (ret < 0)
834 return 0; /* already done is not error */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900835
836 /*
837 * disable interrupt and systems if 1st try
838 */
Kuninori Morimotodad67392011-06-06 14:18:28 +0900839 usbhs_fifo_quit(priv);
840
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900841 /* disable all irq */
842 mod->irq_dev_state = NULL;
843 mod->irq_ctrl_stage = NULL;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900844 usbhs_irq_callback_update(priv, mod);
845
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900846 gpriv->gadget.speed = USB_SPEED_UNKNOWN;
847
848 /* disable sys */
Kuninori Morimotodfbb7f42011-11-24 17:28:35 -0800849 usbhs_sys_set_test_mode(priv, 0);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900850 usbhs_sys_function_ctrl(priv, 0);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900851
Kuninori Morimotod9fa2982012-12-10 22:44:07 -0800852 usbhsg_ep_disable(&dcp->ep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900853
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900854 dev_dbg(dev, "stop gadget\n");
855
856 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900857}
858
859/*
860 *
861 * linux usb function
862 *
863 */
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300864static int usbhsg_gadget_start(struct usb_gadget *gadget,
865 struct usb_gadget_driver *driver)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900866{
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300867 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -0800868 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900869
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300870 if (!driver ||
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900871 !driver->setup ||
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100872 driver->max_speed < USB_SPEED_FULL)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900873 return -EINVAL;
Kuninori Morimoto3b872182011-07-03 17:42:35 -0700874
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900875 /* first hook up the driver ... */
876 gpriv->driver = driver;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900877
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900878 return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900879}
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900880
Felipe Balbi22835b82014-10-17 12:05:12 -0500881static int usbhsg_gadget_stop(struct usb_gadget *gadget)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900882{
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300883 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -0800884 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900885
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900886 usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900887 gpriv->driver = NULL;
888
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900889 return 0;
890}
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900891
892/*
893 * usb gadget ops
894 */
895static int usbhsg_get_frame(struct usb_gadget *gadget)
896{
897 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
898 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
899
900 return usbhs_frame_get_num(priv);
901}
902
kuninori.morimoto.gx@renesas.com4cd2f592012-10-15 23:24:19 -0700903static int usbhsg_pullup(struct usb_gadget *gadget, int is_on)
904{
905 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
906 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
Takeshi Kihara04a5def2014-11-04 10:05:43 +0900907 unsigned long flags;
kuninori.morimoto.gx@renesas.com4cd2f592012-10-15 23:24:19 -0700908
Takeshi Kihara04a5def2014-11-04 10:05:43 +0900909 usbhs_lock(priv, flags);
910 if (is_on)
911 usbhsg_status_set(gpriv, USBHSG_STATUS_SOFT_CONNECT);
912 else
913 usbhsg_status_clr(gpriv, USBHSG_STATUS_SOFT_CONNECT);
914 usbhsg_update_pullup(priv);
915 usbhs_unlock(priv, flags);
kuninori.morimoto.gx@renesas.com4cd2f592012-10-15 23:24:19 -0700916
917 return 0;
918}
919
Shimoda, Yoshihirocac402d2012-03-16 13:10:15 +0900920static int usbhsg_set_selfpowered(struct usb_gadget *gadget, int is_self)
921{
922 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
923
924 if (is_self)
925 usbhsg_status_set(gpriv, USBHSG_STATUS_SELF_POWERED);
926 else
927 usbhsg_status_clr(gpriv, USBHSG_STATUS_SELF_POWERED);
928
929 return 0;
930}
931
Felipe Balbieeef4582013-01-24 17:58:16 +0200932static const struct usb_gadget_ops usbhsg_gadget_ops = {
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900933 .get_frame = usbhsg_get_frame,
Shimoda, Yoshihirocac402d2012-03-16 13:10:15 +0900934 .set_selfpowered = usbhsg_set_selfpowered,
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300935 .udc_start = usbhsg_gadget_start,
936 .udc_stop = usbhsg_gadget_stop,
kuninori.morimoto.gx@renesas.com4cd2f592012-10-15 23:24:19 -0700937 .pullup = usbhsg_pullup,
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900938};
939
940static int usbhsg_start(struct usbhs_priv *priv)
941{
942 return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
943}
944
945static int usbhsg_stop(struct usbhs_priv *priv)
946{
Kuninori Morimoto8885a892011-11-17 18:19:38 -0800947 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
948
949 /* cable disconnect */
950 if (gpriv->driver &&
951 gpriv->driver->disconnect)
952 gpriv->driver->disconnect(&gpriv->gadget);
953
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900954 return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
955}
956
Kuninori Morimotob7a8d172011-10-26 19:33:49 -0700957int usbhs_mod_gadget_probe(struct usbhs_priv *priv)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900958{
959 struct usbhsg_gpriv *gpriv;
960 struct usbhsg_uep *uep;
961 struct device *dev = usbhs_priv_to_dev(priv);
962 int pipe_size = usbhs_get_dparam(priv, pipe_size);
963 int i;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300964 int ret;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900965
966 gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
967 if (!gpriv) {
968 dev_err(dev, "Could not allocate gadget priv\n");
969 return -ENOMEM;
970 }
971
972 uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
973 if (!uep) {
974 dev_err(dev, "Could not allocate ep\n");
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300975 ret = -ENOMEM;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900976 goto usbhs_mod_gadget_probe_err_gpriv;
977 }
978
979 /*
980 * CAUTION
981 *
982 * There is no guarantee that it is possible to access usb module here.
983 * Don't accesses to it.
984 * The accesse will be enable after "usbhsg_start"
985 */
986
987 /*
988 * register itself
989 */
990 usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
991
992 /* init gpriv */
993 gpriv->mod.name = "gadget";
994 gpriv->mod.start = usbhsg_start;
995 gpriv->mod.stop = usbhsg_stop;
996 gpriv->uep = uep;
997 gpriv->uep_size = pipe_size;
998 usbhsg_status_init(gpriv);
999
1000 /*
1001 * init gadget
1002 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001003 gpriv->gadget.dev.parent = dev;
1004 gpriv->gadget.name = "renesas_usbhs_udc";
1005 gpriv->gadget.ops = &usbhsg_gadget_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01001006 gpriv->gadget.max_speed = USB_SPEED_HIGH;
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001007
1008 INIT_LIST_HEAD(&gpriv->gadget.ep_list);
1009
1010 /*
1011 * init usb_ep
1012 */
1013 usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
1014 uep->gpriv = gpriv;
Kuninori Morimoto58482942012-12-10 22:43:45 -08001015 uep->pipe = NULL;
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001016 snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
1017
1018 uep->ep.name = uep->ep_name;
1019 uep->ep.ops = &usbhsg_ep_ops;
1020 INIT_LIST_HEAD(&uep->ep.ep_list);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001021
1022 /* init DCP */
1023 if (usbhsg_is_dcp(uep)) {
1024 gpriv->gadget.ep0 = &uep->ep;
Robert Baldygae117e742013-12-13 12:23:38 +01001025 usb_ep_set_maxpacket_limit(&uep->ep, 64);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001026 }
1027 /* init normal pipe */
1028 else {
Robert Baldygae117e742013-12-13 12:23:38 +01001029 usb_ep_set_maxpacket_limit(&uep->ep, 512);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001030 list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
1031 }
1032 }
1033
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001034 ret = usb_add_gadget_udc(dev, &gpriv->gadget);
1035 if (ret)
Felipe Balbi0972ef72013-01-24 17:08:01 +02001036 goto err_add_udc;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001037
1038
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001039 dev_info(dev, "gadget probed\n");
1040
1041 return 0;
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -08001042
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001043err_add_udc:
1044 kfree(gpriv->uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001045
1046usbhs_mod_gadget_probe_err_gpriv:
1047 kfree(gpriv);
1048
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001049 return ret;
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001050}
1051
Kuninori Morimotob7a8d172011-10-26 19:33:49 -07001052void usbhs_mod_gadget_remove(struct usbhs_priv *priv)
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001053{
1054 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1055
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001056 usb_del_gadget_udc(&gpriv->gadget);
Kuninori Morimoto3b872182011-07-03 17:42:35 -07001057
Sebastian Andrzej Siewior3af51ac2011-06-03 19:50:48 +02001058 kfree(gpriv->uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001059 kfree(gpriv);
1060}