2 * Driver for the SAA5246A or SAA5281 Teletext (=Videotext) decoder chips from
5 * Only capturing of Teletext pages is tested. The videotext chips also have a
6 * TV output but my hardware doesn't use it. For this reason this driver does
7 * not support changing any TV display settings.
9 * Copyright (C) 2004 Michael Geng <linux@MichaelGeng.de>
14 * Copyright (C) 1998 Richard Guenther
15 * <richard.guenther@student.uni-tuebingen.de>
18 * Alan Cox <Alan.Cox@linux.org>
23 * Copyright (C) 1994-97 Martin Buck <martin-2.buck@student.uni-ulm.de>
25 * This program is free software; you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation; either version 2 of the License, or
28 * (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program; if not, write to the Free Software
37 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
41 #include <linux/module.h>
42 #include <linux/kernel.h>
44 #include <linux/init.h>
45 #include <linux/i2c.h>
46 #include <linux/videotext.h>
47 #include <linux/smp_lock.h>
48 #include <linux/videodev.h>
49 #include <media/v4l2-common.h>
50 #include <media/v4l2-ioctl.h>
51 #include <linux/mutex.h>
55 MODULE_AUTHOR("Michael Geng <linux@MichaelGeng.de>");
56 MODULE_DESCRIPTION("Philips SAA5246A, SAA5281 Teletext decoder driver");
57 MODULE_LICENSE("GPL");
59 struct saa5246a_device
61 u8 pgbuf[NUM_DAUS][VTX_VIRTUALSIZE];
62 int is_searching[NUM_DAUS];
63 struct i2c_client *client;
67 static struct video_device saa_template; /* Declared near bottom */
69 /* Addresses to scan */
70 static unsigned short normal_i2c[] = { I2C_ADDRESS, I2C_CLIENT_END };
74 static struct i2c_client client_template;
76 static int saa5246a_attach(struct i2c_adapter *adap, int addr, int kind)
80 struct i2c_client *client;
81 struct video_device *vd;
82 struct saa5246a_device *t;
84 printk(KERN_INFO "saa5246a: teletext chip found.\n");
85 client=kmalloc(sizeof(*client), GFP_KERNEL);
88 client_template.adapter = adap;
89 client_template.addr = addr;
90 memcpy(client, &client_template, sizeof(*client));
91 t = kzalloc(sizeof(*t), GFP_KERNEL);
97 strlcpy(client->name, IF_NAME, I2C_NAME_SIZE);
101 * Now create a video4linux device
104 vd = video_device_alloc();
111 i2c_set_clientdata(client, vd);
112 memcpy(vd, &saa_template, sizeof(*vd));
114 for (pgbuf = 0; pgbuf < NUM_DAUS; pgbuf++)
116 memset(t->pgbuf[pgbuf], ' ', sizeof(t->pgbuf[0]));
117 t->is_searching[pgbuf] = false;
126 if((err=video_register_device(vd, VFL_TYPE_VTX,-1))<0)
130 video_device_release(vd);
134 i2c_attach_client(client);
139 * We do most of the hard work when we become a device on the i2c.
141 static int saa5246a_probe(struct i2c_adapter *adap)
143 if (adap->class & I2C_CLASS_TV_ANALOG)
144 return i2c_probe(adap, &addr_data, saa5246a_attach);
148 static int saa5246a_detach(struct i2c_client *client)
150 struct video_device *vd = i2c_get_clientdata(client);
151 i2c_detach_client(client);
152 video_unregister_device(vd);
162 static struct i2c_driver i2c_driver_videotext =
165 .name = IF_NAME, /* name */
167 .id = I2C_DRIVERID_SAA5249, /* in i2c.h */
168 .attach_adapter = saa5246a_probe,
169 .detach_client = saa5246a_detach,
172 static struct i2c_client client_template = {
173 .driver = &i2c_driver_videotext,
177 static int i2c_sendbuf(struct saa5246a_device *t, int reg, int count, u8 *data)
182 memcpy(buf+1, data, count);
184 if(i2c_master_send(t->client, buf, count+1)==count+1)
189 static int i2c_senddata(struct saa5246a_device *t, ...)
191 unsigned char buf[64];
197 while ((v = va_arg(argp, int)) != -1)
201 return i2c_sendbuf(t, buf[0], ct-1, buf+1);
204 /* Get count number of bytes from I²C-device at address adr, store them in buf.
205 * Start & stop handshaking is done by this routine, ack will be sent after the
206 * last byte to inhibit further sending of data. If uaccess is 'true', data is
207 * written to user-space with put_user. Returns -1 if I²C-device didn't send
208 * acknowledge, 0 otherwise
210 static int i2c_getdata(struct saa5246a_device *t, int count, u8 *buf)
212 if(i2c_master_recv(t->client, buf, count)!=count)
217 /* When a page is found then the not FOUND bit in one of the status registers
218 * of the SAA5264A chip is cleared. Unfortunately this bit is not set
219 * automatically when a new page is requested. Instead this function must be
220 * called after a page has been requested.
222 * Return value: 0 if successful
224 static int saa5246a_clear_found_bit(struct saa5246a_device *t,
225 unsigned char dau_no)
227 unsigned char row_25_column_8;
229 if (i2c_senddata(t, SAA5246A_REGISTER_R8,
232 R8_DO_NOT_CLEAR_MEMORY,
239 i2c_getdata(t, 1, &row_25_column_8))
243 row_25_column_8 |= ROW25_COLUMN8_PAGE_NOT_FOUND;
244 if (i2c_senddata(t, SAA5246A_REGISTER_R8,
247 R8_DO_NOT_CLEAR_MEMORY,
263 /* Requests one videotext page as described in req. The fields of req are
264 * checked and an error is returned if something is invalid.
266 * Return value: 0 if successful
268 static int saa5246a_request_page(struct saa5246a_device *t,
271 if (req->pagemask < 0 || req->pagemask >= PGMASK_MAX)
273 if (req->pagemask & PGMASK_PAGE)
274 if (req->page < 0 || req->page > PAGE_MAX)
276 if (req->pagemask & PGMASK_HOUR)
277 if (req->hour < 0 || req->hour > HOUR_MAX)
279 if (req->pagemask & PGMASK_MINUTE)
280 if (req->minute < 0 || req->minute > MINUTE_MAX)
282 if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
285 if (i2c_senddata(t, SAA5246A_REGISTER_R2,
287 R2_IN_R3_SELECT_PAGE_HUNDREDS |
290 R2_HAMMING_CHECK_OFF,
292 HUNDREDS_OF_PAGE(req->page) |
294 (req->pagemask & PG_HUND ?
295 R3_PAGE_HUNDREDS_DO_CARE :
296 R3_PAGE_HUNDREDS_DO_NOT_CARE),
298 TENS_OF_PAGE(req->page) |
299 (req->pagemask & PG_TEN ?
300 R3_PAGE_TENS_DO_CARE :
301 R3_PAGE_TENS_DO_NOT_CARE),
303 UNITS_OF_PAGE(req->page) |
304 (req->pagemask & PG_UNIT ?
305 R3_PAGE_UNITS_DO_CARE :
306 R3_PAGE_UNITS_DO_NOT_CARE),
308 TENS_OF_HOUR(req->hour) |
309 (req->pagemask & HR_TEN ?
310 R3_HOURS_TENS_DO_CARE :
311 R3_HOURS_TENS_DO_NOT_CARE),
313 UNITS_OF_HOUR(req->hour) |
314 (req->pagemask & HR_UNIT ?
315 R3_HOURS_UNITS_DO_CARE :
316 R3_HOURS_UNITS_DO_NOT_CARE),
318 TENS_OF_MINUTE(req->minute) |
319 (req->pagemask & MIN_TEN ?
320 R3_MINUTES_TENS_DO_CARE :
321 R3_MINUTES_TENS_DO_NOT_CARE),
323 UNITS_OF_MINUTE(req->minute) |
324 (req->pagemask & MIN_UNIT ?
325 R3_MINUTES_UNITS_DO_CARE :
326 R3_MINUTES_UNITS_DO_NOT_CARE),
328 COMMAND_END) || i2c_senddata(t, SAA5246A_REGISTER_R2,
330 R2_IN_R3_SELECT_PAGE_HUNDREDS |
333 R2_HAMMING_CHECK_OFF,
335 HUNDREDS_OF_PAGE(req->page) |
337 (req->pagemask & PG_HUND ?
338 R3_PAGE_HUNDREDS_DO_CARE :
339 R3_PAGE_HUNDREDS_DO_NOT_CARE),
346 t->is_searching[req->pgbuf] = true;
350 /* This routine decodes the page number from the infobits contained in line 25.
353 * infobits: must be bits 0 to 9 of column 25
355 * Return value: page number coded in hexadecimal, i. e. page 123 is coded 0x123
357 static inline int saa5246a_extract_pagenum_from_infobits(
358 unsigned char infobits[10])
360 int page_hundreds, page_tens, page_units;
362 page_units = infobits[0] & ROW25_COLUMN0_PAGE_UNITS;
363 page_tens = infobits[1] & ROW25_COLUMN1_PAGE_TENS;
364 page_hundreds = infobits[8] & ROW25_COLUMN8_PAGE_HUNDREDS;
366 /* page 0x.. means page 8.. */
367 if (page_hundreds == 0)
370 return((page_hundreds << 8) | (page_tens << 4) | page_units);
373 /* Decodes the hour from the infobits contained in line 25.
376 * infobits: must be bits 0 to 9 of column 25
378 * Return: hour coded in hexadecimal, i. e. 12h is coded 0x12
380 static inline int saa5246a_extract_hour_from_infobits(
381 unsigned char infobits[10])
383 int hour_tens, hour_units;
385 hour_units = infobits[4] & ROW25_COLUMN4_HOUR_UNITS;
386 hour_tens = infobits[5] & ROW25_COLUMN5_HOUR_TENS;
388 return((hour_tens << 4) | hour_units);
391 /* Decodes the minutes from the infobits contained in line 25.
394 * infobits: must be bits 0 to 9 of column 25
396 * Return: minutes coded in hexadecimal, i. e. 10min is coded 0x10
398 static inline int saa5246a_extract_minutes_from_infobits(
399 unsigned char infobits[10])
401 int minutes_tens, minutes_units;
403 minutes_units = infobits[2] & ROW25_COLUMN2_MINUTES_UNITS;
404 minutes_tens = infobits[3] & ROW25_COLUMN3_MINUTES_TENS;
406 return((minutes_tens << 4) | minutes_units);
409 /* Reads the status bits contained in the first 10 columns of the first line
410 * and extracts the information into info.
412 * Return value: 0 if successful
414 static inline int saa5246a_get_status(struct saa5246a_device *t,
415 vtx_pageinfo_t *info, unsigned char dau_no)
417 unsigned char infobits[10];
420 if (dau_no >= NUM_DAUS)
423 if (i2c_senddata(t, SAA5246A_REGISTER_R8,
426 R8_DO_NOT_CLEAR_MEMORY,
433 i2c_getdata(t, 10, infobits))
438 info->pagenum = saa5246a_extract_pagenum_from_infobits(infobits);
439 info->hour = saa5246a_extract_hour_from_infobits(infobits);
440 info->minute = saa5246a_extract_minutes_from_infobits(infobits);
441 info->charset = ((infobits[7] & ROW25_COLUMN7_CHARACTER_SET) >> 1);
442 info->delete = !!(infobits[3] & ROW25_COLUMN3_DELETE_PAGE);
443 info->headline = !!(infobits[5] & ROW25_COLUMN5_INSERT_HEADLINE);
444 info->subtitle = !!(infobits[5] & ROW25_COLUMN5_INSERT_SUBTITLE);
445 info->supp_header = !!(infobits[6] & ROW25_COLUMN6_SUPPRESS_HEADER);
446 info->update = !!(infobits[6] & ROW25_COLUMN6_UPDATE_PAGE);
447 info->inter_seq = !!(infobits[6] & ROW25_COLUMN6_INTERRUPTED_SEQUENCE);
448 info->dis_disp = !!(infobits[6] & ROW25_COLUMN6_SUPPRESS_DISPLAY);
449 info->serial = !!(infobits[7] & ROW25_COLUMN7_SERIAL_MODE);
450 info->notfound = !!(infobits[8] & ROW25_COLUMN8_PAGE_NOT_FOUND);
451 info->pblf = !!(infobits[9] & ROW25_COLUMN9_PAGE_BEING_LOOKED_FOR);
453 for (column = 0; column <= 7; column++) {
454 if (infobits[column] & ROW25_COLUMN0_TO_7_HAMMING_ERROR) {
459 if (!info->hamming && !info->notfound)
460 t->is_searching[dau_no] = false;
464 /* Reads 1 videotext page buffer of the SAA5246A.
466 * req is used both as input and as output. It contains information which part
467 * must be read. The videotext page is copied into req->buffer.
469 * Return value: 0 if successful
471 static inline int saa5246a_get_page(struct saa5246a_device *t,
474 int start, end, size;
478 if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS ||
479 req->start < 0 || req->start > req->end || req->end >= VTX_PAGESIZE)
482 buf = kmalloc(VTX_PAGESIZE, GFP_KERNEL);
486 /* Read "normal" part of page */
489 end = min(req->end, VTX_PAGESIZE - 1);
490 if (i2c_senddata(t, SAA5246A_REGISTER_R8,
491 req->pgbuf | R8_DO_NOT_CLEAR_MEMORY,
492 ROW(req->start), COLUMN(req->start), COMMAND_END))
494 if (i2c_getdata(t, end - req->start + 1, buf))
497 if (copy_to_user(req->buffer, buf, end - req->start + 1))
500 /* Always get the time from buffer 4, since this stupid SAA5246A only
501 * updates the currently displayed buffer...
503 if (REQ_CONTAINS_TIME(req)) {
504 start = max(req->start, POS_TIME_START);
505 end = min(req->end, POS_TIME_END);
506 size = end - start + 1;
511 if (i2c_senddata(t, SAA5246A_REGISTER_R8,
512 R8_ACTIVE_CHAPTER_4 | R8_DO_NOT_CLEAR_MEMORY,
513 R9_CURSER_ROW_0, start, COMMAND_END))
515 if (i2c_getdata(t, size, buf))
518 if (copy_to_user(req->buffer + start - req->start, buf, size))
521 /* Insert the header from buffer 4 only, if acquisition circuit is still searching for a page */
522 if (REQ_CONTAINS_HEADER(req) && t->is_searching[req->pgbuf]) {
523 start = max(req->start, POS_HEADER_START);
524 end = min(req->end, POS_HEADER_END);
525 size = end - start + 1;
530 if (i2c_senddata(t, SAA5246A_REGISTER_R8,
531 R8_ACTIVE_CHAPTER_4 | R8_DO_NOT_CLEAR_MEMORY,
532 R9_CURSER_ROW_0, start, COMMAND_END))
534 if (i2c_getdata(t, end - start + 1, buf))
537 if (copy_to_user(req->buffer + start - req->start, buf, size))
546 /* Stops the acquisition circuit given in dau_no. The page buffer associated
547 * with this acquisition circuit will no more be updated. The other daus are
550 * Return value: 0 if successful
552 static inline int saa5246a_stop_dau(struct saa5246a_device *t,
553 unsigned char dau_no)
555 if (dau_no >= NUM_DAUS)
557 if (i2c_senddata(t, SAA5246A_REGISTER_R2,
559 R2_IN_R3_SELECT_PAGE_HUNDREDS |
562 R2_HAMMING_CHECK_OFF,
566 R3_PAGE_HUNDREDS_DO_NOT_CARE,
572 t->is_searching[dau_no] = false;
576 /* Handles ioctls defined in videotext.h
578 * Returns 0 if successful
580 static int do_saa5246a_ioctl(struct inode *inode, struct file *file,
581 unsigned int cmd, void *arg)
583 struct video_device *vd = video_devdata(file);
584 struct saa5246a_device *t=vd->priv;
589 vtx_info_t *info = arg;
591 info->version_major = MAJOR_VERSION;
592 info->version_minor = MINOR_VERSION;
593 info->numpages = NUM_DAUS;
599 vtx_pagereq_t *req = arg;
601 if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
603 memset(t->pgbuf[req->pgbuf], ' ', sizeof(t->pgbuf[0]));
609 vtx_pagereq_t *req = arg;
611 if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
613 return(saa5246a_clear_found_bit(t, req->pgbuf));
618 vtx_pagereq_t *req = arg;
620 return(saa5246a_request_page(t, req));
625 vtx_pagereq_t *req = arg;
629 if ((rval = saa5246a_get_status(t, &info, req->pgbuf)))
631 if(copy_to_user(req->buffer, &info,
632 sizeof(vtx_pageinfo_t)))
639 vtx_pagereq_t *req = arg;
641 return(saa5246a_get_page(t, req));
646 vtx_pagereq_t *req = arg;
648 return(saa5246a_stop_dau(t, req->pgbuf));
663 /* I do not know what "virtual mode" means */
671 * Translates old vtx IOCTLs to new ones
673 * This keeps new kernel versions compatible with old userspace programs.
675 static inline unsigned int vtx_fix_command(unsigned int cmd)
678 case VTXIOCGETINFO_OLD:
681 case VTXIOCCLRPAGE_OLD:
684 case VTXIOCCLRFOUND_OLD:
685 cmd = VTXIOCCLRFOUND;
687 case VTXIOCPAGEREQ_OLD:
690 case VTXIOCGETSTAT_OLD:
693 case VTXIOCGETPAGE_OLD:
696 case VTXIOCSTOPDAU_OLD:
699 case VTXIOCPUTPAGE_OLD:
702 case VTXIOCSETDISP_OLD:
705 case VTXIOCPUTSTAT_OLD:
708 case VTXIOCCLRCACHE_OLD:
709 cmd = VTXIOCCLRCACHE;
711 case VTXIOCSETVIRT_OLD:
721 static int saa5246a_ioctl(struct inode *inode, struct file *file,
722 unsigned int cmd, unsigned long arg)
724 struct video_device *vd = video_devdata(file);
725 struct saa5246a_device *t = vd->priv;
728 cmd = vtx_fix_command(cmd);
729 mutex_lock(&t->lock);
730 err = video_usercopy(inode, file, cmd, arg, do_saa5246a_ioctl);
731 mutex_unlock(&t->lock);
735 static int saa5246a_open(struct inode *inode, struct file *file)
737 struct video_device *vd = video_devdata(file);
738 struct saa5246a_device *t = vd->priv;
742 err = video_exclusive_open(inode,file);
748 if (t->client==NULL) {
753 if (i2c_senddata(t, SAA5246A_REGISTER_R0,
756 R0_PLL_TIME_CONSTANT_LONG |
757 R0_ENABLE_nODD_EVEN_OUTPUT |
759 R0_DO_NOT_FORCE_nODD_EVEN_LOW_IF_PICTURE_DISPLAYED |
761 R0_NO_AUTOMATIC_FASTEXT_PROMPT,
763 R1_NON_INTERLACED_312_312_LINES |
765 R1_EXTENDED_PACKET_DISABLE |
767 R1_8_BITS_NO_PARITY |
771 i2c_senddata(t, SAA5246A_REGISTER_R4,
773 /* We do not care much for the TV display but nevertheless we
774 * need the currently displayed page later because only on that
775 * page the time is updated. */
788 video_exclusive_release(inode,file);
793 static int saa5246a_release(struct inode *inode, struct file *file)
795 struct video_device *vd = video_devdata(file);
796 struct saa5246a_device *t = vd->priv;
798 /* Stop all acquisition circuits. */
799 i2c_senddata(t, SAA5246A_REGISTER_R1,
801 R1_INTERLACED_312_AND_HALF_312_AND_HALF_LINES |
803 R1_EXTENDED_PACKET_DISABLE |
805 R1_8_BITS_NO_PARITY |
809 video_exclusive_release(inode,file);
813 static int __init init_saa_5246a (void)
816 "SAA5246A (or compatible) Teletext decoder driver version %d.%d\n",
817 MAJOR_VERSION, MINOR_VERSION);
818 return i2c_add_driver(&i2c_driver_videotext);
821 static void __exit cleanup_saa_5246a (void)
823 i2c_del_driver(&i2c_driver_videotext);
826 module_init(init_saa_5246a);
827 module_exit(cleanup_saa_5246a);
829 static const struct file_operations saa_fops = {
830 .owner = THIS_MODULE,
831 .open = saa5246a_open,
832 .release = saa5246a_release,
833 .ioctl = saa5246a_ioctl,
837 static struct video_device saa_template =
841 .release = video_device_release,