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