pwc-if.c 36.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/* Linux driver for Philips webcam
   USB and Video4Linux interface part.
   (C) 1999-2004 Nemosoft Unv.
4
   (C) 2004-2006 Luc Saillard (luc@saillard.org)
5
   (C) 2011 Hans de Goede <hdegoede@redhat.com>
L
Linus Torvalds 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

   NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
   driver and thus may have bugs that are not present in the original version.
   Please send bug reports and support requests to <luc@saillard.org>.
   The decompression routines have been implemented by reverse-engineering the
   Nemosoft binary pwcx module. Caveat emptor.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/

29
/*
L
Linus Torvalds 已提交
30 31 32
   This code forms the interface between the USB layers and the Philips
   specific stuff. Some adanved stuff of the driver falls under an
   NDA, signed between me and Philips B.V., Eindhoven, the Netherlands, and
33
   is thus not distributed in source form. The binary pwcx.o module
L
Linus Torvalds 已提交
34
   contains the code that falls under the NDA.
35 36

   In case you're wondering: 'pwc' stands for "Philips WebCam", but
L
Linus Torvalds 已提交
37 38 39
   I really didn't want to type 'philips_web_cam' every time (I'm lazy as
   any Linux kernel hacker, but I don't like uncomprehensible abbreviations
   without explanation).
40

L
Linus Torvalds 已提交
41 42 43
   Oh yes, convention: to disctinguish between all the various pointers to
   device-structures, I use these names for the pointer variables:
   udev: struct usb_device *
44
   vdev: struct video_device (member of pwc_dev)
L
Linus Torvalds 已提交
45 46 47 48 49 50 51 52 53 54 55 56
   pdev: struct pwc_devive *
*/

/* Contributors:
   - Alvarado: adding whitebalance code
   - Alistar Moire: QuickCam 3000 Pro device/product ID
   - Tony Hoyle: Creative Labs Webcam 5 device/product ID
   - Mark Burazin: solving hang in VIDIOCSYNC when camera gets unplugged
   - Jk Fang: Sotec Afina Eye ID
   - Xavier Roche: QuickCam Pro 4000 ID
   - Jens Knudsen: QuickCam Zoom ID
   - J. Debert: QuickCam for Notebooks ID
57
   - Pham Thanh Nam: webcam snapshot button as an event input device
L
Linus Torvalds 已提交
58 59 60 61 62 63 64 65
*/

#include <linux/errno.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/poll.h>
#include <linux/slab.h>
66 67 68
#ifdef CONFIG_USB_PWC_INPUT_EVDEV
#include <linux/usb/input.h>
#endif
L
Linus Torvalds 已提交
69 70
#include <linux/vmalloc.h>
#include <asm/io.h>
71
#include <linux/kernel.h>		/* simple_strtol() */
L
Linus Torvalds 已提交
72 73 74 75

#include "pwc.h"
#include "pwc-kiara.h"
#include "pwc-timon.h"
76 77
#include "pwc-dec23.h"
#include "pwc-dec1.h"
L
Linus Torvalds 已提交
78 79 80 81

/* Function prototypes and driver templates */

/* hotplug device table support */
82
static const struct usb_device_id pwc_device_table [] = {
L
Linus Torvalds 已提交
83 84 85 86 87 88 89
	{ USB_DEVICE(0x0471, 0x0302) }, /* Philips models */
	{ USB_DEVICE(0x0471, 0x0303) },
	{ USB_DEVICE(0x0471, 0x0304) },
	{ USB_DEVICE(0x0471, 0x0307) },
	{ USB_DEVICE(0x0471, 0x0308) },
	{ USB_DEVICE(0x0471, 0x030C) },
	{ USB_DEVICE(0x0471, 0x0310) },
90
	{ USB_DEVICE(0x0471, 0x0311) }, /* Philips ToUcam PRO II */
L
Linus Torvalds 已提交
91 92
	{ USB_DEVICE(0x0471, 0x0312) },
	{ USB_DEVICE(0x0471, 0x0313) }, /* the 'new' 720K */
93
	{ USB_DEVICE(0x0471, 0x0329) }, /* Philips SPC 900NC PC Camera */
L
Linus Torvalds 已提交
94 95 96 97 98 99 100
	{ USB_DEVICE(0x069A, 0x0001) }, /* Askey */
	{ USB_DEVICE(0x046D, 0x08B0) }, /* Logitech QuickCam Pro 3000 */
	{ USB_DEVICE(0x046D, 0x08B1) }, /* Logitech QuickCam Notebook Pro */
	{ USB_DEVICE(0x046D, 0x08B2) }, /* Logitech QuickCam Pro 4000 */
	{ USB_DEVICE(0x046D, 0x08B3) }, /* Logitech QuickCam Zoom (old model) */
	{ USB_DEVICE(0x046D, 0x08B4) }, /* Logitech QuickCam Zoom (new model) */
	{ USB_DEVICE(0x046D, 0x08B5) }, /* Logitech QuickCam Orbit/Sphere */
101 102
	{ USB_DEVICE(0x046D, 0x08B6) }, /* Cisco VT Camera */
	{ USB_DEVICE(0x046D, 0x08B7) }, /* Logitech ViewPort AV 100 */
L
Linus Torvalds 已提交
103
	{ USB_DEVICE(0x046D, 0x08B8) }, /* Logitech (reserved) */
104 105 106
	{ USB_DEVICE(0x055D, 0x9000) }, /* Samsung MPC-C10 */
	{ USB_DEVICE(0x055D, 0x9001) }, /* Samsung MPC-C30 */
	{ USB_DEVICE(0x055D, 0x9002) },	/* Samsung SNC-35E (Ver3.0) */
L
Linus Torvalds 已提交
107 108 109 110 111 112 113 114 115 116 117 118
	{ USB_DEVICE(0x041E, 0x400C) }, /* Creative Webcam 5 */
	{ USB_DEVICE(0x041E, 0x4011) }, /* Creative Webcam Pro Ex */
	{ USB_DEVICE(0x04CC, 0x8116) }, /* Afina Eye */
	{ USB_DEVICE(0x06BE, 0x8116) }, /* new Afina Eye */
	{ USB_DEVICE(0x0d81, 0x1910) }, /* Visionite */
	{ USB_DEVICE(0x0d81, 0x1900) },
	{ }
};
MODULE_DEVICE_TABLE(usb, pwc_device_table);

static int usb_pwc_probe(struct usb_interface *intf, const struct usb_device_id *id);
static void usb_pwc_disconnect(struct usb_interface *intf);
119
static void pwc_isoc_cleanup(struct pwc_device *pdev);
L
Linus Torvalds 已提交
120 121 122 123 124 125 126 127 128 129 130

static struct usb_driver pwc_driver = {
	.name =			"Philips webcam",	/* name */
	.id_table =		pwc_device_table,
	.probe =		usb_pwc_probe,		/* probe() */
	.disconnect =		usb_pwc_disconnect,	/* disconnect() */
};

#define MAX_DEV_HINTS	20
#define MAX_ISOC_ERRORS	20

131
#ifdef CONFIG_USB_PWC_DEBUG
132
	int pwc_trace = PWC_DEBUG_LEVEL;
133
#endif
134
static int power_save = -1;
135
static int leds[2] = { 100, 0 };
L
Linus Torvalds 已提交
136 137 138

/***/

139
static int pwc_video_close(struct file *file);
140
static ssize_t pwc_video_read(struct file *file, char __user *buf,
L
Linus Torvalds 已提交
141 142 143 144
			  size_t count, loff_t *ppos);
static unsigned int pwc_video_poll(struct file *file, poll_table *wait);
static int  pwc_video_mmap(struct file *file, struct vm_area_struct *vma);

