mxb.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. /*
  2. mxb - v4l2 driver for the Multimedia eXtension Board
  3. Copyright (C) 1998-2006 Michael Hunold <michael@mihu.de>
  4. Visit http://www.themm.net/~mihu/linux/saa7146/mxb.html
  5. for further details about this card.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #define DEBUG_VARIABLE debug
  20. #include <media/saa7146_vv.h>
  21. #include <media/tuner.h>
  22. #include <media/v4l2-common.h>
  23. #include <media/saa7115.h>
  24. #include <linux/module.h>
  25. #include "mxb.h"
  26. #include "tea6415c.h"
  27. #include "tea6420.h"
  28. #define I2C_SAA7111A 0x24
  29. #define I2C_TDA9840 0x42
  30. #define I2C_TEA6415C 0x43
  31. #define I2C_TEA6420_1 0x4c
  32. #define I2C_TEA6420_2 0x4d
  33. #define I2C_TUNER 0x60
  34. #define MXB_BOARD_CAN_DO_VBI(dev) (dev->revision != 0)
  35. /* global variable */
  36. static int mxb_num;
  37. /* initial frequence the tuner will be tuned to.
  38. in verden (lower saxony, germany) 4148 is a
  39. channel called "phoenix" */
  40. static int freq = 4148;
  41. module_param(freq, int, 0644);
  42. MODULE_PARM_DESC(freq, "initial frequency the tuner will be tuned to while setup");
  43. static int debug;
  44. module_param(debug, int, 0644);
  45. MODULE_PARM_DESC(debug, "Turn on/off device debugging (default:off).");
  46. #define MXB_INPUTS 4
  47. enum { TUNER, AUX1, AUX3, AUX3_YC };
  48. static struct v4l2_input mxb_inputs[MXB_INPUTS] = {
  49. { TUNER, "Tuner", V4L2_INPUT_TYPE_TUNER, 1, 0, V4L2_STD_PAL_BG|V4L2_STD_NTSC_M, 0, V4L2_IN_CAP_STD },
  50. { AUX1, "AUX1", V4L2_INPUT_TYPE_CAMERA, 2, 0, V4L2_STD_PAL_BG|V4L2_STD_NTSC_M, 0, V4L2_IN_CAP_STD },
  51. { AUX3, "AUX3 Composite", V4L2_INPUT_TYPE_CAMERA, 4, 0, V4L2_STD_PAL_BG|V4L2_STD_NTSC_M, 0, V4L2_IN_CAP_STD },
  52. { AUX3_YC, "AUX3 S-Video", V4L2_INPUT_TYPE_CAMERA, 4, 0, V4L2_STD_PAL_BG|V4L2_STD_NTSC_M, 0, V4L2_IN_CAP_STD },
  53. };
  54. /* this array holds the information, which port of the saa7146 each
  55. input actually uses. the mxb uses port 0 for every input */
  56. static struct {
  57. int hps_source;
  58. int hps_sync;
  59. } input_port_selection[MXB_INPUTS] = {
  60. { SAA7146_HPS_SOURCE_PORT_A, SAA7146_HPS_SYNC_PORT_A },
  61. { SAA7146_HPS_SOURCE_PORT_A, SAA7146_HPS_SYNC_PORT_A },
  62. { SAA7146_HPS_SOURCE_PORT_A, SAA7146_HPS_SYNC_PORT_A },
  63. { SAA7146_HPS_SOURCE_PORT_A, SAA7146_HPS_SYNC_PORT_A },
  64. };
  65. /* this array holds the information of the audio source (mxb_audios),
  66. which has to be switched corresponding to the video source (mxb_channels) */
  67. static int video_audio_connect[MXB_INPUTS] =
  68. { 0, 1, 3, 3 };
  69. struct mxb_routing {
  70. u32 input;
  71. u32 output;
  72. };
  73. /* These are the necessary input-output-pins for bringing one audio source
  74. (see above) to the CD-output. Note that gain is set to 0 in this table. */
  75. static struct mxb_routing TEA6420_cd[MXB_AUDIOS + 1][2] = {
  76. { { 1, 1 }, { 1, 1 } }, /* Tuner */
  77. { { 5, 1 }, { 6, 1 } }, /* AUX 1 */
  78. { { 4, 1 }, { 6, 1 } }, /* AUX 2 */
  79. { { 3, 1 }, { 6, 1 } }, /* AUX 3 */
  80. { { 1, 1 }, { 3, 1 } }, /* Radio */
  81. { { 1, 1 }, { 2, 1 } }, /* CD-Rom */
  82. { { 6, 1 }, { 6, 1 } } /* Mute */
  83. };
  84. /* These are the necessary input-output-pins for bringing one audio source
  85. (see above) to the line-output. Note that gain is set to 0 in this table. */
  86. static struct mxb_routing TEA6420_line[MXB_AUDIOS + 1][2] = {
  87. { { 2, 3 }, { 1, 2 } },
  88. { { 5, 3 }, { 6, 2 } },
  89. { { 4, 3 }, { 6, 2 } },
  90. { { 3, 3 }, { 6, 2 } },
  91. { { 2, 3 }, { 3, 2 } },
  92. { { 2, 3 }, { 2, 2 } },
  93. { { 6, 3 }, { 6, 2 } } /* Mute */
  94. };
  95. #define MAXCONTROLS 1
  96. static struct v4l2_queryctrl mxb_controls[] = {
  97. { V4L2_CID_AUDIO_MUTE, V4L2_CTRL_TYPE_BOOLEAN, "Mute", 0, 1, 1, 0, 0 },
  98. };
  99. struct mxb
  100. {
  101. struct video_device *video_dev;
  102. struct video_device *vbi_dev;
  103. struct i2c_adapter i2c_adapter;
  104. struct v4l2_subdev *saa7111a;
  105. struct v4l2_subdev *tda9840;
  106. struct v4l2_subdev *tea6415c;
  107. struct v4l2_subdev *tuner;
  108. struct v4l2_subdev *tea6420_1;
  109. struct v4l2_subdev *tea6420_2;
  110. int cur_mode; /* current audio mode (mono, stereo, ...) */
  111. int cur_input; /* current input */
  112. int cur_mute; /* current mute status */
  113. struct v4l2_frequency cur_freq; /* current frequency the tuner is tuned to */
  114. };
  115. #define saa7111a_call(mxb, o, f, args...) \
  116. v4l2_subdev_call(mxb->saa7111a, o, f, ##args)
  117. #define tda9840_call(mxb, o, f, args...) \
  118. v4l2_subdev_call(mxb->tda9840, o, f, ##args)
  119. #define tea6415c_call(mxb, o, f, args...) \
  120. v4l2_subdev_call(mxb->tea6415c, o, f, ##args)
  121. #define tuner_call(mxb, o, f, args...) \
  122. v4l2_subdev_call(mxb->tuner, o, f, ##args)
  123. #define call_all(dev, o, f, args...) \
  124. v4l2_device_call_until_err(&dev->v4l2_dev, 0, o, f, ##args)
  125. static inline void tea6420_route_cd(struct mxb *mxb, int idx)
  126. {
  127. v4l2_subdev_call(mxb->tea6420_1, audio, s_routing,
  128. TEA6420_cd[idx][0].input, TEA6420_cd[idx][0].output, 0);
  129. v4l2_subdev_call(mxb->tea6420_2, audio, s_routing,
  130. TEA6420_cd[idx][1].input, TEA6420_cd[idx][1].output, 0);
  131. }
  132. static inline void tea6420_route_line(struct mxb *mxb, int idx)
  133. {
  134. v4l2_subdev_call(mxb->tea6420_1, audio, s_routing,
  135. TEA6420_line[idx][0].input, TEA6420_line[idx][0].output, 0);
  136. v4l2_subdev_call(mxb->tea6420_2, audio, s_routing,
  137. TEA6420_line[idx][1].input, TEA6420_line[idx][1].output, 0);
  138. }
  139. static struct saa7146_extension extension;
  140. static int mxb_probe(struct saa7146_dev *dev)
  141. {
  142. struct mxb *mxb = NULL;
  143. mxb = kzalloc(sizeof(struct mxb), GFP_KERNEL);
  144. if (mxb == NULL) {
  145. DEB_D("not enough kernel memory\n");
  146. return -ENOMEM;
  147. }
  148. snprintf(mxb->i2c_adapter.name, sizeof(mxb->i2c_adapter.name), "mxb%d", mxb_num);
  149. saa7146_i2c_adapter_prepare(dev, &mxb->i2c_adapter, SAA7146_I2C_BUS_BIT_RATE_480);
  150. if (i2c_add_adapter(&mxb->i2c_adapter) < 0) {
  151. DEB_S("cannot register i2c-device. skipping.\n");
  152. kfree(mxb);
  153. return -EFAULT;
  154. }
  155. mxb->saa7111a = v4l2_i2c_new_subdev(&dev->v4l2_dev, &mxb->i2c_adapter,
  156. "saa7111", I2C_SAA7111A, NULL);
  157. mxb->tea6420_1 = v4l2_i2c_new_subdev(&dev->v4l2_dev, &mxb->i2c_adapter,
  158. "tea6420", I2C_TEA6420_1, NULL);
  159. mxb->tea6420_2 = v4l2_i2c_new_subdev(&dev->v4l2_dev, &mxb->i2c_adapter,
  160. "tea6420", I2C_TEA6420_2, NULL);
  161. mxb->tea6415c = v4l2_i2c_new_subdev(&dev->v4l2_dev, &mxb->i2c_adapter,
  162. "tea6415c", I2C_TEA6415C, NULL);
  163. mxb->tda9840 = v4l2_i2c_new_subdev(&dev->v4l2_dev, &mxb->i2c_adapter,
  164. "tda9840", I2C_TDA9840, NULL);
  165. mxb->tuner = v4l2_i2c_new_subdev(&dev->v4l2_dev, &mxb->i2c_adapter,
  166. "tuner", I2C_TUNER, NULL);
  167. /* check if all devices are present */
  168. if (!mxb->tea6420_1 || !mxb->tea6420_2 || !mxb->tea6415c ||
  169. !mxb->tda9840 || !mxb->saa7111a || !mxb->tuner) {
  170. pr_err("did not find all i2c devices. aborting\n");
  171. i2c_del_adapter(&mxb->i2c_adapter);
  172. kfree(mxb);
  173. return -ENODEV;
  174. }
  175. /* all devices are present, probe was successful */
  176. /* we store the pointer in our private data field */
  177. dev->ext_priv = mxb;
  178. return 0;
  179. }
  180. /* some init data for the saa7740, the so-called 'sound arena module'.
  181. there are no specs available, so we simply use some init values */
  182. static struct {
  183. int length;
  184. char data[9];
  185. } mxb_saa7740_init[] = {
  186. { 3, { 0x80, 0x00, 0x00 } },{ 3, { 0x80, 0x89, 0x00 } },
  187. { 3, { 0x80, 0xb0, 0x0a } },{ 3, { 0x00, 0x00, 0x00 } },
  188. { 3, { 0x49, 0x00, 0x00 } },{ 3, { 0x4a, 0x00, 0x00 } },
  189. { 3, { 0x4b, 0x00, 0x00 } },{ 3, { 0x4c, 0x00, 0x00 } },
  190. { 3, { 0x4d, 0x00, 0x00 } },{ 3, { 0x4e, 0x00, 0x00 } },
  191. { 3, { 0x4f, 0x00, 0x00 } },{ 3, { 0x50, 0x00, 0x00 } },
  192. { 3, { 0x51, 0x00, 0x00 } },{ 3, { 0x52, 0x00, 0x00 } },
  193. { 3, { 0x53, 0x00, 0x00 } },{ 3, { 0x54, 0x00, 0x00 } },
  194. { 3, { 0x55, 0x00, 0x00 } },{ 3, { 0x56, 0x00, 0x00 } },
  195. { 3, { 0x57, 0x00, 0x00 } },{ 3, { 0x58, 0x00, 0x00 } },
  196. { 3, { 0x59, 0x00, 0x00 } },{ 3, { 0x5a, 0x00, 0x00 } },
  197. { 3, { 0x5b, 0x00, 0x00 } },{ 3, { 0x5c, 0x00, 0x00 } },
  198. { 3, { 0x5d, 0x00, 0x00 } },{ 3, { 0x5e, 0x00, 0x00 } },
  199. { 3, { 0x5f, 0x00, 0x00 } },{ 3, { 0x60, 0x00, 0x00 } },
  200. { 3, { 0x61, 0x00, 0x00 } },{ 3, { 0x62, 0x00, 0x00 } },
  201. { 3, { 0x63, 0x00, 0x00 } },{ 3, { 0x64, 0x00, 0x00 } },
  202. { 3, { 0x65, 0x00, 0x00 } },{ 3, { 0x66, 0x00, 0x00 } },
  203. { 3, { 0x67, 0x00, 0x00 } },{ 3, { 0x68, 0x00, 0x00 } },
  204. { 3, { 0x69, 0x00, 0x00 } },{ 3, { 0x6a, 0x00, 0x00 } },
  205. { 3, { 0x6b, 0x00, 0x00 } },{ 3, { 0x6c, 0x00, 0x00 } },
  206. { 3, { 0x6d, 0x00, 0x00 } },{ 3, { 0x6e, 0x00, 0x00 } },
  207. { 3, { 0x6f, 0x00, 0x00 } },{ 3, { 0x70, 0x00, 0x00 } },
  208. { 3, { 0x71, 0x00, 0x00 } },{ 3, { 0x72, 0x00, 0x00 } },
  209. { 3, { 0x73, 0x00, 0x00 } },{ 3, { 0x74, 0x00, 0x00 } },
  210. { 3, { 0x75, 0x00, 0x00 } },{ 3, { 0x76, 0x00, 0x00 } },
  211. { 3, { 0x77, 0x00, 0x00 } },{ 3, { 0x41, 0x00, 0x42 } },
  212. { 3, { 0x42, 0x10, 0x42 } },{ 3, { 0x43, 0x20, 0x42 } },
  213. { 3, { 0x44, 0x30, 0x42 } },{ 3, { 0x45, 0x00, 0x01 } },
  214. { 3, { 0x46, 0x00, 0x01 } },{ 3, { 0x47, 0x00, 0x01 } },
  215. { 3, { 0x48, 0x00, 0x01 } },
  216. { 9, { 0x01, 0x03, 0xc5, 0x5c, 0x7a, 0x85, 0x01, 0x00, 0x54 } },
  217. { 9, { 0x21, 0x03, 0xc5, 0x5c, 0x7a, 0x85, 0x01, 0x00, 0x54 } },
  218. { 9, { 0x09, 0x0b, 0xb4, 0x6b, 0x74, 0x85, 0x95, 0x00, 0x34 } },
  219. { 9, { 0x29, 0x0b, 0xb4, 0x6b, 0x74, 0x85, 0x95, 0x00, 0x34 } },
  220. { 9, { 0x11, 0x17, 0x43, 0x62, 0x68, 0x89, 0xd1, 0xff, 0xb0 } },
  221. { 9, { 0x31, 0x17, 0x43, 0x62, 0x68, 0x89, 0xd1, 0xff, 0xb0 } },
  222. { 9, { 0x19, 0x20, 0x62, 0x51, 0x5a, 0x95, 0x19, 0x01, 0x50 } },
  223. { 9, { 0x39, 0x20, 0x62, 0x51, 0x5a, 0x95, 0x19, 0x01, 0x50 } },
  224. { 9, { 0x05, 0x3e, 0xd2, 0x69, 0x4e, 0x9a, 0x51, 0x00, 0xf0 } },
  225. { 9, { 0x25, 0x3e, 0xd2, 0x69, 0x4e, 0x9a, 0x51, 0x00, 0xf0 } },
  226. { 9, { 0x0d, 0x3d, 0xa1, 0x40, 0x7d, 0x9f, 0x29, 0xfe, 0x14 } },
  227. { 9, { 0x2d, 0x3d, 0xa1, 0x40, 0x7d, 0x9f, 0x29, 0xfe, 0x14 } },
  228. { 9, { 0x15, 0x73, 0xa1, 0x50, 0x5d, 0xa6, 0xf5, 0xfe, 0x38 } },
  229. { 9, { 0x35, 0x73, 0xa1, 0x50, 0x5d, 0xa6, 0xf5, 0xfe, 0x38 } },
  230. { 9, { 0x1d, 0xed, 0xd0, 0x68, 0x29, 0xb4, 0xe1, 0x00, 0xb8 } },
  231. { 9, { 0x3d, 0xed, 0xd0, 0x68, 0x29, 0xb4, 0xe1, 0x00, 0xb8 } },
  232. { 3, { 0x80, 0xb3, 0x0a } },
  233. {-1, { 0 } }
  234. };
  235. /* bring hardware to a sane state. this has to be done, just in case someone
  236. wants to capture from this device before it has been properly initialized.
  237. the capture engine would badly fail, because no valid signal arrives on the
  238. saa7146, thus leading to timeouts and stuff. */
  239. static int mxb_init_done(struct saa7146_dev* dev)
  240. {
  241. struct mxb* mxb = (struct mxb*)dev->ext_priv;
  242. struct i2c_msg msg;
  243. struct tuner_setup tun_setup;
  244. v4l2_std_id std = V4L2_STD_PAL_BG;
  245. int i = 0, err = 0;
  246. /* select video mode in saa7111a */
  247. saa7111a_call(mxb, core, s_std, std);
  248. /* select tuner-output on saa7111a */
  249. i = 0;
  250. saa7111a_call(mxb, video, s_routing, SAA7115_COMPOSITE0,
  251. SAA7111_FMT_CCIR, 0);
  252. /* select a tuner type */
  253. tun_setup.mode_mask = T_ANALOG_TV;
  254. tun_setup.addr = ADDR_UNSET;
  255. tun_setup.type = TUNER_PHILIPS_PAL;
  256. tuner_call(mxb, tuner, s_type_addr, &tun_setup);
  257. /* tune in some frequency on tuner */
  258. mxb->cur_freq.tuner = 0;
  259. mxb->cur_freq.type = V4L2_TUNER_ANALOG_TV;
  260. mxb->cur_freq.frequency = freq;
  261. tuner_call(mxb, tuner, s_frequency, &mxb->cur_freq);
  262. /* set a default video standard */
  263. tuner_call(mxb, core, s_std, std);
  264. /* mute audio on tea6420s */
  265. tea6420_route_line(mxb, 6);
  266. tea6420_route_cd(mxb, 6);
  267. /* switch to tuner-channel on tea6415c */
  268. tea6415c_call(mxb, video, s_routing, 3, 17, 0);
  269. /* select tuner-output on multicable on tea6415c */
  270. tea6415c_call(mxb, video, s_routing, 3, 13, 0);
  271. /* the rest for mxb */
  272. mxb->cur_input = 0;
  273. mxb->cur_mute = 1;
  274. mxb->cur_mode = V4L2_TUNER_MODE_STEREO;
  275. /* check if the saa7740 (aka 'sound arena module') is present
  276. on the mxb. if so, we must initialize it. due to lack of
  277. informations about the saa7740, the values were reverse
  278. engineered. */
  279. msg.addr = 0x1b;
  280. msg.flags = 0;
  281. msg.len = mxb_saa7740_init[0].length;
  282. msg.buf = &mxb_saa7740_init[0].data[0];
  283. err = i2c_transfer(&mxb->i2c_adapter, &msg, 1);
  284. if (err == 1) {
  285. /* the sound arena module is a pos, that's probably the reason
  286. philips refuses to hand out a datasheet for the saa7740...
  287. it seems to screw up the i2c bus, so we disable fast irq
  288. based i2c transactions here and rely on the slow and safe
  289. polling method ... */
  290. extension.flags &= ~SAA7146_USE_I2C_IRQ;
  291. for (i = 1; ; i++) {
  292. if (-1 == mxb_saa7740_init[i].length)
  293. break;
  294. msg.len = mxb_saa7740_init[i].length;
  295. msg.buf = &mxb_saa7740_init[i].data[0];
  296. err = i2c_transfer(&mxb->i2c_adapter, &msg, 1);
  297. if (err != 1) {
  298. DEB_D("failed to initialize 'sound arena module'\n");
  299. goto err;
  300. }
  301. }
  302. pr_info("'sound arena module' detected\n");
  303. }
  304. err:
  305. /* the rest for saa7146: you should definitely set some basic values
  306. for the input-port handling of the saa7146. */
  307. /* ext->saa has been filled by the core driver */
  308. /* some stuff is done via variables */
  309. saa7146_set_hps_source_and_sync(dev, input_port_selection[mxb->cur_input].hps_source,
  310. input_port_selection[mxb->cur_input].hps_sync);
  311. /* some stuff is done via direct write to the registers */
  312. /* this is ugly, but because of the fact that this is completely
  313. hardware dependend, it should be done directly... */
  314. saa7146_write(dev, DD1_STREAM_B, 0x00000000);
  315. saa7146_write(dev, DD1_INIT, 0x02000200);
  316. saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
  317. return 0;
  318. }
  319. /* interrupt-handler. this gets called when irq_mask is != 0.
  320. it must clear the interrupt-bits in irq_mask it has handled */
  321. /*
  322. void mxb_irq_bh(struct saa7146_dev* dev, u32* irq_mask)
  323. {
  324. struct mxb* mxb = (struct mxb*)dev->ext_priv;
  325. }
  326. */
  327. static int vidioc_queryctrl(struct file *file, void *fh, struct v4l2_queryctrl *qc)
  328. {
  329. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  330. int i;
  331. for (i = MAXCONTROLS - 1; i >= 0; i--) {
  332. if (mxb_controls[i].id == qc->id) {
  333. *qc = mxb_controls[i];
  334. DEB_D("VIDIOC_QUERYCTRL %d\n", qc->id);
  335. return 0;
  336. }
  337. }
  338. return dev->ext_vv_data->core_ops->vidioc_queryctrl(file, fh, qc);
  339. }
  340. static int vidioc_g_ctrl(struct file *file, void *fh, struct v4l2_control *vc)
  341. {
  342. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  343. struct mxb *mxb = (struct mxb *)dev->ext_priv;
  344. int i;
  345. for (i = MAXCONTROLS - 1; i >= 0; i--) {
  346. if (mxb_controls[i].id == vc->id)
  347. break;
  348. }
  349. if (i < 0)
  350. return dev->ext_vv_data->core_ops->vidioc_g_ctrl(file, fh, vc);
  351. if (vc->id == V4L2_CID_AUDIO_MUTE) {
  352. vc->value = mxb->cur_mute;
  353. DEB_D("VIDIOC_G_CTRL V4L2_CID_AUDIO_MUTE:%d\n", vc->value);
  354. return 0;
  355. }
  356. DEB_EE("VIDIOC_G_CTRL V4L2_CID_AUDIO_MUTE:%d\n", vc->value);
  357. return 0;
  358. }
  359. static int vidioc_s_ctrl(struct file *file, void *fh, struct v4l2_control *vc)
  360. {
  361. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  362. struct mxb *mxb = (struct mxb *)dev->ext_priv;
  363. int i = 0;
  364. for (i = MAXCONTROLS - 1; i >= 0; i--) {
  365. if (mxb_controls[i].id == vc->id)
  366. break;
  367. }
  368. if (i < 0)
  369. return dev->ext_vv_data->core_ops->vidioc_s_ctrl(file, fh, vc);
  370. if (vc->id == V4L2_CID_AUDIO_MUTE) {
  371. mxb->cur_mute = vc->value;
  372. /* switch the audio-source */
  373. tea6420_route_line(mxb, vc->value ? 6 :
  374. video_audio_connect[mxb->cur_input]);
  375. DEB_EE("VIDIOC_S_CTRL, V4L2_CID_AUDIO_MUTE: %d\n", vc->value);
  376. }
  377. return 0;
  378. }
  379. static int vidioc_enum_input(struct file *file, void *fh, struct v4l2_input *i)
  380. {
  381. DEB_EE("VIDIOC_ENUMINPUT %d\n", i->index);
  382. if (i->index >= MXB_INPUTS)
  383. return -EINVAL;
  384. memcpy(i, &mxb_inputs[i->index], sizeof(struct v4l2_input));
  385. return 0;
  386. }
  387. static int vidioc_g_input(struct file *file, void *fh, unsigned int *i)
  388. {
  389. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  390. struct mxb *mxb = (struct mxb *)dev->ext_priv;
  391. *i = mxb->cur_input;
  392. DEB_EE("VIDIOC_G_INPUT %d\n", *i);
  393. return 0;
  394. }
  395. static int vidioc_s_input(struct file *file, void *fh, unsigned int input)
  396. {
  397. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  398. struct mxb *mxb = (struct mxb *)dev->ext_priv;
  399. int err = 0;
  400. int i = 0;
  401. DEB_EE("VIDIOC_S_INPUT %d\n", input);
  402. if (input >= MXB_INPUTS)
  403. return -EINVAL;
  404. mxb->cur_input = input;
  405. saa7146_set_hps_source_and_sync(dev, input_port_selection[input].hps_source,
  406. input_port_selection[input].hps_sync);
  407. /* prepare switching of tea6415c and saa7111a;
  408. have a look at the 'background'-file for further informations */
  409. switch (input) {
  410. case TUNER:
  411. i = SAA7115_COMPOSITE0;
  412. err = tea6415c_call(mxb, video, s_routing, 3, 17, 0);
  413. /* connect tuner-output always to multicable */
  414. if (!err)
  415. err = tea6415c_call(mxb, video, s_routing, 3, 13, 0);
  416. break;
  417. case AUX3_YC:
  418. /* nothing to be done here. aux3_yc is
  419. directly connected to the saa711a */
  420. i = SAA7115_SVIDEO1;
  421. break;
  422. case AUX3:
  423. /* nothing to be done here. aux3 is
  424. directly connected to the saa711a */
  425. i = SAA7115_COMPOSITE1;
  426. break;
  427. case AUX1:
  428. i = SAA7115_COMPOSITE0;
  429. err = tea6415c_call(mxb, video, s_routing, 1, 17, 0);
  430. break;
  431. }
  432. if (err)
  433. return err;
  434. /* switch video in saa7111a */
  435. if (saa7111a_call(mxb, video, s_routing, i, SAA7111_FMT_CCIR, 0))
  436. pr_err("VIDIOC_S_INPUT: could not address saa7111a\n");
  437. /* switch the audio-source only if necessary */
  438. if (0 == mxb->cur_mute)
  439. tea6420_route_line(mxb, video_audio_connect[input]);
  440. return 0;
  441. }
  442. static int vidioc_g_tuner(struct file *file, void *fh, struct v4l2_tuner *t)
  443. {
  444. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  445. struct mxb *mxb = (struct mxb *)dev->ext_priv;
  446. if (t->index) {
  447. DEB_D("VIDIOC_G_TUNER: channel %d does not have a tuner attached\n",
  448. t->index);
  449. return -EINVAL;
  450. }
  451. DEB_EE("VIDIOC_G_TUNER: %d\n", t->index);
  452. memset(t, 0, sizeof(*t));
  453. strlcpy(t->name, "TV Tuner", sizeof(t->name));
  454. t->type = V4L2_TUNER_ANALOG_TV;
  455. t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO |
  456. V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
  457. t->audmode = mxb->cur_mode;
  458. return call_all(dev, tuner, g_tuner, t);
  459. }
  460. static int vidioc_s_tuner(struct file *file, void *fh, struct v4l2_tuner *t)
  461. {
  462. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  463. struct mxb *mxb = (struct mxb *)dev->ext_priv;
  464. if (t->index) {
  465. DEB_D("VIDIOC_S_TUNER: channel %d does not have a tuner attached\n",
  466. t->index);
  467. return -EINVAL;
  468. }
  469. mxb->cur_mode = t->audmode;
  470. return call_all(dev, tuner, s_tuner, t);
  471. }
  472. static int vidioc_g_frequency(struct file *file, void *fh, struct v4l2_frequency *f)
  473. {
  474. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  475. struct mxb *mxb = (struct mxb *)dev->ext_priv;
  476. if (mxb->cur_input) {
  477. DEB_D("VIDIOC_G_FREQ: channel %d does not have a tuner!\n",
  478. mxb->cur_input);
  479. return -EINVAL;
  480. }
  481. *f = mxb->cur_freq;
  482. DEB_EE("VIDIOC_G_FREQ: freq:0x%08x\n", mxb->cur_freq.frequency);
  483. return 0;
  484. }
  485. static int vidioc_s_frequency(struct file *file, void *fh, struct v4l2_frequency *f)
  486. {
  487. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  488. struct mxb *mxb = (struct mxb *)dev->ext_priv;
  489. struct saa7146_vv *vv = dev->vv_data;
  490. if (f->tuner)
  491. return -EINVAL;
  492. if (V4L2_TUNER_ANALOG_TV != f->type)
  493. return -EINVAL;
  494. if (mxb->cur_input) {
  495. DEB_D("VIDIOC_S_FREQ: channel %d does not have a tuner!\n",
  496. mxb->cur_input);
  497. return -EINVAL;
  498. }
  499. mxb->cur_freq = *f;
  500. DEB_EE("VIDIOC_S_FREQUENCY: freq:0x%08x\n", mxb->cur_freq.frequency);
  501. /* tune in desired frequency */
  502. tuner_call(mxb, tuner, s_frequency, &mxb->cur_freq);
  503. /* hack: changing the frequency should invalidate the vbi-counter (=> alevt) */
  504. spin_lock(&dev->slock);
  505. vv->vbi_fieldcount = 0;
  506. spin_unlock(&dev->slock);
  507. return 0;
  508. }
  509. static int vidioc_g_audio(struct file *file, void *fh, struct v4l2_audio *a)
  510. {
  511. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  512. struct mxb *mxb = (struct mxb *)dev->ext_priv;
  513. if (a->index > MXB_INPUTS) {
  514. DEB_D("VIDIOC_G_AUDIO %d out of range\n", a->index);
  515. return -EINVAL;
  516. }
  517. DEB_EE("VIDIOC_G_AUDIO %d\n", a->index);
  518. memcpy(a, &mxb_audios[video_audio_connect[mxb->cur_input]], sizeof(struct v4l2_audio));
  519. return 0;
  520. }
  521. static int vidioc_s_audio(struct file *file, void *fh, struct v4l2_audio *a)
  522. {
  523. DEB_D("VIDIOC_S_AUDIO %d\n", a->index);
  524. return 0;
  525. }
  526. #ifdef CONFIG_VIDEO_ADV_DEBUG
  527. static int vidioc_g_register(struct file *file, void *fh, struct v4l2_dbg_register *reg)
  528. {
  529. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  530. return call_all(dev, core, g_register, reg);
  531. }
  532. static int vidioc_s_register(struct file *file, void *fh, struct v4l2_dbg_register *reg)
  533. {
  534. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  535. return call_all(dev, core, s_register, reg);
  536. }
  537. #endif
  538. static long vidioc_default(struct file *file, void *fh, bool valid_prio,
  539. int cmd, void *arg)
  540. {
  541. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  542. struct mxb *mxb = (struct mxb *)dev->ext_priv;
  543. switch (cmd) {
  544. case MXB_S_AUDIO_CD:
  545. {
  546. int i = *(int *)arg;
  547. if (i < 0 || i >= MXB_AUDIOS) {
  548. DEB_D("invalid argument to MXB_S_AUDIO_CD: i:%d\n", i);
  549. return -EINVAL;
  550. }
  551. DEB_EE("MXB_S_AUDIO_CD: i:%d\n", i);
  552. tea6420_route_cd(mxb, i);
  553. return 0;
  554. }
  555. case MXB_S_AUDIO_LINE:
  556. {
  557. int i = *(int *)arg;
  558. if (i < 0 || i >= MXB_AUDIOS) {
  559. DEB_D("invalid argument to MXB_S_AUDIO_LINE: i:%d\n",
  560. i);
  561. return -EINVAL;
  562. }
  563. DEB_EE("MXB_S_AUDIO_LINE: i:%d\n", i);
  564. tea6420_route_line(mxb, i);
  565. return 0;
  566. }
  567. default:
  568. /*
  569. DEB2(pr_err("does not handle this ioctl\n"));
  570. */
  571. return -ENOIOCTLCMD;
  572. }
  573. return 0;
  574. }
  575. static struct saa7146_ext_vv vv_data;
  576. /* this function only gets called when the probing was successful */
  577. static int mxb_attach(struct saa7146_dev *dev, struct saa7146_pci_extension_data *info)
  578. {
  579. struct mxb *mxb;
  580. DEB_EE("dev:%p\n", dev);
  581. saa7146_vv_init(dev, &vv_data);
  582. if (mxb_probe(dev)) {
  583. saa7146_vv_release(dev);
  584. return -1;
  585. }
  586. mxb = (struct mxb *)dev->ext_priv;
  587. vv_data.ops.vidioc_queryctrl = vidioc_queryctrl;
  588. vv_data.ops.vidioc_g_ctrl = vidioc_g_ctrl;
  589. vv_data.ops.vidioc_s_ctrl = vidioc_s_ctrl;
  590. vv_data.ops.vidioc_enum_input = vidioc_enum_input;
  591. vv_data.ops.vidioc_g_input = vidioc_g_input;
  592. vv_data.ops.vidioc_s_input = vidioc_s_input;
  593. vv_data.ops.vidioc_g_tuner = vidioc_g_tuner;
  594. vv_data.ops.vidioc_s_tuner = vidioc_s_tuner;
  595. vv_data.ops.vidioc_g_frequency = vidioc_g_frequency;
  596. vv_data.ops.vidioc_s_frequency = vidioc_s_frequency;
  597. vv_data.ops.vidioc_g_audio = vidioc_g_audio;
  598. vv_data.ops.vidioc_s_audio = vidioc_s_audio;
  599. #ifdef CONFIG_VIDEO_ADV_DEBUG
  600. vv_data.ops.vidioc_g_register = vidioc_g_register;
  601. vv_data.ops.vidioc_s_register = vidioc_s_register;
  602. #endif
  603. vv_data.ops.vidioc_default = vidioc_default;
  604. if (saa7146_register_device(&mxb->video_dev, dev, "mxb", VFL_TYPE_GRABBER)) {
  605. ERR("cannot register capture v4l2 device. skipping.\n");
  606. saa7146_vv_release(dev);
  607. return -1;
  608. }
  609. /* initialization stuff (vbi) (only for revision > 0 and for extensions which want it)*/
  610. if (MXB_BOARD_CAN_DO_VBI(dev)) {
  611. if (saa7146_register_device(&mxb->vbi_dev, dev, "mxb", VFL_TYPE_VBI)) {
  612. ERR("cannot register vbi v4l2 device. skipping.\n");
  613. }
  614. }
  615. pr_info("found Multimedia eXtension Board #%d\n", mxb_num);
  616. mxb_num++;
  617. mxb_init_done(dev);
  618. return 0;
  619. }
  620. static int mxb_detach(struct saa7146_dev *dev)
  621. {
  622. struct mxb *mxb = (struct mxb *)dev->ext_priv;
  623. DEB_EE("dev:%p\n", dev);
  624. saa7146_unregister_device(&mxb->video_dev,dev);
  625. if (MXB_BOARD_CAN_DO_VBI(dev))
  626. saa7146_unregister_device(&mxb->vbi_dev, dev);
  627. saa7146_vv_release(dev);
  628. mxb_num--;
  629. i2c_del_adapter(&mxb->i2c_adapter);
  630. kfree(mxb);
  631. return 0;
  632. }
  633. static int std_callback(struct saa7146_dev *dev, struct saa7146_standard *standard)
  634. {
  635. struct mxb *mxb = (struct mxb *)dev->ext_priv;
  636. if (V4L2_STD_PAL_I == standard->id) {
  637. v4l2_std_id std = V4L2_STD_PAL_I;
  638. DEB_D("VIDIOC_S_STD: setting mxb for PAL_I\n");
  639. /* set the 7146 gpio register -- I don't know what this does exactly */
  640. saa7146_write(dev, GPIO_CTRL, 0x00404050);
  641. /* unset the 7111 gpio register -- I don't know what this does exactly */
  642. saa7111a_call(mxb, core, s_gpio, 0);
  643. tuner_call(mxb, core, s_std, std);
  644. } else {
  645. v4l2_std_id std = V4L2_STD_PAL_BG;
  646. DEB_D("VIDIOC_S_STD: setting mxb for PAL/NTSC/SECAM\n");
  647. /* set the 7146 gpio register -- I don't know what this does exactly */
  648. saa7146_write(dev, GPIO_CTRL, 0x00404050);
  649. /* set the 7111 gpio register -- I don't know what this does exactly */
  650. saa7111a_call(mxb, core, s_gpio, 1);
  651. tuner_call(mxb, core, s_std, std);
  652. }
  653. return 0;
  654. }
  655. static struct saa7146_standard standard[] = {
  656. {
  657. .name = "PAL-BG", .id = V4L2_STD_PAL_BG,
  658. .v_offset = 0x17, .v_field = 288,
  659. .h_offset = 0x14, .h_pixels = 680,
  660. .v_max_out = 576, .h_max_out = 768,
  661. }, {
  662. .name = "PAL-I", .id = V4L2_STD_PAL_I,
  663. .v_offset = 0x17, .v_field = 288,
  664. .h_offset = 0x14, .h_pixels = 680,
  665. .v_max_out = 576, .h_max_out = 768,
  666. }, {
  667. .name = "NTSC", .id = V4L2_STD_NTSC,
  668. .v_offset = 0x16, .v_field = 240,
  669. .h_offset = 0x06, .h_pixels = 708,
  670. .v_max_out = 480, .h_max_out = 640,
  671. }, {
  672. .name = "SECAM", .id = V4L2_STD_SECAM,
  673. .v_offset = 0x14, .v_field = 288,
  674. .h_offset = 0x14, .h_pixels = 720,
  675. .v_max_out = 576, .h_max_out = 768,
  676. }
  677. };
  678. static struct saa7146_pci_extension_data mxb = {
  679. .ext_priv = "Multimedia eXtension Board",
  680. .ext = &extension,
  681. };
  682. static struct pci_device_id pci_tbl[] = {
  683. {
  684. .vendor = PCI_VENDOR_ID_PHILIPS,
  685. .device = PCI_DEVICE_ID_PHILIPS_SAA7146,
  686. .subvendor = 0x0000,
  687. .subdevice = 0x0000,
  688. .driver_data = (unsigned long)&mxb,
  689. }, {
  690. .vendor = 0,
  691. }
  692. };
  693. MODULE_DEVICE_TABLE(pci, pci_tbl);
  694. static struct saa7146_ext_vv vv_data = {
  695. .inputs = MXB_INPUTS,
  696. .capabilities = V4L2_CAP_TUNER | V4L2_CAP_VBI_CAPTURE,
  697. .stds = &standard[0],
  698. .num_stds = sizeof(standard)/sizeof(struct saa7146_standard),
  699. .std_callback = &std_callback,
  700. };
  701. static struct saa7146_extension extension = {
  702. .name = MXB_IDENTIFIER,
  703. .flags = SAA7146_USE_I2C_IRQ,
  704. .pci_tbl = &pci_tbl[0],
  705. .module = THIS_MODULE,
  706. .attach = mxb_attach,
  707. .detach = mxb_detach,
  708. .irq_mask = 0,
  709. .irq_func = NULL,
  710. };
  711. static int __init mxb_init_module(void)
  712. {
  713. if (saa7146_register_extension(&extension)) {
  714. DEB_S("failed to register extension\n");
  715. return -ENODEV;
  716. }
  717. return 0;
  718. }
  719. static void __exit mxb_cleanup_module(void)
  720. {
  721. saa7146_unregister_extension(&extension);
  722. }
  723. module_init(mxb_init_module);
  724. module_exit(mxb_cleanup_module);
  725. MODULE_DESCRIPTION("video4linux-2 driver for the Siemens-Nixdorf 'Multimedia eXtension board'");
  726. MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
  727. MODULE_LICENSE("GPL");