xref: /openbmc/linux/sound/firewire/dice/dice.c (revision 56d06fa2)
1 /*
2  * TC Applied Technologies Digital Interface Communications Engine driver
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 "dice.h"
9 
10 MODULE_DESCRIPTION("DICE driver");
11 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
12 MODULE_LICENSE("GPL v2");
13 
14 #define OUI_WEISS		0x001c6a
15 #define OUI_LOUD		0x000ff2
16 #define OUI_FOCUSRITE		0x00130e
17 #define OUI_TCELECTRONIC	0x001486
18 
19 #define DICE_CATEGORY_ID	0x04
20 #define WEISS_CATEGORY_ID	0x00
21 #define LOUD_CATEGORY_ID	0x10
22 
23 #define PROBE_DELAY_MS		(2 * MSEC_PER_SEC)
24 
25 /*
26  * Some models support several isochronous channels, while these streams are not
27  * always available. In this case, add the model name to this list.
28  */
29 static bool force_two_pcm_support(struct fw_unit *unit)
30 {
31 	const char *const models[] = {
32 		/* TC Electronic models. */
33 		"StudioKonnekt48",
34 		/* Focusrite models. */
35 		"SAFFIRE_PRO_40",
36 		"LIQUID_SAFFIRE_56",
37 		"SAFFIRE_PRO_40_1",
38 	};
39 	char model[32];
40 	unsigned int i;
41 	int err;
42 
43 	err = fw_csr_string(unit->directory, CSR_MODEL, model, sizeof(model));
44 	if (err < 0)
45 		return false;
46 
47 	for (i = 0; i < ARRAY_SIZE(models); i++) {
48 		if (strcmp(models[i], model) == 0)
49 			break;
50 	}
51 
52 	return i < ARRAY_SIZE(models);
53 }
54 
55 static int check_dice_category(struct fw_unit *unit)
56 {
57 	struct fw_device *device = fw_parent_device(unit);
58 	struct fw_csr_iterator it;
59 	int key, val, vendor = -1, model = -1;
60 	unsigned int category;
61 
62 	/*
63 	 * Check that GUID and unit directory are constructed according to DICE
64 	 * rules, i.e., that the specifier ID is the GUID's OUI, and that the
65 	 * GUID chip ID consists of the 8-bit category ID, the 10-bit product
66 	 * ID, and a 22-bit serial number.
67 	 */
68 	fw_csr_iterator_init(&it, unit->directory);
69 	while (fw_csr_iterator_next(&it, &key, &val)) {
70 		switch (key) {
71 		case CSR_SPECIFIER_ID:
72 			vendor = val;
73 			break;
74 		case CSR_MODEL:
75 			model = val;
76 			break;
77 		}
78 	}
79 
80 	if (vendor == OUI_FOCUSRITE || vendor == OUI_TCELECTRONIC) {
81 		if (force_two_pcm_support(unit))
82 			return 0;
83 	}
84 
85 	if (vendor == OUI_WEISS)
86 		category = WEISS_CATEGORY_ID;
87 	else if (vendor == OUI_LOUD)
88 		category = LOUD_CATEGORY_ID;
89 	else
90 		category = DICE_CATEGORY_ID;
91 	if (device->config_rom[3] != ((vendor << 8) | category) ||
92 	    device->config_rom[4] >> 22 != model)
93 		return -ENODEV;
94 
95 	return 0;
96 }
97 
98 static int check_clock_caps(struct snd_dice *dice)
99 {
100 	__be32 value;
101 	int err;
102 
103 	/* some very old firmwares don't tell about their clock support */
104 	if (dice->clock_caps > 0) {
105 		err = snd_dice_transaction_read_global(dice,
106 						GLOBAL_CLOCK_CAPABILITIES,
107 						&value, 4);
108 		if (err < 0)
109 			return err;
110 		dice->clock_caps = be32_to_cpu(value);
111 	} else {
112 		/* this should be supported by any device */
113 		dice->clock_caps = CLOCK_CAP_RATE_44100 |
114 				   CLOCK_CAP_RATE_48000 |
115 				   CLOCK_CAP_SOURCE_ARX1 |
116 				   CLOCK_CAP_SOURCE_INTERNAL;
117 	}
118 
119 	return 0;
120 }
121 
122 static void dice_card_strings(struct snd_dice *dice)
123 {
124 	struct snd_card *card = dice->card;
125 	struct fw_device *dev = fw_parent_device(dice->unit);
126 	char vendor[32], model[32];
127 	unsigned int i;
128 	int err;
129 
130 	strcpy(card->driver, "DICE");
131 
132 	strcpy(card->shortname, "DICE");
133 	BUILD_BUG_ON(NICK_NAME_SIZE < sizeof(card->shortname));
134 	err = snd_dice_transaction_read_global(dice, GLOBAL_NICK_NAME,
135 					       card->shortname,
136 					       sizeof(card->shortname));
137 	if (err >= 0) {
138 		/* DICE strings are returned in "always-wrong" endianness */
139 		BUILD_BUG_ON(sizeof(card->shortname) % 4 != 0);
140 		for (i = 0; i < sizeof(card->shortname); i += 4)
141 			swab32s((u32 *)&card->shortname[i]);
142 		card->shortname[sizeof(card->shortname) - 1] = '\0';
143 	}
144 
145 	strcpy(vendor, "?");
146 	fw_csr_string(dev->config_rom + 5, CSR_VENDOR, vendor, sizeof(vendor));
147 	strcpy(model, "?");
148 	fw_csr_string(dice->unit->directory, CSR_MODEL, model, sizeof(model));
149 	snprintf(card->longname, sizeof(card->longname),
150 		 "%s %s (serial %u) at %s, S%d",
151 		 vendor, model, dev->config_rom[4] & 0x3fffff,
152 		 dev_name(&dice->unit->device), 100 << dev->max_speed);
153 
154 	strcpy(card->mixername, "DICE");
155 }
156 
157 static void dice_free(struct snd_dice *dice)
158 {
159 	snd_dice_stream_destroy_duplex(dice);
160 	snd_dice_transaction_destroy(dice);
161 	fw_unit_put(dice->unit);
162 
163 	mutex_destroy(&dice->mutex);
164 	kfree(dice);
165 }
166 
167 /*
168  * This module releases the FireWire unit data after all ALSA character devices
169  * are released by applications. This is for releasing stream data or finishing
170  * transactions safely. Thus at returning from .remove(), this module still keep
171  * references for the unit.
172  */
173 static void dice_card_free(struct snd_card *card)
174 {
175 	dice_free(card->private_data);
176 }
177 
178 static void do_registration(struct work_struct *work)
179 {
180 	struct snd_dice *dice = container_of(work, struct snd_dice, dwork.work);
181 	int err;
182 
183 	if (dice->registered)
184 		return;
185 
186 	err = snd_card_new(&dice->unit->device, -1, NULL, THIS_MODULE, 0,
187 			   &dice->card);
188 	if (err < 0)
189 		return;
190 
191 	if (force_two_pcm_support(dice->unit))
192 		dice->force_two_pcms = true;
193 
194 	err = snd_dice_transaction_init(dice);
195 	if (err < 0)
196 		goto error;
197 
198 	err = check_clock_caps(dice);
199 	if (err < 0)
200 		goto error;
201 
202 	dice_card_strings(dice);
203 
204 	snd_dice_create_proc(dice);
205 
206 	err = snd_dice_create_pcm(dice);
207 	if (err < 0)
208 		goto error;
209 
210 	err = snd_dice_create_midi(dice);
211 	if (err < 0)
212 		goto error;
213 
214 	err = snd_dice_create_hwdep(dice);
215 	if (err < 0)
216 		goto error;
217 
218 	err = snd_card_register(dice->card);
219 	if (err < 0)
220 		goto error;
221 
222 	/*
223 	 * After registered, dice instance can be released corresponding to
224 	 * releasing the sound card instance.
225 	 */
226 	dice->card->private_free = dice_card_free;
227 	dice->card->private_data = dice;
228 	dice->registered = true;
229 
230 	return;
231 error:
232 	snd_dice_transaction_destroy(dice);
233 	snd_card_free(dice->card);
234 	dev_info(&dice->unit->device,
235 		 "Sound card registration failed: %d\n", err);
236 }
237 
238 static void schedule_registration(struct snd_dice *dice)
239 {
240 	struct fw_card *fw_card = fw_parent_device(dice->unit)->card;
241 	u64 now, delay;
242 
243 	now = get_jiffies_64();
244 	delay = fw_card->reset_jiffies + msecs_to_jiffies(PROBE_DELAY_MS);
245 
246 	if (time_after64(delay, now))
247 		delay -= now;
248 	else
249 		delay = 0;
250 
251 	mod_delayed_work(system_wq, &dice->dwork, delay);
252 }
253 
254 static int dice_probe(struct fw_unit *unit, const struct ieee1394_device_id *id)
255 {
256 	struct snd_dice *dice;
257 	int err;
258 
259 	err = check_dice_category(unit);
260 	if (err < 0)
261 		return -ENODEV;
262 
263 	/* Allocate this independent of sound card instance. */
264 	dice = kzalloc(sizeof(struct snd_dice), GFP_KERNEL);
265 	if (dice == NULL)
266 		return -ENOMEM;
267 
268 	dice->unit = fw_unit_get(unit);
269 	dev_set_drvdata(&unit->device, dice);
270 
271 	spin_lock_init(&dice->lock);
272 	mutex_init(&dice->mutex);
273 	init_completion(&dice->clock_accepted);
274 	init_waitqueue_head(&dice->hwdep_wait);
275 
276 	err = snd_dice_stream_init_duplex(dice);
277 	if (err < 0) {
278 		dice_free(dice);
279 		return err;
280 	}
281 
282 	/* Allocate and register this sound card later. */
283 	INIT_DEFERRABLE_WORK(&dice->dwork, do_registration);
284 	schedule_registration(dice);
285 
286 	return 0;
287 }
288 
289 static void dice_remove(struct fw_unit *unit)
290 {
291 	struct snd_dice *dice = 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(&dice->dwork);
299 
300 	if (dice->registered) {
301 		/* No need to wait for releasing card object in this context. */
302 		snd_card_free_when_closed(dice->card);
303 	} else {
304 		/* Don't forget this case. */
305 		dice_free(dice);
306 	}
307 }
308 
309 static void dice_bus_reset(struct fw_unit *unit)
310 {
311 	struct snd_dice *dice = dev_get_drvdata(&unit->device);
312 
313 	/* Postpone a workqueue for deferred registration. */
314 	if (!dice->registered)
315 		schedule_registration(dice);
316 
317 	/* The handler address register becomes initialized. */
318 	snd_dice_transaction_reinit(dice);
319 
320 	/*
321 	 * After registration, userspace can start packet streaming, then this
322 	 * code block works fine.
323 	 */
324 	if (dice->registered) {
325 		mutex_lock(&dice->mutex);
326 		snd_dice_stream_update_duplex(dice);
327 		mutex_unlock(&dice->mutex);
328 	}
329 }
330 
331 #define DICE_INTERFACE	0x000001
332 
333 static const struct ieee1394_device_id dice_id_table[] = {
334 	{
335 		.match_flags = IEEE1394_MATCH_VERSION,
336 		.version     = DICE_INTERFACE,
337 	},
338 	{ }
339 };
340 MODULE_DEVICE_TABLE(ieee1394, dice_id_table);
341 
342 static struct fw_driver dice_driver = {
343 	.driver   = {
344 		.owner	= THIS_MODULE,
345 		.name	= KBUILD_MODNAME,
346 		.bus	= &fw_bus_type,
347 	},
348 	.probe    = dice_probe,
349 	.update   = dice_bus_reset,
350 	.remove   = dice_remove,
351 	.id_table = dice_id_table,
352 };
353 
354 static int __init alsa_dice_init(void)
355 {
356 	return driver_register(&dice_driver.driver);
357 }
358 
359 static void __exit alsa_dice_exit(void)
360 {
361 	driver_unregister(&dice_driver.driver);
362 }
363 
364 module_init(alsa_dice_init);
365 module_exit(alsa_dice_exit);
366