saa5246a.c 19 KB

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