xref: /openbmc/linux/sound/firewire/bebob/bebob.c (revision 03cb0503)
1 /*
2  * bebob.c - a part of driver for BeBoB based devices
3  *
4  * Copyright (c) 2013-2014 Takashi Sakamoto
5  *
6  * Licensed under the terms of the GNU General Public License, version 2.
7  */
8 
9 /*
10  * BeBoB is 'BridgeCo enhanced Breakout Box'. This is installed to firewire
11  * devices with DM1000/DM1100/DM1500 chipset. It gives common way for host
12  * system to handle BeBoB based devices.
13  */
14 
15 #include "bebob.h"
16 
17 MODULE_DESCRIPTION("BridgeCo BeBoB driver");
18 MODULE_AUTHOR("Takashi Sakamoto <o-takashi@sakamocchi.jp>");
19 MODULE_LICENSE("GPL v2");
20 
21 static int index[SNDRV_CARDS]	= SNDRV_DEFAULT_IDX;
22 static char *id[SNDRV_CARDS]	= SNDRV_DEFAULT_STR;
23 static bool enable[SNDRV_CARDS]	= SNDRV_DEFAULT_ENABLE_PNP;
24 
25 module_param_array(index, int, NULL, 0444);
26 MODULE_PARM_DESC(index, "card index");
27 module_param_array(id, charp, NULL, 0444);
28 MODULE_PARM_DESC(id, "ID string");
29 module_param_array(enable, bool, NULL, 0444);
30 MODULE_PARM_DESC(enable, "enable BeBoB sound card");
31 
32 static DEFINE_MUTEX(devices_mutex);
33 static DECLARE_BITMAP(devices_used, SNDRV_CARDS);
34 
35 /* Offsets from information register. */
36 #define INFO_OFFSET_GUID		0x10
37 #define INFO_OFFSET_HW_MODEL_ID		0x18
38 #define INFO_OFFSET_HW_MODEL_REVISION	0x1c
39 
40 #define VEN_EDIROL	0x000040ab
41 #define VEN_PRESONUS	0x00000a92
42 #define VEN_BRIDGECO	0x000007f5
43 #define VEN_MACKIE	0x0000000f
44 #define VEN_STANTON	0x00001260
45 #define VEN_TASCAM	0x0000022e
46 #define VEN_BEHRINGER	0x00001564
47 #define VEN_APOGEE	0x000003db
48 #define VEN_ESI		0x00000f1b
49 #define VEN_ACOUSTIC	0x00000002
50 #define VEN_CME		0x0000000a
51 #define VEN_PHONIC	0x00001496
52 #define VEN_LYNX	0x000019e5
53 #define VEN_ICON	0x00001a9e
54 #define VEN_PRISMSOUND	0x00001198
55 #define VEN_TERRATEC	0x00000aac
56 #define VEN_YAMAHA	0x0000a0de
57 #define VEN_FOCUSRITE	0x0000130e
58 #define VEN_MAUDIO1	0x00000d6c
59 #define VEN_MAUDIO2	0x000007f5
60 
61 #define MODEL_FOCUSRITE_SAFFIRE_BOTH	0x00000000
62 #define MODEL_MAUDIO_AUDIOPHILE_BOTH	0x00010060
63 #define MODEL_MAUDIO_FW1814		0x00010071
64 #define MODEL_MAUDIO_PROJECTMIX		0x00010091
65 
66 static int
67 name_device(struct snd_bebob *bebob, unsigned int vendor_id)
68 {
69 	struct fw_device *fw_dev = fw_parent_device(bebob->unit);
70 	char vendor[24] = {0};
71 	char model[32] = {0};
72 	u32 hw_id;
73 	u32 data[2] = {0};
74 	u32 revision;
75 	int err;
76 
77 	/* get vendor name from root directory */
78 	err = fw_csr_string(fw_dev->config_rom + 5, CSR_VENDOR,
79 			    vendor, sizeof(vendor));
80 	if (err < 0)
81 		goto end;
82 
83 	/* get model name from unit directory */
84 	err = fw_csr_string(bebob->unit->directory, CSR_MODEL,
85 			    model, sizeof(model));
86 	if (err < 0)
87 		goto end;
88 
89 	/* get hardware id */
90 	err = snd_bebob_read_quad(bebob->unit, INFO_OFFSET_HW_MODEL_ID,
91 				  &hw_id);
92 	if (err < 0)
93 		goto end;
94 
95 	/* get hardware revision */
96 	err = snd_bebob_read_quad(bebob->unit, INFO_OFFSET_HW_MODEL_REVISION,
97 				  &revision);
98 	if (err < 0)
99 		goto end;
100 
101 	/* get GUID */
102 	err = snd_bebob_read_block(bebob->unit, INFO_OFFSET_GUID,
103 				   data, sizeof(data));
104 	if (err < 0)
105 		goto end;
106 
107 	strcpy(bebob->card->driver, "BeBoB");
108 	strcpy(bebob->card->shortname, model);
109 	strcpy(bebob->card->mixername, model);
110 	snprintf(bebob->card->longname, sizeof(bebob->card->longname),
111 		 "%s %s (id:%d, rev:%d), GUID %08x%08x at %s, S%d",
112 		 vendor, model, hw_id, revision,
113 		 data[0], data[1], dev_name(&bebob->unit->device),
114 		 100 << fw_dev->max_speed);
115 end:
116 	return err;
117 }
118 
119 /*
120  * This module releases the FireWire unit data after all ALSA character devices
121  * are released by applications. This is for releasing stream data or finishing
122  * transactions safely. Thus at returning from .remove(), this module still keep
123  * references for the unit.
124  */
125 static void
126 bebob_card_free(struct snd_card *card)
127 {
128 	struct snd_bebob *bebob = card->private_data;
129 
130 	snd_bebob_stream_destroy_duplex(bebob);
131 	fw_unit_put(bebob->unit);
132 
133 	kfree(bebob->maudio_special_quirk);
134 
135 	if (bebob->card_index >= 0) {
136 		mutex_lock(&devices_mutex);
137 		clear_bit(bebob->card_index, devices_used);
138 		mutex_unlock(&devices_mutex);
139 	}
140 
141 	mutex_destroy(&bebob->mutex);
142 }
143 
144 static const struct snd_bebob_spec *
145 get_saffire_spec(struct fw_unit *unit)
146 {
147 	char name[24] = {0};
148 
149 	if (fw_csr_string(unit->directory, CSR_MODEL, name, sizeof(name)) < 0)
150 		return NULL;
151 
152 	if (strcmp(name, "SaffireLE") == 0)
153 		return &saffire_le_spec;
154 	else
155 		return &saffire_spec;
156 }
157 
158 static bool
159 check_audiophile_booted(struct fw_unit *unit)
160 {
161 	char name[24] = {0};
162 
163 	if (fw_csr_string(unit->directory, CSR_MODEL, name, sizeof(name)) < 0)
164 		return false;
165 
166 	return strncmp(name, "FW Audiophile Bootloader", 15) != 0;
167 }
168 
169 static int
170 bebob_probe(struct fw_unit *unit,
171 	    const struct ieee1394_device_id *entry)
172 {
173 	struct snd_card *card;
174 	struct snd_bebob *bebob;
175 	const struct snd_bebob_spec *spec;
176 	unsigned int card_index;
177 	int err;
178 
179 	mutex_lock(&devices_mutex);
180 
181 	for (card_index = 0; card_index < SNDRV_CARDS; card_index++) {
182 		if (!test_bit(card_index, devices_used) && enable[card_index])
183 			break;
184 	}
185 	if (card_index >= SNDRV_CARDS) {
186 		err = -ENOENT;
187 		goto end;
188 	}
189 
190 	if ((entry->vendor_id == VEN_FOCUSRITE) &&
191 	    (entry->model_id == MODEL_FOCUSRITE_SAFFIRE_BOTH))
192 		spec = get_saffire_spec(unit);
193 	else if ((entry->vendor_id == VEN_MAUDIO1) &&
194 		 (entry->model_id == MODEL_MAUDIO_AUDIOPHILE_BOTH) &&
195 		 !check_audiophile_booted(unit))
196 		spec = NULL;
197 	else
198 		spec = (const struct snd_bebob_spec *)entry->driver_data;
199 
200 	if (spec == NULL) {
201 		if ((entry->vendor_id == VEN_MAUDIO1) ||
202 		    (entry->vendor_id == VEN_MAUDIO2))
203 			err = snd_bebob_maudio_load_firmware(unit);
204 		else
205 			err = -ENOSYS;
206 		goto end;
207 	}
208 
209 	err = snd_card_new(&unit->device, index[card_index], id[card_index],
210 			   THIS_MODULE, sizeof(struct snd_bebob), &card);
211 	if (err < 0)
212 		goto end;
213 	bebob = card->private_data;
214 	bebob->card_index = card_index;
215 	set_bit(card_index, devices_used);
216 	card->private_free = bebob_card_free;
217 
218 	bebob->card = card;
219 	bebob->unit = fw_unit_get(unit);
220 	bebob->spec = spec;
221 	mutex_init(&bebob->mutex);
222 	spin_lock_init(&bebob->lock);
223 	init_waitqueue_head(&bebob->hwdep_wait);
224 
225 	err = name_device(bebob, entry->vendor_id);
226 	if (err < 0)
227 		goto error;
228 
229 	if ((entry->vendor_id == VEN_MAUDIO1) &&
230 	    (entry->model_id == MODEL_MAUDIO_FW1814))
231 		err = snd_bebob_maudio_special_discover(bebob, true);
232 	else if ((entry->vendor_id == VEN_MAUDIO1) &&
233 		 (entry->model_id == MODEL_MAUDIO_PROJECTMIX))
234 		err = snd_bebob_maudio_special_discover(bebob, false);
235 	else
236 		err = snd_bebob_stream_discover(bebob);
237 	if (err < 0)
238 		goto error;
239 
240 	snd_bebob_proc_init(bebob);
241 
242 	if ((bebob->midi_input_ports > 0) ||
243 	    (bebob->midi_output_ports > 0)) {
244 		err = snd_bebob_create_midi_devices(bebob);
245 		if (err < 0)
246 			goto error;
247 	}
248 
249 	err = snd_bebob_create_pcm_devices(bebob);
250 	if (err < 0)
251 		goto error;
252 
253 	err = snd_bebob_create_hwdep_device(bebob);
254 	if (err < 0)
255 		goto error;
256 
257 	err = snd_bebob_stream_init_duplex(bebob);
258 	if (err < 0)
259 		goto error;
260 
261 	if (!bebob->maudio_special_quirk) {
262 		err = snd_card_register(card);
263 		if (err < 0) {
264 			snd_bebob_stream_destroy_duplex(bebob);
265 			goto error;
266 		}
267 	} else {
268 		/*
269 		 * This is a workaround. This bus reset seems to have an effect
270 		 * to make devices correctly handling transactions. Without
271 		 * this, the devices have gap_count mismatch. This causes much
272 		 * failure of transaction.
273 		 *
274 		 * Just after registration, user-land application receive
275 		 * signals from dbus and starts I/Os. To avoid I/Os till the
276 		 * future bus reset, registration is done in next update().
277 		 */
278 		bebob->deferred_registration = true;
279 		fw_schedule_bus_reset(fw_parent_device(bebob->unit)->card,
280 				      false, true);
281 	}
282 
283 	dev_set_drvdata(&unit->device, bebob);
284 end:
285 	mutex_unlock(&devices_mutex);
286 	return err;
287 error:
288 	mutex_unlock(&devices_mutex);
289 	snd_card_free(card);
290 	return err;
291 }
292 
293 static void
294 bebob_update(struct fw_unit *unit)
295 {
296 	struct snd_bebob *bebob = dev_get_drvdata(&unit->device);
297 
298 	if (bebob == NULL)
299 		return;
300 
301 	fcp_bus_reset(bebob->unit);
302 	snd_bebob_stream_update_duplex(bebob);
303 
304 	if (bebob->deferred_registration) {
305 		if (snd_card_register(bebob->card) < 0) {
306 			snd_bebob_stream_destroy_duplex(bebob);
307 			snd_card_free(bebob->card);
308 		}
309 		bebob->deferred_registration = false;
310 	}
311 }
312 
313 static void bebob_remove(struct fw_unit *unit)
314 {
315 	struct snd_bebob *bebob = dev_get_drvdata(&unit->device);
316 
317 	if (bebob == NULL)
318 		return;
319 
320 	/* Awake bus-reset waiters. */
321 	if (!completion_done(&bebob->bus_reset))
322 		complete_all(&bebob->bus_reset);
323 
324 	/* No need to wait for releasing card object in this context. */
325 	snd_card_free_when_closed(bebob->card);
326 }
327 
328 static struct snd_bebob_rate_spec normal_rate_spec = {
329 	.get	= &snd_bebob_stream_get_rate,
330 	.set	= &snd_bebob_stream_set_rate
331 };
332 static const struct snd_bebob_spec spec_normal = {
333 	.clock	= NULL,
334 	.rate	= &normal_rate_spec,
335 	.meter	= NULL
336 };
337 
338 static const struct ieee1394_device_id bebob_id_table[] = {
339 	/* Edirol, FA-66 */
340 	SND_BEBOB_DEV_ENTRY(VEN_EDIROL, 0x00010049, &spec_normal),
341 	/* Edirol, FA-101 */
342 	SND_BEBOB_DEV_ENTRY(VEN_EDIROL, 0x00010048, &spec_normal),
343 	/* Presonus, FIREBOX */
344 	SND_BEBOB_DEV_ENTRY(VEN_PRESONUS, 0x00010000, &spec_normal),
345 	/* PreSonus, FIREPOD/FP10 */
346 	SND_BEBOB_DEV_ENTRY(VEN_PRESONUS, 0x00010066, &spec_normal),
347 	/* PreSonus, Inspire1394 */
348 	SND_BEBOB_DEV_ENTRY(VEN_PRESONUS, 0x00010001, &spec_normal),
349 	/* BridgeCo, RDAudio1 */
350 	SND_BEBOB_DEV_ENTRY(VEN_BRIDGECO, 0x00010048, &spec_normal),
351 	/* BridgeCo, Audio5 */
352 	SND_BEBOB_DEV_ENTRY(VEN_BRIDGECO, 0x00010049, &spec_normal),
353 	/* Mackie, Onyx 1220/1620/1640 (Firewire I/O Card) */
354 	SND_BEBOB_DEV_ENTRY(VEN_MACKIE, 0x00010065, &spec_normal),
355 	/* Mackie, d.2 (Firewire Option) */
356 	SND_BEBOB_DEV_ENTRY(VEN_MACKIE, 0x00010067, &spec_normal),
357 	/* Stanton, ScratchAmp */
358 	SND_BEBOB_DEV_ENTRY(VEN_STANTON, 0x00000001, &spec_normal),
359 	/* Tascam, IF-FW DM */
360 	SND_BEBOB_DEV_ENTRY(VEN_TASCAM, 0x00010067, &spec_normal),
361 	/* Behringer, XENIX UFX 1204 */
362 	SND_BEBOB_DEV_ENTRY(VEN_BEHRINGER, 0x00001204, &spec_normal),
363 	/* Behringer, XENIX UFX 1604 */
364 	SND_BEBOB_DEV_ENTRY(VEN_BEHRINGER, 0x00001604, &spec_normal),
365 	/* Behringer, Digital Mixer X32 series (X-UF Card) */
366 	SND_BEBOB_DEV_ENTRY(VEN_BEHRINGER, 0x00000006, &spec_normal),
367 	/* Apogee Electronics, Rosetta 200/400 (X-FireWire card) */
368 	/* Apogee Electronics, DA/AD/DD-16X (X-FireWire card) */
369 	SND_BEBOB_DEV_ENTRY(VEN_APOGEE, 0x00010048, &spec_normal),
370 	/* Apogee Electronics, Ensemble */
371 	SND_BEBOB_DEV_ENTRY(VEN_APOGEE, 0x00001eee, &spec_normal),
372 	/* ESI, Quatafire610 */
373 	SND_BEBOB_DEV_ENTRY(VEN_ESI, 0x00010064, &spec_normal),
374 	/* AcousticReality, eARMasterOne */
375 	SND_BEBOB_DEV_ENTRY(VEN_ACOUSTIC, 0x00000002, &spec_normal),
376 	/* CME, MatrixKFW */
377 	SND_BEBOB_DEV_ENTRY(VEN_CME, 0x00030000, &spec_normal),
378 	/* Phonic, Helix Board 12 MkII */
379 	SND_BEBOB_DEV_ENTRY(VEN_PHONIC, 0x00050000, &spec_normal),
380 	/* Phonic, Helix Board 18 MkII */
381 	SND_BEBOB_DEV_ENTRY(VEN_PHONIC, 0x00060000, &spec_normal),
382 	/* Phonic, Helix Board 24 MkII */
383 	SND_BEBOB_DEV_ENTRY(VEN_PHONIC, 0x00070000, &spec_normal),
384 	/* Phonic, Helix Board 12 Universal/18 Universal/24 Universal */
385 	SND_BEBOB_DEV_ENTRY(VEN_PHONIC, 0x00000000, &spec_normal),
386 	/* Lynx, Aurora 8/16 (LT-FW) */
387 	SND_BEBOB_DEV_ENTRY(VEN_LYNX, 0x00000001, &spec_normal),
388 	/* ICON, FireXon */
389 	SND_BEBOB_DEV_ENTRY(VEN_ICON, 0x00000001, &spec_normal),
390 	/* PrismSound, Orpheus */
391 	SND_BEBOB_DEV_ENTRY(VEN_PRISMSOUND, 0x00010048, &spec_normal),
392 	/* PrismSound, ADA-8XR */
393 	SND_BEBOB_DEV_ENTRY(VEN_PRISMSOUND, 0x0000ada8, &spec_normal),
394 	/* TerraTec Electronic GmbH, PHASE 88 Rack FW */
395 	SND_BEBOB_DEV_ENTRY(VEN_TERRATEC, 0x00000003, &phase88_rack_spec),
396 	/* TerraTec Electronic GmbH, PHASE 24 FW */
397 	SND_BEBOB_DEV_ENTRY(VEN_TERRATEC, 0x00000004, &phase24_series_spec),
398 	/* TerraTec Electronic GmbH, Phase X24 FW */
399 	SND_BEBOB_DEV_ENTRY(VEN_TERRATEC, 0x00000007, &phase24_series_spec),
400 	/* TerraTec Electronic GmbH, EWS MIC2/MIC8 */
401 	SND_BEBOB_DEV_ENTRY(VEN_TERRATEC, 0x00000005, &spec_normal),
402 	/* Terratec Electronic GmbH, Aureon 7.1 Firewire */
403 	SND_BEBOB_DEV_ENTRY(VEN_TERRATEC, 0x00000002, &spec_normal),
404 	/* Yamaha, GO44 */
405 	SND_BEBOB_DEV_ENTRY(VEN_YAMAHA, 0x0010000b, &yamaha_go_spec),
406 	/* YAMAHA, GO46 */
407 	SND_BEBOB_DEV_ENTRY(VEN_YAMAHA, 0x0010000c, &yamaha_go_spec),
408 	/* Focusrite, SaffirePro 26 I/O */
409 	SND_BEBOB_DEV_ENTRY(VEN_FOCUSRITE, 0x00000003, &saffirepro_26_spec),
410 	/* Focusrite, SaffirePro 10 I/O */
411 	SND_BEBOB_DEV_ENTRY(VEN_FOCUSRITE, 0x00000006, &saffirepro_10_spec),
412 	/* Focusrite, Saffire(no label and LE) */
413 	SND_BEBOB_DEV_ENTRY(VEN_FOCUSRITE, MODEL_FOCUSRITE_SAFFIRE_BOTH,
414 			    &saffire_spec),
415 	/* M-Audio, Firewire 410 */
416 	SND_BEBOB_DEV_ENTRY(VEN_MAUDIO2, 0x00010058, NULL),	/* bootloader */
417 	SND_BEBOB_DEV_ENTRY(VEN_MAUDIO2, 0x00010046, &maudio_fw410_spec),
418 	/* M-Audio, Firewire Audiophile */
419 	SND_BEBOB_DEV_ENTRY(VEN_MAUDIO1, MODEL_MAUDIO_AUDIOPHILE_BOTH,
420 			    &maudio_audiophile_spec),
421 	/* M-Audio, Firewire Solo */
422 	SND_BEBOB_DEV_ENTRY(VEN_MAUDIO1, 0x00010062, &maudio_solo_spec),
423 	/* M-Audio, Ozonic */
424 	SND_BEBOB_DEV_ENTRY(VEN_MAUDIO1, 0x0000000a, &maudio_ozonic_spec),
425 	/* M-Audio NRV10 */
426 	SND_BEBOB_DEV_ENTRY(VEN_MAUDIO1, 0x00010081, &maudio_nrv10_spec),
427 	/* M-Audio, ProFireLightbridge */
428 	SND_BEBOB_DEV_ENTRY(VEN_MAUDIO1, 0x000100a1, &spec_normal),
429 	/* Firewire 1814 */
430 	SND_BEBOB_DEV_ENTRY(VEN_MAUDIO1, 0x00010070, NULL),	/* bootloader */
431 	SND_BEBOB_DEV_ENTRY(VEN_MAUDIO1, MODEL_MAUDIO_FW1814,
432 			    &maudio_special_spec),
433 	/* M-Audio ProjectMix */
434 	SND_BEBOB_DEV_ENTRY(VEN_MAUDIO1, MODEL_MAUDIO_PROJECTMIX,
435 			    &maudio_special_spec),
436 	/* IDs are unknown but able to be supported */
437 	/*  Apogee, Mini-ME Firewire */
438 	/*  Apogee, Mini-DAC Firewire */
439 	/*  Behringer, F-Control Audio 1616 */
440 	/*  Behringer, F-Control Audio 610 */
441 	/*  Cakawalk, Sonar Power Studio 66 */
442 	/*  CME, UF400e */
443 	/*  ESI, Quotafire XL */
444 	/*  Infrasonic, DewX */
445 	/*  Infrasonic, Windy6 */
446 	/*  Mackie, Digital X Bus x.200 */
447 	/*  Mackie, Digital X Bus x.400 */
448 	/*  Phonic, HB 12 */
449 	/*  Phonic, HB 24 */
450 	/*  Phonic, HB 18 */
451 	/*  Phonic, FireFly 202 */
452 	/*  Phonic, FireFly 302 */
453 	/*  Rolf Spuler, Firewire Guitar */
454 	{}
455 };
456 MODULE_DEVICE_TABLE(ieee1394, bebob_id_table);
457 
458 static struct fw_driver bebob_driver = {
459 	.driver = {
460 		.owner	= THIS_MODULE,
461 		.name	= "snd-bebob",
462 		.bus	= &fw_bus_type,
463 	},
464 	.probe    = bebob_probe,
465 	.update	  = bebob_update,
466 	.remove   = bebob_remove,
467 	.id_table = bebob_id_table,
468 };
469 
470 static int __init
471 snd_bebob_init(void)
472 {
473 	return driver_register(&bebob_driver.driver);
474 }
475 
476 static void __exit
477 snd_bebob_exit(void)
478 {
479 	driver_unregister(&bebob_driver.driver);
480 }
481 
482 module_init(snd_bebob_init);
483 module_exit(snd_bebob_exit);
484