int xillybus_init_chrdev()

in xillybus/xillybus_class.c [41:170]


int xillybus_init_chrdev(struct device *dev,
			 const struct file_operations *fops,
			 struct module *owner,
			 void *private_data,
			 unsigned char *idt, unsigned int len,
			 int num_nodes,
			 const char *prefix, bool enumerate)
{
	int rc;
	dev_t mdev;
	int i;
	char devname[48];

	struct device *device;
	size_t namelen;
	struct xilly_unit *unit, *u;

	unit = kzalloc(sizeof(*unit), GFP_KERNEL);

	if (!unit)
		return -ENOMEM;

	mutex_lock(&unit_mutex);

	if (!enumerate)
		snprintf(unit->name, UNITNAMELEN, "%s", prefix);

	for (i = 0; enumerate; i++) {
		snprintf(unit->name, UNITNAMELEN, "%s_%02d",
			 prefix, i);

		enumerate = false;
		list_for_each_entry(u, &unit_list, list_entry)
			if (!strcmp(unit->name, u->name)) {
				enumerate = true;
				break;
			}
	}

	rc = alloc_chrdev_region(&mdev, 0, num_nodes, unit->name);

	if (rc) {
		dev_warn(dev, "Failed to obtain major/minors");
		goto fail_obtain;
	}

	unit->major = MAJOR(mdev);
	unit->lowest_minor = MINOR(mdev);
	unit->num_nodes = num_nodes;
	unit->private_data = private_data;

	unit->cdev = cdev_alloc();
	if (!unit->cdev) {
		rc = -ENOMEM;
		goto unregister_chrdev;
	}
	unit->cdev->ops = fops;
	unit->cdev->owner = owner;

	rc = cdev_add(unit->cdev, MKDEV(unit->major, unit->lowest_minor),
		      unit->num_nodes);
	if (rc) {
		dev_err(dev, "Failed to add cdev.\n");
		/* kobject_put() is normally done by cdev_del() */
		kobject_put(&unit->cdev->kobj);
		goto unregister_chrdev;
	}

	for (i = 0; i < num_nodes; i++) {
		namelen = strnlen(idt, len);

		if (namelen == len) {
			dev_err(dev, "IDT's list of names is too short. This is exceptionally weird, because its CRC is OK\n");
			rc = -ENODEV;
			goto unroll_device_create;
		}

		snprintf(devname, sizeof(devname), "%s_%s",
			 unit->name, idt);

		len -= namelen + 1;
		idt += namelen + 1;

		device = device_create(xillybus_class,
				       NULL,
				       MKDEV(unit->major,
					     i + unit->lowest_minor),
				       NULL,
				       "%s", devname);

		if (IS_ERR(device)) {
			dev_err(dev, "Failed to create %s device. Aborting.\n",
				devname);
			rc = -ENODEV;
			goto unroll_device_create;
		}
	}

	if (len) {
		dev_err(dev, "IDT's list of names is too long. This is exceptionally weird, because its CRC is OK\n");
		rc = -ENODEV;
		goto unroll_device_create;
	}

	list_add_tail(&unit->list_entry, &unit_list);

	dev_info(dev, "Created %d device files.\n", num_nodes);

	mutex_unlock(&unit_mutex);

	return 0;

unroll_device_create:
	for (i--; i >= 0; i--)
		device_destroy(xillybus_class, MKDEV(unit->major,
						     i + unit->lowest_minor));

	cdev_del(unit->cdev);

unregister_chrdev:
	unregister_chrdev_region(MKDEV(unit->major, unit->lowest_minor),
				 unit->num_nodes);

fail_obtain:
	mutex_unlock(&unit_mutex);

	kfree(unit);

	return rc;
}