jack1 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.

304 lines
7.6KB

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