xref: /linux/drivers/media/usb/uvc/uvc_status.c (revision 791d3ef2e11100449837dc0b6fe884e60ca3a484)
1 /*
2  *      uvc_status.c  --  USB Video Class driver - Status endpoint
3  *
4  *      Copyright (C) 2005-2009
5  *          Laurent Pinchart (laurent.pinchart@ideasonboard.com)
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>
15 #include <linux/input.h>
16 #include <linux/slab.h>
17 #include <linux/usb.h>
18 #include <linux/usb/input.h>
19 
20 #include "uvcvideo.h"
21 
22 /* --------------------------------------------------------------------------
23  * Input device
24  */
25 #ifdef CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV
26 static int uvc_input_init(struct uvc_device *dev)
27 {
28 	struct input_dev *input;
29 	int ret;
30 
31 	input = input_allocate_device();
32 	if (input == NULL)
33 		return -ENOMEM;
34 
35 	usb_make_path(dev->udev, dev->input_phys, sizeof(dev->input_phys));
36 	strlcat(dev->input_phys, "/button", sizeof(dev->input_phys));
37 
38 	input->name = dev->name;
39 	input->phys = dev->input_phys;
40 	usb_to_input_id(dev->udev, &input->id);
41 	input->dev.parent = &dev->intf->dev;
42 
43 	__set_bit(EV_KEY, input->evbit);
44 	__set_bit(KEY_CAMERA, input->keybit);
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);
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 
63 static void uvc_input_report_key(struct uvc_device *dev, unsigned int code,
64 	int value)
65 {
66 	if (dev->input) {
67 		input_report_key(dev->input, code, value);
68 		input_sync(dev->input);
69 	}
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 
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);
94 		uvc_input_report_key(dev, KEY_CAMERA, data[3]);
95 	} else {
96 		uvc_trace(UVC_TRACE_STATUS,
97 			  "Stream %u error event %02x len %d.\n",
98 			  data[1], data[2], len);
99 	}
100 }
101 
102 static void uvc_event_control(struct uvc_device *dev, u8 *data, int len)
103 {
104 	char *attrs[3] = { "value", "info", "failure" };
105 
106 	if (len < 6 || data[2] != 0 || data[4] > 2) {
107 		uvc_trace(UVC_TRACE_STATUS, "Invalid control status event "
108 				"received.\n");
109 		return;
110 	}
111 
112 	uvc_trace(UVC_TRACE_STATUS, "Control %u/%u %s change len %d.\n",
113 		data[1], data[3], attrs[data[4]], len);
114 }
115 
116 static void uvc_status_complete(struct urb *urb)
117 {
118 	struct uvc_device *dev = urb->context;
119 	int len, ret;
120 
121 	switch (urb->status) {
122 	case 0:
123 		break;
124 
125 	case -ENOENT:		/* usb_kill_urb() called. */
126 	case -ECONNRESET:	/* usb_unlink_urb() called. */
127 	case -ESHUTDOWN:	/* The endpoint is being disabled. */
128 	case -EPROTO:		/* Device is disconnected (reported by some
129 				 * host controller). */
130 		return;
131 
132 	default:
133 		uvc_printk(KERN_WARNING, "Non-zero status (%d) in status "
134 			"completion handler.\n", urb->status);
135 		return;
136 	}
137 
138 	len = urb->actual_length;
139 	if (len > 0) {
140 		switch (dev->status[0] & 0x0f) {
141 		case UVC_STATUS_TYPE_CONTROL:
142 			uvc_event_control(dev, dev->status, len);
143 			break;
144 
145 		case UVC_STATUS_TYPE_STREAMING:
146 			uvc_event_streaming(dev, dev->status, len);
147 			break;
148 
149 		default:
150 			uvc_trace(UVC_TRACE_STATUS, "Unknown status event "
151 				"type %u.\n", dev->status[0]);
152 			break;
153 		}
154 	}
155 
156 	/* Resubmit the URB. */
157 	urb->interval = dev->int_ep->desc.bInterval;
158 	if ((ret = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
159 		uvc_printk(KERN_ERR, "Failed to resubmit status URB (%d).\n",
160 			ret);
161 	}
162 }
163 
164 int uvc_status_init(struct uvc_device *dev)
165 {
166 	struct usb_host_endpoint *ep = dev->int_ep;
167 	unsigned int pipe;
168 	int interval;
169 
170 	if (ep == NULL)
171 		return 0;
172 
173 	uvc_input_init(dev);
174 
175 	dev->status = kzalloc(UVC_MAX_STATUS_SIZE, GFP_KERNEL);
176 	if (dev->status == NULL)
177 		return -ENOMEM;
178 
179 	dev->int_urb = usb_alloc_urb(0, GFP_KERNEL);
180 	if (dev->int_urb == NULL) {
181 		kfree(dev->status);
182 		return -ENOMEM;
183 	}
184 
185 	pipe = usb_rcvintpipe(dev->udev, ep->desc.bEndpointAddress);
186 
187 	/* For high-speed interrupt endpoints, the bInterval value is used as
188 	 * an exponent of two. Some developers forgot about it.
189 	 */
190 	interval = ep->desc.bInterval;
191 	if (interval > 16 && dev->udev->speed == USB_SPEED_HIGH &&
192 	    (dev->quirks & UVC_QUIRK_STATUS_INTERVAL))
193 		interval = fls(interval) - 1;
194 
195 	usb_fill_int_urb(dev->int_urb, dev->udev, pipe,
196 		dev->status, UVC_MAX_STATUS_SIZE, uvc_status_complete,
197 		dev, interval);
198 
199 	return 0;
200 }
201 
202 void uvc_status_cleanup(struct uvc_device *dev)
203 {
204 	usb_kill_urb(dev->int_urb);
205 	usb_free_urb(dev->int_urb);
206 	kfree(dev->status);
207 	uvc_input_cleanup(dev);
208 }
209 
210 int uvc_status_start(struct uvc_device *dev, gfp_t flags)
211 {
212 	if (dev->int_urb == NULL)
213 		return 0;
214 
215 	return usb_submit_urb(dev->int_urb, flags);
216 }
217 
218 void uvc_status_stop(struct uvc_device *dev)
219 {
220 	usb_kill_urb(dev->int_urb);
221 }
222