blob: f710e17953e334dbf3de7c42fe57b5cbf16579f6 [file] [log] [blame]
Rambaldia70f81c2009-01-17 14:47:34 +01001/*
2 * FireDTV driver (formerly known as FireSAT)
3 *
4 * Copyright (C) 2004 Andreas Monitzer <andy@monitzer.com>
5 * Copyright (C) 2008 Henrik Kurelid <henrik@kurelid.se>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
11 */
12
Stefan Richter15490792009-02-23 14:21:10 +010013#include <linux/bitops.h>
14#include <linux/device.h>
Rambaldia70f81c2009-01-17 14:47:34 +010015#include <linux/errno.h>
16#include <linux/kernel.h>
Stefan Richter15490792009-02-23 14:21:10 +010017#include <linux/module.h>
Rambaldia70f81c2009-01-17 14:47:34 +010018#include <linux/mutex.h>
19#include <linux/types.h>
20
Stefan Richter15490792009-02-23 14:21:10 +010021#include <dmxdev.h>
Rambaldia70f81c2009-01-17 14:47:34 +010022#include <dvb_demux.h>
Rambaldia70f81c2009-01-17 14:47:34 +010023#include <dvbdev.h>
Stefan Richter15490792009-02-23 14:21:10 +010024#include <dvb_frontend.h>
Rambaldia70f81c2009-01-17 14:47:34 +010025
Rambaldia70f81c2009-01-17 14:47:34 +010026#include "firedtv.h"
Rambaldia70f81c2009-01-17 14:47:34 +010027
Stefan Richter15490792009-02-23 14:21:10 +010028static int alloc_channel(struct firedtv *fdtv)
Rambaldia70f81c2009-01-17 14:47:34 +010029{
Stefan Richter15490792009-02-23 14:21:10 +010030 int i;
Rambaldia70f81c2009-01-17 14:47:34 +010031
Stefan Richter15490792009-02-23 14:21:10 +010032 for (i = 0; i < 16; i++)
33 if (!__test_and_set_bit(i, &fdtv->channel_active))
Rambaldia70f81c2009-01-17 14:47:34 +010034 break;
Stefan Richter15490792009-02-23 14:21:10 +010035 return i;
Rambaldia70f81c2009-01-17 14:47:34 +010036}
37
Stefan Richter15490792009-02-23 14:21:10 +010038static void collect_channels(struct firedtv *fdtv, int *pidc, u16 pid[])
Rambaldia70f81c2009-01-17 14:47:34 +010039{
Stefan Richter15490792009-02-23 14:21:10 +010040 int i, n;
Rambaldia70f81c2009-01-17 14:47:34 +010041
Stefan Richter15490792009-02-23 14:21:10 +010042 for (i = 0, n = 0; i < 16; i++)
43 if (test_bit(i, &fdtv->channel_active))
44 pid[n++] = fdtv->channel_pid[i];
45 *pidc = n;
Rambaldia70f81c2009-01-17 14:47:34 +010046}
47
Stefan Richter15490792009-02-23 14:21:10 +010048static inline void dealloc_channel(struct firedtv *fdtv, int i)
Rambaldia70f81c2009-01-17 14:47:34 +010049{
Stefan Richter15490792009-02-23 14:21:10 +010050 __clear_bit(i, &fdtv->channel_active);
Rambaldia70f81c2009-01-17 14:47:34 +010051}
52
53int fdtv_start_feed(struct dvb_demux_feed *dvbdmxfeed)
54{
Stefan Richter15490792009-02-23 14:21:10 +010055 struct firedtv *fdtv = dvbdmxfeed->demux->priv;
56 int pidc, c, ret;
Rambaldia70f81c2009-01-17 14:47:34 +010057 u16 pids[16];
58
59 switch (dvbdmxfeed->type) {
60 case DMX_TYPE_TS:
61 case DMX_TYPE_SEC:
62 break;
63 default:
Stefan Richter15490792009-02-23 14:21:10 +010064 dev_err(fdtv->device, "can't start dmx feed: invalid type %u\n",
65 dvbdmxfeed->type);
Rambaldia70f81c2009-01-17 14:47:34 +010066 return -EINVAL;
67 }
68
Stefan Richter15490792009-02-23 14:21:10 +010069 if (mutex_lock_interruptible(&fdtv->demux_mutex))
70 return -EINTR;
71
Rambaldia70f81c2009-01-17 14:47:34 +010072 if (dvbdmxfeed->type == DMX_TYPE_TS) {
73 switch (dvbdmxfeed->pes_type) {
Mauro Carvalho Chehabfde04ab2013-04-04 13:25:30 -030074 case DMX_PES_VIDEO:
75 case DMX_PES_AUDIO:
76 case DMX_PES_TELETEXT:
77 case DMX_PES_PCR:
78 case DMX_PES_OTHER:
Stefan Richter15490792009-02-23 14:21:10 +010079 c = alloc_channel(fdtv);
Rambaldia70f81c2009-01-17 14:47:34 +010080 break;
81 default:
Stefan Richter15490792009-02-23 14:21:10 +010082 dev_err(fdtv->device,
83 "can't start dmx feed: invalid pes type %u\n",
84 dvbdmxfeed->pes_type);
85 ret = -EINVAL;
86 goto out;
Rambaldia70f81c2009-01-17 14:47:34 +010087 }
88 } else {
Stefan Richter15490792009-02-23 14:21:10 +010089 c = alloc_channel(fdtv);
Rambaldia70f81c2009-01-17 14:47:34 +010090 }
91
Stefan Richter15490792009-02-23 14:21:10 +010092 if (c > 15) {
93 dev_err(fdtv->device, "can't start dmx feed: busy\n");
94 ret = -EBUSY;
95 goto out;
Rambaldia70f81c2009-01-17 14:47:34 +010096 }
97
Stefan Richter15490792009-02-23 14:21:10 +010098 dvbdmxfeed->priv = (typeof(dvbdmxfeed->priv))(unsigned long)c;
99 fdtv->channel_pid[c] = dvbdmxfeed->pid;
100 collect_channels(fdtv, &pidc, pids);
Rambaldia70f81c2009-01-17 14:47:34 +0100101
102 if (dvbdmxfeed->pid == 8192) {
Stefan Richter15490792009-02-23 14:21:10 +0100103 ret = avc_tuner_get_ts(fdtv);
104 if (ret) {
105 dealloc_channel(fdtv, c);
106 dev_err(fdtv->device, "can't get TS\n");
107 goto out;
Rambaldia70f81c2009-01-17 14:47:34 +0100108 }
109 } else {
Stefan Richter15490792009-02-23 14:21:10 +0100110 ret = avc_tuner_set_pids(fdtv, pidc, pids);
111 if (ret) {
112 dealloc_channel(fdtv, c);
113 dev_err(fdtv->device, "can't set PIDs\n");
114 goto out;
Rambaldia70f81c2009-01-17 14:47:34 +0100115 }
116 }
Stefan Richter15490792009-02-23 14:21:10 +0100117out:
118 mutex_unlock(&fdtv->demux_mutex);
Rambaldia70f81c2009-01-17 14:47:34 +0100119
Stefan Richter15490792009-02-23 14:21:10 +0100120 return ret;
Rambaldia70f81c2009-01-17 14:47:34 +0100121}
122
123int fdtv_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
124{
125 struct dvb_demux *demux = dvbdmxfeed->demux;
Stefan Richter15490792009-02-23 14:21:10 +0100126 struct firedtv *fdtv = demux->priv;
127 int pidc, c, ret;
Rambaldia70f81c2009-01-17 14:47:34 +0100128 u16 pids[16];
129
Stefan Richter15490792009-02-23 14:21:10 +0100130 if (dvbdmxfeed->type == DMX_TYPE_TS &&
131 !((dvbdmxfeed->ts_type & TS_PACKET) &&
132 (demux->dmx.frontend->source != DMX_MEMORY_FE))) {
Rambaldia70f81c2009-01-17 14:47:34 +0100133
134 if (dvbdmxfeed->ts_type & TS_DECODER) {
Mauro Carvalho Chehabfde04ab2013-04-04 13:25:30 -0300135 if (dvbdmxfeed->pes_type >= DMX_PES_OTHER ||
Stefan Richter15490792009-02-23 14:21:10 +0100136 !demux->pesfilter[dvbdmxfeed->pes_type])
Rambaldia70f81c2009-01-17 14:47:34 +0100137 return -EINVAL;
138
139 demux->pids[dvbdmxfeed->pes_type] |= 0x8000;
140 demux->pesfilter[dvbdmxfeed->pes_type] = NULL;
141 }
142
143 if (!(dvbdmxfeed->ts_type & TS_DECODER &&
Mauro Carvalho Chehabfde04ab2013-04-04 13:25:30 -0300144 dvbdmxfeed->pes_type < DMX_PES_OTHER))
Rambaldia70f81c2009-01-17 14:47:34 +0100145 return 0;
146 }
147
148 if (mutex_lock_interruptible(&fdtv->demux_mutex))
149 return -EINTR;
150
Stefan Richter15490792009-02-23 14:21:10 +0100151 c = (unsigned long)dvbdmxfeed->priv;
152 dealloc_channel(fdtv, c);
153 collect_channels(fdtv, &pidc, pids);
Rambaldia70f81c2009-01-17 14:47:34 +0100154
Stefan Richter15490792009-02-23 14:21:10 +0100155 ret = avc_tuner_set_pids(fdtv, pidc, pids);
Rambaldia70f81c2009-01-17 14:47:34 +0100156
157 mutex_unlock(&fdtv->demux_mutex);
Stefan Richter15490792009-02-23 14:21:10 +0100158
159 return ret;
Rambaldia70f81c2009-01-17 14:47:34 +0100160}
161
Stefan Richter15490792009-02-23 14:21:10 +0100162DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
163
Stefan Richter92374e82011-02-06 11:41:44 -0300164int fdtv_dvb_register(struct firedtv *fdtv, const char *name)
Rambaldia70f81c2009-01-17 14:47:34 +0100165{
166 int err;
167
Stefan Richter92374e82011-02-06 11:41:44 -0300168 err = dvb_register_adapter(&fdtv->adapter, name,
Stefan Richter15490792009-02-23 14:21:10 +0100169 THIS_MODULE, fdtv->device, adapter_nr);
Rambaldia70f81c2009-01-17 14:47:34 +0100170 if (err < 0)
171 goto fail_log;
172
173 /*DMX_TS_FILTERING | DMX_SECTION_FILTERING*/
174 fdtv->demux.dmx.capabilities = 0;
175
176 fdtv->demux.priv = fdtv;
177 fdtv->demux.filternum = 16;
178 fdtv->demux.feednum = 16;
179 fdtv->demux.start_feed = fdtv_start_feed;
180 fdtv->demux.stop_feed = fdtv_stop_feed;
181 fdtv->demux.write_to_decoder = NULL;
182
183 err = dvb_dmx_init(&fdtv->demux);
184 if (err)
185 goto fail_unreg_adapter;
186
Stefan Richter15490792009-02-23 14:21:10 +0100187 fdtv->dmxdev.filternum = 16;
188 fdtv->dmxdev.demux = &fdtv->demux.dmx;
189 fdtv->dmxdev.capabilities = 0;
Rambaldia70f81c2009-01-17 14:47:34 +0100190
191 err = dvb_dmxdev_init(&fdtv->dmxdev, &fdtv->adapter);
192 if (err)
193 goto fail_dmx_release;
194
195 fdtv->frontend.source = DMX_FRONTEND_0;
196
Stefan Richter15490792009-02-23 14:21:10 +0100197 err = fdtv->demux.dmx.add_frontend(&fdtv->demux.dmx, &fdtv->frontend);
Rambaldia70f81c2009-01-17 14:47:34 +0100198 if (err)
199 goto fail_dmxdev_release;
200
201 err = fdtv->demux.dmx.connect_frontend(&fdtv->demux.dmx,
Stefan Richter15490792009-02-23 14:21:10 +0100202 &fdtv->frontend);
Rambaldia70f81c2009-01-17 14:47:34 +0100203 if (err)
204 goto fail_rem_frontend;
205
Jonathan Niederf1d99f32011-12-31 08:19:56 -0300206 err = dvb_net_init(&fdtv->adapter, &fdtv->dvbnet, &fdtv->demux.dmx);
207 if (err)
208 goto fail_disconnect_frontend;
Rambaldia70f81c2009-01-17 14:47:34 +0100209
Stefan Richter92374e82011-02-06 11:41:44 -0300210 fdtv_frontend_init(fdtv, name);
Rambaldia70f81c2009-01-17 14:47:34 +0100211 err = dvb_register_frontend(&fdtv->adapter, &fdtv->fe);
212 if (err)
213 goto fail_net_release;
214
215 err = fdtv_ca_register(fdtv);
216 if (err)
Stefan Richter15490792009-02-23 14:21:10 +0100217 dev_info(fdtv->device,
218 "Conditional Access Module not enabled\n");
Rambaldia70f81c2009-01-17 14:47:34 +0100219 return 0;
220
221fail_net_release:
222 dvb_net_release(&fdtv->dvbnet);
Jonathan Niederf1d99f32011-12-31 08:19:56 -0300223fail_disconnect_frontend:
Rambaldia70f81c2009-01-17 14:47:34 +0100224 fdtv->demux.dmx.close(&fdtv->demux.dmx);
225fail_rem_frontend:
Stefan Richter15490792009-02-23 14:21:10 +0100226 fdtv->demux.dmx.remove_frontend(&fdtv->demux.dmx, &fdtv->frontend);
Rambaldia70f81c2009-01-17 14:47:34 +0100227fail_dmxdev_release:
228 dvb_dmxdev_release(&fdtv->dmxdev);
229fail_dmx_release:
230 dvb_dmx_release(&fdtv->demux);
231fail_unreg_adapter:
232 dvb_unregister_adapter(&fdtv->adapter);
233fail_log:
Stefan Richter15490792009-02-23 14:21:10 +0100234 dev_err(fdtv->device, "DVB initialization failed\n");
Rambaldia70f81c2009-01-17 14:47:34 +0100235 return err;
236}
237
Stefan Richter15490792009-02-23 14:21:10 +0100238void fdtv_dvb_unregister(struct firedtv *fdtv)
239{
240 fdtv_ca_release(fdtv);
241 dvb_unregister_frontend(&fdtv->fe);
242 dvb_net_release(&fdtv->dvbnet);
243 fdtv->demux.dmx.close(&fdtv->demux.dmx);
244 fdtv->demux.dmx.remove_frontend(&fdtv->demux.dmx, &fdtv->frontend);
245 dvb_dmxdev_release(&fdtv->dmxdev);
246 dvb_dmx_release(&fdtv->demux);
247 dvb_unregister_adapter(&fdtv->adapter);
248}