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