motu: the new portgroups mechanism was added to FFADO in r1886, and refined in r1890...
[ffado.git] / libffado / src / motu / motu_avdevice.cpp
blobd498534169e295c24f030974d591f976ddc17193
1 /*
2 * Copyright (C) 2005-2008 by Pieter Palmers
3 * Copyright (C) 2005-2009 by Jonathan Woithe
5 * This file is part of FFADO
6 * FFADO = Free Firewire (pro-)audio drivers for linux
8 * FFADO is based upon FreeBoB.
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 2 of the License, or
13 * (at your option) version 3 of the License.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "config.h"
27 #include "motu/motu_avdevice.h"
28 #include "motu/motu_mixerdefs.h"
29 #include "motu/motu_mark3_mixerdefs.h"
31 #include "devicemanager.h"
33 #include "libieee1394/configrom.h"
34 #include "libieee1394/ieee1394service.h"
36 #include "libavc/avc_definitions.h"
38 #include "debugmodule/debugmodule.h"
40 #include "libstreaming/motu/MotuReceiveStreamProcessor.h"
41 #include "libstreaming/motu/MotuTransmitStreamProcessor.h"
42 #include "libstreaming/motu/MotuPort.h"
44 #include "libutil/Time.h"
45 #include "libutil/Configuration.h"
47 #include "libcontrol/BasicElements.h"
49 #include <string>
50 #include <stdint.h>
51 #include <assert.h>
52 #include "libutil/ByteSwap.h"
53 #include <iostream>
54 #include <sstream>
56 #include <libraw1394/csr.h>
59 namespace Motu {
61 // Define the supported devices. Device ordering is arbitary here. To
62 // include a MOTU device which cannot yet be used (for identification
63 // purposes only), set the model field to MOTU_MODEL_NONE.
65 // The V4HD device includes 4 sub-devices. Include all in the definition as
66 // a way of documenting it. It's likely that only one of these is of
67 // interest for audio but that's still to be determined.
68 static VendorModelEntry supportedDeviceList[] =
70 // {vendor_id, model_id, unit_version, unit_specifier_id, model, vendor_name,model_name}
71 {FW_VENDORID_MOTU, 0, 0x00000003, 0x000001f2, MOTU_MODEL_828mkII, "MOTU", "828MkII"},
72 {FW_VENDORID_MOTU, 0, 0x00000009, 0x000001f2, MOTU_MODEL_TRAVELER, "MOTU", "Traveler"},
73 {FW_VENDORID_MOTU, 0, 0x0000000d, 0x000001f2, MOTU_MODEL_ULTRALITE, "MOTU", "UltraLite"},
74 {FW_VENDORID_MOTU, 0, 0x0000000f, 0x000001f2, MOTU_MODEL_8PRE, "MOTU", "8pre"},
75 {FW_VENDORID_MOTU, 0, 0x00000001, 0x000001f2, MOTU_MODEL_828MkI, "MOTU", "828MkI"},
76 {FW_VENDORID_MOTU, 0, 0x00000005, 0x000001f2, MOTU_MODEL_896HD, "MOTU", "896HD"},
77 {FW_VENDORID_MOTU, 0, 0x00000015, 0x000001f2, MOTU_MODEL_828mk3, "MOTU", "828Mk3"},
78 {FW_VENDORID_MOTU, 0, 0x00000017, 0x000001f2, MOTU_MODEL_896mk3, "MOTU", "896Mk3"},
79 {FW_VENDORID_MOTU, 0, 0x00000019, 0x000001f2, MOTU_MODEL_ULTRALITEmk3, "MOTU", "UltraLiteMk3"},
80 {FW_VENDORID_MOTU, 0, 0x0000001b, 0x000001f2, MOTU_MODEL_TRAVELERmk3, "MOTU", "TravelerMk3"},
81 {FW_VENDORID_MOTU, 0, 0x00000021, 0x000001f2, MOTU_MODEL_NONE, "MOTU", "V4HD subdevice 0"},
82 {FW_VENDORID_MOTU, 0, 0x00000022, 0x000001f2, MOTU_MODEL_NONE, "MOTU", "V4HD subdevice 1"},
83 {FW_VENDORID_MOTU, 0, 0x00000023, 0x000001f2, MOTU_MODEL_NONE, "MOTU", "V4HD subdevice 2"},
84 {FW_VENDORID_MOTU, 0, 0x00000024, 0x000001f2, MOTU_MODEL_NONE, "MOTU", "V4HD subdevice 3"},
85 {FW_VENDORID_MOTU, 0, 0x00000030, 0x000001f2, MOTU_MODEL_ULTRALITEmk3_HYB, "MOTU", "UltraLiteMk3-hybrid"},
86 {FW_VENDORID_MOTU, 0, 0x00000045, 0x000001f2, MOTU_MODEL_4PRE, "MOTU", "4pre"},
89 // Ports declarations
91 PortGroupEntry PortGroups_828MKI[] =
93 {"Analog%d", 8, MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, -1, },
94 {"SPDIF%d", 2, MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, },
95 {"ADAT%d", 8, MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, },
96 {"ADAT%d", 4, MOTU_PA_INOUT | MOTU_PA_RATE_2x|MOTU_PA_OPTICAL_ADAT, },
99 PortGroupEntry PortGroups_896HD[] =
101 {"Mix-%s", 2, MOTU_PA_IN | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, -1, },
102 {"Phones-%s", 2, MOTU_PA_OUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, },
103 {"Analog%d", 8, MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, },
104 {"MainOut-%s", 2, MOTU_PA_OUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, },
105 {"unknown-%d", 2, MOTU_PA_IN | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, },
106 {"ADAT%d", 8, MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, },
107 {"ADAT%d", 4, MOTU_PA_INOUT | MOTU_PA_RATE_2x|MOTU_PA_OPTICAL_ADAT, },
108 {"AES/EBU%d", 2, MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, },
111 PortGroupEntry PortGroups_828MKII[] =
113 {"Mix-%s", 2, MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, -1},
114 {"Phones-%s", 2, MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, },
115 {"Analog%d", 8, MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, },
116 {"Mic%d", 2, MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, },
117 {"Main-%s", 2, MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, },
118 {"SPDIF%d", 2, MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, },
119 {"ADAT%d", 8, MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, },
120 {"ADAT%d", 4, MOTU_PA_INOUT | MOTU_PA_RATE_2x|MOTU_PA_OPTICAL_ADAT, },
123 PortGroupEntry PortGroups_TRAVELER[] =
125 {"Mix-%s", 2, MOTU_PA_IN | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, -1, },
126 {"Phones-%s", 2, MOTU_PA_OUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, },
127 {"Analog%d", 8, MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, },
128 {"AES/EBU%d", 2, MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, },
129 {"SPDIF%d", 2, MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_OFF|MOTU_PA_OPTICAL_ADAT, },
130 {"Toslink%d", 2, MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_TOSLINK, },
131 {"ADAT%d", 8, MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, },
132 {"ADAT%d", 4, MOTU_PA_INOUT | MOTU_PA_RATE_2x|MOTU_PA_OPTICAL_ADAT, },
135 PortGroupEntry PortGroups_ULTRALITE[] =
137 {"Mix-%s", 2, MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 1, },
138 {"Phones-%s", 2, MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 2, },
139 {"Mic%d", 2, MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 3, },
140 {"Analog%d", 2, MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 4, },
141 {"Analog%d", 6, MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 5, 2},
142 {"Main-%s", 2, MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 0, },
143 {"SPDIF%d", 2, MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 6, },
144 {"Padding%d", 2, MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY|MOTU_PA_PADDING, 7, },
147 PortGroupEntry PortGroups_8PRE[] =
149 {"Mix-%s", 2, MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 2, },
150 {"Phones-%s", 2, MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 3, },
151 {"Analog%d", 8, MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 0, },
152 {"Main-%s", 2, MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 1, },
153 {"Padding%d", 2, MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY|MOTU_PA_PADDING, 4, },
154 {"ADAT%d", 8, MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ADAT, 5, },
155 {"ADAT%d", 4, MOTU_PA_INOUT | MOTU_PA_RATE_2x|MOTU_PA_OPTICAL_ADAT, 6, },
158 PortGroupEntry PortGroups_828mk3[] =
160 {"Mic-%d", 2, MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_MK3_OPT_ANY, -1, },
161 {"Phones-%s", 2, MOTU_PA_OUT | MOTU_PA_RATE_1x2x|MOTU_PA_MK3_OPT_ANY, },
162 {"Unknown-%s", 2, MOTU_PA_OUT | MOTU_PA_RATE_4x|MOTU_PA_MK3_OPT_ANY, },
163 {"Analog%d", 8, MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_MK3_OPT_ANY, },
164 {"Return-%d", 2, MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_MK3_OPT_ANY, },
165 {"MainOut-%s", 2, MOTU_PA_OUT | MOTU_PA_RATE_1x2x|MOTU_PA_MK3_OPT_ANY, },
166 {"SPDIF%d", 2, MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_MK3_OPT_ANY, },
167 {"Unknown-%d", 2, MOTU_PA_OUT | MOTU_PA_RATE_4x|MOTU_PA_MK3_OPT_ANY, },
168 {"Reverb-%d", 2, MOTU_PA_IN | MOTU_PA_RATE_1x2x|MOTU_PA_MK3_OPT_ANY, },
170 // For input at 1x rates there are an additional 2 channel slots which
171 // are yet to be identified.
173 {"UnknownA-%d", 2, MOTU_PA_IN | MOTU_PA_RATE_1x|MOTU_PA_MK3_OPT_ANY, },
175 // Optical ports. Note: UnknownB is flagged as being always present at
176 // 2x rates in the input stream. This is yet to be verified. In the
177 // case of the old PortEntry definition UnknownB's equivalent was only
178 // flagged as present if optical B was set to ADAT; this seems wrong on
179 // reflection.
181 // The purpose of the Toslink-{A,B}-extra-* ports is not yet known,
182 // beyond the observed fact that allowance must be made for them in the
183 // packet format whenever the corresponding optical port is set to
184 // Toslink mode. They may be padding, but for now connect them with
185 // audio ports so users can experiment to see if they correspond to
186 // anything interesting.
188 {"Toslink-A%d", 2, MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_MK3_OPT_A_TOSLINK|MOTU_PA_MK3_OPT_B_ANY, },
189 {"Toslink-A-extra-%d", 2, MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_MK3_OPT_A_TOSLINK|MOTU_PA_MK3_OPT_B_ANY, },
190 {"ADAT-A%d", 8, MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_MK3_OPT_A_ADAT|MOTU_PA_MK3_OPT_B_ANY, },
191 {"ADAT-A%d", 4, MOTU_PA_INOUT | MOTU_PA_RATE_2x|MOTU_PA_MK3_OPT_A_ADAT|MOTU_PA_MK3_OPT_B_ANY, },
192 {"Toslink-B%d", 2, MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_MK3_OPT_A_ANY|MOTU_PA_MK3_OPT_B_TOSLINK, },
193 {"Toslink-B-extra-%d", 2, MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_MK3_OPT_A_ANY|MOTU_PA_MK3_OPT_B_TOSLINK, },
194 {"ADAT-B%d", 8, MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_MK3_OPT_A_ANY|MOTU_PA_MK3_OPT_B_ADAT, },
195 {"ADAT-B%d", 4, MOTU_PA_INOUT | MOTU_PA_RATE_2x|MOTU_PA_MK3_OPT_A_ANY|MOTU_PA_MK3_OPT_B_ADAT, },
196 {"UnknownB-%d", 2, MOTU_PA_IN | MOTU_PA_RATE_2x|MOTU_PA_MK3_OPT_A_ANY|MOTU_PA_MK3_OPT_B_ANY, },
199 PortGroupEntry PortGroups_ULTRALITEmk3[] =
201 {"Mix-%s", 2, MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 6, },
202 {"Main-%s", 2, MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 0, },
203 {"Mic%d", 2, MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 1, },
204 {"Analog%d", 2, MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 2, },
205 {"Analog%d", 6, MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 3, 2},
206 {"Phones-%s", 2, MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 4, },
207 {"SPDIF%d", 2, MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 5, },
208 {"Reverb-%s", 2, MOTU_PA_IN | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 7, },
209 {"Padding%d", 4, MOTU_PA_IN | MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ANY|MOTU_PA_PADDING, 8, },
210 {"Pad out%d", 2, MOTU_PA_OUT | MOTU_PA_RATE_4x|MOTU_PA_OPTICAL_ANY|MOTU_PA_PADDING, 9, },
213 PortGroupEntry PortGroups_ULTRALITEmk3_hybrid[] =
215 {"Mix-%s", 2, MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 1, },
216 {"Phones-%s", 2, MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 2, },
217 {"Mic%d", 2, MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 3, },
218 {"Analog%d", 2, MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 4, },
219 {"Analog%d", 6, MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 5, 2, },
220 {"Main-%s", 2, MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 0, },
221 {"SPDIF%d", 2, MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 6, },
222 {"Reverb%d", 2, MOTU_PA_IN | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 7, },
223 {"Unknown%d", 4, MOTU_PA_IN | MOTU_PA_RATE_1x|MOTU_PA_OPTICAL_ANY, 8, },
226 /* FIXME: as of 5 Aug 2010 this is still under development */
227 PortGroupEntry PortGroups_TRAVELERmk3[] =
229 {"Mix-%s", 2, MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_MK3_OPT_ANY, -1, },
230 {"Phones-%s",2, MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_MK3_OPT_ANY, },
231 {"Analog%d", 8, MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_MK3_OPT_ANY, },
232 {"AES/EBU%d", 2, MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_MK3_OPT_ANY, },
233 {"SPDIF%d", 2, MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_MK3_OPT_ANY, },
234 {"Reverb%d", 2, MOTU_PA_IN | MOTU_PA_RATE_1x|MOTU_PA_MK3_OPT_ANY, },
235 {"Unknown%d", 2, MOTU_PA_IN | MOTU_PA_RATE_1x|MOTU_PA_MK3_OPT_ANY, },
237 // FIXME: optical port details still being determined.
239 // The purpose of the Toslink-{A,B}-extra-* ports is not yet known,
240 // beyond the observed fact that allowance must be made for them in the
241 // packet format whenever the corresponding optical port is set to
242 // Toslink mode. They may be padding, but for now connect them with
243 // audio ports so users can experiment to see if they correspond to
244 // anything interesting.
246 {"Toslink-A%d", 2, MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_MK3_OPT_A_TOSLINK|MOTU_PA_MK3_OPT_B_ANY, },
247 {"Toslink-A-extra-%d", 2, MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_MK3_OPT_A_TOSLINK|MOTU_PA_MK3_OPT_B_ANY, },
248 {"ADAT-A%d", 8, MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_MK3_OPT_A_ADAT|MOTU_PA_MK3_OPT_B_ANY, },
249 {"ADAT-A%d", 4, MOTU_PA_INOUT | MOTU_PA_RATE_2x|MOTU_PA_MK3_OPT_A_ADAT|MOTU_PA_MK3_OPT_B_ANY, },
250 {"Toslink-B%d", 2, MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_MK3_OPT_A_ANY|MOTU_PA_MK3_OPT_B_TOSLINK, },
251 {"Toslink-B-extra-%d", 2, MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_MK3_OPT_A_ANY|MOTU_PA_MK3_OPT_B_TOSLINK, },
252 {"ADAT-B%d", 8, MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_MK3_OPT_A_ANY|MOTU_PA_MK3_OPT_B_ADAT, },
253 {"ADAT-B%d", 4, MOTU_PA_INOUT | MOTU_PA_RATE_2x|MOTU_PA_MK3_OPT_A_ANY|MOTU_PA_MK3_OPT_B_ADAT, },
254 {"UnknownB-%d", 2, MOTU_PA_IN | MOTU_PA_RATE_2x|MOTU_PA_MK3_OPT_A_TOSLINK|MOTU_PA_MK3_OPT_B_TOSLINK, },
257 /* FIXME: as of 8 Oct 2010 this is still under development. Presently this
258 * layout is nothing more than an educated guess. The presence of the
259 * Toslink-{A,B}-extra-* ports is even more of a guess, and is based on
260 * the observation that they have so far proved necessary on the mk3
261 * devices which we have been able to explicitly verify.
263 PortGroupEntry PortGroups_896mk3[] =
265 {"Mix-%s", 2, MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_MK3_OPT_ANY, -1, },
266 {"Phones-%s", 2, MOTU_PA_OUT | MOTU_PA_RATE_1x|MOTU_PA_MK3_OPT_ANY, },
267 {"Analog%d", 8, MOTU_PA_INOUT | MOTU_PA_RATE_ANY|MOTU_PA_MK3_OPT_ANY, },
268 {"MainOut-%s", 2, MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_MK3_OPT_ANY, },
269 {"UnknownIn-%d", 4, MOTU_PA_IN | MOTU_PA_RATE_1x|MOTU_PA_MK3_OPT_ANY, },
270 {"AES/EBU%d", 2, MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_MK3_OPT_ANY, },
271 {"SPDIF%d", 2, MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_MK3_OPT_ANY, },
272 {"UnknownOut-%d", 2, MOTU_PA_OUT | MOTU_PA_RATE_1x|MOTU_PA_MK3_OPT_ANY, },
273 {"Toslink-A%d", 2, MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_MK3_OPT_A_TOSLINK|MOTU_PA_MK3_OPT_B_ANY, },
274 {"ADAT-A%d", 8, MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_MK3_OPT_A_ADAT|MOTU_PA_MK3_OPT_B_ANY, },
275 {"Toslink-A-extra-%d", 2, MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_MK3_OPT_A_TOSLINK|MOTU_PA_MK3_OPT_B_ANY, },
276 {"ADAT-A%d", 4, MOTU_PA_INOUT | MOTU_PA_RATE_2x|MOTU_PA_MK3_OPT_A_ADAT|MOTU_PA_MK3_OPT_B_ANY, },
277 {"Toslink-B%d", 2, MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_MK3_OPT_A_ANY|MOTU_PA_MK3_OPT_B_TOSLINK, },
278 {"Toslink-B-extra-%d", 2, MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_MK3_OPT_A_ANY|MOTU_PA_MK3_OPT_B_TOSLINK, },
279 {"ADAT-B%d", 8, MOTU_PA_INOUT | MOTU_PA_RATE_1x|MOTU_PA_MK3_OPT_A_ANY|MOTU_PA_MK3_OPT_B_ADAT, },
280 {"ADAT-B%d", 4, MOTU_PA_INOUT | MOTU_PA_RATE_2x|MOTU_PA_MK3_OPT_A_ANY|MOTU_PA_MK3_OPT_B_ADAT, },
283 /* Believed correct as of 24 Feb 2014. Thanks to Tim Radvan for testing. */
284 PortGroupEntry PortGroups_4PRE[] =
286 {"Mix-%s", 2, MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 1, },
287 {"Phones-%s", 2, MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 2, },
288 {"Mic/Line-%d", 2, MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 3, },
289 {"Mic/Guitar-%d", 2, MOTU_PA_IN | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 4, },
290 {"Analog%d", 2, MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 5, },
291 {"Main-%s", 2, MOTU_PA_OUT | MOTU_PA_RATE_ANY|MOTU_PA_OPTICAL_ANY, 0, },
292 {"SPDIF%d", 2, MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 6, },
293 {"Extra-%d", 2, MOTU_PA_INOUT | MOTU_PA_RATE_1x2x|MOTU_PA_OPTICAL_ANY, 7, },
296 #define PORTGROUPS(__model) PortGroups_ ## __model, N_ELEMENTS(PortGroups_ ## __model)
298 /* The order of DevicesProperty entries must match the numeric order of the
299 * MOTU model enumeration (EMotuModel).
301 const DevicePropertyEntry DevicesProperty[] = {
302 // { PortGroups_map, N_ELEMENTS( PortGroups_map ),
303 // Ports_map, N_ELEMENTS( Ports_map ), MaxSR, MixerDescrPtr, Mark3MixerDescrPtr },
304 { PORTGROUPS(828MKII), 96000, &Mixer_828Mk2, NULL, },
305 { PORTGROUPS(TRAVELER), 192000, &Mixer_Traveler, NULL, },
306 { PORTGROUPS(ULTRALITE), 96000, &Mixer_Ultralite, NULL, },
307 { PORTGROUPS(8PRE), 96000, &Mixer_8pre, NULL, },
308 { PORTGROUPS(828MKI), 48000, &Mixer_828Mk1, NULL, },
309 { PORTGROUPS(896HD), 192000, &Mixer_896HD, NULL, },
310 { PORTGROUPS(828mk3), 192000, },
311 { PORTGROUPS(ULTRALITEmk3), 192000, }, // Ultralite mk3
312 { PORTGROUPS(ULTRALITEmk3_hybrid), 192000, }, // Ultralite mk3 hybrid
313 { PORTGROUPS(TRAVELERmk3), 192000, },
314 { PORTGROUPS(896mk3), 192000, }, // 896 Mk 3
315 { PORTGROUPS(4PRE), 96000, },
318 MotuDevice::MotuDevice( DeviceManager& d, std::auto_ptr<ConfigRom>( configRom ))
319 : FFADODevice( d, configRom )
320 , m_motu_model( MOTU_MODEL_NONE )
321 , m_iso_recv_channel ( -1 )
322 , m_iso_send_channel ( -1 )
323 , m_rx_bandwidth ( -1 )
324 , m_tx_bandwidth ( -1 )
325 , m_rx_event_size ( 0 )
326 , m_tx_event_size ( 0 )
327 , m_receiveProcessor ( 0 )
328 , m_transmitProcessor ( 0 )
329 , m_MixerContainer ( NULL )
330 , m_ControlContainer ( NULL )
332 debugOutput( DEBUG_LEVEL_VERBOSE, "Created Motu::MotuDevice (NodeID %d)\n",
333 getConfigRom().getNodeId() );
336 MotuDevice::~MotuDevice()
338 delete m_receiveProcessor;
339 delete m_transmitProcessor;
341 // Free ieee1394 bus resources if they have been allocated
342 if (m_iso_recv_channel>=0 && !get1394Service().freeIsoChannel(m_iso_recv_channel)) {
343 debugOutput(DEBUG_LEVEL_VERBOSE, "Could not free recv iso channel %d\n", m_iso_recv_channel);
345 if (m_iso_send_channel>=0 && !get1394Service().freeIsoChannel(m_iso_send_channel)) {
346 debugOutput(DEBUG_LEVEL_VERBOSE, "Could not free send iso channel %d\n", m_iso_send_channel);
349 destroyMixer();
352 bool
353 MotuDevice::probe( Util::Configuration& c, ConfigRom& configRom, bool generic)
355 if(generic) return false;
357 unsigned int vendorId = configRom.getNodeVendorId();
358 unsigned int unitVersion = configRom.getUnitVersion();
359 unsigned int unitSpecifierId = configRom.getUnitSpecifierId();
361 for ( unsigned int i = 0;
362 i < ( sizeof( supportedDeviceList )/sizeof( VendorModelEntry ) );
363 ++i )
365 if ( ( supportedDeviceList[i].vendor_id == vendorId )
366 && ( supportedDeviceList[i].unit_version == unitVersion )
367 && ( supportedDeviceList[i].unit_specifier_id == unitSpecifierId )
370 if (supportedDeviceList[i].model == MOTU_MODEL_NONE) {
371 debugOutput( DEBUG_LEVEL_VERBOSE, "%s %s found but is not currently supported by FFADO\n",
372 supportedDeviceList[i].vendor_name, supportedDeviceList[i].model_name);
373 debugOutput( DEBUG_LEVEL_VERBOSE, " unitVersion=0x%08x\n", unitVersion);
374 return false;
377 return true;
381 return false;
384 FFADODevice *
385 MotuDevice::createDevice(DeviceManager& d, std::auto_ptr<ConfigRom>( configRom ))
387 return new MotuDevice(d, configRom);
390 bool
391 MotuDevice::discover()
393 unsigned int vendorId = getConfigRom().getNodeVendorId();
394 unsigned int unitVersion = getConfigRom().getUnitVersion();
395 unsigned int unitSpecifierId = getConfigRom().getUnitSpecifierId();
397 for ( unsigned int i = 0;
398 i < ( sizeof( supportedDeviceList )/sizeof( VendorModelEntry ) );
399 ++i )
401 if ( ( supportedDeviceList[i].vendor_id == vendorId )
402 && ( supportedDeviceList[i].unit_version == unitVersion )
403 && ( supportedDeviceList[i].unit_specifier_id == unitSpecifierId )
406 m_model = &(supportedDeviceList[i]);
407 m_motu_model=supportedDeviceList[i].model;
411 if (m_model == NULL) {
412 return false;
415 debugOutput( DEBUG_LEVEL_VERBOSE, "found %s %s\n",
416 m_model->vendor_name, m_model->model_name);
418 if (m_motu_model == MOTU_MODEL_NONE) {
419 debugOutput( DEBUG_LEVEL_VERBOSE, "This MOTU device is not currently supported by FFADO\n");
420 return false;
423 // The MOTU 8pre seems to power up in "converter" mode. To toggle it
424 // into "interface" mode it is necessary to do a write to the clock
425 // control register. Since setClockCtrlRegister() will only do a write
426 // if something is explicitly set it isn't sufficient to do something like
427 // setClockCtrlRegister(-1, MOTU_CLKSRC_UNCHANGED)
428 // Instead, we request that the clock source be set to its present value;
429 // effectively this preserves the interface's current clock settings.
430 if (m_motu_model == MOTU_MODEL_8PRE) {
431 setClockCtrlRegister(-1, getHwClockSource());
434 // The MOTU 828mk1 device seems to power up without a valid clock source
435 // configured (the relevant bits don't seem to map to anything sensible).
436 // Deal with this if necessary.
437 if (m_motu_model == MOTU_MODEL_828MkI) {
438 signed int csrc = getHwClockSource();
439 if (csrc == MOTU_CLKSRC_NONE)
440 csrc = MOTU_CLKSRC_INTERNAL;
441 setClockCtrlRegister(-1, csrc);
444 if (!buildMixer()) {
445 debugWarning("Could not build mixer\n");
448 return true;
451 enum FFADODevice::eStreamingState
452 MotuDevice::getStreamingState()
454 unsigned int val = ReadRegister(MOTU_REG_ISOCTRL);
455 /* Streaming is active if either bit 22 (Motu->PC streaming
456 * enable) or bit 30 (PC->Motu streaming enable) is set.
458 debugOutput(DEBUG_LEVEL_VERBOSE, "MOTU_REG_ISOCTRL: %08x\n", val);
460 if((val & 0x40400000) != 0) {
461 return eSS_Both;
462 } else if ((val & 0x40000000) != 0) {
463 return eSS_Receiving;
464 } else if ((val & 0x00400000) != 0) {
465 return eSS_Sending;
466 } else {
467 return eSS_Idle;
472 MotuDevice::getSamplingFrequency( ) {
474 * Retrieve the current sample rate from the MOTU device.
476 quadlet_t q = 0;
477 int rate = 0;
478 unsigned int rate_base_mask, rate_base48k;
479 unsigned int rate_mult_mask, rate_mult2, rate_mult4;
481 if (m_motu_model == MOTU_MODEL_828MkI) {
482 /* The original MOTU interfaces did things rather differently */
483 q = ReadRegister(MOTU_G1_REG_CONFIG);
484 if ((q & MOTU_G1_RATE_MASK) == MOTU_G1_RATE_44100)
485 rate = 44100;
486 else
487 rate = 48000;
488 return rate;
491 /* The way the rate is managed is the same across G2 and G3 devices,
492 * but the actual bits used in the clock control register is different.
494 if (getDeviceGeneration() == MOTU_DEVICE_G2) {
495 rate_base_mask = MOTU_RATE_BASE_MASK;
496 rate_base48k = MOTU_RATE_BASE_48000;
497 rate_mult_mask = MOTU_RATE_MULTIPLIER_MASK;
498 rate_mult2 = MOTU_RATE_MULTIPLIER_2X;
499 rate_mult4 = MOTU_RATE_MULTIPLIER_4X;
500 } else {
501 rate_base_mask = MOTU_G3_RATE_BASE_MASK;
502 rate_base48k = MOTU_G3_RATE_BASE_48000;
503 rate_mult_mask = MOTU_G3_RATE_MULTIPLIER_MASK;
504 rate_mult2 = MOTU_G3_RATE_MULTIPLIER_2X;
505 rate_mult4 = MOTU_G3_RATE_MULTIPLIER_4X;
508 q = ReadRegister(MOTU_REG_CLK_CTRL);
509 if ((q & rate_base_mask) == rate_base48k)
510 rate = 48000;
511 else
512 rate = 44100;
513 if ((q & rate_mult_mask) == rate_mult4)
514 rate *= 4;
515 else
516 if ((q & rate_mult_mask) == rate_mult2)
517 rate *= 2;
519 return rate;
523 MotuDevice::getConfigurationId()
525 return 0;
528 unsigned int
529 MotuDevice::getHwClockSource()
531 unsigned int reg;
533 if (m_motu_model == MOTU_MODEL_828MkI) {
534 reg = ReadRegister(MOTU_G1_REG_CONFIG);
535 switch (reg & MOTU_G1_CLKSRC_MASK) {
536 case MOTU_G1_CLKSRC_INTERNAL: return MOTU_CLKSRC_INTERNAL;
537 case MOTU_G1_CLKSRC_ADAT_9PIN: return MOTU_CLKSRC_ADAT_9PIN;
538 case MOTU_G1_CLKSRC_SPDIF: return MOTU_CLKSRC_SPDIF_TOSLINK;
539 case MOTU_G1_CLKSRC_ADAT_OPTICAL: return MOTU_CLKSRC_ADAT_OPTICAL;
541 return MOTU_CLKSRC_NONE;
544 reg = ReadRegister(MOTU_REG_CLK_CTRL);
545 if (getDeviceGeneration() == MOTU_DEVICE_G2) {
546 switch (reg & MOTU_G2_CLKSRC_MASK) {
547 case MOTU_G2_CLKSRC_INTERNAL: return MOTU_CLKSRC_INTERNAL;
548 case MOTU_G2_CLKSRC_ADAT_OPTICAL: return MOTU_CLKSRC_ADAT_OPTICAL;
549 case MOTU_G2_CLKSRC_SPDIF_TOSLINK: return MOTU_CLKSRC_SPDIF_TOSLINK;
550 case MOTU_G2_CLKSRC_SMPTE: return MOTU_CLKSRC_SMPTE;
551 case MOTU_G2_CLKSRC_WORDCLOCK: return MOTU_CLKSRC_WORDCLOCK;
552 case MOTU_G2_CLKSRC_ADAT_9PIN: return MOTU_CLKSRC_ADAT_9PIN;
553 case MOTU_G2_CLKSRC_AES_EBU: return MOTU_CLKSRC_AES_EBU;
555 } else {
556 /* Handle G3 devices */
557 switch (reg & MOTU_G3_CLKSRC_MASK) {
558 case MOTU_G3_CLKSRC_INTERNAL: return MOTU_CLKSRC_INTERNAL;
559 case MOTU_G3_CLKSRC_SPDIF: return MOTU_CLKSRC_SPDIF_TOSLINK;
560 case MOTU_G3_CLKSRC_SMPTE: return MOTU_CLKSRC_SMPTE;
561 case MOTU_G3_CLKSRC_WORDCLOCK: return MOTU_CLKSRC_WORDCLOCK;
562 case MOTU_G3_CLKSRC_OPTICAL_A: return MOTU_CLKSRC_OPTICAL_A;
563 case MOTU_G3_CLKSRC_OPTICAL_B: return MOTU_CLKSRC_OPTICAL_B;
566 return MOTU_CLKSRC_NONE;
569 #include <unistd.h>
571 bool
572 MotuDevice::setClockCtrlRegister(signed int samplingFrequency, unsigned int clock_source)
575 * Set the MOTU device's samplerate and/or clock source via the clock
576 * control register. If samplingFrequency <= 0 it remains unchanged. If
577 * clock_source is MOTU_CLKSRC_UNCHANGED the clock source remains unchanged.
579 const char *src_name;
580 quadlet_t q, new_rate=0xffffffff;
581 signed int i, supported=true, cancel_adat=false;
582 quadlet_t reg;
583 unsigned int rate_mask = 0;
584 unsigned int old_clock_src = getHwClockSource();
585 signed int device_gen = getDeviceGeneration();
587 /* Don't touch anything if there's nothing to do */
588 if (samplingFrequency<=0 && clock_source==MOTU_CLKSRC_NONE)
589 return true;
591 if ( samplingFrequency > DevicesProperty[m_motu_model-1].MaxSampleRate )
592 return false;
594 /* The original MOTU devices do things differently; they are much
595 * simpler than the later interfaces.
597 if (m_motu_model == MOTU_MODEL_828MkI) {
598 reg = ReadRegister(MOTU_G1_REG_CONFIG);
599 if (samplingFrequency > 0) {
600 reg &= ~MOTU_G1_RATE_MASK;
601 switch (samplingFrequency) {
602 case 44100:
603 reg |= MOTU_G1_RATE_44100;
604 break;
605 case 48000:
606 reg |= MOTU_G1_RATE_48000;
607 break;
608 default:
609 // Unsupported rate
610 return false;
613 if (clock_source != MOTU_CLKSRC_UNCHANGED) {
614 switch (clock_source) {
615 case MOTU_CLKSRC_INTERNAL:
616 clock_source = MOTU_G1_CLKSRC_INTERNAL; break;
617 case MOTU_CLKSRC_SPDIF_TOSLINK:
618 clock_source = MOTU_G1_CLKSRC_SPDIF; break;
619 case MOTU_CLKSRC_ADAT_9PIN:
620 clock_source = MOTU_G1_CLKSRC_ADAT_9PIN; break;
621 case MOTU_CLKSRC_ADAT_OPTICAL:
622 clock_source = MOTU_G1_CLKSRC_ADAT_OPTICAL; break;
623 default:
624 // Unsupported clock source
625 return false;
627 reg &= ~MOTU_G1_CLKSRC_MASK;
628 reg |= clock_source;
630 if (WriteRegister(MOTU_G1_REG_CONFIG, reg) != 0)
631 return false;
632 return true;
635 /* The rest of this function deals with later generation devices */
637 reg = ReadRegister(MOTU_REG_CLK_CTRL);
639 /* The method of controlling the sampling rate is the same for G2/G3
640 * devices but the actual bits used in the rate control register differ.
642 if (device_gen == MOTU_DEVICE_G2) {
643 rate_mask = MOTU_RATE_BASE_MASK | MOTU_RATE_MULTIPLIER_MASK;
644 switch ( samplingFrequency ) {
645 case -1: break;
646 case 44100: new_rate = MOTU_RATE_BASE_44100 | MOTU_RATE_MULTIPLIER_1X; break;
647 case 48000: new_rate = MOTU_RATE_BASE_48000 | MOTU_RATE_MULTIPLIER_1X; break;
648 case 88200: new_rate = MOTU_RATE_BASE_44100 | MOTU_RATE_MULTIPLIER_2X; break;
649 case 96000: new_rate = MOTU_RATE_BASE_48000 | MOTU_RATE_MULTIPLIER_2X; break;
650 case 176400: new_rate = MOTU_RATE_BASE_44100 | MOTU_RATE_MULTIPLIER_4X; break;
651 case 192000: new_rate = MOTU_RATE_BASE_48000 | MOTU_RATE_MULTIPLIER_4X; break;
652 default:
653 supported=false;
655 } else
656 if (device_gen == MOTU_DEVICE_G3) {
657 rate_mask = MOTU_G3_RATE_BASE_MASK | MOTU_G3_RATE_MULTIPLIER_MASK;
658 switch ( samplingFrequency ) {
659 case -1: break;
660 case 44100: new_rate = MOTU_G3_RATE_BASE_44100 | MOTU_G3_RATE_MULTIPLIER_1X; break;
661 case 48000: new_rate = MOTU_G3_RATE_BASE_48000 | MOTU_G3_RATE_MULTIPLIER_1X; break;
662 case 88200: new_rate = MOTU_G3_RATE_BASE_44100 | MOTU_G3_RATE_MULTIPLIER_2X; break;
663 case 96000: new_rate = MOTU_G3_RATE_BASE_48000 | MOTU_G3_RATE_MULTIPLIER_2X; break;
664 case 176400: new_rate = MOTU_G3_RATE_BASE_44100 | MOTU_G3_RATE_MULTIPLIER_4X; break;
665 case 192000: new_rate = MOTU_G3_RATE_BASE_48000 | MOTU_G3_RATE_MULTIPLIER_4X; break;
666 default:
667 supported=false;
670 /* ADAT output is only possible for sample rates up to 96 kHz. For
671 * anything higher, force the ADAT channels off.
673 if (samplingFrequency > 96000) {
674 cancel_adat = true;
677 // Sanity check the clock source
678 if (clock_source>MOTU_CLKSRC_LAST && clock_source!=MOTU_CLKSRC_UNCHANGED)
679 supported = false;
681 // Update the clock control register. FIXME: while this is now rather
682 // comprehensive there may still be a need to manipulate MOTU_REG_CLK_CTRL
683 // a little more than we do.
684 if (supported) {
686 // If optical port must be disabled (because a 4x sample rate has
687 // been selected) then do so before changing the sample rate. At
688 // this stage it will be up to the user to re-enable the optical
689 // port if the sample rate is set to a 1x or 2x rate later.
690 if (cancel_adat) {
691 setOpticalMode(MOTU_CTRL_DIR_INOUT, MOTU_OPTICAL_MODE_OFF, MOTU_OPTICAL_MODE_OFF);
694 // Set up new frequency if requested
695 if (new_rate != 0xffffffff) {
696 reg &= ~rate_mask;
697 reg |= new_rate;
700 // Set up new clock source if required
701 if (clock_source != MOTU_CLKSRC_UNCHANGED) {
702 if (device_gen == MOTU_DEVICE_G2) {
703 reg &= ~MOTU_G2_CLKSRC_MASK;
704 switch (clock_source) {
705 case MOTU_CLKSRC_INTERNAL: reg |= MOTU_G2_CLKSRC_INTERNAL; break;
706 case MOTU_CLKSRC_ADAT_OPTICAL: reg |= MOTU_G2_CLKSRC_ADAT_OPTICAL; break;
707 case MOTU_CLKSRC_SPDIF_TOSLINK: reg |= MOTU_G2_CLKSRC_SPDIF_TOSLINK; break;
708 case MOTU_CLKSRC_SMPTE: reg |= MOTU_G2_CLKSRC_SMPTE; break;
709 case MOTU_CLKSRC_WORDCLOCK: reg |= MOTU_G2_CLKSRC_WORDCLOCK; break;
710 case MOTU_CLKSRC_ADAT_9PIN: reg |= MOTU_G2_CLKSRC_ADAT_9PIN; break;
711 case MOTU_CLKSRC_AES_EBU: reg |= MOTU_G2_CLKSRC_AES_EBU; break;
713 } else {
714 reg &= ~MOTU_G3_CLKSRC_MASK;
715 switch (clock_source) {
716 case MOTU_CLKSRC_INTERNAL: reg |= MOTU_G3_CLKSRC_INTERNAL; break;
717 case MOTU_CLKSRC_SPDIF_TOSLINK: reg |= MOTU_G3_CLKSRC_SPDIF; break;
718 case MOTU_CLKSRC_SMPTE: reg |= MOTU_G3_CLKSRC_SMPTE; break;
719 case MOTU_CLKSRC_WORDCLOCK: reg |= MOTU_G3_CLKSRC_WORDCLOCK; break;
720 case MOTU_CLKSRC_OPTICAL_A: reg |= MOTU_G3_CLKSRC_OPTICAL_A; break;
721 case MOTU_CLKSRC_OPTICAL_B: reg |= MOTU_G3_CLKSRC_OPTICAL_B; break;
724 } else {
725 /* Use the device's current clock source to set the clock
726 * source name registers, which must be done even if we aren't
727 * changing the clock source.
729 clock_source = old_clock_src;
732 // Bits 24-26 of MOTU_REG_CLK_CTRL behave a little differently
733 // depending on the model. In addition, different bit patterns are
734 // written depending on whether streaming is enabled, disabled or is
735 // changing state. For now we go with the combination used when
736 // streaming is enabled since it seems to work for the other states
737 // as well. Since device muting can be effected by these bits, we
738 // may utilise this in future during streaming startup to prevent
739 // noises during stabilisation.
741 // For most models (possibly all except the Ultralite) all 3 bits
742 // can be zero and audio is still output.
744 // For the Traveler, if bit 26 is set (as it is under other OSes),
745 // bit 25 functions as a device mute bit: if set, audio is output
746 // while if 0 the entire device is muted. If bit 26 is unset,
747 // setting bit 25 doesn't appear to be detrimental.
749 // For the Ultralite, other OSes leave bit 26 unset. However, unlike
750 // other devices bit 25 seems to function as a mute bit in this case.
752 // The function of bit 24 is currently unknown. Other OSes set it
753 // for all devices so we will too.
754 reg &= 0xf8ffffff;
755 if (m_motu_model == MOTU_MODEL_TRAVELER)
756 reg |= 0x04000000;
757 reg |= 0x03000000;
758 if (WriteRegister(MOTU_REG_CLK_CTRL, reg) == 0) {
759 supported=true;
760 } else {
761 supported=false;
764 // A write to the rate/clock control register requires the
765 // textual name of the current clock source be sent to the
766 // clock source name registers. This appears to be the same for
767 // both G2 and G3 devices.
768 switch (clock_source) {
769 case MOTU_CLKSRC_INTERNAL:
770 src_name = "Internal ";
771 break;
772 case MOTU_CLKSRC_ADAT_OPTICAL:
773 src_name = "ADAT Optical ";
774 break;
775 case MOTU_CLKSRC_SPDIF_TOSLINK: {
776 unsigned int p0_mode;
777 if (device_gen < MOTU_DEVICE_G3) {
778 getOpticalMode(MOTU_DIR_IN, &p0_mode, NULL);
779 } else
780 p0_mode = MOTU_OPTICAL_MODE_OFF;
781 if (p0_mode == MOTU_OPTICAL_MODE_TOSLINK)
782 src_name = "TOSLink ";
783 else
784 src_name = "SPDIF ";
785 break;
787 case MOTU_CLKSRC_SMPTE:
788 src_name = "SMPTE ";
789 break;
790 case MOTU_CLKSRC_WORDCLOCK:
791 src_name = "Word Clock In ";
792 break;
793 case MOTU_CLKSRC_ADAT_9PIN:
794 src_name = "ADAT 9-pin ";
795 break;
796 case MOTU_CLKSRC_AES_EBU:
797 src_name = "AES-EBU ";
798 break;
799 case MOTU_CLKSRC_OPTICAL_A: {
800 unsigned int p0_mode;
801 getOpticalMode(MOTU_DIR_IN, &p0_mode, NULL);
802 if (p0_mode == MOTU_OPTICAL_MODE_TOSLINK)
803 src_name = "Toslink-A ";
804 else
805 src_name = "ADAT-A Optical ";
806 break;
808 case MOTU_CLKSRC_OPTICAL_B: {
809 unsigned int p1_mode;
810 getOpticalMode(MOTU_DIR_IN, NULL, &p1_mode);
811 if (p1_mode == MOTU_OPTICAL_MODE_TOSLINK)
812 src_name = "Toslink-B ";
813 else
814 src_name = "ADAT-B Optical ";
815 break;
817 default:
818 src_name = "Unknown ";
820 for (i=0; i<16; i+=4) {
821 q = (src_name[i]<<24) | (src_name[i+1]<<16) |
822 (src_name[i+2]<<8) | src_name[i+3];
823 WriteRegister(MOTU_REG_CLKSRC_NAME0+i, q);
826 return supported;
829 bool
830 MotuDevice::setSamplingFrequency( int samplingFrequency )
833 * Set the MOTU device's samplerate.
835 return setClockCtrlRegister(samplingFrequency, MOTU_CLKSRC_UNCHANGED);
838 std::vector<int>
839 MotuDevice::getSupportedSamplingFrequencies()
841 std::vector<int> frequencies;
842 signed int max_freq = DevicesProperty[m_motu_model-1].MaxSampleRate;
844 /* All MOTUs support 1x rates. All others must be conditional. */
845 frequencies.push_back(44100);
846 frequencies.push_back(48000);
848 if (88200 <= max_freq)
849 frequencies.push_back(88200);
850 if (96000 <= max_freq)
851 frequencies.push_back(96000);
852 if (176400 <= max_freq)
853 frequencies.push_back(176400);
854 if (192000 <= max_freq)
855 frequencies.push_back(192000);
856 return frequencies;
859 FFADODevice::ClockSource
860 MotuDevice::clockIdToClockSource(unsigned int id) {
861 ClockSource s;
862 signed int device_gen = getDeviceGeneration();
863 s.id = id;
865 // Assume a clock source is valid/active unless otherwise overridden.
866 s.valid = true;
867 s.locked = true;
868 s.active = true;
870 switch (id) {
871 case MOTU_CLKSRC_INTERNAL:
872 s.type = eCT_Internal;
873 s.description = "Internal sync";
874 break;
875 case MOTU_CLKSRC_ADAT_OPTICAL:
876 s.type = eCT_ADAT;
877 s.description = "ADAT optical";
878 s.valid = s.active = s.locked = (device_gen!=MOTU_DEVICE_G1);
879 break;
880 case MOTU_CLKSRC_SPDIF_TOSLINK:
881 s.type = eCT_SPDIF;
882 if (device_gen < MOTU_DEVICE_G3)
883 s.description = "SPDIF/Toslink";
884 else
885 s.description = "SPDIF";
886 break;
887 case MOTU_CLKSRC_SMPTE:
888 s.type = eCT_SMPTE;
889 s.description = "SMPTE";
890 // Since we don't currently know how to deal with SMPTE on these
891 // devices make sure the SMPTE clock source is disabled.
892 s.valid = false;
893 s.active = false;
894 s.locked = false;
895 break;
896 case MOTU_CLKSRC_WORDCLOCK:
897 s.type = eCT_WordClock;
898 s.description = "Wordclock";
899 s.valid = s.active = s.locked = (device_gen!=MOTU_DEVICE_G1);
900 break;
901 case MOTU_CLKSRC_ADAT_9PIN:
902 s.type = eCT_ADAT;
903 s.description = "ADAT 9-pin";
904 break;
905 case MOTU_CLKSRC_AES_EBU:
906 s.type = eCT_AES;
907 s.description = "AES/EBU";
908 s.valid = s.active = s.locked = (device_gen!=MOTU_DEVICE_G1);
909 break;
910 case MOTU_CLKSRC_OPTICAL_A:
911 s.type = eCT_ADAT;
912 s.description = "ADAT/Toslink port A";
913 break;
914 case MOTU_CLKSRC_OPTICAL_B:
915 s.type = eCT_ADAT;
916 s.description = "ADAT/Toslink port B";
917 break;
918 default:
919 s.type = eCT_Invalid;
922 s.slipping = false;
923 return s;
926 FFADODevice::ClockSourceVector
927 MotuDevice::getSupportedClockSources() {
928 FFADODevice::ClockSourceVector r;
929 ClockSource s;
930 signed int device_gen = getDeviceGeneration();
932 /* Form a list of clocks supported by MOTU interfaces */
934 /* All interfaces support an internal clock */
935 s = clockIdToClockSource(MOTU_CLKSRC_INTERNAL);
936 r.push_back(s);
938 if (device_gen==MOTU_DEVICE_G2 || device_gen==MOTU_DEVICE_G1) {
939 s = clockIdToClockSource(MOTU_CLKSRC_ADAT_OPTICAL);
940 r.push_back(s);
943 s = clockIdToClockSource(MOTU_CLKSRC_SPDIF_TOSLINK);
944 r.push_back(s);
945 s = clockIdToClockSource(MOTU_CLKSRC_SMPTE);
946 r.push_back(s);
948 /* The 828mk1 didn't have a wordclock sync option */
949 if (device_gen != MOTU_DEVICE_G1) {
950 s = clockIdToClockSource(MOTU_CLKSRC_WORDCLOCK);
951 r.push_back(s);
954 /* The 9-pin ADAT sync was only present on selected G2
955 * devices. The 828mk1 also offered this.
957 if (m_motu_model==MOTU_MODEL_828mkII || m_motu_model==MOTU_MODEL_TRAVELER ||
958 m_motu_model==MOTU_MODEL_896HD || m_motu_model==MOTU_MODEL_828MkI) {
959 s = clockIdToClockSource(MOTU_CLKSRC_ADAT_9PIN);
960 r.push_back(s);
962 /* AES/EBU is present on the G2/G3 Travelers and 896HDs */
963 if (m_motu_model==MOTU_MODEL_TRAVELER || m_motu_model==MOTU_MODEL_TRAVELERmk3 ||
964 m_motu_model==MOTU_MODEL_896HD || m_motu_model==MOTU_MODEL_896mk3) {
965 s = clockIdToClockSource(MOTU_CLKSRC_AES_EBU);
966 r.push_back(s);
969 /* Dual-port ADAT is a feature of the G3 devices, and then only some */
970 if (m_motu_model==MOTU_MODEL_828mk3 || m_motu_model==MOTU_MODEL_TRAVELERmk3 ||
971 m_motu_model==MOTU_MODEL_896mk3) {
972 s = clockIdToClockSource(MOTU_CLKSRC_OPTICAL_A);
973 r.push_back(s);
974 s = clockIdToClockSource(MOTU_CLKSRC_OPTICAL_B);
975 r.push_back(s);
978 return r;
981 bool
982 MotuDevice::setActiveClockSource(ClockSource s) {
983 debugOutput(DEBUG_LEVEL_VERBOSE, "setting clock source to id: %d\n",s.id);
985 // FIXME: this could do with some error checking
986 return setClockCtrlRegister(-1, s.id);
989 FFADODevice::ClockSource
990 MotuDevice::getActiveClockSource() {
991 ClockSource s;
992 quadlet_t clock_id = getHwClockSource();
993 s = clockIdToClockSource(clock_id);
994 s.active = true;
995 return s;
998 bool
999 MotuDevice::lock() {
1001 return true;
1005 bool
1006 MotuDevice::unlock() {
1008 return true;
1011 void
1012 MotuDevice::showDevice()
1014 debugOutput(DEBUG_LEVEL_VERBOSE,
1015 "%s %s at node %d\n", m_model->vendor_name, m_model->model_name,
1016 getNodeId());
1019 bool
1020 MotuDevice::prepare() {
1022 int samp_freq = getSamplingFrequency();
1023 unsigned int optical_in_mode_a, optical_out_mode_a;
1024 unsigned int optical_in_mode_b, optical_out_mode_b;
1025 unsigned int event_size_in;
1026 unsigned int event_size_out;
1028 debugOutput(DEBUG_LEVEL_NORMAL, "Preparing MotuDevice...\n" );
1030 /* The 828mk1 powers up without the optical mode register fields set
1031 * to anything in particular. For this interface, hardcode a default
1032 * optical mode if it appears that the interface is uninitialised.
1033 * On powerup, the unknown-2 register is 0xffffffff and this is reset
1034 * by software to 0; this provides a convenient test for the status
1035 * of the interface.
1037 if (m_motu_model==MOTU_MODEL_828MkI && ReadRegister(MOTU_G1_REG_UNKNOWN_2)!=0) {
1038 optical_in_mode_a = optical_out_mode_a = MOTU_OPTICAL_MODE_OFF;
1039 optical_in_mode_b = optical_out_mode_b = MOTU_OPTICAL_MODE_NONE;
1040 } else {
1041 getOpticalMode(MOTU_DIR_IN, &optical_in_mode_a, &optical_in_mode_b);
1042 getOpticalMode(MOTU_DIR_OUT, &optical_out_mode_a, &optical_out_mode_b);
1045 // Initialise port groups and determine the event sizes based on the
1046 // current device mode and sample rate.
1047 initDirPortGroups(Streaming::Port::E_Capture, samp_freq, optical_in_mode_a, optical_in_mode_b);
1048 initDirPortGroups(Streaming::Port::E_Playback, samp_freq, optical_out_mode_a, optical_out_mode_b);
1050 event_size_in = getEventSize(MOTU_DIR_IN);
1051 event_size_out= getEventSize(MOTU_DIR_OUT);
1053 // Explicitly set the optical mode, primarily to ensure that the
1054 // MOTU_REG_OPTICAL_CTRL register is initialised. We need to do this to
1055 // because some interfaces (the Ultralite for example) appear to power
1056 // up without this set to anything sensible. In this case, writes to
1057 // MOTU_REG_ISOCTRL fail more often than not, which is bad.
1058 setOpticalMode(MOTU_DIR_IN, optical_in_mode_a, optical_in_mode_b);
1059 setOpticalMode(MOTU_DIR_OUT, optical_out_mode_a, optical_out_mode_b);
1061 // The original 828 (aka 828mk1) was seen to write to these two
1062 // registers as part of its startup routine. It's not entirely clear
1063 // what these registers control. The inclusion of the local node ID is
1064 // an educated guess on experiments with bus topology when using other
1065 // systems.
1066 if (m_motu_model == MOTU_MODEL_828MkI) {
1067 WriteRegister(MOTU_G1_REG_UNKNOWN_1,
1068 0xffc00001 | ((get1394Service().getLocalNodeId()&0x3f)<<16));
1069 WriteRegister(MOTU_G1_REG_UNKNOWN_2, 0x00000000);
1072 // Allocate bandwidth if not previously done.
1073 // FIXME: The bandwidth allocation calculation can probably be
1074 // refined somewhat since this is currently based on a rudimentary
1075 // understanding of the ieee1394 iso protocol.
1076 // Currently we assume the following.
1077 // * Ack/iso gap = 0.05 us
1078 // * DATA_PREFIX = 0.16 us
1079 // * DATA_END = 0.26 us
1080 // These numbers are the worst-case figures given in the ieee1394
1081 // standard. This gives approximately 0.5 us of overheads per packet -
1082 // around 25 bandwidth allocation units (from the ieee1394 standard 1
1083 // bandwidth allocation unit is 125/6144 us). We further assume the
1084 // MOTU is running at S400 (which it should be) so one allocation unit
1085 // is equivalent to 1 transmitted byte; thus the bandwidth allocation
1086 // required for the packets themselves is just the size of the packet.
1087 // We used to allocate based on the maximum packet size (1160 bytes at
1088 // 192 kHz for the traveler) but now do this based on the actual device
1089 // state by utilising the result from getEventSize() and remembering
1090 // that each packet has an 8 byte CIP header. Note that bandwidth is
1091 // allocated on a *per stream* basis - it must be allocated for both the
1092 // transmit and receive streams. While most MOTU modules are close to
1093 // symmetric in terms of the number of in/out channels there are
1094 // exceptions, so we deal with receive and transmit bandwidth separately.
1095 signed int n_events_per_packet = samp_freq<=48000?8:(samp_freq<=96000?16:32);
1096 m_rx_bandwidth = 25 + (n_events_per_packet*event_size_in);
1097 m_tx_bandwidth = 25 + (n_events_per_packet*event_size_out);
1099 // Assign iso channels if not already done
1100 if (m_iso_send_channel < 0)
1101 m_iso_send_channel = get1394Service().allocateIsoChannelGeneric(m_tx_bandwidth);
1103 if (m_iso_recv_channel < 0)
1104 m_iso_recv_channel = get1394Service().allocateIsoChannelGeneric(m_rx_bandwidth);
1106 debugOutput(DEBUG_LEVEL_VERBOSE, "recv channel = %d, send channel = %d\n",
1107 m_iso_recv_channel, m_iso_send_channel);
1109 if (m_iso_recv_channel<0 || m_iso_send_channel<0) {
1110 // be nice and deallocate
1111 if (m_iso_recv_channel >= 0)
1112 get1394Service().freeIsoChannel(m_iso_recv_channel);
1113 if (m_iso_send_channel >= 0)
1114 get1394Service().freeIsoChannel(m_iso_send_channel);
1116 debugFatal("Could not allocate iso channels!\n");
1117 return false;
1120 // get the device specific and/or global SP configuration
1121 Util::Configuration &config = getDeviceManager().getConfiguration();
1122 // base value is the config.h value
1123 float recv_sp_dll_bw = STREAMPROCESSOR_DLL_BW_HZ;
1124 float xmit_sp_dll_bw = STREAMPROCESSOR_DLL_BW_HZ;
1126 // we can override that globally
1127 config.getValueForSetting("streaming.spm.recv_sp_dll_bw", recv_sp_dll_bw);
1128 config.getValueForSetting("streaming.spm.xmit_sp_dll_bw", xmit_sp_dll_bw);
1130 // or override in the device section
1131 config.getValueForDeviceSetting(getConfigRom().getNodeVendorId(), getConfigRom().getModelId(), "recv_sp_dll_bw", recv_sp_dll_bw);
1132 config.getValueForDeviceSetting(getConfigRom().getNodeVendorId(), getConfigRom().getModelId(), "xmit_sp_dll_bw", xmit_sp_dll_bw);
1134 m_receiveProcessor=new Streaming::MotuReceiveStreamProcessor(*this, event_size_in);
1135 m_receiveProcessor->setVerboseLevel(getDebugLevel());
1137 // The first thing is to initialize the processor. This creates the
1138 // data structures.
1139 if(!m_receiveProcessor->init()) {
1140 debugFatal("Could not initialize receive processor!\n");
1141 return false;
1144 if(!m_receiveProcessor->setDllBandwidth(recv_sp_dll_bw)) {
1145 debugFatal("Could not set DLL bandwidth\n");
1146 delete m_receiveProcessor;
1147 m_receiveProcessor = NULL;
1148 return false;
1151 // Now we add ports to the processor
1152 debugOutput(DEBUG_LEVEL_VERBOSE,"Adding ports to receive processor\n");
1154 char *buff;
1155 Streaming::Port *p=NULL;
1157 // retrieve the ID
1158 std::string id=std::string("dev?");
1159 if(!getOption("id", id)) {
1160 debugWarning("Could not retrieve id parameter, defaulting to 'dev?'\n");
1163 // Add audio capture ports
1164 if (!addDirPortGroups(Streaming::Port::E_Capture, samp_freq, optical_in_mode_a, optical_in_mode_b)) {
1165 return false;
1168 // Add MIDI port. Every MOTU interface except the original 828 has
1169 // exactly one MIDI input port, with each MIDI byte sent using a 3 byte
1170 // sequence starting at byte 4 of the event data.
1171 if (m_motu_model != MOTU_MODEL_828MkI) {
1172 asprintf(&buff,"%s_cap_MIDI0",id.c_str());
1173 p = new Streaming::MotuMidiPort(*m_receiveProcessor, buff,
1174 Streaming::Port::E_Capture, 4);
1175 if (!p) {
1176 debugOutput(DEBUG_LEVEL_VERBOSE, "Skipped port %s\n", buff);
1178 free(buff);
1181 // example of adding an control port:
1182 // asprintf(&buff,"%s_cap_%s",id.c_str(),"myportnamehere");
1183 // p=new Streaming::MotuControlPort(
1184 // buff,
1185 // Streaming::Port::E_Capture,
1186 // 0 // you can add all other port specific stuff you
1187 // // need to pass by extending MotuXXXPort and MotuPortInfo
1188 // );
1189 // free(buff);
1191 // if (!p) {
1192 // debugOutput(DEBUG_LEVEL_VERBOSE, "Skipped port %s\n",buff);
1193 // } else {
1195 // if (!m_receiveProcessor->addPort(p)) {
1196 // debugWarning("Could not register port with stream processor\n");
1197 // return false;
1198 // } else {
1199 // debugOutput(DEBUG_LEVEL_VERBOSE, "Added port %s\n",buff);
1200 // }
1201 // }
1203 // Do the same for the transmit processor
1204 m_transmitProcessor=new Streaming::MotuTransmitStreamProcessor(*this, event_size_out);
1206 m_transmitProcessor->setVerboseLevel(getDebugLevel());
1208 if(!m_transmitProcessor->init()) {
1209 debugFatal("Could not initialize transmit processor!\n");
1210 return false;
1213 if(!m_transmitProcessor->setDllBandwidth(xmit_sp_dll_bw)) {
1214 debugFatal("Could not set DLL bandwidth\n");
1215 delete m_transmitProcessor;
1216 m_transmitProcessor = NULL;
1217 return false;
1220 // Now we add ports to the processor
1221 debugOutput(DEBUG_LEVEL_VERBOSE,"Adding ports to transmit processor\n");
1223 // Add audio playback ports
1224 if (!addDirPortGroups(Streaming::Port::E_Playback, samp_freq, optical_out_mode_a, optical_out_mode_b)) {
1225 return false;
1228 // Add MIDI port. Every MOTU interface except the original 828 has
1229 // exactly one MIDI input port, with each MIDI byte sent using a 3 byte
1230 // sequence starting at byte 4 of the event data.
1231 if (m_motu_model != MOTU_MODEL_828MkI) {
1232 asprintf(&buff,"%s_pbk_MIDI0",id.c_str());
1233 p = new Streaming::MotuMidiPort(*m_transmitProcessor, buff,
1234 Streaming::Port::E_Playback, 4);
1235 if (!p) {
1236 debugOutput(DEBUG_LEVEL_VERBOSE, "Skipped port %s\n", buff);
1238 free(buff);
1241 // example of adding an control port:
1242 // asprintf(&buff,"%s_pbk_%s",id.c_str(),"myportnamehere");
1244 // p=new Streaming::MotuControlPort(
1245 // buff,
1246 // Streaming::Port::E_Playback,
1247 // 0 // you can add all other port specific stuff you
1248 // // need to pass by extending MotuXXXPort and MotuPortInfo
1249 // );
1250 // free(buff);
1252 // if (!p) {
1253 // debugOutput(DEBUG_LEVEL_VERBOSE, "Skipped port %s\n",buff);
1254 // } else {
1255 // if (!m_transmitProcessor->addPort(p)) {
1256 // debugWarning("Could not register port with stream processor\n");
1257 // return false;
1258 // } else {
1259 // debugOutput(DEBUG_LEVEL_VERBOSE, "Added port %s\n",buff);
1260 // }
1261 // }
1263 return true;
1267 MotuDevice::getStreamCount() {
1268 return 2; // one receive, one transmit
1271 Streaming::StreamProcessor *
1272 MotuDevice::getStreamProcessorByIndex(int i) {
1273 switch (i) {
1274 case 0:
1275 return m_receiveProcessor;
1276 case 1:
1277 return m_transmitProcessor;
1278 default:
1279 return NULL;
1281 return 0;
1284 bool
1285 MotuDevice::startStreamByIndex(int i) {
1287 quadlet_t isoctrl = ReadRegister(MOTU_REG_ISOCTRL);
1289 if (m_motu_model == MOTU_MODEL_828MkI) {
1290 // The 828MkI device does this differently. In particular it does
1291 // not appear possible to enable tx and rx separately since they
1292 // share a global enable (MOTU_G1_C1_ISO_ENABLE). Therefore we
1293 // enable both when the 0th index is requested and ignore any
1294 // request for index 1. Also note that on the G1 devices,
1295 // MOTU_REG_ISOCTRL and MOTU_G1_REG_CONFIG are one and the same.
1297 quadlet_t config2_reg = ReadRegister(MOTU_G1_REG_CONFIG_2);
1299 if (i == 1)
1300 return true;
1301 m_receiveProcessor->setChannel(m_iso_recv_channel);
1302 m_transmitProcessor->setChannel(m_iso_send_channel);
1304 /* Start with a setting of the config2 register. The purpose of
1305 * doing this is unclear, but it's done by other drivers so we
1306 * should too, at least until we have something working.
1308 WriteRegister(MOTU_G1_REG_CONFIG_2, config2_reg);
1310 /* Send the iso details to the control register. Note that as for
1311 * other MOTU devices bit 24 enables changes to the MOTU's iso tx
1312 * settings while bit 31 enables iso rx changes.
1314 debugOutput(DEBUG_LEVEL_VERBOSE, "MOTU g1: read isoctl: %x\n", isoctrl);
1315 debugOutput(DEBUG_LEVEL_VERBOSE, "MOTU g1: read config2: %x\n", config2_reg);
1316 isoctrl &= ~MOTU_G1_C1_ISO_INFO_MASK;
1317 isoctrl |= (MOTU_G1_C1_ISO_TX_WREN | MOTU_G1_C1_ISO_RX_WREN);
1318 isoctrl |= (m_iso_recv_channel << MOTU_G1_C1_ISO_TX_CH_BIT0);
1319 isoctrl |= (m_iso_send_channel << MOTU_G1_C1_ISO_RX_CH_BIT0);
1320 isoctrl |= (MOTU_G1_C1_ISO_TX_ACTIVE | MOTU_G1_C1_ISO_RX_ACTIVE);
1321 isoctrl |= (MOTU_G1_IO_ENABLE_0);
1322 WriteRegister(MOTU_REG_ISOCTRL, isoctrl);
1323 debugOutput(DEBUG_LEVEL_VERBOSE, "MOTU g1: isoctrl 1: %08x\n", isoctrl);
1325 /* Streaming should be started with the conclusion of the above
1326 * register writes. There's one final bit that's set afterwards
1327 * by other systems, so we'll do the same until its demonstrated
1328 * that this write can be merged with the previous one without ill
1329 * effects.
1331 isoctrl &= ~MOTU_G1_C1_ISO_INFO_MASK;
1332 isoctrl |= MOTU_G1_C1_ISO_ENABLE;
1333 WriteRegister(MOTU_REG_ISOCTRL, isoctrl);
1335 debugOutput(DEBUG_LEVEL_VERBOSE, "MOTU g1: isoctrl 2: %08x\n", isoctrl);
1337 return true;
1340 // NOTE: this assumes that you have two streams
1341 switch (i) {
1342 case 0:
1343 // TODO: do the stuff that is nescessary to make the device
1344 // receive a stream
1346 // Set the streamprocessor channel to the one obtained by
1347 // the connection management
1348 m_receiveProcessor->setChannel(m_iso_recv_channel);
1350 // Mask out current transmit settings of the MOTU and replace with
1351 // new ones (which correspond to our receive channel). Turn bit 24
1352 // on to enable changes to the MOTU's iso transmit settings when the
1353 // iso control register is written. Bit 23 enables iso transmit
1354 // from the MOTU.
1355 isoctrl &= 0xff00ffff;
1356 isoctrl |= (m_iso_recv_channel << 16);
1357 isoctrl |= 0x00c00000;
1358 WriteRegister(MOTU_REG_ISOCTRL, isoctrl);
1359 break;
1360 case 1:
1361 // TODO: do the stuff that is nescessary to make the device
1362 // transmit a stream
1364 // Set the streamprocessor channel to the one obtained by
1365 // the connection management
1366 m_transmitProcessor->setChannel(m_iso_send_channel);
1368 // Mask out current receive settings of the MOTU and replace with
1369 // new ones (which correspond to our transmit channel). Turn bit 31
1370 // on to enable changes to the MOTU's iso receive settings when the
1371 // iso control register is written. Bit 30 enables iso receive by
1372 // the MOTU.
1373 isoctrl &= 0x00ffffff;
1374 isoctrl |= (m_iso_send_channel << 24);
1375 isoctrl |= 0xc0000000;
1376 WriteRegister(MOTU_REG_ISOCTRL, isoctrl);
1377 break;
1379 default: // Invalid stream index
1380 return false;
1383 return true;
1386 bool
1387 MotuDevice::stopStreamByIndex(int i) {
1389 quadlet_t isoctrl = ReadRegister(MOTU_REG_ISOCTRL);
1391 // Shut down streaming. Essentially the opposite of the start function.
1393 if (m_motu_model == MOTU_MODEL_828MkI) {
1394 quadlet_t reg = isoctrl;
1395 // The 828MkI device does this differently. In particular it does
1396 // not appear possible to disable tx and rx separately since they
1397 // share a global enable (MOTU_G1_C1_ISO_ENABLE). Therefore we
1398 // disable both when the 0th index is requested and ignore any
1399 // request for index 1.
1400 if (i == 1)
1401 return true;
1403 /* First disable the streaming */
1404 reg &= ~MOTU_G1_C1_ISO_INFO_MASK;
1405 reg &= ~MOTU_G1_C1_ISO_ENABLE;
1406 WriteRegister(MOTU_REG_ISOCTRL, reg);
1408 /* Next, turn off individual stream enable flags. As for the stream
1409 * start case, this is separated from the first write because that's
1410 * what other systems do.
1412 reg |= (isoctrl & MOTU_G1_C1_ISO_INFO_MASK);
1413 reg &= ~(MOTU_G1_C1_ISO_TX_ACTIVE | MOTU_G1_C1_ISO_RX_ACTIVE);
1414 WriteRegister(MOTU_REG_ISOCTRL, reg);
1416 return true;
1419 // NOTE: this assumes that you have two streams
1420 switch (i) {
1421 case 0:
1422 // Turn bit 22 off to disable iso send by the MOTU. Turn
1423 // bit 23 on to enable changes to the MOTU's iso transmit
1424 // settings when the iso control register is written.
1425 isoctrl &= 0xffbfffff;
1426 isoctrl |= 0x00800000;
1427 WriteRegister(MOTU_REG_ISOCTRL, isoctrl);
1428 break;
1429 case 1:
1430 // Turn bit 30 off to disable iso receive by the MOTU. Turn
1431 // bit 31 on to enable changes to the MOTU's iso receive
1432 // settings when the iso control register is written.
1433 isoctrl &= 0xbfffffff;
1434 isoctrl |= 0x80000000;
1435 WriteRegister(MOTU_REG_ISOCTRL, isoctrl);
1436 break;
1438 default: // Invalid stream index
1439 return false;
1442 return true;
1445 signed int MotuDevice::getIsoRecvChannel(void) {
1446 return m_iso_recv_channel;
1449 signed int MotuDevice::getIsoSendChannel(void) {
1450 return m_iso_send_channel;
1453 signed int MotuDevice::getDeviceGeneration(void) {
1454 if (m_motu_model == MOTU_MODEL_828MkI)
1455 return MOTU_DEVICE_G1;
1456 if (m_motu_model==MOTU_MODEL_828mk3 ||
1457 m_motu_model==MOTU_MODEL_ULTRALITEmk3 ||
1458 m_motu_model==MOTU_MODEL_ULTRALITEmk3_HYB ||
1459 m_motu_model==MOTU_MODEL_TRAVELERmk3 ||
1460 m_motu_model==MOTU_MODEL_896mk3)
1461 return MOTU_DEVICE_G3;
1462 return MOTU_DEVICE_G2;
1465 unsigned int MotuDevice::getOpticalMode(unsigned int dir,
1466 unsigned int *port_a_mode, unsigned int *port_b_mode) {
1467 // Only the "Mark 3" (aka G3) MOTU devices had more than one optical
1468 // port. Therefore the "port_b_mode" parameter is unused by all
1469 // devices other than the Mark 3 devices.
1471 // If a mode parameter pointer is NULL it will not be returned.
1472 unsigned int reg, reg2;
1473 unsigned int mask, shift;
1475 if (port_b_mode != NULL)
1476 *port_b_mode = MOTU_OPTICAL_MODE_NONE;
1477 if (getDeviceGeneration()!=MOTU_DEVICE_G3 && port_a_mode==NULL)
1478 return 0;
1480 if (m_motu_model == MOTU_MODEL_828MkI) {
1481 // The early devices used a different register layout.
1482 unsigned int mask2;
1483 reg = ReadRegister(MOTU_G1_REG_CONFIG);
1484 reg2 = ReadRegister(MOTU_G1_REG_CONFIG_2);
1486 mask = (dir==MOTU_DIR_IN)?MOTU_G1_C1_OPT_TOSLINK_IN:MOTU_G1_C1_OPT_TOSLINK_OUT;
1487 mask2 = (dir==MOTU_DIR_IN)?MOTU_G1_C2_OPT_nADAT_IN:MOTU_G1_C2_OPT_nADAT_OUT;
1489 if ((reg & mask) && (reg2 & mask2)) {
1490 /* Toslink bit set, nADAT bit set -> Toslink mode */
1491 *port_a_mode = MOTU_OPTICAL_MODE_TOSLINK;
1492 } else
1493 if ((reg & mask)==0 && (reg2 & mask2)==0) {
1494 /* Toslink bit clear, nADAT bit clear -> ADAT mode */
1495 *port_a_mode = MOTU_OPTICAL_MODE_ADAT;
1496 } else {
1497 /* All other combinations are unexpected except toslink clear/
1498 * nADAT set, so just assume the optical port is off if we get
1499 * here.
1501 *port_a_mode = MOTU_OPTICAL_MODE_OFF;
1503 return 0;
1506 if (getDeviceGeneration() == MOTU_DEVICE_G3) {
1507 unsigned int enable, toslink;
1508 /* The Ultralite Mk3s don't have any optical ports. All others have 2. */
1509 if (m_motu_model==MOTU_MODEL_ULTRALITEmk3 || m_motu_model==MOTU_MODEL_ULTRALITEmk3_HYB) {
1510 if (port_a_mode != NULL)
1511 *port_a_mode = MOTU_OPTICAL_MODE_NONE;
1512 if (port_b_mode != NULL)
1513 *port_b_mode = MOTU_OPTICAL_MODE_NONE;
1514 return 0;
1516 reg = ReadRegister(MOTU_G3_REG_OPTICAL_CTRL);
1517 debugOutput(DEBUG_LEVEL_VERBOSE, "mark3 optical control register = 0x%08x\n", reg);
1518 if (port_a_mode != NULL) {
1519 enable = (dir==MOTU_DIR_IN)?MOTU_G3_OPT_A_IN_ENABLE:MOTU_G3_OPT_A_OUT_ENABLE;
1520 toslink = (dir==MOTU_DIR_IN)?MOTU_G3_OPT_A_IN_TOSLINK:MOTU_G3_OPT_A_OUT_TOSLINK;
1521 if ((reg & enable) == 0)
1522 *port_a_mode = MOTU_OPTICAL_MODE_OFF;
1523 else
1524 if ((reg & toslink) != 0)
1525 *port_a_mode = MOTU_OPTICAL_MODE_TOSLINK;
1526 else
1527 *port_a_mode = MOTU_OPTICAL_MODE_ADAT;
1529 if (port_b_mode != NULL) {
1530 enable = (dir==MOTU_DIR_IN)?MOTU_G3_OPT_B_IN_ENABLE:MOTU_G3_OPT_B_OUT_ENABLE;
1531 toslink = (dir==MOTU_DIR_IN)?MOTU_G3_OPT_B_IN_TOSLINK:MOTU_G3_OPT_B_OUT_TOSLINK;
1532 if ((reg & enable) == 0)
1533 *port_b_mode = MOTU_OPTICAL_MODE_OFF;
1534 else
1535 if ((reg & toslink) != 0)
1536 *port_b_mode = MOTU_OPTICAL_MODE_TOSLINK;
1537 else
1538 *port_b_mode = MOTU_OPTICAL_MODE_ADAT;
1540 return 0;
1543 reg = ReadRegister(MOTU_REG_ROUTE_PORT_CONF);
1544 mask = (dir==MOTU_DIR_IN)?MOTU_G2_OPTICAL_IN_MODE_MASK:MOTU_G2_OPTICAL_OUT_MODE_MASK;
1545 shift = (dir==MOTU_DIR_IN)?MOTU_G2_OPTICAL_IN_MODE_BIT0:MOTU_G2_OPTICAL_OUT_MODE_BIT0;
1546 switch ((reg & mask) >> shift) {
1547 case MOTU_G2_OPTICAL_MODE_OFF: *port_a_mode = MOTU_OPTICAL_MODE_OFF; break;
1548 case MOTU_G2_OPTICAL_MODE_ADAT: *port_a_mode = MOTU_OPTICAL_MODE_ADAT; break;
1549 case MOTU_G2_OPTICAL_MODE_TOSLINK: *port_a_mode = MOTU_OPTICAL_MODE_TOSLINK; break;
1551 return 0;
1554 signed int MotuDevice::setOpticalMode(unsigned int dir,
1555 unsigned int port_a_mode, unsigned int port_b_mode) {
1556 // Only the "Mark 3" (aka G3) MOTU devices had more than one optical port.
1557 // Therefore the "port B" mode is ignored for all devices other than
1558 // the Mark 3 devices.
1559 unsigned int reg, g2mode;
1560 unsigned int opt_ctrl = 0x0000002;
1562 /* THe 896HD doesn't have an SPDIF/TOSLINK optical mode, so don't try to
1563 * set it
1565 if (m_motu_model==MOTU_MODEL_896HD && port_a_mode==MOTU_OPTICAL_MODE_TOSLINK)
1566 return -1;
1568 if (getDeviceGeneration()!=MOTU_DEVICE_G3 && port_a_mode==MOTU_OPTICAL_MODE_KEEP)
1569 return 0;
1571 if (m_motu_model == MOTU_MODEL_828MkI) {
1572 // The earlier MOTUs handle this differently.
1573 unsigned int g1_conf1_ref, g1_conf2_ref;
1574 unsigned int g1_conf1, g1_conf2;
1575 unsigned int toslink, n_adat;
1576 signed int err = 0;
1577 g1_conf1 = ReadRegister(MOTU_G1_REG_CONFIG);
1578 g1_conf2 = ReadRegister(MOTU_G1_REG_CONFIG_2);
1579 toslink = (dir==MOTU_DIR_IN)?MOTU_G1_C1_OPT_TOSLINK_IN:MOTU_G1_C1_OPT_TOSLINK_OUT;
1580 n_adat = (dir==MOTU_DIR_IN)?MOTU_G1_C2_OPT_nADAT_IN:MOTU_G1_C2_OPT_nADAT_OUT;
1582 // Don't send ISO information
1583 g1_conf1 &= ~MOTU_G1_C1_ISO_INFO_MASK;
1585 // This bit seems to always be set
1586 g1_conf1 |= (MOTU_G1_IO_ENABLE_0);
1588 /* This bit seems to always be set when this register is set.
1589 * It may be a write enable bit.
1591 g1_conf2 |= MOTU_G1_C2_OPT_nADAT_WREN;
1593 g1_conf1_ref = g1_conf1;
1594 g1_conf2_ref = g1_conf2;
1596 /* Set registers as needed by the requested mode */
1598 if (port_a_mode == MOTU_OPTICAL_MODE_TOSLINK) {
1599 g1_conf1 |= toslink;
1600 } else {
1601 g1_conf1 &= ~toslink;
1603 if (port_a_mode == MOTU_OPTICAL_MODE_ADAT) {
1604 g1_conf2 &= ~n_adat;
1605 } else {
1606 g1_conf2 |= n_adat;
1609 // Under other systems, MOTU_G1_REG_CONFIG is always written
1610 // first, but only if its value has been changed. Similarly,
1611 // MOTU_G1_REG_CONFIG_2 is only written if its value has been
1612 // altered.
1613 if (!err && g1_conf1!=g1_conf1_ref)
1614 err = WriteRegister(MOTU_G1_REG_CONFIG, g1_conf1) != 0;
1615 if (!err && g1_conf2!=g1_conf2_ref)
1616 err = WriteRegister(MOTU_G1_REG_CONFIG_2, g1_conf2) != 0;
1617 if (err)
1618 return -1;
1619 return 0;
1622 /* The G3 devices are also quite a bit different to the G2 units */
1623 if (getDeviceGeneration() == MOTU_DEVICE_G3) {
1624 unsigned int mask, enable, toslink;
1625 reg = ReadRegister(MOTU_G3_REG_OPTICAL_CTRL);
1626 if (port_a_mode != MOTU_OPTICAL_MODE_KEEP) {
1627 mask = enable = toslink = 0;
1628 if (dir & MOTU_DIR_IN) {
1629 mask |= MOTU_G3_OPT_A_IN_MASK;
1630 enable |= MOTU_G3_OPT_A_IN_ENABLE;
1631 toslink |= MOTU_G3_OPT_A_IN_TOSLINK;
1633 if (dir & MOTU_DIR_OUT) {
1634 mask |= MOTU_G3_OPT_A_OUT_MASK;
1635 enable |= MOTU_G3_OPT_A_OUT_ENABLE;
1636 toslink |= MOTU_G3_OPT_A_OUT_TOSLINK;
1638 reg = (reg & ~mask) | enable;
1639 switch (port_a_mode) {
1640 case MOTU_OPTICAL_MODE_OFF: reg &= ~enable; break;
1641 case MOTU_OPTICAL_MODE_TOSLINK: reg |= toslink; break;
1644 if (port_b_mode != MOTU_OPTICAL_MODE_KEEP) {
1645 mask = enable = toslink = 0;
1646 if (dir & MOTU_DIR_IN) {
1647 mask |= MOTU_G3_OPT_B_IN_MASK;
1648 enable |= MOTU_G3_OPT_B_IN_ENABLE;
1649 toslink |= MOTU_G3_OPT_B_IN_TOSLINK;
1651 if (dir & MOTU_DIR_OUT) {
1652 mask |= MOTU_G3_OPT_B_OUT_MASK;
1653 enable |= MOTU_G3_OPT_B_OUT_ENABLE;
1654 toslink |= MOTU_G3_OPT_B_OUT_TOSLINK;
1656 reg = (reg & ~mask) | enable;
1657 switch (port_a_mode) {
1658 case MOTU_OPTICAL_MODE_OFF: reg &= ~enable; break;
1659 case MOTU_OPTICAL_MODE_TOSLINK: reg |= toslink; break;
1661 reg = (reg & ~mask) | enable;
1662 switch (port_b_mode) {
1663 case MOTU_OPTICAL_MODE_OFF: reg &= ~enable; break;
1664 case MOTU_OPTICAL_MODE_TOSLINK: reg |= toslink; break;
1667 return WriteRegister(MOTU_G3_REG_OPTICAL_CTRL, reg);
1670 reg = ReadRegister(MOTU_REG_ROUTE_PORT_CONF);
1672 // Map from user mode to values sent to the device registers.
1673 g2mode = 0;
1674 switch (port_a_mode) {
1675 case MOTU_OPTICAL_MODE_OFF: g2mode = MOTU_G2_OPTICAL_MODE_OFF; break;
1676 case MOTU_OPTICAL_MODE_ADAT: g2mode = MOTU_G2_OPTICAL_MODE_ADAT; break;
1677 case MOTU_OPTICAL_MODE_TOSLINK: g2mode = MOTU_G2_OPTICAL_MODE_TOSLINK; break;
1680 // Set up the optical control register value according to the current
1681 // optical port modes. At this stage it's not completely understood
1682 // what the "Optical control" register does, so the values it's set to
1683 // are more or less "magic" numbers.
1684 if ((reg & MOTU_G2_OPTICAL_IN_MODE_MASK) != (MOTU_G2_OPTICAL_MODE_ADAT<<MOTU_G2_OPTICAL_IN_MODE_BIT0))
1685 opt_ctrl |= 0x00000080;
1686 if ((reg & MOTU_G2_OPTICAL_OUT_MODE_MASK) != (MOTU_G2_OPTICAL_MODE_ADAT<<MOTU_G2_OPTICAL_OUT_MODE_BIT0))
1687 opt_ctrl |= 0x00000040;
1689 if (dir & MOTU_DIR_IN) {
1690 reg &= ~MOTU_G2_OPTICAL_IN_MODE_MASK;
1691 reg |= (g2mode << MOTU_G2_OPTICAL_IN_MODE_BIT0) & MOTU_G2_OPTICAL_IN_MODE_MASK;
1692 if (g2mode != MOTU_G2_OPTICAL_MODE_ADAT)
1693 opt_ctrl |= 0x00000080;
1694 else
1695 opt_ctrl &= ~0x00000080;
1697 if (dir & MOTU_DIR_OUT) {
1698 reg &= ~MOTU_G2_OPTICAL_OUT_MODE_MASK;
1699 reg |= (g2mode << MOTU_G2_OPTICAL_OUT_MODE_BIT0) & MOTU_G2_OPTICAL_OUT_MODE_MASK;
1700 if (g2mode != MOTU_G2_OPTICAL_MODE_ADAT)
1701 opt_ctrl |= 0x00000040;
1702 else
1703 opt_ctrl &= ~0x00000040;
1706 /* Setting bit 25 in the route/port configuration register enables the
1707 * setting of the optical mode. Bit 24 allows the phones assign to be
1708 * set using the lower 8 bits of the register. This function has no
1709 * business setting that, so make sure bit 24 is masked off.
1711 reg |= 0x02000000;
1712 reg &= ~0x01000000;
1714 // FIXME: there seems to be more to it than this, but for
1715 // the moment at least this seems to work.
1716 WriteRegister(MOTU_REG_ROUTE_PORT_CONF, reg);
1717 return WriteRegister(MOTU_REG_OPTICAL_CTRL, opt_ctrl);
1720 signed int MotuDevice::getEventSize(unsigned int direction) {
1722 // Return the size in bytes of a single event sent to (dir==MOTU_OUT) or
1723 // from (dir==MOTU_IN) the MOTU as part of an iso data packet.
1725 // Note that all audio channels are sent using 3 bytes.
1726 signed int size = 0;
1728 if (direction==MOTU_DIR_IN)
1729 size = m_rx_event_size;
1730 else
1731 size = m_tx_event_size;
1733 // Round size up to the next quadlet boundary
1734 return ((size+3)/4)*4;
1736 /* ======================================================================= */
1738 bool MotuDevice::addPort(Streaming::StreamProcessor *s_processor,
1739 char *name, enum Streaming::Port::E_Direction direction,
1740 int position, int size) {
1742 * Internal helper function to add a MOTU port to a given stream processor.
1743 * This just saves the unnecessary replication of what is essentially
1744 * boilerplate code. Note that the port name is freed by this function
1745 * prior to exit.
1747 Streaming::Port *p=NULL;
1749 p = new Streaming::MotuAudioPort(*s_processor, name, direction, position, size);
1751 if (!p) {
1752 debugOutput(DEBUG_LEVEL_VERBOSE, "Skipped port %s\n",name);
1754 free(name);
1755 return true;
1757 /* ======================================================================= */
1759 bool MotuDevice::initDirPortGroups(
1760 enum Streaming::Port::E_Direction direction, unsigned int sample_rate,
1761 unsigned int optical_a_mode, unsigned int optical_b_mode) {
1763 * Internal helper method. Using a PortGroupEntry array the locations of
1764 * channels within a packet is deduced based on the given sample rate and
1765 * optical port modes, and stored back into the PortGroupEntry. Locations
1766 * within the packet start at 10 and are incremented by 3 for each
1767 * subsequent channel. Channels are assumed to be ordered in the packet as
1768 * they are in the port group array. Port groups which are not to be
1769 * created have their packet offset set to -1.
1771 * Notes:
1772 * - When the ports are created by addDirPortGroups() the port_order field
1773 * is used to order the additions. This way the first ports created do
1774 * not necessarily have to be those which appear in the packets first.
1776 * - Currently ports are not flagged for creation if they are disabled due
1777 * to sample rate or optical mode. However, it might be better to
1778 * unconditionally create all ports and just disable those which are not
1779 * active.
1781 signed int i;
1782 unsigned int dir = direction==Streaming::Port::E_Capture?MOTU_PA_IN:MOTU_PA_OUT;
1783 const signed int mode_idx = direction==Streaming::Port::E_Capture?1:0;
1784 unsigned int flags = 0;
1785 unsigned int portgroup_flags;
1786 signed int pkt_ofs;
1787 const DevicePropertyEntry *devprop = &DevicesProperty[m_motu_model-1];
1788 signed int n_groups = devprop->n_portgroup_entries;
1790 if (n_groups <= 0)
1791 return true;
1793 /* Port data starts at offset 10 on most models, and 4 on the 828mk1 */
1794 if (m_motu_model == MOTU_MODEL_828MkI)
1795 pkt_ofs = 4;
1796 else
1797 pkt_ofs = 10;
1799 if ( sample_rate > 96000 )
1800 flags |= MOTU_PA_RATE_4x;
1801 else if ( sample_rate > 48000 )
1802 flags |= MOTU_PA_RATE_2x;
1803 else
1804 flags |= MOTU_PA_RATE_1x;
1806 switch (optical_a_mode) {
1807 case MOTU_OPTICAL_MODE_NONE: flags |= MOTU_PA_OPTICAL_ANY; break;
1808 case MOTU_OPTICAL_MODE_OFF: flags |= MOTU_PA_OPTICAL_OFF; break;
1809 case MOTU_OPTICAL_MODE_ADAT: flags |= MOTU_PA_OPTICAL_ADAT; break;
1810 case MOTU_OPTICAL_MODE_TOSLINK: flags |= MOTU_PA_OPTICAL_TOSLINK; break;
1812 switch (optical_b_mode) {
1813 case MOTU_OPTICAL_MODE_NONE: flags |= MOTU_PA_MK3_OPT_B_ANY; break;
1814 case MOTU_OPTICAL_MODE_OFF: flags |= MOTU_PA_MK3_OPT_B_OFF; break;
1815 case MOTU_OPTICAL_MODE_ADAT: flags |= MOTU_PA_MK3_OPT_B_ADAT; break;
1816 case MOTU_OPTICAL_MODE_TOSLINK: flags |= MOTU_PA_MK3_OPT_B_TOSLINK; break;
1819 debugOutput(DEBUG_LEVEL_VERBOSE, "flags=0x%08x, opta=0x%x, optb=0x%x\n",
1820 flags, optical_a_mode, optical_b_mode);
1822 /* Scan through the port groups, allocating packet offsets for all
1823 * port groups which are found to be active in the device's current state.
1825 for (i=0; i<n_groups; i++) {
1826 portgroup_flags = devprop->portgroup_entry[i].flags;
1827 /* For devices without one or more optical ports, ensure the tests
1828 * on the optical ports always returns "true".
1830 if (optical_a_mode == MOTU_OPTICAL_MODE_NONE)
1831 portgroup_flags |= MOTU_PA_OPTICAL_ANY;
1832 if (optical_b_mode == MOTU_OPTICAL_MODE_NONE)
1833 portgroup_flags |= MOTU_PA_MK3_OPT_B_ANY;
1835 devprop->portgroup_entry[i].group_pkt_offset[mode_idx] = -1;
1836 if (( portgroup_flags & dir ) &&
1837 ( portgroup_flags & MOTU_PA_RATE_MASK & flags ) &&
1838 ( portgroup_flags & MOTU_PA_OPTICAL_MASK & flags ) &&
1839 ( portgroup_flags & MOTU_PA_MK3_OPT_B_MASK & flags )) {
1840 if ((portgroup_flags & MOTU_PA_PADDING) == 0) {
1841 devprop->portgroup_entry[i].group_pkt_offset[mode_idx] = pkt_ofs;
1843 pkt_ofs += 3*devprop->portgroup_entry[i].n_channels;
1847 /* The 828mk1 has an additional 6 bytes tacked onto the end of the
1848 * packet sent by it, which we must account for when using pkt_ofs as a
1849 * proxy for size.
1851 if (direction==Streaming::Port::E_Capture && m_motu_model==MOTU_MODEL_828MkI)
1852 pkt_ofs += 6;
1854 if (direction == Streaming::Port::E_Capture)
1855 m_rx_event_size = pkt_ofs;
1856 else
1857 m_tx_event_size = pkt_ofs;
1859 debugOutput(DEBUG_LEVEL_VERBOSE, "rxsize=%d, txsize=%d\n",
1860 m_rx_event_size, m_tx_event_size);
1862 return true;
1864 /* ======================================================================= */
1866 bool MotuDevice::addDirPortGroups(
1867 enum Streaming::Port::E_Direction direction, unsigned int sample_rate,
1868 unsigned int optical_a_mode, unsigned int optical_b_mode) {
1870 * Internal helper method. Using a PortGroupEntry array previously
1871 * initialised with a call to initPortGroups(), ports are created for each
1872 * active channel of the interface. The locations of channels within a
1873 * packet are obtained from the group_pkt_offset field which was set by
1874 * initPortGroups() and the order they should be created in is specifed by
1875 * the port_order field of a port group.
1877 * The port_order functionality is helpful if it's more convenient to have a
1878 * particular port show up first in jackd even through it's located in the
1879 * middle of the packet. If the port_order field of the first port group is
1880 * -1 it is assumed that the port creation is to happen in the order
1881 * specified in the port group list.
1883 * A port group is taken to be inactive if its group_pkt_offset is set to -1.
1885 const char *mode_str = direction==Streaming::Port::E_Capture?"cap":"pbk";
1886 const signed int mode_idx = direction==Streaming::Port::E_Capture?1:0;
1887 Streaming::StreamProcessor *s_processor;
1888 signed int i;
1889 char *buff;
1890 const DevicePropertyEntry *devprop = &DevicesProperty[m_motu_model-1];
1891 signed int n_groups = devprop->n_portgroup_entries;
1892 signed int creation_indices[n_groups];
1893 signed int create_in_order;
1895 if (n_groups <= 0)
1896 return true;
1898 // retrieve the ID
1899 std::string id=std::string("dev?");
1900 if(!getOption("id", id)) {
1901 debugWarning("Could not retrieve id parameter, defaulting to 'dev?'\n");
1904 if (direction == Streaming::Port::E_Capture) {
1905 s_processor = m_receiveProcessor;
1906 } else {
1907 s_processor = m_transmitProcessor;
1910 for (i=0; i<n_groups; i++) {
1911 creation_indices[i] = -1;
1913 create_in_order = devprop->portgroup_entry[0].port_order<0;
1915 /* First scan through the port groups to determine the creation order */
1916 for (i=0; i<n_groups; i++) {
1917 if (devprop->portgroup_entry[i].group_pkt_offset[mode_idx] >= 0) {
1918 if (create_in_order)
1919 creation_indices[i] = i;
1920 else
1921 creation_indices[devprop->portgroup_entry[i].port_order] = i;
1925 /* Now run through the portgroup list again, this time in creation order,
1926 * to make the ports.
1928 for (i=0; i<n_groups; i++) {
1929 char namestr[64];
1930 signed int ch;
1931 signed int entry;
1932 if (creation_indices[i] < 0)
1933 continue;
1934 entry = creation_indices[i];
1935 for (ch=0; ch<devprop->portgroup_entry[entry].n_channels; ch++) {
1936 /* Deduce the full channel name */
1937 if (strstr(devprop->portgroup_entry[entry].group_name_format, "%d") != NULL)
1938 snprintf(namestr, sizeof(namestr), devprop->portgroup_entry[entry].group_name_format,
1939 ch+1+devprop->portgroup_entry[entry].port_num_offset);
1940 else
1941 if (strstr(devprop->portgroup_entry[entry].group_name_format, "%s") != NULL)
1942 snprintf(namestr, sizeof(namestr), devprop->portgroup_entry[entry].group_name_format,
1943 (ch & 0x1)?"R":"L");
1944 else
1945 snprintf(namestr, sizeof(namestr), "%s", devprop->portgroup_entry[entry].group_name_format);
1946 asprintf(&buff,"%s_%s_%s" , id.c_str(), mode_str, namestr);
1947 if (!addPort(s_processor, buff, direction,
1948 devprop->portgroup_entry[entry].group_pkt_offset[mode_idx]+3*ch, 0))
1949 return false;
1953 return true;
1955 /* ======================================================================== */
1957 unsigned int MotuDevice::ReadRegister(fb_nodeaddr_t reg) {
1959 * Attempts to read the requested register from the MOTU.
1962 quadlet_t quadlet = 0;
1964 /* If the supplied register has no upper bits set assume it's a G1/G2
1965 * register which is assumed to be relative to MOTU_REG_BASE_ADDR.
1967 if ((reg & MOTU_REG_BASE_ADDR) == 0)
1968 reg |= MOTU_REG_BASE_ADDR;
1970 // Note: 1394Service::read() expects a physical ID, not the node id
1971 if (get1394Service().read(0xffc0 | getNodeId(), reg, 1, &quadlet) <= 0) {
1972 debugError("Error doing motu read from register 0x%012llx\n",reg);
1975 return CondSwapFromBus32(quadlet);
1978 signed int
1979 MotuDevice::readBlock(fb_nodeaddr_t reg, quadlet_t *buf, signed int n_quads) {
1981 // Read "n_quads" quadlets from the device starting at register "reg" into
1982 // the buffer pointed to by "buf". "buf" is assumed to have been
1983 // preallocated and be large enough for the requested data.
1985 signed int i;
1987 if (get1394Service().read(0xffc0 | getNodeId(), reg, n_quads, buf) <= 0) {
1988 debugError("Error doing motu block read of %d quadlets from register 0x%llx\n", n_quads, reg);
1989 return -1;
1991 for (i=0; i<n_quads; i++) {
1992 buf[i] = CondSwapFromBus32(buf[i]);
1994 return 0;
1997 signed int MotuDevice::WriteRegister(fb_nodeaddr_t reg, quadlet_t data) {
1999 * Attempts to write the given data to the requested MOTU register.
2002 unsigned int err = 0;
2003 data = CondSwapToBus32(data);
2005 /* If the supplied register has no upper bits set assume it's a G1/G2
2006 * register which is assumed to be relative to MOTU_REG_BASE_ADDR.
2008 if ((reg & MOTU_REG_BASE_ADDR) == 0)
2009 reg |= MOTU_REG_BASE_ADDR;
2011 // Note: 1394Service::write() expects a physical ID, not the node id
2012 if (get1394Service().write(0xffc0 | getNodeId(), reg, 1, &data) <= 0) {
2013 err = 1;
2014 debugError("Error doing motu write to register 0x%012llx\n",reg);
2017 SleepRelativeUsec(100);
2018 return (err==0)?0:-1;
2021 signed int
2022 MotuDevice::writeBlock(fb_nodeaddr_t reg, quadlet_t *data, signed int n_quads) {
2024 // Write n_quads quadlets from "data" to the device register "reg". Note that
2025 // any necessary byte swapping is done in place, so the contents of "data"
2026 // may be altered by this function.
2028 signed int ret = 0;
2029 signed int i;
2030 for (i=0; i<n_quads; i++) {
2031 data[i] = CondSwapToBus32(data[i]);
2033 if (get1394Service().write(0xffc0 | getNodeId(), reg, n_quads, data) <= 0) {
2034 ret = -1;
2035 debugError("Error doing motu block write of %d quadlets to register 0x%llx\n", n_quads, reg);
2037 return ret;