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