blob: 16484060a24c75e9c2dc55b6bc876b01f75e2bf3 [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 Morimotoe8d548d2011-06-06 14:18:03 +0900560 usbhs_pipe_disable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900561
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900562 while (1) {
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900563 pkt = usbhs_pkt_pop(pipe, NULL);
Kuninori Morimoto8a2c2252011-06-06 14:18:33 +0900564 if (!pkt)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900565 break;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900566 }
567
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900568 return 0;
569}
570
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900571static void usbhsg_uep_init(struct usbhsg_gpriv *gpriv)
572{
573 int i;
574 struct usbhsg_uep *uep;
575
576 usbhsg_for_each_uep_with_dcp(uep, gpriv, i)
577 uep->pipe = NULL;
578}
579
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900580/*
581 *
582 * usb_ep_ops
583 *
584 */
585static int usbhsg_ep_enable(struct usb_ep *ep,
586 const struct usb_endpoint_descriptor *desc)
587{
588 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
589 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
590 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
591 struct usbhs_pipe *pipe;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900592 int ret = -EIO;
593
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900594 /*
595 * if it already have pipe,
596 * nothing to do
597 */
Kuninori Morimoto08e6c612011-06-09 16:48:25 +0900598 if (uep->pipe) {
599 usbhs_pipe_clear(uep->pipe);
Kuninori Morimoto6e6db822011-10-10 22:05:30 -0700600 usbhs_pipe_sequence_data0(uep->pipe);
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900601 return 0;
Kuninori Morimoto08e6c612011-06-09 16:48:25 +0900602 }
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900603
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700604 pipe = usbhs_pipe_malloc(priv,
605 usb_endpoint_type(desc),
606 usb_endpoint_dir_in(desc));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900607 if (pipe) {
608 uep->pipe = pipe;
609 pipe->mod_private = uep;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900610
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700611 /* set epnum / maxp */
Kuninori Morimotobc6fbf52011-10-10 22:04:00 -0700612 usbhs_pipe_config_update(pipe, 0,
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700613 usb_endpoint_num(desc),
614 usb_endpoint_maxp(desc));
615
Kuninori Morimoto233f5192011-07-07 00:23:24 -0700616 /*
617 * usbhs_fifo_dma_push/pop_handler try to
618 * use dmaengine if possible.
619 * It will use pio handler if impossible.
620 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900621 if (usb_endpoint_dir_in(desc))
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700622 pipe->handler = &usbhs_fifo_dma_push_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900623 else
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700624 pipe->handler = &usbhs_fifo_dma_pop_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900625
626 ret = 0;
627 }
Kuninori Morimotocb966322011-04-21 14:10:08 +0900628
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900629 return ret;
630}
631
632static int usbhsg_ep_disable(struct usb_ep *ep)
633{
634 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900635
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900636 return usbhsg_pipe_disable(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900637}
638
639static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
640 gfp_t gfp_flags)
641{
642 struct usbhsg_request *ureq;
643
644 ureq = kzalloc(sizeof *ureq, gfp_flags);
645 if (!ureq)
646 return NULL;
647
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900648 usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
649
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900650 ureq->req.dma = DMA_ADDR_INVALID;
651
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900652 return &ureq->req;
653}
654
655static void usbhsg_ep_free_request(struct usb_ep *ep,
656 struct usb_request *req)
657{
658 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
659
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900660 WARN_ON(!list_empty(&ureq->pkt.node));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900661 kfree(ureq);
662}
663
664static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
665 gfp_t gfp_flags)
666{
667 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
668 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
669 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
670 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900671
672 /* param check */
673 if (usbhsg_is_not_connected(gpriv) ||
674 unlikely(!gpriv->driver) ||
675 unlikely(!pipe))
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900676 return -ESHUTDOWN;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900677
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900678 usbhsg_queue_push(uep, ureq);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900679
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900680 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900681}
682
683static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
684{
685 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
686 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900687 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900688
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900689 usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900690 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
691
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900692 return 0;
693}
694
695static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
696{
697 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
698 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
699 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900700 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900701 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900702 unsigned long flags;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900703
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900704 usbhsg_pipe_disable(uep);
705
706 dev_dbg(dev, "set halt %d (pipe %d)\n",
707 halt, usbhs_pipe_number(pipe));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900708
709 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900710 usbhs_lock(priv, flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900711
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900712 if (halt)
713 usbhs_pipe_stall(pipe);
714 else
715 usbhs_pipe_disable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900716
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900717 if (halt && wedge)
718 usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
719 else
720 usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900721
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900722 usbhs_unlock(priv, flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900723 /******************** spin unlock ******************/
724
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900725 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900726}
727
728static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
729{
730 return __usbhsg_ep_set_halt_wedge(ep, value, 0);
731}
732
733static int usbhsg_ep_set_wedge(struct usb_ep *ep)
734{
735 return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
736}
737
738static struct usb_ep_ops usbhsg_ep_ops = {
739 .enable = usbhsg_ep_enable,
740 .disable = usbhsg_ep_disable,
741
742 .alloc_request = usbhsg_ep_alloc_request,
743 .free_request = usbhsg_ep_free_request,
744
745 .queue = usbhsg_ep_queue,
746 .dequeue = usbhsg_ep_dequeue,
747
748 .set_halt = usbhsg_ep_set_halt,
749 .set_wedge = usbhsg_ep_set_wedge,
750};
751
752/*
753 * usb module start/end
754 */
755static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
756{
757 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
758 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
759 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
760 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900761 unsigned long flags;
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900762 int ret = 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900763
764 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900765 usbhs_lock(priv, flags);
766
767 usbhsg_status_set(gpriv, status);
768 if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
769 usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
770 ret = -1; /* not ready */
771
772 usbhs_unlock(priv, flags);
773 /******************** spin unlock ********************/
774
775 if (ret < 0)
776 return 0; /* not ready is not error */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900777
778 /*
779 * enable interrupt and systems if ready
780 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900781 dev_dbg(dev, "start gadget\n");
782
783 /*
784 * pipe initialize and enable DCP
785 */
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900786 usbhs_pipe_init(priv,
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900787 usbhsg_dma_map_ctrl);
Kuninori Morimotodad67392011-06-06 14:18:28 +0900788 usbhs_fifo_init(priv);
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900789 usbhsg_uep_init(gpriv);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900790
791 /* dcp init */
792 dcp->pipe = usbhs_dcp_malloc(priv);
793 dcp->pipe->mod_private = dcp;
Kuninori Morimotobc6fbf52011-10-10 22:04:00 -0700794 usbhs_pipe_config_update(dcp->pipe, 0, 0, 64);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900795
796 /*
797 * system config enble
798 * - HI speed
799 * - function
800 * - usb module
801 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900802 usbhs_sys_function_ctrl(priv, 1);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900803
804 /*
805 * enable irq callback
806 */
807 mod->irq_dev_state = usbhsg_irq_dev_state;
808 mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900809 usbhs_irq_callback_update(priv, mod);
810
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900811 return 0;
812}
813
814static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
815{
816 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
817 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
818 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
819 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900820 unsigned long flags;
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900821 int ret = 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900822
823 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900824 usbhs_lock(priv, flags);
825
826 usbhsg_status_clr(gpriv, status);
827 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
828 !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
829 ret = -1; /* already done */
830
831 usbhs_unlock(priv, flags);
832 /******************** spin unlock ********************/
833
834 if (ret < 0)
835 return 0; /* already done is not error */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900836
837 /*
838 * disable interrupt and systems if 1st try
839 */
Kuninori Morimotodad67392011-06-06 14:18:28 +0900840 usbhs_fifo_quit(priv);
841
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900842 /* disable all irq */
843 mod->irq_dev_state = NULL;
844 mod->irq_ctrl_stage = NULL;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900845 usbhs_irq_callback_update(priv, mod);
846
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900847 gpriv->gadget.speed = USB_SPEED_UNKNOWN;
848
849 /* disable sys */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900850 usbhs_sys_function_ctrl(priv, 0);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900851
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900852 usbhsg_pipe_disable(dcp);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900853
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900854 dev_dbg(dev, "stop gadget\n");
855
856 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900857}
858
859/*
860 *
861 * linux usb function
862 *
863 */
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300864static int usbhsg_gadget_start(struct usb_gadget *gadget,
865 struct usb_gadget_driver *driver)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900866{
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300867 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -0800868 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900869
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300870 if (!driver ||
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900871 !driver->setup ||
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100872 driver->max_speed < USB_SPEED_FULL)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900873 return -EINVAL;
Kuninori Morimoto3b872182011-07-03 17:42:35 -0700874
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900875 /* first hook up the driver ... */
876 gpriv->driver = driver;
877 gpriv->gadget.dev.driver = &driver->driver;
878
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900879 return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900880}
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900881
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300882static int usbhsg_gadget_stop(struct usb_gadget *gadget,
883 struct usb_gadget_driver *driver)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900884{
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300885 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -0800886 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900887
888 if (!driver ||
Kuninori Morimoto3b872182011-07-03 17:42:35 -0700889 !driver->unbind)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900890 return -EINVAL;
891
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900892 usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
Kuninori Morimoto0cdd7d42011-11-17 18:19:56 -0800893 gpriv->gadget.dev.driver = NULL;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900894 gpriv->driver = NULL;
895
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900896 return 0;
897}
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900898
899/*
900 * usb gadget ops
901 */
902static int usbhsg_get_frame(struct usb_gadget *gadget)
903{
904 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
905 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
906
907 return usbhs_frame_get_num(priv);
908}
909
910static struct usb_gadget_ops usbhsg_gadget_ops = {
911 .get_frame = usbhsg_get_frame,
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300912 .udc_start = usbhsg_gadget_start,
913 .udc_stop = usbhsg_gadget_stop,
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900914};
915
916static int usbhsg_start(struct usbhs_priv *priv)
917{
918 return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
919}
920
921static int usbhsg_stop(struct usbhs_priv *priv)
922{
Kuninori Morimoto8885a892011-11-17 18:19:38 -0800923 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
924
925 /* cable disconnect */
926 if (gpriv->driver &&
927 gpriv->driver->disconnect)
928 gpriv->driver->disconnect(&gpriv->gadget);
929
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900930 return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
931}
932
Kuninori Morimotob7a8d172011-10-26 19:33:49 -0700933int usbhs_mod_gadget_probe(struct usbhs_priv *priv)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900934{
935 struct usbhsg_gpriv *gpriv;
936 struct usbhsg_uep *uep;
937 struct device *dev = usbhs_priv_to_dev(priv);
938 int pipe_size = usbhs_get_dparam(priv, pipe_size);
939 int i;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300940 int ret;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900941
942 gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
943 if (!gpriv) {
944 dev_err(dev, "Could not allocate gadget priv\n");
945 return -ENOMEM;
946 }
947
948 uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
949 if (!uep) {
950 dev_err(dev, "Could not allocate ep\n");
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300951 ret = -ENOMEM;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900952 goto usbhs_mod_gadget_probe_err_gpriv;
953 }
954
955 /*
956 * CAUTION
957 *
958 * There is no guarantee that it is possible to access usb module here.
959 * Don't accesses to it.
960 * The accesse will be enable after "usbhsg_start"
961 */
962
963 /*
964 * register itself
965 */
966 usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
967
968 /* init gpriv */
969 gpriv->mod.name = "gadget";
970 gpriv->mod.start = usbhsg_start;
971 gpriv->mod.stop = usbhsg_stop;
972 gpriv->uep = uep;
973 gpriv->uep_size = pipe_size;
974 usbhsg_status_init(gpriv);
975
976 /*
977 * init gadget
978 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900979 dev_set_name(&gpriv->gadget.dev, "gadget");
980 gpriv->gadget.dev.parent = dev;
981 gpriv->gadget.name = "renesas_usbhs_udc";
982 gpriv->gadget.ops = &usbhsg_gadget_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100983 gpriv->gadget.max_speed = USB_SPEED_HIGH;
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -0800984 ret = device_register(&gpriv->gadget.dev);
985 if (ret < 0)
986 goto err_add_udc;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900987
988 INIT_LIST_HEAD(&gpriv->gadget.ep_list);
989
990 /*
991 * init usb_ep
992 */
993 usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
994 uep->gpriv = gpriv;
995 snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
996
997 uep->ep.name = uep->ep_name;
998 uep->ep.ops = &usbhsg_ep_ops;
999 INIT_LIST_HEAD(&uep->ep.ep_list);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001000
1001 /* init DCP */
1002 if (usbhsg_is_dcp(uep)) {
1003 gpriv->gadget.ep0 = &uep->ep;
1004 uep->ep.maxpacket = 64;
1005 }
1006 /* init normal pipe */
1007 else {
1008 uep->ep.maxpacket = 512;
1009 list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
1010 }
1011 }
1012
Kuninori Morimoto3b872182011-07-03 17:42:35 -07001013 usbhsg_controller_register(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001014
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001015 ret = usb_add_gadget_udc(dev, &gpriv->gadget);
1016 if (ret)
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -08001017 goto err_register;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001018
1019
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001020 dev_info(dev, "gadget probed\n");
1021
1022 return 0;
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -08001023
1024err_register:
1025 device_unregister(&gpriv->gadget.dev);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001026err_add_udc:
1027 kfree(gpriv->uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001028
1029usbhs_mod_gadget_probe_err_gpriv:
1030 kfree(gpriv);
1031
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001032 return ret;
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001033}
1034
Kuninori Morimotob7a8d172011-10-26 19:33:49 -07001035void usbhs_mod_gadget_remove(struct usbhs_priv *priv)
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001036{
1037 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1038
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001039 usb_del_gadget_udc(&gpriv->gadget);
Kuninori Morimoto3b872182011-07-03 17:42:35 -07001040
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -08001041 device_unregister(&gpriv->gadget.dev);
1042
Kuninori Morimoto3b872182011-07-03 17:42:35 -07001043 usbhsg_controller_unregister(gpriv);
1044
Sebastian Andrzej Siewior3af51ac2011-06-03 19:50:48 +02001045 kfree(gpriv->uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001046 kfree(gpriv);
1047}