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