saa5249.c 17 KB

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