39 #include <linux/module.h>
40 #include <linux/kernel.h>
41 #include <linux/errno.h>
42 #include <linux/string.h>
44 #include <linux/netdevice.h>
46 #include <linux/slab.h>
50 #include <linux/tty.h>
52 #include <linux/serial.h>
55 #include <asm/uaccess.h>
61 #define CPC_TTY_NPORTS 8
62 #define CPC_TTY_MAJOR CYCLADES_MAJOR
63 #define CPC_TTY_MINOR_START 240
65 #define CPC_TTY_MAX_MTU 2000
68 #define CPC_TTY_ST_IDLE 0
69 #define CPC_TTY_ST_INIT 1
70 #define CPC_TTY_ST_OPEN 2
72 #define CPC_TTY_LOCK(card,flags)\
74 spin_lock_irqsave(&card->card_lock, flags); \
77 #define CPC_TTY_UNLOCK(card,flags) \
79 spin_unlock_irqrestore(&card->card_lock, flags); \
83 #define CPC_TTY_DBG(format,a...)
116 static int cpc_tty_cnt = 0;
117 static int cpc_tty_unreg_flag = 0;
122 static int cpc_tty_write(
struct tty_struct *tty,
const unsigned char *
buf,
int count);
123 static int cpc_tty_write_room(
struct tty_struct *tty);
124 static int cpc_tty_chars_in_buffer(
struct tty_struct *tty);
125 static void cpc_tty_flush_buffer(
struct tty_struct *tty);
126 static void cpc_tty_hangup(
struct tty_struct *tty);
134 static int pc300_tiocmset(
struct tty_struct *,
unsigned int,
unsigned int);
135 static int pc300_tiocmget(
struct tty_struct *);
154 pc300dev->
dev->name, signal);
164 static void cpc_tty_signal_on(
pc300dev_t *pc300dev,
unsigned char signal)
172 pc300dev->
dev->name, signal);
181 .open = cpc_tty_open,
182 .close = cpc_tty_close,
183 .write = cpc_tty_write,
184 .write_room = cpc_tty_write_room,
185 .chars_in_buffer = cpc_tty_chars_in_buffer,
186 .tiocmset = pc300_tiocmset,
187 .tiocmget = pc300_tiocmget,
188 .flush_buffer = cpc_tty_flush_buffer,
189 .hangup = cpc_tty_hangup,
210 port = pc300dev->
dev->name[4] -
'0';
212 printk(
"%s-tty: invalid interface selected (0-%i): %li",
218 if (cpc_tty_cnt == 0) {
219 CPC_TTY_DBG(
"%s-tty: driver init, major:%i, minor range:%i=%i\n",
227 serial_drv.driver_name =
"pc300_tty";
228 serial_drv.name =
"ttyCP";
244 printk(
"%s-tty: Failed to register serial driver! ",
245 pc300dev->
dev->name);
249 memset((
void *)cpc_tty_area, 0,
253 cpc_tty = &cpc_tty_area[
port];
256 CPC_TTY_DBG(
"%s-tty: TTY port %i, already in use.\n",
257 pc300dev->
dev->name, port);
272 pc300dev->cpc_tty = (
void *)cpc_tty;
279 cpc_tty_signal_off(pc300dev,
CTL_DTR);
281 CPC_TTY_DBG(
"%s: Initializing TTY Sync Driver, tty major#%d minor#%i\n",
305 CPC_TTY_DBG(
"pc300_tty: open invalid port %d\n", port);
309 cpc_tty = &cpc_tty_area[
port];
312 CPC_TTY_DBG(
"%s: open - invalid interface, port=%d\n",
318 if (!cpc_tty_area[port].buf_tx){
320 if (!cpc_tty_area[port].buf_tx) {
326 if (cpc_tty_area[port].buf_rx.first) {
328 while (cpc_tty_area[port].buf_rx.first) {
329 aux = (
unsigned char *)cpc_tty_area[port].buf_rx.first;
330 cpc_tty_area[port].buf_rx.first = cpc_tty_area[port].buf_rx.first->next;
360 static void cpc_tty_close(
struct tty_struct *tty,
struct file *flip)
395 if (cpc_tty->
buf_rx.first) {
397 while (cpc_tty->
buf_rx.first) {
398 aux = (
unsigned char *)cpc_tty->
buf_rx.first;
411 if (!serial_drv.refcount && cpc_tty_unreg_flag) {
412 cpc_tty_unreg_flag = 0;
415 CPC_TTY_DBG(
"%s: ERROR ->unregister the tty driver error=%d\n",
483 if (cpc_tty_send_to_card(cpc_tty->
pc300dev, (
void*)buf, count)) {
498 static int cpc_tty_write_room(
struct tty_struct *tty)
525 static int cpc_tty_chars_in_buffer(
struct tty_struct *tty)
530 CPC_TTY_DBG(
"hdlcX-tty: no TTY to chars in buffer\n");
544 static int pc300_tiocmset(
struct tty_struct *tty,
545 unsigned int set,
unsigned int clear)
549 CPC_TTY_DBG(
"%s: set:%x clear:%x\n", __func__,
set, clear);
552 CPC_TTY_DBG(
"hdlcX-tty: no TTY to chars in buffer\n");
571 static int pc300_tiocmget(
struct tty_struct *tty)
585 ((
struct net_device*)(pc300dev->hdlc))->name);
592 ((status &
CTL_RTS) ? TIOCM_RTS : 0);
602 static void cpc_tty_flush_buffer(
struct tty_struct *tty)
607 CPC_TTY_DBG(
"hdlcX-tty: no TTY to flush buffer\n");
631 static void cpc_tty_hangup(
struct tty_struct *tty)
647 if (!serial_drv.refcount && cpc_tty_unreg_flag) {
648 cpc_tty_unreg_flag = 0;
651 CPC_TTY_DBG(
"%s: ERROR ->unregister the tty driver error=%d\n",
671 char flags=0,flg_rx=1;
674 if (cpc_tty_cnt == 0)
return;
676 for (i=0; (i < 4) && flg_rx ; i++) {
680 port = cpc_tty - cpc_tty_area;
683 cpc_tty = &cpc_tty_area[
port];
689 if (ld->
ops->receive_buf) {
691 ld->
ops->receive_buf(cpc_tty->
tty, (
char *)(buf->
data), &flags, buf->
size);
698 buf = cpc_tty->
buf_rx.first;
701 if (++port == CPC_TTY_NPORTS) port = 0;
714 static void cpc_tty_rx_disc_frame(
pc300ch_t *pc300chan)
717 volatile unsigned char status;
746 volatile unsigned char status;
749 unsigned char dsr_rx;
751 if (pc300dev->cpc_tty ==
NULL) {
757 cpc_tty = pc300dev->cpc_tty;
765 if (status & DST_EOM) {
786 cpc_tty_rx_disc_frame(pc300chan);
792 cpc_tty_rx_disc_frame(pc300chan);
819 CPC_TTY_DBG(
"%s: reception error - discard descriptors",
821 cpc_tty_rx_disc_frame(pc300chan);
830 cpc_tty_rx_disc_frame(pc300chan);
849 if (status & DST_EOM)
break;
866 cpc_tty_trace(pc300dev, new->data,rx_len,
'R');
871 cpc_tty->
buf_rx.first =
new;
872 cpc_tty->
buf_rx.last =
new;
874 cpc_tty->
buf_rx.last->next =
new;
875 cpc_tty->
buf_rx.last =
new;
890 static void cpc_tty_tx_work(
struct work_struct *work)
898 if ((tty = cpc_tty->
tty) ==
NULL) {
913 static int cpc_tty_send_to_card(
pc300dev_t *
dev,
void* buf,
int len)
936 for (i = 0 ; i < nbuf ; i++) {
943 &pdata[len - tosend],
946 if ((i + 1) == nbuf) {
965 cpc_tty_trace(dev, buf, len,
'T');
996 static void cpc_tty_trace(
pc300dev_t *dev,
char* buf,
int len,
char rxtx)
1000 if ((skb = dev_alloc_skb(10 + len)) ==
NULL) {
1009 skb_reset_mac_header(skb);
1013 skb_copy_to_linear_data(skb, dev->
dev->name, 5);
1019 skb_copy_to_linear_data_offset(skb, 10, buf, len);
1040 if (cpc_tty->
pc300dev != pc300dev) {
1042 pc300dev->
dev->name, cpc_tty->
name);
1046 if (--cpc_tty_cnt == 0) {
1047 if (serial_drv.refcount) {
1048 CPC_TTY_DBG(
"%s: unregister is not possible, refcount=%d",
1049 cpc_tty->
name, serial_drv.refcount);
1051 cpc_tty_unreg_flag = 1;
1056 CPC_TTY_DBG(
"%s: ERROR ->unregister the tty driver error=%d\n",