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