vmaster.c 10 KB

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