control.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Linux driver for TerraTec DMX 6Fire USB
  3. *
  4. * Mixer control
  5. *
  6. * Author: Torsten Schenk <torsten.schenk@zoho.com>
  7. * Created: Jan 01, 2011
  8. * Version: 0.3.0
  9. * Copyright: (C) Torsten Schenk
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. */
  16. #include <linux/interrupt.h>
  17. #include <sound/control.h>
  18. #include "control.h"
  19. #include "comm.h"
  20. #include "chip.h"
  21. static char *opt_coax_texts[2] = { "Optical", "Coax" };
  22. static char *line_phono_texts[2] = { "Line", "Phono" };
  23. /*
  24. * calculated with $value\[i\] = 128 \cdot sqrt[3]{\frac{i}{128}}$
  25. * this is done because the linear values cause rapid degredation
  26. * of volume in the uppermost region.
  27. */
  28. static const u8 log_volume_table[128] = {
  29. 0x00, 0x19, 0x20, 0x24, 0x28, 0x2b, 0x2e, 0x30, 0x32, 0x34,
  30. 0x36, 0x38, 0x3a, 0x3b, 0x3d, 0x3e, 0x40, 0x41, 0x42, 0x43,
  31. 0x44, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e,
  32. 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x53, 0x54, 0x55, 0x56,
  33. 0x56, 0x57, 0x58, 0x58, 0x59, 0x5a, 0x5b, 0x5b, 0x5c, 0x5c,
  34. 0x5d, 0x5e, 0x5e, 0x5f, 0x60, 0x60, 0x61, 0x61, 0x62, 0x62,
  35. 0x63, 0x63, 0x64, 0x65, 0x65, 0x66, 0x66, 0x67, 0x67, 0x68,
  36. 0x68, 0x69, 0x69, 0x6a, 0x6a, 0x6b, 0x6b, 0x6c, 0x6c, 0x6c,
  37. 0x6d, 0x6d, 0x6e, 0x6e, 0x6f, 0x6f, 0x70, 0x70, 0x70, 0x71,
  38. 0x71, 0x72, 0x72, 0x73, 0x73, 0x73, 0x74, 0x74, 0x75, 0x75,
  39. 0x75, 0x76, 0x76, 0x77, 0x77, 0x77, 0x78, 0x78, 0x78, 0x79,
  40. 0x79, 0x7a, 0x7a, 0x7a, 0x7b, 0x7b, 0x7b, 0x7c, 0x7c, 0x7c,
  41. 0x7d, 0x7d, 0x7d, 0x7e, 0x7e, 0x7e, 0x7f, 0x7f };
  42. /*
  43. * data that needs to be sent to device. sets up card internal stuff.
  44. * values dumped from windows driver and filtered by trial'n'error.
  45. */
  46. static const struct {
  47. u8 type;
  48. u8 reg;
  49. u8 value;
  50. }
  51. init_data[] = {
  52. { 0x22, 0x00, 0x00 }, { 0x20, 0x00, 0x08 }, { 0x22, 0x01, 0x01 },
  53. { 0x20, 0x01, 0x08 }, { 0x22, 0x02, 0x00 }, { 0x20, 0x02, 0x08 },
  54. { 0x22, 0x03, 0x00 }, { 0x20, 0x03, 0x08 }, { 0x22, 0x04, 0x00 },
  55. { 0x20, 0x04, 0x08 }, { 0x22, 0x05, 0x01 }, { 0x20, 0x05, 0x08 },
  56. { 0x22, 0x04, 0x01 }, { 0x12, 0x04, 0x00 }, { 0x12, 0x05, 0x00 },
  57. { 0x12, 0x0d, 0x78 }, { 0x12, 0x21, 0x82 }, { 0x12, 0x22, 0x80 },
  58. { 0x12, 0x23, 0x00 }, { 0x12, 0x06, 0x02 }, { 0x12, 0x03, 0x00 },
  59. { 0x12, 0x02, 0x00 }, { 0x22, 0x03, 0x01 },
  60. { 0 } /* TERMINATING ENTRY */
  61. };
  62. static void usb6fire_control_master_vol_update(struct control_runtime *rt)
  63. {
  64. struct comm_runtime *comm_rt = rt->chip->comm;
  65. if (comm_rt) {
  66. /* set volume */
  67. comm_rt->write8(comm_rt, 0x12, 0x0f, 0x7f -
  68. log_volume_table[rt->master_vol]);
  69. /* unmute */
  70. comm_rt->write8(comm_rt, 0x12, 0x0e, 0x00);
  71. }
  72. }
  73. static void usb6fire_control_line_phono_update(struct control_runtime *rt)
  74. {
  75. struct comm_runtime *comm_rt = rt->chip->comm;
  76. if (comm_rt) {
  77. comm_rt->write8(comm_rt, 0x22, 0x02, rt->line_phono_switch);
  78. comm_rt->write8(comm_rt, 0x21, 0x02, rt->line_phono_switch);
  79. }
  80. }
  81. static void usb6fire_control_opt_coax_update(struct control_runtime *rt)
  82. {
  83. struct comm_runtime *comm_rt = rt->chip->comm;
  84. if (comm_rt) {
  85. comm_rt->write8(comm_rt, 0x22, 0x00, rt->opt_coax_switch);
  86. comm_rt->write8(comm_rt, 0x21, 0x00, rt->opt_coax_switch);
  87. }
  88. }
  89. static int usb6fire_control_master_vol_info(struct snd_kcontrol *kcontrol,
  90. struct snd_ctl_elem_info *uinfo)
  91. {
  92. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  93. uinfo->count = 1;
  94. uinfo->value.integer.min = 0;
  95. uinfo->value.integer.max = 127;
  96. return 0;
  97. }
  98. static int usb6fire_control_master_vol_put(struct snd_kcontrol *kcontrol,
  99. struct snd_ctl_elem_value *ucontrol)
  100. {
  101. struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
  102. int changed = 0;
  103. if (rt->master_vol != ucontrol->value.integer.value[0]) {
  104. rt->master_vol = ucontrol->value.integer.value[0];
  105. usb6fire_control_master_vol_update(rt);
  106. changed = 1;
  107. }
  108. return changed;
  109. }
  110. static int usb6fire_control_master_vol_get(struct snd_kcontrol *kcontrol,
  111. struct snd_ctl_elem_value *ucontrol)
  112. {
  113. struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
  114. ucontrol->value.integer.value[0] = rt->master_vol;
  115. return 0;
  116. }
  117. static int usb6fire_control_line_phono_info(struct snd_kcontrol *kcontrol,
  118. struct snd_ctl_elem_info *uinfo)
  119. {
  120. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  121. uinfo->count = 1;
  122. uinfo->value.enumerated.items = 2;
  123. if (uinfo->value.enumerated.item > 1)
  124. uinfo->value.enumerated.item = 1;
  125. strcpy(uinfo->value.enumerated.name,
  126. line_phono_texts[uinfo->value.enumerated.item]);
  127. return 0;
  128. }
  129. static int usb6fire_control_line_phono_put(struct snd_kcontrol *kcontrol,
  130. struct snd_ctl_elem_value *ucontrol)
  131. {
  132. struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
  133. int changed = 0;
  134. if (rt->line_phono_switch != ucontrol->value.integer.value[0]) {
  135. rt->line_phono_switch = ucontrol->value.integer.value[0];
  136. usb6fire_control_line_phono_update(rt);
  137. changed = 1;
  138. }
  139. return changed;
  140. }
  141. static int usb6fire_control_line_phono_get(struct snd_kcontrol *kcontrol,
  142. struct snd_ctl_elem_value *ucontrol)
  143. {
  144. struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
  145. ucontrol->value.integer.value[0] = rt->line_phono_switch;
  146. return 0;
  147. }
  148. static int usb6fire_control_opt_coax_info(struct snd_kcontrol *kcontrol,
  149. struct snd_ctl_elem_info *uinfo)
  150. {
  151. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  152. uinfo->count = 1;
  153. uinfo->value.enumerated.items = 2;
  154. if (uinfo->value.enumerated.item > 1)
  155. uinfo->value.enumerated.item = 1;
  156. strcpy(uinfo->value.enumerated.name,
  157. opt_coax_texts[uinfo->value.enumerated.item]);
  158. return 0;
  159. }
  160. static int usb6fire_control_opt_coax_put(struct snd_kcontrol *kcontrol,
  161. struct snd_ctl_elem_value *ucontrol)
  162. {
  163. struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
  164. int changed = 0;
  165. if (rt->opt_coax_switch != ucontrol->value.enumerated.item[0]) {
  166. rt->opt_coax_switch = ucontrol->value.enumerated.item[0];
  167. usb6fire_control_opt_coax_update(rt);
  168. changed = 1;
  169. }
  170. return changed;
  171. }
  172. static int usb6fire_control_opt_coax_get(struct snd_kcontrol *kcontrol,
  173. struct snd_ctl_elem_value *ucontrol)
  174. {
  175. struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
  176. ucontrol->value.enumerated.item[0] = rt->opt_coax_switch;
  177. return 0;
  178. }
  179. static struct __devinitdata snd_kcontrol_new elements[] = {
  180. {
  181. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  182. .name = "Master Playback Volume",
  183. .index = 0,
  184. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  185. .info = usb6fire_control_master_vol_info,
  186. .get = usb6fire_control_master_vol_get,
  187. .put = usb6fire_control_master_vol_put
  188. },
  189. {
  190. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  191. .name = "Line/Phono Capture Route",
  192. .index = 0,
  193. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  194. .info = usb6fire_control_line_phono_info,
  195. .get = usb6fire_control_line_phono_get,
  196. .put = usb6fire_control_line_phono_put
  197. },
  198. {
  199. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  200. .name = "Opt/Coax Capture Route",
  201. .index = 0,
  202. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  203. .info = usb6fire_control_opt_coax_info,
  204. .get = usb6fire_control_opt_coax_get,
  205. .put = usb6fire_control_opt_coax_put
  206. },
  207. {}
  208. };
  209. int __devinit usb6fire_control_init(struct sfire_chip *chip)
  210. {
  211. int i;
  212. int ret;
  213. struct control_runtime *rt = kzalloc(sizeof(struct control_runtime),
  214. GFP_KERNEL);
  215. struct comm_runtime *comm_rt = chip->comm;
  216. if (!rt)
  217. return -ENOMEM;
  218. rt->chip = chip;
  219. i = 0;
  220. while (init_data[i].type) {
  221. comm_rt->write8(comm_rt, init_data[i].type, init_data[i].reg,
  222. init_data[i].value);
  223. i++;
  224. }
  225. usb6fire_control_opt_coax_update(rt);
  226. usb6fire_control_line_phono_update(rt);
  227. usb6fire_control_master_vol_update(rt);
  228. i = 0;
  229. while (elements[i].name) {
  230. ret = snd_ctl_add(chip->card, snd_ctl_new1(&elements[i], rt));
  231. if (ret < 0) {
  232. kfree(rt);
  233. snd_printk(KERN_ERR PREFIX "cannot add control.\n");
  234. return ret;
  235. }
  236. i++;
  237. }
  238. chip->control = rt;
  239. return 0;
  240. }
  241. void usb6fire_control_abort(struct sfire_chip *chip)
  242. {}
  243. void usb6fire_control_destroy(struct sfire_chip *chip)
  244. {
  245. kfree(chip->control);
  246. chip->control = NULL;
  247. }