From 68f485b5e6d3a0427f5e6025b9843a057430cfed Mon Sep 17 00:00:00 2001
From: Jeffrey Lee <jlee@gitlab.riscosopen.org>
Date: Mon, 22 Aug 2011 22:38:23 +0000
Subject: [PATCH] Update HAL USB code for new HAL_KbdScanInterrupt behaviour.
 Fix data aborts during keyboard scanning.

Detail:
  build/c/usbhal, build/h/usbhal - Updated USBHAL_KbdScanInterrupt to behave the same way HAL_KbdScanInterrupt is now expected to behave.
  build/c/ehci, build/c/ohci - Fixed some root hub emulation code which was writing to a global structure before copying it into the destination buffer. Although this works fine in ROM modules it was causing data aborts in the USB HAL libraries due to lack of automatic RW data relocation by the compiler.
  build/Version - Updated version numbers
Admin:
  Tested on rev C2 BB.


Version 0.62. Tagged as 'NetBSD-0_62'
---
 VersionNum       | 20 ++++++++++----------
 build/Version    | 12 ++++++------
 build/c/usbhal   |  9 ++++++---
 dev/usb/c/ehci   |  4 ++--
 dev/usb/c/ohci   | 12 ++++++------
 dev/usb/h/usbhal |  4 ++--
 6 files changed, 32 insertions(+), 29 deletions(-)

diff --git a/VersionNum b/VersionNum
index efbdd40..e0c88b0 100644
--- a/VersionNum
+++ b/VersionNum
@@ -1,23 +1,23 @@
-/* (0.61)
+/* (0.62)
  *
  * This file is automatically maintained by srccommit, do not edit manually.
  * Last processed by srccommit version: 1.1.
  *
  */
-#define Module_MajorVersion_CMHG        0.61
+#define Module_MajorVersion_CMHG        0.62
 #define Module_MinorVersion_CMHG        
-#define Module_Date_CMHG                08 Aug 2011
+#define Module_Date_CMHG                22 Aug 2011
 
-#define Module_MajorVersion             "0.61"
-#define Module_Version                  61
+#define Module_MajorVersion             "0.62"
+#define Module_Version                  62
 #define Module_MinorVersion             ""
-#define Module_Date                     "08 Aug 2011"
+#define Module_Date                     "22 Aug 2011"
 
-#define Module_ApplicationDate          "08-Aug-11"
+#define Module_ApplicationDate          "22-Aug-11"
 
 #define Module_ComponentName            "NetBSD"
 #define Module_ComponentPath            "mixed/RiscOS/Sources/HWSupport/USB/NetBSD"
 
-#define Module_FullVersion              "0.61"
-#define Module_HelpVersion              "0.61 (08 Aug 2011)"
-#define Module_LibraryVersionInfo       "0:61"
+#define Module_FullVersion              "0.62"
+#define Module_HelpVersion              "0.62 (22 Aug 2011)"
+#define Module_LibraryVersionInfo       "0:62"
diff --git a/build/Version b/build/Version
index 6eb4a7d..4566fae 100644
--- a/build/Version
+++ b/build/Version
@@ -2,11 +2,11 @@
    VersionNum file really,as a work around define them here so the CMHG
    file remains unaltered */
 
-#define OHCIDriverModule_Module_Date_CMHG  08 Aug 2011
-#define OHCIDriverModule_MajorVersion_CMHG 0.35
+#define OHCIDriverModule_Module_Date_CMHG  22 Aug 2011
+#define OHCIDriverModule_MajorVersion_CMHG 0.36
 
-#define EHCIDriverModule_Module_Date_CMHG  05 Aug 2011
-#define EHCIDriverModule_MajorVersion_CMHG 0.18
+#define EHCIDriverModule_Module_Date_CMHG  22 Aug 2011
+#define EHCIDriverModule_MajorVersion_CMHG 0.19
         
-#define USBDriverModule_Module_Date_CMHG   05 Aug 2011
-#define USBDriverModule_MajorVersion_CMHG  0.53
+#define USBDriverModule_Module_Date_CMHG   22 Aug 2011
+#define USBDriverModule_MajorVersion_CMHG  0.54
diff --git a/build/c/usbhal b/build/c/usbhal
index 9fccf60..80c0c6c 100644
--- a/build/c/usbhal
+++ b/build/c/usbhal
@@ -340,10 +340,9 @@ uint32_t USBHAL_KbdScan(void)
     return USBHALWS->kbdflags;
 }
 
-void USBHAL_KbdScanInterrupt(void)
+int USBHAL_KbdScanInterrupt(int irq)
 {
     USBHALWS->irqsema++;
-    int irq = HAL_IRQSource();
     hdprintf("i%d\n",irq);
     if(irq == HAL_TimerDevice(0))
     {
@@ -364,6 +363,7 @@ void USBHAL_KbdScanInterrupt(void)
             /* Make sure IRQs are still off */
             splbio();
         }
+        irq = -1;
     }
     else if(irq != -1)
     {
@@ -373,16 +373,19 @@ void USBHAL_KbdScanInterrupt(void)
         {
             struct usbd_bus *bus = usb_getbus(dev);
             if(bus->methods->do_intr(bus,irq))
+            {
+                irq = -1;
                 goto done;
+            }
         }
         hdprintf("?\n");
-        HAL_IRQClear(irq);
     }
 done:
     USBHALWS->irqsema--;
     /* Trigger any pending callbacks */
     triggercbs();
     hdprintf("I\n");
