Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / media / pci / mantis / mantis_dvb.c
blob398e32f4469204e24517264d8274196d3add13ed
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 Mantis PCI bridge driver
4 Copyright (C) Manu Abraham (abraham.manu@gmail.com)
6 */
8 #include <linux/kernel.h>
9 #include <linux/bitops.h>
11 #include <linux/signal.h>
12 #include <linux/sched.h>
13 #include <linux/interrupt.h>
14 #include <linux/pci.h>
15 #include <linux/i2c.h>
17 #include <media/dmxdev.h>
18 #include <media/dvbdev.h>
19 #include <media/dvb_demux.h>
20 #include <media/dvb_frontend.h>
21 #include <media/dvb_net.h>
23 #include "mantis_common.h"
24 #include "mantis_dma.h"
25 #include "mantis_ca.h"
26 #include "mantis_ioc.h"
27 #include "mantis_dvb.h"
29 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
31 int mantis_frontend_power(struct mantis_pci *mantis, enum mantis_power power)
33 struct mantis_hwconfig *config = mantis->hwconfig;
35 switch (power) {
36 case POWER_ON:
37 dprintk(MANTIS_DEBUG, 1, "Power ON");
38 mantis_gpio_set_bits(mantis, config->power, POWER_ON);
39 msleep(100);
40 mantis_gpio_set_bits(mantis, config->power, POWER_ON);
41 msleep(100);
42 break;
44 case POWER_OFF:
45 dprintk(MANTIS_DEBUG, 1, "Power OFF");
46 mantis_gpio_set_bits(mantis, config->power, POWER_OFF);
47 msleep(100);
48 break;
50 default:
51 dprintk(MANTIS_DEBUG, 1, "Unknown state <%02x>", power);
52 return -1;
55 return 0;
57 EXPORT_SYMBOL_GPL(mantis_frontend_power);
59 void mantis_frontend_soft_reset(struct mantis_pci *mantis)
61 struct mantis_hwconfig *config = mantis->hwconfig;
63 dprintk(MANTIS_DEBUG, 1, "Frontend RESET");
64 mantis_gpio_set_bits(mantis, config->reset, 0);
65 msleep(100);
66 mantis_gpio_set_bits(mantis, config->reset, 0);
67 msleep(100);
68 mantis_gpio_set_bits(mantis, config->reset, 1);
69 msleep(100);
70 mantis_gpio_set_bits(mantis, config->reset, 1);
71 msleep(100);
73 return;
75 EXPORT_SYMBOL_GPL(mantis_frontend_soft_reset);
77 static int mantis_frontend_shutdown(struct mantis_pci *mantis)
79 int err;
81 mantis_frontend_soft_reset(mantis);
82 err = mantis_frontend_power(mantis, POWER_OFF);
83 if (err != 0) {
84 dprintk(MANTIS_ERROR, 1, "Frontend POWER OFF failed! <%d>", err);
85 return 1;
88 return 0;
91 static int mantis_dvb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
93 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
94 struct mantis_pci *mantis = dvbdmx->priv;
96 dprintk(MANTIS_DEBUG, 1, "Mantis DVB Start feed");
97 if (!dvbdmx->dmx.frontend) {
98 dprintk(MANTIS_DEBUG, 1, "no frontend ?");
99 return -EINVAL;
102 mantis->feeds++;
103 dprintk(MANTIS_DEBUG, 1, "mantis start feed, feeds=%d", mantis->feeds);
105 if (mantis->feeds == 1) {
106 dprintk(MANTIS_DEBUG, 1, "mantis start feed & dma");
107 mantis_dma_start(mantis);
108 enable_and_queue_work(system_bh_wq, &mantis->bh_work);
111 return mantis->feeds;
114 static int mantis_dvb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
116 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
117 struct mantis_pci *mantis = dvbdmx->priv;
119 dprintk(MANTIS_DEBUG, 1, "Mantis DVB Stop feed");
120 if (!dvbdmx->dmx.frontend) {
121 dprintk(MANTIS_DEBUG, 1, "no frontend ?");
122 return -EINVAL;
125 mantis->feeds--;
126 if (mantis->feeds == 0) {
127 dprintk(MANTIS_DEBUG, 1, "mantis stop feed and dma");
128 disable_work_sync(&mantis->bh_work);
129 mantis_dma_stop(mantis);
132 return 0;
135 int mantis_dvb_init(struct mantis_pci *mantis)
137 struct mantis_hwconfig *config = mantis->hwconfig;
138 int result;
140 dprintk(MANTIS_DEBUG, 1, "dvb_register_adapter");
142 result = dvb_register_adapter(&mantis->dvb_adapter,
143 "Mantis DVB adapter",
144 THIS_MODULE,
145 &mantis->pdev->dev,
146 adapter_nr);
148 if (result < 0) {
150 dprintk(MANTIS_ERROR, 1, "Error registering adapter");
151 return -ENODEV;
154 mantis->dvb_adapter.priv = mantis;
155 mantis->demux.dmx.capabilities = DMX_TS_FILTERING |
156 DMX_SECTION_FILTERING |
157 DMX_MEMORY_BASED_FILTERING;
159 mantis->demux.priv = mantis;
160 mantis->demux.filternum = 256;
161 mantis->demux.feednum = 256;
162 mantis->demux.start_feed = mantis_dvb_start_feed;
163 mantis->demux.stop_feed = mantis_dvb_stop_feed;
164 mantis->demux.write_to_decoder = NULL;
166 dprintk(MANTIS_DEBUG, 1, "dvb_dmx_init");
167 result = dvb_dmx_init(&mantis->demux);
168 if (result < 0) {
169 dprintk(MANTIS_ERROR, 1, "dvb_dmx_init failed, ERROR=%d", result);
171 goto err0;
174 mantis->dmxdev.filternum = 256;
175 mantis->dmxdev.demux = &mantis->demux.dmx;
176 mantis->dmxdev.capabilities = 0;
177 dprintk(MANTIS_DEBUG, 1, "dvb_dmxdev_init");
179 result = dvb_dmxdev_init(&mantis->dmxdev, &mantis->dvb_adapter);
180 if (result < 0) {
182 dprintk(MANTIS_ERROR, 1, "dvb_dmxdev_init failed, ERROR=%d", result);
183 goto err1;
186 mantis->fe_hw.source = DMX_FRONTEND_0;
187 result = mantis->demux.dmx.add_frontend(&mantis->demux.dmx, &mantis->fe_hw);
188 if (result < 0) {
190 dprintk(MANTIS_ERROR, 1, "dvb_dmx_init failed, ERROR=%d", result);
191 goto err2;
194 mantis->fe_mem.source = DMX_MEMORY_FE;
195 result = mantis->demux.dmx.add_frontend(&mantis->demux.dmx, &mantis->fe_mem);
196 if (result < 0) {
197 dprintk(MANTIS_ERROR, 1, "dvb_dmx_init failed, ERROR=%d", result);
198 goto err3;
201 result = mantis->demux.dmx.connect_frontend(&mantis->demux.dmx, &mantis->fe_hw);
202 if (result < 0) {
203 dprintk(MANTIS_ERROR, 1, "dvb_dmx_init failed, ERROR=%d", result);
204 goto err4;
207 dvb_net_init(&mantis->dvb_adapter, &mantis->dvbnet, &mantis->demux.dmx);
208 INIT_WORK(&mantis->bh_work, mantis_dma_xfer);
209 disable_work_sync(&mantis->bh_work);
210 if (mantis->hwconfig) {
211 result = config->frontend_init(mantis, mantis->fe);
212 if (result < 0) {
213 dprintk(MANTIS_ERROR, 1, "!!! NO Frontends found !!!");
214 goto err5;
215 } else {
216 if (mantis->fe == NULL) {
217 result = -ENOMEM;
218 dprintk(MANTIS_ERROR, 1, "FE <NULL>");
219 goto err5;
221 result = dvb_register_frontend(&mantis->dvb_adapter, mantis->fe);
222 if (result) {
223 dprintk(MANTIS_ERROR, 1, "ERROR: Frontend registration failed");
225 if (mantis->fe->ops.release)
226 mantis->fe->ops.release(mantis->fe);
228 mantis->fe = NULL;
229 goto err5;
234 return 0;
236 /* Error conditions .. */
237 err5:
238 cancel_work_sync(&mantis->bh_work);
239 dvb_net_release(&mantis->dvbnet);
240 if (mantis->fe) {
241 dvb_unregister_frontend(mantis->fe);
242 dvb_frontend_detach(mantis->fe);
244 err4:
245 mantis->demux.dmx.remove_frontend(&mantis->demux.dmx, &mantis->fe_mem);
247 err3:
248 mantis->demux.dmx.remove_frontend(&mantis->demux.dmx, &mantis->fe_hw);
250 err2:
251 dvb_dmxdev_release(&mantis->dmxdev);
253 err1:
254 dvb_dmx_release(&mantis->demux);
256 err0:
257 dvb_unregister_adapter(&mantis->dvb_adapter);
259 return result;
261 EXPORT_SYMBOL_GPL(mantis_dvb_init);
263 int mantis_dvb_exit(struct mantis_pci *mantis)
265 int err;
267 if (mantis->fe) {
268 /* mantis_ca_exit(mantis); */
269 err = mantis_frontend_shutdown(mantis);
270 if (err != 0)
271 dprintk(MANTIS_ERROR, 1, "Frontend exit while POWER ON! <%d>", err);
272 dvb_unregister_frontend(mantis->fe);
273 dvb_frontend_detach(mantis->fe);
276 cancel_work_sync(&mantis->bh_work);
277 dvb_net_release(&mantis->dvbnet);
279 mantis->demux.dmx.remove_frontend(&mantis->demux.dmx, &mantis->fe_mem);
280 mantis->demux.dmx.remove_frontend(&mantis->demux.dmx, &mantis->fe_hw);
282 dvb_dmxdev_release(&mantis->dmxdev);
283 dvb_dmx_release(&mantis->demux);
285 dprintk(MANTIS_DEBUG, 1, "dvb_unregister_adapter");
286 dvb_unregister_adapter(&mantis->dvb_adapter);
288 return 0;
290 EXPORT_SYMBOL_GPL(mantis_dvb_exit);