Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 | * |
Alan Cox | d9b0144 | 2008-10-27 15:13:47 -0300 | [diff] [blame] | 11 | * Alan Cox <alan@lxorguk.ukuu.org.uk> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | * Derived From |
| 19 | * |
| 20 | * vtx.c: |
| 21 | * This is a loadable character-device-driver for videotext-interfaces |
| 22 | * (aka teletext). Please check the Makefile/README for a list of supported |
| 23 | * interfaces. |
| 24 | * |
| 25 | * Copyright (c) 1994-97 Martin Buck <martin-2.buck@student.uni-ulm.de> |
| 26 | * |
| 27 | * |
| 28 | * This program is free software; you can redistribute it and/or modify |
| 29 | * it under the terms of the GNU General Public License as published by |
| 30 | * the Free Software Foundation; either version 2 of the License, or |
| 31 | * (at your option) any later version. |
| 32 | * |
| 33 | * This program is distributed in the hope that it will be useful, |
| 34 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 35 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 36 | * GNU General Public License for more details. |
| 37 | * |
| 38 | * You should have received a copy of the GNU General Public License |
| 39 | * along with this program; if not, write to the Free Software |
| 40 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 41 | * USA. |
| 42 | */ |
| 43 | |
| 44 | #include <linux/module.h> |
| 45 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <linux/i2c.h> |
Hans Verkuil | 96af988 | 2008-09-07 08:31:38 -0300 | [diff] [blame] | 49 | #include <linux/smp_lock.h> |
| 50 | #include <linux/mutex.h> |
Hans Verkuil | d4f59de | 2008-09-30 03:14:02 -0300 | [diff] [blame] | 51 | #include <linux/delay.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include <linux/videotext.h> |
| 53 | #include <linux/videodev.h> |
Mauro Carvalho Chehab | 5e87efa | 2006-06-05 10:26:32 -0300 | [diff] [blame] | 54 | #include <media/v4l2-common.h> |
Hans Verkuil | 35ea11f | 2008-07-20 08:12:02 -0300 | [diff] [blame] | 55 | #include <media/v4l2-ioctl.h> |
Hans Verkuil | 96af988 | 2008-09-07 08:31:38 -0300 | [diff] [blame] | 56 | #include <media/v4l2-i2c-drv-legacy.h> |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 57 | |
Hans Verkuil | 96af988 | 2008-09-07 08:31:38 -0300 | [diff] [blame] | 58 | MODULE_AUTHOR("Michael Geng <linux@MichaelGeng.de>"); |
| 59 | MODULE_DESCRIPTION("Philips SAA5249 Teletext decoder driver"); |
| 60 | MODULE_LICENSE("GPL"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
| 62 | #define VTX_VER_MAJ 1 |
| 63 | #define VTX_VER_MIN 8 |
| 64 | |
| 65 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | #define NUM_DAUS 4 |
| 67 | #define NUM_BUFS 8 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 69 | static const int disp_modes[8][3] = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | { |
| 71 | { 0x46, 0x03, 0x03 }, /* DISPOFF */ |
| 72 | { 0x46, 0xcc, 0xcc }, /* DISPNORM */ |
| 73 | { 0x44, 0x0f, 0x0f }, /* DISPTRANS */ |
| 74 | { 0x46, 0xcc, 0x46 }, /* DISPINS */ |
| 75 | { 0x44, 0x03, 0x03 }, /* DISPOFF, interlaced */ |
| 76 | { 0x44, 0xcc, 0xcc }, /* DISPNORM, interlaced */ |
| 77 | { 0x44, 0x0f, 0x0f }, /* DISPTRANS, interlaced */ |
| 78 | { 0x44, 0xcc, 0x46 } /* DISPINS, interlaced */ |
| 79 | }; |
| 80 | |
| 81 | |
| 82 | |
Mauro Carvalho Chehab | 09df5cbe | 2007-07-17 16:25:38 -0300 | [diff] [blame] | 83 | #define PAGE_WAIT msecs_to_jiffies(300) /* Time between requesting page and */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | /* checking status bits */ |
Mauro Carvalho Chehab | 09df5cbe | 2007-07-17 16:25:38 -0300 | [diff] [blame] | 85 | #define PGBUF_EXPIRE msecs_to_jiffies(15000) /* Time to wait before retransmitting */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | /* page regardless of infobits */ |
| 87 | typedef struct { |
| 88 | u8 pgbuf[VTX_VIRTUALSIZE]; /* Page-buffer */ |
| 89 | u8 laststat[10]; /* Last value of infobits for DAU */ |
| 90 | u8 sregs[7]; /* Page-request registers */ |
| 91 | unsigned long expire; /* Time when page will be expired */ |
| 92 | unsigned clrfound : 1; /* VTXIOCCLRFOUND has been called */ |
| 93 | unsigned stopped : 1; /* VTXIOCSTOPDAU has been called */ |
| 94 | } vdau_t; |
| 95 | |
| 96 | struct saa5249_device |
| 97 | { |
| 98 | vdau_t vdau[NUM_DAUS]; /* Data for virtual DAUs (the 5249 only has one */ |
| 99 | /* real DAU, so we have to simulate some more) */ |
| 100 | int vtx_use_count; |
| 101 | int is_searching[NUM_DAUS]; |
| 102 | int disp_mode; |
| 103 | int virtual_mode; |
| 104 | struct i2c_client *client; |
Hans Verkuil | 7d43cd5 | 2008-08-23 05:31:47 -0300 | [diff] [blame] | 105 | unsigned long in_use; |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 106 | struct mutex lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | |
| 110 | #define CCTWR 34 /* I²C write/read-address of vtx-chip */ |
| 111 | #define CCTRD 35 |
| 112 | #define NOACK_REPEAT 10 /* Retry access this many times on failure */ |
Mauro Carvalho Chehab | 09df5cbe | 2007-07-17 16:25:38 -0300 | [diff] [blame] | 113 | #define CLEAR_DELAY msecs_to_jiffies(50) /* Time required to clear a page */ |
| 114 | #define READY_TIMEOUT msecs_to_jiffies(30) /* Time to wait for ready signal of I2C-bus interface */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | #define INIT_DELAY 500 /* Time in usec to wait at initialization of CEA interface */ |
| 116 | #define START_DELAY 10 /* Time in usec to wait before starting write-cycle (CEA) */ |
| 117 | |
| 118 | #define VTX_DEV_MINOR 0 |
| 119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | static struct video_device saa_template; /* Declared near bottom */ |
| 121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | /* |
| 123 | * Wait the given number of jiffies (10ms). This calls the scheduler, so the actual |
| 124 | * delay may be longer. |
| 125 | */ |
| 126 | |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 127 | static void jdelay(unsigned long delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | { |
| 129 | sigset_t oldblocked = current->blocked; |
| 130 | |
| 131 | spin_lock_irq(¤t->sighand->siglock); |
| 132 | sigfillset(¤t->blocked); |
| 133 | recalc_sigpending(); |
| 134 | spin_unlock_irq(¤t->sighand->siglock); |
| 135 | msleep_interruptible(jiffies_to_msecs(delay)); |
| 136 | |
| 137 | spin_lock_irq(¤t->sighand->siglock); |
| 138 | current->blocked = oldblocked; |
| 139 | recalc_sigpending(); |
| 140 | spin_unlock_irq(¤t->sighand->siglock); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | /* |
| 145 | * I2C interfaces |
| 146 | */ |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 147 | |
| 148 | static int i2c_sendbuf(struct saa5249_device *t, int reg, int count, u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | { |
| 150 | char buf[64]; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | buf[0] = reg; |
| 153 | memcpy(buf+1, data, count); |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 154 | |
Hans Verkuil | 96af988 | 2008-09-07 08:31:38 -0300 | [diff] [blame] | 155 | if (i2c_master_send(t->client, buf, count + 1) == count + 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | return 0; |
| 157 | return -1; |
| 158 | } |
| 159 | |
| 160 | static int i2c_senddata(struct saa5249_device *t, ...) |
| 161 | { |
| 162 | unsigned char buf[64]; |
| 163 | int v; |
Richard Knutsson | b730a81 | 2007-11-27 06:59:37 -0300 | [diff] [blame] | 164 | int ct = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | va_list argp; |
| 166 | va_start(argp,t); |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 167 | |
Richard Knutsson | b730a81 | 2007-11-27 06:59:37 -0300 | [diff] [blame] | 168 | while ((v = va_arg(argp, int)) != -1) |
| 169 | buf[ct++] = v; |
| 170 | |
| 171 | va_end(argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | return i2c_sendbuf(t, buf[0], ct-1, buf+1); |
| 173 | } |
| 174 | |
Marcin Garski | db955170 | 2007-10-19 23:22:11 +0200 | [diff] [blame] | 175 | /* Get count number of bytes from I²C-device at address adr, store them in buf. Start & stop |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | * handshaking is done by this routine, ack will be sent after the last byte to inhibit further |
Richard Knutsson | e8be02a | 2007-02-06 21:52:04 -0300 | [diff] [blame] | 177 | * sending of data. If uaccess is 'true', data is written to user-space with put_user. |
Marcin Garski | db955170 | 2007-10-19 23:22:11 +0200 | [diff] [blame] | 178 | * Returns -1 if I²C-device didn't send acknowledge, 0 otherwise |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | */ |
| 180 | |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 181 | static int i2c_getdata(struct saa5249_device *t, int count, u8 *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | { |
| 183 | if(i2c_master_recv(t->client, buf, count)!=count) |
| 184 | return -1; |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | |
| 189 | /* |
| 190 | * Standard character-device-driver functions |
| 191 | */ |
| 192 | |
Hans Verkuil | 069b747 | 2008-12-30 07:04:34 -0300 | [diff] [blame] | 193 | static long do_saa5249_ioctl(struct file *file, unsigned int cmd, void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | { |
Richard Knutsson | e8be02a | 2007-02-06 21:52:04 -0300 | [diff] [blame] | 195 | static int virtual_mode = false; |
Hans Verkuil | c170ecf | 2008-08-23 08:32:09 -0300 | [diff] [blame] | 196 | struct saa5249_device *t = video_drvdata(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
Hans Verkuil | 96af988 | 2008-09-07 08:31:38 -0300 | [diff] [blame] | 198 | switch (cmd) { |
| 199 | case VTXIOCGETINFO: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | { |
Hans Verkuil | 96af988 | 2008-09-07 08:31:38 -0300 | [diff] [blame] | 201 | vtx_info_t *info = arg; |
| 202 | info->version_major = VTX_VER_MAJ; |
| 203 | info->version_minor = VTX_VER_MIN; |
| 204 | info->numpages = NUM_DAUS; |
| 205 | /*info->cct_type = CCT_TYPE;*/ |
| 206 | return 0; |
| 207 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
Hans Verkuil | 96af988 | 2008-09-07 08:31:38 -0300 | [diff] [blame] | 209 | case VTXIOCCLRPAGE: |
| 210 | { |
| 211 | vtx_pagereq_t *req = arg; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 212 | |
Hans Verkuil | 96af988 | 2008-09-07 08:31:38 -0300 | [diff] [blame] | 213 | if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS) |
| 214 | return -EINVAL; |
| 215 | memset(t->vdau[req->pgbuf].pgbuf, ' ', sizeof(t->vdau[0].pgbuf)); |
| 216 | t->vdau[req->pgbuf].clrfound = true; |
| 217 | return 0; |
| 218 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
Hans Verkuil | 96af988 | 2008-09-07 08:31:38 -0300 | [diff] [blame] | 220 | case VTXIOCCLRFOUND: |
| 221 | { |
| 222 | vtx_pagereq_t *req = arg; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 223 | |
Hans Verkuil | 96af988 | 2008-09-07 08:31:38 -0300 | [diff] [blame] | 224 | if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS) |
| 225 | return -EINVAL; |
| 226 | t->vdau[req->pgbuf].clrfound = true; |
| 227 | return 0; |
| 228 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
Hans Verkuil | 96af988 | 2008-09-07 08:31:38 -0300 | [diff] [blame] | 230 | case VTXIOCPAGEREQ: |
| 231 | { |
| 232 | vtx_pagereq_t *req = arg; |
| 233 | if (!(req->pagemask & PGMASK_PAGE)) |
| 234 | req->page = 0; |
| 235 | if (!(req->pagemask & PGMASK_HOUR)) |
| 236 | req->hour = 0; |
| 237 | if (!(req->pagemask & PGMASK_MINUTE)) |
| 238 | req->minute = 0; |
| 239 | if (req->page < 0 || req->page > 0x8ff) /* 7FF ?? */ |
| 240 | return -EINVAL; |
| 241 | req->page &= 0x7ff; |
| 242 | if (req->hour < 0 || req->hour > 0x3f || req->minute < 0 || req->minute > 0x7f || |
| 243 | req->pagemask < 0 || req->pagemask >= PGMASK_MAX || req->pgbuf < 0 || req->pgbuf >= NUM_DAUS) |
| 244 | return -EINVAL; |
| 245 | t->vdau[req->pgbuf].sregs[0] = (req->pagemask & PG_HUND ? 0x10 : 0) | (req->page / 0x100); |
| 246 | t->vdau[req->pgbuf].sregs[1] = (req->pagemask & PG_TEN ? 0x10 : 0) | ((req->page / 0x10) & 0xf); |
| 247 | t->vdau[req->pgbuf].sregs[2] = (req->pagemask & PG_UNIT ? 0x10 : 0) | (req->page & 0xf); |
| 248 | t->vdau[req->pgbuf].sregs[3] = (req->pagemask & HR_TEN ? 0x10 : 0) | (req->hour / 0x10); |
| 249 | t->vdau[req->pgbuf].sregs[4] = (req->pagemask & HR_UNIT ? 0x10 : 0) | (req->hour & 0xf); |
| 250 | t->vdau[req->pgbuf].sregs[5] = (req->pagemask & MIN_TEN ? 0x10 : 0) | (req->minute / 0x10); |
| 251 | t->vdau[req->pgbuf].sregs[6] = (req->pagemask & MIN_UNIT ? 0x10 : 0) | (req->minute & 0xf); |
| 252 | t->vdau[req->pgbuf].stopped = false; |
| 253 | t->vdau[req->pgbuf].clrfound = true; |
| 254 | t->is_searching[req->pgbuf] = true; |
| 255 | return 0; |
| 256 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
Hans Verkuil | 96af988 | 2008-09-07 08:31:38 -0300 | [diff] [blame] | 258 | case VTXIOCGETSTAT: |
| 259 | { |
| 260 | vtx_pagereq_t *req = arg; |
| 261 | u8 infobits[10]; |
| 262 | vtx_pageinfo_t info; |
| 263 | int a; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | |
Hans Verkuil | 96af988 | 2008-09-07 08:31:38 -0300 | [diff] [blame] | 265 | if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS) |
| 266 | return -EINVAL; |
| 267 | if (!t->vdau[req->pgbuf].stopped) { |
| 268 | if (i2c_senddata(t, 2, 0, -1) || |
| 269 | i2c_sendbuf(t, 3, sizeof(t->vdau[0].sregs), t->vdau[req->pgbuf].sregs) || |
| 270 | i2c_senddata(t, 8, 0, 25, 0, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', -1) || |
| 271 | i2c_senddata(t, 2, 0, t->vdau[req->pgbuf].sregs[0] | 8, -1) || |
| 272 | i2c_senddata(t, 8, 0, 25, 0, -1)) |
| 273 | return -EIO; |
| 274 | jdelay(PAGE_WAIT); |
| 275 | if (i2c_getdata(t, 10, infobits)) |
| 276 | return -EIO; |
| 277 | |
| 278 | if (!(infobits[8] & 0x10) && !(infobits[7] & 0xf0) && /* check FOUND-bit */ |
| 279 | (memcmp(infobits, t->vdau[req->pgbuf].laststat, sizeof(infobits)) || |
| 280 | time_after_eq(jiffies, t->vdau[req->pgbuf].expire))) |
| 281 | { /* check if new page arrived */ |
| 282 | if (i2c_senddata(t, 8, 0, 0, 0, -1) || |
| 283 | i2c_getdata(t, VTX_PAGESIZE, t->vdau[req->pgbuf].pgbuf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | return -EIO; |
Hans Verkuil | 96af988 | 2008-09-07 08:31:38 -0300 | [diff] [blame] | 285 | t->vdau[req->pgbuf].expire = jiffies + PGBUF_EXPIRE; |
| 286 | memset(t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE, ' ', VTX_VIRTUALSIZE - VTX_PAGESIZE); |
| 287 | if (t->virtual_mode) { |
| 288 | /* Packet X/24 */ |
| 289 | if (i2c_senddata(t, 8, 0, 0x20, 0, -1) || |
| 290 | i2c_getdata(t, 40, t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE + 20 * 40)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | return -EIO; |
Hans Verkuil | 96af988 | 2008-09-07 08:31:38 -0300 | [diff] [blame] | 292 | /* Packet X/27/0 */ |
| 293 | if (i2c_senddata(t, 8, 0, 0x21, 0, -1) || |
| 294 | i2c_getdata(t, 40, t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE + 16 * 40)) |
| 295 | return -EIO; |
| 296 | /* Packet 8/30/0...8/30/15 |
| 297 | * FIXME: AFAIK, the 5249 does hamming-decoding for some bytes in packet 8/30, |
| 298 | * so we should undo this here. |
| 299 | */ |
| 300 | if (i2c_senddata(t, 8, 0, 0x22, 0, -1) || |
| 301 | i2c_getdata(t, 40, t->vdau[req->pgbuf].pgbuf + VTX_PAGESIZE + 23 * 40)) |
| 302 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | } |
Hans Verkuil | 96af988 | 2008-09-07 08:31:38 -0300 | [diff] [blame] | 304 | t->vdau[req->pgbuf].clrfound = false; |
| 305 | memcpy(t->vdau[req->pgbuf].laststat, infobits, sizeof(infobits)); |
| 306 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | memcpy(infobits, t->vdau[req->pgbuf].laststat, sizeof(infobits)); |
| 308 | } |
Hans Verkuil | 96af988 | 2008-09-07 08:31:38 -0300 | [diff] [blame] | 309 | } else { |
| 310 | memcpy(infobits, t->vdau[req->pgbuf].laststat, sizeof(infobits)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Hans Verkuil | 96af988 | 2008-09-07 08:31:38 -0300 | [diff] [blame] | 313 | info.pagenum = ((infobits[8] << 8) & 0x700) | ((infobits[1] << 4) & 0xf0) | (infobits[0] & 0x0f); |
| 314 | if (info.pagenum < 0x100) |
| 315 | info.pagenum += 0x800; |
| 316 | info.hour = ((infobits[5] << 4) & 0x30) | (infobits[4] & 0x0f); |
| 317 | info.minute = ((infobits[3] << 4) & 0x70) | (infobits[2] & 0x0f); |
| 318 | info.charset = ((infobits[7] >> 1) & 7); |
| 319 | info.delete = !!(infobits[3] & 8); |
| 320 | info.headline = !!(infobits[5] & 4); |
| 321 | info.subtitle = !!(infobits[5] & 8); |
| 322 | info.supp_header = !!(infobits[6] & 1); |
| 323 | info.update = !!(infobits[6] & 2); |
| 324 | info.inter_seq = !!(infobits[6] & 4); |
| 325 | info.dis_disp = !!(infobits[6] & 8); |
| 326 | info.serial = !!(infobits[7] & 1); |
| 327 | info.notfound = !!(infobits[8] & 0x10); |
| 328 | info.pblf = !!(infobits[9] & 0x20); |
| 329 | info.hamming = 0; |
| 330 | for (a = 0; a <= 7; a++) { |
| 331 | if (infobits[a] & 0xf0) { |
| 332 | info.hamming = 1; |
| 333 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | } |
Hans Verkuil | 96af988 | 2008-09-07 08:31:38 -0300 | [diff] [blame] | 336 | if (t->vdau[req->pgbuf].clrfound) |
| 337 | info.notfound = 1; |
| 338 | if (copy_to_user(req->buffer, &info, sizeof(vtx_pageinfo_t))) |
| 339 | return -EFAULT; |
| 340 | if (!info.hamming && !info.notfound) |
Richard Knutsson | e8be02a | 2007-02-06 21:52:04 -0300 | [diff] [blame] | 341 | t->is_searching[req->pgbuf] = false; |
Hans Verkuil | 96af988 | 2008-09-07 08:31:38 -0300 | [diff] [blame] | 342 | return 0; |
| 343 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
Hans Verkuil | 96af988 | 2008-09-07 08:31:38 -0300 | [diff] [blame] | 345 | case VTXIOCGETPAGE: |
| 346 | { |
| 347 | vtx_pagereq_t *req = arg; |
| 348 | int start, end; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 349 | |
Hans Verkuil | 96af988 | 2008-09-07 08:31:38 -0300 | [diff] [blame] | 350 | if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS || req->start < 0 || |
| 351 | req->start > req->end || req->end >= (virtual_mode ? VTX_VIRTUALSIZE : VTX_PAGESIZE)) |
| 352 | return -EINVAL; |
| 353 | if (copy_to_user(req->buffer, &t->vdau[req->pgbuf].pgbuf[req->start], req->end - req->start + 1)) |
| 354 | return -EFAULT; |
| 355 | |
| 356 | /* |
| 357 | * Always read the time directly from SAA5249 |
| 358 | */ |
| 359 | |
| 360 | if (req->start <= 39 && req->end >= 32) { |
| 361 | int len; |
| 362 | char buf[16]; |
| 363 | start = max(req->start, 32); |
| 364 | end = min(req->end, 39); |
| 365 | len = end - start + 1; |
| 366 | if (i2c_senddata(t, 8, 0, 0, start, -1) || |
| 367 | i2c_getdata(t, len, buf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | return -EIO; |
Hans Verkuil | 96af988 | 2008-09-07 08:31:38 -0300 | [diff] [blame] | 369 | if (copy_to_user(req->buffer + start - req->start, buf, len)) |
| 370 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | } |
Hans Verkuil | 96af988 | 2008-09-07 08:31:38 -0300 | [diff] [blame] | 372 | /* Insert the current header if DAU is still searching for a page */ |
| 373 | if (req->start <= 31 && req->end >= 7 && t->is_searching[req->pgbuf]) { |
| 374 | char buf[32]; |
| 375 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | |
Hans Verkuil | 96af988 | 2008-09-07 08:31:38 -0300 | [diff] [blame] | 377 | start = max(req->start, 7); |
| 378 | end = min(req->end, 31); |
| 379 | len = end - start + 1; |
| 380 | if (i2c_senddata(t, 8, 0, 0, start, -1) || |
| 381 | i2c_getdata(t, len, buf)) |
| 382 | return -EIO; |
| 383 | if (copy_to_user(req->buffer + start - req->start, buf, len)) |
| 384 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | } |
Hans Verkuil | 96af988 | 2008-09-07 08:31:38 -0300 | [diff] [blame] | 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | case VTXIOCSTOPDAU: |
| 390 | { |
| 391 | vtx_pagereq_t *req = arg; |
| 392 | |
| 393 | if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS) |
| 394 | return -EINVAL; |
| 395 | t->vdau[req->pgbuf].stopped = true; |
| 396 | t->is_searching[req->pgbuf] = false; |
| 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | case VTXIOCPUTPAGE: |
| 401 | case VTXIOCSETDISP: |
| 402 | case VTXIOCPUTSTAT: |
| 403 | return 0; |
| 404 | |
| 405 | case VTXIOCCLRCACHE: |
| 406 | { |
| 407 | if (i2c_senddata(t, 0, NUM_DAUS, 0, 8, -1) || i2c_senddata(t, 11, |
| 408 | ' ', ' ', ' ', ' ', ' ', ' ', |
| 409 | ' ', ' ', ' ', ' ', ' ', ' ', |
| 410 | ' ', ' ', ' ', ' ', ' ', ' ', |
| 411 | ' ', ' ', ' ', ' ', ' ', ' ', |
| 412 | -1)) |
| 413 | return -EIO; |
| 414 | if (i2c_senddata(t, 3, 0x20, -1)) |
| 415 | return -EIO; |
| 416 | jdelay(10 * CLEAR_DELAY); /* I have no idea how long we have to wait here */ |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | case VTXIOCSETVIRT: |
| 421 | { |
| 422 | /* The SAA5249 has virtual-row reception turned on always */ |
| 423 | t->virtual_mode = (int)(long)arg; |
| 424 | return 0; |
| 425 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | } |
| 427 | return -EINVAL; |
| 428 | } |
| 429 | |
| 430 | /* |
| 431 | * Translates old vtx IOCTLs to new ones |
| 432 | * |
| 433 | * This keeps new kernel versions compatible with old userspace programs. |
| 434 | */ |
| 435 | static inline unsigned int vtx_fix_command(unsigned int cmd) |
| 436 | { |
| 437 | switch (cmd) { |
| 438 | case VTXIOCGETINFO_OLD: |
| 439 | cmd = VTXIOCGETINFO; |
| 440 | break; |
| 441 | case VTXIOCCLRPAGE_OLD: |
| 442 | cmd = VTXIOCCLRPAGE; |
| 443 | break; |
| 444 | case VTXIOCCLRFOUND_OLD: |
| 445 | cmd = VTXIOCCLRFOUND; |
| 446 | break; |
| 447 | case VTXIOCPAGEREQ_OLD: |
| 448 | cmd = VTXIOCPAGEREQ; |
| 449 | break; |
| 450 | case VTXIOCGETSTAT_OLD: |
| 451 | cmd = VTXIOCGETSTAT; |
| 452 | break; |
| 453 | case VTXIOCGETPAGE_OLD: |
| 454 | cmd = VTXIOCGETPAGE; |
| 455 | break; |
| 456 | case VTXIOCSTOPDAU_OLD: |
| 457 | cmd = VTXIOCSTOPDAU; |
| 458 | break; |
| 459 | case VTXIOCPUTPAGE_OLD: |
| 460 | cmd = VTXIOCPUTPAGE; |
| 461 | break; |
| 462 | case VTXIOCSETDISP_OLD: |
| 463 | cmd = VTXIOCSETDISP; |
| 464 | break; |
| 465 | case VTXIOCPUTSTAT_OLD: |
| 466 | cmd = VTXIOCPUTSTAT; |
| 467 | break; |
| 468 | case VTXIOCCLRCACHE_OLD: |
| 469 | cmd = VTXIOCCLRCACHE; |
| 470 | break; |
| 471 | case VTXIOCSETVIRT_OLD: |
| 472 | cmd = VTXIOCSETVIRT; |
| 473 | break; |
| 474 | } |
| 475 | return cmd; |
| 476 | } |
| 477 | |
| 478 | /* |
| 479 | * Handle the locking |
| 480 | */ |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 481 | |
Hans Verkuil | 069b747 | 2008-12-30 07:04:34 -0300 | [diff] [blame] | 482 | static long saa5249_ioctl(struct file *file, |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 483 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | { |
Hans Verkuil | c170ecf | 2008-08-23 08:32:09 -0300 | [diff] [blame] | 485 | struct saa5249_device *t = video_drvdata(file); |
Hans Verkuil | 069b747 | 2008-12-30 07:04:34 -0300 | [diff] [blame] | 486 | long err; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 487 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | cmd = vtx_fix_command(cmd); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 489 | mutex_lock(&t->lock); |
Hans Verkuil | f473bf7 | 2008-11-01 08:25:11 -0300 | [diff] [blame] | 490 | err = video_usercopy(file, cmd, arg, do_saa5249_ioctl); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 491 | mutex_unlock(&t->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | return err; |
| 493 | } |
| 494 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 495 | static int saa5249_open(struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | { |
Hans Verkuil | c170ecf | 2008-08-23 08:32:09 -0300 | [diff] [blame] | 497 | struct saa5249_device *t = video_drvdata(file); |
Hans Verkuil | 7d43cd5 | 2008-08-23 05:31:47 -0300 | [diff] [blame] | 498 | int pgbuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | |
Hans Verkuil | 7d43cd5 | 2008-08-23 05:31:47 -0300 | [diff] [blame] | 500 | if (t->client == NULL) |
| 501 | return -ENODEV; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 502 | |
Hans Verkuil | 7d43cd5 | 2008-08-23 05:31:47 -0300 | [diff] [blame] | 503 | if (test_and_set_bit(0, &t->in_use)) |
| 504 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | |
Hans Verkuil | 7d43cd5 | 2008-08-23 05:31:47 -0300 | [diff] [blame] | 506 | if (i2c_senddata(t, 0, 0, -1) || /* Select R11 */ |
| 507 | /* Turn off parity checks (we do this ourselves) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | i2c_senddata(t, 1, disp_modes[t->disp_mode][0], 0, -1) || |
Hans Verkuil | 7d43cd5 | 2008-08-23 05:31:47 -0300 | [diff] [blame] | 509 | /* Display TV-picture, no virtual rows */ |
| 510 | i2c_senddata(t, 4, NUM_DAUS, disp_modes[t->disp_mode][1], disp_modes[t->disp_mode][2], 7, -1)) |
| 511 | /* Set display to page 4 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | { |
Hans Verkuil | 7d43cd5 | 2008-08-23 05:31:47 -0300 | [diff] [blame] | 513 | clear_bit(0, &t->in_use); |
| 514 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | } |
| 516 | |
Hans Verkuil | 7d43cd5 | 2008-08-23 05:31:47 -0300 | [diff] [blame] | 517 | for (pgbuf = 0; pgbuf < NUM_DAUS; pgbuf++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | memset(t->vdau[pgbuf].pgbuf, ' ', sizeof(t->vdau[0].pgbuf)); |
| 519 | memset(t->vdau[pgbuf].sregs, 0, sizeof(t->vdau[0].sregs)); |
| 520 | memset(t->vdau[pgbuf].laststat, 0, sizeof(t->vdau[0].laststat)); |
| 521 | t->vdau[pgbuf].expire = 0; |
Richard Knutsson | e8be02a | 2007-02-06 21:52:04 -0300 | [diff] [blame] | 522 | t->vdau[pgbuf].clrfound = true; |
| 523 | t->vdau[pgbuf].stopped = true; |
| 524 | t->is_searching[pgbuf] = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | } |
Richard Knutsson | e8be02a | 2007-02-06 21:52:04 -0300 | [diff] [blame] | 526 | t->virtual_mode = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | |
| 531 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 532 | static int saa5249_release(struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | { |
Hans Verkuil | c170ecf | 2008-08-23 08:32:09 -0300 | [diff] [blame] | 534 | struct saa5249_device *t = video_drvdata(file); |
Hans Verkuil | 7d43cd5 | 2008-08-23 05:31:47 -0300 | [diff] [blame] | 535 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | i2c_senddata(t, 1, 0x20, -1); /* Turn off CCT */ |
| 537 | i2c_senddata(t, 5, 3, 3, -1); /* Turn off TV-display */ |
Hans Verkuil | 7d43cd5 | 2008-08-23 05:31:47 -0300 | [diff] [blame] | 538 | clear_bit(0, &t->in_use); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | return 0; |
| 540 | } |
| 541 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 542 | static const struct v4l2_file_operations saa_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | .owner = THIS_MODULE, |
| 544 | .open = saa5249_open, |
| 545 | .release = saa5249_release, |
| 546 | .ioctl = saa5249_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | }; |
| 548 | |
| 549 | static struct video_device saa_template = |
| 550 | { |
Hans Verkuil | 96af988 | 2008-09-07 08:31:38 -0300 | [diff] [blame] | 551 | .name = "saa5249", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | .fops = &saa_fops, |
Hans Verkuil | aa5e90a | 2008-08-23 06:23:55 -0300 | [diff] [blame] | 553 | .release = video_device_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | }; |
| 555 | |
Hans Verkuil | 96af988 | 2008-09-07 08:31:38 -0300 | [diff] [blame] | 556 | /* Addresses to scan */ |
| 557 | static unsigned short normal_i2c[] = { 0x22 >> 1, I2C_CLIENT_END }; |
| 558 | |
| 559 | I2C_CLIENT_INSMOD; |
| 560 | |
| 561 | static int saa5249_probe(struct i2c_client *client, |
| 562 | const struct i2c_device_id *id) |
| 563 | { |
| 564 | int pgbuf; |
| 565 | int err; |
| 566 | struct video_device *vd; |
| 567 | struct saa5249_device *t; |
| 568 | |
| 569 | v4l_info(client, "chip found @ 0x%x (%s)\n", |
| 570 | client->addr << 1, client->adapter->name); |
| 571 | v4l_info(client, "VideoText version %d.%d\n", |
| 572 | VTX_VER_MAJ, VTX_VER_MIN); |
| 573 | t = kzalloc(sizeof(*t), GFP_KERNEL); |
| 574 | if (t == NULL) |
| 575 | return -ENOMEM; |
| 576 | mutex_init(&t->lock); |
| 577 | |
| 578 | /* Now create a video4linux device */ |
| 579 | vd = kmalloc(sizeof(struct video_device), GFP_KERNEL); |
| 580 | if (vd == NULL) { |
| 581 | kfree(client); |
| 582 | return -ENOMEM; |
| 583 | } |
| 584 | i2c_set_clientdata(client, vd); |
| 585 | memcpy(vd, &saa_template, sizeof(*vd)); |
| 586 | |
| 587 | for (pgbuf = 0; pgbuf < NUM_DAUS; pgbuf++) { |
| 588 | memset(t->vdau[pgbuf].pgbuf, ' ', sizeof(t->vdau[0].pgbuf)); |
| 589 | memset(t->vdau[pgbuf].sregs, 0, sizeof(t->vdau[0].sregs)); |
| 590 | memset(t->vdau[pgbuf].laststat, 0, sizeof(t->vdau[0].laststat)); |
| 591 | t->vdau[pgbuf].expire = 0; |
| 592 | t->vdau[pgbuf].clrfound = true; |
| 593 | t->vdau[pgbuf].stopped = true; |
| 594 | t->is_searching[pgbuf] = false; |
| 595 | } |
| 596 | video_set_drvdata(vd, t); |
| 597 | |
| 598 | /* Register it */ |
| 599 | err = video_register_device(vd, VFL_TYPE_VTX, -1); |
| 600 | if (err < 0) { |
| 601 | kfree(t); |
| 602 | kfree(vd); |
| 603 | return err; |
| 604 | } |
| 605 | t->client = client; |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | static int saa5249_remove(struct i2c_client *client) |
| 610 | { |
| 611 | struct video_device *vd = i2c_get_clientdata(client); |
| 612 | |
| 613 | video_unregister_device(vd); |
| 614 | kfree(video_get_drvdata(vd)); |
| 615 | kfree(vd); |
| 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | static const struct i2c_device_id saa5249_id[] = { |
| 620 | { "saa5249", 0 }, |
| 621 | { } |
| 622 | }; |
| 623 | MODULE_DEVICE_TABLE(i2c, saa5249_id); |
| 624 | |
| 625 | static struct v4l2_i2c_driver_data v4l2_i2c_data = { |
| 626 | .name = "saa5249", |
Hans Verkuil | 96af988 | 2008-09-07 08:31:38 -0300 | [diff] [blame] | 627 | .probe = saa5249_probe, |
| 628 | .remove = saa5249_remove, |
| 629 | .id_table = saa5249_id, |
| 630 | }; |