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