diff options
author | Gerd Knorr <devnull@localhost> | 2004-11-10 11:07:24 +0000 |
---|---|---|
committer | Gerd Knorr <devnull@localhost> | 2004-11-10 11:07:24 +0000 |
commit | 2acc4a76d33deb986d81d45c29eb1a2b6e97b020 (patch) | |
tree | 7e57109c66f4f589a0a7a2d60eac6c6ae43f0e4d /linux/drivers/media/video/saa6752hs.c | |
parent | 7defb807a53a4d8c71e434e31f73d58cbf195cdc (diff) | |
download | mediapointer-dvb-s2-2acc4a76d33deb986d81d45c29eb1a2b6e97b020.tar.gz mediapointer-dvb-s2-2acc4a76d33deb986d81d45c29eb1a2b6e97b020.tar.bz2 |
- lots of empress stuff from Andrew de Quincey + some rewrites from me.
- misc fixes.
Diffstat (limited to 'linux/drivers/media/video/saa6752hs.c')
-rw-r--r-- | linux/drivers/media/video/saa6752hs.c | 287 |
1 files changed, 191 insertions, 96 deletions
diff --git a/linux/drivers/media/video/saa6752hs.c b/linux/drivers/media/video/saa6752hs.c index 915d418bd..6d3b8b4d1 100644 --- a/linux/drivers/media/video/saa6752hs.c +++ b/linux/drivers/media/video/saa6752hs.c @@ -11,6 +11,7 @@ #include <linux/types.h> #include <linux/videodev.h> #include <linux/init.h> +#include <linux/crc32.h> #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0) #include "i2c-compat.h" @@ -34,6 +35,10 @@ MODULE_LICENSE("GPL"); static struct i2c_driver driver; static struct i2c_client client_template; +struct saa6752hs_state { + struct i2c_client client; + struct v4l2_mpeg_compression params; +}; enum saa6752hs_command { SAA6752HS_COMMAND_RESET = 0, @@ -47,7 +52,6 @@ enum saa6752hs_command { SAA6752HS_COMMAND_MAX }; - /* ---------------------------------------------------------------------- */ static u8 PAT[] = { @@ -71,9 +75,9 @@ static u8 PAT[] = { 0x00, 0x01, // program_number(1) - 0xe0, 0x10, // PMT PID(0x10) + 0xe0, 0x00, // PMT PID - 0x76, 0xf1, 0x44, 0xd1 // CRC32 + 0x00, 0x00, 0x00, 0x00 // CRC32 }; static u8 PMT[] = { @@ -81,7 +85,7 @@ static u8 PMT[] = { 0x01, // table number for encoder 0x47, // sync - 0x40, 0x10, // transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid(0x10) + 0x40, 0x00, // transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid 0x10, // transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) 0x00, // PSI pointer to start of table @@ -95,28 +99,50 @@ static u8 PMT[] = { 0x00, 0x00, // section_number(0), last_section_number(0) - 0xe1, 0x04, // PCR_PID (0x104) + 0xe0, 0x00, // PCR_PID 0xf0, 0x00, // program_info_length(0) - 0x02, 0xe1, 0x00, 0xf0, 0x00, // video stream type(2), pid(0x100) - 0x04, 0xe1, 0x03, 0xf0, 0x00, // audio stream type(4), pid(0x103) + 0x02, 0xe0, 0x00, 0xf0, 0x00, // video stream type(2), pid + 0x04, 0xe0, 0x00, 0xf0, 0x00, // audio stream type(4), pid - 0xa1, 0xca, 0x0f, 0x82 // CRC32 + 0x00, 0x00, 0x00, 0x00 // CRC32 }; -static struct mpeg_params mpeg_params_template = +static struct v4l2_mpeg_compression param_defaults = { - .bitrate_mode = MPEG_BITRATE_MODE_CBR, - .video_target_bitrate = 5000, - .audio_bitrate = MPEG_AUDIO_BITRATE_256, - .total_bitrate = 6000, + .st_type = V4L2_MPEG_TS_2, + .st_bitrate = { + .mode = V4L2_BITRATE_CBR, + .target = 7000, + }, + + .ts_pid_pmt = 16, + .ts_pid_video = 260, + .ts_pid_audio = 256, + .ts_pid_pcr = 259, + + .vi_type = V4L2_MPEG_VI_2, + .vi_bitrate = { + .mode = V4L2_BITRATE_VBR, + .target = 4000, + .max = 6000, + }, + + .au_type = V4L2_MPEG_AU_2_II, + .au_bitrate = { + .mode = V4L2_BITRATE_CBR, + .target = 256, + }, + +#if 0 + /* FIXME: size? via S_FMT? */ + .video_format = MPEG_VIDEO_FORMAT_D1, +#endif }; - /* ---------------------------------------------------------------------- */ - static int saa6752hs_chip_command(struct i2c_client* client, enum saa6752hs_command command) { @@ -131,7 +157,7 @@ static int saa6752hs_chip_command(struct i2c_client* client, break; case SAA6752HS_COMMAND_STOP: - buf[0] = 0x03; + buf[0] = 0x03; break; case SAA6752HS_COMMAND_START: @@ -187,74 +213,129 @@ static int saa6752hs_chip_command(struct i2c_client* client, static int saa6752hs_set_bitrate(struct i2c_client* client, - struct mpeg_params* params) + struct v4l2_mpeg_compression* params) { u8 buf[3]; // set the bitrate mode buf[0] = 0x71; - buf[1] = params->bitrate_mode; + buf[1] = (params->vi_bitrate.mode == V4L2_BITRATE_VBR) ? 0 : 1; i2c_master_send(client, buf, 2); // set the video bitrate - if (params->bitrate_mode == MPEG_BITRATE_MODE_VBR) { + if (params->vi_bitrate.mode == V4L2_BITRATE_VBR) { // set the target bitrate buf[0] = 0x80; - buf[1] = params->video_target_bitrate >> 8; - buf[2] = params->video_target_bitrate & 0xff; + buf[1] = params->vi_bitrate.target >> 8; + buf[2] = params->vi_bitrate.target & 0xff; i2c_master_send(client, buf, 3); // set the max bitrate buf[0] = 0x81; - buf[1] = params->video_max_bitrate >> 8; - buf[2] = params->video_max_bitrate & 0xff; + buf[1] = params->vi_bitrate.max >> 8; + buf[2] = params->vi_bitrate.max & 0xff; i2c_master_send(client, buf, 3); } else { // set the target bitrate (no max bitrate for CBR) buf[0] = 0x81; - buf[1] = params->video_target_bitrate >> 8; - buf[2] = params->video_target_bitrate & 0xff; + buf[1] = params->vi_bitrate.target >> 8; + buf[2] = params->vi_bitrate.target & 0xff; i2c_master_send(client, buf, 3); } // set the audio bitrate buf[0] = 0x94; - buf[1] = params->audio_bitrate; + buf[1] = params->au_bitrate.target; i2c_master_send(client, buf, 2); // set the total bitrate buf[0] = 0xb1; - buf[1] = params->total_bitrate >> 8; - buf[2] = params->total_bitrate & 0xff; + buf[1] = params->st_bitrate.target >> 8; + buf[2] = params->st_bitrate.target & 0xff; i2c_master_send(client, buf, 3); + // return success return 0; } -static int saa6752hs_init(struct i2c_client* client, struct mpeg_params* params) +static int saa6752hs_init(struct i2c_client* client, + struct v4l2_mpeg_compression* params) { unsigned char buf[3]; - void *data; + struct saa6752hs_state *h; + u32 crc; + unsigned char localPAT[256]; + unsigned char localPMT[256]; + + // grab the previous set of parameters + h = i2c_get_clientdata(client); + +#if 0 /* TODO: verify parameters and update h->params */ // check the bitrate parameters first if (params != NULL) { - if (params->bitrate_mode >= MPEG_BITRATE_MODE_MAX) + unsigned int pmt_pid = 0, pcr_pid = 0, video_pid = 0 , audio_pid = 0; + + // check supplied params are valid + if (params->video_format >= MPEG_VIDEO_FORMAT_MAX) return -EINVAL; - if (params->video_target_bitrate >= MPEG_VIDEO_TARGET_BITRATE_MAX) + if (params->video_bitrate_mode >= MPEG_VIDEO_BITRATE_MODE_MAX) return -EINVAL; - if (params->video_max_bitrate >= MPEG_VIDEO_MAX_BITRATE_MAX) + if (params->video_target_bitrate >= MPEG_VIDEO_TARGET_BITRATE_MAX) return -EINVAL; if (params->audio_bitrate >= MPEG_AUDIO_BITRATE_MAX) return -EINVAL; if (params->total_bitrate >= MPEG_TOTAL_BITRATE_MAX) return -EINVAL; - if (params->bitrate_mode == MPEG_BITRATE_MODE_MAX && - params->video_target_bitrate <= params->video_max_bitrate) - return -EINVAL; + if (params->video_bitrate_mode == MPEG_VIDEO_BITRATE_MODE_VBR) { + if (params->video_max_bitrate >= MPEG_VIDEO_MAX_BITRATE_MAX) + return -EINVAL; + if (params->video_target_bitrate >= params->video_max_bitrate) + return -EINVAL; + } + if (params->pmt_pid != 0) { + if (params->pmt_pid > MPEG_PID_MAX) + return -EINVAL; + pmt_pid = params->pmt_pid; + } else { + pmt_pid = cachedParams->pmt_pid; + } + if (params->pcr_pid != 0) { + if (params->pcr_pid > MPEG_PID_MAX) + return -EINVAL; + pcr_pid = params->pcr_pid; + } else { + pcr_pid = cachedParams->pcr_pid; + } + if (params->video_pid != 0) { + if (params->video_pid > MPEG_PID_MAX) + return -EINVAL; + video_pid = params->video_pid; + } else { + video_pid = cachedParams->video_pid; + } + if (params->audio_pid != 0) { + if (params->audio_pid > MPEG_PID_MAX) + return -EINVAL; + audio_pid = params->audio_pid; + } else { + audio_pid = cachedParams->audio_pid; + } + + // update cache + memcpy(cachedParams, params, sizeof(struct mpeg_params)); + cachedParams->pmt_pid = pmt_pid; + cachedParams->pcr_pid = pcr_pid; + cachedParams->video_pid = video_pid; + cachedParams->audio_pid = audio_pid; } +#endif + + // set bitrate + saa6752hs_set_bitrate(client, &h->params); - // Set GOP structure {3, 13} + // Set GOP structure {3, 13} buf[0] = 0x72; buf[1] = 0x03; buf[2] = 0x0D; @@ -280,25 +361,53 @@ static int saa6752hs_init(struct i2c_client* client, struct mpeg_params* params) buf[1] = 0x05; i2c_master_send(client,buf,2); - // Set Audio PID {0x103} + /* compute PAT */ + memcpy(localPAT, PAT, sizeof(PAT)); + localPAT[17] = 0xe0 | ((h->params.ts_pid_pmt >> 8) & 0x0f); + localPAT[18] = h->params.ts_pid_pmt & 0xff; + crc = crc32_be(~0, &localPAT[7], sizeof(PAT) - 7 - 4); + localPAT[sizeof(PAT) - 4] = (crc >> 24) & 0xFF; + localPAT[sizeof(PAT) - 3] = (crc >> 16) & 0xFF; + localPAT[sizeof(PAT) - 2] = (crc >> 8) & 0xFF; + localPAT[sizeof(PAT) - 1] = crc & 0xFF; + + /* compute PMT */ + memcpy(localPMT, PMT, sizeof(PMT)); + localPMT[3] = 0x40 | ((h->params.ts_pid_pmt >> 8) & 0x0f); + localPMT[4] = h->params.ts_pid_pmt & 0xff; + localPMT[15] = 0xE0 | ((h->params.ts_pid_pcr >> 8) & 0x0F); + localPMT[16] = h->params.ts_pid_pcr & 0xFF; + localPMT[20] = 0xE0 | ((h->params.ts_pid_video >> 8) & 0x0F); + localPMT[21] = h->params.ts_pid_video & 0xFF; + localPMT[25] = 0xE0 | ((h->params.ts_pid_audio >> 8) & 0x0F); + localPMT[26] = h->params.ts_pid_audio & 0xFF; + crc = crc32_be(~0, &localPMT[7], sizeof(PMT) - 7 - 4); + localPMT[sizeof(PMT) - 4] = (crc >> 24) & 0xFF; + localPMT[sizeof(PMT) - 3] = (crc >> 16) & 0xFF; + localPMT[sizeof(PMT) - 2] = (crc >> 8) & 0xFF; + localPMT[sizeof(PMT) - 1] = crc & 0xFF; + + // Set Audio PID buf[0] = 0xC1; - buf[1] = 0x01; - buf[2] = 0x03; + buf[1] = (h->params.ts_pid_audio >> 8) & 0xFF; + buf[2] = h->params.ts_pid_audio & 0xFF; i2c_master_send(client,buf,3); - // setup bitrate settings - data = i2c_get_clientdata(client); - if (params) { - saa6752hs_set_bitrate(client, params); - memcpy(data, params, sizeof(struct mpeg_params)); - } else { - // parameters were not supplied. use the previous set - saa6752hs_set_bitrate(client, (struct mpeg_params*) data); - } + // Set Video PID + buf[0] = 0xC0; + buf[1] = (h->params.ts_pid_video >> 8) & 0xFF; + buf[2] = h->params.ts_pid_video & 0xFF; + i2c_master_send(client,buf,3); + + // Set PCR PID + buf[0] = 0xC4; + buf[1] = (h->params.ts_pid_pcr >> 8) & 0xFF; + buf[2] = h->params.ts_pid_pcr & 0xFF; + i2c_master_send(client,buf,3); // Send SI tables - i2c_master_send(client,PAT,sizeof(PAT)); - i2c_master_send(client,PMT,sizeof(PMT)); + i2c_master_send(client,localPAT,sizeof(PAT)); + i2c_master_send(client,localPMT,sizeof(PMT)); // mute then unmute audio. This removes buzzing artefacts buf[0] = 0xa4; @@ -307,97 +416,83 @@ static int saa6752hs_init(struct i2c_client* client, struct mpeg_params* params) buf[1] = 0; i2c_master_send(client, buf, 2); + // Set video format + buf[0] = 0x41; + buf[1] = 0 /* MPEG_VIDEO_FORMAT_D1 */; + i2c_master_send(client, buf, 2); + // start it going saa6752hs_chip_command(client, SAA6752HS_COMMAND_START); + // return success return 0; } -#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0) static int saa6752hs_attach(struct i2c_adapter *adap, int addr, int kind) -#else -static int saa6752hs_attach(struct i2c_adapter *adap, int addr, - unsigned short flags, int kind) -#endif { - struct i2c_client *client; - struct mpeg_params* params; - - client_template.adapter = adap; - client_template.addr = addr; + struct saa6752hs_state *h; printk("saa6752hs: chip found @ 0x%x\n", addr<<1); - if (NULL == (client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL))) + if (NULL == (h = kmalloc(sizeof(*h), GFP_KERNEL))) return -ENOMEM; - memcpy(client,&client_template,sizeof(struct i2c_client)); - strlcpy(client->name, "saa6752hs", sizeof(client->name)); - - if (NULL == (params = kmalloc(sizeof(struct mpeg_params), GFP_KERNEL))) - return -ENOMEM; - memcpy(params,&mpeg_params_template,sizeof(struct mpeg_params)); - i2c_set_clientdata(client, params); + memset(h,0,sizeof(*h)); + h->client = client_template; + h->params = param_defaults; + h->client.adapter = adap; + h->client.addr = addr; - i2c_attach_client(client); + i2c_set_clientdata(&h->client, h); + i2c_attach_client(&h->client); -#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0) - MOD_INC_USE_COUNT; -#endif + saa6752hs_init(&h->client, NULL); return 0; } static int saa6752hs_probe(struct i2c_adapter *adap) { -#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0) if (adap->class & I2C_CLASS_TV_ANALOG) return i2c_probe(adap, &addr_data, saa6752hs_attach); -#else - switch (adap->id) { - case I2C_ALGO_SAA7134: - return i2c_probe(adap, &addr_data, saa6752hs_attach); - break; - } -#endif return 0; } static int saa6752hs_detach(struct i2c_client *client) { - void *data; + struct saa6752hs_state *h; - data = i2c_get_clientdata(client); + h = i2c_get_clientdata(client); i2c_detach_client(client); - kfree(data); - kfree(client); -#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0) - MOD_DEC_USE_COUNT; -#endif + kfree(h); return 0; } static int saa6752hs_command(struct i2c_client *client, unsigned int cmd, void *arg) { - struct mpeg_params* init_arg = arg; + struct saa6752hs_state *h = i2c_get_clientdata(client); + struct v4l2_mpeg_compression *params = arg; + int err = 0; switch (cmd) { - case MPEG_SETPARAMS: - return saa6752hs_init(client, init_arg); - + case VIDIOC_S_MPEGCOMP: + err = saa6752hs_init(client, params); + /* fall through */ + case VIDIOC_G_MPEGCOMP: + if (params) + *params = h->params; + break; default: /* nothing */ break; } - return 0; + return err; } /* ----------------------------------------------------------------------- */ static struct i2c_driver driver = { -#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,54) .owner = THIS_MODULE, -#endif .name = "i2c saa6752hs MPEG encoder", .id = I2C_DRIVERID_SAA6752HS, .flags = I2C_DF_NOTIFY, @@ -408,7 +503,7 @@ static struct i2c_driver driver = { static struct i2c_client client_template = { - I2C_DEVNAME("(saa6752hs unset)"), + I2C_DEVNAME("saa6752hs"), .flags = I2C_CLIENT_ALLOW_USE, .driver = &driver, }; |