aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video/pvrusb2
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media/video/pvrusb2')
-rw-r--r--drivers/media/video/pvrusb2/Kconfig66
-rw-r--r--drivers/media/video/pvrusb2/Makefile22
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-audio.c96
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-audio.h37
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-context.c431
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-context.h94
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-cs53l32a.c95
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-cs53l32a.h48
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-ctrl.c609
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-ctrl.h122
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-cx2584x-v4l.c166
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-cx2584x-v4l.h52
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-debug.h69
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-debugifc.c335
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-debugifc.h52
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-devattr.c558
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-devattr.h199
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-dvb.c435
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-dvb.h41
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-eeprom.c164
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-eeprom.h39
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-encoder.c552
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-encoder.h42
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-fx2-cmd.h72
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-hdw-internal.h408
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-hdw.c5322
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-hdw.h364
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-i2c-core.c697
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-i2c-core.h40
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-io.c695
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-io.h102
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-ioread.c512
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-ioread.h48
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-main.c182
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-std.c411
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-std.h59
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-sysfs.c861
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-sysfs.h46
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-util.h62
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-v4l2.c1356
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-v4l2.h39
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-video-v4l.c114
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-video-v4l.h48
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-wm8775.c70
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-wm8775.h52
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2.h42
46 files changed, 15926 insertions, 0 deletions
diff --git a/drivers/media/video/pvrusb2/Kconfig b/drivers/media/video/pvrusb2/Kconfig
new file mode 100644
index 00000000000..f9b6001e1dd
--- /dev/null
+++ b/drivers/media/video/pvrusb2/Kconfig
@@ -0,0 +1,66 @@
1config VIDEO_PVRUSB2
2 tristate "Hauppauge WinTV-PVR USB2 support"
3 depends on VIDEO_V4L2 && I2C
4 depends on VIDEO_MEDIA # Avoids pvrusb = Y / DVB = M
5 select VIDEO_TUNER
6 select VIDEO_TVEEPROM
7 select VIDEO_CX2341X
8 select VIDEO_SAA711X
9 select VIDEO_CX25840
10 select VIDEO_MSP3400
11 select VIDEO_WM8775
12 select VIDEO_CS53L32A
13 ---help---
14 This is a video4linux driver for Conexant 23416 based
15 usb2 personal video recorder devices.
16
17 To compile this driver as a module, choose M here: the
18 module will be called pvrusb2
19
20config VIDEO_PVRUSB2_SYSFS
21 bool "pvrusb2 sysfs support (EXPERIMENTAL)"
22 default y
23 depends on VIDEO_PVRUSB2 && SYSFS && EXPERIMENTAL
24 ---help---
25 This option enables the operation of a sysfs based
26 interface for query and control of the pvrusb2 driver.
27
28 This is not generally needed for v4l applications,
29 although certain applications are optimized to take
30 advantage of this feature.
31
32 If you are in doubt, say Y.
33
34 Note: This feature is experimental and subject to change.
35
36config VIDEO_PVRUSB2_DVB
37 bool "pvrusb2 ATSC/DVB support (EXPERIMENTAL)"
38 default y
39 depends on VIDEO_PVRUSB2 && DVB_CORE && EXPERIMENTAL
40 select DVB_LGDT330X if !DVB_FE_CUSTOMISE
41 select DVB_S5H1409 if !DVB_FE_CUSTOMISE
42 select DVB_S5H1411 if !DVB_FE_CUSTOMISE
43 select DVB_TDA10048 if !DVB_FE_CUSTOMISE
44 select MEDIA_TUNER_TDA18271 if !MEDIA_TUNER_CUSTOMISE
45 select MEDIA_TUNER_SIMPLE if !MEDIA_TUNER_CUSTOMISE
46 select MEDIA_TUNER_TDA8290 if !MEDIA_TUNER_CUSTOMISE
47 ---help---
48
49 This option enables a DVB interface for the pvrusb2 driver.
50 If your device does not support digital television, this
51 feature will have no affect on the driver's operation.
52
53 If you are in doubt, say Y.
54
55config VIDEO_PVRUSB2_DEBUGIFC
56 bool "pvrusb2 debug interface"
57 depends on VIDEO_PVRUSB2_SYSFS
58 ---help---
59 This option enables the inclusion of a debug interface
60 in the pvrusb2 driver, hosted through sysfs.
61
62 You do not need to select this option unless you plan
63 on debugging the driver or performing a manual firmware
64 extraction.
65
66 If you are in doubt, say N.
diff --git a/drivers/media/video/pvrusb2/Makefile b/drivers/media/video/pvrusb2/Makefile
new file mode 100644
index 00000000000..de2fc14f043
--- /dev/null
+++ b/drivers/media/video/pvrusb2/Makefile
@@ -0,0 +1,22 @@
1obj-pvrusb2-sysfs-$(CONFIG_VIDEO_PVRUSB2_SYSFS) := pvrusb2-sysfs.o
2obj-pvrusb2-debugifc-$(CONFIG_VIDEO_PVRUSB2_DEBUGIFC) := pvrusb2-debugifc.o
3obj-pvrusb2-dvb-$(CONFIG_VIDEO_PVRUSB2_DVB) := pvrusb2-dvb.o
4
5pvrusb2-objs := pvrusb2-i2c-core.o \
6 pvrusb2-audio.o \
7 pvrusb2-encoder.o pvrusb2-video-v4l.o \
8 pvrusb2-eeprom.o \
9 pvrusb2-main.o pvrusb2-hdw.o pvrusb2-v4l2.o \
10 pvrusb2-ctrl.o pvrusb2-std.o pvrusb2-devattr.o \
11 pvrusb2-context.o pvrusb2-io.o pvrusb2-ioread.o \
12 pvrusb2-cx2584x-v4l.o pvrusb2-wm8775.o \
13 pvrusb2-cs53l32a.o \
14 $(obj-pvrusb2-dvb-y) \
15 $(obj-pvrusb2-sysfs-y) $(obj-pvrusb2-debugifc-y)
16
17obj-$(CONFIG_VIDEO_PVRUSB2) += pvrusb2.o
18
19EXTRA_CFLAGS += -Idrivers/media/video
20EXTRA_CFLAGS += -Idrivers/media/common/tuners
21EXTRA_CFLAGS += -Idrivers/media/dvb/dvb-core
22EXTRA_CFLAGS += -Idrivers/media/dvb/frontends
diff --git a/drivers/media/video/pvrusb2/pvrusb2-audio.c b/drivers/media/video/pvrusb2/pvrusb2-audio.c
new file mode 100644
index 00000000000..cc06d5e4adc
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-audio.c
@@ -0,0 +1,96 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include "pvrusb2-audio.h"
23#include "pvrusb2-hdw-internal.h"
24#include "pvrusb2-debug.h"
25#include <linux/videodev2.h>
26#include <media/msp3400.h>
27#include <media/v4l2-common.h>
28
29
30struct routing_scheme {
31 const int *def;
32 unsigned int cnt;
33};
34
35static const int routing_scheme0[] = {
36 [PVR2_CVAL_INPUT_TV] = MSP_INPUT_DEFAULT,
37 [PVR2_CVAL_INPUT_RADIO] = MSP_INPUT(MSP_IN_SCART2,
38 MSP_IN_TUNER1,
39 MSP_DSP_IN_SCART,
40 MSP_DSP_IN_SCART),
41 [PVR2_CVAL_INPUT_COMPOSITE] = MSP_INPUT(MSP_IN_SCART1,
42 MSP_IN_TUNER1,
43 MSP_DSP_IN_SCART,
44 MSP_DSP_IN_SCART),
45 [PVR2_CVAL_INPUT_SVIDEO] = MSP_INPUT(MSP_IN_SCART1,
46 MSP_IN_TUNER1,
47 MSP_DSP_IN_SCART,
48 MSP_DSP_IN_SCART),
49};
50
51static const struct routing_scheme routing_def0 = {
52 .def = routing_scheme0,
53 .cnt = ARRAY_SIZE(routing_scheme0),
54};
55
56static const struct routing_scheme *routing_schemes[] = {
57 [PVR2_ROUTING_SCHEME_HAUPPAUGE] = &routing_def0,
58};
59
60void pvr2_msp3400_subdev_update(struct pvr2_hdw *hdw, struct v4l2_subdev *sd)
61{
62 if (hdw->input_dirty || hdw->force_dirty) {
63 const struct routing_scheme *sp;
64 unsigned int sid = hdw->hdw_desc->signal_routing_scheme;
65 u32 input;
66
67 pvr2_trace(PVR2_TRACE_CHIPS, "subdev msp3400 v4l2 set_stereo");
68 sp = (sid < ARRAY_SIZE(routing_schemes)) ?
69 routing_schemes[sid] : NULL;
70
71 if ((sp != NULL) &&
72 (hdw->input_val >= 0) &&
73 (hdw->input_val < sp->cnt)) {
74 input = sp->def[hdw->input_val];
75 } else {
76 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
77 "*** WARNING *** subdev msp3400 set_input:"
78 " Invalid routing scheme (%u)"
79 " and/or input (%d)",
80 sid, hdw->input_val);
81 return;
82 }
83 sd->ops->audio->s_routing(sd, input,
84 MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
85 }
86}
87
88/*
89 Stuff for Emacs to see, in order to encourage consistent editing style:
90 *** Local Variables: ***
91 *** mode: c ***
92 *** fill-column: 70 ***
93 *** tab-width: 8 ***
94 *** c-basic-offset: 8 ***
95 *** End: ***
96 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-audio.h b/drivers/media/video/pvrusb2/pvrusb2-audio.h
new file mode 100644
index 00000000000..e3e63d75089
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-audio.h
@@ -0,0 +1,37 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#ifndef __PVRUSB2_AUDIO_H
23#define __PVRUSB2_AUDIO_H
24
25#include "pvrusb2-hdw-internal.h"
26void pvr2_msp3400_subdev_update(struct pvr2_hdw *, struct v4l2_subdev *);
27#endif /* __PVRUSB2_AUDIO_H */
28
29/*
30 Stuff for Emacs to see, in order to encourage consistent editing style:
31 *** Local Variables: ***
32 *** mode: c ***
33 *** fill-column: 70 ***
34 *** tab-width: 8 ***
35 *** c-basic-offset: 8 ***
36 *** End: ***
37 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-context.c b/drivers/media/video/pvrusb2/pvrusb2-context.c
new file mode 100644
index 00000000000..7c19ff72e6b
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-context.c
@@ -0,0 +1,431 @@
1/*
2 *
3 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 */
19
20#include "pvrusb2-context.h"
21#include "pvrusb2-io.h"
22#include "pvrusb2-ioread.h"
23#include "pvrusb2-hdw.h"
24#include "pvrusb2-debug.h"
25#include <linux/wait.h>
26#include <linux/kthread.h>
27#include <linux/errno.h>
28#include <linux/string.h>
29#include <linux/slab.h>
30
31static struct pvr2_context *pvr2_context_exist_first;
32static struct pvr2_context *pvr2_context_exist_last;
33static struct pvr2_context *pvr2_context_notify_first;
34static struct pvr2_context *pvr2_context_notify_last;
35static DEFINE_MUTEX(pvr2_context_mutex);
36static DECLARE_WAIT_QUEUE_HEAD(pvr2_context_sync_data);
37static DECLARE_WAIT_QUEUE_HEAD(pvr2_context_cleanup_data);
38static int pvr2_context_cleanup_flag;
39static int pvr2_context_cleaned_flag;
40static struct task_struct *pvr2_context_thread_ptr;
41
42
43static void pvr2_context_set_notify(struct pvr2_context *mp, int fl)
44{
45 int signal_flag = 0;
46 mutex_lock(&pvr2_context_mutex);
47 if (fl) {
48 if (!mp->notify_flag) {
49 signal_flag = (pvr2_context_notify_first == NULL);
50 mp->notify_prev = pvr2_context_notify_last;
51 mp->notify_next = NULL;
52 pvr2_context_notify_last = mp;
53 if (mp->notify_prev) {
54 mp->notify_prev->notify_next = mp;
55 } else {
56 pvr2_context_notify_first = mp;
57 }
58 mp->notify_flag = !0;
59 }
60 } else {
61 if (mp->notify_flag) {
62 mp->notify_flag = 0;
63 if (mp->notify_next) {
64 mp->notify_next->notify_prev = mp->notify_prev;
65 } else {
66 pvr2_context_notify_last = mp->notify_prev;
67 }
68 if (mp->notify_prev) {
69 mp->notify_prev->notify_next = mp->notify_next;
70 } else {
71 pvr2_context_notify_first = mp->notify_next;
72 }
73 }
74 }
75 mutex_unlock(&pvr2_context_mutex);
76 if (signal_flag) wake_up(&pvr2_context_sync_data);
77}
78
79
80static void pvr2_context_destroy(struct pvr2_context *mp)
81{
82 pvr2_trace(PVR2_TRACE_CTXT,"pvr2_context %p (destroy)",mp);
83 if (mp->hdw) pvr2_hdw_destroy(mp->hdw);
84 pvr2_context_set_notify(mp, 0);
85 mutex_lock(&pvr2_context_mutex);
86 if (mp->exist_next) {
87 mp->exist_next->exist_prev = mp->exist_prev;
88 } else {
89 pvr2_context_exist_last = mp->exist_prev;
90 }
91 if (mp->exist_prev) {
92 mp->exist_prev->exist_next = mp->exist_next;
93 } else {
94 pvr2_context_exist_first = mp->exist_next;
95 }
96 if (!pvr2_context_exist_first) {
97 /* Trigger wakeup on control thread in case it is waiting
98 for an exit condition. */
99 wake_up(&pvr2_context_sync_data);
100 }
101 mutex_unlock(&pvr2_context_mutex);
102 kfree(mp);
103}
104
105
106static void pvr2_context_notify(struct pvr2_context *mp)
107{
108 pvr2_context_set_notify(mp,!0);
109}
110
111
112static void pvr2_context_check(struct pvr2_context *mp)
113{
114 struct pvr2_channel *ch1, *ch2;
115 pvr2_trace(PVR2_TRACE_CTXT,
116 "pvr2_context %p (notify)", mp);
117 if (!mp->initialized_flag && !mp->disconnect_flag) {
118 mp->initialized_flag = !0;
119 pvr2_trace(PVR2_TRACE_CTXT,
120 "pvr2_context %p (initialize)", mp);
121 /* Finish hardware initialization */
122 if (pvr2_hdw_initialize(mp->hdw,
123 (void (*)(void *))pvr2_context_notify,
124 mp)) {
125 mp->video_stream.stream =
126 pvr2_hdw_get_video_stream(mp->hdw);
127 /* Trigger interface initialization. By doing this
128 here initialization runs in our own safe and
129 cozy thread context. */
130 if (mp->setup_func) mp->setup_func(mp);
131 } else {
132 pvr2_trace(PVR2_TRACE_CTXT,
133 "pvr2_context %p (thread skipping setup)",
134 mp);
135 /* Even though initialization did not succeed,
136 we're still going to continue anyway. We need
137 to do this in order to await the expected
138 disconnect (which we will detect in the normal
139 course of operation). */
140 }
141 }
142
143 for (ch1 = mp->mc_first; ch1; ch1 = ch2) {
144 ch2 = ch1->mc_next;
145 if (ch1->check_func) ch1->check_func(ch1);
146 }
147
148 if (mp->disconnect_flag && !mp->mc_first) {
149 /* Go away... */
150 pvr2_context_destroy(mp);
151 return;
152 }
153}
154
155
156static int pvr2_context_shutok(void)
157{
158 return pvr2_context_cleanup_flag && (pvr2_context_exist_first == NULL);
159}
160
161
162static int pvr2_context_thread_func(void *foo)
163{
164 struct pvr2_context *mp;
165
166 pvr2_trace(PVR2_TRACE_CTXT,"pvr2_context thread start");
167
168 do {
169 while ((mp = pvr2_context_notify_first) != NULL) {
170 pvr2_context_set_notify(mp, 0);
171 pvr2_context_check(mp);
172 }
173 wait_event_interruptible(
174 pvr2_context_sync_data,
175 ((pvr2_context_notify_first != NULL) ||
176 pvr2_context_shutok()));
177 } while (!pvr2_context_shutok());
178
179 pvr2_context_cleaned_flag = !0;
180 wake_up(&pvr2_context_cleanup_data);
181
182 pvr2_trace(PVR2_TRACE_CTXT,"pvr2_context thread cleaned up");
183
184 wait_event_interruptible(
185 pvr2_context_sync_data,
186 kthread_should_stop());
187
188 pvr2_trace(PVR2_TRACE_CTXT,"pvr2_context thread end");
189
190 return 0;
191}
192
193
194int pvr2_context_global_init(void)
195{
196 pvr2_context_thread_ptr = kthread_run(pvr2_context_thread_func,
197 NULL,
198 "pvrusb2-context");
199 return (pvr2_context_thread_ptr ? 0 : -ENOMEM);
200}
201
202
203void pvr2_context_global_done(void)
204{
205 pvr2_context_cleanup_flag = !0;
206 wake_up(&pvr2_context_sync_data);
207 wait_event_interruptible(
208 pvr2_context_cleanup_data,
209 pvr2_context_cleaned_flag);
210 kthread_stop(pvr2_context_thread_ptr);
211}
212
213
214struct pvr2_context *pvr2_context_create(
215 struct usb_interface *intf,
216 const struct usb_device_id *devid,
217 void (*setup_func)(struct pvr2_context *))
218{
219 struct pvr2_context *mp = NULL;
220 mp = kzalloc(sizeof(*mp),GFP_KERNEL);
221 if (!mp) goto done;
222 pvr2_trace(PVR2_TRACE_CTXT,"pvr2_context %p (create)",mp);
223 mp->setup_func = setup_func;
224 mutex_init(&mp->mutex);
225 mutex_lock(&pvr2_context_mutex);
226 mp->exist_prev = pvr2_context_exist_last;
227 mp->exist_next = NULL;
228 pvr2_context_exist_last = mp;
229 if (mp->exist_prev) {
230 mp->exist_prev->exist_next = mp;
231 } else {
232 pvr2_context_exist_first = mp;
233 }
234 mutex_unlock(&pvr2_context_mutex);
235 mp->hdw = pvr2_hdw_create(intf,devid);
236 if (!mp->hdw) {
237 pvr2_context_destroy(mp);
238 mp = NULL;
239 goto done;
240 }
241 pvr2_context_set_notify(mp, !0);
242 done:
243 return mp;
244}
245
246
247static void pvr2_context_reset_input_limits(struct pvr2_context *mp)
248{
249 unsigned int tmsk,mmsk;
250 struct pvr2_channel *cp;
251 struct pvr2_hdw *hdw = mp->hdw;
252 mmsk = pvr2_hdw_get_input_available(hdw);
253 tmsk = mmsk;
254 for (cp = mp->mc_first; cp; cp = cp->mc_next) {
255 if (!cp->input_mask) continue;
256 tmsk &= cp->input_mask;
257 }
258 pvr2_hdw_set_input_allowed(hdw,mmsk,tmsk);
259 pvr2_hdw_commit_ctl(hdw);
260}
261
262
263static void pvr2_context_enter(struct pvr2_context *mp)
264{
265 mutex_lock(&mp->mutex);
266}
267
268
269static void pvr2_context_exit(struct pvr2_context *mp)
270{
271 int destroy_flag = 0;
272 if (!(mp->mc_first || !mp->disconnect_flag)) {
273 destroy_flag = !0;
274 }
275 mutex_unlock(&mp->mutex);
276 if (destroy_flag) pvr2_context_notify(mp);
277}
278
279
280void pvr2_context_disconnect(struct pvr2_context *mp)
281{
282 pvr2_hdw_disconnect(mp->hdw);
283 mp->disconnect_flag = !0;
284 pvr2_context_notify(mp);
285}
286
287
288void pvr2_channel_init(struct pvr2_channel *cp,struct pvr2_context *mp)
289{
290 pvr2_context_enter(mp);
291 cp->hdw = mp->hdw;
292 cp->mc_head = mp;
293 cp->mc_next = NULL;
294 cp->mc_prev = mp->mc_last;
295 if (mp->mc_last) {
296 mp->mc_last->mc_next = cp;
297 } else {
298 mp->mc_first = cp;
299 }
300 mp->mc_last = cp;
301 pvr2_context_exit(mp);
302}
303
304
305static void pvr2_channel_disclaim_stream(struct pvr2_channel *cp)
306{
307 if (!cp->stream) return;
308 pvr2_stream_kill(cp->stream->stream);
309 cp->stream->user = NULL;
310 cp->stream = NULL;
311}
312
313
314void pvr2_channel_done(struct pvr2_channel *cp)
315{
316 struct pvr2_context *mp = cp->mc_head;
317 pvr2_context_enter(mp);
318 cp->input_mask = 0;
319 pvr2_channel_disclaim_stream(cp);
320 pvr2_context_reset_input_limits(mp);
321 if (cp->mc_next) {
322 cp->mc_next->mc_prev = cp->mc_prev;
323 } else {
324 mp->mc_last = cp->mc_prev;
325 }
326 if (cp->mc_prev) {
327 cp->mc_prev->mc_next = cp->mc_next;
328 } else {
329 mp->mc_first = cp->mc_next;
330 }
331 cp->hdw = NULL;
332 pvr2_context_exit(mp);
333}
334
335
336int pvr2_channel_limit_inputs(struct pvr2_channel *cp,unsigned int cmsk)
337{
338 unsigned int tmsk,mmsk;
339 int ret = 0;
340 struct pvr2_channel *p2;
341 struct pvr2_hdw *hdw = cp->hdw;
342
343 mmsk = pvr2_hdw_get_input_available(hdw);
344 cmsk &= mmsk;
345 if (cmsk == cp->input_mask) {
346 /* No change; nothing to do */
347 return 0;
348 }
349
350 pvr2_context_enter(cp->mc_head);
351 do {
352 if (!cmsk) {
353 cp->input_mask = 0;
354 pvr2_context_reset_input_limits(cp->mc_head);
355 break;
356 }
357 tmsk = mmsk;
358 for (p2 = cp->mc_head->mc_first; p2; p2 = p2->mc_next) {
359 if (p2 == cp) continue;
360 if (!p2->input_mask) continue;
361 tmsk &= p2->input_mask;
362 }
363 if (!(tmsk & cmsk)) {
364 ret = -EPERM;
365 break;
366 }
367 tmsk &= cmsk;
368 if ((ret = pvr2_hdw_set_input_allowed(hdw,mmsk,tmsk)) != 0) {
369 /* Internal failure changing allowed list; probably
370 should not happen, but react if it does. */
371 break;
372 }
373 cp->input_mask = cmsk;
374 pvr2_hdw_commit_ctl(hdw);
375 } while (0);
376 pvr2_context_exit(cp->mc_head);
377 return ret;
378}
379
380
381unsigned int pvr2_channel_get_limited_inputs(struct pvr2_channel *cp)
382{
383 return cp->input_mask;
384}
385
386
387int pvr2_channel_claim_stream(struct pvr2_channel *cp,
388 struct pvr2_context_stream *sp)
389{
390 int code = 0;
391 pvr2_context_enter(cp->mc_head); do {
392 if (sp == cp->stream) break;
393 if (sp && sp->user) {
394 code = -EBUSY;
395 break;
396 }
397 pvr2_channel_disclaim_stream(cp);
398 if (!sp) break;
399 sp->user = cp;
400 cp->stream = sp;
401 } while (0); pvr2_context_exit(cp->mc_head);
402 return code;
403}
404
405
406// This is the marker for the real beginning of a legitimate mpeg2 stream.
407static char stream_sync_key[] = {
408 0x00, 0x00, 0x01, 0xba,
409};
410
411struct pvr2_ioread *pvr2_channel_create_mpeg_stream(
412 struct pvr2_context_stream *sp)
413{
414 struct pvr2_ioread *cp;
415 cp = pvr2_ioread_create();
416 if (!cp) return NULL;
417 pvr2_ioread_setup(cp,sp->stream);
418 pvr2_ioread_set_sync_key(cp,stream_sync_key,sizeof(stream_sync_key));
419 return cp;
420}
421
422
423/*
424 Stuff for Emacs to see, in order to encourage consistent editing style:
425 *** Local Variables: ***
426 *** mode: c ***
427 *** fill-column: 75 ***
428 *** tab-width: 8 ***
429 *** c-basic-offset: 8 ***
430 *** End: ***
431 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-context.h b/drivers/media/video/pvrusb2/pvrusb2-context.h
new file mode 100644
index 00000000000..d657e53bbfa
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-context.h
@@ -0,0 +1,94 @@
1/*
2 *
3 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 */
19#ifndef __PVRUSB2_CONTEXT_H
20#define __PVRUSB2_CONTEXT_H
21
22#include <linux/mutex.h>
23#include <linux/usb.h>
24#include <linux/workqueue.h>
25
26struct pvr2_hdw; /* hardware interface - defined elsewhere */
27struct pvr2_stream; /* stream interface - defined elsewhere */
28
29struct pvr2_context; /* All central state */
30struct pvr2_channel; /* One I/O pathway to a user */
31struct pvr2_context_stream; /* Wrapper for a stream */
32struct pvr2_ioread; /* Low level stream structure */
33
34struct pvr2_context_stream {
35 struct pvr2_channel *user;
36 struct pvr2_stream *stream;
37};
38
39struct pvr2_context {
40 struct pvr2_channel *mc_first;
41 struct pvr2_channel *mc_last;
42 struct pvr2_context *exist_next;
43 struct pvr2_context *exist_prev;
44 struct pvr2_context *notify_next;
45 struct pvr2_context *notify_prev;
46 struct pvr2_hdw *hdw;
47 struct pvr2_context_stream video_stream;
48 struct mutex mutex;
49 int notify_flag;
50 int initialized_flag;
51 int disconnect_flag;
52
53 /* Called after pvr2_context initialization is complete */
54 void (*setup_func)(struct pvr2_context *);
55
56};
57
58struct pvr2_channel {
59 struct pvr2_context *mc_head;
60 struct pvr2_channel *mc_next;
61 struct pvr2_channel *mc_prev;
62 struct pvr2_context_stream *stream;
63 struct pvr2_hdw *hdw;
64 unsigned int input_mask;
65 void (*check_func)(struct pvr2_channel *);
66};
67
68struct pvr2_context *pvr2_context_create(struct usb_interface *intf,
69 const struct usb_device_id *devid,
70 void (*setup_func)(struct pvr2_context *));
71void pvr2_context_disconnect(struct pvr2_context *);
72
73void pvr2_channel_init(struct pvr2_channel *,struct pvr2_context *);
74void pvr2_channel_done(struct pvr2_channel *);
75int pvr2_channel_limit_inputs(struct pvr2_channel *,unsigned int);
76unsigned int pvr2_channel_get_limited_inputs(struct pvr2_channel *);
77int pvr2_channel_claim_stream(struct pvr2_channel *,
78 struct pvr2_context_stream *);
79struct pvr2_ioread *pvr2_channel_create_mpeg_stream(
80 struct pvr2_context_stream *);
81
82int pvr2_context_global_init(void);
83void pvr2_context_global_done(void);
84
85#endif /* __PVRUSB2_CONTEXT_H */
86/*
87 Stuff for Emacs to see, in order to encourage consistent editing style:
88 *** Local Variables: ***
89 *** mode: c ***
90 *** fill-column: 75 ***
91 *** tab-width: 8 ***
92 *** c-basic-offset: 8 ***
93 *** End: ***
94 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-cs53l32a.c b/drivers/media/video/pvrusb2/pvrusb2-cs53l32a.c
new file mode 100644
index 00000000000..88320900dbd
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-cs53l32a.c
@@ -0,0 +1,95 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22/*
23
24 This source file is specifically designed to interface with the
25 v4l-dvb cs53l32a module.
26
27*/
28
29#include "pvrusb2-cs53l32a.h"
30
31
32#include "pvrusb2-hdw-internal.h"
33#include "pvrusb2-debug.h"
34#include <linux/videodev2.h>
35#include <media/v4l2-common.h>
36#include <linux/errno.h>
37
38struct routing_scheme {
39 const int *def;
40 unsigned int cnt;
41};
42
43
44static const int routing_scheme1[] = {
45 [PVR2_CVAL_INPUT_TV] = 2, /* 1 or 2 seems to work here */
46 [PVR2_CVAL_INPUT_RADIO] = 2,
47 [PVR2_CVAL_INPUT_COMPOSITE] = 0,
48 [PVR2_CVAL_INPUT_SVIDEO] = 0,
49};
50
51static const struct routing_scheme routing_def1 = {
52 .def = routing_scheme1,
53 .cnt = ARRAY_SIZE(routing_scheme1),
54};
55
56static const struct routing_scheme *routing_schemes[] = {
57 [PVR2_ROUTING_SCHEME_ONAIR] = &routing_def1,
58};
59
60
61void pvr2_cs53l32a_subdev_update(struct pvr2_hdw *hdw, struct v4l2_subdev *sd)
62{
63 if (hdw->input_dirty || hdw->force_dirty) {
64 const struct routing_scheme *sp;
65 unsigned int sid = hdw->hdw_desc->signal_routing_scheme;
66 u32 input;
67 pvr2_trace(PVR2_TRACE_CHIPS, "subdev v4l2 set_input(%d)",
68 hdw->input_val);
69 sp = (sid < ARRAY_SIZE(routing_schemes)) ?
70 routing_schemes[sid] : NULL;
71 if ((sp == NULL) ||
72 (hdw->input_val < 0) ||
73 (hdw->input_val >= sp->cnt)) {
74 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
75 "*** WARNING *** subdev v4l2 set_input:"
76 " Invalid routing scheme (%u)"
77 " and/or input (%d)",
78 sid, hdw->input_val);
79 return;
80 }
81 input = sp->def[hdw->input_val];
82 sd->ops->audio->s_routing(sd, input, 0, 0);
83 }
84}
85
86
87/*
88 Stuff for Emacs to see, in order to encourage consistent editing style:
89 *** Local Variables: ***
90 *** mode: c ***
91 *** fill-column: 70 ***
92 *** tab-width: 8 ***
93 *** c-basic-offset: 8 ***
94 *** End: ***
95 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-cs53l32a.h b/drivers/media/video/pvrusb2/pvrusb2-cs53l32a.h
new file mode 100644
index 00000000000..53ba548b72a
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-cs53l32a.h
@@ -0,0 +1,48 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#ifndef __PVRUSB2_CS53L32A_H
23#define __PVRUSB2_CS53L32A_H
24
25/*
26
27 This module connects the pvrusb2 driver to the I2C chip level
28 driver which handles device video processing. This interface is
29 used internally by the driver; higher level code should only
30 interact through the interface provided by pvrusb2-hdw.h.
31
32*/
33
34
35#include "pvrusb2-hdw-internal.h"
36void pvr2_cs53l32a_subdev_update(struct pvr2_hdw *, struct v4l2_subdev *);
37
38#endif /* __PVRUSB2_AUDIO_CS53L32A_H */
39
40/*
41 Stuff for Emacs to see, in order to encourage consistent editing style:
42 *** Local Variables: ***
43 *** mode: c ***
44 *** fill-column: 70 ***
45 *** tab-width: 8 ***
46 *** c-basic-offset: 8 ***
47 *** End: ***
48 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-ctrl.c b/drivers/media/video/pvrusb2/pvrusb2-ctrl.c
new file mode 100644
index 00000000000..7d5a7139a45
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-ctrl.c
@@ -0,0 +1,609 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20
21#include "pvrusb2-ctrl.h"
22#include "pvrusb2-hdw-internal.h"
23#include <linux/errno.h>
24#include <linux/string.h>
25#include <linux/mutex.h>
26
27
28static int pvr2_ctrl_range_check(struct pvr2_ctrl *cptr,int val)
29{
30 if (cptr->info->check_value) {
31 if (!cptr->info->check_value(cptr,val)) return -ERANGE;
32 } else if (cptr->info->type == pvr2_ctl_enum) {
33 if (val < 0) return -ERANGE;
34 if (val >= cptr->info->def.type_enum.count) return -ERANGE;
35 } else {
36 int lim;
37 lim = cptr->info->def.type_int.min_value;
38 if (cptr->info->get_min_value) {
39 cptr->info->get_min_value(cptr,&lim);
40 }
41 if (val < lim) return -ERANGE;
42 lim = cptr->info->def.type_int.max_value;
43 if (cptr->info->get_max_value) {
44 cptr->info->get_max_value(cptr,&lim);
45 }
46 if (val > lim) return -ERANGE;
47 }
48 return 0;
49}
50
51
52/* Set the given control. */
53int pvr2_ctrl_set_value(struct pvr2_ctrl *cptr,int val)
54{
55 return pvr2_ctrl_set_mask_value(cptr,~0,val);
56}
57
58
59/* Set/clear specific bits of the given control. */
60int pvr2_ctrl_set_mask_value(struct pvr2_ctrl *cptr,int mask,int val)
61{
62 int ret = 0;
63 if (!cptr) return -EINVAL;
64 LOCK_TAKE(cptr->hdw->big_lock); do {
65 if (cptr->info->set_value) {
66 if (cptr->info->type == pvr2_ctl_bitmask) {
67 mask &= cptr->info->def.type_bitmask.valid_bits;
68 } else if ((cptr->info->type == pvr2_ctl_int)||
69 (cptr->info->type == pvr2_ctl_enum)) {
70 ret = pvr2_ctrl_range_check(cptr,val);
71 if (ret < 0) break;
72 } else if (cptr->info->type != pvr2_ctl_bool) {
73 break;
74 }
75 ret = cptr->info->set_value(cptr,mask,val);
76 } else {
77 ret = -EPERM;
78 }
79 } while(0); LOCK_GIVE(cptr->hdw->big_lock);
80 return ret;
81}
82
83
84/* Get the current value of the given control. */
85int pvr2_ctrl_get_value(struct pvr2_ctrl *cptr,int *valptr)
86{
87 int ret = 0;
88 if (!cptr) return -EINVAL;
89 LOCK_TAKE(cptr->hdw->big_lock); do {
90 ret = cptr->info->get_value(cptr,valptr);
91 } while(0); LOCK_GIVE(cptr->hdw->big_lock);
92 return ret;
93}
94
95
96/* Retrieve control's type */
97enum pvr2_ctl_type pvr2_ctrl_get_type(struct pvr2_ctrl *cptr)
98{
99 if (!cptr) return pvr2_ctl_int;
100 return cptr->info->type;
101}
102
103
104/* Retrieve control's maximum value (int type) */
105int pvr2_ctrl_get_max(struct pvr2_ctrl *cptr)
106{
107 int ret = 0;
108 if (!cptr) return 0;
109 LOCK_TAKE(cptr->hdw->big_lock); do {
110 if (cptr->info->get_max_value) {
111 cptr->info->get_max_value(cptr,&ret);
112 } else if (cptr->info->type == pvr2_ctl_int) {
113 ret = cptr->info->def.type_int.max_value;
114 }
115 } while(0); LOCK_GIVE(cptr->hdw->big_lock);
116 return ret;
117}
118
119
120/* Retrieve control's minimum value (int type) */
121int pvr2_ctrl_get_min(struct pvr2_ctrl *cptr)
122{
123 int ret = 0;
124 if (!cptr) return 0;
125 LOCK_TAKE(cptr->hdw->big_lock); do {
126 if (cptr->info->get_min_value) {
127 cptr->info->get_min_value(cptr,&ret);
128 } else if (cptr->info->type == pvr2_ctl_int) {
129 ret = cptr->info->def.type_int.min_value;
130 }
131 } while(0); LOCK_GIVE(cptr->hdw->big_lock);
132 return ret;
133}
134
135
136/* Retrieve control's default value (any type) */
137int pvr2_ctrl_get_def(struct pvr2_ctrl *cptr, int *valptr)
138{
139 int ret = 0;
140 if (!cptr) return -EINVAL;
141 LOCK_TAKE(cptr->hdw->big_lock); do {
142 if (cptr->info->get_def_value) {
143 ret = cptr->info->get_def_value(cptr, valptr);
144 } else {
145 *valptr = cptr->info->default_value;
146 }
147 } while(0); LOCK_GIVE(cptr->hdw->big_lock);
148 return ret;
149}
150
151
152/* Retrieve control's enumeration count (enum only) */
153int pvr2_ctrl_get_cnt(struct pvr2_ctrl *cptr)
154{
155 int ret = 0;
156 if (!cptr) return 0;
157 LOCK_TAKE(cptr->hdw->big_lock); do {
158 if (cptr->info->type == pvr2_ctl_enum) {
159 ret = cptr->info->def.type_enum.count;
160 }
161 } while(0); LOCK_GIVE(cptr->hdw->big_lock);
162 return ret;
163}
164
165
166/* Retrieve control's valid mask bits (bit mask only) */
167int pvr2_ctrl_get_mask(struct pvr2_ctrl *cptr)
168{
169 int ret = 0;
170 if (!cptr) return 0;
171 LOCK_TAKE(cptr->hdw->big_lock); do {
172 if (cptr->info->type == pvr2_ctl_bitmask) {
173 ret = cptr->info->def.type_bitmask.valid_bits;
174 }
175 } while(0); LOCK_GIVE(cptr->hdw->big_lock);
176 return ret;
177}
178
179
180/* Retrieve the control's name */
181const char *pvr2_ctrl_get_name(struct pvr2_ctrl *cptr)
182{
183 if (!cptr) return NULL;
184 return cptr->info->name;
185}
186
187
188/* Retrieve the control's desc */
189const char *pvr2_ctrl_get_desc(struct pvr2_ctrl *cptr)
190{
191 if (!cptr) return NULL;
192 return cptr->info->desc;
193}
194
195
196/* Retrieve a control enumeration or bit mask value */
197int pvr2_ctrl_get_valname(struct pvr2_ctrl *cptr,int val,
198 char *bptr,unsigned int bmax,
199 unsigned int *blen)
200{
201 int ret = -EINVAL;
202 if (!cptr) return 0;
203 *blen = 0;
204 LOCK_TAKE(cptr->hdw->big_lock); do {
205 if (cptr->info->type == pvr2_ctl_enum) {
206 const char * const *names;
207 names = cptr->info->def.type_enum.value_names;
208 if (pvr2_ctrl_range_check(cptr,val) == 0) {
209 if (names[val]) {
210 *blen = scnprintf(
211 bptr,bmax,"%s",
212 names[val]);
213 } else {
214 *blen = 0;
215 }
216 ret = 0;
217 }
218 } else if (cptr->info->type == pvr2_ctl_bitmask) {
219 const char **names;
220 unsigned int idx;
221 int msk;
222 names = cptr->info->def.type_bitmask.bit_names;
223 val &= cptr->info->def.type_bitmask.valid_bits;
224 for (idx = 0, msk = 1; val; idx++, msk <<= 1) {
225 if (val & msk) {
226 *blen = scnprintf(bptr,bmax,"%s",
227 names[idx]);
228 ret = 0;
229 break;
230 }
231 }
232 }
233 } while(0); LOCK_GIVE(cptr->hdw->big_lock);
234 return ret;
235}
236
237
238/* Return V4L ID for this control or zero if none */
239int pvr2_ctrl_get_v4lid(struct pvr2_ctrl *cptr)
240{
241 if (!cptr) return 0;
242 return cptr->info->v4l_id;
243}
244
245
246unsigned int pvr2_ctrl_get_v4lflags(struct pvr2_ctrl *cptr)
247{
248 unsigned int flags = 0;
249
250 if (cptr->info->get_v4lflags) {
251 flags = cptr->info->get_v4lflags(cptr);
252 }
253
254 if (cptr->info->set_value) {
255 flags &= ~V4L2_CTRL_FLAG_READ_ONLY;
256 } else {
257 flags |= V4L2_CTRL_FLAG_READ_ONLY;
258 }
259
260 return flags;
261}
262
263
264/* Return true if control is writable */
265int pvr2_ctrl_is_writable(struct pvr2_ctrl *cptr)
266{
267 if (!cptr) return 0;
268 return cptr->info->set_value != NULL;
269}
270
271
272/* Return true if control has custom symbolic representation */
273int pvr2_ctrl_has_custom_symbols(struct pvr2_ctrl *cptr)
274{
275 if (!cptr) return 0;
276 if (!cptr->info->val_to_sym) return 0;
277 if (!cptr->info->sym_to_val) return 0;
278 return !0;
279}
280
281
282/* Convert a given mask/val to a custom symbolic value */
283int pvr2_ctrl_custom_value_to_sym(struct pvr2_ctrl *cptr,
284 int mask,int val,
285 char *buf,unsigned int maxlen,
286 unsigned int *len)
287{
288 if (!cptr) return -EINVAL;
289 if (!cptr->info->val_to_sym) return -EINVAL;
290 return cptr->info->val_to_sym(cptr,mask,val,buf,maxlen,len);
291}
292
293
294/* Convert a symbolic value to a mask/value pair */
295int pvr2_ctrl_custom_sym_to_value(struct pvr2_ctrl *cptr,
296 const char *buf,unsigned int len,
297 int *maskptr,int *valptr)
298{
299 if (!cptr) return -EINVAL;
300 if (!cptr->info->sym_to_val) return -EINVAL;
301 return cptr->info->sym_to_val(cptr,buf,len,maskptr,valptr);
302}
303
304
305static unsigned int gen_bitmask_string(int msk,int val,int msk_only,
306 const char **names,
307 char *ptr,unsigned int len)
308{
309 unsigned int idx;
310 long sm,um;
311 int spcFl;
312 unsigned int uc,cnt;
313 const char *idStr;
314
315 spcFl = 0;
316 uc = 0;
317 um = 0;
318 for (idx = 0, sm = 1; msk; idx++, sm <<= 1) {
319 if (sm & msk) {
320 msk &= ~sm;
321 idStr = names[idx];
322 if (idStr) {
323 cnt = scnprintf(ptr,len,"%s%s%s",
324 (spcFl ? " " : ""),
325 (msk_only ? "" :
326 ((val & sm) ? "+" : "-")),
327 idStr);
328 ptr += cnt; len -= cnt; uc += cnt;
329 spcFl = !0;
330 } else {
331 um |= sm;
332 }
333 }
334 }
335 if (um) {
336 if (msk_only) {
337 cnt = scnprintf(ptr,len,"%s0x%lx",
338 (spcFl ? " " : ""),
339 um);
340 ptr += cnt; len -= cnt; uc += cnt;
341 spcFl = !0;
342 } else if (um & val) {
343 cnt = scnprintf(ptr,len,"%s+0x%lx",
344 (spcFl ? " " : ""),
345 um & val);
346 ptr += cnt; len -= cnt; uc += cnt;
347 spcFl = !0;
348 } else if (um & ~val) {
349 cnt = scnprintf(ptr,len,"%s+0x%lx",
350 (spcFl ? " " : ""),
351 um & ~val);
352 ptr += cnt; len -= cnt; uc += cnt;
353 spcFl = !0;
354 }
355 }
356 return uc;
357}
358
359
360static const char *boolNames[] = {
361 "false",
362 "true",
363 "no",
364 "yes",
365};
366
367
368static int parse_token(const char *ptr,unsigned int len,
369 int *valptr,
370 const char * const *names, unsigned int namecnt)
371{
372 char buf[33];
373 unsigned int slen;
374 unsigned int idx;
375 int negfl;
376 char *p2;
377 *valptr = 0;
378 if (!names) namecnt = 0;
379 for (idx = 0; idx < namecnt; idx++) {
380 if (!names[idx]) continue;
381 slen = strlen(names[idx]);
382 if (slen != len) continue;
383 if (memcmp(names[idx],ptr,slen)) continue;
384 *valptr = idx;
385 return 0;
386 }
387 negfl = 0;
388 if ((*ptr == '-') || (*ptr == '+')) {
389 negfl = (*ptr == '-');
390 ptr++; len--;
391 }
392 if (len >= sizeof(buf)) return -EINVAL;
393 memcpy(buf,ptr,len);
394 buf[len] = 0;
395 *valptr = simple_strtol(buf,&p2,0);
396 if (negfl) *valptr = -(*valptr);
397 if (*p2) return -EINVAL;
398 return 1;
399}
400
401
402static int parse_mtoken(const char *ptr,unsigned int len,
403 int *valptr,
404 const char **names,int valid_bits)
405{
406 char buf[33];
407 unsigned int slen;
408 unsigned int idx;
409 char *p2;
410 int msk;
411 *valptr = 0;
412 for (idx = 0, msk = 1; valid_bits; idx++, msk <<= 1) {
413 if (!(msk & valid_bits)) continue;
414 valid_bits &= ~msk;
415 if (!names[idx]) continue;
416 slen = strlen(names[idx]);
417 if (slen != len) continue;
418 if (memcmp(names[idx],ptr,slen)) continue;
419 *valptr = msk;
420 return 0;
421 }
422 if (len >= sizeof(buf)) return -EINVAL;
423 memcpy(buf,ptr,len);
424 buf[len] = 0;
425 *valptr = simple_strtol(buf,&p2,0);
426 if (*p2) return -EINVAL;
427 return 0;
428}
429
430
431static int parse_tlist(const char *ptr,unsigned int len,
432 int *maskptr,int *valptr,
433 const char **names,int valid_bits)
434{
435 unsigned int cnt;
436 int mask,val,kv,mode,ret;
437 mask = 0;
438 val = 0;
439 ret = 0;
440 while (len) {
441 cnt = 0;
442 while ((cnt < len) &&
443 ((ptr[cnt] <= 32) ||
444 (ptr[cnt] >= 127))) cnt++;
445 ptr += cnt;
446 len -= cnt;
447 mode = 0;
448 if ((*ptr == '-') || (*ptr == '+')) {
449 mode = (*ptr == '-') ? -1 : 1;
450 ptr++;
451 len--;
452 }
453 cnt = 0;
454 while (cnt < len) {
455 if (ptr[cnt] <= 32) break;
456 if (ptr[cnt] >= 127) break;
457 cnt++;
458 }
459 if (!cnt) break;
460 if (parse_mtoken(ptr,cnt,&kv,names,valid_bits)) {
461 ret = -EINVAL;
462 break;
463 }
464 ptr += cnt;
465 len -= cnt;
466 switch (mode) {
467 case 0:
468 mask = valid_bits;
469 val |= kv;
470 break;
471 case -1:
472 mask |= kv;
473 val &= ~kv;
474 break;
475 case 1:
476 mask |= kv;
477 val |= kv;
478 break;
479 default:
480 break;
481 }
482 }
483 *maskptr = mask;
484 *valptr = val;
485 return ret;
486}
487
488
489/* Convert a symbolic value to a mask/value pair */
490int pvr2_ctrl_sym_to_value(struct pvr2_ctrl *cptr,
491 const char *ptr,unsigned int len,
492 int *maskptr,int *valptr)
493{
494 int ret = -EINVAL;
495 unsigned int cnt;
496
497 *maskptr = 0;
498 *valptr = 0;
499
500 cnt = 0;
501 while ((cnt < len) && ((ptr[cnt] <= 32) || (ptr[cnt] >= 127))) cnt++;
502 len -= cnt; ptr += cnt;
503 cnt = 0;
504 while ((cnt < len) && ((ptr[len-(cnt+1)] <= 32) ||
505 (ptr[len-(cnt+1)] >= 127))) cnt++;
506 len -= cnt;
507
508 if (!len) return -EINVAL;
509
510 LOCK_TAKE(cptr->hdw->big_lock); do {
511 if (cptr->info->type == pvr2_ctl_int) {
512 ret = parse_token(ptr,len,valptr,NULL,0);
513 if (ret >= 0) {
514 ret = pvr2_ctrl_range_check(cptr,*valptr);
515 }
516 *maskptr = ~0;
517 } else if (cptr->info->type == pvr2_ctl_bool) {
518 ret = parse_token(ptr,len,valptr,boolNames,
519 ARRAY_SIZE(boolNames));
520 if (ret == 1) {
521 *valptr = *valptr ? !0 : 0;
522 } else if (ret == 0) {
523 *valptr = (*valptr & 1) ? !0 : 0;
524 }
525 *maskptr = 1;
526 } else if (cptr->info->type == pvr2_ctl_enum) {
527 ret = parse_token(
528 ptr,len,valptr,
529 cptr->info->def.type_enum.value_names,
530 cptr->info->def.type_enum.count);
531 if (ret >= 0) {
532 ret = pvr2_ctrl_range_check(cptr,*valptr);
533 }
534 *maskptr = ~0;
535 } else if (cptr->info->type == pvr2_ctl_bitmask) {
536 ret = parse_tlist(
537 ptr,len,maskptr,valptr,
538 cptr->info->def.type_bitmask.bit_names,
539 cptr->info->def.type_bitmask.valid_bits);
540 }
541 } while(0); LOCK_GIVE(cptr->hdw->big_lock);
542 return ret;
543}
544
545
546/* Convert a given mask/val to a symbolic value */
547int pvr2_ctrl_value_to_sym_internal(struct pvr2_ctrl *cptr,
548 int mask,int val,
549 char *buf,unsigned int maxlen,
550 unsigned int *len)
551{
552 int ret = -EINVAL;
553
554 *len = 0;
555 if (cptr->info->type == pvr2_ctl_int) {
556 *len = scnprintf(buf,maxlen,"%d",val);
557 ret = 0;
558 } else if (cptr->info->type == pvr2_ctl_bool) {
559 *len = scnprintf(buf,maxlen,"%s",val ? "true" : "false");
560 ret = 0;
561 } else if (cptr->info->type == pvr2_ctl_enum) {
562 const char * const *names;
563 names = cptr->info->def.type_enum.value_names;
564 if ((val >= 0) &&
565 (val < cptr->info->def.type_enum.count)) {
566 if (names[val]) {
567 *len = scnprintf(
568 buf,maxlen,"%s",
569 names[val]);
570 } else {
571 *len = 0;
572 }
573 ret = 0;
574 }
575 } else if (cptr->info->type == pvr2_ctl_bitmask) {
576 *len = gen_bitmask_string(
577 val & mask & cptr->info->def.type_bitmask.valid_bits,
578 ~0,!0,
579 cptr->info->def.type_bitmask.bit_names,
580 buf,maxlen);
581 }
582 return ret;
583}
584
585
586/* Convert a given mask/val to a symbolic value */
587int pvr2_ctrl_value_to_sym(struct pvr2_ctrl *cptr,
588 int mask,int val,
589 char *buf,unsigned int maxlen,
590 unsigned int *len)
591{
592 int ret;
593 LOCK_TAKE(cptr->hdw->big_lock); do {
594 ret = pvr2_ctrl_value_to_sym_internal(cptr,mask,val,
595 buf,maxlen,len);
596 } while(0); LOCK_GIVE(cptr->hdw->big_lock);
597 return ret;
598}
599
600
601/*
602 Stuff for Emacs to see, in order to encourage consistent editing style:
603 *** Local Variables: ***
604 *** mode: c ***
605 *** fill-column: 75 ***
606 *** tab-width: 8 ***
607 *** c-basic-offset: 8 ***
608 *** End: ***
609 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-ctrl.h b/drivers/media/video/pvrusb2/pvrusb2-ctrl.h
new file mode 100644
index 00000000000..794ff90121c
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-ctrl.h
@@ -0,0 +1,122 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20#ifndef __PVRUSB2_CTRL_H
21#define __PVRUSB2_CTRL_H
22
23struct pvr2_ctrl;
24
25enum pvr2_ctl_type {
26 pvr2_ctl_int = 0,
27 pvr2_ctl_enum = 1,
28 pvr2_ctl_bitmask = 2,
29 pvr2_ctl_bool = 3,
30};
31
32
33/* Set the given control. */
34int pvr2_ctrl_set_value(struct pvr2_ctrl *,int val);
35
36/* Set/clear specific bits of the given control. */
37int pvr2_ctrl_set_mask_value(struct pvr2_ctrl *,int mask,int val);
38
39/* Get the current value of the given control. */
40int pvr2_ctrl_get_value(struct pvr2_ctrl *,int *valptr);
41
42/* Retrieve control's type */
43enum pvr2_ctl_type pvr2_ctrl_get_type(struct pvr2_ctrl *);
44
45/* Retrieve control's maximum value (int type) */
46int pvr2_ctrl_get_max(struct pvr2_ctrl *);
47
48/* Retrieve control's minimum value (int type) */
49int pvr2_ctrl_get_min(struct pvr2_ctrl *);
50
51/* Retrieve control's default value (any type) */
52int pvr2_ctrl_get_def(struct pvr2_ctrl *, int *valptr);
53
54/* Retrieve control's enumeration count (enum only) */
55int pvr2_ctrl_get_cnt(struct pvr2_ctrl *);
56
57/* Retrieve control's valid mask bits (bit mask only) */
58int pvr2_ctrl_get_mask(struct pvr2_ctrl *);
59
60/* Retrieve the control's name */
61const char *pvr2_ctrl_get_name(struct pvr2_ctrl *);
62
63/* Retrieve the control's desc */
64const char *pvr2_ctrl_get_desc(struct pvr2_ctrl *);
65
66/* Retrieve a control enumeration or bit mask value */
67int pvr2_ctrl_get_valname(struct pvr2_ctrl *,int,char *,unsigned int,
68 unsigned int *);
69
70/* Return true if control is writable */
71int pvr2_ctrl_is_writable(struct pvr2_ctrl *);
72
73/* Return V4L flags value for control (or zero if there is no v4l control
74 actually under this control) */
75unsigned int pvr2_ctrl_get_v4lflags(struct pvr2_ctrl *);
76
77/* Return V4L ID for this control or zero if none */
78int pvr2_ctrl_get_v4lid(struct pvr2_ctrl *);
79
80/* Return true if control has custom symbolic representation */
81int pvr2_ctrl_has_custom_symbols(struct pvr2_ctrl *);
82
83/* Convert a given mask/val to a custom symbolic value */
84int pvr2_ctrl_custom_value_to_sym(struct pvr2_ctrl *,
85 int mask,int val,
86 char *buf,unsigned int maxlen,
87 unsigned int *len);
88
89/* Convert a symbolic value to a mask/value pair */
90int pvr2_ctrl_custom_sym_to_value(struct pvr2_ctrl *,
91 const char *buf,unsigned int len,
92 int *maskptr,int *valptr);
93
94/* Convert a given mask/val to a symbolic value */
95int pvr2_ctrl_value_to_sym(struct pvr2_ctrl *,
96 int mask,int val,
97 char *buf,unsigned int maxlen,
98 unsigned int *len);
99
100/* Convert a symbolic value to a mask/value pair */
101int pvr2_ctrl_sym_to_value(struct pvr2_ctrl *,
102 const char *buf,unsigned int len,
103 int *maskptr,int *valptr);
104
105/* Convert a given mask/val to a symbolic value - must already be
106 inside of critical region. */
107int pvr2_ctrl_value_to_sym_internal(struct pvr2_ctrl *,
108 int mask,int val,
109 char *buf,unsigned int maxlen,
110 unsigned int *len);
111
112#endif /* __PVRUSB2_CTRL_H */
113
114/*
115 Stuff for Emacs to see, in order to encourage consistent editing style:
116 *** Local Variables: ***
117 *** mode: c ***
118 *** fill-column: 75 ***
119 *** tab-width: 8 ***
120 *** c-basic-offset: 8 ***
121 *** End: ***
122 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-cx2584x-v4l.c b/drivers/media/video/pvrusb2/pvrusb2-cx2584x-v4l.c
new file mode 100644
index 00000000000..c514d0b9ffd
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-cx2584x-v4l.c
@@ -0,0 +1,166 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22/*
23
24 This source file is specifically designed to interface with the
25 cx2584x, in kernels 2.6.16 or newer.
26
27*/
28
29#include "pvrusb2-cx2584x-v4l.h"
30#include "pvrusb2-video-v4l.h"
31
32
33#include "pvrusb2-hdw-internal.h"
34#include "pvrusb2-debug.h"
35#include <media/cx25840.h>
36#include <linux/videodev2.h>
37#include <media/v4l2-common.h>
38#include <linux/errno.h>
39
40
41struct routing_scheme_item {
42 int vid;
43 int aud;
44};
45
46struct routing_scheme {
47 const struct routing_scheme_item *def;
48 unsigned int cnt;
49};
50
51static const struct routing_scheme_item routing_scheme0[] = {
52 [PVR2_CVAL_INPUT_TV] = {
53 .vid = CX25840_COMPOSITE7,
54 .aud = CX25840_AUDIO8,
55 },
56 [PVR2_CVAL_INPUT_RADIO] = { /* Treat the same as composite */
57 .vid = CX25840_COMPOSITE3,
58 .aud = CX25840_AUDIO_SERIAL,
59 },
60 [PVR2_CVAL_INPUT_COMPOSITE] = {
61 .vid = CX25840_COMPOSITE3,
62 .aud = CX25840_AUDIO_SERIAL,
63 },
64 [PVR2_CVAL_INPUT_SVIDEO] = {
65 .vid = CX25840_SVIDEO1,
66 .aud = CX25840_AUDIO_SERIAL,
67 },
68};
69
70static const struct routing_scheme routing_def0 = {
71 .def = routing_scheme0,
72 .cnt = ARRAY_SIZE(routing_scheme0),
73};
74
75/* Specific to gotview device */
76static const struct routing_scheme_item routing_schemegv[] = {
77 [PVR2_CVAL_INPUT_TV] = {
78 .vid = CX25840_COMPOSITE2,
79 .aud = CX25840_AUDIO5,
80 },
81 [PVR2_CVAL_INPUT_RADIO] = {
82 /* line-in is used for radio and composite. A GPIO is
83 used to switch between the two choices. */
84 .vid = CX25840_COMPOSITE1,
85 .aud = CX25840_AUDIO_SERIAL,
86 },
87 [PVR2_CVAL_INPUT_COMPOSITE] = {
88 .vid = CX25840_COMPOSITE1,
89 .aud = CX25840_AUDIO_SERIAL,
90 },
91 [PVR2_CVAL_INPUT_SVIDEO] = {
92 .vid = (CX25840_SVIDEO_LUMA3|CX25840_SVIDEO_CHROMA4),
93 .aud = CX25840_AUDIO_SERIAL,
94 },
95};
96
97static const struct routing_scheme routing_defgv = {
98 .def = routing_schemegv,
99 .cnt = ARRAY_SIZE(routing_schemegv),
100};
101
102/* Specific to grabster av400 device */
103static const struct routing_scheme_item routing_schemeav400[] = {
104 [PVR2_CVAL_INPUT_COMPOSITE] = {
105 .vid = CX25840_COMPOSITE1,
106 .aud = CX25840_AUDIO_SERIAL,
107 },
108 [PVR2_CVAL_INPUT_SVIDEO] = {
109 .vid = (CX25840_SVIDEO_LUMA2|CX25840_SVIDEO_CHROMA4),
110 .aud = CX25840_AUDIO_SERIAL,
111 },
112};
113
114static const struct routing_scheme routing_defav400 = {
115 .def = routing_schemeav400,
116 .cnt = ARRAY_SIZE(routing_schemeav400),
117};
118
119static const struct routing_scheme *routing_schemes[] = {
120 [PVR2_ROUTING_SCHEME_HAUPPAUGE] = &routing_def0,
121 [PVR2_ROUTING_SCHEME_GOTVIEW] = &routing_defgv,
122 [PVR2_ROUTING_SCHEME_AV400] = &routing_defav400,
123};
124
125void pvr2_cx25840_subdev_update(struct pvr2_hdw *hdw, struct v4l2_subdev *sd)
126{
127 pvr2_trace(PVR2_TRACE_CHIPS, "subdev cx2584x update...");
128 if (hdw->input_dirty || hdw->force_dirty) {
129 enum cx25840_video_input vid_input;
130 enum cx25840_audio_input aud_input;
131 const struct routing_scheme *sp;
132 unsigned int sid = hdw->hdw_desc->signal_routing_scheme;
133
134 sp = (sid < ARRAY_SIZE(routing_schemes)) ?
135 routing_schemes[sid] : NULL;
136 if ((sp == NULL) ||
137 (hdw->input_val < 0) ||
138 (hdw->input_val >= sp->cnt)) {
139 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
140 "*** WARNING *** subdev cx2584x set_input:"
141 " Invalid routing scheme (%u)"
142 " and/or input (%d)",
143 sid, hdw->input_val);
144 return;
145 }
146 vid_input = sp->def[hdw->input_val].vid;
147 aud_input = sp->def[hdw->input_val].aud;
148 pvr2_trace(PVR2_TRACE_CHIPS,
149 "subdev cx2584x set_input vid=0x%x aud=0x%x",
150 vid_input, aud_input);
151 sd->ops->video->s_routing(sd, (u32)vid_input, 0, 0);
152 sd->ops->audio->s_routing(sd, (u32)aud_input, 0, 0);
153 }
154}
155
156
157
158/*
159 Stuff for Emacs to see, in order to encourage consistent editing style:
160 *** Local Variables: ***
161 *** mode: c ***
162 *** fill-column: 70 ***
163 *** tab-width: 8 ***
164 *** c-basic-offset: 8 ***
165 *** End: ***
166 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-cx2584x-v4l.h b/drivers/media/video/pvrusb2/pvrusb2-cx2584x-v4l.h
new file mode 100644
index 00000000000..e35c2322a08
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-cx2584x-v4l.h
@@ -0,0 +1,52 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#ifndef __PVRUSB2_CX2584X_V4L_H
23#define __PVRUSB2_CX2584X_V4L_H
24
25/*
26
27 This module connects the pvrusb2 driver to the I2C chip level
28 driver which handles combined device audio & video processing.
29 This interface is used internally by the driver; higher level code
30 should only interact through the interface provided by
31 pvrusb2-hdw.h.
32
33*/
34
35
36
37#include "pvrusb2-hdw-internal.h"
38
39void pvr2_cx25840_subdev_update(struct pvr2_hdw *, struct v4l2_subdev *sd);
40
41
42#endif /* __PVRUSB2_CX2584X_V4L_H */
43
44/*
45 Stuff for Emacs to see, in order to encourage consistent editing style:
46 *** Local Variables: ***
47 *** mode: c ***
48 *** fill-column: 70 ***
49 *** tab-width: 8 ***
50 *** c-basic-offset: 8 ***
51 *** End: ***
52 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-debug.h b/drivers/media/video/pvrusb2/pvrusb2-debug.h
new file mode 100644
index 00000000000..be79249f862
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-debug.h
@@ -0,0 +1,69 @@
1/*
2 *
3 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 */
19#ifndef __PVRUSB2_DEBUG_H
20#define __PVRUSB2_DEBUG_H
21
22extern int pvrusb2_debug;
23
24#define pvr2_trace(msk, fmt, arg...) do {if(msk & pvrusb2_debug) printk(KERN_INFO "pvrusb2: " fmt "\n", ##arg); } while (0)
25
26/* These are listed in *rough* order of decreasing usefulness and
27 increasing noise level. */
28#define PVR2_TRACE_INFO (1 << 0) /* Normal messages */
29#define PVR2_TRACE_ERROR_LEGS (1 << 1) /* error messages */
30#define PVR2_TRACE_TOLERANCE (1 << 2) /* track tolerance-affected errors */
31#define PVR2_TRACE_TRAP (1 << 3) /* Trap & report app misbehavior */
32#define PVR2_TRACE_STD (1 << 4) /* Log video standard stuff */
33#define PVR2_TRACE_INIT (1 << 5) /* misc initialization steps */
34#define PVR2_TRACE_START_STOP (1 << 6) /* Streaming start / stop */
35#define PVR2_TRACE_CTL (1 << 7) /* commit of control changes */
36#define PVR2_TRACE_STATE (1 << 8) /* Device state changes */
37#define PVR2_TRACE_STBITS (1 << 9) /* Individual bit state changes */
38#define PVR2_TRACE_EEPROM (1 << 10) /* eeprom parsing / report */
39#define PVR2_TRACE_STRUCT (1 << 11) /* internal struct creation */
40#define PVR2_TRACE_OPEN_CLOSE (1 << 12) /* application open / close */
41#define PVR2_TRACE_CTXT (1 << 13) /* Main context tracking */
42#define PVR2_TRACE_SYSFS (1 << 14) /* Sysfs driven I/O */
43#define PVR2_TRACE_FIRMWARE (1 << 15) /* firmware upload actions */
44#define PVR2_TRACE_CHIPS (1 << 16) /* chip broadcast operation */
45#define PVR2_TRACE_I2C (1 << 17) /* I2C related stuff */
46#define PVR2_TRACE_I2C_CMD (1 << 18) /* Software commands to I2C modules */
47#define PVR2_TRACE_I2C_CORE (1 << 19) /* I2C core debugging */
48#define PVR2_TRACE_I2C_TRAF (1 << 20) /* I2C traffic through the adapter */
49#define PVR2_TRACE_V4LIOCTL (1 << 21) /* v4l ioctl details */
50#define PVR2_TRACE_ENCODER (1 << 22) /* mpeg2 encoder operation */
51#define PVR2_TRACE_BUF_POOL (1 << 23) /* Track buffer pool management */
52#define PVR2_TRACE_BUF_FLOW (1 << 24) /* Track buffer flow in system */
53#define PVR2_TRACE_DATA_FLOW (1 << 25) /* Track data flow */
54#define PVR2_TRACE_DEBUGIFC (1 << 26) /* Debug interface actions */
55#define PVR2_TRACE_GPIO (1 << 27) /* GPIO state bit changes */
56#define PVR2_TRACE_DVB_FEED (1 << 28) /* DVB transport feed debug */
57
58
59#endif /* __PVRUSB2_HDW_INTERNAL_H */
60
61/*
62 Stuff for Emacs to see, in order to encourage consistent editing style:
63 *** Local Variables: ***
64 *** mode: c ***
65 *** fill-column: 75 ***
66 *** tab-width: 8 ***
67 *** c-basic-offset: 8 ***
68 *** End: ***
69 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-debugifc.c b/drivers/media/video/pvrusb2/pvrusb2-debugifc.c
new file mode 100644
index 00000000000..4279ebb811a
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-debugifc.c
@@ -0,0 +1,335 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20
21#include <linux/string.h>
22#include "pvrusb2-debugifc.h"
23#include "pvrusb2-hdw.h"
24#include "pvrusb2-debug.h"
25
26struct debugifc_mask_item {
27 const char *name;
28 unsigned long msk;
29};
30
31
32static unsigned int debugifc_count_whitespace(const char *buf,
33 unsigned int count)
34{
35 unsigned int scnt;
36 char ch;
37
38 for (scnt = 0; scnt < count; scnt++) {
39 ch = buf[scnt];
40 if (ch == ' ') continue;
41 if (ch == '\t') continue;
42 if (ch == '\n') continue;
43 break;
44 }
45 return scnt;
46}
47
48
49static unsigned int debugifc_count_nonwhitespace(const char *buf,
50 unsigned int count)
51{
52 unsigned int scnt;
53 char ch;
54
55 for (scnt = 0; scnt < count; scnt++) {
56 ch = buf[scnt];
57 if (ch == ' ') break;
58 if (ch == '\t') break;
59 if (ch == '\n') break;
60 }
61 return scnt;
62}
63
64
65static unsigned int debugifc_isolate_word(const char *buf,unsigned int count,
66 const char **wstrPtr,
67 unsigned int *wlenPtr)
68{
69 const char *wptr;
70 unsigned int consume_cnt = 0;
71 unsigned int wlen;
72 unsigned int scnt;
73
74 wptr = NULL;
75 wlen = 0;
76 scnt = debugifc_count_whitespace(buf,count);
77 consume_cnt += scnt; count -= scnt; buf += scnt;
78 if (!count) goto done;
79
80 scnt = debugifc_count_nonwhitespace(buf,count);
81 if (!scnt) goto done;
82 wptr = buf;
83 wlen = scnt;
84 consume_cnt += scnt; count -= scnt; buf += scnt;
85
86 done:
87 *wstrPtr = wptr;
88 *wlenPtr = wlen;
89 return consume_cnt;
90}
91
92
93static int debugifc_parse_unsigned_number(const char *buf,unsigned int count,
94 u32 *num_ptr)
95{
96 u32 result = 0;
97 int radix = 10;
98 if ((count >= 2) && (buf[0] == '0') &&
99 ((buf[1] == 'x') || (buf[1] == 'X'))) {
100 radix = 16;
101 count -= 2;
102 buf += 2;
103 } else if ((count >= 1) && (buf[0] == '0')) {
104 radix = 8;
105 }
106
107 while (count--) {
108 int val = hex_to_bin(*buf++);
109 if (val < 0 || val >= radix)
110 return -EINVAL;
111 result *= radix;
112 result += val;
113 }
114 *num_ptr = result;
115 return 0;
116}
117
118
119static int debugifc_match_keyword(const char *buf,unsigned int count,
120 const char *keyword)
121{
122 unsigned int kl;
123 if (!keyword) return 0;
124 kl = strlen(keyword);
125 if (kl != count) return 0;
126 return !memcmp(buf,keyword,kl);
127}
128
129
130int pvr2_debugifc_print_info(struct pvr2_hdw *hdw,char *buf,unsigned int acnt)
131{
132 int bcnt = 0;
133 int ccnt;
134 ccnt = scnprintf(buf, acnt, "Driver hardware description: %s\n",
135 pvr2_hdw_get_desc(hdw));
136 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
137 ccnt = scnprintf(buf,acnt,"Driver state info:\n");
138 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
139 ccnt = pvr2_hdw_state_report(hdw,buf,acnt);
140 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
141
142 return bcnt;
143}
144
145
146int pvr2_debugifc_print_status(struct pvr2_hdw *hdw,
147 char *buf,unsigned int acnt)
148{
149 int bcnt = 0;
150 int ccnt;
151 int ret;
152 u32 gpio_dir,gpio_in,gpio_out;
153 struct pvr2_stream_stats stats;
154 struct pvr2_stream *sp;
155
156 ret = pvr2_hdw_is_hsm(hdw);
157 ccnt = scnprintf(buf,acnt,"USB link speed: %s\n",
158 (ret < 0 ? "FAIL" : (ret ? "high" : "full")));
159 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
160
161 gpio_dir = 0; gpio_in = 0; gpio_out = 0;
162 pvr2_hdw_gpio_get_dir(hdw,&gpio_dir);
163 pvr2_hdw_gpio_get_out(hdw,&gpio_out);
164 pvr2_hdw_gpio_get_in(hdw,&gpio_in);
165 ccnt = scnprintf(buf,acnt,"GPIO state: dir=0x%x in=0x%x out=0x%x\n",
166 gpio_dir,gpio_in,gpio_out);
167 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
168
169 ccnt = scnprintf(buf,acnt,"Streaming is %s\n",
170 pvr2_hdw_get_streaming(hdw) ? "on" : "off");
171 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
172
173
174 sp = pvr2_hdw_get_video_stream(hdw);
175 if (sp) {
176 pvr2_stream_get_stats(sp, &stats, 0);
177 ccnt = scnprintf(
178 buf,acnt,
179 "Bytes streamed=%u"
180 " URBs: queued=%u idle=%u ready=%u"
181 " processed=%u failed=%u\n",
182 stats.bytes_processed,
183 stats.buffers_in_queue,
184 stats.buffers_in_idle,
185 stats.buffers_in_ready,
186 stats.buffers_processed,
187 stats.buffers_failed);
188 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
189 }
190
191 return bcnt;
192}
193
194
195static int pvr2_debugifc_do1cmd(struct pvr2_hdw *hdw,const char *buf,
196 unsigned int count)
197{
198 const char *wptr;
199 unsigned int wlen;
200 unsigned int scnt;
201
202 scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
203 if (!scnt) return 0;
204 count -= scnt; buf += scnt;
205 if (!wptr) return 0;
206
207 pvr2_trace(PVR2_TRACE_DEBUGIFC,"debugifc cmd: \"%.*s\"",wlen,wptr);
208 if (debugifc_match_keyword(wptr,wlen,"reset")) {
209 scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
210 if (!scnt) return -EINVAL;
211 count -= scnt; buf += scnt;
212 if (!wptr) return -EINVAL;
213 if (debugifc_match_keyword(wptr,wlen,"cpu")) {
214 pvr2_hdw_cpureset_assert(hdw,!0);
215 pvr2_hdw_cpureset_assert(hdw,0);
216 return 0;
217 } else if (debugifc_match_keyword(wptr,wlen,"bus")) {
218 pvr2_hdw_device_reset(hdw);
219 } else if (debugifc_match_keyword(wptr,wlen,"soft")) {
220 return pvr2_hdw_cmd_powerup(hdw);
221 } else if (debugifc_match_keyword(wptr,wlen,"deep")) {
222 return pvr2_hdw_cmd_deep_reset(hdw);
223 } else if (debugifc_match_keyword(wptr,wlen,"firmware")) {
224 return pvr2_upload_firmware2(hdw);
225 } else if (debugifc_match_keyword(wptr,wlen,"decoder")) {
226 return pvr2_hdw_cmd_decoder_reset(hdw);
227 } else if (debugifc_match_keyword(wptr,wlen,"worker")) {
228 return pvr2_hdw_untrip(hdw);
229 } else if (debugifc_match_keyword(wptr,wlen,"usbstats")) {
230 pvr2_stream_get_stats(pvr2_hdw_get_video_stream(hdw),
231 NULL, !0);
232 return 0;
233 }
234 return -EINVAL;
235 } else if (debugifc_match_keyword(wptr,wlen,"cpufw")) {
236 scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
237 if (!scnt) return -EINVAL;
238 count -= scnt; buf += scnt;
239 if (!wptr) return -EINVAL;
240 if (debugifc_match_keyword(wptr,wlen,"fetch")) {
241 scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
242 if (scnt && wptr) {
243 count -= scnt; buf += scnt;
244 if (debugifc_match_keyword(wptr, wlen,
245 "prom")) {
246 pvr2_hdw_cpufw_set_enabled(hdw, 2, !0);
247 } else if (debugifc_match_keyword(wptr, wlen,
248 "ram8k")) {
249 pvr2_hdw_cpufw_set_enabled(hdw, 0, !0);
250 } else if (debugifc_match_keyword(wptr, wlen,
251 "ram16k")) {
252 pvr2_hdw_cpufw_set_enabled(hdw, 1, !0);
253 } else {
254 return -EINVAL;
255 }
256 }
257 pvr2_hdw_cpufw_set_enabled(hdw,0,!0);
258 return 0;
259 } else if (debugifc_match_keyword(wptr,wlen,"done")) {
260 pvr2_hdw_cpufw_set_enabled(hdw,0,0);
261 return 0;
262 } else {
263 return -EINVAL;
264 }
265 } else if (debugifc_match_keyword(wptr,wlen,"gpio")) {
266 int dir_fl = 0;
267 int ret;
268 u32 msk,val;
269 scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
270 if (!scnt) return -EINVAL;
271 count -= scnt; buf += scnt;
272 if (!wptr) return -EINVAL;
273 if (debugifc_match_keyword(wptr,wlen,"dir")) {
274 dir_fl = !0;
275 } else if (!debugifc_match_keyword(wptr,wlen,"out")) {
276 return -EINVAL;
277 }
278 scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
279 if (!scnt) return -EINVAL;
280 count -= scnt; buf += scnt;
281 if (!wptr) return -EINVAL;
282 ret = debugifc_parse_unsigned_number(wptr,wlen,&msk);
283 if (ret) return ret;
284 scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
285 if (wptr) {
286 ret = debugifc_parse_unsigned_number(wptr,wlen,&val);
287 if (ret) return ret;
288 } else {
289 val = msk;
290 msk = 0xffffffff;
291 }
292 if (dir_fl) {
293 ret = pvr2_hdw_gpio_chg_dir(hdw,msk,val);
294 } else {
295 ret = pvr2_hdw_gpio_chg_out(hdw,msk,val);
296 }
297 return ret;
298 }
299 pvr2_trace(PVR2_TRACE_DEBUGIFC,
300 "debugifc failed to recognize cmd: \"%.*s\"",wlen,wptr);
301 return -EINVAL;
302}
303
304
305int pvr2_debugifc_docmd(struct pvr2_hdw *hdw,const char *buf,
306 unsigned int count)
307{
308 unsigned int bcnt = 0;
309 int ret;
310
311 while (count) {
312 for (bcnt = 0; bcnt < count; bcnt++) {
313 if (buf[bcnt] == '\n') break;
314 }
315
316 ret = pvr2_debugifc_do1cmd(hdw,buf,bcnt);
317 if (ret < 0) return ret;
318 if (bcnt < count) bcnt++;
319 buf += bcnt;
320 count -= bcnt;
321 }
322
323 return 0;
324}
325
326
327/*
328 Stuff for Emacs to see, in order to encourage consistent editing style:
329 *** Local Variables: ***
330 *** mode: c ***
331 *** fill-column: 75 ***
332 *** tab-width: 8 ***
333 *** c-basic-offset: 8 ***
334 *** End: ***
335 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-debugifc.h b/drivers/media/video/pvrusb2/pvrusb2-debugifc.h
new file mode 100644
index 00000000000..2f8d46761cd
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-debugifc.h
@@ -0,0 +1,52 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20#ifndef __PVRUSB2_DEBUGIFC_H
21#define __PVRUSB2_DEBUGIFC_H
22
23struct pvr2_hdw;
24
25/* Print general status of driver. This will also trigger a probe of
26 the USB link. Unlike print_info(), this one synchronizes with the
27 driver so the information should be self-consistent (but it will
28 hang if the driver is wedged). */
29int pvr2_debugifc_print_info(struct pvr2_hdw *,
30 char *buf_ptr, unsigned int buf_size);
31
32/* Non-intrusively print some useful debugging info from inside the
33 driver. This should work even if the driver appears to be
34 wedged. */
35int pvr2_debugifc_print_status(struct pvr2_hdw *,
36 char *buf_ptr,unsigned int buf_size);
37
38/* Parse a string command into a driver action. */
39int pvr2_debugifc_docmd(struct pvr2_hdw *,
40 const char *buf_ptr,unsigned int buf_size);
41
42#endif /* __PVRUSB2_DEBUGIFC_H */
43
44/*
45 Stuff for Emacs to see, in order to encourage consistent editing style:
46 *** Local Variables: ***
47 *** mode: c ***
48 *** fill-column: 75 ***
49 *** tab-width: 8 ***
50 *** c-basic-offset: 8 ***
51 *** End: ***
52 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-devattr.c b/drivers/media/video/pvrusb2/pvrusb2-devattr.c
new file mode 100644
index 00000000000..e799331389b
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-devattr.c
@@ -0,0 +1,558 @@
1/*
2 *
3 *
4 * Copyright (C) 2007 Mike Isely <isely@pobox.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20
21/*
22
23This source file should encompass ALL per-device type information for the
24driver. To define a new device, add elements to the pvr2_device_table and
25pvr2_device_desc structures.
26
27*/
28
29#include "pvrusb2-devattr.h"
30#include <linux/usb.h>
31/* This is needed in order to pull in tuner type ids... */
32#include <linux/i2c.h>
33#include <media/tuner.h>
34#ifdef CONFIG_VIDEO_PVRUSB2_DVB
35#include "pvrusb2-hdw-internal.h"
36#include "lgdt330x.h"
37#include "s5h1409.h"
38#include "s5h1411.h"
39#include "tda10048.h"
40#include "tda18271.h"
41#include "tda8290.h"
42#include "tuner-simple.h"
43#endif
44
45
46/*------------------------------------------------------------------------*/
47/* Hauppauge PVR-USB2 Model 29xxx */
48
49static const struct pvr2_device_client_desc pvr2_cli_29xxx[] = {
50 { .module_id = PVR2_CLIENT_ID_SAA7115 },
51 { .module_id = PVR2_CLIENT_ID_MSP3400 },
52 { .module_id = PVR2_CLIENT_ID_TUNER },
53 { .module_id = PVR2_CLIENT_ID_DEMOD },
54};
55
56static const char *pvr2_fw1_names_29xxx[] = {
57 "v4l-pvrusb2-29xxx-01.fw",
58};
59
60static const struct pvr2_device_desc pvr2_device_29xxx = {
61 .description = "WinTV PVR USB2 Model 29xxx",
62 .shortname = "29xxx",
63 .client_table.lst = pvr2_cli_29xxx,
64 .client_table.cnt = ARRAY_SIZE(pvr2_cli_29xxx),
65 .fx2_firmware.lst = pvr2_fw1_names_29xxx,
66 .fx2_firmware.cnt = ARRAY_SIZE(pvr2_fw1_names_29xxx),
67 .flag_has_hauppauge_rom = !0,
68 .flag_has_analogtuner = !0,
69 .flag_has_fmradio = !0,
70 .flag_has_composite = !0,
71 .flag_has_svideo = !0,
72 .signal_routing_scheme = PVR2_ROUTING_SCHEME_HAUPPAUGE,
73 .led_scheme = PVR2_LED_SCHEME_HAUPPAUGE,
74 .ir_scheme = PVR2_IR_SCHEME_29XXX,
75};
76
77
78
79/*------------------------------------------------------------------------*/
80/* Hauppauge PVR-USB2 Model 24xxx */
81
82static const struct pvr2_device_client_desc pvr2_cli_24xxx[] = {
83 { .module_id = PVR2_CLIENT_ID_CX25840 },
84 { .module_id = PVR2_CLIENT_ID_TUNER },
85 { .module_id = PVR2_CLIENT_ID_WM8775 },
86 { .module_id = PVR2_CLIENT_ID_DEMOD },
87};
88
89static const char *pvr2_fw1_names_24xxx[] = {
90 "v4l-pvrusb2-24xxx-01.fw",
91};
92
93static const struct pvr2_device_desc pvr2_device_24xxx = {
94 .description = "WinTV PVR USB2 Model 24xxx",
95 .shortname = "24xxx",
96 .client_table.lst = pvr2_cli_24xxx,
97 .client_table.cnt = ARRAY_SIZE(pvr2_cli_24xxx),
98 .fx2_firmware.lst = pvr2_fw1_names_24xxx,
99 .fx2_firmware.cnt = ARRAY_SIZE(pvr2_fw1_names_24xxx),
100 .flag_has_cx25840 = !0,
101 .flag_has_wm8775 = !0,
102 .flag_has_hauppauge_rom = !0,
103 .flag_has_analogtuner = !0,
104 .flag_has_fmradio = !0,
105 .flag_has_composite = !0,
106 .flag_has_svideo = !0,
107 .signal_routing_scheme = PVR2_ROUTING_SCHEME_HAUPPAUGE,
108 .led_scheme = PVR2_LED_SCHEME_HAUPPAUGE,
109 .ir_scheme = PVR2_IR_SCHEME_24XXX,
110};
111
112
113
114/*------------------------------------------------------------------------*/
115/* GOTVIEW USB2.0 DVD2 */
116
117static const struct pvr2_device_client_desc pvr2_cli_gotview_2[] = {
118 { .module_id = PVR2_CLIENT_ID_CX25840 },
119 { .module_id = PVR2_CLIENT_ID_TUNER },
120 { .module_id = PVR2_CLIENT_ID_DEMOD },
121};
122
123static const struct pvr2_device_desc pvr2_device_gotview_2 = {
124 .description = "Gotview USB 2.0 DVD 2",
125 .shortname = "gv2",
126 .client_table.lst = pvr2_cli_gotview_2,
127 .client_table.cnt = ARRAY_SIZE(pvr2_cli_gotview_2),
128 .flag_has_cx25840 = !0,
129 .default_tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
130 .flag_has_analogtuner = !0,
131 .flag_has_fmradio = !0,
132 .flag_has_composite = !0,
133 .flag_has_svideo = !0,
134 .signal_routing_scheme = PVR2_ROUTING_SCHEME_GOTVIEW,
135};
136
137
138
139/*------------------------------------------------------------------------*/
140/* GOTVIEW USB2.0 DVD Deluxe */
141
142/* (same module list as gotview_2) */
143
144static const struct pvr2_device_desc pvr2_device_gotview_2d = {
145 .description = "Gotview USB 2.0 DVD Deluxe",
146 .shortname = "gv2d",
147 .client_table.lst = pvr2_cli_gotview_2,
148 .client_table.cnt = ARRAY_SIZE(pvr2_cli_gotview_2),
149 .flag_has_cx25840 = !0,
150 .default_tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
151 .flag_has_analogtuner = !0,
152 .flag_has_composite = !0,
153 .flag_has_svideo = !0,
154 .signal_routing_scheme = PVR2_ROUTING_SCHEME_GOTVIEW,
155};
156
157
158
159/*------------------------------------------------------------------------*/
160/* Terratec Grabster AV400 */
161
162static const struct pvr2_device_client_desc pvr2_cli_av400[] = {
163 { .module_id = PVR2_CLIENT_ID_CX25840 },
164};
165
166static const struct pvr2_device_desc pvr2_device_av400 = {
167 .description = "Terratec Grabster AV400",
168 .shortname = "av400",
169 .flag_is_experimental = 1,
170 .client_table.lst = pvr2_cli_av400,
171 .client_table.cnt = ARRAY_SIZE(pvr2_cli_av400),
172 .flag_has_cx25840 = !0,
173 .flag_has_analogtuner = 0,
174 .flag_has_composite = !0,
175 .flag_has_svideo = !0,
176 .signal_routing_scheme = PVR2_ROUTING_SCHEME_AV400,
177};
178
179
180
181/*------------------------------------------------------------------------*/
182/* OnAir Creator */
183
184#ifdef CONFIG_VIDEO_PVRUSB2_DVB
185static struct lgdt330x_config pvr2_lgdt3303_config = {
186 .demod_address = 0x0e,
187 .demod_chip = LGDT3303,
188 .clock_polarity_flip = 1,
189};
190
191static int pvr2_lgdt3303_attach(struct pvr2_dvb_adapter *adap)
192{
193 adap->fe = dvb_attach(lgdt330x_attach, &pvr2_lgdt3303_config,
194 &adap->channel.hdw->i2c_adap);
195 if (adap->fe)
196 return 0;
197
198 return -EIO;
199}
200
201static int pvr2_lgh06xf_attach(struct pvr2_dvb_adapter *adap)
202{
203 dvb_attach(simple_tuner_attach, adap->fe,
204 &adap->channel.hdw->i2c_adap, 0x61,
205 TUNER_LG_TDVS_H06XF);
206
207 return 0;
208}
209
210static const struct pvr2_dvb_props pvr2_onair_creator_fe_props = {
211 .frontend_attach = pvr2_lgdt3303_attach,
212 .tuner_attach = pvr2_lgh06xf_attach,
213};
214#endif
215
216static const struct pvr2_device_client_desc pvr2_cli_onair_creator[] = {
217 { .module_id = PVR2_CLIENT_ID_SAA7115 },
218 { .module_id = PVR2_CLIENT_ID_CS53L32A },
219 { .module_id = PVR2_CLIENT_ID_TUNER },
220};
221
222static const struct pvr2_device_desc pvr2_device_onair_creator = {
223 .description = "OnAir Creator Hybrid USB tuner",
224 .shortname = "oac",
225 .client_table.lst = pvr2_cli_onair_creator,
226 .client_table.cnt = ARRAY_SIZE(pvr2_cli_onair_creator),
227 .default_tuner_type = TUNER_LG_TDVS_H06XF,
228 .flag_has_analogtuner = !0,
229 .flag_has_composite = !0,
230 .flag_has_svideo = !0,
231 .flag_digital_requires_cx23416 = !0,
232 .signal_routing_scheme = PVR2_ROUTING_SCHEME_ONAIR,
233 .digital_control_scheme = PVR2_DIGITAL_SCHEME_ONAIR,
234 .default_std_mask = V4L2_STD_NTSC_M,
235#ifdef CONFIG_VIDEO_PVRUSB2_DVB
236 .dvb_props = &pvr2_onair_creator_fe_props,
237#endif
238};
239
240
241
242/*------------------------------------------------------------------------*/
243/* OnAir USB 2.0 */
244
245#ifdef CONFIG_VIDEO_PVRUSB2_DVB
246static struct lgdt330x_config pvr2_lgdt3302_config = {
247 .demod_address = 0x0e,
248 .demod_chip = LGDT3302,
249};
250
251static int pvr2_lgdt3302_attach(struct pvr2_dvb_adapter *adap)
252{
253 adap->fe = dvb_attach(lgdt330x_attach, &pvr2_lgdt3302_config,
254 &adap->channel.hdw->i2c_adap);
255 if (adap->fe)
256 return 0;
257
258 return -EIO;
259}
260
261static int pvr2_fcv1236d_attach(struct pvr2_dvb_adapter *adap)
262{
263 dvb_attach(simple_tuner_attach, adap->fe,
264 &adap->channel.hdw->i2c_adap, 0x61,
265 TUNER_PHILIPS_FCV1236D);
266
267 return 0;
268}
269
270static const struct pvr2_dvb_props pvr2_onair_usb2_fe_props = {
271 .frontend_attach = pvr2_lgdt3302_attach,
272 .tuner_attach = pvr2_fcv1236d_attach,
273};
274#endif
275
276static const struct pvr2_device_client_desc pvr2_cli_onair_usb2[] = {
277 { .module_id = PVR2_CLIENT_ID_SAA7115 },
278 { .module_id = PVR2_CLIENT_ID_CS53L32A },
279 { .module_id = PVR2_CLIENT_ID_TUNER },
280};
281
282static const struct pvr2_device_desc pvr2_device_onair_usb2 = {
283 .description = "OnAir USB2 Hybrid USB tuner",
284 .shortname = "oa2",
285 .client_table.lst = pvr2_cli_onair_usb2,
286 .client_table.cnt = ARRAY_SIZE(pvr2_cli_onair_usb2),
287 .default_tuner_type = TUNER_PHILIPS_FCV1236D,
288 .flag_has_analogtuner = !0,
289 .flag_has_composite = !0,
290 .flag_has_svideo = !0,
291 .flag_digital_requires_cx23416 = !0,
292 .signal_routing_scheme = PVR2_ROUTING_SCHEME_ONAIR,
293 .digital_control_scheme = PVR2_DIGITAL_SCHEME_ONAIR,
294 .default_std_mask = V4L2_STD_NTSC_M,
295#ifdef CONFIG_VIDEO_PVRUSB2_DVB
296 .dvb_props = &pvr2_onair_usb2_fe_props,
297#endif
298};
299
300
301
302/*------------------------------------------------------------------------*/
303/* Hauppauge PVR-USB2 Model 73xxx */
304
305#ifdef CONFIG_VIDEO_PVRUSB2_DVB
306static struct tda10048_config hauppauge_tda10048_config = {
307 .demod_address = 0x10 >> 1,
308 .output_mode = TDA10048_PARALLEL_OUTPUT,
309 .fwbulkwritelen = TDA10048_BULKWRITE_50,
310 .inversion = TDA10048_INVERSION_ON,
311 .dtv6_if_freq_khz = TDA10048_IF_3300,
312 .dtv7_if_freq_khz = TDA10048_IF_3800,
313 .dtv8_if_freq_khz = TDA10048_IF_4300,
314 .clk_freq_khz = TDA10048_CLK_16000,
315 .disable_gate_access = 1,
316};
317
318static struct tda829x_config tda829x_no_probe = {
319 .probe_tuner = TDA829X_DONT_PROBE,
320};
321
322static struct tda18271_config hauppauge_tda18271_dvb_config = {
323 .gate = TDA18271_GATE_ANALOG,
324 .output_opt = TDA18271_OUTPUT_LT_OFF,
325};
326
327static int pvr2_tda10048_attach(struct pvr2_dvb_adapter *adap)
328{
329 adap->fe = dvb_attach(tda10048_attach, &hauppauge_tda10048_config,
330 &adap->channel.hdw->i2c_adap);
331 if (adap->fe)
332 return 0;
333
334 return -EIO;
335}
336
337static int pvr2_73xxx_tda18271_8295_attach(struct pvr2_dvb_adapter *adap)
338{
339 dvb_attach(tda829x_attach, adap->fe,
340 &adap->channel.hdw->i2c_adap, 0x42,
341 &tda829x_no_probe);
342 dvb_attach(tda18271_attach, adap->fe, 0x60,
343 &adap->channel.hdw->i2c_adap,
344 &hauppauge_tda18271_dvb_config);
345
346 return 0;
347}
348
349static const struct pvr2_dvb_props pvr2_73xxx_dvb_props = {
350 .frontend_attach = pvr2_tda10048_attach,
351 .tuner_attach = pvr2_73xxx_tda18271_8295_attach,
352};
353#endif
354
355static const struct pvr2_device_client_desc pvr2_cli_73xxx[] = {
356 { .module_id = PVR2_CLIENT_ID_CX25840 },
357 { .module_id = PVR2_CLIENT_ID_TUNER,
358 .i2c_address_list = "\x42"},
359};
360
361static const char *pvr2_fw1_names_73xxx[] = {
362 "v4l-pvrusb2-73xxx-01.fw",
363};
364
365static const struct pvr2_device_desc pvr2_device_73xxx = {
366 .description = "WinTV HVR-1900 Model 73xxx",
367 .shortname = "73xxx",
368 .client_table.lst = pvr2_cli_73xxx,
369 .client_table.cnt = ARRAY_SIZE(pvr2_cli_73xxx),
370 .fx2_firmware.lst = pvr2_fw1_names_73xxx,
371 .fx2_firmware.cnt = ARRAY_SIZE(pvr2_fw1_names_73xxx),
372 .flag_has_cx25840 = !0,
373 .flag_has_hauppauge_rom = !0,
374 .flag_has_analogtuner = !0,
375 .flag_has_composite = !0,
376 .flag_has_svideo = !0,
377 .flag_fx2_16kb = !0,
378 .signal_routing_scheme = PVR2_ROUTING_SCHEME_HAUPPAUGE,
379 .digital_control_scheme = PVR2_DIGITAL_SCHEME_HAUPPAUGE,
380 .led_scheme = PVR2_LED_SCHEME_HAUPPAUGE,
381 .ir_scheme = PVR2_IR_SCHEME_ZILOG,
382#ifdef CONFIG_VIDEO_PVRUSB2_DVB
383 .dvb_props = &pvr2_73xxx_dvb_props,
384#endif
385};
386
387
388
389/*------------------------------------------------------------------------*/
390/* Hauppauge PVR-USB2 Model 75xxx */
391
392#ifdef CONFIG_VIDEO_PVRUSB2_DVB
393static struct s5h1409_config pvr2_s5h1409_config = {
394 .demod_address = 0x32 >> 1,
395 .output_mode = S5H1409_PARALLEL_OUTPUT,
396 .gpio = S5H1409_GPIO_OFF,
397 .qam_if = 4000,
398 .inversion = S5H1409_INVERSION_ON,
399 .status_mode = S5H1409_DEMODLOCKING,
400};
401
402static struct s5h1411_config pvr2_s5h1411_config = {
403 .output_mode = S5H1411_PARALLEL_OUTPUT,
404 .gpio = S5H1411_GPIO_OFF,
405 .vsb_if = S5H1411_IF_44000,
406 .qam_if = S5H1411_IF_4000,
407 .inversion = S5H1411_INVERSION_ON,
408 .status_mode = S5H1411_DEMODLOCKING,
409};
410
411static struct tda18271_std_map hauppauge_tda18271_std_map = {
412 .atsc_6 = { .if_freq = 5380, .agc_mode = 3, .std = 3,
413 .if_lvl = 6, .rfagc_top = 0x37, },
414 .qam_6 = { .if_freq = 4000, .agc_mode = 3, .std = 0,
415 .if_lvl = 6, .rfagc_top = 0x37, },
416};
417
418static struct tda18271_config hauppauge_tda18271_config = {
419 .std_map = &hauppauge_tda18271_std_map,
420 .gate = TDA18271_GATE_ANALOG,
421 .output_opt = TDA18271_OUTPUT_LT_OFF,
422};
423
424static int pvr2_s5h1409_attach(struct pvr2_dvb_adapter *adap)
425{
426 adap->fe = dvb_attach(s5h1409_attach, &pvr2_s5h1409_config,
427 &adap->channel.hdw->i2c_adap);
428 if (adap->fe)
429 return 0;
430
431 return -EIO;
432}
433
434static int pvr2_s5h1411_attach(struct pvr2_dvb_adapter *adap)
435{
436 adap->fe = dvb_attach(s5h1411_attach, &pvr2_s5h1411_config,
437 &adap->channel.hdw->i2c_adap);
438 if (adap->fe)
439 return 0;
440
441 return -EIO;
442}
443
444static int pvr2_tda18271_8295_attach(struct pvr2_dvb_adapter *adap)
445{
446 dvb_attach(tda829x_attach, adap->fe,
447 &adap->channel.hdw->i2c_adap, 0x42,
448 &tda829x_no_probe);
449 dvb_attach(tda18271_attach, adap->fe, 0x60,
450 &adap->channel.hdw->i2c_adap,
451 &hauppauge_tda18271_config);
452
453 return 0;
454}
455
456static const struct pvr2_dvb_props pvr2_750xx_dvb_props = {
457 .frontend_attach = pvr2_s5h1409_attach,
458 .tuner_attach = pvr2_tda18271_8295_attach,
459};
460
461static const struct pvr2_dvb_props pvr2_751xx_dvb_props = {
462 .frontend_attach = pvr2_s5h1411_attach,
463 .tuner_attach = pvr2_tda18271_8295_attach,
464};
465#endif
466
467static const char *pvr2_fw1_names_75xxx[] = {
468 "v4l-pvrusb2-73xxx-01.fw",
469};
470
471static const struct pvr2_device_desc pvr2_device_750xx = {
472 .description = "WinTV HVR-1950 Model 750xx",
473 .shortname = "750xx",
474 .client_table.lst = pvr2_cli_73xxx,
475 .client_table.cnt = ARRAY_SIZE(pvr2_cli_73xxx),
476 .fx2_firmware.lst = pvr2_fw1_names_75xxx,
477 .fx2_firmware.cnt = ARRAY_SIZE(pvr2_fw1_names_75xxx),
478 .flag_has_cx25840 = !0,
479 .flag_has_hauppauge_rom = !0,
480 .flag_has_analogtuner = !0,
481 .flag_has_composite = !0,
482 .flag_has_svideo = !0,
483 .flag_fx2_16kb = !0,
484 .signal_routing_scheme = PVR2_ROUTING_SCHEME_HAUPPAUGE,
485 .digital_control_scheme = PVR2_DIGITAL_SCHEME_HAUPPAUGE,
486 .default_std_mask = V4L2_STD_NTSC_M,
487 .led_scheme = PVR2_LED_SCHEME_HAUPPAUGE,
488 .ir_scheme = PVR2_IR_SCHEME_ZILOG,
489#ifdef CONFIG_VIDEO_PVRUSB2_DVB
490 .dvb_props = &pvr2_750xx_dvb_props,
491#endif
492};
493
494static const struct pvr2_device_desc pvr2_device_751xx = {
495 .description = "WinTV HVR-1950 Model 751xx",
496 .shortname = "751xx",
497 .client_table.lst = pvr2_cli_73xxx,
498 .client_table.cnt = ARRAY_SIZE(pvr2_cli_73xxx),
499 .fx2_firmware.lst = pvr2_fw1_names_75xxx,
500 .fx2_firmware.cnt = ARRAY_SIZE(pvr2_fw1_names_75xxx),
501 .flag_has_cx25840 = !0,
502 .flag_has_hauppauge_rom = !0,
503 .flag_has_analogtuner = !0,
504 .flag_has_composite = !0,
505 .flag_has_svideo = !0,
506 .flag_fx2_16kb = !0,
507 .signal_routing_scheme = PVR2_ROUTING_SCHEME_HAUPPAUGE,
508 .digital_control_scheme = PVR2_DIGITAL_SCHEME_HAUPPAUGE,
509 .default_std_mask = V4L2_STD_NTSC_M,
510 .led_scheme = PVR2_LED_SCHEME_HAUPPAUGE,
511 .ir_scheme = PVR2_IR_SCHEME_ZILOG,
512#ifdef CONFIG_VIDEO_PVRUSB2_DVB
513 .dvb_props = &pvr2_751xx_dvb_props,
514#endif
515};
516
517
518
519/*------------------------------------------------------------------------*/
520
521struct usb_device_id pvr2_device_table[] = {
522 { USB_DEVICE(0x2040, 0x2900),
523 .driver_info = (kernel_ulong_t)&pvr2_device_29xxx},
524 { USB_DEVICE(0x2040, 0x2950), /* Logically identical to 2900 */
525 .driver_info = (kernel_ulong_t)&pvr2_device_29xxx},
526 { USB_DEVICE(0x2040, 0x2400),
527 .driver_info = (kernel_ulong_t)&pvr2_device_24xxx},
528 { USB_DEVICE(0x1164, 0x0622),
529 .driver_info = (kernel_ulong_t)&pvr2_device_gotview_2},
530 { USB_DEVICE(0x1164, 0x0602),
531 .driver_info = (kernel_ulong_t)&pvr2_device_gotview_2d},
532 { USB_DEVICE(0x11ba, 0x1003),
533 .driver_info = (kernel_ulong_t)&pvr2_device_onair_creator},
534 { USB_DEVICE(0x11ba, 0x1001),
535 .driver_info = (kernel_ulong_t)&pvr2_device_onair_usb2},
536 { USB_DEVICE(0x2040, 0x7300),
537 .driver_info = (kernel_ulong_t)&pvr2_device_73xxx},
538 { USB_DEVICE(0x2040, 0x7500),
539 .driver_info = (kernel_ulong_t)&pvr2_device_750xx},
540 { USB_DEVICE(0x2040, 0x7501),
541 .driver_info = (kernel_ulong_t)&pvr2_device_751xx},
542 { USB_DEVICE(0x0ccd, 0x0039),
543 .driver_info = (kernel_ulong_t)&pvr2_device_av400},
544 { }
545};
546
547MODULE_DEVICE_TABLE(usb, pvr2_device_table);
548
549
550/*
551 Stuff for Emacs to see, in order to encourage consistent editing style:
552 *** Local Variables: ***
553 *** mode: c ***
554 *** fill-column: 75 ***
555 *** tab-width: 8 ***
556 *** c-basic-offset: 8 ***
557 *** End: ***
558 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-devattr.h b/drivers/media/video/pvrusb2/pvrusb2-devattr.h
new file mode 100644
index 00000000000..273c8d4b385
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-devattr.h
@@ -0,0 +1,199 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20#ifndef __PVRUSB2_DEVATTR_H
21#define __PVRUSB2_DEVATTR_H
22
23#include <linux/mod_devicetable.h>
24#include <linux/videodev2.h>
25#ifdef CONFIG_VIDEO_PVRUSB2_DVB
26#include "pvrusb2-dvb.h"
27#endif
28
29/*
30
31 This header defines structures used to describe attributes of a device.
32
33*/
34
35
36#define PVR2_CLIENT_ID_NULL 0
37#define PVR2_CLIENT_ID_MSP3400 1
38#define PVR2_CLIENT_ID_CX25840 2
39#define PVR2_CLIENT_ID_SAA7115 3
40#define PVR2_CLIENT_ID_TUNER 4
41#define PVR2_CLIENT_ID_CS53L32A 5
42#define PVR2_CLIENT_ID_WM8775 6
43#define PVR2_CLIENT_ID_DEMOD 7
44
45struct pvr2_device_client_desc {
46 /* One ovr PVR2_CLIENT_ID_xxxx */
47 unsigned char module_id;
48
49 /* Null-terminated array of I2C addresses to try in order
50 initialize the module. It's safe to make this null terminated
51 since we're never going to encounter an i2c device with an
52 address of zero. If this is a null pointer or zero-length,
53 then no I2C addresses have been specified, in which case we'll
54 try some compiled in defaults for now. */
55 unsigned char *i2c_address_list;
56};
57
58struct pvr2_device_client_table {
59 const struct pvr2_device_client_desc *lst;
60 unsigned char cnt;
61};
62
63
64struct pvr2_string_table {
65 const char **lst;
66 unsigned int cnt;
67};
68
69#define PVR2_ROUTING_SCHEME_HAUPPAUGE 0
70#define PVR2_ROUTING_SCHEME_GOTVIEW 1
71#define PVR2_ROUTING_SCHEME_ONAIR 2
72#define PVR2_ROUTING_SCHEME_AV400 3
73
74#define PVR2_DIGITAL_SCHEME_NONE 0
75#define PVR2_DIGITAL_SCHEME_HAUPPAUGE 1
76#define PVR2_DIGITAL_SCHEME_ONAIR 2
77
78#define PVR2_LED_SCHEME_NONE 0
79#define PVR2_LED_SCHEME_HAUPPAUGE 1
80
81#define PVR2_IR_SCHEME_NONE 0
82#define PVR2_IR_SCHEME_24XXX 1 /* FX2-controlled IR */
83#define PVR2_IR_SCHEME_ZILOG 2 /* HVR-1950 style (must be taken out of reset) */
84#define PVR2_IR_SCHEME_24XXX_MCE 3 /* 24xxx MCE device */
85#define PVR2_IR_SCHEME_29XXX 4 /* Original 29xxx device */
86
87/* This describes a particular hardware type (except for the USB device ID
88 which must live in a separate structure due to environmental
89 constraints). See the top of pvrusb2-hdw.c for where this is
90 instantiated. */
91struct pvr2_device_desc {
92 /* Single line text description of hardware */
93 const char *description;
94
95 /* Single token identifier for hardware */
96 const char *shortname;
97
98 /* List of additional client modules we need to load */
99 struct pvr2_string_table client_modules;
100
101 /* List of defined client modules we need to load */
102 struct pvr2_device_client_table client_table;
103
104 /* List of FX2 firmware file names we should search; if empty then
105 FX2 firmware check / load is skipped and we assume the device
106 was initialized from internal ROM. */
107 struct pvr2_string_table fx2_firmware;
108
109#ifdef CONFIG_VIDEO_PVRUSB2_DVB
110 /* callback functions to handle attachment of digital tuner & demod */
111 const struct pvr2_dvb_props *dvb_props;
112
113#endif
114 /* Initial standard bits to use for this device, if not zero.
115 Anything set here is also implied as an available standard.
116 Note: This is ignored if overridden on the module load line via
117 the video_std module option. */
118 v4l2_std_id default_std_mask;
119
120 /* V4L tuner type ID to use with this device (only used if the
121 driver could not discover the type any other way). */
122 int default_tuner_type;
123
124 /* Signal routing scheme used by device, contains one of
125 PVR2_ROUTING_SCHEME_XXX. Schemes have to be defined as we
126 encounter them. This is an arbitrary integer scheme id; its
127 meaning is contained entirely within the driver and is
128 interpreted by logic which must send commands to the chip-level
129 drivers (search for things which touch this field). */
130 unsigned char signal_routing_scheme;
131
132 /* Indicates scheme for controlling device's LED (if any). The
133 driver will turn on the LED when streaming is underway. This
134 contains one of PVR2_LED_SCHEME_XXX. */
135 unsigned char led_scheme;
136
137 /* Control scheme to use if there is a digital tuner. This
138 contains one of PVR2_DIGITAL_SCHEME_XXX. This is an arbitrary
139 integer scheme id; its meaning is contained entirely within the
140 driver and is interpreted by logic which must control the
141 streaming pathway (search for things which touch this field). */
142 unsigned char digital_control_scheme;
143
144 /* If set, we don't bother trying to load cx23416 firmware. */
145 unsigned int flag_skip_cx23416_firmware:1;
146
147 /* If set, the encoder must be healthy in order for digital mode to
148 work (otherwise we assume that digital streaming will work even
149 if we fail to locate firmware for the encoder). If the device
150 doesn't support digital streaming then this flag has no
151 effect. */
152 unsigned int flag_digital_requires_cx23416:1;
153
154 /* Device has a hauppauge eeprom which we can interrogate. */
155 unsigned int flag_has_hauppauge_rom:1;
156
157 /* Device does not require a powerup command to be issued. */
158 unsigned int flag_no_powerup:1;
159
160 /* Device has a cx25840 - this enables special additional logic to
161 handle it. */
162 unsigned int flag_has_cx25840:1;
163
164 /* Device has a wm8775 - this enables special additional logic to
165 ensure that it is found. */
166 unsigned int flag_has_wm8775:1;
167
168 /* Indicate IR scheme of hardware. If not set, then it is assumed
169 that IR can work without any help from the driver. */
170 unsigned int ir_scheme:3;
171
172 /* These bits define which kinds of sources the device can handle.
173 Note: Digital tuner presence is inferred by the
174 digital_control_scheme enumeration. */
175 unsigned int flag_has_fmradio:1; /* Has FM radio receiver */
176 unsigned int flag_has_analogtuner:1; /* Has analog tuner */
177 unsigned int flag_has_composite:1; /* Has composite input */
178 unsigned int flag_has_svideo:1; /* Has s-video input */
179 unsigned int flag_fx2_16kb:1; /* 16KB FX2 firmware OK here */
180
181 /* If this driver is considered experimental, i.e. not all aspects
182 are working correctly and/or it is untested, mark that fact
183 with this flag. */
184 unsigned int flag_is_experimental:1;
185};
186
187extern struct usb_device_id pvr2_device_table[];
188
189#endif /* __PVRUSB2_HDW_INTERNAL_H */
190
191/*
192 Stuff for Emacs to see, in order to encourage consistent editing style:
193 *** Local Variables: ***
194 *** mode: c ***
195 *** fill-column: 75 ***
196 *** tab-width: 8 ***
197 *** c-basic-offset: 8 ***
198 *** End: ***
199 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-dvb.c b/drivers/media/video/pvrusb2/pvrusb2-dvb.c
new file mode 100644
index 00000000000..8c95793433e
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-dvb.c
@@ -0,0 +1,435 @@
1/*
2 * pvrusb2-dvb.c - linux-dvb api interface to the pvrusb2 driver.
3 *
4 * Copyright (C) 2007, 2008 Michael Krufky <mkrufky@linuxtv.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20
21#include <linux/kthread.h>
22#include <linux/freezer.h>
23#include <linux/slab.h>
24#include <linux/mm.h>
25#include "dvbdev.h"
26#include "pvrusb2-debug.h"
27#include "pvrusb2-hdw-internal.h"
28#include "pvrusb2-hdw.h"
29#include "pvrusb2-io.h"
30#include "pvrusb2-dvb.h"
31
32DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
33
34static int pvr2_dvb_feed_func(struct pvr2_dvb_adapter *adap)
35{
36 int ret;
37 unsigned int count;
38 struct pvr2_buffer *bp;
39 struct pvr2_stream *stream;
40
41 pvr2_trace(PVR2_TRACE_DVB_FEED, "dvb feed thread started");
42 set_freezable();
43
44 stream = adap->channel.stream->stream;
45
46 for (;;) {
47 if (kthread_should_stop()) break;
48
49 /* Not sure about this... */
50 try_to_freeze();
51
52 bp = pvr2_stream_get_ready_buffer(stream);
53 if (bp != NULL) {
54 count = pvr2_buffer_get_count(bp);
55 if (count) {
56 dvb_dmx_swfilter(
57 &adap->demux,
58 adap->buffer_storage[
59 pvr2_buffer_get_id(bp)],
60 count);
61 } else {
62 ret = pvr2_buffer_get_status(bp);
63 if (ret < 0) break;
64 }
65 ret = pvr2_buffer_queue(bp);
66 if (ret < 0) break;
67
68 /* Since we know we did something to a buffer,
69 just go back and try again. No point in
70 blocking unless we really ran out of
71 buffers to process. */
72 continue;
73 }
74
75
76 /* Wait until more buffers become available or we're
77 told not to wait any longer. */
78 ret = wait_event_interruptible(
79 adap->buffer_wait_data,
80 (pvr2_stream_get_ready_count(stream) > 0) ||
81 kthread_should_stop());
82 if (ret < 0) break;
83 }
84
85 /* If we get here and ret is < 0, then an error has occurred.
86 Probably would be a good idea to communicate that to DVB core... */
87
88 pvr2_trace(PVR2_TRACE_DVB_FEED, "dvb feed thread stopped");
89
90 return 0;
91}
92
93static int pvr2_dvb_feed_thread(void *data)
94{
95 int stat = pvr2_dvb_feed_func(data);
96 /* from videobuf-dvb.c: */
97 while (!kthread_should_stop()) {
98 set_current_state(TASK_INTERRUPTIBLE);
99 schedule();
100 }
101 return stat;
102}
103
104static void pvr2_dvb_notify(struct pvr2_dvb_adapter *adap)
105{
106 wake_up(&adap->buffer_wait_data);
107}
108
109static void pvr2_dvb_stream_end(struct pvr2_dvb_adapter *adap)
110{
111 unsigned int idx;
112 struct pvr2_stream *stream;
113
114 if (adap->thread) {
115 kthread_stop(adap->thread);
116 adap->thread = NULL;
117 }
118
119 if (adap->channel.stream) {
120 stream = adap->channel.stream->stream;
121 } else {
122 stream = NULL;
123 }
124 if (stream) {
125 pvr2_hdw_set_streaming(adap->channel.hdw, 0);
126 pvr2_stream_set_callback(stream, NULL, NULL);
127 pvr2_stream_kill(stream);
128 pvr2_stream_set_buffer_count(stream, 0);
129 pvr2_channel_claim_stream(&adap->channel, NULL);
130 }
131
132 if (adap->stream_run) {
133 for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
134 if (!(adap->buffer_storage[idx])) continue;
135 kfree(adap->buffer_storage[idx]);
136 adap->buffer_storage[idx] = NULL;
137 }
138 adap->stream_run = 0;
139 }
140}
141
142static int pvr2_dvb_stream_do_start(struct pvr2_dvb_adapter *adap)
143{
144 struct pvr2_context *pvr = adap->channel.mc_head;
145 unsigned int idx;
146 int ret;
147 struct pvr2_buffer *bp;
148 struct pvr2_stream *stream = NULL;
149
150 if (adap->stream_run) return -EIO;
151
152 ret = pvr2_channel_claim_stream(&adap->channel, &pvr->video_stream);
153 /* somebody else already has the stream */
154 if (ret < 0) return ret;
155
156 stream = adap->channel.stream->stream;
157
158 for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
159 adap->buffer_storage[idx] = kmalloc(PVR2_DVB_BUFFER_SIZE,
160 GFP_KERNEL);
161 if (!(adap->buffer_storage[idx])) return -ENOMEM;
162 }
163
164 pvr2_stream_set_callback(pvr->video_stream.stream,
165 (pvr2_stream_callback) pvr2_dvb_notify, adap);
166
167 ret = pvr2_stream_set_buffer_count(stream, PVR2_DVB_BUFFER_COUNT);
168 if (ret < 0) return ret;
169
170 for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
171 bp = pvr2_stream_get_buffer(stream, idx);
172 pvr2_buffer_set_buffer(bp,
173 adap->buffer_storage[idx],
174 PVR2_DVB_BUFFER_SIZE);
175 }
176
177 ret = pvr2_hdw_set_streaming(adap->channel.hdw, 1);
178 if (ret < 0) return ret;
179
180 while ((bp = pvr2_stream_get_idle_buffer(stream)) != NULL) {
181 ret = pvr2_buffer_queue(bp);
182 if (ret < 0) return ret;
183 }
184
185 adap->thread = kthread_run(pvr2_dvb_feed_thread, adap, "pvrusb2-dvb");
186
187 if (IS_ERR(adap->thread)) {
188 ret = PTR_ERR(adap->thread);
189 adap->thread = NULL;
190 return ret;
191 }
192
193 adap->stream_run = !0;
194
195 return 0;
196}
197
198static int pvr2_dvb_stream_start(struct pvr2_dvb_adapter *adap)
199{
200 int ret = pvr2_dvb_stream_do_start(adap);
201 if (ret < 0) pvr2_dvb_stream_end(adap);
202 return ret;
203}
204
205static int pvr2_dvb_ctrl_feed(struct dvb_demux_feed *dvbdmxfeed, int onoff)
206{
207 struct pvr2_dvb_adapter *adap = dvbdmxfeed->demux->priv;
208 int ret = 0;
209
210 if (adap == NULL) return -ENODEV;
211
212 mutex_lock(&adap->lock);
213 do {
214 if (onoff) {
215 if (!adap->feedcount) {
216 pvr2_trace(PVR2_TRACE_DVB_FEED,
217 "start feeding demux");
218 ret = pvr2_dvb_stream_start(adap);
219 if (ret < 0) break;
220 }
221 (adap->feedcount)++;
222 } else if (adap->feedcount > 0) {
223 (adap->feedcount)--;
224 if (!adap->feedcount) {
225 pvr2_trace(PVR2_TRACE_DVB_FEED,
226 "stop feeding demux");
227 pvr2_dvb_stream_end(adap);
228 }
229 }
230 } while (0);
231 mutex_unlock(&adap->lock);
232
233 return ret;
234}
235
236static int pvr2_dvb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
237{
238 pvr2_trace(PVR2_TRACE_DVB_FEED, "start pid: 0x%04x", dvbdmxfeed->pid);
239 return pvr2_dvb_ctrl_feed(dvbdmxfeed, 1);
240}
241
242static int pvr2_dvb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
243{
244 pvr2_trace(PVR2_TRACE_DVB_FEED, "stop pid: 0x%04x", dvbdmxfeed->pid);
245 return pvr2_dvb_ctrl_feed(dvbdmxfeed, 0);
246}
247
248static int pvr2_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
249{
250 struct pvr2_dvb_adapter *adap = fe->dvb->priv;
251 return pvr2_channel_limit_inputs(
252 &adap->channel,
253 (acquire ? (1 << PVR2_CVAL_INPUT_DTV) : 0));
254}
255
256static int pvr2_dvb_adapter_init(struct pvr2_dvb_adapter *adap)
257{
258 int ret;
259
260 ret = dvb_register_adapter(&adap->dvb_adap, "pvrusb2-dvb",
261 THIS_MODULE/*&hdw->usb_dev->owner*/,
262 &adap->channel.hdw->usb_dev->dev,
263 adapter_nr);
264 if (ret < 0) {
265 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
266 "dvb_register_adapter failed: error %d", ret);
267 goto err;
268 }
269 adap->dvb_adap.priv = adap;
270
271 adap->demux.dmx.capabilities = DMX_TS_FILTERING |
272 DMX_SECTION_FILTERING |
273 DMX_MEMORY_BASED_FILTERING;
274 adap->demux.priv = adap;
275 adap->demux.filternum = 256;
276 adap->demux.feednum = 256;
277 adap->demux.start_feed = pvr2_dvb_start_feed;
278 adap->demux.stop_feed = pvr2_dvb_stop_feed;
279 adap->demux.write_to_decoder = NULL;
280
281 ret = dvb_dmx_init(&adap->demux);
282 if (ret < 0) {
283 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
284 "dvb_dmx_init failed: error %d", ret);
285 goto err_dmx;
286 }
287
288 adap->dmxdev.filternum = adap->demux.filternum;
289 adap->dmxdev.demux = &adap->demux.dmx;
290 adap->dmxdev.capabilities = 0;
291
292 ret = dvb_dmxdev_init(&adap->dmxdev, &adap->dvb_adap);
293 if (ret < 0) {
294 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
295 "dvb_dmxdev_init failed: error %d", ret);
296 goto err_dmx_dev;
297 }
298
299 dvb_net_init(&adap->dvb_adap, &adap->dvb_net, &adap->demux.dmx);
300
301 return 0;
302
303err_dmx_dev:
304 dvb_dmx_release(&adap->demux);
305err_dmx:
306 dvb_unregister_adapter(&adap->dvb_adap);
307err:
308 return ret;
309}
310
311static int pvr2_dvb_adapter_exit(struct pvr2_dvb_adapter *adap)
312{
313 pvr2_trace(PVR2_TRACE_INFO, "unregistering DVB devices");
314 dvb_net_release(&adap->dvb_net);
315 adap->demux.dmx.close(&adap->demux.dmx);
316 dvb_dmxdev_release(&adap->dmxdev);
317 dvb_dmx_release(&adap->demux);
318 dvb_unregister_adapter(&adap->dvb_adap);
319 return 0;
320}
321
322static int pvr2_dvb_frontend_init(struct pvr2_dvb_adapter *adap)
323{
324 struct pvr2_hdw *hdw = adap->channel.hdw;
325 const struct pvr2_dvb_props *dvb_props = hdw->hdw_desc->dvb_props;
326 int ret = 0;
327
328 if (dvb_props == NULL) {
329 pvr2_trace(PVR2_TRACE_ERROR_LEGS, "fe_props not defined!");
330 return -EINVAL;
331 }
332
333 ret = pvr2_channel_limit_inputs(
334 &adap->channel,
335 (1 << PVR2_CVAL_INPUT_DTV));
336 if (ret) {
337 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
338 "failed to grab control of dtv input (code=%d)",
339 ret);
340 return ret;
341 }
342
343 if (dvb_props->frontend_attach == NULL) {
344 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
345 "frontend_attach not defined!");
346 ret = -EINVAL;
347 goto done;
348 }
349
350 if ((dvb_props->frontend_attach(adap) == 0) && (adap->fe)) {
351
352 if (dvb_register_frontend(&adap->dvb_adap, adap->fe)) {
353 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
354 "frontend registration failed!");
355 dvb_frontend_detach(adap->fe);
356 adap->fe = NULL;
357 ret = -ENODEV;
358 goto done;
359 }
360
361 if (dvb_props->tuner_attach)
362 dvb_props->tuner_attach(adap);
363
364 if (adap->fe->ops.analog_ops.standby)
365 adap->fe->ops.analog_ops.standby(adap->fe);
366
367 /* Ensure all frontends negotiate bus access */
368 adap->fe->ops.ts_bus_ctrl = pvr2_dvb_bus_ctrl;
369
370 } else {
371 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
372 "no frontend was attached!");
373 ret = -ENODEV;
374 return ret;
375 }
376
377 done:
378 pvr2_channel_limit_inputs(&adap->channel, 0);
379 return ret;
380}
381
382static int pvr2_dvb_frontend_exit(struct pvr2_dvb_adapter *adap)
383{
384 if (adap->fe != NULL) {
385 dvb_unregister_frontend(adap->fe);
386 dvb_frontend_detach(adap->fe);
387 }
388 return 0;
389}
390
391static void pvr2_dvb_destroy(struct pvr2_dvb_adapter *adap)
392{
393 pvr2_dvb_stream_end(adap);
394 pvr2_dvb_frontend_exit(adap);
395 pvr2_dvb_adapter_exit(adap);
396 pvr2_channel_done(&adap->channel);
397 kfree(adap);
398}
399
400static void pvr2_dvb_internal_check(struct pvr2_channel *chp)
401{
402 struct pvr2_dvb_adapter *adap;
403 adap = container_of(chp, struct pvr2_dvb_adapter, channel);
404 if (!adap->channel.mc_head->disconnect_flag) return;
405 pvr2_dvb_destroy(adap);
406}
407
408struct pvr2_dvb_adapter *pvr2_dvb_create(struct pvr2_context *pvr)
409{
410 int ret = 0;
411 struct pvr2_dvb_adapter *adap;
412 if (!pvr->hdw->hdw_desc->dvb_props) {
413 /* Device lacks a digital interface so don't set up
414 the DVB side of the driver either. For now. */
415 return NULL;
416 }
417 adap = kzalloc(sizeof(*adap), GFP_KERNEL);
418 if (!adap) return adap;
419 pvr2_channel_init(&adap->channel, pvr);
420 adap->channel.check_func = pvr2_dvb_internal_check;
421 init_waitqueue_head(&adap->buffer_wait_data);
422 mutex_init(&adap->lock);
423 ret = pvr2_dvb_adapter_init(adap);
424 if (ret < 0) goto fail1;
425 ret = pvr2_dvb_frontend_init(adap);
426 if (ret < 0) goto fail2;
427 return adap;
428
429fail2:
430 pvr2_dvb_adapter_exit(adap);
431fail1:
432 pvr2_channel_done(&adap->channel);
433 return NULL;
434}
435
diff --git a/drivers/media/video/pvrusb2/pvrusb2-dvb.h b/drivers/media/video/pvrusb2/pvrusb2-dvb.h
new file mode 100644
index 00000000000..884ff916a35
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-dvb.h
@@ -0,0 +1,41 @@
1#ifndef __PVRUSB2_DVB_H__
2#define __PVRUSB2_DVB_H__
3
4#include "dvb_frontend.h"
5#include "dvb_demux.h"
6#include "dvb_net.h"
7#include "dmxdev.h"
8#include "pvrusb2-context.h"
9
10#define PVR2_DVB_BUFFER_COUNT 32
11#define PVR2_DVB_BUFFER_SIZE PAGE_ALIGN(0x4000)
12
13struct pvr2_dvb_adapter {
14 struct pvr2_channel channel;
15
16 struct dvb_adapter dvb_adap;
17 struct dmxdev dmxdev;
18 struct dvb_demux demux;
19 struct dvb_net dvb_net;
20 struct dvb_frontend *fe;
21
22 int feedcount;
23 int max_feed_count;
24
25 struct task_struct *thread;
26 struct mutex lock;
27
28 unsigned int stream_run:1;
29
30 wait_queue_head_t buffer_wait_data;
31 char *buffer_storage[PVR2_DVB_BUFFER_COUNT];
32};
33
34struct pvr2_dvb_props {
35 int (*frontend_attach) (struct pvr2_dvb_adapter *);
36 int (*tuner_attach) (struct pvr2_dvb_adapter *);
37};
38
39struct pvr2_dvb_adapter *pvr2_dvb_create(struct pvr2_context *pvr);
40
41#endif /* __PVRUSB2_DVB_H__ */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-eeprom.c b/drivers/media/video/pvrusb2/pvrusb2-eeprom.c
new file mode 100644
index 00000000000..9515f3a68f8
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-eeprom.c
@@ -0,0 +1,164 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include <linux/slab.h>
23#include "pvrusb2-eeprom.h"
24#include "pvrusb2-hdw-internal.h"
25#include "pvrusb2-debug.h"
26
27#define trace_eeprom(...) pvr2_trace(PVR2_TRACE_EEPROM,__VA_ARGS__)
28
29
30
31/*
32
33 Read and analyze data in the eeprom. Use tveeprom to figure out
34 the packet structure, since this is another Hauppauge device and
35 internally it has a family resemblance to ivtv-type devices
36
37*/
38
39#include <media/tveeprom.h>
40
41/* We seem to only be interested in the last 128 bytes of the EEPROM */
42#define EEPROM_SIZE 128
43
44/* Grab EEPROM contents, needed for direct method. */
45static u8 *pvr2_eeprom_fetch(struct pvr2_hdw *hdw)
46{
47 struct i2c_msg msg[2];
48 u8 *eeprom;
49 u8 iadd[2];
50 u8 addr;
51 u16 eepromSize;
52 unsigned int offs;
53 int ret;
54 int mode16 = 0;
55 unsigned pcnt,tcnt;
56 eeprom = kmalloc(EEPROM_SIZE,GFP_KERNEL);
57 if (!eeprom) {
58 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
59 "Failed to allocate memory"
60 " required to read eeprom");
61 return NULL;
62 }
63
64 trace_eeprom("Value for eeprom addr from controller was 0x%x",
65 hdw->eeprom_addr);
66 addr = hdw->eeprom_addr;
67 /* Seems that if the high bit is set, then the *real* eeprom
68 address is shifted right now bit position (noticed this in
69 newer PVR USB2 hardware) */
70 if (addr & 0x80) addr >>= 1;
71
72 /* FX2 documentation states that a 16bit-addressed eeprom is
73 expected if the I2C address is an odd number (yeah, this is
74 strange but it's what they do) */
75 mode16 = (addr & 1);
76 eepromSize = (mode16 ? 4096 : 256);
77 trace_eeprom("Examining %d byte eeprom at location 0x%x"
78 " using %d bit addressing",eepromSize,addr,
79 mode16 ? 16 : 8);
80
81 msg[0].addr = addr;
82 msg[0].flags = 0;
83 msg[0].len = mode16 ? 2 : 1;
84 msg[0].buf = iadd;
85 msg[1].addr = addr;
86 msg[1].flags = I2C_M_RD;
87
88 /* We have to do the actual eeprom data fetch ourselves, because
89 (1) we're only fetching part of the eeprom, and (2) if we were
90 getting the whole thing our I2C driver can't grab it in one
91 pass - which is what tveeprom is otherwise going to attempt */
92 memset(eeprom,0,EEPROM_SIZE);
93 for (tcnt = 0; tcnt < EEPROM_SIZE; tcnt += pcnt) {
94 pcnt = 16;
95 if (pcnt + tcnt > EEPROM_SIZE) pcnt = EEPROM_SIZE-tcnt;
96 offs = tcnt + (eepromSize - EEPROM_SIZE);
97 if (mode16) {
98 iadd[0] = offs >> 8;
99 iadd[1] = offs;
100 } else {
101 iadd[0] = offs;
102 }
103 msg[1].len = pcnt;
104 msg[1].buf = eeprom+tcnt;
105 if ((ret = i2c_transfer(&hdw->i2c_adap,
106 msg,ARRAY_SIZE(msg))) != 2) {
107 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
108 "eeprom fetch set offs err=%d",ret);
109 kfree(eeprom);
110 return NULL;
111 }
112 }
113 return eeprom;
114}
115
116
117/* Directly call eeprom analysis function within tveeprom. */
118int pvr2_eeprom_analyze(struct pvr2_hdw *hdw)
119{
120 u8 *eeprom;
121 struct tveeprom tvdata;
122
123 memset(&tvdata,0,sizeof(tvdata));
124
125 eeprom = pvr2_eeprom_fetch(hdw);
126 if (!eeprom) return -EINVAL;
127
128 {
129 struct i2c_client fake_client;
130 /* Newer version expects a useless client interface */
131 fake_client.addr = hdw->eeprom_addr;
132 fake_client.adapter = &hdw->i2c_adap;
133 tveeprom_hauppauge_analog(&fake_client,&tvdata,eeprom);
134 }
135
136 trace_eeprom("eeprom assumed v4l tveeprom module");
137 trace_eeprom("eeprom direct call results:");
138 trace_eeprom("has_radio=%d",tvdata.has_radio);
139 trace_eeprom("tuner_type=%d",tvdata.tuner_type);
140 trace_eeprom("tuner_formats=0x%x",tvdata.tuner_formats);
141 trace_eeprom("audio_processor=%d",tvdata.audio_processor);
142 trace_eeprom("model=%d",tvdata.model);
143 trace_eeprom("revision=%d",tvdata.revision);
144 trace_eeprom("serial_number=%d",tvdata.serial_number);
145 trace_eeprom("rev_str=%s",tvdata.rev_str);
146 hdw->tuner_type = tvdata.tuner_type;
147 hdw->tuner_updated = !0;
148 hdw->serial_number = tvdata.serial_number;
149 hdw->std_mask_eeprom = tvdata.tuner_formats;
150
151 kfree(eeprom);
152
153 return 0;
154}
155
156/*
157 Stuff for Emacs to see, in order to encourage consistent editing style:
158 *** Local Variables: ***
159 *** mode: c ***
160 *** fill-column: 70 ***
161 *** tab-width: 8 ***
162 *** c-basic-offset: 8 ***
163 *** End: ***
164 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-eeprom.h b/drivers/media/video/pvrusb2/pvrusb2-eeprom.h
new file mode 100644
index 00000000000..cca3216f94c
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-eeprom.h
@@ -0,0 +1,39 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#ifndef __PVRUSB2_EEPROM_H
23#define __PVRUSB2_EEPROM_H
24
25struct pvr2_hdw;
26
27int pvr2_eeprom_analyze(struct pvr2_hdw *);
28
29#endif /* __PVRUSB2_EEPROM_H */
30
31/*
32 Stuff for Emacs to see, in order to encourage consistent editing style:
33 *** Local Variables: ***
34 *** mode: c ***
35 *** fill-column: 70 ***
36 *** tab-width: 8 ***
37 *** c-basic-offset: 8 ***
38 *** End: ***
39 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-encoder.c b/drivers/media/video/pvrusb2/pvrusb2-encoder.c
new file mode 100644
index 00000000000..e046fdaec5a
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-encoder.c
@@ -0,0 +1,552 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include <linux/device.h> // for linux/firmware.h
23#include <linux/firmware.h>
24#include "pvrusb2-util.h"
25#include "pvrusb2-encoder.h"
26#include "pvrusb2-hdw-internal.h"
27#include "pvrusb2-debug.h"
28#include "pvrusb2-fx2-cmd.h"
29
30
31
32/* Firmware mailbox flags - definitions found from ivtv */
33#define IVTV_MBOX_FIRMWARE_DONE 0x00000004
34#define IVTV_MBOX_DRIVER_DONE 0x00000002
35#define IVTV_MBOX_DRIVER_BUSY 0x00000001
36
37#define MBOX_BASE 0x44
38
39
40static int pvr2_encoder_write_words(struct pvr2_hdw *hdw,
41 unsigned int offs,
42 const u32 *data, unsigned int dlen)
43{
44 unsigned int idx,addr;
45 unsigned int bAddr;
46 int ret;
47 unsigned int chunkCnt;
48
49 /*
50
51 Format: First byte must be 0x01. Remaining 32 bit words are
52 spread out into chunks of 7 bytes each, with the first 4 bytes
53 being the data word (little endian), and the next 3 bytes
54 being the address where that data word is to be written (big
55 endian). Repeat request for additional words, with offset
56 adjusted accordingly.
57
58 */
59 while (dlen) {
60 chunkCnt = 8;
61 if (chunkCnt > dlen) chunkCnt = dlen;
62 memset(hdw->cmd_buffer,0,sizeof(hdw->cmd_buffer));
63 bAddr = 0;
64 hdw->cmd_buffer[bAddr++] = FX2CMD_MEM_WRITE_DWORD;
65 for (idx = 0; idx < chunkCnt; idx++) {
66 addr = idx + offs;
67 hdw->cmd_buffer[bAddr+6] = (addr & 0xffu);
68 hdw->cmd_buffer[bAddr+5] = ((addr>>8) & 0xffu);
69 hdw->cmd_buffer[bAddr+4] = ((addr>>16) & 0xffu);
70 PVR2_DECOMPOSE_LE(hdw->cmd_buffer, bAddr,data[idx]);
71 bAddr += 7;
72 }
73 ret = pvr2_send_request(hdw,
74 hdw->cmd_buffer,1+(chunkCnt*7),
75 NULL,0);
76 if (ret) return ret;
77 data += chunkCnt;
78 dlen -= chunkCnt;
79 offs += chunkCnt;
80 }
81
82 return 0;
83}
84
85
86static int pvr2_encoder_read_words(struct pvr2_hdw *hdw,
87 unsigned int offs,
88 u32 *data, unsigned int dlen)
89{
90 unsigned int idx;
91 int ret;
92 unsigned int chunkCnt;
93
94 /*
95
96 Format: First byte must be 0x02 (status check) or 0x28 (read
97 back block of 32 bit words). Next 6 bytes must be zero,
98 followed by a single byte of MBOX_BASE+offset for portion to
99 be read. Returned data is packed set of 32 bits words that
100 were read.
101
102 */
103
104 while (dlen) {
105 chunkCnt = 16;
106 if (chunkCnt > dlen) chunkCnt = dlen;
107 if (chunkCnt < 16) chunkCnt = 1;
108 hdw->cmd_buffer[0] =
109 ((chunkCnt == 1) ?
110 FX2CMD_MEM_READ_DWORD : FX2CMD_MEM_READ_64BYTES);
111 hdw->cmd_buffer[1] = 0;
112 hdw->cmd_buffer[2] = 0;
113 hdw->cmd_buffer[3] = 0;
114 hdw->cmd_buffer[4] = 0;
115 hdw->cmd_buffer[5] = ((offs>>16) & 0xffu);
116 hdw->cmd_buffer[6] = ((offs>>8) & 0xffu);
117 hdw->cmd_buffer[7] = (offs & 0xffu);
118 ret = pvr2_send_request(hdw,
119 hdw->cmd_buffer,8,
120 hdw->cmd_buffer,
121 (chunkCnt == 1 ? 4 : 16 * 4));
122 if (ret) return ret;
123
124 for (idx = 0; idx < chunkCnt; idx++) {
125 data[idx] = PVR2_COMPOSE_LE(hdw->cmd_buffer,idx*4);
126 }
127 data += chunkCnt;
128 dlen -= chunkCnt;
129 offs += chunkCnt;
130 }
131
132 return 0;
133}
134
135
136/* This prototype is set up to be compatible with the
137 cx2341x_mbox_func prototype in cx2341x.h, which should be in
138 kernels 2.6.18 or later. We do this so that we can enable
139 cx2341x.ko to write to our encoder (by handing it a pointer to this
140 function). For earlier kernels this doesn't really matter. */
141static int pvr2_encoder_cmd(void *ctxt,
142 u32 cmd,
143 int arg_cnt_send,
144 int arg_cnt_recv,
145 u32 *argp)
146{
147 unsigned int poll_count;
148 unsigned int try_count = 0;
149 int retry_flag;
150 int ret = 0;
151 unsigned int idx;
152 /* These sizes look to be limited by the FX2 firmware implementation */
153 u32 wrData[16];
154 u32 rdData[16];
155 struct pvr2_hdw *hdw = (struct pvr2_hdw *)ctxt;
156
157
158 /*
159
160 The encoder seems to speak entirely using blocks 32 bit words.
161 In ivtv driver terms, this is a mailbox at MBOX_BASE which we
162 populate with data and watch what the hardware does with it.
163 The first word is a set of flags used to control the
164 transaction, the second word is the command to execute, the
165 third byte is zero (ivtv driver suggests that this is some
166 kind of return value), and the fourth byte is a specified
167 timeout (windows driver always uses 0x00060000 except for one
168 case when it is zero). All successive words are the argument
169 words for the command.
170
171 First, write out the entire set of words, with the first word
172 being zero.
173
174 Next, write out just the first word again, but set it to
175 IVTV_MBOX_DRIVER_DONE | IVTV_DRIVER_BUSY this time (which
176 probably means "go").
177
178 Next, read back the return count words. Check the first word,
179 which should have IVTV_MBOX_FIRMWARE_DONE set. If however
180 that bit is not set, then the command isn't done so repeat the
181 read until it is set.
182
183 Finally, write out just the first word again, but set it to
184 0x0 this time (which probably means "idle").
185
186 */
187
188 if (arg_cnt_send > (ARRAY_SIZE(wrData) - 4)) {
189 pvr2_trace(
190 PVR2_TRACE_ERROR_LEGS,
191 "Failed to write cx23416 command"
192 " - too many input arguments"
193 " (was given %u limit %lu)",
194 arg_cnt_send, (long unsigned) ARRAY_SIZE(wrData) - 4);
195 return -EINVAL;
196 }
197
198 if (arg_cnt_recv > (ARRAY_SIZE(rdData) - 4)) {
199 pvr2_trace(
200 PVR2_TRACE_ERROR_LEGS,
201 "Failed to write cx23416 command"
202 " - too many return arguments"
203 " (was given %u limit %lu)",
204 arg_cnt_recv, (long unsigned) ARRAY_SIZE(rdData) - 4);
205 return -EINVAL;
206 }
207
208
209 LOCK_TAKE(hdw->ctl_lock); do {
210
211 if (!hdw->state_encoder_ok) {
212 ret = -EIO;
213 break;
214 }
215
216 retry_flag = 0;
217 try_count++;
218 ret = 0;
219 wrData[0] = 0;
220 wrData[1] = cmd;
221 wrData[2] = 0;
222 wrData[3] = 0x00060000;
223 for (idx = 0; idx < arg_cnt_send; idx++) {
224 wrData[idx+4] = argp[idx];
225 }
226 for (; idx < ARRAY_SIZE(wrData) - 4; idx++) {
227 wrData[idx+4] = 0;
228 }
229
230 ret = pvr2_encoder_write_words(hdw,MBOX_BASE,wrData,idx);
231 if (ret) break;
232 wrData[0] = IVTV_MBOX_DRIVER_DONE|IVTV_MBOX_DRIVER_BUSY;
233 ret = pvr2_encoder_write_words(hdw,MBOX_BASE,wrData,1);
234 if (ret) break;
235 poll_count = 0;
236 while (1) {
237 poll_count++;
238 ret = pvr2_encoder_read_words(hdw,MBOX_BASE,rdData,
239 arg_cnt_recv+4);
240 if (ret) {
241 break;
242 }
243 if (rdData[0] & IVTV_MBOX_FIRMWARE_DONE) {
244 break;
245 }
246 if (rdData[0] && (poll_count < 1000)) continue;
247 if (!rdData[0]) {
248 retry_flag = !0;
249 pvr2_trace(
250 PVR2_TRACE_ERROR_LEGS,
251 "Encoder timed out waiting for us"
252 "; arranging to retry");
253 } else {
254 pvr2_trace(
255 PVR2_TRACE_ERROR_LEGS,
256 "***WARNING*** device's encoder"
257 " appears to be stuck"
258 " (status=0x%08x)",rdData[0]);
259 }
260 pvr2_trace(
261 PVR2_TRACE_ERROR_LEGS,
262 "Encoder command: 0x%02x",cmd);
263 for (idx = 4; idx < arg_cnt_send; idx++) {
264 pvr2_trace(
265 PVR2_TRACE_ERROR_LEGS,
266 "Encoder arg%d: 0x%08x",
267 idx-3,wrData[idx]);
268 }
269 ret = -EBUSY;
270 break;
271 }
272 if (retry_flag) {
273 if (try_count < 20) continue;
274 pvr2_trace(
275 PVR2_TRACE_ERROR_LEGS,
276 "Too many retries...");
277 ret = -EBUSY;
278 }
279 if (ret) {
280 del_timer_sync(&hdw->encoder_run_timer);
281 hdw->state_encoder_ok = 0;
282 pvr2_trace(PVR2_TRACE_STBITS,
283 "State bit %s <-- %s",
284 "state_encoder_ok",
285 (hdw->state_encoder_ok ? "true" : "false"));
286 if (hdw->state_encoder_runok) {
287 hdw->state_encoder_runok = 0;
288 pvr2_trace(PVR2_TRACE_STBITS,
289 "State bit %s <-- %s",
290 "state_encoder_runok",
291 (hdw->state_encoder_runok ?
292 "true" : "false"));
293 }
294 pvr2_trace(
295 PVR2_TRACE_ERROR_LEGS,
296 "Giving up on command."
297 " This is normally recovered via a firmware"
298 " reload and re-initialization; concern"
299 " is only warranted if this happens repeatedly"
300 " and rapidly.");
301 break;
302 }
303 wrData[0] = 0x7;
304 for (idx = 0; idx < arg_cnt_recv; idx++) {
305 argp[idx] = rdData[idx+4];
306 }
307
308 wrData[0] = 0x0;
309 ret = pvr2_encoder_write_words(hdw,MBOX_BASE,wrData,1);
310 if (ret) break;
311
312 } while(0); LOCK_GIVE(hdw->ctl_lock);
313
314 return ret;
315}
316
317
318static int pvr2_encoder_vcmd(struct pvr2_hdw *hdw, int cmd,
319 int args, ...)
320{
321 va_list vl;
322 unsigned int idx;
323 u32 data[12];
324
325 if (args > ARRAY_SIZE(data)) {
326 pvr2_trace(
327 PVR2_TRACE_ERROR_LEGS,
328 "Failed to write cx23416 command"
329 " - too many arguments"
330 " (was given %u limit %lu)",
331 args, (long unsigned) ARRAY_SIZE(data));
332 return -EINVAL;
333 }
334
335 va_start(vl, args);
336 for (idx = 0; idx < args; idx++) {
337 data[idx] = va_arg(vl, u32);
338 }
339 va_end(vl);
340
341 return pvr2_encoder_cmd(hdw,cmd,args,0,data);
342}
343
344
345/* This implements some extra setup for the encoder that seems to be
346 specific to the PVR USB2 hardware. */
347static int pvr2_encoder_prep_config(struct pvr2_hdw *hdw)
348{
349 int ret = 0;
350 int encMisc3Arg = 0;
351
352#if 0
353 /* This inexplicable bit happens in the Hauppauge windows
354 driver (for both 24xxx and 29xxx devices). However I
355 currently see no difference in behavior with or without
356 this stuff. Leave this here as a note of its existence,
357 but don't use it. */
358 LOCK_TAKE(hdw->ctl_lock); do {
359 u32 dat[1];
360 dat[0] = 0x80000640;
361 pvr2_encoder_write_words(hdw,0x01fe,dat,1);
362 pvr2_encoder_write_words(hdw,0x023e,dat,1);
363 } while(0); LOCK_GIVE(hdw->ctl_lock);
364#endif
365
366 /* Mike Isely <isely@pobox.com> 26-Jan-2006 The windows driver
367 sends the following list of ENC_MISC commands (for both
368 24xxx and 29xxx devices). Meanings are not entirely clear,
369 however without the ENC_MISC(3,1) command then we risk
370 random perpetual video corruption whenever the video input
371 breaks up for a moment (like when switching channels). */
372
373
374#if 0
375 /* This ENC_MISC(5,0) command seems to hurt 29xxx sync
376 performance on channel changes, but is not a problem on
377 24xxx devices. */
378 ret |= pvr2_encoder_vcmd(hdw, CX2341X_ENC_MISC,4, 5,0,0,0);
379#endif
380
381 /* This ENC_MISC(3,encMisc3Arg) command is critical - without
382 it there will eventually be video corruption. Also, the
383 saa7115 case is strange - the Windows driver is passing 1
384 regardless of device type but if we have 1 for saa7115
385 devices the video turns sluggish. */
386 if (hdw->hdw_desc->flag_has_cx25840) {
387 encMisc3Arg = 1;
388 } else {
389 encMisc3Arg = 0;
390 }
391 ret |= pvr2_encoder_vcmd(hdw, CX2341X_ENC_MISC,4, 3,
392 encMisc3Arg,0,0);
393
394 ret |= pvr2_encoder_vcmd(hdw, CX2341X_ENC_MISC,4, 8,0,0,0);
395
396#if 0
397 /* This ENC_MISC(4,1) command is poisonous, so it is commented
398 out. But I'm leaving it here anyway to document its
399 existence in the Windows driver. The effect of this
400 command is that apps displaying the stream become sluggish
401 with stuttering video. */
402 ret |= pvr2_encoder_vcmd(hdw, CX2341X_ENC_MISC,4, 4,1,0,0);
403#endif
404
405 ret |= pvr2_encoder_vcmd(hdw, CX2341X_ENC_MISC,4, 0,3,0,0);
406 ret |= pvr2_encoder_vcmd(hdw, CX2341X_ENC_MISC,4,15,0,0,0);
407
408 /* prevent the PTSs from slowly drifting away in the generated
409 MPEG stream */
410 ret |= pvr2_encoder_vcmd(hdw, CX2341X_ENC_MISC, 2, 4, 1);
411
412 return ret;
413}
414
415int pvr2_encoder_adjust(struct pvr2_hdw *hdw)
416{
417 int ret;
418 ret = cx2341x_update(hdw,pvr2_encoder_cmd,
419 (hdw->enc_cur_valid ? &hdw->enc_cur_state : NULL),
420 &hdw->enc_ctl_state);
421 if (ret) {
422 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
423 "Error from cx2341x module code=%d",ret);
424 } else {
425 memcpy(&hdw->enc_cur_state,&hdw->enc_ctl_state,
426 sizeof(struct cx2341x_mpeg_params));
427 hdw->enc_cur_valid = !0;
428 }
429 return ret;
430}
431
432
433int pvr2_encoder_configure(struct pvr2_hdw *hdw)
434{
435 int ret;
436 int val;
437 pvr2_trace(PVR2_TRACE_ENCODER,"pvr2_encoder_configure"
438 " (cx2341x module)");
439 hdw->enc_ctl_state.port = CX2341X_PORT_STREAMING;
440 hdw->enc_ctl_state.width = hdw->res_hor_val;
441 hdw->enc_ctl_state.height = hdw->res_ver_val;
442 hdw->enc_ctl_state.is_50hz = ((hdw->std_mask_cur & V4L2_STD_525_60) ?
443 0 : 1);
444
445 ret = 0;
446
447 ret |= pvr2_encoder_prep_config(hdw);
448
449 /* saa7115: 0xf0 */
450 val = 0xf0;
451 if (hdw->hdw_desc->flag_has_cx25840) {
452 /* ivtv cx25840: 0x140 */
453 val = 0x140;
454 }
455
456 if (!ret) ret = pvr2_encoder_vcmd(
457 hdw,CX2341X_ENC_SET_NUM_VSYNC_LINES, 2,
458 val, val);
459
460 /* setup firmware to notify us about some events (don't know why...) */
461 if (!ret) ret = pvr2_encoder_vcmd(
462 hdw,CX2341X_ENC_SET_EVENT_NOTIFICATION, 4,
463 0, 0, 0x10000000, 0xffffffff);
464
465 if (!ret) ret = pvr2_encoder_vcmd(
466 hdw,CX2341X_ENC_SET_VBI_LINE, 5,
467 0xffffffff,0,0,0,0);
468
469 if (ret) {
470 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
471 "Failed to configure cx23416");
472 return ret;
473 }
474
475 ret = pvr2_encoder_adjust(hdw);
476 if (ret) return ret;
477
478 ret = pvr2_encoder_vcmd(
479 hdw, CX2341X_ENC_INITIALIZE_INPUT, 0);
480
481 if (ret) {
482 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
483 "Failed to initialize cx23416 video input");
484 return ret;
485 }
486
487 return 0;
488}
489
490
491int pvr2_encoder_start(struct pvr2_hdw *hdw)
492{
493 int status;
494
495 /* unmask some interrupts */
496 pvr2_write_register(hdw, 0x0048, 0xbfffffff);
497
498 pvr2_encoder_vcmd(hdw,CX2341X_ENC_MUTE_VIDEO,1,
499 hdw->input_val == PVR2_CVAL_INPUT_RADIO ? 1 : 0);
500
501 switch (hdw->active_stream_type) {
502 case pvr2_config_vbi:
503 status = pvr2_encoder_vcmd(hdw,CX2341X_ENC_START_CAPTURE,2,
504 0x01,0x14);
505 break;
506 case pvr2_config_mpeg:
507 status = pvr2_encoder_vcmd(hdw,CX2341X_ENC_START_CAPTURE,2,
508 0,0x13);
509 break;
510 default: /* Unhandled cases for now */
511 status = pvr2_encoder_vcmd(hdw,CX2341X_ENC_START_CAPTURE,2,
512 0,0x13);
513 break;
514 }
515 return status;
516}
517
518int pvr2_encoder_stop(struct pvr2_hdw *hdw)
519{
520 int status;
521
522 /* mask all interrupts */
523 pvr2_write_register(hdw, 0x0048, 0xffffffff);
524
525 switch (hdw->active_stream_type) {
526 case pvr2_config_vbi:
527 status = pvr2_encoder_vcmd(hdw,CX2341X_ENC_STOP_CAPTURE,3,
528 0x01,0x01,0x14);
529 break;
530 case pvr2_config_mpeg:
531 status = pvr2_encoder_vcmd(hdw,CX2341X_ENC_STOP_CAPTURE,3,
532 0x01,0,0x13);
533 break;
534 default: /* Unhandled cases for now */
535 status = pvr2_encoder_vcmd(hdw,CX2341X_ENC_STOP_CAPTURE,3,
536 0x01,0,0x13);
537 break;
538 }
539
540 return status;
541}
542
543
544/*
545 Stuff for Emacs to see, in order to encourage consistent editing style:
546 *** Local Variables: ***
547 *** mode: c ***
548 *** fill-column: 70 ***
549 *** tab-width: 8 ***
550 *** c-basic-offset: 8 ***
551 *** End: ***
552 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-encoder.h b/drivers/media/video/pvrusb2/pvrusb2-encoder.h
new file mode 100644
index 00000000000..232fefbcd1a
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-encoder.h
@@ -0,0 +1,42 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#ifndef __PVRUSB2_ENCODER_H
23#define __PVRUSB2_ENCODER_H
24
25struct pvr2_hdw;
26
27int pvr2_encoder_adjust(struct pvr2_hdw *);
28int pvr2_encoder_configure(struct pvr2_hdw *);
29int pvr2_encoder_start(struct pvr2_hdw *);
30int pvr2_encoder_stop(struct pvr2_hdw *);
31
32#endif /* __PVRUSB2_ENCODER_H */
33
34/*
35 Stuff for Emacs to see, in order to encourage consistent editing style:
36 *** Local Variables: ***
37 *** mode: c ***
38 *** fill-column: 70 ***
39 *** tab-width: 8 ***
40 *** c-basic-offset: 8 ***
41 *** End: ***
42 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-fx2-cmd.h b/drivers/media/video/pvrusb2/pvrusb2-fx2-cmd.h
new file mode 100644
index 00000000000..614755ea2ea
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-fx2-cmd.h
@@ -0,0 +1,72 @@
1/*
2 *
3 *
4 * Copyright (C) 2007 Michael Krufky <mkrufky@linuxtv.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20
21#ifndef _PVRUSB2_FX2_CMD_H_
22#define _PVRUSB2_FX2_CMD_H_
23
24#define FX2CMD_MEM_WRITE_DWORD 0x01u
25#define FX2CMD_MEM_READ_DWORD 0x02u
26
27#define FX2CMD_HCW_ZILOG_RESET 0x10u /* 1=reset 0=release */
28
29#define FX2CMD_MEM_READ_64BYTES 0x28u
30
31#define FX2CMD_REG_WRITE 0x04u
32#define FX2CMD_REG_READ 0x05u
33#define FX2CMD_MEMSEL 0x06u
34
35#define FX2CMD_I2C_WRITE 0x08u
36#define FX2CMD_I2C_READ 0x09u
37
38#define FX2CMD_GET_USB_SPEED 0x0bu
39
40#define FX2CMD_STREAMING_ON 0x36u
41#define FX2CMD_STREAMING_OFF 0x37u
42
43#define FX2CMD_FWPOST1 0x52u
44
45#define FX2CMD_POWER_OFF 0xdcu
46#define FX2CMD_POWER_ON 0xdeu
47
48#define FX2CMD_DEEP_RESET 0xddu
49
50#define FX2CMD_GET_EEPROM_ADDR 0xebu
51#define FX2CMD_GET_IR_CODE 0xecu
52
53#define FX2CMD_HCW_DEMOD_RESETIN 0xf0u
54#define FX2CMD_HCW_DTV_STREAMING_ON 0xf1u
55#define FX2CMD_HCW_DTV_STREAMING_OFF 0xf2u
56
57#define FX2CMD_ONAIR_DTV_STREAMING_ON 0xa0u
58#define FX2CMD_ONAIR_DTV_STREAMING_OFF 0xa1u
59#define FX2CMD_ONAIR_DTV_POWER_ON 0xa2u
60#define FX2CMD_ONAIR_DTV_POWER_OFF 0xa3u
61
62#endif /* _PVRUSB2_FX2_CMD_H_ */
63
64/*
65 Stuff for Emacs to see, in order to encourage consistent editing style:
66 *** Local Variables: ***
67 *** mode: c ***
68 *** fill-column: 75 ***
69 *** tab-width: 8 ***
70 *** c-basic-offset: 8 ***
71 *** End: ***
72 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-hdw-internal.h b/drivers/media/video/pvrusb2/pvrusb2-hdw-internal.h
new file mode 100644
index 00000000000..305e6aaa844
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-hdw-internal.h
@@ -0,0 +1,408 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20#ifndef __PVRUSB2_HDW_INTERNAL_H
21#define __PVRUSB2_HDW_INTERNAL_H
22
23/*
24
25 This header sets up all the internal structures and definitions needed to
26 track and coordinate the driver's interaction with the hardware. ONLY
27 source files which actually implement part of that whole circus should be
28 including this header. Higher levels, like the external layers to the
29 various public APIs (V4L, sysfs, etc) should NOT ever include this
30 private, internal header. This means that pvrusb2-hdw, pvrusb2-encoder,
31 etc will include this, but pvrusb2-v4l should not.
32
33*/
34
35#include <linux/videodev2.h>
36#include <linux/i2c.h>
37#include <linux/workqueue.h>
38#include <linux/mutex.h>
39#include "pvrusb2-hdw.h"
40#include "pvrusb2-io.h"
41#include <media/v4l2-device.h>
42#include <media/cx2341x.h>
43#include <media/ir-kbd-i2c.h>
44#include "pvrusb2-devattr.h"
45
46/* Legal values for PVR2_CID_HSM */
47#define PVR2_CVAL_HSM_FAIL 0
48#define PVR2_CVAL_HSM_FULL 1
49#define PVR2_CVAL_HSM_HIGH 2
50
51#define PVR2_VID_ENDPOINT 0x84
52#define PVR2_UNK_ENDPOINT 0x86 /* maybe raw yuv ? */
53#define PVR2_VBI_ENDPOINT 0x88
54
55#define PVR2_CTL_BUFFSIZE 64
56
57#define FREQTABLE_SIZE 500
58
59#define LOCK_TAKE(x) do { mutex_lock(&x##_mutex); x##_held = !0; } while (0)
60#define LOCK_GIVE(x) do { x##_held = 0; mutex_unlock(&x##_mutex); } while (0)
61
62typedef int (*pvr2_ctlf_is_dirty)(struct pvr2_ctrl *);
63typedef void (*pvr2_ctlf_clear_dirty)(struct pvr2_ctrl *);
64typedef int (*pvr2_ctlf_check_value)(struct pvr2_ctrl *,int);
65typedef int (*pvr2_ctlf_get_value)(struct pvr2_ctrl *,int *);
66typedef int (*pvr2_ctlf_set_value)(struct pvr2_ctrl *,int msk,int val);
67typedef int (*pvr2_ctlf_val_to_sym)(struct pvr2_ctrl *,int msk,int val,
68 char *,unsigned int,unsigned int *);
69typedef int (*pvr2_ctlf_sym_to_val)(struct pvr2_ctrl *,
70 const char *,unsigned int,
71 int *mskp,int *valp);
72typedef unsigned int (*pvr2_ctlf_get_v4lflags)(struct pvr2_ctrl *);
73
74/* This structure describes a specific control. A table of these is set up
75 in pvrusb2-hdw.c. */
76struct pvr2_ctl_info {
77 /* Control's name suitable for use as an identifier */
78 const char *name;
79
80 /* Short description of control */
81 const char *desc;
82
83 /* Control's implementation */
84 pvr2_ctlf_get_value get_value; /* Get its value */
85 pvr2_ctlf_get_value get_def_value; /* Get its default value */
86 pvr2_ctlf_get_value get_min_value; /* Get minimum allowed value */
87 pvr2_ctlf_get_value get_max_value; /* Get maximum allowed value */
88 pvr2_ctlf_set_value set_value; /* Set its value */
89 pvr2_ctlf_check_value check_value; /* Check that value is valid */
90 pvr2_ctlf_val_to_sym val_to_sym; /* Custom convert value->symbol */
91 pvr2_ctlf_sym_to_val sym_to_val; /* Custom convert symbol->value */
92 pvr2_ctlf_is_dirty is_dirty; /* Return true if dirty */
93 pvr2_ctlf_clear_dirty clear_dirty; /* Clear dirty state */
94 pvr2_ctlf_get_v4lflags get_v4lflags;/* Retrieve v4l flags */
95
96 /* Control's type (int, enum, bitmask) */
97 enum pvr2_ctl_type type;
98
99 /* Associated V4L control ID, if any */
100 int v4l_id;
101
102 /* Associated driver internal ID, if any */
103 int internal_id;
104
105 /* Don't implicitly initialize this control's value */
106 int skip_init;
107
108 /* Starting value for this control */
109 int default_value;
110
111 /* Type-specific control information */
112 union {
113 struct { /* Integer control */
114 long min_value; /* lower limit */
115 long max_value; /* upper limit */
116 } type_int;
117 struct { /* enumerated control */
118 unsigned int count; /* enum value count */
119 const char * const *value_names; /* symbol names */
120 } type_enum;
121 struct { /* bitmask control */
122 unsigned int valid_bits; /* bits in use */
123 const char **bit_names; /* symbol name/bit */
124 } type_bitmask;
125 } def;
126};
127
128
129/* Same as pvr2_ctl_info, but includes storage for the control description */
130#define PVR2_CTLD_INFO_DESC_SIZE 32
131struct pvr2_ctld_info {
132 struct pvr2_ctl_info info;
133 char desc[PVR2_CTLD_INFO_DESC_SIZE];
134};
135
136struct pvr2_ctrl {
137 const struct pvr2_ctl_info *info;
138 struct pvr2_hdw *hdw;
139};
140
141
142
143/* Disposition of firmware1 loading situation */
144#define FW1_STATE_UNKNOWN 0
145#define FW1_STATE_MISSING 1
146#define FW1_STATE_FAILED 2
147#define FW1_STATE_RELOAD 3
148#define FW1_STATE_OK 4
149
150/* What state the device is in if it is a hybrid */
151#define PVR2_PATHWAY_UNKNOWN 0
152#define PVR2_PATHWAY_ANALOG 1
153#define PVR2_PATHWAY_DIGITAL 2
154
155typedef int (*pvr2_i2c_func)(struct pvr2_hdw *,u8,u8 *,u16,u8 *, u16);
156#define PVR2_I2C_FUNC_CNT 128
157
158/* This structure contains all state data directly needed to
159 manipulate the hardware (as opposed to complying with a kernel
160 interface) */
161struct pvr2_hdw {
162 /* Underlying USB device handle */
163 struct usb_device *usb_dev;
164 struct usb_interface *usb_intf;
165
166 /* Our handle into the v4l2 sub-device architecture */
167 struct v4l2_device v4l2_dev;
168 /* Device description, anything that must adjust behavior based on
169 device specific info will use information held here. */
170 const struct pvr2_device_desc *hdw_desc;
171
172 /* Kernel worker thread handling */
173 struct workqueue_struct *workqueue;
174 struct work_struct workpoll; /* Update driver state */
175
176 /* Video spigot */
177 struct pvr2_stream *vid_stream;
178
179 /* Mutex for all hardware state control */
180 struct mutex big_lock_mutex;
181 int big_lock_held; /* For debugging */
182
183 /* This is a simple string which identifies the instance of this
184 driver. It is unique within the set of existing devices, but
185 there is no attempt to keep the name consistent with the same
186 physical device each time. */
187 char name[32];
188
189 /* This is a simple string which identifies the physical device
190 instance itself - if possible. (If not possible, then it is
191 based on the specific driver instance, similar to name above.)
192 The idea here is that userspace might hopefully be able to use
193 this recognize specific tuners. It will encode a serial number,
194 if available. */
195 char identifier[32];
196
197 /* I2C stuff */
198 struct i2c_adapter i2c_adap;
199 struct i2c_algorithm i2c_algo;
200 pvr2_i2c_func i2c_func[PVR2_I2C_FUNC_CNT];
201 int i2c_cx25840_hack_state;
202 int i2c_linked;
203
204 /* IR related */
205 unsigned int ir_scheme_active; /* IR scheme as seen from the outside */
206 struct IR_i2c_init_data ir_init_data; /* params passed to IR modules */
207
208 /* Frequency table */
209 unsigned int freqTable[FREQTABLE_SIZE];
210 unsigned int freqProgSlot;
211
212 /* Stuff for handling low level control interaction with device */
213 struct mutex ctl_lock_mutex;
214 int ctl_lock_held; /* For debugging */
215 struct urb *ctl_write_urb;
216 struct urb *ctl_read_urb;
217 unsigned char *ctl_write_buffer;
218 unsigned char *ctl_read_buffer;
219 int ctl_write_pend_flag;
220 int ctl_read_pend_flag;
221 int ctl_timeout_flag;
222 struct completion ctl_done;
223 unsigned char cmd_buffer[PVR2_CTL_BUFFSIZE];
224 int cmd_debug_state; // Low level command debugging info
225 unsigned char cmd_debug_code; //
226 unsigned int cmd_debug_write_len; //
227 unsigned int cmd_debug_read_len; //
228
229 /* Bits of state that describe what is going on with various parts
230 of the driver. */
231 int state_pathway_ok; /* Pathway config is ok */
232 int state_encoder_ok; /* Encoder is operational */
233 int state_encoder_run; /* Encoder is running */
234 int state_encoder_config; /* Encoder is configured */
235 int state_encoder_waitok; /* Encoder pre-wait done */
236 int state_encoder_runok; /* Encoder has run for >= .25 sec */
237 int state_decoder_run; /* Decoder is running */
238 int state_decoder_ready; /* Decoder is stabilized & streamable */
239 int state_usbstream_run; /* FX2 is streaming */
240 int state_decoder_quiescent; /* Decoder idle for minimal interval */
241 int state_pipeline_config; /* Pipeline is configured */
242 int state_pipeline_req; /* Somebody wants to stream */
243 int state_pipeline_pause; /* Pipeline must be paused */
244 int state_pipeline_idle; /* Pipeline not running */
245
246 /* This is the master state of the driver. It is the combined
247 result of other bits of state. Examining this will indicate the
248 overall state of the driver. Values here are one of
249 PVR2_STATE_xxxx */
250 unsigned int master_state;
251
252 /* True if device led is currently on */
253 int led_on;
254
255 /* True if states must be re-evaluated */
256 int state_stale;
257
258 void (*state_func)(void *);
259 void *state_data;
260
261 /* Timer for measuring required decoder settling time before we're
262 allowed to fire it up again. */
263 struct timer_list quiescent_timer;
264
265 /* Timer for measuring decoder stabilization time, which is the
266 amount of time we need to let the decoder run before we can
267 trust its output (otherwise the encoder might see garbage and
268 then fail to start correctly). */
269 struct timer_list decoder_stabilization_timer;
270
271 /* Timer for measuring encoder pre-wait time */
272 struct timer_list encoder_wait_timer;
273
274 /* Timer for measuring encoder minimum run time */
275 struct timer_list encoder_run_timer;
276
277 /* Place to block while waiting for state changes */
278 wait_queue_head_t state_wait_data;
279
280
281 int force_dirty; /* consider all controls dirty if true */
282 int flag_ok; /* device in known good state */
283 int flag_modulefail; /* true if at least one module failed to load */
284 int flag_disconnected; /* flag_ok == 0 due to disconnect */
285 int flag_init_ok; /* true if structure is fully initialized */
286 int fw1_state; /* current situation with fw1 */
287 int pathway_state; /* one of PVR2_PATHWAY_xxx */
288 int flag_decoder_missed;/* We've noticed missing decoder */
289 int flag_tripped; /* Indicates overall failure to start */
290
291 unsigned int decoder_client_id;
292
293 // CPU firmware info (used to help find / save firmware data)
294 char *fw_buffer;
295 unsigned int fw_size;
296 int fw_cpu_flag; /* True if we are dealing with the CPU */
297
298 /* Tuner / frequency control stuff */
299 unsigned int tuner_type;
300 int tuner_updated;
301 unsigned int freqValTelevision; /* Current freq for tv mode */
302 unsigned int freqValRadio; /* Current freq for radio mode */
303 unsigned int freqSlotTelevision; /* Current slot for tv mode */
304 unsigned int freqSlotRadio; /* Current slot for radio mode */
305 unsigned int freqSelector; /* 0=radio 1=television */
306 int freqDirty;
307
308 /* Current tuner info - this information is polled from the I2C bus */
309 struct v4l2_tuner tuner_signal_info;
310 int tuner_signal_stale;
311
312 /* Cropping capability info */
313 struct v4l2_cropcap cropcap_info;
314 int cropcap_stale;
315
316 /* Video standard handling */
317 v4l2_std_id std_mask_eeprom; // Hardware supported selections
318 v4l2_std_id std_mask_avail; // Which standards we may select from
319 v4l2_std_id std_mask_cur; // Currently selected standard(s)
320 unsigned int std_enum_cnt; // # of enumerated standards
321 int std_enum_cur; // selected standard enumeration value
322 int std_dirty; // True if std_mask_cur has changed
323 struct pvr2_ctl_info std_info_enum;
324 struct pvr2_ctl_info std_info_avail;
325 struct pvr2_ctl_info std_info_cur;
326 struct v4l2_standard *std_defs;
327 const char **std_enum_names;
328
329 // Generated string names, one per actual V4L2 standard
330 const char *std_mask_ptrs[32];
331 char std_mask_names[32][10];
332
333 int unit_number; /* ID for driver instance */
334 unsigned long serial_number; /* ID for hardware itself */
335
336 char bus_info[32]; /* Bus location info */
337
338 /* Minor numbers used by v4l logic (yes, this is a hack, as there
339 should be no v4l junk here). Probably a better way to do this. */
340 int v4l_minor_number_video;
341 int v4l_minor_number_vbi;
342 int v4l_minor_number_radio;
343
344 /* Bit mask of PVR2_CVAL_INPUT choices which are valid for the hardware */
345 unsigned int input_avail_mask;
346 /* Bit mask of PVR2_CVAL_INPUT choices which are currently allowed */
347 unsigned int input_allowed_mask;
348
349 /* Location of eeprom or a negative number if none */
350 int eeprom_addr;
351
352 enum pvr2_config active_stream_type;
353 enum pvr2_config desired_stream_type;
354
355 /* Control state needed for cx2341x module */
356 struct cx2341x_mpeg_params enc_cur_state;
357 struct cx2341x_mpeg_params enc_ctl_state;
358 /* True if an encoder attribute has changed */
359 int enc_stale;
360 /* True if an unsafe encoder attribute has changed */
361 int enc_unsafe_stale;
362 /* True if enc_cur_state is valid */
363 int enc_cur_valid;
364
365 /* Control state */
366#define VCREATE_DATA(lab) int lab##_val; int lab##_dirty
367 VCREATE_DATA(brightness);
368 VCREATE_DATA(contrast);
369 VCREATE_DATA(saturation);
370 VCREATE_DATA(hue);
371 VCREATE_DATA(volume);
372 VCREATE_DATA(balance);
373 VCREATE_DATA(bass);
374 VCREATE_DATA(treble);
375 VCREATE_DATA(mute);
376 VCREATE_DATA(cropl);
377 VCREATE_DATA(cropt);
378 VCREATE_DATA(cropw);
379 VCREATE_DATA(croph);
380 VCREATE_DATA(input);
381 VCREATE_DATA(audiomode);
382 VCREATE_DATA(res_hor);
383 VCREATE_DATA(res_ver);
384 VCREATE_DATA(srate);
385#undef VCREATE_DATA
386
387 struct pvr2_ctld_info *mpeg_ctrl_info;
388
389 struct pvr2_ctrl *controls;
390 unsigned int control_cnt;
391};
392
393/* This function gets the current frequency */
394unsigned long pvr2_hdw_get_cur_freq(struct pvr2_hdw *);
395
396void pvr2_hdw_status_poll(struct pvr2_hdw *);
397
398#endif /* __PVRUSB2_HDW_INTERNAL_H */
399
400/*
401 Stuff for Emacs to see, in order to encourage consistent editing style:
402 *** Local Variables: ***
403 *** mode: c ***
404 *** fill-column: 75 ***
405 *** tab-width: 8 ***
406 *** c-basic-offset: 8 ***
407 *** End: ***
408 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-hdw.c b/drivers/media/video/pvrusb2/pvrusb2-hdw.c
new file mode 100644
index 00000000000..e98d3821279
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-hdw.c
@@ -0,0 +1,5322 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20
21#include <linux/errno.h>
22#include <linux/string.h>
23#include <linux/slab.h>
24#include <linux/firmware.h>
25#include <linux/videodev2.h>
26#include <media/v4l2-common.h>
27#include <media/tuner.h>
28#include "pvrusb2.h"
29#include "pvrusb2-std.h"
30#include "pvrusb2-util.h"
31#include "pvrusb2-hdw.h"
32#include "pvrusb2-i2c-core.h"
33#include "pvrusb2-eeprom.h"
34#include "pvrusb2-hdw-internal.h"
35#include "pvrusb2-encoder.h"
36#include "pvrusb2-debug.h"
37#include "pvrusb2-fx2-cmd.h"
38#include "pvrusb2-wm8775.h"
39#include "pvrusb2-video-v4l.h"
40#include "pvrusb2-cx2584x-v4l.h"
41#include "pvrusb2-cs53l32a.h"
42#include "pvrusb2-audio.h"
43
44#define TV_MIN_FREQ 55250000L
45#define TV_MAX_FREQ 850000000L
46
47/* This defines a minimum interval that the decoder must remain quiet
48 before we are allowed to start it running. */
49#define TIME_MSEC_DECODER_WAIT 50
50
51/* This defines a minimum interval that the decoder must be allowed to run
52 before we can safely begin using its streaming output. */
53#define TIME_MSEC_DECODER_STABILIZATION_WAIT 300
54
55/* This defines a minimum interval that the encoder must remain quiet
56 before we are allowed to configure it. */
57#define TIME_MSEC_ENCODER_WAIT 50
58
59/* This defines the minimum interval that the encoder must successfully run
60 before we consider that the encoder has run at least once since its
61 firmware has been loaded. This measurement is in important for cases
62 where we can't do something until we know that the encoder has been run
63 at least once. */
64#define TIME_MSEC_ENCODER_OK 250
65
66static struct pvr2_hdw *unit_pointers[PVR_NUM] = {[ 0 ... PVR_NUM-1 ] = NULL};
67static DEFINE_MUTEX(pvr2_unit_mtx);
68
69static int ctlchg;
70static int procreload;
71static int tuner[PVR_NUM] = { [0 ... PVR_NUM-1] = -1 };
72static int tolerance[PVR_NUM] = { [0 ... PVR_NUM-1] = 0 };
73static int video_std[PVR_NUM] = { [0 ... PVR_NUM-1] = 0 };
74static int init_pause_msec;
75
76module_param(ctlchg, int, S_IRUGO|S_IWUSR);
77MODULE_PARM_DESC(ctlchg, "0=optimize ctl change 1=always accept new ctl value");
78module_param(init_pause_msec, int, S_IRUGO|S_IWUSR);
79MODULE_PARM_DESC(init_pause_msec, "hardware initialization settling delay");
80module_param(procreload, int, S_IRUGO|S_IWUSR);
81MODULE_PARM_DESC(procreload,
82 "Attempt init failure recovery with firmware reload");
83module_param_array(tuner, int, NULL, 0444);
84MODULE_PARM_DESC(tuner,"specify installed tuner type");
85module_param_array(video_std, int, NULL, 0444);
86MODULE_PARM_DESC(video_std,"specify initial video standard");
87module_param_array(tolerance, int, NULL, 0444);
88MODULE_PARM_DESC(tolerance,"specify stream error tolerance");
89
90/* US Broadcast channel 3 (61.25 MHz), to help with testing */
91static int default_tv_freq = 61250000L;
92/* 104.3 MHz, a usable FM station for my area */
93static int default_radio_freq = 104300000L;
94
95module_param_named(tv_freq, default_tv_freq, int, 0444);
96MODULE_PARM_DESC(tv_freq, "specify initial television frequency");
97module_param_named(radio_freq, default_radio_freq, int, 0444);
98MODULE_PARM_DESC(radio_freq, "specify initial radio frequency");
99
100#define PVR2_CTL_WRITE_ENDPOINT 0x01
101#define PVR2_CTL_READ_ENDPOINT 0x81
102
103#define PVR2_GPIO_IN 0x9008
104#define PVR2_GPIO_OUT 0x900c
105#define PVR2_GPIO_DIR 0x9020
106
107#define trace_firmware(...) pvr2_trace(PVR2_TRACE_FIRMWARE,__VA_ARGS__)
108
109#define PVR2_FIRMWARE_ENDPOINT 0x02
110
111/* size of a firmware chunk */
112#define FIRMWARE_CHUNK_SIZE 0x2000
113
114typedef void (*pvr2_subdev_update_func)(struct pvr2_hdw *,
115 struct v4l2_subdev *);
116
117static const pvr2_subdev_update_func pvr2_module_update_functions[] = {
118 [PVR2_CLIENT_ID_WM8775] = pvr2_wm8775_subdev_update,
119 [PVR2_CLIENT_ID_SAA7115] = pvr2_saa7115_subdev_update,
120 [PVR2_CLIENT_ID_MSP3400] = pvr2_msp3400_subdev_update,
121 [PVR2_CLIENT_ID_CX25840] = pvr2_cx25840_subdev_update,
122 [PVR2_CLIENT_ID_CS53L32A] = pvr2_cs53l32a_subdev_update,
123};
124
125static const char *module_names[] = {
126 [PVR2_CLIENT_ID_MSP3400] = "msp3400",
127 [PVR2_CLIENT_ID_CX25840] = "cx25840",
128 [PVR2_CLIENT_ID_SAA7115] = "saa7115",
129 [PVR2_CLIENT_ID_TUNER] = "tuner",
130 [PVR2_CLIENT_ID_DEMOD] = "tuner",
131 [PVR2_CLIENT_ID_CS53L32A] = "cs53l32a",
132 [PVR2_CLIENT_ID_WM8775] = "wm8775",
133};
134
135
136static const unsigned char *module_i2c_addresses[] = {
137 [PVR2_CLIENT_ID_TUNER] = "\x60\x61\x62\x63",
138 [PVR2_CLIENT_ID_DEMOD] = "\x43",
139 [PVR2_CLIENT_ID_MSP3400] = "\x40",
140 [PVR2_CLIENT_ID_SAA7115] = "\x21",
141 [PVR2_CLIENT_ID_WM8775] = "\x1b",
142 [PVR2_CLIENT_ID_CX25840] = "\x44",
143 [PVR2_CLIENT_ID_CS53L32A] = "\x11",
144};
145
146
147static const char *ir_scheme_names[] = {
148 [PVR2_IR_SCHEME_NONE] = "none",
149 [PVR2_IR_SCHEME_29XXX] = "29xxx",
150 [PVR2_IR_SCHEME_24XXX] = "24xxx (29xxx emulation)",
151 [PVR2_IR_SCHEME_24XXX_MCE] = "24xxx (MCE device)",
152 [PVR2_IR_SCHEME_ZILOG] = "Zilog",
153};
154
155
156/* Define the list of additional controls we'll dynamically construct based
157 on query of the cx2341x module. */
158struct pvr2_mpeg_ids {
159 const char *strid;
160 int id;
161};
162static const struct pvr2_mpeg_ids mpeg_ids[] = {
163 {
164 .strid = "audio_layer",
165 .id = V4L2_CID_MPEG_AUDIO_ENCODING,
166 },{
167 .strid = "audio_bitrate",
168 .id = V4L2_CID_MPEG_AUDIO_L2_BITRATE,
169 },{
170 /* Already using audio_mode elsewhere :-( */
171 .strid = "mpeg_audio_mode",
172 .id = V4L2_CID_MPEG_AUDIO_MODE,
173 },{
174 .strid = "mpeg_audio_mode_extension",
175 .id = V4L2_CID_MPEG_AUDIO_MODE_EXTENSION,
176 },{
177 .strid = "audio_emphasis",
178 .id = V4L2_CID_MPEG_AUDIO_EMPHASIS,
179 },{
180 .strid = "audio_crc",
181 .id = V4L2_CID_MPEG_AUDIO_CRC,
182 },{
183 .strid = "video_aspect",
184 .id = V4L2_CID_MPEG_VIDEO_ASPECT,
185 },{
186 .strid = "video_b_frames",
187 .id = V4L2_CID_MPEG_VIDEO_B_FRAMES,
188 },{
189 .strid = "video_gop_size",
190 .id = V4L2_CID_MPEG_VIDEO_GOP_SIZE,
191 },{
192 .strid = "video_gop_closure",
193 .id = V4L2_CID_MPEG_VIDEO_GOP_CLOSURE,
194 },{
195 .strid = "video_bitrate_mode",
196 .id = V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
197 },{
198 .strid = "video_bitrate",
199 .id = V4L2_CID_MPEG_VIDEO_BITRATE,
200 },{
201 .strid = "video_bitrate_peak",
202 .id = V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
203 },{
204 .strid = "video_temporal_decimation",
205 .id = V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION,
206 },{
207 .strid = "stream_type",
208 .id = V4L2_CID_MPEG_STREAM_TYPE,
209 },{
210 .strid = "video_spatial_filter_mode",
211 .id = V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE,
212 },{
213 .strid = "video_spatial_filter",
214 .id = V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER,
215 },{
216 .strid = "video_luma_spatial_filter_type",
217 .id = V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE,
218 },{
219 .strid = "video_chroma_spatial_filter_type",
220 .id = V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE,
221 },{
222 .strid = "video_temporal_filter_mode",
223 .id = V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE,
224 },{
225 .strid = "video_temporal_filter",
226 .id = V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER,
227 },{
228 .strid = "video_median_filter_type",
229 .id = V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE,
230 },{
231 .strid = "video_luma_median_filter_top",
232 .id = V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_TOP,
233 },{
234 .strid = "video_luma_median_filter_bottom",
235 .id = V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_BOTTOM,
236 },{
237 .strid = "video_chroma_median_filter_top",
238 .id = V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_TOP,
239 },{
240 .strid = "video_chroma_median_filter_bottom",
241 .id = V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_BOTTOM,
242 }
243};
244#define MPEGDEF_COUNT ARRAY_SIZE(mpeg_ids)
245
246
247static const char *control_values_srate[] = {
248 [V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100] = "44.1 kHz",
249 [V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000] = "48 kHz",
250 [V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000] = "32 kHz",
251};
252
253
254
255static const char *control_values_input[] = {
256 [PVR2_CVAL_INPUT_TV] = "television", /*xawtv needs this name*/
257 [PVR2_CVAL_INPUT_DTV] = "dtv",
258 [PVR2_CVAL_INPUT_RADIO] = "radio",
259 [PVR2_CVAL_INPUT_SVIDEO] = "s-video",
260 [PVR2_CVAL_INPUT_COMPOSITE] = "composite",
261};
262
263
264static const char *control_values_audiomode[] = {
265 [V4L2_TUNER_MODE_MONO] = "Mono",
266 [V4L2_TUNER_MODE_STEREO] = "Stereo",
267 [V4L2_TUNER_MODE_LANG1] = "Lang1",
268 [V4L2_TUNER_MODE_LANG2] = "Lang2",
269 [V4L2_TUNER_MODE_LANG1_LANG2] = "Lang1+Lang2",
270};
271
272
273static const char *control_values_hsm[] = {
274 [PVR2_CVAL_HSM_FAIL] = "Fail",
275 [PVR2_CVAL_HSM_HIGH] = "High",
276 [PVR2_CVAL_HSM_FULL] = "Full",
277};
278
279
280static const char *pvr2_state_names[] = {
281 [PVR2_STATE_NONE] = "none",
282 [PVR2_STATE_DEAD] = "dead",
283 [PVR2_STATE_COLD] = "cold",
284 [PVR2_STATE_WARM] = "warm",
285 [PVR2_STATE_ERROR] = "error",
286 [PVR2_STATE_READY] = "ready",
287 [PVR2_STATE_RUN] = "run",
288};
289
290
291struct pvr2_fx2cmd_descdef {
292 unsigned char id;
293 unsigned char *desc;
294};
295
296static const struct pvr2_fx2cmd_descdef pvr2_fx2cmd_desc[] = {
297 {FX2CMD_MEM_WRITE_DWORD, "write encoder dword"},
298 {FX2CMD_MEM_READ_DWORD, "read encoder dword"},
299 {FX2CMD_HCW_ZILOG_RESET, "zilog IR reset control"},
300 {FX2CMD_MEM_READ_64BYTES, "read encoder 64bytes"},
301 {FX2CMD_REG_WRITE, "write encoder register"},
302 {FX2CMD_REG_READ, "read encoder register"},
303 {FX2CMD_MEMSEL, "encoder memsel"},
304 {FX2CMD_I2C_WRITE, "i2c write"},
305 {FX2CMD_I2C_READ, "i2c read"},
306 {FX2CMD_GET_USB_SPEED, "get USB speed"},
307 {FX2CMD_STREAMING_ON, "stream on"},
308 {FX2CMD_STREAMING_OFF, "stream off"},
309 {FX2CMD_FWPOST1, "fwpost1"},
310 {FX2CMD_POWER_OFF, "power off"},
311 {FX2CMD_POWER_ON, "power on"},
312 {FX2CMD_DEEP_RESET, "deep reset"},
313 {FX2CMD_GET_EEPROM_ADDR, "get rom addr"},
314 {FX2CMD_GET_IR_CODE, "get IR code"},
315 {FX2CMD_HCW_DEMOD_RESETIN, "hcw demod resetin"},
316 {FX2CMD_HCW_DTV_STREAMING_ON, "hcw dtv stream on"},
317 {FX2CMD_HCW_DTV_STREAMING_OFF, "hcw dtv stream off"},
318 {FX2CMD_ONAIR_DTV_STREAMING_ON, "onair dtv stream on"},
319 {FX2CMD_ONAIR_DTV_STREAMING_OFF, "onair dtv stream off"},
320 {FX2CMD_ONAIR_DTV_POWER_ON, "onair dtv power on"},
321 {FX2CMD_ONAIR_DTV_POWER_OFF, "onair dtv power off"},
322};
323
324
325static int pvr2_hdw_set_input(struct pvr2_hdw *hdw,int v);
326static void pvr2_hdw_state_sched(struct pvr2_hdw *);
327static int pvr2_hdw_state_eval(struct pvr2_hdw *);
328static void pvr2_hdw_set_cur_freq(struct pvr2_hdw *,unsigned long);
329static void pvr2_hdw_worker_poll(struct work_struct *work);
330static int pvr2_hdw_wait(struct pvr2_hdw *,int state);
331static int pvr2_hdw_untrip_unlocked(struct pvr2_hdw *);
332static void pvr2_hdw_state_log_state(struct pvr2_hdw *);
333static int pvr2_hdw_cmd_usbstream(struct pvr2_hdw *hdw,int runFl);
334static int pvr2_hdw_commit_setup(struct pvr2_hdw *hdw);
335static int pvr2_hdw_get_eeprom_addr(struct pvr2_hdw *hdw);
336static void pvr2_hdw_internal_find_stdenum(struct pvr2_hdw *hdw);
337static void pvr2_hdw_internal_set_std_avail(struct pvr2_hdw *hdw);
338static void pvr2_hdw_quiescent_timeout(unsigned long);
339static void pvr2_hdw_decoder_stabilization_timeout(unsigned long);
340static void pvr2_hdw_encoder_wait_timeout(unsigned long);
341static void pvr2_hdw_encoder_run_timeout(unsigned long);
342static int pvr2_issue_simple_cmd(struct pvr2_hdw *,u32);
343static int pvr2_send_request_ex(struct pvr2_hdw *hdw,
344 unsigned int timeout,int probe_fl,
345 void *write_data,unsigned int write_len,
346 void *read_data,unsigned int read_len);
347static int pvr2_hdw_check_cropcap(struct pvr2_hdw *hdw);
348
349
350static void trace_stbit(const char *name,int val)
351{
352 pvr2_trace(PVR2_TRACE_STBITS,
353 "State bit %s <-- %s",
354 name,(val ? "true" : "false"));
355}
356
357static int ctrl_channelfreq_get(struct pvr2_ctrl *cptr,int *vp)
358{
359 struct pvr2_hdw *hdw = cptr->hdw;
360 if ((hdw->freqProgSlot > 0) && (hdw->freqProgSlot <= FREQTABLE_SIZE)) {
361 *vp = hdw->freqTable[hdw->freqProgSlot-1];
362 } else {
363 *vp = 0;
364 }
365 return 0;
366}
367
368static int ctrl_channelfreq_set(struct pvr2_ctrl *cptr,int m,int v)
369{
370 struct pvr2_hdw *hdw = cptr->hdw;
371 unsigned int slotId = hdw->freqProgSlot;
372 if ((slotId > 0) && (slotId <= FREQTABLE_SIZE)) {
373 hdw->freqTable[slotId-1] = v;
374 /* Handle side effects correctly - if we're tuned to this
375 slot, then forgot the slot id relation since the stored
376 frequency has been changed. */
377 if (hdw->freqSelector) {
378 if (hdw->freqSlotRadio == slotId) {
379 hdw->freqSlotRadio = 0;
380 }
381 } else {
382 if (hdw->freqSlotTelevision == slotId) {
383 hdw->freqSlotTelevision = 0;
384 }
385 }
386 }
387 return 0;
388}
389
390static int ctrl_channelprog_get(struct pvr2_ctrl *cptr,int *vp)
391{
392 *vp = cptr->hdw->freqProgSlot;
393 return 0;
394}
395
396static int ctrl_channelprog_set(struct pvr2_ctrl *cptr,int m,int v)
397{
398 struct pvr2_hdw *hdw = cptr->hdw;
399 if ((v >= 0) && (v <= FREQTABLE_SIZE)) {
400 hdw->freqProgSlot = v;
401 }
402 return 0;
403}
404
405static int ctrl_channel_get(struct pvr2_ctrl *cptr,int *vp)
406{
407 struct pvr2_hdw *hdw = cptr->hdw;
408 *vp = hdw->freqSelector ? hdw->freqSlotRadio : hdw->freqSlotTelevision;
409 return 0;
410}
411
412static int ctrl_channel_set(struct pvr2_ctrl *cptr,int m,int slotId)
413{
414 unsigned freq = 0;
415 struct pvr2_hdw *hdw = cptr->hdw;
416 if ((slotId < 0) || (slotId > FREQTABLE_SIZE)) return 0;
417 if (slotId > 0) {
418 freq = hdw->freqTable[slotId-1];
419 if (!freq) return 0;
420 pvr2_hdw_set_cur_freq(hdw,freq);
421 }
422 if (hdw->freqSelector) {
423 hdw->freqSlotRadio = slotId;
424 } else {
425 hdw->freqSlotTelevision = slotId;
426 }
427 return 0;
428}
429
430static int ctrl_freq_get(struct pvr2_ctrl *cptr,int *vp)
431{
432 *vp = pvr2_hdw_get_cur_freq(cptr->hdw);
433 return 0;
434}
435
436static int ctrl_freq_is_dirty(struct pvr2_ctrl *cptr)
437{
438 return cptr->hdw->freqDirty != 0;
439}
440
441static void ctrl_freq_clear_dirty(struct pvr2_ctrl *cptr)
442{
443 cptr->hdw->freqDirty = 0;
444}
445
446static int ctrl_freq_set(struct pvr2_ctrl *cptr,int m,int v)
447{
448 pvr2_hdw_set_cur_freq(cptr->hdw,v);
449 return 0;
450}
451
452static int ctrl_cropl_min_get(struct pvr2_ctrl *cptr, int *left)
453{
454 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
455 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
456 if (stat != 0) {
457 return stat;
458 }
459 *left = cap->bounds.left;
460 return 0;
461}
462
463static int ctrl_cropl_max_get(struct pvr2_ctrl *cptr, int *left)
464{
465 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
466 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
467 if (stat != 0) {
468 return stat;
469 }
470 *left = cap->bounds.left;
471 if (cap->bounds.width > cptr->hdw->cropw_val) {
472 *left += cap->bounds.width - cptr->hdw->cropw_val;
473 }
474 return 0;
475}
476
477static int ctrl_cropt_min_get(struct pvr2_ctrl *cptr, int *top)
478{
479 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
480 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
481 if (stat != 0) {
482 return stat;
483 }
484 *top = cap->bounds.top;
485 return 0;
486}
487
488static int ctrl_cropt_max_get(struct pvr2_ctrl *cptr, int *top)
489{
490 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
491 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
492 if (stat != 0) {
493 return stat;
494 }
495 *top = cap->bounds.top;
496 if (cap->bounds.height > cptr->hdw->croph_val) {
497 *top += cap->bounds.height - cptr->hdw->croph_val;
498 }
499 return 0;
500}
501
502static int ctrl_cropw_max_get(struct pvr2_ctrl *cptr, int *width)
503{
504 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
505 int stat, bleftend, cleft;
506
507 stat = pvr2_hdw_check_cropcap(cptr->hdw);
508 if (stat != 0) {
509 return stat;
510 }
511 bleftend = cap->bounds.left+cap->bounds.width;
512 cleft = cptr->hdw->cropl_val;
513
514 *width = cleft < bleftend ? bleftend-cleft : 0;
515 return 0;
516}
517
518static int ctrl_croph_max_get(struct pvr2_ctrl *cptr, int *height)
519{
520 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
521 int stat, btopend, ctop;
522
523 stat = pvr2_hdw_check_cropcap(cptr->hdw);
524 if (stat != 0) {
525 return stat;
526 }
527 btopend = cap->bounds.top+cap->bounds.height;
528 ctop = cptr->hdw->cropt_val;
529
530 *height = ctop < btopend ? btopend-ctop : 0;
531 return 0;
532}
533
534static int ctrl_get_cropcapbl(struct pvr2_ctrl *cptr, int *val)
535{
536 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
537 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
538 if (stat != 0) {
539 return stat;
540 }
541 *val = cap->bounds.left;
542 return 0;
543}
544
545static int ctrl_get_cropcapbt(struct pvr2_ctrl *cptr, int *val)
546{
547 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
548 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
549 if (stat != 0) {
550 return stat;
551 }
552 *val = cap->bounds.top;
553 return 0;
554}
555
556static int ctrl_get_cropcapbw(struct pvr2_ctrl *cptr, int *val)
557{
558 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
559 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
560 if (stat != 0) {
561 return stat;
562 }
563 *val = cap->bounds.width;
564 return 0;
565}
566
567static int ctrl_get_cropcapbh(struct pvr2_ctrl *cptr, int *val)
568{
569 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
570 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
571 if (stat != 0) {
572 return stat;
573 }
574 *val = cap->bounds.height;
575 return 0;
576}
577
578static int ctrl_get_cropcapdl(struct pvr2_ctrl *cptr, int *val)
579{
580 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
581 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
582 if (stat != 0) {
583 return stat;
584 }
585 *val = cap->defrect.left;
586 return 0;
587}
588
589static int ctrl_get_cropcapdt(struct pvr2_ctrl *cptr, int *val)
590{
591 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
592 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
593 if (stat != 0) {
594 return stat;
595 }
596 *val = cap->defrect.top;
597 return 0;
598}
599
600static int ctrl_get_cropcapdw(struct pvr2_ctrl *cptr, int *val)
601{
602 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
603 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
604 if (stat != 0) {
605 return stat;
606 }
607 *val = cap->defrect.width;
608 return 0;
609}
610
611static int ctrl_get_cropcapdh(struct pvr2_ctrl *cptr, int *val)
612{
613 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
614 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
615 if (stat != 0) {
616 return stat;
617 }
618 *val = cap->defrect.height;
619 return 0;
620}
621
622static int ctrl_get_cropcappan(struct pvr2_ctrl *cptr, int *val)
623{
624 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
625 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
626 if (stat != 0) {
627 return stat;
628 }
629 *val = cap->pixelaspect.numerator;
630 return 0;
631}
632
633static int ctrl_get_cropcappad(struct pvr2_ctrl *cptr, int *val)
634{
635 struct v4l2_cropcap *cap = &cptr->hdw->cropcap_info;
636 int stat = pvr2_hdw_check_cropcap(cptr->hdw);
637 if (stat != 0) {
638 return stat;
639 }
640 *val = cap->pixelaspect.denominator;
641 return 0;
642}
643
644static int ctrl_vres_max_get(struct pvr2_ctrl *cptr,int *vp)
645{
646 /* Actual maximum depends on the video standard in effect. */
647 if (cptr->hdw->std_mask_cur & V4L2_STD_525_60) {
648 *vp = 480;
649 } else {
650 *vp = 576;
651 }
652 return 0;
653}
654
655static int ctrl_vres_min_get(struct pvr2_ctrl *cptr,int *vp)
656{
657 /* Actual minimum depends on device digitizer type. */
658 if (cptr->hdw->hdw_desc->flag_has_cx25840) {
659 *vp = 75;
660 } else {
661 *vp = 17;
662 }
663 return 0;
664}
665
666static int ctrl_get_input(struct pvr2_ctrl *cptr,int *vp)
667{
668 *vp = cptr->hdw->input_val;
669 return 0;
670}
671
672static int ctrl_check_input(struct pvr2_ctrl *cptr,int v)
673{
674 return ((1 << v) & cptr->hdw->input_allowed_mask) != 0;
675}
676
677static int ctrl_set_input(struct pvr2_ctrl *cptr,int m,int v)
678{
679 return pvr2_hdw_set_input(cptr->hdw,v);
680}
681
682static int ctrl_isdirty_input(struct pvr2_ctrl *cptr)
683{
684 return cptr->hdw->input_dirty != 0;
685}
686
687static void ctrl_cleardirty_input(struct pvr2_ctrl *cptr)
688{
689 cptr->hdw->input_dirty = 0;
690}
691
692
693static int ctrl_freq_max_get(struct pvr2_ctrl *cptr, int *vp)
694{
695 unsigned long fv;
696 struct pvr2_hdw *hdw = cptr->hdw;
697 if (hdw->tuner_signal_stale) {
698 pvr2_hdw_status_poll(hdw);
699 }
700 fv = hdw->tuner_signal_info.rangehigh;
701 if (!fv) {
702 /* Safety fallback */
703 *vp = TV_MAX_FREQ;
704 return 0;
705 }
706 if (hdw->tuner_signal_info.capability & V4L2_TUNER_CAP_LOW) {
707 fv = (fv * 125) / 2;
708 } else {
709 fv = fv * 62500;
710 }
711 *vp = fv;
712 return 0;
713}
714
715static int ctrl_freq_min_get(struct pvr2_ctrl *cptr, int *vp)
716{
717 unsigned long fv;
718 struct pvr2_hdw *hdw = cptr->hdw;
719 if (hdw->tuner_signal_stale) {
720 pvr2_hdw_status_poll(hdw);
721 }
722 fv = hdw->tuner_signal_info.rangelow;
723 if (!fv) {
724 /* Safety fallback */
725 *vp = TV_MIN_FREQ;
726 return 0;
727 }
728 if (hdw->tuner_signal_info.capability & V4L2_TUNER_CAP_LOW) {
729 fv = (fv * 125) / 2;
730 } else {
731 fv = fv * 62500;
732 }
733 *vp = fv;
734 return 0;
735}
736
737static int ctrl_cx2341x_is_dirty(struct pvr2_ctrl *cptr)
738{
739 return cptr->hdw->enc_stale != 0;
740}
741
742static void ctrl_cx2341x_clear_dirty(struct pvr2_ctrl *cptr)
743{
744 cptr->hdw->enc_stale = 0;
745 cptr->hdw->enc_unsafe_stale = 0;
746}
747
748static int ctrl_cx2341x_get(struct pvr2_ctrl *cptr,int *vp)
749{
750 int ret;
751 struct v4l2_ext_controls cs;
752 struct v4l2_ext_control c1;
753 memset(&cs,0,sizeof(cs));
754 memset(&c1,0,sizeof(c1));
755 cs.controls = &c1;
756 cs.count = 1;
757 c1.id = cptr->info->v4l_id;
758 ret = cx2341x_ext_ctrls(&cptr->hdw->enc_ctl_state, 0, &cs,
759 VIDIOC_G_EXT_CTRLS);
760 if (ret) return ret;
761 *vp = c1.value;
762 return 0;
763}
764
765static int ctrl_cx2341x_set(struct pvr2_ctrl *cptr,int m,int v)
766{
767 int ret;
768 struct pvr2_hdw *hdw = cptr->hdw;
769 struct v4l2_ext_controls cs;
770 struct v4l2_ext_control c1;
771 memset(&cs,0,sizeof(cs));
772 memset(&c1,0,sizeof(c1));
773 cs.controls = &c1;
774 cs.count = 1;
775 c1.id = cptr->info->v4l_id;
776 c1.value = v;
777 ret = cx2341x_ext_ctrls(&hdw->enc_ctl_state,
778 hdw->state_encoder_run, &cs,
779 VIDIOC_S_EXT_CTRLS);
780 if (ret == -EBUSY) {
781 /* Oops. cx2341x is telling us it's not safe to change
782 this control while we're capturing. Make a note of this
783 fact so that the pipeline will be stopped the next time
784 controls are committed. Then go on ahead and store this
785 change anyway. */
786 ret = cx2341x_ext_ctrls(&hdw->enc_ctl_state,
787 0, &cs,
788 VIDIOC_S_EXT_CTRLS);
789 if (!ret) hdw->enc_unsafe_stale = !0;
790 }
791 if (ret) return ret;
792 hdw->enc_stale = !0;
793 return 0;
794}
795
796static unsigned int ctrl_cx2341x_getv4lflags(struct pvr2_ctrl *cptr)
797{
798 struct v4l2_queryctrl qctrl;
799 struct pvr2_ctl_info *info;
800 qctrl.id = cptr->info->v4l_id;
801 cx2341x_ctrl_query(&cptr->hdw->enc_ctl_state,&qctrl);
802 /* Strip out the const so we can adjust a function pointer. It's
803 OK to do this here because we know this is a dynamically created
804 control, so the underlying storage for the info pointer is (a)
805 private to us, and (b) not in read-only storage. Either we do
806 this or we significantly complicate the underlying control
807 implementation. */
808 info = (struct pvr2_ctl_info *)(cptr->info);
809 if (qctrl.flags & V4L2_CTRL_FLAG_READ_ONLY) {
810 if (info->set_value) {
811 info->set_value = NULL;
812 }
813 } else {
814 if (!(info->set_value)) {
815 info->set_value = ctrl_cx2341x_set;
816 }
817 }
818 return qctrl.flags;
819}
820
821static int ctrl_streamingenabled_get(struct pvr2_ctrl *cptr,int *vp)
822{
823 *vp = cptr->hdw->state_pipeline_req;
824 return 0;
825}
826
827static int ctrl_masterstate_get(struct pvr2_ctrl *cptr,int *vp)
828{
829 *vp = cptr->hdw->master_state;
830 return 0;
831}
832
833static int ctrl_hsm_get(struct pvr2_ctrl *cptr,int *vp)
834{
835 int result = pvr2_hdw_is_hsm(cptr->hdw);
836 *vp = PVR2_CVAL_HSM_FULL;
837 if (result < 0) *vp = PVR2_CVAL_HSM_FAIL;
838 if (result) *vp = PVR2_CVAL_HSM_HIGH;
839 return 0;
840}
841
842static int ctrl_stdavail_get(struct pvr2_ctrl *cptr,int *vp)
843{
844 *vp = cptr->hdw->std_mask_avail;
845 return 0;
846}
847
848static int ctrl_stdavail_set(struct pvr2_ctrl *cptr,int m,int v)
849{
850 struct pvr2_hdw *hdw = cptr->hdw;
851 v4l2_std_id ns;
852 ns = hdw->std_mask_avail;
853 ns = (ns & ~m) | (v & m);
854 if (ns == hdw->std_mask_avail) return 0;
855 hdw->std_mask_avail = ns;
856 pvr2_hdw_internal_set_std_avail(hdw);
857 pvr2_hdw_internal_find_stdenum(hdw);
858 return 0;
859}
860
861static int ctrl_std_val_to_sym(struct pvr2_ctrl *cptr,int msk,int val,
862 char *bufPtr,unsigned int bufSize,
863 unsigned int *len)
864{
865 *len = pvr2_std_id_to_str(bufPtr,bufSize,msk & val);
866 return 0;
867}
868
869static int ctrl_std_sym_to_val(struct pvr2_ctrl *cptr,
870 const char *bufPtr,unsigned int bufSize,
871 int *mskp,int *valp)
872{
873 int ret;
874 v4l2_std_id id;
875 ret = pvr2_std_str_to_id(&id,bufPtr,bufSize);
876 if (ret < 0) return ret;
877 if (mskp) *mskp = id;
878 if (valp) *valp = id;
879 return 0;
880}
881
882static int ctrl_stdcur_get(struct pvr2_ctrl *cptr,int *vp)
883{
884 *vp = cptr->hdw->std_mask_cur;
885 return 0;
886}
887
888static int ctrl_stdcur_set(struct pvr2_ctrl *cptr,int m,int v)
889{
890 struct pvr2_hdw *hdw = cptr->hdw;
891 v4l2_std_id ns;
892 ns = hdw->std_mask_cur;
893 ns = (ns & ~m) | (v & m);
894 if (ns == hdw->std_mask_cur) return 0;
895 hdw->std_mask_cur = ns;
896 hdw->std_dirty = !0;
897 pvr2_hdw_internal_find_stdenum(hdw);
898 return 0;
899}
900
901static int ctrl_stdcur_is_dirty(struct pvr2_ctrl *cptr)
902{
903 return cptr->hdw->std_dirty != 0;
904}
905
906static void ctrl_stdcur_clear_dirty(struct pvr2_ctrl *cptr)
907{
908 cptr->hdw->std_dirty = 0;
909}
910
911static int ctrl_signal_get(struct pvr2_ctrl *cptr,int *vp)
912{
913 struct pvr2_hdw *hdw = cptr->hdw;
914 pvr2_hdw_status_poll(hdw);
915 *vp = hdw->tuner_signal_info.signal;
916 return 0;
917}
918
919static int ctrl_audio_modes_present_get(struct pvr2_ctrl *cptr,int *vp)
920{
921 int val = 0;
922 unsigned int subchan;
923 struct pvr2_hdw *hdw = cptr->hdw;
924 pvr2_hdw_status_poll(hdw);
925 subchan = hdw->tuner_signal_info.rxsubchans;
926 if (subchan & V4L2_TUNER_SUB_MONO) {
927 val |= (1 << V4L2_TUNER_MODE_MONO);
928 }
929 if (subchan & V4L2_TUNER_SUB_STEREO) {
930 val |= (1 << V4L2_TUNER_MODE_STEREO);
931 }
932 if (subchan & V4L2_TUNER_SUB_LANG1) {
933 val |= (1 << V4L2_TUNER_MODE_LANG1);
934 }
935 if (subchan & V4L2_TUNER_SUB_LANG2) {
936 val |= (1 << V4L2_TUNER_MODE_LANG2);
937 }
938 *vp = val;
939 return 0;
940}
941
942
943static int ctrl_stdenumcur_set(struct pvr2_ctrl *cptr,int m,int v)
944{
945 struct pvr2_hdw *hdw = cptr->hdw;
946 if (v < 0) return -EINVAL;
947 if (v > hdw->std_enum_cnt) return -EINVAL;
948 hdw->std_enum_cur = v;
949 if (!v) return 0;
950 v--;
951 if (hdw->std_mask_cur == hdw->std_defs[v].id) return 0;
952 hdw->std_mask_cur = hdw->std_defs[v].id;
953 hdw->std_dirty = !0;
954 return 0;
955}
956
957
958static int ctrl_stdenumcur_get(struct pvr2_ctrl *cptr,int *vp)
959{
960 *vp = cptr->hdw->std_enum_cur;
961 return 0;
962}
963
964
965static int ctrl_stdenumcur_is_dirty(struct pvr2_ctrl *cptr)
966{
967 return cptr->hdw->std_dirty != 0;
968}
969
970
971static void ctrl_stdenumcur_clear_dirty(struct pvr2_ctrl *cptr)
972{
973 cptr->hdw->std_dirty = 0;
974}
975
976
977#define DEFINT(vmin,vmax) \
978 .type = pvr2_ctl_int, \
979 .def.type_int.min_value = vmin, \
980 .def.type_int.max_value = vmax
981
982#define DEFENUM(tab) \
983 .type = pvr2_ctl_enum, \
984 .def.type_enum.count = ARRAY_SIZE(tab), \
985 .def.type_enum.value_names = tab
986
987#define DEFBOOL \
988 .type = pvr2_ctl_bool
989
990#define DEFMASK(msk,tab) \
991 .type = pvr2_ctl_bitmask, \
992 .def.type_bitmask.valid_bits = msk, \
993 .def.type_bitmask.bit_names = tab
994
995#define DEFREF(vname) \
996 .set_value = ctrl_set_##vname, \
997 .get_value = ctrl_get_##vname, \
998 .is_dirty = ctrl_isdirty_##vname, \
999 .clear_dirty = ctrl_cleardirty_##vname
1000
1001
1002#define VCREATE_FUNCS(vname) \
1003static int ctrl_get_##vname(struct pvr2_ctrl *cptr,int *vp) \
1004{*vp = cptr->hdw->vname##_val; return 0;} \
1005static int ctrl_set_##vname(struct pvr2_ctrl *cptr,int m,int v) \
1006{cptr->hdw->vname##_val = v; cptr->hdw->vname##_dirty = !0; return 0;} \
1007static int ctrl_isdirty_##vname(struct pvr2_ctrl *cptr) \
1008{return cptr->hdw->vname##_dirty != 0;} \
1009static void ctrl_cleardirty_##vname(struct pvr2_ctrl *cptr) \
1010{cptr->hdw->vname##_dirty = 0;}
1011
1012VCREATE_FUNCS(brightness)
1013VCREATE_FUNCS(contrast)
1014VCREATE_FUNCS(saturation)
1015VCREATE_FUNCS(hue)
1016VCREATE_FUNCS(volume)
1017VCREATE_FUNCS(balance)
1018VCREATE_FUNCS(bass)
1019VCREATE_FUNCS(treble)
1020VCREATE_FUNCS(mute)
1021VCREATE_FUNCS(cropl)
1022VCREATE_FUNCS(cropt)
1023VCREATE_FUNCS(cropw)
1024VCREATE_FUNCS(croph)
1025VCREATE_FUNCS(audiomode)
1026VCREATE_FUNCS(res_hor)
1027VCREATE_FUNCS(res_ver)
1028VCREATE_FUNCS(srate)
1029
1030/* Table definition of all controls which can be manipulated */
1031static const struct pvr2_ctl_info control_defs[] = {
1032 {
1033 .v4l_id = V4L2_CID_BRIGHTNESS,
1034 .desc = "Brightness",
1035 .name = "brightness",
1036 .default_value = 128,
1037 DEFREF(brightness),
1038 DEFINT(0,255),
1039 },{
1040 .v4l_id = V4L2_CID_CONTRAST,
1041 .desc = "Contrast",
1042 .name = "contrast",
1043 .default_value = 68,
1044 DEFREF(contrast),
1045 DEFINT(0,127),
1046 },{
1047 .v4l_id = V4L2_CID_SATURATION,
1048 .desc = "Saturation",
1049 .name = "saturation",
1050 .default_value = 64,
1051 DEFREF(saturation),
1052 DEFINT(0,127),
1053 },{
1054 .v4l_id = V4L2_CID_HUE,
1055 .desc = "Hue",
1056 .name = "hue",
1057 .default_value = 0,
1058 DEFREF(hue),
1059 DEFINT(-128,127),
1060 },{
1061 .v4l_id = V4L2_CID_AUDIO_VOLUME,
1062 .desc = "Volume",
1063 .name = "volume",
1064 .default_value = 62000,
1065 DEFREF(volume),
1066 DEFINT(0,65535),
1067 },{
1068 .v4l_id = V4L2_CID_AUDIO_BALANCE,
1069 .desc = "Balance",
1070 .name = "balance",
1071 .default_value = 0,
1072 DEFREF(balance),
1073 DEFINT(-32768,32767),
1074 },{
1075 .v4l_id = V4L2_CID_AUDIO_BASS,
1076 .desc = "Bass",
1077 .name = "bass",
1078 .default_value = 0,
1079 DEFREF(bass),
1080 DEFINT(-32768,32767),
1081 },{
1082 .v4l_id = V4L2_CID_AUDIO_TREBLE,
1083 .desc = "Treble",
1084 .name = "treble",
1085 .default_value = 0,
1086 DEFREF(treble),
1087 DEFINT(-32768,32767),
1088 },{
1089 .v4l_id = V4L2_CID_AUDIO_MUTE,
1090 .desc = "Mute",
1091 .name = "mute",
1092 .default_value = 0,
1093 DEFREF(mute),
1094 DEFBOOL,
1095 }, {
1096 .desc = "Capture crop left margin",
1097 .name = "crop_left",
1098 .internal_id = PVR2_CID_CROPL,
1099 .default_value = 0,
1100 DEFREF(cropl),
1101 DEFINT(-129, 340),
1102 .get_min_value = ctrl_cropl_min_get,
1103 .get_max_value = ctrl_cropl_max_get,
1104 .get_def_value = ctrl_get_cropcapdl,
1105 }, {
1106 .desc = "Capture crop top margin",
1107 .name = "crop_top",
1108 .internal_id = PVR2_CID_CROPT,
1109 .default_value = 0,
1110 DEFREF(cropt),
1111 DEFINT(-35, 544),
1112 .get_min_value = ctrl_cropt_min_get,
1113 .get_max_value = ctrl_cropt_max_get,
1114 .get_def_value = ctrl_get_cropcapdt,
1115 }, {
1116 .desc = "Capture crop width",
1117 .name = "crop_width",
1118 .internal_id = PVR2_CID_CROPW,
1119 .default_value = 720,
1120 DEFREF(cropw),
1121 DEFINT(0, 864),
1122 .get_max_value = ctrl_cropw_max_get,
1123 .get_def_value = ctrl_get_cropcapdw,
1124 }, {
1125 .desc = "Capture crop height",
1126 .name = "crop_height",
1127 .internal_id = PVR2_CID_CROPH,
1128 .default_value = 480,
1129 DEFREF(croph),
1130 DEFINT(0, 576),
1131 .get_max_value = ctrl_croph_max_get,
1132 .get_def_value = ctrl_get_cropcapdh,
1133 }, {
1134 .desc = "Capture capability pixel aspect numerator",
1135 .name = "cropcap_pixel_numerator",
1136 .internal_id = PVR2_CID_CROPCAPPAN,
1137 .get_value = ctrl_get_cropcappan,
1138 }, {
1139 .desc = "Capture capability pixel aspect denominator",
1140 .name = "cropcap_pixel_denominator",
1141 .internal_id = PVR2_CID_CROPCAPPAD,
1142 .get_value = ctrl_get_cropcappad,
1143 }, {
1144 .desc = "Capture capability bounds top",
1145 .name = "cropcap_bounds_top",
1146 .internal_id = PVR2_CID_CROPCAPBT,
1147 .get_value = ctrl_get_cropcapbt,
1148 }, {
1149 .desc = "Capture capability bounds left",
1150 .name = "cropcap_bounds_left",
1151 .internal_id = PVR2_CID_CROPCAPBL,
1152 .get_value = ctrl_get_cropcapbl,
1153 }, {
1154 .desc = "Capture capability bounds width",
1155 .name = "cropcap_bounds_width",
1156 .internal_id = PVR2_CID_CROPCAPBW,
1157 .get_value = ctrl_get_cropcapbw,
1158 }, {
1159 .desc = "Capture capability bounds height",
1160 .name = "cropcap_bounds_height",
1161 .internal_id = PVR2_CID_CROPCAPBH,
1162 .get_value = ctrl_get_cropcapbh,
1163 },{
1164 .desc = "Video Source",
1165 .name = "input",
1166 .internal_id = PVR2_CID_INPUT,
1167 .default_value = PVR2_CVAL_INPUT_TV,
1168 .check_value = ctrl_check_input,
1169 DEFREF(input),
1170 DEFENUM(control_values_input),
1171 },{
1172 .desc = "Audio Mode",
1173 .name = "audio_mode",
1174 .internal_id = PVR2_CID_AUDIOMODE,
1175 .default_value = V4L2_TUNER_MODE_STEREO,
1176 DEFREF(audiomode),
1177 DEFENUM(control_values_audiomode),
1178 },{
1179 .desc = "Horizontal capture resolution",
1180 .name = "resolution_hor",
1181 .internal_id = PVR2_CID_HRES,
1182 .default_value = 720,
1183 DEFREF(res_hor),
1184 DEFINT(19,720),
1185 },{
1186 .desc = "Vertical capture resolution",
1187 .name = "resolution_ver",
1188 .internal_id = PVR2_CID_VRES,
1189 .default_value = 480,
1190 DEFREF(res_ver),
1191 DEFINT(17,576),
1192 /* Hook in check for video standard and adjust maximum
1193 depending on the standard. */
1194 .get_max_value = ctrl_vres_max_get,
1195 .get_min_value = ctrl_vres_min_get,
1196 },{
1197 .v4l_id = V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
1198 .default_value = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000,
1199 .desc = "Audio Sampling Frequency",
1200 .name = "srate",
1201 DEFREF(srate),
1202 DEFENUM(control_values_srate),
1203 },{
1204 .desc = "Tuner Frequency (Hz)",
1205 .name = "frequency",
1206 .internal_id = PVR2_CID_FREQUENCY,
1207 .default_value = 0,
1208 .set_value = ctrl_freq_set,
1209 .get_value = ctrl_freq_get,
1210 .is_dirty = ctrl_freq_is_dirty,
1211 .clear_dirty = ctrl_freq_clear_dirty,
1212 DEFINT(0,0),
1213 /* Hook in check for input value (tv/radio) and adjust
1214 max/min values accordingly */
1215 .get_max_value = ctrl_freq_max_get,
1216 .get_min_value = ctrl_freq_min_get,
1217 },{
1218 .desc = "Channel",
1219 .name = "channel",
1220 .set_value = ctrl_channel_set,
1221 .get_value = ctrl_channel_get,
1222 DEFINT(0,FREQTABLE_SIZE),
1223 },{
1224 .desc = "Channel Program Frequency",
1225 .name = "freq_table_value",
1226 .set_value = ctrl_channelfreq_set,
1227 .get_value = ctrl_channelfreq_get,
1228 DEFINT(0,0),
1229 /* Hook in check for input value (tv/radio) and adjust
1230 max/min values accordingly */
1231 .get_max_value = ctrl_freq_max_get,
1232 .get_min_value = ctrl_freq_min_get,
1233 },{
1234 .desc = "Channel Program ID",
1235 .name = "freq_table_channel",
1236 .set_value = ctrl_channelprog_set,
1237 .get_value = ctrl_channelprog_get,
1238 DEFINT(0,FREQTABLE_SIZE),
1239 },{
1240 .desc = "Streaming Enabled",
1241 .name = "streaming_enabled",
1242 .get_value = ctrl_streamingenabled_get,
1243 DEFBOOL,
1244 },{
1245 .desc = "USB Speed",
1246 .name = "usb_speed",
1247 .get_value = ctrl_hsm_get,
1248 DEFENUM(control_values_hsm),
1249 },{
1250 .desc = "Master State",
1251 .name = "master_state",
1252 .get_value = ctrl_masterstate_get,
1253 DEFENUM(pvr2_state_names),
1254 },{
1255 .desc = "Signal Present",
1256 .name = "signal_present",
1257 .get_value = ctrl_signal_get,
1258 DEFINT(0,65535),
1259 },{
1260 .desc = "Audio Modes Present",
1261 .name = "audio_modes_present",
1262 .get_value = ctrl_audio_modes_present_get,
1263 /* For this type we "borrow" the V4L2_TUNER_MODE enum from
1264 v4l. Nothing outside of this module cares about this,
1265 but I reuse it in order to also reuse the
1266 control_values_audiomode string table. */
1267 DEFMASK(((1 << V4L2_TUNER_MODE_MONO)|
1268 (1 << V4L2_TUNER_MODE_STEREO)|
1269 (1 << V4L2_TUNER_MODE_LANG1)|
1270 (1 << V4L2_TUNER_MODE_LANG2)),
1271 control_values_audiomode),
1272 },{
1273 .desc = "Video Standards Available Mask",
1274 .name = "video_standard_mask_available",
1275 .internal_id = PVR2_CID_STDAVAIL,
1276 .skip_init = !0,
1277 .get_value = ctrl_stdavail_get,
1278 .set_value = ctrl_stdavail_set,
1279 .val_to_sym = ctrl_std_val_to_sym,
1280 .sym_to_val = ctrl_std_sym_to_val,
1281 .type = pvr2_ctl_bitmask,
1282 },{
1283 .desc = "Video Standards In Use Mask",
1284 .name = "video_standard_mask_active",
1285 .internal_id = PVR2_CID_STDCUR,
1286 .skip_init = !0,
1287 .get_value = ctrl_stdcur_get,
1288 .set_value = ctrl_stdcur_set,
1289 .is_dirty = ctrl_stdcur_is_dirty,
1290 .clear_dirty = ctrl_stdcur_clear_dirty,
1291 .val_to_sym = ctrl_std_val_to_sym,
1292 .sym_to_val = ctrl_std_sym_to_val,
1293 .type = pvr2_ctl_bitmask,
1294 },{
1295 .desc = "Video Standard Name",
1296 .name = "video_standard",
1297 .internal_id = PVR2_CID_STDENUM,
1298 .skip_init = !0,
1299 .get_value = ctrl_stdenumcur_get,
1300 .set_value = ctrl_stdenumcur_set,
1301 .is_dirty = ctrl_stdenumcur_is_dirty,
1302 .clear_dirty = ctrl_stdenumcur_clear_dirty,
1303 .type = pvr2_ctl_enum,
1304 }
1305};
1306
1307#define CTRLDEF_COUNT ARRAY_SIZE(control_defs)
1308
1309
1310const char *pvr2_config_get_name(enum pvr2_config cfg)
1311{
1312 switch (cfg) {
1313 case pvr2_config_empty: return "empty";
1314 case pvr2_config_mpeg: return "mpeg";
1315 case pvr2_config_vbi: return "vbi";
1316 case pvr2_config_pcm: return "pcm";
1317 case pvr2_config_rawvideo: return "raw video";
1318 }
1319 return "<unknown>";
1320}
1321
1322
1323struct usb_device *pvr2_hdw_get_dev(struct pvr2_hdw *hdw)
1324{
1325 return hdw->usb_dev;
1326}
1327
1328
1329unsigned long pvr2_hdw_get_sn(struct pvr2_hdw *hdw)
1330{
1331 return hdw->serial_number;
1332}
1333
1334
1335const char *pvr2_hdw_get_bus_info(struct pvr2_hdw *hdw)
1336{
1337 return hdw->bus_info;
1338}
1339
1340
1341const char *pvr2_hdw_get_device_identifier(struct pvr2_hdw *hdw)
1342{
1343 return hdw->identifier;
1344}
1345
1346
1347unsigned long pvr2_hdw_get_cur_freq(struct pvr2_hdw *hdw)
1348{
1349 return hdw->freqSelector ? hdw->freqValTelevision : hdw->freqValRadio;
1350}
1351
1352/* Set the currently tuned frequency and account for all possible
1353 driver-core side effects of this action. */
1354static void pvr2_hdw_set_cur_freq(struct pvr2_hdw *hdw,unsigned long val)
1355{
1356 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
1357 if (hdw->freqSelector) {
1358 /* Swing over to radio frequency selection */
1359 hdw->freqSelector = 0;
1360 hdw->freqDirty = !0;
1361 }
1362 if (hdw->freqValRadio != val) {
1363 hdw->freqValRadio = val;
1364 hdw->freqSlotRadio = 0;
1365 hdw->freqDirty = !0;
1366 }
1367 } else {
1368 if (!(hdw->freqSelector)) {
1369 /* Swing over to television frequency selection */
1370 hdw->freqSelector = 1;
1371 hdw->freqDirty = !0;
1372 }
1373 if (hdw->freqValTelevision != val) {
1374 hdw->freqValTelevision = val;
1375 hdw->freqSlotTelevision = 0;
1376 hdw->freqDirty = !0;
1377 }
1378 }
1379}
1380
1381int pvr2_hdw_get_unit_number(struct pvr2_hdw *hdw)
1382{
1383 return hdw->unit_number;
1384}
1385
1386
1387/* Attempt to locate one of the given set of files. Messages are logged
1388 appropriate to what has been found. The return value will be 0 or
1389 greater on success (it will be the index of the file name found) and
1390 fw_entry will be filled in. Otherwise a negative error is returned on
1391 failure. If the return value is -ENOENT then no viable firmware file
1392 could be located. */
1393static int pvr2_locate_firmware(struct pvr2_hdw *hdw,
1394 const struct firmware **fw_entry,
1395 const char *fwtypename,
1396 unsigned int fwcount,
1397 const char *fwnames[])
1398{
1399 unsigned int idx;
1400 int ret = -EINVAL;
1401 for (idx = 0; idx < fwcount; idx++) {
1402 ret = request_firmware(fw_entry,
1403 fwnames[idx],
1404 &hdw->usb_dev->dev);
1405 if (!ret) {
1406 trace_firmware("Located %s firmware: %s;"
1407 " uploading...",
1408 fwtypename,
1409 fwnames[idx]);
1410 return idx;
1411 }
1412 if (ret == -ENOENT) continue;
1413 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1414 "request_firmware fatal error with code=%d",ret);
1415 return ret;
1416 }
1417 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1418 "***WARNING***"
1419 " Device %s firmware"
1420 " seems to be missing.",
1421 fwtypename);
1422 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1423 "Did you install the pvrusb2 firmware files"
1424 " in their proper location?");
1425 if (fwcount == 1) {
1426 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1427 "request_firmware unable to locate %s file %s",
1428 fwtypename,fwnames[0]);
1429 } else {
1430 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1431 "request_firmware unable to locate"
1432 " one of the following %s files:",
1433 fwtypename);
1434 for (idx = 0; idx < fwcount; idx++) {
1435 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1436 "request_firmware: Failed to find %s",
1437 fwnames[idx]);
1438 }
1439 }
1440 return ret;
1441}
1442
1443
1444/*
1445 * pvr2_upload_firmware1().
1446 *
1447 * Send the 8051 firmware to the device. After the upload, arrange for
1448 * device to re-enumerate.
1449 *
1450 * NOTE : the pointer to the firmware data given by request_firmware()
1451 * is not suitable for an usb transaction.
1452 *
1453 */
1454static int pvr2_upload_firmware1(struct pvr2_hdw *hdw)
1455{
1456 const struct firmware *fw_entry = NULL;
1457 void *fw_ptr;
1458 unsigned int pipe;
1459 unsigned int fwsize;
1460 int ret;
1461 u16 address;
1462
1463 if (!hdw->hdw_desc->fx2_firmware.cnt) {
1464 hdw->fw1_state = FW1_STATE_OK;
1465 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1466 "Connected device type defines"
1467 " no firmware to upload; ignoring firmware");
1468 return -ENOTTY;
1469 }
1470
1471 hdw->fw1_state = FW1_STATE_FAILED; // default result
1472
1473 trace_firmware("pvr2_upload_firmware1");
1474
1475 ret = pvr2_locate_firmware(hdw,&fw_entry,"fx2 controller",
1476 hdw->hdw_desc->fx2_firmware.cnt,
1477 hdw->hdw_desc->fx2_firmware.lst);
1478 if (ret < 0) {
1479 if (ret == -ENOENT) hdw->fw1_state = FW1_STATE_MISSING;
1480 return ret;
1481 }
1482
1483 usb_clear_halt(hdw->usb_dev, usb_sndbulkpipe(hdw->usb_dev, 0 & 0x7f));
1484
1485 pipe = usb_sndctrlpipe(hdw->usb_dev, 0);
1486 fwsize = fw_entry->size;
1487
1488 if ((fwsize != 0x2000) &&
1489 (!(hdw->hdw_desc->flag_fx2_16kb && (fwsize == 0x4000)))) {
1490 if (hdw->hdw_desc->flag_fx2_16kb) {
1491 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1492 "Wrong fx2 firmware size"
1493 " (expected 8192 or 16384, got %u)",
1494 fwsize);
1495 } else {
1496 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1497 "Wrong fx2 firmware size"
1498 " (expected 8192, got %u)",
1499 fwsize);
1500 }
1501 release_firmware(fw_entry);
1502 return -ENOMEM;
1503 }
1504
1505 fw_ptr = kmalloc(0x800, GFP_KERNEL);
1506 if (fw_ptr == NULL){
1507 release_firmware(fw_entry);
1508 return -ENOMEM;
1509 }
1510
1511 /* We have to hold the CPU during firmware upload. */
1512 pvr2_hdw_cpureset_assert(hdw,1);
1513
1514 /* upload the firmware to address 0000-1fff in 2048 (=0x800) bytes
1515 chunk. */
1516
1517 ret = 0;
1518 for (address = 0; address < fwsize; address += 0x800) {
1519 memcpy(fw_ptr, fw_entry->data + address, 0x800);
1520 ret += usb_control_msg(hdw->usb_dev, pipe, 0xa0, 0x40, address,
1521 0, fw_ptr, 0x800, HZ);
1522 }
1523
1524 trace_firmware("Upload done, releasing device's CPU");
1525
1526 /* Now release the CPU. It will disconnect and reconnect later. */
1527 pvr2_hdw_cpureset_assert(hdw,0);
1528
1529 kfree(fw_ptr);
1530 release_firmware(fw_entry);
1531
1532 trace_firmware("Upload done (%d bytes sent)",ret);
1533
1534 /* We should have written fwsize bytes */
1535 if (ret == fwsize) {
1536 hdw->fw1_state = FW1_STATE_RELOAD;
1537 return 0;
1538 }
1539
1540 return -EIO;
1541}
1542
1543
1544/*
1545 * pvr2_upload_firmware2()
1546 *
1547 * This uploads encoder firmware on endpoint 2.
1548 *
1549 */
1550
1551int pvr2_upload_firmware2(struct pvr2_hdw *hdw)
1552{
1553 const struct firmware *fw_entry = NULL;
1554 void *fw_ptr;
1555 unsigned int pipe, fw_len, fw_done, bcnt, icnt;
1556 int actual_length;
1557 int ret = 0;
1558 int fwidx;
1559 static const char *fw_files[] = {
1560 CX2341X_FIRM_ENC_FILENAME,
1561 };
1562
1563 if (hdw->hdw_desc->flag_skip_cx23416_firmware) {
1564 return 0;
1565 }
1566
1567 trace_firmware("pvr2_upload_firmware2");
1568
1569 ret = pvr2_locate_firmware(hdw,&fw_entry,"encoder",
1570 ARRAY_SIZE(fw_files), fw_files);
1571 if (ret < 0) return ret;
1572 fwidx = ret;
1573 ret = 0;
1574 /* Since we're about to completely reinitialize the encoder,
1575 invalidate our cached copy of its configuration state. Next
1576 time we configure the encoder, then we'll fully configure it. */
1577 hdw->enc_cur_valid = 0;
1578
1579 /* Encoder is about to be reset so note that as far as we're
1580 concerned now, the encoder has never been run. */
1581 del_timer_sync(&hdw->encoder_run_timer);
1582 if (hdw->state_encoder_runok) {
1583 hdw->state_encoder_runok = 0;
1584 trace_stbit("state_encoder_runok",hdw->state_encoder_runok);
1585 }
1586
1587 /* First prepare firmware loading */
1588 ret |= pvr2_write_register(hdw, 0x0048, 0xffffffff); /*interrupt mask*/
1589 ret |= pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000088); /*gpio dir*/
1590 ret |= pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000008); /*gpio output state*/
1591 ret |= pvr2_hdw_cmd_deep_reset(hdw);
1592 ret |= pvr2_write_register(hdw, 0xa064, 0x00000000); /*APU command*/
1593 ret |= pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000408); /*gpio dir*/
1594 ret |= pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000008); /*gpio output state*/
1595 ret |= pvr2_write_register(hdw, 0x9058, 0xffffffed); /*VPU ctrl*/
1596 ret |= pvr2_write_register(hdw, 0x9054, 0xfffffffd); /*reset hw blocks*/
1597 ret |= pvr2_write_register(hdw, 0x07f8, 0x80000800); /*encoder SDRAM refresh*/
1598 ret |= pvr2_write_register(hdw, 0x07fc, 0x0000001a); /*encoder SDRAM pre-charge*/
1599 ret |= pvr2_write_register(hdw, 0x0700, 0x00000000); /*I2C clock*/
1600 ret |= pvr2_write_register(hdw, 0xaa00, 0x00000000); /*unknown*/
1601 ret |= pvr2_write_register(hdw, 0xaa04, 0x00057810); /*unknown*/
1602 ret |= pvr2_write_register(hdw, 0xaa10, 0x00148500); /*unknown*/
1603 ret |= pvr2_write_register(hdw, 0xaa18, 0x00840000); /*unknown*/
1604 ret |= pvr2_issue_simple_cmd(hdw,FX2CMD_FWPOST1);
1605 ret |= pvr2_issue_simple_cmd(hdw,FX2CMD_MEMSEL | (1 << 8) | (0 << 16));
1606
1607 if (ret) {
1608 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1609 "firmware2 upload prep failed, ret=%d",ret);
1610 release_firmware(fw_entry);
1611 goto done;
1612 }
1613
1614 /* Now send firmware */
1615
1616 fw_len = fw_entry->size;
1617
1618 if (fw_len % sizeof(u32)) {
1619 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1620 "size of %s firmware"
1621 " must be a multiple of %zu bytes",
1622 fw_files[fwidx],sizeof(u32));
1623 release_firmware(fw_entry);
1624 ret = -EINVAL;
1625 goto done;
1626 }
1627
1628 fw_ptr = kmalloc(FIRMWARE_CHUNK_SIZE, GFP_KERNEL);
1629 if (fw_ptr == NULL){
1630 release_firmware(fw_entry);
1631 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1632 "failed to allocate memory for firmware2 upload");
1633 ret = -ENOMEM;
1634 goto done;
1635 }
1636
1637 pipe = usb_sndbulkpipe(hdw->usb_dev, PVR2_FIRMWARE_ENDPOINT);
1638
1639 fw_done = 0;
1640 for (fw_done = 0; fw_done < fw_len;) {
1641 bcnt = fw_len - fw_done;
1642 if (bcnt > FIRMWARE_CHUNK_SIZE) bcnt = FIRMWARE_CHUNK_SIZE;
1643 memcpy(fw_ptr, fw_entry->data + fw_done, bcnt);
1644 /* Usbsnoop log shows that we must swap bytes... */
1645 /* Some background info: The data being swapped here is a
1646 firmware image destined for the mpeg encoder chip that
1647 lives at the other end of a USB endpoint. The encoder
1648 chip always talks in 32 bit chunks and its storage is
1649 organized into 32 bit words. However from the file
1650 system to the encoder chip everything is purely a byte
1651 stream. The firmware file's contents are always 32 bit
1652 swapped from what the encoder expects. Thus the need
1653 always exists to swap the bytes regardless of the endian
1654 type of the host processor and therefore swab32() makes
1655 the most sense. */
1656 for (icnt = 0; icnt < bcnt/4 ; icnt++)
1657 ((u32 *)fw_ptr)[icnt] = swab32(((u32 *)fw_ptr)[icnt]);
1658
1659 ret |= usb_bulk_msg(hdw->usb_dev, pipe, fw_ptr,bcnt,
1660 &actual_length, HZ);
1661 ret |= (actual_length != bcnt);
1662 if (ret) break;
1663 fw_done += bcnt;
1664 }
1665
1666 trace_firmware("upload of %s : %i / %i ",
1667 fw_files[fwidx],fw_done,fw_len);
1668
1669 kfree(fw_ptr);
1670 release_firmware(fw_entry);
1671
1672 if (ret) {
1673 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1674 "firmware2 upload transfer failure");
1675 goto done;
1676 }
1677
1678 /* Finish upload */
1679
1680 ret |= pvr2_write_register(hdw, 0x9054, 0xffffffff); /*reset hw blocks*/
1681 ret |= pvr2_write_register(hdw, 0x9058, 0xffffffe8); /*VPU ctrl*/
1682 ret |= pvr2_issue_simple_cmd(hdw,FX2CMD_MEMSEL | (1 << 8) | (0 << 16));
1683
1684 if (ret) {
1685 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1686 "firmware2 upload post-proc failure");
1687 }
1688
1689 done:
1690 if (hdw->hdw_desc->signal_routing_scheme ==
1691 PVR2_ROUTING_SCHEME_GOTVIEW) {
1692 /* Ensure that GPIO 11 is set to output for GOTVIEW
1693 hardware. */
1694 pvr2_hdw_gpio_chg_dir(hdw,(1 << 11),~0);
1695 }
1696 return ret;
1697}
1698
1699
1700static const char *pvr2_get_state_name(unsigned int st)
1701{
1702 if (st < ARRAY_SIZE(pvr2_state_names)) {
1703 return pvr2_state_names[st];
1704 }
1705 return "???";
1706}
1707
1708static int pvr2_decoder_enable(struct pvr2_hdw *hdw,int enablefl)
1709{
1710 /* Even though we really only care about the video decoder chip at
1711 this point, we'll broadcast stream on/off to all sub-devices
1712 anyway, just in case somebody else wants to hear the
1713 command... */
1714 pvr2_trace(PVR2_TRACE_CHIPS, "subdev v4l2 stream=%s",
1715 (enablefl ? "on" : "off"));
1716 v4l2_device_call_all(&hdw->v4l2_dev, 0, video, s_stream, enablefl);
1717 v4l2_device_call_all(&hdw->v4l2_dev, 0, audio, s_stream, enablefl);
1718 if (hdw->decoder_client_id) {
1719 /* We get here if the encoder has been noticed. Otherwise
1720 we'll issue a warning to the user (which should
1721 normally never happen). */
1722 return 0;
1723 }
1724 if (!hdw->flag_decoder_missed) {
1725 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1726 "WARNING: No decoder present");
1727 hdw->flag_decoder_missed = !0;
1728 trace_stbit("flag_decoder_missed",
1729 hdw->flag_decoder_missed);
1730 }
1731 return -EIO;
1732}
1733
1734
1735int pvr2_hdw_get_state(struct pvr2_hdw *hdw)
1736{
1737 return hdw->master_state;
1738}
1739
1740
1741static int pvr2_hdw_untrip_unlocked(struct pvr2_hdw *hdw)
1742{
1743 if (!hdw->flag_tripped) return 0;
1744 hdw->flag_tripped = 0;
1745 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1746 "Clearing driver error statuss");
1747 return !0;
1748}
1749
1750
1751int pvr2_hdw_untrip(struct pvr2_hdw *hdw)
1752{
1753 int fl;
1754 LOCK_TAKE(hdw->big_lock); do {
1755 fl = pvr2_hdw_untrip_unlocked(hdw);
1756 } while (0); LOCK_GIVE(hdw->big_lock);
1757 if (fl) pvr2_hdw_state_sched(hdw);
1758 return 0;
1759}
1760
1761
1762
1763
1764int pvr2_hdw_get_streaming(struct pvr2_hdw *hdw)
1765{
1766 return hdw->state_pipeline_req != 0;
1767}
1768
1769
1770int pvr2_hdw_set_streaming(struct pvr2_hdw *hdw,int enable_flag)
1771{
1772 int ret,st;
1773 LOCK_TAKE(hdw->big_lock); do {
1774 pvr2_hdw_untrip_unlocked(hdw);
1775 if ((!enable_flag) != !(hdw->state_pipeline_req)) {
1776 hdw->state_pipeline_req = enable_flag != 0;
1777 pvr2_trace(PVR2_TRACE_START_STOP,
1778 "/*--TRACE_STREAM--*/ %s",
1779 enable_flag ? "enable" : "disable");
1780 }
1781 pvr2_hdw_state_sched(hdw);
1782 } while (0); LOCK_GIVE(hdw->big_lock);
1783 if ((ret = pvr2_hdw_wait(hdw,0)) < 0) return ret;
1784 if (enable_flag) {
1785 while ((st = hdw->master_state) != PVR2_STATE_RUN) {
1786 if (st != PVR2_STATE_READY) return -EIO;
1787 if ((ret = pvr2_hdw_wait(hdw,st)) < 0) return ret;
1788 }
1789 }
1790 return 0;
1791}
1792
1793
1794int pvr2_hdw_set_stream_type(struct pvr2_hdw *hdw,enum pvr2_config config)
1795{
1796 int fl;
1797 LOCK_TAKE(hdw->big_lock);
1798 if ((fl = (hdw->desired_stream_type != config)) != 0) {
1799 hdw->desired_stream_type = config;
1800 hdw->state_pipeline_config = 0;
1801 trace_stbit("state_pipeline_config",
1802 hdw->state_pipeline_config);
1803 pvr2_hdw_state_sched(hdw);
1804 }
1805 LOCK_GIVE(hdw->big_lock);
1806 if (fl) return 0;
1807 return pvr2_hdw_wait(hdw,0);
1808}
1809
1810
1811static int get_default_tuner_type(struct pvr2_hdw *hdw)
1812{
1813 int unit_number = hdw->unit_number;
1814 int tp = -1;
1815 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1816 tp = tuner[unit_number];
1817 }
1818 if (tp < 0) return -EINVAL;
1819 hdw->tuner_type = tp;
1820 hdw->tuner_updated = !0;
1821 return 0;
1822}
1823
1824
1825static v4l2_std_id get_default_standard(struct pvr2_hdw *hdw)
1826{
1827 int unit_number = hdw->unit_number;
1828 int tp = 0;
1829 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1830 tp = video_std[unit_number];
1831 if (tp) return tp;
1832 }
1833 return 0;
1834}
1835
1836
1837static unsigned int get_default_error_tolerance(struct pvr2_hdw *hdw)
1838{
1839 int unit_number = hdw->unit_number;
1840 int tp = 0;
1841 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
1842 tp = tolerance[unit_number];
1843 }
1844 return tp;
1845}
1846
1847
1848static int pvr2_hdw_check_firmware(struct pvr2_hdw *hdw)
1849{
1850 /* Try a harmless request to fetch the eeprom's address over
1851 endpoint 1. See what happens. Only the full FX2 image can
1852 respond to this. If this probe fails then likely the FX2
1853 firmware needs be loaded. */
1854 int result;
1855 LOCK_TAKE(hdw->ctl_lock); do {
1856 hdw->cmd_buffer[0] = FX2CMD_GET_EEPROM_ADDR;
1857 result = pvr2_send_request_ex(hdw,HZ*1,!0,
1858 hdw->cmd_buffer,1,
1859 hdw->cmd_buffer,1);
1860 if (result < 0) break;
1861 } while(0); LOCK_GIVE(hdw->ctl_lock);
1862 if (result) {
1863 pvr2_trace(PVR2_TRACE_INIT,
1864 "Probe of device endpoint 1 result status %d",
1865 result);
1866 } else {
1867 pvr2_trace(PVR2_TRACE_INIT,
1868 "Probe of device endpoint 1 succeeded");
1869 }
1870 return result == 0;
1871}
1872
1873struct pvr2_std_hack {
1874 v4l2_std_id pat; /* Pattern to match */
1875 v4l2_std_id msk; /* Which bits we care about */
1876 v4l2_std_id std; /* What additional standards or default to set */
1877};
1878
1879/* This data structure labels specific combinations of standards from
1880 tveeprom that we'll try to recognize. If we recognize one, then assume
1881 a specified default standard to use. This is here because tveeprom only
1882 tells us about available standards not the intended default standard (if
1883 any) for the device in question. We guess the default based on what has
1884 been reported as available. Note that this is only for guessing a
1885 default - which can always be overridden explicitly - and if the user
1886 has otherwise named a default then that default will always be used in
1887 place of this table. */
1888static const struct pvr2_std_hack std_eeprom_maps[] = {
1889 { /* PAL(B/G) */
1890 .pat = V4L2_STD_B|V4L2_STD_GH,
1891 .std = V4L2_STD_PAL_B|V4L2_STD_PAL_B1|V4L2_STD_PAL_G,
1892 },
1893 { /* NTSC(M) */
1894 .pat = V4L2_STD_MN,
1895 .std = V4L2_STD_NTSC_M,
1896 },
1897 { /* PAL(I) */
1898 .pat = V4L2_STD_PAL_I,
1899 .std = V4L2_STD_PAL_I,
1900 },
1901 { /* SECAM(L/L') */
1902 .pat = V4L2_STD_SECAM_L|V4L2_STD_SECAM_LC,
1903 .std = V4L2_STD_SECAM_L|V4L2_STD_SECAM_LC,
1904 },
1905 { /* PAL(D/D1/K) */
1906 .pat = V4L2_STD_DK,
1907 .std = V4L2_STD_PAL_D|V4L2_STD_PAL_D1|V4L2_STD_PAL_K,
1908 },
1909};
1910
1911static void pvr2_hdw_setup_std(struct pvr2_hdw *hdw)
1912{
1913 char buf[40];
1914 unsigned int bcnt;
1915 v4l2_std_id std1,std2,std3;
1916
1917 std1 = get_default_standard(hdw);
1918 std3 = std1 ? 0 : hdw->hdw_desc->default_std_mask;
1919
1920 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),hdw->std_mask_eeprom);
1921 pvr2_trace(PVR2_TRACE_STD,
1922 "Supported video standard(s) reported available"
1923 " in hardware: %.*s",
1924 bcnt,buf);
1925
1926 hdw->std_mask_avail = hdw->std_mask_eeprom;
1927
1928 std2 = (std1|std3) & ~hdw->std_mask_avail;
1929 if (std2) {
1930 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std2);
1931 pvr2_trace(PVR2_TRACE_STD,
1932 "Expanding supported video standards"
1933 " to include: %.*s",
1934 bcnt,buf);
1935 hdw->std_mask_avail |= std2;
1936 }
1937
1938 pvr2_hdw_internal_set_std_avail(hdw);
1939
1940 if (std1) {
1941 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std1);
1942 pvr2_trace(PVR2_TRACE_STD,
1943 "Initial video standard forced to %.*s",
1944 bcnt,buf);
1945 hdw->std_mask_cur = std1;
1946 hdw->std_dirty = !0;
1947 pvr2_hdw_internal_find_stdenum(hdw);
1948 return;
1949 }
1950 if (std3) {
1951 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),std3);
1952 pvr2_trace(PVR2_TRACE_STD,
1953 "Initial video standard"
1954 " (determined by device type): %.*s",bcnt,buf);
1955 hdw->std_mask_cur = std3;
1956 hdw->std_dirty = !0;
1957 pvr2_hdw_internal_find_stdenum(hdw);
1958 return;
1959 }
1960
1961 {
1962 unsigned int idx;
1963 for (idx = 0; idx < ARRAY_SIZE(std_eeprom_maps); idx++) {
1964 if (std_eeprom_maps[idx].msk ?
1965 ((std_eeprom_maps[idx].pat ^
1966 hdw->std_mask_eeprom) &
1967 std_eeprom_maps[idx].msk) :
1968 (std_eeprom_maps[idx].pat !=
1969 hdw->std_mask_eeprom)) continue;
1970 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),
1971 std_eeprom_maps[idx].std);
1972 pvr2_trace(PVR2_TRACE_STD,
1973 "Initial video standard guessed as %.*s",
1974 bcnt,buf);
1975 hdw->std_mask_cur = std_eeprom_maps[idx].std;
1976 hdw->std_dirty = !0;
1977 pvr2_hdw_internal_find_stdenum(hdw);
1978 return;
1979 }
1980 }
1981
1982 if (hdw->std_enum_cnt > 1) {
1983 // Autoselect the first listed standard
1984 hdw->std_enum_cur = 1;
1985 hdw->std_mask_cur = hdw->std_defs[hdw->std_enum_cur-1].id;
1986 hdw->std_dirty = !0;
1987 pvr2_trace(PVR2_TRACE_STD,
1988 "Initial video standard auto-selected to %s",
1989 hdw->std_defs[hdw->std_enum_cur-1].name);
1990 return;
1991 }
1992
1993 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
1994 "Unable to select a viable initial video standard");
1995}
1996
1997
1998static unsigned int pvr2_copy_i2c_addr_list(
1999 unsigned short *dst, const unsigned char *src,
2000 unsigned int dst_max)
2001{
2002 unsigned int cnt = 0;
2003 if (!src) return 0;
2004 while (src[cnt] && (cnt + 1) < dst_max) {
2005 dst[cnt] = src[cnt];
2006 cnt++;
2007 }
2008 dst[cnt] = I2C_CLIENT_END;
2009 return cnt;
2010}
2011
2012
2013static void pvr2_hdw_cx25840_vbi_hack(struct pvr2_hdw *hdw)
2014{
2015 /*
2016 Mike Isely <isely@pobox.com> 19-Nov-2006 - This bit of nuttiness
2017 for cx25840 causes that module to correctly set up its video
2018 scaling. This is really a problem in the cx25840 module itself,
2019 but we work around it here. The problem has not been seen in
2020 ivtv because there VBI is supported and set up. We don't do VBI
2021 here (at least not yet) and thus we never attempted to even set
2022 it up.
2023 */
2024 struct v4l2_format fmt;
2025 if (hdw->decoder_client_id != PVR2_CLIENT_ID_CX25840) {
2026 /* We're not using a cx25840 so don't enable the hack */
2027 return;
2028 }
2029
2030 pvr2_trace(PVR2_TRACE_INIT,
2031 "Module ID %u:"
2032 " Executing cx25840 VBI hack",
2033 hdw->decoder_client_id);
2034 memset(&fmt, 0, sizeof(fmt));
2035 fmt.type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
2036 fmt.fmt.sliced.service_lines[0][21] = V4L2_SLICED_CAPTION_525;
2037 fmt.fmt.sliced.service_lines[1][21] = V4L2_SLICED_CAPTION_525;
2038 v4l2_device_call_all(&hdw->v4l2_dev, hdw->decoder_client_id,
2039 vbi, s_sliced_fmt, &fmt.fmt.sliced);
2040}
2041
2042
2043static int pvr2_hdw_load_subdev(struct pvr2_hdw *hdw,
2044 const struct pvr2_device_client_desc *cd)
2045{
2046 const char *fname;
2047 unsigned char mid;
2048 struct v4l2_subdev *sd;
2049 unsigned int i2ccnt;
2050 const unsigned char *p;
2051 /* Arbitrary count - max # i2c addresses we will probe */
2052 unsigned short i2caddr[25];
2053
2054 mid = cd->module_id;
2055 fname = (mid < ARRAY_SIZE(module_names)) ? module_names[mid] : NULL;
2056 if (!fname) {
2057 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2058 "Module ID %u for device %s has no name?"
2059 " The driver might have a configuration problem.",
2060 mid,
2061 hdw->hdw_desc->description);
2062 return -EINVAL;
2063 }
2064 pvr2_trace(PVR2_TRACE_INIT,
2065 "Module ID %u (%s) for device %s being loaded...",
2066 mid, fname,
2067 hdw->hdw_desc->description);
2068
2069 i2ccnt = pvr2_copy_i2c_addr_list(i2caddr, cd->i2c_address_list,
2070 ARRAY_SIZE(i2caddr));
2071 if (!i2ccnt && ((p = (mid < ARRAY_SIZE(module_i2c_addresses)) ?
2072 module_i2c_addresses[mid] : NULL) != NULL)) {
2073 /* Second chance: Try default i2c address list */
2074 i2ccnt = pvr2_copy_i2c_addr_list(i2caddr, p,
2075 ARRAY_SIZE(i2caddr));
2076 if (i2ccnt) {
2077 pvr2_trace(PVR2_TRACE_INIT,
2078 "Module ID %u:"
2079 " Using default i2c address list",
2080 mid);
2081 }
2082 }
2083
2084 if (!i2ccnt) {
2085 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2086 "Module ID %u (%s) for device %s:"
2087 " No i2c addresses."
2088 " The driver might have a configuration problem.",
2089 mid, fname, hdw->hdw_desc->description);
2090 return -EINVAL;
2091 }
2092
2093 if (i2ccnt == 1) {
2094 pvr2_trace(PVR2_TRACE_INIT,
2095 "Module ID %u:"
2096 " Setting up with specified i2c address 0x%x",
2097 mid, i2caddr[0]);
2098 sd = v4l2_i2c_new_subdev(&hdw->v4l2_dev, &hdw->i2c_adap,
2099 fname, i2caddr[0], NULL);
2100 } else {
2101 pvr2_trace(PVR2_TRACE_INIT,
2102 "Module ID %u:"
2103 " Setting up with address probe list",
2104 mid);
2105 sd = v4l2_i2c_new_subdev(&hdw->v4l2_dev, &hdw->i2c_adap,
2106 fname, 0, i2caddr);
2107 }
2108
2109 if (!sd) {
2110 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2111 "Module ID %u (%s) for device %s failed to load."
2112 " Possible missing sub-device kernel module or"
2113 " initialization failure within module.",
2114 mid, fname, hdw->hdw_desc->description);
2115 return -EIO;
2116 }
2117
2118 /* Tag this sub-device instance with the module ID we know about.
2119 In other places we'll use that tag to determine if the instance
2120 requires special handling. */
2121 sd->grp_id = mid;
2122
2123 pvr2_trace(PVR2_TRACE_INFO, "Attached sub-driver %s", fname);
2124
2125
2126 /* client-specific setup... */
2127 switch (mid) {
2128 case PVR2_CLIENT_ID_CX25840:
2129 case PVR2_CLIENT_ID_SAA7115:
2130 hdw->decoder_client_id = mid;
2131 break;
2132 default: break;
2133 }
2134
2135 return 0;
2136}
2137
2138
2139static void pvr2_hdw_load_modules(struct pvr2_hdw *hdw)
2140{
2141 unsigned int idx;
2142 const struct pvr2_string_table *cm;
2143 const struct pvr2_device_client_table *ct;
2144 int okFl = !0;
2145
2146 cm = &hdw->hdw_desc->client_modules;
2147 for (idx = 0; idx < cm->cnt; idx++) {
2148 request_module(cm->lst[idx]);
2149 }
2150
2151 ct = &hdw->hdw_desc->client_table;
2152 for (idx = 0; idx < ct->cnt; idx++) {
2153 if (pvr2_hdw_load_subdev(hdw, &ct->lst[idx]) < 0) okFl = 0;
2154 }
2155 if (!okFl) {
2156 hdw->flag_modulefail = !0;
2157 pvr2_hdw_render_useless(hdw);
2158 }
2159}
2160
2161
2162static void pvr2_hdw_setup_low(struct pvr2_hdw *hdw)
2163{
2164 int ret;
2165 unsigned int idx;
2166 struct pvr2_ctrl *cptr;
2167 int reloadFl = 0;
2168 if (hdw->hdw_desc->fx2_firmware.cnt) {
2169 if (!reloadFl) {
2170 reloadFl =
2171 (hdw->usb_intf->cur_altsetting->desc.bNumEndpoints
2172 == 0);
2173 if (reloadFl) {
2174 pvr2_trace(PVR2_TRACE_INIT,
2175 "USB endpoint config looks strange"
2176 "; possibly firmware needs to be"
2177 " loaded");
2178 }
2179 }
2180 if (!reloadFl) {
2181 reloadFl = !pvr2_hdw_check_firmware(hdw);
2182 if (reloadFl) {
2183 pvr2_trace(PVR2_TRACE_INIT,
2184 "Check for FX2 firmware failed"
2185 "; possibly firmware needs to be"
2186 " loaded");
2187 }
2188 }
2189 if (reloadFl) {
2190 if (pvr2_upload_firmware1(hdw) != 0) {
2191 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2192 "Failure uploading firmware1");
2193 }
2194 return;
2195 }
2196 }
2197 hdw->fw1_state = FW1_STATE_OK;
2198
2199 if (!pvr2_hdw_dev_ok(hdw)) return;
2200
2201 hdw->force_dirty = !0;
2202
2203 if (!hdw->hdw_desc->flag_no_powerup) {
2204 pvr2_hdw_cmd_powerup(hdw);
2205 if (!pvr2_hdw_dev_ok(hdw)) return;
2206 }
2207
2208 /* Take the IR chip out of reset, if appropriate */
2209 if (hdw->ir_scheme_active == PVR2_IR_SCHEME_ZILOG) {
2210 pvr2_issue_simple_cmd(hdw,
2211 FX2CMD_HCW_ZILOG_RESET |
2212 (1 << 8) |
2213 ((0) << 16));
2214 }
2215
2216 // This step MUST happen after the earlier powerup step.
2217 pvr2_i2c_core_init(hdw);
2218 if (!pvr2_hdw_dev_ok(hdw)) return;
2219
2220 pvr2_hdw_load_modules(hdw);
2221 if (!pvr2_hdw_dev_ok(hdw)) return;
2222
2223 v4l2_device_call_all(&hdw->v4l2_dev, 0, core, load_fw);
2224
2225 for (idx = 0; idx < CTRLDEF_COUNT; idx++) {
2226 cptr = hdw->controls + idx;
2227 if (cptr->info->skip_init) continue;
2228 if (!cptr->info->set_value) continue;
2229 cptr->info->set_value(cptr,~0,cptr->info->default_value);
2230 }
2231
2232 pvr2_hdw_cx25840_vbi_hack(hdw);
2233
2234 /* Set up special default values for the television and radio
2235 frequencies here. It's not really important what these defaults
2236 are, but I set them to something usable in the Chicago area just
2237 to make driver testing a little easier. */
2238
2239 hdw->freqValTelevision = default_tv_freq;
2240 hdw->freqValRadio = default_radio_freq;
2241
2242 // Do not use pvr2_reset_ctl_endpoints() here. It is not
2243 // thread-safe against the normal pvr2_send_request() mechanism.
2244 // (We should make it thread safe).
2245
2246 if (hdw->hdw_desc->flag_has_hauppauge_rom) {
2247 ret = pvr2_hdw_get_eeprom_addr(hdw);
2248 if (!pvr2_hdw_dev_ok(hdw)) return;
2249 if (ret < 0) {
2250 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2251 "Unable to determine location of eeprom,"
2252 " skipping");
2253 } else {
2254 hdw->eeprom_addr = ret;
2255 pvr2_eeprom_analyze(hdw);
2256 if (!pvr2_hdw_dev_ok(hdw)) return;
2257 }
2258 } else {
2259 hdw->tuner_type = hdw->hdw_desc->default_tuner_type;
2260 hdw->tuner_updated = !0;
2261 hdw->std_mask_eeprom = V4L2_STD_ALL;
2262 }
2263
2264 if (hdw->serial_number) {
2265 idx = scnprintf(hdw->identifier, sizeof(hdw->identifier) - 1,
2266 "sn-%lu", hdw->serial_number);
2267 } else if (hdw->unit_number >= 0) {
2268 idx = scnprintf(hdw->identifier, sizeof(hdw->identifier) - 1,
2269 "unit-%c",
2270 hdw->unit_number + 'a');
2271 } else {
2272 idx = scnprintf(hdw->identifier, sizeof(hdw->identifier) - 1,
2273 "unit-??");
2274 }
2275 hdw->identifier[idx] = 0;
2276
2277 pvr2_hdw_setup_std(hdw);
2278
2279 if (!get_default_tuner_type(hdw)) {
2280 pvr2_trace(PVR2_TRACE_INIT,
2281 "pvr2_hdw_setup: Tuner type overridden to %d",
2282 hdw->tuner_type);
2283 }
2284
2285
2286 if (!pvr2_hdw_dev_ok(hdw)) return;
2287
2288 if (hdw->hdw_desc->signal_routing_scheme ==
2289 PVR2_ROUTING_SCHEME_GOTVIEW) {
2290 /* Ensure that GPIO 11 is set to output for GOTVIEW
2291 hardware. */
2292 pvr2_hdw_gpio_chg_dir(hdw,(1 << 11),~0);
2293 }
2294
2295 pvr2_hdw_commit_setup(hdw);
2296
2297 hdw->vid_stream = pvr2_stream_create();
2298 if (!pvr2_hdw_dev_ok(hdw)) return;
2299 pvr2_trace(PVR2_TRACE_INIT,
2300 "pvr2_hdw_setup: video stream is %p",hdw->vid_stream);
2301 if (hdw->vid_stream) {
2302 idx = get_default_error_tolerance(hdw);
2303 if (idx) {
2304 pvr2_trace(PVR2_TRACE_INIT,
2305 "pvr2_hdw_setup: video stream %p"
2306 " setting tolerance %u",
2307 hdw->vid_stream,idx);
2308 }
2309 pvr2_stream_setup(hdw->vid_stream,hdw->usb_dev,
2310 PVR2_VID_ENDPOINT,idx);
2311 }
2312
2313 if (!pvr2_hdw_dev_ok(hdw)) return;
2314
2315 hdw->flag_init_ok = !0;
2316
2317 pvr2_hdw_state_sched(hdw);
2318}
2319
2320
2321/* Set up the structure and attempt to put the device into a usable state.
2322 This can be a time-consuming operation, which is why it is not done
2323 internally as part of the create() step. */
2324static void pvr2_hdw_setup(struct pvr2_hdw *hdw)
2325{
2326 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_setup(hdw=%p) begin",hdw);
2327 do {
2328 pvr2_hdw_setup_low(hdw);
2329 pvr2_trace(PVR2_TRACE_INIT,
2330 "pvr2_hdw_setup(hdw=%p) done, ok=%d init_ok=%d",
2331 hdw,pvr2_hdw_dev_ok(hdw),hdw->flag_init_ok);
2332 if (pvr2_hdw_dev_ok(hdw)) {
2333 if (hdw->flag_init_ok) {
2334 pvr2_trace(
2335 PVR2_TRACE_INFO,
2336 "Device initialization"
2337 " completed successfully.");
2338 break;
2339 }
2340 if (hdw->fw1_state == FW1_STATE_RELOAD) {
2341 pvr2_trace(
2342 PVR2_TRACE_INFO,
2343 "Device microcontroller firmware"
2344 " (re)loaded; it should now reset"
2345 " and reconnect.");
2346 break;
2347 }
2348 pvr2_trace(
2349 PVR2_TRACE_ERROR_LEGS,
2350 "Device initialization was not successful.");
2351 if (hdw->fw1_state == FW1_STATE_MISSING) {
2352 pvr2_trace(
2353 PVR2_TRACE_ERROR_LEGS,
2354 "Giving up since device"
2355 " microcontroller firmware"
2356 " appears to be missing.");
2357 break;
2358 }
2359 }
2360 if (hdw->flag_modulefail) {
2361 pvr2_trace(
2362 PVR2_TRACE_ERROR_LEGS,
2363 "***WARNING*** pvrusb2 driver initialization"
2364 " failed due to the failure of one or more"
2365 " sub-device kernel modules.");
2366 pvr2_trace(
2367 PVR2_TRACE_ERROR_LEGS,
2368 "You need to resolve the failing condition"
2369 " before this driver can function. There"
2370 " should be some earlier messages giving more"
2371 " information about the problem.");
2372 break;
2373 }
2374 if (procreload) {
2375 pvr2_trace(
2376 PVR2_TRACE_ERROR_LEGS,
2377 "Attempting pvrusb2 recovery by reloading"
2378 " primary firmware.");
2379 pvr2_trace(
2380 PVR2_TRACE_ERROR_LEGS,
2381 "If this works, device should disconnect"
2382 " and reconnect in a sane state.");
2383 hdw->fw1_state = FW1_STATE_UNKNOWN;
2384 pvr2_upload_firmware1(hdw);
2385 } else {
2386 pvr2_trace(
2387 PVR2_TRACE_ERROR_LEGS,
2388 "***WARNING*** pvrusb2 device hardware"
2389 " appears to be jammed"
2390 " and I can't clear it.");
2391 pvr2_trace(
2392 PVR2_TRACE_ERROR_LEGS,
2393 "You might need to power cycle"
2394 " the pvrusb2 device"
2395 " in order to recover.");
2396 }
2397 } while (0);
2398 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_setup(hdw=%p) end",hdw);
2399}
2400
2401
2402/* Perform second stage initialization. Set callback pointer first so that
2403 we can avoid a possible initialization race (if the kernel thread runs
2404 before the callback has been set). */
2405int pvr2_hdw_initialize(struct pvr2_hdw *hdw,
2406 void (*callback_func)(void *),
2407 void *callback_data)
2408{
2409 LOCK_TAKE(hdw->big_lock); do {
2410 if (hdw->flag_disconnected) {
2411 /* Handle a race here: If we're already
2412 disconnected by this point, then give up. If we
2413 get past this then we'll remain connected for
2414 the duration of initialization since the entire
2415 initialization sequence is now protected by the
2416 big_lock. */
2417 break;
2418 }
2419 hdw->state_data = callback_data;
2420 hdw->state_func = callback_func;
2421 pvr2_hdw_setup(hdw);
2422 } while (0); LOCK_GIVE(hdw->big_lock);
2423 return hdw->flag_init_ok;
2424}
2425
2426
2427/* Create, set up, and return a structure for interacting with the
2428 underlying hardware. */
2429struct pvr2_hdw *pvr2_hdw_create(struct usb_interface *intf,
2430 const struct usb_device_id *devid)
2431{
2432 unsigned int idx,cnt1,cnt2,m;
2433 struct pvr2_hdw *hdw = NULL;
2434 int valid_std_mask;
2435 struct pvr2_ctrl *cptr;
2436 struct usb_device *usb_dev;
2437 const struct pvr2_device_desc *hdw_desc;
2438 __u8 ifnum;
2439 struct v4l2_queryctrl qctrl;
2440 struct pvr2_ctl_info *ciptr;
2441
2442 usb_dev = interface_to_usbdev(intf);
2443
2444 hdw_desc = (const struct pvr2_device_desc *)(devid->driver_info);
2445
2446 if (hdw_desc == NULL) {
2447 pvr2_trace(PVR2_TRACE_INIT, "pvr2_hdw_create:"
2448 " No device description pointer,"
2449 " unable to continue.");
2450 pvr2_trace(PVR2_TRACE_INIT, "If you have a new device type,"
2451 " please contact Mike Isely <isely@pobox.com>"
2452 " to get it included in the driver\n");
2453 goto fail;
2454 }
2455
2456 hdw = kzalloc(sizeof(*hdw),GFP_KERNEL);
2457 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_create: hdw=%p, type \"%s\"",
2458 hdw,hdw_desc->description);
2459 pvr2_trace(PVR2_TRACE_INFO, "Hardware description: %s",
2460 hdw_desc->description);
2461 if (hdw_desc->flag_is_experimental) {
2462 pvr2_trace(PVR2_TRACE_INFO, "**********");
2463 pvr2_trace(PVR2_TRACE_INFO,
2464 "WARNING: Support for this device (%s) is"
2465 " experimental.", hdw_desc->description);
2466 pvr2_trace(PVR2_TRACE_INFO,
2467 "Important functionality might not be"
2468 " entirely working.");
2469 pvr2_trace(PVR2_TRACE_INFO,
2470 "Please consider contacting the driver author to"
2471 " help with further stabilization of the driver.");
2472 pvr2_trace(PVR2_TRACE_INFO, "**********");
2473 }
2474 if (!hdw) goto fail;
2475
2476 init_timer(&hdw->quiescent_timer);
2477 hdw->quiescent_timer.data = (unsigned long)hdw;
2478 hdw->quiescent_timer.function = pvr2_hdw_quiescent_timeout;
2479
2480 init_timer(&hdw->decoder_stabilization_timer);
2481 hdw->decoder_stabilization_timer.data = (unsigned long)hdw;
2482 hdw->decoder_stabilization_timer.function =
2483 pvr2_hdw_decoder_stabilization_timeout;
2484
2485 init_timer(&hdw->encoder_wait_timer);
2486 hdw->encoder_wait_timer.data = (unsigned long)hdw;
2487 hdw->encoder_wait_timer.function = pvr2_hdw_encoder_wait_timeout;
2488
2489 init_timer(&hdw->encoder_run_timer);
2490 hdw->encoder_run_timer.data = (unsigned long)hdw;
2491 hdw->encoder_run_timer.function = pvr2_hdw_encoder_run_timeout;
2492
2493 hdw->master_state = PVR2_STATE_DEAD;
2494
2495 init_waitqueue_head(&hdw->state_wait_data);
2496
2497 hdw->tuner_signal_stale = !0;
2498 cx2341x_fill_defaults(&hdw->enc_ctl_state);
2499
2500 /* Calculate which inputs are OK */
2501 m = 0;
2502 if (hdw_desc->flag_has_analogtuner) m |= 1 << PVR2_CVAL_INPUT_TV;
2503 if (hdw_desc->digital_control_scheme != PVR2_DIGITAL_SCHEME_NONE) {
2504 m |= 1 << PVR2_CVAL_INPUT_DTV;
2505 }
2506 if (hdw_desc->flag_has_svideo) m |= 1 << PVR2_CVAL_INPUT_SVIDEO;
2507 if (hdw_desc->flag_has_composite) m |= 1 << PVR2_CVAL_INPUT_COMPOSITE;
2508 if (hdw_desc->flag_has_fmradio) m |= 1 << PVR2_CVAL_INPUT_RADIO;
2509 hdw->input_avail_mask = m;
2510 hdw->input_allowed_mask = hdw->input_avail_mask;
2511
2512 /* If not a hybrid device, pathway_state never changes. So
2513 initialize it here to what it should forever be. */
2514 if (!(hdw->input_avail_mask & (1 << PVR2_CVAL_INPUT_DTV))) {
2515 hdw->pathway_state = PVR2_PATHWAY_ANALOG;
2516 } else if (!(hdw->input_avail_mask & (1 << PVR2_CVAL_INPUT_TV))) {
2517 hdw->pathway_state = PVR2_PATHWAY_DIGITAL;
2518 }
2519
2520 hdw->control_cnt = CTRLDEF_COUNT;
2521 hdw->control_cnt += MPEGDEF_COUNT;
2522 hdw->controls = kzalloc(sizeof(struct pvr2_ctrl) * hdw->control_cnt,
2523 GFP_KERNEL);
2524 if (!hdw->controls) goto fail;
2525 hdw->hdw_desc = hdw_desc;
2526 hdw->ir_scheme_active = hdw->hdw_desc->ir_scheme;
2527 for (idx = 0; idx < hdw->control_cnt; idx++) {
2528 cptr = hdw->controls + idx;
2529 cptr->hdw = hdw;
2530 }
2531 for (idx = 0; idx < 32; idx++) {
2532 hdw->std_mask_ptrs[idx] = hdw->std_mask_names[idx];
2533 }
2534 for (idx = 0; idx < CTRLDEF_COUNT; idx++) {
2535 cptr = hdw->controls + idx;
2536 cptr->info = control_defs+idx;
2537 }
2538
2539 /* Ensure that default input choice is a valid one. */
2540 m = hdw->input_avail_mask;
2541 if (m) for (idx = 0; idx < (sizeof(m) << 3); idx++) {
2542 if (!((1 << idx) & m)) continue;
2543 hdw->input_val = idx;
2544 break;
2545 }
2546
2547 /* Define and configure additional controls from cx2341x module. */
2548 hdw->mpeg_ctrl_info = kzalloc(
2549 sizeof(*(hdw->mpeg_ctrl_info)) * MPEGDEF_COUNT, GFP_KERNEL);
2550 if (!hdw->mpeg_ctrl_info) goto fail;
2551 for (idx = 0; idx < MPEGDEF_COUNT; idx++) {
2552 cptr = hdw->controls + idx + CTRLDEF_COUNT;
2553 ciptr = &(hdw->mpeg_ctrl_info[idx].info);
2554 ciptr->desc = hdw->mpeg_ctrl_info[idx].desc;
2555 ciptr->name = mpeg_ids[idx].strid;
2556 ciptr->v4l_id = mpeg_ids[idx].id;
2557 ciptr->skip_init = !0;
2558 ciptr->get_value = ctrl_cx2341x_get;
2559 ciptr->get_v4lflags = ctrl_cx2341x_getv4lflags;
2560 ciptr->is_dirty = ctrl_cx2341x_is_dirty;
2561 if (!idx) ciptr->clear_dirty = ctrl_cx2341x_clear_dirty;
2562 qctrl.id = ciptr->v4l_id;
2563 cx2341x_ctrl_query(&hdw->enc_ctl_state,&qctrl);
2564 if (!(qctrl.flags & V4L2_CTRL_FLAG_READ_ONLY)) {
2565 ciptr->set_value = ctrl_cx2341x_set;
2566 }
2567 strncpy(hdw->mpeg_ctrl_info[idx].desc,qctrl.name,
2568 PVR2_CTLD_INFO_DESC_SIZE);
2569 hdw->mpeg_ctrl_info[idx].desc[PVR2_CTLD_INFO_DESC_SIZE-1] = 0;
2570 ciptr->default_value = qctrl.default_value;
2571 switch (qctrl.type) {
2572 default:
2573 case V4L2_CTRL_TYPE_INTEGER:
2574 ciptr->type = pvr2_ctl_int;
2575 ciptr->def.type_int.min_value = qctrl.minimum;
2576 ciptr->def.type_int.max_value = qctrl.maximum;
2577 break;
2578 case V4L2_CTRL_TYPE_BOOLEAN:
2579 ciptr->type = pvr2_ctl_bool;
2580 break;
2581 case V4L2_CTRL_TYPE_MENU:
2582 ciptr->type = pvr2_ctl_enum;
2583 ciptr->def.type_enum.value_names =
2584 cx2341x_ctrl_get_menu(&hdw->enc_ctl_state,
2585 ciptr->v4l_id);
2586 for (cnt1 = 0;
2587 ciptr->def.type_enum.value_names[cnt1] != NULL;
2588 cnt1++) { }
2589 ciptr->def.type_enum.count = cnt1;
2590 break;
2591 }
2592 cptr->info = ciptr;
2593 }
2594
2595 // Initialize video standard enum dynamic control
2596 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDENUM);
2597 if (cptr) {
2598 memcpy(&hdw->std_info_enum,cptr->info,
2599 sizeof(hdw->std_info_enum));
2600 cptr->info = &hdw->std_info_enum;
2601
2602 }
2603 // Initialize control data regarding video standard masks
2604 valid_std_mask = pvr2_std_get_usable();
2605 for (idx = 0; idx < 32; idx++) {
2606 if (!(valid_std_mask & (1 << idx))) continue;
2607 cnt1 = pvr2_std_id_to_str(
2608 hdw->std_mask_names[idx],
2609 sizeof(hdw->std_mask_names[idx])-1,
2610 1 << idx);
2611 hdw->std_mask_names[idx][cnt1] = 0;
2612 }
2613 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDAVAIL);
2614 if (cptr) {
2615 memcpy(&hdw->std_info_avail,cptr->info,
2616 sizeof(hdw->std_info_avail));
2617 cptr->info = &hdw->std_info_avail;
2618 hdw->std_info_avail.def.type_bitmask.bit_names =
2619 hdw->std_mask_ptrs;
2620 hdw->std_info_avail.def.type_bitmask.valid_bits =
2621 valid_std_mask;
2622 }
2623 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR);
2624 if (cptr) {
2625 memcpy(&hdw->std_info_cur,cptr->info,
2626 sizeof(hdw->std_info_cur));
2627 cptr->info = &hdw->std_info_cur;
2628 hdw->std_info_cur.def.type_bitmask.bit_names =
2629 hdw->std_mask_ptrs;
2630 hdw->std_info_avail.def.type_bitmask.valid_bits =
2631 valid_std_mask;
2632 }
2633
2634 hdw->cropcap_stale = !0;
2635 hdw->eeprom_addr = -1;
2636 hdw->unit_number = -1;
2637 hdw->v4l_minor_number_video = -1;
2638 hdw->v4l_minor_number_vbi = -1;
2639 hdw->v4l_minor_number_radio = -1;
2640 hdw->ctl_write_buffer = kmalloc(PVR2_CTL_BUFFSIZE,GFP_KERNEL);
2641 if (!hdw->ctl_write_buffer) goto fail;
2642 hdw->ctl_read_buffer = kmalloc(PVR2_CTL_BUFFSIZE,GFP_KERNEL);
2643 if (!hdw->ctl_read_buffer) goto fail;
2644 hdw->ctl_write_urb = usb_alloc_urb(0,GFP_KERNEL);
2645 if (!hdw->ctl_write_urb) goto fail;
2646 hdw->ctl_read_urb = usb_alloc_urb(0,GFP_KERNEL);
2647 if (!hdw->ctl_read_urb) goto fail;
2648
2649 if (v4l2_device_register(&intf->dev, &hdw->v4l2_dev) != 0) {
2650 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2651 "Error registering with v4l core, giving up");
2652 goto fail;
2653 }
2654 mutex_lock(&pvr2_unit_mtx); do {
2655 for (idx = 0; idx < PVR_NUM; idx++) {
2656 if (unit_pointers[idx]) continue;
2657 hdw->unit_number = idx;
2658 unit_pointers[idx] = hdw;
2659 break;
2660 }
2661 } while (0); mutex_unlock(&pvr2_unit_mtx);
2662
2663 cnt1 = 0;
2664 cnt2 = scnprintf(hdw->name+cnt1,sizeof(hdw->name)-cnt1,"pvrusb2");
2665 cnt1 += cnt2;
2666 if (hdw->unit_number >= 0) {
2667 cnt2 = scnprintf(hdw->name+cnt1,sizeof(hdw->name)-cnt1,"_%c",
2668 ('a' + hdw->unit_number));
2669 cnt1 += cnt2;
2670 }
2671 if (cnt1 >= sizeof(hdw->name)) cnt1 = sizeof(hdw->name)-1;
2672 hdw->name[cnt1] = 0;
2673
2674 hdw->workqueue = create_singlethread_workqueue(hdw->name);
2675 INIT_WORK(&hdw->workpoll,pvr2_hdw_worker_poll);
2676
2677 pvr2_trace(PVR2_TRACE_INIT,"Driver unit number is %d, name is %s",
2678 hdw->unit_number,hdw->name);
2679
2680 hdw->tuner_type = -1;
2681 hdw->flag_ok = !0;
2682
2683 hdw->usb_intf = intf;
2684 hdw->usb_dev = usb_dev;
2685
2686 usb_make_path(hdw->usb_dev, hdw->bus_info, sizeof(hdw->bus_info));
2687
2688 ifnum = hdw->usb_intf->cur_altsetting->desc.bInterfaceNumber;
2689 usb_set_interface(hdw->usb_dev,ifnum,0);
2690
2691 mutex_init(&hdw->ctl_lock_mutex);
2692 mutex_init(&hdw->big_lock_mutex);
2693
2694 return hdw;
2695 fail:
2696 if (hdw) {
2697 del_timer_sync(&hdw->quiescent_timer);
2698 del_timer_sync(&hdw->decoder_stabilization_timer);
2699 del_timer_sync(&hdw->encoder_run_timer);
2700 del_timer_sync(&hdw->encoder_wait_timer);
2701 if (hdw->workqueue) {
2702 flush_workqueue(hdw->workqueue);
2703 destroy_workqueue(hdw->workqueue);
2704 hdw->workqueue = NULL;
2705 }
2706 usb_free_urb(hdw->ctl_read_urb);
2707 usb_free_urb(hdw->ctl_write_urb);
2708 kfree(hdw->ctl_read_buffer);
2709 kfree(hdw->ctl_write_buffer);
2710 kfree(hdw->controls);
2711 kfree(hdw->mpeg_ctrl_info);
2712 kfree(hdw->std_defs);
2713 kfree(hdw->std_enum_names);
2714 kfree(hdw);
2715 }
2716 return NULL;
2717}
2718
2719
2720/* Remove _all_ associations between this driver and the underlying USB
2721 layer. */
2722static void pvr2_hdw_remove_usb_stuff(struct pvr2_hdw *hdw)
2723{
2724 if (hdw->flag_disconnected) return;
2725 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_remove_usb_stuff: hdw=%p",hdw);
2726 if (hdw->ctl_read_urb) {
2727 usb_kill_urb(hdw->ctl_read_urb);
2728 usb_free_urb(hdw->ctl_read_urb);
2729 hdw->ctl_read_urb = NULL;
2730 }
2731 if (hdw->ctl_write_urb) {
2732 usb_kill_urb(hdw->ctl_write_urb);
2733 usb_free_urb(hdw->ctl_write_urb);
2734 hdw->ctl_write_urb = NULL;
2735 }
2736 if (hdw->ctl_read_buffer) {
2737 kfree(hdw->ctl_read_buffer);
2738 hdw->ctl_read_buffer = NULL;
2739 }
2740 if (hdw->ctl_write_buffer) {
2741 kfree(hdw->ctl_write_buffer);
2742 hdw->ctl_write_buffer = NULL;
2743 }
2744 hdw->flag_disconnected = !0;
2745 /* If we don't do this, then there will be a dangling struct device
2746 reference to our disappearing device persisting inside the V4L
2747 core... */
2748 v4l2_device_disconnect(&hdw->v4l2_dev);
2749 hdw->usb_dev = NULL;
2750 hdw->usb_intf = NULL;
2751 pvr2_hdw_render_useless(hdw);
2752}
2753
2754
2755/* Destroy hardware interaction structure */
2756void pvr2_hdw_destroy(struct pvr2_hdw *hdw)
2757{
2758 if (!hdw) return;
2759 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_destroy: hdw=%p",hdw);
2760 if (hdw->workqueue) {
2761 flush_workqueue(hdw->workqueue);
2762 destroy_workqueue(hdw->workqueue);
2763 hdw->workqueue = NULL;
2764 }
2765 del_timer_sync(&hdw->quiescent_timer);
2766 del_timer_sync(&hdw->decoder_stabilization_timer);
2767 del_timer_sync(&hdw->encoder_run_timer);
2768 del_timer_sync(&hdw->encoder_wait_timer);
2769 if (hdw->fw_buffer) {
2770 kfree(hdw->fw_buffer);
2771 hdw->fw_buffer = NULL;
2772 }
2773 if (hdw->vid_stream) {
2774 pvr2_stream_destroy(hdw->vid_stream);
2775 hdw->vid_stream = NULL;
2776 }
2777 pvr2_i2c_core_done(hdw);
2778 v4l2_device_unregister(&hdw->v4l2_dev);
2779 pvr2_hdw_remove_usb_stuff(hdw);
2780 mutex_lock(&pvr2_unit_mtx); do {
2781 if ((hdw->unit_number >= 0) &&
2782 (hdw->unit_number < PVR_NUM) &&
2783 (unit_pointers[hdw->unit_number] == hdw)) {
2784 unit_pointers[hdw->unit_number] = NULL;
2785 }
2786 } while (0); mutex_unlock(&pvr2_unit_mtx);
2787 kfree(hdw->controls);
2788 kfree(hdw->mpeg_ctrl_info);
2789 kfree(hdw->std_defs);
2790 kfree(hdw->std_enum_names);
2791 kfree(hdw);
2792}
2793
2794
2795int pvr2_hdw_dev_ok(struct pvr2_hdw *hdw)
2796{
2797 return (hdw && hdw->flag_ok);
2798}
2799
2800
2801/* Called when hardware has been unplugged */
2802void pvr2_hdw_disconnect(struct pvr2_hdw *hdw)
2803{
2804 pvr2_trace(PVR2_TRACE_INIT,"pvr2_hdw_disconnect(hdw=%p)",hdw);
2805 LOCK_TAKE(hdw->big_lock);
2806 LOCK_TAKE(hdw->ctl_lock);
2807 pvr2_hdw_remove_usb_stuff(hdw);
2808 LOCK_GIVE(hdw->ctl_lock);
2809 LOCK_GIVE(hdw->big_lock);
2810}
2811
2812
2813// Attempt to autoselect an appropriate value for std_enum_cur given
2814// whatever is currently in std_mask_cur
2815static void pvr2_hdw_internal_find_stdenum(struct pvr2_hdw *hdw)
2816{
2817 unsigned int idx;
2818 for (idx = 1; idx < hdw->std_enum_cnt; idx++) {
2819 if (hdw->std_defs[idx-1].id == hdw->std_mask_cur) {
2820 hdw->std_enum_cur = idx;
2821 return;
2822 }
2823 }
2824 hdw->std_enum_cur = 0;
2825}
2826
2827
2828// Calculate correct set of enumerated standards based on currently known
2829// set of available standards bits.
2830static void pvr2_hdw_internal_set_std_avail(struct pvr2_hdw *hdw)
2831{
2832 struct v4l2_standard *newstd;
2833 unsigned int std_cnt;
2834 unsigned int idx;
2835
2836 newstd = pvr2_std_create_enum(&std_cnt,hdw->std_mask_avail);
2837
2838 if (hdw->std_defs) {
2839 kfree(hdw->std_defs);
2840 hdw->std_defs = NULL;
2841 }
2842 hdw->std_enum_cnt = 0;
2843 if (hdw->std_enum_names) {
2844 kfree(hdw->std_enum_names);
2845 hdw->std_enum_names = NULL;
2846 }
2847
2848 if (!std_cnt) {
2849 pvr2_trace(
2850 PVR2_TRACE_ERROR_LEGS,
2851 "WARNING: Failed to identify any viable standards");
2852 }
2853
2854 /* Set up the dynamic control for this standard */
2855 hdw->std_enum_names = kmalloc(sizeof(char *)*(std_cnt+1),GFP_KERNEL);
2856 if (hdw->std_enum_names) {
2857 hdw->std_enum_names[0] = "none";
2858 for (idx = 0; idx < std_cnt; idx++)
2859 hdw->std_enum_names[idx+1] = newstd[idx].name;
2860 hdw->std_info_enum.def.type_enum.value_names =
2861 hdw->std_enum_names;
2862 hdw->std_info_enum.def.type_enum.count = std_cnt+1;
2863 } else {
2864 pvr2_trace(
2865 PVR2_TRACE_ERROR_LEGS,
2866 "WARNING: Failed to alloc memory for names");
2867 hdw->std_info_enum.def.type_enum.value_names = NULL;
2868 hdw->std_info_enum.def.type_enum.count = 0;
2869 }
2870 hdw->std_defs = newstd;
2871 hdw->std_enum_cnt = std_cnt+1;
2872 hdw->std_enum_cur = 0;
2873 hdw->std_info_cur.def.type_bitmask.valid_bits = hdw->std_mask_avail;
2874}
2875
2876
2877int pvr2_hdw_get_stdenum_value(struct pvr2_hdw *hdw,
2878 struct v4l2_standard *std,
2879 unsigned int idx)
2880{
2881 int ret = -EINVAL;
2882 if (!idx) return ret;
2883 LOCK_TAKE(hdw->big_lock); do {
2884 if (idx >= hdw->std_enum_cnt) break;
2885 idx--;
2886 memcpy(std,hdw->std_defs+idx,sizeof(*std));
2887 ret = 0;
2888 } while (0); LOCK_GIVE(hdw->big_lock);
2889 return ret;
2890}
2891
2892
2893/* Get the number of defined controls */
2894unsigned int pvr2_hdw_get_ctrl_count(struct pvr2_hdw *hdw)
2895{
2896 return hdw->control_cnt;
2897}
2898
2899
2900/* Retrieve a control handle given its index (0..count-1) */
2901struct pvr2_ctrl *pvr2_hdw_get_ctrl_by_index(struct pvr2_hdw *hdw,
2902 unsigned int idx)
2903{
2904 if (idx >= hdw->control_cnt) return NULL;
2905 return hdw->controls + idx;
2906}
2907
2908
2909/* Retrieve a control handle given its index (0..count-1) */
2910struct pvr2_ctrl *pvr2_hdw_get_ctrl_by_id(struct pvr2_hdw *hdw,
2911 unsigned int ctl_id)
2912{
2913 struct pvr2_ctrl *cptr;
2914 unsigned int idx;
2915 int i;
2916
2917 /* This could be made a lot more efficient, but for now... */
2918 for (idx = 0; idx < hdw->control_cnt; idx++) {
2919 cptr = hdw->controls + idx;
2920 i = cptr->info->internal_id;
2921 if (i && (i == ctl_id)) return cptr;
2922 }
2923 return NULL;
2924}
2925
2926
2927/* Given a V4L ID, retrieve the control structure associated with it. */
2928struct pvr2_ctrl *pvr2_hdw_get_ctrl_v4l(struct pvr2_hdw *hdw,unsigned int ctl_id)
2929{
2930 struct pvr2_ctrl *cptr;
2931 unsigned int idx;
2932 int i;
2933
2934 /* This could be made a lot more efficient, but for now... */
2935 for (idx = 0; idx < hdw->control_cnt; idx++) {
2936 cptr = hdw->controls + idx;
2937 i = cptr->info->v4l_id;
2938 if (i && (i == ctl_id)) return cptr;
2939 }
2940 return NULL;
2941}
2942
2943
2944/* Given a V4L ID for its immediate predecessor, retrieve the control
2945 structure associated with it. */
2946struct pvr2_ctrl *pvr2_hdw_get_ctrl_nextv4l(struct pvr2_hdw *hdw,
2947 unsigned int ctl_id)
2948{
2949 struct pvr2_ctrl *cptr,*cp2;
2950 unsigned int idx;
2951 int i;
2952
2953 /* This could be made a lot more efficient, but for now... */
2954 cp2 = NULL;
2955 for (idx = 0; idx < hdw->control_cnt; idx++) {
2956 cptr = hdw->controls + idx;
2957 i = cptr->info->v4l_id;
2958 if (!i) continue;
2959 if (i <= ctl_id) continue;
2960 if (cp2 && (cp2->info->v4l_id < i)) continue;
2961 cp2 = cptr;
2962 }
2963 return cp2;
2964 return NULL;
2965}
2966
2967
2968static const char *get_ctrl_typename(enum pvr2_ctl_type tp)
2969{
2970 switch (tp) {
2971 case pvr2_ctl_int: return "integer";
2972 case pvr2_ctl_enum: return "enum";
2973 case pvr2_ctl_bool: return "boolean";
2974 case pvr2_ctl_bitmask: return "bitmask";
2975 }
2976 return "";
2977}
2978
2979
2980static void pvr2_subdev_set_control(struct pvr2_hdw *hdw, int id,
2981 const char *name, int val)
2982{
2983 struct v4l2_control ctrl;
2984 pvr2_trace(PVR2_TRACE_CHIPS, "subdev v4l2 %s=%d", name, val);
2985 memset(&ctrl, 0, sizeof(ctrl));
2986 ctrl.id = id;
2987 ctrl.value = val;
2988 v4l2_device_call_all(&hdw->v4l2_dev, 0, core, s_ctrl, &ctrl);
2989}
2990
2991#define PVR2_SUBDEV_SET_CONTROL(hdw, id, lab) \
2992 if ((hdw)->lab##_dirty || (hdw)->force_dirty) { \
2993 pvr2_subdev_set_control(hdw, id, #lab, (hdw)->lab##_val); \
2994 }
2995
2996/* Execute whatever commands are required to update the state of all the
2997 sub-devices so that they match our current control values. */
2998static void pvr2_subdev_update(struct pvr2_hdw *hdw)
2999{
3000 struct v4l2_subdev *sd;
3001 unsigned int id;
3002 pvr2_subdev_update_func fp;
3003
3004 pvr2_trace(PVR2_TRACE_CHIPS, "subdev update...");
3005
3006 if (hdw->tuner_updated || hdw->force_dirty) {
3007 struct tuner_setup setup;
3008 pvr2_trace(PVR2_TRACE_CHIPS, "subdev tuner set_type(%d)",
3009 hdw->tuner_type);
3010 if (((int)(hdw->tuner_type)) >= 0) {
3011 memset(&setup, 0, sizeof(setup));
3012 setup.addr = ADDR_UNSET;
3013 setup.type = hdw->tuner_type;
3014 setup.mode_mask = T_RADIO | T_ANALOG_TV;
3015 v4l2_device_call_all(&hdw->v4l2_dev, 0,
3016 tuner, s_type_addr, &setup);
3017 }
3018 }
3019
3020 if (hdw->input_dirty || hdw->std_dirty || hdw->force_dirty) {
3021 pvr2_trace(PVR2_TRACE_CHIPS, "subdev v4l2 set_standard");
3022 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
3023 v4l2_device_call_all(&hdw->v4l2_dev, 0,
3024 tuner, s_radio);
3025 } else {
3026 v4l2_std_id vs;
3027 vs = hdw->std_mask_cur;
3028 v4l2_device_call_all(&hdw->v4l2_dev, 0,
3029 core, s_std, vs);
3030 pvr2_hdw_cx25840_vbi_hack(hdw);
3031 }
3032 hdw->tuner_signal_stale = !0;
3033 hdw->cropcap_stale = !0;
3034 }
3035
3036 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_BRIGHTNESS, brightness);
3037 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_CONTRAST, contrast);
3038 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_SATURATION, saturation);
3039 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_HUE, hue);
3040 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_AUDIO_MUTE, mute);
3041 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_AUDIO_VOLUME, volume);
3042 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_AUDIO_BALANCE, balance);
3043 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_AUDIO_BASS, bass);
3044 PVR2_SUBDEV_SET_CONTROL(hdw, V4L2_CID_AUDIO_TREBLE, treble);
3045
3046 if (hdw->input_dirty || hdw->audiomode_dirty || hdw->force_dirty) {
3047 struct v4l2_tuner vt;
3048 memset(&vt, 0, sizeof(vt));
3049 vt.type = (hdw->input_val == PVR2_CVAL_INPUT_RADIO) ?
3050 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
3051 vt.audmode = hdw->audiomode_val;
3052 v4l2_device_call_all(&hdw->v4l2_dev, 0, tuner, s_tuner, &vt);
3053 }
3054
3055 if (hdw->freqDirty || hdw->force_dirty) {
3056 unsigned long fv;
3057 struct v4l2_frequency freq;
3058 fv = pvr2_hdw_get_cur_freq(hdw);
3059 pvr2_trace(PVR2_TRACE_CHIPS, "subdev v4l2 set_freq(%lu)", fv);
3060 if (hdw->tuner_signal_stale) pvr2_hdw_status_poll(hdw);
3061 memset(&freq, 0, sizeof(freq));
3062 if (hdw->tuner_signal_info.capability & V4L2_TUNER_CAP_LOW) {
3063 /* ((fv * 1000) / 62500) */
3064 freq.frequency = (fv * 2) / 125;
3065 } else {
3066 freq.frequency = fv / 62500;
3067 }
3068 /* tuner-core currently doesn't seem to care about this, but
3069 let's set it anyway for completeness. */
3070 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
3071 freq.type = V4L2_TUNER_RADIO;
3072 } else {
3073 freq.type = V4L2_TUNER_ANALOG_TV;
3074 }
3075 freq.tuner = 0;
3076 v4l2_device_call_all(&hdw->v4l2_dev, 0, tuner,
3077 s_frequency, &freq);
3078 }
3079
3080 if (hdw->res_hor_dirty || hdw->res_ver_dirty || hdw->force_dirty) {
3081 struct v4l2_mbus_framefmt fmt;
3082 memset(&fmt, 0, sizeof(fmt));
3083 fmt.width = hdw->res_hor_val;
3084 fmt.height = hdw->res_ver_val;
3085 fmt.code = V4L2_MBUS_FMT_FIXED;
3086 pvr2_trace(PVR2_TRACE_CHIPS, "subdev v4l2 set_size(%dx%d)",
3087 fmt.width, fmt.height);
3088 v4l2_device_call_all(&hdw->v4l2_dev, 0, video, s_mbus_fmt, &fmt);
3089 }
3090
3091 if (hdw->srate_dirty || hdw->force_dirty) {
3092 u32 val;
3093 pvr2_trace(PVR2_TRACE_CHIPS, "subdev v4l2 set_audio %d",
3094 hdw->srate_val);
3095 switch (hdw->srate_val) {
3096 default:
3097 case V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000:
3098 val = 48000;
3099 break;
3100 case V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100:
3101 val = 44100;
3102 break;
3103 case V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000:
3104 val = 32000;
3105 break;
3106 }
3107 v4l2_device_call_all(&hdw->v4l2_dev, 0,
3108 audio, s_clock_freq, val);
3109 }
3110
3111 /* Unable to set crop parameters; there is apparently no equivalent
3112 for VIDIOC_S_CROP */
3113
3114 v4l2_device_for_each_subdev(sd, &hdw->v4l2_dev) {
3115 id = sd->grp_id;
3116 if (id >= ARRAY_SIZE(pvr2_module_update_functions)) continue;
3117 fp = pvr2_module_update_functions[id];
3118 if (!fp) continue;
3119 (*fp)(hdw, sd);
3120 }
3121
3122 if (hdw->tuner_signal_stale || hdw->cropcap_stale) {
3123 pvr2_hdw_status_poll(hdw);
3124 }
3125}
3126
3127
3128/* Figure out if we need to commit control changes. If so, mark internal
3129 state flags to indicate this fact and return true. Otherwise do nothing
3130 else and return false. */
3131static int pvr2_hdw_commit_setup(struct pvr2_hdw *hdw)
3132{
3133 unsigned int idx;
3134 struct pvr2_ctrl *cptr;
3135 int value;
3136 int commit_flag = hdw->force_dirty;
3137 char buf[100];
3138 unsigned int bcnt,ccnt;
3139
3140 for (idx = 0; idx < hdw->control_cnt; idx++) {
3141 cptr = hdw->controls + idx;
3142 if (!cptr->info->is_dirty) continue;
3143 if (!cptr->info->is_dirty(cptr)) continue;
3144 commit_flag = !0;
3145
3146 if (!(pvrusb2_debug & PVR2_TRACE_CTL)) continue;
3147 bcnt = scnprintf(buf,sizeof(buf),"\"%s\" <-- ",
3148 cptr->info->name);
3149 value = 0;
3150 cptr->info->get_value(cptr,&value);
3151 pvr2_ctrl_value_to_sym_internal(cptr,~0,value,
3152 buf+bcnt,
3153 sizeof(buf)-bcnt,&ccnt);
3154 bcnt += ccnt;
3155 bcnt += scnprintf(buf+bcnt,sizeof(buf)-bcnt," <%s>",
3156 get_ctrl_typename(cptr->info->type));
3157 pvr2_trace(PVR2_TRACE_CTL,
3158 "/*--TRACE_COMMIT--*/ %.*s",
3159 bcnt,buf);
3160 }
3161
3162 if (!commit_flag) {
3163 /* Nothing has changed */
3164 return 0;
3165 }
3166
3167 hdw->state_pipeline_config = 0;
3168 trace_stbit("state_pipeline_config",hdw->state_pipeline_config);
3169 pvr2_hdw_state_sched(hdw);
3170
3171 return !0;
3172}
3173
3174
3175/* Perform all operations needed to commit all control changes. This must
3176 be performed in synchronization with the pipeline state and is thus
3177 expected to be called as part of the driver's worker thread. Return
3178 true if commit successful, otherwise return false to indicate that
3179 commit isn't possible at this time. */
3180static int pvr2_hdw_commit_execute(struct pvr2_hdw *hdw)
3181{
3182 unsigned int idx;
3183 struct pvr2_ctrl *cptr;
3184 int disruptive_change;
3185
3186 if (hdw->input_dirty && hdw->state_pathway_ok &&
3187 (((hdw->input_val == PVR2_CVAL_INPUT_DTV) ?
3188 PVR2_PATHWAY_DIGITAL : PVR2_PATHWAY_ANALOG) !=
3189 hdw->pathway_state)) {
3190 /* Change of mode being asked for... */
3191 hdw->state_pathway_ok = 0;
3192 trace_stbit("state_pathway_ok", hdw->state_pathway_ok);
3193 }
3194 if (!hdw->state_pathway_ok) {
3195 /* Can't commit anything until pathway is ok. */
3196 return 0;
3197 }
3198
3199 /* Handle some required side effects when the video standard is
3200 changed.... */
3201 if (hdw->std_dirty) {
3202 int nvres;
3203 int gop_size;
3204 if (hdw->std_mask_cur & V4L2_STD_525_60) {
3205 nvres = 480;
3206 gop_size = 15;
3207 } else {
3208 nvres = 576;
3209 gop_size = 12;
3210 }
3211 /* Rewrite the vertical resolution to be appropriate to the
3212 video standard that has been selected. */
3213 if (nvres != hdw->res_ver_val) {
3214 hdw->res_ver_val = nvres;
3215 hdw->res_ver_dirty = !0;
3216 }
3217 /* Rewrite the GOP size to be appropriate to the video
3218 standard that has been selected. */
3219 if (gop_size != hdw->enc_ctl_state.video_gop_size) {
3220 struct v4l2_ext_controls cs;
3221 struct v4l2_ext_control c1;
3222 memset(&cs, 0, sizeof(cs));
3223 memset(&c1, 0, sizeof(c1));
3224 cs.controls = &c1;
3225 cs.count = 1;
3226 c1.id = V4L2_CID_MPEG_VIDEO_GOP_SIZE;
3227 c1.value = gop_size;
3228 cx2341x_ext_ctrls(&hdw->enc_ctl_state, 0, &cs,
3229 VIDIOC_S_EXT_CTRLS);
3230 }
3231 }
3232
3233 /* The broadcast decoder can only scale down, so if
3234 * res_*_dirty && crop window < output format ==> enlarge crop.
3235 *
3236 * The mpeg encoder receives fields of res_hor_val dots and
3237 * res_ver_val halflines. Limits: hor<=720, ver<=576.
3238 */
3239 if (hdw->res_hor_dirty && hdw->cropw_val < hdw->res_hor_val) {
3240 hdw->cropw_val = hdw->res_hor_val;
3241 hdw->cropw_dirty = !0;
3242 } else if (hdw->cropw_dirty) {
3243 hdw->res_hor_dirty = !0; /* must rescale */
3244 hdw->res_hor_val = min(720, hdw->cropw_val);
3245 }
3246 if (hdw->res_ver_dirty && hdw->croph_val < hdw->res_ver_val) {
3247 hdw->croph_val = hdw->res_ver_val;
3248 hdw->croph_dirty = !0;
3249 } else if (hdw->croph_dirty) {
3250 int nvres = hdw->std_mask_cur & V4L2_STD_525_60 ? 480 : 576;
3251 hdw->res_ver_dirty = !0;
3252 hdw->res_ver_val = min(nvres, hdw->croph_val);
3253 }
3254
3255 /* If any of the below has changed, then we can't do the update
3256 while the pipeline is running. Pipeline must be paused first
3257 and decoder -> encoder connection be made quiescent before we
3258 can proceed. */
3259 disruptive_change =
3260 (hdw->std_dirty ||
3261 hdw->enc_unsafe_stale ||
3262 hdw->srate_dirty ||
3263 hdw->res_ver_dirty ||
3264 hdw->res_hor_dirty ||
3265 hdw->cropw_dirty ||
3266 hdw->croph_dirty ||
3267 hdw->input_dirty ||
3268 (hdw->active_stream_type != hdw->desired_stream_type));
3269 if (disruptive_change && !hdw->state_pipeline_idle) {
3270 /* Pipeline is not idle; we can't proceed. Arrange to
3271 cause pipeline to stop so that we can try this again
3272 later.... */
3273 hdw->state_pipeline_pause = !0;
3274 return 0;
3275 }
3276
3277 if (hdw->srate_dirty) {
3278 /* Write new sample rate into control structure since
3279 * the master copy is stale. We must track srate
3280 * separate from the mpeg control structure because
3281 * other logic also uses this value. */
3282 struct v4l2_ext_controls cs;
3283 struct v4l2_ext_control c1;
3284 memset(&cs,0,sizeof(cs));
3285 memset(&c1,0,sizeof(c1));
3286 cs.controls = &c1;
3287 cs.count = 1;
3288 c1.id = V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ;
3289 c1.value = hdw->srate_val;
3290 cx2341x_ext_ctrls(&hdw->enc_ctl_state, 0, &cs,VIDIOC_S_EXT_CTRLS);
3291 }
3292
3293 if (hdw->active_stream_type != hdw->desired_stream_type) {
3294 /* Handle any side effects of stream config here */
3295 hdw->active_stream_type = hdw->desired_stream_type;
3296 }
3297
3298 if (hdw->hdw_desc->signal_routing_scheme ==
3299 PVR2_ROUTING_SCHEME_GOTVIEW) {
3300 u32 b;
3301 /* Handle GOTVIEW audio switching */
3302 pvr2_hdw_gpio_get_out(hdw,&b);
3303 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
3304 /* Set GPIO 11 */
3305 pvr2_hdw_gpio_chg_out(hdw,(1 << 11),~0);
3306 } else {
3307 /* Clear GPIO 11 */
3308 pvr2_hdw_gpio_chg_out(hdw,(1 << 11),0);
3309 }
3310 }
3311
3312 /* Check and update state for all sub-devices. */
3313 pvr2_subdev_update(hdw);
3314
3315 hdw->tuner_updated = 0;
3316 hdw->force_dirty = 0;
3317 for (idx = 0; idx < hdw->control_cnt; idx++) {
3318 cptr = hdw->controls + idx;
3319 if (!cptr->info->clear_dirty) continue;
3320 cptr->info->clear_dirty(cptr);
3321 }
3322
3323 if ((hdw->pathway_state == PVR2_PATHWAY_ANALOG) &&
3324 hdw->state_encoder_run) {
3325 /* If encoder isn't running or it can't be touched, then
3326 this will get worked out later when we start the
3327 encoder. */
3328 if (pvr2_encoder_adjust(hdw) < 0) return !0;
3329 }
3330
3331 hdw->state_pipeline_config = !0;
3332 /* Hardware state may have changed in a way to cause the cropping
3333 capabilities to have changed. So mark it stale, which will
3334 cause a later re-fetch. */
3335 trace_stbit("state_pipeline_config",hdw->state_pipeline_config);
3336 return !0;
3337}
3338
3339
3340int pvr2_hdw_commit_ctl(struct pvr2_hdw *hdw)
3341{
3342 int fl;
3343 LOCK_TAKE(hdw->big_lock);
3344 fl = pvr2_hdw_commit_setup(hdw);
3345 LOCK_GIVE(hdw->big_lock);
3346 if (!fl) return 0;
3347 return pvr2_hdw_wait(hdw,0);
3348}
3349
3350
3351static void pvr2_hdw_worker_poll(struct work_struct *work)
3352{
3353 int fl = 0;
3354 struct pvr2_hdw *hdw = container_of(work,struct pvr2_hdw,workpoll);
3355 LOCK_TAKE(hdw->big_lock); do {
3356 fl = pvr2_hdw_state_eval(hdw);
3357 } while (0); LOCK_GIVE(hdw->big_lock);
3358 if (fl && hdw->state_func) {
3359 hdw->state_func(hdw->state_data);
3360 }
3361}
3362
3363
3364static int pvr2_hdw_wait(struct pvr2_hdw *hdw,int state)
3365{
3366 return wait_event_interruptible(
3367 hdw->state_wait_data,
3368 (hdw->state_stale == 0) &&
3369 (!state || (hdw->master_state != state)));
3370}
3371
3372
3373/* Return name for this driver instance */
3374const char *pvr2_hdw_get_driver_name(struct pvr2_hdw *hdw)
3375{
3376 return hdw->name;
3377}
3378
3379
3380const char *pvr2_hdw_get_desc(struct pvr2_hdw *hdw)
3381{
3382 return hdw->hdw_desc->description;
3383}
3384
3385
3386const char *pvr2_hdw_get_type(struct pvr2_hdw *hdw)
3387{
3388 return hdw->hdw_desc->shortname;
3389}
3390
3391
3392int pvr2_hdw_is_hsm(struct pvr2_hdw *hdw)
3393{
3394 int result;
3395 LOCK_TAKE(hdw->ctl_lock); do {
3396 hdw->cmd_buffer[0] = FX2CMD_GET_USB_SPEED;
3397 result = pvr2_send_request(hdw,
3398 hdw->cmd_buffer,1,
3399 hdw->cmd_buffer,1);
3400 if (result < 0) break;
3401 result = (hdw->cmd_buffer[0] != 0);
3402 } while(0); LOCK_GIVE(hdw->ctl_lock);
3403 return result;
3404}
3405
3406
3407/* Execute poll of tuner status */
3408void pvr2_hdw_execute_tuner_poll(struct pvr2_hdw *hdw)
3409{
3410 LOCK_TAKE(hdw->big_lock); do {
3411 pvr2_hdw_status_poll(hdw);
3412 } while (0); LOCK_GIVE(hdw->big_lock);
3413}
3414
3415
3416static int pvr2_hdw_check_cropcap(struct pvr2_hdw *hdw)
3417{
3418 if (!hdw->cropcap_stale) {
3419 return 0;
3420 }
3421 pvr2_hdw_status_poll(hdw);
3422 if (hdw->cropcap_stale) {
3423 return -EIO;
3424 }
3425 return 0;
3426}
3427
3428
3429/* Return information about cropping capabilities */
3430int pvr2_hdw_get_cropcap(struct pvr2_hdw *hdw, struct v4l2_cropcap *pp)
3431{
3432 int stat = 0;
3433 LOCK_TAKE(hdw->big_lock);
3434 stat = pvr2_hdw_check_cropcap(hdw);
3435 if (!stat) {
3436 memcpy(pp, &hdw->cropcap_info, sizeof(hdw->cropcap_info));
3437 }
3438 LOCK_GIVE(hdw->big_lock);
3439 return stat;
3440}
3441
3442
3443/* Return information about the tuner */
3444int pvr2_hdw_get_tuner_status(struct pvr2_hdw *hdw,struct v4l2_tuner *vtp)
3445{
3446 LOCK_TAKE(hdw->big_lock); do {
3447 if (hdw->tuner_signal_stale) {
3448 pvr2_hdw_status_poll(hdw);
3449 }
3450 memcpy(vtp,&hdw->tuner_signal_info,sizeof(struct v4l2_tuner));
3451 } while (0); LOCK_GIVE(hdw->big_lock);
3452 return 0;
3453}
3454
3455
3456/* Get handle to video output stream */
3457struct pvr2_stream *pvr2_hdw_get_video_stream(struct pvr2_hdw *hp)
3458{
3459 return hp->vid_stream;
3460}
3461
3462
3463void pvr2_hdw_trigger_module_log(struct pvr2_hdw *hdw)
3464{
3465 int nr = pvr2_hdw_get_unit_number(hdw);
3466 LOCK_TAKE(hdw->big_lock); do {
3467 printk(KERN_INFO "pvrusb2: ================= START STATUS CARD #%d =================\n", nr);
3468 v4l2_device_call_all(&hdw->v4l2_dev, 0, core, log_status);
3469 pvr2_trace(PVR2_TRACE_INFO,"cx2341x config:");
3470 cx2341x_log_status(&hdw->enc_ctl_state, "pvrusb2");
3471 pvr2_hdw_state_log_state(hdw);
3472 printk(KERN_INFO "pvrusb2: ================== END STATUS CARD #%d ==================\n", nr);
3473 } while (0); LOCK_GIVE(hdw->big_lock);
3474}
3475
3476
3477/* Grab EEPROM contents, needed for direct method. */
3478#define EEPROM_SIZE 8192
3479#define trace_eeprom(...) pvr2_trace(PVR2_TRACE_EEPROM,__VA_ARGS__)
3480static u8 *pvr2_full_eeprom_fetch(struct pvr2_hdw *hdw)
3481{
3482 struct i2c_msg msg[2];
3483 u8 *eeprom;
3484 u8 iadd[2];
3485 u8 addr;
3486 u16 eepromSize;
3487 unsigned int offs;
3488 int ret;
3489 int mode16 = 0;
3490 unsigned pcnt,tcnt;
3491 eeprom = kmalloc(EEPROM_SIZE,GFP_KERNEL);
3492 if (!eeprom) {
3493 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3494 "Failed to allocate memory"
3495 " required to read eeprom");
3496 return NULL;
3497 }
3498
3499 trace_eeprom("Value for eeprom addr from controller was 0x%x",
3500 hdw->eeprom_addr);
3501 addr = hdw->eeprom_addr;
3502 /* Seems that if the high bit is set, then the *real* eeprom
3503 address is shifted right now bit position (noticed this in
3504 newer PVR USB2 hardware) */
3505 if (addr & 0x80) addr >>= 1;
3506
3507 /* FX2 documentation states that a 16bit-addressed eeprom is
3508 expected if the I2C address is an odd number (yeah, this is
3509 strange but it's what they do) */
3510 mode16 = (addr & 1);
3511 eepromSize = (mode16 ? EEPROM_SIZE : 256);
3512 trace_eeprom("Examining %d byte eeprom at location 0x%x"
3513 " using %d bit addressing",eepromSize,addr,
3514 mode16 ? 16 : 8);
3515
3516 msg[0].addr = addr;
3517 msg[0].flags = 0;
3518 msg[0].len = mode16 ? 2 : 1;
3519 msg[0].buf = iadd;
3520 msg[1].addr = addr;
3521 msg[1].flags = I2C_M_RD;
3522
3523 /* We have to do the actual eeprom data fetch ourselves, because
3524 (1) we're only fetching part of the eeprom, and (2) if we were
3525 getting the whole thing our I2C driver can't grab it in one
3526 pass - which is what tveeprom is otherwise going to attempt */
3527 memset(eeprom,0,EEPROM_SIZE);
3528 for (tcnt = 0; tcnt < EEPROM_SIZE; tcnt += pcnt) {
3529 pcnt = 16;
3530 if (pcnt + tcnt > EEPROM_SIZE) pcnt = EEPROM_SIZE-tcnt;
3531 offs = tcnt + (eepromSize - EEPROM_SIZE);
3532 if (mode16) {
3533 iadd[0] = offs >> 8;
3534 iadd[1] = offs;
3535 } else {
3536 iadd[0] = offs;
3537 }
3538 msg[1].len = pcnt;
3539 msg[1].buf = eeprom+tcnt;
3540 if ((ret = i2c_transfer(&hdw->i2c_adap,
3541 msg,ARRAY_SIZE(msg))) != 2) {
3542 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3543 "eeprom fetch set offs err=%d",ret);
3544 kfree(eeprom);
3545 return NULL;
3546 }
3547 }
3548 return eeprom;
3549}
3550
3551
3552void pvr2_hdw_cpufw_set_enabled(struct pvr2_hdw *hdw,
3553 int mode,
3554 int enable_flag)
3555{
3556 int ret;
3557 u16 address;
3558 unsigned int pipe;
3559 LOCK_TAKE(hdw->big_lock); do {
3560 if ((hdw->fw_buffer == NULL) == !enable_flag) break;
3561
3562 if (!enable_flag) {
3563 pvr2_trace(PVR2_TRACE_FIRMWARE,
3564 "Cleaning up after CPU firmware fetch");
3565 kfree(hdw->fw_buffer);
3566 hdw->fw_buffer = NULL;
3567 hdw->fw_size = 0;
3568 if (hdw->fw_cpu_flag) {
3569 /* Now release the CPU. It will disconnect
3570 and reconnect later. */
3571 pvr2_hdw_cpureset_assert(hdw,0);
3572 }
3573 break;
3574 }
3575
3576 hdw->fw_cpu_flag = (mode != 2);
3577 if (hdw->fw_cpu_flag) {
3578 hdw->fw_size = (mode == 1) ? 0x4000 : 0x2000;
3579 pvr2_trace(PVR2_TRACE_FIRMWARE,
3580 "Preparing to suck out CPU firmware"
3581 " (size=%u)", hdw->fw_size);
3582 hdw->fw_buffer = kzalloc(hdw->fw_size,GFP_KERNEL);
3583 if (!hdw->fw_buffer) {
3584 hdw->fw_size = 0;
3585 break;
3586 }
3587
3588 /* We have to hold the CPU during firmware upload. */
3589 pvr2_hdw_cpureset_assert(hdw,1);
3590
3591 /* download the firmware from address 0000-1fff in 2048
3592 (=0x800) bytes chunk. */
3593
3594 pvr2_trace(PVR2_TRACE_FIRMWARE,
3595 "Grabbing CPU firmware");
3596 pipe = usb_rcvctrlpipe(hdw->usb_dev, 0);
3597 for(address = 0; address < hdw->fw_size;
3598 address += 0x800) {
3599 ret = usb_control_msg(hdw->usb_dev,pipe,
3600 0xa0,0xc0,
3601 address,0,
3602 hdw->fw_buffer+address,
3603 0x800,HZ);
3604 if (ret < 0) break;
3605 }
3606
3607 pvr2_trace(PVR2_TRACE_FIRMWARE,
3608 "Done grabbing CPU firmware");
3609 } else {
3610 pvr2_trace(PVR2_TRACE_FIRMWARE,
3611 "Sucking down EEPROM contents");
3612 hdw->fw_buffer = pvr2_full_eeprom_fetch(hdw);
3613 if (!hdw->fw_buffer) {
3614 pvr2_trace(PVR2_TRACE_FIRMWARE,
3615 "EEPROM content suck failed.");
3616 break;
3617 }
3618 hdw->fw_size = EEPROM_SIZE;
3619 pvr2_trace(PVR2_TRACE_FIRMWARE,
3620 "Done sucking down EEPROM contents");
3621 }
3622
3623 } while (0); LOCK_GIVE(hdw->big_lock);
3624}
3625
3626
3627/* Return true if we're in a mode for retrieval CPU firmware */
3628int pvr2_hdw_cpufw_get_enabled(struct pvr2_hdw *hdw)
3629{
3630 return hdw->fw_buffer != NULL;
3631}
3632
3633
3634int pvr2_hdw_cpufw_get(struct pvr2_hdw *hdw,unsigned int offs,
3635 char *buf,unsigned int cnt)
3636{
3637 int ret = -EINVAL;
3638 LOCK_TAKE(hdw->big_lock); do {
3639 if (!buf) break;
3640 if (!cnt) break;
3641
3642 if (!hdw->fw_buffer) {
3643 ret = -EIO;
3644 break;
3645 }
3646
3647 if (offs >= hdw->fw_size) {
3648 pvr2_trace(PVR2_TRACE_FIRMWARE,
3649 "Read firmware data offs=%d EOF",
3650 offs);
3651 ret = 0;
3652 break;
3653 }
3654
3655 if (offs + cnt > hdw->fw_size) cnt = hdw->fw_size - offs;
3656
3657 memcpy(buf,hdw->fw_buffer+offs,cnt);
3658
3659 pvr2_trace(PVR2_TRACE_FIRMWARE,
3660 "Read firmware data offs=%d cnt=%d",
3661 offs,cnt);
3662 ret = cnt;
3663 } while (0); LOCK_GIVE(hdw->big_lock);
3664
3665 return ret;
3666}
3667
3668
3669int pvr2_hdw_v4l_get_minor_number(struct pvr2_hdw *hdw,
3670 enum pvr2_v4l_type index)
3671{
3672 switch (index) {
3673 case pvr2_v4l_type_video: return hdw->v4l_minor_number_video;
3674 case pvr2_v4l_type_vbi: return hdw->v4l_minor_number_vbi;
3675 case pvr2_v4l_type_radio: return hdw->v4l_minor_number_radio;
3676 default: return -1;
3677 }
3678}
3679
3680
3681/* Store a v4l minor device number */
3682void pvr2_hdw_v4l_store_minor_number(struct pvr2_hdw *hdw,
3683 enum pvr2_v4l_type index,int v)
3684{
3685 switch (index) {
3686 case pvr2_v4l_type_video: hdw->v4l_minor_number_video = v;
3687 case pvr2_v4l_type_vbi: hdw->v4l_minor_number_vbi = v;
3688 case pvr2_v4l_type_radio: hdw->v4l_minor_number_radio = v;
3689 default: break;
3690 }
3691}
3692
3693
3694static void pvr2_ctl_write_complete(struct urb *urb)
3695{
3696 struct pvr2_hdw *hdw = urb->context;
3697 hdw->ctl_write_pend_flag = 0;
3698 if (hdw->ctl_read_pend_flag) return;
3699 complete(&hdw->ctl_done);
3700}
3701
3702
3703static void pvr2_ctl_read_complete(struct urb *urb)
3704{
3705 struct pvr2_hdw *hdw = urb->context;
3706 hdw->ctl_read_pend_flag = 0;
3707 if (hdw->ctl_write_pend_flag) return;
3708 complete(&hdw->ctl_done);
3709}
3710
3711
3712static void pvr2_ctl_timeout(unsigned long data)
3713{
3714 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
3715 if (hdw->ctl_write_pend_flag || hdw->ctl_read_pend_flag) {
3716 hdw->ctl_timeout_flag = !0;
3717 if (hdw->ctl_write_pend_flag)
3718 usb_unlink_urb(hdw->ctl_write_urb);
3719 if (hdw->ctl_read_pend_flag)
3720 usb_unlink_urb(hdw->ctl_read_urb);
3721 }
3722}
3723
3724
3725/* Issue a command and get a response from the device. This extended
3726 version includes a probe flag (which if set means that device errors
3727 should not be logged or treated as fatal) and a timeout in jiffies.
3728 This can be used to non-lethally probe the health of endpoint 1. */
3729static int pvr2_send_request_ex(struct pvr2_hdw *hdw,
3730 unsigned int timeout,int probe_fl,
3731 void *write_data,unsigned int write_len,
3732 void *read_data,unsigned int read_len)
3733{
3734 unsigned int idx;
3735 int status = 0;
3736 struct timer_list timer;
3737 if (!hdw->ctl_lock_held) {
3738 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3739 "Attempted to execute control transfer"
3740 " without lock!!");
3741 return -EDEADLK;
3742 }
3743 if (!hdw->flag_ok && !probe_fl) {
3744 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3745 "Attempted to execute control transfer"
3746 " when device not ok");
3747 return -EIO;
3748 }
3749 if (!(hdw->ctl_read_urb && hdw->ctl_write_urb)) {
3750 if (!probe_fl) {
3751 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3752 "Attempted to execute control transfer"
3753 " when USB is disconnected");
3754 }
3755 return -ENOTTY;
3756 }
3757
3758 /* Ensure that we have sane parameters */
3759 if (!write_data) write_len = 0;
3760 if (!read_data) read_len = 0;
3761 if (write_len > PVR2_CTL_BUFFSIZE) {
3762 pvr2_trace(
3763 PVR2_TRACE_ERROR_LEGS,
3764 "Attempted to execute %d byte"
3765 " control-write transfer (limit=%d)",
3766 write_len,PVR2_CTL_BUFFSIZE);
3767 return -EINVAL;
3768 }
3769 if (read_len > PVR2_CTL_BUFFSIZE) {
3770 pvr2_trace(
3771 PVR2_TRACE_ERROR_LEGS,
3772 "Attempted to execute %d byte"
3773 " control-read transfer (limit=%d)",
3774 write_len,PVR2_CTL_BUFFSIZE);
3775 return -EINVAL;
3776 }
3777 if ((!write_len) && (!read_len)) {
3778 pvr2_trace(
3779 PVR2_TRACE_ERROR_LEGS,
3780 "Attempted to execute null control transfer?");
3781 return -EINVAL;
3782 }
3783
3784
3785 hdw->cmd_debug_state = 1;
3786 if (write_len) {
3787 hdw->cmd_debug_code = ((unsigned char *)write_data)[0];
3788 } else {
3789 hdw->cmd_debug_code = 0;
3790 }
3791 hdw->cmd_debug_write_len = write_len;
3792 hdw->cmd_debug_read_len = read_len;
3793
3794 /* Initialize common stuff */
3795 init_completion(&hdw->ctl_done);
3796 hdw->ctl_timeout_flag = 0;
3797 hdw->ctl_write_pend_flag = 0;
3798 hdw->ctl_read_pend_flag = 0;
3799 init_timer(&timer);
3800 timer.expires = jiffies + timeout;
3801 timer.data = (unsigned long)hdw;
3802 timer.function = pvr2_ctl_timeout;
3803
3804 if (write_len) {
3805 hdw->cmd_debug_state = 2;
3806 /* Transfer write data to internal buffer */
3807 for (idx = 0; idx < write_len; idx++) {
3808 hdw->ctl_write_buffer[idx] =
3809 ((unsigned char *)write_data)[idx];
3810 }
3811 /* Initiate a write request */
3812 usb_fill_bulk_urb(hdw->ctl_write_urb,
3813 hdw->usb_dev,
3814 usb_sndbulkpipe(hdw->usb_dev,
3815 PVR2_CTL_WRITE_ENDPOINT),
3816 hdw->ctl_write_buffer,
3817 write_len,
3818 pvr2_ctl_write_complete,
3819 hdw);
3820 hdw->ctl_write_urb->actual_length = 0;
3821 hdw->ctl_write_pend_flag = !0;
3822 status = usb_submit_urb(hdw->ctl_write_urb,GFP_KERNEL);
3823 if (status < 0) {
3824 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3825 "Failed to submit write-control"
3826 " URB status=%d",status);
3827 hdw->ctl_write_pend_flag = 0;
3828 goto done;
3829 }
3830 }
3831
3832 if (read_len) {
3833 hdw->cmd_debug_state = 3;
3834 memset(hdw->ctl_read_buffer,0x43,read_len);
3835 /* Initiate a read request */
3836 usb_fill_bulk_urb(hdw->ctl_read_urb,
3837 hdw->usb_dev,
3838 usb_rcvbulkpipe(hdw->usb_dev,
3839 PVR2_CTL_READ_ENDPOINT),
3840 hdw->ctl_read_buffer,
3841 read_len,
3842 pvr2_ctl_read_complete,
3843 hdw);
3844 hdw->ctl_read_urb->actual_length = 0;
3845 hdw->ctl_read_pend_flag = !0;
3846 status = usb_submit_urb(hdw->ctl_read_urb,GFP_KERNEL);
3847 if (status < 0) {
3848 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3849 "Failed to submit read-control"
3850 " URB status=%d",status);
3851 hdw->ctl_read_pend_flag = 0;
3852 goto done;
3853 }
3854 }
3855
3856 /* Start timer */
3857 add_timer(&timer);
3858
3859 /* Now wait for all I/O to complete */
3860 hdw->cmd_debug_state = 4;
3861 while (hdw->ctl_write_pend_flag || hdw->ctl_read_pend_flag) {
3862 wait_for_completion(&hdw->ctl_done);
3863 }
3864 hdw->cmd_debug_state = 5;
3865
3866 /* Stop timer */
3867 del_timer_sync(&timer);
3868
3869 hdw->cmd_debug_state = 6;
3870 status = 0;
3871
3872 if (hdw->ctl_timeout_flag) {
3873 status = -ETIMEDOUT;
3874 if (!probe_fl) {
3875 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3876 "Timed out control-write");
3877 }
3878 goto done;
3879 }
3880
3881 if (write_len) {
3882 /* Validate results of write request */
3883 if ((hdw->ctl_write_urb->status != 0) &&
3884 (hdw->ctl_write_urb->status != -ENOENT) &&
3885 (hdw->ctl_write_urb->status != -ESHUTDOWN) &&
3886 (hdw->ctl_write_urb->status != -ECONNRESET)) {
3887 /* USB subsystem is reporting some kind of failure
3888 on the write */
3889 status = hdw->ctl_write_urb->status;
3890 if (!probe_fl) {
3891 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3892 "control-write URB failure,"
3893 " status=%d",
3894 status);
3895 }
3896 goto done;
3897 }
3898 if (hdw->ctl_write_urb->actual_length < write_len) {
3899 /* Failed to write enough data */
3900 status = -EIO;
3901 if (!probe_fl) {
3902 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3903 "control-write URB short,"
3904 " expected=%d got=%d",
3905 write_len,
3906 hdw->ctl_write_urb->actual_length);
3907 }
3908 goto done;
3909 }
3910 }
3911 if (read_len) {
3912 /* Validate results of read request */
3913 if ((hdw->ctl_read_urb->status != 0) &&
3914 (hdw->ctl_read_urb->status != -ENOENT) &&
3915 (hdw->ctl_read_urb->status != -ESHUTDOWN) &&
3916 (hdw->ctl_read_urb->status != -ECONNRESET)) {
3917 /* USB subsystem is reporting some kind of failure
3918 on the read */
3919 status = hdw->ctl_read_urb->status;
3920 if (!probe_fl) {
3921 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3922 "control-read URB failure,"
3923 " status=%d",
3924 status);
3925 }
3926 goto done;
3927 }
3928 if (hdw->ctl_read_urb->actual_length < read_len) {
3929 /* Failed to read enough data */
3930 status = -EIO;
3931 if (!probe_fl) {
3932 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3933 "control-read URB short,"
3934 " expected=%d got=%d",
3935 read_len,
3936 hdw->ctl_read_urb->actual_length);
3937 }
3938 goto done;
3939 }
3940 /* Transfer retrieved data out from internal buffer */
3941 for (idx = 0; idx < read_len; idx++) {
3942 ((unsigned char *)read_data)[idx] =
3943 hdw->ctl_read_buffer[idx];
3944 }
3945 }
3946
3947 done:
3948
3949 hdw->cmd_debug_state = 0;
3950 if ((status < 0) && (!probe_fl)) {
3951 pvr2_hdw_render_useless(hdw);
3952 }
3953 return status;
3954}
3955
3956
3957int pvr2_send_request(struct pvr2_hdw *hdw,
3958 void *write_data,unsigned int write_len,
3959 void *read_data,unsigned int read_len)
3960{
3961 return pvr2_send_request_ex(hdw,HZ*4,0,
3962 write_data,write_len,
3963 read_data,read_len);
3964}
3965
3966
3967static int pvr2_issue_simple_cmd(struct pvr2_hdw *hdw,u32 cmdcode)
3968{
3969 int ret;
3970 unsigned int cnt = 1;
3971 unsigned int args = 0;
3972 LOCK_TAKE(hdw->ctl_lock);
3973 hdw->cmd_buffer[0] = cmdcode & 0xffu;
3974 args = (cmdcode >> 8) & 0xffu;
3975 args = (args > 2) ? 2 : args;
3976 if (args) {
3977 cnt += args;
3978 hdw->cmd_buffer[1] = (cmdcode >> 16) & 0xffu;
3979 if (args > 1) {
3980 hdw->cmd_buffer[2] = (cmdcode >> 24) & 0xffu;
3981 }
3982 }
3983 if (pvrusb2_debug & PVR2_TRACE_INIT) {
3984 unsigned int idx;
3985 unsigned int ccnt,bcnt;
3986 char tbuf[50];
3987 cmdcode &= 0xffu;
3988 bcnt = 0;
3989 ccnt = scnprintf(tbuf+bcnt,
3990 sizeof(tbuf)-bcnt,
3991 "Sending FX2 command 0x%x",cmdcode);
3992 bcnt += ccnt;
3993 for (idx = 0; idx < ARRAY_SIZE(pvr2_fx2cmd_desc); idx++) {
3994 if (pvr2_fx2cmd_desc[idx].id == cmdcode) {
3995 ccnt = scnprintf(tbuf+bcnt,
3996 sizeof(tbuf)-bcnt,
3997 " \"%s\"",
3998 pvr2_fx2cmd_desc[idx].desc);
3999 bcnt += ccnt;
4000 break;
4001 }
4002 }
4003 if (args) {
4004 ccnt = scnprintf(tbuf+bcnt,
4005 sizeof(tbuf)-bcnt,
4006 " (%u",hdw->cmd_buffer[1]);
4007 bcnt += ccnt;
4008 if (args > 1) {
4009 ccnt = scnprintf(tbuf+bcnt,
4010 sizeof(tbuf)-bcnt,
4011 ",%u",hdw->cmd_buffer[2]);
4012 bcnt += ccnt;
4013 }
4014 ccnt = scnprintf(tbuf+bcnt,
4015 sizeof(tbuf)-bcnt,
4016 ")");
4017 bcnt += ccnt;
4018 }
4019 pvr2_trace(PVR2_TRACE_INIT,"%.*s",bcnt,tbuf);
4020 }
4021 ret = pvr2_send_request(hdw,hdw->cmd_buffer,cnt,NULL,0);
4022 LOCK_GIVE(hdw->ctl_lock);
4023 return ret;
4024}
4025
4026
4027int pvr2_write_register(struct pvr2_hdw *hdw, u16 reg, u32 data)
4028{
4029 int ret;
4030
4031 LOCK_TAKE(hdw->ctl_lock);
4032
4033 hdw->cmd_buffer[0] = FX2CMD_REG_WRITE; /* write register prefix */
4034 PVR2_DECOMPOSE_LE(hdw->cmd_buffer,1,data);
4035 hdw->cmd_buffer[5] = 0;
4036 hdw->cmd_buffer[6] = (reg >> 8) & 0xff;
4037 hdw->cmd_buffer[7] = reg & 0xff;
4038
4039
4040 ret = pvr2_send_request(hdw, hdw->cmd_buffer, 8, hdw->cmd_buffer, 0);
4041
4042 LOCK_GIVE(hdw->ctl_lock);
4043
4044 return ret;
4045}
4046
4047
4048static int pvr2_read_register(struct pvr2_hdw *hdw, u16 reg, u32 *data)
4049{
4050 int ret = 0;
4051
4052 LOCK_TAKE(hdw->ctl_lock);
4053
4054 hdw->cmd_buffer[0] = FX2CMD_REG_READ; /* read register prefix */
4055 hdw->cmd_buffer[1] = 0;
4056 hdw->cmd_buffer[2] = 0;
4057 hdw->cmd_buffer[3] = 0;
4058 hdw->cmd_buffer[4] = 0;
4059 hdw->cmd_buffer[5] = 0;
4060 hdw->cmd_buffer[6] = (reg >> 8) & 0xff;
4061 hdw->cmd_buffer[7] = reg & 0xff;
4062
4063 ret |= pvr2_send_request(hdw, hdw->cmd_buffer, 8, hdw->cmd_buffer, 4);
4064 *data = PVR2_COMPOSE_LE(hdw->cmd_buffer,0);
4065
4066 LOCK_GIVE(hdw->ctl_lock);
4067
4068 return ret;
4069}
4070
4071
4072void pvr2_hdw_render_useless(struct pvr2_hdw *hdw)
4073{
4074 if (!hdw->flag_ok) return;
4075 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
4076 "Device being rendered inoperable");
4077 if (hdw->vid_stream) {
4078 pvr2_stream_setup(hdw->vid_stream,NULL,0,0);
4079 }
4080 hdw->flag_ok = 0;
4081 trace_stbit("flag_ok",hdw->flag_ok);
4082 pvr2_hdw_state_sched(hdw);
4083}
4084
4085
4086void pvr2_hdw_device_reset(struct pvr2_hdw *hdw)
4087{
4088 int ret;
4089 pvr2_trace(PVR2_TRACE_INIT,"Performing a device reset...");
4090 ret = usb_lock_device_for_reset(hdw->usb_dev,NULL);
4091 if (ret == 0) {
4092 ret = usb_reset_device(hdw->usb_dev);
4093 usb_unlock_device(hdw->usb_dev);
4094 } else {
4095 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
4096 "Failed to lock USB device ret=%d",ret);
4097 }
4098 if (init_pause_msec) {
4099 pvr2_trace(PVR2_TRACE_INFO,
4100 "Waiting %u msec for hardware to settle",
4101 init_pause_msec);
4102 msleep(init_pause_msec);
4103 }
4104
4105}
4106
4107
4108void pvr2_hdw_cpureset_assert(struct pvr2_hdw *hdw,int val)
4109{
4110 char *da;
4111 unsigned int pipe;
4112 int ret;
4113
4114 if (!hdw->usb_dev) return;
4115
4116 da = kmalloc(16, GFP_KERNEL);
4117
4118 if (da == NULL) {
4119 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
4120 "Unable to allocate memory to control CPU reset");
4121 return;
4122 }
4123
4124 pvr2_trace(PVR2_TRACE_INIT,"cpureset_assert(%d)",val);
4125
4126 da[0] = val ? 0x01 : 0x00;
4127
4128 /* Write the CPUCS register on the 8051. The lsb of the register
4129 is the reset bit; a 1 asserts reset while a 0 clears it. */
4130 pipe = usb_sndctrlpipe(hdw->usb_dev, 0);
4131 ret = usb_control_msg(hdw->usb_dev,pipe,0xa0,0x40,0xe600,0,da,1,HZ);
4132 if (ret < 0) {
4133 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
4134 "cpureset_assert(%d) error=%d",val,ret);
4135 pvr2_hdw_render_useless(hdw);
4136 }
4137
4138 kfree(da);
4139}
4140
4141
4142int pvr2_hdw_cmd_deep_reset(struct pvr2_hdw *hdw)
4143{
4144 return pvr2_issue_simple_cmd(hdw,FX2CMD_DEEP_RESET);
4145}
4146
4147
4148int pvr2_hdw_cmd_powerup(struct pvr2_hdw *hdw)
4149{
4150 return pvr2_issue_simple_cmd(hdw,FX2CMD_POWER_ON);
4151}
4152
4153
4154int pvr2_hdw_cmd_powerdown(struct pvr2_hdw *hdw)
4155{
4156 return pvr2_issue_simple_cmd(hdw,FX2CMD_POWER_OFF);
4157}
4158
4159
4160int pvr2_hdw_cmd_decoder_reset(struct pvr2_hdw *hdw)
4161{
4162 pvr2_trace(PVR2_TRACE_INIT,
4163 "Requesting decoder reset");
4164 if (hdw->decoder_client_id) {
4165 v4l2_device_call_all(&hdw->v4l2_dev, hdw->decoder_client_id,
4166 core, reset, 0);
4167 pvr2_hdw_cx25840_vbi_hack(hdw);
4168 return 0;
4169 }
4170 pvr2_trace(PVR2_TRACE_INIT,
4171 "Unable to reset decoder: nothing attached");
4172 return -ENOTTY;
4173}
4174
4175
4176static int pvr2_hdw_cmd_hcw_demod_reset(struct pvr2_hdw *hdw, int onoff)
4177{
4178 hdw->flag_ok = !0;
4179 return pvr2_issue_simple_cmd(hdw,
4180 FX2CMD_HCW_DEMOD_RESETIN |
4181 (1 << 8) |
4182 ((onoff ? 1 : 0) << 16));
4183}
4184
4185
4186static int pvr2_hdw_cmd_onair_fe_power_ctrl(struct pvr2_hdw *hdw, int onoff)
4187{
4188 hdw->flag_ok = !0;
4189 return pvr2_issue_simple_cmd(hdw,(onoff ?
4190 FX2CMD_ONAIR_DTV_POWER_ON :
4191 FX2CMD_ONAIR_DTV_POWER_OFF));
4192}
4193
4194
4195static int pvr2_hdw_cmd_onair_digital_path_ctrl(struct pvr2_hdw *hdw,
4196 int onoff)
4197{
4198 return pvr2_issue_simple_cmd(hdw,(onoff ?
4199 FX2CMD_ONAIR_DTV_STREAMING_ON :
4200 FX2CMD_ONAIR_DTV_STREAMING_OFF));
4201}
4202
4203
4204static void pvr2_hdw_cmd_modeswitch(struct pvr2_hdw *hdw,int digitalFl)
4205{
4206 int cmode;
4207 /* Compare digital/analog desired setting with current setting. If
4208 they don't match, fix it... */
4209 cmode = (digitalFl ? PVR2_PATHWAY_DIGITAL : PVR2_PATHWAY_ANALOG);
4210 if (cmode == hdw->pathway_state) {
4211 /* They match; nothing to do */
4212 return;
4213 }
4214
4215 switch (hdw->hdw_desc->digital_control_scheme) {
4216 case PVR2_DIGITAL_SCHEME_HAUPPAUGE:
4217 pvr2_hdw_cmd_hcw_demod_reset(hdw,digitalFl);
4218 if (cmode == PVR2_PATHWAY_ANALOG) {
4219 /* If moving to analog mode, also force the decoder
4220 to reset. If no decoder is attached, then it's
4221 ok to ignore this because if/when the decoder
4222 attaches, it will reset itself at that time. */
4223 pvr2_hdw_cmd_decoder_reset(hdw);
4224 }
4225 break;
4226 case PVR2_DIGITAL_SCHEME_ONAIR:
4227 /* Supposedly we should always have the power on whether in
4228 digital or analog mode. But for now do what appears to
4229 work... */
4230 pvr2_hdw_cmd_onair_fe_power_ctrl(hdw,digitalFl);
4231 break;
4232 default: break;
4233 }
4234
4235 pvr2_hdw_untrip_unlocked(hdw);
4236 hdw->pathway_state = cmode;
4237}
4238
4239
4240static void pvr2_led_ctrl_hauppauge(struct pvr2_hdw *hdw, int onoff)
4241{
4242 /* change some GPIO data
4243 *
4244 * note: bit d7 of dir appears to control the LED,
4245 * so we shut it off here.
4246 *
4247 */
4248 if (onoff) {
4249 pvr2_hdw_gpio_chg_dir(hdw, 0xffffffff, 0x00000481);
4250 } else {
4251 pvr2_hdw_gpio_chg_dir(hdw, 0xffffffff, 0x00000401);
4252 }
4253 pvr2_hdw_gpio_chg_out(hdw, 0xffffffff, 0x00000000);
4254}
4255
4256
4257typedef void (*led_method_func)(struct pvr2_hdw *,int);
4258
4259static led_method_func led_methods[] = {
4260 [PVR2_LED_SCHEME_HAUPPAUGE] = pvr2_led_ctrl_hauppauge,
4261};
4262
4263
4264/* Toggle LED */
4265static void pvr2_led_ctrl(struct pvr2_hdw *hdw,int onoff)
4266{
4267 unsigned int scheme_id;
4268 led_method_func fp;
4269
4270 if ((!onoff) == (!hdw->led_on)) return;
4271
4272 hdw->led_on = onoff != 0;
4273
4274 scheme_id = hdw->hdw_desc->led_scheme;
4275 if (scheme_id < ARRAY_SIZE(led_methods)) {
4276 fp = led_methods[scheme_id];
4277 } else {
4278 fp = NULL;
4279 }
4280
4281 if (fp) (*fp)(hdw,onoff);
4282}
4283
4284
4285/* Stop / start video stream transport */
4286static int pvr2_hdw_cmd_usbstream(struct pvr2_hdw *hdw,int runFl)
4287{
4288 int ret;
4289
4290 /* If we're in analog mode, then just issue the usual analog
4291 command. */
4292 if (hdw->pathway_state == PVR2_PATHWAY_ANALOG) {
4293 return pvr2_issue_simple_cmd(hdw,
4294 (runFl ?
4295 FX2CMD_STREAMING_ON :
4296 FX2CMD_STREAMING_OFF));
4297 /*Note: Not reached */
4298 }
4299
4300 if (hdw->pathway_state != PVR2_PATHWAY_DIGITAL) {
4301 /* Whoops, we don't know what mode we're in... */
4302 return -EINVAL;
4303 }
4304
4305 /* To get here we have to be in digital mode. The mechanism here
4306 is unfortunately different for different vendors. So we switch
4307 on the device's digital scheme attribute in order to figure out
4308 what to do. */
4309 switch (hdw->hdw_desc->digital_control_scheme) {
4310 case PVR2_DIGITAL_SCHEME_HAUPPAUGE:
4311 return pvr2_issue_simple_cmd(hdw,
4312 (runFl ?
4313 FX2CMD_HCW_DTV_STREAMING_ON :
4314 FX2CMD_HCW_DTV_STREAMING_OFF));
4315 case PVR2_DIGITAL_SCHEME_ONAIR:
4316 ret = pvr2_issue_simple_cmd(hdw,
4317 (runFl ?
4318 FX2CMD_STREAMING_ON :
4319 FX2CMD_STREAMING_OFF));
4320 if (ret) return ret;
4321 return pvr2_hdw_cmd_onair_digital_path_ctrl(hdw,runFl);
4322 default:
4323 return -EINVAL;
4324 }
4325}
4326
4327
4328/* Evaluate whether or not state_pathway_ok can change */
4329static int state_eval_pathway_ok(struct pvr2_hdw *hdw)
4330{
4331 if (hdw->state_pathway_ok) {
4332 /* Nothing to do if pathway is already ok */
4333 return 0;
4334 }
4335 if (!hdw->state_pipeline_idle) {
4336 /* Not allowed to change anything if pipeline is not idle */
4337 return 0;
4338 }
4339 pvr2_hdw_cmd_modeswitch(hdw,hdw->input_val == PVR2_CVAL_INPUT_DTV);
4340 hdw->state_pathway_ok = !0;
4341 trace_stbit("state_pathway_ok",hdw->state_pathway_ok);
4342 return !0;
4343}
4344
4345
4346/* Evaluate whether or not state_encoder_ok can change */
4347static int state_eval_encoder_ok(struct pvr2_hdw *hdw)
4348{
4349 if (hdw->state_encoder_ok) return 0;
4350 if (hdw->flag_tripped) return 0;
4351 if (hdw->state_encoder_run) return 0;
4352 if (hdw->state_encoder_config) return 0;
4353 if (hdw->state_decoder_run) return 0;
4354 if (hdw->state_usbstream_run) return 0;
4355 if (hdw->pathway_state == PVR2_PATHWAY_DIGITAL) {
4356 if (!hdw->hdw_desc->flag_digital_requires_cx23416) return 0;
4357 } else if (hdw->pathway_state != PVR2_PATHWAY_ANALOG) {
4358 return 0;
4359 }
4360
4361 if (pvr2_upload_firmware2(hdw) < 0) {
4362 hdw->flag_tripped = !0;
4363 trace_stbit("flag_tripped",hdw->flag_tripped);
4364 return !0;
4365 }
4366 hdw->state_encoder_ok = !0;
4367 trace_stbit("state_encoder_ok",hdw->state_encoder_ok);
4368 return !0;
4369}
4370
4371
4372/* Evaluate whether or not state_encoder_config can change */
4373static int state_eval_encoder_config(struct pvr2_hdw *hdw)
4374{
4375 if (hdw->state_encoder_config) {
4376 if (hdw->state_encoder_ok) {
4377 if (hdw->state_pipeline_req &&
4378 !hdw->state_pipeline_pause) return 0;
4379 }
4380 hdw->state_encoder_config = 0;
4381 hdw->state_encoder_waitok = 0;
4382 trace_stbit("state_encoder_waitok",hdw->state_encoder_waitok);
4383 /* paranoia - solve race if timer just completed */
4384 del_timer_sync(&hdw->encoder_wait_timer);
4385 } else {
4386 if (!hdw->state_pathway_ok ||
4387 (hdw->pathway_state != PVR2_PATHWAY_ANALOG) ||
4388 !hdw->state_encoder_ok ||
4389 !hdw->state_pipeline_idle ||
4390 hdw->state_pipeline_pause ||
4391 !hdw->state_pipeline_req ||
4392 !hdw->state_pipeline_config) {
4393 /* We must reset the enforced wait interval if
4394 anything has happened that might have disturbed
4395 the encoder. This should be a rare case. */
4396 if (timer_pending(&hdw->encoder_wait_timer)) {
4397 del_timer_sync(&hdw->encoder_wait_timer);
4398 }
4399 if (hdw->state_encoder_waitok) {
4400 /* Must clear the state - therefore we did
4401 something to a state bit and must also
4402 return true. */
4403 hdw->state_encoder_waitok = 0;
4404 trace_stbit("state_encoder_waitok",
4405 hdw->state_encoder_waitok);
4406 return !0;
4407 }
4408 return 0;
4409 }
4410 if (!hdw->state_encoder_waitok) {
4411 if (!timer_pending(&hdw->encoder_wait_timer)) {
4412 /* waitok flag wasn't set and timer isn't
4413 running. Check flag once more to avoid
4414 a race then start the timer. This is
4415 the point when we measure out a minimal
4416 quiet interval before doing something to
4417 the encoder. */
4418 if (!hdw->state_encoder_waitok) {
4419 hdw->encoder_wait_timer.expires =
4420 jiffies +
4421 (HZ * TIME_MSEC_ENCODER_WAIT
4422 / 1000);
4423 add_timer(&hdw->encoder_wait_timer);
4424 }
4425 }
4426 /* We can't continue until we know we have been
4427 quiet for the interval measured by this
4428 timer. */
4429 return 0;
4430 }
4431 pvr2_encoder_configure(hdw);
4432 if (hdw->state_encoder_ok) hdw->state_encoder_config = !0;
4433 }
4434 trace_stbit("state_encoder_config",hdw->state_encoder_config);
4435 return !0;
4436}
4437
4438
4439/* Return true if the encoder should not be running. */
4440static int state_check_disable_encoder_run(struct pvr2_hdw *hdw)
4441{
4442 if (!hdw->state_encoder_ok) {
4443 /* Encoder isn't healthy at the moment, so stop it. */
4444 return !0;
4445 }
4446 if (!hdw->state_pathway_ok) {
4447 /* Mode is not understood at the moment (i.e. it wants to
4448 change), so encoder must be stopped. */
4449 return !0;
4450 }
4451
4452 switch (hdw->pathway_state) {
4453 case PVR2_PATHWAY_ANALOG:
4454 if (!hdw->state_decoder_run) {
4455 /* We're in analog mode and the decoder is not
4456 running; thus the encoder should be stopped as
4457 well. */
4458 return !0;
4459 }
4460 break;
4461 case PVR2_PATHWAY_DIGITAL:
4462 if (hdw->state_encoder_runok) {
4463 /* This is a funny case. We're in digital mode so
4464 really the encoder should be stopped. However
4465 if it really is running, only kill it after
4466 runok has been set. This gives a chance for the
4467 onair quirk to function (encoder must run
4468 briefly first, at least once, before onair
4469 digital streaming can work). */
4470 return !0;
4471 }
4472 break;
4473 default:
4474 /* Unknown mode; so encoder should be stopped. */
4475 return !0;
4476 }
4477
4478 /* If we get here, we haven't found a reason to stop the
4479 encoder. */
4480 return 0;
4481}
4482
4483
4484/* Return true if the encoder should be running. */
4485static int state_check_enable_encoder_run(struct pvr2_hdw *hdw)
4486{
4487 if (!hdw->state_encoder_ok) {
4488 /* Don't run the encoder if it isn't healthy... */
4489 return 0;
4490 }
4491 if (!hdw->state_pathway_ok) {
4492 /* Don't run the encoder if we don't (yet) know what mode
4493 we need to be in... */
4494 return 0;
4495 }
4496
4497 switch (hdw->pathway_state) {
4498 case PVR2_PATHWAY_ANALOG:
4499 if (hdw->state_decoder_run && hdw->state_decoder_ready) {
4500 /* In analog mode, if the decoder is running, then
4501 run the encoder. */
4502 return !0;
4503 }
4504 break;
4505 case PVR2_PATHWAY_DIGITAL:
4506 if ((hdw->hdw_desc->digital_control_scheme ==
4507 PVR2_DIGITAL_SCHEME_ONAIR) &&
4508 !hdw->state_encoder_runok) {
4509 /* This is a quirk. OnAir hardware won't stream
4510 digital until the encoder has been run at least
4511 once, for a minimal period of time (empiricially
4512 measured to be 1/4 second). So if we're on
4513 OnAir hardware and the encoder has never been
4514 run at all, then start the encoder. Normal
4515 state machine logic in the driver will
4516 automatically handle the remaining bits. */
4517 return !0;
4518 }
4519 break;
4520 default:
4521 /* For completeness (unknown mode; encoder won't run ever) */
4522 break;
4523 }
4524 /* If we get here, then we haven't found any reason to run the
4525 encoder, so don't run it. */
4526 return 0;
4527}
4528
4529
4530/* Evaluate whether or not state_encoder_run can change */
4531static int state_eval_encoder_run(struct pvr2_hdw *hdw)
4532{
4533 if (hdw->state_encoder_run) {
4534 if (!state_check_disable_encoder_run(hdw)) return 0;
4535 if (hdw->state_encoder_ok) {
4536 del_timer_sync(&hdw->encoder_run_timer);
4537 if (pvr2_encoder_stop(hdw) < 0) return !0;
4538 }
4539 hdw->state_encoder_run = 0;
4540 } else {
4541 if (!state_check_enable_encoder_run(hdw)) return 0;
4542 if (pvr2_encoder_start(hdw) < 0) return !0;
4543 hdw->state_encoder_run = !0;
4544 if (!hdw->state_encoder_runok) {
4545 hdw->encoder_run_timer.expires =
4546 jiffies + (HZ * TIME_MSEC_ENCODER_OK / 1000);
4547 add_timer(&hdw->encoder_run_timer);
4548 }
4549 }
4550 trace_stbit("state_encoder_run",hdw->state_encoder_run);
4551 return !0;
4552}
4553
4554
4555/* Timeout function for quiescent timer. */
4556static void pvr2_hdw_quiescent_timeout(unsigned long data)
4557{
4558 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
4559 hdw->state_decoder_quiescent = !0;
4560 trace_stbit("state_decoder_quiescent",hdw->state_decoder_quiescent);
4561 hdw->state_stale = !0;
4562 queue_work(hdw->workqueue,&hdw->workpoll);
4563}
4564
4565
4566/* Timeout function for decoder stabilization timer. */
4567static void pvr2_hdw_decoder_stabilization_timeout(unsigned long data)
4568{
4569 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
4570 hdw->state_decoder_ready = !0;
4571 trace_stbit("state_decoder_ready", hdw->state_decoder_ready);
4572 hdw->state_stale = !0;
4573 queue_work(hdw->workqueue, &hdw->workpoll);
4574}
4575
4576
4577/* Timeout function for encoder wait timer. */
4578static void pvr2_hdw_encoder_wait_timeout(unsigned long data)
4579{
4580 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
4581 hdw->state_encoder_waitok = !0;
4582 trace_stbit("state_encoder_waitok",hdw->state_encoder_waitok);
4583 hdw->state_stale = !0;
4584 queue_work(hdw->workqueue,&hdw->workpoll);
4585}
4586
4587
4588/* Timeout function for encoder run timer. */
4589static void pvr2_hdw_encoder_run_timeout(unsigned long data)
4590{
4591 struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
4592 if (!hdw->state_encoder_runok) {
4593 hdw->state_encoder_runok = !0;
4594 trace_stbit("state_encoder_runok",hdw->state_encoder_runok);
4595 hdw->state_stale = !0;
4596 queue_work(hdw->workqueue,&hdw->workpoll);
4597 }
4598}
4599
4600
4601/* Evaluate whether or not state_decoder_run can change */
4602static int state_eval_decoder_run(struct pvr2_hdw *hdw)
4603{
4604 if (hdw->state_decoder_run) {
4605 if (hdw->state_encoder_ok) {
4606 if (hdw->state_pipeline_req &&
4607 !hdw->state_pipeline_pause &&
4608 hdw->state_pathway_ok) return 0;
4609 }
4610 if (!hdw->flag_decoder_missed) {
4611 pvr2_decoder_enable(hdw,0);
4612 }
4613 hdw->state_decoder_quiescent = 0;
4614 hdw->state_decoder_run = 0;
4615 /* paranoia - solve race if timer(s) just completed */
4616 del_timer_sync(&hdw->quiescent_timer);
4617 /* Kill the stabilization timer, in case we're killing the
4618 encoder before the previous stabilization interval has
4619 been properly timed. */
4620 del_timer_sync(&hdw->decoder_stabilization_timer);
4621 hdw->state_decoder_ready = 0;
4622 } else {
4623 if (!hdw->state_decoder_quiescent) {
4624 if (!timer_pending(&hdw->quiescent_timer)) {
4625 /* We don't do something about the
4626 quiescent timer until right here because
4627 we also want to catch cases where the
4628 decoder was already not running (like
4629 after initialization) as opposed to
4630 knowing that we had just stopped it.
4631 The second flag check is here to cover a
4632 race - the timer could have run and set
4633 this flag just after the previous check
4634 but before we did the pending check. */
4635 if (!hdw->state_decoder_quiescent) {
4636 hdw->quiescent_timer.expires =
4637 jiffies +
4638 (HZ * TIME_MSEC_DECODER_WAIT
4639 / 1000);
4640 add_timer(&hdw->quiescent_timer);
4641 }
4642 }
4643 /* Don't allow decoder to start again until it has
4644 been quiesced first. This little detail should
4645 hopefully further stabilize the encoder. */
4646 return 0;
4647 }
4648 if (!hdw->state_pathway_ok ||
4649 (hdw->pathway_state != PVR2_PATHWAY_ANALOG) ||
4650 !hdw->state_pipeline_req ||
4651 hdw->state_pipeline_pause ||
4652 !hdw->state_pipeline_config ||
4653 !hdw->state_encoder_config ||
4654 !hdw->state_encoder_ok) return 0;
4655 del_timer_sync(&hdw->quiescent_timer);
4656 if (hdw->flag_decoder_missed) return 0;
4657 if (pvr2_decoder_enable(hdw,!0) < 0) return 0;
4658 hdw->state_decoder_quiescent = 0;
4659 hdw->state_decoder_ready = 0;
4660 hdw->state_decoder_run = !0;
4661 if (hdw->decoder_client_id == PVR2_CLIENT_ID_SAA7115) {
4662 hdw->decoder_stabilization_timer.expires =
4663 jiffies +
4664 (HZ * TIME_MSEC_DECODER_STABILIZATION_WAIT /
4665 1000);
4666 add_timer(&hdw->decoder_stabilization_timer);
4667 } else {
4668 hdw->state_decoder_ready = !0;
4669 }
4670 }
4671 trace_stbit("state_decoder_quiescent",hdw->state_decoder_quiescent);
4672 trace_stbit("state_decoder_run",hdw->state_decoder_run);
4673 trace_stbit("state_decoder_ready", hdw->state_decoder_ready);
4674 return !0;
4675}
4676
4677
4678/* Evaluate whether or not state_usbstream_run can change */
4679static int state_eval_usbstream_run(struct pvr2_hdw *hdw)
4680{
4681 if (hdw->state_usbstream_run) {
4682 int fl = !0;
4683 if (hdw->pathway_state == PVR2_PATHWAY_ANALOG) {
4684 fl = (hdw->state_encoder_ok &&
4685 hdw->state_encoder_run);
4686 } else if ((hdw->pathway_state == PVR2_PATHWAY_DIGITAL) &&
4687 (hdw->hdw_desc->flag_digital_requires_cx23416)) {
4688 fl = hdw->state_encoder_ok;
4689 }
4690 if (fl &&
4691 hdw->state_pipeline_req &&
4692 !hdw->state_pipeline_pause &&
4693 hdw->state_pathway_ok) {
4694 return 0;
4695 }
4696 pvr2_hdw_cmd_usbstream(hdw,0);
4697 hdw->state_usbstream_run = 0;
4698 } else {
4699 if (!hdw->state_pipeline_req ||
4700 hdw->state_pipeline_pause ||
4701 !hdw->state_pathway_ok) return 0;
4702 if (hdw->pathway_state == PVR2_PATHWAY_ANALOG) {
4703 if (!hdw->state_encoder_ok ||
4704 !hdw->state_encoder_run) return 0;
4705 } else if ((hdw->pathway_state == PVR2_PATHWAY_DIGITAL) &&
4706 (hdw->hdw_desc->flag_digital_requires_cx23416)) {
4707 if (!hdw->state_encoder_ok) return 0;
4708 if (hdw->state_encoder_run) return 0;
4709 if (hdw->hdw_desc->digital_control_scheme ==
4710 PVR2_DIGITAL_SCHEME_ONAIR) {
4711 /* OnAir digital receivers won't stream
4712 unless the analog encoder has run first.
4713 Why? I have no idea. But don't even
4714 try until we know the analog side is
4715 known to have run. */
4716 if (!hdw->state_encoder_runok) return 0;
4717 }
4718 }
4719 if (pvr2_hdw_cmd_usbstream(hdw,!0) < 0) return 0;
4720 hdw->state_usbstream_run = !0;
4721 }
4722 trace_stbit("state_usbstream_run",hdw->state_usbstream_run);
4723 return !0;
4724}
4725
4726
4727/* Attempt to configure pipeline, if needed */
4728static int state_eval_pipeline_config(struct pvr2_hdw *hdw)
4729{
4730 if (hdw->state_pipeline_config ||
4731 hdw->state_pipeline_pause) return 0;
4732 pvr2_hdw_commit_execute(hdw);
4733 return !0;
4734}
4735
4736
4737/* Update pipeline idle and pipeline pause tracking states based on other
4738 inputs. This must be called whenever the other relevant inputs have
4739 changed. */
4740static int state_update_pipeline_state(struct pvr2_hdw *hdw)
4741{
4742 unsigned int st;
4743 int updatedFl = 0;
4744 /* Update pipeline state */
4745 st = !(hdw->state_encoder_run ||
4746 hdw->state_decoder_run ||
4747 hdw->state_usbstream_run ||
4748 (!hdw->state_decoder_quiescent));
4749 if (!st != !hdw->state_pipeline_idle) {
4750 hdw->state_pipeline_idle = st;
4751 updatedFl = !0;
4752 }
4753 if (hdw->state_pipeline_idle && hdw->state_pipeline_pause) {
4754 hdw->state_pipeline_pause = 0;
4755 updatedFl = !0;
4756 }
4757 return updatedFl;
4758}
4759
4760
4761typedef int (*state_eval_func)(struct pvr2_hdw *);
4762
4763/* Set of functions to be run to evaluate various states in the driver. */
4764static const state_eval_func eval_funcs[] = {
4765 state_eval_pathway_ok,
4766 state_eval_pipeline_config,
4767 state_eval_encoder_ok,
4768 state_eval_encoder_config,
4769 state_eval_decoder_run,
4770 state_eval_encoder_run,
4771 state_eval_usbstream_run,
4772};
4773
4774
4775/* Process various states and return true if we did anything interesting. */
4776static int pvr2_hdw_state_update(struct pvr2_hdw *hdw)
4777{
4778 unsigned int i;
4779 int state_updated = 0;
4780 int check_flag;
4781
4782 if (!hdw->state_stale) return 0;
4783 if ((hdw->fw1_state != FW1_STATE_OK) ||
4784 !hdw->flag_ok) {
4785 hdw->state_stale = 0;
4786 return !0;
4787 }
4788 /* This loop is the heart of the entire driver. It keeps trying to
4789 evaluate various bits of driver state until nothing changes for
4790 one full iteration. Each "bit of state" tracks some global
4791 aspect of the driver, e.g. whether decoder should run, if
4792 pipeline is configured, usb streaming is on, etc. We separately
4793 evaluate each of those questions based on other driver state to
4794 arrive at the correct running configuration. */
4795 do {
4796 check_flag = 0;
4797 state_update_pipeline_state(hdw);
4798 /* Iterate over each bit of state */
4799 for (i = 0; (i<ARRAY_SIZE(eval_funcs)) && hdw->flag_ok; i++) {
4800 if ((*eval_funcs[i])(hdw)) {
4801 check_flag = !0;
4802 state_updated = !0;
4803 state_update_pipeline_state(hdw);
4804 }
4805 }
4806 } while (check_flag && hdw->flag_ok);
4807 hdw->state_stale = 0;
4808 trace_stbit("state_stale",hdw->state_stale);
4809 return state_updated;
4810}
4811
4812
4813static unsigned int print_input_mask(unsigned int msk,
4814 char *buf,unsigned int acnt)
4815{
4816 unsigned int idx,ccnt;
4817 unsigned int tcnt = 0;
4818 for (idx = 0; idx < ARRAY_SIZE(control_values_input); idx++) {
4819 if (!((1 << idx) & msk)) continue;
4820 ccnt = scnprintf(buf+tcnt,
4821 acnt-tcnt,
4822 "%s%s",
4823 (tcnt ? ", " : ""),
4824 control_values_input[idx]);
4825 tcnt += ccnt;
4826 }
4827 return tcnt;
4828}
4829
4830
4831static const char *pvr2_pathway_state_name(int id)
4832{
4833 switch (id) {
4834 case PVR2_PATHWAY_ANALOG: return "analog";
4835 case PVR2_PATHWAY_DIGITAL: return "digital";
4836 default: return "unknown";
4837 }
4838}
4839
4840
4841static unsigned int pvr2_hdw_report_unlocked(struct pvr2_hdw *hdw,int which,
4842 char *buf,unsigned int acnt)
4843{
4844 switch (which) {
4845 case 0:
4846 return scnprintf(
4847 buf,acnt,
4848 "driver:%s%s%s%s%s <mode=%s>",
4849 (hdw->flag_ok ? " <ok>" : " <fail>"),
4850 (hdw->flag_init_ok ? " <init>" : " <uninitialized>"),
4851 (hdw->flag_disconnected ? " <disconnected>" :
4852 " <connected>"),
4853 (hdw->flag_tripped ? " <tripped>" : ""),
4854 (hdw->flag_decoder_missed ? " <no decoder>" : ""),
4855 pvr2_pathway_state_name(hdw->pathway_state));
4856
4857 case 1:
4858 return scnprintf(
4859 buf,acnt,
4860 "pipeline:%s%s%s%s",
4861 (hdw->state_pipeline_idle ? " <idle>" : ""),
4862 (hdw->state_pipeline_config ?
4863 " <configok>" : " <stale>"),
4864 (hdw->state_pipeline_req ? " <req>" : ""),
4865 (hdw->state_pipeline_pause ? " <pause>" : ""));
4866 case 2:
4867 return scnprintf(
4868 buf,acnt,
4869 "worker:%s%s%s%s%s%s%s",
4870 (hdw->state_decoder_run ?
4871 (hdw->state_decoder_ready ?
4872 "<decode:run>" : " <decode:start>") :
4873 (hdw->state_decoder_quiescent ?
4874 "" : " <decode:stop>")),
4875 (hdw->state_decoder_quiescent ?
4876 " <decode:quiescent>" : ""),
4877 (hdw->state_encoder_ok ?
4878 "" : " <encode:init>"),
4879 (hdw->state_encoder_run ?
4880 (hdw->state_encoder_runok ?
4881 " <encode:run>" :
4882 " <encode:firstrun>") :
4883 (hdw->state_encoder_runok ?
4884 " <encode:stop>" :
4885 " <encode:virgin>")),
4886 (hdw->state_encoder_config ?
4887 " <encode:configok>" :
4888 (hdw->state_encoder_waitok ?
4889 "" : " <encode:waitok>")),
4890 (hdw->state_usbstream_run ?
4891 " <usb:run>" : " <usb:stop>"),
4892 (hdw->state_pathway_ok ?
4893 " <pathway:ok>" : ""));
4894 case 3:
4895 return scnprintf(
4896 buf,acnt,
4897 "state: %s",
4898 pvr2_get_state_name(hdw->master_state));
4899 case 4: {
4900 unsigned int tcnt = 0;
4901 unsigned int ccnt;
4902
4903 ccnt = scnprintf(buf,
4904 acnt,
4905 "Hardware supported inputs: ");
4906 tcnt += ccnt;
4907 tcnt += print_input_mask(hdw->input_avail_mask,
4908 buf+tcnt,
4909 acnt-tcnt);
4910 if (hdw->input_avail_mask != hdw->input_allowed_mask) {
4911 ccnt = scnprintf(buf+tcnt,
4912 acnt-tcnt,
4913 "; allowed inputs: ");
4914 tcnt += ccnt;
4915 tcnt += print_input_mask(hdw->input_allowed_mask,
4916 buf+tcnt,
4917 acnt-tcnt);
4918 }
4919 return tcnt;
4920 }
4921 case 5: {
4922 struct pvr2_stream_stats stats;
4923 if (!hdw->vid_stream) break;
4924 pvr2_stream_get_stats(hdw->vid_stream,
4925 &stats,
4926 0);
4927 return scnprintf(
4928 buf,acnt,
4929 "Bytes streamed=%u"
4930 " URBs: queued=%u idle=%u ready=%u"
4931 " processed=%u failed=%u",
4932 stats.bytes_processed,
4933 stats.buffers_in_queue,
4934 stats.buffers_in_idle,
4935 stats.buffers_in_ready,
4936 stats.buffers_processed,
4937 stats.buffers_failed);
4938 }
4939 case 6: {
4940 unsigned int id = hdw->ir_scheme_active;
4941 return scnprintf(buf, acnt, "ir scheme: id=%d %s", id,
4942 (id >= ARRAY_SIZE(ir_scheme_names) ?
4943 "?" : ir_scheme_names[id]));
4944 }
4945 default: break;
4946 }
4947 return 0;
4948}
4949
4950
4951/* Generate report containing info about attached sub-devices and attached
4952 i2c clients, including an indication of which attached i2c clients are
4953 actually sub-devices. */
4954static unsigned int pvr2_hdw_report_clients(struct pvr2_hdw *hdw,
4955 char *buf, unsigned int acnt)
4956{
4957 struct v4l2_subdev *sd;
4958 unsigned int tcnt = 0;
4959 unsigned int ccnt;
4960 struct i2c_client *client;
4961 const char *p;
4962 unsigned int id;
4963
4964 ccnt = scnprintf(buf, acnt, "Associated v4l2-subdev drivers and I2C clients:\n");
4965 tcnt += ccnt;
4966 v4l2_device_for_each_subdev(sd, &hdw->v4l2_dev) {
4967 id = sd->grp_id;
4968 p = NULL;
4969 if (id < ARRAY_SIZE(module_names)) p = module_names[id];
4970 if (p) {
4971 ccnt = scnprintf(buf + tcnt, acnt - tcnt, " %s:", p);
4972 tcnt += ccnt;
4973 } else {
4974 ccnt = scnprintf(buf + tcnt, acnt - tcnt,
4975 " (unknown id=%u):", id);
4976 tcnt += ccnt;
4977 }
4978 client = v4l2_get_subdevdata(sd);
4979 if (client) {
4980 ccnt = scnprintf(buf + tcnt, acnt - tcnt,
4981 " %s @ %02x\n", client->name,
4982 client->addr);
4983 tcnt += ccnt;
4984 } else {
4985 ccnt = scnprintf(buf + tcnt, acnt - tcnt,
4986 " no i2c client\n");
4987 tcnt += ccnt;
4988 }
4989 }
4990 return tcnt;
4991}
4992
4993
4994unsigned int pvr2_hdw_state_report(struct pvr2_hdw *hdw,
4995 char *buf,unsigned int acnt)
4996{
4997 unsigned int bcnt,ccnt,idx;
4998 bcnt = 0;
4999 LOCK_TAKE(hdw->big_lock);
5000 for (idx = 0; ; idx++) {
5001 ccnt = pvr2_hdw_report_unlocked(hdw,idx,buf,acnt);
5002 if (!ccnt) break;
5003 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
5004 if (!acnt) break;
5005 buf[0] = '\n'; ccnt = 1;
5006 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
5007 }
5008 ccnt = pvr2_hdw_report_clients(hdw, buf, acnt);
5009 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
5010 LOCK_GIVE(hdw->big_lock);
5011 return bcnt;
5012}
5013
5014
5015static void pvr2_hdw_state_log_state(struct pvr2_hdw *hdw)
5016{
5017 char buf[256];
5018 unsigned int idx, ccnt;
5019 unsigned int lcnt, ucnt;
5020
5021 for (idx = 0; ; idx++) {
5022 ccnt = pvr2_hdw_report_unlocked(hdw,idx,buf,sizeof(buf));
5023 if (!ccnt) break;
5024 printk(KERN_INFO "%s %.*s\n",hdw->name,ccnt,buf);
5025 }
5026 ccnt = pvr2_hdw_report_clients(hdw, buf, sizeof(buf));
5027 ucnt = 0;
5028 while (ucnt < ccnt) {
5029 lcnt = 0;
5030 while ((lcnt + ucnt < ccnt) && (buf[lcnt + ucnt] != '\n')) {
5031 lcnt++;
5032 }
5033 printk(KERN_INFO "%s %.*s\n", hdw->name, lcnt, buf + ucnt);
5034 ucnt += lcnt + 1;
5035 }
5036}
5037
5038
5039/* Evaluate and update the driver's current state, taking various actions
5040 as appropriate for the update. */
5041static int pvr2_hdw_state_eval(struct pvr2_hdw *hdw)
5042{
5043 unsigned int st;
5044 int state_updated = 0;
5045 int callback_flag = 0;
5046 int analog_mode;
5047
5048 pvr2_trace(PVR2_TRACE_STBITS,
5049 "Drive state check START");
5050 if (pvrusb2_debug & PVR2_TRACE_STBITS) {
5051 pvr2_hdw_state_log_state(hdw);
5052 }
5053
5054 /* Process all state and get back over disposition */
5055 state_updated = pvr2_hdw_state_update(hdw);
5056
5057 analog_mode = (hdw->pathway_state != PVR2_PATHWAY_DIGITAL);
5058
5059 /* Update master state based upon all other states. */
5060 if (!hdw->flag_ok) {
5061 st = PVR2_STATE_DEAD;
5062 } else if (hdw->fw1_state != FW1_STATE_OK) {
5063 st = PVR2_STATE_COLD;
5064 } else if ((analog_mode ||
5065 hdw->hdw_desc->flag_digital_requires_cx23416) &&
5066 !hdw->state_encoder_ok) {
5067 st = PVR2_STATE_WARM;
5068 } else if (hdw->flag_tripped ||
5069 (analog_mode && hdw->flag_decoder_missed)) {
5070 st = PVR2_STATE_ERROR;
5071 } else if (hdw->state_usbstream_run &&
5072 (!analog_mode ||
5073 (hdw->state_encoder_run && hdw->state_decoder_run))) {
5074 st = PVR2_STATE_RUN;
5075 } else {
5076 st = PVR2_STATE_READY;
5077 }
5078 if (hdw->master_state != st) {
5079 pvr2_trace(PVR2_TRACE_STATE,
5080 "Device state change from %s to %s",
5081 pvr2_get_state_name(hdw->master_state),
5082 pvr2_get_state_name(st));
5083 pvr2_led_ctrl(hdw,st == PVR2_STATE_RUN);
5084 hdw->master_state = st;
5085 state_updated = !0;
5086 callback_flag = !0;
5087 }
5088 if (state_updated) {
5089 /* Trigger anyone waiting on any state changes here. */
5090 wake_up(&hdw->state_wait_data);
5091 }
5092
5093 if (pvrusb2_debug & PVR2_TRACE_STBITS) {
5094 pvr2_hdw_state_log_state(hdw);
5095 }
5096 pvr2_trace(PVR2_TRACE_STBITS,
5097 "Drive state check DONE callback=%d",callback_flag);
5098
5099 return callback_flag;
5100}
5101
5102
5103/* Cause kernel thread to check / update driver state */
5104static void pvr2_hdw_state_sched(struct pvr2_hdw *hdw)
5105{
5106 if (hdw->state_stale) return;
5107 hdw->state_stale = !0;
5108 trace_stbit("state_stale",hdw->state_stale);
5109 queue_work(hdw->workqueue,&hdw->workpoll);
5110}
5111
5112
5113int pvr2_hdw_gpio_get_dir(struct pvr2_hdw *hdw,u32 *dp)
5114{
5115 return pvr2_read_register(hdw,PVR2_GPIO_DIR,dp);
5116}
5117
5118
5119int pvr2_hdw_gpio_get_out(struct pvr2_hdw *hdw,u32 *dp)
5120{
5121 return pvr2_read_register(hdw,PVR2_GPIO_OUT,dp);
5122}
5123
5124
5125int pvr2_hdw_gpio_get_in(struct pvr2_hdw *hdw,u32 *dp)
5126{
5127 return pvr2_read_register(hdw,PVR2_GPIO_IN,dp);
5128}
5129
5130
5131int pvr2_hdw_gpio_chg_dir(struct pvr2_hdw *hdw,u32 msk,u32 val)
5132{
5133 u32 cval,nval;
5134 int ret;
5135 if (~msk) {
5136 ret = pvr2_read_register(hdw,PVR2_GPIO_DIR,&cval);
5137 if (ret) return ret;
5138 nval = (cval & ~msk) | (val & msk);
5139 pvr2_trace(PVR2_TRACE_GPIO,
5140 "GPIO direction changing 0x%x:0x%x"
5141 " from 0x%x to 0x%x",
5142 msk,val,cval,nval);
5143 } else {
5144 nval = val;
5145 pvr2_trace(PVR2_TRACE_GPIO,
5146 "GPIO direction changing to 0x%x",nval);
5147 }
5148 return pvr2_write_register(hdw,PVR2_GPIO_DIR,nval);
5149}
5150
5151
5152int pvr2_hdw_gpio_chg_out(struct pvr2_hdw *hdw,u32 msk,u32 val)
5153{
5154 u32 cval,nval;
5155 int ret;
5156 if (~msk) {
5157 ret = pvr2_read_register(hdw,PVR2_GPIO_OUT,&cval);
5158 if (ret) return ret;
5159 nval = (cval & ~msk) | (val & msk);
5160 pvr2_trace(PVR2_TRACE_GPIO,
5161 "GPIO output changing 0x%x:0x%x from 0x%x to 0x%x",
5162 msk,val,cval,nval);
5163 } else {
5164 nval = val;
5165 pvr2_trace(PVR2_TRACE_GPIO,
5166 "GPIO output changing to 0x%x",nval);
5167 }
5168 return pvr2_write_register(hdw,PVR2_GPIO_OUT,nval);
5169}
5170
5171
5172void pvr2_hdw_status_poll(struct pvr2_hdw *hdw)
5173{
5174 struct v4l2_tuner *vtp = &hdw->tuner_signal_info;
5175 memset(vtp, 0, sizeof(*vtp));
5176 vtp->type = (hdw->input_val == PVR2_CVAL_INPUT_RADIO) ?
5177 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
5178 hdw->tuner_signal_stale = 0;
5179 /* Note: There apparently is no replacement for VIDIOC_CROPCAP
5180 using v4l2-subdev - therefore we can't support that AT ALL right
5181 now. (Of course, no sub-drivers seem to implement it either.
5182 But now it's a a chicken and egg problem...) */
5183 v4l2_device_call_all(&hdw->v4l2_dev, 0, tuner, g_tuner, vtp);
5184 pvr2_trace(PVR2_TRACE_CHIPS, "subdev status poll"
5185 " type=%u strength=%u audio=0x%x cap=0x%x"
5186 " low=%u hi=%u",
5187 vtp->type,
5188 vtp->signal, vtp->rxsubchans, vtp->capability,
5189 vtp->rangelow, vtp->rangehigh);
5190
5191 /* We have to do this to avoid getting into constant polling if
5192 there's nobody to answer a poll of cropcap info. */
5193 hdw->cropcap_stale = 0;
5194}
5195
5196
5197unsigned int pvr2_hdw_get_input_available(struct pvr2_hdw *hdw)
5198{
5199 return hdw->input_avail_mask;
5200}
5201
5202
5203unsigned int pvr2_hdw_get_input_allowed(struct pvr2_hdw *hdw)
5204{
5205 return hdw->input_allowed_mask;
5206}
5207
5208
5209static int pvr2_hdw_set_input(struct pvr2_hdw *hdw,int v)
5210{
5211 if (hdw->input_val != v) {
5212 hdw->input_val = v;
5213 hdw->input_dirty = !0;
5214 }
5215
5216 /* Handle side effects - if we switch to a mode that needs the RF
5217 tuner, then select the right frequency choice as well and mark
5218 it dirty. */
5219 if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
5220 hdw->freqSelector = 0;
5221 hdw->freqDirty = !0;
5222 } else if ((hdw->input_val == PVR2_CVAL_INPUT_TV) ||
5223 (hdw->input_val == PVR2_CVAL_INPUT_DTV)) {
5224 hdw->freqSelector = 1;
5225 hdw->freqDirty = !0;
5226 }
5227 return 0;
5228}
5229
5230
5231int pvr2_hdw_set_input_allowed(struct pvr2_hdw *hdw,
5232 unsigned int change_mask,
5233 unsigned int change_val)
5234{
5235 int ret = 0;
5236 unsigned int nv,m,idx;
5237 LOCK_TAKE(hdw->big_lock);
5238 do {
5239 nv = hdw->input_allowed_mask & ~change_mask;
5240 nv |= (change_val & change_mask);
5241 nv &= hdw->input_avail_mask;
5242 if (!nv) {
5243 /* No legal modes left; return error instead. */
5244 ret = -EPERM;
5245 break;
5246 }
5247 hdw->input_allowed_mask = nv;
5248 if ((1 << hdw->input_val) & hdw->input_allowed_mask) {
5249 /* Current mode is still in the allowed mask, so
5250 we're done. */
5251 break;
5252 }
5253 /* Select and switch to a mode that is still in the allowed
5254 mask */
5255 if (!hdw->input_allowed_mask) {
5256 /* Nothing legal; give up */
5257 break;
5258 }
5259 m = hdw->input_allowed_mask;
5260 for (idx = 0; idx < (sizeof(m) << 3); idx++) {
5261 if (!((1 << idx) & m)) continue;
5262 pvr2_hdw_set_input(hdw,idx);
5263 break;
5264 }
5265 } while (0);
5266 LOCK_GIVE(hdw->big_lock);
5267 return ret;
5268}
5269
5270
5271/* Find I2C address of eeprom */
5272static int pvr2_hdw_get_eeprom_addr(struct pvr2_hdw *hdw)
5273{
5274 int result;
5275 LOCK_TAKE(hdw->ctl_lock); do {
5276 hdw->cmd_buffer[0] = FX2CMD_GET_EEPROM_ADDR;
5277 result = pvr2_send_request(hdw,
5278 hdw->cmd_buffer,1,
5279 hdw->cmd_buffer,1);
5280 if (result < 0) break;
5281 result = hdw->cmd_buffer[0];
5282 } while(0); LOCK_GIVE(hdw->ctl_lock);
5283 return result;
5284}
5285
5286
5287int pvr2_hdw_register_access(struct pvr2_hdw *hdw,
5288 struct v4l2_dbg_match *match, u64 reg_id,
5289 int setFl, u64 *val_ptr)
5290{
5291#ifdef CONFIG_VIDEO_ADV_DEBUG
5292 struct v4l2_dbg_register req;
5293 int stat = 0;
5294 int okFl = 0;
5295
5296 if (!capable(CAP_SYS_ADMIN)) return -EPERM;
5297
5298 req.match = *match;
5299 req.reg = reg_id;
5300 if (setFl) req.val = *val_ptr;
5301 /* It would be nice to know if a sub-device answered the request */
5302 v4l2_device_call_all(&hdw->v4l2_dev, 0, core, g_register, &req);
5303 if (!setFl) *val_ptr = req.val;
5304 if (okFl) {
5305 return stat;
5306 }
5307 return -EINVAL;
5308#else
5309 return -ENOSYS;
5310#endif
5311}
5312
5313
5314/*
5315 Stuff for Emacs to see, in order to encourage consistent editing style:
5316 *** Local Variables: ***
5317 *** mode: c ***
5318 *** fill-column: 75 ***
5319 *** tab-width: 8 ***
5320 *** c-basic-offset: 8 ***
5321 *** End: ***
5322 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-hdw.h b/drivers/media/video/pvrusb2/pvrusb2-hdw.h
new file mode 100644
index 00000000000..d7753ae9ff4
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-hdw.h
@@ -0,0 +1,364 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20#ifndef __PVRUSB2_HDW_H
21#define __PVRUSB2_HDW_H
22
23#include <linux/usb.h>
24#include <linux/videodev2.h>
25#include "pvrusb2-io.h"
26#include "pvrusb2-ctrl.h"
27
28
29/* Private internal control ids, look these up with
30 pvr2_hdw_get_ctrl_by_id() - these are NOT visible in V4L */
31#define PVR2_CID_STDENUM 1
32#define PVR2_CID_STDCUR 2
33#define PVR2_CID_STDAVAIL 3
34#define PVR2_CID_INPUT 4
35#define PVR2_CID_AUDIOMODE 5
36#define PVR2_CID_FREQUENCY 6
37#define PVR2_CID_HRES 7
38#define PVR2_CID_VRES 8
39#define PVR2_CID_CROPL 9
40#define PVR2_CID_CROPT 10
41#define PVR2_CID_CROPW 11
42#define PVR2_CID_CROPH 12
43#define PVR2_CID_CROPCAPPAN 13
44#define PVR2_CID_CROPCAPPAD 14
45#define PVR2_CID_CROPCAPBL 15
46#define PVR2_CID_CROPCAPBT 16
47#define PVR2_CID_CROPCAPBW 17
48#define PVR2_CID_CROPCAPBH 18
49
50/* Legal values for the INPUT state variable */
51#define PVR2_CVAL_INPUT_TV 0
52#define PVR2_CVAL_INPUT_DTV 1
53#define PVR2_CVAL_INPUT_COMPOSITE 2
54#define PVR2_CVAL_INPUT_SVIDEO 3
55#define PVR2_CVAL_INPUT_RADIO 4
56
57enum pvr2_config {
58 pvr2_config_empty, /* No configuration */
59 pvr2_config_mpeg, /* Encoded / compressed video */
60 pvr2_config_vbi, /* Standard vbi info */
61 pvr2_config_pcm, /* Audio raw pcm stream */
62 pvr2_config_rawvideo, /* Video raw frames */
63};
64
65enum pvr2_v4l_type {
66 pvr2_v4l_type_video,
67 pvr2_v4l_type_vbi,
68 pvr2_v4l_type_radio,
69};
70
71/* Major states that we can be in:
72 *
73 * DEAD - Device is in an unusable state and cannot be recovered. This
74 * can happen if we completely lose the ability to communicate with it
75 * (but it might still on the bus). In this state there's nothing we can
76 * do; it must be replugged in order to recover.
77 *
78 * COLD - Device is in an unusable state, needs microcontroller firmware.
79 *
80 * WARM - We can communicate with the device and the proper
81 * microcontroller firmware is running, but other device initialization is
82 * still needed (e.g. encoder firmware).
83 *
84 * ERROR - A problem prevents capture operation (e.g. encoder firmware
85 * missing).
86 *
87 * READY - Device is operational, but not streaming.
88 *
89 * RUN - Device is streaming.
90 *
91 */
92#define PVR2_STATE_NONE 0
93#define PVR2_STATE_DEAD 1
94#define PVR2_STATE_COLD 2
95#define PVR2_STATE_WARM 3
96#define PVR2_STATE_ERROR 4
97#define PVR2_STATE_READY 5
98#define PVR2_STATE_RUN 6
99
100/* Translate configuration enum to a string label */
101const char *pvr2_config_get_name(enum pvr2_config);
102
103struct pvr2_hdw;
104
105/* Create and return a structure for interacting with the underlying
106 hardware */
107struct pvr2_hdw *pvr2_hdw_create(struct usb_interface *intf,
108 const struct usb_device_id *devid);
109
110/* Perform second stage initialization, passing in a notification callback
111 for when the master state changes. */
112int pvr2_hdw_initialize(struct pvr2_hdw *,
113 void (*callback_func)(void *),
114 void *callback_data);
115
116/* Destroy hardware interaction structure */
117void pvr2_hdw_destroy(struct pvr2_hdw *);
118
119/* Return true if in the ready (normal) state */
120int pvr2_hdw_dev_ok(struct pvr2_hdw *);
121
122/* Return small integer number [1..N] for logical instance number of this
123 device. This is useful for indexing array-valued module parameters. */
124int pvr2_hdw_get_unit_number(struct pvr2_hdw *);
125
126/* Get pointer to underlying USB device */
127struct usb_device *pvr2_hdw_get_dev(struct pvr2_hdw *);
128
129/* Retrieve serial number of device */
130unsigned long pvr2_hdw_get_sn(struct pvr2_hdw *);
131
132/* Retrieve bus location info of device */
133const char *pvr2_hdw_get_bus_info(struct pvr2_hdw *);
134
135/* Retrieve per-instance string identifier for this specific device */
136const char *pvr2_hdw_get_device_identifier(struct pvr2_hdw *);
137
138/* Called when hardware has been unplugged */
139void pvr2_hdw_disconnect(struct pvr2_hdw *);
140
141/* Get the number of defined controls */
142unsigned int pvr2_hdw_get_ctrl_count(struct pvr2_hdw *);
143
144/* Retrieve a control handle given its index (0..count-1) */
145struct pvr2_ctrl *pvr2_hdw_get_ctrl_by_index(struct pvr2_hdw *,unsigned int);
146
147/* Retrieve a control handle given its internal ID (if any) */
148struct pvr2_ctrl *pvr2_hdw_get_ctrl_by_id(struct pvr2_hdw *,unsigned int);
149
150/* Retrieve a control handle given its V4L ID (if any) */
151struct pvr2_ctrl *pvr2_hdw_get_ctrl_v4l(struct pvr2_hdw *,unsigned int ctl_id);
152
153/* Retrieve a control handle given its immediate predecessor V4L ID (if any) */
154struct pvr2_ctrl *pvr2_hdw_get_ctrl_nextv4l(struct pvr2_hdw *,
155 unsigned int ctl_id);
156
157/* Commit all control changes made up to this point */
158int pvr2_hdw_commit_ctl(struct pvr2_hdw *);
159
160/* Return a bit mask of valid input selections for this device. Mask bits
161 * will be according to PVR_CVAL_INPUT_xxxx definitions. */
162unsigned int pvr2_hdw_get_input_available(struct pvr2_hdw *);
163
164/* Return a bit mask of allowed input selections for this device. Mask bits
165 * will be according to PVR_CVAL_INPUT_xxxx definitions. */
166unsigned int pvr2_hdw_get_input_allowed(struct pvr2_hdw *);
167
168/* Change the set of allowed input selections for this device. Both
169 change_mask and change_valu are mask bits according to
170 PVR_CVAL_INPUT_xxxx definitions. The change_mask parameter indicate
171 which settings are being changed and the change_val parameter indicates
172 whether corresponding settings are being set or cleared. */
173int pvr2_hdw_set_input_allowed(struct pvr2_hdw *,
174 unsigned int change_mask,
175 unsigned int change_val);
176
177/* Return name for this driver instance */
178const char *pvr2_hdw_get_driver_name(struct pvr2_hdw *);
179
180/* Mark tuner status stale so that it will be re-fetched */
181void pvr2_hdw_execute_tuner_poll(struct pvr2_hdw *);
182
183/* Return information about the tuner */
184int pvr2_hdw_get_tuner_status(struct pvr2_hdw *,struct v4l2_tuner *);
185
186/* Return information about cropping capabilities */
187int pvr2_hdw_get_cropcap(struct pvr2_hdw *, struct v4l2_cropcap *);
188
189/* Query device and see if it thinks it is on a high-speed USB link */
190int pvr2_hdw_is_hsm(struct pvr2_hdw *);
191
192/* Return a string token representative of the hardware type */
193const char *pvr2_hdw_get_type(struct pvr2_hdw *);
194
195/* Return a single line description of the hardware type */
196const char *pvr2_hdw_get_desc(struct pvr2_hdw *);
197
198/* Turn streaming on/off */
199int pvr2_hdw_set_streaming(struct pvr2_hdw *,int);
200
201/* Find out if streaming is on */
202int pvr2_hdw_get_streaming(struct pvr2_hdw *);
203
204/* Retrieve driver overall state */
205int pvr2_hdw_get_state(struct pvr2_hdw *);
206
207/* Configure the type of stream to generate */
208int pvr2_hdw_set_stream_type(struct pvr2_hdw *, enum pvr2_config);
209
210/* Get handle to video output stream */
211struct pvr2_stream *pvr2_hdw_get_video_stream(struct pvr2_hdw *);
212
213/* Emit a video standard struct */
214int pvr2_hdw_get_stdenum_value(struct pvr2_hdw *hdw,struct v4l2_standard *std,
215 unsigned int idx);
216
217/* Enable / disable retrieval of CPU firmware or prom contents. This must
218 be enabled before pvr2_hdw_cpufw_get() will function. Note that doing
219 this may prevent the device from running (and leaving this mode may
220 imply a device reset). */
221void pvr2_hdw_cpufw_set_enabled(struct pvr2_hdw *,
222 int mode, /* 0=8KB FX2, 1=16KB FX2, 2=PROM */
223 int enable_flag);
224
225/* Return true if we're in a mode for retrieval CPU firmware */
226int pvr2_hdw_cpufw_get_enabled(struct pvr2_hdw *);
227
228/* Retrieve a piece of the CPU's firmware at the given offset. Return
229 value is the number of bytes retrieved or zero if we're past the end or
230 an error otherwise (e.g. if firmware retrieval is not enabled). */
231int pvr2_hdw_cpufw_get(struct pvr2_hdw *,unsigned int offs,
232 char *buf,unsigned int cnt);
233
234/* Retrieve a previously stored v4l minor device number */
235int pvr2_hdw_v4l_get_minor_number(struct pvr2_hdw *,enum pvr2_v4l_type index);
236
237/* Store a v4l minor device number */
238void pvr2_hdw_v4l_store_minor_number(struct pvr2_hdw *,
239 enum pvr2_v4l_type index,int);
240
241/* Direct read/write access to chip's registers:
242 match - specify criteria to identify target chip (this is a v4l dbg struct)
243 reg_id - register number to access
244 setFl - true to set the register, false to read it
245 val_ptr - storage location for source / result. */
246int pvr2_hdw_register_access(struct pvr2_hdw *,
247 struct v4l2_dbg_match *match, u64 reg_id,
248 int setFl, u64 *val_ptr);
249
250/* The following entry points are all lower level things you normally don't
251 want to worry about. */
252
253/* Issue a command and get a response from the device. LOTS of higher
254 level stuff is built on this. */
255int pvr2_send_request(struct pvr2_hdw *,
256 void *write_ptr,unsigned int write_len,
257 void *read_ptr,unsigned int read_len);
258
259/* Slightly higher level device communication functions. */
260int pvr2_write_register(struct pvr2_hdw *, u16, u32);
261
262/* Call if for any reason we can't talk to the hardware anymore - this will
263 cause the driver to stop flailing on the device. */
264void pvr2_hdw_render_useless(struct pvr2_hdw *);
265
266/* Set / clear 8051's reset bit */
267void pvr2_hdw_cpureset_assert(struct pvr2_hdw *,int);
268
269/* Execute a USB-commanded device reset */
270void pvr2_hdw_device_reset(struct pvr2_hdw *);
271
272/* Reset worker's error trapping circuit breaker */
273int pvr2_hdw_untrip(struct pvr2_hdw *);
274
275/* Execute hard reset command (after this point it's likely that the
276 encoder will have to be reconfigured). This also clears the "useless"
277 state. */
278int pvr2_hdw_cmd_deep_reset(struct pvr2_hdw *);
279
280/* Execute simple reset command */
281int pvr2_hdw_cmd_powerup(struct pvr2_hdw *);
282
283/* suspend */
284int pvr2_hdw_cmd_powerdown(struct pvr2_hdw *);
285
286/* Order decoder to reset */
287int pvr2_hdw_cmd_decoder_reset(struct pvr2_hdw *);
288
289/* Direct manipulation of GPIO bits */
290int pvr2_hdw_gpio_get_dir(struct pvr2_hdw *hdw,u32 *);
291int pvr2_hdw_gpio_get_out(struct pvr2_hdw *hdw,u32 *);
292int pvr2_hdw_gpio_get_in(struct pvr2_hdw *hdw,u32 *);
293int pvr2_hdw_gpio_chg_dir(struct pvr2_hdw *hdw,u32 msk,u32 val);
294int pvr2_hdw_gpio_chg_out(struct pvr2_hdw *hdw,u32 msk,u32 val);
295
296/* This data structure is specifically for the next function... */
297struct pvr2_hdw_debug_info {
298 int big_lock_held;
299 int ctl_lock_held;
300 int flag_disconnected;
301 int flag_init_ok;
302 int flag_ok;
303 int fw1_state;
304 int flag_decoder_missed;
305 int flag_tripped;
306 int state_encoder_ok;
307 int state_encoder_run;
308 int state_decoder_run;
309 int state_decoder_ready;
310 int state_usbstream_run;
311 int state_decoder_quiescent;
312 int state_pipeline_config;
313 int state_pipeline_req;
314 int state_pipeline_pause;
315 int state_pipeline_idle;
316 int cmd_debug_state;
317 int cmd_debug_write_len;
318 int cmd_debug_read_len;
319 int cmd_debug_write_pend;
320 int cmd_debug_read_pend;
321 int cmd_debug_timeout;
322 int cmd_debug_rstatus;
323 int cmd_debug_wstatus;
324 unsigned char cmd_code;
325};
326
327/* Non-intrusively retrieve internal state info - this is useful for
328 diagnosing lockups. Note that this operation is completed without any
329 kind of locking and so it is not atomic and may yield inconsistent
330 results. This is *purely* a debugging aid. */
331void pvr2_hdw_get_debug_info_unlocked(const struct pvr2_hdw *hdw,
332 struct pvr2_hdw_debug_info *);
333
334/* Intrusively retrieve internal state info - this is useful for
335 diagnosing overall driver state. This operation synchronizes against
336 the overall driver mutex - so if there are locking problems this will
337 likely hang! This is *purely* a debugging aid. */
338void pvr2_hdw_get_debug_info_locked(struct pvr2_hdw *hdw,
339 struct pvr2_hdw_debug_info *);
340
341/* Report out several lines of text that describes driver internal state.
342 Results are written into the passed-in buffer. */
343unsigned int pvr2_hdw_state_report(struct pvr2_hdw *hdw,
344 char *buf_ptr,unsigned int buf_size);
345
346/* Cause modules to log their state once */
347void pvr2_hdw_trigger_module_log(struct pvr2_hdw *hdw);
348
349/* Cause encoder firmware to be uploaded into the device. This is normally
350 done autonomously, but the interface is exported here because it is also
351 a debugging aid. */
352int pvr2_upload_firmware2(struct pvr2_hdw *hdw);
353
354#endif /* __PVRUSB2_HDW_H */
355
356/*
357 Stuff for Emacs to see, in order to encourage consistent editing style:
358 *** Local Variables: ***
359 *** mode: c ***
360 *** fill-column: 75 ***
361 *** tab-width: 8 ***
362 *** c-basic-offset: 8 ***
363 *** End: ***
364 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-i2c-core.c b/drivers/media/video/pvrusb2/pvrusb2-i2c-core.c
new file mode 100644
index 00000000000..e72d5103e77
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-i2c-core.c
@@ -0,0 +1,697 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20
21#include <linux/i2c.h>
22#include <media/ir-kbd-i2c.h>
23#include "pvrusb2-i2c-core.h"
24#include "pvrusb2-hdw-internal.h"
25#include "pvrusb2-debug.h"
26#include "pvrusb2-fx2-cmd.h"
27#include "pvrusb2.h"
28
29#define trace_i2c(...) pvr2_trace(PVR2_TRACE_I2C,__VA_ARGS__)
30
31/*
32
33 This module attempts to implement a compliant I2C adapter for the pvrusb2
34 device.
35
36*/
37
38static unsigned int i2c_scan;
39module_param(i2c_scan, int, S_IRUGO|S_IWUSR);
40MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time");
41
42static int ir_mode[PVR_NUM] = { [0 ... PVR_NUM-1] = 1 };
43module_param_array(ir_mode, int, NULL, 0444);
44MODULE_PARM_DESC(ir_mode,"specify: 0=disable IR reception, 1=normal IR");
45
46static int pvr2_disable_ir_video;
47module_param_named(disable_autoload_ir_video, pvr2_disable_ir_video,
48 int, S_IRUGO|S_IWUSR);
49MODULE_PARM_DESC(disable_autoload_ir_video,
50 "1=do not try to autoload ir_video IR receiver");
51
52static int pvr2_i2c_write(struct pvr2_hdw *hdw, /* Context */
53 u8 i2c_addr, /* I2C address we're talking to */
54 u8 *data, /* Data to write */
55 u16 length) /* Size of data to write */
56{
57 /* Return value - default 0 means success */
58 int ret;
59
60
61 if (!data) length = 0;
62 if (length > (sizeof(hdw->cmd_buffer) - 3)) {
63 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
64 "Killing an I2C write to %u that is too large"
65 " (desired=%u limit=%u)",
66 i2c_addr,
67 length,(unsigned int)(sizeof(hdw->cmd_buffer) - 3));
68 return -ENOTSUPP;
69 }
70
71 LOCK_TAKE(hdw->ctl_lock);
72
73 /* Clear the command buffer (likely to be paranoia) */
74 memset(hdw->cmd_buffer, 0, sizeof(hdw->cmd_buffer));
75
76 /* Set up command buffer for an I2C write */
77 hdw->cmd_buffer[0] = FX2CMD_I2C_WRITE; /* write prefix */
78 hdw->cmd_buffer[1] = i2c_addr; /* i2c addr of chip */
79 hdw->cmd_buffer[2] = length; /* length of what follows */
80 if (length) memcpy(hdw->cmd_buffer + 3, data, length);
81
82 /* Do the operation */
83 ret = pvr2_send_request(hdw,
84 hdw->cmd_buffer,
85 length + 3,
86 hdw->cmd_buffer,
87 1);
88 if (!ret) {
89 if (hdw->cmd_buffer[0] != 8) {
90 ret = -EIO;
91 if (hdw->cmd_buffer[0] != 7) {
92 trace_i2c("unexpected status"
93 " from i2_write[%d]: %d",
94 i2c_addr,hdw->cmd_buffer[0]);
95 }
96 }
97 }
98
99 LOCK_GIVE(hdw->ctl_lock);
100
101 return ret;
102}
103
104static int pvr2_i2c_read(struct pvr2_hdw *hdw, /* Context */
105 u8 i2c_addr, /* I2C address we're talking to */
106 u8 *data, /* Data to write */
107 u16 dlen, /* Size of data to write */
108 u8 *res, /* Where to put data we read */
109 u16 rlen) /* Amount of data to read */
110{
111 /* Return value - default 0 means success */
112 int ret;
113
114
115 if (!data) dlen = 0;
116 if (dlen > (sizeof(hdw->cmd_buffer) - 4)) {
117 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
118 "Killing an I2C read to %u that has wlen too large"
119 " (desired=%u limit=%u)",
120 i2c_addr,
121 dlen,(unsigned int)(sizeof(hdw->cmd_buffer) - 4));
122 return -ENOTSUPP;
123 }
124 if (res && (rlen > (sizeof(hdw->cmd_buffer) - 1))) {
125 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
126 "Killing an I2C read to %u that has rlen too large"
127 " (desired=%u limit=%u)",
128 i2c_addr,
129 rlen,(unsigned int)(sizeof(hdw->cmd_buffer) - 1));
130 return -ENOTSUPP;
131 }
132
133 LOCK_TAKE(hdw->ctl_lock);
134
135 /* Clear the command buffer (likely to be paranoia) */
136 memset(hdw->cmd_buffer, 0, sizeof(hdw->cmd_buffer));
137
138 /* Set up command buffer for an I2C write followed by a read */
139 hdw->cmd_buffer[0] = FX2CMD_I2C_READ; /* read prefix */
140 hdw->cmd_buffer[1] = dlen; /* arg length */
141 hdw->cmd_buffer[2] = rlen; /* answer length. Device will send one
142 more byte (status). */
143 hdw->cmd_buffer[3] = i2c_addr; /* i2c addr of chip */
144 if (dlen) memcpy(hdw->cmd_buffer + 4, data, dlen);
145
146 /* Do the operation */
147 ret = pvr2_send_request(hdw,
148 hdw->cmd_buffer,
149 4 + dlen,
150 hdw->cmd_buffer,
151 rlen + 1);
152 if (!ret) {
153 if (hdw->cmd_buffer[0] != 8) {
154 ret = -EIO;
155 if (hdw->cmd_buffer[0] != 7) {
156 trace_i2c("unexpected status"
157 " from i2_read[%d]: %d",
158 i2c_addr,hdw->cmd_buffer[0]);
159 }
160 }
161 }
162
163 /* Copy back the result */
164 if (res && rlen) {
165 if (ret) {
166 /* Error, just blank out the return buffer */
167 memset(res, 0, rlen);
168 } else {
169 memcpy(res, hdw->cmd_buffer + 1, rlen);
170 }
171 }
172
173 LOCK_GIVE(hdw->ctl_lock);
174
175 return ret;
176}
177
178/* This is the common low level entry point for doing I2C operations to the
179 hardware. */
180static int pvr2_i2c_basic_op(struct pvr2_hdw *hdw,
181 u8 i2c_addr,
182 u8 *wdata,
183 u16 wlen,
184 u8 *rdata,
185 u16 rlen)
186{
187 if (!rdata) rlen = 0;
188 if (!wdata) wlen = 0;
189 if (rlen || !wlen) {
190 return pvr2_i2c_read(hdw,i2c_addr,wdata,wlen,rdata,rlen);
191 } else {
192 return pvr2_i2c_write(hdw,i2c_addr,wdata,wlen);
193 }
194}
195
196
197/* This is a special entry point for cases of I2C transaction attempts to
198 the IR receiver. The implementation here simulates the IR receiver by
199 issuing a command to the FX2 firmware and using that response to return
200 what the real I2C receiver would have returned. We use this for 24xxx
201 devices, where the IR receiver chip has been removed and replaced with
202 FX2 related logic. */
203static int i2c_24xxx_ir(struct pvr2_hdw *hdw,
204 u8 i2c_addr,u8 *wdata,u16 wlen,u8 *rdata,u16 rlen)
205{
206 u8 dat[4];
207 unsigned int stat;
208
209 if (!(rlen || wlen)) {
210 /* This is a probe attempt. Just let it succeed. */
211 return 0;
212 }
213
214 /* We don't understand this kind of transaction */
215 if ((wlen != 0) || (rlen == 0)) return -EIO;
216
217 if (rlen < 3) {
218 /* Mike Isely <isely@pobox.com> Appears to be a probe
219 attempt from lirc. Just fill in zeroes and return. If
220 we try instead to do the full transaction here, then bad
221 things seem to happen within the lirc driver module
222 (version 0.8.0-7 sources from Debian, when run under
223 vanilla 2.6.17.6 kernel) - and I don't have the patience
224 to chase it down. */
225 if (rlen > 0) rdata[0] = 0;
226 if (rlen > 1) rdata[1] = 0;
227 return 0;
228 }
229
230 /* Issue a command to the FX2 to read the IR receiver. */
231 LOCK_TAKE(hdw->ctl_lock); do {
232 hdw->cmd_buffer[0] = FX2CMD_GET_IR_CODE;
233 stat = pvr2_send_request(hdw,
234 hdw->cmd_buffer,1,
235 hdw->cmd_buffer,4);
236 dat[0] = hdw->cmd_buffer[0];
237 dat[1] = hdw->cmd_buffer[1];
238 dat[2] = hdw->cmd_buffer[2];
239 dat[3] = hdw->cmd_buffer[3];
240 } while (0); LOCK_GIVE(hdw->ctl_lock);
241
242 /* Give up if that operation failed. */
243 if (stat != 0) return stat;
244
245 /* Mangle the results into something that looks like the real IR
246 receiver. */
247 rdata[2] = 0xc1;
248 if (dat[0] != 1) {
249 /* No code received. */
250 rdata[0] = 0;
251 rdata[1] = 0;
252 } else {
253 u16 val;
254 /* Mash the FX2 firmware-provided IR code into something
255 that the normal i2c chip-level driver expects. */
256 val = dat[1];
257 val <<= 8;
258 val |= dat[2];
259 val >>= 1;
260 val &= ~0x0003;
261 val |= 0x8000;
262 rdata[0] = (val >> 8) & 0xffu;
263 rdata[1] = val & 0xffu;
264 }
265
266 return 0;
267}
268
269/* This is a special entry point that is entered if an I2C operation is
270 attempted to a wm8775 chip on model 24xxx hardware. Autodetect of this
271 part doesn't work, but we know it is really there. So let's look for
272 the autodetect attempt and just return success if we see that. */
273static int i2c_hack_wm8775(struct pvr2_hdw *hdw,
274 u8 i2c_addr,u8 *wdata,u16 wlen,u8 *rdata,u16 rlen)
275{
276 if (!(rlen || wlen)) {
277 // This is a probe attempt. Just let it succeed.
278 return 0;
279 }
280 return pvr2_i2c_basic_op(hdw,i2c_addr,wdata,wlen,rdata,rlen);
281}
282
283/* This is an entry point designed to always fail any attempt to perform a
284 transfer. We use this to cause certain I2C addresses to not be
285 probed. */
286static int i2c_black_hole(struct pvr2_hdw *hdw,
287 u8 i2c_addr,u8 *wdata,u16 wlen,u8 *rdata,u16 rlen)
288{
289 return -EIO;
290}
291
292/* This is a special entry point that is entered if an I2C operation is
293 attempted to a cx25840 chip on model 24xxx hardware. This chip can
294 sometimes wedge itself. Worse still, when this happens msp3400 can
295 falsely detect this part and then the system gets hosed up after msp3400
296 gets confused and dies. What we want to do here is try to keep msp3400
297 away and also try to notice if the chip is wedged and send a warning to
298 the system log. */
299static int i2c_hack_cx25840(struct pvr2_hdw *hdw,
300 u8 i2c_addr,u8 *wdata,u16 wlen,u8 *rdata,u16 rlen)
301{
302 int ret;
303 unsigned int subaddr;
304 u8 wbuf[2];
305 int state = hdw->i2c_cx25840_hack_state;
306
307 if (!(rlen || wlen)) {
308 // Probe attempt - always just succeed and don't bother the
309 // hardware (this helps to make the state machine further
310 // down somewhat easier).
311 return 0;
312 }
313
314 if (state == 3) {
315 return pvr2_i2c_basic_op(hdw,i2c_addr,wdata,wlen,rdata,rlen);
316 }
317
318 /* We're looking for the exact pattern where the revision register
319 is being read. The cx25840 module will always look at the
320 revision register first. Any other pattern of access therefore
321 has to be a probe attempt from somebody else so we'll reject it.
322 Normally we could just let each client just probe the part
323 anyway, but when the cx25840 is wedged, msp3400 will get a false
324 positive and that just screws things up... */
325
326 if (wlen == 0) {
327 switch (state) {
328 case 1: subaddr = 0x0100; break;
329 case 2: subaddr = 0x0101; break;
330 default: goto fail;
331 }
332 } else if (wlen == 2) {
333 subaddr = (wdata[0] << 8) | wdata[1];
334 switch (subaddr) {
335 case 0x0100: state = 1; break;
336 case 0x0101: state = 2; break;
337 default: goto fail;
338 }
339 } else {
340 goto fail;
341 }
342 if (!rlen) goto success;
343 state = 0;
344 if (rlen != 1) goto fail;
345
346 /* If we get to here then we have a legitimate read for one of the
347 two revision bytes, so pass it through. */
348 wbuf[0] = subaddr >> 8;
349 wbuf[1] = subaddr;
350 ret = pvr2_i2c_basic_op(hdw,i2c_addr,wbuf,2,rdata,rlen);
351
352 if ((ret != 0) || (*rdata == 0x04) || (*rdata == 0x0a)) {
353 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
354 "WARNING: Detected a wedged cx25840 chip;"
355 " the device will not work.");
356 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
357 "WARNING: Try power cycling the pvrusb2 device.");
358 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
359 "WARNING: Disabling further access to the device"
360 " to prevent other foul-ups.");
361 // This blocks all further communication with the part.
362 hdw->i2c_func[0x44] = NULL;
363 pvr2_hdw_render_useless(hdw);
364 goto fail;
365 }
366
367 /* Success! */
368 pvr2_trace(PVR2_TRACE_CHIPS,"cx25840 appears to be OK.");
369 state = 3;
370
371 success:
372 hdw->i2c_cx25840_hack_state = state;
373 return 0;
374
375 fail:
376 hdw->i2c_cx25840_hack_state = state;
377 return -EIO;
378}
379
380/* This is a very, very limited I2C adapter implementation. We can only
381 support what we actually know will work on the device... */
382static int pvr2_i2c_xfer(struct i2c_adapter *i2c_adap,
383 struct i2c_msg msgs[],
384 int num)
385{
386 int ret = -ENOTSUPP;
387 pvr2_i2c_func funcp = NULL;
388 struct pvr2_hdw *hdw = (struct pvr2_hdw *)(i2c_adap->algo_data);
389
390 if (!num) {
391 ret = -EINVAL;
392 goto done;
393 }
394 if (msgs[0].addr < PVR2_I2C_FUNC_CNT) {
395 funcp = hdw->i2c_func[msgs[0].addr];
396 }
397 if (!funcp) {
398 ret = -EIO;
399 goto done;
400 }
401
402 if (num == 1) {
403 if (msgs[0].flags & I2C_M_RD) {
404 /* Simple read */
405 u16 tcnt,bcnt,offs;
406 if (!msgs[0].len) {
407 /* Length == 0 read. This is a probe. */
408 if (funcp(hdw,msgs[0].addr,NULL,0,NULL,0)) {
409 ret = -EIO;
410 goto done;
411 }
412 ret = 1;
413 goto done;
414 }
415 /* If the read is short enough we'll do the whole
416 thing atomically. Otherwise we have no choice
417 but to break apart the reads. */
418 tcnt = msgs[0].len;
419 offs = 0;
420 while (tcnt) {
421 bcnt = tcnt;
422 if (bcnt > sizeof(hdw->cmd_buffer)-1) {
423 bcnt = sizeof(hdw->cmd_buffer)-1;
424 }
425 if (funcp(hdw,msgs[0].addr,NULL,0,
426 msgs[0].buf+offs,bcnt)) {
427 ret = -EIO;
428 goto done;
429 }
430 offs += bcnt;
431 tcnt -= bcnt;
432 }
433 ret = 1;
434 goto done;
435 } else {
436 /* Simple write */
437 ret = 1;
438 if (funcp(hdw,msgs[0].addr,
439 msgs[0].buf,msgs[0].len,NULL,0)) {
440 ret = -EIO;
441 }
442 goto done;
443 }
444 } else if (num == 2) {
445 if (msgs[0].addr != msgs[1].addr) {
446 trace_i2c("i2c refusing 2 phase transfer with"
447 " conflicting target addresses");
448 ret = -ENOTSUPP;
449 goto done;
450 }
451 if ((!((msgs[0].flags & I2C_M_RD))) &&
452 (msgs[1].flags & I2C_M_RD)) {
453 u16 tcnt,bcnt,wcnt,offs;
454 /* Write followed by atomic read. If the read
455 portion is short enough we'll do the whole thing
456 atomically. Otherwise we have no choice but to
457 break apart the reads. */
458 tcnt = msgs[1].len;
459 wcnt = msgs[0].len;
460 offs = 0;
461 while (tcnt || wcnt) {
462 bcnt = tcnt;
463 if (bcnt > sizeof(hdw->cmd_buffer)-1) {
464 bcnt = sizeof(hdw->cmd_buffer)-1;
465 }
466 if (funcp(hdw,msgs[0].addr,
467 msgs[0].buf,wcnt,
468 msgs[1].buf+offs,bcnt)) {
469 ret = -EIO;
470 goto done;
471 }
472 offs += bcnt;
473 tcnt -= bcnt;
474 wcnt = 0;
475 }
476 ret = 2;
477 goto done;
478 } else {
479 trace_i2c("i2c refusing complex transfer"
480 " read0=%d read1=%d",
481 (msgs[0].flags & I2C_M_RD),
482 (msgs[1].flags & I2C_M_RD));
483 }
484 } else {
485 trace_i2c("i2c refusing %d phase transfer",num);
486 }
487
488 done:
489 if (pvrusb2_debug & PVR2_TRACE_I2C_TRAF) {
490 unsigned int idx,offs,cnt;
491 for (idx = 0; idx < num; idx++) {
492 cnt = msgs[idx].len;
493 printk(KERN_INFO
494 "pvrusb2 i2c xfer %u/%u:"
495 " addr=0x%x len=%d %s",
496 idx+1,num,
497 msgs[idx].addr,
498 cnt,
499 (msgs[idx].flags & I2C_M_RD ?
500 "read" : "write"));
501 if ((ret > 0) || !(msgs[idx].flags & I2C_M_RD)) {
502 if (cnt > 8) cnt = 8;
503 printk(" [");
504 for (offs = 0; offs < (cnt>8?8:cnt); offs++) {
505 if (offs) printk(" ");
506 printk("%02x",msgs[idx].buf[offs]);
507 }
508 if (offs < cnt) printk(" ...");
509 printk("]");
510 }
511 if (idx+1 == num) {
512 printk(" result=%d",ret);
513 }
514 printk("\n");
515 }
516 if (!num) {
517 printk(KERN_INFO
518 "pvrusb2 i2c xfer null transfer result=%d\n",
519 ret);
520 }
521 }
522 return ret;
523}
524
525static u32 pvr2_i2c_functionality(struct i2c_adapter *adap)
526{
527 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
528}
529
530static struct i2c_algorithm pvr2_i2c_algo_template = {
531 .master_xfer = pvr2_i2c_xfer,
532 .functionality = pvr2_i2c_functionality,
533};
534
535static struct i2c_adapter pvr2_i2c_adap_template = {
536 .owner = THIS_MODULE,
537 .class = 0,
538};
539
540
541/* Return true if device exists at given address */
542static int do_i2c_probe(struct pvr2_hdw *hdw, int addr)
543{
544 struct i2c_msg msg[1];
545 int rc;
546 msg[0].addr = 0;
547 msg[0].flags = I2C_M_RD;
548 msg[0].len = 0;
549 msg[0].buf = NULL;
550 msg[0].addr = addr;
551 rc = i2c_transfer(&hdw->i2c_adap, msg, ARRAY_SIZE(msg));
552 return rc == 1;
553}
554
555static void do_i2c_scan(struct pvr2_hdw *hdw)
556{
557 int i;
558 printk(KERN_INFO "%s: i2c scan beginning\n", hdw->name);
559 for (i = 0; i < 128; i++) {
560 if (do_i2c_probe(hdw, i)) {
561 printk(KERN_INFO "%s: i2c scan: found device @ 0x%x\n",
562 hdw->name, i);
563 }
564 }
565 printk(KERN_INFO "%s: i2c scan done.\n", hdw->name);
566}
567
568static void pvr2_i2c_register_ir(struct pvr2_hdw *hdw)
569{
570 struct i2c_board_info info;
571 struct IR_i2c_init_data *init_data = &hdw->ir_init_data;
572 if (pvr2_disable_ir_video) {
573 pvr2_trace(PVR2_TRACE_INFO,
574 "Automatic binding of ir_video has been disabled.");
575 return;
576 }
577 memset(&info, 0, sizeof(struct i2c_board_info));
578 switch (hdw->ir_scheme_active) {
579 case PVR2_IR_SCHEME_24XXX: /* FX2-controlled IR */
580 case PVR2_IR_SCHEME_29XXX: /* Original 29xxx device */
581 init_data->ir_codes = RC_MAP_HAUPPAUGE;
582 init_data->internal_get_key_func = IR_KBD_GET_KEY_HAUP;
583 init_data->type = RC_TYPE_RC5;
584 init_data->name = hdw->hdw_desc->description;
585 init_data->polling_interval = 100; /* ms From ir-kbd-i2c */
586 /* IR Receiver */
587 info.addr = 0x18;
588 info.platform_data = init_data;
589 strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
590 pvr2_trace(PVR2_TRACE_INFO, "Binding %s to i2c address 0x%02x.",
591 info.type, info.addr);
592 i2c_new_device(&hdw->i2c_adap, &info);
593 break;
594 case PVR2_IR_SCHEME_ZILOG: /* HVR-1950 style */
595 case PVR2_IR_SCHEME_24XXX_MCE: /* 24xxx MCE device */
596 init_data->ir_codes = RC_MAP_HAUPPAUGE;
597 init_data->internal_get_key_func = IR_KBD_GET_KEY_HAUP_XVR;
598 init_data->type = RC_TYPE_RC5;
599 init_data->name = hdw->hdw_desc->description;
600 /* IR Receiver */
601 info.addr = 0x71;
602 info.platform_data = init_data;
603 strlcpy(info.type, "ir_rx_z8f0811_haup", I2C_NAME_SIZE);
604 pvr2_trace(PVR2_TRACE_INFO, "Binding %s to i2c address 0x%02x.",
605 info.type, info.addr);
606 i2c_new_device(&hdw->i2c_adap, &info);
607 /* IR Trasmitter */
608 info.addr = 0x70;
609 info.platform_data = init_data;
610 strlcpy(info.type, "ir_tx_z8f0811_haup", I2C_NAME_SIZE);
611 pvr2_trace(PVR2_TRACE_INFO, "Binding %s to i2c address 0x%02x.",
612 info.type, info.addr);
613 i2c_new_device(&hdw->i2c_adap, &info);
614 break;
615 default:
616 /* The device either doesn't support I2C-based IR or we
617 don't know (yet) how to operate IR on the device. */
618 break;
619 }
620}
621
622void pvr2_i2c_core_init(struct pvr2_hdw *hdw)
623{
624 unsigned int idx;
625
626 /* The default action for all possible I2C addresses is just to do
627 the transfer normally. */
628 for (idx = 0; idx < PVR2_I2C_FUNC_CNT; idx++) {
629 hdw->i2c_func[idx] = pvr2_i2c_basic_op;
630 }
631
632 /* However, deal with various special cases for 24xxx hardware. */
633 if (ir_mode[hdw->unit_number] == 0) {
634 printk(KERN_INFO "%s: IR disabled\n",hdw->name);
635 hdw->i2c_func[0x18] = i2c_black_hole;
636 } else if (ir_mode[hdw->unit_number] == 1) {
637 if (hdw->ir_scheme_active == PVR2_IR_SCHEME_24XXX) {
638 /* Set up translation so that our IR looks like a
639 29xxx device */
640 hdw->i2c_func[0x18] = i2c_24xxx_ir;
641 }
642 }
643 if (hdw->hdw_desc->flag_has_cx25840) {
644 hdw->i2c_func[0x44] = i2c_hack_cx25840;
645 }
646 if (hdw->hdw_desc->flag_has_wm8775) {
647 hdw->i2c_func[0x1b] = i2c_hack_wm8775;
648 }
649
650 // Configure the adapter and set up everything else related to it.
651 memcpy(&hdw->i2c_adap,&pvr2_i2c_adap_template,sizeof(hdw->i2c_adap));
652 memcpy(&hdw->i2c_algo,&pvr2_i2c_algo_template,sizeof(hdw->i2c_algo));
653 strlcpy(hdw->i2c_adap.name,hdw->name,sizeof(hdw->i2c_adap.name));
654 hdw->i2c_adap.dev.parent = &hdw->usb_dev->dev;
655 hdw->i2c_adap.algo = &hdw->i2c_algo;
656 hdw->i2c_adap.algo_data = hdw;
657 hdw->i2c_linked = !0;
658 i2c_set_adapdata(&hdw->i2c_adap, &hdw->v4l2_dev);
659 i2c_add_adapter(&hdw->i2c_adap);
660 if (hdw->i2c_func[0x18] == i2c_24xxx_ir) {
661 /* Probe for a different type of IR receiver on this
662 device. This is really the only way to differentiate
663 older 24xxx devices from 24xxx variants that include an
664 IR blaster. If the IR blaster is present, the IR
665 receiver is part of that chip and thus we must disable
666 the emulated IR receiver. */
667 if (do_i2c_probe(hdw, 0x71)) {
668 pvr2_trace(PVR2_TRACE_INFO,
669 "Device has newer IR hardware;"
670 " disabling unneeded virtual IR device");
671 hdw->i2c_func[0x18] = NULL;
672 /* Remember that this is a different device... */
673 hdw->ir_scheme_active = PVR2_IR_SCHEME_24XXX_MCE;
674 }
675 }
676 if (i2c_scan) do_i2c_scan(hdw);
677
678 pvr2_i2c_register_ir(hdw);
679}
680
681void pvr2_i2c_core_done(struct pvr2_hdw *hdw)
682{
683 if (hdw->i2c_linked) {
684 i2c_del_adapter(&hdw->i2c_adap);
685 hdw->i2c_linked = 0;
686 }
687}
688
689/*
690 Stuff for Emacs to see, in order to encourage consistent editing style:
691 *** Local Variables: ***
692 *** mode: c ***
693 *** fill-column: 75 ***
694 *** tab-width: 8 ***
695 *** c-basic-offset: 8 ***
696 *** End: ***
697 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-i2c-core.h b/drivers/media/video/pvrusb2/pvrusb2-i2c-core.h
new file mode 100644
index 00000000000..6a75769200b
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-i2c-core.h
@@ -0,0 +1,40 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20#ifndef __PVRUSB2_I2C_CORE_H
21#define __PVRUSB2_I2C_CORE_H
22
23struct pvr2_hdw;
24
25void pvr2_i2c_core_init(struct pvr2_hdw *);
26void pvr2_i2c_core_done(struct pvr2_hdw *);
27
28
29#endif /* __PVRUSB2_I2C_ADAPTER_H */
30
31
32/*
33 Stuff for Emacs to see, in order to encourage consistent editing style:
34 *** Local Variables: ***
35 *** mode: c ***
36 *** fill-column: 75 ***
37 *** tab-width: 8 ***
38 *** c-basic-offset: 8 ***
39 *** End: ***
40 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-io.c b/drivers/media/video/pvrusb2/pvrusb2-io.c
new file mode 100644
index 00000000000..20b6ae0bb40
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-io.c
@@ -0,0 +1,695 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20
21#include "pvrusb2-io.h"
22#include "pvrusb2-debug.h"
23#include <linux/errno.h>
24#include <linux/string.h>
25#include <linux/slab.h>
26#include <linux/mutex.h>
27
28static const char *pvr2_buffer_state_decode(enum pvr2_buffer_state);
29
30#define BUFFER_SIG 0x47653271
31
32// #define SANITY_CHECK_BUFFERS
33
34
35#ifdef SANITY_CHECK_BUFFERS
36#define BUFFER_CHECK(bp) do { \
37 if ((bp)->signature != BUFFER_SIG) { \
38 pvr2_trace(PVR2_TRACE_ERROR_LEGS, \
39 "Buffer %p is bad at %s:%d", \
40 (bp),__FILE__,__LINE__); \
41 pvr2_buffer_describe(bp,"BadSig"); \
42 BUG(); \
43 } \
44} while (0)
45#else
46#define BUFFER_CHECK(bp) do {} while(0)
47#endif
48
49struct pvr2_stream {
50 /* Buffers queued for reading */
51 struct list_head queued_list;
52 unsigned int q_count;
53 unsigned int q_bcount;
54 /* Buffers with retrieved data */
55 struct list_head ready_list;
56 unsigned int r_count;
57 unsigned int r_bcount;
58 /* Buffers available for use */
59 struct list_head idle_list;
60 unsigned int i_count;
61 unsigned int i_bcount;
62 /* Pointers to all buffers */
63 struct pvr2_buffer **buffers;
64 /* Array size of buffers */
65 unsigned int buffer_slot_count;
66 /* Total buffers actually in circulation */
67 unsigned int buffer_total_count;
68 /* Designed number of buffers to be in circulation */
69 unsigned int buffer_target_count;
70 /* Executed when ready list become non-empty */
71 pvr2_stream_callback callback_func;
72 void *callback_data;
73 /* Context for transfer endpoint */
74 struct usb_device *dev;
75 int endpoint;
76 /* Overhead for mutex enforcement */
77 spinlock_t list_lock;
78 struct mutex mutex;
79 /* Tracking state for tolerating errors */
80 unsigned int fail_count;
81 unsigned int fail_tolerance;
82
83 unsigned int buffers_processed;
84 unsigned int buffers_failed;
85 unsigned int bytes_processed;
86};
87
88struct pvr2_buffer {
89 int id;
90 int signature;
91 enum pvr2_buffer_state state;
92 void *ptr; /* Pointer to storage area */
93 unsigned int max_count; /* Size of storage area */
94 unsigned int used_count; /* Amount of valid data in storage area */
95 int status; /* Transfer result status */
96 struct pvr2_stream *stream;
97 struct list_head list_overhead;
98 struct urb *purb;
99};
100
101static const char *pvr2_buffer_state_decode(enum pvr2_buffer_state st)
102{
103 switch (st) {
104 case pvr2_buffer_state_none: return "none";
105 case pvr2_buffer_state_idle: return "idle";
106 case pvr2_buffer_state_queued: return "queued";
107 case pvr2_buffer_state_ready: return "ready";
108 }
109 return "unknown";
110}
111
112#ifdef SANITY_CHECK_BUFFERS
113static void pvr2_buffer_describe(struct pvr2_buffer *bp,const char *msg)
114{
115 pvr2_trace(PVR2_TRACE_INFO,
116 "buffer%s%s %p state=%s id=%d status=%d"
117 " stream=%p purb=%p sig=0x%x",
118 (msg ? " " : ""),
119 (msg ? msg : ""),
120 bp,
121 (bp ? pvr2_buffer_state_decode(bp->state) : "(invalid)"),
122 (bp ? bp->id : 0),
123 (bp ? bp->status : 0),
124 (bp ? bp->stream : NULL),
125 (bp ? bp->purb : NULL),
126 (bp ? bp->signature : 0));
127}
128#endif /* SANITY_CHECK_BUFFERS */
129
130static void pvr2_buffer_remove(struct pvr2_buffer *bp)
131{
132 unsigned int *cnt;
133 unsigned int *bcnt;
134 unsigned int ccnt;
135 struct pvr2_stream *sp = bp->stream;
136 switch (bp->state) {
137 case pvr2_buffer_state_idle:
138 cnt = &sp->i_count;
139 bcnt = &sp->i_bcount;
140 ccnt = bp->max_count;
141 break;
142 case pvr2_buffer_state_queued:
143 cnt = &sp->q_count;
144 bcnt = &sp->q_bcount;
145 ccnt = bp->max_count;
146 break;
147 case pvr2_buffer_state_ready:
148 cnt = &sp->r_count;
149 bcnt = &sp->r_bcount;
150 ccnt = bp->used_count;
151 break;
152 default:
153 return;
154 }
155 list_del_init(&bp->list_overhead);
156 (*cnt)--;
157 (*bcnt) -= ccnt;
158 pvr2_trace(PVR2_TRACE_BUF_FLOW,
159 "/*---TRACE_FLOW---*/"
160 " bufferPool %8s dec cap=%07d cnt=%02d",
161 pvr2_buffer_state_decode(bp->state),*bcnt,*cnt);
162 bp->state = pvr2_buffer_state_none;
163}
164
165static void pvr2_buffer_set_none(struct pvr2_buffer *bp)
166{
167 unsigned long irq_flags;
168 struct pvr2_stream *sp;
169 BUFFER_CHECK(bp);
170 sp = bp->stream;
171 pvr2_trace(PVR2_TRACE_BUF_FLOW,
172 "/*---TRACE_FLOW---*/ bufferState %p %6s --> %6s",
173 bp,
174 pvr2_buffer_state_decode(bp->state),
175 pvr2_buffer_state_decode(pvr2_buffer_state_none));
176 spin_lock_irqsave(&sp->list_lock,irq_flags);
177 pvr2_buffer_remove(bp);
178 spin_unlock_irqrestore(&sp->list_lock,irq_flags);
179}
180
181static int pvr2_buffer_set_ready(struct pvr2_buffer *bp)
182{
183 int fl;
184 unsigned long irq_flags;
185 struct pvr2_stream *sp;
186 BUFFER_CHECK(bp);
187 sp = bp->stream;
188 pvr2_trace(PVR2_TRACE_BUF_FLOW,
189 "/*---TRACE_FLOW---*/ bufferState %p %6s --> %6s",
190 bp,
191 pvr2_buffer_state_decode(bp->state),
192 pvr2_buffer_state_decode(pvr2_buffer_state_ready));
193 spin_lock_irqsave(&sp->list_lock,irq_flags);
194 fl = (sp->r_count == 0);
195 pvr2_buffer_remove(bp);
196 list_add_tail(&bp->list_overhead,&sp->ready_list);
197 bp->state = pvr2_buffer_state_ready;
198 (sp->r_count)++;
199 sp->r_bcount += bp->used_count;
200 pvr2_trace(PVR2_TRACE_BUF_FLOW,
201 "/*---TRACE_FLOW---*/"
202 " bufferPool %8s inc cap=%07d cnt=%02d",
203 pvr2_buffer_state_decode(bp->state),
204 sp->r_bcount,sp->r_count);
205 spin_unlock_irqrestore(&sp->list_lock,irq_flags);
206 return fl;
207}
208
209static void pvr2_buffer_set_idle(struct pvr2_buffer *bp)
210{
211 unsigned long irq_flags;
212 struct pvr2_stream *sp;
213 BUFFER_CHECK(bp);
214 sp = bp->stream;
215 pvr2_trace(PVR2_TRACE_BUF_FLOW,
216 "/*---TRACE_FLOW---*/ bufferState %p %6s --> %6s",
217 bp,
218 pvr2_buffer_state_decode(bp->state),
219 pvr2_buffer_state_decode(pvr2_buffer_state_idle));
220 spin_lock_irqsave(&sp->list_lock,irq_flags);
221 pvr2_buffer_remove(bp);
222 list_add_tail(&bp->list_overhead,&sp->idle_list);
223 bp->state = pvr2_buffer_state_idle;
224 (sp->i_count)++;
225 sp->i_bcount += bp->max_count;
226 pvr2_trace(PVR2_TRACE_BUF_FLOW,
227 "/*---TRACE_FLOW---*/"
228 " bufferPool %8s inc cap=%07d cnt=%02d",
229 pvr2_buffer_state_decode(bp->state),
230 sp->i_bcount,sp->i_count);
231 spin_unlock_irqrestore(&sp->list_lock,irq_flags);
232}
233
234static void pvr2_buffer_set_queued(struct pvr2_buffer *bp)
235{
236 unsigned long irq_flags;
237 struct pvr2_stream *sp;
238 BUFFER_CHECK(bp);
239 sp = bp->stream;
240 pvr2_trace(PVR2_TRACE_BUF_FLOW,
241 "/*---TRACE_FLOW---*/ bufferState %p %6s --> %6s",
242 bp,
243 pvr2_buffer_state_decode(bp->state),
244 pvr2_buffer_state_decode(pvr2_buffer_state_queued));
245 spin_lock_irqsave(&sp->list_lock,irq_flags);
246 pvr2_buffer_remove(bp);
247 list_add_tail(&bp->list_overhead,&sp->queued_list);
248 bp->state = pvr2_buffer_state_queued;
249 (sp->q_count)++;
250 sp->q_bcount += bp->max_count;
251 pvr2_trace(PVR2_TRACE_BUF_FLOW,
252 "/*---TRACE_FLOW---*/"
253 " bufferPool %8s inc cap=%07d cnt=%02d",
254 pvr2_buffer_state_decode(bp->state),
255 sp->q_bcount,sp->q_count);
256 spin_unlock_irqrestore(&sp->list_lock,irq_flags);
257}
258
259static void pvr2_buffer_wipe(struct pvr2_buffer *bp)
260{
261 if (bp->state == pvr2_buffer_state_queued) {
262 usb_kill_urb(bp->purb);
263 }
264}
265
266static int pvr2_buffer_init(struct pvr2_buffer *bp,
267 struct pvr2_stream *sp,
268 unsigned int id)
269{
270 memset(bp,0,sizeof(*bp));
271 bp->signature = BUFFER_SIG;
272 bp->id = id;
273 pvr2_trace(PVR2_TRACE_BUF_POOL,
274 "/*---TRACE_FLOW---*/ bufferInit %p stream=%p",bp,sp);
275 bp->stream = sp;
276 bp->state = pvr2_buffer_state_none;
277 INIT_LIST_HEAD(&bp->list_overhead);
278 bp->purb = usb_alloc_urb(0,GFP_KERNEL);
279 if (! bp->purb) return -ENOMEM;
280#ifdef SANITY_CHECK_BUFFERS
281 pvr2_buffer_describe(bp,"create");
282#endif
283 return 0;
284}
285
286static void pvr2_buffer_done(struct pvr2_buffer *bp)
287{
288#ifdef SANITY_CHECK_BUFFERS
289 pvr2_buffer_describe(bp,"delete");
290#endif
291 pvr2_buffer_wipe(bp);
292 pvr2_buffer_set_none(bp);
293 bp->signature = 0;
294 bp->stream = NULL;
295 usb_free_urb(bp->purb);
296 pvr2_trace(PVR2_TRACE_BUF_POOL,"/*---TRACE_FLOW---*/"
297 " bufferDone %p",bp);
298}
299
300static int pvr2_stream_buffer_count(struct pvr2_stream *sp,unsigned int cnt)
301{
302 int ret;
303 unsigned int scnt;
304
305 /* Allocate buffers pointer array in multiples of 32 entries */
306 if (cnt == sp->buffer_total_count) return 0;
307
308 pvr2_trace(PVR2_TRACE_BUF_POOL,
309 "/*---TRACE_FLOW---*/ poolResize "
310 " stream=%p cur=%d adj=%+d",
311 sp,
312 sp->buffer_total_count,
313 cnt-sp->buffer_total_count);
314
315 scnt = cnt & ~0x1f;
316 if (cnt > scnt) scnt += 0x20;
317
318 if (cnt > sp->buffer_total_count) {
319 if (scnt > sp->buffer_slot_count) {
320 struct pvr2_buffer **nb;
321 nb = kmalloc(scnt * sizeof(*nb),GFP_KERNEL);
322 if (!nb) return -ENOMEM;
323 if (sp->buffer_slot_count) {
324 memcpy(nb,sp->buffers,
325 sp->buffer_slot_count * sizeof(*nb));
326 kfree(sp->buffers);
327 }
328 sp->buffers = nb;
329 sp->buffer_slot_count = scnt;
330 }
331 while (sp->buffer_total_count < cnt) {
332 struct pvr2_buffer *bp;
333 bp = kmalloc(sizeof(*bp),GFP_KERNEL);
334 if (!bp) return -ENOMEM;
335 ret = pvr2_buffer_init(bp,sp,sp->buffer_total_count);
336 if (ret) {
337 kfree(bp);
338 return -ENOMEM;
339 }
340 sp->buffers[sp->buffer_total_count] = bp;
341 (sp->buffer_total_count)++;
342 pvr2_buffer_set_idle(bp);
343 }
344 } else {
345 while (sp->buffer_total_count > cnt) {
346 struct pvr2_buffer *bp;
347 bp = sp->buffers[sp->buffer_total_count - 1];
348 /* Paranoia */
349 sp->buffers[sp->buffer_total_count - 1] = NULL;
350 (sp->buffer_total_count)--;
351 pvr2_buffer_done(bp);
352 kfree(bp);
353 }
354 if (scnt < sp->buffer_slot_count) {
355 struct pvr2_buffer **nb = NULL;
356 if (scnt) {
357 nb = kmalloc(scnt * sizeof(*nb),GFP_KERNEL);
358 if (!nb) return -ENOMEM;
359 memcpy(nb,sp->buffers,scnt * sizeof(*nb));
360 }
361 kfree(sp->buffers);
362 sp->buffers = nb;
363 sp->buffer_slot_count = scnt;
364 }
365 }
366 return 0;
367}
368
369static int pvr2_stream_achieve_buffer_count(struct pvr2_stream *sp)
370{
371 struct pvr2_buffer *bp;
372 unsigned int cnt;
373
374 if (sp->buffer_total_count == sp->buffer_target_count) return 0;
375
376 pvr2_trace(PVR2_TRACE_BUF_POOL,
377 "/*---TRACE_FLOW---*/"
378 " poolCheck stream=%p cur=%d tgt=%d",
379 sp,sp->buffer_total_count,sp->buffer_target_count);
380
381 if (sp->buffer_total_count < sp->buffer_target_count) {
382 return pvr2_stream_buffer_count(sp,sp->buffer_target_count);
383 }
384
385 cnt = 0;
386 while ((sp->buffer_total_count - cnt) > sp->buffer_target_count) {
387 bp = sp->buffers[sp->buffer_total_count - (cnt + 1)];
388 if (bp->state != pvr2_buffer_state_idle) break;
389 cnt++;
390 }
391 if (cnt) {
392 pvr2_stream_buffer_count(sp,sp->buffer_total_count - cnt);
393 }
394
395 return 0;
396}
397
398static void pvr2_stream_internal_flush(struct pvr2_stream *sp)
399{
400 struct list_head *lp;
401 struct pvr2_buffer *bp1;
402 while ((lp = sp->queued_list.next) != &sp->queued_list) {
403 bp1 = list_entry(lp,struct pvr2_buffer,list_overhead);
404 pvr2_buffer_wipe(bp1);
405 /* At this point, we should be guaranteed that no
406 completion callback may happen on this buffer. But it's
407 possible that it might have completed after we noticed
408 it but before we wiped it. So double check its status
409 here first. */
410 if (bp1->state != pvr2_buffer_state_queued) continue;
411 pvr2_buffer_set_idle(bp1);
412 }
413 if (sp->buffer_total_count != sp->buffer_target_count) {
414 pvr2_stream_achieve_buffer_count(sp);
415 }
416}
417
418static void pvr2_stream_init(struct pvr2_stream *sp)
419{
420 spin_lock_init(&sp->list_lock);
421 mutex_init(&sp->mutex);
422 INIT_LIST_HEAD(&sp->queued_list);
423 INIT_LIST_HEAD(&sp->ready_list);
424 INIT_LIST_HEAD(&sp->idle_list);
425}
426
427static void pvr2_stream_done(struct pvr2_stream *sp)
428{
429 mutex_lock(&sp->mutex); do {
430 pvr2_stream_internal_flush(sp);
431 pvr2_stream_buffer_count(sp,0);
432 } while (0); mutex_unlock(&sp->mutex);
433}
434
435static void buffer_complete(struct urb *urb)
436{
437 struct pvr2_buffer *bp = urb->context;
438 struct pvr2_stream *sp;
439 unsigned long irq_flags;
440 BUFFER_CHECK(bp);
441 sp = bp->stream;
442 bp->used_count = 0;
443 bp->status = 0;
444 pvr2_trace(PVR2_TRACE_BUF_FLOW,
445 "/*---TRACE_FLOW---*/ bufferComplete %p stat=%d cnt=%d",
446 bp,urb->status,urb->actual_length);
447 spin_lock_irqsave(&sp->list_lock,irq_flags);
448 if ((!(urb->status)) ||
449 (urb->status == -ENOENT) ||
450 (urb->status == -ECONNRESET) ||
451 (urb->status == -ESHUTDOWN)) {
452 (sp->buffers_processed)++;
453 sp->bytes_processed += urb->actual_length;
454 bp->used_count = urb->actual_length;
455 if (sp->fail_count) {
456 pvr2_trace(PVR2_TRACE_TOLERANCE,
457 "stream %p transfer ok"
458 " - fail count reset",sp);
459 sp->fail_count = 0;
460 }
461 } else if (sp->fail_count < sp->fail_tolerance) {
462 // We can tolerate this error, because we're below the
463 // threshold...
464 (sp->fail_count)++;
465 (sp->buffers_failed)++;
466 pvr2_trace(PVR2_TRACE_TOLERANCE,
467 "stream %p ignoring error %d"
468 " - fail count increased to %u",
469 sp,urb->status,sp->fail_count);
470 } else {
471 (sp->buffers_failed)++;
472 bp->status = urb->status;
473 }
474 spin_unlock_irqrestore(&sp->list_lock,irq_flags);
475 pvr2_buffer_set_ready(bp);
476 if (sp && sp->callback_func) {
477 sp->callback_func(sp->callback_data);
478 }
479}
480
481struct pvr2_stream *pvr2_stream_create(void)
482{
483 struct pvr2_stream *sp;
484 sp = kzalloc(sizeof(*sp),GFP_KERNEL);
485 if (!sp) return sp;
486 pvr2_trace(PVR2_TRACE_INIT,"pvr2_stream_create: sp=%p",sp);
487 pvr2_stream_init(sp);
488 return sp;
489}
490
491void pvr2_stream_destroy(struct pvr2_stream *sp)
492{
493 if (!sp) return;
494 pvr2_trace(PVR2_TRACE_INIT,"pvr2_stream_destroy: sp=%p",sp);
495 pvr2_stream_done(sp);
496 kfree(sp);
497}
498
499void pvr2_stream_setup(struct pvr2_stream *sp,
500 struct usb_device *dev,
501 int endpoint,
502 unsigned int tolerance)
503{
504 mutex_lock(&sp->mutex); do {
505 pvr2_stream_internal_flush(sp);
506 sp->dev = dev;
507 sp->endpoint = endpoint;
508 sp->fail_tolerance = tolerance;
509 } while(0); mutex_unlock(&sp->mutex);
510}
511
512void pvr2_stream_set_callback(struct pvr2_stream *sp,
513 pvr2_stream_callback func,
514 void *data)
515{
516 unsigned long irq_flags;
517 mutex_lock(&sp->mutex); do {
518 spin_lock_irqsave(&sp->list_lock,irq_flags);
519 sp->callback_data = data;
520 sp->callback_func = func;
521 spin_unlock_irqrestore(&sp->list_lock,irq_flags);
522 } while(0); mutex_unlock(&sp->mutex);
523}
524
525void pvr2_stream_get_stats(struct pvr2_stream *sp,
526 struct pvr2_stream_stats *stats,
527 int zero_counts)
528{
529 unsigned long irq_flags;
530 spin_lock_irqsave(&sp->list_lock,irq_flags);
531 if (stats) {
532 stats->buffers_in_queue = sp->q_count;
533 stats->buffers_in_idle = sp->i_count;
534 stats->buffers_in_ready = sp->r_count;
535 stats->buffers_processed = sp->buffers_processed;
536 stats->buffers_failed = sp->buffers_failed;
537 stats->bytes_processed = sp->bytes_processed;
538 }
539 if (zero_counts) {
540 sp->buffers_processed = 0;
541 sp->buffers_failed = 0;
542 sp->bytes_processed = 0;
543 }
544 spin_unlock_irqrestore(&sp->list_lock,irq_flags);
545}
546
547/* Query / set the nominal buffer count */
548int pvr2_stream_get_buffer_count(struct pvr2_stream *sp)
549{
550 return sp->buffer_target_count;
551}
552
553int pvr2_stream_set_buffer_count(struct pvr2_stream *sp,unsigned int cnt)
554{
555 int ret;
556 if (sp->buffer_target_count == cnt) return 0;
557 mutex_lock(&sp->mutex); do {
558 sp->buffer_target_count = cnt;
559 ret = pvr2_stream_achieve_buffer_count(sp);
560 } while(0); mutex_unlock(&sp->mutex);
561 return ret;
562}
563
564struct pvr2_buffer *pvr2_stream_get_idle_buffer(struct pvr2_stream *sp)
565{
566 struct list_head *lp = sp->idle_list.next;
567 if (lp == &sp->idle_list) return NULL;
568 return list_entry(lp,struct pvr2_buffer,list_overhead);
569}
570
571struct pvr2_buffer *pvr2_stream_get_ready_buffer(struct pvr2_stream *sp)
572{
573 struct list_head *lp = sp->ready_list.next;
574 if (lp == &sp->ready_list) return NULL;
575 return list_entry(lp,struct pvr2_buffer,list_overhead);
576}
577
578struct pvr2_buffer *pvr2_stream_get_buffer(struct pvr2_stream *sp,int id)
579{
580 if (id < 0) return NULL;
581 if (id >= sp->buffer_total_count) return NULL;
582 return sp->buffers[id];
583}
584
585int pvr2_stream_get_ready_count(struct pvr2_stream *sp)
586{
587 return sp->r_count;
588}
589
590void pvr2_stream_kill(struct pvr2_stream *sp)
591{
592 struct pvr2_buffer *bp;
593 mutex_lock(&sp->mutex); do {
594 pvr2_stream_internal_flush(sp);
595 while ((bp = pvr2_stream_get_ready_buffer(sp)) != NULL) {
596 pvr2_buffer_set_idle(bp);
597 }
598 if (sp->buffer_total_count != sp->buffer_target_count) {
599 pvr2_stream_achieve_buffer_count(sp);
600 }
601 } while(0); mutex_unlock(&sp->mutex);
602}
603
604int pvr2_buffer_queue(struct pvr2_buffer *bp)
605{
606#undef SEED_BUFFER
607#ifdef SEED_BUFFER
608 unsigned int idx;
609 unsigned int val;
610#endif
611 int ret = 0;
612 struct pvr2_stream *sp;
613 if (!bp) return -EINVAL;
614 sp = bp->stream;
615 mutex_lock(&sp->mutex); do {
616 pvr2_buffer_wipe(bp);
617 if (!sp->dev) {
618 ret = -EIO;
619 break;
620 }
621 pvr2_buffer_set_queued(bp);
622#ifdef SEED_BUFFER
623 for (idx = 0; idx < (bp->max_count) / 4; idx++) {
624 val = bp->id << 24;
625 val |= idx;
626 ((unsigned int *)(bp->ptr))[idx] = val;
627 }
628#endif
629 bp->status = -EINPROGRESS;
630 usb_fill_bulk_urb(bp->purb, // struct urb *urb
631 sp->dev, // struct usb_device *dev
632 // endpoint (below)
633 usb_rcvbulkpipe(sp->dev,sp->endpoint),
634 bp->ptr, // void *transfer_buffer
635 bp->max_count, // int buffer_length
636 buffer_complete,
637 bp);
638 usb_submit_urb(bp->purb,GFP_KERNEL);
639 } while(0); mutex_unlock(&sp->mutex);
640 return ret;
641}
642
643int pvr2_buffer_set_buffer(struct pvr2_buffer *bp,void *ptr,unsigned int cnt)
644{
645 int ret = 0;
646 unsigned long irq_flags;
647 struct pvr2_stream *sp;
648 if (!bp) return -EINVAL;
649 sp = bp->stream;
650 mutex_lock(&sp->mutex); do {
651 spin_lock_irqsave(&sp->list_lock,irq_flags);
652 if (bp->state != pvr2_buffer_state_idle) {
653 ret = -EPERM;
654 } else {
655 bp->ptr = ptr;
656 bp->stream->i_bcount -= bp->max_count;
657 bp->max_count = cnt;
658 bp->stream->i_bcount += bp->max_count;
659 pvr2_trace(PVR2_TRACE_BUF_FLOW,
660 "/*---TRACE_FLOW---*/ bufferPool "
661 " %8s cap cap=%07d cnt=%02d",
662 pvr2_buffer_state_decode(
663 pvr2_buffer_state_idle),
664 bp->stream->i_bcount,bp->stream->i_count);
665 }
666 spin_unlock_irqrestore(&sp->list_lock,irq_flags);
667 } while(0); mutex_unlock(&sp->mutex);
668 return ret;
669}
670
671unsigned int pvr2_buffer_get_count(struct pvr2_buffer *bp)
672{
673 return bp->used_count;
674}
675
676int pvr2_buffer_get_status(struct pvr2_buffer *bp)
677{
678 return bp->status;
679}
680
681int pvr2_buffer_get_id(struct pvr2_buffer *bp)
682{
683 return bp->id;
684}
685
686
687/*
688 Stuff for Emacs to see, in order to encourage consistent editing style:
689 *** Local Variables: ***
690 *** mode: c ***
691 *** fill-column: 75 ***
692 *** tab-width: 8 ***
693 *** c-basic-offset: 8 ***
694 *** End: ***
695 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-io.h b/drivers/media/video/pvrusb2/pvrusb2-io.h
new file mode 100644
index 00000000000..afb7e87c039
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-io.h
@@ -0,0 +1,102 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20#ifndef __PVRUSB2_IO_H
21#define __PVRUSB2_IO_H
22
23#include <linux/usb.h>
24#include <linux/list.h>
25
26typedef void (*pvr2_stream_callback)(void *);
27
28enum pvr2_buffer_state {
29 pvr2_buffer_state_none = 0, // Not on any list
30 pvr2_buffer_state_idle = 1, // Buffer is ready to be used again
31 pvr2_buffer_state_queued = 2, // Buffer has been queued for filling
32 pvr2_buffer_state_ready = 3, // Buffer has data available
33};
34
35struct pvr2_stream;
36struct pvr2_buffer;
37
38struct pvr2_stream_stats {
39 unsigned int buffers_in_queue;
40 unsigned int buffers_in_idle;
41 unsigned int buffers_in_ready;
42 unsigned int buffers_processed;
43 unsigned int buffers_failed;
44 unsigned int bytes_processed;
45};
46
47/* Initialize / tear down stream structure */
48struct pvr2_stream *pvr2_stream_create(void);
49void pvr2_stream_destroy(struct pvr2_stream *);
50void pvr2_stream_setup(struct pvr2_stream *,
51 struct usb_device *dev,int endpoint,
52 unsigned int tolerance);
53void pvr2_stream_set_callback(struct pvr2_stream *,
54 pvr2_stream_callback func,
55 void *data);
56void pvr2_stream_get_stats(struct pvr2_stream *,
57 struct pvr2_stream_stats *,
58 int zero_counts);
59
60/* Query / set the nominal buffer count */
61int pvr2_stream_get_buffer_count(struct pvr2_stream *);
62int pvr2_stream_set_buffer_count(struct pvr2_stream *,unsigned int);
63
64/* Get a pointer to a buffer that is either idle, ready, or is specified
65 named. */
66struct pvr2_buffer *pvr2_stream_get_idle_buffer(struct pvr2_stream *);
67struct pvr2_buffer *pvr2_stream_get_ready_buffer(struct pvr2_stream *);
68struct pvr2_buffer *pvr2_stream_get_buffer(struct pvr2_stream *sp,int id);
69
70/* Find out how many buffers are idle or ready */
71int pvr2_stream_get_ready_count(struct pvr2_stream *);
72
73
74/* Kill all pending buffers and throw away any ready buffers as well */
75void pvr2_stream_kill(struct pvr2_stream *);
76
77/* Set up the actual storage for a buffer */
78int pvr2_buffer_set_buffer(struct pvr2_buffer *,void *ptr,unsigned int cnt);
79
80/* Find out size of data in the given ready buffer */
81unsigned int pvr2_buffer_get_count(struct pvr2_buffer *);
82
83/* Retrieve completion code for given ready buffer */
84int pvr2_buffer_get_status(struct pvr2_buffer *);
85
86/* Retrieve ID of given buffer */
87int pvr2_buffer_get_id(struct pvr2_buffer *);
88
89/* Start reading into given buffer (kill it if needed) */
90int pvr2_buffer_queue(struct pvr2_buffer *);
91
92#endif /* __PVRUSB2_IO_H */
93
94/*
95 Stuff for Emacs to see, in order to encourage consistent editing style:
96 *** Local Variables: ***
97 *** mode: c ***
98 *** fill-column: 75 ***
99 *** tab-width: 8 ***
100 *** c-basic-offset: 8 ***
101 *** End: ***
102 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-ioread.c b/drivers/media/video/pvrusb2/pvrusb2-ioread.c
new file mode 100644
index 00000000000..bba6115c9ae
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-ioread.c
@@ -0,0 +1,512 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20
21#include "pvrusb2-ioread.h"
22#include "pvrusb2-debug.h"
23#include <linux/errno.h>
24#include <linux/string.h>
25#include <linux/mm.h>
26#include <linux/slab.h>
27#include <linux/mutex.h>
28#include <asm/uaccess.h>
29
30#define BUFFER_COUNT 32
31#define BUFFER_SIZE PAGE_ALIGN(0x4000)
32
33struct pvr2_ioread {
34 struct pvr2_stream *stream;
35 char *buffer_storage[BUFFER_COUNT];
36 char *sync_key_ptr;
37 unsigned int sync_key_len;
38 unsigned int sync_buf_offs;
39 unsigned int sync_state;
40 unsigned int sync_trashed_count;
41 int enabled; // Streaming is on
42 int spigot_open; // OK to pass data to client
43 int stream_running; // Passing data to client now
44
45 /* State relevant to current buffer being read */
46 struct pvr2_buffer *c_buf;
47 char *c_data_ptr;
48 unsigned int c_data_len;
49 unsigned int c_data_offs;
50 struct mutex mutex;
51};
52
53static int pvr2_ioread_init(struct pvr2_ioread *cp)
54{
55 unsigned int idx;
56
57 cp->stream = NULL;
58 mutex_init(&cp->mutex);
59
60 for (idx = 0; idx < BUFFER_COUNT; idx++) {
61 cp->buffer_storage[idx] = kmalloc(BUFFER_SIZE,GFP_KERNEL);
62 if (!(cp->buffer_storage[idx])) break;
63 }
64
65 if (idx < BUFFER_COUNT) {
66 // An allocation appears to have failed
67 for (idx = 0; idx < BUFFER_COUNT; idx++) {
68 if (!(cp->buffer_storage[idx])) continue;
69 kfree(cp->buffer_storage[idx]);
70 }
71 return -ENOMEM;
72 }
73 return 0;
74}
75
76static void pvr2_ioread_done(struct pvr2_ioread *cp)
77{
78 unsigned int idx;
79
80 pvr2_ioread_setup(cp,NULL);
81 for (idx = 0; idx < BUFFER_COUNT; idx++) {
82 if (!(cp->buffer_storage[idx])) continue;
83 kfree(cp->buffer_storage[idx]);
84 }
85}
86
87struct pvr2_ioread *pvr2_ioread_create(void)
88{
89 struct pvr2_ioread *cp;
90 cp = kzalloc(sizeof(*cp),GFP_KERNEL);
91 if (!cp) return NULL;
92 pvr2_trace(PVR2_TRACE_STRUCT,"pvr2_ioread_create id=%p",cp);
93 if (pvr2_ioread_init(cp) < 0) {
94 kfree(cp);
95 return NULL;
96 }
97 return cp;
98}
99
100void pvr2_ioread_destroy(struct pvr2_ioread *cp)
101{
102 if (!cp) return;
103 pvr2_ioread_done(cp);
104 pvr2_trace(PVR2_TRACE_STRUCT,"pvr2_ioread_destroy id=%p",cp);
105 if (cp->sync_key_ptr) {
106 kfree(cp->sync_key_ptr);
107 cp->sync_key_ptr = NULL;
108 }
109 kfree(cp);
110}
111
112void pvr2_ioread_set_sync_key(struct pvr2_ioread *cp,
113 const char *sync_key_ptr,
114 unsigned int sync_key_len)
115{
116 if (!cp) return;
117
118 if (!sync_key_ptr) sync_key_len = 0;
119 if ((sync_key_len == cp->sync_key_len) &&
120 ((!sync_key_len) ||
121 (!memcmp(sync_key_ptr,cp->sync_key_ptr,sync_key_len)))) return;
122
123 if (sync_key_len != cp->sync_key_len) {
124 if (cp->sync_key_ptr) {
125 kfree(cp->sync_key_ptr);
126 cp->sync_key_ptr = NULL;
127 }
128 cp->sync_key_len = 0;
129 if (sync_key_len) {
130 cp->sync_key_ptr = kmalloc(sync_key_len,GFP_KERNEL);
131 if (cp->sync_key_ptr) {
132 cp->sync_key_len = sync_key_len;
133 }
134 }
135 }
136 if (!cp->sync_key_len) return;
137 memcpy(cp->sync_key_ptr,sync_key_ptr,cp->sync_key_len);
138}
139
140static void pvr2_ioread_stop(struct pvr2_ioread *cp)
141{
142 if (!(cp->enabled)) return;
143 pvr2_trace(PVR2_TRACE_START_STOP,
144 "/*---TRACE_READ---*/ pvr2_ioread_stop id=%p",cp);
145 pvr2_stream_kill(cp->stream);
146 cp->c_buf = NULL;
147 cp->c_data_ptr = NULL;
148 cp->c_data_len = 0;
149 cp->c_data_offs = 0;
150 cp->enabled = 0;
151 cp->stream_running = 0;
152 cp->spigot_open = 0;
153 if (cp->sync_state) {
154 pvr2_trace(PVR2_TRACE_DATA_FLOW,
155 "/*---TRACE_READ---*/ sync_state <== 0");
156 cp->sync_state = 0;
157 }
158}
159
160static int pvr2_ioread_start(struct pvr2_ioread *cp)
161{
162 int stat;
163 struct pvr2_buffer *bp;
164 if (cp->enabled) return 0;
165 if (!(cp->stream)) return 0;
166 pvr2_trace(PVR2_TRACE_START_STOP,
167 "/*---TRACE_READ---*/ pvr2_ioread_start id=%p",cp);
168 while ((bp = pvr2_stream_get_idle_buffer(cp->stream)) != NULL) {
169 stat = pvr2_buffer_queue(bp);
170 if (stat < 0) {
171 pvr2_trace(PVR2_TRACE_DATA_FLOW,
172 "/*---TRACE_READ---*/"
173 " pvr2_ioread_start id=%p"
174 " error=%d",
175 cp,stat);
176 pvr2_ioread_stop(cp);
177 return stat;
178 }
179 }
180 cp->enabled = !0;
181 cp->c_buf = NULL;
182 cp->c_data_ptr = NULL;
183 cp->c_data_len = 0;
184 cp->c_data_offs = 0;
185 cp->stream_running = 0;
186 if (cp->sync_key_len) {
187 pvr2_trace(PVR2_TRACE_DATA_FLOW,
188 "/*---TRACE_READ---*/ sync_state <== 1");
189 cp->sync_state = 1;
190 cp->sync_trashed_count = 0;
191 cp->sync_buf_offs = 0;
192 }
193 cp->spigot_open = 0;
194 return 0;
195}
196
197struct pvr2_stream *pvr2_ioread_get_stream(struct pvr2_ioread *cp)
198{
199 return cp->stream;
200}
201
202int pvr2_ioread_setup(struct pvr2_ioread *cp,struct pvr2_stream *sp)
203{
204 int ret;
205 unsigned int idx;
206 struct pvr2_buffer *bp;
207
208 mutex_lock(&cp->mutex); do {
209 if (cp->stream) {
210 pvr2_trace(PVR2_TRACE_START_STOP,
211 "/*---TRACE_READ---*/"
212 " pvr2_ioread_setup (tear-down) id=%p",cp);
213 pvr2_ioread_stop(cp);
214 pvr2_stream_kill(cp->stream);
215 if (pvr2_stream_get_buffer_count(cp->stream)) {
216 pvr2_stream_set_buffer_count(cp->stream,0);
217 }
218 cp->stream = NULL;
219 }
220 if (sp) {
221 pvr2_trace(PVR2_TRACE_START_STOP,
222 "/*---TRACE_READ---*/"
223 " pvr2_ioread_setup (setup) id=%p",cp);
224 pvr2_stream_kill(sp);
225 ret = pvr2_stream_set_buffer_count(sp,BUFFER_COUNT);
226 if (ret < 0) {
227 mutex_unlock(&cp->mutex);
228 return ret;
229 }
230 for (idx = 0; idx < BUFFER_COUNT; idx++) {
231 bp = pvr2_stream_get_buffer(sp,idx);
232 pvr2_buffer_set_buffer(bp,
233 cp->buffer_storage[idx],
234 BUFFER_SIZE);
235 }
236 cp->stream = sp;
237 }
238 } while (0); mutex_unlock(&cp->mutex);
239
240 return 0;
241}
242
243int pvr2_ioread_set_enabled(struct pvr2_ioread *cp,int fl)
244{
245 int ret = 0;
246 if ((!fl) == (!(cp->enabled))) return ret;
247
248 mutex_lock(&cp->mutex); do {
249 if (fl) {
250 ret = pvr2_ioread_start(cp);
251 } else {
252 pvr2_ioread_stop(cp);
253 }
254 } while (0); mutex_unlock(&cp->mutex);
255 return ret;
256}
257
258static int pvr2_ioread_get_buffer(struct pvr2_ioread *cp)
259{
260 int stat;
261
262 while (cp->c_data_len <= cp->c_data_offs) {
263 if (cp->c_buf) {
264 // Flush out current buffer first.
265 stat = pvr2_buffer_queue(cp->c_buf);
266 if (stat < 0) {
267 // Streaming error...
268 pvr2_trace(PVR2_TRACE_DATA_FLOW,
269 "/*---TRACE_READ---*/"
270 " pvr2_ioread_read id=%p"
271 " queue_error=%d",
272 cp,stat);
273 pvr2_ioread_stop(cp);
274 return 0;
275 }
276 cp->c_buf = NULL;
277 cp->c_data_ptr = NULL;
278 cp->c_data_len = 0;
279 cp->c_data_offs = 0;
280 }
281 // Now get a freshly filled buffer.
282 cp->c_buf = pvr2_stream_get_ready_buffer(cp->stream);
283 if (!cp->c_buf) break; // Nothing ready; done.
284 cp->c_data_len = pvr2_buffer_get_count(cp->c_buf);
285 if (!cp->c_data_len) {
286 // Nothing transferred. Was there an error?
287 stat = pvr2_buffer_get_status(cp->c_buf);
288 if (stat < 0) {
289 // Streaming error...
290 pvr2_trace(PVR2_TRACE_DATA_FLOW,
291 "/*---TRACE_READ---*/"
292 " pvr2_ioread_read id=%p"
293 " buffer_error=%d",
294 cp,stat);
295 pvr2_ioread_stop(cp);
296 // Give up.
297 return 0;
298 }
299 // Start over...
300 continue;
301 }
302 cp->c_data_offs = 0;
303 cp->c_data_ptr = cp->buffer_storage[
304 pvr2_buffer_get_id(cp->c_buf)];
305 }
306 return !0;
307}
308
309static void pvr2_ioread_filter(struct pvr2_ioread *cp)
310{
311 unsigned int idx;
312 if (!cp->enabled) return;
313 if (cp->sync_state != 1) return;
314
315 // Search the stream for our synchronization key. This is made
316 // complicated by the fact that in order to be honest with
317 // ourselves here we must search across buffer boundaries...
318 mutex_lock(&cp->mutex); while (1) {
319 // Ensure we have a buffer
320 if (!pvr2_ioread_get_buffer(cp)) break;
321 if (!cp->c_data_len) break;
322
323 // Now walk the buffer contents until we match the key or
324 // run out of buffer data.
325 for (idx = cp->c_data_offs; idx < cp->c_data_len; idx++) {
326 if (cp->sync_buf_offs >= cp->sync_key_len) break;
327 if (cp->c_data_ptr[idx] ==
328 cp->sync_key_ptr[cp->sync_buf_offs]) {
329 // Found the next key byte
330 (cp->sync_buf_offs)++;
331 } else {
332 // Whoops, mismatched. Start key over...
333 cp->sync_buf_offs = 0;
334 }
335 }
336
337 // Consume what we've walked through
338 cp->c_data_offs += idx;
339 cp->sync_trashed_count += idx;
340
341 // If we've found the key, then update state and get out.
342 if (cp->sync_buf_offs >= cp->sync_key_len) {
343 cp->sync_trashed_count -= cp->sync_key_len;
344 pvr2_trace(PVR2_TRACE_DATA_FLOW,
345 "/*---TRACE_READ---*/"
346 " sync_state <== 2 (skipped %u bytes)",
347 cp->sync_trashed_count);
348 cp->sync_state = 2;
349 cp->sync_buf_offs = 0;
350 break;
351 }
352
353 if (cp->c_data_offs < cp->c_data_len) {
354 // Sanity check - should NEVER get here
355 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
356 "ERROR: pvr2_ioread filter sync problem"
357 " len=%u offs=%u",
358 cp->c_data_len,cp->c_data_offs);
359 // Get out so we don't get stuck in an infinite
360 // loop.
361 break;
362 }
363
364 continue; // (for clarity)
365 } mutex_unlock(&cp->mutex);
366}
367
368int pvr2_ioread_avail(struct pvr2_ioread *cp)
369{
370 int ret;
371 if (!(cp->enabled)) {
372 // Stream is not enabled; so this is an I/O error
373 return -EIO;
374 }
375
376 if (cp->sync_state == 1) {
377 pvr2_ioread_filter(cp);
378 if (cp->sync_state == 1) return -EAGAIN;
379 }
380
381 ret = 0;
382 if (cp->stream_running) {
383 if (!pvr2_stream_get_ready_count(cp->stream)) {
384 // No data available at all right now.
385 ret = -EAGAIN;
386 }
387 } else {
388 if (pvr2_stream_get_ready_count(cp->stream) < BUFFER_COUNT/2) {
389 // Haven't buffered up enough yet; try again later
390 ret = -EAGAIN;
391 }
392 }
393
394 if ((!(cp->spigot_open)) != (!(ret == 0))) {
395 cp->spigot_open = (ret == 0);
396 pvr2_trace(PVR2_TRACE_DATA_FLOW,
397 "/*---TRACE_READ---*/ data is %s",
398 cp->spigot_open ? "available" : "pending");
399 }
400
401 return ret;
402}
403
404int pvr2_ioread_read(struct pvr2_ioread *cp,void __user *buf,unsigned int cnt)
405{
406 unsigned int copied_cnt;
407 unsigned int bcnt;
408 const char *src;
409 int stat;
410 int ret = 0;
411 unsigned int req_cnt = cnt;
412
413 if (!cnt) {
414 pvr2_trace(PVR2_TRACE_TRAP,
415 "/*---TRACE_READ---*/ pvr2_ioread_read id=%p"
416 " ZERO Request? Returning zero.",cp);
417 return 0;
418 }
419
420 stat = pvr2_ioread_avail(cp);
421 if (stat < 0) return stat;
422
423 cp->stream_running = !0;
424
425 mutex_lock(&cp->mutex); do {
426
427 // Suck data out of the buffers and copy to the user
428 copied_cnt = 0;
429 if (!buf) cnt = 0;
430 while (1) {
431 if (!pvr2_ioread_get_buffer(cp)) {
432 ret = -EIO;
433 break;
434 }
435
436 if (!cnt) break;
437
438 if (cp->sync_state == 2) {
439 // We're repeating the sync key data into
440 // the stream.
441 src = cp->sync_key_ptr + cp->sync_buf_offs;
442 bcnt = cp->sync_key_len - cp->sync_buf_offs;
443 } else {
444 // Normal buffer copy
445 src = cp->c_data_ptr + cp->c_data_offs;
446 bcnt = cp->c_data_len - cp->c_data_offs;
447 }
448
449 if (!bcnt) break;
450
451 // Don't run past user's buffer
452 if (bcnt > cnt) bcnt = cnt;
453
454 if (copy_to_user(buf,src,bcnt)) {
455 // User supplied a bad pointer?
456 // Give up - this *will* cause data
457 // to be lost.
458 ret = -EFAULT;
459 break;
460 }
461 cnt -= bcnt;
462 buf += bcnt;
463 copied_cnt += bcnt;
464
465 if (cp->sync_state == 2) {
466 // Update offset inside sync key that we're
467 // repeating back out.
468 cp->sync_buf_offs += bcnt;
469 if (cp->sync_buf_offs >= cp->sync_key_len) {
470 // Consumed entire key; switch mode
471 // to normal.
472 pvr2_trace(PVR2_TRACE_DATA_FLOW,
473 "/*---TRACE_READ---*/"
474 " sync_state <== 0");
475 cp->sync_state = 0;
476 }
477 } else {
478 // Update buffer offset.
479 cp->c_data_offs += bcnt;
480 }
481 }
482
483 } while (0); mutex_unlock(&cp->mutex);
484
485 if (!ret) {
486 if (copied_cnt) {
487 // If anything was copied, return that count
488 ret = copied_cnt;
489 } else {
490 // Nothing copied; suggest to caller that another
491 // attempt should be tried again later
492 ret = -EAGAIN;
493 }
494 }
495
496 pvr2_trace(PVR2_TRACE_DATA_FLOW,
497 "/*---TRACE_READ---*/ pvr2_ioread_read"
498 " id=%p request=%d result=%d",
499 cp,req_cnt,ret);
500 return ret;
501}
502
503
504/*
505 Stuff for Emacs to see, in order to encourage consistent editing style:
506 *** Local Variables: ***
507 *** mode: c ***
508 *** fill-column: 75 ***
509 *** tab-width: 8 ***
510 *** c-basic-offset: 8 ***
511 *** End: ***
512 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-ioread.h b/drivers/media/video/pvrusb2/pvrusb2-ioread.h
new file mode 100644
index 00000000000..100e0780e1a
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-ioread.h
@@ -0,0 +1,48 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20#ifndef __PVRUSB2_IOREAD_H
21#define __PVRUSB2_IOREAD_H
22
23#include "pvrusb2-io.h"
24
25struct pvr2_ioread;
26
27struct pvr2_ioread *pvr2_ioread_create(void);
28void pvr2_ioread_destroy(struct pvr2_ioread *);
29int pvr2_ioread_setup(struct pvr2_ioread *,struct pvr2_stream *);
30struct pvr2_stream *pvr2_ioread_get_stream(struct pvr2_ioread *);
31void pvr2_ioread_set_sync_key(struct pvr2_ioread *,
32 const char *sync_key_ptr,
33 unsigned int sync_key_len);
34int pvr2_ioread_set_enabled(struct pvr2_ioread *,int fl);
35int pvr2_ioread_read(struct pvr2_ioread *,void __user *buf,unsigned int cnt);
36int pvr2_ioread_avail(struct pvr2_ioread *);
37
38#endif /* __PVRUSB2_IOREAD_H */
39
40/*
41 Stuff for Emacs to see, in order to encourage consistent editing style:
42 *** Local Variables: ***
43 *** mode: c ***
44 *** fill-column: 75 ***
45 *** tab-width: 8 ***
46 *** c-basic-offset: 8 ***
47 *** End: ***
48 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-main.c b/drivers/media/video/pvrusb2/pvrusb2-main.c
new file mode 100644
index 00000000000..c1d9bb61cd7
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-main.c
@@ -0,0 +1,182 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include <linux/kernel.h>
23#include <linux/errno.h>
24#include <linux/module.h>
25#include <linux/usb.h>
26#include <linux/videodev2.h>
27
28#include "pvrusb2-hdw.h"
29#include "pvrusb2-devattr.h"
30#include "pvrusb2-context.h"
31#include "pvrusb2-debug.h"
32#include "pvrusb2-v4l2.h"
33#ifdef CONFIG_VIDEO_PVRUSB2_SYSFS
34#include "pvrusb2-sysfs.h"
35#endif /* CONFIG_VIDEO_PVRUSB2_SYSFS */
36
37#define DRIVER_AUTHOR "Mike Isely <isely@pobox.com>"
38#define DRIVER_DESC "Hauppauge WinTV-PVR-USB2 MPEG2 Encoder/Tuner"
39#define DRIVER_VERSION "V4L in-tree version"
40
41#define DEFAULT_DEBUG_MASK (PVR2_TRACE_ERROR_LEGS| \
42 PVR2_TRACE_INFO| \
43 PVR2_TRACE_STD| \
44 PVR2_TRACE_TOLERANCE| \
45 PVR2_TRACE_TRAP| \
46 0)
47
48int pvrusb2_debug = DEFAULT_DEBUG_MASK;
49
50module_param_named(debug,pvrusb2_debug,int,S_IRUGO|S_IWUSR);
51MODULE_PARM_DESC(debug, "Debug trace mask");
52
53#ifdef CONFIG_VIDEO_PVRUSB2_SYSFS
54static struct pvr2_sysfs_class *class_ptr = NULL;
55#endif /* CONFIG_VIDEO_PVRUSB2_SYSFS */
56
57static void pvr_setup_attach(struct pvr2_context *pvr)
58{
59 /* Create association with v4l layer */
60 pvr2_v4l2_create(pvr);
61#ifdef CONFIG_VIDEO_PVRUSB2_DVB
62 /* Create association with dvb layer */
63 pvr2_dvb_create(pvr);
64#endif
65#ifdef CONFIG_VIDEO_PVRUSB2_SYSFS
66 pvr2_sysfs_create(pvr,class_ptr);
67#endif /* CONFIG_VIDEO_PVRUSB2_SYSFS */
68}
69
70static int pvr_probe(struct usb_interface *intf,
71 const struct usb_device_id *devid)
72{
73 struct pvr2_context *pvr;
74
75 /* Create underlying hardware interface */
76 pvr = pvr2_context_create(intf,devid,pvr_setup_attach);
77 if (!pvr) {
78 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
79 "Failed to create hdw handler");
80 return -ENOMEM;
81 }
82
83 pvr2_trace(PVR2_TRACE_INIT,"pvr_probe(pvr=%p)",pvr);
84
85 usb_set_intfdata(intf, pvr);
86
87 return 0;
88}
89
90/*
91 * pvr_disconnect()
92 *
93 */
94static void pvr_disconnect(struct usb_interface *intf)
95{
96 struct pvr2_context *pvr = usb_get_intfdata(intf);
97
98 pvr2_trace(PVR2_TRACE_INIT,"pvr_disconnect(pvr=%p) BEGIN",pvr);
99
100 usb_set_intfdata (intf, NULL);
101 pvr2_context_disconnect(pvr);
102
103 pvr2_trace(PVR2_TRACE_INIT,"pvr_disconnect(pvr=%p) DONE",pvr);
104
105}
106
107static struct usb_driver pvr_driver = {
108 .name = "pvrusb2",
109 .id_table = pvr2_device_table,
110 .probe = pvr_probe,
111 .disconnect = pvr_disconnect
112};
113
114/*
115 * pvr_init() / pvr_exit()
116 *
117 * This code is run to initialize/exit the driver.
118 *
119 */
120static int __init pvr_init(void)
121{
122 int ret;
123
124 pvr2_trace(PVR2_TRACE_INIT,"pvr_init");
125
126 ret = pvr2_context_global_init();
127 if (ret != 0) {
128 pvr2_trace(PVR2_TRACE_INIT,"pvr_init failure code=%d",ret);
129 return ret;
130 }
131
132#ifdef CONFIG_VIDEO_PVRUSB2_SYSFS
133 class_ptr = pvr2_sysfs_class_create();
134#endif /* CONFIG_VIDEO_PVRUSB2_SYSFS */
135
136 ret = usb_register(&pvr_driver);
137
138 if (ret == 0)
139 printk(KERN_INFO "pvrusb2: " DRIVER_VERSION ":"
140 DRIVER_DESC "\n");
141 if (pvrusb2_debug)
142 printk(KERN_INFO "pvrusb2: Debug mask is %d (0x%x)\n",
143 pvrusb2_debug,pvrusb2_debug);
144
145 pvr2_trace(PVR2_TRACE_INIT,"pvr_init complete");
146
147 return ret;
148}
149
150static void __exit pvr_exit(void)
151{
152 pvr2_trace(PVR2_TRACE_INIT,"pvr_exit");
153
154 usb_deregister(&pvr_driver);
155
156 pvr2_context_global_done();
157
158#ifdef CONFIG_VIDEO_PVRUSB2_SYSFS
159 pvr2_sysfs_class_destroy(class_ptr);
160#endif /* CONFIG_VIDEO_PVRUSB2_SYSFS */
161
162 pvr2_trace(PVR2_TRACE_INIT,"pvr_exit complete");
163}
164
165module_init(pvr_init);
166module_exit(pvr_exit);
167
168MODULE_AUTHOR(DRIVER_AUTHOR);
169MODULE_DESCRIPTION(DRIVER_DESC);
170MODULE_LICENSE("GPL");
171MODULE_VERSION("0.9.1");
172
173
174/*
175 Stuff for Emacs to see, in order to encourage consistent editing style:
176 *** Local Variables: ***
177 *** mode: c ***
178 *** fill-column: 70 ***
179 *** tab-width: 8 ***
180 *** c-basic-offset: 8 ***
181 *** End: ***
182 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-std.c b/drivers/media/video/pvrusb2/pvrusb2-std.c
new file mode 100644
index 00000000000..453627b0783
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-std.c
@@ -0,0 +1,411 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20
21#include "pvrusb2-std.h"
22#include "pvrusb2-debug.h"
23#include <asm/string.h>
24#include <linux/slab.h>
25
26struct std_name {
27 const char *name;
28 v4l2_std_id id;
29};
30
31
32#define CSTD_PAL \
33 (V4L2_STD_PAL_B| \
34 V4L2_STD_PAL_B1| \
35 V4L2_STD_PAL_G| \
36 V4L2_STD_PAL_H| \
37 V4L2_STD_PAL_I| \
38 V4L2_STD_PAL_D| \
39 V4L2_STD_PAL_D1| \
40 V4L2_STD_PAL_K| \
41 V4L2_STD_PAL_M| \
42 V4L2_STD_PAL_N| \
43 V4L2_STD_PAL_Nc| \
44 V4L2_STD_PAL_60)
45
46#define CSTD_NTSC \
47 (V4L2_STD_NTSC_M| \
48 V4L2_STD_NTSC_M_JP| \
49 V4L2_STD_NTSC_M_KR| \
50 V4L2_STD_NTSC_443)
51
52#define CSTD_ATSC \
53 (V4L2_STD_ATSC_8_VSB| \
54 V4L2_STD_ATSC_16_VSB)
55
56#define CSTD_SECAM \
57 (V4L2_STD_SECAM_B| \
58 V4L2_STD_SECAM_D| \
59 V4L2_STD_SECAM_G| \
60 V4L2_STD_SECAM_H| \
61 V4L2_STD_SECAM_K| \
62 V4L2_STD_SECAM_K1| \
63 V4L2_STD_SECAM_L| \
64 V4L2_STD_SECAM_LC)
65
66#define TSTD_B (V4L2_STD_PAL_B|V4L2_STD_SECAM_B)
67#define TSTD_B1 (V4L2_STD_PAL_B1)
68#define TSTD_D (V4L2_STD_PAL_D|V4L2_STD_SECAM_D)
69#define TSTD_D1 (V4L2_STD_PAL_D1)
70#define TSTD_G (V4L2_STD_PAL_G|V4L2_STD_SECAM_G)
71#define TSTD_H (V4L2_STD_PAL_H|V4L2_STD_SECAM_H)
72#define TSTD_I (V4L2_STD_PAL_I)
73#define TSTD_K (V4L2_STD_PAL_K|V4L2_STD_SECAM_K)
74#define TSTD_K1 (V4L2_STD_SECAM_K1)
75#define TSTD_L (V4L2_STD_SECAM_L)
76#define TSTD_M (V4L2_STD_PAL_M|V4L2_STD_NTSC_M)
77#define TSTD_N (V4L2_STD_PAL_N)
78#define TSTD_Nc (V4L2_STD_PAL_Nc)
79#define TSTD_60 (V4L2_STD_PAL_60)
80
81#define CSTD_ALL (CSTD_PAL|CSTD_NTSC|CSTD_ATSC|CSTD_SECAM)
82
83/* Mapping of standard bits to color system */
84static const struct std_name std_groups[] = {
85 {"PAL",CSTD_PAL},
86 {"NTSC",CSTD_NTSC},
87 {"SECAM",CSTD_SECAM},
88 {"ATSC",CSTD_ATSC},
89};
90
91/* Mapping of standard bits to modulation system */
92static const struct std_name std_items[] = {
93 {"B",TSTD_B},
94 {"B1",TSTD_B1},
95 {"D",TSTD_D},
96 {"D1",TSTD_D1},
97 {"G",TSTD_G},
98 {"H",TSTD_H},
99 {"I",TSTD_I},
100 {"K",TSTD_K},
101 {"K1",TSTD_K1},
102 {"L",TSTD_L},
103 {"LC",V4L2_STD_SECAM_LC},
104 {"M",TSTD_M},
105 {"Mj",V4L2_STD_NTSC_M_JP},
106 {"443",V4L2_STD_NTSC_443},
107 {"Mk",V4L2_STD_NTSC_M_KR},
108 {"N",TSTD_N},
109 {"Nc",TSTD_Nc},
110 {"60",TSTD_60},
111 {"8VSB",V4L2_STD_ATSC_8_VSB},
112 {"16VSB",V4L2_STD_ATSC_16_VSB},
113};
114
115
116// Search an array of std_name structures and return a pointer to the
117// element with the matching name.
118static const struct std_name *find_std_name(const struct std_name *arrPtr,
119 unsigned int arrSize,
120 const char *bufPtr,
121 unsigned int bufSize)
122{
123 unsigned int idx;
124 const struct std_name *p;
125 for (idx = 0; idx < arrSize; idx++) {
126 p = arrPtr + idx;
127 if (strlen(p->name) != bufSize) continue;
128 if (!memcmp(bufPtr,p->name,bufSize)) return p;
129 }
130 return NULL;
131}
132
133
134int pvr2_std_str_to_id(v4l2_std_id *idPtr,const char *bufPtr,
135 unsigned int bufSize)
136{
137 v4l2_std_id id = 0;
138 v4l2_std_id cmsk = 0;
139 v4l2_std_id t;
140 int mMode = 0;
141 unsigned int cnt;
142 char ch;
143 const struct std_name *sp;
144
145 while (bufSize) {
146 if (!mMode) {
147 cnt = 0;
148 while ((cnt < bufSize) && (bufPtr[cnt] != '-')) cnt++;
149 if (cnt >= bufSize) return 0; // No more characters
150 sp = find_std_name(std_groups, ARRAY_SIZE(std_groups),
151 bufPtr,cnt);
152 if (!sp) return 0; // Illegal color system name
153 cnt++;
154 bufPtr += cnt;
155 bufSize -= cnt;
156 mMode = !0;
157 cmsk = sp->id;
158 continue;
159 }
160 cnt = 0;
161 while (cnt < bufSize) {
162 ch = bufPtr[cnt];
163 if (ch == ';') {
164 mMode = 0;
165 break;
166 }
167 if (ch == '/') break;
168 cnt++;
169 }
170 sp = find_std_name(std_items, ARRAY_SIZE(std_items),
171 bufPtr,cnt);
172 if (!sp) return 0; // Illegal modulation system ID
173 t = sp->id & cmsk;
174 if (!t) return 0; // Specific color + modulation system illegal
175 id |= t;
176 if (cnt < bufSize) cnt++;
177 bufPtr += cnt;
178 bufSize -= cnt;
179 }
180
181 if (idPtr) *idPtr = id;
182 return !0;
183}
184
185
186unsigned int pvr2_std_id_to_str(char *bufPtr, unsigned int bufSize,
187 v4l2_std_id id)
188{
189 unsigned int idx1,idx2;
190 const struct std_name *ip,*gp;
191 int gfl,cfl;
192 unsigned int c1,c2;
193 cfl = 0;
194 c1 = 0;
195 for (idx1 = 0; idx1 < ARRAY_SIZE(std_groups); idx1++) {
196 gp = std_groups + idx1;
197 gfl = 0;
198 for (idx2 = 0; idx2 < ARRAY_SIZE(std_items); idx2++) {
199 ip = std_items + idx2;
200 if (!(gp->id & ip->id & id)) continue;
201 if (!gfl) {
202 if (cfl) {
203 c2 = scnprintf(bufPtr,bufSize,";");
204 c1 += c2;
205 bufSize -= c2;
206 bufPtr += c2;
207 }
208 cfl = !0;
209 c2 = scnprintf(bufPtr,bufSize,
210 "%s-",gp->name);
211 gfl = !0;
212 } else {
213 c2 = scnprintf(bufPtr,bufSize,"/");
214 }
215 c1 += c2;
216 bufSize -= c2;
217 bufPtr += c2;
218 c2 = scnprintf(bufPtr,bufSize,
219 ip->name);
220 c1 += c2;
221 bufSize -= c2;
222 bufPtr += c2;
223 }
224 }
225 return c1;
226}
227
228
229// Template data for possible enumerated video standards. Here we group
230// standards which share common frame rates and resolution.
231static struct v4l2_standard generic_standards[] = {
232 {
233 .id = (TSTD_B|TSTD_B1|
234 TSTD_D|TSTD_D1|
235 TSTD_G|
236 TSTD_H|
237 TSTD_I|
238 TSTD_K|TSTD_K1|
239 TSTD_L|
240 V4L2_STD_SECAM_LC |
241 TSTD_N|TSTD_Nc),
242 .frameperiod =
243 {
244 .numerator = 1,
245 .denominator= 25
246 },
247 .framelines = 625,
248 .reserved = {0,0,0,0}
249 }, {
250 .id = (TSTD_M|
251 V4L2_STD_NTSC_M_JP|
252 V4L2_STD_NTSC_M_KR),
253 .frameperiod =
254 {
255 .numerator = 1001,
256 .denominator= 30000
257 },
258 .framelines = 525,
259 .reserved = {0,0,0,0}
260 }, { // This is a total wild guess
261 .id = (TSTD_60),
262 .frameperiod =
263 {
264 .numerator = 1001,
265 .denominator= 30000
266 },
267 .framelines = 525,
268 .reserved = {0,0,0,0}
269 }, { // This is total wild guess
270 .id = V4L2_STD_NTSC_443,
271 .frameperiod =
272 {
273 .numerator = 1001,
274 .denominator= 30000
275 },
276 .framelines = 525,
277 .reserved = {0,0,0,0}
278 }
279};
280
281static struct v4l2_standard *match_std(v4l2_std_id id)
282{
283 unsigned int idx;
284 for (idx = 0; idx < ARRAY_SIZE(generic_standards); idx++) {
285 if (generic_standards[idx].id & id) {
286 return generic_standards + idx;
287 }
288 }
289 return NULL;
290}
291
292static int pvr2_std_fill(struct v4l2_standard *std,v4l2_std_id id)
293{
294 struct v4l2_standard *template;
295 int idx;
296 unsigned int bcnt;
297 template = match_std(id);
298 if (!template) return 0;
299 idx = std->index;
300 memcpy(std,template,sizeof(*template));
301 std->index = idx;
302 std->id = id;
303 bcnt = pvr2_std_id_to_str(std->name,sizeof(std->name)-1,id);
304 std->name[bcnt] = 0;
305 pvr2_trace(PVR2_TRACE_STD,"Set up standard idx=%u name=%s",
306 std->index,std->name);
307 return !0;
308}
309
310/* These are special cases of combined standards that we should enumerate
311 separately if the component pieces are present. */
312static v4l2_std_id std_mixes[] = {
313 V4L2_STD_PAL_B | V4L2_STD_PAL_G,
314 V4L2_STD_PAL_D | V4L2_STD_PAL_K,
315 V4L2_STD_SECAM_B | V4L2_STD_SECAM_G,
316 V4L2_STD_SECAM_D | V4L2_STD_SECAM_K,
317};
318
319struct v4l2_standard *pvr2_std_create_enum(unsigned int *countptr,
320 v4l2_std_id id)
321{
322 unsigned int std_cnt = 0;
323 unsigned int idx,bcnt,idx2;
324 v4l2_std_id idmsk,cmsk,fmsk;
325 struct v4l2_standard *stddefs;
326
327 if (pvrusb2_debug & PVR2_TRACE_STD) {
328 char buf[100];
329 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),id);
330 pvr2_trace(
331 PVR2_TRACE_STD,"Mapping standards mask=0x%x (%.*s)",
332 (int)id,bcnt,buf);
333 }
334
335 *countptr = 0;
336 std_cnt = 0;
337 fmsk = 0;
338 for (idmsk = 1, cmsk = id; cmsk; idmsk <<= 1) {
339 if (!(idmsk & cmsk)) continue;
340 cmsk &= ~idmsk;
341 if (match_std(idmsk)) {
342 std_cnt++;
343 continue;
344 }
345 fmsk |= idmsk;
346 }
347
348 for (idx2 = 0; idx2 < ARRAY_SIZE(std_mixes); idx2++) {
349 if ((id & std_mixes[idx2]) == std_mixes[idx2]) std_cnt++;
350 }
351
352 /* Don't complain about ATSC standard values */
353 fmsk &= ~CSTD_ATSC;
354
355 if (fmsk) {
356 char buf[100];
357 bcnt = pvr2_std_id_to_str(buf,sizeof(buf),fmsk);
358 pvr2_trace(
359 PVR2_TRACE_ERROR_LEGS,
360 "WARNING:"
361 " Failed to classify the following standard(s): %.*s",
362 bcnt,buf);
363 }
364
365 pvr2_trace(PVR2_TRACE_STD,"Setting up %u unique standard(s)",
366 std_cnt);
367 if (!std_cnt) return NULL; // paranoia
368
369 stddefs = kzalloc(sizeof(struct v4l2_standard) * std_cnt,
370 GFP_KERNEL);
371 if (!stddefs)
372 return NULL;
373
374 for (idx = 0; idx < std_cnt; idx++)
375 stddefs[idx].index = idx;
376
377 idx = 0;
378
379 /* Enumerate potential special cases */
380 for (idx2 = 0; (idx2 < ARRAY_SIZE(std_mixes)) && (idx < std_cnt);
381 idx2++) {
382 if (!(id & std_mixes[idx2])) continue;
383 if (pvr2_std_fill(stddefs+idx,std_mixes[idx2])) idx++;
384 }
385 /* Now enumerate individual pieces */
386 for (idmsk = 1, cmsk = id; cmsk && (idx < std_cnt); idmsk <<= 1) {
387 if (!(idmsk & cmsk)) continue;
388 cmsk &= ~idmsk;
389 if (!pvr2_std_fill(stddefs+idx,idmsk)) continue;
390 idx++;
391 }
392
393 *countptr = std_cnt;
394 return stddefs;
395}
396
397v4l2_std_id pvr2_std_get_usable(void)
398{
399 return CSTD_ALL;
400}
401
402
403/*
404 Stuff for Emacs to see, in order to encourage consistent editing style:
405 *** Local Variables: ***
406 *** mode: c ***
407 *** fill-column: 75 ***
408 *** tab-width: 8 ***
409 *** c-basic-offset: 8 ***
410 *** End: ***
411 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-std.h b/drivers/media/video/pvrusb2/pvrusb2-std.h
new file mode 100644
index 00000000000..a35c53d0b32
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-std.h
@@ -0,0 +1,59 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20#ifndef __PVRUSB2_STD_H
21#define __PVRUSB2_STD_H
22
23#include <linux/videodev2.h>
24
25// Convert string describing one or more video standards into a mask of V4L
26// standard bits. Return true if conversion succeeds otherwise return
27// false. String is expected to be of the form: C1-x/y;C2-a/b where C1 and
28// C2 are color system names (e.g. "PAL", "NTSC") and x, y, a, and b are
29// modulation schemes (e.g. "M", "B", "G", etc).
30int pvr2_std_str_to_id(v4l2_std_id *idPtr,const char *bufPtr,
31 unsigned int bufSize);
32
33// Convert any arbitrary set of video standard bits into an unambiguous
34// readable string. Return value is the number of bytes consumed in the
35// buffer. The formatted string is of a form that can be parsed by our
36// sibling std_std_to_id() function.
37unsigned int pvr2_std_id_to_str(char *bufPtr, unsigned int bufSize,
38 v4l2_std_id id);
39
40// Create an array of suitable v4l2_standard structures given a bit mask of
41// video standards to support. The array is allocated from the heap, and
42// the number of elements is returned in the first argument.
43struct v4l2_standard *pvr2_std_create_enum(unsigned int *countptr,
44 v4l2_std_id id);
45
46// Return mask of which video standard bits are valid
47v4l2_std_id pvr2_std_get_usable(void);
48
49#endif /* __PVRUSB2_STD_H */
50
51/*
52 Stuff for Emacs to see, in order to encourage consistent editing style:
53 *** Local Variables: ***
54 *** mode: c ***
55 *** fill-column: 75 ***
56 *** tab-width: 8 ***
57 *** c-basic-offset: 8 ***
58 *** End: ***
59 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-sysfs.c b/drivers/media/video/pvrusb2/pvrusb2-sysfs.c
new file mode 100644
index 00000000000..6ef1335b285
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-sysfs.c
@@ -0,0 +1,861 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20
21#include <linux/string.h>
22#include <linux/slab.h>
23#include "pvrusb2-sysfs.h"
24#include "pvrusb2-hdw.h"
25#include "pvrusb2-debug.h"
26#ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
27#include "pvrusb2-debugifc.h"
28#endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
29
30#define pvr2_sysfs_trace(...) pvr2_trace(PVR2_TRACE_SYSFS,__VA_ARGS__)
31
32struct pvr2_sysfs {
33 struct pvr2_channel channel;
34 struct device *class_dev;
35#ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
36 struct pvr2_sysfs_debugifc *debugifc;
37#endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
38 struct pvr2_sysfs_ctl_item *item_first;
39 struct pvr2_sysfs_ctl_item *item_last;
40 struct device_attribute attr_v4l_minor_number;
41 struct device_attribute attr_v4l_radio_minor_number;
42 struct device_attribute attr_unit_number;
43 struct device_attribute attr_bus_info;
44 struct device_attribute attr_hdw_name;
45 struct device_attribute attr_hdw_desc;
46 int v4l_minor_number_created_ok;
47 int v4l_radio_minor_number_created_ok;
48 int unit_number_created_ok;
49 int bus_info_created_ok;
50 int hdw_name_created_ok;
51 int hdw_desc_created_ok;
52};
53
54#ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
55struct pvr2_sysfs_debugifc {
56 struct device_attribute attr_debugcmd;
57 struct device_attribute attr_debuginfo;
58 int debugcmd_created_ok;
59 int debuginfo_created_ok;
60};
61#endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
62
63struct pvr2_sysfs_ctl_item {
64 struct device_attribute attr_name;
65 struct device_attribute attr_type;
66 struct device_attribute attr_min;
67 struct device_attribute attr_max;
68 struct device_attribute attr_def;
69 struct device_attribute attr_enum;
70 struct device_attribute attr_bits;
71 struct device_attribute attr_val;
72 struct device_attribute attr_custom;
73 struct pvr2_ctrl *cptr;
74 int ctl_id;
75 struct pvr2_sysfs *chptr;
76 struct pvr2_sysfs_ctl_item *item_next;
77 struct attribute *attr_gen[8];
78 struct attribute_group grp;
79 int created_ok;
80 char name[80];
81};
82
83struct pvr2_sysfs_class {
84 struct class class;
85};
86
87static ssize_t show_name(struct device *class_dev,
88 struct device_attribute *attr,
89 char *buf)
90{
91 struct pvr2_sysfs_ctl_item *cip;
92 const char *name;
93 cip = container_of(attr, struct pvr2_sysfs_ctl_item, attr_name);
94 name = pvr2_ctrl_get_desc(cip->cptr);
95 pvr2_sysfs_trace("pvr2_sysfs(%p) show_name(cid=%d) is %s",
96 cip->chptr, cip->ctl_id, name);
97 if (!name) return -EINVAL;
98 return scnprintf(buf, PAGE_SIZE, "%s\n", name);
99}
100
101static ssize_t show_type(struct device *class_dev,
102 struct device_attribute *attr,
103 char *buf)
104{
105 struct pvr2_sysfs_ctl_item *cip;
106 const char *name;
107 enum pvr2_ctl_type tp;
108 cip = container_of(attr, struct pvr2_sysfs_ctl_item, attr_type);
109 tp = pvr2_ctrl_get_type(cip->cptr);
110 switch (tp) {
111 case pvr2_ctl_int: name = "integer"; break;
112 case pvr2_ctl_enum: name = "enum"; break;
113 case pvr2_ctl_bitmask: name = "bitmask"; break;
114 case pvr2_ctl_bool: name = "boolean"; break;
115 default: name = "?"; break;
116 }
117 pvr2_sysfs_trace("pvr2_sysfs(%p) show_type(cid=%d) is %s",
118 cip->chptr, cip->ctl_id, name);
119 if (!name) return -EINVAL;
120 return scnprintf(buf, PAGE_SIZE, "%s\n", name);
121}
122
123static ssize_t show_min(struct device *class_dev,
124 struct device_attribute *attr,
125 char *buf)
126{
127 struct pvr2_sysfs_ctl_item *cip;
128 long val;
129 cip = container_of(attr, struct pvr2_sysfs_ctl_item, attr_min);
130 val = pvr2_ctrl_get_min(cip->cptr);
131 pvr2_sysfs_trace("pvr2_sysfs(%p) show_min(cid=%d) is %ld",
132 cip->chptr, cip->ctl_id, val);
133 return scnprintf(buf, PAGE_SIZE, "%ld\n", val);
134}
135
136static ssize_t show_max(struct device *class_dev,
137 struct device_attribute *attr,
138 char *buf)
139{
140 struct pvr2_sysfs_ctl_item *cip;
141 long val;
142 cip = container_of(attr, struct pvr2_sysfs_ctl_item, attr_max);
143 val = pvr2_ctrl_get_max(cip->cptr);
144 pvr2_sysfs_trace("pvr2_sysfs(%p) show_max(cid=%d) is %ld",
145 cip->chptr, cip->ctl_id, val);
146 return scnprintf(buf, PAGE_SIZE, "%ld\n", val);
147}
148
149static ssize_t show_def(struct device *class_dev,
150 struct device_attribute *attr,
151 char *buf)
152{
153 struct pvr2_sysfs_ctl_item *cip;
154 int val;
155 int ret;
156 unsigned int cnt = 0;
157 cip = container_of(attr, struct pvr2_sysfs_ctl_item, attr_def);
158 ret = pvr2_ctrl_get_def(cip->cptr, &val);
159 if (ret < 0) return ret;
160 ret = pvr2_ctrl_value_to_sym(cip->cptr, ~0, val,
161 buf, PAGE_SIZE - 1, &cnt);
162 pvr2_sysfs_trace("pvr2_sysfs(%p) show_def(cid=%d) is %.*s (%d)",
163 cip->chptr, cip->ctl_id, cnt, buf, val);
164 buf[cnt] = '\n';
165 return cnt + 1;
166}
167
168static ssize_t show_val_norm(struct device *class_dev,
169 struct device_attribute *attr,
170 char *buf)
171{
172 struct pvr2_sysfs_ctl_item *cip;
173 int val;
174 int ret;
175 unsigned int cnt = 0;
176 cip = container_of(attr, struct pvr2_sysfs_ctl_item, attr_val);
177 ret = pvr2_ctrl_get_value(cip->cptr, &val);
178 if (ret < 0) return ret;
179 ret = pvr2_ctrl_value_to_sym(cip->cptr, ~0, val,
180 buf, PAGE_SIZE - 1, &cnt);
181 pvr2_sysfs_trace("pvr2_sysfs(%p) show_val_norm(cid=%d) is %.*s (%d)",
182 cip->chptr, cip->ctl_id, cnt, buf, val);
183 buf[cnt] = '\n';
184 return cnt+1;
185}
186
187static ssize_t show_val_custom(struct device *class_dev,
188 struct device_attribute *attr,
189 char *buf)
190{
191 struct pvr2_sysfs_ctl_item *cip;
192 int val;
193 int ret;
194 unsigned int cnt = 0;
195 cip = container_of(attr, struct pvr2_sysfs_ctl_item, attr_custom);
196 ret = pvr2_ctrl_get_value(cip->cptr, &val);
197 if (ret < 0) return ret;
198 ret = pvr2_ctrl_custom_value_to_sym(cip->cptr, ~0, val,
199 buf, PAGE_SIZE - 1, &cnt);
200 pvr2_sysfs_trace("pvr2_sysfs(%p) show_val_custom(cid=%d) is %.*s (%d)",
201 cip->chptr, cip->ctl_id, cnt, buf, val);
202 buf[cnt] = '\n';
203 return cnt+1;
204}
205
206static ssize_t show_enum(struct device *class_dev,
207 struct device_attribute *attr,
208 char *buf)
209{
210 struct pvr2_sysfs_ctl_item *cip;
211 long val;
212 unsigned int bcnt, ccnt, ecnt;
213 cip = container_of(attr, struct pvr2_sysfs_ctl_item, attr_enum);
214 ecnt = pvr2_ctrl_get_cnt(cip->cptr);
215 bcnt = 0;
216 for (val = 0; val < ecnt; val++) {
217 pvr2_ctrl_get_valname(cip->cptr, val, buf + bcnt,
218 PAGE_SIZE - bcnt, &ccnt);
219 if (!ccnt) continue;
220 bcnt += ccnt;
221 if (bcnt >= PAGE_SIZE) break;
222 buf[bcnt] = '\n';
223 bcnt++;
224 }
225 pvr2_sysfs_trace("pvr2_sysfs(%p) show_enum(cid=%d)",
226 cip->chptr, cip->ctl_id);
227 return bcnt;
228}
229
230static ssize_t show_bits(struct device *class_dev,
231 struct device_attribute *attr,
232 char *buf)
233{
234 struct pvr2_sysfs_ctl_item *cip;
235 int valid_bits, msk;
236 unsigned int bcnt, ccnt;
237 cip = container_of(attr, struct pvr2_sysfs_ctl_item, attr_bits);
238 valid_bits = pvr2_ctrl_get_mask(cip->cptr);
239 bcnt = 0;
240 for (msk = 1; valid_bits; msk <<= 1) {
241 if (!(msk & valid_bits)) continue;
242 valid_bits &= ~msk;
243 pvr2_ctrl_get_valname(cip->cptr, msk, buf + bcnt,
244 PAGE_SIZE - bcnt, &ccnt);
245 bcnt += ccnt;
246 if (bcnt >= PAGE_SIZE) break;
247 buf[bcnt] = '\n';
248 bcnt++;
249 }
250 pvr2_sysfs_trace("pvr2_sysfs(%p) show_bits(cid=%d)",
251 cip->chptr, cip->ctl_id);
252 return bcnt;
253}
254
255static int store_val_any(struct pvr2_sysfs_ctl_item *cip, int customfl,
256 const char *buf,unsigned int count)
257{
258 int ret;
259 int mask,val;
260 if (customfl) {
261 ret = pvr2_ctrl_custom_sym_to_value(cip->cptr, buf, count,
262 &mask, &val);
263 } else {
264 ret = pvr2_ctrl_sym_to_value(cip->cptr, buf, count,
265 &mask, &val);
266 }
267 if (ret < 0) return ret;
268 ret = pvr2_ctrl_set_mask_value(cip->cptr, mask, val);
269 pvr2_hdw_commit_ctl(cip->chptr->channel.hdw);
270 return ret;
271}
272
273static ssize_t store_val_norm(struct device *class_dev,
274 struct device_attribute *attr,
275 const char *buf, size_t count)
276{
277 struct pvr2_sysfs_ctl_item *cip;
278 int ret;
279 cip = container_of(attr, struct pvr2_sysfs_ctl_item, attr_val);
280 pvr2_sysfs_trace("pvr2_sysfs(%p) store_val_norm(cid=%d) \"%.*s\"",
281 cip->chptr, cip->ctl_id, (int)count, buf);
282 ret = store_val_any(cip, 0, buf, count);
283 if (!ret) ret = count;
284 return ret;
285}
286
287static ssize_t store_val_custom(struct device *class_dev,
288 struct device_attribute *attr,
289 const char *buf, size_t count)
290{
291 struct pvr2_sysfs_ctl_item *cip;
292 int ret;
293 cip = container_of(attr, struct pvr2_sysfs_ctl_item, attr_custom);
294 pvr2_sysfs_trace("pvr2_sysfs(%p) store_val_custom(cid=%d) \"%.*s\"",
295 cip->chptr, cip->ctl_id, (int)count, buf);
296 ret = store_val_any(cip, 1, buf, count);
297 if (!ret) ret = count;
298 return ret;
299}
300
301static void pvr2_sysfs_add_control(struct pvr2_sysfs *sfp,int ctl_id)
302{
303 struct pvr2_sysfs_ctl_item *cip;
304 struct pvr2_ctrl *cptr;
305 unsigned int cnt,acnt;
306 int ret;
307
308 cptr = pvr2_hdw_get_ctrl_by_index(sfp->channel.hdw,ctl_id);
309 if (!cptr) return;
310
311 cip = kzalloc(sizeof(*cip),GFP_KERNEL);
312 if (!cip) return;
313 pvr2_sysfs_trace("Creating pvr2_sysfs_ctl_item id=%p",cip);
314
315 cip->cptr = cptr;
316 cip->ctl_id = ctl_id;
317
318 cip->chptr = sfp;
319 cip->item_next = NULL;
320 if (sfp->item_last) {
321 sfp->item_last->item_next = cip;
322 } else {
323 sfp->item_first = cip;
324 }
325 sfp->item_last = cip;
326
327 sysfs_attr_init(&cip->attr_name.attr);
328 cip->attr_name.attr.name = "name";
329 cip->attr_name.attr.mode = S_IRUGO;
330 cip->attr_name.show = show_name;
331
332 sysfs_attr_init(&cip->attr_type.attr);
333 cip->attr_type.attr.name = "type";
334 cip->attr_type.attr.mode = S_IRUGO;
335 cip->attr_type.show = show_type;
336
337 sysfs_attr_init(&cip->attr_min.attr);
338 cip->attr_min.attr.name = "min_val";
339 cip->attr_min.attr.mode = S_IRUGO;
340 cip->attr_min.show = show_min;
341
342 sysfs_attr_init(&cip->attr_max.attr);
343 cip->attr_max.attr.name = "max_val";
344 cip->attr_max.attr.mode = S_IRUGO;
345 cip->attr_max.show = show_max;
346
347 sysfs_attr_init(&cip->attr_def.attr);
348 cip->attr_def.attr.name = "def_val";
349 cip->attr_def.attr.mode = S_IRUGO;
350 cip->attr_def.show = show_def;
351
352 sysfs_attr_init(&cip->attr_val.attr);
353 cip->attr_val.attr.name = "cur_val";
354 cip->attr_val.attr.mode = S_IRUGO;
355
356 sysfs_attr_init(&cip->attr_custom.attr);
357 cip->attr_custom.attr.name = "custom_val";
358 cip->attr_custom.attr.mode = S_IRUGO;
359
360 sysfs_attr_init(&cip->attr_enum.attr);
361 cip->attr_enum.attr.name = "enum_val";
362 cip->attr_enum.attr.mode = S_IRUGO;
363 cip->attr_enum.show = show_enum;
364
365 sysfs_attr_init(&cip->attr_bits.attr);
366 cip->attr_bits.attr.name = "bit_val";
367 cip->attr_bits.attr.mode = S_IRUGO;
368 cip->attr_bits.show = show_bits;
369
370 if (pvr2_ctrl_is_writable(cptr)) {
371 cip->attr_val.attr.mode |= S_IWUSR|S_IWGRP;
372 cip->attr_custom.attr.mode |= S_IWUSR|S_IWGRP;
373 }
374
375 acnt = 0;
376 cip->attr_gen[acnt++] = &cip->attr_name.attr;
377 cip->attr_gen[acnt++] = &cip->attr_type.attr;
378 cip->attr_gen[acnt++] = &cip->attr_val.attr;
379 cip->attr_gen[acnt++] = &cip->attr_def.attr;
380 cip->attr_val.show = show_val_norm;
381 cip->attr_val.store = store_val_norm;
382 if (pvr2_ctrl_has_custom_symbols(cptr)) {
383 cip->attr_gen[acnt++] = &cip->attr_custom.attr;
384 cip->attr_custom.show = show_val_custom;
385 cip->attr_custom.store = store_val_custom;
386 }
387 switch (pvr2_ctrl_get_type(cptr)) {
388 case pvr2_ctl_enum:
389 // Control is an enumeration
390 cip->attr_gen[acnt++] = &cip->attr_enum.attr;
391 break;
392 case pvr2_ctl_int:
393 // Control is an integer
394 cip->attr_gen[acnt++] = &cip->attr_min.attr;
395 cip->attr_gen[acnt++] = &cip->attr_max.attr;
396 break;
397 case pvr2_ctl_bitmask:
398 // Control is an bitmask
399 cip->attr_gen[acnt++] = &cip->attr_bits.attr;
400 break;
401 default: break;
402 }
403
404 cnt = scnprintf(cip->name,sizeof(cip->name)-1,"ctl_%s",
405 pvr2_ctrl_get_name(cptr));
406 cip->name[cnt] = 0;
407 cip->grp.name = cip->name;
408 cip->grp.attrs = cip->attr_gen;
409
410 ret = sysfs_create_group(&sfp->class_dev->kobj,&cip->grp);
411 if (ret) {
412 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
413 "sysfs_create_group error: %d",
414 ret);
415 return;
416 }
417 cip->created_ok = !0;
418}
419
420#ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
421static ssize_t debuginfo_show(struct device *, struct device_attribute *,
422 char *);
423static ssize_t debugcmd_show(struct device *, struct device_attribute *,
424 char *);
425static ssize_t debugcmd_store(struct device *, struct device_attribute *,
426 const char *, size_t count);
427
428static void pvr2_sysfs_add_debugifc(struct pvr2_sysfs *sfp)
429{
430 struct pvr2_sysfs_debugifc *dip;
431 int ret;
432
433 dip = kzalloc(sizeof(*dip),GFP_KERNEL);
434 if (!dip) return;
435 sysfs_attr_init(&dip->attr_debugcmd.attr);
436 dip->attr_debugcmd.attr.name = "debugcmd";
437 dip->attr_debugcmd.attr.mode = S_IRUGO|S_IWUSR|S_IWGRP;
438 dip->attr_debugcmd.show = debugcmd_show;
439 dip->attr_debugcmd.store = debugcmd_store;
440 sysfs_attr_init(&dip->attr_debuginfo.attr);
441 dip->attr_debuginfo.attr.name = "debuginfo";
442 dip->attr_debuginfo.attr.mode = S_IRUGO;
443 dip->attr_debuginfo.show = debuginfo_show;
444 sfp->debugifc = dip;
445 ret = device_create_file(sfp->class_dev,&dip->attr_debugcmd);
446 if (ret < 0) {
447 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
448 "device_create_file error: %d",
449 ret);
450 } else {
451 dip->debugcmd_created_ok = !0;
452 }
453 ret = device_create_file(sfp->class_dev,&dip->attr_debuginfo);
454 if (ret < 0) {
455 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
456 "device_create_file error: %d",
457 ret);
458 } else {
459 dip->debuginfo_created_ok = !0;
460 }
461}
462
463
464static void pvr2_sysfs_tear_down_debugifc(struct pvr2_sysfs *sfp)
465{
466 if (!sfp->debugifc) return;
467 if (sfp->debugifc->debuginfo_created_ok) {
468 device_remove_file(sfp->class_dev,
469 &sfp->debugifc->attr_debuginfo);
470 }
471 if (sfp->debugifc->debugcmd_created_ok) {
472 device_remove_file(sfp->class_dev,
473 &sfp->debugifc->attr_debugcmd);
474 }
475 kfree(sfp->debugifc);
476 sfp->debugifc = NULL;
477}
478#endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
479
480
481static void pvr2_sysfs_add_controls(struct pvr2_sysfs *sfp)
482{
483 unsigned int idx,cnt;
484 cnt = pvr2_hdw_get_ctrl_count(sfp->channel.hdw);
485 for (idx = 0; idx < cnt; idx++) {
486 pvr2_sysfs_add_control(sfp,idx);
487 }
488}
489
490
491static void pvr2_sysfs_tear_down_controls(struct pvr2_sysfs *sfp)
492{
493 struct pvr2_sysfs_ctl_item *cip1,*cip2;
494 for (cip1 = sfp->item_first; cip1; cip1 = cip2) {
495 cip2 = cip1->item_next;
496 if (cip1->created_ok) {
497 sysfs_remove_group(&sfp->class_dev->kobj,&cip1->grp);
498 }
499 pvr2_sysfs_trace("Destroying pvr2_sysfs_ctl_item id=%p",cip1);
500 kfree(cip1);
501 }
502}
503
504
505static void pvr2_sysfs_class_release(struct class *class)
506{
507 struct pvr2_sysfs_class *clp;
508 clp = container_of(class,struct pvr2_sysfs_class,class);
509 pvr2_sysfs_trace("Destroying pvr2_sysfs_class id=%p",clp);
510 kfree(clp);
511}
512
513
514static void pvr2_sysfs_release(struct device *class_dev)
515{
516 pvr2_sysfs_trace("Releasing class_dev id=%p",class_dev);
517 kfree(class_dev);
518}
519
520
521static void class_dev_destroy(struct pvr2_sysfs *sfp)
522{
523 struct device *dev;
524 if (!sfp->class_dev) return;
525#ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
526 pvr2_sysfs_tear_down_debugifc(sfp);
527#endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
528 pvr2_sysfs_tear_down_controls(sfp);
529 if (sfp->hdw_desc_created_ok) {
530 device_remove_file(sfp->class_dev,
531 &sfp->attr_hdw_desc);
532 }
533 if (sfp->hdw_name_created_ok) {
534 device_remove_file(sfp->class_dev,
535 &sfp->attr_hdw_name);
536 }
537 if (sfp->bus_info_created_ok) {
538 device_remove_file(sfp->class_dev,
539 &sfp->attr_bus_info);
540 }
541 if (sfp->v4l_minor_number_created_ok) {
542 device_remove_file(sfp->class_dev,
543 &sfp->attr_v4l_minor_number);
544 }
545 if (sfp->v4l_radio_minor_number_created_ok) {
546 device_remove_file(sfp->class_dev,
547 &sfp->attr_v4l_radio_minor_number);
548 }
549 if (sfp->unit_number_created_ok) {
550 device_remove_file(sfp->class_dev,
551 &sfp->attr_unit_number);
552 }
553 pvr2_sysfs_trace("Destroying class_dev id=%p",sfp->class_dev);
554 dev_set_drvdata(sfp->class_dev, NULL);
555 dev = sfp->class_dev->parent;
556 sfp->class_dev->parent = NULL;
557 put_device(dev);
558 device_unregister(sfp->class_dev);
559 sfp->class_dev = NULL;
560}
561
562
563static ssize_t v4l_minor_number_show(struct device *class_dev,
564 struct device_attribute *attr, char *buf)
565{
566 struct pvr2_sysfs *sfp;
567 sfp = dev_get_drvdata(class_dev);
568 if (!sfp) return -EINVAL;
569 return scnprintf(buf,PAGE_SIZE,"%d\n",
570 pvr2_hdw_v4l_get_minor_number(sfp->channel.hdw,
571 pvr2_v4l_type_video));
572}
573
574
575static ssize_t bus_info_show(struct device *class_dev,
576 struct device_attribute *attr, char *buf)
577{
578 struct pvr2_sysfs *sfp;
579 sfp = dev_get_drvdata(class_dev);
580 if (!sfp) return -EINVAL;
581 return scnprintf(buf,PAGE_SIZE,"%s\n",
582 pvr2_hdw_get_bus_info(sfp->channel.hdw));
583}
584
585
586static ssize_t hdw_name_show(struct device *class_dev,
587 struct device_attribute *attr, char *buf)
588{
589 struct pvr2_sysfs *sfp;
590 sfp = dev_get_drvdata(class_dev);
591 if (!sfp) return -EINVAL;
592 return scnprintf(buf,PAGE_SIZE,"%s\n",
593 pvr2_hdw_get_type(sfp->channel.hdw));
594}
595
596
597static ssize_t hdw_desc_show(struct device *class_dev,
598 struct device_attribute *attr, char *buf)
599{
600 struct pvr2_sysfs *sfp;
601 sfp = dev_get_drvdata(class_dev);
602 if (!sfp) return -EINVAL;
603 return scnprintf(buf,PAGE_SIZE,"%s\n",
604 pvr2_hdw_get_desc(sfp->channel.hdw));
605}
606
607
608static ssize_t v4l_radio_minor_number_show(struct device *class_dev,
609 struct device_attribute *attr,
610 char *buf)
611{
612 struct pvr2_sysfs *sfp;
613 sfp = dev_get_drvdata(class_dev);
614 if (!sfp) return -EINVAL;
615 return scnprintf(buf,PAGE_SIZE,"%d\n",
616 pvr2_hdw_v4l_get_minor_number(sfp->channel.hdw,
617 pvr2_v4l_type_radio));
618}
619
620
621static ssize_t unit_number_show(struct device *class_dev,
622 struct device_attribute *attr, char *buf)
623{
624 struct pvr2_sysfs *sfp;
625 sfp = dev_get_drvdata(class_dev);
626 if (!sfp) return -EINVAL;
627 return scnprintf(buf,PAGE_SIZE,"%d\n",
628 pvr2_hdw_get_unit_number(sfp->channel.hdw));
629}
630
631
632static void class_dev_create(struct pvr2_sysfs *sfp,
633 struct pvr2_sysfs_class *class_ptr)
634{
635 struct usb_device *usb_dev;
636 struct device *class_dev;
637 int ret;
638
639 usb_dev = pvr2_hdw_get_dev(sfp->channel.hdw);
640 if (!usb_dev) return;
641 class_dev = kzalloc(sizeof(*class_dev),GFP_KERNEL);
642 if (!class_dev) return;
643
644 pvr2_sysfs_trace("Creating class_dev id=%p",class_dev);
645
646 class_dev->class = &class_ptr->class;
647
648 dev_set_name(class_dev, "%s",
649 pvr2_hdw_get_device_identifier(sfp->channel.hdw));
650
651 class_dev->parent = get_device(&usb_dev->dev);
652
653 sfp->class_dev = class_dev;
654 dev_set_drvdata(class_dev, sfp);
655 ret = device_register(class_dev);
656 if (ret) {
657 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
658 "device_register failed");
659 put_device(class_dev);
660 return;
661 }
662
663 sysfs_attr_init(&sfp->attr_v4l_minor_number.attr);
664 sfp->attr_v4l_minor_number.attr.name = "v4l_minor_number";
665 sfp->attr_v4l_minor_number.attr.mode = S_IRUGO;
666 sfp->attr_v4l_minor_number.show = v4l_minor_number_show;
667 sfp->attr_v4l_minor_number.store = NULL;
668 ret = device_create_file(sfp->class_dev,
669 &sfp->attr_v4l_minor_number);
670 if (ret < 0) {
671 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
672 "device_create_file error: %d",
673 ret);
674 } else {
675 sfp->v4l_minor_number_created_ok = !0;
676 }
677
678 sysfs_attr_init(&sfp->attr_v4l_radio_minor_number.attr);
679 sfp->attr_v4l_radio_minor_number.attr.name = "v4l_radio_minor_number";
680 sfp->attr_v4l_radio_minor_number.attr.mode = S_IRUGO;
681 sfp->attr_v4l_radio_minor_number.show = v4l_radio_minor_number_show;
682 sfp->attr_v4l_radio_minor_number.store = NULL;
683 ret = device_create_file(sfp->class_dev,
684 &sfp->attr_v4l_radio_minor_number);
685 if (ret < 0) {
686 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
687 "device_create_file error: %d",
688 ret);
689 } else {
690 sfp->v4l_radio_minor_number_created_ok = !0;
691 }
692
693 sysfs_attr_init(&sfp->attr_unit_number.attr);
694 sfp->attr_unit_number.attr.name = "unit_number";
695 sfp->attr_unit_number.attr.mode = S_IRUGO;
696 sfp->attr_unit_number.show = unit_number_show;
697 sfp->attr_unit_number.store = NULL;
698 ret = device_create_file(sfp->class_dev,&sfp->attr_unit_number);
699 if (ret < 0) {
700 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
701 "device_create_file error: %d",
702 ret);
703 } else {
704 sfp->unit_number_created_ok = !0;
705 }
706
707 sysfs_attr_init(&sfp->attr_bus_info.attr);
708 sfp->attr_bus_info.attr.name = "bus_info_str";
709 sfp->attr_bus_info.attr.mode = S_IRUGO;
710 sfp->attr_bus_info.show = bus_info_show;
711 sfp->attr_bus_info.store = NULL;
712 ret = device_create_file(sfp->class_dev,
713 &sfp->attr_bus_info);
714 if (ret < 0) {
715 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
716 "device_create_file error: %d",
717 ret);
718 } else {
719 sfp->bus_info_created_ok = !0;
720 }
721
722 sysfs_attr_init(&sfp->attr_hdw_name.attr);
723 sfp->attr_hdw_name.attr.name = "device_hardware_type";
724 sfp->attr_hdw_name.attr.mode = S_IRUGO;
725 sfp->attr_hdw_name.show = hdw_name_show;
726 sfp->attr_hdw_name.store = NULL;
727 ret = device_create_file(sfp->class_dev,
728 &sfp->attr_hdw_name);
729 if (ret < 0) {
730 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
731 "device_create_file error: %d",
732 ret);
733 } else {
734 sfp->hdw_name_created_ok = !0;
735 }
736
737 sysfs_attr_init(&sfp->attr_hdw_desc.attr);
738 sfp->attr_hdw_desc.attr.name = "device_hardware_description";
739 sfp->attr_hdw_desc.attr.mode = S_IRUGO;
740 sfp->attr_hdw_desc.show = hdw_desc_show;
741 sfp->attr_hdw_desc.store = NULL;
742 ret = device_create_file(sfp->class_dev,
743 &sfp->attr_hdw_desc);
744 if (ret < 0) {
745 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
746 "device_create_file error: %d",
747 ret);
748 } else {
749 sfp->hdw_desc_created_ok = !0;
750 }
751
752 pvr2_sysfs_add_controls(sfp);
753#ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
754 pvr2_sysfs_add_debugifc(sfp);
755#endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
756}
757
758
759static void pvr2_sysfs_internal_check(struct pvr2_channel *chp)
760{
761 struct pvr2_sysfs *sfp;
762 sfp = container_of(chp,struct pvr2_sysfs,channel);
763 if (!sfp->channel.mc_head->disconnect_flag) return;
764 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_sysfs id=%p",sfp);
765 class_dev_destroy(sfp);
766 pvr2_channel_done(&sfp->channel);
767 kfree(sfp);
768}
769
770
771struct pvr2_sysfs *pvr2_sysfs_create(struct pvr2_context *mp,
772 struct pvr2_sysfs_class *class_ptr)
773{
774 struct pvr2_sysfs *sfp;
775 sfp = kzalloc(sizeof(*sfp),GFP_KERNEL);
776 if (!sfp) return sfp;
777 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_sysfs id=%p",sfp);
778 pvr2_channel_init(&sfp->channel,mp);
779 sfp->channel.check_func = pvr2_sysfs_internal_check;
780
781 class_dev_create(sfp,class_ptr);
782 return sfp;
783}
784
785
786
787struct pvr2_sysfs_class *pvr2_sysfs_class_create(void)
788{
789 struct pvr2_sysfs_class *clp;
790 clp = kzalloc(sizeof(*clp),GFP_KERNEL);
791 if (!clp) return clp;
792 pvr2_sysfs_trace("Creating and registering pvr2_sysfs_class id=%p",
793 clp);
794 clp->class.name = "pvrusb2";
795 clp->class.class_release = pvr2_sysfs_class_release;
796 clp->class.dev_release = pvr2_sysfs_release;
797 if (class_register(&clp->class)) {
798 pvr2_sysfs_trace(
799 "Registration failed for pvr2_sysfs_class id=%p",clp);
800 kfree(clp);
801 clp = NULL;
802 }
803 return clp;
804}
805
806
807void pvr2_sysfs_class_destroy(struct pvr2_sysfs_class *clp)
808{
809 pvr2_sysfs_trace("Unregistering pvr2_sysfs_class id=%p", clp);
810 class_unregister(&clp->class);
811}
812
813
814#ifdef CONFIG_VIDEO_PVRUSB2_DEBUGIFC
815static ssize_t debuginfo_show(struct device *class_dev,
816 struct device_attribute *attr, char *buf)
817{
818 struct pvr2_sysfs *sfp;
819 sfp = dev_get_drvdata(class_dev);
820 if (!sfp) return -EINVAL;
821 pvr2_hdw_trigger_module_log(sfp->channel.hdw);
822 return pvr2_debugifc_print_info(sfp->channel.hdw,buf,PAGE_SIZE);
823}
824
825
826static ssize_t debugcmd_show(struct device *class_dev,
827 struct device_attribute *attr, char *buf)
828{
829 struct pvr2_sysfs *sfp;
830 sfp = dev_get_drvdata(class_dev);
831 if (!sfp) return -EINVAL;
832 return pvr2_debugifc_print_status(sfp->channel.hdw,buf,PAGE_SIZE);
833}
834
835
836static ssize_t debugcmd_store(struct device *class_dev,
837 struct device_attribute *attr,
838 const char *buf, size_t count)
839{
840 struct pvr2_sysfs *sfp;
841 int ret;
842
843 sfp = dev_get_drvdata(class_dev);
844 if (!sfp) return -EINVAL;
845
846 ret = pvr2_debugifc_docmd(sfp->channel.hdw,buf,count);
847 if (ret < 0) return ret;
848 return count;
849}
850#endif /* CONFIG_VIDEO_PVRUSB2_DEBUGIFC */
851
852
853/*
854 Stuff for Emacs to see, in order to encourage consistent editing style:
855 *** Local Variables: ***
856 *** mode: c ***
857 *** fill-column: 75 ***
858 *** tab-width: 8 ***
859 *** c-basic-offset: 8 ***
860 *** End: ***
861 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-sysfs.h b/drivers/media/video/pvrusb2/pvrusb2-sysfs.h
new file mode 100644
index 00000000000..6d875bfe799
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-sysfs.h
@@ -0,0 +1,46 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20#ifndef __PVRUSB2_SYSFS_H
21#define __PVRUSB2_SYSFS_H
22
23#include <linux/list.h>
24#include <linux/sysfs.h>
25#include "pvrusb2-context.h"
26
27struct pvr2_sysfs;
28struct pvr2_sysfs_class;
29
30struct pvr2_sysfs_class *pvr2_sysfs_class_create(void);
31void pvr2_sysfs_class_destroy(struct pvr2_sysfs_class *);
32
33struct pvr2_sysfs *pvr2_sysfs_create(struct pvr2_context *,
34 struct pvr2_sysfs_class *);
35
36#endif /* __PVRUSB2_SYSFS_H */
37
38/*
39 Stuff for Emacs to see, in order to encourage consistent editing style:
40 *** Local Variables: ***
41 *** mode: c ***
42 *** fill-column: 75 ***
43 *** tab-width: 8 ***
44 *** c-basic-offset: 8 ***
45 *** End: ***
46 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-util.h b/drivers/media/video/pvrusb2/pvrusb2-util.h
new file mode 100644
index 00000000000..92b75544ee2
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-util.h
@@ -0,0 +1,62 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20#ifndef __PVRUSB2_UTIL_H
21#define __PVRUSB2_UTIL_H
22
23#define PVR2_DECOMPOSE_LE(t,i,d) \
24 do { \
25 (t)[i] = (d) & 0xff;\
26 (t)[i+1] = ((d) >> 8) & 0xff;\
27 (t)[i+2] = ((d) >> 16) & 0xff;\
28 (t)[i+3] = ((d) >> 24) & 0xff;\
29 } while(0)
30
31#define PVR2_DECOMPOSE_BE(t,i,d) \
32 do { \
33 (t)[i+3] = (d) & 0xff;\
34 (t)[i+2] = ((d) >> 8) & 0xff;\
35 (t)[i+1] = ((d) >> 16) & 0xff;\
36 (t)[i] = ((d) >> 24) & 0xff;\
37 } while(0)
38
39#define PVR2_COMPOSE_LE(t,i) \
40 ((((u32)((t)[i+3])) << 24) | \
41 (((u32)((t)[i+2])) << 16) | \
42 (((u32)((t)[i+1])) << 8) | \
43 ((u32)((t)[i])))
44
45#define PVR2_COMPOSE_BE(t,i) \
46 ((((u32)((t)[i])) << 24) | \
47 (((u32)((t)[i+1])) << 16) | \
48 (((u32)((t)[i+2])) << 8) | \
49 ((u32)((t)[i+3])))
50
51
52#endif /* __PVRUSB2_UTIL_H */
53
54/*
55 Stuff for Emacs to see, in order to encourage consistent editing style:
56 *** Local Variables: ***
57 *** mode: c ***
58 *** fill-column: 75 ***
59 *** tab-width: 8 ***
60 *** c-basic-offset: 8 ***
61 *** End: ***
62 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-v4l2.c b/drivers/media/video/pvrusb2/pvrusb2-v4l2.c
new file mode 100644
index 00000000000..e27f8ab7696
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-v4l2.c
@@ -0,0 +1,1356 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include <linux/kernel.h>
23#include <linux/slab.h>
24#include <linux/version.h>
25#include "pvrusb2-context.h"
26#include "pvrusb2-hdw.h"
27#include "pvrusb2.h"
28#include "pvrusb2-debug.h"
29#include "pvrusb2-v4l2.h"
30#include "pvrusb2-ioread.h"
31#include <linux/videodev2.h>
32#include <media/v4l2-dev.h>
33#include <media/v4l2-common.h>
34#include <media/v4l2-ioctl.h>
35
36struct pvr2_v4l2_dev;
37struct pvr2_v4l2_fh;
38struct pvr2_v4l2;
39
40struct pvr2_v4l2_dev {
41 struct video_device devbase; /* MUST be first! */
42 struct pvr2_v4l2 *v4lp;
43 struct pvr2_context_stream *stream;
44 /* Information about this device: */
45 enum pvr2_config config; /* Expected stream format */
46 int v4l_type; /* V4L defined type for this device node */
47 enum pvr2_v4l_type minor_type; /* pvr2-understood minor device type */
48};
49
50struct pvr2_v4l2_fh {
51 struct pvr2_channel channel;
52 struct pvr2_v4l2_dev *pdi;
53 enum v4l2_priority prio;
54 struct pvr2_ioread *rhp;
55 struct file *file;
56 struct pvr2_v4l2 *vhead;
57 struct pvr2_v4l2_fh *vnext;
58 struct pvr2_v4l2_fh *vprev;
59 wait_queue_head_t wait_data;
60 int fw_mode_flag;
61 /* Map contiguous ordinal value to input id */
62 unsigned char *input_map;
63 unsigned int input_cnt;
64};
65
66struct pvr2_v4l2 {
67 struct pvr2_channel channel;
68 struct pvr2_v4l2_fh *vfirst;
69 struct pvr2_v4l2_fh *vlast;
70
71 struct v4l2_prio_state prio;
72
73 /* streams - Note that these must be separately, individually,
74 * allocated pointers. This is because the v4l core is going to
75 * manage their deletion - separately, individually... */
76 struct pvr2_v4l2_dev *dev_video;
77 struct pvr2_v4l2_dev *dev_radio;
78};
79
80static int video_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
81module_param_array(video_nr, int, NULL, 0444);
82MODULE_PARM_DESC(video_nr, "Offset for device's video dev minor");
83static int radio_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
84module_param_array(radio_nr, int, NULL, 0444);
85MODULE_PARM_DESC(radio_nr, "Offset for device's radio dev minor");
86static int vbi_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
87module_param_array(vbi_nr, int, NULL, 0444);
88MODULE_PARM_DESC(vbi_nr, "Offset for device's vbi dev minor");
89
90static struct v4l2_capability pvr_capability ={
91 .driver = "pvrusb2",
92 .card = "Hauppauge WinTV pvr-usb2",
93 .bus_info = "usb",
94 .version = LINUX_VERSION_CODE,
95 .capabilities = (V4L2_CAP_VIDEO_CAPTURE |
96 V4L2_CAP_TUNER | V4L2_CAP_AUDIO | V4L2_CAP_RADIO |
97 V4L2_CAP_READWRITE),
98 .reserved = {0,0,0,0}
99};
100
101static struct v4l2_fmtdesc pvr_fmtdesc [] = {
102 {
103 .index = 0,
104 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
105 .flags = V4L2_FMT_FLAG_COMPRESSED,
106 .description = "MPEG1/2",
107 // This should really be V4L2_PIX_FMT_MPEG, but xawtv
108 // breaks when I do that.
109 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
110 .reserved = { 0, 0, 0, 0 }
111 }
112};
113
114#define PVR_FORMAT_PIX 0
115#define PVR_FORMAT_VBI 1
116
117static struct v4l2_format pvr_format [] = {
118 [PVR_FORMAT_PIX] = {
119 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
120 .fmt = {
121 .pix = {
122 .width = 720,
123 .height = 576,
124 // This should really be V4L2_PIX_FMT_MPEG,
125 // but xawtv breaks when I do that.
126 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
127 .field = V4L2_FIELD_INTERLACED,
128 .bytesperline = 0, // doesn't make sense
129 // here
130 //FIXME : Don't know what to put here...
131 .sizeimage = (32*1024),
132 .colorspace = 0, // doesn't make sense here
133 .priv = 0
134 }
135 }
136 },
137 [PVR_FORMAT_VBI] = {
138 .type = V4L2_BUF_TYPE_VBI_CAPTURE,
139 .fmt = {
140 .vbi = {
141 .sampling_rate = 27000000,
142 .offset = 248,
143 .samples_per_line = 1443,
144 .sample_format = V4L2_PIX_FMT_GREY,
145 .start = { 0, 0 },
146 .count = { 0, 0 },
147 .flags = 0,
148 .reserved = { 0, 0 }
149 }
150 }
151 }
152};
153
154
155/*
156 * pvr_ioctl()
157 *
158 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
159 *
160 */
161static long pvr2_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
162{
163 struct pvr2_v4l2_fh *fh = file->private_data;
164 struct pvr2_v4l2 *vp = fh->vhead;
165 struct pvr2_v4l2_dev *pdi = fh->pdi;
166 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
167 long ret = -EINVAL;
168
169 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
170 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),cmd);
171 }
172
173 if (!pvr2_hdw_dev_ok(hdw)) {
174 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
175 "ioctl failed - bad or no context");
176 return -EFAULT;
177 }
178
179 /* check priority */
180 switch (cmd) {
181 case VIDIOC_S_CTRL:
182 case VIDIOC_S_STD:
183 case VIDIOC_S_INPUT:
184 case VIDIOC_S_TUNER:
185 case VIDIOC_S_FREQUENCY:
186 ret = v4l2_prio_check(&vp->prio, fh->prio);
187 if (ret)
188 return ret;
189 }
190
191 switch (cmd) {
192 case VIDIOC_QUERYCAP:
193 {
194 struct v4l2_capability *cap = arg;
195
196 memcpy(cap, &pvr_capability, sizeof(struct v4l2_capability));
197 strlcpy(cap->bus_info,pvr2_hdw_get_bus_info(hdw),
198 sizeof(cap->bus_info));
199 strlcpy(cap->card,pvr2_hdw_get_desc(hdw),sizeof(cap->card));
200
201 ret = 0;
202 break;
203 }
204
205 case VIDIOC_G_PRIORITY:
206 {
207 enum v4l2_priority *p = arg;
208
209 *p = v4l2_prio_max(&vp->prio);
210 ret = 0;
211 break;
212 }
213
214 case VIDIOC_S_PRIORITY:
215 {
216 enum v4l2_priority *prio = arg;
217
218 ret = v4l2_prio_change(&vp->prio, &fh->prio, *prio);
219 break;
220 }
221
222 case VIDIOC_ENUMSTD:
223 {
224 struct v4l2_standard *vs = (struct v4l2_standard *)arg;
225 int idx = vs->index;
226 ret = pvr2_hdw_get_stdenum_value(hdw,vs,idx+1);
227 break;
228 }
229
230 case VIDIOC_G_STD:
231 {
232 int val = 0;
233 ret = pvr2_ctrl_get_value(
234 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),&val);
235 *(v4l2_std_id *)arg = val;
236 break;
237 }
238
239 case VIDIOC_S_STD:
240 {
241 ret = pvr2_ctrl_set_value(
242 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),
243 *(v4l2_std_id *)arg);
244 break;
245 }
246
247 case VIDIOC_ENUMINPUT:
248 {
249 struct pvr2_ctrl *cptr;
250 struct v4l2_input *vi = (struct v4l2_input *)arg;
251 struct v4l2_input tmp;
252 unsigned int cnt;
253 int val;
254
255 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
256
257 memset(&tmp,0,sizeof(tmp));
258 tmp.index = vi->index;
259 ret = 0;
260 if (vi->index >= fh->input_cnt) {
261 ret = -EINVAL;
262 break;
263 }
264 val = fh->input_map[vi->index];
265 switch (val) {
266 case PVR2_CVAL_INPUT_TV:
267 case PVR2_CVAL_INPUT_DTV:
268 case PVR2_CVAL_INPUT_RADIO:
269 tmp.type = V4L2_INPUT_TYPE_TUNER;
270 break;
271 case PVR2_CVAL_INPUT_SVIDEO:
272 case PVR2_CVAL_INPUT_COMPOSITE:
273 tmp.type = V4L2_INPUT_TYPE_CAMERA;
274 break;
275 default:
276 ret = -EINVAL;
277 break;
278 }
279 if (ret < 0) break;
280
281 cnt = 0;
282 pvr2_ctrl_get_valname(cptr,val,
283 tmp.name,sizeof(tmp.name)-1,&cnt);
284 tmp.name[cnt] = 0;
285
286 /* Don't bother with audioset, since this driver currently
287 always switches the audio whenever the video is
288 switched. */
289
290 /* Handling std is a tougher problem. It doesn't make
291 sense in cases where a device might be multi-standard.
292 We could just copy out the current value for the
293 standard, but it can change over time. For now just
294 leave it zero. */
295
296 memcpy(vi, &tmp, sizeof(tmp));
297
298 ret = 0;
299 break;
300 }
301
302 case VIDIOC_G_INPUT:
303 {
304 unsigned int idx;
305 struct pvr2_ctrl *cptr;
306 struct v4l2_input *vi = (struct v4l2_input *)arg;
307 int val;
308 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
309 val = 0;
310 ret = pvr2_ctrl_get_value(cptr,&val);
311 vi->index = 0;
312 for (idx = 0; idx < fh->input_cnt; idx++) {
313 if (fh->input_map[idx] == val) {
314 vi->index = idx;
315 break;
316 }
317 }
318 break;
319 }
320
321 case VIDIOC_S_INPUT:
322 {
323 struct v4l2_input *vi = (struct v4l2_input *)arg;
324 if (vi->index >= fh->input_cnt) {
325 ret = -ERANGE;
326 break;
327 }
328 ret = pvr2_ctrl_set_value(
329 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
330 fh->input_map[vi->index]);
331 break;
332 }
333
334 case VIDIOC_ENUMAUDIO:
335 {
336 /* pkt: FIXME: We are returning one "fake" input here
337 which could very well be called "whatever_we_like".
338 This is for apps that want to see an audio input
339 just to feel comfortable, as well as to test if
340 it can do stereo or sth. There is actually no guarantee
341 that the actual audio input cannot change behind the app's
342 back, but most applications should not mind that either.
343
344 Hopefully, mplayer people will work with us on this (this
345 whole mess is to support mplayer pvr://), or Hans will come
346 up with a more standard way to say "we have inputs but we
347 don 't want you to change them independent of video" which
348 will sort this mess.
349 */
350 struct v4l2_audio *vin = arg;
351 ret = -EINVAL;
352 if (vin->index > 0) break;
353 strncpy(vin->name, "PVRUSB2 Audio",14);
354 vin->capability = V4L2_AUDCAP_STEREO;
355 ret = 0;
356 break;
357 break;
358 }
359
360 case VIDIOC_G_AUDIO:
361 {
362 /* pkt: FIXME: see above comment (VIDIOC_ENUMAUDIO) */
363 struct v4l2_audio *vin = arg;
364 memset(vin,0,sizeof(*vin));
365 vin->index = 0;
366 strncpy(vin->name, "PVRUSB2 Audio",14);
367 vin->capability = V4L2_AUDCAP_STEREO;
368 ret = 0;
369 break;
370 }
371
372 case VIDIOC_G_TUNER:
373 {
374 struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
375
376 if (vt->index != 0) break; /* Only answer for the 1st tuner */
377
378 pvr2_hdw_execute_tuner_poll(hdw);
379 ret = pvr2_hdw_get_tuner_status(hdw,vt);
380 break;
381 }
382
383 case VIDIOC_S_TUNER:
384 {
385 struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
386
387 if (vt->index != 0)
388 break;
389
390 ret = pvr2_ctrl_set_value(
391 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
392 vt->audmode);
393 break;
394 }
395
396 case VIDIOC_S_FREQUENCY:
397 {
398 const struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
399 unsigned long fv;
400 struct v4l2_tuner vt;
401 int cur_input;
402 struct pvr2_ctrl *ctrlp;
403 ret = pvr2_hdw_get_tuner_status(hdw,&vt);
404 if (ret != 0) break;
405 ctrlp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
406 ret = pvr2_ctrl_get_value(ctrlp,&cur_input);
407 if (ret != 0) break;
408 if (vf->type == V4L2_TUNER_RADIO) {
409 if (cur_input != PVR2_CVAL_INPUT_RADIO) {
410 pvr2_ctrl_set_value(ctrlp,
411 PVR2_CVAL_INPUT_RADIO);
412 }
413 } else {
414 if (cur_input == PVR2_CVAL_INPUT_RADIO) {
415 pvr2_ctrl_set_value(ctrlp,
416 PVR2_CVAL_INPUT_TV);
417 }
418 }
419 fv = vf->frequency;
420 if (vt.capability & V4L2_TUNER_CAP_LOW) {
421 fv = (fv * 125) / 2;
422 } else {
423 fv = fv * 62500;
424 }
425 ret = pvr2_ctrl_set_value(
426 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),fv);
427 break;
428 }
429
430 case VIDIOC_G_FREQUENCY:
431 {
432 struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
433 int val = 0;
434 int cur_input;
435 struct v4l2_tuner vt;
436 ret = pvr2_hdw_get_tuner_status(hdw,&vt);
437 if (ret != 0) break;
438 ret = pvr2_ctrl_get_value(
439 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
440 &val);
441 if (ret != 0) break;
442 pvr2_ctrl_get_value(
443 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
444 &cur_input);
445 if (cur_input == PVR2_CVAL_INPUT_RADIO) {
446 vf->type = V4L2_TUNER_RADIO;
447 } else {
448 vf->type = V4L2_TUNER_ANALOG_TV;
449 }
450 if (vt.capability & V4L2_TUNER_CAP_LOW) {
451 val = (val * 2) / 125;
452 } else {
453 val /= 62500;
454 }
455 vf->frequency = val;
456 break;
457 }
458
459 case VIDIOC_ENUM_FMT:
460 {
461 struct v4l2_fmtdesc *fd = (struct v4l2_fmtdesc *)arg;
462
463 /* Only one format is supported : mpeg.*/
464 if (fd->index != 0)
465 break;
466
467 memcpy(fd, pvr_fmtdesc, sizeof(struct v4l2_fmtdesc));
468 ret = 0;
469 break;
470 }
471
472 case VIDIOC_G_FMT:
473 {
474 struct v4l2_format *vf = (struct v4l2_format *)arg;
475 int val;
476 switch(vf->type) {
477 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
478 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
479 sizeof(struct v4l2_format));
480 val = 0;
481 pvr2_ctrl_get_value(
482 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES),
483 &val);
484 vf->fmt.pix.width = val;
485 val = 0;
486 pvr2_ctrl_get_value(
487 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES),
488 &val);
489 vf->fmt.pix.height = val;
490 ret = 0;
491 break;
492 case V4L2_BUF_TYPE_VBI_CAPTURE:
493 // ????? Still need to figure out to do VBI correctly
494 ret = -EINVAL;
495 break;
496 default:
497 ret = -EINVAL;
498 break;
499 }
500 break;
501 }
502
503 case VIDIOC_TRY_FMT:
504 case VIDIOC_S_FMT:
505 {
506 struct v4l2_format *vf = (struct v4l2_format *)arg;
507
508 ret = 0;
509 switch(vf->type) {
510 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
511 int lmin,lmax,ldef;
512 struct pvr2_ctrl *hcp,*vcp;
513 int h = vf->fmt.pix.height;
514 int w = vf->fmt.pix.width;
515 hcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES);
516 vcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES);
517
518 lmin = pvr2_ctrl_get_min(hcp);
519 lmax = pvr2_ctrl_get_max(hcp);
520 pvr2_ctrl_get_def(hcp, &ldef);
521 if (w == -1) {
522 w = ldef;
523 } else if (w < lmin) {
524 w = lmin;
525 } else if (w > lmax) {
526 w = lmax;
527 }
528 lmin = pvr2_ctrl_get_min(vcp);
529 lmax = pvr2_ctrl_get_max(vcp);
530 pvr2_ctrl_get_def(vcp, &ldef);
531 if (h == -1) {
532 h = ldef;
533 } else if (h < lmin) {
534 h = lmin;
535 } else if (h > lmax) {
536 h = lmax;
537 }
538
539 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
540 sizeof(struct v4l2_format));
541 vf->fmt.pix.width = w;
542 vf->fmt.pix.height = h;
543
544 if (cmd == VIDIOC_S_FMT) {
545 pvr2_ctrl_set_value(hcp,vf->fmt.pix.width);
546 pvr2_ctrl_set_value(vcp,vf->fmt.pix.height);
547 }
548 } break;
549 case V4L2_BUF_TYPE_VBI_CAPTURE:
550 // ????? Still need to figure out to do VBI correctly
551 ret = -EINVAL;
552 break;
553 default:
554 ret = -EINVAL;
555 break;
556 }
557 break;
558 }
559
560 case VIDIOC_STREAMON:
561 {
562 if (!fh->pdi->stream) {
563 /* No stream defined for this node. This means
564 that we're not currently allowed to stream from
565 this node. */
566 ret = -EPERM;
567 break;
568 }
569 ret = pvr2_hdw_set_stream_type(hdw,pdi->config);
570 if (ret < 0) return ret;
571 ret = pvr2_hdw_set_streaming(hdw,!0);
572 break;
573 }
574
575 case VIDIOC_STREAMOFF:
576 {
577 if (!fh->pdi->stream) {
578 /* No stream defined for this node. This means
579 that we're not currently allowed to stream from
580 this node. */
581 ret = -EPERM;
582 break;
583 }
584 ret = pvr2_hdw_set_streaming(hdw,0);
585 break;
586 }
587
588 case VIDIOC_QUERYCTRL:
589 {
590 struct pvr2_ctrl *cptr;
591 int val;
592 struct v4l2_queryctrl *vc = (struct v4l2_queryctrl *)arg;
593 ret = 0;
594 if (vc->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
595 cptr = pvr2_hdw_get_ctrl_nextv4l(
596 hdw,(vc->id & ~V4L2_CTRL_FLAG_NEXT_CTRL));
597 if (cptr) vc->id = pvr2_ctrl_get_v4lid(cptr);
598 } else {
599 cptr = pvr2_hdw_get_ctrl_v4l(hdw,vc->id);
600 }
601 if (!cptr) {
602 pvr2_trace(PVR2_TRACE_V4LIOCTL,
603 "QUERYCTRL id=0x%x not implemented here",
604 vc->id);
605 ret = -EINVAL;
606 break;
607 }
608
609 pvr2_trace(PVR2_TRACE_V4LIOCTL,
610 "QUERYCTRL id=0x%x mapping name=%s (%s)",
611 vc->id,pvr2_ctrl_get_name(cptr),
612 pvr2_ctrl_get_desc(cptr));
613 strlcpy(vc->name,pvr2_ctrl_get_desc(cptr),sizeof(vc->name));
614 vc->flags = pvr2_ctrl_get_v4lflags(cptr);
615 pvr2_ctrl_get_def(cptr, &val);
616 vc->default_value = val;
617 switch (pvr2_ctrl_get_type(cptr)) {
618 case pvr2_ctl_enum:
619 vc->type = V4L2_CTRL_TYPE_MENU;
620 vc->minimum = 0;
621 vc->maximum = pvr2_ctrl_get_cnt(cptr) - 1;
622 vc->step = 1;
623 break;
624 case pvr2_ctl_bool:
625 vc->type = V4L2_CTRL_TYPE_BOOLEAN;
626 vc->minimum = 0;
627 vc->maximum = 1;
628 vc->step = 1;
629 break;
630 case pvr2_ctl_int:
631 vc->type = V4L2_CTRL_TYPE_INTEGER;
632 vc->minimum = pvr2_ctrl_get_min(cptr);
633 vc->maximum = pvr2_ctrl_get_max(cptr);
634 vc->step = 1;
635 break;
636 default:
637 pvr2_trace(PVR2_TRACE_V4LIOCTL,
638 "QUERYCTRL id=0x%x name=%s not mappable",
639 vc->id,pvr2_ctrl_get_name(cptr));
640 ret = -EINVAL;
641 break;
642 }
643 break;
644 }
645
646 case VIDIOC_QUERYMENU:
647 {
648 struct v4l2_querymenu *vm = (struct v4l2_querymenu *)arg;
649 unsigned int cnt = 0;
650 ret = pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw,vm->id),
651 vm->index,
652 vm->name,sizeof(vm->name)-1,
653 &cnt);
654 vm->name[cnt] = 0;
655 break;
656 }
657
658 case VIDIOC_G_CTRL:
659 {
660 struct v4l2_control *vc = (struct v4l2_control *)arg;
661 int val = 0;
662 ret = pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
663 &val);
664 vc->value = val;
665 break;
666 }
667
668 case VIDIOC_S_CTRL:
669 {
670 struct v4l2_control *vc = (struct v4l2_control *)arg;
671 ret = pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
672 vc->value);
673 break;
674 }
675
676 case VIDIOC_G_EXT_CTRLS:
677 {
678 struct v4l2_ext_controls *ctls =
679 (struct v4l2_ext_controls *)arg;
680 struct v4l2_ext_control *ctrl;
681 unsigned int idx;
682 int val;
683 ret = 0;
684 for (idx = 0; idx < ctls->count; idx++) {
685 ctrl = ctls->controls + idx;
686 ret = pvr2_ctrl_get_value(
687 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),&val);
688 if (ret) {
689 ctls->error_idx = idx;
690 break;
691 }
692 /* Ensure that if read as a 64 bit value, the user
693 will still get a hopefully sane value */
694 ctrl->value64 = 0;
695 ctrl->value = val;
696 }
697 break;
698 }
699
700 case VIDIOC_S_EXT_CTRLS:
701 {
702 struct v4l2_ext_controls *ctls =
703 (struct v4l2_ext_controls *)arg;
704 struct v4l2_ext_control *ctrl;
705 unsigned int idx;
706 ret = 0;
707 for (idx = 0; idx < ctls->count; idx++) {
708 ctrl = ctls->controls + idx;
709 ret = pvr2_ctrl_set_value(
710 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),
711 ctrl->value);
712 if (ret) {
713 ctls->error_idx = idx;
714 break;
715 }
716 }
717 break;
718 }
719
720 case VIDIOC_TRY_EXT_CTRLS:
721 {
722 struct v4l2_ext_controls *ctls =
723 (struct v4l2_ext_controls *)arg;
724 struct v4l2_ext_control *ctrl;
725 struct pvr2_ctrl *pctl;
726 unsigned int idx;
727 /* For the moment just validate that the requested control
728 actually exists. */
729 ret = 0;
730 for (idx = 0; idx < ctls->count; idx++) {
731 ctrl = ctls->controls + idx;
732 pctl = pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id);
733 if (!pctl) {
734 ret = -EINVAL;
735 ctls->error_idx = idx;
736 break;
737 }
738 }
739 break;
740 }
741
742 case VIDIOC_CROPCAP:
743 {
744 struct v4l2_cropcap *cap = (struct v4l2_cropcap *)arg;
745 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
746 ret = -EINVAL;
747 break;
748 }
749 ret = pvr2_hdw_get_cropcap(hdw, cap);
750 cap->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; /* paranoia */
751 break;
752 }
753 case VIDIOC_G_CROP:
754 {
755 struct v4l2_crop *crop = (struct v4l2_crop *)arg;
756 int val = 0;
757 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
758 ret = -EINVAL;
759 break;
760 }
761 ret = pvr2_ctrl_get_value(
762 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPL), &val);
763 if (ret != 0) {
764 ret = -EINVAL;
765 break;
766 }
767 crop->c.left = val;
768 ret = pvr2_ctrl_get_value(
769 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPT), &val);
770 if (ret != 0) {
771 ret = -EINVAL;
772 break;
773 }
774 crop->c.top = val;
775 ret = pvr2_ctrl_get_value(
776 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPW), &val);
777 if (ret != 0) {
778 ret = -EINVAL;
779 break;
780 }
781 crop->c.width = val;
782 ret = pvr2_ctrl_get_value(
783 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPH), &val);
784 if (ret != 0) {
785 ret = -EINVAL;
786 break;
787 }
788 crop->c.height = val;
789 }
790 case VIDIOC_S_CROP:
791 {
792 struct v4l2_crop *crop = (struct v4l2_crop *)arg;
793 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
794 ret = -EINVAL;
795 break;
796 }
797 ret = pvr2_ctrl_set_value(
798 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPL),
799 crop->c.left);
800 if (ret != 0) {
801 ret = -EINVAL;
802 break;
803 }
804 ret = pvr2_ctrl_set_value(
805 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPT),
806 crop->c.top);
807 if (ret != 0) {
808 ret = -EINVAL;
809 break;
810 }
811 ret = pvr2_ctrl_set_value(
812 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPW),
813 crop->c.width);
814 if (ret != 0) {
815 ret = -EINVAL;
816 break;
817 }
818 ret = pvr2_ctrl_set_value(
819 pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPH),
820 crop->c.height);
821 if (ret != 0) {
822 ret = -EINVAL;
823 break;
824 }
825 }
826 case VIDIOC_LOG_STATUS:
827 {
828 pvr2_hdw_trigger_module_log(hdw);
829 ret = 0;
830 break;
831 }
832#ifdef CONFIG_VIDEO_ADV_DEBUG
833 case VIDIOC_DBG_S_REGISTER:
834 case VIDIOC_DBG_G_REGISTER:
835 {
836 u64 val;
837 struct v4l2_dbg_register *req = (struct v4l2_dbg_register *)arg;
838 if (cmd == VIDIOC_DBG_S_REGISTER) val = req->val;
839 ret = pvr2_hdw_register_access(
840 hdw, &req->match, req->reg,
841 cmd == VIDIOC_DBG_S_REGISTER, &val);
842 if (cmd == VIDIOC_DBG_G_REGISTER) req->val = val;
843 break;
844 }
845#endif
846
847 default :
848 ret = -ENOTTY;
849 break;
850 }
851
852 pvr2_hdw_commit_ctl(hdw);
853
854 if (ret < 0) {
855 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
856 pvr2_trace(PVR2_TRACE_V4LIOCTL,
857 "pvr2_v4l2_do_ioctl failure, ret=%ld", ret);
858 } else {
859 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
860 pvr2_trace(PVR2_TRACE_V4LIOCTL,
861 "pvr2_v4l2_do_ioctl failure, ret=%ld"
862 " command was:", ret);
863 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),
864 cmd);
865 }
866 }
867 } else {
868 pvr2_trace(PVR2_TRACE_V4LIOCTL,
869 "pvr2_v4l2_do_ioctl complete, ret=%ld (0x%lx)",
870 ret, ret);
871 }
872 return ret;
873}
874
875static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev *dip)
876{
877 struct pvr2_hdw *hdw = dip->v4lp->channel.mc_head->hdw;
878 enum pvr2_config cfg = dip->config;
879 char msg[80];
880 unsigned int mcnt;
881
882 /* Construct the unregistration message *before* we actually
883 perform the unregistration step. By doing it this way we don't
884 have to worry about potentially touching deleted resources. */
885 mcnt = scnprintf(msg, sizeof(msg) - 1,
886 "pvrusb2: unregistered device %s [%s]",
887 video_device_node_name(&dip->devbase),
888 pvr2_config_get_name(cfg));
889 msg[mcnt] = 0;
890
891 pvr2_hdw_v4l_store_minor_number(hdw,dip->minor_type,-1);
892
893 /* Paranoia */
894 dip->v4lp = NULL;
895 dip->stream = NULL;
896
897 /* Actual deallocation happens later when all internal references
898 are gone. */
899 video_unregister_device(&dip->devbase);
900
901 printk(KERN_INFO "%s\n", msg);
902
903}
904
905
906static void pvr2_v4l2_dev_disassociate_parent(struct pvr2_v4l2_dev *dip)
907{
908 if (!dip) return;
909 if (!dip->devbase.parent) return;
910 dip->devbase.parent = NULL;
911 device_move(&dip->devbase.dev, NULL, DPM_ORDER_NONE);
912}
913
914
915static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
916{
917 if (vp->dev_video) {
918 pvr2_v4l2_dev_destroy(vp->dev_video);
919 vp->dev_video = NULL;
920 }
921 if (vp->dev_radio) {
922 pvr2_v4l2_dev_destroy(vp->dev_radio);
923 vp->dev_radio = NULL;
924 }
925
926 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
927 pvr2_channel_done(&vp->channel);
928 kfree(vp);
929}
930
931
932static void pvr2_video_device_release(struct video_device *vdev)
933{
934 struct pvr2_v4l2_dev *dev;
935 dev = container_of(vdev,struct pvr2_v4l2_dev,devbase);
936 kfree(dev);
937}
938
939
940static void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
941{
942 struct pvr2_v4l2 *vp;
943 vp = container_of(chp,struct pvr2_v4l2,channel);
944 if (!vp->channel.mc_head->disconnect_flag) return;
945 pvr2_v4l2_dev_disassociate_parent(vp->dev_video);
946 pvr2_v4l2_dev_disassociate_parent(vp->dev_radio);
947 if (vp->vfirst) return;
948 pvr2_v4l2_destroy_no_lock(vp);
949}
950
951
952static long pvr2_v4l2_ioctl(struct file *file,
953 unsigned int cmd, unsigned long arg)
954{
955
956 return video_usercopy(file, cmd, arg, pvr2_v4l2_do_ioctl);
957}
958
959
960static int pvr2_v4l2_release(struct file *file)
961{
962 struct pvr2_v4l2_fh *fhp = file->private_data;
963 struct pvr2_v4l2 *vp = fhp->vhead;
964 struct pvr2_hdw *hdw = fhp->channel.mc_head->hdw;
965
966 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");
967
968 if (fhp->rhp) {
969 struct pvr2_stream *sp;
970 pvr2_hdw_set_streaming(hdw,0);
971 sp = pvr2_ioread_get_stream(fhp->rhp);
972 if (sp) pvr2_stream_set_callback(sp,NULL,NULL);
973 pvr2_ioread_destroy(fhp->rhp);
974 fhp->rhp = NULL;
975 }
976
977 v4l2_prio_close(&vp->prio, fhp->prio);
978 file->private_data = NULL;
979
980 if (fhp->vnext) {
981 fhp->vnext->vprev = fhp->vprev;
982 } else {
983 vp->vlast = fhp->vprev;
984 }
985 if (fhp->vprev) {
986 fhp->vprev->vnext = fhp->vnext;
987 } else {
988 vp->vfirst = fhp->vnext;
989 }
990 fhp->vnext = NULL;
991 fhp->vprev = NULL;
992 fhp->vhead = NULL;
993 pvr2_channel_done(&fhp->channel);
994 pvr2_trace(PVR2_TRACE_STRUCT,
995 "Destroying pvr_v4l2_fh id=%p",fhp);
996 if (fhp->input_map) {
997 kfree(fhp->input_map);
998 fhp->input_map = NULL;
999 }
1000 kfree(fhp);
1001 if (vp->channel.mc_head->disconnect_flag && !vp->vfirst) {
1002 pvr2_v4l2_destroy_no_lock(vp);
1003 }
1004 return 0;
1005}
1006
1007
1008static int pvr2_v4l2_open(struct file *file)
1009{
1010 struct pvr2_v4l2_dev *dip; /* Our own context pointer */
1011 struct pvr2_v4l2_fh *fhp;
1012 struct pvr2_v4l2 *vp;
1013 struct pvr2_hdw *hdw;
1014 unsigned int input_mask = 0;
1015 unsigned int input_cnt,idx;
1016 int ret = 0;
1017
1018 dip = container_of(video_devdata(file),struct pvr2_v4l2_dev,devbase);
1019
1020 vp = dip->v4lp;
1021 hdw = vp->channel.hdw;
1022
1023 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_open");
1024
1025 if (!pvr2_hdw_dev_ok(hdw)) {
1026 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,
1027 "pvr2_v4l2_open: hardware not ready");
1028 return -EIO;
1029 }
1030
1031 fhp = kzalloc(sizeof(*fhp),GFP_KERNEL);
1032 if (!fhp) {
1033 return -ENOMEM;
1034 }
1035
1036 init_waitqueue_head(&fhp->wait_data);
1037 fhp->pdi = dip;
1038
1039 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
1040 pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
1041
1042 if (dip->v4l_type == VFL_TYPE_RADIO) {
1043 /* Opening device as a radio, legal input selection subset
1044 is just the radio. */
1045 input_mask = (1 << PVR2_CVAL_INPUT_RADIO);
1046 } else {
1047 /* Opening the main V4L device, legal input selection
1048 subset includes all analog inputs. */
1049 input_mask = ((1 << PVR2_CVAL_INPUT_RADIO) |
1050 (1 << PVR2_CVAL_INPUT_TV) |
1051 (1 << PVR2_CVAL_INPUT_COMPOSITE) |
1052 (1 << PVR2_CVAL_INPUT_SVIDEO));
1053 }
1054 ret = pvr2_channel_limit_inputs(&fhp->channel,input_mask);
1055 if (ret) {
1056 pvr2_channel_done(&fhp->channel);
1057 pvr2_trace(PVR2_TRACE_STRUCT,
1058 "Destroying pvr_v4l2_fh id=%p (input mask error)",
1059 fhp);
1060
1061 kfree(fhp);
1062 return ret;
1063 }
1064
1065 input_mask &= pvr2_hdw_get_input_available(hdw);
1066 input_cnt = 0;
1067 for (idx = 0; idx < (sizeof(input_mask) << 3); idx++) {
1068 if (input_mask & (1 << idx)) input_cnt++;
1069 }
1070 fhp->input_cnt = input_cnt;
1071 fhp->input_map = kzalloc(input_cnt,GFP_KERNEL);
1072 if (!fhp->input_map) {
1073 pvr2_channel_done(&fhp->channel);
1074 pvr2_trace(PVR2_TRACE_STRUCT,
1075 "Destroying pvr_v4l2_fh id=%p (input map failure)",
1076 fhp);
1077 kfree(fhp);
1078 return -ENOMEM;
1079 }
1080 input_cnt = 0;
1081 for (idx = 0; idx < (sizeof(input_mask) << 3); idx++) {
1082 if (!(input_mask & (1 << idx))) continue;
1083 fhp->input_map[input_cnt++] = idx;
1084 }
1085
1086 fhp->vnext = NULL;
1087 fhp->vprev = vp->vlast;
1088 if (vp->vlast) {
1089 vp->vlast->vnext = fhp;
1090 } else {
1091 vp->vfirst = fhp;
1092 }
1093 vp->vlast = fhp;
1094 fhp->vhead = vp;
1095
1096 fhp->file = file;
1097 file->private_data = fhp;
1098 v4l2_prio_open(&vp->prio, &fhp->prio);
1099
1100 fhp->fw_mode_flag = pvr2_hdw_cpufw_get_enabled(hdw);
1101
1102 return 0;
1103}
1104
1105
1106static void pvr2_v4l2_notify(struct pvr2_v4l2_fh *fhp)
1107{
1108 wake_up(&fhp->wait_data);
1109}
1110
1111static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
1112{
1113 int ret;
1114 struct pvr2_stream *sp;
1115 struct pvr2_hdw *hdw;
1116 if (fh->rhp) return 0;
1117
1118 if (!fh->pdi->stream) {
1119 /* No stream defined for this node. This means that we're
1120 not currently allowed to stream from this node. */
1121 return -EPERM;
1122 }
1123
1124 /* First read() attempt. Try to claim the stream and start
1125 it... */
1126 if ((ret = pvr2_channel_claim_stream(&fh->channel,
1127 fh->pdi->stream)) != 0) {
1128 /* Someone else must already have it */
1129 return ret;
1130 }
1131
1132 fh->rhp = pvr2_channel_create_mpeg_stream(fh->pdi->stream);
1133 if (!fh->rhp) {
1134 pvr2_channel_claim_stream(&fh->channel,NULL);
1135 return -ENOMEM;
1136 }
1137
1138 hdw = fh->channel.mc_head->hdw;
1139 sp = fh->pdi->stream->stream;
1140 pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
1141 pvr2_hdw_set_stream_type(hdw,fh->pdi->config);
1142 if ((ret = pvr2_hdw_set_streaming(hdw,!0)) < 0) return ret;
1143 return pvr2_ioread_set_enabled(fh->rhp,!0);
1144}
1145
1146
1147static ssize_t pvr2_v4l2_read(struct file *file,
1148 char __user *buff, size_t count, loff_t *ppos)
1149{
1150 struct pvr2_v4l2_fh *fh = file->private_data;
1151 int ret;
1152
1153 if (fh->fw_mode_flag) {
1154 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
1155 char *tbuf;
1156 int c1,c2;
1157 int tcnt = 0;
1158 unsigned int offs = *ppos;
1159
1160 tbuf = kmalloc(PAGE_SIZE,GFP_KERNEL);
1161 if (!tbuf) return -ENOMEM;
1162
1163 while (count) {
1164 c1 = count;
1165 if (c1 > PAGE_SIZE) c1 = PAGE_SIZE;
1166 c2 = pvr2_hdw_cpufw_get(hdw,offs,tbuf,c1);
1167 if (c2 < 0) {
1168 tcnt = c2;
1169 break;
1170 }
1171 if (!c2) break;
1172 if (copy_to_user(buff,tbuf,c2)) {
1173 tcnt = -EFAULT;
1174 break;
1175 }
1176 offs += c2;
1177 tcnt += c2;
1178 buff += c2;
1179 count -= c2;
1180 *ppos += c2;
1181 }
1182 kfree(tbuf);
1183 return tcnt;
1184 }
1185
1186 if (!fh->rhp) {
1187 ret = pvr2_v4l2_iosetup(fh);
1188 if (ret) {
1189 return ret;
1190 }
1191 }
1192
1193 for (;;) {
1194 ret = pvr2_ioread_read(fh->rhp,buff,count);
1195 if (ret >= 0) break;
1196 if (ret != -EAGAIN) break;
1197 if (file->f_flags & O_NONBLOCK) break;
1198 /* Doing blocking I/O. Wait here. */
1199 ret = wait_event_interruptible(
1200 fh->wait_data,
1201 pvr2_ioread_avail(fh->rhp) >= 0);
1202 if (ret < 0) break;
1203 }
1204
1205 return ret;
1206}
1207
1208
1209static unsigned int pvr2_v4l2_poll(struct file *file, poll_table *wait)
1210{
1211 unsigned int mask = 0;
1212 struct pvr2_v4l2_fh *fh = file->private_data;
1213 int ret;
1214
1215 if (fh->fw_mode_flag) {
1216 mask |= POLLIN | POLLRDNORM;
1217 return mask;
1218 }
1219
1220 if (!fh->rhp) {
1221 ret = pvr2_v4l2_iosetup(fh);
1222 if (ret) return POLLERR;
1223 }
1224
1225 poll_wait(file,&fh->wait_data,wait);
1226
1227 if (pvr2_ioread_avail(fh->rhp) >= 0) {
1228 mask |= POLLIN | POLLRDNORM;
1229 }
1230
1231 return mask;
1232}
1233
1234
1235static const struct v4l2_file_operations vdev_fops = {
1236 .owner = THIS_MODULE,
1237 .open = pvr2_v4l2_open,
1238 .release = pvr2_v4l2_release,
1239 .read = pvr2_v4l2_read,
1240 .ioctl = pvr2_v4l2_ioctl,
1241 .poll = pvr2_v4l2_poll,
1242};
1243
1244
1245static struct video_device vdev_template = {
1246 .fops = &vdev_fops,
1247};
1248
1249
1250static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
1251 struct pvr2_v4l2 *vp,
1252 int v4l_type)
1253{
1254 struct usb_device *usbdev;
1255 int mindevnum;
1256 int unit_number;
1257 int *nr_ptr = NULL;
1258 dip->v4lp = vp;
1259
1260 usbdev = pvr2_hdw_get_dev(vp->channel.mc_head->hdw);
1261 dip->v4l_type = v4l_type;
1262 switch (v4l_type) {
1263 case VFL_TYPE_GRABBER:
1264 dip->stream = &vp->channel.mc_head->video_stream;
1265 dip->config = pvr2_config_mpeg;
1266 dip->minor_type = pvr2_v4l_type_video;
1267 nr_ptr = video_nr;
1268 if (!dip->stream) {
1269 pr_err(KBUILD_MODNAME
1270 ": Failed to set up pvrusb2 v4l video dev"
1271 " due to missing stream instance\n");
1272 return;
1273 }
1274 break;
1275 case VFL_TYPE_VBI:
1276 dip->config = pvr2_config_vbi;
1277 dip->minor_type = pvr2_v4l_type_vbi;
1278 nr_ptr = vbi_nr;
1279 break;
1280 case VFL_TYPE_RADIO:
1281 dip->stream = &vp->channel.mc_head->video_stream;
1282 dip->config = pvr2_config_mpeg;
1283 dip->minor_type = pvr2_v4l_type_radio;
1284 nr_ptr = radio_nr;
1285 break;
1286 default:
1287 /* Bail out (this should be impossible) */
1288 pr_err(KBUILD_MODNAME ": Failed to set up pvrusb2 v4l dev"
1289 " due to unrecognized config\n");
1290 return;
1291 }
1292
1293 memcpy(&dip->devbase,&vdev_template,sizeof(vdev_template));
1294 dip->devbase.release = pvr2_video_device_release;
1295
1296 mindevnum = -1;
1297 unit_number = pvr2_hdw_get_unit_number(vp->channel.mc_head->hdw);
1298 if (nr_ptr && (unit_number >= 0) && (unit_number < PVR_NUM)) {
1299 mindevnum = nr_ptr[unit_number];
1300 }
1301 dip->devbase.parent = &usbdev->dev;
1302 if ((video_register_device(&dip->devbase,
1303 dip->v4l_type, mindevnum) < 0) &&
1304 (video_register_device(&dip->devbase,
1305 dip->v4l_type, -1) < 0)) {
1306 pr_err(KBUILD_MODNAME
1307 ": Failed to register pvrusb2 v4l device\n");
1308 }
1309
1310 printk(KERN_INFO "pvrusb2: registered device %s [%s]\n",
1311 video_device_node_name(&dip->devbase),
1312 pvr2_config_get_name(dip->config));
1313
1314 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
1315 dip->minor_type,dip->devbase.minor);
1316}
1317
1318
1319struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
1320{
1321 struct pvr2_v4l2 *vp;
1322
1323 vp = kzalloc(sizeof(*vp),GFP_KERNEL);
1324 if (!vp) return vp;
1325 pvr2_channel_init(&vp->channel,mnp);
1326 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);
1327
1328 vp->channel.check_func = pvr2_v4l2_internal_check;
1329
1330 /* register streams */
1331 vp->dev_video = kzalloc(sizeof(*vp->dev_video),GFP_KERNEL);
1332 if (!vp->dev_video) goto fail;
1333 pvr2_v4l2_dev_init(vp->dev_video,vp,VFL_TYPE_GRABBER);
1334 if (pvr2_hdw_get_input_available(vp->channel.mc_head->hdw) &
1335 (1 << PVR2_CVAL_INPUT_RADIO)) {
1336 vp->dev_radio = kzalloc(sizeof(*vp->dev_radio),GFP_KERNEL);
1337 if (!vp->dev_radio) goto fail;
1338 pvr2_v4l2_dev_init(vp->dev_radio,vp,VFL_TYPE_RADIO);
1339 }
1340
1341 return vp;
1342 fail:
1343 pvr2_trace(PVR2_TRACE_STRUCT,"Failure creating pvr2_v4l2 id=%p",vp);
1344 pvr2_v4l2_destroy_no_lock(vp);
1345 return NULL;
1346}
1347
1348/*
1349 Stuff for Emacs to see, in order to encourage consistent editing style:
1350 *** Local Variables: ***
1351 *** mode: c ***
1352 *** fill-column: 75 ***
1353 *** tab-width: 8 ***
1354 *** c-basic-offset: 8 ***
1355 *** End: ***
1356 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-v4l2.h b/drivers/media/video/pvrusb2/pvrusb2-v4l2.h
new file mode 100644
index 00000000000..34c011a7b10
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-v4l2.h
@@ -0,0 +1,39 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20#ifndef __PVRUSB2_V4L2_H
21#define __PVRUSB2_V4L2_H
22
23#include "pvrusb2-context.h"
24
25struct pvr2_v4l2;
26
27struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *);
28
29#endif /* __PVRUSB2_V4L2_H */
30
31/*
32 Stuff for Emacs to see, in order to encourage consistent editing style:
33 *** Local Variables: ***
34 *** mode: c ***
35 *** fill-column: 75 ***
36 *** tab-width: 8 ***
37 *** c-basic-offset: 8 ***
38 *** End: ***
39 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-video-v4l.c b/drivers/media/video/pvrusb2/pvrusb2-video-v4l.c
new file mode 100644
index 00000000000..2e205c99eb9
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-video-v4l.c
@@ -0,0 +1,114 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22/*
23
24 This source file is specifically designed to interface with the
25 saa711x support that is available in the v4l available starting
26 with linux 2.6.15.
27
28*/
29
30#include "pvrusb2-video-v4l.h"
31
32
33
34#include "pvrusb2-hdw-internal.h"
35#include "pvrusb2-debug.h"
36#include <linux/videodev2.h>
37#include <media/v4l2-common.h>
38#include <media/saa7115.h>
39#include <linux/errno.h>
40
41struct routing_scheme {
42 const int *def;
43 unsigned int cnt;
44};
45
46
47static const int routing_scheme0[] = {
48 [PVR2_CVAL_INPUT_TV] = SAA7115_COMPOSITE4,
49 /* In radio mode, we mute the video, but point at one
50 spot just to stay consistent */
51 [PVR2_CVAL_INPUT_RADIO] = SAA7115_COMPOSITE5,
52 [PVR2_CVAL_INPUT_COMPOSITE] = SAA7115_COMPOSITE5,
53 [PVR2_CVAL_INPUT_SVIDEO] = SAA7115_SVIDEO2,
54};
55
56static const struct routing_scheme routing_def0 = {
57 .def = routing_scheme0,
58 .cnt = ARRAY_SIZE(routing_scheme0),
59};
60
61static const int routing_scheme1[] = {
62 [PVR2_CVAL_INPUT_TV] = SAA7115_COMPOSITE4,
63 [PVR2_CVAL_INPUT_RADIO] = SAA7115_COMPOSITE5,
64 [PVR2_CVAL_INPUT_COMPOSITE] = SAA7115_COMPOSITE3,
65 [PVR2_CVAL_INPUT_SVIDEO] = SAA7115_SVIDEO2, /* or SVIDEO0, it seems */
66};
67
68static const struct routing_scheme routing_def1 = {
69 .def = routing_scheme1,
70 .cnt = ARRAY_SIZE(routing_scheme1),
71};
72
73static const struct routing_scheme *routing_schemes[] = {
74 [PVR2_ROUTING_SCHEME_HAUPPAUGE] = &routing_def0,
75 [PVR2_ROUTING_SCHEME_ONAIR] = &routing_def1,
76};
77
78void pvr2_saa7115_subdev_update(struct pvr2_hdw *hdw, struct v4l2_subdev *sd)
79{
80 if (hdw->input_dirty || hdw->force_dirty) {
81 const struct routing_scheme *sp;
82 unsigned int sid = hdw->hdw_desc->signal_routing_scheme;
83 u32 input;
84
85 pvr2_trace(PVR2_TRACE_CHIPS, "subdev v4l2 set_input(%d)",
86 hdw->input_val);
87
88 sp = (sid < ARRAY_SIZE(routing_schemes)) ?
89 routing_schemes[sid] : NULL;
90 if ((sp == NULL) ||
91 (hdw->input_val < 0) ||
92 (hdw->input_val >= sp->cnt)) {
93 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
94 "*** WARNING *** subdev v4l2 set_input:"
95 " Invalid routing scheme (%u)"
96 " and/or input (%d)",
97 sid, hdw->input_val);
98 return;
99 }
100 input = sp->def[hdw->input_val];
101 sd->ops->video->s_routing(sd, input, 0, 0);
102 }
103}
104
105
106/*
107 Stuff for Emacs to see, in order to encourage consistent editing style:
108 *** Local Variables: ***
109 *** mode: c ***
110 *** fill-column: 70 ***
111 *** tab-width: 8 ***
112 *** c-basic-offset: 8 ***
113 *** End: ***
114 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-video-v4l.h b/drivers/media/video/pvrusb2/pvrusb2-video-v4l.h
new file mode 100644
index 00000000000..3b0bd5db602
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-video-v4l.h
@@ -0,0 +1,48 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#ifndef __PVRUSB2_VIDEO_V4L_H
23#define __PVRUSB2_VIDEO_V4L_H
24
25/*
26
27 This module connects the pvrusb2 driver to the I2C chip level
28 driver which handles device video processing. This interface is
29 used internally by the driver; higher level code should only
30 interact through the interface provided by pvrusb2-hdw.h.
31
32*/
33
34
35#include "pvrusb2-hdw-internal.h"
36void pvr2_saa7115_subdev_update(struct pvr2_hdw *, struct v4l2_subdev *);
37
38#endif /* __PVRUSB2_VIDEO_V4L_H */
39
40/*
41 Stuff for Emacs to see, in order to encourage consistent editing style:
42 *** Local Variables: ***
43 *** mode: c ***
44 *** fill-column: 70 ***
45 *** tab-width: 8 ***
46 *** c-basic-offset: 8 ***
47 *** End: ***
48 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-wm8775.c b/drivers/media/video/pvrusb2/pvrusb2-wm8775.c
new file mode 100644
index 00000000000..3ac8d751a5c
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-wm8775.c
@@ -0,0 +1,70 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22/*
23
24 This source file is specifically designed to interface with the
25 wm8775.
26
27*/
28
29#include "pvrusb2-wm8775.h"
30
31
32#include "pvrusb2-hdw-internal.h"
33#include "pvrusb2-debug.h"
34#include <linux/videodev2.h>
35#include <media/v4l2-common.h>
36#include <linux/errno.h>
37
38void pvr2_wm8775_subdev_update(struct pvr2_hdw *hdw, struct v4l2_subdev *sd)
39{
40 if (hdw->input_dirty || hdw->force_dirty) {
41 u32 input;
42
43 switch (hdw->input_val) {
44 case PVR2_CVAL_INPUT_RADIO:
45 input = 1;
46 break;
47 default:
48 /* All other cases just use the second input */
49 input = 2;
50 break;
51 }
52 pvr2_trace(PVR2_TRACE_CHIPS, "subdev wm8775"
53 " set_input(val=%d route=0x%x)",
54 hdw->input_val, input);
55
56 sd->ops->audio->s_routing(sd, input, 0, 0);
57 }
58}
59
60
61
62/*
63 Stuff for Emacs to see, in order to encourage consistent editing style:
64 *** Local Variables: ***
65 *** mode: c ***
66 *** fill-column: 70 ***
67 *** tab-width: 8 ***
68 *** c-basic-offset: 8 ***
69 *** End: ***
70 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2-wm8775.h b/drivers/media/video/pvrusb2/pvrusb2-wm8775.h
new file mode 100644
index 00000000000..0577bc7246f
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2-wm8775.h
@@ -0,0 +1,52 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#ifndef __PVRUSB2_WM8775_H
23#define __PVRUSB2_WM8775_H
24
25/*
26
27 This module connects the pvrusb2 driver to the I2C chip level
28 driver which performs analog -> digital audio conversion for
29 external audio inputs. This interface is used internally by the
30 driver; higher level code should only interact through the
31 interface provided by pvrusb2-hdw.h.
32
33*/
34
35
36
37#include "pvrusb2-hdw-internal.h"
38
39void pvr2_wm8775_subdev_update(struct pvr2_hdw *, struct v4l2_subdev *sd);
40
41
42#endif /* __PVRUSB2_WM8775_H */
43
44/*
45 Stuff for Emacs to see, in order to encourage consistent editing style:
46 *** Local Variables: ***
47 *** mode: c ***
48 *** fill-column: 70 ***
49 *** tab-width: 8 ***
50 *** c-basic-offset: 8 ***
51 *** End: ***
52 */
diff --git a/drivers/media/video/pvrusb2/pvrusb2.h b/drivers/media/video/pvrusb2/pvrusb2.h
new file mode 100644
index 00000000000..240de9b3566
--- /dev/null
+++ b/drivers/media/video/pvrusb2/pvrusb2.h
@@ -0,0 +1,42 @@
1/*
2 *
3 *
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#ifndef __PVRUSB2_H
23#define __PVRUSB2_H
24
25/* Maximum number of pvrusb2 instances we can track at once. You
26 might want to increase this - however the driver operation will not
27 be impaired if it is too small. Instead additional units just
28 won't have an ID assigned and it might not be possible to specify
29 module parameters for those extra units. */
30#define PVR_NUM 20
31
32#endif /* __PVRUSB2_H */
33
34/*
35 Stuff for Emacs to see, in order to encourage consistent editing style:
36 *** Local Variables: ***
37 *** mode: c ***
38 *** fill-column: 70 ***
39 *** tab-width: 8 ***
40 *** c-basic-offset: 8 ***
41 *** End: ***
42 */