Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Main USB camera driver |
| 3 | * |
Jean-François Moine | 59929ce | 2011-03-14 05:33:01 -0300 | [diff] [blame] | 4 | * Copyright (C) 2008-2011 Jean-François Moine <http://moinejf.free.fr> |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 5 | * |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 6 | * Camera button input handling by Márton Németh |
| 7 | * Copyright (C) 2009-2010 Márton Németh <nm127@freemail.hu> |
| 8 | * |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 9 | * This program is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU General Public License as published by the |
| 11 | * Free Software Foundation; either version 2 of the License, or (at your |
| 12 | * option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 16 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 17 | * for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software Foundation, |
| 21 | * Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 22 | */ |
| 23 | |
Joe Perches | 133a9fe | 2011-08-21 19:56:57 -0300 | [diff] [blame] | 24 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 25 | |
Jean-François Moine | f22123f | 2011-09-23 05:17:10 -0300 | [diff] [blame] | 26 | #define GSPCA_VERSION "2.14.0" |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 27 | |
| 28 | #include <linux/init.h> |
| 29 | #include <linux/fs.h> |
| 30 | #include <linux/vmalloc.h> |
| 31 | #include <linux/sched.h> |
| 32 | #include <linux/slab.h> |
| 33 | #include <linux/mm.h> |
| 34 | #include <linux/string.h> |
| 35 | #include <linux/pagemap.h> |
Jean-Francois Moine | 956e42d | 2008-07-01 10:03:42 -0300 | [diff] [blame] | 36 | #include <linux/io.h> |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 37 | #include <asm/page.h> |
Jean-Francois Moine | 956e42d | 2008-07-01 10:03:42 -0300 | [diff] [blame] | 38 | #include <linux/uaccess.h> |
Paulo Assis | 2f51d00 | 2010-03-07 06:56:56 -0300 | [diff] [blame] | 39 | #include <linux/ktime.h> |
Hans Verkuil | 35ea11f | 2008-07-20 08:12:02 -0300 | [diff] [blame] | 40 | #include <media/v4l2-ioctl.h> |
Hans Verkuil | 62bba5d | 2012-05-06 09:28:17 -0300 | [diff] [blame] | 41 | #include <media/v4l2-ctrls.h> |
Hans Verkuil | 2333565 | 2012-05-06 09:28:19 -0300 | [diff] [blame] | 42 | #include <media/v4l2-fh.h> |
Hans Verkuil | d57fb9f | 2012-05-06 09:28:20 -0300 | [diff] [blame] | 43 | #include <media/v4l2-event.h> |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 44 | |
| 45 | #include "gspca.h" |
| 46 | |
Peter Senna Tschudin | 13a00fa | 2013-01-24 19:28:59 -0300 | [diff] [blame] | 47 | #if IS_ENABLED(CONFIG_INPUT) |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 48 | #include <linux/input.h> |
| 49 | #include <linux/usb/input.h> |
Jean-François Moine | e070bf9 | 2010-02-18 15:02:51 -0300 | [diff] [blame] | 50 | #endif |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 51 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 52 | /* global values */ |
Jean-Francois Moine | 8fee845 | 2009-01-17 04:46:38 -0300 | [diff] [blame] | 53 | #define DEF_NURBS 3 /* default number of URBs */ |
| 54 | #if DEF_NURBS > MAX_NURBS |
| 55 | #error "DEF_NURBS too big" |
| 56 | #endif |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 57 | |
Jean-François Moine | 27c6f9e | 2010-04-02 06:59:13 -0300 | [diff] [blame] | 58 | MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>"); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 59 | MODULE_DESCRIPTION("GSPCA USB Camera Driver"); |
| 60 | MODULE_LICENSE("GPL"); |
Jean-François Moine | f22123f | 2011-09-23 05:17:10 -0300 | [diff] [blame] | 61 | MODULE_VERSION(GSPCA_VERSION); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 62 | |
Theodore Kilgore | c93396e | 2013-02-04 13:17:55 -0300 | [diff] [blame] | 63 | int gspca_debug; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 64 | EXPORT_SYMBOL(gspca_debug); |
| 65 | |
Theodore Kilgore | c93396e | 2013-02-04 13:17:55 -0300 | [diff] [blame] | 66 | static void PDEBUG_MODE(struct gspca_dev *gspca_dev, int debug, char *txt, |
| 67 | __u32 pixfmt, int w, int h) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 68 | { |
| 69 | if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') { |
Theodore Kilgore | c93396e | 2013-02-04 13:17:55 -0300 | [diff] [blame] | 70 | PDEBUG(debug, "%s %c%c%c%c %dx%d", |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 71 | txt, |
| 72 | pixfmt & 0xff, |
| 73 | (pixfmt >> 8) & 0xff, |
| 74 | (pixfmt >> 16) & 0xff, |
| 75 | pixfmt >> 24, |
| 76 | w, h); |
| 77 | } else { |
Theodore Kilgore | c93396e | 2013-02-04 13:17:55 -0300 | [diff] [blame] | 78 | PDEBUG(debug, "%s 0x%08x %dx%d", |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 79 | txt, |
| 80 | pixfmt, |
| 81 | w, h); |
| 82 | } |
| 83 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 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 | */ |
Peter Senna Tschudin | 13a00fa | 2013-01-24 19:28:59 -0300 | [diff] [blame] | 118 | #if IS_ENABLED(CONFIG_INPUT) |
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) { |
Theodore Kilgore | c93396e | 2013-02-04 13:17:55 -0300 | [diff] [blame] | 129 | PERR("Unknown packet received"); |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 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: |
Theodore Kilgore | c93396e | 2013-02-04 13:17:55 -0300 | [diff] [blame] | 143 | PERR("URB error %i, resubmitting", urb->status); |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 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) { |
Theodore Kilgore | c93396e | 2013-02-04 13:17:55 -0300 | [diff] [blame] | 229 | PERR("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 |
Theodore Kilgore | c93396e | 2013-02-04 13:17:55 -0300 | [diff] [blame] | 315 | PERR("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 |
Theodore Kilgore | c93396e | 2013-02-04 13:17:55 -0300 | [diff] [blame] | 388 | PERR("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) { |
Theodore Kilgore | c93396e | 2013-02-04 13:17:55 -0300 | [diff] [blame] | 460 | PERR("frame overflow %d > %d", |
Jean-François Moine | b192ca9 | 2010-06-27 03:08:19 -0300 | [diff] [blame] | 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 | |
Ondrej Zary | 1966bc2 | 2013-08-30 17:54:23 -0300 | [diff] [blame] | 507 | frsz = gspca_dev->pixfmt.sizeimage; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 508 | PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 509 | frsz = PAGE_ALIGN(frsz); |
Jean-François Moine | f7059ea | 2010-07-06 04:32:27 -0300 | [diff] [blame] | 510 | if (count >= GSPCA_MAX_FRAMES) |
| 511 | count = GSPCA_MAX_FRAMES - 1; |
Jean-François Moine | e795d91 | 2010-06-24 05:14:22 -0300 | [diff] [blame] | 512 | gspca_dev->frbuf = vmalloc_32(frsz * count); |
Jean-Francois Moine | ac0d6df | 2008-07-08 06:58:15 -0300 | [diff] [blame] | 513 | if (!gspca_dev->frbuf) { |
Joe Perches | 133a9fe | 2011-08-21 19:56:57 -0300 | [diff] [blame] | 514 | pr_err("frame alloc failed\n"); |
Jean-Francois Moine | ac0d6df | 2008-07-08 06:58:15 -0300 | [diff] [blame] | 515 | return -ENOMEM; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 516 | } |
Hans de Goede | d642de2 | 2010-12-31 04:51:36 -0300 | [diff] [blame] | 517 | gspca_dev->capt_file = file; |
| 518 | gspca_dev->memory = memory; |
| 519 | gspca_dev->frsz = frsz; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 520 | gspca_dev->nframes = count; |
| 521 | for (i = 0; i < count; i++) { |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 522 | frame = &gspca_dev->frame[i]; |
| 523 | frame->v4l2_buf.index = i; |
| 524 | frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 525 | frame->v4l2_buf.flags = 0; |
| 526 | frame->v4l2_buf.field = V4L2_FIELD_NONE; |
| 527 | frame->v4l2_buf.length = frsz; |
Hans de Goede | d642de2 | 2010-12-31 04:51:36 -0300 | [diff] [blame] | 528 | frame->v4l2_buf.memory = memory; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 529 | frame->v4l2_buf.sequence = 0; |
Jean-François Moine | b192ca9 | 2010-06-27 03:08:19 -0300 | [diff] [blame] | 530 | frame->data = gspca_dev->frbuf + i * frsz; |
Jean-Francois Moine | ac0d6df | 2008-07-08 06:58:15 -0300 | [diff] [blame] | 531 | frame->v4l2_buf.m.offset = i * frsz; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 532 | } |
Jean-François Moine | f7059ea | 2010-07-06 04:32:27 -0300 | [diff] [blame] | 533 | atomic_set(&gspca_dev->fr_q, 0); |
| 534 | atomic_set(&gspca_dev->fr_i, 0); |
| 535 | gspca_dev->fr_o = 0; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 536 | return 0; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | static void frame_free(struct gspca_dev *gspca_dev) |
| 540 | { |
| 541 | int i; |
| 542 | |
| 543 | PDEBUG(D_STREAM, "frame free"); |
Harvey Harrison | efab821 | 2008-07-05 06:12:47 -0300 | [diff] [blame] | 544 | if (gspca_dev->frbuf != NULL) { |
Jean-François Moine | e795d91 | 2010-06-24 05:14:22 -0300 | [diff] [blame] | 545 | vfree(gspca_dev->frbuf); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 546 | gspca_dev->frbuf = NULL; |
| 547 | for (i = 0; i < gspca_dev->nframes; i++) |
| 548 | gspca_dev->frame[i].data = NULL; |
| 549 | } |
| 550 | gspca_dev->nframes = 0; |
Hans de Goede | d642de2 | 2010-12-31 04:51:36 -0300 | [diff] [blame] | 551 | gspca_dev->frsz = 0; |
| 552 | gspca_dev->capt_file = NULL; |
| 553 | gspca_dev->memory = GSPCA_MEMORY_NO; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 554 | } |
| 555 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 556 | static void destroy_urbs(struct gspca_dev *gspca_dev) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 557 | { |
| 558 | struct urb *urb; |
| 559 | unsigned int i; |
| 560 | |
| 561 | PDEBUG(D_STREAM, "kill transfer"); |
Erik Andrén | 4473281 | 2008-10-13 15:52:46 -0300 | [diff] [blame] | 562 | for (i = 0; i < MAX_NURBS; i++) { |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 563 | urb = gspca_dev->urb[i]; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 564 | if (urb == NULL) |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 565 | break; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 566 | |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 567 | gspca_dev->urb[i] = NULL; |
Jean-Francois Moine | 090264e | 2009-02-12 08:05:45 -0300 | [diff] [blame] | 568 | usb_kill_urb(urb); |
Wei Yongjun | 8b2c3da | 2013-03-20 11:30:46 -0300 | [diff] [blame] | 569 | usb_free_coherent(gspca_dev->dev, |
| 570 | urb->transfer_buffer_length, |
| 571 | urb->transfer_buffer, |
| 572 | urb->transfer_dma); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 573 | usb_free_urb(urb); |
| 574 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 575 | } |
| 576 | |
Jean-François Moine | e4dac28 | 2010-04-09 06:11:36 -0300 | [diff] [blame] | 577 | static int gspca_set_alt0(struct gspca_dev *gspca_dev) |
| 578 | { |
| 579 | int ret; |
| 580 | |
| 581 | if (gspca_dev->alt == 0) |
| 582 | return 0; |
| 583 | ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0); |
| 584 | if (ret < 0) |
Joe Perches | 133a9fe | 2011-08-21 19:56:57 -0300 | [diff] [blame] | 585 | pr_err("set alt 0 err %d\n", ret); |
Jean-François Moine | e4dac28 | 2010-04-09 06:11:36 -0300 | [diff] [blame] | 586 | return ret; |
| 587 | } |
| 588 | |
| 589 | /* Note: both the queue and the usb locks should be held when calling this */ |
| 590 | static void gspca_stream_off(struct gspca_dev *gspca_dev) |
| 591 | { |
| 592 | gspca_dev->streaming = 0; |
Hans de Goede | a3cc74d | 2012-05-07 06:44:21 -0300 | [diff] [blame] | 593 | gspca_dev->usb_err = 0; |
Hans de Goede | eb23873 | 2012-05-07 06:25:30 -0300 | [diff] [blame] | 594 | if (gspca_dev->sd_desc->stopN) |
| 595 | gspca_dev->sd_desc->stopN(gspca_dev); |
| 596 | destroy_urbs(gspca_dev); |
| 597 | gspca_input_destroy_urb(gspca_dev); |
| 598 | gspca_set_alt0(gspca_dev); |
| 599 | gspca_input_create_urb(gspca_dev); |
Jean-François Moine | e4dac28 | 2010-04-09 06:11:36 -0300 | [diff] [blame] | 600 | if (gspca_dev->sd_desc->stop0) |
| 601 | gspca_dev->sd_desc->stop0(gspca_dev); |
| 602 | PDEBUG(D_STREAM, "stream off OK"); |
| 603 | } |
| 604 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 605 | /* |
Antonio Ospite | 2fe1524 | 2014-06-25 06:27:56 -0300 | [diff] [blame] | 606 | * look for an input transfer endpoint in an alternate setting. |
| 607 | * |
| 608 | * If xfer_ep is invalid, return the first valid ep found, otherwise |
| 609 | * look for exactly the ep with address equal to xfer_ep. |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 610 | */ |
Jean-Francois Moine | 0be0100 | 2008-09-04 07:01:50 -0300 | [diff] [blame] | 611 | static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt, |
Antonio Ospite | 2fe1524 | 2014-06-25 06:27:56 -0300 | [diff] [blame] | 612 | int xfer, int xfer_ep) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 613 | { |
| 614 | struct usb_host_endpoint *ep; |
| 615 | int i, attr; |
| 616 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 617 | for (i = 0; i < alt->desc.bNumEndpoints; i++) { |
| 618 | ep = &alt->endpoint[i]; |
Jean-Francois Moine | 50e06de | 2008-12-31 08:13:46 -0300 | [diff] [blame] | 619 | attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK; |
Jean-Francois Moine | 30d35e4 | 2009-04-21 13:57:31 -0300 | [diff] [blame] | 620 | if (attr == xfer |
Patrice Chotard | 7106225 | 2011-03-21 16:58:56 -0300 | [diff] [blame] | 621 | && ep->desc.wMaxPacketSize != 0 |
Antonio Ospite | 2fe1524 | 2014-06-25 06:27:56 -0300 | [diff] [blame] | 622 | && usb_endpoint_dir_in(&ep->desc) |
| 623 | && (xfer_ep < 0 || ep->desc.bEndpointAddress == xfer_ep)) |
Jean-Francois Moine | 50e06de | 2008-12-31 08:13:46 -0300 | [diff] [blame] | 624 | return ep; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 625 | } |
| 626 | return NULL; |
| 627 | } |
| 628 | |
Jean-François Moine | 27b1202 | 2011-08-10 07:38:48 -0300 | [diff] [blame] | 629 | /* compute the minimum bandwidth for the current transfer */ |
| 630 | static u32 which_bandwidth(struct gspca_dev *gspca_dev) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 631 | { |
Jean-François Moine | 27b1202 | 2011-08-10 07:38:48 -0300 | [diff] [blame] | 632 | u32 bandwidth; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 633 | |
Jean-François Moine | 18bef42 | 2012-01-04 13:44:02 -0300 | [diff] [blame] | 634 | /* get the (max) image size */ |
Ondrej Zary | 1966bc2 | 2013-08-30 17:54:23 -0300 | [diff] [blame] | 635 | bandwidth = gspca_dev->pixfmt.sizeimage; |
Jean-François Moine | 27b1202 | 2011-08-10 07:38:48 -0300 | [diff] [blame] | 636 | |
Jean-François Moine | 18bef42 | 2012-01-04 13:44:02 -0300 | [diff] [blame] | 637 | /* if the image is compressed, estimate its mean size */ |
Hans de Goede | eb3fb7c | 2012-01-01 16:35:01 -0300 | [diff] [blame] | 638 | if (!gspca_dev->cam.needs_full_bandwidth && |
Ondrej Zary | 1966bc2 | 2013-08-30 17:54:23 -0300 | [diff] [blame] | 639 | bandwidth < gspca_dev->pixfmt.width * |
| 640 | gspca_dev->pixfmt.height) |
Jean-François Moine | 18bef42 | 2012-01-04 13:44:02 -0300 | [diff] [blame] | 641 | bandwidth = bandwidth * 3 / 8; /* 0.375 */ |
Jean-François Moine | 27b1202 | 2011-08-10 07:38:48 -0300 | [diff] [blame] | 642 | |
| 643 | /* estimate the frame rate */ |
| 644 | if (gspca_dev->sd_desc->get_streamparm) { |
| 645 | struct v4l2_streamparm parm; |
| 646 | |
Jean-François Moine | 27b1202 | 2011-08-10 07:38:48 -0300 | [diff] [blame] | 647 | gspca_dev->sd_desc->get_streamparm(gspca_dev, &parm); |
| 648 | bandwidth *= parm.parm.capture.timeperframe.denominator; |
Hans de Goede | 1153f04 | 2011-12-30 09:13:07 -0300 | [diff] [blame] | 649 | bandwidth /= parm.parm.capture.timeperframe.numerator; |
Hans de Goede | a511ba9 | 2009-10-16 07:13:07 -0300 | [diff] [blame] | 650 | } else { |
Jean-François Moine | 18bef42 | 2012-01-04 13:44:02 -0300 | [diff] [blame] | 651 | |
| 652 | /* don't hope more than 15 fps with USB 1.1 and |
| 653 | * image resolution >= 640x480 */ |
Ondrej Zary | 1966bc2 | 2013-08-30 17:54:23 -0300 | [diff] [blame] | 654 | if (gspca_dev->pixfmt.width >= 640 |
Jean-François Moine | 18bef42 | 2012-01-04 13:44:02 -0300 | [diff] [blame] | 655 | && gspca_dev->dev->speed == USB_SPEED_FULL) |
| 656 | bandwidth *= 15; /* 15 fps */ |
| 657 | else |
| 658 | bandwidth *= 30; /* 30 fps */ |
Jean-François Moine | 27b1202 | 2011-08-10 07:38:48 -0300 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | PDEBUG(D_STREAM, "min bandwidth: %d", bandwidth); |
| 662 | return bandwidth; |
| 663 | } |
| 664 | |
| 665 | /* endpoint table */ |
| 666 | #define MAX_ALT 16 |
| 667 | struct ep_tb_s { |
| 668 | u32 alt; |
| 669 | u32 bandwidth; |
| 670 | }; |
| 671 | |
| 672 | /* |
| 673 | * build the table of the endpoints |
| 674 | * and compute the minimum bandwidth for the image transfer |
| 675 | */ |
Hans de Goede | 66957b8 | 2011-12-30 07:58:10 -0300 | [diff] [blame] | 676 | static int build_isoc_ep_tb(struct gspca_dev *gspca_dev, |
Jean-François Moine | 27b1202 | 2011-08-10 07:38:48 -0300 | [diff] [blame] | 677 | struct usb_interface *intf, |
Jean-François Moine | 27b1202 | 2011-08-10 07:38:48 -0300 | [diff] [blame] | 678 | struct ep_tb_s *ep_tb) |
| 679 | { |
| 680 | struct usb_host_endpoint *ep; |
| 681 | int i, j, nbalt, psize, found; |
| 682 | u32 bandwidth, last_bw; |
| 683 | |
| 684 | nbalt = intf->num_altsetting; |
| 685 | if (nbalt > MAX_ALT) |
| 686 | nbalt = MAX_ALT; /* fixme: should warn */ |
| 687 | |
| 688 | /* build the endpoint table */ |
| 689 | i = 0; |
| 690 | last_bw = 0; |
| 691 | for (;;) { |
| 692 | ep_tb->bandwidth = 2000 * 2000 * 120; |
| 693 | found = 0; |
| 694 | for (j = 0; j < nbalt; j++) { |
Hans de Goede | 66957b8 | 2011-12-30 07:58:10 -0300 | [diff] [blame] | 695 | ep = alt_xfer(&intf->altsetting[j], |
Antonio Ospite | 2fe1524 | 2014-06-25 06:27:56 -0300 | [diff] [blame] | 696 | USB_ENDPOINT_XFER_ISOC, |
| 697 | gspca_dev->xfer_ep); |
Jean-François Moine | 27b1202 | 2011-08-10 07:38:48 -0300 | [diff] [blame] | 698 | if (ep == NULL) |
| 699 | continue; |
Hans de Goede | d0d3435 | 2011-12-30 08:20:50 -0300 | [diff] [blame] | 700 | if (ep->desc.bInterval == 0) { |
| 701 | pr_err("alt %d iso endp with 0 interval\n", j); |
| 702 | continue; |
| 703 | } |
Jean-François Moine | 27b1202 | 2011-08-10 07:38:48 -0300 | [diff] [blame] | 704 | psize = le16_to_cpu(ep->desc.wMaxPacketSize); |
Hans de Goede | 66957b8 | 2011-12-30 07:58:10 -0300 | [diff] [blame] | 705 | psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3)); |
Hans de Goede | d0d3435 | 2011-12-30 08:20:50 -0300 | [diff] [blame] | 706 | bandwidth = psize * 1000; |
Jean-François Moine | 27b1202 | 2011-08-10 07:38:48 -0300 | [diff] [blame] | 707 | if (gspca_dev->dev->speed == USB_SPEED_HIGH |
| 708 | || gspca_dev->dev->speed == USB_SPEED_SUPER) |
| 709 | bandwidth *= 8; |
Hans de Goede | d0d3435 | 2011-12-30 08:20:50 -0300 | [diff] [blame] | 710 | bandwidth /= 1 << (ep->desc.bInterval - 1); |
Jean-François Moine | 27b1202 | 2011-08-10 07:38:48 -0300 | [diff] [blame] | 711 | if (bandwidth <= last_bw) |
| 712 | continue; |
| 713 | if (bandwidth < ep_tb->bandwidth) { |
| 714 | ep_tb->bandwidth = bandwidth; |
| 715 | ep_tb->alt = j; |
| 716 | found = 1; |
| 717 | } |
Hans de Goede | a511ba9 | 2009-10-16 07:13:07 -0300 | [diff] [blame] | 718 | } |
Jean-François Moine | 27b1202 | 2011-08-10 07:38:48 -0300 | [diff] [blame] | 719 | if (!found) |
| 720 | break; |
| 721 | PDEBUG(D_STREAM, "alt %d bandwidth %d", |
| 722 | ep_tb->alt, ep_tb->bandwidth); |
| 723 | last_bw = ep_tb->bandwidth; |
| 724 | i++; |
| 725 | ep_tb++; |
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 | |
Hans de Goede | 2d39059 | 2011-12-30 18:26:53 -0300 | [diff] [blame] | 728 | /* |
| 729 | * If the camera: |
| 730 | * has a usb audio class interface (a built in usb mic); and |
| 731 | * is a usb 1 full speed device; and |
| 732 | * uses the max full speed iso bandwidth; and |
| 733 | * and has more than 1 alt setting |
| 734 | * then skip the highest alt setting to spare bandwidth for the mic |
| 735 | */ |
| 736 | if (gspca_dev->audio && |
| 737 | gspca_dev->dev->speed == USB_SPEED_FULL && |
| 738 | last_bw >= 1000000 && |
| 739 | i > 1) { |
| 740 | PDEBUG(D_STREAM, "dev has usb audio, skipping highest alt"); |
| 741 | i--; |
| 742 | ep_tb--; |
| 743 | } |
| 744 | |
Jean-François Moine | 27b1202 | 2011-08-10 07:38:48 -0300 | [diff] [blame] | 745 | /* get the requested bandwidth and start at the highest atlsetting */ |
| 746 | bandwidth = which_bandwidth(gspca_dev); |
| 747 | ep_tb--; |
| 748 | while (i > 1) { |
| 749 | ep_tb--; |
| 750 | if (ep_tb->bandwidth < bandwidth) |
| 751 | break; |
| 752 | i--; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 753 | } |
Jean-François Moine | 27b1202 | 2011-08-10 07:38:48 -0300 | [diff] [blame] | 754 | return i; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | /* |
Jean-Francois Moine | 0be0100 | 2008-09-04 07:01:50 -0300 | [diff] [blame] | 758 | * create the URBs for image transfer |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 759 | */ |
| 760 | static int create_urbs(struct gspca_dev *gspca_dev, |
| 761 | struct usb_host_endpoint *ep) |
| 762 | { |
| 763 | struct urb *urb; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 764 | int n, nurbs, i, psize, npkt, bsize; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 765 | |
| 766 | /* calculate the packet size and the number of packets */ |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 767 | psize = le16_to_cpu(ep->desc.wMaxPacketSize); |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 768 | |
Jean-Francois Moine | 6929dc6 | 2009-04-21 13:45:56 -0300 | [diff] [blame] | 769 | if (!gspca_dev->cam.bulk) { /* isoc */ |
Jean-Francois Moine | 95d9142 | 2008-09-29 05:57:32 -0300 | [diff] [blame] | 770 | |
| 771 | /* 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] | 772 | if (gspca_dev->pkt_size == 0) |
| 773 | psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3)); |
| 774 | else |
| 775 | psize = gspca_dev->pkt_size; |
Jean-Francois Moine | 49cb6b0 | 2009-04-25 13:29:01 -0300 | [diff] [blame] | 776 | npkt = gspca_dev->cam.npkt; |
| 777 | if (npkt == 0) |
| 778 | npkt = 32; /* default value */ |
Jean-Francois Moine | 0be0100 | 2008-09-04 07:01:50 -0300 | [diff] [blame] | 779 | bsize = psize * npkt; |
| 780 | PDEBUG(D_STREAM, |
| 781 | "isoc %d pkts size %d = bsize:%d", |
| 782 | npkt, psize, bsize); |
Jean-Francois Moine | 51977a8 | 2008-09-08 03:22:42 -0300 | [diff] [blame] | 783 | nurbs = DEF_NURBS; |
Jean-Francois Moine | 95d9142 | 2008-09-29 05:57:32 -0300 | [diff] [blame] | 784 | } else { /* bulk */ |
Jean-Francois Moine | 0be0100 | 2008-09-04 07:01:50 -0300 | [diff] [blame] | 785 | npkt = 0; |
Antonio Ospite | dff369a | 2008-11-14 07:53:32 -0300 | [diff] [blame] | 786 | bsize = gspca_dev->cam.bulk_size; |
Jean-Francois Moine | 95d9142 | 2008-09-29 05:57:32 -0300 | [diff] [blame] | 787 | if (bsize == 0) |
| 788 | bsize = psize; |
Jean-Francois Moine | 0be0100 | 2008-09-04 07:01:50 -0300 | [diff] [blame] | 789 | PDEBUG(D_STREAM, "bulk bsize:%d", bsize); |
Antonio Ospite | dff369a | 2008-11-14 07:53:32 -0300 | [diff] [blame] | 790 | if (gspca_dev->cam.bulk_nurbs != 0) |
| 791 | nurbs = gspca_dev->cam.bulk_nurbs; |
| 792 | else |
| 793 | nurbs = 1; |
Jean-Francois Moine | 0be0100 | 2008-09-04 07:01:50 -0300 | [diff] [blame] | 794 | } |
| 795 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 796 | for (n = 0; n < nurbs; n++) { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 797 | urb = usb_alloc_urb(npkt, GFP_KERNEL); |
| 798 | if (!urb) { |
Joe Perches | 133a9fe | 2011-08-21 19:56:57 -0300 | [diff] [blame] | 799 | pr_err("usb_alloc_urb failed\n"); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 800 | return -ENOMEM; |
| 801 | } |
Jean-Francois Moine | db87087 | 2009-12-15 05:16:04 -0300 | [diff] [blame] | 802 | gspca_dev->urb[n] = urb; |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 803 | urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 804 | bsize, |
| 805 | GFP_KERNEL, |
| 806 | &urb->transfer_dma); |
| 807 | |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 808 | if (urb->transfer_buffer == NULL) { |
Joe Perches | 133a9fe | 2011-08-21 19:56:57 -0300 | [diff] [blame] | 809 | pr_err("usb_alloc_coherent failed\n"); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 810 | return -ENOMEM; |
| 811 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 812 | urb->dev = gspca_dev->dev; |
| 813 | urb->context = gspca_dev; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 814 | urb->transfer_buffer_length = bsize; |
Jean-Francois Moine | 0be0100 | 2008-09-04 07:01:50 -0300 | [diff] [blame] | 815 | if (npkt != 0) { /* ISOC */ |
| 816 | urb->pipe = usb_rcvisocpipe(gspca_dev->dev, |
| 817 | ep->desc.bEndpointAddress); |
Frank Zago | 6c49da7 | 2008-09-28 07:43:00 -0300 | [diff] [blame] | 818 | urb->transfer_flags = URB_ISO_ASAP |
| 819 | | URB_NO_TRANSFER_DMA_MAP; |
Hans de Goede | 51e23be | 2012-01-01 13:34:58 -0300 | [diff] [blame] | 820 | urb->interval = 1 << (ep->desc.bInterval - 1); |
Jean-Francois Moine | 0be0100 | 2008-09-04 07:01:50 -0300 | [diff] [blame] | 821 | urb->complete = isoc_irq; |
| 822 | urb->number_of_packets = npkt; |
| 823 | for (i = 0; i < npkt; i++) { |
| 824 | urb->iso_frame_desc[i].length = psize; |
| 825 | urb->iso_frame_desc[i].offset = psize * i; |
| 826 | } |
| 827 | } else { /* bulk */ |
| 828 | urb->pipe = usb_rcvbulkpipe(gspca_dev->dev, |
Jean-François Moine | a23c194 | 2010-10-29 07:57:03 -0300 | [diff] [blame] | 829 | ep->desc.bEndpointAddress); |
Frank Zago | 6c49da7 | 2008-09-28 07:43:00 -0300 | [diff] [blame] | 830 | urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; |
Jean-Francois Moine | 0be0100 | 2008-09-04 07:01:50 -0300 | [diff] [blame] | 831 | urb->complete = bulk_irq; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 832 | } |
| 833 | } |
| 834 | return 0; |
| 835 | } |
| 836 | |
| 837 | /* |
| 838 | * start the USB transfer |
| 839 | */ |
| 840 | static int gspca_init_transfer(struct gspca_dev *gspca_dev) |
| 841 | { |
Jean-François Moine | 27b1202 | 2011-08-10 07:38:48 -0300 | [diff] [blame] | 842 | struct usb_interface *intf; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 843 | struct usb_host_endpoint *ep; |
Jean-Francois Moine | db87087 | 2009-12-15 05:16:04 -0300 | [diff] [blame] | 844 | struct urb *urb; |
Jean-François Moine | 27b1202 | 2011-08-10 07:38:48 -0300 | [diff] [blame] | 845 | struct ep_tb_s ep_tb[MAX_ALT]; |
| 846 | int n, ret, xfer, alt, alt_idx; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 847 | |
Jean-François Moine | f7059ea | 2010-07-06 04:32:27 -0300 | [diff] [blame] | 848 | /* reset the streaming variables */ |
| 849 | gspca_dev->image = NULL; |
| 850 | gspca_dev->image_len = 0; |
| 851 | gspca_dev->last_packet_type = DISCARD_PACKET; |
| 852 | gspca_dev->sequence = 0; |
| 853 | |
Jean-Francois Moine | ab6c8cf | 2010-01-13 15:34:27 -0300 | [diff] [blame] | 854 | gspca_dev->usb_err = 0; |
| 855 | |
Jean-François Moine | 27b1202 | 2011-08-10 07:38:48 -0300 | [diff] [blame] | 856 | /* do the specific subdriver stuff before endpoint selection */ |
Hans de Goede | 47faa6f | 2011-12-29 16:43:12 -0300 | [diff] [blame] | 857 | intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface); |
| 858 | gspca_dev->alt = gspca_dev->cam.bulk ? intf->num_altsetting : 0; |
Jean-Francois Moine | 1f53b0b | 2009-06-30 07:07:01 -0300 | [diff] [blame] | 859 | if (gspca_dev->sd_desc->isoc_init) { |
| 860 | ret = gspca_dev->sd_desc->isoc_init(gspca_dev); |
| 861 | if (ret < 0) |
Hans Verkuil | a3d6e8c | 2012-05-06 09:28:27 -0300 | [diff] [blame] | 862 | return ret; |
Jean-Francois Moine | 1f53b0b | 2009-06-30 07:07:01 -0300 | [diff] [blame] | 863 | } |
Jean-François Moine | 27b1202 | 2011-08-10 07:38:48 -0300 | [diff] [blame] | 864 | xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK |
| 865 | : USB_ENDPOINT_XFER_ISOC; |
Hans de Goede | 314b3e0 | 2010-10-26 05:15:46 -0300 | [diff] [blame] | 866 | |
Hans de Goede | 66957b8 | 2011-12-30 07:58:10 -0300 | [diff] [blame] | 867 | /* if bulk or the subdriver forced an altsetting, get the endpoint */ |
Jean-François Moine | 27b1202 | 2011-08-10 07:38:48 -0300 | [diff] [blame] | 868 | if (gspca_dev->alt != 0) { |
| 869 | gspca_dev->alt--; /* (previous version compatibility) */ |
Antonio Ospite | 2fe1524 | 2014-06-25 06:27:56 -0300 | [diff] [blame] | 870 | ep = alt_xfer(&intf->altsetting[gspca_dev->alt], xfer, |
| 871 | gspca_dev->xfer_ep); |
Jean-François Moine | 27b1202 | 2011-08-10 07:38:48 -0300 | [diff] [blame] | 872 | if (ep == NULL) { |
| 873 | pr_err("bad altsetting %d\n", gspca_dev->alt); |
Hans Verkuil | a3d6e8c | 2012-05-06 09:28:27 -0300 | [diff] [blame] | 874 | return -EIO; |
Jean-François Moine | 27b1202 | 2011-08-10 07:38:48 -0300 | [diff] [blame] | 875 | } |
| 876 | ep_tb[0].alt = gspca_dev->alt; |
| 877 | alt_idx = 1; |
| 878 | } else { |
Antonio Ospite | 59234e5 | 2014-06-04 09:03:39 -0300 | [diff] [blame] | 879 | /* else, compute the minimum bandwidth |
| 880 | * and build the endpoint table */ |
Hans de Goede | 66957b8 | 2011-12-30 07:58:10 -0300 | [diff] [blame] | 881 | alt_idx = build_isoc_ep_tb(gspca_dev, intf, ep_tb); |
Jean-François Moine | 27b1202 | 2011-08-10 07:38:48 -0300 | [diff] [blame] | 882 | if (alt_idx <= 0) { |
| 883 | pr_err("no transfer endpoint found\n"); |
Hans Verkuil | a3d6e8c | 2012-05-06 09:28:27 -0300 | [diff] [blame] | 884 | return -EIO; |
Jean-François Moine | 27b1202 | 2011-08-10 07:38:48 -0300 | [diff] [blame] | 885 | } |
Jean-Francois Moine | 1f53b0b | 2009-06-30 07:07:01 -0300 | [diff] [blame] | 886 | } |
Jean-François Moine | 27b1202 | 2011-08-10 07:38:48 -0300 | [diff] [blame] | 887 | |
| 888 | /* set the highest alternate setting and |
| 889 | * loop until urb submit succeeds */ |
| 890 | gspca_input_destroy_urb(gspca_dev); |
| 891 | |
| 892 | gspca_dev->alt = ep_tb[--alt_idx].alt; |
| 893 | alt = -1; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 894 | for (;;) { |
Jean-François Moine | 27b1202 | 2011-08-10 07:38:48 -0300 | [diff] [blame] | 895 | if (alt != gspca_dev->alt) { |
| 896 | alt = gspca_dev->alt; |
Jean-François Moine | 6121ca5 | 2011-11-30 05:54:16 -0300 | [diff] [blame] | 897 | if (intf->num_altsetting > 1) { |
Jean-François Moine | 27b1202 | 2011-08-10 07:38:48 -0300 | [diff] [blame] | 898 | ret = usb_set_interface(gspca_dev->dev, |
| 899 | gspca_dev->iface, |
| 900 | alt); |
| 901 | if (ret < 0) { |
Jean-François Moine | 4274d84 | 2011-08-10 07:40:47 -0300 | [diff] [blame] | 902 | if (ret == -ENOSPC) |
| 903 | goto retry; /*fixme: ugly*/ |
Jean-François Moine | 27b1202 | 2011-08-10 07:38:48 -0300 | [diff] [blame] | 904 | pr_err("set alt %d err %d\n", alt, ret); |
| 905 | goto out; |
| 906 | } |
| 907 | } |
| 908 | } |
Jean-Francois Moine | 47aaca9 | 2009-12-15 05:23:04 -0300 | [diff] [blame] | 909 | if (!gspca_dev->cam.no_urb_create) { |
Jean-François Moine | 27b1202 | 2011-08-10 07:38:48 -0300 | [diff] [blame] | 910 | PDEBUG(D_STREAM, "init transfer alt %d", alt); |
| 911 | ret = create_urbs(gspca_dev, |
Antonio Ospite | 2fe1524 | 2014-06-25 06:27:56 -0300 | [diff] [blame] | 912 | alt_xfer(&intf->altsetting[alt], xfer, |
| 913 | gspca_dev->xfer_ep)); |
Jean-Francois Moine | 47aaca9 | 2009-12-15 05:23:04 -0300 | [diff] [blame] | 914 | if (ret < 0) { |
| 915 | destroy_urbs(gspca_dev); |
| 916 | goto out; |
| 917 | } |
Jean-Francois Moine | db87087 | 2009-12-15 05:16:04 -0300 | [diff] [blame] | 918 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 919 | |
Jean-Francois Moine | 8fe2f1d | 2008-10-22 14:51:56 -0300 | [diff] [blame] | 920 | /* clear the bulk endpoint */ |
Jean-Francois Moine | 6929dc6 | 2009-04-21 13:45:56 -0300 | [diff] [blame] | 921 | if (gspca_dev->cam.bulk) |
Jean-Francois Moine | 8fe2f1d | 2008-10-22 14:51:56 -0300 | [diff] [blame] | 922 | usb_clear_halt(gspca_dev->dev, |
Jean-Francois Moine | 50e06de | 2008-12-31 08:13:46 -0300 | [diff] [blame] | 923 | gspca_dev->urb[0]->pipe); |
Jean-Francois Moine | 8fe2f1d | 2008-10-22 14:51:56 -0300 | [diff] [blame] | 924 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 925 | /* start the cam */ |
Jean-Francois Moine | 72ab97c | 2008-09-20 06:39:08 -0300 | [diff] [blame] | 926 | ret = gspca_dev->sd_desc->start(gspca_dev); |
| 927 | if (ret < 0) { |
| 928 | destroy_urbs(gspca_dev); |
| 929 | goto out; |
| 930 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 931 | gspca_dev->streaming = 1; |
Hans Verkuil | 82b343b | 2012-06-15 05:24:26 -0300 | [diff] [blame] | 932 | v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 933 | |
Antonio Ospite | dff369a | 2008-11-14 07:53:32 -0300 | [diff] [blame] | 934 | /* some bulk transfers are started by the subdriver */ |
Jean-Francois Moine | 6929dc6 | 2009-04-21 13:45:56 -0300 | [diff] [blame] | 935 | if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0) |
Jean-Francois Moine | 51977a8 | 2008-09-08 03:22:42 -0300 | [diff] [blame] | 936 | break; |
| 937 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 938 | /* submit the URBs */ |
Jean-Francois Moine | db87087 | 2009-12-15 05:16:04 -0300 | [diff] [blame] | 939 | for (n = 0; n < MAX_NURBS; n++) { |
| 940 | urb = gspca_dev->urb[n]; |
| 941 | if (urb == NULL) |
| 942 | break; |
| 943 | ret = usb_submit_urb(urb, GFP_KERNEL); |
Jean-Francois Moine | 1f53b0b | 2009-06-30 07:07:01 -0300 | [diff] [blame] | 944 | if (ret < 0) |
| 945 | break; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 946 | } |
| 947 | if (ret >= 0) |
Jean-François Moine | 27b1202 | 2011-08-10 07:38:48 -0300 | [diff] [blame] | 948 | break; /* transfer is started */ |
| 949 | |
| 950 | /* something when wrong |
| 951 | * stop the webcam and free the transfer resources */ |
Jean-François Moine | e4dac28 | 2010-04-09 06:11:36 -0300 | [diff] [blame] | 952 | gspca_stream_off(gspca_dev); |
Jean-Francois Moine | 4bdf4a8 | 2009-11-10 14:49:43 -0300 | [diff] [blame] | 953 | if (ret != -ENOSPC) { |
Joe Perches | 133a9fe | 2011-08-21 19:56:57 -0300 | [diff] [blame] | 954 | pr_err("usb_submit_urb alt %d err %d\n", |
| 955 | gspca_dev->alt, ret); |
Jean-Francois Moine | 1f53b0b | 2009-06-30 07:07:01 -0300 | [diff] [blame] | 956 | goto out; |
Jean-Francois Moine | 4bdf4a8 | 2009-11-10 14:49:43 -0300 | [diff] [blame] | 957 | } |
Jean-Francois Moine | 1f53b0b | 2009-06-30 07:07:01 -0300 | [diff] [blame] | 958 | |
| 959 | /* the bandwidth is not wide enough |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 960 | * negotiate or try a lower alternate setting */ |
Jean-François Moine | 4274d84 | 2011-08-10 07:40:47 -0300 | [diff] [blame] | 961 | retry: |
Theodore Kilgore | c93396e | 2013-02-04 13:17:55 -0300 | [diff] [blame] | 962 | PERR("alt %d - bandwidth not wide enough, trying again", alt); |
Jean-Francois Moine | 1f53b0b | 2009-06-30 07:07:01 -0300 | [diff] [blame] | 963 | msleep(20); /* wait for kill complete */ |
| 964 | if (gspca_dev->sd_desc->isoc_nego) { |
| 965 | ret = gspca_dev->sd_desc->isoc_nego(gspca_dev); |
| 966 | if (ret < 0) |
| 967 | goto out; |
| 968 | } else { |
Jean-François Moine | 27b1202 | 2011-08-10 07:38:48 -0300 | [diff] [blame] | 969 | if (alt_idx <= 0) { |
| 970 | pr_err("no transfer endpoint found\n"); |
Jean-Francois Moine | 1f53b0b | 2009-06-30 07:07:01 -0300 | [diff] [blame] | 971 | ret = -EIO; |
| 972 | goto out; |
| 973 | } |
Mauro Carvalho Chehab | ac97ecc | 2011-12-31 11:32:03 -0200 | [diff] [blame] | 974 | gspca_dev->alt = ep_tb[--alt_idx].alt; |
Jean-Francois Moine | 1f53b0b | 2009-06-30 07:07:01 -0300 | [diff] [blame] | 975 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 976 | } |
| 977 | out: |
Hans de Goede | 314b3e0 | 2010-10-26 05:15:46 -0300 | [diff] [blame] | 978 | gspca_input_create_urb(gspca_dev); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 979 | return ret; |
| 980 | } |
| 981 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 982 | static void gspca_set_default_mode(struct gspca_dev *gspca_dev) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 983 | { |
| 984 | int i; |
| 985 | |
| 986 | i = gspca_dev->cam.nmodes - 1; /* take the highest mode */ |
| 987 | gspca_dev->curr_mode = i; |
Ondrej Zary | 1966bc2 | 2013-08-30 17:54:23 -0300 | [diff] [blame] | 988 | gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i]; |
Jean-François Moine | 6a33091 | 2010-10-02 04:12:25 -0300 | [diff] [blame] | 989 | |
Hans Verkuil | 62bba5d | 2012-05-06 09:28:17 -0300 | [diff] [blame] | 990 | /* does nothing if ctrl_handler == NULL */ |
| 991 | v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | static int wxh_to_mode(struct gspca_dev *gspca_dev, |
| 995 | int width, int height) |
| 996 | { |
| 997 | int i; |
| 998 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 999 | for (i = gspca_dev->cam.nmodes; --i > 0; ) { |
| 1000 | if (width >= gspca_dev->cam.cam_mode[i].width |
| 1001 | && height >= gspca_dev->cam.cam_mode[i].height) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1002 | break; |
| 1003 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1004 | return i; |
| 1005 | } |
| 1006 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1007 | /* |
| 1008 | * search a mode with the right pixel format |
| 1009 | */ |
| 1010 | static int gspca_get_mode(struct gspca_dev *gspca_dev, |
| 1011 | int mode, |
| 1012 | int pixfmt) |
| 1013 | { |
| 1014 | int modeU, modeD; |
| 1015 | |
| 1016 | modeU = modeD = mode; |
| 1017 | while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) { |
| 1018 | if (--modeD >= 0) { |
Jean-Francois Moine | c2446b3 | 2008-07-05 11:49:20 -0300 | [diff] [blame] | 1019 | if (gspca_dev->cam.cam_mode[modeD].pixelformat |
| 1020 | == pixfmt) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1021 | return modeD; |
| 1022 | } |
| 1023 | if (++modeU < gspca_dev->cam.nmodes) { |
Jean-Francois Moine | c2446b3 | 2008-07-05 11:49:20 -0300 | [diff] [blame] | 1024 | if (gspca_dev->cam.cam_mode[modeU].pixelformat |
| 1025 | == pixfmt) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1026 | return modeU; |
| 1027 | } |
| 1028 | } |
| 1029 | return -EINVAL; |
| 1030 | } |
| 1031 | |
Brian Johnson | af1d9af | 2009-07-19 05:29:20 -0300 | [diff] [blame] | 1032 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
Hans Verkuil | b1c85cc | 2013-05-29 06:59:42 -0300 | [diff] [blame] | 1033 | static int vidioc_g_chip_info(struct file *file, void *priv, |
| 1034 | struct v4l2_dbg_chip_info *chip) |
| 1035 | { |
| 1036 | struct gspca_dev *gspca_dev = video_drvdata(file); |
| 1037 | |
| 1038 | gspca_dev->usb_err = 0; |
| 1039 | if (gspca_dev->sd_desc->get_chip_info) |
| 1040 | return gspca_dev->sd_desc->get_chip_info(gspca_dev, chip); |
| 1041 | return chip->match.addr ? -EINVAL : 0; |
| 1042 | } |
| 1043 | |
Brian Johnson | af1d9af | 2009-07-19 05:29:20 -0300 | [diff] [blame] | 1044 | static int vidioc_g_register(struct file *file, void *priv, |
Hans Verkuil | b1c85cc | 2013-05-29 06:59:42 -0300 | [diff] [blame] | 1045 | struct v4l2_dbg_register *reg) |
Brian Johnson | af1d9af | 2009-07-19 05:29:20 -0300 | [diff] [blame] | 1046 | { |
Hans Verkuil | f4c1605 | 2012-05-06 09:28:18 -0300 | [diff] [blame] | 1047 | struct gspca_dev *gspca_dev = video_drvdata(file); |
Brian Johnson | af1d9af | 2009-07-19 05:29:20 -0300 | [diff] [blame] | 1048 | |
Jean-Francois Moine | 8c4ebae | 2009-12-02 07:14:33 -0300 | [diff] [blame] | 1049 | gspca_dev->usb_err = 0; |
Hans Verkuil | a3d6e8c | 2012-05-06 09:28:27 -0300 | [diff] [blame] | 1050 | return gspca_dev->sd_desc->get_register(gspca_dev, reg); |
Brian Johnson | af1d9af | 2009-07-19 05:29:20 -0300 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | static int vidioc_s_register(struct file *file, void *priv, |
Hans Verkuil | b1c85cc | 2013-05-29 06:59:42 -0300 | [diff] [blame] | 1054 | const struct v4l2_dbg_register *reg) |
Brian Johnson | af1d9af | 2009-07-19 05:29:20 -0300 | [diff] [blame] | 1055 | { |
Hans Verkuil | f4c1605 | 2012-05-06 09:28:18 -0300 | [diff] [blame] | 1056 | struct gspca_dev *gspca_dev = video_drvdata(file); |
Brian Johnson | af1d9af | 2009-07-19 05:29:20 -0300 | [diff] [blame] | 1057 | |
Jean-Francois Moine | 8c4ebae | 2009-12-02 07:14:33 -0300 | [diff] [blame] | 1058 | gspca_dev->usb_err = 0; |
Hans Verkuil | a3d6e8c | 2012-05-06 09:28:27 -0300 | [diff] [blame] | 1059 | return gspca_dev->sd_desc->set_register(gspca_dev, reg); |
Brian Johnson | af1d9af | 2009-07-19 05:29:20 -0300 | [diff] [blame] | 1060 | } |
| 1061 | #endif |
| 1062 | |
Mauro Carvalho Chehab | e078770 | 2008-07-01 04:06:22 -0300 | [diff] [blame] | 1063 | 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] | 1064 | struct v4l2_fmtdesc *fmtdesc) |
| 1065 | { |
Hans Verkuil | f4c1605 | 2012-05-06 09:28:18 -0300 | [diff] [blame] | 1066 | struct gspca_dev *gspca_dev = video_drvdata(file); |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 1067 | int i, j, index; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1068 | __u32 fmt_tb[8]; |
| 1069 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1070 | /* give an index to each format */ |
| 1071 | index = 0; |
| 1072 | j = 0; |
| 1073 | for (i = gspca_dev->cam.nmodes; --i >= 0; ) { |
Jean-Francois Moine | c2446b3 | 2008-07-05 11:49:20 -0300 | [diff] [blame] | 1074 | fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1075 | j = 0; |
| 1076 | for (;;) { |
| 1077 | if (fmt_tb[j] == fmt_tb[index]) |
| 1078 | break; |
| 1079 | j++; |
| 1080 | } |
| 1081 | if (j == index) { |
| 1082 | if (fmtdesc->index == index) |
| 1083 | break; /* new format */ |
| 1084 | index++; |
Julia Lawall | 8029712 | 2008-11-12 23:18:21 -0300 | [diff] [blame] | 1085 | if (index >= ARRAY_SIZE(fmt_tb)) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1086 | return -EINVAL; |
| 1087 | } |
| 1088 | } |
| 1089 | if (i < 0) |
| 1090 | return -EINVAL; /* no more format */ |
| 1091 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1092 | fmtdesc->pixelformat = fmt_tb[index]; |
Jean-François Moine | b796fb6 | 2011-08-10 07:32:57 -0300 | [diff] [blame] | 1093 | if (gspca_dev->cam.cam_mode[i].sizeimage < |
| 1094 | gspca_dev->cam.cam_mode[i].width * |
| 1095 | gspca_dev->cam.cam_mode[i].height) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1096 | fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED; |
| 1097 | fmtdesc->description[0] = fmtdesc->pixelformat & 0xff; |
| 1098 | fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff; |
| 1099 | fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff; |
| 1100 | fmtdesc->description[3] = fmtdesc->pixelformat >> 24; |
| 1101 | fmtdesc->description[4] = '\0'; |
| 1102 | return 0; |
| 1103 | } |
| 1104 | |
Mauro Carvalho Chehab | e078770 | 2008-07-01 04:06:22 -0300 | [diff] [blame] | 1105 | 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] | 1106 | struct v4l2_format *fmt) |
| 1107 | { |
Hans Verkuil | f4c1605 | 2012-05-06 09:28:18 -0300 | [diff] [blame] | 1108 | struct gspca_dev *gspca_dev = video_drvdata(file); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1109 | |
Ondrej Zary | 1966bc2 | 2013-08-30 17:54:23 -0300 | [diff] [blame] | 1110 | fmt->fmt.pix = gspca_dev->pixfmt; |
Laurent Pinchart | d52e238 | 2014-05-27 09:41:05 -0300 | [diff] [blame] | 1111 | /* some drivers use priv internally, zero it before giving it back to |
| 1112 | the core */ |
Hans Verkuil | ac3322b | 2012-05-23 17:17:12 -0300 | [diff] [blame] | 1113 | fmt->fmt.pix.priv = 0; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1114 | return 0; |
| 1115 | } |
| 1116 | |
Mauro Carvalho Chehab | e078770 | 2008-07-01 04:06:22 -0300 | [diff] [blame] | 1117 | static int try_fmt_vid_cap(struct gspca_dev *gspca_dev, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1118 | struct v4l2_format *fmt) |
| 1119 | { |
Hans de Goede | 24c530b | 2008-07-04 17:55:18 -0300 | [diff] [blame] | 1120 | int w, h, mode, mode2; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1121 | |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1122 | w = fmt->fmt.pix.width; |
| 1123 | h = fmt->fmt.pix.height; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1124 | |
Theodore Kilgore | c93396e | 2013-02-04 13:17:55 -0300 | [diff] [blame] | 1125 | PDEBUG_MODE(gspca_dev, D_CONF, "try fmt cap", |
| 1126 | fmt->fmt.pix.pixelformat, w, h); |
| 1127 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1128 | /* search the closest mode for width and height */ |
| 1129 | mode = wxh_to_mode(gspca_dev, w, h); |
| 1130 | |
| 1131 | /* OK if right palette */ |
Jean-Francois Moine | c2446b3 | 2008-07-05 11:49:20 -0300 | [diff] [blame] | 1132 | if (gspca_dev->cam.cam_mode[mode].pixelformat |
| 1133 | != fmt->fmt.pix.pixelformat) { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1134 | |
| 1135 | /* else, search the closest mode with the same pixel format */ |
| 1136 | mode2 = gspca_get_mode(gspca_dev, mode, |
| 1137 | fmt->fmt.pix.pixelformat); |
Jean-Francois Moine | c2446b3 | 2008-07-05 11:49:20 -0300 | [diff] [blame] | 1138 | if (mode2 >= 0) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1139 | mode = mode2; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1140 | } |
Hans Verkuil | ac3322b | 2012-05-23 17:17:12 -0300 | [diff] [blame] | 1141 | fmt->fmt.pix = gspca_dev->cam.cam_mode[mode]; |
Ondrej Zary | 7d687af | 2013-08-30 17:54:24 -0300 | [diff] [blame] | 1142 | if (gspca_dev->sd_desc->try_fmt) { |
| 1143 | /* pass original resolution to subdriver try_fmt */ |
| 1144 | fmt->fmt.pix.width = w; |
| 1145 | fmt->fmt.pix.height = h; |
| 1146 | gspca_dev->sd_desc->try_fmt(gspca_dev, fmt); |
| 1147 | } |
Laurent Pinchart | d52e238 | 2014-05-27 09:41:05 -0300 | [diff] [blame] | 1148 | /* some drivers use priv internally, zero it before giving it back to |
| 1149 | the core */ |
Hans Verkuil | ac3322b | 2012-05-23 17:17:12 -0300 | [diff] [blame] | 1150 | fmt->fmt.pix.priv = 0; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1151 | return mode; /* used when s_fmt */ |
| 1152 | } |
| 1153 | |
Mauro Carvalho Chehab | e078770 | 2008-07-01 04:06:22 -0300 | [diff] [blame] | 1154 | static int vidioc_try_fmt_vid_cap(struct file *file, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1155 | void *priv, |
| 1156 | struct v4l2_format *fmt) |
| 1157 | { |
Hans Verkuil | f4c1605 | 2012-05-06 09:28:18 -0300 | [diff] [blame] | 1158 | struct gspca_dev *gspca_dev = video_drvdata(file); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1159 | int ret; |
| 1160 | |
Mauro Carvalho Chehab | e078770 | 2008-07-01 04:06:22 -0300 | [diff] [blame] | 1161 | ret = try_fmt_vid_cap(gspca_dev, fmt); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1162 | if (ret < 0) |
| 1163 | return ret; |
| 1164 | return 0; |
| 1165 | } |
| 1166 | |
Mauro Carvalho Chehab | e078770 | 2008-07-01 04:06:22 -0300 | [diff] [blame] | 1167 | 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] | 1168 | struct v4l2_format *fmt) |
| 1169 | { |
Hans Verkuil | f4c1605 | 2012-05-06 09:28:18 -0300 | [diff] [blame] | 1170 | struct gspca_dev *gspca_dev = video_drvdata(file); |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1171 | int ret; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1172 | |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1173 | if (mutex_lock_interruptible(&gspca_dev->queue_lock)) |
| 1174 | return -ERESTARTSYS; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1175 | |
Mauro Carvalho Chehab | e078770 | 2008-07-01 04:06:22 -0300 | [diff] [blame] | 1176 | ret = try_fmt_vid_cap(gspca_dev, fmt); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1177 | if (ret < 0) |
| 1178 | goto out; |
| 1179 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1180 | if (gspca_dev->nframes != 0 |
| 1181 | && fmt->fmt.pix.sizeimage > gspca_dev->frsz) { |
| 1182 | ret = -EINVAL; |
| 1183 | goto out; |
| 1184 | } |
| 1185 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1186 | if (gspca_dev->streaming) { |
| 1187 | ret = -EBUSY; |
| 1188 | goto out; |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1189 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1190 | gspca_dev->curr_mode = ret; |
Ondrej Zary | 7d687af | 2013-08-30 17:54:24 -0300 | [diff] [blame] | 1191 | if (gspca_dev->sd_desc->try_fmt) |
| 1192 | /* subdriver try_fmt can modify format parameters */ |
| 1193 | gspca_dev->pixfmt = fmt->fmt.pix; |
| 1194 | else |
| 1195 | gspca_dev->pixfmt = gspca_dev->cam.cam_mode[ret]; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1196 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1197 | ret = 0; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1198 | out: |
| 1199 | mutex_unlock(&gspca_dev->queue_lock); |
| 1200 | return ret; |
| 1201 | } |
| 1202 | |
Hans de Goede | 11c635a | 2009-05-25 15:04:22 -0300 | [diff] [blame] | 1203 | static int vidioc_enum_framesizes(struct file *file, void *priv, |
| 1204 | struct v4l2_frmsizeenum *fsize) |
| 1205 | { |
Hans Verkuil | f4c1605 | 2012-05-06 09:28:18 -0300 | [diff] [blame] | 1206 | struct gspca_dev *gspca_dev = video_drvdata(file); |
Hans de Goede | 11c635a | 2009-05-25 15:04:22 -0300 | [diff] [blame] | 1207 | int i; |
| 1208 | __u32 index = 0; |
| 1209 | |
Ondrej Zary | 7d687af | 2013-08-30 17:54:24 -0300 | [diff] [blame] | 1210 | if (gspca_dev->sd_desc->enum_framesizes) |
| 1211 | return gspca_dev->sd_desc->enum_framesizes(gspca_dev, fsize); |
| 1212 | |
Hans de Goede | 11c635a | 2009-05-25 15:04:22 -0300 | [diff] [blame] | 1213 | for (i = 0; i < gspca_dev->cam.nmodes; i++) { |
| 1214 | if (fsize->pixel_format != |
| 1215 | gspca_dev->cam.cam_mode[i].pixelformat) |
| 1216 | continue; |
| 1217 | |
| 1218 | if (fsize->index == index) { |
| 1219 | fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE; |
| 1220 | fsize->discrete.width = |
| 1221 | gspca_dev->cam.cam_mode[i].width; |
| 1222 | fsize->discrete.height = |
| 1223 | gspca_dev->cam.cam_mode[i].height; |
| 1224 | return 0; |
| 1225 | } |
| 1226 | index++; |
| 1227 | } |
| 1228 | |
| 1229 | return -EINVAL; |
| 1230 | } |
| 1231 | |
Antonio Ospite | 28ffe77 | 2009-12-02 06:18:46 -0300 | [diff] [blame] | 1232 | static int vidioc_enum_frameintervals(struct file *filp, void *priv, |
| 1233 | struct v4l2_frmivalenum *fival) |
| 1234 | { |
Hans Verkuil | f4c1605 | 2012-05-06 09:28:18 -0300 | [diff] [blame] | 1235 | struct gspca_dev *gspca_dev = video_drvdata(filp); |
Antonio Ospite | 28ffe77 | 2009-12-02 06:18:46 -0300 | [diff] [blame] | 1236 | int mode = wxh_to_mode(gspca_dev, fival->width, fival->height); |
| 1237 | __u32 i; |
| 1238 | |
| 1239 | if (gspca_dev->cam.mode_framerates == NULL || |
| 1240 | gspca_dev->cam.mode_framerates[mode].nrates == 0) |
| 1241 | return -EINVAL; |
| 1242 | |
| 1243 | if (fival->pixel_format != |
| 1244 | gspca_dev->cam.cam_mode[mode].pixelformat) |
| 1245 | return -EINVAL; |
| 1246 | |
| 1247 | for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) { |
| 1248 | if (fival->index == i) { |
| 1249 | fival->type = V4L2_FRMSIZE_TYPE_DISCRETE; |
| 1250 | fival->discrete.numerator = 1; |
| 1251 | fival->discrete.denominator = |
| 1252 | gspca_dev->cam.mode_framerates[mode].rates[i]; |
| 1253 | return 0; |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | return -EINVAL; |
| 1258 | } |
| 1259 | |
Hans Verkuil | a3d6e8c | 2012-05-06 09:28:27 -0300 | [diff] [blame] | 1260 | static void gspca_release(struct v4l2_device *v4l2_device) |
Frank Zago | 6b060ff | 2008-09-28 08:12:22 -0300 | [diff] [blame] | 1261 | { |
Hans Verkuil | a3d6e8c | 2012-05-06 09:28:27 -0300 | [diff] [blame] | 1262 | struct gspca_dev *gspca_dev = |
| 1263 | container_of(v4l2_device, struct gspca_dev, v4l2_dev); |
Frank Zago | 6b060ff | 2008-09-28 08:12:22 -0300 | [diff] [blame] | 1264 | |
Hans Verkuil | 62bba5d | 2012-05-06 09:28:17 -0300 | [diff] [blame] | 1265 | v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler); |
Hans Verkuil | 2333565 | 2012-05-06 09:28:19 -0300 | [diff] [blame] | 1266 | v4l2_device_unregister(&gspca_dev->v4l2_dev); |
Frank Zago | 6b060ff | 2008-09-28 08:12:22 -0300 | [diff] [blame] | 1267 | kfree(gspca_dev->usb_buf); |
| 1268 | kfree(gspca_dev); |
| 1269 | } |
| 1270 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 1271 | static int dev_open(struct file *file) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1272 | { |
Hans Verkuil | f4c1605 | 2012-05-06 09:28:18 -0300 | [diff] [blame] | 1273 | struct gspca_dev *gspca_dev = video_drvdata(file); |
Alexey Khoroshilov | b563a0d | 2013-08-05 17:16:37 -0300 | [diff] [blame] | 1274 | int ret; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1275 | |
Jean-Francois Moine | 0d07e24 | 2009-10-01 13:44:49 -0300 | [diff] [blame] | 1276 | PDEBUG(D_STREAM, "[%s] open", current->comm); |
Jean-Francois Moine | 5c4fa00 | 2008-11-18 15:52:31 -0300 | [diff] [blame] | 1277 | |
| 1278 | /* protect the subdriver against rmmod */ |
Hans de Goede | 7f6eb11 | 2010-12-30 20:20:09 -0300 | [diff] [blame] | 1279 | if (!try_module_get(gspca_dev->module)) |
| 1280 | return -ENODEV; |
Frank Zago | 6b060ff | 2008-09-28 08:12:22 -0300 | [diff] [blame] | 1281 | |
Alexey Khoroshilov | b563a0d | 2013-08-05 17:16:37 -0300 | [diff] [blame] | 1282 | ret = v4l2_fh_open(file); |
| 1283 | if (ret) |
| 1284 | module_put(gspca_dev->module); |
| 1285 | return ret; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1286 | } |
| 1287 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 1288 | static int dev_close(struct file *file) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1289 | { |
Hans Verkuil | f4c1605 | 2012-05-06 09:28:18 -0300 | [diff] [blame] | 1290 | struct gspca_dev *gspca_dev = video_drvdata(file); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1291 | |
Jean-Francois Moine | 0d07e24 | 2009-10-01 13:44:49 -0300 | [diff] [blame] | 1292 | PDEBUG(D_STREAM, "[%s] close", current->comm); |
Hans Verkuil | a3d6e8c | 2012-05-06 09:28:27 -0300 | [diff] [blame] | 1293 | |
| 1294 | /* Needed for gspca_stream_off, always lock before queue_lock! */ |
| 1295 | if (mutex_lock_interruptible(&gspca_dev->usb_lock)) |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1296 | return -ERESTARTSYS; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1297 | |
Hans Verkuil | a3d6e8c | 2012-05-06 09:28:27 -0300 | [diff] [blame] | 1298 | if (mutex_lock_interruptible(&gspca_dev->queue_lock)) { |
| 1299 | mutex_unlock(&gspca_dev->usb_lock); |
| 1300 | return -ERESTARTSYS; |
| 1301 | } |
| 1302 | |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1303 | /* if the file did the capture, free the streaming resources */ |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1304 | if (gspca_dev->capt_file == file) { |
Hans de Goede | a3cc74d | 2012-05-07 06:44:21 -0300 | [diff] [blame] | 1305 | if (gspca_dev->streaming) |
Jean-Francois Moine | a1e8288 | 2008-09-03 17:12:17 -0300 | [diff] [blame] | 1306 | gspca_stream_off(gspca_dev); |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1307 | frame_free(gspca_dev); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1308 | } |
Jean-Francois Moine | 5c4fa00 | 2008-11-18 15:52:31 -0300 | [diff] [blame] | 1309 | module_put(gspca_dev->module); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1310 | mutex_unlock(&gspca_dev->queue_lock); |
Hans Verkuil | a3d6e8c | 2012-05-06 09:28:27 -0300 | [diff] [blame] | 1311 | mutex_unlock(&gspca_dev->usb_lock); |
Frank Zago | 6b060ff | 2008-09-28 08:12:22 -0300 | [diff] [blame] | 1312 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1313 | PDEBUG(D_STREAM, "close done"); |
Frank Zago | 6b060ff | 2008-09-28 08:12:22 -0300 | [diff] [blame] | 1314 | |
Hans Verkuil | 2333565 | 2012-05-06 09:28:19 -0300 | [diff] [blame] | 1315 | return v4l2_fh_release(file); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | static int vidioc_querycap(struct file *file, void *priv, |
| 1319 | struct v4l2_capability *cap) |
| 1320 | { |
Hans Verkuil | f4c1605 | 2012-05-06 09:28:18 -0300 | [diff] [blame] | 1321 | struct gspca_dev *gspca_dev = video_drvdata(file); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1322 | |
Hans de Goede | e7d712c | 2011-06-05 14:58:46 -0300 | [diff] [blame] | 1323 | strlcpy((char *) cap->driver, gspca_dev->sd_desc->name, |
Jean-François Moine | d851859 | 2010-12-28 06:55:01 -0300 | [diff] [blame] | 1324 | sizeof cap->driver); |
Jean-Francois Moine | c6edaf1 | 2008-07-23 03:24:06 -0300 | [diff] [blame] | 1325 | if (gspca_dev->dev->product != NULL) { |
Hans de Goede | e7d712c | 2011-06-05 14:58:46 -0300 | [diff] [blame] | 1326 | strlcpy((char *) cap->card, gspca_dev->dev->product, |
Jean-Francois Moine | c6edaf1 | 2008-07-23 03:24:06 -0300 | [diff] [blame] | 1327 | sizeof cap->card); |
| 1328 | } else { |
Jean-François Moine | d851859 | 2010-12-28 06:55:01 -0300 | [diff] [blame] | 1329 | snprintf((char *) cap->card, sizeof cap->card, |
Jean-Francois Moine | c6edaf1 | 2008-07-23 03:24:06 -0300 | [diff] [blame] | 1330 | "USB Camera (%04x:%04x)", |
| 1331 | le16_to_cpu(gspca_dev->dev->descriptor.idVendor), |
| 1332 | le16_to_cpu(gspca_dev->dev->descriptor.idProduct)); |
| 1333 | } |
Jean-François Moine | d851859 | 2010-12-28 06:55:01 -0300 | [diff] [blame] | 1334 | usb_make_path(gspca_dev->dev, (char *) cap->bus_info, |
| 1335 | sizeof(cap->bus_info)); |
Hans Verkuil | cc7b6f2 | 2012-05-06 09:28:21 -0300 | [diff] [blame] | 1336 | cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1337 | | V4L2_CAP_STREAMING |
| 1338 | | V4L2_CAP_READWRITE; |
Hans Verkuil | cc7b6f2 | 2012-05-06 09:28:21 -0300 | [diff] [blame] | 1339 | cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; |
Hans Verkuil | a3d6e8c | 2012-05-06 09:28:27 -0300 | [diff] [blame] | 1340 | return 0; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1341 | } |
| 1342 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1343 | static int vidioc_enum_input(struct file *file, void *priv, |
| 1344 | struct v4l2_input *input) |
| 1345 | { |
Hans Verkuil | f4c1605 | 2012-05-06 09:28:18 -0300 | [diff] [blame] | 1346 | struct gspca_dev *gspca_dev = video_drvdata(file); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1347 | |
| 1348 | if (input->index != 0) |
| 1349 | return -EINVAL; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1350 | input->type = V4L2_INPUT_TYPE_CAMERA; |
Adam Baker | dfa76fa | 2009-03-29 19:17:10 -0300 | [diff] [blame] | 1351 | input->status = gspca_dev->cam.input_flags; |
Hans de Goede | e7d712c | 2011-06-05 14:58:46 -0300 | [diff] [blame] | 1352 | strlcpy(input->name, gspca_dev->sd_desc->name, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1353 | sizeof input->name); |
| 1354 | return 0; |
| 1355 | } |
| 1356 | |
| 1357 | static int vidioc_g_input(struct file *file, void *priv, unsigned int *i) |
| 1358 | { |
| 1359 | *i = 0; |
| 1360 | return 0; |
| 1361 | } |
| 1362 | |
| 1363 | static int vidioc_s_input(struct file *file, void *priv, unsigned int i) |
| 1364 | { |
| 1365 | if (i > 0) |
| 1366 | return -EINVAL; |
| 1367 | return (0); |
| 1368 | } |
| 1369 | |
| 1370 | static int vidioc_reqbufs(struct file *file, void *priv, |
| 1371 | struct v4l2_requestbuffers *rb) |
| 1372 | { |
Hans Verkuil | f4c1605 | 2012-05-06 09:28:18 -0300 | [diff] [blame] | 1373 | struct gspca_dev *gspca_dev = video_drvdata(file); |
Jean-François Moine | 832d0a9 | 2010-04-09 06:07:58 -0300 | [diff] [blame] | 1374 | int i, ret = 0, streaming; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1375 | |
Jean-François Moine | a2ba6f2 | 2010-07-14 06:21:16 -0300 | [diff] [blame] | 1376 | i = rb->memory; /* (avoid compilation warning) */ |
| 1377 | switch (i) { |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1378 | case GSPCA_MEMORY_READ: /* (internal call) */ |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1379 | case V4L2_MEMORY_MMAP: |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1380 | case V4L2_MEMORY_USERPTR: |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 1381 | break; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1382 | default: |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1383 | return -EINVAL; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1384 | } |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1385 | if (mutex_lock_interruptible(&gspca_dev->queue_lock)) |
| 1386 | return -ERESTARTSYS; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1387 | |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1388 | if (gspca_dev->memory != GSPCA_MEMORY_NO |
Hans de Goede | ce5610b | 2010-12-31 07:41:54 -0300 | [diff] [blame] | 1389 | && gspca_dev->memory != GSPCA_MEMORY_READ |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1390 | && gspca_dev->memory != rb->memory) { |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1391 | ret = -EBUSY; |
| 1392 | goto out; |
| 1393 | } |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1394 | |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1395 | /* only one file may do the capture */ |
| 1396 | if (gspca_dev->capt_file != NULL |
| 1397 | && gspca_dev->capt_file != file) { |
| 1398 | ret = -EBUSY; |
| 1399 | goto out; |
| 1400 | } |
| 1401 | |
| 1402 | /* if allocated, the buffers must not be mapped */ |
| 1403 | for (i = 0; i < gspca_dev->nframes; i++) { |
| 1404 | if (gspca_dev->frame[i].vma_use_count) { |
Jean-Francois Moine | ac0d6df | 2008-07-08 06:58:15 -0300 | [diff] [blame] | 1405 | ret = -EBUSY; |
| 1406 | goto out; |
| 1407 | } |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | /* stop streaming */ |
Jean-François Moine | 832d0a9 | 2010-04-09 06:07:58 -0300 | [diff] [blame] | 1411 | streaming = gspca_dev->streaming; |
| 1412 | if (streaming) { |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1413 | gspca_stream_off(gspca_dev); |
Jean-François Moine | 59929ce | 2011-03-14 05:33:01 -0300 | [diff] [blame] | 1414 | |
| 1415 | /* Don't restart the stream when switching from read |
| 1416 | * to mmap mode */ |
| 1417 | if (gspca_dev->memory == GSPCA_MEMORY_READ) |
| 1418 | streaming = 0; |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1419 | } |
| 1420 | |
| 1421 | /* free the previous allocated buffers, if any */ |
Hans de Goede | d642de2 | 2010-12-31 04:51:36 -0300 | [diff] [blame] | 1422 | if (gspca_dev->nframes != 0) |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1423 | frame_free(gspca_dev); |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1424 | if (rb->count == 0) /* unrequest */ |
| 1425 | goto out; |
Hans de Goede | d642de2 | 2010-12-31 04:51:36 -0300 | [diff] [blame] | 1426 | ret = frame_alloc(gspca_dev, file, rb->memory, rb->count); |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1427 | if (ret == 0) { |
| 1428 | rb->count = gspca_dev->nframes; |
Jean-François Moine | 832d0a9 | 2010-04-09 06:07:58 -0300 | [diff] [blame] | 1429 | if (streaming) |
| 1430 | ret = gspca_init_transfer(gspca_dev); |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1431 | } |
| 1432 | out: |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1433 | mutex_unlock(&gspca_dev->queue_lock); |
| 1434 | PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count); |
| 1435 | return ret; |
| 1436 | } |
| 1437 | |
| 1438 | static int vidioc_querybuf(struct file *file, void *priv, |
| 1439 | struct v4l2_buffer *v4l2_buf) |
| 1440 | { |
Hans Verkuil | f4c1605 | 2012-05-06 09:28:18 -0300 | [diff] [blame] | 1441 | struct gspca_dev *gspca_dev = video_drvdata(file); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1442 | struct gspca_frame *frame; |
| 1443 | |
Mauro Carvalho Chehab | 3a70c3f | 2012-10-27 14:55:20 -0300 | [diff] [blame] | 1444 | if (v4l2_buf->index >= gspca_dev->nframes) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1445 | return -EINVAL; |
| 1446 | |
| 1447 | frame = &gspca_dev->frame[v4l2_buf->index]; |
| 1448 | memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf); |
| 1449 | return 0; |
| 1450 | } |
| 1451 | |
| 1452 | static int vidioc_streamon(struct file *file, void *priv, |
| 1453 | enum v4l2_buf_type buf_type) |
| 1454 | { |
Hans Verkuil | f4c1605 | 2012-05-06 09:28:18 -0300 | [diff] [blame] | 1455 | struct gspca_dev *gspca_dev = video_drvdata(file); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1456 | int ret; |
| 1457 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1458 | if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1459 | return -EINVAL; |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1460 | if (mutex_lock_interruptible(&gspca_dev->queue_lock)) |
| 1461 | return -ERESTARTSYS; |
Jean-Francois Moine | 090264e | 2009-02-12 08:05:45 -0300 | [diff] [blame] | 1462 | |
Jean-François Moine | 18bf99a | 2010-04-09 06:06:11 -0300 | [diff] [blame] | 1463 | /* check the capture file */ |
| 1464 | if (gspca_dev->capt_file != file) { |
| 1465 | ret = -EBUSY; |
| 1466 | goto out; |
| 1467 | } |
| 1468 | |
Jean-Francois Moine | 1edabe7 | 2008-12-19 15:17:25 -0300 | [diff] [blame] | 1469 | if (gspca_dev->nframes == 0 |
| 1470 | || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1471 | ret = -EINVAL; |
| 1472 | goto out; |
| 1473 | } |
| 1474 | if (!gspca_dev->streaming) { |
| 1475 | ret = gspca_init_transfer(gspca_dev); |
| 1476 | if (ret < 0) |
| 1477 | goto out; |
| 1478 | } |
Ondrej Zary | 1966bc2 | 2013-08-30 17:54:23 -0300 | [diff] [blame] | 1479 | PDEBUG_MODE(gspca_dev, D_STREAM, "stream on OK", |
| 1480 | gspca_dev->pixfmt.pixelformat, |
| 1481 | gspca_dev->pixfmt.width, gspca_dev->pixfmt.height); |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1482 | ret = 0; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1483 | out: |
| 1484 | mutex_unlock(&gspca_dev->queue_lock); |
| 1485 | return ret; |
| 1486 | } |
| 1487 | |
| 1488 | static int vidioc_streamoff(struct file *file, void *priv, |
| 1489 | enum v4l2_buf_type buf_type) |
| 1490 | { |
Hans Verkuil | f4c1605 | 2012-05-06 09:28:18 -0300 | [diff] [blame] | 1491 | struct gspca_dev *gspca_dev = video_drvdata(file); |
Hans de Goede | af05ef0 | 2012-05-22 11:24:05 -0300 | [diff] [blame] | 1492 | int i, ret; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1493 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1494 | if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1495 | return -EINVAL; |
Hans de Goede | 4e770f7 | 2010-12-30 13:11:21 -0300 | [diff] [blame] | 1496 | |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1497 | if (mutex_lock_interruptible(&gspca_dev->queue_lock)) |
| 1498 | return -ERESTARTSYS; |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1499 | |
Hans de Goede | 4e770f7 | 2010-12-30 13:11:21 -0300 | [diff] [blame] | 1500 | if (!gspca_dev->streaming) { |
| 1501 | ret = 0; |
| 1502 | goto out; |
| 1503 | } |
| 1504 | |
Jean-François Moine | 18bf99a | 2010-04-09 06:06:11 -0300 | [diff] [blame] | 1505 | /* check the capture file */ |
| 1506 | if (gspca_dev->capt_file != file) { |
| 1507 | ret = -EBUSY; |
| 1508 | goto out; |
| 1509 | } |
| 1510 | |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1511 | /* stop streaming */ |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1512 | gspca_stream_off(gspca_dev); |
Hans de Goede | 0d0ae15 | 2010-12-31 17:17:51 -0300 | [diff] [blame] | 1513 | /* In case another thread is waiting in dqbuf */ |
| 1514 | wake_up_interruptible(&gspca_dev->wq); |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1515 | |
Jean-François Moine | f7059ea | 2010-07-06 04:32:27 -0300 | [diff] [blame] | 1516 | /* empty the transfer queues */ |
Hans de Goede | af05ef0 | 2012-05-22 11:24:05 -0300 | [diff] [blame] | 1517 | for (i = 0; i < gspca_dev->nframes; i++) |
| 1518 | gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS; |
Jean-François Moine | f7059ea | 2010-07-06 04:32:27 -0300 | [diff] [blame] | 1519 | atomic_set(&gspca_dev->fr_q, 0); |
| 1520 | atomic_set(&gspca_dev->fr_i, 0); |
| 1521 | gspca_dev->fr_o = 0; |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1522 | ret = 0; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1523 | out: |
| 1524 | mutex_unlock(&gspca_dev->queue_lock); |
| 1525 | return ret; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | static int vidioc_g_jpegcomp(struct file *file, void *priv, |
| 1529 | struct v4l2_jpegcompression *jpegcomp) |
| 1530 | { |
Hans Verkuil | f4c1605 | 2012-05-06 09:28:18 -0300 | [diff] [blame] | 1531 | struct gspca_dev *gspca_dev = video_drvdata(file); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1532 | |
Jean-Francois Moine | 8c4ebae | 2009-12-02 07:14:33 -0300 | [diff] [blame] | 1533 | gspca_dev->usb_err = 0; |
Hans Verkuil | a3d6e8c | 2012-05-06 09:28:27 -0300 | [diff] [blame] | 1534 | return gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1535 | } |
| 1536 | |
| 1537 | static int vidioc_s_jpegcomp(struct file *file, void *priv, |
Hans Verkuil | d88aab5 | 2012-09-17 05:05:25 -0300 | [diff] [blame] | 1538 | const struct v4l2_jpegcompression *jpegcomp) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1539 | { |
Hans Verkuil | f4c1605 | 2012-05-06 09:28:18 -0300 | [diff] [blame] | 1540 | struct gspca_dev *gspca_dev = video_drvdata(file); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1541 | |
Jean-Francois Moine | 8c4ebae | 2009-12-02 07:14:33 -0300 | [diff] [blame] | 1542 | gspca_dev->usb_err = 0; |
Hans Verkuil | a3d6e8c | 2012-05-06 09:28:27 -0300 | [diff] [blame] | 1543 | return gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1544 | } |
| 1545 | |
| 1546 | static int vidioc_g_parm(struct file *filp, void *priv, |
| 1547 | struct v4l2_streamparm *parm) |
| 1548 | { |
Hans Verkuil | f4c1605 | 2012-05-06 09:28:18 -0300 | [diff] [blame] | 1549 | struct gspca_dev *gspca_dev = video_drvdata(filp); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1550 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1551 | parm->parm.capture.readbuffers = gspca_dev->nbufread; |
Jim Paris | 627a5ef | 2008-12-10 06:02:42 -0300 | [diff] [blame] | 1552 | |
| 1553 | if (gspca_dev->sd_desc->get_streamparm) { |
Hans Verkuil | a3d6e8c | 2012-05-06 09:28:27 -0300 | [diff] [blame] | 1554 | gspca_dev->usb_err = 0; |
| 1555 | gspca_dev->sd_desc->get_streamparm(gspca_dev, parm); |
| 1556 | return gspca_dev->usb_err; |
Jim Paris | 627a5ef | 2008-12-10 06:02:42 -0300 | [diff] [blame] | 1557 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1558 | return 0; |
| 1559 | } |
| 1560 | |
| 1561 | static int vidioc_s_parm(struct file *filp, void *priv, |
| 1562 | struct v4l2_streamparm *parm) |
| 1563 | { |
Hans Verkuil | f4c1605 | 2012-05-06 09:28:18 -0300 | [diff] [blame] | 1564 | struct gspca_dev *gspca_dev = video_drvdata(filp); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1565 | int n; |
| 1566 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1567 | n = parm->parm.capture.readbuffers; |
Jean-François Moine | f7059ea | 2010-07-06 04:32:27 -0300 | [diff] [blame] | 1568 | if (n == 0 || n >= GSPCA_MAX_FRAMES) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1569 | parm->parm.capture.readbuffers = gspca_dev->nbufread; |
| 1570 | else |
| 1571 | gspca_dev->nbufread = n; |
Jim Paris | 627a5ef | 2008-12-10 06:02:42 -0300 | [diff] [blame] | 1572 | |
| 1573 | if (gspca_dev->sd_desc->set_streamparm) { |
Hans Verkuil | a3d6e8c | 2012-05-06 09:28:27 -0300 | [diff] [blame] | 1574 | gspca_dev->usb_err = 0; |
| 1575 | gspca_dev->sd_desc->set_streamparm(gspca_dev, parm); |
| 1576 | return gspca_dev->usb_err; |
Jim Paris | 627a5ef | 2008-12-10 06:02:42 -0300 | [diff] [blame] | 1577 | } |
| 1578 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1579 | return 0; |
| 1580 | } |
| 1581 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1582 | static int dev_mmap(struct file *file, struct vm_area_struct *vma) |
| 1583 | { |
Hans Verkuil | f4c1605 | 2012-05-06 09:28:18 -0300 | [diff] [blame] | 1584 | struct gspca_dev *gspca_dev = video_drvdata(file); |
Harvey Harrison | efab821 | 2008-07-05 06:12:47 -0300 | [diff] [blame] | 1585 | struct gspca_frame *frame; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1586 | struct page *page; |
| 1587 | unsigned long addr, start, size; |
| 1588 | int i, ret; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1589 | |
| 1590 | start = vma->vm_start; |
| 1591 | size = vma->vm_end - vma->vm_start; |
| 1592 | PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size); |
| 1593 | |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1594 | if (mutex_lock_interruptible(&gspca_dev->queue_lock)) |
| 1595 | return -ERESTARTSYS; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1596 | if (gspca_dev->capt_file != file) { |
| 1597 | ret = -EINVAL; |
| 1598 | goto out; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1599 | } |
| 1600 | |
Harvey Harrison | efab821 | 2008-07-05 06:12:47 -0300 | [diff] [blame] | 1601 | frame = NULL; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1602 | for (i = 0; i < gspca_dev->nframes; ++i) { |
| 1603 | if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) { |
| 1604 | PDEBUG(D_STREAM, "mmap bad memory type"); |
| 1605 | break; |
| 1606 | } |
| 1607 | if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT) |
| 1608 | == vma->vm_pgoff) { |
| 1609 | frame = &gspca_dev->frame[i]; |
| 1610 | break; |
| 1611 | } |
| 1612 | } |
Harvey Harrison | efab821 | 2008-07-05 06:12:47 -0300 | [diff] [blame] | 1613 | if (frame == NULL) { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1614 | PDEBUG(D_STREAM, "mmap no frame buffer found"); |
| 1615 | ret = -EINVAL; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1616 | goto out; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1617 | } |
Jean-François Moine | 38bff69 | 2010-06-24 05:19:58 -0300 | [diff] [blame] | 1618 | if (size != frame->v4l2_buf.length) { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1619 | PDEBUG(D_STREAM, "mmap bad size"); |
| 1620 | ret = -EINVAL; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1621 | goto out; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1622 | } |
| 1623 | |
| 1624 | /* |
| 1625 | * - VM_IO marks the area as being a mmaped region for I/O to a |
| 1626 | * device. It also prevents the region from being core dumped. |
| 1627 | */ |
| 1628 | vma->vm_flags |= VM_IO; |
| 1629 | |
| 1630 | addr = (unsigned long) frame->data; |
| 1631 | while (size > 0) { |
| 1632 | page = vmalloc_to_page((void *) addr); |
| 1633 | ret = vm_insert_page(vma, start, page); |
| 1634 | if (ret < 0) |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1635 | goto out; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1636 | start += PAGE_SIZE; |
| 1637 | addr += PAGE_SIZE; |
| 1638 | size -= PAGE_SIZE; |
| 1639 | } |
| 1640 | |
Jean-Francois Moine | d50e257 | 2009-11-26 15:36:40 -0300 | [diff] [blame] | 1641 | vma->vm_ops = &gspca_vm_ops; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1642 | vma->vm_private_data = frame; |
| 1643 | gspca_vm_open(vma); |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1644 | ret = 0; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1645 | out: |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1646 | mutex_unlock(&gspca_dev->queue_lock); |
| 1647 | return ret; |
| 1648 | } |
| 1649 | |
Hans de Goede | 27074ef | 2010-12-30 19:54:33 -0300 | [diff] [blame] | 1650 | static int frame_ready_nolock(struct gspca_dev *gspca_dev, struct file *file, |
| 1651 | enum v4l2_memory memory) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1652 | { |
Hans de Goede | 27074ef | 2010-12-30 19:54:33 -0300 | [diff] [blame] | 1653 | if (!gspca_dev->present) |
| 1654 | return -ENODEV; |
| 1655 | if (gspca_dev->capt_file != file || gspca_dev->memory != memory || |
| 1656 | !gspca_dev->streaming) |
| 1657 | return -EINVAL; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1658 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1659 | /* check if a frame is ready */ |
Hans de Goede | 27074ef | 2010-12-30 19:54:33 -0300 | [diff] [blame] | 1660 | return gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i); |
| 1661 | } |
Hans de Goede | 181b704 | 2008-11-19 17:16:26 -0300 | [diff] [blame] | 1662 | |
Hans de Goede | 27074ef | 2010-12-30 19:54:33 -0300 | [diff] [blame] | 1663 | static int frame_ready(struct gspca_dev *gspca_dev, struct file *file, |
| 1664 | enum v4l2_memory memory) |
| 1665 | { |
| 1666 | int ret; |
Hans de Goede | 181b704 | 2008-11-19 17:16:26 -0300 | [diff] [blame] | 1667 | |
Hans de Goede | 27074ef | 2010-12-30 19:54:33 -0300 | [diff] [blame] | 1668 | if (mutex_lock_interruptible(&gspca_dev->queue_lock)) |
| 1669 | return -ERESTARTSYS; |
| 1670 | ret = frame_ready_nolock(gspca_dev, file, memory); |
| 1671 | mutex_unlock(&gspca_dev->queue_lock); |
| 1672 | return ret; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1673 | } |
| 1674 | |
| 1675 | /* |
| 1676 | * dequeue a video buffer |
| 1677 | * |
| 1678 | * If nonblock_ing is false, block until a buffer is available. |
| 1679 | */ |
| 1680 | static int vidioc_dqbuf(struct file *file, void *priv, |
| 1681 | struct v4l2_buffer *v4l2_buf) |
| 1682 | { |
Hans Verkuil | f4c1605 | 2012-05-06 09:28:18 -0300 | [diff] [blame] | 1683 | struct gspca_dev *gspca_dev = video_drvdata(file); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1684 | struct gspca_frame *frame; |
Hans de Goede | 27074ef | 2010-12-30 19:54:33 -0300 | [diff] [blame] | 1685 | int i, j, ret; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1686 | |
| 1687 | PDEBUG(D_FRAM, "dqbuf"); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1688 | |
Hans de Goede | 27074ef | 2010-12-30 19:54:33 -0300 | [diff] [blame] | 1689 | if (mutex_lock_interruptible(&gspca_dev->queue_lock)) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1690 | return -ERESTARTSYS; |
| 1691 | |
Hans de Goede | 27074ef | 2010-12-30 19:54:33 -0300 | [diff] [blame] | 1692 | for (;;) { |
| 1693 | ret = frame_ready_nolock(gspca_dev, file, v4l2_buf->memory); |
| 1694 | if (ret < 0) |
| 1695 | goto out; |
| 1696 | if (ret > 0) |
| 1697 | break; |
| 1698 | |
| 1699 | mutex_unlock(&gspca_dev->queue_lock); |
| 1700 | |
| 1701 | if (file->f_flags & O_NONBLOCK) |
| 1702 | return -EAGAIN; |
| 1703 | |
| 1704 | /* wait till a frame is ready */ |
| 1705 | ret = wait_event_interruptible_timeout(gspca_dev->wq, |
| 1706 | frame_ready(gspca_dev, file, v4l2_buf->memory), |
| 1707 | msecs_to_jiffies(3000)); |
| 1708 | if (ret < 0) |
| 1709 | return ret; |
| 1710 | if (ret == 0) |
| 1711 | return -EIO; |
| 1712 | |
| 1713 | if (mutex_lock_interruptible(&gspca_dev->queue_lock)) |
| 1714 | return -ERESTARTSYS; |
| 1715 | } |
| 1716 | |
| 1717 | i = gspca_dev->fr_o; |
| 1718 | j = gspca_dev->fr_queue[i]; |
| 1719 | frame = &gspca_dev->frame[j]; |
| 1720 | |
| 1721 | gspca_dev->fr_o = (i + 1) % GSPCA_MAX_FRAMES; |
| 1722 | |
Hans de Goede | 4a82bc6 | 2010-12-30 20:00:17 -0300 | [diff] [blame] | 1723 | frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE; |
| 1724 | memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf); |
| 1725 | PDEBUG(D_FRAM, "dqbuf %d", j); |
| 1726 | ret = 0; |
| 1727 | |
Jean-Francois Moine | ac0d6df | 2008-07-08 06:58:15 -0300 | [diff] [blame] | 1728 | if (gspca_dev->memory == V4L2_MEMORY_USERPTR) { |
Hans Verkuil | c6eb8ea | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 1729 | if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr, |
Jean-Francois Moine | ac0d6df | 2008-07-08 06:58:15 -0300 | [diff] [blame] | 1730 | frame->data, |
| 1731 | frame->v4l2_buf.bytesused)) { |
Theodore Kilgore | c93396e | 2013-02-04 13:17:55 -0300 | [diff] [blame] | 1732 | PERR("dqbuf cp to user failed"); |
Jean-Francois Moine | ac0d6df | 2008-07-08 06:58:15 -0300 | [diff] [blame] | 1733 | ret = -EFAULT; |
Jean-Francois Moine | ac0d6df | 2008-07-08 06:58:15 -0300 | [diff] [blame] | 1734 | } |
| 1735 | } |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1736 | out: |
Hans de Goede | 27074ef | 2010-12-30 19:54:33 -0300 | [diff] [blame] | 1737 | mutex_unlock(&gspca_dev->queue_lock); |
Hans Verkuil | a3d6e8c | 2012-05-06 09:28:27 -0300 | [diff] [blame] | 1738 | |
| 1739 | if (ret == 0 && gspca_dev->sd_desc->dq_callback) { |
| 1740 | mutex_lock(&gspca_dev->usb_lock); |
| 1741 | gspca_dev->usb_err = 0; |
| 1742 | if (gspca_dev->present) |
| 1743 | gspca_dev->sd_desc->dq_callback(gspca_dev); |
| 1744 | mutex_unlock(&gspca_dev->usb_lock); |
| 1745 | } |
| 1746 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1747 | return ret; |
| 1748 | } |
| 1749 | |
| 1750 | /* |
| 1751 | * queue a video buffer |
| 1752 | * |
| 1753 | * Attempting to queue a buffer that has already been |
| 1754 | * queued will return -EINVAL. |
| 1755 | */ |
| 1756 | static int vidioc_qbuf(struct file *file, void *priv, |
| 1757 | struct v4l2_buffer *v4l2_buf) |
| 1758 | { |
Hans Verkuil | f4c1605 | 2012-05-06 09:28:18 -0300 | [diff] [blame] | 1759 | struct gspca_dev *gspca_dev = video_drvdata(file); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1760 | struct gspca_frame *frame; |
| 1761 | int i, index, ret; |
| 1762 | |
| 1763 | PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1764 | |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1765 | if (mutex_lock_interruptible(&gspca_dev->queue_lock)) |
| 1766 | return -ERESTARTSYS; |
| 1767 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1768 | index = v4l2_buf->index; |
| 1769 | if ((unsigned) index >= gspca_dev->nframes) { |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1770 | PDEBUG(D_FRAM, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1771 | "qbuf idx %d >= %d", index, gspca_dev->nframes); |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1772 | ret = -EINVAL; |
| 1773 | goto out; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1774 | } |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1775 | if (v4l2_buf->memory != gspca_dev->memory) { |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1776 | PDEBUG(D_FRAM, "qbuf bad memory type"); |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1777 | ret = -EINVAL; |
| 1778 | goto out; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1779 | } |
| 1780 | |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1781 | frame = &gspca_dev->frame[index]; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1782 | if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) { |
| 1783 | PDEBUG(D_FRAM, "qbuf bad state"); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1784 | ret = -EINVAL; |
| 1785 | goto out; |
| 1786 | } |
| 1787 | |
| 1788 | frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1789 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1790 | if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1791 | frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr; |
| 1792 | frame->v4l2_buf.length = v4l2_buf->length; |
| 1793 | } |
| 1794 | |
| 1795 | /* put the buffer in the 'queued' queue */ |
Jean-François Moine | f7059ea | 2010-07-06 04:32:27 -0300 | [diff] [blame] | 1796 | i = atomic_read(&gspca_dev->fr_q); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1797 | gspca_dev->fr_queue[i] = index; |
Jean-François Moine | f7059ea | 2010-07-06 04:32:27 -0300 | [diff] [blame] | 1798 | atomic_set(&gspca_dev->fr_q, (i + 1) % GSPCA_MAX_FRAMES); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1799 | |
| 1800 | v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED; |
| 1801 | v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE; |
| 1802 | ret = 0; |
| 1803 | out: |
| 1804 | mutex_unlock(&gspca_dev->queue_lock); |
| 1805 | return ret; |
| 1806 | } |
| 1807 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1808 | /* |
| 1809 | * allocate the resources for read() |
| 1810 | */ |
| 1811 | static int read_alloc(struct gspca_dev *gspca_dev, |
| 1812 | struct file *file) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1813 | { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1814 | struct v4l2_buffer v4l2_buf; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1815 | int i, ret; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1816 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1817 | PDEBUG(D_STREAM, "read alloc"); |
Hans Verkuil | a3d6e8c | 2012-05-06 09:28:27 -0300 | [diff] [blame] | 1818 | |
| 1819 | if (mutex_lock_interruptible(&gspca_dev->usb_lock)) |
| 1820 | return -ERESTARTSYS; |
| 1821 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1822 | if (gspca_dev->nframes == 0) { |
| 1823 | struct v4l2_requestbuffers rb; |
| 1824 | |
| 1825 | memset(&rb, 0, sizeof rb); |
| 1826 | rb.count = gspca_dev->nbufread; |
| 1827 | rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
Jean-Francois Moine | ac0d6df | 2008-07-08 06:58:15 -0300 | [diff] [blame] | 1828 | rb.memory = GSPCA_MEMORY_READ; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1829 | ret = vidioc_reqbufs(file, gspca_dev, &rb); |
| 1830 | if (ret != 0) { |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1831 | PDEBUG(D_STREAM, "read reqbuf err %d", ret); |
Hans Verkuil | a3d6e8c | 2012-05-06 09:28:27 -0300 | [diff] [blame] | 1832 | goto out; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1833 | } |
| 1834 | memset(&v4l2_buf, 0, sizeof v4l2_buf); |
| 1835 | v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
Jean-Francois Moine | ac0d6df | 2008-07-08 06:58:15 -0300 | [diff] [blame] | 1836 | v4l2_buf.memory = GSPCA_MEMORY_READ; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1837 | for (i = 0; i < gspca_dev->nbufread; i++) { |
| 1838 | v4l2_buf.index = i; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1839 | ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf); |
| 1840 | if (ret != 0) { |
| 1841 | PDEBUG(D_STREAM, "read qbuf err: %d", ret); |
Hans Verkuil | a3d6e8c | 2012-05-06 09:28:27 -0300 | [diff] [blame] | 1842 | goto out; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1843 | } |
| 1844 | } |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1845 | } |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1846 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1847 | /* start streaming */ |
| 1848 | ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE); |
| 1849 | if (ret != 0) |
| 1850 | PDEBUG(D_STREAM, "read streamon err %d", ret); |
Hans Verkuil | a3d6e8c | 2012-05-06 09:28:27 -0300 | [diff] [blame] | 1851 | out: |
| 1852 | mutex_unlock(&gspca_dev->usb_lock); |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1853 | return ret; |
| 1854 | } |
| 1855 | |
| 1856 | static unsigned int dev_poll(struct file *file, poll_table *wait) |
| 1857 | { |
Hans Verkuil | f4c1605 | 2012-05-06 09:28:18 -0300 | [diff] [blame] | 1858 | struct gspca_dev *gspca_dev = video_drvdata(file); |
Hans de Goede | a2d887c | 2012-05-07 08:19:42 -0300 | [diff] [blame] | 1859 | unsigned long req_events = poll_requested_events(wait); |
| 1860 | int ret = 0; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1861 | |
| 1862 | PDEBUG(D_FRAM, "poll"); |
| 1863 | |
Hans de Goede | a2d887c | 2012-05-07 08:19:42 -0300 | [diff] [blame] | 1864 | if (req_events & POLLPRI) |
| 1865 | ret |= v4l2_ctrl_poll(file, wait); |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1866 | |
Hans de Goede | a2d887c | 2012-05-07 08:19:42 -0300 | [diff] [blame] | 1867 | if (req_events & (POLLIN | POLLRDNORM)) { |
| 1868 | /* if reqbufs is not done, the user would use read() */ |
| 1869 | if (gspca_dev->memory == GSPCA_MEMORY_NO) { |
| 1870 | if (read_alloc(gspca_dev, file) != 0) { |
| 1871 | ret |= POLLERR; |
| 1872 | goto out; |
| 1873 | } |
| 1874 | } |
| 1875 | |
| 1876 | poll_wait(file, &gspca_dev->wq, wait); |
| 1877 | |
| 1878 | /* check if an image has been received */ |
| 1879 | if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0) { |
| 1880 | ret |= POLLERR; |
| 1881 | goto out; |
| 1882 | } |
| 1883 | if (gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i)) |
| 1884 | ret |= POLLIN | POLLRDNORM; |
| 1885 | mutex_unlock(&gspca_dev->queue_lock); |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1886 | } |
| 1887 | |
Hans de Goede | a2d887c | 2012-05-07 08:19:42 -0300 | [diff] [blame] | 1888 | out: |
Jean-Francois Moine | 090264e | 2009-02-12 08:05:45 -0300 | [diff] [blame] | 1889 | if (!gspca_dev->present) |
Hans de Goede | a2d887c | 2012-05-07 08:19:42 -0300 | [diff] [blame] | 1890 | ret |= POLLHUP; |
| 1891 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1892 | return ret; |
| 1893 | } |
| 1894 | |
| 1895 | static ssize_t dev_read(struct file *file, char __user *data, |
| 1896 | size_t count, loff_t *ppos) |
| 1897 | { |
Hans Verkuil | f4c1605 | 2012-05-06 09:28:18 -0300 | [diff] [blame] | 1898 | struct gspca_dev *gspca_dev = video_drvdata(file); |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1899 | struct gspca_frame *frame; |
| 1900 | struct v4l2_buffer v4l2_buf; |
| 1901 | struct timeval timestamp; |
Jean-Francois Moine | 82643d0 | 2008-07-02 09:14:56 -0300 | [diff] [blame] | 1902 | int n, ret, ret2; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1903 | |
Mauro Carvalho Chehab | a9e1113 | 2008-07-09 05:26:26 -0300 | [diff] [blame] | 1904 | PDEBUG(D_FRAM, "read (%zd)", count); |
Hans de Goede | ee36299 | 2010-12-31 05:05:56 -0300 | [diff] [blame] | 1905 | if (gspca_dev->memory == GSPCA_MEMORY_NO) { /* first time ? */ |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1906 | ret = read_alloc(gspca_dev, file); |
| 1907 | if (ret != 0) |
| 1908 | return ret; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1909 | } |
| 1910 | |
| 1911 | /* get a frame */ |
Paulo Assis | 2f51d00 | 2010-03-07 06:56:56 -0300 | [diff] [blame] | 1912 | timestamp = ktime_to_timeval(ktime_get()); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1913 | timestamp.tv_sec--; |
Jean-Francois Moine | 82643d0 | 2008-07-02 09:14:56 -0300 | [diff] [blame] | 1914 | n = 2; |
| 1915 | for (;;) { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1916 | memset(&v4l2_buf, 0, sizeof v4l2_buf); |
| 1917 | v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
Jean-Francois Moine | ac0d6df | 2008-07-08 06:58:15 -0300 | [diff] [blame] | 1918 | v4l2_buf.memory = GSPCA_MEMORY_READ; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1919 | ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf); |
| 1920 | if (ret != 0) { |
| 1921 | PDEBUG(D_STREAM, "read dqbuf err %d", ret); |
| 1922 | return ret; |
| 1923 | } |
| 1924 | |
| 1925 | /* if the process slept for more than 1 second, |
Frank Zago | 219423c | 2008-09-26 07:43:54 -0300 | [diff] [blame] | 1926 | * get a newer frame */ |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1927 | frame = &gspca_dev->frame[v4l2_buf.index]; |
Jean-Francois Moine | 82643d0 | 2008-07-02 09:14:56 -0300 | [diff] [blame] | 1928 | if (--n < 0) |
| 1929 | break; /* avoid infinite loop */ |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1930 | if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec) |
| 1931 | break; |
| 1932 | ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf); |
| 1933 | if (ret != 0) { |
| 1934 | PDEBUG(D_STREAM, "read qbuf err %d", ret); |
| 1935 | return ret; |
| 1936 | } |
| 1937 | } |
| 1938 | |
| 1939 | /* copy the frame */ |
Jean-Francois Moine | ac0d6df | 2008-07-08 06:58:15 -0300 | [diff] [blame] | 1940 | if (count > frame->v4l2_buf.bytesused) |
| 1941 | count = frame->v4l2_buf.bytesused; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1942 | ret = copy_to_user(data, frame->data, count); |
| 1943 | if (ret != 0) { |
Theodore Kilgore | c93396e | 2013-02-04 13:17:55 -0300 | [diff] [blame] | 1944 | PERR("read cp to user lack %d / %zd", ret, count); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1945 | ret = -EFAULT; |
| 1946 | goto out; |
| 1947 | } |
| 1948 | ret = count; |
| 1949 | out: |
| 1950 | /* in each case, requeue the buffer */ |
| 1951 | ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf); |
| 1952 | if (ret2 != 0) |
| 1953 | return ret2; |
| 1954 | return ret; |
| 1955 | } |
| 1956 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 1957 | static struct v4l2_file_operations dev_fops = { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1958 | .owner = THIS_MODULE, |
| 1959 | .open = dev_open, |
| 1960 | .release = dev_close, |
| 1961 | .read = dev_read, |
| 1962 | .mmap = dev_mmap, |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 1963 | .unlocked_ioctl = video_ioctl2, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1964 | .poll = dev_poll, |
| 1965 | }; |
| 1966 | |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1967 | static const struct v4l2_ioctl_ops dev_ioctl_ops = { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1968 | .vidioc_querycap = vidioc_querycap, |
| 1969 | .vidioc_dqbuf = vidioc_dqbuf, |
| 1970 | .vidioc_qbuf = vidioc_qbuf, |
Mauro Carvalho Chehab | e078770 | 2008-07-01 04:06:22 -0300 | [diff] [blame] | 1971 | .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, |
| 1972 | .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, |
| 1973 | .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, |
| 1974 | .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1975 | .vidioc_streamon = vidioc_streamon, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1976 | .vidioc_enum_input = vidioc_enum_input, |
| 1977 | .vidioc_g_input = vidioc_g_input, |
| 1978 | .vidioc_s_input = vidioc_s_input, |
| 1979 | .vidioc_reqbufs = vidioc_reqbufs, |
| 1980 | .vidioc_querybuf = vidioc_querybuf, |
| 1981 | .vidioc_streamoff = vidioc_streamoff, |
| 1982 | .vidioc_g_jpegcomp = vidioc_g_jpegcomp, |
| 1983 | .vidioc_s_jpegcomp = vidioc_s_jpegcomp, |
| 1984 | .vidioc_g_parm = vidioc_g_parm, |
| 1985 | .vidioc_s_parm = vidioc_s_parm, |
Hans de Goede | 11c635a | 2009-05-25 15:04:22 -0300 | [diff] [blame] | 1986 | .vidioc_enum_framesizes = vidioc_enum_framesizes, |
Antonio Ospite | 28ffe77 | 2009-12-02 06:18:46 -0300 | [diff] [blame] | 1987 | .vidioc_enum_frameintervals = vidioc_enum_frameintervals, |
Brian Johnson | af1d9af | 2009-07-19 05:29:20 -0300 | [diff] [blame] | 1988 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
Hans Verkuil | b1c85cc | 2013-05-29 06:59:42 -0300 | [diff] [blame] | 1989 | .vidioc_g_chip_info = vidioc_g_chip_info, |
Brian Johnson | af1d9af | 2009-07-19 05:29:20 -0300 | [diff] [blame] | 1990 | .vidioc_g_register = vidioc_g_register, |
| 1991 | .vidioc_s_register = vidioc_s_register, |
| 1992 | #endif |
Hans Verkuil | d57fb9f | 2012-05-06 09:28:20 -0300 | [diff] [blame] | 1993 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, |
| 1994 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1995 | }; |
| 1996 | |
Jean-François Moine | 59929ce | 2011-03-14 05:33:01 -0300 | [diff] [blame] | 1997 | static const struct video_device gspca_template = { |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1998 | .name = "gspca main driver", |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1999 | .fops = &dev_fops, |
| 2000 | .ioctl_ops = &dev_ioctl_ops, |
Hans Verkuil | a3d6e8c | 2012-05-06 09:28:27 -0300 | [diff] [blame] | 2001 | .release = video_device_release_empty, /* We use v4l2_dev.release */ |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 2002 | }; |
| 2003 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2004 | /* |
| 2005 | * probe and create a new gspca device |
| 2006 | * |
| 2007 | * This function must be called by the sub-driver when it is |
| 2008 | * called for probing a new device. |
| 2009 | */ |
Jean-François Moine | 4462864 | 2010-06-05 07:47:36 -0300 | [diff] [blame] | 2010 | int gspca_dev_probe2(struct usb_interface *intf, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2011 | const struct usb_device_id *id, |
| 2012 | const struct sd_desc *sd_desc, |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 2013 | int dev_size, |
| 2014 | struct module *module) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2015 | { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2016 | struct gspca_dev *gspca_dev; |
| 2017 | struct usb_device *dev = interface_to_usbdev(intf); |
| 2018 | int ret; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2019 | |
Jean-François Moine | ee7dd4e | 2011-09-23 05:25:28 -0300 | [diff] [blame] | 2020 | pr_info("%s-" GSPCA_VERSION " probing %04x:%04x\n", |
| 2021 | sd_desc->name, id->idVendor, id->idProduct); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2022 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2023 | /* create the device */ |
| 2024 | if (dev_size < sizeof *gspca_dev) |
| 2025 | dev_size = sizeof *gspca_dev; |
| 2026 | gspca_dev = kzalloc(dev_size, GFP_KERNEL); |
Erik Andren | e293e59 | 2008-10-03 08:46:50 -0300 | [diff] [blame] | 2027 | if (!gspca_dev) { |
Joe Perches | 133a9fe | 2011-08-21 19:56:57 -0300 | [diff] [blame] | 2028 | pr_err("couldn't kzalloc gspca struct\n"); |
Erik Andren | e293e59 | 2008-10-03 08:46:50 -0300 | [diff] [blame] | 2029 | return -ENOMEM; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2030 | } |
Jean-Francois Moine | 8295d99 | 2008-09-03 17:12:19 -0300 | [diff] [blame] | 2031 | gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL); |
| 2032 | if (!gspca_dev->usb_buf) { |
Joe Perches | 133a9fe | 2011-08-21 19:56:57 -0300 | [diff] [blame] | 2033 | pr_err("out of memory\n"); |
Erik Andren | e293e59 | 2008-10-03 08:46:50 -0300 | [diff] [blame] | 2034 | ret = -ENOMEM; |
Jean-Francois Moine | 8295d99 | 2008-09-03 17:12:19 -0300 | [diff] [blame] | 2035 | goto out; |
| 2036 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2037 | gspca_dev->dev = dev; |
Jean-François Moine | 4462864 | 2010-06-05 07:47:36 -0300 | [diff] [blame] | 2038 | gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber; |
Antonio Ospite | 2fe1524 | 2014-06-25 06:27:56 -0300 | [diff] [blame] | 2039 | gspca_dev->xfer_ep = -1; |
Jean-François Moine | 35680ba | 2010-07-14 06:30:18 -0300 | [diff] [blame] | 2040 | |
| 2041 | /* check if any audio device */ |
Hans de Goede | 5dae603 | 2012-01-01 17:20:14 -0300 | [diff] [blame] | 2042 | if (dev->actconfig->desc.bNumInterfaces != 1) { |
Jean-François Moine | 35680ba | 2010-07-14 06:30:18 -0300 | [diff] [blame] | 2043 | int i; |
| 2044 | struct usb_interface *intf2; |
| 2045 | |
Hans de Goede | 5dae603 | 2012-01-01 17:20:14 -0300 | [diff] [blame] | 2046 | for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) { |
| 2047 | intf2 = dev->actconfig->interface[i]; |
Jean-François Moine | 35680ba | 2010-07-14 06:30:18 -0300 | [diff] [blame] | 2048 | if (intf2 != NULL |
| 2049 | && intf2->altsetting != NULL |
| 2050 | && intf2->altsetting->desc.bInterfaceClass == |
| 2051 | USB_CLASS_AUDIO) { |
| 2052 | gspca_dev->audio = 1; |
| 2053 | break; |
| 2054 | } |
| 2055 | } |
| 2056 | } |
| 2057 | |
Hans Verkuil | a3d6e8c | 2012-05-06 09:28:27 -0300 | [diff] [blame] | 2058 | gspca_dev->v4l2_dev.release = gspca_release; |
Hans Verkuil | 2333565 | 2012-05-06 09:28:19 -0300 | [diff] [blame] | 2059 | ret = v4l2_device_register(&intf->dev, &gspca_dev->v4l2_dev); |
| 2060 | if (ret) |
| 2061 | goto out; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2062 | gspca_dev->sd_desc = sd_desc; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2063 | gspca_dev->nbufread = 2; |
Jean-Francois Moine | ff37474 | 2008-10-23 07:29:51 -0300 | [diff] [blame] | 2064 | gspca_dev->empty_packet = -1; /* don't check the empty packets */ |
Hans Verkuil | 62bba5d | 2012-05-06 09:28:17 -0300 | [diff] [blame] | 2065 | gspca_dev->vdev = gspca_template; |
Hans Verkuil | 2333565 | 2012-05-06 09:28:19 -0300 | [diff] [blame] | 2066 | gspca_dev->vdev.v4l2_dev = &gspca_dev->v4l2_dev; |
Hans Verkuil | f4c1605 | 2012-05-06 09:28:18 -0300 | [diff] [blame] | 2067 | video_set_drvdata(&gspca_dev->vdev, gspca_dev); |
Hans Verkuil | 62bba5d | 2012-05-06 09:28:17 -0300 | [diff] [blame] | 2068 | gspca_dev->module = module; |
| 2069 | gspca_dev->present = 1; |
| 2070 | |
| 2071 | mutex_init(&gspca_dev->usb_lock); |
Hans Verkuil | a3d6e8c | 2012-05-06 09:28:27 -0300 | [diff] [blame] | 2072 | gspca_dev->vdev.lock = &gspca_dev->usb_lock; |
Hans Verkuil | 62bba5d | 2012-05-06 09:28:17 -0300 | [diff] [blame] | 2073 | mutex_init(&gspca_dev->queue_lock); |
| 2074 | init_waitqueue_head(&gspca_dev->wq); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2075 | |
Jean-Francois Moine | 012d6b0 | 2008-09-03 17:12:16 -0300 | [diff] [blame] | 2076 | /* configure the subdriver and initialize the USB device */ |
Jean-Francois Moine | ba38ee8 | 2008-11-22 04:27:34 -0300 | [diff] [blame] | 2077 | ret = sd_desc->config(gspca_dev, id); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2078 | if (ret < 0) |
| 2079 | goto out; |
Jean-Francois Moine | ba38ee8 | 2008-11-22 04:27:34 -0300 | [diff] [blame] | 2080 | ret = sd_desc->init(gspca_dev); |
Jean-Francois Moine | 012d6b0 | 2008-09-03 17:12:16 -0300 | [diff] [blame] | 2081 | if (ret < 0) |
| 2082 | goto out; |
Hans Verkuil | 62bba5d | 2012-05-06 09:28:17 -0300 | [diff] [blame] | 2083 | if (sd_desc->init_controls) |
| 2084 | ret = sd_desc->init_controls(gspca_dev); |
| 2085 | if (ret < 0) |
| 2086 | goto out; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2087 | gspca_set_default_mode(gspca_dev); |
| 2088 | |
Hans de Goede | 10ee240 | 2010-02-19 04:41:40 -0300 | [diff] [blame] | 2089 | ret = gspca_input_connect(gspca_dev); |
| 2090 | if (ret) |
| 2091 | goto out; |
| 2092 | |
Hans Verkuil | a3d6e8c | 2012-05-06 09:28:27 -0300 | [diff] [blame] | 2093 | /* |
| 2094 | * Don't take usb_lock for these ioctls. This improves latency if |
| 2095 | * usb_lock is taken for a long time, e.g. when changing a control |
| 2096 | * value, and a new frame is ready to be dequeued. |
| 2097 | */ |
Hans Verkuil | 152a3a7 | 2012-05-14 11:32:48 -0300 | [diff] [blame] | 2098 | v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_DQBUF); |
| 2099 | v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QBUF); |
| 2100 | v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QUERYBUF); |
Hans Verkuil | ac3322b | 2012-05-23 17:17:12 -0300 | [diff] [blame] | 2101 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
Hans Verkuil | b1c85cc | 2013-05-29 06:59:42 -0300 | [diff] [blame] | 2102 | if (!gspca_dev->sd_desc->get_register) |
Hans Verkuil | ac3322b | 2012-05-23 17:17:12 -0300 | [diff] [blame] | 2103 | v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_G_REGISTER); |
Hans Verkuil | b1c85cc | 2013-05-29 06:59:42 -0300 | [diff] [blame] | 2104 | if (!gspca_dev->sd_desc->set_register) |
Hans Verkuil | ac3322b | 2012-05-23 17:17:12 -0300 | [diff] [blame] | 2105 | v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_S_REGISTER); |
| 2106 | #endif |
| 2107 | if (!gspca_dev->sd_desc->get_jcomp) |
| 2108 | v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_G_JPEGCOMP); |
| 2109 | if (!gspca_dev->sd_desc->set_jcomp) |
| 2110 | v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_S_JPEGCOMP); |
Hans Verkuil | a3d6e8c | 2012-05-06 09:28:27 -0300 | [diff] [blame] | 2111 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2112 | /* init video stuff */ |
Jean-Francois Moine | a12ca6a | 2008-11-19 06:37:53 -0300 | [diff] [blame] | 2113 | ret = video_register_device(&gspca_dev->vdev, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2114 | VFL_TYPE_GRABBER, |
Jean-Francois Moine | 8d584a5 | 2009-01-13 06:07:59 -0300 | [diff] [blame] | 2115 | -1); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2116 | if (ret < 0) { |
Joe Perches | 133a9fe | 2011-08-21 19:56:57 -0300 | [diff] [blame] | 2117 | pr_err("video_register_device err %d\n", ret); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2118 | goto out; |
| 2119 | } |
| 2120 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2121 | usb_set_intfdata(intf, gspca_dev); |
Laurent Pinchart | 38c7c03 | 2009-11-27 13:57:15 -0300 | [diff] [blame] | 2122 | 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] | 2123 | |
Hans de Goede | 10ee240 | 2010-02-19 04:41:40 -0300 | [diff] [blame] | 2124 | gspca_input_create_urb(gspca_dev); |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 2125 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2126 | return 0; |
| 2127 | out: |
Peter Senna Tschudin | 13a00fa | 2013-01-24 19:28:59 -0300 | [diff] [blame] | 2128 | #if IS_ENABLED(CONFIG_INPUT) |
Hans de Goede | 10ee240 | 2010-02-19 04:41:40 -0300 | [diff] [blame] | 2129 | if (gspca_dev->input_dev) |
| 2130 | input_unregister_device(gspca_dev->input_dev); |
Hans de Goede | e763752 | 2010-02-19 05:11:09 -0300 | [diff] [blame] | 2131 | #endif |
Hans Verkuil | 62bba5d | 2012-05-06 09:28:17 -0300 | [diff] [blame] | 2132 | v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler); |
Jean-Francois Moine | e07a1d8 | 2008-11-19 06:37:53 -0300 | [diff] [blame] | 2133 | kfree(gspca_dev->usb_buf); |
| 2134 | kfree(gspca_dev); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2135 | return ret; |
| 2136 | } |
Jean-François Moine | 4462864 | 2010-06-05 07:47:36 -0300 | [diff] [blame] | 2137 | EXPORT_SYMBOL(gspca_dev_probe2); |
| 2138 | |
| 2139 | /* same function as the previous one, but check the interface */ |
| 2140 | int gspca_dev_probe(struct usb_interface *intf, |
| 2141 | const struct usb_device_id *id, |
| 2142 | const struct sd_desc *sd_desc, |
| 2143 | int dev_size, |
| 2144 | struct module *module) |
| 2145 | { |
| 2146 | struct usb_device *dev = interface_to_usbdev(intf); |
| 2147 | |
| 2148 | /* we don't handle multi-config cameras */ |
| 2149 | if (dev->descriptor.bNumConfigurations != 1) { |
Joe Perches | 133a9fe | 2011-08-21 19:56:57 -0300 | [diff] [blame] | 2150 | pr_err("%04x:%04x too many config\n", |
| 2151 | id->idVendor, id->idProduct); |
Jean-François Moine | 4462864 | 2010-06-05 07:47:36 -0300 | [diff] [blame] | 2152 | return -ENODEV; |
| 2153 | } |
| 2154 | |
| 2155 | /* the USB video interface must be the first one */ |
Hans de Goede | 5dae603 | 2012-01-01 17:20:14 -0300 | [diff] [blame] | 2156 | if (dev->actconfig->desc.bNumInterfaces != 1 |
Jean-François Moine | c4dc692 | 2010-07-14 06:26:54 -0300 | [diff] [blame] | 2157 | && intf->cur_altsetting->desc.bInterfaceNumber != 0) |
Jean-François Moine | 4462864 | 2010-06-05 07:47:36 -0300 | [diff] [blame] | 2158 | return -ENODEV; |
Jean-François Moine | 4462864 | 2010-06-05 07:47:36 -0300 | [diff] [blame] | 2159 | |
| 2160 | return gspca_dev_probe2(intf, id, sd_desc, dev_size, module); |
| 2161 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2162 | EXPORT_SYMBOL(gspca_dev_probe); |
| 2163 | |
| 2164 | /* |
| 2165 | * USB disconnection |
| 2166 | * |
| 2167 | * This function must be called by the sub-driver |
| 2168 | * when the device disconnects, after the specific resources are freed. |
| 2169 | */ |
| 2170 | void gspca_disconnect(struct usb_interface *intf) |
| 2171 | { |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 2172 | struct gspca_dev *gspca_dev = usb_get_intfdata(intf); |
Peter Senna Tschudin | 13a00fa | 2013-01-24 19:28:59 -0300 | [diff] [blame] | 2173 | #if IS_ENABLED(CONFIG_INPUT) |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 2174 | struct input_dev *input_dev; |
Jean-François Moine | e070bf9 | 2010-02-18 15:02:51 -0300 | [diff] [blame] | 2175 | #endif |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2176 | |
Laurent Pinchart | 38c7c03 | 2009-11-27 13:57:15 -0300 | [diff] [blame] | 2177 | PDEBUG(D_PROBE, "%s disconnect", |
| 2178 | video_device_node_name(&gspca_dev->vdev)); |
Hans Verkuil | a3d6e8c | 2012-05-06 09:28:27 -0300 | [diff] [blame] | 2179 | |
Adam Baker | ad28127 | 2009-02-04 15:33:21 -0300 | [diff] [blame] | 2180 | mutex_lock(&gspca_dev->usb_lock); |
Jean-Francois Moine | 090264e | 2009-02-12 08:05:45 -0300 | [diff] [blame] | 2181 | |
Hans de Goede | 4e770f7 | 2010-12-30 13:11:21 -0300 | [diff] [blame] | 2182 | gspca_dev->present = 0; |
Hans de Goede | 4e770f7 | 2010-12-30 13:11:21 -0300 | [diff] [blame] | 2183 | destroy_urbs(gspca_dev); |
Jean-Francois Moine | 090264e | 2009-02-12 08:05:45 -0300 | [diff] [blame] | 2184 | |
Peter Senna Tschudin | 13a00fa | 2013-01-24 19:28:59 -0300 | [diff] [blame] | 2185 | #if IS_ENABLED(CONFIG_INPUT) |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 2186 | gspca_input_destroy_urb(gspca_dev); |
| 2187 | input_dev = gspca_dev->input_dev; |
| 2188 | if (input_dev) { |
| 2189 | gspca_dev->input_dev = NULL; |
| 2190 | input_unregister_device(input_dev); |
| 2191 | } |
Jean-François Moine | e070bf9 | 2010-02-18 15:02:51 -0300 | [diff] [blame] | 2192 | #endif |
Hans de Goede | eb23873 | 2012-05-07 06:25:30 -0300 | [diff] [blame] | 2193 | /* Free subdriver's streaming resources / stop sd workqueue(s) */ |
| 2194 | if (gspca_dev->sd_desc->stop0 && gspca_dev->streaming) |
| 2195 | gspca_dev->sd_desc->stop0(gspca_dev); |
| 2196 | gspca_dev->streaming = 0; |
Hans de Goede | 345321d | 2012-09-09 06:30:02 -0300 | [diff] [blame] | 2197 | gspca_dev->dev = NULL; |
Hans de Goede | eb23873 | 2012-05-07 06:25:30 -0300 | [diff] [blame] | 2198 | wake_up_interruptible(&gspca_dev->wq); |
Márton Németh | 0274d42 | 2010-01-28 06:39:49 -0300 | [diff] [blame] | 2199 | |
Hans Verkuil | 2333565 | 2012-05-06 09:28:19 -0300 | [diff] [blame] | 2200 | v4l2_device_disconnect(&gspca_dev->v4l2_dev); |
Jean-Francois Moine | a12ca6a | 2008-11-19 06:37:53 -0300 | [diff] [blame] | 2201 | video_unregister_device(&gspca_dev->vdev); |
Jean-Francois Moine | 3145b8c | 2008-10-05 04:22:10 -0300 | [diff] [blame] | 2202 | |
Hans Verkuil | a3d6e8c | 2012-05-06 09:28:27 -0300 | [diff] [blame] | 2203 | mutex_unlock(&gspca_dev->usb_lock); |
| 2204 | |
| 2205 | /* (this will call gspca_release() immediately or on last close) */ |
| 2206 | v4l2_device_put(&gspca_dev->v4l2_dev); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2207 | } |
| 2208 | EXPORT_SYMBOL(gspca_disconnect); |
| 2209 | |
Jean-Francois Moine | 6a70974 | 2008-09-03 16:48:10 -0300 | [diff] [blame] | 2210 | #ifdef CONFIG_PM |
| 2211 | int gspca_suspend(struct usb_interface *intf, pm_message_t message) |
| 2212 | { |
| 2213 | struct gspca_dev *gspca_dev = usb_get_intfdata(intf); |
| 2214 | |
Hans de Goede | 36adfca | 2012-09-09 08:04:05 -0300 | [diff] [blame] | 2215 | gspca_input_destroy_urb(gspca_dev); |
| 2216 | |
Jean-Francois Moine | 6a70974 | 2008-09-03 16:48:10 -0300 | [diff] [blame] | 2217 | if (!gspca_dev->streaming) |
| 2218 | return 0; |
Hans de Goede | 36adfca | 2012-09-09 08:04:05 -0300 | [diff] [blame] | 2219 | |
Hans Verkuil | a3d6e8c | 2012-05-06 09:28:27 -0300 | [diff] [blame] | 2220 | mutex_lock(&gspca_dev->usb_lock); |
Jean-Francois Moine | 6a70974 | 2008-09-03 16:48:10 -0300 | [diff] [blame] | 2221 | gspca_dev->frozen = 1; /* avoid urb error messages */ |
Hans de Goede | 9a190c8 | 2012-05-09 12:11:12 -0300 | [diff] [blame] | 2222 | gspca_dev->usb_err = 0; |
Jean-Francois Moine | 012d6b0 | 2008-09-03 17:12:16 -0300 | [diff] [blame] | 2223 | if (gspca_dev->sd_desc->stopN) |
| 2224 | gspca_dev->sd_desc->stopN(gspca_dev); |
Jean-Francois Moine | 6a70974 | 2008-09-03 16:48:10 -0300 | [diff] [blame] | 2225 | destroy_urbs(gspca_dev); |
| 2226 | gspca_set_alt0(gspca_dev); |
Hans Verkuil | a3d6e8c | 2012-05-06 09:28:27 -0300 | [diff] [blame] | 2227 | if (gspca_dev->sd_desc->stop0) |
Jean-Francois Moine | 012d6b0 | 2008-09-03 17:12:16 -0300 | [diff] [blame] | 2228 | gspca_dev->sd_desc->stop0(gspca_dev); |
Hans Verkuil | a3d6e8c | 2012-05-06 09:28:27 -0300 | [diff] [blame] | 2229 | mutex_unlock(&gspca_dev->usb_lock); |
Hans de Goede | 36adfca | 2012-09-09 08:04:05 -0300 | [diff] [blame] | 2230 | |
Jean-Francois Moine | 6a70974 | 2008-09-03 16:48:10 -0300 | [diff] [blame] | 2231 | return 0; |
| 2232 | } |
| 2233 | EXPORT_SYMBOL(gspca_suspend); |
| 2234 | |
| 2235 | int gspca_resume(struct usb_interface *intf) |
| 2236 | { |
| 2237 | struct gspca_dev *gspca_dev = usb_get_intfdata(intf); |
Hans Verkuil | 254902b | 2012-05-06 09:28:22 -0300 | [diff] [blame] | 2238 | int streaming, ret = 0; |
Jean-Francois Moine | 6a70974 | 2008-09-03 16:48:10 -0300 | [diff] [blame] | 2239 | |
Hans Verkuil | a3d6e8c | 2012-05-06 09:28:27 -0300 | [diff] [blame] | 2240 | mutex_lock(&gspca_dev->usb_lock); |
Jean-Francois Moine | 6a70974 | 2008-09-03 16:48:10 -0300 | [diff] [blame] | 2241 | gspca_dev->frozen = 0; |
Hans de Goede | 9a190c8 | 2012-05-09 12:11:12 -0300 | [diff] [blame] | 2242 | gspca_dev->usb_err = 0; |
Jean-Francois Moine | 012d6b0 | 2008-09-03 17:12:16 -0300 | [diff] [blame] | 2243 | gspca_dev->sd_desc->init(gspca_dev); |
Hans Verkuil | 62bba5d | 2012-05-06 09:28:17 -0300 | [diff] [blame] | 2244 | /* |
| 2245 | * Most subdrivers send all ctrl values on sd_start and thus |
| 2246 | * only write to the device registers on s_ctrl when streaming -> |
| 2247 | * Clear streaming to avoid setting all ctrls twice. |
| 2248 | */ |
| 2249 | streaming = gspca_dev->streaming; |
| 2250 | gspca_dev->streaming = 0; |
Hans Verkuil | a3d6e8c | 2012-05-06 09:28:27 -0300 | [diff] [blame] | 2251 | if (streaming) |
Hans Verkuil | 254902b | 2012-05-06 09:28:22 -0300 | [diff] [blame] | 2252 | ret = gspca_init_transfer(gspca_dev); |
Hans de Goede | 36adfca | 2012-09-09 08:04:05 -0300 | [diff] [blame] | 2253 | else |
| 2254 | gspca_input_create_urb(gspca_dev); |
Hans Verkuil | a3d6e8c | 2012-05-06 09:28:27 -0300 | [diff] [blame] | 2255 | mutex_unlock(&gspca_dev->usb_lock); |
Hans de Goede | 36adfca | 2012-09-09 08:04:05 -0300 | [diff] [blame] | 2256 | |
Hans Verkuil | 254902b | 2012-05-06 09:28:22 -0300 | [diff] [blame] | 2257 | return ret; |
Jean-Francois Moine | 6a70974 | 2008-09-03 16:48:10 -0300 | [diff] [blame] | 2258 | } |
| 2259 | EXPORT_SYMBOL(gspca_resume); |
| 2260 | #endif |
Hans de Goede | dcef323 | 2008-07-10 10:40:53 -0300 | [diff] [blame] | 2261 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2262 | /* -- module insert / remove -- */ |
| 2263 | static int __init gspca_init(void) |
| 2264 | { |
Jean-François Moine | f22123f | 2011-09-23 05:17:10 -0300 | [diff] [blame] | 2265 | pr_info("v" GSPCA_VERSION " registered\n"); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2266 | return 0; |
| 2267 | } |
| 2268 | static void __exit gspca_exit(void) |
| 2269 | { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2270 | } |
| 2271 | |
| 2272 | module_init(gspca_init); |
| 2273 | module_exit(gspca_exit); |
| 2274 | |
| 2275 | module_param_named(debug, gspca_debug, int, 0644); |
| 2276 | MODULE_PARM_DESC(debug, |
Theodore Kilgore | c93396e | 2013-02-04 13:17:55 -0300 | [diff] [blame] | 2277 | "1:probe 2:config 3:stream 4:frame 5:packet 6:usbi 7:usbo"); |