pwc-if.c 34.3 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds's avatar
Linus Torvalds committed
2 3 4
/* Linux driver for Philips webcam
   USB and Video4Linux interface part.
   (C) 1999-2004 Nemosoft Unv.
5
   (C) 2004-2006 Luc Saillard (luc@saillard.org)
6
   (C) 2011 Hans de Goede <hdegoede@redhat.com>
Linus Torvalds's avatar
Linus Torvalds committed
7 8 9 10 11 12 13 14 15 16

   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.


*/

17
/*
Linus Torvalds's avatar
Linus Torvalds committed
18 19 20
   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
21
   is thus not distributed in source form. The binary pwcx.o module
Linus Torvalds's avatar
Linus Torvalds committed
22
   contains the code that falls under the NDA.
23 24

   In case you're wondering: 'pwc' stands for "Philips WebCam", but
Linus Torvalds's avatar
Linus Torvalds committed
25 26 27
   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).
28

Linus Torvalds's avatar
Linus Torvalds committed
29 30 31
   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 *
32
   vdev: struct video_device (member of pwc_dev)
Linus Torvalds's avatar
Linus Torvalds committed
33 34 35 36 37 38 39 40 41 42 43 44
   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
45
   - Pham Thanh Nam: webcam snapshot button as an event input device
Linus Torvalds's avatar
Linus Torvalds committed
46 47 48 49 50 51 52 53
*/

#include <linux/errno.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/poll.h>
#include <linux/slab.h>
54 55 56
#ifdef CONFIG_USB_PWC_INPUT_EVDEV
#include <linux/usb/input.h>
#endif
Linus Torvalds's avatar
Linus Torvalds committed
57 58
#include <linux/vmalloc.h>
#include <asm/io.h>
59
#include <linux/kernel.h>		/* simple_strtol() */
Linus Torvalds's avatar
Linus Torvalds committed
60 61 62 63

#include "pwc.h"
#include "pwc-kiara.h"
#include "pwc-timon.h"
64 65
#include "pwc-dec23.h"
#include "pwc-dec1.h"
Linus Torvalds's avatar
Linus Torvalds committed
66

67 68 69
#define CREATE_TRACE_POINTS
#include <trace/events/pwc.h>

Linus Torvalds's avatar
Linus Torvalds committed
70 71 72
/* Function prototypes and driver templates */

/* hotplug device table support */
73
static const struct usb_device_id pwc_device_table [] = {
74 75 76 77 78 79 80 81 82 83 84 85 86 87
	{ USB_DEVICE(0x0471, 0x0302) }, /* Philips PCA645VC */
	{ USB_DEVICE(0x0471, 0x0303) }, /* Philips PCA646VC */
	{ USB_DEVICE(0x0471, 0x0304) }, /* Askey VC010 type 2 */
	{ USB_DEVICE(0x0471, 0x0307) }, /* Philips PCVC675K (Vesta) */
	{ USB_DEVICE(0x0471, 0x0308) }, /* Philips PCVC680K (Vesta Pro) */
	{ USB_DEVICE(0x0471, 0x030C) }, /* Philips PCVC690K (Vesta Pro Scan) */
	{ USB_DEVICE(0x0471, 0x0310) }, /* Philips PCVC730K (ToUCam Fun)/PCVC830 (ToUCam II) */
	{ USB_DEVICE(0x0471, 0x0311) }, /* Philips PCVC740K (ToUCam Pro)/PCVC840 (ToUCam II) */
	{ USB_DEVICE(0x0471, 0x0312) }, /* Philips PCVC750K (ToUCam Pro Scan) */
	{ USB_DEVICE(0x0471, 0x0313) }, /* Philips PCVC720K/40 (ToUCam XS) */
	{ USB_DEVICE(0x0471, 0x0329) }, /* Philips SPC 900NC webcam */
	{ USB_DEVICE(0x0471, 0x032C) }, /* Philips SPC 880NC webcam */
	{ USB_DEVICE(0x069A, 0x0001) }, /* Askey VC010 type 1 */
	{ USB_DEVICE(0x046D, 0x08B0) }, /* Logitech QuickCam 3000 Pro */
Linus Torvalds's avatar
Linus Torvalds committed
88
	{ USB_DEVICE(0x046D, 0x08B1) }, /* Logitech QuickCam Notebook Pro */
89
	{ USB_DEVICE(0x046D, 0x08B2) }, /* Logitech QuickCam 4000 Pro */
Linus Torvalds's avatar
Linus Torvalds committed
90 91 92
	{ 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 */
93
	{ USB_DEVICE(0x046D, 0x08B6) }, /* Logitech/Cisco VT Camera */
94
	{ USB_DEVICE(0x046D, 0x08B7) }, /* Logitech ViewPort AV 100 */
95
	{ USB_DEVICE(0x046D, 0x08B8) }, /* Logitech QuickCam */
96 97 98
	{ USB_DEVICE(0x055D, 0x9000) }, /* Samsung MPC-C10 */
	{ USB_DEVICE(0x055D, 0x9001) }, /* Samsung MPC-C30 */
	{ USB_DEVICE(0x055D, 0x9002) },	/* Samsung SNC-35E (Ver3.0) */
Linus Torvalds's avatar
Linus Torvalds committed
99 100
	{ USB_DEVICE(0x041E, 0x400C) }, /* Creative Webcam 5 */
	{ USB_DEVICE(0x041E, 0x4011) }, /* Creative Webcam Pro Ex */
101 102 103 104
	{ USB_DEVICE(0x04CC, 0x8116) }, /* Sotec Afina Eye */
	{ USB_DEVICE(0x06BE, 0x8116) }, /* AME Co. Afina Eye */
	{ USB_DEVICE(0x0d81, 0x1900) }, /* Visionite VCS-UC300 */
	{ USB_DEVICE(0x0d81, 0x1910) }, /* Visionite VCS-UM100 */
Linus Torvalds's avatar
Linus Torvalds committed
105 106 107 108 109 110
	{ }
};
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);
111
static void pwc_isoc_cleanup(struct pwc_device *pdev);
Linus Torvalds's avatar
Linus Torvalds committed
112 113 114 115 116 117 118 119 120 121 122

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

