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