1 /* 2 * demux.h 3 * 4 * The Kernel Digital TV Demux kABI defines a driver-internal interface for 5 * registering low-level, hardware specific driver to a hardware independent 6 * demux layer. 7 * 8 * Copyright (c) 2002 Convergence GmbH 9 * 10 * based on code: 11 * Copyright (c) 2000 Nokia Research Center 12 * Tampere, FINLAND 13 * 14 * This program is free software; you can redistribute it and/or 15 * modify it under the terms of the GNU Lesser General Public License 16 * as published by the Free Software Foundation; either version 2.1 17 * of the License, or (at your option) any later version. 18 * 19 * This program is distributed in the hope that it will be useful, 20 * but WITHOUT ANY WARRANTY; without even the implied warranty of 21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 * GNU General Public License for more details. 23 * 24 * You should have received a copy of the GNU Lesser General Public License 25 * along with this program; if not, write to the Free Software 26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 27 * 28 */ 29 30 #ifndef __DEMUX_H 31 #define __DEMUX_H 32 33 #include <linux/types.h> 34 #include <linux/errno.h> 35 #include <linux/list.h> 36 #include <linux/time.h> 37 #include <linux/dvb/dmx.h> 38 39 /* 40 * Common definitions 41 */ 42 43 /* 44 * DMX_MAX_FILTER_SIZE: Maximum length (in bytes) of a section/PES filter. 45 */ 46 47 #ifndef DMX_MAX_FILTER_SIZE 48 #define DMX_MAX_FILTER_SIZE 18 49 #endif 50 51 /* 52 * DMX_MAX_SECFEED_SIZE: Maximum length (in bytes) of a private section feed 53 * filter. 54 */ 55 56 #ifndef DMX_MAX_SECTION_SIZE 57 #define DMX_MAX_SECTION_SIZE 4096 58 #endif 59 #ifndef DMX_MAX_SECFEED_SIZE 60 #define DMX_MAX_SECFEED_SIZE (DMX_MAX_SECTION_SIZE + 188) 61 #endif 62 63 /* 64 * TS packet reception 65 */ 66 67 /** 68 * enum ts_filter_type - filter type bitmap for dmx_ts_feed.set\(\) 69 * 70 * @TS_PACKET: Send TS packets (188 bytes) to callback (default). 71 * @TS_PAYLOAD_ONLY: In case TS_PACKET is set, only send the TS payload 72 * (<=184 bytes per packet) to callback 73 * @TS_DECODER: Send stream to built-in decoder (if present). 74 * @TS_DEMUX: In case TS_PACKET is set, send the TS to the demux 75 * device, not to the dvr device 76 */ 77 enum ts_filter_type { 78 TS_PACKET = 1, 79 TS_PAYLOAD_ONLY = 2, 80 TS_DECODER = 4, 81 TS_DEMUX = 8, 82 }; 83 84 /** 85 * struct dmx_ts_feed - Structure that contains a TS feed filter 86 * 87 * @is_filtering: Set to non-zero when filtering in progress 88 * @parent: pointer to struct dmx_demux 89 * @priv: pointer to private data of the API client 90 * @set: sets the TS filter 91 * @start_filtering: starts TS filtering 92 * @stop_filtering: stops TS filtering 93 * 94 * A TS feed is typically mapped to a hardware PID filter on the demux chip. 95 * Using this API, the client can set the filtering properties to start/stop 96 * filtering TS packets on a particular TS feed. 97 */ 98 struct dmx_ts_feed { 99 int is_filtering; 100 struct dmx_demux *parent; 101 void *priv; 102 int (*set)(struct dmx_ts_feed *feed, 103 u16 pid, 104 int type, 105 enum dmx_ts_pes pes_type, 106 size_t circular_buffer_size, 107 ktime_t timeout); 108 int (*start_filtering)(struct dmx_ts_feed *feed); 109 int (*stop_filtering)(struct dmx_ts_feed *feed); 110 }; 111 112 /* 113 * Section reception 114 */ 115 116 /** 117 * struct dmx_section_filter - Structure that describes a section filter 118 * 119 * @filter_value: Contains up to 16 bytes (128 bits) of the TS section header 120 * that will be matched by the section filter 121 * @filter_mask: Contains a 16 bytes (128 bits) filter mask with the bits 122 * specified by @filter_value that will be used on the filter 123 * match logic. 124 * @filter_mode: Contains a 16 bytes (128 bits) filter mode. 125 * @parent: Pointer to struct dmx_section_feed. 126 * @priv: Pointer to private data of the API client. 127 * 128 * 129 * The @filter_mask controls which bits of @filter_value are compared with 130 * the section headers/payload. On a binary value of 1 in filter_mask, the 131 * corresponding bits are compared. The filter only accepts sections that are 132 * equal to filter_value in all the tested bit positions. 133 */ 134 struct dmx_section_filter { 135 u8 filter_value[DMX_MAX_FILTER_SIZE]; 136 u8 filter_mask[DMX_MAX_FILTER_SIZE]; 137 u8 filter_mode[DMX_MAX_FILTER_SIZE]; 138 struct dmx_section_feed *parent; /* Back-pointer */ 139 void *priv; /* Pointer to private data of the API client */ 140 }; 141 142 /** 143 * struct dmx_section_feed - Structure that contains a section feed filter 144 * 145 * @is_filtering: Set to non-zero when filtering in progress 146 * @parent: pointer to struct dmx_demux 147 * @priv: pointer to private data of the API client 148 * @check_crc: If non-zero, check the CRC values of filtered sections. 149 * @set: sets the section filter 150 * @allocate_filter: This function is used to allocate a section filter on 151 * the demux. It should only be called when no filtering 152 * is in progress on this section feed. If a filter cannot 153 * be allocated, the function fails with -ENOSPC. 154 * @release_filter: This function releases all the resources of a 155 * previously allocated section filter. The function 156 * should not be called while filtering is in progress 157 * on this section feed. After calling this function, 158 * the caller should not try to dereference the filter 159 * pointer. 160 * @start_filtering: starts section filtering 161 * @stop_filtering: stops section filtering 162 * 163 * A TS feed is typically mapped to a hardware PID filter on the demux chip. 164 * Using this API, the client can set the filtering properties to start/stop 165 * filtering TS packets on a particular TS feed. 166 */ 167 struct dmx_section_feed { 168 int is_filtering; 169 struct dmx_demux *parent; 170 void *priv; 171 172 int check_crc; 173 174 /* private: Used internally at dvb_demux.c */ 175 u32 crc_val; 176 177 u8 *secbuf; 178 u8 secbuf_base[DMX_MAX_SECFEED_SIZE]; 179 u16 secbufp, seclen, tsfeedp; 180 181 /* public: */ 182 int (*set)(struct dmx_section_feed *feed, 183 u16 pid, 184 size_t circular_buffer_size, 185 int check_crc); 186 int (*allocate_filter)(struct dmx_section_feed *feed, 187 struct dmx_section_filter **filter); 188 int (*release_filter)(struct dmx_section_feed *feed, 189 struct dmx_section_filter *filter); 190 int (*start_filtering)(struct dmx_section_feed *feed); 191 int (*stop_filtering)(struct dmx_section_feed *feed); 192 }; 193 194 /** 195 * typedef dmx_ts_cb - DVB demux TS filter callback function prototype 196 * 197 * @buffer1: Pointer to the start of the filtered TS packets. 198 * @buffer1_length: Length of the TS data in buffer1. 199 * @buffer2: Pointer to the tail of the filtered TS packets, or NULL. 200 * @buffer2_length: Length of the TS data in buffer2. 201 * @source: Indicates which TS feed is the source of the callback. 202 * 203 * This function callback prototype, provided by the client of the demux API, 204 * is called from the demux code. The function is only called when filtering 205 * on a TS feed has been enabled using the start_filtering\(\) function at 206 * the &dmx_demux. 207 * Any TS packets that match the filter settings are copied to a circular 208 * buffer. The filtered TS packets are delivered to the client using this 209 * callback function. The size of the circular buffer is controlled by the 210 * circular_buffer_size parameter of the &dmx_ts_feed.@set function. 211 * It is expected that the @buffer1 and @buffer2 callback parameters point to 212 * addresses within the circular buffer, but other implementations are also 213 * possible. Note that the called party should not try to free the memory 214 * the @buffer1 and @buffer2 parameters point to. 215 * 216 * When this function is called, the @buffer1 parameter typically points to 217 * the start of the first undelivered TS packet within a circular buffer. 218 * The @buffer2 buffer parameter is normally NULL, except when the received 219 * TS packets have crossed the last address of the circular buffer and 220 * ”wrapped” to the beginning of the buffer. In the latter case the @buffer1 221 * parameter would contain an address within the circular buffer, while the 222 * @buffer2 parameter would contain the first address of the circular buffer. 223 * The number of bytes delivered with this function (i.e. @buffer1_length + 224 * @buffer2_length) is usually equal to the value of callback_length parameter 225 * given in the set() function, with one exception: if a timeout occurs before 226 * receiving callback_length bytes of TS data, any undelivered packets are 227 * immediately delivered to the client by calling this function. The timeout 228 * duration is controlled by the set() function in the TS Feed API. 229 * 230 * If a TS packet is received with errors that could not be fixed by the 231 * TS-level forward error correction (FEC), the Transport_error_indicator 232 * flag of the TS packet header should be set. The TS packet should not be 233 * discarded, as the error can possibly be corrected by a higher layer 234 * protocol. If the called party is slow in processing the callback, it 235 * is possible that the circular buffer eventually fills up. If this happens, 236 * the demux driver should discard any TS packets received while the buffer 237 * is full and return -EOVERFLOW. 238 * 239 * The type of data returned to the callback can be selected by the 240 * &dmx_ts_feed.@set function. The type parameter decides if the raw 241 * TS packet (TS_PACKET) or just the payload (TS_PACKET|TS_PAYLOAD_ONLY) 242 * should be returned. If additionally the TS_DECODER bit is set the stream 243 * will also be sent to the hardware MPEG decoder. 244 * 245 * Return: 246 * 247 * - 0, on success; 248 * 249 * - -EOVERFLOW, on buffer overflow. 250 */ 251 typedef int (*dmx_ts_cb)(const u8 *buffer1, 252 size_t buffer1_length, 253 const u8 *buffer2, 254 size_t buffer2_length, 255 struct dmx_ts_feed *source); 256 257 /** 258 * typedef dmx_section_cb - DVB demux TS filter callback function prototype 259 * 260 * @buffer1: Pointer to the start of the filtered section, e.g. 261 * within the circular buffer of the demux driver. 262 * @buffer1_len: Length of the filtered section data in @buffer1, 263 * including headers and CRC. 264 * @buffer2: Pointer to the tail of the filtered section data, 265 * or NULL. Useful to handle the wrapping of a 266 * circular buffer. 267 * @buffer2_len: Length of the filtered section data in @buffer2, 268 * including headers and CRC. 269 * @source: Indicates which section feed is the source of the 270 * callback. 271 * 272 * This function callback prototype, provided by the client of the demux API, 273 * is called from the demux code. The function is only called when 274 * filtering of sections has been enabled using the function 275 * &dmx_ts_feed.@start_filtering. When the demux driver has received a 276 * complete section that matches at least one section filter, the client 277 * is notified via this callback function. Normally this function is called 278 * for each received section; however, it is also possible to deliver 279 * multiple sections with one callback, for example when the system load 280 * is high. If an error occurs while receiving a section, this 281 * function should be called with the corresponding error type set in the 282 * success field, whether or not there is data to deliver. The Section Feed 283 * implementation should maintain a circular buffer for received sections. 284 * However, this is not necessary if the Section Feed API is implemented as 285 * a client of the TS Feed API, because the TS Feed implementation then 286 * buffers the received data. The size of the circular buffer can be 287 * configured using the &dmx_ts_feed.@set function in the Section Feed API. 288 * If there is no room in the circular buffer when a new section is received, 289 * the section must be discarded. If this happens, the value of the success 290 * parameter should be DMX_OVERRUN_ERROR on the next callback. 291 */ 292 typedef int (*dmx_section_cb)(const u8 *buffer1, 293 size_t buffer1_len, 294 const u8 *buffer2, 295 size_t buffer2_len, 296 struct dmx_section_filter *source); 297 298 /* 299 * DVB Front-End 300 */ 301 302 /** 303 * enum dmx_frontend_source - Used to identify the type of frontend 304 * 305 * @DMX_MEMORY_FE: The source of the demux is memory. It means that 306 * the MPEG-TS to be filtered comes from userspace, 307 * via write() syscall. 308 * 309 * @DMX_FRONTEND_0: The source of the demux is a frontend connected 310 * to the demux. 311 */ 312 enum dmx_frontend_source { 313 DMX_MEMORY_FE, 314 DMX_FRONTEND_0, 315 }; 316 317 /** 318 * struct dmx_frontend - Structure that lists the frontends associated with 319 * a demux 320 * 321 * @connectivity_list: List of front-ends that can be connected to a 322 * particular demux; 323 * @source: Type of the frontend. 324 * 325 * FIXME: this structure should likely be replaced soon by some 326 * media-controller based logic. 327 */ 328 struct dmx_frontend { 329 struct list_head connectivity_list; 330 enum dmx_frontend_source source; 331 }; 332 333 /* 334 * MPEG-2 TS Demux 335 */ 336 337 /** 338 * enum dmx_demux_caps - MPEG-2 TS Demux capabilities bitmap 339 * 340 * @DMX_TS_FILTERING: set if TS filtering is supported; 341 * @DMX_SECTION_FILTERING: set if section filtering is supported; 342 * @DMX_MEMORY_BASED_FILTERING: set if write() available. 343 * 344 * Those flags are OR'ed in the &dmx_demux.capabilities field 345 */ 346 enum dmx_demux_caps { 347 DMX_TS_FILTERING = 1, 348 DMX_SECTION_FILTERING = 4, 349 DMX_MEMORY_BASED_FILTERING = 8, 350 }; 351 352 /* 353 * Demux resource type identifier. 354 */ 355 356 /** 357 * DMX_FE_ENTRY - Casts elements in the list of registered 358 * front-ends from the generic type struct list_head 359 * to the type * struct dmx_frontend 360 * 361 * @list: list of struct dmx_frontend 362 */ 363 #define DMX_FE_ENTRY(list) \ 364 list_entry(list, struct dmx_frontend, connectivity_list) 365 366 /** 367 * struct dmx_demux - Structure that contains the demux capabilities and 368 * callbacks. 369 * 370 * @capabilities: Bitfield of capability flags. 371 * 372 * @frontend: Front-end connected to the demux 373 * 374 * @priv: Pointer to private data of the API client 375 * 376 * @open: This function reserves the demux for use by the caller and, if 377 * necessary, initializes the demux. When the demux is no longer needed, 378 * the function @close should be called. It should be possible for 379 * multiple clients to access the demux at the same time. Thus, the 380 * function implementation should increment the demux usage count when 381 * @open is called and decrement it when @close is called. 382 * The @demux function parameter contains a pointer to the demux API and 383 * instance data. 384 * It returns: 385 * 0 on success; 386 * -EUSERS, if maximum usage count was reached; 387 * -EINVAL, on bad parameter. 388 * 389 * @close: This function reserves the demux for use by the caller and, if 390 * necessary, initializes the demux. When the demux is no longer needed, 391 * the function @close should be called. It should be possible for 392 * multiple clients to access the demux at the same time. Thus, the 393 * function implementation should increment the demux usage count when 394 * @open is called and decrement it when @close is called. 395 * The @demux function parameter contains a pointer to the demux API and 396 * instance data. 397 * It returns: 398 * 0 on success; 399 * -ENODEV, if demux was not in use (e. g. no users); 400 * -EINVAL, on bad parameter. 401 * 402 * @write: This function provides the demux driver with a memory buffer 403 * containing TS packets. Instead of receiving TS packets from the DVB 404 * front-end, the demux driver software will read packets from memory. 405 * Any clients of this demux with active TS, PES or Section filters will 406 * receive filtered data via the Demux callback API (see 0). The function 407 * returns when all the data in the buffer has been consumed by the demux. 408 * Demux hardware typically cannot read TS from memory. If this is the 409 * case, memory-based filtering has to be implemented entirely in software. 410 * The @demux function parameter contains a pointer to the demux API and 411 * instance data. 412 * The @buf function parameter contains a pointer to the TS data in 413 * kernel-space memory. 414 * The @count function parameter contains the length of the TS data. 415 * It returns: 416 * 0 on success; 417 * -ERESTARTSYS, if mutex lock was interrupted; 418 * -EINTR, if a signal handling is pending; 419 * -ENODEV, if demux was removed; 420 * -EINVAL, on bad parameter. 421 * 422 * @allocate_ts_feed: Allocates a new TS feed, which is used to filter the TS 423 * packets carrying a certain PID. The TS feed normally corresponds to a 424 * hardware PID filter on the demux chip. 425 * The @demux function parameter contains a pointer to the demux API and 426 * instance data. 427 * The @feed function parameter contains a pointer to the TS feed API and 428 * instance data. 429 * The @callback function parameter contains a pointer to the callback 430 * function for passing received TS packet. 431 * It returns: 432 * 0 on success; 433 * -ERESTARTSYS, if mutex lock was interrupted; 434 * -EBUSY, if no more TS feeds is available; 435 * -EINVAL, on bad parameter. 436 * 437 * @release_ts_feed: Releases the resources allocated with @allocate_ts_feed. 438 * Any filtering in progress on the TS feed should be stopped before 439 * calling this function. 440 * The @demux function parameter contains a pointer to the demux API and 441 * instance data. 442 * The @feed function parameter contains a pointer to the TS feed API and 443 * instance data. 444 * It returns: 445 * 0 on success; 446 * -EINVAL on bad parameter. 447 * 448 * @allocate_section_feed: Allocates a new section feed, i.e. a demux resource 449 * for filtering and receiving sections. On platforms with hardware 450 * support for section filtering, a section feed is directly mapped to 451 * the demux HW. On other platforms, TS packets are first PID filtered in 452 * hardware and a hardware section filter then emulated in software. The 453 * caller obtains an API pointer of type dmx_section_feed_t as an out 454 * parameter. Using this API the caller can set filtering parameters and 455 * start receiving sections. 456 * The @demux function parameter contains a pointer to the demux API and 457 * instance data. 458 * The @feed function parameter contains a pointer to the TS feed API and 459 * instance data. 460 * The @callback function parameter contains a pointer to the callback 461 * function for passing received TS packet. 462 * It returns: 463 * 0 on success; 464 * -EBUSY, if no more TS feeds is available; 465 * -EINVAL, on bad parameter. 466 * 467 * @release_section_feed: Releases the resources allocated with 468 * @allocate_section_feed, including allocated filters. Any filtering in 469 * progress on the section feed should be stopped before calling this 470 * function. 471 * The @demux function parameter contains a pointer to the demux API and 472 * instance data. 473 * The @feed function parameter contains a pointer to the TS feed API and 474 * instance data. 475 * It returns: 476 * 0 on success; 477 * -EINVAL, on bad parameter. 478 * 479 * @add_frontend: Registers a connectivity between a demux and a front-end, 480 * i.e., indicates that the demux can be connected via a call to 481 * @connect_frontend to use the given front-end as a TS source. The 482 * client of this function has to allocate dynamic or static memory for 483 * the frontend structure and initialize its fields before calling this 484 * function. This function is normally called during the driver 485 * initialization. The caller must not free the memory of the frontend 486 * struct before successfully calling @remove_frontend. 487 * The @demux function parameter contains a pointer to the demux API and 488 * instance data. 489 * The @frontend function parameter contains a pointer to the front-end 490 * instance data. 491 * It returns: 492 * 0 on success; 493 * -EINVAL, on bad parameter. 494 * 495 * @remove_frontend: Indicates that the given front-end, registered by a call 496 * to @add_frontend, can no longer be connected as a TS source by this 497 * demux. The function should be called when a front-end driver or a demux 498 * driver is removed from the system. If the front-end is in use, the 499 * function fails with the return value of -EBUSY. After successfully 500 * calling this function, the caller can free the memory of the frontend 501 * struct if it was dynamically allocated before the @add_frontend 502 * operation. 503 * The @demux function parameter contains a pointer to the demux API and 504 * instance data. 505 * The @frontend function parameter contains a pointer to the front-end 506 * instance data. 507 * It returns: 508 * 0 on success; 509 * -ENODEV, if the front-end was not found, 510 * -EINVAL, on bad parameter. 511 * 512 * @get_frontends: Provides the APIs of the front-ends that have been 513 * registered for this demux. Any of the front-ends obtained with this 514 * call can be used as a parameter for @connect_frontend. The include 515 * file demux.h contains the macro DMX_FE_ENTRY() for converting an 516 * element of the generic type struct &list_head * to the type 517 * struct &dmx_frontend *. The caller must not free the memory of any of 518 * the elements obtained via this function call. 519 * The @demux function parameter contains a pointer to the demux API and 520 * instance data. 521 * It returns a struct list_head pointer to the list of front-end 522 * interfaces, or NULL in the case of an empty list. 523 * 524 * @connect_frontend: Connects the TS output of the front-end to the input of 525 * the demux. A demux can only be connected to a front-end registered to 526 * the demux with the function @add_frontend. It may or may not be 527 * possible to connect multiple demuxes to the same front-end, depending 528 * on the capabilities of the HW platform. When not used, the front-end 529 * should be released by calling @disconnect_frontend. 530 * The @demux function parameter contains a pointer to the demux API and 531 * instance data. 532 * The @frontend function parameter contains a pointer to the front-end 533 * instance data. 534 * It returns: 535 * 0 on success; 536 * -EINVAL, on bad parameter. 537 * 538 * @disconnect_frontend: Disconnects the demux and a front-end previously 539 * connected by a @connect_frontend call. 540 * The @demux function parameter contains a pointer to the demux API and 541 * instance data. 542 * It returns: 543 * 0 on success; 544 * -EINVAL on bad parameter. 545 * 546 * @get_pes_pids: Get the PIDs for DMX_PES_AUDIO0, DMX_PES_VIDEO0, 547 * DMX_PES_TELETEXT0, DMX_PES_SUBTITLE0 and DMX_PES_PCR0. 548 * The @demux function parameter contains a pointer to the demux API and 549 * instance data. 550 * The @pids function parameter contains an array with five u16 elements 551 * where the PIDs will be stored. 552 * It returns: 553 * 0 on success; 554 * -EINVAL on bad parameter. 555 */ 556 struct dmx_demux { 557 enum dmx_demux_caps capabilities; 558 struct dmx_frontend *frontend; 559 void *priv; 560 int (*open)(struct dmx_demux *demux); 561 int (*close)(struct dmx_demux *demux); 562 int (*write)(struct dmx_demux *demux, const char __user *buf, 563 size_t count); 564 int (*allocate_ts_feed)(struct dmx_demux *demux, 565 struct dmx_ts_feed **feed, 566 dmx_ts_cb callback); 567 int (*release_ts_feed)(struct dmx_demux *demux, 568 struct dmx_ts_feed *feed); 569 int (*allocate_section_feed)(struct dmx_demux *demux, 570 struct dmx_section_feed **feed, 571 dmx_section_cb callback); 572 int (*release_section_feed)(struct dmx_demux *demux, 573 struct dmx_section_feed *feed); 574 int (*add_frontend)(struct dmx_demux *demux, 575 struct dmx_frontend *frontend); 576 int (*remove_frontend)(struct dmx_demux *demux, 577 struct dmx_frontend *frontend); 578 struct list_head *(*get_frontends)(struct dmx_demux *demux); 579 int (*connect_frontend)(struct dmx_demux *demux, 580 struct dmx_frontend *frontend); 581 int (*disconnect_frontend)(struct dmx_demux *demux); 582 583 int (*get_pes_pids)(struct dmx_demux *demux, u16 *pids); 584 585 /* private: */ 586 587 /* 588 * Only used at av7110, to read some data from firmware. 589 * As this was never documented, we have no clue about what's 590 * there, and its usage on other drivers aren't encouraged. 591 */ 592 int (*get_stc)(struct dmx_demux *demux, unsigned int num, 593 u64 *stc, unsigned int *base); 594 }; 595 596 #endif /* #ifndef __DEMUX_H */ 597