vmaster.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * Virtual master and slave controls
  3. *
  4. * Copyright (c) 2008 by Takashi Iwai <tiwai@suse.de>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, version 2.
  9. *
  10. */
  11. #include <linux/slab.h>
  12. #include <sound/core.h>
  13. #include <sound/control.h>
  14. /*
  15. * a subset of information returned via ctl info callback
  16. */
  17. struct link_ctl_info {
  18. int type; /* value type */
  19. int count; /* item count */
  20. int min_val, max_val; /* min, max values */
  21. };
  22. /*
  23. * link master - this contains a list of slave controls that are
  24. * identical types, i.e. info returns the same value type and value
  25. * ranges, but may have different number of counts.
  26. *
  27. * The master control is so far only mono volume/switch for simplicity.
  28. * The same value will be applied to all slaves.
  29. */
  30. struct link_master {
  31. struct list_head slaves;
  32. struct link_ctl_info info;
  33. int val; /* the master value */
  34. };
  35. /*
  36. * link slave - this contains a slave control element
  37. *
  38. * It fakes the control callbacsk with additional attenuation by the
  39. * master control. A slave may have either one or two channels.
  40. */
  41. struct link_slave {
  42. struct list_head list;
  43. struct link_master *master;
  44. struct link_ctl_info info;
  45. int vals[2]; /* current values */
  46. struct snd_kcontrol slave; /* the copy of original control entry */
  47. };
  48. /* get the slave ctl info and save the initial values */
  49. static int slave_init(struct link_slave *slave)
  50. {
  51. struct snd_ctl_elem_info *uinfo;
  52. struct snd_ctl_elem_value *uctl;
  53. int err, ch;
  54. if (slave->info.count)
  55. return 0; /* already initialized */
  56. uinfo = kmalloc(sizeof(*uinfo), GFP_KERNEL);
  57. if (!uinfo)
  58. return -ENOMEM;
  59. uinfo->id = slave->slave.id;
  60. err = slave->slave.info(&slave->slave, uinfo);
  61. if (err < 0) {
  62. kfree(uinfo);
  63. return err;
  64. }
  65. slave->info.type = uinfo->type;
  66. slave->info.count = uinfo->count;
  67. if (slave->info.count > 2 ||
  68. (slave->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER &&
  69. slave->info.type != SNDRV_CTL_ELEM_TYPE_BOOLEAN)) {
  70. snd_printk(KERN_ERR "invalid slave element\n");
  71. kfree(uinfo);
  72. return -EINVAL;
  73. }
  74. slave->info.min_val = uinfo->value.integer.min;
  75. slave->info.max_val = uinfo->value.integer.max;
  76. kfree(uinfo);
  77. uctl = kmalloc(sizeof(*uctl), GFP_KERNEL);
  78. if (!uctl)
  79. return -ENOMEM;
  80. uctl->id = slave->slave.id;
  81. err = slave->slave.get(&slave->slave, uctl);
  82. for (ch = 0; ch < slave->info.count; ch++)
  83. slave->vals[ch] = uctl->value.integer.value[ch];
  84. kfree(uctl);
  85. return 0;
  86. }
  87. /* initialize master volume */
  88. static int master_init(struct link_master *master)
  89. {
  90. struct link_slave *slave;
  91. if (master->info.count)
  92. return 0; /* already initialized */
  93. list_for_each_entry(slave, &master->slaves, list) {
  94. int err = slave_init(slave);
  95. if (err < 0)
  96. return err;
  97. master->info = slave->info;
  98. master->info.count = 1; /* always mono */
  99. /* set full volume as default (= no attenuation) */
  100. master->val = master->info.max_val;
  101. return 0;
  102. }
  103. return -ENOENT;
  104. }
  105. static int slave_get_val(struct link_slave *slave,
  106. struct snd_ctl_elem_value *ucontrol)
  107. {
  108. int err, ch;
  109. err = slave_init(slave);
  110. if (err < 0)
  111. return err;
  112. for (ch = 0; ch < slave->info.count; ch++)
  113. ucontrol->value.integer.value[ch] = slave->vals[ch];
  114. return 0;
  115. }
  116. static int slave_put_val(struct link_slave *slave,
  117. struct snd_ctl_elem_value *ucontrol)
  118. {
  119. int err, ch, vol;
  120. err = master_init(slave->master);
  121. if (err < 0)
  122. return err;
  123. switch (slave->info.type) {
  124. case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
  125. for (ch = 0; ch < slave->info.count; ch++)
  126. ucontrol->value.integer.value[ch] &=
  127. !!slave->master->val;
  128. break;
  129. case SNDRV_CTL_ELEM_TYPE_INTEGER:
  130. for (ch = 0; ch < slave->info.count; ch++) {
  131. /* max master volume is supposed to be 0 dB */
  132. vol = ucontrol->value.integer.value[ch];
  133. vol += slave->master->val - slave->master->info.max_val;
  134. if (vol < slave->info.min_val)
  135. vol = slave->info.min_val;
  136. else if (vol > slave->info.max_val)
  137. vol = slave->info.max_val;
  138. ucontrol->value.integer.value[ch] = vol;
  139. }
  140. break;
  141. }
  142. return slave->slave.put(&slave->slave, ucontrol);
  143. }
  144. /*
  145. * ctl callbacks for slaves
  146. */
  147. static int slave_info(struct snd_kcontrol *kcontrol,
  148. struct snd_ctl_elem_info *uinfo)
  149. {
  150. struct link_slave *slave = snd_kcontrol_chip(kcontrol);
  151. return slave->slave.info(&slave->slave, uinfo);
  152. }
  153. static int slave_get(struct snd_kcontrol *kcontrol,
  154. struct snd_ctl_elem_value *ucontrol)
  155. {
  156. struct link_slave *slave = snd_kcontrol_chip(kcontrol);
  157. return slave_get_val(slave, ucontrol);
  158. }
  159. static int slave_put(struct snd_kcontrol *kcontrol,
  160. struct snd_ctl_elem_value *ucontrol)
  161. {
  162. struct link_slave *slave = snd_kcontrol_chip(kcontrol);
  163. int err, ch, changed = 0;
  164. err = slave_init(slave);
  165. if (err < 0)
  166. return err;
  167. for (ch = 0; ch < slave->info.count; ch++) {
  168. if (slave->vals[ch] != ucontrol->value.integer.value[ch]) {
  169. changed = 1;
  170. slave->vals[ch] = ucontrol->value.integer.value[ch];
  171. }
  172. }
  173. if (!changed)
  174. return 0;
  175. return slave_put_val(slave, ucontrol);
  176. }
  177. static int slave_tlv_cmd(struct snd_kcontrol *kcontrol,
  178. int op_flag, unsigned int size,
  179. unsigned int __user *tlv)
  180. {
  181. struct link_slave *slave = snd_kcontrol_chip(kcontrol);
  182. /* FIXME: this assumes that the max volume is 0 dB */
  183. return slave->slave.tlv.c(&slave->slave, op_flag, size, tlv);
  184. }
  185. static void slave_free(struct snd_kcontrol *kcontrol)
  186. {
  187. struct link_slave *slave = snd_kcontrol_chip(kcontrol);
  188. if (slave->slave.private_free)
  189. slave->slave.private_free(&slave->slave);
  190. if (slave->master)
  191. list_del(&slave->list);
  192. kfree(slave);
  193. }
  194. /*
  195. * Add a slave control to the group with the given master control
  196. *
  197. * All slaves must be the same type (returning the same information
  198. * via info callback). The fucntion doesn't check it, so it's your
  199. * responsibility.
  200. *
  201. * Also, some additional limitations:
  202. * - at most two channels
  203. * - logarithmic volume control (dB level), no linear volume
  204. * - master can only attenuate the volume, no gain
  205. */
  206. int snd_ctl_add_slave(struct snd_kcontrol *master, struct snd_kcontrol *slave)
  207. {
  208. struct link_master *master_link = snd_kcontrol_chip(master);
  209. struct link_slave *srec;
  210. srec = kzalloc(sizeof(*srec) +
  211. slave->count * sizeof(*slave->vd), GFP_KERNEL);
  212. if (!srec)
  213. return -ENOMEM;
  214. srec->slave = *slave;
  215. memcpy(srec->slave.vd, slave->vd, slave->count * sizeof(*slave->vd));
  216. srec->master = master_link;
  217. /* override callbacks */
  218. slave->info = slave_info;
  219. slave->get = slave_get;
  220. slave->put = slave_put;
  221. if (slave->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)
  222. slave->tlv.c = slave_tlv_cmd;
  223. slave->private_data = srec;
  224. slave->private_free = slave_free;
  225. list_add_tail(&srec->list, &master_link->slaves);
  226. return 0;
  227. }
  228. /*
  229. * ctl callbacks for master controls
  230. */
  231. static int master_info(struct snd_kcontrol *kcontrol,
  232. struct snd_ctl_elem_info *uinfo)
  233. {
  234. struct link_master *master = snd_kcontrol_chip(kcontrol);
  235. int ret;
  236. ret = master_init(master);
  237. if (ret < 0)
  238. return ret;
  239. uinfo->type = master->info.type;
  240. uinfo->count = master->info.count;
  241. uinfo->value.integer.min = master->info.min_val;
  242. uinfo->value.integer.max = master->info.max_val;
  243. return 0;
  244. }
  245. static int master_get(struct snd_kcontrol *kcontrol,
  246. struct snd_ctl_elem_value *ucontrol)
  247. {
  248. struct link_master *master = snd_kcontrol_chip(kcontrol);
  249. int err = master_init(master);
  250. if (err < 0)
  251. return err;
  252. ucontrol->value.integer.value[0] = master->val;
  253. return 0;
  254. }
  255. static int master_put(struct snd_kcontrol *kcontrol,
  256. struct snd_ctl_elem_value *ucontrol)
  257. {
  258. struct link_master *master = snd_kcontrol_chip(kcontrol);
  259. struct link_slave *slave;
  260. struct snd_ctl_elem_value *uval;
  261. int err, old_val;
  262. err = master_init(master);
  263. if (err < 0)
  264. return err;
  265. old_val = master->val;
  266. if (ucontrol->value.integer.value[0] == old_val)
  267. return 0;
  268. uval = kmalloc(sizeof(*uval), GFP_KERNEL);
  269. if (!uval)
  270. return -ENOMEM;
  271. list_for_each_entry(slave, &master->slaves, list) {
  272. master->val = old_val;
  273. uval->id = slave->slave.id;
  274. slave_get_val(slave, uval);
  275. master->val = ucontrol->value.integer.value[0];
  276. slave_put_val(slave, uval);
  277. }
  278. kfree(uval);
  279. return 1;
  280. }
  281. static void master_free(struct snd_kcontrol *kcontrol)
  282. {
  283. struct link_master *master = snd_kcontrol_chip(kcontrol);
  284. struct link_slave *slave;
  285. list_for_each_entry(slave, &master->slaves, list)
  286. slave->master = NULL;
  287. kfree(master);
  288. }
  289. /*
  290. * Create a virtual master control with the given name
  291. */
  292. struct snd_kcontrol *snd_ctl_make_virtual_master(char *name,
  293. const unsigned int *tlv)
  294. {
  295. struct link_master *master;
  296. struct snd_kcontrol *kctl;
  297. struct snd_kcontrol_new knew;
  298. memset(&knew, 0, sizeof(knew));
  299. knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  300. knew.name = name;
  301. knew.info = master_info;
  302. master = kzalloc(sizeof(*master), GFP_KERNEL);
  303. if (!master)
  304. return NULL;
  305. INIT_LIST_HEAD(&master->slaves);
  306. kctl = snd_ctl_new1(&knew, master);
  307. if (!kctl) {
  308. kfree(master);
  309. return NULL;
  310. }
  311. /* override some callbacks */
  312. kctl->info = master_info;
  313. kctl->get = master_get;
  314. kctl->put = master_put;
  315. kctl->private_free = master_free;
  316. /* additional (constant) TLV read */
  317. if (tlv) {
  318. /* FIXME: this assumes that the max volume is 0 dB */
  319. kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  320. kctl->tlv.p = tlv;
  321. }
  322. return kctl;
  323. }