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.

307 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. pthread_cancel (h->monitor_thread);
  164. pthread_join (h->monitor_thread, &status);
  165. free (h);
  166. }
  167. #if HAMMERFALL_MONITOR_CONTROLS
  168. static void *
  169. hammerfall_monitor_controls (void *arg)
  170. {
  171. jack_hardware_t *hw = (jack_hardware_t *) arg;
  172. hammerfall_t *h = (hammerfall_t *) hw->private_hw;
  173. snd_ctl_elem_id_t *switch_id[3];
  174. snd_ctl_elem_value_t *sw[3];
  175. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  176. snd_ctl_elem_id_malloc (&switch_id[0]);
  177. snd_ctl_elem_id_malloc (&switch_id[1]);
  178. snd_ctl_elem_id_malloc (&switch_id[2]);
  179. snd_ctl_elem_value_malloc (&sw[0]);
  180. snd_ctl_elem_value_malloc (&sw[1]);
  181. snd_ctl_elem_value_malloc (&sw[2]);
  182. set_control_id (switch_id[0], "ADAT1 Sync Check");
  183. set_control_id (switch_id[1], "ADAT2 Sync Check");
  184. set_control_id (switch_id[2], "ADAT3 Sync Check");
  185. snd_ctl_elem_value_set_id (sw[0], switch_id[0]);
  186. snd_ctl_elem_value_set_id (sw[1], switch_id[1]);
  187. snd_ctl_elem_value_set_id (sw[2], switch_id[2]);
  188. while (1) {
  189. if (snd_ctl_elem_read (h->driver->ctl_handle, sw[0])) {
  190. jack_error ("cannot read control switch 0 ...");
  191. }
  192. hammerfall_check_sync (h, sw[0]);
  193. if (snd_ctl_elem_read (h->driver->ctl_handle, sw[1])) {
  194. jack_error ("cannot read control switch 0 ...");
  195. }
  196. hammerfall_check_sync (h, sw[1]);
  197. if (snd_ctl_elem_read (h->driver->ctl_handle, sw[2])) {
  198. jack_error ("cannot read control switch 0 ...");
  199. }
  200. hammerfall_check_sync (h, sw[2]);
  201. if (nanosleep (&h->monitor_interval, 0)) {
  202. break;
  203. }
  204. }
  205. pthread_exit (0);
  206. }
  207. #endif /* HAMMERFALL_MONITOR_CONTROLS */
  208. jack_hardware_t *
  209. jack_alsa_hammerfall_hw_new (alsa_driver_t *driver)
  210. {
  211. jack_hardware_t *hw;
  212. hammerfall_t *h;
  213. hw = (jack_hardware_t *) malloc (sizeof (jack_hardware_t));
  214. hw->capabilities = Cap_HardwareMonitoring|Cap_AutoSync|Cap_WordClock|Cap_ClockMaster|Cap_ClockLockReporting;
  215. hw->input_monitor_mask = 0;
  216. hw->private_hw = 0;
  217. hw->set_input_monitor_mask = hammerfall_set_input_monitor_mask;
  218. hw->change_sample_clock = hammerfall_change_sample_clock;
  219. hw->release = hammerfall_release;
  220. h = (hammerfall_t *) malloc (sizeof (hammerfall_t));
  221. h->lock_status[0] = FALSE;
  222. h->sync_status[0] = FALSE;
  223. h->lock_status[1] = FALSE;
  224. h->sync_status[1] = FALSE;
  225. h->lock_status[2] = FALSE;
  226. h->sync_status[2] = FALSE;
  227. h->said_that_spdif_is_fine = FALSE;
  228. h->driver = driver;
  229. h->monitor_interval.tv_sec = 1;
  230. h->monitor_interval.tv_nsec = 0;
  231. hw->private_hw = h;
  232. #if 0
  233. if (pthread_create (&h->monitor_thread, 0, hammerfall_monitor_controls, hw)) {
  234. jack_error ("ALSA/Hammerfall: cannot create sync monitor thread");
  235. }
  236. #endif
  237. return hw;
  238. }