From: Eli Cohen <echoline@gmail.com>
To: 9front@9front.org
Subject: Re: [9front] new nusb/serial driver
Date: Thu, 11 Jun 2020 18:28:22 -0700 [thread overview]
Message-ID: <CAHwi9bziLUEYYFKdLzLrjqxVr9GrAjABypYF=xFk3UQgO=9_ug@mail.gmail.com> (raw)
In-Reply-To: <46EFE19EBB1B74CCB387820F084840C6@felloff.net>
[-- Attachment #1: Type: text/plain, Size: 1091 bytes --]
This driver still needs work. it's flaky, but I noticed the ftdi
driver is too...
> another issue:
>
> > if(epin == -1 || epout == -1 || (ser->hasepintr && epintr == -1)){
> > ifc++;
> > if(ifc > 1)
> > return -1;
> > goto TOPFINDEPS;
> > }
>
> i think this should not be done here. rather, we should do that in
> the caller of findendendpoints(), and have it continue if it cant
> open a interface. and only fail in the end when all interfaces
> failed to be usable.
>
> the reason is the caller already iterates over the interfaces,
> and the task of this function is to bring the interface up that
> was passed to it.
>
The problem I was having was that with all the other drivers there was
a 1:1 mapping of USB interfaces to serial port interfaces. see what I
mean? ifc is the index of the serial port interface used directly to
index the USB interface. I have changed the code to a loop at least.
There are some other things that need work still. the openbsd umodem
driver does some things this doesn't yet
[-- Attachment #2: serial.patch --]
[-- Type: text/x-patch, Size: 4094 bytes --]
diff -N /sys/src/cmd/nusb/serial/cdc.c nusb/serial/cdc.c
0a1,148
> #include <u.h>
> #include <libc.h>
> #include <thread.h>
> #include <fcall.h>
> #include <9p.h>
> #include "usb.h"
> #include "serial.h"
>
> enum {
> ScACM = 2,
> AT = 1,
> None = 0,
> };
>
> static int
> cdcsetparam(Serialport *p)
> {
> int res;
> uchar vals[7];
> Serial *ser;
>
> ser = p->s;
>
> 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(ser->dev, Rh2d | Rclass | Riface, 0x20, 0, 0, vals, 7);
>
> return res;
> }
>
> static int
> cdcinit(Serialport *p)
> {
> Serial *ser;
>
> p->baud = 115200;
> p->bits = 8;
> p->parity = 0;
> p->stop = 1;
>
> ser = p->s;
>
> cdcsetparam(p);
> incref(ser->dev);
> return 0;
> }
>
> static int
> cdcsetbreak(Serialport *p, int val)
> {
> Serial *ser;
>
> ser = p->s;
> return usbcmd(ser->dev, Rh2d | Rclass | Riface, 0x23, (val != 0? 0xffff: 0x0000), 0, nil, 0);
> }
>
> static int
> cdcsendlines(Serialport *p)
> {
> if(p->rts)
> p->ctlstate |= 0x0002;
> else
> p->ctlstate &= ~0x0002;
> if(p->dtr)
> p->ctlstate |= 0x0001;
> else
> p->ctlstate &= ~0x0001;
>
> return usbcmd(p->s->dev, Rh2d | Rclass | Riface, 0x22, p->ctlstate, 0, nil, 0);
> }
>
> static int
> cdcclearpipes(Serialport *p)
> {
> Serial *ser;
>
> ser = p->s;
>
> if(unstall(ser->dev, p->epout, Eout) < 0)
> dprint(2, "serial: unstall epout: %r\n");
> if(unstall(ser->dev, p->epin, Ein) < 0)
> dprint(2, "serial: unstall epin: %r\n");
> if(unstall(ser->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, c;
> ulong csp;
> Usbdev *ud = ser->dev->usb;
> Ep *ep;
>
> c = 0;
> for (i = 0; i < nelem(ud->ep); i++) {
> if ((ep = ud->ep[i]) == nil)
> continue;
> csp = ep->iface->csp;
> if (Class(csp) == Clcomms && Subclass(csp) == ScACM && Proto(csp) == AT)
> c++;
> }
>
> if (c == 0)
> return -1;
>
> ser->nifcs = 1;
> ser->outhdrsz = 0;
> ser->hasepintr = 1;
> ser->Serialops = cdcops;
> return 0;
> }
diff -N /sys/src/cmd/nusb/serial/mkfile nusb/serial/mkfile
4c4
< 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
diff -N /sys/src/cmd/nusb/serial/serial.c nusb/serial/serial.c
623a624
> int oifc;
624a626
> oifc = ifc;
631c633,634
< eps = ser->dev->usb->conf[0]->iface[ifc]->ep;
---
> do {
> eps = ser->dev->usb->conf[0]->iface[oifc]->ep;
633,643c636,647
< 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(ep->type == Ebulk){
< if((ep->dir == Ein || ep->dir == Eboth) && epin == -1)
< epin = ep->id;
< if((ep->dir == Eout || ep->dir == Eboth) && epout == -1)
< epout = ep->id;
---
> 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(ep->type == Ebulk){
> if((ep->dir == Ein || ep->dir == Eboth) && epin == -1)
> epin = ep->id;
> if((ep->dir == Eout || ep->dir == Eboth) && epout == -1)
> epout = ep->id;
> }
645c649
< }
---
> } while((epin == -1 || epout == -1 || (ser->hasepintr && epintr == -1)) && ++oifc < Niface);
716a721
> extern int cdcprobe(Serial *ser);
753c758,759
< && chprobe(ser))
---
> && chprobe(ser)
> && cdcprobe(ser))
next prev parent reply other threads:[~2020-06-12 1:28 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-06-11 7:03 Eli Cohen
2020-06-11 7:17 ` [9front] " Alex Musolino
2020-06-11 7:19 ` Eli Cohen
2020-06-11 7:29 ` Alex Musolino
2020-06-11 7:36 ` Eli Cohen
2020-06-11 7:57 ` Eli Cohen
2020-06-11 12:15 ` cinap_lenrek
2020-06-11 14:08 ` Eli Cohen
2020-06-11 17:10 ` cinap_lenrek
2020-06-12 1:28 ` Eli Cohen [this message]
2020-06-12 18:02 ` cinap_lenrek
2020-06-12 22:15 ` Eli Cohen
2020-06-13 14:03 ` cinap_lenrek
2020-06-13 14:18 ` Eli Cohen
2020-06-13 15:37 ` 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='CAHwi9bziLUEYYFKdLzLrjqxVr9GrAjABypYF=xFk3UQgO=9_ug@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).