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