1 /*
2  *  Tvheadend - Linux DVB private data
3  *
4  *  Copyright (C) 2013 Adam Sutton
5  *
6  *  This program is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef __TVH_LINUXDVB_PRIVATE_H__
21 #define __TVH_LINUXDVB_PRIVATE_H__
22 
23 #include "input.h"
24 
25 #if ENABLE_LINUXDVB
26 #include <linux/dvb/version.h>
27 
28 #if ENABLE_LIBDVBEN50221
29 #include <libdvben50221/en50221_session.h>
30 #include <libdvben50221/en50221_app_utils.h>
31 #include <libdvben50221/en50221_app_ai.h>
32 #include <libdvben50221/en50221_app_rm.h>
33 #include <libdvben50221/en50221_app_ca.h>
34 #include <libdvben50221/en50221_app_dvb.h>
35 #include <libdvben50221/en50221_app_datetime.h>
36 #include <libdvben50221/en50221_app_smartcard.h>
37 #include <libdvben50221/en50221_app_teletext.h>
38 #include <libdvben50221/en50221_app_mmi.h>
39 #include <libdvben50221/en50221_app_epg.h>
40 #include <libdvben50221/en50221_app_auth.h>
41 #include <libdvben50221/en50221_app_lowspeed.h>
42 #include <libdvbapi/dvbca.h>
43 #endif
44 #endif
45 
46 #define DVB_VER_INT(maj,min) (((maj) << 16) + (min))
47 
48 #define DVB_VER_ATLEAST(maj, min) \
49  (DVB_VER_INT(DVB_API_VERSION,  DVB_API_VERSION_MINOR) >= DVB_VER_INT(maj, min))
50 
51 typedef struct linuxdvb_hardware    linuxdvb_hardware_t;
52 typedef struct linuxdvb_adapter     linuxdvb_adapter_t;
53 typedef struct linuxdvb_frontend    linuxdvb_frontend_t;
54 #if ENABLE_LINUXDVB_CA
55 typedef struct linuxdvb_ca          linuxdvb_ca_t;
56 typedef struct linuxdvb_ca_capmt    linuxdvb_ca_capmt_t;
57 #endif
58 typedef struct linuxdvb_satconf     linuxdvb_satconf_t;
59 typedef struct linuxdvb_satconf_ele linuxdvb_satconf_ele_t;
60 typedef struct linuxdvb_diseqc      linuxdvb_diseqc_t;
61 typedef struct linuxdvb_lnb         linuxdvb_lnb_t;
62 typedef struct linuxdvb_network     linuxdvb_network_t;
63 typedef struct linuxdvb_en50494     linuxdvb_en50494_t;
64 
65 typedef LIST_HEAD(,linuxdvb_hardware) linuxdvb_hardware_list_t;
66 typedef TAILQ_HEAD(linuxdvb_satconf_ele_list,linuxdvb_satconf_ele) linuxdvb_satconf_ele_list_t;
67 #if ENABLE_LINUXDVB_CA
68 typedef TAILQ_HEAD(linuxdvb_ca_capmt_queue,linuxdvb_ca_capmt) linuxdvb_ca_capmt_queue_t;
69 #endif
70 
71 struct linuxdvb_adapter
72 {
73   tvh_hardware_t;
74 
75   /*
76    * Adapter info
77    */
78   char    *la_name;
79   char    *la_rootpath;
80   int      la_dvb_number;
81   int      la_exclusive; /* one frontend at a time */
82 
83   /*
84    * Frontends
85    */
86   LIST_HEAD(,linuxdvb_frontend) la_frontends;
87 
88   /*
89    *  CA devices
90    */
91 #if ENABLE_LINUXDVB_CA
92   LIST_HEAD(,linuxdvb_ca) la_ca_devices;
93 #endif
94   /*
95   * Functions
96   */
97   int (*la_is_enabled) ( linuxdvb_adapter_t *la );
98 };
99 
100 struct linuxdvb_frontend
101 {
102   mpegts_input_t;
103 
104   /*
105    * Adapter
106    */
107   linuxdvb_adapter_t           *lfe_adapter;
108   LIST_ENTRY(linuxdvb_frontend) lfe_link;
109 
110   /*
111    * Frontend info
112    */
113   int                       lfe_number;
114   dvb_fe_type_t             lfe_type;
115   char                      lfe_name[128];
116   char                     *lfe_fe_path;
117   char                     *lfe_dmx_path;
118   char                     *lfe_dvr_path;
119   char                     *lfe_sysfs;
120 
121   /*
122    * Reception
123    */
124   int                       lfe_fe_fd;
125   pthread_t                 lfe_dvr_thread;
126   th_pipe_t                 lfe_dvr_pipe;
127   pthread_mutex_t           lfe_dvr_lock;
128   tvh_cond_t                lfe_dvr_cond;
129   mpegts_apids_t            lfe_pids;
130   int                       lfe_pids_max;
131   int                       lfe_pids_use_all;
132 
133   /*
134    * Tuning
135    */
136   int                       lfe_refcount;
137   int                       lfe_ready;
138   int                       lfe_in_setup;
139   int                       lfe_locked;
140   int                       lfe_status;
141   int                       lfe_status2;
142   int                       lfe_ioctls;
143   int                       lfe_nodata;
144   int                       lfe_freq;
145   time_t                    lfe_monitor;
146   mtimer_t                  lfe_monitor_timer;
147   tvhlog_limit_t            lfe_status_log;
148 
149   /*
150    * Configuration
151    */
152   char                     *lfe_master;
153   int                       lfe_powersave;
154   int                       lfe_tune_repeats;
155   uint32_t                  lfe_skip_bytes;
156   uint32_t                  lfe_ibuf_size;
157   uint32_t                  lfe_status_period;
158   int                       lfe_old_status;
159   int                       lfe_lna;
160 
161   /*
162    * Satconf (DVB-S only)
163    */
164   linuxdvb_satconf_t       *lfe_satconf;
165 };
166 
167 #if ENABLE_LINUXDVB_CA
168 struct linuxdvb_ca
169 {
170   idnode_t                     lca_id;
171   /*
172    * Adapter
173    */
174   linuxdvb_adapter_t          *lca_adapter;
175   LIST_ENTRY(linuxdvb_ca)      lca_link;
176 
177   /*
178    * CA handling
179    */
180   int                       lca_ca_fd;
181   int                       lca_enabled;
182   int                       lca_high_bitrate_mode;
183   int                       lca_capmt_query;
184   mtimer_t                  lca_monitor_timer;
185   mtimer_t                  lca_capmt_queue_timer;
186   int                       lca_capmt_interval;
187   int                       lca_capmt_query_interval;
188   pthread_t                 lca_en50221_thread;
189   int                       lca_en50221_thread_running;
190 
191   /*
192    * EN50221
193    */
194   struct en50221_transport_layer    *lca_tl;
195   struct en50221_session_layer      *lca_sl;
196   struct en50221_app_send_functions  lca_sf;
197   struct en50221_app_rm             *lca_rm_resource;
198   struct en50221_app_ai             *lca_ai_resource;
199   struct en50221_app_ca             *lca_ca_resource;
200   struct en50221_app_datetime       *lca_dt_resource;
201   struct en50221_app_mmi            *lca_mmi_resource;
202   int                                lca_tc;
203   int                                lca_ai_version;
204   uint16_t                           lca_ai_session_number;
205   uint16_t                           lca_ca_session_number;
206 
207   /*
208    * CA info
209    */
210   int                      lca_number;
211   char                     lca_name[128];
212   char                     *lca_ca_path;
213   int                      lca_state;
214   const char               *lca_state_str;
215   linuxdvb_ca_capmt_queue_t lca_capmt_queue;
216   /*
217    * CAM module info
218    */
219   char                     lca_cam_menu_string[64];
220   int                      lca_pin_reply;
221   char                    *lca_pin_str;
222   char                    *lca_pin_match_str;
223 };
224 #endif
225 
226 struct linuxdvb_satconf
227 {
228   idnode_t              ls_id;
229   const char           *ls_type;
230 
231   /*
232    * MPEG-TS hooks
233    */
234   mpegts_input_t        *ls_frontend; ///< Frontend we're proxying for
235   mpegts_mux_instance_t *ls_mmi;      ///< Used within delay diseqc handler
236 
237   /*
238    * Tuning
239    */
240   int                    ls_early_tune;
241 
242   /*
243    * Diseqc handling
244    */
245   mtimer_t               ls_diseqc_timer;
246   int                    ls_diseqc_idx;
247   int                    ls_diseqc_repeats;
248   int                    ls_diseqc_full;
249   int                    ls_switch_rotor;
250 
251   /*
252    * LNB settings
253    */
254   int                    ls_lnb_poweroff;
255   int                    ls_lnb_highvol;
256   uint32_t               ls_max_rotor_move;
257   uint32_t               ls_min_rotor_move;
258   double                 ls_site_lat;
259   double                 ls_site_lon;
260   uint32_t               ls_motor_rate;
261   int                    ls_site_lat_south;
262   int                    ls_site_lon_west;
263   int                    ls_site_altitude;
264 
265   /*
266    * Satconf elements
267    */
268   linuxdvb_satconf_ele_list_t ls_elements;
269   linuxdvb_satconf_ele_t *ls_last_switch;
270   int                    ls_last_switch_pol;
271   int                    ls_last_switch_band;
272   int                    ls_last_vol;
273   int                    ls_last_toneburst;
274   int                    ls_last_tone_off;
275   int                    ls_last_orbital_pos;
276 };
277 
278 /*
279  * Elementary satconf entry
280  */
281 struct linuxdvb_satconf_ele
282 {
283   idnode_t               lse_id;
284   /*
285    * Parent
286    */
287   linuxdvb_satconf_t               *lse_parent;
288   TAILQ_ENTRY(linuxdvb_satconf_ele)  lse_link;
289 
290   /*
291    * Config
292    */
293   int                    lse_enabled;
294   int                    lse_priority;
295   char                  *lse_name;
296 
297   /*
298    * Assigned networks to this SAT configuration
299    */
300   idnode_set_t          *lse_networks;
301 
302   /*
303    * Diseqc kit
304    */
305   linuxdvb_lnb_t        *lse_lnb;
306   linuxdvb_diseqc_t     *lse_switch;
307   linuxdvb_diseqc_t     *lse_rotor;
308   linuxdvb_diseqc_t     *lse_en50494;
309 };
310 
311 struct linuxdvb_diseqc
312 {
313   idnode_t              ld_id;
314   const char           *ld_type;
315   linuxdvb_satconf_ele_t   *ld_satconf;
316   int (*ld_grace) (linuxdvb_diseqc_t *ld, dvb_mux_t *lm);
317   int (*ld_freq)  (linuxdvb_diseqc_t *ld, dvb_mux_t *lm, int freq);
318   int (*ld_match) (linuxdvb_diseqc_t *ld, dvb_mux_t *lm1, dvb_mux_t *lm2);
319   int (*ld_tune)  (linuxdvb_diseqc_t *ld, dvb_mux_t *lm,
320                    linuxdvb_satconf_t *lsp, linuxdvb_satconf_ele_t *ls,
321                    int vol, int pol, int band, int freq);
322   int (*ld_post)  (linuxdvb_diseqc_t *ld, dvb_mux_t *lm,
323                    linuxdvb_satconf_t *lsp, linuxdvb_satconf_ele_t *ls);
324 };
325 
326 struct linuxdvb_lnb
327 {
328   linuxdvb_diseqc_t;
329   uint32_t  (*lnb_freq) (linuxdvb_lnb_t*, dvb_mux_t*);
330   int       (*lnb_match)(linuxdvb_lnb_t*, dvb_mux_t*, dvb_mux_t*);
331   int       (*lnb_band) (linuxdvb_lnb_t*, dvb_mux_t*);
332   int       (*lnb_pol)  (linuxdvb_lnb_t*, dvb_mux_t*);
333 };
334 
335 #define UNICABLE_EN50494     50494
336 #define UNICABLE_EN50607     50607
337 
338 struct linuxdvb_en50494
339 {
340   linuxdvb_diseqc_t;
341 
342   /* en50494 configuration */
343   uint16_t  le_position;  /* satelitte A(0) or B(1) */
344   uint16_t  le_frequency; /* user band frequency in MHz */
345   uint16_t  le_id;        /* user band id 0-7 */
346   uint16_t  le_pin;       /* 0-255 or LINUXDVB_EN50494_NOPIN */
347 
348   /* runtime */
349   uint32_t  le_tune_freq; /* the real frequency to tune to */
350 };
351 
352 /*
353  * Classes
354  */
355 
356 extern const idclass_t linuxdvb_adapter_class;
357 extern const idclass_t linuxdvb_ca_class;
358 
359 extern const idclass_t linuxdvb_frontend_dvbt_class;
360 extern const idclass_t linuxdvb_frontend_dvbs_class;
361 extern const idclass_t linuxdvb_frontend_dvbs_slave_class;
362 extern const idclass_t linuxdvb_frontend_dvbc_class;
363 extern const idclass_t linuxdvb_frontend_atsc_t_class;
364 extern const idclass_t linuxdvb_frontend_atsc_c_class;
365 extern const idclass_t linuxdvb_frontend_isdb_t_class;
366 extern const idclass_t linuxdvb_frontend_isdb_c_class;
367 extern const idclass_t linuxdvb_frontend_isdb_s_class;
368 extern const idclass_t linuxdvb_frontend_dab_class;
369 
370 extern const idclass_t linuxdvb_lnb_class;
371 extern const idclass_t linuxdvb_rotor_class;
372 extern const idclass_t linuxdvb_rotor_gotox_class;
373 extern const idclass_t linuxdvb_rotor_usals_class;
374 extern const idclass_t linuxdvb_en50494_class;
375 extern const idclass_t linuxdvb_switch_class;
376 extern const idclass_t linuxdvb_diseqc_class;
377 
378 extern const idclass_t linuxdvb_satconf_class;
379 extern const idclass_t linuxdvb_satconf_lnbonly_class;
380 extern const idclass_t linuxdvb_satconf_2port_class;
381 extern const idclass_t linuxdvb_satconf_4port_class;
382 extern const idclass_t linuxdvb_satconf_en50494_class;
383 extern const idclass_t linuxdvb_satconf_advanced_class;
384 extern const idclass_t linuxdvb_satconf_ele_class;
385 
386 /*
387  * Methods
388  */
389 
390 #define LINUXDVB_SUBSYS_FE  0x01
391 #define LINUXDVB_SUBSYS_DVR 0x02
392 
393 void linuxdvb_adapter_init ( void );
394 
395 void linuxdvb_adapter_done ( void );
396 
linuxdvb_adapter_changed(linuxdvb_adapter_t * la)397 static inline void linuxdvb_adapter_changed ( linuxdvb_adapter_t *la )
398   { idnode_changed(&la->th_id); }
399 
400 int  linuxdvb_adapter_current_weight ( linuxdvb_adapter_t *la );
401 
402 const void *linuxdvb_frontend_class_active_get ( void *obj );
403 
404 linuxdvb_frontend_t *
405 linuxdvb_frontend_create
406   ( htsmsg_t *conf, linuxdvb_adapter_t *la, int number,
407     const char *fe_path, const char *dmx_path, const char *dvr_path,
408     dvb_fe_type_t type, const char *name );
409 
410 void linuxdvb_frontend_save ( linuxdvb_frontend_t *lfe, htsmsg_t *m );
411 
412 void linuxdvb_frontend_delete ( linuxdvb_frontend_t *lfe );
413 
414 void linuxdvb_frontend_add_network
415   ( linuxdvb_frontend_t *lfe, linuxdvb_network_t *net );
416 
417 int linuxdvb_frontend_clear
418   ( linuxdvb_frontend_t *lfe, mpegts_mux_instance_t *mmi );
419 int linuxdvb_frontend_tune0
420   ( linuxdvb_frontend_t *lfe, mpegts_mux_instance_t *mmi, uint32_t freq );
421 int linuxdvb_frontend_tune1
422   ( linuxdvb_frontend_t *lfe, mpegts_mux_instance_t *mmi, uint32_t freq );
423 
424 int linuxdvb2tvh_delsys ( int delsys );
425 
426 #if ENABLE_LINUXDVB_CA
427 
428 linuxdvb_ca_t *
429 linuxdvb_ca_create
430   ( htsmsg_t *conf, linuxdvb_adapter_t *la, int number, const char *ca_path);
431 
432 void linuxdvb_ca_save( linuxdvb_ca_t *lca, htsmsg_t *m );
433 
434 void
435 linuxdvb_ca_enqueue_capmt(linuxdvb_ca_t *lca, uint8_t slot, const uint8_t *ptr,
436                           int len, uint8_t list_mgmt, uint8_t cmd_id);
437 
438 #endif
439 
440 /*
441  * Diseqc gear
442  */
443 linuxdvb_diseqc_t *linuxdvb_diseqc_create0
444   ( linuxdvb_diseqc_t *ld, const char *uuid, const idclass_t *idc,
445     htsmsg_t *conf, const char *type, linuxdvb_satconf_ele_t *parent );
446 
447 void linuxdvb_diseqc_destroy ( linuxdvb_diseqc_t *ld );
448 
449 #define linuxdvb_diseqc_create(_d, _u, _c, _t, _p)\
450   linuxdvb_diseqc_create0(calloc(1, sizeof(struct _d)),\
451                                  _u, &_d##_class, _c, _t, _p)
452 
453 linuxdvb_lnb_t    *linuxdvb_lnb_create0
454   ( const char *name, htsmsg_t *conf, linuxdvb_satconf_ele_t *ls );
455 linuxdvb_diseqc_t *linuxdvb_switch_create0
456   ( const char *name, htsmsg_t *conf, linuxdvb_satconf_ele_t *ls, int c, int u );
457 linuxdvb_diseqc_t *linuxdvb_rotor_create0
458   ( const char *name, htsmsg_t *conf, linuxdvb_satconf_ele_t *ls );
459 
460 #define UNICABLE_I_NAME       "Unicable I (EN50494)"
461 #define UNICABLE_II_NAME      "Unicable II (EN50607)"
462 
463 linuxdvb_diseqc_t *linuxdvb_en50494_create0
464   ( const char *name, htsmsg_t *conf, linuxdvb_satconf_ele_t *ls, int port );
465 
466 void linuxdvb_lnb_destroy     ( linuxdvb_lnb_t *lnb );
467 void linuxdvb_switch_destroy  ( linuxdvb_diseqc_t *ld );
468 void linuxdvb_rotor_destroy   ( linuxdvb_diseqc_t *ld );
469 void linuxdvb_en50494_destroy ( linuxdvb_diseqc_t *ld );
470 
471 htsmsg_t *linuxdvb_lnb_list     ( void *o, const char *lang );
472 htsmsg_t *linuxdvb_switch_list  ( void *o, const char *lang );
473 htsmsg_t *linuxdvb_rotor_list   ( void *o, const char *lang );
474 htsmsg_t *linuxdvb_en50494_list ( void *o, const char *lang );
475 
476 htsmsg_t *linuxdvb_en50494_id_list  ( void *o, const char *lang );
477 htsmsg_t *linuxdvb_en50607_id_list  ( void *o, const char *lang );
478 htsmsg_t *linuxdvb_en50494_pin_list ( void *o, const char *lang );
479 
linuxdvb_unicable_is_en50607(const char * str)480 static inline int linuxdvb_unicable_is_en50607( const char *str )
481   { return strcmp(str, UNICABLE_II_NAME) == 0; }
linuxdvb_unicable_is_en50494(const char * str)482 static inline int linuxdvb_unicable_is_en50494( const char *str )
483   { return !linuxdvb_unicable_is_en50607(str); }
484 
485 void linuxdvb_en50494_init (void);
486 
487 int linuxdvb_diseqc_raw_send (int fd, int len, ...);
488 int
489 linuxdvb_diseqc_send
490   (int fd, uint8_t framing, uint8_t addr, uint8_t cmd, int len, ...);
491 int linuxdvb_diseqc_set_volt ( linuxdvb_satconf_t *ls, int volt );
492 
493 /*
494  * Satconf
495  */
496 void linuxdvb_satconf_save ( linuxdvb_satconf_t *ls, htsmsg_t *m );
497 
498 linuxdvb_satconf_ele_t *linuxdvb_satconf_ele_create0
499   (const char *uuid, htsmsg_t *conf, linuxdvb_satconf_t *ls);
500 
501 void linuxdvb_satconf_ele_destroy ( linuxdvb_satconf_ele_t *ls );
502 
503 htsmsg_t *linuxdvb_satconf_type_list ( void *o, const char *lang );
504 
505 linuxdvb_satconf_t *linuxdvb_satconf_create
506   ( linuxdvb_frontend_t *lfe, htsmsg_t *conf );
507 
508 void linuxdvb_satconf_delete ( linuxdvb_satconf_t *ls, int delconf );
509 
510 int linuxdvb_satconf_get_priority
511   ( linuxdvb_satconf_t *ls, mpegts_mux_t *mm );
512 
513 int linuxdvb_satconf_get_grace
514   ( linuxdvb_satconf_t *ls, mpegts_mux_t *mm );
515 
516 int linuxdvb_satconf_lnb_freq
517   ( linuxdvb_satconf_t *ls, mpegts_mux_instance_t *mmi );
518 
519 void linuxdvb_satconf_post_stop_mux( linuxdvb_satconf_t *ls );
520 
521 int linuxdvb_satconf_start_mux
522   ( linuxdvb_satconf_t *ls, mpegts_mux_instance_t *mmi, int skip_diseqc );
523 
524 int linuxdvb_satconf_match_mux
525   ( linuxdvb_satconf_t *ls, mpegts_mux_t *mm );
526 
527 int linuxdvb_satconf_start ( linuxdvb_satconf_t *ls, int delay, int vol );
528 
529 void linuxdvb_satconf_reset ( linuxdvb_satconf_t *ls );
530 
linuxdvb_satconf_fe_fd(linuxdvb_satconf_t * ls)531 static inline int linuxdvb_satconf_fe_fd ( linuxdvb_satconf_t *ls )
532   { return ((linuxdvb_frontend_t *)ls->ls_frontend)->lfe_fe_fd; }
533 
534 #endif /* __TVH_LINUXDVB_PRIVATE_H__ */
535