summaryrefslogtreecommitdiff
path: root/sound/drivers/serial-generic.c
blob: e1f864dc7939a7c0c51fa384e209f5cfeb6ee78b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 *   serial-generic.c
 *   Copyright (c) by Daniel Kaehn <kaehndan@gmail.com
 *   Based on serial-u16550.c by Jaroslav Kysela <perex@perex.cz>,
 *		                 Isaku Yamahata <yamahata@private.email.ne.jp>,
 *		                 George Hansper <ghansper@apana.org.au>,
 *		                 Hannu Savolainen
 *
 * Generic serial MIDI driver using the serdev serial bus API for hardware interaction
 */

#include <linux/err.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/ioport.h>
#include <linux/module.h>
#include <linux/of_device.h>
#include <linux/serdev.h>
#include <linux/serial_reg.h>
#include <linux/slab.h>
#include <linux/dev_printk.h>

#include <sound/core.h>
#include <sound/rawmidi.h>
#include <sound/initval.h>

MODULE_DESCRIPTION("Generic serial MIDI driver");
MODULE_LICENSE("GPL");

#define SERIAL_MODE_INPUT_OPEN		1
#define SERIAL_MODE_OUTPUT_OPEN	2
#define SERIAL_MODE_INPUT_TRIGGERED	3
#define SERIAL_MODE_OUTPUT_TRIGGERED	4

#define SERIAL_TX_STATE_ACTIVE	1
#define SERIAL_TX_STATE_WAKEUP	2

struct snd_serial_generic {
	struct serdev_device *serdev;

	struct snd_card *card;
	struct snd_rawmidi *rmidi;
	struct snd_rawmidi_substream *midi_output;
	struct snd_rawmidi_substream *midi_input;

	unsigned int baudrate;

	unsigned long filemode;		/* open status of file */
	struct work_struct tx_work;
	unsigned long tx_state;

};

static void snd_serial_generic_tx_wakeup(struct snd_serial_generic *drvdata)
{
	if (test_and_set_bit(SERIAL_TX_STATE_ACTIVE, &drvdata->tx_state))
		set_bit(SERIAL_TX_STATE_WAKEUP, &drvdata->tx_state);

	schedule_work(&drvdata->tx_work);
}

#define INTERNAL_BUF_SIZE 256

static void snd_serial_generic_tx_work(struct work_struct *work)
{
	static char buf[INTERNAL_BUF_SIZE];
	int num_bytes;
	struct snd_serial_generic *drvdata = container_of(work, struct snd_serial_generic,
						   tx_work);
	struct snd_rawmidi_substream *substream = drvdata->midi_output;

	clear_bit(SERIAL_TX_STATE_WAKEUP, &drvdata->tx_state);

	while (!snd_rawmidi_transmit_empty(substream)) {

		if (!test_bit(SERIAL_MODE_OUTPUT_OPEN, &drvdata->filemode))
			break;

		num_bytes = snd_rawmidi_transmit_peek(substream, buf, INTERNAL_BUF_SIZE);
		num_bytes = serdev_device_write_buf(drvdata->serdev, buf, num_bytes);

		if (!num_bytes)
			break;

		snd_rawmidi_transmit_ack(substream, num_bytes);

		if (!test_bit(SERIAL_TX_STATE_WAKEUP, &drvdata->tx_state))
			break;
	}

	clear_bit(SERIAL_TX_STATE_ACTIVE, &drvdata->tx_state);
}

static void snd_serial_generic_write_wakeup(struct serdev_device *serdev)
{
	struct snd_serial_generic *drvdata = serdev_device_get_drvdata(serdev);

	snd_serial_generic_tx_wakeup(drvdata);
}

static int snd_serial_generic_receive_buf(struct serdev_device *serdev,
				const unsigned char *buf, size_t count)
{
	int ret;
	struct snd_serial_generic *drvdata = serdev_device_get_drvdata(serdev);

	if (!test_bit(SERIAL_MODE_INPUT_OPEN, &drvdata->filemode))
		return 0;

	ret = snd_rawmidi_receive(drvdata->midi_input, buf, count);
	return ret < 0 ? 0 : ret;
}

static const struct serdev_device_ops snd_serial_generic_serdev_device_ops = {
	.receive_buf = snd_serial_generic_receive_buf,
	.write_wakeup = snd_serial_generic_write_wakeup
};

