Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
Luca Risolia | a87cde0 | 2006-04-26 10:19:49 -0300 | [diff] [blame] | 2 | * Video4Linux2 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 | * * |
Luca Risolia | 9e47a52 | 2006-02-08 00:05:27 +0000 | [diff] [blame] | 6 | * Informations about the chip internals needed to enable the I2C protocol * |
| 7 | * have been taken from the documentation of the ZC030x Video4Linux1 * |
| 8 | * driver written by Andrew Birkett <andy@nobugs.org> * |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 9 | * * |
| 10 | * This program is free software; you can redistribute it and/or modify * |
| 11 | * it under the terms of the GNU General Public License as published by * |
| 12 | * the Free Software Foundation; either version 2 of the License, or * |
| 13 | * (at your option) any later version. * |
| 14 | * * |
| 15 | * This program is distributed in the hope that it will be useful, * |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 18 | * GNU General Public License for more details. * |
| 19 | * * |
| 20 | * You should have received a copy of the GNU General Public License * |
| 21 | * along with this program; if not, write to the Free Software * |
| 22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * |
| 23 | ***************************************************************************/ |
| 24 | |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/init.h> |
| 27 | #include <linux/kernel.h> |
| 28 | #include <linux/param.h> |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 29 | #include <linux/errno.h> |
| 30 | #include <linux/slab.h> |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 31 | #include <linux/device.h> |
| 32 | #include <linux/fs.h> |
| 33 | #include <linux/delay.h> |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 34 | #include <linux/compiler.h> |
| 35 | #include <linux/ioctl.h> |
| 36 | #include <linux/poll.h> |
| 37 | #include <linux/stat.h> |
| 38 | #include <linux/mm.h> |
| 39 | #include <linux/vmalloc.h> |
| 40 | #include <linux/page-flags.h> |
Harvey Harrison | 9a6ab76 | 2008-05-16 11:20:25 -0700 | [diff] [blame] | 41 | #include <asm/byteorder.h> |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 42 | #include <asm/page.h> |
| 43 | #include <asm/uaccess.h> |
| 44 | |
| 45 | #include "zc0301.h" |
| 46 | |
| 47 | /*****************************************************************************/ |
| 48 | |
Luca Risolia | a87cde0 | 2006-04-26 10:19:49 -0300 | [diff] [blame] | 49 | #define ZC0301_MODULE_NAME "V4L2 driver for ZC0301[P] " \ |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 50 | "Image Processor and Control Chip" |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 51 | #define ZC0301_MODULE_AUTHOR "(C) 2006-2007 Luca Risolia" |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 52 | #define ZC0301_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>" |
| 53 | #define ZC0301_MODULE_LICENSE "GPL" |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 54 | #define ZC0301_MODULE_VERSION "1:1.10" |
| 55 | #define ZC0301_MODULE_VERSION_CODE KERNEL_VERSION(1, 1, 10) |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 56 | |
| 57 | /*****************************************************************************/ |
| 58 | |
| 59 | MODULE_DEVICE_TABLE(usb, zc0301_id_table); |
| 60 | |
| 61 | MODULE_AUTHOR(ZC0301_MODULE_AUTHOR " " ZC0301_AUTHOR_EMAIL); |
| 62 | MODULE_DESCRIPTION(ZC0301_MODULE_NAME); |
| 63 | MODULE_VERSION(ZC0301_MODULE_VERSION); |
| 64 | MODULE_LICENSE(ZC0301_MODULE_LICENSE); |
| 65 | |
| 66 | static short video_nr[] = {[0 ... ZC0301_MAX_DEVICES-1] = -1}; |
| 67 | module_param_array(video_nr, short, NULL, 0444); |
| 68 | MODULE_PARM_DESC(video_nr, |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 69 | "\n<-1|n[,...]> Specify V4L2 minor mode number." |
| 70 | "\n -1 = use next available (default)" |
| 71 | "\n n = use minor number n (integer >= 0)" |
| 72 | "\nYou can specify up to " |
| 73 | __MODULE_STRING(ZC0301_MAX_DEVICES) " cameras this way." |
| 74 | "\nFor example:" |
| 75 | "\nvideo_nr=-1,2,-1 would assign minor number 2 to" |
| 76 | "\nthe second registered camera and use auto for the first" |
| 77 | "\none and for every other camera." |
| 78 | "\n"); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 79 | |
| 80 | static short force_munmap[] = {[0 ... ZC0301_MAX_DEVICES-1] = |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 81 | ZC0301_FORCE_MUNMAP}; |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 82 | module_param_array(force_munmap, bool, NULL, 0444); |
| 83 | MODULE_PARM_DESC(force_munmap, |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 84 | "\n<0|1[,...]> Force the application to unmap previously" |
| 85 | "\nmapped buffer memory before calling any VIDIOC_S_CROP or" |
| 86 | "\nVIDIOC_S_FMT ioctl's. Not all the applications support" |
| 87 | "\nthis feature. This parameter is specific for each" |
| 88 | "\ndetected camera." |
| 89 | "\n 0 = do not force memory unmapping" |
| 90 | "\n 1 = force memory unmapping (save memory)" |
Luca Risolia | 7e3a066 | 2007-01-08 11:34:35 -0300 | [diff] [blame] | 91 | "\nDefault value is "__MODULE_STRING(ZC0301_FORCE_MUNMAP)"." |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 92 | "\n"); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 93 | |
Luca Risolia | a847423 | 2006-02-25 06:57:49 +0000 | [diff] [blame] | 94 | static unsigned int frame_timeout[] = {[0 ... ZC0301_MAX_DEVICES-1] = |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 95 | ZC0301_FRAME_TIMEOUT}; |
Luca Risolia | a847423 | 2006-02-25 06:57:49 +0000 | [diff] [blame] | 96 | module_param_array(frame_timeout, uint, NULL, 0644); |
| 97 | MODULE_PARM_DESC(frame_timeout, |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 98 | "\n<n[,...]> Timeout for a video frame in seconds." |
| 99 | "\nThis parameter is specific for each detected camera." |
| 100 | "\nDefault value is "__MODULE_STRING(ZC0301_FRAME_TIMEOUT)"." |
| 101 | "\n"); |
Luca Risolia | a847423 | 2006-02-25 06:57:49 +0000 | [diff] [blame] | 102 | |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 103 | #ifdef ZC0301_DEBUG |
| 104 | static unsigned short debug = ZC0301_DEBUG_LEVEL; |
| 105 | module_param(debug, ushort, 0644); |
| 106 | MODULE_PARM_DESC(debug, |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 107 | "\n<n> Debugging information level, from 0 to 3:" |
| 108 | "\n0 = none (use carefully)" |
| 109 | "\n1 = critical errors" |
| 110 | "\n2 = significant informations" |
| 111 | "\n3 = more verbose messages" |
| 112 | "\nLevel 3 is useful for testing only, when only " |
| 113 | "one device is used." |
| 114 | "\nDefault value is "__MODULE_STRING(ZC0301_DEBUG_LEVEL)"." |
| 115 | "\n"); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 116 | #endif |
| 117 | |
| 118 | /*****************************************************************************/ |
| 119 | |
| 120 | static u32 |
| 121 | zc0301_request_buffers(struct zc0301_device* cam, u32 count, |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 122 | enum zc0301_io_method io) |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 123 | { |
Luca Risolia | a847423 | 2006-02-25 06:57:49 +0000 | [diff] [blame] | 124 | struct v4l2_pix_format* p = &(cam->sensor.pix_format); |
| 125 | struct v4l2_rect* r = &(cam->sensor.cropcap.bounds); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 126 | const size_t imagesize = cam->module_param.force_munmap || |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 127 | io == IO_READ ? |
| 128 | (p->width * p->height * p->priv) / 8 : |
| 129 | (r->width * r->height * p->priv) / 8; |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 130 | void* buff = NULL; |
| 131 | u32 i; |
| 132 | |
| 133 | if (count > ZC0301_MAX_FRAMES) |
| 134 | count = ZC0301_MAX_FRAMES; |
| 135 | |
| 136 | cam->nbuffers = count; |
| 137 | while (cam->nbuffers > 0) { |
Luca Risolia | 7e3a066 | 2007-01-08 11:34:35 -0300 | [diff] [blame] | 138 | if ((buff = vmalloc_32_user(cam->nbuffers * |
| 139 | PAGE_ALIGN(imagesize)))) |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 140 | break; |
| 141 | cam->nbuffers--; |
| 142 | } |
| 143 | |
| 144 | for (i = 0; i < cam->nbuffers; i++) { |
| 145 | cam->frame[i].bufmem = buff + i*PAGE_ALIGN(imagesize); |
| 146 | cam->frame[i].buf.index = i; |
| 147 | cam->frame[i].buf.m.offset = i*PAGE_ALIGN(imagesize); |
| 148 | cam->frame[i].buf.length = imagesize; |
| 149 | cam->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 150 | cam->frame[i].buf.sequence = 0; |
| 151 | cam->frame[i].buf.field = V4L2_FIELD_NONE; |
| 152 | cam->frame[i].buf.memory = V4L2_MEMORY_MMAP; |
| 153 | cam->frame[i].buf.flags = 0; |
| 154 | } |
| 155 | |
| 156 | return cam->nbuffers; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | static void zc0301_release_buffers(struct zc0301_device* cam) |
| 161 | { |
| 162 | if (cam->nbuffers) { |
| 163 | vfree(cam->frame[0].bufmem); |
| 164 | cam->nbuffers = 0; |
| 165 | } |
| 166 | cam->frame_current = NULL; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | static void zc0301_empty_framequeues(struct zc0301_device* cam) |
| 171 | { |
| 172 | u32 i; |
| 173 | |
| 174 | INIT_LIST_HEAD(&cam->inqueue); |
| 175 | INIT_LIST_HEAD(&cam->outqueue); |
| 176 | |
| 177 | for (i = 0; i < ZC0301_MAX_FRAMES; i++) { |
| 178 | cam->frame[i].state = F_UNUSED; |
| 179 | cam->frame[i].buf.bytesused = 0; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | |
| 184 | static void zc0301_requeue_outqueue(struct zc0301_device* cam) |
| 185 | { |
| 186 | struct zc0301_frame_t *i; |
| 187 | |
| 188 | list_for_each_entry(i, &cam->outqueue, frame) { |
| 189 | i->state = F_QUEUED; |
| 190 | list_add(&i->frame, &cam->inqueue); |
| 191 | } |
| 192 | |
| 193 | INIT_LIST_HEAD(&cam->outqueue); |
| 194 | } |
| 195 | |
| 196 | |
| 197 | static void zc0301_queue_unusedframes(struct zc0301_device* cam) |
| 198 | { |
| 199 | unsigned long lock_flags; |
| 200 | u32 i; |
| 201 | |
| 202 | for (i = 0; i < cam->nbuffers; i++) |
| 203 | if (cam->frame[i].state == F_UNUSED) { |
| 204 | cam->frame[i].state = F_QUEUED; |
| 205 | spin_lock_irqsave(&cam->queue_lock, lock_flags); |
| 206 | list_add_tail(&cam->frame[i].frame, &cam->inqueue); |
| 207 | spin_unlock_irqrestore(&cam->queue_lock, lock_flags); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | /*****************************************************************************/ |
| 212 | |
| 213 | int zc0301_write_reg(struct zc0301_device* cam, u16 index, u16 value) |
| 214 | { |
| 215 | struct usb_device* udev = cam->usbdev; |
| 216 | int res; |
| 217 | |
| 218 | res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0xa0, 0x40, |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 219 | value, index, NULL, 0, ZC0301_CTRL_TIMEOUT); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 220 | if (res < 0) { |
| 221 | DBG(3, "Failed to write a register (index 0x%04X, " |
| 222 | "value 0x%02X, error %d)",index, value, res); |
| 223 | return -1; |
| 224 | } |
| 225 | |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | |
| 230 | int zc0301_read_reg(struct zc0301_device* cam, u16 index) |
| 231 | { |
| 232 | struct usb_device* udev = cam->usbdev; |
| 233 | u8* buff = cam->control_buffer; |
| 234 | int res; |
| 235 | |
| 236 | res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0xa1, 0xc0, |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 237 | 0x0001, index, buff, 1, ZC0301_CTRL_TIMEOUT); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 238 | if (res < 0) |
| 239 | DBG(3, "Failed to read a register (index 0x%04X, error %d)", |
| 240 | index, res); |
| 241 | |
| 242 | PDBGG("Read: index 0x%04X, value: 0x%04X", index, (int)(*buff)); |
| 243 | |
| 244 | return (res >= 0) ? (int)(*buff) : -1; |
| 245 | } |
| 246 | |
| 247 | |
| 248 | int zc0301_i2c_read(struct zc0301_device* cam, u16 address, u8 length) |
| 249 | { |
| 250 | int err = 0, res, r0, r1; |
| 251 | |
| 252 | err += zc0301_write_reg(cam, 0x0092, address); |
| 253 | err += zc0301_write_reg(cam, 0x0090, 0x02); |
| 254 | |
| 255 | msleep(1); |
| 256 | |
| 257 | res = zc0301_read_reg(cam, 0x0091); |
| 258 | if (res < 0) |
| 259 | err += res; |
| 260 | r0 = zc0301_read_reg(cam, 0x0095); |
| 261 | if (r0 < 0) |
| 262 | err += r0; |
| 263 | r1 = zc0301_read_reg(cam, 0x0096); |
| 264 | if (r1 < 0) |
| 265 | err += r1; |
| 266 | |
| 267 | res = (length <= 1) ? r0 : r0 | (r1 << 8); |
| 268 | |
| 269 | if (err) |
| 270 | DBG(3, "I2C read failed at address 0x%04X, value: 0x%04X", |
| 271 | address, res); |
| 272 | |
| 273 | |
| 274 | PDBGG("I2C read: address 0x%04X, value: 0x%04X", address, res); |
| 275 | |
| 276 | return err ? -1 : res; |
| 277 | } |
| 278 | |
| 279 | |
| 280 | int zc0301_i2c_write(struct zc0301_device* cam, u16 address, u16 value) |
| 281 | { |
| 282 | int err = 0, res; |
| 283 | |
| 284 | err += zc0301_write_reg(cam, 0x0092, address); |
| 285 | err += zc0301_write_reg(cam, 0x0093, value & 0xff); |
| 286 | err += zc0301_write_reg(cam, 0x0094, value >> 8); |
| 287 | err += zc0301_write_reg(cam, 0x0090, 0x01); |
| 288 | |
| 289 | msleep(1); |
| 290 | |
| 291 | res = zc0301_read_reg(cam, 0x0091); |
| 292 | if (res < 0) |
| 293 | err += res; |
| 294 | |
| 295 | if (err) |
| 296 | DBG(3, "I2C write failed at address 0x%04X, value: 0x%04X", |
| 297 | address, value); |
| 298 | |
| 299 | PDBGG("I2C write: address 0x%04X, value: 0x%04X", address, value); |
| 300 | |
| 301 | return err ? -1 : 0; |
| 302 | } |
| 303 | |
| 304 | /*****************************************************************************/ |
| 305 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 306 | static void zc0301_urb_complete(struct urb *urb) |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 307 | { |
| 308 | struct zc0301_device* cam = urb->context; |
| 309 | struct zc0301_frame_t** f; |
| 310 | size_t imagesize; |
| 311 | u8 i; |
| 312 | int err = 0; |
| 313 | |
| 314 | if (urb->status == -ENOENT) |
| 315 | return; |
| 316 | |
| 317 | f = &cam->frame_current; |
| 318 | |
| 319 | if (cam->stream == STREAM_INTERRUPT) { |
| 320 | cam->stream = STREAM_OFF; |
| 321 | if ((*f)) |
| 322 | (*f)->state = F_QUEUED; |
| 323 | DBG(3, "Stream interrupted"); |
Luca Risolia | 9e47a52 | 2006-02-08 00:05:27 +0000 | [diff] [blame] | 324 | wake_up(&cam->wait_stream); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | if (cam->state & DEV_DISCONNECTED) |
| 328 | return; |
| 329 | |
| 330 | if (cam->state & DEV_MISCONFIGURED) { |
| 331 | wake_up_interruptible(&cam->wait_frame); |
| 332 | return; |
| 333 | } |
| 334 | |
| 335 | if (cam->stream == STREAM_OFF || list_empty(&cam->inqueue)) |
| 336 | goto resubmit_urb; |
| 337 | |
| 338 | if (!(*f)) |
| 339 | (*f) = list_entry(cam->inqueue.next, struct zc0301_frame_t, |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 340 | frame); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 341 | |
Luca Risolia | a847423 | 2006-02-25 06:57:49 +0000 | [diff] [blame] | 342 | imagesize = (cam->sensor.pix_format.width * |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 343 | cam->sensor.pix_format.height * |
| 344 | cam->sensor.pix_format.priv) / 8; |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 345 | |
| 346 | for (i = 0; i < urb->number_of_packets; i++) { |
| 347 | unsigned int len, status; |
| 348 | void *pos; |
| 349 | u16* soi; |
| 350 | u8 sof; |
| 351 | |
| 352 | len = urb->iso_frame_desc[i].actual_length; |
| 353 | status = urb->iso_frame_desc[i].status; |
| 354 | pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer; |
| 355 | |
| 356 | if (status) { |
| 357 | DBG(3, "Error in isochronous frame"); |
| 358 | (*f)->state = F_ERROR; |
| 359 | continue; |
| 360 | } |
| 361 | |
| 362 | sof = (*(soi = pos) == 0xd8ff); |
| 363 | |
| 364 | PDBGG("Isochrnous frame: length %u, #%u i,", len, i); |
| 365 | |
| 366 | if ((*f)->state == F_QUEUED || (*f)->state == F_ERROR) |
| 367 | start_of_frame: |
| 368 | if (sof) { |
| 369 | (*f)->state = F_GRABBING; |
| 370 | (*f)->buf.bytesused = 0; |
| 371 | do_gettimeofday(&(*f)->buf.timestamp); |
| 372 | DBG(3, "SOF detected: new video frame"); |
| 373 | } |
| 374 | |
| 375 | if ((*f)->state == F_GRABBING) { |
| 376 | if (sof && (*f)->buf.bytesused) |
| 377 | goto end_of_frame; |
| 378 | |
| 379 | if ((*f)->buf.bytesused + len > imagesize) { |
| 380 | DBG(3, "Video frame size exceeded"); |
| 381 | (*f)->state = F_ERROR; |
| 382 | continue; |
| 383 | } |
| 384 | |
| 385 | memcpy((*f)->bufmem+(*f)->buf.bytesused, pos, len); |
| 386 | (*f)->buf.bytesused += len; |
| 387 | |
| 388 | if ((*f)->buf.bytesused == imagesize) { |
| 389 | u32 b; |
| 390 | end_of_frame: |
| 391 | b = (*f)->buf.bytesused; |
| 392 | (*f)->state = F_DONE; |
| 393 | (*f)->buf.sequence= ++cam->frame_count; |
| 394 | spin_lock(&cam->queue_lock); |
| 395 | list_move_tail(&(*f)->frame, &cam->outqueue); |
| 396 | if (!list_empty(&cam->inqueue)) |
| 397 | (*f) = list_entry(cam->inqueue.next, |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 398 | struct zc0301_frame_t, |
| 399 | frame); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 400 | else |
| 401 | (*f) = NULL; |
| 402 | spin_unlock(&cam->queue_lock); |
| 403 | DBG(3, "Video frame captured: : %lu bytes", |
| 404 | (unsigned long)(b)); |
| 405 | |
| 406 | if (!(*f)) |
| 407 | goto resubmit_urb; |
| 408 | |
| 409 | if (sof) |
| 410 | goto start_of_frame; |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | resubmit_urb: |
| 416 | urb->dev = cam->usbdev; |
| 417 | err = usb_submit_urb(urb, GFP_ATOMIC); |
| 418 | if (err < 0 && err != -EPERM) { |
| 419 | cam->state |= DEV_MISCONFIGURED; |
| 420 | DBG(1, "usb_submit_urb() failed"); |
| 421 | } |
| 422 | |
| 423 | wake_up_interruptible(&cam->wait_frame); |
| 424 | } |
| 425 | |
| 426 | |
| 427 | static int zc0301_start_transfer(struct zc0301_device* cam) |
| 428 | { |
| 429 | struct usb_device *udev = cam->usbdev; |
Luca Risolia | a87cde0 | 2006-04-26 10:19:49 -0300 | [diff] [blame] | 430 | struct usb_host_interface* altsetting = usb_altnum_to_altsetting( |
| 431 | usb_ifnum_to_if(udev, 0), |
| 432 | ZC0301_ALTERNATE_SETTING); |
Luca Risolia | 7e3a066 | 2007-01-08 11:34:35 -0300 | [diff] [blame] | 433 | const unsigned int psz = le16_to_cpu(altsetting-> |
| 434 | endpoint[0].desc.wMaxPacketSize); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 435 | struct urb* urb; |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 436 | s8 i, j; |
| 437 | int err = 0; |
| 438 | |
| 439 | for (i = 0; i < ZC0301_URBS; i++) { |
| 440 | cam->transfer_buffer[i] = kzalloc(ZC0301_ISO_PACKETS * psz, |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 441 | GFP_KERNEL); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 442 | if (!cam->transfer_buffer[i]) { |
| 443 | err = -ENOMEM; |
| 444 | DBG(1, "Not enough memory"); |
| 445 | goto free_buffers; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | for (i = 0; i < ZC0301_URBS; i++) { |
| 450 | urb = usb_alloc_urb(ZC0301_ISO_PACKETS, GFP_KERNEL); |
| 451 | cam->urb[i] = urb; |
| 452 | if (!urb) { |
| 453 | err = -ENOMEM; |
| 454 | DBG(1, "usb_alloc_urb() failed"); |
| 455 | goto free_urbs; |
| 456 | } |
| 457 | urb->dev = udev; |
| 458 | urb->context = cam; |
| 459 | urb->pipe = usb_rcvisocpipe(udev, 1); |
| 460 | urb->transfer_flags = URB_ISO_ASAP; |
| 461 | urb->number_of_packets = ZC0301_ISO_PACKETS; |
| 462 | urb->complete = zc0301_urb_complete; |
| 463 | urb->transfer_buffer = cam->transfer_buffer[i]; |
| 464 | urb->transfer_buffer_length = psz * ZC0301_ISO_PACKETS; |
| 465 | urb->interval = 1; |
| 466 | for (j = 0; j < ZC0301_ISO_PACKETS; j++) { |
| 467 | urb->iso_frame_desc[j].offset = psz * j; |
| 468 | urb->iso_frame_desc[j].length = psz; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | err = usb_set_interface(udev, 0, ZC0301_ALTERNATE_SETTING); |
| 473 | if (err) { |
| 474 | DBG(1, "usb_set_interface() failed"); |
| 475 | goto free_urbs; |
| 476 | } |
| 477 | |
| 478 | cam->frame_current = NULL; |
| 479 | |
| 480 | for (i = 0; i < ZC0301_URBS; i++) { |
| 481 | err = usb_submit_urb(cam->urb[i], GFP_KERNEL); |
| 482 | if (err) { |
| 483 | for (j = i-1; j >= 0; j--) |
| 484 | usb_kill_urb(cam->urb[j]); |
| 485 | DBG(1, "usb_submit_urb() failed, error %d", err); |
| 486 | goto free_urbs; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | return 0; |
| 491 | |
| 492 | free_urbs: |
Luca Risolia | 7e3a066 | 2007-01-08 11:34:35 -0300 | [diff] [blame] | 493 | for (i = 0; (i < ZC0301_URBS) && cam->urb[i]; i++) |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 494 | usb_free_urb(cam->urb[i]); |
| 495 | |
| 496 | free_buffers: |
| 497 | for (i = 0; (i < ZC0301_URBS) && cam->transfer_buffer[i]; i++) |
| 498 | kfree(cam->transfer_buffer[i]); |
| 499 | |
| 500 | return err; |
| 501 | } |
| 502 | |
| 503 | |
| 504 | static int zc0301_stop_transfer(struct zc0301_device* cam) |
| 505 | { |
| 506 | struct usb_device *udev = cam->usbdev; |
| 507 | s8 i; |
| 508 | int err = 0; |
| 509 | |
| 510 | if (cam->state & DEV_DISCONNECTED) |
| 511 | return 0; |
| 512 | |
| 513 | for (i = ZC0301_URBS-1; i >= 0; i--) { |
| 514 | usb_kill_urb(cam->urb[i]); |
| 515 | usb_free_urb(cam->urb[i]); |
| 516 | kfree(cam->transfer_buffer[i]); |
| 517 | } |
| 518 | |
| 519 | err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */ |
| 520 | if (err) |
| 521 | DBG(3, "usb_set_interface() failed"); |
| 522 | |
| 523 | return err; |
| 524 | } |
| 525 | |
| 526 | |
| 527 | static int zc0301_stream_interrupt(struct zc0301_device* cam) |
| 528 | { |
| 529 | long timeout; |
| 530 | |
| 531 | cam->stream = STREAM_INTERRUPT; |
| 532 | timeout = wait_event_timeout(cam->wait_stream, |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 533 | (cam->stream == STREAM_OFF) || |
| 534 | (cam->state & DEV_DISCONNECTED), |
| 535 | ZC0301_URB_TIMEOUT); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 536 | if (cam->state & DEV_DISCONNECTED) |
| 537 | return -ENODEV; |
Luca Risolia | 9e47a52 | 2006-02-08 00:05:27 +0000 | [diff] [blame] | 538 | else if (cam->stream != STREAM_OFF) { |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 539 | cam->state |= DEV_MISCONFIGURED; |
| 540 | DBG(1, "URB timeout reached. The camera is misconfigured. To " |
| 541 | "use it, close and open /dev/video%d again.", |
| 542 | cam->v4ldev->minor); |
| 543 | return -EIO; |
| 544 | } |
| 545 | |
| 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | /*****************************************************************************/ |
| 550 | |
| 551 | static int |
| 552 | zc0301_set_compression(struct zc0301_device* cam, |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 553 | struct v4l2_jpegcompression* compression) |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 554 | { |
| 555 | int r, err = 0; |
| 556 | |
| 557 | if ((r = zc0301_read_reg(cam, 0x0008)) < 0) |
| 558 | err += r; |
Luca Risolia | 9e47a52 | 2006-02-08 00:05:27 +0000 | [diff] [blame] | 559 | err += zc0301_write_reg(cam, 0x0008, r | 0x11 | compression->quality); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 560 | |
| 561 | return err ? -EIO : 0; |
| 562 | } |
| 563 | |
| 564 | |
| 565 | static int zc0301_init(struct zc0301_device* cam) |
| 566 | { |
Luca Risolia | a847423 | 2006-02-25 06:57:49 +0000 | [diff] [blame] | 567 | struct zc0301_sensor* s = &cam->sensor; |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 568 | struct v4l2_control ctrl; |
| 569 | struct v4l2_queryctrl *qctrl; |
| 570 | struct v4l2_rect* rect; |
| 571 | u8 i = 0; |
| 572 | int err = 0; |
| 573 | |
| 574 | if (!(cam->state & DEV_INITIALIZED)) { |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 575 | mutex_init(&cam->open_mutex); |
| 576 | init_waitqueue_head(&cam->wait_open); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 577 | qctrl = s->qctrl; |
| 578 | rect = &(s->cropcap.defrect); |
| 579 | cam->compression.quality = ZC0301_COMPRESSION_QUALITY; |
| 580 | } else { /* use current values */ |
| 581 | qctrl = s->_qctrl; |
| 582 | rect = &(s->_rect); |
| 583 | } |
| 584 | |
| 585 | if (s->init) { |
| 586 | err = s->init(cam); |
| 587 | if (err) { |
| 588 | DBG(3, "Sensor initialization failed"); |
| 589 | return err; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | if ((err = zc0301_set_compression(cam, &cam->compression))) { |
| 594 | DBG(3, "set_compression() failed"); |
| 595 | return err; |
| 596 | } |
| 597 | |
| 598 | if (s->set_crop) |
| 599 | if ((err = s->set_crop(cam, rect))) { |
| 600 | DBG(3, "set_crop() failed"); |
| 601 | return err; |
| 602 | } |
| 603 | |
| 604 | if (s->set_ctrl) { |
| 605 | for (i = 0; i < ARRAY_SIZE(s->qctrl); i++) |
| 606 | if (s->qctrl[i].id != 0 && |
| 607 | !(s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)) { |
| 608 | ctrl.id = s->qctrl[i].id; |
| 609 | ctrl.value = qctrl[i].default_value; |
| 610 | err = s->set_ctrl(cam, &ctrl); |
| 611 | if (err) { |
| 612 | DBG(3, "Set %s control failed", |
| 613 | s->qctrl[i].name); |
| 614 | return err; |
| 615 | } |
| 616 | DBG(3, "Image sensor supports '%s' control", |
| 617 | s->qctrl[i].name); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | if (!(cam->state & DEV_INITIALIZED)) { |
| 622 | mutex_init(&cam->fileop_mutex); |
| 623 | spin_lock_init(&cam->queue_lock); |
| 624 | init_waitqueue_head(&cam->wait_frame); |
| 625 | init_waitqueue_head(&cam->wait_stream); |
| 626 | cam->nreadbuffers = 2; |
| 627 | memcpy(s->_qctrl, s->qctrl, sizeof(s->qctrl)); |
| 628 | memcpy(&(s->_rect), &(s->cropcap.defrect), |
| 629 | sizeof(struct v4l2_rect)); |
| 630 | cam->state |= DEV_INITIALIZED; |
| 631 | } |
| 632 | |
| 633 | DBG(2, "Initialization succeeded"); |
| 634 | return 0; |
| 635 | } |
| 636 | |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 637 | /*****************************************************************************/ |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 638 | |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 639 | static void zc0301_release_resources(struct kref *kref) |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 640 | { |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 641 | struct zc0301_device *cam = container_of(kref, struct zc0301_device, |
| 642 | kref); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 643 | DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor); |
| 644 | video_set_drvdata(cam->v4ldev, NULL); |
| 645 | video_unregister_device(cam->v4ldev); |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 646 | usb_put_dev(cam->usbdev); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 647 | kfree(cam->control_buffer); |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 648 | kfree(cam); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 651 | |
| 652 | static int zc0301_open(struct inode* inode, struct file* filp) |
| 653 | { |
| 654 | struct zc0301_device* cam; |
| 655 | int err = 0; |
| 656 | |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 657 | if (!down_read_trylock(&zc0301_dev_lock)) |
Jiri Slaby | c3e2a8e | 2007-10-09 22:15:27 -0300 | [diff] [blame] | 658 | return -EAGAIN; |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 659 | |
| 660 | cam = video_get_drvdata(video_devdata(filp)); |
| 661 | |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 662 | if (wait_for_completion_interruptible(&cam->probe)) { |
| 663 | up_read(&zc0301_dev_lock); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 664 | return -ERESTARTSYS; |
| 665 | } |
| 666 | |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 667 | kref_get(&cam->kref); |
| 668 | |
| 669 | if (mutex_lock_interruptible(&cam->open_mutex)) { |
| 670 | kref_put(&cam->kref, zc0301_release_resources); |
| 671 | up_read(&zc0301_dev_lock); |
| 672 | return -ERESTARTSYS; |
| 673 | } |
| 674 | |
| 675 | if (cam->state & DEV_DISCONNECTED) { |
| 676 | DBG(1, "Device not present"); |
| 677 | err = -ENODEV; |
| 678 | goto out; |
| 679 | } |
| 680 | |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 681 | if (cam->users) { |
| 682 | DBG(2, "Device /dev/video%d is busy...", cam->v4ldev->minor); |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 683 | DBG(3, "Simultaneous opens are not supported"); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 684 | if ((filp->f_flags & O_NONBLOCK) || |
| 685 | (filp->f_flags & O_NDELAY)) { |
| 686 | err = -EWOULDBLOCK; |
| 687 | goto out; |
| 688 | } |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 689 | DBG(2, "A blocking open() has been requested. Wait for the " |
| 690 | "device to be released..."); |
| 691 | up_read(&zc0301_dev_lock); |
| 692 | err = wait_event_interruptible_exclusive(cam->wait_open, |
| 693 | (cam->state & DEV_DISCONNECTED) |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 694 | || !cam->users); |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 695 | down_read(&zc0301_dev_lock); |
| 696 | if (err) |
| 697 | goto out; |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 698 | if (cam->state & DEV_DISCONNECTED) { |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 699 | err = -ENODEV; |
| 700 | goto out; |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 701 | } |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 704 | if (cam->state & DEV_MISCONFIGURED) { |
| 705 | err = zc0301_init(cam); |
| 706 | if (err) { |
| 707 | DBG(1, "Initialization failed again. " |
| 708 | "I will retry on next open()."); |
| 709 | goto out; |
| 710 | } |
| 711 | cam->state &= ~DEV_MISCONFIGURED; |
| 712 | } |
| 713 | |
| 714 | if ((err = zc0301_start_transfer(cam))) |
| 715 | goto out; |
| 716 | |
| 717 | filp->private_data = cam; |
| 718 | cam->users++; |
| 719 | cam->io = IO_NONE; |
| 720 | cam->stream = STREAM_OFF; |
| 721 | cam->nbuffers = 0; |
| 722 | cam->frame_count = 0; |
| 723 | zc0301_empty_framequeues(cam); |
| 724 | |
| 725 | DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor); |
| 726 | |
| 727 | out: |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 728 | mutex_unlock(&cam->open_mutex); |
| 729 | if (err) |
| 730 | kref_put(&cam->kref, zc0301_release_resources); |
| 731 | up_read(&zc0301_dev_lock); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 732 | return err; |
| 733 | } |
| 734 | |
| 735 | |
| 736 | static int zc0301_release(struct inode* inode, struct file* filp) |
| 737 | { |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 738 | struct zc0301_device* cam; |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 739 | |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 740 | down_write(&zc0301_dev_lock); |
| 741 | |
| 742 | cam = video_get_drvdata(video_devdata(filp)); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 743 | |
| 744 | zc0301_stop_transfer(cam); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 745 | zc0301_release_buffers(cam); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 746 | cam->users--; |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 747 | wake_up_interruptible_nr(&cam->wait_open, 1); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 748 | |
| 749 | DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor); |
| 750 | |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 751 | kref_put(&cam->kref, zc0301_release_resources); |
| 752 | |
| 753 | up_write(&zc0301_dev_lock); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 754 | |
| 755 | return 0; |
| 756 | } |
| 757 | |
| 758 | |
| 759 | static ssize_t |
Luca Risolia | 9e47a52 | 2006-02-08 00:05:27 +0000 | [diff] [blame] | 760 | zc0301_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos) |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 761 | { |
| 762 | struct zc0301_device* cam = video_get_drvdata(video_devdata(filp)); |
| 763 | struct zc0301_frame_t* f, * i; |
| 764 | unsigned long lock_flags; |
| 765 | long timeout; |
| 766 | int err = 0; |
| 767 | |
| 768 | if (mutex_lock_interruptible(&cam->fileop_mutex)) |
| 769 | return -ERESTARTSYS; |
| 770 | |
| 771 | if (cam->state & DEV_DISCONNECTED) { |
| 772 | DBG(1, "Device not present"); |
| 773 | mutex_unlock(&cam->fileop_mutex); |
| 774 | return -ENODEV; |
| 775 | } |
| 776 | |
| 777 | if (cam->state & DEV_MISCONFIGURED) { |
| 778 | DBG(1, "The camera is misconfigured. Close and open it " |
| 779 | "again."); |
| 780 | mutex_unlock(&cam->fileop_mutex); |
| 781 | return -EIO; |
| 782 | } |
| 783 | |
| 784 | if (cam->io == IO_MMAP) { |
| 785 | DBG(3, "Close and open the device again to choose the read " |
| 786 | "method"); |
| 787 | mutex_unlock(&cam->fileop_mutex); |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 788 | return -EBUSY; |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | if (cam->io == IO_NONE) { |
| 792 | if (!zc0301_request_buffers(cam, cam->nreadbuffers, IO_READ)) { |
| 793 | DBG(1, "read() failed, not enough memory"); |
| 794 | mutex_unlock(&cam->fileop_mutex); |
| 795 | return -ENOMEM; |
| 796 | } |
| 797 | cam->io = IO_READ; |
| 798 | cam->stream = STREAM_ON; |
| 799 | } |
| 800 | |
| 801 | if (list_empty(&cam->inqueue)) { |
| 802 | if (!list_empty(&cam->outqueue)) |
| 803 | zc0301_empty_framequeues(cam); |
| 804 | zc0301_queue_unusedframes(cam); |
| 805 | } |
| 806 | |
| 807 | if (!count) { |
| 808 | mutex_unlock(&cam->fileop_mutex); |
| 809 | return 0; |
| 810 | } |
| 811 | |
| 812 | if (list_empty(&cam->outqueue)) { |
| 813 | if (filp->f_flags & O_NONBLOCK) { |
| 814 | mutex_unlock(&cam->fileop_mutex); |
| 815 | return -EAGAIN; |
| 816 | } |
| 817 | timeout = wait_event_interruptible_timeout |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 818 | ( cam->wait_frame, |
| 819 | (!list_empty(&cam->outqueue)) || |
| 820 | (cam->state & DEV_DISCONNECTED) || |
| 821 | (cam->state & DEV_MISCONFIGURED), |
| 822 | cam->module_param.frame_timeout * |
| 823 | 1000 * msecs_to_jiffies(1) ); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 824 | if (timeout < 0) { |
| 825 | mutex_unlock(&cam->fileop_mutex); |
| 826 | return timeout; |
| 827 | } |
| 828 | if (cam->state & DEV_DISCONNECTED) { |
| 829 | mutex_unlock(&cam->fileop_mutex); |
| 830 | return -ENODEV; |
| 831 | } |
| 832 | if (!timeout || (cam->state & DEV_MISCONFIGURED)) { |
| 833 | mutex_unlock(&cam->fileop_mutex); |
| 834 | return -EIO; |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | f = list_entry(cam->outqueue.prev, struct zc0301_frame_t, frame); |
| 839 | |
| 840 | if (count > f->buf.bytesused) |
| 841 | count = f->buf.bytesused; |
| 842 | |
| 843 | if (copy_to_user(buf, f->bufmem, count)) { |
| 844 | err = -EFAULT; |
| 845 | goto exit; |
| 846 | } |
| 847 | *f_pos += count; |
| 848 | |
| 849 | exit: |
| 850 | spin_lock_irqsave(&cam->queue_lock, lock_flags); |
| 851 | list_for_each_entry(i, &cam->outqueue, frame) |
| 852 | i->state = F_UNUSED; |
| 853 | INIT_LIST_HEAD(&cam->outqueue); |
| 854 | spin_unlock_irqrestore(&cam->queue_lock, lock_flags); |
| 855 | |
| 856 | zc0301_queue_unusedframes(cam); |
| 857 | |
| 858 | PDBGG("Frame #%lu, bytes read: %zu", |
| 859 | (unsigned long)f->buf.index, count); |
| 860 | |
| 861 | mutex_unlock(&cam->fileop_mutex); |
| 862 | |
| 863 | return err ? err : count; |
| 864 | } |
| 865 | |
| 866 | |
| 867 | static unsigned int zc0301_poll(struct file *filp, poll_table *wait) |
| 868 | { |
| 869 | struct zc0301_device* cam = video_get_drvdata(video_devdata(filp)); |
| 870 | struct zc0301_frame_t* f; |
| 871 | unsigned long lock_flags; |
| 872 | unsigned int mask = 0; |
| 873 | |
| 874 | if (mutex_lock_interruptible(&cam->fileop_mutex)) |
| 875 | return POLLERR; |
| 876 | |
| 877 | if (cam->state & DEV_DISCONNECTED) { |
| 878 | DBG(1, "Device not present"); |
| 879 | goto error; |
| 880 | } |
| 881 | |
| 882 | if (cam->state & DEV_MISCONFIGURED) { |
| 883 | DBG(1, "The camera is misconfigured. Close and open it " |
| 884 | "again."); |
| 885 | goto error; |
| 886 | } |
| 887 | |
| 888 | if (cam->io == IO_NONE) { |
| 889 | if (!zc0301_request_buffers(cam, cam->nreadbuffers, IO_READ)) { |
| 890 | DBG(1, "poll() failed, not enough memory"); |
| 891 | goto error; |
| 892 | } |
| 893 | cam->io = IO_READ; |
| 894 | cam->stream = STREAM_ON; |
| 895 | } |
| 896 | |
| 897 | if (cam->io == IO_READ) { |
| 898 | spin_lock_irqsave(&cam->queue_lock, lock_flags); |
| 899 | list_for_each_entry(f, &cam->outqueue, frame) |
| 900 | f->state = F_UNUSED; |
| 901 | INIT_LIST_HEAD(&cam->outqueue); |
| 902 | spin_unlock_irqrestore(&cam->queue_lock, lock_flags); |
| 903 | zc0301_queue_unusedframes(cam); |
| 904 | } |
| 905 | |
| 906 | poll_wait(filp, &cam->wait_frame, wait); |
| 907 | |
| 908 | if (!list_empty(&cam->outqueue)) |
| 909 | mask |= POLLIN | POLLRDNORM; |
| 910 | |
| 911 | mutex_unlock(&cam->fileop_mutex); |
| 912 | |
| 913 | return mask; |
| 914 | |
| 915 | error: |
| 916 | mutex_unlock(&cam->fileop_mutex); |
| 917 | return POLLERR; |
| 918 | } |
| 919 | |
| 920 | |
| 921 | static void zc0301_vm_open(struct vm_area_struct* vma) |
| 922 | { |
| 923 | struct zc0301_frame_t* f = vma->vm_private_data; |
| 924 | f->vma_use_count++; |
| 925 | } |
| 926 | |
| 927 | |
| 928 | static void zc0301_vm_close(struct vm_area_struct* vma) |
| 929 | { |
| 930 | /* NOTE: buffers are not freed here */ |
| 931 | struct zc0301_frame_t* f = vma->vm_private_data; |
| 932 | f->vma_use_count--; |
| 933 | } |
| 934 | |
| 935 | |
| 936 | static struct vm_operations_struct zc0301_vm_ops = { |
| 937 | .open = zc0301_vm_open, |
| 938 | .close = zc0301_vm_close, |
| 939 | }; |
| 940 | |
| 941 | |
| 942 | static int zc0301_mmap(struct file* filp, struct vm_area_struct *vma) |
| 943 | { |
| 944 | struct zc0301_device* cam = video_get_drvdata(video_devdata(filp)); |
| 945 | unsigned long size = vma->vm_end - vma->vm_start, |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 946 | start = vma->vm_start; |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 947 | void *pos; |
| 948 | u32 i; |
| 949 | |
| 950 | if (mutex_lock_interruptible(&cam->fileop_mutex)) |
| 951 | return -ERESTARTSYS; |
| 952 | |
| 953 | if (cam->state & DEV_DISCONNECTED) { |
| 954 | DBG(1, "Device not present"); |
| 955 | mutex_unlock(&cam->fileop_mutex); |
| 956 | return -ENODEV; |
| 957 | } |
| 958 | |
| 959 | if (cam->state & DEV_MISCONFIGURED) { |
| 960 | DBG(1, "The camera is misconfigured. Close and open it " |
| 961 | "again."); |
| 962 | mutex_unlock(&cam->fileop_mutex); |
| 963 | return -EIO; |
| 964 | } |
| 965 | |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 966 | if (!(vma->vm_flags & (VM_WRITE | VM_READ))) { |
| 967 | mutex_unlock(&cam->fileop_mutex); |
| 968 | return -EACCES; |
| 969 | } |
| 970 | |
| 971 | if (cam->io != IO_MMAP || |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 972 | size != PAGE_ALIGN(cam->frame[0].buf.length)) { |
| 973 | mutex_unlock(&cam->fileop_mutex); |
| 974 | return -EINVAL; |
| 975 | } |
| 976 | |
| 977 | for (i = 0; i < cam->nbuffers; i++) { |
| 978 | if ((cam->frame[i].buf.m.offset>>PAGE_SHIFT) == vma->vm_pgoff) |
| 979 | break; |
| 980 | } |
| 981 | if (i == cam->nbuffers) { |
| 982 | mutex_unlock(&cam->fileop_mutex); |
| 983 | return -EINVAL; |
| 984 | } |
| 985 | |
| 986 | vma->vm_flags |= VM_IO; |
| 987 | vma->vm_flags |= VM_RESERVED; |
| 988 | |
| 989 | pos = cam->frame[i].bufmem; |
| 990 | while (size > 0) { /* size is page-aligned */ |
| 991 | if (vm_insert_page(vma, start, vmalloc_to_page(pos))) { |
| 992 | mutex_unlock(&cam->fileop_mutex); |
| 993 | return -EAGAIN; |
| 994 | } |
| 995 | start += PAGE_SIZE; |
| 996 | pos += PAGE_SIZE; |
| 997 | size -= PAGE_SIZE; |
| 998 | } |
| 999 | |
| 1000 | vma->vm_ops = &zc0301_vm_ops; |
| 1001 | vma->vm_private_data = &cam->frame[i]; |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1002 | zc0301_vm_open(vma); |
| 1003 | |
| 1004 | mutex_unlock(&cam->fileop_mutex); |
| 1005 | |
| 1006 | return 0; |
| 1007 | } |
| 1008 | |
| 1009 | /*****************************************************************************/ |
| 1010 | |
| 1011 | static int |
| 1012 | zc0301_vidioc_querycap(struct zc0301_device* cam, void __user * arg) |
| 1013 | { |
| 1014 | struct v4l2_capability cap = { |
| 1015 | .driver = "zc0301", |
| 1016 | .version = ZC0301_MODULE_VERSION_CODE, |
| 1017 | .capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE | |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 1018 | V4L2_CAP_STREAMING, |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1019 | }; |
| 1020 | |
| 1021 | strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card)); |
| 1022 | if (usb_make_path(cam->usbdev, cap.bus_info, sizeof(cap.bus_info)) < 0) |
| 1023 | strlcpy(cap.bus_info, cam->usbdev->dev.bus_id, |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 1024 | sizeof(cap.bus_info)); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1025 | |
| 1026 | if (copy_to_user(arg, &cap, sizeof(cap))) |
| 1027 | return -EFAULT; |
| 1028 | |
| 1029 | return 0; |
| 1030 | } |
| 1031 | |
| 1032 | |
| 1033 | static int |
| 1034 | zc0301_vidioc_enuminput(struct zc0301_device* cam, void __user * arg) |
| 1035 | { |
| 1036 | struct v4l2_input i; |
| 1037 | |
| 1038 | if (copy_from_user(&i, arg, sizeof(i))) |
| 1039 | return -EFAULT; |
| 1040 | |
| 1041 | if (i.index) |
| 1042 | return -EINVAL; |
| 1043 | |
| 1044 | memset(&i, 0, sizeof(i)); |
| 1045 | strcpy(i.name, "Camera"); |
Luca Risolia | 9e47a52 | 2006-02-08 00:05:27 +0000 | [diff] [blame] | 1046 | i.type = V4L2_INPUT_TYPE_CAMERA; |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1047 | |
| 1048 | if (copy_to_user(arg, &i, sizeof(i))) |
| 1049 | return -EFAULT; |
| 1050 | |
| 1051 | return 0; |
| 1052 | } |
| 1053 | |
| 1054 | |
| 1055 | static int |
Luca Risolia | 9e47a52 | 2006-02-08 00:05:27 +0000 | [diff] [blame] | 1056 | zc0301_vidioc_g_input(struct zc0301_device* cam, void __user * arg) |
| 1057 | { |
| 1058 | int index = 0; |
| 1059 | |
| 1060 | if (copy_to_user(arg, &index, sizeof(index))) |
| 1061 | return -EFAULT; |
| 1062 | |
| 1063 | return 0; |
| 1064 | } |
| 1065 | |
| 1066 | |
| 1067 | static int |
| 1068 | zc0301_vidioc_s_input(struct zc0301_device* cam, void __user * arg) |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1069 | { |
| 1070 | int index; |
| 1071 | |
| 1072 | if (copy_from_user(&index, arg, sizeof(index))) |
| 1073 | return -EFAULT; |
| 1074 | |
| 1075 | if (index != 0) |
| 1076 | return -EINVAL; |
| 1077 | |
| 1078 | return 0; |
| 1079 | } |
| 1080 | |
| 1081 | |
| 1082 | static int |
| 1083 | zc0301_vidioc_query_ctrl(struct zc0301_device* cam, void __user * arg) |
| 1084 | { |
Luca Risolia | a847423 | 2006-02-25 06:57:49 +0000 | [diff] [blame] | 1085 | struct zc0301_sensor* s = &cam->sensor; |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1086 | struct v4l2_queryctrl qc; |
| 1087 | u8 i; |
| 1088 | |
| 1089 | if (copy_from_user(&qc, arg, sizeof(qc))) |
| 1090 | return -EFAULT; |
| 1091 | |
| 1092 | for (i = 0; i < ARRAY_SIZE(s->qctrl); i++) |
| 1093 | if (qc.id && qc.id == s->qctrl[i].id) { |
| 1094 | memcpy(&qc, &(s->qctrl[i]), sizeof(qc)); |
| 1095 | if (copy_to_user(arg, &qc, sizeof(qc))) |
| 1096 | return -EFAULT; |
| 1097 | return 0; |
| 1098 | } |
| 1099 | |
| 1100 | return -EINVAL; |
| 1101 | } |
| 1102 | |
| 1103 | |
| 1104 | static int |
| 1105 | zc0301_vidioc_g_ctrl(struct zc0301_device* cam, void __user * arg) |
| 1106 | { |
Luca Risolia | a847423 | 2006-02-25 06:57:49 +0000 | [diff] [blame] | 1107 | struct zc0301_sensor* s = &cam->sensor; |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1108 | struct v4l2_control ctrl; |
| 1109 | int err = 0; |
| 1110 | u8 i; |
| 1111 | |
| 1112 | if (!s->get_ctrl && !s->set_ctrl) |
| 1113 | return -EINVAL; |
| 1114 | |
| 1115 | if (copy_from_user(&ctrl, arg, sizeof(ctrl))) |
| 1116 | return -EFAULT; |
| 1117 | |
| 1118 | if (!s->get_ctrl) { |
| 1119 | for (i = 0; i < ARRAY_SIZE(s->qctrl); i++) |
| 1120 | if (ctrl.id == s->qctrl[i].id) { |
| 1121 | ctrl.value = s->_qctrl[i].default_value; |
| 1122 | goto exit; |
| 1123 | } |
| 1124 | return -EINVAL; |
| 1125 | } else |
| 1126 | err = s->get_ctrl(cam, &ctrl); |
| 1127 | |
| 1128 | exit: |
| 1129 | if (copy_to_user(arg, &ctrl, sizeof(ctrl))) |
| 1130 | return -EFAULT; |
| 1131 | |
| 1132 | return err; |
| 1133 | } |
| 1134 | |
| 1135 | |
| 1136 | static int |
| 1137 | zc0301_vidioc_s_ctrl(struct zc0301_device* cam, void __user * arg) |
| 1138 | { |
Luca Risolia | a847423 | 2006-02-25 06:57:49 +0000 | [diff] [blame] | 1139 | struct zc0301_sensor* s = &cam->sensor; |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1140 | struct v4l2_control ctrl; |
| 1141 | u8 i; |
| 1142 | int err = 0; |
| 1143 | |
| 1144 | if (!s->set_ctrl) |
| 1145 | return -EINVAL; |
| 1146 | |
| 1147 | if (copy_from_user(&ctrl, arg, sizeof(ctrl))) |
| 1148 | return -EFAULT; |
| 1149 | |
| 1150 | for (i = 0; i < ARRAY_SIZE(s->qctrl); i++) |
| 1151 | if (ctrl.id == s->qctrl[i].id) { |
Luca Risolia | a847423 | 2006-02-25 06:57:49 +0000 | [diff] [blame] | 1152 | if (s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED) |
| 1153 | return -EINVAL; |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1154 | if (ctrl.value < s->qctrl[i].minimum || |
| 1155 | ctrl.value > s->qctrl[i].maximum) |
| 1156 | return -ERANGE; |
| 1157 | ctrl.value -= ctrl.value % s->qctrl[i].step; |
| 1158 | break; |
| 1159 | } |
| 1160 | |
| 1161 | if ((err = s->set_ctrl(cam, &ctrl))) |
| 1162 | return err; |
| 1163 | |
| 1164 | s->_qctrl[i].default_value = ctrl.value; |
| 1165 | |
| 1166 | return 0; |
| 1167 | } |
| 1168 | |
| 1169 | |
| 1170 | static int |
| 1171 | zc0301_vidioc_cropcap(struct zc0301_device* cam, void __user * arg) |
| 1172 | { |
Luca Risolia | a847423 | 2006-02-25 06:57:49 +0000 | [diff] [blame] | 1173 | struct v4l2_cropcap* cc = &(cam->sensor.cropcap); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1174 | |
| 1175 | cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 1176 | cc->pixelaspect.numerator = 1; |
| 1177 | cc->pixelaspect.denominator = 1; |
| 1178 | |
| 1179 | if (copy_to_user(arg, cc, sizeof(*cc))) |
| 1180 | return -EFAULT; |
| 1181 | |
| 1182 | return 0; |
| 1183 | } |
| 1184 | |
| 1185 | |
| 1186 | static int |
| 1187 | zc0301_vidioc_g_crop(struct zc0301_device* cam, void __user * arg) |
| 1188 | { |
Luca Risolia | a847423 | 2006-02-25 06:57:49 +0000 | [diff] [blame] | 1189 | struct zc0301_sensor* s = &cam->sensor; |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1190 | struct v4l2_crop crop = { |
| 1191 | .type = V4L2_BUF_TYPE_VIDEO_CAPTURE, |
| 1192 | }; |
| 1193 | |
| 1194 | memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect)); |
| 1195 | |
| 1196 | if (copy_to_user(arg, &crop, sizeof(crop))) |
| 1197 | return -EFAULT; |
| 1198 | |
| 1199 | return 0; |
| 1200 | } |
| 1201 | |
| 1202 | |
| 1203 | static int |
| 1204 | zc0301_vidioc_s_crop(struct zc0301_device* cam, void __user * arg) |
| 1205 | { |
Luca Risolia | a847423 | 2006-02-25 06:57:49 +0000 | [diff] [blame] | 1206 | struct zc0301_sensor* s = &cam->sensor; |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1207 | struct v4l2_crop crop; |
| 1208 | struct v4l2_rect* rect; |
| 1209 | struct v4l2_rect* bounds = &(s->cropcap.bounds); |
| 1210 | const enum zc0301_stream_state stream = cam->stream; |
| 1211 | const u32 nbuffers = cam->nbuffers; |
| 1212 | u32 i; |
| 1213 | int err = 0; |
| 1214 | |
| 1215 | if (copy_from_user(&crop, arg, sizeof(crop))) |
| 1216 | return -EFAULT; |
| 1217 | |
| 1218 | rect = &(crop.c); |
| 1219 | |
| 1220 | if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1221 | return -EINVAL; |
| 1222 | |
| 1223 | if (cam->module_param.force_munmap) |
| 1224 | for (i = 0; i < cam->nbuffers; i++) |
| 1225 | if (cam->frame[i].vma_use_count) { |
| 1226 | DBG(3, "VIDIOC_S_CROP failed. " |
| 1227 | "Unmap the buffers first."); |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 1228 | return -EBUSY; |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | if (!s->set_crop) { |
| 1232 | memcpy(rect, &(s->_rect), sizeof(*rect)); |
| 1233 | if (copy_to_user(arg, &crop, sizeof(crop))) |
| 1234 | return -EFAULT; |
| 1235 | return 0; |
| 1236 | } |
| 1237 | |
| 1238 | rect->left &= ~7L; |
| 1239 | rect->top &= ~7L; |
| 1240 | if (rect->width < 8) |
| 1241 | rect->width = 8; |
| 1242 | if (rect->height < 8) |
| 1243 | rect->height = 8; |
| 1244 | if (rect->width > bounds->width) |
| 1245 | rect->width = bounds->width; |
| 1246 | if (rect->height > bounds->height) |
| 1247 | rect->height = bounds->height; |
| 1248 | if (rect->left < bounds->left) |
| 1249 | rect->left = bounds->left; |
| 1250 | if (rect->top < bounds->top) |
| 1251 | rect->top = bounds->top; |
| 1252 | if (rect->left + rect->width > bounds->left + bounds->width) |
| 1253 | rect->left = bounds->left+bounds->width - rect->width; |
| 1254 | if (rect->top + rect->height > bounds->top + bounds->height) |
| 1255 | rect->top = bounds->top+bounds->height - rect->height; |
| 1256 | rect->width &= ~7L; |
| 1257 | rect->height &= ~7L; |
| 1258 | |
| 1259 | if (cam->stream == STREAM_ON) |
| 1260 | if ((err = zc0301_stream_interrupt(cam))) |
| 1261 | return err; |
| 1262 | |
| 1263 | if (copy_to_user(arg, &crop, sizeof(crop))) { |
| 1264 | cam->stream = stream; |
| 1265 | return -EFAULT; |
| 1266 | } |
| 1267 | |
| 1268 | if (cam->module_param.force_munmap || cam->io == IO_READ) |
| 1269 | zc0301_release_buffers(cam); |
| 1270 | |
| 1271 | if (s->set_crop) |
| 1272 | err += s->set_crop(cam, rect); |
| 1273 | |
| 1274 | if (err) { /* atomic, no rollback in ioctl() */ |
| 1275 | cam->state |= DEV_MISCONFIGURED; |
| 1276 | DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To " |
| 1277 | "use the camera, close and open /dev/video%d again.", |
| 1278 | cam->v4ldev->minor); |
| 1279 | return -EIO; |
| 1280 | } |
| 1281 | |
| 1282 | s->pix_format.width = rect->width; |
| 1283 | s->pix_format.height = rect->height; |
| 1284 | memcpy(&(s->_rect), rect, sizeof(*rect)); |
| 1285 | |
| 1286 | if ((cam->module_param.force_munmap || cam->io == IO_READ) && |
| 1287 | nbuffers != zc0301_request_buffers(cam, nbuffers, cam->io)) { |
| 1288 | cam->state |= DEV_MISCONFIGURED; |
| 1289 | DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To " |
| 1290 | "use the camera, close and open /dev/video%d again.", |
| 1291 | cam->v4ldev->minor); |
| 1292 | return -ENOMEM; |
| 1293 | } |
| 1294 | |
| 1295 | if (cam->io == IO_READ) |
| 1296 | zc0301_empty_framequeues(cam); |
| 1297 | else if (cam->module_param.force_munmap) |
| 1298 | zc0301_requeue_outqueue(cam); |
| 1299 | |
| 1300 | cam->stream = stream; |
| 1301 | |
| 1302 | return 0; |
| 1303 | } |
| 1304 | |
| 1305 | |
| 1306 | static int |
Luca Risolia | 7e3a066 | 2007-01-08 11:34:35 -0300 | [diff] [blame] | 1307 | zc0301_vidioc_enum_framesizes(struct zc0301_device* cam, void __user * arg) |
| 1308 | { |
| 1309 | struct v4l2_frmsizeenum frmsize; |
| 1310 | |
| 1311 | if (copy_from_user(&frmsize, arg, sizeof(frmsize))) |
| 1312 | return -EFAULT; |
| 1313 | |
| 1314 | if (frmsize.index != 0 && frmsize.index != 1) |
| 1315 | return -EINVAL; |
| 1316 | |
| 1317 | if (frmsize.pixel_format != V4L2_PIX_FMT_JPEG) |
| 1318 | return -EINVAL; |
| 1319 | |
| 1320 | frmsize.type = V4L2_FRMSIZE_TYPE_DISCRETE; |
| 1321 | |
| 1322 | if (frmsize.index == 1) { |
| 1323 | frmsize.discrete.width = cam->sensor.cropcap.defrect.width; |
| 1324 | frmsize.discrete.height = cam->sensor.cropcap.defrect.height; |
| 1325 | } |
| 1326 | memset(&frmsize.reserved, 0, sizeof(frmsize.reserved)); |
| 1327 | |
| 1328 | if (copy_to_user(arg, &frmsize, sizeof(frmsize))) |
| 1329 | return -EFAULT; |
| 1330 | |
| 1331 | return 0; |
| 1332 | } |
| 1333 | |
| 1334 | |
| 1335 | static int |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1336 | zc0301_vidioc_enum_fmt(struct zc0301_device* cam, void __user * arg) |
| 1337 | { |
| 1338 | struct v4l2_fmtdesc fmtd; |
| 1339 | |
| 1340 | if (copy_from_user(&fmtd, arg, sizeof(fmtd))) |
| 1341 | return -EFAULT; |
| 1342 | |
Luca Risolia | 7e3a066 | 2007-01-08 11:34:35 -0300 | [diff] [blame] | 1343 | if (fmtd.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1344 | return -EINVAL; |
| 1345 | |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1346 | if (fmtd.index == 0) { |
| 1347 | strcpy(fmtd.description, "JPEG"); |
| 1348 | fmtd.pixelformat = V4L2_PIX_FMT_JPEG; |
| 1349 | fmtd.flags = V4L2_FMT_FLAG_COMPRESSED; |
| 1350 | } else |
| 1351 | return -EINVAL; |
| 1352 | |
| 1353 | fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 1354 | memset(&fmtd.reserved, 0, sizeof(fmtd.reserved)); |
| 1355 | |
| 1356 | if (copy_to_user(arg, &fmtd, sizeof(fmtd))) |
| 1357 | return -EFAULT; |
| 1358 | |
| 1359 | return 0; |
| 1360 | } |
| 1361 | |
| 1362 | |
| 1363 | static int |
| 1364 | zc0301_vidioc_g_fmt(struct zc0301_device* cam, void __user * arg) |
| 1365 | { |
| 1366 | struct v4l2_format format; |
Luca Risolia | a847423 | 2006-02-25 06:57:49 +0000 | [diff] [blame] | 1367 | struct v4l2_pix_format* pfmt = &(cam->sensor.pix_format); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1368 | |
| 1369 | if (copy_from_user(&format, arg, sizeof(format))) |
| 1370 | return -EFAULT; |
| 1371 | |
| 1372 | if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1373 | return -EINVAL; |
| 1374 | |
| 1375 | pfmt->bytesperline = 0; |
| 1376 | pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8); |
| 1377 | pfmt->field = V4L2_FIELD_NONE; |
| 1378 | memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt)); |
| 1379 | |
| 1380 | if (copy_to_user(arg, &format, sizeof(format))) |
| 1381 | return -EFAULT; |
| 1382 | |
| 1383 | return 0; |
| 1384 | } |
| 1385 | |
| 1386 | |
| 1387 | static int |
| 1388 | zc0301_vidioc_try_s_fmt(struct zc0301_device* cam, unsigned int cmd, |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 1389 | void __user * arg) |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1390 | { |
Luca Risolia | a847423 | 2006-02-25 06:57:49 +0000 | [diff] [blame] | 1391 | struct zc0301_sensor* s = &cam->sensor; |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1392 | struct v4l2_format format; |
| 1393 | struct v4l2_pix_format* pix; |
| 1394 | struct v4l2_pix_format* pfmt = &(s->pix_format); |
| 1395 | struct v4l2_rect* bounds = &(s->cropcap.bounds); |
| 1396 | struct v4l2_rect rect; |
| 1397 | const enum zc0301_stream_state stream = cam->stream; |
| 1398 | const u32 nbuffers = cam->nbuffers; |
| 1399 | u32 i; |
| 1400 | int err = 0; |
| 1401 | |
| 1402 | if (copy_from_user(&format, arg, sizeof(format))) |
| 1403 | return -EFAULT; |
| 1404 | |
| 1405 | pix = &(format.fmt.pix); |
| 1406 | |
| 1407 | if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1408 | return -EINVAL; |
| 1409 | |
| 1410 | memcpy(&rect, &(s->_rect), sizeof(rect)); |
| 1411 | |
| 1412 | if (!s->set_crop) { |
| 1413 | pix->width = rect.width; |
| 1414 | pix->height = rect.height; |
| 1415 | } else { |
| 1416 | rect.width = pix->width; |
| 1417 | rect.height = pix->height; |
| 1418 | } |
| 1419 | |
| 1420 | if (rect.width < 8) |
| 1421 | rect.width = 8; |
| 1422 | if (rect.height < 8) |
| 1423 | rect.height = 8; |
| 1424 | if (rect.width > bounds->left + bounds->width - rect.left) |
| 1425 | rect.width = bounds->left + bounds->width - rect.left; |
| 1426 | if (rect.height > bounds->top + bounds->height - rect.top) |
| 1427 | rect.height = bounds->top + bounds->height - rect.top; |
| 1428 | rect.width &= ~7L; |
| 1429 | rect.height &= ~7L; |
| 1430 | |
| 1431 | pix->width = rect.width; |
| 1432 | pix->height = rect.height; |
| 1433 | pix->pixelformat = pfmt->pixelformat; |
| 1434 | pix->priv = pfmt->priv; |
| 1435 | pix->colorspace = pfmt->colorspace; |
| 1436 | pix->bytesperline = 0; |
| 1437 | pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8); |
| 1438 | pix->field = V4L2_FIELD_NONE; |
| 1439 | |
| 1440 | if (cmd == VIDIOC_TRY_FMT) { |
| 1441 | if (copy_to_user(arg, &format, sizeof(format))) |
| 1442 | return -EFAULT; |
| 1443 | return 0; |
| 1444 | } |
| 1445 | |
| 1446 | if (cam->module_param.force_munmap) |
| 1447 | for (i = 0; i < cam->nbuffers; i++) |
| 1448 | if (cam->frame[i].vma_use_count) { |
| 1449 | DBG(3, "VIDIOC_S_FMT failed. " |
| 1450 | "Unmap the buffers first."); |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 1451 | return -EBUSY; |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | if (cam->stream == STREAM_ON) |
| 1455 | if ((err = zc0301_stream_interrupt(cam))) |
| 1456 | return err; |
| 1457 | |
| 1458 | if (copy_to_user(arg, &format, sizeof(format))) { |
| 1459 | cam->stream = stream; |
| 1460 | return -EFAULT; |
| 1461 | } |
| 1462 | |
| 1463 | if (cam->module_param.force_munmap || cam->io == IO_READ) |
| 1464 | zc0301_release_buffers(cam); |
| 1465 | |
| 1466 | if (s->set_crop) |
| 1467 | err += s->set_crop(cam, &rect); |
| 1468 | |
| 1469 | if (err) { /* atomic, no rollback in ioctl() */ |
| 1470 | cam->state |= DEV_MISCONFIGURED; |
| 1471 | DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To " |
| 1472 | "use the camera, close and open /dev/video%d again.", |
| 1473 | cam->v4ldev->minor); |
| 1474 | return -EIO; |
| 1475 | } |
| 1476 | |
| 1477 | memcpy(pfmt, pix, sizeof(*pix)); |
| 1478 | memcpy(&(s->_rect), &rect, sizeof(rect)); |
| 1479 | |
| 1480 | if ((cam->module_param.force_munmap || cam->io == IO_READ) && |
| 1481 | nbuffers != zc0301_request_buffers(cam, nbuffers, cam->io)) { |
| 1482 | cam->state |= DEV_MISCONFIGURED; |
| 1483 | DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To " |
| 1484 | "use the camera, close and open /dev/video%d again.", |
| 1485 | cam->v4ldev->minor); |
| 1486 | return -ENOMEM; |
| 1487 | } |
| 1488 | |
| 1489 | if (cam->io == IO_READ) |
| 1490 | zc0301_empty_framequeues(cam); |
| 1491 | else if (cam->module_param.force_munmap) |
| 1492 | zc0301_requeue_outqueue(cam); |
| 1493 | |
| 1494 | cam->stream = stream; |
| 1495 | |
| 1496 | return 0; |
| 1497 | } |
| 1498 | |
| 1499 | |
| 1500 | static int |
| 1501 | zc0301_vidioc_g_jpegcomp(struct zc0301_device* cam, void __user * arg) |
| 1502 | { |
| 1503 | if (copy_to_user(arg, &cam->compression, sizeof(cam->compression))) |
| 1504 | return -EFAULT; |
| 1505 | |
| 1506 | return 0; |
| 1507 | } |
| 1508 | |
| 1509 | |
| 1510 | static int |
| 1511 | zc0301_vidioc_s_jpegcomp(struct zc0301_device* cam, void __user * arg) |
| 1512 | { |
| 1513 | struct v4l2_jpegcompression jc; |
| 1514 | const enum zc0301_stream_state stream = cam->stream; |
| 1515 | int err = 0; |
| 1516 | |
| 1517 | if (copy_from_user(&jc, arg, sizeof(jc))) |
| 1518 | return -EFAULT; |
| 1519 | |
Luca Risolia | 9e47a52 | 2006-02-08 00:05:27 +0000 | [diff] [blame] | 1520 | if (jc.quality != 0) |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1521 | return -EINVAL; |
| 1522 | |
| 1523 | if (cam->stream == STREAM_ON) |
| 1524 | if ((err = zc0301_stream_interrupt(cam))) |
| 1525 | return err; |
| 1526 | |
| 1527 | err += zc0301_set_compression(cam, &jc); |
| 1528 | if (err) { /* atomic, no rollback in ioctl() */ |
| 1529 | cam->state |= DEV_MISCONFIGURED; |
| 1530 | DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware " |
| 1531 | "problems. To use the camera, close and open " |
| 1532 | "/dev/video%d again.", cam->v4ldev->minor); |
| 1533 | return -EIO; |
| 1534 | } |
| 1535 | |
| 1536 | cam->compression.quality = jc.quality; |
| 1537 | |
| 1538 | cam->stream = stream; |
| 1539 | |
| 1540 | return 0; |
| 1541 | } |
| 1542 | |
| 1543 | |
| 1544 | static int |
| 1545 | zc0301_vidioc_reqbufs(struct zc0301_device* cam, void __user * arg) |
| 1546 | { |
| 1547 | struct v4l2_requestbuffers rb; |
| 1548 | u32 i; |
| 1549 | int err; |
| 1550 | |
| 1551 | if (copy_from_user(&rb, arg, sizeof(rb))) |
| 1552 | return -EFAULT; |
| 1553 | |
| 1554 | if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || |
| 1555 | rb.memory != V4L2_MEMORY_MMAP) |
| 1556 | return -EINVAL; |
| 1557 | |
| 1558 | if (cam->io == IO_READ) { |
| 1559 | DBG(3, "Close and open the device again to choose the mmap " |
| 1560 | "I/O method"); |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 1561 | return -EBUSY; |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | for (i = 0; i < cam->nbuffers; i++) |
| 1565 | if (cam->frame[i].vma_use_count) { |
| 1566 | DBG(3, "VIDIOC_REQBUFS failed. " |
| 1567 | "Previous buffers are still mapped."); |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 1568 | return -EBUSY; |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
| 1571 | if (cam->stream == STREAM_ON) |
| 1572 | if ((err = zc0301_stream_interrupt(cam))) |
| 1573 | return err; |
| 1574 | |
| 1575 | zc0301_empty_framequeues(cam); |
| 1576 | |
| 1577 | zc0301_release_buffers(cam); |
| 1578 | if (rb.count) |
| 1579 | rb.count = zc0301_request_buffers(cam, rb.count, IO_MMAP); |
| 1580 | |
| 1581 | if (copy_to_user(arg, &rb, sizeof(rb))) { |
| 1582 | zc0301_release_buffers(cam); |
| 1583 | cam->io = IO_NONE; |
| 1584 | return -EFAULT; |
| 1585 | } |
| 1586 | |
| 1587 | cam->io = rb.count ? IO_MMAP : IO_NONE; |
| 1588 | |
| 1589 | return 0; |
| 1590 | } |
| 1591 | |
| 1592 | |
| 1593 | static int |
| 1594 | zc0301_vidioc_querybuf(struct zc0301_device* cam, void __user * arg) |
| 1595 | { |
| 1596 | struct v4l2_buffer b; |
| 1597 | |
| 1598 | if (copy_from_user(&b, arg, sizeof(b))) |
| 1599 | return -EFAULT; |
| 1600 | |
| 1601 | if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || |
| 1602 | b.index >= cam->nbuffers || cam->io != IO_MMAP) |
| 1603 | return -EINVAL; |
| 1604 | |
| 1605 | memcpy(&b, &cam->frame[b.index].buf, sizeof(b)); |
| 1606 | |
| 1607 | if (cam->frame[b.index].vma_use_count) |
| 1608 | b.flags |= V4L2_BUF_FLAG_MAPPED; |
| 1609 | |
| 1610 | if (cam->frame[b.index].state == F_DONE) |
| 1611 | b.flags |= V4L2_BUF_FLAG_DONE; |
| 1612 | else if (cam->frame[b.index].state != F_UNUSED) |
| 1613 | b.flags |= V4L2_BUF_FLAG_QUEUED; |
| 1614 | |
| 1615 | if (copy_to_user(arg, &b, sizeof(b))) |
| 1616 | return -EFAULT; |
| 1617 | |
| 1618 | return 0; |
| 1619 | } |
| 1620 | |
| 1621 | |
| 1622 | static int |
| 1623 | zc0301_vidioc_qbuf(struct zc0301_device* cam, void __user * arg) |
| 1624 | { |
| 1625 | struct v4l2_buffer b; |
| 1626 | unsigned long lock_flags; |
| 1627 | |
| 1628 | if (copy_from_user(&b, arg, sizeof(b))) |
| 1629 | return -EFAULT; |
| 1630 | |
| 1631 | if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || |
| 1632 | b.index >= cam->nbuffers || cam->io != IO_MMAP) |
| 1633 | return -EINVAL; |
| 1634 | |
| 1635 | if (cam->frame[b.index].state != F_UNUSED) |
| 1636 | return -EINVAL; |
| 1637 | |
| 1638 | cam->frame[b.index].state = F_QUEUED; |
| 1639 | |
| 1640 | spin_lock_irqsave(&cam->queue_lock, lock_flags); |
| 1641 | list_add_tail(&cam->frame[b.index].frame, &cam->inqueue); |
| 1642 | spin_unlock_irqrestore(&cam->queue_lock, lock_flags); |
| 1643 | |
| 1644 | PDBGG("Frame #%lu queued", (unsigned long)b.index); |
| 1645 | |
| 1646 | return 0; |
| 1647 | } |
| 1648 | |
| 1649 | |
| 1650 | static int |
| 1651 | zc0301_vidioc_dqbuf(struct zc0301_device* cam, struct file* filp, |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 1652 | void __user * arg) |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1653 | { |
| 1654 | struct v4l2_buffer b; |
| 1655 | struct zc0301_frame_t *f; |
| 1656 | unsigned long lock_flags; |
| 1657 | long timeout; |
| 1658 | |
| 1659 | if (copy_from_user(&b, arg, sizeof(b))) |
| 1660 | return -EFAULT; |
| 1661 | |
| 1662 | if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io!= IO_MMAP) |
| 1663 | return -EINVAL; |
| 1664 | |
| 1665 | if (list_empty(&cam->outqueue)) { |
| 1666 | if (cam->stream == STREAM_OFF) |
| 1667 | return -EINVAL; |
| 1668 | if (filp->f_flags & O_NONBLOCK) |
| 1669 | return -EAGAIN; |
| 1670 | timeout = wait_event_interruptible_timeout |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 1671 | ( cam->wait_frame, |
| 1672 | (!list_empty(&cam->outqueue)) || |
| 1673 | (cam->state & DEV_DISCONNECTED) || |
| 1674 | (cam->state & DEV_MISCONFIGURED), |
| 1675 | cam->module_param.frame_timeout * |
| 1676 | 1000 * msecs_to_jiffies(1) ); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1677 | if (timeout < 0) |
| 1678 | return timeout; |
| 1679 | if (cam->state & DEV_DISCONNECTED) |
| 1680 | return -ENODEV; |
| 1681 | if (!timeout || (cam->state & DEV_MISCONFIGURED)) |
| 1682 | return -EIO; |
| 1683 | } |
| 1684 | |
| 1685 | spin_lock_irqsave(&cam->queue_lock, lock_flags); |
| 1686 | f = list_entry(cam->outqueue.next, struct zc0301_frame_t, frame); |
| 1687 | list_del(cam->outqueue.next); |
| 1688 | spin_unlock_irqrestore(&cam->queue_lock, lock_flags); |
| 1689 | |
| 1690 | f->state = F_UNUSED; |
| 1691 | |
| 1692 | memcpy(&b, &f->buf, sizeof(b)); |
| 1693 | if (f->vma_use_count) |
| 1694 | b.flags |= V4L2_BUF_FLAG_MAPPED; |
| 1695 | |
| 1696 | if (copy_to_user(arg, &b, sizeof(b))) |
| 1697 | return -EFAULT; |
| 1698 | |
| 1699 | PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index); |
| 1700 | |
| 1701 | return 0; |
| 1702 | } |
| 1703 | |
| 1704 | |
| 1705 | static int |
| 1706 | zc0301_vidioc_streamon(struct zc0301_device* cam, void __user * arg) |
| 1707 | { |
| 1708 | int type; |
| 1709 | |
| 1710 | if (copy_from_user(&type, arg, sizeof(type))) |
| 1711 | return -EFAULT; |
| 1712 | |
| 1713 | if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP) |
| 1714 | return -EINVAL; |
| 1715 | |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1716 | cam->stream = STREAM_ON; |
| 1717 | |
| 1718 | DBG(3, "Stream on"); |
| 1719 | |
| 1720 | return 0; |
| 1721 | } |
| 1722 | |
| 1723 | |
| 1724 | static int |
| 1725 | zc0301_vidioc_streamoff(struct zc0301_device* cam, void __user * arg) |
| 1726 | { |
| 1727 | int type, err; |
| 1728 | |
| 1729 | if (copy_from_user(&type, arg, sizeof(type))) |
| 1730 | return -EFAULT; |
| 1731 | |
| 1732 | if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP) |
| 1733 | return -EINVAL; |
| 1734 | |
| 1735 | if (cam->stream == STREAM_ON) |
| 1736 | if ((err = zc0301_stream_interrupt(cam))) |
| 1737 | return err; |
| 1738 | |
| 1739 | zc0301_empty_framequeues(cam); |
| 1740 | |
| 1741 | DBG(3, "Stream off"); |
| 1742 | |
| 1743 | return 0; |
| 1744 | } |
| 1745 | |
| 1746 | |
| 1747 | static int |
| 1748 | zc0301_vidioc_g_parm(struct zc0301_device* cam, void __user * arg) |
| 1749 | { |
| 1750 | struct v4l2_streamparm sp; |
| 1751 | |
| 1752 | if (copy_from_user(&sp, arg, sizeof(sp))) |
| 1753 | return -EFAULT; |
| 1754 | |
| 1755 | if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1756 | return -EINVAL; |
| 1757 | |
| 1758 | sp.parm.capture.extendedmode = 0; |
| 1759 | sp.parm.capture.readbuffers = cam->nreadbuffers; |
| 1760 | |
| 1761 | if (copy_to_user(arg, &sp, sizeof(sp))) |
| 1762 | return -EFAULT; |
| 1763 | |
| 1764 | return 0; |
| 1765 | } |
| 1766 | |
| 1767 | |
| 1768 | static int |
| 1769 | zc0301_vidioc_s_parm(struct zc0301_device* cam, void __user * arg) |
| 1770 | { |
| 1771 | struct v4l2_streamparm sp; |
| 1772 | |
| 1773 | if (copy_from_user(&sp, arg, sizeof(sp))) |
| 1774 | return -EFAULT; |
| 1775 | |
| 1776 | if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1777 | return -EINVAL; |
| 1778 | |
| 1779 | sp.parm.capture.extendedmode = 0; |
| 1780 | |
| 1781 | if (sp.parm.capture.readbuffers == 0) |
| 1782 | sp.parm.capture.readbuffers = cam->nreadbuffers; |
| 1783 | |
| 1784 | if (sp.parm.capture.readbuffers > ZC0301_MAX_FRAMES) |
| 1785 | sp.parm.capture.readbuffers = ZC0301_MAX_FRAMES; |
| 1786 | |
| 1787 | if (copy_to_user(arg, &sp, sizeof(sp))) |
| 1788 | return -EFAULT; |
| 1789 | |
| 1790 | cam->nreadbuffers = sp.parm.capture.readbuffers; |
| 1791 | |
| 1792 | return 0; |
| 1793 | } |
| 1794 | |
| 1795 | |
| 1796 | static int zc0301_ioctl_v4l2(struct inode* inode, struct file* filp, |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 1797 | unsigned int cmd, void __user * arg) |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1798 | { |
| 1799 | struct zc0301_device* cam = video_get_drvdata(video_devdata(filp)); |
| 1800 | |
| 1801 | switch (cmd) { |
| 1802 | |
| 1803 | case VIDIOC_QUERYCAP: |
| 1804 | return zc0301_vidioc_querycap(cam, arg); |
| 1805 | |
| 1806 | case VIDIOC_ENUMINPUT: |
| 1807 | return zc0301_vidioc_enuminput(cam, arg); |
| 1808 | |
| 1809 | case VIDIOC_G_INPUT: |
Luca Risolia | 9e47a52 | 2006-02-08 00:05:27 +0000 | [diff] [blame] | 1810 | return zc0301_vidioc_g_input(cam, arg); |
| 1811 | |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1812 | case VIDIOC_S_INPUT: |
Luca Risolia | 9e47a52 | 2006-02-08 00:05:27 +0000 | [diff] [blame] | 1813 | return zc0301_vidioc_s_input(cam, arg); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1814 | |
| 1815 | case VIDIOC_QUERYCTRL: |
| 1816 | return zc0301_vidioc_query_ctrl(cam, arg); |
| 1817 | |
| 1818 | case VIDIOC_G_CTRL: |
| 1819 | return zc0301_vidioc_g_ctrl(cam, arg); |
| 1820 | |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1821 | case VIDIOC_S_CTRL: |
| 1822 | return zc0301_vidioc_s_ctrl(cam, arg); |
| 1823 | |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1824 | case VIDIOC_CROPCAP: |
| 1825 | return zc0301_vidioc_cropcap(cam, arg); |
| 1826 | |
| 1827 | case VIDIOC_G_CROP: |
| 1828 | return zc0301_vidioc_g_crop(cam, arg); |
| 1829 | |
| 1830 | case VIDIOC_S_CROP: |
| 1831 | return zc0301_vidioc_s_crop(cam, arg); |
| 1832 | |
| 1833 | case VIDIOC_ENUM_FMT: |
| 1834 | return zc0301_vidioc_enum_fmt(cam, arg); |
| 1835 | |
| 1836 | case VIDIOC_G_FMT: |
| 1837 | return zc0301_vidioc_g_fmt(cam, arg); |
| 1838 | |
| 1839 | case VIDIOC_TRY_FMT: |
| 1840 | case VIDIOC_S_FMT: |
| 1841 | return zc0301_vidioc_try_s_fmt(cam, cmd, arg); |
| 1842 | |
Luca Risolia | 7e3a066 | 2007-01-08 11:34:35 -0300 | [diff] [blame] | 1843 | case VIDIOC_ENUM_FRAMESIZES: |
| 1844 | return zc0301_vidioc_enum_framesizes(cam, arg); |
| 1845 | |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1846 | case VIDIOC_G_JPEGCOMP: |
| 1847 | return zc0301_vidioc_g_jpegcomp(cam, arg); |
| 1848 | |
| 1849 | case VIDIOC_S_JPEGCOMP: |
| 1850 | return zc0301_vidioc_s_jpegcomp(cam, arg); |
| 1851 | |
| 1852 | case VIDIOC_REQBUFS: |
| 1853 | return zc0301_vidioc_reqbufs(cam, arg); |
| 1854 | |
| 1855 | case VIDIOC_QUERYBUF: |
| 1856 | return zc0301_vidioc_querybuf(cam, arg); |
| 1857 | |
| 1858 | case VIDIOC_QBUF: |
| 1859 | return zc0301_vidioc_qbuf(cam, arg); |
| 1860 | |
| 1861 | case VIDIOC_DQBUF: |
| 1862 | return zc0301_vidioc_dqbuf(cam, filp, arg); |
| 1863 | |
| 1864 | case VIDIOC_STREAMON: |
| 1865 | return zc0301_vidioc_streamon(cam, arg); |
| 1866 | |
| 1867 | case VIDIOC_STREAMOFF: |
| 1868 | return zc0301_vidioc_streamoff(cam, arg); |
| 1869 | |
| 1870 | case VIDIOC_G_PARM: |
| 1871 | return zc0301_vidioc_g_parm(cam, arg); |
| 1872 | |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1873 | case VIDIOC_S_PARM: |
| 1874 | return zc0301_vidioc_s_parm(cam, arg); |
| 1875 | |
| 1876 | case VIDIOC_G_STD: |
| 1877 | case VIDIOC_S_STD: |
| 1878 | case VIDIOC_QUERYSTD: |
| 1879 | case VIDIOC_ENUMSTD: |
| 1880 | case VIDIOC_QUERYMENU: |
Luca Risolia | 7e3a066 | 2007-01-08 11:34:35 -0300 | [diff] [blame] | 1881 | case VIDIOC_ENUM_FRAMEINTERVALS: |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1882 | return -EINVAL; |
| 1883 | |
| 1884 | default: |
| 1885 | return -EINVAL; |
| 1886 | |
| 1887 | } |
| 1888 | } |
| 1889 | |
| 1890 | |
| 1891 | static int zc0301_ioctl(struct inode* inode, struct file* filp, |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 1892 | unsigned int cmd, unsigned long arg) |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1893 | { |
| 1894 | struct zc0301_device* cam = video_get_drvdata(video_devdata(filp)); |
| 1895 | int err = 0; |
| 1896 | |
| 1897 | if (mutex_lock_interruptible(&cam->fileop_mutex)) |
| 1898 | return -ERESTARTSYS; |
| 1899 | |
| 1900 | if (cam->state & DEV_DISCONNECTED) { |
| 1901 | DBG(1, "Device not present"); |
| 1902 | mutex_unlock(&cam->fileop_mutex); |
| 1903 | return -ENODEV; |
| 1904 | } |
| 1905 | |
| 1906 | if (cam->state & DEV_MISCONFIGURED) { |
| 1907 | DBG(1, "The camera is misconfigured. Close and open it " |
| 1908 | "again."); |
| 1909 | mutex_unlock(&cam->fileop_mutex); |
| 1910 | return -EIO; |
| 1911 | } |
| 1912 | |
| 1913 | V4LDBG(3, "zc0301", cmd); |
| 1914 | |
| 1915 | err = zc0301_ioctl_v4l2(inode, filp, cmd, (void __user *)arg); |
| 1916 | |
| 1917 | mutex_unlock(&cam->fileop_mutex); |
| 1918 | |
| 1919 | return err; |
| 1920 | } |
| 1921 | |
| 1922 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 1923 | static const struct file_operations zc0301_fops = { |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1924 | .owner = THIS_MODULE, |
| 1925 | .open = zc0301_open, |
| 1926 | .release = zc0301_release, |
| 1927 | .ioctl = zc0301_ioctl, |
Douglas Schilling Landgraf | 078ff79 | 2008-04-22 14:46:11 -0300 | [diff] [blame] | 1928 | #ifdef CONFIG_COMPAT |
Luca Risolia | 7e3a066 | 2007-01-08 11:34:35 -0300 | [diff] [blame] | 1929 | .compat_ioctl = v4l_compat_ioctl32, |
Douglas Schilling Landgraf | 078ff79 | 2008-04-22 14:46:11 -0300 | [diff] [blame] | 1930 | #endif |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1931 | .read = zc0301_read, |
| 1932 | .poll = zc0301_poll, |
| 1933 | .mmap = zc0301_mmap, |
| 1934 | .llseek = no_llseek, |
| 1935 | }; |
| 1936 | |
| 1937 | /*****************************************************************************/ |
| 1938 | |
| 1939 | static int |
| 1940 | zc0301_usb_probe(struct usb_interface* intf, const struct usb_device_id* id) |
| 1941 | { |
| 1942 | struct usb_device *udev = interface_to_usbdev(intf); |
| 1943 | struct zc0301_device* cam; |
Douglas Schilling Landgraf | ff699e6 | 2008-04-22 14:41:48 -0300 | [diff] [blame] | 1944 | static unsigned int dev_nr; |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1945 | unsigned int i; |
| 1946 | int err = 0; |
| 1947 | |
| 1948 | if (!(cam = kzalloc(sizeof(struct zc0301_device), GFP_KERNEL))) |
| 1949 | return -ENOMEM; |
| 1950 | |
| 1951 | cam->usbdev = udev; |
| 1952 | |
| 1953 | if (!(cam->control_buffer = kzalloc(4, GFP_KERNEL))) { |
| 1954 | DBG(1, "kmalloc() failed"); |
| 1955 | err = -ENOMEM; |
| 1956 | goto fail; |
| 1957 | } |
| 1958 | |
| 1959 | if (!(cam->v4ldev = video_device_alloc())) { |
| 1960 | DBG(1, "video_device_alloc() failed"); |
| 1961 | err = -ENOMEM; |
| 1962 | goto fail; |
| 1963 | } |
| 1964 | |
Luca Risolia | a87cde0 | 2006-04-26 10:19:49 -0300 | [diff] [blame] | 1965 | DBG(2, "ZC0301[P] Image Processor and Control Chip detected " |
Luca Risolia | 7e3a066 | 2007-01-08 11:34:35 -0300 | [diff] [blame] | 1966 | "(vid/pid 0x%04X:0x%04X)",id->idVendor, id->idProduct); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1967 | |
| 1968 | for (i = 0; zc0301_sensor_table[i]; i++) { |
| 1969 | err = zc0301_sensor_table[i](cam); |
| 1970 | if (!err) |
| 1971 | break; |
| 1972 | } |
| 1973 | |
Luca Risolia | a847423 | 2006-02-25 06:57:49 +0000 | [diff] [blame] | 1974 | if (!err) |
| 1975 | DBG(2, "%s image sensor detected", cam->sensor.name); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1976 | else { |
| 1977 | DBG(1, "No supported image sensor detected"); |
| 1978 | err = -ENODEV; |
| 1979 | goto fail; |
| 1980 | } |
| 1981 | |
| 1982 | if (zc0301_init(cam)) { |
| 1983 | DBG(1, "Initialization failed. I will retry on open()."); |
| 1984 | cam->state |= DEV_MISCONFIGURED; |
| 1985 | } |
| 1986 | |
Luca Risolia | a87cde0 | 2006-04-26 10:19:49 -0300 | [diff] [blame] | 1987 | strcpy(cam->v4ldev->name, "ZC0301[P] PC Camera"); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1988 | cam->v4ldev->owner = THIS_MODULE; |
| 1989 | cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES; |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1990 | cam->v4ldev->fops = &zc0301_fops; |
| 1991 | cam->v4ldev->minor = video_nr[dev_nr]; |
| 1992 | cam->v4ldev->release = video_device_release; |
| 1993 | video_set_drvdata(cam->v4ldev, cam); |
| 1994 | |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 1995 | init_completion(&cam->probe); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1996 | |
| 1997 | err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER, |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 1998 | video_nr[dev_nr]); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 1999 | if (err) { |
| 2000 | DBG(1, "V4L2 device registration failed"); |
| 2001 | if (err == -ENFILE && video_nr[dev_nr] == -1) |
| 2002 | DBG(1, "Free /dev/videoX node not found"); |
| 2003 | video_nr[dev_nr] = -1; |
| 2004 | dev_nr = (dev_nr < ZC0301_MAX_DEVICES-1) ? dev_nr+1 : 0; |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 2005 | complete_all(&cam->probe); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 2006 | goto fail; |
| 2007 | } |
| 2008 | |
| 2009 | DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor); |
| 2010 | |
| 2011 | cam->module_param.force_munmap = force_munmap[dev_nr]; |
Luca Risolia | a847423 | 2006-02-25 06:57:49 +0000 | [diff] [blame] | 2012 | cam->module_param.frame_timeout = frame_timeout[dev_nr]; |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 2013 | |
| 2014 | dev_nr = (dev_nr < ZC0301_MAX_DEVICES-1) ? dev_nr+1 : 0; |
| 2015 | |
| 2016 | usb_set_intfdata(intf, cam); |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 2017 | kref_init(&cam->kref); |
| 2018 | usb_get_dev(cam->usbdev); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 2019 | |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 2020 | complete_all(&cam->probe); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 2021 | |
| 2022 | return 0; |
| 2023 | |
| 2024 | fail: |
| 2025 | if (cam) { |
| 2026 | kfree(cam->control_buffer); |
| 2027 | if (cam->v4ldev) |
| 2028 | video_device_release(cam->v4ldev); |
| 2029 | kfree(cam); |
| 2030 | } |
| 2031 | return err; |
| 2032 | } |
| 2033 | |
| 2034 | |
| 2035 | static void zc0301_usb_disconnect(struct usb_interface* intf) |
| 2036 | { |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 2037 | struct zc0301_device* cam; |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 2038 | |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 2039 | down_write(&zc0301_dev_lock); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 2040 | |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 2041 | cam = usb_get_intfdata(intf); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 2042 | |
| 2043 | DBG(2, "Disconnecting %s...", cam->v4ldev->name); |
| 2044 | |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 2045 | if (cam->users) { |
| 2046 | DBG(2, "Device /dev/video%d is open! Deregistration and " |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 2047 | "memory deallocation are deferred.", |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 2048 | cam->v4ldev->minor); |
| 2049 | cam->state |= DEV_MISCONFIGURED; |
| 2050 | zc0301_stop_transfer(cam); |
| 2051 | cam->state |= DEV_DISCONNECTED; |
| 2052 | wake_up_interruptible(&cam->wait_frame); |
Luca Risolia | 9e47a52 | 2006-02-08 00:05:27 +0000 | [diff] [blame] | 2053 | wake_up(&cam->wait_stream); |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 2054 | } else |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 2055 | cam->state |= DEV_DISCONNECTED; |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 2056 | |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 2057 | wake_up_interruptible_all(&cam->wait_open); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 2058 | |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 2059 | kref_put(&cam->kref, zc0301_release_resources); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 2060 | |
Luca Risolia | 4052fcc | 2007-06-13 15:11:15 -0300 | [diff] [blame] | 2061 | up_write(&zc0301_dev_lock); |
Luca Risolia | 60f7805 | 2006-02-06 16:29:35 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
| 2064 | |
| 2065 | static struct usb_driver zc0301_usb_driver = { |
| 2066 | .name = "zc0301", |
| 2067 | .id_table = zc0301_id_table, |
| 2068 | .probe = zc0301_usb_probe, |
| 2069 | .disconnect = zc0301_usb_disconnect, |
| 2070 | }; |
| 2071 | |
| 2072 | /*****************************************************************************/ |
| 2073 | |
| 2074 | static int __init zc0301_module_init(void) |
| 2075 | { |
| 2076 | int err = 0; |
| 2077 | |
| 2078 | KDBG(2, ZC0301_MODULE_NAME " v" ZC0301_MODULE_VERSION); |
| 2079 | KDBG(3, ZC0301_MODULE_AUTHOR); |
| 2080 | |
| 2081 | if ((err = usb_register(&zc0301_usb_driver))) |
| 2082 | KDBG(1, "usb_register() failed"); |
| 2083 | |
| 2084 | return err; |
| 2085 | } |
| 2086 | |
| 2087 | |
| 2088 | static void __exit zc0301_module_exit(void) |
| 2089 | { |
| 2090 | usb_deregister(&zc0301_usb_driver); |
| 2091 | } |
| 2092 | |
| 2093 | |
| 2094 | module_init(zc0301_module_init); |
| 2095 | module_exit(zc0301_module_exit); |