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.

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