123
#ifdef CONFIG_USB_PWC_DEBUG
124
	int pwc_trace = PWC_DEBUG_LEVEL;
125
#endif
126
static int power_save = -1;
127
static int leds[2] = { 100, 0 };
Linus Torvalds's avatar
Linus Torvalds committed
128 129 130

/***/

131
static const struct v4l2_file_operations pwc_fops = {
Linus Torvalds's avatar
Linus Torvalds committed
132
	.owner =	THIS_MODULE,
133
	.open =		v4l2_fh_open,
134 135 136 137
	.release =	vb2_fop_release,
	.read =		vb2_fop_read,
	.poll =		vb2_fop_poll,
	.mmap =		vb2_fop_mmap,
138
	.unlocked_ioctl = video_ioctl2,
Linus Torvalds's avatar
Linus Torvalds committed
139
};
140
static const struct video_device pwc_template = {
Linus Torvalds's avatar
Linus Torvalds committed
141
	.name =		"Philips Webcam",	/* Filled in later */
142
	.release =	video_device_release_empty,
Linus Torvalds's avatar
Linus Torvalds committed
143
	.fops =         &pwc_fops,
144
	.ioctl_ops =	&pwc_ioctl_ops,
Linus Torvalds's avatar
Linus Torvalds committed
145 146 147 148 149
};

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

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
static void *pwc_alloc_urb_buffer(struct device *dev,
				  size_t size, dma_addr_t *dma_handle)
{
	void *buffer = kmalloc(size, GFP_KERNEL);

	if (!buffer)
		return NULL;

	*dma_handle = dma_map_single(dev, buffer, size, DMA_FROM_DEVICE);
	if (dma_mapping_error(dev, *dma_handle)) {
		kfree(buffer);
		return NULL;
	}

	return buffer;
}

static void pwc_free_urb_buffer(struct device *dev,
				size_t size,
				void *buffer,
				dma_addr_t dma_handle)
{
	dma_unmap_single(dev, dma_handle, size, DMA_FROM_DEVICE);
	kfree(buffer);
}

176
static struct pwc_frame_buf *pwc_get_next_fill_buf(struct pwc_device *pdev)
Linus Torvalds's avatar
Linus Torvalds committed
177
{
178 179 180 181 182 183 184 185 186 187 188 189
	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;
190 191
}

192 193 194 195 196 197 198 199 200 201
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) {
202
		input_report_key(pdev->button_dev, KEY_CAMERA, down);
203 204 205 206 207
		input_sync(pdev->button_dev);
	}
#endif
}

208
static void pwc_frame_complete(struct pwc_device *pdev)
209
{
210
	struct pwc_frame_buf *fbuf = pdev->fill_buf;
211 212 213 214 215 216 217 218 219 220 221 222 223

	/* 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) {
224
			pwc_snapshot_button(pdev, ptr[0] & 0x01);
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
		}
		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++;
244
	} else if (pdev->type == 740 || pdev->type == 720) {
245 246
		unsigned char *ptr = (unsigned char *)fbuf->data;
		if ((ptr[0] ^ pdev->vmirror) & 0x01) {
247
			pwc_snapshot_button(pdev, ptr[0] & 0x01);
248 249 250 251
		}
		pdev->vmirror = ptr[0] & 0x03;
	}

252 253
	/* In case we were instructed to drop the frame, do so silently. */
	if (pdev->drop_frames > 0) {
254
		pdev->drop_frames--;
255
	} else {
256 257
		/* Check for underflow first */
		if (fbuf->filled < pdev->frame_total_size) {
258 259
			PWC_DEBUG_FLOW("Frame buffer underflow (%d bytes); discarded.\n",
				       fbuf->filled);
260
		} else {
261 262 263
			fbuf->vb.field = V4L2_FIELD_NONE;
			fbuf->vb.sequence = pdev->vframe_count;
			vb2_buffer_done(&fbuf->vb.vb2_buf, VB2_BUF_STATE_DONE);
264 265
			pdev->fill_buf = NULL;
			pdev->vsync = 0;
266 267 268 269
		}
	} /* !drop_frames */
	pdev->vframe_count++;
}
Linus Torvalds's avatar
Linus Torvalds committed
270 271 272 273

/* 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.
 */
274
static void pwc_isoc_handler(struct urb *urb)
Linus Torvalds's avatar
Linus Torvalds committed
275
{
276
	struct pwc_device *pdev = (struct pwc_device *)urb->context;
Linus Torvalds's avatar
Linus Torvalds committed
277
	int i, fst, flen;
278
	unsigned char *iso_buf = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
279

280 281
	trace_pwc_handler_enter(urb, pdev);

282 283
	if (urb->status == -ENOENT || urb->status == -ECONNRESET ||
	    urb->status == -ESHUTDOWN) {
284 285
		PWC_DEBUG_OPEN("URB (%p) unlinked %ssynchronously.\n",
			       urb, urb->status == -ENOENT ? "" : "a");
Linus Torvalds's avatar
Linus Torvalds committed
286 287
		return;
	}
288 289 290 291 292

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

	if (urb->status != 0) {
Linus Torvalds's avatar
Linus Torvalds committed
293 294 295 296 297 298 299 300 301
		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;
302
			case -ETIME:		errmsg = "Device does not respond"; break;
Linus Torvalds's avatar
Linus Torvalds committed
303
		}
304 305 306
		PWC_ERROR("pwc_isoc_handler() called with status %d [%s].\n",
			  urb->status, errmsg);
		/* Give up after a number of contiguous errors */
Linus Torvalds's avatar
Linus Torvalds committed
307 308
		if (++pdev->visoc_errors > MAX_ISOC_ERRORS)
		{
309 310
			PWC_ERROR("Too many ISOC errors, bailing out.\n");
			if (pdev->fill_buf) {
311
				vb2_buffer_done(&pdev->fill_buf->vb.vb2_buf,
312 313 314
						VB2_BUF_STATE_ERROR);
				pdev->fill_buf = NULL;
			}
Linus Torvalds's avatar
Linus Torvalds committed
315
		}
316
		pdev->vsync = 0; /* Drop the current frame */
Linus Torvalds's avatar
Linus Torvalds committed
317 318 319 320 321 322
		goto handler_end;
	}

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

