xref: /openbmc/linux/sound/firewire/digi00x/digi00x.c (revision 4f6cce39)
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_DIGI00X		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_free(struct snd_dg00x *dg00x)
44 {
45 	snd_dg00x_stream_destroy_duplex(dg00x);
46 	snd_dg00x_transaction_unregister(dg00x);
47 
48 	fw_unit_put(dg00x->unit);
49 
50 	mutex_destroy(&dg00x->mutex);
51 }
52 
53 static void dg00x_card_free(struct snd_card *card)
54 {
55 	dg00x_free(card->private_data);
56 }
57 
58 static void do_registration(struct work_struct *work)
59 {
60 	struct snd_dg00x *dg00x =
61 			container_of(work, struct snd_dg00x, dwork.work);
62 	int err;
63 
64 	if (dg00x->registered)
65 		return;
66 
67 	err = snd_card_new(&dg00x->unit->device, -1, NULL, THIS_MODULE, 0,
68 			   &dg00x->card);
69 	if (err < 0)
70 		return;
71 
72 	err = name_card(dg00x);
73 	if (err < 0)
74 		goto error;
75 
76 	err = snd_dg00x_stream_init_duplex(dg00x);
77 	if (err < 0)
78 		goto error;
79 
80 	snd_dg00x_proc_init(dg00x);
81 
82 	err = snd_dg00x_create_pcm_devices(dg00x);
83 	if (err < 0)
84 		goto error;
85 
86 	err = snd_dg00x_create_midi_devices(dg00x);
87 	if (err < 0)
88 		goto error;
89 
90 	err = snd_dg00x_create_hwdep_device(dg00x);
91 	if (err < 0)
92 		goto error;
93 
94 	err = snd_dg00x_transaction_register(dg00x);
95 	if (err < 0)
96 		goto error;
97 
98 	err = snd_card_register(dg00x->card);
99 	if (err < 0)
100 		goto error;
101 
102 	dg00x->card->private_free = dg00x_card_free;
103 	dg00x->card->private_data = dg00x;
104 	dg00x->registered = true;
105 
106 	return;
107 error:
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 
115 static 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);
126 	dev_set_drvdata(&unit->device, dg00x);
127 
128 	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;
137 }
138 
139 static void snd_dg00x_update(struct fw_unit *unit)
140 {
141 	struct snd_dg00x *dg00x = dev_get_drvdata(&unit->device);
142 
143 	/* Postpone a workqueue for deferred registration. */
144 	if (!dg00x->registered)
145 		snd_fw_schedule_registration(unit, &dg00x->dwork);
146 
147 	snd_dg00x_transaction_reregister(dg00x);
148 
149 	/*
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 	}
158 }
159 
160 static void snd_dg00x_remove(struct fw_unit *unit)
161 {
162 	struct snd_dg00x *dg00x = dev_get_drvdata(&unit->device);
163 
164 	/*
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 	}
178 }
179 
180 static 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 };
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 = "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 
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