29 #include <linux/list.h>
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/bitmap.h>
34 #include <linux/i2c.h>
37 #include <linux/slab.h>
51 #define DRIVER_DESC "Empia em28xx based USB video device driver"
53 #define EM28XX_VERSION "0.1.3"
55 #define em28xx_videodbg(fmt, arg...) do {\
57 printk(KERN_INFO "%s %s :"fmt, \
58 dev->name, __func__ , ##arg); } while (0)
60 static unsigned int isoc_debug;
64 #define em28xx_isocdbg(fmt, arg...) \
67 printk(KERN_INFO "%s %s :"fmt, \
68 dev->name, __func__ , ##arg); \
95 .name =
"16 bpp YUY2, 4:2:2, packed",
100 .name =
"16 bpp RGB 565, LE",
105 .name =
"8 bpp Bayer BGBG..GRGR",
110 .name =
"8 bpp Bayer GRGR..BGBG",
115 .name =
"8 bpp Bayer GBGB..RGRG",
120 .name =
"12 bpp YUV411",
137 .default_value = 0x1f,
158 static inline void buffer_filled(
struct em28xx *
dev,
165 buf->
vb.field_count++;
174 static inline void vbi_buffer_filled(
struct em28xx *
dev,
182 buf->
vb.field_count++;
194 static void em28xx_copy_video(
struct em28xx *
dev,
198 unsigned char *
outp,
unsigned long len)
200 void *fieldstart, *startwrite, *startread;
201 int linesdone, currlinedone,
offset, lencopy, remain;
204 if (dma_q->
pos + len > buf->
vb.size)
205 len = buf->
vb.size - dma_q->
pos;
224 offset = linesdone * bytesperline + currlinedone;
226 offset = linesdone * bytesperline * 2 + currlinedone;
228 startwrite = fieldstart +
offset;
229 lencopy = bytesperline - currlinedone;
230 lencopy = lencopy > remain ? remain : lencopy;
232 if ((
char *)startwrite + lencopy > (
char *)outp + buf->
vb.size) {
234 ((
char *)startwrite + lencopy) -
235 ((
char *)outp + buf->
vb.size));
236 remain = (
char *)outp + buf->
vb.size - (
char *)startwrite;
241 memcpy(startwrite, startread, lencopy);
247 startread += lencopy;
248 if (bytesperline > remain)
253 if ((
char *)startwrite + lencopy > (
char *)outp +
257 ((
char *)startwrite + lencopy) -
258 ((
char *)outp + buf->
vb.size));
259 lencopy = remain = (
char *)outp + buf->
vb.size -
265 memcpy(startwrite, startread, lencopy);
273 static void em28xx_copy_vbi(
struct em28xx *dev,
277 unsigned char *outp,
unsigned long len)
279 void *startwrite, *startread;
305 if (dma_q->
pos + len > buf->
vb.size)
306 len = buf->
vb.size - dma_q->
pos;
310 startwrite = outp + dma_q->
pos;
319 memcpy(startwrite, startread, len);
323 static inline void print_err_status(
struct em28xx *dev,
326 char *errmsg =
"Unknown";
330 errmsg =
"unlinked synchronuously";
333 errmsg =
"unlinked asynchronuously";
336 errmsg =
"Buffer error (overrun)";
339 errmsg =
"Stalled (device not responding)";
342 errmsg =
"Babble (bad cable?)";
345 errmsg =
"Bit-stuff error (bad cable?)";
348 errmsg =
"CRC/Timeout (could be anything)";
351 errmsg =
"Device does not respond";
358 packet, status, errmsg);
371 if (list_empty(&dma_q->
active)) {
383 memset(outp, 0, (*buf)->vb.size);
399 if (list_empty(&dma_q->
active)) {
410 memset(outp, 0x00, (*buf)->vb.size);
420 static inline int em28xx_isoc_copy(
struct em28xx *dev,
struct urb *
urb)
424 unsigned char *outp =
NULL;
425 int i, len = 0,
rc = 1;
434 if (urb->status < 0) {
435 print_err_status(dev, -1, urb->status);
436 if (urb->status == -
ENOENT)
444 for (i = 0; i < urb->number_of_packets; i++) {
445 int status = urb->iso_frame_desc[
i].status;
448 print_err_status(dev, i, status);
449 if (urb->iso_frame_desc[i].status != -
EPROTO)
453 len = urb->iso_frame_desc[
i].actual_length - 4;
455 if (urb->iso_frame_desc[i].actual_length <= 0) {
459 if (urb->iso_frame_desc[i].actual_length >
465 p = urb->transfer_buffer + urb->iso_frame_desc[
i].offset;
470 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
475 if (p[0] == 0x22 && p[1] == 0x5a) {
477 len, (p[2] & 1) ?
"odd" :
"even");
481 buffer_filled(dev, dma_q, buf);
482 get_next_buf(dma_q, &buf);
499 if (p[0] != 0x88 && p[0] != 0x22) {
505 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
513 static inline int em28xx_isoc_copy_vbi(
struct em28xx *dev,
struct urb *urb)
518 unsigned char *outp =
NULL;
519 unsigned char *vbioutp =
NULL;
520 int i, len = 0,
rc = 1;
530 if (urb->status < 0) {
531 print_err_status(dev, -1, urb->status);
532 if (urb->status == -
ENOENT)
544 for (i = 0; i < urb->number_of_packets; i++) {
545 int status = urb->iso_frame_desc[
i].status;
548 print_err_status(dev, i, status);
549 if (urb->iso_frame_desc[i].status != -
EPROTO)
553 len = urb->iso_frame_desc[
i].actual_length;
554 if (urb->iso_frame_desc[i].actual_length <= 0) {
558 if (urb->iso_frame_desc[i].actual_length >
564 p = urb->transfer_buffer + urb->iso_frame_desc[
i].offset;
569 if (p[0] == 0x33 && p[1] == 0x95) {
576 }
else if (p[0] == 0x88 && p[1] == 0x88 &&
577 p[2] == 0x88 && p[3] == 0x88) {
581 }
else if (p[0] == 0x22 && p[1] == 0x5a) {
594 }
else if ((dev->
vbi_read + len) < vbi_size) {
600 vbi_buffer_filled(dev,
603 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
613 if (vbi_buf !=
NULL) {
622 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
627 int vbi_data_len = vbi_size - dev->
vbi_read;
629 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
630 vbioutp, vbi_data_len);
641 buffer_filled(dev, dma_q, buf);
642 get_next_buf(dma_q, &buf);
659 if (len >= 4 && p[0] == 0x88 && p[1] == 0x88 &&
660 p[2] == 0x88 && p[3] == 0x88) {
664 if (len >= 4 && p[0] == 0x22 && p[1] == 0x5a) {
666 p[2], len, (p[2] & 1) ?
673 em28xx_copy_video(dev, dma_q, buf, p, outp,
692 *size = (fh->
dev->width * fh->
dev->height * dev->
format->depth + 7)
716 unsigned long flags = 0;
732 spin_unlock_irqrestore(&dev->
slock, flags);
745 int rc = 0, urb_init = 0;
747 buf->
vb.size = (fh->
dev->width * fh->
dev->height * dev->
format->depth
750 if (0 != buf->
vb.baddr && buf->
vb.bsize < buf->
vb.size)
763 if (!dev->
isoc_ctl.analog_bufs.num_bufs)
772 em28xx_isoc_copy_vbi);
821 .buf_setup = buffer_setup,
822 .buf_prepare = buffer_prepare,
823 .buf_queue = buffer_queue,
824 .buf_release = buffer_release,
839 INPUT(index)->vmux, 0, 0);
841 if (dev->
board.has_msp34xx) {
860 static int res_get(
struct em28xx_fh *fh,
unsigned int bit)
880 static int res_check(
struct em28xx_fh *fh,
unsigned int bit)
885 static int res_locked(
struct em28xx *dev,
unsigned int bit)
890 static void res_free(
struct em28xx_fh *fh,
unsigned int bits)
901 static int get_ressource(
struct em28xx_fh *fh)
922 for (i = 0; i <
ARRAY_SIZE(ac97_qctrl); i++) {
923 if (qc->
id && qc->
id == ac97_qctrl[i].
id) {
924 memcpy(qc, &(ac97_qctrl[i]),
sizeof(*qc));
961 if (ctrl->
id == ac97_qctrl[i].
id)
984 static int check_dev(
struct em28xx *dev)
993 "close and open it again\n");
999 static void get_scale(
struct em28xx *dev,
1006 *hscale = (((
unsigned long)maxw) << 12) / width - 4096L;
1007 if (*hscale >= 0x4000)
1010 *vscale = (((
unsigned long)maxh) << 12) / height - 4096L;
1011 if (*vscale >= 0x4000)
1019 static int vidioc_g_fmt_vid_cap(
struct file *
file,
void *
priv,
1046 if (format[i].fourcc == fourcc)
1052 static int vidioc_try_fmt_vid_cap(
struct file *file,
void *priv,
1057 unsigned int width = f->
fmt.
pix.width;
1058 unsigned int height = f->
fmt.
pix.height;
1071 if (dev->
board.is_em2800) {
1073 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
1074 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
1077 if (width == maxw && height == maxh)
1086 get_scale(dev, width, height, &hscale, &vscale);
1088 width = (((
unsigned long)maxw) << 12) / (hscale + 4096L);
1089 height = (((
unsigned long)maxh) << 12) / (vscale + 4096L);
1106 static int em28xx_set_video_format(
struct em28xx *dev,
unsigned int fourcc,
1107 unsigned width,
unsigned height)
1128 static int vidioc_s_fmt_vid_cap(
struct file *file,
void *priv,
1135 rc = check_dev(dev);
1139 vidioc_try_fmt_vid_cap(file, priv, f);
1146 return em28xx_set_video_format(dev, f->
fmt.
pix.pixelformat,
1150 static int vidioc_g_std(
struct file *file,
void *priv,
v4l2_std_id *
norm)
1156 rc = check_dev(dev);
1165 static int vidioc_querystd(
struct file *file,
void *priv,
v4l2_std_id *norm)
1171 rc = check_dev(dev);
1180 static int vidioc_s_std(
struct file *file,
void *priv,
v4l2_std_id *norm)
1187 rc = check_dev(dev);
1196 vidioc_try_fmt_vid_cap(file, priv, &f);
1209 static int vidioc_g_parm(
struct file *file,
void *priv,
1219 if (dev->
board.is_webcam)
1229 static int vidioc_s_parm(
struct file *file,
void *priv,
1235 if (!dev->
board.is_webcam)
1244 static const char *iname[] = {
1256 static int vidioc_enum_input(
struct file *file,
void *priv,
1283 static int vidioc_g_input(
struct file *file,
void *priv,
unsigned int *i)
1293 static int vidioc_s_input(
struct file *file,
void *priv,
unsigned int i)
1299 rc = check_dev(dev);
1312 static int vidioc_g_audio(
struct file *file,
void *priv,
struct v4l2_audio *
a)
1355 static int vidioc_s_audio(
struct file *file,
void *priv,
const struct v4l2_audio *a)
1378 static int vidioc_queryctrl(
struct file *file,
void *priv,
1386 rc = check_dev(dev);
1390 memset(qc, 0,
sizeof(*qc));
1396 rc = ac97_queryctrl(qc);
1415 static int check_subdev_ctrl(
struct em28xx *dev,
int id)
1419 memset(&qc, 0,
sizeof(qc));
1431 static int vidioc_g_ctrl(
struct file *file,
void *priv,
1438 rc = check_dev(dev);
1445 rc = ac97_get_ctrl(dev, ctrl);
1451 if (check_subdev_ctrl(dev, ctrl->
id))
1461 static int vidioc_s_ctrl(
struct file *file,
void *priv,
1468 rc = check_dev(dev);
1474 rc = ac97_set_ctrl(dev, ctrl);
1480 rc = check_subdev_ctrl(dev, ctrl->
id);
1483 core, s_ctrl, ctrl);
1500 return (rc < 0) ? rc : 0;
1503 static int vidioc_g_tuner(
struct file *file,
void *priv,
1510 rc = check_dev(dev);
1524 static int vidioc_s_tuner(
struct file *file,
void *priv,
1531 rc = check_dev(dev);
1542 static int vidioc_g_frequency(
struct file *file,
void *priv,
1553 static int vidioc_s_frequency(
struct file *file,
void *priv,
1560 rc = check_dev(dev);
1578 #ifdef CONFIG_VIDEO_ADV_DEBUG
1579 static int em28xx_reg_len(
int reg)
1591 static int vidioc_g_chip_ident(
struct file *file,
void *priv,
1606 static int vidioc_g_register(
struct file *file,
void *priv,
1613 switch (reg->
match.type) {
1635 reg->
size = em28xx_reg_len(reg->
reg);
1636 if (reg->
size == 1) {
1646 reg->
reg, (
char *)&val, 2);
1656 static int vidioc_s_register(
struct file *file,
void *priv,
1663 switch (reg->
match.type) {
1682 em28xx_reg_len(reg->
reg));
1687 static int vidioc_cropcap(
struct file *file,
void *priv,
1707 static int vidioc_streamon(
struct file *file,
void *priv,
1714 rc = check_dev(dev);
1721 em28xx_videodbg(
"vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1724 if (
unlikely(!res_get(fh, get_ressource(fh))))
1735 static int vidioc_streamoff(
struct file *file,
void *priv,
1742 rc = check_dev(dev);
1749 if (type != fh->
type)
1752 em28xx_videodbg(
"vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1770 static int vidioc_querycap(
struct file *file,
void *priv,
1797 static int vidioc_enum_fmt_vid_cap(
struct file *file,
void *priv,
1809 static int vidioc_enum_framesizes(
struct file *file,
void *priv,
1825 if (dev->
board.is_em2800) {
1826 if (fsize->
index > 1)
1834 if (fsize->
index != 0)
1849 static int vidioc_g_fmt_sliced_vbi_cap(
struct file *file,
void *priv,
1856 rc = check_dev(dev);
1869 static int vidioc_try_set_sliced_vbi_cap(
struct file *file,
void *priv,
1876 rc = check_dev(dev);
1890 static int vidioc_g_fmt_vbi_cap(
struct file *file,
void *priv,
1898 format->
fmt.
vbi.offset = 0;
1899 format->
fmt.
vbi.flags = 0;
1900 format->
fmt.
vbi.sampling_rate = 6750000 * 4 / 2;
1907 format->
fmt.
vbi.start[0] = 10;
1908 format->
fmt.
vbi.start[1] = 273;
1911 format->
fmt.
vbi.start[0] = 6;
1912 format->
fmt.
vbi.start[1] = 318;
1918 static int vidioc_s_fmt_vbi_cap(
struct file *file,
void *priv,
1926 format->
fmt.
vbi.offset = 0;
1927 format->
fmt.
vbi.flags = 0;
1928 format->
fmt.
vbi.sampling_rate = 6750000 * 4 / 2;
1935 format->
fmt.
vbi.start[0] = 10;
1936 format->
fmt.
vbi.start[1] = 273;
1939 format->
fmt.
vbi.start[0] = 6;
1940 format->
fmt.
vbi.start[1] = 318;
1946 static int vidioc_reqbufs(
struct file *file,
void *priv,
1953 rc = check_dev(dev);
1963 static int vidioc_querybuf(
struct file *file,
void *priv,
1970 rc = check_dev(dev);
1989 static int vidioc_qbuf(
struct file *file,
void *priv,
struct v4l2_buffer *b)
1995 rc = check_dev(dev);
2005 static int vidioc_dqbuf(
struct file *file,
void *priv,
struct v4l2_buffer *b)
2011 rc = check_dev(dev);
2027 static int radio_querycap(
struct file *file,
void *priv,
2040 static int radio_g_tuner(
struct file *file,
void *priv,
2056 static int radio_enum_input(
struct file *file,
void *priv,
2067 static int radio_g_audio(
struct file *file,
void *priv,
struct v4l2_audio *a)
2076 static int radio_s_tuner(
struct file *file,
void *priv,
2089 static int radio_s_audio(
struct file *file,
void *fh,
2095 static int radio_s_input(
struct file *file,
void *fh,
unsigned int i)
2100 static int radio_queryctrl(
struct file *file,
void *priv,
2109 for (i = 0; i <
ARRAY_SIZE(ac97_qctrl); i++) {
2110 if (qc->
id && qc->
id == ac97_qctrl[i].
id) {
2111 memcpy(qc, &(ac97_qctrl[i]),
sizeof(*qc));
2123 static int em28xx_v4l2_open(
struct file *filp)
2125 int errCode = 0,
radio = 0;
2127 struct em28xx *dev = video_drvdata(filp);
2211 if (video_is_registered(dev->
radio_dev))
2219 video_device_node_name(dev->
vbi_dev));
2220 if (video_is_registered(dev->
vbi_dev))
2228 video_device_node_name(dev->
vdev));
2229 if (video_is_registered(dev->
vdev))
2242 static int em28xx_v4l2_close(
struct file *filp)
2261 if (dev->
users == 1) {
2286 "0 (error=%i)\n", errCode);
2303 em28xx_v4l2_read(
struct file *filp,
char __user *buf,
size_t count,
2310 rc = check_dev(dev);
2342 static unsigned int em28xx_poll(
struct file *filp,
poll_table *
wait)
2348 rc = check_dev(dev);
2365 static unsigned int em28xx_v4l2_poll(
struct file *filp,
poll_table *wait)
2372 res = em28xx_poll(filp, wait);
2380 static int em28xx_v4l2_mmap(
struct file *filp,
struct vm_area_struct *vma)
2386 rc = check_dev(dev);
2408 .open = em28xx_v4l2_open,
2409 .release = em28xx_v4l2_close,
2410 .read = em28xx_v4l2_read,
2411 .poll = em28xx_v4l2_poll,
2412 .mmap = em28xx_v4l2_mmap,
2429 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2430 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2453 #ifdef CONFIG_VIDEO_ADV_DEBUG
2454 .vidioc_g_register = vidioc_g_register,
2455 .vidioc_s_register = vidioc_s_register,
2460 static const struct video_device em28xx_video_template = {
2461 .fops = &em28xx_v4l_fops,
2463 .ioctl_ops = &video_ioctl_ops,
2471 .open = em28xx_v4l2_open,
2472 .release = em28xx_v4l2_close,
2477 .vidioc_querycap = radio_querycap,
2478 .vidioc_g_tuner = radio_g_tuner,
2479 .vidioc_enum_input = radio_enum_input,
2480 .vidioc_g_audio = radio_g_audio,
2481 .vidioc_s_tuner = radio_s_tuner,
2482 .vidioc_s_audio = radio_s_audio,
2483 .vidioc_s_input = radio_s_input,
2484 .vidioc_queryctrl = radio_queryctrl,
2489 #ifdef CONFIG_VIDEO_ADV_DEBUG
2490 .vidioc_g_register = vidioc_g_register,
2491 .vidioc_s_register = vidioc_s_register,
2496 .name =
"em28xx-radio",
2497 .fops = &radio_fops,
2498 .ioctl_ops = &radio_ioctl_ops,
2507 const char *type_name)
2518 vfd->
debug = video_debug;
2522 dev->
name, type_name);
2524 video_set_drvdata(vfd, dev);
2543 dev->
format = &format[0];
2548 if (dev->
board.is_em2800)
2551 em28xx_set_video_format(dev, format[0].fourcc,
2567 em28xx_compression_disable(dev);
2570 dev->
vdev = em28xx_vdev_init(dev, &em28xx_video_template,
"video");
2578 video_nr[dev->
devno]);
2580 em28xx_errdev(
"unable to register video device (error=%i).\n",
2587 dev->
vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2592 vbi_nr[dev->
devno]);
2600 dev->
radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2607 radio_nr[dev->
devno]);
2613 video_device_node_name(dev->
radio_dev));
2616 em28xx_info(
"V4L2 video device registered as %s\n",
2617 video_device_node_name(dev->
vdev));
2621 video_device_node_name(dev->
vbi_dev));