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