blob: bf66189cb26de1fba4f58884e458b89adb08d2b7 [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
24#include <linux/version.h>
25#include <linux/usb.h>
26#include <linux/videodev2.h>
27#include <media/v4l2-common.h>
28#include <linux/device.h>
29#include <linux/list.h>
30#include <linux/spinlock.h>
31#include <linux/time.h>
32#include <linux/wait.h>
33#include <linux/types.h>
34#include <linux/param.h>
35#include <linux/rwsem.h>
Luca Risoliaccad7782006-02-25 06:54:18 +000036#include <linux/mutex.h>
37#include <linux/stddef.h>
38#include <linux/string.h>
Luca Risolia3b2ae0b2007-06-13 14:52:01 -030039#include <linux/kref.h>
Luca Risolia7ce08c92006-01-11 02:06:59 +000040
41#include "et61x251_sensor.h"
42
43/*****************************************************************************/
44
45#define ET61X251_DEBUG
46#define ET61X251_DEBUG_LEVEL 2
47#define ET61X251_MAX_DEVICES 64
48#define ET61X251_PRESERVE_IMGSCALE 0
49#define ET61X251_FORCE_MUNMAP 0
50#define ET61X251_MAX_FRAMES 32
51#define ET61X251_COMPRESSION_QUALITY 0
52#define ET61X251_URBS 2
53#define ET61X251_ISO_PACKETS 7
54#define ET61X251_ALTERNATE_SETTING 13
55#define ET61X251_URB_TIMEOUT msecs_to_jiffies(2 * ET61X251_ISO_PACKETS)
56#define ET61X251_CTRL_TIMEOUT 100
Luca Risoliaccad7782006-02-25 06:54:18 +000057#define ET61X251_FRAME_TIMEOUT 2
Luca Risolia7ce08c92006-01-11 02:06:59 +000058
59/*****************************************************************************/
60
61static const struct usb_device_id et61x251_id_table[] = {
Luca Risolia7ce08c92006-01-11 02:06:59 +000062 { USB_DEVICE(0x102c, 0x6251), },
Luca Risolia7ce08c92006-01-11 02:06:59 +000063 { }
64};
65
66ET61X251_SENSOR_TABLE
67
68/*****************************************************************************/
69
70enum et61x251_frame_state {
71 F_UNUSED,
72 F_QUEUED,
73 F_GRABBING,
74 F_DONE,
75 F_ERROR,
76};
77
78struct et61x251_frame_t {
79 void* bufmem;
80 struct v4l2_buffer buf;
81 enum et61x251_frame_state state;
82 struct list_head frame;
83 unsigned long vma_use_count;
84};
85
86enum et61x251_dev_state {
87 DEV_INITIALIZED = 0x01,
88 DEV_DISCONNECTED = 0x02,
89 DEV_MISCONFIGURED = 0x04,
90};
91
92enum et61x251_io_method {
93 IO_NONE,
94 IO_READ,
95 IO_MMAP,
96};
97
98enum et61x251_stream_state {
99 STREAM_OFF,
100 STREAM_INTERRUPT,
101 STREAM_ON,
102};
103
104struct et61x251_sysfs_attr {
105 u8 reg, i2c_reg;
106};
107
108struct et61x251_module_param {
109 u8 force_munmap;
Luca Risoliaccad7782006-02-25 06:54:18 +0000110 u16 frame_timeout;
Luca Risolia7ce08c92006-01-11 02:06:59 +0000111};
112
Luca Risoliaccad7782006-02-25 06:54:18 +0000113static DEFINE_MUTEX(et61x251_sysfs_lock);
Luca Risolia3b2ae0b2007-06-13 14:52:01 -0300114static DECLARE_RWSEM(et61x251_dev_lock);
Luca Risolia7ce08c92006-01-11 02:06:59 +0000115
116struct et61x251_device {
117 struct video_device* v4ldev;
118
Luca Risoliaccad7782006-02-25 06:54:18 +0000119 struct et61x251_sensor sensor;
Luca Risolia7ce08c92006-01-11 02:06:59 +0000120
121 struct usb_device* usbdev;
122 struct urb* urb[ET61X251_URBS];
123 void* transfer_buffer[ET61X251_URBS];
124 u8* control_buffer;
125
126 struct et61x251_frame_t *frame_current, frame[ET61X251_MAX_FRAMES];
127 struct list_head inqueue, outqueue;
128 u32 frame_count, nbuffers, nreadbuffers;
129
130 enum et61x251_io_method io;
131 enum et61x251_stream_state stream;
132
133 struct v4l2_jpegcompression compression;
134
135 struct et61x251_sysfs_attr sysfs;
136 struct et61x251_module_param module_param;
137
Luca Risolia3b2ae0b2007-06-13 14:52:01 -0300138 struct kref kref;
Luca Risolia7ce08c92006-01-11 02:06:59 +0000139 enum et61x251_dev_state state;
140 u8 users;
141
Luca Risolia3b2ae0b2007-06-13 14:52:01 -0300142 struct completion probe;
143 struct mutex open_mutex, fileop_mutex;
Luca Risolia7ce08c92006-01-11 02:06:59 +0000144 spinlock_t queue_lock;
Luca Risolia3b2ae0b2007-06-13 14:52:01 -0300145 wait_queue_head_t wait_open, wait_frame, wait_stream;
Luca Risolia7ce08c92006-01-11 02:06:59 +0000146};
147
148/*****************************************************************************/
149
Luca Risoliaccad7782006-02-25 06:54:18 +0000150struct et61x251_device*
151et61x251_match_id(struct et61x251_device* cam, const struct usb_device_id *id)
152{
Luca Risolia26563122007-01-08 11:38:36 -0300153 return usb_match_id(usb_ifnum_to_if(cam->usbdev, 0), id) ? cam : NULL;
Luca Risoliaccad7782006-02-25 06:54:18 +0000154}
155
156
Luca Risolia7ce08c92006-01-11 02:06:59 +0000157void
158et61x251_attach_sensor(struct et61x251_device* cam,
Luca Risolia3b2ae0b2007-06-13 14:52:01 -0300159 const struct et61x251_sensor* sensor)
Luca Risolia7ce08c92006-01-11 02:06:59 +0000160{
Luca Risoliaccad7782006-02-25 06:54:18 +0000161 memcpy(&cam->sensor, sensor, sizeof(struct et61x251_sensor));
Luca Risolia7ce08c92006-01-11 02:06:59 +0000162}
163
164/*****************************************************************************/
165
166#undef DBG
167#undef KDBG
168#ifdef ET61X251_DEBUG
169# define DBG(level, fmt, args...) \
170do { \
171 if (debug >= (level)) { \
172 if ((level) == 1) \
173 dev_err(&cam->usbdev->dev, fmt "\n", ## args); \
174 else if ((level) == 2) \
175 dev_info(&cam->usbdev->dev, fmt "\n", ## args); \
176 else if ((level) >= 3) \
Luca Risolia3b2ae0b2007-06-13 14:52:01 -0300177 dev_info(&cam->usbdev->dev, "[%s:%s:%d] " fmt "\n", \
Harvey Harrison22cc0652008-04-08 23:20:00 -0300178 __FILE__, __func__, __LINE__ , ## args); \
Luca Risolia7ce08c92006-01-11 02:06:59 +0000179 } \
180} while (0)
181# define KDBG(level, fmt, args...) \
182do { \
183 if (debug >= (level)) { \
184 if ((level) == 1 || (level) == 2) \
185 pr_info("et61x251: " fmt "\n", ## args); \
186 else if ((level) == 3) \
Luca Risolia3b2ae0b2007-06-13 14:52:01 -0300187 pr_debug("sn9c102: [%s:%s:%d] " fmt "\n", __FILE__, \
Harvey Harrison22cc0652008-04-08 23:20:00 -0300188 __func__, __LINE__ , ## args); \
Luca Risolia7ce08c92006-01-11 02:06:59 +0000189 } \
190} while (0)
191# define V4LDBG(level, name, cmd) \
192do { \
193 if (debug >= (level)) \
194 v4l_print_ioctl(name, cmd); \
195} while (0)
196#else
197# define DBG(level, fmt, args...) do {;} while(0)
198# define KDBG(level, fmt, args...) do {;} while(0)
199# define V4LDBG(level, name, cmd) do {;} while(0)
200#endif
201
202#undef PDBG
203#define PDBG(fmt, args...) \
Harvey Harrison22cc0652008-04-08 23:20:00 -0300204dev_info(&cam->usbdev->dev, "[%s:%s:%d] " fmt "\n", __FILE__, __func__, \
Luca Risolia3b2ae0b2007-06-13 14:52:01 -0300205 __LINE__ , ## args)
Luca Risolia7ce08c92006-01-11 02:06:59 +0000206
207#undef PDBGG
208#define PDBGG(fmt, args...) do {;} while(0) /* placeholder */
209
210#endif /* _ET61X251_H_ */