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