jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

311 lines
7.7KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. $Id: hammerfall.c,v 1.3 2005/09/29 14:51:59 letz Exp $
  15. */
  16. #include "hardware.h"
  17. #include "alsa_driver.h"
  18. #include "hammerfall.h"
  19. #include "JackError.h"
  20. #define FALSE 0
  21. #define TRUE 1
  22. /* Set this to 1 if you want this compile error:
  23. * warning: `hammerfall_monitor_controls' defined but not used */
  24. #define HAMMERFALL_MONITOR_CONTROLS 0
  25. static void
  26. set_control_id (snd_ctl_elem_id_t *ctl, const char *name)
  27. {
  28. snd_ctl_elem_id_set_name (ctl, name);
  29. snd_ctl_elem_id_set_numid (ctl, 0);
  30. snd_ctl_elem_id_set_interface (ctl, SND_CTL_ELEM_IFACE_MIXER);
  31. snd_ctl_elem_id_set_device (ctl, 0);
  32. snd_ctl_elem_id_set_subdevice (ctl, 0);
  33. snd_ctl_elem_id_set_index (ctl, 0);
  34. }
  35. #if HAMMERFALL_MONITOR_CONTROLS
  36. static void
  37. hammerfall_broadcast_channel_status_change (hammerfall_t *h, int lock, int sync, channel_t lowchn, channel_t highchn)
  38. {
  39. channel_t chn;
  40. ClockSyncStatus status = 0;
  41. if (lock) {
  42. status |= Lock;
  43. } else {
  44. status |= NoLock;
  45. }
  46. if (sync) {
  47. status |= Sync;
  48. } else {
  49. status |= NoSync;
  50. }
  51. for (chn = lowchn; chn < highchn; chn++) {
  52. alsa_driver_set_clock_sync_status (h->driver, chn, status);
  53. }
  54. }
  55. static void
  56. hammerfall_check_sync_state (hammerfall_t *h, int val, int adat_id)
  57. {
  58. int lock;
  59. int sync;
  60. /* S/PDIF channel is always locked and synced, but we only
  61. need tell people once that this is TRUE.
  62. XXX - maybe need to make sure that the rate matches our
  63. idea of the current rate ?
  64. */
  65. if (!h->said_that_spdif_is_fine) {
  66. ClockSyncStatus status;
  67. status = Lock|Sync;
  68. /* XXX broken! fix for hammerfall light ! */
  69. alsa_driver_set_clock_sync_status (h->driver, 24, status);
  70. alsa_driver_set_clock_sync_status (h->driver, 25, status);
  71. h->said_that_spdif_is_fine = TRUE;
  72. }
  73. lock = (val & 0x1) ? TRUE : FALSE;
  74. sync = (val & 0x2) ? TRUE : FALSE;
  75. if (h->lock_status[adat_id] != lock ||
  76. h->sync_status[adat_id] != sync) {
  77. hammerfall_broadcast_channel_status_change (h, lock, sync, adat_id*8, (adat_id*8)+8);
  78. }
  79. h->lock_status[adat_id] = lock;
  80. h->sync_status[adat_id] = sync;
  81. }
  82. static void
  83. hammerfall_check_sync (hammerfall_t *h, snd_ctl_elem_value_t *ctl)
  84. {
  85. const char *name;
  86. int val;
  87. snd_ctl_elem_id_t *ctl_id;
  88. jack_info ("check sync");
  89. snd_ctl_elem_id_alloca (&ctl_id);
  90. snd_ctl_elem_value_get_id (ctl, ctl_id);
  91. name = snd_ctl_elem_id_get_name (ctl_id);
  92. if (strcmp (name, "ADAT1 Sync Check") == 0) {
  93. val = snd_ctl_elem_value_get_enumerated (ctl, 0);
  94. hammerfall_check_sync_state (h, val, 0);
  95. } else if (strcmp (name, "ADAT2 Sync Check") == 0) {
  96. val = snd_ctl_elem_value_get_enumerated (ctl, 0);
  97. hammerfall_check_sync_state (h, val, 1);
  98. } else if (strcmp (name, "ADAT3 Sync Check") == 0) {
  99. val = snd_ctl_elem_value_get_enumerated (ctl, 0);
  100. hammerfall_check_sync_state (h, val, 2);
  101. } else {
  102. jack_error ("Hammerfall: unknown control \"%s\"", name);
  103. }
  104. }
  105. #endif /* HAMMERFALL_MONITOR_CONTROLS */
  106. static int
  107. hammerfall_set_input_monitor_mask (jack_hardware_t *hw, unsigned long mask)
  108. {
  109. hammerfall_t *h = (hammerfall_t *) hw->private_hw;
  110. snd_ctl_elem_value_t *ctl;
  111. snd_ctl_elem_id_t *ctl_id;
  112. int err;
  113. int i;
  114. snd_ctl_elem_value_alloca (&ctl);
  115. snd_ctl_elem_id_alloca (&ctl_id);
  116. set_control_id (ctl_id, "Channels Thru");
  117. snd_ctl_elem_value_set_id (ctl, ctl_id);
  118. for (i = 0; i < 26; i++) {
  119. snd_ctl_elem_value_set_integer (ctl, i, (mask & (1<<i)) ? 1 : 0);
  120. }
  121. if ((err = snd_ctl_elem_write (h->driver->ctl_handle, ctl)) != 0) {
  122. jack_error ("ALSA/Hammerfall: cannot set input monitoring (%s)", snd_strerror (err));
  123. return -1;
  124. }
  125. hw->input_monitor_mask = mask;
  126. return 0;
  127. }
  128. static int
  129. hammerfall_change_sample_clock (jack_hardware_t *hw, SampleClockMode mode)
  130. {
  131. hammerfall_t *h = (hammerfall_t *) hw->private_hw;
  132. snd_ctl_elem_value_t *ctl;
  133. snd_ctl_elem_id_t *ctl_id;
  134. int err;
  135. snd_ctl_elem_value_alloca (&ctl);
  136. snd_ctl_elem_id_alloca (&ctl_id);
  137. set_control_id (ctl_id, "Sync Mode");
  138. snd_ctl_elem_value_set_id (ctl, ctl_id);
  139. switch (mode) {
  140. case AutoSync:
  141. snd_ctl_elem_value_set_enumerated (ctl, 0, 0);
  142. break;
  143. case ClockMaster:
  144. snd_ctl_elem_value_set_enumerated (ctl, 0, 1);
  145. break;
  146. case WordClock:
  147. snd_ctl_elem_value_set_enumerated (ctl, 0, 2);
  148. break;
  149. }
  150. if ((err = snd_ctl_elem_write (h->driver->ctl_handle, ctl)) < 0) {
  151. jack_error ("ALSA-Hammerfall: cannot set clock mode");
  152. }
  153. return 0;
  154. }
  155. static void
  156. hammerfall_release (jack_hardware_t *hw)
  157. {
  158. hammerfall_t *h = (hammerfall_t *) hw->private_hw;
  159. void *status;
  160. if (h == 0) {
  161. return;
  162. }
  163. #ifndef __ANDROID__
  164. if (h->monitor_thread) {
  165. pthread_cancel (h->monitor_thread);
  166. pthread_join (h->monitor_thread, &status);
  167. }
  168. #endif
  169. free (h);
  170. }
  171. #if HAMMERFALL_MONITOR_CONTROLS
  172. static void *
  173. hammerfall_monitor_controls (void *arg)
  174. {
  175. jack_hardware_t *hw = (jack_hardware_t *) arg;
  176. hammerfall_t *h = (hammerfall_t *) hw->private_hw;
  177. snd_ctl_elem_id_t *switch_id[3];
  178. snd_ctl_elem_value_t *sw[3];
  179. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  180. snd_ctl_elem_id_malloc (&switch_id[0]);
  181. snd_ctl_elem_id_malloc (&switch_id[1]);
  182. snd_ctl_elem_id_malloc (&switch_id[2]);
  183. snd_ctl_elem_value_malloc (&sw[0]);
  184. snd_ctl_elem_value_malloc (&sw[1]);
  185. snd_ctl_elem_value_malloc (&sw[2]);
  186. set_control_id (switch_id[0], "ADAT1 Sync Check");
  187. set_control_id (switch_id[1], "ADAT2 Sync Check");
  188. set_control_id (switch_id[2], "ADAT3 Sync Check");
  189. snd_ctl_elem_value_set_id (sw[0], switch_id[0]);
  190. snd_ctl_elem_value_set_id (sw[1], switch_id[1]);
  191. snd_ctl_elem_value_set_id (sw[2], switch_id[2]);
  192. while (1) {
  193. if (snd_ctl_elem_read (h->driver->ctl_handle, sw[0])) {
  194. jack_error ("cannot read control switch 0 ...");
  195. }
  196. hammerfall_check_sync (h, sw[0]);
  197. if (snd_ctl_elem_read (h->driver->ctl_handle, sw[1])) {
  198. jack_error ("cannot read control switch 0 ...");
  199. }
  200. hammerfall_check_sync (h, sw[1]);
  201. if (snd_ctl_elem_read (h->driver->ctl_handle, sw[2])) {
  202. jack_error ("cannot read control switch 0 ...");
  203. }
  204. hammerfall_check_sync (h, sw[2]);
  205. if (nanosleep (&h->monitor_interval, 0)) {
  206. break;
  207. }
  208. }
  209. pthread_exit (0);
  210. }
  211. #endif /* HAMMERFALL_MONITOR_CONTROLS */
  212. jack_hardware_t *
  213. jack_alsa_hammerfall_hw_new (alsa_driver_t *driver)
  214. {
  215. jack_hardware_t *hw;
  216. hammerfall_t *h;
  217. hw = (jack_hardware_t *) malloc (sizeof (jack_hardware_t));
  218. hw->capabilities = Cap_HardwareMonitoring|Cap_AutoSync|Cap_WordClock|Cap_ClockMaster|Cap_ClockLockReporting;
  219. hw->input_monitor_mask = 0;
  220. hw->private_hw = 0;
  221. hw->set_input_monitor_mask = hammerfall_set_input_monitor_mask;
  222. hw->change_sample_clock = hammerfall_change_sample_clock;
  223. hw->release = hammerfall_release;
  224. h = (hammerfall_t *) malloc (sizeof (hammerfall_t));
  225. h->lock_status[0] = FALSE;
  226. h->sync_status[0] = FALSE;
  227. h->lock_status[1] = FALSE;
  228. h->sync_status[1] = FALSE;
  229. h->lock_status[2] = FALSE;
  230. h->sync_status[2] = FALSE;
  231. h->said_that_spdif_is_fine = FALSE;
  232. h->driver = driver;
  233. h->monitor_interval.tv_sec = 1;
  234. h->monitor_interval.tv_nsec = 0;
  235. hw->private_hw = h;
  236. #if 0
  237. if (pthread_create (&h->monitor_thread, 0, hammerfall_monitor_controls, hw)) {
  238. jack_error ("ALSA/Hammerfall: cannot create sync monitor thread");
  239. }
  240. #endif
  241. return hw;
  242. }