blob: a15ad0f3faf1e35ab8597d34fe55c611edf9d8f4 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/* Linux driver for Philips webcam
3 USB and Video4Linux interface part.
4 (C) 1999-2004 Nemosoft Unv.
Luc Saillard2b455db2006-04-24 10:29:46 -03005 (C) 2004-2006 Luc Saillard (luc@saillard.org)
Hans de Goede6eba9352011-06-26 06:49:59 -03006 (C) 2011 Hans de Goede <hdegoede@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007
8 NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
9 driver and thus may have bugs that are not present in the original version.
10 Please send bug reports and support requests to <luc@saillard.org>.
11 The decompression routines have been implemented by reverse-engineering the
12 Nemosoft binary pwcx module. Caveat emptor.
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15*/
16
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030017/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 This code forms the interface between the USB layers and the Philips
19 specific stuff. Some adanved stuff of the driver falls under an
20 NDA, signed between me and Philips B.V., Eindhoven, the Netherlands, and
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030021 is thus not distributed in source form. The binary pwcx.o module
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 contains the code that falls under the NDA.
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030023
24 In case you're wondering: 'pwc' stands for "Philips WebCam", but
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 I really didn't want to type 'philips_web_cam' every time (I'm lazy as
26 any Linux kernel hacker, but I don't like uncomprehensible abbreviations
27 without explanation).
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 Oh yes, convention: to disctinguish between all the various pointers to
30 device-structures, I use these names for the pointer variables:
31 udev: struct usb_device *
Hans de Goede9a7b2d12011-06-06 14:43:39 -030032 vdev: struct video_device (member of pwc_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 pdev: struct pwc_devive *
34*/
35
36/* Contributors:
37 - Alvarado: adding whitebalance code
38 - Alistar Moire: QuickCam 3000 Pro device/product ID
39 - Tony Hoyle: Creative Labs Webcam 5 device/product ID
40 - Mark Burazin: solving hang in VIDIOCSYNC when camera gets unplugged
41 - Jk Fang: Sotec Afina Eye ID
42 - Xavier Roche: QuickCam Pro 4000 ID
43 - Jens Knudsen: QuickCam Zoom ID
44 - J. Debert: QuickCam for Notebooks ID
Nam Phạm Thànhe32a7ecc2009-01-12 02:50:17 -030045 - Pham Thanh Nam: webcam snapshot button as an event input device
Linus Torvalds1da177e2005-04-16 15:20:36 -070046*/
47
48#include <linux/errno.h>
49#include <linux/init.h>
50#include <linux/mm.h>
51#include <linux/module.h>
52#include <linux/poll.h>
53#include <linux/slab.h>
Nam Phạm Thànhe32a7ecc2009-01-12 02:50:17 -030054#ifdef CONFIG_USB_PWC_INPUT_EVDEV
55#include <linux/usb/input.h>
56#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/vmalloc.h>
58#include <asm/io.h>
Andy Shevchenko2d8d7762009-09-24 07:58:09 -030059#include <linux/kernel.h> /* simple_strtol() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61#include "pwc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include "pwc-kiara.h"
63#include "pwc-timon.h"
Luc Saillard2b455db2006-04-24 10:29:46 -030064#include "pwc-dec23.h"
65#include "pwc-dec1.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Matwey V. Kornilovc1d5fb02018-11-09 14:03:26 -050067#define CREATE_TRACE_POINTS
68#include <trace/events/pwc.h>
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/* Function prototypes and driver templates */
71
72/* hotplug device table support */
Luc Saillard2b455db2006-04-24 10:29:46 -030073static const struct usb_device_id pwc_device_table [] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 { USB_DEVICE(0x0471, 0x0302) }, /* Philips models */
75 { USB_DEVICE(0x0471, 0x0303) },
76 { USB_DEVICE(0x0471, 0x0304) },
77 { USB_DEVICE(0x0471, 0x0307) },
78 { USB_DEVICE(0x0471, 0x0308) },
79 { USB_DEVICE(0x0471, 0x030C) },
80 { USB_DEVICE(0x0471, 0x0310) },
Luc Saillard2b455db2006-04-24 10:29:46 -030081 { USB_DEVICE(0x0471, 0x0311) }, /* Philips ToUcam PRO II */
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 { USB_DEVICE(0x0471, 0x0312) },
83 { USB_DEVICE(0x0471, 0x0313) }, /* the 'new' 720K */
Luc Saillard2b455db2006-04-24 10:29:46 -030084 { USB_DEVICE(0x0471, 0x0329) }, /* Philips SPC 900NC PC Camera */
Hans de Goede7445e452016-01-22 08:53:55 -020085 { USB_DEVICE(0x0471, 0x032C) }, /* Philips SPC 880NC PC Camera */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 { USB_DEVICE(0x069A, 0x0001) }, /* Askey */
87 { USB_DEVICE(0x046D, 0x08B0) }, /* Logitech QuickCam Pro 3000 */
88 { USB_DEVICE(0x046D, 0x08B1) }, /* Logitech QuickCam Notebook Pro */
89 { USB_DEVICE(0x046D, 0x08B2) }, /* Logitech QuickCam Pro 4000 */
90 { USB_DEVICE(0x046D, 0x08B3) }, /* Logitech QuickCam Zoom (old model) */
91 { USB_DEVICE(0x046D, 0x08B4) }, /* Logitech QuickCam Zoom (new model) */
92 { USB_DEVICE(0x046D, 0x08B5) }, /* Logitech QuickCam Orbit/Sphere */
Mauro Carvalho Chehab6b1ce3c2007-03-21 16:35:28 -030093 { USB_DEVICE(0x046D, 0x08B6) }, /* Cisco VT Camera */
94 { USB_DEVICE(0x046D, 0x08B7) }, /* Logitech ViewPort AV 100 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 { USB_DEVICE(0x046D, 0x08B8) }, /* Logitech (reserved) */
Luc Saillard2b455db2006-04-24 10:29:46 -030096 { USB_DEVICE(0x055D, 0x9000) }, /* Samsung MPC-C10 */
97 { USB_DEVICE(0x055D, 0x9001) }, /* Samsung MPC-C30 */
98 { USB_DEVICE(0x055D, 0x9002) }, /* Samsung SNC-35E (Ver3.0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 { USB_DEVICE(0x041E, 0x400C) }, /* Creative Webcam 5 */
100 { USB_DEVICE(0x041E, 0x4011) }, /* Creative Webcam Pro Ex */
101 { USB_DEVICE(0x04CC, 0x8116) }, /* Afina Eye */
102 { USB_DEVICE(0x06BE, 0x8116) }, /* new Afina Eye */
103 { USB_DEVICE(0x0d81, 0x1910) }, /* Visionite */
104 { USB_DEVICE(0x0d81, 0x1900) },
105 { }
106};
107MODULE_DEVICE_TABLE(usb, pwc_device_table);
108
109static int usb_pwc_probe(struct usb_interface *intf, const struct usb_device_id *id);
110static void usb_pwc_disconnect(struct usb_interface *intf);
Hans de Goede885fe182011-06-06 15:33:44 -0300111static void pwc_isoc_cleanup(struct pwc_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113static struct usb_driver pwc_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .name = "Philips webcam", /* name */
115 .id_table = pwc_device_table,
116 .probe = usb_pwc_probe, /* probe() */
117 .disconnect = usb_pwc_disconnect, /* disconnect() */
118};
119
120#define MAX_DEV_HINTS 20
121#define MAX_ISOC_ERRORS 20
122
Trent Piepho05ad3902007-01-30 23:26:01 -0300123#ifdef CONFIG_USB_PWC_DEBUG
Michael Krufkyb930e1d2007-08-27 18:16:54 -0300124 int pwc_trace = PWC_DEBUG_LEVEL;
Luc Saillard2b455db2006-04-24 10:29:46 -0300125#endif
Hans de Goede3b4d0ec2011-06-26 03:51:19 -0300126static int power_save = -1;
Hans de Goedec24e1372012-01-10 17:51:12 -0300127static int leds[2] = { 100, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129/***/
130
Hans Verkuilbec43662008-12-30 06:58:20 -0300131static const struct v4l2_file_operations pwc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 .owner = THIS_MODULE,
Hans de Goede76ae8532011-07-19 07:14:22 -0300133 .open = v4l2_fh_open,
Hans Verkuil2e43dec2012-07-02 05:51:58 -0300134 .release = vb2_fop_release,
135 .read = vb2_fop_read,
136 .poll = vb2_fop_poll,
137 .mmap = vb2_fop_mmap,
Hans Verkuilafa38522011-01-22 06:34:55 -0300138 .unlocked_ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139};
Bhumika Goyal86844942017-08-26 09:11:30 -0400140static const struct video_device pwc_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 .name = "Philips Webcam", /* Filled in later */
Hans de Goede76ae8532011-07-19 07:14:22 -0300142 .release = video_device_release_empty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 .fops = &pwc_fops,
Hans de Goede9a7b2d12011-06-06 14:43:39 -0300144 .ioctl_ops = &pwc_ioctl_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145};
146
147/***************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148/* Private functions */
149
Matwey V. Kornilov1161db62018-11-09 14:03:27 -0500150static void *pwc_alloc_urb_buffer(struct device *dev,
151 size_t size, dma_addr_t *dma_handle)
152{
153 void *buffer = kmalloc(size, GFP_KERNEL);
154
155 if (!buffer)
156 return NULL;
157
158 *dma_handle = dma_map_single(dev, buffer, size, DMA_FROM_DEVICE);
159 if (dma_mapping_error(dev, *dma_handle)) {
160 kfree(buffer);
161 return NULL;
162 }
163
164 return buffer;
165}
166
167static void pwc_free_urb_buffer(struct device *dev,
168 size_t size,
169 void *buffer,
170 dma_addr_t dma_handle)
171{
172 dma_unmap_single(dev, dma_handle, size, DMA_FROM_DEVICE);
173 kfree(buffer);
174}
175
Mauro Carvalho Chehab0dc6eb92012-10-27 14:26:35 -0300176static struct pwc_frame_buf *pwc_get_next_fill_buf(struct pwc_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Hans de Goede885fe182011-06-06 15:33:44 -0300178 unsigned long flags = 0;
179 struct pwc_frame_buf *buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Hans de Goede885fe182011-06-06 15:33:44 -0300181 spin_lock_irqsave(&pdev->queued_bufs_lock, flags);
182 if (list_empty(&pdev->queued_bufs))
183 goto leave;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Hans de Goede885fe182011-06-06 15:33:44 -0300185 buf = list_entry(pdev->queued_bufs.next, struct pwc_frame_buf, list);
186 list_del(&buf->list);
187leave:
188 spin_unlock_irqrestore(&pdev->queued_bufs_lock, flags);
189 return buf;
Luc Saillard2b455db2006-04-24 10:29:46 -0300190}
191
Nam Phạm Thànhe32a7ecc2009-01-12 02:50:17 -0300192static void pwc_snapshot_button(struct pwc_device *pdev, int down)
193{
194 if (down) {
195 PWC_TRACE("Snapshot button pressed.\n");
Nam Phạm Thànhe32a7ecc2009-01-12 02:50:17 -0300196 } else {
197 PWC_TRACE("Snapshot button released.\n");
198 }
199
200#ifdef CONFIG_USB_PWC_INPUT_EVDEV
201 if (pdev->button_dev) {
Lennart Poetteringbcd3e4b2009-06-11 11:19:33 -0300202 input_report_key(pdev->button_dev, KEY_CAMERA, down);
Nam Phạm Thànhe32a7ecc2009-01-12 02:50:17 -0300203 input_sync(pdev->button_dev);
204 }
205#endif
206}
207
Hans de Goede885fe182011-06-06 15:33:44 -0300208static void pwc_frame_complete(struct pwc_device *pdev)
Luc Saillard2b455db2006-04-24 10:29:46 -0300209{
Hans de Goede885fe182011-06-06 15:33:44 -0300210 struct pwc_frame_buf *fbuf = pdev->fill_buf;
Luc Saillard2b455db2006-04-24 10:29:46 -0300211
212 /* The ToUCam Fun CMOS sensor causes the firmware to send 2 or 3 bogus
213 frames on the USB wire after an exposure change. This conditition is
214 however detected in the cam and a bit is set in the header.
215 */
216 if (pdev->type == 730) {
217 unsigned char *ptr = (unsigned char *)fbuf->data;
218
219 if (ptr[1] == 1 && ptr[0] & 0x10) {
220 PWC_TRACE("Hyundai CMOS sensor bug. Dropping frame.\n");
221 pdev->drop_frames += 2;
Luc Saillard2b455db2006-04-24 10:29:46 -0300222 }
223 if ((ptr[0] ^ pdev->vmirror) & 0x01) {
Nam Phạm Thànhe32a7ecc2009-01-12 02:50:17 -0300224 pwc_snapshot_button(pdev, ptr[0] & 0x01);
Luc Saillard2b455db2006-04-24 10:29:46 -0300225 }
226 if ((ptr[0] ^ pdev->vmirror) & 0x02) {
227 if (ptr[0] & 0x02)
228 PWC_TRACE("Image is mirrored.\n");
229 else
230 PWC_TRACE("Image is normal.\n");
231 }
232 pdev->vmirror = ptr[0] & 0x03;
233 /* Sometimes the trailer of the 730 is still sent as a 4 byte packet
234 after a short frame; this condition is filtered out specifically. A 4 byte
235 frame doesn't make sense anyway.
236 So we get either this sequence:
237 drop_bit set -> 4 byte frame -> short frame -> good frame
238 Or this one:
239 drop_bit set -> short frame -> good frame
240 So we drop either 3 or 2 frames in all!
241 */
242 if (fbuf->filled == 4)
243 pdev->drop_frames++;
Hans de Goede885fe182011-06-06 15:33:44 -0300244 } else if (pdev->type == 740 || pdev->type == 720) {
Luc Saillard2b455db2006-04-24 10:29:46 -0300245 unsigned char *ptr = (unsigned char *)fbuf->data;
246 if ((ptr[0] ^ pdev->vmirror) & 0x01) {
Nam Phạm Thànhe32a7ecc2009-01-12 02:50:17 -0300247 pwc_snapshot_button(pdev, ptr[0] & 0x01);
Luc Saillard2b455db2006-04-24 10:29:46 -0300248 }
249 pdev->vmirror = ptr[0] & 0x03;
250 }
251
Hans de Goede885fe182011-06-06 15:33:44 -0300252 /* In case we were instructed to drop the frame, do so silently. */
253 if (pdev->drop_frames > 0) {
Luc Saillard2b455db2006-04-24 10:29:46 -0300254 pdev->drop_frames--;
Hans de Goede885fe182011-06-06 15:33:44 -0300255 } else {
Luc Saillard2b455db2006-04-24 10:29:46 -0300256 /* Check for underflow first */
257 if (fbuf->filled < pdev->frame_total_size) {
Mauro Carvalho Chehabc91e42f2016-10-18 17:44:18 -0200258 PWC_DEBUG_FLOW("Frame buffer underflow (%d bytes); discarded.\n",
259 fbuf->filled);
Hans de Goede885fe182011-06-06 15:33:44 -0300260 } else {
Junghak Sung2d700712015-09-22 10:30:30 -0300261 fbuf->vb.field = V4L2_FIELD_NONE;
262 fbuf->vb.sequence = pdev->vframe_count;
263 vb2_buffer_done(&fbuf->vb.vb2_buf, VB2_BUF_STATE_DONE);
Hans de Goede885fe182011-06-06 15:33:44 -0300264 pdev->fill_buf = NULL;
265 pdev->vsync = 0;
Luc Saillard2b455db2006-04-24 10:29:46 -0300266 }
267 } /* !drop_frames */
268 pdev->vframe_count++;
Luc Saillard2b455db2006-04-24 10:29:46 -0300269}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271/* This gets called for the Isochronous pipe (video). This is done in
272 * interrupt time, so it has to be fast, not crash, and not stall. Neat.
273 */
David Howells7d12e782006-10-05 14:55:46 +0100274static void pwc_isoc_handler(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Hans de Goede885fe182011-06-06 15:33:44 -0300276 struct pwc_device *pdev = (struct pwc_device *)urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 int i, fst, flen;
Hans de Goede885fe182011-06-06 15:33:44 -0300278 unsigned char *iso_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Matwey V. Kornilovc1d5fb02018-11-09 14:03:26 -0500280 trace_pwc_handler_enter(urb, pdev);
281
Hans de Goede885fe182011-06-06 15:33:44 -0300282 if (urb->status == -ENOENT || urb->status == -ECONNRESET ||
283 urb->status == -ESHUTDOWN) {
Colin Ian Kingb436e262017-11-02 06:11:53 -0400284 PWC_DEBUG_OPEN("URB (%p) unlinked %ssynchronously.\n",
285 urb, urb->status == -ENOENT ? "" : "a");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return;
287 }
Hans de Goede885fe182011-06-06 15:33:44 -0300288
289 if (pdev->fill_buf == NULL)
290 pdev->fill_buf = pwc_get_next_fill_buf(pdev);
291
292 if (urb->status != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 const char *errmsg;
294
295 errmsg = "Unknown";
296 switch(urb->status) {
297 case -ENOSR: errmsg = "Buffer error (overrun)"; break;
298 case -EPIPE: errmsg = "Stalled (device not responding)"; break;
299 case -EOVERFLOW: errmsg = "Babble (bad cable?)"; break;
300 case -EPROTO: errmsg = "Bit-stuff error (bad cable?)"; break;
301 case -EILSEQ: errmsg = "CRC/Timeout (could be anything)"; break;
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700302 case -ETIME: errmsg = "Device does not respond"; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
Hans de Goede885fe182011-06-06 15:33:44 -0300304 PWC_ERROR("pwc_isoc_handler() called with status %d [%s].\n",
305 urb->status, errmsg);
306 /* Give up after a number of contiguous errors */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (++pdev->visoc_errors > MAX_ISOC_ERRORS)
308 {
Hans de Goede885fe182011-06-06 15:33:44 -0300309 PWC_ERROR("Too many ISOC errors, bailing out.\n");
310 if (pdev->fill_buf) {
Junghak Sung2d700712015-09-22 10:30:30 -0300311 vb2_buffer_done(&pdev->fill_buf->vb.vb2_buf,
Hans de Goede885fe182011-06-06 15:33:44 -0300312 VB2_BUF_STATE_ERROR);
313 pdev->fill_buf = NULL;
314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
Hans de Goede885fe182011-06-06 15:33:44 -0300316 pdev->vsync = 0; /* Drop the current frame */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 goto handler_end;
318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 /* Reset ISOC error counter. We did get here, after all. */
321 pdev->visoc_errors = 0;
322
Matwey V. Kornilov1161db62018-11-09 14:03:27 -0500323 dma_sync_single_for_cpu(&urb->dev->dev,
324 urb->transfer_dma,
325 urb->transfer_buffer_length,
326 DMA_FROM_DEVICE);
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 /* vsync: 0 = don't copy data
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300329 1 = sync-hunt
330 2 = synched
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 */
332 /* Compact data */
333 for (i = 0; i < urb->number_of_packets; i++) {
334 fst = urb->iso_frame_desc[i].status;
335 flen = urb->iso_frame_desc[i].actual_length;
336 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Hans de Goede885fe182011-06-06 15:33:44 -0300337 if (fst != 0) {
338 PWC_ERROR("Iso frame %d has error %d\n", i, fst);
339 continue;
340 }
341 if (flen > 0 && pdev->vsync) {
342 struct pwc_frame_buf *fbuf = pdev->fill_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Hans de Goede885fe182011-06-06 15:33:44 -0300344 if (pdev->vsync == 1) {
Junghak Sungd6dd6452015-11-03 08:16:37 -0200345 fbuf->vb.vb2_buf.timestamp = ktime_get_ns();
Hans de Goede885fe182011-06-06 15:33:44 -0300346 pdev->vsync = 2;
Luc Saillard2b455db2006-04-24 10:29:46 -0300347 }
348
Hans de Goede885fe182011-06-06 15:33:44 -0300349 if (flen + fbuf->filled > pdev->frame_total_size) {
350 PWC_ERROR("Frame overflow (%d > %d)\n",
351 flen + fbuf->filled,
352 pdev->frame_total_size);
353 pdev->vsync = 0; /* Let's wait for an EOF */
354 } else {
355 memcpy(fbuf->data + fbuf->filled, iso_buf,
356 flen);
357 fbuf->filled += flen;
358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
Hans de Goede885fe182011-06-06 15:33:44 -0300360 if (flen < pdev->vlast_packet_size) {
361 /* Shorter packet... end of frame */
362 if (pdev->vsync == 2)
363 pwc_frame_complete(pdev);
364 if (pdev->fill_buf == NULL)
365 pdev->fill_buf = pwc_get_next_fill_buf(pdev);
366 if (pdev->fill_buf) {
367 pdev->fill_buf->filled = 0;
368 pdev->vsync = 1;
369 }
370 }
371 pdev->vlast_packet_size = flen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373
Matwey V. Kornilov1161db62018-11-09 14:03:27 -0500374 dma_sync_single_for_device(&urb->dev->dev,
375 urb->transfer_dma,
376 urb->transfer_buffer_length,
377 DMA_FROM_DEVICE);
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379handler_end:
Matwey V. Kornilovc1d5fb02018-11-09 14:03:26 -0500380 trace_pwc_handler_exit(urb, pdev);
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 i = usb_submit_urb(urb, GFP_ATOMIC);
383 if (i != 0)
Luc Saillard2b455db2006-04-24 10:29:46 -0300384 PWC_ERROR("Error (%d) re-submitting urb in pwc_isoc_handler.\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
Hans de Goedeceede9f2012-05-09 04:43:12 -0300387/* Both v4l2_lock and vb_queue_lock should be locked when calling this */
Hans de Goede885fe182011-06-06 15:33:44 -0300388static int pwc_isoc_init(struct pwc_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
390 struct usb_device *udev;
391 struct urb *urb;
392 int i, j, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 struct usb_interface *intf;
394 struct usb_host_interface *idesc = NULL;
Hans de Goede5bbe18d2012-01-04 18:48:05 -0300395 int compression = 0; /* 0..3 = uncompressed..high */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 pdev->vsync = 0;
Hans de Goede6eba9352011-06-26 06:49:59 -0300398 pdev->vlast_packet_size = 0;
Hans de Goede885fe182011-06-06 15:33:44 -0300399 pdev->fill_buf = NULL;
400 pdev->vframe_count = 0;
Hans de Goede6eba9352011-06-26 06:49:59 -0300401 pdev->visoc_errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 udev = pdev->udev;
403
Hans de Goede5bbe18d2012-01-04 18:48:05 -0300404retry:
405 /* We first try with low compression and then retry with a higher
406 compression setting if there is not enough bandwidth. */
Hans de Goede938d5b92012-01-10 13:14:46 -0300407 ret = pwc_set_video_mode(pdev, pdev->width, pdev->height, pdev->pixfmt,
408 pdev->vframes, &compression, 1);
Hans de Goede5bbe18d2012-01-04 18:48:05 -0300409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 /* Get the current alternate interface, adjust packet size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 intf = usb_ifnum_to_if(udev, 0);
412 if (intf)
413 idesc = usb_altnum_to_altsetting(intf, pdev->valternate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (!idesc)
Hans de Goedec2464122011-06-06 15:25:18 -0300415 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 /* Search video endpoint */
418 pdev->vmax_packet_size = -1;
Luc Saillard2b455db2006-04-24 10:29:46 -0300419 for (i = 0; i < idesc->desc.bNumEndpoints; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if ((idesc->endpoint[i].desc.bEndpointAddress & 0xF) == pdev->vendpoint) {
421 pdev->vmax_packet_size = le16_to_cpu(idesc->endpoint[i].desc.wMaxPacketSize);
422 break;
423 }
Luc Saillard2b455db2006-04-24 10:29:46 -0300424 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (pdev->vmax_packet_size < 0 || pdev->vmax_packet_size > ISO_MAX_FRAME_SIZE) {
Luc Saillard2b455db2006-04-24 10:29:46 -0300427 PWC_ERROR("Failed to find packet size for video endpoint in current alternate setting.\n");
Steven Cole093cf722005-05-03 19:07:24 -0600428 return -ENFILE; /* Odd error, that should be noticeable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430
431 /* Set alternate interface */
Luc Saillard2b455db2006-04-24 10:29:46 -0300432 PWC_DEBUG_OPEN("Setting alternate interface %d\n", pdev->valternate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 ret = usb_set_interface(pdev->udev, 0, pdev->valternate);
Hans de Goede5bbe18d2012-01-04 18:48:05 -0300434 if (ret == -ENOSPC && compression < 3) {
435 compression++;
436 goto retry;
437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (ret < 0)
439 return ret;
440
Hans de Goede04613c52011-06-26 13:57:15 -0300441 /* Allocate and init Isochronuous urbs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 for (i = 0; i < MAX_ISO_BUFS; i++) {
443 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
444 if (urb == NULL) {
Hans de Goede6eba9352011-06-26 06:49:59 -0300445 pwc_isoc_cleanup(pdev);
446 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
Hans de Goede04613c52011-06-26 13:57:15 -0300448 pdev->urbs[i] = urb;
Luc Saillard2b455db2006-04-24 10:29:46 -0300449 PWC_DEBUG_MEMORY("Allocated URB at 0x%p\n", urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 urb->interval = 1; // devik
452 urb->dev = udev;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300453 urb->pipe = usb_rcvisocpipe(udev, pdev->vendpoint);
Hans de Goede04613c52011-06-26 13:57:15 -0300454 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
Matwey V. Kornilov1161db62018-11-09 14:03:27 -0500455 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
456 urb->transfer_buffer = pwc_alloc_urb_buffer(&udev->dev,
457 urb->transfer_buffer_length,
458 &urb->transfer_dma);
Hans de Goede04613c52011-06-26 13:57:15 -0300459 if (urb->transfer_buffer == NULL) {
460 PWC_ERROR("Failed to allocate urb buffer %d\n", i);
Hans de Goede04613c52011-06-26 13:57:15 -0300461 pwc_isoc_cleanup(pdev);
462 return -ENOMEM;
463 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300464 urb->complete = pwc_isoc_handler;
465 urb->context = pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 urb->start_frame = 0;
467 urb->number_of_packets = ISO_FRAMES_PER_DESC;
468 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
469 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
470 urb->iso_frame_desc[j].length = pdev->vmax_packet_size;
471 }
472 }
473
474 /* link */
475 for (i = 0; i < MAX_ISO_BUFS; i++) {
Hans de Goede04613c52011-06-26 13:57:15 -0300476 ret = usb_submit_urb(pdev->urbs[i], GFP_KERNEL);
Hans de Goede5bbe18d2012-01-04 18:48:05 -0300477 if (ret == -ENOSPC && compression < 3) {
478 compression++;
Hans de Goede5bbe18d2012-01-04 18:48:05 -0300479 pwc_isoc_cleanup(pdev);
480 goto retry;
481 }
Hans de Goede622d9f52010-11-16 12:32:09 -0300482 if (ret) {
Luc Saillard2b455db2006-04-24 10:29:46 -0300483 PWC_ERROR("isoc_init() submit_urb %d failed with error %d\n", i, ret);
Hans de Goede622d9f52010-11-16 12:32:09 -0300484 pwc_isoc_cleanup(pdev);
485 return ret;
486 }
Hans de Goede04613c52011-06-26 13:57:15 -0300487 PWC_DEBUG_MEMORY("URB 0x%p submitted.\n", pdev->urbs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489
490 /* All is done... */
Luc Saillard2b455db2006-04-24 10:29:46 -0300491 PWC_DEBUG_OPEN("<< pwc_isoc_init()\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return 0;
493}
494
Oliver Neukum0b67f5c2007-09-26 10:19:01 -0300495static void pwc_iso_stop(struct pwc_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
497 int i;
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 /* Unlinking ISOC buffers one by one */
500 for (i = 0; i < MAX_ISO_BUFS; i++) {
Hans de Goede04613c52011-06-26 13:57:15 -0300501 if (pdev->urbs[i]) {
502 PWC_DEBUG_MEMORY("Unlinking URB %p\n", pdev->urbs[i]);
503 usb_kill_urb(pdev->urbs[i]);
Oliver Neukum0b67f5c2007-09-26 10:19:01 -0300504 }
505 }
506}
507
508static void pwc_iso_free(struct pwc_device *pdev)
509{
510 int i;
511
512 /* Freeing ISOC buffers one by one */
513 for (i = 0; i < MAX_ISO_BUFS; i++) {
Matwey V. Kornilov1161db62018-11-09 14:03:27 -0500514 struct urb *urb = pdev->urbs[i];
515
516 if (urb) {
Luc Saillard2b455db2006-04-24 10:29:46 -0300517 PWC_DEBUG_MEMORY("Freeing URB\n");
Matwey V. Kornilov1161db62018-11-09 14:03:27 -0500518 if (urb->transfer_buffer)
519 pwc_free_urb_buffer(&urb->dev->dev,
520 urb->transfer_buffer_length,
521 urb->transfer_buffer,
522 urb->transfer_dma);
523 usb_free_urb(urb);
Hans de Goede04613c52011-06-26 13:57:15 -0300524 pdev->urbs[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
526 }
Oliver Neukum0b67f5c2007-09-26 10:19:01 -0300527}
528
Hans de Goedeceede9f2012-05-09 04:43:12 -0300529/* Both v4l2_lock and vb_queue_lock should be locked when calling this */
Hans de Goede885fe182011-06-06 15:33:44 -0300530static void pwc_isoc_cleanup(struct pwc_device *pdev)
Oliver Neukum0b67f5c2007-09-26 10:19:01 -0300531{
532 PWC_DEBUG_OPEN(">> pwc_isoc_cleanup()\n");
Hans de Goedec2464122011-06-06 15:25:18 -0300533
Oliver Neukum0b67f5c2007-09-26 10:19:01 -0300534 pwc_iso_stop(pdev);
535 pwc_iso_free(pdev);
Hans de Goedeb824bb42011-06-25 17:39:19 -0300536 usb_set_interface(pdev->udev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Luc Saillard2b455db2006-04-24 10:29:46 -0300538 PWC_DEBUG_OPEN("<< pwc_isoc_cleanup()\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
Hans de Goedeceede9f2012-05-09 04:43:12 -0300541/* Must be called with vb_queue_lock hold */
Hans Verkuil80b09632014-08-04 07:29:59 -0300542static void pwc_cleanup_queued_bufs(struct pwc_device *pdev,
543 enum vb2_buffer_state state)
Hans de Goede885fe182011-06-06 15:33:44 -0300544{
Hans de Goedec20d78c2011-10-09 09:16:46 -0300545 unsigned long flags = 0;
546
547 spin_lock_irqsave(&pdev->queued_bufs_lock, flags);
Hans de Goede885fe182011-06-06 15:33:44 -0300548 while (!list_empty(&pdev->queued_bufs)) {
549 struct pwc_frame_buf *buf;
550
551 buf = list_entry(pdev->queued_bufs.next, struct pwc_frame_buf,
552 list);
553 list_del(&buf->list);
Junghak Sung2d700712015-09-22 10:30:30 -0300554 vb2_buffer_done(&buf->vb.vb2_buf, state);
Hans de Goede885fe182011-06-06 15:33:44 -0300555 }
Hans de Goedec20d78c2011-10-09 09:16:46 -0300556 spin_unlock_irqrestore(&pdev->queued_bufs_lock, flags);
Hans de Goede885fe182011-06-06 15:33:44 -0300557}
558
Trent Piepho05ad3902007-01-30 23:26:01 -0300559#ifdef CONFIG_USB_PWC_DEBUG
Luc Saillard2b455db2006-04-24 10:29:46 -0300560static const char *pwc_sensor_type_to_string(unsigned int sensor_type)
561{
562 switch(sensor_type) {
563 case 0x00:
564 return "Hyundai CMOS sensor";
565 case 0x20:
566 return "Sony CCD sensor + TDA8787";
567 case 0x2E:
568 return "Sony CCD sensor + Exas 98L59";
569 case 0x2F:
570 return "Sony CCD sensor + ADI 9804";
571 case 0x30:
572 return "Sharp CCD sensor + TDA8787";
573 case 0x3E:
574 return "Sharp CCD sensor + Exas 98L59";
575 case 0x3F:
576 return "Sharp CCD sensor + ADI 9804";
577 case 0x40:
578 return "UPA 1021 sensor";
579 case 0x100:
580 return "VGA sensor";
581 case 0x101:
582 return "PAL MR sensor";
583 default:
Trent Piepho657de3c2006-06-20 00:30:57 -0300584 return "unknown type of sensor";
Luc Saillard2b455db2006-04-24 10:29:46 -0300585 }
586}
587#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589/***************************************************************************/
590/* Video4Linux functions */
591
Hans de Goede76ae8532011-07-19 07:14:22 -0300592static void pwc_video_release(struct v4l2_device *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Hans de Goede76ae8532011-07-19 07:14:22 -0300594 struct pwc_device *pdev = container_of(v, struct pwc_device, v4l2_dev);
Dmitry Torokhov89dec012009-08-14 02:22:52 -0300595
Hans de Goede6c9cac82011-06-26 12:52:01 -0300596 v4l2_ctrl_handler_free(&pdev->ctrl_handler);
Hans de Goedeceede9f2012-05-09 04:43:12 -0300597 v4l2_device_unregister(&pdev->v4l2_dev);
Hans de Goede24be6892012-01-10 17:02:04 -0300598 kfree(pdev->ctrl_buf);
Dmitry Torokhov89dec012009-08-14 02:22:52 -0300599 kfree(pdev);
Oliver Neukum85237f22007-08-21 07:10:42 +0200600}
601
Hans de Goede885fe182011-06-06 15:33:44 -0300602/***************************************************************************/
603/* Videobuf2 operations */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200605static int queue_setup(struct vb2_queue *vq,
Guennadi Liakhovetskifc714e702011-08-24 10:30:21 -0300606 unsigned int *nbuffers, unsigned int *nplanes,
Hans Verkuil36c0f8b2016-04-15 09:15:05 -0300607 unsigned int sizes[], struct device *alloc_devs[])
Hans de Goede885fe182011-06-06 15:33:44 -0300608{
609 struct pwc_device *pdev = vb2_get_drv_priv(vq);
Hans de Goede795e6eb2012-01-04 16:58:44 -0300610 int size;
Luc Saillard2b455db2006-04-24 10:29:46 -0300611
Hans de Goede885fe182011-06-06 15:33:44 -0300612 if (*nbuffers < MIN_FRAMES)
613 *nbuffers = MIN_FRAMES;
614 else if (*nbuffers > MAX_FRAMES)
615 *nbuffers = MAX_FRAMES;
616
617 *nplanes = 1;
618
Hans de Goede795e6eb2012-01-04 16:58:44 -0300619 size = pwc_get_size(pdev, MAX_WIDTH, MAX_HEIGHT);
620 sizes[0] = PAGE_ALIGN(pwc_image_sizes[size][0] *
621 pwc_image_sizes[size][1] * 3 / 2);
Hans de Goede885fe182011-06-06 15:33:44 -0300622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return 0;
624}
625
Hans de Goede885fe182011-06-06 15:33:44 -0300626static int buffer_init(struct vb2_buffer *vb)
627{
Junghak Sung2d700712015-09-22 10:30:30 -0300628 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
629 struct pwc_frame_buf *buf =
630 container_of(vbuf, struct pwc_frame_buf, vb);
Hans de Goede885fe182011-06-06 15:33:44 -0300631
632 /* need vmalloc since frame buffer > 128K */
633 buf->data = vzalloc(PWC_FRAME_SIZE);
634 if (buf->data == NULL)
635 return -ENOMEM;
636
637 return 0;
638}
639
640static int buffer_prepare(struct vb2_buffer *vb)
641{
642 struct pwc_device *pdev = vb2_get_drv_priv(vb->vb2_queue);
643
Mauro Carvalho Chehab3e4d8f42019-02-18 14:29:03 -0500644 /* Don't allow queueing new buffers after device disconnection */
Hans de Goedeb824bb42011-06-25 17:39:19 -0300645 if (!pdev->udev)
646 return -ENODEV;
Hans de Goede885fe182011-06-06 15:33:44 -0300647
648 return 0;
649}
650
Hans Verkuil06470642014-03-04 07:27:13 -0300651static void buffer_finish(struct vb2_buffer *vb)
Hans de Goede885fe182011-06-06 15:33:44 -0300652{
653 struct pwc_device *pdev = vb2_get_drv_priv(vb->vb2_queue);
Junghak Sung2d700712015-09-22 10:30:30 -0300654 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
655 struct pwc_frame_buf *buf =
656 container_of(vbuf, struct pwc_frame_buf, vb);
Hans de Goede885fe182011-06-06 15:33:44 -0300657
Hans Verkuil1a179482014-03-04 07:28:11 -0300658 if (vb->state == VB2_BUF_STATE_DONE) {
659 /*
660 * Application has called dqbuf and is getting back a buffer
661 * we've filled, take the pwc data we've stored in buf->data
662 * and decompress it into a usable format, storing the result
663 * in the vb2_buffer.
664 */
665 pwc_decompress(pdev, buf);
666 }
Hans de Goede885fe182011-06-06 15:33:44 -0300667}
668
669static void buffer_cleanup(struct vb2_buffer *vb)
670{
Junghak Sung2d700712015-09-22 10:30:30 -0300671 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
672 struct pwc_frame_buf *buf =
673 container_of(vbuf, struct pwc_frame_buf, vb);
Hans de Goede885fe182011-06-06 15:33:44 -0300674
675 vfree(buf->data);
676}
677
678static void buffer_queue(struct vb2_buffer *vb)
679{
680 struct pwc_device *pdev = vb2_get_drv_priv(vb->vb2_queue);
Junghak Sung2d700712015-09-22 10:30:30 -0300681 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
682 struct pwc_frame_buf *buf =
683 container_of(vbuf, struct pwc_frame_buf, vb);
Hans de Goede885fe182011-06-06 15:33:44 -0300684 unsigned long flags = 0;
685
Hans de Goedec20d78c2011-10-09 09:16:46 -0300686 /* Check the device has not disconnected between prep and queuing */
Hans de Goedeceede9f2012-05-09 04:43:12 -0300687 if (!pdev->udev) {
Junghak Sung2d700712015-09-22 10:30:30 -0300688 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
Hans de Goedeceede9f2012-05-09 04:43:12 -0300689 return;
690 }
691
692 spin_lock_irqsave(&pdev->queued_bufs_lock, flags);
693 list_add_tail(&buf->list, &pdev->queued_bufs);
Hans de Goede885fe182011-06-06 15:33:44 -0300694 spin_unlock_irqrestore(&pdev->queued_bufs_lock, flags);
695}
696
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300697static int start_streaming(struct vb2_queue *vq, unsigned int count)
Hans de Goede885fe182011-06-06 15:33:44 -0300698{
699 struct pwc_device *pdev = vb2_get_drv_priv(vq);
Hans de Goedec20d78c2011-10-09 09:16:46 -0300700 int r;
Hans de Goede885fe182011-06-06 15:33:44 -0300701
Hans de Goedeceede9f2012-05-09 04:43:12 -0300702 if (!pdev->udev)
703 return -ENODEV;
Hans de Goedeb824bb42011-06-25 17:39:19 -0300704
Hans Verkuil2e43dec2012-07-02 05:51:58 -0300705 if (mutex_lock_interruptible(&pdev->v4l2_lock))
706 return -ERESTARTSYS;
Hans de Goede6eba9352011-06-26 06:49:59 -0300707 /* Turn on camera and set LEDS on */
708 pwc_camera_power(pdev, 1);
Hans de Goedec24e1372012-01-10 17:51:12 -0300709 pwc_set_leds(pdev, leds[0], leds[1]);
Hans de Goede6eba9352011-06-26 06:49:59 -0300710
Hans de Goedec20d78c2011-10-09 09:16:46 -0300711 r = pwc_isoc_init(pdev);
Hans de Goede5bbe18d2012-01-04 18:48:05 -0300712 if (r) {
713 /* If we failed turn camera and LEDS back off */
714 pwc_set_leds(pdev, 0, 0);
715 pwc_camera_power(pdev, 0);
716 /* And cleanup any queued bufs!! */
Hans Verkuil80b09632014-08-04 07:29:59 -0300717 pwc_cleanup_queued_bufs(pdev, VB2_BUF_STATE_QUEUED);
Hans de Goede5bbe18d2012-01-04 18:48:05 -0300718 }
Hans Verkuil2e43dec2012-07-02 05:51:58 -0300719 mutex_unlock(&pdev->v4l2_lock);
Hans de Goedeceede9f2012-05-09 04:43:12 -0300720
Hans de Goedec20d78c2011-10-09 09:16:46 -0300721 return r;
Hans de Goede885fe182011-06-06 15:33:44 -0300722}
723
Hans Verkuile37559b2014-04-17 02:47:21 -0300724static void stop_streaming(struct vb2_queue *vq)
Hans de Goede885fe182011-06-06 15:33:44 -0300725{
726 struct pwc_device *pdev = vb2_get_drv_priv(vq);
727
Hans Verkuile37559b2014-04-17 02:47:21 -0300728 mutex_lock(&pdev->v4l2_lock);
Hans de Goede6eba9352011-06-26 06:49:59 -0300729 if (pdev->udev) {
730 pwc_set_leds(pdev, 0, 0);
731 pwc_camera_power(pdev, 0);
Hans de Goedeb824bb42011-06-25 17:39:19 -0300732 pwc_isoc_cleanup(pdev);
Hans de Goede6eba9352011-06-26 06:49:59 -0300733 }
Hans de Goedec20d78c2011-10-09 09:16:46 -0300734
Hans Verkuil80b09632014-08-04 07:29:59 -0300735 pwc_cleanup_queued_bufs(pdev, VB2_BUF_STATE_ERROR);
736 if (pdev->fill_buf)
Junghak Sung2d700712015-09-22 10:30:30 -0300737 vb2_buffer_done(&pdev->fill_buf->vb.vb2_buf,
738 VB2_BUF_STATE_ERROR);
Hans Verkuil2e43dec2012-07-02 05:51:58 -0300739 mutex_unlock(&pdev->v4l2_lock);
Hans de Goede885fe182011-06-06 15:33:44 -0300740}
741
Julia Lawall1bc17712016-09-08 20:59:01 -0300742static const struct vb2_ops pwc_vb_queue_ops = {
Hans de Goede885fe182011-06-06 15:33:44 -0300743 .queue_setup = queue_setup,
744 .buf_init = buffer_init,
745 .buf_prepare = buffer_prepare,
746 .buf_finish = buffer_finish,
747 .buf_cleanup = buffer_cleanup,
748 .buf_queue = buffer_queue,
749 .start_streaming = start_streaming,
750 .stop_streaming = stop_streaming,
Hans Verkuil2e43dec2012-07-02 05:51:58 -0300751 .wait_prepare = vb2_ops_wait_prepare,
752 .wait_finish = vb2_ops_wait_finish,
Hans de Goede885fe182011-06-06 15:33:44 -0300753};
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755/***************************************************************************/
756/* USB functions */
757
758/* This function gets called when a new device is plugged in or the usb core
759 * is loaded.
760 */
761
762static int usb_pwc_probe(struct usb_interface *intf, const struct usb_device_id *id)
763{
764 struct usb_device *udev = interface_to_usbdev(intf);
765 struct pwc_device *pdev = NULL;
766 int vendor_id, product_id, type_id;
Hans de Goedea081c342012-01-10 17:47:25 -0300767 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 int features = 0;
Hans de Goede5bbe18d2012-01-04 18:48:05 -0300769 int compression = 0;
Hans de Goede3b4d0ec2011-06-26 03:51:19 -0300770 int my_power_save = power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 char serial_number[30], *name;
772
Luc Saillard2b455db2006-04-24 10:29:46 -0300773 vendor_id = le16_to_cpu(udev->descriptor.idVendor);
774 product_id = le16_to_cpu(udev->descriptor.idProduct);
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 /* Check if we can handle this device */
Luc Saillard2b455db2006-04-24 10:29:46 -0300777 PWC_DEBUG_PROBE("probe() called [%04X %04X], if %d\n",
778 vendor_id, product_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 intf->altsetting->desc.bInterfaceNumber);
780
781 /* the interfaces are probed one by one. We are only interested in the
782 video interface (0) now.
783 Interface 1 is the Audio Control, and interface 2 Audio itself.
784 */
785 if (intf->altsetting->desc.bInterfaceNumber > 0)
786 return -ENODEV;
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (vendor_id == 0x0471) {
789 switch (product_id) {
790 case 0x0302:
Luc Saillard2b455db2006-04-24 10:29:46 -0300791 PWC_INFO("Philips PCA645VC USB webcam detected.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 name = "Philips 645 webcam";
793 type_id = 645;
794 break;
795 case 0x0303:
Luc Saillard2b455db2006-04-24 10:29:46 -0300796 PWC_INFO("Philips PCA646VC USB webcam detected.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 name = "Philips 646 webcam";
798 type_id = 646;
799 break;
800 case 0x0304:
Luc Saillard2b455db2006-04-24 10:29:46 -0300801 PWC_INFO("Askey VC010 type 2 USB webcam detected.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 name = "Askey VC010 webcam";
803 type_id = 646;
804 break;
805 case 0x0307:
Luc Saillard2b455db2006-04-24 10:29:46 -0300806 PWC_INFO("Philips PCVC675K (Vesta) USB webcam detected.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 name = "Philips 675 webcam";
808 type_id = 675;
809 break;
810 case 0x0308:
Luc Saillard2b455db2006-04-24 10:29:46 -0300811 PWC_INFO("Philips PCVC680K (Vesta Pro) USB webcam detected.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 name = "Philips 680 webcam";
813 type_id = 680;
814 break;
815 case 0x030C:
Luc Saillard2b455db2006-04-24 10:29:46 -0300816 PWC_INFO("Philips PCVC690K (Vesta Pro Scan) USB webcam detected.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 name = "Philips 690 webcam";
818 type_id = 690;
819 break;
820 case 0x0310:
Luc Saillard2b455db2006-04-24 10:29:46 -0300821 PWC_INFO("Philips PCVC730K (ToUCam Fun)/PCVC830 (ToUCam II) USB webcam detected.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 name = "Philips 730 webcam";
823 type_id = 730;
824 break;
825 case 0x0311:
Luc Saillard2b455db2006-04-24 10:29:46 -0300826 PWC_INFO("Philips PCVC740K (ToUCam Pro)/PCVC840 (ToUCam II) USB webcam detected.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 name = "Philips 740 webcam";
828 type_id = 740;
829 break;
830 case 0x0312:
Luc Saillard2b455db2006-04-24 10:29:46 -0300831 PWC_INFO("Philips PCVC750K (ToUCam Pro Scan) USB webcam detected.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 name = "Philips 750 webcam";
833 type_id = 750;
834 break;
835 case 0x0313:
Luc Saillard2b455db2006-04-24 10:29:46 -0300836 PWC_INFO("Philips PCVC720K/40 (ToUCam XS) USB webcam detected.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 name = "Philips 720K/40 webcam";
838 type_id = 720;
839 break;
Luc Saillard2b455db2006-04-24 10:29:46 -0300840 case 0x0329:
841 PWC_INFO("Philips SPC 900NC USB webcam detected.\n");
842 name = "Philips SPC 900NC webcam";
Luc Saillard9ee6d782007-04-22 23:54:36 -0300843 type_id = 740;
Luc Saillard2b455db2006-04-24 10:29:46 -0300844 break;
Hans de Goede7445e452016-01-22 08:53:55 -0200845 case 0x032C:
846 PWC_INFO("Philips SPC 880NC USB webcam detected.\n");
847 name = "Philips SPC 880NC webcam";
848 type_id = 740;
849 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 default:
851 return -ENODEV;
852 break;
853 }
854 }
855 else if (vendor_id == 0x069A) {
856 switch(product_id) {
857 case 0x0001:
Luc Saillard2b455db2006-04-24 10:29:46 -0300858 PWC_INFO("Askey VC010 type 1 USB webcam detected.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 name = "Askey VC010 webcam";
860 type_id = 645;
861 break;
862 default:
863 return -ENODEV;
864 break;
865 }
866 }
867 else if (vendor_id == 0x046d) {
868 switch(product_id) {
869 case 0x08b0:
Luc Saillard2b455db2006-04-24 10:29:46 -0300870 PWC_INFO("Logitech QuickCam Pro 3000 USB webcam detected.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 name = "Logitech QuickCam Pro 3000";
872 type_id = 740; /* CCD sensor */
873 break;
874 case 0x08b1:
Luc Saillard2b455db2006-04-24 10:29:46 -0300875 PWC_INFO("Logitech QuickCam Notebook Pro USB webcam detected.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 name = "Logitech QuickCam Notebook Pro";
877 type_id = 740; /* CCD sensor */
878 break;
879 case 0x08b2:
Luc Saillard2b455db2006-04-24 10:29:46 -0300880 PWC_INFO("Logitech QuickCam 4000 Pro USB webcam detected.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 name = "Logitech QuickCam Pro 4000";
882 type_id = 740; /* CCD sensor */
Hans de Goede51886df2011-07-03 15:52:54 -0300883 if (my_power_save == -1)
884 my_power_save = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 break;
886 case 0x08b3:
Luc Saillard2b455db2006-04-24 10:29:46 -0300887 PWC_INFO("Logitech QuickCam Zoom USB webcam detected.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 name = "Logitech QuickCam Zoom";
889 type_id = 740; /* CCD sensor */
890 break;
891 case 0x08B4:
Luc Saillard2b455db2006-04-24 10:29:46 -0300892 PWC_INFO("Logitech QuickCam Zoom (new model) USB webcam detected.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 name = "Logitech QuickCam Zoom";
894 type_id = 740; /* CCD sensor */
Hans de Goede3b4d0ec2011-06-26 03:51:19 -0300895 if (my_power_save == -1)
896 my_power_save = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 break;
898 case 0x08b5:
Luc Saillard2b455db2006-04-24 10:29:46 -0300899 PWC_INFO("Logitech QuickCam Orbit/Sphere USB webcam detected.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 name = "Logitech QuickCam Orbit";
901 type_id = 740; /* CCD sensor */
Hans de Goede51886df2011-07-03 15:52:54 -0300902 if (my_power_save == -1)
903 my_power_save = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 features |= FEATURE_MOTOR_PANTILT;
905 break;
906 case 0x08b6:
Jean Tourrilhesa63e1572007-03-21 16:29:16 -0300907 PWC_INFO("Logitech/Cisco VT Camera webcam detected.\n");
908 name = "Cisco VT Camera";
909 type_id = 740; /* CCD sensor */
910 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 case 0x08b7:
Mauro Carvalho Chehab6b1ce3c2007-03-21 16:35:28 -0300912 PWC_INFO("Logitech ViewPort AV 100 webcam detected.\n");
913 name = "Logitech ViewPort AV 100";
914 type_id = 740; /* CCD sensor */
915 break;
916 case 0x08b8: /* Where this released? */
Luc Saillard2b455db2006-04-24 10:29:46 -0300917 PWC_INFO("Logitech QuickCam detected (reserved ID).\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 name = "Logitech QuickCam (res.)";
919 type_id = 730; /* Assuming CMOS */
920 break;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300921 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return -ENODEV;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300923 break;
924 }
925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 else if (vendor_id == 0x055d) {
927 /* I don't know the difference between the C10 and the C30;
928 I suppose the difference is the sensor, but both cameras
929 work equally well with a type_id of 675
930 */
931 switch(product_id) {
932 case 0x9000:
Luc Saillard2b455db2006-04-24 10:29:46 -0300933 PWC_INFO("Samsung MPC-C10 USB webcam detected.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 name = "Samsung MPC-C10";
935 type_id = 675;
936 break;
937 case 0x9001:
Luc Saillard2b455db2006-04-24 10:29:46 -0300938 PWC_INFO("Samsung MPC-C30 USB webcam detected.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 name = "Samsung MPC-C30";
940 type_id = 675;
941 break;
Luc Saillard2b455db2006-04-24 10:29:46 -0300942 case 0x9002:
943 PWC_INFO("Samsung SNC-35E (v3.0) USB webcam detected.\n");
944 name = "Samsung MPC-C30";
945 type_id = 740;
946 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 default:
948 return -ENODEV;
949 break;
950 }
951 }
952 else if (vendor_id == 0x041e) {
953 switch(product_id) {
954 case 0x400c:
Luc Saillard2b455db2006-04-24 10:29:46 -0300955 PWC_INFO("Creative Labs Webcam 5 detected.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 name = "Creative Labs Webcam 5";
957 type_id = 730;
Hans de Goede51886df2011-07-03 15:52:54 -0300958 if (my_power_save == -1)
959 my_power_save = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 break;
961 case 0x4011:
Luc Saillard2b455db2006-04-24 10:29:46 -0300962 PWC_INFO("Creative Labs Webcam Pro Ex detected.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 name = "Creative Labs Webcam Pro Ex";
964 type_id = 740;
965 break;
966 default:
967 return -ENODEV;
968 break;
969 }
970 }
971 else if (vendor_id == 0x04cc) {
972 switch(product_id) {
973 case 0x8116:
Luc Saillard2b455db2006-04-24 10:29:46 -0300974 PWC_INFO("Sotec Afina Eye USB webcam detected.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 name = "Sotec Afina Eye";
976 type_id = 730;
977 break;
978 default:
979 return -ENODEV;
980 break;
981 }
982 }
983 else if (vendor_id == 0x06be) {
984 switch(product_id) {
985 case 0x8116:
986 /* This is essentially the same cam as the Sotec Afina Eye */
Luc Saillard2b455db2006-04-24 10:29:46 -0300987 PWC_INFO("AME Co. Afina Eye USB webcam detected.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 name = "AME Co. Afina Eye";
989 type_id = 750;
990 break;
991 default:
992 return -ENODEV;
993 break;
994 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
997 else if (vendor_id == 0x0d81) {
998 switch(product_id) {
999 case 0x1900:
Luc Saillard2b455db2006-04-24 10:29:46 -03001000 PWC_INFO("Visionite VCS-UC300 USB webcam detected.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 name = "Visionite VCS-UC300";
1002 type_id = 740; /* CCD sensor */
1003 break;
1004 case 0x1910:
Luc Saillard2b455db2006-04-24 10:29:46 -03001005 PWC_INFO("Visionite VCS-UM100 USB webcam detected.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 name = "Visionite VCS-UM100";
1007 type_id = 730; /* CMOS sensor */
1008 break;
1009 default:
1010 return -ENODEV;
1011 break;
1012 }
1013 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001014 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return -ENODEV; /* Not any of the know types; but the list keeps growing. */
1016
Hans de Goede3b4d0ec2011-06-26 03:51:19 -03001017 if (my_power_save == -1)
1018 my_power_save = 0;
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 memset(serial_number, 0, 30);
1021 usb_string(udev, udev->descriptor.iSerialNumber, serial_number, 29);
Luc Saillard2b455db2006-04-24 10:29:46 -03001022 PWC_DEBUG_PROBE("Device serial number is %s\n", serial_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 if (udev->descriptor.bNumConfigurations > 1)
Luc Saillard2b455db2006-04-24 10:29:46 -03001025 PWC_WARNING("Warning: more than 1 configuration available.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
1027 /* Allocate structure, initialize pointers, mutexes, etc. and link it to the usb_device */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01001028 pdev = kzalloc(sizeof(struct pwc_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (pdev == NULL) {
Luc Saillard2b455db2006-04-24 10:29:46 -03001030 PWC_ERROR("Oops, could not allocate memory for pwc_device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return -ENOMEM;
1032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 pdev->type = type_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 pdev->features = features;
Hans de Goede3b4d0ec2011-06-26 03:51:19 -03001035 pwc_construct(pdev); /* set min/max sizes correct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Hans de Goedeceede9f2012-05-09 04:43:12 -03001037 mutex_init(&pdev->v4l2_lock);
1038 mutex_init(&pdev->vb_queue_lock);
Hans de Goede885fe182011-06-06 15:33:44 -03001039 spin_lock_init(&pdev->queued_bufs_lock);
1040 INIT_LIST_HEAD(&pdev->queued_bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 pdev->udev = udev;
Hans de Goede3b4d0ec2011-06-26 03:51:19 -03001043 pdev->power_save = my_power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Hans de Goede885fe182011-06-06 15:33:44 -03001045 /* Init videobuf2 queue structure */
Hans de Goede885fe182011-06-06 15:33:44 -03001046 pdev->vb_queue.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1047 pdev->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1048 pdev->vb_queue.drv_priv = pdev;
1049 pdev->vb_queue.buf_struct_size = sizeof(struct pwc_frame_buf);
1050 pdev->vb_queue.ops = &pwc_vb_queue_ops;
1051 pdev->vb_queue.mem_ops = &vb2_vmalloc_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03001052 pdev->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
Mauro Carvalho Chehabeda94712012-10-27 14:26:25 -03001053 rc = vb2_queue_init(&pdev->vb_queue);
1054 if (rc < 0) {
1055 PWC_ERROR("Oops, could not initialize vb2 queue.\n");
1056 goto err_free_mem;
1057 }
Hans de Goede885fe182011-06-06 15:33:44 -03001058
Hans de Goede9a7b2d12011-06-06 14:43:39 -03001059 /* Init video_device structure */
Ezequiel Garcia5c2edef2012-10-23 15:57:08 -03001060 pdev->vdev = pwc_template;
Mauro Carvalho Chehabcc1e6312018-09-10 16:20:42 -04001061 strscpy(pdev->vdev.name, name, sizeof(pdev->vdev.name));
Hans Verkuil2e43dec2012-07-02 05:51:58 -03001062 pdev->vdev.queue = &pdev->vb_queue;
1063 pdev->vdev.queue->lock = &pdev->vb_queue_lock;
Hans de Goede9a7b2d12011-06-06 14:43:39 -03001064 video_set_drvdata(&pdev->vdev, pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 pdev->release = le16_to_cpu(udev->descriptor.bcdDevice);
Luc Saillard2b455db2006-04-24 10:29:46 -03001067 PWC_DEBUG_PROBE("Release: %04x\n", pdev->release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Hans de Goede24be6892012-01-10 17:02:04 -03001069 /* Allocate USB command buffers */
1070 pdev->ctrl_buf = kmalloc(sizeof(pdev->cmd_buf), GFP_KERNEL);
1071 if (!pdev->ctrl_buf) {
1072 PWC_ERROR("Oops, could not allocate memory for pwc_device.\n");
1073 rc = -ENOMEM;
1074 goto err_free_mem;
1075 }
1076
Hans de Goede6eba9352011-06-26 06:49:59 -03001077#ifdef CONFIG_USB_PWC_DEBUG
1078 /* Query sensor type */
1079 if (pwc_get_cmos_sensor(pdev, &rc) >= 0) {
1080 PWC_DEBUG_OPEN("This %s camera is equipped with a %s (%d).\n",
1081 pdev->vdev.name,
1082 pwc_sensor_type_to_string(rc), rc);
1083 }
1084#endif
1085
Luc Saillard2b455db2006-04-24 10:29:46 -03001086 /* Set the leds off */
1087 pwc_set_leds(pdev, 0, 0);
Hans de Goede6eba9352011-06-26 06:49:59 -03001088
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001089 /* Setup initial videomode */
Hans de Goeded167a852012-01-10 13:01:41 -03001090 rc = pwc_set_video_mode(pdev, MAX_WIDTH, MAX_HEIGHT,
Hans de Goede938d5b92012-01-10 13:14:46 -03001091 V4L2_PIX_FMT_YUV420, 30, &compression, 1);
Hans de Goede6eba9352011-06-26 06:49:59 -03001092 if (rc)
1093 goto err_free_mem;
1094
Hans de Goede6c9cac82011-06-26 12:52:01 -03001095 /* Register controls (and read default values from camera */
1096 rc = pwc_init_controls(pdev);
1097 if (rc) {
1098 PWC_ERROR("Failed to register v4l2 controls (%d).\n", rc);
1099 goto err_free_mem;
1100 }
1101
Hans de Goede6eba9352011-06-26 06:49:59 -03001102 /* And powerdown the camera until streaming starts */
Luc Saillard2b455db2006-04-24 10:29:46 -03001103 pwc_camera_power(pdev, 0);
1104
Hans de Goede76ae8532011-07-19 07:14:22 -03001105 /* Register the v4l2_device structure */
1106 pdev->v4l2_dev.release = pwc_video_release;
1107 rc = v4l2_device_register(&intf->dev, &pdev->v4l2_dev);
1108 if (rc) {
1109 PWC_ERROR("Failed to register v4l2-device (%d).\n", rc);
1110 goto err_free_controls;
1111 }
1112
1113 pdev->v4l2_dev.ctrl_handler = &pdev->ctrl_handler;
1114 pdev->vdev.v4l2_dev = &pdev->v4l2_dev;
Hans de Goedeceede9f2012-05-09 04:43:12 -03001115 pdev->vdev.lock = &pdev->v4l2_lock;
1116
Hans de Goedea081c342012-01-10 17:47:25 -03001117 rc = video_register_device(&pdev->vdev, VFL_TYPE_GRABBER, -1);
Hans Verkuil479567c2010-09-12 17:05:11 -03001118 if (rc < 0) {
1119 PWC_ERROR("Failed to register as video device (%d).\n", rc);
Hans de Goede76ae8532011-07-19 07:14:22 -03001120 goto err_unregister_v4l2_dev;
Hans Verkuil479567c2010-09-12 17:05:11 -03001121 }
Hans de Goede9a7b2d12011-06-06 14:43:39 -03001122 PWC_INFO("Registered as %s.\n", video_device_node_name(&pdev->vdev));
Hans Verkuil479567c2010-09-12 17:05:11 -03001123
Nam Phạm Thànhe32a7ecc2009-01-12 02:50:17 -03001124#ifdef CONFIG_USB_PWC_INPUT_EVDEV
1125 /* register webcam snapshot button input device */
1126 pdev->button_dev = input_allocate_device();
1127 if (!pdev->button_dev) {
Dmitry Torokhov89dec012009-08-14 02:22:52 -03001128 rc = -ENOMEM;
Dmitry Torokhov89dec012009-08-14 02:22:52 -03001129 goto err_video_unreg;
Nam Phạm Thànhe32a7ecc2009-01-12 02:50:17 -03001130 }
1131
Dmitry Torokhov89dec012009-08-14 02:22:52 -03001132 usb_make_path(udev, pdev->button_phys, sizeof(pdev->button_phys));
1133 strlcat(pdev->button_phys, "/input0", sizeof(pdev->button_phys));
1134
Nam Phạm Thànhe32a7ecc2009-01-12 02:50:17 -03001135 pdev->button_dev->name = "PWC snapshot button";
Dmitry Torokhov89dec012009-08-14 02:22:52 -03001136 pdev->button_dev->phys = pdev->button_phys;
Nam Phạm Thànhe32a7ecc2009-01-12 02:50:17 -03001137 usb_to_input_id(pdev->udev, &pdev->button_dev->id);
1138 pdev->button_dev->dev.parent = &pdev->udev->dev;
1139 pdev->button_dev->evbit[0] = BIT_MASK(EV_KEY);
Lennart Poetteringbcd3e4b2009-06-11 11:19:33 -03001140 pdev->button_dev->keybit[BIT_WORD(KEY_CAMERA)] = BIT_MASK(KEY_CAMERA);
Nam Phạm Thànhe32a7ecc2009-01-12 02:50:17 -03001141
1142 rc = input_register_device(pdev->button_dev);
1143 if (rc) {
1144 input_free_device(pdev->button_dev);
Nam Phạm Thànhe32a7ecc2009-01-12 02:50:17 -03001145 pdev->button_dev = NULL;
Dmitry Torokhov89dec012009-08-14 02:22:52 -03001146 goto err_video_unreg;
Nam Phạm Thànhe32a7ecc2009-01-12 02:50:17 -03001147 }
1148#endif
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return 0;
Jeff Garzikc12e3be2006-10-13 07:17:32 -03001151
Arnd Bergmann1f6bcd012016-01-26 14:17:24 -02001152#ifdef CONFIG_USB_PWC_INPUT_EVDEV
Dmitry Torokhov89dec012009-08-14 02:22:52 -03001153err_video_unreg:
Hans de Goede9a7b2d12011-06-06 14:43:39 -03001154 video_unregister_device(&pdev->vdev);
Arnd Bergmann1f6bcd012016-01-26 14:17:24 -02001155#endif
Hans de Goede76ae8532011-07-19 07:14:22 -03001156err_unregister_v4l2_dev:
1157 v4l2_device_unregister(&pdev->v4l2_dev);
Hans de Goede6c9cac82011-06-26 12:52:01 -03001158err_free_controls:
1159 v4l2_ctrl_handler_free(&pdev->ctrl_handler);
Dmitry Torokhov89dec012009-08-14 02:22:52 -03001160err_free_mem:
Hans de Goede24be6892012-01-10 17:02:04 -03001161 kfree(pdev->ctrl_buf);
Dmitry Torokhov89dec012009-08-14 02:22:52 -03001162 kfree(pdev);
Jeff Garzikc12e3be2006-10-13 07:17:32 -03001163 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164}
1165
Dmitry Torokhov89dec012009-08-14 02:22:52 -03001166/* The user yanked out the cable... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167static void usb_pwc_disconnect(struct usb_interface *intf)
1168{
Hans de Goede76ae8532011-07-19 07:14:22 -03001169 struct v4l2_device *v = usb_get_intfdata(intf);
1170 struct pwc_device *pdev = container_of(v, struct pwc_device, v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Hans de Goedeceede9f2012-05-09 04:43:12 -03001172 mutex_lock(&pdev->vb_queue_lock);
Hans Verkuil2e43dec2012-07-02 05:51:58 -03001173 mutex_lock(&pdev->v4l2_lock);
Hans de Goede9a7b2d12011-06-06 14:43:39 -03001174 /* No need to keep the urbs around after disconnection */
Hans de Goedeceede9f2012-05-09 04:43:12 -03001175 if (pdev->vb_queue.streaming)
1176 pwc_isoc_cleanup(pdev);
Hans de Goedeb824bb42011-06-25 17:39:19 -03001177 pdev->udev = NULL;
Hans de Goedec20d78c2011-10-09 09:16:46 -03001178
Hans de Goedeceede9f2012-05-09 04:43:12 -03001179 v4l2_device_disconnect(&pdev->v4l2_dev);
Hans de Goede9a7b2d12011-06-06 14:43:39 -03001180 video_unregister_device(&pdev->vdev);
Hans de Goedeceede9f2012-05-09 04:43:12 -03001181 mutex_unlock(&pdev->v4l2_lock);
Ezequiel Garcia0ab61192012-07-15 03:00:33 -03001182 mutex_unlock(&pdev->vb_queue_lock);
Hans de Goede9a7b2d12011-06-06 14:43:39 -03001183
1184#ifdef CONFIG_USB_PWC_INPUT_EVDEV
1185 if (pdev->button_dev)
1186 input_unregister_device(pdev->button_dev);
1187#endif
Hans de Goede76ae8532011-07-19 07:14:22 -03001188
1189 v4l2_device_put(&pdev->v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190}
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001193/*
1194 * Initialization code & module stuff
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 */
1196
Al Viro64a6f952007-10-14 19:35:30 +01001197static unsigned int leds_nargs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Trent Piepho05ad3902007-01-30 23:26:01 -03001199#ifdef CONFIG_USB_PWC_DEBUG
Luc Saillard2b455db2006-04-24 10:29:46 -03001200module_param_named(trace, pwc_trace, int, 0644);
1201#endif
Hans de Goede3b4d0ec2011-06-26 03:51:19 -03001202module_param(power_save, int, 0644);
Luc Saillard2b455db2006-04-24 10:29:46 -03001203module_param_array(leds, int, &leds_nargs, 0444);
Luc Saillard2b455db2006-04-24 10:29:46 -03001204
Andrea Odetti4315c412009-12-10 16:26:10 -03001205#ifdef CONFIG_USB_PWC_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206MODULE_PARM_DESC(trace, "For debugging purposes");
Andrea Odetti4315c412009-12-10 16:26:10 -03001207#endif
Hans de Goede3b4d0ec2011-06-26 03:51:19 -03001208MODULE_PARM_DESC(power_save, "Turn power saving for new cameras on or off");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209MODULE_PARM_DESC(leds, "LED on,off time in milliseconds");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211MODULE_DESCRIPTION("Philips & OEM USB webcam driver");
1212MODULE_AUTHOR("Luc Saillard <luc@saillard.org>");
1213MODULE_LICENSE("GPL");
Luc Saillard2b455db2006-04-24 10:29:46 -03001214MODULE_ALIAS("pwcx");
1215MODULE_VERSION( PWC_VERSION );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Hans de Goedeceede9f2012-05-09 04:43:12 -03001217module_usb_driver(pwc_driver);