145
static const struct v4l2_file_operations pwc_fops = {
L
Linus Torvalds 已提交
146
	.owner =	THIS_MODULE,
147
	.open =		v4l2_fh_open,
L
Linus Torvalds 已提交
148 149 150 151
	.release =     	pwc_video_close,
	.read =		pwc_video_read,
	.poll =		pwc_video_poll,
	.mmap =		pwc_video_mmap,
152
	.unlocked_ioctl = video_ioctl2,
L
Linus Torvalds 已提交
153 154 155
};
static struct video_device pwc_template = {
	.name =		"Philips Webcam",	/* Filled in later */
156
	.release =	video_device_release_empty,
L
Linus Torvalds 已提交
157
	.fops =         &pwc_fops,
158
	.ioctl_ops =	&pwc_ioctl_ops,
L
Linus Torvalds 已提交
159 160 161 162 163
};

/***************************************************************************/
/* Private functions */

164
struct pwc_frame_buf *pwc_get_next_fill_buf(struct pwc_device *pdev)
L
Linus Torvalds 已提交
165
{
166 167 168 169 170 171 172 173 174 175 176 177
	unsigned long flags = 0;
	struct pwc_frame_buf *buf = NULL;

	spin_lock_irqsave(&pdev->queued_bufs_lock, flags);
	if (list_empty(&pdev->queued_bufs))
		goto leave;

	buf = list_entry(pdev->queued_bufs.next, struct pwc_frame_buf, list);
	list_del(&buf->list);
leave:
	spin_unlock_irqrestore(&pdev->queued_bufs_lock, flags);
	return buf;
178 179
}

180 181 182 183 184 185 186 187 188 189
static void pwc_snapshot_button(struct pwc_device *pdev, int down)
{
	if (down) {
		PWC_TRACE("Snapshot button pressed.\n");
	} else {
		PWC_TRACE("Snapshot button released.\n");
	}

#ifdef CONFIG_USB_PWC_INPUT_EVDEV
	if (pdev->button_dev) {
190
		input_report_key(pdev->button_dev, KEY_CAMERA, down);
191 192 193 194 195
		input_sync(pdev->button_dev);
	}
#endif
}

196
static void pwc_frame_complete(struct pwc_device *pdev)
197
{
198
	struct pwc_frame_buf *fbuf = pdev->fill_buf;
199 200 201 202 203 204 205 206 207 208 209 210 211

	/* The ToUCam Fun CMOS sensor causes the firmware to send 2 or 3 bogus
	   frames on the USB wire after an exposure change. This conditition is
	   however detected  in the cam and a bit is set in the header.
	   */
	if (pdev->type == 730) {
		unsigned char *ptr = (unsigned char *)fbuf->data;

		if (ptr[1] == 1 && ptr[0] & 0x10) {
			PWC_TRACE("Hyundai CMOS sensor bug. Dropping frame.\n");
			pdev->drop_frames += 2;
		}
		if ((ptr[0] ^ pdev->vmirror) & 0x01) {
212
			pwc_snapshot_button(pdev, ptr[0] & 0x01);
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
		}
		if ((ptr[0] ^ pdev->vmirror) & 0x02) {
			if (ptr[0] & 0x02)
				PWC_TRACE("Image is mirrored.\n");
			else
				PWC_TRACE("Image is normal.\n");
		}
		pdev->vmirror = ptr[0] & 0x03;
		/* Sometimes the trailer of the 730 is still sent as a 4 byte packet
		   after a short frame; this condition is filtered out specifically. A 4 byte
		   frame doesn't make sense anyway.
		   So we get either this sequence:
		   drop_bit set -> 4 byte frame -> short frame -> good frame
		   Or this one:
		   drop_bit set -> short frame -> good frame
		   So we drop either 3 or 2 frames in all!
		   */
		if (fbuf->filled == 4)
			pdev->drop_frames++;
232
	} else if (pdev->type == 740 || pdev->type == 720) {
233 234
		unsigned char *ptr = (unsigned char *)fbuf->data;
		if ((ptr[0] ^ pdev->vmirror) & 0x01) {
235
			pwc_snapshot_button(pdev, ptr[0] & 0x01);
236 237 238 239
		}
		pdev->vmirror = ptr[0] & 0x03;
	}

240 241
	/* In case we were instructed to drop the frame, do so silently. */
	if (pdev->drop_frames > 0) {
242
		pdev->drop_frames--;
243
	} else {
244 245 246 247
		/* Check for underflow first */
		if (fbuf->filled < pdev->frame_total_size) {
			PWC_DEBUG_FLOW("Frame buffer underflow (%d bytes);"
				       " discarded.\n", fbuf->filled);
248 249 250 251 252 253
		} else {
			fbuf->vb.v4l2_buf.field = V4L2_FIELD_NONE;
			fbuf->vb.v4l2_buf.sequence = pdev->vframe_count;
			vb2_buffer_done(&fbuf->vb, VB2_BUF_STATE_DONE);
			pdev->fill_buf = NULL;
			pdev->vsync = 0;
254 255 256 257
		}
	} /* !drop_frames */
	pdev->vframe_count++;
}
L
Linus Torvalds 已提交
258 259 260 261

/* This gets called for the Isochronous pipe (video). This is done in
 * interrupt time, so it has to be fast, not crash, and not stall. Neat.
 */
262
static void pwc_isoc_handler(struct urb *urb)
L
Linus Torvalds 已提交
263
{
264
	struct pwc_device *pdev = (struct pwc_device *)urb->context;
L
Linus Torvalds 已提交
265
	int i, fst, flen;
266
	unsigned char *iso_buf = NULL;
L
Linus Torvalds 已提交
267

268 269
	if (urb->status == -ENOENT || urb->status == -ECONNRESET ||
	    urb->status == -ESHUTDOWN) {
270
		PWC_DEBUG_OPEN("URB (%p) unlinked %ssynchronuously.\n", urb, urb->status == -ENOENT ? "" : "a");
L
Linus Torvalds 已提交
271 272
		return;
	}
273 274 275 276 277

	if (pdev->fill_buf == NULL)
		pdev->fill_buf = pwc_get_next_fill_buf(pdev);

	if (urb->status != 0) {
L
Linus Torvalds 已提交
278 279 280 281 282 283 284 285 286
		const char *errmsg;

		errmsg = "Unknown";
		switch(urb->status) {
			case -ENOSR:		errmsg = "Buffer error (overrun)"; break;
			case -EPIPE:		errmsg = "Stalled (device not responding)"; break;
			case -EOVERFLOW:	errmsg = "Babble (bad cable?)"; break;
			case -EPROTO:		errmsg = "Bit-stuff error (bad cable?)"; break;
			case -EILSEQ:		errmsg = "CRC/Timeout (could be anything)"; break;
287
			case -ETIME:		errmsg = "Device does not respond"; break;
L
Linus Torvalds 已提交
288
		}
289 290 291
		PWC_ERROR("pwc_isoc_handler() called with status %d [%s].\n",
			  urb->status, errmsg);
		/* Give up after a number of contiguous errors */
L
Linus Torvalds 已提交
292 293
		if (++pdev->visoc_errors > MAX_ISOC_ERRORS)
		{
294 295 296 297 298 299
			PWC_ERROR("Too many ISOC errors, bailing out.\n");
			if (pdev->fill_buf) {
				vb2_buffer_done(&pdev->fill_buf->vb,
						VB2_BUF_STATE_ERROR);
				pdev->fill_buf = NULL;
			}
L
Linus Torvalds 已提交
300
		}
301
		pdev->vsync = 0; /* Drop the current frame */
L
Linus Torvalds 已提交
302 303 304 305 306 307 308
		goto handler_end;
	}

	/* Reset ISOC error counter. We did get here, after all. */
	pdev->visoc_errors = 0;

	/* vsync: 0 = don't copy data
309 310
		  1 = sync-hunt
		  2 = synched
L
Linus Torvalds 已提交
311 312 313 314 315 316
	 */
	/* Compact data */
	for (i = 0; i < urb->number_of_packets; i++) {
		fst  = urb->iso_frame_desc[i].status;
		flen = urb->iso_frame_desc[i].actual_length;
		iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
		if (fst != 0) {
			PWC_ERROR("Iso frame %d has error %d\n", i, fst);
			continue;
		}
		if (flen > 0 && pdev->vsync) {
			struct pwc_frame_buf *fbuf = pdev->fill_buf;

			if (pdev->vsync == 1) {
				do_gettimeofday(&fbuf->vb.v4l2_buf.timestamp);
				pdev->vsync = 2;
			}

			if (flen + fbuf->filled > pdev->frame_total_size) {
				PWC_ERROR("Frame overflow (%d > %d)\n",
					  flen + fbuf->filled,
					  pdev->frame_total_size);
				pdev->vsync = 0; /* Let's wait for an EOF */
			} else {
				memcpy(fbuf->data + fbuf->filled, iso_buf,
				       flen);
L
Linus Torvalds 已提交
337
				fbuf->filled += flen;
338 339 340 341 342 343 344 345 346 347
			}
		}
		if (flen < pdev->vlast_packet_size) {
			/* Shorter packet... end of frame */
			if (pdev->vsync == 2)
				pwc_frame_complete(pdev);
			if (pdev->fill_buf == NULL)
				pdev->fill_buf = pwc_get_next_fill_buf(pdev);
			if (pdev->fill_buf) {
				pdev->fill_buf->filled = 0;
L
Linus Torvalds 已提交
348
				pdev->vsync = 1;
349
			}
L
Linus Torvalds 已提交
350
		}
351
		pdev->vlast_packet_size = flen;
L
Linus Torvalds 已提交
352 353 354 355 356
	}

handler_end:
	i = usb_submit_urb(urb, GFP_ATOMIC);
	if (i != 0)
357
		PWC_ERROR("Error (%d) re-submitting urb in pwc_isoc_handler.\n", i);
L
Linus Torvalds 已提交
358 359
}

