sn9c102_core.c 82.9 KB
Newer Older
L
Linus Torvalds 已提交
1
/***************************************************************************
2
 * V4L2 driver for SN9C1xx PC Camera Controllers                           *
L
Linus Torvalds 已提交
3
 *                                                                         *
4
 * Copyright (C) 2004-2007 by Luca Risolia <luca.risolia@studio.unibo.it>  *
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
 *                                                                         *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.               *
 ***************************************************************************/

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/param.h>
#include <linux/moduleparam.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/delay.h>
#include <linux/compiler.h>
#include <linux/ioctl.h>
#include <linux/poll.h>
#include <linux/stat.h>
#include <linux/mm.h>
#include <linux/vmalloc.h>
#include <linux/page-flags.h>
#include <linux/byteorder/generic.h>
#include <asm/page.h>
#include <asm/uaccess.h>

#include "sn9c102.h"

/*****************************************************************************/

46
#define SN9C102_MODULE_NAME     "V4L2 driver for SN9C1xx PC Camera Controllers"
47 48
#define SN9C102_MODULE_ALIAS    "sn9c1xx"
#define SN9C102_MODULE_AUTHOR   "(C) 2004-2007 Luca Risolia"
49 50
#define SN9C102_AUTHOR_EMAIL    "<luca.risolia@studio.unibo.it>"
#define SN9C102_MODULE_LICENSE  "GPL"
51 52
#define SN9C102_MODULE_VERSION  "1:1.39"
#define SN9C102_MODULE_VERSION_CODE  KERNEL_VERSION(1, 1, 39)
53 54 55

/*****************************************************************************/

L
Linus Torvalds 已提交
56 57 58 59
MODULE_DEVICE_TABLE(usb, sn9c102_id_table);

MODULE_AUTHOR(SN9C102_MODULE_AUTHOR " " SN9C102_AUTHOR_EMAIL);
MODULE_DESCRIPTION(SN9C102_MODULE_NAME);
60
MODULE_ALIAS(SN9C102_MODULE_ALIAS);
L
Linus Torvalds 已提交
61 62 63 64 65 66
MODULE_VERSION(SN9C102_MODULE_VERSION);
MODULE_LICENSE(SN9C102_MODULE_LICENSE);

static short video_nr[] = {[0 ... SN9C102_MAX_DEVICES-1] = -1};
module_param_array(video_nr, short, NULL, 0444);
MODULE_PARM_DESC(video_nr,
67 68 69 70 71 72 73 74 75 76 77 78 79
		 "\n<-1|n[,...]> Specify V4L2 minor mode number."
		 "\n -1 = use next available (default)"
		 "\n  n = use minor number n (integer >= 0)"
		 "\nYou can specify up to "__MODULE_STRING(SN9C102_MAX_DEVICES)
		 " cameras this way."
		 "\nFor example:"
		 "\nvideo_nr=-1,2,-1 would assign minor number 2 to"
		 "\nthe second camera and use auto for the first"
		 "\none and for every other camera."
		 "\n");

static short force_munmap[] = {[0 ... SN9C102_MAX_DEVICES-1] =
			       SN9C102_FORCE_MUNMAP};
L
Linus Torvalds 已提交
80 81
module_param_array(force_munmap, bool, NULL, 0444);
MODULE_PARM_DESC(force_munmap,
82 83 84 85 86 87 88 89 90
		 "\n<0|1[,...]> Force the application to unmap previously"
		 "\nmapped buffer memory before calling any VIDIOC_S_CROP or"
		 "\nVIDIOC_S_FMT ioctl's. Not all the applications support"
		 "\nthis feature. This parameter is specific for each"
		 "\ndetected camera."
		 "\n 0 = do not force memory unmapping"
		 "\n 1 = force memory unmapping (save memory)"
		 "\nDefault value is "__MODULE_STRING(SN9C102_FORCE_MUNMAP)"."
		 "\n");
L
Linus Torvalds 已提交
91

92
static unsigned int frame_timeout[] = {[0 ... SN9C102_MAX_DEVICES-1] =
93
				       SN9C102_FRAME_TIMEOUT};
94 95
module_param_array(frame_timeout, uint, NULL, 0644);
MODULE_PARM_DESC(frame_timeout,
96 97
		 "\n<0|n[,...]> Timeout for a video frame in seconds before"
		 "\nreturning an I/O error; 0 for infinity."
98 99 100
		 "\nThis parameter is specific for each detected camera."
		 "\nDefault value is "__MODULE_STRING(SN9C102_FRAME_TIMEOUT)"."
		 "\n");
101

L
Linus Torvalds 已提交
102 103 104 105
#ifdef SN9C102_DEBUG
static unsigned short debug = SN9C102_DEBUG_LEVEL;
module_param(debug, ushort, 0644);
MODULE_PARM_DESC(debug,
106 107 108 109 110
		 "\n<n> Debugging information level, from 0 to 3:"
		 "\n0 = none (use carefully)"
		 "\n1 = critical errors"
		 "\n2 = significant informations"
		 "\n3 = more verbose messages"
111
		 "\nLevel 3 is useful for testing only."
112 113
		 "\nDefault value is "__MODULE_STRING(SN9C102_DEBUG_LEVEL)"."
		 "\n");
L
Linus Torvalds 已提交
114 115 116 117
#endif

/*****************************************************************************/

118 119 120
static u32
sn9c102_request_buffers(struct sn9c102_device* cam, u32 count,
			enum sn9c102_io_method io)
L
Linus Torvalds 已提交
121
{
122 123
	struct v4l2_pix_format* p = &(cam->sensor.pix_format);
	struct v4l2_rect* r = &(cam->sensor.cropcap.bounds);
124
	size_t imagesize = cam->module_param.force_munmap || io == IO_READ ?
125 126
			   (p->width * p->height * p->priv) / 8 :
			   (r->width * r->height * p->priv) / 8;
L
Linus Torvalds 已提交
127 128 129 130 131 132
	void* buff = NULL;
	u32 i;

	if (count > SN9C102_MAX_FRAMES)
		count = SN9C102_MAX_FRAMES;

133 134 135
	if (cam->bridge == BRIDGE_SN9C105 || cam->bridge == BRIDGE_SN9C120)
		imagesize += 589 + 2; /* length of JPEG header + EOI marker */

L
Linus Torvalds 已提交
136 137
	cam->nbuffers = count;
	while (cam->nbuffers > 0) {
138 139
		if ((buff = vmalloc_32_user(cam->nbuffers *
					    PAGE_ALIGN(imagesize))))
L
Linus Torvalds 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
			break;
		cam->nbuffers--;
	}

	for (i = 0; i < cam->nbuffers; i++) {
		cam->frame[i].bufmem = buff + i*PAGE_ALIGN(imagesize);
		cam->frame[i].buf.index = i;
		cam->frame[i].buf.m.offset = i*PAGE_ALIGN(imagesize);
		cam->frame[i].buf.length = imagesize;
		cam->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
		cam->frame[i].buf.sequence = 0;
		cam->frame[i].buf.field = V4L2_FIELD_NONE;
		cam->frame[i].buf.memory = V4L2_MEMORY_MMAP;
		cam->frame[i].buf.flags = 0;
	}

	return cam->nbuffers;
}


static void sn9c102_release_buffers(struct sn9c102_device* cam)
{
	if (cam->nbuffers) {
163
		vfree(cam->frame[0].bufmem);
L
Linus Torvalds 已提交
164 165
		cam->nbuffers = 0;
	}
166
	cam->frame_current = NULL;
L
Linus Torvalds 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
}


static void sn9c102_empty_framequeues(struct sn9c102_device* cam)
{
	u32 i;

	INIT_LIST_HEAD(&cam->inqueue);
	INIT_LIST_HEAD(&cam->outqueue);

	for (i = 0; i < SN9C102_MAX_FRAMES; i++) {
		cam->frame[i].state = F_UNUSED;
		cam->frame[i].buf.bytesused = 0;
	}
}


184 185 186 187 188 189 190 191 192 193 194 195 196
static void sn9c102_requeue_outqueue(struct sn9c102_device* cam)
{
	struct sn9c102_frame_t *i;

	list_for_each_entry(i, &cam->outqueue, frame) {
		i->state = F_QUEUED;
		list_add(&i->frame, &cam->inqueue);
	}

	INIT_LIST_HEAD(&cam->outqueue);
}


L
Linus Torvalds 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
static void sn9c102_queue_unusedframes(struct sn9c102_device* cam)
{
	unsigned long lock_flags;
	u32 i;

	for (i = 0; i < cam->nbuffers; i++)
		if (cam->frame[i].state == F_UNUSED) {
			cam->frame[i].state = F_QUEUED;
			spin_lock_irqsave(&cam->queue_lock, lock_flags);
			list_add_tail(&cam->frame[i].frame, &cam->inqueue);
			spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
		}
}

/*****************************************************************************/
212 213 214 215 216 217
/*
 * Write a sequence of count value/register pairs.  Returns -1 after the
 * first failed write, or 0 for no errors.
 */
int sn9c102_write_regs(struct sn9c102_device* cam, const u8 valreg[][2],
		       int count)
218 219
{
	struct usb_device* udev = cam->usbdev;
220
	u8* value = cam->control_buffer;  /* Needed for DMA'able memory */
221 222
	int i, res;

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
	for (i = 0; i < count; i++) {
		u8 index = valreg[i][1];

		/*
		 * index is a u8, so it must be <256 and can't be out of range.
		 * If we put in a check anyway, gcc annoys us with a warning
		 * that our check is useless.  People get all uppity when they
		 * see warnings in the kernel compile.
		 */

		*value = valreg[i][0];
		res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
				      0x08, 0x41, index, 0,
				      value, 1, SN9C102_CTRL_TIMEOUT);
		if (res < 0) {
			DBG(3, "Failed to write a register (value 0x%02X, "
			       "index 0x%02X, error %d)", *value, index, res);
			return -1;
		}
242

243
		cam->reg[index] = *value;
244 245 246 247 248 249
	}

	return 0;
}


L
Linus Torvalds 已提交
250 251 252 253 254 255
int sn9c102_write_reg(struct sn9c102_device* cam, u8 value, u16 index)
{
	struct usb_device* udev = cam->usbdev;
	u8* buff = cam->control_buffer;
	int res;

256 257 258
	if (index >= ARRAY_SIZE(cam->reg))
		return -1;

L
Linus Torvalds 已提交
259 260 261
	*buff = value;

	res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
262
			      index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
L
Linus Torvalds 已提交
263 264
	if (res < 0) {
		DBG(3, "Failed to write a register (value 0x%02X, index "
265
		       "0x%02X, error %d)", value, index, res);
L
Linus Torvalds 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
		return -1;
	}

	cam->reg[index] = value;

	return 0;
}


/* NOTE: reading some registers always returns 0 */
static int sn9c102_read_reg(struct sn9c102_device* cam, u16 index)
{
	struct usb_device* udev = cam->usbdev;
	u8* buff = cam->control_buffer;
	int res;

	res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
283
			      index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
L
Linus Torvalds 已提交
284 285
	if (res < 0)
		DBG(3, "Failed to read a register (index 0x%02X, error %d)",
286
		    index, res);
L
Linus Torvalds 已提交
287 288 289 290 291 292 293

	return (res >= 0) ? (int)(*buff) : -1;
}


int sn9c102_pread_reg(struct sn9c102_device* cam, u16 index)
{
294 295
	if (index >= ARRAY_SIZE(cam->reg))
		return -1;
L
Linus Torvalds 已提交
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321

	return cam->reg[index];
}


static int
sn9c102_i2c_wait(struct sn9c102_device* cam, struct sn9c102_sensor* sensor)
{
	int i, r;

	for (i = 1; i <= 5; i++) {
		r = sn9c102_read_reg(cam, 0x08);
		if (r < 0)
			return -EIO;
		if (r & 0x04)
			return 0;
		if (sensor->frequency & SN9C102_I2C_400KHZ)
			udelay(5*16);
		else
			udelay(16*16);
	}
	return -EBUSY;
}


static int
322 323
sn9c102_i2c_detect_read_error(struct sn9c102_device* cam,
			      struct sn9c102_sensor* sensor)
L
Linus Torvalds 已提交
324
{
325 326
	int r , err = 0;

L
Linus Torvalds 已提交
327
	r = sn9c102_read_reg(cam, 0x08);
328 329 330 331 332 333 334 335 336 337 338 339
	if (r < 0)
		err += r;

	if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102) {
		if (!(r & 0x08))
			err += -1;
	} else {
		if (r & 0x08)
			err += -1;
	}

	return err ? -EIO : 0;
L
Linus Torvalds 已提交
340 341 342 343
}


static int
344 345
sn9c102_i2c_detect_write_error(struct sn9c102_device* cam,
			       struct sn9c102_sensor* sensor)
L
Linus Torvalds 已提交
346 347 348 349 350 351 352
{
	int r;
	r = sn9c102_read_reg(cam, 0x08);
	return (r < 0 || (r >= 0 && (r & 0x08))) ? -EIO : 0;
}


353
int
L
Linus Torvalds 已提交
354
sn9c102_i2c_try_raw_read(struct sn9c102_device* cam,
355 356
			 struct sn9c102_sensor* sensor, u8 data0, u8 data1,
			 u8 n, u8 buffer[])
L
Linus Torvalds 已提交
357 358 359 360 361 362 363
{
	struct usb_device* udev = cam->usbdev;
	u8* data = cam->control_buffer;
	int err = 0, res;

	/* Write cycle */
	data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
364
		  ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) | 0x10;
L
Linus Torvalds 已提交
365 366 367 368
	data[1] = data0; /* I2C slave id */
	data[2] = data1; /* address */
	data[7] = 0x10;
	res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
369
			      0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
L
Linus Torvalds 已提交
370 371 372 373 374 375 376
	if (res < 0)
		err += res;

	err += sn9c102_i2c_wait(cam, sensor);

	/* Read cycle - n bytes */
	data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
377 378
		  ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) |
		  (n << 4) | 0x02;
L
Linus Torvalds 已提交
379 380 381
	data[1] = data0;
	data[7] = 0x10;
	res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
382
			      0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
L
Linus Torvalds 已提交
383 384 385 386 387 388 389
	if (res < 0)
		err += res;

	err += sn9c102_i2c_wait(cam, sensor);

	/* The first read byte will be placed in data[4] */
	res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
390
			      0x0a, 0, data, 5, SN9C102_CTRL_TIMEOUT);
L
Linus Torvalds 已提交
391 392 393 394 395 396
	if (res < 0)
		err += res;

	err += sn9c102_i2c_detect_read_error(cam, sensor);

	PDBGG("I2C read: address 0x%02X, first read byte: 0x%02X", data1,
397
	      data[4]);
L
Linus Torvalds 已提交
398 399

	if (err) {
400
		DBG(3, "I2C read failed for %s image sensor", sensor->name);
L
Linus Torvalds 已提交
401 402 403 404 405 406 407 408 409 410
		return -1;
	}

	if (buffer)
		memcpy(buffer, data, sizeof(buffer));

	return (int)data[4];
}


411
int
L
Linus Torvalds 已提交
412
sn9c102_i2c_try_raw_write(struct sn9c102_device* cam,
413 414
			  struct sn9c102_sensor* sensor, u8 n, u8 data0,
			  u8 data1, u8 data2, u8 data3, u8 data4, u8 data5)
L
Linus Torvalds 已提交
415 416 417 418 419 420 421
{
	struct usb_device* udev = cam->usbdev;
	u8* data = cam->control_buffer;
	int err = 0, res;

	/* Write cycle. It usually is address + value */
	data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
422 423
		  ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0)
		  | ((n - 1) << 4);
L
Linus Torvalds 已提交
424 425 426 427 428 429
	data[1] = data0;
	data[2] = data1;
	data[3] = data2;
	data[4] = data3;
	data[5] = data4;
	data[6] = data5;
430
	data[7] = 0x17;
L
Linus Torvalds 已提交
431
	res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
432
			      0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
L
Linus Torvalds 已提交
433 434 435 436 437 438 439
	if (res < 0)
		err += res;

	err += sn9c102_i2c_wait(cam, sensor);
	err += sn9c102_i2c_detect_write_error(cam, sensor);

	if (err)
440
		DBG(3, "I2C write failed for %s image sensor", sensor->name);
L
Linus Torvalds 已提交
441 442 443

	PDBGG("I2C raw write: %u bytes, data0 = 0x%02X, data1 = 0x%02X, "
	      "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X",
444
	      n, data0, data1, data2, data3, data4, data5);
L
Linus Torvalds 已提交
445 446 447 448 449 450 451

	return err ? -1 : 0;
}


int
sn9c102_i2c_try_read(struct sn9c102_device* cam,
452
		     struct sn9c102_sensor* sensor, u8 address)
