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/videodev.h>
48 #include <media/v4l2-common.h>
49 #include <media/v4l2-ioctl.h>
50 #include <linux/mutex.h>
54 MODULE_AUTHOR("Michael Geng <linux@MichaelGeng.de>");
55 MODULE_DESCRIPTION("Philips SAA5246A, SAA5281 Teletext decoder driver");
56 MODULE_LICENSE("GPL");
58 struct saa5246a_device
60 u8 pgbuf[NUM_DAUS][VTX_VIRTUALSIZE];
61 int is_searching[NUM_DAUS];
62 struct i2c_client *client;
66 static struct video_device saa_template; /* Declared near bottom */
68 /* Addresses to scan */
69 static unsigned short normal_i2c[] = { I2C_ADDRESS, I2C_CLIENT_END };
73 static struct i2c_client client_template;
75 static int saa5246a_attach(struct i2c_adapter *adap, int addr, int kind)
79 struct i2c_client *client;
80 struct video_device *vd;
81 struct saa5246a_device *t;
83 printk(KERN_INFO "saa5246a: teletext chip found.\n");
84 client=kmalloc(sizeof(*client), GFP_KERNEL);
87 client_template.adapter = adap;
88 client_template.addr = addr;
89 memcpy(client, &client_template, sizeof(*client));
90 t = kzalloc(sizeof(*t), GFP_KERNEL);
96 strlcpy(client->name, IF_NAME, I2C_NAME_SIZE);
100 * Now create a video4linux device
103 vd = video_device_alloc();
110 i2c_set_clientdata(client, vd);
111 memcpy(vd, &saa_template, sizeof(*vd));
113 for (pgbuf = 0; pgbuf < NUM_DAUS; pgbuf++)
115 memset(t->pgbuf[pgbuf], ' ', sizeof(t->pgbuf[0]));
116 t->is_searching[pgbuf] = false;
125 if((err=video_register_device(vd, VFL_TYPE_VTX,-1))<0)
129 video_device_release(vd);
133 i2c_attach_client(client);
138 * We do most of the hard work when we become a device on the i2c.
140 static int saa5246a_probe(struct i2c_adapter *adap)
142 if (adap->class & I2C_CLASS_TV_ANALOG)
143 return i2c_probe(adap, &addr_data, saa5246a_attach);
147 static int saa5246a_detach(struct i2c_client *client)
149 struct video_device *vd = i2c_get_clientdata(client);
150 i2c_detach_client(client);
151 video_unregister_device(vd);
161 static struct i2c_driver i2c_driver_videotext =
164 .name = IF_NAME, /* name */
166 .id = I2C_DRIVERID_SAA5249, /* in i2c.h */
167 .attach_adapter = saa5246a_probe,
168 .detach_client = saa5246a_detach,
171 static struct i2c_client client_template = {
172 .driver = &i2c_driver_videotext,
176 static int i2c_sendbuf(struct saa5246a_device *t, int reg, int count, u8 *data)
181 memcpy(buf+1, data, count);
183 if(i2c_master_send(t->client, buf, count+1)==count+1)
188 static int i2c_senddata(struct saa5246a_device *t, ...)
190 unsigned char buf[64];
196 while ((v = va_arg(argp, int)) != -1)
200 return i2c_sendbuf(t, buf[0], ct-1, buf+1);
203 /* Get count number of bytes from I²C-device at address adr, store them in buf.
204 * Start & stop handshaking is done by this routine, ack will be sent after the
205 * last byte to inhibit further sending of data. If uaccess is 'true', data is
206 * written to user-space with put_user. Returns -1 if I²C-device didn't send
207 * acknowledge, 0 otherwise
209 static int i2c_getdata(struct saa5246a_device *t, int count, u8 *buf)
211 if(i2c_master_recv(t->client, buf, count)!=count)
216 /* When a page is found then the not FOUND bit in one of the status registers
217 * of the SAA5264A chip is cleared. Unfortunately this bit is not set
218 * automatically when a new page is requested. Instead this function must be
219 * called after a page has been requested.
221 * Return value: 0 if successful
223 static int saa5246a_clear_found_bit(struct saa5246a_device *t,
224 unsigned char dau_no)
226 unsigned char row_25_column_8;
228 if (i2c_senddata(t, SAA5246A_REGISTER_R8,
231 R8_DO_NOT_CLEAR_MEMORY,
238 i2c_getdata(t, 1, &row_25_column_8))
242 row_25_column_8 |= ROW25_COLUMN8_PAGE_NOT_FOUND;
243 if (i2c_senddata(t, SAA5246A_REGISTER_R8,
246 R8_DO_NOT_CLEAR_MEMORY,
262 /* Requests one videotext page as described in req. The fields of req are
263 * checked and an error is returned if something is invalid.
265 * Return value: 0 if successful
267 static int saa5246a_request_page(struct saa5246a_device *t,
270 if (req->pagemask < 0 || req->pagemask >= PGMASK_MAX)
272 if (req->pagemask & PGMASK_PAGE)
273 if (req->page < 0 || req->page > PAGE_MAX)
275 if (req->pagemask & PGMASK_HOUR)
276 if (req->hour < 0 || req->hour > HOUR_MAX)
278 if (req->pagemask & PGMASK_MINUTE)
279 if (req->minute < 0 || req->minute > MINUTE_MAX)
281 if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
284 if (i2c_senddata(t, SAA5246A_REGISTER_R2,
286 R2_IN_R3_SELECT_PAGE_HUNDREDS |
289 R2_HAMMING_CHECK_OFF,
291 HUNDREDS_OF_PAGE(req->page) |
293 (req->pagemask & PG_HUND ?
294 R3_PAGE_HUNDREDS_DO_CARE :
295 R3_PAGE_HUNDREDS_DO_NOT_CARE),
297 TENS_OF_PAGE(req->page) |
298 (req->pagemask & PG_TEN ?
299 R3_PAGE_TENS_DO_CARE :
300 R3_PAGE_TENS_DO_NOT_CARE),
302 UNITS_OF_PAGE(req->page) |
303 (req->pagemask & PG_UNIT ?
304 R3_PAGE_UNITS_DO_CARE :
305 R3_PAGE_UNITS_DO_NOT_CARE),
307 TENS_OF_HOUR(req->hour) |
308 (req->pagemask & HR_TEN ?
309 R3_HOURS_TENS_DO_CARE :
310 R3_HOURS_TENS_DO_NOT_CARE),
312 UNITS_OF_HOUR(req->hour) |
313 (req->pagemask & HR_UNIT ?
314 R3_HOURS_UNITS_DO_CARE :
315 R3_HOURS_UNITS_DO_NOT_CARE),
317 TENS_OF_MINUTE(req->minute) |
318 (req->pagemask & MIN_TEN ?
319 R3_MINUTES_TENS_DO_CARE :
320 R3_MINUTES_TENS_DO_NOT_CARE),
322 UNITS_OF_MINUTE(req->minute) |
323 (req->pagemask & MIN_UNIT ?
324 R3_MINUTES_UNITS_DO_CARE :
325 R3_MINUTES_UNITS_DO_NOT_CARE),
327 COMMAND_END) || i2c_senddata(t, SAA5246A_REGISTER_R2,
329 R2_IN_R3_SELECT_PAGE_HUNDREDS |
332 R2_HAMMING_CHECK_OFF,
334 HUNDREDS_OF_PAGE(req->page) |
336 (req->pagemask & PG_HUND ?
337 R3_PAGE_HUNDREDS_DO_CARE :
338 R3_PAGE_HUNDREDS_DO_NOT_CARE),
345 t->is_searching[req->pgbuf] = true;
349 /* This routine decodes the page number from the infobits contained in line 25.
352 * infobits: must be bits 0 to 9 of column 25
354 * Return value: page number coded in hexadecimal, i. e. page 123 is coded 0x123
356 static inline int saa5246a_extract_pagenum_from_infobits(
357 unsigned char infobits[10])
359 int page_hundreds, page_tens, page_units;
361 page_units = infobits[0] & ROW25_COLUMN0_PAGE_UNITS;
362 page_tens = infobits[1] & ROW25_COLUMN1_PAGE_TENS;
363 page_hundreds = infobits[8] & ROW25_COLUMN8_PAGE_HUNDREDS;
365 /* page 0x.. means page 8.. */
366 if (page_hundreds == 0)
369 return((page_hundreds << 8) | (page_tens << 4) | page_units);
372 /* Decodes the hour from the infobits contained in line 25.
375 * infobits: must be bits 0 to 9 of column 25
377 * Return: hour coded in hexadecimal, i. e. 12h is coded 0x12
379 static inline int saa5246a_extract_hour_from_infobits(
380 unsigned char infobits[10])
382 int hour_tens, hour_units;
384 hour_units = infobits[4] & ROW25_COLUMN4_HOUR_UNITS;
385 hour_tens = infobits[5] & ROW25_COLUMN5_HOUR_TENS;
387 return((hour_tens << 4) | hour_units);
390 /* Decodes the minutes from the infobits contained in line 25.
393 * infobits: must be bits 0 to 9 of column 25
395 * Return: minutes coded in hexadecimal, i. e. 10min is coded 0x10
397 static inline int saa5246a_extract_minutes_from_infobits(
398 unsigned char infobits[10])
400 int minutes_tens, minutes_units;
402 minutes_units = infobits[2] & ROW25_COLUMN2_MINUTES_UNITS;
403 minutes_tens = infobits[3] & ROW25_COLUMN3_MINUTES_TENS;
405 return((minutes_tens << 4) | minutes_units);
408 /* Reads the status bits contained in the first 10 columns of the first line
409 * and extracts the information into info.
411 * Return value: 0 if successful
413 static inline int saa5246a_get_status(struct saa5246a_device *t,
414 vtx_pageinfo_t *info, unsigned char dau_no)
416 unsigned char infobits[10];
419 if (dau_no >= NUM_DAUS)
422 if (i2c_senddata(t, SAA5246A_REGISTER_R8,
425 R8_DO_NOT_CLEAR_MEMORY,
432 i2c_getdata(t, 10, infobits))
437 info->pagenum = saa5246a_extract_pagenum_from_infobits(infobits);
438 info->hour = saa5246a_extract_hour_from_infobits(infobits);
439 info->minute = saa5246a_extract_minutes_from_infobits(infobits);
440 info->charset = ((infobits[7] & ROW25_COLUMN7_CHARACTER_SET) >> 1);
441 info->delete = !!(infobits[3] & ROW25_COLUMN3_DELETE_PAGE);
442 info->headline = !!(infobits[5] & ROW25_COLUMN5_INSERT_HEADLINE);
443 info->subtitle = !!(infobits[5] & ROW25_COLUMN5_INSERT_SUBTITLE);
444 info->supp_header = !!(infobits[6] & ROW25_COLUMN6_SUPPRESS_HEADER);
445 info->update = !!(infobits[6] & ROW25_COLUMN6_UPDATE_PAGE);
446 info->inter_seq = !!(infobits[6] & ROW25_COLUMN6_INTERRUPTED_SEQUENCE);
447 info->dis_disp = !!(infobits[6] & ROW25_COLUMN6_SUPPRESS_DISPLAY);
448 info->serial = !!(infobits[7] & ROW25_COLUMN7_SERIAL_MODE);
449 info->notfound = !!(infobits[8] & ROW25_COLUMN8_PAGE_NOT_FOUND);
450 info->pblf = !!(infobits[9] & ROW25_COLUMN9_PAGE_BEING_LOOKED_FOR);
452 for (column = 0; column <= 7; column++) {
453 if (infobits[column] & ROW25_COLUMN0_TO_7_HAMMING_ERROR) {
458 if (!info->hamming && !info->notfound)
459 t->is_searching[dau_no] = false;
463 /* Reads 1 videotext page buffer of the SAA5246A.
465 * req is used both as input and as output. It contains information which part
466 * must be read. The videotext page is copied into req->buffer.
468 * Return value: 0 if successful
470 static inline int saa5246a_get_page(struct saa5246a_device *t,
473 int start, end, size;
477 if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS ||
478 req->start < 0 || req->start > req->end || req->end >= VTX_PAGESIZE)
481 buf = kmalloc(VTX_PAGESIZE, GFP_KERNEL);
485 /* Read "normal" part of page */
488 end = min(req->end, VTX_PAGESIZE - 1);
489 if (i2c_senddata(t, SAA5246A_REGISTER_R8,
490 req->pgbuf | R8_DO_NOT_CLEAR_MEMORY,
491 ROW(req->start), COLUMN(req->start), COMMAND_END))
493 if (i2c_getdata(t, end - req->start + 1, buf))
496 if (copy_to_user(req->buffer, buf, end - req->start + 1))
499 /* Always get the time from buffer 4, since this stupid SAA5246A only
500 * updates the currently displayed buffer...
502 if (REQ_CONTAINS_TIME(req)) {
503 start = max(req->start, POS_TIME_START);
504 end = min(req->end, POS_TIME_END);
505 size = end - start + 1;
510 if (i2c_senddata(t, SAA5246A_REGISTER_R8,
511 R8_ACTIVE_CHAPTER_4 | R8_DO_NOT_CLEAR_MEMORY,
512 R9_CURSER_ROW_0, start, COMMAND_END))
514 if (i2c_getdata(t, size, buf))
517 if (copy_to_user(req->buffer + start - req->start, buf, size))
520 /* Insert the header from buffer 4 only, if acquisition circuit is still searching for a page */
521 if (REQ_CONTAINS_HEADER(req) && t->is_searching[req->pgbuf]) {
522 start = max(req->start, POS_HEADER_START);
523 end = min(req->end, POS_HEADER_END);
524 size = end - start + 1;
529 if (i2c_senddata(t, SAA5246A_REGISTER_R8,
530 R8_ACTIVE_CHAPTER_4 | R8_DO_NOT_CLEAR_MEMORY,
531 R9_CURSER_ROW_0, start, COMMAND_END))
533 if (i2c_getdata(t, end - start + 1, buf))
536 if (copy_to_user(req->buffer + start - req->start, buf, size))
545 /* Stops the acquisition circuit given in dau_no. The page buffer associated
546 * with this acquisition circuit will no more be updated. The other daus are
549 * Return value: 0 if successful
551 static inline int saa5246a_stop_dau(struct saa5246a_device *t,
552 unsigned char dau_no)
554 if (dau_no >= NUM_DAUS)
556 if (i2c_senddata(t, SAA5246A_REGISTER_R2,
558 R2_IN_R3_SELECT_PAGE_HUNDREDS |
561 R2_HAMMING_CHECK_OFF,
565 R3_PAGE_HUNDREDS_DO_NOT_CARE,
571 t->is_searching[dau_no] = false;
575 /* Handles ioctls defined in videotext.h
577 * Returns 0 if successful
579 static int do_saa5246a_ioctl(struct inode *inode, struct file *file,
580 unsigned int cmd, void *arg)
582 struct video_device *vd = video_devdata(file);
583 struct saa5246a_device *t=vd->priv;
588 vtx_info_t *info = arg;
590 info->version_major = MAJOR_VERSION;
591 info->version_minor = MINOR_VERSION;
592 info->numpages = NUM_DAUS;
598 vtx_pagereq_t *req = arg;
600 if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
602 memset(t->pgbuf[req->pgbuf], ' ', sizeof(t->pgbuf[0]));
608 vtx_pagereq_t *req = arg;
610 if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
612 return(saa5246a_clear_found_bit(t, req->pgbuf));
617 vtx_pagereq_t *req = arg;
619 return(saa5246a_request_page(t, req));
624 vtx_pagereq_t *req = arg;
628 if ((rval = saa5246a_get_status(t, &info, req->pgbuf)))
630 if(copy_to_user(req->buffer, &info,
631 sizeof(vtx_pageinfo_t)))
638 vtx_pagereq_t *req = arg;
640 return(saa5246a_get_page(t, req));
645 vtx_pagereq_t *req = arg;
647 return(saa5246a_stop_dau(t, req->pgbuf));
662 /* I do not know what "virtual mode" means */
670 * Translates old vtx IOCTLs to new ones
672 * This keeps new kernel versions compatible with old userspace programs.
674 static inline unsigned int vtx_fix_command(unsigned int cmd)
677 case VTXIOCGETINFO_OLD:
680 case VTXIOCCLRPAGE_OLD:
683 case VTXIOCCLRFOUND_OLD:
684 cmd = VTXIOCCLRFOUND;
686 case VTXIOCPAGEREQ_OLD:
689 case VTXIOCGETSTAT_OLD:
692 case VTXIOCGETPAGE_OLD:
695 case VTXIOCSTOPDAU_OLD:
698 case VTXIOCPUTPAGE_OLD:
701 case VTXIOCSETDISP_OLD:
704 case VTXIOCPUTSTAT_OLD:
707 case VTXIOCCLRCACHE_OLD:
708 cmd = VTXIOCCLRCACHE;
710 case VTXIOCSETVIRT_OLD:
720 static int saa5246a_ioctl(struct inode *inode, struct file *file,
721 unsigned int cmd, unsigned long arg)
723 struct video_device *vd = video_devdata(file);
724 struct saa5246a_device *t = vd->priv;
727 cmd = vtx_fix_command(cmd);
728 mutex_lock(&t->lock);
729 err = video_usercopy(inode, file, cmd, arg, do_saa5246a_ioctl);
730 mutex_unlock(&t->lock);
734 static int saa5246a_open(struct inode *inode, struct file *file)
736 struct video_device *vd = video_devdata(file);
737 struct saa5246a_device *t = vd->priv;
740 err = video_exclusive_open(inode,file);
744 if (t->client==NULL) {
749 if (i2c_senddata(t, SAA5246A_REGISTER_R0,
752 R0_PLL_TIME_CONSTANT_LONG |
753 R0_ENABLE_nODD_EVEN_OUTPUT |
755 R0_DO_NOT_FORCE_nODD_EVEN_LOW_IF_PICTURE_DISPLAYED |
757 R0_NO_AUTOMATIC_FASTEXT_PROMPT,
759 R1_NON_INTERLACED_312_312_LINES |
761 R1_EXTENDED_PACKET_DISABLE |
763 R1_8_BITS_NO_PARITY |
767 i2c_senddata(t, SAA5246A_REGISTER_R4,
769 /* We do not care much for the TV display but nevertheless we
770 * need the currently displayed page later because only on that
771 * page the time is updated. */
783 video_exclusive_release(inode,file);
787 static int saa5246a_release(struct inode *inode, struct file *file)
789 struct video_device *vd = video_devdata(file);
790 struct saa5246a_device *t = vd->priv;
792 /* Stop all acquisition circuits. */
793 i2c_senddata(t, SAA5246A_REGISTER_R1,
795 R1_INTERLACED_312_AND_HALF_312_AND_HALF_LINES |
797 R1_EXTENDED_PACKET_DISABLE |
799 R1_8_BITS_NO_PARITY |
803 video_exclusive_release(inode,file);
807 static int __init init_saa_5246a (void)
810 "SAA5246A (or compatible) Teletext decoder driver version %d.%d\n",
811 MAJOR_VERSION, MINOR_VERSION);
812 return i2c_add_driver(&i2c_driver_videotext);
815 static void __exit cleanup_saa_5246a (void)
817 i2c_del_driver(&i2c_driver_videotext);
820 module_init(init_saa_5246a);
821 module_exit(cleanup_saa_5246a);
823 static const struct file_operations saa_fops = {
824 .owner = THIS_MODULE,
825 .open = saa5246a_open,
826 .release = saa5246a_release,
827 .ioctl = saa5246a_ioctl,
831 static struct video_device saa_template =
835 .release = video_device_release,