]> git.karo-electronics.de Git - linux-beck.git/blob - sound/firewire/oxfw/oxfw.c
ALSA: oxfw: rename a structure so that it means backward compatibility to old drivers
[linux-beck.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
23 #define MODEL_SATELLITE         0x00200f
24
25 #define SPECIFIER_1394TA        0x00a02d
26 #define VERSION_AVC             0x010001
27
28 MODULE_DESCRIPTION("Oxford Semiconductor FW970/971 driver");
29 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
30 MODULE_LICENSE("GPL v2");
31 MODULE_ALIAS("snd-firewire-speakers");
32
33 struct compat_info {
34         const char *driver_name;
35         const char *vendor_name;
36         const char *model_name;
37 };
38
39 static bool detect_loud_models(struct fw_unit *unit)
40 {
41         const char *const models[] = {
42                 "Onyxi",
43                 "Onyx-i",
44                 "d.Pro",
45                 "Mackie Onyx Satellite",
46                 "Tapco LINK.firewire 4x6",
47                 "U.420"};
48         char model[32];
49         unsigned int i;
50         int err;
51
52         err = fw_csr_string(unit->directory, CSR_MODEL,
53                             model, sizeof(model));
54         if (err < 0)
55                 return false;
56
57         for (i = 0; i < ARRAY_SIZE(models); i++) {
58                 if (strcmp(models[i], model) == 0)
59                         break;
60         }
61
62         return (i < ARRAY_SIZE(models));
63 }
64
65 static int name_card(struct snd_oxfw *oxfw)
66 {
67         struct fw_device *fw_dev = fw_parent_device(oxfw->unit);
68         const struct compat_info *info;
69         char vendor[24];
70         char model[32];
71         const char *d, *v, *m;
72         u32 firmware;
73         int err;
74
75         /* get vendor name from root directory */
76         err = fw_csr_string(fw_dev->config_rom + 5, CSR_VENDOR,
77                             vendor, sizeof(vendor));
78         if (err < 0)
79                 goto end;
80
81         /* get model name from unit directory */
82         err = fw_csr_string(oxfw->unit->directory, CSR_MODEL,
83                             model, sizeof(model));
84         if (err < 0)
85                 goto end;
86
87         err = snd_fw_transaction(oxfw->unit, TCODE_READ_QUADLET_REQUEST,
88                                  OXFORD_FIRMWARE_ID_ADDRESS, &firmware, 4, 0);
89         if (err < 0)
90                 goto end;
91         be32_to_cpus(&firmware);
92
93         /* to apply card definitions */
94         if (oxfw->entry->vendor_id == VENDOR_GRIFFIN ||
95             oxfw->entry->vendor_id == VENDOR_LACIE) {
96                 info = (const struct compat_info *)oxfw->entry->driver_data;
97                 d = info->driver_name;
98                 v = info->vendor_name;
99                 m = info->model_name;
100         } else {
101                 d = "OXFW";
102                 v = vendor;
103                 m = model;
104         }
105
106         strcpy(oxfw->card->driver, d);
107         strcpy(oxfw->card->mixername, m);
108         strcpy(oxfw->card->shortname, m);
109
110         snprintf(oxfw->card->longname, sizeof(oxfw->card->longname),
111                  "%s %s (OXFW%x %04x), GUID %08x%08x at %s, S%d",
112                  v, m, firmware >> 20, firmware & 0xffff,
113                  fw_dev->config_rom[3], fw_dev->config_rom[4],
114                  dev_name(&oxfw->unit->device), 100 << fw_dev->max_speed);
115 end:
116         return err;
117 }
118
119 /*
120  * This module releases the FireWire unit data after all ALSA character devices
121  * are released by applications. This is for releasing stream data or finishing
122  * transactions safely. Thus at returning from .remove(), this module still keep
123  * references for the unit.
124  */
125 static void oxfw_card_free(struct snd_card *card)
126 {
127         struct snd_oxfw *oxfw = card->private_data;
128         unsigned int i;
129
130         snd_oxfw_stream_destroy_simplex(oxfw, &oxfw->rx_stream);
131         if (oxfw->has_output)
132                 snd_oxfw_stream_destroy_simplex(oxfw, &oxfw->tx_stream);
133
134         fw_unit_put(oxfw->unit);
135
136         for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
137                 kfree(oxfw->tx_stream_formats[i]);
138                 kfree(oxfw->rx_stream_formats[i]);
139         }
140
141         kfree(oxfw->spec);
142         mutex_destroy(&oxfw->mutex);
143 }
144
145 static int detect_quirks(struct snd_oxfw *oxfw)
146 {
147         struct fw_device *fw_dev = fw_parent_device(oxfw->unit);
148         struct fw_csr_iterator it;
149         int key, val;
150         int vendor, model;
151
152         /*
153          * Add ALSA control elements for two models to keep compatibility to
154          * old firewire-speaker module.
155          */
156         if (oxfw->entry->vendor_id == VENDOR_GRIFFIN)
157                 return snd_oxfw_add_spkr(oxfw, false);
158         if (oxfw->entry->vendor_id == VENDOR_LACIE)
159                 return snd_oxfw_add_spkr(oxfw, true);
160
161         /*
162          * TASCAM FireOne has physical control and requires a pair of additional
163          * MIDI ports.
164          */
165         if (oxfw->entry->vendor_id == VENDOR_TASCAM) {
166                 oxfw->midi_input_ports++;
167                 oxfw->midi_output_ports++;
168                 return 0;
169         }
170
171         /* Seek from Root Directory of Config ROM. */
172         vendor = model = 0;
173         fw_csr_iterator_init(&it, fw_dev->config_rom + 5);
174         while (fw_csr_iterator_next(&it, &key, &val)) {
175                 if (key == CSR_VENDOR)
176                         vendor = val;
177                 else if (key == CSR_MODEL)
178                         model = val;
179         }
180
181         /*
182          * Mackie Onyx Satellite with base station has a quirk to report a wrong
183          * value in 'dbs' field of CIP header against its format information.
184          */
185         if (vendor == VENDOR_LOUD && model == MODEL_SATELLITE)
186                 oxfw->wrong_dbs = true;
187
188         return 0;
189 }
190
191 static int oxfw_probe(struct fw_unit *unit,
192                       const struct ieee1394_device_id *entry)
193 {
194         struct snd_card *card;
195         struct snd_oxfw *oxfw;
196         int err;
197
198         if (entry->vendor_id == VENDOR_LOUD && !detect_loud_models(unit))
199                 return -ENODEV;
200
201         err = snd_card_new(&unit->device, -1, NULL, THIS_MODULE,
202                            sizeof(*oxfw), &card);
203         if (err < 0)
204                 return err;
205
206         card->private_free = oxfw_card_free;
207         oxfw = card->private_data;
208         oxfw->card = card;
209         mutex_init(&oxfw->mutex);
210         oxfw->unit = fw_unit_get(unit);
211         oxfw->entry = entry;
212         spin_lock_init(&oxfw->lock);
213         init_waitqueue_head(&oxfw->hwdep_wait);
214
215         err = snd_oxfw_stream_discover(oxfw);
216         if (err < 0)
217                 goto error;
218
219         err = detect_quirks(oxfw);
220         if (err < 0)
221                 goto error;
222
223         err = name_card(oxfw);
224         if (err < 0)
225                 goto error;
226
227         err = snd_oxfw_create_pcm(oxfw);
228         if (err < 0)
229                 goto error;
230
231         snd_oxfw_proc_init(oxfw);
232
233         err = snd_oxfw_create_midi(oxfw);
234         if (err < 0)
235                 goto error;
236
237         err = snd_oxfw_create_hwdep(oxfw);
238         if (err < 0)
239                 goto error;
240
241         err = snd_oxfw_stream_init_simplex(oxfw, &oxfw->rx_stream);
242         if (err < 0)
243                 goto error;
244         if (oxfw->has_output) {
245                 err = snd_oxfw_stream_init_simplex(oxfw, &oxfw->tx_stream);
246                 if (err < 0)
247                         goto error;
248         }
249
250         err = snd_card_register(card);
251         if (err < 0) {
252                 snd_oxfw_stream_destroy_simplex(oxfw, &oxfw->rx_stream);
253                 if (oxfw->has_output)
254                         snd_oxfw_stream_destroy_simplex(oxfw, &oxfw->tx_stream);
255                 goto error;
256         }
257         dev_set_drvdata(&unit->device, oxfw);
258
259         return 0;
260 error:
261         snd_card_free(card);
262         return err;
263 }
264
265 static void oxfw_bus_reset(struct fw_unit *unit)
266 {
267         struct snd_oxfw *oxfw = dev_get_drvdata(&unit->device);
268
269         fcp_bus_reset(oxfw->unit);
270
271         mutex_lock(&oxfw->mutex);
272
273         snd_oxfw_stream_update_simplex(oxfw, &oxfw->rx_stream);
274         if (oxfw->has_output)
275                 snd_oxfw_stream_update_simplex(oxfw, &oxfw->tx_stream);
276
277         mutex_unlock(&oxfw->mutex);
278 }
279
280 static void oxfw_remove(struct fw_unit *unit)
281 {
282         struct snd_oxfw *oxfw = dev_get_drvdata(&unit->device);
283
284         /* No need to wait for releasing card object in this context. */
285         snd_card_free_when_closed(oxfw->card);
286 }
287
288 static const struct compat_info griffin_firewave = {
289         .driver_name = "FireWave",
290         .vendor_name = "Griffin",
291         .model_name = "FireWave",
292 };
293
294 static const struct compat_info lacie_speakers = {
295         .driver_name = "FWSpeakers",
296         .vendor_name = "LaCie",
297         .model_name = "FireWire Speakers",
298 };
299
300 static const struct ieee1394_device_id oxfw_id_table[] = {
301         {
302                 .match_flags  = IEEE1394_MATCH_VENDOR_ID |
303                                 IEEE1394_MATCH_MODEL_ID |
304                                 IEEE1394_MATCH_SPECIFIER_ID |
305                                 IEEE1394_MATCH_VERSION,
306                 .vendor_id    = VENDOR_GRIFFIN,
307                 .model_id     = 0x00f970,
308                 .specifier_id = SPECIFIER_1394TA,
309                 .version      = VERSION_AVC,
310                 .driver_data  = (kernel_ulong_t)&griffin_firewave,
311         },
312         {
313                 .match_flags  = IEEE1394_MATCH_VENDOR_ID |
314                                 IEEE1394_MATCH_MODEL_ID |
315                                 IEEE1394_MATCH_SPECIFIER_ID |
316                                 IEEE1394_MATCH_VERSION,
317                 .vendor_id    = VENDOR_LACIE,
318                 .model_id     = 0x00f970,
319                 .specifier_id = SPECIFIER_1394TA,
320                 .version      = VERSION_AVC,
321                 .driver_data  = (kernel_ulong_t)&lacie_speakers,
322         },
323         /* Behringer,F-Control Audio 202 */
324         {
325                 .match_flags    = IEEE1394_MATCH_VENDOR_ID |
326                                   IEEE1394_MATCH_MODEL_ID,
327                 .vendor_id      = VENDOR_BEHRINGER,
328                 .model_id       = 0x00fc22,
329         },
330         /*
331          * Any Mackie(Loud) models (name string/model id):
332          *  Onyx-i series (former models):      0x081216
333          *  Mackie Onyx Satellite:              0x00200f
334          *  Tapco LINK.firewire 4x6:            0x000460
335          *  d.2 pro:                            Unknown
336          *  d.4 pro:                            Unknown
337          *  U.420:                              Unknown
338          *  U.420d:                             Unknown
339          */
340         {
341                 .match_flags    = IEEE1394_MATCH_VENDOR_ID |
342                                   IEEE1394_MATCH_SPECIFIER_ID |
343                                   IEEE1394_MATCH_VERSION,
344                 .vendor_id      = VENDOR_LOUD,
345                 .specifier_id   = SPECIFIER_1394TA,
346                 .version        = VERSION_AVC,
347         },
348         /* TASCAM, FireOne */
349         {
350                 .match_flags    = IEEE1394_MATCH_VENDOR_ID |
351                                   IEEE1394_MATCH_MODEL_ID,
352                 .vendor_id      = VENDOR_TASCAM,
353                 .model_id       = 0x800007,
354         },
355         { }
356 };
357 MODULE_DEVICE_TABLE(ieee1394, oxfw_id_table);
358
359 static struct fw_driver oxfw_driver = {
360         .driver   = {
361                 .owner  = THIS_MODULE,
362                 .name   = KBUILD_MODNAME,
363                 .bus    = &fw_bus_type,
364         },
365         .probe    = oxfw_probe,
366         .update   = oxfw_bus_reset,
367         .remove   = oxfw_remove,
368         .id_table = oxfw_id_table,
369 };
370
371 static int __init snd_oxfw_init(void)
372 {
373         return driver_register(&oxfw_driver.driver);
374 }
375
376 static void __exit snd_oxfw_exit(void)
377 {
378         driver_unregister(&oxfw_driver.driver);
379 }
380
381 module_init(snd_oxfw_init);
382 module_exit(snd_oxfw_exit);