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