Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
Luca Risolia | 7e3a066 | 2007-01-08 11:34:35 -0300 | [diff] [blame] | 2 | * V4L2 driver for ZC0301[P] Image Processor and Control Chip * |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 3 | * * |
Luca Risolia | 7e3a066 | 2007-01-08 11:34:35 -0300 | [diff] [blame] | 4 | * Copyright (C) 2006-2007 by Luca Risolia <luca.risolia@studio.unibo.it> * |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [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 _ZC0301_H_ |
| 22 | #define _ZC0301_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/mutex.h> |
| 36 | #include <linux/rwsem.h> |
Luca Risolia | a847423 | 2006-02-25 06:57:49 +0000 | [diff] [blame] | 37 | #include <linux/stddef.h> |
| 38 | #include <linux/string.h> |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 39 | #include <linux/kref.h> |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 40 | |
| 41 | #include "zc0301_sensor.h" |
| 42 | |
| 43 | /*****************************************************************************/ |
| 44 | |
| 45 | #define ZC0301_DEBUG |
| 46 | #define ZC0301_DEBUG_LEVEL 2 |
| 47 | #define ZC0301_MAX_DEVICES 64 |
| 48 | #define ZC0301_FORCE_MUNMAP 0 |
| 49 | #define ZC0301_MAX_FRAMES 32 |
Luca Risolia | 9e47a52 | 2006-02-08 00:05:27 +0000 | [diff] [blame] | 50 | #define ZC0301_COMPRESSION_QUALITY 0 |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 51 | #define ZC0301_URBS 2 |
| 52 | #define ZC0301_ISO_PACKETS 7 |
| 53 | #define ZC0301_ALTERNATE_SETTING 7 |
| 54 | #define ZC0301_URB_TIMEOUT msecs_to_jiffies(2 * ZC0301_ISO_PACKETS) |
| 55 | #define ZC0301_CTRL_TIMEOUT 100 |
Luca Risolia | a847423 | 2006-02-25 06:57:49 +0000 | [diff] [blame] | 56 | #define ZC0301_FRAME_TIMEOUT 2 |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 57 | |
| 58 | /*****************************************************************************/ |
| 59 | |
| 60 | ZC0301_ID_TABLE |
| 61 | ZC0301_SENSOR_TABLE |
| 62 | |
| 63 | enum zc0301_frame_state { |
| 64 | F_UNUSED, |
| 65 | F_QUEUED, |
| 66 | F_GRABBING, |
| 67 | F_DONE, |
| 68 | F_ERROR, |
| 69 | }; |
| 70 | |
| 71 | struct zc0301_frame_t { |
| 72 | void* bufmem; |
| 73 | struct v4l2_buffer buf; |
| 74 | enum zc0301_frame_state state; |
| 75 | struct list_head frame; |
| 76 | unsigned long vma_use_count; |
| 77 | }; |
| 78 | |
| 79 | enum zc0301_dev_state { |
| 80 | DEV_INITIALIZED = 0x01, |
| 81 | DEV_DISCONNECTED = 0x02, |
| 82 | DEV_MISCONFIGURED = 0x04, |
| 83 | }; |
| 84 | |
| 85 | enum zc0301_io_method { |
| 86 | IO_NONE, |
| 87 | IO_READ, |
| 88 | IO_MMAP, |
| 89 | }; |
| 90 | |
| 91 | enum zc0301_stream_state { |
| 92 | STREAM_OFF, |
| 93 | STREAM_INTERRUPT, |
| 94 | STREAM_ON, |
| 95 | }; |
| 96 | |
| 97 | struct zc0301_module_param { |
| 98 | u8 force_munmap; |
Luca Risolia | a847423 | 2006-02-25 06:57:49 +0000 | [diff] [blame] | 99 | u16 frame_timeout; |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 100 | }; |
| 101 | |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 102 | static DECLARE_RWSEM(zc0301_dev_lock); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 103 | |
| 104 | struct zc0301_device { |
| 105 | struct video_device* v4ldev; |
| 106 | |
Luca Risolia | a847423 | 2006-02-25 06:57:49 +0000 | [diff] [blame] | 107 | struct zc0301_sensor sensor; |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 108 | |
| 109 | struct usb_device* usbdev; |
| 110 | struct urb* urb[ZC0301_URBS]; |
| 111 | void* transfer_buffer[ZC0301_URBS]; |
| 112 | u8* control_buffer; |
| 113 | |
| 114 | struct zc0301_frame_t *frame_current, frame[ZC0301_MAX_FRAMES]; |
| 115 | struct list_head inqueue, outqueue; |
| 116 | u32 frame_count, nbuffers, nreadbuffers; |
| 117 | |
| 118 | enum zc0301_io_method io; |
| 119 | enum zc0301_stream_state stream; |
| 120 | |
| 121 | struct v4l2_jpegcompression compression; |
| 122 | |
| 123 | struct zc0301_module_param module_param; |
| 124 | |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 125 | struct kref kref; |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 126 | enum zc0301_dev_state state; |
| 127 | u8 users; |
| 128 | |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 129 | struct completion probe; |
| 130 | struct mutex open_mutex, fileop_mutex; |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 131 | spinlock_t queue_lock; |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 132 | wait_queue_head_t wait_open, wait_frame, wait_stream; |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | /*****************************************************************************/ |
| 136 | |
Luca Risolia | a847423 | 2006-02-25 06:57:49 +0000 | [diff] [blame] | 137 | struct zc0301_device* |
| 138 | zc0301_match_id(struct zc0301_device* cam, const struct usb_device_id *id) |
| 139 | { |
Luca Risolia | 6e0755a | 2006-03-03 09:58:39 +0000 | [diff] [blame] | 140 | return usb_match_id(usb_ifnum_to_if(cam->usbdev, 0), id) ? cam : NULL; |
Luca Risolia | a847423 | 2006-02-25 06:57:49 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 143 | void |
| 144 | zc0301_attach_sensor(struct zc0301_device* cam, struct zc0301_sensor* sensor) |
| 145 | { |
Luca Risolia | a847423 | 2006-02-25 06:57:49 +0000 | [diff] [blame] | 146 | memcpy(&cam->sensor, sensor, sizeof(struct zc0301_sensor)); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /*****************************************************************************/ |
| 150 | |
| 151 | #undef DBG |
| 152 | #undef KDBG |
| 153 | #ifdef ZC0301_DEBUG |
| 154 | # define DBG(level, fmt, args...) \ |
| 155 | do { \ |
| 156 | if (debug >= (level)) { \ |
| 157 | if ((level) == 1) \ |
| 158 | dev_err(&cam->usbdev->dev, fmt "\n", ## args); \ |
| 159 | else if ((level) == 2) \ |
| 160 | dev_info(&cam->usbdev->dev, fmt "\n", ## args); \ |
| 161 | else if ((level) >= 3) \ |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 162 | dev_info(&cam->usbdev->dev, "[%s:%s:%d] " fmt "\n", \ |
Harvey Harrison | d5a50e4 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 163 | __FILE__, __func__, __LINE__ , ## args); \ |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 164 | } \ |
| 165 | } while (0) |
| 166 | # define KDBG(level, fmt, args...) \ |
| 167 | do { \ |
| 168 | if (debug >= (level)) { \ |
| 169 | if ((level) == 1 || (level) == 2) \ |
| 170 | pr_info("zc0301: " fmt "\n", ## args); \ |
| 171 | else if ((level) == 3) \ |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 172 | pr_debug("sn9c102: [%s:%s:%d] " fmt "\n", __FILE__, \ |
Harvey Harrison | d5a50e4 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 173 | __func__, __LINE__ , ## args); \ |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 174 | } \ |
| 175 | } while (0) |
| 176 | # define V4LDBG(level, name, cmd) \ |
| 177 | do { \ |
| 178 | if (debug >= (level)) \ |
| 179 | v4l_print_ioctl(name, cmd); \ |
| 180 | } while (0) |
| 181 | #else |
| 182 | # define DBG(level, fmt, args...) do {;} while(0) |
| 183 | # define KDBG(level, fmt, args...) do {;} while(0) |
| 184 | # define V4LDBG(level, name, cmd) do {;} while(0) |
| 185 | #endif |
| 186 | |
| 187 | #undef PDBG |
| 188 | #define PDBG(fmt, args...) \ |
Harvey Harrison | d5a50e4 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 189 | dev_info(&cam->usbdev->dev, "[%s:%s:%d] " fmt "\n", __FILE__, __func__, \ |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 190 | __LINE__ , ## args) |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 191 | |
| 192 | #undef PDBGG |
| 193 | #define PDBGG(fmt, args...) do {;} while(0) /* placeholder */ |
| 194 | |
| 195 | #endif /* _ZC0301_H_ */ |