blob: 8a917f0605035bd3c61b278b02df955ba8484c08 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/usb.h>
Luca Risoliacd6fcc52006-01-13 17:19:43 +000025#include <linux/videodev2.h>
26#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030027#include <media/v4l2-ioctl.h>
Hans Verkuilf1741fa2013-06-12 05:46:30 -030028#include <media/v4l2-device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/device.h>
30#include <linux/list.h>
31#include <linux/spinlock.h>
32#include <linux/time.h>
33#include <linux/wait.h>
34#include <linux/types.h>
35#include <linux/param.h>
36#include <linux/rwsem.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010037#include <linux/mutex.h>
Luca Risolia2ffab022006-02-25 06:50:47 +000038#include <linux/string.h>
39#include <linux/stddef.h>
Luca Risolia3770be32007-06-13 14:37:50 -030040#include <linux/kref.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Luca Risoliaf327ebb2007-01-08 10:43:56 -030042#include "sn9c102_config.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "sn9c102_sensor.h"
Luca Risoliaf327ebb2007-01-08 10:43:56 -030044#include "sn9c102_devtable.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47enum sn9c102_frame_state {
48 F_UNUSED,
49 F_QUEUED,
50 F_GRABBING,
51 F_DONE,
52 F_ERROR,
53};
54
55struct sn9c102_frame_t {
56 void* bufmem;
57 struct v4l2_buffer buf;
58 enum sn9c102_frame_state state;
59 struct list_head frame;
60 unsigned long vma_use_count;
61};
62
63enum sn9c102_dev_state {
64 DEV_INITIALIZED = 0x01,
65 DEV_DISCONNECTED = 0x02,
66 DEV_MISCONFIGURED = 0x04,
67};
68
69enum sn9c102_io_method {
70 IO_NONE,
71 IO_READ,
72 IO_MMAP,
73};
74
75enum sn9c102_stream_state {
76 STREAM_OFF,
77 STREAM_INTERRUPT,
78 STREAM_ON,
79};
80
Luca Risoliaf327ebb2007-01-08 10:43:56 -030081typedef char sn9c102_sof_header_t[62];
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Luca Risoliaf423b9a2007-03-26 16:12:04 -030083struct sn9c102_sof_t {
84 sn9c102_sof_header_t header;
85 u16 bytesread;
86};
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088struct sn9c102_sysfs_attr {
Luca Risoliaf423b9a2007-03-26 16:12:04 -030089 u16 reg, i2c_reg;
Luca Risoliaf327ebb2007-01-08 10:43:56 -030090 sn9c102_sof_header_t frame_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091};
92
93struct sn9c102_module_param {
94 u8 force_munmap;
Luca Risolia2ffab022006-02-25 06:50:47 +000095 u16 frame_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096};
97
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010098static DEFINE_MUTEX(sn9c102_sysfs_lock);
Luca Risolia3770be32007-06-13 14:37:50 -030099static DECLARE_RWSEM(sn9c102_dev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101struct sn9c102_device {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 struct video_device* v4ldev;
103
Hans Verkuilf1741fa2013-06-12 05:46:30 -0300104 struct v4l2_device v4l2_dev;
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 enum sn9c102_bridge bridge;
Luca Risolia2ffab022006-02-25 06:50:47 +0000107 struct sn9c102_sensor sensor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 struct usb_device* usbdev;
110 struct urb* urb[SN9C102_URBS];
111 void* transfer_buffer[SN9C102_URBS];
112 u8* control_buffer;
113
114 struct sn9c102_frame_t *frame_current, frame[SN9C102_MAX_FRAMES];
115 struct list_head inqueue, outqueue;
116 u32 frame_count, nbuffers, nreadbuffers;
117
118 enum sn9c102_io_method io;
119 enum sn9c102_stream_state stream;
120
121 struct v4l2_jpegcompression compression;
122
123 struct sn9c102_sysfs_attr sysfs;
Luca Risoliaf423b9a2007-03-26 16:12:04 -0300124 struct sn9c102_sof_t sof;
Luca Risoliaf327ebb2007-01-08 10:43:56 -0300125 u16 reg[384];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 struct sn9c102_module_param module_param;
128
Luca Risolia3770be32007-06-13 14:37:50 -0300129 struct kref kref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 enum sn9c102_dev_state state;
131 u8 users;
132
Luca Risolia3770be32007-06-13 14:37:50 -0300133 struct completion probe;
134 struct mutex open_mutex, fileop_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 spinlock_t queue_lock;
Luca Risolia3770be32007-06-13 14:37:50 -0300136 wait_queue_head_t wait_open, wait_frame, wait_stream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137};
138
139/*****************************************************************************/
140
Luca Risolia2ffab022006-02-25 06:50:47 +0000141struct sn9c102_device*
142sn9c102_match_id(struct sn9c102_device* cam, const struct usb_device_id *id)
143{
Luca Risoliaf327ebb2007-01-08 10:43:56 -0300144 return usb_match_id(usb_ifnum_to_if(cam->usbdev, 0), id) ? cam : NULL;
Luca Risolia2ffab022006-02-25 06:50:47 +0000145}
146
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148void
149sn9c102_attach_sensor(struct sn9c102_device* cam,
Luca Risolia480b55c22007-05-02 10:04:03 -0300150 const struct sn9c102_sensor* sensor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Luca Risolia2ffab022006-02-25 06:50:47 +0000152 memcpy(&cam->sensor, sensor, sizeof(struct sn9c102_sensor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
Luca Risoliaf327ebb2007-01-08 10:43:56 -0300155
156enum sn9c102_bridge
157sn9c102_get_bridge(struct sn9c102_device* cam)
158{
159 return cam->bridge;
160}
161
162
163struct sn9c102_sensor* sn9c102_get_sensor(struct sn9c102_device* cam)
164{
165 return &cam->sensor;
166}
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/*****************************************************************************/
169
170#undef DBG
171#undef KDBG
172#ifdef SN9C102_DEBUG
173# define DBG(level, fmt, args...) \
Luca Risoliaa966f3e2006-01-05 18:14:04 +0000174do { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (debug >= (level)) { \
176 if ((level) == 1) \
Luca Risoliacd6fcc52006-01-13 17:19:43 +0000177 dev_err(&cam->usbdev->dev, fmt "\n", ## args); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 else if ((level) == 2) \
Luca Risoliacd6fcc52006-01-13 17:19:43 +0000179 dev_info(&cam->usbdev->dev, fmt "\n", ## args); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 else if ((level) >= 3) \
Luca Risoliacd6fcc52006-01-13 17:19:43 +0000181 dev_info(&cam->usbdev->dev, "[%s:%d] " fmt "\n", \
Harvey Harrisona79d13b2008-04-08 23:20:00 -0300182 __func__, __LINE__ , ## args); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 } \
Luca Risoliaa966f3e2006-01-05 18:14:04 +0000184} while (0)
Luca Risoliacd6fcc52006-01-13 17:19:43 +0000185# define V4LDBG(level, name, cmd) \
186do { \
187 if (debug >= (level)) \
Hans Verkuil4a085162012-06-22 06:38:06 -0300188 v4l_printk_ioctl(name, cmd); \
Luca Risoliacd6fcc52006-01-13 17:19:43 +0000189} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190# define KDBG(level, fmt, args...) \
Luca Risoliaa966f3e2006-01-05 18:14:04 +0000191do { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (debug >= (level)) { \
193 if ((level) == 1 || (level) == 2) \
194 pr_info("sn9c102: " fmt "\n", ## args); \
195 else if ((level) == 3) \
Luca Risoliaf423b9a2007-03-26 16:12:04 -0300196 pr_debug("sn9c102: [%s:%d] " fmt "\n", \
Harvey Harrisona79d13b2008-04-08 23:20:00 -0300197 __func__, __LINE__ , ## args); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 } \
Luca Risoliaa966f3e2006-01-05 18:14:04 +0000199} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200#else
Luca Risoliaa966f3e2006-01-05 18:14:04 +0000201# define DBG(level, fmt, args...) do {;} while(0)
Luca Risoliacd6fcc52006-01-13 17:19:43 +0000202# define V4LDBG(level, name, cmd) do {;} while(0)
203# define KDBG(level, fmt, args...) do {;} while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204#endif
205
206#undef PDBG
207#define PDBG(fmt, args...) \
Harvey Harrisona79d13b2008-04-08 23:20:00 -0300208dev_info(&cam->usbdev->dev, "[%s:%s:%d] " fmt "\n", __FILE__, __func__, \
Luca Risoliaf423b9a2007-03-26 16:12:04 -0300209 __LINE__ , ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211#undef PDBGG
Luca Risoliaa966f3e2006-01-05 18:14:04 +0000212#define PDBGG(fmt, args...) do {;} while(0) /* placeholder */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214#endif /* _SN9C102_H_ */