blob: 5d99436dfcaee5e70f3957a8f8b59be3d9e213d3 [file] [log] [blame]
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +02001/*
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
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +09008#include "dice.h"
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +02009
10MODULE_DESCRIPTION("DICE driver");
11MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
12MODULE_LICENSE("GPL v2");
13
Clemens Ladischa471fcd2012-02-13 21:55:13 +010014#define OUI_WEISS 0x001c6a
15
16#define DICE_CATEGORY_ID 0x04
17#define WEISS_CATEGORY_ID 0x00
Clemens Ladischcbab3282011-09-04 22:16:02 +020018
19static 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;
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +090030 int key, val, vendor = -1, model = -1, err;
Clemens Ladischa471fcd2012-02-13 21:55:13 +010031 unsigned int category, i;
Takashi Sakamotocbc6f282015-10-18 22:39:50 +090032 __be32 *pointers;
33 u32 value;
Clemens Ladischcbab3282011-09-04 22:16:02 +020034 __be32 version;
35
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +090036 pointers = kmalloc_array(ARRAY_SIZE(min_values), sizeof(__be32),
37 GFP_KERNEL);
38 if (pointers == NULL)
39 return -ENOMEM;
40
Clemens Ladischcbab3282011-09-04 22:16:02 +020041 /*
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
Clemens Ladischa471fcd2012-02-13 21:55:13 +010044 * GUID chip ID consists of the 8-bit category ID, the 10-bit product
45 * ID, and a 22-bit serial number.
Clemens Ladischcbab3282011-09-04 22:16:02 +020046 */
47 fw_csr_iterator_init(&it, unit->directory);
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +090048 while (fw_csr_iterator_next(&it, &key, &val)) {
Clemens Ladischcbab3282011-09-04 22:16:02 +020049 switch (key) {
50 case CSR_SPECIFIER_ID:
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +090051 vendor = val;
Clemens Ladischcbab3282011-09-04 22:16:02 +020052 break;
53 case CSR_MODEL:
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +090054 model = val;
Clemens Ladischcbab3282011-09-04 22:16:02 +020055 break;
56 }
57 }
Clemens Ladischa471fcd2012-02-13 21:55:13 +010058 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) ||
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +090063 device->config_rom[4] >> 22 != model) {
64 err = -ENODEV;
65 goto end;
66 }
Clemens Ladischcbab3282011-09-04 22:16:02 +020067
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,
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +090074 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) {
Clemens Ladischcbab3282011-09-04 22:16:02 +020081 value = be32_to_cpu(pointers[i]);
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +090082 if (value < min_values[i] || value >= 0x40000) {
83 err = -ENODEV;
84 goto end;
85 }
Clemens Ladischcbab3282011-09-04 22:16:02 +020086 }
87
88 /*
89 * Check that the implemented DICE driver specification major version
90 * number matches.
91 */
92 err = snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST,
93 DICE_PRIVATE_SPACE +
94 be32_to_cpu(pointers[0]) * 4 + GLOBAL_VERSION,
Clemens Ladisch1b704852011-09-04 22:17:38 +020095 &version, 4, 0);
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +090096 if (err < 0) {
97 err = -ENODEV;
98 goto end;
99 }
Clemens Ladischcbab3282011-09-04 22:16:02 +0200100 if ((version & cpu_to_be32(0xff000000)) != cpu_to_be32(0x01000000)) {
101 dev_err(&unit->device,
102 "unknown DICE version: 0x%08x\n", be32_to_cpu(version));
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +0900103 err = -ENODEV;
104 goto end;
Clemens Ladischcbab3282011-09-04 22:16:02 +0200105 }
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +0900106end:
107 return err;
Clemens Ladischcbab3282011-09-04 22:16:02 +0200108}
109
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +0900110static int highest_supported_mode_rate(struct snd_dice *dice,
111 unsigned int mode, unsigned int *rate)
Clemens Ladisch15a75c82011-12-04 22:23:59 +0100112{
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +0900113 unsigned int i, m;
Clemens Ladisch15a75c82011-12-04 22:23:59 +0100114
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +0900115 for (i = ARRAY_SIZE(snd_dice_rates); i > 0; i--) {
116 *rate = snd_dice_rates[i - 1];
117 if (snd_dice_stream_get_rate_mode(dice, *rate, &m) < 0)
118 continue;
119 if (mode == m)
120 break;
121 }
122 if (i == 0)
123 return -EINVAL;
Clemens Ladisch15a75c82011-12-04 22:23:59 +0100124
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +0900125 return 0;
Clemens Ladisch15a75c82011-12-04 22:23:59 +0100126}
127
Takashi Sakamoto732d1532014-11-29 00:59:11 +0900128static int dice_read_mode_params(struct snd_dice *dice, unsigned int mode)
Clemens Ladisch15a75c82011-12-04 22:23:59 +0100129{
130 __be32 values[2];
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +0900131 unsigned int rate;
132 int err;
Clemens Ladisch15a75c82011-12-04 22:23:59 +0100133
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +0900134 if (highest_supported_mode_rate(dice, mode, &rate) < 0) {
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900135 dice->tx_channels[mode] = 0;
136 dice->tx_midi_ports[mode] = 0;
Clemens Ladisch15a75c82011-12-04 22:23:59 +0100137 dice->rx_channels[mode] = 0;
138 dice->rx_midi_ports[mode] = 0;
139 return 0;
140 }
141
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +0900142 err = snd_dice_transaction_set_rate(dice, rate);
Clemens Ladisch15a75c82011-12-04 22:23:59 +0100143 if (err < 0)
144 return err;
145
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900146 err = snd_dice_transaction_read_tx(dice, TX_NUMBER_AUDIO,
147 values, sizeof(values));
148 if (err < 0)
149 return err;
150
151 dice->tx_channels[mode] = be32_to_cpu(values[0]);
152 dice->tx_midi_ports[mode] = be32_to_cpu(values[1]);
153
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +0900154 err = snd_dice_transaction_read_rx(dice, RX_NUMBER_AUDIO,
155 values, sizeof(values));
Clemens Ladisch15a75c82011-12-04 22:23:59 +0100156 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
Takashi Sakamoto732d1532014-11-29 00:59:11 +0900165static int dice_read_params(struct snd_dice *dice)
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200166{
Clemens Ladischa0301992011-12-04 21:47:00 +0100167 __be32 value;
Clemens Ladisch15a75c82011-12-04 22:23:59 +0100168 int mode, err;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200169
Clemens Ladischa0301992011-12-04 21:47:00 +0100170 /* some very old firmwares don't tell about their clock support */
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +0900171 if (dice->clock_caps > 0) {
172 err = snd_dice_transaction_read_global(dice,
173 GLOBAL_CLOCK_CAPABILITIES,
174 &value, 4);
Clemens Ladischa0301992011-12-04 21:47:00 +0100175 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
Clemens Ladisch15a75c82011-12-04 22:23:59 +0100186 for (mode = 2; mode >= 0; --mode) {
187 err = dice_read_mode_params(dice, mode);
188 if (err < 0)
189 return err;
190 }
191
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200192 return 0;
193}
194
Takashi Sakamoto732d1532014-11-29 00:59:11 +0900195static void dice_card_strings(struct snd_dice *dice)
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200196{
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));
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +0900207 err = snd_dice_transaction_read_global(dice, GLOBAL_NICK_NAME,
208 card->shortname,
209 sizeof(card->shortname));
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200210 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),
Clemens Ladischcbab3282011-09-04 22:16:02 +0200223 "%s %s (serial %u) at %s, S%d",
224 vendor, model, dev->config_rom[4] & 0x3fffff,
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200225 dev_name(&dice->unit->device), 100 << dev->max_speed);
226
227 strcpy(card->mixername, "DICE");
228}
229
Takashi Sakamoto12ed7192015-02-21 23:54:57 +0900230/*
231 * This module releases the FireWire unit data after all ALSA character devices
232 * are released by applications. This is for releasing stream data or finishing
233 * transactions safely. Thus at returning from .remove(), this module still keep
234 * references for the unit.
235 */
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +0900236static void dice_card_free(struct snd_card *card)
237{
238 struct snd_dice *dice = card->private_data;
239
Takashi Sakamotodec84312015-02-21 23:55:00 +0900240 snd_dice_stream_destroy_duplex(dice);
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +0900241 snd_dice_transaction_destroy(dice);
Takashi Sakamoto12ed7192015-02-21 23:54:57 +0900242 fw_unit_put(dice->unit);
243
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +0900244 mutex_destroy(&dice->mutex);
245}
246
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200247static int dice_probe(struct fw_unit *unit, const struct ieee1394_device_id *id)
248{
249 struct snd_card *card;
Takashi Sakamoto732d1532014-11-29 00:59:11 +0900250 struct snd_dice *dice;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200251 int err;
252
Clemens Ladischcbab3282011-09-04 22:16:02 +0200253 err = dice_interface_check(unit);
254 if (err < 0)
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +0900255 goto end;
Clemens Ladischcbab3282011-09-04 22:16:02 +0200256
Takashi Iwai06b45f02014-01-29 14:23:55 +0100257 err = snd_card_new(&unit->device, -1, NULL, THIS_MODULE,
258 sizeof(*dice), &card);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200259 if (err < 0)
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +0900260 goto end;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200261
262 dice = card->private_data;
263 dice->card = card;
Takashi Sakamoto12ed7192015-02-21 23:54:57 +0900264 dice->unit = fw_unit_get(unit);
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +0900265 card->private_free = dice_card_free;
266
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200267 spin_lock_init(&dice->lock);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200268 mutex_init(&dice->mutex);
Clemens Ladisch15a75c82011-12-04 22:23:59 +0100269 init_completion(&dice->clock_accepted);
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200270 init_waitqueue_head(&dice->hwdep_wait);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200271
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +0900272 err = snd_dice_transaction_init(dice);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200273 if (err < 0)
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +0900274 goto error;
Clemens Ladisch5ea40182011-12-05 22:09:42 +0100275
276 err = dice_read_params(dice);
277 if (err < 0)
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +0900278 goto error;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200279
280 dice_card_strings(dice);
281
Takashi Sakamotoc50fb912014-11-29 00:59:15 +0900282 err = snd_dice_create_pcm(dice);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200283 if (err < 0)
284 goto error;
285
Takashi Sakamoto19af57b2014-11-29 00:59:16 +0900286 err = snd_dice_create_hwdep(dice);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200287 if (err < 0)
288 goto error;
289
Takashi Sakamoto04d426a2014-11-29 00:59:17 +0900290 snd_dice_create_proc(dice);
Clemens Ladischc6144752012-01-05 22:36:08 +0100291
Takashi Sakamotoa113ff82014-12-09 00:10:39 +0900292 err = snd_dice_create_midi(dice);
293 if (err < 0)
294 goto error;
295
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900296 err = snd_dice_stream_init_duplex(dice);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200297 if (err < 0)
298 goto error;
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +0900299
300 err = snd_card_register(card);
301 if (err < 0) {
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900302 snd_dice_stream_destroy_duplex(dice);
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +0900303 goto error;
304 }
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200305
306 dev_set_drvdata(&unit->device, dice);
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +0900307end:
308 return err;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200309error:
310 snd_card_free(card);
311 return err;
312}
313
314static void dice_remove(struct fw_unit *unit)
315{
Takashi Sakamoto732d1532014-11-29 00:59:11 +0900316 struct snd_dice *dice = dev_get_drvdata(&unit->device);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200317
Takashi Sakamoto12ed7192015-02-21 23:54:57 +0900318 /* No need to wait for releasing card object in this context. */
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200319 snd_card_free_when_closed(dice->card);
320}
321
322static void dice_bus_reset(struct fw_unit *unit)
323{
Takashi Sakamoto732d1532014-11-29 00:59:11 +0900324 struct snd_dice *dice = dev_get_drvdata(&unit->device);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200325
Takashi Sakamoto7c2d4c02014-11-29 00:59:13 +0900326 /* The handler address register becomes initialized. */
327 snd_dice_transaction_reinit(dice);
328
Stefan Richtera8c558f2011-08-27 20:05:15 +0200329 mutex_lock(&dice->mutex);
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900330 snd_dice_stream_update_duplex(dice);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200331 mutex_unlock(&dice->mutex);
332}
333
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200334#define DICE_INTERFACE 0x000001
335
336static const struct ieee1394_device_id dice_id_table[] = {
337 {
Clemens Ladischcbab3282011-09-04 22:16:02 +0200338 .match_flags = IEEE1394_MATCH_VERSION,
339 .version = DICE_INTERFACE,
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200340 },
341 { }
342};
343MODULE_DEVICE_TABLE(ieee1394, dice_id_table);
344
345static struct fw_driver dice_driver = {
346 .driver = {
347 .owner = THIS_MODULE,
348 .name = KBUILD_MODNAME,
349 .bus = &fw_bus_type,
350 },
351 .probe = dice_probe,
352 .update = dice_bus_reset,
353 .remove = dice_remove,
354 .id_table = dice_id_table,
355};
356
357static int __init alsa_dice_init(void)
358{
359 return driver_register(&dice_driver.driver);
360}
361
362static void __exit alsa_dice_exit(void)
363{
364 driver_unregister(&dice_driver.driver);
365}
366
367module_init(alsa_dice_init);
368module_exit(alsa_dice_exit);