msi2500.c 36 KB

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