27a1571e1aeefed302f36aa2788bcbf8b69ee97c
[firefly-linux-kernel-4.4.55.git] / drivers / staging / media / as102 / as102_drv.c
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 <asm/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
34 #if defined(CONFIG_DVB_CORE) || defined(CONFIG_DVB_CORE_MODULE)
35 #include "dvbdev.h"
36 #else
37 #warning >>> DVB_CORE not defined !!! <<<
38 #endif
39
40 int debug;
41 module_param_named(debug, debug, int, 0644);
42 MODULE_PARM_DESC(debug, "Turn on/off debugging (default: off)");
43
44 int dual_tuner;
45 module_param_named(dual_tuner, dual_tuner, int, 0644);
46 MODULE_PARM_DESC(dual_tuner, "Activate Dual-Tuner config (default: off)");
47
48 static int fw_upload = 1;
49 module_param_named(fw_upload, fw_upload, int, 0644);
50 MODULE_PARM_DESC(fw_upload, "Turn on/off default FW upload (default: on)");
51
52 static int pid_filtering;
53 module_param_named(pid_filtering, pid_filtering, int, 0644);
54 MODULE_PARM_DESC(pid_filtering, "Activate HW PID filtering (default: off)");
55
56 static int ts_auto_disable;
57 module_param_named(ts_auto_disable, ts_auto_disable, int, 0644);
58 MODULE_PARM_DESC(ts_auto_disable, "Stream Auto Enable on FW (default: off)");
59
60 int elna_enable = 1;
61 module_param_named(elna_enable, elna_enable, int, 0644);
62 MODULE_PARM_DESC(elna_enable, "Activate eLNA (default: on)");
63
64 #ifdef DVB_DEFINE_MOD_OPT_ADAPTER_NR
65 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
66 #endif
67
68 #if defined(CONFIG_DVB_CORE) || defined(CONFIG_DVB_CORE_MODULE)
69 static void as102_stop_stream(struct as102_dev_t *dev)
70 {
71         struct as102_bus_adapter_t *bus_adap;
72
73         if (dev != NULL)
74                 bus_adap = &dev->bus_adap;
75         else
76                 return;
77
78         if (bus_adap->ops->stop_stream != NULL)
79                 bus_adap->ops->stop_stream(dev);
80
81         if (ts_auto_disable) {
82                 if (mutex_lock_interruptible(&dev->bus_adap.lock))
83                         return;
84
85                 if (as10x_cmd_stop_streaming(bus_adap) < 0)
86                         dprintk(debug, "as10x_cmd_stop_streaming failed\n");
87
88                 mutex_unlock(&dev->bus_adap.lock);
89         }
90 }
91
92 static int as102_start_stream(struct as102_dev_t *dev)
93 {
94         struct as102_bus_adapter_t *bus_adap;
95         int ret = -EFAULT;
96
97         if (dev != NULL)
98                 bus_adap = &dev->bus_adap;
99         else
100                 return ret;
101
102         if (bus_adap->ops->start_stream != NULL)
103                 ret = bus_adap->ops->start_stream(dev);
104
105         if (ts_auto_disable) {
106                 if (mutex_lock_interruptible(&dev->bus_adap.lock))
107                         return -EFAULT;
108
109                 ret = as10x_cmd_start_streaming(bus_adap);
110
111                 mutex_unlock(&dev->bus_adap.lock);
112         }
113
114         return ret;
115 }
116
117 static int as10x_pid_filter(struct as102_dev_t *dev,
118                             int index, u16 pid, int onoff) {
119
120         struct as102_bus_adapter_t *bus_adap = &dev->bus_adap;
121         int ret = -EFAULT;
122
123         ENTER();
124
125         if (mutex_lock_interruptible(&dev->bus_adap.lock)) {
126                 dprintk(debug, "mutex_lock_interruptible(lock) failed !\n");
127                 return -EBUSY;
128         }
129
130         switch (onoff) {
131         case 0:
132             ret = as10x_cmd_del_PID_filter(bus_adap, (uint16_t) pid);
133             dprintk(debug, "DEL_PID_FILTER([%02d] 0x%04x) ret = %d\n",
134                     index, pid, ret);
135             break;
136         case 1:
137         {
138             struct as10x_ts_filter filter;
139
140             filter.type = TS_PID_TYPE_TS;
141             filter.idx = 0xFF;
142             filter.pid = pid;
143
144             ret = as10x_cmd_add_PID_filter(bus_adap, &filter);
145             dprintk(debug, "ADD_PID_FILTER([%02d -> %02d], 0x%04x) ret = %d\n",
146                     index, filter.idx, filter.pid, ret);
147             break;
148         }
149         }
150
151         mutex_unlock(&dev->bus_adap.lock);
152
153         LEAVE();
154         return ret;
155 }
156
157 static int as102_dvb_dmx_start_feed(struct dvb_demux_feed *dvbdmxfeed)
158 {
159         int ret = 0;
160         struct dvb_demux *demux = dvbdmxfeed->demux;
161         struct as102_dev_t *as102_dev = demux->priv;
162
163         ENTER();
164
165         if (mutex_lock_interruptible(&as102_dev->sem))
166                 return -ERESTARTSYS;
167
168         if (pid_filtering) {
169                 as10x_pid_filter(as102_dev,
170                                 dvbdmxfeed->index, dvbdmxfeed->pid, 1);
171         }
172
173         if (as102_dev->streaming++ == 0)
174                 ret = as102_start_stream(as102_dev);
175
176         mutex_unlock(&as102_dev->sem);
177         LEAVE();
178         return ret;
179 }
180
181 static int as102_dvb_dmx_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
182 {
183         struct dvb_demux *demux = dvbdmxfeed->demux;
184         struct as102_dev_t *as102_dev = demux->priv;
185
186         ENTER();
187
188         if (mutex_lock_interruptible(&as102_dev->sem))
189                 return -ERESTARTSYS;
190
191         if (--as102_dev->streaming == 0)
192                 as102_stop_stream(as102_dev);
193
194         if (pid_filtering) {
195                 as10x_pid_filter(as102_dev,
196                                 dvbdmxfeed->index, dvbdmxfeed->pid, 0);
197         }
198
199         mutex_unlock(&as102_dev->sem);
200         LEAVE();
201         return 0;
202 }
203 #endif
204
205 int as102_dvb_register(struct as102_dev_t *as102_dev)
206 {
207         int ret = 0;
208         ENTER();
209
210 #if defined(CONFIG_DVB_CORE) || defined(CONFIG_DVB_CORE_MODULE)
211         ret = dvb_register_adapter(&as102_dev->dvb_adap,
212                                    as102_dev->name,
213                                    THIS_MODULE,
214 #if defined(CONFIG_AS102_USB)
215                                    &as102_dev->bus_adap.usb_dev->dev
216 #elif defined(CONFIG_AS102_SPI)
217                                    &as102_dev->bus_adap.spi_dev->dev
218 #else
219 #error >>> dvb_register_adapter <<<
220 #endif
221 #ifdef DVB_DEFINE_MOD_OPT_ADAPTER_NR
222                                    , adapter_nr
223 #endif
224                                    );
225         if (ret < 0) {
226                 err("%s: dvb_register_adapter() failed (errno = %d)",
227                     __func__, ret);
228                 goto failed;
229         }
230
231         as102_dev->dvb_dmx.priv = as102_dev;
232         as102_dev->dvb_dmx.filternum = pid_filtering ? 16 : 256;
233         as102_dev->dvb_dmx.feednum = 256;
234         as102_dev->dvb_dmx.start_feed = as102_dvb_dmx_start_feed;
235         as102_dev->dvb_dmx.stop_feed = as102_dvb_dmx_stop_feed;
236
237         as102_dev->dvb_dmx.dmx.capabilities = DMX_TS_FILTERING |
238                                               DMX_SECTION_FILTERING;
239
240         as102_dev->dvb_dmxdev.filternum = as102_dev->dvb_dmx.filternum;
241         as102_dev->dvb_dmxdev.demux = &as102_dev->dvb_dmx.dmx;
242         as102_dev->dvb_dmxdev.capabilities = 0;
243
244         ret = dvb_dmx_init(&as102_dev->dvb_dmx);
245         if (ret < 0) {
246                 err("%s: dvb_dmx_init() failed (errno = %d)", __func__, ret);
247                 goto failed;
248         }
249
250         ret = dvb_dmxdev_init(&as102_dev->dvb_dmxdev, &as102_dev->dvb_adap);
251         if (ret < 0) {
252                 err("%s: dvb_dmxdev_init() failed (errno = %d)", __func__,
253                     ret);
254                 goto failed;
255         }
256
257         ret = as102_dvb_register_fe(as102_dev, &as102_dev->dvb_fe);
258         if (ret < 0) {
259                 err("%s: as102_dvb_register_frontend() failed (errno = %d)",
260                     __func__, ret);
261                 goto failed;
262         }
263 #endif
264
265         /* init bus mutex for token locking */
266         mutex_init(&as102_dev->bus_adap.lock);
267
268         /* init start / stop stream mutex */
269         mutex_init(&as102_dev->sem);
270
271 #if defined(CONFIG_FW_LOADER) || defined(CONFIG_FW_LOADER_MODULE)
272         /*
273          * try to load as102 firmware. If firmware upload failed, we'll be
274          * able to upload it later.
275          */
276         if (fw_upload)
277                 try_then_request_module(as102_fw_upload(&as102_dev->bus_adap),
278                                 "firmware_class");
279 #endif
280
281 failed:
282         LEAVE();
283         /* FIXME: free dvb_XXX */
284         return ret;
285 }
286
287 void as102_dvb_unregister(struct as102_dev_t *as102_dev)
288 {
289         ENTER();
290
291 #if defined(CONFIG_DVB_CORE) || defined(CONFIG_DVB_CORE_MODULE)
292         /* unregister as102 frontend */
293         as102_dvb_unregister_fe(&as102_dev->dvb_fe);
294
295         /* unregister demux device */
296         dvb_dmxdev_release(&as102_dev->dvb_dmxdev);
297         dvb_dmx_release(&as102_dev->dvb_dmx);
298
299         /* unregister dvb adapter */
300         dvb_unregister_adapter(&as102_dev->dvb_adap);
301 #endif
302         LEAVE();
303 }
304
305 static int __init as102_driver_init(void)
306 {
307         int ret = 0;
308
309         ENTER();
310
311         /* register this driver with the low level subsystem */
312 #if defined(CONFIG_AS102_USB)
313         ret = usb_register(&as102_usb_driver);
314         if (ret)
315                 err("usb_register failed (ret = %d)", ret);
316 #endif
317 #if defined(CONFIG_AS102_SPI)
318         ret = spi_register_driver(&as102_spi_driver);
319         if (ret)
320                 printk(KERN_ERR "spi_register failed (ret = %d)", ret);
321 #endif
322
323         LEAVE();
324         return ret;
325 }
326
327 /*
328  * Mandatory function : Adds a special section to the module indicating
329  * where initialisation function is defined
330  */
331 module_init(as102_driver_init);
332
333 /**
334  * as102_driver_exit - as102 driver exit point
335  *
336  * This function is called when device has to be removed.
337  */
338 static void __exit as102_driver_exit(void)
339 {
340         ENTER();
341         /* deregister this driver with the low level bus subsystem */
342 #if defined(CONFIG_AS102_USB)
343         usb_deregister(&as102_usb_driver);
344 #endif
345 #if defined(CONFIG_AS102_SPI)
346         spi_unregister_driver(&as102_spi_driver);
347 #endif
348         LEAVE();
349 }
350
351 /*
352  * required function for unload: Adds a special section to the module
353  * indicating where unload function is defined
354  */
355 module_exit(as102_driver_exit);
356 /* modinfo details */
357 MODULE_DESCRIPTION(DRIVER_FULL_NAME);
358 MODULE_LICENSE("GPL");
359 MODULE_AUTHOR("Pierrick Hascoet <pierrick.hascoet@abilis.com>");
360
361 /* EOF - vim: set textwidth=80 ts=8 sw=8 sts=8 noet: */