pwc-if.c 56.3 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)
L
Linus Torvalds 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

   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

*/

28
/*
L
Linus Torvalds 已提交
29 30 31
   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
32
   is thus not distributed in source form. The binary pwcx.o module
L
Linus Torvalds 已提交
33
   contains the code that falls under the NDA.
34 35

   In case you're wondering: 'pwc' stands for "Philips WebCam", but
L
Linus Torvalds 已提交
36 37 38
   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).
39

L
Linus Torvalds 已提交
40 41 42
   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 *
43
   vdev: struct video_device (member of pwc_dev)
L
Linus Torvalds 已提交
44 45 46 47 48 49 50 51 52 53 54 55
   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
56
   - Pham Thanh Nam: webcam snapshot button as an event input device
L
Linus Torvalds 已提交
57 58 59 60 61 62 63 64
*/

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

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

/* 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 119 120 121 122 123 124 125 126 127 128 129 130 131 132
	{ 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);

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

static int default_size = PSZ_QCIF;
static int default_fps = 10;
static int default_fbufs = 3;   /* Default number of frame buffers */
133
	int pwc_mbufs = 2;	/* Default number of mmap() buffers */
134
#ifdef CONFIG_USB_PWC_DEBUG
135
	int pwc_trace = PWC_DEBUG_LEVEL;
136
#endif
137 138
static int power_save;
static int led_on = 100, led_off; /* defaults to LED that is on while in use */
139
static int pwc_preferred_compression = 1; /* 0..3 = uncompressed..high */
L
Linus Torvalds 已提交
140 141 142 143 144 145 146 147 148
static struct {
	int type;
	char serial_number[30];
	int device_node;
	struct pwc_device *pdev;
} device_hint[MAX_DEV_HINTS];

/***/

149 150
static int pwc_video_open(struct file *file);
static int pwc_video_close(struct file *file);
151
static ssize_t pwc_video_read(struct file *file, char __user *buf,
L
Linus Torvalds 已提交
152 153 154
			  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);
155
static void pwc_video_release(struct video_device *vfd);
L
Linus Torvalds 已提交
156

157
static const struct v4l2_file_operations pwc_fops = {
L
Linus Torvalds 已提交
158 159 160 161 162 163
	.owner =	THIS_MODULE,
	.open =		pwc_video_open,
	.release =     	pwc_video_close,
	.read =		pwc_video_read,
	.poll =		pwc_video_poll,
	.mmap =		pwc_video_mmap,
164
	.unlocked_ioctl = video_ioctl2,
L
Linus Torvalds 已提交
165 166 167
};
static struct video_device pwc_template = {
	.name =		"Philips Webcam",	/* Filled in later */
168
	.release =	pwc_video_release,
L
Linus Torvalds 已提交
169
	.fops =         &pwc_fops,
170
	.ioctl_ops =	&pwc_ioctl_ops,
L
Linus Torvalds 已提交
171 172 173 174 175 176 177 178 179 180
};

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

/* Here we want the physical address of the memory.
 * This is used when initializing the contents of the area.
 */


181 182

static void *pwc_rvmalloc(unsigned long size)
L
Linus Torvalds 已提交
183 184 185 186
{
	void * mem;
	unsigned long adr;

187
	mem=vmalloc_32(size);
188 189 190 191 192 193 194 195 196 197 198
	if (!mem)
		return NULL;

	memset(mem, 0, size); /* Clear the ram out, no junk to the user */
	adr=(unsigned long) mem;
	while (size > 0)
	 {
	   SetPageReserved(vmalloc_to_page((void *)adr));
	   adr  += PAGE_SIZE;
	   size -= PAGE_SIZE;
	 }
L
Linus Torvalds 已提交
199 200 201
	return mem;
}

202
static void pwc_rvfree(void * mem, unsigned long size)
L
Linus Torvalds 已提交
203
{
204
	unsigned long adr;
L
Linus Torvalds 已提交
205

206 207 208 209 210 211 212 213 214 215 216
	if (!mem)
		return;

	adr=(unsigned long) mem;
	while ((long) size > 0)
	 {
	   ClearPageReserved(vmalloc_to_page((void *)adr));
	   adr  += PAGE_SIZE;
	   size -= PAGE_SIZE;
	 }
	vfree(mem);
L
Linus Torvalds 已提交
217 218 219 220 221 222 223
}




static int pwc_allocate_buffers(struct pwc_device *pdev)
{
224
	int i, err;
L
Linus Torvalds 已提交
225 226
	void *kbuf;

227
	PWC_DEBUG_MEMORY(">> pwc_allocate_buffers(pdev = 0x%p)\n", pdev);
L
Linus Torvalds 已提交
228

229
	/* Allocate Isochronuous pipe buffers */
L
Linus Torvalds 已提交
230 231
	for (i = 0; i < MAX_ISO_BUFS; i++) {
		if (pdev->sbuf[i].data == NULL) {
232
			kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
L
Linus Torvalds 已提交
233
			if (kbuf == NULL) {
234
				PWC_ERROR("Failed to allocate iso buffer %d.\n", i);
L
Linus Torvalds 已提交
235 236
				return -ENOMEM;
			}
237
			PWC_DEBUG_MEMORY("Allocated iso buffer at %p.\n", kbuf);
L
Linus Torvalds 已提交
238 239 240 241 242 243
			pdev->sbuf[i].data = kbuf;
		}
	}

	/* Allocate frame buffer structure */
	if (pdev->fbuf == NULL) {
244
		kbuf = kzalloc(default_fbufs * sizeof(struct pwc_frame_buf), GFP_KERNEL);
L
Linus Torvalds 已提交
245
		if (kbuf == NULL) {
246
			PWC_ERROR("Failed to allocate frame buffer structure.\n");
L
Linus Torvalds 已提交
247 248
			return -ENOMEM;
		}
249
		PWC_DEBUG_MEMORY("Allocated frame buffer structure at %p.\n", kbuf);
L
Linus Torvalds 已提交
250 251
		pdev->fbuf = kbuf;
	}
252

L
Linus Torvalds 已提交
253 254 255
	/* create frame buffers, and make circular ring */
	for (i = 0; i < default_fbufs; i++) {
		if (pdev->fbuf[i].data == NULL) {
J
Joe Perches 已提交
256
			kbuf = vzalloc(PWC_FRAME_SIZE); /* need vmalloc since frame buffer > 128K */
L
Linus Torvalds 已提交
257
			if (kbuf == NULL) {
258
				PWC_ERROR("Failed to allocate frame buffer %d.\n", i);
L
Linus Torvalds 已提交
259 260
				return -ENOMEM;
			}
261
			PWC_DEBUG_MEMORY("Allocated frame buffer %d at %p.\n", i, kbuf);
L
Linus Torvalds 已提交
262 263 264
			pdev->fbuf[i].data = kbuf;
		}
	}
265

L
Linus Torvalds 已提交
266
	/* Allocate decompressor table space */
267 268 269 270 271 272 273 274 275
	if (DEVICE_USE_CODEC1(pdev->type))
		err = pwc_dec1_alloc(pdev);
	else
		err = pwc_dec23_alloc(pdev);

	if (err) {
		PWC_ERROR("Failed to allocate decompress table.\n");
		return err;
	}
276

L
Linus Torvalds 已提交
277
	/* Allocate image buffer; double buffer for mmap() */
278
	kbuf = pwc_rvmalloc(pwc_mbufs * pdev->len_per_image);
L
Linus Torvalds 已提交
279
	if (kbuf == NULL) {
280 281
		PWC_ERROR("Failed to allocate image buffer(s). needed (%d)\n",
				pwc_mbufs * pdev->len_per_image);
L
Linus Torvalds 已提交
282 283
		return -ENOMEM;
	}
284
	PWC_DEBUG_MEMORY("Allocated image buffer at %p.\n", kbuf);
L
Linus Torvalds 已提交
285
	pdev->image_data = kbuf;
286 287 288 289 290 291 292
	for (i = 0; i < pwc_mbufs; i++) {
		pdev->images[i].offset = i * pdev->len_per_image;
		pdev->images[i].vma_use_count = 0;
	}
	for (; i < MAX_IMAGES; i++) {
		pdev->images[i].offset = 0;
	}
L
Linus Torvalds 已提交
293 294

	kbuf = NULL;
295

296
	PWC_DEBUG_MEMORY("<< pwc_allocate_buffers()\n");
L
Linus Torvalds 已提交
297 298 299 300 301 302 303
	return 0;
}

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

304
	PWC_DEBUG_MEMORY("Entering free_buffers(%p).\n", pdev);
L
Linus Torvalds 已提交
305 306 307 308

	/* Release Iso-pipe buffers */
	for (i = 0; i < MAX_ISO_BUFS; i++)
		if (pdev->sbuf[i].data != NULL) {
309
			PWC_DEBUG_MEMORY("Freeing ISO buffer at %p.\n", pdev->sbuf[i].data);
L
Linus Torvalds 已提交
310 311 312 313 314 315 316 317
			kfree(pdev->sbuf[i].data);
			pdev->sbuf[i].data = NULL;
		}

	/* The same for frame buffers */
	if (pdev->fbuf != NULL) {
		for (i = 0; i < default_fbufs; i++) {
			if (pdev->fbuf[i].data != NULL) {
318
				PWC_DEBUG_MEMORY("Freeing frame buffer %d at %p.\n", i, pdev->fbuf[i].data);
L
Linus Torvalds 已提交
319 320 321 322 323 324 325 326 327 328
				vfree(pdev->fbuf[i].data);
				pdev->fbuf[i].data = NULL;
			}
		}
		kfree(pdev->fbuf);
		pdev->fbuf = NULL;
	}

	/* Intermediate decompression buffer & tables */
	if (pdev->decompress_data != NULL) {
329
		PWC_DEBUG_MEMORY("Freeing decompression buffer at %p.\n", pdev->decompress_data);
L
Linus Torvalds 已提交
330 331 332 333 334 335
		kfree(pdev->decompress_data);
		pdev->decompress_data = NULL;
	}

	/* Release image buffers */
	if (pdev->image_data != NULL) {
336 337
		PWC_DEBUG_MEMORY("Freeing image buffer at %p.\n", pdev->image_data);
		pwc_rvfree(pdev->image_data, pwc_mbufs * pdev->len_per_image);
L
Linus Torvalds 已提交
338 339
	}
	pdev->image_data = NULL;
