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