1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Main USB camera driver 4 * 5 * Copyright (C) 2008-2011 Jean-François Moine <http://moinejf.free.fr> 6 * 7 * Camera button input handling by Márton Németh 8 * Copyright (C) 2009-2010 Márton Németh <nm127@freemail.hu> 9 */ 10 11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 12 13 #define GSPCA_VERSION "2.14.0" 14 15 #include <linux/init.h> 16 #include <linux/fs.h> 17 #include <linux/vmalloc.h> 18 #include <linux/sched.h> 19 #include <linux/slab.h> 20 #include <linux/mm.h> 21 #include <linux/string.h> 22 #include <linux/pagemap.h> 23 #include <linux/io.h> 24 #include <asm/page.h> 25 #include <linux/uaccess.h> 26 #include <linux/ktime.h> 27 #include <media/v4l2-ioctl.h> 28 #include <media/v4l2-ctrls.h> 29 #include <media/v4l2-fh.h> 30 #include <media/v4l2-event.h> 31 32 #include "gspca.h" 33 34 #if IS_ENABLED(CONFIG_INPUT) 35 #include <linux/input.h> 36 #include <linux/usb/input.h> 37 #endif 38 39 /* global values */ 40 #define DEF_NURBS 3 /* default number of URBs */ 41 #if DEF_NURBS > MAX_NURBS 42 #error "DEF_NURBS too big" 43 #endif 44 45 MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>"); 46 MODULE_DESCRIPTION("GSPCA USB Camera Driver"); 47 MODULE_LICENSE("GPL"); 48 MODULE_VERSION(GSPCA_VERSION); 49 50 int gspca_debug; 51 EXPORT_SYMBOL(gspca_debug); 52 53 static void PDEBUG_MODE(struct gspca_dev *gspca_dev, int debug, char *txt, 54 __u32 pixfmt, int w, int h) 55 { 56 if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') { 57 gspca_dbg(gspca_dev, debug, "%s %c%c%c%c %dx%d\n", 58 txt, 59 pixfmt & 0xff, 60 (pixfmt >> 8) & 0xff, 61 (pixfmt >> 16) & 0xff, 62 pixfmt >> 24, 63 w, h); 64 } else { 65 gspca_dbg(gspca_dev, debug, "%s 0x%08x %dx%d\n", 66 txt, 67 pixfmt, 68 w, h); 69 } 70 } 71 72 /* specific memory types - !! should be different from V4L2_MEMORY_xxx */ 73 #define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */ 74 #define GSPCA_MEMORY_READ 7 75 76 /* 77 * Input and interrupt endpoint handling functions 78 */ 79 #if IS_ENABLED(CONFIG_INPUT) 80 static void int_irq(struct urb *urb) 81 { 82 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context; 83 int ret; 84 85 ret = urb->status; 86 switch (ret) { 87 case 0: 88 if (gspca_dev->sd_desc->int_pkt_scan(gspca_dev, 89 urb->transfer_buffer, urb->actual_length) < 0) { 90 gspca_err(gspca_dev, "Unknown packet received\n"); 91 } 92 break; 93 94 case -ENOENT: 95 case -ECONNRESET: 96 case -ENODEV: 97 case -ESHUTDOWN: 98 /* Stop is requested either by software or hardware is gone, 99 * keep the ret value non-zero and don't resubmit later. 100 */ 101 break; 102 103 default: 104 gspca_err(gspca_dev, "URB error %i, resubmitting\n", 105 urb->status); 106 urb->status = 0; 107 ret = 0; 108 } 109 110 if (ret == 0) { 111 ret = usb_submit_urb(urb, GFP_ATOMIC); 112 if (ret < 0) 113 pr_err("Resubmit URB failed with error %i\n", ret); 114 } 115 } 116 117 static int gspca_input_connect(struct gspca_dev *dev) 118 { 119 struct input_dev *input_dev; 120 int err = 0; 121 122 dev->input_dev = NULL; 123 if (dev->sd_desc->int_pkt_scan || dev->sd_desc->other_input) { 124 input_dev = input_allocate_device(); 125 if (!input_dev) 126 return -ENOMEM; 127 128 usb_make_path(dev->dev, dev->phys, sizeof(dev->phys)); 129 strlcat(dev->phys, "/input0", sizeof(dev->phys)); 130 131 input_dev->name = dev->sd_desc->name; 132 input_dev->phys = dev->phys; 133 134 usb_to_input_id(dev->dev, &input_dev->id); 135 136 input_dev->evbit[0] = BIT_MASK(EV_KEY); 137 input_dev->keybit[BIT_WORD(KEY_CAMERA)] = BIT_MASK(KEY_CAMERA); 138 input_dev->dev.parent = &dev->dev->dev; 139 140 err = input_register_device(input_dev); 141 if (err) { 142 pr_err("Input device registration failed with error %i\n", 143 err); 144 input_dev->dev.parent = NULL; 145 input_free_device(input_dev); 146 } else { 147 dev->input_dev = input_dev; 148 } 149 } 150 151 return err; 152 } 153 154 static int alloc_and_submit_int_urb(struct gspca_dev *gspca_dev, 155 struct usb_endpoint_descriptor *ep) 156 { 157 unsigned int buffer_len; 158 int interval; 159 struct urb *urb; 160 struct usb_device *dev; 161 void *buffer = NULL; 162 int ret = -EINVAL; 163 164 buffer_len = le16_to_cpu(ep->wMaxPacketSize); 165 interval = ep->bInterval; 166 gspca_dbg(gspca_dev, D_CONF, "found int in endpoint: 0x%x, buffer_len=%u, interval=%u\n", 167 ep->bEndpointAddress, buffer_len, interval); 168 169 dev = gspca_dev->dev; 170 171 urb = usb_alloc_urb(0, GFP_KERNEL); 172 if (!urb) { 173 ret = -ENOMEM; 174 goto error; 175 } 176 177 buffer = usb_alloc_coherent(dev, buffer_len, 178 GFP_KERNEL, &urb->transfer_dma); 179 if (!buffer) { 180 ret = -ENOMEM; 181 goto error_buffer; 182 } 183 usb_fill_int_urb(urb, dev, 184 usb_rcvintpipe(dev, ep->bEndpointAddress), 185 buffer, buffer_len, 186 int_irq, (void *)gspca_dev, interval); 187 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 188 ret = usb_submit_urb(urb, GFP_KERNEL); 189 if (ret < 0) { 190 gspca_err(gspca_dev, "submit int URB failed with error %i\n", 191 ret); 192 goto error_submit; 193 } 194 gspca_dev->int_urb = urb; 195 return ret; 196 197 error_submit: 198 usb_free_coherent(dev, 199 urb->transfer_buffer_length, 200 urb->transfer_buffer, 201 urb->transfer_dma); 202 error_buffer: 203 usb_free_urb(urb); 204 error: 205 return ret; 206 } 207 208 static void gspca_input_create_urb(struct gspca_dev *gspca_dev) 209 { 210 struct usb_interface *intf; 211 struct usb_host_interface *intf_desc; 212 struct usb_endpoint_descriptor *ep; 213 int i; 214 215 if (gspca_dev->sd_desc->int_pkt_scan) { 216 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface); 217 intf_desc = intf->cur_altsetting; 218 for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) { 219 ep = &intf_desc->endpoint[i].desc; 220 if (usb_endpoint_dir_in(ep) && 221 usb_endpoint_xfer_int(ep)) { 222 223 alloc_and_submit_int_urb(gspca_dev, ep); 224 break; 225 } 226 } 227 } 228 } 229 230 static void gspca_input_destroy_urb(struct gspca_dev *gspca_dev) 231 { 232 struct urb *urb; 233 234 urb = gspca_dev->int_urb; 235 if (urb) { 236 gspca_dev->int_urb = NULL; 237 usb_kill_urb(urb); 238 usb_free_coherent(gspca_dev->dev, 239 urb->transfer_buffer_length, 240 urb->transfer_buffer, 241 urb->transfer_dma); 242 usb_free_urb(urb); 243 } 244 } 245 #else 246 static inline void gspca_input_destroy_urb(struct gspca_dev *gspca_dev) 247 { 248 } 249 250 static inline void gspca_input_create_urb(struct gspca_dev *gspca_dev) 251 { 252 } 253 254 static inline int gspca_input_connect(struct gspca_dev *dev) 255 { 256 return 0; 257 } 258 #endif 259 260 /* 261 * fill a video frame from an URB and resubmit 262 */ 263 static void fill_frame(struct gspca_dev *gspca_dev, 264 struct urb *urb) 265 { 266 u8 *data; /* address of data in the iso message */ 267 int i, len, st; 268 cam_pkt_op pkt_scan; 269 270 if (urb->status != 0) { 271 if (urb->status == -ESHUTDOWN) 272 return; /* disconnection */ 273 #ifdef CONFIG_PM 274 if (gspca_dev->frozen) 275 return; 276 #endif 277 gspca_err(gspca_dev, "urb status: %d\n", urb->status); 278 urb->status = 0; 279 goto resubmit; 280 } 281 pkt_scan = gspca_dev->sd_desc->pkt_scan; 282 for (i = 0; i < urb->number_of_packets; i++) { 283 len = urb->iso_frame_desc[i].actual_length; 284 285 /* check the packet status and length */ 286 st = urb->iso_frame_desc[i].status; 287 if (st) { 288 gspca_dbg(gspca_dev, D_PACK, "ISOC data error: [%d] len=%d, status=%d\n", 289 i, len, st); 290 gspca_dev->last_packet_type = DISCARD_PACKET; 291 continue; 292 } 293 if (len == 0) { 294 if (gspca_dev->empty_packet == 0) 295 gspca_dev->empty_packet = 1; 296 continue; 297 } 298 299 /* let the packet be analyzed by the subdriver */ 300 gspca_dbg(gspca_dev, D_PACK, "packet [%d] o:%d l:%d\n", 301 i, urb->iso_frame_desc[i].offset, len); 302 data = (u8 *) urb->transfer_buffer 303 + urb->iso_frame_desc[i].offset; 304 pkt_scan(gspca_dev, data, len); 305 } 306 307 resubmit: 308 if (!gspca_dev->streaming) 309 return; 310 /* resubmit the URB */ 311 st = usb_submit_urb(urb, GFP_ATOMIC); 312 if (st < 0) 313 pr_err("usb_submit_urb() ret %d\n", st); 314 } 315 316 /* 317 * ISOC message interrupt from the USB device 318 * 319 * Analyse each packet and call the subdriver for copy to the frame buffer. 320 */ 321 static void isoc_irq(struct urb *urb) 322 { 323 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context; 324 325 gspca_dbg(gspca_dev, D_PACK, "isoc irq\n"); 326 if (!gspca_dev->streaming) 327 return; 328 fill_frame(gspca_dev, urb); 329 } 330 331 /* 332 * bulk message interrupt from the USB device 333 */ 334 static void bulk_irq(struct urb *urb) 335 { 336 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context; 337 int st; 338 339 gspca_dbg(gspca_dev, D_PACK, "bulk irq\n"); 340 if (!gspca_dev->streaming) 341 return; 342 switch (urb->status) { 343 case 0: 344 break; 345 case -ESHUTDOWN: 346 return; /* disconnection */ 347 default: 348 #ifdef CONFIG_PM 349 if (gspca_dev->frozen) 350 return; 351 #endif 352 gspca_err(gspca_dev, "urb status: %d\n", urb->status); 353 urb->status = 0; 354 goto resubmit; 355 } 356 357 gspca_dbg(gspca_dev, D_PACK, "packet l:%d\n", urb->actual_length); 358 gspca_dev->sd_desc->pkt_scan(gspca_dev, 359 urb->transfer_buffer, 360 urb->actual_length); 361 362 resubmit: 363 if (!gspca_dev->streaming) 364 return; 365 /* resubmit the URB */ 366 if (gspca_dev->cam.bulk_nurbs != 0) { 367 st = usb_submit_urb(urb, GFP_ATOMIC); 368 if (st < 0) 369 pr_err("usb_submit_urb() ret %d\n", st); 370 } 371 } 372 373 /* 374 * add data to the current frame 375 * 376 * This function is called by the subdrivers at interrupt level. 377 * 378 * To build a frame, these ones must add 379 * - one FIRST_PACKET 380 * - 0 or many INTER_PACKETs 381 * - one LAST_PACKET 382 * DISCARD_PACKET invalidates the whole frame. 383 */ 384 void gspca_frame_add(struct gspca_dev *gspca_dev, 385 enum gspca_packet_type packet_type, 386 const u8 *data, 387 int len) 388 { 389 struct gspca_buffer *buf; 390 unsigned long flags; 391 392 gspca_dbg(gspca_dev, D_PACK, "add t:%d l:%d\n", packet_type, len); 393 394 spin_lock_irqsave(&gspca_dev->qlock, flags); 395 buf = list_first_entry_or_null(&gspca_dev->buf_list, 396 typeof(*buf), list); 397 spin_unlock_irqrestore(&gspca_dev->qlock, flags); 398 399 if (packet_type == FIRST_PACKET) { 400 /* if there is no queued buffer, discard the whole frame */ 401 if (!buf) { 402 gspca_dev->last_packet_type = DISCARD_PACKET; 403 gspca_dev->sequence++; 404 return; 405 } 406 gspca_dev->image = vb2_plane_vaddr(&buf->vb.vb2_buf, 0); 407 gspca_dev->image_len = 0; 408 } else { 409 switch (gspca_dev->last_packet_type) { 410 case DISCARD_PACKET: 411 if (packet_type == LAST_PACKET) { 412 gspca_dev->last_packet_type = packet_type; 413 gspca_dev->image = NULL; 414 gspca_dev->image_len = 0; 415 } 416 return; 417 case LAST_PACKET: 418 return; 419 } 420 } 421 422 /* append the packet to the frame buffer */ 423 if (len > 0) { 424 if (gspca_dev->image_len + len > PAGE_ALIGN(gspca_dev->pixfmt.sizeimage)) { 425 gspca_err(gspca_dev, "frame overflow %d > %d\n", 426 gspca_dev->image_len + len, 427 PAGE_ALIGN(gspca_dev->pixfmt.sizeimage)); 428 packet_type = DISCARD_PACKET; 429 } else { 430 /* !! image is NULL only when last pkt is LAST or DISCARD 431 if (gspca_dev->image == NULL) { 432 pr_err("gspca_frame_add() image == NULL\n"); 433 return; 434 } 435 */ 436 memcpy(gspca_dev->image + gspca_dev->image_len, 437 data, len); 438 gspca_dev->image_len += len; 439 } 440 } 441 gspca_dev->last_packet_type = packet_type; 442 443 /* if last packet, invalidate packet concatenation until 444 * next first packet, wake up the application and advance 445 * in the queue */ 446 if (packet_type == LAST_PACKET) { 447 spin_lock_irqsave(&gspca_dev->qlock, flags); 448 list_del(&buf->list); 449 spin_unlock_irqrestore(&gspca_dev->qlock, flags); 450 buf->vb.vb2_buf.timestamp = ktime_get_ns(); 451 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, 452 gspca_dev->image_len); 453 buf->vb.sequence = gspca_dev->sequence++; 454 buf->vb.field = V4L2_FIELD_NONE; 455 gspca_dbg(gspca_dev, D_FRAM, "frame complete len:%d\n", 456 gspca_dev->image_len); 457 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE); 458 gspca_dev->image = NULL; 459 gspca_dev->image_len = 0; 460 } 461 } 462 EXPORT_SYMBOL(gspca_frame_add); 463 464 static void destroy_urbs(struct gspca_dev *gspca_dev) 465 { 466 struct urb *urb; 467 unsigned int i; 468 469 gspca_dbg(gspca_dev, D_STREAM, "kill transfer\n"); 470 471 /* Killing all URBs guarantee that no URB completion 472 * handler is running. Therefore, there shouldn't 473 * be anyone trying to access gspca_dev->urb[i] 474 */ 475 for (i = 0; i < MAX_NURBS; i++) 476 usb_kill_urb(gspca_dev->urb[i]); 477 478 gspca_dbg(gspca_dev, D_STREAM, "releasing urbs\n"); 479 for (i = 0; i < MAX_NURBS; i++) { 480 urb = gspca_dev->urb[i]; 481 if (!urb) 482 continue; 483 gspca_dev->urb[i] = NULL; 484 usb_free_coherent(gspca_dev->dev, 485 urb->transfer_buffer_length, 486 urb->transfer_buffer, 487 urb->transfer_dma); 488 usb_free_urb(urb); 489 } 490 } 491 492 static int gspca_set_alt0(struct gspca_dev *gspca_dev) 493 { 494 int ret; 495 496 if (gspca_dev->alt == 0) 497 return 0; 498 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0); 499 if (ret < 0) 500 pr_err("set alt 0 err %d\n", ret); 501 return ret; 502 } 503 504 /* 505 * look for an input transfer endpoint in an alternate setting. 506 * 507 * If xfer_ep is invalid, return the first valid ep found, otherwise 508 * look for exactly the ep with address equal to xfer_ep. 509 */ 510 static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt, 511 int xfer, int xfer_ep) 512 { 513 struct usb_host_endpoint *ep; 514 int i, attr; 515 516 for (i = 0; i < alt->desc.bNumEndpoints; i++) { 517 ep = &alt->endpoint[i]; 518 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK; 519 if (attr == xfer 520 && ep->desc.wMaxPacketSize != 0 521 && usb_endpoint_dir_in(&ep->desc) 522 && (xfer_ep < 0 || ep->desc.bEndpointAddress == xfer_ep)) 523 return ep; 524 } 525 return NULL; 526 } 527 528 /* compute the minimum bandwidth for the current transfer */ 529 static u32 which_bandwidth(struct gspca_dev *gspca_dev) 530 { 531 u32 bandwidth; 532 533 /* get the (max) image size */ 534 bandwidth = gspca_dev->pixfmt.sizeimage; 535 536 /* if the image is compressed, estimate its mean size */ 537 if (!gspca_dev->cam.needs_full_bandwidth && 538 bandwidth < gspca_dev->pixfmt.width * 539 gspca_dev->pixfmt.height) 540 bandwidth = bandwidth * 3 / 8; /* 0.375 */ 541 542 /* estimate the frame rate */ 543 if (gspca_dev->sd_desc->get_streamparm) { 544 struct v4l2_streamparm parm; 545 546 gspca_dev->sd_desc->get_streamparm(gspca_dev, &parm); 547 bandwidth *= parm.parm.capture.timeperframe.denominator; 548 bandwidth /= parm.parm.capture.timeperframe.numerator; 549 } else { 550 551 /* don't hope more than 15 fps with USB 1.1 and 552 * image resolution >= 640x480 */ 553 if (gspca_dev->pixfmt.width >= 640 554 && gspca_dev->dev->speed == USB_SPEED_FULL) 555 bandwidth *= 15; /* 15 fps */ 556 else 557 bandwidth *= 30; /* 30 fps */ 558 } 559 560 gspca_dbg(gspca_dev, D_STREAM, "min bandwidth: %d\n", bandwidth); 561 return bandwidth; 562 } 563 564 /* endpoint table */ 565 #define MAX_ALT 16 566 struct ep_tb_s { 567 u32 alt; 568 u32 bandwidth; 569 }; 570 571 /* 572 * build the table of the endpoints 573 * and compute the minimum bandwidth for the image transfer 574 */ 575 static int build_isoc_ep_tb(struct gspca_dev *gspca_dev, 576 struct usb_interface *intf, 577 struct ep_tb_s *ep_tb) 578 { 579 struct usb_host_endpoint *ep; 580 int i, j, nbalt, psize, found; 581 u32 bandwidth, last_bw; 582 583 nbalt = intf->num_altsetting; 584 if (nbalt > MAX_ALT) 585 nbalt = MAX_ALT; /* fixme: should warn */ 586 587 /* build the endpoint table */ 588 i = 0; 589 last_bw = 0; 590 for (;;) { 591 ep_tb->bandwidth = 2000 * 2000 * 120; 592 found = 0; 593 for (j = 0; j < nbalt; j++) { 594 ep = alt_xfer(&intf->altsetting[j], 595 USB_ENDPOINT_XFER_ISOC, 596 gspca_dev->xfer_ep); 597 if (ep == NULL) 598 continue; 599 if (ep->desc.bInterval == 0) { 600 pr_err("alt %d iso endp with 0 interval\n", j); 601 continue; 602 } 603 psize = le16_to_cpu(ep->desc.wMaxPacketSize); 604 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3)); 605 bandwidth = psize * 1000; 606 if (gspca_dev->dev->speed == USB_SPEED_HIGH 607 || gspca_dev->dev->speed >= USB_SPEED_SUPER) 608 bandwidth *= 8; 609 bandwidth /= 1 << (ep->desc.bInterval - 1); 610 if (bandwidth <= last_bw) 611 continue; 612 if (bandwidth < ep_tb->bandwidth) { 613 ep_tb->bandwidth = bandwidth; 614 ep_tb->alt = j; 615 found = 1; 616 } 617 } 618 if (!found) 619 break; 620 gspca_dbg(gspca_dev, D_STREAM, "alt %d bandwidth %d\n", 621 ep_tb->alt, ep_tb->bandwidth); 622 last_bw = ep_tb->bandwidth; 623 i++; 624 ep_tb++; 625 } 626 627 /* 628 * If the camera: 629 * has a usb audio class interface (a built in usb mic); and 630 * is a usb 1 full speed device; and 631 * uses the max full speed iso bandwidth; and 632 * and has more than 1 alt setting 633 * then skip the highest alt setting to spare bandwidth for the mic 634 */ 635 if (gspca_dev->audio && 636 gspca_dev->dev->speed == USB_SPEED_FULL && 637 last_bw >= 1000000 && 638 i > 1) { 639 gspca_dbg(gspca_dev, D_STREAM, "dev has usb audio, skipping highest alt\n"); 640 i--; 641 ep_tb--; 642 } 643 644 /* get the requested bandwidth and start at the highest atlsetting */ 645 bandwidth = which_bandwidth(gspca_dev); 646 ep_tb--; 647 while (i > 1) { 648 ep_tb--; 649 if (ep_tb->bandwidth < bandwidth) 650 break; 651 i--; 652 } 653 return i; 654 } 655 656 /* 657 * create the URBs for image transfer 658 */ 659 static int create_urbs(struct gspca_dev *gspca_dev, 660 struct usb_host_endpoint *ep) 661 { 662 struct urb *urb; 663 int n, nurbs, i, psize, npkt, bsize; 664 665 /* calculate the packet size and the number of packets */ 666 psize = le16_to_cpu(ep->desc.wMaxPacketSize); 667 668 if (!gspca_dev->cam.bulk) { /* isoc */ 669 670 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */ 671 if (gspca_dev->pkt_size == 0) 672 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3)); 673 else 674 psize = gspca_dev->pkt_size; 675 npkt = gspca_dev->cam.npkt; 676 if (npkt == 0) 677 npkt = 32; /* default value */ 678 bsize = psize * npkt; 679 gspca_dbg(gspca_dev, D_STREAM, 680 "isoc %d pkts size %d = bsize:%d\n", 681 npkt, psize, bsize); 682 nurbs = DEF_NURBS; 683 } else { /* bulk */ 684 npkt = 0; 685 bsize = gspca_dev->cam.bulk_size; 686 if (bsize == 0) 687 bsize = psize; 688 gspca_dbg(gspca_dev, D_STREAM, "bulk bsize:%d\n", bsize); 689 if (gspca_dev->cam.bulk_nurbs != 0) 690 nurbs = gspca_dev->cam.bulk_nurbs; 691 else 692 nurbs = 1; 693 } 694 695 for (n = 0; n < nurbs; n++) { 696 urb = usb_alloc_urb(npkt, GFP_KERNEL); 697 if (!urb) 698 return -ENOMEM; 699 gspca_dev->urb[n] = urb; 700 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev, 701 bsize, 702 GFP_KERNEL, 703 &urb->transfer_dma); 704 705 if (urb->transfer_buffer == NULL) { 706 pr_err("usb_alloc_coherent failed\n"); 707 return -ENOMEM; 708 } 709 urb->dev = gspca_dev->dev; 710 urb->context = gspca_dev; 711 urb->transfer_buffer_length = bsize; 712 if (npkt != 0) { /* ISOC */ 713 urb->pipe = usb_rcvisocpipe(gspca_dev->dev, 714 ep->desc.bEndpointAddress); 715 urb->transfer_flags = URB_ISO_ASAP 716 | URB_NO_TRANSFER_DMA_MAP; 717 urb->interval = 1 << (ep->desc.bInterval - 1); 718 urb->complete = isoc_irq; 719 urb->number_of_packets = npkt; 720 for (i = 0; i < npkt; i++) { 721 urb->iso_frame_desc[i].length = psize; 722 urb->iso_frame_desc[i].offset = psize * i; 723 } 724 } else { /* bulk */ 725 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev, 726 ep->desc.bEndpointAddress); 727 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; 728 urb->complete = bulk_irq; 729 } 730 } 731 return 0; 732 } 733 734 /* Note: both the queue and the usb locks should be held when calling this */ 735 static void gspca_stream_off(struct gspca_dev *gspca_dev) 736 { 737 gspca_dev->streaming = false; 738 gspca_dev->usb_err = 0; 739 if (gspca_dev->sd_desc->stopN) 740 gspca_dev->sd_desc->stopN(gspca_dev); 741 destroy_urbs(gspca_dev); 742 gspca_input_destroy_urb(gspca_dev); 743 gspca_set_alt0(gspca_dev); 744 if (gspca_dev->present) 745 gspca_input_create_urb(gspca_dev); 746 if (gspca_dev->sd_desc->stop0) 747 gspca_dev->sd_desc->stop0(gspca_dev); 748 gspca_dbg(gspca_dev, D_STREAM, "stream off OK\n"); 749 } 750 751 /* 752 * start the USB transfer 753 */ 754 static int gspca_init_transfer(struct gspca_dev *gspca_dev) 755 { 756 struct usb_interface *intf; 757 struct usb_host_endpoint *ep; 758 struct urb *urb; 759 struct ep_tb_s ep_tb[MAX_ALT]; 760 int n, ret, xfer, alt, alt_idx; 761 762 /* reset the streaming variables */ 763 gspca_dev->image = NULL; 764 gspca_dev->image_len = 0; 765 gspca_dev->last_packet_type = DISCARD_PACKET; 766 767 gspca_dev->usb_err = 0; 768 769 /* do the specific subdriver stuff before endpoint selection */ 770 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface); 771 gspca_dev->alt = gspca_dev->cam.bulk ? intf->num_altsetting : 0; 772 if (gspca_dev->sd_desc->isoc_init) { 773 ret = gspca_dev->sd_desc->isoc_init(gspca_dev); 774 if (ret < 0) 775 return ret; 776 } 777 xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK 778 : USB_ENDPOINT_XFER_ISOC; 779 780 /* if bulk or the subdriver forced an altsetting, get the endpoint */ 781 if (gspca_dev->alt != 0) { 782 gspca_dev->alt--; /* (previous version compatibility) */ 783 ep = alt_xfer(&intf->altsetting[gspca_dev->alt], xfer, 784 gspca_dev->xfer_ep); 785 if (ep == NULL) { 786 pr_err("bad altsetting %d\n", gspca_dev->alt); 787 return -EIO; 788 } 789 ep_tb[0].alt = gspca_dev->alt; 790 alt_idx = 1; 791 } else { 792 /* else, compute the minimum bandwidth 793 * and build the endpoint table */ 794 alt_idx = build_isoc_ep_tb(gspca_dev, intf, ep_tb); 795 if (alt_idx <= 0) { 796 pr_err("no transfer endpoint found\n"); 797 return -EIO; 798 } 799 } 800 801 /* set the highest alternate setting and 802 * loop until urb submit succeeds */ 803 gspca_input_destroy_urb(gspca_dev); 804 805 gspca_dev->alt = ep_tb[--alt_idx].alt; 806 alt = -1; 807 for (;;) { 808 if (alt != gspca_dev->alt) { 809 alt = gspca_dev->alt; 810 if (intf->num_altsetting > 1) { 811 ret = usb_set_interface(gspca_dev->dev, 812 gspca_dev->iface, 813 alt); 814 if (ret < 0) { 815 if (ret == -ENOSPC) 816 goto retry; /*fixme: ugly*/ 817 pr_err("set alt %d err %d\n", alt, ret); 818 goto out; 819 } 820 } 821 } 822 if (!gspca_dev->cam.no_urb_create) { 823 gspca_dbg(gspca_dev, D_STREAM, "init transfer alt %d\n", 824 alt); 825 ret = create_urbs(gspca_dev, 826 alt_xfer(&intf->altsetting[alt], xfer, 827 gspca_dev->xfer_ep)); 828 if (ret < 0) { 829 destroy_urbs(gspca_dev); 830 goto out; 831 } 832 } 833 834 /* clear the bulk endpoint */ 835 if (gspca_dev->cam.bulk) 836 usb_clear_halt(gspca_dev->dev, 837 gspca_dev->urb[0]->pipe); 838 839 /* start the cam */ 840 ret = gspca_dev->sd_desc->start(gspca_dev); 841 if (ret < 0) { 842 destroy_urbs(gspca_dev); 843 goto out; 844 } 845 v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler); 846 gspca_dev->streaming = true; 847 848 /* some bulk transfers are started by the subdriver */ 849 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0) 850 break; 851 852 /* submit the URBs */ 853 for (n = 0; n < MAX_NURBS; n++) { 854 urb = gspca_dev->urb[n]; 855 if (urb == NULL) 856 break; 857 ret = usb_submit_urb(urb, GFP_KERNEL); 858 if (ret < 0) 859 break; 860 } 861 if (ret >= 0) 862 break; /* transfer is started */ 863 864 /* something when wrong 865 * stop the webcam and free the transfer resources */ 866 gspca_stream_off(gspca_dev); 867 if (ret != -ENOSPC) { 868 pr_err("usb_submit_urb alt %d err %d\n", 869 gspca_dev->alt, ret); 870 goto out; 871 } 872 873 /* the bandwidth is not wide enough 874 * negotiate or try a lower alternate setting */ 875 retry: 876 gspca_err(gspca_dev, "alt %d - bandwidth not wide enough, trying again\n", 877 alt); 878 msleep(20); /* wait for kill complete */ 879 if (gspca_dev->sd_desc->isoc_nego) { 880 ret = gspca_dev->sd_desc->isoc_nego(gspca_dev); 881 if (ret < 0) 882 goto out; 883 } else { 884 if (alt_idx <= 0) { 885 pr_err("no transfer endpoint found\n"); 886 ret = -EIO; 887 goto out; 888 } 889 gspca_dev->alt = ep_tb[--alt_idx].alt; 890 } 891 } 892 out: 893 gspca_input_create_urb(gspca_dev); 894 return ret; 895 } 896 897 static void gspca_set_default_mode(struct gspca_dev *gspca_dev) 898 { 899 int i; 900 901 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */ 902 gspca_dev->curr_mode = i; 903 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i]; 904 905 /* does nothing if ctrl_handler == NULL */ 906 v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler); 907 } 908 909 static int wxh_to_mode(struct gspca_dev *gspca_dev, 910 int width, int height, u32 pixelformat) 911 { 912 int i; 913 914 for (i = 0; i < gspca_dev->cam.nmodes; i++) { 915 if (width == gspca_dev->cam.cam_mode[i].width 916 && height == gspca_dev->cam.cam_mode[i].height 917 && pixelformat == gspca_dev->cam.cam_mode[i].pixelformat) 918 return i; 919 } 920 return -EINVAL; 921 } 922 923 static int wxh_to_nearest_mode(struct gspca_dev *gspca_dev, 924 int width, int height, u32 pixelformat) 925 { 926 int i; 927 928 for (i = gspca_dev->cam.nmodes; --i > 0; ) { 929 if (width >= gspca_dev->cam.cam_mode[i].width 930 && height >= gspca_dev->cam.cam_mode[i].height 931 && pixelformat == gspca_dev->cam.cam_mode[i].pixelformat) 932 return i; 933 } 934 for (i = gspca_dev->cam.nmodes; --i > 0; ) { 935 if (width >= gspca_dev->cam.cam_mode[i].width 936 && height >= gspca_dev->cam.cam_mode[i].height) 937 break; 938 } 939 return i; 940 } 941 942 /* 943 * search a mode with the right pixel format 944 */ 945 static int gspca_get_mode(struct gspca_dev *gspca_dev, 946 int mode, 947 int pixfmt) 948 { 949 int modeU, modeD; 950 951 modeU = modeD = mode; 952 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) { 953 if (--modeD >= 0) { 954 if (gspca_dev->cam.cam_mode[modeD].pixelformat 955 == pixfmt) 956 return modeD; 957 } 958 if (++modeU < gspca_dev->cam.nmodes) { 959 if (gspca_dev->cam.cam_mode[modeU].pixelformat 960 == pixfmt) 961 return modeU; 962 } 963 } 964 return -EINVAL; 965 } 966 967 #ifdef CONFIG_VIDEO_ADV_DEBUG 968 static int vidioc_g_chip_info(struct file *file, void *priv, 969 struct v4l2_dbg_chip_info *chip) 970 { 971 struct gspca_dev *gspca_dev = video_drvdata(file); 972 973 gspca_dev->usb_err = 0; 974 if (gspca_dev->sd_desc->get_chip_info) 975 return gspca_dev->sd_desc->get_chip_info(gspca_dev, chip); 976 return chip->match.addr ? -EINVAL : 0; 977 } 978 979 static int vidioc_g_register(struct file *file, void *priv, 980 struct v4l2_dbg_register *reg) 981 { 982 struct gspca_dev *gspca_dev = video_drvdata(file); 983 984 gspca_dev->usb_err = 0; 985 return gspca_dev->sd_desc->get_register(gspca_dev, reg); 986 } 987 988 static int vidioc_s_register(struct file *file, void *priv, 989 const struct v4l2_dbg_register *reg) 990 { 991 struct gspca_dev *gspca_dev = video_drvdata(file); 992 993 gspca_dev->usb_err = 0; 994 return gspca_dev->sd_desc->set_register(gspca_dev, reg); 995 } 996 #endif 997 998 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, 999 struct v4l2_fmtdesc *fmtdesc) 1000 { 1001 struct gspca_dev *gspca_dev = video_drvdata(file); 1002 int i, j, index; 1003 __u32 fmt_tb[8]; 1004 1005 /* give an index to each format */ 1006 index = 0; 1007 for (i = gspca_dev->cam.nmodes; --i >= 0; ) { 1008 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat; 1009 j = 0; 1010 for (;;) { 1011 if (fmt_tb[j] == fmt_tb[index]) 1012 break; 1013 j++; 1014 } 1015 if (j == index) { 1016 if (fmtdesc->index == index) 1017 break; /* new format */ 1018 index++; 1019 if (index >= ARRAY_SIZE(fmt_tb)) 1020 return -EINVAL; 1021 } 1022 } 1023 if (i < 0) 1024 return -EINVAL; /* no more format */ 1025 1026 fmtdesc->pixelformat = fmt_tb[index]; 1027 if (gspca_dev->cam.cam_mode[i].sizeimage < 1028 gspca_dev->cam.cam_mode[i].width * 1029 gspca_dev->cam.cam_mode[i].height) 1030 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED; 1031 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff; 1032 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff; 1033 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff; 1034 fmtdesc->description[3] = fmtdesc->pixelformat >> 24; 1035 fmtdesc->description[4] = '\0'; 1036 return 0; 1037 } 1038 1039 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv, 1040 struct v4l2_format *fmt) 1041 { 1042 struct gspca_dev *gspca_dev = video_drvdata(file); 1043 1044 fmt->fmt.pix = gspca_dev->pixfmt; 1045 /* some drivers use priv internally, zero it before giving it back to 1046 the core */ 1047 fmt->fmt.pix.priv = 0; 1048 return 0; 1049 } 1050 1051 static int try_fmt_vid_cap(struct gspca_dev *gspca_dev, 1052 struct v4l2_format *fmt) 1053 { 1054 int w, h, mode, mode2; 1055 1056 w = fmt->fmt.pix.width; 1057 h = fmt->fmt.pix.height; 1058 1059 PDEBUG_MODE(gspca_dev, D_CONF, "try fmt cap", 1060 fmt->fmt.pix.pixelformat, w, h); 1061 1062 /* search the nearest mode for width and height */ 1063 mode = wxh_to_nearest_mode(gspca_dev, w, h, fmt->fmt.pix.pixelformat); 1064 1065 /* OK if right palette */ 1066 if (gspca_dev->cam.cam_mode[mode].pixelformat 1067 != fmt->fmt.pix.pixelformat) { 1068 1069 /* else, search the closest mode with the same pixel format */ 1070 mode2 = gspca_get_mode(gspca_dev, mode, 1071 fmt->fmt.pix.pixelformat); 1072 if (mode2 >= 0) 1073 mode = mode2; 1074 } 1075 fmt->fmt.pix = gspca_dev->cam.cam_mode[mode]; 1076 if (gspca_dev->sd_desc->try_fmt) { 1077 /* pass original resolution to subdriver try_fmt */ 1078 fmt->fmt.pix.width = w; 1079 fmt->fmt.pix.height = h; 1080 gspca_dev->sd_desc->try_fmt(gspca_dev, fmt); 1081 } 1082 /* some drivers use priv internally, zero it before giving it back to 1083 the core */ 1084 fmt->fmt.pix.priv = 0; 1085 return mode; /* used when s_fmt */ 1086 } 1087 1088 static int vidioc_try_fmt_vid_cap(struct file *file, 1089 void *priv, 1090 struct v4l2_format *fmt) 1091 { 1092 struct gspca_dev *gspca_dev = video_drvdata(file); 1093 1094 if (try_fmt_vid_cap(gspca_dev, fmt) < 0) 1095 return -EINVAL; 1096 return 0; 1097 } 1098 1099 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, 1100 struct v4l2_format *fmt) 1101 { 1102 struct gspca_dev *gspca_dev = video_drvdata(file); 1103 int mode; 1104 1105 if (vb2_is_busy(&gspca_dev->queue)) 1106 return -EBUSY; 1107 1108 mode = try_fmt_vid_cap(gspca_dev, fmt); 1109 if (mode < 0) 1110 return -EINVAL; 1111 1112 gspca_dev->curr_mode = mode; 1113 if (gspca_dev->sd_desc->try_fmt) 1114 /* subdriver try_fmt can modify format parameters */ 1115 gspca_dev->pixfmt = fmt->fmt.pix; 1116 else 1117 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[mode]; 1118 return 0; 1119 } 1120 1121 static int vidioc_enum_framesizes(struct file *file, void *priv, 1122 struct v4l2_frmsizeenum *fsize) 1123 { 1124 struct gspca_dev *gspca_dev = video_drvdata(file); 1125 int i; 1126 __u32 index = 0; 1127 1128 if (gspca_dev->sd_desc->enum_framesizes) 1129 return gspca_dev->sd_desc->enum_framesizes(gspca_dev, fsize); 1130 1131 for (i = 0; i < gspca_dev->cam.nmodes; i++) { 1132 if (fsize->pixel_format != 1133 gspca_dev->cam.cam_mode[i].pixelformat) 1134 continue; 1135 1136 if (fsize->index == index) { 1137 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE; 1138 fsize->discrete.width = 1139 gspca_dev->cam.cam_mode[i].width; 1140 fsize->discrete.height = 1141 gspca_dev->cam.cam_mode[i].height; 1142 return 0; 1143 } 1144 index++; 1145 } 1146 1147 return -EINVAL; 1148 } 1149 1150 static int vidioc_enum_frameintervals(struct file *filp, void *priv, 1151 struct v4l2_frmivalenum *fival) 1152 { 1153 struct gspca_dev *gspca_dev = video_drvdata(filp); 1154 int mode; 1155 __u32 i; 1156 1157 mode = wxh_to_mode(gspca_dev, fival->width, fival->height, 1158 fival->pixel_format); 1159 if (mode < 0) 1160 return -EINVAL; 1161 1162 if (gspca_dev->cam.mode_framerates == NULL || 1163 gspca_dev->cam.mode_framerates[mode].nrates == 0) 1164 return -EINVAL; 1165 1166 if (fival->pixel_format != 1167 gspca_dev->cam.cam_mode[mode].pixelformat) 1168 return -EINVAL; 1169 1170 for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) { 1171 if (fival->index == i) { 1172 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE; 1173 fival->discrete.numerator = 1; 1174 fival->discrete.denominator = 1175 gspca_dev->cam.mode_framerates[mode].rates[i]; 1176 return 0; 1177 } 1178 } 1179 1180 return -EINVAL; 1181 } 1182 1183 static void gspca_release(struct v4l2_device *v4l2_device) 1184 { 1185 struct gspca_dev *gspca_dev = 1186 container_of(v4l2_device, struct gspca_dev, v4l2_dev); 1187 1188 v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler); 1189 v4l2_device_unregister(&gspca_dev->v4l2_dev); 1190 kfree(gspca_dev->usb_buf); 1191 kfree(gspca_dev); 1192 } 1193 1194 static int vidioc_querycap(struct file *file, void *priv, 1195 struct v4l2_capability *cap) 1196 { 1197 struct gspca_dev *gspca_dev = video_drvdata(file); 1198 1199 strscpy((char *)cap->driver, gspca_dev->sd_desc->name, 1200 sizeof(cap->driver)); 1201 if (gspca_dev->dev->product != NULL) { 1202 strscpy((char *)cap->card, gspca_dev->dev->product, 1203 sizeof(cap->card)); 1204 } else { 1205 snprintf((char *) cap->card, sizeof cap->card, 1206 "USB Camera (%04x:%04x)", 1207 le16_to_cpu(gspca_dev->dev->descriptor.idVendor), 1208 le16_to_cpu(gspca_dev->dev->descriptor.idProduct)); 1209 } 1210 usb_make_path(gspca_dev->dev, (char *) cap->bus_info, 1211 sizeof(cap->bus_info)); 1212 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE 1213 | V4L2_CAP_STREAMING 1214 | V4L2_CAP_READWRITE; 1215 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; 1216 return 0; 1217 } 1218 1219 static int vidioc_enum_input(struct file *file, void *priv, 1220 struct v4l2_input *input) 1221 { 1222 struct gspca_dev *gspca_dev = video_drvdata(file); 1223 1224 if (input->index != 0) 1225 return -EINVAL; 1226 input->type = V4L2_INPUT_TYPE_CAMERA; 1227 input->status = gspca_dev->cam.input_flags; 1228 strscpy(input->name, gspca_dev->sd_desc->name, 1229 sizeof input->name); 1230 return 0; 1231 } 1232 1233 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i) 1234 { 1235 *i = 0; 1236 return 0; 1237 } 1238 1239 static int vidioc_s_input(struct file *file, void *priv, unsigned int i) 1240 { 1241 if (i > 0) 1242 return -EINVAL; 1243 return 0; 1244 } 1245 1246 static int vidioc_g_jpegcomp(struct file *file, void *priv, 1247 struct v4l2_jpegcompression *jpegcomp) 1248 { 1249 struct gspca_dev *gspca_dev = video_drvdata(file); 1250 1251 gspca_dev->usb_err = 0; 1252 return gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp); 1253 } 1254 1255 static int vidioc_s_jpegcomp(struct file *file, void *priv, 1256 const struct v4l2_jpegcompression *jpegcomp) 1257 { 1258 struct gspca_dev *gspca_dev = video_drvdata(file); 1259 1260 gspca_dev->usb_err = 0; 1261 return gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp); 1262 } 1263 1264 static int vidioc_g_parm(struct file *filp, void *priv, 1265 struct v4l2_streamparm *parm) 1266 { 1267 struct gspca_dev *gspca_dev = video_drvdata(filp); 1268 1269 parm->parm.capture.readbuffers = gspca_dev->queue.min_buffers_needed; 1270 1271 if (!gspca_dev->sd_desc->get_streamparm) 1272 return 0; 1273 1274 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME; 1275 gspca_dev->usb_err = 0; 1276 gspca_dev->sd_desc->get_streamparm(gspca_dev, parm); 1277 return gspca_dev->usb_err; 1278 } 1279 1280 static int vidioc_s_parm(struct file *filp, void *priv, 1281 struct v4l2_streamparm *parm) 1282 { 1283 struct gspca_dev *gspca_dev = video_drvdata(filp); 1284 1285 parm->parm.capture.readbuffers = gspca_dev->queue.min_buffers_needed; 1286 1287 if (!gspca_dev->sd_desc->set_streamparm) { 1288 parm->parm.capture.capability = 0; 1289 return 0; 1290 } 1291 1292 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME; 1293 gspca_dev->usb_err = 0; 1294 gspca_dev->sd_desc->set_streamparm(gspca_dev, parm); 1295 return gspca_dev->usb_err; 1296 } 1297 1298 static int gspca_queue_setup(struct vb2_queue *vq, 1299 unsigned int *nbuffers, unsigned int *nplanes, 1300 unsigned int sizes[], struct device *alloc_devs[]) 1301 { 1302 struct gspca_dev *gspca_dev = vb2_get_drv_priv(vq); 1303 unsigned int size = PAGE_ALIGN(gspca_dev->pixfmt.sizeimage); 1304 1305 if (*nplanes) 1306 return sizes[0] < size ? -EINVAL : 0; 1307 *nplanes = 1; 1308 sizes[0] = size; 1309 return 0; 1310 } 1311 1312 static int gspca_buffer_prepare(struct vb2_buffer *vb) 1313 { 1314 struct gspca_dev *gspca_dev = vb2_get_drv_priv(vb->vb2_queue); 1315 unsigned long size = PAGE_ALIGN(gspca_dev->pixfmt.sizeimage); 1316 1317 if (vb2_plane_size(vb, 0) < size) { 1318 gspca_err(gspca_dev, "buffer too small (%lu < %lu)\n", 1319 vb2_plane_size(vb, 0), size); 1320 return -EINVAL; 1321 } 1322 return 0; 1323 } 1324 1325 static void gspca_buffer_finish(struct vb2_buffer *vb) 1326 { 1327 struct gspca_dev *gspca_dev = vb2_get_drv_priv(vb->vb2_queue); 1328 1329 if (!gspca_dev->sd_desc->dq_callback) 1330 return; 1331 1332 gspca_dev->usb_err = 0; 1333 if (gspca_dev->present) 1334 gspca_dev->sd_desc->dq_callback(gspca_dev); 1335 } 1336 1337 static void gspca_buffer_queue(struct vb2_buffer *vb) 1338 { 1339 struct gspca_dev *gspca_dev = vb2_get_drv_priv(vb->vb2_queue); 1340 struct gspca_buffer *buf = to_gspca_buffer(vb); 1341 unsigned long flags; 1342 1343 spin_lock_irqsave(&gspca_dev->qlock, flags); 1344 list_add_tail(&buf->list, &gspca_dev->buf_list); 1345 spin_unlock_irqrestore(&gspca_dev->qlock, flags); 1346 } 1347 1348 static void gspca_return_all_buffers(struct gspca_dev *gspca_dev, 1349 enum vb2_buffer_state state) 1350 { 1351 struct gspca_buffer *buf, *node; 1352 unsigned long flags; 1353 1354 spin_lock_irqsave(&gspca_dev->qlock, flags); 1355 list_for_each_entry_safe(buf, node, &gspca_dev->buf_list, list) { 1356 vb2_buffer_done(&buf->vb.vb2_buf, state); 1357 list_del(&buf->list); 1358 } 1359 spin_unlock_irqrestore(&gspca_dev->qlock, flags); 1360 } 1361 1362 static int gspca_start_streaming(struct vb2_queue *vq, unsigned int count) 1363 { 1364 struct gspca_dev *gspca_dev = vb2_get_drv_priv(vq); 1365 int ret; 1366 1367 gspca_dev->sequence = 0; 1368 1369 ret = gspca_init_transfer(gspca_dev); 1370 if (ret) 1371 gspca_return_all_buffers(gspca_dev, VB2_BUF_STATE_QUEUED); 1372 return ret; 1373 } 1374 1375 static void gspca_stop_streaming(struct vb2_queue *vq) 1376 { 1377 struct gspca_dev *gspca_dev = vb2_get_drv_priv(vq); 1378 1379 gspca_stream_off(gspca_dev); 1380 1381 /* Release all active buffers */ 1382 gspca_return_all_buffers(gspca_dev, VB2_BUF_STATE_ERROR); 1383 } 1384 1385 static const struct vb2_ops gspca_qops = { 1386 .queue_setup = gspca_queue_setup, 1387 .buf_prepare = gspca_buffer_prepare, 1388 .buf_finish = gspca_buffer_finish, 1389 .buf_queue = gspca_buffer_queue, 1390 .start_streaming = gspca_start_streaming, 1391 .stop_streaming = gspca_stop_streaming, 1392 .wait_prepare = vb2_ops_wait_prepare, 1393 .wait_finish = vb2_ops_wait_finish, 1394 }; 1395 1396 static const struct v4l2_file_operations dev_fops = { 1397 .owner = THIS_MODULE, 1398 .open = v4l2_fh_open, 1399 .release = vb2_fop_release, 1400 .unlocked_ioctl = video_ioctl2, 1401 .read = vb2_fop_read, 1402 .mmap = vb2_fop_mmap, 1403 .poll = vb2_fop_poll, 1404 }; 1405 1406 static const struct v4l2_ioctl_ops dev_ioctl_ops = { 1407 .vidioc_querycap = vidioc_querycap, 1408 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, 1409 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, 1410 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, 1411 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, 1412 .vidioc_enum_input = vidioc_enum_input, 1413 .vidioc_g_input = vidioc_g_input, 1414 .vidioc_s_input = vidioc_s_input, 1415 .vidioc_g_jpegcomp = vidioc_g_jpegcomp, 1416 .vidioc_s_jpegcomp = vidioc_s_jpegcomp, 1417 .vidioc_g_parm = vidioc_g_parm, 1418 .vidioc_s_parm = vidioc_s_parm, 1419 .vidioc_enum_framesizes = vidioc_enum_framesizes, 1420 .vidioc_enum_frameintervals = vidioc_enum_frameintervals, 1421 1422 .vidioc_reqbufs = vb2_ioctl_reqbufs, 1423 .vidioc_create_bufs = vb2_ioctl_create_bufs, 1424 .vidioc_querybuf = vb2_ioctl_querybuf, 1425 .vidioc_qbuf = vb2_ioctl_qbuf, 1426 .vidioc_dqbuf = vb2_ioctl_dqbuf, 1427 .vidioc_expbuf = vb2_ioctl_expbuf, 1428 .vidioc_streamon = vb2_ioctl_streamon, 1429 .vidioc_streamoff = vb2_ioctl_streamoff, 1430 1431 #ifdef CONFIG_VIDEO_ADV_DEBUG 1432 .vidioc_g_chip_info = vidioc_g_chip_info, 1433 .vidioc_g_register = vidioc_g_register, 1434 .vidioc_s_register = vidioc_s_register, 1435 #endif 1436 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 1437 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 1438 }; 1439 1440 static const struct video_device gspca_template = { 1441 .name = "gspca main driver", 1442 .fops = &dev_fops, 1443 .ioctl_ops = &dev_ioctl_ops, 1444 .release = video_device_release_empty, /* We use v4l2_dev.release */ 1445 }; 1446 1447 /* 1448 * probe and create a new gspca device 1449 * 1450 * This function must be called by the sub-driver when it is 1451 * called for probing a new device. 1452 */ 1453 int gspca_dev_probe2(struct usb_interface *intf, 1454 const struct usb_device_id *id, 1455 const struct sd_desc *sd_desc, 1456 int dev_size, 1457 struct module *module) 1458 { 1459 struct gspca_dev *gspca_dev; 1460 struct usb_device *dev = interface_to_usbdev(intf); 1461 struct vb2_queue *q; 1462 int ret; 1463 1464 pr_info("%s-" GSPCA_VERSION " probing %04x:%04x\n", 1465 sd_desc->name, id->idVendor, id->idProduct); 1466 1467 /* create the device */ 1468 if (dev_size < sizeof *gspca_dev) 1469 dev_size = sizeof *gspca_dev; 1470 gspca_dev = kzalloc(dev_size, GFP_KERNEL); 1471 if (!gspca_dev) { 1472 pr_err("couldn't kzalloc gspca struct\n"); 1473 return -ENOMEM; 1474 } 1475 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL); 1476 if (!gspca_dev->usb_buf) { 1477 pr_err("out of memory\n"); 1478 ret = -ENOMEM; 1479 goto out; 1480 } 1481 gspca_dev->dev = dev; 1482 gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber; 1483 gspca_dev->xfer_ep = -1; 1484 1485 /* check if any audio device */ 1486 if (dev->actconfig->desc.bNumInterfaces != 1) { 1487 int i; 1488 struct usb_interface *intf2; 1489 1490 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) { 1491 intf2 = dev->actconfig->interface[i]; 1492 if (intf2 != NULL 1493 && intf2->altsetting != NULL 1494 && intf2->altsetting->desc.bInterfaceClass == 1495 USB_CLASS_AUDIO) { 1496 gspca_dev->audio = 1; 1497 break; 1498 } 1499 } 1500 } 1501 1502 gspca_dev->v4l2_dev.release = gspca_release; 1503 ret = v4l2_device_register(&intf->dev, &gspca_dev->v4l2_dev); 1504 if (ret) 1505 goto out; 1506 gspca_dev->present = true; 1507 gspca_dev->sd_desc = sd_desc; 1508 gspca_dev->empty_packet = -1; /* don't check the empty packets */ 1509 gspca_dev->vdev = gspca_template; 1510 gspca_dev->vdev.v4l2_dev = &gspca_dev->v4l2_dev; 1511 video_set_drvdata(&gspca_dev->vdev, gspca_dev); 1512 gspca_dev->module = module; 1513 1514 mutex_init(&gspca_dev->usb_lock); 1515 gspca_dev->vdev.lock = &gspca_dev->usb_lock; 1516 init_waitqueue_head(&gspca_dev->wq); 1517 1518 /* Initialize the vb2 queue */ 1519 q = &gspca_dev->queue; 1520 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 1521 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ; 1522 q->drv_priv = gspca_dev; 1523 q->buf_struct_size = sizeof(struct gspca_buffer); 1524 q->ops = &gspca_qops; 1525 q->mem_ops = &vb2_vmalloc_memops; 1526 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1527 q->min_buffers_needed = 2; 1528 q->lock = &gspca_dev->usb_lock; 1529 ret = vb2_queue_init(q); 1530 if (ret) 1531 goto out; 1532 gspca_dev->vdev.queue = q; 1533 1534 INIT_LIST_HEAD(&gspca_dev->buf_list); 1535 spin_lock_init(&gspca_dev->qlock); 1536 1537 /* configure the subdriver and initialize the USB device */ 1538 ret = sd_desc->config(gspca_dev, id); 1539 if (ret < 0) 1540 goto out; 1541 ret = sd_desc->init(gspca_dev); 1542 if (ret < 0) 1543 goto out; 1544 if (sd_desc->init_controls) 1545 ret = sd_desc->init_controls(gspca_dev); 1546 if (ret < 0) 1547 goto out; 1548 gspca_set_default_mode(gspca_dev); 1549 1550 ret = gspca_input_connect(gspca_dev); 1551 if (ret) 1552 goto out; 1553 1554 #ifdef CONFIG_VIDEO_ADV_DEBUG 1555 if (!gspca_dev->sd_desc->get_register) 1556 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_G_REGISTER); 1557 if (!gspca_dev->sd_desc->set_register) 1558 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_S_REGISTER); 1559 #endif 1560 if (!gspca_dev->sd_desc->get_jcomp) 1561 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_G_JPEGCOMP); 1562 if (!gspca_dev->sd_desc->set_jcomp) 1563 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_S_JPEGCOMP); 1564 1565 /* init video stuff */ 1566 ret = video_register_device(&gspca_dev->vdev, 1567 VFL_TYPE_GRABBER, 1568 -1); 1569 if (ret < 0) { 1570 pr_err("video_register_device err %d\n", ret); 1571 goto out; 1572 } 1573 1574 usb_set_intfdata(intf, gspca_dev); 1575 gspca_dbg(gspca_dev, D_PROBE, "%s created\n", 1576 video_device_node_name(&gspca_dev->vdev)); 1577 1578 gspca_input_create_urb(gspca_dev); 1579 1580 return 0; 1581 out: 1582 #if IS_ENABLED(CONFIG_INPUT) 1583 if (gspca_dev->input_dev) 1584 input_unregister_device(gspca_dev->input_dev); 1585 #endif 1586 v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler); 1587 kfree(gspca_dev->usb_buf); 1588 kfree(gspca_dev); 1589 return ret; 1590 } 1591 EXPORT_SYMBOL(gspca_dev_probe2); 1592 1593 /* same function as the previous one, but check the interface */ 1594 int gspca_dev_probe(struct usb_interface *intf, 1595 const struct usb_device_id *id, 1596 const struct sd_desc *sd_desc, 1597 int dev_size, 1598 struct module *module) 1599 { 1600 struct usb_device *dev = interface_to_usbdev(intf); 1601 1602 /* we don't handle multi-config cameras */ 1603 if (dev->descriptor.bNumConfigurations != 1) { 1604 pr_err("%04x:%04x too many config\n", 1605 id->idVendor, id->idProduct); 1606 return -ENODEV; 1607 } 1608 1609 /* the USB video interface must be the first one */ 1610 if (dev->actconfig->desc.bNumInterfaces != 1 1611 && intf->cur_altsetting->desc.bInterfaceNumber != 0) 1612 return -ENODEV; 1613 1614 return gspca_dev_probe2(intf, id, sd_desc, dev_size, module); 1615 } 1616 EXPORT_SYMBOL(gspca_dev_probe); 1617 1618 /* 1619 * USB disconnection 1620 * 1621 * This function must be called by the sub-driver 1622 * when the device disconnects, after the specific resources are freed. 1623 */ 1624 void gspca_disconnect(struct usb_interface *intf) 1625 { 1626 struct gspca_dev *gspca_dev = usb_get_intfdata(intf); 1627 #if IS_ENABLED(CONFIG_INPUT) 1628 struct input_dev *input_dev; 1629 #endif 1630 1631 gspca_dbg(gspca_dev, D_PROBE, "%s disconnect\n", 1632 video_device_node_name(&gspca_dev->vdev)); 1633 1634 mutex_lock(&gspca_dev->usb_lock); 1635 gspca_dev->present = false; 1636 destroy_urbs(gspca_dev); 1637 gspca_input_destroy_urb(gspca_dev); 1638 1639 vb2_queue_error(&gspca_dev->queue); 1640 1641 #if IS_ENABLED(CONFIG_INPUT) 1642 input_dev = gspca_dev->input_dev; 1643 if (input_dev) { 1644 gspca_dev->input_dev = NULL; 1645 input_unregister_device(input_dev); 1646 } 1647 #endif 1648 1649 v4l2_device_disconnect(&gspca_dev->v4l2_dev); 1650 video_unregister_device(&gspca_dev->vdev); 1651 1652 mutex_unlock(&gspca_dev->usb_lock); 1653 1654 /* (this will call gspca_release() immediately or on last close) */ 1655 v4l2_device_put(&gspca_dev->v4l2_dev); 1656 } 1657 EXPORT_SYMBOL(gspca_disconnect); 1658 1659 #ifdef CONFIG_PM 1660 int gspca_suspend(struct usb_interface *intf, pm_message_t message) 1661 { 1662 struct gspca_dev *gspca_dev = usb_get_intfdata(intf); 1663 1664 gspca_input_destroy_urb(gspca_dev); 1665 1666 if (!vb2_start_streaming_called(&gspca_dev->queue)) 1667 return 0; 1668 1669 mutex_lock(&gspca_dev->usb_lock); 1670 gspca_dev->frozen = 1; /* avoid urb error messages */ 1671 gspca_dev->usb_err = 0; 1672 if (gspca_dev->sd_desc->stopN) 1673 gspca_dev->sd_desc->stopN(gspca_dev); 1674 destroy_urbs(gspca_dev); 1675 gspca_set_alt0(gspca_dev); 1676 if (gspca_dev->sd_desc->stop0) 1677 gspca_dev->sd_desc->stop0(gspca_dev); 1678 mutex_unlock(&gspca_dev->usb_lock); 1679 1680 return 0; 1681 } 1682 EXPORT_SYMBOL(gspca_suspend); 1683 1684 int gspca_resume(struct usb_interface *intf) 1685 { 1686 struct gspca_dev *gspca_dev = usb_get_intfdata(intf); 1687 int streaming, ret = 0; 1688 1689 mutex_lock(&gspca_dev->usb_lock); 1690 gspca_dev->frozen = 0; 1691 gspca_dev->usb_err = 0; 1692 gspca_dev->sd_desc->init(gspca_dev); 1693 /* 1694 * Most subdrivers send all ctrl values on sd_start and thus 1695 * only write to the device registers on s_ctrl when streaming -> 1696 * Clear streaming to avoid setting all ctrls twice. 1697 */ 1698 streaming = vb2_start_streaming_called(&gspca_dev->queue); 1699 if (streaming) 1700 ret = gspca_init_transfer(gspca_dev); 1701 else 1702 gspca_input_create_urb(gspca_dev); 1703 mutex_unlock(&gspca_dev->usb_lock); 1704 1705 return ret; 1706 } 1707 EXPORT_SYMBOL(gspca_resume); 1708 #endif 1709 1710 /* -- module insert / remove -- */ 1711 static int __init gspca_init(void) 1712 { 1713 pr_info("v" GSPCA_VERSION " registered\n"); 1714 return 0; 1715 } 1716 static void __exit gspca_exit(void) 1717 { 1718 } 1719 1720 module_init(gspca_init); 1721 module_exit(gspca_exit); 1722 1723 module_param_named(debug, gspca_debug, int, 0644); 1724 MODULE_PARM_DESC(debug, 1725 "1:probe 2:config 3:stream 4:frame 5:packet 6:usbi 7:usbo"); 1726