Skip to content

Commit 04a2c73

Browse files
takaswietiwai
authored andcommitted
ALSA: bebob: delayed registration of sound card
Some bebob based units tends to fail asynchronous communication when IEEE 1394 bus is under bus-reset state. When registering sound card instance at unit probe callback, userspace applications can be involved to the state. This commit postpones the registration till the bus is calm. Signed-off-by: Takashi Sakamoto <[email protected]> Signed-off-by: Takashi Iwai <[email protected]>
1 parent 923f92e commit 04a2c73

File tree

2 files changed

+131
-89
lines changed

2 files changed

+131
-89
lines changed

sound/firewire/bebob/bebob.c

Lines changed: 127 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,17 @@ name_device(struct snd_bebob *bebob)
126126
return err;
127127
}
128128

129+
static void bebob_free(struct snd_bebob *bebob)
130+
{
131+
snd_bebob_stream_destroy_duplex(bebob);
132+
fw_unit_put(bebob->unit);
133+
134+
kfree(bebob->maudio_special_quirk);
135+
136+
mutex_destroy(&bebob->mutex);
137+
kfree(bebob);
138+
}
139+
129140
/*
130141
* This module releases the FireWire unit data after all ALSA character devices
131142
* are released by applications. This is for releasing stream data or finishing
@@ -137,18 +148,11 @@ bebob_card_free(struct snd_card *card)
137148
{
138149
struct snd_bebob *bebob = card->private_data;
139150

140-
snd_bebob_stream_destroy_duplex(bebob);
141-
fw_unit_put(bebob->unit);
142-
143-
kfree(bebob->maudio_special_quirk);
144-
145-
if (bebob->card_index >= 0) {
146-
mutex_lock(&devices_mutex);
147-
clear_bit(bebob->card_index, devices_used);
148-
mutex_unlock(&devices_mutex);
149-
}
151+
mutex_lock(&devices_mutex);
152+
clear_bit(bebob->card_index, devices_used);
153+
mutex_unlock(&devices_mutex);
150154

151-
mutex_destroy(&bebob->mutex);
155+
bebob_free(card->private_data);
152156
}
153157

154158
static const struct snd_bebob_spec *
@@ -176,81 +180,57 @@ check_audiophile_booted(struct fw_unit *unit)
176180
return strncmp(name, "FW Audiophile Bootloader", 15) != 0;
177181
}
178182

179-
static int
180-
bebob_probe(struct fw_unit *unit,
181-
const struct ieee1394_device_id *entry)
183+
static void
184+
do_registration(struct work_struct *work)
182185
{
183-
struct snd_card *card;
184-
struct snd_bebob *bebob;
185-
const struct snd_bebob_spec *spec;
186+
struct snd_bebob *bebob =
187+
container_of(work, struct snd_bebob, dwork.work);
186188
unsigned int card_index;
187189
int err;
188190

191+
if (bebob->registered)
192+
return;
193+
189194
mutex_lock(&devices_mutex);
190195

191196
for (card_index = 0; card_index < SNDRV_CARDS; card_index++) {
192197
if (!test_bit(card_index, devices_used) && enable[card_index])
193198
break;
194199
}
195200
if (card_index >= SNDRV_CARDS) {
196-
err = -ENOENT;
197-
goto end;
201+
mutex_unlock(&devices_mutex);
202+
return;
198203
}
199204

200-
if ((entry->vendor_id == VEN_FOCUSRITE) &&
201-
(entry->model_id == MODEL_FOCUSRITE_SAFFIRE_BOTH))
202-
spec = get_saffire_spec(unit);
203-
else if ((entry->vendor_id == VEN_MAUDIO1) &&
204-
(entry->model_id == MODEL_MAUDIO_AUDIOPHILE_BOTH) &&
205-
!check_audiophile_booted(unit))
206-
spec = NULL;
207-
else
208-
spec = (const struct snd_bebob_spec *)entry->driver_data;
209-
210-
if (spec == NULL) {
211-
if ((entry->vendor_id == VEN_MAUDIO1) ||
212-
(entry->vendor_id == VEN_MAUDIO2))
213-
err = snd_bebob_maudio_load_firmware(unit);
214-
else
215-
err = -ENOSYS;
216-
goto end;
205+
err = snd_card_new(&bebob->unit->device, index[card_index],
206+
id[card_index], THIS_MODULE, 0, &bebob->card);
207+
if (err < 0) {
208+
mutex_unlock(&devices_mutex);
209+
return;
217210
}
218211

219-
err = snd_card_new(&unit->device, index[card_index], id[card_index],
220-
THIS_MODULE, sizeof(struct snd_bebob), &card);
221-
if (err < 0)
222-
goto end;
223-
bebob = card->private_data;
224-
bebob->card_index = card_index;
225-
set_bit(card_index, devices_used);
226-
card->private_free = bebob_card_free;
227-
228-
bebob->card = card;
229-
bebob->unit = fw_unit_get(unit);
230-
bebob->spec = spec;
231-
mutex_init(&bebob->mutex);
232-
spin_lock_init(&bebob->lock);
233-
init_waitqueue_head(&bebob->hwdep_wait);
234-
235212
err = name_device(bebob);
236213
if (err < 0)
237214
goto error;
238215

239-
if ((entry->vendor_id == VEN_MAUDIO1) &&
240-
(entry->model_id == MODEL_MAUDIO_FW1814))
241-
err = snd_bebob_maudio_special_discover(bebob, true);
242-
else if ((entry->vendor_id == VEN_MAUDIO1) &&
243-
(entry->model_id == MODEL_MAUDIO_PROJECTMIX))
244-
err = snd_bebob_maudio_special_discover(bebob, false);
245-
else
216+
if (bebob->spec == &maudio_special_spec) {
217+
if (bebob->entry->model_id == MODEL_MAUDIO_FW1814)
218+
err = snd_bebob_maudio_special_discover(bebob, true);
219+
else
220+
err = snd_bebob_maudio_special_discover(bebob, false);
221+
} else {
246222
err = snd_bebob_stream_discover(bebob);
223+
}
224+
if (err < 0)
225+
goto error;
226+
227+
err = snd_bebob_stream_init_duplex(bebob);
247228
if (err < 0)
248229
goto error;
249230

250231
snd_bebob_proc_init(bebob);
251232

252-
if ((bebob->midi_input_ports > 0) ||
253-
(bebob->midi_output_ports > 0)) {
233+
if (bebob->midi_input_ports > 0 || bebob->midi_output_ports > 0) {
254234
err = snd_bebob_create_midi_devices(bebob);
255235
if (err < 0)
256236
goto error;
@@ -264,16 +244,75 @@ bebob_probe(struct fw_unit *unit,
264244
if (err < 0)
265245
goto error;
266246

267-
err = snd_bebob_stream_init_duplex(bebob);
247+
err = snd_card_register(bebob->card);
268248
if (err < 0)
269249
goto error;
270250

271-
if (!bebob->maudio_special_quirk) {
272-
err = snd_card_register(card);
273-
if (err < 0) {
274-
snd_bebob_stream_destroy_duplex(bebob);
275-
goto error;
276-
}
251+
set_bit(card_index, devices_used);
252+
mutex_unlock(&devices_mutex);
253+
254+
/*
255+
* After registered, bebob instance can be released corresponding to
256+
* releasing the sound card instance.
257+
*/
258+
bebob->card->private_free = bebob_card_free;
259+
bebob->card->private_data = bebob;
260+
bebob->registered = true;
261+
262+
return;
263+
error:
264+
mutex_unlock(&devices_mutex);
265+
snd_bebob_stream_destroy_duplex(bebob);
266+
snd_card_free(bebob->card);
267+
dev_info(&bebob->unit->device,
268+
"Sound card registration failed: %d\n", err);
269+
}
270+
271+
static int
272+
bebob_probe(struct fw_unit *unit, const struct ieee1394_device_id *entry)
273+
{
274+
struct snd_bebob *bebob;
275+
const struct snd_bebob_spec *spec;
276+
277+
if (entry->vendor_id == VEN_FOCUSRITE &&
278+
entry->model_id == MODEL_FOCUSRITE_SAFFIRE_BOTH)
279+
spec = get_saffire_spec(unit);
280+
else if (entry->vendor_id == VEN_MAUDIO1 &&
281+
entry->model_id == MODEL_MAUDIO_AUDIOPHILE_BOTH &&
282+
!check_audiophile_booted(unit))
283+
spec = NULL;
284+
else
285+
spec = (const struct snd_bebob_spec *)entry->driver_data;
286+
287+
if (spec == NULL) {
288+
if (entry->vendor_id == VEN_MAUDIO1 ||
289+
entry->vendor_id == VEN_MAUDIO2)
290+
return snd_bebob_maudio_load_firmware(unit);
291+
else
292+
return -ENODEV;
293+
}
294+
295+
/* Allocate this independent of sound card instance. */
296+
bebob = kzalloc(sizeof(struct snd_bebob), GFP_KERNEL);
297+
if (bebob == NULL)
298+
return -ENOMEM;
299+
300+
bebob->unit = fw_unit_get(unit);
301+
bebob->entry = entry;
302+
bebob->spec = spec;
303+
dev_set_drvdata(&unit->device, bebob);
304+
305+
mutex_init(&bebob->mutex);
306+
spin_lock_init(&bebob->lock);
307+
init_waitqueue_head(&bebob->hwdep_wait);
308+
309+
/* Allocate and register this sound card later. */
310+
INIT_DEFERRABLE_WORK(&bebob->dwork, do_registration);
311+
312+
if (entry->vendor_id != VEN_MAUDIO1 ||
313+
(entry->model_id != MODEL_MAUDIO_FW1814 &&
314+
entry->model_id != MODEL_MAUDIO_PROJECTMIX)) {
315+
snd_fw_schedule_registration(unit, &bebob->dwork);
277316
} else {
278317
/*
279318
* This is a workaround. This bus reset seems to have an effect
@@ -285,19 +324,11 @@ bebob_probe(struct fw_unit *unit,
285324
* signals from dbus and starts I/Os. To avoid I/Os till the
286325
* future bus reset, registration is done in next update().
287326
*/
288-
bebob->deferred_registration = true;
289327
fw_schedule_bus_reset(fw_parent_device(bebob->unit)->card,
290328
false, true);
291329
}
292330

293-
dev_set_drvdata(&unit->device, bebob);
294-
end:
295-
mutex_unlock(&devices_mutex);
296-
return err;
297-
error:
298-
mutex_unlock(&devices_mutex);
299-
snd_card_free(card);
300-
return err;
331+
return 0;
301332
}
302333

