blob: 937f2d40c747a9250fe991eb4ec5548e35c1ec6b [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_ctrl(struct usbhs_pkt *pkt, int map)
169{
Felipe Balbiade78f92011-12-19 11:57:16 +0200170 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
171 struct usb_request *req = &ureq->req;
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900172 struct usbhs_pipe *pipe = pkt->pipe;
173 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
174 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900175 enum dma_data_direction dir;
Felipe Balbiade78f92011-12-19 11:57:16 +0200176 int ret = 0;
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900177
Felipe Balbiade78f92011-12-19 11:57:16 +0200178 dir = usbhs_pipe_is_dir_host(pipe);
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900179
Felipe Balbiade78f92011-12-19 11:57:16 +0200180 if (map) {
181 /* it can not use scatter/gather */
182 WARN_ON(req->num_sgs);
183
184 ret = usb_gadget_map_request(&gpriv->gadget, req, dir);
185 if (ret < 0)
186 return ret;
187
188 pkt->dma = req->dma;
189 } else {
190 usb_gadget_unmap_request(&gpriv->gadget, req, dir);
191 }
192
193 return ret;
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900194}
195
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900196/*
197 * USB_TYPE_STANDARD / clear feature functions
198 */
199static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
200 struct usbhsg_uep *uep,
201 struct usb_ctrlrequest *ctrl)
202{
203 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
204 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
205 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
206
207 usbhs_dcp_control_transfer_done(pipe);
208
209 return 0;
210}
211
212static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
213 struct usbhsg_uep *uep,
214 struct usb_ctrlrequest *ctrl)
215{
216 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
217 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
218
219 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900220 usbhs_pipe_disable(pipe);
Kuninori Morimoto6e6db822011-10-10 22:05:30 -0700221 usbhs_pipe_sequence_data0(pipe);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900222 usbhs_pipe_enable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900223 }
224
225 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
226
Kuninori Morimoto25fa7072011-11-24 17:28:17 -0800227 usbhs_pkt_start(pipe);
228
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900229 return 0;
230}
231
232struct usbhsg_recip_handle req_clear_feature = {
233 .name = "clear feature",
234 .device = usbhsg_recip_handler_std_control_done,
235 .interface = usbhsg_recip_handler_std_control_done,
236 .endpoint = usbhsg_recip_handler_std_clear_endpoint,
237};
238
239/*
Kuninori Morimotoced6e092011-11-24 17:27:50 -0800240 * USB_TYPE_STANDARD / set feature functions
241 */
Kuninori Morimotodfbb7f42011-11-24 17:28:35 -0800242static int usbhsg_recip_handler_std_set_device(struct usbhs_priv *priv,
243 struct usbhsg_uep *uep,
244 struct usb_ctrlrequest *ctrl)
245{
246 switch (le16_to_cpu(ctrl->wValue)) {
247 case USB_DEVICE_TEST_MODE:
248 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
249 udelay(100);
250 usbhs_sys_set_test_mode(priv, le16_to_cpu(ctrl->wIndex >> 8));
251 break;
252 default:
253 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
254 break;
255 }
256
257 return 0;
258}
259
Kuninori Morimotoced6e092011-11-24 17:27:50 -0800260static int usbhsg_recip_handler_std_set_endpoint(struct usbhs_priv *priv,
261 struct usbhsg_uep *uep,
262 struct usb_ctrlrequest *ctrl)
263{
264 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
265
266 usbhs_pipe_stall(pipe);
267
268 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
269
270 return 0;
271}
272
273struct usbhsg_recip_handle req_set_feature = {
274 .name = "set feature",
Kuninori Morimotodfbb7f42011-11-24 17:28:35 -0800275 .device = usbhsg_recip_handler_std_set_device,
Kuninori Morimotoced6e092011-11-24 17:27:50 -0800276 .interface = usbhsg_recip_handler_std_control_done,
277 .endpoint = usbhsg_recip_handler_std_set_endpoint,
278};
279
280/*
Kuninori Morimoto17f7f762011-11-24 17:28:04 -0800281 * USB_TYPE_STANDARD / get status functions
282 */
283static void __usbhsg_recip_send_complete(struct usb_ep *ep,
284 struct usb_request *req)
285{
286 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
287
288 /* free allocated recip-buffer/usb_request */
289 kfree(ureq->pkt.buf);
290 usb_ep_free_request(ep, req);
291}
292
293static void __usbhsg_recip_send_status(struct usbhsg_gpriv *gpriv,
294 unsigned short status)
295{
296 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
297 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
298 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
299 struct usb_request *req;
300 unsigned short *buf;
301
302 /* alloc new usb_request for recip */
303 req = usb_ep_alloc_request(&dcp->ep, GFP_ATOMIC);
304 if (!req) {
305 dev_err(dev, "recip request allocation fail\n");
306 return;
307 }
308
309 /* alloc recip data buffer */
310 buf = kmalloc(sizeof(*buf), GFP_ATOMIC);
311 if (!buf) {
312 usb_ep_free_request(&dcp->ep, req);
313 dev_err(dev, "recip data allocation fail\n");
314 return;
315 }
316
317 /* recip data is status */
318 *buf = cpu_to_le16(status);
319
320 /* allocated usb_request/buffer will be freed */
321 req->complete = __usbhsg_recip_send_complete;
322 req->buf = buf;
323 req->length = sizeof(*buf);
324 req->zero = 0;
325
326 /* push packet */
327 pipe->handler = &usbhs_fifo_pio_push_handler;
328 usbhsg_queue_push(dcp, usbhsg_req_to_ureq(req));
329}
330
331static int usbhsg_recip_handler_std_get_device(struct usbhs_priv *priv,
332 struct usbhsg_uep *uep,
333 struct usb_ctrlrequest *ctrl)
334{
335 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
336 unsigned short status = 1 << USB_DEVICE_SELF_POWERED;
337
338 __usbhsg_recip_send_status(gpriv, status);
339
340 return 0;
341}
342
343static int usbhsg_recip_handler_std_get_interface(struct usbhs_priv *priv,
344 struct usbhsg_uep *uep,
345 struct usb_ctrlrequest *ctrl)
346{
347 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
348 unsigned short status = 0;
349
350 __usbhsg_recip_send_status(gpriv, status);
351
352 return 0;
353}
354
355static int usbhsg_recip_handler_std_get_endpoint(struct usbhs_priv *priv,
356 struct usbhsg_uep *uep,
357 struct usb_ctrlrequest *ctrl)
358{
359 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
360 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
361 unsigned short status = 0;
362
363 if (usbhs_pipe_is_stall(pipe))
364 status = 1 << USB_ENDPOINT_HALT;
365
366 __usbhsg_recip_send_status(gpriv, status);
367
368 return 0;
369}
370
371struct usbhsg_recip_handle req_get_status = {
372 .name = "get status",
373 .device = usbhsg_recip_handler_std_get_device,
374 .interface = usbhsg_recip_handler_std_get_interface,
375 .endpoint = usbhsg_recip_handler_std_get_endpoint,
376};
377
378/*
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900379 * USB_TYPE handler
380 */
381static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
382 struct usbhsg_recip_handle *handler,
383 struct usb_ctrlrequest *ctrl)
384{
385 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
386 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
387 struct usbhsg_uep *uep;
Kuninori Morimoto0432eed2011-06-06 14:18:54 +0900388 struct usbhs_pipe *pipe;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900389 int recip = ctrl->bRequestType & USB_RECIP_MASK;
390 int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
391 int ret;
392 int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
393 struct usb_ctrlrequest *ctrl);
394 char *msg;
395
396 uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
Kuninori Morimoto0432eed2011-06-06 14:18:54 +0900397 pipe = usbhsg_uep_to_pipe(uep);
398 if (!pipe) {
Kuninori Morimoto9a28b7b2011-04-21 14:10:12 +0900399 dev_err(dev, "wrong recip request\n");
Kuninori Morimoto25fa7072011-11-24 17:28:17 -0800400 return -EINVAL;
Kuninori Morimoto9a28b7b2011-04-21 14:10:12 +0900401 }
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900402
403 switch (recip) {
404 case USB_RECIP_DEVICE:
405 msg = "DEVICE";
406 func = handler->device;
407 break;
408 case USB_RECIP_INTERFACE:
409 msg = "INTERFACE";
410 func = handler->interface;
411 break;
412 case USB_RECIP_ENDPOINT:
413 msg = "ENDPOINT";
414 func = handler->endpoint;
415 break;
416 default:
417 dev_warn(dev, "unsupported RECIP(%d)\n", recip);
418 func = NULL;
419 ret = -EINVAL;
420 }
421
422 if (func) {
423 dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
424 ret = func(priv, uep, ctrl);
425 }
426
427 return ret;
428}
429
430/*
431 * irq functions
432 *
433 * it will be called from usbhs_interrupt
434 */
435static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
436 struct usbhs_irq_state *irq_state)
437{
438 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
439 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
440
Kuninori Morimoto75587f52011-10-10 22:01:51 -0700441 gpriv->gadget.speed = usbhs_bus_get_speed(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900442
443 dev_dbg(dev, "state = %x : speed : %d\n",
444 usbhs_status_get_device_state(irq_state),
445 gpriv->gadget.speed);
446
447 return 0;
448}
449
450static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
451 struct usbhs_irq_state *irq_state)
452{
453 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
454 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
455 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
456 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
457 struct usb_ctrlrequest ctrl;
458 struct usbhsg_recip_handle *recip_handler = NULL;
459 int stage = usbhs_status_get_ctrl_stage(irq_state);
460 int ret = 0;
461
462 dev_dbg(dev, "stage = %d\n", stage);
463
464 /*
465 * see Manual
466 *
467 * "Operation"
468 * - "Interrupt Function"
469 * - "Control Transfer Stage Transition Interrupt"
470 * - Fig. "Control Transfer Stage Transitions"
471 */
472
473 switch (stage) {
474 case READ_DATA_STAGE:
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700475 pipe->handler = &usbhs_fifo_pio_push_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900476 break;
477 case WRITE_DATA_STAGE:
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700478 pipe->handler = &usbhs_fifo_pio_pop_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900479 break;
480 case NODATA_STATUS_STAGE:
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700481 pipe->handler = &usbhs_ctrl_stage_end_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900482 break;
483 default:
484 return ret;
485 }
486
487 /*
488 * get usb request
489 */
490 usbhs_usbreq_get_val(priv, &ctrl);
491
492 switch (ctrl.bRequestType & USB_TYPE_MASK) {
493 case USB_TYPE_STANDARD:
494 switch (ctrl.bRequest) {
495 case USB_REQ_CLEAR_FEATURE:
496 recip_handler = &req_clear_feature;
497 break;
Kuninori Morimotoced6e092011-11-24 17:27:50 -0800498 case USB_REQ_SET_FEATURE:
499 recip_handler = &req_set_feature;
500 break;
Kuninori Morimoto17f7f762011-11-24 17:28:04 -0800501 case USB_REQ_GET_STATUS:
502 recip_handler = &req_get_status;
503 break;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900504 }
505 }
506
507 /*
508 * setup stage / run recip
509 */
510 if (recip_handler)
511 ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
512 else
513 ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
514
515 if (ret < 0)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900516 usbhs_pipe_stall(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900517
518 return ret;
519}
520
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900521/*
522 *
523 * usb_dcp_ops
524 *
525 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900526static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
527{
528 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto8a2c2252011-06-06 14:18:33 +0900529 struct usbhs_pkt *pkt;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900530
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900531 while (1) {
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900532 pkt = usbhs_pkt_pop(pipe, NULL);
Kuninori Morimoto8a2c2252011-06-06 14:18:33 +0900533 if (!pkt)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900534 break;
Kuninori Morimoto91b158f2011-11-24 17:28:26 -0800535
536 usbhsg_queue_pop(uep, usbhsg_pkt_to_ureq(pkt), -ECONNRESET);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900537 }
538
Kuninori Morimoto91b158f2011-11-24 17:28:26 -0800539 usbhs_pipe_disable(pipe);
540
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900541 return 0;
542}
543
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900544static void usbhsg_uep_init(struct usbhsg_gpriv *gpriv)
545{
546 int i;
547 struct usbhsg_uep *uep;
548
549 usbhsg_for_each_uep_with_dcp(uep, gpriv, i)
550 uep->pipe = NULL;
551}
552
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900553/*
554 *
555 * usb_ep_ops
556 *
557 */
558static int usbhsg_ep_enable(struct usb_ep *ep,
559 const struct usb_endpoint_descriptor *desc)
560{
561 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
562 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
563 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
564 struct usbhs_pipe *pipe;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900565 int ret = -EIO;
566
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900567 /*
568 * if it already have pipe,
569 * nothing to do
570 */
Kuninori Morimoto08e6c612011-06-09 16:48:25 +0900571 if (uep->pipe) {
572 usbhs_pipe_clear(uep->pipe);
Kuninori Morimoto6e6db822011-10-10 22:05:30 -0700573 usbhs_pipe_sequence_data0(uep->pipe);
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900574 return 0;
Kuninori Morimoto08e6c612011-06-09 16:48:25 +0900575 }
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900576
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700577 pipe = usbhs_pipe_malloc(priv,
578 usb_endpoint_type(desc),
579 usb_endpoint_dir_in(desc));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900580 if (pipe) {
581 uep->pipe = pipe;
582 pipe->mod_private = uep;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900583
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700584 /* set epnum / maxp */
Kuninori Morimotobc6fbf52011-10-10 22:04:00 -0700585 usbhs_pipe_config_update(pipe, 0,
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700586 usb_endpoint_num(desc),
587 usb_endpoint_maxp(desc));
588
Kuninori Morimoto233f5192011-07-07 00:23:24 -0700589 /*
590 * usbhs_fifo_dma_push/pop_handler try to
591 * use dmaengine if possible.
592 * It will use pio handler if impossible.
593 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900594 if (usb_endpoint_dir_in(desc))
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700595 pipe->handler = &usbhs_fifo_dma_push_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900596 else
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700597 pipe->handler = &usbhs_fifo_dma_pop_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900598
599 ret = 0;
600 }
Kuninori Morimotocb966322011-04-21 14:10:08 +0900601
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900602 return ret;
603}
604
605static int usbhsg_ep_disable(struct usb_ep *ep)
606{
607 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900608
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900609 return usbhsg_pipe_disable(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900610}
611
612static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
613 gfp_t gfp_flags)
614{
615 struct usbhsg_request *ureq;
616
617 ureq = kzalloc(sizeof *ureq, gfp_flags);
618 if (!ureq)
619 return NULL;
620
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900621 usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
622
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900623 return &ureq->req;
624}
625
626static void usbhsg_ep_free_request(struct usb_ep *ep,
627 struct usb_request *req)
628{
629 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
630
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900631 WARN_ON(!list_empty(&ureq->pkt.node));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900632 kfree(ureq);
633}
634
635static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
636 gfp_t gfp_flags)
637{
638 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
639 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
640 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
641 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900642
643 /* param check */
644 if (usbhsg_is_not_connected(gpriv) ||
645 unlikely(!gpriv->driver) ||
646 unlikely(!pipe))
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900647 return -ESHUTDOWN;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900648
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900649 usbhsg_queue_push(uep, ureq);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900650
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900651 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900652}
653
654static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
655{
656 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
657 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900658 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900659
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900660 usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900661 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
662
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900663 return 0;
664}
665
666static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
667{
668 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
669 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
670 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900671 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900672 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900673 unsigned long flags;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900674
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900675 usbhsg_pipe_disable(uep);
676
677 dev_dbg(dev, "set halt %d (pipe %d)\n",
678 halt, usbhs_pipe_number(pipe));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900679
680 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900681 usbhs_lock(priv, flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900682
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900683 if (halt)
684 usbhs_pipe_stall(pipe);
685 else
686 usbhs_pipe_disable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900687
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900688 if (halt && wedge)
689 usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
690 else
691 usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900692
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900693 usbhs_unlock(priv, flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900694 /******************** spin unlock ******************/
695
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900696 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900697}
698
699static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
700{
701 return __usbhsg_ep_set_halt_wedge(ep, value, 0);
702}
703
704static int usbhsg_ep_set_wedge(struct usb_ep *ep)
705{
706 return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
707}
708
709static struct usb_ep_ops usbhsg_ep_ops = {
710 .enable = usbhsg_ep_enable,
711 .disable = usbhsg_ep_disable,
712
713 .alloc_request = usbhsg_ep_alloc_request,
714 .free_request = usbhsg_ep_free_request,
715
716 .queue = usbhsg_ep_queue,
717 .dequeue = usbhsg_ep_dequeue,
718
719 .set_halt = usbhsg_ep_set_halt,
720 .set_wedge = usbhsg_ep_set_wedge,
721};
722
723/*
724 * usb module start/end
725 */
726static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
727{
728 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
729 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
730 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
731 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900732 unsigned long flags;
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900733 int ret = 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900734
735 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900736 usbhs_lock(priv, flags);
737
738 usbhsg_status_set(gpriv, status);
739 if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
740 usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
741 ret = -1; /* not ready */
742
743 usbhs_unlock(priv, flags);
744 /******************** spin unlock ********************/
745
746 if (ret < 0)
747 return 0; /* not ready is not error */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900748
749 /*
750 * enable interrupt and systems if ready
751 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900752 dev_dbg(dev, "start gadget\n");
753
754 /*
755 * pipe initialize and enable DCP
756 */
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900757 usbhs_pipe_init(priv,
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900758 usbhsg_dma_map_ctrl);
Kuninori Morimotodad67392011-06-06 14:18:28 +0900759 usbhs_fifo_init(priv);
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900760 usbhsg_uep_init(gpriv);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900761
762 /* dcp init */
763 dcp->pipe = usbhs_dcp_malloc(priv);
764 dcp->pipe->mod_private = dcp;
Kuninori Morimotobc6fbf52011-10-10 22:04:00 -0700765 usbhs_pipe_config_update(dcp->pipe, 0, 0, 64);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900766
767 /*
768 * system config enble
769 * - HI speed
770 * - function
771 * - usb module
772 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900773 usbhs_sys_function_ctrl(priv, 1);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900774
775 /*
776 * enable irq callback
777 */
778 mod->irq_dev_state = usbhsg_irq_dev_state;
779 mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900780 usbhs_irq_callback_update(priv, mod);
781
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900782 return 0;
783}
784
785static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
786{
787 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
788 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
789 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
790 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900791 unsigned long flags;
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900792 int ret = 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900793
794 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900795 usbhs_lock(priv, flags);
796
797 usbhsg_status_clr(gpriv, status);
798 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
799 !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
800 ret = -1; /* already done */
801
802 usbhs_unlock(priv, flags);
803 /******************** spin unlock ********************/
804
805 if (ret < 0)
806 return 0; /* already done is not error */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900807
808 /*
809 * disable interrupt and systems if 1st try
810 */
Kuninori Morimotodad67392011-06-06 14:18:28 +0900811 usbhs_fifo_quit(priv);
812
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900813 /* disable all irq */
814 mod->irq_dev_state = NULL;
815 mod->irq_ctrl_stage = NULL;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900816 usbhs_irq_callback_update(priv, mod);
817
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900818 gpriv->gadget.speed = USB_SPEED_UNKNOWN;
819
820 /* disable sys */
Kuninori Morimotodfbb7f42011-11-24 17:28:35 -0800821 usbhs_sys_set_test_mode(priv, 0);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900822 usbhs_sys_function_ctrl(priv, 0);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900823
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900824 usbhsg_pipe_disable(dcp);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900825
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900826 dev_dbg(dev, "stop gadget\n");
827
828 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900829}
830
831/*
832 *
833 * linux usb function
834 *
835 */
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300836static int usbhsg_gadget_start(struct usb_gadget *gadget,
837 struct usb_gadget_driver *driver)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900838{
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300839 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -0800840 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900841
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300842 if (!driver ||
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900843 !driver->setup ||
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100844 driver->max_speed < USB_SPEED_FULL)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900845 return -EINVAL;
Kuninori Morimoto3b872182011-07-03 17:42:35 -0700846
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900847 /* first hook up the driver ... */
848 gpriv->driver = driver;
849 gpriv->gadget.dev.driver = &driver->driver;
850
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900851 return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900852}
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900853
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300854static int usbhsg_gadget_stop(struct usb_gadget *gadget,
855 struct usb_gadget_driver *driver)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900856{
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300857 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -0800858 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900859
860 if (!driver ||
Kuninori Morimoto3b872182011-07-03 17:42:35 -0700861 !driver->unbind)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900862 return -EINVAL;
863
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900864 usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
Kuninori Morimoto0cdd7d42011-11-17 18:19:56 -0800865 gpriv->gadget.dev.driver = NULL;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900866 gpriv->driver = NULL;
867
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900868 return 0;
869}
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900870
871/*
872 * usb gadget ops
873 */
874static int usbhsg_get_frame(struct usb_gadget *gadget)
875{
876 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
877 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
878
879 return usbhs_frame_get_num(priv);
880}
881
882static struct usb_gadget_ops usbhsg_gadget_ops = {
883 .get_frame = usbhsg_get_frame,
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300884 .udc_start = usbhsg_gadget_start,
885 .udc_stop = usbhsg_gadget_stop,
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900886};
887
888static int usbhsg_start(struct usbhs_priv *priv)
889{
890 return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
891}
892
893static int usbhsg_stop(struct usbhs_priv *priv)
894{
Kuninori Morimoto8885a892011-11-17 18:19:38 -0800895 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
896
897 /* cable disconnect */
898 if (gpriv->driver &&
899 gpriv->driver->disconnect)
900 gpriv->driver->disconnect(&gpriv->gadget);
901
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900902 return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
903}
904
Kuninori Morimoto3b2a2e42012-02-20 17:35:50 -0800905static void usbhs_mod_gadget_release(struct device *pdev)
906{
907 /* do nothing */
908}
909
Kuninori Morimotob7a8d172011-10-26 19:33:49 -0700910int usbhs_mod_gadget_probe(struct usbhs_priv *priv)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900911{
912 struct usbhsg_gpriv *gpriv;
913 struct usbhsg_uep *uep;
914 struct device *dev = usbhs_priv_to_dev(priv);
915 int pipe_size = usbhs_get_dparam(priv, pipe_size);
916 int i;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300917 int ret;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900918
919 gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
920 if (!gpriv) {
921 dev_err(dev, "Could not allocate gadget priv\n");
922 return -ENOMEM;
923 }
924
925 uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
926 if (!uep) {
927 dev_err(dev, "Could not allocate ep\n");
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300928 ret = -ENOMEM;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900929 goto usbhs_mod_gadget_probe_err_gpriv;
930 }
931
932 /*
933 * CAUTION
934 *
935 * There is no guarantee that it is possible to access usb module here.
936 * Don't accesses to it.
937 * The accesse will be enable after "usbhsg_start"
938 */
939
940 /*
941 * register itself
942 */
943 usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
944
945 /* init gpriv */
946 gpriv->mod.name = "gadget";
947 gpriv->mod.start = usbhsg_start;
948 gpriv->mod.stop = usbhsg_stop;
949 gpriv->uep = uep;
950 gpriv->uep_size = pipe_size;
951 usbhsg_status_init(gpriv);
952
953 /*
954 * init gadget
955 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900956 dev_set_name(&gpriv->gadget.dev, "gadget");
957 gpriv->gadget.dev.parent = dev;
Kuninori Morimoto3b2a2e42012-02-20 17:35:50 -0800958 gpriv->gadget.dev.release = usbhs_mod_gadget_release;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900959 gpriv->gadget.name = "renesas_usbhs_udc";
960 gpriv->gadget.ops = &usbhsg_gadget_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100961 gpriv->gadget.max_speed = USB_SPEED_HIGH;
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -0800962 ret = device_register(&gpriv->gadget.dev);
963 if (ret < 0)
964 goto err_add_udc;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900965
966 INIT_LIST_HEAD(&gpriv->gadget.ep_list);
967
968 /*
969 * init usb_ep
970 */
971 usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
972 uep->gpriv = gpriv;
973 snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
974
975 uep->ep.name = uep->ep_name;
976 uep->ep.ops = &usbhsg_ep_ops;
977 INIT_LIST_HEAD(&uep->ep.ep_list);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900978
979 /* init DCP */
980 if (usbhsg_is_dcp(uep)) {
981 gpriv->gadget.ep0 = &uep->ep;
982 uep->ep.maxpacket = 64;
983 }
984 /* init normal pipe */
985 else {
986 uep->ep.maxpacket = 512;
987 list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
988 }
989 }
990
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300991 ret = usb_add_gadget_udc(dev, &gpriv->gadget);
992 if (ret)
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -0800993 goto err_register;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300994
995
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900996 dev_info(dev, "gadget probed\n");
997
998 return 0;
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -0800999
1000err_register:
1001 device_unregister(&gpriv->gadget.dev);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001002err_add_udc:
1003 kfree(gpriv->uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001004
1005usbhs_mod_gadget_probe_err_gpriv:
1006 kfree(gpriv);
1007
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001008 return ret;
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001009}
1010
Kuninori Morimotob7a8d172011-10-26 19:33:49 -07001011void usbhs_mod_gadget_remove(struct usbhs_priv *priv)
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001012{
1013 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1014
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001015 usb_del_gadget_udc(&gpriv->gadget);
Kuninori Morimoto3b872182011-07-03 17:42:35 -07001016
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -08001017 device_unregister(&gpriv->gadget.dev);
1018
Sebastian Andrzej Siewior3af51ac2011-06-03 19:50:48 +02001019 kfree(gpriv->uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001020 kfree(gpriv);
1021}