1 /* 2 * Copyright (C) 2017 Amlogic, Inc. All rights reserved. 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12 * more details. 13 * 14 * You should have received a copy of the GNU General Public License along 15 * with this program; if not, write to the Free Software Foundation, Inc., 16 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 * 18 * Description: 19 */ 20 #ifndef _AML_DVB_H_ 21 #define _AML_DVB_H_ 22 23 #include <linux/interrupt.h> 24 #include <linux/socket.h> 25 #include <linux/netdevice.h> 26 #include <linux/i2c.h> 27 28 #include <linux/dvb/video.h> 29 #include <linux/dvb/audio.h> 30 #include <linux/dvb/dmx.h> 31 #include <linux/dvb/ca.h> 32 #include <linux/dvb/osd.h> 33 #include <linux/dvb/net.h> 34 #include <linux/dvb/frontend.h> 35 36 #include <linux/mutex.h> 37 #include <linux/spinlock.h> 38 #include <linux/interrupt.h> 39 40 #ifdef CONFIG_HAS_EARLYSUSPEND 41 #include <linux/earlysuspend.h> 42 #endif 43 44 45 #include <dvbdev.h> 46 #include <demux.h> 47 #include <dvb_demux.h> 48 #include <dmxdev.h> 49 #include <dvb_filter.h> 50 #include <dvb_net.h> 51 #include <dvb_ringbuffer.h> 52 53 #include <linux/of.h> 54 #include <linux/pinctrl/consumer.h> 55 56 #include "aml_demod_gt.h" 57 58 #define TS_IN_COUNT 4 59 #define S2P_COUNT 3 60 #define ASYNCFIFO_COUNT 3 61 #if 0 62 #define TS_IN_COUNT 3 63 #define S2P_COUNT 2 64 #define ASYNCFIFO_COUNT 2 65 #endif 66 67 #define DMX_DEV_COUNT 3 68 #define FE_DEV_COUNT 2 69 #define CHANNEL_COUNT 31 70 #define FILTER_COUNT 31 71 #define FILTER_LEN 15 72 #define DSC_DEV_COUNT 2 73 #define DSC_COUNT 8 74 #define SEC_BUF_GRP_COUNT 4 75 #define SEC_BUF_BUSY_SIZE 4 76 #define SEC_BUF_COUNT (SEC_BUF_GRP_COUNT*8) 77 78 enum aml_dmx_id_t { 79 AM_DMX_0 = 0, 80 AM_DMX_1, 81 AM_DMX_2, 82 AM_DMX_MAX, 83 }; 84 85 enum aml_ts_source_t { 86 AM_TS_SRC_TS0, 87 AM_TS_SRC_TS1, 88 AM_TS_SRC_TS2, 89 AM_TS_SRC_TS3, 90 91 AM_TS_SRC_S_TS0, 92 AM_TS_SRC_S_TS1, 93 AM_TS_SRC_S_TS2, 94 AM_TS_SRC_S_TS3, 95 96 AM_TS_SRC_HIU, 97 AM_TS_SRC_HIU1, 98 AM_TS_SRC_DMX0, 99 AM_TS_SRC_DMX1, 100 AM_TS_SRC_DMX2 101 }; 102 103 struct aml_sec_buf { 104 unsigned long addr; 105 int len; 106 }; 107 108 struct aml_channel { 109 int type; 110 enum dmx_ts_pes pes_type; 111 int pid; 112 int used; 113 int filter_count; 114 struct dvb_demux_feed *feed; 115 struct dvb_demux_feed *dvr_feed; 116 }; 117 118 struct aml_filter { 119 int chan_id; 120 int used; 121 struct dmx_section_filter *filter; 122 u8 value[FILTER_LEN]; 123 u8 maskandmode[FILTER_LEN]; 124 u8 maskandnotmode[FILTER_LEN]; 125 u8 neq; 126 }; 127 128 #define DVBCSA_MODE 0 129 #define CIPLUS_MODE 1 130 #define CBC_MODE 0 131 #define ECB_MODE 1 132 #define IDSA_MODE 2 133 134 #define DSC_SET_EVEN 1 135 #define DSC_SET_ODD 2 136 #define DSC_SET_AES_EVEN 4 137 #define DSC_SET_AES_ODD 8 138 #define DSC_FROM_KL 16 139 #define DSC_SET_SM4_EVEN 32 140 #define DSC_SET_SM4_ODD 64 141 142 #define DSC_KEY_SIZE_MAX 16 143 144 struct aml_dsc_channel { 145 int pid; 146 u8 even[DSC_KEY_SIZE_MAX]; 147 u8 odd[DSC_KEY_SIZE_MAX]; 148 u8 even_iv[DSC_KEY_SIZE_MAX]; 149 u8 odd_iv[DSC_KEY_SIZE_MAX]; 150 int used; 151 int set; 152 int id; 153 struct aml_dsc *dsc; 154 int work_mode; 155 int mode; 156 }; 157 158 struct aml_dsc { 159 struct dvb_device *dev; 160 struct aml_dsc_channel channel[DSC_COUNT]; 161 enum aml_ts_source_t source; 162 enum aml_ts_source_t dst; 163 struct aml_dvb *dvb; 164 int id; 165 int work_mode; 166 }; 167 168 struct aml_smallsec { 169 struct aml_dmx *dmx; 170 171 int enable; 172 int bufsize; 173 #define SS_BUFSIZE_DEF (16*4*256) /*16KB*/ 174 long buf; 175 long buf_map; 176 }; 177 178 struct aml_dmxtimeout { 179 struct aml_dmx *dmx; 180 181 int enable; 182 183 int timeout; 184 #define DTO_TIMEOUT_DEF (9000) /*0.5s*/ 185 u32 ch_disable; 186 #define DTO_CHDIS_VAS (0xfffffff8) /*v/a/s only*/ 187 int match; 188 189 int trigger; 190 }; 191 192 struct aml_dmx { 193 struct dvb_demux demux; 194 struct dmxdev dmxdev; 195 int id; 196 int feed_count; 197 int chan_count; 198 enum aml_ts_source_t source; 199 int init; 200 int record; 201 struct dmx_frontend hw_fe[DMX_DEV_COUNT]; 202 struct dmx_frontend mem_fe; 203 struct dvb_net dvb_net; 204 int dmx_irq; 205 int dvr_irq; 206 struct tasklet_struct dmx_tasklet; 207 struct tasklet_struct dvr_tasklet; 208 unsigned long sec_pages; 209 unsigned long sec_pages_map; 210 int sec_total_len; 211 struct aml_sec_buf sec_buf[SEC_BUF_COUNT]; 212 unsigned long pes_pages; 213 unsigned long pes_pages_map; 214 int pes_buf_len; 215 union { 216 unsigned long sub_pages; 217 unsigned long sub_buf_base; 218 }; 219 union { 220 unsigned long sub_pages_map; 221 u8 *sub_buf_base_virt; 222 }; 223 int sub_buf_len; 224 225 struct aml_channel channel[CHANNEL_COUNT+1]; 226 struct aml_filter filter[FILTER_COUNT+1]; 227 irq_handler_t irq_handler; 228 void *irq_data; 229 int aud_chan; 230 int vid_chan; 231 int sub_chan; 232 int pcr_chan; 233 u32 section_busy[SEC_BUF_BUSY_SIZE]; 234 struct dvb_frontend *fe; 235 int int_check_count; 236 u32 int_check_time; 237 int in_tune; 238 int error_check; 239 int dump_ts_select; 240 int sec_buf_watchdog_count[SEC_BUF_COUNT]; 241 242 struct aml_smallsec smallsec; 243 struct aml_dmxtimeout timeout; 244 245 int demux_filter_user; 246 247 unsigned long sec_cnt[3]; 248 unsigned long sec_cnt_match[3]; 249 unsigned long sec_cnt_crc_fail[3]; 250 #define SEC_CNT_HW (0) 251 #define SEC_CNT_SW (1) 252 #define SEC_CNT_SS (2) 253 #define SEC_CNT_MAX (3) 254 255 int crc_check_count; 256 u32 crc_check_time; 257 int om_status_error_count; 258 }; 259 260 struct aml_dvr_block { 261 u32 addr; 262 u32 len; 263 }; 264 265 struct aml_asyncfifo { 266 int id; 267 int init; 268 int asyncfifo_irq; 269 enum aml_dmx_id_t source; 270 unsigned long pages; 271 unsigned long pages_map; 272 int buf_len; 273 int buf_toggle; 274 int buf_read; 275 int flush_size; 276 int secure_enable; 277 struct tasklet_struct asyncfifo_tasklet; 278 struct aml_dvb *dvb; 279 struct aml_dvr_block blk; 280 unsigned long stored_pages; 281 }; 282 283 enum{ 284 AM_TS_DISABLE, 285 AM_TS_PARALLEL, 286 AM_TS_SERIAL 287 }; 288 289 struct aml_ts_input { 290 int mode; 291 struct pinctrl *pinctrl; 292 int control; 293 int s2p_id; 294 }; 295 296 struct aml_s2p { 297 int invert; 298 }; 299 300 struct aml_swfilter { 301 int user; 302 struct aml_dmx *dmx; 303 struct aml_asyncfifo *afifo; 304 305 struct dvb_ringbuffer rbuf; 306 #define SF_BUFFER_SIZE (10*188*1024) 307 308 u8 wrapbuf[188]; 309 int track_dmx; 310 }; 311 312 struct aml_dvb { 313 struct dvb_device dvb_dev; 314 int ts_in_total_count; 315 struct aml_ts_input ts[TS_IN_COUNT]; 316 int s2p_total_count; 317 struct aml_s2p s2p[S2P_COUNT]; 318 struct aml_dmx dmx[DMX_DEV_COUNT]; 319 struct aml_dsc dsc[DSC_DEV_COUNT]; 320 int async_fifo_total_count; 321 struct aml_asyncfifo asyncfifo[ASYNCFIFO_COUNT]; 322 struct dvb_adapter dvb_adapter; 323 struct device *dev; 324 struct platform_device *pdev; 325 enum aml_ts_source_t stb_source; 326 enum aml_ts_source_t tso_source; 327 int dmx_init; 328 int reset_flag; 329 spinlock_t slock; 330 struct timer_list watchdog_timer; 331 int dmx_watchdog_disable[DMX_DEV_COUNT]; 332 struct aml_swfilter swfilter; 333 int ts_out_invert; 334 335 unsigned int tuner_num; 336 unsigned int tuner_cur; 337 struct aml_tuner *tuners; 338 bool tuner_attached; 339 340 /*bufs for dmx shared*/ 341 unsigned long pes_pages; 342 unsigned long pes_pages_map; 343 int pes_buf_len; 344 unsigned long sub_pages; 345 unsigned long sub_pages_map; 346 int sub_buf_len; 347 348 349 }; 350 351 352 /*AMLogic demux interface*/ 353 extern int aml_dmx_hw_init(struct aml_dmx *dmx); 354 extern int aml_dmx_hw_deinit(struct aml_dmx *dmx); 355 extern int aml_dmx_hw_start_feed(struct dvb_demux_feed *dvbdmxfeed); 356 extern int aml_dmx_hw_stop_feed(struct dvb_demux_feed *dvbdmxfeed); 357 extern int aml_dmx_hw_set_source(struct dmx_demux *demux, 358 dmx_source_t src); 359 extern int aml_stb_hw_set_source(struct aml_dvb *dvb, dmx_source_t src); 360 extern int aml_dsc_hw_set_source(struct aml_dsc *dsc, 361 dmx_source_t src, dmx_source_t dst); 362 extern int aml_tso_hw_set_source(struct aml_dvb *dvb, dmx_source_t src); 363 extern int aml_dmx_set_skipbyte(struct aml_dvb *dvb, int skipbyte); 364 extern int aml_dmx_set_demux(struct aml_dvb *dvb, int id); 365 extern int aml_dmx_hw_set_dump_ts_select 366 (struct dmx_demux *demux, int dump_ts_select); 367 368 extern int dmx_alloc_chan(struct aml_dmx *dmx, int type, 369 int pes_type, int pid); 370 extern void dmx_free_chan(struct aml_dmx *dmx, int cid); 371 372 extern int dmx_get_ts_serial(enum aml_ts_source_t src); 373 374 extern int dmx_get_sub_buffer(unsigned long *base, unsigned long *virt); 375 extern int dmx_init_sub_buffer(struct aml_dmx *dmx, unsigned long base, unsigned long virt); 376 377 /*AMLogic dsc interface*/ 378 extern int dsc_set_pid(struct aml_dsc_channel *ch, int pid); 379 extern int dsc_set_key(struct aml_dsc_channel *ch, int flags, 380 enum ca_cw_type type, u8 *key); 381 extern void dsc_release(void); 382 extern int aml_ciplus_hw_set_source(int src); 383 384 /*AMLogic ASYNC FIFO interface*/ 385 extern int aml_asyncfifo_hw_init(struct aml_asyncfifo *afifo); 386 extern int aml_asyncfifo_hw_deinit(struct aml_asyncfifo *afifo); 387 extern int aml_asyncfifo_hw_set_source(struct aml_asyncfifo *afifo, 388 enum aml_dmx_id_t src); 389 extern int aml_asyncfifo_hw_reset(struct aml_asyncfifo *afifo); 390 391 /*Get the Audio & Video PTS*/ 392 extern u32 aml_dmx_get_video_pts(struct aml_dvb *dvb); 393 extern u32 aml_dmx_get_audio_pts(struct aml_dvb *dvb); 394 extern u32 aml_dmx_get_video_pts_bit32(struct aml_dvb *dvb); 395 extern u32 aml_dmx_get_audio_pts_bit32(struct aml_dvb *dvb); 396 extern u32 aml_dmx_get_first_video_pts(struct aml_dvb *dvb); 397 extern u32 aml_dmx_get_first_audio_pts(struct aml_dvb *dvb); 398 399 /*Get the DVB device*/ 400 extern struct aml_dvb *aml_get_dvb_device(void); 401 402 extern int aml_regist_dmx_class(void); 403 extern int aml_unregist_dmx_class(void); 404 extern void aml_register_parser_mconfig(void); 405 406 struct devio_aml_platform_data { 407 int (*io_setup)(void *); 408 int (*io_cleanup)(void *); 409 int (*io_power)(void *, int enable); 410 int (*io_reset)(void *, int enable); 411 }; 412 413 void get_aml_dvb(struct aml_dvb *dvb_device); 414 415 /*Reset the demux device*/ 416 void dmx_reset_hw(struct aml_dvb *dvb); 417 void dmx_reset_hw_ex(struct aml_dvb *dvb, int reset_irq); 418 419 /*Reset the individual demux*/ 420 void dmx_reset_dmx_hw(struct aml_dvb *dvb, int id); 421 void dmx_reset_dmx_id_hw_ex(struct aml_dvb *dvb, int id, int reset_irq); 422 void dmx_reset_dmx_id_hw_ex_unlock(struct aml_dvb *dvb, int id, int reset_irq); 423 void dmx_reset_dmx_hw_ex(struct aml_dvb *dvb, 424 struct aml_dmx *dmx, 425 int reset_irq); 426 void dmx_reset_dmx_hw_ex_unlock(struct aml_dvb *dvb, 427 struct aml_dmx *dmx, 428 int reset_irq); 429 430 #endif 431 432