blob: dba15e07fbd21fe74e2629b5527fbafeae2ca8a4 [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 */
Kuninori Morimotod128a2592011-08-03 21:41:26 -070017#include <linux/dma-mapping.h>
Kuninori Morimoto2f983822011-04-05 11:40:54 +090018#include <linux/io.h>
19#include <linux/module.h>
20#include <linux/platform_device.h>
21#include <linux/usb/ch9.h>
22#include <linux/usb/gadget.h>
23#include "common.h"
24
25/*
26 * struct
27 */
28struct usbhsg_request {
29 struct usb_request req;
Kuninori Morimoto4bd04812011-06-06 14:18:07 +090030 struct usbhs_pkt pkt;
Kuninori Morimoto2f983822011-04-05 11:40:54 +090031};
32
33#define EP_NAME_SIZE 8
34struct usbhsg_gpriv;
Kuninori Morimoto2f983822011-04-05 11:40:54 +090035struct usbhsg_uep {
36 struct usb_ep ep;
37 struct usbhs_pipe *pipe;
Kuninori Morimoto2f983822011-04-05 11:40:54 +090038
39 char ep_name[EP_NAME_SIZE];
40
41 struct usbhsg_gpriv *gpriv;
Kuninori Morimoto2f983822011-04-05 11:40:54 +090042};
43
44struct usbhsg_gpriv {
45 struct usb_gadget gadget;
46 struct usbhs_mod mod;
Kuninori Morimoto3b872182011-07-03 17:42:35 -070047 struct list_head link;
Kuninori Morimoto2f983822011-04-05 11:40:54 +090048
49 struct usbhsg_uep *uep;
50 int uep_size;
51
52 struct usb_gadget_driver *driver;
53
54 u32 status;
55#define USBHSG_STATUS_STARTED (1 << 0)
56#define USBHSG_STATUS_REGISTERD (1 << 1)
57#define USBHSG_STATUS_WEDGE (1 << 2)
58};
59
Kuninori Morimoto2f983822011-04-05 11:40:54 +090060struct usbhsg_recip_handle {
61 char *name;
62 int (*device)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
63 struct usb_ctrlrequest *ctrl);
64 int (*interface)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
65 struct usb_ctrlrequest *ctrl);
66 int (*endpoint)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
67 struct usb_ctrlrequest *ctrl);
68};
69
70/*
71 * macro
72 */
73#define usbhsg_priv_to_gpriv(priv) \
74 container_of( \
75 usbhs_mod_get(priv, USBHS_GADGET), \
76 struct usbhsg_gpriv, mod)
77
78#define __usbhsg_for_each_uep(start, pos, g, i) \
Kuninori Morimotoe94c5872011-07-12 22:01:29 -070079 for (i = start, pos = (g)->uep + i; \
Kuninori Morimoto2f983822011-04-05 11:40:54 +090080 i < (g)->uep_size; \
81 i++, pos = (g)->uep + i)
82
83#define usbhsg_for_each_uep(pos, gpriv, i) \
84 __usbhsg_for_each_uep(1, pos, gpriv, i)
85
86#define usbhsg_for_each_uep_with_dcp(pos, gpriv, i) \
87 __usbhsg_for_each_uep(0, pos, gpriv, i)
88
89#define usbhsg_gadget_to_gpriv(g)\
90 container_of(g, struct usbhsg_gpriv, gadget)
91
92#define usbhsg_req_to_ureq(r)\
93 container_of(r, struct usbhsg_request, req)
94
95#define usbhsg_ep_to_uep(e) container_of(e, struct usbhsg_uep, ep)
Kuninori Morimoto2f983822011-04-05 11:40:54 +090096#define usbhsg_gpriv_to_dev(gp) usbhs_priv_to_dev((gp)->mod.priv)
97#define usbhsg_gpriv_to_priv(gp) ((gp)->mod.priv)
98#define usbhsg_gpriv_to_dcp(gp) ((gp)->uep)
99#define usbhsg_gpriv_to_nth_uep(gp, i) ((gp)->uep + i)
100#define usbhsg_uep_to_gpriv(u) ((u)->gpriv)
101#define usbhsg_uep_to_pipe(u) ((u)->pipe)
102#define usbhsg_pipe_to_uep(p) ((p)->mod_private)
103#define usbhsg_is_dcp(u) ((u) == usbhsg_gpriv_to_dcp((u)->gpriv))
104
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900105#define usbhsg_ureq_to_pkt(u) (&(u)->pkt)
106#define usbhsg_pkt_to_ureq(i) \
107 container_of(i, struct usbhsg_request, pkt)
108
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900109#define usbhsg_is_not_connected(gp) ((gp)->gadget.speed == USB_SPEED_UNKNOWN)
110
111/* status */
112#define usbhsg_status_init(gp) do {(gp)->status = 0; } while (0)
113#define usbhsg_status_set(gp, b) (gp->status |= b)
114#define usbhsg_status_clr(gp, b) (gp->status &= ~b)
115#define usbhsg_status_has(gp, b) (gp->status & b)
116
Kuninori Morimoto3b872182011-07-03 17:42:35 -0700117/* controller */
118LIST_HEAD(the_controller_link);
119
120#define usbhsg_for_each_controller(gpriv)\
121 list_for_each_entry(gpriv, &the_controller_link, link)
122#define usbhsg_controller_register(gpriv)\
123 list_add_tail(&(gpriv)->link, &the_controller_link)
124#define usbhsg_controller_unregister(gpriv)\
125 list_del_init(&(gpriv)->link)
126
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900127/*
Kuninori Morimoto233f5192011-07-07 00:23:24 -0700128 * queue push/pop
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900129 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900130static void usbhsg_queue_pop(struct usbhsg_uep *uep,
131 struct usbhsg_request *ureq,
132 int status)
133{
134 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
135 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
136 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900137
138 dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
139
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900140 ureq->req.status = status;
141 ureq->req.complete(&uep->ep, &ureq->req);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900142}
143
Kuninori Morimoto2cc97192011-10-10 22:03:39 -0700144static void usbhsg_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900145{
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900146 struct usbhs_pipe *pipe = pkt->pipe;
147 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
148 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900149
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900150 ureq->req.actual = pkt->actual;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900151
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900152 usbhsg_queue_pop(uep, ureq, 0);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900153}
154
Kuninori Morimotob3318722011-10-10 22:04:41 -0700155static void usbhsg_queue_push(struct usbhsg_uep *uep,
156 struct usbhsg_request *ureq)
157{
158 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
159 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
160 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
161 struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
162 struct usb_request *req = &ureq->req;
163
164 req->actual = 0;
165 req->status = -EINPROGRESS;
166 usbhs_pkt_push(pipe, pkt, usbhsg_queue_done,
167 req->buf, req->length, req->zero);
Kuninori Morimoto654c35a2011-10-10 22:04:53 -0700168 usbhs_pkt_start(pipe);
Kuninori Morimotob3318722011-10-10 22:04:41 -0700169
170 dev_dbg(dev, "pipe %d : queue push (%d)\n",
171 usbhs_pipe_number(pipe),
172 req->length);
173}
174
Kuninori Morimoto233f5192011-07-07 00:23:24 -0700175/*
176 * dma map/unmap
177 */
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900178static int usbhsg_dma_map(struct device *dev,
179 struct usbhs_pkt *pkt,
180 enum dma_data_direction dir)
181{
182 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
183 struct usb_request *req = &ureq->req;
184
185 if (pkt->dma != DMA_ADDR_INVALID) {
186 dev_err(dev, "dma is already mapped\n");
187 return -EIO;
188 }
189
190 if (req->dma == DMA_ADDR_INVALID) {
191 pkt->dma = dma_map_single(dev, pkt->buf, pkt->length, dir);
192 } else {
193 dma_sync_single_for_device(dev, req->dma, req->length, dir);
194 pkt->dma = req->dma;
195 }
196
197 if (dma_mapping_error(dev, pkt->dma)) {
198 dev_err(dev, "dma mapping error %x\n", pkt->dma);
199 return -EIO;
200 }
201
202 return 0;
203}
204
205static int usbhsg_dma_unmap(struct device *dev,
206 struct usbhs_pkt *pkt,
207 enum dma_data_direction dir)
208{
209 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
210 struct usb_request *req = &ureq->req;
211
212 if (pkt->dma == DMA_ADDR_INVALID) {
213 dev_err(dev, "dma is not mapped\n");
214 return -EIO;
215 }
216
217 if (req->dma == DMA_ADDR_INVALID)
218 dma_unmap_single(dev, pkt->dma, pkt->length, dir);
219 else
220 dma_sync_single_for_cpu(dev, req->dma, req->length, dir);
221
222 pkt->dma = DMA_ADDR_INVALID;
223
224 return 0;
225}
226
227static int usbhsg_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
228{
229 struct usbhs_pipe *pipe = pkt->pipe;
230 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
231 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
232 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
233 enum dma_data_direction dir;
234
235 dir = usbhs_pipe_is_dir_in(pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
236
237 if (map)
238 return usbhsg_dma_map(dev, pkt, dir);
239 else
240 return usbhsg_dma_unmap(dev, pkt, dir);
241}
242
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900243/*
244 * USB_TYPE_STANDARD / clear feature functions
245 */
246static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
247 struct usbhsg_uep *uep,
248 struct usb_ctrlrequest *ctrl)
249{
250 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
251 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
252 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
253
254 usbhs_dcp_control_transfer_done(pipe);
255
256 return 0;
257}
258
259static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
260 struct usbhsg_uep *uep,
261 struct usb_ctrlrequest *ctrl)
262{
263 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
264 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
265
266 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900267 usbhs_pipe_disable(pipe);
Kuninori Morimoto6e6db822011-10-10 22:05:30 -0700268 usbhs_pipe_sequence_data0(pipe);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900269 usbhs_pipe_enable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900270 }
271
272 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
273
Kuninori Morimoto25fa7072011-11-24 17:28:17 -0800274 usbhs_pkt_start(pipe);
275
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900276 return 0;
277}
278
279struct usbhsg_recip_handle req_clear_feature = {
280 .name = "clear feature",
281 .device = usbhsg_recip_handler_std_control_done,
282 .interface = usbhsg_recip_handler_std_control_done,
283 .endpoint = usbhsg_recip_handler_std_clear_endpoint,
284};
285
286/*
Kuninori Morimotoced6e092011-11-24 17:27:50 -0800287 * USB_TYPE_STANDARD / set feature functions
288 */
289static int usbhsg_recip_handler_std_set_endpoint(struct usbhs_priv *priv,
290 struct usbhsg_uep *uep,
291 struct usb_ctrlrequest *ctrl)
292{
293 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
294
295 usbhs_pipe_stall(pipe);
296
297 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
298
299 return 0;
300}
301
302struct usbhsg_recip_handle req_set_feature = {
303 .name = "set feature",
304 .device = usbhsg_recip_handler_std_control_done,
305 .interface = usbhsg_recip_handler_std_control_done,
306 .endpoint = usbhsg_recip_handler_std_set_endpoint,
307};
308
309/*
Kuninori Morimoto17f7f762011-11-24 17:28:04 -0800310 * USB_TYPE_STANDARD / get status functions
311 */
312static void __usbhsg_recip_send_complete(struct usb_ep *ep,
313 struct usb_request *req)
314{
315 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
316
317 /* free allocated recip-buffer/usb_request */
318 kfree(ureq->pkt.buf);
319 usb_ep_free_request(ep, req);
320}
321
322static void __usbhsg_recip_send_status(struct usbhsg_gpriv *gpriv,
323 unsigned short status)
324{
325 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
326 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
327 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
328 struct usb_request *req;
329 unsigned short *buf;
330
331 /* alloc new usb_request for recip */
332 req = usb_ep_alloc_request(&dcp->ep, GFP_ATOMIC);
333 if (!req) {
334 dev_err(dev, "recip request allocation fail\n");
335 return;
336 }
337
338 /* alloc recip data buffer */
339 buf = kmalloc(sizeof(*buf), GFP_ATOMIC);
340 if (!buf) {
341 usb_ep_free_request(&dcp->ep, req);
342 dev_err(dev, "recip data allocation fail\n");
343 return;
344 }
345
346 /* recip data is status */
347 *buf = cpu_to_le16(status);
348
349 /* allocated usb_request/buffer will be freed */
350 req->complete = __usbhsg_recip_send_complete;
351 req->buf = buf;
352 req->length = sizeof(*buf);
353 req->zero = 0;
354
355 /* push packet */
356 pipe->handler = &usbhs_fifo_pio_push_handler;
357 usbhsg_queue_push(dcp, usbhsg_req_to_ureq(req));
358}
359
360static int usbhsg_recip_handler_std_get_device(struct usbhs_priv *priv,
361 struct usbhsg_uep *uep,
362 struct usb_ctrlrequest *ctrl)
363{
364 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
365 unsigned short status = 1 << USB_DEVICE_SELF_POWERED;
366
367 __usbhsg_recip_send_status(gpriv, status);
368
369 return 0;
370}
371
372static int usbhsg_recip_handler_std_get_interface(struct usbhs_priv *priv,
373 struct usbhsg_uep *uep,
374 struct usb_ctrlrequest *ctrl)
375{
376 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
377 unsigned short status = 0;
378
379 __usbhsg_recip_send_status(gpriv, status);
380
381 return 0;
382}
383
384static int usbhsg_recip_handler_std_get_endpoint(struct usbhs_priv *priv,
385 struct usbhsg_uep *uep,
386 struct usb_ctrlrequest *ctrl)
387{
388 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
389 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
390 unsigned short status = 0;
391
392 if (usbhs_pipe_is_stall(pipe))
393 status = 1 << USB_ENDPOINT_HALT;
394
395 __usbhsg_recip_send_status(gpriv, status);
396
397 return 0;
398}
399
400struct usbhsg_recip_handle req_get_status = {
401 .name = "get status",
402 .device = usbhsg_recip_handler_std_get_device,
403 .interface = usbhsg_recip_handler_std_get_interface,
404 .endpoint = usbhsg_recip_handler_std_get_endpoint,
405};
406
407/*
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900408 * USB_TYPE handler
409 */
410static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
411 struct usbhsg_recip_handle *handler,
412 struct usb_ctrlrequest *ctrl)
413{
414 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
415 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
416 struct usbhsg_uep *uep;
Kuninori Morimoto0432eed2011-06-06 14:18:54 +0900417 struct usbhs_pipe *pipe;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900418 int recip = ctrl->bRequestType & USB_RECIP_MASK;
419 int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
420 int ret;
421 int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
422 struct usb_ctrlrequest *ctrl);
423 char *msg;
424
425 uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
Kuninori Morimoto0432eed2011-06-06 14:18:54 +0900426 pipe = usbhsg_uep_to_pipe(uep);
427 if (!pipe) {
Kuninori Morimoto9a28b7b2011-04-21 14:10:12 +0900428 dev_err(dev, "wrong recip request\n");
Kuninori Morimoto25fa7072011-11-24 17:28:17 -0800429 return -EINVAL;
Kuninori Morimoto9a28b7b2011-04-21 14:10:12 +0900430 }
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900431
432 switch (recip) {
433 case USB_RECIP_DEVICE:
434 msg = "DEVICE";
435 func = handler->device;
436 break;
437 case USB_RECIP_INTERFACE:
438 msg = "INTERFACE";
439 func = handler->interface;
440 break;
441 case USB_RECIP_ENDPOINT:
442 msg = "ENDPOINT";
443 func = handler->endpoint;
444 break;
445 default:
446 dev_warn(dev, "unsupported RECIP(%d)\n", recip);
447 func = NULL;
448 ret = -EINVAL;
449 }
450
451 if (func) {
452 dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
453 ret = func(priv, uep, ctrl);
454 }
455
456 return ret;
457}
458
459/*
460 * irq functions
461 *
462 * it will be called from usbhs_interrupt
463 */
464static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
465 struct usbhs_irq_state *irq_state)
466{
467 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
468 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
469
Kuninori Morimoto75587f52011-10-10 22:01:51 -0700470 gpriv->gadget.speed = usbhs_bus_get_speed(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900471
472 dev_dbg(dev, "state = %x : speed : %d\n",
473 usbhs_status_get_device_state(irq_state),
474 gpriv->gadget.speed);
475
476 return 0;
477}
478
479static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
480 struct usbhs_irq_state *irq_state)
481{
482 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
483 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
484 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
485 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
486 struct usb_ctrlrequest ctrl;
487 struct usbhsg_recip_handle *recip_handler = NULL;
488 int stage = usbhs_status_get_ctrl_stage(irq_state);
489 int ret = 0;
490
491 dev_dbg(dev, "stage = %d\n", stage);
492
493 /*
494 * see Manual
495 *
496 * "Operation"
497 * - "Interrupt Function"
498 * - "Control Transfer Stage Transition Interrupt"
499 * - Fig. "Control Transfer Stage Transitions"
500 */
501
502 switch (stage) {
503 case READ_DATA_STAGE:
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700504 pipe->handler = &usbhs_fifo_pio_push_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900505 break;
506 case WRITE_DATA_STAGE:
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700507 pipe->handler = &usbhs_fifo_pio_pop_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900508 break;
509 case NODATA_STATUS_STAGE:
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700510 pipe->handler = &usbhs_ctrl_stage_end_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900511 break;
512 default:
513 return ret;
514 }
515
516 /*
517 * get usb request
518 */
519 usbhs_usbreq_get_val(priv, &ctrl);
520
521 switch (ctrl.bRequestType & USB_TYPE_MASK) {
522 case USB_TYPE_STANDARD:
523 switch (ctrl.bRequest) {
524 case USB_REQ_CLEAR_FEATURE:
525 recip_handler = &req_clear_feature;
526 break;
Kuninori Morimotoced6e092011-11-24 17:27:50 -0800527 case USB_REQ_SET_FEATURE:
528 recip_handler = &req_set_feature;
529 break;
Kuninori Morimoto17f7f762011-11-24 17:28:04 -0800530 case USB_REQ_GET_STATUS:
531 recip_handler = &req_get_status;
532 break;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900533 }
534 }
535
536 /*
537 * setup stage / run recip
538 */
539 if (recip_handler)
540 ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
541 else
542 ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
543
544 if (ret < 0)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900545 usbhs_pipe_stall(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900546
547 return ret;
548}
549
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900550/*
551 *
552 * usb_dcp_ops
553 *
554 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900555static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
556{
557 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto8a2c2252011-06-06 14:18:33 +0900558 struct usbhs_pkt *pkt;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900559
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900560 while (1) {
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900561 pkt = usbhs_pkt_pop(pipe, NULL);
Kuninori Morimoto8a2c2252011-06-06 14:18:33 +0900562 if (!pkt)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900563 break;
Kuninori Morimoto91b158f2011-11-24 17:28:26 -0800564
565 usbhsg_queue_pop(uep, usbhsg_pkt_to_ureq(pkt), -ECONNRESET);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900566 }
567
Kuninori Morimoto91b158f2011-11-24 17:28:26 -0800568 usbhs_pipe_disable(pipe);
569
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900570 return 0;
571}
572
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900573static void usbhsg_uep_init(struct usbhsg_gpriv *gpriv)
574{
575 int i;
576 struct usbhsg_uep *uep;
577
578 usbhsg_for_each_uep_with_dcp(uep, gpriv, i)
579 uep->pipe = NULL;
580}
581
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900582/*
583 *
584 * usb_ep_ops
585 *
586 */
587static int usbhsg_ep_enable(struct usb_ep *ep,
588 const struct usb_endpoint_descriptor *desc)
589{
590 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
591 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
592 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
593 struct usbhs_pipe *pipe;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900594 int ret = -EIO;
595
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900596 /*
597 * if it already have pipe,
598 * nothing to do
599 */
Kuninori Morimoto08e6c612011-06-09 16:48:25 +0900600 if (uep->pipe) {
601 usbhs_pipe_clear(uep->pipe);
Kuninori Morimoto6e6db822011-10-10 22:05:30 -0700602 usbhs_pipe_sequence_data0(uep->pipe);
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900603 return 0;
Kuninori Morimoto08e6c612011-06-09 16:48:25 +0900604 }
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900605
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700606 pipe = usbhs_pipe_malloc(priv,
607 usb_endpoint_type(desc),
608 usb_endpoint_dir_in(desc));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900609 if (pipe) {
610 uep->pipe = pipe;
611 pipe->mod_private = uep;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900612
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700613 /* set epnum / maxp */
Kuninori Morimotobc6fbf52011-10-10 22:04:00 -0700614 usbhs_pipe_config_update(pipe, 0,
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700615 usb_endpoint_num(desc),
616 usb_endpoint_maxp(desc));
617
Kuninori Morimoto233f5192011-07-07 00:23:24 -0700618 /*
619 * usbhs_fifo_dma_push/pop_handler try to
620 * use dmaengine if possible.
621 * It will use pio handler if impossible.
622 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900623 if (usb_endpoint_dir_in(desc))
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700624 pipe->handler = &usbhs_fifo_dma_push_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900625 else
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700626 pipe->handler = &usbhs_fifo_dma_pop_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900627
628 ret = 0;
629 }
Kuninori Morimotocb966322011-04-21 14:10:08 +0900630
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900631 return ret;
632}
633
634static int usbhsg_ep_disable(struct usb_ep *ep)
635{
636 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900637
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900638 return usbhsg_pipe_disable(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900639}
640
641static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
642 gfp_t gfp_flags)
643{
644 struct usbhsg_request *ureq;
645
646 ureq = kzalloc(sizeof *ureq, gfp_flags);
647 if (!ureq)
648 return NULL;
649
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900650 usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
651
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900652 ureq->req.dma = DMA_ADDR_INVALID;
653
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900654 return &ureq->req;
655}
656
657static void usbhsg_ep_free_request(struct usb_ep *ep,
658 struct usb_request *req)
659{
660 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
661
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900662 WARN_ON(!list_empty(&ureq->pkt.node));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900663 kfree(ureq);
664}
665
666static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
667 gfp_t gfp_flags)
668{
669 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
670 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
671 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
672 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900673
674 /* param check */
675 if (usbhsg_is_not_connected(gpriv) ||
676 unlikely(!gpriv->driver) ||
677 unlikely(!pipe))
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900678 return -ESHUTDOWN;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900679
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900680 usbhsg_queue_push(uep, ureq);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900681
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900682 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900683}
684
685static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
686{
687 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
688 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900689 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900690
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900691 usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900692 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
693
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900694 return 0;
695}
696
697static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
698{
699 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
700 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
701 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900702 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900703 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900704 unsigned long flags;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900705
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900706 usbhsg_pipe_disable(uep);
707
708 dev_dbg(dev, "set halt %d (pipe %d)\n",
709 halt, usbhs_pipe_number(pipe));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900710
711 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900712 usbhs_lock(priv, flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900713
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900714 if (halt)
715 usbhs_pipe_stall(pipe);
716 else
717 usbhs_pipe_disable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900718
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900719 if (halt && wedge)
720 usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
721 else
722 usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900723
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900724 usbhs_unlock(priv, flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900725 /******************** spin unlock ******************/
726
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900727 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900728}
729
730static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
731{
732 return __usbhsg_ep_set_halt_wedge(ep, value, 0);
733}
734
735static int usbhsg_ep_set_wedge(struct usb_ep *ep)
736{
737 return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
738}
739
740static struct usb_ep_ops usbhsg_ep_ops = {
741 .enable = usbhsg_ep_enable,
742 .disable = usbhsg_ep_disable,
743
744 .alloc_request = usbhsg_ep_alloc_request,
745 .free_request = usbhsg_ep_free_request,
746
747 .queue = usbhsg_ep_queue,
748 .dequeue = usbhsg_ep_dequeue,
749
750 .set_halt = usbhsg_ep_set_halt,
751 .set_wedge = usbhsg_ep_set_wedge,
752};
753
754/*
755 * usb module start/end
756 */
757static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
758{
759 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
760 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
761 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
762 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900763 unsigned long flags;
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900764 int ret = 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900765
766 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900767 usbhs_lock(priv, flags);
768
769 usbhsg_status_set(gpriv, status);
770 if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
771 usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
772 ret = -1; /* not ready */
773
774 usbhs_unlock(priv, flags);
775 /******************** spin unlock ********************/
776
777 if (ret < 0)
778 return 0; /* not ready is not error */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900779
780 /*
781 * enable interrupt and systems if ready
782 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900783 dev_dbg(dev, "start gadget\n");
784
785 /*
786 * pipe initialize and enable DCP
787 */
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900788 usbhs_pipe_init(priv,
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900789 usbhsg_dma_map_ctrl);
Kuninori Morimotodad67392011-06-06 14:18:28 +0900790 usbhs_fifo_init(priv);
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900791 usbhsg_uep_init(gpriv);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900792
793 /* dcp init */
794 dcp->pipe = usbhs_dcp_malloc(priv);
795 dcp->pipe->mod_private = dcp;
Kuninori Morimotobc6fbf52011-10-10 22:04:00 -0700796 usbhs_pipe_config_update(dcp->pipe, 0, 0, 64);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900797
798 /*
799 * system config enble
800 * - HI speed
801 * - function
802 * - usb module
803 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900804 usbhs_sys_function_ctrl(priv, 1);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900805
806 /*
807 * enable irq callback
808 */
809 mod->irq_dev_state = usbhsg_irq_dev_state;
810 mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900811 usbhs_irq_callback_update(priv, mod);
812
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900813 return 0;
814}
815
816static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
817{
818 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
819 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
820 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
821 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900822 unsigned long flags;
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900823 int ret = 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900824
825 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900826 usbhs_lock(priv, flags);
827
828 usbhsg_status_clr(gpriv, status);
829 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
830 !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
831 ret = -1; /* already done */
832
833 usbhs_unlock(priv, flags);
834 /******************** spin unlock ********************/
835
836 if (ret < 0)
837 return 0; /* already done is not error */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900838
839 /*
840 * disable interrupt and systems if 1st try
841 */
Kuninori Morimotodad67392011-06-06 14:18:28 +0900842 usbhs_fifo_quit(priv);
843
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900844 /* disable all irq */
845 mod->irq_dev_state = NULL;
846 mod->irq_ctrl_stage = NULL;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900847 usbhs_irq_callback_update(priv, mod);
848
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900849 gpriv->gadget.speed = USB_SPEED_UNKNOWN;
850
851 /* disable sys */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900852 usbhs_sys_function_ctrl(priv, 0);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900853
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900854 usbhsg_pipe_disable(dcp);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900855
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900856 dev_dbg(dev, "stop gadget\n");
857
858 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900859}
860
861/*
862 *
863 * linux usb function
864 *
865 */
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300866static int usbhsg_gadget_start(struct usb_gadget *gadget,
867 struct usb_gadget_driver *driver)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900868{
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300869 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -0800870 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900871
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300872 if (!driver ||
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900873 !driver->setup ||
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100874 driver->max_speed < USB_SPEED_FULL)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900875 return -EINVAL;
Kuninori Morimoto3b872182011-07-03 17:42:35 -0700876
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900877 /* first hook up the driver ... */
878 gpriv->driver = driver;
879 gpriv->gadget.dev.driver = &driver->driver;
880
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900881 return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900882}
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900883
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300884static int usbhsg_gadget_stop(struct usb_gadget *gadget,
885 struct usb_gadget_driver *driver)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900886{
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300887 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -0800888 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900889
890 if (!driver ||
Kuninori Morimoto3b872182011-07-03 17:42:35 -0700891 !driver->unbind)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900892 return -EINVAL;
893
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900894 usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
Kuninori Morimoto0cdd7d42011-11-17 18:19:56 -0800895 gpriv->gadget.dev.driver = NULL;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900896 gpriv->driver = NULL;
897
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900898 return 0;
899}
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900900
901/*
902 * usb gadget ops
903 */
904static int usbhsg_get_frame(struct usb_gadget *gadget)
905{
906 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
907 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
908
909 return usbhs_frame_get_num(priv);
910}
911
912static struct usb_gadget_ops usbhsg_gadget_ops = {
913 .get_frame = usbhsg_get_frame,
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300914 .udc_start = usbhsg_gadget_start,
915 .udc_stop = usbhsg_gadget_stop,
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900916};
917
918static int usbhsg_start(struct usbhs_priv *priv)
919{
920 return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
921}
922
923static int usbhsg_stop(struct usbhs_priv *priv)
924{
Kuninori Morimoto8885a892011-11-17 18:19:38 -0800925 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
926
927 /* cable disconnect */
928 if (gpriv->driver &&
929 gpriv->driver->disconnect)
930 gpriv->driver->disconnect(&gpriv->gadget);
931
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900932 return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
933}
934
Kuninori Morimotob7a8d172011-10-26 19:33:49 -0700935int usbhs_mod_gadget_probe(struct usbhs_priv *priv)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900936{
937 struct usbhsg_gpriv *gpriv;
938 struct usbhsg_uep *uep;
939 struct device *dev = usbhs_priv_to_dev(priv);
940 int pipe_size = usbhs_get_dparam(priv, pipe_size);
941 int i;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300942 int ret;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900943
944 gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
945 if (!gpriv) {
946 dev_err(dev, "Could not allocate gadget priv\n");
947 return -ENOMEM;
948 }
949
950 uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
951 if (!uep) {
952 dev_err(dev, "Could not allocate ep\n");
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300953 ret = -ENOMEM;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900954 goto usbhs_mod_gadget_probe_err_gpriv;
955 }
956
957 /*
958 * CAUTION
959 *
960 * There is no guarantee that it is possible to access usb module here.
961 * Don't accesses to it.
962 * The accesse will be enable after "usbhsg_start"
963 */
964
965 /*
966 * register itself
967 */
968 usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
969
970 /* init gpriv */
971 gpriv->mod.name = "gadget";
972 gpriv->mod.start = usbhsg_start;
973 gpriv->mod.stop = usbhsg_stop;
974 gpriv->uep = uep;
975 gpriv->uep_size = pipe_size;
976 usbhsg_status_init(gpriv);
977
978 /*
979 * init gadget
980 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900981 dev_set_name(&gpriv->gadget.dev, "gadget");
982 gpriv->gadget.dev.parent = dev;
983 gpriv->gadget.name = "renesas_usbhs_udc";
984 gpriv->gadget.ops = &usbhsg_gadget_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100985 gpriv->gadget.max_speed = USB_SPEED_HIGH;
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -0800986 ret = device_register(&gpriv->gadget.dev);
987 if (ret < 0)
988 goto err_add_udc;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900989
990 INIT_LIST_HEAD(&gpriv->gadget.ep_list);
991
992 /*
993 * init usb_ep
994 */
995 usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
996 uep->gpriv = gpriv;
997 snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
998
999 uep->ep.name = uep->ep_name;
1000 uep->ep.ops = &usbhsg_ep_ops;
1001 INIT_LIST_HEAD(&uep->ep.ep_list);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001002
1003 /* init DCP */
1004 if (usbhsg_is_dcp(uep)) {
1005 gpriv->gadget.ep0 = &uep->ep;
1006 uep->ep.maxpacket = 64;
1007 }
1008 /* init normal pipe */
1009 else {
1010 uep->ep.maxpacket = 512;
1011 list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
1012 }
1013 }
1014
Kuninori Morimoto3b872182011-07-03 17:42:35 -07001015 usbhsg_controller_register(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001016
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001017 ret = usb_add_gadget_udc(dev, &gpriv->gadget);
1018 if (ret)
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -08001019 goto err_register;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001020
1021
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001022 dev_info(dev, "gadget probed\n");
1023
1024 return 0;
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -08001025
1026err_register:
1027 device_unregister(&gpriv->gadget.dev);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001028err_add_udc:
1029 kfree(gpriv->uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001030
1031usbhs_mod_gadget_probe_err_gpriv:
1032 kfree(gpriv);
1033
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001034 return ret;
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001035}
1036
Kuninori Morimotob7a8d172011-10-26 19:33:49 -07001037void usbhs_mod_gadget_remove(struct usbhs_priv *priv)
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001038{
1039 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1040
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001041 usb_del_gadget_udc(&gpriv->gadget);
Kuninori Morimoto3b872182011-07-03 17:42:35 -07001042
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -08001043 device_unregister(&gpriv->gadget.dev);
1044
Kuninori Morimoto3b872182011-07-03 17:42:35 -07001045 usbhsg_controller_unregister(gpriv);
1046
Sebastian Andrzej Siewior3af51ac2011-06-03 19:50:48 +02001047 kfree(gpriv->uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001048 kfree(gpriv);
1049}