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