Linux-2.6.12-rc2
[linux-flexiantxendom0-natty.git] / drivers / media / video / saa5246a.c
1 /*
2  * Driver for the SAA5246A or SAA5281 Teletext (=Videotext) decoder chips from
3  * Philips.
4  *
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.
8  *
9  * Copyright (C) 2004 Michael Geng <linux@MichaelGeng.de>
10  *
11  * Derived from
12  *
13  * saa5249 driver
14  * Copyright (C) 1998 Richard Guenther
15  * <richard.guenther@student.uni-tuebingen.de>
16  *
17  * with changes by
18  * Alan Cox <Alan.Cox@linux.org>
19  *
20  * and
21  *
22  * vtx.c
23  * Copyright (C) 1994-97 Martin Buck  <martin-2.buck@student.uni-ulm.de>
24  *
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.
29  *
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.
34  *
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,
38  * USA.
39  */
40
41 #include <linux/module.h>
42 #include <linux/kernel.h>
43 #include <linux/sched.h>
44 #include <linux/mm.h>
45 #include <linux/init.h>
46 #include <linux/i2c.h>
47 #include <linux/videotext.h>
48 #include <linux/videodev.h>
49 #include "saa5246a.h"
50
51 MODULE_AUTHOR("Michael Geng <linux@MichaelGeng.de>");
52 MODULE_DESCRIPTION("Philips SAA5246A, SAA5281 Teletext decoder driver");
53 MODULE_LICENSE("GPL");
54
55 struct saa5246a_device
56 {
57         u8     pgbuf[NUM_DAUS][VTX_VIRTUALSIZE];
58         int    is_searching[NUM_DAUS];
59         struct i2c_client *client;
60         struct semaphore lock;
61 };
62
63 static struct video_device saa_template;        /* Declared near bottom */
64
65 /* Addresses to scan */
66 static unsigned short normal_i2c[]       = { I2C_ADDRESS, I2C_CLIENT_END };
67 static unsigned short normal_i2c_range[] = { I2C_CLIENT_END };
68 I2C_CLIENT_INSMOD;
69
70 static struct i2c_client client_template;
71
72 static int saa5246a_attach(struct i2c_adapter *adap, int addr, int kind)
73 {
74         int pgbuf;
75         int err;
76         struct i2c_client *client;
77         struct video_device *vd;
78         struct saa5246a_device *t;
79
80         printk(KERN_INFO "saa5246a: teletext chip found.\n");
81         client=kmalloc(sizeof(*client), GFP_KERNEL);
82         if(client==NULL)
83                 return -ENOMEM;
84         client_template.adapter = adap;
85         client_template.addr = addr;
86         memcpy(client, &client_template, sizeof(*client));
87         t = kmalloc(sizeof(*t), GFP_KERNEL);
88         if(t==NULL)
89         {
90                 kfree(client);
91                 return -ENOMEM;
92         }
93         memset(t, 0, sizeof(*t));
94         strlcpy(client->name, IF_NAME, I2C_NAME_SIZE);
95         init_MUTEX(&t->lock);
96
97         /*
98          *      Now create a video4linux device
99          */
100
101         vd = video_device_alloc();
102         if(vd==NULL)
103         {
104                 kfree(t);
105                 kfree(client);
106                 return -ENOMEM;
107         }
108         i2c_set_clientdata(client, vd);
109         memcpy(vd, &saa_template, sizeof(*vd));
110
111         for (pgbuf = 0; pgbuf < NUM_DAUS; pgbuf++)
112         {
113                 memset(t->pgbuf[pgbuf], ' ', sizeof(t->pgbuf[0]));
114                 t->is_searching[pgbuf] = FALSE;
115         }
116         vd->priv=t;
117
118
119         /*
120          *      Register it
121          */
122
123         if((err=video_register_device(vd, VFL_TYPE_VTX,-1))<0)
124         {
125                 kfree(t);
126                 kfree(client);
127                 video_device_release(vd);
128                 return err;
129         }
130         t->client = client;
131         i2c_attach_client(client);
132         return 0;
133 }
134
135 /*
136  *      We do most of the hard work when we become a device on the i2c.
137  */
138 static int saa5246a_probe(struct i2c_adapter *adap)
139 {
140         if (adap->class & I2C_CLASS_TV_ANALOG)
141                 return i2c_probe(adap, &addr_data, saa5246a_attach);
142         return 0;
143 }
144
145 static int saa5246a_detach(struct i2c_client *client)
146 {
147         struct video_device *vd = i2c_get_clientdata(client);
148         i2c_detach_client(client);
149         video_unregister_device(vd);
150         kfree(vd->priv);
151         kfree(client);
152         return 0;
153 }
154
155 static int saa5246a_command(struct i2c_client *device, unsigned int cmd,
156         void *arg)
157 {
158         return -EINVAL;
159 }
160
161 /*
162  *      I2C interfaces
163  */
164
165 static struct i2c_driver i2c_driver_videotext =
166 {
167         .owner          = THIS_MODULE,
168         .name           = IF_NAME,              /* name */
169         .id             = I2C_DRIVERID_SAA5249, /* in i2c.h */
170         .flags          = I2C_DF_NOTIFY,
171         .attach_adapter = saa5246a_probe,
172         .detach_client  = saa5246a_detach,
173         .command        = saa5246a_command
174 };
175
176 static struct i2c_client client_template = {
177         .driver         = &i2c_driver_videotext,
178         .name           = "(unset)",
179 };
180
181 static int i2c_sendbuf(struct saa5246a_device *t, int reg, int count, u8 *data)
182 {
183         char buf[64];
184
185         buf[0] = reg;
186         memcpy(buf+1, data, count);
187
188         if(i2c_master_send(t->client, buf, count+1)==count+1)
189                 return 0;
190         return -1;
191 }
192
193 static int i2c_senddata(struct saa5246a_device *t, ...)
194 {
195         unsigned char buf[64];
196         int v;
197         int ct=0;
198         va_list argp;
199         va_start(argp,t);
200
201         while((v=va_arg(argp,int))!=-1)
202                 buf[ct++]=v;
203         return i2c_sendbuf(t, buf[0], ct-1, buf+1);
204 }
205
206 /* Get count number of bytes from I²C-device at address adr, store them in buf.
207  * Start & stop handshaking is done by this routine, ack will be sent after the
208  * last byte to inhibit further sending of data. If uaccess is TRUE, data is
209  * written to user-space with put_user. Returns -1 if I²C-device didn't send
210  * acknowledge, 0 otherwise
211  */
212 static int i2c_getdata(struct saa5246a_device *t, int count, u8 *buf)
213 {
214         if(i2c_master_recv(t->client, buf, count)!=count)
215                 return -1;
216         return 0;
217 }
218
219 /* When a page is found then the not FOUND bit in one of the status registers
220  * of the SAA5264A chip is cleared. Unfortunately this bit is not set
221  * automatically when a new page is requested. Instead this function must be
222  * called after a page has been requested.
223  *
224  * Return value: 0 if successful
225  */
226 static int saa5246a_clear_found_bit(struct saa5246a_device *t,
227         unsigned char dau_no)
228 {
229         unsigned char row_25_column_8;
230
231         if (i2c_senddata(t, SAA5246A_REGISTER_R8,
232
233                 dau_no |
234                 R8_DO_NOT_CLEAR_MEMORY,
235
236                 R9_CURSER_ROW_25,
237
238                 R10_CURSER_COLUMN_8,
239
240                 COMMAND_END) ||
241                 i2c_getdata(t, 1, &row_25_column_8))
242         {
243                 return -EIO;
244         }
245         row_25_column_8 |= ROW25_COLUMN8_PAGE_NOT_FOUND;
246         if (i2c_senddata(t, SAA5246A_REGISTER_R8,
247
248                 dau_no |
249                 R8_DO_NOT_CLEAR_MEMORY,
250
251                 R9_CURSER_ROW_25,
252
253                 R10_CURSER_COLUMN_8,
254
255                 row_25_column_8,
256
257                 COMMAND_END))
258         {
259                 return -EIO;
260         }
261
262         return 0;
263 }
264
265 /* Requests one videotext page as described in req. The fields of req are
266  * checked and an error is returned if something is invalid.
267  *
268  * Return value: 0 if successful
269  */
270 static int saa5246a_request_page(struct saa5246a_device *t,
271     vtx_pagereq_t *req)
272 {
273         if (req->pagemask < 0 || req->pagemask >= PGMASK_MAX)
274                 return -EINVAL;
275         if (req->pagemask & PGMASK_PAGE)
276                 if (req->page < 0 || req->page > PAGE_MAX)
277                         return -EINVAL;
278         if (req->pagemask & PGMASK_HOUR)
279                 if (req->hour < 0 || req->hour > HOUR_MAX)
280                         return -EINVAL;
281         if (req->pagemask & PGMASK_MINUTE)
282                 if (req->minute < 0 || req->minute > MINUTE_MAX)
283                         return -EINVAL;
284         if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
285                 return -EINVAL;
286
287         if (i2c_senddata(t, SAA5246A_REGISTER_R2,
288
289                 R2_IN_R3_SELECT_PAGE_HUNDREDS |
290                 req->pgbuf << 4 |
291                 R2_BANK_0 |
292                 R2_HAMMING_CHECK_OFF,
293
294                 HUNDREDS_OF_PAGE(req->page) |
295                 R3_HOLD_PAGE |
296                 (req->pagemask & PG_HUND ?
297                         R3_PAGE_HUNDREDS_DO_CARE :
298                         R3_PAGE_HUNDREDS_DO_NOT_CARE),
299
300                 TENS_OF_PAGE(req->page) |
301                 (req->pagemask & PG_TEN ?
302                         R3_PAGE_TENS_DO_CARE :
303                         R3_PAGE_TENS_DO_NOT_CARE),
304
305                 UNITS_OF_PAGE(req->page) |
306                 (req->pagemask & PG_UNIT ?
307                         R3_PAGE_UNITS_DO_CARE :
308                         R3_PAGE_UNITS_DO_NOT_CARE),
309
310                 TENS_OF_HOUR(req->hour) |
311                 (req->pagemask & HR_TEN ?
312                         R3_HOURS_TENS_DO_CARE :
313                         R3_HOURS_TENS_DO_NOT_CARE),
314
315                 UNITS_OF_HOUR(req->hour) |
316                 (req->pagemask & HR_UNIT ?
317                         R3_HOURS_UNITS_DO_CARE :
318                         R3_HOURS_UNITS_DO_NOT_CARE),
319
320                 TENS_OF_MINUTE(req->minute) |
321                 (req->pagemask & MIN_TEN ?
322                         R3_MINUTES_TENS_DO_CARE :
323                         R3_MINUTES_TENS_DO_NOT_CARE),
324
325                 UNITS_OF_MINUTE(req->minute) |
326                 (req->pagemask & MIN_UNIT ?
327                         R3_MINUTES_UNITS_DO_CARE :
328                         R3_MINUTES_UNITS_DO_NOT_CARE),
329
330                 COMMAND_END) || i2c_senddata(t, SAA5246A_REGISTER_R2,
331
332                 R2_IN_R3_SELECT_PAGE_HUNDREDS |
333                 req->pgbuf << 4 |
334                 R2_BANK_0 |
335                 R2_HAMMING_CHECK_OFF,
336
337                 HUNDREDS_OF_PAGE(req->page) |
338                 R3_UPDATE_PAGE |
339                 (req->pagemask & PG_HUND ?
340                         R3_PAGE_HUNDREDS_DO_CARE :
341                         R3_PAGE_HUNDREDS_DO_NOT_CARE),
342
343                 COMMAND_END))
344         {
345                 return -EIO;
346         }
347
348         t->is_searching[req->pgbuf] = TRUE;
349         return 0;
350 }
351
352 /* This routine decodes the page number from the infobits contained in line 25.
353  *
354  * Parameters:
355  * infobits: must be bits 0 to 9 of column 25
356  *
357  * Return value: page number coded in hexadecimal, i. e. page 123 is coded 0x123
358  */
359 static inline int saa5246a_extract_pagenum_from_infobits(
360     unsigned char infobits[10])
361 {
362         int page_hundreds, page_tens, page_units;
363
364         page_units    = infobits[0] & ROW25_COLUMN0_PAGE_UNITS;
365         page_tens     = infobits[1] & ROW25_COLUMN1_PAGE_TENS;
366         page_hundreds = infobits[8] & ROW25_COLUMN8_PAGE_HUNDREDS;
367
368         /* page 0x.. means page 8.. */
369         if (page_hundreds == 0)
370                 page_hundreds = 8;
371
372         return((page_hundreds << 8) | (page_tens << 4) | page_units);
373 }
374
375 /* Decodes the hour from the infobits contained in line 25.
376  *
377  * Parameters:
378  * infobits: must be bits 0 to 9 of column 25
379  *
380  * Return: hour coded in hexadecimal, i. e. 12h is coded 0x12
381  */
382 static inline int saa5246a_extract_hour_from_infobits(
383     unsigned char infobits[10])
384 {
385         int hour_tens, hour_units;
386
387         hour_units = infobits[4] & ROW25_COLUMN4_HOUR_UNITS;
388         hour_tens  = infobits[5] & ROW25_COLUMN5_HOUR_TENS;
389
390         return((hour_tens << 4) | hour_units);
391 }
392
393 /* Decodes the minutes from the infobits contained in line 25.
394  *
395  * Parameters:
396  * infobits: must be bits 0 to 9 of column 25
397  *
398  * Return: minutes coded in hexadecimal, i. e. 10min is coded 0x10
399  */
400 static inline int saa5246a_extract_minutes_from_infobits(
401     unsigned char infobits[10])
402 {
403         int minutes_tens, minutes_units;
404
405         minutes_units = infobits[2] & ROW25_COLUMN2_MINUTES_UNITS;
406         minutes_tens  = infobits[3] & ROW25_COLUMN3_MINUTES_TENS;
407
408         return((minutes_tens << 4) | minutes_units);
409 }
410
411 /* Reads the status bits contained in the first 10 columns of the first line
412  * and extracts the information into info.
413  *
414  * Return value: 0 if successful
415  */
416 static inline int saa5246a_get_status(struct saa5246a_device *t,
417     vtx_pageinfo_t *info, unsigned char dau_no)
418 {
419         unsigned char infobits[10];
420         int column;
421
422         if (dau_no >= NUM_DAUS)
423                 return -EINVAL;
424
425         if (i2c_senddata(t, SAA5246A_REGISTER_R8,
426
427                 dau_no |
428                 R8_DO_NOT_CLEAR_MEMORY,
429
430                 R9_CURSER_ROW_25,
431
432                 R10_CURSER_COLUMN_0,
433
434                 COMMAND_END) ||
435                 i2c_getdata(t, 10, infobits))
436         {
437                 return -EIO;
438         }
439
440         info->pagenum = saa5246a_extract_pagenum_from_infobits(infobits);
441         info->hour    = saa5246a_extract_hour_from_infobits(infobits);
442         info->minute  = saa5246a_extract_minutes_from_infobits(infobits);
443         info->charset = ((infobits[7] & ROW25_COLUMN7_CHARACTER_SET) >> 1);
444         info->delete = !!(infobits[3] & ROW25_COLUMN3_DELETE_PAGE);
445         info->headline = !!(infobits[5] & ROW25_COLUMN5_INSERT_HEADLINE);
446         info->subtitle = !!(infobits[5] & ROW25_COLUMN5_INSERT_SUBTITLE);
447         info->supp_header = !!(infobits[6] & ROW25_COLUMN6_SUPPRESS_HEADER);
448         info->update = !!(infobits[6] & ROW25_COLUMN6_UPDATE_PAGE);
449         info->inter_seq = !!(infobits[6] & ROW25_COLUMN6_INTERRUPTED_SEQUENCE);
450         info->dis_disp = !!(infobits[6] & ROW25_COLUMN6_SUPPRESS_DISPLAY);
451         info->serial = !!(infobits[7] & ROW25_COLUMN7_SERIAL_MODE);
452         info->notfound = !!(infobits[8] & ROW25_COLUMN8_PAGE_NOT_FOUND);
453         info->pblf = !!(infobits[9] & ROW25_COLUMN9_PAGE_BEING_LOOKED_FOR);
454         info->hamming = 0;
455         for (column = 0; column <= 7; column++) {
456                 if (infobits[column] & ROW25_COLUMN0_TO_7_HAMMING_ERROR) {
457                         info->hamming = 1;
458                         break;
459                 }
460         }
461         if (!info->hamming && !info->notfound)
462                 t->is_searching[dau_no] = FALSE;
463         return 0;
464 }
465
466 /* Reads 1 videotext page buffer of the SAA5246A.
467  *
468  * req is used both as input and as output. It contains information which part
469  * must be read. The videotext page is copied into req->buffer.
470  *
471  * Return value: 0 if successful
472  */
473 static inline int saa5246a_get_page(struct saa5246a_device *t,
474         vtx_pagereq_t *req)
475 {
476         int start, end, size;
477         char *buf;
478         int err;
479
480         if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS ||
481             req->start < 0 || req->start > req->end || req->end >= VTX_PAGESIZE)
482                 return -EINVAL;
483
484         buf = kmalloc(VTX_PAGESIZE, GFP_KERNEL);
485         if (!buf)
486                 return -ENOMEM;
487
488         /* Read "normal" part of page */
489         err = -EIO;
490
491         end = min(req->end, VTX_PAGESIZE - 1);
492         if (i2c_senddata(t, SAA5246A_REGISTER_R8,
493                         req->pgbuf | R8_DO_NOT_CLEAR_MEMORY,
494                         ROW(req->start), COLUMN(req->start), COMMAND_END))
495                 goto out;
496         if (i2c_getdata(t, end - req->start + 1, buf))
497                 goto out;
498         err = -EFAULT;
499         if (copy_to_user(req->buffer, buf, end - req->start + 1))
500                 goto out;
501
502         /* Always get the time from buffer 4, since this stupid SAA5246A only
503          * updates the currently displayed buffer...
504          */
505         if (REQ_CONTAINS_TIME(req)) {
506                 start = max(req->start, POS_TIME_START);
507                 end   = min(req->end,   POS_TIME_END);
508                 size = end - start + 1;
509                 err = -EINVAL;
510                 if (size < 0)
511                         goto out;
512                 err = -EIO;
513                 if (i2c_senddata(t, SAA5246A_REGISTER_R8,
514                                 R8_ACTIVE_CHAPTER_4 | R8_DO_NOT_CLEAR_MEMORY,
515                                 R9_CURSER_ROW_0, start, COMMAND_END))
516                         goto out;
517                 if (i2c_getdata(t, size, buf))
518                         goto out;
519                 err = -EFAULT;
520                 if (copy_to_user(req->buffer + start - req->start, buf, size))
521                         goto out;
522         }
523         /* Insert the header from buffer 4 only, if acquisition circuit is still searching for a page */
524         if (REQ_CONTAINS_HEADER(req) && t->is_searching[req->pgbuf]) {
525                 start = max(req->start, POS_HEADER_START);
526                 end   = min(req->end,   POS_HEADER_END);
527                 size = end - start + 1;
528                 err = -EINVAL;
529                 if (size < 0)
530                         goto out;
531                 err = -EIO;
532                 if (i2c_senddata(t, SAA5246A_REGISTER_R8,
533                                 R8_ACTIVE_CHAPTER_4 | R8_DO_NOT_CLEAR_MEMORY,
534                                 R9_CURSER_ROW_0, start, COMMAND_END))
535                         goto out;
536                 if (i2c_getdata(t, end - start + 1, buf))
537                         goto out;
538                 err = -EFAULT;
539                 if (copy_to_user(req->buffer + start - req->start, buf, size))
540                         goto out;
541         }
542         err = 0;
543 out:
544         kfree(buf);
545         return err;
546 }
547
548 /* Stops the acquisition circuit given in dau_no. The page buffer associated
549  * with this acquisition circuit will no more be updated. The other daus are
550  * not affected.
551  *
552  * Return value: 0 if successful
553  */
554 static inline int saa5246a_stop_dau(struct saa5246a_device *t,
555     unsigned char dau_no)
556 {
557         if (dau_no >= NUM_DAUS)
558                 return -EINVAL;
559         if (i2c_senddata(t, SAA5246A_REGISTER_R2,
560
561                 R2_IN_R3_SELECT_PAGE_HUNDREDS |
562                 dau_no << 4 |
563                 R2_BANK_0 |
564                 R2_HAMMING_CHECK_OFF,
565
566                 R3_PAGE_HUNDREDS_0 |
567                 R3_HOLD_PAGE |
568                 R3_PAGE_HUNDREDS_DO_NOT_CARE,
569
570                 COMMAND_END))
571         {
572                 return -EIO;
573         }
574         t->is_searching[dau_no] = FALSE;
575         return 0;
576 }
577
578 /*  Handles ioctls defined in videotext.h
579  *
580  *  Returns 0 if successful
581  */
582 static int do_saa5246a_ioctl(struct inode *inode, struct file *file,
583                             unsigned int cmd, void *arg)
584 {
585         struct video_device *vd = video_devdata(file);
586         struct saa5246a_device *t=vd->priv;
587         switch(cmd)
588         {
589                 case VTXIOCGETINFO:
590                 {
591                         vtx_info_t *info = arg;
592
593                         info->version_major = MAJOR_VERSION;
594                         info->version_minor = MINOR_VERSION;
595                         info->numpages = NUM_DAUS;
596                         return 0;
597                 }
598
599                 case VTXIOCCLRPAGE:
600                 {
601                         vtx_pagereq_t *req = arg;
602
603                         if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
604                                 return -EINVAL;
605                         memset(t->pgbuf[req->pgbuf], ' ', sizeof(t->pgbuf[0]));
606                         return 0;
607                 }
608
609                 case VTXIOCCLRFOUND:
610                 {
611                         vtx_pagereq_t *req = arg;
612
613                         if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
614                                 return -EINVAL;
615                         return(saa5246a_clear_found_bit(t, req->pgbuf));
616                 }
617
618                 case VTXIOCPAGEREQ:
619                 {
620                         vtx_pagereq_t *req = arg;
621
622                         return(saa5246a_request_page(t, req));
623                 }
624
625                 case VTXIOCGETSTAT:
626                 {
627                         vtx_pagereq_t *req = arg;
628                         vtx_pageinfo_t info;
629                         int rval;
630
631                         if ((rval = saa5246a_get_status(t, &info, req->pgbuf)))
632                                 return rval;
633                         if(copy_to_user(req->buffer, &info,
634                                 sizeof(vtx_pageinfo_t)))
635                                 return -EFAULT;
636                         return 0;
637                 }
638
639                 case VTXIOCGETPAGE:
640                 {
641                         vtx_pagereq_t *req = arg;
642
643                         return(saa5246a_get_page(t, req));
644                 }
645
646                 case VTXIOCSTOPDAU:
647                 {
648                         vtx_pagereq_t *req = arg;
649
650                         return(saa5246a_stop_dau(t, req->pgbuf));
651                 }
652
653                 case VTXIOCPUTPAGE:
654                 case VTXIOCSETDISP:
655                 case VTXIOCPUTSTAT:
656                         return 0;
657
658                 case VTXIOCCLRCACHE:
659                 {
660                         return 0;
661                 }
662
663                 case VTXIOCSETVIRT:
664                 {
665                         /* I do not know what "virtual mode" means */
666                         return 0;
667                 }
668         }
669         return -EINVAL;
670 }
671
672 /*
673  * Translates old vtx IOCTLs to new ones
674  *
675  * This keeps new kernel versions compatible with old userspace programs.
676  */
677 static inline unsigned int vtx_fix_command(unsigned int cmd)
678 {
679         switch (cmd) {
680         case VTXIOCGETINFO_OLD:
681                 cmd = VTXIOCGETINFO;
682                 break;
683         case VTXIOCCLRPAGE_OLD:
684                 cmd = VTXIOCCLRPAGE;
685                 break;
686         case VTXIOCCLRFOUND_OLD:
687                 cmd = VTXIOCCLRFOUND;
688                 break;
689         case VTXIOCPAGEREQ_OLD:
690                 cmd = VTXIOCPAGEREQ;
691                 break;
692         case VTXIOCGETSTAT_OLD:
693                 cmd = VTXIOCGETSTAT;
694                 break;
695         case VTXIOCGETPAGE_OLD:
696                 cmd = VTXIOCGETPAGE;
697                 break;
698         case VTXIOCSTOPDAU_OLD:
699                 cmd = VTXIOCSTOPDAU;
700                 break;
701         case VTXIOCPUTPAGE_OLD:
702                 cmd = VTXIOCPUTPAGE;
703                 break;
704         case VTXIOCSETDISP_OLD:
705                 cmd = VTXIOCSETDISP;
706                 break;
707         case VTXIOCPUTSTAT_OLD:
708                 cmd = VTXIOCPUTSTAT;
709                 break;
710         case VTXIOCCLRCACHE_OLD:
711                 cmd = VTXIOCCLRCACHE;
712                 break;
713         case VTXIOCSETVIRT_OLD:
714                 cmd = VTXIOCSETVIRT;
715                 break;
716         }
717         return cmd;
718 }
719
720 /*
721  *      Handle the locking
722  */
723 static int saa5246a_ioctl(struct inode *inode, struct file *file,
724                          unsigned int cmd, unsigned long arg)
725 {
726         struct video_device *vd = video_devdata(file);
727         struct saa5246a_device *t = vd->priv;
728         int err;
729
730         cmd = vtx_fix_command(cmd);
731         down(&t->lock);
732         err = video_usercopy(inode, file, cmd, arg, do_saa5246a_ioctl);
733         up(&t->lock);
734         return err;
735 }
736
737 static int saa5246a_open(struct inode *inode, struct file *file)
738 {
739         struct video_device *vd = video_devdata(file);
740         struct saa5246a_device *t = vd->priv;
741         int err;
742
743         err = video_exclusive_open(inode,file);
744         if (err < 0)
745                 return err;
746
747         if (t->client==NULL) {
748                 err = -ENODEV;
749                 goto fail;
750         }
751
752         if (i2c_senddata(t, SAA5246A_REGISTER_R0,
753
754                 R0_SELECT_R11 |
755                 R0_PLL_TIME_CONSTANT_LONG |
756                 R0_ENABLE_nODD_EVEN_OUTPUT |
757                 R0_ENABLE_HDR_POLL |
758                 R0_DO_NOT_FORCE_nODD_EVEN_LOW_IF_PICTURE_DISPLAYED |
759                 R0_NO_FREE_RUN_PLL |
760                 R0_NO_AUTOMATIC_FASTEXT_PROMPT,
761
762                 R1_NON_INTERLACED_312_312_LINES |
763                 R1_DEW |
764                 R1_EXTENDED_PACKET_DISABLE |
765                 R1_DAUS_ALL_ON |
766                 R1_8_BITS_NO_PARITY |
767                 R1_VCS_TO_SCS,
768
769                 COMMAND_END) ||
770                 i2c_senddata(t, SAA5246A_REGISTER_R4,
771
772                 /* We do not care much for the TV display but nevertheless we
773                  * need the currently displayed page later because only on that
774                  * page the time is updated. */
775                 R4_DISPLAY_PAGE_4,
776
777                 COMMAND_END))
778         {
779                 err = -EIO;
780                 goto fail;
781         }
782
783         return 0;
784
785 fail:
786         video_exclusive_release(inode,file);
787         return err;
788 }
789
790 static int saa5246a_release(struct inode *inode, struct file *file)
791 {
792         struct video_device *vd = video_devdata(file);
793         struct saa5246a_device *t = vd->priv;
794
795         /* Stop all acquisition circuits. */
796         i2c_senddata(t, SAA5246A_REGISTER_R1,
797
798                 R1_INTERLACED_312_AND_HALF_312_AND_HALF_LINES |
799                 R1_DEW |
800                 R1_EXTENDED_PACKET_DISABLE |
801                 R1_DAUS_ALL_OFF |
802                 R1_8_BITS_NO_PARITY |
803                 R1_VCS_TO_SCS,
804
805                 COMMAND_END);
806         video_exclusive_release(inode,file);
807         return 0;
808 }
809
810 static int __init init_saa_5246a (void)
811 {
812         printk(KERN_INFO
813                 "SAA5246A (or compatible) Teletext decoder driver version %d.%d\n",
814                 MAJOR_VERSION, MINOR_VERSION);
815         return i2c_add_driver(&i2c_driver_videotext);
816 }
817
818 static void __exit cleanup_saa_5246a (void)
819 {
820         i2c_del_driver(&i2c_driver_videotext);
821 }
822
823 module_init(init_saa_5246a);
824 module_exit(cleanup_saa_5246a);
825
826 static struct file_operations saa_fops = {
827         .owner   = THIS_MODULE,
828         .open    = saa5246a_open,
829         .release = saa5246a_release,
830         .ioctl   = saa5246a_ioctl,
831         .llseek  = no_llseek,
832 };
833
834 static struct video_device saa_template =
835 {
836         .owner    = THIS_MODULE,
837         .name     = IF_NAME,
838         .type     = VID_TYPE_TELETEXT,
839         .hardware = VID_HARDWARE_SAA5249,
840         .fops     = &saa_fops,
841         .release  = video_device_release,
842         .minor    = -1,
843 };