26271cc9e9d05c169909fba5a5da641399dcd794
[cascardo/linux.git] / sound / firewire / dice / dice.c
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
17 #define DICE_CATEGORY_ID        0x04
18 #define WEISS_CATEGORY_ID       0x00
19 #define LOUD_CATEGORY_ID        0x10
20
21 static int check_dice_category(struct fw_unit *unit)
22 {
23         struct fw_device *device = fw_parent_device(unit);
24         struct fw_csr_iterator it;
25         int key, val, vendor = -1, model = -1;
26         unsigned int category;
27
28         /*
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
31          * GUID chip ID consists of the 8-bit category ID, the 10-bit product
32          * ID, and a 22-bit serial number.
33          */
34         fw_csr_iterator_init(&it, unit->directory);
35         while (fw_csr_iterator_next(&it, &key, &val)) {
36                 switch (key) {
37                 case CSR_SPECIFIER_ID:
38                         vendor = val;
39                         break;
40                 case CSR_MODEL:
41                         model = val;
42                         break;
43                 }
44         }
45         if (vendor == OUI_WEISS)
46                 category = WEISS_CATEGORY_ID;
47         else if (vendor == OUI_LOUD)
48                 category = LOUD_CATEGORY_ID;
49         else
50                 category = DICE_CATEGORY_ID;
51         if (device->config_rom[3] != ((vendor << 8) | category) ||
52             device->config_rom[4] >> 22 != model)
53                 return -ENODEV;
54
55         return 0;
56 }
57
58 static int highest_supported_mode_rate(struct snd_dice *dice,
59                                        unsigned int mode, unsigned int *rate)
60 {
61         unsigned int i, m;
62
63         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;
72
73         return 0;
74 }
75
76 static int dice_read_mode_params(struct snd_dice *dice, unsigned int mode)
77 {
78         __be32 values[2];
79         unsigned int rate;
80         int err;
81
82         if (highest_supported_mode_rate(dice, mode, &rate) < 0) {
83                 dice->tx_channels[mode] = 0;
84                 dice->tx_midi_ports[mode] = 0;
85                 dice->rx_channels[mode] = 0;
86                 dice->rx_midi_ports[mode] = 0;
87                 return 0;
88         }
89
90         err = snd_dice_transaction_set_rate(dice, rate);
91         if (err < 0)
92                 return err;
93
94         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
102         err = snd_dice_transaction_read_rx(dice, RX_NUMBER_AUDIO,
103                                            values, sizeof(values));
104         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
113 static int dice_read_params(struct snd_dice *dice)
114 {
115         __be32 value;
116         int mode, err;
117
118         /* some very old firmwares don't tell about their clock support */
119         if (dice->clock_caps > 0) {
120                 err = snd_dice_transaction_read_global(dice,
121                                                 GLOBAL_CLOCK_CAPABILITIES,
122                                                 &value, 4);
123                 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
134         for (mode = 2; mode >= 0; --mode) {
135                 err = dice_read_mode_params(dice, mode);
136                 if (err < 0)
137                         return err;
138         }
139
140         return 0;
141 }
142
143 static void dice_card_strings(struct snd_dice *dice)
144 {
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));
155         err = snd_dice_transaction_read_global(dice, GLOBAL_NICK_NAME,
156                                                card->shortname,
157                                                sizeof(card->shortname));
158         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),
171                  "%s %s (serial %u) at %s, S%d",
172                  vendor, model, dev->config_rom[4] & 0x3fffff,
173                  dev_name(&dice->unit->device), 100 << dev->max_speed);
174
175         strcpy(card->mixername, "DICE");
176 }
177
178 /*
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  */
184 static void dice_card_free(struct snd_card *card)
185 {
186         struct snd_dice *dice = card->private_data;
187
188         snd_dice_stream_destroy_duplex(dice);
189         snd_dice_transaction_destroy(dice);
190         fw_unit_put(dice->unit);
191
192         mutex_destroy(&dice->mutex);
193 }
194
195 static int dice_probe(struct fw_unit *unit, const struct ieee1394_device_id *id)
196 {
197         struct snd_card *card;
198         struct snd_dice *dice;
199         int err;
200
201         err = check_dice_category(unit);
202         if (err < 0)
203                 return -ENODEV;
204
205         err = snd_card_new(&unit->device, -1, NULL, THIS_MODULE,
206                            sizeof(*dice), &card);
207         if (err < 0)
208                 goto end;
209
210         dice = card->private_data;
211         dice->card = card;
212         dice->unit = fw_unit_get(unit);
213         card->private_free = dice_card_free;
214
215         spin_lock_init(&dice->lock);
216         mutex_init(&dice->mutex);
217         init_completion(&dice->clock_accepted);
218         init_waitqueue_head(&dice->hwdep_wait);
219
220         err = snd_dice_transaction_init(dice);
221         if (err < 0)
222                 goto error;
223
224         err = dice_read_params(dice);
225         if (err < 0)
226                 goto error;
227
228         dice_card_strings(dice);
229
230         err = snd_dice_create_pcm(dice);
231         if (err < 0)
232                 goto error;
233
234         err = snd_dice_create_hwdep(dice);
235         if (err < 0)
236                 goto error;
237
238         snd_dice_create_proc(dice);
239
240         err = snd_dice_create_midi(dice);
241         if (err < 0)
242                 goto error;
243
244         err = snd_dice_stream_init_duplex(dice);
245         if (err < 0)
246                 goto error;
247
248         err = snd_card_register(card);
249         if (err < 0) {
250                 snd_dice_stream_destroy_duplex(dice);
251                 goto error;
252         }
253
254         dev_set_drvdata(&unit->device, dice);
255 end:
256         return err;
257 error:
258         snd_card_free(card);
259         return err;
260 }
261
262 static void dice_remove(struct fw_unit *unit)
263 {
264         struct snd_dice *dice = dev_get_drvdata(&unit->device);
265
266         /* No need to wait for releasing card object in this context. */
267         snd_card_free_when_closed(dice->card);
268 }
269
270 static void dice_bus_reset(struct fw_unit *unit)
271 {
272         struct snd_dice *dice = dev_get_drvdata(&unit->device);
273
274         /* The handler address register becomes initialized. */
275         snd_dice_transaction_reinit(dice);
276
277         mutex_lock(&dice->mutex);
278         snd_dice_stream_update_duplex(dice);
279         mutex_unlock(&dice->mutex);
280 }
281
282 #define DICE_INTERFACE  0x000001
283
284 static const struct ieee1394_device_id dice_id_table[] = {
285         {
286                 .match_flags = IEEE1394_MATCH_VERSION,
287                 .version     = DICE_INTERFACE,
288         },
289         { }
290 };
291 MODULE_DEVICE_TABLE(ieee1394, dice_id_table);
292
293 static 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
305 static int __init alsa_dice_init(void)
306 {
307         return driver_register(&dice_driver.driver);
308 }
309
310 static void __exit alsa_dice_exit(void)
311 {
312         driver_unregister(&dice_driver.driver);
313 }
314
315 module_init(alsa_dice_init);
316 module_exit(alsa_dice_exit);