From uri@watson.ibm.com Sat Nov 11 19:34:10 EST 1995
Article: 16007 of comp.os.linux.development.system
Path: bigblue.oit.unc.edu!concert!news-server.ncren.net!news.duke.edu!news.mathworks.com!newsfeed.internetmci.com!in1.uu.net!newsgate.watson.ibm.com!watnews.watson.ibm.com!usenet
From: uri@watson.ibm.com (Uri Blumenthal)
Newsgroups: comp.os.linux.development.system,comp.windows.x.i386unix
Subject: Re: Is /dev/psaux still a single-open device?
Date: 10 Nov 1995 02:59:29 GMT
Organization: IBM T.J. Watson Research Center
Lines: 178
Message-ID: <47uf6h$na4@watnews1.watson.ibm.com>
References: <rantapaa.815908434@s6.math.umn.edu>
NNTP-Posting-Host: yasc138.watson.ibm.com
X-Newsreader: knews 0.9.3
Xref: bigblue.oit.unc.edu comp.os.linux.development.system:16007 comp.windows.x.i386unix:20618

In article <rantapaa.815908434@s6.math.umn.edu>,
	rantapaa@s6.math.umn.edu (Erik E. Rantapaa) writes:
>I was wondering if /dev/psaux (the PS/2 mouse device) was still
>a single-open device, and if so, if it wouldn't be possible to make
>it a multiple-open device?

It still is in 1.2.13, and see my patch appended to make it "normal".
1.3.35+ already took care of this, and you can start as many mouse-
ha=ungry apps as you wish.

--- /usr/src/linux/drivers/char/psaux.c.~1~	Mon Jan 16 00:17:35 1995
+++ /usr/src/linux/drivers/char/psaux.c	Fri Oct  6 15:24:24 1995
@@ -19,6 +19,11 @@
  * is not what this driver is all about -- QuickPort is just a
  * connector type, and this driver is for the mouse port on the Chips
  * & Technologies 82C710 interface chip. 15Nov93 jem@cs.hut.fi
+ * 
+ * Modified by Uri Blumenthal (uri@watson.ibm.com) 06Oct95
+ *   made it possible for several programs to share mouse device
+ *   (was needed to run three X-servers simultaneously :-).
+ *
  */
 
 /* Uncomment the following line if your mouse needs initialization. */
@@ -100,13 +105,13 @@
 
 static struct aux_queue *queue;
 static int aux_ready = 0;
-static int aux_busy = 0;
+static int aux_busy = 0;   /* how many programs opened mouse dev */
 static int aux_present = 0;
 static int poll_aux_status(void);
 
 #ifdef CONFIG_82C710_MOUSE
 static int qp_present = 0;
-static int qp_busy = 0;
+static int qp_busy = 0;  /* how many programs opened mouse dev */
 static int qp_data = QP_DATA;
 static int qp_status = QP_STATUS;
 
@@ -232,28 +237,32 @@
 
 static void release_aux(struct inode * inode, struct file * file)
 {
-	aux_write_dev(AUX_DISABLE_DEV);		/* disable aux device */
-	aux_write_cmd(AUX_INTS_OFF);		/* disable controller ints */
-	poll_aux_status();
-	outb_p(AUX_DISABLE,AUX_COMMAND);      	/* Disable Aux device */
-	poll_aux_status();
-	free_irq(AUX_IRQ);
-	aux_busy = 0;
+  aux_busy -= 1;                 /* decrease the usage count */
+  if (aux_busy == 0) {      /* if nobody else is using mouse */
+    aux_write_dev(AUX_DISABLE_DEV);	/* disable aux device */
+    aux_write_cmd(AUX_INTS_OFF);   /* disable controller ints */
+    poll_aux_status();
+    outb_p(AUX_DISABLE,AUX_COMMAND);    /* Disable Aux device */
+    poll_aux_status();
+    free_irq(AUX_IRQ);
+  }
 }
 
 #ifdef CONFIG_82C710_MOUSE
 static void release_qp(struct inode * inode, struct file * file)
 {
-	unsigned char status;
-
-	if (!poll_qp_status())
-	        printk("Warning: Mouse device busy in release_qp()\n");
-	status = inb_p(qp_status);
-	outb_p(status & ~(QP_ENABLE|QP_INTS_ON), qp_status);
-	if (!poll_qp_status())
-	        printk("Warning: Mouse device busy in release_qp()\n");
-	free_irq(QP_IRQ);
-	qp_busy = 0;
+  unsigned char status;
+  
+  if (!poll_qp_status())
+    printk("Warning: Mouse device busy in release_qp()\n");
+  qp_busy -= 1;
+  if (qp_busy == 0) {
+    status = inb_p(qp_status);
+    outb_p(status & ~(QP_ENABLE|QP_INTS_ON), qp_status);
+    if (!poll_qp_status())
+      printk("Warning: Mouse device busy in release_qp()\n");
+    free_irq(QP_IRQ);
+  }
 }
 #endif
 
