xref: /openbmc/linux/sound/firewire/oxfw/oxfw.c (revision 07a35edc59d1f461a02c83235d0fe63b4c313920)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * oxfw.c - a part of driver for OXFW970/971 based devices
4  *
5  * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
6  */
7 
8 #include "oxfw.h"
9 
10 #define OXFORD_FIRMWARE_ID_ADDRESS	(CSR_REGISTER_BASE + 0x50000)
11 /* 0x970?vvvv or 0x971?vvvv, where vvvv = firmware version */
12 
13 #define OXFORD_HARDWARE_ID_ADDRESS	(CSR_REGISTER_BASE + 0x90020)
14 #define OXFORD_HARDWARE_ID_OXFW970	0x39443841
15 #define OXFORD_HARDWARE_ID_OXFW971	0x39373100
16 
17 #define VENDOR_LOUD		0x000ff2
18 #define VENDOR_GRIFFIN		0x001292
19 #define VENDOR_BEHRINGER	0x001564
20 #define VENDOR_LACIE		0x00d04b
21 #define VENDOR_TASCAM		0x00022e
22 #define OUI_STANTON		0x001260
23 #define OUI_APOGEE		0x0003db
24 
25 #define MODEL_SATELLITE		0x00200f
26 #define MODEL_SCS1M		0x001000
27 #define MODEL_DUET_FW		0x01dddd
28 
29 #define SPECIFIER_1394TA	0x00a02d
30 #define VERSION_AVC		0x010001
31 
32 MODULE_DESCRIPTION("Oxford Semiconductor FW970/971 driver");
33 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
34 MODULE_LICENSE("GPL v2");
35 MODULE_ALIAS("snd-firewire-speakers");
36 MODULE_ALIAS("snd-scs1x");
37 
38 struct compat_info {
39 	const char *driver_name;
40 	const char *vendor_name;
41 	const char *model_name;
42 };
43 
44 static bool detect_loud_models(struct fw_unit *unit)
45 {
46 	const char *const models[] = {
47 		"Onyxi",
48 		"Onyx-i",
49 		"Onyx 1640i",
50 		"d.Pro",
51 		"U.420"};
52 	char model[32];
53 	int err;
54 
55 	err = fw_csr_string(unit->directory, CSR_MODEL,
56 			    model, sizeof(model));
57 	if (err < 0)
58 		return false;
59 
60 	return match_string(models, ARRAY_SIZE(models), model) >= 0;
61 }
62 
63 static int name_card(struct snd_oxfw *oxfw)
64 {
65 	struct fw_device *fw_dev = fw_parent_device(oxfw->unit);
66 	const struct compat_info *info;
67 	char vendor[24];
68 	char model[32];
69 	const char *d, *v, *m;
70 	u32 firmware;
71 	int err;
72 
73 	/* get vendor name from root directory */
74 	err = fw_csr_string(fw_dev->config_rom + 5, CSR_VENDOR,
75 			    vendor, sizeof(vendor));
76 	if (err < 0)
77 		goto end;
78 
79 	/* get model name from unit directory */
80 	err = fw_csr_string(oxfw->unit->directory, CSR_MODEL,
81 			    model, sizeof(model));
82 	if (err < 0)
83 		goto end;
84 
85 	err = snd_fw_transaction(oxfw->unit, TCODE_READ_QUADLET_REQUEST,
86 				 OXFORD_FIRMWARE_ID_ADDRESS, &firmware, 4, 0);
87 	if (err < 0)
88 		goto end;
89 	be32_to_cpus(&firmware);
90 
91 	if (firmware >> 20 == 0x970)
92 		oxfw->quirks |= SND_OXFW_QUIRK_JUMBO_PAYLOAD;
93 
94 	/* to apply card definitions */
95 	if (oxfw->entry->vendor_id == VENDOR_GRIFFIN ||
96 	    oxfw->entry->vendor_id == VENDOR_LACIE) {
97 		info = (const struct compat_info *)oxfw->entry->driver_data;
98 		d = info->driver_name;
99 		v = info->vendor_name;
100 		m = info->model_name;
101 	} else {
102 		d = "OXFW";
103 		v = vendor;
104 		m = model;
105 	}
106 
107 	strcpy(oxfw->card->driver, d);
108 	strcpy(oxfw->card->mixername, m);
109 	strcpy(oxfw->card->shortname, m);
110 
111 	snprintf(oxfw->card->longname, sizeof(oxfw->card->longname),
112 		 "%s %s (OXFW%x %04x), GUID %08x%08x at %s, S%d",
113 		 v, m, firmware >> 20, firmware & 0xffff,
114 		 fw_dev->config_rom[3], fw_dev->config_rom[4],
115 		 dev_name(&oxfw->unit->device), 100 << fw_dev->max_speed);
116 end:
117 	return err;
118 }
119 
120 static void oxfw_card_free(struct snd_card *card)
121 {
122 	struct snd_oxfw *oxfw = card->private_data;
123 
124 	if (oxfw->has_output || oxfw->has_input)
125 		snd_oxfw_stream_destroy_duplex(oxfw);
126 }
127 
128 static int detect_quirks(struct snd_oxfw *oxfw)
129 {
130 	struct fw_device *fw_dev = fw_parent_device(oxfw->unit);
131 	struct fw_csr_iterator it;
132 	int key, val;
133 	int vendor, model;
134 
135 	/*
136 	 * Add ALSA control elements for two models to keep compatibility to
137 	 * old firewire-speaker module.
138 	 */
139 	if (oxfw->entry->vendor_id == VENDOR_GRIFFIN)
140 		return snd_oxfw_add_spkr(oxfw, false);
141 	if (oxfw->entry->vendor_id == VENDOR_LACIE)
142 		return snd_oxfw_add_spkr(oxfw, true);
143 
144 	/*
145 	 * Stanton models supports asynchronous transactions for unique MIDI
146 	 * messages.
147 	 */
148 	if (oxfw->entry->vendor_id == OUI_STANTON) {
149 		if (oxfw->entry->model_id == MODEL_SCS1M)
150 			oxfw->quirks |= SND_OXFW_QUIRK_BLOCKING_TRANSMISSION;
151 
152 		// No physical MIDI ports.
153 		oxfw->midi_input_ports = 0;
154 		oxfw->midi_output_ports = 0;
155 
156 		return snd_oxfw_scs1x_add(oxfw);
157 	}
158 
159 	if (oxfw->entry->vendor_id == OUI_APOGEE && oxfw->entry->model_id == MODEL_DUET_FW)
160 		oxfw->quirks |= SND_OXFW_QUIRK_BLOCKING_TRANSMISSION;
161 
162 	/*
163 	 * TASCAM FireOne has physical control and requires a pair of additional
164 	 * MIDI ports.
165 	 */
166 	if (oxfw->entry->vendor_id == VENDOR_TASCAM) {
167 		oxfw->midi_input_ports++;
168 		oxfw->midi_output_ports++;
169 		return 0;
170 	}
171 
172 	/* Seek from Root Directory of Config ROM. */
173 	vendor = model = 0;
174 	fw_csr_iterator_init(&it, fw_dev->config_rom + 5);
175 	while (fw_csr_iterator_next(&it, &key, &val)) {
176 		if (key == CSR_VENDOR)
177 			vendor = val;
178 		else if (key == CSR_MODEL)
179 			model = val;
180 	}
181 
182 	/*
183 	 * Mackie Onyx Satellite with base station has a quirk to report a wrong
184 	 * value in 'dbs' field of CIP header against its format information.
185 	 */
186 	if (vendor == VENDOR_LOUD && model == MODEL_SATELLITE)
187 		oxfw->quirks |= SND_OXFW_QUIRK_WRONG_DBS;
188 
189 	return 0;
190 }
191 
192 static void do_registration(struct work_struct *work)
193 {
194 	struct snd_oxfw *oxfw = container_of(work, struct snd_oxfw, dwork.work);
195 	int err;
196 
197 	if (oxfw->registered)
198 		return;
199 
200 	err = snd_card_new(&oxfw->unit->device, -1, NULL, THIS_MODULE, 0,
201 			   &oxfw->card);
202 	if (err < 0)
203 		return;
204 	oxfw->card->private_free = oxfw_card_free;
205 	oxfw->card->private_data = oxfw;
206 
207 	err = name_card(oxfw);
208 	if (err < 0)
209 		goto error;
210 
211 	err = snd_oxfw_stream_discover(oxfw);
212 	if (err < 0)
213 		goto error;
214 
215 	err = detect_quirks(oxfw);
216 	if (err < 0)
217 		goto error;
218 
219 	if (oxfw->has_output || oxfw->has_input) {
220 		err = snd_oxfw_stream_init_duplex(oxfw);
221 		if (err < 0)
222 			goto error;
223 
224 		err = snd_oxfw_create_pcm(oxfw);
225 		if (err < 0)
226 			goto error;
227 
228 		snd_oxfw_proc_init(oxfw);
229 
230 		err = snd_oxfw_create_midi(oxfw);
231 		if (err < 0)
232 			goto error;
233 
234 		err = snd_oxfw_create_hwdep(oxfw);
235 		if (err < 0)
236 			goto error;
237 	}
238 
239 	err = snd_card_register(oxfw->card);
240 	if (err < 0)
241 		goto error;
242 
243 	oxfw->registered = true;
244 
245 	return;
246 error:
247 	snd_card_free(oxfw->card);
248 	dev_info(&oxfw->unit->device,
249 		 "Sound card registration failed: %d\n", err);
250 }
251 
252 static int oxfw_probe(struct fw_unit *unit,
253 		      const struct ieee1394_device_id *entry)
254 {
255 	struct snd_oxfw *oxfw;
256 
257 	if (entry->vendor_id == VENDOR_LOUD && entry->model_id == 0 && !detect_loud_models(unit))
258 		return -ENODEV;
259 
260 	/* Allocate this independent of sound card instance. */
261 	oxfw = devm_kzalloc(&unit->device, sizeof(struct snd_oxfw), GFP_KERNEL);
262 	if (!oxfw)
263 		return -ENOMEM;
264 	oxfw->unit = fw_unit_get(unit);
265 	dev_set_drvdata(&unit->device, oxfw);
266 
267 	oxfw->entry = entry;
268 	mutex_init(&oxfw->mutex);
269 	spin_lock_init(&oxfw->lock);
270 	init_waitqueue_head(&oxfw->hwdep_wait);
271 
272 	/* Allocate and register this sound card later. */
273 	INIT_DEFERRABLE_WORK(&oxfw->dwork, do_registration);
274 	snd_fw_schedule_registration(unit, &oxfw->dwork);
275 
276 	return 0;
277 }
278 
279 static void oxfw_bus_reset(struct fw_unit *unit)
280 {
281 	struct snd_oxfw *oxfw = dev_get_drvdata(&unit->device);
282 
283 	if (!oxfw->registered)
284 		snd_fw_schedule_registration(unit, &oxfw->dwork);
285 
286 	fcp_bus_reset(oxfw->unit);
287 
288 	if (oxfw->registered) {
289 		if (oxfw->has_output || oxfw->has_input) {
290 			mutex_lock(&oxfw->mutex);
291 			snd_oxfw_stream_update_duplex(oxfw);
292 			mutex_unlock(&oxfw->mutex);
293 		}
294 
295 		if (oxfw->entry->vendor_id == OUI_STANTON)
296 			snd_oxfw_scs1x_update(oxfw);
297 	}
298 }
299 
300 static void oxfw_remove(struct fw_unit *unit)
301 {
302 	struct snd_oxfw *oxfw = dev_get_drvdata(&unit->device);
303 
304 	/*
305 	 * Confirm to stop the work for registration before the sound card is
306 	 * going to be released. The work is not scheduled again because bus
307 	 * reset handler is not called anymore.
308 	 */
309 	cancel_delayed_work_sync(&oxfw->dwork);
310 
311 	if (oxfw->registered) {
312 		// Block till all of ALSA character devices are released.
313 		snd_card_free(oxfw->card);
314 	}
315 
316 	mutex_destroy(&oxfw->mutex);
317 	fw_unit_put(oxfw->unit);
318 }
319 
320 static const struct compat_info griffin_firewave = {
321 	.driver_name = "FireWave",
322 	.vendor_name = "Griffin",
323 	.model_name = "FireWave",
324 };
325 
326 static const struct compat_info lacie_speakers = {
327 	.driver_name = "FWSpeakers",
328 	.vendor_name = "LaCie",
329 	.model_name = "FireWire Speakers",
330 };
331 
332 #define OXFW_DEV_ENTRY(vendor, model, data) \
333 { \
334 	.match_flags  = IEEE1394_MATCH_VENDOR_ID | \
335 			IEEE1394_MATCH_MODEL_ID | \
336 			IEEE1394_MATCH_SPECIFIER_ID | \
337 			IEEE1394_MATCH_VERSION, \
338 	.vendor_id    = vendor, \
339 	.model_id     = model, \
340 	.specifier_id = SPECIFIER_1394TA, \
341 	.version      = VERSION_AVC, \
342 	.driver_data  = (kernel_ulong_t)data, \
343 }
344 
345 static const struct ieee1394_device_id oxfw_id_table[] = {
346 	//
347 	// OXFW970 devices:
348 	// Initial firmware has a quirk to postpone isoc packet transmission during finishing async
349 	// transaction. As a result, several isochronous cycles are skipped to transfer the packets
350 	// and the audio data frames which should have been transferred during the cycles are put
351 	// into packet at the first isoc cycle after the postpone. Furthermore, the value of SYT
352 	// field in CIP header is not reliable as synchronization timing,
353 	//
354 	OXFW_DEV_ENTRY(VENDOR_GRIFFIN, 0x00f970, &griffin_firewave),
355 	OXFW_DEV_ENTRY(VENDOR_LACIE, 0x00f970, &lacie_speakers),
356 	// Behringer,F-Control Audio 202. The value of SYT field is not reliable at all.
357 	OXFW_DEV_ENTRY(VENDOR_BEHRINGER, 0x00fc22, NULL),
358 	// Loud Technologies, Tapco Link.FireWire 4x6. The value of SYT field is always 0xffff.
359 	OXFW_DEV_ENTRY(VENDOR_LOUD, 0x000460, NULL),
360 	// Loud Technologies, Mackie Onyx Satellite. Although revised version of firmware is
361 	// installed to avoid the postpone, the value of SYT field is always 0xffff.
362 	OXFW_DEV_ENTRY(VENDOR_LOUD, MODEL_SATELLITE, NULL),
363 	// Miglia HarmonyAudio. Not yet identified.
364 
365 	//
366 	// OXFW971 devices:
367 	// The value of SYT field in CIP header is enough reliable. Both of blocking and non-blocking
368 	// transmission methods are available.
369 	//
370 	// Any Mackie(Loud) models (name string/model id):
371 	//  Onyx-i series (former models):	0x081216
372 	//  Onyx 1640i:				0x001640
373 	//  d.2 pro/d.4 pro (built-in card):	Unknown
374 	//  U.420:				Unknown
375 	//  U.420d:				Unknown
376 	{
377 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
378 				  IEEE1394_MATCH_SPECIFIER_ID |
379 				  IEEE1394_MATCH_VERSION,
380 		.vendor_id	= VENDOR_LOUD,
381 		.model_id	= 0,
382 		.specifier_id	= SPECIFIER_1394TA,
383 		.version	= VERSION_AVC,
384 	},
385 	// TASCAM, FireOne.
386 	OXFW_DEV_ENTRY(VENDOR_TASCAM, 0x800007, NULL),
387 	// Stanton, Stanton Controllers & Systems 1 Mixer (SCS.1m).
388 	OXFW_DEV_ENTRY(OUI_STANTON, MODEL_SCS1M, NULL),
389 	// Stanton, Stanton Controllers & Systems 1 Deck (SCS.1d).
390 	OXFW_DEV_ENTRY(OUI_STANTON, 0x002000, NULL),
391 	// APOGEE, duet FireWire.
392 	OXFW_DEV_ENTRY(OUI_APOGEE, MODEL_DUET_FW, NULL),
393 	{ }
394 };
395 MODULE_DEVICE_TABLE(ieee1394, oxfw_id_table);
396 
397 static struct fw_driver oxfw_driver = {
398 	.driver   = {
399 		.owner	= THIS_MODULE,
400 		.name	= KBUILD_MODNAME,
401 		.bus	= &fw_bus_type,
402 	},
403 	.probe    = oxfw_probe,
404 	.update   = oxfw_bus_reset,
405 	.remove   = oxfw_remove,
406 	.id_table = oxfw_id_table,
407 };
408 
409 static int __init snd_oxfw_init(void)
410 {
411 	return driver_register(&oxfw_driver.driver);
412 }
413 
414 static void __exit snd_oxfw_exit(void)
415 {
416 	driver_unregister(&oxfw_driver.driver);
417 }
418 
419 module_init(snd_oxfw_init);
420 module_exit(snd_oxfw_exit);
421