H
Hans de Goede 已提交
360
/* Both v4l2_lock and vb_queue_lock should be locked when calling this */
361
static int pwc_isoc_init(struct pwc_device *pdev)
L
Linus Torvalds 已提交
362 363 364 365 366 367
{
	struct usb_device *udev;
	struct urb *urb;
	int i, j, ret;
	struct usb_interface *intf;
	struct usb_host_interface *idesc = NULL;
368
	int compression = 0; /* 0..3 = uncompressed..high */
L
Linus Torvalds 已提交
369 370

	pdev->vsync = 0;
371
	pdev->vlast_packet_size = 0;
372 373
	pdev->fill_buf = NULL;
	pdev->vframe_count = 0;
374
	pdev->visoc_errors = 0;
L
Linus Torvalds 已提交
375 376
	udev = pdev->udev;

377 378 379
retry:
	/* We first try with low compression and then retry with a higher
	   compression setting if there is not enough bandwidth. */
380 381
	ret = pwc_set_video_mode(pdev, pdev->width, pdev->height, pdev->pixfmt,
				 pdev->vframes, &compression, 1);
382

L
Linus Torvalds 已提交
383 384 385 386 387
	/* Get the current alternate interface, adjust packet size */
	intf = usb_ifnum_to_if(udev, 0);
	if (intf)
		idesc = usb_altnum_to_altsetting(intf, pdev->valternate);
	if (!idesc)
388
		return -EIO;
L
Linus Torvalds 已提交
389 390 391

	/* Search video endpoint */
	pdev->vmax_packet_size = -1;
392
	for (i = 0; i < idesc->desc.bNumEndpoints; i++) {
L
Linus Torvalds 已提交
393 394 395 396
		if ((idesc->endpoint[i].desc.bEndpointAddress & 0xF) == pdev->vendpoint) {
			pdev->vmax_packet_size = le16_to_cpu(idesc->endpoint[i].desc.wMaxPacketSize);
			break;
		}
397
	}
398

L
Linus Torvalds 已提交
399
	if (pdev->vmax_packet_size < 0 || pdev->vmax_packet_size > ISO_MAX_FRAME_SIZE) {
400
		PWC_ERROR("Failed to find packet size for video endpoint in current alternate setting.\n");
401
		return -ENFILE; /* Odd error, that should be noticeable */
L
Linus Torvalds 已提交
402 403 404
	}

	/* Set alternate interface */
405
	PWC_DEBUG_OPEN("Setting alternate interface %d\n", pdev->valternate);
L
Linus Torvalds 已提交
406
	ret = usb_set_interface(pdev->udev, 0, pdev->valternate);
407 408 409 410
	if (ret == -ENOSPC && compression < 3) {
		compression++;
		goto retry;
	}
L
Linus Torvalds 已提交
411 412 413
	if (ret < 0)
		return ret;

414
	/* Allocate and init Isochronuous urbs */
L
Linus Torvalds 已提交
415 416 417
	for (i = 0; i < MAX_ISO_BUFS; i++) {
		urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
		if (urb == NULL) {
418
			PWC_ERROR("Failed to allocate urb %d\n", i);
419 420
			pwc_isoc_cleanup(pdev);
			return -ENOMEM;
L
Linus Torvalds 已提交
421
		}
422
		pdev->urbs[i] = urb;
423
		PWC_DEBUG_MEMORY("Allocated URB at 0x%p\n", urb);
L
Linus Torvalds 已提交
424 425 426

		urb->interval = 1; // devik
		urb->dev = udev;
427
		urb->pipe = usb_rcvisocpipe(udev, pdev->vendpoint);
428 429 430 431 432 433 434 435 436 437
		urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
		urb->transfer_buffer = usb_alloc_coherent(udev,
							  ISO_BUFFER_SIZE,
							  GFP_KERNEL,
							  &urb->transfer_dma);
		if (urb->transfer_buffer == NULL) {
			PWC_ERROR("Failed to allocate urb buffer %d\n", i);
			pwc_isoc_cleanup(pdev);
			return -ENOMEM;
		}
438 439 440
		urb->transfer_buffer_length = ISO_BUFFER_SIZE;
		urb->complete = pwc_isoc_handler;
		urb->context = pdev;
L
Linus Torvalds 已提交
441 442 443 444 445 446 447 448 449 450
		urb->start_frame = 0;
		urb->number_of_packets = ISO_FRAMES_PER_DESC;
		for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
			urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
			urb->iso_frame_desc[j].length = pdev->vmax_packet_size;
		}
	}

	/* link */
	for (i = 0; i < MAX_ISO_BUFS; i++) {
451
		ret = usb_submit_urb(pdev->urbs[i], GFP_KERNEL);
452 453 454 455 456
		if (ret == -ENOSPC && compression < 3) {
			compression++;
			pwc_isoc_cleanup(pdev);
			goto retry;
		}
457
		if (ret) {
458
			PWC_ERROR("isoc_init() submit_urb %d failed with error %d\n", i, ret);
459 460 461
			pwc_isoc_cleanup(pdev);
			return ret;
		}
462
		PWC_DEBUG_MEMORY("URB 0x%p submitted.\n", pdev->urbs[i]);
L
Linus Torvalds 已提交
463 464 465
	}

	/* All is done... */
466
	PWC_DEBUG_OPEN("<< pwc_isoc_init()\n");
