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