[PATCH 2/6] staging: ft1000: Use misc device instead self created device.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Use simple misc device for ioctl driver funtionality testing.

Signed-off-by: Marek Belisko <marek.belisko@xxxxxxxxxxxxxxx>
---
 drivers/staging/ft1000/ft1000-usb/ft1000_chdev.c |   90 ++++++++++++++++++---
 1 files changed, 77 insertions(+), 13 deletions(-)

diff --git a/drivers/staging/ft1000/ft1000-usb/ft1000_chdev.c b/drivers/staging/ft1000/ft1000-usb/ft1000_chdev.c
index 1aec926..d1784a3 100644
--- a/drivers/staging/ft1000/ft1000-usb/ft1000_chdev.c
+++ b/drivers/staging/ft1000/ft1000-usb/ft1000_chdev.c
@@ -32,6 +32,7 @@
 #include <linux/poll.h>
 #include <linux/netdevice.h>
 #include <linux/delay.h>
+#include <linux/miscdevice.h>
 
 #include <linux/fs.h>
 #include <linux/kmod.h>
@@ -78,6 +79,47 @@ static struct file_operations ft1000fops =
 	.llseek		= no_llseek,
 };
 
+struct ft1000_misc_device {
+	struct miscdevice dev;
+	int inf_id;
+};
+
+#define FREE_ID (0xFF)
+
+/* we support just 3 devices */
+#define MAX_DEVICE 3
+
+static struct ft1000_misc_device ft1000dev[MAX_DEVICE] = {
+	[0] = {
+		.dev = {
+			.minor = MISC_DYNAMIC_MINOR,
+			.name = "ft1000_1",
+			.nodename = "/net/ft1000_1",
+			.fops = &ft1000fops,
+		},
+		.inf_id = FREE_ID,
+	},
+	[1] = {
+		.dev = {
+			.minor = MISC_DYNAMIC_MINOR,
+			.name = "ft1000_2",
+			.nodename = "/net/ft1000_2",
+			.fops = &ft1000fops,
+		},
+		.inf_id = FREE_ID,
+	},
+	[2] = {
+		.dev = {
+			.minor = MISC_DYNAMIC_MINOR,
+			.name = "ft1000_3",
+			.nodename = "/net/ft1000_3",
+			.fops = &ft1000fops,
+		},
+		.inf_id = FREE_ID,
+	},
+};
+
+
 //---------------------------------------------------------------------------
 // Function:    ft1000_get_buffer
 //
@@ -157,8 +199,17 @@ int ft1000_CreateDevice(struct ft1000_device *dev)
     int result;
     int i;
 
-    // make a new device name
-    sprintf(info->DeviceName, "%s%d", "FT100", info->CardNumber);
+	for (i = 0; i < MAX_DEVICE; i++) {
+		if (ft1000dev[i].inf_id == FREE_ID)
+			break;
+	}
+
+	if (i == MAX_DEVICE) {
+		DEBUG("Max number of devices reached.\n");
+		return -ENODEV;
+	}
+
+	ft1000dev[i].inf_id = info->CardNumber;
 
     DEBUG("ft1000_CreateDevice: number of instance = %d\n", ft1000_flarion_cnt);
     DEBUG("DeviceCreated = %x\n", info->DeviceCreated);
@@ -176,17 +227,17 @@ int ft1000_CreateDevice(struct ft1000_device *dev)
 
 
     // register the device
-    DEBUG("ft1000_CreateDevice: \"%s\" device registration\n", info->DeviceName);
+	DEBUG("ft1000_CreateDevice: \"%s\" device registration\n",
+		ft1000dev[i].dev.nodename);
     info->DeviceMajor = 0;
+	result = misc_register(&ft1000dev[i].dev);
+	if (result) {
+		DEBUG("%s: return:  %d\n", __func__, result);
+		return result;
+	}
 
-    result = register_chrdev(info->DeviceMajor, info->DeviceName, &ft1000fops);
-    if (result < 0)
-    {
-	DEBUG("ft1000_CreateDevice: unable to get major %d\n", info->DeviceMajor);
-	return result;
-    }
-
-    DEBUG("ft1000_CreateDevice: registered char device \"%s\"\n", info->DeviceName);
+	DEBUG("ft1000_CreateDevice: registered misc device \"%s\"\n",
+		ft1000dev[i].dev.nodename);
 
     // save a dynamic device major number
     if (info->DeviceMajor == 0)
@@ -271,9 +322,22 @@ void ft1000_DestroyDevice(struct net_device *dev)
     if (info->DeviceCreated)
 	{
         ft1000_flarion_cnt--;
-		unregister_chrdev(info->DeviceMajor, info->DeviceName);
+		for (i = 0; i < MAX_DEVICE; i++) {
+			if (info->CardNumber == ft1000dev[i].inf_id)
+				break;
+		}
+
+		if (i == MAX_DEVICE) {
+			DEBUG("Device couldn't be found\n");
+			return;
+		}
+
 		DEBUG("ft1000_DestroyDevice: unregistered device \"%s\", result = %d\n",
-					   info->DeviceName, result);
+			ft1000dev[i].dev.nodename, result);
+
+		misc_deregister(&ft1000dev[i].dev);
+		ft1000dev[i].inf_id = FREE_ID;
+
 
         // Make sure we free any memory reserve for slow Queue
         for (i=0; i<MAX_NUM_APP; i++) {
-- 
1.7.1

_______________________________________________
devel mailing list
devel@xxxxxxxxxxxxxxxxxxxxxx
http://driverdev.linuxdriverproject.org/mailman/listinfo/devel


[Index of Archives]     [Linux Driver Backports]     [DMA Engine]     [Linux GPIO]     [Linux SPI]     [Video for Linux]     [Linux USB Devel]     [Linux Coverity]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Yosemite Backpacking]
  Powered by Linux