xref: /openbmc/linux/sound/firewire/digi00x/digi00x.c (revision 36fe4655)
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 #define SPEC_VERSION		0x000001
18 
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 
44 static void dg00x_card_free(struct snd_card *card)
45 {
46 	struct snd_dg00x *dg00x = card->private_data;
47 
48 	snd_dg00x_stream_destroy_duplex(dg00x);
49 	snd_dg00x_transaction_unregister(dg00x);
50 }
51 
52 static 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;
57 
58 	if (dg00x->registered)
59 		return;
60 
61 	err = snd_card_new(&dg00x->unit->device, -1, NULL, THIS_MODULE, 0,
62 			   &dg00x->card);
63 	if (err < 0)
64 		return;
65 	dg00x->card->private_free = dg00x_card_free;
66 	dg00x->card->private_data = dg00x;
67 
68 	err = name_card(dg00x);
69 	if (err < 0)
70 		goto error;
71 
72 	err = snd_dg00x_stream_init_duplex(dg00x);
73 	if (err < 0)
74 		goto error;
75 
76 	snd_dg00x_proc_init(dg00x);
77 
78 	err = snd_dg00x_create_pcm_devices(dg00x);
79 	if (err < 0)
80 		goto error;
81 
82 	err = snd_dg00x_create_midi_devices(dg00x);
83 	if (err < 0)
84 		goto error;
85 
86 	err = snd_dg00x_create_hwdep_device(dg00x);
87 	if (err < 0)
88 		goto error;
89 
90 	err = snd_dg00x_transaction_register(dg00x);
91 	if (err < 0)
92 		goto error;
93 
94 	err = snd_card_register(dg00x->card);
95 	if (err < 0)
96 		goto error;
97 
98 	dg00x->registered = true;
99 
100 	return;
101 error:
102 	snd_card_free(dg00x->card);
103 	dev_info(&dg00x->unit->device,
104 		 "Sound card registration failed: %d\n", err);
105 }
106 
107 static 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. */
113 	dg00x = devm_kzalloc(&unit->device, sizeof(struct snd_dg00x),
114 			     GFP_KERNEL);
115 	if (!dg00x)
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 
125 	dg00x->is_console = entry->model_id == MODEL_CONSOLE;
126 
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;
132 }
133 
134 static void snd_dg00x_update(struct fw_unit *unit)
135 {
136 	struct snd_dg00x *dg00x = dev_get_drvdata(&unit->device);
137 
138 	/* Postpone a workqueue for deferred registration. */
139 	if (!dg00x->registered)
140 		snd_fw_schedule_registration(unit, &dg00x->dwork);
141 
142 	snd_dg00x_transaction_reregister(dg00x);
143 
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 	}
153 }
154 
155 static void snd_dg00x_remove(struct fw_unit *unit)
156 {
157 	struct snd_dg00x *dg00x = dev_get_drvdata(&unit->device);
158 
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) {
167 		// Block till all of ALSA character devices are released.
168 		snd_card_free(dg00x->card);
169 	}
170 
171 	mutex_destroy(&dg00x->mutex);
172 	fw_unit_put(dg00x->unit);
173 }
174 
175 static 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_VERSION |
180 			       IEEE1394_MATCH_MODEL_ID,
181 		.vendor_id = VENDOR_DIGIDESIGN,
182 		.version = SPEC_VERSION,
183 		.model_id = MODEL_CONSOLE,
184 	},
185 	{
186 		.match_flags = IEEE1394_MATCH_VENDOR_ID |
187 			       IEEE1394_MATCH_VERSION |
188 			       IEEE1394_MATCH_MODEL_ID,
189 		.vendor_id = VENDOR_DIGIDESIGN,
190 		.version = SPEC_VERSION,
191 		.model_id = MODEL_RACK,
192 	},
193 	{}
194 };
195 MODULE_DEVICE_TABLE(ieee1394, snd_dg00x_id_table);
196 
197 static struct fw_driver dg00x_driver = {
198 	.driver = {
199 		.owner = THIS_MODULE,
200 		.name = KBUILD_MODNAME,
201 		.bus = &fw_bus_type,
202 	},
203 	.probe    = snd_dg00x_probe,
204 	.update   = snd_dg00x_update,
205 	.remove   = snd_dg00x_remove,
206 	.id_table = snd_dg00x_id_table,
207 };
208 
209 static int __init snd_dg00x_init(void)
210 {
211 	return driver_register(&dg00x_driver.driver);
212 }
213 
214 static void __exit snd_dg00x_exit(void)
215 {
216 	driver_unregister(&dg00x_driver.driver);
217 }
218 
219 module_init(snd_dg00x_init);
220 module_exit(snd_dg00x_exit);
221