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