1 /* 2 * Main USB camera driver 3 * 4 * Copyright (C) 2008-2011 Jean-François Moine <http://moinejf.free.fr> 5 * 6 * Camera button input handling by Márton Németh 7 * Copyright (C) 2009-2010 Márton Németh <nm127@freemail.hu> 8 * 9 * This program is free software; you can redistribute it and/or modify it 10 * under the terms of the GNU General Public License as published by the 11 * Free Software Foundation; either version 2 of the License, or (at your 12 * option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, but 15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 17 * for more details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program; if not, write to the Free Software Foundation, 21 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 22 */ 23 24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 25 26 #define GSPCA_VERSION "2.14.0" 27 28 #include <linux/init.h> 29 #include <linux/fs.h> 30 #include <linux/vmalloc.h> 31 #include <linux/sched.h> 32 #include <linux/slab.h> 33 #include <linux/mm.h> 34 #include <linux/string.h> 35 #include <linux/pagemap.h> 36 #include <linux/io.h> 37 #include <asm/page.h> 38 #include <linux/uaccess.h> 39 #include <linux/ktime.h> 40 #include <media/v4l2-ioctl.h> 41 #include <media/v4l2-ctrls.h> 42 #include <media/v4l2-fh.h> 43 #include <media/v4l2-event.h> 44 45 #include "gspca.h" 46 47 #if IS_ENABLED(CONFIG_INPUT) 48 #include <linux/input.h> 49 #include <linux/usb/input.h> 50 #endif 51 52 /* global values */ 53 #define DEF_NURBS 3 /* default number of URBs */ 54 #if DEF_NURBS > MAX_NURBS 55 #error "DEF_NURBS too big" 56 #endif 57 58 MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>"); 59 MODULE_DESCRIPTION("GSPCA USB Camera Driver"); 60 MODULE_LICENSE("GPL"); 61 MODULE_VERSION(GSPCA_VERSION); 62 63 int gspca_debug; 64 EXPORT_SYMBOL(gspca_debug); 65 66 static void PDEBUG_MODE(struct gspca_dev *gspca_dev, int debug, char *txt, 67 __u32 pixfmt, int w, int h) 68 { 69 if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') { 70 PDEBUG(debug, "%s %c%c%c%c %dx%d", 71 txt, 72 pixfmt & 0xff, 73 (pixfmt >> 8) & 0xff, 74 (pixfmt >> 16) & 0xff, 75 pixfmt >> 24, 76 w, h); 77 } else { 78 PDEBUG(debug, "%s 0x%08x %dx%d", 79 txt, 80 pixfmt, 81 w, h); 82 } 83 } 84 85 /* specific memory types - !! should be different from V4L2_MEMORY_xxx */ 86 #define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */ 87 #define GSPCA_MEMORY_READ 7 88 89 #define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE) 90 91 /* 92 * VMA operations. 93 */ 94 static void gspca_vm_open(struct vm_area_struct *vma) 95 { 96 struct gspca_frame *frame = vma->vm_private_data; 97 98 frame->vma_use_count++; 99 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED; 100 } 101 102 static void gspca_vm_close(struct vm_area_struct *vma) 103 { 104 struct gspca_frame *frame = vma->vm_private_data; 105 106 if (--frame->vma_use_count <= 0) 107 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED; 108 } 109 110 static const struct vm_operations_struct gspca_vm_ops = { 111 .open = gspca_vm_open, 112 .close = gspca_vm_close, 113 }; 114 115 /* 116 * Input and interrupt endpoint handling functions 117 */ 118 #if IS_ENABLED(CONFIG_INPUT) 119 static void int_irq(struct urb *urb) 120 { 121 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context; 122 int ret; 123 124 ret = urb->status; 125 switch (ret) { 126 case 0: 127 if (gspca_dev->sd_desc->int_pkt_scan(gspca_dev, 128 urb->transfer_buffer, urb->actual_length) < 0) { 129 PERR("Unknown packet received"); 130 } 131 break; 132 133 case -ENOENT: 134 case -ECONNRESET: 135 case -ENODEV: 136 case -ESHUTDOWN: 137 /* Stop is requested either by software or hardware is gone, 138 * keep the ret value non-zero and don't resubmit later. 139 */ 140 break; 141 142 default: 143 PERR("URB error %i, resubmitting", urb->status); 144 urb->status = 0; 145 ret = 0; 146 } 147 148 if (ret == 0) { 149 ret = usb_submit_urb(urb, GFP_ATOMIC); 150 if (ret < 0) 151 pr_err("Resubmit URB failed with error %i\n", ret); 152 } 153 } 154 155 static int gspca_input_connect(struct gspca_dev *dev) 156 { 157 struct input_dev *input_dev; 158 int err = 0; 159 160 dev->input_dev = NULL; 161 if (dev->sd_desc->int_pkt_scan || dev->sd_desc->other_input) { 162 input_dev = input_allocate_device(); 163 if (!input_dev) 164 return -ENOMEM; 165 166 usb_make_path(dev->dev, dev->phys, sizeof(dev->phys)); 167 strlcat(dev->phys, "/input0", sizeof(dev->phys)); 168 169 input_dev->name = dev->sd_desc->name; 170 input_dev->phys = dev->phys; 171 172 usb_to_input_id(dev->dev, &input_dev->id); 173 174 input_dev->evbit[0] = BIT_MASK(EV_KEY); 175 input_dev->keybit[BIT_WORD(KEY_CAMERA)] = BIT_MASK(KEY_CAMERA); 176 input_dev->dev.parent = &dev->dev->dev; 177 178 err = input_register_device(input_dev); 179 if (err) { 180 pr_err("Input device registration failed with error %i\n", 181 err); 182 input_dev->dev.parent = NULL; 183 input_free_device(input_dev); 184 } else { 185 dev->input_dev = input_dev; 186 } 187 } 188 189 return err; 190 } 191 192 static int alloc_and_submit_int_urb(struct gspca_dev *gspca_dev, 193 struct usb_endpoint_descriptor *ep) 194 { 195 unsigned int buffer_len; 196 int interval; 197 struct urb *urb; 198 struct usb_device *dev; 199 void *buffer = NULL; 200 int ret = -EINVAL; 201 202 buffer_len = le16_to_cpu(ep->wMaxPacketSize); 203 interval = ep->bInterval; 204 PDEBUG(D_CONF, "found int in endpoint: 0x%x, buffer_len=%u, interval=%u", 205 ep->bEndpointAddress, buffer_len, interval); 206 207 dev = gspca_dev->dev; 208 209 urb = usb_alloc_urb(0, GFP_KERNEL); 210 if (!urb) { 211 ret = -ENOMEM; 212 goto error; 213 } 214 215 buffer = usb_alloc_coherent(dev, buffer_len, 216 GFP_KERNEL, &urb->transfer_dma); 217 if (!buffer) { 218 ret = -ENOMEM; 219 goto error_buffer; 220 } 221 usb_fill_int_urb(urb, dev, 222 usb_rcvintpipe(dev, ep->bEndpointAddress), 223 buffer, buffer_len, 224 int_irq, (void *)gspca_dev, interval); 225 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 226 ret = usb_submit_urb(urb, GFP_KERNEL); 227 if (ret < 0) { 228 PERR("submit int URB failed with error %i", ret); 229 goto error_submit; 230 } 231 gspca_dev->int_urb = urb; 232 return ret; 233 234 error_submit: 235 usb_free_coherent(dev, 236 urb->transfer_buffer_length, 237 urb->transfer_buffer, 238 urb->transfer_dma); 239 error_buffer: 240 usb_free_urb(urb); 241 error: 242 return ret; 243 } 244 245 static void gspca_input_create_urb(struct gspca_dev *gspca_dev) 246 { 247 struct usb_interface *intf; 248 struct usb_host_interface *intf_desc; 249 struct usb_endpoint_descriptor *ep; 250 int i; 251 252 if (gspca_dev->sd_desc->int_pkt_scan) { 253 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface); 254 intf_desc = intf->cur_altsetting; 255 for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) { 256 ep = &intf_desc->endpoint[i].desc; 257 if (usb_endpoint_dir_in(ep) && 258 usb_endpoint_xfer_int(ep)) { 259 260 alloc_and_submit_int_urb(gspca_dev, ep); 261 break; 262 } 263 } 264 } 265 } 266 267 static void gspca_input_destroy_urb(struct gspca_dev *gspca_dev) 268 { 269 struct urb *urb; 270 271 urb = gspca_dev->int_urb; 272 if (urb) { 273 gspca_dev->int_urb = NULL; 274 usb_kill_urb(urb); 275 usb_free_coherent(gspca_dev->dev, 276 urb->transfer_buffer_length, 277 urb->transfer_buffer, 278 urb->transfer_dma); 279 usb_free_urb(urb); 280 } 281 } 282 #else 283 static inline void gspca_input_destroy_urb(struct gspca_dev *gspca_dev) 284 { 285 } 286 287 static inline void gspca_input_create_urb(struct gspca_dev *gspca_dev) 288 { 289 } 290 291 static inline int gspca_input_connect(struct gspca_dev *dev) 292 { 293 return 0; 294 } 295 #endif 296 297 /* 298 * fill a video frame from an URB and resubmit 299 */ 300 static void fill_frame(struct gspca_dev *gspca_dev, 301 struct urb *urb) 302 { 303 u8 *data; /* address of data in the iso message */ 304 int i, len, st; 305 cam_pkt_op pkt_scan; 306 307 if (urb->status != 0) { 308 if (urb->status == -ESHUTDOWN) 309 return; /* disconnection */ 310 #ifdef CONFIG_PM 311 if (gspca_dev->frozen) 312 return; 313 #endif 314 PERR("urb status: %d", urb->status); 315 urb->status = 0; 316 goto resubmit; 317 } 318 pkt_scan = gspca_dev->sd_desc->pkt_scan; 319 for (i = 0; i < urb->number_of_packets; i++) { 320 len = urb->iso_frame_desc[i].actual_length; 321 322 /* check the packet status and length */ 323 st = urb->iso_frame_desc[i].status; 324 if (st) { 325 pr_err("ISOC data error: [%d] len=%d, status=%d\n", 326 i, len, st); 327 gspca_dev->last_packet_type = DISCARD_PACKET; 328 continue; 329 } 330 if (len == 0) { 331 if (gspca_dev->empty_packet == 0) 332 gspca_dev->empty_packet = 1; 333 continue; 334 } 335 336 /* let the packet be analyzed by the subdriver */ 337 PDEBUG(D_PACK, "packet [%d] o:%d l:%d", 338 i, urb->iso_frame_desc[i].offset, len); 339 data = (u8 *) urb->transfer_buffer 340 + urb->iso_frame_desc[i].offset; 341 pkt_scan(gspca_dev, data, len); 342 } 343 344 resubmit: 345 /* resubmit the URB */ 346 st = usb_submit_urb(urb, GFP_ATOMIC); 347 if (st < 0) 348 pr_err("usb_submit_urb() ret %d\n", st); 349 } 350 351 /* 352 * ISOC message interrupt from the USB device 353 * 354 * Analyse each packet and call the subdriver for copy to the frame buffer. 355 */ 356 static void isoc_irq(struct urb *urb) 357 { 358 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context; 359 360 PDEBUG(D_PACK, "isoc irq"); 361 if (!gspca_dev->streaming) 362 return; 363 fill_frame(gspca_dev, urb); 364 } 365 366 /* 367 * bulk message interrupt from the USB device 368 */ 369 static void bulk_irq(struct urb *urb) 370 { 371 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context; 372 int st; 373 374 PDEBUG(D_PACK, "bulk irq"); 375 if (!gspca_dev->streaming) 376 return; 377 switch (urb->status) { 378 case 0: 379 break; 380 case -ESHUTDOWN: 381 return; /* disconnection */ 382 default: 383 #ifdef CONFIG_PM 384 if (gspca_dev->frozen) 385 return; 386 #endif 387 PERR("urb status: %d", urb->status); 388 urb->status = 0; 389 goto resubmit; 390 } 391 392 PDEBUG(D_PACK, "packet l:%d", urb->actual_length); 393 gspca_dev->sd_desc->pkt_scan(gspca_dev, 394 urb->transfer_buffer, 395 urb->actual_length); 396 397 resubmit: 398 /* resubmit the URB */ 399 if (gspca_dev->cam.bulk_nurbs != 0) { 400 st = usb_submit_urb(urb, GFP_ATOMIC); 401 if (st < 0) 402 pr_err("usb_submit_urb() ret %d\n", st); 403 } 404 } 405 406 /* 407 * add data to the current frame 408 * 409 * This function is called by the subdrivers at interrupt level. 410 * 411 * To build a frame, these ones must add 412 * - one FIRST_PACKET 413 * - 0 or many INTER_PACKETs 414 * - one LAST_PACKET 415 * DISCARD_PACKET invalidates the whole frame. 416 */ 417 void gspca_frame_add(struct gspca_dev *gspca_dev, 418 enum gspca_packet_type packet_type, 419 const u8 *data, 420 int len) 421 { 422 struct gspca_frame *frame; 423 int i, j; 424 425 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len); 426 427 if (packet_type == FIRST_PACKET) { 428 i = atomic_read(&gspca_dev->fr_i); 429 430 /* if there are no queued buffer, discard the whole frame */ 431 if (i == atomic_read(&gspca_dev->fr_q)) { 432 gspca_dev->last_packet_type = DISCARD_PACKET; 433 gspca_dev->sequence++; 434 return; 435 } 436 j = gspca_dev->fr_queue[i]; 437 frame = &gspca_dev->frame[j]; 438 v4l2_get_timestamp(&frame->v4l2_buf.timestamp); 439 frame->v4l2_buf.sequence = gspca_dev->sequence++; 440 gspca_dev->image = frame->data; 441 gspca_dev->image_len = 0; 442 } else { 443 switch (gspca_dev->last_packet_type) { 444 case DISCARD_PACKET: 445 if (packet_type == LAST_PACKET) { 446 gspca_dev->last_packet_type = packet_type; 447 gspca_dev->image = NULL; 448 gspca_dev->image_len = 0; 449 } 450 return; 451 case LAST_PACKET: 452 return; 453 } 454 } 455 456 /* append the packet to the frame buffer */ 457 if (len > 0) { 458 if (gspca_dev->image_len + len > gspca_dev->frsz) { 459 PERR("frame overflow %d > %d", 460 gspca_dev->image_len + len, 461 gspca_dev->frsz); 462 packet_type = DISCARD_PACKET; 463 } else { 464 /* !! image is NULL only when last pkt is LAST or DISCARD 465 if (gspca_dev->image == NULL) { 466 pr_err("gspca_frame_add() image == NULL\n"); 467 return; 468 } 469 */ 470 memcpy(gspca_dev->image + gspca_dev->image_len, 471 data, len); 472 gspca_dev->image_len += len; 473 } 474 } 475 gspca_dev->last_packet_type = packet_type; 476 477 /* if last packet, invalidate packet concatenation until 478 * next first packet, wake up the application and advance 479 * in the queue */ 480 if (packet_type == LAST_PACKET) { 481 i = atomic_read(&gspca_dev->fr_i); 482 j = gspca_dev->fr_queue[i]; 483 frame = &gspca_dev->frame[j]; 484 frame->v4l2_buf.bytesused = gspca_dev->image_len; 485 frame->v4l2_buf.flags = (frame->v4l2_buf.flags 486 | V4L2_BUF_FLAG_DONE) 487 & ~V4L2_BUF_FLAG_QUEUED; 488 i = (i + 1) % GSPCA_MAX_FRAMES; 489 atomic_set(&gspca_dev->fr_i, i); 490 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */ 491 PDEBUG(D_FRAM, "frame complete len:%d", 492 frame->v4l2_buf.bytesused); 493 gspca_dev->image = NULL; 494 gspca_dev->image_len = 0; 495 } 496 } 497 EXPORT_SYMBOL(gspca_frame_add); 498 499 static int frame_alloc(struct gspca_dev *gspca_dev, struct file *file, 500 enum v4l2_memory memory, unsigned int count) 501 { 502 struct gspca_frame *frame; 503 unsigned int frsz; 504 int i; 505 506 frsz = gspca_dev->pixfmt.sizeimage; 507 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz); 508 frsz = PAGE_ALIGN(frsz); 509 if (count >= GSPCA_MAX_FRAMES) 510 count = GSPCA_MAX_FRAMES - 1; 511 gspca_dev->frbuf = vmalloc_32(frsz * count); 512 if (!gspca_dev->frbuf) { 513 pr_err("frame alloc failed\n"); 514 return -ENOMEM; 515 } 516 gspca_dev->capt_file = file; 517 gspca_dev->memory = memory; 518 gspca_dev->frsz = frsz; 519 gspca_dev->nframes = count; 520 for (i = 0; i < count; i++) { 521 frame = &gspca_dev->frame[i]; 522 frame->v4l2_buf.index = i; 523 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 524 frame->v4l2_buf.flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 525 frame->v4l2_buf.field = V4L2_FIELD_NONE; 526 frame->v4l2_buf.length = frsz; 527 frame->v4l2_buf.memory = memory; 528 frame->v4l2_buf.sequence = 0; 529 frame->data = gspca_dev->frbuf + i * frsz; 530 frame->v4l2_buf.m.offset = i * frsz; 531 } 532 atomic_set(&gspca_dev->fr_q, 0); 533 atomic_set(&gspca_dev->fr_i, 0); 534 gspca_dev->fr_o = 0; 535 return 0; 536 } 537 538 static void frame_free(struct gspca_dev *gspca_dev) 539 { 540 int i; 541 542 PDEBUG(D_STREAM, "frame free"); 543 if (gspca_dev->frbuf != NULL) { 544 vfree(gspca_dev->frbuf); 545 gspca_dev->frbuf = NULL; 546 for (i = 0; i < gspca_dev->nframes; i++) 547 gspca_dev->frame[i].data = NULL; 548 } 549 gspca_dev->nframes = 0; 550 gspca_dev->frsz = 0; 551 gspca_dev->capt_file = NULL; 552 gspca_dev->memory = GSPCA_MEMORY_NO; 553 } 554 555 static void destroy_urbs(struct gspca_dev *gspca_dev) 556 { 557 struct urb *urb; 558 unsigned int i; 559 560 PDEBUG(D_STREAM, "kill transfer"); 561 for (i = 0; i < MAX_NURBS; i++) { 562 urb = gspca_dev->urb[i]; 563 if (urb == NULL) 564 break; 565 566 gspca_dev->urb[i] = NULL; 567 usb_kill_urb(urb); 568 usb_free_coherent(gspca_dev->dev, 569 urb->transfer_buffer_length, 570 urb->transfer_buffer, 571 urb->transfer_dma); 572 usb_free_urb(urb); 573 } 574 } 575 576 static int gspca_set_alt0(struct gspca_dev *gspca_dev) 577 { 578 int ret; 579 580 if (gspca_dev->alt == 0) 581 return 0; 582 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0); 583 if (ret < 0) 584 pr_err("set alt 0 err %d\n", ret); 585 return ret; 586 } 587 588 /* Note: both the queue and the usb locks should be held when calling this */ 589 static void gspca_stream_off(struct gspca_dev *gspca_dev) 590 { 591 gspca_dev->streaming = 0; 592 gspca_dev->usb_err = 0; 593 if (gspca_dev->sd_desc->stopN) 594 gspca_dev->sd_desc->stopN(gspca_dev); 595 destroy_urbs(gspca_dev); 596 gspca_input_destroy_urb(gspca_dev); 597 gspca_set_alt0(gspca_dev); 598 gspca_input_create_urb(gspca_dev); 599 if (gspca_dev->sd_desc->stop0) 600 gspca_dev->sd_desc->stop0(gspca_dev); 601 PDEBUG(D_STREAM, "stream off OK"); 602 } 603 604 /* 605 * look for an input transfer endpoint in an alternate setting. 606 * 607 * If xfer_ep is invalid, return the first valid ep found, otherwise 608 * look for exactly the ep with address equal to xfer_ep. 609 */ 610 static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt, 611 int xfer, int xfer_ep) 612 { 613 struct usb_host_endpoint *ep; 614 int i, attr; 615 616 for (i = 0; i < alt->desc.bNumEndpoints; i++) { 617 ep = &alt->endpoint[i]; 618 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK; 619 if (attr == xfer 620 && ep->desc.wMaxPacketSize != 0 621 && usb_endpoint_dir_in(&ep->desc) 622 && (xfer_ep < 0 || ep->desc.bEndpointAddress == xfer_ep)) 623 return ep; 624 } 625 return NULL; 626 } 627 628 /* compute the minimum bandwidth for the current transfer */ 629 static u32 which_bandwidth(struct gspca_dev *gspca_dev) 630 { 631 u32 bandwidth; 632 633 /* get the (max) image size */ 634 bandwidth = gspca_dev->pixfmt.sizeimage; 635 636 /* if the image is compressed, estimate its mean size */ 637 if (!gspca_dev->cam.needs_full_bandwidth && 638 bandwidth < gspca_dev->pixfmt.width * 639 gspca_dev->pixfmt.height) 640 bandwidth = bandwidth * 3 / 8; /* 0.375 */ 641 642 /* estimate the frame rate */ 643 if (gspca_dev->sd_desc->get_streamparm) { 644 struct v4l2_streamparm parm; 645 646 gspca_dev->sd_desc->get_streamparm(gspca_dev, &parm); 647 bandwidth *= parm.parm.capture.timeperframe.denominator; 648 bandwidth /= parm.parm.capture.timeperframe.numerator; 649 } else { 650 651 /* don't hope more than 15 fps with USB 1.1 and 652 * image resolution >= 640x480 */ 653 if (gspca_dev->pixfmt.width >= 640 654 && gspca_dev->dev->speed == USB_SPEED_FULL) 655 bandwidth *= 15; /* 15 fps */ 656 else 657 bandwidth *= 30; /* 30 fps */ 658 } 659 660 PDEBUG(D_STREAM, "min bandwidth: %d", bandwidth); 661 return bandwidth; 662 } 663 664 /* endpoint table */ 665 #define MAX_ALT 16 666 struct ep_tb_s { 667 u32 alt; 668 u32 bandwidth; 669 }; 670 671 /* 672 * build the table of the endpoints 673 * and compute the minimum bandwidth for the image transfer 674 */ 675 static int build_isoc_ep_tb(struct gspca_dev *gspca_dev, 676 struct usb_interface *intf, 677 struct ep_tb_s *ep_tb) 678 { 679 struct usb_host_endpoint *ep; 680 int i, j, nbalt, psize, found; 681 u32 bandwidth, last_bw; 682 683 nbalt = intf->num_altsetting; 684 if (nbalt > MAX_ALT) 685 nbalt = MAX_ALT; /* fixme: should warn */ 686 687 /* build the endpoint table */ 688 i = 0; 689 last_bw = 0; 690 for (;;) { 691 ep_tb->bandwidth = 2000 * 2000 * 120; 692 found = 0; 693 for (j = 0; j < nbalt; j++) { 694 ep = alt_xfer(&intf->altsetting[j], 695 USB_ENDPOINT_XFER_ISOC, 696 gspca_dev->xfer_ep); 697 if (ep == NULL) 698 continue; 699 if (ep->desc.bInterval == 0) { 700 pr_err("alt %d iso endp with 0 interval\n", j); 701 continue; 702 } 703 psize = le16_to_cpu(ep->desc.wMaxPacketSize); 704 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3)); 705 bandwidth = psize * 1000; 706 if (gspca_dev->dev->speed == USB_SPEED_HIGH 707 || gspca_dev->dev->speed >= USB_SPEED_SUPER) 708 bandwidth *= 8; 709 bandwidth /= 1 << (ep->desc.bInterval - 1); 710 if (bandwidth <= last_bw) 711 continue; 712 if (bandwidth < ep_tb->bandwidth) { 713 ep_tb->bandwidth = bandwidth; 714 ep_tb->alt = j; 715 found = 1; 716 } 717 } 718 if (!found) 719 break; 720 PDEBUG(D_STREAM, "alt %d bandwidth %d", 721 ep_tb->alt, ep_tb->bandwidth); 722 last_bw = ep_tb->bandwidth; 723 i++; 724 ep_tb++; 725 } 726 727 /* 728 * If the camera: 729 * has a usb audio class interface (a built in usb mic); and 730 * is a usb 1 full speed device; and 731 * uses the max full speed iso bandwidth; and 732 * and has more than 1 alt setting 733 * then skip the highest alt setting to spare bandwidth for the mic 734 */ 735 if (gspca_dev->audio && 736 gspca_dev->dev->speed == USB_SPEED_FULL && 737 last_bw >= 1000000 && 738 i > 1) { 739 PDEBUG(D_STREAM, "dev has usb audio, skipping highest alt"); 740 i--; 741 ep_tb--; 742 } 743 744 /* get the requested bandwidth and start at the highest atlsetting */ 745 bandwidth = which_bandwidth(gspca_dev); 746 ep_tb--; 747 while (i > 1) { 748 ep_tb--; 749 if (ep_tb->bandwidth < bandwidth) 750 break; 751 i--; 752 } 753 return i; 754 } 755 756 /* 757 * create the URBs for image transfer 758 */ 759 static int create_urbs(struct gspca_dev *gspca_dev, 760 struct usb_host_endpoint *ep) 761 { 762 struct urb *urb; 763 int n, nurbs, i, psize, npkt, bsize; 764 765 /* calculate the packet size and the number of packets */ 766 psize = le16_to_cpu(ep->desc.wMaxPacketSize); 767 768 if (!gspca_dev->cam.bulk) { /* isoc */ 769 770 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */ 771 if (gspca_dev->pkt_size == 0) 772 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3)); 773 else 774 psize = gspca_dev->pkt_size; 775 npkt = gspca_dev->cam.npkt; 776 if (npkt == 0) 777 npkt = 32; /* default value */ 778 bsize = psize * npkt; 779 PDEBUG(D_STREAM, 780 "isoc %d pkts size %d = bsize:%d", 781 npkt, psize, bsize); 782 nurbs = DEF_NURBS; 783 } else { /* bulk */ 784 npkt = 0; 785 bsize = gspca_dev->cam.bulk_size; 786 if (bsize == 0) 787 bsize = psize; 788 PDEBUG(D_STREAM, "bulk bsize:%d", bsize); 789 if (gspca_dev->cam.bulk_nurbs != 0) 790 nurbs = gspca_dev->cam.bulk_nurbs; 791 else 792 nurbs = 1; 793 } 794 795 for (n = 0; n < nurbs; n++) { 796 urb = usb_alloc_urb(npkt, GFP_KERNEL); 797 if (!urb) 798 return -ENOMEM; 799 gspca_dev->urb[n] = urb; 800 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev, 801 bsize, 802 GFP_KERNEL, 803 &urb->transfer_dma); 804 805 if (urb->transfer_buffer == NULL) { 806 pr_err("usb_alloc_coherent failed\n"); 807 return -ENOMEM; 808 } 809 urb->dev = gspca_dev->dev; 810 urb->context = gspca_dev; 811 urb->transfer_buffer_length = bsize; 812 if (npkt != 0) { /* ISOC */ 813 urb->pipe = usb_rcvisocpipe(gspca_dev->dev, 814 ep->desc.bEndpointAddress); 815 urb->transfer_flags = URB_ISO_ASAP 816 | URB_NO_TRANSFER_DMA_MAP; 817 urb->interval = 1 << (ep->desc.bInterval - 1); 818 urb->complete = isoc_irq; 819 urb->number_of_packets = npkt; 820 for (i = 0; i < npkt; i++) { 821 urb->iso_frame_desc[i].length = psize; 822 urb->iso_frame_desc[i].offset = psize * i; 823 } 824 } else { /* bulk */ 825 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev, 826 ep->desc.bEndpointAddress); 827 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; 828 urb->complete = bulk_irq; 829 } 830 } 831 return 0; 832 } 833 834 /* 835 * start the USB transfer 836 */ 837 static int gspca_init_transfer(struct gspca_dev *gspca_dev) 838 { 839 struct usb_interface *intf; 840 struct usb_host_endpoint *ep; 841 struct urb *urb; 842 struct ep_tb_s ep_tb[MAX_ALT]; 843 int n, ret, xfer, alt, alt_idx; 844 845 /* reset the streaming variables */ 846 gspca_dev->image = NULL; 847 gspca_dev->image_len = 0; 848 gspca_dev->last_packet_type = DISCARD_PACKET; 849 gspca_dev->sequence = 0; 850 851 gspca_dev->usb_err = 0; 852 853 /* do the specific subdriver stuff before endpoint selection */ 854 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface); 855 gspca_dev->alt = gspca_dev->cam.bulk ? intf->num_altsetting : 0; 856 if (gspca_dev->sd_desc->isoc_init) { 857 ret = gspca_dev->sd_desc->isoc_init(gspca_dev); 858 if (ret < 0) 859 return ret; 860 } 861 xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK 862 : USB_ENDPOINT_XFER_ISOC; 863 864 /* if bulk or the subdriver forced an altsetting, get the endpoint */ 865 if (gspca_dev->alt != 0) { 866 gspca_dev->alt--; /* (previous version compatibility) */ 867 ep = alt_xfer(&intf->altsetting[gspca_dev->alt], xfer, 868 gspca_dev->xfer_ep); 869 if (ep == NULL) { 870 pr_err("bad altsetting %d\n", gspca_dev->alt); 871 return -EIO; 872 } 873 ep_tb[0].alt = gspca_dev->alt; 874 alt_idx = 1; 875 } else { 876 /* else, compute the minimum bandwidth 877 * and build the endpoint table */ 878 alt_idx = build_isoc_ep_tb(gspca_dev, intf, ep_tb); 879 if (alt_idx <= 0) { 880 pr_err("no transfer endpoint found\n"); 881 return -EIO; 882 } 883 } 884 885 /* set the highest alternate setting and 886 * loop until urb submit succeeds */ 887 gspca_input_destroy_urb(gspca_dev); 888 889 gspca_dev->alt = ep_tb[--alt_idx].alt; 890 alt = -1; 891 for (;;) { 892 if (alt != gspca_dev->alt) { 893 alt = gspca_dev->alt; 894 if (intf->num_altsetting > 1) { 895 ret = usb_set_interface(gspca_dev->dev, 896 gspca_dev->iface, 897 alt); 898 if (ret < 0) { 899 if (ret == -ENOSPC) 900 goto retry; /*fixme: ugly*/ 901 pr_err("set alt %d err %d\n", alt, ret); 902 goto out; 903 } 904 } 905 } 906 if (!gspca_dev->cam.no_urb_create) { 907 PDEBUG(D_STREAM, "init transfer alt %d", alt); 908 ret = create_urbs(gspca_dev, 909 alt_xfer(&intf->altsetting[alt], xfer, 910 gspca_dev->xfer_ep)); 911 if (ret < 0) { 912 destroy_urbs(gspca_dev); 913 goto out; 914 } 915 } 916 917 /* clear the bulk endpoint */ 918 if (gspca_dev->cam.bulk) 919 usb_clear_halt(gspca_dev->dev, 920 gspca_dev->urb[0]->pipe); 921 922 /* start the cam */ 923 ret = gspca_dev->sd_desc->start(gspca_dev); 924 if (ret < 0) { 925 destroy_urbs(gspca_dev); 926 goto out; 927 } 928 gspca_dev->streaming = 1; 929 v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler); 930 931 /* some bulk transfers are started by the subdriver */ 932 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0) 933 break; 934 935 /* submit the URBs */ 936 for (n = 0; n < MAX_NURBS; n++) { 937 urb = gspca_dev->urb[n]; 938 if (urb == NULL) 939 break; 940 ret = usb_submit_urb(urb, GFP_KERNEL); 941 if (ret < 0) 942 break; 943 } 944 if (ret >= 0) 945 break; /* transfer is started */ 946 947 /* something when wrong 948 * stop the webcam and free the transfer resources */ 949 gspca_stream_off(gspca_dev); 950 if (ret != -ENOSPC) { 951 pr_err("usb_submit_urb alt %d err %d\n", 952 gspca_dev->alt, ret); 953 goto out; 954 } 955 956 /* the bandwidth is not wide enough 957 * negotiate or try a lower alternate setting */ 958 retry: 959 PERR("alt %d - bandwidth not wide enough, trying again", alt); 960 msleep(20); /* wait for kill complete */ 961 if (gspca_dev->sd_desc->isoc_nego) { 962 ret = gspca_dev->sd_desc->isoc_nego(gspca_dev); 963 if (ret < 0) 964 goto out; 965 } else { 966 if (alt_idx <= 0) { 967 pr_err("no transfer endpoint found\n"); 968 ret = -EIO; 969 goto out; 970 } 971 gspca_dev->alt = ep_tb[--alt_idx].alt; 972 } 973 } 974 out: 975 gspca_input_create_urb(gspca_dev); 976 return ret; 977 } 978 979 static void gspca_set_default_mode(struct gspca_dev *gspca_dev) 980 { 981 int i; 982 983 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */ 984 gspca_dev->curr_mode = i; 985 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i]; 986 987 /* does nothing if ctrl_handler == NULL */ 988 v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler); 989 } 990 991 static int wxh_to_mode(struct gspca_dev *gspca_dev, 992 int width, int height) 993 { 994 int i; 995 996 for (i = 0; i < gspca_dev->cam.nmodes; i++) { 997 if (width == gspca_dev->cam.cam_mode[i].width 998 && height == gspca_dev->cam.cam_mode[i].height) 999 return i; 1000 } 1001 return -EINVAL; 1002 } 1003 1004 static int wxh_to_nearest_mode(struct gspca_dev *gspca_dev, 1005 int width, int height) 1006 { 1007 int i; 1008 1009 for (i = gspca_dev->cam.nmodes; --i > 0; ) { 1010 if (width >= gspca_dev->cam.cam_mode[i].width 1011 && height >= gspca_dev->cam.cam_mode[i].height) 1012 break; 1013 } 1014 return i; 1015 } 1016 1017 /* 1018 * search a mode with the right pixel format 1019 */ 1020 static int gspca_get_mode(struct gspca_dev *gspca_dev, 1021 int mode, 1022 int pixfmt) 1023 { 1024 int modeU, modeD; 1025 1026 modeU = modeD = mode; 1027 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) { 1028 if (--modeD >= 0) { 1029 if (gspca_dev->cam.cam_mode[modeD].pixelformat 1030 == pixfmt) 1031 return modeD; 1032 } 1033 if (++modeU < gspca_dev->cam.nmodes) { 1034 if (gspca_dev->cam.cam_mode[modeU].pixelformat 1035 == pixfmt) 1036 return modeU; 1037 } 1038 } 1039 return -EINVAL; 1040 } 1041 1042 #ifdef CONFIG_VIDEO_ADV_DEBUG 1043 static int vidioc_g_chip_info(struct file *file, void *priv, 1044 struct v4l2_dbg_chip_info *chip) 1045 { 1046 struct gspca_dev *gspca_dev = video_drvdata(file); 1047 1048 gspca_dev->usb_err = 0; 1049 if (gspca_dev->sd_desc->get_chip_info) 1050 return gspca_dev->sd_desc->get_chip_info(gspca_dev, chip); 1051 return chip->match.addr ? -EINVAL : 0; 1052 } 1053 1054 static int vidioc_g_register(struct file *file, void *priv, 1055 struct v4l2_dbg_register *reg) 1056 { 1057 struct gspca_dev *gspca_dev = video_drvdata(file); 1058 1059 gspca_dev->usb_err = 0; 1060 return gspca_dev->sd_desc->get_register(gspca_dev, reg); 1061 } 1062 1063 static int vidioc_s_register(struct file *file, void *priv, 1064 const struct v4l2_dbg_register *reg) 1065 { 1066 struct gspca_dev *gspca_dev = video_drvdata(file); 1067 1068 gspca_dev->usb_err = 0; 1069 return gspca_dev->sd_desc->set_register(gspca_dev, reg); 1070 } 1071 #endif 1072 1073 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, 1074 struct v4l2_fmtdesc *fmtdesc) 1075 { 1076 struct gspca_dev *gspca_dev = video_drvdata(file); 1077 int i, j, index; 1078 __u32 fmt_tb[8]; 1079 1080 /* give an index to each format */ 1081 index = 0; 1082 j = 0; 1083 for (i = gspca_dev->cam.nmodes; --i >= 0; ) { 1084 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat; 1085 j = 0; 1086 for (;;) { 1087 if (fmt_tb[j] == fmt_tb[index]) 1088 break; 1089 j++; 1090 } 1091 if (j == index) { 1092 if (fmtdesc->index == index) 1093 break; /* new format */ 1094 index++; 1095 if (index >= ARRAY_SIZE(fmt_tb)) 1096 return -EINVAL; 1097 } 1098 } 1099 if (i < 0) 1100 return -EINVAL; /* no more format */ 1101 1102 fmtdesc->pixelformat = fmt_tb[index]; 1103 if (gspca_dev->cam.cam_mode[i].sizeimage < 1104 gspca_dev->cam.cam_mode[i].width * 1105 gspca_dev->cam.cam_mode[i].height) 1106 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED; 1107 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff; 1108 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff; 1109 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff; 1110 fmtdesc->description[3] = fmtdesc->pixelformat >> 24; 1111 fmtdesc->description[4] = '\0'; 1112 return 0; 1113 } 1114 1115 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv, 1116 struct v4l2_format *fmt) 1117 { 1118 struct gspca_dev *gspca_dev = video_drvdata(file); 1119 1120 fmt->fmt.pix = gspca_dev->pixfmt; 1121 /* some drivers use priv internally, zero it before giving it back to 1122 the core */ 1123 fmt->fmt.pix.priv = 0; 1124 return 0; 1125 } 1126 1127 static int try_fmt_vid_cap(struct gspca_dev *gspca_dev, 1128 struct v4l2_format *fmt) 1129 { 1130 int w, h, mode, mode2; 1131 1132 w = fmt->fmt.pix.width; 1133 h = fmt->fmt.pix.height; 1134 1135 PDEBUG_MODE(gspca_dev, D_CONF, "try fmt cap", 1136 fmt->fmt.pix.pixelformat, w, h); 1137 1138 /* search the nearest mode for width and height */ 1139 mode = wxh_to_nearest_mode(gspca_dev, w, h); 1140 1141 /* OK if right palette */ 1142 if (gspca_dev->cam.cam_mode[mode].pixelformat 1143 != fmt->fmt.pix.pixelformat) { 1144 1145 /* else, search the closest mode with the same pixel format */ 1146 mode2 = gspca_get_mode(gspca_dev, mode, 1147 fmt->fmt.pix.pixelformat); 1148 if (mode2 >= 0) 1149 mode = mode2; 1150 } 1151 fmt->fmt.pix = gspca_dev->cam.cam_mode[mode]; 1152 if (gspca_dev->sd_desc->try_fmt) { 1153 /* pass original resolution to subdriver try_fmt */ 1154 fmt->fmt.pix.width = w; 1155 fmt->fmt.pix.height = h; 1156 gspca_dev->sd_desc->try_fmt(gspca_dev, fmt); 1157 } 1158 /* some drivers use priv internally, zero it before giving it back to 1159 the core */ 1160 fmt->fmt.pix.priv = 0; 1161 return mode; /* used when s_fmt */ 1162 } 1163 1164 static int vidioc_try_fmt_vid_cap(struct file *file, 1165 void *priv, 1166 struct v4l2_format *fmt) 1167 { 1168 struct gspca_dev *gspca_dev = video_drvdata(file); 1169 int ret; 1170 1171 ret = try_fmt_vid_cap(gspca_dev, fmt); 1172 if (ret < 0) 1173 return ret; 1174 return 0; 1175 } 1176 1177 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, 1178 struct v4l2_format *fmt) 1179 { 1180 struct gspca_dev *gspca_dev = video_drvdata(file); 1181 int ret; 1182 1183 if (mutex_lock_interruptible(&gspca_dev->queue_lock)) 1184 return -ERESTARTSYS; 1185 1186 ret = try_fmt_vid_cap(gspca_dev, fmt); 1187 if (ret < 0) 1188 goto out; 1189 1190 if (gspca_dev->nframes != 0 1191 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) { 1192 ret = -EINVAL; 1193 goto out; 1194 } 1195 1196 if (gspca_dev->streaming) { 1197 ret = -EBUSY; 1198 goto out; 1199 } 1200 gspca_dev->curr_mode = ret; 1201 if (gspca_dev->sd_desc->try_fmt) 1202 /* subdriver try_fmt can modify format parameters */ 1203 gspca_dev->pixfmt = fmt->fmt.pix; 1204 else 1205 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[ret]; 1206 1207 ret = 0; 1208 out: 1209 mutex_unlock(&gspca_dev->queue_lock); 1210 return ret; 1211 } 1212 1213 static int vidioc_enum_framesizes(struct file *file, void *priv, 1214 struct v4l2_frmsizeenum *fsize) 1215 { 1216 struct gspca_dev *gspca_dev = video_drvdata(file); 1217 int i; 1218 __u32 index = 0; 1219 1220 if (gspca_dev->sd_desc->enum_framesizes) 1221 return gspca_dev->sd_desc->enum_framesizes(gspca_dev, fsize); 1222 1223 for (i = 0; i < gspca_dev->cam.nmodes; i++) { 1224 if (fsize->pixel_format != 1225 gspca_dev->cam.cam_mode[i].pixelformat) 1226 continue; 1227 1228 if (fsize->index == index) { 1229 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE; 1230 fsize->discrete.width = 1231 gspca_dev->cam.cam_mode[i].width; 1232 fsize->discrete.height = 1233 gspca_dev->cam.cam_mode[i].height; 1234 return 0; 1235 } 1236 index++; 1237 } 1238 1239 return -EINVAL; 1240 } 1241 1242 static int vidioc_enum_frameintervals(struct file *filp, void *priv, 1243 struct v4l2_frmivalenum *fival) 1244 { 1245 struct gspca_dev *gspca_dev = video_drvdata(filp); 1246 int mode; 1247 __u32 i; 1248 1249 mode = wxh_to_mode(gspca_dev, fival->width, fival->height); 1250 if (mode < 0) 1251 return -EINVAL; 1252 1253 if (gspca_dev->cam.mode_framerates == NULL || 1254 gspca_dev->cam.mode_framerates[mode].nrates == 0) 1255 return -EINVAL; 1256 1257 if (fival->pixel_format != 1258 gspca_dev->cam.cam_mode[mode].pixelformat) 1259 return -EINVAL; 1260 1261 for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) { 1262 if (fival->index == i) { 1263 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE; 1264 fival->discrete.numerator = 1; 1265 fival->discrete.denominator = 1266 gspca_dev->cam.mode_framerates[mode].rates[i]; 1267 return 0; 1268 } 1269 } 1270 1271 return -EINVAL; 1272 } 1273 1274 static void gspca_release(struct v4l2_device *v4l2_device) 1275 { 1276 struct gspca_dev *gspca_dev = 1277 container_of(v4l2_device, struct gspca_dev, v4l2_dev); 1278 1279 v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler); 1280 v4l2_device_unregister(&gspca_dev->v4l2_dev); 1281 kfree(gspca_dev->usb_buf); 1282 kfree(gspca_dev); 1283 } 1284 1285 static int dev_open(struct file *file) 1286 { 1287 struct gspca_dev *gspca_dev = video_drvdata(file); 1288 int ret; 1289 1290 PDEBUG(D_STREAM, "[%s] open", current->comm); 1291 1292 /* protect the subdriver against rmmod */ 1293 if (!try_module_get(gspca_dev->module)) 1294 return -ENODEV; 1295 1296 ret = v4l2_fh_open(file); 1297 if (ret) 1298 module_put(gspca_dev->module); 1299 return ret; 1300 } 1301 1302 static int dev_close(struct file *file) 1303 { 1304 struct gspca_dev *gspca_dev = video_drvdata(file); 1305 1306 PDEBUG(D_STREAM, "[%s] close", current->comm); 1307 1308 /* Needed for gspca_stream_off, always lock before queue_lock! */ 1309 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) 1310 return -ERESTARTSYS; 1311 1312 if (mutex_lock_interruptible(&gspca_dev->queue_lock)) { 1313 mutex_unlock(&gspca_dev->usb_lock); 1314 return -ERESTARTSYS; 1315 } 1316 1317 /* if the file did the capture, free the streaming resources */ 1318 if (gspca_dev->capt_file == file) { 1319 if (gspca_dev->streaming) 1320 gspca_stream_off(gspca_dev); 1321 frame_free(gspca_dev); 1322 } 1323 module_put(gspca_dev->module); 1324 mutex_unlock(&gspca_dev->queue_lock); 1325 mutex_unlock(&gspca_dev->usb_lock); 1326 1327 PDEBUG(D_STREAM, "close done"); 1328 1329 return v4l2_fh_release(file); 1330 } 1331 1332 static int vidioc_querycap(struct file *file, void *priv, 1333 struct v4l2_capability *cap) 1334 { 1335 struct gspca_dev *gspca_dev = video_drvdata(file); 1336 1337 strlcpy((char *) cap->driver, gspca_dev->sd_desc->name, 1338 sizeof cap->driver); 1339 if (gspca_dev->dev->product != NULL) { 1340 strlcpy((char *) cap->card, gspca_dev->dev->product, 1341 sizeof cap->card); 1342 } else { 1343 snprintf((char *) cap->card, sizeof cap->card, 1344 "USB Camera (%04x:%04x)", 1345 le16_to_cpu(gspca_dev->dev->descriptor.idVendor), 1346 le16_to_cpu(gspca_dev->dev->descriptor.idProduct)); 1347 } 1348 usb_make_path(gspca_dev->dev, (char *) cap->bus_info, 1349 sizeof(cap->bus_info)); 1350 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE 1351 | V4L2_CAP_STREAMING 1352 | V4L2_CAP_READWRITE; 1353 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; 1354 return 0; 1355 } 1356 1357 static int vidioc_enum_input(struct file *file, void *priv, 1358 struct v4l2_input *input) 1359 { 1360 struct gspca_dev *gspca_dev = video_drvdata(file); 1361 1362 if (input->index != 0) 1363 return -EINVAL; 1364 input->type = V4L2_INPUT_TYPE_CAMERA; 1365 input->status = gspca_dev->cam.input_flags; 1366 strlcpy(input->name, gspca_dev->sd_desc->name, 1367 sizeof input->name); 1368 return 0; 1369 } 1370 1371 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i) 1372 { 1373 *i = 0; 1374 return 0; 1375 } 1376 1377 static int vidioc_s_input(struct file *file, void *priv, unsigned int i) 1378 { 1379 if (i > 0) 1380 return -EINVAL; 1381 return (0); 1382 } 1383 1384 static int vidioc_reqbufs(struct file *file, void *priv, 1385 struct v4l2_requestbuffers *rb) 1386 { 1387 struct gspca_dev *gspca_dev = video_drvdata(file); 1388 int i, ret = 0, streaming; 1389 1390 i = rb->memory; /* (avoid compilation warning) */ 1391 switch (i) { 1392 case GSPCA_MEMORY_READ: /* (internal call) */ 1393 case V4L2_MEMORY_MMAP: 1394 case V4L2_MEMORY_USERPTR: 1395 break; 1396 default: 1397 return -EINVAL; 1398 } 1399 if (mutex_lock_interruptible(&gspca_dev->queue_lock)) 1400 return -ERESTARTSYS; 1401 1402 if (gspca_dev->memory != GSPCA_MEMORY_NO 1403 && gspca_dev->memory != GSPCA_MEMORY_READ 1404 && gspca_dev->memory != rb->memory) { 1405 ret = -EBUSY; 1406 goto out; 1407 } 1408 1409 /* only one file may do the capture */ 1410 if (gspca_dev->capt_file != NULL 1411 && gspca_dev->capt_file != file) { 1412 ret = -EBUSY; 1413 goto out; 1414 } 1415 1416 /* if allocated, the buffers must not be mapped */ 1417 for (i = 0; i < gspca_dev->nframes; i++) { 1418 if (gspca_dev->frame[i].vma_use_count) { 1419 ret = -EBUSY; 1420 goto out; 1421 } 1422 } 1423 1424 /* stop streaming */ 1425 streaming = gspca_dev->streaming; 1426 if (streaming) { 1427 gspca_stream_off(gspca_dev); 1428 1429 /* Don't restart the stream when switching from read 1430 * to mmap mode */ 1431 if (gspca_dev->memory == GSPCA_MEMORY_READ) 1432 streaming = 0; 1433 } 1434 1435 /* free the previous allocated buffers, if any */ 1436 if (gspca_dev->nframes != 0) 1437 frame_free(gspca_dev); 1438 if (rb->count == 0) /* unrequest */ 1439 goto out; 1440 ret = frame_alloc(gspca_dev, file, rb->memory, rb->count); 1441 if (ret == 0) { 1442 rb->count = gspca_dev->nframes; 1443 if (streaming) 1444 ret = gspca_init_transfer(gspca_dev); 1445 } 1446 out: 1447 mutex_unlock(&gspca_dev->queue_lock); 1448 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count); 1449 return ret; 1450 } 1451 1452 static int vidioc_querybuf(struct file *file, void *priv, 1453 struct v4l2_buffer *v4l2_buf) 1454 { 1455 struct gspca_dev *gspca_dev = video_drvdata(file); 1456 struct gspca_frame *frame; 1457 1458 if (v4l2_buf->index >= gspca_dev->nframes) 1459 return -EINVAL; 1460 1461 frame = &gspca_dev->frame[v4l2_buf->index]; 1462 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf); 1463 return 0; 1464 } 1465 1466 static int vidioc_streamon(struct file *file, void *priv, 1467 enum v4l2_buf_type buf_type) 1468 { 1469 struct gspca_dev *gspca_dev = video_drvdata(file); 1470 int ret; 1471 1472 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 1473 return -EINVAL; 1474 if (mutex_lock_interruptible(&gspca_dev->queue_lock)) 1475 return -ERESTARTSYS; 1476 1477 /* check the capture file */ 1478 if (gspca_dev->capt_file != file) { 1479 ret = -EBUSY; 1480 goto out; 1481 } 1482 1483 if (gspca_dev->nframes == 0 1484 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) { 1485 ret = -EINVAL; 1486 goto out; 1487 } 1488 if (!gspca_dev->streaming) { 1489 ret = gspca_init_transfer(gspca_dev); 1490 if (ret < 0) 1491 goto out; 1492 } 1493 PDEBUG_MODE(gspca_dev, D_STREAM, "stream on OK", 1494 gspca_dev->pixfmt.pixelformat, 1495 gspca_dev->pixfmt.width, gspca_dev->pixfmt.height); 1496 ret = 0; 1497 out: 1498 mutex_unlock(&gspca_dev->queue_lock); 1499 return ret; 1500 } 1501 1502 static int vidioc_streamoff(struct file *file, void *priv, 1503 enum v4l2_buf_type buf_type) 1504 { 1505 struct gspca_dev *gspca_dev = video_drvdata(file); 1506 int i, ret; 1507 1508 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 1509 return -EINVAL; 1510 1511 if (mutex_lock_interruptible(&gspca_dev->queue_lock)) 1512 return -ERESTARTSYS; 1513 1514 if (!gspca_dev->streaming) { 1515 ret = 0; 1516 goto out; 1517 } 1518 1519 /* check the capture file */ 1520 if (gspca_dev->capt_file != file) { 1521 ret = -EBUSY; 1522 goto out; 1523 } 1524 1525 /* stop streaming */ 1526 gspca_stream_off(gspca_dev); 1527 /* In case another thread is waiting in dqbuf */ 1528 wake_up_interruptible(&gspca_dev->wq); 1529 1530 /* empty the transfer queues */ 1531 for (i = 0; i < gspca_dev->nframes; i++) 1532 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS; 1533 atomic_set(&gspca_dev->fr_q, 0); 1534 atomic_set(&gspca_dev->fr_i, 0); 1535 gspca_dev->fr_o = 0; 1536 ret = 0; 1537 out: 1538 mutex_unlock(&gspca_dev->queue_lock); 1539 return ret; 1540 } 1541 1542 static int vidioc_g_jpegcomp(struct file *file, void *priv, 1543 struct v4l2_jpegcompression *jpegcomp) 1544 { 1545 struct gspca_dev *gspca_dev = video_drvdata(file); 1546 1547 gspca_dev->usb_err = 0; 1548 return gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp); 1549 } 1550 1551 static int vidioc_s_jpegcomp(struct file *file, void *priv, 1552 const struct v4l2_jpegcompression *jpegcomp) 1553 { 1554 struct gspca_dev *gspca_dev = video_drvdata(file); 1555 1556 gspca_dev->usb_err = 0; 1557 return gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp); 1558 } 1559 1560 static int vidioc_g_parm(struct file *filp, void *priv, 1561 struct v4l2_streamparm *parm) 1562 { 1563 struct gspca_dev *gspca_dev = video_drvdata(filp); 1564 1565 parm->parm.capture.readbuffers = gspca_dev->nbufread; 1566 1567 if (gspca_dev->sd_desc->get_streamparm) { 1568 gspca_dev->usb_err = 0; 1569 gspca_dev->sd_desc->get_streamparm(gspca_dev, parm); 1570 return gspca_dev->usb_err; 1571 } 1572 return 0; 1573 } 1574 1575 static int vidioc_s_parm(struct file *filp, void *priv, 1576 struct v4l2_streamparm *parm) 1577 { 1578 struct gspca_dev *gspca_dev = video_drvdata(filp); 1579 unsigned int n; 1580 1581 n = parm->parm.capture.readbuffers; 1582 if (n == 0 || n >= GSPCA_MAX_FRAMES) 1583 parm->parm.capture.readbuffers = gspca_dev->nbufread; 1584 else 1585 gspca_dev->nbufread = n; 1586 1587 if (gspca_dev->sd_desc->set_streamparm) { 1588 gspca_dev->usb_err = 0; 1589 gspca_dev->sd_desc->set_streamparm(gspca_dev, parm); 1590 return gspca_dev->usb_err; 1591 } 1592 1593 return 0; 1594 } 1595 1596 static int dev_mmap(struct file *file, struct vm_area_struct *vma) 1597 { 1598 struct gspca_dev *gspca_dev = video_drvdata(file); 1599 struct gspca_frame *frame; 1600 struct page *page; 1601 unsigned long addr, start, size; 1602 int i, ret; 1603 1604 start = vma->vm_start; 1605 size = vma->vm_end - vma->vm_start; 1606 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size); 1607 1608 if (mutex_lock_interruptible(&gspca_dev->queue_lock)) 1609 return -ERESTARTSYS; 1610 if (gspca_dev->capt_file != file) { 1611 ret = -EINVAL; 1612 goto out; 1613 } 1614 1615 frame = NULL; 1616 for (i = 0; i < gspca_dev->nframes; ++i) { 1617 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) { 1618 PDEBUG(D_STREAM, "mmap bad memory type"); 1619 break; 1620 } 1621 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT) 1622 == vma->vm_pgoff) { 1623 frame = &gspca_dev->frame[i]; 1624 break; 1625 } 1626 } 1627 if (frame == NULL) { 1628 PDEBUG(D_STREAM, "mmap no frame buffer found"); 1629 ret = -EINVAL; 1630 goto out; 1631 } 1632 if (size != frame->v4l2_buf.length) { 1633 PDEBUG(D_STREAM, "mmap bad size"); 1634 ret = -EINVAL; 1635 goto out; 1636 } 1637 1638 /* 1639 * - VM_IO marks the area as being a mmaped region for I/O to a 1640 * device. It also prevents the region from being core dumped. 1641 */ 1642 vma->vm_flags |= VM_IO; 1643 1644 addr = (unsigned long) frame->data; 1645 while (size > 0) { 1646 page = vmalloc_to_page((void *) addr); 1647 ret = vm_insert_page(vma, start, page); 1648 if (ret < 0) 1649 goto out; 1650 start += PAGE_SIZE; 1651 addr += PAGE_SIZE; 1652 size -= PAGE_SIZE; 1653 } 1654 1655 vma->vm_ops = &gspca_vm_ops; 1656 vma->vm_private_data = frame; 1657 gspca_vm_open(vma); 1658 ret = 0; 1659 out: 1660 mutex_unlock(&gspca_dev->queue_lock); 1661 return ret; 1662 } 1663 1664 static int frame_ready_nolock(struct gspca_dev *gspca_dev, struct file *file, 1665 enum v4l2_memory memory) 1666 { 1667 if (!gspca_dev->present) 1668 return -ENODEV; 1669 if (gspca_dev->capt_file != file || gspca_dev->memory != memory || 1670 !gspca_dev->streaming) 1671 return -EINVAL; 1672 1673 /* check if a frame is ready */ 1674 return gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i); 1675 } 1676 1677 static int frame_ready(struct gspca_dev *gspca_dev, struct file *file, 1678 enum v4l2_memory memory) 1679 { 1680 int ret; 1681 1682 if (mutex_lock_interruptible(&gspca_dev->queue_lock)) 1683 return -ERESTARTSYS; 1684 ret = frame_ready_nolock(gspca_dev, file, memory); 1685 mutex_unlock(&gspca_dev->queue_lock); 1686 return ret; 1687 } 1688 1689 /* 1690 * dequeue a video buffer 1691 * 1692 * If nonblock_ing is false, block until a buffer is available. 1693 */ 1694 static int vidioc_dqbuf(struct file *file, void *priv, 1695 struct v4l2_buffer *v4l2_buf) 1696 { 1697 struct gspca_dev *gspca_dev = video_drvdata(file); 1698 struct gspca_frame *frame; 1699 int i, j, ret; 1700 1701 PDEBUG(D_FRAM, "dqbuf"); 1702 1703 if (mutex_lock_interruptible(&gspca_dev->queue_lock)) 1704 return -ERESTARTSYS; 1705 1706 for (;;) { 1707 ret = frame_ready_nolock(gspca_dev, file, v4l2_buf->memory); 1708 if (ret < 0) 1709 goto out; 1710 if (ret > 0) 1711 break; 1712 1713 mutex_unlock(&gspca_dev->queue_lock); 1714 1715 if (file->f_flags & O_NONBLOCK) 1716 return -EAGAIN; 1717 1718 /* wait till a frame is ready */ 1719 ret = wait_event_interruptible_timeout(gspca_dev->wq, 1720 frame_ready(gspca_dev, file, v4l2_buf->memory), 1721 msecs_to_jiffies(3000)); 1722 if (ret < 0) 1723 return ret; 1724 if (ret == 0) 1725 return -EIO; 1726 1727 if (mutex_lock_interruptible(&gspca_dev->queue_lock)) 1728 return -ERESTARTSYS; 1729 } 1730 1731 i = gspca_dev->fr_o; 1732 j = gspca_dev->fr_queue[i]; 1733 frame = &gspca_dev->frame[j]; 1734 1735 gspca_dev->fr_o = (i + 1) % GSPCA_MAX_FRAMES; 1736 1737 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE; 1738 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf); 1739 PDEBUG(D_FRAM, "dqbuf %d", j); 1740 ret = 0; 1741 1742 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) { 1743 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr, 1744 frame->data, 1745 frame->v4l2_buf.bytesused)) { 1746 PERR("dqbuf cp to user failed"); 1747 ret = -EFAULT; 1748 } 1749 } 1750 out: 1751 mutex_unlock(&gspca_dev->queue_lock); 1752 1753 if (ret == 0 && gspca_dev->sd_desc->dq_callback) { 1754 mutex_lock(&gspca_dev->usb_lock); 1755 gspca_dev->usb_err = 0; 1756 if (gspca_dev->present) 1757 gspca_dev->sd_desc->dq_callback(gspca_dev); 1758 mutex_unlock(&gspca_dev->usb_lock); 1759 } 1760 1761 return ret; 1762 } 1763 1764 /* 1765 * queue a video buffer 1766 * 1767 * Attempting to queue a buffer that has already been 1768 * queued will return -EINVAL. 1769 */ 1770 static int vidioc_qbuf(struct file *file, void *priv, 1771 struct v4l2_buffer *v4l2_buf) 1772 { 1773 struct gspca_dev *gspca_dev = video_drvdata(file); 1774 struct gspca_frame *frame; 1775 int i, index, ret; 1776 1777 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index); 1778 1779 if (mutex_lock_interruptible(&gspca_dev->queue_lock)) 1780 return -ERESTARTSYS; 1781 1782 index = v4l2_buf->index; 1783 if ((unsigned) index >= gspca_dev->nframes) { 1784 PDEBUG(D_FRAM, 1785 "qbuf idx %d >= %d", index, gspca_dev->nframes); 1786 ret = -EINVAL; 1787 goto out; 1788 } 1789 if (v4l2_buf->memory != gspca_dev->memory) { 1790 PDEBUG(D_FRAM, "qbuf bad memory type"); 1791 ret = -EINVAL; 1792 goto out; 1793 } 1794 1795 frame = &gspca_dev->frame[index]; 1796 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) { 1797 PDEBUG(D_FRAM, "qbuf bad state"); 1798 ret = -EINVAL; 1799 goto out; 1800 } 1801 1802 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED; 1803 1804 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) { 1805 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr; 1806 frame->v4l2_buf.length = v4l2_buf->length; 1807 } 1808 1809 /* put the buffer in the 'queued' queue */ 1810 i = atomic_read(&gspca_dev->fr_q); 1811 gspca_dev->fr_queue[i] = index; 1812 atomic_set(&gspca_dev->fr_q, (i + 1) % GSPCA_MAX_FRAMES); 1813 1814 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED; 1815 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE; 1816 ret = 0; 1817 out: 1818 mutex_unlock(&gspca_dev->queue_lock); 1819 return ret; 1820 } 1821 1822 /* 1823 * allocate the resources for read() 1824 */ 1825 static int read_alloc(struct gspca_dev *gspca_dev, 1826 struct file *file) 1827 { 1828 struct v4l2_buffer v4l2_buf; 1829 int i, ret; 1830 1831 PDEBUG(D_STREAM, "read alloc"); 1832 1833 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) 1834 return -ERESTARTSYS; 1835 1836 if (gspca_dev->nframes == 0) { 1837 struct v4l2_requestbuffers rb; 1838 1839 memset(&rb, 0, sizeof rb); 1840 rb.count = gspca_dev->nbufread; 1841 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 1842 rb.memory = GSPCA_MEMORY_READ; 1843 ret = vidioc_reqbufs(file, gspca_dev, &rb); 1844 if (ret != 0) { 1845 PDEBUG(D_STREAM, "read reqbuf err %d", ret); 1846 goto out; 1847 } 1848 memset(&v4l2_buf, 0, sizeof v4l2_buf); 1849 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 1850 v4l2_buf.memory = GSPCA_MEMORY_READ; 1851 for (i = 0; i < gspca_dev->nbufread; i++) { 1852 v4l2_buf.index = i; 1853 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf); 1854 if (ret != 0) { 1855 PDEBUG(D_STREAM, "read qbuf err: %d", ret); 1856 goto out; 1857 } 1858 } 1859 } 1860 1861 /* start streaming */ 1862 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE); 1863 if (ret != 0) 1864 PDEBUG(D_STREAM, "read streamon err %d", ret); 1865 out: 1866 mutex_unlock(&gspca_dev->usb_lock); 1867 return ret; 1868 } 1869 1870 static unsigned int dev_poll(struct file *file, poll_table *wait) 1871 { 1872 struct gspca_dev *gspca_dev = video_drvdata(file); 1873 unsigned long req_events = poll_requested_events(wait); 1874 int ret = 0; 1875 1876 PDEBUG(D_FRAM, "poll"); 1877 1878 if (req_events & POLLPRI) 1879 ret |= v4l2_ctrl_poll(file, wait); 1880 1881 if (req_events & (POLLIN | POLLRDNORM)) { 1882 /* if reqbufs is not done, the user would use read() */ 1883 if (gspca_dev->memory == GSPCA_MEMORY_NO) { 1884 if (read_alloc(gspca_dev, file) != 0) { 1885 ret |= POLLERR; 1886 goto out; 1887 } 1888 } 1889 1890 poll_wait(file, &gspca_dev->wq, wait); 1891 1892 /* check if an image has been received */ 1893 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0) { 1894 ret |= POLLERR; 1895 goto out; 1896 } 1897 if (gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i)) 1898 ret |= POLLIN | POLLRDNORM; 1899 mutex_unlock(&gspca_dev->queue_lock); 1900 } 1901 1902 out: 1903 if (!gspca_dev->present) 1904 ret |= POLLHUP; 1905 1906 return ret; 1907 } 1908 1909 static ssize_t dev_read(struct file *file, char __user *data, 1910 size_t count, loff_t *ppos) 1911 { 1912 struct gspca_dev *gspca_dev = video_drvdata(file); 1913 struct gspca_frame *frame; 1914 struct v4l2_buffer v4l2_buf; 1915 struct timeval timestamp; 1916 int n, ret, ret2; 1917 1918 PDEBUG(D_FRAM, "read (%zd)", count); 1919 if (gspca_dev->memory == GSPCA_MEMORY_NO) { /* first time ? */ 1920 ret = read_alloc(gspca_dev, file); 1921 if (ret != 0) 1922 return ret; 1923 } 1924 1925 /* get a frame */ 1926 v4l2_get_timestamp(×tamp); 1927 timestamp.tv_sec--; 1928 n = 2; 1929 for (;;) { 1930 memset(&v4l2_buf, 0, sizeof v4l2_buf); 1931 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 1932 v4l2_buf.memory = GSPCA_MEMORY_READ; 1933 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf); 1934 if (ret != 0) { 1935 PDEBUG(D_STREAM, "read dqbuf err %d", ret); 1936 return ret; 1937 } 1938 1939 /* if the process slept for more than 1 second, 1940 * get a newer frame */ 1941 frame = &gspca_dev->frame[v4l2_buf.index]; 1942 if (--n < 0) 1943 break; /* avoid infinite loop */ 1944 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec) 1945 break; 1946 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf); 1947 if (ret != 0) { 1948 PDEBUG(D_STREAM, "read qbuf err %d", ret); 1949 return ret; 1950 } 1951 } 1952 1953 /* copy the frame */ 1954 if (count > frame->v4l2_buf.bytesused) 1955 count = frame->v4l2_buf.bytesused; 1956 ret = copy_to_user(data, frame->data, count); 1957 if (ret != 0) { 1958 PERR("read cp to user lack %d / %zd", ret, count); 1959 ret = -EFAULT; 1960 goto out; 1961 } 1962 ret = count; 1963 out: 1964 /* in each case, requeue the buffer */ 1965 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf); 1966 if (ret2 != 0) 1967 return ret2; 1968 return ret; 1969 } 1970 1971 static struct v4l2_file_operations dev_fops = { 1972 .owner = THIS_MODULE, 1973 .open = dev_open, 1974 .release = dev_close, 1975 .read = dev_read, 1976 .mmap = dev_mmap, 1977 .unlocked_ioctl = video_ioctl2, 1978 .poll = dev_poll, 1979 }; 1980 1981 static const struct v4l2_ioctl_ops dev_ioctl_ops = { 1982 .vidioc_querycap = vidioc_querycap, 1983 .vidioc_dqbuf = vidioc_dqbuf, 1984 .vidioc_qbuf = vidioc_qbuf, 1985 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, 1986 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, 1987 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, 1988 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, 1989 .vidioc_streamon = vidioc_streamon, 1990 .vidioc_enum_input = vidioc_enum_input, 1991 .vidioc_g_input = vidioc_g_input, 1992 .vidioc_s_input = vidioc_s_input, 1993 .vidioc_reqbufs = vidioc_reqbufs, 1994 .vidioc_querybuf = vidioc_querybuf, 1995 .vidioc_streamoff = vidioc_streamoff, 1996 .vidioc_g_jpegcomp = vidioc_g_jpegcomp, 1997 .vidioc_s_jpegcomp = vidioc_s_jpegcomp, 1998 .vidioc_g_parm = vidioc_g_parm, 1999 .vidioc_s_parm = vidioc_s_parm, 2000 .vidioc_enum_framesizes = vidioc_enum_framesizes, 2001 .vidioc_enum_frameintervals = vidioc_enum_frameintervals, 2002 #ifdef CONFIG_VIDEO_ADV_DEBUG 2003 .vidioc_g_chip_info = vidioc_g_chip_info, 2004 .vidioc_g_register = vidioc_g_register, 2005 .vidioc_s_register = vidioc_s_register, 2006 #endif 2007 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 2008 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 2009 }; 2010 2011 static const struct video_device gspca_template = { 2012 .name = "gspca main driver", 2013 .fops = &dev_fops, 2014 .ioctl_ops = &dev_ioctl_ops, 2015 .release = video_device_release_empty, /* We use v4l2_dev.release */ 2016 }; 2017 2018 /* 2019 * probe and create a new gspca device 2020 * 2021 * This function must be called by the sub-driver when it is 2022 * called for probing a new device. 2023 */ 2024 int gspca_dev_probe2(struct usb_interface *intf, 2025 const struct usb_device_id *id, 2026 const struct sd_desc *sd_desc, 2027 int dev_size, 2028 struct module *module) 2029 { 2030 struct gspca_dev *gspca_dev; 2031 struct usb_device *dev = interface_to_usbdev(intf); 2032 int ret; 2033 2034 pr_info("%s-" GSPCA_VERSION " probing %04x:%04x\n", 2035 sd_desc->name, id->idVendor, id->idProduct); 2036 2037 /* create the device */ 2038 if (dev_size < sizeof *gspca_dev) 2039 dev_size = sizeof *gspca_dev; 2040 gspca_dev = kzalloc(dev_size, GFP_KERNEL); 2041 if (!gspca_dev) { 2042 pr_err("couldn't kzalloc gspca struct\n"); 2043 return -ENOMEM; 2044 } 2045 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL); 2046 if (!gspca_dev->usb_buf) { 2047 pr_err("out of memory\n"); 2048 ret = -ENOMEM; 2049 goto out; 2050 } 2051 gspca_dev->dev = dev; 2052 gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber; 2053 gspca_dev->xfer_ep = -1; 2054 2055 /* check if any audio device */ 2056 if (dev->actconfig->desc.bNumInterfaces != 1) { 2057 int i; 2058 struct usb_interface *intf2; 2059 2060 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) { 2061 intf2 = dev->actconfig->interface[i]; 2062 if (intf2 != NULL 2063 && intf2->altsetting != NULL 2064 && intf2->altsetting->desc.bInterfaceClass == 2065 USB_CLASS_AUDIO) { 2066 gspca_dev->audio = 1; 2067 break; 2068 } 2069 } 2070 } 2071 2072 gspca_dev->v4l2_dev.release = gspca_release; 2073 ret = v4l2_device_register(&intf->dev, &gspca_dev->v4l2_dev); 2074 if (ret) 2075 goto out; 2076 gspca_dev->sd_desc = sd_desc; 2077 gspca_dev->nbufread = 2; 2078 gspca_dev->empty_packet = -1; /* don't check the empty packets */ 2079 gspca_dev->vdev = gspca_template; 2080 gspca_dev->vdev.v4l2_dev = &gspca_dev->v4l2_dev; 2081 video_set_drvdata(&gspca_dev->vdev, gspca_dev); 2082 gspca_dev->module = module; 2083 gspca_dev->present = 1; 2084 2085 mutex_init(&gspca_dev->usb_lock); 2086 gspca_dev->vdev.lock = &gspca_dev->usb_lock; 2087 mutex_init(&gspca_dev->queue_lock); 2088 init_waitqueue_head(&gspca_dev->wq); 2089 2090 /* configure the subdriver and initialize the USB device */ 2091 ret = sd_desc->config(gspca_dev, id); 2092 if (ret < 0) 2093 goto out; 2094 ret = sd_desc->init(gspca_dev); 2095 if (ret < 0) 2096 goto out; 2097 if (sd_desc->init_controls) 2098 ret = sd_desc->init_controls(gspca_dev); 2099 if (ret < 0) 2100 goto out; 2101 gspca_set_default_mode(gspca_dev); 2102 2103 ret = gspca_input_connect(gspca_dev); 2104 if (ret) 2105 goto out; 2106 2107 /* 2108 * Don't take usb_lock for these ioctls. This improves latency if 2109 * usb_lock is taken for a long time, e.g. when changing a control 2110 * value, and a new frame is ready to be dequeued. 2111 */ 2112 v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_DQBUF); 2113 v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QBUF); 2114 v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QUERYBUF); 2115 #ifdef CONFIG_VIDEO_ADV_DEBUG 2116 if (!gspca_dev->sd_desc->get_register) 2117 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_G_REGISTER); 2118 if (!gspca_dev->sd_desc->set_register) 2119 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_S_REGISTER); 2120 #endif 2121 if (!gspca_dev->sd_desc->get_jcomp) 2122 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_G_JPEGCOMP); 2123 if (!gspca_dev->sd_desc->set_jcomp) 2124 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_S_JPEGCOMP); 2125 2126 /* init video stuff */ 2127 ret = video_register_device(&gspca_dev->vdev, 2128 VFL_TYPE_GRABBER, 2129 -1); 2130 if (ret < 0) { 2131 pr_err("video_register_device err %d\n", ret); 2132 goto out; 2133 } 2134 2135 usb_set_intfdata(intf, gspca_dev); 2136 PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev)); 2137 2138 gspca_input_create_urb(gspca_dev); 2139 2140 return 0; 2141 out: 2142 #if IS_ENABLED(CONFIG_INPUT) 2143 if (gspca_dev->input_dev) 2144 input_unregister_device(gspca_dev->input_dev); 2145 #endif 2146 v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler); 2147 kfree(gspca_dev->usb_buf); 2148 kfree(gspca_dev); 2149 return ret; 2150 } 2151 EXPORT_SYMBOL(gspca_dev_probe2); 2152 2153 /* same function as the previous one, but check the interface */ 2154 int gspca_dev_probe(struct usb_interface *intf, 2155 const struct usb_device_id *id, 2156 const struct sd_desc *sd_desc, 2157 int dev_size, 2158 struct module *module) 2159 { 2160 struct usb_device *dev = interface_to_usbdev(intf); 2161 2162 /* we don't handle multi-config cameras */ 2163 if (dev->descriptor.bNumConfigurations != 1) { 2164 pr_err("%04x:%04x too many config\n", 2165 id->idVendor, id->idProduct); 2166 return -ENODEV; 2167 } 2168 2169 /* the USB video interface must be the first one */ 2170 if (dev->actconfig->desc.bNumInterfaces != 1 2171 && intf->cur_altsetting->desc.bInterfaceNumber != 0) 2172 return -ENODEV; 2173 2174 return gspca_dev_probe2(intf, id, sd_desc, dev_size, module); 2175 } 2176 EXPORT_SYMBOL(gspca_dev_probe); 2177 2178 /* 2179 * USB disconnection 2180 * 2181 * This function must be called by the sub-driver 2182 * when the device disconnects, after the specific resources are freed. 2183 */ 2184 void gspca_disconnect(struct usb_interface *intf) 2185 { 2186 struct gspca_dev *gspca_dev = usb_get_intfdata(intf); 2187 #if IS_ENABLED(CONFIG_INPUT) 2188 struct input_dev *input_dev; 2189 #endif 2190 2191 PDEBUG(D_PROBE, "%s disconnect", 2192 video_device_node_name(&gspca_dev->vdev)); 2193 2194 mutex_lock(&gspca_dev->usb_lock); 2195 2196 gspca_dev->present = 0; 2197 destroy_urbs(gspca_dev); 2198 2199 #if IS_ENABLED(CONFIG_INPUT) 2200 gspca_input_destroy_urb(gspca_dev); 2201 input_dev = gspca_dev->input_dev; 2202 if (input_dev) { 2203 gspca_dev->input_dev = NULL; 2204 input_unregister_device(input_dev); 2205 } 2206 #endif 2207 /* Free subdriver's streaming resources / stop sd workqueue(s) */ 2208 if (gspca_dev->sd_desc->stop0 && gspca_dev->streaming) 2209 gspca_dev->sd_desc->stop0(gspca_dev); 2210 gspca_dev->streaming = 0; 2211 gspca_dev->dev = NULL; 2212 wake_up_interruptible(&gspca_dev->wq); 2213 2214 v4l2_device_disconnect(&gspca_dev->v4l2_dev); 2215 video_unregister_device(&gspca_dev->vdev); 2216 2217 mutex_unlock(&gspca_dev->usb_lock); 2218 2219 /* (this will call gspca_release() immediately or on last close) */ 2220 v4l2_device_put(&gspca_dev->v4l2_dev); 2221 } 2222 EXPORT_SYMBOL(gspca_disconnect); 2223 2224 #ifdef CONFIG_PM 2225 int gspca_suspend(struct usb_interface *intf, pm_message_t message) 2226 { 2227 struct gspca_dev *gspca_dev = usb_get_intfdata(intf); 2228 2229 gspca_input_destroy_urb(gspca_dev); 2230 2231 if (!gspca_dev->streaming) 2232 return 0; 2233 2234 mutex_lock(&gspca_dev->usb_lock); 2235 gspca_dev->frozen = 1; /* avoid urb error messages */ 2236 gspca_dev->usb_err = 0; 2237 if (gspca_dev->sd_desc->stopN) 2238 gspca_dev->sd_desc->stopN(gspca_dev); 2239 destroy_urbs(gspca_dev); 2240 gspca_set_alt0(gspca_dev); 2241 if (gspca_dev->sd_desc->stop0) 2242 gspca_dev->sd_desc->stop0(gspca_dev); 2243 mutex_unlock(&gspca_dev->usb_lock); 2244 2245 return 0; 2246 } 2247 EXPORT_SYMBOL(gspca_suspend); 2248 2249 int gspca_resume(struct usb_interface *intf) 2250 { 2251 struct gspca_dev *gspca_dev = usb_get_intfdata(intf); 2252 int streaming, ret = 0; 2253 2254 mutex_lock(&gspca_dev->usb_lock); 2255 gspca_dev->frozen = 0; 2256 gspca_dev->usb_err = 0; 2257 gspca_dev->sd_desc->init(gspca_dev); 2258 /* 2259 * Most subdrivers send all ctrl values on sd_start and thus 2260 * only write to the device registers on s_ctrl when streaming -> 2261 * Clear streaming to avoid setting all ctrls twice. 2262 */ 2263 streaming = gspca_dev->streaming; 2264 gspca_dev->streaming = 0; 2265 if (streaming) 2266 ret = gspca_init_transfer(gspca_dev); 2267 else 2268 gspca_input_create_urb(gspca_dev); 2269 mutex_unlock(&gspca_dev->usb_lock); 2270 2271 return ret; 2272 } 2273 EXPORT_SYMBOL(gspca_resume); 2274 #endif 2275 2276 /* -- module insert / remove -- */ 2277 static int __init gspca_init(void) 2278 { 2279 pr_info("v" GSPCA_VERSION " registered\n"); 2280 return 0; 2281 } 2282 static void __exit gspca_exit(void) 2283 { 2284 } 2285 2286 module_init(gspca_init); 2287 module_exit(gspca_exit); 2288 2289 module_param_named(debug, gspca_debug, int, 0644); 2290 MODULE_PARM_DESC(debug, 2291 "1:probe 2:config 3:stream 4:frame 5:packet 6:usbi 7:usbo"); 2292