2 * uvc_status.c -- USB Video Class driver - Status endpoint
4 * Copyright (C) 2007-2009
5 * Laurent Pinchart (laurent.pinchart@skynet.be)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
14 #include <linux/kernel.h>
15 #include <linux/input.h>
16 #include <linux/usb.h>
17 #include <linux/usb/input.h>
21 /* --------------------------------------------------------------------------
24 #ifdef CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV
25 static int uvc_input_init(struct uvc_device
*dev
)
27 struct usb_device
*udev
= dev
->udev
;
28 struct input_dev
*input
;
32 input
= input_allocate_device();
36 phys
= kmalloc(6 + strlen(udev
->bus
->bus_name
) + strlen(udev
->devpath
),
42 sprintf(phys
, "usb-%s-%s", udev
->bus
->bus_name
, udev
->devpath
);
44 input
->name
= dev
->name
;
46 usb_to_input_id(udev
, &input
->id
);
47 input
->dev
.parent
= &dev
->intf
->dev
;
49 set_bit(EV_KEY
, input
->evbit
);
50 set_bit(BTN_0
, input
->keybit
);
52 if ((ret
= input_register_device(input
)) < 0)
59 input_free_device(input
);
64 static void uvc_input_cleanup(struct uvc_device
*dev
)
67 input_unregister_device(dev
->input
);
70 static void uvc_input_report_key(struct uvc_device
*dev
, unsigned int code
,
74 input_report_key(dev
->input
, code
, value
);
78 #define uvc_input_init(dev)
79 #define uvc_input_cleanup(dev)
80 #define uvc_input_report_key(dev, code, value)
81 #endif /* CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV */
83 /* --------------------------------------------------------------------------
84 * Status interrupt endpoint
86 static void uvc_event_streaming(struct uvc_device
*dev
, __u8
*data
, int len
)
89 uvc_trace(UVC_TRACE_STATUS
, "Invalid streaming status event "
97 uvc_trace(UVC_TRACE_STATUS
, "Button (intf %u) %s len %d\n",
98 data
[1], data
[3] ? "pressed" : "released", len
);
99 uvc_input_report_key(dev
, BTN_0
, data
[3]);
101 uvc_trace(UVC_TRACE_STATUS
, "Stream %u error event %02x %02x "
102 "len %d.\n", data
[1], data
[2], data
[3], len
);
106 static void uvc_event_control(struct uvc_device
*dev
, __u8
*data
, int len
)
108 char *attrs
[3] = { "value", "info", "failure" };
110 if (len
< 6 || data
[2] != 0 || data
[4] > 2) {
111 uvc_trace(UVC_TRACE_STATUS
, "Invalid control status event "
116 uvc_trace(UVC_TRACE_STATUS
, "Control %u/%u %s change len %d.\n",
117 data
[1], data
[3], attrs
[data
[4]], len
);
120 static void uvc_status_complete(struct urb
*urb
)
122 struct uvc_device
*dev
= urb
->context
;
125 switch (urb
->status
) {
129 case -ENOENT
: /* usb_kill_urb() called. */
130 case -ECONNRESET
: /* usb_unlink_urb() called. */
131 case -ESHUTDOWN
: /* The endpoint is being disabled. */
132 case -EPROTO
: /* Device is disconnected (reported by some
133 * host controller). */
137 uvc_printk(KERN_WARNING
, "Non-zero status (%d) in status "
138 "completion handler.\n", urb
->status
);
142 len
= urb
->actual_length
;
144 switch (dev
->status
[0] & 0x0f) {
145 case UVC_STATUS_TYPE_CONTROL
:
146 uvc_event_control(dev
, dev
->status
, len
);
149 case UVC_STATUS_TYPE_STREAMING
:
150 uvc_event_streaming(dev
, dev
->status
, len
);
154 uvc_printk(KERN_INFO
, "unknown event type %u.\n",
160 /* Resubmit the URB. */
161 urb
->interval
= dev
->int_ep
->desc
.bInterval
;
162 if ((ret
= usb_submit_urb(urb
, GFP_ATOMIC
)) < 0) {
163 uvc_printk(KERN_ERR
, "Failed to resubmit status URB (%d).\n",
168 int uvc_status_init(struct uvc_device
*dev
)
170 struct usb_host_endpoint
*ep
= dev
->int_ep
;
179 dev
->status
= kzalloc(UVC_MAX_STATUS_SIZE
, GFP_KERNEL
);
180 if (dev
->status
== NULL
)
183 dev
->int_urb
= usb_alloc_urb(0, GFP_KERNEL
);
184 if (dev
->int_urb
== NULL
) {
189 pipe
= usb_rcvintpipe(dev
->udev
, ep
->desc
.bEndpointAddress
);
191 /* For high-speed interrupt endpoints, the bInterval value is used as
192 * an exponent of two. Some developers forgot about it.
194 interval
= ep
->desc
.bInterval
;
195 if (interval
> 16 && dev
->udev
->speed
== USB_SPEED_HIGH
&&
196 (dev
->quirks
& UVC_QUIRK_STATUS_INTERVAL
))
197 interval
= fls(interval
) - 1;
199 usb_fill_int_urb(dev
->int_urb
, dev
->udev
, pipe
,
200 dev
->status
, UVC_MAX_STATUS_SIZE
, uvc_status_complete
,
203 return usb_submit_urb(dev
->int_urb
, GFP_KERNEL
);
206 void uvc_status_cleanup(struct uvc_device
*dev
)
208 usb_kill_urb(dev
->int_urb
);
209 usb_free_urb(dev
->int_urb
);
211 uvc_input_cleanup(dev
);
214 int uvc_status_suspend(struct uvc_device
*dev
)
216 usb_kill_urb(dev
->int_urb
);
220 int uvc_status_resume(struct uvc_device
*dev
)
222 if (dev
->int_urb
== NULL
)
225 return usb_submit_urb(dev
->int_urb
, GFP_NOIO
);