📄 ircomm_tty.c
字号:
/********************************************************************* * * Filename: ircomm_tty.c * Version: 1.0 * Description: IrCOMM serial TTY driver * Status: Experimental. * Author: Dag Brattli <dagb@cs.uit.no> * Created at: Sun Jun 6 21:00:56 1999 * Modified at: Wed Feb 23 00:09:02 2000 * Modified by: Dag Brattli <dagb@cs.uit.no> * Sources: serial.c and previous IrCOMM work by Takahide Higuchi * * Copyright (c) 1999-2000 Dag Brattli, All Rights Reserved. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA * ********************************************************************/#include <linux/config.h>#include <linux/init.h>#include <linux/module.h>#include <linux/fs.h>#include <linux/sched.h>#include <linux/termios.h>#include <linux/tty.h>#include <linux/interrupt.h>#include <asm/segment.h>#include <asm/uaccess.h>#include <net/irda/irda.h>#include <net/irda/irmod.h>#include <net/irda/ircomm_core.h>#include <net/irda/ircomm_param.h>#include <net/irda/ircomm_tty_attach.h>#include <net/irda/ircomm_tty.h>static int ircomm_tty_open(struct tty_struct *tty, struct file *filp);static void ircomm_tty_close(struct tty_struct * tty, struct file *filp);static int ircomm_tty_write(struct tty_struct * tty, int from_user, const unsigned char *buf, int count);static int ircomm_tty_write_room(struct tty_struct *tty);static void ircomm_tty_throttle(struct tty_struct *tty);static void ircomm_tty_unthrottle(struct tty_struct *tty);static int ircomm_tty_chars_in_buffer(struct tty_struct *tty);static void ircomm_tty_flush_buffer(struct tty_struct *tty);static void ircomm_tty_send_xchar(struct tty_struct *tty, char ch);static void ircomm_tty_wait_until_sent(struct tty_struct *tty, int timeout);static void ircomm_tty_hangup(struct tty_struct *tty);static void ircomm_tty_do_softint(void *private_);static void ircomm_tty_shutdown(struct ircomm_tty_cb *self);static int ircomm_tty_data_indication(void *instance, void *sap, struct sk_buff *skb);static int ircomm_tty_control_indication(void *instance, void *sap, struct sk_buff *skb);static void ircomm_tty_flow_indication(void *instance, void *sap, LOCAL_FLOW cmd);#ifdef CONFIG_PROC_FSstatic int ircomm_tty_read_proc(char *buf, char **start, off_t offset, int len, int *eof, void *unused);#endif /* CONFIG_PROC_FS */static struct tty_driver driver;static int ircomm_tty_refcount; /* If we manage several devices */static struct tty_struct *ircomm_tty_table[NR_PTYS];static struct termios *ircomm_tty_termios[NR_PTYS];static struct termios *ircomm_tty_termios_locked[NR_PTYS];hashbin_t *ircomm_tty = NULL;/* * Function ircomm_tty_init() * * Init IrCOMM TTY layer/driver * */int __init ircomm_tty_init(void){ ircomm_tty = hashbin_new(HB_LOCAL); if (ircomm_tty == NULL) { ERROR(__FUNCTION__ "(), can't allocate hashbin!\n"); return -ENOMEM; } memset(&driver, 0, sizeof(struct tty_driver)); driver.magic = TTY_DRIVER_MAGIC; driver.driver_name = "ircomm";#ifdef CONFIG_DEVFS_FS driver.name = "ircomm%d";#else driver.name = "ircomm";#endif driver.major = IRCOMM_TTY_MAJOR; driver.minor_start = IRCOMM_TTY_MINOR; driver.num = IRCOMM_TTY_PORTS; driver.type = TTY_DRIVER_TYPE_SERIAL; driver.subtype = SERIAL_TYPE_NORMAL; driver.init_termios = tty_std_termios; driver.init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL; driver.flags = TTY_DRIVER_REAL_RAW; driver.refcount = &ircomm_tty_refcount; driver.table = ircomm_tty_table; driver.termios = ircomm_tty_termios; driver.termios_locked = ircomm_tty_termios_locked; driver.open = ircomm_tty_open; driver.close = ircomm_tty_close; driver.write = ircomm_tty_write; driver.write_room = ircomm_tty_write_room; driver.chars_in_buffer = ircomm_tty_chars_in_buffer; driver.flush_buffer = ircomm_tty_flush_buffer; driver.ioctl = ircomm_tty_ioctl; driver.throttle = ircomm_tty_throttle; driver.unthrottle = ircomm_tty_unthrottle; driver.send_xchar = ircomm_tty_send_xchar; driver.set_termios = ircomm_tty_set_termios; driver.stop = ircomm_tty_stop; driver.start = ircomm_tty_start; driver.hangup = ircomm_tty_hangup; driver.wait_until_sent = ircomm_tty_wait_until_sent;#ifdef CONFIG_PROC_FS driver.read_proc = ircomm_tty_read_proc;#endif /* CONFIG_PROC_FS */ if (tty_register_driver(&driver)) { ERROR(__FUNCTION__ "Couldn't register serial driver\n"); return -1; } return 0;}#ifdef MODULEstatic void __ircomm_tty_cleanup(struct ircomm_tty_cb *self){ IRDA_DEBUG(0, __FUNCTION__ "()\n"); ASSERT(self != NULL, return;); ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;); ircomm_tty_shutdown(self); self->magic = 0; kfree(self);}/* * Function ircomm_tty_cleanup () * * Remove IrCOMM TTY layer/driver * */void ircomm_tty_cleanup(void){ int ret; IRDA_DEBUG(4, __FUNCTION__"()\n"); ret = tty_unregister_driver(&driver); if (ret) { ERROR(__FUNCTION__ "(), failed to unregister driver\n"); return; } hashbin_delete(ircomm_tty, (FREE_FUNC) __ircomm_tty_cleanup);}#endif /* MODULE *//* * Function ircomm_startup (self) * * * */static int ircomm_tty_startup(struct ircomm_tty_cb *self){ notify_t notify; int ret; IRDA_DEBUG(2, __FUNCTION__ "()\n"); ASSERT(self != NULL, return -1;); ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;); /* Already open */ if (self->flags & ASYNC_INITIALIZED) { IRDA_DEBUG(2, __FUNCTION__ "(), already open so break out!\n"); return 0; } /* Register with IrCOMM */ irda_notify_init(¬ify); /* These callbacks we must handle ourselves */ notify.data_indication = ircomm_tty_data_indication; notify.udata_indication = ircomm_tty_control_indication; notify.flow_indication = ircomm_tty_flow_indication; /* Use the ircomm_tty interface for these ones */ notify.disconnect_indication = ircomm_tty_disconnect_indication; notify.connect_confirm = ircomm_tty_connect_confirm; notify.connect_indication = ircomm_tty_connect_indication; strncpy(notify.name, "ircomm_tty", NOTIFY_MAX_NAME); notify.instance = self; if (!self->ircomm) { self->ircomm = ircomm_open(¬ify, self->service_type, self->line); } if (!self->ircomm) return -ENODEV; self->slsap_sel = self->ircomm->slsap_sel; /* Connect IrCOMM link with remote device */ ret = ircomm_tty_attach_cable(self); if (ret < 0) { ERROR(__FUNCTION__ "(), error attaching cable!\n"); return ret; } self->flags |= ASYNC_INITIALIZED; return 0;}/* * Function ircomm_block_til_ready (self, filp) * * * */static int ircomm_tty_block_til_ready(struct ircomm_tty_cb *self, struct file *filp){ DECLARE_WAITQUEUE(wait, current); int retval; int do_clocal = 0, extra_count = 0; unsigned long flags; struct tty_struct *tty; IRDA_DEBUG(2, __FUNCTION__ "()\n"); tty = self->tty; if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) { /* this is a callout device */ /* just verify that normal device is not in use */ if (self->flags & ASYNC_NORMAL_ACTIVE) return -EBUSY; if ((self->flags & ASYNC_CALLOUT_ACTIVE) && (self->flags & ASYNC_SESSION_LOCKOUT) && (self->session != current->session)) return -EBUSY; if ((self->flags & ASYNC_CALLOUT_ACTIVE) && (self->flags & ASYNC_PGRP_LOCKOUT) && (self->pgrp != current->pgrp)) return -EBUSY; self->flags |= ASYNC_CALLOUT_ACTIVE; return 0; } /* * If non-blocking mode is set, or the port is not enabled, * then make the check up front and then exit. */ if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){ /* nonblock mode is set or port is not enabled */ /* just verify that callout device is not active */ if (self->flags & ASYNC_CALLOUT_ACTIVE) return -EBUSY; self->flags |= ASYNC_NORMAL_ACTIVE; IRDA_DEBUG(1, __FUNCTION__ "(), O_NONBLOCK requested!\n"); return 0; } if (self->flags & ASYNC_CALLOUT_ACTIVE) { if (self->normal_termios.c_cflag & CLOCAL) { IRDA_DEBUG(1, __FUNCTION__ "(), doing CLOCAL!\n"); do_clocal = 1; } } else { if (tty->termios->c_cflag & CLOCAL) { IRDA_DEBUG(1, __FUNCTION__ "(), doing CLOCAL!\n"); do_clocal = 1; } } /* Wait for carrier detect and the line to become * free (i.e., not in use by the callout). While we are in * this loop, self->open_count is dropped by one, so that * mgsl_close() knows when to free things. We restore it upon * exit, either normal or abnormal. */ retval = 0; add_wait_queue(&self->open_wait, &wait); IRDA_DEBUG(2, "%s(%d):block_til_ready before block on %s open_count=%d\n", __FILE__,__LINE__, tty->driver.name, self->open_count ); save_flags(flags); cli(); if (!tty_hung_up_p(filp)) { extra_count = 1; self->open_count--; } restore_flags(flags); self->blocked_open++; while (1) { if (!(self->flags & ASYNC_CALLOUT_ACTIVE) && (tty->termios->c_cflag & CBAUD)) { save_flags(flags); cli(); self->settings.dte |= IRCOMM_RTS + IRCOMM_DTR; ircomm_param_request(self, IRCOMM_DTE, TRUE); restore_flags(flags); } current->state = TASK_INTERRUPTIBLE; if (tty_hung_up_p(filp) || !(self->flags & ASYNC_INITIALIZED)){ retval = (self->flags & ASYNC_HUP_NOTIFY) ? -EAGAIN : -ERESTARTSYS; break; } /* * Check if link is ready now. Even if CLOCAL is * specified, we cannot return before the IrCOMM link is * ready */ if (!(self->flags & ASYNC_CALLOUT_ACTIVE) && !(self->flags & ASYNC_CLOSING) && (do_clocal || (self->settings.dce & IRCOMM_CD)) && self->state == IRCOMM_TTY_READY) { break; } if (signal_pending(current)) { retval = -ERESTARTSYS; break; } IRDA_DEBUG(1, "%s(%d):block_til_ready blocking on %s open_count=%d\n", __FILE__,__LINE__, tty->driver.name, self->open_count ); schedule(); } __set_current_state(TASK_RUNNING); remove_wait_queue(&self->open_wait, &wait); if (extra_count) self->open_count++; self->blocked_open--; IRDA_DEBUG(1, "%s(%d):block_til_ready after blocking on %s open_count=%d\n", __FILE__,__LINE__, tty->driver.name, self->open_count); if (!retval) self->flags |= ASYNC_NORMAL_ACTIVE; return retval; }/* * Function ircomm_tty_open (tty, filp) * * This routine is called when a particular tty device is opened. This * routine is mandatory; if this routine is not filled in, the attempted * open will fail with ENODEV. */static int ircomm_tty_open(struct tty_struct *tty, struct file *filp){ struct ircomm_tty_cb *self; int line; int ret; IRDA_DEBUG(2, __FUNCTION__ "()\n"); MOD_INC_USE_COUNT; line = MINOR(tty->device) - tty->driver.minor_start; if ((line < 0) || (line >= IRCOMM_TTY_PORTS)) { MOD_DEC_USE_COUNT; return -ENODEV; } /* Check if instance already exists */ self = hashbin_find(ircomm_tty, line, NULL); if (!self) { /* No, so make new instance */ self = kmalloc(sizeof(struct ircomm_tty_cb), GFP_KERNEL); if (self == NULL) { ERROR(__FUNCTION__"(), kmalloc failed!\n"); MOD_DEC_USE_COUNT; return -ENOMEM; } memset(self, 0, sizeof(struct ircomm_tty_cb)); self->magic = IRCOMM_TTY_MAGIC; self->flow = FLOW_STOP; self->line = line; self->tqueue.routine = ircomm_tty_do_softint; self->tqueue.data = self; self->max_header_size = 5; self->max_data_size = 64-self->max_header_size; self->close_delay = 5*HZ/10; self->closing_wait = 30*HZ; /* Init some important stuff */ init_timer(&self->watchdog_timer); init_waitqueue_head(&self->open_wait); init_waitqueue_head(&self->close_wait); /* * Force TTY into raw mode by default which is usually what * we want for IrCOMM and IrLPT. This way applications will * not have to twiddle with printcap etc. */ tty->termios->c_iflag = 0; tty->termios->c_oflag = 0; /* Insert into hash */ hashbin_insert(ircomm_tty, (irda_queue_t *) self, line, NULL); } self->open_count++; tty->driver_data = self; self->tty = tty; IRDA_DEBUG(1, __FUNCTION__"(), %s%d, count = %d\n", tty->driver.name, self->line, self->open_count); /* Not really used by us, but lets do it anyway */ self->tty->low_latency = (self->flags & ASYNC_LOW_LATENCY) ? 1 : 0; /* * If the port is the middle of closing, bail out now */ if (tty_hung_up_p(filp) || (self->flags & ASYNC_CLOSING)) { if (self->flags & ASYNC_CLOSING) interruptible_sleep_on(&self->close_wait); /* MOD_DEC_USE_COUNT; "info->tty" will cause this? */#ifdef SERIAL_DO_RESTART return ((self->flags & ASYNC_HUP_NOTIFY) ?
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -