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.

298 lines
7.3KB

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