xref: /linux/drivers/media/usb/as102/as102_drv.c (revision 47f79129ecf5fd12d847c2e63b6ce39d1df4a648)
1d6317c68SMauro Carvalho Chehab /*
2d6317c68SMauro Carvalho Chehab  * Abilis Systems Single DVB-T Receiver
3d6317c68SMauro Carvalho Chehab  * Copyright (C) 2008 Pierrick Hascoet <pierrick.hascoet@abilis.com>
4d6317c68SMauro Carvalho Chehab  * Copyright (C) 2010 Devin Heitmueller <dheitmueller@kernellabs.com>
5d6317c68SMauro Carvalho Chehab  *
6d6317c68SMauro Carvalho Chehab  * This program is free software; you can redistribute it and/or modify
7d6317c68SMauro Carvalho Chehab  * it under the terms of the GNU General Public License as published by
8d6317c68SMauro Carvalho Chehab  * the Free Software Foundation; either version 2, or (at your option)
9d6317c68SMauro Carvalho Chehab  * any later version.
10d6317c68SMauro Carvalho Chehab  *
11d6317c68SMauro Carvalho Chehab  * This program is distributed in the hope that it will be useful,
12d6317c68SMauro Carvalho Chehab  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13d6317c68SMauro Carvalho Chehab  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14d6317c68SMauro Carvalho Chehab  * GNU General Public License for more details.
15d6317c68SMauro Carvalho Chehab  */
16d6317c68SMauro Carvalho Chehab #include <linux/kernel.h>
17d6317c68SMauro Carvalho Chehab #include <linux/errno.h>
18d6317c68SMauro Carvalho Chehab #include <linux/slab.h>
19d6317c68SMauro Carvalho Chehab #include <linux/module.h>
20d6317c68SMauro Carvalho Chehab #include <linux/mm.h>
21d6317c68SMauro Carvalho Chehab #include <linux/kref.h>
22d6317c68SMauro Carvalho Chehab #include <linux/uaccess.h>
23d6317c68SMauro Carvalho Chehab #include <linux/usb.h>
24d6317c68SMauro Carvalho Chehab 
25d6317c68SMauro Carvalho Chehab /* header file for usb device driver*/
26d6317c68SMauro Carvalho Chehab #include "as102_drv.h"
27*47f79129SMauro Carvalho Chehab #include "as10x_cmd.h"
28*47f79129SMauro Carvalho Chehab #include "as102_fe.h"
29d6317c68SMauro Carvalho Chehab #include "as102_fw.h"
30d6317c68SMauro Carvalho Chehab #include "dvbdev.h"
31d6317c68SMauro Carvalho Chehab 
32d6317c68SMauro Carvalho Chehab int dual_tuner;
33d6317c68SMauro Carvalho Chehab module_param_named(dual_tuner, dual_tuner, int, 0644);
34d6317c68SMauro Carvalho Chehab MODULE_PARM_DESC(dual_tuner, "Activate Dual-Tuner config (default: off)");
35d6317c68SMauro Carvalho Chehab 
36d6317c68SMauro Carvalho Chehab static int fw_upload = 1;
37d6317c68SMauro Carvalho Chehab module_param_named(fw_upload, fw_upload, int, 0644);
38d6317c68SMauro Carvalho Chehab MODULE_PARM_DESC(fw_upload, "Turn on/off default FW upload (default: on)");
39d6317c68SMauro Carvalho Chehab 
40d6317c68SMauro Carvalho Chehab static int pid_filtering;
41d6317c68SMauro Carvalho Chehab module_param_named(pid_filtering, pid_filtering, int, 0644);
42d6317c68SMauro Carvalho Chehab MODULE_PARM_DESC(pid_filtering, "Activate HW PID filtering (default: off)");
43d6317c68SMauro Carvalho Chehab 
44d6317c68SMauro Carvalho Chehab static int ts_auto_disable;
45d6317c68SMauro Carvalho Chehab module_param_named(ts_auto_disable, ts_auto_disable, int, 0644);
46d6317c68SMauro Carvalho Chehab MODULE_PARM_DESC(ts_auto_disable, "Stream Auto Enable on FW (default: off)");
47d6317c68SMauro Carvalho Chehab 
48d6317c68SMauro Carvalho Chehab int elna_enable = 1;
49d6317c68SMauro Carvalho Chehab module_param_named(elna_enable, elna_enable, int, 0644);
50d6317c68SMauro Carvalho Chehab MODULE_PARM_DESC(elna_enable, "Activate eLNA (default: on)");
51d6317c68SMauro Carvalho Chehab 
52d6317c68SMauro Carvalho Chehab DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
53d6317c68SMauro Carvalho Chehab 
54d6317c68SMauro Carvalho Chehab static void as102_stop_stream(struct as102_dev_t *dev)
55d6317c68SMauro Carvalho Chehab {
56d6317c68SMauro Carvalho Chehab 	struct as10x_bus_adapter_t *bus_adap;
57d6317c68SMauro Carvalho Chehab 
58d6317c68SMauro Carvalho Chehab 	if (dev != NULL)
59d6317c68SMauro Carvalho Chehab 		bus_adap = &dev->bus_adap;
60d6317c68SMauro Carvalho Chehab 	else
61d6317c68SMauro Carvalho Chehab 		return;
62d6317c68SMauro Carvalho Chehab 
63d6317c68SMauro Carvalho Chehab 	if (bus_adap->ops->stop_stream != NULL)
64d6317c68SMauro Carvalho Chehab 		bus_adap->ops->stop_stream(dev);
65d6317c68SMauro Carvalho Chehab 
66d6317c68SMauro Carvalho Chehab 	if (ts_auto_disable) {
67d6317c68SMauro Carvalho Chehab 		if (mutex_lock_interruptible(&dev->bus_adap.lock))
68d6317c68SMauro Carvalho Chehab 			return;
69d6317c68SMauro Carvalho Chehab 
70d6317c68SMauro Carvalho Chehab 		if (as10x_cmd_stop_streaming(bus_adap) < 0)
71d6317c68SMauro Carvalho Chehab 			dev_dbg(&dev->bus_adap.usb_dev->dev,
72d6317c68SMauro Carvalho Chehab 				"as10x_cmd_stop_streaming failed\n");
73d6317c68SMauro Carvalho Chehab 
74d6317c68SMauro Carvalho Chehab 		mutex_unlock(&dev->bus_adap.lock);
75d6317c68SMauro Carvalho Chehab 	}
76d6317c68SMauro Carvalho Chehab }
77d6317c68SMauro Carvalho Chehab 
78d6317c68SMauro Carvalho Chehab static int as102_start_stream(struct as102_dev_t *dev)
79d6317c68SMauro Carvalho Chehab {
80d6317c68SMauro Carvalho Chehab 	struct as10x_bus_adapter_t *bus_adap;
81d6317c68SMauro Carvalho Chehab 	int ret = -EFAULT;
82d6317c68SMauro Carvalho Chehab 
83d6317c68SMauro Carvalho Chehab 	if (dev != NULL)
84d6317c68SMauro Carvalho Chehab 		bus_adap = &dev->bus_adap;
85d6317c68SMauro Carvalho Chehab 	else
86d6317c68SMauro Carvalho Chehab 		return ret;
87d6317c68SMauro Carvalho Chehab 
88d6317c68SMauro Carvalho Chehab 	if (bus_adap->ops->start_stream != NULL)
89d6317c68SMauro Carvalho Chehab 		ret = bus_adap->ops->start_stream(dev);
90d6317c68SMauro Carvalho Chehab 
91d6317c68SMauro Carvalho Chehab 	if (ts_auto_disable) {
92d6317c68SMauro Carvalho Chehab 		if (mutex_lock_interruptible(&dev->bus_adap.lock))
93d6317c68SMauro Carvalho Chehab 			return -EFAULT;
94d6317c68SMauro Carvalho Chehab 
95d6317c68SMauro Carvalho Chehab 		ret = as10x_cmd_start_streaming(bus_adap);
96d6317c68SMauro Carvalho Chehab 
97d6317c68SMauro Carvalho Chehab 		mutex_unlock(&dev->bus_adap.lock);
98d6317c68SMauro Carvalho Chehab 	}
99d6317c68SMauro Carvalho Chehab 
100d6317c68SMauro Carvalho Chehab 	return ret;
101d6317c68SMauro Carvalho Chehab }
102d6317c68SMauro Carvalho Chehab 
103d6317c68SMauro Carvalho Chehab static int as10x_pid_filter(struct as102_dev_t *dev,
104d6317c68SMauro Carvalho Chehab 			    int index, u16 pid, int onoff) {
105d6317c68SMauro Carvalho Chehab 
106d6317c68SMauro Carvalho Chehab 	struct as10x_bus_adapter_t *bus_adap = &dev->bus_adap;
107d6317c68SMauro Carvalho Chehab 	int ret = -EFAULT;
108d6317c68SMauro Carvalho Chehab 
109d6317c68SMauro Carvalho Chehab 	if (mutex_lock_interruptible(&dev->bus_adap.lock)) {
110d6317c68SMauro Carvalho Chehab 		dev_dbg(&dev->bus_adap.usb_dev->dev,
111d6317c68SMauro Carvalho Chehab 			"amutex_lock_interruptible(lock) failed !\n");
112d6317c68SMauro Carvalho Chehab 		return -EBUSY;
113d6317c68SMauro Carvalho Chehab 	}
114d6317c68SMauro Carvalho Chehab 
115d6317c68SMauro Carvalho Chehab 	switch (onoff) {
116d6317c68SMauro Carvalho Chehab 	case 0:
117d6317c68SMauro Carvalho Chehab 		ret = as10x_cmd_del_PID_filter(bus_adap, (uint16_t) pid);
118d6317c68SMauro Carvalho Chehab 		dev_dbg(&dev->bus_adap.usb_dev->dev,
119d6317c68SMauro Carvalho Chehab 			"DEL_PID_FILTER([%02d] 0x%04x) ret = %d\n",
120d6317c68SMauro Carvalho Chehab 			index, pid, ret);
121d6317c68SMauro Carvalho Chehab 		break;
122d6317c68SMauro Carvalho Chehab 	case 1:
123d6317c68SMauro Carvalho Chehab 	{
124d6317c68SMauro Carvalho Chehab 		struct as10x_ts_filter filter;
125d6317c68SMauro Carvalho Chehab 
126d6317c68SMauro Carvalho Chehab 		filter.type = TS_PID_TYPE_TS;
127d6317c68SMauro Carvalho Chehab 		filter.idx = 0xFF;
128d6317c68SMauro Carvalho Chehab 		filter.pid = pid;
129d6317c68SMauro Carvalho Chehab 
130d6317c68SMauro Carvalho Chehab 		ret = as10x_cmd_add_PID_filter(bus_adap, &filter);
131d6317c68SMauro Carvalho Chehab 		dev_dbg(&dev->bus_adap.usb_dev->dev,
132d6317c68SMauro Carvalho Chehab 			"ADD_PID_FILTER([%02d -> %02d], 0x%04x) ret = %d\n",
133d6317c68SMauro Carvalho Chehab 			index, filter.idx, filter.pid, ret);
134d6317c68SMauro Carvalho Chehab 		break;
135d6317c68SMauro Carvalho Chehab 	}
136d6317c68SMauro Carvalho Chehab 	}
137d6317c68SMauro Carvalho Chehab 
138d6317c68SMauro Carvalho Chehab 	mutex_unlock(&dev->bus_adap.lock);
139d6317c68SMauro Carvalho Chehab 	return ret;
140d6317c68SMauro Carvalho Chehab }
141d6317c68SMauro Carvalho Chehab 
142d6317c68SMauro Carvalho Chehab static int as102_dvb_dmx_start_feed(struct dvb_demux_feed *dvbdmxfeed)
143d6317c68SMauro Carvalho Chehab {
144d6317c68SMauro Carvalho Chehab 	int ret = 0;
145d6317c68SMauro Carvalho Chehab 	struct dvb_demux *demux = dvbdmxfeed->demux;
146d6317c68SMauro Carvalho Chehab 	struct as102_dev_t *as102_dev = demux->priv;
147d6317c68SMauro Carvalho Chehab 
148d6317c68SMauro Carvalho Chehab 	if (mutex_lock_interruptible(&as102_dev->sem))
149d6317c68SMauro Carvalho Chehab 		return -ERESTARTSYS;
150d6317c68SMauro Carvalho Chehab 
151d6317c68SMauro Carvalho Chehab 	if (pid_filtering)
152d6317c68SMauro Carvalho Chehab 		as10x_pid_filter(as102_dev, dvbdmxfeed->index,
153d6317c68SMauro Carvalho Chehab 				 dvbdmxfeed->pid, 1);
154d6317c68SMauro Carvalho Chehab 
155d6317c68SMauro Carvalho Chehab 	if (as102_dev->streaming++ == 0)
156d6317c68SMauro Carvalho Chehab 		ret = as102_start_stream(as102_dev);
157d6317c68SMauro Carvalho Chehab 
158d6317c68SMauro Carvalho Chehab 	mutex_unlock(&as102_dev->sem);
159d6317c68SMauro Carvalho Chehab 	return ret;
160d6317c68SMauro Carvalho Chehab }
161d6317c68SMauro Carvalho Chehab 
162d6317c68SMauro Carvalho Chehab static int as102_dvb_dmx_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
163d6317c68SMauro Carvalho Chehab {
164d6317c68SMauro Carvalho Chehab 	struct dvb_demux *demux = dvbdmxfeed->demux;
165d6317c68SMauro Carvalho Chehab 	struct as102_dev_t *as102_dev = demux->priv;
166d6317c68SMauro Carvalho Chehab 
167d6317c68SMauro Carvalho Chehab 	if (mutex_lock_interruptible(&as102_dev->sem))
168d6317c68SMauro Carvalho Chehab 		return -ERESTARTSYS;
169d6317c68SMauro Carvalho Chehab 
170d6317c68SMauro Carvalho Chehab 	if (--as102_dev->streaming == 0)
171d6317c68SMauro Carvalho Chehab 		as102_stop_stream(as102_dev);
172d6317c68SMauro Carvalho Chehab 
173d6317c68SMauro Carvalho Chehab 	if (pid_filtering)
174d6317c68SMauro Carvalho Chehab 		as10x_pid_filter(as102_dev, dvbdmxfeed->index,
175d6317c68SMauro Carvalho Chehab 				 dvbdmxfeed->pid, 0);
176d6317c68SMauro Carvalho Chehab 
177d6317c68SMauro Carvalho Chehab 	mutex_unlock(&as102_dev->sem);
178d6317c68SMauro Carvalho Chehab 	return 0;
179d6317c68SMauro Carvalho Chehab }
180d6317c68SMauro Carvalho Chehab 
181*47f79129SMauro Carvalho Chehab static int as102_set_tune(void *priv, struct as10x_tune_args *tune_args)
182*47f79129SMauro Carvalho Chehab {
183*47f79129SMauro Carvalho Chehab 	struct as10x_bus_adapter_t *bus_adap = priv;
184*47f79129SMauro Carvalho Chehab 	int ret;
185*47f79129SMauro Carvalho Chehab 
186*47f79129SMauro Carvalho Chehab 	/* Set frontend arguments */
187*47f79129SMauro Carvalho Chehab 	if (mutex_lock_interruptible(&bus_adap->lock))
188*47f79129SMauro Carvalho Chehab 		return -EBUSY;
189*47f79129SMauro Carvalho Chehab 
190*47f79129SMauro Carvalho Chehab 	ret =  as10x_cmd_set_tune(bus_adap, tune_args);
191*47f79129SMauro Carvalho Chehab 	if (ret != 0)
192*47f79129SMauro Carvalho Chehab 		dev_dbg(&bus_adap->usb_dev->dev,
193*47f79129SMauro Carvalho Chehab 			"as10x_cmd_set_tune failed. (err = %d)\n", ret);
194*47f79129SMauro Carvalho Chehab 
195*47f79129SMauro Carvalho Chehab 	mutex_unlock(&bus_adap->lock);
196*47f79129SMauro Carvalho Chehab 
197*47f79129SMauro Carvalho Chehab 	return ret;
198*47f79129SMauro Carvalho Chehab }
199*47f79129SMauro Carvalho Chehab 
200*47f79129SMauro Carvalho Chehab static int as102_get_tps(void *priv, struct as10x_tps *tps)
201*47f79129SMauro Carvalho Chehab {
202*47f79129SMauro Carvalho Chehab 	struct as10x_bus_adapter_t *bus_adap = priv;
203*47f79129SMauro Carvalho Chehab 	int ret;
204*47f79129SMauro Carvalho Chehab 
205*47f79129SMauro Carvalho Chehab 	if (mutex_lock_interruptible(&bus_adap->lock))
206*47f79129SMauro Carvalho Chehab 		return -EBUSY;
207*47f79129SMauro Carvalho Chehab 
208*47f79129SMauro Carvalho Chehab 	/* send abilis command: GET_TPS */
209*47f79129SMauro Carvalho Chehab 	ret = as10x_cmd_get_tps(bus_adap, tps);
210*47f79129SMauro Carvalho Chehab 
211*47f79129SMauro Carvalho Chehab 	mutex_unlock(&bus_adap->lock);
212*47f79129SMauro Carvalho Chehab 
213*47f79129SMauro Carvalho Chehab 	return ret;
214*47f79129SMauro Carvalho Chehab }
215*47f79129SMauro Carvalho Chehab 
216*47f79129SMauro Carvalho Chehab static int as102_get_status(void *priv, struct as10x_tune_status *tstate)
217*47f79129SMauro Carvalho Chehab {
218*47f79129SMauro Carvalho Chehab 	struct as10x_bus_adapter_t *bus_adap = priv;
219*47f79129SMauro Carvalho Chehab 	int ret;
220*47f79129SMauro Carvalho Chehab 
221*47f79129SMauro Carvalho Chehab 	if (mutex_lock_interruptible(&bus_adap->lock))
222*47f79129SMauro Carvalho Chehab 		return -EBUSY;
223*47f79129SMauro Carvalho Chehab 
224*47f79129SMauro Carvalho Chehab 	/* send abilis command: GET_TUNE_STATUS */
225*47f79129SMauro Carvalho Chehab 	ret = as10x_cmd_get_tune_status(bus_adap, tstate);
226*47f79129SMauro Carvalho Chehab 	if (ret < 0) {
227*47f79129SMauro Carvalho Chehab 		dev_dbg(&bus_adap->usb_dev->dev,
228*47f79129SMauro Carvalho Chehab 			"as10x_cmd_get_tune_status failed (err = %d)\n",
229*47f79129SMauro Carvalho Chehab 			ret);
230*47f79129SMauro Carvalho Chehab 	}
231*47f79129SMauro Carvalho Chehab 
232*47f79129SMauro Carvalho Chehab 	mutex_unlock(&bus_adap->lock);
233*47f79129SMauro Carvalho Chehab 
234*47f79129SMauro Carvalho Chehab 	return ret;
235*47f79129SMauro Carvalho Chehab }
236*47f79129SMauro Carvalho Chehab 
237*47f79129SMauro Carvalho Chehab static int as102_get_stats(void *priv, struct as10x_demod_stats *demod_stats)
238*47f79129SMauro Carvalho Chehab {
239*47f79129SMauro Carvalho Chehab 	struct as10x_bus_adapter_t *bus_adap = priv;
240*47f79129SMauro Carvalho Chehab 	int ret;
241*47f79129SMauro Carvalho Chehab 
242*47f79129SMauro Carvalho Chehab 	if (mutex_lock_interruptible(&bus_adap->lock))
243*47f79129SMauro Carvalho Chehab 		return -EBUSY;
244*47f79129SMauro Carvalho Chehab 
245*47f79129SMauro Carvalho Chehab 	/* send abilis command: GET_TUNE_STATUS */
246*47f79129SMauro Carvalho Chehab 	ret = as10x_cmd_get_demod_stats(bus_adap, demod_stats);
247*47f79129SMauro Carvalho Chehab 	if (ret < 0) {
248*47f79129SMauro Carvalho Chehab 		dev_dbg(&bus_adap->usb_dev->dev,
249*47f79129SMauro Carvalho Chehab 			"as10x_cmd_get_demod_stats failed (probably not tuned)\n");
250*47f79129SMauro Carvalho Chehab 	} else {
251*47f79129SMauro Carvalho Chehab 		dev_dbg(&bus_adap->usb_dev->dev,
252*47f79129SMauro Carvalho Chehab 			"demod status: fc: 0x%08x, bad fc: 0x%08x, bytes corrected: 0x%08x , MER: 0x%04x\n",
253*47f79129SMauro Carvalho Chehab 			demod_stats->frame_count,
254*47f79129SMauro Carvalho Chehab 			demod_stats->bad_frame_count,
255*47f79129SMauro Carvalho Chehab 			demod_stats->bytes_fixed_by_rs,
256*47f79129SMauro Carvalho Chehab 			demod_stats->mer);
257*47f79129SMauro Carvalho Chehab 	}
258*47f79129SMauro Carvalho Chehab 	mutex_unlock(&bus_adap->lock);
259*47f79129SMauro Carvalho Chehab 
260*47f79129SMauro Carvalho Chehab 	return ret;
261*47f79129SMauro Carvalho Chehab }
262*47f79129SMauro Carvalho Chehab 
263*47f79129SMauro Carvalho Chehab static int as102_stream_ctrl(void *priv, int acquire, uint32_t elna_cfg)
264*47f79129SMauro Carvalho Chehab {
265*47f79129SMauro Carvalho Chehab 	struct as10x_bus_adapter_t *bus_adap = priv;
266*47f79129SMauro Carvalho Chehab 	int ret;
267*47f79129SMauro Carvalho Chehab 
268*47f79129SMauro Carvalho Chehab 	if (mutex_lock_interruptible(&bus_adap->lock))
269*47f79129SMauro Carvalho Chehab 		return -EBUSY;
270*47f79129SMauro Carvalho Chehab 
271*47f79129SMauro Carvalho Chehab 	if (acquire) {
272*47f79129SMauro Carvalho Chehab 		if (elna_enable)
273*47f79129SMauro Carvalho Chehab 			as10x_cmd_set_context(bus_adap,
274*47f79129SMauro Carvalho Chehab 					      CONTEXT_LNA, elna_cfg);
275*47f79129SMauro Carvalho Chehab 
276*47f79129SMauro Carvalho Chehab 		ret = as10x_cmd_turn_on(bus_adap);
277*47f79129SMauro Carvalho Chehab 	} else {
278*47f79129SMauro Carvalho Chehab 		ret = as10x_cmd_turn_off(bus_adap);
279*47f79129SMauro Carvalho Chehab 	}
280*47f79129SMauro Carvalho Chehab 
281*47f79129SMauro Carvalho Chehab 	mutex_unlock(&bus_adap->lock);
282*47f79129SMauro Carvalho Chehab 
283*47f79129SMauro Carvalho Chehab 	return ret;
284*47f79129SMauro Carvalho Chehab }
285*47f79129SMauro Carvalho Chehab 
286*47f79129SMauro Carvalho Chehab static const struct as102_fe_ops as102_fe_ops = {
287*47f79129SMauro Carvalho Chehab 	.set_tune = as102_set_tune,
288*47f79129SMauro Carvalho Chehab 	.get_tps  = as102_get_tps,
289*47f79129SMauro Carvalho Chehab 	.get_status = as102_get_status,
290*47f79129SMauro Carvalho Chehab 	.get_stats = as102_get_stats,
291*47f79129SMauro Carvalho Chehab 	.stream_ctrl = as102_stream_ctrl,
292*47f79129SMauro Carvalho Chehab };
293*47f79129SMauro Carvalho Chehab 
294d6317c68SMauro Carvalho Chehab int as102_dvb_register(struct as102_dev_t *as102_dev)
295d6317c68SMauro Carvalho Chehab {
296d6317c68SMauro Carvalho Chehab 	struct device *dev = &as102_dev->bus_adap.usb_dev->dev;
297d6317c68SMauro Carvalho Chehab 	int ret;
298d6317c68SMauro Carvalho Chehab 
299d6317c68SMauro Carvalho Chehab 	ret = dvb_register_adapter(&as102_dev->dvb_adap,
300d6317c68SMauro Carvalho Chehab 			   as102_dev->name, THIS_MODULE,
301d6317c68SMauro Carvalho Chehab 			   dev, adapter_nr);
302d6317c68SMauro Carvalho Chehab 	if (ret < 0) {
303d6317c68SMauro Carvalho Chehab 		dev_err(dev, "%s: dvb_register_adapter() failed: %d\n",
304d6317c68SMauro Carvalho Chehab 			__func__, ret);
305d6317c68SMauro Carvalho Chehab 		return ret;
306d6317c68SMauro Carvalho Chehab 	}
307d6317c68SMauro Carvalho Chehab 
308d6317c68SMauro Carvalho Chehab 	as102_dev->dvb_dmx.priv = as102_dev;
309d6317c68SMauro Carvalho Chehab 	as102_dev->dvb_dmx.filternum = pid_filtering ? 16 : 256;
310d6317c68SMauro Carvalho Chehab 	as102_dev->dvb_dmx.feednum = 256;
311d6317c68SMauro Carvalho Chehab 	as102_dev->dvb_dmx.start_feed = as102_dvb_dmx_start_feed;
312d6317c68SMauro Carvalho Chehab 	as102_dev->dvb_dmx.stop_feed = as102_dvb_dmx_stop_feed;
313d6317c68SMauro Carvalho Chehab 
314d6317c68SMauro Carvalho Chehab 	as102_dev->dvb_dmx.dmx.capabilities = DMX_TS_FILTERING |
315d6317c68SMauro Carvalho Chehab 					      DMX_SECTION_FILTERING;
316d6317c68SMauro Carvalho Chehab 
317d6317c68SMauro Carvalho Chehab 	as102_dev->dvb_dmxdev.filternum = as102_dev->dvb_dmx.filternum;
318d6317c68SMauro Carvalho Chehab 	as102_dev->dvb_dmxdev.demux = &as102_dev->dvb_dmx.dmx;
319d6317c68SMauro Carvalho Chehab 	as102_dev->dvb_dmxdev.capabilities = 0;
320d6317c68SMauro Carvalho Chehab 
321d6317c68SMauro Carvalho Chehab 	ret = dvb_dmx_init(&as102_dev->dvb_dmx);
322d6317c68SMauro Carvalho Chehab 	if (ret < 0) {
323d6317c68SMauro Carvalho Chehab 		dev_err(dev, "%s: dvb_dmx_init() failed: %d\n", __func__, ret);
324d6317c68SMauro Carvalho Chehab 		goto edmxinit;
325d6317c68SMauro Carvalho Chehab 	}
326d6317c68SMauro Carvalho Chehab 
327d6317c68SMauro Carvalho Chehab 	ret = dvb_dmxdev_init(&as102_dev->dvb_dmxdev, &as102_dev->dvb_adap);
328d6317c68SMauro Carvalho Chehab 	if (ret < 0) {
329d6317c68SMauro Carvalho Chehab 		dev_err(dev, "%s: dvb_dmxdev_init() failed: %d\n",
330d6317c68SMauro Carvalho Chehab 			__func__, ret);
331d6317c68SMauro Carvalho Chehab 		goto edmxdinit;
332d6317c68SMauro Carvalho Chehab 	}
333d6317c68SMauro Carvalho Chehab 
334b601d9a5SMauro Carvalho Chehab 	/* Attach the frontend */
335b601d9a5SMauro Carvalho Chehab 	as102_dev->dvb_fe = dvb_attach(as102_attach, as102_dev->name,
336*47f79129SMauro Carvalho Chehab 				       &as102_fe_ops,
337b601d9a5SMauro Carvalho Chehab 				       &as102_dev->bus_adap,
338b601d9a5SMauro Carvalho Chehab 				       as102_dev->elna_cfg);
339b601d9a5SMauro Carvalho Chehab 	if (!as102_dev->dvb_fe) {
340b601d9a5SMauro Carvalho Chehab 		dev_err(dev, "%s: as102_attach() failed: %d",
341b601d9a5SMauro Carvalho Chehab 		    __func__, ret);
342b601d9a5SMauro Carvalho Chehab 		goto efereg;
343b601d9a5SMauro Carvalho Chehab 	}
344b601d9a5SMauro Carvalho Chehab 
345b601d9a5SMauro Carvalho Chehab 	ret =  dvb_register_frontend(&as102_dev->dvb_adap, as102_dev->dvb_fe);
346d6317c68SMauro Carvalho Chehab 	if (ret < 0) {
347d6317c68SMauro Carvalho Chehab 		dev_err(dev, "%s: as102_dvb_register_frontend() failed: %d",
348d6317c68SMauro Carvalho Chehab 		    __func__, ret);
349d6317c68SMauro Carvalho Chehab 		goto efereg;
350d6317c68SMauro Carvalho Chehab 	}
351d6317c68SMauro Carvalho Chehab 
352d6317c68SMauro Carvalho Chehab 	/* init bus mutex for token locking */
353d6317c68SMauro Carvalho Chehab 	mutex_init(&as102_dev->bus_adap.lock);
354d6317c68SMauro Carvalho Chehab 
355d6317c68SMauro Carvalho Chehab 	/* init start / stop stream mutex */
356d6317c68SMauro Carvalho Chehab 	mutex_init(&as102_dev->sem);
357d6317c68SMauro Carvalho Chehab 
358d6317c68SMauro Carvalho Chehab 	/*
359d6317c68SMauro Carvalho Chehab 	 * try to load as102 firmware. If firmware upload failed, we'll be
360d6317c68SMauro Carvalho Chehab 	 * able to upload it later.
361d6317c68SMauro Carvalho Chehab 	 */
362d6317c68SMauro Carvalho Chehab 	if (fw_upload)
363d6317c68SMauro Carvalho Chehab 		try_then_request_module(as102_fw_upload(&as102_dev->bus_adap),
364d6317c68SMauro Carvalho Chehab 				"firmware_class");
365d6317c68SMauro Carvalho Chehab 
366d6317c68SMauro Carvalho Chehab 	pr_info("Registered device %s", as102_dev->name);
367d6317c68SMauro Carvalho Chehab 	return 0;
368d6317c68SMauro Carvalho Chehab 
369d6317c68SMauro Carvalho Chehab efereg:
370d6317c68SMauro Carvalho Chehab 	dvb_dmxdev_release(&as102_dev->dvb_dmxdev);
371d6317c68SMauro Carvalho Chehab edmxdinit:
372d6317c68SMauro Carvalho Chehab 	dvb_dmx_release(&as102_dev->dvb_dmx);
373d6317c68SMauro Carvalho Chehab edmxinit:
374d6317c68SMauro Carvalho Chehab 	dvb_unregister_adapter(&as102_dev->dvb_adap);
375d6317c68SMauro Carvalho Chehab 	return ret;
376d6317c68SMauro Carvalho Chehab }
377d6317c68SMauro Carvalho Chehab 
378d6317c68SMauro Carvalho Chehab void as102_dvb_unregister(struct as102_dev_t *as102_dev)
379d6317c68SMauro Carvalho Chehab {
380d6317c68SMauro Carvalho Chehab 	/* unregister as102 frontend */
381b601d9a5SMauro Carvalho Chehab 	dvb_unregister_frontend(as102_dev->dvb_fe);
382b601d9a5SMauro Carvalho Chehab 
383b601d9a5SMauro Carvalho Chehab 	/* detach frontend */
384b601d9a5SMauro Carvalho Chehab 	dvb_frontend_detach(as102_dev->dvb_fe);
385d6317c68SMauro Carvalho Chehab 
386d6317c68SMauro Carvalho Chehab 	/* unregister demux device */
387d6317c68SMauro Carvalho Chehab 	dvb_dmxdev_release(&as102_dev->dvb_dmxdev);
388d6317c68SMauro Carvalho Chehab 	dvb_dmx_release(&as102_dev->dvb_dmx);
389d6317c68SMauro Carvalho Chehab 
390d6317c68SMauro Carvalho Chehab 	/* unregister dvb adapter */
391d6317c68SMauro Carvalho Chehab 	dvb_unregister_adapter(&as102_dev->dvb_adap);
392d6317c68SMauro Carvalho Chehab 
393d6317c68SMauro Carvalho Chehab 	pr_info("Unregistered device %s", as102_dev->name);
394d6317c68SMauro Carvalho Chehab }
395d6317c68SMauro Carvalho Chehab 
396d6317c68SMauro Carvalho Chehab module_usb_driver(as102_usb_driver);
397d6317c68SMauro Carvalho Chehab 
398d6317c68SMauro Carvalho Chehab /* modinfo details */
399d6317c68SMauro Carvalho Chehab MODULE_DESCRIPTION(DRIVER_FULL_NAME);
400d6317c68SMauro Carvalho Chehab MODULE_LICENSE("GPL");
401d6317c68SMauro Carvalho Chehab MODULE_AUTHOR("Pierrick Hascoet <pierrick.hascoet@abilis.com>");
402