Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Main USB camera driver |
| 3 | * |
Jean-François Moine | 27c6f9e | 2010-04-02 06:59:13 -0300 | [diff] [blame] | 4 | * Copyright (C) 2008-2010 Jean-François Moine <http://moinejf.free.fr> |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 5 | * |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 6 | * Camera button input handling by Márton Németh |
| 7 | * Copyright (C) 2009-2010 Márton Németh <nm127@freemail.hu> |
| 8 | * |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 9 | * This program is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU General Public License as published by the |
| 11 | * Free Software Foundation; either version 2 of the License, or (at your |
| 12 | * option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 16 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 17 | * for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software Foundation, |
| 21 | * Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 22 | */ |
| 23 | |
| 24 | #define MODULE_NAME "gspca" |
| 25 | |
| 26 | #include <linux/init.h> |
Jean-Francois Moine | cf0fe08 | 2008-10-17 04:36:47 -0300 | [diff] [blame] | 27 | #include <linux/version.h> |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 28 | #include <linux/fs.h> |
| 29 | #include <linux/vmalloc.h> |
| 30 | #include <linux/sched.h> |
| 31 | #include <linux/slab.h> |
| 32 | #include <linux/mm.h> |
| 33 | #include <linux/string.h> |
| 34 | #include <linux/pagemap.h> |
Jean-Francois Moine | 956e42d | 2008-07-01 10:03:42 -0300 | [diff] [blame] | 35 | #include <linux/io.h> |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 36 | #include <asm/page.h> |
Jean-Francois Moine | 956e42d | 2008-07-01 10:03:42 -0300 | [diff] [blame] | 37 | #include <linux/uaccess.h> |
Paulo Assis | 2f51d00 | 2010-03-07 06:56:56 -0300 | [diff] [blame] | 38 | #include <linux/ktime.h> |
Hans Verkuil | 35ea11f | 2008-07-20 08:12:02 -0300 | [diff] [blame] | 39 | #include <media/v4l2-ioctl.h> |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 40 | |
| 41 | #include "gspca.h" |
| 42 | |
Randy Dunlap | f762801 | 2010-03-30 14:32:04 -0300 | [diff] [blame] | 43 | #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE) |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 44 | #include <linux/input.h> |
| 45 | #include <linux/usb/input.h> |
Jean-François Moine | e070bf9 | 2010-02-18 15:02:51 -0300 | [diff] [blame] | 46 | #endif |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 47 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 48 | /* global values */ |
Jean-Francois Moine | 8fee845 | 2009-01-17 04:46:38 -0300 | [diff] [blame] | 49 | #define DEF_NURBS 3 /* default number of URBs */ |
| 50 | #if DEF_NURBS > MAX_NURBS |
| 51 | #error "DEF_NURBS too big" |
| 52 | #endif |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 53 | |
Jean-François Moine | 27c6f9e | 2010-04-02 06:59:13 -0300 | [diff] [blame] | 54 | MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>"); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 55 | MODULE_DESCRIPTION("GSPCA USB Camera Driver"); |
| 56 | MODULE_LICENSE("GPL"); |
| 57 | |
Jean-François Moine | 55461f4 | 2010-01-30 16:03:02 -0300 | [diff] [blame] | 58 | #define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 9, 0) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 59 | |
Jean-Francois Moine | 335b3f8 | 2008-07-30 04:53:02 -0300 | [diff] [blame] | 60 | #ifdef GSPCA_DEBUG |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 61 | int gspca_debug = D_ERR | D_PROBE; |
| 62 | EXPORT_SYMBOL(gspca_debug); |
| 63 | |
| 64 | static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h) |
| 65 | { |
| 66 | if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') { |
| 67 | PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d", |
| 68 | txt, |
| 69 | pixfmt & 0xff, |
| 70 | (pixfmt >> 8) & 0xff, |
| 71 | (pixfmt >> 16) & 0xff, |
| 72 | pixfmt >> 24, |
| 73 | w, h); |
| 74 | } else { |
| 75 | PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d", |
| 76 | txt, |
| 77 | pixfmt, |
| 78 | w, h); |
| 79 | } |
| 80 | } |
| 81 | #else |
| 82 | #define PDEBUG_MODE(txt, pixfmt, w, h) |
| 83 | #endif |
| 84 | |
Jean-Francois Moine | 77ee331 | 2009-11-07 15:30:50 -0300 | [diff] [blame] | 85 | /* specific memory types - !! should be different from V4L2_MEMORY_xxx */ |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 86 | #define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */ |
| 87 | #define GSPCA_MEMORY_READ 7 |
| 88 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 89 | #define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE) |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 90 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 91 | /* |
| 92 | * VMA operations. |
| 93 | */ |
| 94 | static void gspca_vm_open(struct vm_area_struct *vma) |
| 95 | { |
| 96 | struct gspca_frame *frame = vma->vm_private_data; |
| 97 | |
| 98 | frame->vma_use_count++; |
| 99 | frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED; |
| 100 | } |
| 101 | |
| 102 | static void gspca_vm_close(struct vm_area_struct *vma) |
| 103 | { |
| 104 | struct gspca_frame *frame = vma->vm_private_data; |
| 105 | |
| 106 | if (--frame->vma_use_count <= 0) |
| 107 | frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED; |
| 108 | } |
| 109 | |
Alexey Dobriyan | f0f37e2 | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 110 | static const struct vm_operations_struct gspca_vm_ops = { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 111 | .open = gspca_vm_open, |
| 112 | .close = gspca_vm_close, |
| 113 | }; |
| 114 | |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 115 | /* |
| 116 | * Input and interrupt endpoint handling functions |
| 117 | */ |
Randy Dunlap | f762801 | 2010-03-30 14:32:04 -0300 | [diff] [blame] | 118 | #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE) |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 119 | static void int_irq(struct urb *urb) |
| 120 | { |
| 121 | struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context; |
| 122 | int ret; |
| 123 | |
| 124 | ret = urb->status; |
| 125 | switch (ret) { |
| 126 | case 0: |
| 127 | if (gspca_dev->sd_desc->int_pkt_scan(gspca_dev, |
| 128 | urb->transfer_buffer, urb->actual_length) < 0) { |
| 129 | PDEBUG(D_ERR, "Unknown packet received"); |
| 130 | } |
| 131 | break; |
| 132 | |
| 133 | case -ENOENT: |
| 134 | case -ECONNRESET: |
| 135 | case -ENODEV: |
| 136 | case -ESHUTDOWN: |
| 137 | /* Stop is requested either by software or hardware is gone, |
| 138 | * keep the ret value non-zero and don't resubmit later. |
| 139 | */ |
| 140 | break; |
| 141 | |
| 142 | default: |
| 143 | PDEBUG(D_ERR, "URB error %i, resubmitting", urb->status); |
| 144 | urb->status = 0; |
| 145 | ret = 0; |
| 146 | } |
| 147 | |
| 148 | if (ret == 0) { |
| 149 | ret = usb_submit_urb(urb, GFP_ATOMIC); |
| 150 | if (ret < 0) |
| 151 | PDEBUG(D_ERR, "Resubmit URB failed with error %i", ret); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | static int gspca_input_connect(struct gspca_dev *dev) |
| 156 | { |
| 157 | struct input_dev *input_dev; |
| 158 | int err = 0; |
| 159 | |
| 160 | dev->input_dev = NULL; |
Hans de Goede | ac82f59 | 2010-02-19 04:28:39 -0300 | [diff] [blame] | 161 | if (dev->sd_desc->int_pkt_scan || dev->sd_desc->other_input) { |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 162 | input_dev = input_allocate_device(); |
| 163 | if (!input_dev) |
| 164 | return -ENOMEM; |
| 165 | |
| 166 | usb_make_path(dev->dev, dev->phys, sizeof(dev->phys)); |
| 167 | strlcat(dev->phys, "/input0", sizeof(dev->phys)); |
| 168 | |
| 169 | input_dev->name = dev->sd_desc->name; |
| 170 | input_dev->phys = dev->phys; |
| 171 | |
| 172 | usb_to_input_id(dev->dev, &input_dev->id); |
| 173 | |
| 174 | input_dev->evbit[0] = BIT_MASK(EV_KEY); |
| 175 | input_dev->keybit[BIT_WORD(KEY_CAMERA)] = BIT_MASK(KEY_CAMERA); |
| 176 | input_dev->dev.parent = &dev->dev->dev; |
| 177 | |
| 178 | err = input_register_device(input_dev); |
| 179 | if (err) { |
| 180 | PDEBUG(D_ERR, "Input device registration failed " |
| 181 | "with error %i", err); |
| 182 | input_dev->dev.parent = NULL; |
| 183 | input_free_device(input_dev); |
| 184 | } else { |
| 185 | dev->input_dev = input_dev; |
| 186 | } |
Hans de Goede | 10ee240 | 2010-02-19 04:41:40 -0300 | [diff] [blame] | 187 | } |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 188 | |
| 189 | return err; |
| 190 | } |
| 191 | |
| 192 | static int alloc_and_submit_int_urb(struct gspca_dev *gspca_dev, |
| 193 | struct usb_endpoint_descriptor *ep) |
| 194 | { |
| 195 | unsigned int buffer_len; |
| 196 | int interval; |
| 197 | struct urb *urb; |
| 198 | struct usb_device *dev; |
| 199 | void *buffer = NULL; |
| 200 | int ret = -EINVAL; |
| 201 | |
Jean-François Moine | b26d6e2 | 2010-04-29 06:01:53 -0300 | [diff] [blame] | 202 | buffer_len = le16_to_cpu(ep->wMaxPacketSize); |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 203 | interval = ep->bInterval; |
Jean-François Moine | c4dc692 | 2010-07-14 06:26:54 -0300 | [diff] [blame^] | 204 | PDEBUG(D_CONF, "found int in endpoint: 0x%x, " |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 205 | "buffer_len=%u, interval=%u", |
| 206 | ep->bEndpointAddress, buffer_len, interval); |
| 207 | |
| 208 | dev = gspca_dev->dev; |
| 209 | |
| 210 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 211 | if (!urb) { |
| 212 | ret = -ENOMEM; |
| 213 | goto error; |
| 214 | } |
| 215 | |
Linus Torvalds | 7a9b149 | 2010-05-20 21:26:12 -0700 | [diff] [blame] | 216 | buffer = usb_alloc_coherent(dev, buffer_len, |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 217 | GFP_KERNEL, &urb->transfer_dma); |
| 218 | if (!buffer) { |
| 219 | ret = -ENOMEM; |
| 220 | goto error_buffer; |
| 221 | } |
| 222 | usb_fill_int_urb(urb, dev, |
| 223 | usb_rcvintpipe(dev, ep->bEndpointAddress), |
| 224 | buffer, buffer_len, |
| 225 | int_irq, (void *)gspca_dev, interval); |
| 226 | gspca_dev->int_urb = urb; |
| 227 | ret = usb_submit_urb(urb, GFP_KERNEL); |
| 228 | if (ret < 0) { |
Jean-François Moine | c4dc692 | 2010-07-14 06:26:54 -0300 | [diff] [blame^] | 229 | PDEBUG(D_ERR, "submit int URB failed with error %i", ret); |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 230 | goto error_submit; |
| 231 | } |
| 232 | return ret; |
| 233 | |
| 234 | error_submit: |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 235 | usb_free_coherent(dev, |
| 236 | urb->transfer_buffer_length, |
| 237 | urb->transfer_buffer, |
| 238 | urb->transfer_dma); |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 239 | error_buffer: |
| 240 | usb_free_urb(urb); |
| 241 | error: |
| 242 | return ret; |
| 243 | } |
| 244 | |
Hans de Goede | 10ee240 | 2010-02-19 04:41:40 -0300 | [diff] [blame] | 245 | static void gspca_input_create_urb(struct gspca_dev *gspca_dev) |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 246 | { |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 247 | struct usb_interface *intf; |
| 248 | struct usb_host_interface *intf_desc; |
| 249 | struct usb_endpoint_descriptor *ep; |
| 250 | int i; |
| 251 | |
| 252 | if (gspca_dev->sd_desc->int_pkt_scan) { |
| 253 | intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface); |
| 254 | intf_desc = intf->cur_altsetting; |
| 255 | for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) { |
| 256 | ep = &intf_desc->endpoint[i].desc; |
| 257 | if (usb_endpoint_dir_in(ep) && |
| 258 | usb_endpoint_xfer_int(ep)) { |
| 259 | |
Hans de Goede | 10ee240 | 2010-02-19 04:41:40 -0300 | [diff] [blame] | 260 | alloc_and_submit_int_urb(gspca_dev, ep); |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 261 | break; |
| 262 | } |
| 263 | } |
| 264 | } |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | static void gspca_input_destroy_urb(struct gspca_dev *gspca_dev) |
| 268 | { |
| 269 | struct urb *urb; |
| 270 | |
| 271 | urb = gspca_dev->int_urb; |
| 272 | if (urb) { |
| 273 | gspca_dev->int_urb = NULL; |
| 274 | usb_kill_urb(urb); |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 275 | usb_free_coherent(gspca_dev->dev, |
| 276 | urb->transfer_buffer_length, |
| 277 | urb->transfer_buffer, |
| 278 | urb->transfer_dma); |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 279 | usb_free_urb(urb); |
| 280 | } |
| 281 | } |
Randy Dunlap | f762801 | 2010-03-30 14:32:04 -0300 | [diff] [blame] | 282 | #else |
| 283 | static inline void gspca_input_destroy_urb(struct gspca_dev *gspca_dev) |
| 284 | { |
| 285 | } |
| 286 | |
| 287 | static inline void gspca_input_create_urb(struct gspca_dev *gspca_dev) |
| 288 | { |
| 289 | } |
| 290 | |
| 291 | static inline int gspca_input_connect(struct gspca_dev *dev) |
| 292 | { |
| 293 | return 0; |
| 294 | } |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 295 | #endif |
| 296 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 297 | /* |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 298 | * fill a video frame from an URB and resubmit |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 299 | */ |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 300 | static void fill_frame(struct gspca_dev *gspca_dev, |
| 301 | struct urb *urb) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 302 | { |
Jean-Francois Moine | 8d584a5 | 2009-01-13 06:07:59 -0300 | [diff] [blame] | 303 | u8 *data; /* address of data in the iso message */ |
Jean-Francois Moine | 1b60e1a | 2008-09-20 05:44:21 -0300 | [diff] [blame] | 304 | int i, len, st; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 305 | cam_pkt_op pkt_scan; |
| 306 | |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 307 | if (urb->status != 0) { |
Jean-Francois Moine | 090264e | 2009-02-12 08:05:45 -0300 | [diff] [blame] | 308 | if (urb->status == -ESHUTDOWN) |
| 309 | return; /* disconnection */ |
Jean-Francois Moine | 62ee8ee | 2008-09-03 17:12:13 -0300 | [diff] [blame] | 310 | #ifdef CONFIG_PM |
Hans de Goede | 6a540bd | 2009-10-24 15:02:14 -0300 | [diff] [blame] | 311 | if (gspca_dev->frozen) |
| 312 | return; |
Jean-Francois Moine | 62ee8ee | 2008-09-03 17:12:13 -0300 | [diff] [blame] | 313 | #endif |
Hans de Goede | 6a540bd | 2009-10-24 15:02:14 -0300 | [diff] [blame] | 314 | PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status); |
Jean-Francois Moine | 8157852 | 2009-11-13 07:15:08 -0300 | [diff] [blame] | 315 | urb->status = 0; |
Hans de Goede | bf926ad | 2009-10-23 06:56:06 -0300 | [diff] [blame] | 316 | goto resubmit; |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 317 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 318 | pkt_scan = gspca_dev->sd_desc->pkt_scan; |
| 319 | for (i = 0; i < urb->number_of_packets; i++) { |
| 320 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 321 | /* check the packet status and length */ |
| 322 | len = urb->iso_frame_desc[i].actual_length; |
Jean-Francois Moine | ff37474 | 2008-10-23 07:29:51 -0300 | [diff] [blame] | 323 | if (len == 0) { |
| 324 | if (gspca_dev->empty_packet == 0) |
| 325 | gspca_dev->empty_packet = 1; |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 326 | continue; |
Jean-Francois Moine | ff37474 | 2008-10-23 07:29:51 -0300 | [diff] [blame] | 327 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 328 | st = urb->iso_frame_desc[i].status; |
| 329 | if (st) { |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 330 | PDEBUG(D_ERR, |
| 331 | "ISOC data error: [%d] len=%d, status=%d", |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 332 | i, len, st); |
| 333 | gspca_dev->last_packet_type = DISCARD_PACKET; |
| 334 | continue; |
| 335 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 336 | |
| 337 | /* let the packet be analyzed by the subdriver */ |
| 338 | PDEBUG(D_PACK, "packet [%d] o:%d l:%d", |
| 339 | i, urb->iso_frame_desc[i].offset, len); |
Jean-Francois Moine | 8d584a5 | 2009-01-13 06:07:59 -0300 | [diff] [blame] | 340 | data = (u8 *) urb->transfer_buffer |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 341 | + urb->iso_frame_desc[i].offset; |
Jean-Francois Moine | 76dd272 | 2009-11-13 09:21:03 -0300 | [diff] [blame] | 342 | pkt_scan(gspca_dev, data, len); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 343 | } |
| 344 | |
Hans de Goede | bf926ad | 2009-10-23 06:56:06 -0300 | [diff] [blame] | 345 | resubmit: |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 346 | /* resubmit the URB */ |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 347 | st = usb_submit_urb(urb, GFP_ATOMIC); |
| 348 | if (st < 0) |
| 349 | PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st); |
| 350 | } |
| 351 | |
| 352 | /* |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 353 | * ISOC message interrupt from the USB device |
| 354 | * |
Jean-Francois Moine | ac0d6df | 2008-07-08 06:58:15 -0300 | [diff] [blame] | 355 | * Analyse each packet and call the subdriver for copy to the frame buffer. |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 356 | */ |
Jean-Francois Moine | 8d584a5 | 2009-01-13 06:07:59 -0300 | [diff] [blame] | 357 | static void isoc_irq(struct urb *urb) |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 358 | { |
| 359 | struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context; |
| 360 | |
Hans de Goede | 3647fea | 2008-07-15 05:36:30 -0300 | [diff] [blame] | 361 | PDEBUG(D_PACK, "isoc irq"); |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 362 | if (!gspca_dev->streaming) |
| 363 | return; |
| 364 | fill_frame(gspca_dev, urb); |
| 365 | } |
| 366 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 367 | /* |
Jean-Francois Moine | 0be0100 | 2008-09-04 07:01:50 -0300 | [diff] [blame] | 368 | * bulk message interrupt from the USB device |
| 369 | */ |
Jean-Francois Moine | 8d584a5 | 2009-01-13 06:07:59 -0300 | [diff] [blame] | 370 | static void bulk_irq(struct urb *urb) |
Jean-Francois Moine | 0be0100 | 2008-09-04 07:01:50 -0300 | [diff] [blame] | 371 | { |
| 372 | struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context; |
Antonio Ospite | dff369a | 2008-11-14 07:53:32 -0300 | [diff] [blame] | 373 | int st; |
Jean-Francois Moine | 0be0100 | 2008-09-04 07:01:50 -0300 | [diff] [blame] | 374 | |
| 375 | PDEBUG(D_PACK, "bulk irq"); |
| 376 | if (!gspca_dev->streaming) |
| 377 | return; |
Jean-Francois Moine | 880ae24 | 2008-11-18 04:19:52 -0300 | [diff] [blame] | 378 | switch (urb->status) { |
| 379 | case 0: |
| 380 | break; |
Jean-Francois Moine | 090264e | 2009-02-12 08:05:45 -0300 | [diff] [blame] | 381 | case -ESHUTDOWN: |
| 382 | return; /* disconnection */ |
Jean-Francois Moine | 880ae24 | 2008-11-18 04:19:52 -0300 | [diff] [blame] | 383 | default: |
Jean-Francois Moine | 0be0100 | 2008-09-04 07:01:50 -0300 | [diff] [blame] | 384 | #ifdef CONFIG_PM |
Hans de Goede | 6a540bd | 2009-10-24 15:02:14 -0300 | [diff] [blame] | 385 | if (gspca_dev->frozen) |
| 386 | return; |
Jean-Francois Moine | 0be0100 | 2008-09-04 07:01:50 -0300 | [diff] [blame] | 387 | #endif |
Hans de Goede | 6a540bd | 2009-10-24 15:02:14 -0300 | [diff] [blame] | 388 | PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status); |
Jean-Francois Moine | 8157852 | 2009-11-13 07:15:08 -0300 | [diff] [blame] | 389 | urb->status = 0; |
Hans de Goede | bf926ad | 2009-10-23 06:56:06 -0300 | [diff] [blame] | 390 | goto resubmit; |
Jean-Francois Moine | 0be0100 | 2008-09-04 07:01:50 -0300 | [diff] [blame] | 391 | } |
| 392 | |
Jean-Francois Moine | d131c3c | 2009-11-13 08:16:02 -0300 | [diff] [blame] | 393 | PDEBUG(D_PACK, "packet l:%d", urb->actual_length); |
| 394 | gspca_dev->sd_desc->pkt_scan(gspca_dev, |
Jean-Francois Moine | d131c3c | 2009-11-13 08:16:02 -0300 | [diff] [blame] | 395 | urb->transfer_buffer, |
| 396 | urb->actual_length); |
Antonio Ospite | dff369a | 2008-11-14 07:53:32 -0300 | [diff] [blame] | 397 | |
Hans de Goede | bf926ad | 2009-10-23 06:56:06 -0300 | [diff] [blame] | 398 | resubmit: |
Antonio Ospite | dff369a | 2008-11-14 07:53:32 -0300 | [diff] [blame] | 399 | /* resubmit the URB */ |
| 400 | if (gspca_dev->cam.bulk_nurbs != 0) { |
| 401 | st = usb_submit_urb(urb, GFP_ATOMIC); |
| 402 | if (st < 0) |
| 403 | PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st); |
| 404 | } |
Jean-Francois Moine | 0be0100 | 2008-09-04 07:01:50 -0300 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | /* |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 408 | * add data to the current frame |
| 409 | * |
Jean-Francois Moine | ac0d6df | 2008-07-08 06:58:15 -0300 | [diff] [blame] | 410 | * This function is called by the subdrivers at interrupt level. |
| 411 | * |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 412 | * To build a frame, these ones must add |
| 413 | * - one FIRST_PACKET |
| 414 | * - 0 or many INTER_PACKETs |
| 415 | * - one LAST_PACKET |
| 416 | * DISCARD_PACKET invalidates the whole frame. |
| 417 | * On LAST_PACKET, a new frame is returned. |
| 418 | */ |
Jean-Francois Moine | 76dd272 | 2009-11-13 09:21:03 -0300 | [diff] [blame] | 419 | void gspca_frame_add(struct gspca_dev *gspca_dev, |
| 420 | enum gspca_packet_type packet_type, |
| 421 | const u8 *data, |
| 422 | int len) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 423 | { |
Jean-Francois Moine | 3ec342f | 2009-11-13 07:38:16 -0300 | [diff] [blame] | 424 | struct gspca_frame *frame; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 425 | int i, j; |
| 426 | |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 427 | PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len); |
Jean-Francois Moine | d131c3c | 2009-11-13 08:16:02 -0300 | [diff] [blame] | 428 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 429 | if (packet_type == FIRST_PACKET) { |
Jean-François Moine | f7059ea | 2010-07-06 04:32:27 -0300 | [diff] [blame] | 430 | i = atomic_read(&gspca_dev->fr_i); |
| 431 | |
| 432 | /* if there are no queued buffer, discard the whole frame */ |
| 433 | if (i == atomic_read(&gspca_dev->fr_q)) { |
| 434 | gspca_dev->last_packet_type = DISCARD_PACKET; |
| 435 | return; |
| 436 | } |
Jean-François Moine | b192ca9 | 2010-06-27 03:08:19 -0300 | [diff] [blame] | 437 | j = gspca_dev->fr_queue[i]; |
| 438 | frame = &gspca_dev->frame[j]; |
Paulo Assis | 2f51d00 | 2010-03-07 06:56:56 -0300 | [diff] [blame] | 439 | frame->v4l2_buf.timestamp = ktime_to_timeval(ktime_get()); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 440 | frame->v4l2_buf.sequence = ++gspca_dev->sequence; |
Jean-François Moine | f7059ea | 2010-07-06 04:32:27 -0300 | [diff] [blame] | 441 | gspca_dev->image = frame->data; |
Jean-François Moine | b192ca9 | 2010-06-27 03:08:19 -0300 | [diff] [blame] | 442 | gspca_dev->image_len = 0; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 443 | } else if (gspca_dev->last_packet_type == DISCARD_PACKET) { |
Jean-Francois Moine | 06ca78f | 2008-07-22 03:45:08 -0300 | [diff] [blame] | 444 | if (packet_type == LAST_PACKET) |
| 445 | gspca_dev->last_packet_type = packet_type; |
Jean-Francois Moine | 76dd272 | 2009-11-13 09:21:03 -0300 | [diff] [blame] | 446 | return; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 447 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 448 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 449 | /* append the packet to the frame buffer */ |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 450 | if (len > 0) { |
Jean-François Moine | b192ca9 | 2010-06-27 03:08:19 -0300 | [diff] [blame] | 451 | if (gspca_dev->image_len + len > gspca_dev->frsz) { |
| 452 | PDEBUG(D_ERR|D_PACK, "frame overflow %d > %d", |
| 453 | gspca_dev->image_len + len, |
| 454 | gspca_dev->frsz); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 455 | packet_type = DISCARD_PACKET; |
| 456 | } else { |
Jean-François Moine | b192ca9 | 2010-06-27 03:08:19 -0300 | [diff] [blame] | 457 | memcpy(gspca_dev->image + gspca_dev->image_len, |
| 458 | data, len); |
| 459 | gspca_dev->image_len += len; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | gspca_dev->last_packet_type = packet_type; |
| 463 | |
Jean-François Moine | f7059ea | 2010-07-06 04:32:27 -0300 | [diff] [blame] | 464 | /* if last packet, invalidate packet concatenation until |
| 465 | * next first packet, wake up the application and advance |
| 466 | * in the queue */ |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 467 | if (packet_type == LAST_PACKET) { |
Jean-François Moine | f7059ea | 2010-07-06 04:32:27 -0300 | [diff] [blame] | 468 | i = atomic_read(&gspca_dev->fr_i); |
Jean-François Moine | b192ca9 | 2010-06-27 03:08:19 -0300 | [diff] [blame] | 469 | j = gspca_dev->fr_queue[i]; |
| 470 | frame = &gspca_dev->frame[j]; |
| 471 | frame->v4l2_buf.bytesused = gspca_dev->image_len; |
Jean-François Moine | 02bbcb9 | 2010-07-06 04:16:40 -0300 | [diff] [blame] | 472 | frame->v4l2_buf.flags = (frame->v4l2_buf.flags |
| 473 | | V4L2_BUF_FLAG_DONE) |
| 474 | & ~V4L2_BUF_FLAG_QUEUED; |
Jean-François Moine | f7059ea | 2010-07-06 04:32:27 -0300 | [diff] [blame] | 475 | i = (i + 1) % GSPCA_MAX_FRAMES; |
| 476 | atomic_set(&gspca_dev->fr_i, i); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 477 | wake_up_interruptible(&gspca_dev->wq); /* event = new frame */ |
Jean-François Moine | f7059ea | 2010-07-06 04:32:27 -0300 | [diff] [blame] | 478 | PDEBUG(D_FRAM, "frame complete len:%d", |
| 479 | frame->v4l2_buf.bytesused); |
| 480 | gspca_dev->image = NULL; |
| 481 | gspca_dev->image_len = 0; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 482 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 483 | } |
| 484 | EXPORT_SYMBOL(gspca_frame_add); |
| 485 | |
| 486 | static int gspca_is_compressed(__u32 format) |
| 487 | { |
| 488 | switch (format) { |
| 489 | case V4L2_PIX_FMT_MJPEG: |
| 490 | case V4L2_PIX_FMT_JPEG: |
Jean-Francois Moine | 50a871f | 2008-06-30 19:47:33 -0300 | [diff] [blame] | 491 | case V4L2_PIX_FMT_SPCA561: |
Hans de Goede | ab8f12c | 2008-07-04 18:29:32 -0300 | [diff] [blame] | 492 | case V4L2_PIX_FMT_PAC207: |
Kyle Guinn | d661e62 | 2009-01-16 05:36:14 -0300 | [diff] [blame] | 493 | case V4L2_PIX_FMT_MR97310A: |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 494 | return 1; |
| 495 | } |
| 496 | return 0; |
| 497 | } |
| 498 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 499 | static int frame_alloc(struct gspca_dev *gspca_dev, |
| 500 | unsigned int count) |
| 501 | { |
| 502 | struct gspca_frame *frame; |
| 503 | unsigned int frsz; |
| 504 | int i; |
| 505 | |
Jean-Francois Moine | c2446b3 | 2008-07-05 11:49:20 -0300 | [diff] [blame] | 506 | i = gspca_dev->curr_mode; |
| 507 | frsz = gspca_dev->cam.cam_mode[i].sizeimage; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 508 | PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 509 | frsz = PAGE_ALIGN(frsz); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 510 | gspca_dev->frsz = frsz; |
Jean-François Moine | f7059ea | 2010-07-06 04:32:27 -0300 | [diff] [blame] | 511 | if (count >= GSPCA_MAX_FRAMES) |
| 512 | count = GSPCA_MAX_FRAMES - 1; |
Jean-François Moine | e795d91 | 2010-06-24 05:14:22 -0300 | [diff] [blame] | 513 | gspca_dev->frbuf = vmalloc_32(frsz * count); |
Jean-Francois Moine | ac0d6df | 2008-07-08 06:58:15 -0300 | [diff] [blame] | 514 | if (!gspca_dev->frbuf) { |
| 515 | err("frame alloc failed"); |
| 516 | return -ENOMEM; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 517 | } |
| 518 | gspca_dev->nframes = count; |
| 519 | for (i = 0; i < count; i++) { |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 520 | frame = &gspca_dev->frame[i]; |
| 521 | frame->v4l2_buf.index = i; |
| 522 | frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 523 | frame->v4l2_buf.flags = 0; |
| 524 | frame->v4l2_buf.field = V4L2_FIELD_NONE; |
| 525 | frame->v4l2_buf.length = frsz; |
| 526 | frame->v4l2_buf.memory = gspca_dev->memory; |
| 527 | frame->v4l2_buf.sequence = 0; |
Jean-François Moine | b192ca9 | 2010-06-27 03:08:19 -0300 | [diff] [blame] | 528 | frame->data = gspca_dev->frbuf + i * frsz; |
Jean-Francois Moine | ac0d6df | 2008-07-08 06:58:15 -0300 | [diff] [blame] | 529 | frame->v4l2_buf.m.offset = i * frsz; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 530 | } |
Jean-François Moine | f7059ea | 2010-07-06 04:32:27 -0300 | [diff] [blame] | 531 | atomic_set(&gspca_dev->fr_q, 0); |
| 532 | atomic_set(&gspca_dev->fr_i, 0); |
| 533 | gspca_dev->fr_o = 0; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 534 | return 0; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | static void frame_free(struct gspca_dev *gspca_dev) |
| 538 | { |
| 539 | int i; |
| 540 | |
| 541 | PDEBUG(D_STREAM, "frame free"); |
Harvey Harrison | efab821 | 2008-07-05 06:12:47 -0300 | [diff] [blame] | 542 | if (gspca_dev->frbuf != NULL) { |
Jean-François Moine | e795d91 | 2010-06-24 05:14:22 -0300 | [diff] [blame] | 543 | vfree(gspca_dev->frbuf); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 544 | gspca_dev->frbuf = NULL; |
| 545 | for (i = 0; i < gspca_dev->nframes; i++) |
| 546 | gspca_dev->frame[i].data = NULL; |
| 547 | } |
| 548 | gspca_dev->nframes = 0; |
| 549 | } |
| 550 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 551 | static void destroy_urbs(struct gspca_dev *gspca_dev) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 552 | { |
| 553 | struct urb *urb; |
| 554 | unsigned int i; |
| 555 | |
| 556 | PDEBUG(D_STREAM, "kill transfer"); |
Erik Andrén | 4473281 | 2008-10-13 15:52:46 -0300 | [diff] [blame] | 557 | for (i = 0; i < MAX_NURBS; i++) { |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 558 | urb = gspca_dev->urb[i]; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 559 | if (urb == NULL) |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 560 | break; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 561 | |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 562 | gspca_dev->urb[i] = NULL; |
Jean-Francois Moine | 090264e | 2009-02-12 08:05:45 -0300 | [diff] [blame] | 563 | usb_kill_urb(urb); |
Harvey Harrison | efab821 | 2008-07-05 06:12:47 -0300 | [diff] [blame] | 564 | if (urb->transfer_buffer != NULL) |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 565 | usb_free_coherent(gspca_dev->dev, |
| 566 | urb->transfer_buffer_length, |
| 567 | urb->transfer_buffer, |
| 568 | urb->transfer_dma); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 569 | usb_free_urb(urb); |
| 570 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 571 | } |
| 572 | |
Jean-François Moine | e4dac28 | 2010-04-09 06:11:36 -0300 | [diff] [blame] | 573 | static int gspca_set_alt0(struct gspca_dev *gspca_dev) |
| 574 | { |
| 575 | int ret; |
| 576 | |
| 577 | if (gspca_dev->alt == 0) |
| 578 | return 0; |
| 579 | ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0); |
| 580 | if (ret < 0) |
| 581 | PDEBUG(D_ERR|D_STREAM, "set alt 0 err %d", ret); |
| 582 | return ret; |
| 583 | } |
| 584 | |
| 585 | /* Note: both the queue and the usb locks should be held when calling this */ |
| 586 | static void gspca_stream_off(struct gspca_dev *gspca_dev) |
| 587 | { |
| 588 | gspca_dev->streaming = 0; |
| 589 | if (gspca_dev->present) { |
| 590 | if (gspca_dev->sd_desc->stopN) |
| 591 | gspca_dev->sd_desc->stopN(gspca_dev); |
| 592 | destroy_urbs(gspca_dev); |
| 593 | gspca_input_destroy_urb(gspca_dev); |
| 594 | gspca_set_alt0(gspca_dev); |
| 595 | gspca_input_create_urb(gspca_dev); |
| 596 | } |
| 597 | |
| 598 | /* always call stop0 to free the subdriver's resources */ |
| 599 | if (gspca_dev->sd_desc->stop0) |
| 600 | gspca_dev->sd_desc->stop0(gspca_dev); |
| 601 | PDEBUG(D_STREAM, "stream off OK"); |
| 602 | } |
| 603 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 604 | /* |
Frank Zago | 219423c | 2008-09-26 07:43:54 -0300 | [diff] [blame] | 605 | * look for an input transfer endpoint in an alternate setting |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 606 | */ |
Jean-Francois Moine | 0be0100 | 2008-09-04 07:01:50 -0300 | [diff] [blame] | 607 | static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt, |
Jean-Francois Moine | 6929dc6 | 2009-04-21 13:45:56 -0300 | [diff] [blame] | 608 | int xfer) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 609 | { |
| 610 | struct usb_host_endpoint *ep; |
| 611 | int i, attr; |
| 612 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 613 | for (i = 0; i < alt->desc.bNumEndpoints; i++) { |
| 614 | ep = &alt->endpoint[i]; |
Jean-Francois Moine | 50e06de | 2008-12-31 08:13:46 -0300 | [diff] [blame] | 615 | attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK; |
Jean-Francois Moine | 30d35e4 | 2009-04-21 13:57:31 -0300 | [diff] [blame] | 616 | if (attr == xfer |
| 617 | && ep->desc.wMaxPacketSize != 0) |
Jean-Francois Moine | 50e06de | 2008-12-31 08:13:46 -0300 | [diff] [blame] | 618 | return ep; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 619 | } |
| 620 | return NULL; |
| 621 | } |
| 622 | |
| 623 | /* |
Frank Zago | 219423c | 2008-09-26 07:43:54 -0300 | [diff] [blame] | 624 | * look for an input (isoc or bulk) endpoint |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 625 | * |
| 626 | * The endpoint is defined by the subdriver. |
| 627 | * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep). |
| 628 | * This routine may be called many times when the bandwidth is too small |
| 629 | * (the bandwidth is checked on urb submit). |
| 630 | */ |
Jean-Francois Moine | 0be0100 | 2008-09-04 07:01:50 -0300 | [diff] [blame] | 631 | static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 632 | { |
| 633 | struct usb_interface *intf; |
| 634 | struct usb_host_endpoint *ep; |
Jean-Francois Moine | 6929dc6 | 2009-04-21 13:45:56 -0300 | [diff] [blame] | 635 | int xfer, i, ret; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 636 | |
| 637 | intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface); |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 638 | ep = NULL; |
Jean-Francois Moine | 6929dc6 | 2009-04-21 13:45:56 -0300 | [diff] [blame] | 639 | xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK |
| 640 | : USB_ENDPOINT_XFER_ISOC; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 641 | i = gspca_dev->alt; /* previous alt setting */ |
Hans de Goede | a511ba9 | 2009-10-16 07:13:07 -0300 | [diff] [blame] | 642 | if (gspca_dev->cam.reverse_alts) { |
| 643 | while (++i < gspca_dev->nbalt) { |
| 644 | ep = alt_xfer(&intf->altsetting[i], xfer); |
| 645 | if (ep) |
| 646 | break; |
| 647 | } |
| 648 | } else { |
| 649 | while (--i >= 0) { |
| 650 | ep = alt_xfer(&intf->altsetting[i], xfer); |
| 651 | if (ep) |
| 652 | break; |
| 653 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 654 | } |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 655 | if (ep == NULL) { |
Jean-Francois Moine | 6929dc6 | 2009-04-21 13:45:56 -0300 | [diff] [blame] | 656 | err("no transfer endpoint found"); |
| 657 | return NULL; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 658 | } |
Jean-Francois Moine | 0be0100 | 2008-09-04 07:01:50 -0300 | [diff] [blame] | 659 | PDEBUG(D_STREAM, "use alt %d ep 0x%02x", |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 660 | i, ep->desc.bEndpointAddress); |
Jean-Francois Moine | 1c8336d | 2009-07-28 04:38:25 -0300 | [diff] [blame] | 661 | gspca_dev->alt = i; /* memorize the current alt setting */ |
Jean-Francois Moine | 8dd07ef | 2009-04-21 14:05:44 -0300 | [diff] [blame] | 662 | if (gspca_dev->nbalt > 1) { |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 663 | gspca_input_destroy_urb(gspca_dev); |
Jean-Francois Moine | 95d9142 | 2008-09-29 05:57:32 -0300 | [diff] [blame] | 664 | ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i); |
| 665 | if (ret < 0) { |
Jean-Francois Moine | 6929dc6 | 2009-04-21 13:45:56 -0300 | [diff] [blame] | 666 | err("set alt %d err %d", i, ret); |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 667 | ep = NULL; |
Jean-Francois Moine | 95d9142 | 2008-09-29 05:57:32 -0300 | [diff] [blame] | 668 | } |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 669 | gspca_input_create_urb(gspca_dev); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 670 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 671 | return ep; |
| 672 | } |
| 673 | |
| 674 | /* |
Jean-Francois Moine | 0be0100 | 2008-09-04 07:01:50 -0300 | [diff] [blame] | 675 | * create the URBs for image transfer |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 676 | */ |
| 677 | static int create_urbs(struct gspca_dev *gspca_dev, |
| 678 | struct usb_host_endpoint *ep) |
| 679 | { |
| 680 | struct urb *urb; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 681 | int n, nurbs, i, psize, npkt, bsize; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 682 | |
| 683 | /* calculate the packet size and the number of packets */ |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 684 | psize = le16_to_cpu(ep->desc.wMaxPacketSize); |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 685 | |
Jean-Francois Moine | 6929dc6 | 2009-04-21 13:45:56 -0300 | [diff] [blame] | 686 | if (!gspca_dev->cam.bulk) { /* isoc */ |
Jean-Francois Moine | 95d9142 | 2008-09-29 05:57:32 -0300 | [diff] [blame] | 687 | |
| 688 | /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */ |
Jean-Francois Moine | 1f53b0b | 2009-06-30 07:07:01 -0300 | [diff] [blame] | 689 | if (gspca_dev->pkt_size == 0) |
| 690 | psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3)); |
| 691 | else |
| 692 | psize = gspca_dev->pkt_size; |
Jean-Francois Moine | 49cb6b0 | 2009-04-25 13:29:01 -0300 | [diff] [blame] | 693 | npkt = gspca_dev->cam.npkt; |
| 694 | if (npkt == 0) |
| 695 | npkt = 32; /* default value */ |
Jean-Francois Moine | 0be0100 | 2008-09-04 07:01:50 -0300 | [diff] [blame] | 696 | bsize = psize * npkt; |
| 697 | PDEBUG(D_STREAM, |
| 698 | "isoc %d pkts size %d = bsize:%d", |
| 699 | npkt, psize, bsize); |
Jean-Francois Moine | 51977a8 | 2008-09-08 03:22:42 -0300 | [diff] [blame] | 700 | nurbs = DEF_NURBS; |
Jean-Francois Moine | 95d9142 | 2008-09-29 05:57:32 -0300 | [diff] [blame] | 701 | } else { /* bulk */ |
Jean-Francois Moine | 0be0100 | 2008-09-04 07:01:50 -0300 | [diff] [blame] | 702 | npkt = 0; |
Antonio Ospite | dff369a | 2008-11-14 07:53:32 -0300 | [diff] [blame] | 703 | bsize = gspca_dev->cam.bulk_size; |
Jean-Francois Moine | 95d9142 | 2008-09-29 05:57:32 -0300 | [diff] [blame] | 704 | if (bsize == 0) |
| 705 | bsize = psize; |
Jean-Francois Moine | 0be0100 | 2008-09-04 07:01:50 -0300 | [diff] [blame] | 706 | PDEBUG(D_STREAM, "bulk bsize:%d", bsize); |
Antonio Ospite | dff369a | 2008-11-14 07:53:32 -0300 | [diff] [blame] | 707 | if (gspca_dev->cam.bulk_nurbs != 0) |
| 708 | nurbs = gspca_dev->cam.bulk_nurbs; |
| 709 | else |
| 710 | nurbs = 1; |
Jean-Francois Moine | 0be0100 | 2008-09-04 07:01:50 -0300 | [diff] [blame] | 711 | } |
| 712 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 713 | for (n = 0; n < nurbs; n++) { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 714 | urb = usb_alloc_urb(npkt, GFP_KERNEL); |
| 715 | if (!urb) { |
| 716 | err("usb_alloc_urb failed"); |
| 717 | return -ENOMEM; |
| 718 | } |
Jean-Francois Moine | db87087 | 2009-12-15 05:16:04 -0300 | [diff] [blame] | 719 | gspca_dev->urb[n] = urb; |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 720 | urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 721 | bsize, |
| 722 | GFP_KERNEL, |
| 723 | &urb->transfer_dma); |
| 724 | |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 725 | if (urb->transfer_buffer == NULL) { |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 726 | err("usb_alloc_coherent failed"); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 727 | return -ENOMEM; |
| 728 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 729 | urb->dev = gspca_dev->dev; |
| 730 | urb->context = gspca_dev; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 731 | urb->transfer_buffer_length = bsize; |
Jean-Francois Moine | 0be0100 | 2008-09-04 07:01:50 -0300 | [diff] [blame] | 732 | if (npkt != 0) { /* ISOC */ |
| 733 | urb->pipe = usb_rcvisocpipe(gspca_dev->dev, |
| 734 | ep->desc.bEndpointAddress); |
Frank Zago | 6c49da7 | 2008-09-28 07:43:00 -0300 | [diff] [blame] | 735 | urb->transfer_flags = URB_ISO_ASAP |
| 736 | | URB_NO_TRANSFER_DMA_MAP; |
Jean-Francois Moine | 0be0100 | 2008-09-04 07:01:50 -0300 | [diff] [blame] | 737 | urb->interval = ep->desc.bInterval; |
| 738 | urb->complete = isoc_irq; |
| 739 | urb->number_of_packets = npkt; |
| 740 | for (i = 0; i < npkt; i++) { |
| 741 | urb->iso_frame_desc[i].length = psize; |
| 742 | urb->iso_frame_desc[i].offset = psize * i; |
| 743 | } |
| 744 | } else { /* bulk */ |
| 745 | urb->pipe = usb_rcvbulkpipe(gspca_dev->dev, |
| 746 | ep->desc.bEndpointAddress), |
Frank Zago | 6c49da7 | 2008-09-28 07:43:00 -0300 | [diff] [blame] | 747 | urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; |
Jean-Francois Moine | 0be0100 | 2008-09-04 07:01:50 -0300 | [diff] [blame] | 748 | urb->complete = bulk_irq; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | return 0; |
| 752 | } |
| 753 | |
| 754 | /* |
| 755 | * start the USB transfer |
| 756 | */ |
| 757 | static int gspca_init_transfer(struct gspca_dev *gspca_dev) |
| 758 | { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 759 | struct usb_host_endpoint *ep; |
Jean-Francois Moine | db87087 | 2009-12-15 05:16:04 -0300 | [diff] [blame] | 760 | struct urb *urb; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 761 | int n, ret; |
| 762 | |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 763 | if (mutex_lock_interruptible(&gspca_dev->usb_lock)) |
| 764 | return -ERESTARTSYS; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 765 | |
Jean-Francois Moine | 090264e | 2009-02-12 08:05:45 -0300 | [diff] [blame] | 766 | if (!gspca_dev->present) { |
| 767 | ret = -ENODEV; |
| 768 | goto out; |
| 769 | } |
| 770 | |
Jean-François Moine | f7059ea | 2010-07-06 04:32:27 -0300 | [diff] [blame] | 771 | /* reset the streaming variables */ |
| 772 | gspca_dev->image = NULL; |
| 773 | gspca_dev->image_len = 0; |
| 774 | gspca_dev->last_packet_type = DISCARD_PACKET; |
| 775 | gspca_dev->sequence = 0; |
| 776 | |
Jean-Francois Moine | ab6c8cf | 2010-01-13 15:34:27 -0300 | [diff] [blame] | 777 | gspca_dev->usb_err = 0; |
| 778 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 779 | /* set the higher alternate setting and |
| 780 | * loop until urb submit succeeds */ |
Hans de Goede | a511ba9 | 2009-10-16 07:13:07 -0300 | [diff] [blame] | 781 | if (gspca_dev->cam.reverse_alts) |
| 782 | gspca_dev->alt = 0; |
| 783 | else |
| 784 | gspca_dev->alt = gspca_dev->nbalt; |
| 785 | |
Jean-Francois Moine | 1f53b0b | 2009-06-30 07:07:01 -0300 | [diff] [blame] | 786 | if (gspca_dev->sd_desc->isoc_init) { |
| 787 | ret = gspca_dev->sd_desc->isoc_init(gspca_dev); |
| 788 | if (ret < 0) |
| 789 | goto out; |
| 790 | } |
| 791 | ep = get_ep(gspca_dev); |
| 792 | if (ep == NULL) { |
| 793 | ret = -EIO; |
| 794 | goto out; |
| 795 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 796 | for (;;) { |
Jean-Francois Moine | 47aaca9 | 2009-12-15 05:23:04 -0300 | [diff] [blame] | 797 | if (!gspca_dev->cam.no_urb_create) { |
| 798 | PDEBUG(D_STREAM, "init transfer alt %d", |
| 799 | gspca_dev->alt); |
| 800 | ret = create_urbs(gspca_dev, ep); |
| 801 | if (ret < 0) { |
| 802 | destroy_urbs(gspca_dev); |
| 803 | goto out; |
| 804 | } |
Jean-Francois Moine | db87087 | 2009-12-15 05:16:04 -0300 | [diff] [blame] | 805 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 806 | |
Jean-Francois Moine | 8fe2f1d | 2008-10-22 14:51:56 -0300 | [diff] [blame] | 807 | /* clear the bulk endpoint */ |
Jean-Francois Moine | 6929dc6 | 2009-04-21 13:45:56 -0300 | [diff] [blame] | 808 | if (gspca_dev->cam.bulk) |
Jean-Francois Moine | 8fe2f1d | 2008-10-22 14:51:56 -0300 | [diff] [blame] | 809 | usb_clear_halt(gspca_dev->dev, |
Jean-Francois Moine | 50e06de | 2008-12-31 08:13:46 -0300 | [diff] [blame] | 810 | gspca_dev->urb[0]->pipe); |
Jean-Francois Moine | 8fe2f1d | 2008-10-22 14:51:56 -0300 | [diff] [blame] | 811 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 812 | /* start the cam */ |
Jean-Francois Moine | 72ab97c | 2008-09-20 06:39:08 -0300 | [diff] [blame] | 813 | ret = gspca_dev->sd_desc->start(gspca_dev); |
| 814 | if (ret < 0) { |
| 815 | destroy_urbs(gspca_dev); |
| 816 | goto out; |
| 817 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 818 | gspca_dev->streaming = 1; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 819 | |
Antonio Ospite | dff369a | 2008-11-14 07:53:32 -0300 | [diff] [blame] | 820 | /* some bulk transfers are started by the subdriver */ |
Jean-Francois Moine | 6929dc6 | 2009-04-21 13:45:56 -0300 | [diff] [blame] | 821 | if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0) |
Jean-Francois Moine | 51977a8 | 2008-09-08 03:22:42 -0300 | [diff] [blame] | 822 | break; |
| 823 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 824 | /* submit the URBs */ |
Jean-Francois Moine | db87087 | 2009-12-15 05:16:04 -0300 | [diff] [blame] | 825 | for (n = 0; n < MAX_NURBS; n++) { |
| 826 | urb = gspca_dev->urb[n]; |
| 827 | if (urb == NULL) |
| 828 | break; |
| 829 | ret = usb_submit_urb(urb, GFP_KERNEL); |
Jean-Francois Moine | 1f53b0b | 2009-06-30 07:07:01 -0300 | [diff] [blame] | 830 | if (ret < 0) |
| 831 | break; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 832 | } |
| 833 | if (ret >= 0) |
| 834 | break; |
Jean-François Moine | e4dac28 | 2010-04-09 06:11:36 -0300 | [diff] [blame] | 835 | gspca_stream_off(gspca_dev); |
Jean-Francois Moine | 4bdf4a8 | 2009-11-10 14:49:43 -0300 | [diff] [blame] | 836 | if (ret != -ENOSPC) { |
| 837 | PDEBUG(D_ERR|D_STREAM, |
| 838 | "usb_submit_urb alt %d err %d", |
| 839 | gspca_dev->alt, ret); |
Jean-Francois Moine | 1f53b0b | 2009-06-30 07:07:01 -0300 | [diff] [blame] | 840 | goto out; |
Jean-Francois Moine | 4bdf4a8 | 2009-11-10 14:49:43 -0300 | [diff] [blame] | 841 | } |
Jean-Francois Moine | 1f53b0b | 2009-06-30 07:07:01 -0300 | [diff] [blame] | 842 | |
| 843 | /* the bandwidth is not wide enough |
| 844 | * negociate or try a lower alternate setting */ |
Jean-Francois Moine | 4bdf4a8 | 2009-11-10 14:49:43 -0300 | [diff] [blame] | 845 | PDEBUG(D_ERR|D_STREAM, |
| 846 | "bandwidth not wide enough - trying again"); |
Jean-Francois Moine | 1f53b0b | 2009-06-30 07:07:01 -0300 | [diff] [blame] | 847 | msleep(20); /* wait for kill complete */ |
| 848 | if (gspca_dev->sd_desc->isoc_nego) { |
| 849 | ret = gspca_dev->sd_desc->isoc_nego(gspca_dev); |
| 850 | if (ret < 0) |
| 851 | goto out; |
| 852 | } else { |
| 853 | ep = get_ep(gspca_dev); |
| 854 | if (ep == NULL) { |
| 855 | ret = -EIO; |
| 856 | goto out; |
| 857 | } |
| 858 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 859 | } |
| 860 | out: |
| 861 | mutex_unlock(&gspca_dev->usb_lock); |
| 862 | return ret; |
| 863 | } |
| 864 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 865 | static void gspca_set_default_mode(struct gspca_dev *gspca_dev) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 866 | { |
| 867 | int i; |
| 868 | |
| 869 | i = gspca_dev->cam.nmodes - 1; /* take the highest mode */ |
| 870 | gspca_dev->curr_mode = i; |
| 871 | gspca_dev->width = gspca_dev->cam.cam_mode[i].width; |
| 872 | gspca_dev->height = gspca_dev->cam.cam_mode[i].height; |
Jean-Francois Moine | c2446b3 | 2008-07-05 11:49:20 -0300 | [diff] [blame] | 873 | gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | static int wxh_to_mode(struct gspca_dev *gspca_dev, |
| 877 | int width, int height) |
| 878 | { |
| 879 | int i; |
| 880 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 881 | for (i = gspca_dev->cam.nmodes; --i > 0; ) { |
| 882 | if (width >= gspca_dev->cam.cam_mode[i].width |
| 883 | && height >= gspca_dev->cam.cam_mode[i].height) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 884 | break; |
| 885 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 886 | return i; |
| 887 | } |
| 888 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 889 | /* |
| 890 | * search a mode with the right pixel format |
| 891 | */ |
| 892 | static int gspca_get_mode(struct gspca_dev *gspca_dev, |
| 893 | int mode, |
| 894 | int pixfmt) |
| 895 | { |
| 896 | int modeU, modeD; |
| 897 | |
| 898 | modeU = modeD = mode; |
| 899 | while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) { |
| 900 | if (--modeD >= 0) { |
Jean-Francois Moine | c2446b3 | 2008-07-05 11:49:20 -0300 | [diff] [blame] | 901 | if (gspca_dev->cam.cam_mode[modeD].pixelformat |
| 902 | == pixfmt) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 903 | return modeD; |
| 904 | } |
| 905 | if (++modeU < gspca_dev->cam.nmodes) { |
Jean-Francois Moine | c2446b3 | 2008-07-05 11:49:20 -0300 | [diff] [blame] | 906 | if (gspca_dev->cam.cam_mode[modeU].pixelformat |
| 907 | == pixfmt) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 908 | return modeU; |
| 909 | } |
| 910 | } |
| 911 | return -EINVAL; |
| 912 | } |
| 913 | |
Brian Johnson | af1d9af | 2009-07-19 05:29:20 -0300 | [diff] [blame] | 914 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 915 | static int vidioc_g_register(struct file *file, void *priv, |
| 916 | struct v4l2_dbg_register *reg) |
| 917 | { |
| 918 | int ret; |
| 919 | struct gspca_dev *gspca_dev = priv; |
| 920 | |
| 921 | if (!gspca_dev->sd_desc->get_chip_ident) |
| 922 | return -EINVAL; |
| 923 | |
| 924 | if (!gspca_dev->sd_desc->get_register) |
| 925 | return -EINVAL; |
| 926 | |
| 927 | if (mutex_lock_interruptible(&gspca_dev->usb_lock)) |
| 928 | return -ERESTARTSYS; |
Jean-Francois Moine | 8c4ebae | 2009-12-02 07:14:33 -0300 | [diff] [blame] | 929 | gspca_dev->usb_err = 0; |
Brian Johnson | af1d9af | 2009-07-19 05:29:20 -0300 | [diff] [blame] | 930 | if (gspca_dev->present) |
| 931 | ret = gspca_dev->sd_desc->get_register(gspca_dev, reg); |
| 932 | else |
| 933 | ret = -ENODEV; |
| 934 | mutex_unlock(&gspca_dev->usb_lock); |
| 935 | |
| 936 | return ret; |
| 937 | } |
| 938 | |
| 939 | static int vidioc_s_register(struct file *file, void *priv, |
| 940 | struct v4l2_dbg_register *reg) |
| 941 | { |
| 942 | int ret; |
| 943 | struct gspca_dev *gspca_dev = priv; |
| 944 | |
| 945 | if (!gspca_dev->sd_desc->get_chip_ident) |
| 946 | return -EINVAL; |
| 947 | |
| 948 | if (!gspca_dev->sd_desc->set_register) |
| 949 | return -EINVAL; |
| 950 | |
| 951 | if (mutex_lock_interruptible(&gspca_dev->usb_lock)) |
| 952 | return -ERESTARTSYS; |
Jean-Francois Moine | 8c4ebae | 2009-12-02 07:14:33 -0300 | [diff] [blame] | 953 | gspca_dev->usb_err = 0; |
Brian Johnson | af1d9af | 2009-07-19 05:29:20 -0300 | [diff] [blame] | 954 | if (gspca_dev->present) |
| 955 | ret = gspca_dev->sd_desc->set_register(gspca_dev, reg); |
| 956 | else |
| 957 | ret = -ENODEV; |
| 958 | mutex_unlock(&gspca_dev->usb_lock); |
| 959 | |
| 960 | return ret; |
| 961 | } |
| 962 | #endif |
| 963 | |
| 964 | static int vidioc_g_chip_ident(struct file *file, void *priv, |
| 965 | struct v4l2_dbg_chip_ident *chip) |
| 966 | { |
| 967 | int ret; |
| 968 | struct gspca_dev *gspca_dev = priv; |
| 969 | |
| 970 | if (!gspca_dev->sd_desc->get_chip_ident) |
| 971 | return -EINVAL; |
| 972 | |
| 973 | if (mutex_lock_interruptible(&gspca_dev->usb_lock)) |
| 974 | return -ERESTARTSYS; |
Jean-Francois Moine | 8c4ebae | 2009-12-02 07:14:33 -0300 | [diff] [blame] | 975 | gspca_dev->usb_err = 0; |
Brian Johnson | af1d9af | 2009-07-19 05:29:20 -0300 | [diff] [blame] | 976 | if (gspca_dev->present) |
| 977 | ret = gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip); |
| 978 | else |
| 979 | ret = -ENODEV; |
| 980 | mutex_unlock(&gspca_dev->usb_lock); |
| 981 | |
| 982 | return ret; |
| 983 | } |
| 984 | |
Mauro Carvalho Chehab | e078770 | 2008-07-01 04:06:22 -0300 | [diff] [blame] | 985 | static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 986 | struct v4l2_fmtdesc *fmtdesc) |
| 987 | { |
| 988 | struct gspca_dev *gspca_dev = priv; |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 989 | int i, j, index; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 990 | __u32 fmt_tb[8]; |
| 991 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 992 | /* give an index to each format */ |
| 993 | index = 0; |
| 994 | j = 0; |
| 995 | for (i = gspca_dev->cam.nmodes; --i >= 0; ) { |
Jean-Francois Moine | c2446b3 | 2008-07-05 11:49:20 -0300 | [diff] [blame] | 996 | fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 997 | j = 0; |
| 998 | for (;;) { |
| 999 | if (fmt_tb[j] == fmt_tb[index]) |
| 1000 | break; |
| 1001 | j++; |
| 1002 | } |
| 1003 | if (j == index) { |
| 1004 | if (fmtdesc->index == index) |
| 1005 | break; /* new format */ |
| 1006 | index++; |
Julia Lawall | 8029712 | 2008-11-12 23:18:21 -0300 | [diff] [blame] | 1007 | if (index >= ARRAY_SIZE(fmt_tb)) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1008 | return -EINVAL; |
| 1009 | } |
| 1010 | } |
| 1011 | if (i < 0) |
| 1012 | return -EINVAL; /* no more format */ |
| 1013 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1014 | fmtdesc->pixelformat = fmt_tb[index]; |
| 1015 | if (gspca_is_compressed(fmt_tb[index])) |
| 1016 | fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED; |
| 1017 | fmtdesc->description[0] = fmtdesc->pixelformat & 0xff; |
| 1018 | fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff; |
| 1019 | fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff; |
| 1020 | fmtdesc->description[3] = fmtdesc->pixelformat >> 24; |
| 1021 | fmtdesc->description[4] = '\0'; |
| 1022 | return 0; |
| 1023 | } |
| 1024 | |
Mauro Carvalho Chehab | e078770 | 2008-07-01 04:06:22 -0300 | [diff] [blame] | 1025 | static int vidioc_g_fmt_vid_cap(struct file *file, void *priv, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1026 | struct v4l2_format *fmt) |
| 1027 | { |
| 1028 | struct gspca_dev *gspca_dev = priv; |
Jean-Francois Moine | c2446b3 | 2008-07-05 11:49:20 -0300 | [diff] [blame] | 1029 | int mode; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1030 | |
Jean-Francois Moine | c2446b3 | 2008-07-05 11:49:20 -0300 | [diff] [blame] | 1031 | mode = gspca_dev->curr_mode; |
| 1032 | memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode], |
| 1033 | sizeof fmt->fmt.pix); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1034 | return 0; |
| 1035 | } |
| 1036 | |
Mauro Carvalho Chehab | e078770 | 2008-07-01 04:06:22 -0300 | [diff] [blame] | 1037 | static int try_fmt_vid_cap(struct gspca_dev *gspca_dev, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1038 | struct v4l2_format *fmt) |
| 1039 | { |
Hans de Goede | 24c530b | 2008-07-04 17:55:18 -0300 | [diff] [blame] | 1040 | int w, h, mode, mode2; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1041 | |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1042 | w = fmt->fmt.pix.width; |
| 1043 | h = fmt->fmt.pix.height; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1044 | |
Jean-Francois Moine | 335b3f8 | 2008-07-30 04:53:02 -0300 | [diff] [blame] | 1045 | #ifdef GSPCA_DEBUG |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1046 | if (gspca_debug & D_CONF) |
| 1047 | PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h); |
| 1048 | #endif |
| 1049 | /* search the closest mode for width and height */ |
| 1050 | mode = wxh_to_mode(gspca_dev, w, h); |
| 1051 | |
| 1052 | /* OK if right palette */ |
Jean-Francois Moine | c2446b3 | 2008-07-05 11:49:20 -0300 | [diff] [blame] | 1053 | if (gspca_dev->cam.cam_mode[mode].pixelformat |
| 1054 | != fmt->fmt.pix.pixelformat) { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1055 | |
| 1056 | /* else, search the closest mode with the same pixel format */ |
| 1057 | mode2 = gspca_get_mode(gspca_dev, mode, |
| 1058 | fmt->fmt.pix.pixelformat); |
Jean-Francois Moine | c2446b3 | 2008-07-05 11:49:20 -0300 | [diff] [blame] | 1059 | if (mode2 >= 0) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1060 | mode = mode2; |
Jean-Francois Moine | c2446b3 | 2008-07-05 11:49:20 -0300 | [diff] [blame] | 1061 | /* else |
| 1062 | ; * no chance, return this mode */ |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1063 | } |
Jean-Francois Moine | c2446b3 | 2008-07-05 11:49:20 -0300 | [diff] [blame] | 1064 | memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode], |
| 1065 | sizeof fmt->fmt.pix); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1066 | return mode; /* used when s_fmt */ |
| 1067 | } |
| 1068 | |
Mauro Carvalho Chehab | e078770 | 2008-07-01 04:06:22 -0300 | [diff] [blame] | 1069 | static int vidioc_try_fmt_vid_cap(struct file *file, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1070 | void *priv, |
| 1071 | struct v4l2_format *fmt) |
| 1072 | { |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1073 | struct gspca_dev *gspca_dev = priv; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1074 | int ret; |
| 1075 | |
Mauro Carvalho Chehab | e078770 | 2008-07-01 04:06:22 -0300 | [diff] [blame] | 1076 | ret = try_fmt_vid_cap(gspca_dev, fmt); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1077 | if (ret < 0) |
| 1078 | return ret; |
| 1079 | return 0; |
| 1080 | } |
| 1081 | |
Mauro Carvalho Chehab | e078770 | 2008-07-01 04:06:22 -0300 | [diff] [blame] | 1082 | static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1083 | struct v4l2_format *fmt) |
| 1084 | { |
| 1085 | struct gspca_dev *gspca_dev = priv; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1086 | int ret; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1087 | |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1088 | if (mutex_lock_interruptible(&gspca_dev->queue_lock)) |
| 1089 | return -ERESTARTSYS; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1090 | |
Mauro Carvalho Chehab | e078770 | 2008-07-01 04:06:22 -0300 | [diff] [blame] | 1091 | ret = try_fmt_vid_cap(gspca_dev, fmt); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1092 | if (ret < 0) |
| 1093 | goto out; |
| 1094 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1095 | if (gspca_dev->nframes != 0 |
| 1096 | && fmt->fmt.pix.sizeimage > gspca_dev->frsz) { |
| 1097 | ret = -EINVAL; |
| 1098 | goto out; |
| 1099 | } |
| 1100 | |
Jean-Francois Moine | 50a871f | 2008-06-30 19:47:33 -0300 | [diff] [blame] | 1101 | if (ret == gspca_dev->curr_mode) { |
| 1102 | ret = 0; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1103 | goto out; /* same mode */ |
Jean-Francois Moine | 50a871f | 2008-06-30 19:47:33 -0300 | [diff] [blame] | 1104 | } |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1105 | |
| 1106 | if (gspca_dev->streaming) { |
| 1107 | ret = -EBUSY; |
| 1108 | goto out; |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1109 | } |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1110 | gspca_dev->width = fmt->fmt.pix.width; |
| 1111 | gspca_dev->height = fmt->fmt.pix.height; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1112 | gspca_dev->pixfmt = fmt->fmt.pix.pixelformat; |
| 1113 | gspca_dev->curr_mode = ret; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1114 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1115 | ret = 0; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1116 | out: |
| 1117 | mutex_unlock(&gspca_dev->queue_lock); |
| 1118 | return ret; |
| 1119 | } |
| 1120 | |
Hans de Goede | 11c635a | 2009-05-25 15:04:22 -0300 | [diff] [blame] | 1121 | static int vidioc_enum_framesizes(struct file *file, void *priv, |
| 1122 | struct v4l2_frmsizeenum *fsize) |
| 1123 | { |
| 1124 | struct gspca_dev *gspca_dev = priv; |
| 1125 | int i; |
| 1126 | __u32 index = 0; |
| 1127 | |
| 1128 | for (i = 0; i < gspca_dev->cam.nmodes; i++) { |
| 1129 | if (fsize->pixel_format != |
| 1130 | gspca_dev->cam.cam_mode[i].pixelformat) |
| 1131 | continue; |
| 1132 | |
| 1133 | if (fsize->index == index) { |
| 1134 | fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE; |
| 1135 | fsize->discrete.width = |
| 1136 | gspca_dev->cam.cam_mode[i].width; |
| 1137 | fsize->discrete.height = |
| 1138 | gspca_dev->cam.cam_mode[i].height; |
| 1139 | return 0; |
| 1140 | } |
| 1141 | index++; |
| 1142 | } |
| 1143 | |
| 1144 | return -EINVAL; |
| 1145 | } |
| 1146 | |
Antonio Ospite | 28ffe77 | 2009-12-02 06:18:46 -0300 | [diff] [blame] | 1147 | static int vidioc_enum_frameintervals(struct file *filp, void *priv, |
| 1148 | struct v4l2_frmivalenum *fival) |
| 1149 | { |
| 1150 | struct gspca_dev *gspca_dev = priv; |
| 1151 | int mode = wxh_to_mode(gspca_dev, fival->width, fival->height); |
| 1152 | __u32 i; |
| 1153 | |
| 1154 | if (gspca_dev->cam.mode_framerates == NULL || |
| 1155 | gspca_dev->cam.mode_framerates[mode].nrates == 0) |
| 1156 | return -EINVAL; |
| 1157 | |
| 1158 | if (fival->pixel_format != |
| 1159 | gspca_dev->cam.cam_mode[mode].pixelformat) |
| 1160 | return -EINVAL; |
| 1161 | |
| 1162 | for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) { |
| 1163 | if (fival->index == i) { |
| 1164 | fival->type = V4L2_FRMSIZE_TYPE_DISCRETE; |
| 1165 | fival->discrete.numerator = 1; |
| 1166 | fival->discrete.denominator = |
| 1167 | gspca_dev->cam.mode_framerates[mode].rates[i]; |
| 1168 | return 0; |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | return -EINVAL; |
| 1173 | } |
| 1174 | |
Hans de Goede | d018230 | 2008-11-19 17:10:47 -0300 | [diff] [blame] | 1175 | static void gspca_release(struct video_device *vfd) |
Frank Zago | 6b060ff | 2008-09-28 08:12:22 -0300 | [diff] [blame] | 1176 | { |
Hans de Goede | d018230 | 2008-11-19 17:10:47 -0300 | [diff] [blame] | 1177 | struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev); |
Frank Zago | 6b060ff | 2008-09-28 08:12:22 -0300 | [diff] [blame] | 1178 | |
Laurent Pinchart | 38c7c03 | 2009-11-27 13:57:15 -0300 | [diff] [blame] | 1179 | PDEBUG(D_PROBE, "%s released", |
| 1180 | video_device_node_name(&gspca_dev->vdev)); |
Frank Zago | 6b060ff | 2008-09-28 08:12:22 -0300 | [diff] [blame] | 1181 | |
| 1182 | kfree(gspca_dev->usb_buf); |
| 1183 | kfree(gspca_dev); |
| 1184 | } |
| 1185 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 1186 | static int dev_open(struct file *file) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1187 | { |
| 1188 | struct gspca_dev *gspca_dev; |
| 1189 | int ret; |
| 1190 | |
Jean-Francois Moine | 0d07e24 | 2009-10-01 13:44:49 -0300 | [diff] [blame] | 1191 | PDEBUG(D_STREAM, "[%s] open", current->comm); |
Jean-Francois Moine | a12ca6a | 2008-11-19 06:37:53 -0300 | [diff] [blame] | 1192 | gspca_dev = (struct gspca_dev *) video_devdata(file); |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1193 | if (mutex_lock_interruptible(&gspca_dev->queue_lock)) |
| 1194 | return -ERESTARTSYS; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1195 | if (!gspca_dev->present) { |
| 1196 | ret = -ENODEV; |
| 1197 | goto out; |
| 1198 | } |
| 1199 | |
Jean-Francois Moine | 012d6b0 | 2008-09-03 17:12:16 -0300 | [diff] [blame] | 1200 | if (gspca_dev->users > 4) { /* (arbitrary value) */ |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1201 | ret = -EBUSY; |
| 1202 | goto out; |
| 1203 | } |
Jean-Francois Moine | 5c4fa00 | 2008-11-18 15:52:31 -0300 | [diff] [blame] | 1204 | |
| 1205 | /* protect the subdriver against rmmod */ |
| 1206 | if (!try_module_get(gspca_dev->module)) { |
| 1207 | ret = -ENODEV; |
| 1208 | goto out; |
| 1209 | } |
| 1210 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1211 | gspca_dev->users++; |
Frank Zago | 6b060ff | 2008-09-28 08:12:22 -0300 | [diff] [blame] | 1212 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1213 | file->private_data = gspca_dev; |
Jean-Francois Moine | 335b3f8 | 2008-07-30 04:53:02 -0300 | [diff] [blame] | 1214 | #ifdef GSPCA_DEBUG |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1215 | /* activate the v4l2 debug */ |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1216 | if (gspca_debug & D_V4L2) |
Jean-Francois Moine | a12ca6a | 2008-11-19 06:37:53 -0300 | [diff] [blame] | 1217 | gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL |
Frank Zago | 9de436c | 2008-09-15 05:20:38 -0300 | [diff] [blame] | 1218 | | V4L2_DEBUG_IOCTL_ARG; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1219 | else |
Jean-Francois Moine | a12ca6a | 2008-11-19 06:37:53 -0300 | [diff] [blame] | 1220 | gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL |
Frank Zago | 9de436c | 2008-09-15 05:20:38 -0300 | [diff] [blame] | 1221 | | V4L2_DEBUG_IOCTL_ARG); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1222 | #endif |
Jean-Francois Moine | 012d6b0 | 2008-09-03 17:12:16 -0300 | [diff] [blame] | 1223 | ret = 0; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1224 | out: |
| 1225 | mutex_unlock(&gspca_dev->queue_lock); |
| 1226 | if (ret != 0) |
| 1227 | PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret); |
| 1228 | else |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1229 | PDEBUG(D_STREAM, "open done"); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1230 | return ret; |
| 1231 | } |
| 1232 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 1233 | static int dev_close(struct file *file) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1234 | { |
| 1235 | struct gspca_dev *gspca_dev = file->private_data; |
| 1236 | |
Jean-Francois Moine | 0d07e24 | 2009-10-01 13:44:49 -0300 | [diff] [blame] | 1237 | PDEBUG(D_STREAM, "[%s] close", current->comm); |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1238 | if (mutex_lock_interruptible(&gspca_dev->queue_lock)) |
| 1239 | return -ERESTARTSYS; |
| 1240 | gspca_dev->users--; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1241 | |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1242 | /* if the file did the capture, free the streaming resources */ |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1243 | if (gspca_dev->capt_file == file) { |
Jean-Francois Moine | a1e8288 | 2008-09-03 17:12:17 -0300 | [diff] [blame] | 1244 | if (gspca_dev->streaming) { |
| 1245 | mutex_lock(&gspca_dev->usb_lock); |
Jean-Francois Moine | 8c4ebae | 2009-12-02 07:14:33 -0300 | [diff] [blame] | 1246 | gspca_dev->usb_err = 0; |
Jean-Francois Moine | a1e8288 | 2008-09-03 17:12:17 -0300 | [diff] [blame] | 1247 | gspca_stream_off(gspca_dev); |
| 1248 | mutex_unlock(&gspca_dev->usb_lock); |
| 1249 | } |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1250 | frame_free(gspca_dev); |
Harvey Harrison | efab821 | 2008-07-05 06:12:47 -0300 | [diff] [blame] | 1251 | gspca_dev->capt_file = NULL; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1252 | gspca_dev->memory = GSPCA_MEMORY_NO; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1253 | } |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1254 | file->private_data = NULL; |
Jean-Francois Moine | 5c4fa00 | 2008-11-18 15:52:31 -0300 | [diff] [blame] | 1255 | module_put(gspca_dev->module); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1256 | mutex_unlock(&gspca_dev->queue_lock); |
Frank Zago | 6b060ff | 2008-09-28 08:12:22 -0300 | [diff] [blame] | 1257 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1258 | PDEBUG(D_STREAM, "close done"); |
Frank Zago | 6b060ff | 2008-09-28 08:12:22 -0300 | [diff] [blame] | 1259 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1260 | return 0; |
| 1261 | } |
| 1262 | |
| 1263 | static int vidioc_querycap(struct file *file, void *priv, |
| 1264 | struct v4l2_capability *cap) |
| 1265 | { |
| 1266 | struct gspca_dev *gspca_dev = priv; |
Jean-Francois Moine | 090264e | 2009-02-12 08:05:45 -0300 | [diff] [blame] | 1267 | int ret; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1268 | |
Jean-Francois Moine | 090264e | 2009-02-12 08:05:45 -0300 | [diff] [blame] | 1269 | /* protect the access to the usb device */ |
| 1270 | if (mutex_lock_interruptible(&gspca_dev->usb_lock)) |
| 1271 | return -ERESTARTSYS; |
| 1272 | if (!gspca_dev->present) { |
| 1273 | ret = -ENODEV; |
| 1274 | goto out; |
| 1275 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1276 | strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver); |
Jean-Francois Moine | c6edaf1 | 2008-07-23 03:24:06 -0300 | [diff] [blame] | 1277 | if (gspca_dev->dev->product != NULL) { |
| 1278 | strncpy(cap->card, gspca_dev->dev->product, |
| 1279 | sizeof cap->card); |
| 1280 | } else { |
| 1281 | snprintf(cap->card, sizeof cap->card, |
| 1282 | "USB Camera (%04x:%04x)", |
| 1283 | le16_to_cpu(gspca_dev->dev->descriptor.idVendor), |
| 1284 | le16_to_cpu(gspca_dev->dev->descriptor.idProduct)); |
| 1285 | } |
Thierry MERLE | 5932028 | 2009-01-21 15:32:26 -0300 | [diff] [blame] | 1286 | usb_make_path(gspca_dev->dev, cap->bus_info, sizeof(cap->bus_info)); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1287 | cap->version = DRIVER_VERSION_NUMBER; |
| 1288 | cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
| 1289 | | V4L2_CAP_STREAMING |
| 1290 | | V4L2_CAP_READWRITE; |
Jean-Francois Moine | 090264e | 2009-02-12 08:05:45 -0300 | [diff] [blame] | 1291 | ret = 0; |
| 1292 | out: |
| 1293 | mutex_unlock(&gspca_dev->usb_lock); |
| 1294 | return ret; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1295 | } |
| 1296 | |
Hans de Goede | a0001a2 | 2009-06-10 04:52:18 -0300 | [diff] [blame] | 1297 | static const struct ctrl *get_ctrl(struct gspca_dev *gspca_dev, |
| 1298 | int id) |
| 1299 | { |
| 1300 | const struct ctrl *ctrls; |
| 1301 | int i; |
| 1302 | |
| 1303 | for (i = 0, ctrls = gspca_dev->sd_desc->ctrls; |
| 1304 | i < gspca_dev->sd_desc->nctrls; |
| 1305 | i++, ctrls++) { |
| 1306 | if (gspca_dev->ctrl_dis & (1 << i)) |
| 1307 | continue; |
| 1308 | if (id == ctrls->qctrl.id) |
| 1309 | return ctrls; |
| 1310 | } |
| 1311 | return NULL; |
| 1312 | } |
| 1313 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1314 | static int vidioc_queryctrl(struct file *file, void *priv, |
| 1315 | struct v4l2_queryctrl *q_ctrl) |
| 1316 | { |
| 1317 | struct gspca_dev *gspca_dev = priv; |
Hans de Goede | a0001a2 | 2009-06-10 04:52:18 -0300 | [diff] [blame] | 1318 | const struct ctrl *ctrls; |
| 1319 | int i; |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 1320 | u32 id; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1321 | |
Hans de Goede | a0001a2 | 2009-06-10 04:52:18 -0300 | [diff] [blame] | 1322 | ctrls = NULL; |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 1323 | id = q_ctrl->id; |
| 1324 | if (id & V4L2_CTRL_FLAG_NEXT_CTRL) { |
| 1325 | id &= V4L2_CTRL_ID_MASK; |
| 1326 | id++; |
| 1327 | for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) { |
Hans de Goede | a0001a2 | 2009-06-10 04:52:18 -0300 | [diff] [blame] | 1328 | if (gspca_dev->ctrl_dis & (1 << i)) |
Jean-Francois Moine | f50ba1b | 2008-09-03 17:12:14 -0300 | [diff] [blame] | 1329 | continue; |
Hans de Goede | b8bfb5f | 2009-06-13 18:56:22 -0300 | [diff] [blame] | 1330 | if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id) |
Jean-Francois Moine | f50ba1b | 2008-09-03 17:12:14 -0300 | [diff] [blame] | 1331 | continue; |
Hans de Goede | b8bfb5f | 2009-06-13 18:56:22 -0300 | [diff] [blame] | 1332 | if (ctrls && gspca_dev->sd_desc->ctrls[i].qctrl.id |
Hans de Goede | a0001a2 | 2009-06-10 04:52:18 -0300 | [diff] [blame] | 1333 | > ctrls->qctrl.id) |
Hans de Goede | b8bfb5f | 2009-06-13 18:56:22 -0300 | [diff] [blame] | 1334 | continue; |
Hans de Goede | a0001a2 | 2009-06-10 04:52:18 -0300 | [diff] [blame] | 1335 | ctrls = &gspca_dev->sd_desc->ctrls[i]; |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 1336 | } |
Jean-Francois Moine | 8395555 | 2009-12-12 06:58:01 -0300 | [diff] [blame] | 1337 | if (ctrls == NULL) |
| 1338 | return -EINVAL; |
Hans de Goede | a0001a2 | 2009-06-10 04:52:18 -0300 | [diff] [blame] | 1339 | } else { |
| 1340 | ctrls = get_ctrl(gspca_dev, id); |
Jean-Francois Moine | 8395555 | 2009-12-12 06:58:01 -0300 | [diff] [blame] | 1341 | if (ctrls == NULL) |
| 1342 | return -EINVAL; |
Jean-Francois Moine | 4af8566 | 2009-11-12 15:59:27 -0300 | [diff] [blame] | 1343 | i = ctrls - gspca_dev->sd_desc->ctrls; |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 1344 | } |
Hans de Goede | a0001a2 | 2009-06-10 04:52:18 -0300 | [diff] [blame] | 1345 | memcpy(q_ctrl, ctrls, sizeof *q_ctrl); |
Jean-Francois Moine | 4af8566 | 2009-11-12 15:59:27 -0300 | [diff] [blame] | 1346 | if (gspca_dev->ctrl_inac & (1 << i)) |
| 1347 | q_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE; |
Jean-Francois Moine | f50ba1b | 2008-09-03 17:12:14 -0300 | [diff] [blame] | 1348 | return 0; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | static int vidioc_s_ctrl(struct file *file, void *priv, |
| 1352 | struct v4l2_control *ctrl) |
| 1353 | { |
| 1354 | struct gspca_dev *gspca_dev = priv; |
Jean-Francois Moine | a5ae206 | 2008-07-04 11:16:16 -0300 | [diff] [blame] | 1355 | const struct ctrl *ctrls; |
Hans de Goede | a0001a2 | 2009-06-10 04:52:18 -0300 | [diff] [blame] | 1356 | int ret; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1357 | |
Hans de Goede | a0001a2 | 2009-06-10 04:52:18 -0300 | [diff] [blame] | 1358 | ctrls = get_ctrl(gspca_dev, ctrl->id); |
| 1359 | if (ctrls == NULL) |
| 1360 | return -EINVAL; |
| 1361 | |
| 1362 | if (ctrl->value < ctrls->qctrl.minimum |
| 1363 | || ctrl->value > ctrls->qctrl.maximum) |
| 1364 | return -ERANGE; |
| 1365 | PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value); |
| 1366 | if (mutex_lock_interruptible(&gspca_dev->usb_lock)) |
| 1367 | return -ERESTARTSYS; |
Jean-Francois Moine | 8c4ebae | 2009-12-02 07:14:33 -0300 | [diff] [blame] | 1368 | gspca_dev->usb_err = 0; |
Hans de Goede | a0001a2 | 2009-06-10 04:52:18 -0300 | [diff] [blame] | 1369 | if (gspca_dev->present) |
| 1370 | ret = ctrls->set(gspca_dev, ctrl->value); |
| 1371 | else |
| 1372 | ret = -ENODEV; |
| 1373 | mutex_unlock(&gspca_dev->usb_lock); |
| 1374 | return ret; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1375 | } |
| 1376 | |
| 1377 | static int vidioc_g_ctrl(struct file *file, void *priv, |
| 1378 | struct v4l2_control *ctrl) |
| 1379 | { |
| 1380 | struct gspca_dev *gspca_dev = priv; |
Jean-Francois Moine | a5ae206 | 2008-07-04 11:16:16 -0300 | [diff] [blame] | 1381 | const struct ctrl *ctrls; |
Hans de Goede | a0001a2 | 2009-06-10 04:52:18 -0300 | [diff] [blame] | 1382 | int ret; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1383 | |
Hans de Goede | a0001a2 | 2009-06-10 04:52:18 -0300 | [diff] [blame] | 1384 | ctrls = get_ctrl(gspca_dev, ctrl->id); |
| 1385 | if (ctrls == NULL) |
| 1386 | return -EINVAL; |
| 1387 | |
| 1388 | if (mutex_lock_interruptible(&gspca_dev->usb_lock)) |
| 1389 | return -ERESTARTSYS; |
Jean-Francois Moine | 8c4ebae | 2009-12-02 07:14:33 -0300 | [diff] [blame] | 1390 | gspca_dev->usb_err = 0; |
Hans de Goede | a0001a2 | 2009-06-10 04:52:18 -0300 | [diff] [blame] | 1391 | if (gspca_dev->present) |
| 1392 | ret = ctrls->get(gspca_dev, &ctrl->value); |
| 1393 | else |
| 1394 | ret = -ENODEV; |
| 1395 | mutex_unlock(&gspca_dev->usb_lock); |
| 1396 | return ret; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | static int vidioc_querymenu(struct file *file, void *priv, |
| 1400 | struct v4l2_querymenu *qmenu) |
| 1401 | { |
| 1402 | struct gspca_dev *gspca_dev = priv; |
| 1403 | |
| 1404 | if (!gspca_dev->sd_desc->querymenu) |
| 1405 | return -EINVAL; |
| 1406 | return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu); |
| 1407 | } |
| 1408 | |
| 1409 | static int vidioc_enum_input(struct file *file, void *priv, |
| 1410 | struct v4l2_input *input) |
| 1411 | { |
| 1412 | struct gspca_dev *gspca_dev = priv; |
| 1413 | |
| 1414 | if (input->index != 0) |
| 1415 | return -EINVAL; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1416 | input->type = V4L2_INPUT_TYPE_CAMERA; |
Adam Baker | dfa76fa | 2009-03-29 19:17:10 -0300 | [diff] [blame] | 1417 | input->status = gspca_dev->cam.input_flags; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1418 | strncpy(input->name, gspca_dev->sd_desc->name, |
| 1419 | sizeof input->name); |
| 1420 | return 0; |
| 1421 | } |
| 1422 | |
| 1423 | static int vidioc_g_input(struct file *file, void *priv, unsigned int *i) |
| 1424 | { |
| 1425 | *i = 0; |
| 1426 | return 0; |
| 1427 | } |
| 1428 | |
| 1429 | static int vidioc_s_input(struct file *file, void *priv, unsigned int i) |
| 1430 | { |
| 1431 | if (i > 0) |
| 1432 | return -EINVAL; |
| 1433 | return (0); |
| 1434 | } |
| 1435 | |
| 1436 | static int vidioc_reqbufs(struct file *file, void *priv, |
| 1437 | struct v4l2_requestbuffers *rb) |
| 1438 | { |
| 1439 | struct gspca_dev *gspca_dev = priv; |
Jean-François Moine | 832d0a9 | 2010-04-09 06:07:58 -0300 | [diff] [blame] | 1440 | int i, ret = 0, streaming; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1441 | |
Jean-François Moine | a2ba6f2 | 2010-07-14 06:21:16 -0300 | [diff] [blame] | 1442 | i = rb->memory; /* (avoid compilation warning) */ |
| 1443 | switch (i) { |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1444 | case GSPCA_MEMORY_READ: /* (internal call) */ |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1445 | case V4L2_MEMORY_MMAP: |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1446 | case V4L2_MEMORY_USERPTR: |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 1447 | break; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1448 | default: |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1449 | return -EINVAL; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1450 | } |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1451 | if (mutex_lock_interruptible(&gspca_dev->queue_lock)) |
| 1452 | return -ERESTARTSYS; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1453 | |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1454 | if (gspca_dev->memory != GSPCA_MEMORY_NO |
| 1455 | && gspca_dev->memory != rb->memory) { |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1456 | ret = -EBUSY; |
| 1457 | goto out; |
| 1458 | } |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1459 | |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1460 | /* only one file may do the capture */ |
| 1461 | if (gspca_dev->capt_file != NULL |
| 1462 | && gspca_dev->capt_file != file) { |
| 1463 | ret = -EBUSY; |
| 1464 | goto out; |
| 1465 | } |
| 1466 | |
| 1467 | /* if allocated, the buffers must not be mapped */ |
| 1468 | for (i = 0; i < gspca_dev->nframes; i++) { |
| 1469 | if (gspca_dev->frame[i].vma_use_count) { |
Jean-Francois Moine | ac0d6df | 2008-07-08 06:58:15 -0300 | [diff] [blame] | 1470 | ret = -EBUSY; |
| 1471 | goto out; |
| 1472 | } |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1473 | } |
| 1474 | |
| 1475 | /* stop streaming */ |
Jean-François Moine | 832d0a9 | 2010-04-09 06:07:58 -0300 | [diff] [blame] | 1476 | streaming = gspca_dev->streaming; |
| 1477 | if (streaming) { |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1478 | mutex_lock(&gspca_dev->usb_lock); |
Jean-Francois Moine | 8c4ebae | 2009-12-02 07:14:33 -0300 | [diff] [blame] | 1479 | gspca_dev->usb_err = 0; |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1480 | gspca_stream_off(gspca_dev); |
| 1481 | mutex_unlock(&gspca_dev->usb_lock); |
| 1482 | } |
| 1483 | |
| 1484 | /* free the previous allocated buffers, if any */ |
| 1485 | if (gspca_dev->nframes != 0) { |
| 1486 | frame_free(gspca_dev); |
| 1487 | gspca_dev->capt_file = NULL; |
| 1488 | } |
| 1489 | if (rb->count == 0) /* unrequest */ |
| 1490 | goto out; |
| 1491 | gspca_dev->memory = rb->memory; |
| 1492 | ret = frame_alloc(gspca_dev, rb->count); |
| 1493 | if (ret == 0) { |
| 1494 | rb->count = gspca_dev->nframes; |
| 1495 | gspca_dev->capt_file = file; |
Jean-François Moine | 832d0a9 | 2010-04-09 06:07:58 -0300 | [diff] [blame] | 1496 | if (streaming) |
| 1497 | ret = gspca_init_transfer(gspca_dev); |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1498 | } |
| 1499 | out: |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1500 | mutex_unlock(&gspca_dev->queue_lock); |
| 1501 | PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count); |
| 1502 | return ret; |
| 1503 | } |
| 1504 | |
| 1505 | static int vidioc_querybuf(struct file *file, void *priv, |
| 1506 | struct v4l2_buffer *v4l2_buf) |
| 1507 | { |
| 1508 | struct gspca_dev *gspca_dev = priv; |
| 1509 | struct gspca_frame *frame; |
| 1510 | |
Jean-Francois Moine | 13752bd | 2008-12-19 15:08:51 -0300 | [diff] [blame] | 1511 | if (v4l2_buf->index < 0 |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1512 | || v4l2_buf->index >= gspca_dev->nframes) |
| 1513 | return -EINVAL; |
| 1514 | |
| 1515 | frame = &gspca_dev->frame[v4l2_buf->index]; |
| 1516 | memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf); |
| 1517 | return 0; |
| 1518 | } |
| 1519 | |
| 1520 | static int vidioc_streamon(struct file *file, void *priv, |
| 1521 | enum v4l2_buf_type buf_type) |
| 1522 | { |
| 1523 | struct gspca_dev *gspca_dev = priv; |
| 1524 | int ret; |
| 1525 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1526 | if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1527 | return -EINVAL; |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1528 | if (mutex_lock_interruptible(&gspca_dev->queue_lock)) |
| 1529 | return -ERESTARTSYS; |
Jean-Francois Moine | 090264e | 2009-02-12 08:05:45 -0300 | [diff] [blame] | 1530 | |
Jean-François Moine | 18bf99a | 2010-04-09 06:06:11 -0300 | [diff] [blame] | 1531 | /* check the capture file */ |
| 1532 | if (gspca_dev->capt_file != file) { |
| 1533 | ret = -EBUSY; |
| 1534 | goto out; |
| 1535 | } |
| 1536 | |
Jean-Francois Moine | 1edabe7 | 2008-12-19 15:17:25 -0300 | [diff] [blame] | 1537 | if (gspca_dev->nframes == 0 |
| 1538 | || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1539 | ret = -EINVAL; |
| 1540 | goto out; |
| 1541 | } |
| 1542 | if (!gspca_dev->streaming) { |
| 1543 | ret = gspca_init_transfer(gspca_dev); |
| 1544 | if (ret < 0) |
| 1545 | goto out; |
| 1546 | } |
Jean-Francois Moine | 335b3f8 | 2008-07-30 04:53:02 -0300 | [diff] [blame] | 1547 | #ifdef GSPCA_DEBUG |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1548 | if (gspca_debug & D_STREAM) { |
| 1549 | PDEBUG_MODE("stream on OK", |
| 1550 | gspca_dev->pixfmt, |
| 1551 | gspca_dev->width, |
| 1552 | gspca_dev->height); |
| 1553 | } |
| 1554 | #endif |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1555 | ret = 0; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1556 | out: |
| 1557 | mutex_unlock(&gspca_dev->queue_lock); |
| 1558 | return ret; |
| 1559 | } |
| 1560 | |
| 1561 | static int vidioc_streamoff(struct file *file, void *priv, |
| 1562 | enum v4l2_buf_type buf_type) |
| 1563 | { |
| 1564 | struct gspca_dev *gspca_dev = priv; |
Jean-François Moine | f7059ea | 2010-07-06 04:32:27 -0300 | [diff] [blame] | 1565 | int ret; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1566 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1567 | if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1568 | return -EINVAL; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1569 | if (!gspca_dev->streaming) |
| 1570 | return 0; |
| 1571 | if (mutex_lock_interruptible(&gspca_dev->queue_lock)) |
| 1572 | return -ERESTARTSYS; |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1573 | |
Jean-François Moine | 18bf99a | 2010-04-09 06:06:11 -0300 | [diff] [blame] | 1574 | /* check the capture file */ |
| 1575 | if (gspca_dev->capt_file != file) { |
| 1576 | ret = -EBUSY; |
| 1577 | goto out; |
| 1578 | } |
| 1579 | |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1580 | /* stop streaming */ |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1581 | if (mutex_lock_interruptible(&gspca_dev->usb_lock)) { |
| 1582 | ret = -ERESTARTSYS; |
| 1583 | goto out; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1584 | } |
Jean-Francois Moine | 8c4ebae | 2009-12-02 07:14:33 -0300 | [diff] [blame] | 1585 | gspca_dev->usb_err = 0; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1586 | gspca_stream_off(gspca_dev); |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1587 | mutex_unlock(&gspca_dev->usb_lock); |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1588 | |
Jean-François Moine | f7059ea | 2010-07-06 04:32:27 -0300 | [diff] [blame] | 1589 | /* empty the transfer queues */ |
| 1590 | atomic_set(&gspca_dev->fr_q, 0); |
| 1591 | atomic_set(&gspca_dev->fr_i, 0); |
| 1592 | gspca_dev->fr_o = 0; |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1593 | ret = 0; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1594 | out: |
| 1595 | mutex_unlock(&gspca_dev->queue_lock); |
| 1596 | return ret; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1597 | } |
| 1598 | |
| 1599 | static int vidioc_g_jpegcomp(struct file *file, void *priv, |
| 1600 | struct v4l2_jpegcompression *jpegcomp) |
| 1601 | { |
| 1602 | struct gspca_dev *gspca_dev = priv; |
| 1603 | int ret; |
| 1604 | |
| 1605 | if (!gspca_dev->sd_desc->get_jcomp) |
| 1606 | return -EINVAL; |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1607 | if (mutex_lock_interruptible(&gspca_dev->usb_lock)) |
| 1608 | return -ERESTARTSYS; |
Jean-Francois Moine | 8c4ebae | 2009-12-02 07:14:33 -0300 | [diff] [blame] | 1609 | gspca_dev->usb_err = 0; |
Jean-Francois Moine | 090264e | 2009-02-12 08:05:45 -0300 | [diff] [blame] | 1610 | if (gspca_dev->present) |
| 1611 | ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp); |
| 1612 | else |
| 1613 | ret = -ENODEV; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1614 | mutex_unlock(&gspca_dev->usb_lock); |
| 1615 | return ret; |
| 1616 | } |
| 1617 | |
| 1618 | static int vidioc_s_jpegcomp(struct file *file, void *priv, |
| 1619 | struct v4l2_jpegcompression *jpegcomp) |
| 1620 | { |
| 1621 | struct gspca_dev *gspca_dev = priv; |
| 1622 | int ret; |
| 1623 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1624 | if (!gspca_dev->sd_desc->set_jcomp) |
| 1625 | return -EINVAL; |
Jim Paris | f86d4a9 | 2008-12-10 05:50:44 -0300 | [diff] [blame] | 1626 | if (mutex_lock_interruptible(&gspca_dev->usb_lock)) |
| 1627 | return -ERESTARTSYS; |
Jean-Francois Moine | 8c4ebae | 2009-12-02 07:14:33 -0300 | [diff] [blame] | 1628 | gspca_dev->usb_err = 0; |
Jean-Francois Moine | 090264e | 2009-02-12 08:05:45 -0300 | [diff] [blame] | 1629 | if (gspca_dev->present) |
| 1630 | ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp); |
| 1631 | else |
| 1632 | ret = -ENODEV; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1633 | mutex_unlock(&gspca_dev->usb_lock); |
| 1634 | return ret; |
| 1635 | } |
| 1636 | |
| 1637 | static int vidioc_g_parm(struct file *filp, void *priv, |
| 1638 | struct v4l2_streamparm *parm) |
| 1639 | { |
| 1640 | struct gspca_dev *gspca_dev = priv; |
| 1641 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1642 | parm->parm.capture.readbuffers = gspca_dev->nbufread; |
Jim Paris | 627a5ef | 2008-12-10 06:02:42 -0300 | [diff] [blame] | 1643 | |
| 1644 | if (gspca_dev->sd_desc->get_streamparm) { |
| 1645 | int ret; |
| 1646 | |
| 1647 | if (mutex_lock_interruptible(&gspca_dev->usb_lock)) |
| 1648 | return -ERESTARTSYS; |
Jean-Francois Moine | 8c4ebae | 2009-12-02 07:14:33 -0300 | [diff] [blame] | 1649 | gspca_dev->usb_err = 0; |
Jean-Francois Moine | 090264e | 2009-02-12 08:05:45 -0300 | [diff] [blame] | 1650 | if (gspca_dev->present) |
| 1651 | ret = gspca_dev->sd_desc->get_streamparm(gspca_dev, |
| 1652 | parm); |
| 1653 | else |
| 1654 | ret = -ENODEV; |
Jim Paris | 627a5ef | 2008-12-10 06:02:42 -0300 | [diff] [blame] | 1655 | mutex_unlock(&gspca_dev->usb_lock); |
| 1656 | return ret; |
| 1657 | } |
| 1658 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1659 | return 0; |
| 1660 | } |
| 1661 | |
| 1662 | static int vidioc_s_parm(struct file *filp, void *priv, |
| 1663 | struct v4l2_streamparm *parm) |
| 1664 | { |
| 1665 | struct gspca_dev *gspca_dev = priv; |
| 1666 | int n; |
| 1667 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1668 | n = parm->parm.capture.readbuffers; |
Jean-François Moine | f7059ea | 2010-07-06 04:32:27 -0300 | [diff] [blame] | 1669 | if (n == 0 || n >= GSPCA_MAX_FRAMES) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1670 | parm->parm.capture.readbuffers = gspca_dev->nbufread; |
| 1671 | else |
| 1672 | gspca_dev->nbufread = n; |
Jim Paris | 627a5ef | 2008-12-10 06:02:42 -0300 | [diff] [blame] | 1673 | |
| 1674 | if (gspca_dev->sd_desc->set_streamparm) { |
| 1675 | int ret; |
| 1676 | |
| 1677 | if (mutex_lock_interruptible(&gspca_dev->usb_lock)) |
| 1678 | return -ERESTARTSYS; |
Jean-Francois Moine | 8c4ebae | 2009-12-02 07:14:33 -0300 | [diff] [blame] | 1679 | gspca_dev->usb_err = 0; |
Jean-Francois Moine | 090264e | 2009-02-12 08:05:45 -0300 | [diff] [blame] | 1680 | if (gspca_dev->present) |
| 1681 | ret = gspca_dev->sd_desc->set_streamparm(gspca_dev, |
| 1682 | parm); |
| 1683 | else |
| 1684 | ret = -ENODEV; |
Jim Paris | 627a5ef | 2008-12-10 06:02:42 -0300 | [diff] [blame] | 1685 | mutex_unlock(&gspca_dev->usb_lock); |
| 1686 | return ret; |
| 1687 | } |
| 1688 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1689 | return 0; |
| 1690 | } |
| 1691 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1692 | static int dev_mmap(struct file *file, struct vm_area_struct *vma) |
| 1693 | { |
| 1694 | struct gspca_dev *gspca_dev = file->private_data; |
Harvey Harrison | efab821 | 2008-07-05 06:12:47 -0300 | [diff] [blame] | 1695 | struct gspca_frame *frame; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1696 | struct page *page; |
| 1697 | unsigned long addr, start, size; |
| 1698 | int i, ret; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1699 | |
| 1700 | start = vma->vm_start; |
| 1701 | size = vma->vm_end - vma->vm_start; |
| 1702 | PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size); |
| 1703 | |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1704 | if (mutex_lock_interruptible(&gspca_dev->queue_lock)) |
| 1705 | return -ERESTARTSYS; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1706 | if (!gspca_dev->present) { |
| 1707 | ret = -ENODEV; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1708 | goto out; |
| 1709 | } |
| 1710 | if (gspca_dev->capt_file != file) { |
| 1711 | ret = -EINVAL; |
| 1712 | goto out; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1713 | } |
| 1714 | |
Harvey Harrison | efab821 | 2008-07-05 06:12:47 -0300 | [diff] [blame] | 1715 | frame = NULL; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1716 | for (i = 0; i < gspca_dev->nframes; ++i) { |
| 1717 | if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) { |
| 1718 | PDEBUG(D_STREAM, "mmap bad memory type"); |
| 1719 | break; |
| 1720 | } |
| 1721 | if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT) |
| 1722 | == vma->vm_pgoff) { |
| 1723 | frame = &gspca_dev->frame[i]; |
| 1724 | break; |
| 1725 | } |
| 1726 | } |
Harvey Harrison | efab821 | 2008-07-05 06:12:47 -0300 | [diff] [blame] | 1727 | if (frame == NULL) { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1728 | PDEBUG(D_STREAM, "mmap no frame buffer found"); |
| 1729 | ret = -EINVAL; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1730 | goto out; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1731 | } |
Jean-François Moine | 38bff69 | 2010-06-24 05:19:58 -0300 | [diff] [blame] | 1732 | if (size != frame->v4l2_buf.length) { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1733 | PDEBUG(D_STREAM, "mmap bad size"); |
| 1734 | ret = -EINVAL; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1735 | goto out; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1736 | } |
| 1737 | |
| 1738 | /* |
| 1739 | * - VM_IO marks the area as being a mmaped region for I/O to a |
| 1740 | * device. It also prevents the region from being core dumped. |
| 1741 | */ |
| 1742 | vma->vm_flags |= VM_IO; |
| 1743 | |
| 1744 | addr = (unsigned long) frame->data; |
| 1745 | while (size > 0) { |
| 1746 | page = vmalloc_to_page((void *) addr); |
| 1747 | ret = vm_insert_page(vma, start, page); |
| 1748 | if (ret < 0) |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1749 | goto out; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1750 | start += PAGE_SIZE; |
| 1751 | addr += PAGE_SIZE; |
| 1752 | size -= PAGE_SIZE; |
| 1753 | } |
| 1754 | |
Jean-Francois Moine | d50e257 | 2009-11-26 15:36:40 -0300 | [diff] [blame] | 1755 | vma->vm_ops = &gspca_vm_ops; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1756 | vma->vm_private_data = frame; |
| 1757 | gspca_vm_open(vma); |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1758 | ret = 0; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1759 | out: |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1760 | mutex_unlock(&gspca_dev->queue_lock); |
| 1761 | return ret; |
| 1762 | } |
| 1763 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1764 | /* |
| 1765 | * wait for a video frame |
| 1766 | * |
| 1767 | * If a frame is ready, its index is returned. |
| 1768 | */ |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1769 | static int frame_wait(struct gspca_dev *gspca_dev, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1770 | int nonblock_ing) |
| 1771 | { |
Jean-François Moine | f7059ea | 2010-07-06 04:32:27 -0300 | [diff] [blame] | 1772 | int i, ret; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1773 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1774 | /* check if a frame is ready */ |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1775 | i = gspca_dev->fr_o; |
Jean-François Moine | f7059ea | 2010-07-06 04:32:27 -0300 | [diff] [blame] | 1776 | if (i == atomic_read(&gspca_dev->fr_i)) { |
Hans de Goede | 181b704 | 2008-11-19 17:16:26 -0300 | [diff] [blame] | 1777 | if (nonblock_ing) |
| 1778 | return -EAGAIN; |
| 1779 | |
| 1780 | /* wait till a frame is ready */ |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1781 | ret = wait_event_interruptible_timeout(gspca_dev->wq, |
Jean-François Moine | f7059ea | 2010-07-06 04:32:27 -0300 | [diff] [blame] | 1782 | i != atomic_read(&gspca_dev->fr_i) || |
Hans de Goede | 181b704 | 2008-11-19 17:16:26 -0300 | [diff] [blame] | 1783 | !gspca_dev->streaming || !gspca_dev->present, |
| 1784 | msecs_to_jiffies(3000)); |
| 1785 | if (ret < 0) |
| 1786 | return ret; |
| 1787 | if (ret == 0 || !gspca_dev->streaming || !gspca_dev->present) |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1788 | return -EIO; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1789 | } |
Hans de Goede | 181b704 | 2008-11-19 17:16:26 -0300 | [diff] [blame] | 1790 | |
Jean-François Moine | f7059ea | 2010-07-06 04:32:27 -0300 | [diff] [blame] | 1791 | gspca_dev->fr_o = (i + 1) % GSPCA_MAX_FRAMES; |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1792 | |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 1793 | if (gspca_dev->sd_desc->dq_callback) { |
| 1794 | mutex_lock(&gspca_dev->usb_lock); |
Jean-Francois Moine | 8c4ebae | 2009-12-02 07:14:33 -0300 | [diff] [blame] | 1795 | gspca_dev->usb_err = 0; |
Jean-Francois Moine | 090264e | 2009-02-12 08:05:45 -0300 | [diff] [blame] | 1796 | if (gspca_dev->present) |
| 1797 | gspca_dev->sd_desc->dq_callback(gspca_dev); |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 1798 | mutex_unlock(&gspca_dev->usb_lock); |
| 1799 | } |
Jean-François Moine | f7059ea | 2010-07-06 04:32:27 -0300 | [diff] [blame] | 1800 | return gspca_dev->fr_queue[i]; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1801 | } |
| 1802 | |
| 1803 | /* |
| 1804 | * dequeue a video buffer |
| 1805 | * |
| 1806 | * If nonblock_ing is false, block until a buffer is available. |
| 1807 | */ |
| 1808 | static int vidioc_dqbuf(struct file *file, void *priv, |
| 1809 | struct v4l2_buffer *v4l2_buf) |
| 1810 | { |
| 1811 | struct gspca_dev *gspca_dev = priv; |
| 1812 | struct gspca_frame *frame; |
| 1813 | int i, ret; |
| 1814 | |
| 1815 | PDEBUG(D_FRAM, "dqbuf"); |
Jean-Francois Moine | ac0d6df | 2008-07-08 06:58:15 -0300 | [diff] [blame] | 1816 | if (v4l2_buf->memory != gspca_dev->memory) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1817 | return -EINVAL; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1818 | |
Jean-Francois Moine | 090264e | 2009-02-12 08:05:45 -0300 | [diff] [blame] | 1819 | if (!gspca_dev->present) |
| 1820 | return -ENODEV; |
| 1821 | |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1822 | /* if not streaming, be sure the application will not loop forever */ |
| 1823 | if (!(file->f_flags & O_NONBLOCK) |
| 1824 | && !gspca_dev->streaming && gspca_dev->users == 1) |
| 1825 | return -EINVAL; |
| 1826 | |
| 1827 | /* only the capturing file may dequeue */ |
| 1828 | if (gspca_dev->capt_file != file) |
| 1829 | return -EINVAL; |
| 1830 | |
| 1831 | /* only one dequeue / read at a time */ |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1832 | if (mutex_lock_interruptible(&gspca_dev->read_lock)) |
| 1833 | return -ERESTARTSYS; |
| 1834 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1835 | ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1836 | if (ret < 0) |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1837 | goto out; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1838 | i = ret; /* frame index */ |
| 1839 | frame = &gspca_dev->frame[i]; |
Jean-Francois Moine | ac0d6df | 2008-07-08 06:58:15 -0300 | [diff] [blame] | 1840 | if (gspca_dev->memory == V4L2_MEMORY_USERPTR) { |
Hans Verkuil | c6eb8ea | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 1841 | if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr, |
Jean-Francois Moine | ac0d6df | 2008-07-08 06:58:15 -0300 | [diff] [blame] | 1842 | frame->data, |
| 1843 | frame->v4l2_buf.bytesused)) { |
| 1844 | PDEBUG(D_ERR|D_STREAM, |
| 1845 | "dqbuf cp to user failed"); |
| 1846 | ret = -EFAULT; |
| 1847 | goto out; |
| 1848 | } |
| 1849 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1850 | frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE; |
| 1851 | memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf); |
| 1852 | PDEBUG(D_FRAM, "dqbuf %d", i); |
| 1853 | ret = 0; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1854 | out: |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1855 | mutex_unlock(&gspca_dev->read_lock); |
| 1856 | return ret; |
| 1857 | } |
| 1858 | |
| 1859 | /* |
| 1860 | * queue a video buffer |
| 1861 | * |
| 1862 | * Attempting to queue a buffer that has already been |
| 1863 | * queued will return -EINVAL. |
| 1864 | */ |
| 1865 | static int vidioc_qbuf(struct file *file, void *priv, |
| 1866 | struct v4l2_buffer *v4l2_buf) |
| 1867 | { |
| 1868 | struct gspca_dev *gspca_dev = priv; |
| 1869 | struct gspca_frame *frame; |
| 1870 | int i, index, ret; |
| 1871 | |
| 1872 | PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1873 | |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1874 | if (mutex_lock_interruptible(&gspca_dev->queue_lock)) |
| 1875 | return -ERESTARTSYS; |
| 1876 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1877 | index = v4l2_buf->index; |
| 1878 | if ((unsigned) index >= gspca_dev->nframes) { |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1879 | PDEBUG(D_FRAM, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1880 | "qbuf idx %d >= %d", index, gspca_dev->nframes); |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1881 | ret = -EINVAL; |
| 1882 | goto out; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1883 | } |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1884 | if (v4l2_buf->memory != gspca_dev->memory) { |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1885 | PDEBUG(D_FRAM, "qbuf bad memory type"); |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1886 | ret = -EINVAL; |
| 1887 | goto out; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1888 | } |
| 1889 | |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1890 | frame = &gspca_dev->frame[index]; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1891 | if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) { |
| 1892 | PDEBUG(D_FRAM, "qbuf bad state"); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1893 | ret = -EINVAL; |
| 1894 | goto out; |
| 1895 | } |
| 1896 | |
| 1897 | frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1898 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1899 | if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1900 | frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr; |
| 1901 | frame->v4l2_buf.length = v4l2_buf->length; |
| 1902 | } |
| 1903 | |
| 1904 | /* put the buffer in the 'queued' queue */ |
Jean-François Moine | f7059ea | 2010-07-06 04:32:27 -0300 | [diff] [blame] | 1905 | i = atomic_read(&gspca_dev->fr_q); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1906 | gspca_dev->fr_queue[i] = index; |
Jean-François Moine | f7059ea | 2010-07-06 04:32:27 -0300 | [diff] [blame] | 1907 | atomic_set(&gspca_dev->fr_q, (i + 1) % GSPCA_MAX_FRAMES); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1908 | |
| 1909 | v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED; |
| 1910 | v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE; |
| 1911 | ret = 0; |
| 1912 | out: |
| 1913 | mutex_unlock(&gspca_dev->queue_lock); |
| 1914 | return ret; |
| 1915 | } |
| 1916 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1917 | /* |
| 1918 | * allocate the resources for read() |
| 1919 | */ |
| 1920 | static int read_alloc(struct gspca_dev *gspca_dev, |
| 1921 | struct file *file) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1922 | { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1923 | struct v4l2_buffer v4l2_buf; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1924 | int i, ret; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1925 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1926 | PDEBUG(D_STREAM, "read alloc"); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1927 | if (gspca_dev->nframes == 0) { |
| 1928 | struct v4l2_requestbuffers rb; |
| 1929 | |
| 1930 | memset(&rb, 0, sizeof rb); |
| 1931 | rb.count = gspca_dev->nbufread; |
| 1932 | rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
Jean-Francois Moine | ac0d6df | 2008-07-08 06:58:15 -0300 | [diff] [blame] | 1933 | rb.memory = GSPCA_MEMORY_READ; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1934 | ret = vidioc_reqbufs(file, gspca_dev, &rb); |
| 1935 | if (ret != 0) { |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1936 | PDEBUG(D_STREAM, "read reqbuf err %d", ret); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1937 | return ret; |
| 1938 | } |
| 1939 | memset(&v4l2_buf, 0, sizeof v4l2_buf); |
| 1940 | v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
Jean-Francois Moine | ac0d6df | 2008-07-08 06:58:15 -0300 | [diff] [blame] | 1941 | v4l2_buf.memory = GSPCA_MEMORY_READ; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1942 | for (i = 0; i < gspca_dev->nbufread; i++) { |
| 1943 | v4l2_buf.index = i; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1944 | ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf); |
| 1945 | if (ret != 0) { |
| 1946 | PDEBUG(D_STREAM, "read qbuf err: %d", ret); |
| 1947 | return ret; |
| 1948 | } |
| 1949 | } |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1950 | gspca_dev->memory = GSPCA_MEMORY_READ; |
| 1951 | } |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1952 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1953 | /* start streaming */ |
| 1954 | ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE); |
| 1955 | if (ret != 0) |
| 1956 | PDEBUG(D_STREAM, "read streamon err %d", ret); |
| 1957 | return ret; |
| 1958 | } |
| 1959 | |
| 1960 | static unsigned int dev_poll(struct file *file, poll_table *wait) |
| 1961 | { |
| 1962 | struct gspca_dev *gspca_dev = file->private_data; |
Jean-François Moine | f7059ea | 2010-07-06 04:32:27 -0300 | [diff] [blame] | 1963 | int ret; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1964 | |
| 1965 | PDEBUG(D_FRAM, "poll"); |
| 1966 | |
| 1967 | poll_wait(file, &gspca_dev->wq, wait); |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1968 | |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1969 | /* if reqbufs is not done, the user would use read() */ |
| 1970 | if (gspca_dev->nframes == 0) { |
| 1971 | if (gspca_dev->memory != GSPCA_MEMORY_NO) |
| 1972 | return POLLERR; /* not the 1st time */ |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1973 | ret = read_alloc(gspca_dev, file); |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1974 | if (ret != 0) |
| 1975 | return POLLERR; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1976 | } |
| 1977 | |
| 1978 | if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0) |
| 1979 | return POLLERR; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1980 | |
Jean-François Moine | f7059ea | 2010-07-06 04:32:27 -0300 | [diff] [blame] | 1981 | /* check if an image has been received */ |
| 1982 | if (gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i)) |
| 1983 | ret = POLLIN | POLLRDNORM; /* yes */ |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1984 | else |
| 1985 | ret = 0; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1986 | mutex_unlock(&gspca_dev->queue_lock); |
Jean-Francois Moine | 090264e | 2009-02-12 08:05:45 -0300 | [diff] [blame] | 1987 | if (!gspca_dev->present) |
| 1988 | return POLLHUP; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1989 | return ret; |
| 1990 | } |
| 1991 | |
| 1992 | static ssize_t dev_read(struct file *file, char __user *data, |
| 1993 | size_t count, loff_t *ppos) |
| 1994 | { |
| 1995 | struct gspca_dev *gspca_dev = file->private_data; |
| 1996 | struct gspca_frame *frame; |
| 1997 | struct v4l2_buffer v4l2_buf; |
| 1998 | struct timeval timestamp; |
Jean-Francois Moine | 82643d0 | 2008-07-02 09:14:56 -0300 | [diff] [blame] | 1999 | int n, ret, ret2; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 2000 | |
Mauro Carvalho Chehab | a9e1113 | 2008-07-09 05:26:26 -0300 | [diff] [blame] | 2001 | PDEBUG(D_FRAM, "read (%zd)", count); |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 2002 | if (!gspca_dev->present) |
| 2003 | return -ENODEV; |
| 2004 | switch (gspca_dev->memory) { |
| 2005 | case GSPCA_MEMORY_NO: /* first time */ |
| 2006 | ret = read_alloc(gspca_dev, file); |
| 2007 | if (ret != 0) |
| 2008 | return ret; |
| 2009 | break; |
| 2010 | case GSPCA_MEMORY_READ: |
Jean-Francois Moine | 82643d0 | 2008-07-02 09:14:56 -0300 | [diff] [blame] | 2011 | if (gspca_dev->capt_file == file) |
| 2012 | break; |
| 2013 | /* fall thru */ |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 2014 | default: |
| 2015 | return -EINVAL; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2016 | } |
| 2017 | |
| 2018 | /* get a frame */ |
Paulo Assis | 2f51d00 | 2010-03-07 06:56:56 -0300 | [diff] [blame] | 2019 | timestamp = ktime_to_timeval(ktime_get()); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2020 | timestamp.tv_sec--; |
Jean-Francois Moine | 82643d0 | 2008-07-02 09:14:56 -0300 | [diff] [blame] | 2021 | n = 2; |
| 2022 | for (;;) { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2023 | memset(&v4l2_buf, 0, sizeof v4l2_buf); |
| 2024 | v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
Jean-Francois Moine | ac0d6df | 2008-07-08 06:58:15 -0300 | [diff] [blame] | 2025 | v4l2_buf.memory = GSPCA_MEMORY_READ; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2026 | ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf); |
| 2027 | if (ret != 0) { |
| 2028 | PDEBUG(D_STREAM, "read dqbuf err %d", ret); |
| 2029 | return ret; |
| 2030 | } |
| 2031 | |
| 2032 | /* if the process slept for more than 1 second, |
Frank Zago | 219423c | 2008-09-26 07:43:54 -0300 | [diff] [blame] | 2033 | * get a newer frame */ |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2034 | frame = &gspca_dev->frame[v4l2_buf.index]; |
Jean-Francois Moine | 82643d0 | 2008-07-02 09:14:56 -0300 | [diff] [blame] | 2035 | if (--n < 0) |
| 2036 | break; /* avoid infinite loop */ |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2037 | if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec) |
| 2038 | break; |
| 2039 | ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf); |
| 2040 | if (ret != 0) { |
| 2041 | PDEBUG(D_STREAM, "read qbuf err %d", ret); |
| 2042 | return ret; |
| 2043 | } |
| 2044 | } |
| 2045 | |
| 2046 | /* copy the frame */ |
Jean-Francois Moine | ac0d6df | 2008-07-08 06:58:15 -0300 | [diff] [blame] | 2047 | if (count > frame->v4l2_buf.bytesused) |
| 2048 | count = frame->v4l2_buf.bytesused; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2049 | ret = copy_to_user(data, frame->data, count); |
| 2050 | if (ret != 0) { |
| 2051 | PDEBUG(D_ERR|D_STREAM, |
Mauro Carvalho Chehab | a9e1113 | 2008-07-09 05:26:26 -0300 | [diff] [blame] | 2052 | "read cp to user lack %d / %zd", ret, count); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2053 | ret = -EFAULT; |
| 2054 | goto out; |
| 2055 | } |
| 2056 | ret = count; |
| 2057 | out: |
| 2058 | /* in each case, requeue the buffer */ |
| 2059 | ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf); |
| 2060 | if (ret2 != 0) |
| 2061 | return ret2; |
| 2062 | return ret; |
| 2063 | } |
| 2064 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 2065 | static struct v4l2_file_operations dev_fops = { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2066 | .owner = THIS_MODULE, |
| 2067 | .open = dev_open, |
| 2068 | .release = dev_close, |
| 2069 | .read = dev_read, |
| 2070 | .mmap = dev_mmap, |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 2071 | .unlocked_ioctl = video_ioctl2, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2072 | .poll = dev_poll, |
| 2073 | }; |
| 2074 | |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 2075 | static const struct v4l2_ioctl_ops dev_ioctl_ops = { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2076 | .vidioc_querycap = vidioc_querycap, |
| 2077 | .vidioc_dqbuf = vidioc_dqbuf, |
| 2078 | .vidioc_qbuf = vidioc_qbuf, |
Mauro Carvalho Chehab | e078770 | 2008-07-01 04:06:22 -0300 | [diff] [blame] | 2079 | .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, |
| 2080 | .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, |
| 2081 | .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, |
| 2082 | .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2083 | .vidioc_streamon = vidioc_streamon, |
| 2084 | .vidioc_queryctrl = vidioc_queryctrl, |
| 2085 | .vidioc_g_ctrl = vidioc_g_ctrl, |
| 2086 | .vidioc_s_ctrl = vidioc_s_ctrl, |
| 2087 | .vidioc_querymenu = vidioc_querymenu, |
| 2088 | .vidioc_enum_input = vidioc_enum_input, |
| 2089 | .vidioc_g_input = vidioc_g_input, |
| 2090 | .vidioc_s_input = vidioc_s_input, |
| 2091 | .vidioc_reqbufs = vidioc_reqbufs, |
| 2092 | .vidioc_querybuf = vidioc_querybuf, |
| 2093 | .vidioc_streamoff = vidioc_streamoff, |
| 2094 | .vidioc_g_jpegcomp = vidioc_g_jpegcomp, |
| 2095 | .vidioc_s_jpegcomp = vidioc_s_jpegcomp, |
| 2096 | .vidioc_g_parm = vidioc_g_parm, |
| 2097 | .vidioc_s_parm = vidioc_s_parm, |
Hans de Goede | 11c635a | 2009-05-25 15:04:22 -0300 | [diff] [blame] | 2098 | .vidioc_enum_framesizes = vidioc_enum_framesizes, |
Antonio Ospite | 28ffe77 | 2009-12-02 06:18:46 -0300 | [diff] [blame] | 2099 | .vidioc_enum_frameintervals = vidioc_enum_frameintervals, |
Brian Johnson | af1d9af | 2009-07-19 05:29:20 -0300 | [diff] [blame] | 2100 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 2101 | .vidioc_g_register = vidioc_g_register, |
| 2102 | .vidioc_s_register = vidioc_s_register, |
| 2103 | #endif |
| 2104 | .vidioc_g_chip_ident = vidioc_g_chip_ident, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2105 | }; |
| 2106 | |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 2107 | static struct video_device gspca_template = { |
| 2108 | .name = "gspca main driver", |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 2109 | .fops = &dev_fops, |
| 2110 | .ioctl_ops = &dev_ioctl_ops, |
Hans de Goede | d018230 | 2008-11-19 17:10:47 -0300 | [diff] [blame] | 2111 | .release = gspca_release, |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 2112 | }; |
| 2113 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2114 | /* |
| 2115 | * probe and create a new gspca device |
| 2116 | * |
| 2117 | * This function must be called by the sub-driver when it is |
| 2118 | * called for probing a new device. |
| 2119 | */ |
Jean-François Moine | 4462864 | 2010-06-05 07:47:36 -0300 | [diff] [blame] | 2120 | int gspca_dev_probe2(struct usb_interface *intf, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2121 | const struct usb_device_id *id, |
| 2122 | const struct sd_desc *sd_desc, |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 2123 | int dev_size, |
| 2124 | struct module *module) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2125 | { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2126 | struct gspca_dev *gspca_dev; |
| 2127 | struct usb_device *dev = interface_to_usbdev(intf); |
| 2128 | int ret; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2129 | |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 2130 | PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2131 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2132 | /* create the device */ |
| 2133 | if (dev_size < sizeof *gspca_dev) |
| 2134 | dev_size = sizeof *gspca_dev; |
| 2135 | gspca_dev = kzalloc(dev_size, GFP_KERNEL); |
Erik Andren | e293e59 | 2008-10-03 08:46:50 -0300 | [diff] [blame] | 2136 | if (!gspca_dev) { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2137 | err("couldn't kzalloc gspca struct"); |
Erik Andren | e293e59 | 2008-10-03 08:46:50 -0300 | [diff] [blame] | 2138 | return -ENOMEM; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2139 | } |
Jean-Francois Moine | 8295d99 | 2008-09-03 17:12:19 -0300 | [diff] [blame] | 2140 | gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL); |
| 2141 | if (!gspca_dev->usb_buf) { |
| 2142 | err("out of memory"); |
Erik Andren | e293e59 | 2008-10-03 08:46:50 -0300 | [diff] [blame] | 2143 | ret = -ENOMEM; |
Jean-Francois Moine | 8295d99 | 2008-09-03 17:12:19 -0300 | [diff] [blame] | 2144 | goto out; |
| 2145 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2146 | gspca_dev->dev = dev; |
Jean-François Moine | 4462864 | 2010-06-05 07:47:36 -0300 | [diff] [blame] | 2147 | gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 2148 | gspca_dev->nbalt = intf->num_altsetting; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2149 | gspca_dev->sd_desc = sd_desc; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2150 | gspca_dev->nbufread = 2; |
Jean-Francois Moine | ff37474 | 2008-10-23 07:29:51 -0300 | [diff] [blame] | 2151 | gspca_dev->empty_packet = -1; /* don't check the empty packets */ |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2152 | |
Jean-Francois Moine | 012d6b0 | 2008-09-03 17:12:16 -0300 | [diff] [blame] | 2153 | /* configure the subdriver and initialize the USB device */ |
Jean-Francois Moine | ba38ee8 | 2008-11-22 04:27:34 -0300 | [diff] [blame] | 2154 | ret = sd_desc->config(gspca_dev, id); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2155 | if (ret < 0) |
| 2156 | goto out; |
Jean-Francois Moine | ba38ee8 | 2008-11-22 04:27:34 -0300 | [diff] [blame] | 2157 | ret = sd_desc->init(gspca_dev); |
Jean-Francois Moine | 012d6b0 | 2008-09-03 17:12:16 -0300 | [diff] [blame] | 2158 | if (ret < 0) |
| 2159 | goto out; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2160 | gspca_set_default_mode(gspca_dev); |
| 2161 | |
Hans de Goede | 10ee240 | 2010-02-19 04:41:40 -0300 | [diff] [blame] | 2162 | ret = gspca_input_connect(gspca_dev); |
| 2163 | if (ret) |
| 2164 | goto out; |
| 2165 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2166 | mutex_init(&gspca_dev->usb_lock); |
| 2167 | mutex_init(&gspca_dev->read_lock); |
| 2168 | mutex_init(&gspca_dev->queue_lock); |
| 2169 | init_waitqueue_head(&gspca_dev->wq); |
| 2170 | |
| 2171 | /* init video stuff */ |
Jean-Francois Moine | ba38ee8 | 2008-11-22 04:27:34 -0300 | [diff] [blame] | 2172 | memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template); |
Hans de Goede | 2444163 | 2009-03-27 16:08:20 -0300 | [diff] [blame] | 2173 | gspca_dev->vdev.parent = &intf->dev; |
Jean-Francois Moine | 5c4fa00 | 2008-11-18 15:52:31 -0300 | [diff] [blame] | 2174 | gspca_dev->module = module; |
Oliver Neukum | ee56a4d | 2008-07-27 14:01:59 -0300 | [diff] [blame] | 2175 | gspca_dev->present = 1; |
Jean-Francois Moine | a12ca6a | 2008-11-19 06:37:53 -0300 | [diff] [blame] | 2176 | ret = video_register_device(&gspca_dev->vdev, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2177 | VFL_TYPE_GRABBER, |
Jean-Francois Moine | 8d584a5 | 2009-01-13 06:07:59 -0300 | [diff] [blame] | 2178 | -1); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2179 | if (ret < 0) { |
| 2180 | err("video_register_device err %d", ret); |
| 2181 | goto out; |
| 2182 | } |
| 2183 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2184 | usb_set_intfdata(intf, gspca_dev); |
Laurent Pinchart | 38c7c03 | 2009-11-27 13:57:15 -0300 | [diff] [blame] | 2185 | PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev)); |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 2186 | |
Hans de Goede | 10ee240 | 2010-02-19 04:41:40 -0300 | [diff] [blame] | 2187 | gspca_input_create_urb(gspca_dev); |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 2188 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2189 | return 0; |
| 2190 | out: |
Randy Dunlap | f762801 | 2010-03-30 14:32:04 -0300 | [diff] [blame] | 2191 | #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE) |
Hans de Goede | 10ee240 | 2010-02-19 04:41:40 -0300 | [diff] [blame] | 2192 | if (gspca_dev->input_dev) |
| 2193 | input_unregister_device(gspca_dev->input_dev); |
Hans de Goede | e763752 | 2010-02-19 05:11:09 -0300 | [diff] [blame] | 2194 | #endif |
Jean-Francois Moine | e07a1d8 | 2008-11-19 06:37:53 -0300 | [diff] [blame] | 2195 | kfree(gspca_dev->usb_buf); |
| 2196 | kfree(gspca_dev); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2197 | return ret; |
| 2198 | } |
Jean-François Moine | 4462864 | 2010-06-05 07:47:36 -0300 | [diff] [blame] | 2199 | EXPORT_SYMBOL(gspca_dev_probe2); |
| 2200 | |
| 2201 | /* same function as the previous one, but check the interface */ |
| 2202 | int gspca_dev_probe(struct usb_interface *intf, |
| 2203 | const struct usb_device_id *id, |
| 2204 | const struct sd_desc *sd_desc, |
| 2205 | int dev_size, |
| 2206 | struct module *module) |
| 2207 | { |
| 2208 | struct usb_device *dev = interface_to_usbdev(intf); |
| 2209 | |
| 2210 | /* we don't handle multi-config cameras */ |
| 2211 | if (dev->descriptor.bNumConfigurations != 1) { |
| 2212 | PDEBUG(D_ERR, "%04x:%04x too many config", |
| 2213 | id->idVendor, id->idProduct); |
| 2214 | return -ENODEV; |
| 2215 | } |
| 2216 | |
| 2217 | /* the USB video interface must be the first one */ |
| 2218 | if (dev->config->desc.bNumInterfaces != 1 |
Jean-François Moine | c4dc692 | 2010-07-14 06:26:54 -0300 | [diff] [blame^] | 2219 | && intf->cur_altsetting->desc.bInterfaceNumber != 0) |
Jean-François Moine | 4462864 | 2010-06-05 07:47:36 -0300 | [diff] [blame] | 2220 | return -ENODEV; |
Jean-François Moine | 4462864 | 2010-06-05 07:47:36 -0300 | [diff] [blame] | 2221 | |
| 2222 | return gspca_dev_probe2(intf, id, sd_desc, dev_size, module); |
| 2223 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2224 | EXPORT_SYMBOL(gspca_dev_probe); |
| 2225 | |
| 2226 | /* |
| 2227 | * USB disconnection |
| 2228 | * |
| 2229 | * This function must be called by the sub-driver |
| 2230 | * when the device disconnects, after the specific resources are freed. |
| 2231 | */ |
| 2232 | void gspca_disconnect(struct usb_interface *intf) |
| 2233 | { |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 2234 | struct gspca_dev *gspca_dev = usb_get_intfdata(intf); |
Randy Dunlap | f762801 | 2010-03-30 14:32:04 -0300 | [diff] [blame] | 2235 | #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE) |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 2236 | struct input_dev *input_dev; |
Jean-François Moine | e070bf9 | 2010-02-18 15:02:51 -0300 | [diff] [blame] | 2237 | #endif |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2238 | |
Laurent Pinchart | 38c7c03 | 2009-11-27 13:57:15 -0300 | [diff] [blame] | 2239 | PDEBUG(D_PROBE, "%s disconnect", |
| 2240 | video_device_node_name(&gspca_dev->vdev)); |
Adam Baker | ad28127 | 2009-02-04 15:33:21 -0300 | [diff] [blame] | 2241 | mutex_lock(&gspca_dev->usb_lock); |
Jean-Francois Moine | 6e4ae87 | 2008-11-22 05:35:49 -0300 | [diff] [blame] | 2242 | gspca_dev->present = 0; |
Jean-Francois Moine | 090264e | 2009-02-12 08:05:45 -0300 | [diff] [blame] | 2243 | |
| 2244 | if (gspca_dev->streaming) { |
| 2245 | destroy_urbs(gspca_dev); |
| 2246 | wake_up_interruptible(&gspca_dev->wq); |
| 2247 | } |
| 2248 | |
Randy Dunlap | f762801 | 2010-03-30 14:32:04 -0300 | [diff] [blame] | 2249 | #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE) |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 2250 | gspca_input_destroy_urb(gspca_dev); |
| 2251 | input_dev = gspca_dev->input_dev; |
| 2252 | if (input_dev) { |
| 2253 | gspca_dev->input_dev = NULL; |
| 2254 | input_unregister_device(input_dev); |
| 2255 | } |
Jean-François Moine | e070bf9 | 2010-02-18 15:02:51 -0300 | [diff] [blame] | 2256 | #endif |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 2257 | |
Jean-Francois Moine | 090264e | 2009-02-12 08:05:45 -0300 | [diff] [blame] | 2258 | /* the device is freed at exit of this function */ |
| 2259 | gspca_dev->dev = NULL; |
Adam Baker | ad28127 | 2009-02-04 15:33:21 -0300 | [diff] [blame] | 2260 | mutex_unlock(&gspca_dev->usb_lock); |
Jean-Francois Moine | 6e4ae87 | 2008-11-22 05:35:49 -0300 | [diff] [blame] | 2261 | |
Frank Zago | 6b060ff | 2008-09-28 08:12:22 -0300 | [diff] [blame] | 2262 | usb_set_intfdata(intf, NULL); |
| 2263 | |
Jean-Francois Moine | a12ca6a | 2008-11-19 06:37:53 -0300 | [diff] [blame] | 2264 | /* release the device */ |
| 2265 | /* (this will call gspca_release() immediatly or on last close) */ |
| 2266 | video_unregister_device(&gspca_dev->vdev); |
Jean-Francois Moine | 3145b8c | 2008-10-05 04:22:10 -0300 | [diff] [blame] | 2267 | |
Jean-Francois Moine | 0d07e24 | 2009-10-01 13:44:49 -0300 | [diff] [blame] | 2268 | /* PDEBUG(D_PROBE, "disconnect complete"); */ |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2269 | } |
| 2270 | EXPORT_SYMBOL(gspca_disconnect); |
| 2271 | |
Jean-Francois Moine | 6a70974 | 2008-09-03 16:48:10 -0300 | [diff] [blame] | 2272 | #ifdef CONFIG_PM |
| 2273 | int gspca_suspend(struct usb_interface *intf, pm_message_t message) |
| 2274 | { |
| 2275 | struct gspca_dev *gspca_dev = usb_get_intfdata(intf); |
| 2276 | |
| 2277 | if (!gspca_dev->streaming) |
| 2278 | return 0; |
| 2279 | gspca_dev->frozen = 1; /* avoid urb error messages */ |
Jean-Francois Moine | 012d6b0 | 2008-09-03 17:12:16 -0300 | [diff] [blame] | 2280 | if (gspca_dev->sd_desc->stopN) |
| 2281 | gspca_dev->sd_desc->stopN(gspca_dev); |
Jean-Francois Moine | 6a70974 | 2008-09-03 16:48:10 -0300 | [diff] [blame] | 2282 | destroy_urbs(gspca_dev); |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 2283 | gspca_input_destroy_urb(gspca_dev); |
Jean-Francois Moine | 6a70974 | 2008-09-03 16:48:10 -0300 | [diff] [blame] | 2284 | gspca_set_alt0(gspca_dev); |
Jean-Francois Moine | 012d6b0 | 2008-09-03 17:12:16 -0300 | [diff] [blame] | 2285 | if (gspca_dev->sd_desc->stop0) |
| 2286 | gspca_dev->sd_desc->stop0(gspca_dev); |
Jean-Francois Moine | 6a70974 | 2008-09-03 16:48:10 -0300 | [diff] [blame] | 2287 | return 0; |
| 2288 | } |
| 2289 | EXPORT_SYMBOL(gspca_suspend); |
| 2290 | |
| 2291 | int gspca_resume(struct usb_interface *intf) |
| 2292 | { |
| 2293 | struct gspca_dev *gspca_dev = usb_get_intfdata(intf); |
| 2294 | |
| 2295 | gspca_dev->frozen = 0; |
Jean-Francois Moine | 012d6b0 | 2008-09-03 17:12:16 -0300 | [diff] [blame] | 2296 | gspca_dev->sd_desc->init(gspca_dev); |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 2297 | gspca_input_create_urb(gspca_dev); |
Jean-Francois Moine | 012d6b0 | 2008-09-03 17:12:16 -0300 | [diff] [blame] | 2298 | if (gspca_dev->streaming) |
| 2299 | return gspca_init_transfer(gspca_dev); |
Jean-Francois Moine | 1e89e2d | 2008-09-03 17:12:15 -0300 | [diff] [blame] | 2300 | return 0; |
Jean-Francois Moine | 6a70974 | 2008-09-03 16:48:10 -0300 | [diff] [blame] | 2301 | } |
| 2302 | EXPORT_SYMBOL(gspca_resume); |
| 2303 | #endif |
Hans de Goede | dcef323 | 2008-07-10 10:40:53 -0300 | [diff] [blame] | 2304 | /* -- cam driver utility functions -- */ |
| 2305 | |
| 2306 | /* auto gain and exposure algorithm based on the knee algorithm described here: |
| 2307 | http://ytse.tricolour.net/docs/LowLightOptimization.html |
| 2308 | |
| 2309 | Returns 0 if no changes were made, 1 if the gain and or exposure settings |
| 2310 | where changed. */ |
| 2311 | int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum, |
| 2312 | int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee) |
| 2313 | { |
| 2314 | int i, steps, gain, orig_gain, exposure, orig_exposure, autogain; |
| 2315 | const struct ctrl *gain_ctrl = NULL; |
| 2316 | const struct ctrl *exposure_ctrl = NULL; |
| 2317 | const struct ctrl *autogain_ctrl = NULL; |
| 2318 | int retval = 0; |
| 2319 | |
| 2320 | for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) { |
Hans de Goede | 26984b0 | 2010-02-01 13:18:37 -0300 | [diff] [blame] | 2321 | if (gspca_dev->ctrl_dis & (1 << i)) |
| 2322 | continue; |
Hans de Goede | dcef323 | 2008-07-10 10:40:53 -0300 | [diff] [blame] | 2323 | if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN) |
| 2324 | gain_ctrl = &gspca_dev->sd_desc->ctrls[i]; |
| 2325 | if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE) |
| 2326 | exposure_ctrl = &gspca_dev->sd_desc->ctrls[i]; |
| 2327 | if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN) |
| 2328 | autogain_ctrl = &gspca_dev->sd_desc->ctrls[i]; |
| 2329 | } |
| 2330 | if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) { |
| 2331 | PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called " |
| 2332 | "on cam without (auto)gain/exposure"); |
| 2333 | return 0; |
| 2334 | } |
| 2335 | |
| 2336 | if (gain_ctrl->get(gspca_dev, &gain) || |
| 2337 | exposure_ctrl->get(gspca_dev, &exposure) || |
| 2338 | autogain_ctrl->get(gspca_dev, &autogain) || !autogain) |
| 2339 | return 0; |
| 2340 | |
| 2341 | orig_gain = gain; |
| 2342 | orig_exposure = exposure; |
| 2343 | |
| 2344 | /* If we are of a multiple of deadzone, do multiple steps to reach the |
| 2345 | desired lumination fast (with the risc of a slight overshoot) */ |
| 2346 | steps = abs(desired_avg_lum - avg_lum) / deadzone; |
| 2347 | |
Jean-Francois Moine | 1c44d81 | 2008-11-10 05:56:55 -0300 | [diff] [blame] | 2348 | PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d", |
Hans de Goede | dcef323 | 2008-07-10 10:40:53 -0300 | [diff] [blame] | 2349 | avg_lum, desired_avg_lum, steps); |
| 2350 | |
| 2351 | for (i = 0; i < steps; i++) { |
| 2352 | if (avg_lum > desired_avg_lum) { |
| 2353 | if (gain > gain_knee) |
| 2354 | gain--; |
| 2355 | else if (exposure > exposure_knee) |
| 2356 | exposure--; |
| 2357 | else if (gain > gain_ctrl->qctrl.default_value) |
| 2358 | gain--; |
| 2359 | else if (exposure > exposure_ctrl->qctrl.minimum) |
| 2360 | exposure--; |
| 2361 | else if (gain > gain_ctrl->qctrl.minimum) |
| 2362 | gain--; |
| 2363 | else |
| 2364 | break; |
| 2365 | } else { |
| 2366 | if (gain < gain_ctrl->qctrl.default_value) |
| 2367 | gain++; |
| 2368 | else if (exposure < exposure_knee) |
| 2369 | exposure++; |
| 2370 | else if (gain < gain_knee) |
| 2371 | gain++; |
| 2372 | else if (exposure < exposure_ctrl->qctrl.maximum) |
| 2373 | exposure++; |
| 2374 | else if (gain < gain_ctrl->qctrl.maximum) |
| 2375 | gain++; |
| 2376 | else |
| 2377 | break; |
| 2378 | } |
| 2379 | } |
| 2380 | |
| 2381 | if (gain != orig_gain) { |
| 2382 | gain_ctrl->set(gspca_dev, gain); |
| 2383 | retval = 1; |
| 2384 | } |
| 2385 | if (exposure != orig_exposure) { |
| 2386 | exposure_ctrl->set(gspca_dev, exposure); |
| 2387 | retval = 1; |
| 2388 | } |
| 2389 | |
| 2390 | return retval; |
| 2391 | } |
| 2392 | EXPORT_SYMBOL(gspca_auto_gain_n_exposure); |
| 2393 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2394 | /* -- module insert / remove -- */ |
| 2395 | static int __init gspca_init(void) |
| 2396 | { |
Jean-Francois Moine | bb64e86 | 2008-07-19 06:49:28 -0300 | [diff] [blame] | 2397 | info("main v%d.%d.%d registered", |
| 2398 | (DRIVER_VERSION_NUMBER >> 16) & 0xff, |
| 2399 | (DRIVER_VERSION_NUMBER >> 8) & 0xff, |
| 2400 | DRIVER_VERSION_NUMBER & 0xff); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2401 | return 0; |
| 2402 | } |
| 2403 | static void __exit gspca_exit(void) |
| 2404 | { |
| 2405 | info("main deregistered"); |
| 2406 | } |
| 2407 | |
| 2408 | module_init(gspca_init); |
| 2409 | module_exit(gspca_exit); |
| 2410 | |
Jean-Francois Moine | 335b3f8 | 2008-07-30 04:53:02 -0300 | [diff] [blame] | 2411 | #ifdef GSPCA_DEBUG |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2412 | module_param_named(debug, gspca_debug, int, 0644); |
| 2413 | MODULE_PARM_DESC(debug, |
| 2414 | "Debug (bit) 0x01:error 0x02:probe 0x04:config" |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 2415 | " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout" |
| 2416 | " 0x0100: v4l2"); |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 2417 | #endif |