blob: cc3ad634d4f7052ec51a751fa86ee1bb0de1f8ac [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/*
Kuninori Morimoto233f5192011-07-07 00:23:24 -0700117 * queue push/pop
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900118 */
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 Morimoto233f5192011-07-07 00:23:24 -0700163/*
164 * dma map/unmap
165 */
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900166static int usbhsg_dma_map(struct device *dev,
167 struct usbhs_pkt *pkt,
168 enum dma_data_direction dir)
169{
170 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
171 struct usb_request *req = &ureq->req;
172
173 if (pkt->dma != DMA_ADDR_INVALID) {
174 dev_err(dev, "dma is already mapped\n");
175 return -EIO;
176 }
177
178 if (req->dma == DMA_ADDR_INVALID) {
179 pkt->dma = dma_map_single(dev, pkt->buf, pkt->length, dir);
180 } else {
181 dma_sync_single_for_device(dev, req->dma, req->length, dir);
182 pkt->dma = req->dma;
183 }
184
185 if (dma_mapping_error(dev, pkt->dma)) {
186 dev_err(dev, "dma mapping error %x\n", pkt->dma);
187 return -EIO;
188 }
189
190 return 0;
191}
192
193static int usbhsg_dma_unmap(struct device *dev,
194 struct usbhs_pkt *pkt,
195 enum dma_data_direction dir)
196{
197 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
198 struct usb_request *req = &ureq->req;
199
200 if (pkt->dma == DMA_ADDR_INVALID) {
201 dev_err(dev, "dma is not mapped\n");
202 return -EIO;
203 }
204
205 if (req->dma == DMA_ADDR_INVALID)
206 dma_unmap_single(dev, pkt->dma, pkt->length, dir);
207 else
208 dma_sync_single_for_cpu(dev, req->dma, req->length, dir);
209
210 pkt->dma = DMA_ADDR_INVALID;
211
212 return 0;
213}
214
215static int usbhsg_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
216{
217 struct usbhs_pipe *pipe = pkt->pipe;
218 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
219 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
220 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
221 enum dma_data_direction dir;
222
223 dir = usbhs_pipe_is_dir_in(pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
224
225 if (map)
226 return usbhsg_dma_map(dev, pkt, dir);
227 else
228 return usbhsg_dma_unmap(dev, pkt, dir);
229}
230
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900231/*
232 * USB_TYPE_STANDARD / clear feature functions
233 */
234static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
235 struct usbhsg_uep *uep,
236 struct usb_ctrlrequest *ctrl)
237{
238 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
239 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
240 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
241
242 usbhs_dcp_control_transfer_done(pipe);
243
244 return 0;
245}
246
247static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
248 struct usbhsg_uep *uep,
249 struct usb_ctrlrequest *ctrl)
250{
251 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
252 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
253
254 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900255 usbhs_pipe_disable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900256 usbhs_pipe_clear_sequence(pipe);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900257 usbhs_pipe_enable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900258 }
259
260 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
261
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900262 return 0;
263}
264
265struct usbhsg_recip_handle req_clear_feature = {
266 .name = "clear feature",
267 .device = usbhsg_recip_handler_std_control_done,
268 .interface = usbhsg_recip_handler_std_control_done,
269 .endpoint = usbhsg_recip_handler_std_clear_endpoint,
270};
271
272/*
273 * USB_TYPE handler
274 */
275static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
276 struct usbhsg_recip_handle *handler,
277 struct usb_ctrlrequest *ctrl)
278{
279 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
280 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
281 struct usbhsg_uep *uep;
Kuninori Morimoto0432eed2011-06-06 14:18:54 +0900282 struct usbhs_pipe *pipe;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900283 int recip = ctrl->bRequestType & USB_RECIP_MASK;
284 int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
285 int ret;
286 int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
287 struct usb_ctrlrequest *ctrl);
288 char *msg;
289
290 uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
Kuninori Morimoto0432eed2011-06-06 14:18:54 +0900291 pipe = usbhsg_uep_to_pipe(uep);
292 if (!pipe) {
Kuninori Morimoto9a28b7b2011-04-21 14:10:12 +0900293 dev_err(dev, "wrong recip request\n");
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900294 ret = -EINVAL;
295 goto usbhsg_recip_run_handle_end;
Kuninori Morimoto9a28b7b2011-04-21 14:10:12 +0900296 }
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900297
298 switch (recip) {
299 case USB_RECIP_DEVICE:
300 msg = "DEVICE";
301 func = handler->device;
302 break;
303 case USB_RECIP_INTERFACE:
304 msg = "INTERFACE";
305 func = handler->interface;
306 break;
307 case USB_RECIP_ENDPOINT:
308 msg = "ENDPOINT";
309 func = handler->endpoint;
310 break;
311 default:
312 dev_warn(dev, "unsupported RECIP(%d)\n", recip);
313 func = NULL;
314 ret = -EINVAL;
315 }
316
317 if (func) {
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900318 unsigned long flags;
319
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900320 dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900321
322 /******************** spin lock ********************/
323 usbhs_lock(priv, flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900324 ret = func(priv, uep, ctrl);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900325 usbhs_unlock(priv, flags);
326 /******************** spin unlock ******************/
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900327 }
328
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900329usbhsg_recip_run_handle_end:
Kuninori Morimoto0432eed2011-06-06 14:18:54 +0900330 usbhs_pkt_start(pipe);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900331
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900332 return ret;
333}
334
335/*
336 * irq functions
337 *
338 * it will be called from usbhs_interrupt
339 */
340static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
341 struct usbhs_irq_state *irq_state)
342{
343 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
344 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
345
346 gpriv->gadget.speed = usbhs_status_get_usb_speed(irq_state);
347
348 dev_dbg(dev, "state = %x : speed : %d\n",
349 usbhs_status_get_device_state(irq_state),
350 gpriv->gadget.speed);
351
352 return 0;
353}
354
355static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
356 struct usbhs_irq_state *irq_state)
357{
358 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
359 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
360 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
361 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
362 struct usb_ctrlrequest ctrl;
363 struct usbhsg_recip_handle *recip_handler = NULL;
364 int stage = usbhs_status_get_ctrl_stage(irq_state);
365 int ret = 0;
366
367 dev_dbg(dev, "stage = %d\n", stage);
368
369 /*
370 * see Manual
371 *
372 * "Operation"
373 * - "Interrupt Function"
374 * - "Control Transfer Stage Transition Interrupt"
375 * - Fig. "Control Transfer Stage Transitions"
376 */
377
378 switch (stage) {
379 case READ_DATA_STAGE:
Kuninori Morimoto0cb7e612011-06-06 14:18:58 +0900380 dcp->handler = &usbhs_fifo_pio_push_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900381 break;
382 case WRITE_DATA_STAGE:
Kuninori Morimoto0cb7e612011-06-06 14:18:58 +0900383 dcp->handler = &usbhs_fifo_pio_pop_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900384 break;
385 case NODATA_STATUS_STAGE:
Kuninori Morimotodad67392011-06-06 14:18:28 +0900386 dcp->handler = &usbhs_ctrl_stage_end_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900387 break;
388 default:
389 return ret;
390 }
391
392 /*
393 * get usb request
394 */
395 usbhs_usbreq_get_val(priv, &ctrl);
396
397 switch (ctrl.bRequestType & USB_TYPE_MASK) {
398 case USB_TYPE_STANDARD:
399 switch (ctrl.bRequest) {
400 case USB_REQ_CLEAR_FEATURE:
401 recip_handler = &req_clear_feature;
402 break;
403 }
404 }
405
406 /*
407 * setup stage / run recip
408 */
409 if (recip_handler)
410 ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
411 else
412 ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
413
414 if (ret < 0)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900415 usbhs_pipe_stall(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900416
417 return ret;
418}
419
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900420/*
421 *
422 * usb_dcp_ops
423 *
424 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900425static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
426{
427 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto8a2c2252011-06-06 14:18:33 +0900428 struct usbhs_pkt *pkt;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900429
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900430 usbhs_pipe_disable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900431
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900432 while (1) {
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900433 pkt = usbhs_pkt_pop(pipe, NULL);
Kuninori Morimoto8a2c2252011-06-06 14:18:33 +0900434 if (!pkt)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900435 break;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900436 }
437
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900438 return 0;
439}
440
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900441static void usbhsg_uep_init(struct usbhsg_gpriv *gpriv)
442{
443 int i;
444 struct usbhsg_uep *uep;
445
446 usbhsg_for_each_uep_with_dcp(uep, gpriv, i)
447 uep->pipe = NULL;
448}
449
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900450/*
451 *
452 * usb_ep_ops
453 *
454 */
455static int usbhsg_ep_enable(struct usb_ep *ep,
456 const struct usb_endpoint_descriptor *desc)
457{
458 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
459 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
460 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
461 struct usbhs_pipe *pipe;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900462 int ret = -EIO;
463
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900464 /*
465 * if it already have pipe,
466 * nothing to do
467 */
Kuninori Morimoto08e6c612011-06-09 16:48:25 +0900468 if (uep->pipe) {
469 usbhs_pipe_clear(uep->pipe);
470 usbhs_pipe_clear_sequence(uep->pipe);
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900471 return 0;
Kuninori Morimoto08e6c612011-06-09 16:48:25 +0900472 }
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900473
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900474 pipe = usbhs_pipe_malloc(priv, desc);
475 if (pipe) {
476 uep->pipe = pipe;
477 pipe->mod_private = uep;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900478
Kuninori Morimoto233f5192011-07-07 00:23:24 -0700479 /*
480 * usbhs_fifo_dma_push/pop_handler try to
481 * use dmaengine if possible.
482 * It will use pio handler if impossible.
483 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900484 if (usb_endpoint_dir_in(desc))
Kuninori Morimoto6d721b22011-06-21 15:09:19 +0900485 uep->handler = &usbhs_fifo_dma_push_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900486 else
Kuninori Morimoto6d721b22011-06-21 15:09:19 +0900487 uep->handler = &usbhs_fifo_dma_pop_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900488
489 ret = 0;
490 }
Kuninori Morimotocb966322011-04-21 14:10:08 +0900491
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900492 return ret;
493}
494
495static int usbhsg_ep_disable(struct usb_ep *ep)
496{
497 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900498
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900499 return usbhsg_pipe_disable(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900500}
501
502static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
503 gfp_t gfp_flags)
504{
505 struct usbhsg_request *ureq;
506
507 ureq = kzalloc(sizeof *ureq, gfp_flags);
508 if (!ureq)
509 return NULL;
510
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900511 usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
512
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900513 ureq->req.dma = DMA_ADDR_INVALID;
514
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900515 return &ureq->req;
516}
517
518static void usbhsg_ep_free_request(struct usb_ep *ep,
519 struct usb_request *req)
520{
521 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
522
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900523 WARN_ON(!list_empty(&ureq->pkt.node));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900524 kfree(ureq);
525}
526
527static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
528 gfp_t gfp_flags)
529{
530 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
531 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
532 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
533 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900534
535 /* param check */
536 if (usbhsg_is_not_connected(gpriv) ||
537 unlikely(!gpriv->driver) ||
538 unlikely(!pipe))
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900539 return -ESHUTDOWN;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900540
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900541 usbhsg_queue_push(uep, ureq);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900542
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900543 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900544}
545
546static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
547{
548 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
549 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900550 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900551
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900552 usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900553 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
554
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900555 return 0;
556}
557
558static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
559{
560 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
561 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
562 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900563 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900564 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900565 unsigned long flags;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900566
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900567 usbhsg_pipe_disable(uep);
568
569 dev_dbg(dev, "set halt %d (pipe %d)\n",
570 halt, usbhs_pipe_number(pipe));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900571
572 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900573 usbhs_lock(priv, flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900574
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900575 if (halt)
576 usbhs_pipe_stall(pipe);
577 else
578 usbhs_pipe_disable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900579
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900580 if (halt && wedge)
581 usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
582 else
583 usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900584
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900585 usbhs_unlock(priv, flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900586 /******************** spin unlock ******************/
587
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900588 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900589}
590
591static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
592{
593 return __usbhsg_ep_set_halt_wedge(ep, value, 0);
594}
595
596static int usbhsg_ep_set_wedge(struct usb_ep *ep)
597{
598 return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
599}
600
601static struct usb_ep_ops usbhsg_ep_ops = {
602 .enable = usbhsg_ep_enable,
603 .disable = usbhsg_ep_disable,
604
605 .alloc_request = usbhsg_ep_alloc_request,
606 .free_request = usbhsg_ep_free_request,
607
608 .queue = usbhsg_ep_queue,
609 .dequeue = usbhsg_ep_dequeue,
610
611 .set_halt = usbhsg_ep_set_halt,
612 .set_wedge = usbhsg_ep_set_wedge,
613};
614
615/*
616 * usb module start/end
617 */
618static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
619{
620 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
621 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
622 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
623 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900624 unsigned long flags;
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900625 int ret = 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900626
627 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900628 usbhs_lock(priv, flags);
629
630 usbhsg_status_set(gpriv, status);
631 if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
632 usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
633 ret = -1; /* not ready */
634
635 usbhs_unlock(priv, flags);
636 /******************** spin unlock ********************/
637
638 if (ret < 0)
639 return 0; /* not ready is not error */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900640
641 /*
642 * enable interrupt and systems if ready
643 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900644 dev_dbg(dev, "start gadget\n");
645
646 /*
647 * pipe initialize and enable DCP
648 */
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900649 usbhs_pipe_init(priv,
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900650 usbhsg_queue_done,
651 usbhsg_dma_map_ctrl);
Kuninori Morimotodad67392011-06-06 14:18:28 +0900652 usbhs_fifo_init(priv);
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900653 usbhsg_uep_init(gpriv);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900654
655 /* dcp init */
656 dcp->pipe = usbhs_dcp_malloc(priv);
657 dcp->pipe->mod_private = dcp;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900658
659 /*
660 * system config enble
661 * - HI speed
662 * - function
663 * - usb module
664 */
665 usbhs_sys_hispeed_ctrl(priv, 1);
666 usbhs_sys_function_ctrl(priv, 1);
667 usbhs_sys_usb_ctrl(priv, 1);
668
669 /*
670 * enable irq callback
671 */
672 mod->irq_dev_state = usbhsg_irq_dev_state;
673 mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900674 usbhs_irq_callback_update(priv, mod);
675
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900676 return 0;
677}
678
679static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
680{
681 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
682 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
683 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
684 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900685 unsigned long flags;
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900686 int ret = 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900687
688 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900689 usbhs_lock(priv, flags);
690
691 usbhsg_status_clr(gpriv, status);
692 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
693 !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
694 ret = -1; /* already done */
695
696 usbhs_unlock(priv, flags);
697 /******************** spin unlock ********************/
698
699 if (ret < 0)
700 return 0; /* already done is not error */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900701
702 /*
703 * disable interrupt and systems if 1st try
704 */
Kuninori Morimotodad67392011-06-06 14:18:28 +0900705 usbhs_fifo_quit(priv);
706
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900707 /* disable all irq */
708 mod->irq_dev_state = NULL;
709 mod->irq_ctrl_stage = NULL;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900710 usbhs_irq_callback_update(priv, mod);
711
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900712 gpriv->gadget.speed = USB_SPEED_UNKNOWN;
713
714 /* disable sys */
715 usbhs_sys_hispeed_ctrl(priv, 0);
716 usbhs_sys_function_ctrl(priv, 0);
717 usbhs_sys_usb_ctrl(priv, 0);
718
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900719 usbhsg_pipe_disable(dcp);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900720
721 if (gpriv->driver &&
722 gpriv->driver->disconnect)
723 gpriv->driver->disconnect(&gpriv->gadget);
724
725 dev_dbg(dev, "stop gadget\n");
726
727 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900728}
729
730/*
731 *
732 * linux usb function
733 *
734 */
735struct usbhsg_gpriv *the_controller;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300736static int usbhsg_gadget_start(struct usb_gadget_driver *driver,
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900737 int (*bind)(struct usb_gadget *))
738{
739 struct usbhsg_gpriv *gpriv = the_controller;
740 struct usbhs_priv *priv;
741 struct device *dev;
742 int ret;
743
744 if (!bind ||
745 !driver ||
746 !driver->setup ||
747 driver->speed != USB_SPEED_HIGH)
748 return -EINVAL;
749 if (!gpriv)
750 return -ENODEV;
751 if (gpriv->driver)
752 return -EBUSY;
753
754 dev = usbhsg_gpriv_to_dev(gpriv);
755 priv = usbhsg_gpriv_to_priv(gpriv);
756
757 /* first hook up the driver ... */
758 gpriv->driver = driver;
759 gpriv->gadget.dev.driver = &driver->driver;
760
761 ret = device_add(&gpriv->gadget.dev);
762 if (ret) {
763 dev_err(dev, "device_add error %d\n", ret);
764 goto add_fail;
765 }
766
767 ret = bind(&gpriv->gadget);
768 if (ret) {
769 dev_err(dev, "bind to driver %s error %d\n",
770 driver->driver.name, ret);
771 goto bind_fail;
772 }
773
774 dev_dbg(dev, "bind %s\n", driver->driver.name);
775
776 return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
777
778bind_fail:
779 device_del(&gpriv->gadget.dev);
780add_fail:
781 gpriv->driver = NULL;
782 gpriv->gadget.dev.driver = NULL;
783
784 return ret;
785}
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900786
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300787static int usbhsg_gadget_stop(struct usb_gadget_driver *driver)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900788{
789 struct usbhsg_gpriv *gpriv = the_controller;
790 struct usbhs_priv *priv;
791 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
792
793 if (!gpriv)
794 return -ENODEV;
795
796 if (!driver ||
797 !driver->unbind ||
798 driver != gpriv->driver)
799 return -EINVAL;
800
801 dev = usbhsg_gpriv_to_dev(gpriv);
802 priv = usbhsg_gpriv_to_priv(gpriv);
803
804 usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
805 device_del(&gpriv->gadget.dev);
806 gpriv->driver = NULL;
807
808 if (driver->disconnect)
809 driver->disconnect(&gpriv->gadget);
810
811 driver->unbind(&gpriv->gadget);
812 dev_dbg(dev, "unbind %s\n", driver->driver.name);
813
814 return 0;
815}
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900816
817/*
818 * usb gadget ops
819 */
820static int usbhsg_get_frame(struct usb_gadget *gadget)
821{
822 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
823 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
824
825 return usbhs_frame_get_num(priv);
826}
827
828static struct usb_gadget_ops usbhsg_gadget_ops = {
829 .get_frame = usbhsg_get_frame,
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300830 .start = usbhsg_gadget_start,
831 .stop = usbhsg_gadget_stop,
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900832};
833
834static int usbhsg_start(struct usbhs_priv *priv)
835{
836 return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
837}
838
839static int usbhsg_stop(struct usbhs_priv *priv)
840{
841 return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
842}
843
844int __devinit usbhs_mod_gadget_probe(struct usbhs_priv *priv)
845{
846 struct usbhsg_gpriv *gpriv;
847 struct usbhsg_uep *uep;
848 struct device *dev = usbhs_priv_to_dev(priv);
849 int pipe_size = usbhs_get_dparam(priv, pipe_size);
850 int i;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300851 int ret;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900852
853 gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
854 if (!gpriv) {
855 dev_err(dev, "Could not allocate gadget priv\n");
856 return -ENOMEM;
857 }
858
859 uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
860 if (!uep) {
861 dev_err(dev, "Could not allocate ep\n");
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300862 ret = -ENOMEM;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900863 goto usbhs_mod_gadget_probe_err_gpriv;
864 }
865
866 /*
867 * CAUTION
868 *
869 * There is no guarantee that it is possible to access usb module here.
870 * Don't accesses to it.
871 * The accesse will be enable after "usbhsg_start"
872 */
873
874 /*
875 * register itself
876 */
877 usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
878
879 /* init gpriv */
880 gpriv->mod.name = "gadget";
881 gpriv->mod.start = usbhsg_start;
882 gpriv->mod.stop = usbhsg_stop;
883 gpriv->uep = uep;
884 gpriv->uep_size = pipe_size;
885 usbhsg_status_init(gpriv);
886
887 /*
888 * init gadget
889 */
890 device_initialize(&gpriv->gadget.dev);
891 dev_set_name(&gpriv->gadget.dev, "gadget");
892 gpriv->gadget.dev.parent = dev;
893 gpriv->gadget.name = "renesas_usbhs_udc";
894 gpriv->gadget.ops = &usbhsg_gadget_ops;
895 gpriv->gadget.is_dualspeed = 1;
896
897 INIT_LIST_HEAD(&gpriv->gadget.ep_list);
898
899 /*
900 * init usb_ep
901 */
902 usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
903 uep->gpriv = gpriv;
904 snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
905
906 uep->ep.name = uep->ep_name;
907 uep->ep.ops = &usbhsg_ep_ops;
908 INIT_LIST_HEAD(&uep->ep.ep_list);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900909
910 /* init DCP */
911 if (usbhsg_is_dcp(uep)) {
912 gpriv->gadget.ep0 = &uep->ep;
913 uep->ep.maxpacket = 64;
914 }
915 /* init normal pipe */
916 else {
917 uep->ep.maxpacket = 512;
918 list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
919 }
920 }
921
922 the_controller = gpriv;
923
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300924 ret = usb_add_gadget_udc(dev, &gpriv->gadget);
925 if (ret)
926 goto err_add_udc;
927
928
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900929 dev_info(dev, "gadget probed\n");
930
931 return 0;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300932err_add_udc:
933 kfree(gpriv->uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900934
935usbhs_mod_gadget_probe_err_gpriv:
936 kfree(gpriv);
937
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300938 return ret;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900939}
940
941void __devexit usbhs_mod_gadget_remove(struct usbhs_priv *priv)
942{
943 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
944
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300945 usb_del_gadget_udc(&gpriv->gadget);
Sebastian Andrzej Siewior3af51ac2011-06-03 19:50:48 +0200946 kfree(gpriv->uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900947 kfree(gpriv);
948}