xref: /openbmc/linux/sound/firewire/dice/dice.c (revision ca460cc2)
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 
16 #define DICE_CATEGORY_ID	0x04
17 #define WEISS_CATEGORY_ID	0x00
18 
19 static int dice_interface_check(struct fw_unit *unit)
20 {
21 	static const int min_values[10] = {
22 		10, 0x64 / 4,
23 		10, 0x18 / 4,
24 		10, 0x18 / 4,
25 		0, 0,
26 		0, 0,
27 	};
28 	struct fw_device *device = fw_parent_device(unit);
29 	struct fw_csr_iterator it;
30 	int key, val, vendor = -1, model = -1, err;
31 	unsigned int category, i;
32 	__be32 *pointers, value;
33 	__be32 tx_data[4];
34 	__be32 version;
35 
36 	pointers = kmalloc_array(ARRAY_SIZE(min_values), sizeof(__be32),
37 				 GFP_KERNEL);
38 	if (pointers == NULL)
39 		return -ENOMEM;
40 
41 	/*
42 	 * Check that GUID and unit directory are constructed according to DICE
43 	 * rules, i.e., that the specifier ID is the GUID's OUI, and that the
44 	 * GUID chip ID consists of the 8-bit category ID, the 10-bit product
45 	 * ID, and a 22-bit serial number.
46 	 */
47 	fw_csr_iterator_init(&it, unit->directory);
48 	while (fw_csr_iterator_next(&it, &key, &val)) {
49 		switch (key) {
50 		case CSR_SPECIFIER_ID:
51 			vendor = val;
52 			break;
53 		case CSR_MODEL:
54 			model = val;
55 			break;
56 		}
57 	}
58 	if (vendor == OUI_WEISS)
59 		category = WEISS_CATEGORY_ID;
60 	else
61 		category = DICE_CATEGORY_ID;
62 	if (device->config_rom[3] != ((vendor << 8) | category) ||
63 	    device->config_rom[4] >> 22 != model) {
64 		err = -ENODEV;
65 		goto end;
66 	}
67 
68 	/*
69 	 * Check that the sub address spaces exist and are located inside the
70 	 * private address space.  The minimum values are chosen so that all
71 	 * minimally required registers are included.
72 	 */
73 	err = snd_fw_transaction(unit, TCODE_READ_BLOCK_REQUEST,
74 				 DICE_PRIVATE_SPACE, pointers,
75 				 sizeof(__be32) * ARRAY_SIZE(min_values), 0);
76 	if (err < 0) {
77 		err = -ENODEV;
78 		goto end;
79 	}
80 	for (i = 0; i < ARRAY_SIZE(min_values); ++i) {
81 		value = be32_to_cpu(pointers[i]);
82 		if (value < min_values[i] || value >= 0x40000) {
83 			err = -ENODEV;
84 			goto end;
85 		}
86 	}
87 
88 	/* We support playback only. Let capture devices be handled by FFADO. */
89 	err = snd_fw_transaction(unit, TCODE_READ_BLOCK_REQUEST,
90 				 DICE_PRIVATE_SPACE +
91 				 be32_to_cpu(pointers[2]) * 4,
92 				 tx_data, sizeof(tx_data), 0);
93 	if (err < 0 || (tx_data[0] && tx_data[3])) {
94 		err = -ENODEV;
95 		goto end;
96 	}
97 
98 	/*
99 	 * Check that the implemented DICE driver specification major version
100 	 * number matches.
101 	 */
102 	err = snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST,
103 				 DICE_PRIVATE_SPACE +
104 				 be32_to_cpu(pointers[0]) * 4 + GLOBAL_VERSION,
105 				 &version, 4, 0);
106 	if (err < 0) {
107 		err = -ENODEV;
108 		goto end;
109 	}
110 	if ((version & cpu_to_be32(0xff000000)) != cpu_to_be32(0x01000000)) {
111 		dev_err(&unit->device,
112 			"unknown DICE version: 0x%08x\n", be32_to_cpu(version));
113 		err = -ENODEV;
114 		goto end;
115 	}
116 end:
117 	return err;
118 }
119 
120 static int highest_supported_mode_rate(struct snd_dice *dice,
121 				       unsigned int mode, unsigned int *rate)
122 {
123 	unsigned int i, m;
124 
125 	for (i = ARRAY_SIZE(snd_dice_rates); i > 0; i--) {
126 		*rate = snd_dice_rates[i - 1];
127 		if (snd_dice_stream_get_rate_mode(dice, *rate, &m) < 0)
128 			continue;
129 		if (mode == m)
130 			break;
131 	}
132 	if (i == 0)
133 		return -EINVAL;
134 
135 	return 0;
136 }
137 
138 static int dice_read_mode_params(struct snd_dice *dice, unsigned int mode)
139 {
140 	__be32 values[2];
141 	unsigned int rate;
142 	int err;
143 
144 	if (highest_supported_mode_rate(dice, mode, &rate) < 0) {
145 		dice->rx_channels[mode] = 0;
146 		dice->rx_midi_ports[mode] = 0;
147 		return 0;
148 	}
149 
150 	err = snd_dice_transaction_set_rate(dice, rate);
151 	if (err < 0)
152 		return err;
153 
154 	err = snd_dice_transaction_read_rx(dice, RX_NUMBER_AUDIO,
155 					   values, sizeof(values));
156 	if (err < 0)
157 		return err;
158 
159 	dice->rx_channels[mode]   = be32_to_cpu(values[0]);
160 	dice->rx_midi_ports[mode] = be32_to_cpu(values[1]);
161 
162 	return 0;
163 }
164 
165 static int dice_read_params(struct snd_dice *dice)
166 {
167 	__be32 value;
168 	int mode, err;
169 
170 	/* some very old firmwares don't tell about their clock support */
171 	if (dice->clock_caps > 0) {
172 		err = snd_dice_transaction_read_global(dice,
173 						GLOBAL_CLOCK_CAPABILITIES,
174 						&value, 4);
175 		if (err < 0)
176 			return err;
177 		dice->clock_caps = be32_to_cpu(value);
178 	} else {
179 		/* this should be supported by any device */
180 		dice->clock_caps = CLOCK_CAP_RATE_44100 |
181 				   CLOCK_CAP_RATE_48000 |
182 				   CLOCK_CAP_SOURCE_ARX1 |
183 				   CLOCK_CAP_SOURCE_INTERNAL;
184 	}
185 
186 	for (mode = 2; mode >= 0; --mode) {
187 		err = dice_read_mode_params(dice, mode);
188 		if (err < 0)
189 			return err;
190 	}
191 
192 	return 0;
193 }
194 
195 static void dice_card_strings(struct snd_dice *dice)
196 {
197 	struct snd_card *card = dice->card;
198 	struct fw_device *dev = fw_parent_device(dice->unit);
199 	char vendor[32], model[32];
200 	unsigned int i;
201 	int err;
202 
203 	strcpy(card->driver, "DICE");
204 
205 	strcpy(card->shortname, "DICE");
206 	BUILD_BUG_ON(NICK_NAME_SIZE < sizeof(card->shortname));
207 	err = snd_dice_transaction_read_global(dice, GLOBAL_NICK_NAME,
208 					       card->shortname,
209 					       sizeof(card->shortname));
210 	if (err >= 0) {
211 		/* DICE strings are returned in "always-wrong" endianness */
212 		BUILD_BUG_ON(sizeof(card->shortname) % 4 != 0);
213 		for (i = 0; i < sizeof(card->shortname); i += 4)
214 			swab32s((u32 *)&card->shortname[i]);
215 		card->shortname[sizeof(card->shortname) - 1] = '\0';
216 	}
217 
218 	strcpy(vendor, "?");
219 	fw_csr_string(dev->config_rom + 5, CSR_VENDOR, vendor, sizeof(vendor));
220 	strcpy(model, "?");
221 	fw_csr_string(dice->unit->directory, CSR_MODEL, model, sizeof(model));
222 	snprintf(card->longname, sizeof(card->longname),
223 		 "%s %s (serial %u) at %s, S%d",
224 		 vendor, model, dev->config_rom[4] & 0x3fffff,
225 		 dev_name(&dice->unit->device), 100 << dev->max_speed);
226 
227 	strcpy(card->mixername, "DICE");
228 }
229 
230 static void dice_card_free(struct snd_card *card)
231 {
232 	struct snd_dice *dice = card->private_data;
233 
234 	snd_dice_transaction_destroy(dice);
235 	mutex_destroy(&dice->mutex);
236 }
237 
238 static int dice_probe(struct fw_unit *unit, const struct ieee1394_device_id *id)
239 {
240 	struct snd_card *card;
241 	struct snd_dice *dice;
242 	int err;
243 
244 	err = dice_interface_check(unit);
245 	if (err < 0)
246 		goto end;
247 
248 	err = snd_card_new(&unit->device, -1, NULL, THIS_MODULE,
249 			   sizeof(*dice), &card);
250 	if (err < 0)
251 		goto end;
252 
253 	dice = card->private_data;
254 	dice->card = card;
255 	dice->unit = unit;
256 	card->private_free = dice_card_free;
257 
258 	spin_lock_init(&dice->lock);
259 	mutex_init(&dice->mutex);
260 	init_completion(&dice->clock_accepted);
261 	init_waitqueue_head(&dice->hwdep_wait);
262 
263 	err = snd_dice_transaction_init(dice);
264 	if (err < 0)
265 		goto error;
266 
267 	err = dice_read_params(dice);
268 	if (err < 0)
269 		goto error;
270 
271 	dice_card_strings(dice);
272 
273 	err = snd_dice_create_pcm(dice);
274 	if (err < 0)
275 		goto error;
276 
277 	err = snd_dice_create_hwdep(dice);
278 	if (err < 0)
279 		goto error;
280 
281 	snd_dice_create_proc(dice);
282 
283 	err = snd_dice_stream_init(dice);
284 	if (err < 0)
285 		goto error;
286 
287 	err = snd_card_register(card);
288 	if (err < 0) {
289 		snd_dice_stream_destroy(dice);
290 		goto error;
291 	}
292 
293 	dev_set_drvdata(&unit->device, dice);
294 end:
295 	return err;
296 error:
297 	snd_card_free(card);
298 	return err;
299 }
300 
301 static void dice_remove(struct fw_unit *unit)
302 {
303 	struct snd_dice *dice = dev_get_drvdata(&unit->device);
304 
305 	snd_card_disconnect(dice->card);
306 
307 	mutex_lock(&dice->mutex);
308 
309 	snd_dice_stream_destroy(dice);
310 
311 	mutex_unlock(&dice->mutex);
312 
313 	snd_card_free_when_closed(dice->card);
314 }
315 
316 static void dice_bus_reset(struct fw_unit *unit)
317 {
318 	struct snd_dice *dice = dev_get_drvdata(&unit->device);
319 
320 	/* The handler address register becomes initialized. */
321 	snd_dice_transaction_reinit(dice);
322 
323 	mutex_lock(&dice->mutex);
324 	snd_dice_stream_update(dice);
325 	mutex_unlock(&dice->mutex);
326 }
327 
328 #define DICE_INTERFACE	0x000001
329 
330 static const struct ieee1394_device_id dice_id_table[] = {
331 	{
332 		.match_flags = IEEE1394_MATCH_VERSION,
333 		.version     = DICE_INTERFACE,
334 	},
335 	{ }
336 };
337 MODULE_DEVICE_TABLE(ieee1394, dice_id_table);
338 
339 static struct fw_driver dice_driver = {
340 	.driver   = {
341 		.owner	= THIS_MODULE,
342 		.name	= KBUILD_MODNAME,
343 		.bus	= &fw_bus_type,
344 	},
345 	.probe    = dice_probe,
346 	.update   = dice_bus_reset,
347 	.remove   = dice_remove,
348 	.id_table = dice_id_table,
349 };
350 
351 static int __init alsa_dice_init(void)
352 {
353 	return driver_register(&dice_driver.driver);
354 }
355 
356 static void __exit alsa_dice_exit(void)
357 {
358 	driver_unregister(&dice_driver.driver);
359 }
360 
361 module_init(alsa_dice_init);
362 module_exit(alsa_dice_exit);
363