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