1 #include <linux/init.h>
2 #include <linux/list.h>
3 #include <linux/module.h>
4 #include <linux/kernel.h>
5 #include <linux/bitmap.h>
8 #include <media/v4l2-dev.h>
10 #include <linux/mutex.h>
11 #include <media/v4l2-ioctl.h>
12 #include <linux/sched.h>
14 #include "pd-common.h"
15 #include "vendorcmds.h"
17 static int set_frequency(struct poseidon
*p
, __u32 frequency
);
18 static int poseidon_fm_close(struct file
*filp
);
19 static int poseidon_fm_open(struct file
*filp
);
21 #define TUNER_FREQ_MIN_FM 76000000
22 #define TUNER_FREQ_MAX_FM 108000000
24 #define MAX_PREEMPHASIS (V4L2_PREEMPHASIS_75_uS + 1)
25 static int preemphasis
[MAX_PREEMPHASIS
] = {
26 TLG_TUNE_ASTD_NONE
, /* V4L2_PREEMPHASIS_DISABLED */
27 TLG_TUNE_ASTD_FM_EUR
, /* V4L2_PREEMPHASIS_50_uS */
28 TLG_TUNE_ASTD_FM_US
, /* V4L2_PREEMPHASIS_75_uS */
31 static int poseidon_check_mode_radio(struct poseidon
*p
)
36 set_current_state(TASK_INTERRUPTIBLE
);
37 schedule_timeout(HZ
/2);
38 ret
= usb_set_interface(p
->udev
, 0, BULK_ALTERNATE_IFACE
);
42 ret
= set_tuner_mode(p
, TLG_MODE_FM_RADIO
);
46 ret
= send_set_req(p
, SGNL_SRC_SEL
, TLG_SIG_SRC_ANTENNA
, &status
);
47 ret
= send_set_req(p
, TUNER_AUD_ANA_STD
,
48 p
->radio_data
.pre_emphasis
, &status
);
49 ret
|= send_set_req(p
, TUNER_AUD_MODE
,
50 TLG_TUNE_TVAUDIO_MODE_STEREO
, &status
);
51 ret
|= send_set_req(p
, AUDIO_SAMPLE_RATE_SEL
,
52 ATV_AUDIO_RATE_48K
, &status
);
53 ret
|= send_set_req(p
, TUNE_FREQ_SELECT
, TUNER_FREQ_MIN_FM
, &status
);
59 static int pm_fm_suspend(struct poseidon
*p
)
63 usb_set_interface(p
->udev
, 0, 0);
68 static int pm_fm_resume(struct poseidon
*p
)
71 poseidon_check_mode_radio(p
);
72 set_frequency(p
, p
->radio_data
.fm_freq
);
78 static int poseidon_fm_open(struct file
*filp
)
80 struct video_device
*vfd
= video_devdata(filp
);
81 struct poseidon
*p
= video_get_drvdata(vfd
);
88 if (p
->state
& POSEIDON_STATE_DISCONNECT
) {
93 if (p
->state
&& !(p
->state
& POSEIDON_STATE_FM
)) {
98 usb_autopm_get_interface(p
->interface
);
100 /* default pre-emphasis */
101 if (p
->radio_data
.pre_emphasis
== 0)
102 p
->radio_data
.pre_emphasis
= TLG_TUNE_ASTD_FM_EUR
;
103 set_debug_mode(vfd
, debug_mode
);
105 ret
= poseidon_check_mode_radio(p
);
107 usb_autopm_put_interface(p
->interface
);
110 p
->state
|= POSEIDON_STATE_FM
;
112 p
->radio_data
.users
++;
114 filp
->private_data
= p
;
116 mutex_unlock(&p
->lock
);
120 static int poseidon_fm_close(struct file
*filp
)
122 struct poseidon
*p
= filp
->private_data
;
123 struct radio_data
*fm
= &p
->radio_data
;
126 mutex_lock(&p
->lock
);
129 p
->state
&= ~POSEIDON_STATE_FM
;
131 if (fm
->is_radio_streaming
&& filp
== p
->file_for_stream
) {
132 fm
->is_radio_streaming
= 0;
133 send_set_req(p
, PLAY_SERVICE
, TLG_TUNE_PLAY_SVC_STOP
, &status
);
135 usb_autopm_put_interface(p
->interface
);
136 mutex_unlock(&p
->lock
);
138 kref_put(&p
->kref
, poseidon_delete
);
139 filp
->private_data
= NULL
;
143 static int vidioc_querycap(struct file
*file
, void *priv
,
144 struct v4l2_capability
*v
)
146 struct poseidon
*p
= file
->private_data
;
148 strlcpy(v
->driver
, "tele-radio", sizeof(v
->driver
));
149 strlcpy(v
->card
, "Telegent Poseidon", sizeof(v
->card
));
150 usb_make_path(p
->udev
, v
->bus_info
, sizeof(v
->bus_info
));
151 v
->capabilities
= V4L2_CAP_TUNER
| V4L2_CAP_RADIO
;
155 static const struct v4l2_file_operations poseidon_fm_fops
= {
156 .owner
= THIS_MODULE
,
157 .open
= poseidon_fm_open
,
158 .release
= poseidon_fm_close
,
159 .ioctl
= video_ioctl2
,
162 static int tlg_fm_vidioc_g_tuner(struct file
*file
, void *priv
,
163 struct v4l2_tuner
*vt
)
165 struct tuner_fm_sig_stat_s fm_stat
= {};
166 int ret
, status
, count
= 5;
167 struct poseidon
*p
= file
->private_data
;
172 vt
->type
= V4L2_TUNER_RADIO
;
173 vt
->capability
= V4L2_TUNER_CAP_STEREO
;
174 vt
->rangelow
= TUNER_FREQ_MIN_FM
/ 62500;
175 vt
->rangehigh
= TUNER_FREQ_MAX_FM
/ 62500;
176 vt
->rxsubchans
= V4L2_TUNER_SUB_STEREO
;
177 vt
->audmode
= V4L2_TUNER_MODE_STEREO
;
181 mutex_lock(&p
->lock
);
182 ret
= send_get_req(p
, TUNER_STATUS
, TLG_MODE_FM_RADIO
,
183 &fm_stat
, &status
, sizeof(fm_stat
));
185 while (fm_stat
.sig_lock_busy
&& count
-- && !ret
) {
186 set_current_state(TASK_INTERRUPTIBLE
);
187 schedule_timeout(HZ
);
189 ret
= send_get_req(p
, TUNER_STATUS
, TLG_MODE_FM_RADIO
,
190 &fm_stat
, &status
, sizeof(fm_stat
));
192 mutex_unlock(&p
->lock
);
196 } else if ((fm_stat
.sig_present
|| fm_stat
.sig_locked
)
197 && fm_stat
.sig_strength
== 0) {
200 vt
->signal
= (fm_stat
.sig_strength
* 255 / 10) << 8;
205 static int fm_get_freq(struct file
*file
, void *priv
,
206 struct v4l2_frequency
*argp
)
208 struct poseidon
*p
= file
->private_data
;
210 argp
->frequency
= p
->radio_data
.fm_freq
;
214 static int set_frequency(struct poseidon
*p
, __u32 frequency
)
219 mutex_lock(&p
->lock
);
221 ret
= send_set_req(p
, TUNER_AUD_ANA_STD
,
222 p
->radio_data
.pre_emphasis
, &status
);
224 freq
= (frequency
* 125) * 500 / 1000;/* kHZ */
225 if (freq
< TUNER_FREQ_MIN_FM
/1000 || freq
> TUNER_FREQ_MAX_FM
/1000) {
230 ret
= send_set_req(p
, TUNE_FREQ_SELECT
, freq
, &status
);
233 ret
= send_set_req(p
, TAKE_REQUEST
, 0, &status
);
235 set_current_state(TASK_INTERRUPTIBLE
);
236 schedule_timeout(HZ
/4);
237 if (!p
->radio_data
.is_radio_streaming
) {
238 ret
= send_set_req(p
, TAKE_REQUEST
, 0, &status
);
239 ret
= send_set_req(p
, PLAY_SERVICE
,
240 TLG_TUNE_PLAY_SVC_START
, &status
);
241 p
->radio_data
.is_radio_streaming
= 1;
243 p
->radio_data
.fm_freq
= frequency
;
245 mutex_unlock(&p
->lock
);
249 static int fm_set_freq(struct file
*file
, void *priv
,
250 struct v4l2_frequency
*argp
)
252 struct poseidon
*p
= file
->private_data
;
254 p
->file_for_stream
= file
;
256 p
->pm_suspend
= pm_fm_suspend
;
257 p
->pm_resume
= pm_fm_resume
;
259 return set_frequency(p
, argp
->frequency
);
262 static int tlg_fm_vidioc_g_ctrl(struct file
*file
, void *priv
,
263 struct v4l2_control
*arg
)
268 static int tlg_fm_vidioc_g_exts_ctrl(struct file
*file
, void *fh
,
269 struct v4l2_ext_controls
*ctrls
)
271 struct poseidon
*p
= file
->private_data
;
274 if (ctrls
->ctrl_class
!= V4L2_CTRL_CLASS_FM_TX
)
277 for (i
= 0; i
< ctrls
->count
; i
++) {
278 struct v4l2_ext_control
*ctrl
= ctrls
->controls
+ i
;
280 if (ctrl
->id
!= V4L2_CID_TUNE_PREEMPHASIS
)
283 if (i
< MAX_PREEMPHASIS
)
284 ctrl
->value
= p
->radio_data
.pre_emphasis
;
289 static int tlg_fm_vidioc_s_exts_ctrl(struct file
*file
, void *fh
,
290 struct v4l2_ext_controls
*ctrls
)
294 if (ctrls
->ctrl_class
!= V4L2_CTRL_CLASS_FM_TX
)
297 for (i
= 0; i
< ctrls
->count
; i
++) {
298 struct v4l2_ext_control
*ctrl
= ctrls
->controls
+ i
;
300 if (ctrl
->id
!= V4L2_CID_TUNE_PREEMPHASIS
)
303 if (ctrl
->value
>= 0 && ctrl
->value
< MAX_PREEMPHASIS
) {
304 struct poseidon
*p
= file
->private_data
;
305 int pre_emphasis
= preemphasis
[ctrl
->value
];
308 send_set_req(p
, TUNER_AUD_ANA_STD
,
309 pre_emphasis
, &status
);
310 p
->radio_data
.pre_emphasis
= pre_emphasis
;
316 static int tlg_fm_vidioc_s_ctrl(struct file
*file
, void *priv
,
317 struct v4l2_control
*ctrl
)
322 static int tlg_fm_vidioc_queryctrl(struct file
*file
, void *priv
,
323 struct v4l2_queryctrl
*ctrl
)
325 if (!(ctrl
->id
& V4L2_CTRL_FLAG_NEXT_CTRL
))
328 ctrl
->id
&= ~V4L2_CTRL_FLAG_NEXT_CTRL
;
329 if (ctrl
->id
!= V4L2_CID_TUNE_PREEMPHASIS
) {
330 /* return the next supported control */
331 ctrl
->id
= V4L2_CID_TUNE_PREEMPHASIS
;
332 v4l2_ctrl_query_fill(ctrl
, V4L2_PREEMPHASIS_DISABLED
,
333 V4L2_PREEMPHASIS_75_uS
, 1,
334 V4L2_PREEMPHASIS_50_uS
);
335 ctrl
->flags
= V4L2_CTRL_FLAG_UPDATE
;
341 static int tlg_fm_vidioc_querymenu(struct file
*file
, void *fh
,
342 struct v4l2_querymenu
*qmenu
)
344 return v4l2_ctrl_query_menu(qmenu
, NULL
, NULL
);
347 static int vidioc_s_tuner(struct file
*file
, void *priv
, struct v4l2_tuner
*vt
)
349 return vt
->index
> 0 ? -EINVAL
: 0;
351 static int vidioc_s_audio(struct file
*file
, void *priv
, struct v4l2_audio
*va
)
353 return (va
->index
!= 0) ? -EINVAL
: 0;
356 static int vidioc_g_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
360 a
->capability
= V4L2_AUDCAP_STEREO
;
361 strcpy(a
->name
, "Radio");
365 static int vidioc_s_input(struct file
*filp
, void *priv
, u32 i
)
367 return (i
!= 0) ? -EINVAL
: 0;
370 static int vidioc_g_input(struct file
*filp
, void *priv
, u32
*i
)
372 return (*i
!= 0) ? -EINVAL
: 0;
375 static const struct v4l2_ioctl_ops poseidon_fm_ioctl_ops
= {
376 .vidioc_querycap
= vidioc_querycap
,
377 .vidioc_g_audio
= vidioc_g_audio
,
378 .vidioc_s_audio
= vidioc_s_audio
,
379 .vidioc_g_input
= vidioc_g_input
,
380 .vidioc_s_input
= vidioc_s_input
,
381 .vidioc_queryctrl
= tlg_fm_vidioc_queryctrl
,
382 .vidioc_querymenu
= tlg_fm_vidioc_querymenu
,
383 .vidioc_g_ctrl
= tlg_fm_vidioc_g_ctrl
,
384 .vidioc_s_ctrl
= tlg_fm_vidioc_s_ctrl
,
385 .vidioc_s_ext_ctrls
= tlg_fm_vidioc_s_exts_ctrl
,
386 .vidioc_g_ext_ctrls
= tlg_fm_vidioc_g_exts_ctrl
,
387 .vidioc_s_tuner
= vidioc_s_tuner
,
388 .vidioc_g_tuner
= tlg_fm_vidioc_g_tuner
,
389 .vidioc_g_frequency
= fm_get_freq
,
390 .vidioc_s_frequency
= fm_set_freq
,
393 static struct video_device poseidon_fm_template
= {
394 .name
= "Telegent-Radio",
395 .fops
= &poseidon_fm_fops
,
397 .release
= video_device_release
,
398 .ioctl_ops
= &poseidon_fm_ioctl_ops
,
401 int poseidon_fm_init(struct poseidon
*p
)
403 struct video_device
*fm_dev
;
405 fm_dev
= vdev_init(p
, &poseidon_fm_template
);
409 if (video_register_device(fm_dev
, VFL_TYPE_RADIO
, -1) < 0) {
410 video_device_release(fm_dev
);
413 p
->radio_data
.fm_dev
= fm_dev
;
417 int poseidon_fm_exit(struct poseidon
*p
)
419 destroy_video_device(&p
->radio_data
.fm_dev
);