27 #include <linux/module.h>
32 #include <linux/device.h>
43 #define CX231xx_FIRM_IMAGE_SIZE 376836
44 #define CX231xx_FIRM_IMAGE_NAME "v4l-cx23885-enc.fw"
47 #define ITVC_WRITE_DIR 0x03FDFC00
48 #define ITVC_READ_DIR 0x0001FC00
50 #define MCI_MEMORY_DATA_BYTE0 0x00
51 #define MCI_MEMORY_DATA_BYTE1 0x08
52 #define MCI_MEMORY_DATA_BYTE2 0x10
53 #define MCI_MEMORY_DATA_BYTE3 0x18
55 #define MCI_MEMORY_ADDRESS_BYTE2 0x20
56 #define MCI_MEMORY_ADDRESS_BYTE1 0x28
57 #define MCI_MEMORY_ADDRESS_BYTE0 0x30
59 #define MCI_REGISTER_DATA_BYTE0 0x40
60 #define MCI_REGISTER_DATA_BYTE1 0x48
61 #define MCI_REGISTER_DATA_BYTE2 0x50
62 #define MCI_REGISTER_DATA_BYTE3 0x58
64 #define MCI_REGISTER_ADDRESS_BYTE0 0x60
65 #define MCI_REGISTER_ADDRESS_BYTE1 0x68
67 #define MCI_REGISTER_MODE 0x70
70 #define MCI_MODE_REGISTER_READ 0x000
71 #define MCI_MODE_REGISTER_WRITE 0x100
72 #define MCI_MODE_MEMORY_READ 0x000
73 #define MCI_MODE_MEMORY_WRITE 0x4000
75 static unsigned int mpegbufs = 8;
78 static unsigned int mpeglines = 128;
80 MODULE_PARM_DESC(mpeglines,
"number of lines in an MPEG buffer, range 2-32");
81 static unsigned int mpeglinesize = 512;
84 "number of bytes in each line of an MPEG buffer, range 512-1024");
86 static unsigned int v4l_debug = 1;
90 #define dprintk(level, fmt, arg...)\
91 do { if (v4l_debug >= level) \
92 printk(KERN_INFO "%s: " fmt, \
93 (dev) ? dev->name : "cx231xx[?]", ## arg); \
238 #define IVTV_CMD_HW_BLOCKS_RST 0xFFFFFFFF
241 #define IVTV_API_STD_TIMEOUT 500
245 #define IVTV_REG_ENC_SDRAM_REFRESH (0x07F8)
246 #define IVTV_REG_ENC_SDRAM_PRECHARGE (0x07FC)
247 #define IVTV_REG_SPU (0x9050)
248 #define IVTV_REG_HW_BLOCKS (0x9054)
249 #define IVTV_REG_VPU (0x9058)
250 #define IVTV_REG_APU (0xA064)
268 #define MC417_MIWR 0x8000
269 #define MC417_MIRD 0x4000
270 #define MC417_MICS 0x2000
271 #define MC417_MIRDY 0x1000
272 #define MC417_MIADDR 0x0F00
273 #define MC417_MIDATA 0x00FF
282 #define MC417_SPD_CTL(x) (((x) << 4) & 0x00000030)
283 #define MC417_GPIO_SEL(x) (((x) << 1) & 0x00000006)
284 #define MC417_UART_GPIO_EN 0x00000001
287 #define MC417_SPD_CTL_SLOW 0x1
288 #define MC417_SPD_CTL_MEDIUM 0x0
289 #define MC417_SPD_CTL_FAST 0x3
292 #define MC417_GPIO_SEL_GPIO3 0x3
293 #define MC417_GPIO_SEL_GPIO2 0x2
294 #define MC417_GPIO_SEL_GPIO1 0x1
295 #define MC417_GPIO_SEL_GPIO0 0x0
298 #define CX23417_GPIO_MASK 0xFC0003FF
302 u32 _gpio_direction = 0;
305 _gpio_direction = _gpio_direction|gpio_direction;
307 (
u8 *)&value, 4, 0, 0);
313 u32 _gpio_direction = 0;
316 _gpio_direction = _gpio_direction|gpio_direction;
319 (
u8 *)pValue, 4, 0, 1);
323 static int waitForMciComplete(
struct cx231xx *dev)
326 u32 gpio_driection = 0;
328 getITVCReg(dev, gpio_driection, &gpio);
330 while (!(gpio&0x020000)) {
333 getITVCReg(dev, gpio_driection, &gpio);
336 dprintk(3,
"ERROR: Timeout - gpio=%x\n", gpio);
353 temp = temp|((0x05)<<10);
360 temp = temp|((0x05)<<10);
367 temp = temp|((0x05)<<10);
374 temp = temp|((0x05)<<10);
381 temp = temp|((0x05)<<10);
388 temp = temp|((0x05)<<10);
395 temp = temp|((0x05)<<10);
398 return waitForMciComplete(dev);
411 temp = temp | ((0x05) << 10);
418 temp = temp | ((0x05) << 10);
425 temp = temp | ((0x05) << 10);
430 ret = waitForMciComplete(dev);
440 return_value |= ((temp & 0x03FC0000) >> 18);
450 return_value |= ((temp & 0x03FC0000) >> 10);
459 return_value |= ((temp & 0x03FC0000) >> 2);
468 return_value |= ((temp & 0x03FC0000) << 6);
489 temp = temp | ((0x05) << 10);
496 temp = temp | ((0x05) << 10);
503 temp = temp|((0x05)<<10);
510 temp = temp|((0x05)<<10);
515 ((address & 0x003F0000)>>8);
518 temp = temp|((0x05)<<10);
525 temp = temp|((0x05)<<10);
532 temp = temp|((0x05)<<10);
536 waitForMciComplete(dev);
544 u32 return_value = 0;
549 ((address & 0x003F0000)>>8);
554 temp = temp|((0x05)<<10);
561 temp = temp|((0x05)<<10);
568 temp = temp|((0x05)<<10);
572 ret = waitForMciComplete(dev);
581 return_value |= ((temp&0x03FC0000)<<6);
590 return_value |= ((temp&0x03FC0000)>>2);
599 return_value |= ((temp&0x03FC0000)>>10);
608 return_value |= ((temp&0x03FC0000)>>18);
618 static char *cmd_to_str(
int cmd)
624 return "START_CAPTURE";
626 return "STOP_CAPTURE";
628 return "SET_AUDIO_ID";
630 return "SET_VIDEO_ID";
632 return "SET_PCR_PID";
634 return "SET_FRAME_RATE";
636 return "SET_FRAME_SIZE";
638 return "SET_BIT_RATE";
640 return "SET_GOP_PROPERTIES";
642 return "SET_ASPECT_RATIO";
644 return "SET_DNR_FILTER_PROPS";
646 return "SET_DNR_FILTER_PROPS";
648 return "SET_CORING_LEVELS";
650 return "SET_SPATIAL_FILTER_TYPE";
652 return "SET_VBI_LINE";
654 return "SET_STREAM_TYPE";
656 return "SET_OUTPUT_PORT";
658 return "SET_AUDIO_PROPERTIES";
662 return "GET_VERSION";
664 return "SET_GOP_CLOSURE";
666 return "GET_SEQ_END";
668 return "SET_PGM_INDEX_INFO";
670 return "SET_VBI_CONFIG";
672 return "SET_DMA_BLOCK_SIZE";
674 return "GET_PREV_DMA_INFO_MB_10";
676 return "GET_PREV_DMA_INFO_MB_9";
678 return "SCHED_DMA_TO_HOST";
680 return "INITIALIZE_INPUT";
682 return "SET_FRAME_DROP_RATE";
684 return "PAUSE_ENCODER";
686 return "REFRESH_INPUT";
688 return "SET_COPYRIGHT";
690 return "SET_EVENT_NOTIFICATION";
692 return "SET_NUM_VSYNC_LINES";
694 return "SET_PLACEHOLDER";
706 static int cx231xx_mbox_func(
void *
priv,
717 dprintk(3,
"%s: command(0x%X) = %s\n", __func__, command,
718 cmd_to_str(command));
723 if (value != 0x12345678) {
725 "Firmware and/or mailbox pointer not initialized "
726 "or corrupted, signature = 0x%x, cmd = %s\n", value,
727 cmd_to_str(command));
736 dprintk(3,
"ERROR: Mailbox appears to be in use "
737 "(%x), cmd = %s\n", flag, cmd_to_str(command));
749 for (i = 0; i <
in; i++) {
766 dprintk(3,
"ERROR: API Mailbox timeout\n");
773 for (i = 0; i <
out; i++) {
779 dprintk(3,
"API result = %d\n", retval);
790 static int cx231xx_api_cmd(
struct cx231xx *dev,
800 dprintk(3,
"%s() cmds = 0x%08x\n", __func__, command);
803 for (i = 0; i < inputcnt; i++)
804 data[i] =
va_arg(vargs,
int);
806 err = cx231xx_mbox_func(dev, command, inputcnt, outputcnt, data);
807 for (i = 0; i < outputcnt; i++) {
808 int *vptr =
va_arg(vargs,
int *);
816 static int cx231xx_find_mailbox(
struct cx231xx *dev)
819 0x12345678, 0x34567812, 0x56781234, 0x78123456
821 int signaturecnt = 0;
826 dprintk(2,
"%s()\n", __func__);
828 for (i = 0; i < 0x100; i++) {
832 if (value == signature[signaturecnt])
836 if (4 == signaturecnt) {
837 dprintk(1,
"Mailbox signature found at 0x%x\n", i+1);
841 dprintk(3,
"Mailbox signature values not found!\n");
845 static void mciWriteMemoryToGPIO(
struct cx231xx *dev,
u32 address,
u32 value,
856 temp = temp|((0x05)<<10);
865 temp = temp|((0x05)<<10);
874 temp = temp|((0x05)<<10);
883 temp = temp|((0x05)<<10);
889 ((address & 0x003F0000)>>8);
893 temp = temp|((0x05)<<10);
902 temp = temp|((0x05)<<10);
911 temp = temp|((0x05)<<10);
915 for (i = 0; i < 6; i++) {
916 *p_fw_image = 0xFFFFFFFF;
922 static int cx231xx_load_firmware(
struct cx231xx *dev)
924 static const unsigned char magic[8] = {
925 0xa7, 0x0d, 0x00, 0x00, 0x66, 0xbb, 0x55, 0xaa
937 u32 *p_current_fw, *p_fw;
940 u16 _buffer_size = 4096;
943 p_current_fw =
vmalloc(1884180 * 4);
945 if (p_current_fw ==
NULL) {
951 if (p_buffer ==
NULL) {
956 dprintk(2,
"%s()\n", __func__);
984 "ERROR: Hotplug firmware request failed (%s).\n",
987 "not work without firmware loaded!\n");
993 "(have %zd, expected %d)\n",
1001 "ERROR: Firmware magic mismatch, wrong file?\n");
1009 dprintk(2,
"Loading firmware to GPIO...\n");
1010 p_fw_data = (
u32 *)firmware->
data;
1011 dprintk(2,
"firmware->size=%zd\n", firmware->
size);
1012 for (transfer_size = 0; transfer_size < firmware->
size;
1013 transfer_size += 4) {
1014 fw_data = *p_fw_data;
1016 mciWriteMemoryToGPIO(dev, address, fw_data, p_current_fw);
1017 address = address + 1;
1026 for (i = 0; i < _buffer_size; i++) {
1027 *(p_buffer +
i) = (
u8)(*(p_fw + (frame * 128 * 8 + (i / 4))) & 0x000000FF);
1029 *(p_buffer +
i) = (
u8)((*(p_fw + (frame * 128 * 8 + (i / 4))) & 0x0000FF00) >> 8);
1031 *(p_buffer +
i) = (
u8)((*(p_fw + (frame * 128 * 8 + (i / 4))) & 0x00FF0000) >> 16);
1033 *(p_buffer +
i) = (
u8)((*(p_fw + (frame * 128 * 8 + (i / 4))) & 0xFF000000) >> 24);
1038 p_current_fw = p_fw;
1039 vfree(p_current_fw);
1040 p_current_fw =
NULL;
1043 dprintk(1,
"Firmware upload successful.\n");
1067 static void cx231xx_417_check_encoder(
struct cx231xx *dev)
1074 dprintk(1,
"%s() status = %d, seq = %d\n", __func__, status, seq);
1077 static void cx231xx_codec_settings(
struct cx231xx *dev)
1079 dprintk(1,
"%s()\n", __func__);
1094 static int cx231xx_initialize_codec(
struct cx231xx *dev)
1101 dprintk(1,
"%s()\n", __func__);
1105 dprintk(2,
"%s() PING OK\n", __func__);
1106 retval = cx231xx_load_firmware(dev);
1111 retval = cx231xx_find_mailbox(dev);
1121 "ERROR: cx23417 firmware ping failed!\n");
1128 "version failed!\n");
1131 dprintk(1,
"cx23417 firmware version is 0x%08x\n", version);
1135 for (i = 0; i < 1; i++) {
1137 dprintk(3,
"***before enable656() VIM Capture Lines =%d ***\n",
1148 cx231xx_codec_settings(dev);
1163 data[0] = 0x0001BD00;
1166 data[3] = 0x91559155;
1167 data[4] = 0x206080C0;
1172 data[2], data[3], data[4], data[5], data[6]);
1174 for (i = 2; i <= 24; i++) {
1177 valid = ((i >= 19) && (i <= 21));
1181 i | 0x80000000, valid, 0, 0, 0);
1203 for (i = 0; i < 1; i++) {
1205 dprintk(3,
"***VIM Capture Lines =%d ***\n", val);
1214 unsigned int *count,
unsigned int *
size)
1218 fh->
dev->ts1.ts_packet_size = mpeglinesize;
1219 fh->
dev->ts1.ts_packet_count = mpeglines;
1221 *size = fh->
dev->ts1.ts_packet_size * fh->
dev->ts1.ts_packet_count;
1230 unsigned long flags = 0;
1243 spin_unlock_irqrestore(&dev->
video_mode.slock, flags);
1249 static void buffer_copy(
struct cx231xx *dev,
char *data,
int len,
struct urb *
urb,
1258 if (list_empty(&dma_q->
active))
1271 mpeglines*mpeglinesize) {
1293 buf->
vb.field_count++;
1299 if (len - tail_data > 0) {
1300 p_data = data + tail_data;
1303 p_data, len - tail_data);
1311 static void buffer_filled(
char *data,
int len,
struct urb *urb,
1317 if (list_empty(&dma_q->
active))
1329 buf->
vb.field_count++;
1336 static inline int cx231xx_isoc_copy(
struct cx231xx *dev,
struct urb *urb)
1339 unsigned char *p_buffer;
1343 for (i = 0; i < urb->number_of_packets; i++) {
1351 p_buffer = urb->transfer_buffer +
1352 urb->iso_frame_desc[
i].offset;
1353 buffer_size = urb->iso_frame_desc[
i].actual_length;
1355 if (buffer_size > 0)
1356 buffer_copy(dev, p_buffer, buffer_size, urb, dma_q);
1361 static inline int cx231xx_bulk_copy(
struct cx231xx *dev,
struct urb *urb)
1367 unsigned char *p_buffer, *
buffer;
1368 u32 buffer_size = 0;
1370 p_buffer = urb->transfer_buffer;
1371 buffer_size = urb->actual_length;
1376 memcpy(buffer+3, p_buffer, buffer_size-3);
1380 buffer_filled(p_buffer, buffer_size, urb, dma_q);
1393 int rc = 0, urb_init = 0;
1394 int size = fh->
dev->ts1.ts_packet_size * fh->
dev->ts1.ts_packet_count;
1398 if (0 != buf->
vb.baddr && buf->
vb.bsize < size)
1400 buf->
vb.width = fh->
dev->ts1.ts_packet_size;
1401 buf->
vb.height = fh->
dev->ts1.ts_packet_count;
1477 .buf_setup = bb_buf_setup,
1478 .buf_prepare = bb_buf_prepare,
1479 .buf_queue = bb_buf_queue,
1480 .buf_release = bb_buf_release,
1485 static const u32 *ctrl_classes[] = {
1490 static int cx231xx_queryctrl(
struct cx231xx *dev,
1504 static int cx231xx_querymenu(
struct cx231xx *dev,
1509 qctrl.
id = qmenu->
id;
1510 cx231xx_queryctrl(dev, &qctrl);
1529 for (i = 0; i <
ARRAY_SIZE(cx231xx_tvnorms); i++)
1530 if (*
id & cx231xx_tvnorms[i].
id)
1537 dprintk(3,
"encodernorm set to NTSC\n");
1542 dprintk(3,
"encodernorm set to PAL\n");
1551 dprintk(3,
"exit vidioc_s_std() i=0x%x\n", i);
1554 static int vidioc_g_audio(
struct file *file,
void *fh,
1566 static int vidioc_enumaudio(
struct file *file,
void *fh,
1581 static const char *iname[] = {
1589 static int vidioc_enum_input(
struct file *file,
void *priv,
1596 dprintk(3,
"enter vidioc_enum_input()i->index=%d\n", i->
index);
1604 if (input->
type == 0)
1623 static int vidioc_g_input(
struct file *file,
void *priv,
unsigned int *i)
1629 static int vidioc_s_input(
struct file *file,
void *priv,
unsigned int i)
1634 dprintk(3,
"enter vidioc_s_input() i=%d\n", i);
1645 dprintk(3,
"exit vidioc_s_input()\n");
1649 static int vidioc_g_tuner(
struct file *file,
void *priv,
1655 static int vidioc_s_tuner(
struct file *file,
void *priv,
1661 static int vidioc_g_frequency(
struct file *file,
void *priv,
1667 static int vidioc_s_frequency(
struct file *file,
void *priv,
1675 static int vidioc_s_ctrl(
struct file *file,
void *priv,
1680 dprintk(3,
"enter vidioc_s_ctrl()\n");
1683 dprintk(3,
"exit vidioc_s_ctrl()\n");
1687 .driver =
"cx231xx",
1688 .card =
"VideoGrabber",
1695 static int vidioc_querycap(
struct file *file,
void *priv,
1705 static int vidioc_enum_fmt_vid_cap(
struct file *file,
void *priv,
1718 static int vidioc_g_fmt_vid_cap(
struct file *file,
void *priv,
1723 dprintk(3,
"enter vidioc_g_fmt_vid_cap()\n");
1725 f->
fmt.
pix.bytesperline = 0;
1727 dev->ts1.ts_packet_size * dev->ts1.ts_packet_count;
1728 f->
fmt.
pix.colorspace = 0;
1732 dprintk(1,
"VIDIOC_G_FMT: w: %d, h: %d, f: %d\n",
1734 dprintk(3,
"exit vidioc_g_fmt_vid_cap()\n");
1738 static int vidioc_try_fmt_vid_cap(
struct file *file,
void *priv,
1743 dprintk(3,
"enter vidioc_try_fmt_vid_cap()\n");
1745 f->
fmt.
pix.bytesperline = 0;
1747 dev->ts1.ts_packet_size * dev->ts1.ts_packet_count;
1748 f->
fmt.
pix.colorspace = 0;
1749 dprintk(1,
"VIDIOC_TRY_FMT: w: %d, h: %d, f: %d\n",
1751 dprintk(3,
"exit vidioc_try_fmt_vid_cap()\n");
1755 static int vidioc_s_fmt_vid_cap(
struct file *file,
void *priv,
1762 static int vidioc_reqbufs(
struct file *file,
void *priv,
1770 static int vidioc_querybuf(
struct file *file,
void *priv,
1778 static int vidioc_qbuf(
struct file *file,
void *priv,
1786 static int vidioc_dqbuf(
struct file *file,
void *priv,
struct v4l2_buffer *
b)
1794 static int vidioc_streamon(
struct file *file,
void *priv,
1800 dprintk(3,
"enter vidioc_streamon()\n");
1814 dprintk(3,
"exit vidioc_streamon()\n");
1818 static int vidioc_streamoff(
struct file *file,
void *priv,
enum v4l2_buf_type i)
1825 static int vidioc_g_ext_ctrls(
struct file *file,
void *priv,
1830 dprintk(3,
"enter vidioc_g_ext_ctrls()\n");
1833 dprintk(3,
"exit vidioc_g_ext_ctrls()\n");
1837 static int vidioc_s_ext_ctrls(
struct file *file,
void *priv,
1844 dprintk(3,
"enter vidioc_s_ext_ctrls()\n");
1862 static int vidioc_try_ext_ctrls(
struct file *file,
void *priv,
1869 dprintk(3,
"enter vidioc_try_ext_ctrls()\n");
1875 dprintk(3,
"exit vidioc_try_ext_ctrls() err=%d\n", err);
1879 static int vidioc_log_status(
struct file *file,
void *priv)
1887 "%s/2: ============ START LOG STATUS ============\n",
1892 "%s/2: ============= END LOG STATUS =============\n",
1897 static int vidioc_querymenu(
struct file *file,
void *priv,
1902 dprintk(3,
"enter vidioc_querymenu()\n");
1903 dprintk(3,
"exit vidioc_querymenu()\n");
1904 return cx231xx_querymenu(dev, a);
1907 static int vidioc_queryctrl(
struct file *file,
void *priv,
1912 dprintk(3,
"enter vidioc_queryctrl()\n");
1913 dprintk(3,
"exit vidioc_queryctrl()\n");
1914 return cx231xx_queryctrl(dev, c);
1917 static int mpeg_open(
struct file *file)
1925 dprintk(2,
"%s()\n", __func__);
1965 cx231xx_initialize_codec(dev);
1973 static int mpeg_release(
struct file *file)
1978 dprintk(3,
"mpeg_release()! dev=0x%p\n", dev);
2007 cx231xx_417_check_encoder(dev);
2012 if (fh->
vidq.streaming)
2014 if (fh->
vidq.reading)
2024 static ssize_t mpeg_read(
struct file *file,
char __user *data,
2025 size_t count, loff_t *ppos)
2035 if (cx231xx_initialize_codec(dev) < 0)
2044 static unsigned int mpeg_poll(
struct file *file,
2055 static int mpeg_mmap(
struct file *file,
struct vm_area_struct *vma)
2060 dprintk(2,
"%s()\n", __func__);
2068 .release = mpeg_release,
2106 #ifdef CONFIG_VIDEO_ADV_DEBUG
2115 .ioctl_ops = &mpeg_ioctl_ops,
2123 dprintk(1,
"%s()\n", __func__);
2124 dprintk(3,
"%s()\n", __func__);
2137 struct usb_device *usbdev,
2143 dprintk(1,
"%s()\n", __func__);
2165 dprintk(1,
"%s()\n", __func__);
2175 tsport->
width = 720;
2182 dev->
v4l_device = cx231xx_video_dev_alloc(dev,
2183 dev->
udev, &cx231xx_mpeg_template,
"mpeg");
2187 dprintk(3,
"%s: can't register mpeg device\n", dev->
name);
2191 dprintk(3,
"%s: registered device video%d [mpeg]\n",