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