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