msi2500.c 36 KB

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