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