340

341
	PWC_DEBUG_MEMORY("Leaving free_buffers().\n");
L
Linus Torvalds 已提交
342 343
}

344
/* The frame & image buffer mess.
L
Linus Torvalds 已提交
345 346 347 348 349

   Yes, this is a mess. Well, it used to be simple, but alas...  In this
   module, 3 buffers schemes are used to get the data from the USB bus to
   the user program. The first scheme involves the ISO buffers (called thus
   since they transport ISO data from the USB controller), and not really
350
   interesting. Suffices to say the data from this buffer is quickly
L
Linus Torvalds 已提交
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
   gathered in an interrupt handler (pwc_isoc_handler) and placed into the
   frame buffer.

   The frame buffer is the second scheme, and is the central element here.
   It collects the data from a single frame from the camera (hence, the
   name). Frames are delimited by the USB camera with a short USB packet,
   so that's easy to detect. The frame buffers form a list that is filled
   by the camera+USB controller and drained by the user process through
   either read() or mmap().

   The image buffer is the third scheme, in which frames are decompressed
   and converted into planar format. For mmap() there is more than
   one image buffer available.

   The frame buffers provide the image buffering. In case the user process
   is a bit slow, this introduces lag and some undesired side-effects.
   The problem arises when the frame buffer is full. I used to drop the last
   frame, which makes the data in the queue stale very quickly. But dropping
   the frame at the head of the queue proved to be a litte bit more difficult.
   I tried a circular linked scheme, but this introduced more problems than
   it solved.

   Because filling and draining are completely asynchronous processes, this
   requires some fiddling with pointers and mutexes.

   Eventually, I came up with a system with 2 lists: an 'empty' frame list
   and a 'full' frame list:
     * Initially, all frame buffers but one are on the 'empty' list; the one
       remaining buffer is our initial fill frame.
380 381
     * If a frame is needed for filling, we try to take it from the 'empty'
       list, unless that list is empty, in which case we take the buffer at
L
Linus Torvalds 已提交
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
       the head of the 'full' list.
     * When our fill buffer has been filled, it is appended to the 'full'
       list.
     * If a frame is needed by read() or mmap(), it is taken from the head of
       the 'full' list, handled, and then appended to the 'empty' list. If no
       buffer is present on the 'full' list, we wait.
   The advantage is that the buffer that is currently being decompressed/
   converted, is on neither list, and thus not in our way (any other scheme
   I tried had the problem of old data lingering in the queue).

   Whatever strategy you choose, it always remains a tradeoff: with more
   frame buffers the chances of a missed frame are reduced. On the other
   hand, on slower machines it introduces lag because the queue will
   always be full.
 */

/**
  \brief Find next frame buffer to fill. Take from empty or full list, whichever comes first.
 */
401
static int pwc_next_fill_frame(struct pwc_device *pdev)
L
Linus Torvalds 已提交
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
{
	int ret;
	unsigned long flags;

	ret = 0;
	spin_lock_irqsave(&pdev->ptrlock, flags);
	if (pdev->fill_frame != NULL) {
		/* append to 'full' list */
		if (pdev->full_frames == NULL) {
			pdev->full_frames = pdev->fill_frame;
			pdev->full_frames_tail = pdev->full_frames;
		}
		else {
			pdev->full_frames_tail->next = pdev->fill_frame;
			pdev->full_frames_tail = pdev->fill_frame;
		}
	}
	if (pdev->empty_frames != NULL) {
		/* We have empty frames available. That's easy */
		pdev->fill_frame = pdev->empty_frames;
		pdev->empty_frames = pdev->empty_frames->next;
	}
	else {
		/* Hmm. Take it from the full list */
		/* sanity check */
		if (pdev->full_frames == NULL) {
428
			PWC_ERROR("Neither empty or full frames available!\n");
L
Linus Torvalds 已提交
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
			spin_unlock_irqrestore(&pdev->ptrlock, flags);
			return -EINVAL;
		}
		pdev->fill_frame = pdev->full_frames;
		pdev->full_frames = pdev->full_frames->next;
		ret = 1;
	}
	pdev->fill_frame->next = NULL;
	spin_unlock_irqrestore(&pdev->ptrlock, flags);
	return ret;
}


/**
  \brief Reset all buffers, pointers and lists, except for the image_used[] buffer.

  If the image_used[] buffer is cleared too, mmap()/VIDIOCSYNC will run into trouble.
 */
static void pwc_reset_buffers(struct pwc_device *pdev)
{
	int i;
	unsigned long flags;

452
	PWC_DEBUG_MEMORY(">> %s __enter__\n", __func__);
453

L
Linus Torvalds 已提交
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
	spin_lock_irqsave(&pdev->ptrlock, flags);
	pdev->full_frames = NULL;
	pdev->full_frames_tail = NULL;
	for (i = 0; i < default_fbufs; i++) {
		pdev->fbuf[i].filled = 0;
		if (i > 0)
			pdev->fbuf[i].next = &pdev->fbuf[i - 1];
		else
			pdev->fbuf->next = NULL;
	}
	pdev->empty_frames = &pdev->fbuf[default_fbufs - 1];
	pdev->empty_frames_tail = pdev->fbuf;
	pdev->read_frame = NULL;
	pdev->fill_frame = pdev->empty_frames;
	pdev->empty_frames = pdev->empty_frames->next;

	pdev->image_read_pos = 0;
	pdev->fill_image = 0;
	spin_unlock_irqrestore(&pdev->ptrlock, flags);
473

474
	PWC_DEBUG_MEMORY("<< %s __leaving__\n", __func__);
L
Linus Torvalds 已提交
475 476 477 478 479 480
}


/**
  \brief Do all the handling for getting one frame: get pointer, decompress, advance pointers.
 */
481
int pwc_handle_frame(struct pwc_device *pdev)
L
Linus Torvalds 已提交
482 483 484 485 486 487 488 489 490
{
	int ret = 0;
	unsigned long flags;

	spin_lock_irqsave(&pdev->ptrlock, flags);
	/* First grab our read_frame; this is removed from all lists, so
	   we can release the lock after this without problems */
	if (pdev->read_frame != NULL) {
		/* This can't theoretically happen */
491 492 493 494 495 496 497 498
		PWC_ERROR("Huh? Read frame still in use?\n");
		spin_unlock_irqrestore(&pdev->ptrlock, flags);
		return ret;
	}


	if (pdev->full_frames == NULL) {
		PWC_ERROR("Woops. No frames ready.\n");
L
Linus Torvalds 已提交
499 500
	}
	else {
501 502 503 504 505 506
		pdev->read_frame = pdev->full_frames;
		pdev->full_frames = pdev->full_frames->next;
		pdev->read_frame->next = NULL;
	}

	if (pdev->read_frame != NULL) {
507
		/* Decompression is a lengthy process, so it's outside of the lock.
508 509 510 511 512 513 514 515 516 517 518
		   This gives the isoc_handler the opportunity to fill more frames
		   in the mean time.
		*/
		spin_unlock_irqrestore(&pdev->ptrlock, flags);
		ret = pwc_decompress(pdev);
		spin_lock_irqsave(&pdev->ptrlock, flags);

		/* We're done with read_buffer, tack it to the end of the empty buffer list */
		if (pdev->empty_frames == NULL) {
			pdev->empty_frames = pdev->read_frame;
			pdev->empty_frames_tail = pdev->empty_frames;
L
Linus Torvalds 已提交
519 520
		}
		else {
521 522
			pdev->empty_frames_tail->next = pdev->read_frame;
			pdev->empty_frames_tail = pdev->read_frame;
L
Linus Torvalds 已提交
523
		}
524
		pdev->read_frame = NULL;
L
Linus Torvalds 已提交
525 526 527 528 529 530 531 532
	}
	spin_unlock_irqrestore(&pdev->ptrlock, flags);
	return ret;
}

/**
  \brief Advance pointers of image buffer (after each user request)
*/
533
void pwc_next_image(struct pwc_device *pdev)
L
Linus Torvalds 已提交
534 535
{
	pdev->image_used[pdev->fill_image] = 0;
536
	pdev->fill_image = (pdev->fill_image + 1) % pwc_mbufs;
L
Linus Torvalds 已提交
537 538
}

539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
/**
 * Print debug information when a frame is discarded because all of our buffer
 * is full
 */
static void pwc_frame_dumped(struct pwc_device *pdev)
{
	pdev->vframes_dumped++;
	if (pdev->vframe_count < FRAME_LOWMARK)
		return;

	if (pdev->vframes_dumped < 20)
		PWC_DEBUG_FLOW("Dumping frame %d\n", pdev->vframe_count);
	else if (pdev->vframes_dumped == 20)
		PWC_DEBUG_FLOW("Dumping frame %d (last message)\n",
				pdev->vframe_count);
}

556 557 558 559 560 561 562 563 564 565 566
static void pwc_snapshot_button(struct pwc_device *pdev, int down)
{
	if (down) {
		PWC_TRACE("Snapshot button pressed.\n");
		pdev->snapshot_button_status = 1;
	} else {
		PWC_TRACE("Snapshot button released.\n");
	}

#ifdef CONFIG_USB_PWC_INPUT_EVDEV
	if (pdev->button_dev) {
567
		input_report_key(pdev->button_dev, KEY_CAMERA, down);
568 569 570 571 572
		input_sync(pdev->button_dev);
	}
#endif
}

