Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 2 | /* |
| 3 | * uvc_status.c -- USB Video Class driver - Status endpoint |
| 4 | * |
Laurent Pinchart | 11fc5ba | 2010-09-20 06:10:10 -0300 | [diff] [blame] | 5 | * Copyright (C) 2005-2009 |
| 6 | * Laurent Pinchart (laurent.pinchart@ideasonboard.com) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/kernel.h> |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 10 | #include <linux/input.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 11 | #include <linux/slab.h> |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 12 | #include <linux/usb.h> |
| 13 | #include <linux/usb/input.h> |
| 14 | |
| 15 | #include "uvcvideo.h" |
| 16 | |
| 17 | /* -------------------------------------------------------------------------- |
| 18 | * Input device |
| 19 | */ |
Laurent Pinchart | 6833c917 | 2008-07-07 23:04:29 -0300 | [diff] [blame] | 20 | #ifdef CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 21 | static int uvc_input_init(struct uvc_device *dev) |
| 22 | { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 23 | struct input_dev *input; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 24 | int ret; |
| 25 | |
| 26 | input = input_allocate_device(); |
| 27 | if (input == NULL) |
| 28 | return -ENOMEM; |
| 29 | |
Laurent Pinchart | f180152 | 2009-01-22 12:45:10 -0300 | [diff] [blame] | 30 | usb_make_path(dev->udev, dev->input_phys, sizeof(dev->input_phys)); |
| 31 | strlcat(dev->input_phys, "/button", sizeof(dev->input_phys)); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 32 | |
| 33 | input->name = dev->name; |
Laurent Pinchart | f180152 | 2009-01-22 12:45:10 -0300 | [diff] [blame] | 34 | input->phys = dev->input_phys; |
| 35 | usb_to_input_id(dev->udev, &input->id); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 36 | input->dev.parent = &dev->intf->dev; |
| 37 | |
Bastien Nocera | 74ca11c | 2009-01-10 23:44:22 -0800 | [diff] [blame] | 38 | __set_bit(EV_KEY, input->evbit); |
| 39 | __set_bit(KEY_CAMERA, input->keybit); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 40 | |
| 41 | if ((ret = input_register_device(input)) < 0) |
| 42 | goto error; |
| 43 | |
| 44 | dev->input = input; |
| 45 | return 0; |
| 46 | |
| 47 | error: |
| 48 | input_free_device(input); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 49 | return ret; |
| 50 | } |
| 51 | |
Daniel Axtens | 10e1fdb | 2017-04-23 00:53:49 -0400 | [diff] [blame] | 52 | static void uvc_input_unregister(struct uvc_device *dev) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 53 | { |
| 54 | if (dev->input) |
| 55 | input_unregister_device(dev->input); |
| 56 | } |
| 57 | |
Laurent Pinchart | 6833c917 | 2008-07-07 23:04:29 -0300 | [diff] [blame] | 58 | static void uvc_input_report_key(struct uvc_device *dev, unsigned int code, |
| 59 | int value) |
| 60 | { |
Bastien Nocera | 74ca11c | 2009-01-10 23:44:22 -0800 | [diff] [blame] | 61 | if (dev->input) { |
Laurent Pinchart | 6833c917 | 2008-07-07 23:04:29 -0300 | [diff] [blame] | 62 | input_report_key(dev->input, code, value); |
Bastien Nocera | 74ca11c | 2009-01-10 23:44:22 -0800 | [diff] [blame] | 63 | input_sync(dev->input); |
| 64 | } |
Laurent Pinchart | 6833c917 | 2008-07-07 23:04:29 -0300 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | #else |
| 68 | #define uvc_input_init(dev) |
Daniel Axtens | 10e1fdb | 2017-04-23 00:53:49 -0400 | [diff] [blame] | 69 | #define uvc_input_unregister(dev) |
Laurent Pinchart | 6833c917 | 2008-07-07 23:04:29 -0300 | [diff] [blame] | 70 | #define uvc_input_report_key(dev, code, value) |
| 71 | #endif /* CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV */ |
| 72 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 73 | /* -------------------------------------------------------------------------- |
| 74 | * Status interrupt endpoint |
| 75 | */ |
Guennadi Liakhovetski | e5225c8 | 2018-07-26 04:17:53 -0400 | [diff] [blame] | 76 | struct uvc_streaming_status { |
| 77 | u8 bStatusType; |
| 78 | u8 bOriginator; |
| 79 | u8 bEvent; |
| 80 | u8 bValue[]; |
| 81 | } __packed; |
| 82 | |
| 83 | struct uvc_control_status { |
| 84 | u8 bStatusType; |
| 85 | u8 bOriginator; |
| 86 | u8 bEvent; |
| 87 | u8 bSelector; |
| 88 | u8 bAttribute; |
| 89 | u8 bValue[]; |
| 90 | } __packed; |
| 91 | |
| 92 | static void uvc_event_streaming(struct uvc_device *dev, |
| 93 | struct uvc_streaming_status *status, int len) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 94 | { |
| 95 | if (len < 3) { |
| 96 | uvc_trace(UVC_TRACE_STATUS, "Invalid streaming status event " |
| 97 | "received.\n"); |
| 98 | return; |
| 99 | } |
| 100 | |
Guennadi Liakhovetski | e5225c8 | 2018-07-26 04:17:53 -0400 | [diff] [blame] | 101 | if (status->bEvent == 0) { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 102 | if (len < 4) |
| 103 | return; |
| 104 | uvc_trace(UVC_TRACE_STATUS, "Button (intf %u) %s len %d\n", |
Guennadi Liakhovetski | e5225c8 | 2018-07-26 04:17:53 -0400 | [diff] [blame] | 105 | status->bOriginator, |
| 106 | status->bValue[0] ? "pressed" : "released", len); |
| 107 | uvc_input_report_key(dev, KEY_CAMERA, status->bValue[0]); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 108 | } else { |
Laurent Pinchart | 0393e73 | 2017-10-17 13:15:54 -0400 | [diff] [blame] | 109 | uvc_trace(UVC_TRACE_STATUS, |
| 110 | "Stream %u error event %02x len %d.\n", |
Guennadi Liakhovetski | e5225c8 | 2018-07-26 04:17:53 -0400 | [diff] [blame] | 111 | status->bOriginator, status->bEvent, len); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
Guennadi Liakhovetski | e5225c8 | 2018-07-26 04:17:53 -0400 | [diff] [blame] | 115 | #define UVC_CTRL_VALUE_CHANGE 0 |
| 116 | #define UVC_CTRL_INFO_CHANGE 1 |
| 117 | #define UVC_CTRL_FAILURE_CHANGE 2 |
| 118 | #define UVC_CTRL_MIN_CHANGE 3 |
| 119 | #define UVC_CTRL_MAX_CHANGE 4 |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 120 | |
Guennadi Liakhovetski | e5225c8 | 2018-07-26 04:17:53 -0400 | [diff] [blame] | 121 | static struct uvc_control *uvc_event_entity_find_ctrl(struct uvc_entity *entity, |
| 122 | u8 selector) |
| 123 | { |
| 124 | struct uvc_control *ctrl; |
| 125 | unsigned int i; |
| 126 | |
| 127 | for (i = 0, ctrl = entity->controls; i < entity->ncontrols; i++, ctrl++) |
| 128 | if (ctrl->info.selector == selector) |
| 129 | return ctrl; |
| 130 | |
| 131 | return NULL; |
| 132 | } |
| 133 | |
| 134 | static struct uvc_control *uvc_event_find_ctrl(struct uvc_device *dev, |
| 135 | const struct uvc_control_status *status, |
| 136 | struct uvc_video_chain **chain) |
| 137 | { |
| 138 | list_for_each_entry((*chain), &dev->chains, list) { |
| 139 | struct uvc_entity *entity; |
| 140 | struct uvc_control *ctrl; |
| 141 | |
| 142 | list_for_each_entry(entity, &(*chain)->entities, chain) { |
| 143 | if (entity->id != status->bOriginator) |
| 144 | continue; |
| 145 | |
| 146 | ctrl = uvc_event_entity_find_ctrl(entity, |
| 147 | status->bSelector); |
| 148 | if (ctrl) |
| 149 | return ctrl; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return NULL; |
| 154 | } |
| 155 | |
| 156 | static bool uvc_event_control(struct urb *urb, |
| 157 | const struct uvc_control_status *status, int len) |
| 158 | { |
| 159 | static const char *attrs[] = { "value", "info", "failure", "min", "max" }; |
| 160 | struct uvc_device *dev = urb->context; |
| 161 | struct uvc_video_chain *chain; |
| 162 | struct uvc_control *ctrl; |
| 163 | |
| 164 | if (len < 6 || status->bEvent != 0 || |
| 165 | status->bAttribute >= ARRAY_SIZE(attrs)) { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 166 | uvc_trace(UVC_TRACE_STATUS, "Invalid control status event " |
| 167 | "received.\n"); |
Guennadi Liakhovetski | e5225c8 | 2018-07-26 04:17:53 -0400 | [diff] [blame] | 168 | return false; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | uvc_trace(UVC_TRACE_STATUS, "Control %u/%u %s change len %d.\n", |
Guennadi Liakhovetski | e5225c8 | 2018-07-26 04:17:53 -0400 | [diff] [blame] | 172 | status->bOriginator, status->bSelector, |
| 173 | attrs[status->bAttribute], len); |
| 174 | |
| 175 | /* Find the control. */ |
| 176 | ctrl = uvc_event_find_ctrl(dev, status, &chain); |
| 177 | if (!ctrl) |
| 178 | return false; |
| 179 | |
| 180 | switch (status->bAttribute) { |
| 181 | case UVC_CTRL_VALUE_CHANGE: |
| 182 | return uvc_ctrl_status_event(urb, chain, ctrl, status->bValue); |
| 183 | |
| 184 | case UVC_CTRL_INFO_CHANGE: |
| 185 | case UVC_CTRL_FAILURE_CHANGE: |
| 186 | case UVC_CTRL_MIN_CHANGE: |
| 187 | case UVC_CTRL_MAX_CHANGE: |
| 188 | break; |
| 189 | } |
| 190 | |
| 191 | return false; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | static void uvc_status_complete(struct urb *urb) |
| 195 | { |
| 196 | struct uvc_device *dev = urb->context; |
| 197 | int len, ret; |
| 198 | |
| 199 | switch (urb->status) { |
| 200 | case 0: |
| 201 | break; |
| 202 | |
| 203 | case -ENOENT: /* usb_kill_urb() called. */ |
| 204 | case -ECONNRESET: /* usb_unlink_urb() called. */ |
| 205 | case -ESHUTDOWN: /* The endpoint is being disabled. */ |
| 206 | case -EPROTO: /* Device is disconnected (reported by some |
| 207 | * host controller). */ |
| 208 | return; |
| 209 | |
| 210 | default: |
| 211 | uvc_printk(KERN_WARNING, "Non-zero status (%d) in status " |
| 212 | "completion handler.\n", urb->status); |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | len = urb->actual_length; |
| 217 | if (len > 0) { |
| 218 | switch (dev->status[0] & 0x0f) { |
Guennadi Liakhovetski | e5225c8 | 2018-07-26 04:17:53 -0400 | [diff] [blame] | 219 | case UVC_STATUS_TYPE_CONTROL: { |
| 220 | struct uvc_control_status *status = |
| 221 | (struct uvc_control_status *)dev->status; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 222 | |
Guennadi Liakhovetski | e5225c8 | 2018-07-26 04:17:53 -0400 | [diff] [blame] | 223 | if (uvc_event_control(urb, status, len)) |
| 224 | /* The URB will be resubmitted in work context. */ |
| 225 | return; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 226 | break; |
Guennadi Liakhovetski | e5225c8 | 2018-07-26 04:17:53 -0400 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | case UVC_STATUS_TYPE_STREAMING: { |
| 230 | struct uvc_streaming_status *status = |
| 231 | (struct uvc_streaming_status *)dev->status; |
| 232 | |
| 233 | uvc_event_streaming(dev, status, len); |
| 234 | break; |
| 235 | } |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 236 | |
| 237 | default: |
Laurent Pinchart | bd0232c | 2009-08-01 18:14:24 -0300 | [diff] [blame] | 238 | uvc_trace(UVC_TRACE_STATUS, "Unknown status event " |
| 239 | "type %u.\n", dev->status[0]); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 240 | break; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /* Resubmit the URB. */ |
| 245 | urb->interval = dev->int_ep->desc.bInterval; |
| 246 | if ((ret = usb_submit_urb(urb, GFP_ATOMIC)) < 0) { |
| 247 | uvc_printk(KERN_ERR, "Failed to resubmit status URB (%d).\n", |
| 248 | ret); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | int uvc_status_init(struct uvc_device *dev) |
| 253 | { |
| 254 | struct usb_host_endpoint *ep = dev->int_ep; |
| 255 | unsigned int pipe; |
| 256 | int interval; |
| 257 | |
| 258 | if (ep == NULL) |
| 259 | return 0; |
| 260 | |
| 261 | uvc_input_init(dev); |
| 262 | |
Ming Lei | a31a405 | 2008-09-16 03:32:20 -0300 | [diff] [blame] | 263 | dev->status = kzalloc(UVC_MAX_STATUS_SIZE, GFP_KERNEL); |
| 264 | if (dev->status == NULL) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 265 | return -ENOMEM; |
| 266 | |
Ming Lei | a31a405 | 2008-09-16 03:32:20 -0300 | [diff] [blame] | 267 | dev->int_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 268 | if (dev->int_urb == NULL) { |
| 269 | kfree(dev->status); |
| 270 | return -ENOMEM; |
| 271 | } |
| 272 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 273 | pipe = usb_rcvintpipe(dev->udev, ep->desc.bEndpointAddress); |
| 274 | |
| 275 | /* For high-speed interrupt endpoints, the bInterval value is used as |
| 276 | * an exponent of two. Some developers forgot about it. |
| 277 | */ |
| 278 | interval = ep->desc.bInterval; |
| 279 | if (interval > 16 && dev->udev->speed == USB_SPEED_HIGH && |
| 280 | (dev->quirks & UVC_QUIRK_STATUS_INTERVAL)) |
| 281 | interval = fls(interval) - 1; |
| 282 | |
| 283 | usb_fill_int_urb(dev->int_urb, dev->udev, pipe, |
Ming Lei | a31a405 | 2008-09-16 03:32:20 -0300 | [diff] [blame] | 284 | dev->status, UVC_MAX_STATUS_SIZE, uvc_status_complete, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 285 | dev, interval); |
| 286 | |
Laurent Pinchart | 04a37e0 | 2009-05-19 10:08:03 -0300 | [diff] [blame] | 287 | return 0; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 288 | } |
| 289 | |
Daniel Axtens | 10e1fdb | 2017-04-23 00:53:49 -0400 | [diff] [blame] | 290 | void uvc_status_unregister(struct uvc_device *dev) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 291 | { |
| 292 | usb_kill_urb(dev->int_urb); |
Daniel Axtens | 10e1fdb | 2017-04-23 00:53:49 -0400 | [diff] [blame] | 293 | uvc_input_unregister(dev); |
| 294 | } |
| 295 | |
| 296 | void uvc_status_cleanup(struct uvc_device *dev) |
| 297 | { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 298 | usb_free_urb(dev->int_urb); |
Ming Lei | a31a405 | 2008-09-16 03:32:20 -0300 | [diff] [blame] | 299 | kfree(dev->status); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 300 | } |
| 301 | |
Laurent Pinchart | 17706f5 | 2013-04-25 22:28:51 -0300 | [diff] [blame] | 302 | int uvc_status_start(struct uvc_device *dev, gfp_t flags) |
Laurent Pinchart | 04a37e0 | 2009-05-19 10:08:03 -0300 | [diff] [blame] | 303 | { |
| 304 | if (dev->int_urb == NULL) |
| 305 | return 0; |
| 306 | |
Laurent Pinchart | 17706f5 | 2013-04-25 22:28:51 -0300 | [diff] [blame] | 307 | return usb_submit_urb(dev->int_urb, flags); |
Laurent Pinchart | 04a37e0 | 2009-05-19 10:08:03 -0300 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | void uvc_status_stop(struct uvc_device *dev) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 311 | { |
| 312 | usb_kill_urb(dev->int_urb); |
Laurent Pinchart | 04a37e0 | 2009-05-19 10:08:03 -0300 | [diff] [blame] | 313 | } |