20 #include <linux/kernel.h>
21 #include <linux/module.h>
31 #define MIN_VID_BUFFERS 4
32 #define FRAME_BUF_SIZE (128 * 1024)
35 static int solo_enc_thread(
void *
data);
51 static const u32 solo_user_ctrls[] = {
60 static const u32 solo_mpeg_ctrls[] = {
66 static const u32 solo_private_ctrls[] = {
72 static const u32 solo_fmtx_ctrls[] = {
77 static const u32 *solo_ctrl_classes[] = {
85 static int solo_is_motion_on(
struct solo_enc_dev *solo_enc)
95 static void solo_motion_toggle(
struct solo_enc_dev *solo_enc,
int on)
97 struct solo_dev *solo_dev = solo_enc->
solo_dev;
100 spin_lock(&solo_enc->
lock);
121 spin_unlock(&solo_enc->
lock);
125 static void solo_update_mode(
struct solo_enc_dev *solo_enc)
127 struct solo_dev *solo_dev = solo_enc->
solo_dev;
134 switch (solo_enc->
mode) {
145 WARN(1,
"mode is unknown\n");
153 u8 ch = solo_enc->
ch;
154 struct solo_dev *solo_dev = solo_enc->
solo_dev;
162 solo_update_mode(solo_enc);
215 struct solo_dev *solo_dev = solo_enc->
solo_dev;
235 static int solo_start_fh_thread(
struct solo_enc_fh *fh)
243 spin_lock(&solo_enc->
lock);
245 spin_unlock(&solo_enc->
lock);
253 static void enc_reset_gop(
struct solo_dev *solo_dev,
u8 ch)
260 static int enc_gop_reset(
struct solo_dev *solo_dev,
u8 ch,
u8 vop)
263 if (!solo_dev->
v4l2_enc[ch]->reset_gop)
278 for (sg = sglist; sg && size > 0; sg =
sg_next(sg)) {
283 for (i = 0; i < len &&
size; i++)
288 static int enc_get_mpeg_dma_sg(
struct solo_dev *solo_dev,
291 unsigned int off,
unsigned int size)
300 desc, 0, sglist, skip,
317 static int enc_get_mpeg_dma_t(
struct solo_dev *solo_dev,
344 static int enc_get_mpeg_dma(
struct solo_dev *solo_dev,
void *buf,
345 unsigned int off,
unsigned int size)
351 ret = enc_get_mpeg_dma_t(solo_dev, dma_addr, off, size);
357 static int enc_get_jpeg_dma_sg(
struct solo_dev *solo_dev,
360 unsigned int off,
unsigned int size)
369 desc, 0, sglist, skip,
387 #define OFF_IN_RANGE(__off, __range, __chk) \
388 ((__off <= __chk) && ((__off + __range) >= __chk))
390 static void solo_jpeg_header(
struct solo_enc_dev *solo_enc,
394 void *src = jpeg_header;
396 size_t to_copy =
sizeof(jpeg_header);
398 for (sg = vbuf->
sglist; sg && copied < to_copy; sg =
sg_next(sg)) {
400 (
unsigned int)(to_copy - copied));
403 memcpy(p, src + copied, this_copy);
407 0xff & (solo_enc->
height >> 8);
412 0xff & (solo_enc->
width >> 8);
424 struct solo_dev *solo_dev = fh->
enc->solo_dev;
428 solo_jpeg_header(fh->
enc, vbuf);
430 vb->
size = size +
sizeof(jpeg_header);
433 return enc_get_jpeg_dma_sg(solo_dev, fh->
desc, vbuf->
sglist,
438 static inline int vop_interlaced(
__le32 *vh)
448 static inline u8 vop_hsize(
__le32 *vh)
453 static inline u8 vop_vsize(
__le32 *vh)
459 static void write_bytes(
u8 **
out,
unsigned *
bits,
const u8 *src,
unsigned count)
469 value <<= 32 -
count;
473 **out |= !!(value & (1 << 31));
475 if (++(*bits) % 8 == 0)
480 static void write_ue(
u8 **out,
unsigned *bits,
unsigned value)
484 while (value > max) {
485 max = (max + 2) * 2 - 2;
488 write_bits(out, bits, 1,
cnt + 1);
489 write_bits(out, bits, ~(max - value),
cnt);
492 static void write_se(
u8 **out,
unsigned *bits,
int value)
495 write_ue(out, bits, -value * 2);
497 write_ue(out, bits, value * 2 - 1);
500 static void write_mpeg4_end(
u8 **out,
unsigned *bits)
502 write_bits(out, bits, 0, 1);
505 write_bits(out, bits, 0xFFFFFFFF, 32 - *bits % 32);
508 static void write_h264_end(
u8 **out,
unsigned *bits,
int align)
510 write_bits(out, bits, 1, 1);
512 write_bits(out, bits, 0, 1);
515 write_bits(out, bits, 0, 1);
518 static void mpeg4_write_vol(
u8 **out,
struct solo_dev *solo_dev,
519 __le32 *vh,
unsigned fps,
unsigned interval)
521 static const u8 hdr[] = {
526 unsigned width = vop_hsize(vh) << 4;
527 unsigned height = vop_vsize(vh) << 4;
530 write_bytes(out, &bits, hdr,
sizeof(hdr));
531 write_bits(out, &bits, 0, 1);
532 write_bits(out, &bits, 0x04, 8);
533 write_bits(out, &bits, 1, 1);
534 write_bits(out, &bits, 2, 4);
535 write_bits(out, &bits, 0, 3);
537 write_bits(out, &bits, 3, 4);
539 write_bits(out, &bits, 2, 4);
540 write_bits(out, &bits, 1, 1);
541 write_bits(out, &bits, 1, 2);
542 write_bits(out, &bits, 1, 1);
543 write_bits(out, &bits, 0, 1);
544 write_bits(out, &bits, 0, 2);
545 write_bits(out, &bits, 1, 1);
546 write_bits(out, &bits, fps, 16);
547 write_bits(out, &bits, 1, 1);
548 write_bits(out, &bits, 1, 1);
549 write_bits(out, &bits, interval, 15);
550 write_bits(out, &bits, 1, 1);
551 write_bits(out, &bits, width, 13);
552 write_bits(out, &bits, 1, 1);
553 write_bits(out, &bits, height, 13);
554 write_bits(out, &bits, 1, 1);
555 write_bits(out, &bits, interlaced, 1);
556 write_bits(out, &bits, 1, 1);
557 write_bits(out, &bits, 0, 2);
558 write_bits(out, &bits, 0, 1);
559 write_bits(out, &bits, 1, 0);
560 write_bits(out, &bits, 0, 1);
561 write_bits(out, &bits, 0, 1);
562 write_bits(out, &bits, 0, 1);
563 write_bits(out, &bits, 1, 1);
564 write_bits(out, &bits, 1, 1);
565 write_bits(out, &bits, 0, 1);
566 write_bits(out, &bits, 0, 1);
567 write_bits(out, &bits, 0, 1);
568 write_bits(out, &bits, 0, 1);
569 write_mpeg4_end(out, &bits);
572 static void h264_write_vol(
u8 **out,
struct solo_dev *solo_dev,
__le32 *vh)
574 static const u8 sps[] = {
575 0, 0, 0, 1 , 0x67, 66 ,
578 static const u8 pps[] = {
583 unsigned mbs_w = vop_hsize(vh);
584 unsigned mbs_h = vop_vsize(vh);
586 write_bytes(out, &bits, sps,
sizeof(sps));
587 write_ue(out, &bits, 0);
588 write_ue(out, &bits, 5);
589 write_ue(out, &bits, 0);
590 write_ue(out, &bits, 6);
591 write_ue(out, &bits, 1);
592 write_bits(out, &bits, 0, 1);
593 write_ue(out, &bits, mbs_w - 1);
594 write_ue(out, &bits, mbs_h - 1);
595 write_bits(out, &bits, 1, 1);
596 write_bits(out, &bits, 1, 1);
597 write_bits(out, &bits, 0, 1);
598 write_bits(out, &bits, 0, 1);
599 write_h264_end(out, &bits, 0);
601 write_bytes(out, &bits, pps,
sizeof(pps));
602 write_ue(out, &bits, 0);
603 write_ue(out, &bits, 0);
604 write_bits(out, &bits, 0, 1);
605 write_bits(out, &bits, 0, 1);
606 write_ue(out, &bits, 0);
607 write_ue(out, &bits, 0);
608 write_ue(out, &bits, 0);
609 write_bits(out, &bits, 0, 1);
610 write_bits(out, &bits, 0, 2);
611 write_se(out, &bits, 0);
612 write_se(out, &bits, 0);
613 write_se(out, &bits, 2);
614 write_bits(out, &bits, 0, 1);
615 write_bits(out, &bits, 1, 1);
616 write_bits(out, &bits, 0, 1);
617 write_h264_end(out, &bits, 1);
625 struct solo_dev *solo_dev = solo_enc->
solo_dev;
628 #define MAX_VOL_HEADER_LENGTH 64
632 int frame_size, frame_off;
639 ret = enc_get_mpeg_dma(solo_dev, vh, enc_buf->
off,
sizeof(vh));
646 vb->
width = vop_hsize(vh) << 4;
647 vb->
height = vop_vsize(vh) << 4;
648 vb->
size = vop_size(vh);
655 h264_write_vol(&out, solo_dev, vh);
657 mpeg4_write_vol(&out, solo_dev, vh,
658 solo_dev->
fps * 1000,
661 enc_write_sg(vbuf->
sglist, header, skip);
668 frame_size = enc_buf->
size -
sizeof(vh);
670 ret = enc_get_mpeg_dma_sg(solo_dev, fh->
desc, vbuf->
sglist,
671 skip, frame_off, frame_size);
677 static void solo_enc_fillbuf(
struct solo_enc_fh *fh,
681 struct solo_dev *solo_dev = solo_enc->
solo_dev;
693 if (ebuf->
ch != solo_enc->
ch)
725 ret = solo_fill_mpeg(fh, enc_buf, vb, vbuf);
727 ret = solo_fill_jpeg(fh, enc_buf, vb, vbuf);
737 vb->
ts = enc_buf->
ts;
746 static void solo_enc_thread_try(
struct solo_enc_fh *fh)
749 struct solo_dev *solo_dev = solo_enc->
solo_dev;
753 spin_lock(&solo_enc->
lock);
764 if (!waitqueue_active(&vb->
done))
769 spin_unlock(&solo_enc->
lock);
771 solo_enc_fillbuf(fh, vb);
775 spin_unlock(&solo_enc->
lock);
778 static int solo_enc_thread(
void *
data)
791 solo_enc_thread_try(fh);
809 for (i = 0; i < solo_dev->
nr_chans; i++) {
816 if (!(status & (1 << i)))
826 u32 mpeg_current, mpeg_next, mpeg_size;
837 reg_mpeg_size = ((solo_reg_read(solo_dev,
SOLO_VE_STATE(0)) & 0xFFFFF) + 64 + 8) & ~7;
839 while (solo_dev->
enc_idx != cur_q) {
840 mpeg_current = solo_reg_read(solo_dev,
842 jpeg_current = solo_reg_read(solo_dev,
845 mpeg_next = solo_reg_read(solo_dev,
847 jpeg_next = solo_reg_read(solo_dev,
850 ch = (mpeg_current >> 24) & 0x1f;
857 vop_type = (mpeg_current >> 29) & 3;
859 mpeg_current &= 0x00ffffff;
860 mpeg_next &= 0x00ffffff;
861 jpeg_current &= 0x00ffffff;
862 jpeg_next &= 0x00ffffff;
865 mpeg_next - mpeg_current) %
869 jpeg_next - jpeg_current) %
873 if (mpeg_current > mpeg_next && mpeg_size != reg_mpeg_size) {
874 enc_reset_gop(solo_dev, ch);
879 if (enc_gop_reset(solo_dev, ch, vop_type))
884 enc_buf->
vop = vop_type;
886 enc_buf->
off = mpeg_current;
887 enc_buf->
size = mpeg_size;
890 enc_buf->
type = enc_type;
903 static int solo_enc_buf_setup(
struct videobuf_queue *vq,
unsigned int *count,
966 .buf_setup = solo_enc_buf_setup,
967 .buf_prepare = solo_enc_buf_prepare,
968 .buf_queue = solo_enc_buf_queue,
969 .buf_release = solo_enc_buf_release,
972 static unsigned int solo_enc_poll(
struct file *
file,
987 static int solo_enc_open(
struct file *
file)
1012 static ssize_t solo_enc_read(
struct file *file,
char __user *data,
1013 size_t count, loff_t *ppos)
1022 spin_lock(&solo_enc->
lock);
1023 ret = solo_enc_on(fh);
1024 spin_unlock(&solo_enc->
lock);
1028 ret = solo_start_fh_thread(fh);
1037 static int solo_enc_release(
struct file *file)
1045 spin_lock(&solo_enc->
lock);
1047 spin_unlock(&solo_enc->
lock);
1054 static int solo_enc_querycap(
struct file *file,
void *
priv,
1059 struct solo_dev *solo_dev = solo_enc->
solo_dev;
1065 pci_name(solo_dev->
pdev));
1073 static int solo_enc_enum_input(
struct file *file,
void *priv,
1078 struct solo_dev *solo_dev = solo_enc->
solo_dev;
1098 static int solo_enc_set_input(
struct file *file,
void *priv,
unsigned int index)
1106 static int solo_enc_get_input(
struct file *file,
void *priv,
1107 unsigned int *index)
1114 static int solo_enc_enum_fmt_cap(
struct file *file,
void *priv,
1135 static int solo_enc_try_fmt_cap(
struct file *file,
void *priv,
1140 struct solo_dev *solo_dev = solo_enc->
solo_dev;
1177 static int solo_enc_set_fmt_cap(
struct file *file,
void *priv,
1182 struct solo_dev *solo_dev = solo_enc->
solo_dev;
1186 spin_lock(&solo_enc->
lock);
1188 ret = solo_enc_try_fmt_cap(file, priv, f);
1190 spin_unlock(&solo_enc->
lock);
1204 ret = solo_enc_on(fh);
1206 spin_unlock(&solo_enc->
lock);
1211 return solo_start_fh_thread(fh);
1214 static int solo_enc_get_fmt_cap(
struct file *file,
void *priv,
1232 static int solo_enc_reqbufs(
struct file *file,
void *priv,
1240 static int solo_enc_querybuf(
struct file *file,
void *priv,
1248 static int solo_enc_qbuf(
struct file *file,
void *priv,
struct v4l2_buffer *buf)
1255 static int solo_enc_dqbuf(
struct file *file,
void *priv,
1264 spin_lock(&solo_enc->
lock);
1265 ret = solo_enc_on(fh);
1266 spin_unlock(&solo_enc->
lock);
1270 ret = solo_start_fh_thread(fh);
1280 if (solo_is_motion_on(solo_enc)) {
1307 static int solo_enc_streamon(
struct file *file,
void *priv,
1318 static int solo_enc_streamoff(
struct file *file,
void *priv,
1329 static int solo_enc_s_std(
struct file *file,
void *priv,
v4l2_std_id *i)
1334 static int solo_enum_framesizes(
struct file *file,
void *priv,
1338 struct solo_dev *solo_dev = fh->
enc->solo_dev;
1343 switch (fsize->
index) {
1361 static int solo_enum_frameintervals(
struct file *file,
void *priv,
1365 struct solo_dev *solo_dev = fh->
enc->solo_dev;
1373 fintv->
stepwise.min.denominator = 1;
1376 fintv->
stepwise.max.denominator = 15;
1378 fintv->
stepwise.step.numerator = 1;
1379 fintv->
stepwise.step.denominator = 1;
1384 static int solo_g_parm(
struct file *file,
void *priv,
1389 struct solo_dev *solo_dev = solo_enc->
solo_dev;
1402 static int solo_s_parm(
struct file *file,
void *priv,
1407 struct solo_dev *solo_dev = solo_enc->
solo_dev;
1410 spin_lock(&solo_enc->
lock);
1413 spin_unlock(&solo_enc->
lock);
1435 solo_update_mode(solo_enc);
1437 spin_unlock(&solo_enc->
lock);
1442 static int solo_queryctrl(
struct file *file,
void *priv,
1447 struct solo_dev *solo_dev = solo_enc->
solo_dev;
1506 static int solo_querymenu(
struct file *file,
void *priv,
1512 qctrl.id = qmenu->
id;
1513 err = solo_queryctrl(file, priv, &qctrl);
1520 static int solo_g_ctrl(
struct file *file,
void *priv,
1525 struct solo_dev *solo_dev = solo_enc->
solo_dev;
1545 ctrl->
value = solo_is_motion_on(solo_enc);
1554 static int solo_s_ctrl(
struct file *file,
void *priv,
1559 struct solo_dev *solo_dev = solo_enc->
solo_dev;
1591 solo_motion_toggle(solo_enc, ctrl->
value);
1600 static int solo_s_ext_ctrls(
struct file *file,
void *priv,
1607 for (i = 0; i < ctrls->
count; i++) {
1637 static int solo_g_ext_ctrls(
struct file *file,
void *priv,
1644 for (i = 0; i < ctrls->
count; i++) {
1674 .open = solo_enc_open,
1675 .release = solo_enc_release,
1676 .read = solo_enc_read,
1677 .poll = solo_enc_poll,
1678 .mmap = solo_enc_mmap,
1683 .vidioc_querycap = solo_enc_querycap,
1684 .vidioc_s_std = solo_enc_s_std,
1686 .vidioc_enum_input = solo_enc_enum_input,
1687 .vidioc_s_input = solo_enc_set_input,
1688 .vidioc_g_input = solo_enc_get_input,
1690 .vidioc_enum_fmt_vid_cap = solo_enc_enum_fmt_cap,
1691 .vidioc_try_fmt_vid_cap = solo_enc_try_fmt_cap,
1692 .vidioc_s_fmt_vid_cap = solo_enc_set_fmt_cap,
1693 .vidioc_g_fmt_vid_cap = solo_enc_get_fmt_cap,
1695 .vidioc_reqbufs = solo_enc_reqbufs,
1696 .vidioc_querybuf = solo_enc_querybuf,
1697 .vidioc_qbuf = solo_enc_qbuf,
1698 .vidioc_dqbuf = solo_enc_dqbuf,
1699 .vidioc_streamon = solo_enc_streamon,
1700 .vidioc_streamoff = solo_enc_streamoff,
1702 .vidioc_enum_framesizes = solo_enum_framesizes,
1703 .vidioc_enum_frameintervals = solo_enum_frameintervals,
1705 .vidioc_s_parm = solo_s_parm,
1706 .vidioc_g_parm = solo_g_parm,
1708 .vidioc_queryctrl = solo_queryctrl,
1709 .vidioc_querymenu = solo_querymenu,
1710 .vidioc_g_ctrl = solo_g_ctrl,
1711 .vidioc_s_ctrl = solo_s_ctrl,
1712 .vidioc_g_ext_ctrls = solo_g_ext_ctrls,
1713 .vidioc_s_ext_ctrls = solo_s_ext_ctrls,
1718 .fops = &solo_enc_fops,
1719 .ioctl_ops = &solo_enc_ioctl_ops,
1727 static struct solo_enc_dev *solo_enc_alloc(
struct solo_dev *solo_dev,
u8 ch)
1732 solo_enc = kzalloc(
sizeof(*solo_enc),
GFP_KERNEL);
1737 if (!solo_enc->
vfd) {
1745 *solo_enc->
vfd = solo_enc_template;
1746 solo_enc->
vfd->parent = &solo_dev->
pdev->dev;
1752 return ERR_PTR(ret);
1755 video_set_drvdata(solo_enc->
vfd, solo_enc);
1759 solo_enc->
vfd->num);
1769 solo_enc->
gop = solo_dev->
fps;
1774 spin_lock(&solo_enc->
lock);
1775 solo_update_mode(solo_enc);
1776 spin_unlock(&solo_enc->
lock);
1781 static void solo_enc_free(
struct solo_enc_dev *solo_enc)
1783 if (solo_enc ==
NULL)
1794 for (i = 0; i < solo_dev->
nr_chans; i++) {
1795 solo_dev->
v4l2_enc[
i] = solo_enc_alloc(solo_dev, i);
1801 int ret = PTR_ERR(solo_dev->
v4l2_enc[i]);
1803 solo_enc_free(solo_dev->
v4l2_enc[i]);
1810 dev_info(&solo_dev->
pdev->dev,
"Encoders as /dev/video%d-%d\n",
1823 for (i = 0; i < solo_dev->
nr_chans; i++)
1824 solo_enc_free(solo_dev->
v4l2_enc[i]);