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