Merge tag 'fsnotify_for_v5.2-rc1' of ssh://gitolite.kernel.org/pub/scm/linux/kernel...
[linux-2.6-block.git] / sound / firewire / digi00x / digi00x.c
CommitLineData
9edf723f
TS
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
11MODULE_DESCRIPTION("Digidesign Digi 002/003 family Driver");
12MODULE_AUTHOR("Takashi Sakamoto <o-takashi@sakamocchi.jp>");
13MODULE_LICENSE("GPL v2");
14
15#define VENDOR_DIGIDESIGN 0x00a07e
13e005f9
TS
16#define MODEL_CONSOLE 0x000001
17#define MODEL_RACK 0x000002
9edf723f
TS
18
19static 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,
bd04809b 38 fw_dev->config_rom[3], fw_dev->config_rom[4],
9edf723f
TS
39 dev_name(&dg00x->unit->device), 100 << fw_dev->max_speed);
40
41 return 0;
42}
43
3babca45 44static void dg00x_card_free(struct snd_card *card)
9edf723f 45{
3babca45
TS
46 struct snd_dg00x *dg00x = card->private_data;
47
3a2a1797 48 snd_dg00x_stream_destroy_duplex(dg00x);
44b73088 49 snd_dg00x_transaction_unregister(dg00x);
9edf723f
TS
50}
51
86c8dd7f
TS
52static void do_registration(struct work_struct *work)
53{
54 struct snd_dg00x *dg00x =
55 container_of(work, struct snd_dg00x, dwork.work);
56 int err;
9edf723f 57
86c8dd7f
TS
58 if (dg00x->registered)
59 return;
9edf723f 60
86c8dd7f
TS
61 err = snd_card_new(&dg00x->unit->device, -1, NULL, THIS_MODULE, 0,
62 &dg00x->card);
63 if (err < 0)
64 return;
3babca45
TS
65 dg00x->card->private_free = dg00x_card_free;
66 dg00x->card->private_data = dg00x;
9edf723f
TS
67
68 err = name_card(dg00x);
69 if (err < 0)
70 goto error;
71
3a2a1797
TS
72 err = snd_dg00x_stream_init_duplex(dg00x);
73 if (err < 0)
74 goto error;
75
927f17dc
TS
76 snd_dg00x_proc_init(dg00x);
77
0120d0f1
TS
78 err = snd_dg00x_create_pcm_devices(dg00x);
79 if (err < 0)
80 goto error;
81
9fbfd38b
TS
82 err = snd_dg00x_create_midi_devices(dg00x);
83 if (err < 0)
84 goto error;
85
660dd3d5
TS
86 err = snd_dg00x_create_hwdep_device(dg00x);
87 if (err < 0)
88 goto error;
89
44b73088
TS
90 err = snd_dg00x_transaction_register(dg00x);
91 if (err < 0)
92 goto error;
93
86c8dd7f 94 err = snd_card_register(dg00x->card);
9edf723f
TS
95 if (err < 0)
96 goto error;
97
86c8dd7f 98 dg00x->registered = true;
9edf723f 99
86c8dd7f 100 return;
9edf723f 101error:
86c8dd7f
TS
102 snd_card_free(dg00x->card);
103 dev_info(&dg00x->unit->device,
104 "Sound card registration failed: %d\n", err);
105}
106
107static int snd_dg00x_probe(struct fw_unit *unit,
108 const struct ieee1394_device_id *entry)
109{
110 struct snd_dg00x *dg00x;
111
112 /* Allocate this independent of sound card instance. */
366a20d7
TS
113 dg00x = devm_kzalloc(&unit->device, sizeof(struct snd_dg00x),
114 GFP_KERNEL);
115 if (!dg00x)
86c8dd7f
TS
116 return -ENOMEM;
117
118 dg00x->unit = fw_unit_get(unit);
119 dev_set_drvdata(&unit->device, dg00x);
120
121 mutex_init(&dg00x->mutex);
122 spin_lock_init(&dg00x->lock);
123 init_waitqueue_head(&dg00x->hwdep_wait);
124
13e005f9
TS
125 dg00x->is_console = entry->model_id == MODEL_CONSOLE;
126
86c8dd7f
TS
127 /* Allocate and register this sound card later. */
128 INIT_DEFERRABLE_WORK(&dg00x->dwork, do_registration);
129 snd_fw_schedule_registration(unit, &dg00x->dwork);
130
131 return 0;
9edf723f
TS
132}
133
134static void snd_dg00x_update(struct fw_unit *unit)
135{
3a2a1797
TS
136 struct snd_dg00x *dg00x = dev_get_drvdata(&unit->device);
137
86c8dd7f
TS
138 /* Postpone a workqueue for deferred registration. */
139 if (!dg00x->registered)
140 snd_fw_schedule_registration(unit, &dg00x->dwork);
141
44b73088
TS
142 snd_dg00x_transaction_reregister(dg00x);
143
86c8dd7f
TS
144 /*
145 * After registration, userspace can start packet streaming, then this
146 * code block works fine.
147 */
148 if (dg00x->registered) {
149 mutex_lock(&dg00x->mutex);
150 snd_dg00x_stream_update_duplex(dg00x);
151 mutex_unlock(&dg00x->mutex);
152 }
9edf723f
TS
153}
154
155static void snd_dg00x_remove(struct fw_unit *unit)
156{
157 struct snd_dg00x *dg00x = dev_get_drvdata(&unit->device);
158
86c8dd7f
TS
159 /*
160 * Confirm to stop the work for registration before the sound card is
161 * going to be released. The work is not scheduled again because bus
162 * reset handler is not called anymore.
163 */
164 cancel_delayed_work_sync(&dg00x->dwork);
165
166 if (dg00x->registered) {
61ccc6f6
TS
167 // Block till all of ALSA character devices are released.
168 snd_card_free(dg00x->card);
86c8dd7f 169 }
5b14ec25
TS
170
171 mutex_destroy(&dg00x->mutex);
172 fw_unit_put(dg00x->unit);
9edf723f
TS
173}
174
175static const struct ieee1394_device_id snd_dg00x_id_table[] = {
176 /* Both of 002/003 use the same ID. */
177 {
178 .match_flags = IEEE1394_MATCH_VENDOR_ID |
179 IEEE1394_MATCH_MODEL_ID,
180 .vendor_id = VENDOR_DIGIDESIGN,
13e005f9
TS
181 .model_id = MODEL_CONSOLE,
182 },
183 {
184 .match_flags = IEEE1394_MATCH_VENDOR_ID |
185 IEEE1394_MATCH_MODEL_ID,
186 .vendor_id = VENDOR_DIGIDESIGN,
187 .model_id = MODEL_RACK,
9edf723f
TS
188 },
189 {}
190};
191MODULE_DEVICE_TABLE(ieee1394, snd_dg00x_id_table);
192
193static struct fw_driver dg00x_driver = {
194 .driver = {
195 .owner = THIS_MODULE,
196 .name = "snd-firewire-digi00x",
197 .bus = &fw_bus_type,
198 },
199 .probe = snd_dg00x_probe,
200 .update = snd_dg00x_update,
201 .remove = snd_dg00x_remove,
202 .id_table = snd_dg00x_id_table,
203};
204
205static int __init snd_dg00x_init(void)
206{
207 return driver_register(&dg00x_driver.driver);
208}
209
210static void __exit snd_dg00x_exit(void)
211{
212 driver_unregister(&dg00x_driver.driver);
213}
214
215module_init(snd_dg00x_init);
216module_exit(snd_dg00x_exit);