a58ff2e9efa3272a5653657d6f9b1e86d7a1e49e
[linux-2.6.git] / drivers / usb / media / sn9c102.h
1 /***************************************************************************
2  * V4L2 driver for SN9C10x PC Camera Controllers                           *
3  *                                                                         *
4  * Copyright (C) 2004 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 _SN9C102_H_
22 #define _SN9C102_H_
23
24 #include <linux/version.h>
25 #include <linux/usb.h>
26 #include <linux/videodev.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 <asm/semaphore.h>
36
37 #include "sn9c102_sensor.h"
38
39 /*****************************************************************************/
40
41 #define SN9C102_DEBUG
42 #define SN9C102_DEBUG_LEVEL       2
43 #define SN9C102_MAX_DEVICES       64
44 #define SN9C102_PRESERVE_IMGSCALE 0
45 #define SN9C102_MAX_FRAMES        32
46 #define SN9C102_URBS              2
47 #define SN9C102_ISO_PACKETS       7
48 #define SN9C102_ALTERNATE_SETTING 8
49 #define SN9C102_URB_TIMEOUT       msecs_to_jiffies(3)
50 #define SN9C102_CTRL_TIMEOUT      msecs_to_jiffies(100)
51
52 /*****************************************************************************/
53
54 #define SN9C102_MODULE_NAME     "V4L2 driver for SN9C10x PC Camera Controllers"
55 #define SN9C102_MODULE_AUTHOR   "(C) 2004 Luca Risolia"
56 #define SN9C102_AUTHOR_EMAIL    "<luca.risolia@studio.unibo.it>"
57 #define SN9C102_MODULE_LICENSE  "GPL"
58 #define SN9C102_MODULE_VERSION  "1:1.19"
59 #define SN9C102_MODULE_VERSION_CODE  KERNEL_VERSION(1, 0, 19)
60
61 enum sn9c102_bridge {
62         BRIDGE_SN9C101 = 0x01,
63         BRIDGE_SN9C102 = 0x02,
64         BRIDGE_SN9C103 = 0x04,
65 };
66
67 SN9C102_ID_TABLE
68 SN9C102_SENSOR_TABLE
69
70 enum sn9c102_frame_state {
71         F_UNUSED,
72         F_QUEUED,
73         F_GRABBING,
74         F_DONE,
75         F_ERROR,
76 };
77
78 struct sn9c102_frame_t {
79         void* bufmem;
80         struct v4l2_buffer buf;
81         enum sn9c102_frame_state state;
82         struct list_head frame;
83         unsigned long vma_use_count;
84 };
85
86 enum sn9c102_dev_state {
87         DEV_INITIALIZED = 0x01,
88         DEV_DISCONNECTED = 0x02,
89         DEV_MISCONFIGURED = 0x04,
90 };
91
92 enum sn9c102_io_method {
93         IO_NONE,
94         IO_READ,
95         IO_MMAP,
96 };
97
98 enum sn9c102_stream_state {
99         STREAM_OFF,
100         STREAM_INTERRUPT,
101         STREAM_ON,
102 };
103
104 struct sn9c102_sysfs_attr {
105         u8 reg, i2c_reg;
106 };
107
108 static DECLARE_MUTEX(sn9c102_sysfs_lock);
109 static DECLARE_RWSEM(sn9c102_disconnect);
110
111 struct sn9c102_device {
112         struct device dev;
113
114         struct video_device* v4ldev;
115
116         enum sn9c102_bridge bridge;
117         struct sn9c102_sensor* sensor;
118
119         struct usb_device* usbdev;
120         struct urb* urb[SN9C102_URBS];
121         void* transfer_buffer[SN9C102_URBS];
122         u8* control_buffer;
123
124         struct sn9c102_frame_t *frame_current, frame[SN9C102_MAX_FRAMES];
125         struct list_head inqueue, outqueue;
126         u32 frame_count, nbuffers, nreadbuffers;
127
128         enum sn9c102_io_method io;
129         enum sn9c102_stream_state stream;
130
131         struct v4l2_jpegcompression compression;
132
133         struct sn9c102_sysfs_attr sysfs;
134         u16 reg[32];
135
136         enum sn9c102_dev_state state;
137         u8 users;
138
139         struct semaphore dev_sem, fileop_sem;
140         spinlock_t queue_lock;
141         wait_queue_head_t open, wait_frame, wait_stream;
142 };
143
144 /*****************************************************************************/
145
146 void
147 sn9c102_attach_sensor(struct sn9c102_device* cam,
148                       struct sn9c102_sensor* sensor)
149 {
150         cam->sensor = sensor;
151         cam->sensor->dev = &cam->dev;
152         cam->sensor->usbdev = cam->usbdev;
153 }
154
155 /*****************************************************************************/
156
157 #undef DBG
158 #undef KDBG
159 #ifdef SN9C102_DEBUG
160 #       define DBG(level, fmt, args...)                                       \
161 {                                                                             \
162         if (debug >= (level)) {                                               \
163                 if ((level) == 1)                                             \
164                         dev_err(&cam->dev, fmt "\n", ## args);                \
165                 else if ((level) == 2)                                        \
166                         dev_info(&cam->dev, fmt "\n", ## args);               \
167                 else if ((level) >= 3)                                        \
168                         dev_info(&cam->dev, "[%s:%d] " fmt "\n",              \
169                                  __FUNCTION__, __LINE__ , ## args);           \
170         }                                                                     \
171 }
172 #       define KDBG(level, fmt, args...)                                      \
173 {                                                                             \
174         if (debug >= (level)) {                                               \
175                 if ((level) == 1 || (level) == 2)                             \
176                         pr_info("sn9c102: " fmt "\n", ## args);               \
177                 else if ((level) == 3)                                        \
178                         pr_debug("sn9c102: [%s:%d] " fmt "\n", __FUNCTION__,  \
179                                  __LINE__ , ## args);                         \
180         }                                                                     \
181 }
182 #else
183 #       define KDBG(level, fmt, args...) do {;} while(0);
184 #       define DBG(level, fmt, args...) do {;} while(0);
185 #endif
186
187 #undef PDBG
188 #define PDBG(fmt, args...)                                                    \
189 dev_info(&cam->dev, "[%s:%d] " fmt "\n", __FUNCTION__, __LINE__ , ## args);
190
191 #undef PDBGG
192 #define PDBGG(fmt, args...) do {;} while(0); /* placeholder */
193
194 #endif /* _SN9C102_H_ */