+    return irq;
 }
 
 void USBHAL_Shutdown(void)
diff --git a/dev/usb/c/ehci b/dev/usb/c/ehci
index 5996877..4e1ab35 100644
--- a/dev/usb/c/ehci
+++ b/dev/usb/c/ehci
@@ -2052,7 +2052,7 @@ ehci_rem_free_itd_chain(ehci_softc_t *sc, struct ehci_xfer *exfer)
 /*
  * Data structures and routines to emulate the root hub.
  */
-Static usb_device_descriptor_t ehci_devd = {
+Static const usb_device_descriptor_t ehci_devd = {
 	USB_DEVICE_DESCRIPTOR_SIZE,
 	UDESC_DEVICE,		/* type */
 	{0x00, 0x02},		/* USB version */
@@ -2199,12 +2199,12 @@ ehci_root_ctrl_start(usbd_xfer_handle xfer)
 				goto ret;
 			}
 			totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
-			USETW(ehci_devd.idVendor, sc->sc_id_vendor);
 #if defined(__CC_NORCROFT) && !defined(DISABLE_PACKED)
 			memcpy(buf, (void*) &ehci_devd, l);
 #else
 			memcpy(buf, &ehci_devd, l);
 #endif
+			USETW(((usb_device_descriptor_t *) buf)->idVendor, sc->sc_id_vendor);
 			break;
 		/*
 		 * We can't really operate at another speed, but the spec says
diff --git a/dev/usb/c/ohci b/dev/usb/c/ohci
index 40407e5..120ad74 100644
--- a/dev/usb/c/ohci
+++ b/dev/usb/c/ohci
@@ -2615,7 +2615,7 @@ ohci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
 /*
  * Data structures and routines to emulate the root hub.
  */
-Static usb_device_descriptor_t ohci_devd = {
+Static const usb_device_descriptor_t ohci_devd = {
 	USB_DEVICE_DESCRIPTOR_SIZE,
 	UDESC_DEVICE,		/* type */
 	{0x00, 0x01},		/* USB version */
@@ -2628,7 +2628,7 @@ Static usb_device_descriptor_t ohci_devd = {
 	1			/* # of configurations */
 };
 
-Static usb_config_descriptor_t ohci_confd = {
+Static const usb_config_descriptor_t ohci_confd = {
 	USB_CONFIG_DESCRIPTOR_SIZE,
 	UDESC_CONFIG,
 	{USB_CONFIG_DESCRIPTOR_SIZE +
@@ -2641,7 +2641,7 @@ Static usb_config_descriptor_t ohci_confd = {
 	0			/* max power */
 };
 
-Static usb_interface_descriptor_t ohci_ifcd = {
+Static const usb_interface_descriptor_t ohci_ifcd = {
 	USB_INTERFACE_DESCRIPTOR_SIZE,
 	UDESC_INTERFACE,
 	0,
@@ -2653,7 +2653,7 @@ Static usb_interface_descriptor_t ohci_ifcd = {
 	0
 };
 
-Static usb_endpoint_descriptor_t ohci_endpd = {
+Static const usb_endpoint_descriptor_t ohci_endpd = {
 	USB_ENDPOINT_DESCRIPTOR_SIZE,
 	UDESC_ENDPOINT,
 	UE_DIR_IN | OHCI_INTR_ENDPT,
@@ -2662,7 +2662,7 @@ Static usb_endpoint_descriptor_t ohci_endpd = {
 	255
 };
 
-Static usb_hub_descriptor_t ohci_hubd = {
+Static const usb_hub_descriptor_t ohci_hubd = {
 	USB_HUB_DESCRIPTOR_SIZE,
 	UDESC_HUB,
 	0,
@@ -2765,8 +2765,8 @@ ohci_root_ctrl_start(usbd_xfer_handle xfer)
 				goto ret;
 			}
 			totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
-			USETW(ohci_devd.idVendor, sc->sc_id_vendor);
 			memcpy(buf, (void *)&ohci_devd, l);
+			USETW(((usb_device_descriptor_t *) buf)->idVendor, sc->sc_id_vendor);
 			break;
 		case UDESC_CONFIG:
 			if ((value & 0xff) != 0) {
diff --git a/dev/usb/h/usbhal b/dev/usb/h/usbhal
index 66ddaed..abb3f47 100644
--- a/dev/usb/h/usbhal
+++ b/dev/usb/h/usbhal
@@ -75,8 +75,8 @@ extern void USBHAL_Init(void *heapbase,uint32_t heapsize,void *ncnbbase,uint32_t
 /* Process keyboard scan */
 extern uint32_t USBHAL_KbdScan(void);
 
-/* Process keyboard scan interrupt */
-extern void USBHAL_KbdScanInterrupt(void);
+/* Process keyboard scan interrupt. Returns -1 if IRQ handled, or 'irq' if not. */
+extern int USBHAL_KbdScanInterrupt(int irq);
 
 /* Finish keyboard scan and shut everything down */
 extern void USBHAL_Shutdown(void);
-- 
GitLab