1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Abilis Systems Single DVB-T Receiver
4 * Copyright (C) 2008 Pierrick Hascoet <pierrick.hascoet@abilis.com>
5 * Copyright (C) 2010 Devin Heitmueller <dheitmueller@kernellabs.com>
7 #include <linux/kernel.h>
8 #include <linux/errno.h>
9 #include <linux/slab.h>
10 #include <linux/module.h>
12 #include <linux/kref.h>
13 #include <linux/uaccess.h>
14 #include <linux/usb.h>
16 /* header file for usb device driver*/
17 #include "as102_drv.h"
18 #include "as10x_cmd.h"
21 #include <media/dvbdev.h>
24 module_param_named(dual_tuner
, dual_tuner
, int, 0644);
25 MODULE_PARM_DESC(dual_tuner
, "Activate Dual-Tuner config (default: off)");
27 static int fw_upload
= 1;
28 module_param_named(fw_upload
, fw_upload
, int, 0644);
29 MODULE_PARM_DESC(fw_upload
, "Turn on/off default FW upload (default: on)");
31 static int pid_filtering
;
32 module_param_named(pid_filtering
, pid_filtering
, int, 0644);
33 MODULE_PARM_DESC(pid_filtering
, "Activate HW PID filtering (default: off)");
35 static int ts_auto_disable
;
36 module_param_named(ts_auto_disable
, ts_auto_disable
, int, 0644);
37 MODULE_PARM_DESC(ts_auto_disable
, "Stream Auto Enable on FW (default: off)");
40 module_param_named(elna_enable
, elna_enable
, int, 0644);
41 MODULE_PARM_DESC(elna_enable
, "Activate eLNA (default: on)");
43 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr
);
45 static void as102_stop_stream(struct as102_dev_t
*dev
)
47 struct as10x_bus_adapter_t
*bus_adap
;
50 bus_adap
= &dev
->bus_adap
;
54 if (bus_adap
->ops
->stop_stream
!= NULL
)
55 bus_adap
->ops
->stop_stream(dev
);
57 if (ts_auto_disable
) {
58 if (mutex_lock_interruptible(&dev
->bus_adap
.lock
))
61 if (as10x_cmd_stop_streaming(bus_adap
) < 0)
62 dev_dbg(&dev
->bus_adap
.usb_dev
->dev
,
63 "as10x_cmd_stop_streaming failed\n");
65 mutex_unlock(&dev
->bus_adap
.lock
);
69 static int as102_start_stream(struct as102_dev_t
*dev
)
71 struct as10x_bus_adapter_t
*bus_adap
;
75 bus_adap
= &dev
->bus_adap
;
79 if (bus_adap
->ops
->start_stream
!= NULL
)
80 ret
= bus_adap
->ops
->start_stream(dev
);
82 if (ts_auto_disable
) {
83 if (mutex_lock_interruptible(&dev
->bus_adap
.lock
))
86 ret
= as10x_cmd_start_streaming(bus_adap
);
88 mutex_unlock(&dev
->bus_adap
.lock
);
94 static int as10x_pid_filter(struct as102_dev_t
*dev
,
95 int index
, u16 pid
, int onoff
) {
97 struct as10x_bus_adapter_t
*bus_adap
= &dev
->bus_adap
;
100 if (mutex_lock_interruptible(&dev
->bus_adap
.lock
)) {
101 dev_dbg(&dev
->bus_adap
.usb_dev
->dev
,
102 "amutex_lock_interruptible(lock) failed !\n");
108 ret
= as10x_cmd_del_PID_filter(bus_adap
, (uint16_t) pid
);
109 dev_dbg(&dev
->bus_adap
.usb_dev
->dev
,
110 "DEL_PID_FILTER([%02d] 0x%04x) ret = %d\n",
115 struct as10x_ts_filter filter
;
117 filter
.type
= TS_PID_TYPE_TS
;
121 ret
= as10x_cmd_add_PID_filter(bus_adap
, &filter
);
122 dev_dbg(&dev
->bus_adap
.usb_dev
->dev
,
123 "ADD_PID_FILTER([%02d -> %02d], 0x%04x) ret = %d\n",
124 index
, filter
.idx
, filter
.pid
, ret
);
129 mutex_unlock(&dev
->bus_adap
.lock
);
133 static int as102_dvb_dmx_start_feed(struct dvb_demux_feed
*dvbdmxfeed
)
136 struct dvb_demux
*demux
= dvbdmxfeed
->demux
;
137 struct as102_dev_t
*as102_dev
= demux
->priv
;
139 if (mutex_lock_interruptible(&as102_dev
->sem
))
143 as10x_pid_filter(as102_dev
, dvbdmxfeed
->index
,
146 if (as102_dev
->streaming
++ == 0)
147 ret
= as102_start_stream(as102_dev
);
149 mutex_unlock(&as102_dev
->sem
);
153 static int as102_dvb_dmx_stop_feed(struct dvb_demux_feed
*dvbdmxfeed
)
155 struct dvb_demux
*demux
= dvbdmxfeed
->demux
;
156 struct as102_dev_t
*as102_dev
= demux
->priv
;
158 if (mutex_lock_interruptible(&as102_dev
->sem
))
161 if (--as102_dev
->streaming
== 0)
162 as102_stop_stream(as102_dev
);
165 as10x_pid_filter(as102_dev
, dvbdmxfeed
->index
,
168 mutex_unlock(&as102_dev
->sem
);
172 static int as102_set_tune(void *priv
, struct as10x_tune_args
*tune_args
)
174 struct as10x_bus_adapter_t
*bus_adap
= priv
;
177 /* Set frontend arguments */
178 if (mutex_lock_interruptible(&bus_adap
->lock
))
181 ret
= as10x_cmd_set_tune(bus_adap
, tune_args
);
183 dev_dbg(&bus_adap
->usb_dev
->dev
,
184 "as10x_cmd_set_tune failed. (err = %d)\n", ret
);
186 mutex_unlock(&bus_adap
->lock
);
191 static int as102_get_tps(void *priv
, struct as10x_tps
*tps
)
193 struct as10x_bus_adapter_t
*bus_adap
= priv
;
196 if (mutex_lock_interruptible(&bus_adap
->lock
))
199 /* send abilis command: GET_TPS */
200 ret
= as10x_cmd_get_tps(bus_adap
, tps
);
202 mutex_unlock(&bus_adap
->lock
);
207 static int as102_get_status(void *priv
, struct as10x_tune_status
*tstate
)
209 struct as10x_bus_adapter_t
*bus_adap
= priv
;
212 if (mutex_lock_interruptible(&bus_adap
->lock
))
215 /* send abilis command: GET_TUNE_STATUS */
216 ret
= as10x_cmd_get_tune_status(bus_adap
, tstate
);
218 dev_dbg(&bus_adap
->usb_dev
->dev
,
219 "as10x_cmd_get_tune_status failed (err = %d)\n",
223 mutex_unlock(&bus_adap
->lock
);
228 static int as102_get_stats(void *priv
, struct as10x_demod_stats
*demod_stats
)
230 struct as10x_bus_adapter_t
*bus_adap
= priv
;
233 if (mutex_lock_interruptible(&bus_adap
->lock
))
236 /* send abilis command: GET_TUNE_STATUS */
237 ret
= as10x_cmd_get_demod_stats(bus_adap
, demod_stats
);
239 dev_dbg(&bus_adap
->usb_dev
->dev
,
240 "as10x_cmd_get_demod_stats failed (probably not tuned)\n");
242 dev_dbg(&bus_adap
->usb_dev
->dev
,
243 "demod status: fc: 0x%08x, bad fc: 0x%08x, bytes corrected: 0x%08x , MER: 0x%04x\n",
244 demod_stats
->frame_count
,
245 demod_stats
->bad_frame_count
,
246 demod_stats
->bytes_fixed_by_rs
,
249 mutex_unlock(&bus_adap
->lock
);
254 static int as102_stream_ctrl(void *priv
, int acquire
, uint32_t elna_cfg
)
256 struct as10x_bus_adapter_t
*bus_adap
= priv
;
259 if (mutex_lock_interruptible(&bus_adap
->lock
))
264 as10x_cmd_set_context(bus_adap
,
265 CONTEXT_LNA
, elna_cfg
);
267 ret
= as10x_cmd_turn_on(bus_adap
);
269 ret
= as10x_cmd_turn_off(bus_adap
);
272 mutex_unlock(&bus_adap
->lock
);
277 static const struct as102_fe_ops as102_fe_ops
= {
278 .set_tune
= as102_set_tune
,
279 .get_tps
= as102_get_tps
,
280 .get_status
= as102_get_status
,
281 .get_stats
= as102_get_stats
,
282 .stream_ctrl
= as102_stream_ctrl
,
285 int as102_dvb_register(struct as102_dev_t
*as102_dev
)
287 struct device
*dev
= &as102_dev
->bus_adap
.usb_dev
->dev
;
290 ret
= dvb_register_adapter(&as102_dev
->dvb_adap
,
291 as102_dev
->name
, THIS_MODULE
,
294 dev_err(dev
, "%s: dvb_register_adapter() failed: %d\n",
299 as102_dev
->dvb_dmx
.priv
= as102_dev
;
300 as102_dev
->dvb_dmx
.filternum
= pid_filtering
? 16 : 256;
301 as102_dev
->dvb_dmx
.feednum
= 256;
302 as102_dev
->dvb_dmx
.start_feed
= as102_dvb_dmx_start_feed
;
303 as102_dev
->dvb_dmx
.stop_feed
= as102_dvb_dmx_stop_feed
;
305 as102_dev
->dvb_dmx
.dmx
.capabilities
= DMX_TS_FILTERING
|
306 DMX_SECTION_FILTERING
;
308 as102_dev
->dvb_dmxdev
.filternum
= as102_dev
->dvb_dmx
.filternum
;
309 as102_dev
->dvb_dmxdev
.demux
= &as102_dev
->dvb_dmx
.dmx
;
310 as102_dev
->dvb_dmxdev
.capabilities
= 0;
312 ret
= dvb_dmx_init(&as102_dev
->dvb_dmx
);
314 dev_err(dev
, "%s: dvb_dmx_init() failed: %d\n", __func__
, ret
);
318 ret
= dvb_dmxdev_init(&as102_dev
->dvb_dmxdev
, &as102_dev
->dvb_adap
);
320 dev_err(dev
, "%s: dvb_dmxdev_init() failed: %d\n",
325 /* Attach the frontend */
326 as102_dev
->dvb_fe
= dvb_attach(as102_attach
, as102_dev
->name
,
328 &as102_dev
->bus_adap
,
329 as102_dev
->elna_cfg
);
330 if (!as102_dev
->dvb_fe
) {
332 dev_err(dev
, "%s: as102_attach() failed: %d",
337 ret
= dvb_register_frontend(&as102_dev
->dvb_adap
, as102_dev
->dvb_fe
);
339 dev_err(dev
, "%s: as102_dvb_register_frontend() failed: %d",
344 /* init bus mutex for token locking */
345 mutex_init(&as102_dev
->bus_adap
.lock
);
347 /* init start / stop stream mutex */
348 mutex_init(&as102_dev
->sem
);
351 * try to load as102 firmware. If firmware upload failed, we'll be
352 * able to upload it later.
355 try_then_request_module(as102_fw_upload(&as102_dev
->bus_adap
),
358 pr_info("Registered device %s", as102_dev
->name
);
362 dvb_dmxdev_release(&as102_dev
->dvb_dmxdev
);
364 dvb_dmx_release(&as102_dev
->dvb_dmx
);
366 dvb_unregister_adapter(&as102_dev
->dvb_adap
);
370 void as102_dvb_unregister(struct as102_dev_t
*as102_dev
)
372 /* unregister as102 frontend */
373 dvb_unregister_frontend(as102_dev
->dvb_fe
);
375 /* detach frontend */
376 dvb_frontend_detach(as102_dev
->dvb_fe
);
378 /* unregister demux device */
379 dvb_dmxdev_release(&as102_dev
->dvb_dmxdev
);
380 dvb_dmx_release(&as102_dev
->dvb_dmx
);
382 /* unregister dvb adapter */
383 dvb_unregister_adapter(&as102_dev
->dvb_adap
);
385 pr_info("Unregistered device %s", as102_dev
->name
);
388 module_usb_driver(as102_usb_driver
);
390 /* modinfo details */
391 MODULE_DESCRIPTION(DRIVER_FULL_NAME
);
392 MODULE_LICENSE("GPL");
393 MODULE_AUTHOR("Pierrick Hascoet <pierrick.hascoet@abilis.com>");