xref: /openbmc/linux/sound/firewire/dice/dice.c (revision a2818ee4)
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_card_free(struct snd_card *card)
126 {
127 	struct snd_dice *dice = card->private_data;
128 
129 	snd_dice_stream_destroy_duplex(dice);
130 	snd_dice_transaction_destroy(dice);
131 }
132 
133 static void do_registration(struct work_struct *work)
134 {
135 	struct snd_dice *dice = container_of(work, struct snd_dice, dwork.work);
136 	int err;
137 
138 	if (dice->registered)
139 		return;
140 
141 	err = snd_card_new(&dice->unit->device, -1, NULL, THIS_MODULE, 0,
142 			   &dice->card);
143 	if (err < 0)
144 		return;
145 	dice->card->private_free = dice_card_free;
146 	dice->card->private_data = dice;
147 
148 	err = snd_dice_transaction_init(dice);
149 	if (err < 0)
150 		goto error;
151 
152 	err = check_clock_caps(dice);
153 	if (err < 0)
154 		goto error;
155 
156 	dice_card_strings(dice);
157 
158 	err = dice->detect_formats(dice);
159 	if (err < 0)
160 		goto error;
161 
162 	err = snd_dice_stream_init_duplex(dice);
163 	if (err < 0)
164 		goto error;
165 
166 	snd_dice_create_proc(dice);
167 
168 	err = snd_dice_create_pcm(dice);
169 	if (err < 0)
170 		goto error;
171 
172 	err = snd_dice_create_midi(dice);
173 	if (err < 0)
174 		goto error;
175 
176 	err = snd_dice_create_hwdep(dice);
177 	if (err < 0)
178 		goto error;
179 
180 	err = snd_card_register(dice->card);
181 	if (err < 0)
182 		goto error;
183 
184 	dice->registered = true;
185 
186 	return;
187 error:
188 	snd_card_free(dice->card);
189 	dev_info(&dice->unit->device,
190 		 "Sound card registration failed: %d\n", err);
191 }
192 
193 static int dice_probe(struct fw_unit *unit,
194 		      const struct ieee1394_device_id *entry)
195 {
196 	struct snd_dice *dice;
197 	int err;
198 
199 	if (!entry->driver_data) {
200 		err = check_dice_category(unit);
201 		if (err < 0)
202 			return -ENODEV;
203 	}
204 
205 	/* Allocate this independent of sound card instance. */
206 	dice = devm_kzalloc(&unit->device, sizeof(struct snd_dice), GFP_KERNEL);
207 	if (!dice)
208 		return -ENOMEM;
209 	dice->unit = fw_unit_get(unit);
210 	dev_set_drvdata(&unit->device, dice);
211 
212 	if (!entry->driver_data) {
213 		dice->detect_formats = snd_dice_stream_detect_current_formats;
214 	} else {
215 		dice->detect_formats =
216 				(snd_dice_detect_formats_t)entry->driver_data;
217 	}
218 
219 	spin_lock_init(&dice->lock);
220 	mutex_init(&dice->mutex);
221 	init_completion(&dice->clock_accepted);
222 	init_waitqueue_head(&dice->hwdep_wait);
223 
224 	/* Allocate and register this sound card later. */
225 	INIT_DEFERRABLE_WORK(&dice->dwork, do_registration);
226 	snd_fw_schedule_registration(unit, &dice->dwork);
227 
228 	return 0;
229 }
230 
231 static void dice_remove(struct fw_unit *unit)
232 {
233 	struct snd_dice *dice = dev_get_drvdata(&unit->device);
234 
235 	/*
236 	 * Confirm to stop the work for registration before the sound card is
237 	 * going to be released. The work is not scheduled again because bus
238 	 * reset handler is not called anymore.
239 	 */
240 	cancel_delayed_work_sync(&dice->dwork);
241 
242 	if (dice->registered) {
243 		// Block till all of ALSA character devices are released.
244 		snd_card_free(dice->card);
245 	}
246 
247 	mutex_destroy(&dice->mutex);
248 	fw_unit_put(dice->unit);
249 }
250 
251 static void dice_bus_reset(struct fw_unit *unit)
252 {
253 	struct snd_dice *dice = dev_get_drvdata(&unit->device);
254 
255 	/* Postpone a workqueue for deferred registration. */
256 	if (!dice->registered)
257 		snd_fw_schedule_registration(unit, &dice->dwork);
258 
259 	/* The handler address register becomes initialized. */
260 	snd_dice_transaction_reinit(dice);
261 
262 	/*
263 	 * After registration, userspace can start packet streaming, then this
264 	 * code block works fine.
265 	 */
266 	if (dice->registered) {
267 		mutex_lock(&dice->mutex);
268 		snd_dice_stream_update_duplex(dice);
269 		mutex_unlock(&dice->mutex);
270 	}
271 }
272 
273 #define DICE_INTERFACE	0x000001
274 
275 static const struct ieee1394_device_id dice_id_table[] = {
276 	/* M-Audio Profire 2626 has a different value in version field. */
277 	{
278 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
279 				  IEEE1394_MATCH_MODEL_ID,
280 		.vendor_id	= OUI_MAUDIO,
281 		.model_id	= 0x000010,
282 		.driver_data = (kernel_ulong_t)snd_dice_detect_extension_formats,
283 	},
284 	/* M-Audio Profire 610 has a different value in version field. */
285 	{
286 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
287 				  IEEE1394_MATCH_MODEL_ID,
288 		.vendor_id	= OUI_MAUDIO,
289 		.model_id	= 0x000011,
290 		.driver_data = (kernel_ulong_t)snd_dice_detect_extension_formats,
291 	},
292 	/* TC Electronic Konnekt 24D. */
293 	{
294 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
295 				  IEEE1394_MATCH_MODEL_ID,
296 		.vendor_id	= OUI_TCELECTRONIC,
297 		.model_id	= 0x000020,
298 		.driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
299 	},
300 	/* TC Electronic Konnekt 8. */
301 	{
302 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
303 				  IEEE1394_MATCH_MODEL_ID,
304 		.vendor_id	= OUI_TCELECTRONIC,
305 		.model_id	= 0x000021,
306 		.driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
307 	},
308 	/* TC Electronic Studio Konnekt 48. */
309 	{
310 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
311 				  IEEE1394_MATCH_MODEL_ID,
312 		.vendor_id	= OUI_TCELECTRONIC,
313 		.model_id	= 0x000022,
314 		.driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
315 	},
316 	/* TC Electronic Konnekt Live. */
317 	{
318 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
319 				  IEEE1394_MATCH_MODEL_ID,
320 		.vendor_id	= OUI_TCELECTRONIC,
321 		.model_id	= 0x000023,
322 		.driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
323 	},
324 	/* TC Electronic Desktop Konnekt 6. */
325 	{
326 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
327 				  IEEE1394_MATCH_MODEL_ID,
328 		.vendor_id	= OUI_TCELECTRONIC,
329 		.model_id	= 0x000024,
330 		.driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
331 	},
332 	/* TC Electronic Impact Twin. */
333 	{
334 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
335 				  IEEE1394_MATCH_MODEL_ID,
336 		.vendor_id	= OUI_TCELECTRONIC,
337 		.model_id	= 0x000027,
338 		.driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
339 	},
340 	/* TC Electronic Digital Konnekt x32. */
341 	{
342 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
343 				  IEEE1394_MATCH_MODEL_ID,
344 		.vendor_id	= OUI_TCELECTRONIC,
345 		.model_id	= 0x000030,
346 		.driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
347 	},
348 	/* Alesis iO14/iO26. */
349 	{
350 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
351 				  IEEE1394_MATCH_MODEL_ID,
352 		.vendor_id	= OUI_ALESIS,
353 		.model_id	= MODEL_ALESIS_IO_BOTH,
354 		.driver_data = (kernel_ulong_t)snd_dice_detect_alesis_formats,
355 	},
356 	/* Mytek Stereo 192 DSD-DAC. */
357 	{
358 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
359 				  IEEE1394_MATCH_MODEL_ID,
360 		.vendor_id	= OUI_MYTEK,
361 		.model_id	= 0x000002,
362 		.driver_data = (kernel_ulong_t)snd_dice_detect_mytek_formats,
363 	},
364 	{
365 		.match_flags = IEEE1394_MATCH_VERSION,
366 		.version     = DICE_INTERFACE,
367 	},
368 	{ }
369 };
370 MODULE_DEVICE_TABLE(ieee1394, dice_id_table);
371 
372 static struct fw_driver dice_driver = {
373 	.driver   = {
374 		.owner	= THIS_MODULE,
375 		.name	= KBUILD_MODNAME,
376 		.bus	= &fw_bus_type,
377 	},
378 	.probe    = dice_probe,
379 	.update   = dice_bus_reset,
380 	.remove   = dice_remove,
381 	.id_table = dice_id_table,
382 };
383 
384 static int __init alsa_dice_init(void)
385 {
386 	return driver_register(&dice_driver.driver);
387 }
388 
389 static void __exit alsa_dice_exit(void)
390 {
391 	driver_unregister(&dice_driver.driver);
392 }
393 
394 module_init(alsa_dice_init);
395 module_exit(alsa_dice_exit);
396