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