1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Mirics MSi2500 driver 4 * Mirics MSi3101 SDR Dongle driver 5 * 6 * Copyright (C) 2013 Antti Palosaari <crope@iki.fi> 7 * 8 * That driver is somehow based of pwc driver: 9 * (C) 1999-2004 Nemosoft Unv. 10 * (C) 2004-2006 Luc Saillard (luc@saillard.org) 11 * (C) 2011 Hans de Goede <hdegoede@redhat.com> 12 */ 13 14 #include <linux/module.h> 15 #include <linux/slab.h> 16 #include <asm/div64.h> 17 #include <media/v4l2-device.h> 18 #include <media/v4l2-ioctl.h> 19 #include <media/v4l2-ctrls.h> 20 #include <media/v4l2-event.h> 21 #include <linux/usb.h> 22 #include <media/videobuf2-v4l2.h> 23 #include <media/videobuf2-vmalloc.h> 24 #include <linux/spi/spi.h> 25 26 static bool msi2500_emulated_fmt; 27 module_param_named(emulated_formats, msi2500_emulated_fmt, bool, 0644); 28 MODULE_PARM_DESC(emulated_formats, "enable emulated formats (disappears in future)"); 29 30 /* 31 * iConfiguration 0 32 * bInterfaceNumber 0 33 * bAlternateSetting 1 34 * bNumEndpoints 1 35 * bEndpointAddress 0x81 EP 1 IN 36 * bmAttributes 1 37 * Transfer Type Isochronous 38 * wMaxPacketSize 0x1400 3x 1024 bytes 39 * bInterval 1 40 */ 41 #define MAX_ISO_BUFS (8) 42 #define ISO_FRAMES_PER_DESC (8) 43 #define ISO_MAX_FRAME_SIZE (3 * 1024) 44 #define ISO_BUFFER_SIZE (ISO_FRAMES_PER_DESC * ISO_MAX_FRAME_SIZE) 45 #define MAX_ISOC_ERRORS 20 46 47 /* 48 * TODO: These formats should be moved to V4L2 API. Formats are currently 49 * disabled from formats[] table, not visible to userspace. 50 */ 51 /* signed 12-bit */ 52 #define MSI2500_PIX_FMT_SDR_S12 v4l2_fourcc('D', 'S', '1', '2') 53 /* Mirics MSi2500 format 384 */ 54 #define MSI2500_PIX_FMT_SDR_MSI2500_384 v4l2_fourcc('M', '3', '8', '4') 55 56 static const struct v4l2_frequency_band bands[] = { 57 { 58 .tuner = 0, 59 .type = V4L2_TUNER_ADC, 60 .index = 0, 61 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS, 62 .rangelow = 1200000, 63 .rangehigh = 15000000, 64 }, 65 }; 66 67 /* stream formats */ 68 struct msi2500_format { 69 u32 pixelformat; 70 u32 buffersize; 71 }; 72 73 /* format descriptions for capture and preview */ 74 static struct msi2500_format formats[] = { 75 { 76 .pixelformat = V4L2_SDR_FMT_CS8, 77 .buffersize = 3 * 1008, 78 #if 0 79 }, { 80 .pixelformat = MSI2500_PIX_FMT_SDR_MSI2500_384, 81 }, { 82 .pixelformat = MSI2500_PIX_FMT_SDR_S12, 83 #endif 84 }, { 85 .pixelformat = V4L2_SDR_FMT_CS14LE, 86 .buffersize = 3 * 1008, 87 }, { 88 .pixelformat = V4L2_SDR_FMT_CU8, 89 .buffersize = 3 * 1008, 90 }, { 91 .pixelformat = V4L2_SDR_FMT_CU16LE, 92 .buffersize = 3 * 1008, 93 }, 94 }; 95 96 static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats); 97 98 /* intermediate buffers with raw data from the USB device */ 99 struct msi2500_frame_buf { 100 /* common v4l buffer stuff -- must be first */ 101 struct vb2_v4l2_buffer vb; 102 struct list_head list; 103 }; 104 105 struct msi2500_dev { 106 struct device *dev; 107 struct video_device vdev; 108 struct v4l2_device v4l2_dev; 109 struct v4l2_subdev *v4l2_subdev; 110 struct spi_controller *ctlr; 111 112 /* videobuf2 queue and queued buffers list */ 113 struct vb2_queue vb_queue; 114 struct list_head queued_bufs; 115 spinlock_t queued_bufs_lock; /* Protects queued_bufs */ 116 117 /* Note if taking both locks v4l2_lock must always be locked first! */ 118 struct mutex v4l2_lock; /* Protects everything else */ 119 struct mutex vb_queue_lock; /* Protects vb_queue and capt_file */ 120 121 /* Pointer to our usb_device, will be NULL after unplug */ 122 struct usb_device *udev; /* Both mutexes most be hold when setting! */ 123 124 unsigned int f_adc; 125 u32 pixelformat; 126 u32 buffersize; 127 unsigned int num_formats; 128 129 unsigned int isoc_errors; /* number of contiguous ISOC errors */ 130 unsigned int vb_full; /* vb is full and packets dropped */ 131 132 struct urb *urbs[MAX_ISO_BUFS]; 133 134 /* Controls */ 135 struct v4l2_ctrl_handler hdl; 136 137 u32 next_sample; /* for track lost packets */ 138 u32 sample; /* for sample rate calc */ 139 unsigned long jiffies_next; 140 }; 141 142 /* Private functions */ 143 static struct msi2500_frame_buf *msi2500_get_next_fill_buf( 144 struct msi2500_dev *dev) 145 { 146 unsigned long flags; 147 struct msi2500_frame_buf *buf = NULL; 148 149 spin_lock_irqsave(&dev->queued_bufs_lock, flags); 150 if (list_empty(&dev->queued_bufs)) 151 goto leave; 152 153 buf = list_entry(dev->queued_bufs.next, struct msi2500_frame_buf, list); 154 list_del(&buf->list); 155 leave: 156 spin_unlock_irqrestore(&dev->queued_bufs_lock, flags); 157 return buf; 158 } 159 160 /* 161 * +=========================================================================== 162 * | 00-1023 | USB packet type '504' 163 * +=========================================================================== 164 * | 00- 03 | sequence number of first sample in that USB packet 165 * +--------------------------------------------------------------------------- 166 * | 04- 15 | garbage 167 * +--------------------------------------------------------------------------- 168 * | 16-1023 | samples 169 * +--------------------------------------------------------------------------- 170 * signed 8-bit sample 171 * 504 * 2 = 1008 samples 172 * 173 * 174 * +=========================================================================== 175 * | 00-1023 | USB packet type '384' 176 * +=========================================================================== 177 * | 00- 03 | sequence number of first sample in that USB packet 178 * +--------------------------------------------------------------------------- 179 * | 04- 15 | garbage 180 * +--------------------------------------------------------------------------- 181 * | 16- 175 | samples 182 * +--------------------------------------------------------------------------- 183 * | 176- 179 | control bits for previous samples 184 * +--------------------------------------------------------------------------- 185 * | 180- 339 | samples 186 * +--------------------------------------------------------------------------- 187 * | 340- 343 | control bits for previous samples 188 * +--------------------------------------------------------------------------- 189 * | 344- 503 | samples 190 * +--------------------------------------------------------------------------- 191 * | 504- 507 | control bits for previous samples 192 * +--------------------------------------------------------------------------- 193 * | 508- 667 | samples 194 * +--------------------------------------------------------------------------- 195 * | 668- 671 | control bits for previous samples 196 * +--------------------------------------------------------------------------- 197 * | 672- 831 | samples 198 * +--------------------------------------------------------------------------- 199 * | 832- 835 | control bits for previous samples 200 * +--------------------------------------------------------------------------- 201 * | 836- 995 | samples 202 * +--------------------------------------------------------------------------- 203 * | 996- 999 | control bits for previous samples 204 * +--------------------------------------------------------------------------- 205 * | 1000-1023 | garbage 206 * +--------------------------------------------------------------------------- 207 * 208 * Bytes 4 - 7 could have some meaning? 209 * 210 * Control bits for previous samples is 32-bit field, containing 16 x 2-bit 211 * numbers. This results one 2-bit number for 8 samples. It is likely used for 212 * bit shifting sample by given bits, increasing actual sampling resolution. 213 * Number 2 (0b10) was never seen. 214 * 215 * 6 * 16 * 2 * 4 = 768 samples. 768 * 4 = 3072 bytes 216 * 217 * 218 * +=========================================================================== 219 * | 00-1023 | USB packet type '336' 220 * +=========================================================================== 221 * | 00- 03 | sequence number of first sample in that USB packet 222 * +--------------------------------------------------------------------------- 223 * | 04- 15 | garbage 224 * +--------------------------------------------------------------------------- 225 * | 16-1023 | samples 226 * +--------------------------------------------------------------------------- 227 * signed 12-bit sample 228 * 229 * 230 * +=========================================================================== 231 * | 00-1023 | USB packet type '252' 232 * +=========================================================================== 233 * | 00- 03 | sequence number of first sample in that USB packet 234 * +--------------------------------------------------------------------------- 235 * | 04- 15 | garbage 236 * +--------------------------------------------------------------------------- 237 * | 16-1023 | samples 238 * +--------------------------------------------------------------------------- 239 * signed 14-bit sample 240 */ 241 242 static int msi2500_convert_stream(struct msi2500_dev *dev, u8 *dst, u8 *src, 243 unsigned int src_len) 244 { 245 unsigned int i, j, transactions, dst_len = 0; 246 u32 sample[3]; 247 248 /* There could be 1-3 1024 byte transactions per packet */ 249 transactions = src_len / 1024; 250 251 for (i = 0; i < transactions; i++) { 252 sample[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | 253 src[0] << 0; 254 if (i == 0 && dev->next_sample != sample[0]) { 255 dev_dbg_ratelimited(dev->dev, 256 "%d samples lost, %d %08x:%08x\n", 257 sample[0] - dev->next_sample, 258 src_len, dev->next_sample, 259 sample[0]); 260 } 261 262 /* 263 * Dump all unknown 'garbage' data - maybe we will discover 264 * someday if there is something rational... 265 */ 266 dev_dbg_ratelimited(dev->dev, "%*ph\n", 12, &src[4]); 267 268 src += 16; /* skip header */ 269 270 switch (dev->pixelformat) { 271 case V4L2_SDR_FMT_CU8: /* 504 x IQ samples */ 272 { 273 s8 *s8src = (s8 *)src; 274 u8 *u8dst = (u8 *)dst; 275 276 for (j = 0; j < 1008; j++) 277 *u8dst++ = *s8src++ + 128; 278 279 src += 1008; 280 dst += 1008; 281 dst_len += 1008; 282 dev->next_sample = sample[i] + 504; 283 break; 284 } 285 case V4L2_SDR_FMT_CU16LE: /* 252 x IQ samples */ 286 { 287 s16 *s16src = (s16 *)src; 288 u16 *u16dst = (u16 *)dst; 289 struct {signed int x:14; } se; /* sign extension */ 290 unsigned int utmp; 291 292 for (j = 0; j < 1008; j += 2) { 293 /* sign extension from 14-bit to signed int */ 294 se.x = *s16src++; 295 /* from signed int to unsigned int */ 296 utmp = se.x + 8192; 297 /* from 14-bit to 16-bit */ 298 *u16dst++ = utmp << 2 | utmp >> 12; 299 } 300 301 src += 1008; 302 dst += 1008; 303 dst_len += 1008; 304 dev->next_sample = sample[i] + 252; 305 break; 306 } 307 case MSI2500_PIX_FMT_SDR_MSI2500_384: /* 384 x IQ samples */ 308 /* Dump unknown 'garbage' data */ 309 dev_dbg_ratelimited(dev->dev, "%*ph\n", 24, &src[1000]); 310 memcpy(dst, src, 984); 311 src += 984 + 24; 312 dst += 984; 313 dst_len += 984; 314 dev->next_sample = sample[i] + 384; 315 break; 316 case V4L2_SDR_FMT_CS8: /* 504 x IQ samples */ 317 memcpy(dst, src, 1008); 318 src += 1008; 319 dst += 1008; 320 dst_len += 1008; 321 dev->next_sample = sample[i] + 504; 322 break; 323 case MSI2500_PIX_FMT_SDR_S12: /* 336 x IQ samples */ 324 memcpy(dst, src, 1008); 325 src += 1008; 326 dst += 1008; 327 dst_len += 1008; 328 dev->next_sample = sample[i] + 336; 329 break; 330 case V4L2_SDR_FMT_CS14LE: /* 252 x IQ samples */ 331 memcpy(dst, src, 1008); 332 src += 1008; 333 dst += 1008; 334 dst_len += 1008; 335 dev->next_sample = sample[i] + 252; 336 break; 337 default: 338 break; 339 } 340 } 341 342 /* calculate sample rate and output it in 10 seconds intervals */ 343 if (unlikely(time_is_before_jiffies(dev->jiffies_next))) { 344 #define MSECS 10000UL 345 unsigned int msecs = jiffies_to_msecs(jiffies - 346 dev->jiffies_next + msecs_to_jiffies(MSECS)); 347 unsigned int samples = dev->next_sample - dev->sample; 348 349 dev->jiffies_next = jiffies + msecs_to_jiffies(MSECS); 350 dev->sample = dev->next_sample; 351 dev_dbg(dev->dev, "size=%u samples=%u msecs=%u sample rate=%lu\n", 352 src_len, samples, msecs, 353 samples * 1000UL / msecs); 354 } 355 356 return dst_len; 357 } 358 359 /* 360 * This gets called for the Isochronous pipe (stream). This is done in interrupt 361 * time, so it has to be fast, not crash, and not stall. Neat. 362 */ 363 static void msi2500_isoc_handler(struct urb *urb) 364 { 365 struct msi2500_dev *dev = (struct msi2500_dev *)urb->context; 366 int i, flen, fstatus; 367 unsigned char *iso_buf = NULL; 368 struct msi2500_frame_buf *fbuf; 369 370 if (unlikely(urb->status == -ENOENT || 371 urb->status == -ECONNRESET || 372 urb->status == -ESHUTDOWN)) { 373 dev_dbg(dev->dev, "URB (%p) unlinked %ssynchronously\n", 374 urb, urb->status == -ENOENT ? "" : "a"); 375 return; 376 } 377 378 if (unlikely(urb->status != 0)) { 379 dev_dbg(dev->dev, "called with status %d\n", urb->status); 380 /* Give up after a number of contiguous errors */ 381 if (++dev->isoc_errors > MAX_ISOC_ERRORS) 382 dev_dbg(dev->dev, "Too many ISOC errors, bailing out\n"); 383 goto handler_end; 384 } else { 385 /* Reset ISOC error counter. We did get here, after all. */ 386 dev->isoc_errors = 0; 387 } 388 389 /* Compact data */ 390 for (i = 0; i < urb->number_of_packets; i++) { 391 void *ptr; 392 393 /* Check frame error */ 394 fstatus = urb->iso_frame_desc[i].status; 395 if (unlikely(fstatus)) { 396 dev_dbg_ratelimited(dev->dev, 397 "frame=%d/%d has error %d skipping\n", 398 i, urb->number_of_packets, fstatus); 399 continue; 400 } 401 402 /* Check if that frame contains data */ 403 flen = urb->iso_frame_desc[i].actual_length; 404 if (unlikely(flen == 0)) 405 continue; 406 407 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset; 408 409 /* Get free framebuffer */ 410 fbuf = msi2500_get_next_fill_buf(dev); 411 if (unlikely(fbuf == NULL)) { 412 dev->vb_full++; 413 dev_dbg_ratelimited(dev->dev, 414 "video buffer is full, %d packets dropped\n", 415 dev->vb_full); 416 continue; 417 } 418 419 /* fill framebuffer */ 420 ptr = vb2_plane_vaddr(&fbuf->vb.vb2_buf, 0); 421 flen = msi2500_convert_stream(dev, ptr, iso_buf, flen); 422 vb2_set_plane_payload(&fbuf->vb.vb2_buf, 0, flen); 423 vb2_buffer_done(&fbuf->vb.vb2_buf, VB2_BUF_STATE_DONE); 424 } 425 426 handler_end: 427 i = usb_submit_urb(urb, GFP_ATOMIC); 428 if (unlikely(i != 0)) 429 dev_dbg(dev->dev, "Error (%d) re-submitting urb\n", i); 430 } 431 432 static void msi2500_iso_stop(struct msi2500_dev *dev) 433 { 434 int i; 435 436 dev_dbg(dev->dev, "\n"); 437 438 /* Unlinking ISOC buffers one by one */ 439 for (i = 0; i < MAX_ISO_BUFS; i++) { 440 if (dev->urbs[i]) { 441 dev_dbg(dev->dev, "Unlinking URB %p\n", dev->urbs[i]); 442 usb_kill_urb(dev->urbs[i]); 443 } 444 } 445 } 446 447 static void msi2500_iso_free(struct msi2500_dev *dev) 448 { 449 int i; 450 451 dev_dbg(dev->dev, "\n"); 452 453 /* Freeing ISOC buffers one by one */ 454 for (i = 0; i < MAX_ISO_BUFS; i++) { 455 if (dev->urbs[i]) { 456 dev_dbg(dev->dev, "Freeing URB\n"); 457 if (dev->urbs[i]->transfer_buffer) { 458 usb_free_coherent(dev->udev, 459 dev->urbs[i]->transfer_buffer_length, 460 dev->urbs[i]->transfer_buffer, 461 dev->urbs[i]->transfer_dma); 462 } 463 usb_free_urb(dev->urbs[i]); 464 dev->urbs[i] = NULL; 465 } 466 } 467 } 468 469 /* Both v4l2_lock and vb_queue_lock should be locked when calling this */ 470 static void msi2500_isoc_cleanup(struct msi2500_dev *dev) 471 { 472 dev_dbg(dev->dev, "\n"); 473 474 msi2500_iso_stop(dev); 475 msi2500_iso_free(dev); 476 } 477 478 /* Both v4l2_lock and vb_queue_lock should be locked when calling this */ 479 static int msi2500_isoc_init(struct msi2500_dev *dev) 480 { 481 struct urb *urb; 482 int i, j, ret; 483 484 dev_dbg(dev->dev, "\n"); 485 486 dev->isoc_errors = 0; 487 488 ret = usb_set_interface(dev->udev, 0, 1); 489 if (ret) 490 return ret; 491 492 /* Allocate and init Isochronuous urbs */ 493 for (i = 0; i < MAX_ISO_BUFS; i++) { 494 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL); 495 if (urb == NULL) { 496 msi2500_isoc_cleanup(dev); 497 return -ENOMEM; 498 } 499 dev->urbs[i] = urb; 500 dev_dbg(dev->dev, "Allocated URB at 0x%p\n", urb); 501 502 urb->interval = 1; 503 urb->dev = dev->udev; 504 urb->pipe = usb_rcvisocpipe(dev->udev, 0x81); 505 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP; 506 urb->transfer_buffer = usb_alloc_coherent(dev->udev, 507 ISO_BUFFER_SIZE, 508 GFP_KERNEL, &urb->transfer_dma); 509 if (urb->transfer_buffer == NULL) { 510 dev_err(dev->dev, 511 "Failed to allocate urb buffer %d\n", i); 512 msi2500_isoc_cleanup(dev); 513 return -ENOMEM; 514 } 515 urb->transfer_buffer_length = ISO_BUFFER_SIZE; 516 urb->complete = msi2500_isoc_handler; 517 urb->context = dev; 518 urb->start_frame = 0; 519 urb->number_of_packets = ISO_FRAMES_PER_DESC; 520 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) { 521 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE; 522 urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE; 523 } 524 } 525 526 /* link */ 527 for (i = 0; i < MAX_ISO_BUFS; i++) { 528 ret = usb_submit_urb(dev->urbs[i], GFP_KERNEL); 529 if (ret) { 530 dev_err(dev->dev, 531 "usb_submit_urb %d failed with error %d\n", 532 i, ret); 533 msi2500_isoc_cleanup(dev); 534 return ret; 535 } 536 dev_dbg(dev->dev, "URB 0x%p submitted.\n", dev->urbs[i]); 537 } 538 539 /* All is done... */ 540 return 0; 541 } 542 543 /* Must be called with vb_queue_lock hold */ 544 static void msi2500_cleanup_queued_bufs(struct msi2500_dev *dev, 545 enum vb2_buffer_state state) 546 { 547 unsigned long flags; 548 549 dev_dbg(dev->dev, "\n"); 550 551 spin_lock_irqsave(&dev->queued_bufs_lock, flags); 552 while (!list_empty(&dev->queued_bufs)) { 553 struct msi2500_frame_buf *buf; 554 555 buf = list_entry(dev->queued_bufs.next, 556 struct msi2500_frame_buf, list); 557 list_del(&buf->list); 558 vb2_buffer_done(&buf->vb.vb2_buf, state); 559 } 560 spin_unlock_irqrestore(&dev->queued_bufs_lock, flags); 561 } 562 563 /* The user yanked out the cable... */ 564 static void msi2500_disconnect(struct usb_interface *intf) 565 { 566 struct v4l2_device *v = usb_get_intfdata(intf); 567 struct msi2500_dev *dev = 568 container_of(v, struct msi2500_dev, v4l2_dev); 569 570 dev_dbg(dev->dev, "\n"); 571 572 mutex_lock(&dev->vb_queue_lock); 573 mutex_lock(&dev->v4l2_lock); 574 /* No need to keep the urbs around after disconnection */ 575 dev->udev = NULL; 576 v4l2_device_disconnect(&dev->v4l2_dev); 577 video_unregister_device(&dev->vdev); 578 spi_unregister_controller(dev->ctlr); 579 spi_controller_put(dev->ctlr); 580 mutex_unlock(&dev->v4l2_lock); 581 mutex_unlock(&dev->vb_queue_lock); 582 583 v4l2_device_put(&dev->v4l2_dev); 584 } 585 586 static int msi2500_querycap(struct file *file, void *fh, 587 struct v4l2_capability *cap) 588 { 589 struct msi2500_dev *dev = video_drvdata(file); 590 591 dev_dbg(dev->dev, "\n"); 592 593 strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver)); 594 strscpy(cap->card, dev->vdev.name, sizeof(cap->card)); 595 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info)); 596 return 0; 597 } 598 599 /* Videobuf2 operations */ 600 static int msi2500_queue_setup(struct vb2_queue *vq, 601 unsigned int *nbuffers, 602 unsigned int *nplanes, unsigned int sizes[], 603 struct device *alloc_devs[]) 604 { 605 struct msi2500_dev *dev = vb2_get_drv_priv(vq); 606 607 dev_dbg(dev->dev, "nbuffers=%d\n", *nbuffers); 608 609 /* Absolute min and max number of buffers available for mmap() */ 610 *nbuffers = clamp_t(unsigned int, *nbuffers, 8, 32); 611 *nplanes = 1; 612 sizes[0] = PAGE_ALIGN(dev->buffersize); 613 dev_dbg(dev->dev, "nbuffers=%d sizes[0]=%d\n", *nbuffers, sizes[0]); 614 return 0; 615 } 616 617 static void msi2500_buf_queue(struct vb2_buffer *vb) 618 { 619 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 620 struct msi2500_dev *dev = vb2_get_drv_priv(vb->vb2_queue); 621 struct msi2500_frame_buf *buf = container_of(vbuf, 622 struct msi2500_frame_buf, 623 vb); 624 unsigned long flags; 625 626 /* Check the device has not disconnected between prep and queuing */ 627 if (unlikely(!dev->udev)) { 628 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); 629 return; 630 } 631 632 spin_lock_irqsave(&dev->queued_bufs_lock, flags); 633 list_add_tail(&buf->list, &dev->queued_bufs); 634 spin_unlock_irqrestore(&dev->queued_bufs_lock, flags); 635 } 636 637 #define CMD_WREG 0x41 638 #define CMD_START_STREAMING 0x43 639 #define CMD_STOP_STREAMING 0x45 640 #define CMD_READ_UNKNOWN 0x48 641 642 #define msi2500_dbg_usb_control_msg(_dev, _r, _t, _v, _i, _b, _l) { \ 643 char *_direction; \ 644 if (_t & USB_DIR_IN) \ 645 _direction = "<<<"; \ 646 else \ 647 _direction = ">>>"; \ 648 dev_dbg(_dev, "%02x %02x %02x %02x %02x %02x %02x %02x %s %*ph\n", \ 649 _t, _r, _v & 0xff, _v >> 8, _i & 0xff, _i >> 8, \ 650 _l & 0xff, _l >> 8, _direction, _l, _b); \ 651 } 652 653 static int msi2500_ctrl_msg(struct msi2500_dev *dev, u8 cmd, u32 data) 654 { 655 int ret; 656 u8 request = cmd; 657 u8 requesttype = USB_DIR_OUT | USB_TYPE_VENDOR; 658 u16 value = (data >> 0) & 0xffff; 659 u16 index = (data >> 16) & 0xffff; 660 661 msi2500_dbg_usb_control_msg(dev->dev, request, requesttype, 662 value, index, NULL, 0); 663 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0), request, 664 requesttype, value, index, NULL, 0, 2000); 665 if (ret) 666 dev_err(dev->dev, "failed %d, cmd %02x, data %04x\n", 667 ret, cmd, data); 668 669 return ret; 670 } 671 672 static int msi2500_set_usb_adc(struct msi2500_dev *dev) 673 { 674 int ret; 675 unsigned int f_vco, f_sr, div_n, k, k_cw, div_out; 676 u32 reg3, reg4, reg7; 677 struct v4l2_ctrl *bandwidth_auto; 678 struct v4l2_ctrl *bandwidth; 679 680 f_sr = dev->f_adc; 681 682 /* set tuner, subdev, filters according to sampling rate */ 683 bandwidth_auto = v4l2_ctrl_find(&dev->hdl, 684 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO); 685 if (v4l2_ctrl_g_ctrl(bandwidth_auto)) { 686 bandwidth = v4l2_ctrl_find(&dev->hdl, 687 V4L2_CID_RF_TUNER_BANDWIDTH); 688 v4l2_ctrl_s_ctrl(bandwidth, dev->f_adc); 689 } 690 691 /* select stream format */ 692 switch (dev->pixelformat) { 693 case V4L2_SDR_FMT_CU8: 694 reg7 = 0x000c9407; /* 504 */ 695 break; 696 case V4L2_SDR_FMT_CU16LE: 697 reg7 = 0x00009407; /* 252 */ 698 break; 699 case V4L2_SDR_FMT_CS8: 700 reg7 = 0x000c9407; /* 504 */ 701 break; 702 case MSI2500_PIX_FMT_SDR_MSI2500_384: 703 reg7 = 0x0000a507; /* 384 */ 704 break; 705 case MSI2500_PIX_FMT_SDR_S12: 706 reg7 = 0x00008507; /* 336 */ 707 break; 708 case V4L2_SDR_FMT_CS14LE: 709 reg7 = 0x00009407; /* 252 */ 710 break; 711 default: 712 reg7 = 0x000c9407; /* 504 */ 713 break; 714 } 715 716 /* 717 * Fractional-N synthesizer 718 * 719 * +----------------------------------------+ 720 * v | 721 * Fref +----+ +-------+ +-----+ +------+ +---+ 722 * ------> | PD | --> | VCO | --> | /2 | ------> | /N.F | <-- | K | 723 * +----+ +-------+ +-----+ +------+ +---+ 724 * | 725 * | 726 * v 727 * +-------+ +-----+ Fout 728 * | /Rout | --> | /12 | ------> 729 * +-------+ +-----+ 730 */ 731 /* 732 * Synthesizer config is just a educated guess... 733 * 734 * [7:0] 0x03, register address 735 * [8] 1, power control 736 * [9] ?, power control 737 * [12:10] output divider 738 * [13] 0 ? 739 * [14] 0 ? 740 * [15] fractional MSB, bit 20 741 * [16:19] N 742 * [23:20] ? 743 * [24:31] 0x01 744 * 745 * output divider 746 * val div 747 * 0 - (invalid) 748 * 1 4 749 * 2 6 750 * 3 8 751 * 4 10 752 * 5 12 753 * 6 14 754 * 7 16 755 * 756 * VCO 202000000 - 720000000++ 757 */ 758 759 #define F_REF 24000000 760 #define DIV_PRE_N 2 761 #define DIV_LO_OUT 12 762 reg3 = 0x01000303; 763 reg4 = 0x00000004; 764 765 /* XXX: Filters? AGC? VCO band? */ 766 if (f_sr < 6000000) 767 reg3 |= 0x1 << 20; 768 else if (f_sr < 7000000) 769 reg3 |= 0x5 << 20; 770 else if (f_sr < 8500000) 771 reg3 |= 0x9 << 20; 772 else 773 reg3 |= 0xd << 20; 774 775 for (div_out = 4; div_out < 16; div_out += 2) { 776 f_vco = f_sr * div_out * DIV_LO_OUT; 777 dev_dbg(dev->dev, "div_out=%u f_vco=%u\n", div_out, f_vco); 778 if (f_vco >= 202000000) 779 break; 780 } 781 782 /* Calculate PLL integer and fractional control word. */ 783 div_n = div_u64_rem(f_vco, DIV_PRE_N * F_REF, &k); 784 k_cw = div_u64((u64) k * 0x200000, DIV_PRE_N * F_REF); 785 786 reg3 |= div_n << 16; 787 reg3 |= (div_out / 2 - 1) << 10; 788 reg3 |= ((k_cw >> 20) & 0x000001) << 15; /* [20] */ 789 reg4 |= ((k_cw >> 0) & 0x0fffff) << 8; /* [19:0] */ 790 791 dev_dbg(dev->dev, 792 "f_sr=%u f_vco=%u div_n=%u k=%u div_out=%u reg3=%08x reg4=%08x\n", 793 f_sr, f_vco, div_n, k, div_out, reg3, reg4); 794 795 ret = msi2500_ctrl_msg(dev, CMD_WREG, 0x00608008); 796 if (ret) 797 goto err; 798 799 ret = msi2500_ctrl_msg(dev, CMD_WREG, 0x00000c05); 800 if (ret) 801 goto err; 802 803 ret = msi2500_ctrl_msg(dev, CMD_WREG, 0x00020000); 804 if (ret) 805 goto err; 806 807 ret = msi2500_ctrl_msg(dev, CMD_WREG, 0x00480102); 808 if (ret) 809 goto err; 810 811 ret = msi2500_ctrl_msg(dev, CMD_WREG, 0x00f38008); 812 if (ret) 813 goto err; 814 815 ret = msi2500_ctrl_msg(dev, CMD_WREG, reg7); 816 if (ret) 817 goto err; 818 819 ret = msi2500_ctrl_msg(dev, CMD_WREG, reg4); 820 if (ret) 821 goto err; 822 823 ret = msi2500_ctrl_msg(dev, CMD_WREG, reg3); 824 err: 825 return ret; 826 } 827 828 static int msi2500_start_streaming(struct vb2_queue *vq, unsigned int count) 829 { 830 struct msi2500_dev *dev = vb2_get_drv_priv(vq); 831 int ret; 832 833 dev_dbg(dev->dev, "\n"); 834 835 if (!dev->udev) { 836 ret = -ENODEV; 837 goto err_cleanup; 838 } 839 840 if (mutex_lock_interruptible(&dev->v4l2_lock)) { 841 ret = -ERESTARTSYS; 842 goto err_cleanup; 843 } 844 845 /* wake-up tuner */ 846 v4l2_subdev_call(dev->v4l2_subdev, core, s_power, 1); 847 848 ret = msi2500_set_usb_adc(dev); 849 if (ret) 850 goto err_unlock_cleanup; 851 852 ret = msi2500_isoc_init(dev); 853 if (ret) 854 goto err_unlock_cleanup; 855 856 ret = msi2500_ctrl_msg(dev, CMD_START_STREAMING, 0); 857 if (ret) 858 goto err_isoc_cleanup; 859 860 mutex_unlock(&dev->v4l2_lock); 861 return 0; 862 863 err_isoc_cleanup: 864 msi2500_isoc_cleanup(dev); 865 err_unlock_cleanup: 866 mutex_unlock(&dev->v4l2_lock); 867 err_cleanup: 868 msi2500_cleanup_queued_bufs(dev, VB2_BUF_STATE_QUEUED); 869 return ret; 870 } 871 872 static void msi2500_stop_streaming(struct vb2_queue *vq) 873 { 874 struct msi2500_dev *dev = vb2_get_drv_priv(vq); 875 876 dev_dbg(dev->dev, "\n"); 877 878 mutex_lock(&dev->v4l2_lock); 879 880 if (dev->udev) 881 msi2500_isoc_cleanup(dev); 882 883 msi2500_cleanup_queued_bufs(dev, VB2_BUF_STATE_ERROR); 884 885 /* according to tests, at least 700us delay is required */ 886 msleep(20); 887 if (dev->udev && !msi2500_ctrl_msg(dev, CMD_STOP_STREAMING, 0)) { 888 /* sleep USB IF / ADC */ 889 msi2500_ctrl_msg(dev, CMD_WREG, 0x01000003); 890 } 891 892 /* sleep tuner */ 893 v4l2_subdev_call(dev->v4l2_subdev, core, s_power, 0); 894 895 mutex_unlock(&dev->v4l2_lock); 896 } 897 898 static const struct vb2_ops msi2500_vb2_ops = { 899 .queue_setup = msi2500_queue_setup, 900 .buf_queue = msi2500_buf_queue, 901 .start_streaming = msi2500_start_streaming, 902 .stop_streaming = msi2500_stop_streaming, 903 }; 904 905 static int msi2500_enum_fmt_sdr_cap(struct file *file, void *priv, 906 struct v4l2_fmtdesc *f) 907 { 908 struct msi2500_dev *dev = video_drvdata(file); 909 910 dev_dbg(dev->dev, "index=%d\n", f->index); 911 912 if (f->index >= dev->num_formats) 913 return -EINVAL; 914 915 f->pixelformat = formats[f->index].pixelformat; 916 917 return 0; 918 } 919 920 static int msi2500_g_fmt_sdr_cap(struct file *file, void *priv, 921 struct v4l2_format *f) 922 { 923 struct msi2500_dev *dev = video_drvdata(file); 924 925 dev_dbg(dev->dev, "pixelformat fourcc %4.4s\n", 926 (char *)&dev->pixelformat); 927 928 f->fmt.sdr.pixelformat = dev->pixelformat; 929 f->fmt.sdr.buffersize = dev->buffersize; 930 931 return 0; 932 } 933 934 static int msi2500_s_fmt_sdr_cap(struct file *file, void *priv, 935 struct v4l2_format *f) 936 { 937 struct msi2500_dev *dev = video_drvdata(file); 938 struct vb2_queue *q = &dev->vb_queue; 939 int i; 940 941 dev_dbg(dev->dev, "pixelformat fourcc %4.4s\n", 942 (char *)&f->fmt.sdr.pixelformat); 943 944 if (vb2_is_busy(q)) 945 return -EBUSY; 946 947 for (i = 0; i < dev->num_formats; i++) { 948 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) { 949 dev->pixelformat = formats[i].pixelformat; 950 dev->buffersize = formats[i].buffersize; 951 f->fmt.sdr.buffersize = formats[i].buffersize; 952 return 0; 953 } 954 } 955 956 dev->pixelformat = formats[0].pixelformat; 957 dev->buffersize = formats[0].buffersize; 958 f->fmt.sdr.pixelformat = formats[0].pixelformat; 959 f->fmt.sdr.buffersize = formats[0].buffersize; 960 961 return 0; 962 } 963 964 static int msi2500_try_fmt_sdr_cap(struct file *file, void *priv, 965 struct v4l2_format *f) 966 { 967 struct msi2500_dev *dev = video_drvdata(file); 968 int i; 969 970 dev_dbg(dev->dev, "pixelformat fourcc %4.4s\n", 971 (char *)&f->fmt.sdr.pixelformat); 972 973 for (i = 0; i < dev->num_formats; i++) { 974 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) { 975 f->fmt.sdr.buffersize = formats[i].buffersize; 976 return 0; 977 } 978 } 979 980 f->fmt.sdr.pixelformat = formats[0].pixelformat; 981 f->fmt.sdr.buffersize = formats[0].buffersize; 982 983 return 0; 984 } 985 986 static int msi2500_s_tuner(struct file *file, void *priv, 987 const struct v4l2_tuner *v) 988 { 989 struct msi2500_dev *dev = video_drvdata(file); 990 int ret; 991 992 dev_dbg(dev->dev, "index=%d\n", v->index); 993 994 if (v->index == 0) 995 ret = 0; 996 else if (v->index == 1) 997 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, s_tuner, v); 998 else 999 ret = -EINVAL; 1000 1001 return ret; 1002 } 1003 1004 static int msi2500_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v) 1005 { 1006 struct msi2500_dev *dev = video_drvdata(file); 1007 int ret; 1008 1009 dev_dbg(dev->dev, "index=%d\n", v->index); 1010 1011 if (v->index == 0) { 1012 strscpy(v->name, "Mirics MSi2500", sizeof(v->name)); 1013 v->type = V4L2_TUNER_ADC; 1014 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS; 1015 v->rangelow = 1200000; 1016 v->rangehigh = 15000000; 1017 ret = 0; 1018 } else if (v->index == 1) { 1019 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, g_tuner, v); 1020 } else { 1021 ret = -EINVAL; 1022 } 1023 1024 return ret; 1025 } 1026 1027 static int msi2500_g_frequency(struct file *file, void *priv, 1028 struct v4l2_frequency *f) 1029 { 1030 struct msi2500_dev *dev = video_drvdata(file); 1031 int ret = 0; 1032 1033 dev_dbg(dev->dev, "tuner=%d type=%d\n", f->tuner, f->type); 1034 1035 if (f->tuner == 0) { 1036 f->frequency = dev->f_adc; 1037 ret = 0; 1038 } else if (f->tuner == 1) { 1039 f->type = V4L2_TUNER_RF; 1040 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, g_frequency, f); 1041 } else { 1042 ret = -EINVAL; 1043 } 1044 1045 return ret; 1046 } 1047 1048 static int msi2500_s_frequency(struct file *file, void *priv, 1049 const struct v4l2_frequency *f) 1050 { 1051 struct msi2500_dev *dev = video_drvdata(file); 1052 int ret; 1053 1054 dev_dbg(dev->dev, "tuner=%d type=%d frequency=%u\n", 1055 f->tuner, f->type, f->frequency); 1056 1057 if (f->tuner == 0) { 1058 dev->f_adc = clamp_t(unsigned int, f->frequency, 1059 bands[0].rangelow, 1060 bands[0].rangehigh); 1061 dev_dbg(dev->dev, "ADC frequency=%u Hz\n", dev->f_adc); 1062 ret = msi2500_set_usb_adc(dev); 1063 } else if (f->tuner == 1) { 1064 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, s_frequency, f); 1065 } else { 1066 ret = -EINVAL; 1067 } 1068 1069 return ret; 1070 } 1071 1072 static int msi2500_enum_freq_bands(struct file *file, void *priv, 1073 struct v4l2_frequency_band *band) 1074 { 1075 struct msi2500_dev *dev = video_drvdata(file); 1076 int ret; 1077 1078 dev_dbg(dev->dev, "tuner=%d type=%d index=%d\n", 1079 band->tuner, band->type, band->index); 1080 1081 if (band->tuner == 0) { 1082 if (band->index >= ARRAY_SIZE(bands)) { 1083 ret = -EINVAL; 1084 } else { 1085 *band = bands[band->index]; 1086 ret = 0; 1087 } 1088 } else if (band->tuner == 1) { 1089 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, 1090 enum_freq_bands, band); 1091 } else { 1092 ret = -EINVAL; 1093 } 1094 1095 return ret; 1096 } 1097 1098 static const struct v4l2_ioctl_ops msi2500_ioctl_ops = { 1099 .vidioc_querycap = msi2500_querycap, 1100 1101 .vidioc_enum_fmt_sdr_cap = msi2500_enum_fmt_sdr_cap, 1102 .vidioc_g_fmt_sdr_cap = msi2500_g_fmt_sdr_cap, 1103 .vidioc_s_fmt_sdr_cap = msi2500_s_fmt_sdr_cap, 1104 .vidioc_try_fmt_sdr_cap = msi2500_try_fmt_sdr_cap, 1105 1106 .vidioc_reqbufs = vb2_ioctl_reqbufs, 1107 .vidioc_create_bufs = vb2_ioctl_create_bufs, 1108 .vidioc_prepare_buf = vb2_ioctl_prepare_buf, 1109 .vidioc_querybuf = vb2_ioctl_querybuf, 1110 .vidioc_qbuf = vb2_ioctl_qbuf, 1111 .vidioc_dqbuf = vb2_ioctl_dqbuf, 1112 1113 .vidioc_streamon = vb2_ioctl_streamon, 1114 .vidioc_streamoff = vb2_ioctl_streamoff, 1115 1116 .vidioc_g_tuner = msi2500_g_tuner, 1117 .vidioc_s_tuner = msi2500_s_tuner, 1118 1119 .vidioc_g_frequency = msi2500_g_frequency, 1120 .vidioc_s_frequency = msi2500_s_frequency, 1121 .vidioc_enum_freq_bands = msi2500_enum_freq_bands, 1122 1123 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 1124 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 1125 .vidioc_log_status = v4l2_ctrl_log_status, 1126 }; 1127 1128 static const struct v4l2_file_operations msi2500_fops = { 1129 .owner = THIS_MODULE, 1130 .open = v4l2_fh_open, 1131 .release = vb2_fop_release, 1132 .read = vb2_fop_read, 1133 .poll = vb2_fop_poll, 1134 .mmap = vb2_fop_mmap, 1135 .unlocked_ioctl = video_ioctl2, 1136 }; 1137 1138 static const struct video_device msi2500_template = { 1139 .name = "Mirics MSi3101 SDR Dongle", 1140 .release = video_device_release_empty, 1141 .fops = &msi2500_fops, 1142 .ioctl_ops = &msi2500_ioctl_ops, 1143 }; 1144 1145 static void msi2500_video_release(struct v4l2_device *v) 1146 { 1147 struct msi2500_dev *dev = container_of(v, struct msi2500_dev, v4l2_dev); 1148 1149 v4l2_ctrl_handler_free(&dev->hdl); 1150 v4l2_device_unregister(&dev->v4l2_dev); 1151 kfree(dev); 1152 } 1153 1154 static int msi2500_transfer_one_message(struct spi_controller *ctlr, 1155 struct spi_message *m) 1156 { 1157 struct msi2500_dev *dev = spi_controller_get_devdata(ctlr); 1158 struct spi_transfer *t; 1159 int ret = 0; 1160 u32 data; 1161 1162 list_for_each_entry(t, &m->transfers, transfer_list) { 1163 dev_dbg(dev->dev, "msg=%*ph\n", t->len, t->tx_buf); 1164 data = 0x09; /* reg 9 is SPI adapter */ 1165 data |= ((u8 *)t->tx_buf)[0] << 8; 1166 data |= ((u8 *)t->tx_buf)[1] << 16; 1167 data |= ((u8 *)t->tx_buf)[2] << 24; 1168 ret = msi2500_ctrl_msg(dev, CMD_WREG, data); 1169 } 1170 1171 m->status = ret; 1172 spi_finalize_current_message(ctlr); 1173 return ret; 1174 } 1175 1176 static int msi2500_probe(struct usb_interface *intf, 1177 const struct usb_device_id *id) 1178 { 1179 struct msi2500_dev *dev; 1180 struct v4l2_subdev *sd; 1181 struct spi_controller *ctlr; 1182 int ret; 1183 static struct spi_board_info board_info = { 1184 .modalias = "msi001", 1185 .bus_num = 0, 1186 .chip_select = 0, 1187 .max_speed_hz = 12000000, 1188 }; 1189 1190 dev = kzalloc_obj(*dev); 1191 if (!dev) { 1192 ret = -ENOMEM; 1193 goto err; 1194 } 1195 1196 mutex_init(&dev->v4l2_lock); 1197 mutex_init(&dev->vb_queue_lock); 1198 spin_lock_init(&dev->queued_bufs_lock); 1199 INIT_LIST_HEAD(&dev->queued_bufs); 1200 dev->dev = &intf->dev; 1201 dev->udev = interface_to_usbdev(intf); 1202 dev->f_adc = bands[0].rangelow; 1203 dev->pixelformat = formats[0].pixelformat; 1204 dev->buffersize = formats[0].buffersize; 1205 dev->num_formats = NUM_FORMATS; 1206 if (!msi2500_emulated_fmt) 1207 dev->num_formats -= 2; 1208 1209 /* Init videobuf2 queue structure */ 1210 dev->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE; 1211 dev->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ; 1212 dev->vb_queue.drv_priv = dev; 1213 dev->vb_queue.buf_struct_size = sizeof(struct msi2500_frame_buf); 1214 dev->vb_queue.ops = &msi2500_vb2_ops; 1215 dev->vb_queue.mem_ops = &vb2_vmalloc_memops; 1216 dev->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1217 dev->vb_queue.lock = &dev->vb_queue_lock; 1218 ret = vb2_queue_init(&dev->vb_queue); 1219 if (ret) { 1220 dev_err(dev->dev, "Could not initialize vb2 queue\n"); 1221 goto err_free_mem; 1222 } 1223 1224 /* Init video_device structure */ 1225 dev->vdev = msi2500_template; 1226 dev->vdev.queue = &dev->vb_queue; 1227 video_set_drvdata(&dev->vdev, dev); 1228 1229 /* Register the v4l2_device structure */ 1230 dev->v4l2_dev.release = msi2500_video_release; 1231 ret = v4l2_device_register(&intf->dev, &dev->v4l2_dev); 1232 if (ret) { 1233 dev_err(dev->dev, "Failed to register v4l2-device (%d)\n", ret); 1234 goto err_free_mem; 1235 } 1236 1237 /* SPI host adapter */ 1238 ctlr = spi_alloc_host(dev->dev, 0); 1239 if (ctlr == NULL) { 1240 ret = -ENOMEM; 1241 goto err_unregister_v4l2_dev; 1242 } 1243 1244 dev->ctlr = ctlr; 1245 ctlr->bus_num = -1; 1246 ctlr->num_chipselect = 1; 1247 ctlr->transfer_one_message = msi2500_transfer_one_message; 1248 spi_controller_set_devdata(ctlr, dev); 1249 ret = spi_register_controller(ctlr); 1250 if (ret) 1251 goto err_put_controller; 1252 1253 /* load v4l2 subdevice */ 1254 sd = v4l2_spi_new_subdev(&dev->v4l2_dev, ctlr, &board_info); 1255 dev->v4l2_subdev = sd; 1256 if (sd == NULL) { 1257 dev_err(dev->dev, "cannot get v4l2 subdevice\n"); 1258 ret = -ENODEV; 1259 goto err_unregister_controller; 1260 } 1261 1262 /* Register controls */ 1263 v4l2_ctrl_handler_init(&dev->hdl, 0); 1264 if (dev->hdl.error) { 1265 ret = dev->hdl.error; 1266 dev_err(dev->dev, "Could not initialize controls\n"); 1267 goto err_free_controls; 1268 } 1269 1270 /* currently all controls are from subdev */ 1271 v4l2_ctrl_add_handler(&dev->hdl, sd->ctrl_handler, NULL, true); 1272 1273 dev->v4l2_dev.ctrl_handler = &dev->hdl; 1274 dev->vdev.v4l2_dev = &dev->v4l2_dev; 1275 dev->vdev.lock = &dev->v4l2_lock; 1276 dev->vdev.device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_STREAMING | 1277 V4L2_CAP_READWRITE | V4L2_CAP_TUNER; 1278 1279 ret = video_register_device(&dev->vdev, VFL_TYPE_SDR, -1); 1280 if (ret) { 1281 dev_err(dev->dev, 1282 "Failed to register as video device (%d)\n", ret); 1283 goto err_unregister_v4l2_dev; 1284 } 1285 dev_info(dev->dev, "Registered as %s\n", 1286 video_device_node_name(&dev->vdev)); 1287 dev_notice(dev->dev, 1288 "SDR API is still slightly experimental and functionality changes may follow\n"); 1289 return 0; 1290 err_free_controls: 1291 v4l2_ctrl_handler_free(&dev->hdl); 1292 err_unregister_controller: 1293 spi_unregister_controller(dev->ctlr); 1294 err_put_controller: 1295 spi_controller_put(dev->ctlr); 1296 err_unregister_v4l2_dev: 1297 v4l2_device_unregister(&dev->v4l2_dev); 1298 err_free_mem: 1299 kfree(dev); 1300 err: 1301 return ret; 1302 } 1303 1304 /* USB device ID list */ 1305 static const struct usb_device_id msi2500_id_table[] = { 1306 {USB_DEVICE(0x1df7, 0x2500)}, /* Mirics MSi3101 SDR Dongle */ 1307 {USB_DEVICE(0x2040, 0xd300)}, /* Hauppauge WinTV 133559 LF */ 1308 {} 1309 }; 1310 MODULE_DEVICE_TABLE(usb, msi2500_id_table); 1311 1312 /* USB subsystem interface */ 1313 static struct usb_driver msi2500_driver = { 1314 .name = KBUILD_MODNAME, 1315 .probe = msi2500_probe, 1316 .disconnect = msi2500_disconnect, 1317 .id_table = msi2500_id_table, 1318 }; 1319 1320 module_usb_driver(msi2500_driver); 1321 1322 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); 1323 MODULE_DESCRIPTION("Mirics MSi3101 SDR Dongle"); 1324 MODULE_LICENSE("GPL"); 1325