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