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