Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 1 | /* |
| 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 Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 13 | #include <linux/bitops.h> |
| 14 | #include <linux/device.h> |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 15 | #include <linux/errno.h> |
| 16 | #include <linux/kernel.h> |
Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 17 | #include <linux/module.h> |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 18 | #include <linux/mutex.h> |
| 19 | #include <linux/types.h> |
| 20 | |
Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 21 | #include <dmxdev.h> |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 22 | #include <dvb_demux.h> |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 23 | #include <dvbdev.h> |
Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 24 | #include <dvb_frontend.h> |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 25 | |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 26 | #include "firedtv.h" |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 27 | |
Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 28 | static int alloc_channel(struct firedtv *fdtv) |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 29 | { |
Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 30 | int i; |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 31 | |
Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 32 | for (i = 0; i < 16; i++) |
| 33 | if (!__test_and_set_bit(i, &fdtv->channel_active)) |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 34 | break; |
Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 35 | return i; |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 36 | } |
| 37 | |
Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 38 | static void collect_channels(struct firedtv *fdtv, int *pidc, u16 pid[]) |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 39 | { |
Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 40 | int i, n; |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 41 | |
Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 42 | 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; |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 46 | } |
| 47 | |
Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 48 | static inline void dealloc_channel(struct firedtv *fdtv, int i) |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 49 | { |
Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 50 | __clear_bit(i, &fdtv->channel_active); |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | int fdtv_start_feed(struct dvb_demux_feed *dvbdmxfeed) |
| 54 | { |
Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 55 | struct firedtv *fdtv = dvbdmxfeed->demux->priv; |
| 56 | int pidc, c, ret; |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 57 | u16 pids[16]; |
| 58 | |
| 59 | switch (dvbdmxfeed->type) { |
| 60 | case DMX_TYPE_TS: |
| 61 | case DMX_TYPE_SEC: |
| 62 | break; |
| 63 | default: |
Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 64 | dev_err(fdtv->device, "can't start dmx feed: invalid type %u\n", |
| 65 | dvbdmxfeed->type); |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 66 | return -EINVAL; |
| 67 | } |
| 68 | |
Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 69 | if (mutex_lock_interruptible(&fdtv->demux_mutex)) |
| 70 | return -EINTR; |
| 71 | |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 72 | if (dvbdmxfeed->type == DMX_TYPE_TS) { |
| 73 | switch (dvbdmxfeed->pes_type) { |
Mauro Carvalho Chehab | fde04ab | 2013-04-04 13:25:30 -0300 | [diff] [blame] | 74 | 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 Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 79 | c = alloc_channel(fdtv); |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 80 | break; |
| 81 | default: |
Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 82 | 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; |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 87 | } |
| 88 | } else { |
Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 89 | c = alloc_channel(fdtv); |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 90 | } |
| 91 | |
Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 92 | if (c > 15) { |
| 93 | dev_err(fdtv->device, "can't start dmx feed: busy\n"); |
| 94 | ret = -EBUSY; |
| 95 | goto out; |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 96 | } |
| 97 | |
Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 98 | dvbdmxfeed->priv = (typeof(dvbdmxfeed->priv))(unsigned long)c; |
| 99 | fdtv->channel_pid[c] = dvbdmxfeed->pid; |
| 100 | collect_channels(fdtv, &pidc, pids); |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 101 | |
| 102 | if (dvbdmxfeed->pid == 8192) { |
Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 103 | 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; |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 108 | } |
| 109 | } else { |
Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 110 | 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; |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 115 | } |
| 116 | } |
Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 117 | out: |
| 118 | mutex_unlock(&fdtv->demux_mutex); |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 119 | |
Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 120 | return ret; |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | int fdtv_stop_feed(struct dvb_demux_feed *dvbdmxfeed) |
| 124 | { |
| 125 | struct dvb_demux *demux = dvbdmxfeed->demux; |
Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 126 | struct firedtv *fdtv = demux->priv; |
| 127 | int pidc, c, ret; |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 128 | u16 pids[16]; |
| 129 | |
Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 130 | if (dvbdmxfeed->type == DMX_TYPE_TS && |
| 131 | !((dvbdmxfeed->ts_type & TS_PACKET) && |
| 132 | (demux->dmx.frontend->source != DMX_MEMORY_FE))) { |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 133 | |
| 134 | if (dvbdmxfeed->ts_type & TS_DECODER) { |
Mauro Carvalho Chehab | fde04ab | 2013-04-04 13:25:30 -0300 | [diff] [blame] | 135 | if (dvbdmxfeed->pes_type >= DMX_PES_OTHER || |
Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 136 | !demux->pesfilter[dvbdmxfeed->pes_type]) |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 137 | 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 Chehab | fde04ab | 2013-04-04 13:25:30 -0300 | [diff] [blame] | 144 | dvbdmxfeed->pes_type < DMX_PES_OTHER)) |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | if (mutex_lock_interruptible(&fdtv->demux_mutex)) |
| 149 | return -EINTR; |
| 150 | |
Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 151 | c = (unsigned long)dvbdmxfeed->priv; |
| 152 | dealloc_channel(fdtv, c); |
| 153 | collect_channels(fdtv, &pidc, pids); |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 154 | |
Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 155 | ret = avc_tuner_set_pids(fdtv, pidc, pids); |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 156 | |
| 157 | mutex_unlock(&fdtv->demux_mutex); |
Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 158 | |
| 159 | return ret; |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 160 | } |
| 161 | |
Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 162 | DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); |
| 163 | |
Stefan Richter | 92374e8 | 2011-02-06 11:41:44 -0300 | [diff] [blame] | 164 | int fdtv_dvb_register(struct firedtv *fdtv, const char *name) |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 165 | { |
| 166 | int err; |
| 167 | |
Stefan Richter | 92374e8 | 2011-02-06 11:41:44 -0300 | [diff] [blame] | 168 | err = dvb_register_adapter(&fdtv->adapter, name, |
Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 169 | THIS_MODULE, fdtv->device, adapter_nr); |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 170 | 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 Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 187 | fdtv->dmxdev.filternum = 16; |
| 188 | fdtv->dmxdev.demux = &fdtv->demux.dmx; |
| 189 | fdtv->dmxdev.capabilities = 0; |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 190 | |
| 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 Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 197 | err = fdtv->demux.dmx.add_frontend(&fdtv->demux.dmx, &fdtv->frontend); |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 198 | if (err) |
| 199 | goto fail_dmxdev_release; |
| 200 | |
| 201 | err = fdtv->demux.dmx.connect_frontend(&fdtv->demux.dmx, |
Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 202 | &fdtv->frontend); |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 203 | if (err) |
| 204 | goto fail_rem_frontend; |
| 205 | |
Jonathan Nieder | f1d99f3 | 2011-12-31 08:19:56 -0300 | [diff] [blame] | 206 | err = dvb_net_init(&fdtv->adapter, &fdtv->dvbnet, &fdtv->demux.dmx); |
| 207 | if (err) |
| 208 | goto fail_disconnect_frontend; |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 209 | |
Stefan Richter | 92374e8 | 2011-02-06 11:41:44 -0300 | [diff] [blame] | 210 | fdtv_frontend_init(fdtv, name); |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 211 | 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 Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 217 | dev_info(fdtv->device, |
| 218 | "Conditional Access Module not enabled\n"); |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 219 | return 0; |
| 220 | |
| 221 | fail_net_release: |
| 222 | dvb_net_release(&fdtv->dvbnet); |
Jonathan Nieder | f1d99f3 | 2011-12-31 08:19:56 -0300 | [diff] [blame] | 223 | fail_disconnect_frontend: |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 224 | fdtv->demux.dmx.close(&fdtv->demux.dmx); |
| 225 | fail_rem_frontend: |
Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 226 | fdtv->demux.dmx.remove_frontend(&fdtv->demux.dmx, &fdtv->frontend); |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 227 | fail_dmxdev_release: |
| 228 | dvb_dmxdev_release(&fdtv->dmxdev); |
| 229 | fail_dmx_release: |
| 230 | dvb_dmx_release(&fdtv->demux); |
| 231 | fail_unreg_adapter: |
| 232 | dvb_unregister_adapter(&fdtv->adapter); |
| 233 | fail_log: |
Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 234 | dev_err(fdtv->device, "DVB initialization failed\n"); |
Rambaldi | a70f81c | 2009-01-17 14:47:34 +0100 | [diff] [blame] | 235 | return err; |
| 236 | } |
| 237 | |
Stefan Richter | 1549079 | 2009-02-23 14:21:10 +0100 | [diff] [blame] | 238 | void 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 | } |