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