323 324 325 326 327
	dma_sync_single_for_cpu(&urb->dev->dev,
				urb->transfer_dma,
				urb->transfer_buffer_length,
				DMA_FROM_DEVICE);

Linus Torvalds's avatar
Linus Torvalds committed
328
	/* vsync: 0 = don't copy data
329 330
		  1 = sync-hunt
		  2 = synched
Linus Torvalds's avatar
Linus Torvalds committed
331 332 333 334 335 336
	 */
	/* 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;
337 338 339 340 341 342 343 344
		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) {
345
				fbuf->vb.vb2_buf.timestamp = ktime_get_ns();
346 347 348 349 350 351 352 353 354 355 356
				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);
Linus Torvalds's avatar
Linus Torvalds committed
357
				fbuf->filled += flen;
358 359 360 361 362 363 364 365 366 367
			}
		}
		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;
Linus Torvalds's avatar
Linus Torvalds committed
368
				pdev->vsync = 1;
369
			}
Linus Torvalds's avatar
Linus Torvalds committed
370
		}
371
		pdev->vlast_packet_size = flen;
Linus Torvalds's avatar
Linus Torvalds committed
372 373
	}

374 375 376 377 378
	dma_sync_single_for_device(&urb->dev->dev,
				   urb->transfer_dma,
				   urb->transfer_buffer_length,
				   DMA_FROM_DEVICE);

Linus Torvalds's avatar
Linus Torvalds committed
379
handler_end:
380 381
	trace_pwc_handler_exit(urb, pdev);

Linus Torvalds's avatar
Linus Torvalds committed
382 383
	i = usb_submit_urb(urb, GFP_ATOMIC);
	if (i != 0)
384
		PWC_ERROR("Error (%d) re-submitting urb in pwc_isoc_handler.\n", i);
Linus Torvalds's avatar
Linus Torvalds committed
385 386
}

Hans de Goede's avatar
Hans de Goede committed
387
/* Both v4l2_lock and vb_queue_lock should be locked when calling this */
388
static int pwc_isoc_init(struct pwc_device *pdev)
Linus Torvalds's avatar
Linus Torvalds committed
389 390 391 392 393 394
{
	struct usb_device *udev;
	struct urb *urb;
	int i, j, ret;
	struct usb_interface *intf;
	struct usb_host_interface *idesc = NULL;
395
	int compression = 0; /* 0..3 = uncompressed..high */
Linus Torvalds's avatar
Linus Torvalds committed
396 397

	pdev->vsync = 0;
398
	pdev->vlast_packet_size = 0;
399 400
	pdev->fill_buf = NULL;
	pdev->vframe_count = 0;
401
	pdev->visoc_errors = 0;
Linus Torvalds's avatar
Linus Torvalds committed
402 403
	udev = pdev->udev;

404 405 406
retry:
	/* We first try with low compression and then retry with a higher
	   compression setting if there is not enough bandwidth. */
407 408
	ret = pwc_set_video_mode(pdev, pdev->width, pdev->height, pdev->pixfmt,
				 pdev->vframes, &compression, 1);
409

Linus Torvalds's avatar
Linus Torvalds committed
410 411 412 413 414
	/* 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)
415
		return -EIO;
Linus Torvalds's avatar
Linus Torvalds committed
416 417 418

	/* Search video endpoint */
	pdev->vmax_packet_size = -1;
419
	for (i = 0; i < idesc->desc.bNumEndpoints; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
420 421 422 423
		if ((idesc->endpoint[i].desc.bEndpointAddress & 0xF) == pdev->vendpoint) {
			pdev->vmax_packet_size = le16_to_cpu(idesc->endpoint[i].desc.wMaxPacketSize);
			break;
		}
424
	}
425

Linus Torvalds's avatar
Linus Torvalds committed
426
	if (pdev->vmax_packet_size < 0 || pdev->vmax_packet_size > ISO_MAX_FRAME_SIZE) {
427
		PWC_ERROR("Failed to find packet size for video endpoint in current alternate setting.\n");
428
		return -ENFILE; /* Odd error, that should be noticeable */
Linus Torvalds's avatar
Linus Torvalds committed
429 430 431
	}

	/* Set alternate interface */
432
	PWC_DEBUG_OPEN("Setting alternate interface %d\n", pdev->valternate);
Linus Torvalds's avatar
Linus Torvalds committed
433
	ret = usb_set_interface(pdev->udev, 0, pdev->valternate);
434 435 436 437
	if (ret == -ENOSPC && compression < 3) {
		compression++;
		goto retry;
	}
Linus Torvalds's avatar
Linus Torvalds committed
438 439 440
	if (ret < 0)
		return ret;

441
	/* Allocate and init Isochronuous urbs */
Linus Torvalds's avatar
Linus Torvalds committed
442 443 444
	for (i = 0; i < MAX_ISO_BUFS; i++) {
		urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
		if (urb == NULL) {
445 446
			pwc_isoc_cleanup(pdev);
			return -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
447
		}
448
		pdev->urbs[i] = urb;
449
		PWC_DEBUG_MEMORY("Allocated URB at 0x%p\n", urb);
Linus Torvalds's avatar
Linus Torvalds committed
450 451 452

		urb->interval = 1; // devik
		urb->dev = udev;
453
		urb->pipe = usb_rcvisocpipe(udev, pdev->vendpoint);
454
		urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
455 456 457 458
		urb->transfer_buffer_length = ISO_BUFFER_SIZE;
		urb->transfer_buffer = pwc_alloc_urb_buffer(&udev->dev,
							    urb->transfer_buffer_length,
							    &urb->transfer_dma);
459 460 461 462 463
		if (urb->transfer_buffer == NULL) {
			PWC_ERROR("Failed to allocate urb buffer %d\n", i);
			pwc_isoc_cleanup(pdev);
			return -ENOMEM;
		}
464 465
		urb->complete = pwc_isoc_handler;
		urb->context = pdev;
Linus Torvalds's avatar
Linus Torvalds committed
466 467 468 469 470 471 472 473 474 475
		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++) {
476
		ret = usb_submit_urb(pdev->urbs[i], GFP_KERNEL);
477 478 479 480 481
		if (ret == -ENOSPC && compression < 3) {
			compression++;
			pwc_isoc_cleanup(pdev);
			goto retry;
		}
482
		if (ret) {
483
			PWC_ERROR("isoc_init() submit_urb %d failed with error %d\n", i, ret);
484 485 486
			pwc_isoc_cleanup(pdev);
			return ret;
		}
487
		PWC_DEBUG_MEMORY("URB 0x%p submitted.\n", pdev->urbs[i]);
Linus Torvalds's avatar
Linus Torvalds committed
488 489 490
	}

	/* All is done... */
491
	PWC_DEBUG_OPEN("<< pwc_isoc_init()\n");
Linus Torvalds's avatar
Linus Torvalds committed
492 493 494
	return 0;
}

