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