1#include <linux/module.h> 2#include <linux/kernel.h> 3#include <linux/string.h> 4#include <linux/timer.h> 5#include <linux/delay.h> 6#include <linux/errno.h> 7#include <linux/slab.h> 8#include <linux/poll.h> 9#include <linux/i2c.h> 10#include <linux/types.h> 11#include <linux/videodev2.h> 12#include <media/v4l2-common.h> 13#include <linux/init.h> 14#include <linux/crc32.h> 15 16 17#define MPEG_VIDEO_TARGET_BITRATE_MAX 27000 18#define MPEG_VIDEO_MAX_BITRATE_MAX 27000 19#define MPEG_TOTAL_TARGET_BITRATE_MAX 27000 20#define MPEG_PID_MAX ((1 << 14) - 1) 21 22/* Addresses to scan */ 23static unsigned short normal_i2c[] = {0x20, I2C_CLIENT_END}; 24I2C_CLIENT_INSMOD; 25 26MODULE_DESCRIPTION("device driver for saa6752hs MPEG2 encoder"); 27MODULE_AUTHOR("Andrew de Quincey"); 28MODULE_LICENSE("GPL"); 29 30static struct i2c_driver driver; 31static struct i2c_client client_template; 32 33enum saa6752hs_videoformat { 34 SAA6752HS_VF_D1 = 0, /* standard D1 video format: 720x576 */ 35 SAA6752HS_VF_2_3_D1 = 1,/* 2/3D1 video format: 480x576 */ 36 SAA6752HS_VF_1_2_D1 = 2,/* 1/2D1 video format: 352x576 */ 37 SAA6752HS_VF_SIF = 3, /* SIF video format: 352x288 */ 38 SAA6752HS_VF_UNKNOWN, 39}; 40 41struct saa6752hs_mpeg_params { 42 /* transport streams */ 43 __u16 ts_pid_pmt; 44 __u16 ts_pid_audio; 45 __u16 ts_pid_video; 46 __u16 ts_pid_pcr; 47 48 /* audio */ 49 enum v4l2_mpeg_audio_l2_bitrate au_l2_bitrate; 50 51 /* video */ 52 enum v4l2_mpeg_video_aspect vi_aspect; 53 enum v4l2_mpeg_video_bitrate_mode vi_bitrate_mode; 54 __u32 vi_bitrate; 55 __u32 vi_bitrate_peak; 56}; 57 58static const struct v4l2_format v4l2_format_table[] = 59{ 60 [SAA6752HS_VF_D1] = 61 { .fmt = { .pix = { .width = 720, .height = 576 }}}, 62 [SAA6752HS_VF_2_3_D1] = 63 { .fmt = { .pix = { .width = 480, .height = 576 }}}, 64 [SAA6752HS_VF_1_2_D1] = 65 { .fmt = { .pix = { .width = 352, .height = 576 }}}, 66 [SAA6752HS_VF_SIF] = 67 { .fmt = { .pix = { .width = 352, .height = 288 }}}, 68 [SAA6752HS_VF_UNKNOWN] = 69 { .fmt = { .pix = { .width = 0, .height = 0}}}, 70}; 71 72struct saa6752hs_state { 73 struct i2c_client client; 74 struct v4l2_mpeg_compression old_params; 75 struct saa6752hs_mpeg_params params; 76 enum saa6752hs_videoformat video_format; 77 v4l2_std_id standard; 78}; 79 80enum saa6752hs_command { 81 SAA6752HS_COMMAND_RESET = 0, 82 SAA6752HS_COMMAND_STOP = 1, 83 SAA6752HS_COMMAND_START = 2, 84 SAA6752HS_COMMAND_PAUSE = 3, 85 SAA6752HS_COMMAND_RECONFIGURE = 4, 86 SAA6752HS_COMMAND_SLEEP = 5, 87 SAA6752HS_COMMAND_RECONFIGURE_FORCE = 6, 88 89 SAA6752HS_COMMAND_MAX 90}; 91 92/* ---------------------------------------------------------------------- */ 93 94static u8 PAT[] = { 95 0xc2, /* i2c register */ 96 0x00, /* table number for encoder */ 97 98 0x47, /* sync */ 99 0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid(0) */ 100 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */ 101 102 0x00, /* PSI pointer to start of table */ 103 104 0x00, /* tid(0) */ 105 0xb0, 0x0d, /* section_syntax_indicator(1), section_length(13) */ 106 107 0x00, 0x01, /* transport_stream_id(1) */ 108 109 0xc1, /* version_number(0), current_next_indicator(1) */ 110 111 0x00, 0x00, /* section_number(0), last_section_number(0) */ 112 113 0x00, 0x01, /* program_number(1) */ 114 115 0xe0, 0x00, /* PMT PID */ 116 117 0x00, 0x00, 0x00, 0x00 /* CRC32 */ 118}; 119 120static u8 PMT[] = { 121 0xc2, /* i2c register */ 122 0x01, /* table number for encoder */ 123 124 0x47, /* sync */ 125 0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid */ 126 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */ 127 128 0x00, /* PSI pointer to start of table */ 129 130 0x02, /* tid(2) */ 131 0xb0, 0x17, /* section_syntax_indicator(1), section_length(23) */ 132 133 0x00, 0x01, /* program_number(1) */ 134 135 0xc1, /* version_number(0), current_next_indicator(1) */ 136 137 0x00, 0x00, /* section_number(0), last_section_number(0) */ 138 139 0xe0, 0x00, /* PCR_PID */ 140 141 0xf0, 0x00, /* program_info_length(0) */ 142 143 0x02, 0xe0, 0x00, 0xf0, 0x00, /* video stream type(2), pid */ 144 0x04, 0xe0, 0x00, 0xf0, 0x00, /* audio stream type(4), pid */ 145 146 0x00, 0x00, 0x00, 0x00 /* CRC32 */ 147}; 148 149static struct saa6752hs_mpeg_params param_defaults = 150{ 151 .ts_pid_pmt = 16, 152 .ts_pid_video = 260, 153 .ts_pid_audio = 256, 154 .ts_pid_pcr = 259, 155 156 .vi_aspect = V4L2_MPEG_VIDEO_ASPECT_4x3, 157 .vi_bitrate = 4000, 158 .vi_bitrate_peak = 6000, 159 .vi_bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_VBR, 160 161 .au_l2_bitrate = V4L2_MPEG_AUDIO_L2_BITRATE_256K, 162}; 163 164static struct v4l2_mpeg_compression old_param_defaults = 165{ 166 .st_type = V4L2_MPEG_TS_2, 167 .st_bitrate = { 168 .mode = V4L2_BITRATE_CBR, 169 .target = 7000, 170 }, 171 172 .ts_pid_pmt = 16, 173 .ts_pid_video = 260, 174 .ts_pid_audio = 256, 175 .ts_pid_pcr = 259, 176 177 .vi_type = V4L2_MPEG_VI_2, 178 .vi_aspect_ratio = V4L2_MPEG_ASPECT_4_3, 179 .vi_bitrate = { 180 .mode = V4L2_BITRATE_VBR, 181 .target = 4000, 182 .max = 6000, 183 }, 184 185 .au_type = V4L2_MPEG_AU_2_II, 186 .au_bitrate = { 187 .mode = V4L2_BITRATE_CBR, 188 .target = 256, 189 }, 190 191}; 192 193/* ---------------------------------------------------------------------- */ 194 195static int saa6752hs_chip_command(struct i2c_client* client, 196 enum saa6752hs_command command) 197{ 198 unsigned char buf[3]; 199 unsigned long timeout; 200 int status = 0; 201 202 /* execute the command */ 203 switch(command) { 204 case SAA6752HS_COMMAND_RESET: 205 buf[0] = 0x00; 206 break; 207 208 case SAA6752HS_COMMAND_STOP: 209 buf[0] = 0x03; 210 break; 211 212 case SAA6752HS_COMMAND_START: 213 buf[0] = 0x02; 214 break; 215 216 case SAA6752HS_COMMAND_PAUSE: 217 buf[0] = 0x04; 218 break; 219 220 case SAA6752HS_COMMAND_RECONFIGURE: 221 buf[0] = 0x05; 222 break; 223 224 case SAA6752HS_COMMAND_SLEEP: 225 buf[0] = 0x06; 226 break; 227 228 case SAA6752HS_COMMAND_RECONFIGURE_FORCE: 229 buf[0] = 0x07; 230 break; 231 232 default: 233 return -EINVAL; 234 } 235 236 /* set it and wait for it to be so */ 237 i2c_master_send(client, buf, 1); 238 timeout = jiffies + HZ * 3; 239 for (;;) { 240 /* get the current status */ 241 buf[0] = 0x10; 242 i2c_master_send(client, buf, 1); 243 i2c_master_recv(client, buf, 1); 244 245 if (!(buf[0] & 0x20)) 246 break; 247 if (time_after(jiffies,timeout)) { 248 status = -ETIMEDOUT; 249 break; 250 } 251 252 msleep(10); 253 } 254 255 /* delay a bit to let encoder settle */ 256 msleep(50); 257 258 return status; 259} 260 261 262static int saa6752hs_set_bitrate(struct i2c_client* client, 263 struct saa6752hs_mpeg_params* params) 264{ 265 u8 buf[3]; 266 int tot_bitrate; 267 268 /* set the bitrate mode */ 269 buf[0] = 0x71; 270 buf[1] = (params->vi_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) ? 0 : 1; 271 i2c_master_send(client, buf, 2); 272 273 /* set the video bitrate */ 274 if (params->vi_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) { 275 /* set the target bitrate */ 276 buf[0] = 0x80; 277 buf[1] = params->vi_bitrate >> 8; 278 buf[2] = params->vi_bitrate & 0xff; 279 i2c_master_send(client, buf, 3); 280 281 /* set the max bitrate */ 282 buf[0] = 0x81; 283 buf[1] = params->vi_bitrate_peak >> 8; 284 buf[2] = params->vi_bitrate_peak & 0xff; 285 i2c_master_send(client, buf, 3); 286 tot_bitrate = params->vi_bitrate_peak; 287 } else { 288 /* set the target bitrate (no max bitrate for CBR) */ 289 buf[0] = 0x81; 290 buf[1] = params->vi_bitrate >> 8; 291 buf[2] = params->vi_bitrate & 0xff; 292 i2c_master_send(client, buf, 3); 293 tot_bitrate = params->vi_bitrate; 294 } 295 296 /* set the audio bitrate */ 297 buf[0] = 0x94; 298 buf[1] = (V4L2_MPEG_AUDIO_L2_BITRATE_256K == params->au_l2_bitrate) ? 0 : 1; 299 i2c_master_send(client, buf, 2); 300 tot_bitrate += (V4L2_MPEG_AUDIO_L2_BITRATE_256K == params->au_l2_bitrate) ? 256 : 384; 301 302 /* Note: the total max bitrate is determined by adding the video and audio 303 bitrates together and also adding an extra 768kbit/s to stay on the 304 safe side. If more control should be required, then an extra MPEG control 305 should be added. */ 306 tot_bitrate += 768; 307 if (tot_bitrate > MPEG_TOTAL_TARGET_BITRATE_MAX) 308 tot_bitrate = MPEG_TOTAL_TARGET_BITRATE_MAX; 309 310 /* set the total bitrate */ 311 buf[0] = 0xb1; 312 buf[1] = tot_bitrate >> 8; 313 buf[2] = tot_bitrate & 0xff; 314 i2c_master_send(client, buf, 3); 315 316 return 0; 317} 318 319static void saa6752hs_set_subsampling(struct i2c_client* client, 320 struct v4l2_format* f) 321{ 322 struct saa6752hs_state *h = i2c_get_clientdata(client); 323 int dist_352, dist_480, dist_720; 324 325 326 dist_352 = abs(f->fmt.pix.width - 352); 327 dist_480 = abs(f->fmt.pix.width - 480); 328 dist_720 = abs(f->fmt.pix.width - 720); 329 if (dist_720 < dist_480) { 330 f->fmt.pix.width = 720; 331 f->fmt.pix.height = 576; 332 h->video_format = SAA6752HS_VF_D1; 333 } 334 else if (dist_480 < dist_352) { 335 f->fmt.pix.width = 480; 336 f->fmt.pix.height = 576; 337 h->video_format = SAA6752HS_VF_2_3_D1; 338 } 339 else { 340 f->fmt.pix.width = 352; 341 if (abs(f->fmt.pix.height - 576) < 342 abs(f->fmt.pix.height - 288)) { 343 f->fmt.pix.height = 576; 344 h->video_format = SAA6752HS_VF_1_2_D1; 345 } 346 else { 347 f->fmt.pix.height = 288; 348 h->video_format = SAA6752HS_VF_SIF; 349 } 350 } 351} 352 353 354static void saa6752hs_old_set_params(struct i2c_client* client, 355 struct v4l2_mpeg_compression* params) 356{ 357 struct saa6752hs_state *h = i2c_get_clientdata(client); 358 359 /* check PIDs */ 360 if (params->ts_pid_pmt <= MPEG_PID_MAX) { 361 h->old_params.ts_pid_pmt = params->ts_pid_pmt; 362 h->params.ts_pid_pmt = params->ts_pid_pmt; 363 } 364 if (params->ts_pid_pcr <= MPEG_PID_MAX) { 365 h->old_params.ts_pid_pcr = params->ts_pid_pcr; 366 h->params.ts_pid_pcr = params->ts_pid_pcr; 367 } 368 if (params->ts_pid_video <= MPEG_PID_MAX) { 369 h->old_params.ts_pid_video = params->ts_pid_video; 370 h->params.ts_pid_video = params->ts_pid_video; 371 } 372 if (params->ts_pid_audio <= MPEG_PID_MAX) { 373 h->old_params.ts_pid_audio = params->ts_pid_audio; 374 h->params.ts_pid_audio = params->ts_pid_audio; 375 } 376 377 /* check bitrate parameters */ 378 if ((params->vi_bitrate.mode == V4L2_BITRATE_CBR) || 379 (params->vi_bitrate.mode == V4L2_BITRATE_VBR)) { 380 h->old_params.vi_bitrate.mode = params->vi_bitrate.mode; 381 h->params.vi_bitrate_mode = (params->vi_bitrate.mode == V4L2_BITRATE_VBR) ? 382 V4L2_MPEG_VIDEO_BITRATE_MODE_VBR : V4L2_MPEG_VIDEO_BITRATE_MODE_CBR; 383 } 384 if (params->vi_bitrate.mode != V4L2_BITRATE_NONE) 385 h->old_params.st_bitrate.target = params->st_bitrate.target; 386 if (params->vi_bitrate.mode != V4L2_BITRATE_NONE) 387 h->old_params.vi_bitrate.target = params->vi_bitrate.target; 388 if (params->vi_bitrate.mode == V4L2_BITRATE_VBR) 389 h->old_params.vi_bitrate.max = params->vi_bitrate.max; 390 if (params->au_bitrate.mode != V4L2_BITRATE_NONE) 391 h->old_params.au_bitrate.target = params->au_bitrate.target; 392 393 /* aspect ratio */ 394 if (params->vi_aspect_ratio == V4L2_MPEG_ASPECT_4_3 || 395 params->vi_aspect_ratio == V4L2_MPEG_ASPECT_16_9) { 396 h->old_params.vi_aspect_ratio = params->vi_aspect_ratio; 397 if (params->vi_aspect_ratio == V4L2_MPEG_ASPECT_4_3) 398 h->params.vi_aspect = V4L2_MPEG_VIDEO_ASPECT_4x3; 399 else 400 h->params.vi_aspect = V4L2_MPEG_VIDEO_ASPECT_16x9; 401 } 402 403 /* range checks */ 404 if (h->old_params.st_bitrate.target > MPEG_TOTAL_TARGET_BITRATE_MAX) 405 h->old_params.st_bitrate.target = MPEG_TOTAL_TARGET_BITRATE_MAX; 406 if (h->old_params.vi_bitrate.target > MPEG_VIDEO_TARGET_BITRATE_MAX) 407 h->old_params.vi_bitrate.target = MPEG_VIDEO_TARGET_BITRATE_MAX; 408 if (h->old_params.vi_bitrate.max > MPEG_VIDEO_MAX_BITRATE_MAX) 409 h->old_params.vi_bitrate.max = MPEG_VIDEO_MAX_BITRATE_MAX; 410 h->params.vi_bitrate = params->vi_bitrate.target; 411 h->params.vi_bitrate_peak = params->vi_bitrate.max; 412 if (h->old_params.au_bitrate.target <= 256) { 413 h->old_params.au_bitrate.target = 256; 414 h->params.au_l2_bitrate = V4L2_MPEG_AUDIO_L2_BITRATE_256K; 415 } 416 else { 417 h->old_params.au_bitrate.target = 384; 418 h->params.au_l2_bitrate = V4L2_MPEG_AUDIO_L2_BITRATE_384K; 419 } 420} 421 422static int handle_ctrl(struct saa6752hs_mpeg_params *params, 423 struct v4l2_ext_control *ctrl, unsigned int cmd) 424{ 425 int old = 0, new; 426 int set = (cmd == VIDIOC_S_EXT_CTRLS); 427 428 new = ctrl->value; 429 switch (ctrl->id) { 430 case V4L2_CID_MPEG_STREAM_TYPE: 431 old = V4L2_MPEG_STREAM_TYPE_MPEG2_TS; 432 if (set && new != old) 433 return -ERANGE; 434 new = old; 435 break; 436 case V4L2_CID_MPEG_STREAM_PID_PMT: 437 old = params->ts_pid_pmt; 438 if (set && new > MPEG_PID_MAX) 439 return -ERANGE; 440 if (new > MPEG_PID_MAX) 441 new = MPEG_PID_MAX; 442 params->ts_pid_pmt = new; 443 break; 444 case V4L2_CID_MPEG_STREAM_PID_AUDIO: 445 old = params->ts_pid_audio; 446 if (set && new > MPEG_PID_MAX) 447 return -ERANGE; 448 if (new > MPEG_PID_MAX) 449 new = MPEG_PID_MAX; 450 params->ts_pid_audio = new; 451 break; 452 case V4L2_CID_MPEG_STREAM_PID_VIDEO: 453 old = params->ts_pid_video; 454 if (set && new > MPEG_PID_MAX) 455 return -ERANGE; 456 if (new > MPEG_PID_MAX) 457 new = MPEG_PID_MAX; 458 params->ts_pid_video = new; 459 break; 460 case V4L2_CID_MPEG_STREAM_PID_PCR: 461 old = params->ts_pid_pcr; 462 if (set && new > MPEG_PID_MAX) 463 return -ERANGE; 464 if (new > MPEG_PID_MAX) 465 new = MPEG_PID_MAX; 466 params->ts_pid_pcr = new; 467 break; 468 case V4L2_CID_MPEG_AUDIO_ENCODING: 469 old = V4L2_MPEG_AUDIO_ENCODING_LAYER_2; 470 if (set && new != old) 471 return -ERANGE; 472 new = old; 473 break; 474 case V4L2_CID_MPEG_AUDIO_L2_BITRATE: 475 old = params->au_l2_bitrate; 476 if (set && new != V4L2_MPEG_AUDIO_L2_BITRATE_256K && 477 new != V4L2_MPEG_AUDIO_L2_BITRATE_384K) 478 return -ERANGE; 479 if (new <= V4L2_MPEG_AUDIO_L2_BITRATE_256K) 480 new = V4L2_MPEG_AUDIO_L2_BITRATE_256K; 481 else 482 new = V4L2_MPEG_AUDIO_L2_BITRATE_384K; 483 params->au_l2_bitrate = new; 484 break; 485 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ: 486 old = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000; 487 if (set && new != old) 488 return -ERANGE; 489 new = old; 490 break; 491 case V4L2_CID_MPEG_VIDEO_ENCODING: 492 old = V4L2_MPEG_VIDEO_ENCODING_MPEG_2; 493 if (set && new != old) 494 return -ERANGE; 495 new = old; 496 break; 497 case V4L2_CID_MPEG_VIDEO_ASPECT: 498 old = params->vi_aspect; 499 if (set && new != V4L2_MPEG_VIDEO_ASPECT_16x9 && 500 new != V4L2_MPEG_VIDEO_ASPECT_4x3) 501 return -ERANGE; 502 if (new != V4L2_MPEG_VIDEO_ASPECT_16x9) 503 new = V4L2_MPEG_VIDEO_ASPECT_4x3; 504 params->vi_aspect = new; 505 break; 506 case V4L2_CID_MPEG_VIDEO_BITRATE: 507 old = params->vi_bitrate * 1000; 508 new = 1000 * (new / 1000); 509 if (set && new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000) 510 return -ERANGE; 511 if (new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000) 512 new = MPEG_VIDEO_TARGET_BITRATE_MAX * 1000; 513 params->vi_bitrate = new / 1000; 514 break; 515 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: 516 old = params->vi_bitrate_peak * 1000; 517 new = 1000 * (new / 1000); 518 if (set && new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000) 519 return -ERANGE; 520 if (new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000) 521 new = MPEG_VIDEO_TARGET_BITRATE_MAX * 1000; 522 params->vi_bitrate_peak = new / 1000; 523 break; 524 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: 525 old = params->vi_bitrate_mode; 526 params->vi_bitrate_mode = new; 527 break; 528 default: 529 return -EINVAL; 530 } 531 if (cmd == VIDIOC_G_EXT_CTRLS) 532 ctrl->value = old; 533 else 534 ctrl->value = new; 535 return 0; 536} 537 538static int saa6752hs_init(struct i2c_client* client) 539{ 540 unsigned char buf[9], buf2[4]; 541 struct saa6752hs_state *h; 542 u32 crc; 543 unsigned char localPAT[256]; 544 unsigned char localPMT[256]; 545 546 h = i2c_get_clientdata(client); 547 548 /* Set video format - must be done first as it resets other settings */ 549 buf[0] = 0x41; 550 buf[1] = h->video_format; 551 i2c_master_send(client, buf, 2); 552 553 /* Set number of lines in input signal */ 554 buf[0] = 0x40; 555 buf[1] = 0x00; 556 if (h->standard & V4L2_STD_525_60) 557 buf[1] = 0x01; 558 i2c_master_send(client, buf, 2); 559 560 /* set bitrate */ 561 saa6752hs_set_bitrate(client, &h->params); 562 563 /* Set GOP structure {3, 13} */ 564 buf[0] = 0x72; 565 buf[1] = 0x03; 566 buf[2] = 0x0D; 567 i2c_master_send(client,buf,3); 568 569 /* Set minimum Q-scale {4} */ 570 buf[0] = 0x82; 571 buf[1] = 0x04; 572 i2c_master_send(client,buf,2); 573 574 /* Set maximum Q-scale {12} */ 575 buf[0] = 0x83; 576 buf[1] = 0x0C; 577 i2c_master_send(client,buf,2); 578 579 /* Set Output Protocol */ 580 buf[0] = 0xD0; 581 buf[1] = 0x81; 582 i2c_master_send(client,buf,2); 583 584 /* Set video output stream format {TS} */ 585 buf[0] = 0xB0; 586 buf[1] = 0x05; 587 i2c_master_send(client,buf,2); 588 589 /* compute PAT */ 590 memcpy(localPAT, PAT, sizeof(PAT)); 591 localPAT[17] = 0xe0 | ((h->params.ts_pid_pmt >> 8) & 0x0f); 592 localPAT[18] = h->params.ts_pid_pmt & 0xff; 593 crc = crc32_be(~0, &localPAT[7], sizeof(PAT) - 7 - 4); 594 localPAT[sizeof(PAT) - 4] = (crc >> 24) & 0xFF; 595 localPAT[sizeof(PAT) - 3] = (crc >> 16) & 0xFF; 596 localPAT[sizeof(PAT) - 2] = (crc >> 8) & 0xFF; 597 localPAT[sizeof(PAT) - 1] = crc & 0xFF; 598 599 /* compute PMT */ 600 memcpy(localPMT, PMT, sizeof(PMT)); 601 localPMT[3] = 0x40 | ((h->params.ts_pid_pmt >> 8) & 0x0f); 602 localPMT[4] = h->params.ts_pid_pmt & 0xff; 603 localPMT[15] = 0xE0 | ((h->params.ts_pid_pcr >> 8) & 0x0F); 604 localPMT[16] = h->params.ts_pid_pcr & 0xFF; 605 localPMT[20] = 0xE0 | ((h->params.ts_pid_video >> 8) & 0x0F); 606 localPMT[21] = h->params.ts_pid_video & 0xFF; 607 localPMT[25] = 0xE0 | ((h->params.ts_pid_audio >> 8) & 0x0F); 608 localPMT[26] = h->params.ts_pid_audio & 0xFF; 609 crc = crc32_be(~0, &localPMT[7], sizeof(PMT) - 7 - 4); 610 localPMT[sizeof(PMT) - 4] = (crc >> 24) & 0xFF; 611 localPMT[sizeof(PMT) - 3] = (crc >> 16) & 0xFF; 612 localPMT[sizeof(PMT) - 2] = (crc >> 8) & 0xFF; 613 localPMT[sizeof(PMT) - 1] = crc & 0xFF; 614 615 /* Set Audio PID */ 616 buf[0] = 0xC1; 617 buf[1] = (h->params.ts_pid_audio >> 8) & 0xFF; 618 buf[2] = h->params.ts_pid_audio & 0xFF; 619 i2c_master_send(client,buf,3); 620 621 /* Set Video PID */ 622 buf[0] = 0xC0; 623 buf[1] = (h->params.ts_pid_video >> 8) & 0xFF; 624 buf[2] = h->params.ts_pid_video & 0xFF; 625 i2c_master_send(client,buf,3); 626 627 /* Set PCR PID */ 628 buf[0] = 0xC4; 629 buf[1] = (h->params.ts_pid_pcr >> 8) & 0xFF; 630 buf[2] = h->params.ts_pid_pcr & 0xFF; 631 i2c_master_send(client,buf,3); 632 633 /* Send SI tables */ 634 i2c_master_send(client,localPAT,sizeof(PAT)); 635 i2c_master_send(client,localPMT,sizeof(PMT)); 636 637 /* mute then unmute audio. This removes buzzing artefacts */ 638 buf[0] = 0xa4; 639 buf[1] = 1; 640 i2c_master_send(client, buf, 2); 641 buf[1] = 0; 642 i2c_master_send(client, buf, 2); 643 644 /* start it going */ 645 saa6752hs_chip_command(client, SAA6752HS_COMMAND_START); 646 647 /* readout current state */ 648 buf[0] = 0xE1; 649 buf[1] = 0xA7; 650 buf[2] = 0xFE; 651 buf[3] = 0x82; 652 buf[4] = 0xB0; 653 i2c_master_send(client, buf, 5); 654 i2c_master_recv(client, buf2, 4); 655 656 /* change aspect ratio */ 657 buf[0] = 0xE0; 658 buf[1] = 0xA7; 659 buf[2] = 0xFE; 660 buf[3] = 0x82; 661 buf[4] = 0xB0; 662 buf[5] = buf2[0]; 663 switch(h->params.vi_aspect) { 664 case V4L2_MPEG_VIDEO_ASPECT_16x9: 665 buf[6] = buf2[1] | 0x40; 666 break; 667 case V4L2_MPEG_VIDEO_ASPECT_4x3: 668 default: 669 buf[6] = buf2[1] & 0xBF; 670 break; 671 break; 672 } 673 buf[7] = buf2[2]; 674 buf[8] = buf2[3]; 675 i2c_master_send(client, buf, 9); 676 677 return 0; 678} 679 680static int saa6752hs_attach(struct i2c_adapter *adap, int addr, int kind) 681{ 682 struct saa6752hs_state *h; 683 684 685 if (NULL == (h = kzalloc(sizeof(*h), GFP_KERNEL))) 686 return -ENOMEM; 687 h->client = client_template; 688 h->params = param_defaults; 689 h->old_params = old_param_defaults; 690 h->client.adapter = adap; 691 h->client.addr = addr; 692 693 /* Assume 625 input lines */ 694 h->standard = 0; 695 696 i2c_set_clientdata(&h->client, h); 697 i2c_attach_client(&h->client); 698 699 v4l_info(&h->client,"saa6752hs: chip found @ 0x%x\n", addr<<1); 700 701 return 0; 702} 703 704static int saa6752hs_probe(struct i2c_adapter *adap) 705{ 706 if (adap->class & I2C_CLASS_TV_ANALOG) 707 return i2c_probe(adap, &addr_data, saa6752hs_attach); 708 return 0; 709} 710 711static int saa6752hs_detach(struct i2c_client *client) 712{ 713 struct saa6752hs_state *h; 714 715 h = i2c_get_clientdata(client); 716 i2c_detach_client(client); 717 kfree(h); 718 return 0; 719} 720 721static int 722saa6752hs_command(struct i2c_client *client, unsigned int cmd, void *arg) 723{ 724 struct saa6752hs_state *h = i2c_get_clientdata(client); 725 struct v4l2_ext_controls *ctrls = arg; 726 struct v4l2_mpeg_compression *old_params = arg; 727 struct saa6752hs_mpeg_params params; 728 int err = 0; 729 int i; 730 731 switch (cmd) { 732 case VIDIOC_S_MPEGCOMP: 733 if (NULL == old_params) { 734 /* apply settings and start encoder */ 735 saa6752hs_init(client); 736 break; 737 } 738 saa6752hs_old_set_params(client, old_params); 739 /* fall through */ 740 case VIDIOC_G_MPEGCOMP: 741 *old_params = h->old_params; 742 break; 743 case VIDIOC_S_EXT_CTRLS: 744 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG) 745 return -EINVAL; 746 if (ctrls->count == 0) { 747 /* apply settings and start encoder */ 748 saa6752hs_init(client); 749 break; 750 } 751 /* fall through */ 752 case VIDIOC_TRY_EXT_CTRLS: 753 case VIDIOC_G_EXT_CTRLS: 754 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG) 755 return -EINVAL; 756 params = h->params; 757 for (i = 0; i < ctrls->count; i++) { 758 if ((err = handle_ctrl(¶ms, ctrls->controls + i, cmd))) { 759 ctrls->error_idx = i; 760 return err; 761 } 762 } 763 h->params = params; 764 break; 765 case VIDIOC_G_FMT: 766 { 767 struct v4l2_format *f = arg; 768 769 if (h->video_format == SAA6752HS_VF_UNKNOWN) 770 h->video_format = SAA6752HS_VF_D1; 771 f->fmt.pix.width = 772 v4l2_format_table[h->video_format].fmt.pix.width; 773 f->fmt.pix.height = 774 v4l2_format_table[h->video_format].fmt.pix.height; 775 break ; 776 } 777 case VIDIOC_S_FMT: 778 { 779 struct v4l2_format *f = arg; 780 781 saa6752hs_set_subsampling(client, f); 782 break; 783 } 784 case VIDIOC_S_STD: 785 h->standard = *((v4l2_std_id *) arg); 786 break; 787 default: 788 /* nothing */ 789 break; 790 } 791 792 return err; 793} 794 795/* ----------------------------------------------------------------------- */ 796 797static struct i2c_driver driver = { 798 .driver = { 799 .name = "saa6752hs", 800 }, 801 .id = I2C_DRIVERID_SAA6752HS, 802 .attach_adapter = saa6752hs_probe, 803 .detach_client = saa6752hs_detach, 804 .command = saa6752hs_command, 805}; 806 807static struct i2c_client client_template = 808{ 809 .name = "saa6752hs", 810 .driver = &driver, 811}; 812 813static int __init saa6752hs_init_module(void) 814{ 815 return i2c_add_driver(&driver); 816} 817 818static void __exit saa6752hs_cleanup_module(void) 819{ 820 i2c_del_driver(&driver); 821} 822 823module_init(saa6752hs_init_module); 824module_exit(saa6752hs_cleanup_module); 825 826/* 827 * Overrides for Emacs so that we follow Linus's tabbing style. 828 * --------------------------------------------------------------------------- 829 * Local variables: 830 * c-basic-offset: 8 831 * End: 832 */ 833