saa5246a.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. /*
  2. * Driver for the SAA5246A or SAA5281 Teletext (=Videotext) decoder chips from
  3. * Philips.
  4. *
  5. * Only capturing of Teletext pages is tested. The videotext chips also have a
  6. * TV output but my hardware doesn't use it. For this reason this driver does
  7. * not support changing any TV display settings.
  8. *
  9. * Copyright (C) 2004 Michael Geng <linux@MichaelGeng.de>
  10. *
  11. * Derived from
  12. *
  13. * saa5249 driver
  14. * Copyright (C) 1998 Richard Guenther
  15. * <richard.guenther@student.uni-tuebingen.de>
  16. *
  17. * with changes by
  18. * Alan Cox <Alan.Cox@linux.org>
  19. *
  20. * and
  21. *
  22. * vtx.c
  23. * Copyright (C) 1994-97 Martin Buck <martin-2.buck@student.uni-ulm.de>
  24. *
  25. * This program is free software; you can redistribute it and/or modify
  26. * it under the terms of the GNU General Public License as published by
  27. * the Free Software Foundation; either version 2 of the License, or
  28. * (at your option) any later version.
  29. *
  30. * This program is distributed in the hope that it will be useful,
  31. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33. * GNU General Public License for more details.
  34. *
  35. * You should have received a copy of the GNU General Public License
  36. * along with this program; if not, write to the Free Software
  37. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  38. * USA.
  39. */
  40. #include <linux/module.h>
  41. #include <linux/kernel.h>
  42. #include <linux/sched.h>
  43. #include <linux/mm.h>
  44. #include <linux/init.h>
  45. #include <linux/i2c.h>
  46. #include <linux/videotext.h>
  47. #include <linux/videodev.h>
  48. #include "saa5246a.h"
  49. MODULE_AUTHOR("Michael Geng <linux@MichaelGeng.de>");
  50. MODULE_DESCRIPTION("Philips SAA5246A, SAA5281 Teletext decoder driver");
  51. MODULE_LICENSE("GPL");
  52. struct saa5246a_device
  53. {
  54. u8 pgbuf[NUM_DAUS][VTX_VIRTUALSIZE];
  55. int is_searching[NUM_DAUS];
  56. struct i2c_client *client;
  57. struct semaphore lock;
  58. };
  59. static struct video_device saa_template; /* Declared near bottom */
  60. /* Addresses to scan */
  61. static unsigned short normal_i2c[] = { I2C_ADDRESS, I2C_CLIENT_END };
  62. I2C_CLIENT_INSMOD;
  63. static struct i2c_client client_template;
  64. static int saa5246a_attach(struct i2c_adapter *adap, int addr, int kind)
  65. {
  66. int pgbuf;
  67. int err;
  68. struct i2c_client *client;
  69. struct video_device *vd;
  70. struct saa5246a_device *t;
  71. printk(KERN_INFO "saa5246a: teletext chip found.\n");
  72. client=kmalloc(sizeof(*client), GFP_KERNEL);
  73. if(client==NULL)
  74. return -ENOMEM;
  75. client_template.adapter = adap;
  76. client_template.addr = addr;
  77. memcpy(client, &client_template, sizeof(*client));
  78. t = kzalloc(sizeof(*t), GFP_KERNEL);
  79. if(t==NULL)
  80. {
  81. kfree(client);
  82. return -ENOMEM;
  83. }
  84. strlcpy(client->name, IF_NAME, I2C_NAME_SIZE);
  85. init_MUTEX(&t->lock);
  86. /*
  87. * Now create a video4linux device
  88. */
  89. vd = video_device_alloc();
  90. if(vd==NULL)
  91. {
  92. kfree(t);
  93. kfree(client);
  94. return -ENOMEM;
  95. }
  96. i2c_set_clientdata(client, vd);
  97. memcpy(vd, &saa_template, sizeof(*vd));
  98. for (pgbuf = 0; pgbuf < NUM_DAUS; pgbuf++)
  99. {
  100. memset(t->pgbuf[pgbuf], ' ', sizeof(t->pgbuf[0]));
  101. t->is_searching[pgbuf] = FALSE;
  102. }
  103. vd->priv=t;
  104. /*
  105. * Register it
  106. */
  107. if((err=video_register_device(vd, VFL_TYPE_VTX,-1))<0)
  108. {
  109. kfree(t);
  110. kfree(client);
  111. video_device_release(vd);
  112. return err;
  113. }
  114. t->client = client;
  115. i2c_attach_client(client);
  116. return 0;
  117. }
  118. /*
  119. * We do most of the hard work when we become a device on the i2c.
  120. */
  121. static int saa5246a_probe(struct i2c_adapter *adap)
  122. {
  123. if (adap->class & I2C_CLASS_TV_ANALOG)
  124. return i2c_probe(adap, &addr_data, saa5246a_attach);
  125. return 0;
  126. }
  127. static int saa5246a_detach(struct i2c_client *client)
  128. {
  129. struct video_device *vd = i2c_get_clientdata(client);
  130. i2c_detach_client(client);
  131. video_unregister_device(vd);
  132. kfree(vd->priv);
  133. kfree(client);
  134. return 0;
  135. }
  136. /*
  137. * I2C interfaces
  138. */
  139. static struct i2c_driver i2c_driver_videotext =
  140. {
  141. .driver = {
  142. .name = IF_NAME, /* name */
  143. },
  144. .id = I2C_DRIVERID_SAA5249, /* in i2c.h */
  145. .attach_adapter = saa5246a_probe,
  146. .detach_client = saa5246a_detach,
  147. };
  148. static struct i2c_client client_template = {
  149. .driver = &i2c_driver_videotext,
  150. .name = "(unset)",
  151. };
  152. static int i2c_sendbuf(struct saa5246a_device *t, int reg, int count, u8 *data)
  153. {
  154. char buf[64];
  155. buf[0] = reg;
  156. memcpy(buf+1, data, count);
  157. if(i2c_master_send(t->client, buf, count+1)==count+1)
  158. return 0;
  159. return -1;
  160. }
  161. static int i2c_senddata(struct saa5246a_device *t, ...)
  162. {
  163. unsigned char buf[64];
  164. int v;
  165. int ct=0;
  166. va_list argp;
  167. va_start(argp,t);
  168. while((v=va_arg(argp,int))!=-1)
  169. buf[ct++]=v;
  170. return i2c_sendbuf(t, buf[0], ct-1, buf+1);
  171. }
  172. /* Get count number of bytes from I²C-device at address adr, store them in buf.
  173. * Start & stop handshaking is done by this routine, ack will be sent after the
  174. * last byte to inhibit further sending of data. If uaccess is TRUE, data is
  175. * written to user-space with put_user. Returns -1 if I²C-device didn't send
  176. * acknowledge, 0 otherwise
  177. */
  178. static int i2c_getdata(struct saa5246a_device *t, int count, u8 *buf)
  179. {
  180. if(i2c_master_recv(t->client, buf, count)!=count)
  181. return -1;
  182. return 0;
  183. }
  184. /* When a page is found then the not FOUND bit in one of the status registers
  185. * of the SAA5264A chip is cleared. Unfortunately this bit is not set
  186. * automatically when a new page is requested. Instead this function must be
  187. * called after a page has been requested.
  188. *
  189. * Return value: 0 if successful
  190. */
  191. static int saa5246a_clear_found_bit(struct saa5246a_device *t,
  192. unsigned char dau_no)
  193. {
  194. unsigned char row_25_column_8;
  195. if (i2c_senddata(t, SAA5246A_REGISTER_R8,
  196. dau_no |
  197. R8_DO_NOT_CLEAR_MEMORY,
  198. R9_CURSER_ROW_25,
  199. R10_CURSER_COLUMN_8,
  200. COMMAND_END) ||
  201. i2c_getdata(t, 1, &row_25_column_8))
  202. {
  203. return -EIO;
  204. }
  205. row_25_column_8 |= ROW25_COLUMN8_PAGE_NOT_FOUND;
  206. if (i2c_senddata(t, SAA5246A_REGISTER_R8,
  207. dau_no |
  208. R8_DO_NOT_CLEAR_MEMORY,
  209. R9_CURSER_ROW_25,
  210. R10_CURSER_COLUMN_8,
  211. row_25_column_8,
  212. COMMAND_END))
  213. {
  214. return -EIO;
  215. }
  216. return 0;
  217. }
  218. /* Requests one videotext page as described in req. The fields of req are
  219. * checked and an error is returned if something is invalid.
  220. *
  221. * Return value: 0 if successful
  222. */
  223. static int saa5246a_request_page(struct saa5246a_device *t,
  224. vtx_pagereq_t *req)
  225. {
  226. if (req->pagemask < 0 || req->pagemask >= PGMASK_MAX)
  227. return -EINVAL;
  228. if (req->pagemask & PGMASK_PAGE)
  229. if (req->page < 0 || req->page > PAGE_MAX)
  230. return -EINVAL;
  231. if (req->pagemask & PGMASK_HOUR)
  232. if (req->hour < 0 || req->hour > HOUR_MAX)
  233. return -EINVAL;
  234. if (req->pagemask & PGMASK_MINUTE)
  235. if (req->minute < 0 || req->minute > MINUTE_MAX)
  236. return -EINVAL;
  237. if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
  238. return -EINVAL;
  239. if (i2c_senddata(t, SAA5246A_REGISTER_R2,
  240. R2_IN_R3_SELECT_PAGE_HUNDREDS |
  241. req->pgbuf << 4 |
  242. R2_BANK_0 |
  243. R2_HAMMING_CHECK_OFF,
  244. HUNDREDS_OF_PAGE(req->page) |
  245. R3_HOLD_PAGE |
  246. (req->pagemask & PG_HUND ?
  247. R3_PAGE_HUNDREDS_DO_CARE :
  248. R3_PAGE_HUNDREDS_DO_NOT_CARE),
  249. TENS_OF_PAGE(req->page) |
  250. (req->pagemask & PG_TEN ?
  251. R3_PAGE_TENS_DO_CARE :
  252. R3_PAGE_TENS_DO_NOT_CARE),
  253. UNITS_OF_PAGE(req->page) |
  254. (req->pagemask & PG_UNIT ?
  255. R3_PAGE_UNITS_DO_CARE :
  256. R3_PAGE_UNITS_DO_NOT_CARE),
  257. TENS_OF_HOUR(req->hour) |
  258. (req->pagemask & HR_TEN ?
  259. R3_HOURS_TENS_DO_CARE :
  260. R3_HOURS_TENS_DO_NOT_CARE),
  261. UNITS_OF_HOUR(req->hour) |
  262. (req->pagemask & HR_UNIT ?
  263. R3_HOURS_UNITS_DO_CARE :
  264. R3_HOURS_UNITS_DO_NOT_CARE),
  265. TENS_OF_MINUTE(req->minute) |
  266. (req->pagemask & MIN_TEN ?
  267. R3_MINUTES_TENS_DO_CARE :
  268. R3_MINUTES_TENS_DO_NOT_CARE),
  269. UNITS_OF_MINUTE(req->minute) |
  270. (req->pagemask & MIN_UNIT ?
  271. R3_MINUTES_UNITS_DO_CARE :
  272. R3_MINUTES_UNITS_DO_NOT_CARE),
  273. COMMAND_END) || i2c_senddata(t, SAA5246A_REGISTER_R2,
  274. R2_IN_R3_SELECT_PAGE_HUNDREDS |
  275. req->pgbuf << 4 |
  276. R2_BANK_0 |
  277. R2_HAMMING_CHECK_OFF,
  278. HUNDREDS_OF_PAGE(req->page) |
  279. R3_UPDATE_PAGE |
  280. (req->pagemask & PG_HUND ?
  281. R3_PAGE_HUNDREDS_DO_CARE :
  282. R3_PAGE_HUNDREDS_DO_NOT_CARE),
  283. COMMAND_END))
  284. {
  285. return -EIO;
  286. }
  287. t->is_searching[req->pgbuf] = TRUE;
  288. return 0;
  289. }
  290. /* This routine decodes the page number from the infobits contained in line 25.
  291. *
  292. * Parameters:
  293. * infobits: must be bits 0 to 9 of column 25
  294. *
  295. * Return value: page number coded in hexadecimal, i. e. page 123 is coded 0x123
  296. */
  297. static inline int saa5246a_extract_pagenum_from_infobits(
  298. unsigned char infobits[10])
  299. {
  300. int page_hundreds, page_tens, page_units;
  301. page_units = infobits[0] & ROW25_COLUMN0_PAGE_UNITS;
  302. page_tens = infobits[1] & ROW25_COLUMN1_PAGE_TENS;
  303. page_hundreds = infobits[8] & ROW25_COLUMN8_PAGE_HUNDREDS;
  304. /* page 0x.. means page 8.. */
  305. if (page_hundreds == 0)
  306. page_hundreds = 8;
  307. return((page_hundreds << 8) | (page_tens << 4) | page_units);
  308. }
  309. /* Decodes the hour from the infobits contained in line 25.
  310. *
  311. * Parameters:
  312. * infobits: must be bits 0 to 9 of column 25
  313. *
  314. * Return: hour coded in hexadecimal, i. e. 12h is coded 0x12
  315. */
  316. static inline int saa5246a_extract_hour_from_infobits(
  317. unsigned char infobits[10])
  318. {
  319. int hour_tens, hour_units;
  320. hour_units = infobits[4] & ROW25_COLUMN4_HOUR_UNITS;
  321. hour_tens = infobits[5] & ROW25_COLUMN5_HOUR_TENS;
  322. return((hour_tens << 4) | hour_units);
  323. }
  324. /* Decodes the minutes from the infobits contained in line 25.
  325. *
  326. * Parameters:
  327. * infobits: must be bits 0 to 9 of column 25
  328. *
  329. * Return: minutes coded in hexadecimal, i. e. 10min is coded 0x10
  330. */
  331. static inline int saa5246a_extract_minutes_from_infobits(
  332. unsigned char infobits[10])
  333. {
  334. int minutes_tens, minutes_units;
  335. minutes_units = infobits[2] & ROW25_COLUMN2_MINUTES_UNITS;
  336. minutes_tens = infobits[3] & ROW25_COLUMN3_MINUTES_TENS;
  337. return((minutes_tens << 4) | minutes_units);
  338. }
  339. /* Reads the status bits contained in the first 10 columns of the first line
  340. * and extracts the information into info.
  341. *
  342. * Return value: 0 if successful
  343. */
  344. static inline int saa5246a_get_status(struct saa5246a_device *t,
  345. vtx_pageinfo_t *info, unsigned char dau_no)
  346. {
  347. unsigned char infobits[10];
  348. int column;
  349. if (dau_no >= NUM_DAUS)
  350. return -EINVAL;
  351. if (i2c_senddata(t, SAA5246A_REGISTER_R8,
  352. dau_no |
  353. R8_DO_NOT_CLEAR_MEMORY,
  354. R9_CURSER_ROW_25,
  355. R10_CURSER_COLUMN_0,
  356. COMMAND_END) ||
  357. i2c_getdata(t, 10, infobits))
  358. {
  359. return -EIO;
  360. }
  361. info->pagenum = saa5246a_extract_pagenum_from_infobits(infobits);
  362. info->hour = saa5246a_extract_hour_from_infobits(infobits);
  363. info->minute = saa5246a_extract_minutes_from_infobits(infobits);
  364. info->charset = ((infobits[7] & ROW25_COLUMN7_CHARACTER_SET) >> 1);
  365. info->delete = !!(infobits[3] & ROW25_COLUMN3_DELETE_PAGE);
  366. info->headline = !!(infobits[5] & ROW25_COLUMN5_INSERT_HEADLINE);
  367. info->subtitle = !!(infobits[5] & ROW25_COLUMN5_INSERT_SUBTITLE);
  368. info->supp_header = !!(infobits[6] & ROW25_COLUMN6_SUPPRESS_HEADER);
  369. info->update = !!(infobits[6] & ROW25_COLUMN6_UPDATE_PAGE);
  370. info->inter_seq = !!(infobits[6] & ROW25_COLUMN6_INTERRUPTED_SEQUENCE);
  371. info->dis_disp = !!(infobits[6] & ROW25_COLUMN6_SUPPRESS_DISPLAY);
  372. info->serial = !!(infobits[7] & ROW25_COLUMN7_SERIAL_MODE);
  373. info->notfound = !!(infobits[8] & ROW25_COLUMN8_PAGE_NOT_FOUND);
  374. info->pblf = !!(infobits[9] & ROW25_COLUMN9_PAGE_BEING_LOOKED_FOR);
  375. info->hamming = 0;
  376. for (column = 0; column <= 7; column++) {
  377. if (infobits[column] & ROW25_COLUMN0_TO_7_HAMMING_ERROR) {
  378. info->hamming = 1;
  379. break;
  380. }
  381. }
  382. if (!info->hamming && !info->notfound)
  383. t->is_searching[dau_no] = FALSE;
  384. return 0;
  385. }
  386. /* Reads 1 videotext page buffer of the SAA5246A.
  387. *
  388. * req is used both as input and as output. It contains information which part
  389. * must be read. The videotext page is copied into req->buffer.
  390. *
  391. * Return value: 0 if successful
  392. */
  393. static inline int saa5246a_get_page(struct saa5246a_device *t,
  394. vtx_pagereq_t *req)
  395. {
  396. int start, end, size;
  397. char *buf;
  398. int err;
  399. if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS ||
  400. req->start < 0 || req->start > req->end || req->end >= VTX_PAGESIZE)
  401. return -EINVAL;
  402. buf = kmalloc(VTX_PAGESIZE, GFP_KERNEL);
  403. if (!buf)
  404. return -ENOMEM;
  405. /* Read "normal" part of page */
  406. err = -EIO;
  407. end = min(req->end, VTX_PAGESIZE - 1);
  408. if (i2c_senddata(t, SAA5246A_REGISTER_R8,
  409. req->pgbuf | R8_DO_NOT_CLEAR_MEMORY,
  410. ROW(req->start), COLUMN(req->start), COMMAND_END))
  411. goto out;
  412. if (i2c_getdata(t, end - req->start + 1, buf))
  413. goto out;
  414. err = -EFAULT;
  415. if (copy_to_user(req->buffer, buf, end - req->start + 1))
  416. goto out;
  417. /* Always get the time from buffer 4, since this stupid SAA5246A only
  418. * updates the currently displayed buffer...
  419. */
  420. if (REQ_CONTAINS_TIME(req)) {
  421. start = max(req->start, POS_TIME_START);
  422. end = min(req->end, POS_TIME_END);
  423. size = end - start + 1;
  424. err = -EINVAL;
  425. if (size < 0)
  426. goto out;
  427. err = -EIO;
  428. if (i2c_senddata(t, SAA5246A_REGISTER_R8,
  429. R8_ACTIVE_CHAPTER_4 | R8_DO_NOT_CLEAR_MEMORY,
  430. R9_CURSER_ROW_0, start, COMMAND_END))
  431. goto out;
  432. if (i2c_getdata(t, size, buf))
  433. goto out;
  434. err = -EFAULT;
  435. if (copy_to_user(req->buffer + start - req->start, buf, size))
  436. goto out;
  437. }
  438. /* Insert the header from buffer 4 only, if acquisition circuit is still searching for a page */
  439. if (REQ_CONTAINS_HEADER(req) && t->is_searching[req->pgbuf]) {
  440. start = max(req->start, POS_HEADER_START);
  441. end = min(req->end, POS_HEADER_END);
  442. size = end - start + 1;
  443. err = -EINVAL;
  444. if (size < 0)
  445. goto out;
  446. err = -EIO;
  447. if (i2c_senddata(t, SAA5246A_REGISTER_R8,
  448. R8_ACTIVE_CHAPTER_4 | R8_DO_NOT_CLEAR_MEMORY,
  449. R9_CURSER_ROW_0, start, COMMAND_END))
  450. goto out;
  451. if (i2c_getdata(t, end - start + 1, buf))
  452. goto out;
  453. err = -EFAULT;
  454. if (copy_to_user(req->buffer + start - req->start, buf, size))
  455. goto out;
  456. }
  457. err = 0;
  458. out:
  459. kfree(buf);
  460. return err;
  461. }
  462. /* Stops the acquisition circuit given in dau_no. The page buffer associated
  463. * with this acquisition circuit will no more be updated. The other daus are
  464. * not affected.
  465. *
  466. * Return value: 0 if successful
  467. */
  468. static inline int saa5246a_stop_dau(struct saa5246a_device *t,
  469. unsigned char dau_no)
  470. {
  471. if (dau_no >= NUM_DAUS)
  472. return -EINVAL;
  473. if (i2c_senddata(t, SAA5246A_REGISTER_R2,
  474. R2_IN_R3_SELECT_PAGE_HUNDREDS |
  475. dau_no << 4 |
  476. R2_BANK_0 |
  477. R2_HAMMING_CHECK_OFF,
  478. R3_PAGE_HUNDREDS_0 |
  479. R3_HOLD_PAGE |
  480. R3_PAGE_HUNDREDS_DO_NOT_CARE,
  481. COMMAND_END))
  482. {
  483. return -EIO;
  484. }
  485. t->is_searching[dau_no] = FALSE;
  486. return 0;
  487. }
  488. /* Handles ioctls defined in videotext.h
  489. *
  490. * Returns 0 if successful
  491. */
  492. static int do_saa5246a_ioctl(struct inode *inode, struct file *file,
  493. unsigned int cmd, void *arg)
  494. {
  495. struct video_device *vd = video_devdata(file);
  496. struct saa5246a_device *t=vd->priv;
  497. switch(cmd)
  498. {
  499. case VTXIOCGETINFO:
  500. {
  501. vtx_info_t *info = arg;
  502. info->version_major = MAJOR_VERSION;
  503. info->version_minor = MINOR_VERSION;
  504. info->numpages = NUM_DAUS;
  505. return 0;
  506. }
  507. case VTXIOCCLRPAGE:
  508. {
  509. vtx_pagereq_t *req = arg;
  510. if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
  511. return -EINVAL;
  512. memset(t->pgbuf[req->pgbuf], ' ', sizeof(t->pgbuf[0]));
  513. return 0;
  514. }
  515. case VTXIOCCLRFOUND:
  516. {
  517. vtx_pagereq_t *req = arg;
  518. if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
  519. return -EINVAL;
  520. return(saa5246a_clear_found_bit(t, req->pgbuf));
  521. }
  522. case VTXIOCPAGEREQ:
  523. {
  524. vtx_pagereq_t *req = arg;
  525. return(saa5246a_request_page(t, req));
  526. }
  527. case VTXIOCGETSTAT:
  528. {
  529. vtx_pagereq_t *req = arg;
  530. vtx_pageinfo_t info;
  531. int rval;
  532. if ((rval = saa5246a_get_status(t, &info, req->pgbuf)))
  533. return rval;
  534. if(copy_to_user(req->buffer, &info,
  535. sizeof(vtx_pageinfo_t)))
  536. return -EFAULT;
  537. return 0;
  538. }
  539. case VTXIOCGETPAGE:
  540. {
  541. vtx_pagereq_t *req = arg;
  542. return(saa5246a_get_page(t, req));
  543. }
  544. case VTXIOCSTOPDAU:
  545. {
  546. vtx_pagereq_t *req = arg;
  547. return(saa5246a_stop_dau(t, req->pgbuf));
  548. }
  549. case VTXIOCPUTPAGE:
  550. case VTXIOCSETDISP:
  551. case VTXIOCPUTSTAT:
  552. return 0;
  553. case VTXIOCCLRCACHE:
  554. {
  555. return 0;
  556. }
  557. case VTXIOCSETVIRT:
  558. {
  559. /* I do not know what "virtual mode" means */
  560. return 0;
  561. }
  562. }
  563. return -EINVAL;
  564. }
  565. /*
  566. * Translates old vtx IOCTLs to new ones
  567. *
  568. * This keeps new kernel versions compatible with old userspace programs.
  569. */
  570. static inline unsigned int vtx_fix_command(unsigned int cmd)
  571. {
  572. switch (cmd) {
  573. case VTXIOCGETINFO_OLD:
  574. cmd = VTXIOCGETINFO;
  575. break;
  576. case VTXIOCCLRPAGE_OLD:
  577. cmd = VTXIOCCLRPAGE;
  578. break;
  579. case VTXIOCCLRFOUND_OLD:
  580. cmd = VTXIOCCLRFOUND;
  581. break;
  582. case VTXIOCPAGEREQ_OLD:
  583. cmd = VTXIOCPAGEREQ;
  584. break;
  585. case VTXIOCGETSTAT_OLD:
  586. cmd = VTXIOCGETSTAT;
  587. break;
  588. case VTXIOCGETPAGE_OLD:
  589. cmd = VTXIOCGETPAGE;
  590. break;
  591. case VTXIOCSTOPDAU_OLD:
  592. cmd = VTXIOCSTOPDAU;
  593. break;
  594. case VTXIOCPUTPAGE_OLD:
  595. cmd = VTXIOCPUTPAGE;
  596. break;
  597. case VTXIOCSETDISP_OLD:
  598. cmd = VTXIOCSETDISP;
  599. break;
  600. case VTXIOCPUTSTAT_OLD:
  601. cmd = VTXIOCPUTSTAT;
  602. break;
  603. case VTXIOCCLRCACHE_OLD:
  604. cmd = VTXIOCCLRCACHE;
  605. break;
  606. case VTXIOCSETVIRT_OLD:
  607. cmd = VTXIOCSETVIRT;
  608. break;
  609. }
  610. return cmd;
  611. }
  612. /*
  613. * Handle the locking
  614. */
  615. static int saa5246a_ioctl(struct inode *inode, struct file *file,
  616. unsigned int cmd, unsigned long arg)
  617. {
  618. struct video_device *vd = video_devdata(file);
  619. struct saa5246a_device *t = vd->priv;
  620. int err;
  621. cmd = vtx_fix_command(cmd);
  622. down(&t->lock);
  623. err = video_usercopy(inode, file, cmd, arg, do_saa5246a_ioctl);
  624. up(&t->lock);
  625. return err;
  626. }
  627. static int saa5246a_open(struct inode *inode, struct file *file)
  628. {
  629. struct video_device *vd = video_devdata(file);
  630. struct saa5246a_device *t = vd->priv;
  631. int err;
  632. err = video_exclusive_open(inode,file);
  633. if (err < 0)
  634. return err;
  635. if (t->client==NULL) {
  636. err = -ENODEV;
  637. goto fail;
  638. }
  639. if (i2c_senddata(t, SAA5246A_REGISTER_R0,
  640. R0_SELECT_R11 |
  641. R0_PLL_TIME_CONSTANT_LONG |
  642. R0_ENABLE_nODD_EVEN_OUTPUT |
  643. R0_ENABLE_HDR_POLL |
  644. R0_DO_NOT_FORCE_nODD_EVEN_LOW_IF_PICTURE_DISPLAYED |
  645. R0_NO_FREE_RUN_PLL |
  646. R0_NO_AUTOMATIC_FASTEXT_PROMPT,
  647. R1_NON_INTERLACED_312_312_LINES |
  648. R1_DEW |
  649. R1_EXTENDED_PACKET_DISABLE |
  650. R1_DAUS_ALL_ON |
  651. R1_8_BITS_NO_PARITY |
  652. R1_VCS_TO_SCS,
  653. COMMAND_END) ||
  654. i2c_senddata(t, SAA5246A_REGISTER_R4,
  655. /* We do not care much for the TV display but nevertheless we
  656. * need the currently displayed page later because only on that
  657. * page the time is updated. */
  658. R4_DISPLAY_PAGE_4,
  659. COMMAND_END))
  660. {
  661. err = -EIO;
  662. goto fail;
  663. }
  664. return 0;
  665. fail:
  666. video_exclusive_release(inode,file);
  667. return err;
  668. }
  669. static int saa5246a_release(struct inode *inode, struct file *file)
  670. {
  671. struct video_device *vd = video_devdata(file);
  672. struct saa5246a_device *t = vd->priv;
  673. /* Stop all acquisition circuits. */
  674. i2c_senddata(t, SAA5246A_REGISTER_R1,
  675. R1_INTERLACED_312_AND_HALF_312_AND_HALF_LINES |
  676. R1_DEW |
  677. R1_EXTENDED_PACKET_DISABLE |
  678. R1_DAUS_ALL_OFF |
  679. R1_8_BITS_NO_PARITY |
  680. R1_VCS_TO_SCS,
  681. COMMAND_END);
  682. video_exclusive_release(inode,file);
  683. return 0;
  684. }
  685. static int __init init_saa_5246a (void)
  686. {
  687. printk(KERN_INFO
  688. "SAA5246A (or compatible) Teletext decoder driver version %d.%d\n",
  689. MAJOR_VERSION, MINOR_VERSION);
  690. return i2c_add_driver(&i2c_driver_videotext);
  691. }
  692. static void __exit cleanup_saa_5246a (void)
  693. {
  694. i2c_del_driver(&i2c_driver_videotext);
  695. }
  696. module_init(init_saa_5246a);
  697. module_exit(cleanup_saa_5246a);
  698. static struct file_operations saa_fops = {
  699. .owner = THIS_MODULE,
  700. .open = saa5246a_open,
  701. .release = saa5246a_release,
  702. .ioctl = saa5246a_ioctl,
  703. .llseek = no_llseek,
  704. };
  705. static struct video_device saa_template =
  706. {
  707. .owner = THIS_MODULE,
  708. .name = IF_NAME,
  709. .type = VID_TYPE_TELETEXT,
  710. .hardware = VID_HARDWARE_SAA5249,
  711. .fops = &saa_fops,
  712. .release = video_device_release,
  713. .minor = -1,
  714. };