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