573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
static int pwc_rcv_short_packet(struct pwc_device *pdev, const struct pwc_frame_buf *fbuf)
{
	int awake = 0;

	/* 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;
			pdev->vframes_error++;
		}
		if ((ptr[0] ^ pdev->vmirror) & 0x01) {
590
			pwc_snapshot_button(pdev, ptr[0] & 0x01);
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
		}
		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++;
	}
	else if (pdev->type == 740 || pdev->type == 720) {
		unsigned char *ptr = (unsigned char *)fbuf->data;
		if ((ptr[0] ^ pdev->vmirror) & 0x01) {
614
			pwc_snapshot_button(pdev, ptr[0] & 0x01);
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
		}
		pdev->vmirror = ptr[0] & 0x03;
	}

	/* In case we were instructed to drop the frame, do so silently.
	   The buffer pointers are not updated either (but the counters are reset below).
	   */
	if (pdev->drop_frames > 0)
		pdev->drop_frames--;
	else {
		/* Check for underflow first */
		if (fbuf->filled < pdev->frame_total_size) {
			PWC_DEBUG_FLOW("Frame buffer underflow (%d bytes);"
				       " discarded.\n", fbuf->filled);
			pdev->vframes_error++;
		}
		else {
			/* Send only once per EOF */
			awake = 1; /* delay wake_ups */

			/* Find our next frame to fill. This will always succeed, since we
			 * nick a frame from either empty or full list, but if we had to
			 * take it from the full list, it means a frame got dropped.
			 */
			if (pwc_next_fill_frame(pdev))
				pwc_frame_dumped(pdev);

		}
	} /* !drop_frames */
	pdev->vframe_count++;
	return awake;
}
L
Linus Torvalds 已提交
647 648 649 650

/* 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.
 */
651
static void pwc_isoc_handler(struct urb *urb)
L
Linus Torvalds 已提交
652 653 654 655 656 657 658 659 660 661
{
	struct pwc_device *pdev;
	int i, fst, flen;
	int awake;
	struct pwc_frame_buf *fbuf;
	unsigned char *fillptr = NULL, *iso_buf = NULL;

	awake = 0;
	pdev = (struct pwc_device *)urb->context;
	if (pdev == NULL) {
662
		PWC_ERROR("isoc_handler() called with NULL device?!\n");
L
Linus Torvalds 已提交
663 664
		return;
	}
665

L
Linus Torvalds 已提交
666
	if (urb->status == -ENOENT || urb->status == -ECONNRESET) {
667
		PWC_DEBUG_OPEN("URB (%p) unlinked %ssynchronuously.\n", urb, urb->status == -ENOENT ? "" : "a");
L
Linus Torvalds 已提交
668 669 670 671 672 673 674 675 676 677 678 679
		return;
	}
	if (urb->status != -EINPROGRESS && urb->status != 0) {
		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;
680
			case -ETIME:		errmsg = "Device does not respond"; break;
L
Linus Torvalds 已提交
681
		}
682
		PWC_DEBUG_FLOW("pwc_isoc_handler() called with status %d [%s].\n", urb->status, errmsg);
683
		/* Give up after a number of contiguous errors on the USB bus.
L
Linus Torvalds 已提交
684 685 686 687
		   Appearantly something is wrong so we simulate an unplug event.
		 */
		if (++pdev->visoc_errors > MAX_ISOC_ERRORS)
		{
688
			PWC_INFO("Too many ISOC errors, bailing out.\n");
L
Linus Torvalds 已提交
689 690 691 692 693 694 695 696 697
			pdev->error_status = EIO;
			awake = 1;
			wake_up_interruptible(&pdev->frameq);
		}
		goto handler_end; // ugly, but practical
	}

	fbuf = pdev->fill_frame;
	if (fbuf == NULL) {
698
		PWC_ERROR("pwc_isoc_handler without valid fill frame.\n");
L
Linus Torvalds 已提交
699 700 701 702 703 704 705 706 707 708 709
		awake = 1;
		goto handler_end;
	}
	else {
		fillptr = fbuf->data + fbuf->filled;
	}

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

	/* vsync: 0 = don't copy data
710 711
		  1 = sync-hunt
		  2 = synched
L
Linus Torvalds 已提交
712 713 714 715 716 717 718 719 720 721 722 723 724
	 */
	/* 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;
		if (fst == 0) {
			if (flen > 0) { /* if valid data... */
				if (pdev->vsync > 0) { /* ...and we are not sync-hunting... */
					pdev->vsync = 2;

					/* ...copy data to frame buffer, if possible */
					if (flen + fbuf->filled > pdev->frame_total_size) {
725
						PWC_DEBUG_FLOW("Frame buffer overflow (flen = %d, frame_total_size = %d).\n", flen, pdev->frame_total_size);
L
Linus Torvalds 已提交
726 727 728 729 730 731 732 733 734 735 736 737
						pdev->vsync = 0; /* Hmm, let's wait for an EOF (end-of-frame) */
						pdev->vframes_error++;
					}
					else {
						memmove(fillptr, iso_buf, flen);
						fillptr += flen;
					}
				}
				fbuf->filled += flen;
			} /* ..flen > 0 */

			if (flen < pdev->vlast_packet_size) {
738
				/* Shorter packet... We probably have the end of an image-frame;
L
Linus Torvalds 已提交
739 740
				   wake up read() process and let select()/poll() do something.
				   Decompression is done in user time over there.
741
				   */
L
Linus Torvalds 已提交
742
				if (pdev->vsync == 2) {
743 744 745
					if (pwc_rcv_short_packet(pdev, fbuf)) {
						awake = 1;
						fbuf = pdev->fill_frame;
L
Linus Torvalds 已提交
746 747 748 749 750
					}
				}
				fbuf->filled = 0;
				fillptr = fbuf->data;
				pdev->vsync = 1;
751 752
			}

L
Linus Torvalds 已提交
753 754 755
			pdev->vlast_packet_size = flen;
		} /* ..status == 0 */
		else {
756
			/* This is normally not interesting to the user, unless
757 758
			 * you are really debugging something, default = 0 */
			static int iso_error;
L
Linus Torvalds 已提交
759 760
			iso_error++;
			if (iso_error < 20)
761
				PWC_DEBUG_FLOW("Iso frame %d of USB has error %d\n", i, fst);
L
Linus Torvalds 已提交
762 763 764 765 766 767 768 769 770 771
		}
	}

handler_end:
	if (awake)
		wake_up_interruptible(&pdev->frameq);

	urb->dev = pdev->udev;
	i = usb_submit_urb(urb, GFP_ATOMIC);
	if (i != 0)
772
		PWC_ERROR("Error (%d) re-submitting urb in pwc_isoc_handler.\n", i);
L
Linus Torvalds 已提交
773 774 775
}


776
int pwc_isoc_init(struct pwc_device *pdev)
L
Linus Torvalds 已提交
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
{
	struct usb_device *udev;
	struct urb *urb;
	int i, j, ret;
	struct usb_interface *intf;
	struct usb_host_interface *idesc = NULL;

	if (pdev->iso_init)
		return 0;
	pdev->vsync = 0;
	udev = pdev->udev;

	/* 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)
794
		return -EIO;
L
Linus Torvalds 已提交
795 796 797

	/* Search video endpoint */
	pdev->vmax_packet_size = -1;
798
	for (i = 0; i < idesc->desc.bNumEndpoints; i++) {
L
Linus Torvalds 已提交
799 800 801 802
		if ((idesc->endpoint[i].desc.bEndpointAddress & 0xF) == pdev->vendpoint) {
			pdev->vmax_packet_size = le16_to_cpu(idesc->endpoint[i].desc.wMaxPacketSize);
			break;
		}
803
	}
804

L
Linus Torvalds 已提交
805
	if (pdev->vmax_packet_size < 0 || pdev->vmax_packet_size > ISO_MAX_FRAME_SIZE) {
806
		PWC_ERROR("Failed to find packet size for video endpoint in current alternate setting.\n");
807
		return -ENFILE; /* Odd error, that should be noticeable */
L
Linus Torvalds 已提交
808 809 810 811
	}

	/* Set alternate interface */
	ret = 0;
812
	PWC_DEBUG_OPEN("Setting alternate interface %d\n", pdev->valternate);
L
Linus Torvalds 已提交
813 814 815 816 817 818 819
	ret = usb_set_interface(pdev->udev, 0, pdev->valternate);
	if (ret < 0)
		return ret;

	for (i = 0; i < MAX_ISO_BUFS; i++) {
		urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
		if (urb == NULL) {
820
			PWC_ERROR("Failed to allocate urb %d\n", i);
L
Linus Torvalds 已提交
821 822 823 824
			ret = -ENOMEM;
			break;
		}
		pdev->sbuf[i].urb = urb;
825
		PWC_DEBUG_MEMORY("Allocated URB at 0x%p\n", urb);
L
Linus Torvalds 已提交
826 827 828
	}
	if (ret) {
		/* De-allocate in reverse order */
M
Mariusz Kozlowski 已提交
829
		while (i--) {
M
Mariusz Kozlowski 已提交
830
			usb_free_urb(pdev->sbuf[i].urb);
L
Linus Torvalds 已提交
831 832 833 834 835
			pdev->sbuf[i].urb = NULL;
		}
		return ret;
	}

836
	/* init URB structure */
L
Linus Torvalds 已提交
837 838 839 840 841
	for (i = 0; i < MAX_ISO_BUFS; i++) {
		urb = pdev->sbuf[i].urb;

		urb->interval = 1; // devik
		urb->dev = udev;
842
		urb->pipe = usb_rcvisocpipe(udev, pdev->vendpoint);
L
Linus Torvalds 已提交
843
		urb->transfer_flags = URB_ISO_ASAP;
844 845 846 847
		urb->transfer_buffer = pdev->sbuf[i].data;
		urb->transfer_buffer_length = ISO_BUFFER_SIZE;
		urb->complete = pwc_isoc_handler;
		urb->context = pdev;
L
Linus Torvalds 已提交
848 849 850 851 852 853 854 855 856 857 858
		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++) {
		ret = usb_submit_urb(pdev->sbuf[i].urb, GFP_KERNEL);
859
		if (ret) {
860
			PWC_ERROR("isoc_init() submit_urb %d failed with error %d\n", i, ret);
861 862 863 864 865
			pdev->iso_init = 1;
			pwc_isoc_cleanup(pdev);
			return ret;
		}
		PWC_DEBUG_MEMORY("URB 0x%p submitted.\n", pdev->sbuf[i].urb);
L
Linus Torvalds 已提交
866 867 868 869
	}

	/* All is done... */
	pdev->iso_init = 1;
870
	PWC_DEBUG_OPEN("<< pwc_isoc_init()\n");
L
Linus Torvalds 已提交
871 872 873
	return 0;
}