static int snd_serial_generic_ensure_serdev_open(struct snd_serial_generic *drvdata)
{
	int err;
	unsigned int actual_baud;

	if (drvdata->filemode)
		return 0;

	dev_dbg(drvdata->card->dev, "Opening serial port for card %s\n",
		drvdata->card->shortname);
	err = serdev_device_open(drvdata->serdev);
	if (err < 0)
		return err;

	actual_baud = serdev_device_set_baudrate(drvdata->serdev,
		drvdata->baudrate);
	if (actual_baud != drvdata->baudrate) {
		dev_warn(drvdata->card->dev, "requested %d baud for card %s but it was actually set to %d\n",
			drvdata->baudrate, drvdata->card->shortname, actual_baud);
	}

	return 0;
}

static int snd_serial_generic_input_open(struct snd_rawmidi_substream *substream)
{
	int err;
	struct snd_serial_generic *drvdata = substream->rmidi->card->private_data;

	dev_dbg(drvdata->card->dev, "Opening input for card %s\n",
		drvdata->card->shortname);

	err = snd_serial_generic_ensure_serdev_open(drvdata);
	if (err < 0)
		return err;

	set_bit(SERIAL_MODE_INPUT_OPEN, &drvdata->filemode);
	drvdata->midi_input = substream;
	return 0;
}

static int snd_serial_generic_input_close(struct snd_rawmidi_substream *substream)
{
	struct snd_serial_generic *drvdata = substream->rmidi->card->private_data;

	dev_dbg(drvdata->card->dev, "Closing input for card %s\n",
		drvdata->card->shortname);

	clear_bit(SERIAL_MODE_INPUT_OPEN, &drvdata->filemode);
	clear_bit(SERIAL_MODE_INPUT_TRIGGERED, &drvdata->filemode);

	drvdata->midi_input = NULL;

	if (!drvdata->filemode)
		serdev_device_close(drvdata->serdev);
	return 0;
}

static void snd_serial_generic_input_trigger(struct snd_rawmidi_substream *substream,
					int up)
{
	struct snd_serial_generic *drvdata = substream->rmidi->card->private_data;

	if (up)
		set_bit(SERIAL_MODE_INPUT_TRIGGERED, &drvdata->filemode);
	else
		clear_bit(SERIAL_MODE_INPUT_TRIGGERED, &drvdata->filemode);
}

static int snd_serial_generic_output_open(struct snd_rawmidi_substream *substream)
{
	struct snd_serial_generic *drvdata = substream->rmidi->card->private_data;
	int err;

	dev_dbg(drvdata->card->dev, "Opening output for card %s\n",
		drvdata->card->shortname);

	err = snd_serial_generic_ensure_serdev_open(drvdata);
	if (err < 0)
		return err;

	set_bit(SERIAL_MODE_OUTPUT_OPEN, &drvdata->filemode);

	drvdata->midi_output = substream;
	return 0;
};

static int snd_serial_generic_output_close(struct snd_rawmidi_substream *substream)
{
	struct snd_serial_generic *drvdata = substream->rmidi->card->private_data;

	dev_dbg(drvdata->card->dev, "Closing output for card %s\n",
		drvdata->card->shortname);

	clear_bit(SERIAL_MODE_OUTPUT_OPEN, &drvdata->filemode);
	clear_bit(SERIAL_MODE_OUTPUT_TRIGGERED, &drvdata->filemode);

	if (!drvdata->filemode)
		serdev_device_close(drvdata->serdev);

	drvdata->midi_output = NULL;

	return 0;
};

static void snd_serial_generic_output_trigger(struct snd_rawmidi_substream *substream,
					 int up)
{
	struct snd_serial_generic *drvdata = substream->rmidi->card->private_data;

	if (up)
		set_bit(SERIAL_MODE_OUTPUT_TRIGGERED, &drvdata->filemode);
	else
		clear_bit(SERIAL_MODE_OUTPUT_TRIGGERED, &drvdata->filemode);

	if (up)
		snd_serial_generic_tx_wakeup(drvdata);
}

static void snd_serial_generic_output_drain(struct snd_rawmidi_substream *substream)
{
	struct snd_serial_generic *drvdata = substream->rmidi->card->private_data;

	/* Flush any pending characters */
	serdev_device_write_flush(drvdata->serdev);
	cancel_work_sync(&drvdata->tx_work);
}

