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