]> git.karo-electronics.de Git - karo-tx-linux.git/blob - sound/firewire/dice/dice.c
Merge remote-tracking branch 'edac-amd/for-next'
[karo-tx-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 #define PROBE_DELAY_MS          (2 * MSEC_PER_SEC)
22
23 static int check_dice_category(struct fw_unit *unit)
24 {
25         struct fw_device *device = fw_parent_device(unit);
26         struct fw_csr_iterator it;
27         int key, val, vendor = -1, model = -1;
28         unsigned int category;
29
30         /*
31          * Check that GUID and unit directory are constructed according to DICE
32          * rules, i.e., that the specifier ID is the GUID's OUI, and that the
33          * GUID chip ID consists of the 8-bit category ID, the 10-bit product
34          * ID, and a 22-bit serial number.
35          */
36         fw_csr_iterator_init(&it, unit->directory);
37         while (fw_csr_iterator_next(&it, &key, &val)) {
38                 switch (key) {
39                 case CSR_SPECIFIER_ID:
40                         vendor = val;
41                         break;
42                 case CSR_MODEL:
43                         model = val;
44                         break;
45                 }
46         }
47         if (vendor == OUI_WEISS)
48                 category = WEISS_CATEGORY_ID;
49         else if (vendor == OUI_LOUD)
50                 category = LOUD_CATEGORY_ID;
51         else
52                 category = DICE_CATEGORY_ID;
53         if (device->config_rom[3] != ((vendor << 8) | category) ||
54             device->config_rom[4] >> 22 != model)
55                 return -ENODEV;
56
57         return 0;
58 }
59
60 static int check_clock_caps(struct snd_dice *dice)
61 {
62         __be32 value;
63         int err;
64
65         /* some very old firmwares don't tell about their clock support */
66         if (dice->clock_caps > 0) {
67                 err = snd_dice_transaction_read_global(dice,
68                                                 GLOBAL_CLOCK_CAPABILITIES,
69                                                 &value, 4);
70                 if (err < 0)
71                         return err;
72                 dice->clock_caps = be32_to_cpu(value);
73         } else {
74                 /* this should be supported by any device */
75                 dice->clock_caps = CLOCK_CAP_RATE_44100 |
76                                    CLOCK_CAP_RATE_48000 |
77                                    CLOCK_CAP_SOURCE_ARX1 |
78                                    CLOCK_CAP_SOURCE_INTERNAL;
79         }
80
81         return 0;
82 }
83
84 static void dice_card_strings(struct snd_dice *dice)
85 {
86         struct snd_card *card = dice->card;
87         struct fw_device *dev = fw_parent_device(dice->unit);
88         char vendor[32], model[32];
89         unsigned int i;
90         int err;
91
92         strcpy(card->driver, "DICE");
93
94         strcpy(card->shortname, "DICE");
95         BUILD_BUG_ON(NICK_NAME_SIZE < sizeof(card->shortname));
96         err = snd_dice_transaction_read_global(dice, GLOBAL_NICK_NAME,
97                                                card->shortname,
98                                                sizeof(card->shortname));
99         if (err >= 0) {
100                 /* DICE strings are returned in "always-wrong" endianness */
101                 BUILD_BUG_ON(sizeof(card->shortname) % 4 != 0);
102                 for (i = 0; i < sizeof(card->shortname); i += 4)
103                         swab32s((u32 *)&card->shortname[i]);
104                 card->shortname[sizeof(card->shortname) - 1] = '\0';
105         }
106
107         strcpy(vendor, "?");
108         fw_csr_string(dev->config_rom + 5, CSR_VENDOR, vendor, sizeof(vendor));
109         strcpy(model, "?");
110         fw_csr_string(dice->unit->directory, CSR_MODEL, model, sizeof(model));
111         snprintf(card->longname, sizeof(card->longname),
112                  "%s %s (serial %u) at %s, S%d",
113                  vendor, model, dev->config_rom[4] & 0x3fffff,
114                  dev_name(&dice->unit->device), 100 << dev->max_speed);
115
116         strcpy(card->mixername, "DICE");
117 }
118
119 static void dice_free(struct snd_dice *dice)
120 {
121         snd_dice_stream_destroy_duplex(dice);
122         snd_dice_transaction_destroy(dice);
123         fw_unit_put(dice->unit);
124
125         mutex_destroy(&dice->mutex);
126         kfree(dice);
127 }
128
129 /*
130  * This module releases the FireWire unit data after all ALSA character devices
131  * are released by applications. This is for releasing stream data or finishing
132  * transactions safely. Thus at returning from .remove(), this module still keep
133  * references for the unit.
134  */
135 static void dice_card_free(struct snd_card *card)
136 {
137         dice_free(card->private_data);
138 }
139
140 static void do_registration(struct work_struct *work)
141 {
142         struct snd_dice *dice = container_of(work, struct snd_dice, dwork.work);
143         int err;
144
145         if (dice->registered)
146                 return;
147
148         err = snd_card_new(&dice->unit->device, -1, NULL, THIS_MODULE, 0,
149                            &dice->card);
150         if (err < 0)
151                 return;
152
153         err = snd_dice_transaction_init(dice);
154         if (err < 0)
155                 goto error;
156
157         err = check_clock_caps(dice);
158         if (err < 0)
159                 goto error;
160
161         dice_card_strings(dice);
162
163         snd_dice_create_proc(dice);
164
165         err = snd_dice_create_pcm(dice);
166         if (err < 0)
167                 goto error;
168
169         err = snd_dice_create_midi(dice);
170         if (err < 0)
171                 goto error;
172
173         err = snd_dice_create_hwdep(dice);
174         if (err < 0)
175                 goto error;
176
177         err = snd_card_register(dice->card);
178         if (err < 0)
179                 goto error;
180
181         /*
182          * After registered, dice instance can be released corresponding to
183          * releasing the sound card instance.
184          */
185         dice->card->private_free = dice_card_free;
186         dice->card->private_data = dice;
187         dice->registered = true;
188
189         return;
190 error:
191         snd_dice_transaction_destroy(dice);
192         snd_card_free(dice->card);
193         dev_info(&dice->unit->device,
194                  "Sound card registration failed: %d\n", err);
195 }
196
197 static void schedule_registration(struct snd_dice *dice)
198 {
199         struct fw_card *fw_card = fw_parent_device(dice->unit)->card;
200         u64 now, delay;
201
202         now = get_jiffies_64();
203         delay = fw_card->reset_jiffies + msecs_to_jiffies(PROBE_DELAY_MS);
204
205         if (time_after64(delay, now))
206                 delay -= now;
207         else
208                 delay = 0;
209
210         mod_delayed_work(system_wq, &dice->dwork, delay);
211 }
212
213 static int dice_probe(struct fw_unit *unit, const struct ieee1394_device_id *id)
214 {
215         struct snd_dice *dice;
216         int err;
217
218         err = check_dice_category(unit);
219         if (err < 0)
220                 return -ENODEV;
221
222         /* Allocate this independent of sound card instance. */
223         dice = kzalloc(sizeof(struct snd_dice), GFP_KERNEL);
224         if (dice == NULL)
225                 return -ENOMEM;
226
227         dice->unit = fw_unit_get(unit);
228         dev_set_drvdata(&unit->device, dice);
229
230         spin_lock_init(&dice->lock);
231         mutex_init(&dice->mutex);
232         init_completion(&dice->clock_accepted);
233         init_waitqueue_head(&dice->hwdep_wait);
234
235         err = snd_dice_stream_init_duplex(dice);
236         if (err < 0) {
237                 dice_free(dice);
238                 return err;
239         }
240
241         /* Allocate and register this sound card later. */
242         INIT_DEFERRABLE_WORK(&dice->dwork, do_registration);
243         schedule_registration(dice);
244
245         return 0;
246 }
247
248 static void dice_remove(struct fw_unit *unit)
249 {
250         struct snd_dice *dice = dev_get_drvdata(&unit->device);
251
252         /*
253          * Confirm to stop the work for registration before the sound card is
254          * going to be released. The work is not scheduled again because bus
255          * reset handler is not called anymore.
256          */
257         cancel_delayed_work_sync(&dice->dwork);
258
259         if (dice->registered) {
260                 /* No need to wait for releasing card object in this context. */
261                 snd_card_free_when_closed(dice->card);
262         } else {
263                 /* Don't forget this case. */
264                 dice_free(dice);
265         }
266 }
267
268 static void dice_bus_reset(struct fw_unit *unit)
269 {
270         struct snd_dice *dice = dev_get_drvdata(&unit->device);
271
272         /* Postpone a workqueue for deferred registration. */
273         if (!dice->registered)
274                 schedule_registration(dice);
275
276         /* The handler address register becomes initialized. */
277         snd_dice_transaction_reinit(dice);
278
279         /*
280          * After registration, userspace can start packet streaming, then this
281          * code block works fine.
282          */
283         if (dice->registered) {
284                 mutex_lock(&dice->mutex);
285                 snd_dice_stream_update_duplex(dice);
286                 mutex_unlock(&dice->mutex);
287         }
288 }
289
290 #define DICE_INTERFACE  0x000001
291
292 static const struct ieee1394_device_id dice_id_table[] = {
293         {
294                 .match_flags = IEEE1394_MATCH_VERSION,
295                 .version     = DICE_INTERFACE,
296         },
297         { }
298 };
299 MODULE_DEVICE_TABLE(ieee1394, dice_id_table);
300
301 static struct fw_driver dice_driver = {
302         .driver   = {
303                 .owner  = THIS_MODULE,
304                 .name   = KBUILD_MODNAME,
305                 .bus    = &fw_bus_type,
306         },
307         .probe    = dice_probe,
308         .update   = dice_bus_reset,
309         .remove   = dice_remove,
310         .id_table = dice_id_table,
311 };
312
313 static int __init alsa_dice_init(void)
314 {
315         return driver_register(&dice_driver.driver);
316 }
317
318 static void __exit alsa_dice_exit(void)
319 {
320         driver_unregister(&dice_driver.driver);
321 }
322
323 module_init(alsa_dice_init);
324 module_exit(alsa_dice_exit);