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