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