303334
/*
@@ -324,15 +355,11 @@ bebob_update(struct fw_unit *unit)
324355
if (bebob == NULL)
325356
return;
326357

327-
fcp_bus_reset(bebob->unit);
328-
329-
if (bebob->deferred_registration) {
330-
if (snd_card_register(bebob->card) < 0) {
331-
snd_bebob_stream_destroy_duplex(bebob);
332-
snd_card_free(bebob->card);
333-
}
334-
bebob->deferred_registration = false;
335-
}
358+
/* Postpone a workqueue for deferred registration. */
359+
if (!bebob->registered)
360+
snd_fw_schedule_registration(unit, &bebob->dwork);
361+
else
362+
fcp_bus_reset(bebob->unit);
336363
}
337364

338365
static void bebob_remove(struct fw_unit *unit)
@@ -342,8 +369,20 @@ static void bebob_remove(struct fw_unit *unit)
342369
if (bebob == NULL)
343370
return;
344371

345-
/* No need to wait for releasing card object in this context. */
346-
snd_card_free_when_closed(bebob->card);
372+
/*
373+
* Confirm to stop the work for registration before the sound card is
374+
* going to be released. The work is not scheduled again because bus
375+
* reset handler is not called anymore.
376+
*/
377+
cancel_delayed_work_sync(&bebob->dwork);
378+
379+
if (bebob->registered) {
380+
/* No need to wait for releasing card object in this context. */
381+
snd_card_free_when_closed(bebob->card);
382+
} else {
383+
/* Don't forget this case. */
384+
bebob_free(bebob);
385+
}
347386
}
348387

349388
static const struct snd_bebob_rate_spec normal_rate_spec = {

sound/firewire/bebob/bebob.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ struct snd_bebob {
8383
struct mutex mutex;
8484
spinlock_t lock;
8585

86+
bool registered;
87+
struct delayed_work dwork;
88+
89+
const struct ieee1394_device_id *entry;
8690
const struct snd_bebob_spec *spec;
8791

8892
unsigned int midi_input_ports;
@@ -111,7 +115,6 @@ struct snd_bebob {
111115

112116
/* for M-Audio special devices */
113117
void *maudio_special_quirk;
114-
bool deferred_registration;
115118

116119
/* For BeBoB version quirk. */
117120
unsigned int version;

0 commit comments

Comments
 (0)