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