1*4882a593Smuzhiyun /* 2*4882a593Smuzhiyun * dvb_demux.h: DVB kernel demux API 3*4882a593Smuzhiyun * 4*4882a593Smuzhiyun * Copyright (C) 2000-2001 Marcus Metzler & Ralph Metzler 5*4882a593Smuzhiyun * for convergence integrated media GmbH 6*4882a593Smuzhiyun * 7*4882a593Smuzhiyun * This program is free software; you can redistribute it and/or 8*4882a593Smuzhiyun * modify it under the terms of the GNU Lesser General Public License 9*4882a593Smuzhiyun * as published by the Free Software Foundation; either version 2.1 10*4882a593Smuzhiyun * of the License, or (at your option) any later version. 11*4882a593Smuzhiyun * 12*4882a593Smuzhiyun * This program is distributed in the hope that it will be useful, 13*4882a593Smuzhiyun * but WITHOUT ANY WARRANTY; without even the implied warranty of 14*4882a593Smuzhiyun * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15*4882a593Smuzhiyun * GNU General Public License for more details. 16*4882a593Smuzhiyun * 17*4882a593Smuzhiyun */ 18*4882a593Smuzhiyun 19*4882a593Smuzhiyun #ifndef _DVB_DEMUX_H_ 20*4882a593Smuzhiyun #define _DVB_DEMUX_H_ 21*4882a593Smuzhiyun 22*4882a593Smuzhiyun #include <linux/time.h> 23*4882a593Smuzhiyun #include <linux/timer.h> 24*4882a593Smuzhiyun #include <linux/spinlock.h> 25*4882a593Smuzhiyun #include <linux/mutex.h> 26*4882a593Smuzhiyun 27*4882a593Smuzhiyun #include <media/demux.h> 28*4882a593Smuzhiyun 29*4882a593Smuzhiyun /** 30*4882a593Smuzhiyun * enum dvb_dmx_filter_type - type of demux feed. 31*4882a593Smuzhiyun * 32*4882a593Smuzhiyun * @DMX_TYPE_TS: feed is in TS mode. 33*4882a593Smuzhiyun * @DMX_TYPE_SEC: feed is in Section mode. 34*4882a593Smuzhiyun */ 35*4882a593Smuzhiyun enum dvb_dmx_filter_type { 36*4882a593Smuzhiyun DMX_TYPE_TS, 37*4882a593Smuzhiyun DMX_TYPE_SEC, 38*4882a593Smuzhiyun }; 39*4882a593Smuzhiyun 40*4882a593Smuzhiyun /** 41*4882a593Smuzhiyun * enum dvb_dmx_state - state machine for a demux filter. 42*4882a593Smuzhiyun * 43*4882a593Smuzhiyun * @DMX_STATE_FREE: indicates that the filter is freed. 44*4882a593Smuzhiyun * @DMX_STATE_ALLOCATED: indicates that the filter was allocated 45*4882a593Smuzhiyun * to be used. 46*4882a593Smuzhiyun * @DMX_STATE_READY: indicates that the filter is ready 47*4882a593Smuzhiyun * to be used. 48*4882a593Smuzhiyun * @DMX_STATE_GO: indicates that the filter is running. 49*4882a593Smuzhiyun */ 50*4882a593Smuzhiyun enum dvb_dmx_state { 51*4882a593Smuzhiyun DMX_STATE_FREE, 52*4882a593Smuzhiyun DMX_STATE_ALLOCATED, 53*4882a593Smuzhiyun DMX_STATE_READY, 54*4882a593Smuzhiyun DMX_STATE_GO, 55*4882a593Smuzhiyun }; 56*4882a593Smuzhiyun 57*4882a593Smuzhiyun #define DVB_DEMUX_MASK_MAX 18 58*4882a593Smuzhiyun 59*4882a593Smuzhiyun #define MAX_PID 0x1fff 60*4882a593Smuzhiyun 61*4882a593Smuzhiyun #define SPEED_PKTS_INTERVAL 50000 62*4882a593Smuzhiyun 63*4882a593Smuzhiyun /** 64*4882a593Smuzhiyun * struct dvb_demux_filter - Describes a DVB demux section filter. 65*4882a593Smuzhiyun * 66*4882a593Smuzhiyun * @filter: Section filter as defined by &struct dmx_section_filter. 67*4882a593Smuzhiyun * @maskandmode: logical ``and`` bit mask. 68*4882a593Smuzhiyun * @maskandnotmode: logical ``and not`` bit mask. 69*4882a593Smuzhiyun * @doneq: flag that indicates when a filter is ready. 70*4882a593Smuzhiyun * @next: pointer to the next section filter. 71*4882a593Smuzhiyun * @feed: &struct dvb_demux_feed pointer. 72*4882a593Smuzhiyun * @index: index of the used demux filter. 73*4882a593Smuzhiyun * @state: state of the filter as described by &enum dvb_dmx_state. 74*4882a593Smuzhiyun * @type: type of the filter as described 75*4882a593Smuzhiyun * by &enum dvb_dmx_filter_type. 76*4882a593Smuzhiyun */ 77*4882a593Smuzhiyun 78*4882a593Smuzhiyun struct dvb_demux_filter { 79*4882a593Smuzhiyun struct dmx_section_filter filter; 80*4882a593Smuzhiyun u8 maskandmode[DMX_MAX_FILTER_SIZE]; 81*4882a593Smuzhiyun u8 maskandnotmode[DMX_MAX_FILTER_SIZE]; 82*4882a593Smuzhiyun bool doneq; 83*4882a593Smuzhiyun 84*4882a593Smuzhiyun struct dvb_demux_filter *next; 85*4882a593Smuzhiyun struct dvb_demux_feed *feed; 86*4882a593Smuzhiyun int index; 87*4882a593Smuzhiyun enum dvb_dmx_state state; 88*4882a593Smuzhiyun enum dvb_dmx_filter_type type; 89*4882a593Smuzhiyun 90*4882a593Smuzhiyun /* private: used only by av7110 */ 91*4882a593Smuzhiyun u16 hw_handle; 92*4882a593Smuzhiyun }; 93*4882a593Smuzhiyun 94*4882a593Smuzhiyun /** 95*4882a593Smuzhiyun * struct dvb_demux_feed - describes a DVB field 96*4882a593Smuzhiyun * 97*4882a593Smuzhiyun * @feed: a union describing a digital TV feed. 98*4882a593Smuzhiyun * Depending on the feed type, it can be either 99*4882a593Smuzhiyun * @feed.ts or @feed.sec. 100*4882a593Smuzhiyun * @feed.ts: a &struct dmx_ts_feed pointer. 101*4882a593Smuzhiyun * For TS feed only. 102*4882a593Smuzhiyun * @feed.sec: a &struct dmx_section_feed pointer. 103*4882a593Smuzhiyun * For section feed only. 104*4882a593Smuzhiyun * @cb: a union describing digital TV callbacks. 105*4882a593Smuzhiyun * Depending on the feed type, it can be either 106*4882a593Smuzhiyun * @cb.ts or @cb.sec. 107*4882a593Smuzhiyun * @cb.ts: a dmx_ts_cb() calback function pointer. 108*4882a593Smuzhiyun * For TS feed only. 109*4882a593Smuzhiyun * @cb.sec: a dmx_section_cb() callback function pointer. 110*4882a593Smuzhiyun * For section feed only. 111*4882a593Smuzhiyun * @demux: pointer to &struct dvb_demux. 112*4882a593Smuzhiyun * @priv: private data that can optionally be used by a DVB driver. 113*4882a593Smuzhiyun * @type: type of the filter, as defined by &enum dvb_dmx_filter_type. 114*4882a593Smuzhiyun * @state: state of the filter as defined by &enum dvb_dmx_state. 115*4882a593Smuzhiyun * @pid: PID to be filtered. 116*4882a593Smuzhiyun * @timeout: feed timeout. 117*4882a593Smuzhiyun * @filter: pointer to &struct dvb_demux_filter. 118*4882a593Smuzhiyun * @buffer_flags: Buffer flags used to report discontinuity users via DVB 119*4882a593Smuzhiyun * memory mapped API, as defined by &enum dmx_buffer_flags. 120*4882a593Smuzhiyun * @ts_type: type of TS, as defined by &enum ts_filter_type. 121*4882a593Smuzhiyun * @pes_type: type of PES, as defined by &enum dmx_ts_pes. 122*4882a593Smuzhiyun * @cc: MPEG-TS packet continuity counter 123*4882a593Smuzhiyun * @pusi_seen: if true, indicates that a discontinuity was detected. 124*4882a593Smuzhiyun * it is used to prevent feeding of garbage from previous section. 125*4882a593Smuzhiyun * @peslen: length of the PES (Packet Elementary Stream). 126*4882a593Smuzhiyun * @list_head: head for the list of digital TV demux feeds. 127*4882a593Smuzhiyun * @index: a unique index for each feed. Can be used as hardware 128*4882a593Smuzhiyun * pid filter index. 129*4882a593Smuzhiyun */ 130*4882a593Smuzhiyun struct dvb_demux_feed { 131*4882a593Smuzhiyun union { 132*4882a593Smuzhiyun struct dmx_ts_feed ts; 133*4882a593Smuzhiyun struct dmx_section_feed sec; 134*4882a593Smuzhiyun } feed; 135*4882a593Smuzhiyun 136*4882a593Smuzhiyun union { 137*4882a593Smuzhiyun dmx_ts_cb ts; 138*4882a593Smuzhiyun dmx_section_cb sec; 139*4882a593Smuzhiyun } cb; 140*4882a593Smuzhiyun 141*4882a593Smuzhiyun struct dvb_demux *demux; 142*4882a593Smuzhiyun void *priv; 143*4882a593Smuzhiyun enum dvb_dmx_filter_type type; 144*4882a593Smuzhiyun enum dvb_dmx_state state; 145*4882a593Smuzhiyun u16 pid; 146*4882a593Smuzhiyun 147*4882a593Smuzhiyun ktime_t timeout; 148*4882a593Smuzhiyun struct dvb_demux_filter *filter; 149*4882a593Smuzhiyun 150*4882a593Smuzhiyun u32 buffer_flags; 151*4882a593Smuzhiyun 152*4882a593Smuzhiyun enum ts_filter_type ts_type; 153*4882a593Smuzhiyun enum dmx_ts_pes pes_type; 154*4882a593Smuzhiyun 155*4882a593Smuzhiyun int cc; 156*4882a593Smuzhiyun bool pusi_seen; 157*4882a593Smuzhiyun 158*4882a593Smuzhiyun u16 peslen; 159*4882a593Smuzhiyun 160*4882a593Smuzhiyun struct list_head list_head; 161*4882a593Smuzhiyun unsigned int index; 162*4882a593Smuzhiyun }; 163*4882a593Smuzhiyun 164*4882a593Smuzhiyun /** 165*4882a593Smuzhiyun * struct dvb_demux - represents a digital TV demux 166*4882a593Smuzhiyun * @dmx: embedded &struct dmx_demux with demux capabilities 167*4882a593Smuzhiyun * and callbacks. 168*4882a593Smuzhiyun * @priv: private data that can optionally be used by 169*4882a593Smuzhiyun * a DVB driver. 170*4882a593Smuzhiyun * @filternum: maximum amount of DVB filters. 171*4882a593Smuzhiyun * @feednum: maximum amount of DVB feeds. 172*4882a593Smuzhiyun * @start_feed: callback routine to be called in order to start 173*4882a593Smuzhiyun * a DVB feed. 174*4882a593Smuzhiyun * @stop_feed: callback routine to be called in order to stop 175*4882a593Smuzhiyun * a DVB feed. 176*4882a593Smuzhiyun * @write_to_decoder: callback routine to be called if the feed is TS and 177*4882a593Smuzhiyun * it is routed to an A/V decoder, when a new TS packet 178*4882a593Smuzhiyun * is received. 179*4882a593Smuzhiyun * Used only on av7110-av.c. 180*4882a593Smuzhiyun * @check_crc32: callback routine to check CRC. If not initialized, 181*4882a593Smuzhiyun * dvb_demux will use an internal one. 182*4882a593Smuzhiyun * @memcopy: callback routine to memcopy received data. 183*4882a593Smuzhiyun * If not initialized, dvb_demux will default to memcpy(). 184*4882a593Smuzhiyun * @users: counter for the number of demux opened file descriptors. 185*4882a593Smuzhiyun * Currently, it is limited to 10 users. 186*4882a593Smuzhiyun * @filter: pointer to &struct dvb_demux_filter. 187*4882a593Smuzhiyun * @feed: pointer to &struct dvb_demux_feed. 188*4882a593Smuzhiyun * @frontend_list: &struct list_head with frontends used by the demux. 189*4882a593Smuzhiyun * @pesfilter: array of &struct dvb_demux_feed with the PES types 190*4882a593Smuzhiyun * that will be filtered. 191*4882a593Smuzhiyun * @pids: list of filtered program IDs. 192*4882a593Smuzhiyun * @feed_list: &struct list_head with feeds. 193*4882a593Smuzhiyun * @tsbuf: temporary buffer used internally to store TS packets. 194*4882a593Smuzhiyun * @tsbufp: temporary buffer index used internally. 195*4882a593Smuzhiyun * @mutex: pointer to &struct mutex used to protect feed set 196*4882a593Smuzhiyun * logic. 197*4882a593Smuzhiyun * @lock: pointer to &spinlock_t, used to protect buffer handling. 198*4882a593Smuzhiyun * @cnt_storage: buffer used for TS/TEI continuity check. 199*4882a593Smuzhiyun * @speed_last_time: &ktime_t used for TS speed check. 200*4882a593Smuzhiyun * @speed_pkts_cnt: packets count used for TS speed check. 201*4882a593Smuzhiyun */ 202*4882a593Smuzhiyun struct dvb_demux { 203*4882a593Smuzhiyun struct dmx_demux dmx; 204*4882a593Smuzhiyun void *priv; 205*4882a593Smuzhiyun int filternum; 206*4882a593Smuzhiyun int feednum; 207*4882a593Smuzhiyun int (*start_feed)(struct dvb_demux_feed *feed); 208*4882a593Smuzhiyun int (*stop_feed)(struct dvb_demux_feed *feed); 209*4882a593Smuzhiyun int (*write_to_decoder)(struct dvb_demux_feed *feed, 210*4882a593Smuzhiyun const u8 *buf, size_t len); 211*4882a593Smuzhiyun u32 (*check_crc32)(struct dvb_demux_feed *feed, 212*4882a593Smuzhiyun const u8 *buf, size_t len); 213*4882a593Smuzhiyun void (*memcopy)(struct dvb_demux_feed *feed, u8 *dst, 214*4882a593Smuzhiyun const u8 *src, size_t len); 215*4882a593Smuzhiyun 216*4882a593Smuzhiyun int users; 217*4882a593Smuzhiyun #define MAX_DVB_DEMUX_USERS 10 218*4882a593Smuzhiyun struct dvb_demux_filter *filter; 219*4882a593Smuzhiyun struct dvb_demux_feed *feed; 220*4882a593Smuzhiyun 221*4882a593Smuzhiyun struct list_head frontend_list; 222*4882a593Smuzhiyun 223*4882a593Smuzhiyun struct dvb_demux_feed *pesfilter[DMX_PES_OTHER]; 224*4882a593Smuzhiyun u16 pids[DMX_PES_OTHER]; 225*4882a593Smuzhiyun 226*4882a593Smuzhiyun #define DMX_MAX_PID 0x2000 227*4882a593Smuzhiyun struct list_head feed_list; 228*4882a593Smuzhiyun u8 tsbuf[204]; 229*4882a593Smuzhiyun int tsbufp; 230*4882a593Smuzhiyun 231*4882a593Smuzhiyun struct mutex mutex; 232*4882a593Smuzhiyun spinlock_t lock; 233*4882a593Smuzhiyun 234*4882a593Smuzhiyun uint8_t *cnt_storage; /* for TS continuity check */ 235*4882a593Smuzhiyun 236*4882a593Smuzhiyun ktime_t speed_last_time; /* for TS speed check */ 237*4882a593Smuzhiyun uint32_t speed_pkts_cnt; /* for TS speed check */ 238*4882a593Smuzhiyun 239*4882a593Smuzhiyun /* private: used only on av7110 */ 240*4882a593Smuzhiyun int playing; 241*4882a593Smuzhiyun int recording; 242*4882a593Smuzhiyun }; 243*4882a593Smuzhiyun 244*4882a593Smuzhiyun /** 245*4882a593Smuzhiyun * dvb_dmx_init - initialize a digital TV demux struct. 246*4882a593Smuzhiyun * 247*4882a593Smuzhiyun * @demux: &struct dvb_demux to be initialized. 248*4882a593Smuzhiyun * 249*4882a593Smuzhiyun * Before being able to register a digital TV demux struct, drivers 250*4882a593Smuzhiyun * should call this routine. On its typical usage, some fields should 251*4882a593Smuzhiyun * be initialized at the driver before calling it. 252*4882a593Smuzhiyun * 253*4882a593Smuzhiyun * A typical usecase is:: 254*4882a593Smuzhiyun * 255*4882a593Smuzhiyun * dvb->demux.dmx.capabilities = 256*4882a593Smuzhiyun * DMX_TS_FILTERING | DMX_SECTION_FILTERING | 257*4882a593Smuzhiyun * DMX_MEMORY_BASED_FILTERING; 258*4882a593Smuzhiyun * dvb->demux.priv = dvb; 259*4882a593Smuzhiyun * dvb->demux.filternum = 256; 260*4882a593Smuzhiyun * dvb->demux.feednum = 256; 261*4882a593Smuzhiyun * dvb->demux.start_feed = driver_start_feed; 262*4882a593Smuzhiyun * dvb->demux.stop_feed = driver_stop_feed; 263*4882a593Smuzhiyun * ret = dvb_dmx_init(&dvb->demux); 264*4882a593Smuzhiyun * if (ret < 0) 265*4882a593Smuzhiyun * return ret; 266*4882a593Smuzhiyun */ 267*4882a593Smuzhiyun int dvb_dmx_init(struct dvb_demux *demux); 268*4882a593Smuzhiyun 269*4882a593Smuzhiyun /** 270*4882a593Smuzhiyun * dvb_dmx_release - releases a digital TV demux internal buffers. 271*4882a593Smuzhiyun * 272*4882a593Smuzhiyun * @demux: &struct dvb_demux to be released. 273*4882a593Smuzhiyun * 274*4882a593Smuzhiyun * The DVB core internally allocates data at @demux. This routine 275*4882a593Smuzhiyun * releases those data. Please notice that the struct itelf is not 276*4882a593Smuzhiyun * released, as it can be embedded on other structs. 277*4882a593Smuzhiyun */ 278*4882a593Smuzhiyun void dvb_dmx_release(struct dvb_demux *demux); 279*4882a593Smuzhiyun 280*4882a593Smuzhiyun /** 281*4882a593Smuzhiyun * dvb_dmx_swfilter_packets - use dvb software filter for a buffer with 282*4882a593Smuzhiyun * multiple MPEG-TS packets with 188 bytes each. 283*4882a593Smuzhiyun * 284*4882a593Smuzhiyun * @demux: pointer to &struct dvb_demux 285*4882a593Smuzhiyun * @buf: buffer with data to be filtered 286*4882a593Smuzhiyun * @count: number of MPEG-TS packets with size of 188. 287*4882a593Smuzhiyun * 288*4882a593Smuzhiyun * The routine will discard a DVB packet that don't start with 0x47. 289*4882a593Smuzhiyun * 290*4882a593Smuzhiyun * Use this routine if the DVB demux fills MPEG-TS buffers that are 291*4882a593Smuzhiyun * already aligned. 292*4882a593Smuzhiyun * 293*4882a593Smuzhiyun * NOTE: The @buf size should have size equal to ``count * 188``. 294*4882a593Smuzhiyun */ 295*4882a593Smuzhiyun void dvb_dmx_swfilter_packets(struct dvb_demux *demux, const u8 *buf, 296*4882a593Smuzhiyun size_t count); 297*4882a593Smuzhiyun 298*4882a593Smuzhiyun /** 299*4882a593Smuzhiyun * dvb_dmx_swfilter - use dvb software filter for a buffer with 300*4882a593Smuzhiyun * multiple MPEG-TS packets with 188 bytes each. 301*4882a593Smuzhiyun * 302*4882a593Smuzhiyun * @demux: pointer to &struct dvb_demux 303*4882a593Smuzhiyun * @buf: buffer with data to be filtered 304*4882a593Smuzhiyun * @count: number of MPEG-TS packets with size of 188. 305*4882a593Smuzhiyun * 306*4882a593Smuzhiyun * If a DVB packet doesn't start with 0x47, it will seek for the first 307*4882a593Smuzhiyun * byte that starts with 0x47. 308*4882a593Smuzhiyun * 309*4882a593Smuzhiyun * Use this routine if the DVB demux fill buffers that may not start with 310*4882a593Smuzhiyun * a packet start mark (0x47). 311*4882a593Smuzhiyun * 312*4882a593Smuzhiyun * NOTE: The @buf size should have size equal to ``count * 188``. 313*4882a593Smuzhiyun */ 314*4882a593Smuzhiyun void dvb_dmx_swfilter(struct dvb_demux *demux, const u8 *buf, size_t count); 315*4882a593Smuzhiyun 316*4882a593Smuzhiyun /** 317*4882a593Smuzhiyun * dvb_dmx_swfilter_204 - use dvb software filter for a buffer with 318*4882a593Smuzhiyun * multiple MPEG-TS packets with 204 bytes each. 319*4882a593Smuzhiyun * 320*4882a593Smuzhiyun * @demux: pointer to &struct dvb_demux 321*4882a593Smuzhiyun * @buf: buffer with data to be filtered 322*4882a593Smuzhiyun * @count: number of MPEG-TS packets with size of 204. 323*4882a593Smuzhiyun * 324*4882a593Smuzhiyun * If a DVB packet doesn't start with 0x47, it will seek for the first 325*4882a593Smuzhiyun * byte that starts with 0x47. 326*4882a593Smuzhiyun * 327*4882a593Smuzhiyun * Use this routine if the DVB demux fill buffers that may not start with 328*4882a593Smuzhiyun * a packet start mark (0x47). 329*4882a593Smuzhiyun * 330*4882a593Smuzhiyun * NOTE: The @buf size should have size equal to ``count * 204``. 331*4882a593Smuzhiyun */ 332*4882a593Smuzhiyun void dvb_dmx_swfilter_204(struct dvb_demux *demux, const u8 *buf, 333*4882a593Smuzhiyun size_t count); 334*4882a593Smuzhiyun 335*4882a593Smuzhiyun /** 336*4882a593Smuzhiyun * dvb_dmx_swfilter_raw - make the raw data available to userspace without 337*4882a593Smuzhiyun * filtering 338*4882a593Smuzhiyun * 339*4882a593Smuzhiyun * @demux: pointer to &struct dvb_demux 340*4882a593Smuzhiyun * @buf: buffer with data 341*4882a593Smuzhiyun * @count: number of packets to be passed. The actual size of each packet 342*4882a593Smuzhiyun * depends on the &dvb_demux->feed->cb.ts logic. 343*4882a593Smuzhiyun * 344*4882a593Smuzhiyun * Use it if the driver needs to deliver the raw payload to userspace without 345*4882a593Smuzhiyun * passing through the kernel demux. That is meant to support some 346*4882a593Smuzhiyun * delivery systems that aren't based on MPEG-TS. 347*4882a593Smuzhiyun * 348*4882a593Smuzhiyun * This function relies on &dvb_demux->feed->cb.ts to actually handle the 349*4882a593Smuzhiyun * buffer. 350*4882a593Smuzhiyun */ 351*4882a593Smuzhiyun void dvb_dmx_swfilter_raw(struct dvb_demux *demux, const u8 *buf, 352*4882a593Smuzhiyun size_t count); 353*4882a593Smuzhiyun 354*4882a593Smuzhiyun #endif /* _DVB_DEMUX_H_ */ 355