habanalabs: change device_setup_cdev() to be more generic
authorOded Gabbay <oded.gabbay@gmail.com>
Tue, 30 Jul 2019 06:10:02 +0000 (09:10 +0300)
committerOded Gabbay <oded.gabbay@gmail.com>
Thu, 5 Sep 2019 11:55:26 +0000 (14:55 +0300)
This patch re-factors the device_setup_cdev() function to make it more
generic. It doesn't manipulate members of the driver's internal device
structure but instead works only on the arguments that are sent to it.

This is in preparation for using this function to create an additional
char device per ASIC.

Signed-off-by: Oded Gabbay <oded.gabbay@gmail.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/misc/habanalabs/device.c

index 21a05de1e3ff1dbcec39e769bfa1e4a03591096e..20493060772032e0d9fedc351370db0d694df60b 100644 (file)
@@ -131,48 +131,43 @@ static const struct file_operations hl_ops = {
  * @hdev: pointer to habanalabs device structure
  * @hclass: pointer to the class object of the device
  * @minor: minor number of the specific device
- * @fpos : file operations to install for this device
+ * @fpos: file operations to install for this device
+ * @name: name of the device as it will appear in the filesystem
+ * @cdev: pointer to the char device object that will be created
+ * @dev: pointer to the device object that will be created
  *
  * Create a cdev and a Linux device for habanalabs's device. Need to be
  * called at the end of the habanalabs device initialization process,
  * because this function exposes the device to the user
  */
 static int device_setup_cdev(struct hl_device *hdev, struct class *hclass,
-                               int minor, const struct file_operations *fops)
+                               int minor, const struct file_operations *fops,
+                               char *name, struct cdev *cdev,
+                               struct device **dev)
 {
        int err, devno = MKDEV(hdev->major, minor);
-       struct cdev *hdev_cdev = &hdev->cdev;
-       char *name;
-
-       name = kasprintf(GFP_KERNEL, "hl%d", hdev->id);
-       if (!name)
-               return -ENOMEM;
 
-       cdev_init(hdev_cdev, fops);
-       hdev_cdev->owner = THIS_MODULE;
-       err = cdev_add(hdev_cdev, devno, 1);
+       cdev_init(cdev, fops);
+       cdev->owner = THIS_MODULE;
+       err = cdev_add(cdev, devno, 1);
        if (err) {
                pr_err("Failed to add char device %s\n", name);
-               goto err_cdev_add;
+               return err;
        }
 
-       hdev->dev = device_create(hclass, NULL, devno, NULL, "%s", name);
-       if (IS_ERR(hdev->dev)) {
+       *dev = device_create(hclass, NULL, devno, NULL, "%s", name);
+       if (IS_ERR(*dev)) {
                pr_err("Failed to create device %s\n", name);
-               err = PTR_ERR(hdev->dev);
+               err = PTR_ERR(*dev);
                goto err_device_create;
        }
 
-       dev_set_drvdata(hdev->dev, hdev);
-
-       kfree(name);
+       dev_set_drvdata(*dev, hdev);
 
        return 0;
 
 err_device_create:
-       cdev_del(hdev_cdev);
-err_cdev_add:
-       kfree(name);
+       cdev_del(cdev);
        return err;
 }
 
@@ -875,9 +870,17 @@ out_err:
 int hl_device_init(struct hl_device *hdev, struct class *hclass)
 {
        int i, rc, cq_ready_cnt;
+       char *name;
+
+       name = kasprintf(GFP_KERNEL, "hl%d", hdev->id);
+       if (!name)
+               return -ENOMEM;
 
        /* Create device */
-       rc = device_setup_cdev(hdev, hclass, hdev->id, &hl_ops);
+       rc = device_setup_cdev(hdev, hclass, hdev->id, &hl_ops, name,
+                               &hdev->cdev, &hdev->dev);
+
+       kfree(name);
 
        if (rc)
                goto out_disabled;