874
static void pwc_iso_stop(struct pwc_device *pdev)
L
Linus Torvalds 已提交
875 876 877 878 879 880 881 882
{
	int i;

	/* Unlinking ISOC buffers one by one */
	for (i = 0; i < MAX_ISO_BUFS; i++) {
		struct urb *urb;

		urb = pdev->sbuf[i].urb;
A
Al Viro 已提交
883
		if (urb) {
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
			PWC_DEBUG_MEMORY("Unlinking URB %p\n", urb);
			usb_kill_urb(urb);
		}
	}
}

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++) {
		struct urb *urb;

		urb = pdev->sbuf[i].urb;
A
Al Viro 已提交
899
		if (urb) {
900
			PWC_DEBUG_MEMORY("Freeing URB\n");
L
Linus Torvalds 已提交
901 902 903 904
			usb_free_urb(urb);
			pdev->sbuf[i].urb = NULL;
		}
	}
905 906 907 908 909
}

void pwc_isoc_cleanup(struct pwc_device *pdev)
{
	PWC_DEBUG_OPEN(">> pwc_isoc_cleanup()\n");
910

911 912 913 914 915
	if (pdev->iso_init == 0)
		return;

	pwc_iso_stop(pdev);
	pwc_iso_free(pdev);
L
Linus Torvalds 已提交
916 917

	/* Stop camera, but only if we are sure the camera is still there (unplug
918
	   is signalled by EPIPE)
L
Linus Torvalds 已提交
919
	 */
920
	if (pdev->error_status != EPIPE) {
921
		PWC_DEBUG_OPEN("Setting alternate interface 0.\n");
L
Linus Torvalds 已提交
922 923 924 925
		usb_set_interface(pdev->udev, 0, 0);
	}

	pdev->iso_init = 0;
926
	PWC_DEBUG_OPEN("<< pwc_isoc_cleanup()\n");
L
Linus Torvalds 已提交
927 928
}

929 930 931
/*********
 * sysfs
 *********/
932
static struct pwc_device *cd_to_pwc(struct device *cd)
933 934 935 936 937
{
	struct video_device *vdev = to_video_device(cd);
	return video_get_drvdata(vdev);
}

938 939
static ssize_t show_pan_tilt(struct device *class_dev,
			     struct device_attribute *attr, char *buf)
940 941 942 943 944
{
	struct pwc_device *pdev = cd_to_pwc(class_dev);
	return sprintf(buf, "%d %d\n", pdev->pan_angle, pdev->tilt_angle);
}

945 946 947
static ssize_t store_pan_tilt(struct device *class_dev,
			      struct device_attribute *attr,
			      const char *buf, size_t count)
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
{
	struct pwc_device *pdev = cd_to_pwc(class_dev);
	int pan, tilt;
	int ret = -EINVAL;

	if (strncmp(buf, "reset", 5) == 0)
		ret = pwc_mpt_reset(pdev, 0x3);

	else if (sscanf(buf, "%d %d", &pan, &tilt) > 0)
		ret = pwc_mpt_set_angle(pdev, pan, tilt);

	if (ret < 0)
		return ret;
	return strlen(buf);
}
963 964
static DEVICE_ATTR(pan_tilt, S_IRUGO | S_IWUSR, show_pan_tilt,
		   store_pan_tilt);
965

966 967
static ssize_t show_snapshot_button_status(struct device *class_dev,
					   struct device_attribute *attr, char *buf)
968 969 970 971 972 973 974
{
	struct pwc_device *pdev = cd_to_pwc(class_dev);
	int status = pdev->snapshot_button_status;
	pdev->snapshot_button_status = 0;
	return sprintf(buf, "%d\n", status);
}

975 976
static DEVICE_ATTR(button, S_IRUGO | S_IWUSR, show_snapshot_button_status,
		   NULL);
977

978
static int pwc_create_sysfs_files(struct pwc_device *pdev)
979
{
980 981
	int rc;

982
	rc = device_create_file(&pdev->vdev.dev, &dev_attr_button);
983 984 985
	if (rc)
		goto err;
	if (pdev->features & FEATURE_MOTOR_PANTILT) {
986
		rc = device_create_file(&pdev->vdev.dev, &dev_attr_pan_tilt);
987 988
		if (rc)
			goto err_button;
989 990 991 992 993
	}

	return 0;

err_button:
994
	device_remove_file(&pdev->vdev.dev, &dev_attr_button);
995
err:
996
	PWC_ERROR("Could not create sysfs files.\n");
997
	return rc;
998 999
}

1000
static void pwc_remove_sysfs_files(struct pwc_device *pdev)
1001 1002
{
	if (pdev->features & FEATURE_MOTOR_PANTILT)
1003 1004
		device_remove_file(&pdev->vdev.dev, &dev_attr_pan_tilt);
	device_remove_file(&pdev->vdev.dev, &dev_attr_button);
1005 1006
}

1007
#ifdef CONFIG_USB_PWC_DEBUG
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
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:
1032
			return "unknown type of sensor";
1033 1034 1035
	}
}
#endif
L
Linus Torvalds 已提交
1036 1037 1038 1039

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

1040
static int pwc_video_open(struct file *file)
L
Linus Torvalds 已提交
1041
{
1042
	int i, ret;
L
Linus Torvalds 已提交
1043 1044 1045
	struct video_device *vdev = video_devdata(file);
	struct pwc_device *pdev;

1046
	PWC_DEBUG_OPEN(">> video_open called(vdev = 0x%p).\n", vdev);
1047

1048
	pdev = video_get_drvdata(vdev);
1049 1050
	if (pdev->vopen) {
		PWC_DEBUG_OPEN("I'm busy, someone is using the device.\n");
L
Linus Torvalds 已提交
1051
		return -EBUSY;
1052
	}
1053

1054
	pwc_construct(pdev); /* set min/max sizes correct */
L
Linus Torvalds 已提交
1055
	if (!pdev->usb_init) {
1056
		PWC_DEBUG_OPEN("Doing first time initialization.\n");
L
Linus Torvalds 已提交
1057
		pdev->usb_init = 1;
1058

1059 1060 1061
		/* Query sensor type */
		ret = pwc_get_cmos_sensor(pdev, &i);
		if (ret >= 0)
L
Linus Torvalds 已提交
1062
		{
1063
			PWC_DEBUG_OPEN("This %s camera is equipped with a %s (%d).\n",
1064
					pdev->vdev.name,
1065
					pwc_sensor_type_to_string(i), i);
L
Linus Torvalds 已提交
1066 1067 1068 1069 1070 1071 1072
		}
	}

	/* Turn on camera */
	if (power_save) {
		i = pwc_camera_power(pdev, 1);
		if (i < 0)
1073
			PWC_DEBUG_OPEN("Failed to restore power to the camera! (%d)\n", i);
L
Linus Torvalds 已提交
1074 1075 1076
	}
	/* Set LED on/off time */
	if (pwc_set_leds(pdev, led_on, led_off) < 0)
1077
		PWC_DEBUG_OPEN("Failed to set LED on/off time.\n");
1078

L
Linus Torvalds 已提交
1079 1080 1081 1082

	/* So far, so good. Allocate memory. */
	i = pwc_allocate_buffers(pdev);
	if (i < 0) {
1083 1084
		PWC_DEBUG_OPEN("Failed to allocate buffers memory.\n");
		pwc_free_buffers(pdev);
L
Linus Torvalds 已提交
1085 1086
		return i;
	}
1087

L
Linus Torvalds 已提交
1088 1089
	/* Reset buffers & parameters */
	pwc_reset_buffers(pdev);
1090
	for (i = 0; i < pwc_mbufs; i++)
L
Linus Torvalds 已提交
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
		pdev->image_used[i] = 0;
	pdev->vframe_count = 0;
	pdev->vframes_dumped = 0;
	pdev->vframes_error = 0;
	pdev->visoc_errors = 0;
	pdev->error_status = 0;
	pwc_construct(pdev); /* set min/max sizes correct */

	/* Set some defaults */
	pdev->vsnapshot = 0;

1102
	/* Set video size, first try the last used video size
L
Linus Torvalds 已提交
1103 1104 1105 1106 1107
	   (or the default one); if that fails try QCIF/10 or QSIF/10;
	   it that fails too, give up.
	 */
	i = pwc_set_video_mode(pdev, pwc_image_sizes[pdev->vsize].x, pwc_image_sizes[pdev->vsize].y, pdev->vframes, pdev->vcompression, 0);
	if (i)	{
1108 1109 1110 1111
		unsigned int default_resolution;
		PWC_DEBUG_OPEN("First attempt at set_video_mode failed.\n");
		if (pdev->type>= 730)
			default_resolution = PSZ_QSIF;
L
Linus Torvalds 已提交
1112
		else
1113 1114 1115 1116 1117 1118 1119 1120
			default_resolution = PSZ_QCIF;

		i = pwc_set_video_mode(pdev,
				       pwc_image_sizes[default_resolution].x,
				       pwc_image_sizes[default_resolution].y,
				       10,
				       pdev->vcompression,
				       0);
L
Linus Torvalds 已提交
1121 1122
	}
	if (i) {
1123 1124
		PWC_DEBUG_OPEN("Second attempt at set_video_mode failed.\n");
		pwc_free_buffers(pdev);
L
Linus Torvalds 已提交
1125 1126
		return i;
	}
1127

1128 1129 1130 1131
	/* Initialize the webcam to sane value */
	pwc_set_brightness(pdev, 0x7fff);
	pwc_set_agc(pdev, 1, 0);

L
Linus Torvalds 已提交
1132 1133
	pdev->vopen++;
	file->private_data = vdev;
1134
	PWC_DEBUG_OPEN("<< video_open() returns 0.\n");
L
Linus Torvalds 已提交
1135 1136 1137
	return 0;
}

