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-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. msi2500_isoc_cleanup(dev);
  455. return -ENOMEM;
  456. }
  457. dev->urbs[i] = urb;
  458. dev_dbg(dev->dev, "Allocated URB at 0x%p\n", urb);
  459. urb->interval = 1;
  460. urb->dev = dev->udev;
  461. urb->pipe = usb_rcvisocpipe(dev->udev, 0x81);
  462. urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
  463. urb->transfer_buffer = usb_alloc_coherent(dev->udev,
  464. ISO_BUFFER_SIZE,
  465. GFP_KERNEL, &urb->transfer_dma);
  466. if (urb->transfer_buffer == NULL) {
  467. dev_err(dev->dev,
  468. "Failed to allocate urb buffer %d\n", i);
  469. msi2500_isoc_cleanup(dev);
  470. return -ENOMEM;
  471. }
  472. urb->transfer_buffer_length = ISO_BUFFER_SIZE;
  473. urb->complete = msi2500_isoc_handler;
  474. urb->context = dev;
  475. urb->start_frame = 0;
  476. urb->number_of_packets = ISO_FRAMES_PER_DESC;
  477. for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
  478. urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
  479. urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
  480. }
  481. }
  482. /* link */
  483. for (i = 0; i < MAX_ISO_BUFS; i++) {
  484. ret = usb_submit_urb(dev->urbs[i], GFP_KERNEL);
  485. if (ret) {
  486. dev_err(dev->dev,
  487. "usb_submit_urb %d failed with error %d\n",
  488. i, ret);
  489. msi2500_isoc_cleanup(dev);
  490. return ret;
  491. }
  492. dev_dbg(dev->dev, "URB 0x%p submitted.\n", dev->urbs[i]);
  493. }
  494. /* All is done... */
  495. return 0;
  496. }
  497. /* Must be called with vb_queue_lock hold */
  498. static void msi2500_cleanup_queued_bufs(struct msi2500_dev *dev)
  499. {
  500. unsigned long flags;
  501. dev_dbg(dev->dev, "\n");
  502. spin_lock_irqsave(&dev->queued_bufs_lock, flags);
  503. while (!list_empty(&dev->queued_bufs)) {
  504. struct msi2500_frame_buf *buf;
  505. buf = list_entry(dev->queued_bufs.next,
  506. struct msi2500_frame_buf, list);
  507. list_del(&buf->list);
  508. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  509. }
  510. spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
  511. }
  512. /* The user yanked out the cable... */
  513. static void msi2500_disconnect(struct usb_interface *intf)
  514. {
  515. struct v4l2_device *v = usb_get_intfdata(intf);
  516. struct msi2500_dev *dev =
  517. container_of(v, struct msi2500_dev, v4l2_dev);
  518. dev_dbg(dev->dev, "\n");
  519. mutex_lock(&dev->vb_queue_lock);
  520. mutex_lock(&dev->v4l2_lock);
  521. /* No need to keep the urbs around after disconnection */
  522. dev->udev = NULL;
  523. v4l2_device_disconnect(&dev->v4l2_dev);
  524. video_unregister_device(&dev->vdev);
  525. spi_unregister_master(dev->master);
  526. mutex_unlock(&dev->v4l2_lock);
  527. mutex_unlock(&dev->vb_queue_lock);
  528. v4l2_device_put(&dev->v4l2_dev);
  529. }
  530. static int msi2500_querycap(struct file *file, void *fh,
  531. struct v4l2_capability *cap)
  532. {
  533. struct msi2500_dev *dev = video_drvdata(file);
  534. dev_dbg(dev->dev, "\n");
  535. strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
  536. strlcpy(cap->card, dev->vdev.name, sizeof(cap->card));
  537. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  538. cap->device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_STREAMING |
  539. V4L2_CAP_READWRITE | V4L2_CAP_TUNER;
  540. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  541. return 0;
  542. }
  543. /* Videobuf2 operations */
  544. static int msi2500_queue_setup(struct vb2_queue *vq,
  545. unsigned int *nbuffers,
  546. unsigned int *nplanes, unsigned int sizes[],
  547. struct device *alloc_devs[])
  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 vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  561. struct msi2500_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  562. struct msi2500_frame_buf *buf = container_of(vbuf,
  563. struct msi2500_frame_buf,
  564. vb);
  565. unsigned long flags;
  566. /* Check the device has not disconnected between prep and queuing */
  567. if (unlikely(!dev->udev)) {
  568. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  569. return;
  570. }
  571. spin_lock_irqsave(&dev->queued_bufs_lock, flags);
  572. list_add_tail(&buf->list, &dev->queued_bufs);
  573. spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
  574. }
  575. #define CMD_WREG 0x41
  576. #define CMD_START_STREAMING 0x43
  577. #define CMD_STOP_STREAMING 0x45
  578. #define CMD_READ_UNKNOWN 0x48
  579. #define msi2500_dbg_usb_control_msg(_dev, _r, _t, _v, _i, _b, _l) { \
  580. char *_direction; \
  581. if (_t & USB_DIR_IN) \
  582. _direction = "<<<"; \
  583. else \
  584. _direction = ">>>"; \
  585. dev_dbg(_dev, "%02x %02x %02x %02x %02x %02x %02x %02x %s %*ph\n", \
  586. _t, _r, _v & 0xff, _v >> 8, _i & 0xff, _i >> 8, \
  587. _l & 0xff, _l >> 8, _direction, _l, _b); \
  588. }
  589. static int msi2500_ctrl_msg(struct msi2500_dev *dev, u8 cmd, u32 data)
  590. {
  591. int ret;
  592. u8 request = cmd;
  593. u8 requesttype = USB_DIR_OUT | USB_TYPE_VENDOR;
  594. u16 value = (data >> 0) & 0xffff;
  595. u16 index = (data >> 16) & 0xffff;
  596. msi2500_dbg_usb_control_msg(dev->dev, request, requesttype,
  597. value, index, NULL, 0);
  598. ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0), request,
  599. requesttype, value, index, NULL, 0, 2000);
  600. if (ret)
  601. dev_err(dev->dev, "failed %d, cmd %02x, data %04x\n",
  602. ret, cmd, data);
  603. return ret;
  604. }
  605. static int msi2500_set_usb_adc(struct msi2500_dev *dev)
  606. {
  607. int ret;
  608. unsigned int f_vco, f_sr, div_n, k, k_cw, div_out;
  609. u32 reg3, reg4, reg7;
  610. struct v4l2_ctrl *bandwidth_auto;
  611. struct v4l2_ctrl *bandwidth;
  612. f_sr = dev->f_adc;
  613. /* set tuner, subdev, filters according to sampling rate */
  614. bandwidth_auto = v4l2_ctrl_find(&dev->hdl,
  615. V4L2_CID_RF_TUNER_BANDWIDTH_AUTO);
  616. if (v4l2_ctrl_g_ctrl(bandwidth_auto)) {
  617. bandwidth = v4l2_ctrl_find(&dev->hdl,
  618. V4L2_CID_RF_TUNER_BANDWIDTH);
  619. v4l2_ctrl_s_ctrl(bandwidth, dev->f_adc);
  620. }
  621. /* select stream format */
  622. switch (dev->pixelformat) {
  623. case V4L2_SDR_FMT_CU8:
  624. reg7 = 0x000c9407; /* 504 */
  625. break;
  626. case V4L2_SDR_FMT_CU16LE:
  627. reg7 = 0x00009407; /* 252 */
  628. break;
  629. case V4L2_SDR_FMT_CS8:
  630. reg7 = 0x000c9407; /* 504 */
  631. break;
  632. case MSI2500_PIX_FMT_SDR_MSI2500_384:
  633. reg7 = 0x0000a507; /* 384 */
  634. break;
  635. case MSI2500_PIX_FMT_SDR_S12:
  636. reg7 = 0x00008507; /* 336 */
  637. break;
  638. case V4L2_SDR_FMT_CS14LE:
  639. reg7 = 0x00009407; /* 252 */
  640. break;
  641. default:
  642. reg7 = 0x000c9407; /* 504 */
  643. break;
  644. }
  645. /*
  646. * Fractional-N synthesizer
  647. *
  648. * +----------------------------------------+
  649. * v |
  650. * Fref +----+ +-------+ +-----+ +------+ +---+
  651. * ------> | PD | --> | VCO | --> | /2 | ------> | /N.F | <-- | K |
  652. * +----+ +-------+ +-----+ +------+ +---+
  653. * |
  654. * |
  655. * v
  656. * +-------+ +-----+ Fout
  657. * | /Rout | --> | /12 | ------>
  658. * +-------+ +-----+
  659. */
  660. /*
  661. * Synthesizer config is just a educated guess...
  662. *
  663. * [7:0] 0x03, register address
  664. * [8] 1, power control
  665. * [9] ?, power control
  666. * [12:10] output divider
  667. * [13] 0 ?
  668. * [14] 0 ?
  669. * [15] fractional MSB, bit 20
  670. * [16:19] N
  671. * [23:20] ?
  672. * [24:31] 0x01
  673. *
  674. * output divider
  675. * val div
  676. * 0 - (invalid)
  677. * 1 4
  678. * 2 6
  679. * 3 8
  680. * 4 10
  681. * 5 12
  682. * 6 14
  683. * 7 16
  684. *
  685. * VCO 202000000 - 720000000++
  686. */
  687. #define F_REF 24000000
  688. #define DIV_PRE_N 2
  689. #define DIV_LO_OUT 12
  690. reg3 = 0x01000303;
  691. reg4 = 0x00000004;
  692. /* XXX: Filters? AGC? VCO band? */
  693. if (f_sr < 6000000)
  694. reg3 |= 0x1 << 20;
  695. else if (f_sr < 7000000)
  696. reg3 |= 0x5 << 20;
  697. else if (f_sr < 8500000)
  698. reg3 |= 0x9 << 20;
  699. else
  700. reg3 |= 0xd << 20;
  701. for (div_out = 4; div_out < 16; div_out += 2) {
  702. f_vco = f_sr * div_out * DIV_LO_OUT;
  703. dev_dbg(dev->dev, "div_out=%u f_vco=%u\n", div_out, f_vco);
  704. if (f_vco >= 202000000)
  705. break;
  706. }
  707. /* Calculate PLL integer and fractional control word. */
  708. div_n = div_u64_rem(f_vco, DIV_PRE_N * F_REF, &k);
  709. k_cw = div_u64((u64) k * 0x200000, DIV_PRE_N * F_REF);
  710. reg3 |= div_n << 16;
  711. reg3 |= (div_out / 2 - 1) << 10;
  712. reg3 |= ((k_cw >> 20) & 0x000001) << 15; /* [20] */
  713. reg4 |= ((k_cw >> 0) & 0x0fffff) << 8; /* [19:0] */
  714. dev_dbg(dev->dev,
  715. "f_sr=%u f_vco=%u div_n=%u k=%u div_out=%u reg3=%08x reg4=%08x\n",
  716. f_sr, f_vco, div_n, k, div_out, reg3, reg4);
  717. ret = msi2500_ctrl_msg(dev, CMD_WREG, 0x00608008);
  718. if (ret)
  719. goto err;
  720. ret = msi2500_ctrl_msg(dev, CMD_WREG, 0x00000c05);
  721. if (ret)
  722. goto err;
  723. ret = msi2500_ctrl_msg(dev, CMD_WREG, 0x00020000);
  724. if (ret)
  725. goto err;
  726. ret = msi2500_ctrl_msg(dev, CMD_WREG, 0x00480102);
  727. if (ret)
  728. goto err;
  729. ret = msi2500_ctrl_msg(dev, CMD_WREG, 0x00f38008);
  730. if (ret)
  731. goto err;
  732. ret = msi2500_ctrl_msg(dev, CMD_WREG, reg7);
  733. if (ret)
  734. goto err;
  735. ret = msi2500_ctrl_msg(dev, CMD_WREG, reg4);
  736. if (ret)
  737. goto err;
  738. ret = msi2500_ctrl_msg(dev, CMD_WREG, reg3);
  739. err:
  740. return ret;
  741. }
  742. static int msi2500_start_streaming(struct vb2_queue *vq, unsigned int count)
  743. {
  744. struct msi2500_dev *dev = vb2_get_drv_priv(vq);
  745. int ret;
  746. dev_dbg(dev->dev, "\n");
  747. if (!dev->udev)
  748. return -ENODEV;
  749. if (mutex_lock_interruptible(&dev->v4l2_lock))
  750. return -ERESTARTSYS;
  751. /* wake-up tuner */
  752. v4l2_subdev_call(dev->v4l2_subdev, core, s_power, 1);
  753. ret = msi2500_set_usb_adc(dev);
  754. ret = msi2500_isoc_init(dev);
  755. if (ret)
  756. msi2500_cleanup_queued_bufs(dev);
  757. ret = msi2500_ctrl_msg(dev, CMD_START_STREAMING, 0);
  758. mutex_unlock(&dev->v4l2_lock);
  759. return ret;
  760. }
  761. static void msi2500_stop_streaming(struct vb2_queue *vq)
  762. {
  763. struct msi2500_dev *dev = vb2_get_drv_priv(vq);
  764. dev_dbg(dev->dev, "\n");
  765. mutex_lock(&dev->v4l2_lock);
  766. if (dev->udev)
  767. msi2500_isoc_cleanup(dev);
  768. msi2500_cleanup_queued_bufs(dev);
  769. /* according to tests, at least 700us delay is required */
  770. msleep(20);
  771. if (!msi2500_ctrl_msg(dev, CMD_STOP_STREAMING, 0)) {
  772. /* sleep USB IF / ADC */
  773. msi2500_ctrl_msg(dev, CMD_WREG, 0x01000003);
  774. }
  775. /* sleep tuner */
  776. v4l2_subdev_call(dev->v4l2_subdev, core, s_power, 0);
  777. mutex_unlock(&dev->v4l2_lock);
  778. }
  779. static const struct vb2_ops msi2500_vb2_ops = {
  780. .queue_setup = msi2500_queue_setup,
  781. .buf_queue = msi2500_buf_queue,
  782. .start_streaming = msi2500_start_streaming,
  783. .stop_streaming = msi2500_stop_streaming,
  784. .wait_prepare = vb2_ops_wait_prepare,
  785. .wait_finish = vb2_ops_wait_finish,
  786. };
  787. static int msi2500_enum_fmt_sdr_cap(struct file *file, void *priv,
  788. struct v4l2_fmtdesc *f)
  789. {
  790. struct msi2500_dev *dev = video_drvdata(file);
  791. dev_dbg(dev->dev, "index=%d\n", f->index);
  792. if (f->index >= dev->num_formats)
  793. return -EINVAL;
  794. strlcpy(f->description, formats[f->index].name, sizeof(f->description));
  795. f->pixelformat = formats[f->index].pixelformat;
  796. return 0;
  797. }
  798. static int msi2500_g_fmt_sdr_cap(struct file *file, void *priv,
  799. struct v4l2_format *f)
  800. {
  801. struct msi2500_dev *dev = video_drvdata(file);
  802. dev_dbg(dev->dev, "pixelformat fourcc %4.4s\n",
  803. (char *)&dev->pixelformat);
  804. f->fmt.sdr.pixelformat = dev->pixelformat;
  805. f->fmt.sdr.buffersize = dev->buffersize;
  806. memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
  807. return 0;
  808. }
  809. static int msi2500_s_fmt_sdr_cap(struct file *file, void *priv,
  810. struct v4l2_format *f)
  811. {
  812. struct msi2500_dev *dev = video_drvdata(file);
  813. struct vb2_queue *q = &dev->vb_queue;
  814. int i;
  815. dev_dbg(dev->dev, "pixelformat fourcc %4.4s\n",
  816. (char *)&f->fmt.sdr.pixelformat);
  817. if (vb2_is_busy(q))
  818. return -EBUSY;
  819. memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
  820. for (i = 0; i < dev->num_formats; i++) {
  821. if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
  822. dev->pixelformat = formats[i].pixelformat;
  823. dev->buffersize = formats[i].buffersize;
  824. f->fmt.sdr.buffersize = formats[i].buffersize;
  825. return 0;
  826. }
  827. }
  828. dev->pixelformat = formats[0].pixelformat;
  829. dev->buffersize = formats[0].buffersize;
  830. f->fmt.sdr.pixelformat = formats[0].pixelformat;
  831. f->fmt.sdr.buffersize = formats[0].buffersize;
  832. return 0;
  833. }
  834. static int msi2500_try_fmt_sdr_cap(struct file *file, void *priv,
  835. struct v4l2_format *f)
  836. {
  837. struct msi2500_dev *dev = video_drvdata(file);
  838. int i;
  839. dev_dbg(dev->dev, "pixelformat fourcc %4.4s\n",
  840. (char *)&f->fmt.sdr.pixelformat);
  841. memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
  842. for (i = 0; i < dev->num_formats; i++) {
  843. if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
  844. f->fmt.sdr.buffersize = formats[i].buffersize;
  845. return 0;
  846. }
  847. }
  848. f->fmt.sdr.pixelformat = formats[0].pixelformat;
  849. f->fmt.sdr.buffersize = formats[0].buffersize;
  850. return 0;
  851. }
  852. static int msi2500_s_tuner(struct file *file, void *priv,
  853. const struct v4l2_tuner *v)
  854. {
  855. struct msi2500_dev *dev = video_drvdata(file);
  856. int ret;
  857. dev_dbg(dev->dev, "index=%d\n", v->index);
  858. if (v->index == 0)
  859. ret = 0;
  860. else if (v->index == 1)
  861. ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, s_tuner, v);
  862. else
  863. ret = -EINVAL;
  864. return ret;
  865. }
  866. static int msi2500_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
  867. {
  868. struct msi2500_dev *dev = video_drvdata(file);
  869. int ret;
  870. dev_dbg(dev->dev, "index=%d\n", v->index);
  871. if (v->index == 0) {
  872. strlcpy(v->name, "Mirics MSi2500", sizeof(v->name));
  873. v->type = V4L2_TUNER_ADC;
  874. v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
  875. v->rangelow = 1200000;
  876. v->rangehigh = 15000000;
  877. ret = 0;
  878. } else if (v->index == 1) {
  879. ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, g_tuner, v);
  880. } else {
  881. ret = -EINVAL;
  882. }
  883. return ret;
  884. }
  885. static int msi2500_g_frequency(struct file *file, void *priv,
  886. struct v4l2_frequency *f)
  887. {
  888. struct msi2500_dev *dev = video_drvdata(file);
  889. int ret = 0;
  890. dev_dbg(dev->dev, "tuner=%d type=%d\n", f->tuner, f->type);
  891. if (f->tuner == 0) {
  892. f->frequency = dev->f_adc;
  893. ret = 0;
  894. } else if (f->tuner == 1) {
  895. f->type = V4L2_TUNER_RF;
  896. ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, g_frequency, f);
  897. } else {
  898. ret = -EINVAL;
  899. }
  900. return ret;
  901. }
  902. static int msi2500_s_frequency(struct file *file, void *priv,
  903. const struct v4l2_frequency *f)
  904. {
  905. struct msi2500_dev *dev = video_drvdata(file);
  906. int ret;
  907. dev_dbg(dev->dev, "tuner=%d type=%d frequency=%u\n",
  908. f->tuner, f->type, f->frequency);
  909. if (f->tuner == 0) {
  910. dev->f_adc = clamp_t(unsigned int, f->frequency,
  911. bands[0].rangelow,
  912. bands[0].rangehigh);
  913. dev_dbg(dev->dev, "ADC frequency=%u Hz\n", dev->f_adc);
  914. ret = msi2500_set_usb_adc(dev);
  915. } else if (f->tuner == 1) {
  916. ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, s_frequency, f);
  917. } else {
  918. ret = -EINVAL;
  919. }
  920. return ret;
  921. }
  922. static int msi2500_enum_freq_bands(struct file *file, void *priv,
  923. struct v4l2_frequency_band *band)
  924. {
  925. struct msi2500_dev *dev = video_drvdata(file);
  926. int ret;
  927. dev_dbg(dev->dev, "tuner=%d type=%d index=%d\n",
  928. band->tuner, band->type, band->index);
  929. if (band->tuner == 0) {
  930. if (band->index >= ARRAY_SIZE(bands)) {
  931. ret = -EINVAL;
  932. } else {
  933. *band = bands[band->index];
  934. ret = 0;
  935. }
  936. } else if (band->tuner == 1) {
  937. ret = v4l2_subdev_call(dev->v4l2_subdev, tuner,
  938. enum_freq_bands, band);
  939. } else {
  940. ret = -EINVAL;
  941. }
  942. return ret;
  943. }
  944. static const struct v4l2_ioctl_ops msi2500_ioctl_ops = {
  945. .vidioc_querycap = msi2500_querycap,
  946. .vidioc_enum_fmt_sdr_cap = msi2500_enum_fmt_sdr_cap,
  947. .vidioc_g_fmt_sdr_cap = msi2500_g_fmt_sdr_cap,
  948. .vidioc_s_fmt_sdr_cap = msi2500_s_fmt_sdr_cap,
  949. .vidioc_try_fmt_sdr_cap = msi2500_try_fmt_sdr_cap,
  950. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  951. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  952. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  953. .vidioc_querybuf = vb2_ioctl_querybuf,
  954. .vidioc_qbuf = vb2_ioctl_qbuf,
  955. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  956. .vidioc_streamon = vb2_ioctl_streamon,
  957. .vidioc_streamoff = vb2_ioctl_streamoff,
  958. .vidioc_g_tuner = msi2500_g_tuner,
  959. .vidioc_s_tuner = msi2500_s_tuner,
  960. .vidioc_g_frequency = msi2500_g_frequency,
  961. .vidioc_s_frequency = msi2500_s_frequency,
  962. .vidioc_enum_freq_bands = msi2500_enum_freq_bands,
  963. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  964. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  965. .vidioc_log_status = v4l2_ctrl_log_status,
  966. };
  967. static const struct v4l2_file_operations msi2500_fops = {
  968. .owner = THIS_MODULE,
  969. .open = v4l2_fh_open,
  970. .release = vb2_fop_release,
  971. .read = vb2_fop_read,
  972. .poll = vb2_fop_poll,
  973. .mmap = vb2_fop_mmap,
  974. .unlocked_ioctl = video_ioctl2,
  975. };
  976. static struct video_device msi2500_template = {
  977. .name = "Mirics MSi3101 SDR Dongle",
  978. .release = video_device_release_empty,
  979. .fops = &msi2500_fops,
  980. .ioctl_ops = &msi2500_ioctl_ops,
  981. };
  982. static void msi2500_video_release(struct v4l2_device *v)
  983. {
  984. struct msi2500_dev *dev = container_of(v, struct msi2500_dev, v4l2_dev);
  985. v4l2_ctrl_handler_free(&dev->hdl);
  986. v4l2_device_unregister(&dev->v4l2_dev);
  987. kfree(dev);
  988. }
  989. static int msi2500_transfer_one_message(struct spi_master *master,
  990. struct spi_message *m)
  991. {
  992. struct msi2500_dev *dev = spi_master_get_devdata(master);
  993. struct spi_transfer *t;
  994. int ret = 0;
  995. u32 data;
  996. list_for_each_entry(t, &m->transfers, transfer_list) {
  997. dev_dbg(dev->dev, "msg=%*ph\n", t->len, t->tx_buf);
  998. data = 0x09; /* reg 9 is SPI adapter */
  999. data |= ((u8 *)t->tx_buf)[0] << 8;
  1000. data |= ((u8 *)t->tx_buf)[1] << 16;
  1001. data |= ((u8 *)t->tx_buf)[2] << 24;
  1002. ret = msi2500_ctrl_msg(dev, CMD_WREG, data);
  1003. }
  1004. m->status = ret;
  1005. spi_finalize_current_message(master);
  1006. return ret;
  1007. }
  1008. static int msi2500_probe(struct usb_interface *intf,
  1009. const struct usb_device_id *id)
  1010. {
  1011. struct msi2500_dev *dev;
  1012. struct v4l2_subdev *sd;
  1013. struct spi_master *master;
  1014. int ret;
  1015. static struct spi_board_info board_info = {
  1016. .modalias = "msi001",
  1017. .bus_num = 0,
  1018. .chip_select = 0,
  1019. .max_speed_hz = 12000000,
  1020. };
  1021. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1022. if (!dev) {
  1023. ret = -ENOMEM;
  1024. goto err;
  1025. }
  1026. mutex_init(&dev->v4l2_lock);
  1027. mutex_init(&dev->vb_queue_lock);
  1028. spin_lock_init(&dev->queued_bufs_lock);
  1029. INIT_LIST_HEAD(&dev->queued_bufs);
  1030. dev->dev = &intf->dev;
  1031. dev->udev = interface_to_usbdev(intf);
  1032. dev->f_adc = bands[0].rangelow;
  1033. dev->pixelformat = formats[0].pixelformat;
  1034. dev->buffersize = formats[0].buffersize;
  1035. dev->num_formats = NUM_FORMATS;
  1036. if (!msi2500_emulated_fmt)
  1037. dev->num_formats -= 2;
  1038. /* Init videobuf2 queue structure */
  1039. dev->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE;
  1040. dev->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
  1041. dev->vb_queue.drv_priv = dev;
  1042. dev->vb_queue.buf_struct_size = sizeof(struct msi2500_frame_buf);
  1043. dev->vb_queue.ops = &msi2500_vb2_ops;
  1044. dev->vb_queue.mem_ops = &vb2_vmalloc_memops;
  1045. dev->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1046. ret = vb2_queue_init(&dev->vb_queue);
  1047. if (ret) {
  1048. dev_err(dev->dev, "Could not initialize vb2 queue\n");
  1049. goto err_free_mem;
  1050. }
  1051. /* Init video_device structure */
  1052. dev->vdev = msi2500_template;
  1053. dev->vdev.queue = &dev->vb_queue;
  1054. dev->vdev.queue->lock = &dev->vb_queue_lock;
  1055. video_set_drvdata(&dev->vdev, dev);
  1056. /* Register the v4l2_device structure */
  1057. dev->v4l2_dev.release = msi2500_video_release;
  1058. ret = v4l2_device_register(&intf->dev, &dev->v4l2_dev);
  1059. if (ret) {
  1060. dev_err(dev->dev, "Failed to register v4l2-device (%d)\n", ret);
  1061. goto err_free_mem;
  1062. }
  1063. /* SPI master adapter */
  1064. master = spi_alloc_master(dev->dev, 0);
  1065. if (master == NULL) {
  1066. ret = -ENOMEM;
  1067. goto err_unregister_v4l2_dev;
  1068. }
  1069. dev->master = master;
  1070. master->bus_num = 0;
  1071. master->num_chipselect = 1;
  1072. master->transfer_one_message = msi2500_transfer_one_message;
  1073. spi_master_set_devdata(master, dev);
  1074. ret = spi_register_master(master);
  1075. if (ret) {
  1076. spi_master_put(master);
  1077. goto err_unregister_v4l2_dev;
  1078. }
  1079. /* load v4l2 subdevice */
  1080. sd = v4l2_spi_new_subdev(&dev->v4l2_dev, master, &board_info);
  1081. dev->v4l2_subdev = sd;
  1082. if (sd == NULL) {
  1083. dev_err(dev->dev, "cannot get v4l2 subdevice\n");
  1084. ret = -ENODEV;
  1085. goto err_unregister_master;
  1086. }
  1087. /* Register controls */
  1088. v4l2_ctrl_handler_init(&dev->hdl, 0);
  1089. if (dev->hdl.error) {
  1090. ret = dev->hdl.error;
  1091. dev_err(dev->dev, "Could not initialize controls\n");
  1092. goto err_free_controls;
  1093. }
  1094. /* currently all controls are from subdev */
  1095. v4l2_ctrl_add_handler(&dev->hdl, sd->ctrl_handler, NULL);
  1096. dev->v4l2_dev.ctrl_handler = &dev->hdl;
  1097. dev->vdev.v4l2_dev = &dev->v4l2_dev;
  1098. dev->vdev.lock = &dev->v4l2_lock;
  1099. ret = video_register_device(&dev->vdev, VFL_TYPE_SDR, -1);
  1100. if (ret) {
  1101. dev_err(dev->dev,
  1102. "Failed to register as video device (%d)\n", ret);
  1103. goto err_unregister_v4l2_dev;
  1104. }
  1105. dev_info(dev->dev, "Registered as %s\n",
  1106. video_device_node_name(&dev->vdev));
  1107. dev_notice(dev->dev,
  1108. "SDR API is still slightly experimental and functionality changes may follow\n");
  1109. return 0;
  1110. err_free_controls:
  1111. v4l2_ctrl_handler_free(&dev->hdl);
  1112. err_unregister_master:
  1113. spi_unregister_master(dev->master);
  1114. err_unregister_v4l2_dev:
  1115. v4l2_device_unregister(&dev->v4l2_dev);
  1116. err_free_mem:
  1117. kfree(dev);
  1118. err:
  1119. return ret;
  1120. }
  1121. /* USB device ID list */
  1122. static struct usb_device_id msi2500_id_table[] = {
  1123. {USB_DEVICE(0x1df7, 0x2500)}, /* Mirics MSi3101 SDR Dongle */
  1124. {USB_DEVICE(0x2040, 0xd300)}, /* Hauppauge WinTV 133559 LF */
  1125. {}
  1126. };
  1127. MODULE_DEVICE_TABLE(usb, msi2500_id_table);
  1128. /* USB subsystem interface */
  1129. static struct usb_driver msi2500_driver = {
  1130. .name = KBUILD_MODNAME,
  1131. .probe = msi2500_probe,
  1132. .disconnect = msi2500_disconnect,
  1133. .id_table = msi2500_id_table,
  1134. };
  1135. module_usb_driver(msi2500_driver);
  1136. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  1137. MODULE_DESCRIPTION("Mirics MSi3101 SDR Dongle");
  1138. MODULE_LICENSE("GPL");