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