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