blob: 31d28dc78aa3e79276e0bec4a47ca313e8537376 [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 Morimotoe73a9892011-06-06 14:19:03 +0900163static int usbhsg_dma_map(struct device *dev,
164 struct usbhs_pkt *pkt,
165 enum dma_data_direction dir)
166{
167 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
168 struct usb_request *req = &ureq->req;
169
170 if (pkt->dma != DMA_ADDR_INVALID) {
171 dev_err(dev, "dma is already mapped\n");
172 return -EIO;
173 }
174
175 if (req->dma == DMA_ADDR_INVALID) {
176 pkt->dma = dma_map_single(dev, pkt->buf, pkt->length, dir);
177 } else {
178 dma_sync_single_for_device(dev, req->dma, req->length, dir);
179 pkt->dma = req->dma;
180 }
181
182 if (dma_mapping_error(dev, pkt->dma)) {
183 dev_err(dev, "dma mapping error %x\n", pkt->dma);
184 return -EIO;
185 }
186
187 return 0;
188}
189
190static int usbhsg_dma_unmap(struct device *dev,
191 struct usbhs_pkt *pkt,
192 enum dma_data_direction dir)
193{
194 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
195 struct usb_request *req = &ureq->req;
196
197 if (pkt->dma == DMA_ADDR_INVALID) {
198 dev_err(dev, "dma is not mapped\n");
199 return -EIO;
200 }
201
202 if (req->dma == DMA_ADDR_INVALID)
203 dma_unmap_single(dev, pkt->dma, pkt->length, dir);
204 else
205 dma_sync_single_for_cpu(dev, req->dma, req->length, dir);
206
207 pkt->dma = DMA_ADDR_INVALID;
208
209 return 0;
210}
211
212static int usbhsg_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
213{
214 struct usbhs_pipe *pipe = pkt->pipe;
215 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
216 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
217 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
218 enum dma_data_direction dir;
219
220 dir = usbhs_pipe_is_dir_in(pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
221
222 if (map)
223 return usbhsg_dma_map(dev, pkt, dir);
224 else
225 return usbhsg_dma_unmap(dev, pkt, dir);
226}
227
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900228/*
229 * USB_TYPE_STANDARD / clear feature functions
230 */
231static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
232 struct usbhsg_uep *uep,
233 struct usb_ctrlrequest *ctrl)
234{
235 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
236 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
237 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
238
239 usbhs_dcp_control_transfer_done(pipe);
240
241 return 0;
242}
243
244static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
245 struct usbhsg_uep *uep,
246 struct usb_ctrlrequest *ctrl)
247{
248 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
249 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
250
251 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900252 usbhs_pipe_disable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900253 usbhs_pipe_clear_sequence(pipe);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900254 usbhs_pipe_enable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900255 }
256
257 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
258
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900259 return 0;
260}
261
262struct usbhsg_recip_handle req_clear_feature = {
263 .name = "clear feature",
264 .device = usbhsg_recip_handler_std_control_done,
265 .interface = usbhsg_recip_handler_std_control_done,
266 .endpoint = usbhsg_recip_handler_std_clear_endpoint,
267};
268
269/*
270 * USB_TYPE handler
271 */
272static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
273 struct usbhsg_recip_handle *handler,
274 struct usb_ctrlrequest *ctrl)
275{
276 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
277 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
278 struct usbhsg_uep *uep;
Kuninori Morimoto0432eed2011-06-06 14:18:54 +0900279 struct usbhs_pipe *pipe;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900280 int recip = ctrl->bRequestType & USB_RECIP_MASK;
281 int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
282 int ret;
283 int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
284 struct usb_ctrlrequest *ctrl);
285 char *msg;
286
287 uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
Kuninori Morimoto0432eed2011-06-06 14:18:54 +0900288 pipe = usbhsg_uep_to_pipe(uep);
289 if (!pipe) {
Kuninori Morimoto9a28b7b2011-04-21 14:10:12 +0900290 dev_err(dev, "wrong recip request\n");
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900291 ret = -EINVAL;
292 goto usbhsg_recip_run_handle_end;
Kuninori Morimoto9a28b7b2011-04-21 14:10:12 +0900293 }
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900294
295 switch (recip) {
296 case USB_RECIP_DEVICE:
297 msg = "DEVICE";
298 func = handler->device;
299 break;
300 case USB_RECIP_INTERFACE:
301 msg = "INTERFACE";
302 func = handler->interface;
303 break;
304 case USB_RECIP_ENDPOINT:
305 msg = "ENDPOINT";
306 func = handler->endpoint;
307 break;
308 default:
309 dev_warn(dev, "unsupported RECIP(%d)\n", recip);
310 func = NULL;
311 ret = -EINVAL;
312 }
313
314 if (func) {
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900315 unsigned long flags;
316
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900317 dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900318
319 /******************** spin lock ********************/
320 usbhs_lock(priv, flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900321 ret = func(priv, uep, ctrl);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900322 usbhs_unlock(priv, flags);
323 /******************** spin unlock ******************/
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900324 }
325
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900326usbhsg_recip_run_handle_end:
Kuninori Morimoto0432eed2011-06-06 14:18:54 +0900327 usbhs_pkt_start(pipe);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900328
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900329 return ret;
330}
331
332/*
333 * irq functions
334 *
335 * it will be called from usbhs_interrupt
336 */
337static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
338 struct usbhs_irq_state *irq_state)
339{
340 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
341 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
342
343 gpriv->gadget.speed = usbhs_status_get_usb_speed(irq_state);
344
345 dev_dbg(dev, "state = %x : speed : %d\n",
346 usbhs_status_get_device_state(irq_state),
347 gpriv->gadget.speed);
348
349 return 0;
350}
351
352static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
353 struct usbhs_irq_state *irq_state)
354{
355 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
356 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
357 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
358 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
359 struct usb_ctrlrequest ctrl;
360 struct usbhsg_recip_handle *recip_handler = NULL;
361 int stage = usbhs_status_get_ctrl_stage(irq_state);
362 int ret = 0;
363
364 dev_dbg(dev, "stage = %d\n", stage);
365
366 /*
367 * see Manual
368 *
369 * "Operation"
370 * - "Interrupt Function"
371 * - "Control Transfer Stage Transition Interrupt"
372 * - Fig. "Control Transfer Stage Transitions"
373 */
374
375 switch (stage) {
376 case READ_DATA_STAGE:
Kuninori Morimoto0cb7e612011-06-06 14:18:58 +0900377 dcp->handler = &usbhs_fifo_pio_push_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900378 break;
379 case WRITE_DATA_STAGE:
Kuninori Morimoto0cb7e612011-06-06 14:18:58 +0900380 dcp->handler = &usbhs_fifo_pio_pop_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900381 break;
382 case NODATA_STATUS_STAGE:
Kuninori Morimotodad67392011-06-06 14:18:28 +0900383 dcp->handler = &usbhs_ctrl_stage_end_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900384 break;
385 default:
386 return ret;
387 }
388
389 /*
390 * get usb request
391 */
392 usbhs_usbreq_get_val(priv, &ctrl);
393
394 switch (ctrl.bRequestType & USB_TYPE_MASK) {
395 case USB_TYPE_STANDARD:
396 switch (ctrl.bRequest) {
397 case USB_REQ_CLEAR_FEATURE:
398 recip_handler = &req_clear_feature;
399 break;
400 }
401 }
402
403 /*
404 * setup stage / run recip
405 */
406 if (recip_handler)
407 ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
408 else
409 ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
410
411 if (ret < 0)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900412 usbhs_pipe_stall(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900413
414 return ret;
415}
416
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900417/*
418 *
419 * usb_dcp_ops
420 *
421 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900422static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
423{
424 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto8a2c2252011-06-06 14:18:33 +0900425 struct usbhs_pkt *pkt;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900426
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900427 usbhs_pipe_disable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900428
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900429 while (1) {
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900430 pkt = usbhs_pkt_pop(pipe, NULL);
Kuninori Morimoto8a2c2252011-06-06 14:18:33 +0900431 if (!pkt)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900432 break;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900433 }
434
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900435 return 0;
436}
437
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900438static void usbhsg_uep_init(struct usbhsg_gpriv *gpriv)
439{
440 int i;
441 struct usbhsg_uep *uep;
442
443 usbhsg_for_each_uep_with_dcp(uep, gpriv, i)
444 uep->pipe = NULL;
445}
446
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900447/*
448 *
449 * usb_ep_ops
450 *
451 */
452static int usbhsg_ep_enable(struct usb_ep *ep,
453 const struct usb_endpoint_descriptor *desc)
454{
455 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
456 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
457 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
458 struct usbhs_pipe *pipe;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900459 int ret = -EIO;
460
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900461 /*
462 * if it already have pipe,
463 * nothing to do
464 */
465 if (uep->pipe)
466 return 0;
467
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900468 pipe = usbhs_pipe_malloc(priv, desc);
469 if (pipe) {
470 uep->pipe = pipe;
471 pipe->mod_private = uep;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900472
473 if (usb_endpoint_dir_in(desc))
Kuninori Morimoto0cb7e612011-06-06 14:18:58 +0900474 uep->handler = &usbhs_fifo_pio_push_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900475 else
Kuninori Morimoto0cb7e612011-06-06 14:18:58 +0900476 uep->handler = &usbhs_fifo_pio_pop_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900477
478 ret = 0;
479 }
Kuninori Morimotocb966322011-04-21 14:10:08 +0900480
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900481 return ret;
482}
483
484static int usbhsg_ep_disable(struct usb_ep *ep)
485{
486 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900487
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900488 return usbhsg_pipe_disable(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900489}
490
491static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
492 gfp_t gfp_flags)
493{
494 struct usbhsg_request *ureq;
495
496 ureq = kzalloc(sizeof *ureq, gfp_flags);
497 if (!ureq)
498 return NULL;
499
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900500 usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
501
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900502 ureq->req.dma = DMA_ADDR_INVALID;
503
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900504 return &ureq->req;
505}
506
507static void usbhsg_ep_free_request(struct usb_ep *ep,
508 struct usb_request *req)
509{
510 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
511
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900512 WARN_ON(!list_empty(&ureq->pkt.node));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900513 kfree(ureq);
514}
515
516static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
517 gfp_t gfp_flags)
518{
519 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
520 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
521 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
522 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900523
524 /* param check */
525 if (usbhsg_is_not_connected(gpriv) ||
526 unlikely(!gpriv->driver) ||
527 unlikely(!pipe))
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900528 return -ESHUTDOWN;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900529
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900530 usbhsg_queue_push(uep, ureq);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900531
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900532 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900533}
534
535static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
536{
537 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
538 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900539 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900540
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900541 usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900542 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
543
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900544 return 0;
545}
546
547static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
548{
549 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
550 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
551 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900552 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900553 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900554 unsigned long flags;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900555
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900556 usbhsg_pipe_disable(uep);
557
558 dev_dbg(dev, "set halt %d (pipe %d)\n",
559 halt, usbhs_pipe_number(pipe));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900560
561 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900562 usbhs_lock(priv, flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900563
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900564 if (halt)
565 usbhs_pipe_stall(pipe);
566 else
567 usbhs_pipe_disable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900568
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900569 if (halt && wedge)
570 usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
571 else
572 usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900573
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900574 usbhs_unlock(priv, flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900575 /******************** spin unlock ******************/
576
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900577 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900578}
579
580static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
581{
582 return __usbhsg_ep_set_halt_wedge(ep, value, 0);
583}
584
585static int usbhsg_ep_set_wedge(struct usb_ep *ep)
586{
587 return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
588}
589
590static struct usb_ep_ops usbhsg_ep_ops = {
591 .enable = usbhsg_ep_enable,
592 .disable = usbhsg_ep_disable,
593
594 .alloc_request = usbhsg_ep_alloc_request,
595 .free_request = usbhsg_ep_free_request,
596
597 .queue = usbhsg_ep_queue,
598 .dequeue = usbhsg_ep_dequeue,
599
600 .set_halt = usbhsg_ep_set_halt,
601 .set_wedge = usbhsg_ep_set_wedge,
602};
603
604/*
605 * usb module start/end
606 */
607static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
608{
609 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
610 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
611 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
612 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900613 unsigned long flags;
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900614 int ret = 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900615
616 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900617 usbhs_lock(priv, flags);
618
619 usbhsg_status_set(gpriv, status);
620 if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
621 usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
622 ret = -1; /* not ready */
623
624 usbhs_unlock(priv, flags);
625 /******************** spin unlock ********************/
626
627 if (ret < 0)
628 return 0; /* not ready is not error */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900629
630 /*
631 * enable interrupt and systems if ready
632 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900633 dev_dbg(dev, "start gadget\n");
634
635 /*
636 * pipe initialize and enable DCP
637 */
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900638 usbhs_pipe_init(priv,
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900639 usbhsg_queue_done,
640 usbhsg_dma_map_ctrl);
Kuninori Morimotodad67392011-06-06 14:18:28 +0900641 usbhs_fifo_init(priv);
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900642 usbhsg_uep_init(gpriv);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900643
644 /* dcp init */
645 dcp->pipe = usbhs_dcp_malloc(priv);
646 dcp->pipe->mod_private = dcp;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900647
648 /*
649 * system config enble
650 * - HI speed
651 * - function
652 * - usb module
653 */
654 usbhs_sys_hispeed_ctrl(priv, 1);
655 usbhs_sys_function_ctrl(priv, 1);
656 usbhs_sys_usb_ctrl(priv, 1);
657
658 /*
659 * enable irq callback
660 */
661 mod->irq_dev_state = usbhsg_irq_dev_state;
662 mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900663 usbhs_irq_callback_update(priv, mod);
664
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900665 return 0;
666}
667
668static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
669{
670 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
671 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
672 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
673 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900674 unsigned long flags;
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900675 int ret = 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900676
677 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900678 usbhs_lock(priv, flags);
679
680 usbhsg_status_clr(gpriv, status);
681 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
682 !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
683 ret = -1; /* already done */
684
685 usbhs_unlock(priv, flags);
686 /******************** spin unlock ********************/
687
688 if (ret < 0)
689 return 0; /* already done is not error */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900690
691 /*
692 * disable interrupt and systems if 1st try
693 */
Kuninori Morimotodad67392011-06-06 14:18:28 +0900694 usbhs_fifo_quit(priv);
695
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900696 /* disable all irq */
697 mod->irq_dev_state = NULL;
698 mod->irq_ctrl_stage = NULL;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900699 usbhs_irq_callback_update(priv, mod);
700
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900701 gpriv->gadget.speed = USB_SPEED_UNKNOWN;
702
703 /* disable sys */
704 usbhs_sys_hispeed_ctrl(priv, 0);
705 usbhs_sys_function_ctrl(priv, 0);
706 usbhs_sys_usb_ctrl(priv, 0);
707
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900708 usbhsg_pipe_disable(dcp);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900709
710 if (gpriv->driver &&
711 gpriv->driver->disconnect)
712 gpriv->driver->disconnect(&gpriv->gadget);
713
714 dev_dbg(dev, "stop gadget\n");
715
716 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900717}
718
719/*
720 *
721 * linux usb function
722 *
723 */
724struct usbhsg_gpriv *the_controller;
725int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
726 int (*bind)(struct usb_gadget *))
727{
728 struct usbhsg_gpriv *gpriv = the_controller;
729 struct usbhs_priv *priv;
730 struct device *dev;
731 int ret;
732
733 if (!bind ||
734 !driver ||
735 !driver->setup ||
736 driver->speed != USB_SPEED_HIGH)
737 return -EINVAL;
738 if (!gpriv)
739 return -ENODEV;
740 if (gpriv->driver)
741 return -EBUSY;
742
743 dev = usbhsg_gpriv_to_dev(gpriv);
744 priv = usbhsg_gpriv_to_priv(gpriv);
745
746 /* first hook up the driver ... */
747 gpriv->driver = driver;
748 gpriv->gadget.dev.driver = &driver->driver;
749
750 ret = device_add(&gpriv->gadget.dev);
751 if (ret) {
752 dev_err(dev, "device_add error %d\n", ret);
753 goto add_fail;
754 }
755
756 ret = bind(&gpriv->gadget);
757 if (ret) {
758 dev_err(dev, "bind to driver %s error %d\n",
759 driver->driver.name, ret);
760 goto bind_fail;
761 }
762
763 dev_dbg(dev, "bind %s\n", driver->driver.name);
764
765 return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
766
767bind_fail:
768 device_del(&gpriv->gadget.dev);
769add_fail:
770 gpriv->driver = NULL;
771 gpriv->gadget.dev.driver = NULL;
772
773 return ret;
774}
775EXPORT_SYMBOL(usb_gadget_probe_driver);
776
777int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
778{
779 struct usbhsg_gpriv *gpriv = the_controller;
780 struct usbhs_priv *priv;
781 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
782
783 if (!gpriv)
784 return -ENODEV;
785
786 if (!driver ||
787 !driver->unbind ||
788 driver != gpriv->driver)
789 return -EINVAL;
790
791 dev = usbhsg_gpriv_to_dev(gpriv);
792 priv = usbhsg_gpriv_to_priv(gpriv);
793
794 usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
795 device_del(&gpriv->gadget.dev);
796 gpriv->driver = NULL;
797
798 if (driver->disconnect)
799 driver->disconnect(&gpriv->gadget);
800
801 driver->unbind(&gpriv->gadget);
802 dev_dbg(dev, "unbind %s\n", driver->driver.name);
803
804 return 0;
805}
806EXPORT_SYMBOL(usb_gadget_unregister_driver);
807
808/*
809 * usb gadget ops
810 */
811static int usbhsg_get_frame(struct usb_gadget *gadget)
812{
813 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
814 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
815
816 return usbhs_frame_get_num(priv);
817}
818
819static struct usb_gadget_ops usbhsg_gadget_ops = {
820 .get_frame = usbhsg_get_frame,
821};
822
823static int usbhsg_start(struct usbhs_priv *priv)
824{
825 return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
826}
827
828static int usbhsg_stop(struct usbhs_priv *priv)
829{
830 return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
831}
832
833int __devinit usbhs_mod_gadget_probe(struct usbhs_priv *priv)
834{
835 struct usbhsg_gpriv *gpriv;
836 struct usbhsg_uep *uep;
837 struct device *dev = usbhs_priv_to_dev(priv);
838 int pipe_size = usbhs_get_dparam(priv, pipe_size);
839 int i;
840
841 gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
842 if (!gpriv) {
843 dev_err(dev, "Could not allocate gadget priv\n");
844 return -ENOMEM;
845 }
846
847 uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
848 if (!uep) {
849 dev_err(dev, "Could not allocate ep\n");
850 goto usbhs_mod_gadget_probe_err_gpriv;
851 }
852
853 /*
854 * CAUTION
855 *
856 * There is no guarantee that it is possible to access usb module here.
857 * Don't accesses to it.
858 * The accesse will be enable after "usbhsg_start"
859 */
860
861 /*
862 * register itself
863 */
864 usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
865
866 /* init gpriv */
867 gpriv->mod.name = "gadget";
868 gpriv->mod.start = usbhsg_start;
869 gpriv->mod.stop = usbhsg_stop;
870 gpriv->uep = uep;
871 gpriv->uep_size = pipe_size;
872 usbhsg_status_init(gpriv);
873
874 /*
875 * init gadget
876 */
877 device_initialize(&gpriv->gadget.dev);
878 dev_set_name(&gpriv->gadget.dev, "gadget");
879 gpriv->gadget.dev.parent = dev;
880 gpriv->gadget.name = "renesas_usbhs_udc";
881 gpriv->gadget.ops = &usbhsg_gadget_ops;
882 gpriv->gadget.is_dualspeed = 1;
883
884 INIT_LIST_HEAD(&gpriv->gadget.ep_list);
885
886 /*
887 * init usb_ep
888 */
889 usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
890 uep->gpriv = gpriv;
891 snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
892
893 uep->ep.name = uep->ep_name;
894 uep->ep.ops = &usbhsg_ep_ops;
895 INIT_LIST_HEAD(&uep->ep.ep_list);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900896
897 /* init DCP */
898 if (usbhsg_is_dcp(uep)) {
899 gpriv->gadget.ep0 = &uep->ep;
900 uep->ep.maxpacket = 64;
901 }
902 /* init normal pipe */
903 else {
904 uep->ep.maxpacket = 512;
905 list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
906 }
907 }
908
909 the_controller = gpriv;
910
911 dev_info(dev, "gadget probed\n");
912
913 return 0;
914
915usbhs_mod_gadget_probe_err_gpriv:
916 kfree(gpriv);
917
918 return -ENOMEM;
919}
920
921void __devexit usbhs_mod_gadget_remove(struct usbhs_priv *priv)
922{
923 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
924
925 kfree(gpriv);
926}