31 #include <linux/module.h>
32 #include <linux/slab.h>
34 #include <linux/device.h>
49 static unsigned int isoc_debug;
53 #define au0828_isocdbg(fmt, arg...) \
56 printk(KERN_INFO "au0828 %s :"fmt, \
64 char *errmsg =
"Unknown";
68 errmsg =
"unlinked synchronuously";
71 errmsg =
"unlinked asynchronuously";
74 errmsg =
"Buffer error (overrun)";
77 errmsg =
"Stalled (device not responding)";
80 errmsg =
"Babble (bad cable?)";
83 errmsg =
"Bit-stuff error (bad cable?)";
86 errmsg =
"CRC/Timeout (could be anything)";
89 errmsg =
"Device does not respond";
96 packet, status, errmsg);
109 "close and open it again\n");
118 static void au0828_irq_callback(
struct urb *
urb)
122 unsigned long flags = 0;
125 switch (urb->status) {
142 spin_unlock_irqrestore(&dev->
slock, flags);
145 for (i = 0; i < urb->number_of_packets; i++) {
146 urb->iso_frame_desc[
i].status = 0;
147 urb->iso_frame_desc[
i].actual_length = 0;
169 for (i = 0; i < dev->
isoc_ctl.num_bufs; i++) {
177 if (dev->
isoc_ctl.transfer_buffer[i]) {
179 urb->transfer_buffer_length,
201 int num_bufs,
int max_pkt_size,
202 int (*isoc_copy) (
struct au0828_dev *dev,
struct urb *urb))
216 dev->
isoc_ctl.isoc_copy = isoc_copy;
225 dev->
isoc_ctl.transfer_buffer = kzalloc(
sizeof(
void *)*num_bufs,
227 if (!dev->
isoc_ctl.transfer_buffer) {
233 dev->
isoc_ctl.max_pkt_size = max_pkt_size;
236 sb_size = max_packets * dev->
isoc_ctl.max_pkt_size;
239 for (i = 0; i < dev->
isoc_ctl.num_bufs; i++) {
250 if (!dev->
isoc_ctl.transfer_buffer[i]) {
251 printk(
"unable to allocate %i bytes for transfer"
260 pipe = usb_rcvisocpipe(dev->
usbdev,
263 usb_fill_int_urb(urb, dev->
usbdev, pipe,
264 dev->
isoc_ctl.transfer_buffer[i], sb_size,
265 au0828_irq_callback, dma_q, 1);
267 urb->number_of_packets = max_packets;
268 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
271 for (j = 0; j < max_packets; j++) {
272 urb->iso_frame_desc[
j].offset =
k;
273 urb->iso_frame_desc[
j].length =
282 for (i = 0; i < dev->
isoc_ctl.num_bufs; i++) {
298 static inline void buffer_filled(
struct au0828_dev *dev,
306 buf->
vb.field_count++;
315 static inline void vbi_buffer_filled(
struct au0828_dev *dev,
323 buf->
vb.field_count++;
335 static void au0828_copy_video(
struct au0828_dev *dev,
339 unsigned char *
outp,
unsigned long len)
341 void *fieldstart, *startwrite, *startread;
342 int linesdone, currlinedone,
offset, lencopy, remain;
348 if (dma_q->
pos + len > buf->
vb.size)
349 len = buf->
vb.size - dma_q->
pos;
362 offset = linesdone * bytesperline * 2 + currlinedone;
363 startwrite = fieldstart +
offset;
364 lencopy = bytesperline - currlinedone;
365 lencopy = lencopy > remain ? remain : lencopy;
367 if ((
char *)startwrite + lencopy > (
char *)outp + buf->
vb.size) {
369 ((
char *)startwrite + lencopy) -
370 ((
char *)outp + buf->
vb.size));
371 remain = (
char *)outp + buf->
vb.size - (
char *)startwrite;
376 memcpy(startwrite, startread, lencopy);
382 startread += lencopy;
383 if (bytesperline > remain)
388 if ((
char *)startwrite + lencopy > (
char *)outp +
391 ((
char *)startwrite + lencopy) -
392 ((
char *)outp + buf->
vb.size));
393 lencopy = remain = (
char *)outp + buf->
vb.size -
399 memcpy(startwrite, startread, lencopy);
406 if (outp[0] < 0x60 && outp[1440] < 0x60)
421 if (list_empty(&dma_q->
active)) {
435 static void au0828_copy_vbi(
struct au0828_dev *dev,
439 unsigned char *outp,
unsigned long len)
441 unsigned char *startwrite, *startread;
467 if (dma_q->
pos + len > buf->
vb.size)
468 len = buf->
vb.size - dma_q->
pos;
471 startwrite = outp + (dma_q->
pos / 2);
477 for (i = 0; i < len; i += 2)
478 startwrite[j++] = startread[i+1];
493 if (list_empty(&dma_q->
active)) {
504 memset(outp, 0x00, (*buf)->vb.size);
514 static inline int au0828_isoc_copy(
struct au0828_dev *dev,
struct urb *urb)
520 unsigned char *outp =
NULL;
521 unsigned char *vbioutp =
NULL;
522 int i, len = 0,
rc = 1;
525 unsigned int vbi_field_size;
526 unsigned int remain, lencopy;
535 if (urb->status < 0) {
536 print_err_status(dev, -1, urb->status);
537 if (urb->status == -
ENOENT)
549 for (i = 0; i < urb->number_of_packets; i++) {
550 int status = urb->iso_frame_desc[
i].status;
553 print_err_status(dev, i, status);
554 if (urb->iso_frame_desc[i].status != -
EPROTO)
558 if (urb->iso_frame_desc[i].actual_length <= 0)
561 if (urb->iso_frame_desc[i].actual_length >
567 p = urb->transfer_buffer + urb->iso_frame_desc[
i].offset;
569 len = urb->iso_frame_desc[
i].actual_length - 4;
576 (fbyte & 0x40) ?
"odd" :
"even");
580 vbi_buffer_filled(dev,
583 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
592 buffer_filled(dev, dma_q, buf);
593 get_next_buf(dma_q, &buf);
603 jiffies + (
HZ / 10));
606 jiffies + (
HZ / 10));
616 if (vbi_buf !=
NULL) {
629 if (dev->
vbi_read < vbi_field_size) {
630 remain = vbi_field_size - dev->
vbi_read;
637 au0828_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
646 au0828_copy_video(dev, dma_q, buf, p, outp, len);
656 *size = (fh->
dev->width * fh->
dev->height * 16 + 7) >> 3;
671 unsigned long flags = 0;
687 spin_unlock_irqrestore(&dev->
slock, flags);
700 int rc = 0, urb_init = 0;
702 buf->
vb.size = (fh->
dev->width * fh->
dev->height * 16 + 7) >> 3;
704 if (0 != buf->
vb.baddr && buf->
vb.bsize < buf->
vb.size)
765 .buf_setup = buffer_setup,
766 .buf_prepare = buffer_prepare,
767 .buf_queue = buffer_queue,
768 .buf_release = buffer_release,
775 static int au0828_i2s_init(
struct au0828_dev *dev)
788 dprintk(1,
"au0828_analog_stream_enable called\n");
808 dprintk(1,
"au0828_analog_stream_disable called\n");
815 dprintk(1,
"au0828_analog_stream_reset called\n");
824 static int au0828_stream_interrupt(
struct au0828_dev *dev)
833 dprintk(1,
"%s device is misconfigured!\n", __func__);
845 dprintk(1,
"au0828_release_resources called\n");
858 static int res_get(
struct au0828_fh *fh,
unsigned int bit)
874 dprintk(1,
"res: get %d\n", bit);
879 static int res_check(
struct au0828_fh *fh,
unsigned int bit)
884 static int res_locked(
struct au0828_dev *dev,
unsigned int bit)
889 static void res_free(
struct au0828_fh *fh,
unsigned int bits)
897 dprintk(1,
"res: put %d\n", bits);
900 static int get_ressource(
struct au0828_fh *fh)
921 unsigned char *vid_data;
922 unsigned long flags = 0;
929 memset(vid_data, 0x00, buf->
vb.size);
930 buffer_filled(dev, dma_q, buf);
932 get_next_buf(dma_q, &buf);
937 spin_unlock_irqrestore(&dev->
slock, flags);
946 unsigned long flags = 0;
953 memset(vbi_data, 0x00, buf->
vb.size);
954 vbi_buffer_filled(dev, dma_q, buf);
956 vbi_get_next_buf(dma_q, &buf);
960 spin_unlock_irqrestore(&dev->
slock, flags);
964 static int au0828_v4l2_open(
struct file *filp)
985 dprintk(1,
"Failed allocate au0828_fh struct!\n");
1010 au0828_i2s_init(dev);
1037 static int au0828_v4l2_close(
struct file *filp)
1061 if (dev->
users == 1) {
1090 static ssize_t au0828_v4l2_read(
struct file *filp,
char __user *buf,
1091 size_t count, loff_t *
pos)
1097 rc = check_dev(dev);
1133 rc = check_dev(dev);
1156 rc = check_dev(dev);
1168 static int au0828_set_format(
struct au0828_dev *dev,
unsigned int cmd,
1192 format->
fmt.
pix.bytesperline = width * 2;
1193 format->
fmt.
pix.sizeimage = width * height * 2;
1208 dprintk(1,
"VIDIOC_SET_FMT: interrupting stream!\n");
1209 ret = au0828_stream_interrupt(dev);
1211 dprintk(1,
"error interrupting video stream!\n");
1229 static int vidioc_queryctrl(
struct file *
file,
void *
priv,
1241 static int vidioc_querycap(
struct file *file,
void *priv,
1261 static int vidioc_enum_fmt_vid_cap(
struct file *file,
void *priv,
1276 static int vidioc_g_fmt_vid_cap(
struct file *file,
void *priv,
1292 static int vidioc_try_fmt_vid_cap(
struct file *file,
void *priv,
1301 static int vidioc_s_fmt_vid_cap(
struct file *file,
void *priv,
1308 rc = check_dev(dev);
1323 static int vidioc_s_std(
struct file *file,
void *priv,
v4l2_std_id * norm)
1328 if (dev->
dvb.frontend && dev->
dvb.frontend->ops.analog_ops.i2c_gate_ctrl)
1329 dev->
dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->
dvb.frontend, 1);
1338 if (dev->
dvb.frontend && dev->
dvb.frontend->ops.analog_ops.i2c_gate_ctrl)
1339 dev->
dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->
dvb.frontend, 0);
1344 static int vidioc_enum_input(
struct file *file,
void *priv,
1351 static const char *inames[] = {
1376 input->
std = dev->
vdev->tvnorms;
1381 static int vidioc_g_input(
struct file *file,
void *priv,
unsigned int *i)
1389 static int vidioc_s_input(
struct file *file,
void *priv,
unsigned int index)
1395 dprintk(1,
"VIDIOC_S_INPUT in function %s, input=%d\n", __func__,
1414 dprintk(1,
"VIDIOC_S_INPUT unknown input type set [%d]\n",
1448 static int vidioc_g_audio(
struct file *file,
void *priv,
struct v4l2_audio *
a)
1452 unsigned int index = a->
index;
1468 static int vidioc_s_audio(
struct file *file,
void *priv,
const struct v4l2_audio *a)
1477 static int vidioc_g_ctrl(
struct file *file,
void *priv,
1488 static int vidioc_s_ctrl(
struct file *file,
void *priv,
1497 static int vidioc_g_tuner(
struct file *file,
void *priv,
struct v4l2_tuner *
t)
1510 static int vidioc_s_tuner(
struct file *file,
void *priv,
1521 if (dev->
dvb.frontend && dev->
dvb.frontend->ops.analog_ops.i2c_gate_ctrl)
1522 dev->
dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->
dvb.frontend, 1);
1526 if (dev->
dvb.frontend && dev->
dvb.frontend->ops.analog_ops.i2c_gate_ctrl)
1527 dev->
dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->
dvb.frontend, 0);
1529 dprintk(1,
"VIDIOC_S_TUNER: signal = %x, afc = %x\n", t->
signal,
1536 static int vidioc_g_frequency(
struct file *file,
void *priv,
1547 static int vidioc_s_frequency(
struct file *file,
void *priv,
1553 if (freq->
tuner != 0)
1560 if (dev->
dvb.frontend && dev->
dvb.frontend->ops.analog_ops.i2c_gate_ctrl)
1561 dev->
dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->
dvb.frontend, 1);
1568 dev->
vdev->tvnorms);
1574 if (dev->
dvb.frontend && dev->
dvb.frontend->ops.analog_ops.i2c_gate_ctrl)
1575 dev->
dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->
dvb.frontend, 0);
1585 static int vidioc_g_fmt_vbi_cap(
struct file *file,
void *priv,
1593 format->
fmt.
vbi.offset = 0;
1594 format->
fmt.
vbi.flags = 0;
1595 format->
fmt.
vbi.sampling_rate = 6750000 * 4 / 2;
1599 format->
fmt.
vbi.start[0] = 21;
1600 format->
fmt.
vbi.start[1] = 284;
1605 static int vidioc_g_chip_ident(
struct file *file,
void *priv,
1625 static int vidioc_cropcap(
struct file *file,
void *priv,
1647 static int vidioc_streamon(
struct file *file,
void *priv,
1654 rc = check_dev(dev);
1661 dprintk(1,
"vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1664 if (
unlikely(!res_get(fh, get_ressource(fh))))
1685 static int vidioc_streamoff(
struct file *file,
void *priv,
1693 rc = check_dev(dev);
1700 if (type != fh->
type)
1703 dprintk(1,
"vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1711 rc = au0828_stream_interrupt(dev);
1738 #ifdef CONFIG_VIDEO_ADV_DEBUG
1739 static int vidioc_g_register(
struct file *file,
void *priv,
1745 switch (reg->
match.type) {
1758 static int vidioc_s_register(
struct file *file,
void *priv,
1764 switch (reg->
match.type) {
1776 static int vidioc_reqbufs(
struct file *file,
void *priv,
1783 rc = check_dev(dev);
1795 static int vidioc_querybuf(
struct file *file,
void *priv,
1802 rc = check_dev(dev);
1814 static int vidioc_qbuf(
struct file *file,
void *priv,
struct v4l2_buffer *b)
1820 rc = check_dev(dev);
1832 static int vidioc_dqbuf(
struct file *file,
void *priv,
struct v4l2_buffer *b)
1838 rc = check_dev(dev);
1845 dprintk(1,
"Detected green frame. Resetting stream...\n");
1860 .open = au0828_v4l2_open,
1861 .release = au0828_v4l2_close,
1862 .read = au0828_v4l2_read,
1863 .poll = au0828_v4l2_poll,
1864 .mmap = au0828_v4l2_mmap,
1896 #ifdef CONFIG_VIDEO_ADV_DEBUG
1897 .vidioc_g_register = vidioc_g_register,
1898 .vidioc_s_register = vidioc_s_register,
1903 static const struct video_device au0828_video_template = {
1904 .fops = &au0828_v4l_fops,
1906 .ioctl_ops = &video_ioctl_ops,
1917 struct usb_host_interface *iface_desc;
1921 dprintk(1,
"au0828_analog_register called!\n");
1925 interface->cur_altsetting->desc.bInterfaceNumber, 5);
1932 iface_desc = interface->cur_altsetting;
1933 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
1934 endpoint = &iface_desc->endpoint[
i].desc;
1943 (((tmp & 0x1800) >> 11) + 1);
1957 INIT_LIST_HEAD(&dev->
vidq.active);
1958 INIT_LIST_HEAD(&dev->
vidq.queued);
1959 INIT_LIST_HEAD(&dev->
vbiq.active);
1960 INIT_LIST_HEAD(&dev->
vbiq.queued);
1980 dprintk(1,
"Can't allocate video_device.\n");
1987 dprintk(1,
"Can't allocate vbi_device.\n");
1993 *dev->
vdev = au0828_video_template;
1999 *dev->
vbi_dev = au0828_video_template;
2005 video_set_drvdata(dev->
vdev, dev);
2008 dprintk(1,
"unable to register video device (error = %d).\n",
2015 video_set_drvdata(dev->
vbi_dev, dev);
2018 dprintk(1,
"unable to register vbi device (error = %d).\n",
2024 dprintk(1,
"%s completed!\n", __func__);