blob: 7542aa94a4622421219237598c8db20ff1f46a87 [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;
48
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
117/*
Kuninori Morimoto233f5192011-07-07 00:23:24 -0700118 * queue push/pop
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900119 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900120static void usbhsg_queue_pop(struct usbhsg_uep *uep,
121 struct usbhsg_request *ureq,
122 int status)
123{
124 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
125 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
126 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900127
128 dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
129
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900130 ureq->req.status = status;
131 ureq->req.complete(&uep->ep, &ureq->req);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900132}
133
Kuninori Morimoto2cc97192011-10-10 22:03:39 -0700134static void usbhsg_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900135{
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900136 struct usbhs_pipe *pipe = pkt->pipe;
137 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
138 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900139
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900140 ureq->req.actual = pkt->actual;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900141
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900142 usbhsg_queue_pop(uep, ureq, 0);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900143}
144
Kuninori Morimotob3318722011-10-10 22:04:41 -0700145static void usbhsg_queue_push(struct usbhsg_uep *uep,
146 struct usbhsg_request *ureq)
147{
148 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
149 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
150 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
151 struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
152 struct usb_request *req = &ureq->req;
153
154 req->actual = 0;
155 req->status = -EINPROGRESS;
156 usbhs_pkt_push(pipe, pkt, usbhsg_queue_done,
Kuninori Morimoto3edeee32011-12-08 18:28:54 -0800157 req->buf, req->length, req->zero, -1);
Kuninori Morimoto654c35a2011-10-10 22:04:53 -0700158 usbhs_pkt_start(pipe);
Kuninori Morimotob3318722011-10-10 22:04:41 -0700159
160 dev_dbg(dev, "pipe %d : queue push (%d)\n",
161 usbhs_pipe_number(pipe),
162 req->length);
163}
164
Kuninori Morimoto233f5192011-07-07 00:23:24 -0700165/*
166 * dma map/unmap
167 */
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900168static int usbhsg_dma_map(struct device *dev,
169 struct usbhs_pkt *pkt,
170 enum dma_data_direction dir)
171{
172 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
173 struct usb_request *req = &ureq->req;
174
175 if (pkt->dma != DMA_ADDR_INVALID) {
176 dev_err(dev, "dma is already mapped\n");
177 return -EIO;
178 }
179
180 if (req->dma == DMA_ADDR_INVALID) {
181 pkt->dma = dma_map_single(dev, pkt->buf, pkt->length, dir);
182 } else {
183 dma_sync_single_for_device(dev, req->dma, req->length, dir);
184 pkt->dma = req->dma;
185 }
186
187 if (dma_mapping_error(dev, pkt->dma)) {
Dan Carpenter84181532011-12-15 14:31:37 +0300188 dev_err(dev, "dma mapping error %llx\n", (u64)pkt->dma);
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900189 return -EIO;
190 }
191
192 return 0;
193}
194
195static int usbhsg_dma_unmap(struct device *dev,
196 struct usbhs_pkt *pkt,
197 enum dma_data_direction dir)
198{
199 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
200 struct usb_request *req = &ureq->req;
201
202 if (pkt->dma == DMA_ADDR_INVALID) {
203 dev_err(dev, "dma is not mapped\n");
204 return -EIO;
205 }
206
207 if (req->dma == DMA_ADDR_INVALID)
208 dma_unmap_single(dev, pkt->dma, pkt->length, dir);
209 else
210 dma_sync_single_for_cpu(dev, req->dma, req->length, dir);
211
212 pkt->dma = DMA_ADDR_INVALID;
213
214 return 0;
215}
216
217static int usbhsg_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
218{
219 struct usbhs_pipe *pipe = pkt->pipe;
220 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
221 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
222 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
223 enum dma_data_direction dir;
224
225 dir = usbhs_pipe_is_dir_in(pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
226
227 if (map)
228 return usbhsg_dma_map(dev, pkt, dir);
229 else
230 return usbhsg_dma_unmap(dev, pkt, dir);
231}
232
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900233/*
234 * USB_TYPE_STANDARD / clear feature functions
235 */
236static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
237 struct usbhsg_uep *uep,
238 struct usb_ctrlrequest *ctrl)
239{
240 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
241 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
242 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
243
244 usbhs_dcp_control_transfer_done(pipe);
245
246 return 0;
247}
248
249static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
250 struct usbhsg_uep *uep,
251 struct usb_ctrlrequest *ctrl)
252{
253 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
254 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
255
256 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900257 usbhs_pipe_disable(pipe);
Kuninori Morimoto6e6db822011-10-10 22:05:30 -0700258 usbhs_pipe_sequence_data0(pipe);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900259 usbhs_pipe_enable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900260 }
261
262 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
263
Kuninori Morimoto25fa7072011-11-24 17:28:17 -0800264 usbhs_pkt_start(pipe);
265
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900266 return 0;
267}
268
269struct usbhsg_recip_handle req_clear_feature = {
270 .name = "clear feature",
271 .device = usbhsg_recip_handler_std_control_done,
272 .interface = usbhsg_recip_handler_std_control_done,
273 .endpoint = usbhsg_recip_handler_std_clear_endpoint,
274};
275
276/*
Kuninori Morimotoced6e092011-11-24 17:27:50 -0800277 * USB_TYPE_STANDARD / set feature functions
278 */
Kuninori Morimotodfbb7f42011-11-24 17:28:35 -0800279static int usbhsg_recip_handler_std_set_device(struct usbhs_priv *priv,
280 struct usbhsg_uep *uep,
281 struct usb_ctrlrequest *ctrl)
282{
283 switch (le16_to_cpu(ctrl->wValue)) {
284 case USB_DEVICE_TEST_MODE:
285 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
286 udelay(100);
287 usbhs_sys_set_test_mode(priv, le16_to_cpu(ctrl->wIndex >> 8));
288 break;
289 default:
290 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
291 break;
292 }
293
294 return 0;
295}
296
Kuninori Morimotoced6e092011-11-24 17:27:50 -0800297static int usbhsg_recip_handler_std_set_endpoint(struct usbhs_priv *priv,
298 struct usbhsg_uep *uep,
299 struct usb_ctrlrequest *ctrl)
300{
301 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
302
303 usbhs_pipe_stall(pipe);
304
305 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
306
307 return 0;
308}
309
310struct usbhsg_recip_handle req_set_feature = {
311 .name = "set feature",
Kuninori Morimotodfbb7f42011-11-24 17:28:35 -0800312 .device = usbhsg_recip_handler_std_set_device,
Kuninori Morimotoced6e092011-11-24 17:27:50 -0800313 .interface = usbhsg_recip_handler_std_control_done,
314 .endpoint = usbhsg_recip_handler_std_set_endpoint,
315};
316
317/*
Kuninori Morimoto17f7f762011-11-24 17:28:04 -0800318 * USB_TYPE_STANDARD / get status functions
319 */
320static void __usbhsg_recip_send_complete(struct usb_ep *ep,
321 struct usb_request *req)
322{
323 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
324
325 /* free allocated recip-buffer/usb_request */
326 kfree(ureq->pkt.buf);
327 usb_ep_free_request(ep, req);
328}
329
330static void __usbhsg_recip_send_status(struct usbhsg_gpriv *gpriv,
331 unsigned short status)
332{
333 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
334 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
335 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
336 struct usb_request *req;
337 unsigned short *buf;
338
339 /* alloc new usb_request for recip */
340 req = usb_ep_alloc_request(&dcp->ep, GFP_ATOMIC);
341 if (!req) {
342 dev_err(dev, "recip request allocation fail\n");
343 return;
344 }
345
346 /* alloc recip data buffer */
347 buf = kmalloc(sizeof(*buf), GFP_ATOMIC);
348 if (!buf) {
349 usb_ep_free_request(&dcp->ep, req);
350 dev_err(dev, "recip data allocation fail\n");
351 return;
352 }
353
354 /* recip data is status */
355 *buf = cpu_to_le16(status);
356
357 /* allocated usb_request/buffer will be freed */
358 req->complete = __usbhsg_recip_send_complete;
359 req->buf = buf;
360 req->length = sizeof(*buf);
361 req->zero = 0;
362
363 /* push packet */
364 pipe->handler = &usbhs_fifo_pio_push_handler;
365 usbhsg_queue_push(dcp, usbhsg_req_to_ureq(req));
366}
367
368static int usbhsg_recip_handler_std_get_device(struct usbhs_priv *priv,
369 struct usbhsg_uep *uep,
370 struct usb_ctrlrequest *ctrl)
371{
372 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
373 unsigned short status = 1 << USB_DEVICE_SELF_POWERED;
374
375 __usbhsg_recip_send_status(gpriv, status);
376
377 return 0;
378}
379
380static int usbhsg_recip_handler_std_get_interface(struct usbhs_priv *priv,
381 struct usbhsg_uep *uep,
382 struct usb_ctrlrequest *ctrl)
383{
384 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
385 unsigned short status = 0;
386
387 __usbhsg_recip_send_status(gpriv, status);
388
389 return 0;
390}
391
392static int usbhsg_recip_handler_std_get_endpoint(struct usbhs_priv *priv,
393 struct usbhsg_uep *uep,
394 struct usb_ctrlrequest *ctrl)
395{
396 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
397 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
398 unsigned short status = 0;
399
400 if (usbhs_pipe_is_stall(pipe))
401 status = 1 << USB_ENDPOINT_HALT;
402
403 __usbhsg_recip_send_status(gpriv, status);
404
405 return 0;
406}
407
408struct usbhsg_recip_handle req_get_status = {
409 .name = "get status",
410 .device = usbhsg_recip_handler_std_get_device,
411 .interface = usbhsg_recip_handler_std_get_interface,
412 .endpoint = usbhsg_recip_handler_std_get_endpoint,
413};
414
415/*
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900416 * USB_TYPE handler
417 */
418static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
419 struct usbhsg_recip_handle *handler,
420 struct usb_ctrlrequest *ctrl)
421{
422 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
423 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
424 struct usbhsg_uep *uep;
Kuninori Morimoto0432eed2011-06-06 14:18:54 +0900425 struct usbhs_pipe *pipe;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900426 int recip = ctrl->bRequestType & USB_RECIP_MASK;
427 int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
Jesper Juhl7983bc72012-01-16 22:42:10 +0100428 int ret = 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900429 int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
430 struct usb_ctrlrequest *ctrl);
431 char *msg;
432
433 uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
Kuninori Morimoto0432eed2011-06-06 14:18:54 +0900434 pipe = usbhsg_uep_to_pipe(uep);
435 if (!pipe) {
Kuninori Morimoto9a28b7b2011-04-21 14:10:12 +0900436 dev_err(dev, "wrong recip request\n");
Kuninori Morimoto25fa7072011-11-24 17:28:17 -0800437 return -EINVAL;
Kuninori Morimoto9a28b7b2011-04-21 14:10:12 +0900438 }
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900439
440 switch (recip) {
441 case USB_RECIP_DEVICE:
442 msg = "DEVICE";
443 func = handler->device;
444 break;
445 case USB_RECIP_INTERFACE:
446 msg = "INTERFACE";
447 func = handler->interface;
448 break;
449 case USB_RECIP_ENDPOINT:
450 msg = "ENDPOINT";
451 func = handler->endpoint;
452 break;
453 default:
454 dev_warn(dev, "unsupported RECIP(%d)\n", recip);
455 func = NULL;
456 ret = -EINVAL;
457 }
458
459 if (func) {
460 dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
461 ret = func(priv, uep, ctrl);
462 }
463
464 return ret;
465}
466
467/*
468 * irq functions
469 *
470 * it will be called from usbhs_interrupt
471 */
472static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
473 struct usbhs_irq_state *irq_state)
474{
475 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
476 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
477
Kuninori Morimoto75587f52011-10-10 22:01:51 -0700478 gpriv->gadget.speed = usbhs_bus_get_speed(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900479
480 dev_dbg(dev, "state = %x : speed : %d\n",
481 usbhs_status_get_device_state(irq_state),
482 gpriv->gadget.speed);
483
484 return 0;
485}
486
487static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
488 struct usbhs_irq_state *irq_state)
489{
490 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
491 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
492 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
493 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
494 struct usb_ctrlrequest ctrl;
495 struct usbhsg_recip_handle *recip_handler = NULL;
496 int stage = usbhs_status_get_ctrl_stage(irq_state);
497 int ret = 0;
498
499 dev_dbg(dev, "stage = %d\n", stage);
500
501 /*
502 * see Manual
503 *
504 * "Operation"
505 * - "Interrupt Function"
506 * - "Control Transfer Stage Transition Interrupt"
507 * - Fig. "Control Transfer Stage Transitions"
508 */
509
510 switch (stage) {
511 case READ_DATA_STAGE:
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700512 pipe->handler = &usbhs_fifo_pio_push_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900513 break;
514 case WRITE_DATA_STAGE:
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700515 pipe->handler = &usbhs_fifo_pio_pop_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900516 break;
517 case NODATA_STATUS_STAGE:
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700518 pipe->handler = &usbhs_ctrl_stage_end_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900519 break;
520 default:
521 return ret;
522 }
523
524 /*
525 * get usb request
526 */
527 usbhs_usbreq_get_val(priv, &ctrl);
528
529 switch (ctrl.bRequestType & USB_TYPE_MASK) {
530 case USB_TYPE_STANDARD:
531 switch (ctrl.bRequest) {
532 case USB_REQ_CLEAR_FEATURE:
533 recip_handler = &req_clear_feature;
534 break;
Kuninori Morimotoced6e092011-11-24 17:27:50 -0800535 case USB_REQ_SET_FEATURE:
536 recip_handler = &req_set_feature;
537 break;
Kuninori Morimoto17f7f762011-11-24 17:28:04 -0800538 case USB_REQ_GET_STATUS:
539 recip_handler = &req_get_status;
540 break;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900541 }
542 }
543
544 /*
545 * setup stage / run recip
546 */
547 if (recip_handler)
548 ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
549 else
550 ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
551
552 if (ret < 0)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900553 usbhs_pipe_stall(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900554
555 return ret;
556}
557
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900558/*
559 *
560 * usb_dcp_ops
561 *
562 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900563static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
564{
565 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto8a2c2252011-06-06 14:18:33 +0900566 struct usbhs_pkt *pkt;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900567
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900568 while (1) {
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900569 pkt = usbhs_pkt_pop(pipe, NULL);
Kuninori Morimoto8a2c2252011-06-06 14:18:33 +0900570 if (!pkt)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900571 break;
Kuninori Morimoto91b158f2011-11-24 17:28:26 -0800572
573 usbhsg_queue_pop(uep, usbhsg_pkt_to_ureq(pkt), -ECONNRESET);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900574 }
575
Kuninori Morimoto91b158f2011-11-24 17:28:26 -0800576 usbhs_pipe_disable(pipe);
577
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900578 return 0;
579}
580
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900581static void usbhsg_uep_init(struct usbhsg_gpriv *gpriv)
582{
583 int i;
584 struct usbhsg_uep *uep;
585
586 usbhsg_for_each_uep_with_dcp(uep, gpriv, i)
587 uep->pipe = NULL;
588}
589
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900590/*
591 *
592 * usb_ep_ops
593 *
594 */
595static int usbhsg_ep_enable(struct usb_ep *ep,
596 const struct usb_endpoint_descriptor *desc)
597{
598 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
599 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
600 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
601 struct usbhs_pipe *pipe;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900602 int ret = -EIO;
603
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900604 /*
605 * if it already have pipe,
606 * nothing to do
607 */
Kuninori Morimoto08e6c612011-06-09 16:48:25 +0900608 if (uep->pipe) {
609 usbhs_pipe_clear(uep->pipe);
Kuninori Morimoto6e6db822011-10-10 22:05:30 -0700610 usbhs_pipe_sequence_data0(uep->pipe);
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900611 return 0;
Kuninori Morimoto08e6c612011-06-09 16:48:25 +0900612 }
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900613
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700614 pipe = usbhs_pipe_malloc(priv,
615 usb_endpoint_type(desc),
616 usb_endpoint_dir_in(desc));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900617 if (pipe) {
618 uep->pipe = pipe;
619 pipe->mod_private = uep;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900620
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700621 /* set epnum / maxp */
Kuninori Morimotobc6fbf52011-10-10 22:04:00 -0700622 usbhs_pipe_config_update(pipe, 0,
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700623 usb_endpoint_num(desc),
624 usb_endpoint_maxp(desc));
625
Kuninori Morimoto233f5192011-07-07 00:23:24 -0700626 /*
627 * usbhs_fifo_dma_push/pop_handler try to
628 * use dmaengine if possible.
629 * It will use pio handler if impossible.
630 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900631 if (usb_endpoint_dir_in(desc))
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700632 pipe->handler = &usbhs_fifo_dma_push_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900633 else
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700634 pipe->handler = &usbhs_fifo_dma_pop_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900635
636 ret = 0;
637 }
Kuninori Morimotocb966322011-04-21 14:10:08 +0900638
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900639 return ret;
640}
641
642static int usbhsg_ep_disable(struct usb_ep *ep)
643{
644 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900645
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900646 return usbhsg_pipe_disable(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900647}
648
649static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
650 gfp_t gfp_flags)
651{
652 struct usbhsg_request *ureq;
653
654 ureq = kzalloc(sizeof *ureq, gfp_flags);
655 if (!ureq)
656 return NULL;
657
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900658 usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
659
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900660 ureq->req.dma = DMA_ADDR_INVALID;
661
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900662 return &ureq->req;
663}
664
665static void usbhsg_ep_free_request(struct usb_ep *ep,
666 struct usb_request *req)
667{
668 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
669
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900670 WARN_ON(!list_empty(&ureq->pkt.node));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900671 kfree(ureq);
672}
673
674static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
675 gfp_t gfp_flags)
676{
677 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
678 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
679 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
680 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900681
682 /* param check */
683 if (usbhsg_is_not_connected(gpriv) ||
684 unlikely(!gpriv->driver) ||
685 unlikely(!pipe))
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900686 return -ESHUTDOWN;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900687
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900688 usbhsg_queue_push(uep, ureq);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900689
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900690 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900691}
692
693static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
694{
695 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
696 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900697 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900698
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900699 usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900700 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
701
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900702 return 0;
703}
704
705static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
706{
707 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
708 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
709 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900710 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900711 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900712 unsigned long flags;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900713
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900714 usbhsg_pipe_disable(uep);
715
716 dev_dbg(dev, "set halt %d (pipe %d)\n",
717 halt, usbhs_pipe_number(pipe));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900718
719 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900720 usbhs_lock(priv, flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900721
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900722 if (halt)
723 usbhs_pipe_stall(pipe);
724 else
725 usbhs_pipe_disable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900726
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900727 if (halt && wedge)
728 usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
729 else
730 usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900731
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900732 usbhs_unlock(priv, flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900733 /******************** spin unlock ******************/
734
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900735 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900736}
737
738static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
739{
740 return __usbhsg_ep_set_halt_wedge(ep, value, 0);
741}
742
743static int usbhsg_ep_set_wedge(struct usb_ep *ep)
744{
745 return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
746}
747
748static struct usb_ep_ops usbhsg_ep_ops = {
749 .enable = usbhsg_ep_enable,
750 .disable = usbhsg_ep_disable,
751
752 .alloc_request = usbhsg_ep_alloc_request,
753 .free_request = usbhsg_ep_free_request,
754
755 .queue = usbhsg_ep_queue,
756 .dequeue = usbhsg_ep_dequeue,
757
758 .set_halt = usbhsg_ep_set_halt,
759 .set_wedge = usbhsg_ep_set_wedge,
760};
761
762/*
763 * usb module start/end
764 */
765static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
766{
767 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
768 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
769 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
770 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900771 unsigned long flags;
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900772 int ret = 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900773
774 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900775 usbhs_lock(priv, flags);
776
777 usbhsg_status_set(gpriv, status);
778 if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
779 usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
780 ret = -1; /* not ready */
781
782 usbhs_unlock(priv, flags);
783 /******************** spin unlock ********************/
784
785 if (ret < 0)
786 return 0; /* not ready is not error */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900787
788 /*
789 * enable interrupt and systems if ready
790 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900791 dev_dbg(dev, "start gadget\n");
792
793 /*
794 * pipe initialize and enable DCP
795 */
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900796 usbhs_pipe_init(priv,
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900797 usbhsg_dma_map_ctrl);
Kuninori Morimotodad67392011-06-06 14:18:28 +0900798 usbhs_fifo_init(priv);
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900799 usbhsg_uep_init(gpriv);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900800
801 /* dcp init */
802 dcp->pipe = usbhs_dcp_malloc(priv);
803 dcp->pipe->mod_private = dcp;
Kuninori Morimotobc6fbf52011-10-10 22:04:00 -0700804 usbhs_pipe_config_update(dcp->pipe, 0, 0, 64);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900805
806 /*
807 * system config enble
808 * - HI speed
809 * - function
810 * - usb module
811 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900812 usbhs_sys_function_ctrl(priv, 1);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900813
814 /*
815 * enable irq callback
816 */
817 mod->irq_dev_state = usbhsg_irq_dev_state;
818 mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900819 usbhs_irq_callback_update(priv, mod);
820
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900821 return 0;
822}
823
824static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
825{
826 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
827 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
828 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
829 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900830 unsigned long flags;
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900831 int ret = 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900832
833 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900834 usbhs_lock(priv, flags);
835
836 usbhsg_status_clr(gpriv, status);
837 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
838 !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
839 ret = -1; /* already done */
840
841 usbhs_unlock(priv, flags);
842 /******************** spin unlock ********************/
843
844 if (ret < 0)
845 return 0; /* already done is not error */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900846
847 /*
848 * disable interrupt and systems if 1st try
849 */
Kuninori Morimotodad67392011-06-06 14:18:28 +0900850 usbhs_fifo_quit(priv);
851
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900852 /* disable all irq */
853 mod->irq_dev_state = NULL;
854 mod->irq_ctrl_stage = NULL;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900855 usbhs_irq_callback_update(priv, mod);
856
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900857 gpriv->gadget.speed = USB_SPEED_UNKNOWN;
858
859 /* disable sys */
Kuninori Morimotodfbb7f42011-11-24 17:28:35 -0800860 usbhs_sys_set_test_mode(priv, 0);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900861 usbhs_sys_function_ctrl(priv, 0);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900862
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900863 usbhsg_pipe_disable(dcp);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900864
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900865 dev_dbg(dev, "stop gadget\n");
866
867 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900868}
869
870/*
871 *
872 * linux usb function
873 *
874 */
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300875static int usbhsg_gadget_start(struct usb_gadget *gadget,
876 struct usb_gadget_driver *driver)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900877{
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300878 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -0800879 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900880
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300881 if (!driver ||
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900882 !driver->setup ||
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100883 driver->max_speed < USB_SPEED_FULL)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900884 return -EINVAL;
Kuninori Morimoto3b872182011-07-03 17:42:35 -0700885
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900886 /* first hook up the driver ... */
887 gpriv->driver = driver;
888 gpriv->gadget.dev.driver = &driver->driver;
889
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900890 return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900891}
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900892
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300893static int usbhsg_gadget_stop(struct usb_gadget *gadget,
894 struct usb_gadget_driver *driver)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900895{
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300896 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -0800897 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900898
899 if (!driver ||
Kuninori Morimoto3b872182011-07-03 17:42:35 -0700900 !driver->unbind)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900901 return -EINVAL;
902
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900903 usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
Kuninori Morimoto0cdd7d42011-11-17 18:19:56 -0800904 gpriv->gadget.dev.driver = NULL;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900905 gpriv->driver = NULL;
906
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900907 return 0;
908}
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900909
910/*
911 * usb gadget ops
912 */
913static int usbhsg_get_frame(struct usb_gadget *gadget)
914{
915 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
916 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
917
918 return usbhs_frame_get_num(priv);
919}
920
921static struct usb_gadget_ops usbhsg_gadget_ops = {
922 .get_frame = usbhsg_get_frame,
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300923 .udc_start = usbhsg_gadget_start,
924 .udc_stop = usbhsg_gadget_stop,
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900925};
926
927static int usbhsg_start(struct usbhs_priv *priv)
928{
929 return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
930}
931
932static int usbhsg_stop(struct usbhs_priv *priv)
933{
Kuninori Morimoto8885a892011-11-17 18:19:38 -0800934 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
935
936 /* cable disconnect */
937 if (gpriv->driver &&
938 gpriv->driver->disconnect)
939 gpriv->driver->disconnect(&gpriv->gadget);
940
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900941 return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
942}
943
Kuninori Morimotob7a8d172011-10-26 19:33:49 -0700944int usbhs_mod_gadget_probe(struct usbhs_priv *priv)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900945{
946 struct usbhsg_gpriv *gpriv;
947 struct usbhsg_uep *uep;
948 struct device *dev = usbhs_priv_to_dev(priv);
949 int pipe_size = usbhs_get_dparam(priv, pipe_size);
950 int i;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300951 int ret;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900952
953 gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
954 if (!gpriv) {
955 dev_err(dev, "Could not allocate gadget priv\n");
956 return -ENOMEM;
957 }
958
959 uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
960 if (!uep) {
961 dev_err(dev, "Could not allocate ep\n");
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300962 ret = -ENOMEM;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900963 goto usbhs_mod_gadget_probe_err_gpriv;
964 }
965
966 /*
967 * CAUTION
968 *
969 * There is no guarantee that it is possible to access usb module here.
970 * Don't accesses to it.
971 * The accesse will be enable after "usbhsg_start"
972 */
973
974 /*
975 * register itself
976 */
977 usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
978
979 /* init gpriv */
980 gpriv->mod.name = "gadget";
981 gpriv->mod.start = usbhsg_start;
982 gpriv->mod.stop = usbhsg_stop;
983 gpriv->uep = uep;
984 gpriv->uep_size = pipe_size;
985 usbhsg_status_init(gpriv);
986
987 /*
988 * init gadget
989 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900990 dev_set_name(&gpriv->gadget.dev, "gadget");
991 gpriv->gadget.dev.parent = dev;
992 gpriv->gadget.name = "renesas_usbhs_udc";
993 gpriv->gadget.ops = &usbhsg_gadget_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100994 gpriv->gadget.max_speed = USB_SPEED_HIGH;
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -0800995 ret = device_register(&gpriv->gadget.dev);
996 if (ret < 0)
997 goto err_add_udc;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900998
999 INIT_LIST_HEAD(&gpriv->gadget.ep_list);
1000
1001 /*
1002 * init usb_ep
1003 */
1004 usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
1005 uep->gpriv = gpriv;
1006 snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
1007
1008 uep->ep.name = uep->ep_name;
1009 uep->ep.ops = &usbhsg_ep_ops;
1010 INIT_LIST_HEAD(&uep->ep.ep_list);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001011
1012 /* init DCP */
1013 if (usbhsg_is_dcp(uep)) {
1014 gpriv->gadget.ep0 = &uep->ep;
1015 uep->ep.maxpacket = 64;
1016 }
1017 /* init normal pipe */
1018 else {
1019 uep->ep.maxpacket = 512;
1020 list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
1021 }
1022 }
1023
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001024 ret = usb_add_gadget_udc(dev, &gpriv->gadget);
1025 if (ret)
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -08001026 goto err_register;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001027
1028
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001029 dev_info(dev, "gadget probed\n");
1030
1031 return 0;
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -08001032
1033err_register:
1034 device_unregister(&gpriv->gadget.dev);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001035err_add_udc:
1036 kfree(gpriv->uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001037
1038usbhs_mod_gadget_probe_err_gpriv:
1039 kfree(gpriv);
1040
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001041 return ret;
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001042}
1043
Kuninori Morimotob7a8d172011-10-26 19:33:49 -07001044void usbhs_mod_gadget_remove(struct usbhs_priv *priv)
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001045{
1046 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1047
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001048 usb_del_gadget_udc(&gpriv->gadget);
Kuninori Morimoto3b872182011-07-03 17:42:35 -07001049
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -08001050 device_unregister(&gpriv->gadget.dev);
1051
Sebastian Andrzej Siewior3af51ac2011-06-03 19:50:48 +02001052 kfree(gpriv->uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001053 kfree(gpriv);
1054}