rtl2832_sdr.c 43 KB

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