L
Linus Torvalds 已提交
467 468 469
	return 0;
}

470
static void pwc_iso_stop(struct pwc_device *pdev)
L
Linus Torvalds 已提交
471 472 473 474 475
{
	int i;

	/* Unlinking ISOC buffers one by one */
	for (i = 0; i < MAX_ISO_BUFS; i++) {
476 477 478
		if (pdev->urbs[i]) {
			PWC_DEBUG_MEMORY("Unlinking URB %p\n", pdev->urbs[i]);
			usb_kill_urb(pdev->urbs[i]);
479 480 481 482 483 484 485 486 487 488
		}
	}
}

static void pwc_iso_free(struct pwc_device *pdev)
{
	int i;

	/* Freeing ISOC buffers one by one */
	for (i = 0; i < MAX_ISO_BUFS; i++) {
489
		if (pdev->urbs[i]) {
490
			PWC_DEBUG_MEMORY("Freeing URB\n");
491 492 493 494 495 496 497 498
			if (pdev->urbs[i]->transfer_buffer) {
				usb_free_coherent(pdev->udev,
					pdev->urbs[i]->transfer_buffer_length,
					pdev->urbs[i]->transfer_buffer,
					pdev->urbs[i]->transfer_dma);
			}
			usb_free_urb(pdev->urbs[i]);
			pdev->urbs[i] = NULL;
L
Linus Torvalds 已提交
499 500
		}
	}
501 502
}

H
Hans de Goede 已提交
503
/* Both v4l2_lock and vb_queue_lock should be locked when calling this */
504
static void pwc_isoc_cleanup(struct pwc_device *pdev)
505 506
{
	PWC_DEBUG_OPEN(">> pwc_isoc_cleanup()\n");
507

508 509
	pwc_iso_stop(pdev);
	pwc_iso_free(pdev);
510
	usb_set_interface(pdev->udev, 0, 0);
L
Linus Torvalds 已提交
511

512
	PWC_DEBUG_OPEN("<< pwc_isoc_cleanup()\n");
L
Linus Torvalds 已提交
513 514
}

H
Hans de Goede 已提交
515
/* Must be called with vb_queue_lock hold */
516 517
static void pwc_cleanup_queued_bufs(struct pwc_device *pdev)
{
H
Hans de Goede 已提交
518 519 520
	unsigned long flags = 0;

	spin_lock_irqsave(&pdev->queued_bufs_lock, flags);
521 522 523 524 525 526 527 528
	while (!list_empty(&pdev->queued_bufs)) {
		struct pwc_frame_buf *buf;

		buf = list_entry(pdev->queued_bufs.next, struct pwc_frame_buf,
				 list);
		list_del(&buf->list);
		vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
	}
H
Hans de Goede 已提交
529
	spin_unlock_irqrestore(&pdev->queued_bufs_lock, flags);
530 531
}

532
#ifdef CONFIG_USB_PWC_DEBUG
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
static const char *pwc_sensor_type_to_string(unsigned int sensor_type)
{
	switch(sensor_type) {
		case 0x00:
			return "Hyundai CMOS sensor";
		case 0x20:
			return "Sony CCD sensor + TDA8787";
		case 0x2E:
			return "Sony CCD sensor + Exas 98L59";
		case 0x2F:
			return "Sony CCD sensor + ADI 9804";
		case 0x30:
			return "Sharp CCD sensor + TDA8787";
		case 0x3E:
			return "Sharp CCD sensor + Exas 98L59";
		case 0x3F:
			return "Sharp CCD sensor + ADI 9804";
		case 0x40:
			return "UPA 1021 sensor";
		case 0x100:
			return "VGA sensor";
		case 0x101:
			return "PAL MR sensor";
		default:
557
			return "unknown type of sensor";
558 559 560
	}
}
#endif
L
Linus Torvalds 已提交
561 562 563 564

/***************************************************************************/
/* Video4Linux functions */

H
Hans de Goede 已提交
565 566 567
int pwc_test_n_set_capt_file(struct pwc_device *pdev, struct file *file)
{
	if (pdev->capt_file != NULL &&
H
Hans de Goede 已提交
568 569 570
	    pdev->capt_file != file)
		return -EBUSY;

H
Hans de Goede 已提交
571
	pdev->capt_file = file;
H
Hans de Goede 已提交
572 573

	return 0;
H
Hans de Goede 已提交
574 575
}

576
static void pwc_video_release(struct v4l2_device *v)
L
Linus Torvalds 已提交
577
{
578
	struct pwc_device *pdev = container_of(v, struct pwc_device, v4l2_dev);
579

580
	v4l2_ctrl_handler_free(&pdev->ctrl_handler);
H
Hans de Goede 已提交
581
	v4l2_device_unregister(&pdev->v4l2_dev);
582
	kfree(pdev->ctrl_buf);
583
	kfree(pdev);
584 585
}

586
static int pwc_video_close(struct file *file)
L
Linus Torvalds 已提交
587
{
588
	struct pwc_device *pdev = video_drvdata(file);
L
Linus Torvalds 已提交
589

H
Hans de Goede 已提交
590 591 592 593 594 595 596 597 598 599 600
	/*
	 * If we're still streaming vb2_queue_release will call stream_stop
	 * so we must take both the v4l2_lock and the vb_queue_lock.
	 */
	if (mutex_lock_interruptible(&pdev->v4l2_lock))
		return -ERESTARTSYS;
	if (mutex_lock_interruptible(&pdev->vb_queue_lock)) {
		mutex_unlock(&pdev->v4l2_lock);
		return -ERESTARTSYS;
	}

601 602 603 604
	if (pdev->capt_file == file) {
		vb2_queue_release(&pdev->vb_queue);
		pdev->capt_file = NULL;
	}
H
Hans de Goede 已提交
605 606 607 608

	mutex_unlock(&pdev->vb_queue_lock);
	mutex_unlock(&pdev->v4l2_lock);

609
	return v4l2_fh_release(file);
L
Linus Torvalds 已提交
610 611
}

612
static ssize_t pwc_video_read(struct file *file, char __user *buf,
613
			      size_t count, loff_t *ppos)
L
Linus Torvalds 已提交
614
{
615
	struct pwc_device *pdev = video_drvdata(file);
H
Hans de Goede 已提交
616 617
	int lock_v4l2 = 0;
	ssize_t ret;
L
Linus Torvalds 已提交
618

H
Hans de Goede 已提交
619 620
	if (mutex_lock_interruptible(&pdev->vb_queue_lock))
		return -ERESTARTSYS;
621

H
Hans de Goede 已提交
622 623 624 625 626 627 628
	ret = pwc_test_n_set_capt_file(pdev, file);
	if (ret)
		goto out;

	/* stream_start will get called so we must take the v4l2_lock */
	if (pdev->vb_queue.fileio == NULL)
		lock_v4l2 = 1;
629

H
Hans de Goede 已提交
630 631 632 633 634 635 636 637 638 639 640 641
	/* Use try_lock, since we're taking the locks in the *wrong* order! */
	if (lock_v4l2 && !mutex_trylock(&pdev->v4l2_lock)) {
		ret = -ERESTARTSYS;
		goto out;
	}
	ret = vb2_read(&pdev->vb_queue, buf, count, ppos,
		       file->f_flags & O_NONBLOCK);
	if (lock_v4l2)
		mutex_unlock(&pdev->v4l2_lock);
out:
	mutex_unlock(&pdev->vb_queue_lock);
	return ret;
642
}
L
Linus Torvalds 已提交
643