1138
static void pwc_video_release(struct video_device *vfd)
1139
{
1140 1141
	struct pwc_device *pdev = container_of(vfd, struct pwc_device, vdev);
	int hint;
1142

1143 1144 1145 1146
	/* search device_hint[] table if we occupy a slot, by any chance */
	for (hint = 0; hint < MAX_DEV_HINTS; hint++)
		if (device_hint[hint].pdev == pdev)
			device_hint[hint].pdev = NULL;
1147 1148

	kfree(pdev);
1149 1150
}

L
Linus Torvalds 已提交
1151
/* Note that all cleanup is done in the reverse order as in _open */
1152
static int pwc_video_close(struct file *file)
L
Linus Torvalds 已提交
1153 1154 1155
{
	struct video_device *vdev = file->private_data;
	struct pwc_device *pdev;
1156
	int i;
L
Linus Torvalds 已提交
1157

1158
	PWC_DEBUG_OPEN(">> video_close called(vdev = 0x%p).\n", vdev);
L
Linus Torvalds 已提交
1159

1160
	pdev = video_get_drvdata(vdev);
L
Linus Torvalds 已提交
1161
	if (pdev->vopen == 0)
1162
		PWC_DEBUG_MODULE("video_close() called on closed device?\n");
L
Linus Torvalds 已提交
1163 1164 1165 1166 1167 1168

	/* Dump statistics, but only if a reasonable amount of frames were
	   processed (to prevent endless log-entries in case of snap-shot
	   programs)
	 */
	if (pdev->vframe_count > 20)
1169
		PWC_DEBUG_MODULE("Closing video device: %d frames received, dumped %d frames, %d frames with errors.\n", pdev->vframe_count, pdev->vframes_dumped, pdev->vframes_error);
L
Linus Torvalds 已提交
1170

1171 1172 1173 1174
	if (DEVICE_USE_CODEC1(pdev->type))
	    pwc_dec1_exit();
	else
	    pwc_dec23_exit();
L
Linus Torvalds 已提交
1175 1176 1177 1178 1179

	pwc_isoc_cleanup(pdev);
	pwc_free_buffers(pdev);

	/* Turn off LEDS and power down camera, but only when not unplugged */
1180
	if (!pdev->unplugged) {
L
Linus Torvalds 已提交
1181 1182
		/* Turn LEDs off */
		if (pwc_set_leds(pdev, 0, 0) < 0)
1183
			PWC_DEBUG_MODULE("Failed to set LED on/off time.\n");
L
Linus Torvalds 已提交
1184 1185 1186
		if (power_save) {
			i = pwc_camera_power(pdev, 0);
			if (i < 0)
1187
				PWC_ERROR("Failed to power down camera (%d)\n", i);
L
Linus Torvalds 已提交
1188
		}
1189
		pdev->vopen--;
1190
		PWC_DEBUG_OPEN("<< video_close() vopen=%d\n", pdev->vopen);
L
Linus Torvalds 已提交
1191
	}
1192

L
Linus Torvalds 已提交
1193 1194 1195 1196 1197 1198
	return 0;
}

/*
 *	FIXME: what about two parallel reads ????
 *      ANSWER: Not supported. You can't open the device more than once,
1199 1200 1201 1202 1203 1204 1205
		despite what the V4L1 interface says. First, I don't see
		the need, second there's no mechanism of alerting the
		2nd/3rd/... process of events like changing image size.
		And I don't see the point of blocking that for the
		2nd/3rd/... process.
		In multi-threaded environments reading parallel from any
		device is tricky anyhow.
L
Linus Torvalds 已提交
1206 1207
 */

1208
static ssize_t pwc_video_read(struct file *file, char __user *buf,
L
Linus Torvalds 已提交
1209 1210 1211 1212 1213 1214
			  size_t count, loff_t *ppos)
{
	struct video_device *vdev = file->private_data;
	struct pwc_device *pdev;
	int noblock = file->f_flags & O_NONBLOCK;
	DECLARE_WAITQUEUE(wait, current);
1215
	int bytes_to_read, rv = 0;
1216
	void *image_buffer_addr;
L
Linus Torvalds 已提交
1217

1218 1219
	PWC_DEBUG_READ("pwc_video_read(vdev=0x%p, buf=%p, count=%zd) called.\n",
			vdev, buf, count);
1220
	pdev = video_get_drvdata(vdev);
1221 1222 1223 1224 1225

	if (pdev->error_status) {
		rv = -pdev->error_status; /* Something happened, report what. */
		goto err_out;
	}
L
Linus Torvalds 已提交
1226

1227 1228 1229 1230 1231
	/* Start the stream (if not already started) */
	rv = pwc_isoc_init(pdev);
	if (rv)
		goto err_out;

L
Linus Torvalds 已提交
1232 1233 1234 1235 1236 1237 1238 1239 1240
	/* In case we're doing partial reads, we don't have to wait for a frame */
	if (pdev->image_read_pos == 0) {
		/* Do wait queueing according to the (doc)book */
		add_wait_queue(&pdev->frameq, &wait);
		while (pdev->full_frames == NULL) {
			/* Check for unplugged/etc. here */
			if (pdev->error_status) {
				remove_wait_queue(&pdev->frameq, &wait);
				set_current_state(TASK_RUNNING);
1241 1242
				rv = -pdev->error_status ;
				goto err_out;
L
Linus Torvalds 已提交
1243
			}
1244 1245 1246
			if (noblock) {
				remove_wait_queue(&pdev->frameq, &wait);
				set_current_state(TASK_RUNNING);
1247 1248
				rv = -EWOULDBLOCK;
				goto err_out;
1249 1250 1251 1252
			}
			if (signal_pending(current)) {
				remove_wait_queue(&pdev->frameq, &wait);
				set_current_state(TASK_RUNNING);
1253 1254
				rv = -ERESTARTSYS;
				goto err_out;
1255
			}
1256
			mutex_unlock(&pdev->modlock);
1257 1258
			schedule();
			set_current_state(TASK_INTERRUPTIBLE);
1259
			mutex_lock(&pdev->modlock);
L
Linus Torvalds 已提交
1260 1261 1262
		}
		remove_wait_queue(&pdev->frameq, &wait);
		set_current_state(TASK_RUNNING);
1263

L
Linus Torvalds 已提交
1264
		/* Decompress and release frame */
1265 1266
		rv = pwc_handle_frame(pdev);
		if (rv)
1267
			goto err_out;
L
Linus Torvalds 已提交
1268 1269
	}

1270
	PWC_DEBUG_READ("Copying data to user space.\n");
1271
	if (pdev->pixfmt != V4L2_PIX_FMT_YUV420)
1272
		bytes_to_read = pdev->frame_size + sizeof(struct pwc_raw_frame);
L
Linus Torvalds 已提交
1273
	else
1274
		bytes_to_read = pdev->view.size;
L
Linus Torvalds 已提交
1275 1276 1277 1278

	/* copy bytes to user space; we allow for partial reads */
	if (count + pdev->image_read_pos > bytes_to_read)
		count = bytes_to_read - pdev->image_read_pos;
1279 1280 1281
	image_buffer_addr = pdev->image_data;
	image_buffer_addr += pdev->images[pdev->fill_image].offset;
	image_buffer_addr += pdev->image_read_pos;
1282 1283 1284 1285
	if (copy_to_user(buf, image_buffer_addr, count)) {
		rv = -EFAULT;
		goto err_out;
	}
L
Linus Torvalds 已提交
1286 1287 1288 1289 1290 1291
	pdev->image_read_pos += count;
	if (pdev->image_read_pos >= bytes_to_read) { /* All data has been read */
		pdev->image_read_pos = 0;
		pwc_next_image(pdev);
	}
	return count;
1292 1293
err_out:
	return rv;
L
Linus Torvalds 已提交
1294 1295 1296 1297 1298 1299
}

static unsigned int pwc_video_poll(struct file *file, poll_table *wait)
{
	struct video_device *vdev = file->private_data;
	struct pwc_device *pdev;
1300
	int ret;
L
Linus Torvalds 已提交
1301

1302
	pdev = video_get_drvdata(vdev);
L
Linus Torvalds 已提交
1303

1304 1305 1306 1307 1308
	/* Start the stream (if not already started) */
	ret = pwc_isoc_init(pdev);
	if (ret)
		return ret;

L
Linus Torvalds 已提交
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
	poll_wait(file, &pdev->frameq, wait);
	if (pdev->error_status)
		return POLLERR;
	if (pdev->full_frames != NULL) /* we have frames waiting */
		return (POLLIN | POLLRDNORM);

	return 0;
}

