pvrusb2-audio.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. *
  3. *
  4. * Copyright (C) 2005 Mike Isely <isely@pobox.com>
  5. * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include "pvrusb2-audio.h"
  22. #include "pvrusb2-hdw-internal.h"
  23. #include "pvrusb2-debug.h"
  24. #include <linux/videodev2.h>
  25. #include <media/msp3400.h>
  26. #include <media/v4l2-common.h>
  27. struct routing_scheme {
  28. const int *def;
  29. unsigned int cnt;
  30. };
  31. static const int routing_scheme0[] = {
  32. [PVR2_CVAL_INPUT_TV] = MSP_INPUT_DEFAULT,
  33. [PVR2_CVAL_INPUT_RADIO] = MSP_INPUT(MSP_IN_SCART2,
  34. MSP_IN_TUNER1,
  35. MSP_DSP_IN_SCART,
  36. MSP_DSP_IN_SCART),
  37. [PVR2_CVAL_INPUT_COMPOSITE] = MSP_INPUT(MSP_IN_SCART1,
  38. MSP_IN_TUNER1,
  39. MSP_DSP_IN_SCART,
  40. MSP_DSP_IN_SCART),
  41. [PVR2_CVAL_INPUT_SVIDEO] = MSP_INPUT(MSP_IN_SCART1,
  42. MSP_IN_TUNER1,
  43. MSP_DSP_IN_SCART,
  44. MSP_DSP_IN_SCART),
  45. };
  46. static const struct routing_scheme routing_schemes[] = {
  47. [PVR2_ROUTING_SCHEME_HAUPPAUGE] = {
  48. .def = routing_scheme0,
  49. .cnt = ARRAY_SIZE(routing_scheme0),
  50. },
  51. };
  52. struct pvr2_msp3400_handler {
  53. struct pvr2_hdw *hdw;
  54. struct pvr2_i2c_client *client;
  55. struct pvr2_i2c_handler i2c_handler;
  56. unsigned long stale_mask;
  57. };
  58. /* This function selects the correct audio input source */
  59. static void set_stereo(struct pvr2_msp3400_handler *ctxt)
  60. {
  61. struct pvr2_hdw *hdw = ctxt->hdw;
  62. struct v4l2_routing route;
  63. const struct routing_scheme *sp;
  64. unsigned int sid = hdw->hdw_desc->signal_routing_scheme;
  65. pvr2_trace(PVR2_TRACE_CHIPS,"i2c msp3400 v4l2 set_stereo");
  66. if ((sid < ARRAY_SIZE(routing_schemes)) &&
  67. ((sp = routing_schemes + sid) != NULL) &&
  68. (hdw->input_val >= 0) &&
  69. (hdw->input_val < sp->cnt)) {
  70. route.input = sp->def[hdw->input_val];
  71. } else {
  72. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  73. "*** WARNING *** i2c msp3400 v4l2 set_stereo:"
  74. " Invalid routing scheme (%u) and/or input (%d)",
  75. sid,hdw->input_val);
  76. return;
  77. }
  78. route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
  79. pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_S_AUDIO_ROUTING,&route);
  80. }
  81. static int check_stereo(struct pvr2_msp3400_handler *ctxt)
  82. {
  83. struct pvr2_hdw *hdw = ctxt->hdw;
  84. return hdw->input_dirty;
  85. }
  86. struct pvr2_msp3400_ops {
  87. void (*update)(struct pvr2_msp3400_handler *);
  88. int (*check)(struct pvr2_msp3400_handler *);
  89. };
  90. static const struct pvr2_msp3400_ops msp3400_ops[] = {
  91. { .update = set_stereo, .check = check_stereo},
  92. };
  93. static int msp3400_check(struct pvr2_msp3400_handler *ctxt)
  94. {
  95. unsigned long msk;
  96. unsigned int idx;
  97. for (idx = 0; idx < ARRAY_SIZE(msp3400_ops); idx++) {
  98. msk = 1 << idx;
  99. if (ctxt->stale_mask & msk) continue;
  100. if (msp3400_ops[idx].check(ctxt)) {
  101. ctxt->stale_mask |= msk;
  102. }
  103. }
  104. return ctxt->stale_mask != 0;
  105. }
  106. static void msp3400_update(struct pvr2_msp3400_handler *ctxt)
  107. {
  108. unsigned long msk;
  109. unsigned int idx;
  110. for (idx = 0; idx < ARRAY_SIZE(msp3400_ops); idx++) {
  111. msk = 1 << idx;
  112. if (!(ctxt->stale_mask & msk)) continue;
  113. ctxt->stale_mask &= ~msk;
  114. msp3400_ops[idx].update(ctxt);
  115. }
  116. }
  117. static void pvr2_msp3400_detach(struct pvr2_msp3400_handler *ctxt)
  118. {
  119. ctxt->client->handler = NULL;
  120. kfree(ctxt);
  121. }
  122. static unsigned int pvr2_msp3400_describe(struct pvr2_msp3400_handler *ctxt,
  123. char *buf,unsigned int cnt)
  124. {
  125. return scnprintf(buf,cnt,"handler: pvrusb2-audio v4l2");
  126. }
  127. static const struct pvr2_i2c_handler_functions msp3400_funcs = {
  128. .detach = (void (*)(void *))pvr2_msp3400_detach,
  129. .check = (int (*)(void *))msp3400_check,
  130. .update = (void (*)(void *))msp3400_update,
  131. .describe = (unsigned int (*)(void *,char *,unsigned int))pvr2_msp3400_describe,
  132. };
  133. int pvr2_i2c_msp3400_setup(struct pvr2_hdw *hdw,struct pvr2_i2c_client *cp)
  134. {
  135. struct pvr2_msp3400_handler *ctxt;
  136. if (cp->handler) return 0;
  137. ctxt = kzalloc(sizeof(*ctxt),GFP_KERNEL);
  138. if (!ctxt) return 0;
  139. ctxt->i2c_handler.func_data = ctxt;
  140. ctxt->i2c_handler.func_table = &msp3400_funcs;
  141. ctxt->client = cp;
  142. ctxt->hdw = hdw;
  143. ctxt->stale_mask = (1 << ARRAY_SIZE(msp3400_ops)) - 1;
  144. cp->handler = &ctxt->i2c_handler;
  145. pvr2_trace(PVR2_TRACE_CHIPS,"i2c 0x%x msp3400 V4L2 handler set up",
  146. cp->client->addr);
  147. return !0;
  148. }
  149. void pvr2_msp3400_subdev_update(struct pvr2_hdw *hdw, struct v4l2_subdev *sd)
  150. {
  151. if (hdw->input_dirty || hdw->force_dirty) {
  152. struct v4l2_routing route;
  153. const struct routing_scheme *sp;
  154. unsigned int sid = hdw->hdw_desc->signal_routing_scheme;
  155. pvr2_trace(PVR2_TRACE_CHIPS, "subdev msp3400 v4l2 set_stereo");
  156. if ((sid < ARRAY_SIZE(routing_schemes)) &&
  157. ((sp = routing_schemes + sid) != NULL) &&
  158. (hdw->input_val >= 0) &&
  159. (hdw->input_val < sp->cnt)) {
  160. route.input = sp->def[hdw->input_val];
  161. } else {
  162. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  163. "*** WARNING *** subdev msp3400 set_input:"
  164. " Invalid routing scheme (%u)"
  165. " and/or input (%d)",
  166. sid, hdw->input_val);
  167. return;
  168. }
  169. route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
  170. sd->ops->audio->s_routing(sd, &route);
  171. }
  172. }
  173. /*
  174. Stuff for Emacs to see, in order to encourage consistent editing style:
  175. *** Local Variables: ***
  176. *** mode: c ***
  177. *** fill-column: 70 ***
  178. *** tab-width: 8 ***
  179. *** c-basic-offset: 8 ***
  180. *** End: ***
  181. */