blob: cc4776c6ded313673ef1c767101f2ed7e0c4369f [file] [log] [blame]
Takashi Sakamoto9edf7232015-09-30 09:39:16 +09001/*
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
16#define MODEL_DIGI00X 0x000002
17
18static int name_card(struct snd_dg00x *dg00x)
19{
20 struct fw_device *fw_dev = fw_parent_device(dg00x->unit);
21 char name[32] = {0};
22 char *model;
23 int err;
24
25 err = fw_csr_string(dg00x->unit->directory, CSR_MODEL, name,
26 sizeof(name));
27 if (err < 0)
28 return err;
29
30 model = skip_spaces(name);
31
32 strcpy(dg00x->card->driver, "Digi00x");
33 strcpy(dg00x->card->shortname, model);
34 strcpy(dg00x->card->mixername, model);
35 snprintf(dg00x->card->longname, sizeof(dg00x->card->longname),
36 "Digidesign %s, GUID %08x%08x at %s, S%d", model,
Takashi Sakamotobd048092015-10-18 22:39:53 +090037 fw_dev->config_rom[3], fw_dev->config_rom[4],
Takashi Sakamoto9edf7232015-09-30 09:39:16 +090038 dev_name(&dg00x->unit->device), 100 << fw_dev->max_speed);
39
40 return 0;
41}
42
Takashi Sakamoto86c8dd72016-03-31 08:47:08 +090043static void dg00x_free(struct snd_dg00x *dg00x)
Takashi Sakamoto9edf7232015-09-30 09:39:16 +090044{
Takashi Sakamoto3a2a1792015-09-30 09:39:18 +090045 snd_dg00x_stream_destroy_duplex(dg00x);
Takashi Sakamoto44b73082015-09-30 09:39:22 +090046 snd_dg00x_transaction_unregister(dg00x);
Takashi Sakamoto3a2a1792015-09-30 09:39:18 +090047
Takashi Sakamoto9edf7232015-09-30 09:39:16 +090048 fw_unit_put(dg00x->unit);
49
50 mutex_destroy(&dg00x->mutex);
51}
52
Takashi Sakamoto86c8dd72016-03-31 08:47:08 +090053static void dg00x_card_free(struct snd_card *card)
Takashi Sakamoto9edf7232015-09-30 09:39:16 +090054{
Takashi Sakamoto86c8dd72016-03-31 08:47:08 +090055 dg00x_free(card->private_data);
56}
57
58static void do_registration(struct work_struct *work)
59{
60 struct snd_dg00x *dg00x =
61 container_of(work, struct snd_dg00x, dwork.work);
Takashi Sakamoto9edf7232015-09-30 09:39:16 +090062 int err;
63
Takashi Sakamoto86c8dd72016-03-31 08:47:08 +090064 if (dg00x->registered)
65 return;
66
67 err = snd_card_new(&dg00x->unit->device, -1, NULL, THIS_MODULE, 0,
68 &dg00x->card);
Takashi Sakamoto9edf7232015-09-30 09:39:16 +090069 if (err < 0)
Takashi Sakamoto86c8dd72016-03-31 08:47:08 +090070 return;
Takashi Sakamoto9edf7232015-09-30 09:39:16 +090071
72 err = name_card(dg00x);
73 if (err < 0)
74 goto error;
75
Takashi Sakamoto3a2a1792015-09-30 09:39:18 +090076 err = snd_dg00x_stream_init_duplex(dg00x);
77 if (err < 0)
78 goto error;
79
Takashi Sakamoto927f17d2015-09-30 09:39:19 +090080 snd_dg00x_proc_init(dg00x);
81
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +090082 err = snd_dg00x_create_pcm_devices(dg00x);
83 if (err < 0)
84 goto error;
85
Takashi Sakamoto9fbfd382015-10-11 12:30:16 +090086 err = snd_dg00x_create_midi_devices(dg00x);
87 if (err < 0)
88 goto error;
89
Takashi Sakamoto660dd3d2015-09-30 09:39:21 +090090 err = snd_dg00x_create_hwdep_device(dg00x);
91 if (err < 0)
92 goto error;
93
Takashi Sakamoto44b73082015-09-30 09:39:22 +090094 err = snd_dg00x_transaction_register(dg00x);
95 if (err < 0)
96 goto error;
97
Takashi Sakamoto86c8dd72016-03-31 08:47:08 +090098 err = snd_card_register(dg00x->card);
Takashi Sakamoto9edf7232015-09-30 09:39:16 +090099 if (err < 0)
100 goto error;
101
Takashi Sakamoto86c8dd72016-03-31 08:47:08 +0900102 dg00x->card->private_free = dg00x_card_free;
103 dg00x->card->private_data = dg00x;
104 dg00x->registered = true;
105
106 return;
107error:
108 snd_dg00x_transaction_unregister(dg00x);
109 snd_dg00x_stream_destroy_duplex(dg00x);
110 snd_card_free(dg00x->card);
111 dev_info(&dg00x->unit->device,
112 "Sound card registration failed: %d\n", err);
113}
114
115static int snd_dg00x_probe(struct fw_unit *unit,
116 const struct ieee1394_device_id *entry)
117{
118 struct snd_dg00x *dg00x;
119
120 /* Allocate this independent of sound card instance. */
121 dg00x = kzalloc(sizeof(struct snd_dg00x), GFP_KERNEL);
122 if (dg00x == NULL)
123 return -ENOMEM;
124
125 dg00x->unit = fw_unit_get(unit);
Takashi Sakamoto9edf7232015-09-30 09:39:16 +0900126 dev_set_drvdata(&unit->device, dg00x);
127
Takashi Sakamoto86c8dd72016-03-31 08:47:08 +0900128 mutex_init(&dg00x->mutex);
129 spin_lock_init(&dg00x->lock);
130 init_waitqueue_head(&dg00x->hwdep_wait);
131
132 /* Allocate and register this sound card later. */
133 INIT_DEFERRABLE_WORK(&dg00x->dwork, do_registration);
134 snd_fw_schedule_registration(unit, &dg00x->dwork);
135
136 return 0;
Takashi Sakamoto9edf7232015-09-30 09:39:16 +0900137}
138
139static void snd_dg00x_update(struct fw_unit *unit)
140{
Takashi Sakamoto3a2a1792015-09-30 09:39:18 +0900141 struct snd_dg00x *dg00x = dev_get_drvdata(&unit->device);
142
Takashi Sakamoto86c8dd72016-03-31 08:47:08 +0900143 /* Postpone a workqueue for deferred registration. */
144 if (!dg00x->registered)
145 snd_fw_schedule_registration(unit, &dg00x->dwork);
146
Takashi Sakamoto44b73082015-09-30 09:39:22 +0900147 snd_dg00x_transaction_reregister(dg00x);
148
Takashi Sakamoto86c8dd72016-03-31 08:47:08 +0900149 /*
150 * After registration, userspace can start packet streaming, then this
151 * code block works fine.
152 */
153 if (dg00x->registered) {
154 mutex_lock(&dg00x->mutex);
155 snd_dg00x_stream_update_duplex(dg00x);
156 mutex_unlock(&dg00x->mutex);
157 }
Takashi Sakamoto9edf7232015-09-30 09:39:16 +0900158}
159
160static void snd_dg00x_remove(struct fw_unit *unit)
161{
162 struct snd_dg00x *dg00x = dev_get_drvdata(&unit->device);
163
Takashi Sakamoto86c8dd72016-03-31 08:47:08 +0900164 /*
165 * Confirm to stop the work for registration before the sound card is
166 * going to be released. The work is not scheduled again because bus
167 * reset handler is not called anymore.
168 */
169 cancel_delayed_work_sync(&dg00x->dwork);
170
171 if (dg00x->registered) {
172 /* No need to wait for releasing card object in this context. */
173 snd_card_free_when_closed(dg00x->card);
174 } else {
175 /* Don't forget this case. */
176 dg00x_free(dg00x);
177 }
Takashi Sakamoto9edf7232015-09-30 09:39:16 +0900178}
179
180static const struct ieee1394_device_id snd_dg00x_id_table[] = {
181 /* Both of 002/003 use the same ID. */
182 {
183 .match_flags = IEEE1394_MATCH_VENDOR_ID |
184 IEEE1394_MATCH_MODEL_ID,
185 .vendor_id = VENDOR_DIGIDESIGN,
186 .model_id = MODEL_DIGI00X,
187 },
188 {}
189};
190MODULE_DEVICE_TABLE(ieee1394, snd_dg00x_id_table);
191
192static struct fw_driver dg00x_driver = {
193 .driver = {
194 .owner = THIS_MODULE,
195 .name = "snd-firewire-digi00x",
196 .bus = &fw_bus_type,
197 },
198 .probe = snd_dg00x_probe,
199 .update = snd_dg00x_update,
200 .remove = snd_dg00x_remove,
201 .id_table = snd_dg00x_id_table,
202};
203
204static int __init snd_dg00x_init(void)
205{
206 return driver_register(&dg00x_driver.driver);
207}
208
209static void __exit snd_dg00x_exit(void)
210{
211 driver_unregister(&dg00x_driver.driver);
212}
213
214module_init(snd_dg00x_init);
215module_exit(snd_dg00x_exit);