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 if (gspca_dev->image_len > gspca_dev->pixfmt.sizeimage) 448 gspca_dev->image_len = gspca_dev->pixfmt.sizeimage; 449 spin_lock_irqsave(&gspca_dev->qlock, flags); 450 list_del(&buf->list); 451 spin_unlock_irqrestore(&gspca_dev->qlock, flags); 452 buf->vb.vb2_buf.timestamp = ktime_get_ns(); 453 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, 454 gspca_dev->image_len); 455 buf->vb.sequence = gspca_dev->sequence++; 456 buf->vb.field = V4L2_FIELD_NONE; 457 gspca_dbg(gspca_dev, D_FRAM, "frame complete len:%d\n", 458 gspca_dev->image_len); 459 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE); 460 gspca_dev->image = NULL; 461 gspca_dev->image_len = 0; 462 } 463 } 464 EXPORT_SYMBOL(gspca_frame_add); 465 466 static void destroy_urbs(struct gspca_dev *gspca_dev) 467 { 468 struct urb *urb; 469 unsigned int i; 470 471 gspca_dbg(gspca_dev, D_STREAM, "kill transfer\n"); 472 473 /* Killing all URBs guarantee that no URB completion 474 * handler is running. Therefore, there shouldn't 475 * be anyone trying to access gspca_dev->urb[i] 476 */ 477 for (i = 0; i < MAX_NURBS; i++) 478 usb_kill_urb(gspca_dev->urb[i]); 479 480 gspca_dbg(gspca_dev, D_STREAM, "releasing urbs\n"); 481 for (i = 0; i < MAX_NURBS; i++) { 482 urb = gspca_dev->urb[i]; 483 if (!urb) 484 continue; 485 gspca_dev->urb[i] = NULL; 486 usb_free_coherent(gspca_dev->dev, 487 urb->transfer_buffer_length, 488 urb->transfer_buffer, 489 urb->transfer_dma); 490 usb_free_urb(urb); 491 } 492 } 493 494 static int gspca_set_alt0(struct gspca_dev *gspca_dev) 495 { 496 int ret; 497 498 if (gspca_dev->alt == 0) 499 return 0; 500 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0); 501 if (ret < 0) 502 pr_err("set alt 0 err %d\n", ret); 503 return ret; 504 } 505 506 /* 507 * look for an input transfer endpoint in an alternate setting. 508 * 509 * If xfer_ep is invalid, return the first valid ep found, otherwise 510 * look for exactly the ep with address equal to xfer_ep. 511 */ 512 static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt, 513 int xfer, int xfer_ep) 514 { 515 struct usb_host_endpoint *ep; 516 int i, attr; 517 518 for (i = 0; i < alt->desc.bNumEndpoints; i++) { 519 ep = &alt->endpoint[i]; 520 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK; 521 if (attr == xfer 522 && ep->desc.wMaxPacketSize != 0 523 && usb_endpoint_dir_in(&ep->desc) 524 && (xfer_ep < 0 || ep->desc.bEndpointAddress == xfer_ep)) 525 return ep; 526 } 527 return NULL; 528 } 529 530 /* compute the minimum bandwidth for the current transfer */ 531 static u32 which_bandwidth(struct gspca_dev *gspca_dev) 532 { 533 u32 bandwidth; 534 535 /* get the (max) image size */ 536 bandwidth = gspca_dev->pixfmt.sizeimage; 537 538 /* if the image is compressed, estimate its mean size */ 539 if (!gspca_dev->cam.needs_full_bandwidth && 540 bandwidth < gspca_dev->pixfmt.width * 541 gspca_dev->pixfmt.height) 542 bandwidth = bandwidth * 3 / 8; /* 0.375 */ 543 544 /* estimate the frame rate */ 545 if (gspca_dev->sd_desc->get_streamparm) { 546 struct v4l2_streamparm parm; 547 548 gspca_dev->sd_desc->get_streamparm(gspca_dev, &parm); 549 bandwidth *= parm.parm.capture.timeperframe.denominator; 550 bandwidth /= parm.parm.capture.timeperframe.numerator; 551 } else { 552 553 /* don't hope more than 15 fps with USB 1.1 and 554 * image resolution >= 640x480 */ 555 if (gspca_dev->pixfmt.width >= 640 556 && gspca_dev->dev->speed == USB_SPEED_FULL) 557 bandwidth *= 15; /* 15 fps */ 558 else 559 bandwidth *= 30; /* 30 fps */ 560 } 561 562 gspca_dbg(gspca_dev, D_STREAM, "min bandwidth: %d\n", bandwidth); 563 return bandwidth; 564 } 565 566 /* endpoint table */ 567 #define MAX_ALT 16 568 struct ep_tb_s { 569 u32 alt; 570 u32 bandwidth; 571 }; 572 573 /* 574 * build the table of the endpoints 575 * and compute the minimum bandwidth for the image transfer 576 */ 577 static int build_isoc_ep_tb(struct gspca_dev *gspca_dev, 578 struct usb_interface *intf, 579 struct ep_tb_s *ep_tb) 580 { 581 struct usb_host_endpoint *ep; 582 int i, j, nbalt, psize, found; 583 u32 bandwidth, last_bw; 584 585 nbalt = intf->num_altsetting; 586 if (nbalt > MAX_ALT) 587 nbalt = MAX_ALT; /* fixme: should warn */ 588 589 /* build the endpoint table */ 590 i = 0; 591 last_bw = 0; 592 for (;;) { 593 ep_tb->bandwidth = 2000 * 2000 * 120; 594 found = 0; 595 for (j = 0; j < nbalt; j++) { 596 ep = alt_xfer(&intf->altsetting[j], 597 USB_ENDPOINT_XFER_ISOC, 598 gspca_dev->xfer_ep); 599 if (ep == NULL) 600 continue; 601 if (ep->desc.bInterval == 0) { 602 pr_err("alt %d iso endp with 0 interval\n", j); 603 continue; 604 } 605 psize = le16_to_cpu(ep->desc.wMaxPacketSize); 606 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3)); 607 bandwidth = psize * 1000; 608 if (gspca_dev->dev->speed == USB_SPEED_HIGH 609 || gspca_dev->dev->speed >= USB_SPEED_SUPER) 610 bandwidth *= 8; 611 bandwidth /= 1 << (ep->desc.bInterval - 1); 612 if (bandwidth <= last_bw) 613 continue; 614 if (bandwidth < ep_tb->bandwidth) { 615 ep_tb->bandwidth = bandwidth; 616 ep_tb->alt = j; 617 found = 1; 618 } 619 } 620 if (!found) 621 break; 622 gspca_dbg(gspca_dev, D_STREAM, "alt %d bandwidth %d\n", 623 ep_tb->alt, ep_tb->bandwidth); 624 last_bw = ep_tb->bandwidth; 625 i++; 626 ep_tb++; 627 } 628 629 /* 630 * If the camera: 631 * has a usb audio class interface (a built in usb mic); and 632 * is a usb 1 full speed device; and 633 * uses the max full speed iso bandwidth; and 634 * and has more than 1 alt setting 635 * then skip the highest alt setting to spare bandwidth for the mic 636 */ 637 if (gspca_dev->audio && 638 gspca_dev->dev->speed == USB_SPEED_FULL && 639 last_bw >= 1000000 && 640 i > 1) { 641 gspca_dbg(gspca_dev, D_STREAM, "dev has usb audio, skipping highest alt\n"); 642 i--; 643 ep_tb--; 644 } 645 646 /* get the requested bandwidth and start at the highest atlsetting */ 647 bandwidth = which_bandwidth(gspca_dev); 648 ep_tb--; 649 while (i > 1) { 650 ep_tb--; 651 if (ep_tb->bandwidth < bandwidth) 652 break; 653 i--; 654 } 655 return i; 656 } 657 658 /* 659 * create the URBs for image transfer 660 */ 661 static int create_urbs(struct gspca_dev *gspca_dev, 662 struct usb_host_endpoint *ep) 663 { 664 struct urb *urb; 665 int n, nurbs, i, psize, npkt, bsize; 666 667 /* calculate the packet size and the number of packets */ 668 psize = le16_to_cpu(ep->desc.wMaxPacketSize); 669 670 if (!gspca_dev->cam.bulk) { /* isoc */ 671 672 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */ 673 if (gspca_dev->pkt_size == 0) 674 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3)); 675 else 676 psize = gspca_dev->pkt_size; 677 npkt = gspca_dev->cam.npkt; 678 if (npkt == 0) 679 npkt = 32; /* default value */ 680 bsize = psize * npkt; 681 gspca_dbg(gspca_dev, D_STREAM, 682 "isoc %d pkts size %d = bsize:%d\n", 683 npkt, psize, bsize); 684 nurbs = DEF_NURBS; 685 } else { /* bulk */ 686 npkt = 0; 687 bsize = gspca_dev->cam.bulk_size; 688 if (bsize == 0) 689 bsize = psize; 690 gspca_dbg(gspca_dev, D_STREAM, "bulk bsize:%d\n", bsize); 691 if (gspca_dev->cam.bulk_nurbs != 0) 692 nurbs = gspca_dev->cam.bulk_nurbs; 693 else 694 nurbs = 1; 695 } 696 697 for (n = 0; n < nurbs; n++) { 698 urb = usb_alloc_urb(npkt, GFP_KERNEL); 699 if (!urb) 700 return -ENOMEM; 701 gspca_dev->urb[n] = urb; 702 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev, 703 bsize, 704 GFP_KERNEL, 705 &urb->transfer_dma); 706 707 if (urb->transfer_buffer == NULL) { 708 pr_err("usb_alloc_coherent failed\n"); 709 return -ENOMEM; 710 } 711 urb->dev = gspca_dev->dev; 712 urb->context = gspca_dev; 713 urb->transfer_buffer_length = bsize; 714 if (npkt != 0) { /* ISOC */ 715 urb->pipe = usb_rcvisocpipe(gspca_dev->dev, 716 ep->desc.bEndpointAddress); 717 urb->transfer_flags = URB_ISO_ASAP 718 | URB_NO_TRANSFER_DMA_MAP; 719 urb->interval = 1 << (ep->desc.bInterval - 1); 720 urb->complete = isoc_irq; 721 urb->number_of_packets = npkt; 722 for (i = 0; i < npkt; i++) { 723 urb->iso_frame_desc[i].length = psize; 724 urb->iso_frame_desc[i].offset = psize * i; 725 } 726 } else { /* bulk */ 727 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev, 728 ep->desc.bEndpointAddress); 729 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; 730 urb->complete = bulk_irq; 731 } 732 } 733 return 0; 734 } 735 736 /* Note: both the queue and the usb locks should be held when calling this */ 737 static void gspca_stream_off(struct gspca_dev *gspca_dev) 738 { 739 gspca_dev->streaming = false; 740 gspca_dev->usb_err = 0; 741 if (gspca_dev->sd_desc->stopN) 742 gspca_dev->sd_desc->stopN(gspca_dev); 743 destroy_urbs(gspca_dev); 744 gspca_input_destroy_urb(gspca_dev); 745 gspca_set_alt0(gspca_dev); 746 if (gspca_dev->present) 747 gspca_input_create_urb(gspca_dev); 748 if (gspca_dev->sd_desc->stop0) 749 gspca_dev->sd_desc->stop0(gspca_dev); 750 gspca_dbg(gspca_dev, D_STREAM, "stream off OK\n"); 751 } 752 753 /* 754 * start the USB transfer 755 */ 756 static int gspca_init_transfer(struct gspca_dev *gspca_dev) 757 { 758 struct usb_interface *intf; 759 struct usb_host_endpoint *ep; 760 struct urb *urb; 761 struct ep_tb_s ep_tb[MAX_ALT]; 762 int n, ret, xfer, alt, alt_idx; 763 764 /* reset the streaming variables */ 765 gspca_dev->image = NULL; 766 gspca_dev->image_len = 0; 767 gspca_dev->last_packet_type = DISCARD_PACKET; 768 769 gspca_dev->usb_err = 0; 770 771 /* do the specific subdriver stuff before endpoint selection */ 772 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface); 773 gspca_dev->alt = gspca_dev->cam.bulk ? intf->num_altsetting : 0; 774 if (gspca_dev->sd_desc->isoc_init) { 775 ret = gspca_dev->sd_desc->isoc_init(gspca_dev); 776 if (ret < 0) 777 return ret; 778 } 779 xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK 780 : USB_ENDPOINT_XFER_ISOC; 781 782 /* if bulk or the subdriver forced an altsetting, get the endpoint */ 783 if (gspca_dev->alt != 0) { 784 gspca_dev->alt--; /* (previous version compatibility) */ 785 ep = alt_xfer(&intf->altsetting[gspca_dev->alt], xfer, 786 gspca_dev->xfer_ep); 787 if (ep == NULL) { 788 pr_err("bad altsetting %d\n", gspca_dev->alt); 789 return -EIO; 790 } 791 ep_tb[0].alt = gspca_dev->alt; 792 alt_idx = 1; 793 } else { 794 /* else, compute the minimum bandwidth 795 * and build the endpoint table */ 796 alt_idx = build_isoc_ep_tb(gspca_dev, intf, ep_tb); 797 if (alt_idx <= 0) { 798 pr_err("no transfer endpoint found\n"); 799 return -EIO; 800 } 801 } 802 803 /* set the highest alternate setting and 804 * loop until urb submit succeeds */ 805 gspca_input_destroy_urb(gspca_dev); 806 807 gspca_dev->alt = ep_tb[--alt_idx].alt; 808 alt = -1; 809 for (;;) { 810 if (alt != gspca_dev->alt) { 811 alt = gspca_dev->alt; 812 if (intf->num_altsetting > 1) { 813 ret = usb_set_interface(gspca_dev->dev, 814 gspca_dev->iface, 815 alt); 816 if (ret < 0) { 817 if (ret == -ENOSPC) 818 goto retry; /*fixme: ugly*/ 819 pr_err("set alt %d err %d\n", alt, ret); 820 goto out; 821 } 822 } 823 } 824 if (!gspca_dev->cam.no_urb_create) { 825 gspca_dbg(gspca_dev, D_STREAM, "init transfer alt %d\n", 826 alt); 827 ret = create_urbs(gspca_dev, 828 alt_xfer(&intf->altsetting[alt], xfer, 829 gspca_dev->xfer_ep)); 830 if (ret < 0) { 831 destroy_urbs(gspca_dev); 832 goto out; 833 } 834 } 835 836 /* clear the bulk endpoint */ 837 if (gspca_dev->cam.bulk) 838 usb_clear_halt(gspca_dev->dev, 839 gspca_dev->urb[0]->pipe); 840 841 /* start the cam */ 842 ret = gspca_dev->sd_desc->start(gspca_dev); 843 if (ret < 0) { 844 destroy_urbs(gspca_dev); 845 goto out; 846 } 847 v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler); 848 gspca_dev->streaming = true; 849 850 /* some bulk transfers are started by the subdriver */ 851 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0) 852 break; 853 854 /* submit the URBs */ 855 for (n = 0; n < MAX_NURBS; n++) { 856 urb = gspca_dev->urb[n]; 857 if (urb == NULL) 858 break; 859 ret = usb_submit_urb(urb, GFP_KERNEL); 860 if (ret < 0) 861 break; 862 } 863 if (ret >= 0) 864 break; /* transfer is started */ 865 866 /* something when wrong 867 * stop the webcam and free the transfer resources */ 868 gspca_stream_off(gspca_dev); 869 if (ret != -ENOSPC) { 870 pr_err("usb_submit_urb alt %d err %d\n", 871 gspca_dev->alt, ret); 872 goto out; 873 } 874 875 /* the bandwidth is not wide enough 876 * negotiate or try a lower alternate setting */ 877 retry: 878 gspca_err(gspca_dev, "alt %d - bandwidth not wide enough, trying again\n", 879 alt); 880 msleep(20); /* wait for kill complete */ 881 if (gspca_dev->sd_desc->isoc_nego) { 882 ret = gspca_dev->sd_desc->isoc_nego(gspca_dev); 883 if (ret < 0) 884 goto out; 885 } else { 886 if (alt_idx <= 0) { 887 pr_err("no transfer endpoint found\n"); 888 ret = -EIO; 889 goto out; 890 } 891 gspca_dev->alt = ep_tb[--alt_idx].alt; 892 } 893 } 894 out: 895 gspca_input_create_urb(gspca_dev); 896 return ret; 897 } 898 899 static void gspca_set_default_mode(struct gspca_dev *gspca_dev) 900 { 901 int i; 902 903 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */ 904 gspca_dev->curr_mode = i; 905 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i]; 906 907 /* does nothing if ctrl_handler == NULL */ 908 v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler); 909 } 910 911 static int wxh_to_mode(struct gspca_dev *gspca_dev, 912 int width, int height, u32 pixelformat) 913 { 914 int i; 915 916 for (i = 0; i < gspca_dev->cam.nmodes; i++) { 917 if (width == gspca_dev->cam.cam_mode[i].width 918 && height == gspca_dev->cam.cam_mode[i].height 919 && pixelformat == gspca_dev->cam.cam_mode[i].pixelformat) 920 return i; 921 } 922 return -EINVAL; 923 } 924 925 static int wxh_to_nearest_mode(struct gspca_dev *gspca_dev, 926 int width, int height, u32 pixelformat) 927 { 928 int i; 929 930 for (i = gspca_dev->cam.nmodes; --i >= 0; ) { 931 if (width >= gspca_dev->cam.cam_mode[i].width 932 && height >= gspca_dev->cam.cam_mode[i].height 933 && pixelformat == gspca_dev->cam.cam_mode[i].pixelformat) 934 return i; 935 } 936 for (i = gspca_dev->cam.nmodes; --i > 0; ) { 937 if (width >= gspca_dev->cam.cam_mode[i].width 938 && height >= gspca_dev->cam.cam_mode[i].height) 939 break; 940 } 941 return i; 942 } 943 944 /* 945 * search a mode with the right pixel format 946 */ 947 static int gspca_get_mode(struct gspca_dev *gspca_dev, 948 int mode, 949 int pixfmt) 950 { 951 int modeU, modeD; 952 953 modeU = modeD = mode; 954 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) { 955 if (--modeD >= 0) { 956 if (gspca_dev->cam.cam_mode[modeD].pixelformat 957 == pixfmt) 958 return modeD; 959 } 960 if (++modeU < gspca_dev->cam.nmodes) { 961 if (gspca_dev->cam.cam_mode[modeU].pixelformat 962 == pixfmt) 963 return modeU; 964 } 965 } 966 return -EINVAL; 967 } 968 969 #ifdef CONFIG_VIDEO_ADV_DEBUG 970 static int vidioc_g_chip_info(struct file *file, void *priv, 971 struct v4l2_dbg_chip_info *chip) 972 { 973 struct gspca_dev *gspca_dev = video_drvdata(file); 974 975 gspca_dev->usb_err = 0; 976 if (gspca_dev->sd_desc->get_chip_info) 977 return gspca_dev->sd_desc->get_chip_info(gspca_dev, chip); 978 return chip->match.addr ? -EINVAL : 0; 979 } 980 981 static int vidioc_g_register(struct file *file, void *priv, 982 struct v4l2_dbg_register *reg) 983 { 984 struct gspca_dev *gspca_dev = video_drvdata(file); 985 986 gspca_dev->usb_err = 0; 987 return gspca_dev->sd_desc->get_register(gspca_dev, reg); 988 } 989 990 static int vidioc_s_register(struct file *file, void *priv, 991 const struct v4l2_dbg_register *reg) 992 { 993 struct gspca_dev *gspca_dev = video_drvdata(file); 994 995 gspca_dev->usb_err = 0; 996 return gspca_dev->sd_desc->set_register(gspca_dev, reg); 997 } 998 #endif 999 1000 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, 1001 struct v4l2_fmtdesc *fmtdesc) 1002 { 1003 struct gspca_dev *gspca_dev = video_drvdata(file); 1004 int i, j, index; 1005 __u32 fmt_tb[8]; 1006 1007 /* give an index to each format */ 1008 index = 0; 1009 for (i = gspca_dev->cam.nmodes; --i >= 0; ) { 1010 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat; 1011 j = 0; 1012 for (;;) { 1013 if (fmt_tb[j] == fmt_tb[index]) 1014 break; 1015 j++; 1016 } 1017 if (j == index) { 1018 if (fmtdesc->index == index) 1019 break; /* new format */ 1020 index++; 1021 if (index >= ARRAY_SIZE(fmt_tb)) 1022 return -EINVAL; 1023 } 1024 } 1025 if (i < 0) 1026 return -EINVAL; /* no more format */ 1027 1028 fmtdesc->pixelformat = fmt_tb[index]; 1029 return 0; 1030 } 1031 1032 static int vidioc_g_fmt_vid_cap(struct file *file, void *_priv, 1033 struct v4l2_format *fmt) 1034 { 1035 struct gspca_dev *gspca_dev = video_drvdata(file); 1036 u32 priv = fmt->fmt.pix.priv; 1037 1038 fmt->fmt.pix = gspca_dev->pixfmt; 1039 /* some drivers use priv internally, so keep the original value */ 1040 fmt->fmt.pix.priv = priv; 1041 return 0; 1042 } 1043 1044 static int try_fmt_vid_cap(struct gspca_dev *gspca_dev, 1045 struct v4l2_format *fmt) 1046 { 1047 int w, h, mode, mode2; 1048 1049 w = fmt->fmt.pix.width; 1050 h = fmt->fmt.pix.height; 1051 1052 PDEBUG_MODE(gspca_dev, D_CONF, "try fmt cap", 1053 fmt->fmt.pix.pixelformat, w, h); 1054 1055 /* search the nearest mode for width and height */ 1056 mode = wxh_to_nearest_mode(gspca_dev, w, h, fmt->fmt.pix.pixelformat); 1057 1058 /* OK if right palette */ 1059 if (gspca_dev->cam.cam_mode[mode].pixelformat 1060 != fmt->fmt.pix.pixelformat) { 1061 1062 /* else, search the closest mode with the same pixel format */ 1063 mode2 = gspca_get_mode(gspca_dev, mode, 1064 fmt->fmt.pix.pixelformat); 1065 if (mode2 >= 0) 1066 mode = mode2; 1067 } 1068 fmt->fmt.pix = gspca_dev->cam.cam_mode[mode]; 1069 if (gspca_dev->sd_desc->try_fmt) { 1070 /* pass original resolution to subdriver try_fmt */ 1071 fmt->fmt.pix.width = w; 1072 fmt->fmt.pix.height = h; 1073 gspca_dev->sd_desc->try_fmt(gspca_dev, fmt); 1074 } 1075 return mode; /* used when s_fmt */ 1076 } 1077 1078 static int vidioc_try_fmt_vid_cap(struct file *file, void *_priv, 1079 struct v4l2_format *fmt) 1080 { 1081 struct gspca_dev *gspca_dev = video_drvdata(file); 1082 u32 priv = fmt->fmt.pix.priv; 1083 1084 if (try_fmt_vid_cap(gspca_dev, fmt) < 0) 1085 return -EINVAL; 1086 /* some drivers use priv internally, so keep the original value */ 1087 fmt->fmt.pix.priv = priv; 1088 return 0; 1089 } 1090 1091 static int vidioc_s_fmt_vid_cap(struct file *file, void *_priv, 1092 struct v4l2_format *fmt) 1093 { 1094 struct gspca_dev *gspca_dev = video_drvdata(file); 1095 u32 priv = fmt->fmt.pix.priv; 1096 int mode; 1097 1098 if (vb2_is_busy(&gspca_dev->queue)) 1099 return -EBUSY; 1100 1101 mode = try_fmt_vid_cap(gspca_dev, fmt); 1102 if (mode < 0) 1103 return -EINVAL; 1104 1105 gspca_dev->curr_mode = mode; 1106 if (gspca_dev->sd_desc->try_fmt) 1107 /* subdriver try_fmt can modify format parameters */ 1108 gspca_dev->pixfmt = fmt->fmt.pix; 1109 else 1110 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[mode]; 1111 /* some drivers use priv internally, so keep the original value */ 1112 fmt->fmt.pix.priv = priv; 1113 return 0; 1114 } 1115 1116 static int vidioc_enum_framesizes(struct file *file, void *priv, 1117 struct v4l2_frmsizeenum *fsize) 1118 { 1119 struct gspca_dev *gspca_dev = video_drvdata(file); 1120 int i; 1121 __u32 index = 0; 1122 1123 if (gspca_dev->sd_desc->enum_framesizes) 1124 return gspca_dev->sd_desc->enum_framesizes(gspca_dev, fsize); 1125 1126 for (i = 0; i < gspca_dev->cam.nmodes; i++) { 1127 if (fsize->pixel_format != 1128 gspca_dev->cam.cam_mode[i].pixelformat) 1129 continue; 1130 1131 if (fsize->index == index) { 1132 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE; 1133 fsize->discrete.width = 1134 gspca_dev->cam.cam_mode[i].width; 1135 fsize->discrete.height = 1136 gspca_dev->cam.cam_mode[i].height; 1137 return 0; 1138 } 1139 index++; 1140 } 1141 1142 return -EINVAL; 1143 } 1144 1145 static int vidioc_enum_frameintervals(struct file *filp, void *priv, 1146 struct v4l2_frmivalenum *fival) 1147 { 1148 struct gspca_dev *gspca_dev = video_drvdata(filp); 1149 int mode; 1150 __u32 i; 1151 1152 mode = wxh_to_mode(gspca_dev, fival->width, fival->height, 1153 fival->pixel_format); 1154 if (mode < 0) 1155 return -EINVAL; 1156 1157 if (gspca_dev->cam.mode_framerates == NULL || 1158 gspca_dev->cam.mode_framerates[mode].nrates == 0) 1159 return -EINVAL; 1160 1161 if (fival->pixel_format != 1162 gspca_dev->cam.cam_mode[mode].pixelformat) 1163 return -EINVAL; 1164 1165 for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) { 1166 if (fival->index == i) { 1167 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE; 1168 fival->discrete.numerator = 1; 1169 fival->discrete.denominator = 1170 gspca_dev->cam.mode_framerates[mode].rates[i]; 1171 return 0; 1172 } 1173 } 1174 1175 return -EINVAL; 1176 } 1177 1178 static void gspca_release(struct v4l2_device *v4l2_device) 1179 { 1180 struct gspca_dev *gspca_dev = 1181 container_of(v4l2_device, struct gspca_dev, v4l2_dev); 1182 1183 v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler); 1184 v4l2_device_unregister(&gspca_dev->v4l2_dev); 1185 kfree(gspca_dev->usb_buf); 1186 kfree(gspca_dev); 1187 } 1188 1189 static int vidioc_querycap(struct file *file, void *priv, 1190 struct v4l2_capability *cap) 1191 { 1192 struct gspca_dev *gspca_dev = video_drvdata(file); 1193 1194 strscpy((char *)cap->driver, gspca_dev->sd_desc->name, 1195 sizeof(cap->driver)); 1196 if (gspca_dev->dev->product != NULL) { 1197 strscpy((char *)cap->card, gspca_dev->dev->product, 1198 sizeof(cap->card)); 1199 } else { 1200 snprintf((char *) cap->card, sizeof cap->card, 1201 "USB Camera (%04x:%04x)", 1202 le16_to_cpu(gspca_dev->dev->descriptor.idVendor), 1203 le16_to_cpu(gspca_dev->dev->descriptor.idProduct)); 1204 } 1205 usb_make_path(gspca_dev->dev, (char *) cap->bus_info, 1206 sizeof(cap->bus_info)); 1207 return 0; 1208 } 1209 1210 static int vidioc_enum_input(struct file *file, void *priv, 1211 struct v4l2_input *input) 1212 { 1213 struct gspca_dev *gspca_dev = video_drvdata(file); 1214 1215 if (input->index != 0) 1216 return -EINVAL; 1217 input->type = V4L2_INPUT_TYPE_CAMERA; 1218 input->status = gspca_dev->cam.input_flags; 1219 strscpy(input->name, gspca_dev->sd_desc->name, 1220 sizeof input->name); 1221 return 0; 1222 } 1223 1224 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i) 1225 { 1226 *i = 0; 1227 return 0; 1228 } 1229 1230 static int vidioc_s_input(struct file *file, void *priv, unsigned int i) 1231 { 1232 if (i > 0) 1233 return -EINVAL; 1234 return 0; 1235 } 1236 1237 static int vidioc_g_jpegcomp(struct file *file, void *priv, 1238 struct v4l2_jpegcompression *jpegcomp) 1239 { 1240 struct gspca_dev *gspca_dev = video_drvdata(file); 1241 1242 gspca_dev->usb_err = 0; 1243 return gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp); 1244 } 1245 1246 static int vidioc_s_jpegcomp(struct file *file, void *priv, 1247 const 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->set_jcomp(gspca_dev, jpegcomp); 1253 } 1254 1255 static int vidioc_g_parm(struct file *filp, void *priv, 1256 struct v4l2_streamparm *parm) 1257 { 1258 struct gspca_dev *gspca_dev = video_drvdata(filp); 1259 1260 parm->parm.capture.readbuffers = gspca_dev->queue.min_queued_buffers; 1261 1262 if (!gspca_dev->sd_desc->get_streamparm) 1263 return 0; 1264 1265 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME; 1266 gspca_dev->usb_err = 0; 1267 gspca_dev->sd_desc->get_streamparm(gspca_dev, parm); 1268 return gspca_dev->usb_err; 1269 } 1270 1271 static int vidioc_s_parm(struct file *filp, void *priv, 1272 struct v4l2_streamparm *parm) 1273 { 1274 struct gspca_dev *gspca_dev = video_drvdata(filp); 1275 1276 parm->parm.capture.readbuffers = gspca_dev->queue.min_queued_buffers; 1277 1278 if (!gspca_dev->sd_desc->set_streamparm) { 1279 parm->parm.capture.capability = 0; 1280 return 0; 1281 } 1282 1283 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME; 1284 gspca_dev->usb_err = 0; 1285 gspca_dev->sd_desc->set_streamparm(gspca_dev, parm); 1286 return gspca_dev->usb_err; 1287 } 1288 1289 static int gspca_queue_setup(struct vb2_queue *vq, 1290 unsigned int *nbuffers, unsigned int *nplanes, 1291 unsigned int sizes[], struct device *alloc_devs[]) 1292 { 1293 struct gspca_dev *gspca_dev = vb2_get_drv_priv(vq); 1294 unsigned int size = PAGE_ALIGN(gspca_dev->pixfmt.sizeimage); 1295 1296 if (*nplanes) 1297 return sizes[0] < size ? -EINVAL : 0; 1298 *nplanes = 1; 1299 sizes[0] = size; 1300 return 0; 1301 } 1302 1303 static int gspca_buffer_prepare(struct vb2_buffer *vb) 1304 { 1305 struct gspca_dev *gspca_dev = vb2_get_drv_priv(vb->vb2_queue); 1306 unsigned long size = PAGE_ALIGN(gspca_dev->pixfmt.sizeimage); 1307 1308 if (vb2_plane_size(vb, 0) < size) { 1309 gspca_err(gspca_dev, "buffer too small (%lu < %lu)\n", 1310 vb2_plane_size(vb, 0), size); 1311 return -EINVAL; 1312 } 1313 return 0; 1314 } 1315 1316 static void gspca_buffer_finish(struct vb2_buffer *vb) 1317 { 1318 struct gspca_dev *gspca_dev = vb2_get_drv_priv(vb->vb2_queue); 1319 1320 if (!gspca_dev->sd_desc->dq_callback) 1321 return; 1322 1323 gspca_dev->usb_err = 0; 1324 if (gspca_dev->present) 1325 gspca_dev->sd_desc->dq_callback(gspca_dev); 1326 } 1327 1328 static void gspca_buffer_queue(struct vb2_buffer *vb) 1329 { 1330 struct gspca_dev *gspca_dev = vb2_get_drv_priv(vb->vb2_queue); 1331 struct gspca_buffer *buf = to_gspca_buffer(vb); 1332 unsigned long flags; 1333 1334 spin_lock_irqsave(&gspca_dev->qlock, flags); 1335 list_add_tail(&buf->list, &gspca_dev->buf_list); 1336 spin_unlock_irqrestore(&gspca_dev->qlock, flags); 1337 } 1338 1339 static void gspca_return_all_buffers(struct gspca_dev *gspca_dev, 1340 enum vb2_buffer_state state) 1341 { 1342 struct gspca_buffer *buf, *node; 1343 unsigned long flags; 1344 1345 spin_lock_irqsave(&gspca_dev->qlock, flags); 1346 list_for_each_entry_safe(buf, node, &gspca_dev->buf_list, list) { 1347 vb2_buffer_done(&buf->vb.vb2_buf, state); 1348 list_del(&buf->list); 1349 } 1350 spin_unlock_irqrestore(&gspca_dev->qlock, flags); 1351 } 1352 1353 static int gspca_start_streaming(struct vb2_queue *vq, unsigned int count) 1354 { 1355 struct gspca_dev *gspca_dev = vb2_get_drv_priv(vq); 1356 int ret; 1357 1358 gspca_dev->sequence = 0; 1359 1360 ret = gspca_init_transfer(gspca_dev); 1361 if (ret) 1362 gspca_return_all_buffers(gspca_dev, VB2_BUF_STATE_QUEUED); 1363 return ret; 1364 } 1365 1366 static void gspca_stop_streaming(struct vb2_queue *vq) 1367 { 1368 struct gspca_dev *gspca_dev = vb2_get_drv_priv(vq); 1369 1370 gspca_stream_off(gspca_dev); 1371 1372 /* Release all active buffers */ 1373 gspca_return_all_buffers(gspca_dev, VB2_BUF_STATE_ERROR); 1374 } 1375 1376 static const struct vb2_ops gspca_qops = { 1377 .queue_setup = gspca_queue_setup, 1378 .buf_prepare = gspca_buffer_prepare, 1379 .buf_finish = gspca_buffer_finish, 1380 .buf_queue = gspca_buffer_queue, 1381 .start_streaming = gspca_start_streaming, 1382 .stop_streaming = gspca_stop_streaming, 1383 .wait_prepare = vb2_ops_wait_prepare, 1384 .wait_finish = vb2_ops_wait_finish, 1385 }; 1386 1387 static const struct v4l2_file_operations dev_fops = { 1388 .owner = THIS_MODULE, 1389 .open = v4l2_fh_open, 1390 .release = vb2_fop_release, 1391 .unlocked_ioctl = video_ioctl2, 1392 .read = vb2_fop_read, 1393 .mmap = vb2_fop_mmap, 1394 .poll = vb2_fop_poll, 1395 }; 1396 1397 static const struct v4l2_ioctl_ops dev_ioctl_ops = { 1398 .vidioc_querycap = vidioc_querycap, 1399 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, 1400 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, 1401 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, 1402 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, 1403 .vidioc_enum_input = vidioc_enum_input, 1404 .vidioc_g_input = vidioc_g_input, 1405 .vidioc_s_input = vidioc_s_input, 1406 .vidioc_g_jpegcomp = vidioc_g_jpegcomp, 1407 .vidioc_s_jpegcomp = vidioc_s_jpegcomp, 1408 .vidioc_g_parm = vidioc_g_parm, 1409 .vidioc_s_parm = vidioc_s_parm, 1410 .vidioc_enum_framesizes = vidioc_enum_framesizes, 1411 .vidioc_enum_frameintervals = vidioc_enum_frameintervals, 1412 1413 .vidioc_reqbufs = vb2_ioctl_reqbufs, 1414 .vidioc_create_bufs = vb2_ioctl_create_bufs, 1415 .vidioc_querybuf = vb2_ioctl_querybuf, 1416 .vidioc_qbuf = vb2_ioctl_qbuf, 1417 .vidioc_dqbuf = vb2_ioctl_dqbuf, 1418 .vidioc_expbuf = vb2_ioctl_expbuf, 1419 .vidioc_streamon = vb2_ioctl_streamon, 1420 .vidioc_streamoff = vb2_ioctl_streamoff, 1421 1422 #ifdef CONFIG_VIDEO_ADV_DEBUG 1423 .vidioc_g_chip_info = vidioc_g_chip_info, 1424 .vidioc_g_register = vidioc_g_register, 1425 .vidioc_s_register = vidioc_s_register, 1426 #endif 1427 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 1428 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 1429 }; 1430 1431 static const struct video_device gspca_template = { 1432 .name = "gspca main driver", 1433 .fops = &dev_fops, 1434 .ioctl_ops = &dev_ioctl_ops, 1435 .release = video_device_release_empty, /* We use v4l2_dev.release */ 1436 }; 1437 1438 /* 1439 * probe and create a new gspca device 1440 * 1441 * This function must be called by the sub-driver when it is 1442 * called for probing a new device. 1443 */ 1444 int gspca_dev_probe2(struct usb_interface *intf, 1445 const struct usb_device_id *id, 1446 const struct sd_desc *sd_desc, 1447 int dev_size, 1448 struct module *module) 1449 { 1450 struct gspca_dev *gspca_dev; 1451 struct usb_device *dev = interface_to_usbdev(intf); 1452 struct vb2_queue *q; 1453 int ret; 1454 1455 pr_info("%s-" GSPCA_VERSION " probing %04x:%04x\n", 1456 sd_desc->name, id->idVendor, id->idProduct); 1457 1458 /* create the device */ 1459 if (dev_size < sizeof *gspca_dev) 1460 dev_size = sizeof *gspca_dev; 1461 gspca_dev = kzalloc(dev_size, GFP_KERNEL); 1462 if (!gspca_dev) { 1463 pr_err("couldn't kzalloc gspca struct\n"); 1464 return -ENOMEM; 1465 } 1466 gspca_dev->usb_buf = kzalloc(USB_BUF_SZ, GFP_KERNEL); 1467 if (!gspca_dev->usb_buf) { 1468 pr_err("out of memory\n"); 1469 ret = -ENOMEM; 1470 goto out; 1471 } 1472 gspca_dev->dev = dev; 1473 gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber; 1474 gspca_dev->xfer_ep = -1; 1475 1476 /* check if any audio device */ 1477 if (dev->actconfig->desc.bNumInterfaces != 1) { 1478 int i; 1479 struct usb_interface *intf2; 1480 1481 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) { 1482 intf2 = dev->actconfig->interface[i]; 1483 if (intf2 != NULL 1484 && intf2->altsetting != NULL 1485 && intf2->altsetting->desc.bInterfaceClass == 1486 USB_CLASS_AUDIO) { 1487 gspca_dev->audio = 1; 1488 break; 1489 } 1490 } 1491 } 1492 1493 gspca_dev->v4l2_dev.release = gspca_release; 1494 ret = v4l2_device_register(&intf->dev, &gspca_dev->v4l2_dev); 1495 if (ret) 1496 goto out; 1497 gspca_dev->present = true; 1498 gspca_dev->sd_desc = sd_desc; 1499 gspca_dev->empty_packet = -1; /* don't check the empty packets */ 1500 gspca_dev->vdev = gspca_template; 1501 gspca_dev->vdev.v4l2_dev = &gspca_dev->v4l2_dev; 1502 gspca_dev->vdev.device_caps = V4L2_CAP_VIDEO_CAPTURE | 1503 V4L2_CAP_STREAMING | V4L2_CAP_READWRITE; 1504 video_set_drvdata(&gspca_dev->vdev, gspca_dev); 1505 gspca_dev->module = module; 1506 1507 mutex_init(&gspca_dev->usb_lock); 1508 gspca_dev->vdev.lock = &gspca_dev->usb_lock; 1509 init_waitqueue_head(&gspca_dev->wq); 1510 1511 /* Initialize the vb2 queue */ 1512 q = &gspca_dev->queue; 1513 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 1514 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ; 1515 q->drv_priv = gspca_dev; 1516 q->buf_struct_size = sizeof(struct gspca_buffer); 1517 q->ops = &gspca_qops; 1518 q->mem_ops = &vb2_vmalloc_memops; 1519 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1520 q->min_queued_buffers = 2; 1521 q->lock = &gspca_dev->usb_lock; 1522 ret = vb2_queue_init(q); 1523 if (ret) 1524 goto out; 1525 gspca_dev->vdev.queue = q; 1526 1527 INIT_LIST_HEAD(&gspca_dev->buf_list); 1528 spin_lock_init(&gspca_dev->qlock); 1529 1530 /* configure the subdriver and initialize the USB device */ 1531 ret = sd_desc->config(gspca_dev, id); 1532 if (ret < 0) 1533 goto out; 1534 ret = sd_desc->init(gspca_dev); 1535 if (ret < 0) 1536 goto out; 1537 if (sd_desc->init_controls) 1538 ret = sd_desc->init_controls(gspca_dev); 1539 if (ret < 0) 1540 goto out; 1541 gspca_set_default_mode(gspca_dev); 1542 1543 ret = gspca_input_connect(gspca_dev); 1544 if (ret) 1545 goto out; 1546 1547 #ifdef CONFIG_VIDEO_ADV_DEBUG 1548 if (!gspca_dev->sd_desc->get_register) 1549 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_G_REGISTER); 1550 if (!gspca_dev->sd_desc->set_register) 1551 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_S_REGISTER); 1552 #endif 1553 if (!gspca_dev->sd_desc->get_jcomp) 1554 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_G_JPEGCOMP); 1555 if (!gspca_dev->sd_desc->set_jcomp) 1556 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_S_JPEGCOMP); 1557 1558 /* init video stuff */ 1559 ret = video_register_device(&gspca_dev->vdev, 1560 VFL_TYPE_VIDEO, 1561 -1); 1562 if (ret < 0) { 1563 pr_err("video_register_device err %d\n", ret); 1564 goto out; 1565 } 1566 1567 usb_set_intfdata(intf, gspca_dev); 1568 gspca_dbg(gspca_dev, D_PROBE, "%s created\n", 1569 video_device_node_name(&gspca_dev->vdev)); 1570 1571 gspca_input_create_urb(gspca_dev); 1572 1573 return 0; 1574 out: 1575 #if IS_ENABLED(CONFIG_INPUT) 1576 if (gspca_dev->input_dev) 1577 input_unregister_device(gspca_dev->input_dev); 1578 #endif 1579 v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler); 1580 v4l2_device_unregister(&gspca_dev->v4l2_dev); 1581 if (sd_desc->probe_error) 1582 sd_desc->probe_error(gspca_dev); 1583 kfree(gspca_dev->usb_buf); 1584 kfree(gspca_dev); 1585 return ret; 1586 } 1587 EXPORT_SYMBOL(gspca_dev_probe2); 1588 1589 /* same function as the previous one, but check the interface */ 1590 int gspca_dev_probe(struct usb_interface *intf, 1591 const struct usb_device_id *id, 1592 const struct sd_desc *sd_desc, 1593 int dev_size, 1594 struct module *module) 1595 { 1596 struct usb_device *dev = interface_to_usbdev(intf); 1597 1598 /* we don't handle multi-config cameras */ 1599 if (dev->descriptor.bNumConfigurations != 1) { 1600 pr_err("%04x:%04x too many config\n", 1601 id->idVendor, id->idProduct); 1602 return -ENODEV; 1603 } 1604 1605 /* the USB video interface must be the first one */ 1606 if (dev->actconfig->desc.bNumInterfaces != 1 1607 && intf->cur_altsetting->desc.bInterfaceNumber != 0) 1608 return -ENODEV; 1609 1610 return gspca_dev_probe2(intf, id, sd_desc, dev_size, module); 1611 } 1612 EXPORT_SYMBOL(gspca_dev_probe); 1613 1614 /* 1615 * USB disconnection 1616 * 1617 * This function must be called by the sub-driver 1618 * when the device disconnects, after the specific resources are freed. 1619 */ 1620 void gspca_disconnect(struct usb_interface *intf) 1621 { 1622 struct gspca_dev *gspca_dev = usb_get_intfdata(intf); 1623 #if IS_ENABLED(CONFIG_INPUT) 1624 struct input_dev *input_dev; 1625 #endif 1626 1627 gspca_dbg(gspca_dev, D_PROBE, "%s disconnect\n", 1628 video_device_node_name(&gspca_dev->vdev)); 1629 1630 mutex_lock(&gspca_dev->usb_lock); 1631 gspca_dev->present = false; 1632 destroy_urbs(gspca_dev); 1633 gspca_input_destroy_urb(gspca_dev); 1634 1635 vb2_queue_error(&gspca_dev->queue); 1636 1637 #if IS_ENABLED(CONFIG_INPUT) 1638 input_dev = gspca_dev->input_dev; 1639 if (input_dev) { 1640 gspca_dev->input_dev = NULL; 1641 input_unregister_device(input_dev); 1642 } 1643 #endif 1644 1645 v4l2_device_disconnect(&gspca_dev->v4l2_dev); 1646 video_unregister_device(&gspca_dev->vdev); 1647 1648 mutex_unlock(&gspca_dev->usb_lock); 1649 1650 /* (this will call gspca_release() immediately or on last close) */ 1651 v4l2_device_put(&gspca_dev->v4l2_dev); 1652 } 1653 EXPORT_SYMBOL(gspca_disconnect); 1654 1655 #ifdef CONFIG_PM 1656 int gspca_suspend(struct usb_interface *intf, pm_message_t message) 1657 { 1658 struct gspca_dev *gspca_dev = usb_get_intfdata(intf); 1659 1660 gspca_input_destroy_urb(gspca_dev); 1661 1662 if (!vb2_start_streaming_called(&gspca_dev->queue)) 1663 return 0; 1664 1665 mutex_lock(&gspca_dev->usb_lock); 1666 gspca_dev->frozen = 1; /* avoid urb error messages */ 1667 gspca_dev->usb_err = 0; 1668 if (gspca_dev->sd_desc->stopN) 1669 gspca_dev->sd_desc->stopN(gspca_dev); 1670 destroy_urbs(gspca_dev); 1671 gspca_set_alt0(gspca_dev); 1672 if (gspca_dev->sd_desc->stop0) 1673 gspca_dev->sd_desc->stop0(gspca_dev); 1674 mutex_unlock(&gspca_dev->usb_lock); 1675 1676 return 0; 1677 } 1678 EXPORT_SYMBOL(gspca_suspend); 1679 1680 int gspca_resume(struct usb_interface *intf) 1681 { 1682 struct gspca_dev *gspca_dev = usb_get_intfdata(intf); 1683 int streaming, ret = 0; 1684 1685 mutex_lock(&gspca_dev->usb_lock); 1686 gspca_dev->frozen = 0; 1687 gspca_dev->usb_err = 0; 1688 gspca_dev->sd_desc->init(gspca_dev); 1689 /* 1690 * Most subdrivers send all ctrl values on sd_start and thus 1691 * only write to the device registers on s_ctrl when streaming -> 1692 * Clear streaming to avoid setting all ctrls twice. 1693 */ 1694 streaming = vb2_start_streaming_called(&gspca_dev->queue); 1695 if (streaming) 1696 ret = gspca_init_transfer(gspca_dev); 1697 else 1698 gspca_input_create_urb(gspca_dev); 1699 mutex_unlock(&gspca_dev->usb_lock); 1700 1701 return ret; 1702 } 1703 EXPORT_SYMBOL(gspca_resume); 1704 #endif 1705 1706 /* -- module insert / remove -- */ 1707 static int __init gspca_init(void) 1708 { 1709 pr_info("v" GSPCA_VERSION " registered\n"); 1710 return 0; 1711 } 1712 static void __exit gspca_exit(void) 1713 { 1714 } 1715 1716 module_init(gspca_init); 1717 module_exit(gspca_exit); 1718 1719 module_param_named(debug, gspca_debug, int, 0644); 1720 MODULE_PARM_DESC(debug, 1721 "1:probe 2:config 3:stream 4:frame 5:packet 6:usbi 7:usbo"); 1722