blob: 531e9461cb66d4fdbc7ab03f6941e4af2d8d43a9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Modified in order to keep it compatible both with new and old videotext IOCTLs by
3 * Michael Geng <linux@MichaelGeng.de>
4 *
5 * Cleaned up to use existing videodev interface and allow the idea
6 * of multiple teletext decoders on the video4linux iface. Changed i2c
7 * to cover addressing clashes on device busses. It's also rebuilt so
8 * you can add arbitary multiple teletext devices to Linux video4linux
9 * now (well 32 anyway).
10 *
11 * Alan Cox <Alan.Cox@linux.org>
12 *
13 * The original driver was heavily modified to match the i2c interface
14 * It was truncated to use the WinTV boards, too.
15 *
16 * Copyright (c) 1998 Richard Guenther <richard.guenther@student.uni-tuebingen.de>
17 *
18 * $Id: saa5249.c,v 1.1 1998/03/30 22:23:23 alan Exp $
19 *
20 * Derived From
21 *
22 * vtx.c:
23 * This is a loadable character-device-driver for videotext-interfaces
24 * (aka teletext). Please check the Makefile/README for a list of supported
25 * interfaces.
26 *
27 * Copyright (c) 1994-97 Martin Buck <martin-2.buck@student.uni-ulm.de>
28 *
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License
41 * along with this program; if not, write to the Free Software
42 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
43 * USA.
44 */
45
46#include <linux/module.h>
47#include <linux/kernel.h>
48#include <linux/sched.h>
49#include <linux/mm.h>
50#include <linux/errno.h>
51#include <linux/delay.h>
52#include <linux/ioport.h>
53#include <linux/slab.h>
54#include <linux/init.h>
55#include <stdarg.h>
56#include <linux/i2c.h>
57#include <linux/videotext.h>
58#include <linux/videodev.h>
Ingo Molnar3593cab2006-02-07 06:49:14 -020059#include <linux/mutex.h>
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62#include <asm/io.h>
63#include <asm/uaccess.h>
64
65#define VTX_VER_MAJ 1
66#define VTX_VER_MIN 8
67
68
69
70#define NUM_DAUS 4
71#define NUM_BUFS 8
72#define IF_NAME "SAA5249"
73
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030074static const int disp_modes[8][3] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
76 { 0x46, 0x03, 0x03 }, /* DISPOFF */
77 { 0x46, 0xcc, 0xcc }, /* DISPNORM */
78 { 0x44, 0x0f, 0x0f }, /* DISPTRANS */
79 { 0x46, 0xcc, 0x46 }, /* DISPINS */
80 { 0x44, 0x03, 0x03 }, /* DISPOFF, interlaced */
81 { 0x44, 0xcc, 0xcc }, /* DISPNORM, interlaced */
82 { 0x44, 0x0f, 0x0f }, /* DISPTRANS, interlaced */
83 { 0x44, 0xcc, 0x46 } /* DISPINS, interlaced */
84};
85
86
87
88#define PAGE_WAIT (300*HZ/1000) /* Time between requesting page and */
89 /* checking status bits */
90#define PGBUF_EXPIRE (15*HZ) /* Time to wait before retransmitting */
91 /* page regardless of infobits */
92typedef struct {
93 u8 pgbuf[VTX_VIRTUALSIZE]; /* Page-buffer */
94 u8 laststat[10]; /* Last value of infobits for DAU */
95 u8 sregs[7]; /* Page-request registers */
96 unsigned long expire; /* Time when page will be expired */
97 unsigned clrfound : 1; /* VTXIOCCLRFOUND has been called */
98 unsigned stopped : 1; /* VTXIOCSTOPDAU has been called */
99} vdau_t;
100
101struct saa5249_device
102{
103 vdau_t vdau[NUM_DAUS]; /* Data for virtual DAUs (the 5249 only has one */
104 /* real DAU, so we have to simulate some more) */
105 int vtx_use_count;
106 int is_searching[NUM_DAUS];
107 int disp_mode;
108 int virtual_mode;
109 struct i2c_client *client;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200110 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111};
112
113
114#define CCTWR 34 /* I²C write/read-address of vtx-chip */
115#define CCTRD 35
116#define NOACK_REPEAT 10 /* Retry access this many times on failure */
117#define CLEAR_DELAY (HZ/20) /* Time required to clear a page */
118#define READY_TIMEOUT (30*HZ/1000) /* Time to wait for ready signal of I²C-bus interface */
119#define INIT_DELAY 500 /* Time in usec to wait at initialization of CEA interface */
120#define START_DELAY 10 /* Time in usec to wait before starting write-cycle (CEA) */
121
122#define VTX_DEV_MINOR 0
123
124/* General defines and debugging support */
125
126#ifndef FALSE
127#define FALSE 0
128#define TRUE 1
129#endif
130
131#define RESCHED do { cond_resched(); } while(0)
132
133static struct video_device saa_template; /* Declared near bottom */
134
135/* Addresses to scan */
136static unsigned short normal_i2c[] = {34>>1,I2C_CLIENT_END};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137I2C_CLIENT_INSMOD;
138
139static struct i2c_client client_template;
140
141static int saa5249_attach(struct i2c_adapter *adap, int addr, int kind)
142{
143 int pgbuf;
144 int err;
145 struct i2c_client *client;
146 struct video_device *vd;
147 struct saa5249_device *t;
148
149 printk(KERN_INFO "saa5249: teletext chip found.\n");
150 client=kmalloc(sizeof(*client), GFP_KERNEL);
151 if(client==NULL)
152 return -ENOMEM;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300153 client_template.adapter = adap;
154 client_template.addr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 memcpy(client, &client_template, sizeof(*client));
Panagiotis Issaris74081872006-01-11 19:40:56 -0200156 t = kzalloc(sizeof(*t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if(t==NULL)
158 {
159 kfree(client);
160 return -ENOMEM;
161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 strlcpy(client->name, IF_NAME, I2C_NAME_SIZE);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200163 mutex_init(&t->lock);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 /*
166 * Now create a video4linux device
167 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300168
Panagiotis Issaris74081872006-01-11 19:40:56 -0200169 vd = kmalloc(sizeof(struct video_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if(vd==NULL)
171 {
172 kfree(t);
173 kfree(client);
174 return -ENOMEM;
175 }
176 i2c_set_clientdata(client, vd);
177 memcpy(vd, &saa_template, sizeof(*vd));
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300178
179 for (pgbuf = 0; pgbuf < NUM_DAUS; pgbuf++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 {
181 memset(t->vdau[pgbuf].pgbuf, ' ', sizeof(t->vdau[0].pgbuf));
182 memset(t->vdau[pgbuf].sregs, 0, sizeof(t->vdau[0].sregs));
183 memset(t->vdau[pgbuf].laststat, 0, sizeof(t->vdau[0].laststat));
184 t->vdau[pgbuf].expire = 0;
185 t->vdau[pgbuf].clrfound = TRUE;
186 t->vdau[pgbuf].stopped = TRUE;
187 t->is_searching[pgbuf] = FALSE;
188 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300189 vd->priv=t;
190
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 /*
193 * Register it
194 */
195
196 if((err=video_register_device(vd, VFL_TYPE_VTX,-1))<0)
197 {
198 kfree(t);
199 kfree(vd);
200 kfree(client);
201 return err;
202 }
203 t->client = client;
204 i2c_attach_client(client);
205 return 0;
206}
207
208/*
209 * We do most of the hard work when we become a device on the i2c.
210 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212static int saa5249_probe(struct i2c_adapter *adap)
213{
214 if (adap->class & I2C_CLASS_TV_ANALOG)
215 return i2c_probe(adap, &addr_data, saa5249_attach);
216 return 0;
217}
218
219static int saa5249_detach(struct i2c_client *client)
220{
221 struct video_device *vd = i2c_get_clientdata(client);
222 i2c_detach_client(client);
223 video_unregister_device(vd);
224 kfree(vd->priv);
225 kfree(vd);
226 kfree(client);
227 return 0;
228}
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230/* new I2C driver support */
231
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300232static struct i2c_driver i2c_driver_videotext =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Laurent Riffard604f28e2005-11-26 20:43:39 +0100234 .driver = {
Laurent Riffard604f28e2005-11-26 20:43:39 +0100235 .name = IF_NAME, /* name */
236 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 .id = I2C_DRIVERID_SAA5249, /* in i2c.h */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 .attach_adapter = saa5249_probe,
239 .detach_client = saa5249_detach,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240};
241
242static struct i2c_client client_template = {
243 .driver = &i2c_driver_videotext,
244 .name = "(unset)",
245};
246
247/*
248 * Wait the given number of jiffies (10ms). This calls the scheduler, so the actual
249 * delay may be longer.
250 */
251
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300252static void jdelay(unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 sigset_t oldblocked = current->blocked;
255
256 spin_lock_irq(&current->sighand->siglock);
257 sigfillset(&current->blocked);
258 recalc_sigpending();
259 spin_unlock_irq(&current->sighand->siglock);
260 msleep_interruptible(jiffies_to_msecs(delay));
261
262 spin_lock_irq(&current->sighand->siglock);
263 current->blocked = oldblocked;
264 recalc_sigpending();
265 spin_unlock_irq(&current->sighand->siglock);
266}
267
268
269/*
270 * I2C interfaces
271 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300272
273static int i2c_sendbuf(struct saa5249_device *t, int reg, int count, u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 char buf[64];
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 buf[0] = reg;
278 memcpy(buf+1, data, count);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if(i2c_master_send(t->client, buf, count+1)==count+1)
281 return 0;
282 return -1;
283}
284
285static int i2c_senddata(struct saa5249_device *t, ...)
286{
287 unsigned char buf[64];
288 int v;
289 int ct=0;
290 va_list argp;
291 va_start(argp,t);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 while((v=va_arg(argp,int))!=-1)
294 buf[ct++]=v;
295 return i2c_sendbuf(t, buf[0], ct-1, buf+1);
296}
297
298/* Get count number of bytes from I²C-device at address adr, store them in buf. Start & stop
299 * handshaking is done by this routine, ack will be sent after the last byte to inhibit further
300 * sending of data. If uaccess is TRUE, data is written to user-space with put_user.
301 * Returns -1 if I²C-device didn't send acknowledge, 0 otherwise
302 */
303
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300304static int i2c_getdata(struct saa5249_device *t, int count, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
306 if(i2c_master_recv(t->client, buf, count)!=count)
307 return -1;
308 return 0;
309}
310
311
312/*
313 * Standard character-device-driver functions
314 */
315
316static int do_saa5249_ioctl(struct inode *inode, struct file *file,
317 unsigned int cmd, void *arg)
318{
319 static int virtual_mode = FALSE;
320 struct video_device *vd = video_devdata(file);
321 struct saa5249_device *t=vd->priv;
322
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300323 switch(cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300325 case VTXIOCGETINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 {
327 vtx_info_t *info = arg;
328 info->version_major = VTX_VER_MAJ;
329 info->version_minor = VTX_VER_MIN;
330 info->numpages = NUM_DAUS;
331 /*info->cct_type = CCT_TYPE;*/
332 return 0;
333 }
334
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300335 case VTXIOCCLRPAGE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 {
337 vtx_pagereq_t *req = arg;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
340 return -EINVAL;
341 memset(t->vdau[req->pgbuf].pgbuf, ' ', sizeof(t->vdau[0].pgbuf));
342 t->vdau[req->pgbuf].clrfound = TRUE;
343 return 0;
344 }
345
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300346 case VTXIOCCLRFOUND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 {
348 vtx_pagereq_t *req = arg;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
351 return -EINVAL;
352 t->vdau[req->pgbuf].clrfound = TRUE;
353 return 0;
354 }
355
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300356 case VTXIOCPAGEREQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 {
358 vtx_pagereq_t *req = arg;
359 if (!(req->pagemask & PGMASK_PAGE))
360 req->page = 0;
361 if (!(req->pagemask & PGMASK_HOUR))
362 req->hour = 0;
363 if (!(req->pagemask & PGMASK_MINUTE))
364 req->minute = 0;
365 if (req->page < 0 || req->page > 0x8ff) /* 7FF ?? */
366 return -EINVAL;
367 req->page &= 0x7ff;
368 if (req->hour < 0 || req->hour > 0x3f || req->minute < 0 || req->minute > 0x7f ||
369 req->pagemask < 0 || req->pagemask >= PGMASK_MAX || req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
370 return -EINVAL;
371 t->vdau[req->pgbuf].sregs[0] = (req->pagemask & PG_HUND ? 0x10 : 0) | (req->page / 0x100);
372 t->vdau[req->pgbuf].sregs[1] = (req->pagemask & PG_TEN ? 0x10 : 0) | ((req->page / 0x10) & 0xf);
373 t->vdau[req->pgbuf].sregs[2] = (req->pagemask & PG_UNIT ? 0x10 : 0) | (req->page & 0xf);
374 t->vdau[req->pgbuf].sregs[3] = (req->pagemask & HR_TEN ? 0x10 : 0) | (req->hour / 0x10);
375 t->vdau[req->pgbuf].sregs[4] = (req->pagemask & HR_UNIT ? 0x10 : 0) | (req->hour & 0xf);
376 t->vdau[req->pgbuf].sregs[5] = (req->pagemask & MIN_TEN ? 0x10 : 0) | (req->minute / 0x10);
377 t->vdau[req->pgbuf].sregs[6] = (req->pagemask & MIN_UNIT ? 0x10 : 0) | (req->minute & 0xf);
378 t->vdau[req->pgbuf].stopped = FALSE;
379 t->vdau[req->pgbuf].clrfound = TRUE;
380 t->is_searching[req->pgbuf] = TRUE;
381 return 0;
382 }
383
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300384 case VTXIOCGETSTAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 {
386 vtx_pagereq_t *req = arg;
387 u8 infobits[10];
388 vtx_pageinfo_t info;
389 int a;
390
391 if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
392 return -EINVAL;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300393 if (!t->vdau[req->pgbuf].stopped)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 {
395 if (i2c_senddata(t, 2, 0, -1) ||
396 i2c_sendbuf(t, 3, sizeof(t->vdau[0].sregs), t->vdau[req->pgbuf].sregs) ||
397 i2c_senddata(t, 8, 0, 25, 0, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', -1) ||
398 i2c_senddata(t, 2, 0, t->vdau[req->pgbuf].sregs[0] | 8, -1) ||
399 i2c_senddata(t, 8, 0, 25, 0, -1))
400 return -EIO;
401 jdelay(PAGE_WAIT);
402 if (i2c_getdata(t, 10, infobits))
403 return -EIO;
404
405 if (!(infobits[8] & 0x10) && !(infobits[7] & 0xf0) && /* check FOUND-bit */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300406 (memcmp(infobits, t->vdau[req->pgbuf].laststat, sizeof(infobits)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 time_after_eq(jiffies, t->vdau[req->pgbuf].expire)))
408 { /* check if new page arrived */
409 if (i2c_senddata(t, 8, 0, 0, 0, -1) ||
410 i2c_getdata(t, VTX_PAGESIZE, t->vdau[req->pgbuf].pgbuf))
411 return -EIO;
412 t->vdau[req->pgbuf].expire = jiffies + PGBUF_EXPIRE;
413 memset(t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE, ' ', VTX_VIRTUALSIZE - VTX_PAGESIZE);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300414 if (t->virtual_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 {
416 /* Packet X/24 */
417 if (i2c_senddata(t, 8, 0, 0x20, 0, -1) ||
418 i2c_getdata(t, 40, t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE + 20 * 40))
419 return -EIO;
420 /* Packet X/27/0 */
421 if (i2c_senddata(t, 8, 0, 0x21, 0, -1) ||
422 i2c_getdata(t, 40, t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE + 16 * 40))
423 return -EIO;
424 /* Packet 8/30/0...8/30/15
425 * FIXME: AFAIK, the 5249 does hamming-decoding for some bytes in packet 8/30,
426 * so we should undo this here.
427 */
428 if (i2c_senddata(t, 8, 0, 0x22, 0, -1) ||
429 i2c_getdata(t, 40, t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE + 23 * 40))
430 return -EIO;
431 }
432 t->vdau[req->pgbuf].clrfound = FALSE;
433 memcpy(t->vdau[req->pgbuf].laststat, infobits, sizeof(infobits));
434 }
435 else
436 {
437 memcpy(infobits, t->vdau[req->pgbuf].laststat, sizeof(infobits));
438 }
439 }
440 else
441 {
442 memcpy(infobits, t->vdau[req->pgbuf].laststat, sizeof(infobits));
443 }
444
445 info.pagenum = ((infobits[8] << 8) & 0x700) | ((infobits[1] << 4) & 0xf0) | (infobits[0] & 0x0f);
446 if (info.pagenum < 0x100)
447 info.pagenum += 0x800;
448 info.hour = ((infobits[5] << 4) & 0x30) | (infobits[4] & 0x0f);
449 info.minute = ((infobits[3] << 4) & 0x70) | (infobits[2] & 0x0f);
450 info.charset = ((infobits[7] >> 1) & 7);
451 info.delete = !!(infobits[3] & 8);
452 info.headline = !!(infobits[5] & 4);
453 info.subtitle = !!(infobits[5] & 8);
454 info.supp_header = !!(infobits[6] & 1);
455 info.update = !!(infobits[6] & 2);
456 info.inter_seq = !!(infobits[6] & 4);
457 info.dis_disp = !!(infobits[6] & 8);
458 info.serial = !!(infobits[7] & 1);
459 info.notfound = !!(infobits[8] & 0x10);
460 info.pblf = !!(infobits[9] & 0x20);
461 info.hamming = 0;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300462 for (a = 0; a <= 7; a++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300464 if (infobits[a] & 0xf0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 {
466 info.hamming = 1;
467 break;
468 }
469 }
470 if (t->vdau[req->pgbuf].clrfound)
471 info.notfound = 1;
472 if(copy_to_user(req->buffer, &info, sizeof(vtx_pageinfo_t)))
473 return -EFAULT;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300474 if (!info.hamming && !info.notfound)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 {
476 t->is_searching[req->pgbuf] = FALSE;
477 }
478 return 0;
479 }
480
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300481 case VTXIOCGETPAGE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 {
483 vtx_pagereq_t *req = arg;
484 int start, end;
485
486 if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS || req->start < 0 ||
487 req->start > req->end || req->end >= (virtual_mode ? VTX_VIRTUALSIZE : VTX_PAGESIZE))
488 return -EINVAL;
489 if(copy_to_user(req->buffer, &t->vdau[req->pgbuf].pgbuf[req->start], req->end - req->start + 1))
490 return -EFAULT;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300491
492 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 * Always read the time directly from SAA5249
494 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300495
496 if (req->start <= 39 && req->end >= 32)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 {
498 int len;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300499 char buf[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 start = max(req->start, 32);
501 end = min(req->end, 39);
502 len=end-start+1;
503 if (i2c_senddata(t, 8, 0, 0, start, -1) ||
504 i2c_getdata(t, len, buf))
505 return -EIO;
506 if(copy_to_user(req->buffer+start-req->start, buf, len))
507 return -EFAULT;
508 }
509 /* Insert the current header if DAU is still searching for a page */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300510 if (req->start <= 31 && req->end >= 7 && t->is_searching[req->pgbuf])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 {
512 char buf[32];
513 int len;
514 start = max(req->start, 7);
515 end = min(req->end, 31);
516 len=end-start+1;
517 if (i2c_senddata(t, 8, 0, 0, start, -1) ||
518 i2c_getdata(t, len, buf))
519 return -EIO;
520 if(copy_to_user(req->buffer+start-req->start, buf, len))
521 return -EFAULT;
522 }
523 return 0;
524 }
525
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300526 case VTXIOCSTOPDAU:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 {
528 vtx_pagereq_t *req = arg;
529
530 if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
531 return -EINVAL;
532 t->vdau[req->pgbuf].stopped = TRUE;
533 t->is_searching[req->pgbuf] = FALSE;
534 return 0;
535 }
536
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300537 case VTXIOCPUTPAGE:
538 case VTXIOCSETDISP:
539 case VTXIOCPUTSTAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return 0;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300541
542 case VTXIOCCLRCACHE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 {
544 if (i2c_senddata(t, 0, NUM_DAUS, 0, 8, -1) || i2c_senddata(t, 11,
545 ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
546 ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', -1))
547 return -EIO;
548 if (i2c_senddata(t, 3, 0x20, -1))
549 return -EIO;
550 jdelay(10 * CLEAR_DELAY); /* I have no idea how long we have to wait here */
551 return 0;
552 }
553
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300554 case VTXIOCSETVIRT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 {
556 /* The SAA5249 has virtual-row reception turned on always */
557 t->virtual_mode = (int)(long)arg;
558 return 0;
559 }
560 }
561 return -EINVAL;
562}
563
564/*
565 * Translates old vtx IOCTLs to new ones
566 *
567 * This keeps new kernel versions compatible with old userspace programs.
568 */
569static inline unsigned int vtx_fix_command(unsigned int cmd)
570{
571 switch (cmd) {
572 case VTXIOCGETINFO_OLD:
573 cmd = VTXIOCGETINFO;
574 break;
575 case VTXIOCCLRPAGE_OLD:
576 cmd = VTXIOCCLRPAGE;
577 break;
578 case VTXIOCCLRFOUND_OLD:
579 cmd = VTXIOCCLRFOUND;
580 break;
581 case VTXIOCPAGEREQ_OLD:
582 cmd = VTXIOCPAGEREQ;
583 break;
584 case VTXIOCGETSTAT_OLD:
585 cmd = VTXIOCGETSTAT;
586 break;
587 case VTXIOCGETPAGE_OLD:
588 cmd = VTXIOCGETPAGE;
589 break;
590 case VTXIOCSTOPDAU_OLD:
591 cmd = VTXIOCSTOPDAU;
592 break;
593 case VTXIOCPUTPAGE_OLD:
594 cmd = VTXIOCPUTPAGE;
595 break;
596 case VTXIOCSETDISP_OLD:
597 cmd = VTXIOCSETDISP;
598 break;
599 case VTXIOCPUTSTAT_OLD:
600 cmd = VTXIOCPUTSTAT;
601 break;
602 case VTXIOCCLRCACHE_OLD:
603 cmd = VTXIOCCLRCACHE;
604 break;
605 case VTXIOCSETVIRT_OLD:
606 cmd = VTXIOCSETVIRT;
607 break;
608 }
609 return cmd;
610}
611
612/*
613 * Handle the locking
614 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616static int saa5249_ioctl(struct inode *inode, struct file *file,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300617 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
619 struct video_device *vd = video_devdata(file);
620 struct saa5249_device *t=vd->priv;
621 int err;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 cmd = vtx_fix_command(cmd);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200624 mutex_lock(&t->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 err = video_usercopy(inode,file,cmd,arg,do_saa5249_ioctl);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200626 mutex_unlock(&t->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return err;
628}
629
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300630static int saa5249_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
632 struct video_device *vd = video_devdata(file);
633 struct saa5249_device *t=vd->priv;
634 int err,pgbuf;
635
636 err = video_exclusive_open(inode,file);
637 if (err < 0)
638 return err;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (t->client==NULL) {
641 err = -ENODEV;
642 goto fail;
643 }
644
645 if (i2c_senddata(t, 0, 0, -1) || /* Select R11 */
646 /* Turn off parity checks (we do this ourselves) */
647 i2c_senddata(t, 1, disp_modes[t->disp_mode][0], 0, -1) ||
648 /* Display TV-picture, no virtual rows */
649 i2c_senddata(t, 4, NUM_DAUS, disp_modes[t->disp_mode][1], disp_modes[t->disp_mode][2], 7, -1)) /* Set display to page 4 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 {
652 err = -EIO;
653 goto fail;
654 }
655
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300656 for (pgbuf = 0; pgbuf < NUM_DAUS; pgbuf++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 {
658 memset(t->vdau[pgbuf].pgbuf, ' ', sizeof(t->vdau[0].pgbuf));
659 memset(t->vdau[pgbuf].sregs, 0, sizeof(t->vdau[0].sregs));
660 memset(t->vdau[pgbuf].laststat, 0, sizeof(t->vdau[0].laststat));
661 t->vdau[pgbuf].expire = 0;
662 t->vdau[pgbuf].clrfound = TRUE;
663 t->vdau[pgbuf].stopped = TRUE;
664 t->is_searching[pgbuf] = FALSE;
665 }
666 t->virtual_mode=FALSE;
667 return 0;
668
669 fail:
670 video_exclusive_release(inode,file);
671 return err;
672}
673
674
675
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300676static int saa5249_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
678 struct video_device *vd = video_devdata(file);
679 struct saa5249_device *t=vd->priv;
680 i2c_senddata(t, 1, 0x20, -1); /* Turn off CCT */
681 i2c_senddata(t, 5, 3, 3, -1); /* Turn off TV-display */
682 video_exclusive_release(inode,file);
683 return 0;
684}
685
686static int __init init_saa_5249 (void)
687{
688 printk(KERN_INFO "SAA5249 driver (" IF_NAME " interface) for VideoText version %d.%d\n",
689 VTX_VER_MAJ, VTX_VER_MIN);
690 return i2c_add_driver(&i2c_driver_videotext);
691}
692
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300693static void __exit cleanup_saa_5249 (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
695 i2c_del_driver(&i2c_driver_videotext);
696}
697
698module_init(init_saa_5249);
699module_exit(cleanup_saa_5249);
700
701static struct file_operations saa_fops = {
702 .owner = THIS_MODULE,
703 .open = saa5249_open,
704 .release = saa5249_release,
705 .ioctl = saa5249_ioctl,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200706 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 .llseek = no_llseek,
708};
709
710static struct video_device saa_template =
711{
712 .owner = THIS_MODULE,
713 .name = IF_NAME,
714 .type = VID_TYPE_TELETEXT, /*| VID_TYPE_TUNER ?? */
715 .hardware = VID_HARDWARE_SAA5249,
716 .fops = &saa_fops,
717};
718
719MODULE_LICENSE("GPL");