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