vmaster.c 11 KB

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