static const struct snd_rawmidi_ops snd_serial_generic_output = {
	.open =		snd_serial_generic_output_open,
	.close =	snd_serial_generic_output_close,
	.trigger =	snd_serial_generic_output_trigger,
	.drain =	snd_serial_generic_output_drain,
};

static const struct snd_rawmidi_ops snd_serial_generic_input = {
	.open =		snd_serial_generic_input_open,
	.close =	snd_serial_generic_input_close,
	.trigger =	snd_serial_generic_input_trigger,
};

static void snd_serial_generic_parse_dt(struct serdev_device *serdev,
				struct snd_serial_generic *drvdata)
{
	int err;

	err = of_property_read_u32(serdev->dev.of_node, "current-speed",
		&drvdata->baudrate);
	if (err < 0) {
		dev_dbg(drvdata->card->dev,
			"MIDI device reading of current-speed DT param failed with error %d, using default of 38400\n",
			err);
		drvdata->baudrate = 38400;
	}

}

static void snd_serial_generic_substreams(struct snd_rawmidi_str *stream, int dev_num)
{
	struct snd_rawmidi_substream *substream;

	list_for_each_entry(substream, &stream->substreams, list) {
		sprintf(substream->name, "Serial MIDI %d-%d", dev_num, substream->number);
	}
}

static int snd_serial_generic_rmidi(struct snd_serial_generic *drvdata,
				int outs, int ins, struct snd_rawmidi **rmidi)
{
	struct snd_rawmidi *rrawmidi;
	int err;

	err = snd_rawmidi_new(drvdata->card, drvdata->card->driver, 0,
				outs, ins, &rrawmidi);

	if (err < 0)
		return err;

	snd_rawmidi_set_ops(rrawmidi, SNDRV_RAWMIDI_STREAM_INPUT,
				&snd_serial_generic_input);
	snd_rawmidi_set_ops(rrawmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
				&snd_serial_generic_output);
	strcpy(rrawmidi->name, drvdata->card->shortname);

	snd_serial_generic_substreams(&rrawmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
					drvdata->serdev->ctrl->nr);
	snd_serial_generic_substreams(&rrawmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
					drvdata->serdev->ctrl->nr);

	rrawmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
			       SNDRV_RAWMIDI_INFO_INPUT |
			       SNDRV_RAWMIDI_INFO_DUPLEX;

	if (rmidi)
		*rmidi = rrawmidi;
	return 0;
}

static int snd_serial_generic_probe(struct serdev_device *serdev)
{
	struct snd_card *card;
	struct snd_serial_generic *drvdata;
	int err;

	err  = snd_devm_card_new(&serdev->dev, SNDRV_DEFAULT_IDX1,
				SNDRV_DEFAULT_STR1, THIS_MODULE,
				sizeof(struct snd_serial_generic), &card);

	if (err < 0)
		return err;

	strcpy(card->driver, "SerialMIDI");
	sprintf(card->shortname, "SerialMIDI-%d", serdev->ctrl->nr);
	sprintf(card->longname, "Serial MIDI device at serial%d", serdev->ctrl->nr);

	drvdata = card->private_data;

	drvdata->serdev = serdev;
	drvdata->card = card;

	snd_serial_generic_parse_dt(serdev, drvdata);

	INIT_WORK(&drvdata->tx_work, snd_serial_generic_tx_work);

	err = snd_serial_generic_rmidi(drvdata, 1, 1, &drvdata->rmidi);
	if (err < 0)
		return err;

	serdev_device_set_client_ops(serdev, &snd_serial_generic_serdev_device_ops);
	serdev_device_set_drvdata(drvdata->serdev, drvdata);

	err = snd_card_register(card);
	if (err < 0)
		return err;

	return 0;
}

static const struct of_device_id snd_serial_generic_dt_ids[] = {
	{ .compatible = "serial-midi" },
	{},
};

MODULE_DEVICE_TABLE(of, snd_serial_generic_dt_ids);

static struct serdev_device_driver snd_serial_generic_driver = {
	.driver	= {
		.name		= "snd-serial-generic",
		.of_match_table	= of_match_ptr(snd_serial_generic_dt_ids),
	},
	.probe	= snd_serial_generic_probe,
};

module_serdev_device_driver(snd_serial_generic_driver);