blob: 812960ba95e15efd1a01b3d36e68f487c7b0ab5e [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 Morimoto2f983822011-04-05 11:40:54 +0900274 return 0;
275}
276
277struct usbhsg_recip_handle req_clear_feature = {
278 .name = "clear feature",
279 .device = usbhsg_recip_handler_std_control_done,
280 .interface = usbhsg_recip_handler_std_control_done,
281 .endpoint = usbhsg_recip_handler_std_clear_endpoint,
282};
283
284/*
Kuninori Morimotoced6e092011-11-24 17:27:50 -0800285 * USB_TYPE_STANDARD / set feature functions
286 */
287static int usbhsg_recip_handler_std_set_endpoint(struct usbhs_priv *priv,
288 struct usbhsg_uep *uep,
289 struct usb_ctrlrequest *ctrl)
290{
291 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
292
293 usbhs_pipe_stall(pipe);
294
295 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
296
297 return 0;
298}
299
300struct usbhsg_recip_handle req_set_feature = {
301 .name = "set feature",
302 .device = usbhsg_recip_handler_std_control_done,
303 .interface = usbhsg_recip_handler_std_control_done,
304 .endpoint = usbhsg_recip_handler_std_set_endpoint,
305};
306
307/*
Kuninori Morimoto17f7f762011-11-24 17:28:04 -0800308 * USB_TYPE_STANDARD / get status functions
309 */
310static void __usbhsg_recip_send_complete(struct usb_ep *ep,
311 struct usb_request *req)
312{
313 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
314
315 /* free allocated recip-buffer/usb_request */
316 kfree(ureq->pkt.buf);
317 usb_ep_free_request(ep, req);
318}
319
320static void __usbhsg_recip_send_status(struct usbhsg_gpriv *gpriv,
321 unsigned short status)
322{
323 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
324 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
325 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
326 struct usb_request *req;
327 unsigned short *buf;
328
329 /* alloc new usb_request for recip */
330 req = usb_ep_alloc_request(&dcp->ep, GFP_ATOMIC);
331 if (!req) {
332 dev_err(dev, "recip request allocation fail\n");
333 return;
334 }
335
336 /* alloc recip data buffer */
337 buf = kmalloc(sizeof(*buf), GFP_ATOMIC);
338 if (!buf) {
339 usb_ep_free_request(&dcp->ep, req);
340 dev_err(dev, "recip data allocation fail\n");
341 return;
342 }
343
344 /* recip data is status */
345 *buf = cpu_to_le16(status);
346
347 /* allocated usb_request/buffer will be freed */
348 req->complete = __usbhsg_recip_send_complete;
349 req->buf = buf;
350 req->length = sizeof(*buf);
351 req->zero = 0;
352
353 /* push packet */
354 pipe->handler = &usbhs_fifo_pio_push_handler;
355 usbhsg_queue_push(dcp, usbhsg_req_to_ureq(req));
356}
357
358static int usbhsg_recip_handler_std_get_device(struct usbhs_priv *priv,
359 struct usbhsg_uep *uep,
360 struct usb_ctrlrequest *ctrl)
361{
362 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
363 unsigned short status = 1 << USB_DEVICE_SELF_POWERED;
364
365 __usbhsg_recip_send_status(gpriv, status);
366
367 return 0;
368}
369
370static int usbhsg_recip_handler_std_get_interface(struct usbhs_priv *priv,
371 struct usbhsg_uep *uep,
372 struct usb_ctrlrequest *ctrl)
373{
374 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
375 unsigned short status = 0;
376
377 __usbhsg_recip_send_status(gpriv, status);
378
379 return 0;
380}
381
382static int usbhsg_recip_handler_std_get_endpoint(struct usbhs_priv *priv,
383 struct usbhsg_uep *uep,
384 struct usb_ctrlrequest *ctrl)
385{
386 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
387 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
388 unsigned short status = 0;
389
390 if (usbhs_pipe_is_stall(pipe))
391 status = 1 << USB_ENDPOINT_HALT;
392
393 __usbhsg_recip_send_status(gpriv, status);
394
395 return 0;
396}
397
398struct usbhsg_recip_handle req_get_status = {
399 .name = "get status",
400 .device = usbhsg_recip_handler_std_get_device,
401 .interface = usbhsg_recip_handler_std_get_interface,
402 .endpoint = usbhsg_recip_handler_std_get_endpoint,
403};
404
405/*
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900406 * USB_TYPE handler
407 */
408static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
409 struct usbhsg_recip_handle *handler,
410 struct usb_ctrlrequest *ctrl)
411{
412 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
413 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
414 struct usbhsg_uep *uep;
Kuninori Morimoto0432eed2011-06-06 14:18:54 +0900415 struct usbhs_pipe *pipe;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900416 int recip = ctrl->bRequestType & USB_RECIP_MASK;
417 int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
418 int ret;
419 int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
420 struct usb_ctrlrequest *ctrl);
421 char *msg;
422
423 uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
Kuninori Morimoto0432eed2011-06-06 14:18:54 +0900424 pipe = usbhsg_uep_to_pipe(uep);
425 if (!pipe) {
Kuninori Morimoto9a28b7b2011-04-21 14:10:12 +0900426 dev_err(dev, "wrong recip request\n");
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900427 ret = -EINVAL;
428 goto usbhsg_recip_run_handle_end;
Kuninori Morimoto9a28b7b2011-04-21 14:10:12 +0900429 }
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900430
431 switch (recip) {
432 case USB_RECIP_DEVICE:
433 msg = "DEVICE";
434 func = handler->device;
435 break;
436 case USB_RECIP_INTERFACE:
437 msg = "INTERFACE";
438 func = handler->interface;
439 break;
440 case USB_RECIP_ENDPOINT:
441 msg = "ENDPOINT";
442 func = handler->endpoint;
443 break;
444 default:
445 dev_warn(dev, "unsupported RECIP(%d)\n", recip);
446 func = NULL;
447 ret = -EINVAL;
448 }
449
450 if (func) {
451 dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
452 ret = func(priv, uep, ctrl);
453 }
454
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900455usbhsg_recip_run_handle_end:
Kuninori Morimoto0432eed2011-06-06 14:18:54 +0900456 usbhs_pkt_start(pipe);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900457
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900458 return ret;
459}
460
461/*
462 * irq functions
463 *
464 * it will be called from usbhs_interrupt
465 */
466static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
467 struct usbhs_irq_state *irq_state)
468{
469 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
470 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
471
Kuninori Morimoto75587f52011-10-10 22:01:51 -0700472 gpriv->gadget.speed = usbhs_bus_get_speed(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900473
474 dev_dbg(dev, "state = %x : speed : %d\n",
475 usbhs_status_get_device_state(irq_state),
476 gpriv->gadget.speed);
477
478 return 0;
479}
480
481static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
482 struct usbhs_irq_state *irq_state)
483{
484 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
485 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
486 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
487 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
488 struct usb_ctrlrequest ctrl;
489 struct usbhsg_recip_handle *recip_handler = NULL;
490 int stage = usbhs_status_get_ctrl_stage(irq_state);
491 int ret = 0;
492
493 dev_dbg(dev, "stage = %d\n", stage);
494
495 /*
496 * see Manual
497 *
498 * "Operation"
499 * - "Interrupt Function"
500 * - "Control Transfer Stage Transition Interrupt"
501 * - Fig. "Control Transfer Stage Transitions"
502 */
503
504 switch (stage) {
505 case READ_DATA_STAGE:
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700506 pipe->handler = &usbhs_fifo_pio_push_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900507 break;
508 case WRITE_DATA_STAGE:
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700509 pipe->handler = &usbhs_fifo_pio_pop_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900510 break;
511 case NODATA_STATUS_STAGE:
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700512 pipe->handler = &usbhs_ctrl_stage_end_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900513 break;
514 default:
515 return ret;
516 }
517
518 /*
519 * get usb request
520 */
521 usbhs_usbreq_get_val(priv, &ctrl);
522
523 switch (ctrl.bRequestType & USB_TYPE_MASK) {
524 case USB_TYPE_STANDARD:
525 switch (ctrl.bRequest) {
526 case USB_REQ_CLEAR_FEATURE:
527 recip_handler = &req_clear_feature;
528 break;
Kuninori Morimotoced6e092011-11-24 17:27:50 -0800529 case USB_REQ_SET_FEATURE:
530 recip_handler = &req_set_feature;
531 break;
Kuninori Morimoto17f7f762011-11-24 17:28:04 -0800532 case USB_REQ_GET_STATUS:
533 recip_handler = &req_get_status;
534 break;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900535 }
536 }
537
538 /*
539 * setup stage / run recip
540 */
541 if (recip_handler)
542 ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
543 else
544 ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
545
546 if (ret < 0)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900547 usbhs_pipe_stall(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900548
549 return ret;
550}
551
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900552/*
553 *
554 * usb_dcp_ops
555 *
556 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900557static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
558{
559 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto8a2c2252011-06-06 14:18:33 +0900560 struct usbhs_pkt *pkt;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900561
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900562 usbhs_pipe_disable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900563
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900564 while (1) {
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900565 pkt = usbhs_pkt_pop(pipe, NULL);
Kuninori Morimoto8a2c2252011-06-06 14:18:33 +0900566 if (!pkt)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900567 break;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900568 }
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}