msi2500.c 37 KB

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