495
static void pwc_iso_stop(struct pwc_device *pdev)
Linus Torvalds's avatar
Linus Torvalds committed
496 497 498 499 500
{
	int i;

	/* Unlinking ISOC buffers one by one */
	for (i = 0; i < MAX_ISO_BUFS; i++) {
501 502 503
		if (pdev->urbs[i]) {
			PWC_DEBUG_MEMORY("Unlinking URB %p\n", pdev->urbs[i]);
			usb_kill_urb(pdev->urbs[i]);
504 505 506 507 508 509 510 511 512 513
		}
	}
}

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++) {
514 515 516
		struct urb *urb = pdev->urbs[i];

		if (urb) {
517
			PWC_DEBUG_MEMORY("Freeing URB\n");
518 519 520 521 522 523
			if (urb->transfer_buffer)
				pwc_free_urb_buffer(&urb->dev->dev,
						    urb->transfer_buffer_length,
						    urb->transfer_buffer,
						    urb->transfer_dma);
			usb_free_urb(urb);
524
			pdev->urbs[i] = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
525 526
		}
	}
527 528
}

Hans de Goede's avatar
Hans de Goede committed
529
/* Both v4l2_lock and vb_queue_lock should be locked when calling this */
530
static void pwc_isoc_cleanup(struct pwc_device *pdev)
531 532
{
	PWC_DEBUG_OPEN(">> pwc_isoc_cleanup()\n");
533

534 535
	pwc_iso_stop(pdev);
	pwc_iso_free(pdev);
536
	usb_set_interface(pdev->udev, 0, 0);
Linus Torvalds's avatar
Linus Torvalds committed
537

538
	PWC_DEBUG_OPEN("<< pwc_isoc_cleanup()\n");
Linus Torvalds's avatar
Linus Torvalds committed
539 540
}

Hans de Goede's avatar
Hans de Goede committed
541
/* Must be called with vb_queue_lock hold */
Hans Verkuil's avatar
Hans Verkuil committed
542 543
static void pwc_cleanup_queued_bufs(struct pwc_device *pdev,
				    enum vb2_buffer_state state)
544
{
Hans de Goede's avatar
Hans de Goede committed
545 546 547
	unsigned long flags = 0;

	spin_lock_irqsave(&pdev->queued_bufs_lock, flags);
548 549 550 551 552 553
	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);
554
		vb2_buffer_done(&buf->vb.vb2_buf, state);
555
	}
Hans de Goede's avatar
Hans de Goede committed
556
	spin_unlock_irqrestore(&pdev->queued_bufs_lock, flags);
557 558
}

559
#ifdef CONFIG_USB_PWC_DEBUG
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
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:
584
			return "unknown type of sensor";
585 586 587
	}
}
#endif
Linus Torvalds's avatar
Linus Torvalds committed
588 589 590 591

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

592
static void pwc_video_release(struct v4l2_device *v)
Linus Torvalds's avatar
Linus Torvalds committed
593
{
594
	struct pwc_device *pdev = container_of(v, struct pwc_device, v4l2_dev);
595

596
	v4l2_ctrl_handler_free(&pdev->ctrl_handler);
Hans de Goede's avatar
Hans de Goede committed
597
	v4l2_device_unregister(&pdev->v4l2_dev);
598
	kfree(pdev->ctrl_buf);
599
	kfree(pdev);
600 601
}

602 603 604
/***************************************************************************/
/* Videobuf2 operations */

605
static int queue_setup(struct vb2_queue *vq,
606
				unsigned int *nbuffers, unsigned int *nplanes,
607
				unsigned int sizes[], struct device *alloc_devs[])
608 609
{
	struct pwc_device *pdev = vb2_get_drv_priv(vq);
610
	int size;
611 612 613 614 615 616 617 618

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

	*nplanes = 1;

619 620 621
	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);
622 623 624 625 626 627

	return 0;
}

static int buffer_init(struct vb2_buffer *vb)
{
628 629 630
	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
	struct pwc_frame_buf *buf =
		container_of(vbuf, struct pwc_frame_buf, vb);
631

632 633 634 635 636 637 638 639 640 641 642 643
	/* 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);

644
	/* Don't allow queueing new buffers after device disconnection */
645 646
	if (!pdev->udev)
		return -ENODEV;
Linus Torvalds's avatar
Linus Torvalds committed
647 648 649 650

	return 0;
}

651
static void buffer_finish(struct vb2_buffer *vb)
Linus Torvalds's avatar
Linus Torvalds committed
652
{
653
	struct pwc_device *pdev = vb2_get_drv_priv(vb->vb2_queue);
654 655 656
	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
	struct pwc_frame_buf *buf =
		container_of(vbuf, struct pwc_frame_buf, vb);
657

658 659 660 661 662 663 664 665 666
	if (vb->state == VB2_BUF_STATE_DONE) {
		/*
		 * 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.
		 */
		pwc_decompress(pdev, buf);
	}
667 668 669 670
}

