Skip to contentSkip to conversation

Staging: hv: mousevsc: cleanup the mouse driver

PATCH In review K. Y. Srinivasan 4 participants 13 messages Oct 25, 2011 – Oct 27, 2011

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Staging: hv: mousevsc: cleanup the mouse driver

As part of the effort to move the mouse driver out of staging, I had posted the code for review by the community. This patch-set addresses the comments I received. I would like to thank Dmitry and Joe for their comments and suggestions.

Regards,

K. Y

#1 lore raw

PATCH 1/6 Staging: hv: mousevsc: Make boolean states boolean

Make some state that is boolean in nature, a boolean variable.

---
 drivers/staging/hv/hv_mouse.c |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)
drivers/staging/hv/hv_mouse.c +4 -4
index ccd39c7..15aa2ce 100644
@@ -148,7 +148,7 @@ struct  mousevsc_prt_msg {
  */
 struct mousevsc_dev {
 	struct hv_device	*device;
-	unsigned char		init_complete;
+	bool			init_complete;
 	struct mousevsc_prt_msg	protocol_req;
 	struct mousevsc_prt_msg	protocol_resp;
 	/* Synchronize the request/response if needed */
@@ -159,7 +159,7 @@ struct mousevsc_dev {
 	unsigned char		*report_desc;
 	u32			report_desc_size;
 	struct hv_input_dev_info hid_dev_info;
-	int			connected;
+	bool			connected;
 	struct hid_device       *hid_device;
 };
 
@@ -487,7 +487,7 @@ static void reportdesc_callback(struct hv_device *dev, void *packet, u32 len)
 	if (!hidinput_connect(hid_dev, 0)) {
 		hid_dev->claimed |= HID_CLAIMED_INPUT;
 
-		input_device->connected = 1;
+		input_device->connected = true;
 
 	}
 
@@ -558,7 +558,7 @@ static int mousevsc_remove(struct hv_device *dev)
 
 	if (input_dev->connected) {
 		hidinput_disconnect(input_dev->hid_device);
-		input_dev->connected = 0;
+		input_dev->connected = false;
 		hid_destroy_device(input_dev->hid_device);
 	}
 
Signature · 1 line
1.7.4.1
#2 lore raw

Re: [PATCH 1/6] Staging: hv: mousevsc: Make boolean states boolean

Quoted 1 line · K. Y. Srinivasan

Make some state that is boolean in nature, a boolean variable.

[]

drivers/staging/hv/hv_mouse.c

Quoted 1 line
drivers/staging/hv/hv_mouse.c +0 -0

[]
@@ -148,7 +148,7 @@ struct mousevsc_prt_msg {

Quoted 9 lines

*/
struct mousevsc_dev {

drivers/staging/hv/hv_mouse.c +1 -1
 	struct hv_device	*device;
-	unsigned char		init_complete;
+	bool			init_complete;
 	struct mousevsc_prt_msg	protocol_req;
 	struct mousevsc_prt_msg	protocol_resp;
 	/* Synchronize the request/response if needed */

drivers/staging/hv/hv_mouse.c struct mousevsc_dev { · line 164

Quoted 7 lines
drivers/staging/hv/hv_mouse.c +1 -1
@@ -159,7 +159,7 @@ struct mousevsc_dev {
 	unsigned char		*report_desc;
 	u32			report_desc_size;
 	struct hv_input_dev_info hid_dev_info;
-	int			connected;
+	bool			connected;
 	struct hid_device       *hid_device;
 };

One of the bools should be moved to be adjacent to the other.

#3 lore raw

Re: [PATCH 1/6] Staging: hv: mousevsc: Make boolean states boolean

Quoted 16 lines · Joe Perches
Quoted 1 line · K. Y. Srinivasan

Make some state that is boolean in nature, a boolean variable.

[]

drivers/staging/hv/hv_mouse.c

Quoted 1 line
drivers/staging/hv/hv_mouse.c +0 -0

[]

(quoted diff) +0 -0
@@ -148,7 +148,7 @@ struct  mousevsc_prt_msg {
Quoted 10 lines

*/
struct mousevsc_dev {

drivers/staging/hv/hv_mouse.c +1 -1
 	struct hv_device	*device;
-	unsigned char		init_complete;
+	bool			init_complete;
 	struct mousevsc_prt_msg	protocol_req;
 	struct mousevsc_prt_msg	protocol_resp;
 	/* Synchronize the request/response if needed */
@@ -159,7 +159,7 @@ struct mousevsc_dev {
 	unsigned char		*report_desc;
Quoted 8 lines

drivers/staging/hv/hv_mouse.c struct mousevsc_dev { · line 164

Quoted 6 lines
drivers/staging/hv/hv_mouse.c +1 -1
 	u32			report_desc_size;
 	struct hv_input_dev_info hid_dev_info;
-	int			connected;
+	bool			connected;
 	struct hid_device       *hid_device;
 };

One of the bools should be moved to be adjacent to the other.

For a mouse driver, it really doesn't matter.

#4 lore raw

Re: [PATCH 1/6] Staging: hv: mousevsc: Make boolean states boolean

Quoted 25 lines · Greg KH
Quoted 16 lines · Joe Perches
Quoted 1 line · K. Y. Srinivasan

Make some state that is boolean in nature, a boolean variable.

[]

drivers/staging/hv/hv_mouse.c

Quoted 1 line
drivers/staging/hv/hv_mouse.c +0 -0

[]

(quoted diff) +0 -0
@@ -148,7 +148,7 @@ struct  mousevsc_prt_msg {
Quoted 10 lines

*/
struct mousevsc_dev {

drivers/staging/hv/hv_mouse.c +1 -1
 	struct hv_device	*device;
-	unsigned char		init_complete;
+	bool			init_complete;
 	struct mousevsc_prt_msg	protocol_req;
 	struct mousevsc_prt_msg	protocol_resp;
 	/* Synchronize the request/response if needed */
@@ -159,7 +159,7 @@ struct mousevsc_dev {
 	unsigned char		*report_desc;
Quoted 8 lines

drivers/staging/hv/hv_mouse.c struct mousevsc_dev { · line 164

Quoted 6 lines
drivers/staging/hv/hv_mouse.c +1 -1
 	u32			report_desc_size;
 	struct hv_input_dev_info hid_dev_info;
-	int			connected;
+	bool			connected;
 	struct hid_device       *hid_device;
 };

One of the bools should be moved to be adjacent to the other.

Quoted 1 line

For a mouse driver, it really doesn't matter.

'course it doesn't matter, just like whitespace
doesn't matter. It's just done for form's sake.

#5 lore raw

Re: [PATCH 1/6] Staging: hv: mousevsc: Make boolean states boolean

Quoted 26 lines · Joe Perches
Quoted 25 lines · Greg KH
Quoted 24 lines · Joe Perches
Quoted 1 line · K. Y. Srinivasan

Make some state that is boolean in nature, a boolean variable.

[]

drivers/staging/hv/hv_mouse.c

Quoted 1 line
drivers/staging/hv/hv_mouse.c +0 -0

[]

(quoted diff) +0 -0
@@ -148,7 +148,7 @@ struct  mousevsc_prt_msg {
Quoted 9 lines

*/
struct mousevsc_dev {

drivers/staging/hv/hv_mouse.c +1 -1
 	struct hv_device	*device;
-	unsigned char		init_complete;
+	bool			init_complete;
 	struct mousevsc_prt_msg	protocol_req;
 	struct mousevsc_prt_msg	protocol_resp;
 	/* Synchronize the request/response if needed */

drivers/staging/hv/hv_mouse.c struct mousevsc_dev { · line 164

Quoted 7 lines
drivers/staging/hv/hv_mouse.c +1 -1
@@ -159,7 +159,7 @@ struct mousevsc_dev {
 	unsigned char		*report_desc;
 	u32			report_desc_size;
 	struct hv_input_dev_info hid_dev_info;
-	int			connected;
+	bool			connected;
 	struct hid_device       *hid_device;
 };

One of the bools should be moved to be adjacent to the other.

Quoted 4 lines
Quoted 1 line

For a mouse driver, it really doesn't matter.

'course it doesn't matter, just like whitespace
doesn't matter. It's just done for form's sake.

Not at all.

Whitespace does matter, there is documented proof of that.

The fact that you want to move a bool to a different part of a structure really is only your personal choice. It doesn't matter for size or layout to make this code work better at all to do this.

This is your personal opinion only, please don't construe it as anything else.

greg k-h

Quoted 1 line
#6 lore raw

Re: [PATCH 1/6] Staging: hv: mousevsc: Make boolean states boolean

Quoted 26 lines · Greg KH
Quoted 25 lines · Joe Perches
Quoted 24 lines · Greg KH
Quoted 23 lines · Joe Perches
Quoted 1 line · K. Y. Srinivasan

Make some state that is boolean in nature, a boolean variable.

[]

drivers/staging/hv/hv_mouse.c

Quoted 1 line
drivers/staging/hv/hv_mouse.c +0 -0

[]

(quoted diff) +0 -0
@@ -148,7 +148,7 @@ struct  mousevsc_prt_msg {
Quoted 9 lines

*/
struct mousevsc_dev {

drivers/staging/hv/hv_mouse.c +1 -1
 	struct hv_device	*device;
-	unsigned char		init_complete;
+	bool			init_complete;
 	struct mousevsc_prt_msg	protocol_req;
 	struct mousevsc_prt_msg	protocol_resp;
 	/* Synchronize the request/response if needed */

drivers/staging/hv/hv_mouse.c struct mousevsc_dev { · line 164

Quoted 7 lines
drivers/staging/hv/hv_mouse.c +1 -1
@@ -159,7 +159,7 @@ struct mousevsc_dev {
 	unsigned char		*report_desc;
 	u32			report_desc_size;
 	struct hv_input_dev_info hid_dev_info;
-	int			connected;
+	bool			connected;
 	struct hid_device       *hid_device;
 };

One of the bools should be moved to be adjacent to the other.

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

For a mouse driver, it really doesn't matter.

'course it doesn't matter, just like whitespace
doesn't matter. It's just done for form's sake.

Not at all.
Whitespace does matter, there is documented proof of that. The fact that you want to move a bool to a different part of a structure really is only your personal choice. It doesn't matter for size or layout to make this code work better at all to do this.

Just as what you suggest is your opinion.

Whether or not 4 or 8 extra/wasted bytes in
a struct is important or not is arguable.

I think disaggregating bools is akin to
disaggregating bitfields.

Not necessarily wrong, just odd.

cheers, Joe

#7 lore raw

Re: [PATCH 1/6] Staging: hv: mousevsc: Make boolean states boolean

In reply to Greg KH
Quoted 32 lines · Greg KH
Quoted 26 lines · Joe Perches
Quoted 25 lines · Greg KH
Quoted 24 lines · Joe Perches
Quoted 1 line · K. Y. Srinivasan

Make some state that is boolean in nature, a boolean variable.

[]

drivers/staging/hv/hv_mouse.c

Quoted 1 line
drivers/staging/hv/hv_mouse.c +0 -0

[]

(quoted diff) +0 -0
@@ -148,7 +148,7 @@ struct  mousevsc_prt_msg {
Quoted 9 lines

*/
struct mousevsc_dev {

drivers/staging/hv/hv_mouse.c +1 -1
 	struct hv_device	*device;
-	unsigned char		init_complete;
+	bool			init_complete;
 	struct mousevsc_prt_msg	protocol_req;
 	struct mousevsc_prt_msg	protocol_resp;
 	/* Synchronize the request/response if needed */

drivers/staging/hv/hv_mouse.c struct mousevsc_dev { · line 164

Quoted 7 lines
drivers/staging/hv/hv_mouse.c +1 -1
@@ -159,7 +159,7 @@ struct mousevsc_dev {
 	unsigned char		*report_desc;
 	u32			report_desc_size;
 	struct hv_input_dev_info hid_dev_info;
-	int			connected;
+	bool			connected;
 	struct hid_device       *hid_device;
 };

One of the bools should be moved to be adjacent to the other.

Quoted 4 lines
Quoted 1 line

For a mouse driver, it really doesn't matter.

'course it doesn't matter, just like whitespace
doesn't matter. It's just done for form's sake.

Quoted 7 lines

Not at all.

Whitespace does matter, there is documented proof of that.

The fact that you want to move a bool to a different part of a structure really is only your personal choice. It doesn't matter for size or layout to make this code work better at all to do this.

It is a good practice to try minimizing footprint though. While it might not matter much in this particular driver we should still try to produce best code we can as it is often taken as source for new drivers.

Quoted 3 lines

This is your personal opinion only, please don't construe it as anything else.

Same could be said about the email I am replying to.

Signature · 1 line
Dmitry
#8 lore raw

PATCH 2/6 Staging: hv: mousevsc: Inline the code for mousevsc_on_device_add()

In reply to K. Y. Srinivasan

Inline the code for mousevsc_on_device_add() as this only used from the function mousevsc_probe().

---
 drivers/staging/hv/hv_mouse.c |   10 ++--------
 1 files changed, 2 insertions(+), 8 deletions(-)
drivers/staging/hv/hv_mouse.c +2 -8
index 15aa2ce..0a5676c 100644
@@ -494,7 +494,8 @@ static void reportdesc_callback(struct hv_device *dev, void *packet, u32 len)
 	input_device->hid_device = hid_dev;
 }
 
-static int mousevsc_on_device_add(struct hv_device *device)
+static int mousevsc_probe(struct hv_device *device,
+			const struct hv_vmbus_device_id *dev_id)
 {
 	int ret = 0;
 	struct mousevsc_dev *input_dev;
@@ -542,13 +543,6 @@ static int mousevsc_on_device_add(struct hv_device *device)
 	return ret;
 }
 
-static int mousevsc_probe(struct hv_device *dev,
-			const struct hv_vmbus_device_id *dev_id)
-{
-
-	return mousevsc_on_device_add(dev);
-
-}
 
 static int mousevsc_remove(struct hv_device *dev)
 {
Signature · 1 line
1.7.4.1
#9 lore raw

PATCH 3/6 Staging: hv: mousevsc: Inline the code for reportdesc_callback()

In reply to K. Y. Srinivasan

Inline the code for reportdesc_callback() as this function is called from mousevsc_probe(). As part of this, cleanup the code in reportdesc_callback().

---
 drivers/staging/hv/hv_mouse.c |   93 +++++++++++++++++++++++-----------------
 1 files changed, 53 insertions(+), 40 deletions(-)
drivers/staging/hv/hv_mouse.c +53 -40
index 0a5676c..ed071b8 100644
@@ -451,54 +451,34 @@ static int mousevsc_hid_open(struct hid_device *hid)
 	return 0;
 }
 
+static int mousevsc_hid_start(struct hid_device *hid)
+{
+	return 0;
+}
+
 static void mousevsc_hid_close(struct hid_device *hid)
 {
 }
 
+static void mousevsc_hid_stop(struct hid_device *hid)
+{
+}
+
 static struct hid_ll_driver mousevsc_ll_driver = {
 	.open = mousevsc_hid_open,
 	.close = mousevsc_hid_close,
+	.start = mousevsc_hid_start,
+	.stop = mousevsc_hid_stop,
 };
 
 static struct hid_driver mousevsc_hid_driver;
 
-static void reportdesc_callback(struct hv_device *dev, void *packet, u32 len)
-{
-	struct hid_device *hid_dev;
-	struct mousevsc_dev *input_device = hv_get_drvdata(dev);
-
-	hid_dev = hid_allocate_device();
-	if (IS_ERR(hid_dev))
-		return;
-
-	hid_dev->ll_driver = &mousevsc_ll_driver;
-	hid_dev->driver = &mousevsc_hid_driver;
-
-	if (hid_parse_report(hid_dev, packet, len))
-		return;
-
-	hid_dev->bus = BUS_VIRTUAL;
-	hid_dev->vendor = input_device->hid_dev_info.vendor;
-	hid_dev->product = input_device->hid_dev_info.product;
-	hid_dev->version = input_device->hid_dev_info.version;
-
-	sprintf(hid_dev->name, "%s", "Microsoft Vmbus HID-compliant Mouse");
-
-	if (!hidinput_connect(hid_dev, 0)) {
-		hid_dev->claimed |= HID_CLAIMED_INPUT;
-
-		input_device->connected = true;
-
-	}
-
-	input_device->hid_device = hid_dev;
-}
-
 static int mousevsc_probe(struct hv_device *device,
 			const struct hv_vmbus_device_id *dev_id)
 {
 	int ret = 0;
 	struct mousevsc_dev *input_dev;
+	struct hid_device *hid_dev;
 
 	input_dev = alloc_input_device(device);
 
@@ -524,23 +504,56 @@ static int mousevsc_probe(struct hv_device *device,
 
 	ret = mousevsc_connect_to_vsp(device);
 
-	if (ret != 0) {
-		vmbus_close(device->channel);
-		free_input_device(input_dev);
-		return ret;
-	}
-
+	if (ret != 0)
+		goto probe_err0;
 
 	/* workaround SA-167 */
 	if (input_dev->report_desc[14] == 0x25)
 		input_dev->report_desc[14] = 0x29;
 
-	reportdesc_callback(device, input_dev->report_desc,
-			    input_dev->report_desc_size);
+	hid_dev = hid_allocate_device();
+	if (IS_ERR(hid_dev)) {
+		ret = PTR_ERR(hid_dev);
+		goto probe_err0;
+	}
+
+	hid_dev->ll_driver = &mousevsc_ll_driver;
+	hid_dev->driver = &mousevsc_hid_driver;
+	hid_dev->bus = BUS_VIRTUAL;
+	hid_dev->vendor = input_dev->hid_dev_info.vendor;
+	hid_dev->product = input_dev->hid_dev_info.product;
+	hid_dev->version = input_dev->hid_dev_info.version;
+	input_dev->hid_device = hid_dev;
+
+	sprintf(hid_dev->name, "%s", "Microsoft Vmbus HID-compliant Mouse");
+
+	ret = hid_parse_report(hid_dev, input_dev->report_desc,
+				input_dev->report_desc_size);
+
+	if (ret) {
+		hid_err(hid_dev, "parse failed\n");
+		goto probe_err1;
+	}
+
+	ret = hid_hw_start(hid_dev, HID_CONNECT_HIDINPUT | HID_CONNECT_HIDDEV);
 
+	if (ret) {
+		hid_err(hid_dev, "hw start failed\n");
+		goto probe_err1;
+	}
+
+	input_dev->connected = true;
 	input_dev->init_complete = true;
 
 	return ret;
+
+probe_err1:
+	hid_destroy_device(hid_dev);
+
+probe_err0:
+	vmbus_close(device->channel);
+	free_input_device(input_dev);
+	return ret;
 }
 
 
Signature · 1 line
1.7.4.1
#10 lore raw

PATCH 4/6 Staging: hv: mousevsc: Cleanup mousevsc_on_channel_callback()

In reply to K. Y. Srinivasan

Cleanup mousevsc_on_channel_callback(). This is based on the code provided by Joe Perches <[email protected]>.

---
 drivers/staging/hv/hv_mouse.c |   85 ++++++++++++++++++-----------------------
 1 files changed, 37 insertions(+), 48 deletions(-)
drivers/staging/hv/hv_mouse.c +37 -48
index ed071b8..b10466f 100644
@@ -313,73 +313,62 @@ static void mousevsc_on_receive(struct hv_device *device,
 
 static void mousevsc_on_channel_callback(void *context)
 {
-	const int packetSize = 0x100;
-	int ret = 0;
-	struct hv_device *device = (struct hv_device *)context;
-
+	const int packet_size = 0x100;
+	int ret;
+	struct hv_device *device = context;
 	u32 bytes_recvd;
 	u64 req_id;
-	unsigned char packet[0x100];
 	struct vmpacket_descriptor *desc;
-	unsigned char	*buffer = packet;
-	int	bufferlen = packetSize;
+	unsigned char	*buffer;
+	int	bufferlen = packet_size;
 
+	buffer = kmalloc(bufferlen, GFP_ATOMIC);
+	if (!buffer)
+		return;
 
 	do {
 		ret = vmbus_recvpacket_raw(device->channel, buffer,
 					bufferlen, &bytes_recvd, &req_id);
 
-		if (ret == 0) {
-			if (bytes_recvd > 0) {
-				desc = (struct vmpacket_descriptor *)buffer;
-
-				switch (desc->type) {
-				case VM_PKT_COMP:
-					break;
-
-				case VM_PKT_DATA_INBAND:
-					mousevsc_on_receive(
-						device, desc);
-					break;
-
-				default:
-					pr_err("unhandled packet type %d, tid %llx len %d\n",
-						   desc->type,
-						   req_id,
-						   bytes_recvd);
-					break;
-				}
-
-				/* reset */
-				if (bufferlen > packetSize) {
-					kfree(buffer);
-
-					buffer = packet;
-					bufferlen = packetSize;
-				}
-			} else {
-				if (bufferlen > packetSize) {
-					kfree(buffer);
-
-					buffer = packet;
-					bufferlen = packetSize;
-				}
+		switch (ret) {
+		case 0:
+			if (bytes_recvd <= 0) {
+				kfree(buffer);
+				return;
+			}
+			desc = (struct vmpacket_descriptor *)buffer;
+
+			switch (desc->type) {
+			case VM_PKT_COMP:
+				break;
+
+			case VM_PKT_DATA_INBAND:
+				mousevsc_on_receive(device, desc);
+				break;
+
+			default:
+				pr_err("unhandled packet type %d, tid %llx len %d\n",
+				   desc->type,
+				   req_id,
+				   bytes_recvd);
 				break;
 			}
-		} else if (ret == -ENOBUFS) {
+
+			break;
+
+		case -ENOBUFS:
+			kfree(buffer);
 			/* Handle large packet */
 			bufferlen = bytes_recvd;
-			buffer = kzalloc(bytes_recvd, GFP_ATOMIC);
+			buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
 
 			if (buffer == NULL) {
-				buffer = packet;
-				bufferlen = packetSize;
-				break;
+				return;
 			}
+			break;
 		}
 	} while (1);
 
-	return;
 }
 
 static int mousevsc_connect_to_vsp(struct hv_device *device)
Signature · 1 line
1.7.4.1
#11 lore raw

PATCH 5/6 Staging: hv: mousevsc: Add a new line to a debug string

In reply to K. Y. Srinivasan

Add a new line to a debug string.

---
 drivers/staging/hv/hv_mouse.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
drivers/staging/hv/hv_mouse.c +1 -1
index b10466f..21cd050 100644
@@ -411,7 +411,7 @@ static int mousevsc_connect_to_vsp(struct hv_device *device)
 	response = &input_dev->protocol_resp;
 
 	if (!response->response.approved) {
-		pr_err("synthhid protocol request failed (version %d)",
+		pr_err("synthhid protocol request failed (version %d)\n",
 		       SYNTHHID_INPUT_VERSION);
 		ret = -ENODEV;
 		goto cleanup;
Signature · 1 line
1.7.4.1
#12 lore raw

PATCH 6/6 Staging: hv: mousevsc: Get rid of unnecessary include files

In reply to K. Y. Srinivasan

Get rid of unnecessary include files.

---
 drivers/staging/hv/hv_mouse.c |    5 +----
 1 files changed, 1 insertions(+), 4 deletions(-)
drivers/staging/hv/hv_mouse.c +1 -4
index 21cd050..dd42411 100644
@@ -14,11 +14,8 @@
  */
 #include <linux/init.h>
 #include <linux/module.h>
-#include <linux/delay.h>
 #include <linux/device.h>
-#include <linux/workqueue.h>
-#include <linux/sched.h>
-#include <linux/wait.h>
+#include <linux/completion.h>
 #include <linux/input.h>
 #include <linux/hid.h>
 #include <linux/hiddev.h>
Signature · 1 line
1.7.4.1