rtl2832_sdr.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549
  1. /*
  2. * Realtek RTL2832U SDR 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. * GNU Radio plugin "gr-kernel" for device usage will be on:
  21. * http://git.linuxtv.org/anttip/gr-kernel.git
  22. *
  23. */
  24. #include "rtl2832_sdr.h"
  25. #include "dvb_usb.h"
  26. #include <media/v4l2-device.h>
  27. #include <media/v4l2-ioctl.h>
  28. #include <media/v4l2-ctrls.h>
  29. #include <media/v4l2-event.h>
  30. #include <media/videobuf2-vmalloc.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/jiffies.h>
  33. #include <linux/math64.h>
  34. static bool rtl2832_sdr_emulated_fmt;
  35. module_param_named(emulated_formats, rtl2832_sdr_emulated_fmt, bool, 0644);
  36. MODULE_PARM_DESC(emulated_formats, "enable emulated formats (disappears in future)");
  37. /* Original macro does not contain enough null pointer checks for our need */
  38. #define V4L2_SUBDEV_HAS_OP(sd, o, f) \
  39. ((sd) && (sd)->ops && (sd)->ops->o && (sd)->ops->o->f)
  40. #define MAX_BULK_BUFS (10)
  41. #define BULK_BUFFER_SIZE (128 * 512)
  42. static const struct v4l2_frequency_band bands_adc[] = {
  43. {
  44. .tuner = 0,
  45. .type = V4L2_TUNER_ADC,
  46. .index = 0,
  47. .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
  48. .rangelow = 300000,
  49. .rangehigh = 300000,
  50. },
  51. {
  52. .tuner = 0,
  53. .type = V4L2_TUNER_ADC,
  54. .index = 1,
  55. .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
  56. .rangelow = 900001,
  57. .rangehigh = 2800000,
  58. },
  59. {
  60. .tuner = 0,
  61. .type = V4L2_TUNER_ADC,
  62. .index = 2,
  63. .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
  64. .rangelow = 3200000,
  65. .rangehigh = 3200000,
  66. },
  67. };
  68. static const struct v4l2_frequency_band bands_fm[] = {
  69. {
  70. .tuner = 1,
  71. .type = V4L2_TUNER_RF,
  72. .index = 0,
  73. .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
  74. .rangelow = 50000000,
  75. .rangehigh = 2000000000,
  76. },
  77. };
  78. /* stream formats */
  79. struct rtl2832_sdr_format {
  80. char *name;
  81. u32 pixelformat;
  82. u32 buffersize;
  83. };
  84. static struct rtl2832_sdr_format formats[] = {
  85. {
  86. .name = "Complex U8",
  87. .pixelformat = V4L2_SDR_FMT_CU8,
  88. .buffersize = BULK_BUFFER_SIZE,
  89. }, {
  90. .name = "Complex U16LE (emulated)",
  91. .pixelformat = V4L2_SDR_FMT_CU16LE,
  92. .buffersize = BULK_BUFFER_SIZE * 2,
  93. },
  94. };
  95. static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats);
  96. /* intermediate buffers with raw data from the USB device */
  97. struct rtl2832_sdr_frame_buf {
  98. struct vb2_buffer vb; /* common v4l buffer stuff -- must be first */
  99. struct list_head list;
  100. };
  101. struct rtl2832_sdr_dev {
  102. #define POWER_ON 0 /* BIT(0) */
  103. #define URB_BUF 1 /* BIT(1) */
  104. unsigned long flags;
  105. struct platform_device *pdev;
  106. struct video_device vdev;
  107. struct v4l2_device v4l2_dev;
  108. struct v4l2_subdev *v4l2_subdev;
  109. /* videobuf2 queue and queued buffers list */
  110. struct vb2_queue vb_queue;
  111. struct list_head queued_bufs;
  112. spinlock_t queued_bufs_lock; /* Protects queued_bufs */
  113. unsigned sequence; /* buffer sequence counter */
  114. /* Note if taking both locks v4l2_lock must always be locked first! */
  115. struct mutex v4l2_lock; /* Protects everything else */
  116. struct mutex vb_queue_lock; /* Protects vb_queue and capt_file */
  117. /* Pointer to our usb_device, will be NULL after unplug */
  118. struct usb_device *udev; /* Both mutexes most be hold when setting! */
  119. unsigned int vb_full; /* vb is full and packets dropped */
  120. struct urb *urb_list[MAX_BULK_BUFS];
  121. int buf_num;
  122. unsigned long buf_size;
  123. u8 *buf_list[MAX_BULK_BUFS];
  124. dma_addr_t dma_addr[MAX_BULK_BUFS];
  125. int urbs_initialized;
  126. int urbs_submitted;
  127. unsigned int f_adc, f_tuner;
  128. u32 pixelformat;
  129. u32 buffersize;
  130. unsigned int num_formats;
  131. /* Controls */
  132. struct v4l2_ctrl_handler hdl;
  133. struct v4l2_ctrl *bandwidth_auto;
  134. struct v4l2_ctrl *bandwidth;
  135. /* for sample rate calc */
  136. unsigned int sample;
  137. unsigned int sample_measured;
  138. unsigned long jiffies_next;
  139. };
  140. /* write multiple registers */
  141. static int rtl2832_sdr_wr_regs(struct rtl2832_sdr_dev *dev, u16 reg,
  142. const u8 *val, int len)
  143. {
  144. struct platform_device *pdev = dev->pdev;
  145. struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
  146. struct i2c_client *client = pdata->i2c_client;
  147. return pdata->bulk_write(client, reg, val, len);
  148. }
  149. #if 0
  150. /* read multiple registers */
  151. static int rtl2832_sdr_rd_regs(struct rtl2832_sdr_dev *dev, u16 reg, u8 *val,
  152. int len)
  153. {
  154. struct platform_device *pdev = dev->pdev;
  155. struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
  156. struct i2c_client *client = pdata->i2c_client;
  157. return pdata->bulk_read(client, reg, val, len);
  158. }
  159. #endif
  160. /* write single register */
  161. static int rtl2832_sdr_wr_reg(struct rtl2832_sdr_dev *dev, u16 reg, u8 val)
  162. {
  163. return rtl2832_sdr_wr_regs(dev, reg, &val, 1);
  164. }
  165. /* write single register with mask */
  166. static int rtl2832_sdr_wr_reg_mask(struct rtl2832_sdr_dev *dev, u16 reg,
  167. u8 val, u8 mask)
  168. {
  169. struct platform_device *pdev = dev->pdev;
  170. struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
  171. struct i2c_client *client = pdata->i2c_client;
  172. return pdata->update_bits(client, reg, mask, val);
  173. }
  174. /* Private functions */
  175. static struct rtl2832_sdr_frame_buf *rtl2832_sdr_get_next_fill_buf(
  176. struct rtl2832_sdr_dev *dev)
  177. {
  178. unsigned long flags;
  179. struct rtl2832_sdr_frame_buf *buf = NULL;
  180. spin_lock_irqsave(&dev->queued_bufs_lock, flags);
  181. if (list_empty(&dev->queued_bufs))
  182. goto leave;
  183. buf = list_entry(dev->queued_bufs.next,
  184. struct rtl2832_sdr_frame_buf, list);
  185. list_del(&buf->list);
  186. leave:
  187. spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
  188. return buf;
  189. }
  190. static unsigned int rtl2832_sdr_convert_stream(struct rtl2832_sdr_dev *dev,
  191. void *dst, const u8 *src, unsigned int src_len)
  192. {
  193. struct platform_device *pdev = dev->pdev;
  194. unsigned int dst_len;
  195. if (dev->pixelformat == V4L2_SDR_FMT_CU8) {
  196. /* native stream, no need to convert */
  197. memcpy(dst, src, src_len);
  198. dst_len = src_len;
  199. } else if (dev->pixelformat == V4L2_SDR_FMT_CU16LE) {
  200. /* convert u8 to u16 */
  201. unsigned int i;
  202. u16 *u16dst = dst;
  203. for (i = 0; i < src_len; i++)
  204. *u16dst++ = (src[i] << 8) | (src[i] >> 0);
  205. dst_len = 2 * src_len;
  206. } else {
  207. dst_len = 0;
  208. }
  209. /* calculate sample rate and output it in 10 seconds intervals */
  210. if (unlikely(time_is_before_jiffies(dev->jiffies_next))) {
  211. #define MSECS 10000UL
  212. unsigned int msecs = jiffies_to_msecs(jiffies -
  213. dev->jiffies_next + msecs_to_jiffies(MSECS));
  214. unsigned int samples = dev->sample - dev->sample_measured;
  215. dev->jiffies_next = jiffies + msecs_to_jiffies(MSECS);
  216. dev->sample_measured = dev->sample;
  217. dev_dbg(&pdev->dev,
  218. "slen=%u samples=%u msecs=%u sample rate=%lu\n",
  219. src_len, samples, msecs, samples * 1000UL / msecs);
  220. }
  221. /* total number of I+Q pairs */
  222. dev->sample += src_len / 2;
  223. return dst_len;
  224. }
  225. /*
  226. * This gets called for the bulk stream pipe. This is done in interrupt
  227. * time, so it has to be fast, not crash, and not stall. Neat.
  228. */
  229. static void rtl2832_sdr_urb_complete(struct urb *urb)
  230. {
  231. struct rtl2832_sdr_dev *dev = urb->context;
  232. struct platform_device *pdev = dev->pdev;
  233. struct rtl2832_sdr_frame_buf *fbuf;
  234. dev_dbg_ratelimited(&pdev->dev, "status=%d length=%d/%d errors=%d\n",
  235. urb->status, urb->actual_length,
  236. urb->transfer_buffer_length, urb->error_count);
  237. switch (urb->status) {
  238. case 0: /* success */
  239. case -ETIMEDOUT: /* NAK */
  240. break;
  241. case -ECONNRESET: /* kill */
  242. case -ENOENT:
  243. case -ESHUTDOWN:
  244. return;
  245. default: /* error */
  246. dev_err_ratelimited(&pdev->dev, "urb failed=%d\n", urb->status);
  247. break;
  248. }
  249. if (likely(urb->actual_length > 0)) {
  250. void *ptr;
  251. unsigned int len;
  252. /* get free framebuffer */
  253. fbuf = rtl2832_sdr_get_next_fill_buf(dev);
  254. if (unlikely(fbuf == NULL)) {
  255. dev->vb_full++;
  256. dev_notice_ratelimited(&pdev->dev,
  257. "videobuf is full, %d packets dropped\n",
  258. dev->vb_full);
  259. goto skip;
  260. }
  261. /* fill framebuffer */
  262. ptr = vb2_plane_vaddr(&fbuf->vb, 0);
  263. len = rtl2832_sdr_convert_stream(dev, ptr, urb->transfer_buffer,
  264. urb->actual_length);
  265. vb2_set_plane_payload(&fbuf->vb, 0, len);
  266. v4l2_get_timestamp(&fbuf->vb.v4l2_buf.timestamp);
  267. fbuf->vb.v4l2_buf.sequence = dev->sequence++;
  268. vb2_buffer_done(&fbuf->vb, VB2_BUF_STATE_DONE);
  269. }
  270. skip:
  271. usb_submit_urb(urb, GFP_ATOMIC);
  272. }
  273. static int rtl2832_sdr_kill_urbs(struct rtl2832_sdr_dev *dev)
  274. {
  275. struct platform_device *pdev = dev->pdev;
  276. int i;
  277. for (i = dev->urbs_submitted - 1; i >= 0; i--) {
  278. dev_dbg(&pdev->dev, "kill urb=%d\n", i);
  279. /* stop the URB */
  280. usb_kill_urb(dev->urb_list[i]);
  281. }
  282. dev->urbs_submitted = 0;
  283. return 0;
  284. }
  285. static int rtl2832_sdr_submit_urbs(struct rtl2832_sdr_dev *dev)
  286. {
  287. struct platform_device *pdev = dev->pdev;
  288. int i, ret;
  289. for (i = 0; i < dev->urbs_initialized; i++) {
  290. dev_dbg(&pdev->dev, "submit urb=%d\n", i);
  291. ret = usb_submit_urb(dev->urb_list[i], GFP_ATOMIC);
  292. if (ret) {
  293. dev_err(&pdev->dev,
  294. "Could not submit urb no. %d - get them all back\n",
  295. i);
  296. rtl2832_sdr_kill_urbs(dev);
  297. return ret;
  298. }
  299. dev->urbs_submitted++;
  300. }
  301. return 0;
  302. }
  303. static int rtl2832_sdr_free_stream_bufs(struct rtl2832_sdr_dev *dev)
  304. {
  305. struct platform_device *pdev = dev->pdev;
  306. if (test_bit(URB_BUF, &dev->flags)) {
  307. while (dev->buf_num) {
  308. dev->buf_num--;
  309. dev_dbg(&pdev->dev, "free buf=%d\n", dev->buf_num);
  310. usb_free_coherent(dev->udev, dev->buf_size,
  311. dev->buf_list[dev->buf_num],
  312. dev->dma_addr[dev->buf_num]);
  313. }
  314. }
  315. clear_bit(URB_BUF, &dev->flags);
  316. return 0;
  317. }
  318. static int rtl2832_sdr_alloc_stream_bufs(struct rtl2832_sdr_dev *dev)
  319. {
  320. struct platform_device *pdev = dev->pdev;
  321. dev->buf_num = 0;
  322. dev->buf_size = BULK_BUFFER_SIZE;
  323. dev_dbg(&pdev->dev, "all in all I will use %u bytes for streaming\n",
  324. MAX_BULK_BUFS * BULK_BUFFER_SIZE);
  325. for (dev->buf_num = 0; dev->buf_num < MAX_BULK_BUFS; dev->buf_num++) {
  326. dev->buf_list[dev->buf_num] = usb_alloc_coherent(dev->udev,
  327. BULK_BUFFER_SIZE, GFP_ATOMIC,
  328. &dev->dma_addr[dev->buf_num]);
  329. if (!dev->buf_list[dev->buf_num]) {
  330. dev_dbg(&pdev->dev, "alloc buf=%d failed\n",
  331. dev->buf_num);
  332. rtl2832_sdr_free_stream_bufs(dev);
  333. return -ENOMEM;
  334. }
  335. dev_dbg(&pdev->dev, "alloc buf=%d %p (dma %llu)\n",
  336. dev->buf_num, dev->buf_list[dev->buf_num],
  337. (long long)dev->dma_addr[dev->buf_num]);
  338. set_bit(URB_BUF, &dev->flags);
  339. }
  340. return 0;
  341. }
  342. static int rtl2832_sdr_free_urbs(struct rtl2832_sdr_dev *dev)
  343. {
  344. struct platform_device *pdev = dev->pdev;
  345. int i;
  346. rtl2832_sdr_kill_urbs(dev);
  347. for (i = dev->urbs_initialized - 1; i >= 0; i--) {
  348. if (dev->urb_list[i]) {
  349. dev_dbg(&pdev->dev, "free urb=%d\n", i);
  350. /* free the URBs */
  351. usb_free_urb(dev->urb_list[i]);
  352. }
  353. }
  354. dev->urbs_initialized = 0;
  355. return 0;
  356. }
  357. static int rtl2832_sdr_alloc_urbs(struct rtl2832_sdr_dev *dev)
  358. {
  359. struct platform_device *pdev = dev->pdev;
  360. int i, j;
  361. /* allocate the URBs */
  362. for (i = 0; i < MAX_BULK_BUFS; i++) {
  363. dev_dbg(&pdev->dev, "alloc urb=%d\n", i);
  364. dev->urb_list[i] = usb_alloc_urb(0, GFP_ATOMIC);
  365. if (!dev->urb_list[i]) {
  366. dev_dbg(&pdev->dev, "failed\n");
  367. for (j = 0; j < i; j++)
  368. usb_free_urb(dev->urb_list[j]);
  369. return -ENOMEM;
  370. }
  371. usb_fill_bulk_urb(dev->urb_list[i],
  372. dev->udev,
  373. usb_rcvbulkpipe(dev->udev, 0x81),
  374. dev->buf_list[i],
  375. BULK_BUFFER_SIZE,
  376. rtl2832_sdr_urb_complete, dev);
  377. dev->urb_list[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  378. dev->urb_list[i]->transfer_dma = dev->dma_addr[i];
  379. dev->urbs_initialized++;
  380. }
  381. return 0;
  382. }
  383. /* Must be called with vb_queue_lock hold */
  384. static void rtl2832_sdr_cleanup_queued_bufs(struct rtl2832_sdr_dev *dev)
  385. {
  386. struct platform_device *pdev = dev->pdev;
  387. unsigned long flags;
  388. dev_dbg(&pdev->dev, "\n");
  389. spin_lock_irqsave(&dev->queued_bufs_lock, flags);
  390. while (!list_empty(&dev->queued_bufs)) {
  391. struct rtl2832_sdr_frame_buf *buf;
  392. buf = list_entry(dev->queued_bufs.next,
  393. struct rtl2832_sdr_frame_buf, list);
  394. list_del(&buf->list);
  395. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  396. }
  397. spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
  398. }
  399. static int rtl2832_sdr_querycap(struct file *file, void *fh,
  400. struct v4l2_capability *cap)
  401. {
  402. struct rtl2832_sdr_dev *dev = video_drvdata(file);
  403. struct platform_device *pdev = dev->pdev;
  404. dev_dbg(&pdev->dev, "\n");
  405. strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
  406. strlcpy(cap->card, dev->vdev.name, sizeof(cap->card));
  407. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  408. cap->device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_STREAMING |
  409. V4L2_CAP_READWRITE | V4L2_CAP_TUNER;
  410. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  411. return 0;
  412. }
  413. /* Videobuf2 operations */
  414. static int rtl2832_sdr_queue_setup(struct vb2_queue *vq,
  415. const struct v4l2_format *fmt, unsigned int *nbuffers,
  416. unsigned int *nplanes, unsigned int sizes[], void *alloc_ctxs[])
  417. {
  418. struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq);
  419. struct platform_device *pdev = dev->pdev;
  420. dev_dbg(&pdev->dev, "nbuffers=%d\n", *nbuffers);
  421. /* Need at least 8 buffers */
  422. if (vq->num_buffers + *nbuffers < 8)
  423. *nbuffers = 8 - vq->num_buffers;
  424. *nplanes = 1;
  425. sizes[0] = PAGE_ALIGN(dev->buffersize);
  426. dev_dbg(&pdev->dev, "nbuffers=%d sizes[0]=%d\n", *nbuffers, sizes[0]);
  427. return 0;
  428. }
  429. static int rtl2832_sdr_buf_prepare(struct vb2_buffer *vb)
  430. {
  431. struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  432. /* Don't allow queing new buffers after device disconnection */
  433. if (!dev->udev)
  434. return -ENODEV;
  435. return 0;
  436. }
  437. static void rtl2832_sdr_buf_queue(struct vb2_buffer *vb)
  438. {
  439. struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  440. struct rtl2832_sdr_frame_buf *buf =
  441. container_of(vb, struct rtl2832_sdr_frame_buf, vb);
  442. unsigned long flags;
  443. /* Check the device has not disconnected between prep and queuing */
  444. if (!dev->udev) {
  445. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  446. return;
  447. }
  448. spin_lock_irqsave(&dev->queued_bufs_lock, flags);
  449. list_add_tail(&buf->list, &dev->queued_bufs);
  450. spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
  451. }
  452. static int rtl2832_sdr_set_adc(struct rtl2832_sdr_dev *dev)
  453. {
  454. struct platform_device *pdev = dev->pdev;
  455. struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
  456. struct dvb_frontend *fe = pdata->dvb_frontend;
  457. int ret;
  458. unsigned int f_sr, f_if;
  459. u8 buf[4], u8tmp1, u8tmp2;
  460. u64 u64tmp;
  461. u32 u32tmp;
  462. dev_dbg(&pdev->dev, "f_adc=%u\n", dev->f_adc);
  463. if (!test_bit(POWER_ON, &dev->flags))
  464. return 0;
  465. if (dev->f_adc == 0)
  466. return 0;
  467. f_sr = dev->f_adc;
  468. ret = rtl2832_sdr_wr_regs(dev, 0x13e, "\x00\x00", 2);
  469. if (ret)
  470. goto err;
  471. ret = rtl2832_sdr_wr_regs(dev, 0x115, "\x00\x00\x00\x00", 4);
  472. if (ret)
  473. goto err;
  474. /* get IF from tuner */
  475. if (fe->ops.tuner_ops.get_if_frequency)
  476. ret = fe->ops.tuner_ops.get_if_frequency(fe, &f_if);
  477. else
  478. ret = -EINVAL;
  479. if (ret)
  480. goto err;
  481. /* program IF */
  482. u64tmp = f_if % pdata->clk;
  483. u64tmp *= 0x400000;
  484. u64tmp = div_u64(u64tmp, pdata->clk);
  485. u64tmp = -u64tmp;
  486. u32tmp = u64tmp & 0x3fffff;
  487. dev_dbg(&pdev->dev, "f_if=%u if_ctl=%08x\n", f_if, u32tmp);
  488. buf[0] = (u32tmp >> 16) & 0xff;
  489. buf[1] = (u32tmp >> 8) & 0xff;
  490. buf[2] = (u32tmp >> 0) & 0xff;
  491. ret = rtl2832_sdr_wr_regs(dev, 0x119, buf, 3);
  492. if (ret)
  493. goto err;
  494. /* BB / IF mode */
  495. /* POR: 0x1b1=0x1f, 0x008=0x0d, 0x006=0x80 */
  496. if (f_if) {
  497. u8tmp1 = 0x1a; /* disable Zero-IF */
  498. u8tmp2 = 0x8d; /* enable ADC I */
  499. } else {
  500. u8tmp1 = 0x1b; /* enable Zero-IF, DC, IQ */
  501. u8tmp2 = 0xcd; /* enable ADC I, ADC Q */
  502. }
  503. ret = rtl2832_sdr_wr_reg(dev, 0x1b1, u8tmp1);
  504. if (ret)
  505. goto err;
  506. ret = rtl2832_sdr_wr_reg(dev, 0x008, u8tmp2);
  507. if (ret)
  508. goto err;
  509. ret = rtl2832_sdr_wr_reg(dev, 0x006, 0x80);
  510. if (ret)
  511. goto err;
  512. /* program sampling rate (resampling down) */
  513. u32tmp = div_u64(pdata->clk * 0x400000ULL, f_sr * 4U);
  514. u32tmp <<= 2;
  515. buf[0] = (u32tmp >> 24) & 0xff;
  516. buf[1] = (u32tmp >> 16) & 0xff;
  517. buf[2] = (u32tmp >> 8) & 0xff;
  518. buf[3] = (u32tmp >> 0) & 0xff;
  519. ret = rtl2832_sdr_wr_regs(dev, 0x19f, buf, 4);
  520. if (ret)
  521. goto err;
  522. /* low-pass filter */
  523. ret = rtl2832_sdr_wr_regs(dev, 0x11c,
  524. "\xca\xdc\xd7\xd8\xe0\xf2\x0e\x35\x06\x50\x9c\x0d\x71\x11\x14\x71\x74\x19\x41\xa5",
  525. 20);
  526. if (ret)
  527. goto err;
  528. ret = rtl2832_sdr_wr_regs(dev, 0x017, "\x11\x10", 2);
  529. if (ret)
  530. goto err;
  531. /* mode */
  532. ret = rtl2832_sdr_wr_regs(dev, 0x019, "\x05", 1);
  533. if (ret)
  534. goto err;
  535. ret = rtl2832_sdr_wr_regs(dev, 0x01a, "\x1b\x16\x0d\x06\x01\xff", 6);
  536. if (ret)
  537. goto err;
  538. /* FSM */
  539. ret = rtl2832_sdr_wr_regs(dev, 0x192, "\x00\xf0\x0f", 3);
  540. if (ret)
  541. goto err;
  542. /* PID filter */
  543. ret = rtl2832_sdr_wr_regs(dev, 0x061, "\x60", 1);
  544. if (ret)
  545. goto err;
  546. /* used RF tuner based settings */
  547. switch (pdata->tuner) {
  548. case RTL2832_SDR_TUNER_E4000:
  549. ret = rtl2832_sdr_wr_regs(dev, 0x112, "\x5a", 1);
  550. ret = rtl2832_sdr_wr_regs(dev, 0x102, "\x40", 1);
  551. ret = rtl2832_sdr_wr_regs(dev, 0x103, "\x5a", 1);
  552. ret = rtl2832_sdr_wr_regs(dev, 0x1c7, "\x30", 1);
  553. ret = rtl2832_sdr_wr_regs(dev, 0x104, "\xd0", 1);
  554. ret = rtl2832_sdr_wr_regs(dev, 0x105, "\xbe", 1);
  555. ret = rtl2832_sdr_wr_regs(dev, 0x1c8, "\x18", 1);
  556. ret = rtl2832_sdr_wr_regs(dev, 0x106, "\x35", 1);
  557. ret = rtl2832_sdr_wr_regs(dev, 0x1c9, "\x21", 1);
  558. ret = rtl2832_sdr_wr_regs(dev, 0x1ca, "\x21", 1);
  559. ret = rtl2832_sdr_wr_regs(dev, 0x1cb, "\x00", 1);
  560. ret = rtl2832_sdr_wr_regs(dev, 0x107, "\x40", 1);
  561. ret = rtl2832_sdr_wr_regs(dev, 0x1cd, "\x10", 1);
  562. ret = rtl2832_sdr_wr_regs(dev, 0x1ce, "\x10", 1);
  563. ret = rtl2832_sdr_wr_regs(dev, 0x108, "\x80", 1);
  564. ret = rtl2832_sdr_wr_regs(dev, 0x109, "\x7f", 1);
  565. ret = rtl2832_sdr_wr_regs(dev, 0x10a, "\x80", 1);
  566. ret = rtl2832_sdr_wr_regs(dev, 0x10b, "\x7f", 1);
  567. ret = rtl2832_sdr_wr_regs(dev, 0x00e, "\xfc", 1);
  568. ret = rtl2832_sdr_wr_regs(dev, 0x00e, "\xfc", 1);
  569. ret = rtl2832_sdr_wr_regs(dev, 0x011, "\xd4", 1);
  570. ret = rtl2832_sdr_wr_regs(dev, 0x1e5, "\xf0", 1);
  571. ret = rtl2832_sdr_wr_regs(dev, 0x1d9, "\x00", 1);
  572. ret = rtl2832_sdr_wr_regs(dev, 0x1db, "\x00", 1);
  573. ret = rtl2832_sdr_wr_regs(dev, 0x1dd, "\x14", 1);
  574. ret = rtl2832_sdr_wr_regs(dev, 0x1de, "\xec", 1);
  575. ret = rtl2832_sdr_wr_regs(dev, 0x1d8, "\x0c", 1);
  576. ret = rtl2832_sdr_wr_regs(dev, 0x1e6, "\x02", 1);
  577. ret = rtl2832_sdr_wr_regs(dev, 0x1d7, "\x09", 1);
  578. ret = rtl2832_sdr_wr_regs(dev, 0x00d, "\x83", 1);
  579. ret = rtl2832_sdr_wr_regs(dev, 0x010, "\x49", 1);
  580. ret = rtl2832_sdr_wr_regs(dev, 0x00d, "\x87", 1);
  581. ret = rtl2832_sdr_wr_regs(dev, 0x00d, "\x85", 1);
  582. ret = rtl2832_sdr_wr_regs(dev, 0x013, "\x02", 1);
  583. break;
  584. case RTL2832_SDR_TUNER_FC0012:
  585. case RTL2832_SDR_TUNER_FC0013:
  586. ret = rtl2832_sdr_wr_regs(dev, 0x112, "\x5a", 1);
  587. ret = rtl2832_sdr_wr_regs(dev, 0x102, "\x40", 1);
  588. ret = rtl2832_sdr_wr_regs(dev, 0x103, "\x5a", 1);
  589. ret = rtl2832_sdr_wr_regs(dev, 0x1c7, "\x2c", 1);
  590. ret = rtl2832_sdr_wr_regs(dev, 0x104, "\xcc", 1);
  591. ret = rtl2832_sdr_wr_regs(dev, 0x105, "\xbe", 1);
  592. ret = rtl2832_sdr_wr_regs(dev, 0x1c8, "\x16", 1);
  593. ret = rtl2832_sdr_wr_regs(dev, 0x106, "\x35", 1);
  594. ret = rtl2832_sdr_wr_regs(dev, 0x1c9, "\x21", 1);
  595. ret = rtl2832_sdr_wr_regs(dev, 0x1ca, "\x21", 1);
  596. ret = rtl2832_sdr_wr_regs(dev, 0x1cb, "\x00", 1);
  597. ret = rtl2832_sdr_wr_regs(dev, 0x107, "\x40", 1);
  598. ret = rtl2832_sdr_wr_regs(dev, 0x1cd, "\x10", 1);
  599. ret = rtl2832_sdr_wr_regs(dev, 0x1ce, "\x10", 1);
  600. ret = rtl2832_sdr_wr_regs(dev, 0x108, "\x80", 1);
  601. ret = rtl2832_sdr_wr_regs(dev, 0x109, "\x7f", 1);
  602. ret = rtl2832_sdr_wr_regs(dev, 0x10a, "\x80", 1);
  603. ret = rtl2832_sdr_wr_regs(dev, 0x10b, "\x7f", 1);
  604. ret = rtl2832_sdr_wr_regs(dev, 0x00e, "\xfc", 1);
  605. ret = rtl2832_sdr_wr_regs(dev, 0x00e, "\xfc", 1);
  606. ret = rtl2832_sdr_wr_regs(dev, 0x011, "\xe9\xbf", 2);
  607. ret = rtl2832_sdr_wr_regs(dev, 0x1e5, "\xf0", 1);
  608. ret = rtl2832_sdr_wr_regs(dev, 0x1d9, "\x00", 1);
  609. ret = rtl2832_sdr_wr_regs(dev, 0x1db, "\x00", 1);
  610. ret = rtl2832_sdr_wr_regs(dev, 0x1dd, "\x11", 1);
  611. ret = rtl2832_sdr_wr_regs(dev, 0x1de, "\xef", 1);
  612. ret = rtl2832_sdr_wr_regs(dev, 0x1d8, "\x0c", 1);
  613. ret = rtl2832_sdr_wr_regs(dev, 0x1e6, "\x02", 1);
  614. ret = rtl2832_sdr_wr_regs(dev, 0x1d7, "\x09", 1);
  615. break;
  616. case RTL2832_SDR_TUNER_R820T:
  617. case RTL2832_SDR_TUNER_R828D:
  618. ret = rtl2832_sdr_wr_regs(dev, 0x112, "\x5a", 1);
  619. ret = rtl2832_sdr_wr_regs(dev, 0x102, "\x40", 1);
  620. ret = rtl2832_sdr_wr_regs(dev, 0x115, "\x01", 1);
  621. ret = rtl2832_sdr_wr_regs(dev, 0x103, "\x80", 1);
  622. ret = rtl2832_sdr_wr_regs(dev, 0x1c7, "\x24", 1);
  623. ret = rtl2832_sdr_wr_regs(dev, 0x104, "\xcc", 1);
  624. ret = rtl2832_sdr_wr_regs(dev, 0x105, "\xbe", 1);
  625. ret = rtl2832_sdr_wr_regs(dev, 0x1c8, "\x14", 1);
  626. ret = rtl2832_sdr_wr_regs(dev, 0x106, "\x35", 1);
  627. ret = rtl2832_sdr_wr_regs(dev, 0x1c9, "\x21", 1);
  628. ret = rtl2832_sdr_wr_regs(dev, 0x1ca, "\x21", 1);
  629. ret = rtl2832_sdr_wr_regs(dev, 0x1cb, "\x00", 1);
  630. ret = rtl2832_sdr_wr_regs(dev, 0x107, "\x40", 1);
  631. ret = rtl2832_sdr_wr_regs(dev, 0x1cd, "\x10", 1);
  632. ret = rtl2832_sdr_wr_regs(dev, 0x1ce, "\x10", 1);
  633. ret = rtl2832_sdr_wr_regs(dev, 0x108, "\x80", 1);
  634. ret = rtl2832_sdr_wr_regs(dev, 0x109, "\x7f", 1);
  635. ret = rtl2832_sdr_wr_regs(dev, 0x10a, "\x80", 1);
  636. ret = rtl2832_sdr_wr_regs(dev, 0x10b, "\x7f", 1);
  637. ret = rtl2832_sdr_wr_regs(dev, 0x00e, "\xfc", 1);
  638. ret = rtl2832_sdr_wr_regs(dev, 0x00e, "\xfc", 1);
  639. ret = rtl2832_sdr_wr_regs(dev, 0x011, "\xf4", 1);
  640. break;
  641. case RTL2832_SDR_TUNER_FC2580:
  642. ret = rtl2832_sdr_wr_regs(dev, 0x112, "\x39", 1);
  643. ret = rtl2832_sdr_wr_regs(dev, 0x102, "\x40", 1);
  644. ret = rtl2832_sdr_wr_regs(dev, 0x103, "\x5a", 1);
  645. ret = rtl2832_sdr_wr_regs(dev, 0x1c7, "\x2c", 1);
  646. ret = rtl2832_sdr_wr_regs(dev, 0x104, "\xcc", 1);
  647. ret = rtl2832_sdr_wr_regs(dev, 0x105, "\xbe", 1);
  648. ret = rtl2832_sdr_wr_regs(dev, 0x1c8, "\x16", 1);
  649. ret = rtl2832_sdr_wr_regs(dev, 0x106, "\x35", 1);
  650. ret = rtl2832_sdr_wr_regs(dev, 0x1c9, "\x21", 1);
  651. ret = rtl2832_sdr_wr_regs(dev, 0x1ca, "\x21", 1);
  652. ret = rtl2832_sdr_wr_regs(dev, 0x1cb, "\x00", 1);
  653. ret = rtl2832_sdr_wr_regs(dev, 0x107, "\x40", 1);
  654. ret = rtl2832_sdr_wr_regs(dev, 0x1cd, "\x10", 1);
  655. ret = rtl2832_sdr_wr_regs(dev, 0x1ce, "\x10", 1);
  656. ret = rtl2832_sdr_wr_regs(dev, 0x108, "\x80", 1);
  657. ret = rtl2832_sdr_wr_regs(dev, 0x109, "\x7f", 1);
  658. ret = rtl2832_sdr_wr_regs(dev, 0x10a, "\x9c", 1);
  659. ret = rtl2832_sdr_wr_regs(dev, 0x10b, "\x7f", 1);
  660. ret = rtl2832_sdr_wr_regs(dev, 0x00e, "\xfc", 1);
  661. ret = rtl2832_sdr_wr_regs(dev, 0x00e, "\xfc", 1);
  662. ret = rtl2832_sdr_wr_regs(dev, 0x011, "\xe9\xf4", 2);
  663. break;
  664. default:
  665. dev_notice(&pdev->dev, "Unsupported tuner\n");
  666. }
  667. /* software reset */
  668. ret = rtl2832_sdr_wr_reg_mask(dev, 0x101, 0x04, 0x04);
  669. if (ret)
  670. goto err;
  671. ret = rtl2832_sdr_wr_reg_mask(dev, 0x101, 0x00, 0x04);
  672. if (ret)
  673. goto err;
  674. err:
  675. return ret;
  676. };
  677. static void rtl2832_sdr_unset_adc(struct rtl2832_sdr_dev *dev)
  678. {
  679. struct platform_device *pdev = dev->pdev;
  680. int ret;
  681. dev_dbg(&pdev->dev, "\n");
  682. /* PID filter */
  683. ret = rtl2832_sdr_wr_regs(dev, 0x061, "\xe0", 1);
  684. if (ret)
  685. goto err;
  686. /* mode */
  687. ret = rtl2832_sdr_wr_regs(dev, 0x019, "\x20", 1);
  688. if (ret)
  689. goto err;
  690. ret = rtl2832_sdr_wr_regs(dev, 0x017, "\x11\x10", 2);
  691. if (ret)
  692. goto err;
  693. /* FSM */
  694. ret = rtl2832_sdr_wr_regs(dev, 0x192, "\x00\x0f\xff", 3);
  695. if (ret)
  696. goto err;
  697. ret = rtl2832_sdr_wr_regs(dev, 0x13e, "\x40\x00", 2);
  698. if (ret)
  699. goto err;
  700. ret = rtl2832_sdr_wr_regs(dev, 0x115, "\x06\x3f\xce\xcc", 4);
  701. if (ret)
  702. goto err;
  703. err:
  704. return;
  705. };
  706. static int rtl2832_sdr_set_tuner_freq(struct rtl2832_sdr_dev *dev)
  707. {
  708. struct platform_device *pdev = dev->pdev;
  709. struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
  710. struct dvb_frontend *fe = pdata->dvb_frontend;
  711. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  712. struct v4l2_ctrl *bandwidth_auto;
  713. struct v4l2_ctrl *bandwidth;
  714. /*
  715. * tuner RF (Hz)
  716. */
  717. if (dev->f_tuner == 0)
  718. return 0;
  719. /*
  720. * bandwidth (Hz)
  721. */
  722. bandwidth_auto = v4l2_ctrl_find(&dev->hdl,
  723. V4L2_CID_RF_TUNER_BANDWIDTH_AUTO);
  724. bandwidth = v4l2_ctrl_find(&dev->hdl, V4L2_CID_RF_TUNER_BANDWIDTH);
  725. if (v4l2_ctrl_g_ctrl(bandwidth_auto)) {
  726. c->bandwidth_hz = dev->f_adc;
  727. v4l2_ctrl_s_ctrl(bandwidth, dev->f_adc);
  728. } else {
  729. c->bandwidth_hz = v4l2_ctrl_g_ctrl(bandwidth);
  730. }
  731. c->frequency = dev->f_tuner;
  732. c->delivery_system = SYS_DVBT;
  733. dev_dbg(&pdev->dev, "frequency=%u bandwidth=%d\n",
  734. c->frequency, c->bandwidth_hz);
  735. if (!test_bit(POWER_ON, &dev->flags))
  736. return 0;
  737. if (!V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, s_frequency)) {
  738. if (fe->ops.tuner_ops.set_params)
  739. fe->ops.tuner_ops.set_params(fe);
  740. }
  741. return 0;
  742. };
  743. static int rtl2832_sdr_set_tuner(struct rtl2832_sdr_dev *dev)
  744. {
  745. struct platform_device *pdev = dev->pdev;
  746. struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
  747. struct dvb_frontend *fe = pdata->dvb_frontend;
  748. dev_dbg(&pdev->dev, "\n");
  749. if (fe->ops.tuner_ops.init)
  750. fe->ops.tuner_ops.init(fe);
  751. return 0;
  752. };
  753. static void rtl2832_sdr_unset_tuner(struct rtl2832_sdr_dev *dev)
  754. {
  755. struct platform_device *pdev = dev->pdev;
  756. struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
  757. struct dvb_frontend *fe = pdata->dvb_frontend;
  758. dev_dbg(&pdev->dev, "\n");
  759. if (fe->ops.tuner_ops.sleep)
  760. fe->ops.tuner_ops.sleep(fe);
  761. return;
  762. };
  763. static int rtl2832_sdr_start_streaming(struct vb2_queue *vq, unsigned int count)
  764. {
  765. struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq);
  766. struct platform_device *pdev = dev->pdev;
  767. struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
  768. struct dvb_usb_device *d = pdata->dvb_usb_device;
  769. int ret;
  770. dev_dbg(&pdev->dev, "\n");
  771. if (!dev->udev)
  772. return -ENODEV;
  773. if (mutex_lock_interruptible(&dev->v4l2_lock))
  774. return -ERESTARTSYS;
  775. if (d->props->power_ctrl)
  776. d->props->power_ctrl(d, 1);
  777. /* enable ADC */
  778. if (d->props->frontend_ctrl)
  779. d->props->frontend_ctrl(pdata->dvb_frontend, 1);
  780. set_bit(POWER_ON, &dev->flags);
  781. /* wake-up tuner */
  782. if (V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, core, s_power))
  783. ret = v4l2_subdev_call(dev->v4l2_subdev, core, s_power, 1);
  784. else
  785. ret = rtl2832_sdr_set_tuner(dev);
  786. if (ret)
  787. goto err;
  788. ret = rtl2832_sdr_set_tuner_freq(dev);
  789. if (ret)
  790. goto err;
  791. ret = rtl2832_sdr_set_adc(dev);
  792. if (ret)
  793. goto err;
  794. ret = rtl2832_sdr_alloc_stream_bufs(dev);
  795. if (ret)
  796. goto err;
  797. ret = rtl2832_sdr_alloc_urbs(dev);
  798. if (ret)
  799. goto err;
  800. dev->sequence = 0;
  801. ret = rtl2832_sdr_submit_urbs(dev);
  802. if (ret)
  803. goto err;
  804. err:
  805. mutex_unlock(&dev->v4l2_lock);
  806. return ret;
  807. }
  808. static void rtl2832_sdr_stop_streaming(struct vb2_queue *vq)
  809. {
  810. struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq);
  811. struct platform_device *pdev = dev->pdev;
  812. struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
  813. struct dvb_usb_device *d = pdata->dvb_usb_device;
  814. dev_dbg(&pdev->dev, "\n");
  815. mutex_lock(&dev->v4l2_lock);
  816. rtl2832_sdr_kill_urbs(dev);
  817. rtl2832_sdr_free_urbs(dev);
  818. rtl2832_sdr_free_stream_bufs(dev);
  819. rtl2832_sdr_cleanup_queued_bufs(dev);
  820. rtl2832_sdr_unset_adc(dev);
  821. /* sleep tuner */
  822. if (V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, core, s_power))
  823. v4l2_subdev_call(dev->v4l2_subdev, core, s_power, 0);
  824. else
  825. rtl2832_sdr_unset_tuner(dev);
  826. clear_bit(POWER_ON, &dev->flags);
  827. /* disable ADC */
  828. if (d->props->frontend_ctrl)
  829. d->props->frontend_ctrl(pdata->dvb_frontend, 0);
  830. if (d->props->power_ctrl)
  831. d->props->power_ctrl(d, 0);
  832. mutex_unlock(&dev->v4l2_lock);
  833. }
  834. static struct vb2_ops rtl2832_sdr_vb2_ops = {
  835. .queue_setup = rtl2832_sdr_queue_setup,
  836. .buf_prepare = rtl2832_sdr_buf_prepare,
  837. .buf_queue = rtl2832_sdr_buf_queue,
  838. .start_streaming = rtl2832_sdr_start_streaming,
  839. .stop_streaming = rtl2832_sdr_stop_streaming,
  840. .wait_prepare = vb2_ops_wait_prepare,
  841. .wait_finish = vb2_ops_wait_finish,
  842. };
  843. static int rtl2832_sdr_g_tuner(struct file *file, void *priv,
  844. struct v4l2_tuner *v)
  845. {
  846. struct rtl2832_sdr_dev *dev = video_drvdata(file);
  847. struct platform_device *pdev = dev->pdev;
  848. int ret;
  849. dev_dbg(&pdev->dev, "index=%d type=%d\n", v->index, v->type);
  850. if (v->index == 0) {
  851. strlcpy(v->name, "ADC: Realtek RTL2832", sizeof(v->name));
  852. v->type = V4L2_TUNER_ADC;
  853. v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
  854. v->rangelow = 300000;
  855. v->rangehigh = 3200000;
  856. ret = 0;
  857. } else if (v->index == 1 &&
  858. V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, g_tuner)) {
  859. ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, g_tuner, v);
  860. } else if (v->index == 1) {
  861. strlcpy(v->name, "RF: <unknown>", sizeof(v->name));
  862. v->type = V4L2_TUNER_RF;
  863. v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
  864. v->rangelow = 50000000;
  865. v->rangehigh = 2000000000;
  866. ret = 0;
  867. } else {
  868. ret = -EINVAL;
  869. }
  870. return ret;
  871. }
  872. static int rtl2832_sdr_s_tuner(struct file *file, void *priv,
  873. const struct v4l2_tuner *v)
  874. {
  875. struct rtl2832_sdr_dev *dev = video_drvdata(file);
  876. struct platform_device *pdev = dev->pdev;
  877. int ret;
  878. dev_dbg(&pdev->dev, "\n");
  879. if (v->index == 0) {
  880. ret = 0;
  881. } else if (v->index == 1 &&
  882. V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, s_tuner)) {
  883. ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, s_tuner, v);
  884. } else if (v->index == 1) {
  885. ret = 0;
  886. } else {
  887. ret = -EINVAL;
  888. }
  889. return ret;
  890. }
  891. static int rtl2832_sdr_enum_freq_bands(struct file *file, void *priv,
  892. struct v4l2_frequency_band *band)
  893. {
  894. struct rtl2832_sdr_dev *dev = video_drvdata(file);
  895. struct platform_device *pdev = dev->pdev;
  896. int ret;
  897. dev_dbg(&pdev->dev, "tuner=%d type=%d index=%d\n",
  898. band->tuner, band->type, band->index);
  899. if (band->tuner == 0) {
  900. if (band->index >= ARRAY_SIZE(bands_adc))
  901. return -EINVAL;
  902. *band = bands_adc[band->index];
  903. ret = 0;
  904. } else if (band->tuner == 1 &&
  905. V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, enum_freq_bands)) {
  906. ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, enum_freq_bands, band);
  907. } else if (band->tuner == 1) {
  908. if (band->index >= ARRAY_SIZE(bands_fm))
  909. return -EINVAL;
  910. *band = bands_fm[band->index];
  911. ret = 0;
  912. } else {
  913. ret = -EINVAL;
  914. }
  915. return ret;
  916. }
  917. static int rtl2832_sdr_g_frequency(struct file *file, void *priv,
  918. struct v4l2_frequency *f)
  919. {
  920. struct rtl2832_sdr_dev *dev = video_drvdata(file);
  921. struct platform_device *pdev = dev->pdev;
  922. int ret;
  923. dev_dbg(&pdev->dev, "tuner=%d type=%d\n", f->tuner, f->type);
  924. if (f->tuner == 0) {
  925. f->frequency = dev->f_adc;
  926. f->type = V4L2_TUNER_ADC;
  927. ret = 0;
  928. } else if (f->tuner == 1 &&
  929. V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, g_frequency)) {
  930. f->type = V4L2_TUNER_RF;
  931. ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, g_frequency, f);
  932. } else if (f->tuner == 1) {
  933. f->frequency = dev->f_tuner;
  934. f->type = V4L2_TUNER_RF;
  935. ret = 0;
  936. } else {
  937. ret = -EINVAL;
  938. }
  939. return ret;
  940. }
  941. static int rtl2832_sdr_s_frequency(struct file *file, void *priv,
  942. const struct v4l2_frequency *f)
  943. {
  944. struct rtl2832_sdr_dev *dev = video_drvdata(file);
  945. struct platform_device *pdev = dev->pdev;
  946. int ret, band;
  947. dev_dbg(&pdev->dev, "tuner=%d type=%d frequency=%u\n",
  948. f->tuner, f->type, f->frequency);
  949. /* ADC band midpoints */
  950. #define BAND_ADC_0 ((bands_adc[0].rangehigh + bands_adc[1].rangelow) / 2)
  951. #define BAND_ADC_1 ((bands_adc[1].rangehigh + bands_adc[2].rangelow) / 2)
  952. if (f->tuner == 0 && f->type == V4L2_TUNER_ADC) {
  953. if (f->frequency < BAND_ADC_0)
  954. band = 0;
  955. else if (f->frequency < BAND_ADC_1)
  956. band = 1;
  957. else
  958. band = 2;
  959. dev->f_adc = clamp_t(unsigned int, f->frequency,
  960. bands_adc[band].rangelow,
  961. bands_adc[band].rangehigh);
  962. dev_dbg(&pdev->dev, "ADC frequency=%u Hz\n", dev->f_adc);
  963. ret = rtl2832_sdr_set_adc(dev);
  964. } else if (f->tuner == 1 &&
  965. V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, s_frequency)) {
  966. ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, s_frequency, f);
  967. } else if (f->tuner == 1) {
  968. dev->f_tuner = clamp_t(unsigned int, f->frequency,
  969. bands_fm[0].rangelow,
  970. bands_fm[0].rangehigh);
  971. dev_dbg(&pdev->dev, "RF frequency=%u Hz\n", f->frequency);
  972. ret = rtl2832_sdr_set_tuner_freq(dev);
  973. } else {
  974. ret = -EINVAL;
  975. }
  976. return ret;
  977. }
  978. static int rtl2832_sdr_enum_fmt_sdr_cap(struct file *file, void *priv,
  979. struct v4l2_fmtdesc *f)
  980. {
  981. struct rtl2832_sdr_dev *dev = video_drvdata(file);
  982. struct platform_device *pdev = dev->pdev;
  983. dev_dbg(&pdev->dev, "\n");
  984. if (f->index >= dev->num_formats)
  985. return -EINVAL;
  986. strlcpy(f->description, formats[f->index].name, sizeof(f->description));
  987. f->pixelformat = formats[f->index].pixelformat;
  988. return 0;
  989. }
  990. static int rtl2832_sdr_g_fmt_sdr_cap(struct file *file, void *priv,
  991. struct v4l2_format *f)
  992. {
  993. struct rtl2832_sdr_dev *dev = video_drvdata(file);
  994. struct platform_device *pdev = dev->pdev;
  995. dev_dbg(&pdev->dev, "\n");
  996. f->fmt.sdr.pixelformat = dev->pixelformat;
  997. f->fmt.sdr.buffersize = dev->buffersize;
  998. memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
  999. return 0;
  1000. }
  1001. static int rtl2832_sdr_s_fmt_sdr_cap(struct file *file, void *priv,
  1002. struct v4l2_format *f)
  1003. {
  1004. struct rtl2832_sdr_dev *dev = video_drvdata(file);
  1005. struct platform_device *pdev = dev->pdev;
  1006. struct vb2_queue *q = &dev->vb_queue;
  1007. int i;
  1008. dev_dbg(&pdev->dev, "pixelformat fourcc %4.4s\n",
  1009. (char *)&f->fmt.sdr.pixelformat);
  1010. if (vb2_is_busy(q))
  1011. return -EBUSY;
  1012. memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
  1013. for (i = 0; i < dev->num_formats; i++) {
  1014. if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
  1015. dev->pixelformat = formats[i].pixelformat;
  1016. dev->buffersize = formats[i].buffersize;
  1017. f->fmt.sdr.buffersize = formats[i].buffersize;
  1018. return 0;
  1019. }
  1020. }
  1021. dev->pixelformat = formats[0].pixelformat;
  1022. dev->buffersize = formats[0].buffersize;
  1023. f->fmt.sdr.pixelformat = formats[0].pixelformat;
  1024. f->fmt.sdr.buffersize = formats[0].buffersize;
  1025. return 0;
  1026. }
  1027. static int rtl2832_sdr_try_fmt_sdr_cap(struct file *file, void *priv,
  1028. struct v4l2_format *f)
  1029. {
  1030. struct rtl2832_sdr_dev *dev = video_drvdata(file);
  1031. struct platform_device *pdev = dev->pdev;
  1032. int i;
  1033. dev_dbg(&pdev->dev, "pixelformat fourcc %4.4s\n",
  1034. (char *)&f->fmt.sdr.pixelformat);
  1035. memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
  1036. for (i = 0; i < dev->num_formats; i++) {
  1037. if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
  1038. f->fmt.sdr.buffersize = formats[i].buffersize;
  1039. return 0;
  1040. }
  1041. }
  1042. f->fmt.sdr.pixelformat = formats[0].pixelformat;
  1043. f->fmt.sdr.buffersize = formats[0].buffersize;
  1044. return 0;
  1045. }
  1046. static const struct v4l2_ioctl_ops rtl2832_sdr_ioctl_ops = {
  1047. .vidioc_querycap = rtl2832_sdr_querycap,
  1048. .vidioc_enum_fmt_sdr_cap = rtl2832_sdr_enum_fmt_sdr_cap,
  1049. .vidioc_g_fmt_sdr_cap = rtl2832_sdr_g_fmt_sdr_cap,
  1050. .vidioc_s_fmt_sdr_cap = rtl2832_sdr_s_fmt_sdr_cap,
  1051. .vidioc_try_fmt_sdr_cap = rtl2832_sdr_try_fmt_sdr_cap,
  1052. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1053. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  1054. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  1055. .vidioc_querybuf = vb2_ioctl_querybuf,
  1056. .vidioc_qbuf = vb2_ioctl_qbuf,
  1057. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1058. .vidioc_streamon = vb2_ioctl_streamon,
  1059. .vidioc_streamoff = vb2_ioctl_streamoff,
  1060. .vidioc_g_tuner = rtl2832_sdr_g_tuner,
  1061. .vidioc_s_tuner = rtl2832_sdr_s_tuner,
  1062. .vidioc_enum_freq_bands = rtl2832_sdr_enum_freq_bands,
  1063. .vidioc_g_frequency = rtl2832_sdr_g_frequency,
  1064. .vidioc_s_frequency = rtl2832_sdr_s_frequency,
  1065. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1066. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1067. .vidioc_log_status = v4l2_ctrl_log_status,
  1068. };
  1069. static const struct v4l2_file_operations rtl2832_sdr_fops = {
  1070. .owner = THIS_MODULE,
  1071. .open = v4l2_fh_open,
  1072. .release = vb2_fop_release,
  1073. .read = vb2_fop_read,
  1074. .poll = vb2_fop_poll,
  1075. .mmap = vb2_fop_mmap,
  1076. .unlocked_ioctl = video_ioctl2,
  1077. };
  1078. static struct video_device rtl2832_sdr_template = {
  1079. .name = "Realtek RTL2832 SDR",
  1080. .release = video_device_release_empty,
  1081. .fops = &rtl2832_sdr_fops,
  1082. .ioctl_ops = &rtl2832_sdr_ioctl_ops,
  1083. };
  1084. static int rtl2832_sdr_s_ctrl(struct v4l2_ctrl *ctrl)
  1085. {
  1086. struct rtl2832_sdr_dev *dev =
  1087. container_of(ctrl->handler, struct rtl2832_sdr_dev,
  1088. hdl);
  1089. struct platform_device *pdev = dev->pdev;
  1090. struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
  1091. struct dvb_frontend *fe = pdata->dvb_frontend;
  1092. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1093. int ret;
  1094. dev_dbg(&pdev->dev, "id=%d name=%s val=%d min=%lld max=%lld step=%lld\n",
  1095. ctrl->id, ctrl->name, ctrl->val, ctrl->minimum, ctrl->maximum,
  1096. ctrl->step);
  1097. switch (ctrl->id) {
  1098. case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
  1099. case V4L2_CID_RF_TUNER_BANDWIDTH:
  1100. /* TODO: these controls should be moved to tuner drivers */
  1101. if (dev->bandwidth_auto->val) {
  1102. /* Round towards the closest legal value */
  1103. s32 val = dev->f_adc + div_u64(dev->bandwidth->step, 2);
  1104. u32 offset;
  1105. val = clamp_t(s32, val, dev->bandwidth->minimum,
  1106. dev->bandwidth->maximum);
  1107. offset = val - dev->bandwidth->minimum;
  1108. offset = dev->bandwidth->step *
  1109. div_u64(offset, dev->bandwidth->step);
  1110. dev->bandwidth->val = dev->bandwidth->minimum + offset;
  1111. }
  1112. c->bandwidth_hz = dev->bandwidth->val;
  1113. if (!test_bit(POWER_ON, &dev->flags))
  1114. return 0;
  1115. if (fe->ops.tuner_ops.set_params)
  1116. ret = fe->ops.tuner_ops.set_params(fe);
  1117. else
  1118. ret = 0;
  1119. break;
  1120. default:
  1121. ret = -EINVAL;
  1122. }
  1123. return ret;
  1124. }
  1125. static const struct v4l2_ctrl_ops rtl2832_sdr_ctrl_ops = {
  1126. .s_ctrl = rtl2832_sdr_s_ctrl,
  1127. };
  1128. static void rtl2832_sdr_video_release(struct v4l2_device *v)
  1129. {
  1130. struct rtl2832_sdr_dev *dev =
  1131. container_of(v, struct rtl2832_sdr_dev, v4l2_dev);
  1132. struct platform_device *pdev = dev->pdev;
  1133. dev_dbg(&pdev->dev, "\n");
  1134. v4l2_ctrl_handler_free(&dev->hdl);
  1135. v4l2_device_unregister(&dev->v4l2_dev);
  1136. kfree(dev);
  1137. }
  1138. /* Platform driver interface */
  1139. static int rtl2832_sdr_probe(struct platform_device *pdev)
  1140. {
  1141. struct rtl2832_sdr_dev *dev;
  1142. struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
  1143. const struct v4l2_ctrl_ops *ops = &rtl2832_sdr_ctrl_ops;
  1144. struct v4l2_subdev *subdev;
  1145. int ret;
  1146. dev_dbg(&pdev->dev, "\n");
  1147. if (!pdata) {
  1148. dev_err(&pdev->dev, "Cannot proceed without platform data\n");
  1149. ret = -EINVAL;
  1150. goto err;
  1151. }
  1152. if (!pdev->dev.parent->driver) {
  1153. dev_dbg(&pdev->dev, "No parent device\n");
  1154. ret = -EINVAL;
  1155. goto err;
  1156. }
  1157. /* try to refcount host drv since we are the consumer */
  1158. if (!try_module_get(pdev->dev.parent->driver->owner)) {
  1159. dev_err(&pdev->dev, "Refcount fail");
  1160. ret = -EINVAL;
  1161. goto err;
  1162. }
  1163. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1164. if (dev == NULL) {
  1165. ret = -ENOMEM;
  1166. goto err_module_put;
  1167. }
  1168. /* setup the state */
  1169. subdev = pdata->v4l2_subdev;
  1170. dev->v4l2_subdev = pdata->v4l2_subdev;
  1171. dev->pdev = pdev;
  1172. dev->udev = pdata->dvb_usb_device->udev;
  1173. dev->f_adc = bands_adc[0].rangelow;
  1174. dev->f_tuner = bands_fm[0].rangelow;
  1175. dev->pixelformat = formats[0].pixelformat;
  1176. dev->buffersize = formats[0].buffersize;
  1177. dev->num_formats = NUM_FORMATS;
  1178. if (!rtl2832_sdr_emulated_fmt)
  1179. dev->num_formats -= 1;
  1180. mutex_init(&dev->v4l2_lock);
  1181. mutex_init(&dev->vb_queue_lock);
  1182. spin_lock_init(&dev->queued_bufs_lock);
  1183. INIT_LIST_HEAD(&dev->queued_bufs);
  1184. /* Init videobuf2 queue structure */
  1185. dev->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE;
  1186. dev->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
  1187. dev->vb_queue.drv_priv = dev;
  1188. dev->vb_queue.buf_struct_size = sizeof(struct rtl2832_sdr_frame_buf);
  1189. dev->vb_queue.ops = &rtl2832_sdr_vb2_ops;
  1190. dev->vb_queue.mem_ops = &vb2_vmalloc_memops;
  1191. dev->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1192. ret = vb2_queue_init(&dev->vb_queue);
  1193. if (ret) {
  1194. dev_err(&pdev->dev, "Could not initialize vb2 queue\n");
  1195. goto err_kfree;
  1196. }
  1197. /* Register controls */
  1198. switch (pdata->tuner) {
  1199. case RTL2832_SDR_TUNER_E4000:
  1200. v4l2_ctrl_handler_init(&dev->hdl, 9);
  1201. if (subdev)
  1202. v4l2_ctrl_add_handler(&dev->hdl, subdev->ctrl_handler, NULL);
  1203. break;
  1204. case RTL2832_SDR_TUNER_R820T:
  1205. case RTL2832_SDR_TUNER_R828D:
  1206. v4l2_ctrl_handler_init(&dev->hdl, 2);
  1207. dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, ops,
  1208. V4L2_CID_RF_TUNER_BANDWIDTH_AUTO,
  1209. 0, 1, 1, 1);
  1210. dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, ops,
  1211. V4L2_CID_RF_TUNER_BANDWIDTH,
  1212. 0, 8000000, 100000, 0);
  1213. v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false);
  1214. break;
  1215. case RTL2832_SDR_TUNER_FC0012:
  1216. case RTL2832_SDR_TUNER_FC0013:
  1217. v4l2_ctrl_handler_init(&dev->hdl, 2);
  1218. dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, ops,
  1219. V4L2_CID_RF_TUNER_BANDWIDTH_AUTO,
  1220. 0, 1, 1, 1);
  1221. dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, ops,
  1222. V4L2_CID_RF_TUNER_BANDWIDTH,
  1223. 6000000, 8000000, 1000000,
  1224. 6000000);
  1225. v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false);
  1226. break;
  1227. case RTL2832_SDR_TUNER_FC2580:
  1228. v4l2_ctrl_handler_init(&dev->hdl, 2);
  1229. if (subdev)
  1230. v4l2_ctrl_add_handler(&dev->hdl, subdev->ctrl_handler,
  1231. NULL);
  1232. break;
  1233. default:
  1234. v4l2_ctrl_handler_init(&dev->hdl, 0);
  1235. dev_err(&pdev->dev, "Unsupported tuner\n");
  1236. goto err_v4l2_ctrl_handler_free;
  1237. }
  1238. if (dev->hdl.error) {
  1239. ret = dev->hdl.error;
  1240. dev_err(&pdev->dev, "Could not initialize controls\n");
  1241. goto err_v4l2_ctrl_handler_free;
  1242. }
  1243. /* Init video_device structure */
  1244. dev->vdev = rtl2832_sdr_template;
  1245. dev->vdev.queue = &dev->vb_queue;
  1246. dev->vdev.queue->lock = &dev->vb_queue_lock;
  1247. video_set_drvdata(&dev->vdev, dev);
  1248. /* Register the v4l2_device structure */
  1249. dev->v4l2_dev.release = rtl2832_sdr_video_release;
  1250. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  1251. if (ret) {
  1252. dev_err(&pdev->dev, "Failed to register v4l2-device %d\n", ret);
  1253. goto err_v4l2_ctrl_handler_free;
  1254. }
  1255. dev->v4l2_dev.ctrl_handler = &dev->hdl;
  1256. dev->vdev.v4l2_dev = &dev->v4l2_dev;
  1257. dev->vdev.lock = &dev->v4l2_lock;
  1258. dev->vdev.vfl_dir = VFL_DIR_RX;
  1259. ret = video_register_device(&dev->vdev, VFL_TYPE_SDR, -1);
  1260. if (ret) {
  1261. dev_err(&pdev->dev, "Failed to register as video device %d\n",
  1262. ret);
  1263. goto err_v4l2_device_unregister;
  1264. }
  1265. dev_info(&pdev->dev, "Registered as %s\n",
  1266. video_device_node_name(&dev->vdev));
  1267. dev_info(&pdev->dev, "Realtek RTL2832 SDR attached\n");
  1268. dev_notice(&pdev->dev,
  1269. "SDR API is still slightly experimental and functionality changes may follow\n");
  1270. platform_set_drvdata(pdev, dev);
  1271. return 0;
  1272. err_v4l2_device_unregister:
  1273. v4l2_device_unregister(&dev->v4l2_dev);
  1274. err_v4l2_ctrl_handler_free:
  1275. v4l2_ctrl_handler_free(&dev->hdl);
  1276. err_kfree:
  1277. kfree(dev);
  1278. err_module_put:
  1279. module_put(pdev->dev.parent->driver->owner);
  1280. err:
  1281. return ret;
  1282. }
  1283. static int rtl2832_sdr_remove(struct platform_device *pdev)
  1284. {
  1285. struct rtl2832_sdr_dev *dev = platform_get_drvdata(pdev);
  1286. dev_dbg(&pdev->dev, "\n");
  1287. mutex_lock(&dev->vb_queue_lock);
  1288. mutex_lock(&dev->v4l2_lock);
  1289. /* No need to keep the urbs around after disconnection */
  1290. dev->udev = NULL;
  1291. v4l2_device_disconnect(&dev->v4l2_dev);
  1292. video_unregister_device(&dev->vdev);
  1293. mutex_unlock(&dev->v4l2_lock);
  1294. mutex_unlock(&dev->vb_queue_lock);
  1295. v4l2_device_put(&dev->v4l2_dev);
  1296. module_put(pdev->dev.parent->driver->owner);
  1297. return 0;
  1298. }
  1299. static struct platform_driver rtl2832_sdr_driver = {
  1300. .driver = {
  1301. .name = "rtl2832_sdr",
  1302. },
  1303. .probe = rtl2832_sdr_probe,
  1304. .remove = rtl2832_sdr_remove,
  1305. };
  1306. module_platform_driver(rtl2832_sdr_driver);
  1307. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  1308. MODULE_DESCRIPTION("Realtek RTL2832 SDR driver");
  1309. MODULE_LICENSE("GPL");