9front - general discussion about 9front
 help / color / mirror / Atom feed
From: Eli Cohen <echoline@gmail.com>
To: 9front@9front.org
Subject: Re: [9front] CDC ACM nusb/serial driver
Date: Mon, 5 Oct 2020 14:53:48 -0700	[thread overview]
Message-ID: <CAHwi9bwc5iiqRnMpnyX4SEuxEcehz0-f4zvAF3X+iKctjv5fvQ@mail.gmail.com> (raw)
In-Reply-To: <CAHwi9bxMpu2ooaY99i=SENLRtfkV5NM+rQ8LqzuTivgjogCi-A@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 201 bytes --]

I was finally looking at this again today. it does use hardware flow
control, in the sendlines function.

it works well here, but I have only tested it with an arduino mega
2560 connected to an x230 t

[-- Attachment #2: serial.patch --]
[-- Type: text/x-patch, Size: 7721 bytes --]

diff -r bd503458654d sys/src/cmd/nusb/serial/cdc.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/src/cmd/nusb/serial/cdc.c	Mon Oct 05 14:03:47 2020 -0700
@@ -0,0 +1,259 @@
+#include <u.h>
+#include <libc.h>
+#include <thread.h>
+#include <fcall.h>
+#include <9p.h>
+#include "usb.h"
+#include "serial.h"
+
+enum {
+	Kyocera		= 0x0482,
+	AHK3001V	= 0x0203,
+};
+
+enum {
+	ScACM	= 2,
+	AT		= 1,
+	None	= 0,
+};
+
+enum {
+	LineCoding	= 0x20,
+	SetBreak	= 0x23,
+	ControlState = 0x22,
+};
+
+enum {
+	ClassCDCData = 0x0a,
+	SubclassData = 0,
+};
+
+enum {
+	DescSubCDCCM	= 1,
+	DescSubCDCACM	= 2,
+	DescSubCDCUnion	= 6,
+};
+
+enum {
+	CMDoesCM	= 1,
+	CMOverData	= 2,
+};
+
+enum {
+	ACMHasFeature	= 1,
+	ACMHasLine		= 2,
+	ACMHasBreak		= 4,
+	ACMHasNetwork	= 8,
+};
+
+enum {
+	Notification = 0xa1,
+	SerialState = 0x20,
+};
+
+enum {
+	DcdStatus	= 0x01,
+	DsrStatus	= 0x02,
+	BreakStatus	= 0x04,
+	RingStatus	= 0x08,
+};
+
+static int
+cdcsetparam(Serialport *p)
+{
+	int res;
+	uchar vals[7];
+
+	PUT4(vals, p->baud);
+
+	vals[4] = 0;
+	if(p->stop == 1)
+		vals[4] = 0;
+	if(p->stop == 15)
+		vals[4] = 1;
+	if(p->stop == 2)
+		vals[4] = 2;
+
+	vals[5] = p->parity;
+
+	vals[6] = p->bits;
+
+	res = usbcmd(p->s->dev, Rh2d | Rclass | Riface, LineCoding, 0, p->ctl_ifc, vals, 7);
+
+	return res;
+}
+
+void
+getcaps(Serialport *p)
+{
+	int i, j;
+	Desc *desc;
+	Iface *iface;
+	DDesc *ddesc;
+	int cur_ifc;
+
+	for (i = 0; i < Niface; i++) {
+		iface = p->s->dev->usb->conf[0]->iface[i];
+		if (iface == nil)
+			continue;
+
+		cur_ifc = iface->id;
+		if (cur_ifc != p->ctl_ifc && Class(iface->csp) == ClassCDCData && Subclass(iface->csp) == SubclassData && p->data_ifc == -1)
+			p->data_ifc = cur_ifc;
+
+		if (cur_ifc == p->ctl_ifc) {
+			for (j = 0; j < Nddesc; j++) {
+				desc = p->s->dev->usb->ddesc[j];
+				if (desc == nil)
+					continue;
+
+				ddesc = &desc->data;
+				if (ddesc->bDescriptorType == Dfunction) {
+					switch (ddesc->bbytes[0]) {
+					case DescSubCDCCM:
+						p->data_ifc = ddesc->bbytes[2];
+						break;
+					case DescSubCDCACM:
+						p->hasbreak = ddesc->bbytes[1] & ACMHasBreak? 1: 0;
+						break;
+					case DescSubCDCUnion:
+						p->data_ifc = ddesc->bbytes[2];
+						break;
+					}
+				}
+			}
+		}
+	}
+}
+
+void
+statusreader(void *u)
+{
+	Serialport *p = u;
+	uchar buf[24];
+
+	threadsetname("statusreaderproc");
+
+	while(readn(p->epintr->dfd, buf, sizeof(buf)) == sizeof(buf)) {
+		if (buf[0] == Notification && buf[1] == SerialState && buf[6] == 2 && buf[7] == 0) {
+			p->dcd = buf[8] & DcdStatus? 1: 0;
+			p->dsr = buf[8] & DsrStatus? 1: 0;
+			p->ring = buf[8] & RingStatus? 1: 0;
+		} else {
+			fprint(2, "serial: unknown intr message: reqtype:%02x notify:%02x state:%02x\n", buf[0], buf[1], buf[8]);
+		}
+	}
+
+	fprint(2, "serial: statusreader exiting: %r\n");
+	closedev(p->s->dev);
+}
+
+static int
+cdcinit(Serialport *p)
+{
+	p->baud = 115200;
+	p->bits = 8;
+	p->parity = 0;
+	p->stop = 1;
+	cdcsetparam(p);
+
+	incref(p->s->dev);
+	proccreate(statusreader, p, 8*1024);
+
+	return 0;
+}
+
+static int
+cdcsetbreak(Serialport *p, int val)
+{
+	if(p->hasbreak == 0)
+		return 0;
+
+	return usbcmd(p->s->dev, Rh2d | Rclass | Riface, SetBreak, (val != 0? 0xffff: 0x0000), p->ctl_ifc, nil, 0);
+}
+
+static int
+cdcsendlines(Serialport *p)
+{
+	if(p->rts)
+		p->ctlstate |= CtlRTS;
+	else
+		p->ctlstate &= ~CtlRTS;
+	if(p->dtr)
+		p->ctlstate |= CtlDTR;
+	else
+		p->ctlstate &= ~CtlDTR;
+
+	return usbcmd(p->s->dev, Rh2d | Rclass | Riface, ControlState, p->ctlstate, p->ctl_ifc, nil, 0);
+}
+
+static int
+cdcclearpipes(Serialport *p)
+{
+	if(unstall(p->s->dev, p->epout, Eout) < 0)
+		dprint(2, "serial: unstall epout: %r\n");
+	if(unstall(p->s->dev, p->epin, Ein) < 0)
+		dprint(2, "serial: unstall epin: %r\n");
+	if(unstall(p->s->dev, p->epintr, Ein) < 0)
+		dprint(2, "serial: unstall epintr: %r\n");
+
+	return 0;
+}
+
+static int
+cdcwait4data(Serialport *p, uchar *data, int count)
+{
+	int n;
+
+	qunlock(p->s);
+	while ((n = read(p->epin->dfd, data, count)) == 0)
+		;
+	qlock(p->s);
+
+	return n;
+}
+
+static Serialops cdcops = {
+	.init		= cdcinit,
+	.setparam	= cdcsetparam,
+	.setbreak	= cdcsetbreak,
+	.sendlines	= cdcsendlines,
+	.clearpipes	= cdcclearpipes,
+	.wait4data	= cdcwait4data,
+};
+
+int
+cdcprobe(Serial *ser)
+{
+	int i, ifcs;
+	Usbdev *ud = ser->dev->usb;
+	Iface *iface;
+	ulong csp;
+
+	ifcs = 0;
+	if (ud->vid == Kyocera || ud->did == AHK3001V)
+		ifcs++;
+	else for (i = 0; i < Niface; i++) {
+		iface = ud->conf[0]->iface[i];
+		if (iface == nil)
+			continue;
+
+		csp = iface->csp;
+		if (Class(csp) == Clcomms && Subclass(csp) == ScACM && (Proto(csp) == AT || Proto(csp) == None)) {
+			ser->p[ifcs].ctl_ifc = i;
+			ser->p[ifcs].s = ser;
+			getcaps(&ser->p[ifcs]);
+			ifcs++;
+		}
+	}
+
+	if (ifcs == 0)
+		return -1;
+
+	ser->nifcs = ifcs;
+	ser->isacm = 1;
+	ser->outhdrsz = 0;
+	ser->hasepintr = 1;
+	ser->Serialops = cdcops;
+	return 0;
+}
diff -r bd503458654d sys/src/cmd/nusb/serial/mkfile
--- a/sys/src/cmd/nusb/serial/mkfile	Sun Oct 04 22:45:22 2020 +0200
+++ b/sys/src/cmd/nusb/serial/mkfile	Mon Oct 05 14:03:47 2020 -0700
@@ -1,7 +1,7 @@
 </$objtype/mkfile
 
 TARG=serial
-OFILES=ftdi.$O serial.$O prolific.$O ucons.$O silabs.$O ch340.$O
+OFILES=ftdi.$O serial.$O prolific.$O ucons.$O silabs.$O ch340.$O cdc.$O
 HFILES=\
 	../lib/usb.h\
 	serial.h\
diff -r bd503458654d sys/src/cmd/nusb/serial/serial.c
--- a/sys/src/cmd/nusb/serial/serial.c	Sun Oct 04 22:45:22 2020 +0200
+++ b/sys/src/cmd/nusb/serial/serial.c	Mon Oct 05 14:03:47 2020 -0700
@@ -624,18 +624,32 @@
 
 	epintr = epin = epout = -1;
 
+	if(ser->isacm){
+		eps = ser->dev->usb->conf[0]->iface[ser->p[ifc].ctl_ifc]->ep;
+
+		for(i = 0; i < Nep; i++){
+			if((ep = eps[i]) == nil)
+				continue;
+			if(ep->type == Eintr && ep->dir == Ein && epintr == -1)
+				epintr = ep->id;
+		}
+	}
+
 	/*
 	 * interfc 0 means start from the start which is equiv to
 	 * iterate through endpoints probably, could be done better
 	 */
-	eps = ser->dev->usb->conf[0]->iface[ifc]->ep;
+	if (!ser->isacm)
+		eps = ser->dev->usb->conf[0]->iface[ifc]->ep;
+	else
+		eps = ser->dev->usb->conf[0]->iface[ser->p[ifc].data_ifc]->ep;
 
 	for(i = 0; i < Nep; i++){
 		if((ep = eps[i]) == nil)
 			continue;
-		if(ser->hasepintr && ep->type == Eintr &&
-		    ep->dir == Ein && epintr == -1)
-			epintr = ep->id;
+		if(!ser->isacm && ser->hasepintr &&
+			ep->type == Eintr && ep->dir == Ein && epintr == -1)
+				epintr = ep->id;
 		if(ep->type == Ebulk){
 			if((ep->dir == Ein || ep->dir == Eboth) && epin == -1)
 				epin = ep->id;
@@ -714,6 +728,7 @@
 extern int slprobe(Serial *ser);
 extern int chprobe(Serial *ser);
 extern int uconsprobe(Serial *ser);
+extern int cdcprobe(Serial *ser);
 
 void
 threadmain(int argc, char* argv[])
@@ -750,7 +765,8 @@
 	&& ftprobe(ser)
 	&& slprobe(ser)
 	&& plprobe(ser)
-	&& chprobe(ser))
+	&& chprobe(ser)
+	&& cdcprobe(ser))
 		sysfatal("no serial devices found");
 
 	for(i = 0; i < ser->nifcs; i++){
diff -r bd503458654d sys/src/cmd/nusb/serial/serial.h
--- a/sys/src/cmd/nusb/serial/serial.h	Sun Oct 04 22:45:22 2020 +0200
+++ b/sys/src/cmd/nusb/serial/serial.h	Mon Oct 05 14:03:47 2020 -0700
@@ -60,6 +60,10 @@
 
 	int	interfc;	/* interfc on the device for ftdi */
 
+	int	hasbreak;	/* CDC ACM may not have break */
+	int	ctl_ifc;	/* epintr iface for CDC ACM */
+	int	data_ifc;	/* epin/out iface for CDC ACM */
+
 	Channel *w4data;
 	Channel *gotdata;
 	int	ndata;
@@ -81,6 +85,7 @@
 
 	int	jtag;		/* index of jtag interface, -1 none */
 	int	nifcs;		/* # of serial interfaces, including JTAG */
+	int	isacm; 		/* CDC ACM interfaces handled differently */
 	Serialport p[Maxifc];
 	int	maxrtrans;
 	int	maxwtrans;

  reply	other threads:[~2020-10-05 21:54 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-30 21:18 Eli Cohen
2020-07-30 21:48 ` Eli Cohen
2020-07-31 15:38   ` [9front] " ori
2020-07-31 11:18 ` [9front] " Ethan Gardener
2020-08-01  5:35   ` Anthony Martin
2020-08-01 15:34     ` Ethan Gardener
2020-08-01 16:45       ` Eli Cohen
2020-08-01 20:32         ` Ethan Gardener
2020-08-02 13:34           ` cinap_lenrek
2020-08-03  6:27             ` Eli Cohen
2020-10-05 21:53               ` Eli Cohen [this message]
2020-08-01 15:34 ` cinap_lenrek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAHwi9bwc5iiqRnMpnyX4SEuxEcehz0-f4zvAF3X+iKctjv5fvQ@mail.gmail.com \
    --to=echoline@gmail.com \
    --cc=9front@9front.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).