L
Linus Torvalds 已提交
453 454
{
	return sn9c102_i2c_try_raw_read(cam, sensor, sensor->i2c_slave_id,
455
					address, 1, NULL);
L
Linus Torvalds 已提交
456 457 458
}


459
int
L
Linus Torvalds 已提交
460
sn9c102_i2c_try_write(struct sn9c102_device* cam,
461
		      struct sn9c102_sensor* sensor, u8 address, u8 value)
L
Linus Torvalds 已提交
462
{
463 464 465
	return sn9c102_i2c_try_raw_write(cam, sensor, 3,
					 sensor->i2c_slave_id, address,
					 value, 0, 0, 0);
L
Linus Torvalds 已提交
466 467 468 469 470
}


int sn9c102_i2c_read(struct sn9c102_device* cam, u8 address)
{
471
	return sn9c102_i2c_try_read(cam, &cam->sensor, address);
L
Linus Torvalds 已提交
472 473 474 475 476
}


int sn9c102_i2c_write(struct sn9c102_device* cam, u8 address, u8 value)
{
477
	return sn9c102_i2c_try_write(cam, &cam->sensor, address, value);
L
Linus Torvalds 已提交
478 479 480 481
}

/*****************************************************************************/

482
static size_t sn9c102_sof_length(struct sn9c102_device* cam)
L
Linus Torvalds 已提交
483
{
484 485 486
	switch (cam->bridge) {
	case BRIDGE_SN9C101:
	case BRIDGE_SN9C102:
487
		return 12;
488
	case BRIDGE_SN9C103:
489 490 491 492
		return 18;
	case BRIDGE_SN9C105:
	case BRIDGE_SN9C120:
		return 62;
493
	}
L
Linus Torvalds 已提交
494

495 496 497 498 499 500 501
	return 0;
}


static void*
sn9c102_find_sof_header(struct sn9c102_device* cam, void* mem, size_t len)
{
502
	static const char marker[6] = {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96};
503 504
	const char *m = mem;
	size_t soflen = 0, i, j;
505 506 507

	soflen = sn9c102_sof_length(cam);

508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
	for (i = 0; i < len; i++) {
		size_t b;

		/* Read the variable part of the header */
		if (unlikely(cam->sof.bytesread >= sizeof(marker))) {
			cam->sof.header[cam->sof.bytesread] = *(m+i);
			if (++cam->sof.bytesread == soflen) {
				cam->sof.bytesread = 0;
				return mem + i;
			}
			continue;
		}

		/* Search for the SOF marker (fixed part) in the header */
		for (j = 0, b=cam->sof.bytesread; j+b < sizeof(marker); j++) {
			if (unlikely(i+j) == len)
				return NULL;
			if (*(m+i+j) == marker[cam->sof.bytesread]) {
				cam->sof.header[cam->sof.bytesread] = *(m+i+j);
				if (++cam->sof.bytesread == sizeof(marker)) {
					PDBGG("Bytes to analyze: %zd. SOF "
					      "starts at byte #%zd", len, i);
					i += j+1;
					break;
				}
			} else {
				cam->sof.bytesread = 0;
				break;
L
Linus Torvalds 已提交
536
			}
537 538
		}
	}
L
Linus Torvalds 已提交
539 540 541 542 543 544 545 546

	return NULL;
}


static void*
sn9c102_find_eof_header(struct sn9c102_device* cam, void* mem, size_t len)
{
547
	static const u8 eof_header[4][4] = {
548 549 550 551 552 553
		{0x00, 0x00, 0x00, 0x00},
		{0x40, 0x00, 0x00, 0x00},
		{0x80, 0x00, 0x00, 0x00},
		{0xc0, 0x00, 0x00, 0x00},
	};
	size_t i, j;
L
Linus Torvalds 已提交
554

555
	/* The EOF header does not exist in compressed data */
556 557
	if (cam->sensor.pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X ||
	    cam->sensor.pix_format.pixelformat == V4L2_PIX_FMT_JPEG)
558
		return NULL;
L
Linus Torvalds 已提交
559

560 561 562 563 564
	/*
	   The EOF header might cross the packet boundary, but this is not a
	   problem, since the end of a frame is determined by checking its size
	   in the first place.
	*/
565 566 567
	for (i = 0; (len >= 4) && (i <= len - 4); i++)
		for (j = 0; j < ARRAY_SIZE(eof_header); j++)
			if (!memcmp(mem + i, eof_header[j], 4))
L
Linus Torvalds 已提交
568 569 570 571 572 573
				return mem + i;

	return NULL;
}


574 575 576
static void
sn9c102_write_jpegheader(struct sn9c102_device* cam, struct sn9c102_frame_t* f)
{
577
	static const u8 jpeg_header[589] = {
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 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 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
		0xff, 0xd8, 0xff, 0xdb, 0x00, 0x84, 0x00, 0x06, 0x04, 0x05,
		0x06, 0x05, 0x04, 0x06, 0x06, 0x05, 0x06, 0x07, 0x07, 0x06,
		0x08, 0x0a, 0x10, 0x0a, 0x0a, 0x09, 0x09, 0x0a, 0x14, 0x0e,
		0x0f, 0x0c, 0x10, 0x17, 0x14, 0x18, 0x18, 0x17, 0x14, 0x16,
		0x16, 0x1a, 0x1d, 0x25, 0x1f, 0x1a, 0x1b, 0x23, 0x1c, 0x16,
		0x16, 0x20, 0x2c, 0x20, 0x23, 0x26, 0x27, 0x29, 0x2a, 0x29,
		0x19, 0x1f, 0x2d, 0x30, 0x2d, 0x28, 0x30, 0x25, 0x28, 0x29,
		0x28, 0x01, 0x07, 0x07, 0x07, 0x0a, 0x08, 0x0a, 0x13, 0x0a,
		0x0a, 0x13, 0x28, 0x1a, 0x16, 0x1a, 0x28, 0x28, 0x28, 0x28,
		0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,
		0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,
		0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,
		0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,
		0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0xff, 0xc4, 0x01, 0xa2,
		0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
		0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x01,
		0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
		0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03,
		0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x10, 0x00,
		0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03, 0x05, 0x05, 0x04,
		0x04, 0x00, 0x00, 0x01, 0x7d, 0x01, 0x02, 0x03, 0x00, 0x04,
		0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61,
		0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 0x23,
		0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, 0x24, 0x33, 0x62,
		0x72, 0x82, 0x09, 0x0a, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x25,
		0x26, 0x27, 0x28, 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38,
		0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,
		0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64,
		0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76,
		0x77, 0x78, 0x79, 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88,
		0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
		0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa,
		0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2,
		0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3,
		0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, 0xe3,
		0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf1, 0xf2, 0xf3,
		0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0x11, 0x00, 0x02,
		0x01, 0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05, 0x04, 0x04,
		0x00, 0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11, 0x04,
		0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
		0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xa1, 0xb1,
		0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1,
		0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19,
		0x1a, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
		0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,
		0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64,
		0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76,
		0x77, 0x78, 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
		0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
		0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9,
		0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba,
		0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
		0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe2, 0xe3,
		0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4,
		0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xff, 0xc0, 0x00, 0x11,
		0x08, 0x01, 0xe0, 0x02, 0x80, 0x03, 0x01, 0x21, 0x00, 0x02,
		0x11, 0x01, 0x03, 0x11, 0x01, 0xff, 0xda, 0x00, 0x0c, 0x03,
		0x01, 0x00, 0x02, 0x11, 0x03, 0x11, 0x00, 0x3f, 0x00
	};
	u8 *pos = f->bufmem;

	memcpy(pos, jpeg_header, sizeof(jpeg_header));
	*(pos + 6) = 0x00;
	*(pos + 7 + 64) = 0x01;
	if (cam->compression.quality == 0) {
		memcpy(pos + 7, SN9C102_Y_QTABLE0, 64);
		memcpy(pos + 8 + 64, SN9C102_UV_QTABLE0, 64);
	} else if (cam->compression.quality == 1) {
		memcpy(pos + 7, SN9C102_Y_QTABLE1, 64);
		memcpy(pos + 8 + 64, SN9C102_UV_QTABLE1, 64);
	}
	*(pos + 564) = cam->sensor.pix_format.width & 0xFF;
	*(pos + 563) = (cam->sensor.pix_format.width >> 8) & 0xFF;
	*(pos + 562) = cam->sensor.pix_format.height & 0xFF;
	*(pos + 561) = (cam->sensor.pix_format.height >> 8) & 0xFF;
	*(pos + 567) = 0x21;

	f->buf.bytesused += sizeof(jpeg_header);
}


static void
sn9c102_write_eoimarker(struct sn9c102_device* cam, struct sn9c102_frame_t* f)
{
	static const u8 eoi_marker[2] = {0xff, 0xd9};

	memcpy(f->bufmem + f->buf.bytesused, eoi_marker, sizeof(eoi_marker));
	f->buf.bytesused += sizeof(eoi_marker);
}


670
static void sn9c102_urb_complete(struct urb *urb)
L
Linus Torvalds 已提交
671 672 673
{
	struct sn9c102_device* cam = urb->context;
	struct sn9c102_frame_t** f;
674
	size_t imagesize, soflen;
L
Linus Torvalds 已提交
675 676 677 678 679 680 681 682 683 684 685 686
	u8 i;
	int err = 0;

	if (urb->status == -ENOENT)
		return;

	f = &cam->frame_current;

	if (cam->stream == STREAM_INTERRUPT) {
		cam->stream = STREAM_OFF;
		if ((*f))
			(*f)->state = F_QUEUED;
687
		cam->sof.bytesread = 0;
688
		DBG(3, "Stream interrupted by application");
689
		wake_up(&cam->wait_stream);
L
Linus Torvalds 已提交
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
	}

	if (cam->state & DEV_DISCONNECTED)
		return;

	if (cam->state & DEV_MISCONFIGURED) {
		wake_up_interruptible(&cam->wait_frame);
		return;
	}

	if (cam->stream == STREAM_OFF || list_empty(&cam->inqueue))
		goto resubmit_urb;

	if (!(*f))
		(*f) = list_entry(cam->inqueue.next, struct sn9c102_frame_t,
705
				  frame);
L
Linus Torvalds 已提交
706

707
	imagesize = (cam->sensor.pix_format.width *
708 709
		     cam->sensor.pix_format.height *
		     cam->sensor.pix_format.priv) / 8;
710 711 712
	if (cam->sensor.pix_format.pixelformat == V4L2_PIX_FMT_JPEG)
		imagesize += 589; /* length of jpeg header */
	soflen = sn9c102_sof_length(cam);
713

L
Linus Torvalds 已提交
714 715 716 717 718 719 720 721 722
	for (i = 0; i < urb->number_of_packets; i++) {
		unsigned int img, len, status;
		void *pos, *sof, *eof;

		len = urb->iso_frame_desc[i].actual_length;
		status = urb->iso_frame_desc[i].status;
		pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;

		if (status) {
723
			DBG(3, "Error in isochronous frame");
L
Linus Torvalds 已提交
724
			(*f)->state = F_ERROR;
725
			cam->sof.bytesread = 0;
L
Linus Torvalds 已提交
726 727 728
			continue;
		}

729
		PDBGG("Isochrnous frame: length %u, #%u i", len, i);
L
Linus Torvalds 已提交
730 731 732

redo:
		sof = sn9c102_find_sof_header(cam, pos, len);
733
		if (likely(!sof)) {
L
Linus Torvalds 已提交
734 735 736 737 738 739 740 741
			eof = sn9c102_find_eof_header(cam, pos, len);
			if ((*f)->state == F_GRABBING) {
end_of_frame:
				img = len;

				if (eof)
					img = (eof > pos) ? eof - pos - 1 : 0;

742
				if ((*f)->buf.bytesused + img > imagesize) {
743 744 745
					u32 b;
					b = (*f)->buf.bytesused + img -
					    imagesize;
L
Linus Torvalds 已提交
746
					img = imagesize - (*f)->buf.bytesused;
747 748
					PDBGG("Expected EOF not found: video "
					      "frame cut");
L
Linus Torvalds 已提交
749 750
					if (eof)
						DBG(3, "Exceeded limit: +%u "
751
						       "bytes", (unsigned)(b));
L
Linus Torvalds 已提交
752 753 754 755 756 757 758 759 760 761 762
				}

				memcpy((*f)->bufmem + (*f)->buf.bytesused, pos,
				       img);

				if ((*f)->buf.bytesused == 0)
					do_gettimeofday(&(*f)->buf.timestamp);

				(*f)->buf.bytesused += img;

				if ((*f)->buf.bytesused == imagesize ||
763 764 765 766
				    ((cam->sensor.pix_format.pixelformat ==
				      V4L2_PIX_FMT_SN9C10X ||
				      cam->sensor.pix_format.pixelformat ==
				      V4L2_PIX_FMT_JPEG) && eof)) {
767
					u32 b;
768

769
					b = (*f)->buf.bytesused;
L
Linus Torvalds 已提交
770 771
					(*f)->state = F_DONE;
					(*f)->buf.sequence= ++cam->frame_count;
772

773
					spin_lock(&cam->queue_lock);
L
Linus Torvalds 已提交
774
					list_move_tail(&(*f)->frame,
775
						       &cam->outqueue);
L
Linus Torvalds 已提交
776 777
					if (!list_empty(&cam->inqueue))
						(*f) = list_entry(
778 779 780
							cam->inqueue.next,
							struct sn9c102_frame_t,
							frame );
L
Linus Torvalds 已提交
781 782
					else
						(*f) = NULL;
783
					spin_unlock(&cam->queue_lock);
784

L
Linus Torvalds 已提交
785
					memcpy(cam->sysfs.frame_header,
786
					       cam->sof.header, soflen);
787

788 789
					DBG(3, "Video frame captured: %lu "
					       "bytes", (unsigned long)(b));
L
Linus Torvalds 已提交
790 791 792 793 794 795 796

					if (!(*f))
						goto resubmit_urb;

				} else if (eof) {
					(*f)->state = F_ERROR;
					DBG(3, "Not expected EOF after %lu "
797
					       "bytes of image data",
798 799
					    (unsigned long)
					    ((*f)->buf.bytesused));
L
Linus Torvalds 已提交
800 801 802 803 804 805
				}

				if (sof) /* (1) */
					goto start_of_frame;

			} else if (eof) {
806
				DBG(3, "EOF without SOF");
L
Linus Torvalds 已提交
807 808 809
				continue;

			} else {
810
				PDBGG("Ignoring pointless isochronous frame");
L
Linus Torvalds 已提交
811 812 813 814 815 816 817 818 819
				continue;
			}

		} else if ((*f)->state == F_QUEUED || (*f)->state == F_ERROR) {
start_of_frame:
			(*f)->state = F_GRABBING;
			(*f)->buf.bytesused = 0;
			len -= (sof - pos);
			pos = sof;
820 821 822
			if (cam->sensor.pix_format.pixelformat ==
			    V4L2_PIX_FMT_JPEG)
				sn9c102_write_jpegheader(cam, (*f));
823
			DBG(3, "SOF detected: new video frame");
L
Linus Torvalds 已提交
824 825 826 827 828 829 830 831
			if (len)
				goto redo;

		} else if ((*f)->state == F_GRABBING) {
			eof = sn9c102_find_eof_header(cam, pos, len);
			if (eof && eof < sof)
				goto end_of_frame; /* (1) */
			else {
832
				if (cam->sensor.pix_format.pixelformat ==
833 834 835
				    V4L2_PIX_FMT_SN9C10X ||
				    cam->sensor.pix_format.pixelformat ==
				    V4L2_PIX_FMT_JPEG) {
836 837 838 839 840 841 842
					if (sof - pos >= soflen) {
						eof = sof - soflen;
					} else { /* remove header */
						eof = pos;
						(*f)->buf.bytesused -=
							(soflen - (sof - pos));
					}
L
Linus Torvalds 已提交
843 844 845
					goto end_of_frame;
				} else {
					DBG(3, "SOF before expected EOF after "
846
					       "%lu bytes of image data",
847 848
					    (unsigned long)
					    ((*f)->buf.bytesused));
L
Linus Torvalds 已提交
849 850 851 852 853 854 855 856 857 858 859
					goto start_of_frame;
				}
			}
		}
	}

resubmit_urb:
	urb->dev = cam->usbdev;
	err = usb_submit_urb(urb, GFP_ATOMIC);
	if (err < 0 && err != -EPERM) {
		cam->state |= DEV_MISCONFIGURED;
860
		DBG(1, "usb_submit_urb() failed");
L
Linus Torvalds 已提交
861 862 863 864 865 866 867 868 869 870
	}

	wake_up_interruptible(&cam->wait_frame);
}