@@ -264,25 +273,35 @@
 
 static int open_aux(struct inode * inode, struct file * file)
 {
-	if (!aux_present)
-		return -EINVAL;
-	if (aux_busy)
-		return -EBUSY;
-	if (!poll_aux_status())
-		return -EBUSY;
-	aux_busy = 1;
-	queue->head = queue->tail = 0;	        /* Flush input queue */
-	if (request_irq(AUX_IRQ, aux_interrupt, 0, "PS/2 Mouse")) {
-		aux_busy = 0;
-		return -EBUSY;
-	}
-	poll_aux_status();
-	outb_p(AUX_ENABLE,AUX_COMMAND);		/* Enable Aux */
-	aux_write_dev(AUX_ENABLE_DEV);		/* enable aux device */
-	aux_write_cmd(AUX_INTS_ON);		/* enable controller ints */
-	poll_aux_status();
-	aux_ready = 0;
-	return 0;
+  if (!aux_present)
+    return -EINVAL;
+
+#if 0
+  /* a relict from times when only one program could open mouse */
+  if (aux_busy)
+    return -EBUSY;
+#endif
+
+  if (!poll_aux_status())
+    return -EBUSY;
+  aux_busy += 1;           /* increase mouse usage counter */
+  queue->head = queue->tail = 0;      /* Flush input queue */
+
+  /* If first time opened mouse - need to allocate IRQ */
+  if (aux_busy == 1) {
+    if (request_irq(AUX_IRQ, aux_interrupt, 0, "PS/2 Mouse")) {
+      aux_busy = 0;
+      return -EBUSY;
+    }
+  }
+  
+  poll_aux_status();
+  outb_p(AUX_ENABLE,AUX_COMMAND);	/* Enable Aux */
+  aux_write_dev(AUX_ENABLE_DEV);	/* enable aux device */
+  aux_write_cmd(AUX_INTS_ON);		/* enable controller ints */
+  poll_aux_status();
+  aux_ready = 0;
+  return 0;
 }
 
 #ifdef CONFIG_82C710_MOUSE
@@ -299,13 +318,18 @@
 	if (!qp_present)
 		return -EINVAL;
 
+#if 0
+	/* relict from old times when mouse wasn't sharable */
 	if (qp_busy)
 		return -EBUSY;
+#endif
 
-	if (request_irq(QP_IRQ, qp_interrupt, 0, "PS/2 Mouse"))
-		return -EBUSY;
+	qp_busy += 1; /* increase usage counter */
 
-	qp_busy = 1;
+	/* If first time opened mouse - need to allocate IRQ */
+	if (qp_busy == 1)
+	  if (request_irq(QP_IRQ, qp_interrupt, 0, "PS/2 Mouse"))
+	    return -EBUSY;
 
 	status = inb_p(qp_status);
 	status |= (QP_ENABLE|QP_RESET);

-- 
Regards,
Uri.
-=-=-==-=-=-             uri@watson.ibm.com
<Disclaimer>
I'm not sure which upsets me more: that people are so unwilling to accept
responsibility for their own actions, or that they are so eager to regulate
everyone else's.




