blob: b5a5ba7efb5e6ee94e92c981d53b0460382c473b [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 */
17#include <linux/io.h>
18#include <linux/module.h>
19#include <linux/platform_device.h>
20#include <linux/usb/ch9.h>
21#include <linux/usb/gadget.h>
22#include "common.h"
23
24/*
25 * struct
26 */
27struct usbhsg_request {
28 struct usb_request req;
Kuninori Morimoto4bd04812011-06-06 14:18:07 +090029 struct usbhs_pkt pkt;
Kuninori Morimoto2f983822011-04-05 11:40:54 +090030};
31
32#define EP_NAME_SIZE 8
33struct usbhsg_gpriv;
Kuninori Morimoto2f983822011-04-05 11:40:54 +090034struct usbhsg_uep {
35 struct usb_ep ep;
36 struct usbhs_pipe *pipe;
Kuninori Morimoto2f983822011-04-05 11:40:54 +090037
38 char ep_name[EP_NAME_SIZE];
39
40 struct usbhsg_gpriv *gpriv;
Kuninori Morimotodad67392011-06-06 14:18:28 +090041 struct usbhs_pkt_handle *handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +090042};
43
44struct usbhsg_gpriv {
45 struct usb_gadget gadget;
46 struct usbhs_mod mod;
47
48 struct usbhsg_uep *uep;
49 int uep_size;
50
51 struct usb_gadget_driver *driver;
52
53 u32 status;
54#define USBHSG_STATUS_STARTED (1 << 0)
55#define USBHSG_STATUS_REGISTERD (1 << 1)
56#define USBHSG_STATUS_WEDGE (1 << 2)
57};
58
Kuninori Morimoto2f983822011-04-05 11:40:54 +090059struct usbhsg_recip_handle {
60 char *name;
61 int (*device)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
62 struct usb_ctrlrequest *ctrl);
63 int (*interface)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
64 struct usb_ctrlrequest *ctrl);
65 int (*endpoint)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
66 struct usb_ctrlrequest *ctrl);
67};
68
69/*
70 * macro
71 */
72#define usbhsg_priv_to_gpriv(priv) \
73 container_of( \
74 usbhs_mod_get(priv, USBHS_GADGET), \
75 struct usbhsg_gpriv, mod)
76
77#define __usbhsg_for_each_uep(start, pos, g, i) \
78 for (i = start, pos = (g)->uep; \
79 i < (g)->uep_size; \
80 i++, pos = (g)->uep + i)
81
82#define usbhsg_for_each_uep(pos, gpriv, i) \
83 __usbhsg_for_each_uep(1, pos, gpriv, i)
84
85#define usbhsg_for_each_uep_with_dcp(pos, gpriv, i) \
86 __usbhsg_for_each_uep(0, pos, gpriv, i)
87
88#define usbhsg_gadget_to_gpriv(g)\
89 container_of(g, struct usbhsg_gpriv, gadget)
90
91#define usbhsg_req_to_ureq(r)\
92 container_of(r, struct usbhsg_request, req)
93
94#define usbhsg_ep_to_uep(e) container_of(e, struct usbhsg_uep, ep)
Kuninori Morimoto2f983822011-04-05 11:40:54 +090095#define usbhsg_gpriv_to_dev(gp) usbhs_priv_to_dev((gp)->mod.priv)
96#define usbhsg_gpriv_to_priv(gp) ((gp)->mod.priv)
97#define usbhsg_gpriv_to_dcp(gp) ((gp)->uep)
98#define usbhsg_gpriv_to_nth_uep(gp, i) ((gp)->uep + i)
99#define usbhsg_uep_to_gpriv(u) ((u)->gpriv)
100#define usbhsg_uep_to_pipe(u) ((u)->pipe)
101#define usbhsg_pipe_to_uep(p) ((p)->mod_private)
102#define usbhsg_is_dcp(u) ((u) == usbhsg_gpriv_to_dcp((u)->gpriv))
103
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900104#define usbhsg_ureq_to_pkt(u) (&(u)->pkt)
105#define usbhsg_pkt_to_ureq(i) \
106 container_of(i, struct usbhsg_request, pkt)
107
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900108#define usbhsg_is_not_connected(gp) ((gp)->gadget.speed == USB_SPEED_UNKNOWN)
109
110/* status */
111#define usbhsg_status_init(gp) do {(gp)->status = 0; } while (0)
112#define usbhsg_status_set(gp, b) (gp->status |= b)
113#define usbhsg_status_clr(gp, b) (gp->status &= ~b)
114#define usbhsg_status_has(gp, b) (gp->status & b)
115
116/*
117 * list push/pop
118 */
119static void usbhsg_queue_push(struct usbhsg_uep *uep,
120 struct usbhsg_request *ureq)
121{
122 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
123 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
124 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900125 struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900126 struct usb_request *req = &ureq->req;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900127
Kuninori Morimotodad67392011-06-06 14:18:28 +0900128 usbhs_pkt_push(pipe, pkt, uep->handler,
129 req->buf, req->length, req->zero);
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900130 req->actual = 0;
131 req->status = -EINPROGRESS;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900132
133 dev_dbg(dev, "pipe %d : queue push (%d)\n",
134 usbhs_pipe_number(pipe),
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900135 req->length);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900136}
137
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900138static void usbhsg_queue_start(struct usbhsg_uep *uep)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900139{
Kuninori Morimotodad67392011-06-06 14:18:28 +0900140 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900141
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900142 usbhs_pkt_start(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900143}
144
145static void usbhsg_queue_pop(struct usbhsg_uep *uep,
146 struct usbhsg_request *ureq,
147 int status)
148{
149 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
150 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
151 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900152
153 dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
154
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900155 ureq->req.status = status;
156 ureq->req.complete(&uep->ep, &ureq->req);
157
158 /* more request ? */
159 if (0 == status)
Kuninori Morimotodad67392011-06-06 14:18:28 +0900160 usbhsg_queue_start(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900161}
162
Kuninori Morimotodad67392011-06-06 14:18:28 +0900163static void usbhsg_queue_done(struct usbhs_pkt *pkt)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900164{
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900165 struct usbhs_pipe *pipe = pkt->pipe;
166 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
167 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900168
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900169 ureq->req.actual = pkt->actual;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900170
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900171 usbhsg_queue_pop(uep, ureq, 0);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900172}
173
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900174/*
175 * USB_TYPE_STANDARD / clear feature functions
176 */
177static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
178 struct usbhsg_uep *uep,
179 struct usb_ctrlrequest *ctrl)
180{
181 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
182 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
183 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
184
185 usbhs_dcp_control_transfer_done(pipe);
186
187 return 0;
188}
189
190static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
191 struct usbhsg_uep *uep,
192 struct usb_ctrlrequest *ctrl)
193{
194 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
195 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
196
197 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900198 usbhs_pipe_disable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900199 usbhs_pipe_clear_sequence(pipe);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900200 usbhs_pipe_enable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900201 }
202
203 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
204
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900205 return 0;
206}
207
208struct usbhsg_recip_handle req_clear_feature = {
209 .name = "clear feature",
210 .device = usbhsg_recip_handler_std_control_done,
211 .interface = usbhsg_recip_handler_std_control_done,
212 .endpoint = usbhsg_recip_handler_std_clear_endpoint,
213};
214
215/*
216 * USB_TYPE handler
217 */
218static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
219 struct usbhsg_recip_handle *handler,
220 struct usb_ctrlrequest *ctrl)
221{
222 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
223 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
224 struct usbhsg_uep *uep;
225 int recip = ctrl->bRequestType & USB_RECIP_MASK;
226 int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
227 int ret;
228 int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
229 struct usb_ctrlrequest *ctrl);
230 char *msg;
231
232 uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
Kuninori Morimoto9a28b7b2011-04-21 14:10:12 +0900233 if (!usbhsg_uep_to_pipe(uep)) {
234 dev_err(dev, "wrong recip request\n");
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900235 ret = -EINVAL;
236 goto usbhsg_recip_run_handle_end;
Kuninori Morimoto9a28b7b2011-04-21 14:10:12 +0900237 }
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900238
239 switch (recip) {
240 case USB_RECIP_DEVICE:
241 msg = "DEVICE";
242 func = handler->device;
243 break;
244 case USB_RECIP_INTERFACE:
245 msg = "INTERFACE";
246 func = handler->interface;
247 break;
248 case USB_RECIP_ENDPOINT:
249 msg = "ENDPOINT";
250 func = handler->endpoint;
251 break;
252 default:
253 dev_warn(dev, "unsupported RECIP(%d)\n", recip);
254 func = NULL;
255 ret = -EINVAL;
256 }
257
258 if (func) {
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900259 unsigned long flags;
260
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900261 dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900262
263 /******************** spin lock ********************/
264 usbhs_lock(priv, flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900265 ret = func(priv, uep, ctrl);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900266 usbhs_unlock(priv, flags);
267 /******************** spin unlock ******************/
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900268 }
269
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900270usbhsg_recip_run_handle_end:
271 usbhsg_queue_start(uep);
272
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900273 return ret;
274}
275
276/*
277 * irq functions
278 *
279 * it will be called from usbhs_interrupt
280 */
281static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
282 struct usbhs_irq_state *irq_state)
283{
284 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
285 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
286
287 gpriv->gadget.speed = usbhs_status_get_usb_speed(irq_state);
288
289 dev_dbg(dev, "state = %x : speed : %d\n",
290 usbhs_status_get_device_state(irq_state),
291 gpriv->gadget.speed);
292
293 return 0;
294}
295
296static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
297 struct usbhs_irq_state *irq_state)
298{
299 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
300 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
301 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
302 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
303 struct usb_ctrlrequest ctrl;
304 struct usbhsg_recip_handle *recip_handler = NULL;
305 int stage = usbhs_status_get_ctrl_stage(irq_state);
306 int ret = 0;
307
308 dev_dbg(dev, "stage = %d\n", stage);
309
310 /*
311 * see Manual
312 *
313 * "Operation"
314 * - "Interrupt Function"
315 * - "Control Transfer Stage Transition Interrupt"
316 * - Fig. "Control Transfer Stage Transitions"
317 */
318
319 switch (stage) {
320 case READ_DATA_STAGE:
Kuninori Morimotodad67392011-06-06 14:18:28 +0900321 dcp->handler = &usbhs_fifo_push_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900322 break;
323 case WRITE_DATA_STAGE:
Kuninori Morimotodad67392011-06-06 14:18:28 +0900324 dcp->handler = &usbhs_fifo_pop_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900325 break;
326 case NODATA_STATUS_STAGE:
Kuninori Morimotodad67392011-06-06 14:18:28 +0900327 dcp->handler = &usbhs_ctrl_stage_end_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900328 break;
329 default:
330 return ret;
331 }
332
333 /*
334 * get usb request
335 */
336 usbhs_usbreq_get_val(priv, &ctrl);
337
338 switch (ctrl.bRequestType & USB_TYPE_MASK) {
339 case USB_TYPE_STANDARD:
340 switch (ctrl.bRequest) {
341 case USB_REQ_CLEAR_FEATURE:
342 recip_handler = &req_clear_feature;
343 break;
344 }
345 }
346
347 /*
348 * setup stage / run recip
349 */
350 if (recip_handler)
351 ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
352 else
353 ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
354
355 if (ret < 0)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900356 usbhs_pipe_stall(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900357
358 return ret;
359}
360
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900361/*
362 *
363 * usb_dcp_ops
364 *
365 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900366static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
367{
368 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto8a2c2252011-06-06 14:18:33 +0900369 struct usbhs_pkt *pkt;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900370
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900371 usbhs_pipe_disable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900372
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900373 while (1) {
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900374 pkt = usbhs_pkt_pop(pipe, NULL);
Kuninori Morimoto8a2c2252011-06-06 14:18:33 +0900375 if (!pkt)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900376 break;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900377 }
378
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900379 return 0;
380}
381
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900382static void usbhsg_uep_init(struct usbhsg_gpriv *gpriv)
383{
384 int i;
385 struct usbhsg_uep *uep;
386
387 usbhsg_for_each_uep_with_dcp(uep, gpriv, i)
388 uep->pipe = NULL;
389}
390
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900391/*
392 *
393 * usb_ep_ops
394 *
395 */
396static int usbhsg_ep_enable(struct usb_ep *ep,
397 const struct usb_endpoint_descriptor *desc)
398{
399 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
400 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
401 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
402 struct usbhs_pipe *pipe;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900403 int ret = -EIO;
404
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900405 /*
406 * if it already have pipe,
407 * nothing to do
408 */
409 if (uep->pipe)
410 return 0;
411
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900412 pipe = usbhs_pipe_malloc(priv, desc);
413 if (pipe) {
414 uep->pipe = pipe;
415 pipe->mod_private = uep;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900416
417 if (usb_endpoint_dir_in(desc))
Kuninori Morimotodad67392011-06-06 14:18:28 +0900418 uep->handler = &usbhs_fifo_push_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900419 else
Kuninori Morimotodad67392011-06-06 14:18:28 +0900420 uep->handler = &usbhs_fifo_pop_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900421
422 ret = 0;
423 }
Kuninori Morimotocb966322011-04-21 14:10:08 +0900424
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900425 return ret;
426}
427
428static int usbhsg_ep_disable(struct usb_ep *ep)
429{
430 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900431
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900432 return usbhsg_pipe_disable(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900433}
434
435static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
436 gfp_t gfp_flags)
437{
438 struct usbhsg_request *ureq;
439
440 ureq = kzalloc(sizeof *ureq, gfp_flags);
441 if (!ureq)
442 return NULL;
443
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900444 usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
445
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900446 return &ureq->req;
447}
448
449static void usbhsg_ep_free_request(struct usb_ep *ep,
450 struct usb_request *req)
451{
452 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
453
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900454 WARN_ON(!list_empty(&ureq->pkt.node));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900455 kfree(ureq);
456}
457
458static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
459 gfp_t gfp_flags)
460{
461 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
462 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
463 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
464 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900465
466 /* param check */
467 if (usbhsg_is_not_connected(gpriv) ||
468 unlikely(!gpriv->driver) ||
469 unlikely(!pipe))
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900470 return -ESHUTDOWN;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900471
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900472 usbhsg_queue_push(uep, ureq);
Kuninori Morimotodad67392011-06-06 14:18:28 +0900473 usbhsg_queue_start(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900474
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900475 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900476}
477
478static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
479{
480 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
481 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900482 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900483
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900484 usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900485 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
486
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900487 return 0;
488}
489
490static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
491{
492 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
493 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
494 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900495 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900496 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900497 unsigned long flags;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900498
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900499 usbhsg_pipe_disable(uep);
500
501 dev_dbg(dev, "set halt %d (pipe %d)\n",
502 halt, usbhs_pipe_number(pipe));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900503
504 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900505 usbhs_lock(priv, flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900506
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900507 if (halt)
508 usbhs_pipe_stall(pipe);
509 else
510 usbhs_pipe_disable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900511
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900512 if (halt && wedge)
513 usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
514 else
515 usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900516
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900517 usbhs_unlock(priv, flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900518 /******************** spin unlock ******************/
519
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900520 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900521}
522
523static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
524{
525 return __usbhsg_ep_set_halt_wedge(ep, value, 0);
526}
527
528static int usbhsg_ep_set_wedge(struct usb_ep *ep)
529{
530 return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
531}
532
533static struct usb_ep_ops usbhsg_ep_ops = {
534 .enable = usbhsg_ep_enable,
535 .disable = usbhsg_ep_disable,
536
537 .alloc_request = usbhsg_ep_alloc_request,
538 .free_request = usbhsg_ep_free_request,
539
540 .queue = usbhsg_ep_queue,
541 .dequeue = usbhsg_ep_dequeue,
542
543 .set_halt = usbhsg_ep_set_halt,
544 .set_wedge = usbhsg_ep_set_wedge,
545};
546
547/*
548 * usb module start/end
549 */
550static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
551{
552 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
553 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
554 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
555 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900556 unsigned long flags;
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900557 int ret = 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900558
559 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900560 usbhs_lock(priv, flags);
561
562 usbhsg_status_set(gpriv, status);
563 if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
564 usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
565 ret = -1; /* not ready */
566
567 usbhs_unlock(priv, flags);
568 /******************** spin unlock ********************/
569
570 if (ret < 0)
571 return 0; /* not ready is not error */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900572
573 /*
574 * enable interrupt and systems if ready
575 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900576 dev_dbg(dev, "start gadget\n");
577
578 /*
579 * pipe initialize and enable DCP
580 */
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900581 usbhs_pipe_init(priv,
Kuninori Morimotodad67392011-06-06 14:18:28 +0900582 usbhsg_queue_done);
583 usbhs_fifo_init(priv);
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900584 usbhsg_uep_init(gpriv);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900585
586 /* dcp init */
587 dcp->pipe = usbhs_dcp_malloc(priv);
588 dcp->pipe->mod_private = dcp;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900589
590 /*
591 * system config enble
592 * - HI speed
593 * - function
594 * - usb module
595 */
596 usbhs_sys_hispeed_ctrl(priv, 1);
597 usbhs_sys_function_ctrl(priv, 1);
598 usbhs_sys_usb_ctrl(priv, 1);
599
600 /*
601 * enable irq callback
602 */
603 mod->irq_dev_state = usbhsg_irq_dev_state;
604 mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900605 usbhs_irq_callback_update(priv, mod);
606
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900607 return 0;
608}
609
610static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
611{
612 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
613 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
614 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
615 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900616 unsigned long flags;
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900617 int ret = 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900618
619 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900620 usbhs_lock(priv, flags);
621
622 usbhsg_status_clr(gpriv, status);
623 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
624 !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
625 ret = -1; /* already done */
626
627 usbhs_unlock(priv, flags);
628 /******************** spin unlock ********************/
629
630 if (ret < 0)
631 return 0; /* already done is not error */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900632
633 /*
634 * disable interrupt and systems if 1st try
635 */
Kuninori Morimotodad67392011-06-06 14:18:28 +0900636 usbhs_fifo_quit(priv);
637
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900638 /* disable all irq */
639 mod->irq_dev_state = NULL;
640 mod->irq_ctrl_stage = NULL;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900641 usbhs_irq_callback_update(priv, mod);
642
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900643 gpriv->gadget.speed = USB_SPEED_UNKNOWN;
644
645 /* disable sys */
646 usbhs_sys_hispeed_ctrl(priv, 0);
647 usbhs_sys_function_ctrl(priv, 0);
648 usbhs_sys_usb_ctrl(priv, 0);
649
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900650 usbhsg_pipe_disable(dcp);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900651
652 if (gpriv->driver &&
653 gpriv->driver->disconnect)
654 gpriv->driver->disconnect(&gpriv->gadget);
655
656 dev_dbg(dev, "stop gadget\n");
657
658 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900659}
660
661/*
662 *
663 * linux usb function
664 *
665 */
666struct usbhsg_gpriv *the_controller;
667int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
668 int (*bind)(struct usb_gadget *))
669{
670 struct usbhsg_gpriv *gpriv = the_controller;
671 struct usbhs_priv *priv;
672 struct device *dev;
673 int ret;
674
675 if (!bind ||
676 !driver ||
677 !driver->setup ||
678 driver->speed != USB_SPEED_HIGH)
679 return -EINVAL;
680 if (!gpriv)
681 return -ENODEV;
682 if (gpriv->driver)
683 return -EBUSY;
684
685 dev = usbhsg_gpriv_to_dev(gpriv);
686 priv = usbhsg_gpriv_to_priv(gpriv);
687
688 /* first hook up the driver ... */
689 gpriv->driver = driver;
690 gpriv->gadget.dev.driver = &driver->driver;
691
692 ret = device_add(&gpriv->gadget.dev);
693 if (ret) {
694 dev_err(dev, "device_add error %d\n", ret);
695 goto add_fail;
696 }
697
698 ret = bind(&gpriv->gadget);
699 if (ret) {
700 dev_err(dev, "bind to driver %s error %d\n",
701 driver->driver.name, ret);
702 goto bind_fail;
703 }
704
705 dev_dbg(dev, "bind %s\n", driver->driver.name);
706
707 return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
708
709bind_fail:
710 device_del(&gpriv->gadget.dev);
711add_fail:
712 gpriv->driver = NULL;
713 gpriv->gadget.dev.driver = NULL;
714
715 return ret;
716}
717EXPORT_SYMBOL(usb_gadget_probe_driver);
718
719int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
720{
721 struct usbhsg_gpriv *gpriv = the_controller;
722 struct usbhs_priv *priv;
723 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
724
725 if (!gpriv)
726 return -ENODEV;
727
728 if (!driver ||
729 !driver->unbind ||
730 driver != gpriv->driver)
731 return -EINVAL;
732
733 dev = usbhsg_gpriv_to_dev(gpriv);
734 priv = usbhsg_gpriv_to_priv(gpriv);
735
736 usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
737 device_del(&gpriv->gadget.dev);
738 gpriv->driver = NULL;
739
740 if (driver->disconnect)
741 driver->disconnect(&gpriv->gadget);
742
743 driver->unbind(&gpriv->gadget);
744 dev_dbg(dev, "unbind %s\n", driver->driver.name);
745
746 return 0;
747}
748EXPORT_SYMBOL(usb_gadget_unregister_driver);
749
750/*
751 * usb gadget ops
752 */
753static int usbhsg_get_frame(struct usb_gadget *gadget)
754{
755 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
756 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
757
758 return usbhs_frame_get_num(priv);
759}
760
761static struct usb_gadget_ops usbhsg_gadget_ops = {
762 .get_frame = usbhsg_get_frame,
763};
764
765static int usbhsg_start(struct usbhs_priv *priv)
766{
767 return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
768}
769
770static int usbhsg_stop(struct usbhs_priv *priv)
771{
772 return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
773}
774
775int __devinit usbhs_mod_gadget_probe(struct usbhs_priv *priv)
776{
777 struct usbhsg_gpriv *gpriv;
778 struct usbhsg_uep *uep;
779 struct device *dev = usbhs_priv_to_dev(priv);
780 int pipe_size = usbhs_get_dparam(priv, pipe_size);
781 int i;
782
783 gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
784 if (!gpriv) {
785 dev_err(dev, "Could not allocate gadget priv\n");
786 return -ENOMEM;
787 }
788
789 uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
790 if (!uep) {
791 dev_err(dev, "Could not allocate ep\n");
792 goto usbhs_mod_gadget_probe_err_gpriv;
793 }
794
795 /*
796 * CAUTION
797 *
798 * There is no guarantee that it is possible to access usb module here.
799 * Don't accesses to it.
800 * The accesse will be enable after "usbhsg_start"
801 */
802
803 /*
804 * register itself
805 */
806 usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
807
808 /* init gpriv */
809 gpriv->mod.name = "gadget";
810 gpriv->mod.start = usbhsg_start;
811 gpriv->mod.stop = usbhsg_stop;
812 gpriv->uep = uep;
813 gpriv->uep_size = pipe_size;
814 usbhsg_status_init(gpriv);
815
816 /*
817 * init gadget
818 */
819 device_initialize(&gpriv->gadget.dev);
820 dev_set_name(&gpriv->gadget.dev, "gadget");
821 gpriv->gadget.dev.parent = dev;
822 gpriv->gadget.name = "renesas_usbhs_udc";
823 gpriv->gadget.ops = &usbhsg_gadget_ops;
824 gpriv->gadget.is_dualspeed = 1;
825
826 INIT_LIST_HEAD(&gpriv->gadget.ep_list);
827
828 /*
829 * init usb_ep
830 */
831 usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
832 uep->gpriv = gpriv;
833 snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
834
835 uep->ep.name = uep->ep_name;
836 uep->ep.ops = &usbhsg_ep_ops;
837 INIT_LIST_HEAD(&uep->ep.ep_list);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900838
839 /* init DCP */
840 if (usbhsg_is_dcp(uep)) {
841 gpriv->gadget.ep0 = &uep->ep;
842 uep->ep.maxpacket = 64;
843 }
844 /* init normal pipe */
845 else {
846 uep->ep.maxpacket = 512;
847 list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
848 }
849 }
850
851 the_controller = gpriv;
852
853 dev_info(dev, "gadget probed\n");
854
855 return 0;
856
857usbhs_mod_gadget_probe_err_gpriv:
858 kfree(gpriv);
859
860 return -ENOMEM;
861}
862
863void __devexit usbhs_mod_gadget_remove(struct usbhs_priv *priv)
864{
865 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
866
867 kfree(gpriv);
868}