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