blob: a3818773dec113c9cd3f3d0ff6a636c363e0d7e2 [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 */
17#include <linux/io.h>
18#include <linux/module.h>
19#include <linux/platform_device.h>
20#include <linux/usb/ch9.h>
21#include <linux/usb/gadget.h>
22#include "common.h"
23
24/*
25 * struct
26 */
27struct usbhsg_request {
28 struct usb_request req;
Kuninori Morimoto4bd04812011-06-06 14:18:07 +090029 struct usbhs_pkt pkt;
Kuninori Morimoto2f983822011-04-05 11:40:54 +090030};
31
32#define EP_NAME_SIZE 8
33struct usbhsg_gpriv;
34struct usbhsg_pipe_handle;
35struct usbhsg_uep {
36 struct usb_ep ep;
37 struct usbhs_pipe *pipe;
Kuninori Morimoto2f983822011-04-05 11:40:54 +090038
39 char ep_name[EP_NAME_SIZE];
40
41 struct usbhsg_gpriv *gpriv;
42 struct usbhsg_pipe_handle *handler;
43};
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
60struct usbhsg_pipe_handle {
61 int (*prepare)(struct usbhsg_uep *uep, struct usbhsg_request *ureq);
62 int (*try_run)(struct usbhsg_uep *uep, struct usbhsg_request *ureq);
63 void (*irq_mask)(struct usbhsg_uep *uep, int enable);
64};
65
66struct usbhsg_recip_handle {
67 char *name;
68 int (*device)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
69 struct usb_ctrlrequest *ctrl);
70 int (*interface)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
71 struct usb_ctrlrequest *ctrl);
72 int (*endpoint)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
73 struct usb_ctrlrequest *ctrl);
74};
75
76/*
77 * macro
78 */
79#define usbhsg_priv_to_gpriv(priv) \
80 container_of( \
81 usbhs_mod_get(priv, USBHS_GADGET), \
82 struct usbhsg_gpriv, mod)
83
84#define __usbhsg_for_each_uep(start, pos, g, i) \
85 for (i = start, pos = (g)->uep; \
86 i < (g)->uep_size; \
87 i++, pos = (g)->uep + i)
88
89#define usbhsg_for_each_uep(pos, gpriv, i) \
90 __usbhsg_for_each_uep(1, pos, gpriv, i)
91
92#define usbhsg_for_each_uep_with_dcp(pos, gpriv, i) \
93 __usbhsg_for_each_uep(0, pos, gpriv, i)
94
95#define usbhsg_gadget_to_gpriv(g)\
96 container_of(g, struct usbhsg_gpriv, gadget)
97
98#define usbhsg_req_to_ureq(r)\
99 container_of(r, struct usbhsg_request, req)
100
101#define usbhsg_ep_to_uep(e) container_of(e, struct usbhsg_uep, ep)
102#define usbhsg_gpriv_to_lock(gp) usbhs_priv_to_lock((gp)->mod.priv)
103#define usbhsg_gpriv_to_dev(gp) usbhs_priv_to_dev((gp)->mod.priv)
104#define usbhsg_gpriv_to_priv(gp) ((gp)->mod.priv)
105#define usbhsg_gpriv_to_dcp(gp) ((gp)->uep)
106#define usbhsg_gpriv_to_nth_uep(gp, i) ((gp)->uep + i)
107#define usbhsg_uep_to_gpriv(u) ((u)->gpriv)
108#define usbhsg_uep_to_pipe(u) ((u)->pipe)
109#define usbhsg_pipe_to_uep(p) ((p)->mod_private)
110#define usbhsg_is_dcp(u) ((u) == usbhsg_gpriv_to_dcp((u)->gpriv))
111
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900112#define usbhsg_ureq_to_pkt(u) (&(u)->pkt)
113#define usbhsg_pkt_to_ureq(i) \
114 container_of(i, struct usbhsg_request, pkt)
115
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900116#define usbhsg_is_not_connected(gp) ((gp)->gadget.speed == USB_SPEED_UNKNOWN)
117
118/* status */
119#define usbhsg_status_init(gp) do {(gp)->status = 0; } while (0)
120#define usbhsg_status_set(gp, b) (gp->status |= b)
121#define usbhsg_status_clr(gp, b) (gp->status &= ~b)
122#define usbhsg_status_has(gp, b) (gp->status & b)
123
124/*
Kuninori Morimotocb966322011-04-21 14:10:08 +0900125 * usbhsg_trylock
126 *
127 * This driver don't use spin_try_lock
128 * to avoid warning of CONFIG_DEBUG_SPINLOCK
129 */
130static spinlock_t *usbhsg_trylock(struct usbhsg_gpriv *gpriv,
131 unsigned long *flags)
132{
133 spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
134
135 /* check spin lock status
136 * to avoid deadlock/nest */
137 if (spin_is_locked(lock))
138 return NULL;
139
140 spin_lock_irqsave(lock, *flags);
141
142 return lock;
143}
144
145static void usbhsg_unlock(spinlock_t *lock, unsigned long *flags)
146{
147 if (!lock)
148 return;
149
150 spin_unlock_irqrestore(lock, *flags);
151}
152
153/*
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900154 * list push/pop
155 */
156static 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);
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900162 struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900163
164 /*
165 ********* assume under spin lock *********
166 */
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900167 usbhs_pkt_push(pipe, pkt);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900168 ureq->req.actual = 0;
169 ureq->req.status = -EINPROGRESS;
170
171 dev_dbg(dev, "pipe %d : queue push (%d)\n",
172 usbhs_pipe_number(pipe),
173 ureq->req.length);
174}
175
176static struct usbhsg_request *usbhsg_queue_get(struct usbhsg_uep *uep)
177{
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900178 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
179 struct usbhs_pkt *pkt = usbhs_pkt_get(pipe);
180
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900181 /*
182 ********* assume under spin lock *********
183 */
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900184 if (!pkt)
185 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900186
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900187 return usbhsg_pkt_to_ureq(pkt);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900188}
189
190#define usbhsg_queue_prepare(uep) __usbhsg_queue_handler(uep, 1);
191#define usbhsg_queue_handle(uep) __usbhsg_queue_handler(uep, 0);
192static int __usbhsg_queue_handler(struct usbhsg_uep *uep, int prepare)
193{
194 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
195 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
196 struct usbhsg_request *ureq;
Kuninori Morimotocb966322011-04-21 14:10:08 +0900197 spinlock_t *lock;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900198 unsigned long flags;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900199 int ret = 0;
200
201 if (!uep->handler) {
202 dev_err(dev, "no handler function\n");
203 return -EIO;
204 }
205
206 /*
207 * CAUTION [*queue handler*]
208 *
209 * This function will be called for start/restart queue operation.
210 * OTOH the most much worry for USB driver is spinlock nest.
211 * Specially it are
212 * - usb_ep_ops :: queue
213 * - usb_request :: complete
214 *
215 * But the caller of this function need not care about spinlock.
Kuninori Morimotocb966322011-04-21 14:10:08 +0900216 * This function is using usbhsg_trylock for it.
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900217 * if "is_locked" is 1, this mean this function lock it.
218 * but if it is 0, this mean it is already under spin lock.
219 * see also
220 * CAUTION [*endpoint queue*]
221 * CAUTION [*request complete*]
222 */
223
224 /****************** spin try lock *******************/
Kuninori Morimotocb966322011-04-21 14:10:08 +0900225 lock = usbhsg_trylock(gpriv, &flags);
226
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900227 ureq = usbhsg_queue_get(uep);
228 if (ureq) {
229 if (prepare)
230 ret = uep->handler->prepare(uep, ureq);
231 else
232 ret = uep->handler->try_run(uep, ureq);
233 }
Kuninori Morimotocb966322011-04-21 14:10:08 +0900234 usbhsg_unlock(lock, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900235 /******************** spin unlock ******************/
236
237 return ret;
238}
239
240static void usbhsg_queue_pop(struct usbhsg_uep *uep,
241 struct usbhsg_request *ureq,
242 int status)
243{
244 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
245 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
246 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900247 struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900248
249 /*
250 ********* assume under spin lock *********
251 */
252
253 /*
254 * CAUTION [*request complete*]
255 *
256 * There is a possibility not to be called in correct order
257 * if "complete" is called without spinlock.
258 *
259 * So, this function assume it is under spinlock,
260 * and call usb_request :: complete.
261 *
262 * But this "complete" will push next usb_request.
263 * It mean "usb_ep_ops :: queue" which is using spinlock is called
264 * under spinlock.
265 *
Kuninori Morimotocb966322011-04-21 14:10:08 +0900266 * To avoid dead-lock, this driver is using usbhsg_trylock.
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900267 * CAUTION [*endpoint queue*]
268 * CAUTION [*queue handler*]
269 */
270
271 dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
272
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900273 usbhs_pkt_pop(pkt);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900274
275 ureq->req.status = status;
276 ureq->req.complete(&uep->ep, &ureq->req);
277
278 /* more request ? */
279 if (0 == status)
280 usbhsg_queue_prepare(uep);
281}
282
283/*
284 * irq enable/disable function
285 */
286#define usbhsg_irq_callback_ctrl(uep, status, enable) \
287 ({ \
288 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); \
289 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); \
290 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); \
291 struct usbhs_mod *mod = usbhs_mod_get_current(priv); \
292 if (!mod) \
293 return; \
294 if (enable) \
295 mod->irq_##status |= (1 << usbhs_pipe_number(pipe)); \
296 else \
297 mod->irq_##status &= ~(1 << usbhs_pipe_number(pipe)); \
298 usbhs_irq_callback_update(priv, mod); \
299 })
300
301static void usbhsg_irq_empty_ctrl(struct usbhsg_uep *uep, int enable)
302{
303 usbhsg_irq_callback_ctrl(uep, bempsts, enable);
304}
305
306static void usbhsg_irq_ready_ctrl(struct usbhsg_uep *uep, int enable)
307{
308 usbhsg_irq_callback_ctrl(uep, brdysts, enable);
309}
310
311/*
312 * handler function
313 */
314static int usbhsg_try_run_ctrl_stage_end(struct usbhsg_uep *uep,
315 struct usbhsg_request *ureq)
316{
317 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
318
319 /*
320 ********* assume under spin lock *********
321 */
322
323 usbhs_dcp_control_transfer_done(pipe);
324 usbhsg_queue_pop(uep, ureq, 0);
325
326 return 0;
327}
328
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900329/*
330 * packet send hander
331 */
332static void usbhsg_try_run_send_packet_bh(struct usbhs_pkt *pkt)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900333{
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900334 struct usbhs_pipe *pipe = pkt->pipe;
335 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
336 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900337 struct usb_request *req = &ureq->req;
338 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
339 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900340 int remainder, send, maxp;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900341 int is_done = 0;
342 int enable;
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900343
344 maxp = pkt->maxp;
345 send = pkt->actual;
346 remainder = pkt->length;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900347
348 /*
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900349 * send = 0 : send zero packet
350 * send > 0 : send data
351 *
352 * send <= max_packet
353 */
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900354 req->actual += send;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900355
356 /* send all packet ? */
357 if (send < remainder)
358 is_done = 0; /* there are remainder data */
359 else if (send < maxp)
360 is_done = 1; /* short packet */
361 else
362 is_done = !req->zero; /* send zero packet ? */
363
364 dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n",
365 usbhs_pipe_number(pipe),
366 remainder, send, is_done, req->zero);
367
368 /*
369 * enable interrupt and send again in irq handler
370 * if it still have remainder data which should be sent.
371 */
372 enable = !is_done;
373 uep->handler->irq_mask(uep, enable);
374
375 /*
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900376 * all data were sent ?
377 */
378 if (is_done) {
379 /* it care below call in
380 "function mode" */
381 if (usbhsg_is_dcp(uep))
382 usbhs_dcp_control_transfer_done(pipe);
383
384 usbhsg_queue_pop(uep, ureq, 0);
385 }
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900386}
387
388static int usbhsg_try_run_send_packet(struct usbhsg_uep *uep,
389 struct usbhsg_request *ureq)
390{
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900391 struct usb_request *req = &ureq->req;
392 struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
393 int ret;
394
395 /*
396 ********* assume under spin lock *********
397 */
398
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900399 usbhs_pkt_update(pkt,
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900400 req->buf + req->actual,
401 req->length - req->actual);
402
403 ret = usbhs_fifo_write(pkt);
404 if (ret < 0) {
405 /* pipe is busy.
406 * retry in interrupt */
407 uep->handler->irq_mask(uep, 1);
408 }
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900409
410 return 0;
411}
412
413static int usbhsg_prepare_send_packet(struct usbhsg_uep *uep,
414 struct usbhsg_request *ureq)
415{
416 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
417
418 /*
419 ********* assume under spin lock *********
420 */
421
422 usbhs_fifo_prepare_write(pipe);
423 usbhsg_try_run_send_packet(uep, ureq);
424
425 return 0;
426}
427
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900428/*
429 * packet recv hander
430 */
431static void usbhsg_try_run_receive_packet_bh(struct usbhs_pkt *pkt)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900432{
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900433 struct usbhs_pipe *pipe = pkt->pipe;
434 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
435 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900436 struct usb_request *req = &ureq->req;
437 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
438 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900439 int remainder, recv, maxp;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900440 int is_done = 0;
441
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900442 maxp = pkt->maxp;
443 remainder = pkt->length;
444 recv = pkt->actual;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900445
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900446 /*
447 * recv < 0 : pipe busy
448 * recv >= 0 : receive data
449 *
450 * recv <= max_packet
451 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900452
453 /* update parameters */
454 req->actual += recv;
455
456 if ((recv == remainder) || /* receive all data */
457 (recv < maxp)) /* short packet */
458 is_done = 1;
459
460 dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n",
461 usbhs_pipe_number(pipe),
462 remainder, recv, is_done, req->zero);
463
464 /* read all data ? */
465 if (is_done) {
466 int disable = 0;
467
468 uep->handler->irq_mask(uep, disable);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900469 usbhs_pipe_disable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900470 usbhsg_queue_pop(uep, ureq, 0);
471 }
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900472}
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900473
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900474static int usbhsg_try_run_receive_packet(struct usbhsg_uep *uep,
475 struct usbhsg_request *ureq)
476{
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900477 struct usb_request *req = &ureq->req;
478 struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
479
480 /*
481 ********* assume under spin lock *********
482 */
483
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900484 usbhs_pkt_update(pkt,
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900485 req->buf + req->actual,
486 req->length - req->actual);
487
488 return usbhs_fifo_read(pkt);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900489}
490
491static int usbhsg_prepare_receive_packet(struct usbhsg_uep *uep,
492 struct usbhsg_request *ureq)
493{
494 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
495 int enable = 1;
496 int ret;
497
498 /*
499 ********* assume under spin lock *********
500 */
501
502 ret = usbhs_fifo_prepare_read(pipe);
503 if (ret < 0)
504 return ret;
505
506 /*
507 * data will be read in interrupt handler
508 */
509 uep->handler->irq_mask(uep, enable);
510
511 return ret;
512}
513
514static struct usbhsg_pipe_handle usbhsg_handler_send_by_empty = {
515 .prepare = usbhsg_prepare_send_packet,
516 .try_run = usbhsg_try_run_send_packet,
517 .irq_mask = usbhsg_irq_empty_ctrl,
518};
519
520static struct usbhsg_pipe_handle usbhsg_handler_send_by_ready = {
521 .prepare = usbhsg_prepare_send_packet,
522 .try_run = usbhsg_try_run_send_packet,
523 .irq_mask = usbhsg_irq_ready_ctrl,
524};
525
526static struct usbhsg_pipe_handle usbhsg_handler_recv_by_ready = {
527 .prepare = usbhsg_prepare_receive_packet,
528 .try_run = usbhsg_try_run_receive_packet,
529 .irq_mask = usbhsg_irq_ready_ctrl,
530};
531
532static struct usbhsg_pipe_handle usbhsg_handler_ctrl_stage_end = {
533 .prepare = usbhsg_try_run_ctrl_stage_end,
534 .try_run = usbhsg_try_run_ctrl_stage_end,
535};
536
537/*
538 * DCP pipe can NOT use "ready interrupt" for "send"
539 * it should use "empty" interrupt.
540 * see
541 * "Operation" - "Interrupt Function" - "BRDY Interrupt"
542 *
543 * on the other hand, normal pipe can use "ready interrupt" for "send"
544 * even though it is single/double buffer
545 */
546#define usbhsg_handler_send_ctrl usbhsg_handler_send_by_empty
547#define usbhsg_handler_recv_ctrl usbhsg_handler_recv_by_ready
548
549#define usbhsg_handler_send_packet usbhsg_handler_send_by_ready
550#define usbhsg_handler_recv_packet usbhsg_handler_recv_by_ready
551
552/*
553 * USB_TYPE_STANDARD / clear feature functions
554 */
555static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
556 struct usbhsg_uep *uep,
557 struct usb_ctrlrequest *ctrl)
558{
559 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
560 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
561 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
562
563 usbhs_dcp_control_transfer_done(pipe);
564
565 return 0;
566}
567
568static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
569 struct usbhsg_uep *uep,
570 struct usb_ctrlrequest *ctrl)
571{
572 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
573 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
574
575 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900576 usbhs_pipe_disable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900577 usbhs_pipe_clear_sequence(pipe);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900578 usbhs_pipe_enable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900579 }
580
581 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
582
583 usbhsg_queue_prepare(uep);
584
585 return 0;
586}
587
588struct usbhsg_recip_handle req_clear_feature = {
589 .name = "clear feature",
590 .device = usbhsg_recip_handler_std_control_done,
591 .interface = usbhsg_recip_handler_std_control_done,
592 .endpoint = usbhsg_recip_handler_std_clear_endpoint,
593};
594
595/*
596 * USB_TYPE handler
597 */
598static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
599 struct usbhsg_recip_handle *handler,
600 struct usb_ctrlrequest *ctrl)
601{
602 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
603 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
604 struct usbhsg_uep *uep;
605 int recip = ctrl->bRequestType & USB_RECIP_MASK;
606 int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
607 int ret;
608 int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
609 struct usb_ctrlrequest *ctrl);
610 char *msg;
611
612 uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
Kuninori Morimoto9a28b7b2011-04-21 14:10:12 +0900613 if (!usbhsg_uep_to_pipe(uep)) {
614 dev_err(dev, "wrong recip request\n");
615 return -EINVAL;
616 }
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900617
618 switch (recip) {
619 case USB_RECIP_DEVICE:
620 msg = "DEVICE";
621 func = handler->device;
622 break;
623 case USB_RECIP_INTERFACE:
624 msg = "INTERFACE";
625 func = handler->interface;
626 break;
627 case USB_RECIP_ENDPOINT:
628 msg = "ENDPOINT";
629 func = handler->endpoint;
630 break;
631 default:
632 dev_warn(dev, "unsupported RECIP(%d)\n", recip);
633 func = NULL;
634 ret = -EINVAL;
635 }
636
637 if (func) {
638 dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
639 ret = func(priv, uep, ctrl);
640 }
641
642 return ret;
643}
644
645/*
646 * irq functions
647 *
648 * it will be called from usbhs_interrupt
649 */
650static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
651 struct usbhs_irq_state *irq_state)
652{
653 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
654 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
655
656 gpriv->gadget.speed = usbhs_status_get_usb_speed(irq_state);
657
658 dev_dbg(dev, "state = %x : speed : %d\n",
659 usbhs_status_get_device_state(irq_state),
660 gpriv->gadget.speed);
661
662 return 0;
663}
664
665static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
666 struct usbhs_irq_state *irq_state)
667{
668 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
669 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
670 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
671 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
672 struct usb_ctrlrequest ctrl;
673 struct usbhsg_recip_handle *recip_handler = NULL;
674 int stage = usbhs_status_get_ctrl_stage(irq_state);
675 int ret = 0;
676
677 dev_dbg(dev, "stage = %d\n", stage);
678
679 /*
680 * see Manual
681 *
682 * "Operation"
683 * - "Interrupt Function"
684 * - "Control Transfer Stage Transition Interrupt"
685 * - Fig. "Control Transfer Stage Transitions"
686 */
687
688 switch (stage) {
689 case READ_DATA_STAGE:
690 dcp->handler = &usbhsg_handler_send_ctrl;
691 break;
692 case WRITE_DATA_STAGE:
693 dcp->handler = &usbhsg_handler_recv_ctrl;
694 break;
695 case NODATA_STATUS_STAGE:
696 dcp->handler = &usbhsg_handler_ctrl_stage_end;
697 break;
698 default:
699 return ret;
700 }
701
702 /*
703 * get usb request
704 */
705 usbhs_usbreq_get_val(priv, &ctrl);
706
707 switch (ctrl.bRequestType & USB_TYPE_MASK) {
708 case USB_TYPE_STANDARD:
709 switch (ctrl.bRequest) {
710 case USB_REQ_CLEAR_FEATURE:
711 recip_handler = &req_clear_feature;
712 break;
713 }
714 }
715
716 /*
717 * setup stage / run recip
718 */
719 if (recip_handler)
720 ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
721 else
722 ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
723
724 if (ret < 0)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900725 usbhs_pipe_stall(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900726
727 return ret;
728}
729
730static int usbhsg_irq_empty(struct usbhs_priv *priv,
731 struct usbhs_irq_state *irq_state)
732{
733 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
734 struct usbhsg_uep *uep;
735 struct usbhs_pipe *pipe;
736 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
737 int i, ret;
738
739 if (!irq_state->bempsts) {
740 dev_err(dev, "debug %s !!\n", __func__);
741 return -EIO;
742 }
743
744 dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
745
746 /*
747 * search interrupted "pipe"
748 * not "uep".
749 */
750 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
751 if (!(irq_state->bempsts & (1 << i)))
752 continue;
753
754 uep = usbhsg_pipe_to_uep(pipe);
755 ret = usbhsg_queue_handle(uep);
756 if (ret < 0)
757 dev_err(dev, "send error %d : %d\n", i, ret);
758 }
759
760 return 0;
761}
762
763static int usbhsg_irq_ready(struct usbhs_priv *priv,
764 struct usbhs_irq_state *irq_state)
765{
766 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
767 struct usbhsg_uep *uep;
768 struct usbhs_pipe *pipe;
769 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
770 int i, ret;
771
772 if (!irq_state->brdysts) {
773 dev_err(dev, "debug %s !!\n", __func__);
774 return -EIO;
775 }
776
777 dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
778
779 /*
780 * search interrupted "pipe"
781 * not "uep".
782 */
783 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
784 if (!(irq_state->brdysts & (1 << i)))
785 continue;
786
787 uep = usbhsg_pipe_to_uep(pipe);
788 ret = usbhsg_queue_handle(uep);
789 if (ret < 0)
790 dev_err(dev, "receive error %d : %d\n", i, ret);
791 }
792
793 return 0;
794}
795
796/*
797 *
798 * usb_dcp_ops
799 *
800 */
801static int usbhsg_dcp_enable(struct usbhsg_uep *uep)
802{
803 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
804 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
805 struct usbhs_pipe *pipe;
806
807 /*
808 ********* assume under spin lock *********
809 */
810
811 pipe = usbhs_dcp_malloc(priv);
812 if (!pipe)
813 return -EIO;
814
815 uep->pipe = pipe;
816 uep->pipe->mod_private = uep;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900817
818 return 0;
819}
820
821#define usbhsg_dcp_disable usbhsg_pipe_disable
822static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
823{
824 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
825 struct usbhsg_request *ureq;
826 int disable = 0;
827
828 /*
829 ********* assume under spin lock *********
830 */
831
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900832 usbhs_pipe_disable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900833
834 /*
835 * disable pipe irq
836 */
837 usbhsg_irq_empty_ctrl(uep, disable);
838 usbhsg_irq_ready_ctrl(uep, disable);
839
840 while (1) {
841 ureq = usbhsg_queue_get(uep);
842 if (!ureq)
843 break;
844
845 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
846 }
847
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900848 return 0;
849}
850
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900851static void usbhsg_uep_init(struct usbhsg_gpriv *gpriv)
852{
853 int i;
854 struct usbhsg_uep *uep;
855
856 usbhsg_for_each_uep_with_dcp(uep, gpriv, i)
857 uep->pipe = NULL;
858}
859
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900860/*
861 *
862 * usb_ep_ops
863 *
864 */
865static int usbhsg_ep_enable(struct usb_ep *ep,
866 const struct usb_endpoint_descriptor *desc)
867{
868 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
869 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
870 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
871 struct usbhs_pipe *pipe;
Kuninori Morimotocb966322011-04-21 14:10:08 +0900872 spinlock_t *lock;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900873 unsigned long flags;
874 int ret = -EIO;
875
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900876 /*
877 * if it already have pipe,
878 * nothing to do
879 */
880 if (uep->pipe)
881 return 0;
882
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900883 /******************** spin lock ********************/
Kuninori Morimotocb966322011-04-21 14:10:08 +0900884 lock = usbhsg_trylock(gpriv, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900885
886 pipe = usbhs_pipe_malloc(priv, desc);
887 if (pipe) {
888 uep->pipe = pipe;
889 pipe->mod_private = uep;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900890
891 if (usb_endpoint_dir_in(desc))
892 uep->handler = &usbhsg_handler_send_packet;
893 else
894 uep->handler = &usbhsg_handler_recv_packet;
895
896 ret = 0;
897 }
Kuninori Morimotocb966322011-04-21 14:10:08 +0900898
899 usbhsg_unlock(lock, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900900 /******************** spin unlock ******************/
901
902 return ret;
903}
904
905static int usbhsg_ep_disable(struct usb_ep *ep)
906{
907 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
908 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
Kuninori Morimotocb966322011-04-21 14:10:08 +0900909 spinlock_t *lock;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900910 unsigned long flags;
911 int ret;
912
913 /******************** spin lock ********************/
Kuninori Morimotocb966322011-04-21 14:10:08 +0900914 lock = usbhsg_trylock(gpriv, &flags);
915
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900916 ret = usbhsg_pipe_disable(uep);
Kuninori Morimotocb966322011-04-21 14:10:08 +0900917
918 usbhsg_unlock(lock, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900919 /******************** spin unlock ******************/
920
921 return ret;
922}
923
924static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
925 gfp_t gfp_flags)
926{
927 struct usbhsg_request *ureq;
928
929 ureq = kzalloc(sizeof *ureq, gfp_flags);
930 if (!ureq)
931 return NULL;
932
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900933 usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
934
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900935 return &ureq->req;
936}
937
938static void usbhsg_ep_free_request(struct usb_ep *ep,
939 struct usb_request *req)
940{
941 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
942
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900943 WARN_ON(!list_empty(&ureq->pkt.node));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900944 kfree(ureq);
945}
946
947static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
948 gfp_t gfp_flags)
949{
950 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
951 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
952 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
953 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimotocb966322011-04-21 14:10:08 +0900954 spinlock_t *lock;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900955 unsigned long flags;
956 int ret = 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900957
958 /*
959 * CAUTION [*endpoint queue*]
960 *
961 * This function will be called from usb_request :: complete
962 * or usb driver timing.
963 * If this function is called from usb_request :: complete,
964 * it is already under spinlock on this driver.
965 * but it is called frm usb driver, this function should call spinlock.
966 *
Kuninori Morimotocb966322011-04-21 14:10:08 +0900967 * This function is using usbshg_trylock to solve this issue.
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900968 * if "is_locked" is 1, this mean this function lock it.
969 * but if it is 0, this mean it is already under spin lock.
970 * see also
971 * CAUTION [*queue handler*]
972 * CAUTION [*request complete*]
973 */
974
975 /******************** spin lock ********************/
Kuninori Morimotocb966322011-04-21 14:10:08 +0900976 lock = usbhsg_trylock(gpriv, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900977
978 /* param check */
979 if (usbhsg_is_not_connected(gpriv) ||
980 unlikely(!gpriv->driver) ||
981 unlikely(!pipe))
982 ret = -ESHUTDOWN;
983 else
984 usbhsg_queue_push(uep, ureq);
985
Kuninori Morimotocb966322011-04-21 14:10:08 +0900986 usbhsg_unlock(lock, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900987 /******************** spin unlock ******************/
988
989 usbhsg_queue_prepare(uep);
990
991 return ret;
992}
993
994static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
995{
996 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
997 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
998 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
Kuninori Morimotocb966322011-04-21 14:10:08 +0900999 spinlock_t *lock;
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001000 unsigned long flags;
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001001
1002 /*
1003 * see
1004 * CAUTION [*queue handler*]
1005 * CAUTION [*endpoint queue*]
1006 * CAUTION [*request complete*]
1007 */
1008
1009 /******************** spin lock ********************/
Kuninori Morimotocb966322011-04-21 14:10:08 +09001010 lock = usbhsg_trylock(gpriv, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001011
1012 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
1013
Kuninori Morimotocb966322011-04-21 14:10:08 +09001014 usbhsg_unlock(lock, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001015 /******************** spin unlock ******************/
1016
1017 return 0;
1018}
1019
1020static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
1021{
1022 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
1023 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
1024 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
1025 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
Kuninori Morimotocb966322011-04-21 14:10:08 +09001026 spinlock_t *lock;
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001027 unsigned long flags;
1028 int ret = -EAGAIN;
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001029
1030 /*
1031 * see
1032 * CAUTION [*queue handler*]
1033 * CAUTION [*endpoint queue*]
1034 * CAUTION [*request complete*]
1035 */
1036
1037 /******************** spin lock ********************/
Kuninori Morimotocb966322011-04-21 14:10:08 +09001038 lock = usbhsg_trylock(gpriv, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001039 if (!usbhsg_queue_get(uep)) {
1040
1041 dev_dbg(dev, "set halt %d (pipe %d)\n",
1042 halt, usbhs_pipe_number(pipe));
1043
1044 if (halt)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +09001045 usbhs_pipe_stall(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001046 else
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +09001047 usbhs_pipe_disable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001048
1049 if (halt && wedge)
1050 usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
1051 else
1052 usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
1053
1054 ret = 0;
1055 }
1056
Kuninori Morimotocb966322011-04-21 14:10:08 +09001057 usbhsg_unlock(lock, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001058 /******************** spin unlock ******************/
1059
1060 return ret;
1061}
1062
1063static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
1064{
1065 return __usbhsg_ep_set_halt_wedge(ep, value, 0);
1066}
1067
1068static int usbhsg_ep_set_wedge(struct usb_ep *ep)
1069{
1070 return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
1071}
1072
1073static struct usb_ep_ops usbhsg_ep_ops = {
1074 .enable = usbhsg_ep_enable,
1075 .disable = usbhsg_ep_disable,
1076
1077 .alloc_request = usbhsg_ep_alloc_request,
1078 .free_request = usbhsg_ep_free_request,
1079
1080 .queue = usbhsg_ep_queue,
1081 .dequeue = usbhsg_ep_dequeue,
1082
1083 .set_halt = usbhsg_ep_set_halt,
1084 .set_wedge = usbhsg_ep_set_wedge,
1085};
1086
1087/*
1088 * usb module start/end
1089 */
1090static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
1091{
1092 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1093 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
1094 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1095 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimotocb966322011-04-21 14:10:08 +09001096 spinlock_t *lock;
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001097 unsigned long flags;
1098
1099 /******************** spin lock ********************/
Kuninori Morimotocb966322011-04-21 14:10:08 +09001100 lock = usbhsg_trylock(gpriv, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001101
1102 /*
1103 * enable interrupt and systems if ready
1104 */
1105 usbhsg_status_set(gpriv, status);
1106 if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
1107 usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
1108 goto usbhsg_try_start_unlock;
1109
1110 dev_dbg(dev, "start gadget\n");
1111
1112 /*
1113 * pipe initialize and enable DCP
1114 */
Kuninori Morimoto4bd04812011-06-06 14:18:07 +09001115 usbhs_pipe_init(priv,
1116 usbhsg_try_run_send_packet_bh,
1117 usbhsg_try_run_receive_packet_bh);
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +09001118 usbhsg_uep_init(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001119 usbhsg_dcp_enable(dcp);
1120
1121 /*
1122 * system config enble
1123 * - HI speed
1124 * - function
1125 * - usb module
1126 */
1127 usbhs_sys_hispeed_ctrl(priv, 1);
1128 usbhs_sys_function_ctrl(priv, 1);
1129 usbhs_sys_usb_ctrl(priv, 1);
1130
1131 /*
1132 * enable irq callback
1133 */
1134 mod->irq_dev_state = usbhsg_irq_dev_state;
1135 mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
1136 mod->irq_empty = usbhsg_irq_empty;
1137 mod->irq_ready = usbhsg_irq_ready;
1138 mod->irq_bempsts = 0;
1139 mod->irq_brdysts = 0;
1140 usbhs_irq_callback_update(priv, mod);
1141
1142usbhsg_try_start_unlock:
Kuninori Morimotocb966322011-04-21 14:10:08 +09001143 usbhsg_unlock(lock, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001144 /******************** spin unlock ********************/
1145
1146 return 0;
1147}
1148
1149static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
1150{
1151 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1152 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1153 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
1154 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimotocb966322011-04-21 14:10:08 +09001155 spinlock_t *lock;
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001156 unsigned long flags;
1157
1158 /******************** spin lock ********************/
Kuninori Morimotocb966322011-04-21 14:10:08 +09001159 lock = usbhsg_trylock(gpriv, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001160
1161 /*
1162 * disable interrupt and systems if 1st try
1163 */
1164 usbhsg_status_clr(gpriv, status);
1165 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
1166 !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
1167 goto usbhsg_try_stop_unlock;
1168
1169 /* disable all irq */
1170 mod->irq_dev_state = NULL;
1171 mod->irq_ctrl_stage = NULL;
1172 mod->irq_empty = NULL;
1173 mod->irq_ready = NULL;
1174 mod->irq_bempsts = 0;
1175 mod->irq_brdysts = 0;
1176 usbhs_irq_callback_update(priv, mod);
1177
1178 usbhsg_dcp_disable(dcp);
1179
1180 gpriv->gadget.speed = USB_SPEED_UNKNOWN;
1181
1182 /* disable sys */
1183 usbhs_sys_hispeed_ctrl(priv, 0);
1184 usbhs_sys_function_ctrl(priv, 0);
1185 usbhs_sys_usb_ctrl(priv, 0);
1186
Kuninori Morimotocb966322011-04-21 14:10:08 +09001187 usbhsg_unlock(lock, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001188 /******************** spin unlock ********************/
1189
1190 if (gpriv->driver &&
1191 gpriv->driver->disconnect)
1192 gpriv->driver->disconnect(&gpriv->gadget);
1193
1194 dev_dbg(dev, "stop gadget\n");
1195
1196 return 0;
1197
1198usbhsg_try_stop_unlock:
Kuninori Morimotocb966322011-04-21 14:10:08 +09001199 usbhsg_unlock(lock, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001200
1201 return 0;
1202}
1203
1204/*
1205 *
1206 * linux usb function
1207 *
1208 */
1209struct usbhsg_gpriv *the_controller;
1210int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1211 int (*bind)(struct usb_gadget *))
1212{
1213 struct usbhsg_gpriv *gpriv = the_controller;
1214 struct usbhs_priv *priv;
1215 struct device *dev;
1216 int ret;
1217
1218 if (!bind ||
1219 !driver ||
1220 !driver->setup ||
1221 driver->speed != USB_SPEED_HIGH)
1222 return -EINVAL;
1223 if (!gpriv)
1224 return -ENODEV;
1225 if (gpriv->driver)
1226 return -EBUSY;
1227
1228 dev = usbhsg_gpriv_to_dev(gpriv);
1229 priv = usbhsg_gpriv_to_priv(gpriv);
1230
1231 /* first hook up the driver ... */
1232 gpriv->driver = driver;
1233 gpriv->gadget.dev.driver = &driver->driver;
1234
1235 ret = device_add(&gpriv->gadget.dev);
1236 if (ret) {
1237 dev_err(dev, "device_add error %d\n", ret);
1238 goto add_fail;
1239 }
1240
1241 ret = bind(&gpriv->gadget);
1242 if (ret) {
1243 dev_err(dev, "bind to driver %s error %d\n",
1244 driver->driver.name, ret);
1245 goto bind_fail;
1246 }
1247
1248 dev_dbg(dev, "bind %s\n", driver->driver.name);
1249
1250 return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
1251
1252bind_fail:
1253 device_del(&gpriv->gadget.dev);
1254add_fail:
1255 gpriv->driver = NULL;
1256 gpriv->gadget.dev.driver = NULL;
1257
1258 return ret;
1259}
1260EXPORT_SYMBOL(usb_gadget_probe_driver);
1261
1262int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1263{
1264 struct usbhsg_gpriv *gpriv = the_controller;
1265 struct usbhs_priv *priv;
1266 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
1267
1268 if (!gpriv)
1269 return -ENODEV;
1270
1271 if (!driver ||
1272 !driver->unbind ||
1273 driver != gpriv->driver)
1274 return -EINVAL;
1275
1276 dev = usbhsg_gpriv_to_dev(gpriv);
1277 priv = usbhsg_gpriv_to_priv(gpriv);
1278
1279 usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
1280 device_del(&gpriv->gadget.dev);
1281 gpriv->driver = NULL;
1282
1283 if (driver->disconnect)
1284 driver->disconnect(&gpriv->gadget);
1285
1286 driver->unbind(&gpriv->gadget);
1287 dev_dbg(dev, "unbind %s\n", driver->driver.name);
1288
1289 return 0;
1290}
1291EXPORT_SYMBOL(usb_gadget_unregister_driver);
1292
1293/*
1294 * usb gadget ops
1295 */
1296static int usbhsg_get_frame(struct usb_gadget *gadget)
1297{
1298 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
1299 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
1300
1301 return usbhs_frame_get_num(priv);
1302}
1303
1304static struct usb_gadget_ops usbhsg_gadget_ops = {
1305 .get_frame = usbhsg_get_frame,
1306};
1307
1308static int usbhsg_start(struct usbhs_priv *priv)
1309{
1310 return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
1311}
1312
1313static int usbhsg_stop(struct usbhs_priv *priv)
1314{
1315 return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
1316}
1317
1318int __devinit usbhs_mod_gadget_probe(struct usbhs_priv *priv)
1319{
1320 struct usbhsg_gpriv *gpriv;
1321 struct usbhsg_uep *uep;
1322 struct device *dev = usbhs_priv_to_dev(priv);
1323 int pipe_size = usbhs_get_dparam(priv, pipe_size);
1324 int i;
1325
1326 gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
1327 if (!gpriv) {
1328 dev_err(dev, "Could not allocate gadget priv\n");
1329 return -ENOMEM;
1330 }
1331
1332 uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
1333 if (!uep) {
1334 dev_err(dev, "Could not allocate ep\n");
1335 goto usbhs_mod_gadget_probe_err_gpriv;
1336 }
1337
1338 /*
1339 * CAUTION
1340 *
1341 * There is no guarantee that it is possible to access usb module here.
1342 * Don't accesses to it.
1343 * The accesse will be enable after "usbhsg_start"
1344 */
1345
1346 /*
1347 * register itself
1348 */
1349 usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
1350
1351 /* init gpriv */
1352 gpriv->mod.name = "gadget";
1353 gpriv->mod.start = usbhsg_start;
1354 gpriv->mod.stop = usbhsg_stop;
1355 gpriv->uep = uep;
1356 gpriv->uep_size = pipe_size;
1357 usbhsg_status_init(gpriv);
1358
1359 /*
1360 * init gadget
1361 */
1362 device_initialize(&gpriv->gadget.dev);
1363 dev_set_name(&gpriv->gadget.dev, "gadget");
1364 gpriv->gadget.dev.parent = dev;
1365 gpriv->gadget.name = "renesas_usbhs_udc";
1366 gpriv->gadget.ops = &usbhsg_gadget_ops;
1367 gpriv->gadget.is_dualspeed = 1;
1368
1369 INIT_LIST_HEAD(&gpriv->gadget.ep_list);
1370
1371 /*
1372 * init usb_ep
1373 */
1374 usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
1375 uep->gpriv = gpriv;
1376 snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
1377
1378 uep->ep.name = uep->ep_name;
1379 uep->ep.ops = &usbhsg_ep_ops;
1380 INIT_LIST_HEAD(&uep->ep.ep_list);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001381
1382 /* init DCP */
1383 if (usbhsg_is_dcp(uep)) {
1384 gpriv->gadget.ep0 = &uep->ep;
1385 uep->ep.maxpacket = 64;
1386 }
1387 /* init normal pipe */
1388 else {
1389 uep->ep.maxpacket = 512;
1390 list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
1391 }
1392 }
1393
1394 the_controller = gpriv;
1395
1396 dev_info(dev, "gadget probed\n");
1397
1398 return 0;
1399
1400usbhs_mod_gadget_probe_err_gpriv:
1401 kfree(gpriv);
1402
1403 return -ENOMEM;
1404}
1405
1406void __devexit usbhs_mod_gadget_remove(struct usbhs_priv *priv)
1407{
1408 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1409
1410 kfree(gpriv);
1411}