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