Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * V4L2 driver for SN9C10x PC Camera Controllers * |
| 3 | * * |
Luca Risolia | a966f3e | 2006-01-05 18:14:04 +0000 | [diff] [blame] | 4 | * Copyright (C) 2004-2006 by Luca Risolia <luca.risolia@studio.unibo.it> * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 _SN9C102_H_ |
| 22 | #define _SN9C102_H_ |
| 23 | |
| 24 | #include <linux/version.h> |
| 25 | #include <linux/usb.h> |
Luca Risolia | cd6fcc5 | 2006-01-13 17:19:43 +0000 | [diff] [blame] | 26 | #include <linux/videodev2.h> |
| 27 | #include <media/v4l2-common.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame^] | 36 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <asm/semaphore.h> |
| 38 | |
| 39 | #include "sn9c102_sensor.h" |
| 40 | |
| 41 | /*****************************************************************************/ |
| 42 | |
| 43 | #define SN9C102_DEBUG |
| 44 | #define SN9C102_DEBUG_LEVEL 2 |
| 45 | #define SN9C102_MAX_DEVICES 64 |
| 46 | #define SN9C102_PRESERVE_IMGSCALE 0 |
| 47 | #define SN9C102_FORCE_MUNMAP 0 |
| 48 | #define SN9C102_MAX_FRAMES 32 |
| 49 | #define SN9C102_URBS 2 |
| 50 | #define SN9C102_ISO_PACKETS 7 |
| 51 | #define SN9C102_ALTERNATE_SETTING 8 |
| 52 | #define SN9C102_URB_TIMEOUT msecs_to_jiffies(2 * SN9C102_ISO_PACKETS) |
| 53 | #define SN9C102_CTRL_TIMEOUT 300 |
| 54 | |
| 55 | /*****************************************************************************/ |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | enum sn9c102_bridge { |
| 58 | BRIDGE_SN9C101 = 0x01, |
| 59 | BRIDGE_SN9C102 = 0x02, |
| 60 | BRIDGE_SN9C103 = 0x04, |
| 61 | }; |
| 62 | |
| 63 | SN9C102_ID_TABLE |
| 64 | SN9C102_SENSOR_TABLE |
| 65 | |
| 66 | enum sn9c102_frame_state { |
| 67 | F_UNUSED, |
| 68 | F_QUEUED, |
| 69 | F_GRABBING, |
| 70 | F_DONE, |
| 71 | F_ERROR, |
| 72 | }; |
| 73 | |
| 74 | struct sn9c102_frame_t { |
| 75 | void* bufmem; |
| 76 | struct v4l2_buffer buf; |
| 77 | enum sn9c102_frame_state state; |
| 78 | struct list_head frame; |
| 79 | unsigned long vma_use_count; |
| 80 | }; |
| 81 | |
| 82 | enum sn9c102_dev_state { |
| 83 | DEV_INITIALIZED = 0x01, |
| 84 | DEV_DISCONNECTED = 0x02, |
| 85 | DEV_MISCONFIGURED = 0x04, |
| 86 | }; |
| 87 | |
| 88 | enum sn9c102_io_method { |
| 89 | IO_NONE, |
| 90 | IO_READ, |
| 91 | IO_MMAP, |
| 92 | }; |
| 93 | |
| 94 | enum sn9c102_stream_state { |
| 95 | STREAM_OFF, |
| 96 | STREAM_INTERRUPT, |
| 97 | STREAM_ON, |
| 98 | }; |
| 99 | |
Luca Risolia | a966f3e | 2006-01-05 18:14:04 +0000 | [diff] [blame] | 100 | typedef char sn9c103_sof_header_t[18]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | typedef char sn9c102_sof_header_t[12]; |
| 102 | typedef char sn9c102_eof_header_t[4]; |
| 103 | |
| 104 | struct sn9c102_sysfs_attr { |
| 105 | u8 reg, i2c_reg; |
Luca Risolia | a966f3e | 2006-01-05 18:14:04 +0000 | [diff] [blame] | 106 | sn9c103_sof_header_t frame_header; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | struct sn9c102_module_param { |
| 110 | u8 force_munmap; |
| 111 | }; |
| 112 | |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame^] | 113 | static DEFINE_MUTEX(sn9c102_sysfs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | static DECLARE_RWSEM(sn9c102_disconnect); |
| 115 | |
| 116 | struct sn9c102_device { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | struct video_device* v4ldev; |
| 118 | |
| 119 | enum sn9c102_bridge bridge; |
| 120 | struct sn9c102_sensor* sensor; |
| 121 | |
| 122 | struct usb_device* usbdev; |
| 123 | struct urb* urb[SN9C102_URBS]; |
| 124 | void* transfer_buffer[SN9C102_URBS]; |
| 125 | u8* control_buffer; |
| 126 | |
| 127 | struct sn9c102_frame_t *frame_current, frame[SN9C102_MAX_FRAMES]; |
| 128 | struct list_head inqueue, outqueue; |
| 129 | u32 frame_count, nbuffers, nreadbuffers; |
| 130 | |
| 131 | enum sn9c102_io_method io; |
| 132 | enum sn9c102_stream_state stream; |
| 133 | |
| 134 | struct v4l2_jpegcompression compression; |
| 135 | |
| 136 | struct sn9c102_sysfs_attr sysfs; |
Luca Risolia | a966f3e | 2006-01-05 18:14:04 +0000 | [diff] [blame] | 137 | sn9c103_sof_header_t sof_header; |
| 138 | u16 reg[63]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
| 140 | struct sn9c102_module_param module_param; |
| 141 | |
| 142 | enum sn9c102_dev_state state; |
| 143 | u8 users; |
| 144 | |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame^] | 145 | struct mutex dev_mutex, fileop_mutex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | spinlock_t queue_lock; |
| 147 | wait_queue_head_t open, wait_frame, wait_stream; |
| 148 | }; |
| 149 | |
| 150 | /*****************************************************************************/ |
| 151 | |
| 152 | void |
| 153 | sn9c102_attach_sensor(struct sn9c102_device* cam, |
| 154 | struct sn9c102_sensor* sensor) |
| 155 | { |
| 156 | cam->sensor = sensor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | cam->sensor->usbdev = cam->usbdev; |
| 158 | } |
| 159 | |
| 160 | /*****************************************************************************/ |
| 161 | |
| 162 | #undef DBG |
| 163 | #undef KDBG |
| 164 | #ifdef SN9C102_DEBUG |
| 165 | # define DBG(level, fmt, args...) \ |
Luca Risolia | a966f3e | 2006-01-05 18:14:04 +0000 | [diff] [blame] | 166 | do { \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | if (debug >= (level)) { \ |
| 168 | if ((level) == 1) \ |
Luca Risolia | cd6fcc5 | 2006-01-13 17:19:43 +0000 | [diff] [blame] | 169 | dev_err(&cam->usbdev->dev, fmt "\n", ## args); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | else if ((level) == 2) \ |
Luca Risolia | cd6fcc5 | 2006-01-13 17:19:43 +0000 | [diff] [blame] | 171 | dev_info(&cam->usbdev->dev, fmt "\n", ## args); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | else if ((level) >= 3) \ |
Luca Risolia | cd6fcc5 | 2006-01-13 17:19:43 +0000 | [diff] [blame] | 173 | dev_info(&cam->usbdev->dev, "[%s:%d] " fmt "\n", \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | __FUNCTION__, __LINE__ , ## args); \ |
| 175 | } \ |
Luca Risolia | a966f3e | 2006-01-05 18:14:04 +0000 | [diff] [blame] | 176 | } while (0) |
Luca Risolia | cd6fcc5 | 2006-01-13 17:19:43 +0000 | [diff] [blame] | 177 | # define V4LDBG(level, name, cmd) \ |
| 178 | do { \ |
| 179 | if (debug >= (level)) \ |
| 180 | v4l_print_ioctl(name, cmd); \ |
| 181 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | # define KDBG(level, fmt, args...) \ |
Luca Risolia | a966f3e | 2006-01-05 18:14:04 +0000 | [diff] [blame] | 183 | do { \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | if (debug >= (level)) { \ |
| 185 | if ((level) == 1 || (level) == 2) \ |
| 186 | pr_info("sn9c102: " fmt "\n", ## args); \ |
| 187 | else if ((level) == 3) \ |
| 188 | pr_debug("sn9c102: [%s:%d] " fmt "\n", __FUNCTION__, \ |
| 189 | __LINE__ , ## args); \ |
| 190 | } \ |
Luca Risolia | a966f3e | 2006-01-05 18:14:04 +0000 | [diff] [blame] | 191 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | #else |
Luca Risolia | a966f3e | 2006-01-05 18:14:04 +0000 | [diff] [blame] | 193 | # define DBG(level, fmt, args...) do {;} while(0) |
Luca Risolia | cd6fcc5 | 2006-01-13 17:19:43 +0000 | [diff] [blame] | 194 | # define V4LDBG(level, name, cmd) do {;} while(0) |
| 195 | # define KDBG(level, fmt, args...) do {;} while(0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | #endif |
| 197 | |
| 198 | #undef PDBG |
| 199 | #define PDBG(fmt, args...) \ |
Luca Risolia | a966f3e | 2006-01-05 18:14:04 +0000 | [diff] [blame] | 200 | dev_info(&cam->dev, "[%s:%d] " fmt "\n", __FUNCTION__, __LINE__ , ## args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
| 202 | #undef PDBGG |
Luca Risolia | a966f3e | 2006-01-05 18:14:04 +0000 | [diff] [blame] | 203 | #define PDBGG(fmt, args...) do {;} while(0) /* placeholder */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
| 205 | #endif /* _SN9C102_H_ */ |