ff.c 3.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * ff.c - a part of driver for RME Fireface series
 *
 * Copyright (c) 2015-2017 Takashi Sakamoto
 *
 * Licensed under the terms of the GNU General Public License, version 2.
 */

#include "ff.h"

#define OUI_RME	0x000a35

MODULE_DESCRIPTION("RME Fireface series Driver");
MODULE_AUTHOR("Takashi Sakamoto <o-takashi@sakamocchi.jp>");
MODULE_LICENSE("GPL v2");

static void name_card(struct snd_ff *ff)
{
	struct fw_device *fw_dev = fw_parent_device(ff->unit);

	strcpy(ff->card->driver, "Fireface");
22 23
	strcpy(ff->card->shortname, ff->spec->name);
	strcpy(ff->card->mixername, ff->spec->name);
24
	snprintf(ff->card->longname, sizeof(ff->card->longname),
25
		 "RME %s, GUID %08x%08x at %s, S%d", ff->spec->name,
26 27 28 29
		 fw_dev->config_rom[3], fw_dev->config_rom[4],
		 dev_name(&ff->unit->device), 100 << fw_dev->max_speed);
}

30
static void ff_free(struct snd_ff *ff)
31
{
32
	snd_ff_stream_destroy_duplex(ff);
33 34
	snd_ff_transaction_unregister(ff);

35 36 37
	fw_unit_put(ff->unit);

	mutex_destroy(&ff->mutex);
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
	kfree(ff);
}

static void ff_card_free(struct snd_card *card)
{
	ff_free(card->private_data);
}

static void do_registration(struct work_struct *work)
{
	struct snd_ff *ff = container_of(work, struct snd_ff, dwork.work);
	int err;

	if (ff->registered)
		return;

	err = snd_card_new(&ff->unit->device, -1, NULL, THIS_MODULE, 0,
			   &ff->card);
	if (err < 0)
		return;

59 60 61 62
	err = snd_ff_transaction_register(ff);
	if (err < 0)
		goto error;

63 64
	name_card(ff);

65 66 67 68
	err = snd_ff_stream_init_duplex(ff);
	if (err < 0)
		goto error;

69 70
	snd_ff_proc_init(ff);

71 72 73 74
	err = snd_ff_create_midi_devices(ff);
	if (err < 0)
		goto error;

75 76 77 78
	err = snd_ff_create_pcm_devices(ff);
	if (err < 0)
		goto error;

79 80 81 82 83 84 85 86 87 88
	err = snd_card_register(ff->card);
	if (err < 0)
		goto error;

	ff->card->private_free = ff_card_free;
	ff->card->private_data = ff;
	ff->registered = true;

	return;
error:
89
	snd_ff_transaction_unregister(ff);
90
	snd_ff_stream_destroy_duplex(ff);
91 92 93
	snd_card_free(ff->card);
	dev_info(&ff->unit->device,
		 "Sound card registration failed: %d\n", err);
94 95 96 97 98 99 100
}

static int snd_ff_probe(struct fw_unit *unit,
			   const struct ieee1394_device_id *entry)
{
	struct snd_ff *ff;

101 102 103
	ff = kzalloc(sizeof(struct snd_ff), GFP_KERNEL);
	if (ff == NULL)
		return -ENOMEM;
104 105 106 107 108 109

	/* initialize myself */
	ff->unit = fw_unit_get(unit);
	dev_set_drvdata(&unit->device, ff);

	mutex_init(&ff->mutex);
110
	spin_lock_init(&ff->lock);
111

112 113
	ff->spec = (const struct snd_ff_spec *)entry->driver_data;

114 115 116
	/* Register this sound card later. */
	INIT_DEFERRABLE_WORK(&ff->dwork, do_registration);
	snd_fw_schedule_registration(unit, &ff->dwork);
117 118 119 120 121 122

	return 0;
}

static void snd_ff_update(struct fw_unit *unit)
{
123 124 125 126 127
	struct snd_ff *ff = dev_get_drvdata(&unit->device);

	/* Postpone a workqueue for deferred registration. */
	if (!ff->registered)
		snd_fw_schedule_registration(unit, &ff->dwork);
128 129

	snd_ff_transaction_reregister(ff);
130 131 132

	if (ff->registered)
		snd_ff_stream_update_duplex(ff);
133 134 135 136 137 138
}

static void snd_ff_remove(struct fw_unit *unit)
{
	struct snd_ff *ff = dev_get_drvdata(&unit->device);

139 140 141 142 143 144 145 146 147 148 149 150 151 152
	/*
	 * Confirm to stop the work for registration before the sound card is
	 * going to be released. The work is not scheduled again because bus
	 * reset handler is not called anymore.
	 */
	cancel_work_sync(&ff->dwork.work);

	if (ff->registered) {
		/* No need to wait for releasing card object in this context. */
		snd_card_free_when_closed(ff->card);
	} else {
		/* Don't forget this case. */
		ff_free(ff);
	}
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
}

static const struct ieee1394_device_id snd_ff_id_table[] = {
	{}
};
MODULE_DEVICE_TABLE(ieee1394, snd_ff_id_table);

static struct fw_driver ff_driver = {
	.driver = {
		.owner	= THIS_MODULE,
		.name	= "snd-fireface",
		.bus	= &fw_bus_type,
	},
	.probe    = snd_ff_probe,
	.update   = snd_ff_update,
	.remove   = snd_ff_remove,
	.id_table = snd_ff_id_table,
};

static int __init snd_ff_init(void)
{
	return driver_register(&ff_driver.driver);
}

static void __exit snd_ff_exit(void)
{
	driver_unregister(&ff_driver.driver);
}

module_init(snd_ff_init);
module_exit(snd_ff_exit);