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