blob: 26271cc9e9d05c169909fba5a5da641399dcd794 [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 Sakamoto7c2d4c0c2014-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
Takashi Sakamoto5d5563b2015-11-14 16:42:04 +090015#define OUI_LOUD 0x000ff2
Clemens Ladischa471fcd2012-02-13 21:55:13 +010016
17#define DICE_CATEGORY_ID 0x04
18#define WEISS_CATEGORY_ID 0x00
Takashi Sakamoto5d5563b2015-11-14 16:42:04 +090019#define LOUD_CATEGORY_ID 0x10
Clemens Ladischcbab3282011-09-04 22:16:02 +020020
Takashi Sakamoto4a47a872015-12-31 13:58:11 +090021static int check_dice_category(struct fw_unit *unit)
Clemens Ladischcbab3282011-09-04 22:16:02 +020022{
Clemens Ladischcbab3282011-09-04 22:16:02 +020023 struct fw_device *device = fw_parent_device(unit);
24 struct fw_csr_iterator it;
Takashi Sakamoto4a47a872015-12-31 13:58:11 +090025 int key, val, vendor = -1, model = -1;
26 unsigned int category;
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +090027
Clemens Ladischcbab3282011-09-04 22:16:02 +020028 /*
29 * Check that GUID and unit directory are constructed according to DICE
30 * rules, i.e., that the specifier ID is the GUID's OUI, and that the
Clemens Ladischa471fcd2012-02-13 21:55:13 +010031 * GUID chip ID consists of the 8-bit category ID, the 10-bit product
32 * ID, and a 22-bit serial number.
Clemens Ladischcbab3282011-09-04 22:16:02 +020033 */
34 fw_csr_iterator_init(&it, unit->directory);
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +090035 while (fw_csr_iterator_next(&it, &key, &val)) {
Clemens Ladischcbab3282011-09-04 22:16:02 +020036 switch (key) {
37 case CSR_SPECIFIER_ID:
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +090038 vendor = val;
Clemens Ladischcbab3282011-09-04 22:16:02 +020039 break;
40 case CSR_MODEL:
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +090041 model = val;
Clemens Ladischcbab3282011-09-04 22:16:02 +020042 break;
43 }
44 }
Clemens Ladischa471fcd2012-02-13 21:55:13 +010045 if (vendor == OUI_WEISS)
46 category = WEISS_CATEGORY_ID;
Takashi Sakamoto5d5563b2015-11-14 16:42:04 +090047 else if (vendor == OUI_LOUD)
48 category = LOUD_CATEGORY_ID;
Clemens Ladischa471fcd2012-02-13 21:55:13 +010049 else
50 category = DICE_CATEGORY_ID;
51 if (device->config_rom[3] != ((vendor << 8) | category) ||
Takashi Sakamoto4a47a872015-12-31 13:58:11 +090052 device->config_rom[4] >> 22 != model)
53 return -ENODEV;
Clemens Ladischcbab3282011-09-04 22:16:02 +020054
Takashi Sakamoto4a47a872015-12-31 13:58:11 +090055 return 0;
Clemens Ladischcbab3282011-09-04 22:16:02 +020056}
57
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +090058static int highest_supported_mode_rate(struct snd_dice *dice,
59 unsigned int mode, unsigned int *rate)
Clemens Ladisch15a75c82011-12-04 22:23:59 +010060{
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +090061 unsigned int i, m;
Clemens Ladisch15a75c82011-12-04 22:23:59 +010062
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +090063 for (i = ARRAY_SIZE(snd_dice_rates); i > 0; i--) {
64 *rate = snd_dice_rates[i - 1];
65 if (snd_dice_stream_get_rate_mode(dice, *rate, &m) < 0)
66 continue;
67 if (mode == m)
68 break;
69 }
70 if (i == 0)
71 return -EINVAL;
Clemens Ladisch15a75c82011-12-04 22:23:59 +010072
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +090073 return 0;
Clemens Ladisch15a75c82011-12-04 22:23:59 +010074}
75
Takashi Sakamoto732d1532014-11-29 00:59:11 +090076static int dice_read_mode_params(struct snd_dice *dice, unsigned int mode)
Clemens Ladisch15a75c82011-12-04 22:23:59 +010077{
78 __be32 values[2];
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +090079 unsigned int rate;
80 int err;
Clemens Ladisch15a75c82011-12-04 22:23:59 +010081
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +090082 if (highest_supported_mode_rate(dice, mode, &rate) < 0) {
Takashi Sakamoto9a028432014-12-09 00:10:36 +090083 dice->tx_channels[mode] = 0;
84 dice->tx_midi_ports[mode] = 0;
Clemens Ladisch15a75c82011-12-04 22:23:59 +010085 dice->rx_channels[mode] = 0;
86 dice->rx_midi_ports[mode] = 0;
87 return 0;
88 }
89
Takashi Sakamoto6eb6c812014-11-29 00:59:14 +090090 err = snd_dice_transaction_set_rate(dice, rate);
Clemens Ladisch15a75c82011-12-04 22:23:59 +010091 if (err < 0)
92 return err;
93
Takashi Sakamoto9a028432014-12-09 00:10:36 +090094 err = snd_dice_transaction_read_tx(dice, TX_NUMBER_AUDIO,
95 values, sizeof(values));
96 if (err < 0)
97 return err;
98
99 dice->tx_channels[mode] = be32_to_cpu(values[0]);
100 dice->tx_midi_ports[mode] = be32_to_cpu(values[1]);
101
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900102 err = snd_dice_transaction_read_rx(dice, RX_NUMBER_AUDIO,
103 values, sizeof(values));
Clemens Ladisch15a75c82011-12-04 22:23:59 +0100104 if (err < 0)
105 return err;
106
107 dice->rx_channels[mode] = be32_to_cpu(values[0]);
108 dice->rx_midi_ports[mode] = be32_to_cpu(values[1]);
109
110 return 0;
111}
112
Takashi Sakamoto732d1532014-11-29 00:59:11 +0900113static int dice_read_params(struct snd_dice *dice)
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200114{
Clemens Ladischa0301992011-12-04 21:47:00 +0100115 __be32 value;
Clemens Ladisch15a75c82011-12-04 22:23:59 +0100116 int mode, err;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200117
Clemens Ladischa0301992011-12-04 21:47:00 +0100118 /* some very old firmwares don't tell about their clock support */
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900119 if (dice->clock_caps > 0) {
120 err = snd_dice_transaction_read_global(dice,
121 GLOBAL_CLOCK_CAPABILITIES,
122 &value, 4);
Clemens Ladischa0301992011-12-04 21:47:00 +0100123 if (err < 0)
124 return err;
125 dice->clock_caps = be32_to_cpu(value);
126 } else {
127 /* this should be supported by any device */
128 dice->clock_caps = CLOCK_CAP_RATE_44100 |
129 CLOCK_CAP_RATE_48000 |
130 CLOCK_CAP_SOURCE_ARX1 |
131 CLOCK_CAP_SOURCE_INTERNAL;
132 }
133
Clemens Ladisch15a75c82011-12-04 22:23:59 +0100134 for (mode = 2; mode >= 0; --mode) {
135 err = dice_read_mode_params(dice, mode);
136 if (err < 0)
137 return err;
138 }
139
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200140 return 0;
141}
142
Takashi Sakamoto732d1532014-11-29 00:59:11 +0900143static void dice_card_strings(struct snd_dice *dice)
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200144{
145 struct snd_card *card = dice->card;
146 struct fw_device *dev = fw_parent_device(dice->unit);
147 char vendor[32], model[32];
148 unsigned int i;
149 int err;
150
151 strcpy(card->driver, "DICE");
152
153 strcpy(card->shortname, "DICE");
154 BUILD_BUG_ON(NICK_NAME_SIZE < sizeof(card->shortname));
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900155 err = snd_dice_transaction_read_global(dice, GLOBAL_NICK_NAME,
156 card->shortname,
157 sizeof(card->shortname));
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200158 if (err >= 0) {
159 /* DICE strings are returned in "always-wrong" endianness */
160 BUILD_BUG_ON(sizeof(card->shortname) % 4 != 0);
161 for (i = 0; i < sizeof(card->shortname); i += 4)
162 swab32s((u32 *)&card->shortname[i]);
163 card->shortname[sizeof(card->shortname) - 1] = '\0';
164 }
165
166 strcpy(vendor, "?");
167 fw_csr_string(dev->config_rom + 5, CSR_VENDOR, vendor, sizeof(vendor));
168 strcpy(model, "?");
169 fw_csr_string(dice->unit->directory, CSR_MODEL, model, sizeof(model));
170 snprintf(card->longname, sizeof(card->longname),
Clemens Ladischcbab3282011-09-04 22:16:02 +0200171 "%s %s (serial %u) at %s, S%d",
172 vendor, model, dev->config_rom[4] & 0x3fffff,
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200173 dev_name(&dice->unit->device), 100 << dev->max_speed);
174
175 strcpy(card->mixername, "DICE");
176}
177
Takashi Sakamoto12ed7192015-02-21 23:54:57 +0900178/*
179 * This module releases the FireWire unit data after all ALSA character devices
180 * are released by applications. This is for releasing stream data or finishing
181 * transactions safely. Thus at returning from .remove(), this module still keep
182 * references for the unit.
183 */
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900184static void dice_card_free(struct snd_card *card)
185{
186 struct snd_dice *dice = card->private_data;
187
Takashi Sakamotodec84312015-02-21 23:55:00 +0900188 snd_dice_stream_destroy_duplex(dice);
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900189 snd_dice_transaction_destroy(dice);
Takashi Sakamoto12ed7192015-02-21 23:54:57 +0900190 fw_unit_put(dice->unit);
191
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900192 mutex_destroy(&dice->mutex);
193}
194
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200195static int dice_probe(struct fw_unit *unit, const struct ieee1394_device_id *id)
196{
197 struct snd_card *card;
Takashi Sakamoto732d1532014-11-29 00:59:11 +0900198 struct snd_dice *dice;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200199 int err;
200
Takashi Sakamoto4a47a872015-12-31 13:58:11 +0900201 err = check_dice_category(unit);
Clemens Ladischcbab3282011-09-04 22:16:02 +0200202 if (err < 0)
Takashi Sakamoto4a47a872015-12-31 13:58:11 +0900203 return -ENODEV;
Clemens Ladischcbab3282011-09-04 22:16:02 +0200204
Takashi Iwai06b45f02014-01-29 14:23:55 +0100205 err = snd_card_new(&unit->device, -1, NULL, THIS_MODULE,
206 sizeof(*dice), &card);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200207 if (err < 0)
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900208 goto end;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200209
210 dice = card->private_data;
211 dice->card = card;
Takashi Sakamoto12ed7192015-02-21 23:54:57 +0900212 dice->unit = fw_unit_get(unit);
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900213 card->private_free = dice_card_free;
214
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200215 spin_lock_init(&dice->lock);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200216 mutex_init(&dice->mutex);
Clemens Ladisch15a75c82011-12-04 22:23:59 +0100217 init_completion(&dice->clock_accepted);
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200218 init_waitqueue_head(&dice->hwdep_wait);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200219
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900220 err = snd_dice_transaction_init(dice);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200221 if (err < 0)
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900222 goto error;
Clemens Ladisch5ea40182011-12-05 22:09:42 +0100223
224 err = dice_read_params(dice);
225 if (err < 0)
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900226 goto error;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200227
228 dice_card_strings(dice);
229
Takashi Sakamotoc50fb912014-11-29 00:59:15 +0900230 err = snd_dice_create_pcm(dice);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200231 if (err < 0)
232 goto error;
233
Takashi Sakamoto19af57b2014-11-29 00:59:16 +0900234 err = snd_dice_create_hwdep(dice);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200235 if (err < 0)
236 goto error;
237
Takashi Sakamoto04d426a2014-11-29 00:59:17 +0900238 snd_dice_create_proc(dice);
Clemens Ladischc6144752012-01-05 22:36:08 +0100239
Takashi Sakamotoa113ff82014-12-09 00:10:39 +0900240 err = snd_dice_create_midi(dice);
241 if (err < 0)
242 goto error;
243
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900244 err = snd_dice_stream_init_duplex(dice);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200245 if (err < 0)
246 goto error;
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900247
248 err = snd_card_register(card);
249 if (err < 0) {
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900250 snd_dice_stream_destroy_duplex(dice);
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900251 goto error;
252 }
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200253
254 dev_set_drvdata(&unit->device, dice);
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900255end:
256 return err;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200257error:
258 snd_card_free(card);
259 return err;
260}
261
262static void dice_remove(struct fw_unit *unit)
263{
Takashi Sakamoto732d1532014-11-29 00:59:11 +0900264 struct snd_dice *dice = dev_get_drvdata(&unit->device);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200265
Takashi Sakamoto12ed7192015-02-21 23:54:57 +0900266 /* No need to wait for releasing card object in this context. */
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200267 snd_card_free_when_closed(dice->card);
268}
269
270static void dice_bus_reset(struct fw_unit *unit)
271{
Takashi Sakamoto732d1532014-11-29 00:59:11 +0900272 struct snd_dice *dice = dev_get_drvdata(&unit->device);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200273
Takashi Sakamoto7c2d4c0c2014-11-29 00:59:13 +0900274 /* The handler address register becomes initialized. */
275 snd_dice_transaction_reinit(dice);
276
Stefan Richtera8c558f2011-08-27 20:05:15 +0200277 mutex_lock(&dice->mutex);
Takashi Sakamoto9a028432014-12-09 00:10:36 +0900278 snd_dice_stream_update_duplex(dice);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200279 mutex_unlock(&dice->mutex);
280}
281
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200282#define DICE_INTERFACE 0x000001
283
284static const struct ieee1394_device_id dice_id_table[] = {
285 {
Clemens Ladischcbab3282011-09-04 22:16:02 +0200286 .match_flags = IEEE1394_MATCH_VERSION,
287 .version = DICE_INTERFACE,
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200288 },
289 { }
290};
291MODULE_DEVICE_TABLE(ieee1394, dice_id_table);
292
293static struct fw_driver dice_driver = {
294 .driver = {
295 .owner = THIS_MODULE,
296 .name = KBUILD_MODNAME,
297 .bus = &fw_bus_type,
298 },
299 .probe = dice_probe,
300 .update = dice_bus_reset,
301 .remove = dice_remove,
302 .id_table = dice_id_table,
303};
304
305static int __init alsa_dice_init(void)
306{
307 return driver_register(&dice_driver.driver);
308}
309
310static void __exit alsa_dice_exit(void)
311{
312 driver_unregister(&dice_driver.driver);
313}
314
315module_init(alsa_dice_init);
316module_exit(alsa_dice_exit);