1 /*
2 * Abilis Systems Single DVB-T Receiver
3 * Copyright (C) 2008 Pierrick Hascoet <pierrick.hascoet@abilis.com>
4 * Copyright (C) 2010 Devin Heitmueller <dheitmueller@kernellabs.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, or (at your option)
9 * any later version.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20 #include <linux/kernel.h>
21 #include <linux/errno.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/module.h>
25 #include <linux/mm.h>
26 #include <linux/kref.h>
27 #include <linux/uaccess.h>
28 #include <linux/usb.h>
29
30 /* header file for usb device driver*/
31 #include "as102_drv.h"
32 #include "as102_fw.h"
33 #include "dvbdev.h"
34
35 int as102_debug;
36 module_param_named(debug, as102_debug, int, 0644);
37 MODULE_PARM_DESC(debug, "Turn on/off debugging (default: off)");
38
39 int dual_tuner;
40 module_param_named(dual_tuner, dual_tuner, int, 0644);
41 MODULE_PARM_DESC(dual_tuner, "Activate Dual-Tuner config (default: off)");
42
43 static int fw_upload = 1;
44 module_param_named(fw_upload, fw_upload, int, 0644);
45 MODULE_PARM_DESC(fw_upload, "Turn on/off default FW upload (default: on)");
46
47 static int pid_filtering;
48 module_param_named(pid_filtering, pid_filtering, int, 0644);
49 MODULE_PARM_DESC(pid_filtering, "Activate HW PID filtering (default: off)");
50
51 static int ts_auto_disable;
52 module_param_named(ts_auto_disable, ts_auto_disable, int, 0644);
53 MODULE_PARM_DESC(ts_auto_disable, "Stream Auto Enable on FW (default: off)");
54
55 int elna_enable = 1;
56 module_param_named(elna_enable, elna_enable, int, 0644);
57 MODULE_PARM_DESC(elna_enable, "Activate eLNA (default: on)");
58
59 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
60
as102_stop_stream(struct as102_dev_t * dev)61 static void as102_stop_stream(struct as102_dev_t *dev)
62 {
63 struct as10x_bus_adapter_t *bus_adap;
64
65 if (dev != NULL)
66 bus_adap = &dev->bus_adap;
67 else
68 return;
69
70 if (bus_adap->ops->stop_stream != NULL)
71 bus_adap->ops->stop_stream(dev);
72
73 if (ts_auto_disable) {
74 if (mutex_lock_interruptible(&dev->bus_adap.lock))
75 return;
76
77 if (as10x_cmd_stop_streaming(bus_adap) < 0)
78 dprintk(debug, "as10x_cmd_stop_streaming failed\n");
79
80 mutex_unlock(&dev->bus_adap.lock);
81 }
82 }
83
as102_start_stream(struct as102_dev_t * dev)84 static int as102_start_stream(struct as102_dev_t *dev)
85 {
86 struct as10x_bus_adapter_t *bus_adap;
87 int ret = -EFAULT;
88
89 if (dev != NULL)
90 bus_adap = &dev->bus_adap;
91 else
92 return ret;
93
94 if (bus_adap->ops->start_stream != NULL)
95 ret = bus_adap->ops->start_stream(dev);
96
97 if (ts_auto_disable) {
98 if (mutex_lock_interruptible(&dev->bus_adap.lock))
99 return -EFAULT;
100
101 ret = as10x_cmd_start_streaming(bus_adap);
102
103 mutex_unlock(&dev->bus_adap.lock);
104 }
105
106 return ret;
107 }
108
as10x_pid_filter(struct as102_dev_t * dev,int index,u16 pid,int onoff)109 static int as10x_pid_filter(struct as102_dev_t *dev,
110 int index, u16 pid, int onoff) {
111
112 struct as10x_bus_adapter_t *bus_adap = &dev->bus_adap;
113 int ret = -EFAULT;
114
115 ENTER();
116
117 if (mutex_lock_interruptible(&dev->bus_adap.lock)) {
118 dprintk(debug, "mutex_lock_interruptible(lock) failed !\n");
119 return -EBUSY;
120 }
121
122 switch (onoff) {
123 case 0:
124 ret = as10x_cmd_del_PID_filter(bus_adap, (uint16_t) pid);
125 dprintk(debug, "DEL_PID_FILTER([%02d] 0x%04x) ret = %d\n",
126 index, pid, ret);
127 break;
128 case 1:
129 {
130 struct as10x_ts_filter filter;
131
132 filter.type = TS_PID_TYPE_TS;
133 filter.idx = 0xFF;
134 filter.pid = pid;
135
136 ret = as10x_cmd_add_PID_filter(bus_adap, &filter);
137 dprintk(debug, "ADD_PID_FILTER([%02d -> %02d], 0x%04x) ret = %d\n",
138 index, filter.idx, filter.pid, ret);
139 break;
140 }
141 }
142
143 mutex_unlock(&dev->bus_adap.lock);
144
145 LEAVE();
146 return ret;
147 }
148
as102_dvb_dmx_start_feed(struct dvb_demux_feed * dvbdmxfeed)149 static int as102_dvb_dmx_start_feed(struct dvb_demux_feed *dvbdmxfeed)
150 {
151 int ret = 0;
152 struct dvb_demux *demux = dvbdmxfeed->demux;
153 struct as102_dev_t *as102_dev = demux->priv;
154
155 ENTER();
156
157 if (mutex_lock_interruptible(&as102_dev->sem))
158 return -ERESTARTSYS;
159
160 if (pid_filtering)
161 as10x_pid_filter(as102_dev, dvbdmxfeed->index,
162 dvbdmxfeed->pid, 1);
163
164 if (as102_dev->streaming++ == 0)
165 ret = as102_start_stream(as102_dev);
166
167 mutex_unlock(&as102_dev->sem);
168 LEAVE();
169 return ret;
170 }
171
as102_dvb_dmx_stop_feed(struct dvb_demux_feed * dvbdmxfeed)172 static int as102_dvb_dmx_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
173 {
174 struct dvb_demux *demux = dvbdmxfeed->demux;
175 struct as102_dev_t *as102_dev = demux->priv;
176
177 ENTER();
178
179 if (mutex_lock_interruptible(&as102_dev->sem))
180 return -ERESTARTSYS;
181
182 if (--as102_dev->streaming == 0)
183 as102_stop_stream(as102_dev);
184
185 if (pid_filtering)
186 as10x_pid_filter(as102_dev, dvbdmxfeed->index,
187 dvbdmxfeed->pid, 0);
188
189 mutex_unlock(&as102_dev->sem);
190 LEAVE();
191 return 0;
192 }
193
as102_dvb_register(struct as102_dev_t * as102_dev)194 int as102_dvb_register(struct as102_dev_t *as102_dev)
195 {
196 struct device *dev = &as102_dev->bus_adap.usb_dev->dev;
197 int ret;
198
199 ret = dvb_register_adapter(&as102_dev->dvb_adap,
200 as102_dev->name, THIS_MODULE,
201 dev, adapter_nr);
202 if (ret < 0) {
203 dev_err(dev, "%s: dvb_register_adapter() failed: %d\n",
204 __func__, ret);
205 return ret;
206 }
207
208 as102_dev->dvb_dmx.priv = as102_dev;
209 as102_dev->dvb_dmx.filternum = pid_filtering ? 16 : 256;
210 as102_dev->dvb_dmx.feednum = 256;
211 as102_dev->dvb_dmx.start_feed = as102_dvb_dmx_start_feed;
212 as102_dev->dvb_dmx.stop_feed = as102_dvb_dmx_stop_feed;
213
214 as102_dev->dvb_dmx.dmx.capabilities = DMX_TS_FILTERING |
215 DMX_SECTION_FILTERING;
216
217 as102_dev->dvb_dmxdev.filternum = as102_dev->dvb_dmx.filternum;
218 as102_dev->dvb_dmxdev.demux = &as102_dev->dvb_dmx.dmx;
219 as102_dev->dvb_dmxdev.capabilities = 0;
220
221 ret = dvb_dmx_init(&as102_dev->dvb_dmx);
222 if (ret < 0) {
223 dev_err(dev, "%s: dvb_dmx_init() failed: %d\n", __func__, ret);
224 goto edmxinit;
225 }
226
227 ret = dvb_dmxdev_init(&as102_dev->dvb_dmxdev, &as102_dev->dvb_adap);
228 if (ret < 0) {
229 dev_err(dev, "%s: dvb_dmxdev_init() failed: %d\n",
230 __func__, ret);
231 goto edmxdinit;
232 }
233
234 ret = as102_dvb_register_fe(as102_dev, &as102_dev->dvb_fe);
235 if (ret < 0) {
236 dev_err(dev, "%s: as102_dvb_register_frontend() failed: %d",
237 __func__, ret);
238 goto efereg;
239 }
240
241 /* init bus mutex for token locking */
242 mutex_init(&as102_dev->bus_adap.lock);
243
244 /* init start / stop stream mutex */
245 mutex_init(&as102_dev->sem);
246
247 /*
248 * try to load as102 firmware. If firmware upload failed, we'll be
249 * able to upload it later.
250 */
251 if (fw_upload)
252 try_then_request_module(as102_fw_upload(&as102_dev->bus_adap),
253 "firmware_class");
254
255 pr_info("Registered device %s", as102_dev->name);
256 return 0;
257
258 efereg:
259 dvb_dmxdev_release(&as102_dev->dvb_dmxdev);
260 edmxdinit:
261 dvb_dmx_release(&as102_dev->dvb_dmx);
262 edmxinit:
263 dvb_unregister_adapter(&as102_dev->dvb_adap);
264 return ret;
265 }
266
as102_dvb_unregister(struct as102_dev_t * as102_dev)267 void as102_dvb_unregister(struct as102_dev_t *as102_dev)
268 {
269 /* unregister as102 frontend */
270 as102_dvb_unregister_fe(&as102_dev->dvb_fe);
271
272 /* unregister demux device */
273 dvb_dmxdev_release(&as102_dev->dvb_dmxdev);
274 dvb_dmx_release(&as102_dev->dvb_dmx);
275
276 /* unregister dvb adapter */
277 dvb_unregister_adapter(&as102_dev->dvb_adap);
278
279 pr_info("Unregistered device %s", as102_dev->name);
280 }
281
as102_driver_init(void)282 static int __init as102_driver_init(void)
283 {
284 int ret;
285
286 /* register this driver with the low level subsystem */
287 ret = usb_register(&as102_usb_driver);
288 if (ret)
289 err("usb_register failed (ret = %d)", ret);
290
291 return ret;
292 }
293
294 /*
295 * Mandatory function : Adds a special section to the module indicating
296 * where initialisation function is defined
297 */
298 module_init(as102_driver_init);
299
300 /**
301 * as102_driver_exit - as102 driver exit point
302 *
303 * This function is called when device has to be removed.
304 */
as102_driver_exit(void)305 static void __exit as102_driver_exit(void)
306 {
307 /* deregister this driver with the low level bus subsystem */
308 usb_deregister(&as102_usb_driver);
309 }
310
311 /*
312 * required function for unload: Adds a special section to the module
313 * indicating where unload function is defined
314 */
315 module_exit(as102_driver_exit);
316 /* modinfo details */
317 MODULE_DESCRIPTION(DRIVER_FULL_NAME);
318 MODULE_LICENSE("GPL");
319 MODULE_AUTHOR("Pierrick Hascoet <pierrick.hascoet@abilis.com>");
320