644 645
static unsigned int pwc_video_poll(struct file *file, poll_table *wait)
{
646
	struct pwc_device *pdev = video_drvdata(file);
H
Hans de Goede 已提交
647
	struct vb2_queue *q = &pdev->vb_queue;
648
	unsigned long req_events = poll_requested_events(wait);
H
Hans de Goede 已提交
649 650
	unsigned int ret = POLL_ERR;
	int lock_v4l2 = 0;
651

H
Hans de Goede 已提交
652
	if (mutex_lock_interruptible(&pdev->vb_queue_lock))
653
		return POLL_ERR;
L
Linus Torvalds 已提交
654

H
Hans de Goede 已提交
655
	/* Will this start fileio and thus call start_stream? */
656
	if ((req_events & (POLLIN | POLLRDNORM)) &&
H
Hans de Goede 已提交
657
	    q->num_buffers == 0 && !q->streaming && q->fileio == NULL) {
658
		if (pwc_test_n_set_capt_file(pdev, file))
H
Hans de Goede 已提交
659 660
			goto out;
		lock_v4l2 = 1;
661 662
	}

H
Hans de Goede 已提交
663 664 665 666 667 668 669 670 671 672 673 674
	/* Use try_lock, since we're taking the locks in the *wrong* order! */
	if (lock_v4l2 && !mutex_trylock(&pdev->v4l2_lock))
		goto out;
	ret = vb2_poll(&pdev->vb_queue, file, wait);
	if (lock_v4l2)
		mutex_unlock(&pdev->v4l2_lock);

out:
	if (!pdev->udev)
		ret |= POLLHUP;
	mutex_unlock(&pdev->vb_queue_lock);
	return ret;
L
Linus Torvalds 已提交
675 676
}

677
static int pwc_video_mmap(struct file *file, struct vm_area_struct *vma)
L
Linus Torvalds 已提交
678
{
679
	struct pwc_device *pdev = video_drvdata(file);
H
Hans de Goede 已提交
680
	int ret;
L
Linus Torvalds 已提交
681

H
Hans de Goede 已提交
682 683 684 685 686 687
	if (mutex_lock_interruptible(&pdev->vb_queue_lock))
		return -ERESTARTSYS;

	ret = pwc_test_n_set_capt_file(pdev, file);
	if (ret == 0)
		ret = vb2_mmap(&pdev->vb_queue, vma);
688

H
Hans de Goede 已提交
689 690
	mutex_unlock(&pdev->vb_queue_lock);
	return ret;
691
}
L
Linus Torvalds 已提交
692

693 694 695
/***************************************************************************/
/* Videobuf2 operations */

696 697 698
static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
				unsigned int *nbuffers, unsigned int *nplanes,
				unsigned int sizes[], void *alloc_ctxs[])
699 700
{
	struct pwc_device *pdev = vb2_get_drv_priv(vq);
701
	int size;
702 703 704 705 706 707 708 709

	if (*nbuffers < MIN_FRAMES)
		*nbuffers = MIN_FRAMES;
	else if (*nbuffers > MAX_FRAMES)
		*nbuffers = MAX_FRAMES;

	*nplanes = 1;

710 711 712
	size = pwc_get_size(pdev, MAX_WIDTH, MAX_HEIGHT);
	sizes[0] = PAGE_ALIGN(pwc_image_sizes[size][0] *
			      pwc_image_sizes[size][1] * 3 / 2);
713 714 715 716 717 718 719

	return 0;
}

static int buffer_init(struct vb2_buffer *vb)
{
	struct pwc_frame_buf *buf = container_of(vb, struct pwc_frame_buf, vb);
720

721 722 723 724 725 726 727 728 729 730 731 732 733
	/* need vmalloc since frame buffer > 128K */
	buf->data = vzalloc(PWC_FRAME_SIZE);
	if (buf->data == NULL)
		return -ENOMEM;

	return 0;
}

static int buffer_prepare(struct vb2_buffer *vb)
{
	struct pwc_device *pdev = vb2_get_drv_priv(vb->vb2_queue);

	/* Don't allow queing new buffers after device disconnection */
734 735
	if (!pdev->udev)
		return -ENODEV;
L
Linus Torvalds 已提交
736 737 738 739

	return 0;
}

740
static int buffer_finish(struct vb2_buffer *vb)
L
Linus Torvalds 已提交
741
{
742 743
	struct pwc_device *pdev = vb2_get_drv_priv(vb->vb2_queue);
	struct pwc_frame_buf *buf = container_of(vb, struct pwc_frame_buf, vb);
744

745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
	/*
	 * Application has called dqbuf and is getting back a buffer we've
	 * filled, take the pwc data we've stored in buf->data and decompress
	 * it into a usable format, storing the result in the vb2_buffer
	 */
	return pwc_decompress(pdev, buf);
}

static void buffer_cleanup(struct vb2_buffer *vb)
{
	struct pwc_frame_buf *buf = container_of(vb, struct pwc_frame_buf, vb);

	vfree(buf->data);
}

static void buffer_queue(struct vb2_buffer *vb)
{
	struct pwc_device *pdev = vb2_get_drv_priv(vb->vb2_queue);
	struct pwc_frame_buf *buf = container_of(vb, struct pwc_frame_buf, vb);
	unsigned long flags = 0;

H
Hans de Goede 已提交
766
	/* Check the device has not disconnected between prep and queuing */
H
Hans de Goede 已提交
767
	if (!pdev->udev) {
H
Hans de Goede 已提交
768
		vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
H
Hans de Goede 已提交
769 770 771 772 773
		return;
	}

	spin_lock_irqsave(&pdev->queued_bufs_lock, flags);
	list_add_tail(&buf->list, &pdev->queued_bufs);
774 775 776
	spin_unlock_irqrestore(&pdev->queued_bufs_lock, flags);
}

777
static int start_streaming(struct vb2_queue *vq, unsigned int count)
778 779
{
	struct pwc_device *pdev = vb2_get_drv_priv(vq);
H
Hans de Goede 已提交
780
	int r;
781

H
Hans de Goede 已提交
782 783
	if (!pdev->udev)
		return -ENODEV;
784

785 786
	/* Turn on camera and set LEDS on */
	pwc_camera_power(pdev, 1);
787
	pwc_set_leds(pdev, leds[0], leds[1]);
788

H
Hans de Goede 已提交
789
	r = pwc_isoc_init(pdev);
790 791 792 793 794 795 796
	if (r) {
		/* If we failed turn camera and LEDS back off */
		pwc_set_leds(pdev, 0, 0);
		pwc_camera_power(pdev, 0);
		/* And cleanup any queued bufs!! */
		pwc_cleanup_queued_bufs(pdev);
	}
H
Hans de Goede 已提交
797

H
Hans de Goede 已提交
798
	return r;
799 800 801 802 803 804
}

static int stop_streaming(struct vb2_queue *vq)
{
	struct pwc_device *pdev = vb2_get_drv_priv(vq);

805 806 807
	if (pdev->udev) {
		pwc_set_leds(pdev, 0, 0);
		pwc_camera_power(pdev, 0);
808
		pwc_isoc_cleanup(pdev);
809
	}
H
Hans de Goede 已提交
810

811
	pwc_cleanup_queued_bufs(pdev);
812

L
Linus Torvalds 已提交
813 814 815
	return 0;
}

H
Hans de Goede 已提交
816 817 818 819 820 821 822 823 824 825 826 827
static void wait_prepare(struct vb2_queue *vq)
{
	struct pwc_device *pdev = vb2_get_drv_priv(vq);
	mutex_unlock(&pdev->vb_queue_lock);
}

static void wait_finish(struct vb2_queue *vq)
{
	struct pwc_device *pdev = vb2_get_drv_priv(vq);
	mutex_lock(&pdev->vb_queue_lock);
}

