drivers/accessibility/speakup/spk_ttyio.c

Source file repositories/reference/linux-study-clean/drivers/accessibility/speakup/spk_ttyio.c

File Facts

System
Linux kernel
Corpus path
drivers/accessibility/speakup/spk_ttyio.c
Extension
.c
Size
8970 bytes
Lines
388
Domain
Driver Families
Bucket
drivers/accessibility
Inferred role
Driver Families: exported/initcall integration point
Status
integration implementation candidate

Why This File Exists

Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.

Dependency Surface

Detected Declarations

Annotated Snippet

struct spk_ldisc_data {
	char buf;
	struct completion completion;
	bool buf_free;
	struct spk_synth *synth;
};

/*
 * This allows to catch within spk_ttyio_ldisc_open whether it is getting set
 * on for a speakup-driven device.
 */
static struct tty_struct *speakup_tty;
/* This mutex serializes the use of such global speakup_tty variable */
static DEFINE_MUTEX(speakup_tty_mutex);

static int ser_to_dev(int ser, dev_t *dev_no)
{
	if (ser < 0 || ser > (255 - 64)) {
		pr_err("speakup: Invalid ser param. Must be between 0 and 191 inclusive.\n");
		return -EINVAL;
	}

	*dev_no = MKDEV(4, (64 + ser));
	return 0;
}

static int get_dev_to_use(struct spk_synth *synth, dev_t *dev_no)
{
	/* use ser only when dev is not specified */
	if (strcmp(synth->dev_name, SYNTH_DEFAULT_DEV) ||
	    synth->ser == SYNTH_DEFAULT_SER)
		return tty_dev_name_to_number(synth->dev_name, dev_no);

	return ser_to_dev(synth->ser, dev_no);
}

static int spk_ttyio_ldisc_open(struct tty_struct *tty)
{
	struct spk_ldisc_data *ldisc_data;

	if (tty != speakup_tty)
		/* Somebody tried to use this line discipline outside speakup */
		return -ENODEV;

	if (!tty->ops->write)
		return -EOPNOTSUPP;

	ldisc_data = kmalloc_obj(*ldisc_data);
	if (!ldisc_data)
		return -ENOMEM;

	init_completion(&ldisc_data->completion);
	ldisc_data->buf_free = true;
	tty->disc_data = ldisc_data;

	return 0;
}

static void spk_ttyio_ldisc_close(struct tty_struct *tty)
{
	kfree(tty->disc_data);
}

static size_t spk_ttyio_receive_buf2(struct tty_struct *tty, const u8 *cp,
				     const u8 *fp, size_t count)
{
	struct spk_ldisc_data *ldisc_data = tty->disc_data;
	struct spk_synth *synth = ldisc_data->synth;

	if (synth->read_buff_add) {
		unsigned int i;

		for (i = 0; i < count; i++)
			synth->read_buff_add(cp[i]);

		return count;
	}

	if (!ldisc_data->buf_free)
		/* ttyio_in will tty_flip_buffer_push */
		return 0;

	/* Make sure the consumer has read buf before we have seen
	 * buf_free == true and overwrite buf
	 */
	mb();

	ldisc_data->buf = cp[0];
	ldisc_data->buf_free = false;
	complete(&ldisc_data->completion);

Annotation

Implementation Notes