static void buffer_cleanup(struct vb2_buffer *vb)
{
671 672 673
	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
	struct pwc_frame_buf *buf =
		container_of(vbuf, struct pwc_frame_buf, vb);
674 675 676 677 678 679 680

	vfree(buf->data);
}

static void buffer_queue(struct vb2_buffer *vb)
{
	struct pwc_device *pdev = vb2_get_drv_priv(vb->vb2_queue);
681 682 683
	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
	struct pwc_frame_buf *buf =
		container_of(vbuf, struct pwc_frame_buf, vb);
684 685
	unsigned long flags = 0;

Hans de Goede's avatar
Hans de Goede committed
686
	/* Check the device has not disconnected between prep and queuing */
Hans de Goede's avatar
Hans de Goede committed
687
	if (!pdev->udev) {
688
		vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
Hans de Goede's avatar
Hans de Goede committed
689 690 691 692 693
		return;
	}

	spin_lock_irqsave(&pdev->queued_bufs_lock, flags);
	list_add_tail(&buf->list, &pdev->queued_bufs);
694 695 696
	spin_unlock_irqrestore(&pdev->queued_bufs_lock, flags);
}

697
static int start_streaming(struct vb2_queue *vq, unsigned int count)
698 699
{
	struct pwc_device *pdev = vb2_get_drv_priv(vq);
Hans de Goede's avatar
Hans de Goede committed
700
	int r;
701

Hans de Goede's avatar
Hans de Goede committed
702 703
	if (!pdev->udev)
		return -ENODEV;
704

705 706
	if (mutex_lock_interruptible(&pdev->v4l2_lock))
		return -ERESTARTSYS;
707 708
	/* Turn on camera and set LEDS on */
	pwc_camera_power(pdev, 1);
709
	pwc_set_leds(pdev, leds[0], leds[1]);
710

Hans de Goede's avatar
Hans de Goede committed
711
	r = pwc_isoc_init(pdev);
712 713 714 715 716
	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!! */
Hans Verkuil's avatar
Hans Verkuil committed
717
		pwc_cleanup_queued_bufs(pdev, VB2_BUF_STATE_QUEUED);
718
	}
719
	mutex_unlock(&pdev->v4l2_lock);
Hans de Goede's avatar
Hans de Goede committed
720

Hans de Goede's avatar
Hans de Goede committed
721
	return r;
722 723
}

724
static void stop_streaming(struct vb2_queue *vq)
725 726 727
{
	struct pwc_device *pdev = vb2_get_drv_priv(vq);

728
	mutex_lock(&pdev->v4l2_lock);
729 730 731
	if (pdev->udev) {
		pwc_set_leds(pdev, 0, 0);
		pwc_camera_power(pdev, 0);
732
		pwc_isoc_cleanup(pdev);
733
	}
Hans de Goede's avatar
Hans de Goede committed
734

Hans Verkuil's avatar
Hans Verkuil committed
735 736
	pwc_cleanup_queued_bufs(pdev, VB2_BUF_STATE_ERROR);
	if (pdev->fill_buf)
737 738
		vb2_buffer_done(&pdev->fill_buf->vb.vb2_buf,
				VB2_BUF_STATE_ERROR);
739
	mutex_unlock(&pdev->v4l2_lock);
Linus Torvalds's avatar
Linus Torvalds committed
740 741
}

742
static const struct vb2_ops pwc_vb_queue_ops = {
743 744 745 746 747 748 749 750
	.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,
751 752
	.wait_prepare		= vb2_ops_wait_prepare,
	.wait_finish		= vb2_ops_wait_finish,
753 754
};

Linus Torvalds's avatar
Linus Torvalds committed
755 756 757 758 759 760 761 762 763 764 765 766
/***************************************************************************/
/* 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;
767
	int rc;
Linus Torvalds's avatar
Linus Torvalds committed
768
	int features = 0;
769
	int compression = 0;
770
	int my_power_save = power_save;
Linus Torvalds's avatar
Linus Torvalds committed
771 772
	char serial_number[30], *name;

773 774 775
	vendor_id = le16_to_cpu(udev->descriptor.idVendor);
	product_id = le16_to_cpu(udev->descriptor.idProduct);

Linus Torvalds's avatar
Linus Torvalds committed
776
	/* Check if we can handle this device */
