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*
et61x251_match_id(struct et61x251_device * cam,const struct usb_device_id * id)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
et61x251_attach_sensor(struct et61x251_device * cam,const struct et61x251_sensor * sensor)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, ...)						\
169 do {									\
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 	}								\
182 } while (0)
183 #define KDBG(level, fmt, ...)						\
184 do {									\
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 	}								\
193 } while (0)
194 #define V4LDBG(level, name, cmd)					\
195 do {									\
196 	if (debug >= (level))						\
197 		v4l_print_ioctl(name, cmd);				\
198 } while (0)
199 #else
200 #define DBG(level, fmt, ...) do {;} while(0)
201 #define KDBG(level, fmt, ...) do {;} while(0)
202 #define V4LDBG(level, name, cmd) do {;} while(0)
203 #endif
204 
205 #undef PDBG
206 #define PDBG(fmt, ...)							\
207 	dev_info(&cam->usbdev->dev, "[%s:%s:%d] " fmt "\n",		\
208 		 __FILE__, __func__, __LINE__, ##__VA_ARGS__)
209 
210 #undef PDBGG
211 #define PDBGG(fmt, args...) do {;} while (0) /* placeholder */
212 
213 #endif /* _ET61X251_H_ */
214