blob: 1951de02957e8c6bd96fd0b7aca496baa8fb1fe3 [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 Morimotodfbb7f42011-11-24 17:28:35 -080017#include <linux/delay.h>
Kuninori Morimotod128a2592011-08-03 21:41:26 -070018#include <linux/dma-mapping.h>
Kuninori Morimoto2f983822011-04-05 11:40:54 +090019#include <linux/io.h>
20#include <linux/module.h>
21#include <linux/platform_device.h>
22#include <linux/usb/ch9.h>
23#include <linux/usb/gadget.h>
24#include "common.h"
25
26/*
27 * struct
28 */
29struct usbhsg_request {
30 struct usb_request req;
Kuninori Morimoto4bd04812011-06-06 14:18:07 +090031 struct usbhs_pkt pkt;
Kuninori Morimoto2f983822011-04-05 11:40:54 +090032};
33
34#define EP_NAME_SIZE 8
35struct usbhsg_gpriv;
Kuninori Morimoto2f983822011-04-05 11:40:54 +090036struct usbhsg_uep {
37 struct usb_ep ep;
38 struct usbhs_pipe *pipe;
Kuninori Morimoto2f983822011-04-05 11:40:54 +090039
40 char ep_name[EP_NAME_SIZE];
41
42 struct usbhsg_gpriv *gpriv;
Kuninori Morimoto2f983822011-04-05 11:40:54 +090043};
44
45struct usbhsg_gpriv {
46 struct usb_gadget gadget;
47 struct usbhs_mod mod;
Kuninori Morimoto3b872182011-07-03 17:42:35 -070048 struct list_head link;
Kuninori Morimoto2f983822011-04-05 11:40:54 +090049
50 struct usbhsg_uep *uep;
51 int uep_size;
52
53 struct usb_gadget_driver *driver;
54
55 u32 status;
56#define USBHSG_STATUS_STARTED (1 << 0)
57#define USBHSG_STATUS_REGISTERD (1 << 1)
58#define USBHSG_STATUS_WEDGE (1 << 2)
59};
60
Kuninori Morimoto2f983822011-04-05 11:40:54 +090061struct usbhsg_recip_handle {
62 char *name;
63 int (*device)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
64 struct usb_ctrlrequest *ctrl);
65 int (*interface)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
66 struct usb_ctrlrequest *ctrl);
67 int (*endpoint)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
68 struct usb_ctrlrequest *ctrl);
69};
70
71/*
72 * macro
73 */
74#define usbhsg_priv_to_gpriv(priv) \
75 container_of( \
76 usbhs_mod_get(priv, USBHS_GADGET), \
77 struct usbhsg_gpriv, mod)
78
79#define __usbhsg_for_each_uep(start, pos, g, i) \
Kuninori Morimotoe94c5872011-07-12 22:01:29 -070080 for (i = start, pos = (g)->uep + i; \
Kuninori Morimoto2f983822011-04-05 11:40:54 +090081 i < (g)->uep_size; \
82 i++, pos = (g)->uep + i)
83
84#define usbhsg_for_each_uep(pos, gpriv, i) \
85 __usbhsg_for_each_uep(1, pos, gpriv, i)
86
87#define usbhsg_for_each_uep_with_dcp(pos, gpriv, i) \
88 __usbhsg_for_each_uep(0, pos, gpriv, i)
89
90#define usbhsg_gadget_to_gpriv(g)\
91 container_of(g, struct usbhsg_gpriv, gadget)
92
93#define usbhsg_req_to_ureq(r)\
94 container_of(r, struct usbhsg_request, req)
95
96#define usbhsg_ep_to_uep(e) container_of(e, struct usbhsg_uep, ep)
Kuninori Morimoto2f983822011-04-05 11:40:54 +090097#define usbhsg_gpriv_to_dev(gp) usbhs_priv_to_dev((gp)->mod.priv)
98#define usbhsg_gpriv_to_priv(gp) ((gp)->mod.priv)
99#define usbhsg_gpriv_to_dcp(gp) ((gp)->uep)
100#define usbhsg_gpriv_to_nth_uep(gp, i) ((gp)->uep + i)
101#define usbhsg_uep_to_gpriv(u) ((u)->gpriv)
102#define usbhsg_uep_to_pipe(u) ((u)->pipe)
103#define usbhsg_pipe_to_uep(p) ((p)->mod_private)
104#define usbhsg_is_dcp(u) ((u) == usbhsg_gpriv_to_dcp((u)->gpriv))
105
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900106#define usbhsg_ureq_to_pkt(u) (&(u)->pkt)
107#define usbhsg_pkt_to_ureq(i) \
108 container_of(i, struct usbhsg_request, pkt)
109
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900110#define usbhsg_is_not_connected(gp) ((gp)->gadget.speed == USB_SPEED_UNKNOWN)
111
112/* status */
113#define usbhsg_status_init(gp) do {(gp)->status = 0; } while (0)
114#define usbhsg_status_set(gp, b) (gp->status |= b)
115#define usbhsg_status_clr(gp, b) (gp->status &= ~b)
116#define usbhsg_status_has(gp, b) (gp->status & b)
117
Kuninori Morimoto3b872182011-07-03 17:42:35 -0700118/* controller */
119LIST_HEAD(the_controller_link);
120
121#define usbhsg_for_each_controller(gpriv)\
122 list_for_each_entry(gpriv, &the_controller_link, link)
123#define usbhsg_controller_register(gpriv)\
124 list_add_tail(&(gpriv)->link, &the_controller_link)
125#define usbhsg_controller_unregister(gpriv)\
126 list_del_init(&(gpriv)->link)
127
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900128/*
Kuninori Morimoto233f5192011-07-07 00:23:24 -0700129 * queue push/pop
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900130 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900131static void usbhsg_queue_pop(struct usbhsg_uep *uep,
132 struct usbhsg_request *ureq,
133 int status)
134{
135 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
136 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
137 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900138
139 dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
140
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900141 ureq->req.status = status;
142 ureq->req.complete(&uep->ep, &ureq->req);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900143}
144
Kuninori Morimoto2cc97192011-10-10 22:03:39 -0700145static void usbhsg_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900146{
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900147 struct usbhs_pipe *pipe = pkt->pipe;
148 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
149 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900150
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900151 ureq->req.actual = pkt->actual;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900152
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900153 usbhsg_queue_pop(uep, ureq, 0);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900154}
155
Kuninori Morimotob3318722011-10-10 22:04:41 -0700156static void usbhsg_queue_push(struct usbhsg_uep *uep,
157 struct usbhsg_request *ureq)
158{
159 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
160 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
161 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
162 struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
163 struct usb_request *req = &ureq->req;
164
165 req->actual = 0;
166 req->status = -EINPROGRESS;
167 usbhs_pkt_push(pipe, pkt, usbhsg_queue_done,
168 req->buf, req->length, req->zero);
Kuninori Morimoto654c35a2011-10-10 22:04:53 -0700169 usbhs_pkt_start(pipe);
Kuninori Morimotob3318722011-10-10 22:04:41 -0700170
171 dev_dbg(dev, "pipe %d : queue push (%d)\n",
172 usbhs_pipe_number(pipe),
173 req->length);
174}
175
Kuninori Morimoto233f5192011-07-07 00:23:24 -0700176/*
177 * dma map/unmap
178 */
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900179static int usbhsg_dma_map(struct device *dev,
180 struct usbhs_pkt *pkt,
181 enum dma_data_direction dir)
182{
183 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
184 struct usb_request *req = &ureq->req;
185
186 if (pkt->dma != DMA_ADDR_INVALID) {
187 dev_err(dev, "dma is already mapped\n");
188 return -EIO;
189 }
190
191 if (req->dma == DMA_ADDR_INVALID) {
192 pkt->dma = dma_map_single(dev, pkt->buf, pkt->length, dir);
193 } else {
194 dma_sync_single_for_device(dev, req->dma, req->length, dir);
195 pkt->dma = req->dma;
196 }
197
198 if (dma_mapping_error(dev, pkt->dma)) {
199 dev_err(dev, "dma mapping error %x\n", pkt->dma);
200 return -EIO;
201 }
202
203 return 0;
204}
205
206static int usbhsg_dma_unmap(struct device *dev,
207 struct usbhs_pkt *pkt,
208 enum dma_data_direction dir)
209{
210 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
211 struct usb_request *req = &ureq->req;
212
213 if (pkt->dma == DMA_ADDR_INVALID) {
214 dev_err(dev, "dma is not mapped\n");
215 return -EIO;
216 }
217
218 if (req->dma == DMA_ADDR_INVALID)
219 dma_unmap_single(dev, pkt->dma, pkt->length, dir);
220 else
221 dma_sync_single_for_cpu(dev, req->dma, req->length, dir);
222
223 pkt->dma = DMA_ADDR_INVALID;
224
225 return 0;
226}
227
228static int usbhsg_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
229{
230 struct usbhs_pipe *pipe = pkt->pipe;
231 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
232 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
233 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
234 enum dma_data_direction dir;
235
236 dir = usbhs_pipe_is_dir_in(pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
237
238 if (map)
239 return usbhsg_dma_map(dev, pkt, dir);
240 else
241 return usbhsg_dma_unmap(dev, pkt, dir);
242}
243
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900244/*
245 * USB_TYPE_STANDARD / clear feature functions
246 */
247static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
248 struct usbhsg_uep *uep,
249 struct usb_ctrlrequest *ctrl)
250{
251 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
252 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
253 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
254
255 usbhs_dcp_control_transfer_done(pipe);
256
257 return 0;
258}
259
260static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
261 struct usbhsg_uep *uep,
262 struct usb_ctrlrequest *ctrl)
263{
264 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
265 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
266
267 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900268 usbhs_pipe_disable(pipe);
Kuninori Morimoto6e6db822011-10-10 22:05:30 -0700269 usbhs_pipe_sequence_data0(pipe);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900270 usbhs_pipe_enable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900271 }
272
273 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
274
Kuninori Morimoto25fa7072011-11-24 17:28:17 -0800275 usbhs_pkt_start(pipe);
276
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900277 return 0;
278}
279
280struct usbhsg_recip_handle req_clear_feature = {
281 .name = "clear feature",
282 .device = usbhsg_recip_handler_std_control_done,
283 .interface = usbhsg_recip_handler_std_control_done,
284 .endpoint = usbhsg_recip_handler_std_clear_endpoint,
285};
286
287/*
Kuninori Morimotoced6e092011-11-24 17:27:50 -0800288 * USB_TYPE_STANDARD / set feature functions
289 */
Kuninori Morimotodfbb7f42011-11-24 17:28:35 -0800290static int usbhsg_recip_handler_std_set_device(struct usbhs_priv *priv,
291 struct usbhsg_uep *uep,
292 struct usb_ctrlrequest *ctrl)
293{
294 switch (le16_to_cpu(ctrl->wValue)) {
295 case USB_DEVICE_TEST_MODE:
296 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
297 udelay(100);
298 usbhs_sys_set_test_mode(priv, le16_to_cpu(ctrl->wIndex >> 8));
299 break;
300 default:
301 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
302 break;
303 }
304
305 return 0;
306}
307
Kuninori Morimotoced6e092011-11-24 17:27:50 -0800308static int usbhsg_recip_handler_std_set_endpoint(struct usbhs_priv *priv,
309 struct usbhsg_uep *uep,
310 struct usb_ctrlrequest *ctrl)
311{
312 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
313
314 usbhs_pipe_stall(pipe);
315
316 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
317
318 return 0;
319}
320
321struct usbhsg_recip_handle req_set_feature = {
322 .name = "set feature",
Kuninori Morimotodfbb7f42011-11-24 17:28:35 -0800323 .device = usbhsg_recip_handler_std_set_device,
Kuninori Morimotoced6e092011-11-24 17:27:50 -0800324 .interface = usbhsg_recip_handler_std_control_done,
325 .endpoint = usbhsg_recip_handler_std_set_endpoint,
326};
327
328/*
Kuninori Morimoto17f7f762011-11-24 17:28:04 -0800329 * USB_TYPE_STANDARD / get status functions
330 */
331static void __usbhsg_recip_send_complete(struct usb_ep *ep,
332 struct usb_request *req)
333{
334 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
335
336 /* free allocated recip-buffer/usb_request */
337 kfree(ureq->pkt.buf);
338 usb_ep_free_request(ep, req);
339}
340
341static void __usbhsg_recip_send_status(struct usbhsg_gpriv *gpriv,
342 unsigned short status)
343{
344 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
345 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
346 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
347 struct usb_request *req;
348 unsigned short *buf;
349
350 /* alloc new usb_request for recip */
351 req = usb_ep_alloc_request(&dcp->ep, GFP_ATOMIC);
352 if (!req) {
353 dev_err(dev, "recip request allocation fail\n");
354 return;
355 }
356
357 /* alloc recip data buffer */
358 buf = kmalloc(sizeof(*buf), GFP_ATOMIC);
359 if (!buf) {
360 usb_ep_free_request(&dcp->ep, req);
361 dev_err(dev, "recip data allocation fail\n");
362 return;
363 }
364
365 /* recip data is status */
366 *buf = cpu_to_le16(status);
367
368 /* allocated usb_request/buffer will be freed */
369 req->complete = __usbhsg_recip_send_complete;
370 req->buf = buf;
371 req->length = sizeof(*buf);
372 req->zero = 0;
373
374 /* push packet */
375 pipe->handler = &usbhs_fifo_pio_push_handler;
376 usbhsg_queue_push(dcp, usbhsg_req_to_ureq(req));
377}
378
379static int usbhsg_recip_handler_std_get_device(struct usbhs_priv *priv,
380 struct usbhsg_uep *uep,
381 struct usb_ctrlrequest *ctrl)
382{
383 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
384 unsigned short status = 1 << USB_DEVICE_SELF_POWERED;
385
386 __usbhsg_recip_send_status(gpriv, status);
387
388 return 0;
389}
390
391static int usbhsg_recip_handler_std_get_interface(struct usbhs_priv *priv,
392 struct usbhsg_uep *uep,
393 struct usb_ctrlrequest *ctrl)
394{
395 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
396 unsigned short status = 0;
397
398 __usbhsg_recip_send_status(gpriv, status);
399
400 return 0;
401}
402
403static int usbhsg_recip_handler_std_get_endpoint(struct usbhs_priv *priv,
404 struct usbhsg_uep *uep,
405 struct usb_ctrlrequest *ctrl)
406{
407 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
408 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
409 unsigned short status = 0;
410
411 if (usbhs_pipe_is_stall(pipe))
412 status = 1 << USB_ENDPOINT_HALT;
413
414 __usbhsg_recip_send_status(gpriv, status);
415
416 return 0;
417}
418
419struct usbhsg_recip_handle req_get_status = {
420 .name = "get status",
421 .device = usbhsg_recip_handler_std_get_device,
422 .interface = usbhsg_recip_handler_std_get_interface,
423 .endpoint = usbhsg_recip_handler_std_get_endpoint,
424};
425
426/*
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900427 * USB_TYPE handler
428 */
429static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
430 struct usbhsg_recip_handle *handler,
431 struct usb_ctrlrequest *ctrl)
432{
433 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
434 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
435 struct usbhsg_uep *uep;
Kuninori Morimoto0432eed2011-06-06 14:18:54 +0900436 struct usbhs_pipe *pipe;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900437 int recip = ctrl->bRequestType & USB_RECIP_MASK;
438 int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
439 int ret;
440 int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
441 struct usb_ctrlrequest *ctrl);
442 char *msg;
443
444 uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
Kuninori Morimoto0432eed2011-06-06 14:18:54 +0900445 pipe = usbhsg_uep_to_pipe(uep);
446 if (!pipe) {
Kuninori Morimoto9a28b7b2011-04-21 14:10:12 +0900447 dev_err(dev, "wrong recip request\n");
Kuninori Morimoto25fa7072011-11-24 17:28:17 -0800448 return -EINVAL;
Kuninori Morimoto9a28b7b2011-04-21 14:10:12 +0900449 }
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900450
451 switch (recip) {
452 case USB_RECIP_DEVICE:
453 msg = "DEVICE";
454 func = handler->device;
455 break;
456 case USB_RECIP_INTERFACE:
457 msg = "INTERFACE";
458 func = handler->interface;
459 break;
460 case USB_RECIP_ENDPOINT:
461 msg = "ENDPOINT";
462 func = handler->endpoint;
463 break;
464 default:
465 dev_warn(dev, "unsupported RECIP(%d)\n", recip);
466 func = NULL;
467 ret = -EINVAL;
468 }
469
470 if (func) {
471 dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
472 ret = func(priv, uep, ctrl);
473 }
474
475 return ret;
476}
477
478/*
479 * irq functions
480 *
481 * it will be called from usbhs_interrupt
482 */
483static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
484 struct usbhs_irq_state *irq_state)
485{
486 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
487 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
488
Kuninori Morimoto75587f52011-10-10 22:01:51 -0700489 gpriv->gadget.speed = usbhs_bus_get_speed(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900490
491 dev_dbg(dev, "state = %x : speed : %d\n",
492 usbhs_status_get_device_state(irq_state),
493 gpriv->gadget.speed);
494
495 return 0;
496}
497
498static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
499 struct usbhs_irq_state *irq_state)
500{
501 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
502 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
503 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
504 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
505 struct usb_ctrlrequest ctrl;
506 struct usbhsg_recip_handle *recip_handler = NULL;
507 int stage = usbhs_status_get_ctrl_stage(irq_state);
508 int ret = 0;
509
510 dev_dbg(dev, "stage = %d\n", stage);
511
512 /*
513 * see Manual
514 *
515 * "Operation"
516 * - "Interrupt Function"
517 * - "Control Transfer Stage Transition Interrupt"
518 * - Fig. "Control Transfer Stage Transitions"
519 */
520
521 switch (stage) {
522 case READ_DATA_STAGE:
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700523 pipe->handler = &usbhs_fifo_pio_push_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900524 break;
525 case WRITE_DATA_STAGE:
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700526 pipe->handler = &usbhs_fifo_pio_pop_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900527 break;
528 case NODATA_STATUS_STAGE:
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700529 pipe->handler = &usbhs_ctrl_stage_end_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900530 break;
531 default:
532 return ret;
533 }
534
535 /*
536 * get usb request
537 */
538 usbhs_usbreq_get_val(priv, &ctrl);
539
540 switch (ctrl.bRequestType & USB_TYPE_MASK) {
541 case USB_TYPE_STANDARD:
542 switch (ctrl.bRequest) {
543 case USB_REQ_CLEAR_FEATURE:
544 recip_handler = &req_clear_feature;
545 break;
Kuninori Morimotoced6e092011-11-24 17:27:50 -0800546 case USB_REQ_SET_FEATURE:
547 recip_handler = &req_set_feature;
548 break;
Kuninori Morimoto17f7f762011-11-24 17:28:04 -0800549 case USB_REQ_GET_STATUS:
550 recip_handler = &req_get_status;
551 break;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900552 }
553 }
554
555 /*
556 * setup stage / run recip
557 */
558 if (recip_handler)
559 ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
560 else
561 ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
562
563 if (ret < 0)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900564 usbhs_pipe_stall(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900565
566 return ret;
567}
568
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900569/*
570 *
571 * usb_dcp_ops
572 *
573 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900574static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
575{
576 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto8a2c2252011-06-06 14:18:33 +0900577 struct usbhs_pkt *pkt;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900578
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900579 while (1) {
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900580 pkt = usbhs_pkt_pop(pipe, NULL);
Kuninori Morimoto8a2c2252011-06-06 14:18:33 +0900581 if (!pkt)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900582 break;
Kuninori Morimoto91b158f2011-11-24 17:28:26 -0800583
584 usbhsg_queue_pop(uep, usbhsg_pkt_to_ureq(pkt), -ECONNRESET);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900585 }
586
Kuninori Morimoto91b158f2011-11-24 17:28:26 -0800587 usbhs_pipe_disable(pipe);
588
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900589 return 0;
590}
591
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900592static void usbhsg_uep_init(struct usbhsg_gpriv *gpriv)
593{
594 int i;
595 struct usbhsg_uep *uep;
596
597 usbhsg_for_each_uep_with_dcp(uep, gpriv, i)
598 uep->pipe = NULL;
599}
600
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900601/*
602 *
603 * usb_ep_ops
604 *
605 */
606static int usbhsg_ep_enable(struct usb_ep *ep,
607 const struct usb_endpoint_descriptor *desc)
608{
609 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
610 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
611 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
612 struct usbhs_pipe *pipe;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900613 int ret = -EIO;
614
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900615 /*
616 * if it already have pipe,
617 * nothing to do
618 */
Kuninori Morimoto08e6c612011-06-09 16:48:25 +0900619 if (uep->pipe) {
620 usbhs_pipe_clear(uep->pipe);
Kuninori Morimoto6e6db822011-10-10 22:05:30 -0700621 usbhs_pipe_sequence_data0(uep->pipe);
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900622 return 0;
Kuninori Morimoto08e6c612011-06-09 16:48:25 +0900623 }
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900624
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700625 pipe = usbhs_pipe_malloc(priv,
626 usb_endpoint_type(desc),
627 usb_endpoint_dir_in(desc));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900628 if (pipe) {
629 uep->pipe = pipe;
630 pipe->mod_private = uep;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900631
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700632 /* set epnum / maxp */
Kuninori Morimotobc6fbf52011-10-10 22:04:00 -0700633 usbhs_pipe_config_update(pipe, 0,
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700634 usb_endpoint_num(desc),
635 usb_endpoint_maxp(desc));
636
Kuninori Morimoto233f5192011-07-07 00:23:24 -0700637 /*
638 * usbhs_fifo_dma_push/pop_handler try to
639 * use dmaengine if possible.
640 * It will use pio handler if impossible.
641 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900642 if (usb_endpoint_dir_in(desc))
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700643 pipe->handler = &usbhs_fifo_dma_push_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900644 else
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700645 pipe->handler = &usbhs_fifo_dma_pop_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900646
647 ret = 0;
648 }
Kuninori Morimotocb966322011-04-21 14:10:08 +0900649
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900650 return ret;
651}
652
653static int usbhsg_ep_disable(struct usb_ep *ep)
654{
655 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900656
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900657 return usbhsg_pipe_disable(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900658}
659
660static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
661 gfp_t gfp_flags)
662{
663 struct usbhsg_request *ureq;
664
665 ureq = kzalloc(sizeof *ureq, gfp_flags);
666 if (!ureq)
667 return NULL;
668
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900669 usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
670
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900671 ureq->req.dma = DMA_ADDR_INVALID;
672
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900673 return &ureq->req;
674}
675
676static void usbhsg_ep_free_request(struct usb_ep *ep,
677 struct usb_request *req)
678{
679 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
680
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900681 WARN_ON(!list_empty(&ureq->pkt.node));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900682 kfree(ureq);
683}
684
685static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
686 gfp_t gfp_flags)
687{
688 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
689 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
690 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
691 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900692
693 /* param check */
694 if (usbhsg_is_not_connected(gpriv) ||
695 unlikely(!gpriv->driver) ||
696 unlikely(!pipe))
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900697 return -ESHUTDOWN;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900698
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900699 usbhsg_queue_push(uep, ureq);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900700
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900701 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900702}
703
704static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
705{
706 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
707 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900708 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900709
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900710 usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900711 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
712
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900713 return 0;
714}
715
716static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
717{
718 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
719 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
720 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900721 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900722 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900723 unsigned long flags;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900724
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900725 usbhsg_pipe_disable(uep);
726
727 dev_dbg(dev, "set halt %d (pipe %d)\n",
728 halt, usbhs_pipe_number(pipe));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900729
730 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900731 usbhs_lock(priv, flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900732
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900733 if (halt)
734 usbhs_pipe_stall(pipe);
735 else
736 usbhs_pipe_disable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900737
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900738 if (halt && wedge)
739 usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
740 else
741 usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900742
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900743 usbhs_unlock(priv, flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900744 /******************** spin unlock ******************/
745
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900746 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900747}
748
749static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
750{
751 return __usbhsg_ep_set_halt_wedge(ep, value, 0);
752}
753
754static int usbhsg_ep_set_wedge(struct usb_ep *ep)
755{
756 return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
757}
758
759static struct usb_ep_ops usbhsg_ep_ops = {
760 .enable = usbhsg_ep_enable,
761 .disable = usbhsg_ep_disable,
762
763 .alloc_request = usbhsg_ep_alloc_request,
764 .free_request = usbhsg_ep_free_request,
765
766 .queue = usbhsg_ep_queue,
767 .dequeue = usbhsg_ep_dequeue,
768
769 .set_halt = usbhsg_ep_set_halt,
770 .set_wedge = usbhsg_ep_set_wedge,
771};
772
773/*
774 * usb module start/end
775 */
776static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
777{
778 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
779 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
780 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
781 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900782 unsigned long flags;
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900783 int ret = 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900784
785 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900786 usbhs_lock(priv, flags);
787
788 usbhsg_status_set(gpriv, status);
789 if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
790 usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
791 ret = -1; /* not ready */
792
793 usbhs_unlock(priv, flags);
794 /******************** spin unlock ********************/
795
796 if (ret < 0)
797 return 0; /* not ready is not error */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900798
799 /*
800 * enable interrupt and systems if ready
801 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900802 dev_dbg(dev, "start gadget\n");
803
804 /*
805 * pipe initialize and enable DCP
806 */
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900807 usbhs_pipe_init(priv,
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900808 usbhsg_dma_map_ctrl);
Kuninori Morimotodad67392011-06-06 14:18:28 +0900809 usbhs_fifo_init(priv);
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900810 usbhsg_uep_init(gpriv);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900811
812 /* dcp init */
813 dcp->pipe = usbhs_dcp_malloc(priv);
814 dcp->pipe->mod_private = dcp;
Kuninori Morimotobc6fbf52011-10-10 22:04:00 -0700815 usbhs_pipe_config_update(dcp->pipe, 0, 0, 64);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900816
817 /*
818 * system config enble
819 * - HI speed
820 * - function
821 * - usb module
822 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900823 usbhs_sys_function_ctrl(priv, 1);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900824
825 /*
826 * enable irq callback
827 */
828 mod->irq_dev_state = usbhsg_irq_dev_state;
829 mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900830 usbhs_irq_callback_update(priv, mod);
831
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900832 return 0;
833}
834
835static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
836{
837 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
838 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
839 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
840 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900841 unsigned long flags;
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900842 int ret = 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900843
844 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900845 usbhs_lock(priv, flags);
846
847 usbhsg_status_clr(gpriv, status);
848 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
849 !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
850 ret = -1; /* already done */
851
852 usbhs_unlock(priv, flags);
853 /******************** spin unlock ********************/
854
855 if (ret < 0)
856 return 0; /* already done is not error */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900857
858 /*
859 * disable interrupt and systems if 1st try
860 */
Kuninori Morimotodad67392011-06-06 14:18:28 +0900861 usbhs_fifo_quit(priv);
862
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900863 /* disable all irq */
864 mod->irq_dev_state = NULL;
865 mod->irq_ctrl_stage = NULL;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900866 usbhs_irq_callback_update(priv, mod);
867
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900868 gpriv->gadget.speed = USB_SPEED_UNKNOWN;
869
870 /* disable sys */
Kuninori Morimotodfbb7f42011-11-24 17:28:35 -0800871 usbhs_sys_set_test_mode(priv, 0);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900872 usbhs_sys_function_ctrl(priv, 0);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900873
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900874 usbhsg_pipe_disable(dcp);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900875
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900876 dev_dbg(dev, "stop gadget\n");
877
878 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900879}
880
881/*
882 *
883 * linux usb function
884 *
885 */
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300886static int usbhsg_gadget_start(struct usb_gadget *gadget,
887 struct usb_gadget_driver *driver)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900888{
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300889 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -0800890 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900891
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300892 if (!driver ||
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900893 !driver->setup ||
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100894 driver->max_speed < USB_SPEED_FULL)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900895 return -EINVAL;
Kuninori Morimoto3b872182011-07-03 17:42:35 -0700896
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900897 /* first hook up the driver ... */
898 gpriv->driver = driver;
899 gpriv->gadget.dev.driver = &driver->driver;
900
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900901 return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900902}
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900903
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300904static int usbhsg_gadget_stop(struct usb_gadget *gadget,
905 struct usb_gadget_driver *driver)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900906{
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300907 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -0800908 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900909
910 if (!driver ||
Kuninori Morimoto3b872182011-07-03 17:42:35 -0700911 !driver->unbind)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900912 return -EINVAL;
913
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900914 usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
Kuninori Morimoto0cdd7d42011-11-17 18:19:56 -0800915 gpriv->gadget.dev.driver = NULL;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900916 gpriv->driver = NULL;
917
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900918 return 0;
919}
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900920
921/*
922 * usb gadget ops
923 */
924static int usbhsg_get_frame(struct usb_gadget *gadget)
925{
926 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
927 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
928
929 return usbhs_frame_get_num(priv);
930}
931
932static struct usb_gadget_ops usbhsg_gadget_ops = {
933 .get_frame = usbhsg_get_frame,
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300934 .udc_start = usbhsg_gadget_start,
935 .udc_stop = usbhsg_gadget_stop,
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900936};
937
938static int usbhsg_start(struct usbhs_priv *priv)
939{
940 return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
941}
942
943static int usbhsg_stop(struct usbhs_priv *priv)
944{
Kuninori Morimoto8885a892011-11-17 18:19:38 -0800945 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
946
947 /* cable disconnect */
948 if (gpriv->driver &&
949 gpriv->driver->disconnect)
950 gpriv->driver->disconnect(&gpriv->gadget);
951
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900952 return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
953}
954
Kuninori Morimotob7a8d172011-10-26 19:33:49 -0700955int usbhs_mod_gadget_probe(struct usbhs_priv *priv)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900956{
957 struct usbhsg_gpriv *gpriv;
958 struct usbhsg_uep *uep;
959 struct device *dev = usbhs_priv_to_dev(priv);
960 int pipe_size = usbhs_get_dparam(priv, pipe_size);
961 int i;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300962 int ret;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900963
964 gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
965 if (!gpriv) {
966 dev_err(dev, "Could not allocate gadget priv\n");
967 return -ENOMEM;
968 }
969
970 uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
971 if (!uep) {
972 dev_err(dev, "Could not allocate ep\n");
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300973 ret = -ENOMEM;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900974 goto usbhs_mod_gadget_probe_err_gpriv;
975 }
976
977 /*
978 * CAUTION
979 *
980 * There is no guarantee that it is possible to access usb module here.
981 * Don't accesses to it.
982 * The accesse will be enable after "usbhsg_start"
983 */
984
985 /*
986 * register itself
987 */
988 usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
989
990 /* init gpriv */
991 gpriv->mod.name = "gadget";
992 gpriv->mod.start = usbhsg_start;
993 gpriv->mod.stop = usbhsg_stop;
994 gpriv->uep = uep;
995 gpriv->uep_size = pipe_size;
996 usbhsg_status_init(gpriv);
997
998 /*
999 * init gadget
1000 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001001 dev_set_name(&gpriv->gadget.dev, "gadget");
1002 gpriv->gadget.dev.parent = dev;
1003 gpriv->gadget.name = "renesas_usbhs_udc";
1004 gpriv->gadget.ops = &usbhsg_gadget_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01001005 gpriv->gadget.max_speed = USB_SPEED_HIGH;
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -08001006 ret = device_register(&gpriv->gadget.dev);
1007 if (ret < 0)
1008 goto err_add_udc;
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001009
1010 INIT_LIST_HEAD(&gpriv->gadget.ep_list);
1011
1012 /*
1013 * init usb_ep
1014 */
1015 usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
1016 uep->gpriv = gpriv;
1017 snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
1018
1019 uep->ep.name = uep->ep_name;
1020 uep->ep.ops = &usbhsg_ep_ops;
1021 INIT_LIST_HEAD(&uep->ep.ep_list);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001022
1023 /* init DCP */
1024 if (usbhsg_is_dcp(uep)) {
1025 gpriv->gadget.ep0 = &uep->ep;
1026 uep->ep.maxpacket = 64;
1027 }
1028 /* init normal pipe */
1029 else {
1030 uep->ep.maxpacket = 512;
1031 list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
1032 }
1033 }
1034
Kuninori Morimoto3b872182011-07-03 17:42:35 -07001035 usbhsg_controller_register(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001036
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001037 ret = usb_add_gadget_udc(dev, &gpriv->gadget);
1038 if (ret)
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -08001039 goto err_register;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001040
1041
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001042 dev_info(dev, "gadget probed\n");
1043
1044 return 0;
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -08001045
1046err_register:
1047 device_unregister(&gpriv->gadget.dev);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001048err_add_udc:
1049 kfree(gpriv->uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001050
1051usbhs_mod_gadget_probe_err_gpriv:
1052 kfree(gpriv);
1053
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001054 return ret;
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001055}
1056
Kuninori Morimotob7a8d172011-10-26 19:33:49 -07001057void usbhs_mod_gadget_remove(struct usbhs_priv *priv)
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001058{
1059 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1060
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001061 usb_del_gadget_udc(&gpriv->gadget);
Kuninori Morimoto3b872182011-07-03 17:42:35 -07001062
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -08001063 device_unregister(&gpriv->gadget.dev);
1064
Kuninori Morimoto3b872182011-07-03 17:42:35 -07001065 usbhsg_controller_unregister(gpriv);
1066
Sebastian Andrzej Siewior3af51ac2011-06-03 19:50:48 +02001067 kfree(gpriv->uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001068 kfree(gpriv);
1069}