777 778
	PWC_DEBUG_PROBE("probe() called [%04X %04X], if %d\n",
		vendor_id, product_id,
Linus Torvalds's avatar
Linus Torvalds committed
779 780 781 782 783 784 785 786 787 788 789 790
		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:
791
			PWC_INFO("Philips PCA645VC USB webcam detected.\n");
Linus Torvalds's avatar
Linus Torvalds committed
792 793 794 795
			name = "Philips 645 webcam";
			type_id = 645;
			break;
		case 0x0303:
796
			PWC_INFO("Philips PCA646VC USB webcam detected.\n");
Linus Torvalds's avatar
Linus Torvalds committed
797 798 799 800
			name = "Philips 646 webcam";
			type_id = 646;
			break;
		case 0x0304:
801
			PWC_INFO("Askey VC010 type 2 USB webcam detected.\n");
Linus Torvalds's avatar
Linus Torvalds committed
802 803 804 805
			name = "Askey VC010 webcam";
			type_id = 646;
			break;
		case 0x0307:
806
			PWC_INFO("Philips PCVC675K (Vesta) USB webcam detected.\n");
Linus Torvalds's avatar
Linus Torvalds committed
807 808 809 810
			name = "Philips 675 webcam";
			type_id = 675;
			break;
		case 0x0308:
811
			PWC_INFO("Philips PCVC680K (Vesta Pro) USB webcam detected.\n");
Linus Torvalds's avatar
Linus Torvalds committed
812 813 814 815
			name = "Philips 680 webcam";
			type_id = 680;
			break;
		case 0x030C:
816
			PWC_INFO("Philips PCVC690K (Vesta Pro Scan) USB webcam detected.\n");
Linus Torvalds's avatar
Linus Torvalds committed
817 818 819 820
			name = "Philips 690 webcam";
			type_id = 690;
			break;
		case 0x0310:
821
			PWC_INFO("Philips PCVC730K (ToUCam Fun)/PCVC830 (ToUCam II) USB webcam detected.\n");
Linus Torvalds's avatar
Linus Torvalds committed
822 823 824 825
			name = "Philips 730 webcam";
			type_id = 730;
			break;
		case 0x0311:
826
			PWC_INFO("Philips PCVC740K (ToUCam Pro)/PCVC840 (ToUCam II) USB webcam detected.\n");
Linus Torvalds's avatar
Linus Torvalds committed
827 828 829 830
			name = "Philips 740 webcam";
			type_id = 740;
			break;
		case 0x0312:
831
			PWC_INFO("Philips PCVC750K (ToUCam Pro Scan) USB webcam detected.\n");
Linus Torvalds's avatar
Linus Torvalds committed
832 833 834 835
			name = "Philips 750 webcam";
			type_id = 750;
			break;
		case 0x0313:
836
			PWC_INFO("Philips PCVC720K/40 (ToUCam XS) USB webcam detected.\n");
Linus Torvalds's avatar
Linus Torvalds committed
837 838 839
			name = "Philips 720K/40 webcam";
			type_id = 720;
			break;
840 841 842
		case 0x0329:
			PWC_INFO("Philips SPC 900NC USB webcam detected.\n");
			name = "Philips SPC 900NC webcam";
843
			type_id = 740;
844
			break;
845 846 847 848 849
		case 0x032C:
			PWC_INFO("Philips SPC 880NC USB webcam detected.\n");
			name = "Philips SPC 880NC webcam";
			type_id = 740;
			break;
Linus Torvalds's avatar
Linus Torvalds committed
850 851 852 853 854 855 856 857
		default:
			return -ENODEV;
			break;
		}
	}
	else if (vendor_id == 0x069A) {
		switch(product_id) {
		case 0x0001:
858
			PWC_INFO("Askey VC010 type 1 USB webcam detected.\n");
Linus Torvalds's avatar
Linus Torvalds committed
859 860 861 862 863 864 865 866 867 868 869
			name = "Askey VC010 webcam";
			type_id = 645;
			break;
		default:
			return -ENODEV;
			break;
		}
	}
	else if (vendor_id == 0x046d) {
		switch(product_id) {
		case 0x08b0:
870
			PWC_INFO("Logitech QuickCam Pro 3000 USB webcam detected.\n");
Linus Torvalds's avatar
Linus Torvalds committed
871 872 873 874
			name = "Logitech QuickCam Pro 3000";
			type_id = 740; /* CCD sensor */
			break;
		case 0x08b1:
875
			PWC_INFO("Logitech QuickCam Notebook Pro USB webcam detected.\n");
Linus Torvalds's avatar
Linus Torvalds committed
876 877 878 879
			name = "Logitech QuickCam Notebook Pro";
			type_id = 740; /* CCD sensor */
			break;
		case 0x08b2:
880
			PWC_INFO("Logitech QuickCam 4000 Pro USB webcam detected.\n");
Linus Torvalds's avatar
Linus Torvalds committed
881 882
			name = "Logitech QuickCam Pro 4000";
			type_id = 740; /* CCD sensor */
883 884
			if (my_power_save == -1)
				my_power_save = 1;
Linus Torvalds's avatar
Linus Torvalds committed
885 886
			break;
		case 0x08b3:
887
			PWC_INFO("Logitech QuickCam Zoom USB webcam detected.\n");
Linus Torvalds's avatar
Linus Torvalds committed
888 889 890 891
			name = "Logitech QuickCam Zoom";
			type_id = 740; /* CCD sensor */
			break;
		case 0x08B4:
892
			PWC_INFO("Logitech QuickCam Zoom (new model) USB webcam detected.\n");
Linus Torvalds's avatar
Linus Torvalds committed
893 894
			name = "Logitech QuickCam Zoom";
			type_id = 740; /* CCD sensor */
895 896
			if (my_power_save == -1)
				my_power_save = 1;
Linus Torvalds's avatar
Linus Torvalds committed
897 898
			break;
		case 0x08b5:
899
			PWC_INFO("Logitech QuickCam Orbit/Sphere USB webcam detected.\n");
Linus Torvalds's avatar
Linus Torvalds committed
900 901
			name = "Logitech QuickCam Orbit";
			type_id = 740; /* CCD sensor */
902 903
			if (my_power_save == -1)
				my_power_save = 1;
Linus Torvalds's avatar
Linus Torvalds committed
904 905 906
			features |= FEATURE_MOTOR_PANTILT;
			break;
		case 0x08b6:
907 908 909 910
			PWC_INFO("Logitech/Cisco VT Camera webcam detected.\n");
			name = "Cisco VT Camera";
			type_id = 740; /* CCD sensor */
			break;
Linus Torvalds's avatar
Linus Torvalds committed
911
		case 0x08b7:
912 913 914 915 916
			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? */
917
			PWC_INFO("Logitech QuickCam detected (reserved ID).\n");
Linus Torvalds's avatar
Linus Torvalds committed
918 919 920
			name = "Logitech QuickCam (res.)";
			type_id = 730; /* Assuming CMOS */
			break;
921
		default:
Linus Torvalds's avatar
Linus Torvalds committed
922
			return -ENODEV;
923 924 925
			break;
		}
	}
Linus Torvalds's avatar
Linus Torvalds committed
926 927 928 929 930 931 932
	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:
933
			PWC_INFO("Samsung MPC-C10 USB webcam detected.\n");
Linus Torvalds's avatar
Linus Torvalds committed
934 935 936 937
			name = "Samsung MPC-C10";
			type_id = 675;
			break;
		case 0x9001:
938
			PWC_INFO("Samsung MPC-C30 USB webcam detected.\n");