static int sn9c102_start_transfer(struct sn9c102_device* cam)
{
	struct usb_device *udev = cam->usbdev;
	struct urb* urb;
871 872 873 874 875
	struct usb_host_interface* altsetting = usb_altnum_to_altsetting(
						    usb_ifnum_to_if(udev, 0),
						    SN9C102_ALTERNATE_SETTING);
	const unsigned int psz = le16_to_cpu(altsetting->
					     endpoint[0].desc.wMaxPacketSize);
L
Linus Torvalds 已提交
876 877 878 879
	s8 i, j;
	int err = 0;

	for (i = 0; i < SN9C102_URBS; i++) {
880
		cam->transfer_buffer[i] = kzalloc(SN9C102_ISO_PACKETS * psz,
881
						  GFP_KERNEL);
L
Linus Torvalds 已提交
882 883
		if (!cam->transfer_buffer[i]) {
			err = -ENOMEM;
884
			DBG(1, "Not enough memory");
L
Linus Torvalds 已提交
885 886 887 888 889 890 891 892 893
			goto free_buffers;
		}
	}

	for (i = 0; i < SN9C102_URBS; i++) {
		urb = usb_alloc_urb(SN9C102_ISO_PACKETS, GFP_KERNEL);
		cam->urb[i] = urb;
		if (!urb) {
			err = -ENOMEM;
894
			DBG(1, "usb_alloc_urb() failed");
L
Linus Torvalds 已提交
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
			goto free_urbs;
		}
		urb->dev = udev;
		urb->context = cam;
		urb->pipe = usb_rcvisocpipe(udev, 1);
		urb->transfer_flags = URB_ISO_ASAP;
		urb->number_of_packets = SN9C102_ISO_PACKETS;
		urb->complete = sn9c102_urb_complete;
		urb->transfer_buffer = cam->transfer_buffer[i];
		urb->transfer_buffer_length = psz * SN9C102_ISO_PACKETS;
		urb->interval = 1;
		for (j = 0; j < SN9C102_ISO_PACKETS; j++) {
			urb->iso_frame_desc[j].offset = psz * j;
			urb->iso_frame_desc[j].length = psz;
		}
	}

	/* Enable video */
	if (!(cam->reg[0x01] & 0x04)) {
		err = sn9c102_write_reg(cam, cam->reg[0x01] | 0x04, 0x01);
		if (err) {
			err = -EIO;
917
			DBG(1, "I/O hardware error");
L
Linus Torvalds 已提交
918 919 920 921 922 923
			goto free_urbs;
		}
	}

	err = usb_set_interface(udev, 0, SN9C102_ALTERNATE_SETTING);
	if (err) {
924
		DBG(1, "usb_set_interface() failed");
L
Linus Torvalds 已提交
925 926 927 928
		goto free_urbs;
	}

	cam->frame_current = NULL;
929
	cam->sof.bytesread = 0;
L
Linus Torvalds 已提交
930 931 932 933 934 935

	for (i = 0; i < SN9C102_URBS; i++) {
		err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
		if (err) {
			for (j = i-1; j >= 0; j--)
				usb_kill_urb(cam->urb[j]);
936
			DBG(1, "usb_submit_urb() failed, error %d", err);
L
Linus Torvalds 已提交
937 938 939 940 941 942 943
			goto free_urbs;
		}
	}

	return 0;

free_urbs:
944
	for (i = 0; (i < SN9C102_URBS) && cam->urb[i]; i++)
L
Linus Torvalds 已提交
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
		usb_free_urb(cam->urb[i]);

free_buffers:
	for (i = 0; (i < SN9C102_URBS) && cam->transfer_buffer[i]; i++)
		kfree(cam->transfer_buffer[i]);

	return err;
}


static int sn9c102_stop_transfer(struct sn9c102_device* cam)
{
	struct usb_device *udev = cam->usbdev;
	s8 i;
	int err = 0;

	if (cam->state & DEV_DISCONNECTED)
		return 0;

	for (i = SN9C102_URBS-1; i >= 0; i--) {
		usb_kill_urb(cam->urb[i]);
		usb_free_urb(cam->urb[i]);
		kfree(cam->transfer_buffer[i]);
	}

	err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */
	if (err)
972
		DBG(3, "usb_set_interface() failed");
L
Linus Torvalds 已提交
973 974 975 976 977

	return err;
}


978
static int sn9c102_stream_interrupt(struct sn9c102_device* cam)
L
Linus Torvalds 已提交
979
{
980
	long timeout;
L
Linus Torvalds 已提交
981 982

	cam->stream = STREAM_INTERRUPT;
983
	timeout = wait_event_timeout(cam->wait_stream,
984 985 986
				     (cam->stream == STREAM_OFF) ||
				     (cam->state & DEV_DISCONNECTED),
				     SN9C102_URB_TIMEOUT);
L
Linus Torvalds 已提交
987 988
	if (cam->state & DEV_DISCONNECTED)
		return -ENODEV;
989
	else if (cam->stream != STREAM_OFF) {
L
Linus Torvalds 已提交
990
		cam->state |= DEV_MISCONFIGURED;
991 992 993 994
		DBG(1, "URB timeout reached. The camera is misconfigured. "
		       "To use it, close and open /dev/video%d again.",
		    cam->v4ldev->minor);
		return -EIO;
L
Linus Torvalds 已提交
995 996 997 998 999 1000 1001
	}

	return 0;
}

/*****************************************************************************/

1002
#ifdef CONFIG_VIDEO_ADV_DEBUG
1003
static u16 sn9c102_strtou16(const char* buff, size_t len, ssize_t* count)
L
Linus Torvalds 已提交
1004
{
1005
	char str[7];
L
Linus Torvalds 已提交
1006 1007 1008
	char* endp;
	unsigned long val;

1009
	if (len < 6) {
L
Linus Torvalds 已提交
1010
		strncpy(str, buff, len);
1011
		str[len] = '\0';
L
Linus Torvalds 已提交
1012
	} else {
1013
		strncpy(str, buff, 6);
1014
		str[6] = '\0';
L
Linus Torvalds 已提交
1015 1016 1017 1018 1019
	}

	val = simple_strtoul(str, &endp, 0);

	*count = 0;
1020
	if (val <= 0xffff)
L
Linus Torvalds 已提交
1021 1022 1023 1024
		*count = (ssize_t)(endp - str);
	if ((*count) && (len == *count+1) && (buff[*count] == '\n'))
		*count += 1;

1025
	return (u16)val;
L
Linus Torvalds 已提交
1026 1027 1028 1029
}

/*
   NOTE 1: being inside one of the following methods implies that the v4l
1030
	   device exists for sure (see kobjects and reference counters)
L
Linus Torvalds 已提交
1031 1032 1033 1034 1035 1036 1037 1038
   NOTE 2: buffers are PAGE_SIZE long
*/

static ssize_t sn9c102_show_reg(struct class_device* cd, char* buf)
{
	struct sn9c102_device* cam;
	ssize_t count;

1039
	if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
L
Linus Torvalds 已提交
1040 1041
		return -ERESTARTSYS;

1042 1043
	cam = video_get_drvdata(container_of(cd, struct video_device,
					     class_dev));
L
Linus Torvalds 已提交
1044
	if (!cam) {
1045
		mutex_unlock(&sn9c102_sysfs_lock);
L
Linus Torvalds 已提交
1046 1047 1048 1049 1050
		return -ENODEV;
	}

	count = sprintf(buf, "%u\n", cam->sysfs.reg);

1051
	mutex_unlock(&sn9c102_sysfs_lock);
L
Linus Torvalds 已提交
1052 1053

	return count;
1054
}
L
Linus Torvalds 已提交
1055 1056


1057
static ssize_t
L
Linus Torvalds 已提交
1058 1059 1060
sn9c102_store_reg(struct class_device* cd, const char* buf, size_t len)
{
	struct sn9c102_device* cam;
1061
	u16 index;
L
Linus Torvalds 已提交
1062 1063
	ssize_t count;

1064
	if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
L
Linus Torvalds 已提交
1065 1066
		return -ERESTARTSYS;

1067 1068
	cam = video_get_drvdata(container_of(cd, struct video_device,
					     class_dev));
L
Linus Torvalds 已提交
1069
	if (!cam) {
1070
		mutex_unlock(&sn9c102_sysfs_lock);
L
Linus Torvalds 已提交
1071 1072 1073
		return -ENODEV;
	}

1074 1075
	index = sn9c102_strtou16(buf, len, &count);
	if (index >= ARRAY_SIZE(cam->reg) || !count) {
1076
		mutex_unlock(&sn9c102_sysfs_lock);
L
Linus Torvalds 已提交
1077 1078 1079 1080 1081
		return -EINVAL;
	}

	cam->sysfs.reg = index;

1082
	DBG(2, "Moved SN9C1XX register index to 0x%02X", cam->sysfs.reg);
1083
	DBG(3, "Written bytes: %zd", count);
L
Linus Torvalds 已提交
1084

1085
	mutex_unlock(&sn9c102_sysfs_lock);
L
Linus Torvalds 已提交
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096

	return count;
}


static ssize_t sn9c102_show_val(struct class_device* cd, char* buf)
{
	struct sn9c102_device* cam;
	ssize_t count;
	int val;

1097
	if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
L
Linus Torvalds 已提交
1098 1099
		return -ERESTARTSYS;

1100 1101
	cam = video_get_drvdata(container_of(cd, struct video_device,
					     class_dev));
L
Linus Torvalds 已提交
1102
	if (!cam) {
1103
		mutex_unlock(&sn9c102_sysfs_lock);
L
Linus Torvalds 已提交
1104 1105 1106 1107
		return -ENODEV;
	}

	if ((val = sn9c102_read_reg(cam, cam->sysfs.reg)) < 0) {
1108
		mutex_unlock(&sn9c102_sysfs_lock);
L
Linus Torvalds 已提交
1109 1110 1111 1112 1113
		return -EIO;
	}

	count = sprintf(buf, "%d\n", val);

1114
	DBG(3, "Read bytes: %zd, value: %d", count, val);
L
Linus Torvalds 已提交
1115

1116
	mutex_unlock(&sn9c102_sysfs_lock);
L
Linus Torvalds 已提交
1117 1118

	return count;
1119
}
L
Linus Torvalds 已提交
1120 1121 1122 1123 1124 1125


static ssize_t
sn9c102_store_val(struct class_device* cd, const char* buf, size_t len)
{
	struct sn9c102_device* cam;
1126
	u16 value;
L
Linus Torvalds 已提交
1127 1128 1129
	ssize_t count;
	int err;

1130
	if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
L
Linus Torvalds 已提交
1131 1132
		return -ERESTARTSYS;

1133 1134
	cam = video_get_drvdata(container_of(cd, struct video_device,
					     class_dev));
L
Linus Torvalds 已提交
1135
	if (!cam) {
1136
		mutex_unlock(&sn9c102_sysfs_lock);
L
Linus Torvalds 已提交
1137 1138 1139
		return -ENODEV;
	}

1140
	value = sn9c102_strtou16(buf, len, &count);
L
Linus Torvalds 已提交
1141
	if (!count) {
1142
		mutex_unlock(&sn9c102_sysfs_lock);
L
Linus Torvalds 已提交
1143 1144 1145 1146 1147
		return -EINVAL;
	}

	err = sn9c102_write_reg(cam, value, cam->sysfs.reg);
	if (err) {
1148
		mutex_unlock(&sn9c102_sysfs_lock);
L
Linus Torvalds 已提交
1149 1150 1151
		return -EIO;
	}

1152
	DBG(2, "Written SN9C1XX reg. 0x%02X, val. 0x%02X",
1153 1154
	    cam->sysfs.reg, value);
	DBG(3, "Written bytes: %zd", count);
L
Linus Torvalds 已提交
1155

1156
	mutex_unlock(&sn9c102_sysfs_lock);
L
Linus Torvalds 已提交
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166

	return count;
}


static ssize_t sn9c102_show_i2c_reg(struct class_device* cd, char* buf)
{
	struct sn9c102_device* cam;
	ssize_t count;

1167
	if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
L
Linus Torvalds 已提交
1168 1169
		return -ERESTARTSYS;

1170 1171
	cam = video_get_drvdata(container_of(cd, struct video_device,
					     class_dev));
L
Linus Torvalds 已提交
1172
	if (!cam) {
1173
		mutex_unlock(&sn9c102_sysfs_lock);
L
Linus Torvalds 已提交
1174 1175 1176 1177 1178
		return -ENODEV;
	}

	count = sprintf(buf, "%u\n", cam->sysfs.i2c_reg);

1179
	DBG(3, "Read bytes: %zd", count);
L
Linus Torvalds 已提交
1180

1181
	mutex_unlock(&sn9c102_sysfs_lock);
L
Linus Torvalds 已提交
1182 1183 1184 1185 1186

	return count;
}


1187
static ssize_t
L
Linus Torvalds 已提交
1188 1189 1190
sn9c102_store_i2c_reg(struct class_device* cd, const char* buf, size_t len)
{
	struct sn9c102_device* cam;
1191
	u16 index;
L
Linus Torvalds 已提交
1192 1193
	ssize_t count;

1194
	if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
L
Linus Torvalds 已提交
1195 1196
		return -ERESTARTSYS;

1197 1198
	cam = video_get_drvdata(container_of(cd, struct video_device,
					     class_dev));
L
Linus Torvalds 已提交
1199
	if (!cam) {
1200
		mutex_unlock(&sn9c102_sysfs_lock);
L
Linus Torvalds 已提交
1201 1202 1203
		return -ENODEV;
	}

1204
	index = sn9c102_strtou16(buf, len, &count);
L
Linus Torvalds 已提交
1205
	if (!count) {
1206
		mutex_unlock(&sn9c102_sysfs_lock);
L
Linus Torvalds 已提交
1207 1208 1209 1210 1211
		return -EINVAL;
	}

	cam->sysfs.i2c_reg = index;

1212 1213
	DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg);
	DBG(3, "Written bytes: %zd", count);
L
Linus Torvalds 已提交
1214

1215
	mutex_unlock(&sn9c102_sysfs_lock);
L
Linus Torvalds 已提交
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226

	return count;
}


static ssize_t sn9c102_show_i2c_val(struct class_device* cd, char* buf)
{
	struct sn9c102_device* cam;
	ssize_t count;
	int val;

1227
	if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
L
Linus Torvalds 已提交
1228 1229
		return -ERESTARTSYS;

1230 1231
	cam = video_get_drvdata(container_of(cd, struct video_device,
					     class_dev));
L
Linus Torvalds 已提交
1232
	if (!cam) {
1233
		mutex_unlock(&sn9c102_sysfs_lock);
L
Linus Torvalds 已提交
1234 1235 1236
		return -ENODEV;
	}

1237
	if (!(cam->sensor.sysfs_ops & SN9C102_I2C_READ)) {
1238
		mutex_unlock(&sn9c102_sysfs_lock);
L
Linus Torvalds 已提交
1239 1240 1241 1242
		return -ENOSYS;
	}

	if ((val = sn9c102_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) {
1243
		mutex_unlock(&sn9c102_sysfs_lock);
L
Linus Torvalds 已提交
1244 1245 1246 1247 1248
		return -EIO;
	}

	count = sprintf(buf, "%d\n", val);

1249
	DBG(3, "Read bytes: %zd, value: %d", count, val);
L
Linus Torvalds 已提交
1250

1251
	mutex_unlock(&sn9c102_sysfs_lock);
L
Linus Torvalds 已提交
1252 1253

	return count;
1254
}
L
Linus Torvalds 已提交
1255 1256 1257 1258 1259 1260


static ssize_t
sn9c102_store_i2c_val(struct class_device* cd, const char* buf, size_t len)
{
	struct sn9c102_device* cam;
1261
	u16 value;
L
Linus Torvalds 已提交
1262 1263 1264
	ssize_t count;
	int err;

1265
	if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
L
Linus Torvalds 已提交
1266 1267
		return -ERESTARTSYS;

1268 1269
	cam = video_get_drvdata(container_of(cd, struct video_device,
					     class_dev));
L
Linus Torvalds 已提交
1270
	if (!cam) {
1271
		mutex_unlock(&sn9c102_sysfs_lock);
L
Linus Torvalds 已提交
1272 1273 1274
		return -ENODEV;
	}

1275
	if (!(cam->sensor.sysfs_ops & SN9C102_I2C_WRITE)) {
1276
		mutex_unlock(&sn9c102_sysfs_lock);
L
Linus Torvalds 已提交
1277 1278 1279
		return -ENOSYS;
	}

1280
	value = sn9c102_strtou16(buf, len, &count);
L
Linus Torvalds 已提交
1281
	if (!count) {
1282
		mutex_unlock(&sn9c102_sysfs_lock);
L
Linus Torvalds 已提交
1283 1284 1285 1286 1287
		return -EINVAL;
	}

	err = sn9c102_i2c_write(cam, cam->sysfs.i2c_reg, value);
	if (err) {
1288
		mutex_unlock(&sn9c102_sysfs_lock);
L
Linus Torvalds 已提交
1289 1290 1291 1292
		return -EIO;
	}

	DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X",
1293 1294
	    cam->sysfs.i2c_reg, value);
	DBG(3, "Written bytes: %zd", count);
L
Linus Torvalds 已提交
1295

1296
	mutex_unlock(&sn9c102_sysfs_lock);
L
Linus Torvalds 已提交
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307

	return count;
}


static ssize_t
sn9c102_store_green(struct class_device* cd, const char* buf, size_t len)
{
	struct sn9c102_device* cam;
	enum sn9c102_bridge bridge;
	ssize_t res = 0;
1308
	u16 value;
L
Linus Torvalds 已提交
1309 1310
	ssize_t count;

1311
	if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
L
Linus Torvalds 已提交
1312 1313
		return -ERESTARTSYS;

1314 1315
	cam = video_get_drvdata(container_of(cd, struct video_device,
					     class_dev));
L
Linus Torvalds 已提交
1316
	if (!cam) {
1317
		mutex_unlock(&sn9c102_sysfs_lock);
L
Linus Torvalds 已提交
1318 1319 1320 1321 1322
		return -ENODEV;
	}

	bridge = cam->bridge;

1323
	mutex_unlock(&sn9c102_sysfs_lock);
L
Linus Torvalds 已提交
1324

1325
	value = sn9c102_strtou16(buf, len, &count);
L
Linus Torvalds 已提交
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
	if (!count)
		return -EINVAL;

	switch (bridge) {
	case BRIDGE_SN9C101:
	case BRIDGE_SN9C102:
		if (value > 0x0f)
			return -EINVAL;
		if ((res = sn9c102_store_reg(cd, "0x11", 4)) >= 0)
			res = sn9c102_store_val(cd, buf, len);
		break;
	case BRIDGE_SN9C103:
1338 1339
	case BRIDGE_SN9C105:
	case BRIDGE_SN9C120:
L
Linus Torvalds 已提交
1340 1341
		if (value > 0x7f)
			return -EINVAL;
1342
		if ((res = sn9c102_store_reg(cd, "0x07", 4)) >= 0)
L
Linus Torvalds 已提交
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
			res = sn9c102_store_val(cd, buf, len);
		break;
	}

	return res;
}


static ssize_t
sn9c102_store_blue(struct class_device* cd, const char* buf, size_t len)
{
	ssize_t res = 0;
1355
	u16 value;
L
Linus Torvalds 已提交
1356 1357
	ssize_t count;

1358
	value = sn9c102_strtou16(buf, len, &count);
L
Linus Torvalds 已提交
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
	if (!count || value > 0x7f)
		return -EINVAL;

	if ((res = sn9c102_store_reg(cd, "0x06", 4)) >= 0)
		res = sn9c102_store_val(cd, buf, len);

	return res;
}


static ssize_t
sn9c102_store_red(struct class_device* cd, const char* buf, size_t len)
{
	ssize_t res = 0;
1373
	u16 value;
L
Linus Torvalds 已提交
1374 1375
	ssize_t count;

1376
	value = sn9c102_strtou16(buf, len, &count);
L
Linus Torvalds 已提交
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
	if (!count || value > 0x7f)
		return -EINVAL;

	if ((res = sn9c102_store_reg(cd, "0x05", 4)) >= 0)
		res = sn9c102_store_val(cd, buf, len);

	return res;
}


static ssize_t sn9c102_show_frame_header(struct class_device* cd, char* buf)
{
	struct sn9c102_device* cam;
	ssize_t count;

1392 1393
	cam = video_get_drvdata(container_of(cd, struct video_device,
					     class_dev));
L
Linus Torvalds 已提交
1394 1395 1396 1397 1398 1399
	if (!cam)
		return -ENODEV;

	count = sizeof(cam->sysfs.frame_header);
	memcpy(buf, cam->sysfs.frame_header, count);

1400
	DBG(3, "Frame header, read bytes: %zd", count);
L
Linus Torvalds 已提交
1401 1402

	return count;
1403
}
L
Linus Torvalds 已提交
1404 1405 1406


static CLASS_DEVICE_ATTR(reg, S_IRUGO | S_IWUSR,
1407
			 sn9c102_show_reg, sn9c102_store_reg);
L
Linus Torvalds 已提交
1408
static CLASS_DEVICE_ATTR(val, S_IRUGO | S_IWUSR,
1409
			 sn9c102_show_val, sn9c102_store_val);
L
Linus Torvalds 已提交
1410
static CLASS_DEVICE_ATTR(i2c_reg, S_IRUGO | S_IWUSR,
1411
			 sn9c102_show_i2c_reg, sn9c102_store_i2c_reg);
L
Linus Torvalds 已提交
1412
static CLASS_DEVICE_ATTR(i2c_val, S_IRUGO | S_IWUSR,
1413
			 sn9c102_show_i2c_val, sn9c102_store_i2c_val);
L
Linus Torvalds 已提交
1414 1415 1416 1417
static CLASS_DEVICE_ATTR(green, S_IWUGO, NULL, sn9c102_store_green);
static CLASS_DEVICE_ATTR(blue, S_IWUGO, NULL, sn9c102_store_blue);
static CLASS_DEVICE_ATTR(red, S_IWUGO, NULL, sn9c102_store_red);
static CLASS_DEVICE_ATTR(frame_header, S_IRUGO,
1418
			 sn9c102_show_frame_header, NULL);
L
Linus Torvalds 已提交
1419 1420


1421
static int sn9c102_create_sysfs(struct sn9c102_device* cam)
L
Linus Torvalds 已提交
1422 1423
{
	struct video_device *v4ldev = cam->v4ldev;
1424
	int err = 0;
1425

1426 1427 1428 1429 1430 1431 1432
	if ((err = video_device_create_file(v4ldev, &class_device_attr_reg)))
		goto err_out;
	if ((err = video_device_create_file(v4ldev, &class_device_attr_val)))
		goto err_reg;
	if ((err = video_device_create_file(v4ldev,
					    &class_device_attr_frame_header)))
		goto err_val;
L
Linus Torvalds 已提交
1433

1434
	if (cam->sensor.sysfs_ops) {
1435 1436 1437 1438 1439 1440
		if ((err = video_device_create_file(v4ldev,
						  &class_device_attr_i2c_reg)))
			goto err_frame_header;
		if ((err = video_device_create_file(v4ldev,
						  &class_device_attr_i2c_val)))
			goto err_i2c_reg;
1441 1442 1443
	}

	if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102) {
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
		if ((err = video_device_create_file(v4ldev,
						    &class_device_attr_green)))
			goto err_i2c_val;
	} else {
		if ((err = video_device_create_file(v4ldev,
						    &class_device_attr_blue)))
			goto err_i2c_val;
		if ((err = video_device_create_file(v4ldev,
						    &class_device_attr_red)))
			goto err_blue;
L
Linus Torvalds 已提交
1454
	}
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465

	return 0;

err_blue:
	video_device_remove_file(v4ldev, &class_device_attr_blue);
err_i2c_val:
	if (cam->sensor.sysfs_ops)
		video_device_remove_file(v4ldev, &class_device_attr_i2c_val);
err_i2c_reg:
	if (cam->sensor.sysfs_ops)
		video_device_remove_file(v4ldev, &class_device_attr_i2c_reg);
1466
err_frame_header:
1467 1468 1469 1470 1471
	video_device_remove_file(v4ldev, &class_device_attr_frame_header);
err_val:
	video_device_remove_file(v4ldev, &class_device_attr_val);
err_reg:
	video_device_remove_file(v4ldev, &class_device_attr_reg);
1472 1473
err_out:
	return err;
L
Linus Torvalds 已提交
1474
}
1475
#endif /* CONFIG_VIDEO_ADV_DEBUG */
L
Linus Torvalds 已提交
1476 1477 1478 1479 1480 1481 1482 1483

/*****************************************************************************/

static int
sn9c102_set_pix_format(struct sn9c102_device* cam, struct v4l2_pix_format* pix)
{
	int err = 0;

1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
	if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X ||
	    pix->pixelformat == V4L2_PIX_FMT_JPEG) {
		switch (cam->bridge) {
		case BRIDGE_SN9C101:
		case BRIDGE_SN9C102:
		case BRIDGE_SN9C103:
			err += sn9c102_write_reg(cam, cam->reg[0x18] | 0x80,
						 0x18);
			break;
		case BRIDGE_SN9C105:
		case BRIDGE_SN9C120:
			err += sn9c102_write_reg(cam, cam->reg[0x18] & 0x7f,
						 0x18);
			break;
		}
	} else {
		switch (cam->bridge) {
		case BRIDGE_SN9C101:
		case BRIDGE_SN9C102:
		case BRIDGE_SN9C103:
			err += sn9c102_write_reg(cam, cam->reg[0x18] & 0x7f,
						 0x18);
			break;
		case BRIDGE_SN9C105:
		case BRIDGE_SN9C120:
			err += sn9c102_write_reg(cam, cam->reg[0x18] | 0x80,
						 0x18);
			break;
		}
	}
L
Linus Torvalds 已提交
1514 1515 1516 1517 1518 1519 1520

	return err ? -EIO : 0;
}


static int
sn9c102_set_compression(struct sn9c102_device* cam,
1521
			struct v4l2_jpegcompression* compression)
L
Linus Torvalds 已提交
1522
{
1523
	int i, err = 0;
L
Linus Torvalds 已提交
1524

1525 1526 1527 1528
	switch (cam->bridge) {
	case BRIDGE_SN9C101:
	case BRIDGE_SN9C102:
	case BRIDGE_SN9C103:
1529
		if (compression->quality == 0)
1530 1531
			err += sn9c102_write_reg(cam, cam->reg[0x17] | 0x01,
						 0x17);
1532
		else if (compression->quality == 1)
1533 1534 1535 1536 1537 1538 1539 1540
			err += sn9c102_write_reg(cam, cam->reg[0x17] & 0xfe,
						 0x17);
		break;
	case BRIDGE_SN9C105:
	case BRIDGE_SN9C120:
		if (compression->quality == 0) {
			for (i = 0; i <= 63; i++) {
				err += sn9c102_write_reg(cam,
1541
							 SN9C102_Y_QTABLE1[i],
1542 1543
							 0x100 + i);
				err += sn9c102_write_reg(cam,
1544
							 SN9C102_UV_QTABLE1[i],
1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562
							 0x140 + i);
			}
			err += sn9c102_write_reg(cam, cam->reg[0x18] & 0xbf,
						 0x18);
		} else if (compression->quality == 1) {
			for (i = 0; i <= 63; i++) {
				err += sn9c102_write_reg(cam,
							 SN9C102_Y_QTABLE1[i],
							 0x100 + i);
				err += sn9c102_write_reg(cam,
							 SN9C102_UV_QTABLE1[i],
							 0x140 + i);
			}
			err += sn9c102_write_reg(cam, cam->reg[0x18] | 0x40,
						 0x18);
		}
		break;
	}
L
Linus Torvalds 已提交
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584

	return err ? -EIO : 0;
}


static int sn9c102_set_scale(struct sn9c102_device* cam, u8 scale)
{
	u8 r = 0;
	int err = 0;

	if (scale == 1)
		r = cam->reg[0x18] & 0xcf;
	else if (scale == 2) {
		r = cam->reg[0x18] & 0xcf;
		r |= 0x10;
	} else if (scale == 4)
		r = cam->reg[0x18] | 0x20;

	err += sn9c102_write_reg(cam, r, 0x18);
	if (err)
		return -EIO;

1585
	PDBGG("Scaling factor: %u", scale);
L
Linus Torvalds 已提交
1586 1587 1588 1589 1590 1591 1592

	return 0;
}


static int sn9c102_set_crop(struct sn9c102_device* cam, struct v4l2_rect* rect)
{
1593
	struct sn9c102_sensor* s = &cam->sensor;
L
Linus Torvalds 已提交
1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607
	u8 h_start = (u8)(rect->left - s->cropcap.bounds.left),
	   v_start = (u8)(rect->top - s->cropcap.bounds.top),
	   h_size = (u8)(rect->width / 16),
	   v_size = (u8)(rect->height / 16);
	int err = 0;

	err += sn9c102_write_reg(cam, h_start, 0x12);
	err += sn9c102_write_reg(cam, v_start, 0x13);
	err += sn9c102_write_reg(cam, h_size, 0x15);
	err += sn9c102_write_reg(cam, v_size, 0x16);
	if (err)
		return -EIO;

	PDBGG("h_start, v_start, h_size, v_size, ho_size, vo_size "
1608
	      "%u %u %u %u", h_start, v_start, h_size, v_size);
L
Linus Torvalds 已提交
1609 1610 1611 1612 1613 1614 1615

	return 0;
}


static int sn9c102_init(struct sn9c102_device* cam)
{
1616
	struct sn9c102_sensor* s = &cam->sensor;
L
Linus Torvalds 已提交
1617 1618 1619
	struct v4l2_control ctrl;
	struct v4l2_queryctrl *qctrl;
	struct v4l2_rect* rect;
1620
	u8 i = 0;
L
Linus Torvalds 已提交
1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639
	int err = 0;

	if (!(cam->state & DEV_INITIALIZED)) {
		init_waitqueue_head(&cam->open);
		qctrl = s->qctrl;
		rect = &(s->cropcap.defrect);
	} else { /* use current values */
		qctrl = s->_qctrl;
		rect = &(s->_rect);
	}

	err += sn9c102_set_scale(cam, rect->width / s->pix_format.width);
	err += sn9c102_set_crop(cam, rect);
	if (err)
		return err;

	if (s->init) {
		err = s->init(cam);
		if (err) {
1640
			DBG(3, "Sensor initialization failed");
L
Linus Torvalds 已提交
1641 1642 1643 1644 1645
			return err;
		}
	}

	if (!(cam->state & DEV_INITIALIZED))
1646 1647 1648
		if (cam->bridge == BRIDGE_SN9C101 ||
		    cam->bridge == BRIDGE_SN9C102 ||
		    cam->bridge == BRIDGE_SN9C103) {
1649 1650
			if (s->pix_format.pixelformat == V4L2_PIX_FMT_JPEG)
				s->pix_format.pixelformat= V4L2_PIX_FMT_SBGGR8;
1651 1652 1653
			cam->compression.quality =  cam->reg[0x17] & 0x01 ?
						    0 : 1;
		} else {
1654 1655
			if (s->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X)
				s->pix_format.pixelformat = V4L2_PIX_FMT_JPEG;
1656 1657 1658 1659
			cam->compression.quality =  cam->reg[0x18] & 0x40 ?
						    0 : 1;
			err += sn9c102_set_compression(cam, &cam->compression);
		}
L
Linus Torvalds 已提交
1660 1661 1662 1663 1664 1665 1666 1667
	else
		err += sn9c102_set_compression(cam, &cam->compression);
	err += sn9c102_set_pix_format(cam, &s->pix_format);
	if (s->set_pix_format)
		err += s->set_pix_format(cam, &s->pix_format);
	if (err)
		return err;

1668 1669
	if (s->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X ||
	    s->pix_format.pixelformat == V4L2_PIX_FMT_JPEG)
1670
		DBG(3, "Compressed video format is active, quality %d",
1671
		    cam->compression.quality);
L
Linus Torvalds 已提交
1672
	else
1673
		DBG(3, "Uncompressed video format is active");
L
Linus Torvalds 已提交
1674 1675 1676

	if (s->set_crop)
		if ((err = s->set_crop(cam, rect))) {
1677
			DBG(3, "set_crop() failed");
L
Linus Torvalds 已提交
1678 1679 1680 1681
			return err;
		}

	if (s->set_ctrl) {
1682 1683
		for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
			if (s->qctrl[i].id != 0 &&
L
Linus Torvalds 已提交
1684 1685 1686 1687 1688 1689
			    !(s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)) {
				ctrl.id = s->qctrl[i].id;
				ctrl.value = qctrl[i].default_value;
				err = s->set_ctrl(cam, &ctrl);
				if (err) {
					DBG(3, "Set %s control failed",
1690
					    s->qctrl[i].name);
L
Linus Torvalds 已提交
1691 1692 1693
					return err;
				}
				DBG(3, "Image sensor supports '%s' control",
1694
				    s->qctrl[i].name);
L
Linus Torvalds 已提交
1695 1696 1697 1698
			}
	}

	if (!(cam->state & DEV_INITIALIZED)) {
1699
		mutex_init(&cam->fileop_mutex);
L
Linus Torvalds 已提交
1700 1701 1702 1703 1704
		spin_lock_init(&cam->queue_lock);
		init_waitqueue_head(&cam->wait_frame);
		init_waitqueue_head(&cam->wait_stream);
		cam->nreadbuffers = 2;
		memcpy(s->_qctrl, s->qctrl, sizeof(s->qctrl));
1705
		memcpy(&(s->_rect), &(s->cropcap.defrect),
L
Linus Torvalds 已提交
1706 1707 1708 1709
		       sizeof(struct v4l2_rect));
		cam->state |= DEV_INITIALIZED;
	}

1710
	DBG(2, "Initialization succeeded");
L
Linus Torvalds 已提交
1711 1712 1713 1714 1715 1716
	return 0;
}


static void sn9c102_release_resources(struct sn9c102_device* cam)
{
1717
	mutex_lock(&sn9c102_sysfs_lock);
L
Linus Torvalds 已提交
1718

1719
	DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor);
L
Linus Torvalds 已提交
1720 1721 1722
	video_set_drvdata(cam->v4ldev, NULL);
	video_unregister_device(cam->v4ldev);

1723
	mutex_unlock(&sn9c102_sysfs_lock);
L
Linus Torvalds 已提交
1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743

	kfree(cam->control_buffer);
}

/*****************************************************************************/

static int sn9c102_open(struct inode* inode, struct file* filp)
{
	struct sn9c102_device* cam;
	int err = 0;

	/*
	   This is the only safe way to prevent race conditions with
	   disconnect
	*/
	if (!down_read_trylock(&sn9c102_disconnect))
		return -ERESTARTSYS;

	cam = video_get_drvdata(video_devdata(filp));

1744
	if (mutex_lock_interruptible(&cam->dev_mutex)) {
L
Linus Torvalds 已提交
1745 1746 1747 1748 1749
		up_read(&sn9c102_disconnect);
		return -ERESTARTSYS;
	}

	if (cam->users) {
1750
		DBG(2, "Device /dev/video%d is busy...", cam->v4ldev->minor);
1751
		DBG(3, "Simultaneous opens are not supported");
L
Linus Torvalds 已提交
1752 1753 1754 1755 1756
		if ((filp->f_flags & O_NONBLOCK) ||
		    (filp->f_flags & O_NDELAY)) {
			err = -EWOULDBLOCK;
			goto out;
		}
1757
		mutex_unlock(&cam->dev_mutex);
L
Linus Torvalds 已提交
1758
		err = wait_event_interruptible_exclusive(cam->open,
1759 1760
						  cam->state & DEV_DISCONNECTED
							 || !cam->users);
L
Linus Torvalds 已提交
1761 1762 1763 1764 1765 1766 1767 1768
		if (err) {
			up_read(&sn9c102_disconnect);
			return err;
		}
		if (cam->state & DEV_DISCONNECTED) {
			up_read(&sn9c102_disconnect);
			return -ENODEV;
		}
1769
		mutex_lock(&cam->dev_mutex);
L
Linus Torvalds 已提交
1770 1771 1772 1773 1774 1775 1776
	}


	if (cam->state & DEV_MISCONFIGURED) {
		err = sn9c102_init(cam);
		if (err) {
			DBG(1, "Initialization failed again. "
1777
			       "I will retry on next open().");
L
Linus Torvalds 已提交
1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793
			goto out;
		}
		cam->state &= ~DEV_MISCONFIGURED;
	}

	if ((err = sn9c102_start_transfer(cam)))
		goto out;

	filp->private_data = cam;
	cam->users++;
	cam->io = IO_NONE;
	cam->stream = STREAM_OFF;
	cam->nbuffers = 0;
	cam->frame_count = 0;
	sn9c102_empty_framequeues(cam);

1794
	DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor);
L
Linus Torvalds 已提交
1795 1796

out:
1797
	mutex_unlock(&cam->dev_mutex);
L
Linus Torvalds 已提交
1798 1799 1800 1801 1802 1803 1804 1805 1806
	up_read(&sn9c102_disconnect);
	return err;
}


static int sn9c102_release(struct inode* inode, struct file* filp)
{
	struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));

1807
	mutex_lock(&cam->dev_mutex); /* prevent disconnect() to be called */
L
Linus Torvalds 已提交
1808 1809 1810 1811 1812 1813 1814

	sn9c102_stop_transfer(cam);

	sn9c102_release_buffers(cam);

	if (cam->state & DEV_DISCONNECTED) {
		sn9c102_release_resources(cam);
1815
		usb_put_dev(cam->usbdev);
1816
		mutex_unlock(&cam->dev_mutex);
L
Linus Torvalds 已提交
1817 1818 1819 1820 1821 1822 1823
		kfree(cam);
		return 0;
	}

	cam->users--;
	wake_up_interruptible_nr(&cam->open, 1);

1824
	DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor);
L
Linus Torvalds 已提交
1825

1826
	mutex_unlock(&cam->dev_mutex);
L
Linus Torvalds 已提交
1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837

	return 0;
}


static ssize_t
sn9c102_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
{
	struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
	struct sn9c102_frame_t* f, * i;
	unsigned long lock_flags;
1838
	long timeout;
L
Linus Torvalds 已提交
1839 1840
	int err = 0;

1841
	if (mutex_lock_interruptible(&cam->fileop_mutex))
L
Linus Torvalds 已提交
1842 1843 1844
		return -ERESTARTSYS;

	if (cam->state & DEV_DISCONNECTED) {
1845
		DBG(1, "Device not present");
1846
		mutex_unlock(&cam->fileop_mutex);
L
Linus Torvalds 已提交
1847 1848 1849 1850
		return -ENODEV;
	}

	if (cam->state & DEV_MISCONFIGURED) {
1851 1852
		DBG(1, "The camera is misconfigured. Close and open it "
		       "again.");
1853
		mutex_unlock(&cam->fileop_mutex);
L
Linus Torvalds 已提交
1854 1855 1856 1857 1858
		return -EIO;
	}

	if (cam->io == IO_MMAP) {
		DBG(3, "Close and open the device again to choose "
1859
		       "the read method");
1860
		mutex_unlock(&cam->fileop_mutex);
1861
		return -EBUSY;
L
Linus Torvalds 已提交
1862 1863 1864 1865
	}

	if (cam->io == IO_NONE) {
		if (!sn9c102_request_buffers(cam,cam->nreadbuffers, IO_READ)) {
1866
			DBG(1, "read() failed, not enough memory");
1867
			mutex_unlock(&cam->fileop_mutex);
L
Linus Torvalds 已提交
1868 1869 1870 1871
			return -ENOMEM;
		}
		cam->io = IO_READ;
		cam->stream = STREAM_ON;
1872 1873 1874 1875 1876
	}

	if (list_empty(&cam->inqueue)) {
		if (!list_empty(&cam->outqueue))
			sn9c102_empty_framequeues(cam);
L
Linus Torvalds 已提交
1877 1878 1879 1880
		sn9c102_queue_unusedframes(cam);
	}

	if (!count) {
1881
		mutex_unlock(&cam->fileop_mutex);
L
Linus Torvalds 已提交
1882 1883 1884 1885 1886
		return 0;
	}

	if (list_empty(&cam->outqueue)) {
		if (filp->f_flags & O_NONBLOCK) {
1887
			mutex_unlock(&cam->fileop_mutex);
L
Linus Torvalds 已提交
1888 1889
			return -EAGAIN;
		}
1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900
		if (!cam->module_param.frame_timeout) {
			err = wait_event_interruptible
			      ( cam->wait_frame,
				(!list_empty(&cam->outqueue)) ||
				(cam->state & DEV_DISCONNECTED) ||
				(cam->state & DEV_MISCONFIGURED) );
			if (err) {
				mutex_unlock(&cam->fileop_mutex);
				return err;
			}
		} else {
1901 1902 1903 1904 1905 1906 1907 1908 1909 1910
			timeout = wait_event_interruptible_timeout
				  ( cam->wait_frame,
				    (!list_empty(&cam->outqueue)) ||
				    (cam->state & DEV_DISCONNECTED) ||
				    (cam->state & DEV_MISCONFIGURED),
				    cam->module_param.frame_timeout *
				    1000 * msecs_to_jiffies(1) );
			if (timeout < 0) {
				mutex_unlock(&cam->fileop_mutex);
				return timeout;
1911 1912 1913 1914 1915 1916
			} else if (timeout == 0 &&
				   !(cam->state & DEV_DISCONNECTED)) {
				DBG(1, "Video frame timeout elapsed");
				mutex_unlock(&cam->fileop_mutex);
				return -EIO;
			}
L
Linus Torvalds 已提交
1917 1918
		}
		if (cam->state & DEV_DISCONNECTED) {
1919
			mutex_unlock(&cam->fileop_mutex);
L
Linus Torvalds 已提交
1920 1921
			return -ENODEV;
		}
1922
		if (cam->state & DEV_MISCONFIGURED) {
1923
			mutex_unlock(&cam->fileop_mutex);
L
Linus Torvalds 已提交
1924 1925 1926 1927 1928 1929
			return -EIO;
		}
	}

	f = list_entry(cam->outqueue.prev, struct sn9c102_frame_t, frame);

1930 1931 1932 1933 1934 1935 1936 1937 1938 1939
	if (count > f->buf.bytesused)
		count = f->buf.bytesused;

	if (copy_to_user(buf, f->bufmem, count)) {
		err = -EFAULT;
		goto exit;
	}
	*f_pos += count;

exit:
L
Linus Torvalds 已提交
1940 1941 1942 1943 1944 1945 1946 1947
	spin_lock_irqsave(&cam->queue_lock, lock_flags);
	list_for_each_entry(i, &cam->outqueue, frame)
		i->state = F_UNUSED;
	INIT_LIST_HEAD(&cam->outqueue);
	spin_unlock_irqrestore(&cam->queue_lock, lock_flags);

	sn9c102_queue_unusedframes(cam);

1948 1949
	PDBGG("Frame #%lu, bytes read: %zu",
	      (unsigned long)f->buf.index, count);
L
Linus Torvalds 已提交
1950

1951
	mutex_unlock(&cam->fileop_mutex);
L
Linus Torvalds 已提交
1952 1953 1954 1955 1956 1957 1958 1959

	return count;
}


static unsigned int sn9c102_poll(struct file *filp, poll_table *wait)
{
	struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1960 1961
	struct sn9c102_frame_t* f;
	unsigned long lock_flags;
L
Linus Torvalds 已提交
1962 1963
	unsigned int mask = 0;

1964
	if (mutex_lock_interruptible(&cam->fileop_mutex))
L
Linus Torvalds 已提交
1965 1966 1967
		return POLLERR;

	if (cam->state & DEV_DISCONNECTED) {
1968
		DBG(1, "Device not present");
L
Linus Torvalds 已提交
1969 1970 1971 1972
		goto error;
	}

	if (cam->state & DEV_MISCONFIGURED) {
1973 1974
		DBG(1, "The camera is misconfigured. Close and open it "
		       "again.");
L
Linus Torvalds 已提交
1975 1976 1977 1978 1979
		goto error;
	}

	if (cam->io == IO_NONE) {
		if (!sn9c102_request_buffers(cam, cam->nreadbuffers,
1980
					     IO_READ)) {
1981
			DBG(1, "poll() failed, not enough memory");
L
Linus Torvalds 已提交
1982 1983 1984 1985 1986 1987
			goto error;
		}
		cam->io = IO_READ;
		cam->stream = STREAM_ON;
	}

1988 1989 1990 1991 1992 1993
	if (cam->io == IO_READ) {
		spin_lock_irqsave(&cam->queue_lock, lock_flags);
		list_for_each_entry(f, &cam->outqueue, frame)
			f->state = F_UNUSED;
		INIT_LIST_HEAD(&cam->outqueue);
		spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
L
Linus Torvalds 已提交
1994
		sn9c102_queue_unusedframes(cam);
1995
	}
L
Linus Torvalds 已提交
1996 1997 1998 1999 2000 2001

	poll_wait(filp, &cam->wait_frame, wait);

	if (!list_empty(&cam->outqueue))
		mask |= POLLIN | POLLRDNORM;

2002
	mutex_unlock(&cam->fileop_mutex);
L
Linus Torvalds 已提交
2003 2004 2005 2006

	return mask;

error:
2007
	mutex_unlock(&cam->fileop_mutex);
L
Linus Torvalds 已提交
2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036
	return POLLERR;
}


static void sn9c102_vm_open(struct vm_area_struct* vma)
{
	struct sn9c102_frame_t* f = vma->vm_private_data;
	f->vma_use_count++;
}


static void sn9c102_vm_close(struct vm_area_struct* vma)
{
	/* NOTE: buffers are not freed here */
	struct sn9c102_frame_t* f = vma->vm_private_data;
	f->vma_use_count--;
}


static struct vm_operations_struct sn9c102_vm_ops = {
	.open = sn9c102_vm_open,
	.close = sn9c102_vm_close,
};


static int sn9c102_mmap(struct file* filp, struct vm_area_struct *vma)
{
	struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
	unsigned long size = vma->vm_end - vma->vm_start,
2037
		      start = vma->vm_start;
2038
	void *pos;
L
Linus Torvalds 已提交
2039 2040
	u32 i;

2041
	if (mutex_lock_interruptible(&cam->fileop_mutex))
L
Linus Torvalds 已提交
2042 2043 2044
		return -ERESTARTSYS;

	if (cam->state & DEV_DISCONNECTED) {
2045
		DBG(1, "Device not present");
2046
		mutex_unlock(&cam->fileop_mutex);
L
Linus Torvalds 已提交
2047 2048 2049 2050
		return -ENODEV;
	}

	if (cam->state & DEV_MISCONFIGURED) {
2051 2052
		DBG(1, "The camera is misconfigured. Close and open it "
		       "again.");
2053
		mutex_unlock(&cam->fileop_mutex);
L
Linus Torvalds 已提交
2054 2055 2056
		return -EIO;
	}

2057 2058 2059 2060 2061 2062
	if (!(vma->vm_flags & (VM_WRITE | VM_READ))) {
		mutex_unlock(&cam->fileop_mutex);
		return -EACCES;
	}

	if (cam->io != IO_MMAP ||
L
Linus Torvalds 已提交
2063
	    size != PAGE_ALIGN(cam->frame[0].buf.length)) {
2064
		mutex_unlock(&cam->fileop_mutex);
L
Linus Torvalds 已提交
2065 2066 2067 2068 2069 2070 2071 2072
		return -EINVAL;
	}

	for (i = 0; i < cam->nbuffers; i++) {
		if ((cam->frame[i].buf.m.offset>>PAGE_SHIFT) == vma->vm_pgoff)
			break;
	}
	if (i == cam->nbuffers) {
2073
		mutex_unlock(&cam->fileop_mutex);
L
Linus Torvalds 已提交
2074 2075 2076 2077
		return -EINVAL;
	}

	vma->vm_flags |= VM_IO;
2078
	vma->vm_flags |= VM_RESERVED;
L
Linus Torvalds 已提交
2079

2080
	pos = cam->frame[i].bufmem;
L
Linus Torvalds 已提交
2081
	while (size > 0) { /* size is page-aligned */
2082
		if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
2083
			mutex_unlock(&cam->fileop_mutex);
L
Linus Torvalds 已提交
2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095
			return -EAGAIN;
		}
		start += PAGE_SIZE;
		pos += PAGE_SIZE;
		size -= PAGE_SIZE;
	}

	vma->vm_ops = &sn9c102_vm_ops;
	vma->vm_private_data = &cam->frame[i];

	sn9c102_vm_open(vma);

2096
	mutex_unlock(&cam->fileop_mutex);
L
Linus Torvalds 已提交
2097 2098 2099 2100

	return 0;
}

2101
/*****************************************************************************/
L
Linus Torvalds 已提交
2102

2103 2104
static int
sn9c102_vidioc_querycap(struct sn9c102_device* cam, void __user * arg)
L
Linus Torvalds 已提交
2105
{
2106 2107 2108 2109
	struct v4l2_capability cap = {
		.driver = "sn9c102",
		.version = SN9C102_MODULE_VERSION_CODE,
		.capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
2110
				V4L2_CAP_STREAMING,
2111 2112 2113 2114
	};

	strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card));
	if (usb_make_path(cam->usbdev, cap.bus_info, sizeof(cap.bus_info)) < 0)
2115
		strlcpy(cap.bus_info, cam->usbdev->dev.bus_id,
2116
			sizeof(cap.bus_info));
2117 2118 2119

	if (copy_to_user(arg, &cap, sizeof(cap)))
		return -EFAULT;
L
Linus Torvalds 已提交
2120

2121 2122
	return 0;
}
L
Linus Torvalds 已提交
2123 2124