828 829 830 831 832 833 834 835 836
static struct vb2_ops pwc_vb_queue_ops = {
	.queue_setup		= queue_setup,
	.buf_init		= buffer_init,
	.buf_prepare		= buffer_prepare,
	.buf_finish		= buffer_finish,
	.buf_cleanup		= buffer_cleanup,
	.buf_queue		= buffer_queue,
	.start_streaming	= start_streaming,
	.stop_streaming		= stop_streaming,
H
Hans de Goede 已提交
837 838
	.wait_prepare		= wait_prepare,
	.wait_finish		= wait_finish,
839 840
};

L
Linus Torvalds 已提交
841 842 843 844 845 846 847 848 849 850 851 852
/***************************************************************************/
/* USB functions */

/* This function gets called when a new device is plugged in or the usb core
 * is loaded.
 */

static int usb_pwc_probe(struct usb_interface *intf, const struct usb_device_id *id)
{
	struct usb_device *udev = interface_to_usbdev(intf);
	struct pwc_device *pdev = NULL;
	int vendor_id, product_id, type_id;
853
	int rc;
L
Linus Torvalds 已提交
854
	int features = 0;
855
	int compression = 0;
856
	int my_power_save = power_save;
L
Linus Torvalds 已提交
857 858
	char serial_number[30], *name;

859 860 861
	vendor_id = le16_to_cpu(udev->descriptor.idVendor);
	product_id = le16_to_cpu(udev->descriptor.idProduct);

L
Linus Torvalds 已提交
862
	/* Check if we can handle this device */
863 864
	PWC_DEBUG_PROBE("probe() called [%04X %04X], if %d\n",
		vendor_id, product_id,
L
Linus Torvalds 已提交
865 866 867 868 869 870 871 872 873 874 875 876
		intf->altsetting->desc.bInterfaceNumber);

	/* the interfaces are probed one by one. We are only interested in the
	   video interface (0) now.
	   Interface 1 is the Audio Control, and interface 2 Audio itself.
	 */
	if (intf->altsetting->desc.bInterfaceNumber > 0)
		return -ENODEV;

	if (vendor_id == 0x0471) {
		switch (product_id) {
		case 0x0302:
877
			PWC_INFO("Philips PCA645VC USB webcam detected.\n");
L
Linus Torvalds 已提交
878 879 880 881
			name = "Philips 645 webcam";
			type_id = 645;
			break;
		case 0x0303:
882
			PWC_INFO("Philips PCA646VC USB webcam detected.\n");
L
Linus Torvalds 已提交
883 884 885 886
			name = "Philips 646 webcam";
			type_id = 646;
			break;
		case 0x0304:
887
			PWC_INFO("Askey VC010 type 2 USB webcam detected.\n");
L
Linus Torvalds 已提交
888 889 890 891
			name = "Askey VC010 webcam";
			type_id = 646;
			break;
		case 0x0307:
892
			PWC_INFO("Philips PCVC675K (Vesta) USB webcam detected.\n");
L
Linus Torvalds 已提交
893 894 895 896
			name = "Philips 675 webcam";
			type_id = 675;
			break;
		case 0x0308:
897
			PWC_INFO("Philips PCVC680K (Vesta Pro) USB webcam detected.\n");
L
Linus Torvalds 已提交
898 899 900 901
			name = "Philips 680 webcam";
			type_id = 680;
			break;
		case 0x030C:
902
			PWC_INFO("Philips PCVC690K (Vesta Pro Scan) USB webcam detected.\n");
L
Linus Torvalds 已提交
903 904 905 906
			name = "Philips 690 webcam";
			type_id = 690;
			break;
		case 0x0310:
907
			PWC_INFO("Philips PCVC730K (ToUCam Fun)/PCVC830 (ToUCam II) USB webcam detected.\n");
L
Linus Torvalds 已提交
908 909 910 911
			name = "Philips 730 webcam";
			type_id = 730;
			break;
		case 0x0311:
912
			PWC_INFO("Philips PCVC740K (ToUCam Pro)/PCVC840 (ToUCam II) USB webcam detected.\n");
L
Linus Torvalds 已提交
913 914 915 916
			name = "Philips 740 webcam";
			type_id = 740;
			break;
		case 0x0312:
917
			PWC_INFO("Philips PCVC750K (ToUCam Pro Scan) USB webcam detected.\n");
L
Linus Torvalds 已提交
918 919 920 921
			name = "Philips 750 webcam";
			type_id = 750;
			break;
		case 0x0313:
922
			PWC_INFO("Philips PCVC720K/40 (ToUCam XS) USB webcam detected.\n");
L
Linus Torvalds 已提交
923 924 925
			name = "Philips 720K/40 webcam";
			type_id = 720;
			break;
926 927 928
		case 0x0329:
			PWC_INFO("Philips SPC 900NC USB webcam detected.\n");
			name = "Philips SPC 900NC webcam";
929
			type_id = 740;
930
			break;
L
Linus Torvalds 已提交
931 932 933 934 935 936 937 938
		default:
			return -ENODEV;
			break;
		}
	}
	else if (vendor_id == 0x069A) {
		switch(product_id) {
		case 0x0001:
939
			PWC_INFO("Askey VC010 type 1 USB webcam detected.\n");
L
Linus Torvalds 已提交
940 941 942 943 944 945 946 947 948 949 950
			name = "Askey VC010 webcam";
			type_id = 645;
			break;
		default:
			return -ENODEV;
			break;
		}
	}
	else if (vendor_id == 0x046d) {
		switch(product_id) {
		case 0x08b0:
951
			PWC_INFO("Logitech QuickCam Pro 3000 USB webcam detected.\n");
L
Linus Torvalds 已提交
952 953 954 955
			name = "Logitech QuickCam Pro 3000";
			type_id = 740; /* CCD sensor */
			break;
		case 0x08b1:
956
			PWC_INFO("Logitech QuickCam Notebook Pro USB webcam detected.\n");
L
Linus Torvalds 已提交
957 958 959 960
			name = "Logitech QuickCam Notebook Pro";
			type_id = 740; /* CCD sensor */
			break;
		case 0x08b2:
961
			PWC_INFO("Logitech QuickCam 4000 Pro USB webcam detected.\n");
L
Linus Torvalds 已提交
962 963
			name = "Logitech QuickCam Pro 4000";
			type_id = 740; /* CCD sensor */
964 965
			if (my_power_save == -1)
				my_power_save = 1;
L
Linus Torvalds 已提交
966 967
			break;
		case 0x08b3:
968
			PWC_INFO("Logitech QuickCam Zoom USB webcam detected.\n");
L
Linus Torvalds 已提交
969 970 971 972
			name = "Logitech QuickCam Zoom";
			type_id = 740; /* CCD sensor */
			break;
		case 0x08B4:
973
			PWC_INFO("Logitech QuickCam Zoom (new model) USB webcam detected.\n");
L
Linus Torvalds 已提交
974 975
			name = "Logitech QuickCam Zoom";
			type_id = 740; /* CCD sensor */
976 977
			if (my_power_save == -1)
				my_power_save = 1;
L
Linus Torvalds 已提交
978 979
			break;
		case 0x08b5:
980
			PWC_INFO("Logitech QuickCam Orbit/Sphere USB webcam detected.\n");
L
Linus Torvalds 已提交
981 982
			name = "Logitech QuickCam Orbit";
			type_id = 740; /* CCD sensor */
983 984
			if (my_power_save == -1)
				my_power_save = 1;
L
Linus Torvalds 已提交
985 986 987
			features |= FEATURE_MOTOR_PANTILT;
			break;
		case 0x08b6:
988 989 990 991
			PWC_INFO("Logitech/Cisco VT Camera webcam detected.\n");
			name = "Cisco VT Camera";
			type_id = 740; /* CCD sensor */
			break;
L
Linus Torvalds 已提交
992
		case 0x08b7:
993 994 995 996 997
			PWC_INFO("Logitech ViewPort AV 100 webcam detected.\n");
			name = "Logitech ViewPort AV 100";
			type_id = 740; /* CCD sensor */
			break;
		case 0x08b8: /* Where this released? */
998
			PWC_INFO("Logitech QuickCam detected (reserved ID).\n");
L
Linus Torvalds 已提交
999 1000 1001
			name = "Logitech QuickCam (res.)";
			type_id = 730; /* Assuming CMOS */
			break;
1002
		default:
L
Linus Torvalds 已提交
1003
			return -ENODEV;
1004 1005 1006
			break;
		}
	}
L
Linus Torvalds 已提交
1007 1008 1009 1010 1011 1012 1013
	else if (vendor_id == 0x055d) {
		/* I don't know the difference between the C10 and the C30;
		   I suppose the difference is the sensor, but both cameras
		   work equally well with a type_id of 675
		 */
		switch(product_id) {
		case 0x9000:
1014
			PWC_INFO("Samsung MPC-C10 USB webcam detected.\n");
L
Linus Torvalds 已提交
1015 1016 1017 1018
			name = "Samsung MPC-C10";
			type_id = 675;
			break;
		case 0x9001:
1019
			PWC_INFO("Samsung MPC-C30 USB webcam detected.\n");
L
Linus Torvalds 已提交
1020 1021 1022
			name = "Samsung MPC-C30";
			type_id = 675;
			break;
1023 1024 1025 1026 1027
		case 0x9002:
			PWC_INFO("Samsung SNC-35E (v3.0) USB webcam detected.\n");
			name = "Samsung MPC-C30";
			type_id = 740;
			break;
L
Linus Torvalds 已提交
1028 1029 1030 1031 1032 1033 1034 1035
		default:
			return -ENODEV;
			break;
		}
	}
	else if (vendor_id == 0x041e) {
		switch(product_id) {
		case 0x400c:
1036
			PWC_INFO("Creative Labs Webcam 5 detected.\n");
L
Linus Torvalds 已提交
1037 1038
			name = "Creative Labs Webcam 5";
			type_id = 730;
1039 1040
			if (my_power_save == -1)
				my_power_save = 1;
L
Linus Torvalds 已提交
1041 1042
			break;
		case 0x4011:
1043
			PWC_INFO("Creative Labs Webcam Pro Ex detected.\n");
L
Linus Torvalds 已提交
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
			name = "Creative Labs Webcam Pro Ex";
			type_id = 740;
			break;
		default:
			return -ENODEV;
			break;
		}
	}
	else if (vendor_id == 0x04cc) {
		switch(product_id) {
		case 0x8116:
1055
			PWC_INFO("Sotec Afina Eye USB webcam detected.\n");
L
Linus Torvalds 已提交
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
			name = "Sotec Afina Eye";
			type_id = 730;
			break;
		default:
			return -ENODEV;
			break;
		}
	}
	else if (vendor_id == 0x06be) {
		switch(product_id) {
		case 0x8116:
			/* This is essentially the same cam as the Sotec Afina Eye */
1068
			PWC_INFO("AME Co. Afina Eye USB webcam detected.\n");
L
Linus Torvalds 已提交
1069 1070 1071 1072 1073 1074 1075
			name = "AME Co. Afina Eye";
			type_id = 750;
			break;
		default:
			return -ENODEV;
			break;
		}
1076

L
Linus Torvalds 已提交
1077 1078 1079 1080
	}
	else if (vendor_id == 0x0d81) {
		switch(product_id) {
		case 0x1900:
1081
			PWC_INFO("Visionite VCS-UC300 USB webcam detected.\n");
L
Linus Torvalds 已提交
1082 1083 1084 1085
			name = "Visionite VCS-UC300";
			type_id = 740; /* CCD sensor */
			break;
		case 0x1910:
1086
			PWC_INFO("Visionite VCS-UM100 USB webcam detected.\n");
L
Linus Torvalds 已提交
1087 1088 1089 1090 1091 1092 1093 1094
			name = "Visionite VCS-UM100";
			type_id = 730; /* CMOS sensor */
			break;
		default:
			return -ENODEV;
			break;
		}
	}
1095
	else
L
Linus Torvalds 已提交
1096 1097
		return -ENODEV; /* Not any of the know types; but the list keeps growing. */

1098 1099 1100
	if (my_power_save == -1)
		my_power_save = 0;

L
Linus Torvalds 已提交
1101 1102
	memset(serial_number, 0, 30);
	usb_string(udev, udev->descriptor.iSerialNumber, serial_number, 29);
1103
	PWC_DEBUG_PROBE("Device serial number is %s\n", serial_number);
L
Linus Torvalds 已提交
1104 1105

	if (udev->descriptor.bNumConfigurations > 1)
1106
		PWC_WARNING("Warning: more than 1 configuration available.\n");
L
Linus Torvalds 已提交
1107 1108

	/* Allocate structure, initialize pointers, mutexes, etc. and link it to the usb_device */
1109
	pdev = kzalloc(sizeof(struct pwc_device), GFP_KERNEL);
L
Linus Torvalds 已提交
1110
	if (pdev == NULL) {
1111
		PWC_ERROR("Oops, could not allocate memory for pwc_device.\n");
L
Linus Torvalds 已提交
1112 1113 1114 1115
		return -ENOMEM;
	}
	pdev->type = type_id;
	pdev->features = features;
1116
	pwc_construct(pdev); /* set min/max sizes correct */
L
Linus Torvalds 已提交
1117

H
Hans de Goede 已提交
1118 1119
	mutex_init(&pdev->v4l2_lock);
	mutex_init(&pdev->vb_queue_lock);
1120 1121
	spin_lock_init(&pdev->queued_bufs_lock);
	INIT_LIST_HEAD(&pdev->queued_bufs);
L
Linus Torvalds 已提交
1122 1123

	pdev->udev = udev;
1124
	pdev->power_save = my_power_save;
L
Linus Torvalds 已提交
1125

1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
	/* Init videobuf2 queue structure */
	memset(&pdev->vb_queue, 0, sizeof(pdev->vb_queue));
	pdev->vb_queue.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	pdev->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
	pdev->vb_queue.drv_priv = pdev;
	pdev->vb_queue.buf_struct_size = sizeof(struct pwc_frame_buf);
	pdev->vb_queue.ops = &pwc_vb_queue_ops;
	pdev->vb_queue.mem_ops = &vb2_vmalloc_memops;
	vb2_queue_init(&pdev->vb_queue);

1136 1137 1138
	/* Init video_device structure */
	memcpy(&pdev->vdev, &pwc_template, sizeof(pwc_template));
	strcpy(pdev->vdev.name, name);
1139
	set_bit(V4L2_FL_USE_FH_PRIO, &pdev->vdev.flags);
1140
	video_set_drvdata(&pdev->vdev, pdev);
L
Linus Torvalds 已提交
1141 1142

	pdev->release = le16_to_cpu(udev->descriptor.bcdDevice);
1143
	PWC_DEBUG_PROBE("Release: %04x\n", pdev->release);
L
Linus Torvalds 已提交
1144

1145 1146 1147 1148 1149 1150 1151 1152
	/* Allocate USB command buffers */
	pdev->ctrl_buf = kmalloc(sizeof(pdev->cmd_buf), GFP_KERNEL);
	if (!pdev->ctrl_buf) {
		PWC_ERROR("Oops, could not allocate memory for pwc_device.\n");
		rc = -ENOMEM;
		goto err_free_mem;
	}

1153 1154 1155 1156 1157 1158 1159 1160 1161
#ifdef CONFIG_USB_PWC_DEBUG
	/* Query sensor type */
	if (pwc_get_cmos_sensor(pdev, &rc) >= 0) {
		PWC_DEBUG_OPEN("This %s camera is equipped with a %s (%d).\n",
				pdev->vdev.name,
				pwc_sensor_type_to_string(rc), rc);
	}
#endif

1162 1163
	/* Set the leds off */
	pwc_set_leds(pdev, 0, 0);
1164 1165

	/* Setup intial videomode */
H
Hans de Goede 已提交
1166
	rc = pwc_set_video_mode(pdev, MAX_WIDTH, MAX_HEIGHT,
1167
				V4L2_PIX_FMT_YUV420, 30, &compression, 1);
1168 1169 1170
	if (rc)
		goto err_free_mem;

1171 1172 1173 1174 1175 1176 1177
	/* Register controls (and read default values from camera */
	rc = pwc_init_controls(pdev);
	if (rc) {
		PWC_ERROR("Failed to register v4l2 controls (%d).\n", rc);
		goto err_free_mem;
	}

1178
	/* And powerdown the camera until streaming starts */
1179 1180
	pwc_camera_power(pdev, 0);

1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
	/* Register the v4l2_device structure */
	pdev->v4l2_dev.release = pwc_video_release;
	rc = v4l2_device_register(&intf->dev, &pdev->v4l2_dev);
	if (rc) {
		PWC_ERROR("Failed to register v4l2-device (%d).\n", rc);
		goto err_free_controls;
	}

	pdev->v4l2_dev.ctrl_handler = &pdev->ctrl_handler;
	pdev->vdev.v4l2_dev = &pdev->v4l2_dev;
H
Hans de Goede 已提交
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
	pdev->vdev.lock = &pdev->v4l2_lock;

	/*
	 * Don't take v4l2_lock for these ioctls. This improves latency if
	 * v4l2_lock is taken for a long time, e.g. when changing a control
	 * value, and a new frame is ready to be dequeued.
	 */
	v4l2_dont_use_lock(&pdev->vdev, VIDIOC_DQBUF);
	v4l2_dont_use_lock(&pdev->vdev, VIDIOC_QBUF);
	v4l2_dont_use_lock(&pdev->vdev, VIDIOC_QUERYBUF);
1201

1202
	rc = video_register_device(&pdev->vdev, VFL_TYPE_GRABBER, -1);
1203 1204
	if (rc < 0) {
		PWC_ERROR("Failed to register as video device (%d).\n", rc);
1205
		goto err_unregister_v4l2_dev;
1206
	}
1207
	PWC_INFO("Registered as %s.\n", video_device_node_name(&pdev->vdev));
1208

1209 1210 1211 1212 1213
#ifdef CONFIG_USB_PWC_INPUT_EVDEV
	/* register webcam snapshot button input device */
	pdev->button_dev = input_allocate_device();
	if (!pdev->button_dev) {
		PWC_ERROR("Err, insufficient memory for webcam snapshot button device.");
1214 1215
		rc = -ENOMEM;
		goto err_video_unreg;
1216 1217
	}

1218 1219 1220
	usb_make_path(udev, pdev->button_phys, sizeof(pdev->button_phys));
	strlcat(pdev->button_phys, "/input0", sizeof(pdev->button_phys));

1221
	pdev->button_dev->name = "PWC snapshot button";
1222
	pdev->button_dev->phys = pdev->button_phys;
1223 1224 1225
	usb_to_input_id(pdev->udev, &pdev->button_dev->id);
	pdev->button_dev->dev.parent = &pdev->udev->dev;
	pdev->button_dev->evbit[0] = BIT_MASK(EV_KEY);
1226
	pdev->button_dev->keybit[BIT_WORD(KEY_CAMERA)] = BIT_MASK(KEY_CAMERA);
1227 1228 1229 1230 1231

	rc = input_register_device(pdev->button_dev);
	if (rc) {
		input_free_device(pdev->button_dev);
		pdev->button_dev = NULL;
1232
		goto err_video_unreg;
1233 1234 1235
	}
#endif

L
Linus Torvalds 已提交
1236
	return 0;
1237

1238
err_video_unreg:
1239
	video_unregister_device(&pdev->vdev);
1240 1241
err_unregister_v4l2_dev:
	v4l2_device_unregister(&pdev->v4l2_dev);
1242 1243
err_free_controls:
	v4l2_ctrl_handler_free(&pdev->ctrl_handler);
1244
err_free_mem:
1245
	kfree(pdev->ctrl_buf);
1246
	kfree(pdev);
1247
	return rc;
L
Linus Torvalds 已提交
1248 1249
}

