]> Git Repo - linux.git/blob - sound/firewire/dice/dice.c
dm bufio: fix deadlock with loop device
[linux.git] / sound / firewire / dice / dice.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * TC Applied Technologies Digital Interface Communications Engine driver
4  *
5  * Copyright (c) Clemens Ladisch <[email protected]>
6  */
7
8 #include "dice.h"
9
10 MODULE_DESCRIPTION("DICE driver");
11 MODULE_AUTHOR("Clemens Ladisch <[email protected]>");
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_card_free(struct snd_card *card)
127 {
128         struct snd_dice *dice = card->private_data;
129
130         snd_dice_stream_destroy_duplex(dice);
131         snd_dice_transaction_destroy(dice);
132 }
133
134 static void do_registration(struct work_struct *work)
135 {
136         struct snd_dice *dice = container_of(work, struct snd_dice, dwork.work);
137         int err;
138
139         if (dice->registered)
140                 return;
141
142         err = snd_card_new(&dice->unit->device, -1, NULL, THIS_MODULE, 0,
143                            &dice->card);
144         if (err < 0)
145                 return;
146         dice->card->private_free = dice_card_free;
147         dice->card->private_data = dice;
148
149         err = snd_dice_transaction_init(dice);
150         if (err < 0)
151                 goto error;
152
153         err = check_clock_caps(dice);
154         if (err < 0)
155                 goto error;
156
157         dice_card_strings(dice);
158
159         err = dice->detect_formats(dice);
160         if (err < 0)
161                 goto error;
162
163         err = snd_dice_stream_init_duplex(dice);
164         if (err < 0)
165                 goto error;
166
167         snd_dice_create_proc(dice);
168
169         err = snd_dice_create_pcm(dice);
170         if (err < 0)
171                 goto error;
172
173         err = snd_dice_create_midi(dice);
174         if (err < 0)
175                 goto error;
176
177         err = snd_dice_create_hwdep(dice);
178         if (err < 0)
179                 goto error;
180
181         err = snd_card_register(dice->card);
182         if (err < 0)
183                 goto error;
184
185         dice->registered = true;
186
187         return;
188 error:
189         snd_card_free(dice->card);
190         dev_info(&dice->unit->device,
191                  "Sound card registration failed: %d\n", err);
192 }
193
194 static int dice_probe(struct fw_unit *unit,
195                       const struct ieee1394_device_id *entry)
196 {
197         struct snd_dice *dice;
198         int err;
199
200         if (!entry->driver_data && entry->vendor_id != OUI_SSL) {
201                 err = check_dice_category(unit);
202                 if (err < 0)
203                         return -ENODEV;
204         }
205
206         /* Allocate this independent of sound card instance. */
207         dice = devm_kzalloc(&unit->device, sizeof(struct snd_dice), GFP_KERNEL);
208         if (!dice)
209                 return -ENOMEM;
210         dice->unit = fw_unit_get(unit);
211         dev_set_drvdata(&unit->device, dice);
212
213         if (!entry->driver_data) {
214                 dice->detect_formats = snd_dice_stream_detect_current_formats;
215         } else {
216                 dice->detect_formats =
217                                 (snd_dice_detect_formats_t)entry->driver_data;
218         }
219
220         spin_lock_init(&dice->lock);
221         mutex_init(&dice->mutex);
222         init_completion(&dice->clock_accepted);
223         init_waitqueue_head(&dice->hwdep_wait);
224
225         /* Allocate and register this sound card later. */
226         INIT_DEFERRABLE_WORK(&dice->dwork, do_registration);
227         snd_fw_schedule_registration(unit, &dice->dwork);
228
229         return 0;
230 }
231
232 static void dice_remove(struct fw_unit *unit)
233 {
234         struct snd_dice *dice = dev_get_drvdata(&unit->device);
235
236         /*
237          * Confirm to stop the work for registration before the sound card is
238          * going to be released. The work is not scheduled again because bus
239          * reset handler is not called anymore.
240          */
241         cancel_delayed_work_sync(&dice->dwork);
242
243         if (dice->registered) {
244                 // Block till all of ALSA character devices are released.
245                 snd_card_free(dice->card);
246         }
247
248         mutex_destroy(&dice->mutex);
249         fw_unit_put(dice->unit);
250 }
251
252 static void dice_bus_reset(struct fw_unit *unit)
253 {
254         struct snd_dice *dice = dev_get_drvdata(&unit->device);
255
256         /* Postpone a workqueue for deferred registration. */
257         if (!dice->registered)
258                 snd_fw_schedule_registration(unit, &dice->dwork);
259
260         /* The handler address register becomes initialized. */
261         snd_dice_transaction_reinit(dice);
262
263         /*
264          * After registration, userspace can start packet streaming, then this
265          * code block works fine.
266          */
267         if (dice->registered) {
268                 mutex_lock(&dice->mutex);
269                 snd_dice_stream_update_duplex(dice);
270                 mutex_unlock(&dice->mutex);
271         }
272 }
273
274 #define DICE_INTERFACE  0x000001
275
276 static const struct ieee1394_device_id dice_id_table[] = {
277         /* M-Audio Profire 2626 has a different value in version field. */
278         {
279                 .match_flags    = IEEE1394_MATCH_VENDOR_ID |
280                                   IEEE1394_MATCH_MODEL_ID,
281                 .vendor_id      = OUI_MAUDIO,
282                 .model_id       = 0x000010,
283                 .driver_data = (kernel_ulong_t)snd_dice_detect_extension_formats,
284         },
285         /* M-Audio Profire 610 has a different value in version field. */
286         {
287                 .match_flags    = IEEE1394_MATCH_VENDOR_ID |
288                                   IEEE1394_MATCH_MODEL_ID,
289                 .vendor_id      = OUI_MAUDIO,
290                 .model_id       = 0x000011,
291                 .driver_data = (kernel_ulong_t)snd_dice_detect_extension_formats,
292         },
293         /* TC Electronic Konnekt 24D. */
294         {
295                 .match_flags    = IEEE1394_MATCH_VENDOR_ID |
296                                   IEEE1394_MATCH_MODEL_ID,
297                 .vendor_id      = OUI_TCELECTRONIC,
298                 .model_id       = 0x000020,
299                 .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
300         },
301         /* TC Electronic Konnekt 8. */
302         {
303                 .match_flags    = IEEE1394_MATCH_VENDOR_ID |
304                                   IEEE1394_MATCH_MODEL_ID,
305                 .vendor_id      = OUI_TCELECTRONIC,
306                 .model_id       = 0x000021,
307                 .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
308         },
309         /* TC Electronic Studio Konnekt 48. */
310         {
311                 .match_flags    = IEEE1394_MATCH_VENDOR_ID |
312                                   IEEE1394_MATCH_MODEL_ID,
313                 .vendor_id      = OUI_TCELECTRONIC,
314                 .model_id       = 0x000022,
315                 .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
316         },
317         /* TC Electronic Konnekt Live. */
318         {
319                 .match_flags    = IEEE1394_MATCH_VENDOR_ID |
320                                   IEEE1394_MATCH_MODEL_ID,
321                 .vendor_id      = OUI_TCELECTRONIC,
322                 .model_id       = 0x000023,
323                 .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
324         },
325         /* TC Electronic Desktop Konnekt 6. */
326         {
327                 .match_flags    = IEEE1394_MATCH_VENDOR_ID |
328                                   IEEE1394_MATCH_MODEL_ID,
329                 .vendor_id      = OUI_TCELECTRONIC,
330                 .model_id       = 0x000024,
331                 .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
332         },
333         /* TC Electronic Impact Twin. */
334         {
335                 .match_flags    = IEEE1394_MATCH_VENDOR_ID |
336                                   IEEE1394_MATCH_MODEL_ID,
337                 .vendor_id      = OUI_TCELECTRONIC,
338                 .model_id       = 0x000027,
339                 .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
340         },
341         /* TC Electronic Digital Konnekt x32. */
342         {
343                 .match_flags    = IEEE1394_MATCH_VENDOR_ID |
344                                   IEEE1394_MATCH_MODEL_ID,
345                 .vendor_id      = OUI_TCELECTRONIC,
346                 .model_id       = 0x000030,
347                 .driver_data = (kernel_ulong_t)snd_dice_detect_tcelectronic_formats,
348         },
349         /* Alesis iO14/iO26. */
350         {
351                 .match_flags    = IEEE1394_MATCH_VENDOR_ID |
352                                   IEEE1394_MATCH_MODEL_ID,
353                 .vendor_id      = OUI_ALESIS,
354                 .model_id       = MODEL_ALESIS_IO_BOTH,
355                 .driver_data = (kernel_ulong_t)snd_dice_detect_alesis_formats,
356         },
357         /* Mytek Stereo 192 DSD-DAC. */
358         {
359                 .match_flags    = IEEE1394_MATCH_VENDOR_ID |
360                                   IEEE1394_MATCH_MODEL_ID,
361                 .vendor_id      = OUI_MYTEK,
362                 .model_id       = 0x000002,
363                 .driver_data = (kernel_ulong_t)snd_dice_detect_mytek_formats,
364         },
365         // Solid State Logic, Duende Classic and Mini.
366         // NOTE: each field of GUID in config ROM is not compliant to standard
367         // DICE scheme.
368         {
369                 .match_flags    = IEEE1394_MATCH_VENDOR_ID |
370                                   IEEE1394_MATCH_MODEL_ID,
371                 .vendor_id      = OUI_SSL,
372                 .model_id       = 0x000070,
373         },
374         {
375                 .match_flags = IEEE1394_MATCH_VERSION,
376                 .version     = DICE_INTERFACE,
377         },
378         { }
379 };
380 MODULE_DEVICE_TABLE(ieee1394, dice_id_table);
381
382 static struct fw_driver dice_driver = {
383         .driver   = {
384                 .owner  = THIS_MODULE,
385                 .name   = KBUILD_MODNAME,
386                 .bus    = &fw_bus_type,
387         },
388         .probe    = dice_probe,
389         .update   = dice_bus_reset,
390         .remove   = dice_remove,
391         .id_table = dice_id_table,
392 };
393
394 static int __init alsa_dice_init(void)
395 {
396         return driver_register(&dice_driver.driver);
397 }
398
399 static void __exit alsa_dice_exit(void)
400 {
401         driver_unregister(&dice_driver.driver);
402 }
403
404 module_init(alsa_dice_init);
405 module_exit(alsa_dice_exit);
This page took 0.058089 seconds and 4 git commands to generate.