Linus Torvalds's avatar
Linus Torvalds committed
939 940 941
			name = "Samsung MPC-C30";
			type_id = 675;
			break;
942 943 944 945 946
		case 0x9002:
			PWC_INFO("Samsung SNC-35E (v3.0) USB webcam detected.\n");
			name = "Samsung MPC-C30";
			type_id = 740;
			break;
Linus Torvalds's avatar
Linus Torvalds committed
947 948 949 950 951 952 953 954
		default:
			return -ENODEV;
			break;
		}
	}
	else if (vendor_id == 0x041e) {
		switch(product_id) {
		case 0x400c:
955
			PWC_INFO("Creative Labs Webcam 5 detected.\n");
Linus Torvalds's avatar
Linus Torvalds committed
956 957
			name = "Creative Labs Webcam 5";
			type_id = 730;
958 959
			if (my_power_save == -1)
				my_power_save = 1;
Linus Torvalds's avatar
Linus Torvalds committed
960 961
			break;
		case 0x4011:
962
			PWC_INFO("Creative Labs Webcam Pro Ex detected.\n");
Linus Torvalds's avatar
Linus Torvalds committed
963 964 965 966 967 968 969 970 971 972 973
			name = "Creative Labs Webcam Pro Ex";
			type_id = 740;
			break;
		default:
			return -ENODEV;
			break;
		}
	}
	else if (vendor_id == 0x04cc) {
		switch(product_id) {
		case 0x8116:
974
			PWC_INFO("Sotec Afina Eye USB webcam detected.\n");
Linus Torvalds's avatar
Linus Torvalds committed
975 976 977 978 979 980 981 982 983 984 985 986
			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 */
987
			PWC_INFO("AME Co. Afina Eye USB webcam detected.\n");
Linus Torvalds's avatar
Linus Torvalds committed
988 989 990 991 992 993 994
			name = "AME Co. Afina Eye";
			type_id = 750;
			break;
		default:
			return -ENODEV;
			break;
		}
995

Linus Torvalds's avatar
Linus Torvalds committed
996 997 998 999
	}
	else if (vendor_id == 0x0d81) {
		switch(product_id) {
		case 0x1900:
1000
			PWC_INFO("Visionite VCS-UC300 USB webcam detected.\n");
Linus Torvalds's avatar
Linus Torvalds committed
1001 1002 1003 1004
			name = "Visionite VCS-UC300";
			type_id = 740; /* CCD sensor */
			break;
		case 0x1910:
1005
			PWC_INFO("Visionite VCS-UM100 USB webcam detected.\n");
Linus Torvalds's avatar
Linus Torvalds committed
1006 1007 1008 1009 1010 1011 1012 1013
			name = "Visionite VCS-UM100";
			type_id = 730; /* CMOS sensor */
			break;
		default:
			return -ENODEV;
			break;
		}
	}
1014
	else
Linus Torvalds's avatar
Linus Torvalds committed
1015 1016
		return -ENODEV; /* Not any of the know types; but the list keeps growing. */

1017 1018 1019
	if (my_power_save == -1)
		my_power_save = 0;

Linus Torvalds's avatar
Linus Torvalds committed
1020 1021
	memset(serial_number, 0, 30);
	usb_string(udev, udev->descriptor.iSerialNumber, serial_number, 29);
1022
	PWC_DEBUG_PROBE("Device serial number is %s\n", serial_number);
Linus Torvalds's avatar
Linus Torvalds committed
1023 1024

	if (udev->descriptor.bNumConfigurations > 1)
1025
		PWC_WARNING("Warning: more than 1 configuration available.\n");
Linus Torvalds's avatar
Linus Torvalds committed
1026 1027

	/* Allocate structure, initialize pointers, mutexes, etc. and link it to the usb_device */
1028
	pdev = kzalloc(sizeof(struct pwc_device), GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
1029
	if (pdev == NULL) {
1030
		PWC_ERROR("Oops, could not allocate memory for pwc_device.\n");
Linus Torvalds's avatar
Linus Torvalds committed
1031 1032 1033 1034
		return -ENOMEM;
	}
	pdev->type = type_id;
	pdev->features = features;
1035
	pwc_construct(pdev); /* set min/max sizes correct */
Linus Torvalds's avatar
Linus Torvalds committed
1036

Hans de Goede's avatar
Hans de Goede committed
1037 1038
	mutex_init(&pdev->v4l2_lock);
	mutex_init(&pdev->vb_queue_lock);
1039 1040
	spin_lock_init(&pdev->queued_bufs_lock);
	INIT_LIST_HEAD(&pdev->queued_bufs);
Linus Torvalds's avatar
Linus Torvalds committed
1041 1042

	pdev->udev = udev;
1043
	pdev->power_save = my_power_save;
Linus Torvalds's avatar
Linus Torvalds committed
1044

1045 1046 1047 1048 1049 1050 1051
	/* Init videobuf2 queue structure */
	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;
1052
	pdev->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1053 1054 1055 1056 1057
	rc = vb2_queue_init(&pdev->vb_queue);
	if (rc < 0) {
		PWC_ERROR("Oops, could not initialize vb2 queue.\n");
		goto err_free_mem;
	}
1058

1059
	/* Init video_device structure */
1060
	pdev->vdev = pwc_template;
1061
	strscpy(pdev->vdev.name, name, sizeof(pdev->vdev.name));
1062 1063
	pdev->vdev.queue = &pdev->vb_queue;
	pdev->vdev.queue->lock = &pdev->vb_queue_lock;
1064
	video_set_drvdata(&pdev->vdev, pdev);
Linus Torvalds's avatar
Linus Torvalds committed
1065 1066

	pdev->release = le16_to_cpu(udev->descriptor.bcdDevice);
1067
	PWC_DEBUG_PROBE("Release: %04x\n", pdev->release);
Linus Torvalds's avatar
Linus Torvalds committed
1068

1069 1070 1071 1072 1073 1074 1075 1076
	/* 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;
	}

1077 1078 1079 1080 1081 1082 1083 1084 1085
#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

1086 1087
	/* Set the leds off */
	pwc_set_leds(pdev, 0, 0);
1088

1089
	/* Setup initial videomode */
1090
	rc = pwc_set_video_mode(pdev, MAX_WIDTH, MAX_HEIGHT,
1091
				V4L2_PIX_FMT_YUV420, 30, &compression, 1);
1092 1093 1094
	if (rc)
		goto err_free_mem;

1095 1096 1097 1098 1099 1100 1101
	/* 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;
	}

1102
	/* And powerdown the camera until streaming starts */
1103 1104
	pwc_camera_power(pdev, 0);

1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
	/* 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;
Hans de Goede's avatar
Hans de Goede committed
1115
	pdev->vdev.lock = &pdev->v4l2_lock;
1116 1117
	pdev->vdev.device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
				 V4L2_CAP_READWRITE;
Hans de Goede's avatar
Hans de Goede committed
1118

1119
	rc = video_register_device(&pdev->vdev, VFL_TYPE_VIDEO, -1);
1120 1121
	if (rc < 0) {
		PWC_ERROR("Failed to register as video device (%d).\n", rc);
1122
		goto err_unregister_v4l2_dev;
1123
	}
1124
	PWC_INFO("Registered as %s.\n", video_device_node_name(&pdev->vdev));
1125

1126 1127 1128 1129
#ifdef CONFIG_USB_PWC_INPUT_EVDEV
	/* register webcam snapshot button input device */
	pdev->button_dev = input_allocate_device();
	if (!pdev->button_dev) {
1130 1131
		rc = -ENOMEM;
		goto err_video_unreg;
1132 1133
	}

1134 1135 1136
	usb_make_path(udev, pdev->button_phys, sizeof(pdev->button_phys));
	strlcat(pdev->button_phys, "/input0", sizeof(pdev->button_phys));

1137
	pdev->button_dev->name = "PWC snapshot button";
1138
	pdev->button_dev->phys = pdev->button_phys;
1139 1140 1141
	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);
