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) && \ |
Andreas Oberritter | 4a24ce3 | 2008-04-22 14:45:47 -0300 | [diff] [blame] | 371 | (((f)->ts_type & (TS_PACKET | TS_DEMUX)) == TS_PACKET)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | |
| 373 | static void dvb_dmx_swfilter_packet(struct dvb_demux *demux, const u8 *buf) |
| 374 | { |
| 375 | struct dvb_demux_feed *feed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | u16 pid = ts_pid(buf); |
| 377 | int dvr_done = 0; |
| 378 | |
Trent Piepho | 7948261 | 2007-10-10 05:37:39 -0300 | [diff] [blame] | 379 | list_for_each_entry(feed, &demux->feed_list, list_head) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | if ((feed->pid != pid) && (feed->pid != 0x2000)) |
| 381 | continue; |
| 382 | |
| 383 | /* copy each packet only once to the dvr device, even |
| 384 | * if a PID is in multiple filters (e.g. video + PCR) */ |
| 385 | if ((DVR_FEED(feed)) && (dvr_done++)) |
| 386 | continue; |
| 387 | |
| 388 | if (feed->pid == pid) { |
| 389 | dvb_dmx_swfilter_packet_type(feed, buf); |
| 390 | if (DVR_FEED(feed)) |
| 391 | continue; |
| 392 | } |
| 393 | |
| 394 | if (feed->pid == 0x2000) |
| 395 | feed->cb.ts(buf, 188, NULL, 0, &feed->feed.ts, DMX_OK); |
| 396 | } |
| 397 | } |
| 398 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 399 | void dvb_dmx_swfilter_packets(struct dvb_demux *demux, const u8 *buf, |
| 400 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | { |
| 402 | spin_lock(&demux->lock); |
| 403 | |
| 404 | while (count--) { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 405 | if (buf[0] == 0x47) |
| 406 | dvb_dmx_swfilter_packet(demux, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | buf += 188; |
| 408 | } |
| 409 | |
| 410 | spin_unlock(&demux->lock); |
| 411 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 413 | EXPORT_SYMBOL(dvb_dmx_swfilter_packets); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | |
| 415 | void dvb_dmx_swfilter(struct dvb_demux *demux, const u8 *buf, size_t count) |
| 416 | { |
| 417 | int p = 0, i, j; |
| 418 | |
| 419 | spin_lock(&demux->lock); |
| 420 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 421 | if (demux->tsbufp) { |
| 422 | i = demux->tsbufp; |
| 423 | j = 188 - i; |
| 424 | if (count < j) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | memcpy(&demux->tsbuf[i], buf, count); |
| 426 | demux->tsbufp += count; |
| 427 | goto bailout; |
| 428 | } |
| 429 | memcpy(&demux->tsbuf[i], buf, j); |
| 430 | if (demux->tsbuf[0] == 0x47) |
| 431 | dvb_dmx_swfilter_packet(demux, demux->tsbuf); |
| 432 | demux->tsbufp = 0; |
| 433 | p += j; |
| 434 | } |
| 435 | |
| 436 | while (p < count) { |
| 437 | if (buf[p] == 0x47) { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 438 | if (count - p >= 188) { |
| 439 | dvb_dmx_swfilter_packet(demux, &buf[p]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | p += 188; |
| 441 | } else { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 442 | i = count - p; |
| 443 | memcpy(demux->tsbuf, &buf[p], i); |
| 444 | demux->tsbufp = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | goto bailout; |
| 446 | } |
| 447 | } else |
| 448 | p++; |
| 449 | } |
| 450 | |
| 451 | bailout: |
| 452 | spin_unlock(&demux->lock); |
| 453 | } |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 454 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | EXPORT_SYMBOL(dvb_dmx_swfilter); |
| 456 | |
| 457 | void dvb_dmx_swfilter_204(struct dvb_demux *demux, const u8 *buf, size_t count) |
| 458 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 459 | int p = 0, i, j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | u8 tmppack[188]; |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 461 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | spin_lock(&demux->lock); |
| 463 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 464 | if (demux->tsbufp) { |
| 465 | i = demux->tsbufp; |
| 466 | j = 204 - i; |
| 467 | if (count < j) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | memcpy(&demux->tsbuf[i], buf, count); |
| 469 | demux->tsbufp += count; |
| 470 | goto bailout; |
| 471 | } |
| 472 | memcpy(&demux->tsbuf[i], buf, j); |
Dave Jones | ec675bc | 2006-06-23 09:01:42 -0300 | [diff] [blame] | 473 | if ((demux->tsbuf[0] == 0x47) || (demux->tsbuf[0] == 0xB8)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | memcpy(tmppack, demux->tsbuf, 188); |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 475 | if (tmppack[0] == 0xB8) |
| 476 | tmppack[0] = 0x47; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | dvb_dmx_swfilter_packet(demux, tmppack); |
| 478 | } |
| 479 | demux->tsbufp = 0; |
| 480 | p += j; |
| 481 | } |
| 482 | |
| 483 | while (p < count) { |
Dave Jones | ec675bc | 2006-06-23 09:01:42 -0300 | [diff] [blame] | 484 | if ((buf[p] == 0x47) || (buf[p] == 0xB8)) { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 485 | if (count - p >= 204) { |
| 486 | memcpy(tmppack, &buf[p], 188); |
| 487 | if (tmppack[0] == 0xB8) |
| 488 | tmppack[0] = 0x47; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | dvb_dmx_swfilter_packet(demux, tmppack); |
| 490 | p += 204; |
| 491 | } else { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 492 | i = count - p; |
| 493 | memcpy(demux->tsbuf, &buf[p], i); |
| 494 | demux->tsbufp = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | goto bailout; |
| 496 | } |
| 497 | } else { |
| 498 | p++; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | bailout: |
| 503 | spin_unlock(&demux->lock); |
| 504 | } |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 505 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | EXPORT_SYMBOL(dvb_dmx_swfilter_204); |
| 507 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 508 | 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] | 509 | { |
| 510 | int i; |
| 511 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 512 | for (i = 0; i < demux->filternum; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | if (demux->filter[i].state == DMX_STATE_FREE) |
| 514 | break; |
| 515 | |
| 516 | if (i == demux->filternum) |
| 517 | return NULL; |
| 518 | |
| 519 | demux->filter[i].state = DMX_STATE_ALLOCATED; |
| 520 | |
| 521 | return &demux->filter[i]; |
| 522 | } |
| 523 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 524 | 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] | 525 | { |
| 526 | int i; |
| 527 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 528 | for (i = 0; i < demux->feednum; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | if (demux->feed[i].state == DMX_STATE_FREE) |
| 530 | break; |
| 531 | |
| 532 | if (i == demux->feednum) |
| 533 | return NULL; |
| 534 | |
| 535 | demux->feed[i].state = DMX_STATE_ALLOCATED; |
| 536 | |
| 537 | return &demux->feed[i]; |
| 538 | } |
| 539 | |
| 540 | static int dvb_demux_feed_find(struct dvb_demux_feed *feed) |
| 541 | { |
| 542 | struct dvb_demux_feed *entry; |
| 543 | |
| 544 | list_for_each_entry(entry, &feed->demux->feed_list, list_head) |
| 545 | if (entry == feed) |
| 546 | return 1; |
| 547 | |
| 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | static void dvb_demux_feed_add(struct dvb_demux_feed *feed) |
| 552 | { |
| 553 | spin_lock_irq(&feed->demux->lock); |
| 554 | if (dvb_demux_feed_find(feed)) { |
| 555 | printk(KERN_ERR "%s: feed already in list (type=%x state=%x pid=%x)\n", |
Harvey Harrison | 46b4f7c | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 556 | __func__, feed->type, feed->state, feed->pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | goto out; |
| 558 | } |
| 559 | |
| 560 | list_add(&feed->list_head, &feed->demux->feed_list); |
| 561 | out: |
| 562 | spin_unlock_irq(&feed->demux->lock); |
| 563 | } |
| 564 | |
| 565 | static void dvb_demux_feed_del(struct dvb_demux_feed *feed) |
| 566 | { |
| 567 | spin_lock_irq(&feed->demux->lock); |
| 568 | if (!(dvb_demux_feed_find(feed))) { |
| 569 | printk(KERN_ERR "%s: feed not in list (type=%x state=%x pid=%x)\n", |
Harvey Harrison | 46b4f7c | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 570 | __func__, feed->type, feed->state, feed->pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | goto out; |
| 572 | } |
| 573 | |
| 574 | list_del(&feed->list_head); |
| 575 | out: |
| 576 | spin_unlock_irq(&feed->demux->lock); |
| 577 | } |
| 578 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 579 | static int dmx_ts_feed_set(struct dmx_ts_feed *ts_feed, u16 pid, int ts_type, |
| 580 | enum dmx_ts_pes pes_type, |
| 581 | size_t circular_buffer_size, struct timespec timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 583 | struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | struct dvb_demux *demux = feed->demux; |
| 585 | |
| 586 | if (pid > DMX_MAX_PID) |
| 587 | return -EINVAL; |
| 588 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 589 | if (mutex_lock_interruptible(&demux->mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | return -ERESTARTSYS; |
| 591 | |
| 592 | if (ts_type & TS_DECODER) { |
| 593 | if (pes_type >= DMX_TS_PES_OTHER) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 594 | mutex_unlock(&demux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | return -EINVAL; |
| 596 | } |
| 597 | |
| 598 | if (demux->pesfilter[pes_type] && |
| 599 | demux->pesfilter[pes_type] != feed) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 600 | mutex_unlock(&demux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | return -EINVAL; |
| 602 | } |
| 603 | |
| 604 | demux->pesfilter[pes_type] = feed; |
| 605 | demux->pids[pes_type] = pid; |
| 606 | } |
| 607 | |
| 608 | dvb_demux_feed_add(feed); |
| 609 | |
| 610 | feed->pid = pid; |
| 611 | feed->buffer_size = circular_buffer_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | feed->timeout = timeout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | feed->ts_type = ts_type; |
| 614 | feed->pes_type = pes_type; |
| 615 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | if (feed->buffer_size) { |
| 617 | #ifdef NOBUFS |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 618 | feed->buffer = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | #else |
| 620 | feed->buffer = vmalloc(feed->buffer_size); |
| 621 | if (!feed->buffer) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 622 | mutex_unlock(&demux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | return -ENOMEM; |
| 624 | } |
| 625 | #endif |
| 626 | } |
| 627 | |
| 628 | feed->state = DMX_STATE_READY; |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 629 | mutex_unlock(&demux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | |
| 631 | return 0; |
| 632 | } |
| 633 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 634 | 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] | 635 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 636 | struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | struct dvb_demux *demux = feed->demux; |
| 638 | int ret; |
| 639 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 640 | if (mutex_lock_interruptible(&demux->mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | return -ERESTARTSYS; |
| 642 | |
| 643 | if (feed->state != DMX_STATE_READY || feed->type != DMX_TYPE_TS) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 644 | mutex_unlock(&demux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | return -EINVAL; |
| 646 | } |
| 647 | |
| 648 | if (!demux->start_feed) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 649 | mutex_unlock(&demux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | return -ENODEV; |
| 651 | } |
| 652 | |
| 653 | if ((ret = demux->start_feed(feed)) < 0) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 654 | mutex_unlock(&demux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | return ret; |
| 656 | } |
| 657 | |
| 658 | spin_lock_irq(&demux->lock); |
| 659 | ts_feed->is_filtering = 1; |
| 660 | feed->state = DMX_STATE_GO; |
| 661 | spin_unlock_irq(&demux->lock); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 662 | mutex_unlock(&demux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | |
| 664 | return 0; |
| 665 | } |
| 666 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 667 | 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] | 668 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 669 | struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | struct dvb_demux *demux = feed->demux; |
| 671 | int ret; |
| 672 | |
Simon Arlott | c278850 | 2007-03-10 06:21:25 -0300 | [diff] [blame] | 673 | mutex_lock(&demux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | |
| 675 | if (feed->state < DMX_STATE_GO) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 676 | mutex_unlock(&demux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | return -EINVAL; |
| 678 | } |
| 679 | |
| 680 | if (!demux->stop_feed) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 681 | mutex_unlock(&demux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | return -ENODEV; |
| 683 | } |
| 684 | |
| 685 | ret = demux->stop_feed(feed); |
| 686 | |
| 687 | spin_lock_irq(&demux->lock); |
| 688 | ts_feed->is_filtering = 0; |
| 689 | feed->state = DMX_STATE_ALLOCATED; |
| 690 | spin_unlock_irq(&demux->lock); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 691 | mutex_unlock(&demux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | |
| 693 | return ret; |
| 694 | } |
| 695 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 696 | static int dvbdmx_allocate_ts_feed(struct dmx_demux *dmx, |
| 697 | struct dmx_ts_feed **ts_feed, |
| 698 | dmx_ts_cb callback) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 700 | struct dvb_demux *demux = (struct dvb_demux *)dmx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | struct dvb_demux_feed *feed; |
| 702 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 703 | if (mutex_lock_interruptible(&demux->mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | return -ERESTARTSYS; |
| 705 | |
| 706 | if (!(feed = dvb_dmx_feed_alloc(demux))) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 707 | mutex_unlock(&demux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | return -EBUSY; |
| 709 | } |
| 710 | |
| 711 | feed->type = DMX_TYPE_TS; |
| 712 | feed->cb.ts = callback; |
| 713 | feed->demux = demux; |
| 714 | feed->pid = 0xffff; |
| 715 | feed->peslen = 0xfffa; |
| 716 | feed->buffer = NULL; |
| 717 | |
| 718 | (*ts_feed) = &feed->feed.ts; |
| 719 | (*ts_feed)->parent = dmx; |
| 720 | (*ts_feed)->priv = NULL; |
| 721 | (*ts_feed)->is_filtering = 0; |
| 722 | (*ts_feed)->start_filtering = dmx_ts_feed_start_filtering; |
| 723 | (*ts_feed)->stop_filtering = dmx_ts_feed_stop_filtering; |
| 724 | (*ts_feed)->set = dmx_ts_feed_set; |
| 725 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | if (!(feed->filter = dvb_dmx_filter_alloc(demux))) { |
| 727 | feed->state = DMX_STATE_FREE; |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 728 | mutex_unlock(&demux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | return -EBUSY; |
| 730 | } |
| 731 | |
| 732 | feed->filter->type = DMX_TYPE_TS; |
| 733 | feed->filter->feed = feed; |
| 734 | feed->filter->state = DMX_STATE_READY; |
| 735 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 736 | mutex_unlock(&demux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | |
| 738 | return 0; |
| 739 | } |
| 740 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 741 | static int dvbdmx_release_ts_feed(struct dmx_demux *dmx, |
| 742 | struct dmx_ts_feed *ts_feed) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 744 | struct dvb_demux *demux = (struct dvb_demux *)dmx; |
| 745 | struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | |
Simon Arlott | c278850 | 2007-03-10 06:21:25 -0300 | [diff] [blame] | 747 | mutex_lock(&demux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | |
| 749 | if (feed->state == DMX_STATE_FREE) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 750 | mutex_unlock(&demux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | return -EINVAL; |
| 752 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | #ifndef NOBUFS |
| 754 | vfree(feed->buffer); |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 755 | feed->buffer = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | #endif |
| 757 | |
| 758 | feed->state = DMX_STATE_FREE; |
| 759 | feed->filter->state = DMX_STATE_FREE; |
| 760 | |
| 761 | dvb_demux_feed_del(feed); |
| 762 | |
| 763 | feed->pid = 0xffff; |
| 764 | |
| 765 | if (feed->ts_type & TS_DECODER && feed->pes_type < DMX_TS_PES_OTHER) |
| 766 | demux->pesfilter[feed->pes_type] = NULL; |
| 767 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 768 | mutex_unlock(&demux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | return 0; |
| 770 | } |
| 771 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | /****************************************************************************** |
| 773 | * dmx_section_feed API calls |
| 774 | ******************************************************************************/ |
| 775 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 776 | static int dmx_section_feed_allocate_filter(struct dmx_section_feed *feed, |
| 777 | struct dmx_section_filter **filter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 779 | struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | struct dvb_demux *dvbdemux = dvbdmxfeed->demux; |
| 781 | struct dvb_demux_filter *dvbdmxfilter; |
| 782 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 783 | if (mutex_lock_interruptible(&dvbdemux->mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | return -ERESTARTSYS; |
| 785 | |
| 786 | dvbdmxfilter = dvb_dmx_filter_alloc(dvbdemux); |
| 787 | if (!dvbdmxfilter) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 788 | mutex_unlock(&dvbdemux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | return -EBUSY; |
| 790 | } |
| 791 | |
| 792 | spin_lock_irq(&dvbdemux->lock); |
| 793 | *filter = &dvbdmxfilter->filter; |
| 794 | (*filter)->parent = feed; |
| 795 | (*filter)->priv = NULL; |
| 796 | dvbdmxfilter->feed = dvbdmxfeed; |
| 797 | dvbdmxfilter->type = DMX_TYPE_SEC; |
| 798 | dvbdmxfilter->state = DMX_STATE_READY; |
| 799 | dvbdmxfilter->next = dvbdmxfeed->filter; |
| 800 | dvbdmxfeed->filter = dvbdmxfilter; |
| 801 | spin_unlock_irq(&dvbdemux->lock); |
| 802 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 803 | mutex_unlock(&dvbdemux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | return 0; |
| 805 | } |
| 806 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 807 | static int dmx_section_feed_set(struct dmx_section_feed *feed, |
| 808 | u16 pid, size_t circular_buffer_size, |
| 809 | int check_crc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 811 | struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | struct dvb_demux *dvbdmx = dvbdmxfeed->demux; |
| 813 | |
| 814 | if (pid > 0x1fff) |
| 815 | return -EINVAL; |
| 816 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 817 | if (mutex_lock_interruptible(&dvbdmx->mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | return -ERESTARTSYS; |
| 819 | |
| 820 | dvb_demux_feed_add(dvbdmxfeed); |
| 821 | |
| 822 | dvbdmxfeed->pid = pid; |
| 823 | dvbdmxfeed->buffer_size = circular_buffer_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | dvbdmxfeed->feed.sec.check_crc = check_crc; |
| 825 | |
| 826 | #ifdef NOBUFS |
| 827 | dvbdmxfeed->buffer = NULL; |
| 828 | #else |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 829 | dvbdmxfeed->buffer = vmalloc(dvbdmxfeed->buffer_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | if (!dvbdmxfeed->buffer) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 831 | mutex_unlock(&dvbdmx->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | return -ENOMEM; |
| 833 | } |
| 834 | #endif |
| 835 | |
| 836 | dvbdmxfeed->state = DMX_STATE_READY; |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 837 | mutex_unlock(&dvbdmx->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | return 0; |
| 839 | } |
| 840 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | static void prepare_secfilters(struct dvb_demux_feed *dvbdmxfeed) |
| 842 | { |
| 843 | int i; |
| 844 | struct dvb_demux_filter *f; |
| 845 | struct dmx_section_filter *sf; |
| 846 | u8 mask, mode, doneq; |
| 847 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 848 | if (!(f = dvbdmxfeed->filter)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | return; |
| 850 | do { |
| 851 | sf = &f->filter; |
| 852 | doneq = 0; |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 853 | for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | mode = sf->filter_mode[i]; |
| 855 | mask = sf->filter_mask[i]; |
| 856 | f->maskandmode[i] = mask & mode; |
| 857 | doneq |= f->maskandnotmode[i] = mask & ~mode; |
| 858 | } |
| 859 | f->doneq = doneq ? 1 : 0; |
| 860 | } while ((f = f->next)); |
| 861 | } |
| 862 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | static int dmx_section_feed_start_filtering(struct dmx_section_feed *feed) |
| 864 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 865 | struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | struct dvb_demux *dvbdmx = dvbdmxfeed->demux; |
| 867 | int ret; |
| 868 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 869 | if (mutex_lock_interruptible(&dvbdmx->mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | return -ERESTARTSYS; |
| 871 | |
| 872 | if (feed->is_filtering) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 873 | mutex_unlock(&dvbdmx->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | return -EBUSY; |
| 875 | } |
| 876 | |
| 877 | if (!dvbdmxfeed->filter) { |
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 -EINVAL; |
| 880 | } |
| 881 | |
| 882 | dvbdmxfeed->feed.sec.tsfeedp = 0; |
| 883 | dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base; |
| 884 | dvbdmxfeed->feed.sec.secbufp = 0; |
| 885 | dvbdmxfeed->feed.sec.seclen = 0; |
| 886 | |
| 887 | if (!dvbdmx->start_feed) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 888 | mutex_unlock(&dvbdmx->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | return -ENODEV; |
| 890 | } |
| 891 | |
| 892 | prepare_secfilters(dvbdmxfeed); |
| 893 | |
| 894 | if ((ret = dvbdmx->start_feed(dvbdmxfeed)) < 0) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 895 | mutex_unlock(&dvbdmx->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | return ret; |
| 897 | } |
| 898 | |
| 899 | spin_lock_irq(&dvbdmx->lock); |
| 900 | feed->is_filtering = 1; |
| 901 | dvbdmxfeed->state = DMX_STATE_GO; |
| 902 | spin_unlock_irq(&dvbdmx->lock); |
| 903 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 904 | mutex_unlock(&dvbdmx->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | return 0; |
| 906 | } |
| 907 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 908 | static int dmx_section_feed_stop_filtering(struct dmx_section_feed *feed) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 910 | struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | struct dvb_demux *dvbdmx = dvbdmxfeed->demux; |
| 912 | int ret; |
| 913 | |
Simon Arlott | c278850 | 2007-03-10 06:21:25 -0300 | [diff] [blame] | 914 | mutex_lock(&dvbdmx->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | |
| 916 | if (!dvbdmx->stop_feed) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 917 | mutex_unlock(&dvbdmx->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | return -ENODEV; |
| 919 | } |
| 920 | |
| 921 | ret = dvbdmx->stop_feed(dvbdmxfeed); |
| 922 | |
| 923 | spin_lock_irq(&dvbdmx->lock); |
| 924 | dvbdmxfeed->state = DMX_STATE_READY; |
| 925 | feed->is_filtering = 0; |
| 926 | spin_unlock_irq(&dvbdmx->lock); |
| 927 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 928 | mutex_unlock(&dvbdmx->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | return ret; |
| 930 | } |
| 931 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | static int dmx_section_feed_release_filter(struct dmx_section_feed *feed, |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 933 | struct dmx_section_filter *filter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 935 | struct dvb_demux_filter *dvbdmxfilter = (struct dvb_demux_filter *)filter, *f; |
| 936 | struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | struct dvb_demux *dvbdmx = dvbdmxfeed->demux; |
| 938 | |
Simon Arlott | c278850 | 2007-03-10 06:21:25 -0300 | [diff] [blame] | 939 | mutex_lock(&dvbdmx->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | |
| 941 | if (dvbdmxfilter->feed != dvbdmxfeed) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 942 | mutex_unlock(&dvbdmx->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | return -EINVAL; |
| 944 | } |
| 945 | |
| 946 | if (feed->is_filtering) |
| 947 | feed->stop_filtering(feed); |
| 948 | |
| 949 | spin_lock_irq(&dvbdmx->lock); |
| 950 | f = dvbdmxfeed->filter; |
| 951 | |
| 952 | if (f == dvbdmxfilter) { |
| 953 | dvbdmxfeed->filter = dvbdmxfilter->next; |
| 954 | } else { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 955 | while (f->next != dvbdmxfilter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | f = f->next; |
| 957 | f->next = f->next->next; |
| 958 | } |
| 959 | |
| 960 | dvbdmxfilter->state = DMX_STATE_FREE; |
| 961 | spin_unlock_irq(&dvbdmx->lock); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 962 | mutex_unlock(&dvbdmx->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | return 0; |
| 964 | } |
| 965 | |
| 966 | static int dvbdmx_allocate_section_feed(struct dmx_demux *demux, |
| 967 | struct dmx_section_feed **feed, |
| 968 | dmx_section_cb callback) |
| 969 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 970 | struct dvb_demux *dvbdmx = (struct dvb_demux *)demux; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | struct dvb_demux_feed *dvbdmxfeed; |
| 972 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 973 | if (mutex_lock_interruptible(&dvbdmx->mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | return -ERESTARTSYS; |
| 975 | |
| 976 | if (!(dvbdmxfeed = dvb_dmx_feed_alloc(dvbdmx))) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 977 | mutex_unlock(&dvbdmx->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | return -EBUSY; |
| 979 | } |
| 980 | |
| 981 | dvbdmxfeed->type = DMX_TYPE_SEC; |
| 982 | dvbdmxfeed->cb.sec = callback; |
| 983 | dvbdmxfeed->demux = dvbdmx; |
| 984 | dvbdmxfeed->pid = 0xffff; |
| 985 | dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base; |
| 986 | dvbdmxfeed->feed.sec.secbufp = dvbdmxfeed->feed.sec.seclen = 0; |
| 987 | dvbdmxfeed->feed.sec.tsfeedp = 0; |
| 988 | dvbdmxfeed->filter = NULL; |
| 989 | dvbdmxfeed->buffer = NULL; |
| 990 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 991 | (*feed) = &dvbdmxfeed->feed.sec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | (*feed)->is_filtering = 0; |
| 993 | (*feed)->parent = demux; |
| 994 | (*feed)->priv = NULL; |
| 995 | |
| 996 | (*feed)->set = dmx_section_feed_set; |
| 997 | (*feed)->allocate_filter = dmx_section_feed_allocate_filter; |
| 998 | (*feed)->start_filtering = dmx_section_feed_start_filtering; |
| 999 | (*feed)->stop_filtering = dmx_section_feed_stop_filtering; |
| 1000 | (*feed)->release_filter = dmx_section_feed_release_filter; |
| 1001 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1002 | mutex_unlock(&dvbdmx->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | return 0; |
| 1004 | } |
| 1005 | |
| 1006 | static int dvbdmx_release_section_feed(struct dmx_demux *demux, |
| 1007 | struct dmx_section_feed *feed) |
| 1008 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1009 | struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed; |
| 1010 | struct dvb_demux *dvbdmx = (struct dvb_demux *)demux; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | |
Simon Arlott | c278850 | 2007-03-10 06:21:25 -0300 | [diff] [blame] | 1012 | mutex_lock(&dvbdmx->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1014 | if (dvbdmxfeed->state == DMX_STATE_FREE) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1015 | mutex_unlock(&dvbdmx->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | return -EINVAL; |
| 1017 | } |
| 1018 | #ifndef NOBUFS |
| 1019 | vfree(dvbdmxfeed->buffer); |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1020 | dvbdmxfeed->buffer = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | #endif |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1022 | dvbdmxfeed->state = DMX_STATE_FREE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | |
| 1024 | dvb_demux_feed_del(dvbdmxfeed); |
| 1025 | |
| 1026 | dvbdmxfeed->pid = 0xffff; |
| 1027 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1028 | mutex_unlock(&dvbdmx->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | return 0; |
| 1030 | } |
| 1031 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | /****************************************************************************** |
| 1033 | * dvb_demux kernel data API calls |
| 1034 | ******************************************************************************/ |
| 1035 | |
| 1036 | static int dvbdmx_open(struct dmx_demux *demux) |
| 1037 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1038 | struct dvb_demux *dvbdemux = (struct dvb_demux *)demux; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | |
| 1040 | if (dvbdemux->users >= MAX_DVB_DEMUX_USERS) |
| 1041 | return -EUSERS; |
| 1042 | |
| 1043 | dvbdemux->users++; |
| 1044 | return 0; |
| 1045 | } |
| 1046 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | static int dvbdmx_close(struct dmx_demux *demux) |
| 1048 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1049 | struct dvb_demux *dvbdemux = (struct dvb_demux *)demux; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | |
| 1051 | if (dvbdemux->users == 0) |
| 1052 | return -ENODEV; |
| 1053 | |
| 1054 | dvbdemux->users--; |
| 1055 | //FIXME: release any unneeded resources if users==0 |
| 1056 | return 0; |
| 1057 | } |
| 1058 | |
Al Viro | 947a080 | 2008-06-22 14:20:19 -0300 | [diff] [blame^] | 1059 | static int dvbdmx_write(struct dmx_demux *demux, const char __user *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1061 | struct dvb_demux *dvbdemux = (struct dvb_demux *)demux; |
Al Viro | 947a080 | 2008-06-22 14:20:19 -0300 | [diff] [blame^] | 1062 | void *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | |
| 1064 | if ((!demux->frontend) || (demux->frontend->source != DMX_MEMORY_FE)) |
| 1065 | return -EINVAL; |
| 1066 | |
Al Viro | 947a080 | 2008-06-22 14:20:19 -0300 | [diff] [blame^] | 1067 | p = kmalloc(count, GFP_USER); |
| 1068 | if (!p) |
| 1069 | return -ENOMEM; |
| 1070 | if (copy_from_user(p, buf, count)) { |
| 1071 | kfree(p); |
| 1072 | return -EFAULT; |
| 1073 | } |
| 1074 | if (mutex_lock_interruptible(&dvbdemux->mutex)) { |
| 1075 | kfree(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | return -ERESTARTSYS; |
Al Viro | 947a080 | 2008-06-22 14:20:19 -0300 | [diff] [blame^] | 1077 | } |
| 1078 | dvb_dmx_swfilter(dvbdemux, p, count); |
| 1079 | kfree(p); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1080 | mutex_unlock(&dvbdemux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | |
| 1082 | if (signal_pending(current)) |
| 1083 | return -EINTR; |
| 1084 | return count; |
| 1085 | } |
| 1086 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1087 | static int dvbdmx_add_frontend(struct dmx_demux *demux, |
| 1088 | struct dmx_frontend *frontend) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1090 | struct dvb_demux *dvbdemux = (struct dvb_demux *)demux; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | struct list_head *head = &dvbdemux->frontend_list; |
| 1092 | |
| 1093 | list_add(&(frontend->connectivity_list), head); |
| 1094 | |
| 1095 | return 0; |
| 1096 | } |
| 1097 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1098 | static int dvbdmx_remove_frontend(struct dmx_demux *demux, |
| 1099 | struct dmx_frontend *frontend) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1101 | struct dvb_demux *dvbdemux = (struct dvb_demux *)demux; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | struct list_head *pos, *n, *head = &dvbdemux->frontend_list; |
| 1103 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1104 | list_for_each_safe(pos, n, head) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | if (DMX_FE_ENTRY(pos) == frontend) { |
| 1106 | list_del(pos); |
| 1107 | return 0; |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | return -ENODEV; |
| 1112 | } |
| 1113 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1114 | static struct list_head *dvbdmx_get_frontends(struct dmx_demux *demux) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1116 | struct dvb_demux *dvbdemux = (struct dvb_demux *)demux; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | |
| 1118 | if (list_empty(&dvbdemux->frontend_list)) |
| 1119 | return NULL; |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | return &dvbdemux->frontend_list; |
| 1122 | } |
| 1123 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1124 | static int dvbdmx_connect_frontend(struct dmx_demux *demux, |
| 1125 | struct dmx_frontend *frontend) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1127 | struct dvb_demux *dvbdemux = (struct dvb_demux *)demux; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | |
| 1129 | if (demux->frontend) |
| 1130 | return -EINVAL; |
| 1131 | |
Simon Arlott | c278850 | 2007-03-10 06:21:25 -0300 | [diff] [blame] | 1132 | mutex_lock(&dvbdemux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | |
| 1134 | demux->frontend = frontend; |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1135 | mutex_unlock(&dvbdemux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | return 0; |
| 1137 | } |
| 1138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | static int dvbdmx_disconnect_frontend(struct dmx_demux *demux) |
| 1140 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1141 | struct dvb_demux *dvbdemux = (struct dvb_demux *)demux; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | |
Simon Arlott | c278850 | 2007-03-10 06:21:25 -0300 | [diff] [blame] | 1143 | mutex_lock(&dvbdemux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | |
| 1145 | demux->frontend = NULL; |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1146 | mutex_unlock(&dvbdemux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | return 0; |
| 1148 | } |
| 1149 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1150 | static int dvbdmx_get_pes_pids(struct dmx_demux *demux, u16 * pids) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | { |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1152 | struct dvb_demux *dvbdemux = (struct dvb_demux *)demux; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1154 | memcpy(pids, dvbdemux->pids, 5 * sizeof(u16)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | return 0; |
| 1156 | } |
| 1157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | int dvb_dmx_init(struct dvb_demux *dvbdemux) |
| 1159 | { |
Andreas Oberritter | 9365346 | 2005-09-09 13:02:23 -0700 | [diff] [blame] | 1160 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | struct dmx_demux *dmx = &dvbdemux->dmx; |
| 1162 | |
| 1163 | dvbdemux->users = 0; |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1164 | dvbdemux->filter = vmalloc(dvbdemux->filternum * sizeof(struct dvb_demux_filter)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | |
| 1166 | if (!dvbdemux->filter) |
| 1167 | return -ENOMEM; |
| 1168 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1169 | dvbdemux->feed = vmalloc(dvbdemux->feednum * sizeof(struct dvb_demux_feed)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | if (!dvbdemux->feed) { |
| 1171 | vfree(dvbdemux->filter); |
| 1172 | return -ENOMEM; |
| 1173 | } |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1174 | for (i = 0; i < dvbdemux->filternum; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | dvbdemux->filter[i].state = DMX_STATE_FREE; |
| 1176 | dvbdemux->filter[i].index = i; |
| 1177 | } |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1178 | for (i = 0; i < dvbdemux->feednum; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | dvbdemux->feed[i].state = DMX_STATE_FREE; |
| 1180 | dvbdemux->feed[i].index = i; |
| 1181 | } |
Andreas Oberritter | db574d7 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1182 | |
| 1183 | INIT_LIST_HEAD(&dvbdemux->frontend_list); |
| 1184 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1185 | for (i = 0; i < DMX_TS_PES_OTHER; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | dvbdemux->pesfilter[i] = NULL; |
| 1187 | dvbdemux->pids[i] = 0xffff; |
| 1188 | } |
| 1189 | |
| 1190 | INIT_LIST_HEAD(&dvbdemux->feed_list); |
| 1191 | |
| 1192 | dvbdemux->playing = 0; |
| 1193 | dvbdemux->recording = 0; |
| 1194 | dvbdemux->tsbufp = 0; |
| 1195 | |
| 1196 | if (!dvbdemux->check_crc32) |
| 1197 | dvbdemux->check_crc32 = dvb_dmx_crc32; |
| 1198 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1199 | if (!dvbdemux->memcopy) |
| 1200 | dvbdemux->memcopy = dvb_dmx_memcopy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | |
| 1202 | dmx->frontend = NULL; |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1203 | dmx->priv = dvbdemux; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | dmx->open = dvbdmx_open; |
| 1205 | dmx->close = dvbdmx_close; |
| 1206 | dmx->write = dvbdmx_write; |
| 1207 | dmx->allocate_ts_feed = dvbdmx_allocate_ts_feed; |
| 1208 | dmx->release_ts_feed = dvbdmx_release_ts_feed; |
| 1209 | dmx->allocate_section_feed = dvbdmx_allocate_section_feed; |
| 1210 | dmx->release_section_feed = dvbdmx_release_section_feed; |
| 1211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | dmx->add_frontend = dvbdmx_add_frontend; |
| 1213 | dmx->remove_frontend = dvbdmx_remove_frontend; |
| 1214 | dmx->get_frontends = dvbdmx_get_frontends; |
| 1215 | dmx->connect_frontend = dvbdmx_connect_frontend; |
| 1216 | dmx->disconnect_frontend = dvbdmx_disconnect_frontend; |
| 1217 | dmx->get_pes_pids = dvbdmx_get_pes_pids; |
| 1218 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1219 | mutex_init(&dvbdemux->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | spin_lock_init(&dvbdemux->lock); |
| 1221 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | return 0; |
| 1223 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1225 | EXPORT_SYMBOL(dvb_dmx_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | |
Andreas Oberritter | 9365346 | 2005-09-09 13:02:23 -0700 | [diff] [blame] | 1227 | void dvb_dmx_release(struct dvb_demux *dvbdemux) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | vfree(dvbdemux->filter); |
| 1230 | vfree(dvbdemux->feed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | } |
Andreas Oberritter | dad4a73 | 2005-09-09 13:02:26 -0700 | [diff] [blame] | 1232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | EXPORT_SYMBOL(dvb_dmx_release); |