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) |
Frank Zago | 9de436c | 2008-09-15 05:20:38 -0300 | [diff] [blame^] | 846 | gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL |
| 847 | | V4L2_DEBUG_IOCTL_ARG; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 848 | else |
Frank Zago | 9de436c | 2008-09-15 05:20:38 -0300 | [diff] [blame^] | 849 | gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL |
| 850 | | V4L2_DEBUG_IOCTL_ARG); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 851 | #endif |
Jean-Francois Moine | 012d6b0 | 2008-09-03 17:12:16 -0300 | [diff] [blame] | 852 | ret = 0; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 853 | out: |
| 854 | mutex_unlock(&gspca_dev->queue_lock); |
| 855 | if (ret != 0) |
| 856 | PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret); |
| 857 | else |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 858 | PDEBUG(D_STREAM, "open done"); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 859 | return ret; |
| 860 | } |
| 861 | |
| 862 | static int dev_close(struct inode *inode, struct file *file) |
| 863 | { |
| 864 | struct gspca_dev *gspca_dev = file->private_data; |
| 865 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 866 | PDEBUG(D_STREAM, "%s close", current->comm); |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 867 | if (mutex_lock_interruptible(&gspca_dev->queue_lock)) |
| 868 | return -ERESTARTSYS; |
| 869 | gspca_dev->users--; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 870 | |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 871 | /* if the file did the capture, free the streaming resources */ |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 872 | if (gspca_dev->capt_file == file) { |
Jean-Francois Moine | a1e8288 | 2008-09-03 17:12:17 -0300 | [diff] [blame] | 873 | if (gspca_dev->streaming) { |
| 874 | mutex_lock(&gspca_dev->usb_lock); |
| 875 | gspca_stream_off(gspca_dev); |
| 876 | mutex_unlock(&gspca_dev->usb_lock); |
| 877 | } |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 878 | frame_free(gspca_dev); |
Harvey Harrison | efab821 | 2008-07-05 06:12:47 -0300 | [diff] [blame] | 879 | gspca_dev->capt_file = NULL; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 880 | gspca_dev->memory = GSPCA_MEMORY_NO; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 881 | } |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 882 | file->private_data = NULL; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 883 | mutex_unlock(&gspca_dev->queue_lock); |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 884 | PDEBUG(D_STREAM, "close done"); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 885 | return 0; |
| 886 | } |
| 887 | |
| 888 | static int vidioc_querycap(struct file *file, void *priv, |
| 889 | struct v4l2_capability *cap) |
| 890 | { |
| 891 | struct gspca_dev *gspca_dev = priv; |
| 892 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 893 | memset(cap, 0, sizeof *cap); |
| 894 | strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver); |
Jean-Francois Moine | c6edaf1 | 2008-07-23 03:24:06 -0300 | [diff] [blame] | 895 | /* strncpy(cap->card, gspca_dev->cam.dev_name, sizeof cap->card); */ |
| 896 | if (gspca_dev->dev->product != NULL) { |
| 897 | strncpy(cap->card, gspca_dev->dev->product, |
| 898 | sizeof cap->card); |
| 899 | } else { |
| 900 | snprintf(cap->card, sizeof cap->card, |
| 901 | "USB Camera (%04x:%04x)", |
| 902 | le16_to_cpu(gspca_dev->dev->descriptor.idVendor), |
| 903 | le16_to_cpu(gspca_dev->dev->descriptor.idProduct)); |
| 904 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 905 | strncpy(cap->bus_info, gspca_dev->dev->bus->bus_name, |
| 906 | sizeof cap->bus_info); |
| 907 | cap->version = DRIVER_VERSION_NUMBER; |
| 908 | cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
| 909 | | V4L2_CAP_STREAMING |
| 910 | | V4L2_CAP_READWRITE; |
| 911 | return 0; |
| 912 | } |
| 913 | |
| 914 | static int vidioc_queryctrl(struct file *file, void *priv, |
| 915 | struct v4l2_queryctrl *q_ctrl) |
| 916 | { |
| 917 | struct gspca_dev *gspca_dev = priv; |
Jean-Francois Moine | f50ba1b | 2008-09-03 17:12:14 -0300 | [diff] [blame] | 918 | int i, ix; |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 919 | u32 id; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 920 | |
Jean-Francois Moine | f50ba1b | 2008-09-03 17:12:14 -0300 | [diff] [blame] | 921 | ix = -1; |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 922 | id = q_ctrl->id; |
| 923 | if (id & V4L2_CTRL_FLAG_NEXT_CTRL) { |
| 924 | id &= V4L2_CTRL_ID_MASK; |
| 925 | id++; |
| 926 | for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) { |
Hans de Goede | 0f523c2 | 2008-09-03 17:12:14 -0300 | [diff] [blame] | 927 | if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id) |
Jean-Francois Moine | f50ba1b | 2008-09-03 17:12:14 -0300 | [diff] [blame] | 928 | continue; |
| 929 | if (ix < 0) { |
| 930 | ix = i; |
| 931 | continue; |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 932 | } |
Jean-Francois Moine | f50ba1b | 2008-09-03 17:12:14 -0300 | [diff] [blame] | 933 | if (gspca_dev->sd_desc->ctrls[i].qctrl.id |
| 934 | > gspca_dev->sd_desc->ctrls[ix].qctrl.id) |
| 935 | continue; |
| 936 | ix = i; |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 937 | } |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 938 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 939 | for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) { |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 940 | if (id == gspca_dev->sd_desc->ctrls[i].qctrl.id) { |
Jean-Francois Moine | f50ba1b | 2008-09-03 17:12:14 -0300 | [diff] [blame] | 941 | ix = i; |
| 942 | break; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 943 | } |
| 944 | } |
Jean-Francois Moine | f50ba1b | 2008-09-03 17:12:14 -0300 | [diff] [blame] | 945 | if (ix < 0) |
| 946 | return -EINVAL; |
| 947 | memcpy(q_ctrl, &gspca_dev->sd_desc->ctrls[ix].qctrl, |
| 948 | sizeof *q_ctrl); |
| 949 | if (gspca_dev->ctrl_dis & (1 << ix)) |
| 950 | q_ctrl->flags |= V4L2_CTRL_FLAG_DISABLED; |
| 951 | return 0; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | static int vidioc_s_ctrl(struct file *file, void *priv, |
| 955 | struct v4l2_control *ctrl) |
| 956 | { |
| 957 | struct gspca_dev *gspca_dev = priv; |
Jean-Francois Moine | a5ae206 | 2008-07-04 11:16:16 -0300 | [diff] [blame] | 958 | const struct ctrl *ctrls; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 959 | int i, ret; |
| 960 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 961 | for (i = 0, ctrls = gspca_dev->sd_desc->ctrls; |
| 962 | i < gspca_dev->sd_desc->nctrls; |
| 963 | i++, ctrls++) { |
| 964 | if (ctrl->id != ctrls->qctrl.id) |
| 965 | continue; |
Hans de Goede | 0f523c2 | 2008-09-03 17:12:14 -0300 | [diff] [blame] | 966 | if (gspca_dev->ctrl_dis & (1 << i)) |
| 967 | return -EINVAL; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 968 | if (ctrl->value < ctrls->qctrl.minimum |
Rabin Vincent | fcf5cb2 | 2008-08-03 07:58:54 -0300 | [diff] [blame] | 969 | || ctrl->value > ctrls->qctrl.maximum) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 970 | return -ERANGE; |
| 971 | PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value); |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 972 | if (mutex_lock_interruptible(&gspca_dev->usb_lock)) |
| 973 | return -ERESTARTSYS; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 974 | ret = ctrls->set(gspca_dev, ctrl->value); |
| 975 | mutex_unlock(&gspca_dev->usb_lock); |
| 976 | return ret; |
| 977 | } |
| 978 | return -EINVAL; |
| 979 | } |
| 980 | |
| 981 | static int vidioc_g_ctrl(struct file *file, void *priv, |
| 982 | struct v4l2_control *ctrl) |
| 983 | { |
| 984 | struct gspca_dev *gspca_dev = priv; |
| 985 | |
Jean-Francois Moine | a5ae206 | 2008-07-04 11:16:16 -0300 | [diff] [blame] | 986 | const struct ctrl *ctrls; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 987 | int i, ret; |
| 988 | |
| 989 | for (i = 0, ctrls = gspca_dev->sd_desc->ctrls; |
| 990 | i < gspca_dev->sd_desc->nctrls; |
| 991 | i++, ctrls++) { |
| 992 | if (ctrl->id != ctrls->qctrl.id) |
| 993 | continue; |
Hans de Goede | 0f523c2 | 2008-09-03 17:12:14 -0300 | [diff] [blame] | 994 | if (gspca_dev->ctrl_dis & (1 << i)) |
| 995 | return -EINVAL; |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 996 | if (mutex_lock_interruptible(&gspca_dev->usb_lock)) |
| 997 | return -ERESTARTSYS; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 998 | ret = ctrls->get(gspca_dev, &ctrl->value); |
| 999 | mutex_unlock(&gspca_dev->usb_lock); |
| 1000 | return ret; |
| 1001 | } |
| 1002 | return -EINVAL; |
| 1003 | } |
| 1004 | |
| 1005 | static int vidioc_querymenu(struct file *file, void *priv, |
| 1006 | struct v4l2_querymenu *qmenu) |
| 1007 | { |
| 1008 | struct gspca_dev *gspca_dev = priv; |
| 1009 | |
| 1010 | if (!gspca_dev->sd_desc->querymenu) |
| 1011 | return -EINVAL; |
| 1012 | return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu); |
| 1013 | } |
| 1014 | |
| 1015 | static int vidioc_enum_input(struct file *file, void *priv, |
| 1016 | struct v4l2_input *input) |
| 1017 | { |
| 1018 | struct gspca_dev *gspca_dev = priv; |
| 1019 | |
| 1020 | if (input->index != 0) |
| 1021 | return -EINVAL; |
| 1022 | memset(input, 0, sizeof *input); |
| 1023 | input->type = V4L2_INPUT_TYPE_CAMERA; |
| 1024 | strncpy(input->name, gspca_dev->sd_desc->name, |
| 1025 | sizeof input->name); |
| 1026 | return 0; |
| 1027 | } |
| 1028 | |
| 1029 | static int vidioc_g_input(struct file *file, void *priv, unsigned int *i) |
| 1030 | { |
| 1031 | *i = 0; |
| 1032 | return 0; |
| 1033 | } |
| 1034 | |
| 1035 | static int vidioc_s_input(struct file *file, void *priv, unsigned int i) |
| 1036 | { |
| 1037 | if (i > 0) |
| 1038 | return -EINVAL; |
| 1039 | return (0); |
| 1040 | } |
| 1041 | |
| 1042 | static int vidioc_reqbufs(struct file *file, void *priv, |
| 1043 | struct v4l2_requestbuffers *rb) |
| 1044 | { |
| 1045 | struct gspca_dev *gspca_dev = priv; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1046 | int i, ret = 0; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1047 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1048 | if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1049 | return -EINVAL; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1050 | switch (rb->memory) { |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1051 | case GSPCA_MEMORY_READ: /* (internal call) */ |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1052 | case V4L2_MEMORY_MMAP: |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1053 | case V4L2_MEMORY_USERPTR: |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 1054 | break; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1055 | default: |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1056 | return -EINVAL; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1057 | } |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1058 | if (mutex_lock_interruptible(&gspca_dev->queue_lock)) |
| 1059 | return -ERESTARTSYS; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1060 | |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1061 | if (gspca_dev->memory != GSPCA_MEMORY_NO |
| 1062 | && gspca_dev->memory != rb->memory) { |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1063 | ret = -EBUSY; |
| 1064 | goto out; |
| 1065 | } |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1066 | |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1067 | /* only one file may do the capture */ |
| 1068 | if (gspca_dev->capt_file != NULL |
| 1069 | && gspca_dev->capt_file != file) { |
| 1070 | ret = -EBUSY; |
| 1071 | goto out; |
| 1072 | } |
| 1073 | |
| 1074 | /* if allocated, the buffers must not be mapped */ |
| 1075 | for (i = 0; i < gspca_dev->nframes; i++) { |
| 1076 | if (gspca_dev->frame[i].vma_use_count) { |
Jean-Francois Moine | ac0d6df | 2008-07-08 06:58:15 -0300 | [diff] [blame] | 1077 | ret = -EBUSY; |
| 1078 | goto out; |
| 1079 | } |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | /* stop streaming */ |
| 1083 | if (gspca_dev->streaming) { |
| 1084 | mutex_lock(&gspca_dev->usb_lock); |
| 1085 | gspca_stream_off(gspca_dev); |
| 1086 | mutex_unlock(&gspca_dev->usb_lock); |
| 1087 | } |
| 1088 | |
| 1089 | /* free the previous allocated buffers, if any */ |
| 1090 | if (gspca_dev->nframes != 0) { |
| 1091 | frame_free(gspca_dev); |
| 1092 | gspca_dev->capt_file = NULL; |
| 1093 | } |
| 1094 | if (rb->count == 0) /* unrequest */ |
| 1095 | goto out; |
| 1096 | gspca_dev->memory = rb->memory; |
| 1097 | ret = frame_alloc(gspca_dev, rb->count); |
| 1098 | if (ret == 0) { |
| 1099 | rb->count = gspca_dev->nframes; |
| 1100 | gspca_dev->capt_file = file; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1101 | } |
| 1102 | out: |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1103 | mutex_unlock(&gspca_dev->queue_lock); |
| 1104 | PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count); |
| 1105 | return ret; |
| 1106 | } |
| 1107 | |
| 1108 | static int vidioc_querybuf(struct file *file, void *priv, |
| 1109 | struct v4l2_buffer *v4l2_buf) |
| 1110 | { |
| 1111 | struct gspca_dev *gspca_dev = priv; |
| 1112 | struct gspca_frame *frame; |
| 1113 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1114 | if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE |
| 1115 | || v4l2_buf->index < 0 |
| 1116 | || v4l2_buf->index >= gspca_dev->nframes) |
| 1117 | return -EINVAL; |
| 1118 | |
| 1119 | frame = &gspca_dev->frame[v4l2_buf->index]; |
| 1120 | memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf); |
| 1121 | return 0; |
| 1122 | } |
| 1123 | |
| 1124 | static int vidioc_streamon(struct file *file, void *priv, |
| 1125 | enum v4l2_buf_type buf_type) |
| 1126 | { |
| 1127 | struct gspca_dev *gspca_dev = priv; |
| 1128 | int ret; |
| 1129 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1130 | if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1131 | return -EINVAL; |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1132 | if (mutex_lock_interruptible(&gspca_dev->queue_lock)) |
| 1133 | return -ERESTARTSYS; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1134 | if (!gspca_dev->present) { |
| 1135 | ret = -ENODEV; |
| 1136 | goto out; |
| 1137 | } |
| 1138 | if (gspca_dev->nframes == 0) { |
| 1139 | ret = -EINVAL; |
| 1140 | goto out; |
| 1141 | } |
| 1142 | if (!gspca_dev->streaming) { |
| 1143 | ret = gspca_init_transfer(gspca_dev); |
| 1144 | if (ret < 0) |
| 1145 | goto out; |
| 1146 | } |
Jean-Francois Moine | 335b3f8 | 2008-07-30 04:53:02 -0300 | [diff] [blame] | 1147 | #ifdef GSPCA_DEBUG |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1148 | if (gspca_debug & D_STREAM) { |
| 1149 | PDEBUG_MODE("stream on OK", |
| 1150 | gspca_dev->pixfmt, |
| 1151 | gspca_dev->width, |
| 1152 | gspca_dev->height); |
| 1153 | } |
| 1154 | #endif |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1155 | ret = 0; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1156 | out: |
| 1157 | mutex_unlock(&gspca_dev->queue_lock); |
| 1158 | return ret; |
| 1159 | } |
| 1160 | |
| 1161 | static int vidioc_streamoff(struct file *file, void *priv, |
| 1162 | enum v4l2_buf_type buf_type) |
| 1163 | { |
| 1164 | struct gspca_dev *gspca_dev = priv; |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1165 | int i, ret; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1166 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1167 | if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1168 | return -EINVAL; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1169 | if (!gspca_dev->streaming) |
| 1170 | return 0; |
| 1171 | if (mutex_lock_interruptible(&gspca_dev->queue_lock)) |
| 1172 | return -ERESTARTSYS; |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1173 | |
| 1174 | /* stop streaming */ |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1175 | if (mutex_lock_interruptible(&gspca_dev->usb_lock)) { |
| 1176 | ret = -ERESTARTSYS; |
| 1177 | goto out; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1178 | } |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1179 | gspca_stream_off(gspca_dev); |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1180 | mutex_unlock(&gspca_dev->usb_lock); |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1181 | |
| 1182 | /* empty the application queues */ |
| 1183 | for (i = 0; i < gspca_dev->nframes; i++) |
| 1184 | gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS; |
| 1185 | gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0; |
| 1186 | gspca_dev->last_packet_type = DISCARD_PACKET; |
| 1187 | gspca_dev->sequence = 0; |
| 1188 | atomic_set(&gspca_dev->nevent, 0); |
| 1189 | ret = 0; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1190 | out: |
| 1191 | mutex_unlock(&gspca_dev->queue_lock); |
| 1192 | return ret; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | static int vidioc_g_jpegcomp(struct file *file, void *priv, |
| 1196 | struct v4l2_jpegcompression *jpegcomp) |
| 1197 | { |
| 1198 | struct gspca_dev *gspca_dev = priv; |
| 1199 | int ret; |
| 1200 | |
| 1201 | if (!gspca_dev->sd_desc->get_jcomp) |
| 1202 | return -EINVAL; |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1203 | if (mutex_lock_interruptible(&gspca_dev->usb_lock)) |
| 1204 | return -ERESTARTSYS; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1205 | ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp); |
| 1206 | mutex_unlock(&gspca_dev->usb_lock); |
| 1207 | return ret; |
| 1208 | } |
| 1209 | |
| 1210 | static int vidioc_s_jpegcomp(struct file *file, void *priv, |
| 1211 | struct v4l2_jpegcompression *jpegcomp) |
| 1212 | { |
| 1213 | struct gspca_dev *gspca_dev = priv; |
| 1214 | int ret; |
| 1215 | |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1216 | if (mutex_lock_interruptible(&gspca_dev->usb_lock)) |
| 1217 | return -ERESTARTSYS; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1218 | if (!gspca_dev->sd_desc->set_jcomp) |
| 1219 | return -EINVAL; |
| 1220 | ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp); |
| 1221 | mutex_unlock(&gspca_dev->usb_lock); |
| 1222 | return ret; |
| 1223 | } |
| 1224 | |
| 1225 | static int vidioc_g_parm(struct file *filp, void *priv, |
| 1226 | struct v4l2_streamparm *parm) |
| 1227 | { |
| 1228 | struct gspca_dev *gspca_dev = priv; |
| 1229 | |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 1230 | memset(parm, 0, sizeof *parm); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1231 | parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 1232 | parm->parm.capture.readbuffers = gspca_dev->nbufread; |
| 1233 | return 0; |
| 1234 | } |
| 1235 | |
| 1236 | static int vidioc_s_parm(struct file *filp, void *priv, |
| 1237 | struct v4l2_streamparm *parm) |
| 1238 | { |
| 1239 | struct gspca_dev *gspca_dev = priv; |
| 1240 | int n; |
| 1241 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1242 | n = parm->parm.capture.readbuffers; |
| 1243 | if (n == 0 || n > GSPCA_MAX_FRAMES) |
| 1244 | parm->parm.capture.readbuffers = gspca_dev->nbufread; |
| 1245 | else |
| 1246 | gspca_dev->nbufread = n; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1247 | return 0; |
| 1248 | } |
| 1249 | |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 1250 | static int vidioc_s_std(struct file *filp, void *priv, |
| 1251 | v4l2_std_id *parm) |
| 1252 | { |
| 1253 | return 0; |
| 1254 | } |
| 1255 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1256 | #ifdef CONFIG_VIDEO_V4L1_COMPAT |
| 1257 | static int vidiocgmbuf(struct file *file, void *priv, |
| 1258 | struct video_mbuf *mbuf) |
| 1259 | { |
| 1260 | struct gspca_dev *gspca_dev = file->private_data; |
| 1261 | int i; |
| 1262 | |
| 1263 | PDEBUG(D_STREAM, "cgmbuf"); |
| 1264 | if (gspca_dev->nframes == 0) { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1265 | int ret; |
| 1266 | |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 1267 | { |
| 1268 | struct v4l2_format fmt; |
| 1269 | |
| 1270 | memset(&fmt, 0, sizeof fmt); |
| 1271 | fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 1272 | i = gspca_dev->cam.nmodes - 1; /* highest mode */ |
| 1273 | fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width; |
| 1274 | fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height; |
| 1275 | fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24; |
Mauro Carvalho Chehab | e078770 | 2008-07-01 04:06:22 -0300 | [diff] [blame] | 1276 | ret = vidioc_s_fmt_vid_cap(file, priv, &fmt); |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 1277 | if (ret != 0) |
| 1278 | return ret; |
| 1279 | } |
| 1280 | { |
| 1281 | struct v4l2_requestbuffers rb; |
| 1282 | |
| 1283 | memset(&rb, 0, sizeof rb); |
| 1284 | rb.count = 4; |
| 1285 | rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 1286 | rb.memory = V4L2_MEMORY_MMAP; |
| 1287 | ret = vidioc_reqbufs(file, priv, &rb); |
| 1288 | if (ret != 0) |
| 1289 | return ret; |
| 1290 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1291 | } |
| 1292 | mbuf->frames = gspca_dev->nframes; |
| 1293 | mbuf->size = gspca_dev->frsz * gspca_dev->nframes; |
| 1294 | for (i = 0; i < mbuf->frames; i++) |
| 1295 | mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset; |
| 1296 | return 0; |
| 1297 | } |
| 1298 | #endif |
| 1299 | |
| 1300 | static int dev_mmap(struct file *file, struct vm_area_struct *vma) |
| 1301 | { |
| 1302 | struct gspca_dev *gspca_dev = file->private_data; |
Harvey Harrison | efab821 | 2008-07-05 06:12:47 -0300 | [diff] [blame] | 1303 | struct gspca_frame *frame; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1304 | struct page *page; |
| 1305 | unsigned long addr, start, size; |
| 1306 | int i, ret; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1307 | |
| 1308 | start = vma->vm_start; |
| 1309 | size = vma->vm_end - vma->vm_start; |
| 1310 | PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size); |
| 1311 | |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1312 | if (mutex_lock_interruptible(&gspca_dev->queue_lock)) |
| 1313 | return -ERESTARTSYS; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1314 | if (!gspca_dev->present) { |
| 1315 | ret = -ENODEV; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1316 | goto out; |
| 1317 | } |
| 1318 | if (gspca_dev->capt_file != file) { |
| 1319 | ret = -EINVAL; |
| 1320 | goto out; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1321 | } |
| 1322 | |
Harvey Harrison | efab821 | 2008-07-05 06:12:47 -0300 | [diff] [blame] | 1323 | frame = NULL; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1324 | for (i = 0; i < gspca_dev->nframes; ++i) { |
| 1325 | if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) { |
| 1326 | PDEBUG(D_STREAM, "mmap bad memory type"); |
| 1327 | break; |
| 1328 | } |
| 1329 | if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT) |
| 1330 | == vma->vm_pgoff) { |
| 1331 | frame = &gspca_dev->frame[i]; |
| 1332 | break; |
| 1333 | } |
| 1334 | } |
Harvey Harrison | efab821 | 2008-07-05 06:12:47 -0300 | [diff] [blame] | 1335 | if (frame == NULL) { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1336 | PDEBUG(D_STREAM, "mmap no frame buffer found"); |
| 1337 | ret = -EINVAL; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1338 | goto out; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1339 | } |
| 1340 | #ifdef CONFIG_VIDEO_V4L1_COMPAT |
Jean-Francois Moine | ac0d6df | 2008-07-08 06:58:15 -0300 | [diff] [blame] | 1341 | /* v4l1 maps all the buffers */ |
| 1342 | if (i != 0 |
| 1343 | || size != frame->v4l2_buf.length * gspca_dev->nframes) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1344 | #endif |
Jean-Francois Moine | ac0d6df | 2008-07-08 06:58:15 -0300 | [diff] [blame] | 1345 | if (size != frame->v4l2_buf.length) { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1346 | PDEBUG(D_STREAM, "mmap bad size"); |
| 1347 | ret = -EINVAL; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1348 | goto out; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | /* |
| 1352 | * - VM_IO marks the area as being a mmaped region for I/O to a |
| 1353 | * device. It also prevents the region from being core dumped. |
| 1354 | */ |
| 1355 | vma->vm_flags |= VM_IO; |
| 1356 | |
| 1357 | addr = (unsigned long) frame->data; |
| 1358 | while (size > 0) { |
| 1359 | page = vmalloc_to_page((void *) addr); |
| 1360 | ret = vm_insert_page(vma, start, page); |
| 1361 | if (ret < 0) |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1362 | goto out; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1363 | start += PAGE_SIZE; |
| 1364 | addr += PAGE_SIZE; |
| 1365 | size -= PAGE_SIZE; |
| 1366 | } |
| 1367 | |
| 1368 | vma->vm_ops = &gspca_vm_ops; |
| 1369 | vma->vm_private_data = frame; |
| 1370 | gspca_vm_open(vma); |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1371 | ret = 0; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1372 | out: |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1373 | mutex_unlock(&gspca_dev->queue_lock); |
| 1374 | return ret; |
| 1375 | } |
| 1376 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1377 | /* |
| 1378 | * wait for a video frame |
| 1379 | * |
| 1380 | * If a frame is ready, its index is returned. |
| 1381 | */ |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1382 | static int frame_wait(struct gspca_dev *gspca_dev, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1383 | int nonblock_ing) |
| 1384 | { |
| 1385 | struct gspca_frame *frame; |
| 1386 | int i, j, ret; |
| 1387 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1388 | /* check if a frame is ready */ |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1389 | i = gspca_dev->fr_o; |
| 1390 | j = gspca_dev->fr_queue[i]; |
| 1391 | frame = &gspca_dev->frame[j]; |
Jean-Francois Moine | 82643d0 | 2008-07-02 09:14:56 -0300 | [diff] [blame] | 1392 | if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) { |
| 1393 | atomic_dec(&gspca_dev->nevent); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1394 | goto ok; |
Jean-Francois Moine | 82643d0 | 2008-07-02 09:14:56 -0300 | [diff] [blame] | 1395 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1396 | if (nonblock_ing) /* no frame yet */ |
| 1397 | return -EAGAIN; |
| 1398 | |
| 1399 | /* wait till a frame is ready */ |
| 1400 | for (;;) { |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1401 | ret = wait_event_interruptible_timeout(gspca_dev->wq, |
| 1402 | atomic_read(&gspca_dev->nevent) > 0, |
| 1403 | msecs_to_jiffies(3000)); |
| 1404 | if (ret <= 0) { |
| 1405 | if (ret < 0) |
Jean-Francois Moine | 82643d0 | 2008-07-02 09:14:56 -0300 | [diff] [blame] | 1406 | return ret; /* interrupt */ |
| 1407 | return -EIO; /* timeout */ |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1408 | } |
Jean-Francois Moine | 82643d0 | 2008-07-02 09:14:56 -0300 | [diff] [blame] | 1409 | atomic_dec(&gspca_dev->nevent); |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1410 | if (!gspca_dev->streaming || !gspca_dev->present) |
| 1411 | return -EIO; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1412 | i = gspca_dev->fr_o; |
| 1413 | j = gspca_dev->fr_queue[i]; |
| 1414 | frame = &gspca_dev->frame[j]; |
| 1415 | if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) |
| 1416 | break; |
| 1417 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1418 | ok: |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1419 | gspca_dev->fr_o = (i + 1) % gspca_dev->nframes; |
| 1420 | PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d", |
| 1421 | gspca_dev->fr_q, |
| 1422 | gspca_dev->fr_i, |
| 1423 | gspca_dev->fr_o); |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1424 | |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 1425 | if (gspca_dev->sd_desc->dq_callback) { |
| 1426 | mutex_lock(&gspca_dev->usb_lock); |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1427 | gspca_dev->sd_desc->dq_callback(gspca_dev); |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 1428 | mutex_unlock(&gspca_dev->usb_lock); |
| 1429 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1430 | return j; |
| 1431 | } |
| 1432 | |
| 1433 | /* |
| 1434 | * dequeue a video buffer |
| 1435 | * |
| 1436 | * If nonblock_ing is false, block until a buffer is available. |
| 1437 | */ |
| 1438 | static int vidioc_dqbuf(struct file *file, void *priv, |
| 1439 | struct v4l2_buffer *v4l2_buf) |
| 1440 | { |
| 1441 | struct gspca_dev *gspca_dev = priv; |
| 1442 | struct gspca_frame *frame; |
| 1443 | int i, ret; |
| 1444 | |
| 1445 | PDEBUG(D_FRAM, "dqbuf"); |
Jean-Francois Moine | ac0d6df | 2008-07-08 06:58:15 -0300 | [diff] [blame] | 1446 | if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1447 | return -EINVAL; |
| 1448 | if (v4l2_buf->memory != gspca_dev->memory) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1449 | return -EINVAL; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1450 | |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1451 | /* if not streaming, be sure the application will not loop forever */ |
| 1452 | if (!(file->f_flags & O_NONBLOCK) |
| 1453 | && !gspca_dev->streaming && gspca_dev->users == 1) |
| 1454 | return -EINVAL; |
| 1455 | |
| 1456 | /* only the capturing file may dequeue */ |
| 1457 | if (gspca_dev->capt_file != file) |
| 1458 | return -EINVAL; |
| 1459 | |
| 1460 | /* only one dequeue / read at a time */ |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1461 | if (mutex_lock_interruptible(&gspca_dev->read_lock)) |
| 1462 | return -ERESTARTSYS; |
| 1463 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1464 | ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1465 | if (ret < 0) |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1466 | goto out; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1467 | i = ret; /* frame index */ |
| 1468 | frame = &gspca_dev->frame[i]; |
Jean-Francois Moine | ac0d6df | 2008-07-08 06:58:15 -0300 | [diff] [blame] | 1469 | if (gspca_dev->memory == V4L2_MEMORY_USERPTR) { |
Hans Verkuil | c6eb8ea | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 1470 | if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr, |
Jean-Francois Moine | ac0d6df | 2008-07-08 06:58:15 -0300 | [diff] [blame] | 1471 | frame->data, |
| 1472 | frame->v4l2_buf.bytesused)) { |
| 1473 | PDEBUG(D_ERR|D_STREAM, |
| 1474 | "dqbuf cp to user failed"); |
| 1475 | ret = -EFAULT; |
| 1476 | goto out; |
| 1477 | } |
| 1478 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1479 | frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE; |
| 1480 | memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf); |
| 1481 | PDEBUG(D_FRAM, "dqbuf %d", i); |
| 1482 | ret = 0; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1483 | out: |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1484 | mutex_unlock(&gspca_dev->read_lock); |
| 1485 | return ret; |
| 1486 | } |
| 1487 | |
| 1488 | /* |
| 1489 | * queue a video buffer |
| 1490 | * |
| 1491 | * Attempting to queue a buffer that has already been |
| 1492 | * queued will return -EINVAL. |
| 1493 | */ |
| 1494 | static int vidioc_qbuf(struct file *file, void *priv, |
| 1495 | struct v4l2_buffer *v4l2_buf) |
| 1496 | { |
| 1497 | struct gspca_dev *gspca_dev = priv; |
| 1498 | struct gspca_frame *frame; |
| 1499 | int i, index, ret; |
| 1500 | |
| 1501 | PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index); |
| 1502 | if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1503 | return -EINVAL; |
| 1504 | |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1505 | if (mutex_lock_interruptible(&gspca_dev->queue_lock)) |
| 1506 | return -ERESTARTSYS; |
| 1507 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1508 | index = v4l2_buf->index; |
| 1509 | if ((unsigned) index >= gspca_dev->nframes) { |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1510 | PDEBUG(D_FRAM, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1511 | "qbuf idx %d >= %d", index, gspca_dev->nframes); |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1512 | ret = -EINVAL; |
| 1513 | goto out; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1514 | } |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1515 | if (v4l2_buf->memory != gspca_dev->memory) { |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1516 | PDEBUG(D_FRAM, "qbuf bad memory type"); |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1517 | ret = -EINVAL; |
| 1518 | goto out; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1519 | } |
| 1520 | |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1521 | frame = &gspca_dev->frame[index]; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1522 | if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) { |
| 1523 | PDEBUG(D_FRAM, "qbuf bad state"); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1524 | ret = -EINVAL; |
| 1525 | goto out; |
| 1526 | } |
| 1527 | |
| 1528 | frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1529 | /* frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE; */ |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1530 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1531 | if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1532 | frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr; |
| 1533 | frame->v4l2_buf.length = v4l2_buf->length; |
| 1534 | } |
| 1535 | |
| 1536 | /* put the buffer in the 'queued' queue */ |
| 1537 | i = gspca_dev->fr_q; |
| 1538 | gspca_dev->fr_queue[i] = index; |
| 1539 | gspca_dev->fr_q = (i + 1) % gspca_dev->nframes; |
| 1540 | PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d", |
| 1541 | gspca_dev->fr_q, |
| 1542 | gspca_dev->fr_i, |
| 1543 | gspca_dev->fr_o); |
| 1544 | |
| 1545 | v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED; |
| 1546 | v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE; |
| 1547 | ret = 0; |
| 1548 | out: |
| 1549 | mutex_unlock(&gspca_dev->queue_lock); |
| 1550 | return ret; |
| 1551 | } |
| 1552 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1553 | /* |
| 1554 | * allocate the resources for read() |
| 1555 | */ |
| 1556 | static int read_alloc(struct gspca_dev *gspca_dev, |
| 1557 | struct file *file) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1558 | { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1559 | struct v4l2_buffer v4l2_buf; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1560 | int i, ret; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1561 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1562 | PDEBUG(D_STREAM, "read alloc"); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1563 | if (gspca_dev->nframes == 0) { |
| 1564 | struct v4l2_requestbuffers rb; |
| 1565 | |
| 1566 | memset(&rb, 0, sizeof rb); |
| 1567 | rb.count = gspca_dev->nbufread; |
| 1568 | rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
Jean-Francois Moine | ac0d6df | 2008-07-08 06:58:15 -0300 | [diff] [blame] | 1569 | rb.memory = GSPCA_MEMORY_READ; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1570 | ret = vidioc_reqbufs(file, gspca_dev, &rb); |
| 1571 | if (ret != 0) { |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1572 | PDEBUG(D_STREAM, "read reqbuf err %d", ret); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1573 | return ret; |
| 1574 | } |
| 1575 | memset(&v4l2_buf, 0, sizeof v4l2_buf); |
| 1576 | v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
Jean-Francois Moine | ac0d6df | 2008-07-08 06:58:15 -0300 | [diff] [blame] | 1577 | v4l2_buf.memory = GSPCA_MEMORY_READ; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1578 | for (i = 0; i < gspca_dev->nbufread; i++) { |
| 1579 | v4l2_buf.index = i; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1580 | ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf); |
| 1581 | if (ret != 0) { |
| 1582 | PDEBUG(D_STREAM, "read qbuf err: %d", ret); |
| 1583 | return ret; |
| 1584 | } |
| 1585 | } |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1586 | gspca_dev->memory = GSPCA_MEMORY_READ; |
| 1587 | } |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1588 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1589 | /* start streaming */ |
| 1590 | ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE); |
| 1591 | if (ret != 0) |
| 1592 | PDEBUG(D_STREAM, "read streamon err %d", ret); |
| 1593 | return ret; |
| 1594 | } |
| 1595 | |
| 1596 | static unsigned int dev_poll(struct file *file, poll_table *wait) |
| 1597 | { |
| 1598 | struct gspca_dev *gspca_dev = file->private_data; |
| 1599 | int i, ret; |
| 1600 | |
| 1601 | PDEBUG(D_FRAM, "poll"); |
| 1602 | |
| 1603 | poll_wait(file, &gspca_dev->wq, wait); |
| 1604 | if (!gspca_dev->present) |
| 1605 | return POLLERR; |
| 1606 | |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1607 | /* if reqbufs is not done, the user would use read() */ |
| 1608 | if (gspca_dev->nframes == 0) { |
| 1609 | if (gspca_dev->memory != GSPCA_MEMORY_NO) |
| 1610 | return POLLERR; /* not the 1st time */ |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1611 | ret = read_alloc(gspca_dev, file); |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1612 | if (ret != 0) |
| 1613 | return POLLERR; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1614 | } |
| 1615 | |
| 1616 | if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0) |
| 1617 | return POLLERR; |
| 1618 | if (!gspca_dev->present) { |
| 1619 | ret = POLLERR; |
| 1620 | goto out; |
| 1621 | } |
| 1622 | |
Jean-Francois Moine | 5b77ae7 | 2008-07-12 09:28:04 -0300 | [diff] [blame] | 1623 | /* check the next incoming buffer */ |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1624 | i = gspca_dev->fr_o; |
| 1625 | i = gspca_dev->fr_queue[i]; |
| 1626 | if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE) |
| 1627 | ret = POLLIN | POLLRDNORM; /* something to read */ |
| 1628 | else |
| 1629 | ret = 0; |
| 1630 | out: |
| 1631 | mutex_unlock(&gspca_dev->queue_lock); |
| 1632 | return ret; |
| 1633 | } |
| 1634 | |
| 1635 | static ssize_t dev_read(struct file *file, char __user *data, |
| 1636 | size_t count, loff_t *ppos) |
| 1637 | { |
| 1638 | struct gspca_dev *gspca_dev = file->private_data; |
| 1639 | struct gspca_frame *frame; |
| 1640 | struct v4l2_buffer v4l2_buf; |
| 1641 | struct timeval timestamp; |
Jean-Francois Moine | 82643d0 | 2008-07-02 09:14:56 -0300 | [diff] [blame] | 1642 | int n, ret, ret2; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1643 | |
Mauro Carvalho Chehab | a9e1113 | 2008-07-09 05:26:26 -0300 | [diff] [blame] | 1644 | PDEBUG(D_FRAM, "read (%zd)", count); |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1645 | if (!gspca_dev->present) |
| 1646 | return -ENODEV; |
| 1647 | switch (gspca_dev->memory) { |
| 1648 | case GSPCA_MEMORY_NO: /* first time */ |
| 1649 | ret = read_alloc(gspca_dev, file); |
| 1650 | if (ret != 0) |
| 1651 | return ret; |
| 1652 | break; |
| 1653 | case GSPCA_MEMORY_READ: |
Jean-Francois Moine | 82643d0 | 2008-07-02 09:14:56 -0300 | [diff] [blame] | 1654 | if (gspca_dev->capt_file == file) |
| 1655 | break; |
| 1656 | /* fall thru */ |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1657 | default: |
| 1658 | return -EINVAL; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | /* get a frame */ |
| 1662 | jiffies_to_timeval(get_jiffies_64(), ×tamp); |
| 1663 | timestamp.tv_sec--; |
Jean-Francois Moine | 82643d0 | 2008-07-02 09:14:56 -0300 | [diff] [blame] | 1664 | n = 2; |
| 1665 | for (;;) { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1666 | memset(&v4l2_buf, 0, sizeof v4l2_buf); |
| 1667 | v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
Jean-Francois Moine | ac0d6df | 2008-07-08 06:58:15 -0300 | [diff] [blame] | 1668 | v4l2_buf.memory = GSPCA_MEMORY_READ; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1669 | ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf); |
| 1670 | if (ret != 0) { |
| 1671 | PDEBUG(D_STREAM, "read dqbuf err %d", ret); |
| 1672 | return ret; |
| 1673 | } |
| 1674 | |
| 1675 | /* if the process slept for more than 1 second, |
Jean-Francois Moine | 82643d0 | 2008-07-02 09:14:56 -0300 | [diff] [blame] | 1676 | * get anewer frame */ |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1677 | frame = &gspca_dev->frame[v4l2_buf.index]; |
Jean-Francois Moine | 82643d0 | 2008-07-02 09:14:56 -0300 | [diff] [blame] | 1678 | if (--n < 0) |
| 1679 | break; /* avoid infinite loop */ |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1680 | if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec) |
| 1681 | break; |
| 1682 | ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf); |
| 1683 | if (ret != 0) { |
| 1684 | PDEBUG(D_STREAM, "read qbuf err %d", ret); |
| 1685 | return ret; |
| 1686 | } |
| 1687 | } |
| 1688 | |
| 1689 | /* copy the frame */ |
Jean-Francois Moine | ac0d6df | 2008-07-08 06:58:15 -0300 | [diff] [blame] | 1690 | if (count > frame->v4l2_buf.bytesused) |
| 1691 | count = frame->v4l2_buf.bytesused; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1692 | ret = copy_to_user(data, frame->data, count); |
| 1693 | if (ret != 0) { |
| 1694 | PDEBUG(D_ERR|D_STREAM, |
Mauro Carvalho Chehab | a9e1113 | 2008-07-09 05:26:26 -0300 | [diff] [blame] | 1695 | "read cp to user lack %d / %zd", ret, count); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1696 | ret = -EFAULT; |
| 1697 | goto out; |
| 1698 | } |
| 1699 | ret = count; |
| 1700 | out: |
| 1701 | /* in each case, requeue the buffer */ |
| 1702 | ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf); |
| 1703 | if (ret2 != 0) |
| 1704 | return ret2; |
| 1705 | return ret; |
| 1706 | } |
| 1707 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1708 | static void dev_release(struct video_device *vfd) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1709 | { |
| 1710 | /* nothing */ |
| 1711 | } |
| 1712 | |
| 1713 | static struct file_operations dev_fops = { |
| 1714 | .owner = THIS_MODULE, |
| 1715 | .open = dev_open, |
| 1716 | .release = dev_close, |
| 1717 | .read = dev_read, |
| 1718 | .mmap = dev_mmap, |
| 1719 | .ioctl = video_ioctl2, |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 1720 | #ifdef CONFIG_COMPAT |
| 1721 | .compat_ioctl = v4l_compat_ioctl32, |
| 1722 | #endif |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1723 | .llseek = no_llseek, |
| 1724 | .poll = dev_poll, |
| 1725 | }; |
| 1726 | |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1727 | static const struct v4l2_ioctl_ops dev_ioctl_ops = { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1728 | .vidioc_querycap = vidioc_querycap, |
| 1729 | .vidioc_dqbuf = vidioc_dqbuf, |
| 1730 | .vidioc_qbuf = vidioc_qbuf, |
Mauro Carvalho Chehab | e078770 | 2008-07-01 04:06:22 -0300 | [diff] [blame] | 1731 | .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, |
| 1732 | .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, |
| 1733 | .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, |
| 1734 | .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1735 | .vidioc_streamon = vidioc_streamon, |
| 1736 | .vidioc_queryctrl = vidioc_queryctrl, |
| 1737 | .vidioc_g_ctrl = vidioc_g_ctrl, |
| 1738 | .vidioc_s_ctrl = vidioc_s_ctrl, |
| 1739 | .vidioc_querymenu = vidioc_querymenu, |
| 1740 | .vidioc_enum_input = vidioc_enum_input, |
| 1741 | .vidioc_g_input = vidioc_g_input, |
| 1742 | .vidioc_s_input = vidioc_s_input, |
| 1743 | .vidioc_reqbufs = vidioc_reqbufs, |
| 1744 | .vidioc_querybuf = vidioc_querybuf, |
| 1745 | .vidioc_streamoff = vidioc_streamoff, |
| 1746 | .vidioc_g_jpegcomp = vidioc_g_jpegcomp, |
| 1747 | .vidioc_s_jpegcomp = vidioc_s_jpegcomp, |
| 1748 | .vidioc_g_parm = vidioc_g_parm, |
| 1749 | .vidioc_s_parm = vidioc_s_parm, |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 1750 | .vidioc_s_std = vidioc_s_std, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1751 | #ifdef CONFIG_VIDEO_V4L1_COMPAT |
| 1752 | .vidiocgmbuf = vidiocgmbuf, |
| 1753 | #endif |
| 1754 | }; |
| 1755 | |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1756 | static struct video_device gspca_template = { |
| 1757 | .name = "gspca main driver", |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1758 | .fops = &dev_fops, |
| 1759 | .ioctl_ops = &dev_ioctl_ops, |
| 1760 | .release = dev_release, /* mandatory */ |
| 1761 | .minor = -1, |
| 1762 | }; |
| 1763 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1764 | /* |
| 1765 | * probe and create a new gspca device |
| 1766 | * |
| 1767 | * This function must be called by the sub-driver when it is |
| 1768 | * called for probing a new device. |
| 1769 | */ |
| 1770 | int gspca_dev_probe(struct usb_interface *intf, |
| 1771 | const struct usb_device_id *id, |
| 1772 | const struct sd_desc *sd_desc, |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1773 | int dev_size, |
| 1774 | struct module *module) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1775 | { |
| 1776 | struct usb_interface_descriptor *interface; |
| 1777 | struct gspca_dev *gspca_dev; |
| 1778 | struct usb_device *dev = interface_to_usbdev(intf); |
| 1779 | int ret; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1780 | |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 1781 | PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1782 | |
| 1783 | /* we don't handle multi-config cameras */ |
| 1784 | if (dev->descriptor.bNumConfigurations != 1) |
| 1785 | return -ENODEV; |
| 1786 | interface = &intf->cur_altsetting->desc; |
| 1787 | if (interface->bInterfaceNumber > 0) |
| 1788 | return -ENODEV; |
| 1789 | |
| 1790 | /* create the device */ |
| 1791 | if (dev_size < sizeof *gspca_dev) |
| 1792 | dev_size = sizeof *gspca_dev; |
| 1793 | gspca_dev = kzalloc(dev_size, GFP_KERNEL); |
| 1794 | if (gspca_dev == NULL) { |
| 1795 | err("couldn't kzalloc gspca struct"); |
| 1796 | return -EIO; |
| 1797 | } |
Jean-Francois Moine | 8295d99 | 2008-09-03 17:12:19 -0300 | [diff] [blame] | 1798 | gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL); |
| 1799 | if (!gspca_dev->usb_buf) { |
| 1800 | err("out of memory"); |
| 1801 | ret = -EIO; |
| 1802 | goto out; |
| 1803 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1804 | gspca_dev->dev = dev; |
| 1805 | gspca_dev->iface = interface->bInterfaceNumber; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1806 | gspca_dev->nbalt = intf->num_altsetting; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1807 | gspca_dev->sd_desc = sd_desc; |
| 1808 | /* gspca_dev->users = 0; (done by kzalloc) */ |
| 1809 | gspca_dev->nbufread = 2; |
| 1810 | |
Jean-Francois Moine | 012d6b0 | 2008-09-03 17:12:16 -0300 | [diff] [blame] | 1811 | /* configure the subdriver and initialize the USB device */ |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1812 | ret = gspca_dev->sd_desc->config(gspca_dev, id); |
| 1813 | if (ret < 0) |
| 1814 | goto out; |
Jean-Francois Moine | 012d6b0 | 2008-09-03 17:12:16 -0300 | [diff] [blame] | 1815 | ret = gspca_dev->sd_desc->init(gspca_dev); |
| 1816 | if (ret < 0) |
| 1817 | goto out; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1818 | ret = gspca_set_alt0(gspca_dev); |
| 1819 | if (ret < 0) |
| 1820 | goto out; |
| 1821 | gspca_set_default_mode(gspca_dev); |
| 1822 | |
| 1823 | mutex_init(&gspca_dev->usb_lock); |
| 1824 | mutex_init(&gspca_dev->read_lock); |
| 1825 | mutex_init(&gspca_dev->queue_lock); |
| 1826 | init_waitqueue_head(&gspca_dev->wq); |
| 1827 | |
| 1828 | /* init video stuff */ |
| 1829 | memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template); |
Hans Verkuil | 5e85e73 | 2008-07-20 06:31:39 -0300 | [diff] [blame] | 1830 | gspca_dev->vdev.parent = &dev->dev; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1831 | memcpy(&gspca_dev->fops, &dev_fops, sizeof gspca_dev->fops); |
| 1832 | gspca_dev->vdev.fops = &gspca_dev->fops; |
| 1833 | gspca_dev->fops.owner = module; /* module protection */ |
Oliver Neukum | ee56a4d | 2008-07-27 14:01:59 -0300 | [diff] [blame] | 1834 | gspca_dev->present = 1; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1835 | ret = video_register_device(&gspca_dev->vdev, |
| 1836 | VFL_TYPE_GRABBER, |
| 1837 | video_nr); |
| 1838 | if (ret < 0) { |
| 1839 | err("video_register_device err %d", ret); |
| 1840 | goto out; |
| 1841 | } |
| 1842 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1843 | usb_set_intfdata(intf, gspca_dev); |
| 1844 | PDEBUG(D_PROBE, "probe ok"); |
| 1845 | return 0; |
| 1846 | out: |
Jean-Francois Moine | 8295d99 | 2008-09-03 17:12:19 -0300 | [diff] [blame] | 1847 | kfree(gspca_dev->usb_buf); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1848 | kfree(gspca_dev); |
| 1849 | return ret; |
| 1850 | } |
| 1851 | EXPORT_SYMBOL(gspca_dev_probe); |
| 1852 | |
| 1853 | /* |
| 1854 | * USB disconnection |
| 1855 | * |
| 1856 | * This function must be called by the sub-driver |
| 1857 | * when the device disconnects, after the specific resources are freed. |
| 1858 | */ |
| 1859 | void gspca_disconnect(struct usb_interface *intf) |
| 1860 | { |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 1861 | struct gspca_dev *gspca_dev = usb_get_intfdata(intf); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1862 | |
| 1863 | if (!gspca_dev) |
| 1864 | return; |
| 1865 | gspca_dev->present = 0; |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1866 | mutex_lock(&gspca_dev->queue_lock); |
| 1867 | mutex_lock(&gspca_dev->usb_lock); |
| 1868 | gspca_dev->streaming = 0; |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 1869 | destroy_urbs(gspca_dev); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1870 | mutex_unlock(&gspca_dev->usb_lock); |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1871 | mutex_unlock(&gspca_dev->queue_lock); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1872 | while (gspca_dev->users != 0) { /* wait until fully closed */ |
| 1873 | atomic_inc(&gspca_dev->nevent); |
| 1874 | wake_up_interruptible(&gspca_dev->wq); /* wake processes */ |
| 1875 | schedule(); |
| 1876 | } |
| 1877 | /* We don't want people trying to open up the device */ |
| 1878 | video_unregister_device(&gspca_dev->vdev); |
| 1879 | /* Free the memory */ |
Jean-Francois Moine | 8295d99 | 2008-09-03 17:12:19 -0300 | [diff] [blame] | 1880 | kfree(gspca_dev->usb_buf); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1881 | kfree(gspca_dev); |
| 1882 | PDEBUG(D_PROBE, "disconnect complete"); |
| 1883 | } |
| 1884 | EXPORT_SYMBOL(gspca_disconnect); |
| 1885 | |
Jean-Francois Moine | 6a70974 | 2008-09-03 16:48:10 -0300 | [diff] [blame] | 1886 | #ifdef CONFIG_PM |
| 1887 | int gspca_suspend(struct usb_interface *intf, pm_message_t message) |
| 1888 | { |
| 1889 | struct gspca_dev *gspca_dev = usb_get_intfdata(intf); |
| 1890 | |
| 1891 | if (!gspca_dev->streaming) |
| 1892 | return 0; |
| 1893 | gspca_dev->frozen = 1; /* avoid urb error messages */ |
Jean-Francois Moine | 012d6b0 | 2008-09-03 17:12:16 -0300 | [diff] [blame] | 1894 | if (gspca_dev->sd_desc->stopN) |
| 1895 | gspca_dev->sd_desc->stopN(gspca_dev); |
Jean-Francois Moine | 6a70974 | 2008-09-03 16:48:10 -0300 | [diff] [blame] | 1896 | destroy_urbs(gspca_dev); |
| 1897 | gspca_set_alt0(gspca_dev); |
Jean-Francois Moine | 012d6b0 | 2008-09-03 17:12:16 -0300 | [diff] [blame] | 1898 | if (gspca_dev->sd_desc->stop0) |
| 1899 | gspca_dev->sd_desc->stop0(gspca_dev); |
Jean-Francois Moine | 6a70974 | 2008-09-03 16:48:10 -0300 | [diff] [blame] | 1900 | return 0; |
| 1901 | } |
| 1902 | EXPORT_SYMBOL(gspca_suspend); |
| 1903 | |
| 1904 | int gspca_resume(struct usb_interface *intf) |
| 1905 | { |
| 1906 | struct gspca_dev *gspca_dev = usb_get_intfdata(intf); |
| 1907 | |
| 1908 | gspca_dev->frozen = 0; |
Jean-Francois Moine | 012d6b0 | 2008-09-03 17:12:16 -0300 | [diff] [blame] | 1909 | gspca_dev->sd_desc->init(gspca_dev); |
| 1910 | if (gspca_dev->streaming) |
| 1911 | return gspca_init_transfer(gspca_dev); |
Jean-Francois Moine | 1e89e2d | 2008-09-03 17:12:15 -0300 | [diff] [blame] | 1912 | return 0; |
Jean-Francois Moine | 6a70974 | 2008-09-03 16:48:10 -0300 | [diff] [blame] | 1913 | } |
| 1914 | EXPORT_SYMBOL(gspca_resume); |
| 1915 | #endif |
Hans de Goede | dcef323 | 2008-07-10 10:40:53 -0300 | [diff] [blame] | 1916 | /* -- cam driver utility functions -- */ |
| 1917 | |
| 1918 | /* auto gain and exposure algorithm based on the knee algorithm described here: |
| 1919 | http://ytse.tricolour.net/docs/LowLightOptimization.html |
| 1920 | |
| 1921 | Returns 0 if no changes were made, 1 if the gain and or exposure settings |
| 1922 | where changed. */ |
| 1923 | int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum, |
| 1924 | int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee) |
| 1925 | { |
| 1926 | int i, steps, gain, orig_gain, exposure, orig_exposure, autogain; |
| 1927 | const struct ctrl *gain_ctrl = NULL; |
| 1928 | const struct ctrl *exposure_ctrl = NULL; |
| 1929 | const struct ctrl *autogain_ctrl = NULL; |
| 1930 | int retval = 0; |
| 1931 | |
| 1932 | for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) { |
| 1933 | if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN) |
| 1934 | gain_ctrl = &gspca_dev->sd_desc->ctrls[i]; |
| 1935 | if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE) |
| 1936 | exposure_ctrl = &gspca_dev->sd_desc->ctrls[i]; |
| 1937 | if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN) |
| 1938 | autogain_ctrl = &gspca_dev->sd_desc->ctrls[i]; |
| 1939 | } |
| 1940 | if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) { |
| 1941 | PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called " |
| 1942 | "on cam without (auto)gain/exposure"); |
| 1943 | return 0; |
| 1944 | } |
| 1945 | |
| 1946 | if (gain_ctrl->get(gspca_dev, &gain) || |
| 1947 | exposure_ctrl->get(gspca_dev, &exposure) || |
| 1948 | autogain_ctrl->get(gspca_dev, &autogain) || !autogain) |
| 1949 | return 0; |
| 1950 | |
| 1951 | orig_gain = gain; |
| 1952 | orig_exposure = exposure; |
| 1953 | |
| 1954 | /* If we are of a multiple of deadzone, do multiple steps to reach the |
| 1955 | desired lumination fast (with the risc of a slight overshoot) */ |
| 1956 | steps = abs(desired_avg_lum - avg_lum) / deadzone; |
| 1957 | |
| 1958 | PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d\n", |
| 1959 | avg_lum, desired_avg_lum, steps); |
| 1960 | |
| 1961 | for (i = 0; i < steps; i++) { |
| 1962 | if (avg_lum > desired_avg_lum) { |
| 1963 | if (gain > gain_knee) |
| 1964 | gain--; |
| 1965 | else if (exposure > exposure_knee) |
| 1966 | exposure--; |
| 1967 | else if (gain > gain_ctrl->qctrl.default_value) |
| 1968 | gain--; |
| 1969 | else if (exposure > exposure_ctrl->qctrl.minimum) |
| 1970 | exposure--; |
| 1971 | else if (gain > gain_ctrl->qctrl.minimum) |
| 1972 | gain--; |
| 1973 | else |
| 1974 | break; |
| 1975 | } else { |
| 1976 | if (gain < gain_ctrl->qctrl.default_value) |
| 1977 | gain++; |
| 1978 | else if (exposure < exposure_knee) |
| 1979 | exposure++; |
| 1980 | else if (gain < gain_knee) |
| 1981 | gain++; |
| 1982 | else if (exposure < exposure_ctrl->qctrl.maximum) |
| 1983 | exposure++; |
| 1984 | else if (gain < gain_ctrl->qctrl.maximum) |
| 1985 | gain++; |
| 1986 | else |
| 1987 | break; |
| 1988 | } |
| 1989 | } |
| 1990 | |
| 1991 | if (gain != orig_gain) { |
| 1992 | gain_ctrl->set(gspca_dev, gain); |
| 1993 | retval = 1; |
| 1994 | } |
| 1995 | if (exposure != orig_exposure) { |
| 1996 | exposure_ctrl->set(gspca_dev, exposure); |
| 1997 | retval = 1; |
| 1998 | } |
| 1999 | |
| 2000 | return retval; |
| 2001 | } |
| 2002 | EXPORT_SYMBOL(gspca_auto_gain_n_exposure); |
| 2003 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2004 | /* -- module insert / remove -- */ |
| 2005 | static int __init gspca_init(void) |
| 2006 | { |
Jean-Francois Moine | bb64e86 | 2008-07-19 06:49:28 -0300 | [diff] [blame] | 2007 | info("main v%d.%d.%d registered", |
| 2008 | (DRIVER_VERSION_NUMBER >> 16) & 0xff, |
| 2009 | (DRIVER_VERSION_NUMBER >> 8) & 0xff, |
| 2010 | DRIVER_VERSION_NUMBER & 0xff); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2011 | return 0; |
| 2012 | } |
| 2013 | static void __exit gspca_exit(void) |
| 2014 | { |
| 2015 | info("main deregistered"); |
| 2016 | } |
| 2017 | |
| 2018 | module_init(gspca_init); |
| 2019 | module_exit(gspca_exit); |
| 2020 | |
Jean-Francois Moine | 335b3f8 | 2008-07-30 04:53:02 -0300 | [diff] [blame] | 2021 | #ifdef GSPCA_DEBUG |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 2022 | module_param_named(debug, gspca_debug, int, 0644); |
| 2023 | MODULE_PARM_DESC(debug, |
| 2024 | "Debug (bit) 0x01:error 0x02:probe 0x04:config" |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 2025 | " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout" |
| 2026 | " 0x0100: v4l2"); |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 2027 | #endif |