drivers/comedi/kcomedilib/kcomedilib_main.c

Source file repositories/reference/linux-study-clean/drivers/comedi/kcomedilib/kcomedilib_main.c

File Facts

System
Linux kernel
Corpus path
drivers/comedi/kcomedilib/kcomedilib_main.c
Extension
.c
Size
8416 bytes
Lines
354
Domain
Driver Families
Bucket
drivers/comedi
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

if (test_bit(to, destinations[cur])) {
			/* Loop detected. */
			okay = false;
			break;
		}
		/* Create next set of destinations. */
		bitmap_zero(destinations[next], COMEDI_NUM_BOARD_MINORS);
		while ((t = find_next_bit(destinations[cur],
					  COMEDI_NUM_BOARD_MINORS,
					  t)) < COMEDI_NUM_BOARD_MINORS) {
			unsigned int f;

			for (f = 0; f < COMEDI_NUM_BOARD_MINORS; f++) {
				if (kcomedilib_to_from[t][f])
					set_bit(f, destinations[next]);
			}
			t++;
		}
		cur = next;
	} while (!bitmap_empty(destinations[cur], COMEDI_NUM_BOARD_MINORS));
	if (okay) {
		/* Allow a maximum of 255 links from "from" to "to". */
		if (kcomedilib_to_from[to][from] < 255)
			kcomedilib_to_from[to][from]++;
		else
			okay = false;
	}
	mutex_unlock(&kcomedilib_to_from_lock);
	return okay;
}

static void kcomedilib_clear_link_from_to(unsigned int from, unsigned int to)
{
	if (to < COMEDI_NUM_BOARD_MINORS && from < COMEDI_NUM_BOARD_MINORS) {
		mutex_lock(&kcomedilib_to_from_lock);
		if (kcomedilib_to_from[to][from])
			kcomedilib_to_from[to][from]--;
		mutex_unlock(&kcomedilib_to_from_lock);
	}
}

/**
 * comedi_open_from() - Open a COMEDI device from the kernel with loop checks
 * @filename: Fake pathname of the form "/dev/comediN".
 * @from: Device number it is being opened from (if in range).
 *
 * Converts @filename to a COMEDI device number and "opens" it if it exists
 * and is attached to a low-level COMEDI driver.
 *
 * If @from is in range, refuse to open the device if doing so would form a
 * loop of devices opening each other.  There is also a limit of 255 on the
 * number of concurrent opens from one device to another.
 *
 * Return: A pointer to the COMEDI device on success.
 * Return %NULL on failure.
 */
struct comedi_device *comedi_open_from(const char *filename, int from)
{
	struct comedi_device *dev, *retval = NULL;
	unsigned int minor;

	if (strncmp(filename, "/dev/comedi", 11) != 0)
		return NULL;

	if (kstrtouint(filename + 11, 0, &minor))
		return NULL;

	if (minor >= COMEDI_NUM_BOARD_MINORS)
		return NULL;

	dev = comedi_dev_get_from_minor(minor);
	if (!dev)
		return NULL;

	down_read(&dev->attach_lock);
	if (dev->attached && kcomedilib_set_link_from_to(from, minor))
		retval = dev;
	else
		retval = NULL;
	up_read(&dev->attach_lock);

	if (!retval)
		comedi_dev_put(dev);

	return retval;
}
EXPORT_SYMBOL_GPL(comedi_open_from);

/**
 * comedi_close_from() - Close a COMEDI device from the kernel with loop checks

Annotation

Implementation Notes