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