saa5246a.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  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/mutex.h>
  46. #include <linux/videotext.h>
  47. #include <linux/videodev2.h>
  48. #include <media/v4l2-device.h>
  49. #include <media/v4l2-chip-ident.h>
  50. #include <media/v4l2-ioctl.h>
  51. #include <media/v4l2-i2c-drv.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. struct v4l2_subdev sd;
  308. struct video_device *vdev;
  309. u8 pgbuf[NUM_DAUS][VTX_VIRTUALSIZE];
  310. int is_searching[NUM_DAUS];
  311. unsigned long in_use;
  312. struct mutex lock;
  313. };
  314. static inline struct saa5246a_device *to_dev(struct v4l2_subdev *sd)
  315. {
  316. return container_of(sd, struct saa5246a_device, sd);
  317. }
  318. static struct video_device saa_template; /* Declared near bottom */
  319. /*
  320. * I2C interfaces
  321. */
  322. static int i2c_sendbuf(struct saa5246a_device *t, int reg, int count, u8 *data)
  323. {
  324. struct i2c_client *client = v4l2_get_subdevdata(&t->sd);
  325. char buf[64];
  326. buf[0] = reg;
  327. memcpy(buf+1, data, count);
  328. if (i2c_master_send(client, buf, count + 1) == count + 1)
  329. return 0;
  330. return -1;
  331. }
  332. static int i2c_senddata(struct saa5246a_device *t, ...)
  333. {
  334. unsigned char buf[64];
  335. int v;
  336. int ct = 0;
  337. va_list argp;
  338. va_start(argp, t);
  339. while ((v = va_arg(argp, int)) != -1)
  340. buf[ct++] = v;
  341. va_end(argp);
  342. return i2c_sendbuf(t, buf[0], ct-1, buf+1);
  343. }
  344. /* Get count number of bytes from I²C-device at address adr, store them in buf.
  345. * Start & stop handshaking is done by this routine, ack will be sent after the
  346. * last byte to inhibit further sending of data. If uaccess is 'true', data is
  347. * written to user-space with put_user. Returns -1 if I²C-device didn't send
  348. * acknowledge, 0 otherwise
  349. */
  350. static int i2c_getdata(struct saa5246a_device *t, int count, u8 *buf)
  351. {
  352. struct i2c_client *client = v4l2_get_subdevdata(&t->sd);
  353. if (i2c_master_recv(client, buf, count) != count)
  354. return -1;
  355. return 0;
  356. }
  357. /* When a page is found then the not FOUND bit in one of the status registers
  358. * of the SAA5264A chip is cleared. Unfortunately this bit is not set
  359. * automatically when a new page is requested. Instead this function must be
  360. * called after a page has been requested.
  361. *
  362. * Return value: 0 if successful
  363. */
  364. static int saa5246a_clear_found_bit(struct saa5246a_device *t,
  365. unsigned char dau_no)
  366. {
  367. unsigned char row_25_column_8;
  368. if (i2c_senddata(t, SAA5246A_REGISTER_R8,
  369. dau_no |
  370. R8_DO_NOT_CLEAR_MEMORY,
  371. R9_CURSER_ROW_25,
  372. R10_CURSER_COLUMN_8,
  373. COMMAND_END) ||
  374. i2c_getdata(t, 1, &row_25_column_8))
  375. {
  376. return -EIO;
  377. }
  378. row_25_column_8 |= ROW25_COLUMN8_PAGE_NOT_FOUND;
  379. if (i2c_senddata(t, SAA5246A_REGISTER_R8,
  380. dau_no |
  381. R8_DO_NOT_CLEAR_MEMORY,
  382. R9_CURSER_ROW_25,
  383. R10_CURSER_COLUMN_8,
  384. row_25_column_8,
  385. COMMAND_END))
  386. {
  387. return -EIO;
  388. }
  389. return 0;
  390. }
  391. /* Requests one videotext page as described in req. The fields of req are
  392. * checked and an error is returned if something is invalid.
  393. *
  394. * Return value: 0 if successful
  395. */
  396. static int saa5246a_request_page(struct saa5246a_device *t,
  397. vtx_pagereq_t *req)
  398. {
  399. if (req->pagemask < 0 || req->pagemask >= PGMASK_MAX)
  400. return -EINVAL;
  401. if (req->pagemask & PGMASK_PAGE)
  402. if (req->page < 0 || req->page > PAGE_MAX)
  403. return -EINVAL;
  404. if (req->pagemask & PGMASK_HOUR)
  405. if (req->hour < 0 || req->hour > HOUR_MAX)
  406. return -EINVAL;
  407. if (req->pagemask & PGMASK_MINUTE)
  408. if (req->minute < 0 || req->minute > MINUTE_MAX)
  409. return -EINVAL;
  410. if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
  411. return -EINVAL;
  412. if (i2c_senddata(t, SAA5246A_REGISTER_R2,
  413. R2_IN_R3_SELECT_PAGE_HUNDREDS |
  414. req->pgbuf << 4 |
  415. R2_BANK_0 |
  416. R2_HAMMING_CHECK_OFF,
  417. HUNDREDS_OF_PAGE(req->page) |
  418. R3_HOLD_PAGE |
  419. (req->pagemask & PG_HUND ?
  420. R3_PAGE_HUNDREDS_DO_CARE :
  421. R3_PAGE_HUNDREDS_DO_NOT_CARE),
  422. TENS_OF_PAGE(req->page) |
  423. (req->pagemask & PG_TEN ?
  424. R3_PAGE_TENS_DO_CARE :
  425. R3_PAGE_TENS_DO_NOT_CARE),
  426. UNITS_OF_PAGE(req->page) |
  427. (req->pagemask & PG_UNIT ?
  428. R3_PAGE_UNITS_DO_CARE :
  429. R3_PAGE_UNITS_DO_NOT_CARE),
  430. TENS_OF_HOUR(req->hour) |
  431. (req->pagemask & HR_TEN ?
  432. R3_HOURS_TENS_DO_CARE :
  433. R3_HOURS_TENS_DO_NOT_CARE),
  434. UNITS_OF_HOUR(req->hour) |
  435. (req->pagemask & HR_UNIT ?
  436. R3_HOURS_UNITS_DO_CARE :
  437. R3_HOURS_UNITS_DO_NOT_CARE),
  438. TENS_OF_MINUTE(req->minute) |
  439. (req->pagemask & MIN_TEN ?
  440. R3_MINUTES_TENS_DO_CARE :
  441. R3_MINUTES_TENS_DO_NOT_CARE),
  442. UNITS_OF_MINUTE(req->minute) |
  443. (req->pagemask & MIN_UNIT ?
  444. R3_MINUTES_UNITS_DO_CARE :
  445. R3_MINUTES_UNITS_DO_NOT_CARE),
  446. COMMAND_END) || i2c_senddata(t, SAA5246A_REGISTER_R2,
  447. R2_IN_R3_SELECT_PAGE_HUNDREDS |
  448. req->pgbuf << 4 |
  449. R2_BANK_0 |
  450. R2_HAMMING_CHECK_OFF,
  451. HUNDREDS_OF_PAGE(req->page) |
  452. R3_UPDATE_PAGE |
  453. (req->pagemask & PG_HUND ?
  454. R3_PAGE_HUNDREDS_DO_CARE :
  455. R3_PAGE_HUNDREDS_DO_NOT_CARE),
  456. COMMAND_END))
  457. {
  458. return -EIO;
  459. }
  460. t->is_searching[req->pgbuf] = true;
  461. return 0;
  462. }
  463. /* This routine decodes the page number from the infobits contained in line 25.
  464. *
  465. * Parameters:
  466. * infobits: must be bits 0 to 9 of column 25
  467. *
  468. * Return value: page number coded in hexadecimal, i. e. page 123 is coded 0x123
  469. */
  470. static inline int saa5246a_extract_pagenum_from_infobits(
  471. unsigned char infobits[10])
  472. {
  473. int page_hundreds, page_tens, page_units;
  474. page_units = infobits[0] & ROW25_COLUMN0_PAGE_UNITS;
  475. page_tens = infobits[1] & ROW25_COLUMN1_PAGE_TENS;
  476. page_hundreds = infobits[8] & ROW25_COLUMN8_PAGE_HUNDREDS;
  477. /* page 0x.. means page 8.. */
  478. if (page_hundreds == 0)
  479. page_hundreds = 8;
  480. return((page_hundreds << 8) | (page_tens << 4) | page_units);
  481. }
  482. /* Decodes the hour from the infobits contained in line 25.
  483. *
  484. * Parameters:
  485. * infobits: must be bits 0 to 9 of column 25
  486. *
  487. * Return: hour coded in hexadecimal, i. e. 12h is coded 0x12
  488. */
  489. static inline int saa5246a_extract_hour_from_infobits(
  490. unsigned char infobits[10])
  491. {
  492. int hour_tens, hour_units;
  493. hour_units = infobits[4] & ROW25_COLUMN4_HOUR_UNITS;
  494. hour_tens = infobits[5] & ROW25_COLUMN5_HOUR_TENS;
  495. return((hour_tens << 4) | hour_units);
  496. }
  497. /* Decodes the minutes from the infobits contained in line 25.
  498. *
  499. * Parameters:
  500. * infobits: must be bits 0 to 9 of column 25
  501. *
  502. * Return: minutes coded in hexadecimal, i. e. 10min is coded 0x10
  503. */
  504. static inline int saa5246a_extract_minutes_from_infobits(
  505. unsigned char infobits[10])
  506. {
  507. int minutes_tens, minutes_units;
  508. minutes_units = infobits[2] & ROW25_COLUMN2_MINUTES_UNITS;
  509. minutes_tens = infobits[3] & ROW25_COLUMN3_MINUTES_TENS;
  510. return((minutes_tens << 4) | minutes_units);
  511. }
  512. /* Reads the status bits contained in the first 10 columns of the first line
  513. * and extracts the information into info.
  514. *
  515. * Return value: 0 if successful
  516. */
  517. static inline int saa5246a_get_status(struct saa5246a_device *t,
  518. vtx_pageinfo_t *info, unsigned char dau_no)
  519. {
  520. unsigned char infobits[10];
  521. int column;
  522. if (dau_no >= NUM_DAUS)
  523. return -EINVAL;
  524. if (i2c_senddata(t, SAA5246A_REGISTER_R8,
  525. dau_no |
  526. R8_DO_NOT_CLEAR_MEMORY,
  527. R9_CURSER_ROW_25,
  528. R10_CURSER_COLUMN_0,
  529. COMMAND_END) ||
  530. i2c_getdata(t, 10, infobits))
  531. {
  532. return -EIO;
  533. }
  534. info->pagenum = saa5246a_extract_pagenum_from_infobits(infobits);
  535. info->hour = saa5246a_extract_hour_from_infobits(infobits);
  536. info->minute = saa5246a_extract_minutes_from_infobits(infobits);
  537. info->charset = ((infobits[7] & ROW25_COLUMN7_CHARACTER_SET) >> 1);
  538. info->delete = !!(infobits[3] & ROW25_COLUMN3_DELETE_PAGE);
  539. info->headline = !!(infobits[5] & ROW25_COLUMN5_INSERT_HEADLINE);
  540. info->subtitle = !!(infobits[5] & ROW25_COLUMN5_INSERT_SUBTITLE);
  541. info->supp_header = !!(infobits[6] & ROW25_COLUMN6_SUPPRESS_HEADER);
  542. info->update = !!(infobits[6] & ROW25_COLUMN6_UPDATE_PAGE);
  543. info->inter_seq = !!(infobits[6] & ROW25_COLUMN6_INTERRUPTED_SEQUENCE);
  544. info->dis_disp = !!(infobits[6] & ROW25_COLUMN6_SUPPRESS_DISPLAY);
  545. info->serial = !!(infobits[7] & ROW25_COLUMN7_SERIAL_MODE);
  546. info->notfound = !!(infobits[8] & ROW25_COLUMN8_PAGE_NOT_FOUND);
  547. info->pblf = !!(infobits[9] & ROW25_COLUMN9_PAGE_BEING_LOOKED_FOR);
  548. info->hamming = 0;
  549. for (column = 0; column <= 7; column++) {
  550. if (infobits[column] & ROW25_COLUMN0_TO_7_HAMMING_ERROR) {
  551. info->hamming = 1;
  552. break;
  553. }
  554. }
  555. if (!info->hamming && !info->notfound)
  556. t->is_searching[dau_no] = false;
  557. return 0;
  558. }
  559. /* Reads 1 videotext page buffer of the SAA5246A.
  560. *
  561. * req is used both as input and as output. It contains information which part
  562. * must be read. The videotext page is copied into req->buffer.
  563. *
  564. * Return value: 0 if successful
  565. */
  566. static inline int saa5246a_get_page(struct saa5246a_device *t,
  567. vtx_pagereq_t *req)
  568. {
  569. int start, end, size;
  570. char *buf;
  571. int err;
  572. if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS ||
  573. req->start < 0 || req->start > req->end || req->end >= VTX_PAGESIZE)
  574. return -EINVAL;
  575. buf = kmalloc(VTX_PAGESIZE, GFP_KERNEL);
  576. if (!buf)
  577. return -ENOMEM;
  578. /* Read "normal" part of page */
  579. err = -EIO;
  580. end = min(req->end, VTX_PAGESIZE - 1);
  581. if (i2c_senddata(t, SAA5246A_REGISTER_R8,
  582. req->pgbuf | R8_DO_NOT_CLEAR_MEMORY,
  583. ROW(req->start), COLUMN(req->start), COMMAND_END))
  584. goto out;
  585. if (i2c_getdata(t, end - req->start + 1, buf))
  586. goto out;
  587. err = -EFAULT;
  588. if (copy_to_user(req->buffer, buf, end - req->start + 1))
  589. goto out;
  590. /* Always get the time from buffer 4, since this stupid SAA5246A only
  591. * updates the currently displayed buffer...
  592. */
  593. if (REQ_CONTAINS_TIME(req)) {
  594. start = max(req->start, POS_TIME_START);
  595. end = min(req->end, POS_TIME_END);
  596. size = end - start + 1;
  597. err = -EINVAL;
  598. if (size < 0)
  599. goto out;
  600. err = -EIO;
  601. if (i2c_senddata(t, SAA5246A_REGISTER_R8,
  602. R8_ACTIVE_CHAPTER_4 | R8_DO_NOT_CLEAR_MEMORY,
  603. R9_CURSER_ROW_0, start, COMMAND_END))
  604. goto out;
  605. if (i2c_getdata(t, size, buf))
  606. goto out;
  607. err = -EFAULT;
  608. if (copy_to_user(req->buffer + start - req->start, buf, size))
  609. goto out;
  610. }
  611. /* Insert the header from buffer 4 only, if acquisition circuit is still searching for a page */
  612. if (REQ_CONTAINS_HEADER(req) && t->is_searching[req->pgbuf]) {
  613. start = max(req->start, POS_HEADER_START);
  614. end = min(req->end, POS_HEADER_END);
  615. size = end - start + 1;
  616. err = -EINVAL;
  617. if (size < 0)
  618. goto out;
  619. err = -EIO;
  620. if (i2c_senddata(t, SAA5246A_REGISTER_R8,
  621. R8_ACTIVE_CHAPTER_4 | R8_DO_NOT_CLEAR_MEMORY,
  622. R9_CURSER_ROW_0, start, COMMAND_END))
  623. goto out;
  624. if (i2c_getdata(t, end - start + 1, buf))
  625. goto out;
  626. err = -EFAULT;
  627. if (copy_to_user(req->buffer + start - req->start, buf, size))
  628. goto out;
  629. }
  630. err = 0;
  631. out:
  632. kfree(buf);
  633. return err;
  634. }
  635. /* Stops the acquisition circuit given in dau_no. The page buffer associated
  636. * with this acquisition circuit will no more be updated. The other daus are
  637. * not affected.
  638. *
  639. * Return value: 0 if successful
  640. */
  641. static inline int saa5246a_stop_dau(struct saa5246a_device *t,
  642. unsigned char dau_no)
  643. {
  644. if (dau_no >= NUM_DAUS)
  645. return -EINVAL;
  646. if (i2c_senddata(t, SAA5246A_REGISTER_R2,
  647. R2_IN_R3_SELECT_PAGE_HUNDREDS |
  648. dau_no << 4 |
  649. R2_BANK_0 |
  650. R2_HAMMING_CHECK_OFF,
  651. R3_PAGE_HUNDREDS_0 |
  652. R3_HOLD_PAGE |
  653. R3_PAGE_HUNDREDS_DO_NOT_CARE,
  654. COMMAND_END))
  655. {
  656. return -EIO;
  657. }
  658. t->is_searching[dau_no] = false;
  659. return 0;
  660. }
  661. /* Handles ioctls defined in videotext.h
  662. *
  663. * Returns 0 if successful
  664. */
  665. static long do_saa5246a_ioctl(struct file *file, unsigned int cmd, void *arg)
  666. {
  667. struct saa5246a_device *t = video_drvdata(file);
  668. switch(cmd)
  669. {
  670. case VTXIOCGETINFO:
  671. {
  672. vtx_info_t *info = arg;
  673. info->version_major = MAJOR_VERSION;
  674. info->version_minor = MINOR_VERSION;
  675. info->numpages = NUM_DAUS;
  676. return 0;
  677. }
  678. case VTXIOCCLRPAGE:
  679. {
  680. vtx_pagereq_t *req = arg;
  681. if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
  682. return -EINVAL;
  683. memset(t->pgbuf[req->pgbuf], ' ', sizeof(t->pgbuf[0]));
  684. return 0;
  685. }
  686. case VTXIOCCLRFOUND:
  687. {
  688. vtx_pagereq_t *req = arg;
  689. if (req->pgbuf < 0 || req->pgbuf >= NUM_DAUS)
  690. return -EINVAL;
  691. return(saa5246a_clear_found_bit(t, req->pgbuf));
  692. }
  693. case VTXIOCPAGEREQ:
  694. {
  695. vtx_pagereq_t *req = arg;
  696. return(saa5246a_request_page(t, req));
  697. }
  698. case VTXIOCGETSTAT:
  699. {
  700. vtx_pagereq_t *req = arg;
  701. vtx_pageinfo_t info;
  702. int rval;
  703. if ((rval = saa5246a_get_status(t, &info, req->pgbuf)))
  704. return rval;
  705. if(copy_to_user(req->buffer, &info,
  706. sizeof(vtx_pageinfo_t)))
  707. return -EFAULT;
  708. return 0;
  709. }
  710. case VTXIOCGETPAGE:
  711. {
  712. vtx_pagereq_t *req = arg;
  713. return(saa5246a_get_page(t, req));
  714. }
  715. case VTXIOCSTOPDAU:
  716. {
  717. vtx_pagereq_t *req = arg;
  718. return(saa5246a_stop_dau(t, req->pgbuf));
  719. }
  720. case VTXIOCPUTPAGE:
  721. case VTXIOCSETDISP:
  722. case VTXIOCPUTSTAT:
  723. return 0;
  724. case VTXIOCCLRCACHE:
  725. {
  726. return 0;
  727. }
  728. case VTXIOCSETVIRT:
  729. {
  730. /* I do not know what "virtual mode" means */
  731. return 0;
  732. }
  733. }
  734. return -EINVAL;
  735. }
  736. /*
  737. * Translates old vtx IOCTLs to new ones
  738. *
  739. * This keeps new kernel versions compatible with old userspace programs.
  740. */
  741. static inline unsigned int vtx_fix_command(unsigned int cmd)
  742. {
  743. switch (cmd) {
  744. case VTXIOCGETINFO_OLD:
  745. cmd = VTXIOCGETINFO;
  746. break;
  747. case VTXIOCCLRPAGE_OLD:
  748. cmd = VTXIOCCLRPAGE;
  749. break;
  750. case VTXIOCCLRFOUND_OLD:
  751. cmd = VTXIOCCLRFOUND;
  752. break;
  753. case VTXIOCPAGEREQ_OLD:
  754. cmd = VTXIOCPAGEREQ;
  755. break;
  756. case VTXIOCGETSTAT_OLD:
  757. cmd = VTXIOCGETSTAT;
  758. break;
  759. case VTXIOCGETPAGE_OLD:
  760. cmd = VTXIOCGETPAGE;
  761. break;
  762. case VTXIOCSTOPDAU_OLD:
  763. cmd = VTXIOCSTOPDAU;
  764. break;
  765. case VTXIOCPUTPAGE_OLD:
  766. cmd = VTXIOCPUTPAGE;
  767. break;
  768. case VTXIOCSETDISP_OLD:
  769. cmd = VTXIOCSETDISP;
  770. break;
  771. case VTXIOCPUTSTAT_OLD:
  772. cmd = VTXIOCPUTSTAT;
  773. break;
  774. case VTXIOCCLRCACHE_OLD:
  775. cmd = VTXIOCCLRCACHE;
  776. break;
  777. case VTXIOCSETVIRT_OLD:
  778. cmd = VTXIOCSETVIRT;
  779. break;
  780. }
  781. return cmd;
  782. }
  783. /*
  784. * Handle the locking
  785. */
  786. static long saa5246a_ioctl(struct file *file,
  787. unsigned int cmd, unsigned long arg)
  788. {
  789. struct saa5246a_device *t = video_drvdata(file);
  790. long err;
  791. cmd = vtx_fix_command(cmd);
  792. mutex_lock(&t->lock);
  793. err = video_usercopy(file, cmd, arg, do_saa5246a_ioctl);
  794. mutex_unlock(&t->lock);
  795. return err;
  796. }
  797. static int saa5246a_open(struct file *file)
  798. {
  799. struct saa5246a_device *t = video_drvdata(file);
  800. if (test_and_set_bit(0, &t->in_use))
  801. return -EBUSY;
  802. if (i2c_senddata(t, SAA5246A_REGISTER_R0,
  803. R0_SELECT_R11 |
  804. R0_PLL_TIME_CONSTANT_LONG |
  805. R0_ENABLE_nODD_EVEN_OUTPUT |
  806. R0_ENABLE_HDR_POLL |
  807. R0_DO_NOT_FORCE_nODD_EVEN_LOW_IF_PICTURE_DISPLAYED |
  808. R0_NO_FREE_RUN_PLL |
  809. R0_NO_AUTOMATIC_FASTEXT_PROMPT,
  810. R1_NON_INTERLACED_312_312_LINES |
  811. R1_DEW |
  812. R1_EXTENDED_PACKET_DISABLE |
  813. R1_DAUS_ALL_ON |
  814. R1_8_BITS_NO_PARITY |
  815. R1_VCS_TO_SCS,
  816. COMMAND_END) ||
  817. i2c_senddata(t, SAA5246A_REGISTER_R4,
  818. /* We do not care much for the TV display but nevertheless we
  819. * need the currently displayed page later because only on that
  820. * page the time is updated. */
  821. R4_DISPLAY_PAGE_4,
  822. COMMAND_END))
  823. {
  824. clear_bit(0, &t->in_use);
  825. return -EIO;
  826. }
  827. return 0;
  828. }
  829. static int saa5246a_release(struct file *file)
  830. {
  831. struct saa5246a_device *t = video_drvdata(file);
  832. /* Stop all acquisition circuits. */
  833. i2c_senddata(t, SAA5246A_REGISTER_R1,
  834. R1_INTERLACED_312_AND_HALF_312_AND_HALF_LINES |
  835. R1_DEW |
  836. R1_EXTENDED_PACKET_DISABLE |
  837. R1_DAUS_ALL_OFF |
  838. R1_8_BITS_NO_PARITY |
  839. R1_VCS_TO_SCS,
  840. COMMAND_END);
  841. clear_bit(0, &t->in_use);
  842. return 0;
  843. }
  844. static const struct v4l2_file_operations saa_fops = {
  845. .owner = THIS_MODULE,
  846. .open = saa5246a_open,
  847. .release = saa5246a_release,
  848. .ioctl = saa5246a_ioctl,
  849. };
  850. static struct video_device saa_template =
  851. {
  852. .name = "saa5246a",
  853. .fops = &saa_fops,
  854. .release = video_device_release,
  855. .minor = -1,
  856. };
  857. static int saa5246a_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
  858. {
  859. struct i2c_client *client = v4l2_get_subdevdata(sd);
  860. return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_SAA5246A, 0);
  861. }
  862. static const struct v4l2_subdev_core_ops saa5246a_core_ops = {
  863. .g_chip_ident = saa5246a_g_chip_ident,
  864. };
  865. static const struct v4l2_subdev_ops saa5246a_ops = {
  866. .core = &saa5246a_core_ops,
  867. };
  868. static int saa5246a_probe(struct i2c_client *client,
  869. const struct i2c_device_id *id)
  870. {
  871. int pgbuf;
  872. int err;
  873. struct saa5246a_device *t;
  874. struct v4l2_subdev *sd;
  875. v4l_info(client, "chip found @ 0x%x (%s)\n",
  876. client->addr << 1, client->adapter->name);
  877. v4l_info(client, "VideoText version %d.%d\n",
  878. MAJOR_VERSION, MINOR_VERSION);
  879. t = kzalloc(sizeof(*t), GFP_KERNEL);
  880. if (t == NULL)
  881. return -ENOMEM;
  882. sd = &t->sd;
  883. v4l2_i2c_subdev_init(sd, client, &saa5246a_ops);
  884. mutex_init(&t->lock);
  885. /* Now create a video4linux device */
  886. t->vdev = video_device_alloc();
  887. if (t->vdev == NULL) {
  888. kfree(t);
  889. return -ENOMEM;
  890. }
  891. memcpy(t->vdev, &saa_template, sizeof(*t->vdev));
  892. for (pgbuf = 0; pgbuf < NUM_DAUS; pgbuf++) {
  893. memset(t->pgbuf[pgbuf], ' ', sizeof(t->pgbuf[0]));
  894. t->is_searching[pgbuf] = false;
  895. }
  896. video_set_drvdata(t->vdev, t);
  897. /* Register it */
  898. err = video_register_device(t->vdev, VFL_TYPE_VTX, -1);
  899. if (err < 0) {
  900. video_device_release(t->vdev);
  901. kfree(t);
  902. return err;
  903. }
  904. return 0;
  905. }
  906. static int saa5246a_remove(struct i2c_client *client)
  907. {
  908. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  909. struct saa5246a_device *t = to_dev(sd);
  910. video_unregister_device(t->vdev);
  911. v4l2_device_unregister_subdev(sd);
  912. kfree(t);
  913. return 0;
  914. }
  915. static const struct i2c_device_id saa5246a_id[] = {
  916. { "saa5246a", 0 },
  917. { }
  918. };
  919. MODULE_DEVICE_TABLE(i2c, saa5246a_id);
  920. static struct v4l2_i2c_driver_data v4l2_i2c_data = {
  921. .name = "saa5246a",
  922. .probe = saa5246a_probe,
  923. .remove = saa5246a_remove,
  924. .id_table = saa5246a_id,
  925. };