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