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