blob: 337ded4a6388d68d58c6f7afb78dc18fe0e7ba29 [file] [log] [blame]
Luca Risolia7ce08c92006-01-11 02:06:59 +00001/***************************************************************************
2 * V4L2 driver for ET61X[12]51 PC Camera Controllers *
3 * *
4 * Copyright (C) 2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
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#ifndef _ET61X251_H_
22#define _ET61X251_H_
23
Luca Risolia7ce08c92006-01-11 02:06:59 +000024#include <linux/usb.h>
25#include <linux/videodev2.h>
26#include <media/v4l2-common.h>
27#include <linux/device.h>
28#include <linux/list.h>
29#include <linux/spinlock.h>
30#include <linux/time.h>
31#include <linux/wait.h>
32#include <linux/types.h>
33#include <linux/param.h>
34#include <linux/rwsem.h>
Luca Risoliaccad7782006-02-25 06:54:18 +000035#include <linux/mutex.h>
36#include <linux/stddef.h>
37#include <linux/string.h>
Luca Risolia3b2ae0b2007-06-13 14:52:01 -030038#include <linux/kref.h>
Luca Risolia7ce08c92006-01-11 02:06:59 +000039
40#include "et61x251_sensor.h"
41
42/*****************************************************************************/
43
44#define ET61X251_DEBUG
45#define ET61X251_DEBUG_LEVEL 2
46#define ET61X251_MAX_DEVICES 64
47#define ET61X251_PRESERVE_IMGSCALE 0
48#define ET61X251_FORCE_MUNMAP 0
49#define ET61X251_MAX_FRAMES 32
50#define ET61X251_COMPRESSION_QUALITY 0
51#define ET61X251_URBS 2
52#define ET61X251_ISO_PACKETS 7
53#define ET61X251_ALTERNATE_SETTING 13
54#define ET61X251_URB_TIMEOUT msecs_to_jiffies(2 * ET61X251_ISO_PACKETS)
55#define ET61X251_CTRL_TIMEOUT 100
Luca Risoliaccad7782006-02-25 06:54:18 +000056#define ET61X251_FRAME_TIMEOUT 2
Luca Risolia7ce08c92006-01-11 02:06:59 +000057
58/*****************************************************************************/
59
60static const struct usb_device_id et61x251_id_table[] = {
Luca Risolia7ce08c92006-01-11 02:06:59 +000061 { USB_DEVICE(0x102c, 0x6251), },
Luca Risolia7ce08c92006-01-11 02:06:59 +000062 { }
63};
64
65ET61X251_SENSOR_TABLE
66
67/*****************************************************************************/
68
69enum et61x251_frame_state {
70 F_UNUSED,
71 F_QUEUED,
72 F_GRABBING,
73 F_DONE,
74 F_ERROR,
75};
76
77struct et61x251_frame_t {
78 void* bufmem;
79 struct v4l2_buffer buf;
80 enum et61x251_frame_state state;
81 struct list_head frame;
82 unsigned long vma_use_count;
83};
84
85enum et61x251_dev_state {
86 DEV_INITIALIZED = 0x01,
87 DEV_DISCONNECTED = 0x02,
88 DEV_MISCONFIGURED = 0x04,
89};
90
91enum et61x251_io_method {
92 IO_NONE,
93 IO_READ,
94 IO_MMAP,
95};
96
97enum et61x251_stream_state {
98 STREAM_OFF,
99 STREAM_INTERRUPT,
100 STREAM_ON,
101};
102
103struct et61x251_sysfs_attr {
104 u8 reg, i2c_reg;
105};
106
107struct et61x251_module_param {
108 u8 force_munmap;
Luca Risoliaccad7782006-02-25 06:54:18 +0000109 u16 frame_timeout;
Luca Risolia7ce08c92006-01-11 02:06:59 +0000110};
111
Luca Risoliaccad7782006-02-25 06:54:18 +0000112static DEFINE_MUTEX(et61x251_sysfs_lock);
Luca Risolia3b2ae0b2007-06-13 14:52:01 -0300113static DECLARE_RWSEM(et61x251_dev_lock);
Luca Risolia7ce08c92006-01-11 02:06:59 +0000114
115struct et61x251_device {
116 struct video_device* v4ldev;
117
Luca Risoliaccad7782006-02-25 06:54:18 +0000118 struct et61x251_sensor sensor;
Luca Risolia7ce08c92006-01-11 02:06:59 +0000119
120 struct usb_device* usbdev;
121 struct urb* urb[ET61X251_URBS];
122 void* transfer_buffer[ET61X251_URBS];
123 u8* control_buffer;
124
125 struct et61x251_frame_t *frame_current, frame[ET61X251_MAX_FRAMES];
126 struct list_head inqueue, outqueue;
127 u32 frame_count, nbuffers, nreadbuffers;
128
129 enum et61x251_io_method io;
130 enum et61x251_stream_state stream;
131
132 struct v4l2_jpegcompression compression;
133
134 struct et61x251_sysfs_attr sysfs;
135 struct et61x251_module_param module_param;
136
Luca Risolia3b2ae0b2007-06-13 14:52:01 -0300137 struct kref kref;
Luca Risolia7ce08c92006-01-11 02:06:59 +0000138 enum et61x251_dev_state state;
139 u8 users;
140
Luca Risolia3b2ae0b2007-06-13 14:52:01 -0300141 struct completion probe;
142 struct mutex open_mutex, fileop_mutex;
Luca Risolia7ce08c92006-01-11 02:06:59 +0000143 spinlock_t queue_lock;
Luca Risolia3b2ae0b2007-06-13 14:52:01 -0300144 wait_queue_head_t wait_open, wait_frame, wait_stream;
Luca Risolia7ce08c92006-01-11 02:06:59 +0000145};
146
147/*****************************************************************************/
148
Luca Risoliaccad7782006-02-25 06:54:18 +0000149struct et61x251_device*
150et61x251_match_id(struct et61x251_device* cam, const struct usb_device_id *id)
151{
Luca Risolia26563122007-01-08 11:38:36 -0300152 return usb_match_id(usb_ifnum_to_if(cam->usbdev, 0), id) ? cam : NULL;
Luca Risoliaccad7782006-02-25 06:54:18 +0000153}
154
155
Luca Risolia7ce08c92006-01-11 02:06:59 +0000156void
157et61x251_attach_sensor(struct et61x251_device* cam,
Luca Risolia3b2ae0b2007-06-13 14:52:01 -0300158 const struct et61x251_sensor* sensor)
Luca Risolia7ce08c92006-01-11 02:06:59 +0000159{
Luca Risoliaccad7782006-02-25 06:54:18 +0000160 memcpy(&cam->sensor, sensor, sizeof(struct et61x251_sensor));
Luca Risolia7ce08c92006-01-11 02:06:59 +0000161}
162
163/*****************************************************************************/
164
165#undef DBG
166#undef KDBG
167#ifdef ET61X251_DEBUG
Joe Perches857011e2011-08-21 19:56:50 -0300168#define DBG(level, fmt, ...) \
169do { \
170 if (debug >= (level)) { \
171 if ((level) == 1) \
172 dev_err(&cam->usbdev->dev, fmt "\n", \
173 ##__VA_ARGS__); \
174 else if ((level) == 2) \
175 dev_info(&cam->usbdev->dev, fmt "\n", \
176 ##__VA_ARGS__); \
177 else if ((level) >= 3) \
178 dev_info(&cam->usbdev->dev, "[%s:%s:%d] " fmt "\n", \
179 __FILE__, __func__, __LINE__, \
180 ##__VA_ARGS__); \
181 } \
Luca Risolia7ce08c92006-01-11 02:06:59 +0000182} while (0)
Joe Perches857011e2011-08-21 19:56:50 -0300183#define KDBG(level, fmt, ...) \
184do { \
185 if (debug >= (level)) { \
186 if ((level) == 1 || (level) == 2) \
187 pr_info(fmt "\n", ##__VA_ARGS__); \
188 else if ((level) == 3) \
189 pr_debug("[%s:%s:%d] " fmt "\n", \
190 __FILE__, __func__, __LINE__, \
191 ##__VA_ARGS__); \
192 } \
Luca Risolia7ce08c92006-01-11 02:06:59 +0000193} while (0)
Joe Perches857011e2011-08-21 19:56:50 -0300194#define V4LDBG(level, name, cmd) \
195do { \
196 if (debug >= (level)) \
197 v4l_print_ioctl(name, cmd); \
Luca Risolia7ce08c92006-01-11 02:06:59 +0000198} while (0)
199#else
Joe Perches857011e2011-08-21 19:56:50 -0300200#define DBG(level, fmt, ...) do {;} while(0)
201#define KDBG(level, fmt, ...) do {;} while(0)
202#define V4LDBG(level, name, cmd) do {;} while(0)
Luca Risolia7ce08c92006-01-11 02:06:59 +0000203#endif
204
205#undef PDBG
Joe Perches857011e2011-08-21 19:56:50 -0300206#define PDBG(fmt, ...) \
207 dev_info(&cam->usbdev->dev, "[%s:%s:%d] " fmt "\n", \
208 __FILE__, __func__, __LINE__, ##__VA_ARGS__)
Luca Risolia7ce08c92006-01-11 02:06:59 +0000209
210#undef PDBGG
Joe Perches857011e2011-08-21 19:56:50 -0300211#define PDBGG(fmt, args...) do {;} while (0) /* placeholder */
Luca Risolia7ce08c92006-01-11 02:06:59 +0000212
213#endif /* _ET61X251_H_ */