Merge branch 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jwessel...
[pandora-kernel.git] / drivers / media / video / et61x251 / et61x251.h
1 /***************************************************************************
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
24 #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>
35 #include <linux/mutex.h>
36 #include <linux/stddef.h>
37 #include <linux/string.h>
38 #include <linux/kref.h>
39
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
56 #define ET61X251_FRAME_TIMEOUT       2
57
58 /*****************************************************************************/
59
60 static const struct usb_device_id et61x251_id_table[] = {
61         { USB_DEVICE(0x102c, 0x6251), },
62         { }
63 };
64
65 ET61X251_SENSOR_TABLE
66
67 /*****************************************************************************/
68
69 enum et61x251_frame_state {
70         F_UNUSED,
71         F_QUEUED,
72         F_GRABBING,
73         F_DONE,
74         F_ERROR,
75 };
76
77 struct 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
85 enum et61x251_dev_state {
86         DEV_INITIALIZED = 0x01,
87         DEV_DISCONNECTED = 0x02,
88         DEV_MISCONFIGURED = 0x04,
89 };
90
91 enum et61x251_io_method {
92         IO_NONE,
93         IO_READ,
94         IO_MMAP,
95 };
96
97 enum et61x251_stream_state {
98         STREAM_OFF,
99         STREAM_INTERRUPT,
100         STREAM_ON,
101 };
102
103 struct et61x251_sysfs_attr {
104         u8 reg, i2c_reg;
105 };
106
107 struct et61x251_module_param {
108         u8 force_munmap;
109         u16 frame_timeout;
110 };
111
112 static DEFINE_MUTEX(et61x251_sysfs_lock);
113 static DECLARE_RWSEM(et61x251_dev_lock);
114
115 struct et61x251_device {
116         struct video_device* v4ldev;
117
118         struct et61x251_sensor sensor;
119
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
137         struct kref kref;
138         enum et61x251_dev_state state;
139         u8 users;
140
141         struct completion probe;
142         struct mutex open_mutex, fileop_mutex;
143         spinlock_t queue_lock;
144         wait_queue_head_t wait_open, wait_frame, wait_stream;
145 };
146
147 /*****************************************************************************/
148
149 struct et61x251_device*
150 et61x251_match_id(struct et61x251_device* cam, const struct usb_device_id *id)
151 {
152         return usb_match_id(usb_ifnum_to_if(cam->usbdev, 0), id) ? cam : NULL;
153 }
154
155
156 void
157 et61x251_attach_sensor(struct et61x251_device* cam,
158                        const struct et61x251_sensor* sensor)
159 {
160         memcpy(&cam->sensor, sensor, sizeof(struct et61x251_sensor));
161 }
162
163 /*****************************************************************************/
164
165 #undef DBG
166 #undef KDBG
167 #ifdef ET61X251_DEBUG
168 #       define DBG(level, fmt, args...)                                       \
169 do {                                                                          \
170         if (debug >= (level)) {                                               \
171                 if ((level) == 1)                                             \
172                         dev_err(&cam->usbdev->dev, fmt "\n", ## args);        \
173                 else if ((level) == 2)                                        \
174                         dev_info(&cam->usbdev->dev, fmt "\n", ## args);       \
175                 else if ((level) >= 3)                                        \
176                         dev_info(&cam->usbdev->dev, "[%s:%s:%d] " fmt "\n",   \
177                                  __FILE__, __func__, __LINE__ , ## args); \
178         }                                                                     \
179 } while (0)
180 #       define KDBG(level, fmt, args...)                                      \
181 do {                                                                          \
182         if (debug >= (level)) {                                               \
183                 if ((level) == 1 || (level) == 2)                             \
184                         pr_info("et61x251: " fmt "\n", ## args);              \
185                 else if ((level) == 3)                                        \
186                         pr_debug("sn9c102: [%s:%s:%d] " fmt "\n", __FILE__,   \
187                                  __func__, __LINE__ , ## args);           \
188         }                                                                     \
189 } while (0)
190 #       define V4LDBG(level, name, cmd)                                       \
191 do {                                                                          \
192         if (debug >= (level))                                                 \
193                 v4l_print_ioctl(name, cmd);                                   \
194 } while (0)
195 #else
196 #       define DBG(level, fmt, args...) do {;} while(0)
197 #       define KDBG(level, fmt, args...) do {;} while(0)
198 #       define V4LDBG(level, name, cmd) do {;} while(0)
199 #endif
200
201 #undef PDBG
202 #define PDBG(fmt, args...)                                                    \
203 dev_info(&cam->usbdev->dev, "[%s:%s:%d] " fmt "\n", __FILE__, __func__,   \
204          __LINE__ , ## args)
205
206 #undef PDBGG
207 #define PDBGG(fmt, args...) do {;} while(0) /* placeholder */
208
209 #endif /* _ET61X251_H_ */