1142
	pdev->button_dev->keybit[BIT_WORD(KEY_CAMERA)] = BIT_MASK(KEY_CAMERA);
1143 1144 1145 1146 1147

	rc = input_register_device(pdev->button_dev);
	if (rc) {
		input_free_device(pdev->button_dev);
		pdev->button_dev = NULL;
1148
		goto err_video_unreg;
1149 1150 1151
	}
#endif

Linus Torvalds's avatar
Linus Torvalds committed
1152
	return 0;
1153

1154
#ifdef CONFIG_USB_PWC_INPUT_EVDEV
1155
err_video_unreg:
1156
	video_unregister_device(&pdev->vdev);
1157
#endif
1158 1159
err_unregister_v4l2_dev:
	v4l2_device_unregister(&pdev->v4l2_dev);
1160 1161
err_free_controls:
	v4l2_ctrl_handler_free(&pdev->ctrl_handler);
1162
err_free_mem:
1163
	kfree(pdev->ctrl_buf);
1164
	kfree(pdev);
1165
	return rc;
Linus Torvalds's avatar
Linus Torvalds committed
1166 1167
}

1168
/* The user yanked out the cable... */
Linus Torvalds's avatar
Linus Torvalds committed
1169 1170
static void usb_pwc_disconnect(struct usb_interface *intf)
{
1171 1172
	struct v4l2_device *v = usb_get_intfdata(intf);
	struct pwc_device *pdev = container_of(v, struct pwc_device, v4l2_dev);
Linus Torvalds's avatar
Linus Torvalds committed
1173

Hans de Goede's avatar
Hans de Goede committed
1174
	mutex_lock(&pdev->vb_queue_lock);
1175
	mutex_lock(&pdev->v4l2_lock);
1176
	/* No need to keep the urbs around after disconnection */
Hans de Goede's avatar
Hans de Goede committed
1177 1178
	if (pdev->vb_queue.streaming)
		pwc_isoc_cleanup(pdev);
1179
	pdev->udev = NULL;
Hans de Goede's avatar
Hans de Goede committed
1180

Hans de Goede's avatar
Hans de Goede committed
1181
	v4l2_device_disconnect(&pdev->v4l2_dev);
1182
	video_unregister_device(&pdev->vdev);
Hans de Goede's avatar
Hans de Goede committed
1183
	mutex_unlock(&pdev->v4l2_lock);
1184
	mutex_unlock(&pdev->vb_queue_lock);
1185 1186 1187 1188 1189

#ifdef CONFIG_USB_PWC_INPUT_EVDEV
	if (pdev->button_dev)
		input_unregister_device(pdev->button_dev);
#endif
1190 1191

	v4l2_device_put(&pdev->v4l2_dev);
Linus Torvalds's avatar
Linus Torvalds committed
1192 1193 1194
}


1195 1196
/*
 * Initialization code & module stuff
Linus Torvalds's avatar
Linus Torvalds committed
1197 1198
 */

1199
static unsigned int leds_nargs;
1200

1201
#ifdef CONFIG_USB_PWC_DEBUG
1202 1203
module_param_named(trace, pwc_trace, int, 0644);
#endif
1204
module_param(power_save, int, 0644);
1205
module_param_array(leds, int, &leds_nargs, 0444);
Linus Torvalds's avatar
Linus Torvalds committed
1206

1207
#ifdef CONFIG_USB_PWC_DEBUG
Linus Torvalds's avatar
Linus Torvalds committed
1208
MODULE_PARM_DESC(trace, "For debugging purposes");
1209
#endif
1210
MODULE_PARM_DESC(power_save, "Turn power saving for new cameras on or off");
Linus Torvalds's avatar
Linus Torvalds committed
1211 1212 1213 1214 1215
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");
1216 1217
MODULE_ALIAS("pwcx");
MODULE_VERSION( PWC_VERSION );
Linus Torvalds's avatar
Linus Torvalds committed
1218

Hans de Goede's avatar
Hans de Goede committed
1219
module_usb_driver(pwc_driver);