Luca Risolia | 7ce08c9 | 2006-01-11 02:06:59 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 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 Risolia | ccad778 | 2006-02-25 06:54:18 +0000 | [diff] [blame] | 36 | #include <linux/mutex.h> |
| 37 | #include <linux/stddef.h> |
| 38 | #include <linux/string.h> |
Luca Risolia | 3b2ae0b | 2007-06-13 14:52:01 -0300 | [diff] [blame] | 39 | #include <linux/kref.h> |
Luca Risolia | 7ce08c9 | 2006-01-11 02:06:59 +0000 | [diff] [blame] | 40 | |
| 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 Risolia | ccad778 | 2006-02-25 06:54:18 +0000 | [diff] [blame] | 57 | #define ET61X251_FRAME_TIMEOUT 2 |
Luca Risolia | 7ce08c9 | 2006-01-11 02:06:59 +0000 | [diff] [blame] | 58 | |
| 59 | /*****************************************************************************/ |
| 60 | |
| 61 | static const struct usb_device_id et61x251_id_table[] = { |
| 62 | { USB_DEVICE(0x102c, 0x6151), }, |
| 63 | { USB_DEVICE(0x102c, 0x6251), }, |
| 64 | { USB_DEVICE(0x102c, 0x6253), }, |
| 65 | { USB_DEVICE(0x102c, 0x6254), }, |
| 66 | { USB_DEVICE(0x102c, 0x6255), }, |
| 67 | { USB_DEVICE(0x102c, 0x6256), }, |
| 68 | { USB_DEVICE(0x102c, 0x6257), }, |
| 69 | { USB_DEVICE(0x102c, 0x6258), }, |
| 70 | { USB_DEVICE(0x102c, 0x6259), }, |
| 71 | { USB_DEVICE(0x102c, 0x625a), }, |
| 72 | { USB_DEVICE(0x102c, 0x625b), }, |
| 73 | { USB_DEVICE(0x102c, 0x625c), }, |
| 74 | { USB_DEVICE(0x102c, 0x625d), }, |
| 75 | { USB_DEVICE(0x102c, 0x625e), }, |
| 76 | { USB_DEVICE(0x102c, 0x625f), }, |
| 77 | { USB_DEVICE(0x102c, 0x6260), }, |
| 78 | { USB_DEVICE(0x102c, 0x6261), }, |
| 79 | { USB_DEVICE(0x102c, 0x6262), }, |
| 80 | { USB_DEVICE(0x102c, 0x6263), }, |
| 81 | { USB_DEVICE(0x102c, 0x6264), }, |
| 82 | { USB_DEVICE(0x102c, 0x6265), }, |
| 83 | { USB_DEVICE(0x102c, 0x6266), }, |
| 84 | { USB_DEVICE(0x102c, 0x6267), }, |
| 85 | { USB_DEVICE(0x102c, 0x6268), }, |
| 86 | { USB_DEVICE(0x102c, 0x6269), }, |
| 87 | { } |
| 88 | }; |
| 89 | |
| 90 | ET61X251_SENSOR_TABLE |
| 91 | |
| 92 | /*****************************************************************************/ |
| 93 | |
| 94 | enum et61x251_frame_state { |
| 95 | F_UNUSED, |
| 96 | F_QUEUED, |
| 97 | F_GRABBING, |
| 98 | F_DONE, |
| 99 | F_ERROR, |
| 100 | }; |
| 101 | |
| 102 | struct et61x251_frame_t { |
| 103 | void* bufmem; |
| 104 | struct v4l2_buffer buf; |
| 105 | enum et61x251_frame_state state; |
| 106 | struct list_head frame; |
| 107 | unsigned long vma_use_count; |
| 108 | }; |
| 109 | |
| 110 | enum et61x251_dev_state { |
| 111 | DEV_INITIALIZED = 0x01, |
| 112 | DEV_DISCONNECTED = 0x02, |
| 113 | DEV_MISCONFIGURED = 0x04, |
| 114 | }; |
| 115 | |
| 116 | enum et61x251_io_method { |
| 117 | IO_NONE, |
| 118 | IO_READ, |
| 119 | IO_MMAP, |
| 120 | }; |
| 121 | |
| 122 | enum et61x251_stream_state { |
| 123 | STREAM_OFF, |
| 124 | STREAM_INTERRUPT, |
| 125 | STREAM_ON, |
| 126 | }; |
| 127 | |
| 128 | struct et61x251_sysfs_attr { |
| 129 | u8 reg, i2c_reg; |
| 130 | }; |
| 131 | |
| 132 | struct et61x251_module_param { |
| 133 | u8 force_munmap; |
Luca Risolia | ccad778 | 2006-02-25 06:54:18 +0000 | [diff] [blame] | 134 | u16 frame_timeout; |
Luca Risolia | 7ce08c9 | 2006-01-11 02:06:59 +0000 | [diff] [blame] | 135 | }; |
| 136 | |
Luca Risolia | ccad778 | 2006-02-25 06:54:18 +0000 | [diff] [blame] | 137 | static DEFINE_MUTEX(et61x251_sysfs_lock); |
Luca Risolia | 3b2ae0b | 2007-06-13 14:52:01 -0300 | [diff] [blame] | 138 | static DECLARE_RWSEM(et61x251_dev_lock); |
Luca Risolia | 7ce08c9 | 2006-01-11 02:06:59 +0000 | [diff] [blame] | 139 | |
| 140 | struct et61x251_device { |
| 141 | struct video_device* v4ldev; |
| 142 | |
Luca Risolia | ccad778 | 2006-02-25 06:54:18 +0000 | [diff] [blame] | 143 | struct et61x251_sensor sensor; |
Luca Risolia | 7ce08c9 | 2006-01-11 02:06:59 +0000 | [diff] [blame] | 144 | |
| 145 | struct usb_device* usbdev; |
| 146 | struct urb* urb[ET61X251_URBS]; |
| 147 | void* transfer_buffer[ET61X251_URBS]; |
| 148 | u8* control_buffer; |
| 149 | |
| 150 | struct et61x251_frame_t *frame_current, frame[ET61X251_MAX_FRAMES]; |
| 151 | struct list_head inqueue, outqueue; |
| 152 | u32 frame_count, nbuffers, nreadbuffers; |
| 153 | |
| 154 | enum et61x251_io_method io; |
| 155 | enum et61x251_stream_state stream; |
| 156 | |
| 157 | struct v4l2_jpegcompression compression; |
| 158 | |
| 159 | struct et61x251_sysfs_attr sysfs; |
| 160 | struct et61x251_module_param module_param; |
| 161 | |
Luca Risolia | 3b2ae0b | 2007-06-13 14:52:01 -0300 | [diff] [blame] | 162 | struct kref kref; |
Luca Risolia | 7ce08c9 | 2006-01-11 02:06:59 +0000 | [diff] [blame] | 163 | enum et61x251_dev_state state; |
| 164 | u8 users; |
| 165 | |
Luca Risolia | 3b2ae0b | 2007-06-13 14:52:01 -0300 | [diff] [blame] | 166 | struct completion probe; |
| 167 | struct mutex open_mutex, fileop_mutex; |
Luca Risolia | 7ce08c9 | 2006-01-11 02:06:59 +0000 | [diff] [blame] | 168 | spinlock_t queue_lock; |
Luca Risolia | 3b2ae0b | 2007-06-13 14:52:01 -0300 | [diff] [blame] | 169 | wait_queue_head_t wait_open, wait_frame, wait_stream; |
Luca Risolia | 7ce08c9 | 2006-01-11 02:06:59 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | /*****************************************************************************/ |
| 173 | |
Luca Risolia | ccad778 | 2006-02-25 06:54:18 +0000 | [diff] [blame] | 174 | struct et61x251_device* |
| 175 | et61x251_match_id(struct et61x251_device* cam, const struct usb_device_id *id) |
| 176 | { |
Luca Risolia | 2656312 | 2007-01-08 11:38:36 -0300 | [diff] [blame] | 177 | return usb_match_id(usb_ifnum_to_if(cam->usbdev, 0), id) ? cam : NULL; |
Luca Risolia | ccad778 | 2006-02-25 06:54:18 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | |
Luca Risolia | 7ce08c9 | 2006-01-11 02:06:59 +0000 | [diff] [blame] | 181 | void |
| 182 | et61x251_attach_sensor(struct et61x251_device* cam, |
Luca Risolia | 3b2ae0b | 2007-06-13 14:52:01 -0300 | [diff] [blame] | 183 | const struct et61x251_sensor* sensor) |
Luca Risolia | 7ce08c9 | 2006-01-11 02:06:59 +0000 | [diff] [blame] | 184 | { |
Luca Risolia | ccad778 | 2006-02-25 06:54:18 +0000 | [diff] [blame] | 185 | memcpy(&cam->sensor, sensor, sizeof(struct et61x251_sensor)); |
Luca Risolia | 7ce08c9 | 2006-01-11 02:06:59 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /*****************************************************************************/ |
| 189 | |
| 190 | #undef DBG |
| 191 | #undef KDBG |
| 192 | #ifdef ET61X251_DEBUG |
| 193 | # define DBG(level, fmt, args...) \ |
| 194 | do { \ |
| 195 | if (debug >= (level)) { \ |
| 196 | if ((level) == 1) \ |
| 197 | dev_err(&cam->usbdev->dev, fmt "\n", ## args); \ |
| 198 | else if ((level) == 2) \ |
| 199 | dev_info(&cam->usbdev->dev, fmt "\n", ## args); \ |
| 200 | else if ((level) >= 3) \ |
Luca Risolia | 3b2ae0b | 2007-06-13 14:52:01 -0300 | [diff] [blame] | 201 | dev_info(&cam->usbdev->dev, "[%s:%s:%d] " fmt "\n", \ |
Harvey Harrison | 22cc065 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 202 | __FILE__, __func__, __LINE__ , ## args); \ |
Luca Risolia | 7ce08c9 | 2006-01-11 02:06:59 +0000 | [diff] [blame] | 203 | } \ |
| 204 | } while (0) |
| 205 | # define KDBG(level, fmt, args...) \ |
| 206 | do { \ |
| 207 | if (debug >= (level)) { \ |
| 208 | if ((level) == 1 || (level) == 2) \ |
| 209 | pr_info("et61x251: " fmt "\n", ## args); \ |
| 210 | else if ((level) == 3) \ |
Luca Risolia | 3b2ae0b | 2007-06-13 14:52:01 -0300 | [diff] [blame] | 211 | pr_debug("sn9c102: [%s:%s:%d] " fmt "\n", __FILE__, \ |
Harvey Harrison | 22cc065 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 212 | __func__, __LINE__ , ## args); \ |
Luca Risolia | 7ce08c9 | 2006-01-11 02:06:59 +0000 | [diff] [blame] | 213 | } \ |
| 214 | } while (0) |
| 215 | # define V4LDBG(level, name, cmd) \ |
| 216 | do { \ |
| 217 | if (debug >= (level)) \ |
| 218 | v4l_print_ioctl(name, cmd); \ |
| 219 | } while (0) |
| 220 | #else |
| 221 | # define DBG(level, fmt, args...) do {;} while(0) |
| 222 | # define KDBG(level, fmt, args...) do {;} while(0) |
| 223 | # define V4LDBG(level, name, cmd) do {;} while(0) |
| 224 | #endif |
| 225 | |
| 226 | #undef PDBG |
| 227 | #define PDBG(fmt, args...) \ |
Harvey Harrison | 22cc065 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 228 | dev_info(&cam->usbdev->dev, "[%s:%s:%d] " fmt "\n", __FILE__, __func__, \ |
Luca Risolia | 3b2ae0b | 2007-06-13 14:52:01 -0300 | [diff] [blame] | 229 | __LINE__ , ## args) |
Luca Risolia | 7ce08c9 | 2006-01-11 02:06:59 +0000 | [diff] [blame] | 230 | |
| 231 | #undef PDBGG |
| 232 | #define PDBGG(fmt, args...) do {;} while(0) /* placeholder */ |
| 233 | |
| 234 | #endif /* _ET61X251_H_ */ |