Subject: pkg/15565: Fix the USB gamepad driver of xmame
To: None <gnats-bugs@gnats.netbsd.org>
From: None <rxg@ms25.url.com.tw>
List: netbsd-bugs
Date: 02/10/2002 16:49:26
>Number:         15565
>Category:       pkg
>Synopsis:       Fix the USB gamepad driver of xmame
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    pkg-manager
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Sun Feb 10 00:51:01 PST 2002
>Closed-Date:
>Last-Modified:
>Originator:     Rui-Xiang Guo
>Release:        NetBSD 1.5ZA
>Organization:
	
>Environment:
	
	
System: NetBSD rxgpc.sparqnet.net 1.5ZA NetBSD 1.5ZA (HIVE) #0: Mon Feb 4 14:41:48 CST 2002 rxg@rxgpc.sparqnet.net:/usr/src/sys/arch/i386/compile/HIVE i386
Architecture: i386
Machine: i386
>Description:
	Fix the USB gamepad driver of xmame to make it work correctly with
	some gamepad. Like mine. ;)
>How-To-Repeat:
	1. Please replace the patch-ac with this:
$NetBSD$

--- src/unix/joystick-drivers/joy_usb.c.orig    Wed Jan  2 10:45:55 2002
+++ src/unix/joystick-drivers/joy_usb.c
@@ -21,7 +21,11 @@
 #endif
 
 #if defined(__ARCH_netbsd)
+#ifdef HAVE_USBHID_H
+#include <usbhid.h>
+#else
 #include <usb.h>
+#endif
 #elif defined(__ARCH_freebsd)
 #include <libusb.h>
 #endif
@@ -69,7 +73,7 @@
 
 static int joy_initialize_hid(int i)
 {
-  int size, is_joystick, report_id;
+  int size, is_joystick, report_id = 0;
   struct hid_data *d;
   struct hid_item h;
   report_desc_t rd;
@@ -82,7 +86,19 @@
 
   priv_joy_data[i].hids = NULL;
 
+#ifdef HAVE_USBHID_H
+  if (ioctl(joy_data[i].fd, USB_GET_REPORT_ID, &report_id) < 0)
+    {
+      fprintf(stderr_file, "error: /dev/uhid%d: %s", i, strerror(errno));
+      return FALSE;
+    }
+
+  size = hid_report_size(rd, hid_input, report_id);
+  priv_joy_data[i].offset = 0;
+#else
   size = hid_report_size(rd, hid_input, &report_id);
+  priv_joy_data[i].offset = (report_id != 0);
+#endif
   if ((priv_joy_data[i].data_buf = malloc(size)) == NULL)
     {
       fprintf(stderr_file, "error: couldn't malloc %d bytes\n", size);
@@ -90,10 +106,14 @@
       return FALSE;
     }
   priv_joy_data[i].dlen = size;
-  priv_joy_data[i].offset = (report_id != 0);
 
   is_joystick = 0;
+#ifdef HAVE_USBHID_H
+  for (d = hid_start_parse(rd, 1 << hid_input, report_id);
+       hid_get_item(d, &h); )
+#else
   for (d = hid_start_parse(rd, 1 << hid_input); hid_get_item(d, &h); )
+#endif
     {
       int axis, usage, page, interesting_hid;
 
@@ -127,13 +147,12 @@
 
          if (interesting_hid)
            {
-             joy_data[i].axis[axis].min = h.logical_minimum;
-             joy_data[i].axis[axis].max = h.logical_maximum;
+             joy_data[i].axis[axis].min = h.physical_minimum;
+             joy_data[i].axis[axis].max = h.physical_maximum;
 
              /* Set the theoretical center. This will be used in case
               * the heuristic below fails. */
-             joy_data[i].axis[axis].center =
-               (h.logical_minimum + h.logical_maximum) / 2;
+             joy_data[i].axis[axis].center = (h.physical_maximum + 1) / 2;
 
              if (joy_data[i].num_axis < (axis + 1))
                joy_data[i].num_axis = axis + 1;
@@ -172,6 +191,18 @@
          joy_data[i].axis[0].center = joy_data[i].axis[0].val;
          joy_data[i].axis[1].center = joy_data[i].axis[1].val;
          joy_data[i].axis[2].center = joy_data[i].axis[2].val;
+         /* Approximate min/max values. Observe that we cannot use the
+          * max/min values that the HID reports, since that is theoretical 
+          * values that may be wrong for analogs joystics (especially if
+          * you have a joystick -> USB adaptor.) We cannot use greater delta
+          * values than +/- 1, since it is OK for a gamepad (or my USB TAC 2)
+          * to reports directions as center +/- 1. */
+         joy_data[i].axis[0].min = joy_data[i].axis[0].center - 1;
+         joy_data[i].axis[1].min = joy_data[i].axis[1].center - 1;
+         joy_data[i].axis[2].min = joy_data[i].axis[2].center - 1;
+         joy_data[i].axis[0].max = joy_data[i].axis[0].center + 1;
+         joy_data[i].axis[1].max = joy_data[i].axis[1].center + 1;
+         joy_data[i].axis[2].max = joy_data[i].axis[2].center + 1;
        }
       else
        {
@@ -183,19 +214,6 @@
          joy_data[i].axis[1].val = joy_data[i].axis[1].center;
          joy_data[i].axis[2].val = joy_data[i].axis[2].center;
        }
-
-      /* Approximate min/max values. Observe that we cannot use the
-       * max/min values that the HID reports, since that is theoretical
-       * values that may be wrong for analogs joystics (especially if
-       * you have a joystick -> USB adaptor.) We cannot use greater delta
-       * values than +/- 1, since it is OK for a gamepad (or my USB TAC 2)
-       * to reports directions as center +/- 1. */
-      joy_data[i].axis[0].min = joy_data[i].axis[0].center - 1;
-      joy_data[i].axis[1].min = joy_data[i].axis[1].center - 1;
-      joy_data[i].axis[2].min = joy_data[i].axis[2].center - 1;
-      joy_data[i].axis[0].max = joy_data[i].axis[0].center + 1;
-      joy_data[i].axis[1].max = joy_data[i].axis[1].center + 1;
-      joy_data[i].axis[2].max = joy_data[i].axis[2].center + 1;
     }
 
   return (priv_joy_data[i].hids != NULL);
@@ -244,7 +262,7 @@
          else
            axis = 2;
 
-         joy_data[i].axis[axis].val = d;
+         joy_data[i].axis[axis].val = d & h->physical_maximum;
        }
       else if (page == HUP_BUTTON)
        {

	2. make makepatchsum ; make ; make install
>Fix:
	
>Release-Note:
>Audit-Trail:
>Unformatted: