V4L/DVB (4742): Drivers/media/video: handle sysfs errors
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / media / video / sn9c102 / sn9c102_core.c
CommitLineData
1da177e4
LT
1/***************************************************************************
2 * V4L2 driver for SN9C10x PC Camera Controllers *
3 * *
a966f3e7 4 * Copyright (C) 2004-2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
1da177e4
LT
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
19 ***************************************************************************/
20
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/kernel.h>
24#include <linux/param.h>
25#include <linux/moduleparam.h>
26#include <linux/errno.h>
27#include <linux/slab.h>
1da177e4
LT
28#include <linux/device.h>
29#include <linux/fs.h>
30#include <linux/delay.h>
1da177e4
LT
31#include <linux/compiler.h>
32#include <linux/ioctl.h>
33#include <linux/poll.h>
34#include <linux/stat.h>
35#include <linux/mm.h>
36#include <linux/vmalloc.h>
37#include <linux/page-flags.h>
38#include <linux/byteorder/generic.h>
39#include <asm/page.h>
40#include <asm/uaccess.h>
41
42#include "sn9c102.h"
43
44/*****************************************************************************/
45
cd6fcc55
LR
46#define SN9C102_MODULE_NAME "V4L2 driver for SN9C10x PC Camera Controllers"
47#define SN9C102_MODULE_AUTHOR "(C) 2004-2006 Luca Risolia"
48#define SN9C102_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>"
49#define SN9C102_MODULE_LICENSE "GPL"
2ffab02f
LR
50#define SN9C102_MODULE_VERSION "1:1.27"
51#define SN9C102_MODULE_VERSION_CODE KERNEL_VERSION(1, 0, 27)
cd6fcc55
LR
52
53/*****************************************************************************/
54
1da177e4
LT
55MODULE_DEVICE_TABLE(usb, sn9c102_id_table);
56
57MODULE_AUTHOR(SN9C102_MODULE_AUTHOR " " SN9C102_AUTHOR_EMAIL);
58MODULE_DESCRIPTION(SN9C102_MODULE_NAME);
59MODULE_VERSION(SN9C102_MODULE_VERSION);
60MODULE_LICENSE(SN9C102_MODULE_LICENSE);
61
62static short video_nr[] = {[0 ... SN9C102_MAX_DEVICES-1] = -1};
63module_param_array(video_nr, short, NULL, 0444);
64MODULE_PARM_DESC(video_nr,
d56410e0
MCC
65 "\n<-1|n[,...]> Specify V4L2 minor mode number."
66 "\n -1 = use next available (default)"
67 "\n n = use minor number n (integer >= 0)"
68 "\nYou can specify up to "__MODULE_STRING(SN9C102_MAX_DEVICES)
69 " cameras this way."
70 "\nFor example:"
71 "\nvideo_nr=-1,2,-1 would assign minor number 2 to"
72 "\nthe second camera and use auto for the first"
73 "\none and for every other camera."
74 "\n");
75
76static short force_munmap[] = {[0 ... SN9C102_MAX_DEVICES-1] =
77 SN9C102_FORCE_MUNMAP};
1da177e4
LT
78module_param_array(force_munmap, bool, NULL, 0444);
79MODULE_PARM_DESC(force_munmap,
d56410e0
MCC
80 "\n<0|1[,...]> Force the application to unmap previously"
81 "\nmapped buffer memory before calling any VIDIOC_S_CROP or"
82 "\nVIDIOC_S_FMT ioctl's. Not all the applications support"
83 "\nthis feature. This parameter is specific for each"
84 "\ndetected camera."
85 "\n 0 = do not force memory unmapping"
86 "\n 1 = force memory unmapping (save memory)"
87 "\nDefault value is "__MODULE_STRING(SN9C102_FORCE_MUNMAP)"."
88 "\n");
1da177e4 89
2ffab02f 90static unsigned int frame_timeout[] = {[0 ... SN9C102_MAX_DEVICES-1] =
d56410e0 91 SN9C102_FRAME_TIMEOUT};
2ffab02f
LR
92module_param_array(frame_timeout, uint, NULL, 0644);
93MODULE_PARM_DESC(frame_timeout,
d56410e0
MCC
94 "\n<n[,...]> Timeout for a video frame in seconds."
95 "\nThis parameter is specific for each detected camera."
96 "\nDefault value is "__MODULE_STRING(SN9C102_FRAME_TIMEOUT)"."
97 "\n");
2ffab02f 98
1da177e4
LT
99#ifdef SN9C102_DEBUG
100static unsigned short debug = SN9C102_DEBUG_LEVEL;
101module_param(debug, ushort, 0644);
102MODULE_PARM_DESC(debug,
d56410e0
MCC
103 "\n<n> Debugging information level, from 0 to 3:"
104 "\n0 = none (use carefully)"
105 "\n1 = critical errors"
106 "\n2 = significant informations"
107 "\n3 = more verbose messages"
108 "\nLevel 3 is useful for testing only, when only "
109 "one device is used."
110 "\nDefault value is "__MODULE_STRING(SN9C102_DEBUG_LEVEL)"."
111 "\n");
1da177e4
LT
112#endif
113
114/*****************************************************************************/
115
116static sn9c102_sof_header_t sn9c102_sof_header[] = {
117 {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96, 0x00},
118 {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96, 0x01},
119};
120
a966f3e7
LR
121static sn9c103_sof_header_t sn9c103_sof_header[] = {
122 {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96, 0x20},
123};
1da177e4
LT
124
125static sn9c102_eof_header_t sn9c102_eof_header[] = {
126 {0x00, 0x00, 0x00, 0x00},
127 {0x40, 0x00, 0x00, 0x00},
128 {0x80, 0x00, 0x00, 0x00},
129 {0xc0, 0x00, 0x00, 0x00},
130};
131
132/*****************************************************************************/
133
d56410e0
MCC
134static u32
135sn9c102_request_buffers(struct sn9c102_device* cam, u32 count,
136 enum sn9c102_io_method io)
1da177e4 137{
2ffab02f
LR
138 struct v4l2_pix_format* p = &(cam->sensor.pix_format);
139 struct v4l2_rect* r = &(cam->sensor.cropcap.bounds);
1da177e4 140 const size_t imagesize = cam->module_param.force_munmap ||
d56410e0
MCC
141 io == IO_READ ?
142 (p->width * p->height * p->priv) / 8 :
143 (r->width * r->height * p->priv) / 8;
1da177e4
LT
144 void* buff = NULL;
145 u32 i;
146
147 if (count > SN9C102_MAX_FRAMES)
148 count = SN9C102_MAX_FRAMES;
149
150 cam->nbuffers = count;
151 while (cam->nbuffers > 0) {
cd6fcc55 152 if ((buff = vmalloc_32(cam->nbuffers * PAGE_ALIGN(imagesize))))
1da177e4
LT
153 break;
154 cam->nbuffers--;
155 }
156
157 for (i = 0; i < cam->nbuffers; i++) {
158 cam->frame[i].bufmem = buff + i*PAGE_ALIGN(imagesize);
159 cam->frame[i].buf.index = i;
160 cam->frame[i].buf.m.offset = i*PAGE_ALIGN(imagesize);
161 cam->frame[i].buf.length = imagesize;
162 cam->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
163 cam->frame[i].buf.sequence = 0;
164 cam->frame[i].buf.field = V4L2_FIELD_NONE;
165 cam->frame[i].buf.memory = V4L2_MEMORY_MMAP;
166 cam->frame[i].buf.flags = 0;
167 }
168
169 return cam->nbuffers;
170}
171
172
173static void sn9c102_release_buffers(struct sn9c102_device* cam)
174{
175 if (cam->nbuffers) {
cd6fcc55 176 vfree(cam->frame[0].bufmem);
1da177e4
LT
177 cam->nbuffers = 0;
178 }
a966f3e7 179 cam->frame_current = NULL;
1da177e4
LT
180}
181
182
183static void sn9c102_empty_framequeues(struct sn9c102_device* cam)
184{
185 u32 i;
186
187 INIT_LIST_HEAD(&cam->inqueue);
188 INIT_LIST_HEAD(&cam->outqueue);
189
190 for (i = 0; i < SN9C102_MAX_FRAMES; i++) {
191 cam->frame[i].state = F_UNUSED;
192 cam->frame[i].buf.bytesused = 0;
193 }
194}
195
196
a966f3e7
LR
197static void sn9c102_requeue_outqueue(struct sn9c102_device* cam)
198{
199 struct sn9c102_frame_t *i;
200
201 list_for_each_entry(i, &cam->outqueue, frame) {
202 i->state = F_QUEUED;
203 list_add(&i->frame, &cam->inqueue);
204 }
205
206 INIT_LIST_HEAD(&cam->outqueue);
207}
208
209
1da177e4
LT
210static void sn9c102_queue_unusedframes(struct sn9c102_device* cam)
211{
212 unsigned long lock_flags;
213 u32 i;
214
215 for (i = 0; i < cam->nbuffers; i++)
216 if (cam->frame[i].state == F_UNUSED) {
217 cam->frame[i].state = F_QUEUED;
218 spin_lock_irqsave(&cam->queue_lock, lock_flags);
219 list_add_tail(&cam->frame[i].frame, &cam->inqueue);
220 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
221 }
222}
223
224/*****************************************************************************/
225
a966f3e7
LR
226int sn9c102_write_regs(struct sn9c102_device* cam, u8* buff, u16 index)
227{
228 struct usb_device* udev = cam->usbdev;
229 int i, res;
230
231 if (index + sizeof(buff) >= ARRAY_SIZE(cam->reg))
232 return -1;
233
234 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
d56410e0
MCC
235 index, 0, buff, sizeof(buff),
236 SN9C102_CTRL_TIMEOUT*sizeof(buff));
a966f3e7
LR
237 if (res < 0) {
238 DBG(3, "Failed to write registers (index 0x%02X, error %d)",
239 index, res);
240 return -1;
241 }
242
243 for (i = 0; i < sizeof(buff); i++)
244 cam->reg[index+i] = buff[i];
245
246 return 0;
247}
248
249
1da177e4
LT
250int sn9c102_write_reg(struct sn9c102_device* cam, u8 value, u16 index)
251{
252 struct usb_device* udev = cam->usbdev;
253 u8* buff = cam->control_buffer;
254 int res;
255
a966f3e7
LR
256 if (index >= ARRAY_SIZE(cam->reg))
257 return -1;
258
1da177e4
LT
259 *buff = value;
260
261 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
d56410e0 262 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
1da177e4
LT
263 if (res < 0) {
264 DBG(3, "Failed to write a register (value 0x%02X, index "
a966f3e7 265 "0x%02X, error %d)", value, index, res);
1da177e4
LT
266 return -1;
267 }
268
269 cam->reg[index] = value;
270
271 return 0;
272}
273
274
275/* NOTE: reading some registers always returns 0 */
276static int sn9c102_read_reg(struct sn9c102_device* cam, u16 index)
277{
278 struct usb_device* udev = cam->usbdev;
279 u8* buff = cam->control_buffer;
280 int res;
281
282 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
d56410e0 283 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
1da177e4
LT
284 if (res < 0)
285 DBG(3, "Failed to read a register (index 0x%02X, error %d)",
a966f3e7 286 index, res);
1da177e4
LT
287
288 return (res >= 0) ? (int)(*buff) : -1;
289}
290
291
292int sn9c102_pread_reg(struct sn9c102_device* cam, u16 index)
293{
a966f3e7
LR
294 if (index >= ARRAY_SIZE(cam->reg))
295 return -1;
1da177e4
LT
296
297 return cam->reg[index];
298}
299
300
301static int
302sn9c102_i2c_wait(struct sn9c102_device* cam, struct sn9c102_sensor* sensor)
303{
304 int i, r;
305
306 for (i = 1; i <= 5; i++) {
307 r = sn9c102_read_reg(cam, 0x08);
308 if (r < 0)
309 return -EIO;
310 if (r & 0x04)
311 return 0;
312 if (sensor->frequency & SN9C102_I2C_400KHZ)
313 udelay(5*16);
314 else
315 udelay(16*16);
316 }
317 return -EBUSY;
318}
319
320
321static int
d56410e0
MCC
322sn9c102_i2c_detect_read_error(struct sn9c102_device* cam,
323 struct sn9c102_sensor* sensor)
1da177e4
LT
324{
325 int r;
326 r = sn9c102_read_reg(cam, 0x08);
327 return (r < 0 || (r >= 0 && !(r & 0x08))) ? -EIO : 0;
328}
329
330
331static int
d56410e0
MCC
332sn9c102_i2c_detect_write_error(struct sn9c102_device* cam,
333 struct sn9c102_sensor* sensor)
1da177e4
LT
334{
335 int r;
336 r = sn9c102_read_reg(cam, 0x08);
337 return (r < 0 || (r >= 0 && (r & 0x08))) ? -EIO : 0;
338}
339
340
d56410e0 341int
1da177e4 342sn9c102_i2c_try_raw_read(struct sn9c102_device* cam,
d56410e0
MCC
343 struct sn9c102_sensor* sensor, u8 data0, u8 data1,
344 u8 n, u8 buffer[])
1da177e4
LT
345{
346 struct usb_device* udev = cam->usbdev;
347 u8* data = cam->control_buffer;
348 int err = 0, res;
349
350 /* Write cycle */
351 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
d56410e0 352 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) | 0x10;
1da177e4
LT
353 data[1] = data0; /* I2C slave id */
354 data[2] = data1; /* address */
355 data[7] = 0x10;
356 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
d56410e0 357 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
1da177e4
LT
358 if (res < 0)
359 err += res;
360
361 err += sn9c102_i2c_wait(cam, sensor);
362
363 /* Read cycle - n bytes */
364 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
d56410e0
MCC
365 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) |
366 (n << 4) | 0x02;
1da177e4
LT
367 data[1] = data0;
368 data[7] = 0x10;
369 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
d56410e0 370 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
1da177e4
LT
371 if (res < 0)
372 err += res;
373
374 err += sn9c102_i2c_wait(cam, sensor);
375
376 /* The first read byte will be placed in data[4] */
377 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
d56410e0 378 0x0a, 0, data, 5, SN9C102_CTRL_TIMEOUT);
1da177e4
LT
379 if (res < 0)
380 err += res;
381
382 err += sn9c102_i2c_detect_read_error(cam, sensor);
383
384 PDBGG("I2C read: address 0x%02X, first read byte: 0x%02X", data1,
a966f3e7 385 data[4]);
1da177e4
LT
386
387 if (err) {
a966f3e7 388 DBG(3, "I2C read failed for %s image sensor", sensor->name);
1da177e4
LT
389 return -1;
390 }
391
392 if (buffer)
393 memcpy(buffer, data, sizeof(buffer));
394
395 return (int)data[4];
396}
397
398
d56410e0 399int
1da177e4 400sn9c102_i2c_try_raw_write(struct sn9c102_device* cam,
d56410e0
MCC
401 struct sn9c102_sensor* sensor, u8 n, u8 data0,
402 u8 data1, u8 data2, u8 data3, u8 data4, u8 data5)
1da177e4
LT
403{
404 struct usb_device* udev = cam->usbdev;
405 u8* data = cam->control_buffer;
406 int err = 0, res;
407
408 /* Write cycle. It usually is address + value */
409 data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) |
d56410e0
MCC
410 ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0)
411 | ((n - 1) << 4);
1da177e4
LT
412 data[1] = data0;
413 data[2] = data1;
414 data[3] = data2;
415 data[4] = data3;
416 data[5] = data4;
417 data[6] = data5;
418 data[7] = 0x14;
419 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
d56410e0 420 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT);
1da177e4
LT
421 if (res < 0)
422 err += res;
423
424 err += sn9c102_i2c_wait(cam, sensor);
425 err += sn9c102_i2c_detect_write_error(cam, sensor);
426
427 if (err)
a966f3e7 428 DBG(3, "I2C write failed for %s image sensor", sensor->name);
1da177e4
LT
429
430 PDBGG("I2C raw write: %u bytes, data0 = 0x%02X, data1 = 0x%02X, "
431 "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X",
a966f3e7 432 n, data0, data1, data2, data3, data4, data5);
1da177e4
LT
433
434 return err ? -1 : 0;
435}
436
437
438int
439sn9c102_i2c_try_read(struct sn9c102_device* cam,
d56410e0 440 struct sn9c102_sensor* sensor, u8 address)
1da177e4
LT
441{
442 return sn9c102_i2c_try_raw_read(cam, sensor, sensor->i2c_slave_id,
d56410e0 443 address, 1, NULL);
1da177e4
LT
444}
445
446
b9df978f 447int
1da177e4 448sn9c102_i2c_try_write(struct sn9c102_device* cam,
d56410e0 449 struct sn9c102_sensor* sensor, u8 address, u8 value)
1da177e4 450{
d56410e0
MCC
451 return sn9c102_i2c_try_raw_write(cam, sensor, 3,
452 sensor->i2c_slave_id, address,
453 value, 0, 0, 0);
1da177e4
LT
454}
455
456
457int sn9c102_i2c_read(struct sn9c102_device* cam, u8 address)
458{
2ffab02f 459 return sn9c102_i2c_try_read(cam, &cam->sensor, address);
1da177e4
LT
460}
461
462
463int sn9c102_i2c_write(struct sn9c102_device* cam, u8 address, u8 value)
464{
2ffab02f 465 return sn9c102_i2c_try_write(cam, &cam->sensor, address, value);
1da177e4
LT
466}
467
468/*****************************************************************************/
469
470static void*
471sn9c102_find_sof_header(struct sn9c102_device* cam, void* mem, size_t len)
472{
a966f3e7
LR
473 size_t soflen = 0, i;
474 u8 j, n = 0;
475
476 switch (cam->bridge) {
477 case BRIDGE_SN9C101:
478 case BRIDGE_SN9C102:
479 soflen = sizeof(sn9c102_sof_header_t);
480 n = sizeof(sn9c102_sof_header) / soflen;
481 break;
482 case BRIDGE_SN9C103:
483 soflen = sizeof(sn9c103_sof_header_t);
484 n = sizeof(sn9c103_sof_header) / soflen;
485 }
1da177e4 486
d56410e0 487 for (i = 0; (len >= soflen) && (i <= len - soflen); i++)
1da177e4 488 for (j = 0; j < n; j++)
a966f3e7
LR
489 /* The invariable part of the header is 6 bytes long */
490 if ((cam->bridge != BRIDGE_SN9C103 &&
491 !memcmp(mem + i, sn9c102_sof_header[j], 6)) ||
492 (cam->bridge == BRIDGE_SN9C103 &&
493 !memcmp(mem + i, sn9c103_sof_header[j], 6))) {
1da177e4
LT
494 memcpy(cam->sof_header, mem + i, soflen);
495 /* Skip the header */
496 return mem + i + soflen;
497 }
498
499 return NULL;
500}
501
502
503static void*
504sn9c102_find_eof_header(struct sn9c102_device* cam, void* mem, size_t len)
505{
506 size_t eoflen = sizeof(sn9c102_eof_header_t), i;
507 unsigned j, n = sizeof(sn9c102_eof_header) / eoflen;
508
2ffab02f 509 if (cam->sensor.pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X)
1da177e4
LT
510 return NULL; /* EOF header does not exist in compressed data */
511
512 for (i = 0; (len >= eoflen) && (i <= len - eoflen); i++)
513 for (j = 0; j < n; j++)
514 if (!memcmp(mem + i, sn9c102_eof_header[j], eoflen))
515 return mem + i;
516
517 return NULL;
518}
519
520
7d12e780 521static void sn9c102_urb_complete(struct urb *urb)
1da177e4
LT
522{
523 struct sn9c102_device* cam = urb->context;
524 struct sn9c102_frame_t** f;
a966f3e7 525 size_t imagesize, soflen;
1da177e4
LT
526 u8 i;
527 int err = 0;
528
529 if (urb->status == -ENOENT)
530 return;
531
532 f = &cam->frame_current;
533
534 if (cam->stream == STREAM_INTERRUPT) {
535 cam->stream = STREAM_OFF;
536 if ((*f))
537 (*f)->state = F_QUEUED;
a966f3e7 538 DBG(3, "Stream interrupted");
2ffab02f 539 wake_up(&cam->wait_stream);
1da177e4
LT
540 }
541
542 if (cam->state & DEV_DISCONNECTED)
543 return;
544
545 if (cam->state & DEV_MISCONFIGURED) {
546 wake_up_interruptible(&cam->wait_frame);
547 return;
548 }
549
550 if (cam->stream == STREAM_OFF || list_empty(&cam->inqueue))
551 goto resubmit_urb;
552
553 if (!(*f))
554 (*f) = list_entry(cam->inqueue.next, struct sn9c102_frame_t,
d56410e0 555 frame);
1da177e4 556
2ffab02f 557 imagesize = (cam->sensor.pix_format.width *
d56410e0
MCC
558 cam->sensor.pix_format.height *
559 cam->sensor.pix_format.priv) / 8;
1da177e4 560
a966f3e7 561 soflen = (cam->bridge) == BRIDGE_SN9C103 ?
d56410e0
MCC
562 sizeof(sn9c103_sof_header_t) :
563 sizeof(sn9c102_sof_header_t);
a966f3e7 564
1da177e4
LT
565 for (i = 0; i < urb->number_of_packets; i++) {
566 unsigned int img, len, status;
567 void *pos, *sof, *eof;
568
569 len = urb->iso_frame_desc[i].actual_length;
570 status = urb->iso_frame_desc[i].status;
571 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
572
573 if (status) {
a966f3e7 574 DBG(3, "Error in isochronous frame");
1da177e4
LT
575 (*f)->state = F_ERROR;
576 continue;
577 }
578
a966f3e7 579 PDBGG("Isochrnous frame: length %u, #%u i", len, i);
1da177e4
LT
580
581redo:
582 sof = sn9c102_find_sof_header(cam, pos, len);
2ffab02f 583 if (likely(!sof)) {
1da177e4
LT
584 eof = sn9c102_find_eof_header(cam, pos, len);
585 if ((*f)->state == F_GRABBING) {
586end_of_frame:
587 img = len;
588
589 if (eof)
590 img = (eof > pos) ? eof - pos - 1 : 0;
591
592 if ((*f)->buf.bytesused+img > imagesize) {
2ffab02f
LR
593 u32 b;
594 b = (*f)->buf.bytesused + img -
595 imagesize;
1da177e4
LT
596 img = imagesize - (*f)->buf.bytesused;
597 DBG(3, "Expected EOF not found: "
a966f3e7 598 "video frame cut");
1da177e4
LT
599 if (eof)
600 DBG(3, "Exceeded limit: +%u "
a966f3e7 601 "bytes", (unsigned)(b));
1da177e4
LT
602 }
603
604 memcpy((*f)->bufmem + (*f)->buf.bytesused, pos,
605 img);
606
607 if ((*f)->buf.bytesused == 0)
608 do_gettimeofday(&(*f)->buf.timestamp);
609
610 (*f)->buf.bytesused += img;
611
612 if ((*f)->buf.bytesused == imagesize ||
2ffab02f 613 (cam->sensor.pix_format.pixelformat ==
d56410e0 614 V4L2_PIX_FMT_SN9C10X && eof)) {
2ffab02f
LR
615 u32 b;
616 b = (*f)->buf.bytesused;
1da177e4
LT
617 (*f)->state = F_DONE;
618 (*f)->buf.sequence= ++cam->frame_count;
a966f3e7 619 spin_lock(&cam->queue_lock);
1da177e4 620 list_move_tail(&(*f)->frame,
d56410e0 621 &cam->outqueue);
1da177e4
LT
622 if (!list_empty(&cam->inqueue))
623 (*f) = list_entry(
d56410e0
MCC
624 cam->inqueue.next,
625 struct sn9c102_frame_t,
626 frame );
1da177e4
LT
627 else
628 (*f) = NULL;
a966f3e7 629 spin_unlock(&cam->queue_lock);
1da177e4 630 memcpy(cam->sysfs.frame_header,
a966f3e7
LR
631 cam->sof_header, soflen);
632 DBG(3, "Video frame captured: %lu "
633 "bytes", (unsigned long)(b));
1da177e4
LT
634
635 if (!(*f))
636 goto resubmit_urb;
637
638 } else if (eof) {
639 (*f)->state = F_ERROR;
640 DBG(3, "Not expected EOF after %lu "
d56410e0 641 "bytes of image data",
a966f3e7
LR
642 (unsigned long)
643 ((*f)->buf.bytesused));
1da177e4
LT
644 }
645
646 if (sof) /* (1) */
647 goto start_of_frame;
648
649 } else if (eof) {
a966f3e7 650 DBG(3, "EOF without SOF");
1da177e4
LT
651 continue;
652
653 } else {
a966f3e7 654 PDBGG("Ignoring pointless isochronous frame");
1da177e4
LT
655 continue;
656 }
657
658 } else if ((*f)->state == F_QUEUED || (*f)->state == F_ERROR) {
659start_of_frame:
660 (*f)->state = F_GRABBING;
661 (*f)->buf.bytesused = 0;
662 len -= (sof - pos);
663 pos = sof;
a966f3e7 664 DBG(3, "SOF detected: new video frame");
1da177e4
LT
665 if (len)
666 goto redo;
667
668 } else if ((*f)->state == F_GRABBING) {
669 eof = sn9c102_find_eof_header(cam, pos, len);
670 if (eof && eof < sof)
671 goto end_of_frame; /* (1) */
672 else {
2ffab02f 673 if (cam->sensor.pix_format.pixelformat ==
1da177e4 674 V4L2_PIX_FMT_SN9C10X) {
a966f3e7 675 eof = sof - soflen;
1da177e4
LT
676 goto end_of_frame;
677 } else {
678 DBG(3, "SOF before expected EOF after "
d56410e0 679 "%lu bytes of image data",
a966f3e7
LR
680 (unsigned long)
681 ((*f)->buf.bytesused));
1da177e4
LT
682 goto start_of_frame;
683 }
684 }
685 }
686 }
687
688resubmit_urb:
689 urb->dev = cam->usbdev;
690 err = usb_submit_urb(urb, GFP_ATOMIC);
691 if (err < 0 && err != -EPERM) {
692 cam->state |= DEV_MISCONFIGURED;
a966f3e7 693 DBG(1, "usb_submit_urb() failed");
1da177e4
LT
694 }
695
696 wake_up_interruptible(&cam->wait_frame);
697}
698
699
700static int sn9c102_start_transfer(struct sn9c102_device* cam)
701{
702 struct usb_device *udev = cam->usbdev;
703 struct urb* urb;
a966f3e7 704 const unsigned int sn9c102_wMaxPacketSize[] = {0, 128, 256, 384, 512,
d56410e0 705 680, 800, 900, 1023};
a966f3e7 706 const unsigned int sn9c103_wMaxPacketSize[] = {0, 128, 256, 384, 512,
d56410e0 707 680, 800, 900, 1003};
a966f3e7 708 const unsigned int psz = (cam->bridge == BRIDGE_SN9C103) ?
d56410e0
MCC
709 sn9c103_wMaxPacketSize[SN9C102_ALTERNATE_SETTING] :
710 sn9c102_wMaxPacketSize[SN9C102_ALTERNATE_SETTING];
1da177e4
LT
711 s8 i, j;
712 int err = 0;
713
714 for (i = 0; i < SN9C102_URBS; i++) {
cd6fcc55 715 cam->transfer_buffer[i] = kzalloc(SN9C102_ISO_PACKETS * psz,
d56410e0 716 GFP_KERNEL);
1da177e4
LT
717 if (!cam->transfer_buffer[i]) {
718 err = -ENOMEM;
a966f3e7 719 DBG(1, "Not enough memory");
1da177e4
LT
720 goto free_buffers;
721 }
722 }
723
724 for (i = 0; i < SN9C102_URBS; i++) {
725 urb = usb_alloc_urb(SN9C102_ISO_PACKETS, GFP_KERNEL);
726 cam->urb[i] = urb;
727 if (!urb) {
728 err = -ENOMEM;
a966f3e7 729 DBG(1, "usb_alloc_urb() failed");
1da177e4
LT
730 goto free_urbs;
731 }
732 urb->dev = udev;
733 urb->context = cam;
734 urb->pipe = usb_rcvisocpipe(udev, 1);
735 urb->transfer_flags = URB_ISO_ASAP;
736 urb->number_of_packets = SN9C102_ISO_PACKETS;
737 urb->complete = sn9c102_urb_complete;
738 urb->transfer_buffer = cam->transfer_buffer[i];
739 urb->transfer_buffer_length = psz * SN9C102_ISO_PACKETS;
740 urb->interval = 1;
741 for (j = 0; j < SN9C102_ISO_PACKETS; j++) {
742 urb->iso_frame_desc[j].offset = psz * j;
743 urb->iso_frame_desc[j].length = psz;
744 }
745 }
746
747 /* Enable video */
748 if (!(cam->reg[0x01] & 0x04)) {
749 err = sn9c102_write_reg(cam, cam->reg[0x01] | 0x04, 0x01);
750 if (err) {
751 err = -EIO;
a966f3e7 752 DBG(1, "I/O hardware error");
1da177e4
LT
753 goto free_urbs;
754 }
755 }
756
757 err = usb_set_interface(udev, 0, SN9C102_ALTERNATE_SETTING);
758 if (err) {
a966f3e7 759 DBG(1, "usb_set_interface() failed");
1da177e4
LT
760 goto free_urbs;
761 }
762
763 cam->frame_current = NULL;
764
765 for (i = 0; i < SN9C102_URBS; i++) {
766 err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
767 if (err) {
768 for (j = i-1; j >= 0; j--)
769 usb_kill_urb(cam->urb[j]);
a966f3e7 770 DBG(1, "usb_submit_urb() failed, error %d", err);
1da177e4
LT
771 goto free_urbs;
772 }
773 }
774
775 return 0;
776
777free_urbs:
778 for (i = 0; (i < SN9C102_URBS) && cam->urb[i]; i++)
779 usb_free_urb(cam->urb[i]);
780
781free_buffers:
782 for (i = 0; (i < SN9C102_URBS) && cam->transfer_buffer[i]; i++)
783 kfree(cam->transfer_buffer[i]);
784
785 return err;
786}
787
788
789static int sn9c102_stop_transfer(struct sn9c102_device* cam)
790{
791 struct usb_device *udev = cam->usbdev;
792 s8 i;
793 int err = 0;
794
795 if (cam->state & DEV_DISCONNECTED)
796 return 0;
797
798 for (i = SN9C102_URBS-1; i >= 0; i--) {
799 usb_kill_urb(cam->urb[i]);
800 usb_free_urb(cam->urb[i]);
801 kfree(cam->transfer_buffer[i]);
802 }
803
804 err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */
805 if (err)
a966f3e7 806 DBG(3, "usb_set_interface() failed");
1da177e4
LT
807
808 return err;
809}
810
811
7107627b 812static int sn9c102_stream_interrupt(struct sn9c102_device* cam)
1da177e4 813{
2ffab02f 814 long timeout;
1da177e4
LT
815
816 cam->stream = STREAM_INTERRUPT;
2ffab02f 817 timeout = wait_event_timeout(cam->wait_stream,
d56410e0
MCC
818 (cam->stream == STREAM_OFF) ||
819 (cam->state & DEV_DISCONNECTED),
820 SN9C102_URB_TIMEOUT);
1da177e4
LT
821 if (cam->state & DEV_DISCONNECTED)
822 return -ENODEV;
2ffab02f 823 else if (cam->stream != STREAM_OFF) {
1da177e4 824 cam->state |= DEV_MISCONFIGURED;
2ffab02f
LR
825 DBG(1, "URB timeout reached. The camera is misconfigured. "
826 "To use it, close and open /dev/video%d again.",
827 cam->v4ldev->minor);
828 return -EIO;
1da177e4
LT
829 }
830
831 return 0;
832}
833
834/*****************************************************************************/
835
cd6fcc55 836#ifdef CONFIG_VIDEO_ADV_DEBUG
1da177e4
LT
837static u8 sn9c102_strtou8(const char* buff, size_t len, ssize_t* count)
838{
839 char str[5];
840 char* endp;
841 unsigned long val;
842
843 if (len < 4) {
844 strncpy(str, buff, len);
845 str[len+1] = '\0';
846 } else {
847 strncpy(str, buff, 4);
848 str[4] = '\0';
849 }
850
851 val = simple_strtoul(str, &endp, 0);
852
853 *count = 0;
854 if (val <= 0xff)
855 *count = (ssize_t)(endp - str);
856 if ((*count) && (len == *count+1) && (buff[*count] == '\n'))
857 *count += 1;
858
859 return (u8)val;
860}
861
862/*
863 NOTE 1: being inside one of the following methods implies that the v4l
d56410e0 864 device exists for sure (see kobjects and reference counters)
1da177e4
LT
865 NOTE 2: buffers are PAGE_SIZE long
866*/
867
868static ssize_t sn9c102_show_reg(struct class_device* cd, char* buf)
869{
870 struct sn9c102_device* cam;
871 ssize_t count;
872
4186ecf8 873 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1da177e4
LT
874 return -ERESTARTSYS;
875
876 cam = video_get_drvdata(to_video_device(cd));
877 if (!cam) {
4186ecf8 878 mutex_unlock(&sn9c102_sysfs_lock);
1da177e4
LT
879 return -ENODEV;
880 }
881
882 count = sprintf(buf, "%u\n", cam->sysfs.reg);
883
4186ecf8 884 mutex_unlock(&sn9c102_sysfs_lock);
1da177e4
LT
885
886 return count;
d56410e0 887}
1da177e4
LT
888
889
d56410e0 890static ssize_t
1da177e4
LT
891sn9c102_store_reg(struct class_device* cd, const char* buf, size_t len)
892{
893 struct sn9c102_device* cam;
894 u8 index;
895 ssize_t count;
896
4186ecf8 897 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1da177e4
LT
898 return -ERESTARTSYS;
899
900 cam = video_get_drvdata(to_video_device(cd));
901 if (!cam) {
4186ecf8 902 mutex_unlock(&sn9c102_sysfs_lock);
1da177e4
LT
903 return -ENODEV;
904 }
905
906 index = sn9c102_strtou8(buf, len, &count);
907 if (index > 0x1f || !count) {
4186ecf8 908 mutex_unlock(&sn9c102_sysfs_lock);
1da177e4
LT
909 return -EINVAL;
910 }
911
912 cam->sysfs.reg = index;
913
a966f3e7
LR
914 DBG(2, "Moved SN9C10X register index to 0x%02X", cam->sysfs.reg);
915 DBG(3, "Written bytes: %zd", count);
1da177e4 916
4186ecf8 917 mutex_unlock(&sn9c102_sysfs_lock);
1da177e4
LT
918
919 return count;
920}
921
922
923static ssize_t sn9c102_show_val(struct class_device* cd, char* buf)
924{
925 struct sn9c102_device* cam;
926 ssize_t count;
927 int val;
928
4186ecf8 929 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1da177e4
LT
930 return -ERESTARTSYS;
931
932 cam = video_get_drvdata(to_video_device(cd));
933 if (!cam) {
4186ecf8 934 mutex_unlock(&sn9c102_sysfs_lock);
1da177e4
LT
935 return -ENODEV;
936 }
937
938 if ((val = sn9c102_read_reg(cam, cam->sysfs.reg)) < 0) {
4186ecf8 939 mutex_unlock(&sn9c102_sysfs_lock);
1da177e4
LT
940 return -EIO;
941 }
942
943 count = sprintf(buf, "%d\n", val);
944
a966f3e7 945 DBG(3, "Read bytes: %zd", count);
1da177e4 946
4186ecf8 947 mutex_unlock(&sn9c102_sysfs_lock);
1da177e4
LT
948
949 return count;
d56410e0 950}
1da177e4
LT
951
952
953static ssize_t
954sn9c102_store_val(struct class_device* cd, const char* buf, size_t len)
955{
956 struct sn9c102_device* cam;
957 u8 value;
958 ssize_t count;
959 int err;
960
4186ecf8 961 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1da177e4
LT
962 return -ERESTARTSYS;
963
964 cam = video_get_drvdata(to_video_device(cd));
965 if (!cam) {
4186ecf8 966 mutex_unlock(&sn9c102_sysfs_lock);
1da177e4
LT
967 return -ENODEV;
968 }
969
970 value = sn9c102_strtou8(buf, len, &count);
971 if (!count) {
4186ecf8 972 mutex_unlock(&sn9c102_sysfs_lock);
1da177e4
LT
973 return -EINVAL;
974 }
975
976 err = sn9c102_write_reg(cam, value, cam->sysfs.reg);
977 if (err) {
4186ecf8 978 mutex_unlock(&sn9c102_sysfs_lock);
1da177e4
LT
979 return -EIO;
980 }
981
982 DBG(2, "Written SN9C10X reg. 0x%02X, val. 0x%02X",
a966f3e7
LR
983 cam->sysfs.reg, value);
984 DBG(3, "Written bytes: %zd", count);
1da177e4 985
4186ecf8 986 mutex_unlock(&sn9c102_sysfs_lock);
1da177e4
LT
987
988 return count;
989}
990
991
992static ssize_t sn9c102_show_i2c_reg(struct class_device* cd, char* buf)
993{
994 struct sn9c102_device* cam;
995 ssize_t count;
996
4186ecf8 997 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1da177e4
LT
998 return -ERESTARTSYS;
999
1000 cam = video_get_drvdata(to_video_device(cd));
1001 if (!cam) {
4186ecf8 1002 mutex_unlock(&sn9c102_sysfs_lock);
1da177e4
LT
1003 return -ENODEV;
1004 }
1005
1006 count = sprintf(buf, "%u\n", cam->sysfs.i2c_reg);
1007
a966f3e7 1008 DBG(3, "Read bytes: %zd", count);
1da177e4 1009
4186ecf8 1010 mutex_unlock(&sn9c102_sysfs_lock);
1da177e4
LT
1011
1012 return count;
1013}
1014
1015
d56410e0 1016static ssize_t
1da177e4
LT
1017sn9c102_store_i2c_reg(struct class_device* cd, const char* buf, size_t len)
1018{
1019 struct sn9c102_device* cam;
1020 u8 index;
1021 ssize_t count;
1022
4186ecf8 1023 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1da177e4
LT
1024 return -ERESTARTSYS;
1025
1026 cam = video_get_drvdata(to_video_device(cd));
1027 if (!cam) {
4186ecf8 1028 mutex_unlock(&sn9c102_sysfs_lock);
1da177e4
LT
1029 return -ENODEV;
1030 }
1031
1032 index = sn9c102_strtou8(buf, len, &count);
1033 if (!count) {
4186ecf8 1034 mutex_unlock(&sn9c102_sysfs_lock);
1da177e4
LT
1035 return -EINVAL;
1036 }
1037
1038 cam->sysfs.i2c_reg = index;
1039
a966f3e7
LR
1040 DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg);
1041 DBG(3, "Written bytes: %zd", count);
1da177e4 1042
4186ecf8 1043 mutex_unlock(&sn9c102_sysfs_lock);
1da177e4
LT
1044
1045 return count;
1046}
1047
1048
1049static ssize_t sn9c102_show_i2c_val(struct class_device* cd, char* buf)
1050{
1051 struct sn9c102_device* cam;
1052 ssize_t count;
1053 int val;
1054
4186ecf8 1055 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1da177e4
LT
1056 return -ERESTARTSYS;
1057
1058 cam = video_get_drvdata(to_video_device(cd));
1059 if (!cam) {
4186ecf8 1060 mutex_unlock(&sn9c102_sysfs_lock);
1da177e4
LT
1061 return -ENODEV;
1062 }
1063
2ffab02f 1064 if (!(cam->sensor.sysfs_ops & SN9C102_I2C_READ)) {
4186ecf8 1065 mutex_unlock(&sn9c102_sysfs_lock);
1da177e4
LT
1066 return -ENOSYS;
1067 }
1068
1069 if ((val = sn9c102_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) {
4186ecf8 1070 mutex_unlock(&sn9c102_sysfs_lock);
1da177e4
LT
1071 return -EIO;
1072 }
1073
1074 count = sprintf(buf, "%d\n", val);
1075
a966f3e7 1076 DBG(3, "Read bytes: %zd", count);
1da177e4 1077
4186ecf8 1078 mutex_unlock(&sn9c102_sysfs_lock);
1da177e4
LT
1079
1080 return count;
d56410e0 1081}
1da177e4
LT
1082
1083
1084static ssize_t
1085sn9c102_store_i2c_val(struct class_device* cd, const char* buf, size_t len)
1086{
1087 struct sn9c102_device* cam;
1088 u8 value;
1089 ssize_t count;
1090 int err;
1091
4186ecf8 1092 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1da177e4
LT
1093 return -ERESTARTSYS;
1094
1095 cam = video_get_drvdata(to_video_device(cd));
1096 if (!cam) {
4186ecf8 1097 mutex_unlock(&sn9c102_sysfs_lock);
1da177e4
LT
1098 return -ENODEV;
1099 }
1100
2ffab02f 1101 if (!(cam->sensor.sysfs_ops & SN9C102_I2C_WRITE)) {
4186ecf8 1102 mutex_unlock(&sn9c102_sysfs_lock);
1da177e4
LT
1103 return -ENOSYS;
1104 }
1105
1106 value = sn9c102_strtou8(buf, len, &count);
1107 if (!count) {
4186ecf8 1108 mutex_unlock(&sn9c102_sysfs_lock);
1da177e4
LT
1109 return -EINVAL;
1110 }
1111
1112 err = sn9c102_i2c_write(cam, cam->sysfs.i2c_reg, value);
1113 if (err) {
4186ecf8 1114 mutex_unlock(&sn9c102_sysfs_lock);
1da177e4
LT
1115 return -EIO;
1116 }
1117
1118 DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X",
a966f3e7
LR
1119 cam->sysfs.i2c_reg, value);
1120 DBG(3, "Written bytes: %zd", count);
1da177e4 1121
4186ecf8 1122 mutex_unlock(&sn9c102_sysfs_lock);
1da177e4
LT
1123
1124 return count;
1125}
1126
1127
1128static ssize_t
1129sn9c102_store_green(struct class_device* cd, const char* buf, size_t len)
1130{
1131 struct sn9c102_device* cam;
1132 enum sn9c102_bridge bridge;
1133 ssize_t res = 0;
1134 u8 value;
1135 ssize_t count;
1136
4186ecf8 1137 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1da177e4
LT
1138 return -ERESTARTSYS;
1139
1140 cam = video_get_drvdata(to_video_device(cd));
1141 if (!cam) {
4186ecf8 1142 mutex_unlock(&sn9c102_sysfs_lock);
1da177e4
LT
1143 return -ENODEV;
1144 }
1145
1146 bridge = cam->bridge;
1147
4186ecf8 1148 mutex_unlock(&sn9c102_sysfs_lock);
1da177e4
LT
1149
1150 value = sn9c102_strtou8(buf, len, &count);
1151 if (!count)
1152 return -EINVAL;
1153
1154 switch (bridge) {
1155 case BRIDGE_SN9C101:
1156 case BRIDGE_SN9C102:
1157 if (value > 0x0f)
1158 return -EINVAL;
1159 if ((res = sn9c102_store_reg(cd, "0x11", 4)) >= 0)
1160 res = sn9c102_store_val(cd, buf, len);
1161 break;
1162 case BRIDGE_SN9C103:
1163 if (value > 0x7f)
1164 return -EINVAL;
1165 if ((res = sn9c102_store_reg(cd, "0x04", 4)) >= 0)
1166 res = sn9c102_store_val(cd, buf, len);
1167 break;
1168 }
1169
1170 return res;
1171}
1172
1173
1174static ssize_t
1175sn9c102_store_blue(struct class_device* cd, const char* buf, size_t len)
1176{
1177 ssize_t res = 0;
1178 u8 value;
1179 ssize_t count;
1180
1181 value = sn9c102_strtou8(buf, len, &count);
1182 if (!count || value > 0x7f)
1183 return -EINVAL;
1184
1185 if ((res = sn9c102_store_reg(cd, "0x06", 4)) >= 0)
1186 res = sn9c102_store_val(cd, buf, len);
1187
1188 return res;
1189}
1190
1191
1192static ssize_t
1193sn9c102_store_red(struct class_device* cd, const char* buf, size_t len)
1194{
1195 ssize_t res = 0;
1196 u8 value;
1197 ssize_t count;
1198
1199 value = sn9c102_strtou8(buf, len, &count);
1200 if (!count || value > 0x7f)
1201 return -EINVAL;
1202
1203 if ((res = sn9c102_store_reg(cd, "0x05", 4)) >= 0)
1204 res = sn9c102_store_val(cd, buf, len);
1205
1206 return res;
1207}
1208
1209
1210static ssize_t sn9c102_show_frame_header(struct class_device* cd, char* buf)
1211{
1212 struct sn9c102_device* cam;
1213 ssize_t count;
1214
1215 cam = video_get_drvdata(to_video_device(cd));
1216 if (!cam)
1217 return -ENODEV;
1218
1219 count = sizeof(cam->sysfs.frame_header);
1220 memcpy(buf, cam->sysfs.frame_header, count);
1221
a966f3e7 1222 DBG(3, "Frame header, read bytes: %zd", count);
1da177e4
LT
1223
1224 return count;
d56410e0 1225}
1da177e4
LT
1226
1227
1228static CLASS_DEVICE_ATTR(reg, S_IRUGO | S_IWUSR,
d56410e0 1229 sn9c102_show_reg, sn9c102_store_reg);
1da177e4 1230static CLASS_DEVICE_ATTR(val, S_IRUGO | S_IWUSR,
d56410e0 1231 sn9c102_show_val, sn9c102_store_val);
1da177e4 1232static CLASS_DEVICE_ATTR(i2c_reg, S_IRUGO | S_IWUSR,
d56410e0 1233 sn9c102_show_i2c_reg, sn9c102_store_i2c_reg);
1da177e4 1234static CLASS_DEVICE_ATTR(i2c_val, S_IRUGO | S_IWUSR,
d56410e0 1235 sn9c102_show_i2c_val, sn9c102_store_i2c_val);
1da177e4
LT
1236static CLASS_DEVICE_ATTR(green, S_IWUGO, NULL, sn9c102_store_green);
1237static CLASS_DEVICE_ATTR(blue, S_IWUGO, NULL, sn9c102_store_blue);
1238static CLASS_DEVICE_ATTR(red, S_IWUGO, NULL, sn9c102_store_red);
1239static CLASS_DEVICE_ATTR(frame_header, S_IRUGO,
d56410e0 1240 sn9c102_show_frame_header, NULL);
1da177e4
LT
1241
1242
c12e3be0 1243static int sn9c102_create_sysfs(struct sn9c102_device* cam)
1da177e4
LT
1244{
1245 struct video_device *v4ldev = cam->v4ldev;
c12e3be0
JG
1246 int rc;
1247
1248 rc = video_device_create_file(v4ldev, &class_device_attr_reg);
1249 if (rc) goto err;
1250 rc = video_device_create_file(v4ldev, &class_device_attr_val);
1251 if (rc) goto err_reg;
1252 rc = video_device_create_file(v4ldev, &class_device_attr_frame_header);
1253 if (rc) goto err_val;
1da177e4 1254
2ffab02f 1255 if (cam->sensor.sysfs_ops) {
c12e3be0
JG
1256 rc = video_device_create_file(v4ldev, &class_device_attr_i2c_reg);
1257 if (rc) goto err_frhead;
1258 rc = video_device_create_file(v4ldev, &class_device_attr_i2c_val);
1259 if (rc) goto err_i2c_reg;
1260 }
1261
1262 if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102) {
1263 rc = video_device_create_file(v4ldev, &class_device_attr_green);
1264 if (rc) goto err_i2c_val;
1265 } else if (cam->bridge == BRIDGE_SN9C103) {
1266 rc = video_device_create_file(v4ldev, &class_device_attr_blue);
1267 if (rc) goto err_i2c_val;
1268 rc = video_device_create_file(v4ldev, &class_device_attr_red);
1269 if (rc) goto err_blue;
1da177e4 1270 }
c12e3be0
JG
1271
1272 return 0;
1273
1274err_blue:
1275 video_device_remove_file(v4ldev, &class_device_attr_blue);
1276err_i2c_val:
1277 if (cam->sensor.sysfs_ops)
1278 video_device_remove_file(v4ldev, &class_device_attr_i2c_val);
1279err_i2c_reg:
1280 if (cam->sensor.sysfs_ops)
1281 video_device_remove_file(v4ldev, &class_device_attr_i2c_reg);
1282err_frhead:
1283 video_device_remove_file(v4ldev, &class_device_attr_frame_header);
1284err_val:
1285 video_device_remove_file(v4ldev, &class_device_attr_val);
1286err_reg:
1287 video_device_remove_file(v4ldev, &class_device_attr_reg);
1288err:
1289 return rc;
1da177e4 1290}
cd6fcc55 1291#endif /* CONFIG_VIDEO_ADV_DEBUG */
1da177e4
LT
1292
1293/*****************************************************************************/
1294
1295static int
1296sn9c102_set_pix_format(struct sn9c102_device* cam, struct v4l2_pix_format* pix)
1297{
1298 int err = 0;
1299
1300 if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
1301 err += sn9c102_write_reg(cam, cam->reg[0x18] | 0x80, 0x18);
1302 else
1303 err += sn9c102_write_reg(cam, cam->reg[0x18] & 0x7f, 0x18);
1304
1305 return err ? -EIO : 0;
1306}
1307
1308
1309static int
1310sn9c102_set_compression(struct sn9c102_device* cam,
d56410e0 1311 struct v4l2_jpegcompression* compression)
1da177e4
LT
1312{
1313 int err = 0;
1314
1315 if (compression->quality == 0)
1316 err += sn9c102_write_reg(cam, cam->reg[0x17] | 0x01, 0x17);
1317 else if (compression->quality == 1)
1318 err += sn9c102_write_reg(cam, cam->reg[0x17] & 0xfe, 0x17);
1319
1320 return err ? -EIO : 0;
1321}
1322
1323
1324static int sn9c102_set_scale(struct sn9c102_device* cam, u8 scale)
1325{
1326 u8 r = 0;
1327 int err = 0;
1328
1329 if (scale == 1)
1330 r = cam->reg[0x18] & 0xcf;
1331 else if (scale == 2) {
1332 r = cam->reg[0x18] & 0xcf;
1333 r |= 0x10;
1334 } else if (scale == 4)
1335 r = cam->reg[0x18] | 0x20;
1336
1337 err += sn9c102_write_reg(cam, r, 0x18);
1338 if (err)
1339 return -EIO;
1340
a966f3e7 1341 PDBGG("Scaling factor: %u", scale);
1da177e4
LT
1342
1343 return 0;
1344}
1345
1346
1347static int sn9c102_set_crop(struct sn9c102_device* cam, struct v4l2_rect* rect)
1348{
2ffab02f 1349 struct sn9c102_sensor* s = &cam->sensor;
1da177e4
LT
1350 u8 h_start = (u8)(rect->left - s->cropcap.bounds.left),
1351 v_start = (u8)(rect->top - s->cropcap.bounds.top),
1352 h_size = (u8)(rect->width / 16),
1353 v_size = (u8)(rect->height / 16);
1354 int err = 0;
1355
1356 err += sn9c102_write_reg(cam, h_start, 0x12);
1357 err += sn9c102_write_reg(cam, v_start, 0x13);
1358 err += sn9c102_write_reg(cam, h_size, 0x15);
1359 err += sn9c102_write_reg(cam, v_size, 0x16);
1360 if (err)
1361 return -EIO;
1362
1363 PDBGG("h_start, v_start, h_size, v_size, ho_size, vo_size "
a966f3e7 1364 "%u %u %u %u", h_start, v_start, h_size, v_size);
1da177e4
LT
1365
1366 return 0;
1367}
1368
1369
1370static int sn9c102_init(struct sn9c102_device* cam)
1371{
2ffab02f 1372 struct sn9c102_sensor* s = &cam->sensor;
1da177e4
LT
1373 struct v4l2_control ctrl;
1374 struct v4l2_queryctrl *qctrl;
1375 struct v4l2_rect* rect;
52950ed4 1376 u8 i = 0;
1da177e4
LT
1377 int err = 0;
1378
1379 if (!(cam->state & DEV_INITIALIZED)) {
1380 init_waitqueue_head(&cam->open);
1381 qctrl = s->qctrl;
1382 rect = &(s->cropcap.defrect);
1383 } else { /* use current values */
1384 qctrl = s->_qctrl;
1385 rect = &(s->_rect);
1386 }
1387
1388 err += sn9c102_set_scale(cam, rect->width / s->pix_format.width);
1389 err += sn9c102_set_crop(cam, rect);
1390 if (err)
1391 return err;
1392
1393 if (s->init) {
1394 err = s->init(cam);
1395 if (err) {
a966f3e7 1396 DBG(3, "Sensor initialization failed");
1da177e4
LT
1397 return err;
1398 }
1399 }
1400
1401 if (!(cam->state & DEV_INITIALIZED))
1402 cam->compression.quality = cam->reg[0x17] & 0x01 ? 0 : 1;
1403 else
1404 err += sn9c102_set_compression(cam, &cam->compression);
1405 err += sn9c102_set_pix_format(cam, &s->pix_format);
1406 if (s->set_pix_format)
1407 err += s->set_pix_format(cam, &s->pix_format);
1408 if (err)
1409 return err;
1410
1411 if (s->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X)
52950ed4 1412 DBG(3, "Compressed video format is active, quality %d",
a966f3e7 1413 cam->compression.quality);
1da177e4 1414 else
a966f3e7 1415 DBG(3, "Uncompressed video format is active");
1da177e4
LT
1416
1417 if (s->set_crop)
1418 if ((err = s->set_crop(cam, rect))) {
a966f3e7 1419 DBG(3, "set_crop() failed");
1da177e4
LT
1420 return err;
1421 }
1422
1423 if (s->set_ctrl) {
52950ed4
TK
1424 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1425 if (s->qctrl[i].id != 0 &&
1da177e4
LT
1426 !(s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)) {
1427 ctrl.id = s->qctrl[i].id;
1428 ctrl.value = qctrl[i].default_value;
1429 err = s->set_ctrl(cam, &ctrl);
1430 if (err) {
1431 DBG(3, "Set %s control failed",
a966f3e7 1432 s->qctrl[i].name);
1da177e4
LT
1433 return err;
1434 }
1435 DBG(3, "Image sensor supports '%s' control",
a966f3e7 1436 s->qctrl[i].name);
1da177e4
LT
1437 }
1438 }
1439
1440 if (!(cam->state & DEV_INITIALIZED)) {
4186ecf8 1441 mutex_init(&cam->fileop_mutex);
1da177e4
LT
1442 spin_lock_init(&cam->queue_lock);
1443 init_waitqueue_head(&cam->wait_frame);
1444 init_waitqueue_head(&cam->wait_stream);
1445 cam->nreadbuffers = 2;
1446 memcpy(s->_qctrl, s->qctrl, sizeof(s->qctrl));
52950ed4 1447 memcpy(&(s->_rect), &(s->cropcap.defrect),
1da177e4
LT
1448 sizeof(struct v4l2_rect));
1449 cam->state |= DEV_INITIALIZED;
1450 }
1451
a966f3e7 1452 DBG(2, "Initialization succeeded");
1da177e4
LT
1453 return 0;
1454}
1455
1456
1457static void sn9c102_release_resources(struct sn9c102_device* cam)
1458{
4186ecf8 1459 mutex_lock(&sn9c102_sysfs_lock);
1da177e4 1460
a966f3e7 1461 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor);
1da177e4
LT
1462 video_set_drvdata(cam->v4ldev, NULL);
1463 video_unregister_device(cam->v4ldev);
1464
2ffab02f
LR
1465 usb_put_dev(cam->usbdev);
1466
4186ecf8 1467 mutex_unlock(&sn9c102_sysfs_lock);
1da177e4
LT
1468
1469 kfree(cam->control_buffer);
1470}
1471
1472/*****************************************************************************/
1473
1474static int sn9c102_open(struct inode* inode, struct file* filp)
1475{
1476 struct sn9c102_device* cam;
1477 int err = 0;
1478
1479 /*
1480 This is the only safe way to prevent race conditions with
1481 disconnect
1482 */
1483 if (!down_read_trylock(&sn9c102_disconnect))
1484 return -ERESTARTSYS;
1485
1486 cam = video_get_drvdata(video_devdata(filp));
1487
4186ecf8 1488 if (mutex_lock_interruptible(&cam->dev_mutex)) {
1da177e4
LT
1489 up_read(&sn9c102_disconnect);
1490 return -ERESTARTSYS;
1491 }
1492
1493 if (cam->users) {
a966f3e7 1494 DBG(2, "Device /dev/video%d is busy...", cam->v4ldev->minor);
1da177e4
LT
1495 if ((filp->f_flags & O_NONBLOCK) ||
1496 (filp->f_flags & O_NDELAY)) {
1497 err = -EWOULDBLOCK;
1498 goto out;
1499 }
4186ecf8 1500 mutex_unlock(&cam->dev_mutex);
1da177e4 1501 err = wait_event_interruptible_exclusive(cam->open,
d56410e0
MCC
1502 cam->state & DEV_DISCONNECTED
1503 || !cam->users);
1da177e4
LT
1504 if (err) {
1505 up_read(&sn9c102_disconnect);
1506 return err;
1507 }
1508 if (cam->state & DEV_DISCONNECTED) {
1509 up_read(&sn9c102_disconnect);
1510 return -ENODEV;
1511 }
4186ecf8 1512 mutex_lock(&cam->dev_mutex);
1da177e4
LT
1513 }
1514
1515
1516 if (cam->state & DEV_MISCONFIGURED) {
1517 err = sn9c102_init(cam);
1518 if (err) {
1519 DBG(1, "Initialization failed again. "
a966f3e7 1520 "I will retry on next open().");
1da177e4
LT
1521 goto out;
1522 }
1523 cam->state &= ~DEV_MISCONFIGURED;
1524 }
1525
1526 if ((err = sn9c102_start_transfer(cam)))
1527 goto out;
1528
1529 filp->private_data = cam;
1530 cam->users++;
1531 cam->io = IO_NONE;
1532 cam->stream = STREAM_OFF;
1533 cam->nbuffers = 0;
1534 cam->frame_count = 0;
1535 sn9c102_empty_framequeues(cam);
1536
a966f3e7 1537 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor);
1da177e4
LT
1538
1539out:
4186ecf8 1540 mutex_unlock(&cam->dev_mutex);
1da177e4
LT
1541 up_read(&sn9c102_disconnect);
1542 return err;
1543}
1544
1545
1546static int sn9c102_release(struct inode* inode, struct file* filp)
1547{
1548 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1549
4186ecf8 1550 mutex_lock(&cam->dev_mutex); /* prevent disconnect() to be called */
1da177e4
LT
1551
1552 sn9c102_stop_transfer(cam);
1553
1554 sn9c102_release_buffers(cam);
1555
1556 if (cam->state & DEV_DISCONNECTED) {
1557 sn9c102_release_resources(cam);
4186ecf8 1558 mutex_unlock(&cam->dev_mutex);
1da177e4
LT
1559 kfree(cam);
1560 return 0;
1561 }
1562
1563 cam->users--;
1564 wake_up_interruptible_nr(&cam->open, 1);
1565
a966f3e7 1566 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor);
1da177e4 1567
4186ecf8 1568 mutex_unlock(&cam->dev_mutex);
1da177e4
LT
1569
1570 return 0;
1571}
1572
1573
1574static ssize_t
1575sn9c102_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
1576{
1577 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1578 struct sn9c102_frame_t* f, * i;
1579 unsigned long lock_flags;
2ffab02f 1580 long timeout;
1da177e4
LT
1581 int err = 0;
1582
4186ecf8 1583 if (mutex_lock_interruptible(&cam->fileop_mutex))
1da177e4
LT
1584 return -ERESTARTSYS;
1585
1586 if (cam->state & DEV_DISCONNECTED) {
a966f3e7 1587 DBG(1, "Device not present");
4186ecf8 1588 mutex_unlock(&cam->fileop_mutex);
1da177e4
LT
1589 return -ENODEV;
1590 }
1591
1592 if (cam->state & DEV_MISCONFIGURED) {
a966f3e7
LR
1593 DBG(1, "The camera is misconfigured. Close and open it "
1594 "again.");
4186ecf8 1595 mutex_unlock(&cam->fileop_mutex);
1da177e4
LT
1596 return -EIO;
1597 }
1598
1599 if (cam->io == IO_MMAP) {
1600 DBG(3, "Close and open the device again to choose "
a966f3e7 1601 "the read method");
4186ecf8 1602 mutex_unlock(&cam->fileop_mutex);
1da177e4
LT
1603 return -EINVAL;
1604 }
1605
1606 if (cam->io == IO_NONE) {
1607 if (!sn9c102_request_buffers(cam,cam->nreadbuffers, IO_READ)) {
a966f3e7 1608 DBG(1, "read() failed, not enough memory");
4186ecf8 1609 mutex_unlock(&cam->fileop_mutex);
1da177e4
LT
1610 return -ENOMEM;
1611 }
1612 cam->io = IO_READ;
1613 cam->stream = STREAM_ON;
a966f3e7
LR
1614 }
1615
1616 if (list_empty(&cam->inqueue)) {
1617 if (!list_empty(&cam->outqueue))
1618 sn9c102_empty_framequeues(cam);
1da177e4
LT
1619 sn9c102_queue_unusedframes(cam);
1620 }
1621
1622 if (!count) {
4186ecf8 1623 mutex_unlock(&cam->fileop_mutex);
1da177e4
LT
1624 return 0;
1625 }
1626
1627 if (list_empty(&cam->outqueue)) {
1628 if (filp->f_flags & O_NONBLOCK) {
4186ecf8 1629 mutex_unlock(&cam->fileop_mutex);
1da177e4
LT
1630 return -EAGAIN;
1631 }
2ffab02f 1632 timeout = wait_event_interruptible_timeout
d56410e0
MCC
1633 ( cam->wait_frame,
1634 (!list_empty(&cam->outqueue)) ||
1635 (cam->state & DEV_DISCONNECTED) ||
1636 (cam->state & DEV_MISCONFIGURED),
1637 cam->module_param.frame_timeout *
1638 1000 * msecs_to_jiffies(1) );
2ffab02f 1639 if (timeout < 0) {
4186ecf8 1640 mutex_unlock(&cam->fileop_mutex);
2ffab02f 1641 return timeout;
1da177e4
LT
1642 }
1643 if (cam->state & DEV_DISCONNECTED) {
4186ecf8 1644 mutex_unlock(&cam->fileop_mutex);
1da177e4
LT
1645 return -ENODEV;
1646 }
2ffab02f 1647 if (!timeout || (cam->state & DEV_MISCONFIGURED)) {
4186ecf8 1648 mutex_unlock(&cam->fileop_mutex);
1da177e4
LT
1649 return -EIO;
1650 }
1651 }
1652
1653 f = list_entry(cam->outqueue.prev, struct sn9c102_frame_t, frame);
1654
a966f3e7
LR
1655 if (count > f->buf.bytesused)
1656 count = f->buf.bytesused;
1657
1658 if (copy_to_user(buf, f->bufmem, count)) {
1659 err = -EFAULT;
1660 goto exit;
1661 }
1662 *f_pos += count;
1663
1664exit:
1da177e4
LT
1665 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1666 list_for_each_entry(i, &cam->outqueue, frame)
1667 i->state = F_UNUSED;
1668 INIT_LIST_HEAD(&cam->outqueue);
1669 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1670
1671 sn9c102_queue_unusedframes(cam);
1672
a966f3e7
LR
1673 PDBGG("Frame #%lu, bytes read: %zu",
1674 (unsigned long)f->buf.index, count);
1da177e4 1675
4186ecf8 1676 mutex_unlock(&cam->fileop_mutex);
1da177e4
LT
1677
1678 return count;
1679}
1680
1681
1682static unsigned int sn9c102_poll(struct file *filp, poll_table *wait)
1683{
1684 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
a966f3e7
LR
1685 struct sn9c102_frame_t* f;
1686 unsigned long lock_flags;
1da177e4
LT
1687 unsigned int mask = 0;
1688
4186ecf8 1689 if (mutex_lock_interruptible(&cam->fileop_mutex))
1da177e4
LT
1690 return POLLERR;
1691
1692 if (cam->state & DEV_DISCONNECTED) {
a966f3e7 1693 DBG(1, "Device not present");
1da177e4
LT
1694 goto error;
1695 }
1696
1697 if (cam->state & DEV_MISCONFIGURED) {
a966f3e7
LR
1698 DBG(1, "The camera is misconfigured. Close and open it "
1699 "again.");
1da177e4
LT
1700 goto error;
1701 }
1702
1703 if (cam->io == IO_NONE) {
1704 if (!sn9c102_request_buffers(cam, cam->nreadbuffers,
d56410e0 1705 IO_READ)) {
a966f3e7 1706 DBG(1, "poll() failed, not enough memory");
1da177e4
LT
1707 goto error;
1708 }
1709 cam->io = IO_READ;
1710 cam->stream = STREAM_ON;
1711 }
1712
a966f3e7
LR
1713 if (cam->io == IO_READ) {
1714 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1715 list_for_each_entry(f, &cam->outqueue, frame)
1716 f->state = F_UNUSED;
1717 INIT_LIST_HEAD(&cam->outqueue);
1718 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1da177e4 1719 sn9c102_queue_unusedframes(cam);
a966f3e7 1720 }
1da177e4
LT
1721
1722 poll_wait(filp, &cam->wait_frame, wait);
1723
1724 if (!list_empty(&cam->outqueue))
1725 mask |= POLLIN | POLLRDNORM;
1726
4186ecf8 1727 mutex_unlock(&cam->fileop_mutex);
1da177e4
LT
1728
1729 return mask;
1730
1731error:
4186ecf8 1732 mutex_unlock(&cam->fileop_mutex);
1da177e4
LT
1733 return POLLERR;
1734}
1735
1736
1737static void sn9c102_vm_open(struct vm_area_struct* vma)
1738{
1739 struct sn9c102_frame_t* f = vma->vm_private_data;
1740 f->vma_use_count++;
1741}
1742
1743
1744static void sn9c102_vm_close(struct vm_area_struct* vma)
1745{
1746 /* NOTE: buffers are not freed here */
1747 struct sn9c102_frame_t* f = vma->vm_private_data;
1748 f->vma_use_count--;
1749}
1750
1751
1752static struct vm_operations_struct sn9c102_vm_ops = {
1753 .open = sn9c102_vm_open,
1754 .close = sn9c102_vm_close,
1755};
1756
1757
1758static int sn9c102_mmap(struct file* filp, struct vm_area_struct *vma)
1759{
1760 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1761 unsigned long size = vma->vm_end - vma->vm_start,
d56410e0 1762 start = vma->vm_start;
cd6fcc55 1763 void *pos;
1da177e4
LT
1764 u32 i;
1765
4186ecf8 1766 if (mutex_lock_interruptible(&cam->fileop_mutex))
1da177e4
LT
1767 return -ERESTARTSYS;
1768
1769 if (cam->state & DEV_DISCONNECTED) {
a966f3e7 1770 DBG(1, "Device not present");
4186ecf8 1771 mutex_unlock(&cam->fileop_mutex);
1da177e4
LT
1772 return -ENODEV;
1773 }
1774
1775 if (cam->state & DEV_MISCONFIGURED) {
a966f3e7
LR
1776 DBG(1, "The camera is misconfigured. Close and open it "
1777 "again.");
4186ecf8 1778 mutex_unlock(&cam->fileop_mutex);
1da177e4
LT
1779 return -EIO;
1780 }
1781
1782 if (cam->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
1783 size != PAGE_ALIGN(cam->frame[0].buf.length)) {
4186ecf8 1784 mutex_unlock(&cam->fileop_mutex);
1da177e4
LT
1785 return -EINVAL;
1786 }
1787
1788 for (i = 0; i < cam->nbuffers; i++) {
1789 if ((cam->frame[i].buf.m.offset>>PAGE_SHIFT) == vma->vm_pgoff)
1790 break;
1791 }
1792 if (i == cam->nbuffers) {
4186ecf8 1793 mutex_unlock(&cam->fileop_mutex);
1da177e4
LT
1794 return -EINVAL;
1795 }
1796
1da177e4 1797 vma->vm_flags |= VM_IO;
cd6fcc55 1798 vma->vm_flags |= VM_RESERVED;
1da177e4 1799
cd6fcc55 1800 pos = cam->frame[i].bufmem;
1da177e4 1801 while (size > 0) { /* size is page-aligned */
cd6fcc55 1802 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
4186ecf8 1803 mutex_unlock(&cam->fileop_mutex);
1da177e4
LT
1804 return -EAGAIN;
1805 }
1806 start += PAGE_SIZE;
1807 pos += PAGE_SIZE;
1808 size -= PAGE_SIZE;
1809 }
1810
1811 vma->vm_ops = &sn9c102_vm_ops;
1812 vma->vm_private_data = &cam->frame[i];
1813
1814 sn9c102_vm_open(vma);
1815
4186ecf8 1816 mutex_unlock(&cam->fileop_mutex);
1da177e4
LT
1817
1818 return 0;
1819}
1820
a966f3e7 1821/*****************************************************************************/
1da177e4 1822
a966f3e7
LR
1823static int
1824sn9c102_vidioc_querycap(struct sn9c102_device* cam, void __user * arg)
1da177e4 1825{
a966f3e7
LR
1826 struct v4l2_capability cap = {
1827 .driver = "sn9c102",
1828 .version = SN9C102_MODULE_VERSION_CODE,
1829 .capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
d56410e0 1830 V4L2_CAP_STREAMING,
a966f3e7
LR
1831 };
1832
1833 strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card));
1834 if (usb_make_path(cam->usbdev, cap.bus_info, sizeof(cap.bus_info)) < 0)
cd6fcc55 1835 strlcpy(cap.bus_info, cam->usbdev->dev.bus_id,
d56410e0 1836 sizeof(cap.bus_info));
a966f3e7
LR
1837
1838 if (copy_to_user(arg, &cap, sizeof(cap)))
1839 return -EFAULT;
1da177e4 1840
a966f3e7
LR
1841 return 0;
1842}
1da177e4 1843
1da177e4 1844
a966f3e7
LR
1845static int
1846sn9c102_vidioc_enuminput(struct sn9c102_device* cam, void __user * arg)
1847{
1848 struct v4l2_input i;
1da177e4 1849
a966f3e7
LR
1850 if (copy_from_user(&i, arg, sizeof(i)))
1851 return -EFAULT;
1da177e4 1852
a966f3e7
LR
1853 if (i.index)
1854 return -EINVAL;
1da177e4 1855
a966f3e7 1856 memset(&i, 0, sizeof(i));
cd6fcc55 1857 strcpy(i.name, "Camera");
2ffab02f 1858 i.type = V4L2_INPUT_TYPE_CAMERA;
1da177e4 1859
a966f3e7
LR
1860 if (copy_to_user(arg, &i, sizeof(i)))
1861 return -EFAULT;
1da177e4 1862
a966f3e7
LR
1863 return 0;
1864}
1da177e4 1865
1da177e4 1866
a966f3e7 1867static int
2ffab02f
LR
1868sn9c102_vidioc_g_input(struct sn9c102_device* cam, void __user * arg)
1869{
1870 int index = 0;
1871
1872 if (copy_to_user(arg, &index, sizeof(index)))
1873 return -EFAULT;
1874
1875 return 0;
1876}
1877
1878
1879static int
1880sn9c102_vidioc_s_input(struct sn9c102_device* cam, void __user * arg)
a966f3e7
LR
1881{
1882 int index;
1da177e4 1883
a966f3e7
LR
1884 if (copy_from_user(&index, arg, sizeof(index)))
1885 return -EFAULT;
1da177e4 1886
a966f3e7 1887 if (index != 0)
1da177e4 1888 return -EINVAL;
1da177e4 1889
a966f3e7
LR
1890 return 0;
1891}
1da177e4 1892
1da177e4 1893
a966f3e7
LR
1894static int
1895sn9c102_vidioc_query_ctrl(struct sn9c102_device* cam, void __user * arg)
1896{
2ffab02f 1897 struct sn9c102_sensor* s = &cam->sensor;
a966f3e7
LR
1898 struct v4l2_queryctrl qc;
1899 u8 i;
1da177e4 1900
a966f3e7
LR
1901 if (copy_from_user(&qc, arg, sizeof(qc)))
1902 return -EFAULT;
1da177e4 1903
a966f3e7
LR
1904 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1905 if (qc.id && qc.id == s->qctrl[i].id) {
1906 memcpy(&qc, &(s->qctrl[i]), sizeof(qc));
1907 if (copy_to_user(arg, &qc, sizeof(qc)))
1908 return -EFAULT;
1909 return 0;
1910 }
1da177e4 1911
a966f3e7
LR
1912 return -EINVAL;
1913}
1da177e4 1914
1da177e4 1915
a966f3e7
LR
1916static int
1917sn9c102_vidioc_g_ctrl(struct sn9c102_device* cam, void __user * arg)
1918{
2ffab02f 1919 struct sn9c102_sensor* s = &cam->sensor;
a966f3e7
LR
1920 struct v4l2_control ctrl;
1921 int err = 0;
1922 u8 i;
1da177e4 1923
a966f3e7
LR
1924 if (!s->get_ctrl && !s->set_ctrl)
1925 return -EINVAL;
1da177e4 1926
a966f3e7
LR
1927 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1928 return -EFAULT;
1929
1930 if (!s->get_ctrl) {
52950ed4 1931 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
a966f3e7
LR
1932 if (ctrl.id && ctrl.id == s->qctrl[i].id) {
1933 ctrl.value = s->_qctrl[i].default_value;
1934 goto exit;
1da177e4 1935 }
a966f3e7
LR
1936 return -EINVAL;
1937 } else
1938 err = s->get_ctrl(cam, &ctrl);
1da177e4 1939
a966f3e7
LR
1940exit:
1941 if (copy_to_user(arg, &ctrl, sizeof(ctrl)))
1942 return -EFAULT;
1da177e4 1943
a966f3e7
LR
1944 return err;
1945}
1da177e4 1946
1da177e4 1947
a966f3e7
LR
1948static int
1949sn9c102_vidioc_s_ctrl(struct sn9c102_device* cam, void __user * arg)
1950{
2ffab02f 1951 struct sn9c102_sensor* s = &cam->sensor;
a966f3e7
LR
1952 struct v4l2_control ctrl;
1953 u8 i;
1954 int err = 0;
1da177e4 1955
a966f3e7
LR
1956 if (!s->set_ctrl)
1957 return -EINVAL;
1da177e4 1958
a966f3e7
LR
1959 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1960 return -EFAULT;
1da177e4 1961
a966f3e7
LR
1962 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1963 if (ctrl.id == s->qctrl[i].id) {
2ffab02f
LR
1964 if (s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)
1965 return -EINVAL;
a966f3e7
LR
1966 if (ctrl.value < s->qctrl[i].minimum ||
1967 ctrl.value > s->qctrl[i].maximum)
1968 return -ERANGE;
1969 ctrl.value -= ctrl.value % s->qctrl[i].step;
1970 break;
1971 }
1da177e4 1972
a966f3e7
LR
1973 if ((err = s->set_ctrl(cam, &ctrl)))
1974 return err;
1da177e4 1975
a966f3e7 1976 s->_qctrl[i].default_value = ctrl.value;
1da177e4 1977
a966f3e7
LR
1978 PDBGG("VIDIOC_S_CTRL: id %lu, value %lu",
1979 (unsigned long)ctrl.id, (unsigned long)ctrl.value);
1da177e4 1980
a966f3e7
LR
1981 return 0;
1982}
1da177e4 1983
1da177e4 1984
a966f3e7
LR
1985static int
1986sn9c102_vidioc_cropcap(struct sn9c102_device* cam, void __user * arg)
1987{
2ffab02f 1988 struct v4l2_cropcap* cc = &(cam->sensor.cropcap);
1da177e4 1989
a966f3e7
LR
1990 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1991 cc->pixelaspect.numerator = 1;
1992 cc->pixelaspect.denominator = 1;
1da177e4 1993
a966f3e7
LR
1994 if (copy_to_user(arg, cc, sizeof(*cc)))
1995 return -EFAULT;
1da177e4 1996
a966f3e7
LR
1997 return 0;
1998}
1da177e4 1999
1da177e4 2000
a966f3e7
LR
2001static int
2002sn9c102_vidioc_g_crop(struct sn9c102_device* cam, void __user * arg)
2003{
2ffab02f 2004 struct sn9c102_sensor* s = &cam->sensor;
a966f3e7
LR
2005 struct v4l2_crop crop = {
2006 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
2007 };
1da177e4 2008
a966f3e7 2009 memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect));
1da177e4 2010
a966f3e7
LR
2011 if (copy_to_user(arg, &crop, sizeof(crop)))
2012 return -EFAULT;
1da177e4 2013
a966f3e7
LR
2014 return 0;
2015}
1da177e4 2016
1da177e4 2017
a966f3e7
LR
2018static int
2019sn9c102_vidioc_s_crop(struct sn9c102_device* cam, void __user * arg)
2020{
2ffab02f 2021 struct sn9c102_sensor* s = &cam->sensor;
a966f3e7
LR
2022 struct v4l2_crop crop;
2023 struct v4l2_rect* rect;
2024 struct v4l2_rect* bounds = &(s->cropcap.bounds);
2025 struct v4l2_pix_format* pix_format = &(s->pix_format);
2026 u8 scale;
2027 const enum sn9c102_stream_state stream = cam->stream;
2028 const u32 nbuffers = cam->nbuffers;
2029 u32 i;
2030 int err = 0;
1da177e4 2031
a966f3e7
LR
2032 if (copy_from_user(&crop, arg, sizeof(crop)))
2033 return -EFAULT;
1da177e4 2034
a966f3e7 2035 rect = &(crop.c);
1da177e4 2036
a966f3e7
LR
2037 if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2038 return -EINVAL;
1da177e4 2039
a966f3e7
LR
2040 if (cam->module_param.force_munmap)
2041 for (i = 0; i < cam->nbuffers; i++)
2042 if (cam->frame[i].vma_use_count) {
2043 DBG(3, "VIDIOC_S_CROP failed. "
2044 "Unmap the buffers first.");
2045 return -EINVAL;
2046 }
1da177e4 2047
a966f3e7
LR
2048 /* Preserve R,G or B origin */
2049 rect->left = (s->_rect.left & 1L) ? rect->left | 1L : rect->left & ~1L;
2050 rect->top = (s->_rect.top & 1L) ? rect->top | 1L : rect->top & ~1L;
2051
2052 if (rect->width < 16)
2053 rect->width = 16;
2054 if (rect->height < 16)
2055 rect->height = 16;
2056 if (rect->width > bounds->width)
2057 rect->width = bounds->width;
2058 if (rect->height > bounds->height)
2059 rect->height = bounds->height;
2060 if (rect->left < bounds->left)
2061 rect->left = bounds->left;
2062 if (rect->top < bounds->top)
2063 rect->top = bounds->top;
2064 if (rect->left + rect->width > bounds->left + bounds->width)
2065 rect->left = bounds->left+bounds->width - rect->width;
2066 if (rect->top + rect->height > bounds->top + bounds->height)
2067 rect->top = bounds->top+bounds->height - rect->height;
2068
2069 rect->width &= ~15L;
2070 rect->height &= ~15L;
2071
2072 if (SN9C102_PRESERVE_IMGSCALE) {
2073 /* Calculate the actual scaling factor */
2074 u32 a, b;
2075 a = rect->width * rect->height;
2076 b = pix_format->width * pix_format->height;
2077 scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
2078 } else
2079 scale = 1;
2080
2081 if (cam->stream == STREAM_ON)
2082 if ((err = sn9c102_stream_interrupt(cam)))
2083 return err;
1da177e4 2084
a966f3e7
LR
2085 if (copy_to_user(arg, &crop, sizeof(crop))) {
2086 cam->stream = stream;
2087 return -EFAULT;
1da177e4
LT
2088 }
2089
a966f3e7
LR
2090 if (cam->module_param.force_munmap || cam->io == IO_READ)
2091 sn9c102_release_buffers(cam);
1da177e4 2092
a966f3e7
LR
2093 err = sn9c102_set_crop(cam, rect);
2094 if (s->set_crop)
2095 err += s->set_crop(cam, rect);
2096 err += sn9c102_set_scale(cam, scale);
1da177e4 2097
a966f3e7
LR
2098 if (err) { /* atomic, no rollback in ioctl() */
2099 cam->state |= DEV_MISCONFIGURED;
2100 DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To "
2101 "use the camera, close and open /dev/video%d again.",
2102 cam->v4ldev->minor);
2103 return -EIO;
2104 }
1da177e4 2105
a966f3e7
LR
2106 s->pix_format.width = rect->width/scale;
2107 s->pix_format.height = rect->height/scale;
2108 memcpy(&(s->_rect), rect, sizeof(*rect));
1da177e4 2109
a966f3e7
LR
2110 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
2111 nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->io)) {
2112 cam->state |= DEV_MISCONFIGURED;
2113 DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To "
2114 "use the camera, close and open /dev/video%d again.",
2115 cam->v4ldev->minor);
2116 return -ENOMEM;
1da177e4
LT
2117 }
2118
a966f3e7
LR
2119 if (cam->io == IO_READ)
2120 sn9c102_empty_framequeues(cam);
2121 else if (cam->module_param.force_munmap)
2122 sn9c102_requeue_outqueue(cam);
1da177e4 2123
a966f3e7 2124 cam->stream = stream;
1da177e4 2125
a966f3e7
LR
2126 return 0;
2127}
1da177e4 2128
1da177e4 2129
a966f3e7
LR
2130static int
2131sn9c102_vidioc_enum_fmt(struct sn9c102_device* cam, void __user * arg)
2132{
2133 struct v4l2_fmtdesc fmtd;
1da177e4 2134
a966f3e7
LR
2135 if (copy_from_user(&fmtd, arg, sizeof(fmtd)))
2136 return -EFAULT;
1da177e4 2137
a966f3e7
LR
2138 if (fmtd.index == 0) {
2139 strcpy(fmtd.description, "bayer rgb");
2140 fmtd.pixelformat = V4L2_PIX_FMT_SBGGR8;
2141 } else if (fmtd.index == 1) {
2142 strcpy(fmtd.description, "compressed");
2143 fmtd.pixelformat = V4L2_PIX_FMT_SN9C10X;
2144 fmtd.flags = V4L2_FMT_FLAG_COMPRESSED;
2145 } else
2146 return -EINVAL;
1da177e4 2147
a966f3e7
LR
2148 fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2149 memset(&fmtd.reserved, 0, sizeof(fmtd.reserved));
1da177e4 2150
a966f3e7
LR
2151 if (copy_to_user(arg, &fmtd, sizeof(fmtd)))
2152 return -EFAULT;
1da177e4 2153
a966f3e7
LR
2154 return 0;
2155}
1da177e4 2156
1da177e4 2157
a966f3e7
LR
2158static int
2159sn9c102_vidioc_g_fmt(struct sn9c102_device* cam, void __user * arg)
2160{
2161 struct v4l2_format format;
2ffab02f 2162 struct v4l2_pix_format* pfmt = &(cam->sensor.pix_format);
1da177e4 2163
a966f3e7
LR
2164 if (copy_from_user(&format, arg, sizeof(format)))
2165 return -EFAULT;
1da177e4 2166
a966f3e7
LR
2167 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2168 return -EINVAL;
1da177e4 2169
a966f3e7 2170 pfmt->bytesperline = (pfmt->pixelformat==V4L2_PIX_FMT_SN9C10X)
d56410e0 2171 ? 0 : (pfmt->width * pfmt->priv) / 8;
a966f3e7
LR
2172 pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8);
2173 pfmt->field = V4L2_FIELD_NONE;
2174 memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt));
1da177e4 2175
a966f3e7
LR
2176 if (copy_to_user(arg, &format, sizeof(format)))
2177 return -EFAULT;
1da177e4 2178
a966f3e7
LR
2179 return 0;
2180}
1da177e4 2181
1da177e4 2182
a966f3e7
LR
2183static int
2184sn9c102_vidioc_try_s_fmt(struct sn9c102_device* cam, unsigned int cmd,
d56410e0 2185 void __user * arg)
a966f3e7 2186{
2ffab02f 2187 struct sn9c102_sensor* s = &cam->sensor;
a966f3e7
LR
2188 struct v4l2_format format;
2189 struct v4l2_pix_format* pix;
2190 struct v4l2_pix_format* pfmt = &(s->pix_format);
2191 struct v4l2_rect* bounds = &(s->cropcap.bounds);
2192 struct v4l2_rect rect;
2193 u8 scale;
2194 const enum sn9c102_stream_state stream = cam->stream;
2195 const u32 nbuffers = cam->nbuffers;
2196 u32 i;
2197 int err = 0;
1da177e4 2198
a966f3e7
LR
2199 if (copy_from_user(&format, arg, sizeof(format)))
2200 return -EFAULT;
1da177e4 2201
a966f3e7 2202 pix = &(format.fmt.pix);
1da177e4 2203
a966f3e7
LR
2204 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2205 return -EINVAL;
1da177e4 2206
a966f3e7 2207 memcpy(&rect, &(s->_rect), sizeof(rect));
1da177e4 2208
a966f3e7
LR
2209 { /* calculate the actual scaling factor */
2210 u32 a, b;
2211 a = rect.width * rect.height;
2212 b = pix->width * pix->height;
2213 scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
1da177e4
LT
2214 }
2215
a966f3e7
LR
2216 rect.width = scale * pix->width;
2217 rect.height = scale * pix->height;
1da177e4 2218
a966f3e7
LR
2219 if (rect.width < 16)
2220 rect.width = 16;
2221 if (rect.height < 16)
2222 rect.height = 16;
2223 if (rect.width > bounds->left + bounds->width - rect.left)
2224 rect.width = bounds->left + bounds->width - rect.left;
2225 if (rect.height > bounds->top + bounds->height - rect.top)
2226 rect.height = bounds->top + bounds->height - rect.top;
1da177e4 2227
a966f3e7
LR
2228 rect.width &= ~15L;
2229 rect.height &= ~15L;
1da177e4 2230
a966f3e7
LR
2231 { /* adjust the scaling factor */
2232 u32 a, b;
2233 a = rect.width * rect.height;
2234 b = pix->width * pix->height;
2235 scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
2236 }
2237
2238 pix->width = rect.width / scale;
2239 pix->height = rect.height / scale;
2240
2241 if (pix->pixelformat != V4L2_PIX_FMT_SN9C10X &&
2242 pix->pixelformat != V4L2_PIX_FMT_SBGGR8)
2243 pix->pixelformat = pfmt->pixelformat;
2244 pix->priv = pfmt->priv; /* bpp */
2245 pix->colorspace = pfmt->colorspace;
2246 pix->bytesperline = (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
d56410e0 2247 ? 0 : (pix->width * pix->priv) / 8;
a966f3e7
LR
2248 pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8);
2249 pix->field = V4L2_FIELD_NONE;
2250
2251 if (cmd == VIDIOC_TRY_FMT) {
2252 if (copy_to_user(arg, &format, sizeof(format)))
2253 return -EFAULT;
2254 return 0;
2255 }
1da177e4 2256
a966f3e7 2257 if (cam->module_param.force_munmap)
1da177e4
LT
2258 for (i = 0; i < cam->nbuffers; i++)
2259 if (cam->frame[i].vma_use_count) {
a966f3e7
LR
2260 DBG(3, "VIDIOC_S_FMT failed. Unmap the "
2261 "buffers first.");
1da177e4
LT
2262 return -EINVAL;
2263 }
2264
a966f3e7
LR
2265 if (cam->stream == STREAM_ON)
2266 if ((err = sn9c102_stream_interrupt(cam)))
2267 return err;
1da177e4 2268
a966f3e7
LR
2269 if (copy_to_user(arg, &format, sizeof(format))) {
2270 cam->stream = stream;
2271 return -EFAULT;
2272 }
1da177e4 2273
a966f3e7 2274 if (cam->module_param.force_munmap || cam->io == IO_READ)
1da177e4 2275 sn9c102_release_buffers(cam);
1da177e4 2276
a966f3e7
LR
2277 err += sn9c102_set_pix_format(cam, pix);
2278 err += sn9c102_set_crop(cam, &rect);
2279 if (s->set_pix_format)
2280 err += s->set_pix_format(cam, pix);
2281 if (s->set_crop)
2282 err += s->set_crop(cam, &rect);
2283 err += sn9c102_set_scale(cam, scale);
1da177e4 2284
a966f3e7
LR
2285 if (err) { /* atomic, no rollback in ioctl() */
2286 cam->state |= DEV_MISCONFIGURED;
2287 DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To "
2288 "use the camera, close and open /dev/video%d again.",
2289 cam->v4ldev->minor);
2290 return -EIO;
2291 }
1da177e4 2292
a966f3e7
LR
2293 memcpy(pfmt, pix, sizeof(*pix));
2294 memcpy(&(s->_rect), &rect, sizeof(rect));
2295
2296 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
2297 nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->io)) {
2298 cam->state |= DEV_MISCONFIGURED;
2299 DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To "
2300 "use the camera, close and open /dev/video%d again.",
2301 cam->v4ldev->minor);
2302 return -ENOMEM;
1da177e4
LT
2303 }
2304
a966f3e7
LR
2305 if (cam->io == IO_READ)
2306 sn9c102_empty_framequeues(cam);
2307 else if (cam->module_param.force_munmap)
2308 sn9c102_requeue_outqueue(cam);
1da177e4 2309
a966f3e7 2310 cam->stream = stream;
1da177e4 2311
a966f3e7
LR
2312 return 0;
2313}
2314
2315
2316static int
2317sn9c102_vidioc_g_jpegcomp(struct sn9c102_device* cam, void __user * arg)
2318{
2319 if (copy_to_user(arg, &cam->compression,
d56410e0 2320 sizeof(cam->compression)))
a966f3e7 2321 return -EFAULT;
1da177e4 2322
a966f3e7
LR
2323 return 0;
2324}
1da177e4 2325
1da177e4 2326
a966f3e7
LR
2327static int
2328sn9c102_vidioc_s_jpegcomp(struct sn9c102_device* cam, void __user * arg)
2329{
2330 struct v4l2_jpegcompression jc;
2331 const enum sn9c102_stream_state stream = cam->stream;
2332 int err = 0;
1da177e4 2333
a966f3e7
LR
2334 if (copy_from_user(&jc, arg, sizeof(jc)))
2335 return -EFAULT;
1da177e4 2336
a966f3e7
LR
2337 if (jc.quality != 0 && jc.quality != 1)
2338 return -EINVAL;
2339
2340 if (cam->stream == STREAM_ON)
2341 if ((err = sn9c102_stream_interrupt(cam)))
2342 return err;
2343
2344 err += sn9c102_set_compression(cam, &jc);
2345 if (err) { /* atomic, no rollback in ioctl() */
2346 cam->state |= DEV_MISCONFIGURED;
2347 DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
2348 "problems. To use the camera, close and open "
2349 "/dev/video%d again.", cam->v4ldev->minor);
2350 return -EIO;
1da177e4
LT
2351 }
2352
a966f3e7 2353 cam->compression.quality = jc.quality;
1da177e4 2354
a966f3e7 2355 cam->stream = stream;
1da177e4 2356
a966f3e7
LR
2357 return 0;
2358}
1da177e4 2359
a966f3e7
LR
2360
2361static int
2362sn9c102_vidioc_reqbufs(struct sn9c102_device* cam, void __user * arg)
2363{
2364 struct v4l2_requestbuffers rb;
2365 u32 i;
2366 int err;
2367
2368 if (copy_from_user(&rb, arg, sizeof(rb)))
2369 return -EFAULT;
2370
2371 if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2372 rb.memory != V4L2_MEMORY_MMAP)
2373 return -EINVAL;
2374
2375 if (cam->io == IO_READ) {
2376 DBG(3, "Close and open the device again to choose the mmap "
2377 "I/O method");
2378 return -EINVAL;
2379 }
2380
2381 for (i = 0; i < cam->nbuffers; i++)
2382 if (cam->frame[i].vma_use_count) {
2383 DBG(3, "VIDIOC_REQBUFS failed. Previous buffers are "
2384 "still mapped.");
1da177e4 2385 return -EINVAL;
a966f3e7 2386 }
1da177e4 2387
a966f3e7
LR
2388 if (cam->stream == STREAM_ON)
2389 if ((err = sn9c102_stream_interrupt(cam)))
2390 return err;
1da177e4 2391
a966f3e7 2392 sn9c102_empty_framequeues(cam);
1da177e4 2393
a966f3e7
LR
2394 sn9c102_release_buffers(cam);
2395 if (rb.count)
2396 rb.count = sn9c102_request_buffers(cam, rb.count, IO_MMAP);
1da177e4 2397
a966f3e7
LR
2398 if (copy_to_user(arg, &rb, sizeof(rb))) {
2399 sn9c102_release_buffers(cam);
2400 cam->io = IO_NONE;
2401 return -EFAULT;
1da177e4
LT
2402 }
2403
a966f3e7 2404 cam->io = rb.count ? IO_MMAP : IO_NONE;
1da177e4 2405
a966f3e7
LR
2406 return 0;
2407}
1da177e4 2408
1da177e4 2409
a966f3e7
LR
2410static int
2411sn9c102_vidioc_querybuf(struct sn9c102_device* cam, void __user * arg)
2412{
2413 struct v4l2_buffer b;
1da177e4 2414
a966f3e7
LR
2415 if (copy_from_user(&b, arg, sizeof(b)))
2416 return -EFAULT;
1da177e4 2417
a966f3e7
LR
2418 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2419 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2420 return -EINVAL;
1da177e4 2421
a966f3e7 2422 memcpy(&b, &cam->frame[b.index].buf, sizeof(b));
1da177e4 2423
a966f3e7
LR
2424 if (cam->frame[b.index].vma_use_count)
2425 b.flags |= V4L2_BUF_FLAG_MAPPED;
1da177e4 2426
a966f3e7
LR
2427 if (cam->frame[b.index].state == F_DONE)
2428 b.flags |= V4L2_BUF_FLAG_DONE;
2429 else if (cam->frame[b.index].state != F_UNUSED)
2430 b.flags |= V4L2_BUF_FLAG_QUEUED;
1da177e4 2431
a966f3e7
LR
2432 if (copy_to_user(arg, &b, sizeof(b)))
2433 return -EFAULT;
1da177e4 2434
a966f3e7
LR
2435 return 0;
2436}
1da177e4 2437
1da177e4 2438
a966f3e7
LR
2439static int
2440sn9c102_vidioc_qbuf(struct sn9c102_device* cam, void __user * arg)
2441{
2442 struct v4l2_buffer b;
2443 unsigned long lock_flags;
1da177e4 2444
a966f3e7
LR
2445 if (copy_from_user(&b, arg, sizeof(b)))
2446 return -EFAULT;
1da177e4 2447
a966f3e7
LR
2448 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2449 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2450 return -EINVAL;
1da177e4 2451
a966f3e7
LR
2452 if (cam->frame[b.index].state != F_UNUSED)
2453 return -EINVAL;
1da177e4 2454
a966f3e7 2455 cam->frame[b.index].state = F_QUEUED;
1da177e4 2456
a966f3e7
LR
2457 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2458 list_add_tail(&cam->frame[b.index].frame, &cam->inqueue);
2459 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1da177e4 2460
a966f3e7 2461 PDBGG("Frame #%lu queued", (unsigned long)b.index);
1da177e4 2462
a966f3e7
LR
2463 return 0;
2464}
1da177e4 2465
1da177e4 2466
a966f3e7
LR
2467static int
2468sn9c102_vidioc_dqbuf(struct sn9c102_device* cam, struct file* filp,
d56410e0 2469 void __user * arg)
a966f3e7
LR
2470{
2471 struct v4l2_buffer b;
2472 struct sn9c102_frame_t *f;
2473 unsigned long lock_flags;
2ffab02f 2474 long timeout;
1da177e4 2475
a966f3e7
LR
2476 if (copy_from_user(&b, arg, sizeof(b)))
2477 return -EFAULT;
1da177e4 2478
a966f3e7
LR
2479 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2480 return -EINVAL;
2481
2482 if (list_empty(&cam->outqueue)) {
2483 if (cam->stream == STREAM_OFF)
2484 return -EINVAL;
2485 if (filp->f_flags & O_NONBLOCK)
2486 return -EAGAIN;
2ffab02f 2487 timeout = wait_event_interruptible_timeout
d56410e0
MCC
2488 ( cam->wait_frame,
2489 (!list_empty(&cam->outqueue)) ||
2490 (cam->state & DEV_DISCONNECTED) ||
2491 (cam->state & DEV_MISCONFIGURED),
2492 cam->module_param.frame_timeout *
2493 1000 * msecs_to_jiffies(1) );
2ffab02f
LR
2494 if (timeout < 0)
2495 return timeout;
a966f3e7
LR
2496 if (cam->state & DEV_DISCONNECTED)
2497 return -ENODEV;
2ffab02f 2498 if (!timeout || (cam->state & DEV_MISCONFIGURED))
a966f3e7 2499 return -EIO;
1da177e4
LT
2500 }
2501
a966f3e7
LR
2502 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2503 f = list_entry(cam->outqueue.next, struct sn9c102_frame_t, frame);
2504 list_del(cam->outqueue.next);
2505 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1da177e4 2506
a966f3e7 2507 f->state = F_UNUSED;
1da177e4 2508
a966f3e7
LR
2509 memcpy(&b, &f->buf, sizeof(b));
2510 if (f->vma_use_count)
2511 b.flags |= V4L2_BUF_FLAG_MAPPED;
2512
2513 if (copy_to_user(arg, &b, sizeof(b)))
2514 return -EFAULT;
2515
2516 PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index);
2517
2518 return 0;
2519}
2520
2521
2522static int
2523sn9c102_vidioc_streamon(struct sn9c102_device* cam, void __user * arg)
2524{
2525 int type;
2526
2527 if (copy_from_user(&type, arg, sizeof(type)))
2528 return -EFAULT;
2529
2530 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2531 return -EINVAL;
2532
2533 if (list_empty(&cam->inqueue))
2534 return -EINVAL;
2535
2536 cam->stream = STREAM_ON;
2537
2538 DBG(3, "Stream on");
2539
2540 return 0;
2541}
2542
2543
2544static int
2545sn9c102_vidioc_streamoff(struct sn9c102_device* cam, void __user * arg)
2546{
2547 int type, err;
2548
2549 if (copy_from_user(&type, arg, sizeof(type)))
2550 return -EFAULT;
2551
2552 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2553 return -EINVAL;
2554
2555 if (cam->stream == STREAM_ON)
2556 if ((err = sn9c102_stream_interrupt(cam)))
2557 return err;
2558
2559 sn9c102_empty_framequeues(cam);
2560
2561 DBG(3, "Stream off");
2562
2563 return 0;
2564}
2565
2566
2567static int
2568sn9c102_vidioc_g_parm(struct sn9c102_device* cam, void __user * arg)
2569{
2570 struct v4l2_streamparm sp;
2571
2572 if (copy_from_user(&sp, arg, sizeof(sp)))
2573 return -EFAULT;
2574
2575 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2576 return -EINVAL;
2577
2578 sp.parm.capture.extendedmode = 0;
2579 sp.parm.capture.readbuffers = cam->nreadbuffers;
2580
2581 if (copy_to_user(arg, &sp, sizeof(sp)))
2582 return -EFAULT;
2583
2584 return 0;
2585}
2586
2587
2588static int
2589sn9c102_vidioc_s_parm(struct sn9c102_device* cam, void __user * arg)
2590{
2591 struct v4l2_streamparm sp;
2592
2593 if (copy_from_user(&sp, arg, sizeof(sp)))
2594 return -EFAULT;
2595
2596 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2597 return -EINVAL;
2598
2599 sp.parm.capture.extendedmode = 0;
1da177e4 2600
a966f3e7 2601 if (sp.parm.capture.readbuffers == 0)
1da177e4
LT
2602 sp.parm.capture.readbuffers = cam->nreadbuffers;
2603
a966f3e7
LR
2604 if (sp.parm.capture.readbuffers > SN9C102_MAX_FRAMES)
2605 sp.parm.capture.readbuffers = SN9C102_MAX_FRAMES;
1da177e4 2606
a966f3e7
LR
2607 if (copy_to_user(arg, &sp, sizeof(sp)))
2608 return -EFAULT;
1da177e4 2609
a966f3e7 2610 cam->nreadbuffers = sp.parm.capture.readbuffers;
1da177e4 2611
a966f3e7
LR
2612 return 0;
2613}
1da177e4 2614
1da177e4 2615
a966f3e7 2616static int sn9c102_ioctl_v4l2(struct inode* inode, struct file* filp,
d56410e0 2617 unsigned int cmd, void __user * arg)
a966f3e7
LR
2618{
2619 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1da177e4 2620
a966f3e7 2621 switch (cmd) {
1da177e4 2622
a966f3e7
LR
2623 case VIDIOC_QUERYCAP:
2624 return sn9c102_vidioc_querycap(cam, arg);
1da177e4 2625
a966f3e7
LR
2626 case VIDIOC_ENUMINPUT:
2627 return sn9c102_vidioc_enuminput(cam, arg);
1da177e4 2628
a966f3e7 2629 case VIDIOC_G_INPUT:
2ffab02f
LR
2630 return sn9c102_vidioc_g_input(cam, arg);
2631
a966f3e7 2632 case VIDIOC_S_INPUT:
2ffab02f 2633 return sn9c102_vidioc_s_input(cam, arg);
1da177e4 2634
a966f3e7
LR
2635 case VIDIOC_QUERYCTRL:
2636 return sn9c102_vidioc_query_ctrl(cam, arg);
2637
2638 case VIDIOC_G_CTRL:
2639 return sn9c102_vidioc_g_ctrl(cam, arg);
2640
a966f3e7
LR
2641 case VIDIOC_S_CTRL:
2642 return sn9c102_vidioc_s_ctrl(cam, arg);
2643
a966f3e7
LR
2644 case VIDIOC_CROPCAP:
2645 return sn9c102_vidioc_cropcap(cam, arg);
2646
2647 case VIDIOC_G_CROP:
2648 return sn9c102_vidioc_g_crop(cam, arg);
2649
2650 case VIDIOC_S_CROP:
2651 return sn9c102_vidioc_s_crop(cam, arg);
2652
2653 case VIDIOC_ENUM_FMT:
2654 return sn9c102_vidioc_enum_fmt(cam, arg);
2655
2656 case VIDIOC_G_FMT:
2657 return sn9c102_vidioc_g_fmt(cam, arg);
2658
2659 case VIDIOC_TRY_FMT:
2660 case VIDIOC_S_FMT:
2661 return sn9c102_vidioc_try_s_fmt(cam, cmd, arg);
2662
2663 case VIDIOC_G_JPEGCOMP:
2664 return sn9c102_vidioc_g_jpegcomp(cam, arg);
2665
2666 case VIDIOC_S_JPEGCOMP:
2667 return sn9c102_vidioc_s_jpegcomp(cam, arg);
2668
2669 case VIDIOC_REQBUFS:
2670 return sn9c102_vidioc_reqbufs(cam, arg);
2671
2672 case VIDIOC_QUERYBUF:
2673 return sn9c102_vidioc_querybuf(cam, arg);
2674
2675 case VIDIOC_QBUF:
2676 return sn9c102_vidioc_qbuf(cam, arg);
2677
2678 case VIDIOC_DQBUF:
2679 return sn9c102_vidioc_dqbuf(cam, filp, arg);
2680
2681 case VIDIOC_STREAMON:
2682 return sn9c102_vidioc_streamon(cam, arg);
2683
2684 case VIDIOC_STREAMOFF:
2685 return sn9c102_vidioc_streamoff(cam, arg);
2686
2687 case VIDIOC_G_PARM:
2688 return sn9c102_vidioc_g_parm(cam, arg);
2689
a966f3e7
LR
2690 case VIDIOC_S_PARM:
2691 return sn9c102_vidioc_s_parm(cam, arg);
1da177e4
LT
2692
2693 case VIDIOC_G_STD:
2694 case VIDIOC_S_STD:
2695 case VIDIOC_QUERYSTD:
2696 case VIDIOC_ENUMSTD:
2697 case VIDIOC_QUERYMENU:
2698 return -EINVAL;
2699
2700 default:
2701 return -EINVAL;
2702
2703 }
2704}
2705
2706
2707static int sn9c102_ioctl(struct inode* inode, struct file* filp,
d56410e0 2708 unsigned int cmd, unsigned long arg)
1da177e4
LT
2709{
2710 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
2711 int err = 0;
2712
4186ecf8 2713 if (mutex_lock_interruptible(&cam->fileop_mutex))
1da177e4
LT
2714 return -ERESTARTSYS;
2715
2716 if (cam->state & DEV_DISCONNECTED) {
a966f3e7 2717 DBG(1, "Device not present");
4186ecf8 2718 mutex_unlock(&cam->fileop_mutex);
1da177e4
LT
2719 return -ENODEV;
2720 }
2721
2722 if (cam->state & DEV_MISCONFIGURED) {
a966f3e7
LR
2723 DBG(1, "The camera is misconfigured. Close and open it "
2724 "again.");
4186ecf8 2725 mutex_unlock(&cam->fileop_mutex);
1da177e4
LT
2726 return -EIO;
2727 }
2728
cd6fcc55
LR
2729 V4LDBG(3, "sn9c102", cmd);
2730
1da177e4
LT
2731 err = sn9c102_ioctl_v4l2(inode, filp, cmd, (void __user *)arg);
2732
4186ecf8 2733 mutex_unlock(&cam->fileop_mutex);
1da177e4
LT
2734
2735 return err;
2736}
2737
a966f3e7 2738/*****************************************************************************/
1da177e4
LT
2739
2740static struct file_operations sn9c102_fops = {
a966f3e7 2741 .owner = THIS_MODULE,
1da177e4
LT
2742 .open = sn9c102_open,
2743 .release = sn9c102_release,
2744 .ioctl = sn9c102_ioctl,
2745 .read = sn9c102_read,
2746 .poll = sn9c102_poll,
2747 .mmap = sn9c102_mmap,
2748 .llseek = no_llseek,
2749};
2750
2751/*****************************************************************************/
2752
2753/* It exists a single interface only. We do not need to validate anything. */
2754static int
2755sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2756{
2757 struct usb_device *udev = interface_to_usbdev(intf);
2758 struct sn9c102_device* cam;
2759 static unsigned int dev_nr = 0;
a966f3e7 2760 unsigned int i;
1da177e4
LT
2761 int err = 0, r;
2762
cd6fcc55 2763 if (!(cam = kzalloc(sizeof(struct sn9c102_device), GFP_KERNEL)))
1da177e4 2764 return -ENOMEM;
1da177e4
LT
2765
2766 cam->usbdev = udev;
1da177e4 2767
cd6fcc55 2768 if (!(cam->control_buffer = kzalloc(8, GFP_KERNEL))) {
a966f3e7 2769 DBG(1, "kmalloc() failed");
1da177e4
LT
2770 err = -ENOMEM;
2771 goto fail;
2772 }
1da177e4
LT
2773
2774 if (!(cam->v4ldev = video_device_alloc())) {
a966f3e7 2775 DBG(1, "video_device_alloc() failed");
1da177e4
LT
2776 err = -ENOMEM;
2777 goto fail;
2778 }
2779
4186ecf8 2780 mutex_init(&cam->dev_mutex);
1da177e4
LT
2781
2782 r = sn9c102_read_reg(cam, 0x00);
2783 if (r < 0 || r != 0x10) {
2784 DBG(1, "Sorry, this is not a SN9C10x based camera "
a966f3e7 2785 "(vid/pid 0x%04X/0x%04X)", id->idVendor, id->idProduct);
1da177e4
LT
2786 err = -ENODEV;
2787 goto fail;
2788 }
2789
a966f3e7 2790 cam->bridge = (id->idProduct & 0xffc0) == 0x6080 ?
d56410e0 2791 BRIDGE_SN9C103 : BRIDGE_SN9C102;
1da177e4
LT
2792 switch (cam->bridge) {
2793 case BRIDGE_SN9C101:
2794 case BRIDGE_SN9C102:
2795 DBG(2, "SN9C10[12] PC Camera Controller detected "
a966f3e7 2796 "(vid/pid 0x%04X/0x%04X)", id->idVendor, id->idProduct);
1da177e4
LT
2797 break;
2798 case BRIDGE_SN9C103:
2799 DBG(2, "SN9C103 PC Camera Controller detected "
a966f3e7 2800 "(vid/pid 0x%04X/0x%04X)", id->idVendor, id->idProduct);
1da177e4
LT
2801 break;
2802 }
2803
2804 for (i = 0; sn9c102_sensor_table[i]; i++) {
2805 err = sn9c102_sensor_table[i](cam);
2806 if (!err)
2807 break;
2808 }
2809
2ffab02f
LR
2810 if (!err) {
2811 DBG(2, "%s image sensor detected", cam->sensor.name);
1da177e4 2812 DBG(3, "Support for %s maintained by %s",
2ffab02f 2813 cam->sensor.name, cam->sensor.maintainer);
1da177e4 2814 } else {
a966f3e7 2815 DBG(1, "No supported image sensor detected");
1da177e4
LT
2816 err = -ENODEV;
2817 goto fail;
2818 }
2819
2820 if (sn9c102_init(cam)) {
a966f3e7 2821 DBG(1, "Initialization failed. I will retry on open().");
1da177e4
LT
2822 cam->state |= DEV_MISCONFIGURED;
2823 }
2824
2825 strcpy(cam->v4ldev->name, "SN9C10x PC Camera");
2826 cam->v4ldev->owner = THIS_MODULE;
2827 cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
cd6fcc55 2828 cam->v4ldev->hardware = 0;
1da177e4
LT
2829 cam->v4ldev->fops = &sn9c102_fops;
2830 cam->v4ldev->minor = video_nr[dev_nr];
2831 cam->v4ldev->release = video_device_release;
2832 video_set_drvdata(cam->v4ldev, cam);
2833
4186ecf8 2834 mutex_lock(&cam->dev_mutex);
1da177e4
LT
2835
2836 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
d56410e0 2837 video_nr[dev_nr]);
1da177e4 2838 if (err) {
a966f3e7 2839 DBG(1, "V4L2 device registration failed");
1da177e4 2840 if (err == -ENFILE && video_nr[dev_nr] == -1)
a966f3e7 2841 DBG(1, "Free /dev/videoX node not found");
c12e3be0 2842 goto fail2;
1da177e4
LT
2843 }
2844
a966f3e7 2845 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor);
1da177e4
LT
2846
2847 cam->module_param.force_munmap = force_munmap[dev_nr];
2ffab02f 2848 cam->module_param.frame_timeout = frame_timeout[dev_nr];
1da177e4
LT
2849
2850 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
2851
cd6fcc55 2852#ifdef CONFIG_VIDEO_ADV_DEBUG
c12e3be0
JG
2853 err = sn9c102_create_sysfs(cam);
2854 if (err)
2855 goto fail3;
a966f3e7 2856 DBG(2, "Optional device control through 'sysfs' interface ready");
cd6fcc55 2857#endif
1da177e4
LT
2858
2859 usb_set_intfdata(intf, cam);
2860
4186ecf8 2861 mutex_unlock(&cam->dev_mutex);
1da177e4
LT
2862
2863 return 0;
2864
c12e3be0
JG
2865#ifdef CONFIG_VIDEO_ADV_DEBUG
2866fail3:
2867 video_unregister_device(cam->v4ldev);
2868#endif
2869fail2:
2870 video_nr[dev_nr] = -1;
2871 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
2872 mutex_unlock(&cam->dev_mutex);
1da177e4
LT
2873fail:
2874 if (cam) {
2875 kfree(cam->control_buffer);
2876 if (cam->v4ldev)
2877 video_device_release(cam->v4ldev);
2878 kfree(cam);
2879 }
2880 return err;
2881}
2882
2883
2884static void sn9c102_usb_disconnect(struct usb_interface* intf)
2885{
2886 struct sn9c102_device* cam = usb_get_intfdata(intf);
2887
2888 if (!cam)
2889 return;
2890
2891 down_write(&sn9c102_disconnect);
2892
4186ecf8 2893 mutex_lock(&cam->dev_mutex);
1da177e4 2894
a966f3e7 2895 DBG(2, "Disconnecting %s...", cam->v4ldev->name);
1da177e4
LT
2896
2897 wake_up_interruptible_all(&cam->open);
2898
2899 if (cam->users) {
2900 DBG(2, "Device /dev/video%d is open! Deregistration and "
2901 "memory deallocation are deferred on close.",
a966f3e7 2902 cam->v4ldev->minor);
1da177e4
LT
2903 cam->state |= DEV_MISCONFIGURED;
2904 sn9c102_stop_transfer(cam);
2905 cam->state |= DEV_DISCONNECTED;
2906 wake_up_interruptible(&cam->wait_frame);
2ffab02f
LR
2907 wake_up(&cam->wait_stream);
2908 usb_get_dev(cam->usbdev);
1da177e4
LT
2909 } else {
2910 cam->state |= DEV_DISCONNECTED;
2911 sn9c102_release_resources(cam);
2912 }
2913
4186ecf8 2914 mutex_unlock(&cam->dev_mutex);
1da177e4
LT
2915
2916 if (!cam->users)
2917 kfree(cam);
2918
2919 up_write(&sn9c102_disconnect);
2920}
2921
2922
2923static struct usb_driver sn9c102_usb_driver = {
1da177e4
LT
2924 .name = "sn9c102",
2925 .id_table = sn9c102_id_table,
2926 .probe = sn9c102_usb_probe,
2927 .disconnect = sn9c102_usb_disconnect,
2928};
2929
2930/*****************************************************************************/
2931
2932static int __init sn9c102_module_init(void)
2933{
2934 int err = 0;
2935
a966f3e7
LR
2936 KDBG(2, SN9C102_MODULE_NAME " v" SN9C102_MODULE_VERSION);
2937 KDBG(3, SN9C102_MODULE_AUTHOR);
1da177e4
LT
2938
2939 if ((err = usb_register(&sn9c102_usb_driver)))
a966f3e7 2940 KDBG(1, "usb_register() failed");
1da177e4
LT
2941
2942 return err;
2943}
2944
2945
2946static void __exit sn9c102_module_exit(void)
2947{
2948 usb_deregister(&sn9c102_usb_driver);
2949}
2950
2951
2952module_init(sn9c102_module_init);
2953module_exit(sn9c102_module_exit);