GNU Linux-libre 4.19.207-gnu1
[releases.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 #define OUI_FOCUSRITE           0x00130e
17 #define OUI_TCELECTRONIC        0x000166
18 #define OUI_ALESIS              0x000595
19 #define OUI_MAUDIO              0x000d6c
20 #define OUI_MYTEK               0x001ee8
21 #define OUI_SSL                 0x0050c2        // Actually ID reserved by IEEE.
22
23 #define DICE_CATEGORY_ID        0x04
24 #define WEISS_CATEGORY_ID       0x00
25 #define LOUD_CATEGORY_ID        0x10
26
27 #define MODEL_ALESIS_IO_BOTH    0x000001
28
29 static int check_dice_category(struct fw_unit *unit)
30 {
31         struct fw_device *device = fw_parent_device(unit);
32         struct fw_csr_iterator it;
33         int key, val, vendor = -1, model = -1;
34         unsigned int category;
35
36         /*
37          * Check that GUID and unit directory are constructed according to DICE
38          * rules, i.e., that the specifier ID is the GUID's OUI, and that the
39          * GUID chip ID consists of the 8-bit category ID, the 10-bit product
40          * ID, and a 22-bit serial number.
41          */
42         fw_csr_iterator_init(&it, unit->directory);
43         while (fw_csr_iterator_next(&it, &key, &val)) {
44                 switch (key) {
45                 case CSR_SPECIFIER_ID:
46                         vendor = val;
47                         break;
48                 case CSR_MODEL:
49                         model = val;
50                         break;
51                 }
52         }
53
54         if (vendor == OUI_WEISS)
55                 category = WEISS_CATEGORY_ID;
56         else if (vendor == OUI_LOUD)
57                 category = LOUD_CATEGORY_ID;
58         else
59                 category = DICE_CATEGORY_ID;
60         if (device->config_rom[3] != ((vendor << 8) | category) ||
61             device->config_rom[4] >> 22 != model)
62                 return -ENODEV;
63
64         return 0;
65 }
66
67 static int check_clock_caps(struct snd_dice *dice)
68 {
69         __be32 value;
70         int err;
71
72         /* some very old firmwares don't tell about their clock support */
73         if (dice->clock_caps > 0) {
74                 err = snd_dice_transaction_read_global(dice,
75                                                 GLOBAL_CLOCK_CAPABILITIES,
76                                                 &value, 4);
77                 if (err < 0)
78                         return err;
79                 dice->clock_caps = be32_to_cpu(value);
80         } else {
81                 /* this should be supported by any device */
82                 dice->clock_caps = CLOCK_CAP_RATE_44100 |
83                                    CLOCK_CAP_RATE_48000 |
84                                    CLOCK_CAP_SOURCE_ARX1 |
85                                    CLOCK_CAP_SOURCE_INTERNAL;
86         }
87
88         return 0;
89 }
90
91 static void dice_card_strings(struct snd_dice *dice)
92 {
93         struct snd_card *card = dice->card;
94         struct fw_device *dev = fw_parent_device(dice->unit);
95         char vendor[32], model[32];
96         unsigned int i;
97         int err;
98
99         strcpy(card->driver, "DICE");
100
101         strcpy(card->shortname, "DICE");
102         BUILD_BUG_ON(NICK_NAME_SIZE < sizeof(card->shortname));
103         err = snd_dice_transaction_read_global(dice, GLOBAL_NICK_NAME,
104                                                card->shortname,
105                                                sizeof(card->shortname));
106         if (err >= 0) {
107                 /* DICE strings are returned in "always-wrong" endianness */
108                 BUILD_BUG_ON(sizeof(card->shortname) % 4 != 0);
109                 for (i = 0; i < sizeof(card->shortname); i += 4)
110                         swab32s((u32 *)&card->shortname[i]);
111                 card->shortname[sizeof(card->shortname) - 1] = '\0';
112         }
113
114         strcpy(vendor, "?");
115         fw_csr_string(dev->config_rom + 5, CSR_VENDOR, vendor, sizeof(vendor));
116         strcpy(model, "?");
117         fw_csr_string(dice->unit->directory, CSR_MODEL, model, sizeof(model));
118         snprintf(card->longname, sizeof(card->longname),
119                  "%s %s (serial %u) at %s, S%d",
120                  vendor, model, dev->config_rom[4] & 0x3fffff,
121                  dev_name(&dice->unit->device), 100 << dev->max_speed);
122
123         strcpy(card->mixername, "DICE");
124 }
125
126 static void dice_free(struct snd_dice *dice)
127 {
128         snd_dice_stream_destroy_duplex(dice);
129         snd_dice_transaction_destroy(dice);
130         fw_unit_put(dice->unit);
131
132         mutex_destroy(&dice->mutex);
133         kfree(dice);
134 }
135
136 /*
137  * This module releases the FireWire unit data after all ALSA character devices
138  * are released by applications. This is for releasing stream data or finishing
139  * transactions safely. Thus at returning from .remove(), this module still keep
140  * references for the unit.
141  */
142 static void dice_card_free(struct snd_card *card)
143 {
144         dice_free(card->private_data);
145 }
146
147 static void do_registration(struct work_struct *work)
148 {
149         struct snd_dice *dice = container_of(work, struct snd_dice, dwork.work);
150         int err;
151
152         if (dice->registered)
153                 return;
154
155         err = snd_card_new(&dice->unit->device, -1, NULL, THIS_MODULE, 0,
156                            &dice->card);
157         if (err < 0)
158                 return;
159
160         err = snd_dice_transaction_init(dice);
161         if (err < 0)
162                 goto error;
163
164         err = check_clock_caps(dice);
165         if (err < 0)
166                 goto error;
167
168         dice_card_strings(dice);
169
170         err = dice->detect_formats(dice);
171         if (err < 0)
172                 goto error;
173
174         err = snd_dice_stream_init_duplex(dice);
175         if (err < 0)
176                 goto error;
177
178         snd_dice_create_proc(dice);
179
180         err = snd_dice_create_pcm(dice);
181         if (err < 0)
182                 goto error;
183
184         err = snd_dice_create_midi(dice);
185         if (err < 0)
186                 goto error;
187
188         err = snd_dice_create_hwdep(dice);
189         if (err < 0)
190                 goto error;
191
192         err = snd_card_register(dice->card);
193         if (err < 0)
194                 goto error;
195
196         /*
197          * After registered, dice instance can be released corresponding to
198          * releasing the sound card instance.
199          */
200         dice->card->private_free = dice_card_free;
201         dice->card->private_data = dice;
202         dice->registered = true;
203
204         return;
205 error:
206         snd_dice_stream_destroy_duplex(dice);
207         snd_dice_transaction_destroy(dice);
208         snd_dice_stream_destroy_duplex(dice);
209         snd_card_free(dice->card);
210         dev_info(&dice->unit->device,
211                  "Sound card registration failed: %d\n", err);
212 }
213
214 static int dice_probe(struct fw_unit *unit,
215                       const struct ieee1394_device_id *entry)
216 {
217         struct snd_dice *dice;
218         int err;
219
220         if (!entry->driver_data && entry->vendor_id != OUI_SSL) {
221                 err = check_dice_category(unit);
222                 if (err < 0)
223                         return -ENODEV;
224         }
225
226         /* Allocate this independent of sound card instance. */
227         dice = kzalloc(sizeof(struct snd_dice), GFP_KERNEL);
228         if (dice == NULL)
229                 return -ENOMEM;
230
231         dice->unit = fw_unit_get(unit);
232         dev_set_drvdata(&unit->device, dice);
233
234         if (!entry->driver_data) {
235                 dice->detect_formats = snd_dice_stream_detect_current_formats;
236         } else {
237                 dice->detect_formats =
238                                 (snd_dice_detect_formats_t)entry->driver_data;
239         }
240
241         spin_lock_init(&dice->lock);
242         mutex_init(&dice->mutex);
243         init_completion(&dice->clock_accepted);
244         init_waitqueue_head(&dice->hwdep_wait);
245
246         /* Allocate and register this sound card later. */
247         INIT_DEFERRABLE_WORK(&dice->dwork, do_registration);
248         snd_fw_schedule_registration(unit, &dice->dwork);
249
250         return 0;
251 }
252
253 static void dice_remove(struct fw_unit *unit)
254 {
255         struct snd_dice *dice = dev_get_drvdata(&unit->device);
256
257         /*
258          * Confirm to stop the work for registration before the sound card is
259          * going to be released. The work is not scheduled again because bus
260          * reset handler is not called anymore.
261          */
262         cancel_delayed_work_sync(&dice->dwork);
263
264         if (dice->registered) {
265                 /* No need to wait for releasing card object in this context. */
266                 snd_card_free_when_closed(dice->card);
267         } else {
268                 /* Don't forget this case. */
269                 dice_free(dice);
270         }
271 }
272
273 static void dice_bus_reset(struct fw_unit *unit)
274 {
275         struct snd_dice *dice = dev_get_drvdata(&unit->device);
276
277         /* Postpone a workqueue for deferred registration. */
278         if (!dice->registered)
279                 snd_fw_schedule_registration(unit, &dice->dwork);
280
281         /* The handler address register becomes initialized. */
282         snd_dice_transaction_reinit(dice);
283
284         /*
285          * After registration, userspace can start packet streaming, then this
286          * code block works fine.
287          */
288         if (dice->registered) {
289                 mutex_lock(&dice->mutex);
290                 snd_dice_stream_update_duplex(dice);
291                 mutex_unlock(&dice->mutex);
292         }
293 }
294
295 #define DICE_INTERFACE  0x000001
296
297 static const struct ieee1394_device_id dice_id_table[] = {
298         /* M-Audio Profire 2626 has a different value in version field. */
299         {
300                 .match_flags    = IEEE1394_MATCH_VENDOR_ID |
301                                   IEEE1394_MATCH_MODEL_ID,
302                 .vendor_id      = OUI_MAUDIO,
303                 .model_id       = 0x000010,
304                 .driver_data = (kernel_ulong_t)snd_dice_detect_extension_formats,
305         },
306         /* M-Audio Profire 610 has a different value in version field. */
307         {
308                 .match_flags    = IEEE1394_MATCH_VENDOR_ID |
309                                   IEEE1394_MATCH_MODEL_ID,
310                 .vendor_id      = OUI_MAUDIO,
311                 .model_id       = 0x000011,
312                 .driver_data = (kernel_ulong_t)snd_dice_detect_extension_formats,
313         },
314         /* TC Electronic Konnekt 24D. */
315         {
316                 .match_flags    = IEEE1394_MATCH_VENDOR_ID |
317                                   IEEE1394_MATCH_MODEL_ID,
318                 .vendor_id      = OUI_TCELECTRONIC,
319                 .model_id       = 0x000020,
320                 .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
321         },
322         /* TC Electronic Konnekt 8. */
323         {
324                 .match_flags    = IEEE1394_MATCH_VENDOR_ID |
325                                   IEEE1394_MATCH_MODEL_ID,
326                 .vendor_id      = OUI_TCELECTRONIC,
327                 .model_id       = 0x000021,
328                 .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
329         },
330         /* TC Electronic Studio Konnekt 48. */
331         {
332                 .match_flags    = IEEE1394_MATCH_VENDOR_ID |
333                                   IEEE1394_MATCH_MODEL_ID,
334                 .vendor_id      = OUI_TCELECTRONIC,
335                 .model_id       = 0x000022,
336                 .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
337         },
338         /* TC Electronic Konnekt Live. */
339         {
340                 .match_flags    = IEEE1394_MATCH_VENDOR_ID |
341                                   IEEE1394_MATCH_MODEL_ID,
342                 .vendor_id      = OUI_TCELECTRONIC,
343                 .model_id       = 0x000023,
344                 .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
345         },
346         /* TC Electronic Desktop Konnekt 6. */
347         {
348                 .match_flags    = IEEE1394_MATCH_VENDOR_ID |
349                                   IEEE1394_MATCH_MODEL_ID,
350                 .vendor_id      = OUI_TCELECTRONIC,
351                 .model_id       = 0x000024,
352                 .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
353         },
354         /* TC Electronic Impact Twin. */
355         {
356                 .match_flags    = IEEE1394_MATCH_VENDOR_ID |
357                                   IEEE1394_MATCH_MODEL_ID,
358                 .vendor_id      = OUI_TCELECTRONIC,
359                 .model_id       = 0x000027,
360                 .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
361         },
362         /* TC Electronic Digital Konnekt x32. */
363         {
364                 .match_flags    = IEEE1394_MATCH_VENDOR_ID |
365                                   IEEE1394_MATCH_MODEL_ID,
366                 .vendor_id      = OUI_TCELECTRONIC,
367                 .model_id       = 0x000030,
368                 .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
369         },
370         /* Alesis iO14/iO26. */
371         {
372                 .match_flags    = IEEE1394_MATCH_VENDOR_ID |
373                                   IEEE1394_MATCH_MODEL_ID,
374                 .vendor_id      = OUI_ALESIS,
375                 .model_id       = MODEL_ALESIS_IO_BOTH,
376                 .driver_data = (kernel_ulong_t)snd_dice_detect_alesis_formats,
377         },
378         /* Mytek Stereo 192 DSD-DAC. */
379         {
380                 .match_flags    = IEEE1394_MATCH_VENDOR_ID |
381                                   IEEE1394_MATCH_MODEL_ID,
382                 .vendor_id      = OUI_MYTEK,
383                 .model_id       = 0x000002,
384                 .driver_data = (kernel_ulong_t)snd_dice_detect_mytek_formats,
385         },
386         // Solid State Logic, Duende Classic and Mini.
387         // NOTE: each field of GUID in config ROM is not compliant to standard
388         // DICE scheme.
389         {
390                 .match_flags    = IEEE1394_MATCH_VENDOR_ID |
391                                   IEEE1394_MATCH_MODEL_ID,
392                 .vendor_id      = OUI_SSL,
393                 .model_id       = 0x000070,
394         },
395         {
396                 .match_flags = IEEE1394_MATCH_VERSION,
397                 .version     = DICE_INTERFACE,
398         },
399         { }
400 };
401 MODULE_DEVICE_TABLE(ieee1394, dice_id_table);
402
403 static struct fw_driver dice_driver = {
404         .driver   = {
405                 .owner  = THIS_MODULE,
406                 .name   = KBUILD_MODNAME,
407                 .bus    = &fw_bus_type,
408         },
409         .probe    = dice_probe,
410         .update   = dice_bus_reset,
411         .remove   = dice_remove,
412         .id_table = dice_id_table,
413 };
414
415 static int __init alsa_dice_init(void)
416 {
417         return driver_register(&dice_driver.driver);
418 }
419
420 static void __exit alsa_dice_exit(void)
421 {
422         driver_unregister(&dice_driver.driver);
423 }
424
425 module_init(alsa_dice_init);
426 module_exit(alsa_dice_exit);