[media] tlg2300: embed video_device instead of allocating it
[deliverable/linux.git] / drivers / media / usb / tlg2300 / pd-radio.c
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>
6 #include <linux/usb.h>
7 #include <linux/i2c.h>
8 #include <media/v4l2-dev.h>
9 #include <linux/mm.h>
10 #include <linux/mutex.h>
11 #include <media/v4l2-ioctl.h>
12 #include <linux/sched.h>
13
14 #include "pd-common.h"
15 #include "vendorcmds.h"
16
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);
20
21 #define TUNER_FREQ_MIN_FM 76000000U
22 #define TUNER_FREQ_MAX_FM 108000000U
23
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 */
29 };
30
31 static int poseidon_check_mode_radio(struct poseidon *p)
32 {
33 int ret;
34 u32 status;
35
36 set_current_state(TASK_INTERRUPTIBLE);
37 schedule_timeout(HZ/2);
38 ret = usb_set_interface(p->udev, 0, BULK_ALTERNATE_IFACE);
39 if (ret < 0)
40 goto out;
41
42 ret = set_tuner_mode(p, TLG_MODE_FM_RADIO);
43 if (ret != 0)
44 goto out;
45
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);
54 out:
55 return ret;
56 }
57
58 #ifdef CONFIG_PM
59 static int pm_fm_suspend(struct poseidon *p)
60 {
61 logpm(p);
62 pm_alsa_suspend(p);
63 usb_set_interface(p->udev, 0, 0);
64 msleep(300);
65 return 0;
66 }
67
68 static int pm_fm_resume(struct poseidon *p)
69 {
70 logpm(p);
71 poseidon_check_mode_radio(p);
72 set_frequency(p, p->radio_data.fm_freq);
73 pm_alsa_resume(p);
74 return 0;
75 }
76 #endif
77
78 static int poseidon_fm_open(struct file *filp)
79 {
80 struct video_device *vfd = video_devdata(filp);
81 struct poseidon *p = video_get_drvdata(vfd);
82 int ret = 0;
83
84 if (!p)
85 return -1;
86
87 mutex_lock(&p->lock);
88 if (p->state & POSEIDON_STATE_DISCONNECT) {
89 ret = -ENODEV;
90 goto out;
91 }
92
93 if (p->state && !(p->state & POSEIDON_STATE_FM)) {
94 ret = -EBUSY;
95 goto out;
96 }
97
98 usb_autopm_get_interface(p->interface);
99 if (0 == p->state) {
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);
104
105 ret = poseidon_check_mode_radio(p);
106 if (ret < 0) {
107 usb_autopm_put_interface(p->interface);
108 goto out;
109 }
110 p->state |= POSEIDON_STATE_FM;
111 }
112 p->radio_data.users++;
113 kref_get(&p->kref);
114 filp->private_data = p;
115 out:
116 mutex_unlock(&p->lock);
117 return ret;
118 }
119
120 static int poseidon_fm_close(struct file *filp)
121 {
122 struct poseidon *p = filp->private_data;
123 struct radio_data *fm = &p->radio_data;
124 uint32_t status;
125
126 mutex_lock(&p->lock);
127 fm->users--;
128 if (0 == fm->users)
129 p->state &= ~POSEIDON_STATE_FM;
130
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);
134 }
135 usb_autopm_put_interface(p->interface);
136 mutex_unlock(&p->lock);
137
138 kref_put(&p->kref, poseidon_delete);
139 filp->private_data = NULL;
140 return 0;
141 }
142
143 static int vidioc_querycap(struct file *file, void *priv,
144 struct v4l2_capability *v)
145 {
146 struct poseidon *p = file->private_data;
147
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;
152 return 0;
153 }
154
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 .unlocked_ioctl = video_ioctl2,
160 };
161
162 static int tlg_fm_vidioc_g_tuner(struct file *file, void *priv,
163 struct v4l2_tuner *vt)
164 {
165 struct tuner_fm_sig_stat_s fm_stat = {};
166 int ret, status, count = 5;
167 struct poseidon *p = file->private_data;
168
169 if (vt->index != 0)
170 return -EINVAL;
171
172 vt->type = V4L2_TUNER_RADIO;
173 vt->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LOW;
174 vt->rangelow = TUNER_FREQ_MIN_FM * 2 / 125;
175 vt->rangehigh = TUNER_FREQ_MAX_FM * 2 / 125;
176 vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
177 vt->audmode = V4L2_TUNER_MODE_STEREO;
178 vt->signal = 0;
179 vt->afc = 0;
180 strlcpy(vt->name, "Radio", sizeof(vt->name));
181
182 mutex_lock(&p->lock);
183 ret = send_get_req(p, TUNER_STATUS, TLG_MODE_FM_RADIO,
184 &fm_stat, &status, sizeof(fm_stat));
185
186 while (fm_stat.sig_lock_busy && count-- && !ret) {
187 set_current_state(TASK_INTERRUPTIBLE);
188 schedule_timeout(HZ);
189
190 ret = send_get_req(p, TUNER_STATUS, TLG_MODE_FM_RADIO,
191 &fm_stat, &status, sizeof(fm_stat));
192 }
193 mutex_unlock(&p->lock);
194
195 if (ret || status) {
196 vt->signal = 0;
197 } else if ((fm_stat.sig_present || fm_stat.sig_locked)
198 && fm_stat.sig_strength == 0) {
199 vt->signal = 0xffff;
200 } else
201 vt->signal = (fm_stat.sig_strength * 255 / 10) << 8;
202
203 return 0;
204 }
205
206 static int fm_get_freq(struct file *file, void *priv,
207 struct v4l2_frequency *argp)
208 {
209 struct poseidon *p = file->private_data;
210
211 if (argp->tuner)
212 return -EINVAL;
213 argp->frequency = p->radio_data.fm_freq;
214 return 0;
215 }
216
217 static int set_frequency(struct poseidon *p, __u32 frequency)
218 {
219 __u32 freq ;
220 int ret, status;
221
222 mutex_lock(&p->lock);
223
224 ret = send_set_req(p, TUNER_AUD_ANA_STD,
225 p->radio_data.pre_emphasis, &status);
226
227 freq = (frequency * 125) / 2; /* Hz */
228 freq = clamp(freq, TUNER_FREQ_MIN_FM, TUNER_FREQ_MAX_FM);
229
230 ret = send_set_req(p, TUNE_FREQ_SELECT, freq, &status);
231 if (ret < 0)
232 goto error ;
233 ret = send_set_req(p, TAKE_REQUEST, 0, &status);
234
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;
242 }
243 p->radio_data.fm_freq = freq * 2 / 125;
244 error:
245 mutex_unlock(&p->lock);
246 return ret;
247 }
248
249 static int fm_set_freq(struct file *file, void *priv,
250 struct v4l2_frequency *argp)
251 {
252 struct poseidon *p = file->private_data;
253
254 if (argp->tuner)
255 return -EINVAL;
256 p->file_for_stream = file;
257 #ifdef CONFIG_PM
258 p->pm_suspend = pm_fm_suspend;
259 p->pm_resume = pm_fm_resume;
260 #endif
261 return set_frequency(p, argp->frequency);
262 }
263
264 static int tlg_fm_vidioc_g_ctrl(struct file *file, void *priv,
265 struct v4l2_control *arg)
266 {
267 return 0;
268 }
269
270 static int tlg_fm_vidioc_g_exts_ctrl(struct file *file, void *fh,
271 struct v4l2_ext_controls *ctrls)
272 {
273 struct poseidon *p = file->private_data;
274 int i;
275
276 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_FM_TX)
277 return -EINVAL;
278
279 for (i = 0; i < ctrls->count; i++) {
280 struct v4l2_ext_control *ctrl = ctrls->controls + i;
281
282 if (ctrl->id != V4L2_CID_TUNE_PREEMPHASIS)
283 continue;
284
285 if (i < MAX_PREEMPHASIS)
286 ctrl->value = p->radio_data.pre_emphasis;
287 }
288 return 0;
289 }
290
291 static int tlg_fm_vidioc_s_exts_ctrl(struct file *file, void *fh,
292 struct v4l2_ext_controls *ctrls)
293 {
294 int i;
295
296 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_FM_TX)
297 return -EINVAL;
298
299 for (i = 0; i < ctrls->count; i++) {
300 struct v4l2_ext_control *ctrl = ctrls->controls + i;
301
302 if (ctrl->id != V4L2_CID_TUNE_PREEMPHASIS)
303 continue;
304
305 if (ctrl->value >= 0 && ctrl->value < MAX_PREEMPHASIS) {
306 struct poseidon *p = file->private_data;
307 int pre_emphasis = preemphasis[ctrl->value];
308 u32 status;
309
310 send_set_req(p, TUNER_AUD_ANA_STD,
311 pre_emphasis, &status);
312 p->radio_data.pre_emphasis = pre_emphasis;
313 }
314 }
315 return 0;
316 }
317
318 static int tlg_fm_vidioc_s_ctrl(struct file *file, void *priv,
319 struct v4l2_control *ctrl)
320 {
321 return 0;
322 }
323
324 static int tlg_fm_vidioc_queryctrl(struct file *file, void *priv,
325 struct v4l2_queryctrl *ctrl)
326 {
327 if (!(ctrl->id & V4L2_CTRL_FLAG_NEXT_CTRL))
328 return -EINVAL;
329
330 ctrl->id &= ~V4L2_CTRL_FLAG_NEXT_CTRL;
331 if (ctrl->id != V4L2_CID_TUNE_PREEMPHASIS) {
332 /* return the next supported control */
333 ctrl->id = V4L2_CID_TUNE_PREEMPHASIS;
334 v4l2_ctrl_query_fill(ctrl, V4L2_PREEMPHASIS_DISABLED,
335 V4L2_PREEMPHASIS_75_uS, 1,
336 V4L2_PREEMPHASIS_50_uS);
337 ctrl->flags = V4L2_CTRL_FLAG_UPDATE;
338 return 0;
339 }
340 return -EINVAL;
341 }
342
343 static int tlg_fm_vidioc_querymenu(struct file *file, void *fh,
344 struct v4l2_querymenu *qmenu)
345 {
346 return v4l2_ctrl_query_menu(qmenu, NULL, NULL);
347 }
348
349 static int vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *vt)
350 {
351 return vt->index > 0 ? -EINVAL : 0;
352 }
353
354 static const struct v4l2_ioctl_ops poseidon_fm_ioctl_ops = {
355 .vidioc_querycap = vidioc_querycap,
356 .vidioc_queryctrl = tlg_fm_vidioc_queryctrl,
357 .vidioc_querymenu = tlg_fm_vidioc_querymenu,
358 .vidioc_g_ctrl = tlg_fm_vidioc_g_ctrl,
359 .vidioc_s_ctrl = tlg_fm_vidioc_s_ctrl,
360 .vidioc_s_ext_ctrls = tlg_fm_vidioc_s_exts_ctrl,
361 .vidioc_g_ext_ctrls = tlg_fm_vidioc_g_exts_ctrl,
362 .vidioc_s_tuner = vidioc_s_tuner,
363 .vidioc_g_tuner = tlg_fm_vidioc_g_tuner,
364 .vidioc_g_frequency = fm_get_freq,
365 .vidioc_s_frequency = fm_set_freq,
366 };
367
368 static struct video_device poseidon_fm_template = {
369 .name = "Telegent-Radio",
370 .fops = &poseidon_fm_fops,
371 .minor = -1,
372 .release = video_device_release_empty,
373 .ioctl_ops = &poseidon_fm_ioctl_ops,
374 };
375
376 int poseidon_fm_init(struct poseidon *p)
377 {
378 struct video_device *vfd = &p->radio_data.fm_dev;
379
380 *vfd = poseidon_fm_template;
381 vfd->v4l2_dev = &p->v4l2_dev;
382 video_set_drvdata(vfd, p);
383
384 set_frequency(p, TUNER_FREQ_MIN_FM);
385 return video_register_device(vfd, VFL_TYPE_RADIO, -1);
386 }
387
388 int poseidon_fm_exit(struct poseidon *p)
389 {
390 return 0;
391 }
This page took 0.039816 seconds and 5 git commands to generate.