26 #include <linux/list.h>
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/bitmap.h>
31 #include <linux/i2c.h>
34 #include <linux/slab.h>
47 #define CX231XX_VERSION "0.0.2"
50 #define DRIVER_DESC "Conexant cx231xx based USB video device driver"
52 #define cx231xx_videodbg(fmt, arg...) do {\
54 printk(KERN_INFO "%s %s :"fmt, \
55 dev->name, __func__ , ##arg); } while (0)
57 static unsigned int isoc_debug;
61 #define cx231xx_isocdbg(fmt, arg...) \
64 printk(KERN_INFO "%s %s :"fmt, \
65 dev->name, __func__ , ##arg); \
96 .name =
"16bpp YUY2, 4:2:2, packed",
118 .name =
"Brightness",
122 .default_value = 0x7f,
136 .default_value = 0x3f,
150 .default_value = 0x7f,
163 .name =
"Saturation",
167 .default_value = 0x7f,
185 .mask = (0x1f << 24),
194 .default_value = 0x3f,
202 static const int CX231XX_CTLS =
ARRAY_SIZE(cx231xx_ctls);
204 static const u32 cx231xx_user_ctrls[] = {
218 static const u32 *ctrl_classes[] = {
230 static inline void buffer_filled(
struct cx231xx *
dev,
237 buf->
vb.field_count++;
251 char *errmsg =
"Unknown";
255 errmsg =
"unlinked synchronuously";
258 errmsg =
"unlinked asynchronuously";
261 errmsg =
"Buffer error (overrun)";
264 errmsg =
"Stalled (device not responding)";
267 errmsg =
"Babble (bad cable?)";
270 errmsg =
"Bit-stuff error (bad cable?)";
273 errmsg =
"CRC/Timeout (could be anything)";
276 errmsg =
"Device does not respond";
283 packet, status, errmsg);
299 if (list_empty(&dma_q->
active)) {
314 memset(outp, 0, (*buf)->vb.size);
327 static inline int cx231xx_isoc_copy(
struct cx231xx *dev,
struct urb *
urb)
331 unsigned char *p_buffer;
341 if (urb->status < 0) {
342 print_err_status(dev, -1, urb->status);
343 if (urb->status == -
ENOENT)
347 for (i = 0; i < urb->number_of_packets; i++) {
348 int status = urb->iso_frame_desc[
i].status;
351 print_err_status(dev, i, status);
352 if (urb->iso_frame_desc[i].status != -
EPROTO)
356 if (urb->iso_frame_desc[i].actual_length <= 0) {
360 if (urb->iso_frame_desc[i].actual_length >
367 p_buffer = urb->transfer_buffer + urb->iso_frame_desc[
i].offset;
389 p_buffer + bytes_parsed,
390 buffer_size - bytes_parsed);
396 while (bytes_parsed < buffer_size) {
400 p_buffer + bytes_parsed,
401 buffer_size - bytes_parsed,
407 if (sav_eav && (bytes_parsed < buffer_size)) {
410 p_buffer + bytes_parsed,
411 buffer_size - bytes_parsed);
424 static inline int cx231xx_bulk_copy(
struct cx231xx *dev,
struct urb *urb)
428 unsigned char *p_buffer;
438 if (urb->status < 0) {
439 print_err_status(dev, -1, urb->status);
440 if (urb->status == -
ENOENT)
447 p_buffer = urb->transfer_buffer;
469 p_buffer + bytes_parsed,
470 buffer_size - bytes_parsed);
476 while (bytes_parsed < buffer_size) {
480 p_buffer + bytes_parsed,
481 buffer_size - bytes_parsed,
487 if (sav_eav && (bytes_parsed < buffer_size)) {
490 p_buffer + bytes_parsed,
491 buffer_size - bytes_parsed);
509 u8 boundary_bytes[8];
517 memcpy(boundary_bytes, partial_buf, 4);
518 memcpy(boundary_bytes + 4, p_buffer, 4);
527 *p_bytes_used = bytes_used - 4;
543 if (buffer_size < 4) {
544 *p_bytes_used = buffer_size;
548 for (i = 0; i < (buffer_size - 3); i++) {
550 if ((p_buffer[i] == 0xFF) &&
551 (p_buffer[i + 1] == 0x00) && (p_buffer[i + 2] == 0x00)) {
553 *p_bytes_used = i + 4;
554 sav_eav = p_buffer[i + 3];
559 *p_bytes_used = buffer_size;
565 u8 *p_buffer,
u32 buffer_size)
567 u32 bytes_copied = 0;
575 if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
576 (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
589 if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
590 (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
603 buffer_size, current_field);
629 if (bytes_to_copy > length)
647 return bytes_to_copy;
653 dma_q->
pos += bytes_to_copy;
662 buffer_filled(dev, dma_q, buf);
670 return bytes_to_copy;
693 get_next_buf(dma_q, &buf);
706 u8 *p_buffer,
u32 bytes_to_copy)
709 u32 current_line_bytes_copied = 0;
731 startwrite = p_out_buffer +
offset;
737 startwrite += current_line_bytes_copied;
742 if ((
u8 *)(startwrite + lencopy) > (
u8 *)(p_out_buffer + buf->
vb.size))
758 for (i = 0; i < len / 2; i++)
759 to[i] = (from[i] << 8) | (from[
i] >> 8);
764 u8 buffer_complete = 0;
771 return buffer_complete;
784 *size = (fh->
dev->width * fh->
dev->height * dev->
format->depth + 7)>>3;
799 unsigned long flags = 0;
821 spin_unlock_irqrestore(&dev->
video_mode.slock, flags);
835 int rc = 0, urb_init = 0;
838 buf->
vb.size = (fh->
dev->width * fh->
dev->height * dev->
format->depth
840 if (0 != buf->
vb.baddr && buf->
vb.bsize < buf->
vb.size)
913 .buf_setup = buffer_setup,
914 .buf_prepare = buffer_prepare,
915 .buf_queue = buffer_queue,
916 .buf_release = buffer_release,
981 static int check_dev(
struct cx231xx *dev)
994 static int vidioc_g_fmt_vid_cap(
struct file *
file,
void *
priv,
1017 if (format[i].fourcc == fourcc)
1023 static int vidioc_try_fmt_vid_cap(
struct file *
file,
void *
priv,
1056 static int vidioc_s_fmt_vid_cap(
struct file *file,
void *priv,
1063 struct v4l2_mbus_framefmt mbus_fmt;
1065 rc = check_dev(dev);
1069 vidioc_try_fmt_vid_cap(file, priv, f);
1091 call_all(dev, video, s_mbus_fmt, &mbus_fmt);
1092 v4l2_fill_pix_format(&f->
fmt.
pix, &mbus_fmt);
1097 static int vidioc_g_std(
struct file *file,
void *priv,
v4l2_std_id *
id)
1106 static int vidioc_s_std(
struct file *file,
void *priv,
v4l2_std_id *
norm)
1110 struct v4l2_mbus_framefmt mbus_fmt;
1114 rc = check_dev(dev);
1118 cx231xx_info(
"vidioc_s_std : 0x%x\n", (
unsigned int)*norm);
1125 vidioc_try_fmt_vid_cap(file, priv, &f);
1133 call_all(dev, video, s_mbus_fmt, &mbus_fmt);
1134 v4l2_fill_pix_format(&f.
fmt.
pix, &mbus_fmt);
1146 static const char *iname[] = {
1155 static int vidioc_enum_input(
struct file *file,
void *priv,
1161 unsigned int ret,
n;
1195 static int vidioc_g_input(
struct file *file,
void *priv,
unsigned int *i)
1205 static int vidioc_s_input(
struct file *file,
void *priv,
unsigned int i)
1212 rc = check_dev(dev);
1234 static int vidioc_g_audio(
struct file *file,
void *priv,
struct v4l2_audio *
a)
1256 static int vidioc_s_audio(
struct file *file,
void *priv,
const struct v4l2_audio *a)
1272 static int vidioc_queryctrl(
struct file *file,
void *priv,
1281 rc = check_dev(dev);
1289 memset(qc, 0,
sizeof(*qc));
1296 for (i = 0; i < CX231XX_CTLS; i++)
1297 if (cx231xx_ctls[i].
v.id == qc->
id)
1300 if (i == CX231XX_CTLS) {
1304 *qc = cx231xx_ctls[
i].
v;
1314 static int vidioc_g_ctrl(
struct file *file,
void *priv,
1321 rc = check_dev(dev);
1329 static int vidioc_s_ctrl(
struct file *file,
void *priv,
1336 rc = check_dev(dev);
1344 static int vidioc_g_tuner(
struct file *file,
void *priv,
struct v4l2_tuner *
t)
1350 rc = check_dev(dev);
1367 static int vidioc_s_tuner(
struct file *file,
void *priv,
struct v4l2_tuner *t)
1373 rc = check_dev(dev);
1385 static int vidioc_g_frequency(
struct file *file,
void *priv,
1399 static int vidioc_s_frequency(
struct file *file,
void *priv,
1405 u32 if_frequency = 5400000;
1407 cx231xx_info(
"Enter vidioc_s_frequency()f->frequency=%d;f->type=%d\n",
1411 rc = check_dev(dev);
1434 if_frequency = 5400000;
1436 if_frequency = 6000000;
1438 if_frequency = 6900000;
1440 if_frequency = 7100000;
1442 if_frequency = 7250000;
1444 if_frequency = 6900000;
1446 if_frequency = 1250000;
1448 cx231xx_info(
"if_frequency is set to %d\n", if_frequency);
1459 #ifdef CONFIG_VIDEO_ADV_DEBUG
1474 static int vidioc_g_register(
struct file *file,
void *priv,
1483 switch (reg->
match.type) {
1485 switch (reg->
match.addr) {
1488 (
u16)reg->
reg, value, 4);
1489 reg->
val = value[0] | value[1] << 8 |
1490 value[2] << 16 | value[3] << 24;
1494 (
u16)reg->
reg, 2, &data, 1);
1499 (
u16)reg->
reg, 2, &data, 4);
1504 (
u16)reg->
reg, 2, &data, 1);
1509 (
u16)reg->
reg, 2, &data, 4);
1522 (
u16)reg->
reg, 1, &data, 4);
1526 return ret < 0 ? ret : 0;
1532 switch (reg->
match.addr) {
1535 (
u16)reg->
reg, value, 4);
1536 reg->
val = value[0] | value[1] << 8 |
1537 value[2] << 16 | value[3] << 24;
1548 if (reg->
reg < 0x0b) {
1609 return ret < 0 ? ret : 0;
1621 static int vidioc_s_register(
struct file *file,
void *priv,
1629 u8 data[4] = { 0, 0, 0, 0 };
1633 switch (reg->
match.type) {
1636 value = (
u32) buf & 0xffffffff;
1638 switch (reg->
match.addr) {
1640 data[0] = (
u8) value;
1641 data[1] = (
u8) (value >> 8);
1642 data[2] = (
u8) (value >> 16);
1643 data[3] = (
u8) (value >> 24);
1691 return ret < 0 ? ret : 0;
1694 value = (
u32) buf & 0xffffffff;
1696 switch (reg->
match.addr) {
1698 data[0] = (
u8) value;
1699 data[1] = (
u8) (value >> 8);
1700 data[2] = (
u8) (value >> 16);
1701 data[3] = (
u8) (value >> 24);
1715 if (reg->
reg < 0x0b)
1771 "the value is %x\n", reg->
match.addr);
1787 static int vidioc_cropcap(
struct file *file,
void *priv,
1807 static int vidioc_streamon(
struct file *file,
void *priv,
1814 rc = check_dev(dev);
1828 static int vidioc_streamoff(
struct file *file,
void *priv,
1835 rc = check_dev(dev);
1842 if (type != fh->
type)
1853 static int vidioc_querycap(
struct file *file,
void *priv,
1878 static int vidioc_enum_fmt_vid_cap(
struct file *file,
void *priv,
1891 static int vidioc_g_fmt_sliced_vbi_cap(
struct file *file,
void *priv,
1898 rc = check_dev(dev);
1912 static int vidioc_try_set_sliced_vbi_cap(
struct file *file,
void *priv,
1919 rc = check_dev(dev);
1933 static int vidioc_g_fmt_vbi_cap(
struct file *file,
void *priv,
1938 f->
fmt.
vbi.sampling_rate = 6750000 * 4;
1954 static int vidioc_try_fmt_vbi_cap(
struct file *file,
void *priv,
1966 f->
fmt.
vbi.sampling_rate = 6750000 * 4;
1983 static int vidioc_reqbufs(
struct file *file,
void *priv,
1990 rc = check_dev(dev);
1997 static int vidioc_querybuf(
struct file *file,
void *priv,
struct v4l2_buffer *
b)
2003 rc = check_dev(dev);
2010 static int vidioc_qbuf(
struct file *file,
void *priv,
struct v4l2_buffer *b)
2016 rc = check_dev(dev);
2023 static int vidioc_dqbuf(
struct file *file,
void *priv,
struct v4l2_buffer *b)
2029 rc = check_dev(dev);
2040 static int radio_querycap(
struct file *file,
void *priv,
2053 static int radio_g_tuner(
struct file *file,
void *priv,
struct v4l2_tuner *t)
2068 static int radio_enum_input(
struct file *file,
void *priv,
struct v4l2_input *i)
2078 static int radio_g_audio(
struct file *file,
void *priv,
struct v4l2_audio *a)
2087 static int radio_s_tuner(
struct file *file,
void *priv,
struct v4l2_tuner *t)
2099 static int radio_s_audio(
struct file *file,
void *fh,
const struct v4l2_audio *a)
2104 static int radio_s_input(
struct file *file,
void *fh,
unsigned int i)
2109 static int radio_queryctrl(
struct file *file,
void *priv,
2117 for (i = 0; i < CX231XX_CTLS; i++) {
2118 if (cx231xx_ctls[i].
v.id == c->
id)
2121 if (i == CX231XX_CTLS)
2123 *c = cx231xx_ctls[
i].
v;
2133 static int cx231xx_v4l2_open(
struct file *filp)
2135 int errCode = 0,
radio = 0;
2137 struct cx231xx *dev = video_drvdata(filp);
2161 (
"Device locked on digital mode. Can't open analog\n");
2185 if (dev->
board.external_av)
2249 if (video_is_registered(dev->
radio_dev))
2257 video_device_node_name(dev->
vbi_dev));
2258 if (video_is_registered(dev->
vbi_dev))
2266 video_device_node_name(dev->
vdev));
2268 if (dev->
board.has_417)
2271 if (video_is_registered(dev->
vdev))
2284 static int cx231xx_close(
struct file *filp)
2301 if (!dev->
board.no_alt_vanc)
2363 static int cx231xx_v4l2_close(
struct file *filp)
2370 rc = cx231xx_close(filp);
2380 cx231xx_v4l2_read(
struct file *filp,
char __user *buf,
size_t count,
2387 rc = check_dev(dev);
2412 static unsigned int cx231xx_v4l2_poll(
struct file *filp,
poll_table *
wait)
2418 rc = check_dev(dev);
2442 static int cx231xx_v4l2_mmap(
struct file *filp,
struct vm_area_struct *vma)
2448 rc = check_dev(dev);
2464 (
unsigned long)vma->
vm_end -
2472 .open = cx231xx_v4l2_open,
2473 .release = cx231xx_v4l2_close,
2474 .read = cx231xx_v4l2_read,
2475 .poll = cx231xx_v4l2_poll,
2476 .mmap = cx231xx_v4l2_mmap,
2493 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2512 #ifdef CONFIG_VIDEO_ADV_DEBUG
2513 .vidioc_g_register = vidioc_g_register,
2514 .vidioc_s_register = vidioc_s_register,
2520 static const struct video_device cx231xx_video_template = {
2521 .fops = &cx231xx_v4l_fops,
2523 .ioctl_ops = &video_ioctl_ops,
2530 .open = cx231xx_v4l2_open,
2531 .release = cx231xx_v4l2_close,
2536 .vidioc_querycap = radio_querycap,
2537 .vidioc_g_tuner = radio_g_tuner,
2538 .vidioc_enum_input = radio_enum_input,
2539 .vidioc_g_audio = radio_g_audio,
2540 .vidioc_s_tuner = radio_s_tuner,
2541 .vidioc_s_audio = radio_s_audio,
2542 .vidioc_s_input = radio_s_input,
2543 .vidioc_queryctrl = radio_queryctrl,
2548 #ifdef CONFIG_VIDEO_ADV_DEBUG
2549 .vidioc_g_register = vidioc_g_register,
2550 .vidioc_s_register = vidioc_s_register,
2555 .name =
"cx231xx-radio",
2556 .fops = &radio_fops,
2557 .ioctl_ops = &radio_ioctl_ops,
2564 *
template,
const char *type_name)
2575 vfd->
debug = video_debug;
2580 video_set_drvdata(vfd, dev);
2598 dev->
format = &format[0];
2611 dev->
vdev = cx231xx_vdev_init(dev, &cx231xx_video_template,
"video");
2619 video_nr[dev->
devno]);
2627 dev->
name, video_device_node_name(dev->
vdev));
2630 memcpy(&cx231xx_vbi_template, &cx231xx_video_template,
2631 sizeof(cx231xx_vbi_template));
2632 strcpy(cx231xx_vbi_template.name,
"cx231xx-vbi");
2635 dev->
vbi_dev = cx231xx_vdev_init(dev, &cx231xx_vbi_template,
"vbi");
2639 vbi_nr[dev->
devno]);
2649 dev->
radio_dev = cx231xx_vdev_init(dev, &cx231xx_radio_template,
2656 radio_nr[dev->
devno]);
2662 video_device_node_name(dev->
radio_dev));
2666 video_device_node_name(dev->
vdev),
2667 video_device_node_name(dev->
vbi_dev));