1250
/* The user yanked out the cable... */
L
Linus Torvalds 已提交
1251 1252
static void usb_pwc_disconnect(struct usb_interface *intf)
{
1253 1254
	struct v4l2_device *v = usb_get_intfdata(intf);
	struct pwc_device *pdev = container_of(v, struct pwc_device, v4l2_dev);
L
Linus Torvalds 已提交
1255

H
Hans de Goede 已提交
1256 1257 1258
	mutex_lock(&pdev->v4l2_lock);

	mutex_lock(&pdev->vb_queue_lock);
1259
	/* No need to keep the urbs around after disconnection */
H
Hans de Goede 已提交
1260 1261
	if (pdev->vb_queue.streaming)
		pwc_isoc_cleanup(pdev);
1262
	pdev->udev = NULL;
H
Hans de Goede 已提交
1263
	pwc_cleanup_queued_bufs(pdev);
H
Hans de Goede 已提交
1264
	mutex_unlock(&pdev->vb_queue_lock);
H
Hans de Goede 已提交
1265

H
Hans de Goede 已提交
1266
	v4l2_device_disconnect(&pdev->v4l2_dev);
1267
	video_unregister_device(&pdev->vdev);
H
Hans de Goede 已提交
1268 1269

	mutex_unlock(&pdev->v4l2_lock);
1270 1271 1272 1273 1274

#ifdef CONFIG_USB_PWC_INPUT_EVDEV
	if (pdev->button_dev)
		input_unregister_device(pdev->button_dev);
#endif
1275 1276

	v4l2_device_put(&pdev->v4l2_dev);
L
Linus Torvalds 已提交
1277 1278 1279
}