2125 2126 2127 2128
static int
sn9c102_vidioc_enuminput(struct sn9c102_device* cam, void __user * arg)
{
	struct v4l2_input i;
L
Linus Torvalds 已提交
2129

2130 2131
	if (copy_from_user(&i, arg, sizeof(i)))
		return -EFAULT;
L
Linus Torvalds 已提交
2132

2133 2134
	if (i.index)
		return -EINVAL;
L
Linus Torvalds 已提交
2135

2136
	memset(&i, 0, sizeof(i));
2137
	strcpy(i.name, "Camera");
2138
	i.type = V4L2_INPUT_TYPE_CAMERA;
L
Linus Torvalds 已提交
2139

2140 2141
	if (copy_to_user(arg, &i, sizeof(i)))
		return -EFAULT;
L
Linus Torvalds 已提交
2142

2143 2144
	return 0;
}
L
Linus Torvalds 已提交
2145 2146


2147
static int
2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160
sn9c102_vidioc_g_input(struct sn9c102_device* cam, void __user * arg)
{
	int index = 0;

	if (copy_to_user(arg, &index, sizeof(index)))
		return -EFAULT;

	return 0;
}


static int
sn9c102_vidioc_s_input(struct sn9c102_device* cam, void __user * arg)
2161 2162
{
	int index;
L
Linus Torvalds 已提交
2163

2164 2165
	if (copy_from_user(&index, arg, sizeof(index)))
		return -EFAULT;
L
Linus Torvalds 已提交
2166

2167
	if (index != 0)
L
Linus Torvalds 已提交
2168 2169
		return -EINVAL;

2170 2171
	return 0;
}
L
Linus Torvalds 已提交
2172 2173


2174 2175 2176
static int
sn9c102_vidioc_query_ctrl(struct sn9c102_device* cam, void __user * arg)
{
2177
	struct sn9c102_sensor* s = &cam->sensor;
2178 2179
	struct v4l2_queryctrl qc;
	u8 i;
L
Linus Torvalds 已提交
2180

2181 2182
	if (copy_from_user(&qc, arg, sizeof(qc)))
		return -EFAULT;
L
Linus Torvalds 已提交
2183

2184 2185 2186 2187 2188 2189 2190
	for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
		if (qc.id && qc.id == s->qctrl[i].id) {
			memcpy(&qc, &(s->qctrl[i]), sizeof(qc));
			if (copy_to_user(arg, &qc, sizeof(qc)))
				return -EFAULT;
			return 0;
		}
L
Linus Torvalds 已提交
2191

2192 2193
	return -EINVAL;
}
L
Linus Torvalds 已提交
2194 2195


2196 2197 2198
static int
sn9c102_vidioc_g_ctrl(struct sn9c102_device* cam, void __user * arg)
{
2199
	struct sn9c102_sensor* s = &cam->sensor;
2200 2201 2202
	struct v4l2_control ctrl;
	int err = 0;
	u8 i;
L
Linus Torvalds 已提交
2203

2204 2205
	if (!s->get_ctrl && !s->set_ctrl)
		return -EINVAL;
L
Linus Torvalds 已提交
2206

2207 2208 2209 2210
	if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
		return -EFAULT;

	if (!s->get_ctrl) {
2211
		for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
2212 2213 2214
			if (ctrl.id && ctrl.id == s->qctrl[i].id) {
				ctrl.value = s->_qctrl[i].default_value;
				goto exit;
L
Linus Torvalds 已提交
2215
			}
2216 2217 2218
		return -EINVAL;
	} else
		err = s->get_ctrl(cam, &ctrl);
L
Linus Torvalds 已提交
2219

2220 2221 2222
exit:
	if (copy_to_user(arg, &ctrl, sizeof(ctrl)))
		return -EFAULT;
L
Linus Torvalds 已提交
2223

2224 2225 2226
	PDBGG("VIDIOC_G_CTRL: id %lu, value %lu",
	      (unsigned long)ctrl.id, (unsigned long)ctrl.value);

2227 2228
	return err;
}
L
Linus Torvalds 已提交
2229 2230


2231 2232 2233
static int
sn9c102_vidioc_s_ctrl(struct sn9c102_device* cam, void __user * arg)
{
2234
	struct sn9c102_sensor* s = &cam->sensor;
2235 2236 2237
	struct v4l2_control ctrl;
	u8 i;
	int err = 0;
L
Linus Torvalds 已提交
2238

2239 2240
	if (!s->set_ctrl)
		return -EINVAL;
L
Linus Torvalds 已提交
2241

2242 2243
	if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
		return -EFAULT;
L
Linus Torvalds 已提交
2244

2245 2246
	for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
		if (ctrl.id == s->qctrl[i].id) {
2247 2248
			if (s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)
				return -EINVAL;
2249 2250 2251 2252 2253 2254
			if (ctrl.value < s->qctrl[i].minimum ||
			    ctrl.value > s->qctrl[i].maximum)
				return -ERANGE;
			ctrl.value -= ctrl.value % s->qctrl[i].step;
			break;
		}
L
Linus Torvalds 已提交
2255

2256 2257
	if ((err = s->set_ctrl(cam, &ctrl)))
		return err;
L
Linus Torvalds 已提交
2258

2259
	s->_qctrl[i].default_value = ctrl.value;
L
Linus Torvalds 已提交
2260

2261 2262
	PDBGG("VIDIOC_S_CTRL: id %lu, value %lu",
	      (unsigned long)ctrl.id, (unsigned long)ctrl.value);
L
Linus Torvalds 已提交
2263

2264 2265
	return 0;
}
L
Linus Torvalds 已提交
2266 2267


2268 2269 2270
static int
sn9c102_vidioc_cropcap(struct sn9c102_device* cam, void __user * arg)
{
2271
	struct v4l2_cropcap* cc = &(cam->sensor.cropcap);
L
Linus Torvalds 已提交
2272

2273 2274 2275
	cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	cc->pixelaspect.numerator = 1;
	cc->pixelaspect.denominator = 1;
L
Linus Torvalds 已提交
2276

2277 2278
	if (copy_to_user(arg, cc, sizeof(*cc)))
		return -EFAULT;
L
Linus Torvalds 已提交
2279

2280 2281
	return 0;
}
L
Linus Torvalds 已提交
2282 2283


2284 2285 2286
static int
sn9c102_vidioc_g_crop(struct sn9c102_device* cam, void __user * arg)
{
2287
	struct sn9c102_sensor* s = &cam->sensor;
2288 2289 2290
	struct v4l2_crop crop = {
		.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
	};
L
Linus Torvalds 已提交
2291

2292
	memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect));
L
Linus Torvalds 已提交
2293

2294 2295
	if (copy_to_user(arg, &crop, sizeof(crop)))
		return -EFAULT;
L
Linus Torvalds 已提交
2296

2297 2298
	return 0;
}
L
Linus Torvalds 已提交
2299 2300


2301 2302 2303
static int
sn9c102_vidioc_s_crop(struct sn9c102_device* cam, void __user * arg)
{
2304
	struct sn9c102_sensor* s = &cam->sensor;
2305 2306 2307 2308 2309 2310 2311 2312 2313
	struct v4l2_crop crop;
	struct v4l2_rect* rect;
	struct v4l2_rect* bounds = &(s->cropcap.bounds);
	struct v4l2_pix_format* pix_format = &(s->pix_format);
	u8 scale;
	const enum sn9c102_stream_state stream = cam->stream;
	const u32 nbuffers = cam->nbuffers;
	u32 i;
	int err = 0;
L
Linus Torvalds 已提交
2314

2315 2316
	if (copy_from_user(&crop, arg, sizeof(crop)))
		return -EFAULT;
L
Linus Torvalds 已提交
2317

2318
	rect = &(crop.c);
L
Linus Torvalds 已提交
2319

2320 2321
	if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;
L
Linus Torvalds 已提交
2322

2323 2324 2325 2326 2327
	if (cam->module_param.force_munmap)
		for (i = 0; i < cam->nbuffers; i++)
			if (cam->frame[i].vma_use_count) {
				DBG(3, "VIDIOC_S_CROP failed. "
				       "Unmap the buffers first.");
2328
				return -EBUSY;
2329
			}
L
Linus Torvalds 已提交
2330

2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366
	/* Preserve R,G or B origin */
	rect->left = (s->_rect.left & 1L) ? rect->left | 1L : rect->left & ~1L;
	rect->top = (s->_rect.top & 1L) ? rect->top | 1L : rect->top & ~1L;

	if (rect->width < 16)
		rect->width = 16;
	if (rect->height < 16)
		rect->height = 16;
	if (rect->width > bounds->width)
		rect->width = bounds->width;
	if (rect->height > bounds->height)
		rect->height = bounds->height;
	if (rect->left < bounds->left)
		rect->left = bounds->left;
	if (rect->top < bounds->top)
		rect->top = bounds->top;
	if (rect->left + rect->width > bounds->left + bounds->width)
		rect->left = bounds->left+bounds->width - rect->width;
	if (rect->top + rect->height > bounds->top + bounds->height)
		rect->top = bounds->top+bounds->height - rect->height;

	rect->width &= ~15L;
	rect->height &= ~15L;

	if (SN9C102_PRESERVE_IMGSCALE) {
		/* Calculate the actual scaling factor */
		u32 a, b;
		a = rect->width * rect->height;
		b = pix_format->width * pix_format->height;
		scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
	} else
		scale = 1;

	if (cam->stream == STREAM_ON)
		if ((err = sn9c102_stream_interrupt(cam)))
			return err;
L
Linus Torvalds 已提交
2367

2368 2369 2370
	if (copy_to_user(arg, &crop, sizeof(crop))) {
		cam->stream = stream;
		return -EFAULT;
L
Linus Torvalds 已提交
2371 2372
	}

2373 2374
	if (cam->module_param.force_munmap || cam->io == IO_READ)
		sn9c102_release_buffers(cam);
L
Linus Torvalds 已提交
2375

2376 2377 2378 2379
	err = sn9c102_set_crop(cam, rect);
	if (s->set_crop)
		err += s->set_crop(cam, rect);
	err += sn9c102_set_scale(cam, scale);
L
Linus Torvalds 已提交
2380

2381 2382 2383 2384 2385 2386 2387
	if (err) { /* atomic, no rollback in ioctl() */
		cam->state |= DEV_MISCONFIGURED;
		DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To "
		       "use the camera, close and open /dev/video%d again.",
		    cam->v4ldev->minor);
		return -EIO;
	}
L
Linus Torvalds 已提交
2388

2389 2390 2391
	s->pix_format.width = rect->width/scale;
	s->pix_format.height = rect->height/scale;
	memcpy(&(s->_rect), rect, sizeof(*rect));
L
Linus Torvalds 已提交
2392

2393 2394 2395 2396 2397 2398 2399
	if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
	    nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->io)) {
		cam->state |= DEV_MISCONFIGURED;
		DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To "
		       "use the camera, close and open /dev/video%d again.",
		    cam->v4ldev->minor);
		return -ENOMEM;
L
Linus Torvalds 已提交
2400 2401
	}

2402 2403 2404 2405
	if (cam->io == IO_READ)
		sn9c102_empty_framequeues(cam);
	else if (cam->module_param.force_munmap)
		sn9c102_requeue_outqueue(cam);
L
Linus Torvalds 已提交
2406

2407
	cam->stream = stream;
L
Linus Torvalds 已提交
2408

2409 2410
	return 0;
}
L
Linus Torvalds 已提交
2411 2412


2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451
static int
sn9c102_vidioc_enum_framesizes(struct sn9c102_device* cam, void __user * arg)
{
	struct v4l2_frmsizeenum frmsize;

	if (copy_from_user(&frmsize, arg, sizeof(frmsize)))
		return -EFAULT;

	if (frmsize.index != 0)
		return -EINVAL;

	switch (cam->bridge) {
	case BRIDGE_SN9C101:
	case BRIDGE_SN9C102:
	case BRIDGE_SN9C103:
		if (frmsize.pixel_format != V4L2_PIX_FMT_SN9C10X &&
		    frmsize.pixel_format != V4L2_PIX_FMT_SBGGR8)
			return -EINVAL;
	case BRIDGE_SN9C105:
	case BRIDGE_SN9C120:
		if (frmsize.pixel_format != V4L2_PIX_FMT_JPEG &&
		    frmsize.pixel_format != V4L2_PIX_FMT_SBGGR8)
			return -EINVAL;
	}

	frmsize.type = V4L2_FRMSIZE_TYPE_STEPWISE;
	frmsize.stepwise.min_width = frmsize.stepwise.step_width = 16;
	frmsize.stepwise.min_height = frmsize.stepwise.step_height = 16;
	frmsize.stepwise.max_width = cam->sensor.cropcap.bounds.width;
	frmsize.stepwise.max_height = cam->sensor.cropcap.bounds.height;
	memset(&frmsize.reserved, 0, sizeof(frmsize.reserved));

	if (copy_to_user(arg, &frmsize, sizeof(frmsize)))
		return -EFAULT;

	return 0;
}


2452 2453 2454 2455
static int
sn9c102_vidioc_enum_fmt(struct sn9c102_device* cam, void __user * arg)
{
	struct v4l2_fmtdesc fmtd;
L
Linus Torvalds 已提交
2456

2457 2458
	if (copy_from_user(&fmtd, arg, sizeof(fmtd)))
		return -EFAULT;
L
Linus Torvalds 已提交
2459

2460 2461 2462
	if (fmtd.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;

2463 2464 2465 2466
	if (fmtd.index == 0) {
		strcpy(fmtd.description, "bayer rgb");
		fmtd.pixelformat = V4L2_PIX_FMT_SBGGR8;
	} else if (fmtd.index == 1) {
2467 2468 2469 2470
		switch (cam->bridge) {
		case BRIDGE_SN9C101:
		case BRIDGE_SN9C102:
		case BRIDGE_SN9C103:
2471 2472
			strcpy(fmtd.description, "compressed");
			fmtd.pixelformat = V4L2_PIX_FMT_SN9C10X;
2473 2474 2475 2476 2477 2478 2479
			break;
		case BRIDGE_SN9C105:
		case BRIDGE_SN9C120:
			strcpy(fmtd.description, "JPEG");
			fmtd.pixelformat = V4L2_PIX_FMT_JPEG;
			break;
		}
2480 2481 2482
		fmtd.flags = V4L2_FMT_FLAG_COMPRESSED;
	} else
		return -EINVAL;
L
Linus Torvalds 已提交
2483

2484 2485
	fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	memset(&fmtd.reserved, 0, sizeof(fmtd.reserved));
L
Linus Torvalds 已提交
2486

2487 2488
	if (copy_to_user(arg, &fmtd, sizeof(fmtd)))
		return -EFAULT;
L
Linus Torvalds 已提交
2489

2490 2491
	return 0;
}
L
Linus Torvalds 已提交
2492 2493


2494 2495 2496 2497
static int
sn9c102_vidioc_g_fmt(struct sn9c102_device* cam, void __user * arg)
{
	struct v4l2_format format;
2498
	struct v4l2_pix_format* pfmt = &(cam->sensor.pix_format);
L
Linus Torvalds 已提交
2499

2500 2501
	if (copy_from_user(&format, arg, sizeof(format)))
		return -EFAULT;
L
Linus Torvalds 已提交
2502

2503 2504
	if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;
L
Linus Torvalds 已提交
2505

2506 2507 2508 2509
	pfmt->colorspace = (pfmt->pixelformat == V4L2_PIX_FMT_JPEG) ?
			   V4L2_COLORSPACE_JPEG : V4L2_COLORSPACE_SRGB;
	pfmt->bytesperline = (pfmt->pixelformat == V4L2_PIX_FMT_SN9C10X ||
			      pfmt->pixelformat == V4L2_PIX_FMT_JPEG)
2510
			     ? 0 : (pfmt->width * pfmt->priv) / 8;
2511 2512 2513
	pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8);
	pfmt->field = V4L2_FIELD_NONE;
	memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt));
L
Linus Torvalds 已提交
2514

2515 2516
	if (copy_to_user(arg, &format, sizeof(format)))
		return -EFAULT;
L
Linus Torvalds 已提交
2517

2518 2519
	return 0;
}
L
Linus Torvalds 已提交
2520 2521


2522 2523
static int
sn9c102_vidioc_try_s_fmt(struct sn9c102_device* cam, unsigned int cmd,
2524
			 void __user * arg)
