oxfw-control.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * oxfw_stream.c - a part of driver for OXFW970/971 based devices
  3. *
  4. * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
  5. * Licensed under the terms of the GNU General Public License, version 2.
  6. */
  7. #include "oxfw.h"
  8. enum control_action { CTL_READ, CTL_WRITE };
  9. enum control_attribute {
  10. CTL_MIN = 0x02,
  11. CTL_MAX = 0x03,
  12. CTL_CURRENT = 0x10,
  13. };
  14. static int oxfw_mute_command(struct snd_oxfw *oxfw, bool *value,
  15. enum control_action action)
  16. {
  17. u8 *buf;
  18. u8 response_ok;
  19. int err;
  20. buf = kmalloc(11, GFP_KERNEL);
  21. if (!buf)
  22. return -ENOMEM;
  23. if (action == CTL_READ) {
  24. buf[0] = 0x01; /* AV/C, STATUS */
  25. response_ok = 0x0c; /* STABLE */
  26. } else {
  27. buf[0] = 0x00; /* AV/C, CONTROL */
  28. response_ok = 0x09; /* ACCEPTED */
  29. }
  30. buf[1] = 0x08; /* audio unit 0 */
  31. buf[2] = 0xb8; /* FUNCTION BLOCK */
  32. buf[3] = 0x81; /* function block type: feature */
  33. buf[4] = oxfw->device_info->mute_fb_id; /* function block ID */
  34. buf[5] = 0x10; /* control attribute: current */
  35. buf[6] = 0x02; /* selector length */
  36. buf[7] = 0x00; /* audio channel number */
  37. buf[8] = 0x01; /* control selector: mute */
  38. buf[9] = 0x01; /* control data length */
  39. if (action == CTL_READ)
  40. buf[10] = 0xff;
  41. else
  42. buf[10] = *value ? 0x70 : 0x60;
  43. err = fcp_avc_transaction(oxfw->unit, buf, 11, buf, 11, 0x3fe);
  44. if (err < 0)
  45. goto error;
  46. if (err < 11) {
  47. dev_err(&oxfw->unit->device, "short FCP response\n");
  48. err = -EIO;
  49. goto error;
  50. }
  51. if (buf[0] != response_ok) {
  52. dev_err(&oxfw->unit->device, "mute command failed\n");
  53. err = -EIO;
  54. goto error;
  55. }
  56. if (action == CTL_READ)
  57. *value = buf[10] == 0x70;
  58. err = 0;
  59. error:
  60. kfree(buf);
  61. return err;
  62. }
  63. static int oxfw_volume_command(struct snd_oxfw *oxfw, s16 *value,
  64. unsigned int channel,
  65. enum control_attribute attribute,
  66. enum control_action action)
  67. {
  68. u8 *buf;
  69. u8 response_ok;
  70. int err;
  71. buf = kmalloc(12, GFP_KERNEL);
  72. if (!buf)
  73. return -ENOMEM;
  74. if (action == CTL_READ) {
  75. buf[0] = 0x01; /* AV/C, STATUS */
  76. response_ok = 0x0c; /* STABLE */
  77. } else {
  78. buf[0] = 0x00; /* AV/C, CONTROL */
  79. response_ok = 0x09; /* ACCEPTED */
  80. }
  81. buf[1] = 0x08; /* audio unit 0 */
  82. buf[2] = 0xb8; /* FUNCTION BLOCK */
  83. buf[3] = 0x81; /* function block type: feature */
  84. buf[4] = oxfw->device_info->volume_fb_id; /* function block ID */
  85. buf[5] = attribute; /* control attribute */
  86. buf[6] = 0x02; /* selector length */
  87. buf[7] = channel; /* audio channel number */
  88. buf[8] = 0x02; /* control selector: volume */
  89. buf[9] = 0x02; /* control data length */
  90. if (action == CTL_READ) {
  91. buf[10] = 0xff;
  92. buf[11] = 0xff;
  93. } else {
  94. buf[10] = *value >> 8;
  95. buf[11] = *value;
  96. }
  97. err = fcp_avc_transaction(oxfw->unit, buf, 12, buf, 12, 0x3fe);
  98. if (err < 0)
  99. goto error;
  100. if (err < 12) {
  101. dev_err(&oxfw->unit->device, "short FCP response\n");
  102. err = -EIO;
  103. goto error;
  104. }
  105. if (buf[0] != response_ok) {
  106. dev_err(&oxfw->unit->device, "volume command failed\n");
  107. err = -EIO;
  108. goto error;
  109. }
  110. if (action == CTL_READ)
  111. *value = (buf[10] << 8) | buf[11];
  112. err = 0;
  113. error:
  114. kfree(buf);
  115. return err;
  116. }
  117. static int oxfw_mute_get(struct snd_kcontrol *control,
  118. struct snd_ctl_elem_value *value)
  119. {
  120. struct snd_oxfw *oxfw = control->private_data;
  121. value->value.integer.value[0] = !oxfw->mute;
  122. return 0;
  123. }
  124. static int oxfw_mute_put(struct snd_kcontrol *control,
  125. struct snd_ctl_elem_value *value)
  126. {
  127. struct snd_oxfw *oxfw = control->private_data;
  128. bool mute;
  129. int err;
  130. mute = !value->value.integer.value[0];
  131. if (mute == oxfw->mute)
  132. return 0;
  133. err = oxfw_mute_command(oxfw, &mute, CTL_WRITE);
  134. if (err < 0)
  135. return err;
  136. oxfw->mute = mute;
  137. return 1;
  138. }
  139. static int oxfw_volume_info(struct snd_kcontrol *control,
  140. struct snd_ctl_elem_info *info)
  141. {
  142. struct snd_oxfw *oxfw = control->private_data;
  143. info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  144. info->count = oxfw->device_info->mixer_channels;
  145. info->value.integer.min = oxfw->volume_min;
  146. info->value.integer.max = oxfw->volume_max;
  147. return 0;
  148. }
  149. static const u8 channel_map[6] = { 0, 1, 4, 5, 2, 3 };
  150. static int oxfw_volume_get(struct snd_kcontrol *control,
  151. struct snd_ctl_elem_value *value)
  152. {
  153. struct snd_oxfw *oxfw = control->private_data;
  154. unsigned int i;
  155. for (i = 0; i < oxfw->device_info->mixer_channels; ++i)
  156. value->value.integer.value[channel_map[i]] = oxfw->volume[i];
  157. return 0;
  158. }
  159. static int oxfw_volume_put(struct snd_kcontrol *control,
  160. struct snd_ctl_elem_value *value)
  161. {
  162. struct snd_oxfw *oxfw = control->private_data;
  163. unsigned int i, changed_channels;
  164. bool equal_values = true;
  165. s16 volume;
  166. int err;
  167. for (i = 0; i < oxfw->device_info->mixer_channels; ++i) {
  168. if (value->value.integer.value[i] < oxfw->volume_min ||
  169. value->value.integer.value[i] > oxfw->volume_max)
  170. return -EINVAL;
  171. if (value->value.integer.value[i] !=
  172. value->value.integer.value[0])
  173. equal_values = false;
  174. }
  175. changed_channels = 0;
  176. for (i = 0; i < oxfw->device_info->mixer_channels; ++i)
  177. if (value->value.integer.value[channel_map[i]] !=
  178. oxfw->volume[i])
  179. changed_channels |= 1 << (i + 1);
  180. if (equal_values && changed_channels != 0)
  181. changed_channels = 1 << 0;
  182. for (i = 0; i <= oxfw->device_info->mixer_channels; ++i) {
  183. volume = value->value.integer.value[channel_map[i ? i - 1 : 0]];
  184. if (changed_channels & (1 << i)) {
  185. err = oxfw_volume_command(oxfw, &volume, i,
  186. CTL_CURRENT, CTL_WRITE);
  187. if (err < 0)
  188. return err;
  189. }
  190. if (i > 0)
  191. oxfw->volume[i - 1] = volume;
  192. }
  193. return changed_channels != 0;
  194. }
  195. int snd_oxfw_create_mixer(struct snd_oxfw *oxfw)
  196. {
  197. static const struct snd_kcontrol_new controls[] = {
  198. {
  199. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  200. .name = "PCM Playback Switch",
  201. .info = snd_ctl_boolean_mono_info,
  202. .get = oxfw_mute_get,
  203. .put = oxfw_mute_put,
  204. },
  205. {
  206. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  207. .name = "PCM Playback Volume",
  208. .info = oxfw_volume_info,
  209. .get = oxfw_volume_get,
  210. .put = oxfw_volume_put,
  211. },
  212. };
  213. unsigned int i, first_ch;
  214. int err;
  215. err = oxfw_volume_command(oxfw, &oxfw->volume_min,
  216. 0, CTL_MIN, CTL_READ);
  217. if (err < 0)
  218. return err;
  219. err = oxfw_volume_command(oxfw, &oxfw->volume_max,
  220. 0, CTL_MAX, CTL_READ);
  221. if (err < 0)
  222. return err;
  223. err = oxfw_mute_command(oxfw, &oxfw->mute, CTL_READ);
  224. if (err < 0)
  225. return err;
  226. first_ch = oxfw->device_info->mixer_channels == 1 ? 0 : 1;
  227. for (i = 0; i < oxfw->device_info->mixer_channels; ++i) {
  228. err = oxfw_volume_command(oxfw, &oxfw->volume[i],
  229. first_ch + i, CTL_CURRENT, CTL_READ);
  230. if (err < 0)
  231. return err;
  232. }
  233. for (i = 0; i < ARRAY_SIZE(controls); ++i) {
  234. err = snd_ctl_add(oxfw->card,
  235. snd_ctl_new1(&controls[i], oxfw));
  236. if (err < 0)
  237. return err;
  238. }
  239. return 0;
  240. }