saa5246a.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  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@lxorguk.ukuu.org.uk>
  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/smp_lock.h>
  46. #include <linux/mutex.h>
  47. #include <linux/videotext.h>
  48. #include <linux/videodev.h>
  49. #include <media/v4l2-common.h>
  50. #include <media/v4l2-ioctl.h>
  51. #include <media/v4l2-i2c-drv-legacy.h>
  52. MODULE_AUTHOR("Michael Geng <linux@MichaelGeng.de>");
  53. MODULE_DESCRIPTION("Philips SAA5246A, SAA5281 Teletext decoder driver");
  54. MODULE_LICENSE("GPL");
  55. #define MAJOR_VERSION 1 /* driver major version number */
  56. #define MINOR_VERSION 8 /* driver minor version number */
  57. /* Number of DAUs = number of pages that can be searched at the same time. */
  58. #define NUM_DAUS 4
  59. #define NUM_ROWS_PER_PAGE 40
  60. /* first column is 0 (not 1) */
  61. #define POS_TIME_START 32
  62. #define POS_TIME_END 39
  63. #define POS_HEADER_START 7
  64. #define POS_HEADER_END 31
  65. /* Returns 'true' if the part of the videotext page described with req contains
  66. (at least parts of) the time field */
  67. #define REQ_CONTAINS_TIME(p_req) \
  68. ((p_req)->start <= POS_TIME_END && \
  69. (p_req)->end >= POS_TIME_START)
  70. /* Returns 'true' if the part of the videotext page described with req contains
  71. (at least parts of) the page header */
  72. #define REQ_CONTAINS_HEADER(p_req) \
  73. ((p_req)->start <= POS_HEADER_END && \
  74. (p_req)->end >= POS_HEADER_START)
  75. /*****************************************************************************/
  76. /* Mode register numbers of the SAA5246A */
  77. /*****************************************************************************/
  78. #define SAA5246A_REGISTER_R0 0
  79. #define SAA5246A_REGISTER_R1 1
  80. #define SAA5246A_REGISTER_R2 2
  81. #define SAA5246A_REGISTER_R3 3
  82. #define SAA5246A_REGISTER_R4 4
  83. #define SAA5246A_REGISTER_R5 5
  84. #define SAA5246A_REGISTER_R6 6
  85. #define SAA5246A_REGISTER_R7 7
  86. #define SAA5246A_REGISTER_R8 8
  87. #define SAA5246A_REGISTER_R9 9
  88. #define SAA5246A_REGISTER_R10 10
  89. #define SAA5246A_REGISTER_R11 11
  90. #define SAA5246A_REGISTER_R11B 11
  91. /* SAA5246A mode registers often autoincrement to the next register.
  92. Therefore we use variable argument lists. The following macro indicates
  93. the end of a command list. */
  94. #define COMMAND_END (-1)
  95. /*****************************************************************************/
  96. /* Contents of the mode registers of the SAA5246A */
  97. /*****************************************************************************/
  98. /* Register R0 (Advanced Control) */
  99. #define R0_SELECT_R11 0x00
  100. #define R0_SELECT_R11B 0x01
  101. #define R0_PLL_TIME_CONSTANT_LONG 0x00
  102. #define R0_PLL_TIME_CONSTANT_SHORT 0x02
  103. #define R0_ENABLE_nODD_EVEN_OUTPUT 0x00
  104. #define R0_DISABLE_nODD_EVEN_OUTPUT 0x04
  105. #define R0_ENABLE_HDR_POLL 0x00
  106. #define R0_DISABLE_HDR_POLL 0x10
  107. #define R0_DO_NOT_FORCE_nODD_EVEN_LOW_IF_PICTURE_DISPLAYED 0x00
  108. #define R0_FORCE_nODD_EVEN_LOW_IF_PICTURE_DISPLAYED 0x20
  109. #define R0_NO_FREE_RUN_PLL 0x00
  110. #define R0_FREE_RUN_PLL 0x40
  111. #define R0_NO_AUTOMATIC_FASTEXT_PROMPT 0x00
  112. #define R0_AUTOMATIC_FASTEXT_PROMPT 0x80
  113. /* Register R1 (Mode) */
  114. #define R1_INTERLACED_312_AND_HALF_312_AND_HALF_LINES 0x00
  115. #define R1_NON_INTERLACED_312_313_LINES 0x01
  116. #define R1_NON_INTERLACED_312_312_LINES 0x02
  117. #define R1_FFB_LEADING_EDGE_IN_FIRST_BROAD_PULSE 0x03
  118. #define R1_FFB_LEADING_EDGE_IN_SECOND_BROAD_PULSE 0x07
  119. #define R1_DEW 0x00
  120. #define R1_FULL_FIELD 0x08
  121. #define R1_EXTENDED_PACKET_DISABLE 0x00
  122. #define R1_EXTENDED_PACKET_ENABLE 0x10
  123. #define R1_DAUS_ALL_ON 0x00
  124. #define R1_DAUS_ALL_OFF 0x20
  125. #define R1_7_BITS_PLUS_PARITY 0x00
  126. #define R1_8_BITS_NO_PARITY 0x40
  127. #define R1_VCS_TO_SCS 0x00
  128. #define R1_NO_VCS_TO_SCS 0x80
  129. /* Register R2 (Page request address) */
  130. #define R2_IN_R3_SELECT_PAGE_HUNDREDS 0x00
  131. #define R2_IN_R3_SELECT_PAGE_TENS 0x01
  132. #define R2_IN_R3_SELECT_PAGE_UNITS 0x02
  133. #define R2_IN_R3_SELECT_HOURS_TENS 0x03
  134. #define R2_IN_R3_SELECT_HOURS_UNITS 0x04
  135. #define R2_IN_R3_SELECT_MINUTES_TENS 0x05
  136. #define R2_IN_R3_SELECT_MINUTES_UNITS 0x06
  137. #define R2_DAU_0 0x00
  138. #define R2_DAU_1 0x10
  139. #define R2_DAU_2 0x20
  140. #define R2_DAU_3 0x30
  141. #define R2_BANK_0 0x00
  142. #define R2_BANK 1 0x40
  143. #define R2_HAMMING_CHECK_ON 0x80
  144. #define R2_HAMMING_CHECK_OFF 0x00
  145. /* Register R3 (Page request data) */
  146. #define R3_PAGE_HUNDREDS_0 0x00
  147. #define R3_PAGE_HUNDREDS_1 0x01
  148. #define R3_PAGE_HUNDREDS_2 0x02
  149. #define R3_PAGE_HUNDREDS_3 0x03
  150. #define R3_PAGE_HUNDREDS_4 0x04
  151. #define R3_PAGE_HUNDREDS_5 0x05
  152. #define R3_PAGE_HUNDREDS_6 0x06
  153. #define R3_PAGE_HUNDREDS_7 0x07
  154. #define R3_HOLD_PAGE 0x00
  155. #define R3_UPDATE_PAGE 0x08
  156. #define R3_PAGE_HUNDREDS_DO_NOT_CARE 0x00
  157. #define R3_PAGE_HUNDREDS_DO_CARE 0x10
  158. #define R3_PAGE_TENS_DO_NOT_CARE 0x00
  159. #define R3_PAGE_TENS_DO_CARE 0x10
  160. #define R3_PAGE_UNITS_DO_NOT_CARE 0x00
  161. #define R3_PAGE_UNITS_DO_CARE 0x10
  162. #define R3_HOURS_TENS_DO_NOT_CARE 0x00
  163. #define R3_HOURS_TENS_DO_CARE 0x10
  164. #define R3_HOURS_UNITS_DO_NOT_CARE 0x00
  165. #define R3_HOURS_UNITS_DO_CARE 0x10
  166. #define R3_MINUTES_TENS_DO_NOT_CARE 0x00
  167. #define R3_MINUTES_TENS_DO_CARE 0x10
  168. #define R3_MINUTES_UNITS_DO_NOT_CARE 0x00
  169. #define R3_MINUTES_UNITS_DO_CARE 0x10
  170. /* Register R4 (Display chapter) */
  171. #define R4_DISPLAY_PAGE_0 0x00
  172. #define R4_DISPLAY_PAGE_1 0x01
  173. #define R4_DISPLAY_PAGE_2 0x02
  174. #define R4_DISPLAY_PAGE_3 0x03
  175. #define R4_DISPLAY_PAGE_4 0x04
  176. #define R4_DISPLAY_PAGE_5 0x05
  177. #define R4_DISPLAY_PAGE_6 0x06
  178. #define R4_DISPLAY_PAGE_7 0x07
  179. /* Register R5 (Normal display control) */
  180. #define R5_PICTURE_INSIDE_BOXING_OFF 0x00
  181. #define R5_PICTURE_INSIDE_BOXING_ON 0x01
  182. #define R5_PICTURE_OUTSIDE_BOXING_OFF 0x00
  183. #define R5_PICTURE_OUTSIDE_BOXING_ON 0x02
  184. #define R5_TEXT_INSIDE_BOXING_OFF 0x00
  185. #define R5_TEXT_INSIDE_BOXING_ON 0x04
  186. #define R5_TEXT_OUTSIDE_BOXING_OFF 0x00
  187. #define R5_TEXT_OUTSIDE_BOXING_ON 0x08
  188. #define R5_CONTRAST_REDUCTION_INSIDE_BOXING_OFF 0x00
  189. #define R5_CONTRAST_REDUCTION_INSIDE_BOXING_ON 0x10
  190. #define R5_CONTRAST_REDUCTION_OUTSIDE_BOXING_OFF 0x00
  191. #define R5_CONTRAST_REDUCTION_OUTSIDE_BOXING_ON 0x20
  192. #define R5_BACKGROUND_COLOR_INSIDE_BOXING_OFF 0x00
  193. #define R5_BACKGROUND_COLOR_INSIDE_BOXING_ON 0x40
  194. #define R5_BACKGROUND_COLOR_OUTSIDE_BOXING_OFF 0x00
  195. #define R5_BACKGROUND_COLOR_OUTSIDE_BOXING_ON 0x80
  196. /* Register R6 (Newsflash display) */
  197. #define R6_NEWSFLASH_PICTURE_INSIDE_BOXING_OFF 0x00
  198. #define R6_NEWSFLASH_PICTURE_INSIDE_BOXING_ON 0x01
  199. #define R6_NEWSFLASH_PICTURE_OUTSIDE_BOXING_OFF 0x00
  200. #define R6_NEWSFLASH_PICTURE_OUTSIDE_BOXING_ON 0x02
  201. #define R6_NEWSFLASH_TEXT_INSIDE_BOXING_OFF 0x00
  202. #define R6_NEWSFLASH_TEXT_INSIDE_BOXING_ON 0x04
  203. #define R6_NEWSFLASH_TEXT_OUTSIDE_BOXING_OFF 0x00
  204. #define R6_NEWSFLASH_TEXT_OUTSIDE_BOXING_ON 0x08
  205. #define R6_NEWSFLASH_CONTRAST_REDUCTION_INSIDE_BOXING_OFF 0x00
  206. #define R6_NEWSFLASH_CONTRAST_REDUCTION_INSIDE_BOXING_ON 0x10
  207. #define R6_NEWSFLASH_CONTRAST_REDUCTION_OUTSIDE_BOXING_OFF 0x00
  208. #define R6_NEWSFLASH_CONTRAST_REDUCTION_OUTSIDE_BOXING_ON 0x20
  209. #define R6_NEWSFLASH_BACKGROUND_COLOR_INSIDE_BOXING_OFF 0x00
  210. #define R6_NEWSFLASH_BACKGROUND_COLOR_INSIDE_BOXING_ON 0x40
  211. #define R6_NEWSFLASH_BACKGROUND_COLOR_OUTSIDE_BOXING_OFF 0x00
  212. #define R6_NEWSFLASH_BACKGROUND_COLOR_OUTSIDE_BOXING_ON 0x80
  213. /* Register R7 (Display mode) */
  214. #define R7_BOX_OFF_ROW_0 0x00
  215. #define R7_BOX_ON_ROW_0 0x01
  216. #define R7_BOX_OFF_ROW_1_TO_23 0x00
  217. #define R7_BOX_ON_ROW_1_TO_23 0x02
  218. #define R7_BOX_OFF_ROW_24 0x00
  219. #define R7_BOX_ON_ROW_24 0x04
  220. #define R7_SINGLE_HEIGHT 0x00
  221. #define R7_DOUBLE_HEIGHT 0x08
  222. #define R7_TOP_HALF 0x00
  223. #define R7_BOTTOM_HALF 0x10
  224. #define R7_REVEAL_OFF 0x00
  225. #define R7_REVEAL_ON 0x20
  226. #define R7_CURSER_OFF 0x00
  227. #define R7_CURSER_ON 0x40
  228. #define R7_STATUS_BOTTOM 0x00
  229. #define R7_STATUS_TOP 0x80
  230. /* Register R8 (Active chapter) */
  231. #define R8_ACTIVE_CHAPTER_0 0x00
  232. #define R8_ACTIVE_CHAPTER_1 0x01
  233. #define R8_ACTIVE_CHAPTER_2 0x02
  234. #define R8_ACTIVE_CHAPTER_3 0x03
  235. #define R8_ACTIVE_CHAPTER_4 0x04
  236. #define R8_ACTIVE_CHAPTER_5 0x05
  237. #define R8_ACTIVE_CHAPTER_6 0x06
  238. #define R8_ACTIVE_CHAPTER_7 0x07
  239. #define R8_CLEAR_MEMORY 0x08
  240. #define R8_DO_NOT_CLEAR_MEMORY 0x00
  241. /* Register R9 (Curser row) */
  242. #define R9_CURSER_ROW_0 0x00
  243. #define R9_CURSER_ROW_1 0x01
  244. #define R9_CURSER_ROW_2 0x02
  245. #define R9_CURSER_ROW_25 0x19
  246. /* Register R10 (Curser column) */
  247. #define R10_CURSER_COLUMN_0 0x00
  248. #define R10_CURSER_COLUMN_6 0x06
  249. #define R10_CURSER_COLUMN_8 0x08
  250. /*****************************************************************************/
  251. /* Row 25 control data in column 0 to 9 */
  252. /*****************************************************************************/
  253. #define ROW25_COLUMN0_PAGE_UNITS 0x0F
  254. #define ROW25_COLUMN1_PAGE_TENS 0x0F
  255. #define ROW25_COLUMN2_MINUTES_UNITS 0x0F
  256. #define ROW25_COLUMN3_MINUTES_TENS 0x07
  257. #define ROW25_COLUMN3_DELETE_PAGE 0x08
  258. #define ROW25_COLUMN4_HOUR_UNITS 0x0F
  259. #define ROW25_COLUMN5_HOUR_TENS 0x03
  260. #define ROW25_COLUMN5_INSERT_HEADLINE 0x04
  261. #define ROW25_COLUMN5_INSERT_SUBTITLE 0x08
  262. #define ROW25_COLUMN6_SUPPRESS_HEADER 0x01
  263. #define ROW25_COLUMN6_UPDATE_PAGE 0x02
  264. #define ROW25_COLUMN6_INTERRUPTED_SEQUENCE 0x04
  265. #define ROW25_COLUMN6_SUPPRESS_DISPLAY 0x08
  266. #define ROW25_COLUMN7_SERIAL_MODE 0x01
  267. #define ROW25_COLUMN7_CHARACTER_SET 0x0E
  268. #define ROW25_COLUMN8_PAGE_HUNDREDS 0x07
  269. #define ROW25_COLUMN8_PAGE_NOT_FOUND 0x10
  270. #define ROW25_COLUMN9_PAGE_BEING_LOOKED_FOR 0x20
  271. #define ROW25_COLUMN0_TO_7_HAMMING_ERROR 0x10
  272. /*****************************************************************************/
  273. /* Helper macros for extracting page, hour and minute digits */
  274. /*****************************************************************************/
  275. /* BYTE_POS 0 is at row 0, column 0,
  276. BYTE_POS 1 is at row 0, column 1,
  277. BYTE_POS 40 is at row 1, column 0, (with NUM_ROWS_PER_PAGE = 40)
  278. BYTE_POS 41 is at row 1, column 1, (with NUM_ROWS_PER_PAGE = 40),
  279. ... */
  280. #define ROW(BYTE_POS) (BYTE_POS / NUM_ROWS_PER_PAGE)
  281. #define COLUMN(BYTE_POS) (BYTE_POS % NUM_ROWS_PER_PAGE)
  282. /*****************************************************************************/
  283. /* Helper macros for extracting page, hour and minute digits */
  284. /*****************************************************************************/
  285. /* Macros for extracting hundreds, tens and units of a page number which
  286. must be in the range 0 ... 0x799.
  287. Note that page is coded in hexadecimal, i.e. 0x123 means page 123.
  288. page 0x.. means page 8.. */
  289. #define HUNDREDS_OF_PAGE(page) (((page) / 0x100) & 0x7)
  290. #define TENS_OF_PAGE(page) (((page) / 0x10) & 0xF)
  291. #define UNITS_OF_PAGE(page) ((page) & 0xF)
  292. /* Macros for extracting tens and units of a hour information which
  293. must be in the range 0 ... 0x24.
  294. Note that hour is coded in hexadecimal, i.e. 0x12 means 12 hours */
  295. #define TENS_OF_HOUR(hour) ((hour) / 0x10)
  296. #define UNITS_OF_HOUR(hour) ((hour) & 0xF)
  297. /* Macros for extracting tens and units of a minute information which
  298. must be in the range 0 ... 0x59.
  299. Note that minute is coded in hexadecimal, i.e. 0x12 means 12 minutes */
  300. #define TENS_OF_MINUTE(minute) ((minute) / 0x10)
  301. #define UNITS_OF_MINUTE(minute) ((minute) & 0xF)
  302. #define HOUR_MAX 0x23
  303. #define MINUTE_MAX 0x59
  304. #define PAGE_MAX 0x8FF
  305. struct saa5246a_device
  306. {
  307. u8 pgbuf[NUM_DAUS][VTX_VIRTUALSIZE];
  308. int is_searching[NUM_DAUS];
  309. struct i2c_client *client;
  310. unsigned long in_use;
  311. struct mutex lock;
  312. };
  313. static struct video_device saa_template; /* Declared near bottom */
  314. /*
  315. * I2C interfaces
  316. */
  317. static int i2c_sendbuf(struct saa5246a_device *t, int reg, int count, u8 *data)
  318. {
  319. char buf[64];
  320. buf[0] = reg;
  321. memcpy(buf+1, data, count);
  322. if(i2c_master_send(t->client, buf, count+1)==count+1)
  323. return 0;
  324. return -1;
  325. }
  326. static int i2c_senddata(struct saa5246a_device *t, ...)
  327. {
  328. unsigned char buf[64];
  329. int v;
  330. int ct = 0;
  331. va_list argp;
  332. va_start(argp, t);
  333. while ((v = va_arg(argp, int)) != -1)
  334. buf[ct++] = v;
  335. va_end(argp);
  336. return i2c_sendbuf(t, buf[0], ct-1, buf+1);
  337. }
  338. /* Get count number of bytes from I²C-device at address adr, store them in buf.
  339. * Start & stop handshaking is done by this routine, ack will be sent after the
  340. * last byte to inhibit further sending of data. If uaccess is 'true', data is
  341. * written to user-space with put_user. Returns -1 if I²C-device didn't send
  342. * acknowledge, 0 otherwise
  343. */
  344. static int i2c_getdata(struct saa5246a_device *t, int count, u8 *buf)
  345. {
  346. if(i2c_master_recv(t->client, buf, count)!=count)
  347. return -1;
  348. return 0;
  349. }
  350. /* When a page is found then the not FOUND bit in one of the status registers
  351. * of the SAA5264A chip is cleared. Unfortunately this bit is not set
  352. * automatically when a new page is requested. Instead this function must be
  353. * called after a page has been requested.
  354. *
  355. * Return value: 0 if successful
  356. */
  357. static int saa5246a_clear_found_bit(struct saa5246a_device *t,
  358. unsigned char dau_no)
  359. {
  360. unsigned char row_25_column_8;
  361. if (i2c_senddata(t, SAA5246A_REGISTER_R8,
  362. dau_no |
  363. R8_DO_NOT_CLEAR_MEMORY,
  364. R9_CURSER_ROW_25,
  365. R10_CURSER_COLUMN_8,
  366. COMMAND_END) ||
  367. i2c_getdata(t, 1, &row_25_column_8))
  368. {
  369. return -EIO;
  370. }
  371. row_25_column_8 |= ROW25_COLUMN8_PAGE_NOT_FOUND;
  372. if (i2c_senddata(t, SAA5246A_REGISTER_R8,
  373. dau_no |
  374. R8_DO_NOT_CLEAR_MEMORY,
  375. R9_CURSER_ROW_25,
  376. R10_CURSER_COLUMN_8,
  377. row_25_column_8,
  378. COMMAND_END))
  379. {
  380. return -EIO;
  381. }
  382. return 0;
  383. }
  384. /* Requests one videotext page as described in req. The fields of req are
  385. * checked and an error is returned if something is invalid.
  386. *
  387. * Return value: 0 if successful
  388. */
  389. static int saa5246a_request_page(struct saa5246a_device *t,
  390. vtx_pagereq_t *req)
  391. {
  392. if (req->pagemask < 0 || req->pagemask >= PGMASK_MAX)
  393. return -EINVAL;
  394. if (req->pagemask & PGMASK_PAGE)
  395. if (req->page < 0 || req->page > PAGE_MAX)
  396. return -EINVAL;
  397. if (req->pagemask & PGMASK_HOUR)
  398. if (req->hour < 0 || req->hour > HOUR_MAX)
  399. return -EINVAL;
  400. if (req->pagemask & PGMASK_MINUTE)
  401. if (req->minute < 0 || req->minute > MINUTE_MAX)
  402. return -EINVAL;
  403. if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
  404. return -EINVAL;
  405. if (i2c_senddata(t, SAA5246A_REGISTER_R2,
  406. R2_IN_R3_SELECT_PAGE_HUNDREDS |
  407. req->pgbuf << 4 |
  408. R2_BANK_0 |
  409. R2_HAMMING_CHECK_OFF,
  410. HUNDREDS_OF_PAGE(req->page) |
  411. R3_HOLD_PAGE |
  412. (req->pagemask & PG_HUND ?
  413. R3_PAGE_HUNDREDS_DO_CARE :
  414. R3_PAGE_HUNDREDS_DO_NOT_CARE),
  415. TENS_OF_PAGE(req->page) |
  416. (req->pagemask & PG_TEN ?
  417. R3_PAGE_TENS_DO_CARE :
  418. R3_PAGE_TENS_DO_NOT_CARE),
  419. UNITS_OF_PAGE(req->page) |
  420. (req->pagemask & PG_UNIT ?
  421. R3_PAGE_UNITS_DO_CARE :
  422. R3_PAGE_UNITS_DO_NOT_CARE),
  423. TENS_OF_HOUR(req->hour) |
  424. (req->pagemask & HR_TEN ?
  425. R3_HOURS_TENS_DO_CARE :
  426. R3_HOURS_TENS_DO_NOT_CARE),
  427. UNITS_OF_HOUR(req->hour) |
  428. (req->pagemask & HR_UNIT ?
  429. R3_HOURS_UNITS_DO_CARE :
  430. R3_HOURS_UNITS_DO_NOT_CARE),
  431. TENS_OF_MINUTE(req->minute) |
  432. (req->pagemask & MIN_TEN ?
  433. R3_MINUTES_TENS_DO_CARE :
  434. R3_MINUTES_TENS_DO_NOT_CARE),
  435. UNITS_OF_MINUTE(req->minute) |
  436. (req->pagemask & MIN_UNIT ?
  437. R3_MINUTES_UNITS_DO_CARE :
  438. R3_MINUTES_UNITS_DO_NOT_CARE),
  439. COMMAND_END) || i2c_senddata(t, SAA5246A_REGISTER_R2,
  440. R2_IN_R3_SELECT_PAGE_HUNDREDS |
  441. req->pgbuf << 4 |
  442. R2_BANK_0 |
  443. R2_HAMMING_CHECK_OFF,
  444. HUNDREDS_OF_PAGE(req->page) |
  445. R3_UPDATE_PAGE |
  446. (req->pagemask & PG_HUND ?
  447. R3_PAGE_HUNDREDS_DO_CARE :
  448. R3_PAGE_HUNDREDS_DO_NOT_CARE),
  449. COMMAND_END))
  450. {
  451. return -EIO;
  452. }
  453. t->is_searching[req->pgbuf] = true;
  454. return 0;
  455. }
  456. /* This routine decodes the page number from the infobits contained in line 25.
  457. *
  458. * Parameters:
  459. * infobits: must be bits 0 to 9 of column 25
  460. *
  461. * Return value: page number coded in hexadecimal, i. e. page 123 is coded 0x123
  462. */
  463. static inline int saa5246a_extract_pagenum_from_infobits(
  464. unsigned char infobits[10])
  465. {
  466. int page_hundreds, page_tens, page_units;
  467. page_units = infobits[0] & ROW25_COLUMN0_PAGE_UNITS;
  468. page_tens = infobits[1] & ROW25_COLUMN1_PAGE_TENS;
  469. page_hundreds = infobits[8] & ROW25_COLUMN8_PAGE_HUNDREDS;
  470. /* page 0x.. means page 8.. */
  471. if (page_hundreds == 0)
  472. page_hundreds = 8;
  473. return((page_hundreds << 8) | (page_tens << 4) | page_units);
  474. }
  475. /* Decodes the hour from the infobits contained in line 25.
  476. *
  477. * Parameters:
  478. * infobits: must be bits 0 to 9 of column 25
  479. *
  480. * Return: hour coded in hexadecimal, i. e. 12h is coded 0x12
  481. */
  482. static inline int saa5246a_extract_hour_from_infobits(
  483. unsigned char infobits[10])
  484. {
  485. int hour_tens, hour_units;
  486. hour_units = infobits[4] & ROW25_COLUMN4_HOUR_UNITS;
  487. hour_tens = infobits[5] & ROW25_COLUMN5_HOUR_TENS;
  488. return((hour_tens << 4) | hour_units);
  489. }
  490. /* Decodes the minutes from the infobits contained in line 25.
  491. *
  492. * Parameters:
  493. * infobits: must be bits 0 to 9 of column 25
  494. *
  495. * Return: minutes coded in hexadecimal, i. e. 10min is coded 0x10
  496. */
  497. static inline int saa5246a_extract_minutes_from_infobits(
  498. unsigned char infobits[10])
  499. {
  500. int minutes_tens, minutes_units;
  501. minutes_units = infobits[2] & ROW25_COLUMN2_MINUTES_UNITS;
  502. minutes_tens = infobits[3] & ROW25_COLUMN3_MINUTES_TENS;
  503. return((minutes_tens << 4) | minutes_units);
  504. }
  505. /* Reads the status bits contained in the first 10 columns of the first line
  506. * and extracts the information into info.
  507. *
  508. * Return value: 0 if successful
  509. */
  510. static inline int saa5246a_get_status(struct saa5246a_device *t,
  511. vtx_pageinfo_t *info, unsigned char dau_no)
  512. {
  513. unsigned char infobits[10];
  514. int column;
  515. if (dau_no >= NUM_DAUS)
  516. return -EINVAL;
  517. if (i2c_senddata(t, SAA5246A_REGISTER_R8,
  518. dau_no |
  519. R8_DO_NOT_CLEAR_MEMORY,
  520. R9_CURSER_ROW_25,
  521. R10_CURSER_COLUMN_0,
  522. COMMAND_END) ||
  523. i2c_getdata(t, 10, infobits))
  524. {
  525. return -EIO;
  526. }
  527. info->pagenum = saa5246a_extract_pagenum_from_infobits(infobits);
  528. info->hour = saa5246a_extract_hour_from_infobits(infobits);
  529. info->minute = saa5246a_extract_minutes_from_infobits(infobits);
  530. info->charset = ((infobits[7] & ROW25_COLUMN7_CHARACTER_SET) >> 1);
  531. info->delete = !!(infobits[3] & ROW25_COLUMN3_DELETE_PAGE);
  532. info->headline = !!(infobits[5] & ROW25_COLUMN5_INSERT_HEADLINE);
  533. info->subtitle = !!(infobits[5] & ROW25_COLUMN5_INSERT_SUBTITLE);
  534. info->supp_header = !!(infobits[6] & ROW25_COLUMN6_SUPPRESS_HEADER);
  535. info->update = !!(infobits[6] & ROW25_COLUMN6_UPDATE_PAGE);
  536. info->inter_seq = !!(infobits[6] & ROW25_COLUMN6_INTERRUPTED_SEQUENCE);
  537. info->dis_disp = !!(infobits[6] & ROW25_COLUMN6_SUPPRESS_DISPLAY);
  538. info->serial = !!(infobits[7] & ROW25_COLUMN7_SERIAL_MODE);
  539. info->notfound = !!(infobits[8] & ROW25_COLUMN8_PAGE_NOT_FOUND);
  540. info->pblf = !!(infobits[9] & ROW25_COLUMN9_PAGE_BEING_LOOKED_FOR);
  541. info->hamming = 0;
  542. for (column = 0; column <= 7; column++) {
  543. if (infobits[column] & ROW25_COLUMN0_TO_7_HAMMING_ERROR) {
  544. info->hamming = 1;
  545. break;
  546. }
  547. }
  548. if (!info->hamming && !info->notfound)
  549. t->is_searching[dau_no] = false;
  550. return 0;
  551. }
  552. /* Reads 1 videotext page buffer of the SAA5246A.
  553. *
  554. * req is used both as input and as output. It contains information which part
  555. * must be read. The videotext page is copied into req->buffer.
  556. *
  557. * Return value: 0 if successful
  558. */
  559. static inline int saa5246a_get_page(struct saa5246a_device *t,
  560. vtx_pagereq_t *req)
  561. {
  562. int start, end, size;
  563. char *buf;
  564. int err;
  565. if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS ||
  566. req->start < 0 || req->start > req->end || req->end >= VTX_PAGESIZE)
  567. return -EINVAL;
  568. buf = kmalloc(VTX_PAGESIZE, GFP_KERNEL);
  569. if (!buf)
  570. return -ENOMEM;
  571. /* Read "normal" part of page */
  572. err = -EIO;
  573. end = min(req->end, VTX_PAGESIZE - 1);
  574. if (i2c_senddata(t, SAA5246A_REGISTER_R8,
  575. req->pgbuf | R8_DO_NOT_CLEAR_MEMORY,
  576. ROW(req->start), COLUMN(req->start), COMMAND_END))
  577. goto out;
  578. if (i2c_getdata(t, end - req->start + 1, buf))
  579. goto out;
  580. err = -EFAULT;
  581. if (copy_to_user(req->buffer, buf, end - req->start + 1))
  582. goto out;
  583. /* Always get the time from buffer 4, since this stupid SAA5246A only
  584. * updates the currently displayed buffer...
  585. */
  586. if (REQ_CONTAINS_TIME(req)) {
  587. start = max(req->start, POS_TIME_START);
  588. end = min(req->end, POS_TIME_END);
  589. size = end - start + 1;
  590. err = -EINVAL;
  591. if (size < 0)
  592. goto out;
  593. err = -EIO;
  594. if (i2c_senddata(t, SAA5246A_REGISTER_R8,
  595. R8_ACTIVE_CHAPTER_4 | R8_DO_NOT_CLEAR_MEMORY,
  596. R9_CURSER_ROW_0, start, COMMAND_END))
  597. goto out;
  598. if (i2c_getdata(t, size, buf))
  599. goto out;
  600. err = -EFAULT;
  601. if (copy_to_user(req->buffer + start - req->start, buf, size))
  602. goto out;
  603. }
  604. /* Insert the header from buffer 4 only, if acquisition circuit is still searching for a page */
  605. if (REQ_CONTAINS_HEADER(req) && t->is_searching[req->pgbuf]) {
  606. start = max(req->start, POS_HEADER_START);
  607. end = min(req->end, POS_HEADER_END);
  608. size = end - start + 1;
  609. err = -EINVAL;
  610. if (size < 0)
  611. goto out;
  612. err = -EIO;
  613. if (i2c_senddata(t, SAA5246A_REGISTER_R8,
  614. R8_ACTIVE_CHAPTER_4 | R8_DO_NOT_CLEAR_MEMORY,
  615. R9_CURSER_ROW_0, start, COMMAND_END))
  616. goto out;
  617. if (i2c_getdata(t, end - start + 1, buf))
  618. goto out;
  619. err = -EFAULT;
  620. if (copy_to_user(req->buffer + start - req->start, buf, size))
  621. goto out;
  622. }
  623. err = 0;
  624. out:
  625. kfree(buf);
  626. return err;
  627. }
  628. /* Stops the acquisition circuit given in dau_no. The page buffer associated
  629. * with this acquisition circuit will no more be updated. The other daus are
  630. * not affected.
  631. *
  632. * Return value: 0 if successful
  633. */
  634. static inline int saa5246a_stop_dau(struct saa5246a_device *t,
  635. unsigned char dau_no)
  636. {
  637. if (dau_no >= NUM_DAUS)
  638. return -EINVAL;
  639. if (i2c_senddata(t, SAA5246A_REGISTER_R2,
  640. R2_IN_R3_SELECT_PAGE_HUNDREDS |
  641. dau_no << 4 |
  642. R2_BANK_0 |
  643. R2_HAMMING_CHECK_OFF,
  644. R3_PAGE_HUNDREDS_0 |
  645. R3_HOLD_PAGE |
  646. R3_PAGE_HUNDREDS_DO_NOT_CARE,
  647. COMMAND_END))
  648. {
  649. return -EIO;
  650. }
  651. t->is_searching[dau_no] = false;
  652. return 0;
  653. }
  654. /* Handles ioctls defined in videotext.h
  655. *
  656. * Returns 0 if successful
  657. */
  658. static long do_saa5246a_ioctl(struct file *file, unsigned int cmd, void *arg)
  659. {
  660. struct saa5246a_device *t = video_drvdata(file);
  661. switch(cmd)
  662. {
  663. case VTXIOCGETINFO:
  664. {
  665. vtx_info_t *info = arg;
  666. info->version_major = MAJOR_VERSION;
  667. info->version_minor = MINOR_VERSION;
  668. info->numpages = NUM_DAUS;
  669. return 0;
  670. }
  671. case VTXIOCCLRPAGE:
  672. {
  673. vtx_pagereq_t *req = arg;
  674. if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
  675. return -EINVAL;
  676. memset(t->pgbuf[req->pgbuf], ' ', sizeof(t->pgbuf[0]));
  677. return 0;
  678. }
  679. case VTXIOCCLRFOUND:
  680. {
  681. vtx_pagereq_t *req = arg;
  682. if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
  683. return -EINVAL;
  684. return(saa5246a_clear_found_bit(t, req->pgbuf));
  685. }
  686. case VTXIOCPAGEREQ:
  687. {
  688. vtx_pagereq_t *req = arg;
  689. return(saa5246a_request_page(t, req));
  690. }
  691. case VTXIOCGETSTAT:
  692. {
  693. vtx_pagereq_t *req = arg;
  694. vtx_pageinfo_t info;
  695. int rval;
  696. if ((rval = saa5246a_get_status(t, &info, req->pgbuf)))
  697. return rval;
  698. if(copy_to_user(req->buffer, &info,
  699. sizeof(vtx_pageinfo_t)))
  700. return -EFAULT;
  701. return 0;
  702. }
  703. case VTXIOCGETPAGE:
  704. {
  705. vtx_pagereq_t *req = arg;
  706. return(saa5246a_get_page(t, req));
  707. }
  708. case VTXIOCSTOPDAU:
  709. {
  710. vtx_pagereq_t *req = arg;
  711. return(saa5246a_stop_dau(t, req->pgbuf));
  712. }
  713. case VTXIOCPUTPAGE:
  714. case VTXIOCSETDISP:
  715. case VTXIOCPUTSTAT:
  716. return 0;
  717. case VTXIOCCLRCACHE:
  718. {
  719. return 0;
  720. }
  721. case VTXIOCSETVIRT:
  722. {
  723. /* I do not know what "virtual mode" means */
  724. return 0;
  725. }
  726. }
  727. return -EINVAL;
  728. }
  729. /*
  730. * Translates old vtx IOCTLs to new ones
  731. *
  732. * This keeps new kernel versions compatible with old userspace programs.
  733. */
  734. static inline unsigned int vtx_fix_command(unsigned int cmd)
  735. {
  736. switch (cmd) {
  737. case VTXIOCGETINFO_OLD:
  738. cmd = VTXIOCGETINFO;
  739. break;
  740. case VTXIOCCLRPAGE_OLD:
  741. cmd = VTXIOCCLRPAGE;
  742. break;
  743. case VTXIOCCLRFOUND_OLD:
  744. cmd = VTXIOCCLRFOUND;
  745. break;
  746. case VTXIOCPAGEREQ_OLD:
  747. cmd = VTXIOCPAGEREQ;
  748. break;
  749. case VTXIOCGETSTAT_OLD:
  750. cmd = VTXIOCGETSTAT;
  751. break;
  752. case VTXIOCGETPAGE_OLD:
  753. cmd = VTXIOCGETPAGE;
  754. break;
  755. case VTXIOCSTOPDAU_OLD:
  756. cmd = VTXIOCSTOPDAU;
  757. break;
  758. case VTXIOCPUTPAGE_OLD:
  759. cmd = VTXIOCPUTPAGE;
  760. break;
  761. case VTXIOCSETDISP_OLD:
  762. cmd = VTXIOCSETDISP;
  763. break;
  764. case VTXIOCPUTSTAT_OLD:
  765. cmd = VTXIOCPUTSTAT;
  766. break;
  767. case VTXIOCCLRCACHE_OLD:
  768. cmd = VTXIOCCLRCACHE;
  769. break;
  770. case VTXIOCSETVIRT_OLD:
  771. cmd = VTXIOCSETVIRT;
  772. break;
  773. }
  774. return cmd;
  775. }
  776. /*
  777. * Handle the locking
  778. */
  779. static long saa5246a_ioctl(struct file *file,
  780. unsigned int cmd, unsigned long arg)
  781. {
  782. struct saa5246a_device *t = video_drvdata(file);
  783. long err;
  784. cmd = vtx_fix_command(cmd);
  785. mutex_lock(&t->lock);
  786. err = video_usercopy(file, cmd, arg, do_saa5246a_ioctl);
  787. mutex_unlock(&t->lock);
  788. return err;
  789. }
  790. static int saa5246a_open(struct file *file)
  791. {
  792. struct saa5246a_device *t = video_drvdata(file);
  793. if (t->client == NULL)
  794. return -ENODEV;
  795. if (test_and_set_bit(0, &t->in_use))
  796. return -EBUSY;
  797. if (i2c_senddata(t, SAA5246A_REGISTER_R0,
  798. R0_SELECT_R11 |
  799. R0_PLL_TIME_CONSTANT_LONG |
  800. R0_ENABLE_nODD_EVEN_OUTPUT |
  801. R0_ENABLE_HDR_POLL |
  802. R0_DO_NOT_FORCE_nODD_EVEN_LOW_IF_PICTURE_DISPLAYED |
  803. R0_NO_FREE_RUN_PLL |
  804. R0_NO_AUTOMATIC_FASTEXT_PROMPT,
  805. R1_NON_INTERLACED_312_312_LINES |
  806. R1_DEW |
  807. R1_EXTENDED_PACKET_DISABLE |
  808. R1_DAUS_ALL_ON |
  809. R1_8_BITS_NO_PARITY |
  810. R1_VCS_TO_SCS,
  811. COMMAND_END) ||
  812. i2c_senddata(t, SAA5246A_REGISTER_R4,
  813. /* We do not care much for the TV display but nevertheless we
  814. * need the currently displayed page later because only on that
  815. * page the time is updated. */
  816. R4_DISPLAY_PAGE_4,
  817. COMMAND_END))
  818. {
  819. clear_bit(0, &t->in_use);
  820. return -EIO;
  821. }
  822. return 0;
  823. }
  824. static int saa5246a_release(struct file *file)
  825. {
  826. struct saa5246a_device *t = video_drvdata(file);
  827. /* Stop all acquisition circuits. */
  828. i2c_senddata(t, SAA5246A_REGISTER_R1,
  829. R1_INTERLACED_312_AND_HALF_312_AND_HALF_LINES |
  830. R1_DEW |
  831. R1_EXTENDED_PACKET_DISABLE |
  832. R1_DAUS_ALL_OFF |
  833. R1_8_BITS_NO_PARITY |
  834. R1_VCS_TO_SCS,
  835. COMMAND_END);
  836. clear_bit(0, &t->in_use);
  837. return 0;
  838. }
  839. static const struct v4l2_file_operations saa_fops = {
  840. .owner = THIS_MODULE,
  841. .open = saa5246a_open,
  842. .release = saa5246a_release,
  843. .ioctl = saa5246a_ioctl,
  844. };
  845. static struct video_device saa_template =
  846. {
  847. .name = "saa5246a",
  848. .fops = &saa_fops,
  849. .release = video_device_release,
  850. .minor = -1,
  851. };
  852. /* Addresses to scan */
  853. static unsigned short normal_i2c[] = { 0x22 >> 1, I2C_CLIENT_END };
  854. I2C_CLIENT_INSMOD;
  855. static int saa5246a_probe(struct i2c_client *client,
  856. const struct i2c_device_id *id)
  857. {
  858. int pgbuf;
  859. int err;
  860. struct video_device *vd;
  861. struct saa5246a_device *t;
  862. v4l_info(client, "chip found @ 0x%x (%s)\n",
  863. client->addr << 1, client->adapter->name);
  864. v4l_info(client, "VideoText version %d.%d\n",
  865. MAJOR_VERSION, MINOR_VERSION);
  866. t = kzalloc(sizeof(*t), GFP_KERNEL);
  867. if (t == NULL)
  868. return -ENOMEM;
  869. mutex_init(&t->lock);
  870. /* Now create a video4linux device */
  871. vd = video_device_alloc();
  872. if (vd == NULL) {
  873. kfree(t);
  874. return -ENOMEM;
  875. }
  876. i2c_set_clientdata(client, vd);
  877. memcpy(vd, &saa_template, sizeof(*vd));
  878. for (pgbuf = 0; pgbuf < NUM_DAUS; pgbuf++) {
  879. memset(t->pgbuf[pgbuf], ' ', sizeof(t->pgbuf[0]));
  880. t->is_searching[pgbuf] = false;
  881. }
  882. video_set_drvdata(vd, t);
  883. /* Register it */
  884. err = video_register_device(vd, VFL_TYPE_VTX, -1);
  885. if (err < 0) {
  886. kfree(t);
  887. video_device_release(vd);
  888. return err;
  889. }
  890. t->client = client;
  891. return 0;
  892. }
  893. static int saa5246a_remove(struct i2c_client *client)
  894. {
  895. struct video_device *vd = i2c_get_clientdata(client);
  896. video_unregister_device(vd);
  897. kfree(video_get_drvdata(vd));
  898. return 0;
  899. }
  900. static const struct i2c_device_id saa5246a_id[] = {
  901. { "saa5246a", 0 },
  902. { }
  903. };
  904. MODULE_DEVICE_TABLE(i2c, saa5246a_id);
  905. static struct v4l2_i2c_driver_data v4l2_i2c_data = {
  906. .name = "saa5246a",
  907. .driverid = I2C_DRIVERID_SAA5249,
  908. .probe = saa5246a_probe,
  909. .remove = saa5246a_remove,
  910. .id_table = saa5246a_id,
  911. };