blob: 5d60b264d59a1d2f7d2a3b41c224023cc5aff40e [file] [log] [blame]
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001/*
2 * uvc_status.c -- USB Video Class driver - Status endpoint
3 *
4 * Copyright (C) 2007-2008
5 * Laurent Pinchart (laurent.pinchart@skynet.be)
6 *
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.
11 *
12 */
13
14#include <linux/kernel.h>
15#include <linux/version.h>
16#include <linux/input.h>
17#include <linux/usb.h>
18#include <linux/usb/input.h>
19
20#include "uvcvideo.h"
21
22/* --------------------------------------------------------------------------
23 * Input device
24 */
Laurent Pinchart6833c912008-07-07 23:04:29 -030025#ifdef CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV
Laurent Pinchartc0efd232008-06-30 15:04:50 -030026static int uvc_input_init(struct uvc_device *dev)
27{
28 struct usb_device *udev = dev->udev;
29 struct input_dev *input;
30 char *phys = NULL;
31 int ret;
32
33 input = input_allocate_device();
34 if (input == NULL)
35 return -ENOMEM;
36
37 phys = kmalloc(6 + strlen(udev->bus->bus_name) + strlen(udev->devpath),
38 GFP_KERNEL);
39 if (phys == NULL) {
40 ret = -ENOMEM;
41 goto error;
42 }
43 sprintf(phys, "usb-%s-%s", udev->bus->bus_name, udev->devpath);
44
45 input->name = dev->name;
46 input->phys = phys;
47 usb_to_input_id(udev, &input->id);
48 input->dev.parent = &dev->intf->dev;
49
50 set_bit(EV_KEY, input->evbit);
51 set_bit(BTN_0, input->keybit);
52
53 if ((ret = input_register_device(input)) < 0)
54 goto error;
55
56 dev->input = input;
57 return 0;
58
59error:
60 input_free_device(input);
61 kfree(phys);
62 return ret;
63}
64
65static void uvc_input_cleanup(struct uvc_device *dev)
66{
67 if (dev->input)
68 input_unregister_device(dev->input);
69}
70
Laurent Pinchart6833c912008-07-07 23:04:29 -030071static void uvc_input_report_key(struct uvc_device *dev, unsigned int code,
72 int value)
73{
74 if (dev->input)
75 input_report_key(dev->input, code, value);
76}
77
78#else
79#define uvc_input_init(dev)
80#define uvc_input_cleanup(dev)
81#define uvc_input_report_key(dev, code, value)
82#endif /* CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV */
83
Laurent Pinchartc0efd232008-06-30 15:04:50 -030084/* --------------------------------------------------------------------------
85 * Status interrupt endpoint
86 */
87static void uvc_event_streaming(struct uvc_device *dev, __u8 *data, int len)
88{
89 if (len < 3) {
90 uvc_trace(UVC_TRACE_STATUS, "Invalid streaming status event "
91 "received.\n");
92 return;
93 }
94
95 if (data[2] == 0) {
96 if (len < 4)
97 return;
98 uvc_trace(UVC_TRACE_STATUS, "Button (intf %u) %s len %d\n",
99 data[1], data[3] ? "pressed" : "released", len);
Laurent Pinchart6833c912008-07-07 23:04:29 -0300100 uvc_input_report_key(dev, BTN_0, data[3]);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300101 } else {
102 uvc_trace(UVC_TRACE_STATUS, "Stream %u error event %02x %02x "
103 "len %d.\n", data[1], data[2], data[3], len);
104 }
105}
106
107static void uvc_event_control(struct uvc_device *dev, __u8 *data, int len)
108{
109 char *attrs[3] = { "value", "info", "failure" };
110
111 if (len < 6 || data[2] != 0 || data[4] > 2) {
112 uvc_trace(UVC_TRACE_STATUS, "Invalid control status event "
113 "received.\n");
114 return;
115 }
116
117 uvc_trace(UVC_TRACE_STATUS, "Control %u/%u %s change len %d.\n",
118 data[1], data[3], attrs[data[4]], len);
119}
120
121static void uvc_status_complete(struct urb *urb)
122{
123 struct uvc_device *dev = urb->context;
124 int len, ret;
125
126 switch (urb->status) {
127 case 0:
128 break;
129
130 case -ENOENT: /* usb_kill_urb() called. */
131 case -ECONNRESET: /* usb_unlink_urb() called. */
132 case -ESHUTDOWN: /* The endpoint is being disabled. */
133 case -EPROTO: /* Device is disconnected (reported by some
134 * host controller). */
135 return;
136
137 default:
138 uvc_printk(KERN_WARNING, "Non-zero status (%d) in status "
139 "completion handler.\n", urb->status);
140 return;
141 }
142
143 len = urb->actual_length;
144 if (len > 0) {
145 switch (dev->status[0] & 0x0f) {
146 case UVC_STATUS_TYPE_CONTROL:
147 uvc_event_control(dev, dev->status, len);
148 break;
149
150 case UVC_STATUS_TYPE_STREAMING:
151 uvc_event_streaming(dev, dev->status, len);
152 break;
153
154 default:
155 uvc_printk(KERN_INFO, "unknown event type %u.\n",
156 dev->status[0]);
157 break;
158 }
159 }
160
161 /* Resubmit the URB. */
162 urb->interval = dev->int_ep->desc.bInterval;
163 if ((ret = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
164 uvc_printk(KERN_ERR, "Failed to resubmit status URB (%d).\n",
165 ret);
166 }
167}
168
169int uvc_status_init(struct uvc_device *dev)
170{
171 struct usb_host_endpoint *ep = dev->int_ep;
172 unsigned int pipe;
173 int interval;
174
175 if (ep == NULL)
176 return 0;
177
178 uvc_input_init(dev);
179
Ming Leia31a4052008-09-16 03:32:20 -0300180 dev->status = kzalloc(UVC_MAX_STATUS_SIZE, GFP_KERNEL);
181 if (dev->status == NULL)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300182 return -ENOMEM;
183
Ming Leia31a4052008-09-16 03:32:20 -0300184 dev->int_urb = usb_alloc_urb(0, GFP_KERNEL);
185 if (dev->int_urb == NULL) {
186 kfree(dev->status);
187 return -ENOMEM;
188 }
189
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300190 pipe = usb_rcvintpipe(dev->udev, ep->desc.bEndpointAddress);
191
192 /* For high-speed interrupt endpoints, the bInterval value is used as
193 * an exponent of two. Some developers forgot about it.
194 */
195 interval = ep->desc.bInterval;
196 if (interval > 16 && dev->udev->speed == USB_SPEED_HIGH &&
197 (dev->quirks & UVC_QUIRK_STATUS_INTERVAL))
198 interval = fls(interval) - 1;
199
200 usb_fill_int_urb(dev->int_urb, dev->udev, pipe,
Ming Leia31a4052008-09-16 03:32:20 -0300201 dev->status, UVC_MAX_STATUS_SIZE, uvc_status_complete,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300202 dev, interval);
203
204 return usb_submit_urb(dev->int_urb, GFP_KERNEL);
205}
206
207void uvc_status_cleanup(struct uvc_device *dev)
208{
209 usb_kill_urb(dev->int_urb);
210 usb_free_urb(dev->int_urb);
Ming Leia31a4052008-09-16 03:32:20 -0300211 kfree(dev->status);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300212 uvc_input_cleanup(dev);
213}
214
215int uvc_status_suspend(struct uvc_device *dev)
216{
217 usb_kill_urb(dev->int_urb);
218 return 0;
219}
220
221int uvc_status_resume(struct uvc_device *dev)
222{
223 if (dev->int_urb == NULL)
224 return 0;
225
Laurent Pinchart29135872008-07-04 00:35:26 -0300226 return usb_submit_urb(dev->int_urb, GFP_NOIO);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300227}
Hans Verkuilf87086e2008-07-18 00:50:58 -0300228