saa5249.c 18 KB

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