static int pwc_video_mmap(struct file *file, struct vm_area_struct *vma)
{
	struct video_device *vdev = file->private_data;
	struct pwc_device *pdev;
1322 1323 1324 1325
	unsigned long start;
	unsigned long size;
	unsigned long page, pos = 0;
	int index;
1326

1327
	PWC_DEBUG_MEMORY(">> %s\n", __func__);
1328
	pdev = video_get_drvdata(vdev);
1329 1330
	size = vma->vm_end - vma->vm_start;
	start = vma->vm_start;
1331

1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
	/* Find the idx buffer for this mapping */
	for (index = 0; index < pwc_mbufs; index++) {
		pos = pdev->images[index].offset;
		if ((pos>>PAGE_SHIFT) == vma->vm_pgoff)
			break;
	}
	if (index == MAX_IMAGES)
		return -EINVAL;
	if (index == 0) {
		/*
		 * Special case for v4l1. In v4l1, we map only one big buffer,
		 * but in v4l2 each buffer is mapped
		 */
		unsigned long total_size;
		total_size = pwc_mbufs * pdev->len_per_image;
		if (size != pdev->len_per_image && size != total_size) {
			PWC_ERROR("Wrong size (%lu) needed to be len_per_image=%d or total_size=%lu\n",
				   size, pdev->len_per_image, total_size);
			return -EINVAL;
		}
	} else if (size > pdev->len_per_image)
		return -EINVAL;

	vma->vm_flags |= VM_IO;	/* from 2.6.9-acX */
L
Linus Torvalds 已提交
1356

1357
	pos += (unsigned long)pdev->image_data;
L
Linus Torvalds 已提交
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
	while (size > 0) {
		page = vmalloc_to_pfn((void *)pos);
		if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
			return -EAGAIN;
		start += PAGE_SIZE;
		pos += PAGE_SIZE;
		if (size > PAGE_SIZE)
			size -= PAGE_SIZE;
		else
			size = 0;
	}
	return 0;
}

/***************************************************************************/
/* 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;
1384
	int hint, rc;
L
Linus Torvalds 已提交
1385 1386 1387 1388
	int features = 0;
	int video_nr = -1; /* default: use next available device */
	char serial_number[30], *name;

1389 1390 1391
	vendor_id = le16_to_cpu(udev->descriptor.idVendor);
	product_id = le16_to_cpu(udev->descriptor.idProduct);

L
Linus Torvalds 已提交
1392
	/* Check if we can handle this device */
1393 1394
	PWC_DEBUG_PROBE("probe() called [%04X %04X], if %d\n",
		vendor_id, product_id,
L
Linus Torvalds 已提交
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406
		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:
1407
			PWC_INFO("Philips PCA645VC USB webcam detected.\n");
L
Linus Torvalds 已提交
1408 1409 1410 1411
			name = "Philips 645 webcam";
			type_id = 645;
			break;
		case 0x0303:
1412
			PWC_INFO("Philips PCA646VC USB webcam detected.\n");
L
Linus Torvalds 已提交
1413 1414 1415 1416
			name = "Philips 646 webcam";
			type_id = 646;
			break;
		case 0x0304:
1417
			PWC_INFO("Askey VC010 type 2 USB webcam detected.\n");
L
Linus Torvalds 已提交
1418 1419 1420 1421
			name = "Askey VC010 webcam";
			type_id = 646;
			break;
		case 0x0307:
1422
			PWC_INFO("Philips PCVC675K (Vesta) USB webcam detected.\n");
L
Linus Torvalds 已提交
1423 1424 1425 1426
			name = "Philips 675 webcam";
			type_id = 675;
			break;
		case 0x0308:
1427
			PWC_INFO("Philips PCVC680K (Vesta Pro) USB webcam detected.\n");
L
Linus Torvalds 已提交
1428 1429 1430 1431
			name = "Philips 680 webcam";
			type_id = 680;
			break;
		case 0x030C:
1432
			PWC_INFO("Philips PCVC690K (Vesta Pro Scan) USB webcam detected.\n");
L
Linus Torvalds 已提交
1433 1434 1435 1436
			name = "Philips 690 webcam";
			type_id = 690;
			break;
		case 0x0310:
1437
			PWC_INFO("Philips PCVC730K (ToUCam Fun)/PCVC830 (ToUCam II) USB webcam detected.\n");
L
Linus Torvalds 已提交
1438 1439 1440 1441
			name = "Philips 730 webcam";
			type_id = 730;
			break;
		case 0x0311:
1442
			PWC_INFO("Philips PCVC740K (ToUCam Pro)/PCVC840 (ToUCam II) USB webcam detected.\n");
L
Linus Torvalds 已提交
1443 1444 1445 1446
			name = "Philips 740 webcam";
			type_id = 740;
			break;
		case 0x0312:
1447
			PWC_INFO("Philips PCVC750K (ToUCam Pro Scan) USB webcam detected.\n");
L
Linus Torvalds 已提交
1448 1449 1450 1451
			name = "Philips 750 webcam";
			type_id = 750;
			break;
		case 0x0313:
1452
			PWC_INFO("Philips PCVC720K/40 (ToUCam XS) USB webcam detected.\n");
L
Linus Torvalds 已提交
1453 1454 1455
			name = "Philips 720K/40 webcam";
			type_id = 720;
			break;
1456 1457 1458
		case 0x0329:
			PWC_INFO("Philips SPC 900NC USB webcam detected.\n");
			name = "Philips SPC 900NC webcam";
1459
			type_id = 740;
1460
			break;
L
Linus Torvalds 已提交
1461 1462 1463 1464 1465 1466 1467 1468
		default:
			return -ENODEV;
			break;
		}
	}
	else if (vendor_id == 0x069A) {
		switch(product_id) {
		case 0x0001:
1469
			PWC_INFO("Askey VC010 type 1 USB webcam detected.\n");
L
Linus Torvalds 已提交
1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480
			name = "Askey VC010 webcam";
			type_id = 645;
			break;
		default:
			return -ENODEV;
			break;
		}
	}
	else if (vendor_id == 0x046d) {
		switch(product_id) {
		case 0x08b0:
1481
			PWC_INFO("Logitech QuickCam Pro 3000 USB webcam detected.\n");
L
Linus Torvalds 已提交
1482 1483 1484 1485
			name = "Logitech QuickCam Pro 3000";
			type_id = 740; /* CCD sensor */
			break;
		case 0x08b1:
1486
			PWC_INFO("Logitech QuickCam Notebook Pro USB webcam detected.\n");
L
Linus Torvalds 已提交
1487 1488 1489 1490
			name = "Logitech QuickCam Notebook Pro";
			type_id = 740; /* CCD sensor */
			break;
		case 0x08b2:
1491
			PWC_INFO("Logitech QuickCam 4000 Pro USB webcam detected.\n");
L
Linus Torvalds 已提交
1492 1493 1494 1495
			name = "Logitech QuickCam Pro 4000";
			type_id = 740; /* CCD sensor */
			break;
		case 0x08b3:
1496
			PWC_INFO("Logitech QuickCam Zoom USB webcam detected.\n");
L
Linus Torvalds 已提交
1497 1498 1499 1500
			name = "Logitech QuickCam Zoom";
			type_id = 740; /* CCD sensor */
			break;
		case 0x08B4:
1501
			PWC_INFO("Logitech QuickCam Zoom (new model) USB webcam detected.\n");
L
Linus Torvalds 已提交
1502 1503
			name = "Logitech QuickCam Zoom";
			type_id = 740; /* CCD sensor */
1504
			power_save = 1;
L
Linus Torvalds 已提交
1505 1506
			break;
		case 0x08b5:
1507
			PWC_INFO("Logitech QuickCam Orbit/Sphere USB webcam detected.\n");
L
Linus Torvalds 已提交
1508 1509 1510 1511 1512
			name = "Logitech QuickCam Orbit";
			type_id = 740; /* CCD sensor */
			features |= FEATURE_MOTOR_PANTILT;
			break;
		case 0x08b6:
1513 1514 1515 1516
			PWC_INFO("Logitech/Cisco VT Camera webcam detected.\n");
			name = "Cisco VT Camera";
			type_id = 740; /* CCD sensor */
			break;
L
Linus Torvalds 已提交
1517
		case 0x08b7:
1518 1519 1520 1521 1522
			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? */
1523
			PWC_INFO("Logitech QuickCam detected (reserved ID).\n");
L
Linus Torvalds 已提交
1524 1525 1526
			name = "Logitech QuickCam (res.)";
			type_id = 730; /* Assuming CMOS */
			break;
1527
		default:
L
Linus Torvalds 已提交
1528
			return -ENODEV;
1529 1530 1531
			break;
		}
	}
L
Linus Torvalds 已提交
1532 1533 1534 1535 1536 1537 1538
	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:
1539
			PWC_INFO("Samsung MPC-C10 USB webcam detected.\n");
L
Linus Torvalds 已提交
1540 1541 1542 1543
			name = "Samsung MPC-C10";
			type_id = 675;
			break;
		case 0x9001:
1544
			PWC_INFO("Samsung MPC-C30 USB webcam detected.\n");
L
Linus Torvalds 已提交
1545 1546 1547
			name = "Samsung MPC-C30";
			type_id = 675;
			break;
1548 1549 1550 1551 1552
		case 0x9002:
			PWC_INFO("Samsung SNC-35E (v3.0) USB webcam detected.\n");
			name = "Samsung MPC-C30";
			type_id = 740;
			break;
