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.

294 lines
7.2KB

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