xref: /openbmc/linux/drivers/media/usb/uvc/uvc_status.c (revision d9c8763e61295be0a21dc04ad9c379d5d17c3d86)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
20c0d06caSMauro Carvalho Chehab /*
30c0d06caSMauro Carvalho Chehab  *      uvc_status.c  --  USB Video Class driver - Status endpoint
40c0d06caSMauro Carvalho Chehab  *
50c0d06caSMauro Carvalho Chehab  *      Copyright (C) 2005-2009
60c0d06caSMauro Carvalho Chehab  *          Laurent Pinchart (laurent.pinchart@ideasonboard.com)
70c0d06caSMauro Carvalho Chehab  */
80c0d06caSMauro Carvalho Chehab 
90c0d06caSMauro Carvalho Chehab #include <linux/kernel.h>
100c0d06caSMauro Carvalho Chehab #include <linux/input.h>
110c0d06caSMauro Carvalho Chehab #include <linux/slab.h>
120c0d06caSMauro Carvalho Chehab #include <linux/usb.h>
130c0d06caSMauro Carvalho Chehab #include <linux/usb/input.h>
140c0d06caSMauro Carvalho Chehab 
150c0d06caSMauro Carvalho Chehab #include "uvcvideo.h"
160c0d06caSMauro Carvalho Chehab 
170c0d06caSMauro Carvalho Chehab /* --------------------------------------------------------------------------
180c0d06caSMauro Carvalho Chehab  * Input device
190c0d06caSMauro Carvalho Chehab  */
200c0d06caSMauro Carvalho Chehab #ifdef CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV
210c0d06caSMauro Carvalho Chehab static int uvc_input_init(struct uvc_device *dev)
220c0d06caSMauro Carvalho Chehab {
230c0d06caSMauro Carvalho Chehab 	struct input_dev *input;
240c0d06caSMauro Carvalho Chehab 	int ret;
250c0d06caSMauro Carvalho Chehab 
260c0d06caSMauro Carvalho Chehab 	input = input_allocate_device();
270c0d06caSMauro Carvalho Chehab 	if (input == NULL)
280c0d06caSMauro Carvalho Chehab 		return -ENOMEM;
290c0d06caSMauro Carvalho Chehab 
300c0d06caSMauro Carvalho Chehab 	usb_make_path(dev->udev, dev->input_phys, sizeof(dev->input_phys));
310c0d06caSMauro Carvalho Chehab 	strlcat(dev->input_phys, "/button", sizeof(dev->input_phys));
320c0d06caSMauro Carvalho Chehab 
330c0d06caSMauro Carvalho Chehab 	input->name = dev->name;
340c0d06caSMauro Carvalho Chehab 	input->phys = dev->input_phys;
350c0d06caSMauro Carvalho Chehab 	usb_to_input_id(dev->udev, &input->id);
360c0d06caSMauro Carvalho Chehab 	input->dev.parent = &dev->intf->dev;
370c0d06caSMauro Carvalho Chehab 
380c0d06caSMauro Carvalho Chehab 	__set_bit(EV_KEY, input->evbit);
390c0d06caSMauro Carvalho Chehab 	__set_bit(KEY_CAMERA, input->keybit);
400c0d06caSMauro Carvalho Chehab 
410c0d06caSMauro Carvalho Chehab 	if ((ret = input_register_device(input)) < 0)
420c0d06caSMauro Carvalho Chehab 		goto error;
430c0d06caSMauro Carvalho Chehab 
440c0d06caSMauro Carvalho Chehab 	dev->input = input;
450c0d06caSMauro Carvalho Chehab 	return 0;
460c0d06caSMauro Carvalho Chehab 
470c0d06caSMauro Carvalho Chehab error:
480c0d06caSMauro Carvalho Chehab 	input_free_device(input);
490c0d06caSMauro Carvalho Chehab 	return ret;
500c0d06caSMauro Carvalho Chehab }
510c0d06caSMauro Carvalho Chehab 
5210e1fdb9SDaniel Axtens static void uvc_input_unregister(struct uvc_device *dev)
530c0d06caSMauro Carvalho Chehab {
540c0d06caSMauro Carvalho Chehab 	if (dev->input)
550c0d06caSMauro Carvalho Chehab 		input_unregister_device(dev->input);
560c0d06caSMauro Carvalho Chehab }
570c0d06caSMauro Carvalho Chehab 
580c0d06caSMauro Carvalho Chehab static void uvc_input_report_key(struct uvc_device *dev, unsigned int code,
590c0d06caSMauro Carvalho Chehab 	int value)
600c0d06caSMauro Carvalho Chehab {
610c0d06caSMauro Carvalho Chehab 	if (dev->input) {
620c0d06caSMauro Carvalho Chehab 		input_report_key(dev->input, code, value);
630c0d06caSMauro Carvalho Chehab 		input_sync(dev->input);
640c0d06caSMauro Carvalho Chehab 	}
650c0d06caSMauro Carvalho Chehab }
660c0d06caSMauro Carvalho Chehab 
670c0d06caSMauro Carvalho Chehab #else
680c0d06caSMauro Carvalho Chehab #define uvc_input_init(dev)
6910e1fdb9SDaniel Axtens #define uvc_input_unregister(dev)
700c0d06caSMauro Carvalho Chehab #define uvc_input_report_key(dev, code, value)
710c0d06caSMauro Carvalho Chehab #endif /* CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV */
720c0d06caSMauro Carvalho Chehab 
730c0d06caSMauro Carvalho Chehab /* --------------------------------------------------------------------------
740c0d06caSMauro Carvalho Chehab  * Status interrupt endpoint
750c0d06caSMauro Carvalho Chehab  */
76e5225c82SGuennadi Liakhovetski struct uvc_streaming_status {
77e5225c82SGuennadi Liakhovetski 	u8	bStatusType;
78e5225c82SGuennadi Liakhovetski 	u8	bOriginator;
79e5225c82SGuennadi Liakhovetski 	u8	bEvent;
80e5225c82SGuennadi Liakhovetski 	u8	bValue[];
81e5225c82SGuennadi Liakhovetski } __packed;
82e5225c82SGuennadi Liakhovetski 
83e5225c82SGuennadi Liakhovetski struct uvc_control_status {
84e5225c82SGuennadi Liakhovetski 	u8	bStatusType;
85e5225c82SGuennadi Liakhovetski 	u8	bOriginator;
86e5225c82SGuennadi Liakhovetski 	u8	bEvent;
87e5225c82SGuennadi Liakhovetski 	u8	bSelector;
88e5225c82SGuennadi Liakhovetski 	u8	bAttribute;
89e5225c82SGuennadi Liakhovetski 	u8	bValue[];
90e5225c82SGuennadi Liakhovetski } __packed;
91e5225c82SGuennadi Liakhovetski 
92e5225c82SGuennadi Liakhovetski static void uvc_event_streaming(struct uvc_device *dev,
93e5225c82SGuennadi Liakhovetski 				struct uvc_streaming_status *status, int len)
940c0d06caSMauro Carvalho Chehab {
950c0d06caSMauro Carvalho Chehab 	if (len < 3) {
960c0d06caSMauro Carvalho Chehab 		uvc_trace(UVC_TRACE_STATUS, "Invalid streaming status event "
970c0d06caSMauro Carvalho Chehab 				"received.\n");
980c0d06caSMauro Carvalho Chehab 		return;
990c0d06caSMauro Carvalho Chehab 	}
1000c0d06caSMauro Carvalho Chehab 
101e5225c82SGuennadi Liakhovetski 	if (status->bEvent == 0) {
1020c0d06caSMauro Carvalho Chehab 		if (len < 4)
1030c0d06caSMauro Carvalho Chehab 			return;
1040c0d06caSMauro Carvalho Chehab 		uvc_trace(UVC_TRACE_STATUS, "Button (intf %u) %s len %d\n",
105e5225c82SGuennadi Liakhovetski 			  status->bOriginator,
106e5225c82SGuennadi Liakhovetski 			  status->bValue[0] ? "pressed" : "released", len);
107e5225c82SGuennadi Liakhovetski 		uvc_input_report_key(dev, KEY_CAMERA, status->bValue[0]);
1080c0d06caSMauro Carvalho Chehab 	} else {
1090393e735SLaurent Pinchart 		uvc_trace(UVC_TRACE_STATUS,
1100393e735SLaurent Pinchart 			  "Stream %u error event %02x len %d.\n",
111e5225c82SGuennadi Liakhovetski 			  status->bOriginator, status->bEvent, len);
1120c0d06caSMauro Carvalho Chehab 	}
1130c0d06caSMauro Carvalho Chehab }
1140c0d06caSMauro Carvalho Chehab 
115e5225c82SGuennadi Liakhovetski #define UVC_CTRL_VALUE_CHANGE	0
116e5225c82SGuennadi Liakhovetski #define UVC_CTRL_INFO_CHANGE	1
117e5225c82SGuennadi Liakhovetski #define UVC_CTRL_FAILURE_CHANGE	2
118e5225c82SGuennadi Liakhovetski #define UVC_CTRL_MIN_CHANGE	3
119e5225c82SGuennadi Liakhovetski #define UVC_CTRL_MAX_CHANGE	4
120e5225c82SGuennadi Liakhovetski 
121e5225c82SGuennadi Liakhovetski static struct uvc_control *uvc_event_entity_find_ctrl(struct uvc_entity *entity,
122e5225c82SGuennadi Liakhovetski 						      u8 selector)
1230c0d06caSMauro Carvalho Chehab {
124e5225c82SGuennadi Liakhovetski 	struct uvc_control *ctrl;
125e5225c82SGuennadi Liakhovetski 	unsigned int i;
1260c0d06caSMauro Carvalho Chehab 
127e5225c82SGuennadi Liakhovetski 	for (i = 0, ctrl = entity->controls; i < entity->ncontrols; i++, ctrl++)
128e5225c82SGuennadi Liakhovetski 		if (ctrl->info.selector == selector)
129e5225c82SGuennadi Liakhovetski 			return ctrl;
130e5225c82SGuennadi Liakhovetski 
131e5225c82SGuennadi Liakhovetski 	return NULL;
132e5225c82SGuennadi Liakhovetski }
133e5225c82SGuennadi Liakhovetski 
134e5225c82SGuennadi Liakhovetski static struct uvc_control *uvc_event_find_ctrl(struct uvc_device *dev,
135e5225c82SGuennadi Liakhovetski 					const struct uvc_control_status *status,
136e5225c82SGuennadi Liakhovetski 					struct uvc_video_chain **chain)
137e5225c82SGuennadi Liakhovetski {
138e5225c82SGuennadi Liakhovetski 	list_for_each_entry((*chain), &dev->chains, list) {
139e5225c82SGuennadi Liakhovetski 		struct uvc_entity *entity;
140e5225c82SGuennadi Liakhovetski 		struct uvc_control *ctrl;
141e5225c82SGuennadi Liakhovetski 
142e5225c82SGuennadi Liakhovetski 		list_for_each_entry(entity, &(*chain)->entities, chain) {
143e5225c82SGuennadi Liakhovetski 			if (entity->id != status->bOriginator)
144e5225c82SGuennadi Liakhovetski 				continue;
145e5225c82SGuennadi Liakhovetski 
146e5225c82SGuennadi Liakhovetski 			ctrl = uvc_event_entity_find_ctrl(entity,
147e5225c82SGuennadi Liakhovetski 							  status->bSelector);
148e5225c82SGuennadi Liakhovetski 			if (ctrl)
149e5225c82SGuennadi Liakhovetski 				return ctrl;
150e5225c82SGuennadi Liakhovetski 		}
151e5225c82SGuennadi Liakhovetski 	}
152e5225c82SGuennadi Liakhovetski 
153e5225c82SGuennadi Liakhovetski 	return NULL;
154e5225c82SGuennadi Liakhovetski }
155e5225c82SGuennadi Liakhovetski 
156e5225c82SGuennadi Liakhovetski static bool uvc_event_control(struct urb *urb,
157e5225c82SGuennadi Liakhovetski 			      const struct uvc_control_status *status, int len)
158e5225c82SGuennadi Liakhovetski {
159e5225c82SGuennadi Liakhovetski 	static const char *attrs[] = { "value", "info", "failure", "min", "max" };
160e5225c82SGuennadi Liakhovetski 	struct uvc_device *dev = urb->context;
161e5225c82SGuennadi Liakhovetski 	struct uvc_video_chain *chain;
162e5225c82SGuennadi Liakhovetski 	struct uvc_control *ctrl;
163e5225c82SGuennadi Liakhovetski 
164e5225c82SGuennadi Liakhovetski 	if (len < 6 || status->bEvent != 0 ||
165e5225c82SGuennadi Liakhovetski 	    status->bAttribute >= ARRAY_SIZE(attrs)) {
1660c0d06caSMauro Carvalho Chehab 		uvc_trace(UVC_TRACE_STATUS, "Invalid control status event "
1670c0d06caSMauro Carvalho Chehab 				"received.\n");
168e5225c82SGuennadi Liakhovetski 		return false;
1690c0d06caSMauro Carvalho Chehab 	}
1700c0d06caSMauro Carvalho Chehab 
1710c0d06caSMauro Carvalho Chehab 	uvc_trace(UVC_TRACE_STATUS, "Control %u/%u %s change len %d.\n",
172e5225c82SGuennadi Liakhovetski 		  status->bOriginator, status->bSelector,
173e5225c82SGuennadi Liakhovetski 		  attrs[status->bAttribute], len);
174e5225c82SGuennadi Liakhovetski 
175e5225c82SGuennadi Liakhovetski 	/* Find the control. */
176e5225c82SGuennadi Liakhovetski 	ctrl = uvc_event_find_ctrl(dev, status, &chain);
177e5225c82SGuennadi Liakhovetski 	if (!ctrl)
178e5225c82SGuennadi Liakhovetski 		return false;
179e5225c82SGuennadi Liakhovetski 
180e5225c82SGuennadi Liakhovetski 	switch (status->bAttribute) {
181e5225c82SGuennadi Liakhovetski 	case UVC_CTRL_VALUE_CHANGE:
182*d9c8763eSRicardo Ribalda 		return uvc_ctrl_status_event_async(urb, chain, ctrl,
183*d9c8763eSRicardo Ribalda 						   status->bValue);
184e5225c82SGuennadi Liakhovetski 
185e5225c82SGuennadi Liakhovetski 	case UVC_CTRL_INFO_CHANGE:
186e5225c82SGuennadi Liakhovetski 	case UVC_CTRL_FAILURE_CHANGE:
187e5225c82SGuennadi Liakhovetski 	case UVC_CTRL_MIN_CHANGE:
188e5225c82SGuennadi Liakhovetski 	case UVC_CTRL_MAX_CHANGE:
189e5225c82SGuennadi Liakhovetski 		break;
190e5225c82SGuennadi Liakhovetski 	}
191e5225c82SGuennadi Liakhovetski 
192e5225c82SGuennadi Liakhovetski 	return false;
1930c0d06caSMauro Carvalho Chehab }
1940c0d06caSMauro Carvalho Chehab 
1950c0d06caSMauro Carvalho Chehab static void uvc_status_complete(struct urb *urb)
1960c0d06caSMauro Carvalho Chehab {
1970c0d06caSMauro Carvalho Chehab 	struct uvc_device *dev = urb->context;
1980c0d06caSMauro Carvalho Chehab 	int len, ret;
1990c0d06caSMauro Carvalho Chehab 
2000c0d06caSMauro Carvalho Chehab 	switch (urb->status) {
2010c0d06caSMauro Carvalho Chehab 	case 0:
2020c0d06caSMauro Carvalho Chehab 		break;
2030c0d06caSMauro Carvalho Chehab 
2040c0d06caSMauro Carvalho Chehab 	case -ENOENT:		/* usb_kill_urb() called. */
2050c0d06caSMauro Carvalho Chehab 	case -ECONNRESET:	/* usb_unlink_urb() called. */
2060c0d06caSMauro Carvalho Chehab 	case -ESHUTDOWN:	/* The endpoint is being disabled. */
2070c0d06caSMauro Carvalho Chehab 	case -EPROTO:		/* Device is disconnected (reported by some
2080c0d06caSMauro Carvalho Chehab 				 * host controller). */
2090c0d06caSMauro Carvalho Chehab 		return;
2100c0d06caSMauro Carvalho Chehab 
2110c0d06caSMauro Carvalho Chehab 	default:
2120c0d06caSMauro Carvalho Chehab 		uvc_printk(KERN_WARNING, "Non-zero status (%d) in status "
2130c0d06caSMauro Carvalho Chehab 			"completion handler.\n", urb->status);
2140c0d06caSMauro Carvalho Chehab 		return;
2150c0d06caSMauro Carvalho Chehab 	}
2160c0d06caSMauro Carvalho Chehab 
2170c0d06caSMauro Carvalho Chehab 	len = urb->actual_length;
2180c0d06caSMauro Carvalho Chehab 	if (len > 0) {
2190c0d06caSMauro Carvalho Chehab 		switch (dev->status[0] & 0x0f) {
220e5225c82SGuennadi Liakhovetski 		case UVC_STATUS_TYPE_CONTROL: {
221e5225c82SGuennadi Liakhovetski 			struct uvc_control_status *status =
222e5225c82SGuennadi Liakhovetski 				(struct uvc_control_status *)dev->status;
2230c0d06caSMauro Carvalho Chehab 
224e5225c82SGuennadi Liakhovetski 			if (uvc_event_control(urb, status, len))
225e5225c82SGuennadi Liakhovetski 				/* The URB will be resubmitted in work context. */
226e5225c82SGuennadi Liakhovetski 				return;
2270c0d06caSMauro Carvalho Chehab 			break;
228e5225c82SGuennadi Liakhovetski 		}
229e5225c82SGuennadi Liakhovetski 
230e5225c82SGuennadi Liakhovetski 		case UVC_STATUS_TYPE_STREAMING: {
231e5225c82SGuennadi Liakhovetski 			struct uvc_streaming_status *status =
232e5225c82SGuennadi Liakhovetski 				(struct uvc_streaming_status *)dev->status;
233e5225c82SGuennadi Liakhovetski 
234e5225c82SGuennadi Liakhovetski 			uvc_event_streaming(dev, status, len);
235e5225c82SGuennadi Liakhovetski 			break;
236e5225c82SGuennadi Liakhovetski 		}
2370c0d06caSMauro Carvalho Chehab 
2380c0d06caSMauro Carvalho Chehab 		default:
2390c0d06caSMauro Carvalho Chehab 			uvc_trace(UVC_TRACE_STATUS, "Unknown status event "
2400c0d06caSMauro Carvalho Chehab 				"type %u.\n", dev->status[0]);
2410c0d06caSMauro Carvalho Chehab 			break;
2420c0d06caSMauro Carvalho Chehab 		}
2430c0d06caSMauro Carvalho Chehab 	}
2440c0d06caSMauro Carvalho Chehab 
2450c0d06caSMauro Carvalho Chehab 	/* Resubmit the URB. */
2460c0d06caSMauro Carvalho Chehab 	urb->interval = dev->int_ep->desc.bInterval;
2470c0d06caSMauro Carvalho Chehab 	if ((ret = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
2480c0d06caSMauro Carvalho Chehab 		uvc_printk(KERN_ERR, "Failed to resubmit status URB (%d).\n",
2490c0d06caSMauro Carvalho Chehab 			ret);
2500c0d06caSMauro Carvalho Chehab 	}
2510c0d06caSMauro Carvalho Chehab }
2520c0d06caSMauro Carvalho Chehab 
2530c0d06caSMauro Carvalho Chehab int uvc_status_init(struct uvc_device *dev)
2540c0d06caSMauro Carvalho Chehab {
2550c0d06caSMauro Carvalho Chehab 	struct usb_host_endpoint *ep = dev->int_ep;
2560c0d06caSMauro Carvalho Chehab 	unsigned int pipe;
2570c0d06caSMauro Carvalho Chehab 	int interval;
2580c0d06caSMauro Carvalho Chehab 
2590c0d06caSMauro Carvalho Chehab 	if (ep == NULL)
2600c0d06caSMauro Carvalho Chehab 		return 0;
2610c0d06caSMauro Carvalho Chehab 
2620c0d06caSMauro Carvalho Chehab 	uvc_input_init(dev);
2630c0d06caSMauro Carvalho Chehab 
2640c0d06caSMauro Carvalho Chehab 	dev->status = kzalloc(UVC_MAX_STATUS_SIZE, GFP_KERNEL);
2650c0d06caSMauro Carvalho Chehab 	if (dev->status == NULL)
2660c0d06caSMauro Carvalho Chehab 		return -ENOMEM;
2670c0d06caSMauro Carvalho Chehab 
2680c0d06caSMauro Carvalho Chehab 	dev->int_urb = usb_alloc_urb(0, GFP_KERNEL);
2690c0d06caSMauro Carvalho Chehab 	if (dev->int_urb == NULL) {
2700c0d06caSMauro Carvalho Chehab 		kfree(dev->status);
2710c0d06caSMauro Carvalho Chehab 		return -ENOMEM;
2720c0d06caSMauro Carvalho Chehab 	}
2730c0d06caSMauro Carvalho Chehab 
2740c0d06caSMauro Carvalho Chehab 	pipe = usb_rcvintpipe(dev->udev, ep->desc.bEndpointAddress);
2750c0d06caSMauro Carvalho Chehab 
2760c0d06caSMauro Carvalho Chehab 	/* For high-speed interrupt endpoints, the bInterval value is used as
2770c0d06caSMauro Carvalho Chehab 	 * an exponent of two. Some developers forgot about it.
2780c0d06caSMauro Carvalho Chehab 	 */
2790c0d06caSMauro Carvalho Chehab 	interval = ep->desc.bInterval;
2800c0d06caSMauro Carvalho Chehab 	if (interval > 16 && dev->udev->speed == USB_SPEED_HIGH &&
2810c0d06caSMauro Carvalho Chehab 	    (dev->quirks & UVC_QUIRK_STATUS_INTERVAL))
2820c0d06caSMauro Carvalho Chehab 		interval = fls(interval) - 1;
2830c0d06caSMauro Carvalho Chehab 
2840c0d06caSMauro Carvalho Chehab 	usb_fill_int_urb(dev->int_urb, dev->udev, pipe,
2850c0d06caSMauro Carvalho Chehab 		dev->status, UVC_MAX_STATUS_SIZE, uvc_status_complete,
2860c0d06caSMauro Carvalho Chehab 		dev, interval);
2870c0d06caSMauro Carvalho Chehab 
2880c0d06caSMauro Carvalho Chehab 	return 0;
2890c0d06caSMauro Carvalho Chehab }
2900c0d06caSMauro Carvalho Chehab 
29110e1fdb9SDaniel Axtens void uvc_status_unregister(struct uvc_device *dev)
2920c0d06caSMauro Carvalho Chehab {
2930c0d06caSMauro Carvalho Chehab 	usb_kill_urb(dev->int_urb);
29410e1fdb9SDaniel Axtens 	uvc_input_unregister(dev);
29510e1fdb9SDaniel Axtens }
29610e1fdb9SDaniel Axtens 
29710e1fdb9SDaniel Axtens void uvc_status_cleanup(struct uvc_device *dev)
29810e1fdb9SDaniel Axtens {
2990c0d06caSMauro Carvalho Chehab 	usb_free_urb(dev->int_urb);
3000c0d06caSMauro Carvalho Chehab 	kfree(dev->status);
3010c0d06caSMauro Carvalho Chehab }
3020c0d06caSMauro Carvalho Chehab 
30317706f56SLaurent Pinchart int uvc_status_start(struct uvc_device *dev, gfp_t flags)
3040c0d06caSMauro Carvalho Chehab {
3050c0d06caSMauro Carvalho Chehab 	if (dev->int_urb == NULL)
3060c0d06caSMauro Carvalho Chehab 		return 0;
3070c0d06caSMauro Carvalho Chehab 
30817706f56SLaurent Pinchart 	return usb_submit_urb(dev->int_urb, flags);
3090c0d06caSMauro Carvalho Chehab }
3100c0d06caSMauro Carvalho Chehab 
3110c0d06caSMauro Carvalho Chehab void uvc_status_stop(struct uvc_device *dev)
3120c0d06caSMauro Carvalho Chehab {
3130c0d06caSMauro Carvalho Chehab 	usb_kill_urb(dev->int_urb);
3140c0d06caSMauro Carvalho Chehab }
315