GNU Linux-libre 4.9.309-gnu1
[releases.git] / sound / firewire / oxfw / oxfw.c
1 /*
2  * oxfw.c - a part of driver for OXFW970/971 based devices
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 "oxfw.h"
9
10 #define OXFORD_FIRMWARE_ID_ADDRESS      (CSR_REGISTER_BASE + 0x50000)
11 /* 0x970?vvvv or 0x971?vvvv, where vvvv = firmware version */
12
13 #define OXFORD_HARDWARE_ID_ADDRESS      (CSR_REGISTER_BASE + 0x90020)
14 #define OXFORD_HARDWARE_ID_OXFW970      0x39443841
15 #define OXFORD_HARDWARE_ID_OXFW971      0x39373100
16
17 #define VENDOR_LOUD             0x000ff2
18 #define VENDOR_GRIFFIN          0x001292
19 #define VENDOR_BEHRINGER        0x001564
20 #define VENDOR_LACIE            0x00d04b
21 #define VENDOR_TASCAM           0x00022e
22 #define OUI_STANTON             0x001260
23 #define OUI_APOGEE              0x0003db
24
25 #define MODEL_SATELLITE         0x00200f
26
27 #define SPECIFIER_1394TA        0x00a02d
28 #define VERSION_AVC             0x010001
29
30 MODULE_DESCRIPTION("Oxford Semiconductor FW970/971 driver");
31 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
32 MODULE_LICENSE("GPL v2");
33 MODULE_ALIAS("snd-firewire-speakers");
34 MODULE_ALIAS("snd-scs1x");
35
36 struct compat_info {
37         const char *driver_name;
38         const char *vendor_name;
39         const char *model_name;
40 };
41
42 static bool detect_loud_models(struct fw_unit *unit)
43 {
44         const char *const models[] = {
45                 "Onyxi",
46                 "Onyx-i",
47                 "d.Pro",
48                 "Mackie Onyx Satellite",
49                 "Tapco LINK.firewire 4x6",
50                 "U.420"};
51         char model[32];
52         unsigned int i;
53         int err;
54
55         err = fw_csr_string(unit->directory, CSR_MODEL,
56                             model, sizeof(model));
57         if (err < 0)
58                 return false;
59
60         for (i = 0; i < ARRAY_SIZE(models); i++) {
61                 if (strcmp(models[i], model) == 0)
62                         break;
63         }
64
65         return (i < ARRAY_SIZE(models));
66 }
67
68 static int name_card(struct snd_oxfw *oxfw)
69 {
70         struct fw_device *fw_dev = fw_parent_device(oxfw->unit);
71         const struct compat_info *info;
72         char vendor[24];
73         char model[32];
74         const char *d, *v, *m;
75         u32 firmware;
76         int err;
77
78         /* get vendor name from root directory */
79         err = fw_csr_string(fw_dev->config_rom + 5, CSR_VENDOR,
80                             vendor, sizeof(vendor));
81         if (err < 0)
82                 goto end;
83
84         /* get model name from unit directory */
85         err = fw_csr_string(oxfw->unit->directory, CSR_MODEL,
86                             model, sizeof(model));
87         if (err < 0)
88                 goto end;
89
90         err = snd_fw_transaction(oxfw->unit, TCODE_READ_QUADLET_REQUEST,
91                                  OXFORD_FIRMWARE_ID_ADDRESS, &firmware, 4, 0);
92         if (err < 0)
93                 goto end;
94         be32_to_cpus(&firmware);
95
96         /* to apply card definitions */
97         if (oxfw->entry->vendor_id == VENDOR_GRIFFIN ||
98             oxfw->entry->vendor_id == VENDOR_LACIE) {
99                 info = (const struct compat_info *)oxfw->entry->driver_data;
100                 d = info->driver_name;
101                 v = info->vendor_name;
102                 m = info->model_name;
103         } else {
104                 d = "OXFW";
105                 v = vendor;
106                 m = model;
107         }
108
109         strcpy(oxfw->card->driver, d);
110         strcpy(oxfw->card->mixername, m);
111         strcpy(oxfw->card->shortname, m);
112
113         snprintf(oxfw->card->longname, sizeof(oxfw->card->longname),
114                  "%s %s (OXFW%x %04x), GUID %08x%08x at %s, S%d",
115                  v, m, firmware >> 20, firmware & 0xffff,
116                  fw_dev->config_rom[3], fw_dev->config_rom[4],
117                  dev_name(&oxfw->unit->device), 100 << fw_dev->max_speed);
118 end:
119         return err;
120 }
121
122 static void oxfw_free(struct snd_oxfw *oxfw)
123 {
124         unsigned int i;
125
126         snd_oxfw_stream_destroy_simplex(oxfw, &oxfw->rx_stream);
127         if (oxfw->has_output)
128                 snd_oxfw_stream_destroy_simplex(oxfw, &oxfw->tx_stream);
129
130         fw_unit_put(oxfw->unit);
131
132         for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
133                 kfree(oxfw->tx_stream_formats[i]);
134                 kfree(oxfw->rx_stream_formats[i]);
135         }
136
137         kfree(oxfw->spec);
138         mutex_destroy(&oxfw->mutex);
139         kfree(oxfw);
140 }
141
142 /*
143  * This module releases the FireWire unit data after all ALSA character devices
144  * are released by applications. This is for releasing stream data or finishing
145  * transactions safely. Thus at returning from .remove(), this module still keep
146  * references for the unit.
147  */
148 static void oxfw_card_free(struct snd_card *card)
149 {
150         oxfw_free(card->private_data);
151 }
152
153 static int detect_quirks(struct snd_oxfw *oxfw)
154 {
155         struct fw_device *fw_dev = fw_parent_device(oxfw->unit);
156         struct fw_csr_iterator it;
157         int key, val;
158         int vendor, model;
159
160         /*
161          * Add ALSA control elements for two models to keep compatibility to
162          * old firewire-speaker module.
163          */
164         if (oxfw->entry->vendor_id == VENDOR_GRIFFIN)
165                 return snd_oxfw_add_spkr(oxfw, false);
166         if (oxfw->entry->vendor_id == VENDOR_LACIE)
167                 return snd_oxfw_add_spkr(oxfw, true);
168
169         /*
170          * Stanton models supports asynchronous transactions for unique MIDI
171          * messages.
172          */
173         if (oxfw->entry->vendor_id == OUI_STANTON) {
174                 /* No physical MIDI ports. */
175                 oxfw->midi_input_ports = 0;
176                 oxfw->midi_output_ports = 0;
177
178                 return snd_oxfw_scs1x_add(oxfw);
179         }
180
181         /*
182          * TASCAM FireOne has physical control and requires a pair of additional
183          * MIDI ports.
184          */
185         if (oxfw->entry->vendor_id == VENDOR_TASCAM) {
186                 oxfw->midi_input_ports++;
187                 oxfw->midi_output_ports++;
188                 return 0;
189         }
190
191         /* Seek from Root Directory of Config ROM. */
192         vendor = model = 0;
193         fw_csr_iterator_init(&it, fw_dev->config_rom + 5);
194         while (fw_csr_iterator_next(&it, &key, &val)) {
195                 if (key == CSR_VENDOR)
196                         vendor = val;
197                 else if (key == CSR_MODEL)
198                         model = val;
199         }
200
201         /*
202          * Mackie Onyx Satellite with base station has a quirk to report a wrong
203          * value in 'dbs' field of CIP header against its format information.
204          */
205         if (vendor == VENDOR_LOUD && model == MODEL_SATELLITE)
206                 oxfw->wrong_dbs = true;
207
208         return 0;
209 }
210
211 static void do_registration(struct work_struct *work)
212 {
213         struct snd_oxfw *oxfw = container_of(work, struct snd_oxfw, dwork.work);
214         int i;
215         int err;
216
217         if (oxfw->registered)
218                 return;
219
220         err = snd_card_new(&oxfw->unit->device, -1, NULL, THIS_MODULE, 0,
221                            &oxfw->card);
222         if (err < 0)
223                 return;
224
225         err = name_card(oxfw);
226         if (err < 0)
227                 goto error;
228
229         err = snd_oxfw_stream_discover(oxfw);
230         if (err < 0)
231                 goto error;
232
233         err = detect_quirks(oxfw);
234         if (err < 0)
235                 goto error;
236
237         err = snd_oxfw_stream_init_simplex(oxfw, &oxfw->rx_stream);
238         if (err < 0)
239                 goto error;
240         if (oxfw->has_output) {
241                 err = snd_oxfw_stream_init_simplex(oxfw, &oxfw->tx_stream);
242                 if (err < 0)
243                         goto error;
244         }
245
246         err = snd_oxfw_create_pcm(oxfw);
247         if (err < 0)
248                 goto error;
249
250         snd_oxfw_proc_init(oxfw);
251
252         err = snd_oxfw_create_midi(oxfw);
253         if (err < 0)
254                 goto error;
255
256         err = snd_oxfw_create_hwdep(oxfw);
257         if (err < 0)
258                 goto error;
259
260         err = snd_card_register(oxfw->card);
261         if (err < 0)
262                 goto error;
263
264         /*
265          * After registered, oxfw instance can be released corresponding to
266          * releasing the sound card instance.
267          */
268         oxfw->card->private_free = oxfw_card_free;
269         oxfw->card->private_data = oxfw;
270         oxfw->registered = true;
271
272         return;
273 error:
274         snd_oxfw_stream_destroy_simplex(oxfw, &oxfw->rx_stream);
275         if (oxfw->has_output)
276                 snd_oxfw_stream_destroy_simplex(oxfw, &oxfw->tx_stream);
277         for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; ++i) {
278                 kfree(oxfw->tx_stream_formats[i]);
279                 oxfw->tx_stream_formats[i] = NULL;
280                 kfree(oxfw->rx_stream_formats[i]);
281                 oxfw->rx_stream_formats[i] = NULL;
282         }
283         snd_card_free(oxfw->card);
284         kfree(oxfw->spec);
285         oxfw->spec = NULL;
286         dev_info(&oxfw->unit->device,
287                  "Sound card registration failed: %d\n", err);
288 }
289
290 static int oxfw_probe(struct fw_unit *unit,
291                       const struct ieee1394_device_id *entry)
292 {
293         struct snd_oxfw *oxfw;
294
295         if (entry->vendor_id == VENDOR_LOUD && !detect_loud_models(unit))
296                 return -ENODEV;
297
298         /* Allocate this independent of sound card instance. */
299         oxfw = kzalloc(sizeof(struct snd_oxfw), GFP_KERNEL);
300         if (oxfw == NULL)
301                 return -ENOMEM;
302
303         oxfw->entry = entry;
304         oxfw->unit = fw_unit_get(unit);
305         dev_set_drvdata(&unit->device, oxfw);
306
307         mutex_init(&oxfw->mutex);
308         spin_lock_init(&oxfw->lock);
309         init_waitqueue_head(&oxfw->hwdep_wait);
310
311         /* Allocate and register this sound card later. */
312         INIT_DEFERRABLE_WORK(&oxfw->dwork, do_registration);
313         snd_fw_schedule_registration(unit, &oxfw->dwork);
314
315         return 0;
316 }
317
318 static void oxfw_bus_reset(struct fw_unit *unit)
319 {
320         struct snd_oxfw *oxfw = dev_get_drvdata(&unit->device);
321
322         if (!oxfw->registered)
323                 snd_fw_schedule_registration(unit, &oxfw->dwork);
324
325         fcp_bus_reset(oxfw->unit);
326
327         if (oxfw->registered) {
328                 mutex_lock(&oxfw->mutex);
329
330                 snd_oxfw_stream_update_simplex(oxfw, &oxfw->rx_stream);
331                 if (oxfw->has_output)
332                         snd_oxfw_stream_update_simplex(oxfw, &oxfw->tx_stream);
333
334                 mutex_unlock(&oxfw->mutex);
335
336                 if (oxfw->entry->vendor_id == OUI_STANTON)
337                         snd_oxfw_scs1x_update(oxfw);
338         }
339 }
340
341 static void oxfw_remove(struct fw_unit *unit)
342 {
343         struct snd_oxfw *oxfw = dev_get_drvdata(&unit->device);
344
345         /*
346          * Confirm to stop the work for registration before the sound card is
347          * going to be released. The work is not scheduled again because bus
348          * reset handler is not called anymore.
349          */
350         cancel_delayed_work_sync(&oxfw->dwork);
351
352         if (oxfw->registered) {
353                 /* No need to wait for releasing card object in this context. */
354                 snd_card_free_when_closed(oxfw->card);
355         } else {
356                 /* Don't forget this case. */
357                 oxfw_free(oxfw);
358         }
359 }
360
361 static const struct compat_info griffin_firewave = {
362         .driver_name = "FireWave",
363         .vendor_name = "Griffin",
364         .model_name = "FireWave",
365 };
366
367 static const struct compat_info lacie_speakers = {
368         .driver_name = "FWSpeakers",
369         .vendor_name = "LaCie",
370         .model_name = "FireWire Speakers",
371 };
372
373 static const struct ieee1394_device_id oxfw_id_table[] = {
374         {
375                 .match_flags  = IEEE1394_MATCH_VENDOR_ID |
376                                 IEEE1394_MATCH_MODEL_ID |
377                                 IEEE1394_MATCH_SPECIFIER_ID |
378                                 IEEE1394_MATCH_VERSION,
379                 .vendor_id    = VENDOR_GRIFFIN,
380                 .model_id     = 0x00f970,
381                 .specifier_id = SPECIFIER_1394TA,
382                 .version      = VERSION_AVC,
383                 .driver_data  = (kernel_ulong_t)&griffin_firewave,
384         },
385         {
386                 .match_flags  = IEEE1394_MATCH_VENDOR_ID |
387                                 IEEE1394_MATCH_MODEL_ID |
388                                 IEEE1394_MATCH_SPECIFIER_ID |
389                                 IEEE1394_MATCH_VERSION,
390                 .vendor_id    = VENDOR_LACIE,
391                 .model_id     = 0x00f970,
392                 .specifier_id = SPECIFIER_1394TA,
393                 .version      = VERSION_AVC,
394                 .driver_data  = (kernel_ulong_t)&lacie_speakers,
395         },
396         /* Behringer,F-Control Audio 202 */
397         {
398                 .match_flags    = IEEE1394_MATCH_VENDOR_ID |
399                                   IEEE1394_MATCH_MODEL_ID,
400                 .vendor_id      = VENDOR_BEHRINGER,
401                 .model_id       = 0x00fc22,
402         },
403         /*
404          * Any Mackie(Loud) models (name string/model id):
405          *  Onyx-i series (former models):      0x081216
406          *  Mackie Onyx Satellite:              0x00200f
407          *  Tapco LINK.firewire 4x6:            0x000460
408          *  d.2 pro/d.4 pro (built-in card):    Unknown
409          *  U.420:                              Unknown
410          *  U.420d:                             Unknown
411          */
412         {
413                 .match_flags    = IEEE1394_MATCH_VENDOR_ID |
414                                   IEEE1394_MATCH_SPECIFIER_ID |
415                                   IEEE1394_MATCH_VERSION,
416                 .vendor_id      = VENDOR_LOUD,
417                 .specifier_id   = SPECIFIER_1394TA,
418                 .version        = VERSION_AVC,
419         },
420         /* TASCAM, FireOne */
421         {
422                 .match_flags    = IEEE1394_MATCH_VENDOR_ID |
423                                   IEEE1394_MATCH_MODEL_ID,
424                 .vendor_id      = VENDOR_TASCAM,
425                 .model_id       = 0x800007,
426         },
427         /* Stanton, Stanton Controllers & Systems 1 Mixer (SCS.1m) */
428         {
429                 .match_flags    = IEEE1394_MATCH_VENDOR_ID |
430                                   IEEE1394_MATCH_MODEL_ID,
431                 .vendor_id      = OUI_STANTON,
432                 .model_id       = 0x001000,
433         },
434         /* Stanton, Stanton Controllers & Systems 1 Deck (SCS.1d) */
435         {
436                 .match_flags    = IEEE1394_MATCH_VENDOR_ID |
437                                   IEEE1394_MATCH_MODEL_ID,
438                 .vendor_id      = OUI_STANTON,
439                 .model_id       = 0x002000,
440         },
441         // APOGEE, duet FireWire
442         {
443                 .match_flags    = IEEE1394_MATCH_VENDOR_ID |
444                                   IEEE1394_MATCH_MODEL_ID,
445                 .vendor_id      = OUI_APOGEE,
446                 .model_id       = 0x01dddd,
447         },
448         { }
449 };
450 MODULE_DEVICE_TABLE(ieee1394, oxfw_id_table);
451
452 static struct fw_driver oxfw_driver = {
453         .driver   = {
454                 .owner  = THIS_MODULE,
455                 .name   = KBUILD_MODNAME,
456                 .bus    = &fw_bus_type,
457         },
458         .probe    = oxfw_probe,
459         .update   = oxfw_bus_reset,
460         .remove   = oxfw_remove,
461         .id_table = oxfw_id_table,
462 };
463
464 static int __init snd_oxfw_init(void)
465 {
466         return driver_register(&oxfw_driver.driver);
467 }
468
469 static void __exit snd_oxfw_exit(void)
470 {
471         driver_unregister(&oxfw_driver.driver);
472 }
473
474 module_init(snd_oxfw_init);
475 module_exit(snd_oxfw_exit);