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