blob: 89d2b16fbb108149174e316f6b4733612779968a [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 Morimoto659d4952011-06-06 14:18:23 +0900128 req->actual = 0;
129 req->status = -EINPROGRESS;
Kuninori Morimoto0432eed2011-06-06 14:18:54 +0900130 usbhs_pkt_push(pipe, pkt, uep->handler,
131 req->buf, req->length, req->zero);
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 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);
143 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
144 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900145
146 dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
147
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900148 ureq->req.status = status;
149 ureq->req.complete(&uep->ep, &ureq->req);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900150}
151
Kuninori Morimotodad67392011-06-06 14:18:28 +0900152static void usbhsg_queue_done(struct usbhs_pkt *pkt)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900153{
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900154 struct usbhs_pipe *pipe = pkt->pipe;
155 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
156 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
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
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900160 usbhsg_queue_pop(uep, ureq, 0);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900161}
162
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900163/*
164 * USB_TYPE_STANDARD / clear feature functions
165 */
166static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
167 struct usbhsg_uep *uep,
168 struct usb_ctrlrequest *ctrl)
169{
170 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
171 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
172 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
173
174 usbhs_dcp_control_transfer_done(pipe);
175
176 return 0;
177}
178
179static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
180 struct usbhsg_uep *uep,
181 struct usb_ctrlrequest *ctrl)
182{
183 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
184 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
185
186 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900187 usbhs_pipe_disable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900188 usbhs_pipe_clear_sequence(pipe);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900189 usbhs_pipe_enable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900190 }
191
192 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
193
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900194 return 0;
195}
196
197struct usbhsg_recip_handle req_clear_feature = {
198 .name = "clear feature",
199 .device = usbhsg_recip_handler_std_control_done,
200 .interface = usbhsg_recip_handler_std_control_done,
201 .endpoint = usbhsg_recip_handler_std_clear_endpoint,
202};
203
204/*
205 * USB_TYPE handler
206 */
207static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
208 struct usbhsg_recip_handle *handler,
209 struct usb_ctrlrequest *ctrl)
210{
211 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
212 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
213 struct usbhsg_uep *uep;
Kuninori Morimoto0432eed2011-06-06 14:18:54 +0900214 struct usbhs_pipe *pipe;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900215 int recip = ctrl->bRequestType & USB_RECIP_MASK;
216 int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
217 int ret;
218 int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
219 struct usb_ctrlrequest *ctrl);
220 char *msg;
221
222 uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
Kuninori Morimoto0432eed2011-06-06 14:18:54 +0900223 pipe = usbhsg_uep_to_pipe(uep);
224 if (!pipe) {
Kuninori Morimoto9a28b7b2011-04-21 14:10:12 +0900225 dev_err(dev, "wrong recip request\n");
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900226 ret = -EINVAL;
227 goto usbhsg_recip_run_handle_end;
Kuninori Morimoto9a28b7b2011-04-21 14:10:12 +0900228 }
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900229
230 switch (recip) {
231 case USB_RECIP_DEVICE:
232 msg = "DEVICE";
233 func = handler->device;
234 break;
235 case USB_RECIP_INTERFACE:
236 msg = "INTERFACE";
237 func = handler->interface;
238 break;
239 case USB_RECIP_ENDPOINT:
240 msg = "ENDPOINT";
241 func = handler->endpoint;
242 break;
243 default:
244 dev_warn(dev, "unsupported RECIP(%d)\n", recip);
245 func = NULL;
246 ret = -EINVAL;
247 }
248
249 if (func) {
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900250 unsigned long flags;
251
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900252 dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900253
254 /******************** spin lock ********************/
255 usbhs_lock(priv, flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900256 ret = func(priv, uep, ctrl);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900257 usbhs_unlock(priv, flags);
258 /******************** spin unlock ******************/
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900259 }
260
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900261usbhsg_recip_run_handle_end:
Kuninori Morimoto0432eed2011-06-06 14:18:54 +0900262 usbhs_pkt_start(pipe);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900263
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900264 return ret;
265}
266
267/*
268 * irq functions
269 *
270 * it will be called from usbhs_interrupt
271 */
272static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
273 struct usbhs_irq_state *irq_state)
274{
275 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
276 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
277
278 gpriv->gadget.speed = usbhs_status_get_usb_speed(irq_state);
279
280 dev_dbg(dev, "state = %x : speed : %d\n",
281 usbhs_status_get_device_state(irq_state),
282 gpriv->gadget.speed);
283
284 return 0;
285}
286
287static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
288 struct usbhs_irq_state *irq_state)
289{
290 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
291 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
292 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
293 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
294 struct usb_ctrlrequest ctrl;
295 struct usbhsg_recip_handle *recip_handler = NULL;
296 int stage = usbhs_status_get_ctrl_stage(irq_state);
297 int ret = 0;
298
299 dev_dbg(dev, "stage = %d\n", stage);
300
301 /*
302 * see Manual
303 *
304 * "Operation"
305 * - "Interrupt Function"
306 * - "Control Transfer Stage Transition Interrupt"
307 * - Fig. "Control Transfer Stage Transitions"
308 */
309
310 switch (stage) {
311 case READ_DATA_STAGE:
Kuninori Morimoto0cb7e612011-06-06 14:18:58 +0900312 dcp->handler = &usbhs_fifo_pio_push_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900313 break;
314 case WRITE_DATA_STAGE:
Kuninori Morimoto0cb7e612011-06-06 14:18:58 +0900315 dcp->handler = &usbhs_fifo_pio_pop_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900316 break;
317 case NODATA_STATUS_STAGE:
Kuninori Morimotodad67392011-06-06 14:18:28 +0900318 dcp->handler = &usbhs_ctrl_stage_end_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900319 break;
320 default:
321 return ret;
322 }
323
324 /*
325 * get usb request
326 */
327 usbhs_usbreq_get_val(priv, &ctrl);
328
329 switch (ctrl.bRequestType & USB_TYPE_MASK) {
330 case USB_TYPE_STANDARD:
331 switch (ctrl.bRequest) {
332 case USB_REQ_CLEAR_FEATURE:
333 recip_handler = &req_clear_feature;
334 break;
335 }
336 }
337
338 /*
339 * setup stage / run recip
340 */
341 if (recip_handler)
342 ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
343 else
344 ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
345
346 if (ret < 0)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900347 usbhs_pipe_stall(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900348
349 return ret;
350}
351
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900352/*
353 *
354 * usb_dcp_ops
355 *
356 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900357static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
358{
359 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto8a2c2252011-06-06 14:18:33 +0900360 struct usbhs_pkt *pkt;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900361
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900362 usbhs_pipe_disable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900363
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900364 while (1) {
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900365 pkt = usbhs_pkt_pop(pipe, NULL);
Kuninori Morimoto8a2c2252011-06-06 14:18:33 +0900366 if (!pkt)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900367 break;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900368 }
369
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900370 return 0;
371}
372
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900373static void usbhsg_uep_init(struct usbhsg_gpriv *gpriv)
374{
375 int i;
376 struct usbhsg_uep *uep;
377
378 usbhsg_for_each_uep_with_dcp(uep, gpriv, i)
379 uep->pipe = NULL;
380}
381
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900382/*
383 *
384 * usb_ep_ops
385 *
386 */
387static int usbhsg_ep_enable(struct usb_ep *ep,
388 const struct usb_endpoint_descriptor *desc)
389{
390 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
391 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
392 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
393 struct usbhs_pipe *pipe;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900394 int ret = -EIO;
395
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900396 /*
397 * if it already have pipe,
398 * nothing to do
399 */
400 if (uep->pipe)
401 return 0;
402
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900403 pipe = usbhs_pipe_malloc(priv, desc);
404 if (pipe) {
405 uep->pipe = pipe;
406 pipe->mod_private = uep;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900407
408 if (usb_endpoint_dir_in(desc))
Kuninori Morimoto0cb7e612011-06-06 14:18:58 +0900409 uep->handler = &usbhs_fifo_pio_push_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900410 else
Kuninori Morimoto0cb7e612011-06-06 14:18:58 +0900411 uep->handler = &usbhs_fifo_pio_pop_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900412
413 ret = 0;
414 }
Kuninori Morimotocb966322011-04-21 14:10:08 +0900415
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900416 return ret;
417}
418
419static int usbhsg_ep_disable(struct usb_ep *ep)
420{
421 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900422
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900423 return usbhsg_pipe_disable(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900424}
425
426static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
427 gfp_t gfp_flags)
428{
429 struct usbhsg_request *ureq;
430
431 ureq = kzalloc(sizeof *ureq, gfp_flags);
432 if (!ureq)
433 return NULL;
434
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900435 usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
436
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900437 return &ureq->req;
438}
439
440static void usbhsg_ep_free_request(struct usb_ep *ep,
441 struct usb_request *req)
442{
443 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
444
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900445 WARN_ON(!list_empty(&ureq->pkt.node));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900446 kfree(ureq);
447}
448
449static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
450 gfp_t gfp_flags)
451{
452 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
453 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
454 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
455 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900456
457 /* param check */
458 if (usbhsg_is_not_connected(gpriv) ||
459 unlikely(!gpriv->driver) ||
460 unlikely(!pipe))
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900461 return -ESHUTDOWN;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900462
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900463 usbhsg_queue_push(uep, ureq);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900464
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900465 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900466}
467
468static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
469{
470 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
471 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900472 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900473
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900474 usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900475 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
476
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900477 return 0;
478}
479
480static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
481{
482 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
483 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
484 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900485 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900486 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900487 unsigned long flags;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900488
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900489 usbhsg_pipe_disable(uep);
490
491 dev_dbg(dev, "set halt %d (pipe %d)\n",
492 halt, usbhs_pipe_number(pipe));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900493
494 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900495 usbhs_lock(priv, flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900496
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900497 if (halt)
498 usbhs_pipe_stall(pipe);
499 else
500 usbhs_pipe_disable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900501
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900502 if (halt && wedge)
503 usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
504 else
505 usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900506
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900507 usbhs_unlock(priv, flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900508 /******************** spin unlock ******************/
509
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900510 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900511}
512
513static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
514{
515 return __usbhsg_ep_set_halt_wedge(ep, value, 0);
516}
517
518static int usbhsg_ep_set_wedge(struct usb_ep *ep)
519{
520 return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
521}
522
523static struct usb_ep_ops usbhsg_ep_ops = {
524 .enable = usbhsg_ep_enable,
525 .disable = usbhsg_ep_disable,
526
527 .alloc_request = usbhsg_ep_alloc_request,
528 .free_request = usbhsg_ep_free_request,
529
530 .queue = usbhsg_ep_queue,
531 .dequeue = usbhsg_ep_dequeue,
532
533 .set_halt = usbhsg_ep_set_halt,
534 .set_wedge = usbhsg_ep_set_wedge,
535};
536
537/*
538 * usb module start/end
539 */
540static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
541{
542 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
543 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
544 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
545 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900546 unsigned long flags;
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900547 int ret = 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900548
549 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900550 usbhs_lock(priv, flags);
551
552 usbhsg_status_set(gpriv, status);
553 if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
554 usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
555 ret = -1; /* not ready */
556
557 usbhs_unlock(priv, flags);
558 /******************** spin unlock ********************/
559
560 if (ret < 0)
561 return 0; /* not ready is not error */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900562
563 /*
564 * enable interrupt and systems if ready
565 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900566 dev_dbg(dev, "start gadget\n");
567
568 /*
569 * pipe initialize and enable DCP
570 */
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900571 usbhs_pipe_init(priv,
Kuninori Morimotodad67392011-06-06 14:18:28 +0900572 usbhsg_queue_done);
573 usbhs_fifo_init(priv);
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900574 usbhsg_uep_init(gpriv);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900575
576 /* dcp init */
577 dcp->pipe = usbhs_dcp_malloc(priv);
578 dcp->pipe->mod_private = dcp;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900579
580 /*
581 * system config enble
582 * - HI speed
583 * - function
584 * - usb module
585 */
586 usbhs_sys_hispeed_ctrl(priv, 1);
587 usbhs_sys_function_ctrl(priv, 1);
588 usbhs_sys_usb_ctrl(priv, 1);
589
590 /*
591 * enable irq callback
592 */
593 mod->irq_dev_state = usbhsg_irq_dev_state;
594 mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900595 usbhs_irq_callback_update(priv, mod);
596
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900597 return 0;
598}
599
600static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
601{
602 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
603 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
604 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
605 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900606 unsigned long flags;
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900607 int ret = 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900608
609 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900610 usbhs_lock(priv, flags);
611
612 usbhsg_status_clr(gpriv, status);
613 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
614 !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
615 ret = -1; /* already done */
616
617 usbhs_unlock(priv, flags);
618 /******************** spin unlock ********************/
619
620 if (ret < 0)
621 return 0; /* already done is not error */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900622
623 /*
624 * disable interrupt and systems if 1st try
625 */
Kuninori Morimotodad67392011-06-06 14:18:28 +0900626 usbhs_fifo_quit(priv);
627
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900628 /* disable all irq */
629 mod->irq_dev_state = NULL;
630 mod->irq_ctrl_stage = NULL;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900631 usbhs_irq_callback_update(priv, mod);
632
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900633 gpriv->gadget.speed = USB_SPEED_UNKNOWN;
634
635 /* disable sys */
636 usbhs_sys_hispeed_ctrl(priv, 0);
637 usbhs_sys_function_ctrl(priv, 0);
638 usbhs_sys_usb_ctrl(priv, 0);
639
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900640 usbhsg_pipe_disable(dcp);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900641
642 if (gpriv->driver &&
643 gpriv->driver->disconnect)
644 gpriv->driver->disconnect(&gpriv->gadget);
645
646 dev_dbg(dev, "stop gadget\n");
647
648 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900649}
650
651/*
652 *
653 * linux usb function
654 *
655 */
656struct usbhsg_gpriv *the_controller;
657int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
658 int (*bind)(struct usb_gadget *))
659{
660 struct usbhsg_gpriv *gpriv = the_controller;
661 struct usbhs_priv *priv;
662 struct device *dev;
663 int ret;
664
665 if (!bind ||
666 !driver ||
667 !driver->setup ||
668 driver->speed != USB_SPEED_HIGH)
669 return -EINVAL;
670 if (!gpriv)
671 return -ENODEV;
672 if (gpriv->driver)
673 return -EBUSY;
674
675 dev = usbhsg_gpriv_to_dev(gpriv);
676 priv = usbhsg_gpriv_to_priv(gpriv);
677
678 /* first hook up the driver ... */
679 gpriv->driver = driver;
680 gpriv->gadget.dev.driver = &driver->driver;
681
682 ret = device_add(&gpriv->gadget.dev);
683 if (ret) {
684 dev_err(dev, "device_add error %d\n", ret);
685 goto add_fail;
686 }
687
688 ret = bind(&gpriv->gadget);
689 if (ret) {
690 dev_err(dev, "bind to driver %s error %d\n",
691 driver->driver.name, ret);
692 goto bind_fail;
693 }
694
695 dev_dbg(dev, "bind %s\n", driver->driver.name);
696
697 return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
698
699bind_fail:
700 device_del(&gpriv->gadget.dev);
701add_fail:
702 gpriv->driver = NULL;
703 gpriv->gadget.dev.driver = NULL;
704
705 return ret;
706}
707EXPORT_SYMBOL(usb_gadget_probe_driver);
708
709int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
710{
711 struct usbhsg_gpriv *gpriv = the_controller;
712 struct usbhs_priv *priv;
713 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
714
715 if (!gpriv)
716 return -ENODEV;
717
718 if (!driver ||
719 !driver->unbind ||
720 driver != gpriv->driver)
721 return -EINVAL;
722
723 dev = usbhsg_gpriv_to_dev(gpriv);
724 priv = usbhsg_gpriv_to_priv(gpriv);
725
726 usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
727 device_del(&gpriv->gadget.dev);
728 gpriv->driver = NULL;
729
730 if (driver->disconnect)
731 driver->disconnect(&gpriv->gadget);
732
733 driver->unbind(&gpriv->gadget);
734 dev_dbg(dev, "unbind %s\n", driver->driver.name);
735
736 return 0;
737}
738EXPORT_SYMBOL(usb_gadget_unregister_driver);
739
740/*
741 * usb gadget ops
742 */
743static int usbhsg_get_frame(struct usb_gadget *gadget)
744{
745 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
746 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
747
748 return usbhs_frame_get_num(priv);
749}
750
751static struct usb_gadget_ops usbhsg_gadget_ops = {
752 .get_frame = usbhsg_get_frame,
753};
754
755static int usbhsg_start(struct usbhs_priv *priv)
756{
757 return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
758}
759
760static int usbhsg_stop(struct usbhs_priv *priv)
761{
762 return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
763}
764
765int __devinit usbhs_mod_gadget_probe(struct usbhs_priv *priv)
766{
767 struct usbhsg_gpriv *gpriv;
768 struct usbhsg_uep *uep;
769 struct device *dev = usbhs_priv_to_dev(priv);
770 int pipe_size = usbhs_get_dparam(priv, pipe_size);
771 int i;
772
773 gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
774 if (!gpriv) {
775 dev_err(dev, "Could not allocate gadget priv\n");
776 return -ENOMEM;
777 }
778
779 uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
780 if (!uep) {
781 dev_err(dev, "Could not allocate ep\n");
782 goto usbhs_mod_gadget_probe_err_gpriv;
783 }
784
785 /*
786 * CAUTION
787 *
788 * There is no guarantee that it is possible to access usb module here.
789 * Don't accesses to it.
790 * The accesse will be enable after "usbhsg_start"
791 */
792
793 /*
794 * register itself
795 */
796 usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
797
798 /* init gpriv */
799 gpriv->mod.name = "gadget";
800 gpriv->mod.start = usbhsg_start;
801 gpriv->mod.stop = usbhsg_stop;
802 gpriv->uep = uep;
803 gpriv->uep_size = pipe_size;
804 usbhsg_status_init(gpriv);
805
806 /*
807 * init gadget
808 */
809 device_initialize(&gpriv->gadget.dev);
810 dev_set_name(&gpriv->gadget.dev, "gadget");
811 gpriv->gadget.dev.parent = dev;
812 gpriv->gadget.name = "renesas_usbhs_udc";
813 gpriv->gadget.ops = &usbhsg_gadget_ops;
814 gpriv->gadget.is_dualspeed = 1;
815
816 INIT_LIST_HEAD(&gpriv->gadget.ep_list);
817
818 /*
819 * init usb_ep
820 */
821 usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
822 uep->gpriv = gpriv;
823 snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
824
825 uep->ep.name = uep->ep_name;
826 uep->ep.ops = &usbhsg_ep_ops;
827 INIT_LIST_HEAD(&uep->ep.ep_list);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900828
829 /* init DCP */
830 if (usbhsg_is_dcp(uep)) {
831 gpriv->gadget.ep0 = &uep->ep;
832 uep->ep.maxpacket = 64;
833 }
834 /* init normal pipe */
835 else {
836 uep->ep.maxpacket = 512;
837 list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
838 }
839 }
840
841 the_controller = gpriv;
842
843 dev_info(dev, "gadget probed\n");
844
845 return 0;
846
847usbhs_mod_gadget_probe_err_gpriv:
848 kfree(gpriv);
849
850 return -ENOMEM;
851}
852
853void __devexit usbhs_mod_gadget_remove(struct usbhs_priv *priv)
854{
855 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
856
857 kfree(gpriv);
858}