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