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