blob: 2c6ff396dafc46c87f6c1ea1568dbc96ee6b82ff [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/***************************************************************************
2 * V4L2 driver for SN9C10x PC Camera Controllers *
3 * *
Luca Risoliaa966f3e2006-01-05 18:14:04 +00004 * Copyright (C) 2004-2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * *
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>
Luca Risoliacd6fcc52006-01-13 17:19:43 +000026#include <linux/videodev2.h>
27#include <media/v4l2-common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#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>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010036#include <linux/mutex.h>
Luca Risolia2ffab022006-02-25 06:50:47 +000037#include <linux/string.h>
38#include <linux/stddef.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#include "sn9c102_sensor.h"
41
42/*****************************************************************************/
43
44#define SN9C102_DEBUG
45#define SN9C102_DEBUG_LEVEL 2
46#define SN9C102_MAX_DEVICES 64
47#define SN9C102_PRESERVE_IMGSCALE 0
48#define SN9C102_FORCE_MUNMAP 0
49#define SN9C102_MAX_FRAMES 32
50#define SN9C102_URBS 2
51#define SN9C102_ISO_PACKETS 7
52#define SN9C102_ALTERNATE_SETTING 8
53#define SN9C102_URB_TIMEOUT msecs_to_jiffies(2 * SN9C102_ISO_PACKETS)
54#define SN9C102_CTRL_TIMEOUT 300
Luca Risolia2ffab022006-02-25 06:50:47 +000055#define SN9C102_FRAME_TIMEOUT 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57/*****************************************************************************/
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059enum sn9c102_bridge {
60 BRIDGE_SN9C101 = 0x01,
61 BRIDGE_SN9C102 = 0x02,
62 BRIDGE_SN9C103 = 0x04,
63};
64
65SN9C102_ID_TABLE
66SN9C102_SENSOR_TABLE
67
68enum sn9c102_frame_state {
69 F_UNUSED,
70 F_QUEUED,
71 F_GRABBING,
72 F_DONE,
73 F_ERROR,
74};
75
76struct sn9c102_frame_t {
77 void* bufmem;
78 struct v4l2_buffer buf;
79 enum sn9c102_frame_state state;
80 struct list_head frame;
81 unsigned long vma_use_count;
82};
83
84enum sn9c102_dev_state {
85 DEV_INITIALIZED = 0x01,
86 DEV_DISCONNECTED = 0x02,
87 DEV_MISCONFIGURED = 0x04,
88};
89
90enum sn9c102_io_method {
91 IO_NONE,
92 IO_READ,
93 IO_MMAP,
94};
95
96enum sn9c102_stream_state {
97 STREAM_OFF,
98 STREAM_INTERRUPT,
99 STREAM_ON,
100};
101
Luca Risoliaa966f3e2006-01-05 18:14:04 +0000102typedef char sn9c103_sof_header_t[18];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103typedef char sn9c102_sof_header_t[12];
104typedef char sn9c102_eof_header_t[4];
105
106struct sn9c102_sysfs_attr {
107 u8 reg, i2c_reg;
Luca Risoliaa966f3e2006-01-05 18:14:04 +0000108 sn9c103_sof_header_t frame_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109};
110
111struct sn9c102_module_param {
112 u8 force_munmap;
Luca Risolia2ffab022006-02-25 06:50:47 +0000113 u16 frame_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114};
115
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100116static DEFINE_MUTEX(sn9c102_sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117static DECLARE_RWSEM(sn9c102_disconnect);
118
119struct sn9c102_device {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 struct video_device* v4ldev;
121
122 enum sn9c102_bridge bridge;
Luca Risolia2ffab022006-02-25 06:50:47 +0000123 struct sn9c102_sensor sensor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 struct usb_device* usbdev;
126 struct urb* urb[SN9C102_URBS];
127 void* transfer_buffer[SN9C102_URBS];
128 u8* control_buffer;
129
130 struct sn9c102_frame_t *frame_current, frame[SN9C102_MAX_FRAMES];
131 struct list_head inqueue, outqueue;
132 u32 frame_count, nbuffers, nreadbuffers;
133
134 enum sn9c102_io_method io;
135 enum sn9c102_stream_state stream;
136
137 struct v4l2_jpegcompression compression;
138
139 struct sn9c102_sysfs_attr sysfs;
Luca Risoliaa966f3e2006-01-05 18:14:04 +0000140 sn9c103_sof_header_t sof_header;
141 u16 reg[63];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 struct sn9c102_module_param module_param;
144
145 enum sn9c102_dev_state state;
146 u8 users;
147
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100148 struct mutex dev_mutex, fileop_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 spinlock_t queue_lock;
150 wait_queue_head_t open, wait_frame, wait_stream;
151};
152
153/*****************************************************************************/
154
Luca Risolia2ffab022006-02-25 06:50:47 +0000155struct sn9c102_device*
156sn9c102_match_id(struct sn9c102_device* cam, const struct usb_device_id *id)
157{
158 if (usb_match_id(usb_ifnum_to_if(cam->usbdev, 0), id))
159 return cam;
160
161 return NULL;
162}
163
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165void
166sn9c102_attach_sensor(struct sn9c102_device* cam,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300167 struct sn9c102_sensor* sensor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Luca Risolia2ffab022006-02-25 06:50:47 +0000169 memcpy(&cam->sensor, sensor, sizeof(struct sn9c102_sensor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
172/*****************************************************************************/
173
174#undef DBG
175#undef KDBG
176#ifdef SN9C102_DEBUG
177# define DBG(level, fmt, args...) \
Luca Risoliaa966f3e2006-01-05 18:14:04 +0000178do { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 if (debug >= (level)) { \
180 if ((level) == 1) \
Luca Risoliacd6fcc52006-01-13 17:19:43 +0000181 dev_err(&cam->usbdev->dev, fmt "\n", ## args); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 else if ((level) == 2) \
Luca Risoliacd6fcc52006-01-13 17:19:43 +0000183 dev_info(&cam->usbdev->dev, fmt "\n", ## args); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 else if ((level) >= 3) \
Luca Risoliacd6fcc52006-01-13 17:19:43 +0000185 dev_info(&cam->usbdev->dev, "[%s:%d] " fmt "\n", \
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300186 __FUNCTION__, __LINE__ , ## args); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 } \
Luca Risoliaa966f3e2006-01-05 18:14:04 +0000188} while (0)
Luca Risoliacd6fcc52006-01-13 17:19:43 +0000189# define V4LDBG(level, name, cmd) \
190do { \
191 if (debug >= (level)) \
192 v4l_print_ioctl(name, cmd); \
193} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194# define KDBG(level, fmt, args...) \
Luca Risoliaa966f3e2006-01-05 18:14:04 +0000195do { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (debug >= (level)) { \
197 if ((level) == 1 || (level) == 2) \
198 pr_info("sn9c102: " fmt "\n", ## args); \
199 else if ((level) == 3) \
200 pr_debug("sn9c102: [%s:%d] " fmt "\n", __FUNCTION__, \
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300201 __LINE__ , ## args); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 } \
Luca Risoliaa966f3e2006-01-05 18:14:04 +0000203} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204#else
Luca Risoliaa966f3e2006-01-05 18:14:04 +0000205# define DBG(level, fmt, args...) do {;} while(0)
Luca Risoliacd6fcc52006-01-13 17:19:43 +0000206# define V4LDBG(level, name, cmd) do {;} while(0)
207# define KDBG(level, fmt, args...) do {;} while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208#endif
209
210#undef PDBG
211#define PDBG(fmt, args...) \
Luca Risolia2ffab022006-02-25 06:50:47 +0000212dev_info(&cam->usbdev->dev, "[%s:%d] " fmt "\n", \
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300213 __FUNCTION__, __LINE__ , ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215#undef PDBGG
Luca Risoliaa966f3e2006-01-05 18:14:04 +0000216#define PDBGG(fmt, args...) do {;} while(0) /* placeholder */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218#endif /* _SN9C102_H_ */