Merge 4.14.73 into android-4.14-p
[GitHub/moto-9609/android_kernel_motorola_exynos9610.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
24 #define MODEL_SATELLITE 0x00200f
25
26 #define SPECIFIER_1394TA 0x00a02d
27 #define VERSION_AVC 0x010001
28
29 MODULE_DESCRIPTION("Oxford Semiconductor FW970/971 driver");
30 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
31 MODULE_LICENSE("GPL v2");
32 MODULE_ALIAS("snd-firewire-speakers");
33 MODULE_ALIAS("snd-scs1x");
34
35 struct compat_info {
36 const char *driver_name;
37 const char *vendor_name;
38 const char *model_name;
39 };
40
41 static bool detect_loud_models(struct fw_unit *unit)
42 {
43 const char *const models[] = {
44 "Onyxi",
45 "Onyx-i",
46 "Onyx 1640i",
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 /* Output stream exists but no data channels are useful. */
179 oxfw->has_output = false;
180
181 return snd_oxfw_scs1x_add(oxfw);
182 }
183
184 /*
185 * TASCAM FireOne has physical control and requires a pair of additional
186 * MIDI ports.
187 */
188 if (oxfw->entry->vendor_id == VENDOR_TASCAM) {
189 oxfw->midi_input_ports++;
190 oxfw->midi_output_ports++;
191 return 0;
192 }
193
194 /* Seek from Root Directory of Config ROM. */
195 vendor = model = 0;
196 fw_csr_iterator_init(&it, fw_dev->config_rom + 5);
197 while (fw_csr_iterator_next(&it, &key, &val)) {
198 if (key == CSR_VENDOR)
199 vendor = val;
200 else if (key == CSR_MODEL)
201 model = val;
202 }
203
204 /*
205 * Mackie Onyx Satellite with base station has a quirk to report a wrong
206 * value in 'dbs' field of CIP header against its format information.
207 */
208 if (vendor == VENDOR_LOUD && model == MODEL_SATELLITE)
209 oxfw->wrong_dbs = true;
210
211 return 0;
212 }
213
214 static void do_registration(struct work_struct *work)
215 {
216 struct snd_oxfw *oxfw = container_of(work, struct snd_oxfw, dwork.work);
217 int i;
218 int err;
219
220 if (oxfw->registered)
221 return;
222
223 err = snd_card_new(&oxfw->unit->device, -1, NULL, THIS_MODULE, 0,
224 &oxfw->card);
225 if (err < 0)
226 return;
227
228 err = name_card(oxfw);
229 if (err < 0)
230 goto error;
231
232 err = snd_oxfw_stream_discover(oxfw);
233 if (err < 0)
234 goto error;
235
236 err = detect_quirks(oxfw);
237 if (err < 0)
238 goto error;
239
240 err = snd_oxfw_stream_init_simplex(oxfw, &oxfw->rx_stream);
241 if (err < 0)
242 goto error;
243 if (oxfw->has_output) {
244 err = snd_oxfw_stream_init_simplex(oxfw, &oxfw->tx_stream);
245 if (err < 0)
246 goto error;
247 }
248
249 err = snd_oxfw_create_pcm(oxfw);
250 if (err < 0)
251 goto error;
252
253 snd_oxfw_proc_init(oxfw);
254
255 err = snd_oxfw_create_midi(oxfw);
256 if (err < 0)
257 goto error;
258
259 err = snd_oxfw_create_hwdep(oxfw);
260 if (err < 0)
261 goto error;
262
263 err = snd_card_register(oxfw->card);
264 if (err < 0)
265 goto error;
266
267 /*
268 * After registered, oxfw instance can be released corresponding to
269 * releasing the sound card instance.
270 */
271 oxfw->card->private_free = oxfw_card_free;
272 oxfw->card->private_data = oxfw;
273 oxfw->registered = true;
274
275 return;
276 error:
277 snd_oxfw_stream_destroy_simplex(oxfw, &oxfw->rx_stream);
278 if (oxfw->has_output)
279 snd_oxfw_stream_destroy_simplex(oxfw, &oxfw->tx_stream);
280 for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; ++i) {
281 kfree(oxfw->tx_stream_formats[i]);
282 oxfw->tx_stream_formats[i] = NULL;
283 kfree(oxfw->rx_stream_formats[i]);
284 oxfw->rx_stream_formats[i] = NULL;
285 }
286 snd_card_free(oxfw->card);
287 kfree(oxfw->spec);
288 oxfw->spec = NULL;
289 dev_info(&oxfw->unit->device,
290 "Sound card registration failed: %d\n", err);
291 }
292
293 static int oxfw_probe(struct fw_unit *unit,
294 const struct ieee1394_device_id *entry)
295 {
296 struct snd_oxfw *oxfw;
297
298 if (entry->vendor_id == VENDOR_LOUD && !detect_loud_models(unit))
299 return -ENODEV;
300
301 /* Allocate this independent of sound card instance. */
302 oxfw = kzalloc(sizeof(struct snd_oxfw), GFP_KERNEL);
303 if (oxfw == NULL)
304 return -ENOMEM;
305
306 oxfw->entry = entry;
307 oxfw->unit = fw_unit_get(unit);
308 dev_set_drvdata(&unit->device, oxfw);
309
310 mutex_init(&oxfw->mutex);
311 spin_lock_init(&oxfw->lock);
312 init_waitqueue_head(&oxfw->hwdep_wait);
313
314 /* Allocate and register this sound card later. */
315 INIT_DEFERRABLE_WORK(&oxfw->dwork, do_registration);
316 snd_fw_schedule_registration(unit, &oxfw->dwork);
317
318 return 0;
319 }
320
321 static void oxfw_bus_reset(struct fw_unit *unit)
322 {
323 struct snd_oxfw *oxfw = dev_get_drvdata(&unit->device);
324
325 if (!oxfw->registered)
326 snd_fw_schedule_registration(unit, &oxfw->dwork);
327
328 fcp_bus_reset(oxfw->unit);
329
330 if (oxfw->registered) {
331 mutex_lock(&oxfw->mutex);
332
333 snd_oxfw_stream_update_simplex(oxfw, &oxfw->rx_stream);
334 if (oxfw->has_output)
335 snd_oxfw_stream_update_simplex(oxfw, &oxfw->tx_stream);
336
337 mutex_unlock(&oxfw->mutex);
338
339 if (oxfw->entry->vendor_id == OUI_STANTON)
340 snd_oxfw_scs1x_update(oxfw);
341 }
342 }
343
344 static void oxfw_remove(struct fw_unit *unit)
345 {
346 struct snd_oxfw *oxfw = dev_get_drvdata(&unit->device);
347
348 /*
349 * Confirm to stop the work for registration before the sound card is
350 * going to be released. The work is not scheduled again because bus
351 * reset handler is not called anymore.
352 */
353 cancel_delayed_work_sync(&oxfw->dwork);
354
355 if (oxfw->registered) {
356 /* No need to wait for releasing card object in this context. */
357 snd_card_free_when_closed(oxfw->card);
358 } else {
359 /* Don't forget this case. */
360 oxfw_free(oxfw);
361 }
362 }
363
364 static const struct compat_info griffin_firewave = {
365 .driver_name = "FireWave",
366 .vendor_name = "Griffin",
367 .model_name = "FireWave",
368 };
369
370 static const struct compat_info lacie_speakers = {
371 .driver_name = "FWSpeakers",
372 .vendor_name = "LaCie",
373 .model_name = "FireWire Speakers",
374 };
375
376 static const struct ieee1394_device_id oxfw_id_table[] = {
377 {
378 .match_flags = IEEE1394_MATCH_VENDOR_ID |
379 IEEE1394_MATCH_MODEL_ID |
380 IEEE1394_MATCH_SPECIFIER_ID |
381 IEEE1394_MATCH_VERSION,
382 .vendor_id = VENDOR_GRIFFIN,
383 .model_id = 0x00f970,
384 .specifier_id = SPECIFIER_1394TA,
385 .version = VERSION_AVC,
386 .driver_data = (kernel_ulong_t)&griffin_firewave,
387 },
388 {
389 .match_flags = IEEE1394_MATCH_VENDOR_ID |
390 IEEE1394_MATCH_MODEL_ID |
391 IEEE1394_MATCH_SPECIFIER_ID |
392 IEEE1394_MATCH_VERSION,
393 .vendor_id = VENDOR_LACIE,
394 .model_id = 0x00f970,
395 .specifier_id = SPECIFIER_1394TA,
396 .version = VERSION_AVC,
397 .driver_data = (kernel_ulong_t)&lacie_speakers,
398 },
399 /* Behringer,F-Control Audio 202 */
400 {
401 .match_flags = IEEE1394_MATCH_VENDOR_ID |
402 IEEE1394_MATCH_MODEL_ID,
403 .vendor_id = VENDOR_BEHRINGER,
404 .model_id = 0x00fc22,
405 },
406 /*
407 * Any Mackie(Loud) models (name string/model id):
408 * Onyx-i series (former models): 0x081216
409 * Mackie Onyx Satellite: 0x00200f
410 * Tapco LINK.firewire 4x6: 0x000460
411 * d.2 pro: Unknown
412 * d.4 pro: Unknown
413 * U.420: Unknown
414 * U.420d: Unknown
415 */
416 {
417 .match_flags = IEEE1394_MATCH_VENDOR_ID |
418 IEEE1394_MATCH_SPECIFIER_ID |
419 IEEE1394_MATCH_VERSION,
420 .vendor_id = VENDOR_LOUD,
421 .specifier_id = SPECIFIER_1394TA,
422 .version = VERSION_AVC,
423 },
424 /* TASCAM, FireOne */
425 {
426 .match_flags = IEEE1394_MATCH_VENDOR_ID |
427 IEEE1394_MATCH_MODEL_ID,
428 .vendor_id = VENDOR_TASCAM,
429 .model_id = 0x800007,
430 },
431 /* Stanton, Stanton Controllers & Systems 1 Mixer (SCS.1m) */
432 {
433 .match_flags = IEEE1394_MATCH_VENDOR_ID |
434 IEEE1394_MATCH_MODEL_ID,
435 .vendor_id = OUI_STANTON,
436 .model_id = 0x001000,
437 },
438 /* Stanton, Stanton Controllers & Systems 1 Deck (SCS.1d) */
439 {
440 .match_flags = IEEE1394_MATCH_VENDOR_ID |
441 IEEE1394_MATCH_MODEL_ID,
442 .vendor_id = OUI_STANTON,
443 .model_id = 0x002000,
444 },
445 { }
446 };
447 MODULE_DEVICE_TABLE(ieee1394, oxfw_id_table);
448
449 static struct fw_driver oxfw_driver = {
450 .driver = {
451 .owner = THIS_MODULE,
452 .name = KBUILD_MODNAME,
453 .bus = &fw_bus_type,
454 },
455 .probe = oxfw_probe,
456 .update = oxfw_bus_reset,
457 .remove = oxfw_remove,
458 .id_table = oxfw_id_table,
459 };
460
461 static int __init snd_oxfw_init(void)
462 {
463 return driver_register(&oxfw_driver.driver);
464 }
465
466 static void __exit snd_oxfw_exit(void)
467 {
468 driver_unregister(&oxfw_driver.driver);
469 }
470
471 module_init(snd_oxfw_init);
472 module_exit(snd_oxfw_exit);