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