xref: /openbmc/linux/sound/firewire/oxfw/oxfw.c (revision 22d55f02)
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 
27 #define SPECIFIER_1394TA	0x00a02d
28 #define VERSION_AVC		0x010001
29 
30 MODULE_DESCRIPTION("Oxford Semiconductor FW970/971 driver");
31 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
32 MODULE_LICENSE("GPL v2");
33 MODULE_ALIAS("snd-firewire-speakers");
34 MODULE_ALIAS("snd-scs1x");
35 
36 struct compat_info {
37 	const char *driver_name;
38 	const char *vendor_name;
39 	const char *model_name;
40 };
41 
42 static bool detect_loud_models(struct fw_unit *unit)
43 {
44 	const char *const models[] = {
45 		"Onyxi",
46 		"Onyx-i",
47 		"Onyx 1640i",
48 		"d.Pro",
49 		"Mackie Onyx Satellite",
50 		"Tapco LINK.firewire 4x6",
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 	/* to apply card definitions */
92 	if (oxfw->entry->vendor_id == VENDOR_GRIFFIN ||
93 	    oxfw->entry->vendor_id == VENDOR_LACIE) {
94 		info = (const struct compat_info *)oxfw->entry->driver_data;
95 		d = info->driver_name;
96 		v = info->vendor_name;
97 		m = info->model_name;
98 	} else {
99 		d = "OXFW";
100 		v = vendor;
101 		m = model;
102 	}
103 
104 	strcpy(oxfw->card->driver, d);
105 	strcpy(oxfw->card->mixername, m);
106 	strcpy(oxfw->card->shortname, m);
107 
108 	snprintf(oxfw->card->longname, sizeof(oxfw->card->longname),
109 		 "%s %s (OXFW%x %04x), GUID %08x%08x at %s, S%d",
110 		 v, m, firmware >> 20, firmware & 0xffff,
111 		 fw_dev->config_rom[3], fw_dev->config_rom[4],
112 		 dev_name(&oxfw->unit->device), 100 << fw_dev->max_speed);
113 end:
114 	return err;
115 }
116 
117 static void oxfw_card_free(struct snd_card *card)
118 {
119 	struct snd_oxfw *oxfw = card->private_data;
120 
121 	snd_oxfw_stream_destroy_simplex(oxfw, &oxfw->rx_stream);
122 	if (oxfw->has_output)
123 		snd_oxfw_stream_destroy_simplex(oxfw, &oxfw->tx_stream);
124 }
125 
126 static int detect_quirks(struct snd_oxfw *oxfw)
127 {
128 	struct fw_device *fw_dev = fw_parent_device(oxfw->unit);
129 	struct fw_csr_iterator it;
130 	int key, val;
131 	int vendor, model;
132 
133 	/*
134 	 * Add ALSA control elements for two models to keep compatibility to
135 	 * old firewire-speaker module.
136 	 */
137 	if (oxfw->entry->vendor_id == VENDOR_GRIFFIN)
138 		return snd_oxfw_add_spkr(oxfw, false);
139 	if (oxfw->entry->vendor_id == VENDOR_LACIE)
140 		return snd_oxfw_add_spkr(oxfw, true);
141 
142 	/*
143 	 * Stanton models supports asynchronous transactions for unique MIDI
144 	 * messages.
145 	 */
146 	if (oxfw->entry->vendor_id == OUI_STANTON) {
147 		/* No physical MIDI ports. */
148 		oxfw->midi_input_ports = 0;
149 		oxfw->midi_output_ports = 0;
150 
151 		return snd_oxfw_scs1x_add(oxfw);
152 	}
153 
154 	/*
155 	 * TASCAM FireOne has physical control and requires a pair of additional
156 	 * MIDI ports.
157 	 */
158 	if (oxfw->entry->vendor_id == VENDOR_TASCAM) {
159 		oxfw->midi_input_ports++;
160 		oxfw->midi_output_ports++;
161 		return 0;
162 	}
163 
164 	/* Seek from Root Directory of Config ROM. */
165 	vendor = model = 0;
166 	fw_csr_iterator_init(&it, fw_dev->config_rom + 5);
167 	while (fw_csr_iterator_next(&it, &key, &val)) {
168 		if (key == CSR_VENDOR)
169 			vendor = val;
170 		else if (key == CSR_MODEL)
171 			model = val;
172 	}
173 
174 	/*
175 	 * Mackie Onyx Satellite with base station has a quirk to report a wrong
176 	 * value in 'dbs' field of CIP header against its format information.
177 	 */
178 	if (vendor == VENDOR_LOUD && model == MODEL_SATELLITE)
179 		oxfw->wrong_dbs = true;
180 
181 	return 0;
182 }
183 
184 static void do_registration(struct work_struct *work)
185 {
186 	struct snd_oxfw *oxfw = container_of(work, struct snd_oxfw, dwork.work);
187 	int err;
188 
189 	if (oxfw->registered)
190 		return;
191 
192 	err = snd_card_new(&oxfw->unit->device, -1, NULL, THIS_MODULE, 0,
193 			   &oxfw->card);
194 	if (err < 0)
195 		return;
196 	oxfw->card->private_free = oxfw_card_free;
197 	oxfw->card->private_data = oxfw;
198 
199 	err = name_card(oxfw);
200 	if (err < 0)
201 		goto error;
202 
203 	err = snd_oxfw_stream_discover(oxfw);
204 	if (err < 0)
205 		goto error;
206 
207 	err = detect_quirks(oxfw);
208 	if (err < 0)
209 		goto error;
210 
211 	err = snd_oxfw_stream_init_simplex(oxfw, &oxfw->rx_stream);
212 	if (err < 0)
213 		goto error;
214 	if (oxfw->has_output) {
215 		err = snd_oxfw_stream_init_simplex(oxfw, &oxfw->tx_stream);
216 		if (err < 0)
217 			goto error;
218 	}
219 
220 	err = snd_oxfw_create_pcm(oxfw);
221 	if (err < 0)
222 		goto error;
223 
224 	snd_oxfw_proc_init(oxfw);
225 
226 	err = snd_oxfw_create_midi(oxfw);
227 	if (err < 0)
228 		goto error;
229 
230 	err = snd_oxfw_create_hwdep(oxfw);
231 	if (err < 0)
232 		goto error;
233 
234 	err = snd_card_register(oxfw->card);
235 	if (err < 0)
236 		goto error;
237 
238 	oxfw->registered = true;
239 
240 	return;
241 error:
242 	snd_card_free(oxfw->card);
243 	dev_info(&oxfw->unit->device,
244 		 "Sound card registration failed: %d\n", err);
245 }
246 
247 static int oxfw_probe(struct fw_unit *unit,
248 		      const struct ieee1394_device_id *entry)
249 {
250 	struct snd_oxfw *oxfw;
251 
252 	if (entry->vendor_id == VENDOR_LOUD && !detect_loud_models(unit))
253 		return -ENODEV;
254 
255 	/* Allocate this independent of sound card instance. */
256 	oxfw = devm_kzalloc(&unit->device, sizeof(struct snd_oxfw), GFP_KERNEL);
257 	if (!oxfw)
258 		return -ENOMEM;
259 	oxfw->unit = fw_unit_get(unit);
260 	dev_set_drvdata(&unit->device, oxfw);
261 
262 	oxfw->entry = entry;
263 	mutex_init(&oxfw->mutex);
264 	spin_lock_init(&oxfw->lock);
265 	init_waitqueue_head(&oxfw->hwdep_wait);
266 
267 	/* Allocate and register this sound card later. */
268 	INIT_DEFERRABLE_WORK(&oxfw->dwork, do_registration);
269 	snd_fw_schedule_registration(unit, &oxfw->dwork);
270 
271 	return 0;
272 }
273 
274 static void oxfw_bus_reset(struct fw_unit *unit)
275 {
276 	struct snd_oxfw *oxfw = dev_get_drvdata(&unit->device);
277 
278 	if (!oxfw->registered)
279 		snd_fw_schedule_registration(unit, &oxfw->dwork);
280 
281 	fcp_bus_reset(oxfw->unit);
282 
283 	if (oxfw->registered) {
284 		mutex_lock(&oxfw->mutex);
285 
286 		snd_oxfw_stream_update_simplex(oxfw, &oxfw->rx_stream);
287 		if (oxfw->has_output)
288 			snd_oxfw_stream_update_simplex(oxfw, &oxfw->tx_stream);
289 
290 		mutex_unlock(&oxfw->mutex);
291 
292 		if (oxfw->entry->vendor_id == OUI_STANTON)
293 			snd_oxfw_scs1x_update(oxfw);
294 	}
295 }
296 
297 static void oxfw_remove(struct fw_unit *unit)
298 {
299 	struct snd_oxfw *oxfw = dev_get_drvdata(&unit->device);
300 
301 	/*
302 	 * Confirm to stop the work for registration before the sound card is
303 	 * going to be released. The work is not scheduled again because bus
304 	 * reset handler is not called anymore.
305 	 */
306 	cancel_delayed_work_sync(&oxfw->dwork);
307 
308 	if (oxfw->registered) {
309 		// Block till all of ALSA character devices are released.
310 		snd_card_free(oxfw->card);
311 	}
312 
313 	mutex_destroy(&oxfw->mutex);
314 	fw_unit_put(oxfw->unit);
315 }
316 
317 static const struct compat_info griffin_firewave = {
318 	.driver_name = "FireWave",
319 	.vendor_name = "Griffin",
320 	.model_name = "FireWave",
321 };
322 
323 static const struct compat_info lacie_speakers = {
324 	.driver_name = "FWSpeakers",
325 	.vendor_name = "LaCie",
326 	.model_name = "FireWire Speakers",
327 };
328 
329 static const struct ieee1394_device_id oxfw_id_table[] = {
330 	{
331 		.match_flags  = IEEE1394_MATCH_VENDOR_ID |
332 				IEEE1394_MATCH_MODEL_ID |
333 				IEEE1394_MATCH_SPECIFIER_ID |
334 				IEEE1394_MATCH_VERSION,
335 		.vendor_id    = VENDOR_GRIFFIN,
336 		.model_id     = 0x00f970,
337 		.specifier_id = SPECIFIER_1394TA,
338 		.version      = VERSION_AVC,
339 		.driver_data  = (kernel_ulong_t)&griffin_firewave,
340 	},
341 	{
342 		.match_flags  = IEEE1394_MATCH_VENDOR_ID |
343 				IEEE1394_MATCH_MODEL_ID |
344 				IEEE1394_MATCH_SPECIFIER_ID |
345 				IEEE1394_MATCH_VERSION,
346 		.vendor_id    = VENDOR_LACIE,
347 		.model_id     = 0x00f970,
348 		.specifier_id = SPECIFIER_1394TA,
349 		.version      = VERSION_AVC,
350 		.driver_data  = (kernel_ulong_t)&lacie_speakers,
351 	},
352 	/* Behringer,F-Control Audio 202 */
353 	{
354 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
355 				  IEEE1394_MATCH_MODEL_ID,
356 		.vendor_id	= VENDOR_BEHRINGER,
357 		.model_id	= 0x00fc22,
358 	},
359 	/*
360 	 * Any Mackie(Loud) models (name string/model id):
361 	 *  Onyx-i series (former models):	0x081216
362 	 *  Mackie Onyx Satellite:		0x00200f
363 	 *  Tapco LINK.firewire 4x6:		0x000460
364 	 *  d.2 pro:				Unknown
365 	 *  d.4 pro:				Unknown
366 	 *  U.420:				Unknown
367 	 *  U.420d:				Unknown
368 	 */
369 	{
370 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
371 				  IEEE1394_MATCH_SPECIFIER_ID |
372 				  IEEE1394_MATCH_VERSION,
373 		.vendor_id	= VENDOR_LOUD,
374 		.specifier_id	= SPECIFIER_1394TA,
375 		.version	= VERSION_AVC,
376 	},
377 	/* TASCAM, FireOne */
378 	{
379 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
380 				  IEEE1394_MATCH_MODEL_ID,
381 		.vendor_id	= VENDOR_TASCAM,
382 		.model_id	= 0x800007,
383 	},
384 	/* Stanton, Stanton Controllers & Systems 1 Mixer (SCS.1m) */
385 	{
386 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
387 				  IEEE1394_MATCH_MODEL_ID,
388 		.vendor_id	= OUI_STANTON,
389 		.model_id	= 0x001000,
390 	},
391 	/* Stanton, Stanton Controllers & Systems 1 Deck (SCS.1d) */
392 	{
393 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
394 				  IEEE1394_MATCH_MODEL_ID,
395 		.vendor_id	= OUI_STANTON,
396 		.model_id	= 0x002000,
397 	},
398 	// APOGEE, duet FireWire
399 	{
400 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
401 				  IEEE1394_MATCH_MODEL_ID,
402 		.vendor_id	= OUI_APOGEE,
403 		.model_id	= 0x01dddd,
404 	},
405 	{ }
406 };
407 MODULE_DEVICE_TABLE(ieee1394, oxfw_id_table);
408 
409 static struct fw_driver oxfw_driver = {
410 	.driver   = {
411 		.owner	= THIS_MODULE,
412 		.name	= KBUILD_MODNAME,
413 		.bus	= &fw_bus_type,
414 	},
415 	.probe    = oxfw_probe,
416 	.update   = oxfw_bus_reset,
417 	.remove   = oxfw_remove,
418 	.id_table = oxfw_id_table,
419 };
420 
421 static int __init snd_oxfw_init(void)
422 {
423 	return driver_register(&oxfw_driver.driver);
424 }
425 
426 static void __exit snd_oxfw_exit(void)
427 {
428 	driver_unregister(&oxfw_driver.driver);
429 }
430 
431 module_init(snd_oxfw_init);
432 module_exit(snd_oxfw_exit);
433