blob: bbf24703331940c422b9055483fce194b3df2d7f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Hamad Kadmanyd432f712013-01-17 17:36:17 +02008 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
Hamad Kadmany32cb9822012-05-10 08:47:44 +03009 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 *
24 */
25
Alexey Dobriyana99bbaf2009-10-04 16:11:37 +040026#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/spinlock.h>
28#include <linux/slab.h>
29#include <linux/vmalloc.h>
30#include <linux/module.h>
31#include <linux/poll.h>
32#include <linux/string.h>
33#include <linux/crc32.h>
34#include <asm/uaccess.h>
Abylay Ospan26b9d6c2009-11-01 18:46:53 -030035#include <asm/div64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#include "dvb_demux.h"
38
39#define NOBUFS
40/*
41** #define DVB_DEMUX_SECTION_LOSS_LOG to monitor payload loss in the syslog
42*/
43// #define DVB_DEMUX_SECTION_LOSS_LOG
44
Abylay Ospan0d834632009-06-06 09:31:34 -030045static int dvb_demux_tscheck;
46module_param(dvb_demux_tscheck, int, 0644);
47MODULE_PARM_DESC(dvb_demux_tscheck,
48 "enable transport stream continuity and TEI check");
49
Abylay Ospan26b9d6c2009-11-01 18:46:53 -030050static int dvb_demux_speedcheck;
51module_param(dvb_demux_speedcheck, int, 0644);
52MODULE_PARM_DESC(dvb_demux_speedcheck,
53 "enable transport stream speed check");
54
Hamad Kadmany32cb9822012-05-10 08:47:44 +030055/* counter advancing for each new dvb-demux device */
56static int dvb_demux_index;
57
58static int dvb_demux_performancecheck;
59module_param(dvb_demux_performancecheck, int, 0644);
60MODULE_PARM_DESC(dvb_demux_performancecheck,
61 "enable transport stream performance check, reported through debugfs");
62
Abylay Ospan0d834632009-06-06 09:31:34 -030063#define dprintk_tscheck(x...) do { \
64 if (dvb_demux_tscheck && printk_ratelimit()) \
65 printk(x); \
66 } while (0)
67
Hamad Kadmanye1dba202013-04-10 18:37:25 +030068static const struct dvb_dmx_video_patterns mpeg2_seq_hdr = {
69 {0x00, 0x00, 0x01, 0xB3},
70 {0xFF, 0xFF, 0xFF, 0xFF},
71 4,
72 DMX_IDX_MPEG_SEQ_HEADER
73};
74
75static const struct dvb_dmx_video_patterns mpeg2_gop = {
76 {0x00, 0x00, 0x01, 0xB8},
77 {0xFF, 0xFF, 0xFF, 0xFF},
78 4,
79 DMX_IDX_MPEG_GOP
80};
81
82static const struct dvb_dmx_video_patterns mpeg2_iframe = {
83 {0x00, 0x00, 0x01, 0x00, 0x00, 0x08},
84 {0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x38},
85 6,
86 DMX_IDX_MPEG_I_FRAME_START
87};
88
89static const struct dvb_dmx_video_patterns mpeg2_pframe = {
90 {0x00, 0x00, 0x01, 0x00, 0x00, 0x10},
91 {0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x38},
92 6,
93 DMX_IDX_MPEG_P_FRAME_START
94};
95
96static const struct dvb_dmx_video_patterns mpeg2_bframe = {
97 {0x00, 0x00, 0x01, 0x00, 0x00, 0x18},
98 {0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x38},
99 6,
100 DMX_IDX_MPEG_B_FRAME_START
101};
102
103static const struct dvb_dmx_video_patterns h264_sps = {
104 {0x00, 0x00, 0x01, 0x07},
105 {0xFF, 0xFF, 0xFF, 0x1F},
106 4,
107 DMX_IDX_H264_SPS
108};
109
110static const struct dvb_dmx_video_patterns h264_pps = {
111 {0x00, 0x00, 0x01, 0x08},
112 {0xFF, 0xFF, 0xFF, 0x1F},
113 4,
114 DMX_IDX_H264_PPS
115};
116
117static const struct dvb_dmx_video_patterns h264_idr = {
118 {0x00, 0x00, 0x01, 0x05, 0x80},
119 {0xFF, 0xFF, 0xFF, 0x1F, 0x80},
120 5,
121 DMX_IDX_H264_IDR_START
122};
123
124static const struct dvb_dmx_video_patterns h264_non_idr = {
125 {0x00, 0x00, 0x01, 0x01, 0x80},
126 {0xFF, 0xFF, 0xFF, 0x1F, 0x80},
127 5,
128 DMX_IDX_H264_NON_IDR_START
129};
130
131static const struct dvb_dmx_video_patterns vc1_seq_hdr = {
132 {0x00, 0x00, 0x01, 0x0F},
133 {0xFF, 0xFF, 0xFF, 0xFF},
134 4,
135 DMX_IDX_VC1_SEQ_HEADER
136};
137
138static const struct dvb_dmx_video_patterns vc1_entry_point = {
139 {0x00, 0x00, 0x01, 0x0E},
140 {0xFF, 0xFF, 0xFF, 0xFF},
141 4,
142 DMX_IDX_VC1_ENTRY_POINT
143};
144
145static const struct dvb_dmx_video_patterns vc1_frame = {
146 {0x00, 0x00, 0x01, 0x0D},
147 {0xFF, 0xFF, 0xFF, 0xFF},
148 4,
149 DMX_IDX_VC1_FRAME_START
150};
151
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/******************************************************************************
154 * static inlined helper functions
155 ******************************************************************************/
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static inline u16 section_length(const u8 *buf)
158{
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700159 return 3 + ((buf[1] & 0x0f) << 8) + buf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162static inline u16 ts_pid(const u8 *buf)
163{
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700164 return ((buf[1] & 0x1f) << 8) + buf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Hamad Kadmanyd0abf312013-05-13 18:56:06 +0300167static inline u16 ts_scrambling_ctrl(const u8 *buf)
168{
169 return (buf[3] >> 6) & 0x3;
170}
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172static inline u8 payload(const u8 *tsp)
173{
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700174 if (!(tsp[3] & 0x10)) // no payload?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return 0;
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700176
177 if (tsp[3] & 0x20) { // adaptation field?
178 if (tsp[4] > 183) // corrupted data?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return 0;
180 else
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700181 return 184 - 1 - tsp[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return 184;
185}
186
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700187static u32 dvb_dmx_crc32(struct dvb_demux_feed *f, const u8 *src, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700189 return (f->feed.sec.crc_val = crc32_be(f->feed.sec.crc_val, src, len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700192static void dvb_dmx_memcopy(struct dvb_demux_feed *f, u8 *d, const u8 *s,
193 size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700195 memcpy(d, s, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
Hamad Kadmany32cb9822012-05-10 08:47:44 +0300198static u32 dvb_dmx_calc_time_delta(struct timespec past_time)
199{
200 struct timespec curr_time, delta_time;
201 u64 delta_time_us;
202
203 curr_time = current_kernel_time();
204 delta_time = timespec_sub(curr_time, past_time);
205 delta_time_us = ((s64)delta_time.tv_sec * USEC_PER_SEC) +
206 delta_time.tv_nsec / 1000;
207
208 return (u32)delta_time_us;
209}
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/******************************************************************************
212 * Software filter functions
213 ******************************************************************************/
214
Hamad Kadmanyc37dee82013-04-10 18:14:48 +0300215/*
216 * Check if two patterns are identical, taking mask into consideration.
217 * @pattern1: the first byte pattern to compare.
218 * @pattern2: the second byte pattern to compare.
219 * @mask: the bit mask to use.
220 * @pattern_size: the length of both patterns and the mask, in bytes.
221 *
222 * Return: 1 if patterns match, 0 otherwise.
223 */
224static inline int dvb_dmx_patterns_match(const u8 *pattern1, const u8 *pattern2,
225 const u8 *mask, size_t pattern_size)
226{
227 int i;
228
229 /*
230 * Assumption: it is OK to access pattern1, pattern2 and mask.
231 * This function performs no sanity checks to keep things fast.
232 */
233
234 for (i = 0; i < pattern_size; i++)
235 if ((pattern1[i] & mask[i]) != (pattern2[i] & mask[i]))
236 return 0;
237
238 return 1;
239}
240
241/*
242 * dvb_dmx_video_pattern_search -
243 * search for framing patterns in a given buffer.
244 *
245 * Optimized version: first search for a common substring, e.g. 0x00 0x00 0x01.
246 * If this string is found, go over all the given patterns (all must start
247 * with this string) and search for their ending in the buffer.
248 *
249 * Assumption: the patterns we look for do not spread over more than two
250 * buffers.
251 *
252 * @paterns: the full patterns information to look for.
253 * @patterns_num: the number of patterns to look for.
254 * @buf: the buffer to search.
255 * @buf_size: the size of the buffer to search. we search the entire buffer.
256 * @prefix_size_masks: a bit mask (per pattern) of possible prefix sizes to use
257 * when searching for a pattern that started at the last buffer.
258 * Updated in this function for use in the next lookup.
259 * @results: lookup results (offset, type, used_prefix_size) per found pattern,
260 * up to DVB_DMX_MAX_FOUND_PATTERNS.
261 *
262 * Return:
263 * Number of patterns found (up to DVB_DMX_MAX_FOUND_PATTERNS).
264 * 0 if pattern was not found.
265 * error value on failure.
266 */
267int dvb_dmx_video_pattern_search(
Hamad Kadmanye1dba202013-04-10 18:37:25 +0300268 const struct dvb_dmx_video_patterns
269 *patterns[DVB_DMX_MAX_SEARCH_PATTERN_NUM],
Hamad Kadmanyc37dee82013-04-10 18:14:48 +0300270 int patterns_num,
271 const u8 *buf,
272 size_t buf_size,
273 struct dvb_dmx_video_prefix_size_masks *prefix_size_masks,
274 struct dvb_dmx_video_patterns_results *results)
275{
276 int i, j;
277 unsigned int current_size;
278 u32 prefix;
279 int found = 0;
280 int start_offset = 0;
281 /* the starting common substring to look for */
282 u8 string[] = {0x00, 0x00, 0x01};
283 /* the mask for the starting string */
284 u8 string_mask[] = {0xFF, 0xFF, 0xFF};
285 /* the size of the starting string (in bytes) */
286 size_t string_size = 3;
287
288 if ((patterns == NULL) || (patterns_num <= 0) || (buf == NULL))
289 return -EINVAL;
290
291 memset(results, 0, sizeof(struct dvb_dmx_video_patterns_results));
292
293 /*
294 * handle prefix - disregard string, simply check all patterns,
295 * looking for a matching suffix at the very beginning of the buffer.
296 */
297 for (j = 0; (j < patterns_num) && !found; j++) {
298 prefix = prefix_size_masks->size_mask[j];
299 current_size = 32;
300 while (prefix) {
301 if (prefix & (0x1 << (current_size - 1))) {
302 /*
303 * check that we don't look further
304 * than buf_size boundary
305 */
Hamad Kadmanye1dba202013-04-10 18:37:25 +0300306 if ((int)(patterns[j]->size - current_size) >
Hamad Kadmanyc37dee82013-04-10 18:14:48 +0300307 buf_size)
308 break;
309
310 if (dvb_dmx_patterns_match(
Hamad Kadmanye1dba202013-04-10 18:37:25 +0300311 (patterns[j]->pattern + current_size),
312 buf, (patterns[j]->mask + current_size),
313 (patterns[j]->size - current_size))) {
Hamad Kadmanyc37dee82013-04-10 18:14:48 +0300314
315 /*
316 * pattern found using prefix at the
317 * very beginning of the buffer, so
318 * offset is 0, but we already zeroed
319 * everything in the beginning of the
320 * function. that's why the next line
321 * is commented.
322 */
323 /* results->info[found].offset = 0; */
324 results->info[found].type =
Hamad Kadmanye1dba202013-04-10 18:37:25 +0300325 patterns[j]->type;
Hamad Kadmanyc37dee82013-04-10 18:14:48 +0300326 results->info[found].used_prefix_size =
327 current_size;
328 found++;
329 /*
330 * save offset to start looking from
331 * in the buffer, to avoid reusing the
332 * data of a pattern we already found.
333 */
Hamad Kadmanye1dba202013-04-10 18:37:25 +0300334 start_offset = (patterns[j]->size -
Hamad Kadmanyc37dee82013-04-10 18:14:48 +0300335 current_size);
336
337 if (found >= DVB_DMX_MAX_FOUND_PATTERNS)
338 goto next_prefix_lookup;
339 /*
340 * we don't want to search for the same
341 * pattern with several possible prefix
342 * sizes if we have already found it,
343 * so we break from the inner loop.
344 * since we incremented 'found', we
345 * will not search for additional
346 * patterns using a prefix - that would
347 * imply ambiguous patterns where one
348 * pattern can be included in another.
349 * the for loop will exit.
350 */
351 break;
352 }
353 }
354 prefix &= ~(0x1 << (current_size - 1));
355 current_size--;
356 }
357 }
358
359 /*
360 * Search buffer for entire pattern, starting with the string.
361 * Note the external for loop does not execute if buf_size is
362 * smaller than string_size (the cast to int is required, since
363 * size_t is unsigned).
364 */
365 for (i = start_offset; i < (int)(buf_size - string_size + 1); i++) {
366 if (dvb_dmx_patterns_match(string, (buf + i), string_mask,
367 string_size)) {
368 /* now search for patterns: */
369 for (j = 0; j < patterns_num; j++) {
370 /* avoid overflow to next buffer */
Hamad Kadmanye1dba202013-04-10 18:37:25 +0300371 if ((i + patterns[j]->size) > buf_size)
Hamad Kadmanyc37dee82013-04-10 18:14:48 +0300372 continue;
373
374 if (dvb_dmx_patterns_match(
Hamad Kadmanye1dba202013-04-10 18:37:25 +0300375 (patterns[j]->pattern + string_size),
Hamad Kadmanyc37dee82013-04-10 18:14:48 +0300376 (buf + i + string_size),
Hamad Kadmanye1dba202013-04-10 18:37:25 +0300377 (patterns[j]->mask + string_size),
378 (patterns[j]->size - string_size))) {
Hamad Kadmanyc37dee82013-04-10 18:14:48 +0300379
380 results->info[found].offset = i;
381 results->info[found].type =
Hamad Kadmanye1dba202013-04-10 18:37:25 +0300382 patterns[j]->type;
Hamad Kadmanyc37dee82013-04-10 18:14:48 +0300383 /*
384 * save offset to start next prefix
385 * lookup, to avoid reusing the data
386 * of any pattern we already found.
387 */
Hamad Kadmanye1dba202013-04-10 18:37:25 +0300388 if ((i + patterns[j]->size) >
Hamad Kadmanyc37dee82013-04-10 18:14:48 +0300389 start_offset)
390 start_offset = (i +
Hamad Kadmanye1dba202013-04-10 18:37:25 +0300391 patterns[j]->size);
Hamad Kadmanyc37dee82013-04-10 18:14:48 +0300392 /*
393 * did not use a prefix to find this
394 * pattern, but we zeroed everything
395 * in the beginning of the function.
396 * So no need to zero used_prefix_size
397 * for results->info[found]
398 */
399
400 found++;
401 if (found >= DVB_DMX_MAX_FOUND_PATTERNS)
402 goto next_prefix_lookup;
403 /*
404 * theoretically we don't have to break
405 * here, but we don't want to search
406 * for the other matching patterns on
407 * the very same same place in the
408 * buffer. That would mean the
409 * (pattern & mask) combinations are
410 * not unique. So we break from inner
411 * loop and move on to the next place
412 * in the buffer.
413 */
414 break;
415 }
416 }
417 }
418 }
419
420next_prefix_lookup:
421 /* check for possible prefix sizes for the next buffer */
422 for (j = 0; j < patterns_num; j++) {
423 prefix_size_masks->size_mask[j] = 0;
Hamad Kadmanye1dba202013-04-10 18:37:25 +0300424 for (i = 1; i < patterns[j]->size; i++) {
Hamad Kadmanyc37dee82013-04-10 18:14:48 +0300425 /*
426 * avoid looking outside of the buffer
427 * or reusing previously used data.
428 */
429 if (i > (buf_size - start_offset))
430 break;
431
Hamad Kadmanye1dba202013-04-10 18:37:25 +0300432 if (dvb_dmx_patterns_match(patterns[j]->pattern,
Hamad Kadmanyc37dee82013-04-10 18:14:48 +0300433 (buf + buf_size - i),
Hamad Kadmanye1dba202013-04-10 18:37:25 +0300434 patterns[j]->mask, i)) {
Hamad Kadmanyc37dee82013-04-10 18:14:48 +0300435 prefix_size_masks->size_mask[j] |=
436 (1 << (i - 1));
437 }
438 }
439 }
440
441 return found;
442}
443EXPORT_SYMBOL(dvb_dmx_video_pattern_search);
444
Gilad Broneracb4b872013-05-30 18:55:59 +0300445/**
446 * dvb_dmx_notify_section_event() - Notify demux event for all filters of a
447 * specified section feed.
448 *
449 * @feed: dvb_demux_feed object
450 * @event: demux event to notify
451 * @should_lock: specifies whether the function should lock the demux
452 *
453 * Caller is responsible for locking the demux properly, either by doing the
454 * locking itself and setting 'should_lock' to 0, or have the function do it
455 * by setting 'should_lock' to 1.
456 */
457int dvb_dmx_notify_section_event(struct dvb_demux_feed *feed,
458 struct dmx_data_ready *event, int should_lock)
459{
460 struct dvb_demux_filter *f;
461
462 if (feed == NULL || event == NULL || feed->type != DMX_TYPE_SEC)
463 return -EINVAL;
464
465 if (!should_lock && !spin_is_locked(&feed->demux->lock))
466 return -EINVAL;
467
468 if (should_lock)
469 spin_lock(&feed->demux->lock);
470
471 f = feed->filter;
472 while (f && feed->feed.sec.is_filtering) {
473 feed->data_ready_cb.sec(&f->filter, event);
474 f = f->next;
475 }
476
477 if (should_lock)
478 spin_unlock(&feed->demux->lock);
479
480 return 0;
481}
482EXPORT_SYMBOL(dvb_dmx_notify_section_event);
483
Gilad Broner180f65f2013-05-09 18:55:42 +0300484static int dvb_dmx_check_pes_end(struct dvb_demux_feed *feed)
485{
486 struct dmx_data_ready data;
487
488 if (!feed->pusi_seen)
489 return 0;
490
491 data.status = DMX_OK_PES_END;
492 data.data_length = 0;
493 data.pes_end.start_gap = 0;
494 data.pes_end.actual_length = feed->peslen;
495 data.pes_end.disc_indicator_set = 0;
496 data.pes_end.pes_length_mismatch = 0;
497 data.pes_end.stc = 0;
498 data.pes_end.tei_counter = feed->pes_tei_counter;
499 data.pes_end.cont_err_counter = feed->pes_cont_err_counter;
500 data.pes_end.ts_packets_num = feed->pes_ts_packets_num;
501
502 return feed->data_ready_cb.ts(&feed->feed.ts, &data);
503}
504
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700505static inline int dvb_dmx_swfilter_payload(struct dvb_demux_feed *feed,
506 const u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
508 int count = payload(buf);
509 int p;
Hamad Kadmanyd432f712013-01-17 17:36:17 +0200510 int ccok;
511 u8 cc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 if (count == 0)
514 return -1;
515
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700516 p = 188 - count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700518 cc = buf[3] & 0x0f;
Hamad Kadmanyeb75d1c2012-07-29 13:17:28 +0300519 if (feed->first_cc)
520 ccok = 1;
521 else
522 ccok = ((feed->cc + 1) & 0x0f) == cc;
523
524 feed->first_cc = 0;
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700525 feed->cc = cc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Hamad Kadmany32cb9822012-05-10 08:47:44 +0300527 /* PUSI ? */
528 if (buf[1] & 0x40) {
Gilad Broner180f65f2013-05-09 18:55:42 +0300529 dvb_dmx_check_pes_end(feed);
Hamad Kadmany32cb9822012-05-10 08:47:44 +0300530 feed->pusi_seen = 1;
Hamad Kadmany4f877942012-07-15 15:06:01 +0300531 feed->peslen = 0;
Hamad Kadmanyd432f712013-01-17 17:36:17 +0200532 feed->pes_tei_counter = 0;
533 feed->pes_ts_packets_num = 0;
534 feed->pes_cont_err_counter = 0;
Hamad Kadmany32cb9822012-05-10 08:47:44 +0300535 }
536
537 if (feed->pusi_seen == 0)
538 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Hamad Kadmanyd432f712013-01-17 17:36:17 +0200540 feed->pes_ts_packets_num++;
541 feed->pes_cont_err_counter += !ccok;
542 feed->pes_tei_counter += (buf[1] & 0x80) ? 1 : 0;
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 feed->peslen += count;
545
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700546 return feed->cb.ts(&buf[p], count, NULL, 0, &feed->feed.ts, DMX_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700549static int dvb_dmx_swfilter_sectionfilter(struct dvb_demux_feed *feed,
550 struct dvb_demux_filter *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 u8 neq = 0;
553 int i;
554
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700555 for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 u8 xor = f->filter.filter_value[i] ^ feed->feed.sec.secbuf[i];
557
558 if (f->maskandmode[i] & xor)
559 return 0;
560
561 neq |= f->maskandnotmode[i] & xor;
562 }
563
564 if (f->doneq && !neq)
565 return 0;
566
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700567 return feed->cb.sec(feed->feed.sec.secbuf, feed->feed.sec.seclen,
568 NULL, 0, &f->filter, DMX_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700571static inline int dvb_dmx_swfilter_section_feed(struct dvb_demux_feed *feed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
573 struct dvb_demux *demux = feed->demux;
574 struct dvb_demux_filter *f = feed->filter;
575 struct dmx_section_feed *sec = &feed->feed.sec;
576 int section_syntax_indicator;
577
578 if (!sec->is_filtering)
579 return 0;
580
581 if (!f)
582 return 0;
583
584 if (sec->check_crc) {
Hamad Kadmany32cb9822012-05-10 08:47:44 +0300585 struct timespec pre_crc_time;
586
587 if (dvb_demux_performancecheck)
588 pre_crc_time = current_kernel_time();
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 section_syntax_indicator = ((sec->secbuf[1] & 0x80) != 0);
591 if (section_syntax_indicator &&
Hamad Kadmany32cb9822012-05-10 08:47:44 +0300592 demux->check_crc32(feed, sec->secbuf, sec->seclen)) {
593 if (dvb_demux_performancecheck)
594 demux->total_crc_time +=
595 dvb_dmx_calc_time_delta(pre_crc_time);
Hamad Kadmany4f877942012-07-15 15:06:01 +0300596
597 /* Notify on CRC error */
598 feed->cb.sec(NULL, 0, NULL, 0,
599 &f->filter, DMX_CRC_ERROR);
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return -1;
Hamad Kadmany32cb9822012-05-10 08:47:44 +0300602 }
603
604 if (dvb_demux_performancecheck)
605 demux->total_crc_time +=
606 dvb_dmx_calc_time_delta(pre_crc_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
608
609 do {
610 if (dvb_dmx_swfilter_sectionfilter(feed, f) < 0)
611 return -1;
612 } while ((f = f->next) && sec->is_filtering);
613
614 sec->seclen = 0;
615
616 return 0;
617}
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619static void dvb_dmx_swfilter_section_new(struct dvb_demux_feed *feed)
620{
621 struct dmx_section_feed *sec = &feed->feed.sec;
622
623#ifdef DVB_DEMUX_SECTION_LOSS_LOG
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700624 if (sec->secbufp < sec->tsfeedp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 int i, n = sec->tsfeedp - sec->secbufp;
626
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700627 /*
628 * Section padding is done with 0xff bytes entirely.
629 * Due to speed reasons, we won't check all of them
630 * but just first and last.
631 */
632 if (sec->secbuf[0] != 0xff || sec->secbuf[n - 1] != 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 printk("dvb_demux.c section ts padding loss: %d/%d\n",
634 n, sec->tsfeedp);
635 printk("dvb_demux.c pad data:");
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700636 for (i = 0; i < n; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 printk(" %02x", sec->secbuf[i]);
638 printk("\n");
639 }
640 }
641#endif
642
643 sec->tsfeedp = sec->secbufp = sec->seclen = 0;
644 sec->secbuf = sec->secbuf_base;
645}
646
647/*
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700648 * Losless Section Demux 1.4.1 by Emard
649 * Valsecchi Patrick:
650 * - middle of section A (no PUSI)
651 * - end of section A and start of section B
652 * (with PUSI pointing to the start of the second section)
653 *
654 * In this case, without feed->pusi_seen you'll receive a garbage section
655 * consisting of the end of section A. Basically because tsfeedp
656 * is incemented and the use=0 condition is not raised
657 * when the second packet arrives.
658 *
659 * Fix:
660 * when demux is started, let feed->pusi_seen = 0 to
661 * prevent initial feeding of garbage from the end of
662 * previous section. When you for the first time see PUSI=1
663 * then set feed->pusi_seen = 1
664 */
665static int dvb_dmx_swfilter_section_copy_dump(struct dvb_demux_feed *feed,
666 const u8 *buf, u8 len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 struct dvb_demux *demux = feed->demux;
669 struct dmx_section_feed *sec = &feed->feed.sec;
670 u16 limit, seclen, n;
671
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700672 if (sec->tsfeedp >= DMX_MAX_SECFEED_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return 0;
674
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700675 if (sec->tsfeedp + len > DMX_MAX_SECFEED_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676#ifdef DVB_DEMUX_SECTION_LOSS_LOG
677 printk("dvb_demux.c section buffer full loss: %d/%d\n",
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700678 sec->tsfeedp + len - DMX_MAX_SECFEED_SIZE,
679 DMX_MAX_SECFEED_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680#endif
681 len = DMX_MAX_SECFEED_SIZE - sec->tsfeedp;
682 }
683
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700684 if (len <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return 0;
686
687 demux->memcopy(feed, sec->secbuf_base + sec->tsfeedp, buf, len);
688 sec->tsfeedp += len;
689
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700690 /*
691 * Dump all the sections we can find in the data (Emard)
692 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 limit = sec->tsfeedp;
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700694 if (limit > DMX_MAX_SECFEED_SIZE)
695 return -1; /* internal error should never happen */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 /* to be sure always set secbuf */
698 sec->secbuf = sec->secbuf_base + sec->secbufp;
699
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700700 for (n = 0; sec->secbufp + 2 < limit; n++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 seclen = section_length(sec->secbuf);
Mark Adamsb3967d62005-11-08 21:35:50 -0800702 if (seclen <= 0 || seclen > DMX_MAX_SECTION_SIZE
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700703 || seclen + sec->secbufp > limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return 0;
705 sec->seclen = seclen;
706 sec->crc_val = ~0;
707 /* dump [secbuf .. secbuf+seclen) */
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700708 if (feed->pusi_seen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 dvb_dmx_swfilter_section_feed(feed);
710#ifdef DVB_DEMUX_SECTION_LOSS_LOG
711 else
712 printk("dvb_demux.c pusi not seen, discarding section data\n");
713#endif
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700714 sec->secbufp += seclen; /* secbufp and secbuf moving together is */
715 sec->secbuf += seclen; /* redundant but saves pointer arithmetic */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
717
718 return 0;
719}
720
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700721static int dvb_dmx_swfilter_section_packet(struct dvb_demux_feed *feed,
722 const u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
724 u8 p, count;
725 int ccok, dc_i = 0;
726 u8 cc;
727
728 count = payload(buf);
729
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700730 if (count == 0) /* count == 0 if no payload or out of range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return -1;
732
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700733 p = 188 - count; /* payload start */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 cc = buf[3] & 0x0f;
Hamad Kadmanyeb75d1c2012-07-29 13:17:28 +0300736 if (feed->first_cc)
737 ccok = 1;
738 else
739 ccok = ((feed->cc + 1) & 0x0f) == cc;
740
Hamad Kadmany1101b002013-04-11 11:55:16 +0300741 /* discard TS packets holding sections with TEI bit set */
742 if (buf[1] & 0x80)
743 return -EINVAL;
744
Hamad Kadmanyeb75d1c2012-07-29 13:17:28 +0300745 feed->first_cc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 feed->cc = cc;
747
748 if (buf[3] & 0x20) {
749 /* adaption field present, check for discontinuity_indicator */
750 if ((buf[4] > 0) && (buf[5] & 0x80))
751 dc_i = 1;
752 }
753
754 if (!ccok || dc_i) {
755#ifdef DVB_DEMUX_SECTION_LOSS_LOG
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700756 printk("dvb_demux.c discontinuity detected %d bytes lost\n",
757 count);
758 /*
759 * those bytes under sume circumstances will again be reported
760 * in the following dvb_dmx_swfilter_section_new
761 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762#endif
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700763 /*
764 * Discontinuity detected. Reset pusi_seen = 0 to
765 * stop feeding of suspicious data until next PUSI=1 arrives
766 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 feed->pusi_seen = 0;
768 dvb_dmx_swfilter_section_new(feed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
770
771 if (buf[1] & 0x40) {
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700772 /* PUSI=1 (is set), section boundary is here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (count > 1 && buf[p] < count) {
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700774 const u8 *before = &buf[p + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 u8 before_len = buf[p];
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700776 const u8 *after = &before[before_len];
777 u8 after_len = count - 1 - before_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700779 dvb_dmx_swfilter_section_copy_dump(feed, before,
780 before_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 /* before start of new section, set pusi_seen = 1 */
782 feed->pusi_seen = 1;
783 dvb_dmx_swfilter_section_new(feed);
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700784 dvb_dmx_swfilter_section_copy_dump(feed, after,
785 after_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
787#ifdef DVB_DEMUX_SECTION_LOSS_LOG
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700788 else if (count > 0)
789 printk("dvb_demux.c PUSI=1 but %d bytes lost\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790#endif
791 } else {
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700792 /* PUSI=0 (is not set), no section boundary */
793 dvb_dmx_swfilter_section_copy_dump(feed, &buf[p], count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return 0;
797}
798
Hamad Kadmanye1dba202013-04-10 18:37:25 +0300799static int dvb_demux_save_idx_event(struct dvb_demux_feed *feed,
800 struct dmx_index_event_info *idx_event,
801 int traverse_from_tail)
802{
803 struct dmx_index_entry *idx_entry;
804 struct dmx_index_entry *curr_entry;
805 struct list_head *pos;
806
807 /* get entry from free list */
808 if (list_empty(&feed->rec_info->idx_info.free_list)) {
809 printk(KERN_ERR "%s: index free list is empty\n", __func__);
810 return -ENOMEM;
811 }
812
813 idx_entry = list_first_entry(&feed->rec_info->idx_info.free_list,
814 struct dmx_index_entry, next);
815 list_del(&idx_entry->next);
816
817 idx_entry->event = *idx_event;
818
819 pos = &feed->rec_info->idx_info.ready_list;
820 if (traverse_from_tail) {
821 list_for_each_entry_reverse(curr_entry,
822 &feed->rec_info->idx_info.ready_list, next) {
823 if (curr_entry->event.match_tsp_num <=
824 idx_event->match_tsp_num) {
825 pos = &curr_entry->next;
826 break;
827 }
828 }
829 } else {
830 list_for_each_entry(curr_entry,
831 &feed->rec_info->idx_info.ready_list, next) {
832 if (curr_entry->event.match_tsp_num >
833 idx_event->match_tsp_num) {
834 pos = &curr_entry->next;
835 break;
836 }
837 }
838 }
839
840 if (traverse_from_tail)
841 list_add(&idx_entry->next, pos);
842 else
843 list_add_tail(&idx_entry->next, pos);
844
845 return 0;
846}
847
848int dvb_demux_push_idx_event(struct dvb_demux_feed *feed,
849 struct dmx_index_event_info *idx_event)
850{
851 int ret;
852
853 spin_lock(&feed->demux->lock);
854 ret = dvb_demux_save_idx_event(feed, idx_event, 1);
855 spin_unlock(&feed->demux->lock);
856
857 return ret;
858}
859EXPORT_SYMBOL(dvb_demux_push_idx_event);
860
861static inline void dvb_dmx_notify_indexing(struct dvb_demux_feed *feed)
862{
863 struct dmx_data_ready dmx_data_ready;
864 struct dmx_index_entry *curr_entry;
865 struct list_head *n, *pos;
866
867 dmx_data_ready.status = DMX_OK_IDX;
868
869 list_for_each_safe(pos, n, &feed->rec_info->idx_info.ready_list) {
870 curr_entry = list_entry(pos, struct dmx_index_entry, next);
871
872 if ((feed->rec_info->idx_info.min_pattern_tsp_num == (u64)-1) ||
873 (curr_entry->event.match_tsp_num <=
874 feed->rec_info->idx_info.min_pattern_tsp_num)) {
875 dmx_data_ready.idx_event = curr_entry->event;
876 feed->data_ready_cb.ts(&feed->feed.ts, &dmx_data_ready);
877 list_del(&curr_entry->next);
878 list_add_tail(&curr_entry->next,
879 &feed->rec_info->idx_info.free_list);
880 }
881 }
882}
883
884void dvb_dmx_notify_idx_events(struct dvb_demux_feed *feed)
885{
886 spin_lock(&feed->demux->lock);
887 dvb_dmx_notify_indexing(feed);
888 spin_unlock(&feed->demux->lock);
889}
890EXPORT_SYMBOL(dvb_dmx_notify_idx_events);
891
892static void dvb_dmx_process_pattern_result(struct dvb_demux_feed *feed,
893 struct dvb_dmx_video_patterns_results *patterns, int pattern,
894 u64 curr_stc, u64 prev_stc,
895 u64 curr_match_tsp, u64 prev_match_tsp,
896 u64 curr_pusi_tsp, u64 prev_pusi_tsp)
897{
898 int mpeg_frame_start;
899 int h264_frame_start;
900 int vc1_frame_start;
901 int seq_start;
902 u64 frame_end_in_seq;
903 struct dmx_index_event_info idx_event;
904
905 idx_event.pid = feed->pid;
906 if (patterns->info[pattern].used_prefix_size) {
907 idx_event.match_tsp_num = prev_match_tsp;
908 idx_event.last_pusi_tsp_num = prev_pusi_tsp;
909 idx_event.stc = prev_stc;
910 } else {
911 idx_event.match_tsp_num = curr_match_tsp;
912 idx_event.last_pusi_tsp_num = curr_pusi_tsp;
913 idx_event.stc = curr_stc;
914 }
915
916 /* notify on frame-end if needed */
917 if (feed->prev_frame_valid) {
918 if (feed->prev_frame_type & DMX_IDX_MPEG_I_FRAME_START) {
919 idx_event.type = DMX_IDX_MPEG_I_FRAME_END;
920 frame_end_in_seq = DMX_IDX_MPEG_FIRST_SEQ_FRAME_END;
921 } else if (feed->prev_frame_type & DMX_IDX_MPEG_P_FRAME_START) {
922 idx_event.type = DMX_IDX_MPEG_P_FRAME_END;
923 frame_end_in_seq = DMX_IDX_MPEG_FIRST_SEQ_FRAME_END;
924 } else if (feed->prev_frame_type & DMX_IDX_MPEG_B_FRAME_START) {
925 idx_event.type = DMX_IDX_MPEG_B_FRAME_END;
926 frame_end_in_seq = DMX_IDX_MPEG_FIRST_SEQ_FRAME_END;
927 } else if (feed->prev_frame_type & DMX_IDX_H264_IDR_START) {
928 idx_event.type = DMX_IDX_H264_IDR_END;
929 frame_end_in_seq = DMX_IDX_H264_FIRST_SPS_FRAME_END;
930 } else if (feed->prev_frame_type & DMX_IDX_H264_NON_IDR_START) {
931 idx_event.type = DMX_IDX_H264_NON_IDR_END;
932 frame_end_in_seq = DMX_IDX_H264_FIRST_SPS_FRAME_END;
933 } else {
934 idx_event.type = DMX_IDX_VC1_FRAME_END;
935 frame_end_in_seq = DMX_IDX_VC1_FIRST_SEQ_FRAME_END;
936 }
937
938 if (feed->idx_params.types & idx_event.type)
939 dvb_demux_save_idx_event(feed, &idx_event, 1);
940
941 if (feed->first_frame_in_seq_notified &&
942 feed->idx_params.types & frame_end_in_seq) {
943 idx_event.type = frame_end_in_seq;
944 dvb_demux_save_idx_event(feed, &idx_event, 1);
945 feed->first_frame_in_seq_notified = 0;
946 }
947 }
948
949 seq_start = patterns->info[pattern].type &
950 (DMX_IDX_MPEG_SEQ_HEADER | DMX_IDX_H264_SPS |
951 DMX_IDX_VC1_SEQ_HEADER);
952
953 /* did we find start of sequence/SPS? */
954 if (seq_start) {
955 feed->first_frame_in_seq = 1;
956 feed->first_frame_in_seq_notified = 0;
957 feed->prev_frame_valid = 0;
958 idx_event.type = patterns->info[pattern].type;
959 if (feed->idx_params.types & idx_event.type)
960 dvb_demux_save_idx_event(feed, &idx_event, 1);
961 return;
962 }
963
964 mpeg_frame_start = patterns->info[pattern].type &
965 (DMX_IDX_MPEG_I_FRAME_START |
966 DMX_IDX_MPEG_P_FRAME_START |
967 DMX_IDX_MPEG_B_FRAME_START);
968
969 h264_frame_start = patterns->info[pattern].type &
970 (DMX_IDX_H264_IDR_START | DMX_IDX_H264_NON_IDR_START);
971
972 vc1_frame_start = patterns->info[pattern].type &
973 DMX_IDX_VC1_FRAME_START;
974
975 if (!mpeg_frame_start && !h264_frame_start && !vc1_frame_start) {
976 /* neither sequence nor frame, notify on the entry if needed */
977 idx_event.type = patterns->info[pattern].type;
978 if (feed->idx_params.types & idx_event.type)
979 dvb_demux_save_idx_event(feed, &idx_event, 1);
980 feed->prev_frame_valid = 0;
981 return;
982 }
983
984 /* notify on first frame in sequence/sps if needed */
985 if (feed->first_frame_in_seq) {
986 feed->first_frame_in_seq = 0;
987 feed->first_frame_in_seq_notified = 1;
988 if (mpeg_frame_start)
989 idx_event.type = DMX_IDX_MPEG_FIRST_SEQ_FRAME_START;
990 else if (h264_frame_start)
991 idx_event.type = DMX_IDX_H264_FIRST_SPS_FRAME_START;
992 else
993 idx_event.type = DMX_IDX_VC1_FIRST_SEQ_FRAME_START;
994
995 if (feed->idx_params.types & idx_event.type)
996 dvb_demux_save_idx_event(feed, &idx_event, 1);
997 }
998
999 /* notify on frame start if needed */
1000 idx_event.type = patterns->info[pattern].type;
1001 if (feed->idx_params.types & idx_event.type)
1002 dvb_demux_save_idx_event(feed, &idx_event, 1);
1003
1004 feed->prev_frame_valid = 1;
1005 feed->prev_frame_type = patterns->info[pattern].type;
1006}
1007
1008void dvb_dmx_process_idx_pattern(struct dvb_demux_feed *feed,
1009 struct dvb_dmx_video_patterns_results *patterns, int pattern,
1010 u64 curr_stc, u64 prev_stc,
1011 u64 curr_match_tsp, u64 prev_match_tsp,
1012 u64 curr_pusi_tsp, u64 prev_pusi_tsp)
1013{
1014 spin_lock(&feed->demux->lock);
1015 dvb_dmx_process_pattern_result(feed,
1016 patterns, pattern,
1017 curr_stc, prev_stc,
1018 curr_match_tsp, prev_match_tsp,
1019 curr_pusi_tsp, prev_pusi_tsp);
1020 spin_unlock(&feed->demux->lock);
1021}
1022EXPORT_SYMBOL(dvb_dmx_process_idx_pattern);
1023
1024static void dvb_dmx_index(struct dvb_demux_feed *feed,
1025 const u8 *buf,
1026 const u8 timestamp[TIMESTAMP_LEN])
1027{
1028 int i;
1029 int p;
1030 u64 stc;
1031 int found_patterns;
1032 int count = payload(buf);
1033 u64 min_pattern_tsp_num;
1034 struct dvb_demux_feed *tmp_feed;
1035 struct dvb_demux *demux = feed->demux;
1036 struct dmx_index_event_info idx_event;
1037 struct dvb_dmx_video_patterns_results patterns;
1038
1039 if (feed->demux->convert_ts)
1040 feed->demux->convert_ts(feed, timestamp, &stc);
1041 else
1042 stc = 0;
1043
1044 idx_event.pid = feed->pid;
1045 idx_event.stc = stc;
1046 idx_event.match_tsp_num = feed->rec_info->ts_output_count;
1047
1048 /* PUSI ? */
1049 if (buf[1] & 0x40) {
1050 feed->curr_pusi_tsp_num = feed->rec_info->ts_output_count;
1051 if (feed->idx_params.types & DMX_IDX_PUSI) {
1052 idx_event.type = DMX_IDX_PUSI;
1053 idx_event.last_pusi_tsp_num =
1054 feed->curr_pusi_tsp_num;
1055 dvb_demux_save_idx_event(feed, &idx_event, 1);
1056 }
1057 }
1058
1059 /*
1060 * if we still did not encounter a TS packet with PUSI indication,
1061 * we cannot report index entries yet as we need to provide
1062 * the TS packet number with PUSI indication preceeding the TS
1063 * packet pointed by the reported index entry.
1064 */
1065 if (feed->curr_pusi_tsp_num == (u64)-1) {
1066 dvb_dmx_notify_indexing(feed);
1067 return;
1068 }
1069
1070 if ((feed->idx_params.types & DMX_IDX_RAI) && /* index RAI? */
1071 (buf[3] & 0x20) && /* adaptation field exists? */
1072 (buf[4] > 0) && /* adaptation field len > 0 ? */
1073 (buf[5] & 0x40)) { /* RAI is set? */
1074 idx_event.type = DMX_IDX_RAI;
1075 idx_event.last_pusi_tsp_num =
1076 feed->curr_pusi_tsp_num;
1077 dvb_demux_save_idx_event(feed, &idx_event, 1);
1078 }
1079
1080 /*
1081 * if no pattern search is required, or the TS packet has no payload,
1082 * pattern search is not executed.
1083 */
1084 if (!feed->pattern_num || !count) {
1085 dvb_dmx_notify_indexing(feed);
1086 return;
1087 }
1088
1089 p = 188 - count; /* payload start */
1090
1091 found_patterns =
1092 dvb_dmx_video_pattern_search(feed->patterns,
1093 feed->pattern_num, &buf[p], count,
1094 &feed->prefix_size, &patterns);
1095
1096 for (i = 0; i < found_patterns; i++)
1097 dvb_dmx_process_pattern_result(feed, &patterns, i,
1098 stc, feed->prev_stc,
1099 feed->rec_info->ts_output_count, feed->prev_tsp_num,
1100 feed->curr_pusi_tsp_num, feed->prev_pusi_tsp_num);
1101
1102 feed->prev_tsp_num = feed->rec_info->ts_output_count;
1103 feed->prev_pusi_tsp_num = feed->curr_pusi_tsp_num;
1104 feed->prev_stc = stc;
1105 feed->last_pattern_tsp_num = feed->rec_info->ts_output_count;
1106
1107 /*
1108 * it is possible to have a TS packet that has a prefix of
1109 * a video pattern but the video pattern is not identified yet
1110 * until we get the next TS packet of that PID. When we get
1111 * the next TS packet of that PID, pattern-search would
1112 * detect that we have a new index entry that starts in the
1113 * previous TS packet.
1114 * In order to notify the user on index entries with match_tsp_num
1115 * in ascending order, index events with match_tsp_num up to
1116 * the last_pattern_tsp_num are notified now to the user,
1117 * the rest can't be notified now as we might hit the above
1118 * scenario and cause the events not to be notified with
1119 * ascending order of match_tsp_num.
1120 */
1121 if (feed->rec_info->idx_info.pattern_search_feeds_num == 1) {
1122 /*
1123 * optimization for case we have only one PID
1124 * with video pattern search, in this case
1125 * min_pattern_tsp_num is simply updated to the new
1126 * TS packet number of the PID with pattern search.
1127 */
1128 feed->rec_info->idx_info.min_pattern_tsp_num =
1129 feed->last_pattern_tsp_num;
1130 dvb_dmx_notify_indexing(feed);
1131 return;
1132 }
1133
1134 /*
1135 * if we have more than one PID with pattern search,
1136 * min_pattern_tsp_num needs to be updated now based on
1137 * last_pattern_tsp_num of all PIDs with pattern search.
1138 */
1139 min_pattern_tsp_num = (u64)-1;
1140 i = feed->rec_info->idx_info.pattern_search_feeds_num;
1141 list_for_each_entry(tmp_feed, &demux->feed_list, list_head) {
1142 if ((tmp_feed->state != DMX_STATE_GO) ||
1143 (tmp_feed->type != DMX_TYPE_TS) ||
1144 (tmp_feed->feed.ts.buffer.ringbuff !=
1145 feed->feed.ts.buffer.ringbuff))
1146 continue;
1147
1148 if ((tmp_feed->last_pattern_tsp_num != (u64)-1) &&
1149 ((min_pattern_tsp_num == (u64)-1) ||
1150 (tmp_feed->last_pattern_tsp_num <
1151 min_pattern_tsp_num)))
1152 min_pattern_tsp_num = tmp_feed->last_pattern_tsp_num;
1153
1154 if (tmp_feed->pattern_num) {
1155 i--;
1156 if (i == 0)
1157 break;
1158 }
1159 }
1160
1161 feed->rec_info->idx_info.min_pattern_tsp_num = min_pattern_tsp_num;
1162
1163 /* notify all index entries up to min_pattern_tsp_num */
1164 dvb_dmx_notify_indexing(feed);
1165}
1166
Hamad Kadmany32cb9822012-05-10 08:47:44 +03001167static inline void dvb_dmx_swfilter_output_packet(
1168 struct dvb_demux_feed *feed,
Hamad Kadmany9e5ce472012-11-22 13:47:07 +02001169 const u8 *buf,
1170 const u8 timestamp[TIMESTAMP_LEN])
Hamad Kadmany32cb9822012-05-10 08:47:44 +03001171{
Hamad Kadmany32cb9822012-05-10 08:47:44 +03001172 /*
1173 * if we output 192 packet with timestamp at head of packet,
1174 * output the timestamp now before the 188 TS packet
1175 */
Hamad Kadmany9e5ce472012-11-22 13:47:07 +02001176 if (feed->tsp_out_format == DMX_TSP_FORMAT_192_HEAD)
1177 feed->cb.ts(timestamp, TIMESTAMP_LEN, NULL,
1178 0, &feed->feed.ts, DMX_OK);
Hamad Kadmany32cb9822012-05-10 08:47:44 +03001179
1180 feed->cb.ts(buf, 188, NULL, 0, &feed->feed.ts, DMX_OK);
1181
1182 /*
1183 * if we output 192 packet with timestamp at tail of packet,
1184 * output the timestamp now after the 188 TS packet
1185 */
Hamad Kadmany9e5ce472012-11-22 13:47:07 +02001186 if (feed->tsp_out_format == DMX_TSP_FORMAT_192_TAIL)
1187 feed->cb.ts(timestamp, TIMESTAMP_LEN, NULL,
1188 0, &feed->feed.ts, DMX_OK);
Hamad Kadmanye1dba202013-04-10 18:37:25 +03001189
1190 if (feed->idx_params.enable)
1191 dvb_dmx_index(feed, buf, timestamp);
1192
1193 feed->rec_info->ts_output_count++;
Hamad Kadmany32cb9822012-05-10 08:47:44 +03001194}
1195
1196static inline void dvb_dmx_configure_decoder_fullness(
1197 struct dvb_demux *demux,
1198 int initialize)
1199{
1200 struct dvb_demux_feed *feed;
1201 int j;
1202
1203 for (j = 0; j < demux->feednum; j++) {
1204 feed = &demux->feed[j];
1205
1206 if ((feed->state != DMX_STATE_GO) ||
1207 (feed->type != DMX_TYPE_TS) ||
1208 !(feed->ts_type & TS_DECODER))
1209 continue;
1210
1211 if (initialize) {
1212 if (demux->decoder_fullness_init)
1213 demux->decoder_fullness_init(feed);
1214 } else {
1215 if (demux->decoder_fullness_abort)
1216 demux->decoder_fullness_abort(feed);
1217 }
1218 }
1219}
1220
1221static inline int dvb_dmx_swfilter_buffer_check(
1222 struct dvb_demux *demux,
1223 u16 pid)
1224{
1225 int desired_space;
1226 int ret;
1227 struct dmx_ts_feed *ts;
1228 struct dvb_demux_filter *f;
1229 struct dvb_demux_feed *feed;
1230 int was_locked;
1231 int i, j;
1232
1233 if (likely(spin_is_locked(&demux->lock)))
1234 was_locked = 1;
1235 else
1236 was_locked = 0;
1237
1238 /*
1239 * Check that there's enough free space for data output.
1240 * If there no space, wait for it (block).
1241 * Since this function is called while spinlock
1242 * is aquired, the lock should be released first.
1243 * Once we get control back, lock is aquired back
1244 * and checks that the filter is still valid.
1245 */
1246 for (j = 0; j < demux->feednum; j++) {
1247 feed = &demux->feed[j];
1248
1249 if (demux->sw_filter_abort)
1250 return -ENODEV;
1251
1252 if ((feed->state != DMX_STATE_GO) ||
1253 ((feed->pid != pid) && (feed->pid != 0x2000)))
1254 continue;
1255
Gilad Broner4e663752013-03-03 13:41:10 +02001256 if (feed->secure_mode.is_secured &&
1257 !dvb_dmx_is_rec_feed(feed))
1258 return 0;
1259
Hamad Kadmany32cb9822012-05-10 08:47:44 +03001260 if (feed->type == DMX_TYPE_TS) {
1261 desired_space = 192; /* upper bound */
1262 ts = &feed->feed.ts;
1263
1264 if (feed->ts_type & TS_PACKET) {
1265 if (likely(was_locked))
1266 spin_unlock(&demux->lock);
1267
1268 ret = demux->buffer_ctrl.ts(ts, desired_space);
1269
1270 if (likely(was_locked))
1271 spin_lock(&demux->lock);
1272
1273 if (ret < 0)
1274 continue;
1275 }
1276
1277 if (demux->sw_filter_abort)
1278 return -ENODEV;
1279
1280 if (!ts->is_filtering)
1281 continue;
1282
1283 if ((feed->ts_type & TS_DECODER) &&
1284 (demux->decoder_fullness_wait)) {
1285 if (likely(was_locked))
1286 spin_unlock(&demux->lock);
1287
1288 ret = demux->decoder_fullness_wait(
1289 feed,
1290 desired_space);
1291
1292 if (likely(was_locked))
1293 spin_lock(&demux->lock);
1294
1295 if (ret < 0)
1296 continue;
1297 }
1298
1299 continue;
1300 }
1301
1302 /* else - section case */
1303 desired_space = feed->feed.sec.tsfeedp + 188; /* upper bound */
1304 for (i = 0; i < demux->filternum; i++) {
1305 if (demux->sw_filter_abort)
1306 return -EPERM;
1307
1308 if (!feed->feed.sec.is_filtering)
1309 continue;
1310
1311 f = &demux->filter[i];
1312 if (f->feed != feed)
1313 continue;
1314
1315 if (likely(was_locked))
1316 spin_unlock(&demux->lock);
1317
1318 ret = demux->buffer_ctrl.sec(&f->filter, desired_space);
1319
1320 if (likely(was_locked))
1321 spin_lock(&demux->lock);
1322
1323 if (ret < 0)
1324 break;
1325 }
1326 }
1327
1328 return 0;
1329}
1330
Andreas Oberritterdad4a732005-09-09 13:02:26 -07001331static inline void dvb_dmx_swfilter_packet_type(struct dvb_demux_feed *feed,
Hamad Kadmany9e5ce472012-11-22 13:47:07 +02001332 const u8 *buf, const u8 timestamp[TIMESTAMP_LEN])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
Hamad Kadmanyd0abf312013-05-13 18:56:06 +03001334 u16 pid = ts_pid(buf);
1335 u8 scrambling_bits = ts_scrambling_ctrl(buf);
1336 struct dmx_data_ready dmx_data_ready;
1337
1338 /*
1339 * Notify on scrambling status change only when we move
1340 * from clear (0) to non-clear and vise-versa
1341 */
1342 if ((scrambling_bits && !feed->scrambling_bits) ||
1343 (!scrambling_bits && feed->scrambling_bits)) {
1344 dmx_data_ready.status = DMX_OK_SCRAMBLING_STATUS;
1345 dmx_data_ready.data_length = 0;
1346 dmx_data_ready.scrambling_bits.pid = pid;
1347 dmx_data_ready.scrambling_bits.old_value =
1348 feed->scrambling_bits;
1349 dmx_data_ready.scrambling_bits.new_value = scrambling_bits;
1350
1351 if (feed->type == DMX_TYPE_SEC)
Gilad Broneracb4b872013-05-30 18:55:59 +03001352 dvb_dmx_notify_section_event(feed, &dmx_data_ready, 0);
Hamad Kadmanyd0abf312013-05-13 18:56:06 +03001353 else
1354 feed->data_ready_cb.ts(&feed->feed.ts, &dmx_data_ready);
1355 }
1356
1357 feed->scrambling_bits = scrambling_bits;
1358
Andreas Oberritterdad4a732005-09-09 13:02:26 -07001359 switch (feed->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 case DMX_TYPE_TS:
1361 if (!feed->feed.ts.is_filtering)
1362 break;
1363 if (feed->ts_type & TS_PACKET) {
Gilad Broner4e663752013-03-03 13:41:10 +02001364 if (feed->ts_type & TS_PAYLOAD_ONLY) {
1365 if (!feed->secure_mode.is_secured)
1366 dvb_dmx_swfilter_payload(feed, buf);
1367 } else {
Hamad Kadmany9e5ce472012-11-22 13:47:07 +02001368 dvb_dmx_swfilter_output_packet(feed,
1369 buf, timestamp);
Gilad Broner4e663752013-03-03 13:41:10 +02001370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
Gilad Broner4e663752013-03-03 13:41:10 +02001372 if ((feed->ts_type & TS_DECODER) &&
1373 !feed->secure_mode.is_secured)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 if (feed->demux->write_to_decoder)
1375 feed->demux->write_to_decoder(feed, buf, 188);
1376 break;
1377
1378 case DMX_TYPE_SEC:
Gilad Broner4e663752013-03-03 13:41:10 +02001379 if (!feed->feed.sec.is_filtering ||
1380 feed->secure_mode.is_secured)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 break;
1382 if (dvb_dmx_swfilter_section_packet(feed, buf) < 0)
Andreas Oberritterdad4a732005-09-09 13:02:26 -07001383 feed->feed.sec.seclen = feed->feed.sec.secbufp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 break;
1385
1386 default:
1387 break;
1388 }
1389}
1390
1391#define DVR_FEED(f) \
1392 (((f)->type == DMX_TYPE_TS) && \
1393 ((f)->feed.ts.is_filtering) && \
Andreas Oberritter4a24ce32008-04-22 14:45:47 -03001394 (((f)->ts_type & (TS_PACKET | TS_DEMUX)) == TS_PACKET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Hamad Kadmanye1dba202013-04-10 18:37:25 +03001396static void dvb_dmx_swfilter_one_packet(struct dvb_demux *demux, const u8 *buf,
Hamad Kadmany9e5ce472012-11-22 13:47:07 +02001397 const u8 timestamp[TIMESTAMP_LEN])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
1399 struct dvb_demux_feed *feed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 u16 pid = ts_pid(buf);
1401 int dvr_done = 0;
1402
Abylay Ospan26b9d6c2009-11-01 18:46:53 -03001403 if (dvb_demux_speedcheck) {
1404 struct timespec cur_time, delta_time;
1405 u64 speed_bytes, speed_timedelta;
1406
1407 demux->speed_pkts_cnt++;
1408
1409 /* show speed every SPEED_PKTS_INTERVAL packets */
1410 if (!(demux->speed_pkts_cnt % SPEED_PKTS_INTERVAL)) {
1411 cur_time = current_kernel_time();
1412
1413 if (demux->speed_last_time.tv_sec != 0 &&
1414 demux->speed_last_time.tv_nsec != 0) {
1415 delta_time = timespec_sub(cur_time,
1416 demux->speed_last_time);
1417 speed_bytes = (u64)demux->speed_pkts_cnt
1418 * 188 * 8;
1419 /* convert to 1024 basis */
1420 speed_bytes = 1000 * div64_u64(speed_bytes,
1421 1024);
1422 speed_timedelta =
1423 (u64)timespec_to_ns(&delta_time);
1424 speed_timedelta = div64_u64(speed_timedelta,
1425 1000000); /* nsec -> usec */
1426 printk(KERN_INFO "TS speed %llu Kbits/sec \n",
1427 div64_u64(speed_bytes,
1428 speed_timedelta));
1429 };
1430
1431 demux->speed_last_time = cur_time;
1432 demux->speed_pkts_cnt = 0;
1433 };
1434 };
1435
Abylay Ospanab6a21f2010-03-06 14:58:01 -03001436 if (demux->cnt_storage && dvb_demux_tscheck) {
Abylay Ospan0d834632009-06-06 09:31:34 -03001437 /* check pkt counter */
1438 if (pid < MAX_PID) {
1439 if (buf[1] & 0x80)
1440 dprintk_tscheck("TEI detected. "
1441 "PID=0x%x data1=0x%x\n",
1442 pid, buf[1]);
1443
1444 if ((buf[3] & 0xf) != demux->cnt_storage[pid])
1445 dprintk_tscheck("TS packet counter mismatch. "
1446 "PID=0x%x expected 0x%x "
1447 "got 0x%x\n",
1448 pid, demux->cnt_storage[pid],
1449 buf[3] & 0xf);
1450
1451 demux->cnt_storage[pid] = ((buf[3] & 0xf) + 1)&0xf;
1452 };
1453 /* end check */
1454 };
Abylay Ospan0d834632009-06-06 09:31:34 -03001455
Hamad Kadmany32cb9822012-05-10 08:47:44 +03001456 if (demux->playback_mode == DMX_PB_MODE_PULL)
1457 if (dvb_dmx_swfilter_buffer_check(demux, pid) < 0)
1458 return;
1459
Trent Piepho79482612007-10-10 05:37:39 -03001460 list_for_each_entry(feed, &demux->feed_list, list_head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 if ((feed->pid != pid) && (feed->pid != 0x2000))
1462 continue;
1463
1464 /* copy each packet only once to the dvr device, even
1465 * if a PID is in multiple filters (e.g. video + PCR) */
1466 if ((DVR_FEED(feed)) && (dvr_done++))
1467 continue;
1468
Andreas Oberrittera4afd652009-07-14 20:48:37 -03001469 if (feed->pid == pid)
Hamad Kadmany9e5ce472012-11-22 13:47:07 +02001470 dvb_dmx_swfilter_packet_type(feed, buf, timestamp);
Hamad Kadmany32cb9822012-05-10 08:47:44 +03001471 else if ((feed->pid == 0x2000) &&
1472 (feed->feed.ts.is_filtering))
Hamad Kadmany9e5ce472012-11-22 13:47:07 +02001473 dvb_dmx_swfilter_output_packet(feed, buf, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 }
1475}
Hamad Kadmanye1dba202013-04-10 18:37:25 +03001476
1477void dvb_dmx_swfilter_packet(struct dvb_demux *demux, const u8 *buf,
1478 const u8 timestamp[TIMESTAMP_LEN])
1479{
1480 spin_lock(&demux->lock);
1481 dvb_dmx_swfilter_one_packet(demux, buf, timestamp);
1482 spin_unlock(&demux->lock);
1483}
Hamad Kadmany81cee052012-11-29 14:15:57 +02001484EXPORT_SYMBOL(dvb_dmx_swfilter_packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Hamad Kadmany94bc0ab2012-08-02 10:07:21 +03001486void dvb_dmx_swfilter_section_packets(struct dvb_demux *demux, const u8 *buf,
1487 size_t count)
1488{
1489 struct dvb_demux_feed *feed;
1490 u16 pid = ts_pid(buf);
1491 struct timespec pre_time;
1492
1493 if (dvb_demux_performancecheck)
1494 pre_time = current_kernel_time();
1495
1496 spin_lock(&demux->lock);
1497
1498 demux->sw_filter_abort = 0;
1499
1500 while (count--) {
1501 if (buf[0] != 0x47) {
1502 buf += 188;
1503 continue;
1504 }
1505
1506 if (demux->playback_mode == DMX_PB_MODE_PULL)
1507 if (dvb_dmx_swfilter_buffer_check(demux, pid) < 0)
1508 break;
1509
1510 list_for_each_entry(feed, &demux->feed_list, list_head) {
1511 if (feed->pid != pid)
1512 continue;
1513
1514 if (!feed->feed.sec.is_filtering)
1515 continue;
1516
1517 if (dvb_dmx_swfilter_section_packet(feed, buf) < 0) {
1518 feed->feed.sec.seclen = 0;
1519 feed->feed.sec.secbufp = 0;
1520 }
1521 }
1522 buf += 188;
1523 }
1524
1525 spin_unlock(&demux->lock);
1526
1527 if (dvb_demux_performancecheck)
1528 demux->total_process_time += dvb_dmx_calc_time_delta(pre_time);
1529}
1530EXPORT_SYMBOL(dvb_dmx_swfilter_section_packets);
1531
Andreas Oberritterdad4a732005-09-09 13:02:26 -07001532void dvb_dmx_swfilter_packets(struct dvb_demux *demux, const u8 *buf,
1533 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
Hamad Kadmany32cb9822012-05-10 08:47:44 +03001535 struct timespec pre_time;
Hamad Kadmany9e5ce472012-11-22 13:47:07 +02001536 u8 timestamp[TIMESTAMP_LEN] = {0};
Hamad Kadmany32cb9822012-05-10 08:47:44 +03001537
1538 if (dvb_demux_performancecheck)
1539 pre_time = current_kernel_time();
1540
Mauro Carvalho Chehab28100162009-02-16 15:27:44 -03001541 spin_lock(&demux->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Hamad Kadmany32cb9822012-05-10 08:47:44 +03001543 demux->sw_filter_abort = 0;
1544 dvb_dmx_configure_decoder_fullness(demux, 1);
1545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 while (count--) {
Andreas Oberritterdad4a732005-09-09 13:02:26 -07001547 if (buf[0] == 0x47)
Hamad Kadmanye1dba202013-04-10 18:37:25 +03001548 dvb_dmx_swfilter_one_packet(demux, buf, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 buf += 188;
1550 }
1551
Mauro Carvalho Chehab28100162009-02-16 15:27:44 -03001552 spin_unlock(&demux->lock);
Hamad Kadmany32cb9822012-05-10 08:47:44 +03001553
1554 if (dvb_demux_performancecheck)
1555 demux->total_process_time += dvb_dmx_calc_time_delta(pre_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Andreas Oberritterdad4a732005-09-09 13:02:26 -07001558EXPORT_SYMBOL(dvb_dmx_swfilter_packets);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Marko Ristola38e009a2011-04-08 12:40:51 -03001560static inline int find_next_packet(const u8 *buf, int pos, size_t count,
Hamad Kadmany32cb9822012-05-10 08:47:44 +03001561 const int pktsize, const int leadingbytes)
Marko Ristola38e009a2011-04-08 12:40:51 -03001562{
1563 int start = pos, lost;
1564
1565 while (pos < count) {
Hamad Kadmany32cb9822012-05-10 08:47:44 +03001566 if ((buf[pos] == 0x47 && !leadingbytes) ||
1567 (pktsize == 204 && buf[pos] == 0xB8) ||
1568 (pktsize == 192 && leadingbytes &&
1569 (pos+leadingbytes < count) &&
1570 buf[pos+leadingbytes] == 0x47))
Marko Ristola38e009a2011-04-08 12:40:51 -03001571 break;
1572 pos++;
1573 }
1574
1575 lost = pos - start;
1576 if (lost) {
1577 /* This garbage is part of a valid packet? */
1578 int backtrack = pos - pktsize;
1579 if (backtrack >= 0 && (buf[backtrack] == 0x47 ||
Hamad Kadmany32cb9822012-05-10 08:47:44 +03001580 (pktsize == 204 && buf[backtrack] == 0xB8) ||
1581 (pktsize == 192 &&
1582 buf[backtrack+leadingbytes] == 0x47)))
Marko Ristola38e009a2011-04-08 12:40:51 -03001583 return backtrack;
1584 }
1585
1586 return pos;
1587}
1588
1589/* Filter all pktsize= 188 or 204 sized packets and skip garbage. */
1590static inline void _dvb_dmx_swfilter(struct dvb_demux *demux, const u8 *buf,
Hamad Kadmany32cb9822012-05-10 08:47:44 +03001591 size_t count, const int pktsize, const int leadingbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
1593 int p = 0, i, j;
Marko Ristola38e009a2011-04-08 12:40:51 -03001594 const u8 *q;
Hamad Kadmany32cb9822012-05-10 08:47:44 +03001595 struct timespec pre_time;
Hamad Kadmany9e5ce472012-11-22 13:47:07 +02001596 u8 timestamp[TIMESTAMP_LEN];
Hamad Kadmany32cb9822012-05-10 08:47:44 +03001597
1598 if (dvb_demux_performancecheck)
1599 pre_time = current_kernel_time();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Mauro Carvalho Chehab28100162009-02-16 15:27:44 -03001601 spin_lock(&demux->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Hamad Kadmany32cb9822012-05-10 08:47:44 +03001603 demux->sw_filter_abort = 0;
1604 dvb_dmx_configure_decoder_fullness(demux, 1);
1605
Marko Ristola38e009a2011-04-08 12:40:51 -03001606 if (demux->tsbufp) { /* tsbuf[0] is now 0x47. */
Andreas Oberritterdad4a732005-09-09 13:02:26 -07001607 i = demux->tsbufp;
Marko Ristola38e009a2011-04-08 12:40:51 -03001608 j = pktsize - i;
Andreas Oberritterdad4a732005-09-09 13:02:26 -07001609 if (count < j) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 memcpy(&demux->tsbuf[i], buf, count);
1611 demux->tsbufp += count;
1612 goto bailout;
1613 }
1614 memcpy(&demux->tsbuf[i], buf, j);
Hamad Kadmany9e5ce472012-11-22 13:47:07 +02001615
1616 if (pktsize == 192) {
1617 if (leadingbytes)
Hamad Kadmany1c287d72013-05-19 10:09:15 +03001618 memcpy(timestamp, &demux->tsbuf[p],
1619 TIMESTAMP_LEN);
Hamad Kadmany9e5ce472012-11-22 13:47:07 +02001620 else
Hamad Kadmany1c287d72013-05-19 10:09:15 +03001621 memcpy(timestamp, &demux->tsbuf[188],
1622 TIMESTAMP_LEN);
Hamad Kadmany9e5ce472012-11-22 13:47:07 +02001623 } else {
1624 memset(timestamp, 0, TIMESTAMP_LEN);
1625 }
1626
Hamad Kadmany32cb9822012-05-10 08:47:44 +03001627 if (pktsize == 192 &&
1628 leadingbytes &&
1629 demux->tsbuf[leadingbytes] == 0x47) /* double check */
Hamad Kadmanye1dba202013-04-10 18:37:25 +03001630 dvb_dmx_swfilter_one_packet(demux,
Hamad Kadmany9e5ce472012-11-22 13:47:07 +02001631 demux->tsbuf + TIMESTAMP_LEN, timestamp);
Hamad Kadmany32cb9822012-05-10 08:47:44 +03001632 else if (demux->tsbuf[0] == 0x47) /* double check */
Hamad Kadmanye1dba202013-04-10 18:37:25 +03001633 dvb_dmx_swfilter_one_packet(demux,
1634 demux->tsbuf, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 demux->tsbufp = 0;
1636 p += j;
1637 }
1638
Marko Ristola38e009a2011-04-08 12:40:51 -03001639 while (1) {
Hamad Kadmany32cb9822012-05-10 08:47:44 +03001640 p = find_next_packet(buf, p, count, pktsize, leadingbytes);
1641
1642 if (demux->sw_filter_abort)
1643 goto bailout;
1644
Marko Ristola38e009a2011-04-08 12:40:51 -03001645 if (p >= count)
1646 break;
1647 if (count - p < pktsize)
1648 break;
1649
1650 q = &buf[p];
1651
1652 if (pktsize == 204 && (*q == 0xB8)) {
1653 memcpy(demux->tsbuf, q, 188);
1654 demux->tsbuf[0] = 0x47;
1655 q = demux->tsbuf;
1656 }
Hamad Kadmany32cb9822012-05-10 08:47:44 +03001657
Hamad Kadmany9e5ce472012-11-22 13:47:07 +02001658 if (pktsize == 192) {
1659 if (leadingbytes) {
1660 q = &buf[p+leadingbytes];
1661 memcpy(timestamp, &buf[p], TIMESTAMP_LEN);
1662 } else {
Hamad Kadmanye1dba202013-04-10 18:37:25 +03001663 memcpy(timestamp, &buf[p+188], TIMESTAMP_LEN);
Hamad Kadmany9e5ce472012-11-22 13:47:07 +02001664 }
1665 } else {
1666 memset(timestamp, 0, TIMESTAMP_LEN);
1667 }
Hamad Kadmany32cb9822012-05-10 08:47:44 +03001668
Hamad Kadmanye1dba202013-04-10 18:37:25 +03001669 dvb_dmx_swfilter_one_packet(demux, q, timestamp);
Marko Ristola38e009a2011-04-08 12:40:51 -03001670 p += pktsize;
1671 }
1672
1673 i = count - p;
1674 if (i) {
1675 memcpy(demux->tsbuf, &buf[p], i);
1676 demux->tsbufp = i;
1677 if (pktsize == 204 && demux->tsbuf[0] == 0xB8)
1678 demux->tsbuf[0] = 0x47;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 }
1680
1681bailout:
Mauro Carvalho Chehab28100162009-02-16 15:27:44 -03001682 spin_unlock(&demux->lock);
Hamad Kadmany32cb9822012-05-10 08:47:44 +03001683
1684 if (dvb_demux_performancecheck)
1685 demux->total_process_time += dvb_dmx_calc_time_delta(pre_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686}
Andreas Oberritterdad4a732005-09-09 13:02:26 -07001687
Marko Ristola38e009a2011-04-08 12:40:51 -03001688void dvb_dmx_swfilter(struct dvb_demux *demux, const u8 *buf, size_t count)
1689{
Hamad Kadmany32cb9822012-05-10 08:47:44 +03001690 _dvb_dmx_swfilter(demux, buf, count, 188, 0);
Marko Ristola38e009a2011-04-08 12:40:51 -03001691}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692EXPORT_SYMBOL(dvb_dmx_swfilter);
1693
1694void dvb_dmx_swfilter_204(struct dvb_demux *demux, const u8 *buf, size_t count)
1695{
Hamad Kadmany32cb9822012-05-10 08:47:44 +03001696 _dvb_dmx_swfilter(demux, buf, count, 204, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697}
1698EXPORT_SYMBOL(dvb_dmx_swfilter_204);
1699
Hamad Kadmany32cb9822012-05-10 08:47:44 +03001700void dvb_dmx_swfilter_format(
1701 struct dvb_demux *demux,
1702 const u8 *buf,
1703 size_t count,
1704 enum dmx_tsp_format_t tsp_format)
1705{
1706 switch (tsp_format) {
1707 case DMX_TSP_FORMAT_188:
1708 _dvb_dmx_swfilter(demux, buf, count, 188, 0);
1709 break;
1710
1711 case DMX_TSP_FORMAT_192_TAIL:
1712 _dvb_dmx_swfilter(demux, buf, count, 192, 0);
1713 break;
1714
1715 case DMX_TSP_FORMAT_192_HEAD:
Hamad Kadmany9e5ce472012-11-22 13:47:07 +02001716 _dvb_dmx_swfilter(demux, buf, count, 192, TIMESTAMP_LEN);
Hamad Kadmany32cb9822012-05-10 08:47:44 +03001717 break;
1718
1719 case DMX_TSP_FORMAT_204:
1720 _dvb_dmx_swfilter(demux, buf, count, 204, 0);
1721 break;
1722
1723 default:
1724 printk(KERN_ERR "%s: invalid TS packet format (format=%d)\n",
1725 __func__,
1726 tsp_format);
1727 break;
1728 }
1729}
1730EXPORT_SYMBOL(dvb_dmx_swfilter_format);
1731
Andreas Oberritterdad4a732005-09-09 13:02:26 -07001732static struct dvb_demux_filter *dvb_dmx_filter_alloc(struct dvb_demux *demux)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
1734 int i;
1735
Andreas Oberritterdad4a732005-09-09 13:02:26 -07001736 for (i = 0; i < demux->filternum; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 if (demux->filter[i].state == DMX_STATE_FREE)
1738 break;
1739
1740 if (i == demux->filternum)
1741 return NULL;
1742
1743 demux->filter[i].state = DMX_STATE_ALLOCATED;
1744
1745 return &demux->filter[i];
1746}
1747
Andreas Oberritterdad4a732005-09-09 13:02:26 -07001748static struct dvb_demux_feed *dvb_dmx_feed_alloc(struct dvb_demux *demux)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
1750 int i;
1751
Andreas Oberritterdad4a732005-09-09 13:02:26 -07001752 for (i = 0; i < demux->feednum; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 if (demux->feed[i].state == DMX_STATE_FREE)
1754 break;
1755
1756 if (i == demux->feednum)
1757 return NULL;
1758
1759 demux->feed[i].state = DMX_STATE_ALLOCATED;
1760
1761 return &demux->feed[i];
1762}
1763
Hamad Kadmanye1dba202013-04-10 18:37:25 +03001764const struct dvb_dmx_video_patterns *dvb_dmx_get_pattern(u64 dmx_idx_pattern)
1765{
1766 switch (dmx_idx_pattern) {
1767 case DMX_IDX_MPEG_SEQ_HEADER:
1768 return &mpeg2_seq_hdr;
1769
1770 case DMX_IDX_MPEG_GOP:
1771 return &mpeg2_gop;
1772
1773 case DMX_IDX_MPEG_I_FRAME_START:
1774 return &mpeg2_iframe;
1775
1776 case DMX_IDX_MPEG_P_FRAME_START:
1777 return &mpeg2_pframe;
1778
1779 case DMX_IDX_MPEG_B_FRAME_START:
1780 return &mpeg2_bframe;
1781
1782 case DMX_IDX_H264_SPS:
1783 return &h264_sps;
1784
1785 case DMX_IDX_H264_PPS:
1786 return &h264_pps;
1787
1788 case DMX_IDX_H264_IDR_START:
1789 return &h264_idr;
1790
1791 case DMX_IDX_H264_NON_IDR_START:
1792 return &h264_non_idr;
1793
1794 case DMX_IDX_VC1_SEQ_HEADER:
1795 return &vc1_seq_hdr;
1796
1797 case DMX_IDX_VC1_ENTRY_POINT:
1798 return &vc1_entry_point;
1799
1800 case DMX_IDX_VC1_FRAME_START:
1801 return &vc1_frame;
1802
1803 default:
1804 return NULL;
1805 }
1806}
1807EXPORT_SYMBOL(dvb_dmx_get_pattern);
1808
1809static void dvb_dmx_init_idx_state(struct dvb_demux_feed *feed)
1810{
1811 feed->prev_tsp_num = (u64)-1;
1812 feed->curr_pusi_tsp_num = (u64)-1;
1813 feed->prev_pusi_tsp_num = (u64)-1;
1814 feed->prev_frame_valid = 0;
1815 feed->first_frame_in_seq = 0;
1816 feed->first_frame_in_seq_notified = 0;
1817 feed->last_pattern_tsp_num = (u64)-1;
1818 feed->pattern_num = 0;
1819 memset(&feed->prefix_size, 0,
1820 sizeof(struct dvb_dmx_video_prefix_size_masks));
1821
1822 if (feed->idx_params.types &
1823 (DMX_IDX_MPEG_SEQ_HEADER |
1824 DMX_IDX_MPEG_FIRST_SEQ_FRAME_START |
1825 DMX_IDX_MPEG_FIRST_SEQ_FRAME_END)) {
1826 feed->patterns[feed->pattern_num] =
1827 dvb_dmx_get_pattern(DMX_IDX_MPEG_SEQ_HEADER);
1828 feed->pattern_num++;
1829 }
1830
1831 if ((feed->pattern_num < DVB_DMX_MAX_SEARCH_PATTERN_NUM) &&
1832 (feed->idx_params.types & DMX_IDX_MPEG_GOP)) {
1833 feed->patterns[feed->pattern_num] =
1834 dvb_dmx_get_pattern(DMX_IDX_MPEG_GOP);
1835 feed->pattern_num++;
1836 }
1837
1838 /* MPEG2 I-frame */
1839 if ((feed->pattern_num < DVB_DMX_MAX_SEARCH_PATTERN_NUM) &&
1840 (feed->idx_params.types &
1841 (DMX_IDX_MPEG_I_FRAME_START | DMX_IDX_MPEG_I_FRAME_END |
1842 DMX_IDX_MPEG_P_FRAME_END | DMX_IDX_MPEG_B_FRAME_END |
1843 DMX_IDX_MPEG_FIRST_SEQ_FRAME_START |
1844 DMX_IDX_MPEG_FIRST_SEQ_FRAME_END))) {
1845 feed->patterns[feed->pattern_num] =
1846 dvb_dmx_get_pattern(DMX_IDX_MPEG_I_FRAME_START);
1847 feed->pattern_num++;
1848 }
1849
1850 /* MPEG2 P-frame */
1851 if ((feed->pattern_num < DVB_DMX_MAX_SEARCH_PATTERN_NUM) &&
1852 (feed->idx_params.types &
1853 (DMX_IDX_MPEG_P_FRAME_START | DMX_IDX_MPEG_P_FRAME_END |
1854 DMX_IDX_MPEG_I_FRAME_END | DMX_IDX_MPEG_B_FRAME_END |
1855 DMX_IDX_MPEG_FIRST_SEQ_FRAME_START |
1856 DMX_IDX_MPEG_FIRST_SEQ_FRAME_END))) {
1857 feed->patterns[feed->pattern_num] =
1858 dvb_dmx_get_pattern(DMX_IDX_MPEG_P_FRAME_START);
1859 feed->pattern_num++;
1860 }
1861
1862 /* MPEG2 B-frame */
1863 if ((feed->pattern_num < DVB_DMX_MAX_SEARCH_PATTERN_NUM) &&
1864 (feed->idx_params.types &
1865 (DMX_IDX_MPEG_B_FRAME_START | DMX_IDX_MPEG_B_FRAME_END |
1866 DMX_IDX_MPEG_I_FRAME_END | DMX_IDX_MPEG_P_FRAME_END |
1867 DMX_IDX_MPEG_FIRST_SEQ_FRAME_START |
1868 DMX_IDX_MPEG_FIRST_SEQ_FRAME_END))) {
1869 feed->patterns[feed->pattern_num] =
1870 dvb_dmx_get_pattern(DMX_IDX_MPEG_B_FRAME_START);
1871 feed->pattern_num++;
1872 }
1873
1874 if ((feed->pattern_num < DVB_DMX_MAX_SEARCH_PATTERN_NUM) &&
1875 (feed->idx_params.types &
1876 (DMX_IDX_H264_SPS |
1877 DMX_IDX_H264_FIRST_SPS_FRAME_START |
1878 DMX_IDX_H264_FIRST_SPS_FRAME_END))) {
1879 feed->patterns[feed->pattern_num] =
1880 dvb_dmx_get_pattern(DMX_IDX_H264_SPS);
1881 feed->pattern_num++;
1882 }
1883
1884 if ((feed->pattern_num < DVB_DMX_MAX_SEARCH_PATTERN_NUM) &&
1885 (feed->idx_params.types & DMX_IDX_H264_PPS)) {
1886 feed->patterns[feed->pattern_num] =
1887 dvb_dmx_get_pattern(DMX_IDX_H264_PPS);
1888 feed->pattern_num++;
1889 }
1890
1891 /* H264 IDR */
1892 if ((feed->pattern_num < DVB_DMX_MAX_SEARCH_PATTERN_NUM) &&
1893 (feed->idx_params.types &
1894 (DMX_IDX_H264_IDR_START | DMX_IDX_H264_IDR_END |
1895 DMX_IDX_H264_NON_IDR_END |
Hamad Kadmany65f0e932013-06-19 13:06:52 +03001896 DMX_IDX_H264_FIRST_SPS_FRAME_START |
Hamad Kadmanye1dba202013-04-10 18:37:25 +03001897 DMX_IDX_H264_FIRST_SPS_FRAME_END))) {
1898 feed->patterns[feed->pattern_num] =
1899 dvb_dmx_get_pattern(DMX_IDX_H264_IDR_START);
1900 feed->pattern_num++;
1901 }
1902
1903 /* H264 non-IDR */
1904 if ((feed->pattern_num < DVB_DMX_MAX_SEARCH_PATTERN_NUM) &&
1905 (feed->idx_params.types &
1906 (DMX_IDX_H264_NON_IDR_START | DMX_IDX_H264_NON_IDR_END |
1907 DMX_IDX_H264_IDR_END |
Hamad Kadmany65f0e932013-06-19 13:06:52 +03001908 DMX_IDX_H264_FIRST_SPS_FRAME_START |
Hamad Kadmanye1dba202013-04-10 18:37:25 +03001909 DMX_IDX_H264_FIRST_SPS_FRAME_END))) {
1910 feed->patterns[feed->pattern_num] =
1911 dvb_dmx_get_pattern(DMX_IDX_H264_NON_IDR_START);
1912 feed->pattern_num++;
1913 }
1914
1915 if ((feed->pattern_num < DVB_DMX_MAX_SEARCH_PATTERN_NUM) &&
1916 (feed->idx_params.types &
1917 (DMX_IDX_VC1_SEQ_HEADER |
1918 DMX_IDX_VC1_FIRST_SEQ_FRAME_START |
1919 DMX_IDX_VC1_FIRST_SEQ_FRAME_END))) {
1920 feed->patterns[feed->pattern_num] =
1921 dvb_dmx_get_pattern(DMX_IDX_VC1_SEQ_HEADER);
1922 feed->pattern_num++;
1923 }
1924
1925 if ((feed->pattern_num < DVB_DMX_MAX_SEARCH_PATTERN_NUM) &&
1926 (feed->idx_params.types & DMX_IDX_VC1_ENTRY_POINT)) {
1927 feed->patterns[feed->pattern_num] =
1928 dvb_dmx_get_pattern(DMX_IDX_VC1_ENTRY_POINT);
1929 feed->pattern_num++;
1930 }
1931
1932 /* VC1 frame */
1933 if ((feed->pattern_num < DVB_DMX_MAX_SEARCH_PATTERN_NUM) &&
1934 (feed->idx_params.types &
1935 (DMX_IDX_VC1_FRAME_START | DMX_IDX_VC1_FRAME_END |
1936 DMX_IDX_VC1_FIRST_SEQ_FRAME_START |
1937 DMX_IDX_VC1_FIRST_SEQ_FRAME_END))) {
1938 feed->patterns[feed->pattern_num] =
1939 dvb_dmx_get_pattern(DMX_IDX_VC1_FRAME_START);
1940 feed->pattern_num++;
1941 }
1942
1943 if (feed->pattern_num)
1944 feed->rec_info->idx_info.pattern_search_feeds_num++;
1945}
1946
1947static struct dvb_demux_rec_info *dvb_dmx_alloc_rec_info(
1948 struct dmx_ts_feed *ts_feed)
1949{
1950 int i;
1951 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
1952 struct dvb_demux *demux = feed->demux;
1953 struct dvb_demux_rec_info *rec_info;
1954 struct dvb_demux_feed *tmp_feed;
1955
1956 /* check if this feed share recording buffer with other active feeds */
1957 list_for_each_entry(tmp_feed, &demux->feed_list, list_head) {
1958 if ((tmp_feed->state == DMX_STATE_GO) &&
1959 (tmp_feed->type == DMX_TYPE_TS) &&
1960 (tmp_feed != feed) &&
1961 (tmp_feed->feed.ts.buffer.ringbuff ==
1962 ts_feed->buffer.ringbuff)) {
1963 /* indexing information is shared between the feeds */
1964 tmp_feed->rec_info->ref_count++;
1965 return tmp_feed->rec_info;
1966 }
1967 }
1968
1969 /* Need to allocate a new indexing info */
1970 for (i = 0; i < demux->feednum; i++)
1971 if (!demux->rec_info_pool[i].ref_count)
1972 break;
1973
1974 if (i == demux->feednum)
1975 return NULL;
1976
1977 rec_info = &demux->rec_info_pool[i];
1978 rec_info->ref_count++;
1979 INIT_LIST_HEAD(&rec_info->idx_info.free_list);
1980 INIT_LIST_HEAD(&rec_info->idx_info.ready_list);
1981
1982 for (i = 0; i < DMX_IDX_EVENT_QUEUE_SIZE; i++)
1983 list_add(&rec_info->idx_info.events[i].next,
1984 &rec_info->idx_info.free_list);
1985
1986 rec_info->ts_output_count = 0;
1987 rec_info->idx_info.min_pattern_tsp_num = (u64)-1;
1988 rec_info->idx_info.pattern_search_feeds_num = 0;
1989
1990 return rec_info;
1991}
1992
1993static void dvb_dmx_free_rec_info(struct dmx_ts_feed *ts_feed)
1994{
1995 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
1996
1997 if (!feed->rec_info || !feed->rec_info->ref_count) {
1998 printk(KERN_ERR "%s: invalid idx info state\n", __func__);
1999 return;
2000 }
2001
2002 feed->rec_info->ref_count--;
2003
2004 return;
2005}
2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007static int dvb_demux_feed_find(struct dvb_demux_feed *feed)
2008{
2009 struct dvb_demux_feed *entry;
2010
2011 list_for_each_entry(entry, &feed->demux->feed_list, list_head)
2012 if (entry == feed)
2013 return 1;
2014
2015 return 0;
2016}
2017
2018static void dvb_demux_feed_add(struct dvb_demux_feed *feed)
2019{
2020 spin_lock_irq(&feed->demux->lock);
2021 if (dvb_demux_feed_find(feed)) {
2022 printk(KERN_ERR "%s: feed already in list (type=%x state=%x pid=%x)\n",
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03002023 __func__, feed->type, feed->state, feed->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 goto out;
2025 }
2026
2027 list_add(&feed->list_head, &feed->demux->feed_list);
2028out:
2029 spin_unlock_irq(&feed->demux->lock);
2030}
2031
2032static void dvb_demux_feed_del(struct dvb_demux_feed *feed)
2033{
2034 spin_lock_irq(&feed->demux->lock);
2035 if (!(dvb_demux_feed_find(feed))) {
2036 printk(KERN_ERR "%s: feed not in list (type=%x state=%x pid=%x)\n",
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03002037 __func__, feed->type, feed->state, feed->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 goto out;
2039 }
2040
2041 list_del(&feed->list_head);
2042out:
2043 spin_unlock_irq(&feed->demux->lock);
2044}
2045
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002046static int dmx_ts_feed_set(struct dmx_ts_feed *ts_feed, u16 pid, int ts_type,
2047 enum dmx_ts_pes pes_type,
2048 size_t circular_buffer_size, struct timespec timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002050 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 struct dvb_demux *demux = feed->demux;
2052
2053 if (pid > DMX_MAX_PID)
2054 return -EINVAL;
2055
Ingo Molnar3593cab2006-02-07 06:49:14 -02002056 if (mutex_lock_interruptible(&demux->mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 return -ERESTARTSYS;
2058
2059 if (ts_type & TS_DECODER) {
2060 if (pes_type >= DMX_TS_PES_OTHER) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02002061 mutex_unlock(&demux->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 return -EINVAL;
2063 }
2064
2065 if (demux->pesfilter[pes_type] &&
2066 demux->pesfilter[pes_type] != feed) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02002067 mutex_unlock(&demux->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 return -EINVAL;
2069 }
2070
2071 demux->pesfilter[pes_type] = feed;
2072 demux->pids[pes_type] = pid;
2073 }
2074
2075 dvb_demux_feed_add(feed);
2076
2077 feed->pid = pid;
2078 feed->buffer_size = circular_buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 feed->timeout = timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 feed->ts_type = ts_type;
2081 feed->pes_type = pes_type;
2082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 if (feed->buffer_size) {
2084#ifdef NOBUFS
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002085 feed->buffer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086#else
2087 feed->buffer = vmalloc(feed->buffer_size);
2088 if (!feed->buffer) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02002089 mutex_unlock(&demux->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 return -ENOMEM;
2091 }
2092#endif
2093 }
2094
2095 feed->state = DMX_STATE_READY;
Ingo Molnar3593cab2006-02-07 06:49:14 -02002096 mutex_unlock(&demux->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
2098 return 0;
2099}
2100
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002101static int dmx_ts_feed_start_filtering(struct dmx_ts_feed *ts_feed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102{
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002103 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 struct dvb_demux *demux = feed->demux;
2105 int ret;
2106
Ingo Molnar3593cab2006-02-07 06:49:14 -02002107 if (mutex_lock_interruptible(&demux->mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 return -ERESTARTSYS;
2109
2110 if (feed->state != DMX_STATE_READY || feed->type != DMX_TYPE_TS) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02002111 mutex_unlock(&demux->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 return -EINVAL;
2113 }
2114
2115 if (!demux->start_feed) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02002116 mutex_unlock(&demux->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 return -ENODEV;
2118 }
2119
Hamad Kadmanyeb75d1c2012-07-29 13:17:28 +03002120 feed->first_cc = 1;
Hamad Kadmanyd0abf312013-05-13 18:56:06 +03002121 feed->scrambling_bits = 0;
Hamad Kadmanyeb75d1c2012-07-29 13:17:28 +03002122
Hamad Kadmanye1dba202013-04-10 18:37:25 +03002123 if ((feed->ts_type & TS_PACKET) &&
2124 !(feed->ts_type & TS_PAYLOAD_ONLY)) {
2125 feed->rec_info = dvb_dmx_alloc_rec_info(ts_feed);
2126 if (!feed->rec_info) {
2127 mutex_unlock(&demux->mutex);
2128 return -ENOMEM;
2129 }
2130 dvb_dmx_init_idx_state(feed);
2131 } else {
2132 feed->pattern_num = 0;
2133 feed->rec_info = NULL;
2134 }
2135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 if ((ret = demux->start_feed(feed)) < 0) {
Hamad Kadmanye1dba202013-04-10 18:37:25 +03002137 dvb_dmx_free_rec_info(ts_feed);
2138 feed->rec_info = NULL;
Ingo Molnar3593cab2006-02-07 06:49:14 -02002139 mutex_unlock(&demux->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 return ret;
2141 }
2142
2143 spin_lock_irq(&demux->lock);
2144 ts_feed->is_filtering = 1;
2145 feed->state = DMX_STATE_GO;
2146 spin_unlock_irq(&demux->lock);
Ingo Molnar3593cab2006-02-07 06:49:14 -02002147 mutex_unlock(&demux->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149 return 0;
2150}
2151
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002152static int dmx_ts_feed_stop_filtering(struct dmx_ts_feed *ts_feed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002154 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 struct dvb_demux *demux = feed->demux;
2156 int ret;
2157
Simon Arlottc2788502007-03-10 06:21:25 -03002158 mutex_lock(&demux->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
2160 if (feed->state < DMX_STATE_GO) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02002161 mutex_unlock(&demux->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 return -EINVAL;
2163 }
2164
2165 if (!demux->stop_feed) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02002166 mutex_unlock(&demux->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 return -ENODEV;
2168 }
2169
2170 ret = demux->stop_feed(feed);
2171
2172 spin_lock_irq(&demux->lock);
2173 ts_feed->is_filtering = 0;
2174 feed->state = DMX_STATE_ALLOCATED;
2175 spin_unlock_irq(&demux->lock);
Hamad Kadmanye1dba202013-04-10 18:37:25 +03002176
2177 if (feed->rec_info) {
2178 if (feed->pattern_num)
2179 feed->rec_info->idx_info.pattern_search_feeds_num--;
2180 dvb_dmx_free_rec_info(ts_feed);
2181 feed->rec_info = NULL;
2182 }
2183
Ingo Molnar3593cab2006-02-07 06:49:14 -02002184 mutex_unlock(&demux->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
2186 return ret;
2187}
2188
Hamad Kadmany1ea84a92012-06-04 13:34:13 +03002189static int dmx_ts_feed_decoder_buff_status(struct dmx_ts_feed *ts_feed,
2190 struct dmx_buffer_status *dmx_buffer_status)
2191{
2192 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
2193 struct dvb_demux *demux = feed->demux;
2194 int ret;
2195
Liron Kuch41543b72012-11-26 13:27:22 +02002196 mutex_lock(&demux->mutex);
Hamad Kadmany1ea84a92012-06-04 13:34:13 +03002197
2198 if (feed->state < DMX_STATE_GO) {
Liron Kuch41543b72012-11-26 13:27:22 +02002199 mutex_unlock(&demux->mutex);
Hamad Kadmany1ea84a92012-06-04 13:34:13 +03002200 return -EINVAL;
2201 }
2202
2203 if (!demux->decoder_buffer_status) {
Liron Kuch41543b72012-11-26 13:27:22 +02002204 mutex_unlock(&demux->mutex);
Hamad Kadmany1ea84a92012-06-04 13:34:13 +03002205 return -ENODEV;
2206 }
2207
2208 ret = demux->decoder_buffer_status(feed, dmx_buffer_status);
2209
Liron Kuch41543b72012-11-26 13:27:22 +02002210 mutex_unlock(&demux->mutex);
2211
2212 return ret;
2213}
2214
2215static int dmx_ts_feed_reuse_decoder_buffer(struct dmx_ts_feed *ts_feed,
2216 int cookie)
2217{
2218 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
2219 struct dvb_demux *demux = feed->demux;
2220 int ret;
2221
2222 mutex_lock(&demux->mutex);
2223
2224 if (feed->state < DMX_STATE_GO) {
2225 mutex_unlock(&demux->mutex);
2226 return -EINVAL;
2227 }
2228
2229 if (!demux->reuse_decoder_buffer) {
2230 mutex_unlock(&demux->mutex);
2231 return -ENODEV;
2232 }
2233
2234 ret = demux->reuse_decoder_buffer(feed, cookie);
2235
2236 mutex_unlock(&demux->mutex);
Hamad Kadmany1ea84a92012-06-04 13:34:13 +03002237
2238 return ret;
2239}
2240
Hamad Kadmany4f877942012-07-15 15:06:01 +03002241static int dmx_ts_feed_data_ready_cb(struct dmx_ts_feed *feed,
2242 dmx_ts_data_ready_cb callback)
2243{
2244 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
2245 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
2246
2247 mutex_lock(&dvbdmx->mutex);
2248
2249 if (dvbdmxfeed->state == DMX_STATE_GO) {
2250 mutex_unlock(&dvbdmx->mutex);
2251 return -EINVAL;
2252 }
2253
2254 dvbdmxfeed->data_ready_cb.ts = callback;
2255
2256 mutex_unlock(&dvbdmx->mutex);
2257 return 0;
2258}
2259
Hamad Kadmany04e59fb2013-01-29 18:37:35 +02002260static int dmx_ts_set_secure_mode(struct dmx_ts_feed *feed,
2261 struct dmx_secure_mode *secure_mode)
2262{
2263 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
2264 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
Gilad Broner4e663752013-03-03 13:41:10 +02002265 int ret = 0;
Hamad Kadmany04e59fb2013-01-29 18:37:35 +02002266
2267 mutex_lock(&dvbdmx->mutex);
2268
Hamad Kadmany04e59fb2013-01-29 18:37:35 +02002269 if ((dvbdmxfeed->state == DMX_STATE_GO) &&
Gilad Broner4e663752013-03-03 13:41:10 +02002270 dvbdmxfeed->demux->set_secure_mode) {
2271 ret = dvbdmxfeed->demux->set_secure_mode(dvbdmxfeed,
2272 secure_mode);
2273 if (!ret)
2274 dvbdmxfeed->secure_mode = *secure_mode;
2275 } else {
2276 dvbdmxfeed->secure_mode = *secure_mode;
2277 }
Hamad Kadmany04e59fb2013-01-29 18:37:35 +02002278
2279 mutex_unlock(&dvbdmx->mutex);
Gilad Broner4e663752013-03-03 13:41:10 +02002280 return ret;
Hamad Kadmany04e59fb2013-01-29 18:37:35 +02002281}
2282
Hamad Kadmanyc37dee82013-04-10 18:14:48 +03002283static int dmx_ts_set_video_codec(
Liron Kuchd4cc3b02012-05-17 16:31:58 +03002284 struct dmx_ts_feed *ts_feed,
Hamad Kadmanyc37dee82013-04-10 18:14:48 +03002285 enum dmx_video_codec video_codec)
Liron Kuchd4cc3b02012-05-17 16:31:58 +03002286{
2287 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
2288
Hamad Kadmanyc37dee82013-04-10 18:14:48 +03002289 feed->video_codec = video_codec;
Liron Kuchd4cc3b02012-05-17 16:31:58 +03002290
2291 return 0;
2292}
2293
Hamad Kadmanye1dba202013-04-10 18:37:25 +03002294static int dmx_ts_set_idx_params(struct dmx_ts_feed *ts_feed,
2295 struct dmx_indexing_params *idx_params)
2296{
2297 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
2298 struct dvb_demux *dvbdmx = feed->demux;
2299
2300 mutex_lock(&dvbdmx->mutex);
2301
2302 if ((feed->state == DMX_STATE_GO) &&
2303 !feed->rec_info) {
2304 mutex_unlock(&dvbdmx->mutex);
2305 return -EINVAL;
2306 }
2307 feed->idx_params = *idx_params;
2308
2309 if (feed->state == DMX_STATE_GO) {
2310 spin_lock_irq(&dvbdmx->lock);
2311 if (feed->pattern_num)
2312 feed->rec_info->idx_info.pattern_search_feeds_num--;
2313 dvb_dmx_init_idx_state(feed);
2314 spin_unlock_irq(&dvbdmx->lock);
2315 }
2316
2317 mutex_unlock(&dvbdmx->mutex);
2318
2319 return 0;
2320}
2321
Gilad Bronera81f42e2013-04-07 16:38:47 +03002322static int dvbdmx_ts_feed_oob_cmd(struct dmx_ts_feed *ts_feed,
2323 struct dmx_oob_command *cmd)
2324{
2325 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
2326 struct dmx_data_ready data;
2327 struct dvb_demux *dvbdmx = feed->demux;
Gilad Broner180f65f2013-05-09 18:55:42 +03002328 int ret = 0;
2329 int secure_non_rec = feed->secure_mode.is_secured &&
2330 !dvb_dmx_is_rec_feed(feed);
Gilad Bronera81f42e2013-04-07 16:38:47 +03002331
2332 mutex_lock(&dvbdmx->mutex);
2333
2334 if (feed->state != DMX_STATE_GO) {
2335 mutex_unlock(&dvbdmx->mutex);
2336 return -EINVAL;
2337 }
2338
Gilad Broner180f65f2013-05-09 18:55:42 +03002339 /* Decoder & non-recording secure feeds are handled by plug-in */
2340 if ((feed->ts_type & TS_DECODER) || secure_non_rec) {
Gilad Bronera81f42e2013-04-07 16:38:47 +03002341 if (feed->demux->oob_command)
2342 ret = feed->demux->oob_command(feed, cmd);
Gilad Broner180f65f2013-05-09 18:55:42 +03002343 }
Gilad Bronera81f42e2013-04-07 16:38:47 +03002344
Gilad Broner180f65f2013-05-09 18:55:42 +03002345 if (!(feed->ts_type & (TS_PAYLOAD_ONLY | TS_PACKET)) ||
2346 secure_non_rec) {
Gilad Bronera81f42e2013-04-07 16:38:47 +03002347 mutex_unlock(&dvbdmx->mutex);
2348 return ret;
2349 }
2350
2351 data.data_length = 0;
2352
2353 switch (cmd->type) {
2354 case DMX_OOB_CMD_EOS:
Gilad Broner180f65f2013-05-09 18:55:42 +03002355 if (feed->ts_type & TS_PAYLOAD_ONLY)
2356 dvb_dmx_check_pes_end(feed);
Gilad Bronera81f42e2013-04-07 16:38:47 +03002357
Gilad Bronera81f42e2013-04-07 16:38:47 +03002358 data.status = DMX_OK_EOS;
2359 ret = feed->data_ready_cb.ts(&feed->feed.ts, &data);
2360 break;
2361
2362 case DMX_OOB_CMD_MARKER:
2363 data.status = DMX_OK_MARKER;
2364 data.marker.id = cmd->params.marker.id;
2365 ret = feed->data_ready_cb.ts(&feed->feed.ts, &data);
2366 break;
2367
2368 default:
2369 ret = -EINVAL;
2370 break;
2371 }
2372
2373 mutex_unlock(&dvbdmx->mutex);
2374 return ret;
2375}
2376
Hamad Kadmanyd0abf312013-05-13 18:56:06 +03002377static int dvbdmx_ts_get_scrambling_bits(struct dmx_ts_feed *ts_feed,
2378 u8 *value)
2379{
2380 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
2381 struct dvb_demux *demux = feed->demux;
2382
2383 spin_lock(&demux->lock);
2384
2385 if (!ts_feed->is_filtering) {
2386 spin_unlock(&demux->lock);
2387 return -EINVAL;
2388 }
2389
2390 *value = feed->scrambling_bits;
2391 spin_unlock(&demux->lock);
2392
2393 return 0;
2394}
2395
Hamad Kadmany0402d062013-05-02 15:52:07 +03002396static int dvbdmx_ts_insertion_insert_buffer(struct dmx_ts_feed *ts_feed,
2397 char *data, size_t size)
2398{
2399 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
2400 struct dvb_demux *demux = feed->demux;
2401
2402 spin_lock(&demux->lock);
2403 if (!ts_feed->is_filtering) {
2404 spin_unlock(&demux->lock);
2405 return 0;
2406 }
2407
2408 feed->cb.ts(data, size, NULL, 0, ts_feed, DMX_OK);
2409
2410 spin_unlock(&demux->lock);
2411
2412 return 0;
2413}
2414
Hamad Kadmany9e5ce472012-11-22 13:47:07 +02002415static int dmx_ts_set_tsp_out_format(
2416 struct dmx_ts_feed *ts_feed,
2417 enum dmx_tsp_format_t tsp_format)
2418{
2419 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
2420 struct dvb_demux *dvbdmx = feed->demux;
2421
2422 mutex_lock(&dvbdmx->mutex);
2423
2424 if (feed->state == DMX_STATE_GO) {
2425 mutex_unlock(&dvbdmx->mutex);
2426 return -EINVAL;
2427 }
2428
2429 feed->tsp_out_format = tsp_format;
2430 mutex_unlock(&dvbdmx->mutex);
2431 return 0;
2432}
2433
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002434static int dvbdmx_allocate_ts_feed(struct dmx_demux *dmx,
2435 struct dmx_ts_feed **ts_feed,
2436 dmx_ts_cb callback)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437{
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002438 struct dvb_demux *demux = (struct dvb_demux *)dmx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 struct dvb_demux_feed *feed;
2440
Ingo Molnar3593cab2006-02-07 06:49:14 -02002441 if (mutex_lock_interruptible(&demux->mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 return -ERESTARTSYS;
2443
2444 if (!(feed = dvb_dmx_feed_alloc(demux))) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02002445 mutex_unlock(&demux->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 return -EBUSY;
2447 }
2448
2449 feed->type = DMX_TYPE_TS;
2450 feed->cb.ts = callback;
2451 feed->demux = demux;
2452 feed->pid = 0xffff;
Hamad Kadmany4f877942012-07-15 15:06:01 +03002453 feed->peslen = 0;
Hamad Kadmanyd432f712013-01-17 17:36:17 +02002454 feed->pes_tei_counter = 0;
2455 feed->pes_ts_packets_num = 0;
2456 feed->pes_cont_err_counter = 0;
Hamad Kadmany04e59fb2013-01-29 18:37:35 +02002457 feed->secure_mode.is_secured = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 feed->buffer = NULL;
Hamad Kadmany9e5ce472012-11-22 13:47:07 +02002459 feed->tsp_out_format = DMX_TSP_FORMAT_188;
Hamad Kadmanye1dba202013-04-10 18:37:25 +03002460 feed->idx_params.enable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
Hamad Kadmany32cb9822012-05-10 08:47:44 +03002462 /* default behaviour - pass first PES data even if it is
2463 * partial PES data from previous PES that we didn't receive its header.
2464 * Override this to 0 in your start_feed function in order to handle
2465 * first PES differently.
2466 */
2467 feed->pusi_seen = 1;
2468
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 (*ts_feed) = &feed->feed.ts;
2470 (*ts_feed)->parent = dmx;
2471 (*ts_feed)->priv = NULL;
2472 (*ts_feed)->is_filtering = 0;
2473 (*ts_feed)->start_filtering = dmx_ts_feed_start_filtering;
2474 (*ts_feed)->stop_filtering = dmx_ts_feed_stop_filtering;
2475 (*ts_feed)->set = dmx_ts_feed_set;
Hamad Kadmanyc37dee82013-04-10 18:14:48 +03002476 (*ts_feed)->set_video_codec = dmx_ts_set_video_codec;
Hamad Kadmanye1dba202013-04-10 18:37:25 +03002477 (*ts_feed)->set_idx_params = dmx_ts_set_idx_params;
Hamad Kadmany9e5ce472012-11-22 13:47:07 +02002478 (*ts_feed)->set_tsp_out_format = dmx_ts_set_tsp_out_format;
Hamad Kadmany1ea84a92012-06-04 13:34:13 +03002479 (*ts_feed)->get_decoder_buff_status = dmx_ts_feed_decoder_buff_status;
Liron Kuch41543b72012-11-26 13:27:22 +02002480 (*ts_feed)->reuse_decoder_buffer = dmx_ts_feed_reuse_decoder_buffer;
Hamad Kadmany4f877942012-07-15 15:06:01 +03002481 (*ts_feed)->data_ready_cb = dmx_ts_feed_data_ready_cb;
Hamad Kadmanyc8530102012-07-23 11:43:09 +03002482 (*ts_feed)->notify_data_read = NULL;
Hamad Kadmany04e59fb2013-01-29 18:37:35 +02002483 (*ts_feed)->set_secure_mode = dmx_ts_set_secure_mode;
Gilad Bronera81f42e2013-04-07 16:38:47 +03002484 (*ts_feed)->oob_command = dvbdmx_ts_feed_oob_cmd;
Hamad Kadmanyd0abf312013-05-13 18:56:06 +03002485 (*ts_feed)->get_scrambling_bits = dvbdmx_ts_get_scrambling_bits;
Hamad Kadmany0402d062013-05-02 15:52:07 +03002486 (*ts_feed)->ts_insertion_init = NULL;
2487 (*ts_feed)->ts_insertion_terminate = NULL;
2488 (*ts_feed)->ts_insertion_insert_buffer =
2489 dvbdmx_ts_insertion_insert_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 if (!(feed->filter = dvb_dmx_filter_alloc(demux))) {
2492 feed->state = DMX_STATE_FREE;
Ingo Molnar3593cab2006-02-07 06:49:14 -02002493 mutex_unlock(&demux->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 return -EBUSY;
2495 }
2496
2497 feed->filter->type = DMX_TYPE_TS;
2498 feed->filter->feed = feed;
2499 feed->filter->state = DMX_STATE_READY;
2500
Ingo Molnar3593cab2006-02-07 06:49:14 -02002501 mutex_unlock(&demux->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
2503 return 0;
2504}
2505
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002506static int dvbdmx_release_ts_feed(struct dmx_demux *dmx,
2507 struct dmx_ts_feed *ts_feed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508{
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002509 struct dvb_demux *demux = (struct dvb_demux *)dmx;
2510 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
Simon Arlottc2788502007-03-10 06:21:25 -03002512 mutex_lock(&demux->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
2514 if (feed->state == DMX_STATE_FREE) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02002515 mutex_unlock(&demux->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 return -EINVAL;
2517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518#ifndef NOBUFS
2519 vfree(feed->buffer);
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002520 feed->buffer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521#endif
2522
2523 feed->state = DMX_STATE_FREE;
2524 feed->filter->state = DMX_STATE_FREE;
2525
2526 dvb_demux_feed_del(feed);
2527
2528 feed->pid = 0xffff;
2529
2530 if (feed->ts_type & TS_DECODER && feed->pes_type < DMX_TS_PES_OTHER)
2531 demux->pesfilter[feed->pes_type] = NULL;
2532
Ingo Molnar3593cab2006-02-07 06:49:14 -02002533 mutex_unlock(&demux->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 return 0;
2535}
2536
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537/******************************************************************************
2538 * dmx_section_feed API calls
2539 ******************************************************************************/
2540
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002541static int dmx_section_feed_allocate_filter(struct dmx_section_feed *feed,
2542 struct dmx_section_filter **filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543{
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002544 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 struct dvb_demux *dvbdemux = dvbdmxfeed->demux;
2546 struct dvb_demux_filter *dvbdmxfilter;
2547
Ingo Molnar3593cab2006-02-07 06:49:14 -02002548 if (mutex_lock_interruptible(&dvbdemux->mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 return -ERESTARTSYS;
2550
2551 dvbdmxfilter = dvb_dmx_filter_alloc(dvbdemux);
2552 if (!dvbdmxfilter) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02002553 mutex_unlock(&dvbdemux->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 return -EBUSY;
2555 }
2556
2557 spin_lock_irq(&dvbdemux->lock);
2558 *filter = &dvbdmxfilter->filter;
2559 (*filter)->parent = feed;
2560 (*filter)->priv = NULL;
2561 dvbdmxfilter->feed = dvbdmxfeed;
2562 dvbdmxfilter->type = DMX_TYPE_SEC;
2563 dvbdmxfilter->state = DMX_STATE_READY;
2564 dvbdmxfilter->next = dvbdmxfeed->filter;
2565 dvbdmxfeed->filter = dvbdmxfilter;
2566 spin_unlock_irq(&dvbdemux->lock);
2567
Ingo Molnar3593cab2006-02-07 06:49:14 -02002568 mutex_unlock(&dvbdemux->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 return 0;
2570}
2571
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002572static int dmx_section_feed_set(struct dmx_section_feed *feed,
2573 u16 pid, size_t circular_buffer_size,
2574 int check_crc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575{
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002576 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
2578
2579 if (pid > 0x1fff)
2580 return -EINVAL;
2581
Ingo Molnar3593cab2006-02-07 06:49:14 -02002582 if (mutex_lock_interruptible(&dvbdmx->mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 return -ERESTARTSYS;
2584
2585 dvb_demux_feed_add(dvbdmxfeed);
2586
2587 dvbdmxfeed->pid = pid;
2588 dvbdmxfeed->buffer_size = circular_buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 dvbdmxfeed->feed.sec.check_crc = check_crc;
2590
2591#ifdef NOBUFS
2592 dvbdmxfeed->buffer = NULL;
2593#else
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002594 dvbdmxfeed->buffer = vmalloc(dvbdmxfeed->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 if (!dvbdmxfeed->buffer) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02002596 mutex_unlock(&dvbdmx->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 return -ENOMEM;
2598 }
2599#endif
2600
2601 dvbdmxfeed->state = DMX_STATE_READY;
Ingo Molnar3593cab2006-02-07 06:49:14 -02002602 mutex_unlock(&dvbdmx->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 return 0;
2604}
2605
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606static void prepare_secfilters(struct dvb_demux_feed *dvbdmxfeed)
2607{
2608 int i;
2609 struct dvb_demux_filter *f;
2610 struct dmx_section_filter *sf;
2611 u8 mask, mode, doneq;
2612
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002613 if (!(f = dvbdmxfeed->filter))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 return;
2615 do {
2616 sf = &f->filter;
2617 doneq = 0;
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002618 for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 mode = sf->filter_mode[i];
2620 mask = sf->filter_mask[i];
2621 f->maskandmode[i] = mask & mode;
2622 doneq |= f->maskandnotmode[i] = mask & ~mode;
2623 }
2624 f->doneq = doneq ? 1 : 0;
2625 } while ((f = f->next));
2626}
2627
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628static int dmx_section_feed_start_filtering(struct dmx_section_feed *feed)
2629{
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002630 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
2632 int ret;
2633
Ingo Molnar3593cab2006-02-07 06:49:14 -02002634 if (mutex_lock_interruptible(&dvbdmx->mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 return -ERESTARTSYS;
2636
2637 if (feed->is_filtering) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02002638 mutex_unlock(&dvbdmx->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 return -EBUSY;
2640 }
2641
2642 if (!dvbdmxfeed->filter) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02002643 mutex_unlock(&dvbdmx->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 return -EINVAL;
2645 }
2646
2647 dvbdmxfeed->feed.sec.tsfeedp = 0;
2648 dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base;
2649 dvbdmxfeed->feed.sec.secbufp = 0;
2650 dvbdmxfeed->feed.sec.seclen = 0;
Hamad Kadmanyeb75d1c2012-07-29 13:17:28 +03002651 dvbdmxfeed->first_cc = 1;
Hamad Kadmanyd0abf312013-05-13 18:56:06 +03002652 dvbdmxfeed->scrambling_bits = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
2654 if (!dvbdmx->start_feed) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02002655 mutex_unlock(&dvbdmx->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 return -ENODEV;
2657 }
2658
2659 prepare_secfilters(dvbdmxfeed);
2660
2661 if ((ret = dvbdmx->start_feed(dvbdmxfeed)) < 0) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02002662 mutex_unlock(&dvbdmx->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 return ret;
2664 }
2665
2666 spin_lock_irq(&dvbdmx->lock);
2667 feed->is_filtering = 1;
2668 dvbdmxfeed->state = DMX_STATE_GO;
2669 spin_unlock_irq(&dvbdmx->lock);
2670
Ingo Molnar3593cab2006-02-07 06:49:14 -02002671 mutex_unlock(&dvbdmx->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 return 0;
2673}
2674
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002675static int dmx_section_feed_stop_filtering(struct dmx_section_feed *feed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676{
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002677 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
2679 int ret;
2680
Simon Arlottc2788502007-03-10 06:21:25 -03002681 mutex_lock(&dvbdmx->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
2683 if (!dvbdmx->stop_feed) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02002684 mutex_unlock(&dvbdmx->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 return -ENODEV;
2686 }
2687
2688 ret = dvbdmx->stop_feed(dvbdmxfeed);
2689
2690 spin_lock_irq(&dvbdmx->lock);
2691 dvbdmxfeed->state = DMX_STATE_READY;
2692 feed->is_filtering = 0;
2693 spin_unlock_irq(&dvbdmx->lock);
2694
Ingo Molnar3593cab2006-02-07 06:49:14 -02002695 mutex_unlock(&dvbdmx->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 return ret;
2697}
2698
Hamad Kadmany4f877942012-07-15 15:06:01 +03002699
2700static int dmx_section_feed_data_ready_cb(struct dmx_section_feed *feed,
2701 dmx_section_data_ready_cb callback)
2702{
2703 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
2704 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
2705
2706 mutex_lock(&dvbdmx->mutex);
2707
2708 if (dvbdmxfeed->state == DMX_STATE_GO) {
2709 mutex_unlock(&dvbdmx->mutex);
2710 return -EINVAL;
2711 }
2712
2713 dvbdmxfeed->data_ready_cb.sec = callback;
2714
2715 mutex_unlock(&dvbdmx->mutex);
2716 return 0;
2717}
2718
Hamad Kadmany04e59fb2013-01-29 18:37:35 +02002719static int dmx_section_set_secure_mode(struct dmx_section_feed *feed,
2720 struct dmx_secure_mode *secure_mode)
2721{
2722 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
2723 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
2724
2725 mutex_lock(&dvbdmx->mutex);
2726
2727 dvbdmxfeed->secure_mode = *secure_mode;
2728 if ((dvbdmxfeed->state == DMX_STATE_GO) &&
2729 dvbdmxfeed->demux->set_secure_mode)
2730 dvbdmxfeed->demux->set_secure_mode(dvbdmxfeed, secure_mode);
2731
2732 mutex_unlock(&dvbdmx->mutex);
2733 return 0;
2734}
2735
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736static int dmx_section_feed_release_filter(struct dmx_section_feed *feed,
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002737 struct dmx_section_filter *filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738{
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002739 struct dvb_demux_filter *dvbdmxfilter = (struct dvb_demux_filter *)filter, *f;
2740 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
2742
Simon Arlottc2788502007-03-10 06:21:25 -03002743 mutex_lock(&dvbdmx->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
2745 if (dvbdmxfilter->feed != dvbdmxfeed) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02002746 mutex_unlock(&dvbdmx->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 return -EINVAL;
2748 }
2749
2750 if (feed->is_filtering)
2751 feed->stop_filtering(feed);
2752
2753 spin_lock_irq(&dvbdmx->lock);
2754 f = dvbdmxfeed->filter;
2755
2756 if (f == dvbdmxfilter) {
2757 dvbdmxfeed->filter = dvbdmxfilter->next;
2758 } else {
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002759 while (f->next != dvbdmxfilter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 f = f->next;
2761 f->next = f->next->next;
2762 }
2763
2764 dvbdmxfilter->state = DMX_STATE_FREE;
2765 spin_unlock_irq(&dvbdmx->lock);
Ingo Molnar3593cab2006-02-07 06:49:14 -02002766 mutex_unlock(&dvbdmx->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 return 0;
2768}
2769
Gilad Bronera81f42e2013-04-07 16:38:47 +03002770static int dvbdmx_section_feed_oob_cmd(struct dmx_section_feed *section_feed,
2771 struct dmx_oob_command *cmd)
2772{
2773 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)section_feed;
2774 struct dvb_demux *dvbdmx = feed->demux;
2775 struct dmx_data_ready data;
Gilad Broneracb4b872013-05-30 18:55:59 +03002776 int ret = 0;
Gilad Bronera81f42e2013-04-07 16:38:47 +03002777
2778 data.data_length = 0;
2779
2780 mutex_lock(&dvbdmx->mutex);
2781
2782 if (feed->state != DMX_STATE_GO) {
2783 mutex_unlock(&dvbdmx->mutex);
2784 return -EINVAL;
2785 }
2786
2787 /* Secure section feeds are handled by the plug-in */
2788 if (feed->secure_mode.is_secured) {
2789 if (feed->demux->oob_command)
2790 ret = feed->demux->oob_command(feed, cmd);
2791 else
2792 ret = 0;
2793
2794 mutex_unlock(&dvbdmx->mutex);
2795 return ret;
2796 }
2797
2798 switch (cmd->type) {
2799 case DMX_OOB_CMD_EOS:
2800 data.status = DMX_OK_EOS;
Gilad Bronera81f42e2013-04-07 16:38:47 +03002801 break;
2802
2803 case DMX_OOB_CMD_MARKER:
2804 data.status = DMX_OK_MARKER;
2805 data.marker.id = cmd->params.marker.id;
Gilad Bronera81f42e2013-04-07 16:38:47 +03002806 break;
2807
2808 default:
2809 ret = -EINVAL;
2810 break;
2811 }
2812
Gilad Broneracb4b872013-05-30 18:55:59 +03002813 if (!ret)
2814 ret = dvb_dmx_notify_section_event(feed, &data, 1);
2815
Gilad Bronera81f42e2013-04-07 16:38:47 +03002816 mutex_unlock(&dvbdmx->mutex);
2817 return ret;
2818}
2819
Hamad Kadmanyd0abf312013-05-13 18:56:06 +03002820static int dvbdmx_section_get_scrambling_bits(
2821 struct dmx_section_feed *section_feed, u8 *value)
2822{
2823 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)section_feed;
2824 struct dvb_demux *demux = feed->demux;
2825
2826 spin_lock(&demux->lock);
2827
2828 if (!section_feed->is_filtering) {
2829 spin_unlock(&demux->lock);
2830 return -EINVAL;
2831 }
2832
2833 *value = feed->scrambling_bits;
2834 spin_unlock(&demux->lock);
2835
2836 return 0;
2837}
2838
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839static int dvbdmx_allocate_section_feed(struct dmx_demux *demux,
2840 struct dmx_section_feed **feed,
2841 dmx_section_cb callback)
2842{
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002843 struct dvb_demux *dvbdmx = (struct dvb_demux *)demux;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 struct dvb_demux_feed *dvbdmxfeed;
2845
Ingo Molnar3593cab2006-02-07 06:49:14 -02002846 if (mutex_lock_interruptible(&dvbdmx->mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 return -ERESTARTSYS;
2848
2849 if (!(dvbdmxfeed = dvb_dmx_feed_alloc(dvbdmx))) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02002850 mutex_unlock(&dvbdmx->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 return -EBUSY;
2852 }
2853
2854 dvbdmxfeed->type = DMX_TYPE_SEC;
2855 dvbdmxfeed->cb.sec = callback;
2856 dvbdmxfeed->demux = dvbdmx;
2857 dvbdmxfeed->pid = 0xffff;
Hamad Kadmany04e59fb2013-01-29 18:37:35 +02002858 dvbdmxfeed->secure_mode.is_secured = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base;
2860 dvbdmxfeed->feed.sec.secbufp = dvbdmxfeed->feed.sec.seclen = 0;
2861 dvbdmxfeed->feed.sec.tsfeedp = 0;
2862 dvbdmxfeed->filter = NULL;
2863 dvbdmxfeed->buffer = NULL;
2864
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002865 (*feed) = &dvbdmxfeed->feed.sec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 (*feed)->is_filtering = 0;
2867 (*feed)->parent = demux;
2868 (*feed)->priv = NULL;
2869
2870 (*feed)->set = dmx_section_feed_set;
2871 (*feed)->allocate_filter = dmx_section_feed_allocate_filter;
2872 (*feed)->start_filtering = dmx_section_feed_start_filtering;
2873 (*feed)->stop_filtering = dmx_section_feed_stop_filtering;
2874 (*feed)->release_filter = dmx_section_feed_release_filter;
Hamad Kadmany4f877942012-07-15 15:06:01 +03002875 (*feed)->data_ready_cb = dmx_section_feed_data_ready_cb;
Hamad Kadmanyc8530102012-07-23 11:43:09 +03002876 (*feed)->notify_data_read = NULL;
Hamad Kadmany04e59fb2013-01-29 18:37:35 +02002877 (*feed)->set_secure_mode = dmx_section_set_secure_mode;
Gilad Bronera81f42e2013-04-07 16:38:47 +03002878 (*feed)->oob_command = dvbdmx_section_feed_oob_cmd;
Hamad Kadmanyd0abf312013-05-13 18:56:06 +03002879 (*feed)->get_scrambling_bits = dvbdmx_section_get_scrambling_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
Ingo Molnar3593cab2006-02-07 06:49:14 -02002881 mutex_unlock(&dvbdmx->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 return 0;
2883}
2884
2885static int dvbdmx_release_section_feed(struct dmx_demux *demux,
2886 struct dmx_section_feed *feed)
2887{
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002888 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
2889 struct dvb_demux *dvbdmx = (struct dvb_demux *)demux;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
Simon Arlottc2788502007-03-10 06:21:25 -03002891 mutex_lock(&dvbdmx->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002893 if (dvbdmxfeed->state == DMX_STATE_FREE) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02002894 mutex_unlock(&dvbdmx->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 return -EINVAL;
2896 }
2897#ifndef NOBUFS
2898 vfree(dvbdmxfeed->buffer);
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002899 dvbdmxfeed->buffer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900#endif
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002901 dvbdmxfeed->state = DMX_STATE_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
2903 dvb_demux_feed_del(dvbdmxfeed);
2904
2905 dvbdmxfeed->pid = 0xffff;
2906
Ingo Molnar3593cab2006-02-07 06:49:14 -02002907 mutex_unlock(&dvbdmx->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 return 0;
2909}
2910
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911/******************************************************************************
2912 * dvb_demux kernel data API calls
2913 ******************************************************************************/
2914
2915static int dvbdmx_open(struct dmx_demux *demux)
2916{
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002917 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
2919 if (dvbdemux->users >= MAX_DVB_DEMUX_USERS)
2920 return -EUSERS;
2921
2922 dvbdemux->users++;
2923 return 0;
2924}
2925
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926static int dvbdmx_close(struct dmx_demux *demux)
2927{
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002928 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
2930 if (dvbdemux->users == 0)
2931 return -ENODEV;
2932
2933 dvbdemux->users--;
2934 //FIXME: release any unneeded resources if users==0
2935 return 0;
2936}
2937
Hamad Kadmany32cb9822012-05-10 08:47:44 +03002938static int dvbdmx_write(struct dmx_demux *demux, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939{
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002940 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941
2942 if ((!demux->frontend) || (demux->frontend->source != DMX_MEMORY_FE))
2943 return -EINVAL;
2944
Hamad Kadmany32cb9822012-05-10 08:47:44 +03002945 dvb_dmx_swfilter_format(dvbdemux, buf, count, dvbdemux->tsp_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
2947 if (signal_pending(current))
2948 return -EINTR;
2949 return count;
2950}
2951
Hamad Kadmany32cb9822012-05-10 08:47:44 +03002952static int dvbdmx_write_cancel(struct dmx_demux *demux)
2953{
2954 struct dvb_demux *dvbdmx = (struct dvb_demux *)demux;
2955
2956 spin_lock_irq(&dvbdmx->lock);
2957
2958 /* cancel any pending wait for decoder's buffers */
2959 dvbdmx->sw_filter_abort = 1;
2960 dvbdmx->tsbufp = 0;
2961 dvb_dmx_configure_decoder_fullness(dvbdmx, 0);
2962
2963 spin_unlock_irq(&dvbdmx->lock);
2964
2965 return 0;
2966}
2967
2968static int dvbdmx_set_playback_mode(struct dmx_demux *demux,
2969 enum dmx_playback_mode_t mode,
2970 dmx_ts_fullness ts_fullness_callback,
2971 dmx_section_fullness sec_fullness_callback)
2972{
2973 struct dvb_demux *dvbdmx = (struct dvb_demux *)demux;
2974
2975 mutex_lock(&dvbdmx->mutex);
2976
2977 dvbdmx->playback_mode = mode;
2978 dvbdmx->buffer_ctrl.ts = ts_fullness_callback;
2979 dvbdmx->buffer_ctrl.sec = sec_fullness_callback;
2980
2981 mutex_unlock(&dvbdmx->mutex);
2982
2983 return 0;
2984}
2985
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002986static int dvbdmx_add_frontend(struct dmx_demux *demux,
2987 struct dmx_frontend *frontend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988{
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002989 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 struct list_head *head = &dvbdemux->frontend_list;
2991
2992 list_add(&(frontend->connectivity_list), head);
2993
2994 return 0;
2995}
2996
Andreas Oberritterdad4a732005-09-09 13:02:26 -07002997static int dvbdmx_remove_frontend(struct dmx_demux *demux,
2998 struct dmx_frontend *frontend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999{
Andreas Oberritterdad4a732005-09-09 13:02:26 -07003000 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 struct list_head *pos, *n, *head = &dvbdemux->frontend_list;
3002
Andreas Oberritterdad4a732005-09-09 13:02:26 -07003003 list_for_each_safe(pos, n, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 if (DMX_FE_ENTRY(pos) == frontend) {
3005 list_del(pos);
3006 return 0;
3007 }
3008 }
3009
3010 return -ENODEV;
3011}
3012
Andreas Oberritterdad4a732005-09-09 13:02:26 -07003013static struct list_head *dvbdmx_get_frontends(struct dmx_demux *demux)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014{
Andreas Oberritterdad4a732005-09-09 13:02:26 -07003015 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
3017 if (list_empty(&dvbdemux->frontend_list))
3018 return NULL;
Andreas Oberritterdad4a732005-09-09 13:02:26 -07003019
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 return &dvbdemux->frontend_list;
3021}
3022
Andreas Oberritterdad4a732005-09-09 13:02:26 -07003023static int dvbdmx_connect_frontend(struct dmx_demux *demux,
3024 struct dmx_frontend *frontend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025{
Andreas Oberritterdad4a732005-09-09 13:02:26 -07003026 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
3028 if (demux->frontend)
3029 return -EINVAL;
3030
Simon Arlottc2788502007-03-10 06:21:25 -03003031 mutex_lock(&dvbdemux->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
3033 demux->frontend = frontend;
Ingo Molnar3593cab2006-02-07 06:49:14 -02003034 mutex_unlock(&dvbdemux->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 return 0;
3036}
3037
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038static int dvbdmx_disconnect_frontend(struct dmx_demux *demux)
3039{
Andreas Oberritterdad4a732005-09-09 13:02:26 -07003040 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
Simon Arlottc2788502007-03-10 06:21:25 -03003042 mutex_lock(&dvbdemux->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
3044 demux->frontend = NULL;
Ingo Molnar3593cab2006-02-07 06:49:14 -02003045 mutex_unlock(&dvbdemux->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 return 0;
3047}
3048
Andreas Oberritterdad4a732005-09-09 13:02:26 -07003049static int dvbdmx_get_pes_pids(struct dmx_demux *demux, u16 * pids)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050{
Andreas Oberritterdad4a732005-09-09 13:02:26 -07003051 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Andreas Oberritterdad4a732005-09-09 13:02:26 -07003053 memcpy(pids, dvbdemux->pids, 5 * sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 return 0;
3055}
3056
Gilad Bronera81f42e2013-04-07 16:38:47 +03003057static int dvbdmx_get_tsp_size(struct dmx_demux *demux)
3058{
3059 int tsp_size;
3060 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
3061
3062 mutex_lock(&dvbdemux->mutex);
3063 tsp_size = dvbdemux->ts_packet_size;
3064 mutex_unlock(&dvbdemux->mutex);
3065
3066 return tsp_size;
3067}
3068
Hamad Kadmany32cb9822012-05-10 08:47:44 +03003069static int dvbdmx_set_tsp_format(
3070 struct dmx_demux *demux,
3071 enum dmx_tsp_format_t tsp_format)
3072{
3073 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
3074
3075 if ((tsp_format > DMX_TSP_FORMAT_204) ||
3076 (tsp_format < DMX_TSP_FORMAT_188))
3077 return -EINVAL;
3078
3079 mutex_lock(&dvbdemux->mutex);
3080
3081 dvbdemux->tsp_format = tsp_format;
Hamad Kadmany168203b2013-01-28 15:22:46 +02003082 switch (tsp_format) {
3083 case DMX_TSP_FORMAT_188:
3084 dvbdemux->ts_packet_size = 188;
3085 break;
3086 case DMX_TSP_FORMAT_192_TAIL:
3087 case DMX_TSP_FORMAT_192_HEAD:
3088 dvbdemux->ts_packet_size = 192;
3089 break;
3090 case DMX_TSP_FORMAT_204:
3091 dvbdemux->ts_packet_size = 204;
3092 break;
3093 }
3094
Hamad Kadmany32cb9822012-05-10 08:47:44 +03003095 mutex_unlock(&dvbdemux->mutex);
3096 return 0;
3097}
3098
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099int dvb_dmx_init(struct dvb_demux *dvbdemux)
3100{
Andreas Oberritter93653462005-09-09 13:02:23 -07003101 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 struct dmx_demux *dmx = &dvbdemux->dmx;
3103
Abylay Ospan0d834632009-06-06 09:31:34 -03003104 dvbdemux->cnt_storage = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 dvbdemux->users = 0;
Andreas Oberritterdad4a732005-09-09 13:02:26 -07003106 dvbdemux->filter = vmalloc(dvbdemux->filternum * sizeof(struct dvb_demux_filter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
3108 if (!dvbdemux->filter)
3109 return -ENOMEM;
3110
Andreas Oberritterdad4a732005-09-09 13:02:26 -07003111 dvbdemux->feed = vmalloc(dvbdemux->feednum * sizeof(struct dvb_demux_feed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 if (!dvbdemux->feed) {
3113 vfree(dvbdemux->filter);
Mauro Carvalho Chehabadefdce2010-02-01 10:35:22 -03003114 dvbdemux->filter = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 return -ENOMEM;
3116 }
Hamad Kadmany32cb9822012-05-10 08:47:44 +03003117
Hamad Kadmanye1dba202013-04-10 18:37:25 +03003118 dvbdemux->rec_info_pool = vmalloc(dvbdemux->feednum *
3119 sizeof(struct dvb_demux_rec_info));
3120 if (!dvbdemux->rec_info_pool) {
3121 vfree(dvbdemux->feed);
3122 vfree(dvbdemux->filter);
3123 dvbdemux->feed = NULL;
3124 dvbdemux->filter = NULL;
3125 return -ENOMEM;
3126 }
3127
Hamad Kadmany32cb9822012-05-10 08:47:44 +03003128 dvbdemux->total_process_time = 0;
3129 dvbdemux->total_crc_time = 0;
3130 snprintf(dvbdemux->alias,
3131 MAX_DVB_DEMUX_NAME_LEN,
3132 "demux%d",
3133 dvb_demux_index++);
3134
Hamad Kadmany4b5b7302012-11-15 09:01:41 +02003135 dvbdemux->dmx.debugfs_demux_dir =
3136 debugfs_create_dir(dvbdemux->alias, NULL);
Hamad Kadmany32cb9822012-05-10 08:47:44 +03003137
Hamad Kadmany4b5b7302012-11-15 09:01:41 +02003138 if (dvbdemux->dmx.debugfs_demux_dir != NULL) {
Hamad Kadmany32cb9822012-05-10 08:47:44 +03003139 debugfs_create_u32(
3140 "total_processing_time",
Hamad Kadmanya1dde822013-03-28 08:23:26 +02003141 S_IRUGO | S_IWUSR | S_IWGRP,
Hamad Kadmany4b5b7302012-11-15 09:01:41 +02003142 dvbdemux->dmx.debugfs_demux_dir,
Hamad Kadmany32cb9822012-05-10 08:47:44 +03003143 &dvbdemux->total_process_time);
3144
3145 debugfs_create_u32(
3146 "total_crc_time",
Hamad Kadmanya1dde822013-03-28 08:23:26 +02003147 S_IRUGO | S_IWUSR | S_IWGRP,
Hamad Kadmany4b5b7302012-11-15 09:01:41 +02003148 dvbdemux->dmx.debugfs_demux_dir,
Hamad Kadmany32cb9822012-05-10 08:47:44 +03003149 &dvbdemux->total_crc_time);
3150 }
3151
Andreas Oberritterdad4a732005-09-09 13:02:26 -07003152 for (i = 0; i < dvbdemux->filternum; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 dvbdemux->filter[i].state = DMX_STATE_FREE;
3154 dvbdemux->filter[i].index = i;
3155 }
Hamad Kadmanye1dba202013-04-10 18:37:25 +03003156
Andreas Oberritterdad4a732005-09-09 13:02:26 -07003157 for (i = 0; i < dvbdemux->feednum; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 dvbdemux->feed[i].state = DMX_STATE_FREE;
3159 dvbdemux->feed[i].index = i;
Hamad Kadmanye1dba202013-04-10 18:37:25 +03003160
3161 dvbdemux->rec_info_pool[i].ref_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 }
Andreas Oberritterdb574d72005-09-09 13:02:26 -07003163
Abylay Ospanab6a21f2010-03-06 14:58:01 -03003164 dvbdemux->cnt_storage = vmalloc(MAX_PID + 1);
3165 if (!dvbdemux->cnt_storage)
3166 printk(KERN_WARNING "Couldn't allocate memory for TS/TEI check. Disabling it\n");
Mauro Carvalho Chehabbc081cc2010-02-01 11:50:42 -03003167
Andreas Oberritterdb574d72005-09-09 13:02:26 -07003168 INIT_LIST_HEAD(&dvbdemux->frontend_list);
3169
Andreas Oberritterdad4a732005-09-09 13:02:26 -07003170 for (i = 0; i < DMX_TS_PES_OTHER; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 dvbdemux->pesfilter[i] = NULL;
3172 dvbdemux->pids[i] = 0xffff;
3173 }
3174
3175 INIT_LIST_HEAD(&dvbdemux->feed_list);
3176
3177 dvbdemux->playing = 0;
3178 dvbdemux->recording = 0;
3179 dvbdemux->tsbufp = 0;
3180
Hamad Kadmany32cb9822012-05-10 08:47:44 +03003181 dvbdemux->tsp_format = DMX_TSP_FORMAT_188;
Hamad Kadmany168203b2013-01-28 15:22:46 +02003182 dvbdemux->ts_packet_size = 188;
Hamad Kadmany32cb9822012-05-10 08:47:44 +03003183
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 if (!dvbdemux->check_crc32)
3185 dvbdemux->check_crc32 = dvb_dmx_crc32;
3186
Andreas Oberritterdad4a732005-09-09 13:02:26 -07003187 if (!dvbdemux->memcopy)
3188 dvbdemux->memcopy = dvb_dmx_memcopy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189
3190 dmx->frontend = NULL;
Andreas Oberritterdad4a732005-09-09 13:02:26 -07003191 dmx->priv = dvbdemux;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 dmx->open = dvbdmx_open;
3193 dmx->close = dvbdmx_close;
3194 dmx->write = dvbdmx_write;
Hamad Kadmany32cb9822012-05-10 08:47:44 +03003195 dmx->write_cancel = dvbdmx_write_cancel;
3196 dmx->set_playback_mode = dvbdmx_set_playback_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 dmx->allocate_ts_feed = dvbdmx_allocate_ts_feed;
3198 dmx->release_ts_feed = dvbdmx_release_ts_feed;
3199 dmx->allocate_section_feed = dvbdmx_allocate_section_feed;
3200 dmx->release_section_feed = dvbdmx_release_section_feed;
Hamad Kadmanyec6202d2012-09-20 07:58:57 +03003201 dmx->map_buffer = NULL;
3202 dmx->unmap_buffer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 dmx->add_frontend = dvbdmx_add_frontend;
3205 dmx->remove_frontend = dvbdmx_remove_frontend;
3206 dmx->get_frontends = dvbdmx_get_frontends;
3207 dmx->connect_frontend = dvbdmx_connect_frontend;
3208 dmx->disconnect_frontend = dvbdmx_disconnect_frontend;
3209 dmx->get_pes_pids = dvbdmx_get_pes_pids;
3210
Hamad Kadmany32cb9822012-05-10 08:47:44 +03003211 dmx->set_tsp_format = dvbdmx_set_tsp_format;
Gilad Bronera81f42e2013-04-07 16:38:47 +03003212 dmx->get_tsp_size = dvbdmx_get_tsp_size;
Hamad Kadmany32cb9822012-05-10 08:47:44 +03003213
Ingo Molnar3593cab2006-02-07 06:49:14 -02003214 mutex_init(&dvbdemux->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 spin_lock_init(&dvbdemux->lock);
3216
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 return 0;
3218}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
Andreas Oberritterdad4a732005-09-09 13:02:26 -07003220EXPORT_SYMBOL(dvb_dmx_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
Andreas Oberritter93653462005-09-09 13:02:23 -07003222void dvb_dmx_release(struct dvb_demux *dvbdemux)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223{
Hamad Kadmany4b5b7302012-11-15 09:01:41 +02003224 if (dvbdemux->dmx.debugfs_demux_dir != NULL)
3225 debugfs_remove_recursive(dvbdemux->dmx.debugfs_demux_dir);
Hamad Kadmany32cb9822012-05-10 08:47:44 +03003226
Hamad Kadmany4b5b7302012-11-15 09:01:41 +02003227 dvb_demux_index--;
Abylay Ospan0d834632009-06-06 09:31:34 -03003228 vfree(dvbdemux->cnt_storage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 vfree(dvbdemux->filter);
3230 vfree(dvbdemux->feed);
Hamad Kadmanye1dba202013-04-10 18:37:25 +03003231 vfree(dvbdemux->rec_info_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232}
Andreas Oberritterdad4a732005-09-09 13:02:26 -07003233
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234EXPORT_SYMBOL(dvb_dmx_release);