Adding a generic tty module based largely on the tty driver. This has a new API
and doesn't come with BeOS backwards compatibility. It also has the BeOS compatibility ioctl ops removed and such. I've actually made this back in april, so I don't really remember any more details. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@39760 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
parent
deb25b1cc2
commit
91bcf08ee5
73
headers/os/drivers/tty/tty_module.h
Normal file
73
headers/os/drivers/tty/tty_module.h
Normal file
@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright 2010, Haiku Inc. All Rights Reserved.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
|
||||
#ifndef _TTY_MODULE_H
|
||||
#define _TTY_MODULE_H
|
||||
|
||||
#include <module.h>
|
||||
#include <termios.h>
|
||||
#include <Select.h>
|
||||
|
||||
struct tty;
|
||||
struct tty_cookie;
|
||||
|
||||
typedef bool (*tty_service_func)(struct tty *tty, uint32 op, void *buffer,
|
||||
size_t length);
|
||||
|
||||
// flags
|
||||
#define TTYCARRIER (1 << 0)
|
||||
#define TTYWRITABLE (1 << 1)
|
||||
#define TTYWRITING (1 << 2)
|
||||
#define TTYREADING (1 << 3)
|
||||
#define TTYOSTOPPED (1 << 4)
|
||||
#define TTYEXCLUSIVE (1 << 5)
|
||||
#define TTYHWDCD (1 << 6)
|
||||
#define TTYHWCTS (1 << 7)
|
||||
#define TTYHWDSR (1 << 8)
|
||||
#define TTYHWRI (1 << 9)
|
||||
#define TTYFLOWFORCED (1 << 10)
|
||||
|
||||
// ops
|
||||
#define TTYENABLE 0 /* bool enabled */
|
||||
#define TTYSETMODES 1 /* struct termios termios */
|
||||
#define TTYOSTART 2
|
||||
#define TTYOSYNC 3
|
||||
#define TTYISTOP 4 /* bool stopInput */
|
||||
#define TTYSETBREAK 5 /* bool break */
|
||||
#define TTYSETDTR 6 /* bool dataTerminalReady */
|
||||
#define TTYSETRTS 7 /* bool requestToSend */
|
||||
#define TTYGETSIGNALS 8 /* call tty_hardware_signal for all bits */
|
||||
|
||||
typedef struct tty_module_info tty_module_info;
|
||||
|
||||
struct tty_module_info {
|
||||
module_info mi;
|
||||
|
||||
struct tty *(*tty_create)(tty_service_func serviceFunction, bool isMaster);
|
||||
void (*tty_destroy)(struct tty *tty);
|
||||
|
||||
struct tty_cookie *
|
||||
(*tty_create_cookie)(struct tty *masterTTY, struct tty *slaveTTY,
|
||||
uint32 openMode);
|
||||
void (*tty_destroy_cookie)(struct tty_cookie *cookie);
|
||||
|
||||
status_t (*tty_read)(struct tty_cookie *cookie, void *_buffer,
|
||||
size_t *_length);
|
||||
status_t (*tty_write)(struct tty_cookie *cookie, const void *buffer,
|
||||
size_t *length);
|
||||
status_t (*tty_control)(struct tty_cookie *cookie, uint32 op,
|
||||
void *buffer, size_t length);
|
||||
status_t (*tty_select)(struct tty_cookie *cookie, uint8 event,
|
||||
uint32 ref, selectsync *sync);
|
||||
status_t (*tty_deselect)(struct tty_cookie *cookie, uint8 event,
|
||||
selectsync *sync);
|
||||
|
||||
status_t (*tty_hardware_signal)(struct tty_cookie *cookie,
|
||||
int signal, bool);
|
||||
};
|
||||
|
||||
#define B_TTY_MODULE_NAME "generic/tty/v1"
|
||||
|
||||
#endif /* _TTY_MODULE_H */
|
@ -1,4 +1,4 @@
|
||||
SubDir HAIKU_TOP src add-ons kernel generic ;
|
||||
SubDir HAIKU_TOP src add-ons kernel generic ;
|
||||
|
||||
SubInclude HAIKU_TOP src add-ons kernel generic ata_adapter ;
|
||||
SubInclude HAIKU_TOP src add-ons kernel generic atomizer ;
|
||||
@ -7,3 +7,4 @@ SubInclude HAIKU_TOP src add-ons kernel generic ide_adapter ;
|
||||
SubInclude HAIKU_TOP src add-ons kernel generic locked_pool ;
|
||||
SubInclude HAIKU_TOP src add-ons kernel generic mpu401 ;
|
||||
SubInclude HAIKU_TOP src add-ons kernel generic scsi_periph ;
|
||||
SubInclude HAIKU_TOP src add-ons kernel generic tty ;
|
||||
|
10
src/add-ons/kernel/generic/tty/Jamfile
Normal file
10
src/add-ons/kernel/generic/tty/Jamfile
Normal file
@ -0,0 +1,10 @@
|
||||
SubDir HAIKU_TOP src add-ons kernel generic tty ;
|
||||
|
||||
UsePrivateKernelHeaders ;
|
||||
UsePrivateHeaders drivers ;
|
||||
|
||||
KernelAddon <module>tty :
|
||||
line_buffer.cpp
|
||||
module.cpp
|
||||
tty.cpp
|
||||
;
|
169
src/add-ons/kernel/generic/tty/line_buffer.cpp
Normal file
169
src/add-ons/kernel/generic/tty/line_buffer.cpp
Normal file
@ -0,0 +1,169 @@
|
||||
/*
|
||||
* Copyright 2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
|
||||
|
||||
#include "line_buffer.h"
|
||||
|
||||
#include <KernelExport.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
status_t
|
||||
clear_line_buffer(struct line_buffer &buffer)
|
||||
{
|
||||
buffer.in = 0;
|
||||
buffer.first = 0;
|
||||
return B_OK;
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
init_line_buffer(struct line_buffer &buffer, size_t size)
|
||||
{
|
||||
clear_line_buffer(buffer);
|
||||
|
||||
buffer.buffer = (char *)malloc(size);
|
||||
if (buffer.buffer == NULL)
|
||||
return B_NO_MEMORY;
|
||||
|
||||
buffer.size = size;
|
||||
|
||||
return B_OK;
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
uninit_line_buffer(struct line_buffer &buffer)
|
||||
{
|
||||
free(buffer.buffer);
|
||||
return B_OK;
|
||||
}
|
||||
|
||||
|
||||
int32
|
||||
line_buffer_readable(struct line_buffer &buffer)
|
||||
{
|
||||
return buffer.in;
|
||||
}
|
||||
|
||||
|
||||
int32
|
||||
line_buffer_readable_line(struct line_buffer &buffer, char eol, char eof)
|
||||
{
|
||||
size_t size = buffer.in;
|
||||
if (size == 0)
|
||||
return 0;
|
||||
|
||||
// find EOL or EOF char
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
char c = buffer.buffer[(buffer.first + i) % buffer.size];
|
||||
if (c == eol || c == '\n' || c == '\r' || c == eof)
|
||||
return i + 1;
|
||||
}
|
||||
|
||||
// If the buffer is full, but doesn't contain a EOL or EOF, we report the
|
||||
// full size anyway, since otherwise the reader would wait forever.
|
||||
return buffer.in == buffer.size ? buffer.in : 0;
|
||||
}
|
||||
|
||||
|
||||
int32
|
||||
line_buffer_writable(struct line_buffer &buffer)
|
||||
{
|
||||
return buffer.size - buffer.in;
|
||||
}
|
||||
|
||||
|
||||
ssize_t
|
||||
line_buffer_user_read(struct line_buffer &buffer, char *data, size_t length,
|
||||
char eof, bool* hitEOF)
|
||||
{
|
||||
size_t available = buffer.in;
|
||||
|
||||
if (length > available)
|
||||
length = available;
|
||||
|
||||
if (length == 0)
|
||||
return 0;
|
||||
|
||||
// check for EOF, if the caller asked us to
|
||||
if (hitEOF) {
|
||||
*hitEOF = false;
|
||||
for (size_t i = 0; i < available; i++) {
|
||||
char c = buffer.buffer[(buffer.first + i) % buffer.size];
|
||||
if (c == eof) {
|
||||
*hitEOF = true;
|
||||
length = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ssize_t bytesRead = length;
|
||||
|
||||
if (buffer.first + length < buffer.size) {
|
||||
// simple copy
|
||||
if (user_memcpy(data, buffer.buffer + buffer.first, length) != B_OK)
|
||||
bytesRead = B_BAD_ADDRESS;
|
||||
} else {
|
||||
// need to copy both ends
|
||||
size_t upper = buffer.size - buffer.first;
|
||||
size_t lower = length - upper;
|
||||
|
||||
if (user_memcpy(data, buffer.buffer + buffer.first, upper) != B_OK
|
||||
|| user_memcpy(data + upper, buffer.buffer, lower) != B_OK)
|
||||
bytesRead = B_BAD_ADDRESS;
|
||||
}
|
||||
|
||||
if (bytesRead > 0) {
|
||||
buffer.first = (buffer.first + bytesRead) % buffer.size;
|
||||
buffer.in -= bytesRead;
|
||||
}
|
||||
|
||||
// dispose of EOF char
|
||||
if (hitEOF && *hitEOF) {
|
||||
buffer.first = (buffer.first + 1) % buffer.size;
|
||||
buffer.in--;
|
||||
}
|
||||
|
||||
return bytesRead;
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
line_buffer_putc(struct line_buffer &buffer, char c)
|
||||
{
|
||||
if (buffer.in == buffer.size)
|
||||
return B_NO_MEMORY;
|
||||
|
||||
buffer.buffer[(buffer.first + buffer.in++) % buffer.size] = c;
|
||||
return B_OK;
|
||||
}
|
||||
|
||||
|
||||
#if 0
|
||||
status_t
|
||||
line_buffer_getc(struct line_buffer &buffer, char *_c)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
line_buffer_ungetc(struct line_buffer &buffer, char *c)
|
||||
{
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
bool
|
||||
line_buffer_tail_getc(struct line_buffer &buffer, char *c)
|
||||
{
|
||||
if (buffer.in == 0)
|
||||
return false;
|
||||
|
||||
*c = buffer.buffer[(buffer.first + --buffer.in) % buffer.size];
|
||||
return true;
|
||||
}
|
32
src/add-ons/kernel/generic/tty/line_buffer.h
Normal file
32
src/add-ons/kernel/generic/tty/line_buffer.h
Normal file
@ -0,0 +1,32 @@
|
||||
/*
|
||||
** Copyright 2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
|
||||
** Distributed under the terms of the Haiku License.
|
||||
*/
|
||||
#ifndef LINE_BUFFER_H
|
||||
#define LINE_BUFFER_H
|
||||
|
||||
|
||||
#include <OS.h>
|
||||
|
||||
|
||||
struct line_buffer {
|
||||
int32 first;
|
||||
size_t in;
|
||||
size_t size;
|
||||
char *buffer;
|
||||
};
|
||||
|
||||
status_t init_line_buffer(struct line_buffer &buffer, size_t size);
|
||||
status_t uninit_line_buffer(struct line_buffer &buffer);
|
||||
status_t clear_line_buffer(struct line_buffer &buffer);
|
||||
int32 line_buffer_readable(struct line_buffer &buffer);
|
||||
int32 line_buffer_readable_line(struct line_buffer &buffer, char eol, char eof);
|
||||
int32 line_buffer_writable(struct line_buffer &buffer);
|
||||
ssize_t line_buffer_user_read(struct line_buffer &buffer, char *data,
|
||||
size_t length, char eof = 0, bool* hitEOF = NULL);
|
||||
status_t line_buffer_getc(struct line_buffer &buffer, char *_c);
|
||||
status_t line_buffer_putc(struct line_buffer &buffer, char c);
|
||||
status_t line_buffer_ungetc(struct line_buffer &buffer, char *c);
|
||||
bool line_buffer_tail_getc(struct line_buffer &buffer, char *c);
|
||||
|
||||
#endif /* LINE_BUFFER_H */
|
87
src/add-ons/kernel/generic/tty/module.cpp
Normal file
87
src/add-ons/kernel/generic/tty/module.cpp
Normal file
@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright 2010, Michael Lotz, mmlr@mlotz.ch.
|
||||
* Copyright 2004, Axel Dörfler, axeld@pinc-software.de.
|
||||
* Distributed under the terms of the Haiku License.
|
||||
*/
|
||||
|
||||
#include <new>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <lock.h>
|
||||
|
||||
#include <tty/tty_module.h>
|
||||
|
||||
#include "tty_private.h"
|
||||
|
||||
struct mutex gGlobalTTYLock;
|
||||
struct mutex gTTYCookieLock;
|
||||
struct recursive_lock gTTYRequestLock;
|
||||
|
||||
|
||||
static status_t
|
||||
init_tty_module()
|
||||
{
|
||||
// create the request mutex
|
||||
recursive_lock_init(&gTTYRequestLock, "tty requests");
|
||||
|
||||
// create the global mutex
|
||||
mutex_init(&gGlobalTTYLock, "tty global");
|
||||
|
||||
// create the cookie mutex
|
||||
mutex_init(&gTTYCookieLock, "tty cookies");
|
||||
|
||||
return B_OK;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
uninit_tty_module()
|
||||
{
|
||||
recursive_lock_destroy(&gTTYRequestLock);
|
||||
mutex_destroy(&gTTYCookieLock);
|
||||
mutex_destroy(&gGlobalTTYLock);
|
||||
}
|
||||
|
||||
|
||||
static int32
|
||||
tty_module_std_ops(int32 op, ...)
|
||||
{
|
||||
switch (op) {
|
||||
case B_MODULE_INIT:
|
||||
return init_tty_module();
|
||||
|
||||
case B_MODULE_UNINIT:
|
||||
uninit_tty_module();
|
||||
return B_OK;
|
||||
}
|
||||
|
||||
return B_BAD_VALUE;
|
||||
}
|
||||
|
||||
|
||||
static struct tty_module_info sTTYModule = {
|
||||
{
|
||||
B_TTY_MODULE_NAME,
|
||||
0, //B_KEEP_LOADED,
|
||||
tty_module_std_ops
|
||||
},
|
||||
&tty_create,
|
||||
&tty_destroy,
|
||||
&tty_create_cookie,
|
||||
&tty_destroy_cookie,
|
||||
&tty_read,
|
||||
&tty_write,
|
||||
&tty_control,
|
||||
&tty_select,
|
||||
&tty_deselect,
|
||||
&tty_hardware_signal
|
||||
};
|
||||
|
||||
|
||||
module_info *modules[] = {
|
||||
(module_info *)&sTTYModule,
|
||||
NULL
|
||||
};
|
1973
src/add-ons/kernel/generic/tty/tty.cpp
Normal file
1973
src/add-ons/kernel/generic/tty/tty.cpp
Normal file
File diff suppressed because it is too large
Load Diff
169
src/add-ons/kernel/generic/tty/tty_private.h
Normal file
169
src/add-ons/kernel/generic/tty/tty_private.h
Normal file
@ -0,0 +1,169 @@
|
||||
/*
|
||||
* Copyright 2004-2006, Axel Dörfler, axeld@pinc-software.de.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
#ifndef TTY_PRIVATE_H
|
||||
#define TTY_PRIVATE_H
|
||||
|
||||
#include <termios.h>
|
||||
|
||||
#include <Drivers.h>
|
||||
#include <KernelExport.h>
|
||||
#include <tty/tty_module.h>
|
||||
|
||||
#include <condition_variable.h>
|
||||
#include <fs/select_sync_pool.h>
|
||||
#include <lock.h>
|
||||
#include <util/DoublyLinkedList.h>
|
||||
|
||||
#include "line_buffer.h"
|
||||
|
||||
|
||||
#define TTY_BUFFER_SIZE 4096
|
||||
|
||||
// The only nice Rico'ism :)
|
||||
#define CTRL(c) ((c) - 0100)
|
||||
|
||||
|
||||
class RequestOwner;
|
||||
class Semaphore;
|
||||
struct tty;
|
||||
struct tty_cookie;
|
||||
|
||||
class Request : public DoublyLinkedListLinkImpl<Request> {
|
||||
public:
|
||||
Request();
|
||||
|
||||
void Init(RequestOwner *owner, tty_cookie *cookie, size_t bytesNeeded);
|
||||
|
||||
tty_cookie *TTYCookie() const { return fCookie; }
|
||||
|
||||
void Notify(size_t bytesAvailable);
|
||||
void NotifyError(status_t error);
|
||||
|
||||
bool WasNotified() const { return fNotified; }
|
||||
bool HasError() const { return fError; }
|
||||
|
||||
void Dump(const char* prefix);
|
||||
|
||||
private:
|
||||
RequestOwner *fOwner;
|
||||
tty_cookie *fCookie;
|
||||
size_t fBytesNeeded;
|
||||
bool fNotified;
|
||||
bool fError;
|
||||
};
|
||||
|
||||
class RequestQueue {
|
||||
public:
|
||||
RequestQueue();
|
||||
~RequestQueue() {}
|
||||
|
||||
void Add(Request *request);
|
||||
void Remove(Request *request);
|
||||
|
||||
Request *First() const { return fRequests.First(); }
|
||||
bool IsEmpty() const { return fRequests.IsEmpty(); }
|
||||
|
||||
void NotifyFirst(size_t bytesAvailable);
|
||||
void NotifyError(status_t error);
|
||||
void NotifyError(tty_cookie *cookie, status_t error);
|
||||
|
||||
void Dump(const char* prefix);
|
||||
|
||||
private:
|
||||
typedef DoublyLinkedList<Request> RequestList;
|
||||
|
||||
RequestList fRequests;
|
||||
};
|
||||
|
||||
class RequestOwner {
|
||||
public:
|
||||
RequestOwner();
|
||||
|
||||
void Enqueue(tty_cookie *cookie, RequestQueue *queue1,
|
||||
RequestQueue *queue2 = NULL);
|
||||
void Dequeue();
|
||||
|
||||
void SetBytesNeeded(size_t bytesNeeded);
|
||||
size_t BytesNeeded() const { return fBytesNeeded; }
|
||||
|
||||
status_t Wait(bool interruptable, bigtime_t timeout = B_INFINITE_TIMEOUT);
|
||||
|
||||
bool IsFirstInQueues();
|
||||
|
||||
void Notify(Request *request);
|
||||
void NotifyError(Request *request, status_t error);
|
||||
|
||||
status_t Error() const { return fError; }
|
||||
|
||||
private:
|
||||
ConditionVariable* fConditionVariable;
|
||||
tty_cookie* fCookie;
|
||||
status_t fError;
|
||||
RequestQueue* fRequestQueues[2];
|
||||
Request fRequests[2];
|
||||
size_t fBytesNeeded;
|
||||
};
|
||||
|
||||
|
||||
struct tty_cookie : DoublyLinkedListLinkImpl<tty_cookie> {
|
||||
struct tty *tty;
|
||||
struct tty *other_tty;
|
||||
uint32 open_mode;
|
||||
int32 thread_count;
|
||||
sem_id blocking_semaphore;
|
||||
bool closed;
|
||||
};
|
||||
|
||||
typedef DoublyLinkedList<tty_cookie> TTYCookieList;
|
||||
|
||||
struct tty_settings {
|
||||
pid_t pgrp_id;
|
||||
pid_t session_id;
|
||||
struct termios termios;
|
||||
struct winsize window_size;
|
||||
};
|
||||
|
||||
struct tty {
|
||||
int32 ref_count; // referenced by cookies
|
||||
int32 open_count;
|
||||
struct mutex lock;
|
||||
tty_settings settings;
|
||||
select_sync_pool* select_pool;
|
||||
RequestQueue reader_queue;
|
||||
RequestQueue writer_queue;
|
||||
TTYCookieList cookies;
|
||||
line_buffer input_buffer;
|
||||
tty_service_func service_func;
|
||||
uint32 pending_eof;
|
||||
bool is_master;
|
||||
uint8 hardware_bits;
|
||||
};
|
||||
|
||||
|
||||
extern struct mutex gGlobalTTYLock;
|
||||
extern struct mutex gTTYCookieLock;
|
||||
extern struct recursive_lock gTTYRequestLock;
|
||||
|
||||
extern struct tty *tty_create(tty_service_func func, bool isMaster);
|
||||
extern void tty_destroy(struct tty *tty);
|
||||
|
||||
extern tty_cookie *tty_create_cookie(struct tty *tty, struct tty *otherTTY,
|
||||
uint32 openMode);
|
||||
extern void tty_destroy_cookie(tty_cookie *cookie);
|
||||
extern void tty_close_cookie(tty_cookie *cookie);
|
||||
|
||||
extern status_t tty_read(tty_cookie *cookie, void *buffer, size_t *_length);
|
||||
extern status_t tty_write(tty_cookie *sourceCookie, const void *buffer,
|
||||
size_t *_length);
|
||||
extern status_t tty_control(tty_cookie *cookie, uint32 op, void *buffer,
|
||||
size_t length);
|
||||
extern status_t tty_select(tty_cookie *cookie, uint8 event, uint32 ref,
|
||||
selectsync *sync);
|
||||
extern status_t tty_deselect(tty_cookie *cookie, uint8 event, selectsync *sync);
|
||||
|
||||
extern status_t tty_input_lock(tty_cookie* cookie, bool lock);
|
||||
extern status_t tty_hardware_signal(tty_cookie* cookie, int signal, bool);
|
||||
|
||||
#endif /* TTY_PRIVATE_H */
|
Loading…
x
Reference in New Issue
Block a user