oxfw-spkr.c 7.3 KB

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