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