2525
{
2526
	struct sn9c102_sensor* s = &cam->sensor;
2527 2528 2529 2530 2531 2532 2533 2534 2535 2536
	struct v4l2_format format;
	struct v4l2_pix_format* pix;
	struct v4l2_pix_format* pfmt = &(s->pix_format);
	struct v4l2_rect* bounds = &(s->cropcap.bounds);
	struct v4l2_rect rect;
	u8 scale;
	const enum sn9c102_stream_state stream = cam->stream;
	const u32 nbuffers = cam->nbuffers;
	u32 i;
	int err = 0;
L
Linus Torvalds 已提交
2537

2538 2539
	if (copy_from_user(&format, arg, sizeof(format)))
		return -EFAULT;
L
Linus Torvalds 已提交
2540

2541
	pix = &(format.fmt.pix);
L
Linus Torvalds 已提交
2542

2543 2544
	if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;
L
Linus Torvalds 已提交
2545

2546
	memcpy(&rect, &(s->_rect), sizeof(rect));
L
Linus Torvalds 已提交
2547

2548 2549 2550 2551 2552
	{ /* calculate the actual scaling factor */
		u32 a, b;
		a = rect.width * rect.height;
		b = pix->width * pix->height;
		scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
L
Linus Torvalds 已提交
2553 2554
	}

2555 2556
	rect.width = scale * pix->width;
	rect.height = scale * pix->height;
L
Linus Torvalds 已提交
2557

2558 2559 2560 2561 2562 2563 2564 2565
	if (rect.width < 16)
		rect.width = 16;
	if (rect.height < 16)
		rect.height = 16;
	if (rect.width > bounds->left + bounds->width - rect.left)
		rect.width = bounds->left + bounds->width - rect.left;
	if (rect.height > bounds->top + bounds->height - rect.top)
		rect.height = bounds->top + bounds->height - rect.top;
L
Linus Torvalds 已提交
2566

2567 2568
	rect.width &= ~15L;
	rect.height &= ~15L;
L
Linus Torvalds 已提交
2569

2570 2571 2572 2573 2574 2575 2576 2577 2578 2579
	{ /* adjust the scaling factor */
		u32 a, b;
		a = rect.width * rect.height;
		b = pix->width * pix->height;
		scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
	}

	pix->width = rect.width / scale;
	pix->height = rect.height / scale;

2580 2581 2582 2583
	switch (cam->bridge) {
	case BRIDGE_SN9C101:
	case BRIDGE_SN9C102:
	case BRIDGE_SN9C103:
2584 2585 2586
		if (pix->pixelformat != V4L2_PIX_FMT_SN9C10X &&
		    pix->pixelformat != V4L2_PIX_FMT_SBGGR8)
			pix->pixelformat = pfmt->pixelformat;
2587 2588 2589 2590 2591 2592 2593 2594
		break;
	case BRIDGE_SN9C105:
	case BRIDGE_SN9C120:
		if (pix->pixelformat != V4L2_PIX_FMT_JPEG &&
		    pix->pixelformat != V4L2_PIX_FMT_SBGGR8)
			pix->pixelformat = pfmt->pixelformat;
		break;
	}
2595
	pix->priv = pfmt->priv; /* bpp */
2596 2597
	pix->colorspace = (pix->pixelformat == V4L2_PIX_FMT_JPEG) ?
			  V4L2_COLORSPACE_JPEG : V4L2_COLORSPACE_SRGB;
2598 2599
	pix->bytesperline = (pix->pixelformat == V4L2_PIX_FMT_SN9C10X ||
			     pix->pixelformat == V4L2_PIX_FMT_JPEG)
2600
			    ? 0 : (pix->width * pix->priv) / 8;
2601 2602 2603 2604 2605 2606 2607 2608
	pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8);
	pix->field = V4L2_FIELD_NONE;

	if (cmd == VIDIOC_TRY_FMT) {
		if (copy_to_user(arg, &format, sizeof(format)))
			return -EFAULT;
		return 0;
	}
L
Linus Torvalds 已提交
2609

2610
	if (cam->module_param.force_munmap)
L
Linus Torvalds 已提交
2611 2612
		for (i = 0; i < cam->nbuffers; i++)
			if (cam->frame[i].vma_use_count) {
2613 2614
				DBG(3, "VIDIOC_S_FMT failed. Unmap the "
				       "buffers first.");
2615
				return -EBUSY;
L
Linus Torvalds 已提交
2616 2617
			}

2618 2619 2620
	if (cam->stream == STREAM_ON)
		if ((err = sn9c102_stream_interrupt(cam)))
			return err;
L
Linus Torvalds 已提交
2621

2622 2623 2624 2625
	if (copy_to_user(arg, &format, sizeof(format))) {
		cam->stream = stream;
		return -EFAULT;
	}
L
Linus Torvalds 已提交
2626

2627
	if (cam->module_param.force_munmap  || cam->io == IO_READ)
L
Linus Torvalds 已提交
2628 2629
		sn9c102_release_buffers(cam);

2630 2631 2632 2633 2634 2635 2636
	err += sn9c102_set_pix_format(cam, pix);
	err += sn9c102_set_crop(cam, &rect);
	if (s->set_pix_format)
		err += s->set_pix_format(cam, pix);
	if (s->set_crop)
		err += s->set_crop(cam, &rect);
	err += sn9c102_set_scale(cam, scale);
L
Linus Torvalds 已提交
2637

2638 2639 2640 2641 2642 2643 2644
	if (err) { /* atomic, no rollback in ioctl() */
		cam->state |= DEV_MISCONFIGURED;
		DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To "
		       "use the camera, close and open /dev/video%d again.",
		    cam->v4ldev->minor);
		return -EIO;
	}
L
Linus Torvalds 已提交
2645

2646 2647 2648 2649 2650 2651 2652 2653 2654 2655
	memcpy(pfmt, pix, sizeof(*pix));
	memcpy(&(s->_rect), &rect, sizeof(rect));

	if ((cam->module_param.force_munmap  || cam->io == IO_READ) &&
	    nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->io)) {
		cam->state |= DEV_MISCONFIGURED;
		DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To "
		       "use the camera, close and open /dev/video%d again.",
		    cam->v4ldev->minor);
		return -ENOMEM;
L
Linus Torvalds 已提交
2656 2657
	}

2658 2659 2660 2661
	if (cam->io == IO_READ)
		sn9c102_empty_framequeues(cam);
	else if (cam->module_param.force_munmap)
		sn9c102_requeue_outqueue(cam);
L
Linus Torvalds 已提交
2662

2663
	cam->stream = stream;
L
Linus Torvalds 已提交
2664

2665 2666 2667 2668 2669 2670 2671
	return 0;
}


static int
sn9c102_vidioc_g_jpegcomp(struct sn9c102_device* cam, void __user * arg)
{
2672
	if (copy_to_user(arg, &cam->compression, sizeof(cam->compression)))
2673
		return -EFAULT;
L
Linus Torvalds 已提交
2674

2675 2676
	return 0;
}
L
Linus Torvalds 已提交
2677 2678


2679 2680 2681 2682 2683 2684
static int
sn9c102_vidioc_s_jpegcomp(struct sn9c102_device* cam, void __user * arg)
{
	struct v4l2_jpegcompression jc;
	const enum sn9c102_stream_state stream = cam->stream;
	int err = 0;
L
Linus Torvalds 已提交
2685

2686 2687
	if (copy_from_user(&jc, arg, sizeof(jc)))
		return -EFAULT;
L
Linus Torvalds 已提交
2688

2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702
	if (jc.quality != 0 && jc.quality != 1)
		return -EINVAL;

	if (cam->stream == STREAM_ON)
		if ((err = sn9c102_stream_interrupt(cam)))
			return err;

	err += sn9c102_set_compression(cam, &jc);
	if (err) { /* atomic, no rollback in ioctl() */
		cam->state |= DEV_MISCONFIGURED;
		DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
		       "problems. To use the camera, close and open "
		       "/dev/video%d again.", cam->v4ldev->minor);
		return -EIO;
L
Linus Torvalds 已提交
2703 2704
	}

2705
	cam->compression.quality = jc.quality;
L
Linus Torvalds 已提交
2706

2707
	cam->stream = stream;
L
Linus Torvalds 已提交
2708

2709 2710
	return 0;
}
L
Linus Torvalds 已提交
2711

2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729

static int
sn9c102_vidioc_reqbufs(struct sn9c102_device* cam, void __user * arg)
{
	struct v4l2_requestbuffers rb;
	u32 i;
	int err;

	if (copy_from_user(&rb, arg, sizeof(rb)))
		return -EFAULT;

	if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
	    rb.memory != V4L2_MEMORY_MMAP)
		return -EINVAL;

	if (cam->io == IO_READ) {
		DBG(3, "Close and open the device again to choose the mmap "
		       "I/O method");
2730
		return -EBUSY;
2731 2732 2733 2734 2735 2736
	}

	for (i = 0; i < cam->nbuffers; i++)
		if (cam->frame[i].vma_use_count) {
			DBG(3, "VIDIOC_REQBUFS failed. Previous buffers are "
			       "still mapped.");
2737
			return -EBUSY;
2738
		}
L
Linus Torvalds 已提交
2739

2740 2741 2742
	if (cam->stream == STREAM_ON)
		if ((err = sn9c102_stream_interrupt(cam)))
			return err;
L
Linus Torvalds 已提交
2743

2744
	sn9c102_empty_framequeues(cam);
L
Linus Torvalds 已提交
2745

2746 2747 2748
	sn9c102_release_buffers(cam);
	if (rb.count)
		rb.count = sn9c102_request_buffers(cam, rb.count, IO_MMAP);
L
Linus Torvalds 已提交
2749

2750 2751 2752 2753
	if (copy_to_user(arg, &rb, sizeof(rb))) {
		sn9c102_release_buffers(cam);
		cam->io = IO_NONE;
		return -EFAULT;
L
Linus Torvalds 已提交
2754 2755
	}

2756
	cam->io = rb.count ? IO_MMAP : IO_NONE;
L
Linus Torvalds 已提交
2757

2758 2759
	return 0;
}
L
Linus Torvalds 已提交
2760 2761


2762 2763 2764 2765
static int
sn9c102_vidioc_querybuf(struct sn9c102_device* cam, void __user * arg)
{
	struct v4l2_buffer b;
L
Linus Torvalds 已提交
2766

2767 2768
	if (copy_from_user(&b, arg, sizeof(b)))
		return -EFAULT;
L
Linus Torvalds 已提交
2769

2770 2771 2772
	if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
	    b.index >= cam->nbuffers || cam->io != IO_MMAP)
		return -EINVAL;
L
Linus Torvalds 已提交
2773

2774
	memcpy(&b, &cam->frame[b.index].buf, sizeof(b));
L
Linus Torvalds 已提交
2775

2776 2777
	if (cam->frame[b.index].vma_use_count)
		b.flags |= V4L2_BUF_FLAG_MAPPED;
L
Linus Torvalds 已提交
2778

2779 2780 2781 2782
	if (cam->frame[b.index].state == F_DONE)
		b.flags |= V4L2_BUF_FLAG_DONE;
	else if (cam->frame[b.index].state != F_UNUSED)
		b.flags |= V4L2_BUF_FLAG_QUEUED;
L
Linus Torvalds 已提交
2783

2784 2785
	if (copy_to_user(arg, &b, sizeof(b)))
		return -EFAULT;
L
Linus Torvalds 已提交
2786

2787 2788
	return 0;
}
L
Linus Torvalds 已提交
2789 2790


2791 2792 2793 2794 2795
static int
sn9c102_vidioc_qbuf(struct sn9c102_device* cam, void __user * arg)
{
	struct v4l2_buffer b;
	unsigned long lock_flags;
L
Linus Torvalds 已提交
2796

2797 2798
	if (copy_from_user(&b, arg, sizeof(b)))
		return -EFAULT;
L
Linus Torvalds 已提交
2799

2800 2801 2802
	if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
	    b.index >= cam->nbuffers || cam->io != IO_MMAP)
		return -EINVAL;
L
Linus Torvalds 已提交
2803

2804 2805
	if (cam->frame[b.index].state != F_UNUSED)
		return -EINVAL;
L
Linus Torvalds 已提交
2806

2807
	cam->frame[b.index].state = F_QUEUED;
L
Linus Torvalds 已提交
2808

2809 2810 2811
	spin_lock_irqsave(&cam->queue_lock, lock_flags);
	list_add_tail(&cam->frame[b.index].frame, &cam->inqueue);
	spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
L
Linus Torvalds 已提交
2812

2813
	PDBGG("Frame #%lu queued", (unsigned long)b.index);
L
Linus Torvalds 已提交
2814

2815 2816
	return 0;
}
L
Linus Torvalds 已提交
2817 2818


2819 2820
static int
sn9c102_vidioc_dqbuf(struct sn9c102_device* cam, struct file* filp,
2821
		     void __user * arg)
2822 2823 2824 2825
{
	struct v4l2_buffer b;
	struct sn9c102_frame_t *f;
	unsigned long lock_flags;
2826
	long timeout;
2827
	int err = 0;
L
Linus Torvalds 已提交
2828

2829 2830
	if (copy_from_user(&b, arg, sizeof(b)))
		return -EFAULT;
L
Linus Torvalds 已提交
2831

2832 2833 2834 2835 2836 2837 2838 2839
	if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
		return -EINVAL;

	if (list_empty(&cam->outqueue)) {
		if (cam->stream == STREAM_OFF)
			return -EINVAL;
		if (filp->f_flags & O_NONBLOCK)
			return -EAGAIN;
2840 2841 2842 2843 2844 2845 2846 2847 2848
		if (!cam->module_param.frame_timeout) {
			err = wait_event_interruptible
			      ( cam->wait_frame,
				(!list_empty(&cam->outqueue)) ||
				(cam->state & DEV_DISCONNECTED) ||
				(cam->state & DEV_MISCONFIGURED) );
			if (err)
				return err;
		} else {
2849 2850 2851 2852 2853 2854 2855 2856 2857
			timeout = wait_event_interruptible_timeout
				  ( cam->wait_frame,
				    (!list_empty(&cam->outqueue)) ||
				    (cam->state & DEV_DISCONNECTED) ||
				    (cam->state & DEV_MISCONFIGURED),
				    cam->module_param.frame_timeout *
				    1000 * msecs_to_jiffies(1) );
			if (timeout < 0)
				return timeout;
2858 2859 2860 2861 2862 2863
			else if (timeout == 0 &&
				 !(cam->state & DEV_DISCONNECTED)) {
				DBG(1, "Video frame timeout elapsed");
				return -EIO;
			}
		}
2864 2865
		if (cam->state & DEV_DISCONNECTED)
			return -ENODEV;
2866
		if (cam->state & DEV_MISCONFIGURED)
2867
			return -EIO;
L
Linus Torvalds 已提交
2868 2869
	}

2870 2871 2872 2873
	spin_lock_irqsave(&cam->queue_lock, lock_flags);
	f = list_entry(cam->outqueue.next, struct sn9c102_frame_t, frame);
	list_del(cam->outqueue.next);
	spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
L
Linus Torvalds 已提交
2874

2875
	f->state = F_UNUSED;
L
Linus Torvalds 已提交
2876

2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964
	memcpy(&b, &f->buf, sizeof(b));
	if (f->vma_use_count)
		b.flags |= V4L2_BUF_FLAG_MAPPED;

	if (copy_to_user(arg, &b, sizeof(b)))
		return -EFAULT;

	PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index);

	return 0;
}


static int
sn9c102_vidioc_streamon(struct sn9c102_device* cam, void __user * arg)
{
	int type;

	if (copy_from_user(&type, arg, sizeof(type)))
		return -EFAULT;

	if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
		return -EINVAL;

	cam->stream = STREAM_ON;

	DBG(3, "Stream on");

	return 0;
}


static int
sn9c102_vidioc_streamoff(struct sn9c102_device* cam, void __user * arg)
{
	int type, err;

	if (copy_from_user(&type, arg, sizeof(type)))
		return -EFAULT;

	if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
		return -EINVAL;

	if (cam->stream == STREAM_ON)
		if ((err = sn9c102_stream_interrupt(cam)))
			return err;

	sn9c102_empty_framequeues(cam);

	DBG(3, "Stream off");

	return 0;
}


static int
sn9c102_vidioc_g_parm(struct sn9c102_device* cam, void __user * arg)
{
	struct v4l2_streamparm sp;

	if (copy_from_user(&sp, arg, sizeof(sp)))
		return -EFAULT;

	if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;

	sp.parm.capture.extendedmode = 0;
	sp.parm.capture.readbuffers = cam->nreadbuffers;

	if (copy_to_user(arg, &sp, sizeof(sp)))
		return -EFAULT;

	return 0;
}


