blob: aa591b663835cec66469f86d10e244d32e605c7b [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 */
Kuninori Morimoto08e6c612011-06-09 16:48:25 +0900465 if (uep->pipe) {
466 usbhs_pipe_clear(uep->pipe);
467 usbhs_pipe_clear_sequence(uep->pipe);
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900468 return 0;
Kuninori Morimoto08e6c612011-06-09 16:48:25 +0900469 }
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900470
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900471 pipe = usbhs_pipe_malloc(priv, desc);
472 if (pipe) {
473 uep->pipe = pipe;
474 pipe->mod_private = uep;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900475
476 if (usb_endpoint_dir_in(desc))
Kuninori Morimoto0cb7e612011-06-06 14:18:58 +0900477 uep->handler = &usbhs_fifo_pio_push_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900478 else
Kuninori Morimoto0cb7e612011-06-06 14:18:58 +0900479 uep->handler = &usbhs_fifo_pio_pop_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900480
481 ret = 0;
482 }
Kuninori Morimotocb966322011-04-21 14:10:08 +0900483
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900484 return ret;
485}
486
487static int usbhsg_ep_disable(struct usb_ep *ep)
488{
489 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900490
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900491 return usbhsg_pipe_disable(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900492}
493
494static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
495 gfp_t gfp_flags)
496{
497 struct usbhsg_request *ureq;
498
499 ureq = kzalloc(sizeof *ureq, gfp_flags);
500 if (!ureq)
501 return NULL;
502
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900503 usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
504
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900505 ureq->req.dma = DMA_ADDR_INVALID;
506
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900507 return &ureq->req;
508}
509
510static void usbhsg_ep_free_request(struct usb_ep *ep,
511 struct usb_request *req)
512{
513 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
514
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900515 WARN_ON(!list_empty(&ureq->pkt.node));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900516 kfree(ureq);
517}
518
519static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
520 gfp_t gfp_flags)
521{
522 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
523 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
524 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
525 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900526
527 /* param check */
528 if (usbhsg_is_not_connected(gpriv) ||
529 unlikely(!gpriv->driver) ||
530 unlikely(!pipe))
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900531 return -ESHUTDOWN;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900532
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900533 usbhsg_queue_push(uep, ureq);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900534
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900535 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900536}
537
538static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
539{
540 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
541 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900542 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900543
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900544 usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900545 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
546
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900547 return 0;
548}
549
550static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
551{
552 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
553 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
554 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900555 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900556 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900557 unsigned long flags;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900558
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900559 usbhsg_pipe_disable(uep);
560
561 dev_dbg(dev, "set halt %d (pipe %d)\n",
562 halt, usbhs_pipe_number(pipe));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900563
564 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900565 usbhs_lock(priv, flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900566
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900567 if (halt)
568 usbhs_pipe_stall(pipe);
569 else
570 usbhs_pipe_disable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900571
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900572 if (halt && wedge)
573 usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
574 else
575 usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900576
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900577 usbhs_unlock(priv, flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900578 /******************** spin unlock ******************/
579
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900580 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900581}
582
583static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
584{
585 return __usbhsg_ep_set_halt_wedge(ep, value, 0);
586}
587
588static int usbhsg_ep_set_wedge(struct usb_ep *ep)
589{
590 return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
591}
592
593static struct usb_ep_ops usbhsg_ep_ops = {
594 .enable = usbhsg_ep_enable,
595 .disable = usbhsg_ep_disable,
596
597 .alloc_request = usbhsg_ep_alloc_request,
598 .free_request = usbhsg_ep_free_request,
599
600 .queue = usbhsg_ep_queue,
601 .dequeue = usbhsg_ep_dequeue,
602
603 .set_halt = usbhsg_ep_set_halt,
604 .set_wedge = usbhsg_ep_set_wedge,
605};
606
607/*
608 * usb module start/end
609 */
610static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
611{
612 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
613 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
614 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
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_set(gpriv, status);
623 if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
624 usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
625 ret = -1; /* not ready */
626
627 usbhs_unlock(priv, flags);
628 /******************** spin unlock ********************/
629
630 if (ret < 0)
631 return 0; /* not ready is not error */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900632
633 /*
634 * enable interrupt and systems if ready
635 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900636 dev_dbg(dev, "start gadget\n");
637
638 /*
639 * pipe initialize and enable DCP
640 */
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900641 usbhs_pipe_init(priv,
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900642 usbhsg_queue_done,
643 usbhsg_dma_map_ctrl);
Kuninori Morimotodad67392011-06-06 14:18:28 +0900644 usbhs_fifo_init(priv);
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900645 usbhsg_uep_init(gpriv);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900646
647 /* dcp init */
648 dcp->pipe = usbhs_dcp_malloc(priv);
649 dcp->pipe->mod_private = dcp;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900650
651 /*
652 * system config enble
653 * - HI speed
654 * - function
655 * - usb module
656 */
657 usbhs_sys_hispeed_ctrl(priv, 1);
658 usbhs_sys_function_ctrl(priv, 1);
659 usbhs_sys_usb_ctrl(priv, 1);
660
661 /*
662 * enable irq callback
663 */
664 mod->irq_dev_state = usbhsg_irq_dev_state;
665 mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900666 usbhs_irq_callback_update(priv, mod);
667
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900668 return 0;
669}
670
671static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
672{
673 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
674 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
675 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
676 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900677 unsigned long flags;
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900678 int ret = 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900679
680 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900681 usbhs_lock(priv, flags);
682
683 usbhsg_status_clr(gpriv, status);
684 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
685 !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
686 ret = -1; /* already done */
687
688 usbhs_unlock(priv, flags);
689 /******************** spin unlock ********************/
690
691 if (ret < 0)
692 return 0; /* already done is not error */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900693
694 /*
695 * disable interrupt and systems if 1st try
696 */
Kuninori Morimotodad67392011-06-06 14:18:28 +0900697 usbhs_fifo_quit(priv);
698
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900699 /* disable all irq */
700 mod->irq_dev_state = NULL;
701 mod->irq_ctrl_stage = NULL;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900702 usbhs_irq_callback_update(priv, mod);
703
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900704 gpriv->gadget.speed = USB_SPEED_UNKNOWN;
705
706 /* disable sys */
707 usbhs_sys_hispeed_ctrl(priv, 0);
708 usbhs_sys_function_ctrl(priv, 0);
709 usbhs_sys_usb_ctrl(priv, 0);
710
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900711 usbhsg_pipe_disable(dcp);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900712
713 if (gpriv->driver &&
714 gpriv->driver->disconnect)
715 gpriv->driver->disconnect(&gpriv->gadget);
716
717 dev_dbg(dev, "stop gadget\n");
718
719 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900720}
721
722/*
723 *
724 * linux usb function
725 *
726 */
727struct usbhsg_gpriv *the_controller;
728int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
729 int (*bind)(struct usb_gadget *))
730{
731 struct usbhsg_gpriv *gpriv = the_controller;
732 struct usbhs_priv *priv;
733 struct device *dev;
734 int ret;
735
736 if (!bind ||
737 !driver ||
738 !driver->setup ||
739 driver->speed != USB_SPEED_HIGH)
740 return -EINVAL;
741 if (!gpriv)
742 return -ENODEV;
743 if (gpriv->driver)
744 return -EBUSY;
745
746 dev = usbhsg_gpriv_to_dev(gpriv);
747 priv = usbhsg_gpriv_to_priv(gpriv);
748
749 /* first hook up the driver ... */
750 gpriv->driver = driver;
751 gpriv->gadget.dev.driver = &driver->driver;
752
753 ret = device_add(&gpriv->gadget.dev);
754 if (ret) {
755 dev_err(dev, "device_add error %d\n", ret);
756 goto add_fail;
757 }
758
759 ret = bind(&gpriv->gadget);
760 if (ret) {
761 dev_err(dev, "bind to driver %s error %d\n",
762 driver->driver.name, ret);
763 goto bind_fail;
764 }
765
766 dev_dbg(dev, "bind %s\n", driver->driver.name);
767
768 return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
769
770bind_fail:
771 device_del(&gpriv->gadget.dev);
772add_fail:
773 gpriv->driver = NULL;
774 gpriv->gadget.dev.driver = NULL;
775
776 return ret;
777}
778EXPORT_SYMBOL(usb_gadget_probe_driver);
779
780int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
781{
782 struct usbhsg_gpriv *gpriv = the_controller;
783 struct usbhs_priv *priv;
784 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
785
786 if (!gpriv)
787 return -ENODEV;
788
789 if (!driver ||
790 !driver->unbind ||
791 driver != gpriv->driver)
792 return -EINVAL;
793
794 dev = usbhsg_gpriv_to_dev(gpriv);
795 priv = usbhsg_gpriv_to_priv(gpriv);
796
797 usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
798 device_del(&gpriv->gadget.dev);
799 gpriv->driver = NULL;
800
801 if (driver->disconnect)
802 driver->disconnect(&gpriv->gadget);
803
804 driver->unbind(&gpriv->gadget);
805 dev_dbg(dev, "unbind %s\n", driver->driver.name);
806
807 return 0;
808}
809EXPORT_SYMBOL(usb_gadget_unregister_driver);
810
811/*
812 * usb gadget ops
813 */
814static int usbhsg_get_frame(struct usb_gadget *gadget)
815{
816 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
817 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
818
819 return usbhs_frame_get_num(priv);
820}
821
822static struct usb_gadget_ops usbhsg_gadget_ops = {
823 .get_frame = usbhsg_get_frame,
824};
825
826static int usbhsg_start(struct usbhs_priv *priv)
827{
828 return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
829}
830
831static int usbhsg_stop(struct usbhs_priv *priv)
832{
833 return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
834}
835
836int __devinit usbhs_mod_gadget_probe(struct usbhs_priv *priv)
837{
838 struct usbhsg_gpriv *gpriv;
839 struct usbhsg_uep *uep;
840 struct device *dev = usbhs_priv_to_dev(priv);
841 int pipe_size = usbhs_get_dparam(priv, pipe_size);
842 int i;
843
844 gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
845 if (!gpriv) {
846 dev_err(dev, "Could not allocate gadget priv\n");
847 return -ENOMEM;
848 }
849
850 uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
851 if (!uep) {
852 dev_err(dev, "Could not allocate ep\n");
853 goto usbhs_mod_gadget_probe_err_gpriv;
854 }
855
856 /*
857 * CAUTION
858 *
859 * There is no guarantee that it is possible to access usb module here.
860 * Don't accesses to it.
861 * The accesse will be enable after "usbhsg_start"
862 */
863
864 /*
865 * register itself
866 */
867 usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
868
869 /* init gpriv */
870 gpriv->mod.name = "gadget";
871 gpriv->mod.start = usbhsg_start;
872 gpriv->mod.stop = usbhsg_stop;
873 gpriv->uep = uep;
874 gpriv->uep_size = pipe_size;
875 usbhsg_status_init(gpriv);
876
877 /*
878 * init gadget
879 */
880 device_initialize(&gpriv->gadget.dev);
881 dev_set_name(&gpriv->gadget.dev, "gadget");
882 gpriv->gadget.dev.parent = dev;
883 gpriv->gadget.name = "renesas_usbhs_udc";
884 gpriv->gadget.ops = &usbhsg_gadget_ops;
885 gpriv->gadget.is_dualspeed = 1;
886
887 INIT_LIST_HEAD(&gpriv->gadget.ep_list);
888
889 /*
890 * init usb_ep
891 */
892 usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
893 uep->gpriv = gpriv;
894 snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
895
896 uep->ep.name = uep->ep_name;
897 uep->ep.ops = &usbhsg_ep_ops;
898 INIT_LIST_HEAD(&uep->ep.ep_list);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900899
900 /* init DCP */
901 if (usbhsg_is_dcp(uep)) {
902 gpriv->gadget.ep0 = &uep->ep;
903 uep->ep.maxpacket = 64;
904 }
905 /* init normal pipe */
906 else {
907 uep->ep.maxpacket = 512;
908 list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
909 }
910 }
911
912 the_controller = gpriv;
913
914 dev_info(dev, "gadget probed\n");
915
916 return 0;
917
918usbhs_mod_gadget_probe_err_gpriv:
919 kfree(gpriv);
920
921 return -ENOMEM;
922}
923
924void __devexit usbhs_mod_gadget_remove(struct usbhs_priv *priv)
925{
926 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
927
Sebastian Andrzej Siewior3af51ac2011-06-03 19:50:48 +0200928 kfree(gpriv->uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900929 kfree(gpriv);
930}