msi2500.c 37 KB

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