saa5249.c 19 KB

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