GNU Linux-libre 4.19.264-gnu1
[releases.git] / sound / firewire / digi00x / digi00x.c
1 /*
2  * digi00x.c - a part of driver for Digidesign Digi 002/003 family
3  *
4  * Copyright (c) 2014-2015 Takashi Sakamoto
5  *
6  * Licensed under the terms of the GNU General Public License, version 2.
7  */
8
9 #include "digi00x.h"
10
11 MODULE_DESCRIPTION("Digidesign Digi 002/003 family Driver");
12 MODULE_AUTHOR("Takashi Sakamoto <o-takashi@sakamocchi.jp>");
13 MODULE_LICENSE("GPL v2");
14
15 #define VENDOR_DIGIDESIGN       0x00a07e
16 #define MODEL_CONSOLE           0x000001
17 #define MODEL_RACK              0x000002
18 #define SPEC_VERSION            0x000001
19
20 static int name_card(struct snd_dg00x *dg00x)
21 {
22         struct fw_device *fw_dev = fw_parent_device(dg00x->unit);
23         char name[32] = {0};
24         char *model;
25         int err;
26
27         err = fw_csr_string(dg00x->unit->directory, CSR_MODEL, name,
28                             sizeof(name));
29         if (err < 0)
30                 return err;
31
32         model = skip_spaces(name);
33
34         strcpy(dg00x->card->driver, "Digi00x");
35         strcpy(dg00x->card->shortname, model);
36         strcpy(dg00x->card->mixername, model);
37         snprintf(dg00x->card->longname, sizeof(dg00x->card->longname),
38                  "Digidesign %s, GUID %08x%08x at %s, S%d", model,
39                  fw_dev->config_rom[3], fw_dev->config_rom[4],
40                  dev_name(&dg00x->unit->device), 100 << fw_dev->max_speed);
41
42         return 0;
43 }
44
45 static void dg00x_free(struct snd_dg00x *dg00x)
46 {
47         snd_dg00x_stream_destroy_duplex(dg00x);
48         snd_dg00x_transaction_unregister(dg00x);
49
50         fw_unit_put(dg00x->unit);
51
52         mutex_destroy(&dg00x->mutex);
53         kfree(dg00x);
54 }
55
56 static void dg00x_card_free(struct snd_card *card)
57 {
58         dg00x_free(card->private_data);
59 }
60
61 static void do_registration(struct work_struct *work)
62 {
63         struct snd_dg00x *dg00x =
64                         container_of(work, struct snd_dg00x, dwork.work);
65         int err;
66
67         if (dg00x->registered)
68                 return;
69
70         err = snd_card_new(&dg00x->unit->device, -1, NULL, THIS_MODULE, 0,
71                            &dg00x->card);
72         if (err < 0)
73                 return;
74
75         err = name_card(dg00x);
76         if (err < 0)
77                 goto error;
78
79         err = snd_dg00x_stream_init_duplex(dg00x);
80         if (err < 0)
81                 goto error;
82
83         snd_dg00x_proc_init(dg00x);
84
85         err = snd_dg00x_create_pcm_devices(dg00x);
86         if (err < 0)
87                 goto error;
88
89         err = snd_dg00x_create_midi_devices(dg00x);
90         if (err < 0)
91                 goto error;
92
93         err = snd_dg00x_create_hwdep_device(dg00x);
94         if (err < 0)
95                 goto error;
96
97         err = snd_dg00x_transaction_register(dg00x);
98         if (err < 0)
99                 goto error;
100
101         err = snd_card_register(dg00x->card);
102         if (err < 0)
103                 goto error;
104
105         dg00x->card->private_free = dg00x_card_free;
106         dg00x->card->private_data = dg00x;
107         dg00x->registered = true;
108
109         return;
110 error:
111         snd_dg00x_transaction_unregister(dg00x);
112         snd_dg00x_stream_destroy_duplex(dg00x);
113         snd_card_free(dg00x->card);
114         dev_info(&dg00x->unit->device,
115                  "Sound card registration failed: %d\n", err);
116 }
117
118 static int snd_dg00x_probe(struct fw_unit *unit,
119                            const struct ieee1394_device_id *entry)
120 {
121         struct snd_dg00x *dg00x;
122
123         /* Allocate this independent of sound card instance. */
124         dg00x = kzalloc(sizeof(struct snd_dg00x), GFP_KERNEL);
125         if (dg00x == NULL)
126                 return -ENOMEM;
127
128         dg00x->unit = fw_unit_get(unit);
129         dev_set_drvdata(&unit->device, dg00x);
130
131         mutex_init(&dg00x->mutex);
132         spin_lock_init(&dg00x->lock);
133         init_waitqueue_head(&dg00x->hwdep_wait);
134
135         dg00x->is_console = entry->model_id == MODEL_CONSOLE;
136
137         /* Allocate and register this sound card later. */
138         INIT_DEFERRABLE_WORK(&dg00x->dwork, do_registration);
139         snd_fw_schedule_registration(unit, &dg00x->dwork);
140
141         return 0;
142 }
143
144 static void snd_dg00x_update(struct fw_unit *unit)
145 {
146         struct snd_dg00x *dg00x = dev_get_drvdata(&unit->device);
147
148         /* Postpone a workqueue for deferred registration. */
149         if (!dg00x->registered)
150                 snd_fw_schedule_registration(unit, &dg00x->dwork);
151
152         snd_dg00x_transaction_reregister(dg00x);
153
154         /*
155          * After registration, userspace can start packet streaming, then this
156          * code block works fine.
157          */
158         if (dg00x->registered) {
159                 mutex_lock(&dg00x->mutex);
160                 snd_dg00x_stream_update_duplex(dg00x);
161                 mutex_unlock(&dg00x->mutex);
162         }
163 }
164
165 static void snd_dg00x_remove(struct fw_unit *unit)
166 {
167         struct snd_dg00x *dg00x = dev_get_drvdata(&unit->device);
168
169         /*
170          * Confirm to stop the work for registration before the sound card is
171          * going to be released. The work is not scheduled again because bus
172          * reset handler is not called anymore.
173          */
174         cancel_delayed_work_sync(&dg00x->dwork);
175
176         if (dg00x->registered) {
177                 /* No need to wait for releasing card object in this context. */
178                 snd_card_free_when_closed(dg00x->card);
179         } else {
180                 /* Don't forget this case. */
181                 dg00x_free(dg00x);
182         }
183 }
184
185 static const struct ieee1394_device_id snd_dg00x_id_table[] = {
186         /* Both of 002/003 use the same ID. */
187         {
188                 .match_flags = IEEE1394_MATCH_VENDOR_ID |
189                                IEEE1394_MATCH_VERSION |
190                                IEEE1394_MATCH_MODEL_ID,
191                 .vendor_id = VENDOR_DIGIDESIGN,
192                 .version = SPEC_VERSION,
193                 .model_id = MODEL_CONSOLE,
194         },
195         {
196                 .match_flags = IEEE1394_MATCH_VENDOR_ID |
197                                IEEE1394_MATCH_VERSION |
198                                IEEE1394_MATCH_MODEL_ID,
199                 .vendor_id = VENDOR_DIGIDESIGN,
200                 .version = SPEC_VERSION,
201                 .model_id = MODEL_RACK,
202         },
203         {}
204 };
205 MODULE_DEVICE_TABLE(ieee1394, snd_dg00x_id_table);
206
207 static struct fw_driver dg00x_driver = {
208         .driver = {
209                 .owner = THIS_MODULE,
210                 .name = "snd-firewire-digi00x",
211                 .bus = &fw_bus_type,
212         },
213         .probe    = snd_dg00x_probe,
214         .update   = snd_dg00x_update,
215         .remove   = snd_dg00x_remove,
216         .id_table = snd_dg00x_id_table,
217 };
218
219 static int __init snd_dg00x_init(void)
220 {
221         return driver_register(&dg00x_driver.driver);
222 }
223
224 static void __exit snd_dg00x_exit(void)
225 {
226         driver_unregister(&dg00x_driver.driver);
227 }
228
229 module_init(snd_dg00x_init);
230 module_exit(snd_dg00x_exit);