Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * dvb_demux.c - DVB kernel demux API |
| 3 | * |
| 4 | * Copyright (C) 2000-2001 Ralph Metzler <ralph@convergence.de> |
| 5 | * & Marcus Metzler <marcus@convergence.de> |
| 6 | * for convergence integrated media GmbH |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Lesser General Public License |
| 10 | * as published by the Free Software Foundation; either version 2.1 |
| 11 | * of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Lesser General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <linux/spinlock.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/vmalloc.h> |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/poll.h> |
| 29 | #include <linux/string.h> |
| 30 | #include <linux/crc32.h> |
| 31 | #include <asm/uaccess.h> |
| 32 | |
| 33 | #include "dvb_demux.h" |
| 34 | |
| 35 | #define NOBUFS |
| 36 | /* |
| 37 | ** #define DVB_DEMUX_SECTION_LOSS_LOG to monitor payload loss in the syslog |
| 38 | */ |
| 39 | // #define DVB_DEMUX_SECTION_LOSS_LOG |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | /****************************************************************************** |
| 42 | * static inlined helper functions |
| 43 | ******************************************************************************/ |
| 44 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | static inline u16 section_length(const u8 *buf) |
| 46 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 47 | return 3 + ((buf[1] & 0x0f) << 8) + buf[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | static inline u16 ts_pid(const u8 *buf) |
| 51 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 52 | return ((buf[1] & 0x1f) << 8) + buf[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | static inline u8 payload(const u8 *tsp) |
| 56 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 57 | if (!(tsp[3] & 0x10)) // no payload? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | return 0; |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 59 | |
| 60 | if (tsp[3] & 0x20) { // adaptation field? |
| 61 | if (tsp[4] > 183) // corrupted data? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | return 0; |
| 63 | else |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 64 | return 184 - 1 - tsp[4]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | } |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | return 184; |
| 68 | } |
| 69 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 70 | static u32 dvb_dmx_crc32(struct dvb_demux_feed *f, const u8 *src, size_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 72 | return (f->feed.sec.crc_val = crc32_be(f->feed.sec.crc_val, src, len)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 75 | static void dvb_dmx_memcopy(struct dvb_demux_feed *f, u8 *d, const u8 *s, |
| 76 | size_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 78 | memcpy(d, s, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | /****************************************************************************** |
| 82 | * Software filter functions |
| 83 | ******************************************************************************/ |
| 84 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 85 | static inline int dvb_dmx_swfilter_payload(struct dvb_demux_feed *feed, |
| 86 | const u8 *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | { |
| 88 | int count = payload(buf); |
| 89 | int p; |
| 90 | //int ccok; |
| 91 | //u8 cc; |
| 92 | |
| 93 | if (count == 0) |
| 94 | return -1; |
| 95 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 96 | p = 188 - count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
| 98 | /* |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 99 | cc = buf[3] & 0x0f; |
| 100 | ccok = ((feed->cc + 1) & 0x0f) == cc; |
| 101 | feed->cc = cc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | if (!ccok) |
| 103 | printk("missed packet!\n"); |
| 104 | */ |
| 105 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 106 | if (buf[1] & 0x40) // PUSI ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | feed->peslen = 0xfffa; |
| 108 | |
| 109 | feed->peslen += count; |
| 110 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 111 | return feed->cb.ts(&buf[p], count, NULL, 0, &feed->feed.ts, DMX_OK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 114 | static int dvb_dmx_swfilter_sectionfilter(struct dvb_demux_feed *feed, |
| 115 | struct dvb_demux_filter *f) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { |
| 117 | u8 neq = 0; |
| 118 | int i; |
| 119 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 120 | for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | u8 xor = f->filter.filter_value[i] ^ feed->feed.sec.secbuf[i]; |
| 122 | |
| 123 | if (f->maskandmode[i] & xor) |
| 124 | return 0; |
| 125 | |
| 126 | neq |= f->maskandnotmode[i] & xor; |
| 127 | } |
| 128 | |
| 129 | if (f->doneq && !neq) |
| 130 | return 0; |
| 131 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 132 | return feed->cb.sec(feed->feed.sec.secbuf, feed->feed.sec.seclen, |
| 133 | NULL, 0, &f->filter, DMX_OK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 136 | static inline int dvb_dmx_swfilter_section_feed(struct dvb_demux_feed *feed) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | { |
| 138 | struct dvb_demux *demux = feed->demux; |
| 139 | struct dvb_demux_filter *f = feed->filter; |
| 140 | struct dmx_section_feed *sec = &feed->feed.sec; |
| 141 | int section_syntax_indicator; |
| 142 | |
| 143 | if (!sec->is_filtering) |
| 144 | return 0; |
| 145 | |
| 146 | if (!f) |
| 147 | return 0; |
| 148 | |
| 149 | if (sec->check_crc) { |
| 150 | section_syntax_indicator = ((sec->secbuf[1] & 0x80) != 0); |
| 151 | if (section_syntax_indicator && |
| 152 | demux->check_crc32(feed, sec->secbuf, sec->seclen)) |
| 153 | return -1; |
| 154 | } |
| 155 | |
| 156 | do { |
| 157 | if (dvb_dmx_swfilter_sectionfilter(feed, f) < 0) |
| 158 | return -1; |
| 159 | } while ((f = f->next) && sec->is_filtering); |
| 160 | |
| 161 | sec->seclen = 0; |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | static void dvb_dmx_swfilter_section_new(struct dvb_demux_feed *feed) |
| 167 | { |
| 168 | struct dmx_section_feed *sec = &feed->feed.sec; |
| 169 | |
| 170 | #ifdef DVB_DEMUX_SECTION_LOSS_LOG |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 171 | if (sec->secbufp < sec->tsfeedp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | int i, n = sec->tsfeedp - sec->secbufp; |
| 173 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 174 | /* |
| 175 | * Section padding is done with 0xff bytes entirely. |
| 176 | * Due to speed reasons, we won't check all of them |
| 177 | * but just first and last. |
| 178 | */ |
| 179 | if (sec->secbuf[0] != 0xff || sec->secbuf[n - 1] != 0xff) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | printk("dvb_demux.c section ts padding loss: %d/%d\n", |
| 181 | n, sec->tsfeedp); |
| 182 | printk("dvb_demux.c pad data:"); |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 183 | for (i = 0; i < n; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | printk(" %02x", sec->secbuf[i]); |
| 185 | printk("\n"); |
| 186 | } |
| 187 | } |
| 188 | #endif |
| 189 | |
| 190 | sec->tsfeedp = sec->secbufp = sec->seclen = 0; |
| 191 | sec->secbuf = sec->secbuf_base; |
| 192 | } |
| 193 | |
| 194 | /* |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 195 | * Losless Section Demux 1.4.1 by Emard |
| 196 | * Valsecchi Patrick: |
| 197 | * - middle of section A (no PUSI) |
| 198 | * - end of section A and start of section B |
| 199 | * (with PUSI pointing to the start of the second section) |
| 200 | * |
| 201 | * In this case, without feed->pusi_seen you'll receive a garbage section |
| 202 | * consisting of the end of section A. Basically because tsfeedp |
| 203 | * is incemented and the use=0 condition is not raised |
| 204 | * when the second packet arrives. |
| 205 | * |
| 206 | * Fix: |
| 207 | * when demux is started, let feed->pusi_seen = 0 to |
| 208 | * prevent initial feeding of garbage from the end of |
| 209 | * previous section. When you for the first time see PUSI=1 |
| 210 | * then set feed->pusi_seen = 1 |
| 211 | */ |
| 212 | static int dvb_dmx_swfilter_section_copy_dump(struct dvb_demux_feed *feed, |
| 213 | const u8 *buf, u8 len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | { |
| 215 | struct dvb_demux *demux = feed->demux; |
| 216 | struct dmx_section_feed *sec = &feed->feed.sec; |
| 217 | u16 limit, seclen, n; |
| 218 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 219 | if (sec->tsfeedp >= DMX_MAX_SECFEED_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | return 0; |
| 221 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 222 | if (sec->tsfeedp + len > DMX_MAX_SECFEED_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | #ifdef DVB_DEMUX_SECTION_LOSS_LOG |
| 224 | printk("dvb_demux.c section buffer full loss: %d/%d\n", |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 225 | sec->tsfeedp + len - DMX_MAX_SECFEED_SIZE, |
| 226 | DMX_MAX_SECFEED_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | #endif |
| 228 | len = DMX_MAX_SECFEED_SIZE - sec->tsfeedp; |
| 229 | } |
| 230 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 231 | if (len <= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | return 0; |
| 233 | |
| 234 | demux->memcopy(feed, sec->secbuf_base + sec->tsfeedp, buf, len); |
| 235 | sec->tsfeedp += len; |
| 236 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 237 | /* |
| 238 | * Dump all the sections we can find in the data (Emard) |
| 239 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | limit = sec->tsfeedp; |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 241 | if (limit > DMX_MAX_SECFEED_SIZE) |
| 242 | return -1; /* internal error should never happen */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
| 244 | /* to be sure always set secbuf */ |
| 245 | sec->secbuf = sec->secbuf_base + sec->secbufp; |
| 246 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 247 | for (n = 0; sec->secbufp + 2 < limit; n++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | seclen = section_length(sec->secbuf); |
Mark Adams | b3967d6 | 2005-11-08 21:35:50 -0800 | [diff] [blame] | 249 | if (seclen <= 0 || seclen > DMX_MAX_SECTION_SIZE |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 250 | || seclen + sec->secbufp > limit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | return 0; |
| 252 | sec->seclen = seclen; |
| 253 | sec->crc_val = ~0; |
| 254 | /* dump [secbuf .. secbuf+seclen) */ |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 255 | if (feed->pusi_seen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | dvb_dmx_swfilter_section_feed(feed); |
| 257 | #ifdef DVB_DEMUX_SECTION_LOSS_LOG |
| 258 | else |
| 259 | printk("dvb_demux.c pusi not seen, discarding section data\n"); |
| 260 | #endif |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 261 | sec->secbufp += seclen; /* secbufp and secbuf moving together is */ |
| 262 | sec->secbuf += seclen; /* redundant but saves pointer arithmetic */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 268 | static int dvb_dmx_swfilter_section_packet(struct dvb_demux_feed *feed, |
| 269 | const u8 *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | { |
| 271 | u8 p, count; |
| 272 | int ccok, dc_i = 0; |
| 273 | u8 cc; |
| 274 | |
| 275 | count = payload(buf); |
| 276 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 277 | if (count == 0) /* count == 0 if no payload or out of range */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | return -1; |
| 279 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 280 | p = 188 - count; /* payload start */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
| 282 | cc = buf[3] & 0x0f; |
| 283 | ccok = ((feed->cc + 1) & 0x0f) == cc; |
| 284 | feed->cc = cc; |
| 285 | |
| 286 | if (buf[3] & 0x20) { |
| 287 | /* adaption field present, check for discontinuity_indicator */ |
| 288 | if ((buf[4] > 0) && (buf[5] & 0x80)) |
| 289 | dc_i = 1; |
| 290 | } |
| 291 | |
| 292 | if (!ccok || dc_i) { |
| 293 | #ifdef DVB_DEMUX_SECTION_LOSS_LOG |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 294 | printk("dvb_demux.c discontinuity detected %d bytes lost\n", |
| 295 | count); |
| 296 | /* |
| 297 | * those bytes under sume circumstances will again be reported |
| 298 | * in the following dvb_dmx_swfilter_section_new |
| 299 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | #endif |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 301 | /* |
| 302 | * Discontinuity detected. Reset pusi_seen = 0 to |
| 303 | * stop feeding of suspicious data until next PUSI=1 arrives |
| 304 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | feed->pusi_seen = 0; |
| 306 | dvb_dmx_swfilter_section_new(feed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | if (buf[1] & 0x40) { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 310 | /* PUSI=1 (is set), section boundary is here */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | if (count > 1 && buf[p] < count) { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 312 | const u8 *before = &buf[p + 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | u8 before_len = buf[p]; |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 314 | const u8 *after = &before[before_len]; |
| 315 | u8 after_len = count - 1 - before_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 317 | dvb_dmx_swfilter_section_copy_dump(feed, before, |
| 318 | before_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | /* before start of new section, set pusi_seen = 1 */ |
| 320 | feed->pusi_seen = 1; |
| 321 | dvb_dmx_swfilter_section_new(feed); |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 322 | dvb_dmx_swfilter_section_copy_dump(feed, after, |
| 323 | after_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | } |
| 325 | #ifdef DVB_DEMUX_SECTION_LOSS_LOG |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 326 | else if (count > 0) |
| 327 | printk("dvb_demux.c PUSI=1 but %d bytes lost\n", count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | #endif |
| 329 | } else { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 330 | /* PUSI=0 (is not set), no section boundary */ |
| 331 | dvb_dmx_swfilter_section_copy_dump(feed, &buf[p], count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | } |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | return 0; |
| 335 | } |
| 336 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 337 | static inline void dvb_dmx_swfilter_packet_type(struct dvb_demux_feed *feed, |
| 338 | const u8 *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 340 | switch (feed->type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | case DMX_TYPE_TS: |
| 342 | if (!feed->feed.ts.is_filtering) |
| 343 | break; |
| 344 | if (feed->ts_type & TS_PACKET) { |
| 345 | if (feed->ts_type & TS_PAYLOAD_ONLY) |
| 346 | dvb_dmx_swfilter_payload(feed, buf); |
| 347 | else |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 348 | feed->cb.ts(buf, 188, NULL, 0, &feed->feed.ts, |
| 349 | DMX_OK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | } |
| 351 | if (feed->ts_type & TS_DECODER) |
| 352 | if (feed->demux->write_to_decoder) |
| 353 | feed->demux->write_to_decoder(feed, buf, 188); |
| 354 | break; |
| 355 | |
| 356 | case DMX_TYPE_SEC: |
| 357 | if (!feed->feed.sec.is_filtering) |
| 358 | break; |
| 359 | if (dvb_dmx_swfilter_section_packet(feed, buf) < 0) |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 360 | feed->feed.sec.seclen = feed->feed.sec.secbufp = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | break; |
| 362 | |
| 363 | default: |
| 364 | break; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | #define DVR_FEED(f) \ |
| 369 | (((f)->type == DMX_TYPE_TS) && \ |
| 370 | ((f)->feed.ts.is_filtering) && \ |
| 371 | (((f)->ts_type & (TS_PACKET|TS_PAYLOAD_ONLY)) == TS_PACKET)) |
| 372 | |
| 373 | static void dvb_dmx_swfilter_packet(struct dvb_demux *demux, const u8 *buf) |
| 374 | { |
| 375 | struct dvb_demux_feed *feed; |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 376 | struct list_head *pos, *head = &demux->feed_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | u16 pid = ts_pid(buf); |
| 378 | int dvr_done = 0; |
| 379 | |
| 380 | list_for_each(pos, head) { |
| 381 | feed = list_entry(pos, struct dvb_demux_feed, list_head); |
| 382 | |
| 383 | if ((feed->pid != pid) && (feed->pid != 0x2000)) |
| 384 | continue; |
| 385 | |
| 386 | /* copy each packet only once to the dvr device, even |
| 387 | * if a PID is in multiple filters (e.g. video + PCR) */ |
| 388 | if ((DVR_FEED(feed)) && (dvr_done++)) |
| 389 | continue; |
| 390 | |
| 391 | if (feed->pid == pid) { |
| 392 | dvb_dmx_swfilter_packet_type(feed, buf); |
| 393 | if (DVR_FEED(feed)) |
| 394 | continue; |
| 395 | } |
| 396 | |
| 397 | if (feed->pid == 0x2000) |
| 398 | feed->cb.ts(buf, 188, NULL, 0, &feed->feed.ts, DMX_OK); |
| 399 | } |
| 400 | } |
| 401 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 402 | void dvb_dmx_swfilter_packets(struct dvb_demux *demux, const u8 *buf, |
| 403 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | { |
| 405 | spin_lock(&demux->lock); |
| 406 | |
| 407 | while (count--) { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 408 | if (buf[0] == 0x47) |
| 409 | dvb_dmx_swfilter_packet(demux, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | buf += 188; |
| 411 | } |
| 412 | |
| 413 | spin_unlock(&demux->lock); |
| 414 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 416 | EXPORT_SYMBOL(dvb_dmx_swfilter_packets); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | |
| 418 | void dvb_dmx_swfilter(struct dvb_demux *demux, const u8 *buf, size_t count) |
| 419 | { |
| 420 | int p = 0, i, j; |
| 421 | |
| 422 | spin_lock(&demux->lock); |
| 423 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 424 | if (demux->tsbufp) { |
| 425 | i = demux->tsbufp; |
| 426 | j = 188 - i; |
| 427 | if (count < j) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | memcpy(&demux->tsbuf[i], buf, count); |
| 429 | demux->tsbufp += count; |
| 430 | goto bailout; |
| 431 | } |
| 432 | memcpy(&demux->tsbuf[i], buf, j); |
| 433 | if (demux->tsbuf[0] == 0x47) |
| 434 | dvb_dmx_swfilter_packet(demux, demux->tsbuf); |
| 435 | demux->tsbufp = 0; |
| 436 | p += j; |
| 437 | } |
| 438 | |
| 439 | while (p < count) { |
| 440 | if (buf[p] == 0x47) { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 441 | if (count - p >= 188) { |
| 442 | dvb_dmx_swfilter_packet(demux, &buf[p]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | p += 188; |
| 444 | } else { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 445 | i = count - p; |
| 446 | memcpy(demux->tsbuf, &buf[p], i); |
| 447 | demux->tsbufp = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | goto bailout; |
| 449 | } |
| 450 | } else |
| 451 | p++; |
| 452 | } |
| 453 | |
| 454 | bailout: |
| 455 | spin_unlock(&demux->lock); |
| 456 | } |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 457 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | EXPORT_SYMBOL(dvb_dmx_swfilter); |
| 459 | |
| 460 | void dvb_dmx_swfilter_204(struct dvb_demux *demux, const u8 *buf, size_t count) |
| 461 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 462 | int p = 0, i, j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | u8 tmppack[188]; |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 464 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | spin_lock(&demux->lock); |
| 466 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 467 | if (demux->tsbufp) { |
| 468 | i = demux->tsbufp; |
| 469 | j = 204 - i; |
| 470 | if (count < j) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | memcpy(&demux->tsbuf[i], buf, count); |
| 472 | demux->tsbufp += count; |
| 473 | goto bailout; |
| 474 | } |
| 475 | memcpy(&demux->tsbuf[i], buf, j); |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 476 | if ((demux->tsbuf[0] == 0x47) | (demux->tsbuf[0] == 0xB8)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | memcpy(tmppack, demux->tsbuf, 188); |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 478 | if (tmppack[0] == 0xB8) |
| 479 | tmppack[0] = 0x47; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | dvb_dmx_swfilter_packet(demux, tmppack); |
| 481 | } |
| 482 | demux->tsbufp = 0; |
| 483 | p += j; |
| 484 | } |
| 485 | |
| 486 | while (p < count) { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 487 | if ((buf[p] == 0x47) | (buf[p] == 0xB8)) { |
| 488 | if (count - p >= 204) { |
| 489 | memcpy(tmppack, &buf[p], 188); |
| 490 | if (tmppack[0] == 0xB8) |
| 491 | tmppack[0] = 0x47; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | dvb_dmx_swfilter_packet(demux, tmppack); |
| 493 | p += 204; |
| 494 | } else { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 495 | i = count - p; |
| 496 | memcpy(demux->tsbuf, &buf[p], i); |
| 497 | demux->tsbufp = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | goto bailout; |
| 499 | } |
| 500 | } else { |
| 501 | p++; |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | bailout: |
| 506 | spin_unlock(&demux->lock); |
| 507 | } |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 508 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | EXPORT_SYMBOL(dvb_dmx_swfilter_204); |
| 510 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 511 | static struct dvb_demux_filter *dvb_dmx_filter_alloc(struct dvb_demux *demux) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | { |
| 513 | int i; |
| 514 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 515 | for (i = 0; i < demux->filternum; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | if (demux->filter[i].state == DMX_STATE_FREE) |
| 517 | break; |
| 518 | |
| 519 | if (i == demux->filternum) |
| 520 | return NULL; |
| 521 | |
| 522 | demux->filter[i].state = DMX_STATE_ALLOCATED; |
| 523 | |
| 524 | return &demux->filter[i]; |
| 525 | } |
| 526 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 527 | static struct dvb_demux_feed *dvb_dmx_feed_alloc(struct dvb_demux *demux) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | { |
| 529 | int i; |
| 530 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 531 | for (i = 0; i < demux->feednum; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | if (demux->feed[i].state == DMX_STATE_FREE) |
| 533 | break; |
| 534 | |
| 535 | if (i == demux->feednum) |
| 536 | return NULL; |
| 537 | |
| 538 | demux->feed[i].state = DMX_STATE_ALLOCATED; |
| 539 | |
| 540 | return &demux->feed[i]; |
| 541 | } |
| 542 | |
| 543 | static int dvb_demux_feed_find(struct dvb_demux_feed *feed) |
| 544 | { |
| 545 | struct dvb_demux_feed *entry; |
| 546 | |
| 547 | list_for_each_entry(entry, &feed->demux->feed_list, list_head) |
| 548 | if (entry == feed) |
| 549 | return 1; |
| 550 | |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | static void dvb_demux_feed_add(struct dvb_demux_feed *feed) |
| 555 | { |
| 556 | spin_lock_irq(&feed->demux->lock); |
| 557 | if (dvb_demux_feed_find(feed)) { |
| 558 | printk(KERN_ERR "%s: feed already in list (type=%x state=%x pid=%x)\n", |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 559 | __FUNCTION__, feed->type, feed->state, feed->pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | goto out; |
| 561 | } |
| 562 | |
| 563 | list_add(&feed->list_head, &feed->demux->feed_list); |
| 564 | out: |
| 565 | spin_unlock_irq(&feed->demux->lock); |
| 566 | } |
| 567 | |
| 568 | static void dvb_demux_feed_del(struct dvb_demux_feed *feed) |
| 569 | { |
| 570 | spin_lock_irq(&feed->demux->lock); |
| 571 | if (!(dvb_demux_feed_find(feed))) { |
| 572 | printk(KERN_ERR "%s: feed not in list (type=%x state=%x pid=%x)\n", |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 573 | __FUNCTION__, feed->type, feed->state, feed->pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | goto out; |
| 575 | } |
| 576 | |
| 577 | list_del(&feed->list_head); |
| 578 | out: |
| 579 | spin_unlock_irq(&feed->demux->lock); |
| 580 | } |
| 581 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 582 | static int dmx_ts_feed_set(struct dmx_ts_feed *ts_feed, u16 pid, int ts_type, |
| 583 | enum dmx_ts_pes pes_type, |
| 584 | size_t circular_buffer_size, struct timespec timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 586 | struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | struct dvb_demux *demux = feed->demux; |
| 588 | |
| 589 | if (pid > DMX_MAX_PID) |
| 590 | return -EINVAL; |
| 591 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 592 | if (mutex_lock_interruptible(&demux->mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | return -ERESTARTSYS; |
| 594 | |
| 595 | if (ts_type & TS_DECODER) { |
| 596 | if (pes_type >= DMX_TS_PES_OTHER) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 597 | mutex_unlock(&demux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | return -EINVAL; |
| 599 | } |
| 600 | |
| 601 | if (demux->pesfilter[pes_type] && |
| 602 | demux->pesfilter[pes_type] != feed) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 603 | mutex_unlock(&demux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | return -EINVAL; |
| 605 | } |
| 606 | |
| 607 | demux->pesfilter[pes_type] = feed; |
| 608 | demux->pids[pes_type] = pid; |
| 609 | } |
| 610 | |
| 611 | dvb_demux_feed_add(feed); |
| 612 | |
| 613 | feed->pid = pid; |
| 614 | feed->buffer_size = circular_buffer_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | feed->timeout = timeout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | feed->ts_type = ts_type; |
| 617 | feed->pes_type = pes_type; |
| 618 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | if (feed->buffer_size) { |
| 620 | #ifdef NOBUFS |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 621 | feed->buffer = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | #else |
| 623 | feed->buffer = vmalloc(feed->buffer_size); |
| 624 | if (!feed->buffer) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 625 | mutex_unlock(&demux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | return -ENOMEM; |
| 627 | } |
| 628 | #endif |
| 629 | } |
| 630 | |
| 631 | feed->state = DMX_STATE_READY; |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 632 | mutex_unlock(&demux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | |
| 634 | return 0; |
| 635 | } |
| 636 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 637 | static int dmx_ts_feed_start_filtering(struct dmx_ts_feed *ts_feed) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 639 | struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | struct dvb_demux *demux = feed->demux; |
| 641 | int ret; |
| 642 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 643 | if (mutex_lock_interruptible(&demux->mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | return -ERESTARTSYS; |
| 645 | |
| 646 | if (feed->state != DMX_STATE_READY || feed->type != DMX_TYPE_TS) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 647 | mutex_unlock(&demux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | return -EINVAL; |
| 649 | } |
| 650 | |
| 651 | if (!demux->start_feed) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 652 | mutex_unlock(&demux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | return -ENODEV; |
| 654 | } |
| 655 | |
| 656 | if ((ret = demux->start_feed(feed)) < 0) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 657 | mutex_unlock(&demux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | return ret; |
| 659 | } |
| 660 | |
| 661 | spin_lock_irq(&demux->lock); |
| 662 | ts_feed->is_filtering = 1; |
| 663 | feed->state = DMX_STATE_GO; |
| 664 | spin_unlock_irq(&demux->lock); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 665 | mutex_unlock(&demux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | |
| 667 | return 0; |
| 668 | } |
| 669 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 670 | static int dmx_ts_feed_stop_filtering(struct dmx_ts_feed *ts_feed) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 672 | struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | struct dvb_demux *demux = feed->demux; |
| 674 | int ret; |
| 675 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 676 | if (mutex_lock_interruptible(&demux->mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | return -ERESTARTSYS; |
| 678 | |
| 679 | if (feed->state < DMX_STATE_GO) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 680 | mutex_unlock(&demux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | return -EINVAL; |
| 682 | } |
| 683 | |
| 684 | if (!demux->stop_feed) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 685 | mutex_unlock(&demux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | return -ENODEV; |
| 687 | } |
| 688 | |
| 689 | ret = demux->stop_feed(feed); |
| 690 | |
| 691 | spin_lock_irq(&demux->lock); |
| 692 | ts_feed->is_filtering = 0; |
| 693 | feed->state = DMX_STATE_ALLOCATED; |
| 694 | spin_unlock_irq(&demux->lock); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 695 | mutex_unlock(&demux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | |
| 697 | return ret; |
| 698 | } |
| 699 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 700 | static int dvbdmx_allocate_ts_feed(struct dmx_demux *dmx, |
| 701 | struct dmx_ts_feed **ts_feed, |
| 702 | dmx_ts_cb callback) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 704 | struct dvb_demux *demux = (struct dvb_demux *)dmx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | struct dvb_demux_feed *feed; |
| 706 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 707 | if (mutex_lock_interruptible(&demux->mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | return -ERESTARTSYS; |
| 709 | |
| 710 | if (!(feed = dvb_dmx_feed_alloc(demux))) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 711 | mutex_unlock(&demux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | return -EBUSY; |
| 713 | } |
| 714 | |
| 715 | feed->type = DMX_TYPE_TS; |
| 716 | feed->cb.ts = callback; |
| 717 | feed->demux = demux; |
| 718 | feed->pid = 0xffff; |
| 719 | feed->peslen = 0xfffa; |
| 720 | feed->buffer = NULL; |
| 721 | |
| 722 | (*ts_feed) = &feed->feed.ts; |
| 723 | (*ts_feed)->parent = dmx; |
| 724 | (*ts_feed)->priv = NULL; |
| 725 | (*ts_feed)->is_filtering = 0; |
| 726 | (*ts_feed)->start_filtering = dmx_ts_feed_start_filtering; |
| 727 | (*ts_feed)->stop_filtering = dmx_ts_feed_stop_filtering; |
| 728 | (*ts_feed)->set = dmx_ts_feed_set; |
| 729 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | if (!(feed->filter = dvb_dmx_filter_alloc(demux))) { |
| 731 | feed->state = DMX_STATE_FREE; |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 732 | mutex_unlock(&demux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | return -EBUSY; |
| 734 | } |
| 735 | |
| 736 | feed->filter->type = DMX_TYPE_TS; |
| 737 | feed->filter->feed = feed; |
| 738 | feed->filter->state = DMX_STATE_READY; |
| 739 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 740 | mutex_unlock(&demux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | |
| 742 | return 0; |
| 743 | } |
| 744 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 745 | static int dvbdmx_release_ts_feed(struct dmx_demux *dmx, |
| 746 | struct dmx_ts_feed *ts_feed) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 748 | struct dvb_demux *demux = (struct dvb_demux *)dmx; |
| 749 | struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 751 | if (mutex_lock_interruptible(&demux->mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | return -ERESTARTSYS; |
| 753 | |
| 754 | if (feed->state == DMX_STATE_FREE) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 755 | mutex_unlock(&demux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | return -EINVAL; |
| 757 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | #ifndef NOBUFS |
| 759 | vfree(feed->buffer); |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 760 | feed->buffer = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | #endif |
| 762 | |
| 763 | feed->state = DMX_STATE_FREE; |
| 764 | feed->filter->state = DMX_STATE_FREE; |
| 765 | |
| 766 | dvb_demux_feed_del(feed); |
| 767 | |
| 768 | feed->pid = 0xffff; |
| 769 | |
| 770 | if (feed->ts_type & TS_DECODER && feed->pes_type < DMX_TS_PES_OTHER) |
| 771 | demux->pesfilter[feed->pes_type] = NULL; |
| 772 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 773 | mutex_unlock(&demux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | return 0; |
| 775 | } |
| 776 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | /****************************************************************************** |
| 778 | * dmx_section_feed API calls |
| 779 | ******************************************************************************/ |
| 780 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 781 | static int dmx_section_feed_allocate_filter(struct dmx_section_feed *feed, |
| 782 | struct dmx_section_filter **filter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 784 | struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | struct dvb_demux *dvbdemux = dvbdmxfeed->demux; |
| 786 | struct dvb_demux_filter *dvbdmxfilter; |
| 787 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 788 | if (mutex_lock_interruptible(&dvbdemux->mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | return -ERESTARTSYS; |
| 790 | |
| 791 | dvbdmxfilter = dvb_dmx_filter_alloc(dvbdemux); |
| 792 | if (!dvbdmxfilter) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 793 | mutex_unlock(&dvbdemux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | return -EBUSY; |
| 795 | } |
| 796 | |
| 797 | spin_lock_irq(&dvbdemux->lock); |
| 798 | *filter = &dvbdmxfilter->filter; |
| 799 | (*filter)->parent = feed; |
| 800 | (*filter)->priv = NULL; |
| 801 | dvbdmxfilter->feed = dvbdmxfeed; |
| 802 | dvbdmxfilter->type = DMX_TYPE_SEC; |
| 803 | dvbdmxfilter->state = DMX_STATE_READY; |
| 804 | dvbdmxfilter->next = dvbdmxfeed->filter; |
| 805 | dvbdmxfeed->filter = dvbdmxfilter; |
| 806 | spin_unlock_irq(&dvbdemux->lock); |
| 807 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 808 | mutex_unlock(&dvbdemux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | return 0; |
| 810 | } |
| 811 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 812 | static int dmx_section_feed_set(struct dmx_section_feed *feed, |
| 813 | u16 pid, size_t circular_buffer_size, |
| 814 | int check_crc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 816 | struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | struct dvb_demux *dvbdmx = dvbdmxfeed->demux; |
| 818 | |
| 819 | if (pid > 0x1fff) |
| 820 | return -EINVAL; |
| 821 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 822 | if (mutex_lock_interruptible(&dvbdmx->mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | return -ERESTARTSYS; |
| 824 | |
| 825 | dvb_demux_feed_add(dvbdmxfeed); |
| 826 | |
| 827 | dvbdmxfeed->pid = pid; |
| 828 | dvbdmxfeed->buffer_size = circular_buffer_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | dvbdmxfeed->feed.sec.check_crc = check_crc; |
| 830 | |
| 831 | #ifdef NOBUFS |
| 832 | dvbdmxfeed->buffer = NULL; |
| 833 | #else |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 834 | dvbdmxfeed->buffer = vmalloc(dvbdmxfeed->buffer_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | if (!dvbdmxfeed->buffer) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 836 | mutex_unlock(&dvbdmx->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | return -ENOMEM; |
| 838 | } |
| 839 | #endif |
| 840 | |
| 841 | dvbdmxfeed->state = DMX_STATE_READY; |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 842 | mutex_unlock(&dvbdmx->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | return 0; |
| 844 | } |
| 845 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | static void prepare_secfilters(struct dvb_demux_feed *dvbdmxfeed) |
| 847 | { |
| 848 | int i; |
| 849 | struct dvb_demux_filter *f; |
| 850 | struct dmx_section_filter *sf; |
| 851 | u8 mask, mode, doneq; |
| 852 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 853 | if (!(f = dvbdmxfeed->filter)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | return; |
| 855 | do { |
| 856 | sf = &f->filter; |
| 857 | doneq = 0; |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 858 | for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | mode = sf->filter_mode[i]; |
| 860 | mask = sf->filter_mask[i]; |
| 861 | f->maskandmode[i] = mask & mode; |
| 862 | doneq |= f->maskandnotmode[i] = mask & ~mode; |
| 863 | } |
| 864 | f->doneq = doneq ? 1 : 0; |
| 865 | } while ((f = f->next)); |
| 866 | } |
| 867 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | static int dmx_section_feed_start_filtering(struct dmx_section_feed *feed) |
| 869 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 870 | struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | struct dvb_demux *dvbdmx = dvbdmxfeed->demux; |
| 872 | int ret; |
| 873 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 874 | if (mutex_lock_interruptible(&dvbdmx->mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | return -ERESTARTSYS; |
| 876 | |
| 877 | if (feed->is_filtering) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 878 | mutex_unlock(&dvbdmx->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | return -EBUSY; |
| 880 | } |
| 881 | |
| 882 | if (!dvbdmxfeed->filter) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 883 | mutex_unlock(&dvbdmx->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | return -EINVAL; |
| 885 | } |
| 886 | |
| 887 | dvbdmxfeed->feed.sec.tsfeedp = 0; |
| 888 | dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base; |
| 889 | dvbdmxfeed->feed.sec.secbufp = 0; |
| 890 | dvbdmxfeed->feed.sec.seclen = 0; |
| 891 | |
| 892 | if (!dvbdmx->start_feed) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 893 | mutex_unlock(&dvbdmx->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | return -ENODEV; |
| 895 | } |
| 896 | |
| 897 | prepare_secfilters(dvbdmxfeed); |
| 898 | |
| 899 | if ((ret = dvbdmx->start_feed(dvbdmxfeed)) < 0) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 900 | mutex_unlock(&dvbdmx->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | return ret; |
| 902 | } |
| 903 | |
| 904 | spin_lock_irq(&dvbdmx->lock); |
| 905 | feed->is_filtering = 1; |
| 906 | dvbdmxfeed->state = DMX_STATE_GO; |
| 907 | spin_unlock_irq(&dvbdmx->lock); |
| 908 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 909 | mutex_unlock(&dvbdmx->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | return 0; |
| 911 | } |
| 912 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 913 | static int dmx_section_feed_stop_filtering(struct dmx_section_feed *feed) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 915 | struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | struct dvb_demux *dvbdmx = dvbdmxfeed->demux; |
| 917 | int ret; |
| 918 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 919 | if (mutex_lock_interruptible(&dvbdmx->mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | return -ERESTARTSYS; |
| 921 | |
| 922 | if (!dvbdmx->stop_feed) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 923 | mutex_unlock(&dvbdmx->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | return -ENODEV; |
| 925 | } |
| 926 | |
| 927 | ret = dvbdmx->stop_feed(dvbdmxfeed); |
| 928 | |
| 929 | spin_lock_irq(&dvbdmx->lock); |
| 930 | dvbdmxfeed->state = DMX_STATE_READY; |
| 931 | feed->is_filtering = 0; |
| 932 | spin_unlock_irq(&dvbdmx->lock); |
| 933 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 934 | mutex_unlock(&dvbdmx->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | return ret; |
| 936 | } |
| 937 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | static int dmx_section_feed_release_filter(struct dmx_section_feed *feed, |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 939 | struct dmx_section_filter *filter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 941 | struct dvb_demux_filter *dvbdmxfilter = (struct dvb_demux_filter *)filter, *f; |
| 942 | struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | struct dvb_demux *dvbdmx = dvbdmxfeed->demux; |
| 944 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 945 | if (mutex_lock_interruptible(&dvbdmx->mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | return -ERESTARTSYS; |
| 947 | |
| 948 | if (dvbdmxfilter->feed != dvbdmxfeed) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 949 | mutex_unlock(&dvbdmx->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | return -EINVAL; |
| 951 | } |
| 952 | |
| 953 | if (feed->is_filtering) |
| 954 | feed->stop_filtering(feed); |
| 955 | |
| 956 | spin_lock_irq(&dvbdmx->lock); |
| 957 | f = dvbdmxfeed->filter; |
| 958 | |
| 959 | if (f == dvbdmxfilter) { |
| 960 | dvbdmxfeed->filter = dvbdmxfilter->next; |
| 961 | } else { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 962 | while (f->next != dvbdmxfilter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | f = f->next; |
| 964 | f->next = f->next->next; |
| 965 | } |
| 966 | |
| 967 | dvbdmxfilter->state = DMX_STATE_FREE; |
| 968 | spin_unlock_irq(&dvbdmx->lock); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 969 | mutex_unlock(&dvbdmx->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | return 0; |
| 971 | } |
| 972 | |
| 973 | static int dvbdmx_allocate_section_feed(struct dmx_demux *demux, |
| 974 | struct dmx_section_feed **feed, |
| 975 | dmx_section_cb callback) |
| 976 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 977 | struct dvb_demux *dvbdmx = (struct dvb_demux *)demux; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | struct dvb_demux_feed *dvbdmxfeed; |
| 979 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 980 | if (mutex_lock_interruptible(&dvbdmx->mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | return -ERESTARTSYS; |
| 982 | |
| 983 | if (!(dvbdmxfeed = dvb_dmx_feed_alloc(dvbdmx))) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 984 | mutex_unlock(&dvbdmx->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | return -EBUSY; |
| 986 | } |
| 987 | |
| 988 | dvbdmxfeed->type = DMX_TYPE_SEC; |
| 989 | dvbdmxfeed->cb.sec = callback; |
| 990 | dvbdmxfeed->demux = dvbdmx; |
| 991 | dvbdmxfeed->pid = 0xffff; |
| 992 | dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base; |
| 993 | dvbdmxfeed->feed.sec.secbufp = dvbdmxfeed->feed.sec.seclen = 0; |
| 994 | dvbdmxfeed->feed.sec.tsfeedp = 0; |
| 995 | dvbdmxfeed->filter = NULL; |
| 996 | dvbdmxfeed->buffer = NULL; |
| 997 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 998 | (*feed) = &dvbdmxfeed->feed.sec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | (*feed)->is_filtering = 0; |
| 1000 | (*feed)->parent = demux; |
| 1001 | (*feed)->priv = NULL; |
| 1002 | |
| 1003 | (*feed)->set = dmx_section_feed_set; |
| 1004 | (*feed)->allocate_filter = dmx_section_feed_allocate_filter; |
| 1005 | (*feed)->start_filtering = dmx_section_feed_start_filtering; |
| 1006 | (*feed)->stop_filtering = dmx_section_feed_stop_filtering; |
| 1007 | (*feed)->release_filter = dmx_section_feed_release_filter; |
| 1008 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1009 | mutex_unlock(&dvbdmx->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | return 0; |
| 1011 | } |
| 1012 | |
| 1013 | static int dvbdmx_release_section_feed(struct dmx_demux *demux, |
| 1014 | struct dmx_section_feed *feed) |
| 1015 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1016 | struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed; |
| 1017 | struct dvb_demux *dvbdmx = (struct dvb_demux *)demux; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1019 | if (mutex_lock_interruptible(&dvbdmx->mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | return -ERESTARTSYS; |
| 1021 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1022 | if (dvbdmxfeed->state == DMX_STATE_FREE) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1023 | mutex_unlock(&dvbdmx->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | return -EINVAL; |
| 1025 | } |
| 1026 | #ifndef NOBUFS |
| 1027 | vfree(dvbdmxfeed->buffer); |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1028 | dvbdmxfeed->buffer = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | #endif |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1030 | dvbdmxfeed->state = DMX_STATE_FREE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | |
| 1032 | dvb_demux_feed_del(dvbdmxfeed); |
| 1033 | |
| 1034 | dvbdmxfeed->pid = 0xffff; |
| 1035 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1036 | mutex_unlock(&dvbdmx->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | return 0; |
| 1038 | } |
| 1039 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | /****************************************************************************** |
| 1041 | * dvb_demux kernel data API calls |
| 1042 | ******************************************************************************/ |
| 1043 | |
| 1044 | static int dvbdmx_open(struct dmx_demux *demux) |
| 1045 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1046 | struct dvb_demux *dvbdemux = (struct dvb_demux *)demux; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | |
| 1048 | if (dvbdemux->users >= MAX_DVB_DEMUX_USERS) |
| 1049 | return -EUSERS; |
| 1050 | |
| 1051 | dvbdemux->users++; |
| 1052 | return 0; |
| 1053 | } |
| 1054 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | static int dvbdmx_close(struct dmx_demux *demux) |
| 1056 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1057 | struct dvb_demux *dvbdemux = (struct dvb_demux *)demux; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | |
| 1059 | if (dvbdemux->users == 0) |
| 1060 | return -ENODEV; |
| 1061 | |
| 1062 | dvbdemux->users--; |
| 1063 | //FIXME: release any unneeded resources if users==0 |
| 1064 | return 0; |
| 1065 | } |
| 1066 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | static int dvbdmx_write(struct dmx_demux *demux, const char *buf, size_t count) |
| 1068 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1069 | struct dvb_demux *dvbdemux = (struct dvb_demux *)demux; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | |
| 1071 | if ((!demux->frontend) || (demux->frontend->source != DMX_MEMORY_FE)) |
| 1072 | return -EINVAL; |
| 1073 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1074 | if (mutex_lock_interruptible(&dvbdemux->mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | return -ERESTARTSYS; |
| 1076 | dvb_dmx_swfilter(dvbdemux, buf, count); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1077 | mutex_unlock(&dvbdemux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | |
| 1079 | if (signal_pending(current)) |
| 1080 | return -EINTR; |
| 1081 | return count; |
| 1082 | } |
| 1083 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1084 | static int dvbdmx_add_frontend(struct dmx_demux *demux, |
| 1085 | struct dmx_frontend *frontend) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1087 | struct dvb_demux *dvbdemux = (struct dvb_demux *)demux; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | struct list_head *head = &dvbdemux->frontend_list; |
| 1089 | |
| 1090 | list_add(&(frontend->connectivity_list), head); |
| 1091 | |
| 1092 | return 0; |
| 1093 | } |
| 1094 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1095 | static int dvbdmx_remove_frontend(struct dmx_demux *demux, |
| 1096 | struct dmx_frontend *frontend) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1098 | struct dvb_demux *dvbdemux = (struct dvb_demux *)demux; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | struct list_head *pos, *n, *head = &dvbdemux->frontend_list; |
| 1100 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1101 | list_for_each_safe(pos, n, head) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | if (DMX_FE_ENTRY(pos) == frontend) { |
| 1103 | list_del(pos); |
| 1104 | return 0; |
| 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | return -ENODEV; |
| 1109 | } |
| 1110 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1111 | static struct list_head *dvbdmx_get_frontends(struct dmx_demux *demux) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1113 | struct dvb_demux *dvbdemux = (struct dvb_demux *)demux; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | |
| 1115 | if (list_empty(&dvbdemux->frontend_list)) |
| 1116 | return NULL; |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | return &dvbdemux->frontend_list; |
| 1119 | } |
| 1120 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1121 | static int dvbdmx_connect_frontend(struct dmx_demux *demux, |
| 1122 | struct dmx_frontend *frontend) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1124 | struct dvb_demux *dvbdemux = (struct dvb_demux *)demux; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | |
| 1126 | if (demux->frontend) |
| 1127 | return -EINVAL; |
| 1128 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1129 | if (mutex_lock_interruptible(&dvbdemux->mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | return -ERESTARTSYS; |
| 1131 | |
| 1132 | demux->frontend = frontend; |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1133 | mutex_unlock(&dvbdemux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | return 0; |
| 1135 | } |
| 1136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | static int dvbdmx_disconnect_frontend(struct dmx_demux *demux) |
| 1138 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1139 | struct dvb_demux *dvbdemux = (struct dvb_demux *)demux; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1141 | if (mutex_lock_interruptible(&dvbdemux->mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | return -ERESTARTSYS; |
| 1143 | |
| 1144 | demux->frontend = NULL; |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1145 | mutex_unlock(&dvbdemux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | return 0; |
| 1147 | } |
| 1148 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1149 | static int dvbdmx_get_pes_pids(struct dmx_demux *demux, u16 * pids) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1151 | struct dvb_demux *dvbdemux = (struct dvb_demux *)demux; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1153 | memcpy(pids, dvbdemux->pids, 5 * sizeof(u16)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | return 0; |
| 1155 | } |
| 1156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | int dvb_dmx_init(struct dvb_demux *dvbdemux) |
| 1158 | { |
Andreas Oberritter | 9365346 | 2005-09-09 13:02:23 -0700 | [diff] [blame] | 1159 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | struct dmx_demux *dmx = &dvbdemux->dmx; |
| 1161 | |
| 1162 | dvbdemux->users = 0; |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1163 | dvbdemux->filter = vmalloc(dvbdemux->filternum * sizeof(struct dvb_demux_filter)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | |
| 1165 | if (!dvbdemux->filter) |
| 1166 | return -ENOMEM; |
| 1167 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1168 | dvbdemux->feed = vmalloc(dvbdemux->feednum * sizeof(struct dvb_demux_feed)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | if (!dvbdemux->feed) { |
| 1170 | vfree(dvbdemux->filter); |
| 1171 | return -ENOMEM; |
| 1172 | } |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1173 | for (i = 0; i < dvbdemux->filternum; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | dvbdemux->filter[i].state = DMX_STATE_FREE; |
| 1175 | dvbdemux->filter[i].index = i; |
| 1176 | } |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1177 | for (i = 0; i < dvbdemux->feednum; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | dvbdemux->feed[i].state = DMX_STATE_FREE; |
| 1179 | dvbdemux->feed[i].index = i; |
| 1180 | } |
Andreas Oberritter | db574d7 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1181 | |
| 1182 | INIT_LIST_HEAD(&dvbdemux->frontend_list); |
| 1183 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1184 | for (i = 0; i < DMX_TS_PES_OTHER; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | dvbdemux->pesfilter[i] = NULL; |
| 1186 | dvbdemux->pids[i] = 0xffff; |
| 1187 | } |
| 1188 | |
| 1189 | INIT_LIST_HEAD(&dvbdemux->feed_list); |
| 1190 | |
| 1191 | dvbdemux->playing = 0; |
| 1192 | dvbdemux->recording = 0; |
| 1193 | dvbdemux->tsbufp = 0; |
| 1194 | |
| 1195 | if (!dvbdemux->check_crc32) |
| 1196 | dvbdemux->check_crc32 = dvb_dmx_crc32; |
| 1197 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1198 | if (!dvbdemux->memcopy) |
| 1199 | dvbdemux->memcopy = dvb_dmx_memcopy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | |
| 1201 | dmx->frontend = NULL; |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1202 | dmx->priv = dvbdemux; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | dmx->open = dvbdmx_open; |
| 1204 | dmx->close = dvbdmx_close; |
| 1205 | dmx->write = dvbdmx_write; |
| 1206 | dmx->allocate_ts_feed = dvbdmx_allocate_ts_feed; |
| 1207 | dmx->release_ts_feed = dvbdmx_release_ts_feed; |
| 1208 | dmx->allocate_section_feed = dvbdmx_allocate_section_feed; |
| 1209 | dmx->release_section_feed = dvbdmx_release_section_feed; |
| 1210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | dmx->add_frontend = dvbdmx_add_frontend; |
| 1212 | dmx->remove_frontend = dvbdmx_remove_frontend; |
| 1213 | dmx->get_frontends = dvbdmx_get_frontends; |
| 1214 | dmx->connect_frontend = dvbdmx_connect_frontend; |
| 1215 | dmx->disconnect_frontend = dvbdmx_disconnect_frontend; |
| 1216 | dmx->get_pes_pids = dvbdmx_get_pes_pids; |
| 1217 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1218 | mutex_init(&dvbdemux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | spin_lock_init(&dvbdemux->lock); |
| 1220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | return 0; |
| 1222 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1224 | EXPORT_SYMBOL(dvb_dmx_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | |
Andreas Oberritter | 9365346 | 2005-09-09 13:02:23 -0700 | [diff] [blame] | 1226 | void dvb_dmx_release(struct dvb_demux *dvbdemux) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | vfree(dvbdemux->filter); |
| 1229 | vfree(dvbdemux->feed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | } |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | EXPORT_SYMBOL(dvb_dmx_release); |