shithub: riscv

Download patch

ref: 61d322679b65aedb569af6f4e90764c106eca9ef
parent: 85e676568892d4a5639b0f9dc8b8c8b08e79c804
author: aiju <[email protected]>
date: Fri Jul 29 06:24:18 EDT 2011

minor polishing of usbd

--- a/sys/src/cmd/nusb/usbd/usbd.c
+++ b/sys/src/cmd/nusb/usbd/usbd.c
@@ -26,7 +26,10 @@
 	char *data;
 	int len;
 	Event *link;
-	int ref, prev;
+	int ref;  /* number of readers which will read this one
+                     the next time they'll read */
+	int prev; /* number of events pointing to this one with
+                     their link pointers */
 };
 
 static Event *evlast;
@@ -59,7 +62,7 @@
 static void
 initevent(void)
 {
-	evlast = mallocz(sizeof(Event), 1);
+	evlast = emallocz(sizeof(Event), 1);
 }
 
 static void
@@ -95,8 +98,6 @@
 	qlock(&evlock);
 	e = evlast;
 	ee = emallocz(sizeof(Event), 1);
-	if(ee == nil)
-		sysfatal("malloc: %r");
 	evlast = ee;
 	e->data = data;
 	e->len = strlen(data);
@@ -180,6 +181,10 @@
 		respond(req, nil);
 		break;
 	case Qusbevent:
+		if(req->fid->aux == nil){
+			respond(req, "the front fell off");
+			return;
+		}
 		readevent(req);
 		break;
 	default:
@@ -219,9 +224,7 @@
 		for(p = h->port; p < h->port + h->nport; p++){
 			if(p->dev == nil || p->dev->usb == nil || p->hub != nil)
 				continue;
-			e = mallocz(sizeof(Event), 1);
-			if(e == nil)
-				sysfatal("malloc: %r");
+			e = emallocz(sizeof(Event), 1);
 			e->data = formatdev(p->dev);
 			e->len = strlen(e->data);
 			e->prev = 1;
@@ -251,7 +254,7 @@
 {
 	Event *e, *ee;
 
-	if(fid->qid.path == Qusbevent){
+	if(fid->qid.path == Qusbevent && fid->aux != nil){
 		qlock(&evlock);
 		e = fid->aux;
 		if(--e->ref == 0 && e->prev == 0){