27 #include <linux/module.h>
32 #include <linux/device.h>
34 #include <linux/slab.h>
42 #define CX23885_FIRM_IMAGE_SIZE 376836
43 #define CX23885_FIRM_IMAGE_NAME "v4l-cx23885-enc.fw"
45 static unsigned int mpegbufs = 32;
48 static unsigned int mpeglines = 32;
50 MODULE_PARM_DESC(mpeglines,
"number of lines in an MPEG buffer, range 2-32");
51 static unsigned int mpeglinesize = 512;
54 "number of bytes in each line of an MPEG buffer, range 512-1024");
56 static unsigned int v4l_debug;
60 #define dprintk(level, fmt, arg...)\
61 do { if (v4l_debug >= level) \
62 printk(KERN_DEBUG "%s: " fmt, \
63 (dev) ? dev->name : "cx23885[?]", ## arg); \
208 #define IVTV_CMD_HW_BLOCKS_RST 0xFFFFFFFF
211 #define IVTV_API_STD_TIMEOUT 500
215 #define IVTV_REG_ENC_SDRAM_REFRESH (0x07F8)
216 #define IVTV_REG_ENC_SDRAM_PRECHARGE (0x07FC)
217 #define IVTV_REG_SPU (0x9050)
218 #define IVTV_REG_HW_BLOCKS (0x9054)
219 #define IVTV_REG_VPU (0x9058)
220 #define IVTV_REG_APU (0xA064)
236 #define MC417_MIWR 0x8000
237 #define MC417_MIRD 0x4000
238 #define MC417_MICS 0x2000
239 #define MC417_MIRDY 0x1000
240 #define MC417_MIADDR 0x0F00
241 #define MC417_MIDATA 0x00FF
244 #define MCI_MEMORY_DATA_BYTE0 0x000
245 #define MCI_MEMORY_DATA_BYTE1 0x100
246 #define MCI_MEMORY_DATA_BYTE2 0x200
247 #define MCI_MEMORY_DATA_BYTE3 0x300
248 #define MCI_MEMORY_ADDRESS_BYTE2 0x400
249 #define MCI_MEMORY_ADDRESS_BYTE1 0x500
250 #define MCI_MEMORY_ADDRESS_BYTE0 0x600
251 #define MCI_REGISTER_DATA_BYTE0 0x800
252 #define MCI_REGISTER_DATA_BYTE1 0x900
253 #define MCI_REGISTER_DATA_BYTE2 0xA00
254 #define MCI_REGISTER_DATA_BYTE3 0xB00
255 #define MCI_REGISTER_ADDRESS_BYTE0 0xC00
256 #define MCI_REGISTER_ADDRESS_BYTE1 0xD00
257 #define MCI_REGISTER_MODE 0xE00
260 #define MCI_MODE_REGISTER_READ 0
261 #define MCI_MODE_REGISTER_WRITE 1
262 #define MCI_MODE_MEMORY_READ 0
263 #define MCI_MODE_MEMORY_WRITE 0x40
271 #define MC417_SPD_CTL(x) (((x) << 4) & 0x00000030)
272 #define MC417_GPIO_SEL(x) (((x) << 1) & 0x00000006)
273 #define MC417_UART_GPIO_EN 0x00000001
276 #define MC417_SPD_CTL_SLOW 0x1
277 #define MC417_SPD_CTL_MEDIUM 0x0
278 #define MC417_SPD_CTL_FAST 0x3
281 #define MC417_GPIO_SEL_GPIO3 0x3
282 #define MC417_GPIO_SEL_GPIO2 0x2
283 #define MC417_GPIO_SEL_GPIO1 0x1
284 #define MC417_GPIO_SEL_GPIO0 0x0
290 dprintk(2,
"%s()\n", __func__);
333 (value & 0x000000FF);
342 ((value >> 8) & 0x000000FF);
349 ((value >> 16) & 0x000000FF);
356 ((value >> 24) & 0x000000FF);
370 ((address >> 8) & 0xFF);
383 return mc417_wait_ready(dev);
400 ((address & 0x00FF));
407 ((address >> 8) & 0xFF);
420 retval = mc417_wait_ready(dev);
439 dataval = tempval & 0x000000FF;
451 dataval |= ((tempval & 0x000000FF) << 8);
461 dataval |= ((tempval & 0x000000FF) << 16);
471 dataval |= ((tempval & 0x000000FF) << 24);
491 (value & 0x000000FF);
500 ((value >> 8) & 0x000000FF);
507 ((value >> 16) & 0x000000FF);
514 ((value >> 24) & 0x000000FF);
528 ((address >> 8) & 0xFF);
541 return mc417_wait_ready(dev);
565 ((address >> 8) & 0xFF);
578 retval = mc417_wait_ready(dev);
593 dataval = ((tempval & 0x000000FF) << 24);
605 dataval |= ((tempval & 0x000000FF) << 16);
615 dataval |= ((tempval & 0x000000FF) << 8);
625 dataval |= (tempval & 0x000000FF);
640 val |= (mask & 0x000ffff);
650 val &= ~(mask & 0x0000ffff);
661 val |= (mask & 0x0000ffff);
663 val &= ~(mask & 0x0000ffff);
670 static char *cmd_to_str(
int cmd)
676 return "START_CAPTURE";
678 return "STOP_CAPTURE";
680 return "SET_AUDIO_ID";
682 return "SET_VIDEO_ID";
686 return "SET_FRAME_RATE";
688 return "SET_FRAME_SIZE";
690 return "SET_BIT_RATE";
692 return "SET_GOP_PROPERTIES";
694 return "SET_ASPECT_RATIO";
696 return "SET_DNR_FILTER_MODE";
698 return "SET_DNR_FILTER_PROPS";
700 return "SET_CORING_LEVELS";
702 return "SET_SPATIAL_FILTER_TYPE";
704 return "SET_VBI_LINE";
706 return "SET_STREAM_TYPE";
708 return "SET_OUTPUT_PORT";
710 return "SET_AUDIO_PROPERTIES";
714 return "GET_VERSION";
716 return "SET_GOP_CLOSURE";
718 return "GET_SEQ_END";
720 return "SET_PGM_INDEX_INFO";
722 return "SET_VBI_CONFIG";
724 return "SET_DMA_BLOCK_SIZE";
726 return "GET_PREV_DMA_INFO_MB_10";
728 return "GET_PREV_DMA_INFO_MB_9";
730 return "SCHED_DMA_TO_HOST";
732 return "INITIALIZE_INPUT";
734 return "SET_FRAME_DROP_RATE";
736 return "PAUSE_ENCODER";
738 return "REFRESH_INPUT";
740 return "SET_COPYRIGHT";
742 return "SET_EVENT_NOTIFICATION";
744 return "SET_NUM_VSYNC_LINES";
746 return "SET_PLACEHOLDER";
758 static int cx23885_mbox_func(
void *
priv,
765 unsigned long timeout;
769 dprintk(3,
"%s: command(0x%X) = %s\n", __func__, command,
770 cmd_to_str(command));
775 if (value != 0x12345678) {
777 "Firmware and/or mailbox pointer not initialized "
778 "or corrupted, signature = 0x%x, cmd = %s\n", value,
779 cmd_to_str(command));
789 "(%x), cmd = %s\n", flag, cmd_to_str(command));
801 for (i = 0; i <
in; i++) {
825 for (i = 0; i <
out; i++) {
831 dprintk(3,
"API result = %d\n", retval);
842 static int cx23885_api_cmd(
struct cx23885_dev *dev,
852 dprintk(3,
"%s() cmds = 0x%08x\n", __func__, command);
855 for (i = 0; i < inputcnt; i++)
856 data[i] =
va_arg(vargs,
int);
858 err = cx23885_mbox_func(dev, command, inputcnt, outputcnt, data);
859 for (i = 0; i < outputcnt; i++) {
860 int *vptr =
va_arg(vargs,
int *);
868 static int cx23885_find_mailbox(
struct cx23885_dev *dev)
871 0x12345678, 0x34567812, 0x56781234, 0x78123456
873 int signaturecnt = 0;
877 dprintk(2,
"%s()\n", __func__);
881 if (value == signature[signaturecnt])
885 if (4 == signaturecnt) {
886 dprintk(1,
"Mailbox signature found at 0x%x\n", i+1);
894 static int cx23885_load_firmware(
struct cx23885_dev *dev)
896 static const unsigned char magic[8] = {
897 0xa7, 0x0d, 0x00, 0x00, 0x66, 0xbb, 0x55, 0xaa
907 dprintk(2,
"%s()\n", __func__);
935 "ERROR: Hotplug firmware request failed (%s).\n",
938 "not work without firmware loaded!\n");
942 if (firmware->
size != CX23885_FIRM_IMAGE_SIZE) {
944 "(have %zd, expected %d)\n",
945 firmware->
size, CX23885_FIRM_IMAGE_SIZE);
952 "ERROR: Firmware magic mismatch, wrong file?\n");
958 dprintk(2,
"Loading firmware ...\n");
959 dataptr = (
u32 *)firmware->
data;
960 for (i = 0; i < (firmware->
size >> 2); i++) {
972 dprintk(1,
"Verifying firmware ...\n");
973 for (i--; i >= 0; i--) {
983 "ERROR: Firmware load failed (checksum mismatch).\n");
988 dprintk(1,
"Firmware upload successful.\n");
1019 dprintk(1,
"%s() status = %d, seq = %d\n", __func__, status, seq);
1022 static void cx23885_codec_settings(
struct cx23885_dev *dev)
1024 dprintk(1,
"%s()\n", __func__);
1028 dev->ts1.height = 480;
1030 dev->ts1.height = 576;
1034 dev->ts1.height, dev->ts1.width);
1047 static int cx23885_initialize_codec(
struct cx23885_dev *dev,
int startencoder)
1053 dprintk(1,
"%s()\n", __func__);
1057 dprintk(2,
"%s() PING OK\n", __func__);
1058 retval = cx23885_load_firmware(dev);
1063 retval = cx23885_find_mailbox(dev);
1073 "ERROR: cx23417 firmware ping failed!\n");
1080 "version failed!\n");
1083 dprintk(1,
"cx23417 firmware version is 0x%08x\n", version);
1087 cx23885_codec_settings(dev);
1093 CX23885_CUSTOM_EXTENSION_USR_DATA, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1097 data[0] = 0x0001BD00;
1100 data[3] = 0x91559155;
1101 data[4] = 0x206080C0;
1106 data[2], data[3], data[4], data[5], data[6]);
1108 for (i = 2; i <= 24; i++) {
1111 valid = ((i >= 19) && (i <= 21));
1115 i | 0x80000000, valid, 0, 0, 0);
1141 unsigned int *
count,
unsigned int *
size)
1145 fh->
dev->ts1.ts_packet_size = mpeglinesize;
1146 fh->
dev->ts1.ts_packet_count = mpeglines;
1148 *size = fh->
dev->ts1.ts_packet_size * fh->
dev->ts1.ts_packet_count;
1177 .buf_setup = bb_buf_setup,
1178 .buf_prepare = bb_buf_prepare,
1179 .buf_queue = bb_buf_queue,
1180 .buf_release = bb_buf_release,
1185 static const u32 *ctrl_classes[] = {
1190 static int cx23885_queryctrl(
struct cx23885_dev *dev,
1204 static int cx23885_querymenu(
struct cx23885_dev *dev,
1209 qctrl.
id = qmenu->
id;
1210 cx23885_queryctrl(dev, &qctrl);
1231 for (i = 0; i <
ARRAY_SIZE(cx23885_tvnorms); i++)
1232 if (*
id & cx23885_tvnorms[i].
id)
1246 static int vidioc_enum_input(
struct file *file,
void *priv,
1250 dprintk(1,
"%s()\n", __func__);
1254 static int vidioc_g_input(
struct file *file,
void *priv,
unsigned int *i)
1259 static int vidioc_s_input(
struct file *file,
void *priv,
unsigned int i)
1264 static int vidioc_g_tuner(
struct file *file,
void *priv,
1277 dprintk(1,
"VIDIOC_G_TUNER: tuner type %d\n", t->
type);
1282 static int vidioc_s_tuner(
struct file *file,
void *priv,
1297 static int vidioc_g_frequency(
struct file *file,
void *priv,
1313 static int vidioc_s_frequency(
struct file *file,
void *priv,
1319 static int vidioc_g_ctrl(
struct file *file,
void *priv,
1327 static int vidioc_s_ctrl(
struct file *file,
void *priv,
1335 static int vidioc_querycap(
struct file *file,
void *priv,
1357 static int vidioc_enum_fmt_vid_cap(
struct file *file,
void *priv,
1369 static int vidioc_g_fmt_vid_cap(
struct file *file,
void *priv,
1376 f->
fmt.
pix.bytesperline = 0;
1378 dev->ts1.ts_packet_size * dev->ts1.ts_packet_count;
1379 f->
fmt.
pix.colorspace = 0;
1380 f->
fmt.
pix.width = dev->ts1.width;
1381 f->
fmt.
pix.height = dev->ts1.height;
1383 dprintk(1,
"VIDIOC_G_FMT: w: %d, h: %d, f: %d\n",
1384 dev->ts1.width, dev->ts1.height, fh->
mpegq.field);
1388 static int vidioc_try_fmt_vid_cap(
struct file *file,
void *priv,
1395 f->
fmt.
pix.bytesperline = 0;
1397 dev->ts1.ts_packet_size * dev->ts1.ts_packet_count;
1398 f->
fmt.
pix.colorspace = 0;
1399 dprintk(1,
"VIDIOC_TRY_FMT: w: %d, h: %d, f: %d\n",
1400 dev->ts1.width, dev->ts1.height, fh->
mpegq.field);
1404 static int vidioc_s_fmt_vid_cap(
struct file *file,
void *priv,
1411 f->
fmt.
pix.bytesperline = 0;
1413 dev->ts1.ts_packet_size * dev->ts1.ts_packet_count;
1414 f->
fmt.
pix.colorspace = 0;
1415 dprintk(1,
"VIDIOC_S_FMT: w: %d, h: %d, f: %d\n",
1420 static int vidioc_reqbufs(
struct file *file,
void *priv,
1428 static int vidioc_querybuf(
struct file *file,
void *priv,
1436 static int vidioc_qbuf(
struct file *file,
void *priv,
1444 static int vidioc_dqbuf(
struct file *file,
void *priv,
struct v4l2_buffer *
b)
1452 static int vidioc_streamon(
struct file *file,
void *priv,
1460 static int vidioc_streamoff(
struct file *file,
void *priv,
enum v4l2_buf_type i)
1467 static int vidioc_g_ext_ctrls(
struct file *file,
void *priv,
1478 static int vidioc_s_ext_ctrls(
struct file *file,
void *priv,
1500 static int vidioc_try_ext_ctrls(
struct file *file,
void *priv,
1516 static int vidioc_log_status(
struct file *file,
void *priv)
1524 "%s/2: ============ START LOG STATUS ============\n",
1529 "%s/2: ============= END LOG STATUS =============\n",
1534 static int vidioc_querymenu(
struct file *file,
void *priv,
1540 return cx23885_querymenu(dev, a);
1543 static int vidioc_queryctrl(
struct file *file,
void *priv,
1549 return cx23885_queryctrl(dev, c);
1552 static int mpeg_open(
struct file *file)
1557 dprintk(2,
"%s()\n", __func__);
1576 static int mpeg_release(
struct file *file)
1581 dprintk(2,
"%s()\n", __func__);
1599 if (fh->
mpegq.streaming)
1601 if (fh->
mpegq.reading)
1611 static ssize_t mpeg_read(
struct file *file,
char __user *data,
1612 size_t count, loff_t *ppos)
1617 dprintk(2,
"%s()\n", __func__);
1623 if (cx23885_initialize_codec(dev, 1) < 0)
1632 static unsigned int mpeg_poll(
struct file *file,
1643 static int mpeg_mmap(
struct file *file,
struct vm_area_struct *vma)
1648 dprintk(2,
"%s()\n", __func__);
1656 .release = mpeg_release,
1694 #ifdef CONFIG_VIDEO_ADV_DEBUG
1695 .vidioc_g_register = cx23885_g_register,
1696 .vidioc_s_register = cx23885_s_register,
1703 .ioctl_ops = &mpeg_ioctl_ops,
1710 dprintk(1,
"%s()\n", __func__);
1730 dprintk(1,
"%s()\n", __func__);
1749 dprintk(1,
"%s()\n", __func__);
1762 tsport->
width = 720;
1768 dev->
v4l_device = cx23885_video_dev_alloc(tsport,
1769 dev->
pci, &cx23885_mpeg_template,
"mpeg");
1785 cx23885_initialize_codec(dev, 0);