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