Port of usb_serial to the generic tty module. I don't remember exactly if or how

well this was working, but since it was non-functional on Haiku anyway this
shouldn't hurt.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@39762 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Michael Lotz
2010-12-08 00:32:24 +00:00
parent 503233ca7d
commit b994a18508
6 changed files with 124 additions and 231 deletions
@@ -21,7 +21,6 @@ SerialDevice *gSerialDevices[DEVICES_COUNT];
char *gDeviceNames[DEVICES_COUNT + 1]; char *gDeviceNames[DEVICES_COUNT + 1];
usb_module_info *gUSBModule = NULL; usb_module_info *gUSBModule = NULL;
tty_module_info *gTTYModule = NULL; tty_module_info *gTTYModule = NULL;
struct ddomain gSerialDomain;
sem_id gDriverLock = -1; sem_id gDriverLock = -1;
@@ -188,12 +187,14 @@ uninit_driver()
bool bool
usb_serial_service(struct tty *ptty, struct ddrover *ddr, uint flags) usb_serial_service(struct tty *tty, uint32 op, void *buffer, size_t length)
{ {
TRACE_FUNCALLS("> usb_serial_service(0x%08x, 0x%08x, 0x%08x)\n", ptty, ddr, flags); TRACE_FUNCALLS("> usb_serial_service(%p, 0x%08lx, %p, %lu)\n", tty,
op, buffer, length);
for (int32 i = 0; i < DEVICES_COUNT; i++) { for (int32 i = 0; i < DEVICES_COUNT; i++) {
if (gSerialDevices[i] && gSerialDevices[i]->Service(ptty, ddr, flags)) { if (gSerialDevices[i]
&& gSerialDevices[i]->Service(tty, op, buffer, length)) {
TRACE_FUNCRET("< usb_serial_service() returns: true\n"); TRACE_FUNCRET("< usb_serial_service() returns: true\n");
return true; return true;
} }
@@ -23,7 +23,7 @@
#include "USB3.h" #include "USB3.h"
extern "C" { extern "C" {
#include <ttylayer.h> #include <tty_module.h>
} }
#define DRIVER_NAME "usb_serial" // driver name for debug output #define DRIVER_NAME "usb_serial" // driver name for debug output
@@ -90,7 +90,8 @@ status_t usb_serial_device_removed(void *cookie);
status_t init_hardware(); status_t init_hardware();
void uninit_driver(); void uninit_driver();
bool usb_serial_service(struct tty *ptty, struct ddrover *ddr, uint flags); bool usb_serial_service(struct tty *tty, uint32 op, void *buffer,
size_t length);
status_t usb_serial_open(const char *name, uint32 flags, void **cookie); status_t usb_serial_open(const char *name, uint32 flags, void **cookie);
status_t usb_serial_read(void *cookie, off_t position, void *buffer, size_t *numBytes); status_t usb_serial_read(void *cookie, off_t position, void *buffer, size_t *numBytes);
@@ -35,11 +35,14 @@ SerialDevice::SerialDevice(usb_device device, uint16 vendorID,
fDoneWrite(-1), fDoneWrite(-1),
fControlOut(0), fControlOut(0),
fInputStopped(false), fInputStopped(false),
fMasterTTY(NULL),
fSlaveTTY(NULL),
fTTYCookie(NULL),
fDeviceThread(-1), fDeviceThread(-1),
fStopDeviceThread(false) fStopDeviceThread(false)
{ {
memset(&fTTYFile, 0, sizeof(ttyfile)); mutex_init(&fReadLock, "usb_serial read lock");
memset(&fTTY, 0, sizeof(tty)); mutex_init(&fWriteLock, "usb_serial write lock");
} }
@@ -101,12 +104,10 @@ SerialDevice::SetWritePipe(usb_pipe handle)
void void
SerialDevice::SetModes() SerialDevice::SetModes(struct termios *tios)
{ {
struct termios tios;
memcpy(&tios, &fTTY.t, sizeof(struct termios));
uint16 newControl = fControlOut; uint16 newControl = fControlOut;
TRACE_FUNCRES(trace_termios, &tios); TRACE_FUNCRES(trace_termios, tios);
static uint32 baudRates[] = { static uint32 baudRates[] = {
0x00000000, //B0 0x00000000, //B0
@@ -133,28 +134,26 @@ SerialDevice::SetModes()
}; };
uint32 baudCount = sizeof(baudRates) / sizeof(baudRates[0]); uint32 baudCount = sizeof(baudRates) / sizeof(baudRates[0]);
uint32 baudIndex = tios.c_cflag & CBAUD; uint32 baudIndex = tios->c_cflag & CBAUD;
if (baudIndex == 0)
baudIndex = tios->c_ispeed;
if (baudIndex == 0)
baudIndex = tios->c_ospeed;
if (baudIndex > baudCount) if (baudIndex > baudCount)
baudIndex = baudCount - 1; baudIndex = baudCount - 1;
usb_serial_line_coding lineCoding; usb_serial_line_coding lineCoding;
lineCoding.speed = baudRates[baudIndex]; lineCoding.speed = baudRates[baudIndex];
lineCoding.stopbits = (tios.c_cflag & CSTOPB) ? LC_STOP_BIT_2 : LC_STOP_BIT_1; lineCoding.stopbits = (tios->c_cflag & CSTOPB) ? LC_STOP_BIT_2 : LC_STOP_BIT_1;
if (tios.c_cflag & PARENB) { if (tios->c_cflag & PARENB) {
lineCoding.parity = LC_PARITY_EVEN; lineCoding.parity = LC_PARITY_EVEN;
if (tios.c_cflag & PARODD) if (tios->c_cflag & PARODD)
lineCoding.parity = LC_PARITY_ODD; lineCoding.parity = LC_PARITY_ODD;
} else } else
lineCoding.parity = LC_PARITY_NONE; lineCoding.parity = LC_PARITY_NONE;
lineCoding.databits = (tios.c_cflag & CS8) ? 8 : 7; lineCoding.databits = (tios->c_cflag & CS8) ? 8 : 7;
if (lineCoding.speed == 0) {
newControl &= 0xfffffffe;
lineCoding.speed = fLineCoding.speed;
} else
newControl = CLS_LINE_DTR;
if (fControlOut != newControl) { if (fControlOut != newControl) {
fControlOut = newControl; fControlOut = newControl;
@@ -176,64 +175,64 @@ SerialDevice::SetModes()
bool bool
SerialDevice::Service(struct tty *ptty, struct ddrover *ddr, uint flags) SerialDevice::Service(struct tty *tty, uint32 op, void *buffer, size_t length)
{ {
if (&fTTY != ptty) if (tty != fMasterTTY)
return false; return false;
if (flags <= TTYGETSIGNALS) { switch (op) {
switch (flags) { case TTYENABLE:
case TTYENABLE: {
TRACE("TTYENABLE\n"); bool enable = *(bool *)buffer;
gTTYModule->ttyhwsignal(ptty, ddr, TTYHWDCD, false); TRACE("TTYENABLE: %sable\n", enable ? "en" : "dis");
gTTYModule->ttyhwsignal(ptty, ddr, TTYHWCTS, true);
fControlOut = CLS_LINE_DTR | CLS_LINE_RTS;
SetControlLineState(fControlOut);
break;
case TTYDISABLE: gTTYModule->tty_hardware_signal(fTTYCookie, TTYHWDCD, enable);
TRACE("TTYDISABLE\n"); gTTYModule->tty_hardware_signal(fTTYCookie, TTYHWCTS, enable);
gTTYModule->ttyhwsignal(ptty, ddr, TTYHWDCD, false);
fControlOut = 0x0;
SetControlLineState(fControlOut);
break;
case TTYISTOP: fControlOut = enable ? CLS_LINE_DTR | CLS_LINE_RTS : 0;
TRACE("TTYISTOP\n"); SetControlLineState(fControlOut);
fInputStopped = true; return true;
gTTYModule->ttyhwsignal(ptty, ddr, TTYHWCTS, false);
break;
case TTYIRESUME:
TRACE("TTYIRESUME\n");
gTTYModule->ttyhwsignal(ptty, ddr, TTYHWCTS, true);
fInputStopped = false;
break;
case TTYGETSIGNALS:
TRACE("TTYGETSIGNALS\n");
gTTYModule->ttyhwsignal(ptty, ddr, TTYHWDCD, true);
gTTYModule->ttyhwsignal(ptty, ddr, TTYHWCTS, true);
gTTYModule->ttyhwsignal(ptty, ddr, TTYHWDSR, false);
gTTYModule->ttyhwsignal(ptty, ddr, TTYHWRI, false);
break;
case TTYSETMODES:
TRACE("TTYSETMODES\n");
SetModes();
break;
case TTYOSTART:
case TTYOSYNC:
case TTYSETBREAK:
case TTYCLRBREAK:
case TTYSETDTR:
case TTYCLRDTR:
TRACE("TTY other\n");
break;
} }
return true; case TTYISTOP:
fInputStopped = *(bool *)buffer;
TRACE("TTYISTOP: %sstopped\n", fInputStopped ? "" : "not ");
gTTYModule->tty_hardware_signal(fTTYCookie, TTYHWCTS, !fInputStopped);
return true;
case TTYGETSIGNALS:
TRACE("TTYGETSIGNALS\n");
gTTYModule->tty_hardware_signal(fTTYCookie, TTYHWDCD,
(fControlOut & (CLS_LINE_DTR | CLS_LINE_RTS)) != 0);
gTTYModule->tty_hardware_signal(fTTYCookie, TTYHWCTS, !fInputStopped);
gTTYModule->tty_hardware_signal(fTTYCookie, TTYHWDSR, false);
gTTYModule->tty_hardware_signal(fTTYCookie, TTYHWRI, false);
return true;
case TTYSETMODES:
TRACE("TTYSETMODES\n");
SetModes((struct termios *)buffer);
return true;
case TTYSETDTR:
case TTYSETRTS:
{
bool set = *(bool *)buffer;
uint8 bit = TTYSETDTR ? CLS_LINE_DTR : CLS_LINE_RTS;
if (set)
fControlOut |= bit;
else
fControlOut &= ~bit;
SetControlLineState(fControlOut);
return true;
}
case TTYOSTART:
case TTYOSYNC:
case TTYSETBREAK:
TRACE("TTY other\n");
return true;
} }
return false; return false;
@@ -249,24 +248,29 @@ SerialDevice::Open(uint32 flags)
if (fDeviceRemoved) if (fDeviceRemoved)
return B_DEV_NOT_READY; return B_DEV_NOT_READY;
gTTYModule->ttyinit(&fTTY, true); fMasterTTY = gTTYModule->tty_create(usb_serial_service, true);
fTTYFile.tty = &fTTY; if (fMasterTTY == NULL) {
fTTYFile.flags = flags; TRACE_ALWAYS("open: failed to init master tty\n");
ResetDevice();
struct ddrover *ddr = gTTYModule->ddrstart(NULL);
if (!ddr)
return B_NO_MEMORY; return B_NO_MEMORY;
gTTYModule->ddacquire(ddr, &gSerialDomain);
status_t status = gTTYModule->ttyopen(&fTTYFile, ddr, usb_serial_service);
gTTYModule->ddrdone(ddr);
if (status < B_OK) {
TRACE_ALWAYS("open: failed to open tty\n");
return status;
} }
fSlaveTTY = gTTYModule->tty_create(NULL, false);
if (fSlaveTTY == NULL) {
TRACE_ALWAYS("open: failed to init slave tty\n");
gTTYModule->tty_destroy(fMasterTTY);
return B_NO_MEMORY;
}
fTTYCookie = gTTYModule->tty_create_cookie(fMasterTTY, fSlaveTTY, flags);
if (fTTYCookie == NULL) {
TRACE_ALWAYS("open: failed to init tty cookie\n");
gTTYModule->tty_destroy(fMasterTTY);
gTTYModule->tty_destroy(fSlaveTTY);
return B_NO_MEMORY;
}
ResetDevice();
fDeviceThread = spawn_kernel_thread(DeviceThread, "usb_serial device thread", fDeviceThread = spawn_kernel_thread(DeviceThread, "usb_serial device thread",
B_NORMAL_PRIORITY, this); B_NORMAL_PRIORITY, this);
@@ -280,8 +284,9 @@ SerialDevice::Open(uint32 flags)
fControlOut = CLS_LINE_DTR | CLS_LINE_RTS; fControlOut = CLS_LINE_DTR | CLS_LINE_RTS;
SetControlLineState(fControlOut); SetControlLineState(fControlOut);
status = gUSBModule->queue_interrupt(fControlPipe, fInterruptBuffer, status_t status = gUSBModule->queue_interrupt(fControlPipe,
fInterruptBufferSize, InterruptCallbackFunction, this); fInterruptBuffer, fInterruptBufferSize, InterruptCallbackFunction,
this);
if (status < B_OK) if (status < B_OK)
TRACE_ALWAYS("failed to queue initial interrupt\n"); TRACE_ALWAYS("failed to queue initial interrupt\n");
@@ -305,15 +310,7 @@ SerialDevice::Read(char *buffer, size_t *numBytes)
return status; return status;
} }
struct ddrover *ddr = gTTYModule->ddrstart(NULL); status = gTTYModule->tty_read(fTTYCookie, buffer, numBytes);
if (!ddr) {
*numBytes = 0;
mutex_unlock(&fReadLock);
return B_NO_MEMORY;
}
status = gTTYModule->ttyread(&fTTYFile, ddr, buffer, numBytes);
gTTYModule->ddrdone(ddr);
mutex_unlock(&fReadLock); mutex_unlock(&fReadLock);
return status; return status;
@@ -383,13 +380,7 @@ SerialDevice::Control(uint32 op, void *arg, size_t length)
if (fDeviceRemoved) if (fDeviceRemoved)
return B_DEV_NOT_READY; return B_DEV_NOT_READY;
struct ddrover *ddr = gTTYModule->ddrstart(NULL); return gTTYModule->tty_control(fTTYCookie, op, arg, length);
if (!ddr)
return B_NO_MEMORY;
status_t status = gTTYModule->ttycontrol(&fTTYFile, ddr, op, arg, length);
gTTYModule->ddrdone(ddr);
return status;
} }
@@ -399,13 +390,7 @@ SerialDevice::Select(uint8 event, uint32 ref, selectsync *sync)
if (fDeviceRemoved) if (fDeviceRemoved)
return B_DEV_NOT_READY; return B_DEV_NOT_READY;
struct ddrover *ddr = gTTYModule->ddrstart(NULL); return gTTYModule->tty_select(fTTYCookie, event, ref, sync);
if (!ddr)
return B_NO_MEMORY;
status_t status = gTTYModule->ttyselect(&fTTYFile, ddr, event, ref, sync);
gTTYModule->ddrdone(ddr);
return status;
} }
@@ -415,13 +400,7 @@ SerialDevice::DeSelect(uint8 event, selectsync *sync)
if (fDeviceRemoved) if (fDeviceRemoved)
return B_DEV_NOT_READY; return B_DEV_NOT_READY;
struct ddrover *ddr = gTTYModule->ddrstart(NULL); return gTTYModule->tty_deselect(fTTYCookie, event, sync);
if (!ddr)
return B_NO_MEMORY;
status_t status = gTTYModule->ttydeselect(&fTTYFile, ddr, event, sync);
gTTYModule->ddrdone(ddr);
return status;
} }
@@ -436,28 +415,19 @@ SerialDevice::Close()
gUSBModule->cancel_queued_transfers(fControlPipe); gUSBModule->cancel_queued_transfers(fControlPipe);
} }
struct ddrover *ddr = gTTYModule->ddrstart(NULL); gTTYModule->tty_destroy_cookie(fTTYCookie);
if (!ddr)
return B_NO_MEMORY;
status_t status = gTTYModule->ttyclose(&fTTYFile, ddr);
gTTYModule->ddrdone(ddr);
fDeviceOpen = false; fDeviceOpen = false;
return status; return B_OK;
} }
status_t status_t
SerialDevice::Free() SerialDevice::Free()
{ {
struct ddrover *ddr = gTTYModule->ddrstart(NULL); gTTYModule->tty_destroy(fMasterTTY);
if (!ddr) gTTYModule->tty_destroy(fSlaveTTY);
return B_NO_MEMORY; return B_OK;
status_t status = gTTYModule->ttyfree(&fTTYFile, ddr);
gTTYModule->ddrdone(ddr);
return status;
} }
@@ -575,21 +545,10 @@ SerialDevice::DeviceThread(void *data)
if (readLength == 0) if (readLength == 0)
continue; continue;
ddrover *ddr = gTTYModule->ddrstart(NULL);
if (!ddr) {
TRACE_ALWAYS("device thread: ddrstart problem\n");
return B_NO_MEMORY;
}
while (device->fInputStopped) while (device->fInputStopped)
snooze(100); snooze(100);
gTTYModule->ttyilock(&device->fTTY, ddr, true); gTTYModule->tty_write(device->fTTYCookie, buffer, &readLength);
for (size_t i = 0; i < readLength; i++)
gTTYModule->ttyin(&device->fTTY, ddr, buffer[i]);
gTTYModule->ttyilock(&device->fTTY, ddr, false);
gTTYModule->ddrdone(ddr);
} }
return B_OK; return B_OK;
@@ -42,9 +42,9 @@ static SerialDevice * MakeDevice(usb_device device, uint16 vendorID,
char * WriteBuffer() { return fWriteBuffer; }; char * WriteBuffer() { return fWriteBuffer; };
size_t WriteBufferSize() { return fWriteBufferSize; }; size_t WriteBufferSize() { return fWriteBufferSize; };
void SetModes(); void SetModes(struct termios *tios);
bool Service(struct tty *ptty, struct ddrover *ddr, bool Service(struct tty *tty, uint32 op,
uint flags); void *buffer, size_t length);
status_t Open(uint32 flags); status_t Open(uint32 flags);
status_t Read(char *buffer, size_t *numBytes); status_t Read(char *buffer, size_t *numBytes);
@@ -68,7 +68,7 @@ virtual status_t SetLineCoding(usb_serial_line_coding *coding);
virtual status_t SetControlLineState(uint16 state); virtual status_t SetControlLineState(uint16 state);
virtual void OnRead(char **buffer, size_t *numBytes); virtual void OnRead(char **buffer, size_t *numBytes);
virtual void OnWrite(const char *buffer, size_t *numBytes, virtual void OnWrite(const char *buffer, size_t *numBytes,
size_t *packetBytes); size_t *packetBytes);
virtual void OnClose(); virtual void OnClose();
@@ -127,8 +127,9 @@ static void InterruptCallbackFunction(void *cookie,
uint16 fControlOut; uint16 fControlOut;
bool fInputStopped; bool fInputStopped;
struct ttyfile fTTYFile; struct tty * fMasterTTY;
struct tty fTTY; struct tty * fSlaveTTY;
struct tty_cookie * fTTYCookie;
/* device thread management */ /* device thread management */
thread_id fDeviceThread; thread_id fDeviceThread;
@@ -18,14 +18,14 @@
#if DEBUG #if DEBUG
bool gLogEnabled = true; bool gLogEnabled = true;
#else #else
bool gLogEnabled = false; bool gLogEnabled = true;
#endif #endif
bool gLogToFile = false; bool gLogToFile = false;
bool gLogAppend = false; bool gLogAppend = false;
bool gLogFunctionCalls = false; bool gLogFunctionCalls = true;
bool gLogFunctionReturns = false; bool gLogFunctionReturns = true;
bool gLogFunctionResults = false; bool gLogFunctionResults = true;
static const char *sLogFilePath="/boot/home/"DRIVER_NAME".log"; static const char *sLogFilePath="/boot/home/"DRIVER_NAME".log";
static sem_id sLogLock; static sem_id sLogLock;
@@ -99,15 +99,6 @@ usb_serial_trace(bool force, char *format, ...)
} }
void
trace_ddomain(struct ddomain *dd)
{
TRACE("struct ddomain:\n"
"\tddrover: 0x%08x\n"
"\tbg: %d, locked: %d\n", dd->r, dd->bg, dd->locked);
}
void void
trace_termios(struct termios *tios) trace_termios(struct termios *tios)
{ {
@@ -120,66 +111,10 @@ trace_termios(struct termios *tios)
// "\tc_ixxxxx: 0x%08x\n" // "\tc_ixxxxx: 0x%08x\n"
// "\tc_oxxxxx: 0x%08x\n" // "\tc_oxxxxx: 0x%08x\n"
"\tc_cc[0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x]\n", "\tc_cc[0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x]\n",
tios->c_iflag, tios->c_oflag, tios->c_cflag, tios->c_lflag, tios->c_iflag, tios->c_oflag, tios->c_cflag, tios->c_lflag,
tios->c_line, tios->c_line,
// tios->c_ixxxxx, tios->c_oxxxxx, // tios->c_ixxxxx, tios->c_oxxxxx,
tios->c_cc[0], tios->c_cc[1], tios->c_cc[2], tios->c_cc[3], tios->c_cc[0], tios->c_cc[1], tios->c_cc[2], tios->c_cc[3],
tios->c_cc[4], tios->c_cc[5], tios->c_cc[6], tios->c_cc[7], tios->c_cc[4], tios->c_cc[5], tios->c_cc[6], tios->c_cc[7],
tios->c_cc[8], tios->c_cc[9], tios->c_cc[10]); tios->c_cc[8], tios->c_cc[9], tios->c_cc[10]);
} }
void
trace_str(struct str *str)
{
TRACE("struct str:\n"
"\tbuffer: 0x%08x\n"
"\tbufsize: %d\n"
"\tcount: %d\n"
"\ttail: %d\n"
"\tallocated: %d\n",
str->buffer, str->bufsize, str->count, str->tail, str->allocated);
}
void
trace_winsize(struct winsize *ws)
{
TRACE("struct winsize:\n"
"\tws_row: %d\n"
"\tws_col: %d\n"
"\tws_xpixel: %d\n"
"\tws_ypixel: %d\n",
ws->ws_row, ws->ws_col, ws->ws_xpixel, ws->ws_ypixel);
}
void
trace_tty(struct tty *tty)
{
TRACE("struct tty:\n"
"\tnopen: %d, flags: 0x%08x,\n", tty->nopen, tty->flags);
TRACE("ddomain dd:\n");
trace_ddomain(&tty->dd);
TRACE("ddomain ddi:\n");
trace_ddomain(&tty->ddi);
TRACE("\tpgid: %08x\n", tty->pgid);
TRACE("termios t:");
trace_termios(&tty->t);
TRACE("\tiactivity: %d, ibusy: %d\n", tty->iactivity, tty->ibusy);
TRACE("str istr:\n");
trace_str(&tty->istr);
TRACE("str rstr:\n");
trace_str(&tty->rstr);
TRACE("str ostr:\n");
trace_str(&tty->ostr);
TRACE("winsize wsize:\n");
trace_winsize(&tty->wsize);
TRACE("\tservice: 0x%08x\n", tty->service);
}
@@ -30,10 +30,6 @@ extern bool gLogFunctionResults;
if (gLogFunctionResults) \ if (gLogFunctionResults) \
func(param); func(param);
void trace_ddomain(struct ddomain *dd);
void trace_termios(struct termios *tios); void trace_termios(struct termios *tios);
void trace_str(struct str *str);
void trace_winsize(struct winsize *ws);
void trace_tty(struct tty *tty);
#endif //_USB_SERIAL_TRACING_H_ #endif //_USB_SERIAL_TRACING_H_