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