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