static int
sn9c102_vidioc_s_parm(struct sn9c102_device* cam, void __user * arg)
{
	struct v4l2_streamparm sp;

	if (copy_from_user(&sp, arg, sizeof(sp)))
		return -EFAULT;

	if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;

	sp.parm.capture.extendedmode = 0;
L
Linus Torvalds 已提交
2965

2966
	if (sp.parm.capture.readbuffers == 0)
L
Linus Torvalds 已提交
2967 2968
		sp.parm.capture.readbuffers = cam->nreadbuffers;

2969 2970
	if (sp.parm.capture.readbuffers > SN9C102_MAX_FRAMES)
		sp.parm.capture.readbuffers = SN9C102_MAX_FRAMES;
L
Linus Torvalds 已提交
2971

2972 2973
	if (copy_to_user(arg, &sp, sizeof(sp)))
		return -EFAULT;
L
Linus Torvalds 已提交
2974

2975
	cam->nreadbuffers = sp.parm.capture.readbuffers;
L
Linus Torvalds 已提交
2976

2977 2978
	return 0;
}
L
Linus Torvalds 已提交
2979 2980


2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044
static int
sn9c102_vidioc_enumaudio(struct sn9c102_device* cam, void __user * arg)
{
	struct v4l2_audio audio;

	if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102)
		return -EINVAL;

	if (copy_from_user(&audio, arg, sizeof(audio)))
		return -EFAULT;

	if (audio.index != 0)
		return -EINVAL;

	strcpy(audio.name, "Microphone");
	audio.capability = 0;
	audio.mode = 0;

	if (copy_to_user(arg, &audio, sizeof(audio)))
		return -EFAULT;

	return 0;
}


static int
sn9c102_vidioc_g_audio(struct sn9c102_device* cam, void __user * arg)
{
	struct v4l2_audio audio;

	if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102)
		return -EINVAL;

	if (copy_from_user(&audio, arg, sizeof(audio)))
		return -EFAULT;

	memset(&audio, 0, sizeof(audio));
	strcpy(audio.name, "Microphone");

	if (copy_to_user(arg, &audio, sizeof(audio)))
		return -EFAULT;

	return 0;
}


static int
sn9c102_vidioc_s_audio(struct sn9c102_device* cam, void __user * arg)
{
	struct v4l2_audio audio;

	if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102)
		return -EINVAL;

	if (copy_from_user(&audio, arg, sizeof(audio)))
		return -EFAULT;

	if (audio.index != 0)
		return -EINVAL;

	return 0;
}


3045
static int sn9c102_ioctl_v4l2(struct inode* inode, struct file* filp,
3046
			      unsigned int cmd, void __user * arg)
3047 3048
{
	struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
L
Linus Torvalds 已提交
3049

3050
	switch (cmd) {
L
Linus Torvalds 已提交
3051

3052 3053
	case VIDIOC_QUERYCAP:
		return sn9c102_vidioc_querycap(cam, arg);
L
Linus Torvalds 已提交
3054

3055 3056
	case VIDIOC_ENUMINPUT:
		return sn9c102_vidioc_enuminput(cam, arg);
L
Linus Torvalds 已提交
3057

3058
	case VIDIOC_G_INPUT:
3059 3060
		return sn9c102_vidioc_g_input(cam, arg);

3061
	case VIDIOC_S_INPUT:
3062
		return sn9c102_vidioc_s_input(cam, arg);
L
Linus Torvalds 已提交
3063

3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081
	case VIDIOC_QUERYCTRL:
		return sn9c102_vidioc_query_ctrl(cam, arg);

	case VIDIOC_G_CTRL:
		return sn9c102_vidioc_g_ctrl(cam, arg);

	case VIDIOC_S_CTRL:
		return sn9c102_vidioc_s_ctrl(cam, arg);

	case VIDIOC_CROPCAP:
		return sn9c102_vidioc_cropcap(cam, arg);

	case VIDIOC_G_CROP:
		return sn9c102_vidioc_g_crop(cam, arg);

	case VIDIOC_S_CROP:
		return sn9c102_vidioc_s_crop(cam, arg);

3082 3083 3084
	case VIDIOC_ENUM_FRAMESIZES:
		return sn9c102_vidioc_enum_framesizes(cam, arg);

3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123
	case VIDIOC_ENUM_FMT:
		return sn9c102_vidioc_enum_fmt(cam, arg);

	case VIDIOC_G_FMT:
		return sn9c102_vidioc_g_fmt(cam, arg);

	case VIDIOC_TRY_FMT:
	case VIDIOC_S_FMT:
		return sn9c102_vidioc_try_s_fmt(cam, cmd, arg);

	case VIDIOC_G_JPEGCOMP:
		return sn9c102_vidioc_g_jpegcomp(cam, arg);

	case VIDIOC_S_JPEGCOMP:
		return sn9c102_vidioc_s_jpegcomp(cam, arg);

	case VIDIOC_REQBUFS:
		return sn9c102_vidioc_reqbufs(cam, arg);

	case VIDIOC_QUERYBUF:
		return sn9c102_vidioc_querybuf(cam, arg);

	case VIDIOC_QBUF:
		return sn9c102_vidioc_qbuf(cam, arg);

	case VIDIOC_DQBUF:
		return sn9c102_vidioc_dqbuf(cam, filp, arg);

	case VIDIOC_STREAMON:
		return sn9c102_vidioc_streamon(cam, arg);

	case VIDIOC_STREAMOFF:
		return sn9c102_vidioc_streamoff(cam, arg);

	case VIDIOC_G_PARM:
		return sn9c102_vidioc_g_parm(cam, arg);

	case VIDIOC_S_PARM:
		return sn9c102_vidioc_s_parm(cam, arg);
L
Linus Torvalds 已提交
3124

3125 3126 3127 3128 3129 3130 3131 3132 3133
	case VIDIOC_ENUMAUDIO:
		return sn9c102_vidioc_enumaudio(cam, arg);

	case VIDIOC_G_AUDIO:
		return sn9c102_vidioc_g_audio(cam, arg);

	case VIDIOC_S_AUDIO:
		return sn9c102_vidioc_s_audio(cam, arg);

L
Linus Torvalds 已提交
3134 3135 3136 3137 3138
	case VIDIOC_G_STD:
	case VIDIOC_S_STD:
	case VIDIOC_QUERYSTD:
	case VIDIOC_ENUMSTD:
	case VIDIOC_QUERYMENU:
3139
	case VIDIOC_ENUM_FRAMEINTERVALS:
L
Linus Torvalds 已提交
3140 3141 3142 3143 3144 3145 3146 3147 3148 3149
		return -EINVAL;

	default:
		return -EINVAL;

	}
}


static int sn9c102_ioctl(struct inode* inode, struct file* filp,
3150
			 unsigned int cmd, unsigned long arg)
L
Linus Torvalds 已提交
3151 3152 3153 3154
{
	struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
	int err = 0;

3155
	if (mutex_lock_interruptible(&cam->fileop_mutex))
L
Linus Torvalds 已提交
3156 3157 3158
		return -ERESTARTSYS;

	if (cam->state & DEV_DISCONNECTED) {
3159
		DBG(1, "Device not present");
3160
		mutex_unlock(&cam->fileop_mutex);
L
Linus Torvalds 已提交
3161 3162 3163 3164
		return -ENODEV;
	}

	if (cam->state & DEV_MISCONFIGURED) {
3165 3166
		DBG(1, "The camera is misconfigured. Close and open it "
		       "again.");
3167
		mutex_unlock(&cam->fileop_mutex);
L
Linus Torvalds 已提交
3168 3169 3170
		return -EIO;
	}

3171 3172
	V4LDBG(3, "sn9c102", cmd);

L
Linus Torvalds 已提交
3173 3174
	err = sn9c102_ioctl_v4l2(inode, filp, cmd, (void __user *)arg);

3175
	mutex_unlock(&cam->fileop_mutex);
L
Linus Torvalds 已提交
3176 3177 3178 3179

	return err;
}

3180
/*****************************************************************************/
L
Linus Torvalds 已提交
3181

3182
static const struct file_operations sn9c102_fops = {
3183
	.owner = THIS_MODULE,
L
Linus Torvalds 已提交
3184 3185 3186
	.open =    sn9c102_open,
	.release = sn9c102_release,
	.ioctl =   sn9c102_ioctl,
3187
	.compat_ioctl = v4l_compat_ioctl32,
L
Linus Torvalds 已提交
3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202
	.read =    sn9c102_read,
	.poll =    sn9c102_poll,
	.mmap =    sn9c102_mmap,
	.llseek =  no_llseek,
};

/*****************************************************************************/

/* It exists a single interface only. We do not need to validate anything. */
static int
sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
{
	struct usb_device *udev = interface_to_usbdev(intf);
	struct sn9c102_device* cam;
	static unsigned int dev_nr = 0;
3203
	unsigned int i;
L
Linus Torvalds 已提交
3204 3205
	int err = 0, r;

3206
	if (!(cam = kzalloc(sizeof(struct sn9c102_device), GFP_KERNEL)))
L
Linus Torvalds 已提交
3207 3208 3209 3210
		return -ENOMEM;

	cam->usbdev = udev;

3211
	if (!(cam->control_buffer = kzalloc(8, GFP_KERNEL))) {
3212
		DBG(1, "kzalloc() failed");
L
Linus Torvalds 已提交
3213 3214 3215 3216 3217
		err = -ENOMEM;
		goto fail;
	}

	if (!(cam->v4ldev = video_device_alloc())) {
3218
		DBG(1, "video_device_alloc() failed");
L
Linus Torvalds 已提交
3219 3220 3221 3222
		err = -ENOMEM;
		goto fail;
	}

3223
	mutex_init(&cam->dev_mutex);
L
Linus Torvalds 已提交
3224 3225

	r = sn9c102_read_reg(cam, 0x00);
3226
	if (r < 0 || (r != 0x10 && r != 0x11 && r != 0x12)) {
3227 3228
		DBG(1, "Sorry, this is not a SN9C1xx-based camera "
		       "(vid:pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
L
Linus Torvalds 已提交
3229 3230 3231 3232
		err = -ENODEV;
		goto fail;
	}

3233
	cam->bridge = id->driver_info;
L
Linus Torvalds 已提交
3234 3235 3236 3237
	switch (cam->bridge) {
	case BRIDGE_SN9C101:
	case BRIDGE_SN9C102:
		DBG(2, "SN9C10[12] PC Camera Controller detected "
3238
		       "(vid:pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
L
Linus Torvalds 已提交
3239 3240 3241
		break;
	case BRIDGE_SN9C103:
		DBG(2, "SN9C103 PC Camera Controller detected "
3242
		       "(vid:pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
3243 3244 3245
		break;
	case BRIDGE_SN9C105:
		DBG(2, "SN9C105 PC Camera Controller detected "
3246
		       "(vid:pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
3247 3248 3249
		break;
	case BRIDGE_SN9C120:
		DBG(2, "SN9C120 PC Camera Controller detected "
3250
		       "(vid:pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
L
Linus Torvalds 已提交
3251 3252 3253 3254 3255 3256 3257 3258 3259
		break;
	}

	for  (i = 0; sn9c102_sensor_table[i]; i++) {
		err = sn9c102_sensor_table[i](cam);
		if (!err)
			break;
	}

3260 3261
	if (!err) {
		DBG(2, "%s image sensor detected", cam->sensor.name);
L
Linus Torvalds 已提交
3262
		DBG(3, "Support for %s maintained by %s",
3263
		    cam->sensor.name, cam->sensor.maintainer);
L
Linus Torvalds 已提交
3264
	} else {
3265
		DBG(1, "No supported image sensor detected");
L
Linus Torvalds 已提交
3266 3267 3268 3269
		err = -ENODEV;
		goto fail;
	}

3270 3271 3272 3273 3274 3275
	if (!(cam->bridge & cam->sensor.supported_bridge)) {
		DBG(1, "Bridge not supported");
		err = -ENODEV;
		goto fail;
	}

L
Linus Torvalds 已提交
3276
	if (sn9c102_init(cam)) {
3277
		DBG(1, "Initialization failed. I will retry on open().");
L
Linus Torvalds 已提交
3278 3279 3280
		cam->state |= DEV_MISCONFIGURED;
	}

3281
	strcpy(cam->v4ldev->name, "SN9C1xx PC Camera");
L
Linus Torvalds 已提交
3282 3283
	cam->v4ldev->owner = THIS_MODULE;
	cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
3284
	cam->v4ldev->hardware = 0;
L
Linus Torvalds 已提交
3285 3286 3287 3288 3289
	cam->v4ldev->fops = &sn9c102_fops;
	cam->v4ldev->minor = video_nr[dev_nr];
	cam->v4ldev->release = video_device_release;
	video_set_drvdata(cam->v4ldev, cam);

3290
	mutex_lock(&cam->dev_mutex);
L
Linus Torvalds 已提交
3291 3292

	err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
3293
				    video_nr[dev_nr]);
L
Linus Torvalds 已提交
3294
	if (err) {
3295
		DBG(1, "V4L2 device registration failed");
L
Linus Torvalds 已提交
3296
		if (err == -ENFILE && video_nr[dev_nr] == -1)
3297
			DBG(1, "Free /dev/videoX node not found");
3298 3299 3300 3301
		video_nr[dev_nr] = -1;
		dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
		mutex_unlock(&cam->dev_mutex);
		goto fail;
L
Linus Torvalds 已提交
3302 3303
	}

3304
	DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor);
L
Linus Torvalds 已提交
3305 3306

	cam->module_param.force_munmap = force_munmap[dev_nr];
3307
	cam->module_param.frame_timeout = frame_timeout[dev_nr];
L
Linus Torvalds 已提交
3308 3309 3310

	dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;

3311
#ifdef CONFIG_VIDEO_ADV_DEBUG
3312
	err = sn9c102_create_sysfs(cam);
3313 3314 3315 3316 3317 3318 3319 3320
	if (!err)
		DBG(2, "Optional device control through 'sysfs' "
		       "interface ready");
	else
		DBG(2, "Failed to create optional 'sysfs' interface for "
		       "device controlling. Error #%d", err);
#else
	DBG(2, "Optional device control through 'sysfs' interface disabled");
3321 3322
	DBG(3, "Compile the kernel with the 'CONFIG_VIDEO_ADV_DEBUG' "
	       "configuration option to enable it.");
3323
#endif
L
Linus Torvalds 已提交
3324 3325 3326

	usb_set_intfdata(intf, cam);

3327
	mutex_unlock(&cam->dev_mutex);
L
Linus Torvalds 已提交
3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350

	return 0;

fail:
	if (cam) {
		kfree(cam->control_buffer);
		if (cam->v4ldev)
			video_device_release(cam->v4ldev);
		kfree(cam);
	}
	return err;
}


static void sn9c102_usb_disconnect(struct usb_interface* intf)
{
	struct sn9c102_device* cam = usb_get_intfdata(intf);

	if (!cam)
		return;

	down_write(&sn9c102_disconnect);

3351
	mutex_lock(&cam->dev_mutex);
L
Linus Torvalds 已提交
3352

3353
	DBG(2, "Disconnecting %s...", cam->v4ldev->name);
L
Linus Torvalds 已提交
3354 3355 3356 3357 3358 3359

	wake_up_interruptible_all(&cam->open);

	if (cam->users) {
		DBG(2, "Device /dev/video%d is open! Deregistration and "
		       "memory deallocation are deferred on close.",
3360
		    cam->v4ldev->minor);
L
Linus Torvalds 已提交
3361 3362 3363 3364
		cam->state |= DEV_MISCONFIGURED;
		sn9c102_stop_transfer(cam);
		cam->state |= DEV_DISCONNECTED;
		wake_up_interruptible(&cam->wait_frame);
3365 3366
		wake_up(&cam->wait_stream);
		usb_get_dev(cam->usbdev);
L
Linus Torvalds 已提交
3367 3368 3369 3370 3371
	} else {
		cam->state |= DEV_DISCONNECTED;
		sn9c102_release_resources(cam);
	}

3372
	mutex_unlock(&cam->dev_mutex);
L
Linus Torvalds 已提交
3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393

	if (!cam->users)
		kfree(cam);

	up_write(&sn9c102_disconnect);
}


static struct usb_driver sn9c102_usb_driver = {
	.name =       "sn9c102",
	.id_table =   sn9c102_id_table,
	.probe =      sn9c102_usb_probe,
	.disconnect = sn9c102_usb_disconnect,
};

/*****************************************************************************/

static int __init sn9c102_module_init(void)
{
	int err = 0;

3394 3395
	KDBG(2, SN9C102_MODULE_NAME " v" SN9C102_MODULE_VERSION);
	KDBG(3, SN9C102_MODULE_AUTHOR);
L
Linus Torvalds 已提交
3396 3397

	if ((err = usb_register(&sn9c102_usb_driver)))
3398
		KDBG(1, "usb_register() failed");
L
Linus Torvalds 已提交
3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411

	return err;
}


static void __exit sn9c102_module_exit(void)
{
	usb_deregister(&sn9c102_usb_driver);
}


module_init(sn9c102_module_init);
module_exit(sn9c102_module_exit);