xref: /openbmc/linux/sound/firewire/dice/dice.c (revision ba61bb17)
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	0x000166
18 #define OUI_ALESIS		0x000595
19 #define OUI_MAUDIO		0x000d6c
20 #define OUI_MYTEK		0x001ee8
21 
22 #define DICE_CATEGORY_ID	0x04
23 #define WEISS_CATEGORY_ID	0x00
24 #define LOUD_CATEGORY_ID	0x10
25 
26 #define MODEL_ALESIS_IO_BOTH	0x000001
27 
28 static int check_dice_category(struct fw_unit *unit)
29 {
30 	struct fw_device *device = fw_parent_device(unit);
31 	struct fw_csr_iterator it;
32 	int key, val, vendor = -1, model = -1;
33 	unsigned int category;
34 
35 	/*
36 	 * Check that GUID and unit directory are constructed according to DICE
37 	 * rules, i.e., that the specifier ID is the GUID's OUI, and that the
38 	 * GUID chip ID consists of the 8-bit category ID, the 10-bit product
39 	 * ID, and a 22-bit serial number.
40 	 */
41 	fw_csr_iterator_init(&it, unit->directory);
42 	while (fw_csr_iterator_next(&it, &key, &val)) {
43 		switch (key) {
44 		case CSR_SPECIFIER_ID:
45 			vendor = val;
46 			break;
47 		case CSR_MODEL:
48 			model = val;
49 			break;
50 		}
51 	}
52 
53 	if (vendor == OUI_WEISS)
54 		category = WEISS_CATEGORY_ID;
55 	else if (vendor == OUI_LOUD)
56 		category = LOUD_CATEGORY_ID;
57 	else
58 		category = DICE_CATEGORY_ID;
59 	if (device->config_rom[3] != ((vendor << 8) | category) ||
60 	    device->config_rom[4] >> 22 != model)
61 		return -ENODEV;
62 
63 	return 0;
64 }
65 
66 static int check_clock_caps(struct snd_dice *dice)
67 {
68 	__be32 value;
69 	int err;
70 
71 	/* some very old firmwares don't tell about their clock support */
72 	if (dice->clock_caps > 0) {
73 		err = snd_dice_transaction_read_global(dice,
74 						GLOBAL_CLOCK_CAPABILITIES,
75 						&value, 4);
76 		if (err < 0)
77 			return err;
78 		dice->clock_caps = be32_to_cpu(value);
79 	} else {
80 		/* this should be supported by any device */
81 		dice->clock_caps = CLOCK_CAP_RATE_44100 |
82 				   CLOCK_CAP_RATE_48000 |
83 				   CLOCK_CAP_SOURCE_ARX1 |
84 				   CLOCK_CAP_SOURCE_INTERNAL;
85 	}
86 
87 	return 0;
88 }
89 
90 static void dice_card_strings(struct snd_dice *dice)
91 {
92 	struct snd_card *card = dice->card;
93 	struct fw_device *dev = fw_parent_device(dice->unit);
94 	char vendor[32], model[32];
95 	unsigned int i;
96 	int err;
97 
98 	strcpy(card->driver, "DICE");
99 
100 	strcpy(card->shortname, "DICE");
101 	BUILD_BUG_ON(NICK_NAME_SIZE < sizeof(card->shortname));
102 	err = snd_dice_transaction_read_global(dice, GLOBAL_NICK_NAME,
103 					       card->shortname,
104 					       sizeof(card->shortname));
105 	if (err >= 0) {
106 		/* DICE strings are returned in "always-wrong" endianness */
107 		BUILD_BUG_ON(sizeof(card->shortname) % 4 != 0);
108 		for (i = 0; i < sizeof(card->shortname); i += 4)
109 			swab32s((u32 *)&card->shortname[i]);
110 		card->shortname[sizeof(card->shortname) - 1] = '\0';
111 	}
112 
113 	strcpy(vendor, "?");
114 	fw_csr_string(dev->config_rom + 5, CSR_VENDOR, vendor, sizeof(vendor));
115 	strcpy(model, "?");
116 	fw_csr_string(dice->unit->directory, CSR_MODEL, model, sizeof(model));
117 	snprintf(card->longname, sizeof(card->longname),
118 		 "%s %s (serial %u) at %s, S%d",
119 		 vendor, model, dev->config_rom[4] & 0x3fffff,
120 		 dev_name(&dice->unit->device), 100 << dev->max_speed);
121 
122 	strcpy(card->mixername, "DICE");
123 }
124 
125 static void dice_free(struct snd_dice *dice)
126 {
127 	snd_dice_stream_destroy_duplex(dice);
128 	snd_dice_transaction_destroy(dice);
129 	fw_unit_put(dice->unit);
130 
131 	mutex_destroy(&dice->mutex);
132 	kfree(dice);
133 }
134 
135 /*
136  * This module releases the FireWire unit data after all ALSA character devices
137  * are released by applications. This is for releasing stream data or finishing
138  * transactions safely. Thus at returning from .remove(), this module still keep
139  * references for the unit.
140  */
141 static void dice_card_free(struct snd_card *card)
142 {
143 	dice_free(card->private_data);
144 }
145 
146 static void do_registration(struct work_struct *work)
147 {
148 	struct snd_dice *dice = container_of(work, struct snd_dice, dwork.work);
149 	int err;
150 
151 	if (dice->registered)
152 		return;
153 
154 	err = snd_card_new(&dice->unit->device, -1, NULL, THIS_MODULE, 0,
155 			   &dice->card);
156 	if (err < 0)
157 		return;
158 
159 	err = snd_dice_transaction_init(dice);
160 	if (err < 0)
161 		goto error;
162 
163 	err = check_clock_caps(dice);
164 	if (err < 0)
165 		goto error;
166 
167 	dice_card_strings(dice);
168 
169 	err = dice->detect_formats(dice);
170 	if (err < 0)
171 		goto error;
172 
173 	err = snd_dice_stream_init_duplex(dice);
174 	if (err < 0)
175 		goto error;
176 
177 	snd_dice_create_proc(dice);
178 
179 	err = snd_dice_create_pcm(dice);
180 	if (err < 0)
181 		goto error;
182 
183 	err = snd_dice_create_midi(dice);
184 	if (err < 0)
185 		goto error;
186 
187 	err = snd_dice_create_hwdep(dice);
188 	if (err < 0)
189 		goto error;
190 
191 	err = snd_card_register(dice->card);
192 	if (err < 0)
193 		goto error;
194 
195 	/*
196 	 * After registered, dice instance can be released corresponding to
197 	 * releasing the sound card instance.
198 	 */
199 	dice->card->private_free = dice_card_free;
200 	dice->card->private_data = dice;
201 	dice->registered = true;
202 
203 	return;
204 error:
205 	snd_dice_stream_destroy_duplex(dice);
206 	snd_dice_transaction_destroy(dice);
207 	snd_dice_stream_destroy_duplex(dice);
208 	snd_card_free(dice->card);
209 	dev_info(&dice->unit->device,
210 		 "Sound card registration failed: %d\n", err);
211 }
212 
213 static int dice_probe(struct fw_unit *unit,
214 		      const struct ieee1394_device_id *entry)
215 {
216 	struct snd_dice *dice;
217 	int err;
218 
219 	if (!entry->driver_data) {
220 		err = check_dice_category(unit);
221 		if (err < 0)
222 			return -ENODEV;
223 	}
224 
225 	/* Allocate this independent of sound card instance. */
226 	dice = kzalloc(sizeof(struct snd_dice), GFP_KERNEL);
227 	if (dice == NULL)
228 		return -ENOMEM;
229 
230 	dice->unit = fw_unit_get(unit);
231 	dev_set_drvdata(&unit->device, dice);
232 
233 	if (!entry->driver_data) {
234 		dice->detect_formats = snd_dice_stream_detect_current_formats;
235 	} else {
236 		dice->detect_formats =
237 				(snd_dice_detect_formats_t)entry->driver_data;
238 	}
239 
240 	spin_lock_init(&dice->lock);
241 	mutex_init(&dice->mutex);
242 	init_completion(&dice->clock_accepted);
243 	init_waitqueue_head(&dice->hwdep_wait);
244 
245 	/* Allocate and register this sound card later. */
246 	INIT_DEFERRABLE_WORK(&dice->dwork, do_registration);
247 	snd_fw_schedule_registration(unit, &dice->dwork);
248 
249 	return 0;
250 }
251 
252 static void dice_remove(struct fw_unit *unit)
253 {
254 	struct snd_dice *dice = dev_get_drvdata(&unit->device);
255 
256 	/*
257 	 * Confirm to stop the work for registration before the sound card is
258 	 * going to be released. The work is not scheduled again because bus
259 	 * reset handler is not called anymore.
260 	 */
261 	cancel_delayed_work_sync(&dice->dwork);
262 
263 	if (dice->registered) {
264 		/* No need to wait for releasing card object in this context. */
265 		snd_card_free_when_closed(dice->card);
266 	} else {
267 		/* Don't forget this case. */
268 		dice_free(dice);
269 	}
270 }
271 
272 static void dice_bus_reset(struct fw_unit *unit)
273 {
274 	struct snd_dice *dice = dev_get_drvdata(&unit->device);
275 
276 	/* Postpone a workqueue for deferred registration. */
277 	if (!dice->registered)
278 		snd_fw_schedule_registration(unit, &dice->dwork);
279 
280 	/* The handler address register becomes initialized. */
281 	snd_dice_transaction_reinit(dice);
282 
283 	/*
284 	 * After registration, userspace can start packet streaming, then this
285 	 * code block works fine.
286 	 */
287 	if (dice->registered) {
288 		mutex_lock(&dice->mutex);
289 		snd_dice_stream_update_duplex(dice);
290 		mutex_unlock(&dice->mutex);
291 	}
292 }
293 
294 #define DICE_INTERFACE	0x000001
295 
296 static const struct ieee1394_device_id dice_id_table[] = {
297 	/* M-Audio Profire 2626 has a different value in version field. */
298 	{
299 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
300 				  IEEE1394_MATCH_MODEL_ID,
301 		.vendor_id	= OUI_MAUDIO,
302 		.model_id	= 0x000010,
303 		.driver_data = (kernel_ulong_t)snd_dice_detect_extension_formats,
304 	},
305 	/* M-Audio Profire 610 has a different value in version field. */
306 	{
307 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
308 				  IEEE1394_MATCH_MODEL_ID,
309 		.vendor_id	= OUI_MAUDIO,
310 		.model_id	= 0x000011,
311 		.driver_data = (kernel_ulong_t)snd_dice_detect_extension_formats,
312 	},
313 	/* TC Electronic Konnekt 24D. */
314 	{
315 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
316 				  IEEE1394_MATCH_MODEL_ID,
317 		.vendor_id	= OUI_TCELECTRONIC,
318 		.model_id	= 0x000020,
319 		.driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
320 	},
321 	/* TC Electronic Konnekt 8. */
322 	{
323 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
324 				  IEEE1394_MATCH_MODEL_ID,
325 		.vendor_id	= OUI_TCELECTRONIC,
326 		.model_id	= 0x000021,
327 		.driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
328 	},
329 	/* TC Electronic Studio Konnekt 48. */
330 	{
331 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
332 				  IEEE1394_MATCH_MODEL_ID,
333 		.vendor_id	= OUI_TCELECTRONIC,
334 		.model_id	= 0x000022,
335 		.driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
336 	},
337 	/* TC Electronic Konnekt Live. */
338 	{
339 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
340 				  IEEE1394_MATCH_MODEL_ID,
341 		.vendor_id	= OUI_TCELECTRONIC,
342 		.model_id	= 0x000023,
343 		.driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
344 	},
345 	/* TC Electronic Desktop Konnekt 6. */
346 	{
347 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
348 				  IEEE1394_MATCH_MODEL_ID,
349 		.vendor_id	= OUI_TCELECTRONIC,
350 		.model_id	= 0x000024,
351 		.driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
352 	},
353 	/* TC Electronic Impact Twin. */
354 	{
355 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
356 				  IEEE1394_MATCH_MODEL_ID,
357 		.vendor_id	= OUI_TCELECTRONIC,
358 		.model_id	= 0x000027,
359 		.driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
360 	},
361 	/* TC Electronic Digital Konnekt x32. */
362 	{
363 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
364 				  IEEE1394_MATCH_MODEL_ID,
365 		.vendor_id	= OUI_TCELECTRONIC,
366 		.model_id	= 0x000030,
367 		.driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
368 	},
369 	/* Alesis iO14/iO26. */
370 	{
371 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
372 				  IEEE1394_MATCH_MODEL_ID,
373 		.vendor_id	= OUI_ALESIS,
374 		.model_id	= MODEL_ALESIS_IO_BOTH,
375 		.driver_data = (kernel_ulong_t)snd_dice_detect_alesis_formats,
376 	},
377 	/* Mytek Stereo 192 DSD-DAC. */
378 	{
379 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
380 				  IEEE1394_MATCH_MODEL_ID,
381 		.vendor_id	= OUI_MYTEK,
382 		.model_id	= 0x000002,
383 		.driver_data = (kernel_ulong_t)snd_dice_detect_mytek_formats,
384 	},
385 	{
386 		.match_flags = IEEE1394_MATCH_VERSION,
387 		.version     = DICE_INTERFACE,
388 	},
389 	{ }
390 };
391 MODULE_DEVICE_TABLE(ieee1394, dice_id_table);
392 
393 static struct fw_driver dice_driver = {
394 	.driver   = {
395 		.owner	= THIS_MODULE,
396 		.name	= KBUILD_MODNAME,
397 		.bus	= &fw_bus_type,
398 	},
399 	.probe    = dice_probe,
400 	.update   = dice_bus_reset,
401 	.remove   = dice_remove,
402 	.id_table = dice_id_table,
403 };
404 
405 static int __init alsa_dice_init(void)
406 {
407 	return driver_register(&dice_driver.driver);
408 }
409 
410 static void __exit alsa_dice_exit(void)
411 {
412 	driver_unregister(&dice_driver.driver);
413 }
414 
415 module_init(alsa_dice_init);
416 module_exit(alsa_dice_exit);
417