2 * Modified in order to keep it compatible both with new and old videotext IOCTLs by
3 * Michael Geng <linux@MichaelGeng.de>
5 * Cleaned up to use existing videodev interface and allow the idea
6 * of multiple teletext decoders on the video4linux iface. Changed i2c
7 * to cover addressing clashes on device busses. It's also rebuilt so
8 * you can add arbitary multiple teletext devices to Linux video4linux
9 * now (well 32 anyway).
11 * Alan Cox <Alan.Cox@linux.org>
13 * The original driver was heavily modified to match the i2c interface
14 * It was truncated to use the WinTV boards, too.
16 * Copyright (c) 1998 Richard Guenther <richard.guenther@student.uni-tuebingen.de>
18 * $Id: saa5249.c,v 1.1 1998/03/30 22:23:23 alan Exp $
23 * This is a loadable character-device-driver for videotext-interfaces
24 * (aka teletext). Please check the Makefile/README for a list of supported
27 * Copyright (c) 1994-97 Martin Buck <martin-2.buck@student.uni-ulm.de>
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
40 * You should have received a copy of the GNU General Public License
41 * along with this program; if not, write to the Free Software
42 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
46 #include <linux/module.h>
47 #include <linux/kernel.h>
48 #include <linux/sched.h>
50 #include <linux/errno.h>
51 #include <linux/delay.h>
52 #include <linux/ioport.h>
53 #include <linux/slab.h>
54 #include <linux/init.h>
56 #include <linux/i2c.h>
57 #include <linux/videotext.h>
58 #include <linux/videodev.h>
61 #include <asm/uaccess.h>
70 #define IF_NAME "SAA5249"
72 static const int disp_modes[8][3] =
74 { 0x46, 0x03, 0x03 }, /* DISPOFF */
75 { 0x46, 0xcc, 0xcc }, /* DISPNORM */
76 { 0x44, 0x0f, 0x0f }, /* DISPTRANS */
77 { 0x46, 0xcc, 0x46 }, /* DISPINS */
78 { 0x44, 0x03, 0x03 }, /* DISPOFF, interlaced */
79 { 0x44, 0xcc, 0xcc }, /* DISPNORM, interlaced */
80 { 0x44, 0x0f, 0x0f }, /* DISPTRANS, interlaced */
81 { 0x44, 0xcc, 0x46 } /* DISPINS, interlaced */
86 #define PAGE_WAIT (300*HZ/1000) /* Time between requesting page and */
87 /* checking status bits */
88 #define PGBUF_EXPIRE (15*HZ) /* Time to wait before retransmitting */
89 /* page regardless of infobits */
91 u8 pgbuf[VTX_VIRTUALSIZE]; /* Page-buffer */
92 u8 laststat[10]; /* Last value of infobits for DAU */
93 u8 sregs[7]; /* Page-request registers */
94 unsigned long expire; /* Time when page will be expired */
95 unsigned clrfound : 1; /* VTXIOCCLRFOUND has been called */
96 unsigned stopped : 1; /* VTXIOCSTOPDAU has been called */
101 vdau_t vdau[NUM_DAUS]; /* Data for virtual DAUs (the 5249 only has one */
102 /* real DAU, so we have to simulate some more) */
104 int is_searching[NUM_DAUS];
107 struct i2c_client *client;
108 struct semaphore lock;
112 #define CCTWR 34 /* I²C write/read-address of vtx-chip */
114 #define NOACK_REPEAT 10 /* Retry access this many times on failure */
115 #define CLEAR_DELAY (HZ/20) /* Time required to clear a page */
116 #define READY_TIMEOUT (30*HZ/1000) /* Time to wait for ready signal of I²C-bus interface */
117 #define INIT_DELAY 500 /* Time in usec to wait at initialization of CEA interface */
118 #define START_DELAY 10 /* Time in usec to wait before starting write-cycle (CEA) */
120 #define VTX_DEV_MINOR 0
122 /* General defines and debugging support */
129 #define RESCHED do { cond_resched(); } while(0)
131 static struct video_device saa_template; /* Declared near bottom */
133 /* Addresses to scan */
134 static unsigned short normal_i2c[] = {34>>1,I2C_CLIENT_END};
137 static struct i2c_client client_template;
139 static int saa5249_attach(struct i2c_adapter *adap, int addr, int kind)
143 struct i2c_client *client;
144 struct video_device *vd;
145 struct saa5249_device *t;
147 printk(KERN_INFO "saa5249: teletext chip found.\n");
148 client=kmalloc(sizeof(*client), GFP_KERNEL);
151 client_template.adapter = adap;
152 client_template.addr = addr;
153 memcpy(client, &client_template, sizeof(*client));
154 t = kmalloc(sizeof(*t), GFP_KERNEL);
160 memset(t, 0, sizeof(*t));
161 strlcpy(client->name, IF_NAME, I2C_NAME_SIZE);
162 init_MUTEX(&t->lock);
165 * Now create a video4linux device
168 vd = (struct video_device *)kmalloc(sizeof(struct video_device), GFP_KERNEL);
175 i2c_set_clientdata(client, vd);
176 memcpy(vd, &saa_template, sizeof(*vd));
178 for (pgbuf = 0; pgbuf < NUM_DAUS; pgbuf++)
180 memset(t->vdau[pgbuf].pgbuf, ' ', sizeof(t->vdau[0].pgbuf));
181 memset(t->vdau[pgbuf].sregs, 0, sizeof(t->vdau[0].sregs));
182 memset(t->vdau[pgbuf].laststat, 0, sizeof(t->vdau[0].laststat));
183 t->vdau[pgbuf].expire = 0;
184 t->vdau[pgbuf].clrfound = TRUE;
185 t->vdau[pgbuf].stopped = TRUE;
186 t->is_searching[pgbuf] = FALSE;
195 if((err=video_register_device(vd, VFL_TYPE_VTX,-1))<0)
203 i2c_attach_client(client);
208 * We do most of the hard work when we become a device on the i2c.
211 static int saa5249_probe(struct i2c_adapter *adap)
213 if (adap->class & I2C_CLASS_TV_ANALOG)
214 return i2c_probe(adap, &addr_data, saa5249_attach);
218 static int saa5249_detach(struct i2c_client *client)
220 struct video_device *vd = i2c_get_clientdata(client);
221 i2c_detach_client(client);
222 video_unregister_device(vd);
229 /* new I2C driver support */
231 static struct i2c_driver i2c_driver_videotext =
234 .name = IF_NAME, /* name */
236 .id = I2C_DRIVERID_SAA5249, /* in i2c.h */
237 .attach_adapter = saa5249_probe,
238 .detach_client = saa5249_detach,
241 static struct i2c_client client_template = {
242 .driver = &i2c_driver_videotext,
247 * Wait the given number of jiffies (10ms). This calls the scheduler, so the actual
248 * delay may be longer.
251 static void jdelay(unsigned long delay)
253 sigset_t oldblocked = current->blocked;
255 spin_lock_irq(¤t->sighand->siglock);
256 sigfillset(¤t->blocked);
258 spin_unlock_irq(¤t->sighand->siglock);
259 msleep_interruptible(jiffies_to_msecs(delay));
261 spin_lock_irq(¤t->sighand->siglock);
262 current->blocked = oldblocked;
264 spin_unlock_irq(¤t->sighand->siglock);
272 static int i2c_sendbuf(struct saa5249_device *t, int reg, int count, u8 *data)
277 memcpy(buf+1, data, count);
279 if(i2c_master_send(t->client, buf, count+1)==count+1)
284 static int i2c_senddata(struct saa5249_device *t, ...)
286 unsigned char buf[64];
292 while((v=va_arg(argp,int))!=-1)
294 return i2c_sendbuf(t, buf[0], ct-1, buf+1);
297 /* Get count number of bytes from I²C-device at address adr, store them in buf. Start & stop
298 * handshaking is done by this routine, ack will be sent after the last byte to inhibit further
299 * sending of data. If uaccess is TRUE, data is written to user-space with put_user.
300 * Returns -1 if I²C-device didn't send acknowledge, 0 otherwise
303 static int i2c_getdata(struct saa5249_device *t, int count, u8 *buf)
305 if(i2c_master_recv(t->client, buf, count)!=count)
312 * Standard character-device-driver functions
315 static int do_saa5249_ioctl(struct inode *inode, struct file *file,
316 unsigned int cmd, void *arg)
318 static int virtual_mode = FALSE;
319 struct video_device *vd = video_devdata(file);
320 struct saa5249_device *t=vd->priv;
326 vtx_info_t *info = arg;
327 info->version_major = VTX_VER_MAJ;
328 info->version_minor = VTX_VER_MIN;
329 info->numpages = NUM_DAUS;
330 /*info->cct_type = CCT_TYPE;*/
336 vtx_pagereq_t *req = arg;
338 if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
340 memset(t->vdau[req->pgbuf].pgbuf, ' ', sizeof(t->vdau[0].pgbuf));
341 t->vdau[req->pgbuf].clrfound = TRUE;
347 vtx_pagereq_t *req = arg;
349 if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
351 t->vdau[req->pgbuf].clrfound = TRUE;
357 vtx_pagereq_t *req = arg;
358 if (!(req->pagemask & PGMASK_PAGE))
360 if (!(req->pagemask & PGMASK_HOUR))
362 if (!(req->pagemask & PGMASK_MINUTE))
364 if (req->page < 0 || req->page > 0x8ff) /* 7FF ?? */
367 if (req->hour < 0 || req->hour > 0x3f || req->minute < 0 || req->minute > 0x7f ||
368 req->pagemask < 0 || req->pagemask >= PGMASK_MAX || req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
370 t->vdau[req->pgbuf].sregs[0] = (req->pagemask & PG_HUND ? 0x10 : 0) | (req->page / 0x100);
371 t->vdau[req->pgbuf].sregs[1] = (req->pagemask & PG_TEN ? 0x10 : 0) | ((req->page / 0x10) & 0xf);
372 t->vdau[req->pgbuf].sregs[2] = (req->pagemask & PG_UNIT ? 0x10 : 0) | (req->page & 0xf);
373 t->vdau[req->pgbuf].sregs[3] = (req->pagemask & HR_TEN ? 0x10 : 0) | (req->hour / 0x10);
374 t->vdau[req->pgbuf].sregs[4] = (req->pagemask & HR_UNIT ? 0x10 : 0) | (req->hour & 0xf);
375 t->vdau[req->pgbuf].sregs[5] = (req->pagemask & MIN_TEN ? 0x10 : 0) | (req->minute / 0x10);
376 t->vdau[req->pgbuf].sregs[6] = (req->pagemask & MIN_UNIT ? 0x10 : 0) | (req->minute & 0xf);
377 t->vdau[req->pgbuf].stopped = FALSE;
378 t->vdau[req->pgbuf].clrfound = TRUE;
379 t->is_searching[req->pgbuf] = TRUE;
385 vtx_pagereq_t *req = arg;
390 if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
392 if (!t->vdau[req->pgbuf].stopped)
394 if (i2c_senddata(t, 2, 0, -1) ||
395 i2c_sendbuf(t, 3, sizeof(t->vdau[0].sregs), t->vdau[req->pgbuf].sregs) ||
396 i2c_senddata(t, 8, 0, 25, 0, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', -1) ||
397 i2c_senddata(t, 2, 0, t->vdau[req->pgbuf].sregs[0] | 8, -1) ||
398 i2c_senddata(t, 8, 0, 25, 0, -1))
401 if (i2c_getdata(t, 10, infobits))
404 if (!(infobits[8] & 0x10) && !(infobits[7] & 0xf0) && /* check FOUND-bit */
405 (memcmp(infobits, t->vdau[req->pgbuf].laststat, sizeof(infobits)) ||
406 time_after_eq(jiffies, t->vdau[req->pgbuf].expire)))
407 { /* check if new page arrived */
408 if (i2c_senddata(t, 8, 0, 0, 0, -1) ||
409 i2c_getdata(t, VTX_PAGESIZE, t->vdau[req->pgbuf].pgbuf))
411 t->vdau[req->pgbuf].expire = jiffies + PGBUF_EXPIRE;
412 memset(t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE, ' ', VTX_VIRTUALSIZE - VTX_PAGESIZE);
416 if (i2c_senddata(t, 8, 0, 0x20, 0, -1) ||
417 i2c_getdata(t, 40, t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE + 20 * 40))
420 if (i2c_senddata(t, 8, 0, 0x21, 0, -1) ||
421 i2c_getdata(t, 40, t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE + 16 * 40))
423 /* Packet 8/30/0...8/30/15
424 * FIXME: AFAIK, the 5249 does hamming-decoding for some bytes in packet 8/30,
425 * so we should undo this here.
427 if (i2c_senddata(t, 8, 0, 0x22, 0, -1) ||
428 i2c_getdata(t, 40, t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE + 23 * 40))
431 t->vdau[req->pgbuf].clrfound = FALSE;
432 memcpy(t->vdau[req->pgbuf].laststat, infobits, sizeof(infobits));
436 memcpy(infobits, t->vdau[req->pgbuf].laststat, sizeof(infobits));
441 memcpy(infobits, t->vdau[req->pgbuf].laststat, sizeof(infobits));
444 info.pagenum = ((infobits[8] << 8) & 0x700) | ((infobits[1] << 4) & 0xf0) | (infobits[0] & 0x0f);
445 if (info.pagenum < 0x100)
446 info.pagenum += 0x800;
447 info.hour = ((infobits[5] << 4) & 0x30) | (infobits[4] & 0x0f);
448 info.minute = ((infobits[3] << 4) & 0x70) | (infobits[2] & 0x0f);
449 info.charset = ((infobits[7] >> 1) & 7);
450 info.delete = !!(infobits[3] & 8);
451 info.headline = !!(infobits[5] & 4);
452 info.subtitle = !!(infobits[5] & 8);
453 info.supp_header = !!(infobits[6] & 1);
454 info.update = !!(infobits[6] & 2);
455 info.inter_seq = !!(infobits[6] & 4);
456 info.dis_disp = !!(infobits[6] & 8);
457 info.serial = !!(infobits[7] & 1);
458 info.notfound = !!(infobits[8] & 0x10);
459 info.pblf = !!(infobits[9] & 0x20);
461 for (a = 0; a <= 7; a++)
463 if (infobits[a] & 0xf0)
469 if (t->vdau[req->pgbuf].clrfound)
471 if(copy_to_user(req->buffer, &info, sizeof(vtx_pageinfo_t)))
473 if (!info.hamming && !info.notfound)
475 t->is_searching[req->pgbuf] = FALSE;
482 vtx_pagereq_t *req = arg;
485 if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS || req->start < 0 ||
486 req->start > req->end || req->end >= (virtual_mode ? VTX_VIRTUALSIZE : VTX_PAGESIZE))
488 if(copy_to_user(req->buffer, &t->vdau[req->pgbuf].pgbuf[req->start], req->end - req->start + 1))
492 * Always read the time directly from SAA5249
495 if (req->start <= 39 && req->end >= 32)
499 start = max(req->start, 32);
500 end = min(req->end, 39);
502 if (i2c_senddata(t, 8, 0, 0, start, -1) ||
503 i2c_getdata(t, len, buf))
505 if(copy_to_user(req->buffer+start-req->start, buf, len))
508 /* Insert the current header if DAU is still searching for a page */
509 if (req->start <= 31 && req->end >= 7 && t->is_searching[req->pgbuf])
513 start = max(req->start, 7);
514 end = min(req->end, 31);
516 if (i2c_senddata(t, 8, 0, 0, start, -1) ||
517 i2c_getdata(t, len, buf))
519 if(copy_to_user(req->buffer+start-req->start, buf, len))
527 vtx_pagereq_t *req = arg;
529 if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
531 t->vdau[req->pgbuf].stopped = TRUE;
532 t->is_searching[req->pgbuf] = FALSE;
543 if (i2c_senddata(t, 0, NUM_DAUS, 0, 8, -1) || i2c_senddata(t, 11,
544 ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
545 ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', -1))
547 if (i2c_senddata(t, 3, 0x20, -1))
549 jdelay(10 * CLEAR_DELAY); /* I have no idea how long we have to wait here */
555 /* The SAA5249 has virtual-row reception turned on always */
556 t->virtual_mode = (int)(long)arg;
564 * Translates old vtx IOCTLs to new ones
566 * This keeps new kernel versions compatible with old userspace programs.
568 static inline unsigned int vtx_fix_command(unsigned int cmd)
571 case VTXIOCGETINFO_OLD:
574 case VTXIOCCLRPAGE_OLD:
577 case VTXIOCCLRFOUND_OLD:
578 cmd = VTXIOCCLRFOUND;
580 case VTXIOCPAGEREQ_OLD:
583 case VTXIOCGETSTAT_OLD:
586 case VTXIOCGETPAGE_OLD:
589 case VTXIOCSTOPDAU_OLD:
592 case VTXIOCPUTPAGE_OLD:
595 case VTXIOCSETDISP_OLD:
598 case VTXIOCPUTSTAT_OLD:
601 case VTXIOCCLRCACHE_OLD:
602 cmd = VTXIOCCLRCACHE;
604 case VTXIOCSETVIRT_OLD:
615 static int saa5249_ioctl(struct inode *inode, struct file *file,
616 unsigned int cmd, unsigned long arg)
618 struct video_device *vd = video_devdata(file);
619 struct saa5249_device *t=vd->priv;
622 cmd = vtx_fix_command(cmd);
624 err = video_usercopy(inode,file,cmd,arg,do_saa5249_ioctl);
629 static int saa5249_open(struct inode *inode, struct file *file)
631 struct video_device *vd = video_devdata(file);
632 struct saa5249_device *t=vd->priv;
635 err = video_exclusive_open(inode,file);
639 if (t->client==NULL) {
644 if (i2c_senddata(t, 0, 0, -1) || /* Select R11 */
645 /* Turn off parity checks (we do this ourselves) */
646 i2c_senddata(t, 1, disp_modes[t->disp_mode][0], 0, -1) ||
647 /* Display TV-picture, no virtual rows */
648 i2c_senddata(t, 4, NUM_DAUS, disp_modes[t->disp_mode][1], disp_modes[t->disp_mode][2], 7, -1)) /* Set display to page 4 */
655 for (pgbuf = 0; pgbuf < NUM_DAUS; pgbuf++)
657 memset(t->vdau[pgbuf].pgbuf, ' ', sizeof(t->vdau[0].pgbuf));
658 memset(t->vdau[pgbuf].sregs, 0, sizeof(t->vdau[0].sregs));
659 memset(t->vdau[pgbuf].laststat, 0, sizeof(t->vdau[0].laststat));
660 t->vdau[pgbuf].expire = 0;
661 t->vdau[pgbuf].clrfound = TRUE;
662 t->vdau[pgbuf].stopped = TRUE;
663 t->is_searching[pgbuf] = FALSE;
665 t->virtual_mode=FALSE;
669 video_exclusive_release(inode,file);
675 static int saa5249_release(struct inode *inode, struct file *file)
677 struct video_device *vd = video_devdata(file);
678 struct saa5249_device *t=vd->priv;
679 i2c_senddata(t, 1, 0x20, -1); /* Turn off CCT */
680 i2c_senddata(t, 5, 3, 3, -1); /* Turn off TV-display */
681 video_exclusive_release(inode,file);
685 static int __init init_saa_5249 (void)
687 printk(KERN_INFO "SAA5249 driver (" IF_NAME " interface) for VideoText version %d.%d\n",
688 VTX_VER_MAJ, VTX_VER_MIN);
689 return i2c_add_driver(&i2c_driver_videotext);
692 static void __exit cleanup_saa_5249 (void)
694 i2c_del_driver(&i2c_driver_videotext);
697 module_init(init_saa_5249);
698 module_exit(cleanup_saa_5249);
700 static struct file_operations saa_fops = {
701 .owner = THIS_MODULE,
702 .open = saa5249_open,
703 .release = saa5249_release,
704 .ioctl = saa5249_ioctl,
708 static struct video_device saa_template =
710 .owner = THIS_MODULE,
712 .type = VID_TYPE_TELETEXT, /*| VID_TYPE_TUNER ?? */
713 .hardware = VID_HARDWARE_SAA5249,
717 MODULE_LICENSE("GPL");