aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video/tlg2300
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media/video/tlg2300')
-rw-r--r--drivers/media/video/tlg2300/Kconfig16
-rw-r--r--drivers/media/video/tlg2300/Makefile9
-rw-r--r--drivers/media/video/tlg2300/pd-alsa.c332
-rw-r--r--drivers/media/video/tlg2300/pd-common.h282
-rw-r--r--drivers/media/video/tlg2300/pd-dvb.c593
-rw-r--r--drivers/media/video/tlg2300/pd-main.c539
-rw-r--r--drivers/media/video/tlg2300/pd-radio.c420
-rw-r--r--drivers/media/video/tlg2300/pd-video.c1667
-rw-r--r--drivers/media/video/tlg2300/vendorcmds.h243
9 files changed, 4101 insertions, 0 deletions
diff --git a/drivers/media/video/tlg2300/Kconfig b/drivers/media/video/tlg2300/Kconfig
new file mode 100644
index 000000000000..2c29ec659b4e
--- /dev/null
+++ b/drivers/media/video/tlg2300/Kconfig
@@ -0,0 +1,16 @@
1config VIDEO_TLG2300
2 tristate "Telegent TLG2300 USB video capture support"
3 depends on VIDEO_DEV && I2C && INPUT && SND && DVB_CORE
4 select VIDEO_TUNER
5 select VIDEO_TVEEPROM
6 select VIDEO_IR
7 select VIDEOBUF_VMALLOC
8 select SND_PCM
9 select VIDEOBUF_DVB
10
11 ---help---
12 This is a video4linux driver for Telegent tlg2300 based TV cards.
13 The driver supports V4L2, DVB-T and radio.
14
15 To compile this driver as a module, choose M here: the
16 module will be called poseidon
diff --git a/drivers/media/video/tlg2300/Makefile b/drivers/media/video/tlg2300/Makefile
new file mode 100644
index 000000000000..81bb7fdd1e3d
--- /dev/null
+++ b/drivers/media/video/tlg2300/Makefile
@@ -0,0 +1,9 @@
1poseidon-objs := pd-video.o pd-alsa.o pd-dvb.o pd-radio.o pd-main.o
2
3obj-$(CONFIG_VIDEO_TLG2300) += poseidon.o
4
5EXTRA_CFLAGS += -Idrivers/media/video
6EXTRA_CFLAGS += -Idrivers/media/common/tuners
7EXTRA_CFLAGS += -Idrivers/media/dvb/dvb-core
8EXTRA_CFLAGS += -Idrivers/media/dvb/frontends
9
diff --git a/drivers/media/video/tlg2300/pd-alsa.c b/drivers/media/video/tlg2300/pd-alsa.c
new file mode 100644
index 000000000000..6f42621ad478
--- /dev/null
+++ b/drivers/media/video/tlg2300/pd-alsa.c
@@ -0,0 +1,332 @@
1#include <linux/kernel.h>
2#include <linux/usb.h>
3#include <linux/init.h>
4#include <linux/sound.h>
5#include <linux/spinlock.h>
6#include <linux/soundcard.h>
7#include <linux/slab.h>
8#include <linux/vmalloc.h>
9#include <linux/proc_fs.h>
10#include <linux/module.h>
11#include <sound/core.h>
12#include <sound/pcm.h>
13#include <sound/pcm_params.h>
14#include <sound/info.h>
15#include <sound/initval.h>
16#include <sound/control.h>
17#include <media/v4l2-common.h>
18#include "pd-common.h"
19#include "vendorcmds.h"
20
21static void complete_handler_audio(struct urb *urb);
22#define AUDIO_EP (0x83)
23#define AUDIO_BUF_SIZE (512)
24#define PERIOD_SIZE (1024 * 8)
25#define PERIOD_MIN (4)
26#define PERIOD_MAX PERIOD_MIN
27
28static struct snd_pcm_hardware snd_pd_hw_capture = {
29 .info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
30 SNDRV_PCM_INFO_MMAP |
31 SNDRV_PCM_INFO_INTERLEAVED |
32 SNDRV_PCM_INFO_MMAP_VALID,
33
34 .formats = SNDRV_PCM_FMTBIT_S16_LE,
35 .rates = SNDRV_PCM_RATE_48000,
36
37 .rate_min = 48000,
38 .rate_max = 48000,
39 .channels_min = 2,
40 .channels_max = 2,
41 .buffer_bytes_max = PERIOD_SIZE * PERIOD_MIN,
42 .period_bytes_min = PERIOD_SIZE,
43 .period_bytes_max = PERIOD_SIZE,
44 .periods_min = PERIOD_MIN,
45 .periods_max = PERIOD_MAX,
46 /*
47 .buffer_bytes_max = 62720 * 8,
48 .period_bytes_min = 64,
49 .period_bytes_max = 12544,
50 .periods_min = 2,
51 .periods_max = 98
52 */
53};
54
55static int snd_pd_capture_open(struct snd_pcm_substream *substream)
56{
57 struct poseidon *p = snd_pcm_substream_chip(substream);
58 struct poseidon_audio *pa = &p->audio;
59 struct snd_pcm_runtime *runtime = substream->runtime;
60
61 if (!p)
62 return -ENODEV;
63 pa->users++;
64 pa->card_close = 0;
65 pa->capture_pcm_substream = substream;
66 runtime->private_data = p;
67
68 runtime->hw = snd_pd_hw_capture;
69 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
70 usb_autopm_get_interface(p->interface);
71 kref_get(&p->kref);
72 return 0;
73}
74
75static int snd_pd_pcm_close(struct snd_pcm_substream *substream)
76{
77 struct poseidon *p = snd_pcm_substream_chip(substream);
78 struct poseidon_audio *pa = &p->audio;
79
80 pa->users--;
81 pa->card_close = 1;
82 usb_autopm_put_interface(p->interface);
83 kref_put(&p->kref, poseidon_delete);
84 return 0;
85}
86
87static int snd_pd_hw_capture_params(struct snd_pcm_substream *substream,
88 struct snd_pcm_hw_params *hw_params)
89{
90 struct snd_pcm_runtime *runtime = substream->runtime;
91 unsigned int size;
92
93 size = params_buffer_bytes(hw_params);
94 if (runtime->dma_area) {
95 if (runtime->dma_bytes > size)
96 return 0;
97 vfree(runtime->dma_area);
98 }
99 runtime->dma_area = vmalloc(size);
100 if (!runtime->dma_area)
101 return -ENOMEM;
102 else
103 runtime->dma_bytes = size;
104 return 0;
105}
106
107static int audio_buf_free(struct poseidon *p)
108{
109 struct poseidon_audio *pa = &p->audio;
110 int i;
111
112 for (i = 0; i < AUDIO_BUFS; i++)
113 if (pa->urb_array[i])
114 usb_kill_urb(pa->urb_array[i]);
115 free_all_urb_generic(pa->urb_array, AUDIO_BUFS);
116 logpm();
117 return 0;
118}
119
120static int snd_pd_hw_capture_free(struct snd_pcm_substream *substream)
121{
122 struct poseidon *p = snd_pcm_substream_chip(substream);
123
124 logpm();
125 audio_buf_free(p);
126 return 0;
127}
128
129static int snd_pd_prepare(struct snd_pcm_substream *substream)
130{
131 return 0;
132}
133
134#define AUDIO_TRAILER_SIZE (16)
135static inline void handle_audio_data(struct urb *urb, int *period_elapsed)
136{
137 struct poseidon_audio *pa = urb->context;
138 struct snd_pcm_runtime *runtime = pa->capture_pcm_substream->runtime;
139
140 int stride = runtime->frame_bits >> 3;
141 int len = urb->actual_length / stride;
142 unsigned char *cp = urb->transfer_buffer;
143 unsigned int oldptr = pa->rcv_position;
144
145 if (urb->actual_length == AUDIO_BUF_SIZE - 4)
146 len -= (AUDIO_TRAILER_SIZE / stride);
147
148 /* do the copy */
149 if (oldptr + len >= runtime->buffer_size) {
150 unsigned int cnt = runtime->buffer_size - oldptr;
151
152 memcpy(runtime->dma_area + oldptr * stride, cp, cnt * stride);
153 memcpy(runtime->dma_area, (cp + cnt * stride),
154 (len * stride - cnt * stride));
155 } else
156 memcpy(runtime->dma_area + oldptr * stride, cp, len * stride);
157
158 /* update the statas */
159 snd_pcm_stream_lock(pa->capture_pcm_substream);
160 pa->rcv_position += len;
161 if (pa->rcv_position >= runtime->buffer_size)
162 pa->rcv_position -= runtime->buffer_size;
163
164 pa->copied_position += (len);
165 if (pa->copied_position >= runtime->period_size) {
166 pa->copied_position -= runtime->period_size;
167 *period_elapsed = 1;
168 }
169 snd_pcm_stream_unlock(pa->capture_pcm_substream);
170}
171
172static void complete_handler_audio(struct urb *urb)
173{
174 struct poseidon_audio *pa = urb->context;
175 struct snd_pcm_substream *substream = pa->capture_pcm_substream;
176 int period_elapsed = 0;
177 int ret;
178
179 if (1 == pa->card_close || pa->capture_stream != STREAM_ON)
180 return;
181
182 if (urb->status != 0) {
183 /*if (urb->status == -ESHUTDOWN)*/
184 return;
185 }
186
187 if (substream) {
188 if (urb->actual_length) {
189 handle_audio_data(urb, &period_elapsed);
190 if (period_elapsed)
191 snd_pcm_period_elapsed(substream);
192 }
193 }
194
195 ret = usb_submit_urb(urb, GFP_ATOMIC);
196 if (ret < 0)
197 log("audio urb failed (errcod = %i)", ret);
198 return;
199}
200
201static int fire_audio_urb(struct poseidon *p)
202{
203 int i, ret = 0;
204 struct poseidon_audio *pa = &p->audio;
205
206 alloc_bulk_urbs_generic(pa->urb_array, AUDIO_BUFS,
207 p->udev, AUDIO_EP,
208 AUDIO_BUF_SIZE, GFP_ATOMIC,
209 complete_handler_audio, pa);
210
211 for (i = 0; i < AUDIO_BUFS; i++) {
212 ret = usb_submit_urb(pa->urb_array[i], GFP_KERNEL);
213 if (ret)
214 log("urb err : %d", ret);
215 }
216 log();
217 return ret;
218}
219
220static int snd_pd_capture_trigger(struct snd_pcm_substream *substream, int cmd)
221{
222 struct poseidon *p = snd_pcm_substream_chip(substream);
223 struct poseidon_audio *pa = &p->audio;
224
225 if (debug_mode)
226 log("cmd %d, audio stat : %d\n", cmd, pa->capture_stream);
227
228 switch (cmd) {
229 case SNDRV_PCM_TRIGGER_RESUME:
230 case SNDRV_PCM_TRIGGER_START:
231 if (pa->capture_stream == STREAM_ON)
232 return 0;
233
234 pa->rcv_position = pa->copied_position = 0;
235 pa->capture_stream = STREAM_ON;
236
237 if (in_hibernation(p))
238 return 0;
239 fire_audio_urb(p);
240 return 0;
241
242 case SNDRV_PCM_TRIGGER_SUSPEND:
243 pa->capture_stream = STREAM_SUSPEND;
244 return 0;
245 case SNDRV_PCM_TRIGGER_STOP:
246 pa->capture_stream = STREAM_OFF;
247 return 0;
248 default:
249 return -EINVAL;
250 }
251}
252
253static snd_pcm_uframes_t
254snd_pd_capture_pointer(struct snd_pcm_substream *substream)
255{
256 struct poseidon *p = snd_pcm_substream_chip(substream);
257 struct poseidon_audio *pa = &p->audio;
258 return pa->rcv_position;
259}
260
261static struct page *snd_pcm_pd_get_page(struct snd_pcm_substream *subs,
262 unsigned long offset)
263{
264 void *pageptr = subs->runtime->dma_area + offset;
265 return vmalloc_to_page(pageptr);
266}
267
268static struct snd_pcm_ops pcm_capture_ops = {
269 .open = snd_pd_capture_open,
270 .close = snd_pd_pcm_close,
271 .ioctl = snd_pcm_lib_ioctl,
272 .hw_params = snd_pd_hw_capture_params,
273 .hw_free = snd_pd_hw_capture_free,
274 .prepare = snd_pd_prepare,
275 .trigger = snd_pd_capture_trigger,
276 .pointer = snd_pd_capture_pointer,
277 .page = snd_pcm_pd_get_page,
278};
279
280#ifdef CONFIG_PM
281int pm_alsa_suspend(struct poseidon *p)
282{
283 logpm(p);
284 audio_buf_free(p);
285 return 0;
286}
287
288int pm_alsa_resume(struct poseidon *p)
289{
290 logpm(p);
291 fire_audio_urb(p);
292 return 0;
293}
294#endif
295
296int poseidon_audio_init(struct poseidon *p)
297{
298 struct poseidon_audio *pa = &p->audio;
299 struct snd_card *card;
300 struct snd_pcm *pcm;
301 int ret;
302
303 ret = snd_card_create(-1, "Telegent", THIS_MODULE, 0, &card);
304 if (ret != 0)
305 return ret;
306
307 ret = snd_pcm_new(card, "poseidon audio", 0, 0, 1, &pcm);
308 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pcm_capture_ops);
309 pcm->info_flags = 0;
310 pcm->private_data = p;
311 strcpy(pcm->name, "poseidon audio capture");
312
313 strcpy(card->driver, "ALSA driver");
314 strcpy(card->shortname, "poseidon Audio");
315 strcpy(card->longname, "poseidon ALSA Audio");
316
317 if (snd_card_register(card)) {
318 snd_card_free(card);
319 return -ENOMEM;
320 }
321 pa->card = card;
322 return 0;
323}
324
325int poseidon_audio_free(struct poseidon *p)
326{
327 struct poseidon_audio *pa = &p->audio;
328
329 if (pa->card)
330 snd_card_free(pa->card);
331 return 0;
332}
diff --git a/drivers/media/video/tlg2300/pd-common.h b/drivers/media/video/tlg2300/pd-common.h
new file mode 100644
index 000000000000..46066bdc73f9
--- /dev/null
+++ b/drivers/media/video/tlg2300/pd-common.h
@@ -0,0 +1,282 @@
1#ifndef PD_COMMON_H
2#define PD_COMMON_H
3
4#include <linux/version.h>
5#include <linux/fs.h>
6#include <linux/wait.h>
7#include <linux/list.h>
8#include <linux/videodev2.h>
9#include <linux/semaphore.h>
10#include <linux/usb.h>
11#include <linux/poll.h>
12#include <media/videobuf-vmalloc.h>
13#include <media/v4l2-device.h>
14
15#include "dvb_frontend.h"
16#include "dvbdev.h"
17#include "dvb_demux.h"
18#include "dmxdev.h"
19
20#define SBUF_NUM 8
21#define MAX_BUFFER_NUM 6
22#define PK_PER_URB 32
23#define ISO_PKT_SIZE 3072
24
25#define POSEIDON_STATE_NONE (0x0000)
26#define POSEIDON_STATE_ANALOG (0x0001)
27#define POSEIDON_STATE_FM (0x0002)
28#define POSEIDON_STATE_DVBT (0x0004)
29#define POSEIDON_STATE_VBI (0x0008)
30#define POSEIDON_STATE_DISCONNECT (0x0080)
31
32#define PM_SUSPEND_DELAY 3
33
34#define V4L_PAL_VBI_LINES 18
35#define V4L_NTSC_VBI_LINES 12
36#define V4L_PAL_VBI_FRAMESIZE (V4L_PAL_VBI_LINES * 1440 * 2)
37#define V4L_NTSC_VBI_FRAMESIZE (V4L_NTSC_VBI_LINES * 1440 * 2)
38
39#define TUNER_FREQ_MIN (45000000)
40#define TUNER_FREQ_MAX (862000000)
41
42struct vbi_data {
43 struct video_device *v_dev;
44 struct video_data *video;
45 struct front_face *front;
46
47 unsigned int copied;
48 unsigned int vbi_size; /* the whole size of two fields */
49 int users;
50};
51
52/*
53 * This is the running context of the video, it is useful for
54 * resume()
55 */
56struct running_context {
57 u32 freq; /* VIDIOC_S_FREQUENCY */
58 int audio_idx; /* VIDIOC_S_TUNER */
59 v4l2_std_id tvnormid; /* VIDIOC_S_STD */
60 int sig_index; /* VIDIOC_S_INPUT */
61 struct v4l2_pix_format pix; /* VIDIOC_S_FMT */
62};
63
64struct video_data {
65 /* v4l2 video device */
66 struct video_device *v_dev;
67
68 /* the working context */
69 struct running_context context;
70
71 /* for data copy */
72 int field_count;
73
74 char *dst;
75 int lines_copied;
76 int prev_left;
77
78 int lines_per_field;
79 int lines_size;
80
81 /* for communication */
82 u8 endpoint_addr;
83 struct urb *urb_array[SBUF_NUM];
84 struct vbi_data *vbi;
85 struct poseidon *pd;
86 struct front_face *front;
87
88 int is_streaming;
89 int users;
90
91 /* for bubble handler */
92 struct work_struct bubble_work;
93};
94
95enum pcm_stream_state {
96 STREAM_OFF,
97 STREAM_ON,
98 STREAM_SUSPEND,
99};
100
101#define AUDIO_BUFS (3)
102#define CAPTURE_STREAM_EN 1
103struct poseidon_audio {
104 struct urb *urb_array[AUDIO_BUFS];
105 unsigned int copied_position;
106 struct snd_pcm_substream *capture_pcm_substream;
107
108 unsigned int rcv_position;
109 struct snd_card *card;
110 int card_close;
111
112 int users;
113 int pm_state;
114 enum pcm_stream_state capture_stream;
115};
116
117struct radio_data {
118 __u32 fm_freq;
119 int users;
120 unsigned int is_radio_streaming;
121 int pre_emphasis;
122 struct video_device *fm_dev;
123};
124
125#define DVB_SBUF_NUM 4
126#define DVB_URB_BUF_SIZE 0x2000
127struct pd_dvb_adapter {
128 struct dvb_adapter dvb_adap;
129 struct dvb_frontend dvb_fe;
130 struct dmxdev dmxdev;
131 struct dvb_demux demux;
132
133 atomic_t users;
134 atomic_t active_feed;
135
136 /* data transfer */
137 s32 is_streaming;
138 struct urb *urb_array[DVB_SBUF_NUM];
139 struct poseidon *pd_device;
140 u8 ep_addr;
141 u8 reserved[3];
142
143 /* data for power resume*/
144 struct dvb_frontend_parameters fe_param;
145
146 /* for channel scanning */
147 int prev_freq;
148 int bandwidth;
149 unsigned long last_jiffies;
150};
151
152struct front_face {
153 /* use this field to distinguish VIDEO and VBI */
154 enum v4l2_buf_type type;
155
156 /* for host */
157 struct videobuf_queue q;
158
159 /* the bridge for host and device */
160 struct videobuf_buffer *curr_frame;
161
162 /* for device */
163 spinlock_t queue_lock;
164 struct list_head active;
165 struct poseidon *pd;
166};
167
168struct poseidon {
169 struct list_head device_list;
170
171 struct mutex lock;
172 struct kref kref;
173
174 /* for V4L2 */
175 struct v4l2_device v4l2_dev;
176
177 /* hardware info */
178 struct usb_device *udev;
179 struct usb_interface *interface;
180 int cur_transfer_mode;
181
182 struct video_data video_data; /* video */
183 struct vbi_data vbi_data; /* vbi */
184 struct poseidon_audio audio; /* audio (alsa) */
185 struct radio_data radio_data; /* FM */
186 struct pd_dvb_adapter dvb_data; /* DVB */
187
188 u32 state;
189 struct file *file_for_stream; /* the active stream*/
190
191#ifdef CONFIG_PM
192 int (*pm_suspend)(struct poseidon *);
193 int (*pm_resume)(struct poseidon *);
194 pm_message_t msg;
195
196 struct work_struct pm_work;
197 u8 portnum;
198#endif
199};
200
201struct poseidon_format {
202 char *name;
203 int fourcc; /* video4linux 2 */
204 int depth; /* bit/pixel */
205 int flags;
206};
207
208struct poseidon_tvnorm {
209 v4l2_std_id v4l2_id;
210 char name[12];
211 u32 tlg_tvnorm;
212};
213
214/* video */
215int pd_video_init(struct poseidon *);
216void pd_video_exit(struct poseidon *);
217int stop_all_video_stream(struct poseidon *);
218
219/* alsa audio */
220int poseidon_audio_init(struct poseidon *);
221int poseidon_audio_free(struct poseidon *);
222#ifdef CONFIG_PM
223int pm_alsa_suspend(struct poseidon *);
224int pm_alsa_resume(struct poseidon *);
225#endif
226
227/* dvb */
228int pd_dvb_usb_device_init(struct poseidon *);
229void pd_dvb_usb_device_exit(struct poseidon *);
230void pd_dvb_usb_device_cleanup(struct poseidon *);
231int pd_dvb_get_adapter_num(struct pd_dvb_adapter *);
232void dvb_stop_streaming(struct pd_dvb_adapter *);
233
234/* FM */
235int poseidon_fm_init(struct poseidon *);
236int poseidon_fm_exit(struct poseidon *);
237struct video_device *vdev_init(struct poseidon *, struct video_device *);
238
239/* vendor command ops */
240int send_set_req(struct poseidon*, u8, s32, s32*);
241int send_get_req(struct poseidon*, u8, s32, void*, s32*, s32);
242s32 set_tuner_mode(struct poseidon*, unsigned char);
243
244/* bulk urb alloc/free */
245int alloc_bulk_urbs_generic(struct urb **urb_array, int num,
246 struct usb_device *udev, u8 ep_addr,
247 int buf_size, gfp_t gfp_flags,
248 usb_complete_t complete_fn, void *context);
249void free_all_urb_generic(struct urb **urb_array, int num);
250
251/* misc */
252void poseidon_delete(struct kref *kref);
253void destroy_video_device(struct video_device **v_dev);
254extern int debug_mode;
255void set_debug_mode(struct video_device *vfd, int debug_mode);
256
257#ifdef CONFIG_PM
258#define in_hibernation(pd) (pd->msg.event == PM_EVENT_FREEZE)
259#else
260#define in_hibernation(pd) (0)
261#endif
262#define get_pm_count(p) (atomic_read(&(p)->interface->pm_usage_cnt))
263
264#define log(a, ...) printk(KERN_DEBUG "\t[ %s : %.3d ] "a"\n", \
265 __func__, __LINE__, ## __VA_ARGS__)
266
267/* for power management */
268#define logpm(pd) do {\
269 if (debug_mode & 0x10)\
270 log();\
271 } while (0)
272
273#define logs(f) do { \
274 if ((debug_mode & 0x4) && \
275 (f)->type == V4L2_BUF_TYPE_VBI_CAPTURE) \
276 log("type : VBI");\
277 \
278 if ((debug_mode & 0x8) && \
279 (f)->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) \
280 log("type : VIDEO");\
281 } while (0)
282#endif
diff --git a/drivers/media/video/tlg2300/pd-dvb.c b/drivers/media/video/tlg2300/pd-dvb.c
new file mode 100644
index 000000000000..4133aee568bf
--- /dev/null
+++ b/drivers/media/video/tlg2300/pd-dvb.c
@@ -0,0 +1,593 @@
1#include "pd-common.h"
2#include <linux/kernel.h>
3#include <linux/usb.h>
4#include <linux/dvb/dmx.h>
5#include <linux/delay.h>
6
7#include "vendorcmds.h"
8#include <linux/sched.h>
9#include <asm/atomic.h>
10
11static void dvb_urb_cleanup(struct pd_dvb_adapter *pd_dvb);
12
13static int dvb_bandwidth[][2] = {
14 { TLG_BW_8, BANDWIDTH_8_MHZ },
15 { TLG_BW_7, BANDWIDTH_7_MHZ },
16 { TLG_BW_6, BANDWIDTH_6_MHZ }
17};
18static int dvb_bandwidth_length = ARRAY_SIZE(dvb_bandwidth);
19
20static s32 dvb_start_streaming(struct pd_dvb_adapter *pd_dvb);
21static int poseidon_check_mode_dvbt(struct poseidon *pd)
22{
23 s32 ret = 0, cmd_status = 0;
24
25 set_current_state(TASK_INTERRUPTIBLE);
26 schedule_timeout(HZ/4);
27
28 ret = usb_set_interface(pd->udev, 0, BULK_ALTERNATE_IFACE);
29 if (ret != 0)
30 return ret;
31
32 ret = set_tuner_mode(pd, TLG_MODE_CAPS_DVB_T);
33 if (ret)
34 return ret;
35
36 /* signal source */
37 ret = send_set_req(pd, SGNL_SRC_SEL, TLG_SIG_SRC_ANTENNA, &cmd_status);
38 if (ret|cmd_status)
39 return ret;
40
41 return 0;
42}
43
44/* acquire :
45 * 1 == open
46 * 0 == release
47 */
48static int poseidon_ts_bus_ctrl(struct dvb_frontend *fe, int acquire)
49{
50 struct poseidon *pd = fe->demodulator_priv;
51 struct pd_dvb_adapter *pd_dvb;
52 int ret = 0;
53
54 if (!pd)
55 return -ENODEV;
56
57 pd_dvb = container_of(fe, struct pd_dvb_adapter, dvb_fe);
58 if (acquire) {
59 mutex_lock(&pd->lock);
60 if (pd->state & POSEIDON_STATE_DISCONNECT) {
61 ret = -ENODEV;
62 goto open_out;
63 }
64
65 if (pd->state && !(pd->state & POSEIDON_STATE_DVBT)) {
66 ret = -EBUSY;
67 goto open_out;
68 }
69
70 usb_autopm_get_interface(pd->interface);
71 if (0 == pd->state) {
72 ret = poseidon_check_mode_dvbt(pd);
73 if (ret < 0) {
74 usb_autopm_put_interface(pd->interface);
75 goto open_out;
76 }
77 pd->state |= POSEIDON_STATE_DVBT;
78 pd_dvb->bandwidth = 0;
79 pd_dvb->prev_freq = 0;
80 }
81 atomic_inc(&pd_dvb->users);
82 kref_get(&pd->kref);
83open_out:
84 mutex_unlock(&pd->lock);
85 } else {
86 dvb_stop_streaming(pd_dvb);
87
88 if (atomic_dec_and_test(&pd_dvb->users)) {
89 mutex_lock(&pd->lock);
90 pd->state &= ~POSEIDON_STATE_DVBT;
91 mutex_unlock(&pd->lock);
92 }
93 kref_put(&pd->kref, poseidon_delete);
94 usb_autopm_put_interface(pd->interface);
95 }
96 return ret;
97}
98
99static void poseidon_fe_release(struct dvb_frontend *fe)
100{
101 struct poseidon *pd = fe->demodulator_priv;
102
103#ifdef CONFIG_PM
104 pd->pm_suspend = NULL;
105 pd->pm_resume = NULL;
106#endif
107}
108
109static s32 poseidon_fe_sleep(struct dvb_frontend *fe)
110{
111 return 0;
112}
113
114/*
115 * return true if we can satisfy the conditions, else return false.
116 */
117static bool check_scan_ok(__u32 freq, int bandwidth,
118 struct pd_dvb_adapter *adapter)
119{
120 if (bandwidth < 0)
121 return false;
122
123 if (adapter->prev_freq == freq
124 && adapter->bandwidth == bandwidth) {
125 long nl = jiffies - adapter->last_jiffies;
126 unsigned int msec ;
127
128 msec = jiffies_to_msecs(abs(nl));
129 return msec > 15000 ? true : false;
130 }
131 return true;
132}
133
134/*
135 * Check if the firmware delays too long for an invalid frequency.
136 */
137static int fw_delay_overflow(struct pd_dvb_adapter *adapter)
138{
139 long nl = jiffies - adapter->last_jiffies;
140 unsigned int msec ;
141
142 msec = jiffies_to_msecs(abs(nl));
143 return msec > 800 ? true : false;
144}
145
146static int poseidon_set_fe(struct dvb_frontend *fe,
147 struct dvb_frontend_parameters *fep)
148{
149 s32 ret = 0, cmd_status = 0;
150 s32 i, bandwidth = -1;
151 struct poseidon *pd = fe->demodulator_priv;
152 struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;
153
154 if (in_hibernation(pd))
155 return -EBUSY;
156
157 mutex_lock(&pd->lock);
158 for (i = 0; i < dvb_bandwidth_length; i++)
159 if (fep->u.ofdm.bandwidth == dvb_bandwidth[i][1])
160 bandwidth = dvb_bandwidth[i][0];
161
162 if (check_scan_ok(fep->frequency, bandwidth, pd_dvb)) {
163 ret = send_set_req(pd, TUNE_FREQ_SELECT,
164 fep->frequency / 1000, &cmd_status);
165 if (ret | cmd_status) {
166 log("error line");
167 goto front_out;
168 }
169
170 ret = send_set_req(pd, DVBT_BANDW_SEL,
171 bandwidth, &cmd_status);
172 if (ret | cmd_status) {
173 log("error line");
174 goto front_out;
175 }
176
177 ret = send_set_req(pd, TAKE_REQUEST, 0, &cmd_status);
178 if (ret | cmd_status) {
179 log("error line");
180 goto front_out;
181 }
182
183 /* save the context for future */
184 memcpy(&pd_dvb->fe_param, fep, sizeof(*fep));
185 pd_dvb->bandwidth = bandwidth;
186 pd_dvb->prev_freq = fep->frequency;
187 pd_dvb->last_jiffies = jiffies;
188 }
189front_out:
190 mutex_unlock(&pd->lock);
191 return ret;
192}
193
194#ifdef CONFIG_PM
195static int pm_dvb_suspend(struct poseidon *pd)
196{
197 struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;
198 dvb_stop_streaming(pd_dvb);
199 dvb_urb_cleanup(pd_dvb);
200 msleep(500);
201 return 0;
202}
203
204static int pm_dvb_resume(struct poseidon *pd)
205{
206 struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;
207
208 poseidon_check_mode_dvbt(pd);
209 msleep(300);
210 poseidon_set_fe(&pd_dvb->dvb_fe, &pd_dvb->fe_param);
211
212 dvb_start_streaming(pd_dvb);
213 return 0;
214}
215#endif
216
217static s32 poseidon_fe_init(struct dvb_frontend *fe)
218{
219 struct poseidon *pd = fe->demodulator_priv;
220 struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;
221
222#ifdef CONFIG_PM
223 pd->pm_suspend = pm_dvb_suspend;
224 pd->pm_resume = pm_dvb_resume;
225#endif
226 memset(&pd_dvb->fe_param, 0,
227 sizeof(struct dvb_frontend_parameters));
228 return 0;
229}
230
231static int poseidon_get_fe(struct dvb_frontend *fe,
232 struct dvb_frontend_parameters *fep)
233{
234 struct poseidon *pd = fe->demodulator_priv;
235 struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;
236
237 memcpy(fep, &pd_dvb->fe_param, sizeof(*fep));
238 return 0;
239}
240
241static int poseidon_fe_get_tune_settings(struct dvb_frontend *fe,
242 struct dvb_frontend_tune_settings *tune)
243{
244 tune->min_delay_ms = 1000;
245 return 0;
246}
247
248static int poseidon_read_status(struct dvb_frontend *fe, fe_status_t *stat)
249{
250 struct poseidon *pd = fe->demodulator_priv;
251 s32 ret = -1, cmd_status;
252 struct tuner_dtv_sig_stat_s status = {};
253
254 if (in_hibernation(pd))
255 return -EBUSY;
256 mutex_lock(&pd->lock);
257
258 ret = send_get_req(pd, TUNER_STATUS, TLG_MODE_DVB_T,
259 &status, &cmd_status, sizeof(status));
260 if (ret | cmd_status) {
261 log("get tuner status error");
262 goto out;
263 }
264
265 if (debug_mode)
266 log("P : %d, L %d, LB :%d", status.sig_present,
267 status.sig_locked, status.sig_lock_busy);
268
269 if (status.sig_lock_busy) {
270 goto out;
271 } else if (status.sig_present || status.sig_locked) {
272 *stat |= FE_HAS_LOCK | FE_HAS_SIGNAL | FE_HAS_CARRIER
273 | FE_HAS_SYNC | FE_HAS_VITERBI;
274 } else {
275 if (fw_delay_overflow(&pd->dvb_data))
276 *stat |= FE_TIMEDOUT;
277 }
278out:
279 mutex_unlock(&pd->lock);
280 return ret;
281}
282
283static int poseidon_read_ber(struct dvb_frontend *fe, u32 *ber)
284{
285 struct poseidon *pd = fe->demodulator_priv;
286 struct tuner_ber_rate_s tlg_ber = {};
287 s32 ret = -1, cmd_status;
288
289 mutex_lock(&pd->lock);
290 ret = send_get_req(pd, TUNER_BER_RATE, 0,
291 &tlg_ber, &cmd_status, sizeof(tlg_ber));
292 if (ret | cmd_status)
293 goto out;
294 *ber = tlg_ber.ber_rate;
295out:
296 mutex_unlock(&pd->lock);
297 return ret;
298}
299
300static s32 poseidon_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
301{
302 struct poseidon *pd = fe->demodulator_priv;
303 struct tuner_dtv_sig_stat_s status = {};
304 s32 ret = 0, cmd_status;
305
306 mutex_lock(&pd->lock);
307 ret = send_get_req(pd, TUNER_STATUS, TLG_MODE_DVB_T,
308 &status, &cmd_status, sizeof(status));
309 if (ret | cmd_status)
310 goto out;
311 if ((status.sig_present || status.sig_locked) && !status.sig_strength)
312 *strength = 0xFFFF;
313 else
314 *strength = status.sig_strength;
315out:
316 mutex_unlock(&pd->lock);
317 return ret;
318}
319
320static int poseidon_read_snr(struct dvb_frontend *fe, u16 *snr)
321{
322 return 0;
323}
324
325static int poseidon_read_unc_blocks(struct dvb_frontend *fe, u32 *unc)
326{
327 *unc = 0;
328 return 0;
329}
330
331static struct dvb_frontend_ops poseidon_frontend_ops = {
332 .info = {
333 .name = "Poseidon DVB-T",
334 .type = FE_OFDM,
335 .frequency_min = 174000000,
336 .frequency_max = 862000000,
337 .frequency_stepsize = 62500,/* FIXME */
338 .caps = FE_CAN_INVERSION_AUTO |
339 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
340 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
341 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
342 FE_CAN_QAM_AUTO | FE_CAN_TRANSMISSION_MODE_AUTO |
343 FE_CAN_GUARD_INTERVAL_AUTO |
344 FE_CAN_RECOVER |
345 FE_CAN_HIERARCHY_AUTO,
346 },
347
348 .release = poseidon_fe_release,
349
350 .init = poseidon_fe_init,
351 .sleep = poseidon_fe_sleep,
352
353 .set_frontend = poseidon_set_fe,
354 .get_frontend = poseidon_get_fe,
355 .get_tune_settings = poseidon_fe_get_tune_settings,
356
357 .read_status = poseidon_read_status,
358 .read_ber = poseidon_read_ber,
359 .read_signal_strength = poseidon_read_signal_strength,
360 .read_snr = poseidon_read_snr,
361 .read_ucblocks = poseidon_read_unc_blocks,
362
363 .ts_bus_ctrl = poseidon_ts_bus_ctrl,
364};
365
366static void dvb_urb_irq(struct urb *urb)
367{
368 struct pd_dvb_adapter *pd_dvb = urb->context;
369 int len = urb->transfer_buffer_length;
370 struct dvb_demux *demux = &pd_dvb->demux;
371 s32 ret;
372
373 if (!pd_dvb->is_streaming || urb->status) {
374 if (urb->status == -EPROTO)
375 goto resend;
376 return;
377 }
378
379 if (urb->actual_length == len)
380 dvb_dmx_swfilter(demux, urb->transfer_buffer, len);
381 else if (urb->actual_length == len - 4) {
382 int offset;
383 u8 *buf = urb->transfer_buffer;
384
385 /*
386 * The packet size is 512,
387 * last packet contains 456 bytes tsp data
388 */
389 for (offset = 456; offset < len; offset += 512) {
390 if (!strncmp(buf + offset, "DVHS", 4)) {
391 dvb_dmx_swfilter(demux, buf, offset);
392 if (len > offset + 52 + 4) {
393 /*16 bytes trailer + 36 bytes padding */
394 buf += offset + 52;
395 len -= offset + 52 + 4;
396 dvb_dmx_swfilter(demux, buf, len);
397 }
398 break;
399 }
400 }
401 }
402
403resend:
404 ret = usb_submit_urb(urb, GFP_ATOMIC);
405 if (ret)
406 log(" usb_submit_urb failed: error %d", ret);
407}
408
409static int dvb_urb_init(struct pd_dvb_adapter *pd_dvb)
410{
411 if (pd_dvb->urb_array[0])
412 return 0;
413
414 alloc_bulk_urbs_generic(pd_dvb->urb_array, DVB_SBUF_NUM,
415 pd_dvb->pd_device->udev, pd_dvb->ep_addr,
416 DVB_URB_BUF_SIZE, GFP_KERNEL,
417 dvb_urb_irq, pd_dvb);
418 return 0;
419}
420
421static void dvb_urb_cleanup(struct pd_dvb_adapter *pd_dvb)
422{
423 free_all_urb_generic(pd_dvb->urb_array, DVB_SBUF_NUM);
424}
425
426static s32 dvb_start_streaming(struct pd_dvb_adapter *pd_dvb)
427{
428 struct poseidon *pd = pd_dvb->pd_device;
429 int ret = 0;
430
431 if (pd->state & POSEIDON_STATE_DISCONNECT)
432 return -ENODEV;
433
434 mutex_lock(&pd->lock);
435 if (!pd_dvb->is_streaming) {
436 s32 i, cmd_status = 0;
437 /*
438 * Once upon a time, there was a difficult bug lying here.
439 * ret = send_set_req(pd, TAKE_REQUEST, 0, &cmd_status);
440 */
441
442 ret = send_set_req(pd, PLAY_SERVICE, 1, &cmd_status);
443 if (ret | cmd_status)
444 goto out;
445
446 ret = dvb_urb_init(pd_dvb);
447 if (ret < 0)
448 goto out;
449
450 pd_dvb->is_streaming = 1;
451 for (i = 0; i < DVB_SBUF_NUM; i++) {
452 ret = usb_submit_urb(pd_dvb->urb_array[i],
453 GFP_KERNEL);
454 if (ret) {
455 log(" submit urb error %d", ret);
456 goto out;
457 }
458 }
459 }
460out:
461 mutex_unlock(&pd->lock);
462 return ret;
463}
464
465void dvb_stop_streaming(struct pd_dvb_adapter *pd_dvb)
466{
467 struct poseidon *pd = pd_dvb->pd_device;
468
469 mutex_lock(&pd->lock);
470 if (pd_dvb->is_streaming) {
471 s32 i, ret, cmd_status = 0;
472
473 pd_dvb->is_streaming = 0;
474
475 for (i = 0; i < DVB_SBUF_NUM; i++)
476 if (pd_dvb->urb_array[i])
477 usb_kill_urb(pd_dvb->urb_array[i]);
478
479 ret = send_set_req(pd, PLAY_SERVICE, TLG_TUNE_PLAY_SVC_STOP,
480 &cmd_status);
481 if (ret | cmd_status)
482 log("error");
483 }
484 mutex_unlock(&pd->lock);
485}
486
487static int pd_start_feed(struct dvb_demux_feed *feed)
488{
489 struct pd_dvb_adapter *pd_dvb = feed->demux->priv;
490 int ret = 0;
491
492 if (!pd_dvb)
493 return -1;
494 if (atomic_inc_return(&pd_dvb->active_feed) == 1)
495 ret = dvb_start_streaming(pd_dvb);
496 return ret;
497}
498
499static int pd_stop_feed(struct dvb_demux_feed *feed)
500{
501 struct pd_dvb_adapter *pd_dvb = feed->demux->priv;
502
503 if (!pd_dvb)
504 return -1;
505 if (atomic_dec_and_test(&pd_dvb->active_feed))
506 dvb_stop_streaming(pd_dvb);
507 return 0;
508}
509
510DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
511int pd_dvb_usb_device_init(struct poseidon *pd)
512{
513 struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;
514 struct dvb_demux *dvbdemux;
515 int ret = 0;
516
517 pd_dvb->ep_addr = 0x82;
518 atomic_set(&pd_dvb->users, 0);
519 atomic_set(&pd_dvb->active_feed, 0);
520 pd_dvb->pd_device = pd;
521
522 ret = dvb_register_adapter(&pd_dvb->dvb_adap,
523 "Poseidon dvbt adapter",
524 THIS_MODULE,
525 NULL /* for hibernation correctly*/,
526 adapter_nr);
527 if (ret < 0)
528 goto error1;
529
530 /* register frontend */
531 pd_dvb->dvb_fe.demodulator_priv = pd;
532 memcpy(&pd_dvb->dvb_fe.ops, &poseidon_frontend_ops,
533 sizeof(struct dvb_frontend_ops));
534 ret = dvb_register_frontend(&pd_dvb->dvb_adap, &pd_dvb->dvb_fe);
535 if (ret < 0)
536 goto error2;
537
538 /* register demux device */
539 dvbdemux = &pd_dvb->demux;
540 dvbdemux->dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
541 dvbdemux->priv = pd_dvb;
542 dvbdemux->feednum = dvbdemux->filternum = 64;
543 dvbdemux->start_feed = pd_start_feed;
544 dvbdemux->stop_feed = pd_stop_feed;
545 dvbdemux->write_to_decoder = NULL;
546
547 ret = dvb_dmx_init(dvbdemux);
548 if (ret < 0)
549 goto error3;
550
551 pd_dvb->dmxdev.filternum = pd_dvb->demux.filternum;
552 pd_dvb->dmxdev.demux = &pd_dvb->demux.dmx;
553 pd_dvb->dmxdev.capabilities = 0;
554
555 ret = dvb_dmxdev_init(&pd_dvb->dmxdev, &pd_dvb->dvb_adap);
556 if (ret < 0)
557 goto error3;
558 return 0;
559
560error3:
561 dvb_unregister_frontend(&pd_dvb->dvb_fe);
562error2:
563 dvb_unregister_adapter(&pd_dvb->dvb_adap);
564error1:
565 return ret;
566}
567
568void pd_dvb_usb_device_exit(struct poseidon *pd)
569{
570 struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;
571
572 while (atomic_read(&pd_dvb->users) != 0
573 || atomic_read(&pd_dvb->active_feed) != 0) {
574 set_current_state(TASK_INTERRUPTIBLE);
575 schedule_timeout(HZ);
576 }
577 dvb_dmxdev_release(&pd_dvb->dmxdev);
578 dvb_unregister_frontend(&pd_dvb->dvb_fe);
579 dvb_unregister_adapter(&pd_dvb->dvb_adap);
580 pd_dvb_usb_device_cleanup(pd);
581}
582
583void pd_dvb_usb_device_cleanup(struct poseidon *pd)
584{
585 struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;
586
587 dvb_urb_cleanup(pd_dvb);
588}
589
590int pd_dvb_get_adapter_num(struct pd_dvb_adapter *pd_dvb)
591{
592 return pd_dvb->dvb_adap.num;
593}
diff --git a/drivers/media/video/tlg2300/pd-main.c b/drivers/media/video/tlg2300/pd-main.c
new file mode 100644
index 000000000000..2cf0ebf9f28b
--- /dev/null
+++ b/drivers/media/video/tlg2300/pd-main.c
@@ -0,0 +1,539 @@
1/*
2 * device driver for Telegent tlg2300 based TV cards
3 *
4 * Author :
5 * Kang Yong <kangyong@telegent.com>
6 * Zhang Xiaobing <xbzhang@telegent.com>
7 * Huang Shijie <zyziii@telegent.com> or <shijie8@gmail.com>
8 *
9 * (c) 2009 Telegent Systems
10 * (c) 2010 Telegent Systems
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 */
26
27#include <linux/version.h>
28#include <linux/kernel.h>
29#include <linux/errno.h>
30#include <linux/init.h>
31#include <linux/slab.h>
32#include <linux/module.h>
33#include <linux/kref.h>
34#include <linux/suspend.h>
35#include <linux/usb/quirks.h>
36#include <linux/ctype.h>
37#include <linux/string.h>
38#include <linux/types.h>
39#include <linux/firmware.h>
40#include <linux/smp_lock.h>
41
42#include "vendorcmds.h"
43#include "pd-common.h"
44
45#define VENDOR_ID 0x1B24
46#define PRODUCT_ID 0x4001
47static struct usb_device_id id_table[] = {
48 { USB_DEVICE_AND_INTERFACE_INFO(VENDOR_ID, PRODUCT_ID, 255, 1, 0) },
49 { USB_DEVICE_AND_INTERFACE_INFO(VENDOR_ID, PRODUCT_ID, 255, 1, 1) },
50 { },
51};
52MODULE_DEVICE_TABLE(usb, id_table);
53
54int debug_mode;
55module_param(debug_mode, int, 0644);
56MODULE_PARM_DESC(debug_mode, "0 = disable, 1 = enable, 2 = verbose");
57
58const char *firmware_name = "tlg2300_firmware.bin";
59struct usb_driver poseidon_driver;
60static LIST_HEAD(pd_device_list);
61
62/*
63 * send set request to USB firmware.
64 */
65s32 send_set_req(struct poseidon *pd, u8 cmdid, s32 param, s32 *cmd_status)
66{
67 s32 ret;
68 s8 data[32] = {};
69 u16 lower_16, upper_16;
70
71 if (pd->state & POSEIDON_STATE_DISCONNECT)
72 return -ENODEV;
73
74 mdelay(30);
75
76 if (param == 0) {
77 upper_16 = lower_16 = 0;
78 } else {
79 /* send 32 bit param as two 16 bit param,little endian */
80 lower_16 = (unsigned short)(param & 0xffff);
81 upper_16 = (unsigned short)((param >> 16) & 0xffff);
82 }
83 ret = usb_control_msg(pd->udev,
84 usb_rcvctrlpipe(pd->udev, 0),
85 REQ_SET_CMD | cmdid,
86 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
87 lower_16,
88 upper_16,
89 &data,
90 sizeof(*cmd_status),
91 USB_CTRL_GET_TIMEOUT);
92
93 if (!ret) {
94 return -ENXIO;
95 } else {
96 /* 1st 4 bytes into cmd_status */
97 memcpy((char *)cmd_status, &(data[0]), sizeof(*cmd_status));
98 }
99 return 0;
100}
101
102/*
103 * send get request to Poseidon firmware.
104 */
105s32 send_get_req(struct poseidon *pd, u8 cmdid, s32 param,
106 void *buf, s32 *cmd_status, s32 datalen)
107{
108 s32 ret;
109 s8 data[128] = {};
110 u16 lower_16, upper_16;
111
112 if (pd->state & POSEIDON_STATE_DISCONNECT)
113 return -ENODEV;
114
115 mdelay(30);
116 if (param == 0) {
117 upper_16 = lower_16 = 0;
118 } else {
119 /*send 32 bit param as two 16 bit param, little endian */
120 lower_16 = (unsigned short)(param & 0xffff);
121 upper_16 = (unsigned short)((param >> 16) & 0xffff);
122 }
123 ret = usb_control_msg(pd->udev,
124 usb_rcvctrlpipe(pd->udev, 0),
125 REQ_GET_CMD | cmdid,
126 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
127 lower_16,
128 upper_16,
129 &data,
130 (datalen + sizeof(*cmd_status)),
131 USB_CTRL_GET_TIMEOUT);
132
133 if (ret < 0) {
134 return -ENXIO;
135 } else {
136 /* 1st 4 bytes into cmd_status, remaining data into cmd_data */
137 memcpy((char *)cmd_status, &data[0], sizeof(*cmd_status));
138 memcpy((char *)buf, &data[sizeof(*cmd_status)], datalen);
139 }
140 return 0;
141}
142
143static int pm_notifier_block(struct notifier_block *nb,
144 unsigned long event, void *dummy)
145{
146 struct poseidon *pd = NULL;
147 struct list_head *node, *next;
148
149 switch (event) {
150 case PM_POST_HIBERNATION:
151 list_for_each_safe(node, next, &pd_device_list) {
152 struct usb_device *udev;
153 struct usb_interface *iface;
154 int rc = 0;
155
156 pd = container_of(node, struct poseidon, device_list);
157 udev = pd->udev;
158 iface = pd->interface;
159
160 /* It will cause the system to reload the firmware */
161 rc = usb_lock_device_for_reset(udev, iface);
162 if (rc >= 0) {
163 usb_reset_device(udev);
164 usb_unlock_device(udev);
165 }
166 }
167 break;
168 default:
169 break;
170 }
171 log("event :%ld\n", event);
172 return 0;
173}
174
175static struct notifier_block pm_notifer = {
176 .notifier_call = pm_notifier_block,
177};
178
179int set_tuner_mode(struct poseidon *pd, unsigned char mode)
180{
181 s32 ret, cmd_status;
182
183 if (pd->state & POSEIDON_STATE_DISCONNECT)
184 return -ENODEV;
185
186 ret = send_set_req(pd, TUNE_MODE_SELECT, mode, &cmd_status);
187 if (ret || cmd_status)
188 return -ENXIO;
189 return 0;
190}
191
192void poseidon_delete(struct kref *kref)
193{
194 struct poseidon *pd = container_of(kref, struct poseidon, kref);
195
196 if (!pd)
197 return;
198 list_del_init(&pd->device_list);
199
200 pd_dvb_usb_device_cleanup(pd);
201 /* clean_audio_data(&pd->audio_data);*/
202
203 if (pd->udev) {
204 usb_put_dev(pd->udev);
205 pd->udev = NULL;
206 }
207 if (pd->interface) {
208 usb_put_intf(pd->interface);
209 pd->interface = NULL;
210 }
211 kfree(pd);
212 log();
213}
214
215static int firmware_download(struct usb_device *udev)
216{
217 int ret = 0, actual_length;
218 const struct firmware *fw = NULL;
219 void *fwbuf = NULL;
220 size_t fwlength = 0, offset;
221 size_t max_packet_size;
222
223 ret = request_firmware(&fw, firmware_name, &udev->dev);
224 if (ret) {
225 log("download err : %d", ret);
226 return ret;
227 }
228
229 fwlength = fw->size;
230
231 fwbuf = kzalloc(fwlength, GFP_KERNEL);
232 if (!fwbuf) {
233 ret = -ENOMEM;
234 goto out;
235 }
236 memcpy(fwbuf, fw->data, fwlength);
237
238 max_packet_size = udev->ep_out[0x1]->desc.wMaxPacketSize;
239 log("\t\t download size : %d", (int)max_packet_size);
240
241 for (offset = 0; offset < fwlength; offset += max_packet_size) {
242 actual_length = 0;
243 ret = usb_bulk_msg(udev,
244 usb_sndbulkpipe(udev, 0x01), /* ep 1 */
245 fwbuf + offset,
246 min(max_packet_size, fwlength - offset),
247 &actual_length,
248 HZ * 10);
249 if (ret)
250 break;
251 }
252 kfree(fwbuf);
253out:
254 release_firmware(fw);
255 return ret;
256}
257
258static inline struct poseidon *get_pd(struct usb_interface *intf)
259{
260 return usb_get_intfdata(intf);
261}
262
263#ifdef CONFIG_PM
264/* one-to-one map : poseidon{} <----> usb_device{}'s port */
265static inline void set_map_flags(struct poseidon *pd, struct usb_device *udev)
266{
267 pd->portnum = udev->portnum;
268}
269
270static inline int get_autopm_ref(struct poseidon *pd)
271{
272 return pd->video_data.users + pd->vbi_data.users + pd->audio.users
273 + atomic_read(&pd->dvb_data.users) + pd->radio_data.users;
274}
275
276/* fixup something for poseidon */
277static inline struct poseidon *fixup(struct poseidon *pd)
278{
279 int count;
280
281 /* old udev and interface have gone, so put back reference . */
282 count = get_autopm_ref(pd);
283 log("count : %d, ref count : %d", count, get_pm_count(pd));
284 while (count--)
285 usb_autopm_put_interface(pd->interface);
286 /*usb_autopm_set_interface(pd->interface); */
287
288 usb_put_dev(pd->udev);
289 usb_put_intf(pd->interface);
290 log("event : %d\n", pd->msg.event);
291 return pd;
292}
293
294static struct poseidon *find_old_poseidon(struct usb_device *udev)
295{
296 struct poseidon *pd;
297
298 list_for_each_entry(pd, &pd_device_list, device_list) {
299 if (pd->portnum == udev->portnum && in_hibernation(pd))
300 return fixup(pd);
301 }
302 return NULL;
303}
304
305/* Is the card working now ? */
306static inline int is_working(struct poseidon *pd)
307{
308 return get_pm_count(pd) > 0;
309}
310
311static int poseidon_suspend(struct usb_interface *intf, pm_message_t msg)
312{
313 struct poseidon *pd = get_pd(intf);
314
315 if (!pd)
316 return 0;
317 if (!is_working(pd)) {
318 if (get_pm_count(pd) <= 0 && !in_hibernation(pd)) {
319 pd->msg.event = PM_EVENT_AUTO_SUSPEND;
320 pd->pm_resume = NULL; /* a good guard */
321 printk(KERN_DEBUG "\n\t+ TLG2300 auto suspend +\n\n");
322 }
323 return 0;
324 }
325 pd->msg = msg; /* save it here */
326 logpm(pd);
327 return pd->pm_suspend ? pd->pm_suspend(pd) : 0;
328}
329
330static int poseidon_resume(struct usb_interface *intf)
331{
332 struct poseidon *pd = get_pd(intf);
333
334 if (!pd)
335 return 0;
336 printk(KERN_DEBUG "\n\t ++ TLG2300 resume ++\n\n");
337
338 if (!is_working(pd)) {
339 if (PM_EVENT_AUTO_SUSPEND == pd->msg.event)
340 pd->msg = PMSG_ON;
341 return 0;
342 }
343 if (in_hibernation(pd)) {
344 logpm(pd);
345 return 0;
346 }
347 logpm(pd);
348 return pd->pm_resume ? pd->pm_resume(pd) : 0;
349}
350
351static void hibernation_resume(struct work_struct *w)
352{
353 struct poseidon *pd = container_of(w, struct poseidon, pm_work);
354 int count;
355
356 pd->msg.event = 0; /* clear it here */
357 pd->state &= ~POSEIDON_STATE_DISCONNECT;
358
359 /* set the new interface's reference */
360 count = get_autopm_ref(pd);
361 while (count--)
362 usb_autopm_get_interface(pd->interface);
363
364 /* resume the context */
365 logpm(pd);
366 if (pd->pm_resume)
367 pd->pm_resume(pd);
368}
369#else /* CONFIG_PM is not enabled: */
370static inline struct poseidon *find_old_poseidon(struct usb_device *udev)
371{
372 return NULL;
373}
374
375static inline void set_map_flags(struct poseidon *pd, struct usb_device *udev)
376{
377}
378#endif
379
380static bool check_firmware(struct usb_device *udev, int *down_firmware)
381{
382 void *buf;
383 int ret;
384 struct cmd_firmware_vers_s *cmd_firm;
385
386 buf = kzalloc(sizeof(*cmd_firm) + sizeof(u32), GFP_KERNEL);
387 if (!buf)
388 return -ENOMEM;
389 ret = usb_control_msg(udev,
390 usb_rcvctrlpipe(udev, 0),
391 REQ_GET_CMD | GET_FW_ID,
392 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
393 0,
394 0,
395 buf,
396 sizeof(*cmd_firm) + sizeof(u32),
397 USB_CTRL_GET_TIMEOUT);
398 kfree(buf);
399
400 if (ret < 0) {
401 *down_firmware = 1;
402 return firmware_download(udev);
403 }
404 return ret;
405}
406
407static int poseidon_probe(struct usb_interface *interface,
408 const struct usb_device_id *id)
409{
410 struct usb_device *udev = interface_to_usbdev(interface);
411 struct poseidon *pd = NULL;
412 int ret = 0;
413 int new_one = 0;
414
415 /* download firmware */
416 check_firmware(udev, &ret);
417 if (ret)
418 return 0;
419
420 /* Do I recovery from the hibernate ? */
421 pd = find_old_poseidon(udev);
422 if (!pd) {
423 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
424 if (!pd)
425 return -ENOMEM;
426 kref_init(&pd->kref);
427 set_map_flags(pd, udev);
428 new_one = 1;
429 }
430
431 pd->udev = usb_get_dev(udev);
432 pd->interface = usb_get_intf(interface);
433 usb_set_intfdata(interface, pd);
434
435 if (new_one) {
436 struct device *dev = &interface->dev;
437
438 logpm(pd);
439 mutex_init(&pd->lock);
440
441 /* register v4l2 device */
442 snprintf(pd->v4l2_dev.name, sizeof(pd->v4l2_dev.name), "%s %s",
443 dev->driver->name, dev_name(dev));
444 ret = v4l2_device_register(NULL, &pd->v4l2_dev);
445
446 /* register devices in directory /dev */
447 ret = pd_video_init(pd);
448 poseidon_audio_init(pd);
449 poseidon_fm_init(pd);
450 pd_dvb_usb_device_init(pd);
451
452 INIT_LIST_HEAD(&pd->device_list);
453 list_add_tail(&pd->device_list, &pd_device_list);
454 }
455
456 device_init_wakeup(&udev->dev, 1);
457#ifdef CONFIG_PM
458 pd->udev->autosuspend_disabled = 0;
459 pd->udev->autosuspend_delay = HZ * PM_SUSPEND_DELAY;
460
461 if (in_hibernation(pd)) {
462 INIT_WORK(&pd->pm_work, hibernation_resume);
463 schedule_work(&pd->pm_work);
464 }
465#endif
466 return 0;
467}
468
469static void poseidon_disconnect(struct usb_interface *interface)
470{
471 struct poseidon *pd = get_pd(interface);
472
473 if (!pd)
474 return;
475 logpm(pd);
476 if (in_hibernation(pd))
477 return;
478
479 mutex_lock(&pd->lock);
480 pd->state |= POSEIDON_STATE_DISCONNECT;
481 mutex_unlock(&pd->lock);
482
483 /* stop urb transferring */
484 stop_all_video_stream(pd);
485 dvb_stop_streaming(&pd->dvb_data);
486
487 /*unregister v4l2 device */
488 v4l2_device_unregister(&pd->v4l2_dev);
489
490 lock_kernel();
491 {
492 pd_dvb_usb_device_exit(pd);
493 poseidon_fm_exit(pd);
494
495 poseidon_audio_free(pd);
496 pd_video_exit(pd);
497 }
498 unlock_kernel();
499
500 usb_set_intfdata(interface, NULL);
501 kref_put(&pd->kref, poseidon_delete);
502}
503
504struct usb_driver poseidon_driver = {
505 .name = "poseidon",
506 .probe = poseidon_probe,
507 .disconnect = poseidon_disconnect,
508 .id_table = id_table,
509#ifdef CONFIG_PM
510 .suspend = poseidon_suspend,
511 .resume = poseidon_resume,
512#endif
513 .supports_autosuspend = 1,
514};
515
516static int __init poseidon_init(void)
517{
518 int ret;
519
520 ret = usb_register(&poseidon_driver);
521 if (ret)
522 return ret;
523 register_pm_notifier(&pm_notifer);
524 return ret;
525}
526
527static void __exit poseidon_exit(void)
528{
529 log();
530 unregister_pm_notifier(&pm_notifer);
531 usb_deregister(&poseidon_driver);
532}
533
534module_init(poseidon_init);
535module_exit(poseidon_exit);
536
537MODULE_AUTHOR("Telegent Systems");
538MODULE_DESCRIPTION("For tlg2300-based USB device ");
539MODULE_LICENSE("GPL");
diff --git a/drivers/media/video/tlg2300/pd-radio.c b/drivers/media/video/tlg2300/pd-radio.c
new file mode 100644
index 000000000000..755766b15157
--- /dev/null
+++ b/drivers/media/video/tlg2300/pd-radio.c
@@ -0,0 +1,420 @@
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
18static int set_frequency(struct poseidon *p, __u32 frequency);
19static int poseidon_fm_close(struct file *filp);
20static 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)
26static 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
32static 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);
55out:
56 return ret;
57}
58
59#ifdef CONFIG_PM
60static 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
69static 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
79static 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;
116out:
117 mutex_unlock(&p->lock);
118 return ret;
119}
120
121static 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
144static 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
157static 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
164int 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
206int 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
214static 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;
244error:
245 mutex_unlock(&p->lock);
246 return ret;
247}
248
249int 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
261int tlg_fm_vidioc_g_ctrl(struct file *file, void *priv,
262 struct v4l2_control *arg)
263{
264 return 0;
265}
266
267int 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
288int 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
315int tlg_fm_vidioc_s_ctrl(struct file *file, void *priv,
316 struct v4l2_control *ctrl)
317{
318 return 0;
319}
320
321int 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
340int 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
346static int vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *vt)
347{
348 return vt->index > 0 ? -EINVAL : 0;
349}
350static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *va)
351{
352 return (va->index != 0) ? -EINVAL : 0;
353}
354
355static 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
364static int vidioc_s_input(struct file *filp, void *priv, u32 i)
365{
366 return (i != 0) ? -EINVAL : 0;
367}
368
369static int vidioc_g_input(struct file *filp, void *priv, u32 *i)
370{
371 return (*i != 0) ? -EINVAL : 0;
372}
373
374static 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
392static 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
400int 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
416int poseidon_fm_exit(struct poseidon *p)
417{
418 destroy_video_device(&p->radio_data.fm_dev);
419 return 0;
420}
diff --git a/drivers/media/video/tlg2300/pd-video.c b/drivers/media/video/tlg2300/pd-video.c
new file mode 100644
index 000000000000..becfba6a3041
--- /dev/null
+++ b/drivers/media/video/tlg2300/pd-video.c
@@ -0,0 +1,1667 @@
1#include <linux/fs.h>
2#include <linux/vmalloc.h>
3#include <linux/videodev2.h>
4#include <linux/usb.h>
5#include <linux/mm.h>
6#include <linux/sched.h>
7
8#include <media/v4l2-ioctl.h>
9#include <media/v4l2-dev.h>
10
11#include "pd-common.h"
12#include "vendorcmds.h"
13
14static int pm_video_suspend(struct poseidon *pd);
15static int pm_video_resume(struct poseidon *pd);
16static void iso_bubble_handler(struct work_struct *w);
17
18int usb_transfer_mode;
19module_param(usb_transfer_mode, int, 0644);
20MODULE_PARM_DESC(usb_transfer_mode, "0 = Bulk, 1 = Isochronous");
21
22static const struct poseidon_format poseidon_formats[] = {
23 { "YUV 422", V4L2_PIX_FMT_YUYV, 16, 0},
24 { "RGB565", V4L2_PIX_FMT_RGB565, 16, 0},
25};
26
27static const struct poseidon_tvnorm poseidon_tvnorms[] = {
28 { V4L2_STD_PAL_D, "PAL-D", TLG_TUNE_VSTD_PAL_D },
29 { V4L2_STD_PAL_B, "PAL-B", TLG_TUNE_VSTD_PAL_B },
30 { V4L2_STD_PAL_G, "PAL-G", TLG_TUNE_VSTD_PAL_G },
31 { V4L2_STD_PAL_H, "PAL-H", TLG_TUNE_VSTD_PAL_H },
32 { V4L2_STD_PAL_I, "PAL-I", TLG_TUNE_VSTD_PAL_I },
33 { V4L2_STD_PAL_M, "PAL-M", TLG_TUNE_VSTD_PAL_M },
34 { V4L2_STD_PAL_N, "PAL-N", TLG_TUNE_VSTD_PAL_N_COMBO },
35 { V4L2_STD_PAL_Nc, "PAL-Nc", TLG_TUNE_VSTD_PAL_N_COMBO },
36 { V4L2_STD_NTSC_M, "NTSC-M", TLG_TUNE_VSTD_NTSC_M },
37 { V4L2_STD_NTSC_M_JP, "NTSC-JP", TLG_TUNE_VSTD_NTSC_M_J },
38 { V4L2_STD_SECAM_B, "SECAM-B", TLG_TUNE_VSTD_SECAM_B },
39 { V4L2_STD_SECAM_D, "SECAM-D", TLG_TUNE_VSTD_SECAM_D },
40 { V4L2_STD_SECAM_G, "SECAM-G", TLG_TUNE_VSTD_SECAM_G },
41 { V4L2_STD_SECAM_H, "SECAM-H", TLG_TUNE_VSTD_SECAM_H },
42 { V4L2_STD_SECAM_K, "SECAM-K", TLG_TUNE_VSTD_SECAM_K },
43 { V4L2_STD_SECAM_K1, "SECAM-K1", TLG_TUNE_VSTD_SECAM_K1 },
44 { V4L2_STD_SECAM_L, "SECAM-L", TLG_TUNE_VSTD_SECAM_L },
45 { V4L2_STD_SECAM_LC, "SECAM-LC", TLG_TUNE_VSTD_SECAM_L1 },
46};
47static const unsigned int POSEIDON_TVNORMS = ARRAY_SIZE(poseidon_tvnorms);
48
49struct pd_audio_mode {
50 u32 tlg_audio_mode;
51 u32 v4l2_audio_sub;
52 u32 v4l2_audio_mode;
53};
54
55static const struct pd_audio_mode pd_audio_modes[] = {
56 { TLG_TUNE_TVAUDIO_MODE_MONO, V4L2_TUNER_SUB_MONO,
57 V4L2_TUNER_MODE_MONO },
58 { TLG_TUNE_TVAUDIO_MODE_STEREO, V4L2_TUNER_SUB_STEREO,
59 V4L2_TUNER_MODE_STEREO },
60 { TLG_TUNE_TVAUDIO_MODE_LANG_A, V4L2_TUNER_SUB_LANG1,
61 V4L2_TUNER_MODE_LANG1 },
62 { TLG_TUNE_TVAUDIO_MODE_LANG_B, V4L2_TUNER_SUB_LANG2,
63 V4L2_TUNER_MODE_LANG2 },
64 { TLG_TUNE_TVAUDIO_MODE_LANG_C, V4L2_TUNER_SUB_LANG1,
65 V4L2_TUNER_MODE_LANG1_LANG2 }
66};
67static const unsigned int POSEIDON_AUDIOMODS = ARRAY_SIZE(pd_audio_modes);
68
69struct pd_input {
70 char *name;
71 uint32_t tlg_src;
72};
73
74static const struct pd_input pd_inputs[] = {
75 { "TV Antenna", TLG_SIG_SRC_ANTENNA },
76 { "TV Cable", TLG_SIG_SRC_CABLE },
77 { "TV SVideo", TLG_SIG_SRC_SVIDEO },
78 { "TV Composite", TLG_SIG_SRC_COMPOSITE }
79};
80static const unsigned int POSEIDON_INPUTS = ARRAY_SIZE(pd_inputs);
81
82struct poseidon_control {
83 struct v4l2_queryctrl v4l2_ctrl;
84 enum cmd_custom_param_id vc_id;
85};
86
87static struct poseidon_control controls[] = {
88 {
89 { V4L2_CID_BRIGHTNESS, V4L2_CTRL_TYPE_INTEGER,
90 "brightness", 0, 10000, 1, 100, 0, },
91 CUST_PARM_ID_BRIGHTNESS_CTRL
92 }, {
93 { V4L2_CID_CONTRAST, V4L2_CTRL_TYPE_INTEGER,
94 "contrast", 0, 10000, 1, 100, 0, },
95 CUST_PARM_ID_CONTRAST_CTRL,
96 }, {
97 { V4L2_CID_HUE, V4L2_CTRL_TYPE_INTEGER,
98 "hue", 0, 10000, 1, 100, 0, },
99 CUST_PARM_ID_HUE_CTRL,
100 }, {
101 { V4L2_CID_SATURATION, V4L2_CTRL_TYPE_INTEGER,
102 "saturation", 0, 10000, 1, 100, 0, },
103 CUST_PARM_ID_SATURATION_CTRL,
104 },
105};
106
107struct video_std_to_audio_std {
108 v4l2_std_id video_std;
109 int audio_std;
110};
111
112static const struct video_std_to_audio_std video_to_audio_map[] = {
113 /* country : { 27, 32, 33, 34, 36, 44, 45, 46, 47, 48, 64,
114 65, 86, 351, 352, 353, 354, 358, 372, 852, 972 } */
115 { (V4L2_STD_PAL_I | V4L2_STD_PAL_B | V4L2_STD_PAL_D |
116 V4L2_STD_SECAM_L | V4L2_STD_SECAM_D), TLG_TUNE_ASTD_NICAM },
117
118 /* country : { 1, 52, 54, 55, 886 } */
119 {V4L2_STD_NTSC_M | V4L2_STD_PAL_N | V4L2_STD_PAL_M, TLG_TUNE_ASTD_BTSC},
120
121 /* country : { 81 } */
122 { V4L2_STD_NTSC_M_JP, TLG_TUNE_ASTD_EIAJ },
123
124 /* other country : TLG_TUNE_ASTD_A2 */
125};
126static const unsigned int map_size = ARRAY_SIZE(video_to_audio_map);
127
128static int get_audio_std(v4l2_std_id v4l2_std)
129{
130 int i = 0;
131
132 for (; i < map_size; i++) {
133 if (v4l2_std & video_to_audio_map[i].video_std)
134 return video_to_audio_map[i].audio_std;
135 }
136 return TLG_TUNE_ASTD_A2;
137}
138
139static int vidioc_querycap(struct file *file, void *fh,
140 struct v4l2_capability *cap)
141{
142 struct front_face *front = fh;
143 struct poseidon *p = front->pd;
144
145 logs(front);
146
147 strcpy(cap->driver, "tele-video");
148 strcpy(cap->card, "Telegent Poseidon");
149 usb_make_path(p->udev, cap->bus_info, sizeof(cap->bus_info));
150 cap->version = KERNEL_VERSION(0, 0, 1);
151 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TUNER |
152 V4L2_CAP_AUDIO | V4L2_CAP_STREAMING |
153 V4L2_CAP_READWRITE | V4L2_CAP_VBI_CAPTURE;
154 return 0;
155}
156
157/*====================================================================*/
158static void init_copy(struct video_data *video, bool index)
159{
160 struct front_face *front = video->front;
161
162 video->field_count = index;
163 video->lines_copied = 0;
164 video->prev_left = 0 ;
165 video->dst = (char *)videobuf_to_vmalloc(front->curr_frame)
166 + index * video->lines_size;
167 video->vbi->copied = 0; /* set it here */
168}
169
170static bool get_frame(struct front_face *front, int *need_init)
171{
172 struct videobuf_buffer *vb = front->curr_frame;
173
174 if (vb)
175 return true;
176
177 spin_lock(&front->queue_lock);
178 if (!list_empty(&front->active)) {
179 vb = list_entry(front->active.next,
180 struct videobuf_buffer, queue);
181 if (need_init)
182 *need_init = 1;
183 front->curr_frame = vb;
184 list_del_init(&vb->queue);
185 }
186 spin_unlock(&front->queue_lock);
187
188 return !!vb;
189}
190
191/* check if the video's buffer is ready */
192static bool get_video_frame(struct front_face *front, struct video_data *video)
193{
194 int need_init = 0;
195 bool ret = true;
196
197 ret = get_frame(front, &need_init);
198 if (ret && need_init)
199 init_copy(video, 0);
200 return ret;
201}
202
203static void submit_frame(struct front_face *front)
204{
205 struct videobuf_buffer *vb = front->curr_frame;
206
207 if (vb == NULL)
208 return;
209
210 front->curr_frame = NULL;
211 vb->state = VIDEOBUF_DONE;
212 vb->field_count++;
213 do_gettimeofday(&vb->ts);
214
215 wake_up(&vb->done);
216}
217
218/*
219 * A frame is composed of two fields. If we receive all the two fields,
220 * call the submit_frame() to submit the whole frame to applications.
221 */
222static void end_field(struct video_data *video)
223{
224 /* logs(video->front); */
225 if (1 == video->field_count)
226 submit_frame(video->front);
227 else
228 init_copy(video, 1);
229}
230
231static void copy_video_data(struct video_data *video, char *src,
232 unsigned int count)
233{
234#define copy_data(len) \
235 do { \
236 if (++video->lines_copied > video->lines_per_field) \
237 goto overflow; \
238 memcpy(video->dst, src, len);\
239 video->dst += len + video->lines_size; \
240 src += len; \
241 count -= len; \
242 } while (0)
243
244 while (count && count >= video->lines_size) {
245 if (video->prev_left) {
246 copy_data(video->prev_left);
247 video->prev_left = 0;
248 continue;
249 }
250 copy_data(video->lines_size);
251 }
252 if (count && count < video->lines_size) {
253 memcpy(video->dst, src, count);
254
255 video->prev_left = video->lines_size - count;
256 video->dst += count;
257 }
258 return;
259
260overflow:
261 end_field(video);
262}
263
264static void check_trailer(struct video_data *video, char *src, int count)
265{
266 struct vbi_data *vbi = video->vbi;
267 int offset; /* trailer's offset */
268 char *buf;
269
270 offset = (video->context.pix.sizeimage / 2 + vbi->vbi_size / 2)
271 - (vbi->copied + video->lines_size * video->lines_copied);
272 if (video->prev_left)
273 offset -= (video->lines_size - video->prev_left);
274
275 if (offset > count || offset <= 0)
276 goto short_package;
277
278 buf = src + offset;
279
280 /* trailer : (VFHS) + U32 + U32 + field_num */
281 if (!strncmp(buf, "VFHS", 4)) {
282 int field_num = *((u32 *)(buf + 12));
283
284 if ((field_num & 1) ^ video->field_count) {
285 init_copy(video, video->field_count);
286 return;
287 }
288 copy_video_data(video, src, offset);
289 }
290short_package:
291 end_field(video);
292}
293
294/* ========== Check this more carefully! =========== */
295static inline void copy_vbi_data(struct vbi_data *vbi,
296 char *src, unsigned int count)
297{
298 struct front_face *front = vbi->front;
299
300 if (front && get_frame(front, NULL)) {
301 char *buf = videobuf_to_vmalloc(front->curr_frame);
302
303 if (vbi->video->field_count)
304 buf += (vbi->vbi_size / 2);
305 memcpy(buf + vbi->copied, src, count);
306 }
307 vbi->copied += count;
308}
309
310/*
311 * Copy the normal data (VBI or VIDEO) without the trailer.
312 * VBI is not interlaced, while VIDEO is interlaced.
313 */
314static inline void copy_vbi_video_data(struct video_data *video,
315 char *src, unsigned int count)
316{
317 struct vbi_data *vbi = video->vbi;
318 unsigned int vbi_delta = (vbi->vbi_size / 2) - vbi->copied;
319
320 if (vbi_delta >= count) {
321 copy_vbi_data(vbi, src, count);
322 } else {
323 if (vbi_delta) {
324 copy_vbi_data(vbi, src, vbi_delta);
325
326 /* we receive the two fields of the VBI*/
327 if (vbi->front && video->field_count)
328 submit_frame(vbi->front);
329 }
330 copy_video_data(video, src + vbi_delta, count - vbi_delta);
331 }
332}
333
334static void urb_complete_bulk(struct urb *urb)
335{
336 struct front_face *front = urb->context;
337 struct video_data *video = &front->pd->video_data;
338 char *src = (char *)urb->transfer_buffer;
339 int count = urb->actual_length;
340 int ret = 0;
341
342 if (!video->is_streaming || urb->status) {
343 if (urb->status == -EPROTO)
344 goto resend_it;
345 return;
346 }
347 if (!get_video_frame(front, video))
348 goto resend_it;
349
350 if (count == urb->transfer_buffer_length)
351 copy_vbi_video_data(video, src, count);
352 else
353 check_trailer(video, src, count);
354
355resend_it:
356 ret = usb_submit_urb(urb, GFP_ATOMIC);
357 if (ret)
358 log(" submit failed: error %d", ret);
359}
360
361/************************* for ISO *********************/
362#define GET_SUCCESS (0)
363#define GET_TRAILER (1)
364#define GET_TOO_MUCH_BUBBLE (2)
365#define GET_NONE (3)
366static int get_chunk(int start, struct urb *urb,
367 int *head, int *tail, int *bubble_err)
368{
369 struct usb_iso_packet_descriptor *pkt = NULL;
370 int ret = GET_SUCCESS;
371
372 for (*head = *tail = -1; start < urb->number_of_packets; start++) {
373 pkt = &urb->iso_frame_desc[start];
374
375 /* handle the bubble of the Hub */
376 if (-EOVERFLOW == pkt->status) {
377 if (++*bubble_err > urb->number_of_packets / 3)
378 return GET_TOO_MUCH_BUBBLE;
379 continue;
380 }
381
382 /* This is the gap */
383 if (pkt->status || pkt->actual_length <= 0
384 || pkt->actual_length > ISO_PKT_SIZE) {
385 if (*head != -1)
386 break;
387 continue;
388 }
389
390 /* a good isochronous packet */
391 if (pkt->actual_length == ISO_PKT_SIZE) {
392 if (*head == -1)
393 *head = start;
394 *tail = start;
395 continue;
396 }
397
398 /* trailer is here */
399 if (pkt->actual_length < ISO_PKT_SIZE) {
400 if (*head == -1) {
401 *head = start;
402 *tail = start;
403 return GET_TRAILER;
404 }
405 break;
406 }
407 }
408
409 if (*head == -1 && *tail == -1)
410 ret = GET_NONE;
411 return ret;
412}
413
414/*
415 * |__|------|___|-----|_______|
416 * ^ ^
417 * | |
418 * gap gap
419 */
420static void urb_complete_iso(struct urb *urb)
421{
422 struct front_face *front = urb->context;
423 struct video_data *video = &front->pd->video_data;
424 int bubble_err = 0, head = 0, tail = 0;
425 char *src = (char *)urb->transfer_buffer;
426 int ret = 0;
427
428 if (!video->is_streaming)
429 return;
430
431 do {
432 if (!get_video_frame(front, video))
433 goto out;
434
435 switch (get_chunk(head, urb, &head, &tail, &bubble_err)) {
436 case GET_SUCCESS:
437 copy_vbi_video_data(video, src + (head * ISO_PKT_SIZE),
438 (tail - head + 1) * ISO_PKT_SIZE);
439 break;
440 case GET_TRAILER:
441 check_trailer(video, src + (head * ISO_PKT_SIZE),
442 ISO_PKT_SIZE);
443 break;
444 case GET_NONE:
445 goto out;
446 case GET_TOO_MUCH_BUBBLE:
447 log("\t We got too much bubble");
448 schedule_work(&video->bubble_work);
449 return;
450 }
451 } while (head = tail + 1, head < urb->number_of_packets);
452
453out:
454 ret = usb_submit_urb(urb, GFP_ATOMIC);
455 if (ret)
456 log("usb_submit_urb err : %d", ret);
457}
458/*============================= [ end ] =====================*/
459
460static int prepare_iso_urb(struct video_data *video)
461{
462 struct usb_device *udev = video->pd->udev;
463 int i;
464
465 if (video->urb_array[0])
466 return 0;
467
468 for (i = 0; i < SBUF_NUM; i++) {
469 struct urb *urb;
470 void *mem;
471 int j;
472
473 urb = usb_alloc_urb(PK_PER_URB, GFP_KERNEL);
474 if (urb == NULL)
475 goto out;
476
477 video->urb_array[i] = urb;
478 mem = usb_buffer_alloc(udev,
479 ISO_PKT_SIZE * PK_PER_URB,
480 GFP_KERNEL,
481 &urb->transfer_dma);
482
483 urb->complete = urb_complete_iso; /* handler */
484 urb->dev = udev;
485 urb->context = video->front;
486 urb->pipe = usb_rcvisocpipe(udev,
487 video->endpoint_addr);
488 urb->interval = 1;
489 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
490 urb->number_of_packets = PK_PER_URB;
491 urb->transfer_buffer = mem;
492 urb->transfer_buffer_length = PK_PER_URB * ISO_PKT_SIZE;
493
494 for (j = 0; j < PK_PER_URB; j++) {
495 urb->iso_frame_desc[j].offset = ISO_PKT_SIZE * j;
496 urb->iso_frame_desc[j].length = ISO_PKT_SIZE;
497 }
498 }
499 return 0;
500out:
501 for (; i > 0; i--)
502 ;
503 return -ENOMEM;
504}
505
506/* return the succeeded number of the allocation */
507int alloc_bulk_urbs_generic(struct urb **urb_array, int num,
508 struct usb_device *udev, u8 ep_addr,
509 int buf_size, gfp_t gfp_flags,
510 usb_complete_t complete_fn, void *context)
511{
512 struct urb *urb;
513 void *mem;
514 int i;
515
516 for (i = 0; i < num; i++) {
517 urb = usb_alloc_urb(0, gfp_flags);
518 if (urb == NULL)
519 return i;
520
521 mem = usb_buffer_alloc(udev, buf_size, gfp_flags,
522 &urb->transfer_dma);
523 if (mem == NULL)
524 return i;
525
526 usb_fill_bulk_urb(urb, udev, usb_rcvbulkpipe(udev, ep_addr),
527 mem, buf_size, complete_fn, context);
528 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
529 urb_array[i] = urb;
530 }
531 return i;
532}
533
534void free_all_urb_generic(struct urb **urb_array, int num)
535{
536 int i;
537 struct urb *urb;
538
539 for (i = 0; i < num; i++) {
540 urb = urb_array[i];
541 if (urb) {
542 usb_buffer_free(urb->dev,
543 urb->transfer_buffer_length,
544 urb->transfer_buffer,
545 urb->transfer_dma);
546 usb_free_urb(urb);
547 urb_array[i] = NULL;
548 }
549 }
550}
551
552static int prepare_bulk_urb(struct video_data *video)
553{
554 if (video->urb_array[0])
555 return 0;
556
557 alloc_bulk_urbs_generic(video->urb_array, SBUF_NUM,
558 video->pd->udev, video->endpoint_addr,
559 0x2000, GFP_KERNEL,
560 urb_complete_bulk, video->front);
561 return 0;
562}
563
564/* free the URBs */
565static void free_all_urb(struct video_data *video)
566{
567 free_all_urb_generic(video->urb_array, SBUF_NUM);
568}
569
570static void pd_buf_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
571{
572 videobuf_vmalloc_free(vb);
573 vb->state = VIDEOBUF_NEEDS_INIT;
574}
575
576static void pd_buf_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
577{
578 struct front_face *front = q->priv_data;
579 vb->state = VIDEOBUF_QUEUED;
580 list_add_tail(&vb->queue, &front->active);
581}
582
583static int pd_buf_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
584 enum v4l2_field field)
585{
586 struct front_face *front = q->priv_data;
587 int rc;
588
589 switch (front->type) {
590 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
591 if (VIDEOBUF_NEEDS_INIT == vb->state) {
592 struct v4l2_pix_format *pix;
593
594 pix = &front->pd->video_data.context.pix;
595 vb->size = pix->sizeimage; /* real frame size */
596 vb->width = pix->width;
597 vb->height = pix->height;
598 rc = videobuf_iolock(q, vb, NULL);
599 if (rc < 0)
600 return rc;
601 }
602 break;
603 case V4L2_BUF_TYPE_VBI_CAPTURE:
604 if (VIDEOBUF_NEEDS_INIT == vb->state) {
605 vb->size = front->pd->vbi_data.vbi_size;
606 rc = videobuf_iolock(q, vb, NULL);
607 if (rc < 0)
608 return rc;
609 }
610 break;
611 default:
612 return -EINVAL;
613 }
614 vb->field = field;
615 vb->state = VIDEOBUF_PREPARED;
616 return 0;
617}
618
619int fire_all_urb(struct video_data *video)
620{
621 int i, ret;
622
623 video->is_streaming = 1;
624
625 for (i = 0; i < SBUF_NUM; i++) {
626 ret = usb_submit_urb(video->urb_array[i], GFP_KERNEL);
627 if (ret)
628 log("(%d) failed: error %d", i, ret);
629 }
630 return ret;
631}
632
633static int start_video_stream(struct poseidon *pd)
634{
635 struct video_data *video = &pd->video_data;
636 s32 cmd_status;
637
638 send_set_req(pd, TAKE_REQUEST, 0, &cmd_status);
639 send_set_req(pd, PLAY_SERVICE, TLG_TUNE_PLAY_SVC_START, &cmd_status);
640
641 if (pd->cur_transfer_mode) {
642 prepare_iso_urb(video);
643 INIT_WORK(&video->bubble_work, iso_bubble_handler);
644 } else {
645 /* The bulk mode does not need a bubble handler */
646 prepare_bulk_urb(video);
647 }
648 fire_all_urb(video);
649 return 0;
650}
651
652static int pd_buf_setup(struct videobuf_queue *q, unsigned int *count,
653 unsigned int *size)
654{
655 struct front_face *front = q->priv_data;
656 struct poseidon *pd = front->pd;
657
658 switch (front->type) {
659 default:
660 return -EINVAL;
661 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
662 struct video_data *video = &pd->video_data;
663 struct v4l2_pix_format *pix = &video->context.pix;
664
665 *size = PAGE_ALIGN(pix->sizeimage);/* page aligned frame size */
666 if (*count < 4)
667 *count = 4;
668 if (1) {
669 /* same in different altersetting */
670 video->endpoint_addr = 0x82;
671 video->vbi = &pd->vbi_data;
672 video->vbi->video = video;
673 video->pd = pd;
674 video->lines_per_field = pix->height / 2;
675 video->lines_size = pix->width * 2;
676 video->front = front;
677 }
678 return start_video_stream(pd);
679 }
680
681 case V4L2_BUF_TYPE_VBI_CAPTURE: {
682 struct vbi_data *vbi = &pd->vbi_data;
683
684 *size = PAGE_ALIGN(vbi->vbi_size);
685 log("size : %d", *size);
686 if (*count == 0)
687 *count = 4;
688 }
689 break;
690 }
691 return 0;
692}
693
694static struct videobuf_queue_ops pd_video_qops = {
695 .buf_setup = pd_buf_setup,
696 .buf_prepare = pd_buf_prepare,
697 .buf_queue = pd_buf_queue,
698 .buf_release = pd_buf_release,
699};
700
701static int vidioc_enum_fmt(struct file *file, void *fh,
702 struct v4l2_fmtdesc *f)
703{
704 if (ARRAY_SIZE(poseidon_formats) <= f->index)
705 return -EINVAL;
706 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
707 f->flags = 0;
708 f->pixelformat = poseidon_formats[f->index].fourcc;
709 strcpy(f->description, poseidon_formats[f->index].name);
710 return 0;
711}
712
713static int vidioc_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
714{
715 struct front_face *front = fh;
716 struct poseidon *pd = front->pd;
717
718 logs(front);
719 f->fmt.pix = pd->video_data.context.pix;
720 return 0;
721}
722
723static int vidioc_try_fmt(struct file *file, void *fh,
724 struct v4l2_format *f)
725{
726 return 0;
727}
728
729/*
730 * VLC calls VIDIOC_S_STD before VIDIOC_S_FMT, while
731 * Mplayer calls them in the reverse order.
732 */
733static int pd_vidioc_s_fmt(struct poseidon *pd, struct v4l2_pix_format *pix)
734{
735 struct video_data *video = &pd->video_data;
736 struct running_context *context = &video->context;
737 struct v4l2_pix_format *pix_def = &context->pix;
738 s32 ret = 0, cmd_status = 0, vid_resol;
739
740 /* set the pixel format to firmware */
741 if (pix->pixelformat == V4L2_PIX_FMT_RGB565) {
742 vid_resol = TLG_TUNER_VID_FORMAT_RGB_565;
743 } else {
744 pix->pixelformat = V4L2_PIX_FMT_YUYV;
745 vid_resol = TLG_TUNER_VID_FORMAT_YUV;
746 }
747 ret = send_set_req(pd, VIDEO_STREAM_FMT_SEL,
748 vid_resol, &cmd_status);
749
750 /* set the resolution to firmware */
751 vid_resol = TLG_TUNE_VID_RES_720;
752 switch (pix->width) {
753 case 704:
754 vid_resol = TLG_TUNE_VID_RES_704;
755 break;
756 default:
757 pix->width = 720;
758 case 720:
759 break;
760 }
761 ret |= send_set_req(pd, VIDEO_ROSOLU_SEL,
762 vid_resol, &cmd_status);
763 if (ret || cmd_status) {
764 mutex_unlock(&pd->lock);
765 return -EBUSY;
766 }
767
768 pix_def->pixelformat = pix->pixelformat; /* save it */
769 pix->height = (context->tvnormid & V4L2_STD_525_60) ? 480 : 576;
770
771 /* Compare with the default setting */
772 if ((pix_def->width != pix->width)
773 || (pix_def->height != pix->height)) {
774 pix_def->width = pix->width;
775 pix_def->height = pix->height;
776 pix_def->bytesperline = pix->width * 2;
777 pix_def->sizeimage = pix->width * pix->height * 2;
778 }
779 *pix = *pix_def;
780
781 return 0;
782}
783
784static int vidioc_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
785{
786 struct front_face *front = fh;
787 struct poseidon *pd = front->pd;
788
789 logs(front);
790 /* stop VBI here */
791 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != f->type)
792 return -EINVAL;
793
794 mutex_lock(&pd->lock);
795 if (pd->file_for_stream == NULL)
796 pd->file_for_stream = file;
797 else if (file != pd->file_for_stream) {
798 mutex_unlock(&pd->lock);
799 return -EINVAL;
800 }
801
802 pd_vidioc_s_fmt(pd, &f->fmt.pix);
803 mutex_unlock(&pd->lock);
804 return 0;
805}
806
807static int vidioc_g_fmt_vbi(struct file *file, void *fh,
808 struct v4l2_format *v4l2_f)
809{
810 struct front_face *front = fh;
811 struct poseidon *pd = front->pd;
812 struct v4l2_vbi_format *vbi_fmt = &v4l2_f->fmt.vbi;
813
814 vbi_fmt->samples_per_line = 720 * 2;
815 vbi_fmt->sampling_rate = 6750000 * 4;
816 vbi_fmt->sample_format = V4L2_PIX_FMT_GREY;
817 vbi_fmt->offset = 64 * 4; /*FIXME: why offset */
818 if (pd->video_data.context.tvnormid & V4L2_STD_525_60) {
819 vbi_fmt->start[0] = 10;
820 vbi_fmt->start[1] = 264;
821 vbi_fmt->count[0] = V4L_NTSC_VBI_LINES;
822 vbi_fmt->count[1] = V4L_NTSC_VBI_LINES;
823 } else {
824 vbi_fmt->start[0] = 6;
825 vbi_fmt->start[1] = 314;
826 vbi_fmt->count[0] = V4L_PAL_VBI_LINES;
827 vbi_fmt->count[1] = V4L_PAL_VBI_LINES;
828 }
829 vbi_fmt->flags = V4L2_VBI_UNSYNC;
830 logs(front);
831 return 0;
832}
833
834static int set_std(struct poseidon *pd, v4l2_std_id *norm)
835{
836 struct video_data *video = &pd->video_data;
837 struct vbi_data *vbi = &pd->vbi_data;
838 struct running_context *context;
839 struct v4l2_pix_format *pix;
840 s32 i, ret = 0, cmd_status, param;
841 int height;
842
843 for (i = 0; i < POSEIDON_TVNORMS; i++) {
844 if (*norm & poseidon_tvnorms[i].v4l2_id) {
845 param = poseidon_tvnorms[i].tlg_tvnorm;
846 log("name : %s", poseidon_tvnorms[i].name);
847 goto found;
848 }
849 }
850 return -EINVAL;
851found:
852 mutex_lock(&pd->lock);
853 ret = send_set_req(pd, VIDEO_STD_SEL, param, &cmd_status);
854 if (ret || cmd_status)
855 goto out;
856
857 /* Set vbi size and check the height of the frame */
858 context = &video->context;
859 context->tvnormid = poseidon_tvnorms[i].v4l2_id;
860 if (context->tvnormid & V4L2_STD_525_60) {
861 vbi->vbi_size = V4L_NTSC_VBI_FRAMESIZE;
862 height = 480;
863 } else {
864 vbi->vbi_size = V4L_PAL_VBI_FRAMESIZE;
865 height = 576;
866 }
867
868 pix = &context->pix;
869 if (pix->height != height) {
870 pix->height = height;
871 pix->sizeimage = pix->width * pix->height * 2;
872 }
873
874out:
875 mutex_unlock(&pd->lock);
876 return ret;
877}
878
879int vidioc_s_std(struct file *file, void *fh, v4l2_std_id *norm)
880{
881 struct front_face *front = fh;
882 logs(front);
883 return set_std(front->pd, norm);
884}
885
886static int vidioc_enum_input(struct file *file, void *fh, struct v4l2_input *in)
887{
888 struct front_face *front = fh;
889
890 if (in->index < 0 || in->index >= POSEIDON_INPUTS)
891 return -EINVAL;
892 strcpy(in->name, pd_inputs[in->index].name);
893 in->type = V4L2_INPUT_TYPE_TUNER;
894
895 /*
896 * the audio input index mixed with this video input,
897 * Poseidon only have one audio/video, set to "0"
898 */
899 in->audioset = 0;
900 in->tuner = 0;
901 in->std = V4L2_STD_ALL;
902 in->status = 0;
903 logs(front);
904 return 0;
905}
906
907static int vidioc_g_input(struct file *file, void *fh, unsigned int *i)
908{
909 struct front_face *front = fh;
910 struct poseidon *pd = front->pd;
911 struct running_context *context = &pd->video_data.context;
912
913 logs(front);
914 *i = context->sig_index;
915 return 0;
916}
917
918/* We can support several inputs */
919static int vidioc_s_input(struct file *file, void *fh, unsigned int i)
920{
921 struct front_face *front = fh;
922 struct poseidon *pd = front->pd;
923 s32 ret, cmd_status;
924
925 if (i < 0 || i >= POSEIDON_INPUTS)
926 return -EINVAL;
927 ret = send_set_req(pd, SGNL_SRC_SEL,
928 pd_inputs[i].tlg_src, &cmd_status);
929 if (ret)
930 return ret;
931
932 pd->video_data.context.sig_index = i;
933 return 0;
934}
935
936static struct poseidon_control *check_control_id(__u32 id)
937{
938 struct poseidon_control *control = &controls[0];
939 int array_size = ARRAY_SIZE(controls);
940
941 for (; control < &controls[array_size]; control++)
942 if (control->v4l2_ctrl.id == id)
943 return control;
944 return NULL;
945}
946
947static int vidioc_queryctrl(struct file *file, void *fh,
948 struct v4l2_queryctrl *a)
949{
950 struct poseidon_control *control = NULL;
951
952 control = check_control_id(a->id);
953 if (!control)
954 return -EINVAL;
955
956 *a = control->v4l2_ctrl;
957 return 0;
958}
959
960static int vidioc_g_ctrl(struct file *file, void *fh, struct v4l2_control *ctrl)
961{
962 struct front_face *front = fh;
963 struct poseidon *pd = front->pd;
964 struct poseidon_control *control = NULL;
965 struct tuner_custom_parameter_s tuner_param;
966 s32 ret = 0, cmd_status;
967
968 control = check_control_id(ctrl->id);
969 if (!control)
970 return -EINVAL;
971
972 mutex_lock(&pd->lock);
973 ret = send_get_req(pd, TUNER_CUSTOM_PARAMETER, control->vc_id,
974 &tuner_param, &cmd_status, sizeof(tuner_param));
975 mutex_unlock(&pd->lock);
976
977 if (ret || cmd_status)
978 return -1;
979
980 ctrl->value = tuner_param.param_value;
981 return 0;
982}
983
984static int vidioc_s_ctrl(struct file *file, void *fh, struct v4l2_control *a)
985{
986 struct tuner_custom_parameter_s param = {0};
987 struct poseidon_control *control = NULL;
988 struct front_face *front = fh;
989 struct poseidon *pd = front->pd;
990 s32 ret = 0, cmd_status, params;
991
992 control = check_control_id(a->id);
993 if (!control)
994 return -EINVAL;
995
996 param.param_value = a->value;
997 param.param_id = control->vc_id;
998 params = *(s32 *)&param; /* temp code */
999
1000 mutex_lock(&pd->lock);
1001 ret = send_set_req(pd, TUNER_CUSTOM_PARAMETER, params, &cmd_status);
1002 ret = send_set_req(pd, TAKE_REQUEST, 0, &cmd_status);
1003 mutex_unlock(&pd->lock);
1004
1005 set_current_state(TASK_INTERRUPTIBLE);
1006 schedule_timeout(HZ/4);
1007 return ret;
1008}
1009
1010/* Audio ioctls */
1011static int vidioc_enumaudio(struct file *file, void *fh, struct v4l2_audio *a)
1012{
1013 if (0 != a->index)
1014 return -EINVAL;
1015 a->capability = V4L2_AUDCAP_STEREO;
1016 strcpy(a->name, "USB audio in");
1017 /*Poseidon have no AVL function.*/
1018 a->mode = 0;
1019 return 0;
1020}
1021
1022int vidioc_g_audio(struct file *file, void *fh, struct v4l2_audio *a)
1023{
1024 a->index = 0;
1025 a->capability = V4L2_AUDCAP_STEREO;
1026 strcpy(a->name, "USB audio in");
1027 a->mode = 0;
1028 return 0;
1029}
1030
1031int vidioc_s_audio(struct file *file, void *fh, struct v4l2_audio *a)
1032{
1033 return (0 == a->index) ? 0 : -EINVAL;
1034}
1035
1036/* Tuner ioctls */
1037static int vidioc_g_tuner(struct file *file, void *fh, struct v4l2_tuner *tuner)
1038{
1039 struct front_face *front = fh;
1040 struct poseidon *pd = front->pd;
1041 struct tuner_atv_sig_stat_s atv_stat;
1042 s32 count = 5, ret, cmd_status;
1043 int index;
1044
1045 if (0 != tuner->index)
1046 return -EINVAL;
1047
1048 mutex_lock(&pd->lock);
1049 ret = send_get_req(pd, TUNER_STATUS, TLG_MODE_ANALOG_TV,
1050 &atv_stat, &cmd_status, sizeof(atv_stat));
1051
1052 while (atv_stat.sig_lock_busy && count-- && !ret) {
1053 set_current_state(TASK_INTERRUPTIBLE);
1054 schedule_timeout(HZ);
1055
1056 ret = send_get_req(pd, TUNER_STATUS, TLG_MODE_ANALOG_TV,
1057 &atv_stat, &cmd_status, sizeof(atv_stat));
1058 }
1059 mutex_unlock(&pd->lock);
1060
1061 if (debug_mode)
1062 log("P:%d,S:%d", atv_stat.sig_present, atv_stat.sig_strength);
1063
1064 if (ret || cmd_status)
1065 tuner->signal = 0;
1066 else if (atv_stat.sig_present && !atv_stat.sig_strength)
1067 tuner->signal = 0xFFFF;
1068 else
1069 tuner->signal = (atv_stat.sig_strength * 255 / 10) << 8;
1070
1071 strcpy(tuner->name, "Telegent Systems");
1072 tuner->type = V4L2_TUNER_ANALOG_TV;
1073 tuner->rangelow = TUNER_FREQ_MIN / 62500;
1074 tuner->rangehigh = TUNER_FREQ_MAX / 62500;
1075 tuner->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO |
1076 V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
1077 index = pd->video_data.context.audio_idx;
1078 tuner->rxsubchans = pd_audio_modes[index].v4l2_audio_sub;
1079 tuner->audmode = pd_audio_modes[index].v4l2_audio_mode;
1080 tuner->afc = 0;
1081 logs(front);
1082 return 0;
1083}
1084
1085static int pd_vidioc_s_tuner(struct poseidon *pd, int index)
1086{
1087 s32 ret = 0, cmd_status, param, audiomode;
1088
1089 mutex_lock(&pd->lock);
1090 param = pd_audio_modes[index].tlg_audio_mode;
1091 ret = send_set_req(pd, TUNER_AUD_MODE, param, &cmd_status);
1092 audiomode = get_audio_std(pd->video_data.context.tvnormid);
1093 ret |= send_set_req(pd, TUNER_AUD_ANA_STD, audiomode,
1094 &cmd_status);
1095 if (!ret)
1096 pd->video_data.context.audio_idx = index;
1097 mutex_unlock(&pd->lock);
1098 return ret;
1099}
1100
1101static int vidioc_s_tuner(struct file *file, void *fh, struct v4l2_tuner *a)
1102{
1103 struct front_face *front = fh;
1104 struct poseidon *pd = front->pd;
1105 int index;
1106
1107 if (0 != a->index)
1108 return -EINVAL;
1109 logs(front);
1110 for (index = 0; index < POSEIDON_AUDIOMODS; index++)
1111 if (a->audmode == pd_audio_modes[index].v4l2_audio_mode)
1112 return pd_vidioc_s_tuner(pd, index);
1113 return -EINVAL;
1114}
1115
1116static int vidioc_g_frequency(struct file *file, void *fh,
1117 struct v4l2_frequency *freq)
1118{
1119 struct front_face *front = fh;
1120 struct poseidon *pd = front->pd;
1121 struct running_context *context = &pd->video_data.context;
1122
1123 if (0 != freq->tuner)
1124 return -EINVAL;
1125 freq->frequency = context->freq;
1126 freq->type = V4L2_TUNER_ANALOG_TV;
1127 return 0;
1128}
1129
1130static int set_frequency(struct poseidon *pd, __u32 frequency)
1131{
1132 s32 ret = 0, param, cmd_status;
1133 struct running_context *context = &pd->video_data.context;
1134
1135 param = frequency * 62500 / 1000;
1136 if (param < TUNER_FREQ_MIN/1000 || param > TUNER_FREQ_MAX / 1000)
1137 return -EINVAL;
1138
1139 mutex_lock(&pd->lock);
1140 ret = send_set_req(pd, TUNE_FREQ_SELECT, param, &cmd_status);
1141 ret = send_set_req(pd, TAKE_REQUEST, 0, &cmd_status);
1142
1143 msleep(250); /* wait for a while until the hardware is ready. */
1144 context->freq = frequency;
1145 mutex_unlock(&pd->lock);
1146 return ret;
1147}
1148
1149static int vidioc_s_frequency(struct file *file, void *fh,
1150 struct v4l2_frequency *freq)
1151{
1152 struct front_face *front = fh;
1153 struct poseidon *pd = front->pd;
1154
1155 logs(front);
1156#ifdef CONFIG_PM
1157 pd->pm_suspend = pm_video_suspend;
1158 pd->pm_resume = pm_video_resume;
1159#endif
1160 return set_frequency(pd, freq->frequency);
1161}
1162
1163static int vidioc_reqbufs(struct file *file, void *fh,
1164 struct v4l2_requestbuffers *b)
1165{
1166 struct front_face *front = file->private_data;
1167 logs(front);
1168 return videobuf_reqbufs(&front->q, b);
1169}
1170
1171static int vidioc_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
1172{
1173 struct front_face *front = file->private_data;
1174 logs(front);
1175 return videobuf_querybuf(&front->q, b);
1176}
1177
1178static int vidioc_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
1179{
1180 struct front_face *front = file->private_data;
1181 return videobuf_qbuf(&front->q, b);
1182}
1183
1184static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
1185{
1186 struct front_face *front = file->private_data;
1187 return videobuf_dqbuf(&front->q, b, file->f_flags & O_NONBLOCK);
1188}
1189
1190/* Just stop the URBs, do not free the URBs */
1191int usb_transfer_stop(struct video_data *video)
1192{
1193 if (video->is_streaming) {
1194 int i;
1195 s32 cmd_status;
1196 struct poseidon *pd = video->pd;
1197
1198 video->is_streaming = 0;
1199 for (i = 0; i < SBUF_NUM; ++i) {
1200 if (video->urb_array[i])
1201 usb_kill_urb(video->urb_array[i]);
1202 }
1203
1204 send_set_req(pd, PLAY_SERVICE, TLG_TUNE_PLAY_SVC_STOP,
1205 &cmd_status);
1206 }
1207 return 0;
1208}
1209
1210int stop_all_video_stream(struct poseidon *pd)
1211{
1212 struct video_data *video = &pd->video_data;
1213 struct vbi_data *vbi = &pd->vbi_data;
1214
1215 mutex_lock(&pd->lock);
1216 if (video->is_streaming) {
1217 struct front_face *front = video->front;
1218
1219 /* stop the URBs */
1220 usb_transfer_stop(video);
1221 free_all_urb(video);
1222
1223 /* stop the host side of VIDEO */
1224 videobuf_stop(&front->q);
1225 videobuf_mmap_free(&front->q);
1226
1227 /* stop the host side of VBI */
1228 front = vbi->front;
1229 if (front) {
1230 videobuf_stop(&front->q);
1231 videobuf_mmap_free(&front->q);
1232 }
1233 }
1234 mutex_unlock(&pd->lock);
1235 return 0;
1236}
1237
1238/*
1239 * The bubbles can seriously damage the video's quality,
1240 * though it occurs in very rare situation.
1241 */
1242static void iso_bubble_handler(struct work_struct *w)
1243{
1244 struct video_data *video;
1245 struct poseidon *pd;
1246
1247 video = container_of(w, struct video_data, bubble_work);
1248 pd = video->pd;
1249
1250 mutex_lock(&pd->lock);
1251 usb_transfer_stop(video);
1252 msleep(500);
1253 start_video_stream(pd);
1254 mutex_unlock(&pd->lock);
1255}
1256
1257
1258static int vidioc_streamon(struct file *file, void *fh,
1259 enum v4l2_buf_type type)
1260{
1261 struct front_face *front = fh;
1262
1263 logs(front);
1264 if (unlikely(type != front->type))
1265 return -EINVAL;
1266 return videobuf_streamon(&front->q);
1267}
1268
1269static int vidioc_streamoff(struct file *file, void *fh,
1270 enum v4l2_buf_type type)
1271{
1272 struct front_face *front = file->private_data;
1273
1274 logs(front);
1275 if (unlikely(type != front->type))
1276 return -EINVAL;
1277 return videobuf_streamoff(&front->q);
1278}
1279
1280/* Set the firmware's default values : need altersetting */
1281static int pd_video_checkmode(struct poseidon *pd)
1282{
1283 s32 ret = 0, cmd_status, audiomode;
1284
1285 set_current_state(TASK_INTERRUPTIBLE);
1286 schedule_timeout(HZ/2);
1287
1288 /* choose the altersetting */
1289 ret = usb_set_interface(pd->udev, 0,
1290 (pd->cur_transfer_mode ?
1291 ISO_3K_BULK_ALTERNATE_IFACE :
1292 BULK_ALTERNATE_IFACE));
1293 if (ret < 0)
1294 goto error;
1295
1296 /* set default parameters for PAL-D , with the VBI enabled*/
1297 ret = set_tuner_mode(pd, TLG_MODE_ANALOG_TV);
1298 ret |= send_set_req(pd, SGNL_SRC_SEL,
1299 TLG_SIG_SRC_ANTENNA, &cmd_status);
1300 ret |= send_set_req(pd, VIDEO_STD_SEL,
1301 TLG_TUNE_VSTD_PAL_D, &cmd_status);
1302 ret |= send_set_req(pd, VIDEO_STREAM_FMT_SEL,
1303 TLG_TUNER_VID_FORMAT_YUV, &cmd_status);
1304 ret |= send_set_req(pd, VIDEO_ROSOLU_SEL,
1305 TLG_TUNE_VID_RES_720, &cmd_status);
1306 ret |= send_set_req(pd, TUNE_FREQ_SELECT, TUNER_FREQ_MIN, &cmd_status);
1307 ret |= send_set_req(pd, VBI_DATA_SEL, 1, &cmd_status);/* enable vbi */
1308
1309 /* set the audio */
1310 audiomode = get_audio_std(pd->video_data.context.tvnormid);
1311 ret |= send_set_req(pd, TUNER_AUD_ANA_STD, audiomode, &cmd_status);
1312 ret |= send_set_req(pd, TUNER_AUD_MODE,
1313 TLG_TUNE_TVAUDIO_MODE_STEREO, &cmd_status);
1314 ret |= send_set_req(pd, AUDIO_SAMPLE_RATE_SEL,
1315 ATV_AUDIO_RATE_48K, &cmd_status);
1316error:
1317 return ret;
1318}
1319
1320#ifdef CONFIG_PM
1321static int pm_video_suspend(struct poseidon *pd)
1322{
1323 /* stop audio */
1324 pm_alsa_suspend(pd);
1325
1326 /* stop and free all the URBs */
1327 usb_transfer_stop(&pd->video_data);
1328 free_all_urb(&pd->video_data);
1329
1330 /* reset the interface */
1331 usb_set_interface(pd->udev, 0, 0);
1332 msleep(300);
1333 return 0;
1334}
1335
1336static int restore_v4l2_context(struct poseidon *pd,
1337 struct running_context *context)
1338{
1339 struct front_face *front = pd->video_data.front;
1340
1341 pd_video_checkmode(pd);
1342
1343 set_std(pd, &context->tvnormid);
1344 vidioc_s_input(NULL, front, context->sig_index);
1345 pd_vidioc_s_tuner(pd, context->audio_idx);
1346 pd_vidioc_s_fmt(pd, &context->pix);
1347 set_frequency(pd, context->freq);
1348 return 0;
1349}
1350
1351static int pm_video_resume(struct poseidon *pd)
1352{
1353 struct video_data *video = &pd->video_data;
1354
1355 /* resume the video */
1356 /* [1] restore the origin V4L2 parameters */
1357 restore_v4l2_context(pd, &video->context);
1358
1359 /* [2] initiate video copy variables */
1360 if (video->front->curr_frame)
1361 init_copy(video, 0);
1362
1363 /* [3] fire urbs */
1364 start_video_stream(pd);
1365
1366 /* resume the audio */
1367 pm_alsa_resume(pd);
1368 return 0;
1369}
1370#endif
1371
1372void set_debug_mode(struct video_device *vfd, int debug_mode)
1373{
1374 vfd->debug = 0;
1375 if (debug_mode & 0x1)
1376 vfd->debug = V4L2_DEBUG_IOCTL;
1377 if (debug_mode & 0x2)
1378 vfd->debug = V4L2_DEBUG_IOCTL | V4L2_DEBUG_IOCTL_ARG;
1379}
1380
1381static void init_video_context(struct running_context *context)
1382{
1383 context->sig_index = 0;
1384 context->audio_idx = 1; /* stereo */
1385 context->tvnormid = V4L2_STD_PAL_D;
1386 context->pix = (struct v4l2_pix_format) {
1387 .width = 720,
1388 .height = 576,
1389 .pixelformat = V4L2_PIX_FMT_YUYV,
1390 .field = V4L2_FIELD_INTERLACED,
1391 .bytesperline = 720 * 2,
1392 .sizeimage = 720 * 576 * 2,
1393 .colorspace = V4L2_COLORSPACE_SMPTE170M,
1394 .priv = 0
1395 };
1396}
1397
1398static int pd_video_open(struct file *file)
1399{
1400 struct video_device *vfd = video_devdata(file);
1401 struct poseidon *pd = video_get_drvdata(vfd);
1402 struct front_face *front = NULL;
1403 int ret = -ENOMEM;
1404
1405 mutex_lock(&pd->lock);
1406 usb_autopm_get_interface(pd->interface);
1407
1408 if (vfd->vfl_type == VFL_TYPE_GRABBER
1409 && !(pd->state & POSEIDON_STATE_ANALOG)) {
1410 front = kzalloc(sizeof(struct front_face), GFP_KERNEL);
1411 if (!front)
1412 goto out;
1413
1414 pd->cur_transfer_mode = usb_transfer_mode;/* bulk or iso */
1415 init_video_context(&pd->video_data.context);
1416
1417 ret = pd_video_checkmode(pd);
1418 if (ret < 0) {
1419 kfree(front);
1420 ret = -1;
1421 goto out;
1422 }
1423
1424 pd->state |= POSEIDON_STATE_ANALOG;
1425 front->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1426 pd->video_data.users++;
1427 set_debug_mode(vfd, debug_mode);
1428
1429 videobuf_queue_vmalloc_init(&front->q, &pd_video_qops,
1430 NULL, &front->queue_lock,
1431 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1432 V4L2_FIELD_INTERLACED,/* video is interlacd */
1433 sizeof(struct videobuf_buffer),/*it's enough*/
1434 front);
1435 } else if (vfd->vfl_type == VFL_TYPE_VBI
1436 && !(pd->state & POSEIDON_STATE_VBI)) {
1437 front = kzalloc(sizeof(struct front_face), GFP_KERNEL);
1438 if (!front)
1439 goto out;
1440
1441 pd->state |= POSEIDON_STATE_VBI;
1442 front->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1443 pd->vbi_data.front = front;
1444 pd->vbi_data.users++;
1445
1446 videobuf_queue_vmalloc_init(&front->q, &pd_video_qops,
1447 NULL, &front->queue_lock,
1448 V4L2_BUF_TYPE_VBI_CAPTURE,
1449 V4L2_FIELD_NONE, /* vbi is NONE mode */
1450 sizeof(struct videobuf_buffer),
1451 front);
1452 } else {
1453 /* maybe add FM support here */
1454 log("other ");
1455 ret = -EINVAL;
1456 goto out;
1457 }
1458
1459 front->pd = pd;
1460 front->curr_frame = NULL;
1461 INIT_LIST_HEAD(&front->active);
1462 spin_lock_init(&front->queue_lock);
1463
1464 file->private_data = front;
1465 kref_get(&pd->kref);
1466
1467 mutex_unlock(&pd->lock);
1468 return 0;
1469out:
1470 usb_autopm_put_interface(pd->interface);
1471 mutex_unlock(&pd->lock);
1472 return ret;
1473}
1474
1475static int pd_video_release(struct file *file)
1476{
1477 struct front_face *front = file->private_data;
1478 struct poseidon *pd = front->pd;
1479 s32 cmd_status = 0;
1480
1481 logs(front);
1482 mutex_lock(&pd->lock);
1483
1484 if (front->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1485 pd->state &= ~POSEIDON_STATE_ANALOG;
1486
1487 /* stop the device, and free the URBs */
1488 usb_transfer_stop(&pd->video_data);
1489 free_all_urb(&pd->video_data);
1490
1491 /* stop the firmware */
1492 send_set_req(pd, PLAY_SERVICE, TLG_TUNE_PLAY_SVC_STOP,
1493 &cmd_status);
1494
1495 pd->file_for_stream = NULL;
1496 pd->video_data.users--;
1497 } else if (front->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1498 pd->state &= ~POSEIDON_STATE_VBI;
1499 pd->vbi_data.front = NULL;
1500 pd->vbi_data.users--;
1501 }
1502 videobuf_stop(&front->q);
1503 videobuf_mmap_free(&front->q);
1504
1505 usb_autopm_put_interface(pd->interface);
1506 mutex_unlock(&pd->lock);
1507
1508 kfree(front);
1509 file->private_data = NULL;
1510 kref_put(&pd->kref, poseidon_delete);
1511 return 0;
1512}
1513
1514static int pd_video_mmap(struct file *file, struct vm_area_struct *vma)
1515{
1516 struct front_face *front = file->private_data;
1517 return videobuf_mmap_mapper(&front->q, vma);
1518}
1519
1520unsigned int pd_video_poll(struct file *file, poll_table *table)
1521{
1522 struct front_face *front = file->private_data;
1523 return videobuf_poll_stream(file, &front->q, table);
1524}
1525
1526ssize_t pd_video_read(struct file *file, char __user *buffer,
1527 size_t count, loff_t *ppos)
1528{
1529 struct front_face *front = file->private_data;
1530 return videobuf_read_stream(&front->q, buffer, count, ppos,
1531 0, file->f_flags & O_NONBLOCK);
1532}
1533
1534/* This struct works for both VIDEO and VBI */
1535static const struct v4l2_file_operations pd_video_fops = {
1536 .owner = THIS_MODULE,
1537 .open = pd_video_open,
1538 .release = pd_video_release,
1539 .read = pd_video_read,
1540 .poll = pd_video_poll,
1541 .mmap = pd_video_mmap,
1542 .ioctl = video_ioctl2, /* maybe changed in future */
1543};
1544
1545static const struct v4l2_ioctl_ops pd_video_ioctl_ops = {
1546 .vidioc_querycap = vidioc_querycap,
1547
1548 /* Video format */
1549 .vidioc_g_fmt_vid_cap = vidioc_g_fmt,
1550 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt,
1551 .vidioc_s_fmt_vid_cap = vidioc_s_fmt,
1552 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi, /* VBI */
1553 .vidioc_try_fmt_vid_cap = vidioc_try_fmt,
1554
1555 /* Input */
1556 .vidioc_g_input = vidioc_g_input,
1557 .vidioc_s_input = vidioc_s_input,
1558 .vidioc_enum_input = vidioc_enum_input,
1559
1560 /* Audio ioctls */
1561 .vidioc_enumaudio = vidioc_enumaudio,
1562 .vidioc_g_audio = vidioc_g_audio,
1563 .vidioc_s_audio = vidioc_s_audio,
1564
1565 /* Tuner ioctls */
1566 .vidioc_g_tuner = vidioc_g_tuner,
1567 .vidioc_s_tuner = vidioc_s_tuner,
1568 .vidioc_s_std = vidioc_s_std,
1569 .vidioc_g_frequency = vidioc_g_frequency,
1570 .vidioc_s_frequency = vidioc_s_frequency,
1571
1572 /* Buffer handlers */
1573 .vidioc_reqbufs = vidioc_reqbufs,
1574 .vidioc_querybuf = vidioc_querybuf,
1575 .vidioc_qbuf = vidioc_qbuf,
1576 .vidioc_dqbuf = vidioc_dqbuf,
1577
1578 /* Stream on/off */
1579 .vidioc_streamon = vidioc_streamon,
1580 .vidioc_streamoff = vidioc_streamoff,
1581
1582 /* Control handling */
1583 .vidioc_queryctrl = vidioc_queryctrl,
1584 .vidioc_g_ctrl = vidioc_g_ctrl,
1585 .vidioc_s_ctrl = vidioc_s_ctrl,
1586};
1587
1588static struct video_device pd_video_template = {
1589 .name = "Telegent-Video",
1590 .fops = &pd_video_fops,
1591 .minor = -1,
1592 .release = video_device_release,
1593 .tvnorms = V4L2_STD_ALL,
1594 .ioctl_ops = &pd_video_ioctl_ops,
1595};
1596
1597struct video_device *vdev_init(struct poseidon *pd, struct video_device *tmp)
1598{
1599 struct video_device *vfd;
1600
1601 vfd = video_device_alloc();
1602 if (vfd == NULL)
1603 return NULL;
1604 *vfd = *tmp;
1605 vfd->minor = -1;
1606 vfd->v4l2_dev = &pd->v4l2_dev;
1607 /*vfd->parent = &(pd->udev->dev); */
1608 vfd->release = video_device_release;
1609 video_set_drvdata(vfd, pd);
1610 return vfd;
1611}
1612
1613void destroy_video_device(struct video_device **v_dev)
1614{
1615 struct video_device *dev = *v_dev;
1616
1617 if (dev == NULL)
1618 return;
1619
1620 if (video_is_registered(dev))
1621 video_unregister_device(dev);
1622 else
1623 video_device_release(dev);
1624 *v_dev = NULL;
1625}
1626
1627void pd_video_exit(struct poseidon *pd)
1628{
1629 struct video_data *video = &pd->video_data;
1630 struct vbi_data *vbi = &pd->vbi_data;
1631
1632 destroy_video_device(&video->v_dev);
1633 destroy_video_device(&vbi->v_dev);
1634 log();
1635}
1636
1637int pd_video_init(struct poseidon *pd)
1638{
1639 struct video_data *video = &pd->video_data;
1640 struct vbi_data *vbi = &pd->vbi_data;
1641 int ret = -ENOMEM;
1642
1643 video->v_dev = vdev_init(pd, &pd_video_template);
1644 if (video->v_dev == NULL)
1645 goto out;
1646
1647 ret = video_register_device(video->v_dev, VFL_TYPE_GRABBER, -1);
1648 if (ret != 0)
1649 goto out;
1650
1651 /* VBI uses the same template as video */
1652 vbi->v_dev = vdev_init(pd, &pd_video_template);
1653 if (vbi->v_dev == NULL) {
1654 ret = -ENOMEM;
1655 goto out;
1656 }
1657 ret = video_register_device(vbi->v_dev, VFL_TYPE_VBI, -1);
1658 if (ret != 0)
1659 goto out;
1660 log("register VIDEO/VBI devices");
1661 return 0;
1662out:
1663 log("VIDEO/VBI devices register failed, : %d", ret);
1664 pd_video_exit(pd);
1665 return ret;
1666}
1667
diff --git a/drivers/media/video/tlg2300/vendorcmds.h b/drivers/media/video/tlg2300/vendorcmds.h
new file mode 100644
index 000000000000..ba6f4ae3b2c2
--- /dev/null
+++ b/drivers/media/video/tlg2300/vendorcmds.h
@@ -0,0 +1,243 @@
1#ifndef VENDOR_CMD_H_
2#define VENDOR_CMD_H_
3
4#define BULK_ALTERNATE_IFACE (2)
5#define ISO_3K_BULK_ALTERNATE_IFACE (1)
6#define REQ_SET_CMD (0X00)
7#define REQ_GET_CMD (0X80)
8
9enum tlg__analog_audio_standard {
10 TLG_TUNE_ASTD_NONE = 0x00000000,
11 TLG_TUNE_ASTD_A2 = 0x00000001,
12 TLG_TUNE_ASTD_NICAM = 0x00000002,
13 TLG_TUNE_ASTD_EIAJ = 0x00000004,
14 TLG_TUNE_ASTD_BTSC = 0x00000008,
15 TLG_TUNE_ASTD_FM_US = 0x00000010,
16 TLG_TUNE_ASTD_FM_EUR = 0x00000020,
17 TLG_TUNE_ASTD_ALL = 0x0000003f
18};
19
20/*
21 * identifiers for Custom Parameter messages.
22 * @typedef cmd_custom_param_id_t
23 */
24enum cmd_custom_param_id {
25 CUST_PARM_ID_NONE = 0x00,
26 CUST_PARM_ID_BRIGHTNESS_CTRL = 0x01,
27 CUST_PARM_ID_CONTRAST_CTRL = 0x02,
28 CUST_PARM_ID_HUE_CTRL = 0x03,
29 CUST_PARM_ID_SATURATION_CTRL = 0x04,
30 CUST_PARM_ID_AUDIO_SNR_THRESHOLD = 0x10,
31 CUST_PARM_ID_AUDIO_AGC_THRESHOLD = 0x11,
32 CUST_PARM_ID_MAX
33};
34
35struct tuner_custom_parameter_s {
36 uint16_t param_id; /* Parameter identifier */
37 uint16_t param_value; /* Parameter value */
38};
39
40struct tuner_ber_rate_s {
41 uint32_t ber_rate; /* BER sample rate in seconds */
42};
43
44struct tuner_atv_sig_stat_s {
45 uint32_t sig_present;
46 uint32_t sig_locked;
47 uint32_t sig_lock_busy;
48 uint32_t sig_strength; /* milliDb */
49 uint32_t tv_audio_chan; /* mono/stereo/sap*/
50 uint32_t mvision_stat; /* macrovision status */
51};
52
53struct tuner_dtv_sig_stat_s {
54 uint32_t sig_present; /* Boolean*/
55 uint32_t sig_locked; /* Boolean */
56 uint32_t sig_lock_busy; /* Boolean (Can this time-out?) */
57 uint32_t sig_strength; /* milliDb*/
58};
59
60struct tuner_fm_sig_stat_s {
61 uint32_t sig_present; /* Boolean*/
62 uint32_t sig_locked; /* Boolean */
63 uint32_t sig_lock_busy; /* Boolean */
64 uint32_t sig_stereo_mono;/* TBD*/
65 uint32_t sig_strength; /* milliDb*/
66};
67
68enum _tag_tlg_tune_srv_cmd {
69 TLG_TUNE_PLAY_SVC_START = 1,
70 TLG_TUNE_PLAY_SVC_STOP
71};
72
73enum _tag_tune_atv_audio_mode_caps {
74 TLG_TUNE_TVAUDIO_MODE_MONO = 0x00000001,
75 TLG_TUNE_TVAUDIO_MODE_STEREO = 0x00000002,
76 TLG_TUNE_TVAUDIO_MODE_LANG_A = 0x00000010,/* Primary language*/
77 TLG_TUNE_TVAUDIO_MODE_LANG_B = 0x00000020,/* 2nd avail language*/
78 TLG_TUNE_TVAUDIO_MODE_LANG_C = 0x00000040
79};
80
81
82enum _tag_tuner_atv_audio_rates {
83 ATV_AUDIO_RATE_NONE = 0x00,/* Audio not supported*/
84 ATV_AUDIO_RATE_32K = 0x01,/* Audio rate = 32 KHz*/
85 ATV_AUDIO_RATE_48K = 0x02, /* Audio rate = 48 KHz*/
86 ATV_AUDIO_RATE_31_25K = 0x04 /* Audio rate = 31.25KHz */
87};
88
89enum _tag_tune_atv_vid_res_caps {
90 TLG_TUNE_VID_RES_NONE = 0x00000000,
91 TLG_TUNE_VID_RES_720 = 0x00000001,
92 TLG_TUNE_VID_RES_704 = 0x00000002,
93 TLG_TUNE_VID_RES_360 = 0x00000004
94};
95
96enum _tag_tuner_analog_video_format {
97 TLG_TUNER_VID_FORMAT_YUV = 0x00000001,
98 TLG_TUNER_VID_FORMAT_YCRCB = 0x00000002,
99 TLG_TUNER_VID_FORMAT_RGB_565 = 0x00000004,
100};
101
102enum tlg_ext_audio_support {
103 TLG_EXT_AUDIO_NONE = 0x00,/* No external audio input supported */
104 TLG_EXT_AUDIO_LR = 0x01/* LR external audio inputs supported*/
105};
106
107enum {
108 TLG_MODE_NONE = 0x00, /* No Mode specified*/
109 TLG_MODE_ANALOG_TV = 0x01, /* Analog Television mode*/
110 TLG_MODE_ANALOG_TV_UNCOMP = 0x01, /* Analog Television mode*/
111 TLG_MODE_ANALOG_TV_COMP = 0x02, /* Analog TV mode (compressed)*/
112 TLG_MODE_FM_RADIO = 0x04, /* FM Radio mode*/
113 TLG_MODE_DVB_T = 0x08, /* Digital TV (DVB-T)*/
114};
115
116enum tlg_signal_sources_t {
117 TLG_SIG_SRC_NONE = 0x00,/* Signal source not specified */
118 TLG_SIG_SRC_ANTENNA = 0x01,/* Signal src is: Antenna */
119 TLG_SIG_SRC_CABLE = 0x02,/* Signal src is: Coax Cable*/
120 TLG_SIG_SRC_SVIDEO = 0x04,/* Signal src is: S_VIDEO */
121 TLG_SIG_SRC_COMPOSITE = 0x08 /* Signal src is: Composite Video */
122};
123
124enum tuner_analog_video_standard {
125 TLG_TUNE_VSTD_NONE = 0x00000000,
126 TLG_TUNE_VSTD_NTSC_M = 0x00000001,
127 TLG_TUNE_VSTD_NTSC_M_J = 0x00000002,/* Japan */
128 TLG_TUNE_VSTD_PAL_B = 0x00000010,
129 TLG_TUNE_VSTD_PAL_D = 0x00000020,
130 TLG_TUNE_VSTD_PAL_G = 0x00000040,
131 TLG_TUNE_VSTD_PAL_H = 0x00000080,
132 TLG_TUNE_VSTD_PAL_I = 0x00000100,
133 TLG_TUNE_VSTD_PAL_M = 0x00000200,
134 TLG_TUNE_VSTD_PAL_N = 0x00000400,
135 TLG_TUNE_VSTD_SECAM_B = 0x00001000,
136 TLG_TUNE_VSTD_SECAM_D = 0x00002000,
137 TLG_TUNE_VSTD_SECAM_G = 0x00004000,
138 TLG_TUNE_VSTD_SECAM_H = 0x00008000,
139 TLG_TUNE_VSTD_SECAM_K = 0x00010000,
140 TLG_TUNE_VSTD_SECAM_K1 = 0x00020000,
141 TLG_TUNE_VSTD_SECAM_L = 0x00040000,
142 TLG_TUNE_VSTD_SECAM_L1 = 0x00080000,
143 TLG_TUNE_VSTD_PAL_N_COMBO = 0x00100000
144};
145
146enum tlg_mode_caps {
147 TLG_MODE_CAPS_NONE = 0x00, /* No Mode specified */
148 TLG_MODE_CAPS_ANALOG_TV_UNCOMP = 0x01, /* Analog TV mode */
149 TLG_MODE_CAPS_ANALOG_TV_COMP = 0x02, /* Analog TV (compressed)*/
150 TLG_MODE_CAPS_FM_RADIO = 0x04, /* FM Radio mode */
151 TLG_MODE_CAPS_DVB_T = 0x08, /* Digital TV (DVB-T) */
152};
153
154enum poseidon_vendor_cmds {
155 LAST_CMD_STAT = 0x00,
156 GET_CHIP_ID = 0x01,
157 GET_FW_ID = 0x02,
158 PRODUCT_CAPS = 0x03,
159
160 TUNE_MODE_CAP_ATV = 0x10,
161 TUNE_MODE_CAP_ATVCOMP = 0X10,
162 TUNE_MODE_CAP_DVBT = 0x10,
163 TUNE_MODE_CAP_FM = 0x10,
164 TUNE_MODE_SELECT = 0x11,
165 TUNE_FREQ_SELECT = 0x12,
166 SGNL_SRC_SEL = 0x13,
167
168 VIDEO_STD_SEL = 0x14,
169 VIDEO_STREAM_FMT_SEL = 0x15,
170 VIDEO_ROSOLU_AVAIL = 0x16,
171 VIDEO_ROSOLU_SEL = 0x17,
172 VIDEO_CONT_PROTECT = 0x20,
173
174 VCR_TIMING_MODSEL = 0x21,
175 EXT_AUDIO_CAP = 0x22,
176 EXT_AUDIO_SEL = 0x23,
177 TEST_PATTERN_SEL = 0x24,
178 VBI_DATA_SEL = 0x25,
179 AUDIO_SAMPLE_RATE_CAP = 0x28,
180 AUDIO_SAMPLE_RATE_SEL = 0x29,
181 TUNER_AUD_MODE = 0x2a,
182 TUNER_AUD_MODE_AVAIL = 0x2b,
183 TUNER_AUD_ANA_STD = 0x2c,
184 TUNER_CUSTOM_PARAMETER = 0x2f,
185
186 DVBT_TUNE_MODE_SEL = 0x30,
187 DVBT_BANDW_CAP = 0x31,
188 DVBT_BANDW_SEL = 0x32,
189 DVBT_GUARD_INTERV_CAP = 0x33,
190 DVBT_GUARD_INTERV_SEL = 0x34,
191 DVBT_MODULATION_CAP = 0x35,
192 DVBT_MODULATION_SEL = 0x36,
193 DVBT_INNER_FEC_RATE_CAP = 0x37,
194 DVBT_INNER_FEC_RATE_SEL = 0x38,
195 DVBT_TRANS_MODE_CAP = 0x39,
196 DVBT_TRANS_MODE_SEL = 0x3a,
197 DVBT_SEARCH_RANG = 0x3c,
198
199 TUNER_SETUP_ANALOG = 0x40,
200 TUNER_SETUP_DIGITAL = 0x41,
201 TUNER_SETUP_FM_RADIO = 0x42,
202 TAKE_REQUEST = 0x43, /* Take effect of the command */
203 PLAY_SERVICE = 0x44, /* Play start or Play stop */
204 TUNER_STATUS = 0x45,
205 TUNE_PROP_DVBT = 0x46,
206 ERR_RATE_STATS = 0x47,
207 TUNER_BER_RATE = 0x48,
208
209 SCAN_CAPS = 0x50,
210 SCAN_SETUP = 0x51,
211 SCAN_SERVICE = 0x52,
212 SCAN_STATS = 0x53,
213
214 PID_SET = 0x58,
215 PID_UNSET = 0x59,
216 PID_LIST = 0x5a,
217
218 IRD_CAP = 0x60,
219 IRD_MODE_SEL = 0x61,
220 IRD_SETUP = 0x62,
221
222 PTM_MODE_CAP = 0x70,
223 PTM_MODE_SEL = 0x71,
224 PTM_SERVICE = 0x72,
225 TUNER_REG_SCRIPT = 0x73,
226 CMD_CHIP_RST = 0x74,
227};
228
229enum tlg_bw {
230 TLG_BW_5 = 5,
231 TLG_BW_6 = 6,
232 TLG_BW_7 = 7,
233 TLG_BW_8 = 8,
234 TLG_BW_12 = 12,
235 TLG_BW_15 = 15
236};
237
238struct cmd_firmware_vers_s {
239 uint8_t fw_rev_major;
240 uint8_t fw_rev_minor;
241 uint16_t fw_patch;
242};
243#endif /* VENDOR_CMD_H_ */