blob: 5428f34e7c5b4c69d2bcd2672ec72e666d1aede4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/***************************************************************************
Luca Risoliaf327ebb2007-01-08 10:43:56 -03002 * V4L2 driver for SN9C1xx PC Camera Controllers *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * *
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
Luca Risoliaf327ebb2007-01-08 10:43:56 -030040#include "sn9c102_config.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "sn9c102_sensor.h"
Luca Risoliaf327ebb2007-01-08 10:43:56 -030042#include "sn9c102_devtable.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45enum sn9c102_frame_state {
46 F_UNUSED,
47 F_QUEUED,
48 F_GRABBING,
49 F_DONE,
50 F_ERROR,
51};
52
53struct sn9c102_frame_t {
54 void* bufmem;
55 struct v4l2_buffer buf;
56 enum sn9c102_frame_state state;
57 struct list_head frame;
58 unsigned long vma_use_count;
59};
60
61enum sn9c102_dev_state {
62 DEV_INITIALIZED = 0x01,
63 DEV_DISCONNECTED = 0x02,
64 DEV_MISCONFIGURED = 0x04,
65};
66
67enum sn9c102_io_method {
68 IO_NONE,
69 IO_READ,
70 IO_MMAP,
71};
72
73enum sn9c102_stream_state {
74 STREAM_OFF,
75 STREAM_INTERRUPT,
76 STREAM_ON,
77};
78
Luca Risoliaf327ebb2007-01-08 10:43:56 -030079typedef char sn9c102_sof_header_t[62];
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81struct sn9c102_sysfs_attr {
82 u8 reg, i2c_reg;
Luca Risoliaf327ebb2007-01-08 10:43:56 -030083 sn9c102_sof_header_t frame_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084};
85
86struct sn9c102_module_param {
87 u8 force_munmap;
Luca Risolia2ffab022006-02-25 06:50:47 +000088 u16 frame_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089};
90
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010091static DEFINE_MUTEX(sn9c102_sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static DECLARE_RWSEM(sn9c102_disconnect);
93
94struct sn9c102_device {
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 struct video_device* v4ldev;
96
97 enum sn9c102_bridge bridge;
Luca Risolia2ffab022006-02-25 06:50:47 +000098 struct sn9c102_sensor sensor;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 struct usb_device* usbdev;
101 struct urb* urb[SN9C102_URBS];
102 void* transfer_buffer[SN9C102_URBS];
103 u8* control_buffer;
104
105 struct sn9c102_frame_t *frame_current, frame[SN9C102_MAX_FRAMES];
106 struct list_head inqueue, outqueue;
107 u32 frame_count, nbuffers, nreadbuffers;
108
109 enum sn9c102_io_method io;
110 enum sn9c102_stream_state stream;
111
112 struct v4l2_jpegcompression compression;
113
114 struct sn9c102_sysfs_attr sysfs;
Luca Risoliaf327ebb2007-01-08 10:43:56 -0300115 sn9c102_sof_header_t sof_header;
116 u16 reg[384];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 struct sn9c102_module_param module_param;
119
120 enum sn9c102_dev_state state;
121 u8 users;
122
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100123 struct mutex dev_mutex, fileop_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 spinlock_t queue_lock;
125 wait_queue_head_t open, wait_frame, wait_stream;
126};
127
128/*****************************************************************************/
129
Luca Risolia2ffab022006-02-25 06:50:47 +0000130struct sn9c102_device*
131sn9c102_match_id(struct sn9c102_device* cam, const struct usb_device_id *id)
132{
Luca Risoliaf327ebb2007-01-08 10:43:56 -0300133 return usb_match_id(usb_ifnum_to_if(cam->usbdev, 0), id) ? cam : NULL;
Luca Risolia2ffab022006-02-25 06:50:47 +0000134}
135
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137void
138sn9c102_attach_sensor(struct sn9c102_device* cam,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300139 struct sn9c102_sensor* sensor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Luca Risolia2ffab022006-02-25 06:50:47 +0000141 memcpy(&cam->sensor, sensor, sizeof(struct sn9c102_sensor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
Luca Risoliaf327ebb2007-01-08 10:43:56 -0300144
145enum sn9c102_bridge
146sn9c102_get_bridge(struct sn9c102_device* cam)
147{
148 return cam->bridge;
149}
150
151
152struct sn9c102_sensor* sn9c102_get_sensor(struct sn9c102_device* cam)
153{
154 return &cam->sensor;
155}
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/*****************************************************************************/
158
159#undef DBG
160#undef KDBG
161#ifdef SN9C102_DEBUG
162# define DBG(level, fmt, args...) \
Luca Risoliaa966f3e2006-01-05 18:14:04 +0000163do { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (debug >= (level)) { \
165 if ((level) == 1) \
Luca Risoliacd6fcc52006-01-13 17:19:43 +0000166 dev_err(&cam->usbdev->dev, fmt "\n", ## args); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 else if ((level) == 2) \
Luca Risoliacd6fcc52006-01-13 17:19:43 +0000168 dev_info(&cam->usbdev->dev, fmt "\n", ## args); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 else if ((level) >= 3) \
Luca Risoliacd6fcc52006-01-13 17:19:43 +0000170 dev_info(&cam->usbdev->dev, "[%s:%d] " fmt "\n", \
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300171 __FUNCTION__, __LINE__ , ## args); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 } \
Luca Risoliaa966f3e2006-01-05 18:14:04 +0000173} while (0)
Luca Risoliacd6fcc52006-01-13 17:19:43 +0000174# define V4LDBG(level, name, cmd) \
175do { \
176 if (debug >= (level)) \
177 v4l_print_ioctl(name, cmd); \
178} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179# define KDBG(level, fmt, args...) \
Luca Risoliaa966f3e2006-01-05 18:14:04 +0000180do { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (debug >= (level)) { \
182 if ((level) == 1 || (level) == 2) \
183 pr_info("sn9c102: " fmt "\n", ## args); \
184 else if ((level) == 3) \
185 pr_debug("sn9c102: [%s:%d] " fmt "\n", __FUNCTION__, \
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300186 __LINE__ , ## args); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 } \
Luca Risoliaa966f3e2006-01-05 18:14:04 +0000188} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189#else
Luca Risoliaa966f3e2006-01-05 18:14:04 +0000190# define DBG(level, fmt, args...) do {;} while(0)
Luca Risoliacd6fcc52006-01-13 17:19:43 +0000191# define V4LDBG(level, name, cmd) do {;} while(0)
192# define KDBG(level, fmt, args...) do {;} while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193#endif
194
195#undef PDBG
196#define PDBG(fmt, args...) \
Luca Risolia2ffab022006-02-25 06:50:47 +0000197dev_info(&cam->usbdev->dev, "[%s:%d] " fmt "\n", \
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300198 __FUNCTION__, __LINE__ , ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200#undef PDBGG
Luca Risoliaa966f3e2006-01-05 18:14:04 +0000201#define PDBGG(fmt, args...) do {;} while(0) /* placeholder */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203#endif /* _SN9C102_H_ */