Input: psmouse - small formatting changes to better follow coding style
[pandora-kernel.git] / drivers / media / video / 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/version.h>
10 #include <linux/mm.h>
11 #include <linux/mutex.h>
12 #include <media/v4l2-ioctl.h>
13 #include <linux/sched.h>
14
15 #include "pd-common.h"
16 #include "vendorcmds.h"
17
18 static int set_frequency(struct poseidon *p, __u32 frequency);
19 static int poseidon_fm_close(struct file *filp);
20 static int poseidon_fm_open(struct file *filp);
21
22 #define TUNER_FREQ_MIN_FM 76000000
23 #define TUNER_FREQ_MAX_FM 108000000
24
25 #define MAX_PREEMPHASIS (V4L2_PREEMPHASIS_75_uS + 1)
26 static int preemphasis[MAX_PREEMPHASIS] = {
27         TLG_TUNE_ASTD_NONE,   /* V4L2_PREEMPHASIS_DISABLED */
28         TLG_TUNE_ASTD_FM_EUR, /* V4L2_PREEMPHASIS_50_uS    */
29         TLG_TUNE_ASTD_FM_US,  /* V4L2_PREEMPHASIS_75_uS    */
30 };
31
32 static int poseidon_check_mode_radio(struct poseidon *p)
33 {
34         int ret;
35         u32 status;
36
37         set_current_state(TASK_INTERRUPTIBLE);
38         schedule_timeout(HZ/2);
39         ret = usb_set_interface(p->udev, 0, BULK_ALTERNATE_IFACE);
40         if (ret < 0)
41                 goto out;
42
43         ret = set_tuner_mode(p, TLG_MODE_FM_RADIO);
44         if (ret != 0)
45                 goto out;
46
47         ret = send_set_req(p, SGNL_SRC_SEL, TLG_SIG_SRC_ANTENNA, &status);
48         ret = send_set_req(p, TUNER_AUD_ANA_STD,
49                                 p->radio_data.pre_emphasis, &status);
50         ret |= send_set_req(p, TUNER_AUD_MODE,
51                                 TLG_TUNE_TVAUDIO_MODE_STEREO, &status);
52         ret |= send_set_req(p, AUDIO_SAMPLE_RATE_SEL,
53                                 ATV_AUDIO_RATE_48K, &status);
54         ret |= send_set_req(p, TUNE_FREQ_SELECT, TUNER_FREQ_MIN_FM, &status);
55 out:
56         return ret;
57 }
58
59 #ifdef CONFIG_PM
60 static int pm_fm_suspend(struct poseidon *p)
61 {
62         logpm(p);
63         pm_alsa_suspend(p);
64         usb_set_interface(p->udev, 0, 0);
65         msleep(300);
66         return 0;
67 }
68
69 static int pm_fm_resume(struct poseidon *p)
70 {
71         logpm(p);
72         poseidon_check_mode_radio(p);
73         set_frequency(p, p->radio_data.fm_freq);
74         pm_alsa_resume(p);
75         return 0;
76 }
77 #endif
78
79 static int poseidon_fm_open(struct file *filp)
80 {
81         struct video_device *vfd = video_devdata(filp);
82         struct poseidon *p = video_get_drvdata(vfd);
83         int ret = 0;
84
85         if (!p)
86                 return -1;
87
88         mutex_lock(&p->lock);
89         if (p->state & POSEIDON_STATE_DISCONNECT) {
90                 ret = -ENODEV;
91                 goto out;
92         }
93
94         if (p->state && !(p->state & POSEIDON_STATE_FM)) {
95                 ret = -EBUSY;
96                 goto out;
97         }
98
99         usb_autopm_get_interface(p->interface);
100         if (0 == p->state) {
101                 /* default pre-emphasis */
102                 if (p->radio_data.pre_emphasis == 0)
103                         p->radio_data.pre_emphasis = TLG_TUNE_ASTD_FM_EUR;
104                 set_debug_mode(vfd, debug_mode);
105
106                 ret = poseidon_check_mode_radio(p);
107                 if (ret < 0) {
108                         usb_autopm_put_interface(p->interface);
109                         goto out;
110                 }
111                 p->state |= POSEIDON_STATE_FM;
112         }
113         p->radio_data.users++;
114         kref_get(&p->kref);
115         filp->private_data = p;
116 out:
117         mutex_unlock(&p->lock);
118         return ret;
119 }
120
121 static int poseidon_fm_close(struct file *filp)
122 {
123         struct poseidon *p = filp->private_data;
124         struct radio_data *fm = &p->radio_data;
125         uint32_t status;
126
127         mutex_lock(&p->lock);
128         fm->users--;
129         if (0 == fm->users)
130                 p->state &= ~POSEIDON_STATE_FM;
131
132         if (fm->is_radio_streaming && filp == p->file_for_stream) {
133                 fm->is_radio_streaming = 0;
134                 send_set_req(p, PLAY_SERVICE, TLG_TUNE_PLAY_SVC_STOP, &status);
135         }
136         usb_autopm_put_interface(p->interface);
137         mutex_unlock(&p->lock);
138
139         kref_put(&p->kref, poseidon_delete);
140         filp->private_data = NULL;
141         return 0;
142 }
143
144 static int vidioc_querycap(struct file *file, void *priv,
145                         struct v4l2_capability *v)
146 {
147         struct poseidon *p = file->private_data;
148
149         strlcpy(v->driver, "tele-radio", sizeof(v->driver));
150         strlcpy(v->card, "Telegent Poseidon", sizeof(v->card));
151         usb_make_path(p->udev, v->bus_info, sizeof(v->bus_info));
152         v->version = KERNEL_VERSION(0, 0, 1);
153         v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
154         return 0;
155 }
156
157 static const struct v4l2_file_operations poseidon_fm_fops = {
158         .owner         = THIS_MODULE,
159         .open          = poseidon_fm_open,
160         .release       = poseidon_fm_close,
161         .ioctl         = video_ioctl2,
162 };
163
164 int tlg_fm_vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *vt)
165 {
166         struct tuner_fm_sig_stat_s fm_stat = {};
167         int ret, status, count = 5;
168         struct poseidon *p = file->private_data;
169
170         if (vt->index != 0)
171                 return -EINVAL;
172
173         vt->type        = V4L2_TUNER_RADIO;
174         vt->capability  = V4L2_TUNER_CAP_STEREO;
175         vt->rangelow    = TUNER_FREQ_MIN_FM / 62500;
176         vt->rangehigh   = TUNER_FREQ_MAX_FM / 62500;
177         vt->rxsubchans  = V4L2_TUNER_SUB_STEREO;
178         vt->audmode     = V4L2_TUNER_MODE_STEREO;
179         vt->signal      = 0;
180         vt->afc         = 0;
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 int fm_get_freq(struct file *file, void *priv, struct v4l2_frequency *argp)
207 {
208         struct poseidon *p = file->private_data;
209
210         argp->frequency = p->radio_data.fm_freq;
211         return 0;
212 }
213
214 static int set_frequency(struct poseidon *p, __u32 frequency)
215 {
216         __u32 freq ;
217         int ret, status;
218
219         mutex_lock(&p->lock);
220
221         ret = send_set_req(p, TUNER_AUD_ANA_STD,
222                                 p->radio_data.pre_emphasis, &status);
223
224         freq =  (frequency * 125) * 500 / 1000;/* kHZ */
225         if (freq < TUNER_FREQ_MIN_FM/1000 || freq > TUNER_FREQ_MAX_FM/1000) {
226                 ret = -EINVAL;
227                 goto error;
228         }
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 = frequency;
244 error:
245         mutex_unlock(&p->lock);
246         return ret;
247 }
248
249 int fm_set_freq(struct file *file, void *priv, struct v4l2_frequency *argp)
250 {
251         struct poseidon *p = file->private_data;
252
253         p->file_for_stream  = file;
254 #ifdef CONFIG_PM
255         p->pm_suspend = pm_fm_suspend;
256         p->pm_resume  = pm_fm_resume;
257 #endif
258         return set_frequency(p, argp->frequency);
259 }
260
261 int tlg_fm_vidioc_g_ctrl(struct file *file, void *priv,
262                 struct v4l2_control *arg)
263 {
264         return 0;
265 }
266
267 int tlg_fm_vidioc_g_exts_ctrl(struct file *file, void *fh,
268                                 struct v4l2_ext_controls *ctrls)
269 {
270         struct poseidon *p = file->private_data;
271         int i;
272
273         if (ctrls->ctrl_class != V4L2_CTRL_CLASS_FM_TX)
274                 return -EINVAL;
275
276         for (i = 0; i < ctrls->count; i++) {
277                 struct v4l2_ext_control *ctrl = ctrls->controls + i;
278
279                 if (ctrl->id != V4L2_CID_TUNE_PREEMPHASIS)
280                         continue;
281
282                 if (i < MAX_PREEMPHASIS)
283                         ctrl->value = p->radio_data.pre_emphasis;
284         }
285         return 0;
286 }
287
288 int tlg_fm_vidioc_s_exts_ctrl(struct file *file, void *fh,
289                         struct v4l2_ext_controls *ctrls)
290 {
291         int i;
292
293         if (ctrls->ctrl_class != V4L2_CTRL_CLASS_FM_TX)
294                 return -EINVAL;
295
296         for (i = 0; i < ctrls->count; i++) {
297                 struct v4l2_ext_control *ctrl = ctrls->controls + i;
298
299                 if (ctrl->id != V4L2_CID_TUNE_PREEMPHASIS)
300                         continue;
301
302                 if (ctrl->value >= 0 && ctrl->value < MAX_PREEMPHASIS) {
303                         struct poseidon *p = file->private_data;
304                         int pre_emphasis = preemphasis[ctrl->value];
305                         u32 status;
306
307                         send_set_req(p, TUNER_AUD_ANA_STD,
308                                                 pre_emphasis, &status);
309                         p->radio_data.pre_emphasis = pre_emphasis;
310                 }
311         }
312         return 0;
313 }
314
315 int tlg_fm_vidioc_s_ctrl(struct file *file, void *priv,
316                 struct v4l2_control *ctrl)
317 {
318         return 0;
319 }
320
321 int tlg_fm_vidioc_queryctrl(struct file *file, void *priv,
322                 struct v4l2_queryctrl *ctrl)
323 {
324         if (!(ctrl->id & V4L2_CTRL_FLAG_NEXT_CTRL))
325                 return -EINVAL;
326
327         ctrl->id &= ~V4L2_CTRL_FLAG_NEXT_CTRL;
328         if (ctrl->id != V4L2_CID_TUNE_PREEMPHASIS) {
329                 /* return the next supported control */
330                 ctrl->id = V4L2_CID_TUNE_PREEMPHASIS;
331                 v4l2_ctrl_query_fill(ctrl, V4L2_PREEMPHASIS_DISABLED,
332                                         V4L2_PREEMPHASIS_75_uS, 1,
333                                         V4L2_PREEMPHASIS_50_uS);
334                 ctrl->flags = V4L2_CTRL_FLAG_UPDATE;
335                 return 0;
336         }
337         return -EINVAL;
338 }
339
340 int tlg_fm_vidioc_querymenu(struct file *file, void *fh,
341                                 struct v4l2_querymenu *qmenu)
342 {
343         return v4l2_ctrl_query_menu(qmenu, NULL, NULL);
344 }
345
346 static int vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *vt)
347 {
348         return vt->index > 0 ? -EINVAL : 0;
349 }
350 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *va)
351 {
352         return (va->index != 0) ? -EINVAL : 0;
353 }
354
355 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
356 {
357         a->index    = 0;
358         a->mode    = 0;
359         a->capability = V4L2_AUDCAP_STEREO;
360         strcpy(a->name, "Radio");
361         return 0;
362 }
363
364 static int vidioc_s_input(struct file *filp, void *priv, u32 i)
365 {
366         return (i != 0) ? -EINVAL : 0;
367 }
368
369 static int vidioc_g_input(struct file *filp, void *priv, u32 *i)
370 {
371         return (*i != 0) ? -EINVAL : 0;
372 }
373
374 static const struct v4l2_ioctl_ops poseidon_fm_ioctl_ops = {
375         .vidioc_querycap    = vidioc_querycap,
376         .vidioc_g_audio     = vidioc_g_audio,
377         .vidioc_s_audio     = vidioc_s_audio,
378         .vidioc_g_input     = vidioc_g_input,
379         .vidioc_s_input     = vidioc_s_input,
380         .vidioc_queryctrl   = tlg_fm_vidioc_queryctrl,
381         .vidioc_querymenu   = tlg_fm_vidioc_querymenu,
382         .vidioc_g_ctrl      = tlg_fm_vidioc_g_ctrl,
383         .vidioc_s_ctrl      = tlg_fm_vidioc_s_ctrl,
384         .vidioc_s_ext_ctrls = tlg_fm_vidioc_s_exts_ctrl,
385         .vidioc_g_ext_ctrls = tlg_fm_vidioc_g_exts_ctrl,
386         .vidioc_s_tuner     = vidioc_s_tuner,
387         .vidioc_g_tuner     = tlg_fm_vidioc_g_tuner,
388         .vidioc_g_frequency = fm_get_freq,
389         .vidioc_s_frequency = fm_set_freq,
390 };
391
392 static struct video_device poseidon_fm_template = {
393         .name       = "Telegent-Radio",
394         .fops       = &poseidon_fm_fops,
395         .minor      = -1,
396         .release    = video_device_release,
397         .ioctl_ops  = &poseidon_fm_ioctl_ops,
398 };
399
400 int poseidon_fm_init(struct poseidon *p)
401 {
402         struct video_device *fm_dev;
403
404         fm_dev = vdev_init(p, &poseidon_fm_template);
405         if (fm_dev == NULL)
406                 return -1;
407
408         if (video_register_device(fm_dev, VFL_TYPE_RADIO, -1) < 0) {
409                 video_device_release(fm_dev);
410                 return -1;
411         }
412         p->radio_data.fm_dev = fm_dev;
413         return 0;
414 }
415
416 int poseidon_fm_exit(struct poseidon *p)
417 {
418         destroy_video_device(&p->radio_data.fm_dev);
419         return 0;
420 }