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 return 0; 1213 } 1214 1215 static int vidioc_enum_input(struct file *file, void *priv, 1216 struct v4l2_input *input) 1217 { 1218 struct gspca_dev *gspca_dev = video_drvdata(file); 1219 1220 if (input->index != 0) 1221 return -EINVAL; 1222 input->type = V4L2_INPUT_TYPE_CAMERA; 1223 input->status = gspca_dev->cam.input_flags; 1224 strscpy(input->name, gspca_dev->sd_desc->name, 1225 sizeof input->name); 1226 return 0; 1227 } 1228 1229 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i) 1230 { 1231 *i = 0; 1232 return 0; 1233 } 1234 1235 static int vidioc_s_input(struct file *file, void *priv, unsigned int i) 1236 { 1237 if (i > 0) 1238 return -EINVAL; 1239 return 0; 1240 } 1241 1242 static int vidioc_g_jpegcomp(struct file *file, void *priv, 1243 struct v4l2_jpegcompression *jpegcomp) 1244 { 1245 struct gspca_dev *gspca_dev = video_drvdata(file); 1246 1247 gspca_dev->usb_err = 0; 1248 return gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp); 1249 } 1250 1251 static int vidioc_s_jpegcomp(struct file *file, void *priv, 1252 const struct v4l2_jpegcompression *jpegcomp) 1253 { 1254 struct gspca_dev *gspca_dev = video_drvdata(file); 1255 1256 gspca_dev->usb_err = 0; 1257 return gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp); 1258 } 1259 1260 static int vidioc_g_parm(struct file *filp, void *priv, 1261 struct v4l2_streamparm *parm) 1262 { 1263 struct gspca_dev *gspca_dev = video_drvdata(filp); 1264 1265 parm->parm.capture.readbuffers = gspca_dev->queue.min_buffers_needed; 1266 1267 if (!gspca_dev->sd_desc->get_streamparm) 1268 return 0; 1269 1270 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME; 1271 gspca_dev->usb_err = 0; 1272 gspca_dev->sd_desc->get_streamparm(gspca_dev, parm); 1273 return gspca_dev->usb_err; 1274 } 1275 1276 static int vidioc_s_parm(struct file *filp, void *priv, 1277 struct v4l2_streamparm *parm) 1278 { 1279 struct gspca_dev *gspca_dev = video_drvdata(filp); 1280 1281 parm->parm.capture.readbuffers = gspca_dev->queue.min_buffers_needed; 1282 1283 if (!gspca_dev->sd_desc->set_streamparm) { 1284 parm->parm.capture.capability = 0; 1285 return 0; 1286 } 1287 1288 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME; 1289 gspca_dev->usb_err = 0; 1290 gspca_dev->sd_desc->set_streamparm(gspca_dev, parm); 1291 return gspca_dev->usb_err; 1292 } 1293 1294 static int gspca_queue_setup(struct vb2_queue *vq, 1295 unsigned int *nbuffers, unsigned int *nplanes, 1296 unsigned int sizes[], struct device *alloc_devs[]) 1297 { 1298 struct gspca_dev *gspca_dev = vb2_get_drv_priv(vq); 1299 unsigned int size = PAGE_ALIGN(gspca_dev->pixfmt.sizeimage); 1300 1301 if (*nplanes) 1302 return sizes[0] < size ? -EINVAL : 0; 1303 *nplanes = 1; 1304 sizes[0] = size; 1305 return 0; 1306 } 1307 1308 static int gspca_buffer_prepare(struct vb2_buffer *vb) 1309 { 1310 struct gspca_dev *gspca_dev = vb2_get_drv_priv(vb->vb2_queue); 1311 unsigned long size = PAGE_ALIGN(gspca_dev->pixfmt.sizeimage); 1312 1313 if (vb2_plane_size(vb, 0) < size) { 1314 gspca_err(gspca_dev, "buffer too small (%lu < %lu)\n", 1315 vb2_plane_size(vb, 0), size); 1316 return -EINVAL; 1317 } 1318 return 0; 1319 } 1320 1321 static void gspca_buffer_finish(struct vb2_buffer *vb) 1322 { 1323 struct gspca_dev *gspca_dev = vb2_get_drv_priv(vb->vb2_queue); 1324 1325 if (!gspca_dev->sd_desc->dq_callback) 1326 return; 1327 1328 gspca_dev->usb_err = 0; 1329 if (gspca_dev->present) 1330 gspca_dev->sd_desc->dq_callback(gspca_dev); 1331 } 1332 1333 static void gspca_buffer_queue(struct vb2_buffer *vb) 1334 { 1335 struct gspca_dev *gspca_dev = vb2_get_drv_priv(vb->vb2_queue); 1336 struct gspca_buffer *buf = to_gspca_buffer(vb); 1337 unsigned long flags; 1338 1339 spin_lock_irqsave(&gspca_dev->qlock, flags); 1340 list_add_tail(&buf->list, &gspca_dev->buf_list); 1341 spin_unlock_irqrestore(&gspca_dev->qlock, flags); 1342 } 1343 1344 static void gspca_return_all_buffers(struct gspca_dev *gspca_dev, 1345 enum vb2_buffer_state state) 1346 { 1347 struct gspca_buffer *buf, *node; 1348 unsigned long flags; 1349 1350 spin_lock_irqsave(&gspca_dev->qlock, flags); 1351 list_for_each_entry_safe(buf, node, &gspca_dev->buf_list, list) { 1352 vb2_buffer_done(&buf->vb.vb2_buf, state); 1353 list_del(&buf->list); 1354 } 1355 spin_unlock_irqrestore(&gspca_dev->qlock, flags); 1356 } 1357 1358 static int gspca_start_streaming(struct vb2_queue *vq, unsigned int count) 1359 { 1360 struct gspca_dev *gspca_dev = vb2_get_drv_priv(vq); 1361 int ret; 1362 1363 gspca_dev->sequence = 0; 1364 1365 ret = gspca_init_transfer(gspca_dev); 1366 if (ret) 1367 gspca_return_all_buffers(gspca_dev, VB2_BUF_STATE_QUEUED); 1368 return ret; 1369 } 1370 1371 static void gspca_stop_streaming(struct vb2_queue *vq) 1372 { 1373 struct gspca_dev *gspca_dev = vb2_get_drv_priv(vq); 1374 1375 gspca_stream_off(gspca_dev); 1376 1377 /* Release all active buffers */ 1378 gspca_return_all_buffers(gspca_dev, VB2_BUF_STATE_ERROR); 1379 } 1380 1381 static const struct vb2_ops gspca_qops = { 1382 .queue_setup = gspca_queue_setup, 1383 .buf_prepare = gspca_buffer_prepare, 1384 .buf_finish = gspca_buffer_finish, 1385 .buf_queue = gspca_buffer_queue, 1386 .start_streaming = gspca_start_streaming, 1387 .stop_streaming = gspca_stop_streaming, 1388 .wait_prepare = vb2_ops_wait_prepare, 1389 .wait_finish = vb2_ops_wait_finish, 1390 }; 1391 1392 static const struct v4l2_file_operations dev_fops = { 1393 .owner = THIS_MODULE, 1394 .open = v4l2_fh_open, 1395 .release = vb2_fop_release, 1396 .unlocked_ioctl = video_ioctl2, 1397 .read = vb2_fop_read, 1398 .mmap = vb2_fop_mmap, 1399 .poll = vb2_fop_poll, 1400 }; 1401 1402 static const struct v4l2_ioctl_ops dev_ioctl_ops = { 1403 .vidioc_querycap = vidioc_querycap, 1404 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, 1405 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, 1406 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, 1407 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, 1408 .vidioc_enum_input = vidioc_enum_input, 1409 .vidioc_g_input = vidioc_g_input, 1410 .vidioc_s_input = vidioc_s_input, 1411 .vidioc_g_jpegcomp = vidioc_g_jpegcomp, 1412 .vidioc_s_jpegcomp = vidioc_s_jpegcomp, 1413 .vidioc_g_parm = vidioc_g_parm, 1414 .vidioc_s_parm = vidioc_s_parm, 1415 .vidioc_enum_framesizes = vidioc_enum_framesizes, 1416 .vidioc_enum_frameintervals = vidioc_enum_frameintervals, 1417 1418 .vidioc_reqbufs = vb2_ioctl_reqbufs, 1419 .vidioc_create_bufs = vb2_ioctl_create_bufs, 1420 .vidioc_querybuf = vb2_ioctl_querybuf, 1421 .vidioc_qbuf = vb2_ioctl_qbuf, 1422 .vidioc_dqbuf = vb2_ioctl_dqbuf, 1423 .vidioc_expbuf = vb2_ioctl_expbuf, 1424 .vidioc_streamon = vb2_ioctl_streamon, 1425 .vidioc_streamoff = vb2_ioctl_streamoff, 1426 1427 #ifdef CONFIG_VIDEO_ADV_DEBUG 1428 .vidioc_g_chip_info = vidioc_g_chip_info, 1429 .vidioc_g_register = vidioc_g_register, 1430 .vidioc_s_register = vidioc_s_register, 1431 #endif 1432 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 1433 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 1434 }; 1435 1436 static const struct video_device gspca_template = { 1437 .name = "gspca main driver", 1438 .fops = &dev_fops, 1439 .ioctl_ops = &dev_ioctl_ops, 1440 .release = video_device_release_empty, /* We use v4l2_dev.release */ 1441 }; 1442 1443 /* 1444 * probe and create a new gspca device 1445 * 1446 * This function must be called by the sub-driver when it is 1447 * called for probing a new device. 1448 */ 1449 int gspca_dev_probe2(struct usb_interface *intf, 1450 const struct usb_device_id *id, 1451 const struct sd_desc *sd_desc, 1452 int dev_size, 1453 struct module *module) 1454 { 1455 struct gspca_dev *gspca_dev; 1456 struct usb_device *dev = interface_to_usbdev(intf); 1457 struct vb2_queue *q; 1458 int ret; 1459 1460 pr_info("%s-" GSPCA_VERSION " probing %04x:%04x\n", 1461 sd_desc->name, id->idVendor, id->idProduct); 1462 1463 /* create the device */ 1464 if (dev_size < sizeof *gspca_dev) 1465 dev_size = sizeof *gspca_dev; 1466 gspca_dev = kzalloc(dev_size, GFP_KERNEL); 1467 if (!gspca_dev) { 1468 pr_err("couldn't kzalloc gspca struct\n"); 1469 return -ENOMEM; 1470 } 1471 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL); 1472 if (!gspca_dev->usb_buf) { 1473 pr_err("out of memory\n"); 1474 ret = -ENOMEM; 1475 goto out; 1476 } 1477 gspca_dev->dev = dev; 1478 gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber; 1479 gspca_dev->xfer_ep = -1; 1480 1481 /* check if any audio device */ 1482 if (dev->actconfig->desc.bNumInterfaces != 1) { 1483 int i; 1484 struct usb_interface *intf2; 1485 1486 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) { 1487 intf2 = dev->actconfig->interface[i]; 1488 if (intf2 != NULL 1489 && intf2->altsetting != NULL 1490 && intf2->altsetting->desc.bInterfaceClass == 1491 USB_CLASS_AUDIO) { 1492 gspca_dev->audio = 1; 1493 break; 1494 } 1495 } 1496 } 1497 1498 gspca_dev->v4l2_dev.release = gspca_release; 1499 ret = v4l2_device_register(&intf->dev, &gspca_dev->v4l2_dev); 1500 if (ret) 1501 goto out; 1502 gspca_dev->present = true; 1503 gspca_dev->sd_desc = sd_desc; 1504 gspca_dev->empty_packet = -1; /* don't check the empty packets */ 1505 gspca_dev->vdev = gspca_template; 1506 gspca_dev->vdev.v4l2_dev = &gspca_dev->v4l2_dev; 1507 gspca_dev->vdev.device_caps = V4L2_CAP_VIDEO_CAPTURE | 1508 V4L2_CAP_STREAMING | V4L2_CAP_READWRITE; 1509 video_set_drvdata(&gspca_dev->vdev, gspca_dev); 1510 gspca_dev->module = module; 1511 1512 mutex_init(&gspca_dev->usb_lock); 1513 gspca_dev->vdev.lock = &gspca_dev->usb_lock; 1514 init_waitqueue_head(&gspca_dev->wq); 1515 1516 /* Initialize the vb2 queue */ 1517 q = &gspca_dev->queue; 1518 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 1519 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ; 1520 q->drv_priv = gspca_dev; 1521 q->buf_struct_size = sizeof(struct gspca_buffer); 1522 q->ops = &gspca_qops; 1523 q->mem_ops = &vb2_vmalloc_memops; 1524 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1525 q->min_buffers_needed = 2; 1526 q->lock = &gspca_dev->usb_lock; 1527 ret = vb2_queue_init(q); 1528 if (ret) 1529 goto out; 1530 gspca_dev->vdev.queue = q; 1531 1532 INIT_LIST_HEAD(&gspca_dev->buf_list); 1533 spin_lock_init(&gspca_dev->qlock); 1534 1535 /* configure the subdriver and initialize the USB device */ 1536 ret = sd_desc->config(gspca_dev, id); 1537 if (ret < 0) 1538 goto out; 1539 ret = sd_desc->init(gspca_dev); 1540 if (ret < 0) 1541 goto out; 1542 if (sd_desc->init_controls) 1543 ret = sd_desc->init_controls(gspca_dev); 1544 if (ret < 0) 1545 goto out; 1546 gspca_set_default_mode(gspca_dev); 1547 1548 ret = gspca_input_connect(gspca_dev); 1549 if (ret) 1550 goto out; 1551 1552 #ifdef CONFIG_VIDEO_ADV_DEBUG 1553 if (!gspca_dev->sd_desc->get_register) 1554 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_G_REGISTER); 1555 if (!gspca_dev->sd_desc->set_register) 1556 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_S_REGISTER); 1557 #endif 1558 if (!gspca_dev->sd_desc->get_jcomp) 1559 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_G_JPEGCOMP); 1560 if (!gspca_dev->sd_desc->set_jcomp) 1561 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_S_JPEGCOMP); 1562 1563 /* init video stuff */ 1564 ret = video_register_device(&gspca_dev->vdev, 1565 VFL_TYPE_GRABBER, 1566 -1); 1567 if (ret < 0) { 1568 pr_err("video_register_device err %d\n", ret); 1569 goto out; 1570 } 1571 1572 usb_set_intfdata(intf, gspca_dev); 1573 gspca_dbg(gspca_dev, D_PROBE, "%s created\n", 1574 video_device_node_name(&gspca_dev->vdev)); 1575 1576 gspca_input_create_urb(gspca_dev); 1577 1578 return 0; 1579 out: 1580 #if IS_ENABLED(CONFIG_INPUT) 1581 if (gspca_dev->input_dev) 1582 input_unregister_device(gspca_dev->input_dev); 1583 #endif 1584 v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler); 1585 kfree(gspca_dev->usb_buf); 1586 kfree(gspca_dev); 1587 return ret; 1588 } 1589 EXPORT_SYMBOL(gspca_dev_probe2); 1590 1591 /* same function as the previous one, but check the interface */ 1592 int gspca_dev_probe(struct usb_interface *intf, 1593 const struct usb_device_id *id, 1594 const struct sd_desc *sd_desc, 1595 int dev_size, 1596 struct module *module) 1597 { 1598 struct usb_device *dev = interface_to_usbdev(intf); 1599 1600 /* we don't handle multi-config cameras */ 1601 if (dev->descriptor.bNumConfigurations != 1) { 1602 pr_err("%04x:%04x too many config\n", 1603 id->idVendor, id->idProduct); 1604 return -ENODEV; 1605 } 1606 1607 /* the USB video interface must be the first one */ 1608 if (dev->actconfig->desc.bNumInterfaces != 1 1609 && intf->cur_altsetting->desc.bInterfaceNumber != 0) 1610 return -ENODEV; 1611 1612 return gspca_dev_probe2(intf, id, sd_desc, dev_size, module); 1613 } 1614 EXPORT_SYMBOL(gspca_dev_probe); 1615 1616 /* 1617 * USB disconnection 1618 * 1619 * This function must be called by the sub-driver 1620 * when the device disconnects, after the specific resources are freed. 1621 */ 1622 void gspca_disconnect(struct usb_interface *intf) 1623 { 1624 struct gspca_dev *gspca_dev = usb_get_intfdata(intf); 1625 #if IS_ENABLED(CONFIG_INPUT) 1626 struct input_dev *input_dev; 1627 #endif 1628 1629 gspca_dbg(gspca_dev, D_PROBE, "%s disconnect\n", 1630 video_device_node_name(&gspca_dev->vdev)); 1631 1632 mutex_lock(&gspca_dev->usb_lock); 1633 gspca_dev->present = false; 1634 destroy_urbs(gspca_dev); 1635 gspca_input_destroy_urb(gspca_dev); 1636 1637 vb2_queue_error(&gspca_dev->queue); 1638 1639 #if IS_ENABLED(CONFIG_INPUT) 1640 input_dev = gspca_dev->input_dev; 1641 if (input_dev) { 1642 gspca_dev->input_dev = NULL; 1643 input_unregister_device(input_dev); 1644 } 1645 #endif 1646 1647 v4l2_device_disconnect(&gspca_dev->v4l2_dev); 1648 video_unregister_device(&gspca_dev->vdev); 1649 1650 mutex_unlock(&gspca_dev->usb_lock); 1651 1652 /* (this will call gspca_release() immediately or on last close) */ 1653 v4l2_device_put(&gspca_dev->v4l2_dev); 1654 } 1655 EXPORT_SYMBOL(gspca_disconnect); 1656 1657 #ifdef CONFIG_PM 1658 int gspca_suspend(struct usb_interface *intf, pm_message_t message) 1659 { 1660 struct gspca_dev *gspca_dev = usb_get_intfdata(intf); 1661 1662 gspca_input_destroy_urb(gspca_dev); 1663 1664 if (!vb2_start_streaming_called(&gspca_dev->queue)) 1665 return 0; 1666 1667 mutex_lock(&gspca_dev->usb_lock); 1668 gspca_dev->frozen = 1; /* avoid urb error messages */ 1669 gspca_dev->usb_err = 0; 1670 if (gspca_dev->sd_desc->stopN) 1671 gspca_dev->sd_desc->stopN(gspca_dev); 1672 destroy_urbs(gspca_dev); 1673 gspca_set_alt0(gspca_dev); 1674 if (gspca_dev->sd_desc->stop0) 1675 gspca_dev->sd_desc->stop0(gspca_dev); 1676 mutex_unlock(&gspca_dev->usb_lock); 1677 1678 return 0; 1679 } 1680 EXPORT_SYMBOL(gspca_suspend); 1681 1682 int gspca_resume(struct usb_interface *intf) 1683 { 1684 struct gspca_dev *gspca_dev = usb_get_intfdata(intf); 1685 int streaming, ret = 0; 1686 1687 mutex_lock(&gspca_dev->usb_lock); 1688 gspca_dev->frozen = 0; 1689 gspca_dev->usb_err = 0; 1690 gspca_dev->sd_desc->init(gspca_dev); 1691 /* 1692 * Most subdrivers send all ctrl values on sd_start and thus 1693 * only write to the device registers on s_ctrl when streaming -> 1694 * Clear streaming to avoid setting all ctrls twice. 1695 */ 1696 streaming = vb2_start_streaming_called(&gspca_dev->queue); 1697 if (streaming) 1698 ret = gspca_init_transfer(gspca_dev); 1699 else 1700 gspca_input_create_urb(gspca_dev); 1701 mutex_unlock(&gspca_dev->usb_lock); 1702 1703 return ret; 1704 } 1705 EXPORT_SYMBOL(gspca_resume); 1706 #endif 1707 1708 /* -- module insert / remove -- */ 1709 static int __init gspca_init(void) 1710 { 1711 pr_info("v" GSPCA_VERSION " registered\n"); 1712 return 0; 1713 } 1714 static void __exit gspca_exit(void) 1715 { 1716 } 1717 1718 module_init(gspca_init); 1719 module_exit(gspca_exit); 1720 1721 module_param_named(debug, gspca_debug, int, 0644); 1722 MODULE_PARM_DESC(debug, 1723 "1:probe 2:config 3:stream 4:frame 5:packet 6:usbi 7:usbo"); 1724