4
* Copyright (C) 2011 Renesas Solutions Corp.
5
* Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
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.
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
18
#include <linux/module.h>
19
#include <linux/platform_device.h>
20
#include <linux/usb/ch9.h>
21
#include <linux/usb/gadget.h>
27
struct usbhsg_request {
28
struct usb_request req;
29
struct list_head node;
32
#define EP_NAME_SIZE 8
34
struct usbhsg_pipe_handle;
37
struct usbhs_pipe *pipe;
38
struct list_head list;
40
char ep_name[EP_NAME_SIZE];
42
struct usbhsg_gpriv *gpriv;
43
struct usbhsg_pipe_handle *handler;
47
struct usb_gadget gadget;
50
struct usbhsg_uep *uep;
53
struct usb_gadget_driver *driver;
56
#define USBHSG_STATUS_STARTED (1 << 0)
57
#define USBHSG_STATUS_REGISTERD (1 << 1)
58
#define USBHSG_STATUS_WEDGE (1 << 2)
61
struct usbhsg_pipe_handle {
62
int (*prepare)(struct usbhsg_uep *uep, struct usbhsg_request *ureq);
63
int (*try_run)(struct usbhsg_uep *uep, struct usbhsg_request *ureq);
64
void (*irq_mask)(struct usbhsg_uep *uep, int enable);
67
struct usbhsg_recip_handle {
69
int (*device)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
70
struct usb_ctrlrequest *ctrl);
71
int (*interface)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
72
struct usb_ctrlrequest *ctrl);
73
int (*endpoint)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
74
struct usb_ctrlrequest *ctrl);
80
#define usbhsg_priv_to_gpriv(priv) \
82
usbhs_mod_get(priv, USBHS_GADGET), \
83
struct usbhsg_gpriv, mod)
85
#define __usbhsg_for_each_uep(start, pos, g, i) \
86
for (i = start, pos = (g)->uep; \
88
i++, pos = (g)->uep + i)
90
#define usbhsg_for_each_uep(pos, gpriv, i) \
91
__usbhsg_for_each_uep(1, pos, gpriv, i)
93
#define usbhsg_for_each_uep_with_dcp(pos, gpriv, i) \
94
__usbhsg_for_each_uep(0, pos, gpriv, i)
96
#define usbhsg_gadget_to_gpriv(g)\
97
container_of(g, struct usbhsg_gpriv, gadget)
99
#define usbhsg_req_to_ureq(r)\
100
container_of(r, struct usbhsg_request, req)
102
#define usbhsg_ep_to_uep(e) container_of(e, struct usbhsg_uep, ep)
103
#define usbhsg_gpriv_to_lock(gp) usbhs_priv_to_lock((gp)->mod.priv)
104
#define usbhsg_gpriv_to_dev(gp) usbhs_priv_to_dev((gp)->mod.priv)
105
#define usbhsg_gpriv_to_priv(gp) ((gp)->mod.priv)
106
#define usbhsg_gpriv_to_dcp(gp) ((gp)->uep)
107
#define usbhsg_gpriv_to_nth_uep(gp, i) ((gp)->uep + i)
108
#define usbhsg_uep_to_gpriv(u) ((u)->gpriv)
109
#define usbhsg_uep_to_pipe(u) ((u)->pipe)
110
#define usbhsg_pipe_to_uep(p) ((p)->mod_private)
111
#define usbhsg_is_dcp(u) ((u) == usbhsg_gpriv_to_dcp((u)->gpriv))
113
#define usbhsg_is_not_connected(gp) ((gp)->gadget.speed == USB_SPEED_UNKNOWN)
116
#define usbhsg_status_init(gp) do {(gp)->status = 0; } while (0)
117
#define usbhsg_status_set(gp, b) (gp->status |= b)
118
#define usbhsg_status_clr(gp, b) (gp->status &= ~b)
119
#define usbhsg_status_has(gp, b) (gp->status & b)
124
* This driver don't use spin_try_lock
125
* to avoid warning of CONFIG_DEBUG_SPINLOCK
127
static spinlock_t *usbhsg_trylock(struct usbhsg_gpriv *gpriv,
128
unsigned long *flags)
130
spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
132
/* check spin lock status
133
* to avoid deadlock/nest */
134
if (spin_is_locked(lock))
137
spin_lock_irqsave(lock, *flags);
142
static void usbhsg_unlock(spinlock_t *lock, unsigned long *flags)
147
spin_unlock_irqrestore(lock, *flags);
153
static void usbhsg_queue_push(struct usbhsg_uep *uep,
154
struct usbhsg_request *ureq)
156
struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
157
struct device *dev = usbhsg_gpriv_to_dev(gpriv);
158
struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
161
********* assume under spin lock *********
163
list_del_init(&ureq->node);
164
list_add_tail(&ureq->node, &uep->list);
165
ureq->req.actual = 0;
166
ureq->req.status = -EINPROGRESS;
168
dev_dbg(dev, "pipe %d : queue push (%d)\n",
169
usbhs_pipe_number(pipe),
173
static struct usbhsg_request *usbhsg_queue_get(struct usbhsg_uep *uep)
176
********* assume under spin lock *********
178
if (list_empty(&uep->list))
181
return list_entry(uep->list.next, struct usbhsg_request, node);
184
#define usbhsg_queue_prepare(uep) __usbhsg_queue_handler(uep, 1);
185
#define usbhsg_queue_handle(uep) __usbhsg_queue_handler(uep, 0);
186
static int __usbhsg_queue_handler(struct usbhsg_uep *uep, int prepare)
188
struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
189
struct device *dev = usbhsg_gpriv_to_dev(gpriv);
190
struct usbhsg_request *ureq;
196
dev_err(dev, "no handler function\n");
201
* CAUTION [*queue handler*]
203
* This function will be called for start/restart queue operation.
204
* OTOH the most much worry for USB driver is spinlock nest.
206
* - usb_ep_ops :: queue
207
* - usb_request :: complete
209
* But the caller of this function need not care about spinlock.
210
* This function is using usbhsg_trylock for it.
211
* if "is_locked" is 1, this mean this function lock it.
212
* but if it is 0, this mean it is already under spin lock.
214
* CAUTION [*endpoint queue*]
215
* CAUTION [*request complete*]
218
/****************** spin try lock *******************/
219
lock = usbhsg_trylock(gpriv, &flags);
221
ureq = usbhsg_queue_get(uep);
224
ret = uep->handler->prepare(uep, ureq);
226
ret = uep->handler->try_run(uep, ureq);
228
usbhsg_unlock(lock, &flags);
229
/******************** spin unlock ******************/
234
static void usbhsg_queue_pop(struct usbhsg_uep *uep,
235
struct usbhsg_request *ureq,
238
struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
239
struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
240
struct device *dev = usbhsg_gpriv_to_dev(gpriv);
243
********* assume under spin lock *********
247
* CAUTION [*request complete*]
249
* There is a possibility not to be called in correct order
250
* if "complete" is called without spinlock.
252
* So, this function assume it is under spinlock,
253
* and call usb_request :: complete.
255
* But this "complete" will push next usb_request.
256
* It mean "usb_ep_ops :: queue" which is using spinlock is called
259
* To avoid dead-lock, this driver is using usbhsg_trylock.
260
* CAUTION [*endpoint queue*]
261
* CAUTION [*queue handler*]
264
dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
266
list_del_init(&ureq->node);
268
ureq->req.status = status;
269
ureq->req.complete(&uep->ep, &ureq->req);
273
usbhsg_queue_prepare(uep);
277
* irq enable/disable function
279
#define usbhsg_irq_callback_ctrl(uep, status, enable) \
281
struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); \
282
struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); \
283
struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); \
284
struct usbhs_mod *mod = usbhs_mod_get_current(priv); \
288
mod->irq_##status |= (1 << usbhs_pipe_number(pipe)); \
290
mod->irq_##status &= ~(1 << usbhs_pipe_number(pipe)); \
291
usbhs_irq_callback_update(priv, mod); \
294
static void usbhsg_irq_empty_ctrl(struct usbhsg_uep *uep, int enable)
296
usbhsg_irq_callback_ctrl(uep, bempsts, enable);
299
static void usbhsg_irq_ready_ctrl(struct usbhsg_uep *uep, int enable)
301
usbhsg_irq_callback_ctrl(uep, brdysts, enable);
307
static int usbhsg_try_run_ctrl_stage_end(struct usbhsg_uep *uep,
308
struct usbhsg_request *ureq)
310
struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
313
********* assume under spin lock *********
316
usbhs_dcp_control_transfer_done(pipe);
317
usbhsg_queue_pop(uep, ureq, 0);
322
static int usbhsg_try_run_send_packet(struct usbhsg_uep *uep,
323
struct usbhsg_request *ureq)
325
struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
326
struct usb_request *req = &ureq->req;
327
struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
328
struct device *dev = usbhsg_gpriv_to_dev(gpriv);
336
********* assume under spin lock *********
339
maxp = usbhs_pipe_get_maxpacket(pipe);
340
buf = req->buf + req->actual;
341
remainder = req->length - req->actual;
343
send = usbhs_fifo_write(pipe, buf, remainder);
346
* send < 0 : pipe busy
347
* send = 0 : send zero packet
348
* send > 0 : send data
355
/* send all packet ? */
356
if (send < remainder)
357
is_done = 0; /* there are remainder data */
358
else if (send < maxp)
359
is_done = 1; /* short packet */
361
is_done = !req->zero; /* send zero packet ? */
363
dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n",
364
usbhs_pipe_number(pipe),
365
remainder, send, is_done, req->zero);
368
* enable interrupt and send again in irq handler
369
* if it still have remainder data which should be sent.
372
uep->handler->irq_mask(uep, enable);
375
* usbhs_fifo_enable execute
376
* - after callback_update,
377
* - before queue_pop / stage_end
379
usbhs_fifo_enable(pipe);
382
* all data were sent ?
385
/* it care below call in
387
if (usbhsg_is_dcp(uep))
388
usbhs_dcp_control_transfer_done(pipe);
390
usbhsg_queue_pop(uep, ureq, 0);
396
static int usbhsg_prepare_send_packet(struct usbhsg_uep *uep,
397
struct usbhsg_request *ureq)
399
struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
402
********* assume under spin lock *********
405
usbhs_fifo_prepare_write(pipe);
406
usbhsg_try_run_send_packet(uep, ureq);
411
static int usbhsg_try_run_receive_packet(struct usbhsg_uep *uep,
412
struct usbhsg_request *ureq)
414
struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
415
struct usb_request *req = &ureq->req;
416
struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
417
struct device *dev = usbhsg_gpriv_to_dev(gpriv);
424
********* assume under spin lock *********
427
maxp = usbhs_pipe_get_maxpacket(pipe);
428
buf = req->buf + req->actual;
429
remainder = req->length - req->actual;
431
recv = usbhs_fifo_read(pipe, buf, remainder);
433
* recv < 0 : pipe busy
434
* recv >= 0 : receive data
441
/* update parameters */
444
if ((recv == remainder) || /* receive all data */
445
(recv < maxp)) /* short packet */
448
dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n",
449
usbhs_pipe_number(pipe),
450
remainder, recv, is_done, req->zero);
452
/* read all data ? */
456
uep->handler->irq_mask(uep, disable);
457
usbhs_fifo_disable(pipe);
458
usbhsg_queue_pop(uep, ureq, 0);
464
static int usbhsg_prepare_receive_packet(struct usbhsg_uep *uep,
465
struct usbhsg_request *ureq)
467
struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
472
********* assume under spin lock *********
475
ret = usbhs_fifo_prepare_read(pipe);
480
* data will be read in interrupt handler
482
uep->handler->irq_mask(uep, enable);
487
static struct usbhsg_pipe_handle usbhsg_handler_send_by_empty = {
488
.prepare = usbhsg_prepare_send_packet,
489
.try_run = usbhsg_try_run_send_packet,
490
.irq_mask = usbhsg_irq_empty_ctrl,
493
static struct usbhsg_pipe_handle usbhsg_handler_send_by_ready = {
494
.prepare = usbhsg_prepare_send_packet,
495
.try_run = usbhsg_try_run_send_packet,
496
.irq_mask = usbhsg_irq_ready_ctrl,
499
static struct usbhsg_pipe_handle usbhsg_handler_recv_by_ready = {
500
.prepare = usbhsg_prepare_receive_packet,
501
.try_run = usbhsg_try_run_receive_packet,
502
.irq_mask = usbhsg_irq_ready_ctrl,
505
static struct usbhsg_pipe_handle usbhsg_handler_ctrl_stage_end = {
506
.prepare = usbhsg_try_run_ctrl_stage_end,
507
.try_run = usbhsg_try_run_ctrl_stage_end,
511
* DCP pipe can NOT use "ready interrupt" for "send"
512
* it should use "empty" interrupt.
514
* "Operation" - "Interrupt Function" - "BRDY Interrupt"
516
* on the other hand, normal pipe can use "ready interrupt" for "send"
517
* even though it is single/double buffer
519
#define usbhsg_handler_send_ctrl usbhsg_handler_send_by_empty
520
#define usbhsg_handler_recv_ctrl usbhsg_handler_recv_by_ready
522
#define usbhsg_handler_send_packet usbhsg_handler_send_by_ready
523
#define usbhsg_handler_recv_packet usbhsg_handler_recv_by_ready
526
* USB_TYPE_STANDARD / clear feature functions
528
static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
529
struct usbhsg_uep *uep,
530
struct usb_ctrlrequest *ctrl)
532
struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
533
struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
534
struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
536
usbhs_dcp_control_transfer_done(pipe);
541
static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
542
struct usbhsg_uep *uep,
543
struct usb_ctrlrequest *ctrl)
545
struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
546
struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
548
if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
549
usbhs_fifo_disable(pipe);
550
usbhs_pipe_clear_sequence(pipe);
551
usbhs_fifo_enable(pipe);
554
usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
556
usbhsg_queue_prepare(uep);
561
struct usbhsg_recip_handle req_clear_feature = {
562
.name = "clear feature",
563
.device = usbhsg_recip_handler_std_control_done,
564
.interface = usbhsg_recip_handler_std_control_done,
565
.endpoint = usbhsg_recip_handler_std_clear_endpoint,
571
static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
572
struct usbhsg_recip_handle *handler,
573
struct usb_ctrlrequest *ctrl)
575
struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
576
struct device *dev = usbhsg_gpriv_to_dev(gpriv);
577
struct usbhsg_uep *uep;
578
int recip = ctrl->bRequestType & USB_RECIP_MASK;
579
int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
581
int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
582
struct usb_ctrlrequest *ctrl);
585
uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
586
if (!usbhsg_uep_to_pipe(uep)) {
587
dev_err(dev, "wrong recip request\n");
592
case USB_RECIP_DEVICE:
594
func = handler->device;
596
case USB_RECIP_INTERFACE:
598
func = handler->interface;
600
case USB_RECIP_ENDPOINT:
602
func = handler->endpoint;
605
dev_warn(dev, "unsupported RECIP(%d)\n", recip);
611
dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
612
ret = func(priv, uep, ctrl);
621
* it will be called from usbhs_interrupt
623
static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
624
struct usbhs_irq_state *irq_state)
626
struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
627
struct device *dev = usbhsg_gpriv_to_dev(gpriv);
629
gpriv->gadget.speed = usbhs_status_get_usb_speed(irq_state);
631
dev_dbg(dev, "state = %x : speed : %d\n",
632
usbhs_status_get_device_state(irq_state),
633
gpriv->gadget.speed);
638
static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
639
struct usbhs_irq_state *irq_state)
641
struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
642
struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
643
struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
644
struct device *dev = usbhsg_gpriv_to_dev(gpriv);
645
struct usb_ctrlrequest ctrl;
646
struct usbhsg_recip_handle *recip_handler = NULL;
647
int stage = usbhs_status_get_ctrl_stage(irq_state);
650
dev_dbg(dev, "stage = %d\n", stage);
656
* - "Interrupt Function"
657
* - "Control Transfer Stage Transition Interrupt"
658
* - Fig. "Control Transfer Stage Transitions"
662
case READ_DATA_STAGE:
663
dcp->handler = &usbhsg_handler_send_ctrl;
665
case WRITE_DATA_STAGE:
666
dcp->handler = &usbhsg_handler_recv_ctrl;
668
case NODATA_STATUS_STAGE:
669
dcp->handler = &usbhsg_handler_ctrl_stage_end;
678
usbhs_usbreq_get_val(priv, &ctrl);
680
switch (ctrl.bRequestType & USB_TYPE_MASK) {
681
case USB_TYPE_STANDARD:
682
switch (ctrl.bRequest) {
683
case USB_REQ_CLEAR_FEATURE:
684
recip_handler = &req_clear_feature;
690
* setup stage / run recip
693
ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
695
ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
698
usbhs_fifo_stall(pipe);
703
static int usbhsg_irq_empty(struct usbhs_priv *priv,
704
struct usbhs_irq_state *irq_state)
706
struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
707
struct usbhsg_uep *uep;
708
struct usbhs_pipe *pipe;
709
struct device *dev = usbhsg_gpriv_to_dev(gpriv);
712
if (!irq_state->bempsts) {
713
dev_err(dev, "debug %s !!\n", __func__);
717
dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
720
* search interrupted "pipe"
723
usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
724
if (!(irq_state->bempsts & (1 << i)))
727
uep = usbhsg_pipe_to_uep(pipe);
728
ret = usbhsg_queue_handle(uep);
730
dev_err(dev, "send error %d : %d\n", i, ret);
736
static int usbhsg_irq_ready(struct usbhs_priv *priv,
737
struct usbhs_irq_state *irq_state)
739
struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
740
struct usbhsg_uep *uep;
741
struct usbhs_pipe *pipe;
742
struct device *dev = usbhsg_gpriv_to_dev(gpriv);
745
if (!irq_state->brdysts) {
746
dev_err(dev, "debug %s !!\n", __func__);
750
dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
753
* search interrupted "pipe"
756
usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
757
if (!(irq_state->brdysts & (1 << i)))
760
uep = usbhsg_pipe_to_uep(pipe);
761
ret = usbhsg_queue_handle(uep);
763
dev_err(dev, "receive error %d : %d\n", i, ret);
774
static int usbhsg_dcp_enable(struct usbhsg_uep *uep)
776
struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
777
struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
778
struct usbhs_pipe *pipe;
781
********* assume under spin lock *********
784
pipe = usbhs_dcp_malloc(priv);
789
uep->pipe->mod_private = uep;
790
INIT_LIST_HEAD(&uep->list);
795
#define usbhsg_dcp_disable usbhsg_pipe_disable
796
static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
798
struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
799
struct usbhsg_request *ureq;
803
********* assume under spin lock *********
806
usbhs_fifo_disable(pipe);
811
usbhsg_irq_empty_ctrl(uep, disable);
812
usbhsg_irq_ready_ctrl(uep, disable);
815
ureq = usbhsg_queue_get(uep);
819
usbhsg_queue_pop(uep, ureq, -ECONNRESET);
825
static void usbhsg_uep_init(struct usbhsg_gpriv *gpriv)
828
struct usbhsg_uep *uep;
830
usbhsg_for_each_uep_with_dcp(uep, gpriv, i)
839
static int usbhsg_ep_enable(struct usb_ep *ep,
840
const struct usb_endpoint_descriptor *desc)
842
struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
843
struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
844
struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
845
struct usbhs_pipe *pipe;
851
* if it already have pipe,
857
/******************** spin lock ********************/
858
lock = usbhsg_trylock(gpriv, &flags);
860
pipe = usbhs_pipe_malloc(priv, desc);
863
pipe->mod_private = uep;
864
INIT_LIST_HEAD(&uep->list);
866
if (usb_endpoint_dir_in(desc))
867
uep->handler = &usbhsg_handler_send_packet;
869
uep->handler = &usbhsg_handler_recv_packet;
874
usbhsg_unlock(lock, &flags);
875
/******************** spin unlock ******************/
880
static int usbhsg_ep_disable(struct usb_ep *ep)
882
struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
883
struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
888
/******************** spin lock ********************/
889
lock = usbhsg_trylock(gpriv, &flags);
891
ret = usbhsg_pipe_disable(uep);
893
usbhsg_unlock(lock, &flags);
894
/******************** spin unlock ******************/
899
static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
902
struct usbhsg_request *ureq;
904
ureq = kzalloc(sizeof *ureq, gfp_flags);
908
INIT_LIST_HEAD(&ureq->node);
912
static void usbhsg_ep_free_request(struct usb_ep *ep,
913
struct usb_request *req)
915
struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
917
WARN_ON(!list_empty(&ureq->node));
921
static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
924
struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
925
struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
926
struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
927
struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
933
* CAUTION [*endpoint queue*]
935
* This function will be called from usb_request :: complete
936
* or usb driver timing.
937
* If this function is called from usb_request :: complete,
938
* it is already under spinlock on this driver.
939
* but it is called frm usb driver, this function should call spinlock.
941
* This function is using usbshg_trylock to solve this issue.
942
* if "is_locked" is 1, this mean this function lock it.
943
* but if it is 0, this mean it is already under spin lock.
945
* CAUTION [*queue handler*]
946
* CAUTION [*request complete*]
949
/******************** spin lock ********************/
950
lock = usbhsg_trylock(gpriv, &flags);
953
if (usbhsg_is_not_connected(gpriv) ||
954
unlikely(!gpriv->driver) ||
958
usbhsg_queue_push(uep, ureq);
960
usbhsg_unlock(lock, &flags);
961
/******************** spin unlock ******************/
963
usbhsg_queue_prepare(uep);
968
static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
970
struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
971
struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
972
struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
978
* CAUTION [*queue handler*]
979
* CAUTION [*endpoint queue*]
980
* CAUTION [*request complete*]
983
/******************** spin lock ********************/
984
lock = usbhsg_trylock(gpriv, &flags);
986
usbhsg_queue_pop(uep, ureq, -ECONNRESET);
988
usbhsg_unlock(lock, &flags);
989
/******************** spin unlock ******************/
994
static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
996
struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
997
struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
998
struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
999
struct device *dev = usbhsg_gpriv_to_dev(gpriv);
1001
unsigned long flags;
1006
* CAUTION [*queue handler*]
1007
* CAUTION [*endpoint queue*]
1008
* CAUTION [*request complete*]
1011
/******************** spin lock ********************/
1012
lock = usbhsg_trylock(gpriv, &flags);
1013
if (!usbhsg_queue_get(uep)) {
1015
dev_dbg(dev, "set halt %d (pipe %d)\n",
1016
halt, usbhs_pipe_number(pipe));
1019
usbhs_fifo_stall(pipe);
1021
usbhs_fifo_disable(pipe);
1024
usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
1026
usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
1031
usbhsg_unlock(lock, &flags);
1032
/******************** spin unlock ******************/
1037
static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
1039
return __usbhsg_ep_set_halt_wedge(ep, value, 0);
1042
static int usbhsg_ep_set_wedge(struct usb_ep *ep)
1044
return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
1047
static struct usb_ep_ops usbhsg_ep_ops = {
1048
.enable = usbhsg_ep_enable,
1049
.disable = usbhsg_ep_disable,
1051
.alloc_request = usbhsg_ep_alloc_request,
1052
.free_request = usbhsg_ep_free_request,
1054
.queue = usbhsg_ep_queue,
1055
.dequeue = usbhsg_ep_dequeue,
1057
.set_halt = usbhsg_ep_set_halt,
1058
.set_wedge = usbhsg_ep_set_wedge,
1062
* usb module start/end
1064
static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
1066
struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1067
struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
1068
struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1069
struct device *dev = usbhs_priv_to_dev(priv);
1071
unsigned long flags;
1073
/******************** spin lock ********************/
1074
lock = usbhsg_trylock(gpriv, &flags);
1077
* enable interrupt and systems if ready
1079
usbhsg_status_set(gpriv, status);
1080
if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
1081
usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
1082
goto usbhsg_try_start_unlock;
1084
dev_dbg(dev, "start gadget\n");
1087
* pipe initialize and enable DCP
1089
usbhs_pipe_init(priv);
1090
usbhsg_uep_init(gpriv);
1091
usbhsg_dcp_enable(dcp);
1094
* system config enble
1099
usbhs_sys_hispeed_ctrl(priv, 1);
1100
usbhs_sys_function_ctrl(priv, 1);
1101
usbhs_sys_usb_ctrl(priv, 1);
1104
* enable irq callback
1106
mod->irq_dev_state = usbhsg_irq_dev_state;
1107
mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
1108
mod->irq_empty = usbhsg_irq_empty;
1109
mod->irq_ready = usbhsg_irq_ready;
1110
mod->irq_bempsts = 0;
1111
mod->irq_brdysts = 0;
1112
usbhs_irq_callback_update(priv, mod);
1114
usbhsg_try_start_unlock:
1115
usbhsg_unlock(lock, &flags);
1116
/******************** spin unlock ********************/
1121
static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
1123
struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1124
struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1125
struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
1126
struct device *dev = usbhs_priv_to_dev(priv);
1128
unsigned long flags;
1130
/******************** spin lock ********************/
1131
lock = usbhsg_trylock(gpriv, &flags);
1134
* disable interrupt and systems if 1st try
1136
usbhsg_status_clr(gpriv, status);
1137
if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
1138
!usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
1139
goto usbhsg_try_stop_unlock;
1141
/* disable all irq */
1142
mod->irq_dev_state = NULL;
1143
mod->irq_ctrl_stage = NULL;
1144
mod->irq_empty = NULL;
1145
mod->irq_ready = NULL;
1146
mod->irq_bempsts = 0;
1147
mod->irq_brdysts = 0;
1148
usbhs_irq_callback_update(priv, mod);
1150
usbhsg_dcp_disable(dcp);
1152
gpriv->gadget.speed = USB_SPEED_UNKNOWN;
1155
usbhs_sys_hispeed_ctrl(priv, 0);
1156
usbhs_sys_function_ctrl(priv, 0);
1157
usbhs_sys_usb_ctrl(priv, 0);
1159
usbhsg_unlock(lock, &flags);
1160
/******************** spin unlock ********************/
1162
if (gpriv->driver &&
1163
gpriv->driver->disconnect)
1164
gpriv->driver->disconnect(&gpriv->gadget);
1166
dev_dbg(dev, "stop gadget\n");
1170
usbhsg_try_stop_unlock:
1171
usbhsg_unlock(lock, &flags);
1178
* linux usb function
1181
struct usbhsg_gpriv *the_controller;
1182
int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1183
int (*bind)(struct usb_gadget *))
1185
struct usbhsg_gpriv *gpriv = the_controller;
1186
struct usbhs_priv *priv;
1193
driver->speed != USB_SPEED_HIGH)
1200
dev = usbhsg_gpriv_to_dev(gpriv);
1201
priv = usbhsg_gpriv_to_priv(gpriv);
1203
/* first hook up the driver ... */
1204
gpriv->driver = driver;
1205
gpriv->gadget.dev.driver = &driver->driver;
1207
ret = device_add(&gpriv->gadget.dev);
1209
dev_err(dev, "device_add error %d\n", ret);
1213
ret = bind(&gpriv->gadget);
1215
dev_err(dev, "bind to driver %s error %d\n",
1216
driver->driver.name, ret);
1220
dev_dbg(dev, "bind %s\n", driver->driver.name);
1222
return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
1225
device_del(&gpriv->gadget.dev);
1227
gpriv->driver = NULL;
1228
gpriv->gadget.dev.driver = NULL;
1232
EXPORT_SYMBOL(usb_gadget_probe_driver);
1234
int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1236
struct usbhsg_gpriv *gpriv = the_controller;
1237
struct usbhs_priv *priv;
1238
struct device *dev = usbhsg_gpriv_to_dev(gpriv);
1245
driver != gpriv->driver)
1248
dev = usbhsg_gpriv_to_dev(gpriv);
1249
priv = usbhsg_gpriv_to_priv(gpriv);
1251
usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
1252
device_del(&gpriv->gadget.dev);
1253
gpriv->driver = NULL;
1255
if (driver->disconnect)
1256
driver->disconnect(&gpriv->gadget);
1258
driver->unbind(&gpriv->gadget);
1259
dev_dbg(dev, "unbind %s\n", driver->driver.name);
1263
EXPORT_SYMBOL(usb_gadget_unregister_driver);
1268
static int usbhsg_get_frame(struct usb_gadget *gadget)
1270
struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
1271
struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
1273
return usbhs_frame_get_num(priv);
1276
static struct usb_gadget_ops usbhsg_gadget_ops = {
1277
.get_frame = usbhsg_get_frame,
1280
static int usbhsg_start(struct usbhs_priv *priv)
1282
return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
1285
static int usbhsg_stop(struct usbhs_priv *priv)
1287
return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
1290
int __devinit usbhs_mod_gadget_probe(struct usbhs_priv *priv)
1292
struct usbhsg_gpriv *gpriv;
1293
struct usbhsg_uep *uep;
1294
struct device *dev = usbhs_priv_to_dev(priv);
1295
int pipe_size = usbhs_get_dparam(priv, pipe_size);
1298
gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
1300
dev_err(dev, "Could not allocate gadget priv\n");
1304
uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
1306
dev_err(dev, "Could not allocate ep\n");
1307
goto usbhs_mod_gadget_probe_err_gpriv;
1313
* There is no guarantee that it is possible to access usb module here.
1314
* Don't accesses to it.
1315
* The accesse will be enable after "usbhsg_start"
1321
usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
1324
gpriv->mod.name = "gadget";
1325
gpriv->mod.start = usbhsg_start;
1326
gpriv->mod.stop = usbhsg_stop;
1328
gpriv->uep_size = pipe_size;
1329
usbhsg_status_init(gpriv);
1334
device_initialize(&gpriv->gadget.dev);
1335
dev_set_name(&gpriv->gadget.dev, "gadget");
1336
gpriv->gadget.dev.parent = dev;
1337
gpriv->gadget.name = "renesas_usbhs_udc";
1338
gpriv->gadget.ops = &usbhsg_gadget_ops;
1339
gpriv->gadget.is_dualspeed = 1;
1341
INIT_LIST_HEAD(&gpriv->gadget.ep_list);
1346
usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
1348
snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
1350
uep->ep.name = uep->ep_name;
1351
uep->ep.ops = &usbhsg_ep_ops;
1352
INIT_LIST_HEAD(&uep->ep.ep_list);
1353
INIT_LIST_HEAD(&uep->list);
1356
if (usbhsg_is_dcp(uep)) {
1357
gpriv->gadget.ep0 = &uep->ep;
1358
uep->ep.maxpacket = 64;
1360
/* init normal pipe */
1362
uep->ep.maxpacket = 512;
1363
list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
1367
the_controller = gpriv;
1369
dev_info(dev, "gadget probed\n");
1373
usbhs_mod_gadget_probe_err_gpriv:
1379
void __devexit usbhs_mod_gadget_remove(struct usbhs_priv *priv)
1381
struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);