vmaster.c 9.3 KB

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