These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / 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 dice_interface_check(struct fw_unit *unit)
22 {
23         static const int min_values[10] = {
24                 10, 0x64 / 4,
25                 10, 0x18 / 4,
26                 10, 0x18 / 4,
27                 0, 0,
28                 0, 0,
29         };
30         struct fw_device *device = fw_parent_device(unit);
31         struct fw_csr_iterator it;
32         int key, val, vendor = -1, model = -1, err;
33         unsigned int category, i;
34         __be32 *pointers;
35         u32 value;
36         __be32 version;
37
38         pointers = kmalloc_array(ARRAY_SIZE(min_values), sizeof(__be32),
39                                  GFP_KERNEL);
40         if (pointers == NULL)
41                 return -ENOMEM;
42
43         /*
44          * Check that GUID and unit directory are constructed according to DICE
45          * rules, i.e., that the specifier ID is the GUID's OUI, and that the
46          * GUID chip ID consists of the 8-bit category ID, the 10-bit product
47          * ID, and a 22-bit serial number.
48          */
49         fw_csr_iterator_init(&it, unit->directory);
50         while (fw_csr_iterator_next(&it, &key, &val)) {
51                 switch (key) {
52                 case CSR_SPECIFIER_ID:
53                         vendor = val;
54                         break;
55                 case CSR_MODEL:
56                         model = val;
57                         break;
58                 }
59         }
60         if (vendor == OUI_WEISS)
61                 category = WEISS_CATEGORY_ID;
62         else if (vendor == OUI_LOUD)
63                 category = LOUD_CATEGORY_ID;
64         else
65                 category = DICE_CATEGORY_ID;
66         if (device->config_rom[3] != ((vendor << 8) | category) ||
67             device->config_rom[4] >> 22 != model) {
68                 err = -ENODEV;
69                 goto end;
70         }
71
72         /*
73          * Check that the sub address spaces exist and are located inside the
74          * private address space.  The minimum values are chosen so that all
75          * minimally required registers are included.
76          */
77         err = snd_fw_transaction(unit, TCODE_READ_BLOCK_REQUEST,
78                                  DICE_PRIVATE_SPACE, pointers,
79                                  sizeof(__be32) * ARRAY_SIZE(min_values), 0);
80         if (err < 0) {
81                 err = -ENODEV;
82                 goto end;
83         }
84         for (i = 0; i < ARRAY_SIZE(min_values); ++i) {
85                 value = be32_to_cpu(pointers[i]);
86                 if (value < min_values[i] || value >= 0x40000) {
87                         err = -ENODEV;
88                         goto end;
89                 }
90         }
91
92         /*
93          * Check that the implemented DICE driver specification major version
94          * number matches.
95          */
96         err = snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST,
97                                  DICE_PRIVATE_SPACE +
98                                  be32_to_cpu(pointers[0]) * 4 + GLOBAL_VERSION,
99                                  &version, 4, 0);
100         if (err < 0) {
101                 err = -ENODEV;
102                 goto end;
103         }
104         if ((version & cpu_to_be32(0xff000000)) != cpu_to_be32(0x01000000)) {
105                 dev_err(&unit->device,
106                         "unknown DICE version: 0x%08x\n", be32_to_cpu(version));
107                 err = -ENODEV;
108                 goto end;
109         }
110 end:
111         return err;
112 }
113
114 static int highest_supported_mode_rate(struct snd_dice *dice,
115                                        unsigned int mode, unsigned int *rate)
116 {
117         unsigned int i, m;
118
119         for (i = ARRAY_SIZE(snd_dice_rates); i > 0; i--) {
120                 *rate = snd_dice_rates[i - 1];
121                 if (snd_dice_stream_get_rate_mode(dice, *rate, &m) < 0)
122                         continue;
123                 if (mode == m)
124                         break;
125         }
126         if (i == 0)
127                 return -EINVAL;
128
129         return 0;
130 }
131
132 static int dice_read_mode_params(struct snd_dice *dice, unsigned int mode)
133 {
134         __be32 values[2];
135         unsigned int rate;
136         int err;
137
138         if (highest_supported_mode_rate(dice, mode, &rate) < 0) {
139                 dice->tx_channels[mode] = 0;
140                 dice->tx_midi_ports[mode] = 0;
141                 dice->rx_channels[mode] = 0;
142                 dice->rx_midi_ports[mode] = 0;
143                 return 0;
144         }
145
146         err = snd_dice_transaction_set_rate(dice, rate);
147         if (err < 0)
148                 return err;
149
150         err = snd_dice_transaction_read_tx(dice, TX_NUMBER_AUDIO,
151                                            values, sizeof(values));
152         if (err < 0)
153                 return err;
154
155         dice->tx_channels[mode]   = be32_to_cpu(values[0]);
156         dice->tx_midi_ports[mode] = be32_to_cpu(values[1]);
157
158         err = snd_dice_transaction_read_rx(dice, RX_NUMBER_AUDIO,
159                                            values, sizeof(values));
160         if (err < 0)
161                 return err;
162
163         dice->rx_channels[mode]   = be32_to_cpu(values[0]);
164         dice->rx_midi_ports[mode] = be32_to_cpu(values[1]);
165
166         return 0;
167 }
168
169 static int dice_read_params(struct snd_dice *dice)
170 {
171         __be32 value;
172         int mode, err;
173
174         /* some very old firmwares don't tell about their clock support */
175         if (dice->clock_caps > 0) {
176                 err = snd_dice_transaction_read_global(dice,
177                                                 GLOBAL_CLOCK_CAPABILITIES,
178                                                 &value, 4);
179                 if (err < 0)
180                         return err;
181                 dice->clock_caps = be32_to_cpu(value);
182         } else {
183                 /* this should be supported by any device */
184                 dice->clock_caps = CLOCK_CAP_RATE_44100 |
185                                    CLOCK_CAP_RATE_48000 |
186                                    CLOCK_CAP_SOURCE_ARX1 |
187                                    CLOCK_CAP_SOURCE_INTERNAL;
188         }
189
190         for (mode = 2; mode >= 0; --mode) {
191                 err = dice_read_mode_params(dice, mode);
192                 if (err < 0)
193                         return err;
194         }
195
196         return 0;
197 }
198
199 static void dice_card_strings(struct snd_dice *dice)
200 {
201         struct snd_card *card = dice->card;
202         struct fw_device *dev = fw_parent_device(dice->unit);
203         char vendor[32], model[32];
204         unsigned int i;
205         int err;
206
207         strcpy(card->driver, "DICE");
208
209         strcpy(card->shortname, "DICE");
210         BUILD_BUG_ON(NICK_NAME_SIZE < sizeof(card->shortname));
211         err = snd_dice_transaction_read_global(dice, GLOBAL_NICK_NAME,
212                                                card->shortname,
213                                                sizeof(card->shortname));
214         if (err >= 0) {
215                 /* DICE strings are returned in "always-wrong" endianness */
216                 BUILD_BUG_ON(sizeof(card->shortname) % 4 != 0);
217                 for (i = 0; i < sizeof(card->shortname); i += 4)
218                         swab32s((u32 *)&card->shortname[i]);
219                 card->shortname[sizeof(card->shortname) - 1] = '\0';
220         }
221
222         strcpy(vendor, "?");
223         fw_csr_string(dev->config_rom + 5, CSR_VENDOR, vendor, sizeof(vendor));
224         strcpy(model, "?");
225         fw_csr_string(dice->unit->directory, CSR_MODEL, model, sizeof(model));
226         snprintf(card->longname, sizeof(card->longname),
227                  "%s %s (serial %u) at %s, S%d",
228                  vendor, model, dev->config_rom[4] & 0x3fffff,
229                  dev_name(&dice->unit->device), 100 << dev->max_speed);
230
231         strcpy(card->mixername, "DICE");
232 }
233
234 /*
235  * This module releases the FireWire unit data after all ALSA character devices
236  * are released by applications. This is for releasing stream data or finishing
237  * transactions safely. Thus at returning from .remove(), this module still keep
238  * references for the unit.
239  */
240 static void dice_card_free(struct snd_card *card)
241 {
242         struct snd_dice *dice = card->private_data;
243
244         snd_dice_stream_destroy_duplex(dice);
245         snd_dice_transaction_destroy(dice);
246         fw_unit_put(dice->unit);
247
248         mutex_destroy(&dice->mutex);
249 }
250
251 static int dice_probe(struct fw_unit *unit, const struct ieee1394_device_id *id)
252 {
253         struct snd_card *card;
254         struct snd_dice *dice;
255         int err;
256
257         err = dice_interface_check(unit);
258         if (err < 0)
259                 goto end;
260
261         err = snd_card_new(&unit->device, -1, NULL, THIS_MODULE,
262                            sizeof(*dice), &card);
263         if (err < 0)
264                 goto end;
265
266         dice = card->private_data;
267         dice->card = card;
268         dice->unit = fw_unit_get(unit);
269         card->private_free = dice_card_free;
270
271         spin_lock_init(&dice->lock);
272         mutex_init(&dice->mutex);
273         init_completion(&dice->clock_accepted);
274         init_waitqueue_head(&dice->hwdep_wait);
275
276         err = snd_dice_transaction_init(dice);
277         if (err < 0)
278                 goto error;
279
280         err = dice_read_params(dice);
281         if (err < 0)
282                 goto error;
283
284         dice_card_strings(dice);
285
286         err = snd_dice_create_pcm(dice);
287         if (err < 0)
288                 goto error;
289
290         err = snd_dice_create_hwdep(dice);
291         if (err < 0)
292                 goto error;
293
294         snd_dice_create_proc(dice);
295
296         err = snd_dice_create_midi(dice);
297         if (err < 0)
298                 goto error;
299
300         err = snd_dice_stream_init_duplex(dice);
301         if (err < 0)
302                 goto error;
303
304         err = snd_card_register(card);
305         if (err < 0) {
306                 snd_dice_stream_destroy_duplex(dice);
307                 goto error;
308         }
309
310         dev_set_drvdata(&unit->device, dice);
311 end:
312         return err;
313 error:
314         snd_card_free(card);
315         return err;
316 }
317
318 static void dice_remove(struct fw_unit *unit)
319 {
320         struct snd_dice *dice = dev_get_drvdata(&unit->device);
321
322         /* No need to wait for releasing card object in this context. */
323         snd_card_free_when_closed(dice->card);
324 }
325
326 static void dice_bus_reset(struct fw_unit *unit)
327 {
328         struct snd_dice *dice = dev_get_drvdata(&unit->device);
329
330         /* The handler address register becomes initialized. */
331         snd_dice_transaction_reinit(dice);
332
333         mutex_lock(&dice->mutex);
334         snd_dice_stream_update_duplex(dice);
335         mutex_unlock(&dice->mutex);
336 }
337
338 #define DICE_INTERFACE  0x000001
339
340 static const struct ieee1394_device_id dice_id_table[] = {
341         {
342                 .match_flags = IEEE1394_MATCH_VERSION,
343                 .version     = DICE_INTERFACE,
344         },
345         { }
346 };
347 MODULE_DEVICE_TABLE(ieee1394, dice_id_table);
348
349 static struct fw_driver dice_driver = {
350         .driver   = {
351                 .owner  = THIS_MODULE,
352                 .name   = KBUILD_MODNAME,
353                 .bus    = &fw_bus_type,
354         },
355         .probe    = dice_probe,
356         .update   = dice_bus_reset,
357         .remove   = dice_remove,
358         .id_table = dice_id_table,
359 };
360
361 static int __init alsa_dice_init(void)
362 {
363         return driver_register(&dice_driver.driver);
364 }
365
366 static void __exit alsa_dice_exit(void)
367 {
368         driver_unregister(&dice_driver.driver);
369 }
370
371 module_init(alsa_dice_init);
372 module_exit(alsa_dice_exit);