2 * uvc_status.c -- USB Video Class driver - Status endpoint
4 * Copyright (C) 2005-2009
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
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 <asm/barrier.h>
15 #include <linux/kernel.h>
16 #include <linux/input.h>
17 #include <linux/slab.h>
18 #include <linux/usb.h>
19 #include <linux/usb/input.h>
23 /* --------------------------------------------------------------------------
26 #ifdef CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV
27 static int uvc_input_init(struct uvc_device *dev)
29 struct input_dev *input;
32 input = input_allocate_device();
36 usb_make_path(dev->udev, dev->input_phys, sizeof(dev->input_phys));
37 strlcat(dev->input_phys, "/button", sizeof(dev->input_phys));
39 input->name = dev->name;
40 input->phys = dev->input_phys;
41 usb_to_input_id(dev->udev, &input->id);
42 input->dev.parent = &dev->intf->dev;
44 __set_bit(EV_KEY, input->evbit);
45 __set_bit(KEY_CAMERA, input->keybit);
47 if ((ret = input_register_device(input)) < 0)
54 input_free_device(input);
58 static void uvc_input_unregister(struct uvc_device *dev)
61 input_unregister_device(dev->input);
64 static void uvc_input_report_key(struct uvc_device *dev, unsigned int code,
68 input_report_key(dev->input, code, value);
69 input_sync(dev->input);
74 #define uvc_input_init(dev)
75 #define uvc_input_unregister(dev)
76 #define uvc_input_report_key(dev, code, value)
77 #endif /* CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV */
79 /* --------------------------------------------------------------------------
80 * Status interrupt endpoint
82 struct uvc_streaming_status {
89 struct uvc_control_status {
98 static void uvc_event_streaming(struct uvc_device *dev,
99 struct uvc_streaming_status *status, int len)
102 uvc_trace(UVC_TRACE_STATUS, "Invalid streaming status event "
107 if (status->bEvent == 0) {
110 uvc_trace(UVC_TRACE_STATUS, "Button (intf %u) %s len %d\n",
112 status->bValue[0] ? "pressed" : "released", len);
113 uvc_input_report_key(dev, KEY_CAMERA, status->bValue[0]);
115 uvc_trace(UVC_TRACE_STATUS,
116 "Stream %u error event %02x len %d.\n",
117 status->bOriginator, status->bEvent, len);
121 #define UVC_CTRL_VALUE_CHANGE 0
122 #define UVC_CTRL_INFO_CHANGE 1
123 #define UVC_CTRL_FAILURE_CHANGE 2
124 #define UVC_CTRL_MIN_CHANGE 3
125 #define UVC_CTRL_MAX_CHANGE 4
127 static struct uvc_control *uvc_event_entity_find_ctrl(struct uvc_entity *entity,
130 struct uvc_control *ctrl;
133 for (i = 0, ctrl = entity->controls; i < entity->ncontrols; i++, ctrl++)
134 if (ctrl->info.selector == selector)
140 static struct uvc_control *uvc_event_find_ctrl(struct uvc_device *dev,
141 const struct uvc_control_status *status,
142 struct uvc_video_chain **chain)
144 list_for_each_entry((*chain), &dev->chains, list) {
145 struct uvc_entity *entity;
146 struct uvc_control *ctrl;
148 list_for_each_entry(entity, &(*chain)->entities, chain) {
149 if (entity->id != status->bOriginator)
152 ctrl = uvc_event_entity_find_ctrl(entity,
162 static bool uvc_event_control(struct urb *urb,
163 const struct uvc_control_status *status, int len)
165 static const char *attrs[] = { "value", "info", "failure", "min", "max" };
166 struct uvc_device *dev = urb->context;
167 struct uvc_video_chain *chain;
168 struct uvc_control *ctrl;
170 if (len < 6 || status->bEvent != 0 ||
171 status->bAttribute >= ARRAY_SIZE(attrs)) {
172 uvc_trace(UVC_TRACE_STATUS, "Invalid control status event "
177 uvc_trace(UVC_TRACE_STATUS, "Control %u/%u %s change len %d.\n",
178 status->bOriginator, status->bSelector,
179 attrs[status->bAttribute], len);
181 /* Find the control. */
182 ctrl = uvc_event_find_ctrl(dev, status, &chain);
186 switch (status->bAttribute) {
187 case UVC_CTRL_VALUE_CHANGE:
188 return uvc_ctrl_status_event_async(urb, chain, ctrl,
191 case UVC_CTRL_INFO_CHANGE:
192 case UVC_CTRL_FAILURE_CHANGE:
193 case UVC_CTRL_MIN_CHANGE:
194 case UVC_CTRL_MAX_CHANGE:
201 static void uvc_status_complete(struct urb *urb)
203 struct uvc_device *dev = urb->context;
206 switch (urb->status) {
210 case -ENOENT: /* usb_kill_urb() called. */
211 case -ECONNRESET: /* usb_unlink_urb() called. */
212 case -ESHUTDOWN: /* The endpoint is being disabled. */
213 case -EPROTO: /* Device is disconnected (reported by some
214 * host controller). */
218 uvc_printk(KERN_WARNING, "Non-zero status (%d) in status "
219 "completion handler.\n", urb->status);
223 len = urb->actual_length;
225 switch (dev->status[0] & 0x0f) {
226 case UVC_STATUS_TYPE_CONTROL: {
227 struct uvc_control_status *status =
228 (struct uvc_control_status *)dev->status;
230 if (uvc_event_control(urb, status, len))
231 /* The URB will be resubmitted in work context. */
236 case UVC_STATUS_TYPE_STREAMING: {
237 struct uvc_streaming_status *status =
238 (struct uvc_streaming_status *)dev->status;
240 uvc_event_streaming(dev, status, len);
245 uvc_trace(UVC_TRACE_STATUS, "Unknown status event "
246 "type %u.\n", dev->status[0]);
251 /* Resubmit the URB. */
252 urb->interval = dev->int_ep->desc.bInterval;
253 if ((ret = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
254 uvc_printk(KERN_ERR, "Failed to resubmit status URB (%d).\n",
259 int uvc_status_init(struct uvc_device *dev)
261 struct usb_host_endpoint *ep = dev->int_ep;
270 dev->status = kzalloc(UVC_MAX_STATUS_SIZE, GFP_KERNEL);
271 if (dev->status == NULL)
274 dev->int_urb = usb_alloc_urb(0, GFP_KERNEL);
275 if (dev->int_urb == NULL) {
280 pipe = usb_rcvintpipe(dev->udev, ep->desc.bEndpointAddress);
282 /* For high-speed interrupt endpoints, the bInterval value is used as
283 * an exponent of two. Some developers forgot about it.
285 interval = ep->desc.bInterval;
286 if (interval > 16 && dev->udev->speed == USB_SPEED_HIGH &&
287 (dev->quirks & UVC_QUIRK_STATUS_INTERVAL))
288 interval = fls(interval) - 1;
290 usb_fill_int_urb(dev->int_urb, dev->udev, pipe,
291 dev->status, UVC_MAX_STATUS_SIZE, uvc_status_complete,
297 void uvc_status_unregister(struct uvc_device *dev)
299 usb_kill_urb(dev->int_urb);
300 uvc_input_unregister(dev);
303 void uvc_status_cleanup(struct uvc_device *dev)
305 usb_free_urb(dev->int_urb);
309 int uvc_status_start(struct uvc_device *dev, gfp_t flags)
311 if (dev->int_urb == NULL)
314 return usb_submit_urb(dev->int_urb, flags);
317 void uvc_status_stop(struct uvc_device *dev)
319 struct uvc_ctrl_work *w = &dev->async_ctrl;
322 * Prevent the asynchronous control handler from requeing the URB. The
323 * barrier is needed so the flush_status change is visible to other
324 * CPUs running the asynchronous handler before usb_kill_urb() is
327 smp_store_release(&dev->flush_status, true);
330 * Cancel any pending asynchronous work. If any status event was queued,
331 * process it synchronously.
333 if (cancel_work_sync(&w->work))
334 uvc_ctrl_status_event(w->chain, w->ctrl, w->data);
337 usb_kill_urb(dev->int_urb);
340 * The URB completion handler may have queued asynchronous work. This
341 * won't resubmit the URB as flush_status is set, but it needs to be
342 * cancelled before returning or it could then race with a future
343 * uvc_status_start() call.
345 if (cancel_work_sync(&w->work))
346 uvc_ctrl_status_event(w->chain, w->ctrl, w->data);
349 * From this point, there are no events on the queue and the status URB
350 * is dead. No events will be queued until uvc_status_start() is called.
351 * The barrier is needed to make sure that flush_status is visible to
352 * uvc_ctrl_status_event_work() when uvc_status_start() will be called
355 smp_store_release(&dev->flush_status, false);