L
Linus Torvalds 已提交
1553 1554 1555 1556 1557 1558 1559 1560
		default:
			return -ENODEV;
			break;
		}
	}
	else if (vendor_id == 0x041e) {
		switch(product_id) {
		case 0x400c:
1561
			PWC_INFO("Creative Labs Webcam 5 detected.\n");
L
Linus Torvalds 已提交
1562 1563 1564 1565
			name = "Creative Labs Webcam 5";
			type_id = 730;
			break;
		case 0x4011:
1566
			PWC_INFO("Creative Labs Webcam Pro Ex detected.\n");
L
Linus Torvalds 已提交
1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577
			name = "Creative Labs Webcam Pro Ex";
			type_id = 740;
			break;
		default:
			return -ENODEV;
			break;
		}
	}
	else if (vendor_id == 0x04cc) {
		switch(product_id) {
		case 0x8116:
1578
			PWC_INFO("Sotec Afina Eye USB webcam detected.\n");
L
Linus Torvalds 已提交
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590
			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 */
1591
			PWC_INFO("AME Co. Afina Eye USB webcam detected.\n");
L
Linus Torvalds 已提交
1592 1593 1594 1595 1596 1597 1598
			name = "AME Co. Afina Eye";
			type_id = 750;
			break;
		default:
			return -ENODEV;
			break;
		}
1599

L
Linus Torvalds 已提交
1600 1601 1602 1603
	}
	else if (vendor_id == 0x0d81) {
		switch(product_id) {
		case 0x1900:
1604
			PWC_INFO("Visionite VCS-UC300 USB webcam detected.\n");
L
Linus Torvalds 已提交
1605 1606 1607 1608
			name = "Visionite VCS-UC300";
			type_id = 740; /* CCD sensor */
			break;
		case 0x1910:
1609
			PWC_INFO("Visionite VCS-UM100 USB webcam detected.\n");
L
Linus Torvalds 已提交
1610 1611 1612 1613 1614 1615 1616 1617
			name = "Visionite VCS-UM100";
			type_id = 730; /* CMOS sensor */
			break;
		default:
			return -ENODEV;
			break;
		}
	}
1618
	else
L
Linus Torvalds 已提交
1619 1620 1621 1622
		return -ENODEV; /* Not any of the know types; but the list keeps growing. */

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

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

	/* Allocate structure, initialize pointers, mutexes, etc. and link it to the usb_device */
1629
	pdev = kzalloc(sizeof(struct pwc_device), GFP_KERNEL);
L
Linus Torvalds 已提交
1630
	if (pdev == NULL) {
1631
		PWC_ERROR("Oops, could not allocate memory for pwc_device.\n");
L
Linus Torvalds 已提交
1632 1633 1634 1635 1636 1637 1638
		return -ENOMEM;
	}
	pdev->type = type_id;
	pdev->vsize = default_size;
	pdev->vframes = default_fps;
	strcpy(pdev->serial, serial_number);
	pdev->features = features;
1639
	if (vendor_id == 0x046D && product_id == 0x08B5) {
L
Linus Torvalds 已提交
1640
		/* Logitech QuickCam Orbit
1641 1642 1643
		   The ranges have been determined experimentally; they may differ from cam to cam.
		   Also, the exact ranges left-right and up-down are different for my cam
		  */
L
Linus Torvalds 已提交
1644 1645 1646 1647 1648 1649
		pdev->angle_range.pan_min  = -7000;
		pdev->angle_range.pan_max  =  7000;
		pdev->angle_range.tilt_min = -3000;
		pdev->angle_range.tilt_max =  2500;
	}

1650
	mutex_init(&pdev->modlock);
L
Linus Torvalds 已提交
1651 1652 1653 1654 1655 1656
	spin_lock_init(&pdev->ptrlock);

	pdev->udev = udev;
	init_waitqueue_head(&pdev->frameq);
	pdev->vcompression = pwc_preferred_compression;

1657 1658 1659 1660 1661 1662
	/* Init video_device structure */
	memcpy(&pdev->vdev, &pwc_template, sizeof(pwc_template));
	pdev->vdev.parent = &intf->dev;
	pdev->vdev.lock = &pdev->modlock;
	strcpy(pdev->vdev.name, name);
	video_set_drvdata(&pdev->vdev, pdev);
L
Linus Torvalds 已提交
1663 1664

	pdev->release = le16_to_cpu(udev->descriptor.bcdDevice);
1665
	PWC_DEBUG_PROBE("Release: %04x\n", pdev->release);
L
Linus Torvalds 已提交
1666 1667 1668 1669 1670 1671 1672

	/* Now search device_hint[] table for a match, so we can hint a node number. */
	for (hint = 0; hint < MAX_DEV_HINTS; hint++) {
		if (((device_hint[hint].type == -1) || (device_hint[hint].type == pdev->type)) &&
		     (device_hint[hint].pdev == NULL)) {
			/* so far, so good... try serial number */
			if ((device_hint[hint].serial_number[0] == '*') || !strcmp(device_hint[hint].serial_number, serial_number)) {
1673 1674 1675 1676
				/* match! */
				video_nr = device_hint[hint].device_node;
				PWC_DEBUG_PROBE("Found hint, will try to register as /dev/video%d\n", video_nr);
				break;
L
Linus Torvalds 已提交
1677 1678 1679 1680 1681
			}
		}
	}

	/* occupy slot */
1682
	if (hint < MAX_DEV_HINTS)
L
Linus Torvalds 已提交
1683 1684
		device_hint[hint].pdev = pdev;

1685
	PWC_DEBUG_PROBE("probe() function returning struct at 0x%p.\n", pdev);
1686
	usb_set_intfdata(intf, pdev);
1687 1688 1689 1690 1691

	/* Set the leds off */
	pwc_set_leds(pdev, 0, 0);
	pwc_camera_power(pdev, 0);

1692
	rc = video_register_device(&pdev->vdev, VFL_TYPE_GRABBER, video_nr);
1693 1694
	if (rc < 0) {
		PWC_ERROR("Failed to register as video device (%d).\n", rc);
1695
		goto err_free_mem;
1696
	}
1697
	rc = pwc_create_sysfs_files(pdev);
1698 1699 1700
	if (rc)
		goto err_video_unreg;

1701
	PWC_INFO("Registered as %s.\n", video_device_node_name(&pdev->vdev));
1702

1703 1704 1705 1706 1707
#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.");
1708
		rc = -ENOMEM;
1709
		pwc_remove_sysfs_files(pdev);
1710
		goto err_video_unreg;
1711 1712
	}

1713 1714 1715
	usb_make_path(udev, pdev->button_phys, sizeof(pdev->button_phys));
	strlcat(pdev->button_phys, "/input0", sizeof(pdev->button_phys));

1716
	pdev->button_dev->name = "PWC snapshot button";
1717
	pdev->button_dev->phys = pdev->button_phys;
1718 1719 1720
	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);
1721
	pdev->button_dev->keybit[BIT_WORD(KEY_CAMERA)] = BIT_MASK(KEY_CAMERA);
1722 1723 1724 1725 1726

	rc = input_register_device(pdev->button_dev);
	if (rc) {
		input_free_device(pdev->button_dev);
		pdev->button_dev = NULL;
1727
		pwc_remove_sysfs_files(pdev);
1728
		goto err_video_unreg;
1729 1730 1731
	}
#endif

L
Linus Torvalds 已提交
1732
	return 0;
1733

1734
err_video_unreg:
1735 1736
	if (hint < MAX_DEV_HINTS)
		device_hint[hint].pdev = NULL;
1737
	video_unregister_device(&pdev->vdev);
1738 1739
err_free_mem:
	kfree(pdev);
1740
	return rc;
L
Linus Torvalds 已提交
1741 1742
}

1743
/* The user yanked out the cable... */
L
Linus Torvalds 已提交
1744 1745
static void usb_pwc_disconnect(struct usb_interface *intf)
{
1746
	struct pwc_device *pdev  = usb_get_intfdata(intf);
L
Linus Torvalds 已提交
1747

H
Hans Verkuil 已提交
1748
	mutex_lock(&pdev->modlock);
L
Linus Torvalds 已提交
1749
	usb_set_intfdata (intf, NULL);
1750

L
Linus Torvalds 已提交
1751
	/* We got unplugged; this is signalled by an EPIPE error code */
1752 1753
	pdev->error_status = EPIPE;
	pdev->unplugged = 1;
L
Linus Torvalds 已提交
1754 1755 1756 1757

	/* Alert waiting processes */
	wake_up_interruptible(&pdev->frameq);

1758 1759
	/* No need to keep the urbs around after disconnection */
	pwc_isoc_cleanup(pdev);
L
Linus Torvalds 已提交
1760

H
Hans Verkuil 已提交
1761
	mutex_unlock(&pdev->modlock);
1762 1763 1764 1765 1766 1767 1768 1769

	pwc_remove_sysfs_files(pdev);
	video_unregister_device(&pdev->vdev);

#ifdef CONFIG_USB_PWC_INPUT_EVDEV
	if (pdev->button_dev)
		input_unregister_device(pdev->button_dev);
#endif
L
Linus Torvalds 已提交
1770 1771 1772
}


1773 1774
/*
 * Initialization code & module stuff
L
Linus Torvalds 已提交
1775 1776
 */

1777 1778 1779 1780
static char *size;
static int fps;
static int fbufs;
static int mbufs;
L
Linus Torvalds 已提交
1781 1782
static int compression = -1;
static int leds[2] = { -1, -1 };
1783
static unsigned int leds_nargs;
1784
static char *dev_hint[MAX_DEV_HINTS];
1785
static unsigned int dev_hint_nargs;
1786 1787 1788 1789 1790

