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