1280 1281
/*
 * Initialization code & module stuff
L
Linus Torvalds 已提交
1282 1283
 */

1284
static unsigned int leds_nargs;
1285

1286
#ifdef CONFIG_USB_PWC_DEBUG
1287 1288
module_param_named(trace, pwc_trace, int, 0644);
#endif
1289
module_param(power_save, int, 0644);
1290
module_param_array(leds, int, &leds_nargs, 0444);
L
Linus Torvalds 已提交
1291

1292
#ifdef CONFIG_USB_PWC_DEBUG
L
Linus Torvalds 已提交
1293
MODULE_PARM_DESC(trace, "For debugging purposes");
1294
#endif
1295
MODULE_PARM_DESC(power_save, "Turn power saving for new cameras on or off");
L
Linus Torvalds 已提交
1296 1297 1298 1299 1300
MODULE_PARM_DESC(leds, "LED on,off time in milliseconds");

MODULE_DESCRIPTION("Philips & OEM USB webcam driver");
MODULE_AUTHOR("Luc Saillard <luc@saillard.org>");
MODULE_LICENSE("GPL");
1301 1302
MODULE_ALIAS("pwcx");
MODULE_VERSION( PWC_VERSION );
L
Linus Torvalds 已提交
1303

H
Hans de Goede 已提交
1304
module_usb_driver(pwc_driver);