module_param(size, charp, 0444);
module_param(fps, int, 0444);
module_param(fbufs, int, 0444);
module_param(mbufs, int, 0444);
1791
#ifdef CONFIG_USB_PWC_DEBUG
1792 1793 1794 1795 1796 1797
module_param_named(trace, pwc_trace, int, 0644);
#endif
module_param(power_save, int, 0444);
module_param(compression, int, 0444);
module_param_array(leds, int, &leds_nargs, 0444);
module_param_array(dev_hint, charp, &dev_hint_nargs, 0444);
L
Linus Torvalds 已提交
1798 1799 1800 1801 1802

MODULE_PARM_DESC(size, "Initial image size. One of sqcif, qsif, qcif, sif, cif, vga");
MODULE_PARM_DESC(fps, "Initial frames per second. Varies with model, useful range 5-30");
MODULE_PARM_DESC(fbufs, "Number of internal frame buffers to reserve");
MODULE_PARM_DESC(mbufs, "Number of external (mmap()ed) image buffers");
1803
#ifdef CONFIG_USB_PWC_DEBUG
L
Linus Torvalds 已提交
1804
MODULE_PARM_DESC(trace, "For debugging purposes");
1805
#endif
L
Linus Torvalds 已提交
1806 1807 1808 1809 1810 1811 1812 1813
MODULE_PARM_DESC(power_save, "Turn power save feature in camera on or off");
MODULE_PARM_DESC(compression, "Preferred compression quality. Range 0 (uncompressed) to 3 (high compression)");
MODULE_PARM_DESC(leds, "LED on,off time in milliseconds");
MODULE_PARM_DESC(dev_hint, "Device node hints");

MODULE_DESCRIPTION("Philips & OEM USB webcam driver");
MODULE_AUTHOR("Luc Saillard <luc@saillard.org>");
MODULE_LICENSE("GPL");
1814 1815
MODULE_ALIAS("pwcx");
MODULE_VERSION( PWC_VERSION );
L
Linus Torvalds 已提交
1816 1817 1818 1819 1820 1821

static int __init usb_pwc_init(void)
{
	int i, sz;
	char *sizenames[PSZ_MAX] = { "sqcif", "qsif", "qcif", "sif", "cif", "vga" };

1822 1823 1824 1825
	PWC_INFO("Philips webcam module version " PWC_VERSION " loaded.\n");
	PWC_INFO("Supports Philips PCA645/646, PCVC675/680/690, PCVC720[40]/730/740/750 & PCVC830/840.\n");
	PWC_INFO("Also supports the Askey VC010, various Logitech Quickcams, Samsung MPC-C10 and MPC-C30,\n");
	PWC_INFO("the Creative WebCam 5 & Pro Ex, SOTEC Afina Eye and Visionite VCS-UC300 and VCS-UM100.\n");
L
Linus Torvalds 已提交
1826 1827 1828

	if (fps) {
		if (fps < 4 || fps > 30) {
1829
			PWC_ERROR("Framerate out of bounds (4-30).\n");
L
Linus Torvalds 已提交
1830 1831 1832
			return -EINVAL;
		}
		default_fps = fps;
1833
		PWC_DEBUG_MODULE("Default framerate set to %d.\n", default_fps);
L
Linus Torvalds 已提交
1834 1835
	}

1836
	if (size) {
L
Linus Torvalds 已提交
1837 1838 1839 1840 1841 1842 1843 1844
		/* string; try matching with array */
		for (sz = 0; sz < PSZ_MAX; sz++) {
			if (!strcmp(sizenames[sz], size)) { /* Found! */
				default_size = sz;
				break;
			}
		}
		if (sz == PSZ_MAX) {
1845
			PWC_ERROR("Size not recognized; try size=[sqcif | qsif | qcif | sif | cif | vga].\n");
L
Linus Torvalds 已提交
1846 1847
			return -EINVAL;
		}
1848
		PWC_DEBUG_MODULE("Default image size set to %s [%dx%d].\n", sizenames[default_size], pwc_image_sizes[default_size].x, pwc_image_sizes[default_size].y);
L
Linus Torvalds 已提交
1849 1850 1851
	}
	if (mbufs) {
		if (mbufs < 1 || mbufs > MAX_IMAGES) {
1852
			PWC_ERROR("Illegal number of mmap() buffers; use a number between 1 and %d.\n", MAX_IMAGES);
L
Linus Torvalds 已提交
1853 1854
			return -EINVAL;
		}
1855 1856
		pwc_mbufs = mbufs;
		PWC_DEBUG_MODULE("Number of image buffers set to %d.\n", pwc_mbufs);
L
Linus Torvalds 已提交
1857 1858 1859
	}
	if (fbufs) {
		if (fbufs < 2 || fbufs > MAX_FRAMES) {
1860
			PWC_ERROR("Illegal number of frame buffers; use a number between 2 and %d.\n", MAX_FRAMES);
L
Linus Torvalds 已提交
1861 1862 1863
			return -EINVAL;
		}
		default_fbufs = fbufs;
1864
		PWC_DEBUG_MODULE("Number of frame buffers set to %d.\n", default_fbufs);
L
Linus Torvalds 已提交
1865
	}
1866
#ifdef CONFIG_USB_PWC_DEBUG
1867 1868
	if (pwc_trace >= 0) {
		PWC_DEBUG_MODULE("Trace options: 0x%04x\n", pwc_trace);
L
Linus Torvalds 已提交
1869
	}
1870
#endif
L
Linus Torvalds 已提交
1871 1872
	if (compression >= 0) {
		if (compression > 3) {
1873
			PWC_ERROR("Invalid compression setting; use a number between 0 (uncompressed) and 3 (high).\n");
L
Linus Torvalds 已提交
1874 1875 1876
			return -EINVAL;
		}
		pwc_preferred_compression = compression;
1877
		PWC_DEBUG_MODULE("Preferred compression set to %d.\n", pwc_preferred_compression);
L
Linus Torvalds 已提交
1878 1879
	}
	if (power_save)
1880
		PWC_DEBUG_MODULE("Enabling power save on open/close.\n");
L
Linus Torvalds 已提交
1881 1882 1883 1884 1885
	if (leds[0] >= 0)
		led_on = leds[0];
	if (leds[1] >= 0)
		led_off = leds[1];

1886
	/* Big device node whoopla. Basically, it allows you to assign a
L
Linus Torvalds 已提交
1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910
	   device node (/dev/videoX) to a camera, based on its type
	   & serial number. The format is [type[.serialnumber]:]node.

	   Any camera that isn't matched by these rules gets the next
	   available free device node.
	 */
	for (i = 0; i < MAX_DEV_HINTS; i++) {
		char *s, *colon, *dot;

		/* This loop also initializes the array */
		device_hint[i].pdev = NULL;
		s = dev_hint[i];
		if (s != NULL && *s != '\0') {
			device_hint[i].type = -1; /* wildcard */
			strcpy(device_hint[i].serial_number, "*");

			/* parse string: chop at ':' & '/' */
			colon = dot = s;
			while (*colon != '\0' && *colon != ':')
				colon++;
			while (*dot != '\0' && *dot != '.')
				dot++;
			/* Few sanity checks */
			if (*dot != '\0' && dot > colon) {
1911
				PWC_ERROR("Malformed camera hint: the colon must be after the dot.\n");
L
Linus Torvalds 已提交
1912 1913 1914 1915 1916 1917
				return -EINVAL;
			}

			if (*colon == '\0') {
				/* No colon */
				if (*dot != '\0') {
1918
					PWC_ERROR("Malformed camera hint: no colon + device node given.\n");
L
Linus Torvalds 已提交
1919 1920 1921 1922
					return -EINVAL;
				}
				else {
					/* No type or serial number specified, just a number. */
1923 1924
					device_hint[i].device_node =
						simple_strtol(s, NULL, 10);
L
Linus Torvalds 已提交
1925 1926 1927 1928
				}
			}
			else {
				/* There's a colon, so we have at least a type and a device node */
1929 1930 1931 1932
				device_hint[i].type =
					simple_strtol(s, NULL, 10);
				device_hint[i].device_node =
					simple_strtol(colon + 1, NULL, 10);
L
Linus Torvalds 已提交
1933 1934 1935
				if (*dot != '\0') {
					/* There's a serial number as well */
					int k;
1936

L
Linus Torvalds 已提交
1937 1938 1939 1940 1941 1942 1943 1944 1945
					dot++;
					k = 0;
					while (*dot != ':' && k < 29) {
						device_hint[i].serial_number[k++] = *dot;
						dot++;
					}
					device_hint[i].serial_number[k] = '\0';
				}
			}
1946 1947 1948 1949
			PWC_TRACE("device_hint[%d]:\n", i);
			PWC_TRACE("  type    : %d\n", device_hint[i].type);
			PWC_TRACE("  serial# : %s\n", device_hint[i].serial_number);
			PWC_TRACE("  node    : %d\n", device_hint[i].device_node);
L
Linus Torvalds 已提交
1950 1951 1952 1953 1954
		}
		else
			device_hint[i].type = 0; /* not filled */
	} /* ..for MAX_DEV_HINTS */

1955
	PWC_DEBUG_PROBE("Registering driver at address 0x%p.\n", &pwc_driver);
L
Linus Torvalds 已提交
1956 1957 1958 1959 1960
	return usb_register(&pwc_driver);
}

static void __exit usb_pwc_exit(void)
{
1961
	PWC_DEBUG_MODULE("Deregistering driver.\n");
L
Linus Torvalds 已提交
1962
	usb_deregister(&pwc_driver);
1963
	PWC_INFO("Philips webcam module removed.\n");
L
Linus Torvalds 已提交
1964 1965 1966 1967 1968
}

module_init(usb_pwc_init);
module_exit(usb_pwc_exit);

1969
/* vim: set cino= formatoptions=croql cindent shiftwidth=8 tabstop=8: */