| Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 1 | /* | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 2 |  * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 |  * Licensed under the GPL | 
 | 4 |  */ | 
 | 5 |  | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 6 | #include "linux/irqreturn.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include "linux/kd.h" | 
| Alexey Dobriyan | d43c36d | 2009-10-07 17:09:06 +0400 | [diff] [blame] | 8 | #include "linux/sched.h" | 
| Jan Kiszka | 7f3c1fa | 2010-04-19 17:46:23 +0900 | [diff] [blame] | 9 | #include "linux/slab.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include "chan_kern.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include "irq_kern.h" | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 12 | #include "irq_user.h" | 
| Jeff Dike | edea138 | 2008-02-04 22:30:46 -0800 | [diff] [blame] | 13 | #include "kern_util.h" | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 14 | #include "os.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 |  | 
 | 16 | #define LINE_BUFSIZE 4096 | 
 | 17 |  | 
| Al Viro | 7bea96f | 2006-10-08 22:49:34 +0100 | [diff] [blame] | 18 | static irqreturn_t line_interrupt(int irq, void *data) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | { | 
| Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 20 | 	struct chan *chan = data; | 
 | 21 | 	struct line *line = chan->line; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 |  | 
 | 23 | 	if (line) | 
| Alexander Beregalov | 03315b5 | 2010-03-05 13:42:33 -0800 | [diff] [blame] | 24 | 		chan_interrupt(&line->chan_list, &line->task, line->tty, irq); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | 	return IRQ_HANDLED; | 
 | 26 | } | 
 | 27 |  | 
| Andrew Morton | a2ce774 | 2006-12-06 20:31:36 -0800 | [diff] [blame] | 28 | static void line_timer_cb(struct work_struct *work) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | { | 
| Andrew Morton | a2ce774 | 2006-12-06 20:31:36 -0800 | [diff] [blame] | 30 | 	struct line *line = container_of(work, struct line, task.work); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 |  | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 32 | 	if (!line->throttled) | 
| Jeff Dike | e4dcee8 | 2006-01-06 00:18:58 -0800 | [diff] [blame] | 33 | 		chan_interrupt(&line->chan_list, &line->task, line->tty, | 
 | 34 | 			       line->driver->read_irq); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | } | 
 | 36 |  | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 37 | /* | 
 | 38 |  * Returns the free space inside the ring buffer of this line. | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 39 |  * | 
| Simon Arlott | b60745b | 2007-10-20 01:23:03 +0200 | [diff] [blame] | 40 |  * Should be called while holding line->lock (this does not modify data). | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 41 |  */ | 
 | 42 | static int write_room(struct line *line) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | { | 
 | 44 | 	int n; | 
 | 45 |  | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 46 | 	if (line->buffer == NULL) | 
 | 47 | 		return LINE_BUFSIZE - 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 |  | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 49 | 	/* This is for the case where the buffer is wrapped! */ | 
 | 50 | 	n = line->head - line->tail; | 
 | 51 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | 	if (n <= 0) | 
| Jeff Dike | acb2cf3 | 2008-02-04 22:30:42 -0800 | [diff] [blame] | 53 | 		n += LINE_BUFSIZE; /* The other case */ | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 54 | 	return n - 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | } | 
 | 56 |  | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 57 | int line_write_room(struct tty_struct *tty) | 
 | 58 | { | 
 | 59 | 	struct line *line = tty->driver_data; | 
 | 60 | 	unsigned long flags; | 
 | 61 | 	int room; | 
 | 62 |  | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 63 | 	spin_lock_irqsave(&line->lock, flags); | 
 | 64 | 	room = write_room(line); | 
 | 65 | 	spin_unlock_irqrestore(&line->lock, flags); | 
 | 66 |  | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 67 | 	return room; | 
 | 68 | } | 
 | 69 |  | 
 | 70 | int line_chars_in_buffer(struct tty_struct *tty) | 
 | 71 | { | 
 | 72 | 	struct line *line = tty->driver_data; | 
 | 73 | 	unsigned long flags; | 
 | 74 | 	int ret; | 
 | 75 |  | 
 | 76 | 	spin_lock_irqsave(&line->lock, flags); | 
| Jeff Dike | acb2cf3 | 2008-02-04 22:30:42 -0800 | [diff] [blame] | 77 | 	/* write_room subtracts 1 for the needed NULL, so we readd it.*/ | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 78 | 	ret = LINE_BUFSIZE - (write_room(line) + 1); | 
 | 79 | 	spin_unlock_irqrestore(&line->lock, flags); | 
 | 80 |  | 
 | 81 | 	return ret; | 
 | 82 | } | 
 | 83 |  | 
 | 84 | /* | 
 | 85 |  * This copies the content of buf into the circular buffer associated with | 
 | 86 |  * this line. | 
 | 87 |  * The return value is the number of characters actually copied, i.e. the ones | 
 | 88 |  * for which there was space: this function is not supposed to ever flush out | 
 | 89 |  * the circular buffer. | 
 | 90 |  * | 
 | 91 |  * Must be called while holding line->lock! | 
 | 92 |  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | static int buffer_data(struct line *line, const char *buf, int len) | 
 | 94 | { | 
 | 95 | 	int end, room; | 
 | 96 |  | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 97 | 	if (line->buffer == NULL) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | 		line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC); | 
 | 99 | 		if (line->buffer == NULL) { | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 100 | 			printk(KERN_ERR "buffer_data - atomic allocation " | 
 | 101 | 			       "failed\n"); | 
 | 102 | 			return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | 		} | 
 | 104 | 		line->head = line->buffer; | 
 | 105 | 		line->tail = line->buffer; | 
 | 106 | 	} | 
 | 107 |  | 
 | 108 | 	room = write_room(line); | 
 | 109 | 	len = (len > room) ? room : len; | 
 | 110 |  | 
 | 111 | 	end = line->buffer + LINE_BUFSIZE - line->tail; | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 112 |  | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 113 | 	if (len < end) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | 		memcpy(line->tail, buf, len); | 
 | 115 | 		line->tail += len; | 
| Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 116 | 	} | 
 | 117 | 	else { | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 118 | 		/* The circular buffer is wrapping */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | 		memcpy(line->tail, buf, end); | 
 | 120 | 		buf += end; | 
 | 121 | 		memcpy(line->buffer, buf, len - end); | 
 | 122 | 		line->tail = line->buffer + len - end; | 
 | 123 | 	} | 
 | 124 |  | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 125 | 	return len; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } | 
 | 127 |  | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 128 | /* | 
 | 129 |  * Flushes the ring buffer to the output channels. That is, write_chan is | 
 | 130 |  * called, passing it line->head as buffer, and an appropriate count. | 
 | 131 |  * | 
 | 132 |  * On exit, returns 1 when the buffer is empty, | 
 | 133 |  * 0 when the buffer is not empty on exit, | 
 | 134 |  * and -errno when an error occurred. | 
 | 135 |  * | 
 | 136 |  * Must be called while holding line->lock!*/ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | static int flush_buffer(struct line *line) | 
 | 138 | { | 
 | 139 | 	int n, count; | 
 | 140 |  | 
 | 141 | 	if ((line->buffer == NULL) || (line->head == line->tail)) | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 142 | 		return 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 |  | 
 | 144 | 	if (line->tail < line->head) { | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 145 | 		/* line->buffer + LINE_BUFSIZE is the end of the buffer! */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | 		count = line->buffer + LINE_BUFSIZE - line->head; | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 147 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | 		n = write_chan(&line->chan_list, line->head, count, | 
 | 149 | 			       line->driver->write_irq); | 
 | 150 | 		if (n < 0) | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 151 | 			return n; | 
 | 152 | 		if (n == count) { | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 153 | 			/* | 
 | 154 | 			 * We have flushed from ->head to buffer end, now we | 
 | 155 | 			 * must flush only from the beginning to ->tail. | 
 | 156 | 			 */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | 			line->head = line->buffer; | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 158 | 		} else { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | 			line->head += n; | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 160 | 			return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | 		} | 
 | 162 | 	} | 
 | 163 |  | 
 | 164 | 	count = line->tail - line->head; | 
| Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 165 | 	n = write_chan(&line->chan_list, line->head, count, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | 		       line->driver->write_irq); | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 167 |  | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 168 | 	if (n < 0) | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 169 | 		return n; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 |  | 
 | 171 | 	line->head += n; | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 172 | 	return line->head == line->tail; | 
 | 173 | } | 
 | 174 |  | 
 | 175 | void line_flush_buffer(struct tty_struct *tty) | 
 | 176 | { | 
 | 177 | 	struct line *line = tty->driver_data; | 
 | 178 | 	unsigned long flags; | 
 | 179 | 	int err; | 
 | 180 |  | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 181 | 	spin_lock_irqsave(&line->lock, flags); | 
 | 182 | 	err = flush_buffer(line); | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 183 | 	spin_unlock_irqrestore(&line->lock, flags); | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 184 | } | 
 | 185 |  | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 186 | /* | 
 | 187 |  * We map both ->flush_chars and ->put_char (which go in pair) onto | 
 | 188 |  * ->flush_buffer and ->write. Hope it's not that bad. | 
 | 189 |  */ | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 190 | void line_flush_chars(struct tty_struct *tty) | 
 | 191 | { | 
 | 192 | 	line_flush_buffer(tty); | 
 | 193 | } | 
 | 194 |  | 
| WANG Cong | 3168cb9 | 2008-05-06 20:42:33 -0700 | [diff] [blame] | 195 | int line_put_char(struct tty_struct *tty, unsigned char ch) | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 196 | { | 
| WANG Cong | 3168cb9 | 2008-05-06 20:42:33 -0700 | [diff] [blame] | 197 | 	return line_write(tty, &ch, sizeof(ch)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } | 
 | 199 |  | 
 | 200 | int line_write(struct tty_struct *tty, const unsigned char *buf, int len) | 
 | 201 | { | 
 | 202 | 	struct line *line = tty->driver_data; | 
 | 203 | 	unsigned long flags; | 
| Jeff Dike | c59dbca | 2007-10-16 01:26:42 -0700 | [diff] [blame] | 204 | 	int n, ret = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 |  | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 206 | 	spin_lock_irqsave(&line->lock, flags); | 
| Jeff Dike | c59dbca | 2007-10-16 01:26:42 -0700 | [diff] [blame] | 207 | 	if (line->head != line->tail) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | 		ret = buffer_data(line, buf, len); | 
| Jeff Dike | c59dbca | 2007-10-16 01:26:42 -0700 | [diff] [blame] | 209 | 	else { | 
| Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 210 | 		n = write_chan(&line->chan_list, buf, len, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | 			       line->driver->write_irq); | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 212 | 		if (n < 0) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | 			ret = n; | 
 | 214 | 			goto out_up; | 
 | 215 | 		} | 
 | 216 |  | 
 | 217 | 		len -= n; | 
 | 218 | 		ret += n; | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 219 | 		if (len > 0) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | 			ret += buffer_data(line, buf + n, len); | 
 | 221 | 	} | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 222 | out_up: | 
 | 223 | 	spin_unlock_irqrestore(&line->lock, flags); | 
 | 224 | 	return ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | } | 
 | 226 |  | 
| Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 227 | void line_set_termios(struct tty_struct *tty, struct ktermios * old) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | { | 
 | 229 | 	/* nothing */ | 
 | 230 | } | 
 | 231 |  | 
| Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 232 | static const struct { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | 	int  cmd; | 
 | 234 | 	char *level; | 
 | 235 | 	char *name; | 
 | 236 | } tty_ioctls[] = { | 
 | 237 | 	/* don't print these, they flood the log ... */ | 
 | 238 | 	{ TCGETS,      NULL,       "TCGETS"      }, | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 239 | 	{ TCSETS,      NULL,       "TCSETS"      }, | 
 | 240 | 	{ TCSETSW,     NULL,       "TCSETSW"     }, | 
 | 241 | 	{ TCFLSH,      NULL,       "TCFLSH"      }, | 
 | 242 | 	{ TCSBRK,      NULL,       "TCSBRK"      }, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 |  | 
 | 244 | 	/* general tty stuff */ | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 245 | 	{ TCSETSF,     KERN_DEBUG, "TCSETSF"     }, | 
 | 246 | 	{ TCGETA,      KERN_DEBUG, "TCGETA"      }, | 
 | 247 | 	{ TIOCMGET,    KERN_DEBUG, "TIOCMGET"    }, | 
 | 248 | 	{ TCSBRKP,     KERN_DEBUG, "TCSBRKP"     }, | 
 | 249 | 	{ TIOCMSET,    KERN_DEBUG, "TIOCMSET"    }, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 |  | 
 | 251 | 	/* linux-specific ones */ | 
 | 252 | 	{ TIOCLINUX,   KERN_INFO,  "TIOCLINUX"   }, | 
 | 253 | 	{ KDGKBMODE,   KERN_INFO,  "KDGKBMODE"   }, | 
 | 254 | 	{ KDGKBTYPE,   KERN_INFO,  "KDGKBTYPE"   }, | 
 | 255 | 	{ KDSIGACCEPT, KERN_INFO,  "KDSIGACCEPT" }, | 
 | 256 | }; | 
 | 257 |  | 
| Richard Weinberger | 8a06dc4d | 2011-03-22 16:33:48 -0700 | [diff] [blame] | 258 | int line_ioctl(struct tty_struct *tty, unsigned int cmd, | 
 | 259 | 				unsigned long arg) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | { | 
 | 261 | 	int ret; | 
 | 262 | 	int i; | 
 | 263 |  | 
 | 264 | 	ret = 0; | 
 | 265 | 	switch(cmd) { | 
 | 266 | #ifdef TIOCGETP | 
 | 267 | 	case TIOCGETP: | 
 | 268 | 	case TIOCSETP: | 
 | 269 | 	case TIOCSETN: | 
 | 270 | #endif | 
 | 271 | #ifdef TIOCGETC | 
 | 272 | 	case TIOCGETC: | 
 | 273 | 	case TIOCSETC: | 
 | 274 | #endif | 
 | 275 | #ifdef TIOCGLTC | 
 | 276 | 	case TIOCGLTC: | 
 | 277 | 	case TIOCSLTC: | 
 | 278 | #endif | 
| Alan Cox | c564b6f | 2008-10-13 10:36:49 +0100 | [diff] [blame] | 279 | 	/* Note: these are out of date as we now have TCGETS2 etc but this | 
 | 280 | 	   whole lot should probably go away */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | 	case TCGETS: | 
 | 282 | 	case TCSETSF: | 
 | 283 | 	case TCSETSW: | 
 | 284 | 	case TCSETS: | 
 | 285 | 	case TCGETA: | 
 | 286 | 	case TCSETAF: | 
 | 287 | 	case TCSETAW: | 
 | 288 | 	case TCSETA: | 
 | 289 | 	case TCXONC: | 
 | 290 | 	case TCFLSH: | 
 | 291 | 	case TIOCOUTQ: | 
 | 292 | 	case TIOCINQ: | 
 | 293 | 	case TIOCGLCKTRMIOS: | 
 | 294 | 	case TIOCSLCKTRMIOS: | 
 | 295 | 	case TIOCPKT: | 
 | 296 | 	case TIOCGSOFTCAR: | 
 | 297 | 	case TIOCSSOFTCAR: | 
 | 298 | 		return -ENOIOCTLCMD; | 
 | 299 | #if 0 | 
 | 300 | 	case TCwhatever: | 
 | 301 | 		/* do something */ | 
 | 302 | 		break; | 
 | 303 | #endif | 
 | 304 | 	default: | 
 | 305 | 		for (i = 0; i < ARRAY_SIZE(tty_ioctls); i++) | 
 | 306 | 			if (cmd == tty_ioctls[i].cmd) | 
 | 307 | 				break; | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 308 | 		if (i == ARRAY_SIZE(tty_ioctls)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | 			printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n", | 
| Harvey Harrison | 3595726 | 2008-04-28 02:13:53 -0700 | [diff] [blame] | 310 | 			       __func__, tty->name, cmd); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | 		} | 
 | 312 | 		ret = -ENOIOCTLCMD; | 
 | 313 | 		break; | 
 | 314 | 	} | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 315 | 	return ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | } | 
 | 317 |  | 
| Jeff Dike | e4dcee8 | 2006-01-06 00:18:58 -0800 | [diff] [blame] | 318 | void line_throttle(struct tty_struct *tty) | 
 | 319 | { | 
 | 320 | 	struct line *line = tty->driver_data; | 
 | 321 |  | 
 | 322 | 	deactivate_chan(&line->chan_list, line->driver->read_irq); | 
 | 323 | 	line->throttled = 1; | 
 | 324 | } | 
 | 325 |  | 
 | 326 | void line_unthrottle(struct tty_struct *tty) | 
 | 327 | { | 
 | 328 | 	struct line *line = tty->driver_data; | 
 | 329 |  | 
 | 330 | 	line->throttled = 0; | 
 | 331 | 	chan_interrupt(&line->chan_list, &line->task, tty, | 
 | 332 | 		       line->driver->read_irq); | 
 | 333 |  | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 334 | 	/* | 
 | 335 | 	 * Maybe there is enough stuff pending that calling the interrupt | 
| Jeff Dike | e4dcee8 | 2006-01-06 00:18:58 -0800 | [diff] [blame] | 336 | 	 * throttles us again.  In this case, line->throttled will be 1 | 
 | 337 | 	 * again and we shouldn't turn the interrupt back on. | 
 | 338 | 	 */ | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 339 | 	if (!line->throttled) | 
| Jeff Dike | e4dcee8 | 2006-01-06 00:18:58 -0800 | [diff] [blame] | 340 | 		reactivate_chan(&line->chan_list, line->driver->read_irq); | 
 | 341 | } | 
 | 342 |  | 
| Al Viro | 7bea96f | 2006-10-08 22:49:34 +0100 | [diff] [blame] | 343 | static irqreturn_t line_write_interrupt(int irq, void *data) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | { | 
| Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 345 | 	struct chan *chan = data; | 
 | 346 | 	struct line *line = chan->line; | 
 | 347 | 	struct tty_struct *tty = line->tty; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | 	int err; | 
 | 349 |  | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 350 | 	/* | 
 | 351 | 	 * Interrupts are disabled here because we registered the interrupt with | 
 | 352 | 	 * IRQF_DISABLED (see line_setup_irq). | 
 | 353 | 	 */ | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 354 |  | 
| Paolo 'Blaisorblade' Giarrusso | ec0ac8a | 2007-03-07 20:41:12 -0800 | [diff] [blame] | 355 | 	spin_lock(&line->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | 	err = flush_buffer(line); | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 357 | 	if (err == 0) { | 
 | 358 | 		return IRQ_NONE; | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 359 | 	} else if (err < 0) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | 		line->head = line->buffer; | 
 | 361 | 		line->tail = line->buffer; | 
 | 362 | 	} | 
| Paolo 'Blaisorblade' Giarrusso | ec0ac8a | 2007-03-07 20:41:12 -0800 | [diff] [blame] | 363 | 	spin_unlock(&line->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 |  | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 365 | 	if (tty == NULL) | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 366 | 		return IRQ_NONE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 |  | 
| WANG Cong | 06ac667 | 2008-07-29 22:33:34 -0700 | [diff] [blame] | 368 | 	tty_wakeup(tty); | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 369 | 	return IRQ_HANDLED; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | } | 
 | 371 |  | 
| Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 372 | int line_setup_irq(int fd, int input, int output, struct line *line, void *data) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | { | 
| Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 374 | 	const struct line_driver *driver = line->driver; | 
| Thomas Gleixner | bd6aa65 | 2006-07-01 19:29:27 -0700 | [diff] [blame] | 375 | 	int err = 0, flags = IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 |  | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 377 | 	if (input) | 
 | 378 | 		err = um_request_irq(driver->read_irq, fd, IRQ_READ, | 
| Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 379 | 				       line_interrupt, flags, | 
| Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 380 | 				       driver->read_irq_name, data); | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 381 | 	if (err) | 
 | 382 | 		return err; | 
 | 383 | 	if (output) | 
 | 384 | 		err = um_request_irq(driver->write_irq, fd, IRQ_WRITE, | 
| Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 385 | 					line_write_interrupt, flags, | 
| Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 386 | 					driver->write_irq_name, data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | 	line->have_irq = 1; | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 388 | 	return err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | } | 
 | 390 |  | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 391 | /* | 
 | 392 |  * Normally, a driver like this can rely mostly on the tty layer | 
| Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 393 |  * locking, particularly when it comes to the driver structure. | 
 | 394 |  * However, in this case, mconsole requests can come in "from the | 
 | 395 |  * side", and race with opens and closes. | 
 | 396 |  * | 
| Jeff Dike | c6256c6 | 2007-02-10 01:44:08 -0800 | [diff] [blame] | 397 |  * mconsole config requests will want to be sure the device isn't in | 
 | 398 |  * use, and get_config, open, and close will want a stable | 
 | 399 |  * configuration.  The checking and modification of the configuration | 
 | 400 |  * is done under a spinlock.  Checking whether the device is in use is | 
 | 401 |  * line->tty->count > 1, also under the spinlock. | 
| Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 402 |  * | 
| Jeff Dike | c6256c6 | 2007-02-10 01:44:08 -0800 | [diff] [blame] | 403 |  * tty->count serves to decide whether the device should be enabled or | 
 | 404 |  * disabled on the host.  If it's equal to 1, then we are doing the | 
 | 405 |  * first open or last close.  Otherwise, open and close just return. | 
| Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 406 |  */ | 
 | 407 |  | 
| Jeff Dike | 1f80171 | 2006-01-06 00:18:55 -0800 | [diff] [blame] | 408 | int line_open(struct line *lines, struct tty_struct *tty) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | { | 
| Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 410 | 	struct line *line = &lines[tty->index]; | 
| Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 411 | 	int err = -ENODEV; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 |  | 
| Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 413 | 	spin_lock(&line->count_lock); | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 414 | 	if (!line->valid) | 
| Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 415 | 		goto out_unlock; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 |  | 
| Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 417 | 	err = 0; | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 418 | 	if (tty->count > 1) | 
| Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 419 | 		goto out_unlock; | 
 | 420 |  | 
| Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 421 | 	spin_unlock(&line->count_lock); | 
| Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 422 |  | 
 | 423 | 	tty->driver_data = line; | 
 | 424 | 	line->tty = tty; | 
| Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 425 |  | 
| Jeff Dike | d14ad81 | 2007-07-15 23:38:54 -0700 | [diff] [blame] | 426 | 	err = enable_chan(line); | 
 | 427 | 	if (err) | 
 | 428 | 		return err; | 
 | 429 |  | 
| Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 430 | 	INIT_DELAYED_WORK(&line->task, line_timer_cb); | 
| Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 431 |  | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 432 | 	if (!line->sigio) { | 
| Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 433 | 		chan_enable_winch(&line->chan_list, tty); | 
 | 434 | 		line->sigio = 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | 	} | 
 | 436 |  | 
| Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 437 | 	chan_window_size(&line->chan_list, &tty->winsize.ws_row, | 
 | 438 | 			 &tty->winsize.ws_col); | 
 | 439 |  | 
| Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 440 | 	return err; | 
 | 441 |  | 
 | 442 | out_unlock: | 
 | 443 | 	spin_unlock(&line->count_lock); | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 444 | 	return err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | } | 
 | 446 |  | 
| Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 447 | static void unregister_winch(struct tty_struct *tty); | 
 | 448 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | void line_close(struct tty_struct *tty, struct file * filp) | 
 | 450 | { | 
 | 451 | 	struct line *line = tty->driver_data; | 
 | 452 |  | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 453 | 	/* | 
 | 454 | 	 * If line_open fails (and tty->driver_data is never set), | 
| Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 455 | 	 * tty_open will call line_close.  So just return in this case. | 
 | 456 | 	 */ | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 457 | 	if (line == NULL) | 
| Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 458 | 		return; | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 459 |  | 
 | 460 | 	/* We ignore the error anyway! */ | 
 | 461 | 	flush_buffer(line); | 
 | 462 |  | 
| Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 463 | 	spin_lock(&line->count_lock); | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 464 | 	if (!line->valid) | 
| Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 465 | 		goto out_unlock; | 
| Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 466 |  | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 467 | 	if (tty->count > 1) | 
| Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 468 | 		goto out_unlock; | 
 | 469 |  | 
| Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 470 | 	spin_unlock(&line->count_lock); | 
 | 471 |  | 
 | 472 | 	line->tty = NULL; | 
 | 473 | 	tty->driver_data = NULL; | 
 | 474 |  | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 475 | 	if (line->sigio) { | 
| Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 476 | 		unregister_winch(tty); | 
 | 477 | 		line->sigio = 0; | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 478 | 	} | 
| Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 479 |  | 
| Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 480 | 	return; | 
 | 481 |  | 
 | 482 | out_unlock: | 
 | 483 | 	spin_unlock(&line->count_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | } | 
 | 485 |  | 
 | 486 | void close_lines(struct line *lines, int nlines) | 
 | 487 | { | 
 | 488 | 	int i; | 
 | 489 |  | 
 | 490 | 	for(i = 0; i < nlines; i++) | 
| Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 491 | 		close_chan(&lines[i].chan_list, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | } | 
 | 493 |  | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 494 | static int setup_one_line(struct line *lines, int n, char *init, int init_prio, | 
 | 495 | 			  char **error_out) | 
| Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 496 | { | 
 | 497 | 	struct line *line = &lines[n]; | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 498 | 	int err = -EINVAL; | 
| Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 499 |  | 
 | 500 | 	spin_lock(&line->count_lock); | 
 | 501 |  | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 502 | 	if (line->tty != NULL) { | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 503 | 		*error_out = "Device is already open"; | 
| Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 504 | 		goto out; | 
 | 505 | 	} | 
 | 506 |  | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 507 | 	if (line->init_pri <= init_prio) { | 
| Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 508 | 		line->init_pri = init_prio; | 
 | 509 | 		if (!strcmp(init, "none")) | 
 | 510 | 			line->valid = 0; | 
 | 511 | 		else { | 
 | 512 | 			line->init_str = init; | 
 | 513 | 			line->valid = 1; | 
 | 514 | 		} | 
 | 515 | 	} | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 516 | 	err = 0; | 
| Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 517 | out: | 
 | 518 | 	spin_unlock(&line->count_lock); | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 519 | 	return err; | 
| Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 520 | } | 
 | 521 |  | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 522 | /* | 
 | 523 |  * Common setup code for both startup command line and mconsole initialization. | 
| Matt LaPlante | 4b3f686 | 2006-10-03 22:21:02 +0200 | [diff] [blame] | 524 |  * @lines contains the array (of size @num) to modify; | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 525 |  * @init is the setup string; | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 526 |  * @error_out is an error string in the case of failure; | 
| Jeff Dike | d571cd1 | 2006-01-06 00:18:53 -0800 | [diff] [blame] | 527 |  */ | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 528 |  | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 529 | int line_setup(struct line *lines, unsigned int num, char *init, | 
 | 530 | 	       char **error_out) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | { | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 532 | 	int i, n, err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | 	char *end; | 
 | 534 |  | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 535 | 	if (*init == '=') { | 
 | 536 | 		/* | 
 | 537 | 		 * We said con=/ssl= instead of con#=, so we are configuring all | 
 | 538 | 		 * consoles at once. | 
 | 539 | 		 */ | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 540 | 		n = -1; | 
| Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 541 | 	} | 
 | 542 | 	else { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | 		n = simple_strtoul(init, &end, 0); | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 544 | 		if (*end != '=') { | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 545 | 			*error_out = "Couldn't parse device number"; | 
 | 546 | 			return -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | 		} | 
 | 548 | 		init = end; | 
 | 549 | 	} | 
 | 550 | 	init++; | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 551 |  | 
 | 552 | 	if (n >= (signed int) num) { | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 553 | 		*error_out = "Device number out of range"; | 
 | 554 | 		return -EINVAL; | 
| Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 555 | 	} | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 556 | 	else if (n >= 0) { | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 557 | 		err = setup_one_line(lines, n, init, INIT_ONE, error_out); | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 558 | 		if (err) | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 559 | 			return err; | 
 | 560 | 	} | 
| Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 561 | 	else { | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 562 | 		for(i = 0; i < num; i++) { | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 563 | 			err = setup_one_line(lines, i, init, INIT_ALL, | 
 | 564 | 					     error_out); | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 565 | 			if (err) | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 566 | 				return err; | 
 | 567 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | 	} | 
| Jeff Dike | 418e55d | 2006-01-06 00:18:54 -0800 | [diff] [blame] | 569 | 	return n == -1 ? num : n; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | } | 
 | 571 |  | 
| Jeff Dike | 1f80171 | 2006-01-06 00:18:55 -0800 | [diff] [blame] | 572 | int line_config(struct line *lines, unsigned int num, char *str, | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 573 | 		const struct chan_opts *opts, char **error_out) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | { | 
| Jeff Dike | 1f80171 | 2006-01-06 00:18:55 -0800 | [diff] [blame] | 575 | 	struct line *line; | 
| Jeff Dike | 970d6e3 | 2006-01-06 00:18:48 -0800 | [diff] [blame] | 576 | 	char *new; | 
| Jeff Dike | 418e55d | 2006-01-06 00:18:54 -0800 | [diff] [blame] | 577 | 	int n; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 |  | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 579 | 	if (*str == '=') { | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 580 | 		*error_out = "Can't configure all devices from mconsole"; | 
 | 581 | 		return -EINVAL; | 
| Jeff Dike | d571cd1 | 2006-01-06 00:18:53 -0800 | [diff] [blame] | 582 | 	} | 
 | 583 |  | 
| Jeff Dike | 970d6e3 | 2006-01-06 00:18:48 -0800 | [diff] [blame] | 584 | 	new = kstrdup(str, GFP_KERNEL); | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 585 | 	if (new == NULL) { | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 586 | 		*error_out = "Failed to allocate memory"; | 
 | 587 | 		return -ENOMEM; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | 	} | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 589 | 	n = line_setup(lines, num, new, error_out); | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 590 | 	if (n < 0) | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 591 | 		return n; | 
| Jeff Dike | 1f80171 | 2006-01-06 00:18:55 -0800 | [diff] [blame] | 592 |  | 
 | 593 | 	line = &lines[n]; | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 594 | 	return parse_chan_pair(line->init_str, line, n, opts, error_out); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | } | 
 | 596 |  | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 597 | int line_get_config(char *name, struct line *lines, unsigned int num, char *str, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | 		    int size, char **error_out) | 
 | 599 | { | 
 | 600 | 	struct line *line; | 
 | 601 | 	char *end; | 
 | 602 | 	int dev, n = 0; | 
 | 603 |  | 
 | 604 | 	dev = simple_strtoul(name, &end, 0); | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 605 | 	if ((*end != '\0') || (end == name)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | 		*error_out = "line_get_config failed to parse device number"; | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 607 | 		return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | 	} | 
 | 609 |  | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 610 | 	if ((dev < 0) || (dev >= num)) { | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 611 | 		*error_out = "device number out of range"; | 
 | 612 | 		return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | 	} | 
 | 614 |  | 
 | 615 | 	line = &lines[dev]; | 
 | 616 |  | 
| Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 617 | 	spin_lock(&line->count_lock); | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 618 | 	if (!line->valid) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | 		CONFIG_CHUNK(str, size, n, "none", 1); | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 620 | 	else if (line->tty == NULL) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | 		CONFIG_CHUNK(str, size, n, line->init_str, 1); | 
 | 622 | 	else n = chan_config_string(&line->chan_list, str, size, error_out); | 
| Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 623 | 	spin_unlock(&line->count_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 |  | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 625 | 	return n; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | } | 
 | 627 |  | 
| Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 628 | int line_id(char **str, int *start_out, int *end_out) | 
 | 629 | { | 
 | 630 | 	char *end; | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 631 | 	int n; | 
| Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 632 |  | 
 | 633 | 	n = simple_strtoul(*str, &end, 0); | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 634 | 	if ((*end != '\0') || (end == *str)) | 
 | 635 | 		return -1; | 
| Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 636 |  | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 637 | 	*str = end; | 
 | 638 | 	*start_out = n; | 
 | 639 | 	*end_out = n; | 
 | 640 | 	return n; | 
| Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 641 | } | 
 | 642 |  | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 643 | int line_remove(struct line *lines, unsigned int num, int n, char **error_out) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | { | 
| Jeff Dike | 418e55d | 2006-01-06 00:18:54 -0800 | [diff] [blame] | 645 | 	int err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | 	char config[sizeof("conxxxx=none\0")]; | 
 | 647 |  | 
| Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 648 | 	sprintf(config, "%d=none", n); | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 649 | 	err = line_setup(lines, num, config, error_out); | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 650 | 	if (err >= 0) | 
| Jeff Dike | 418e55d | 2006-01-06 00:18:54 -0800 | [diff] [blame] | 651 | 		err = 0; | 
 | 652 | 	return err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | } | 
 | 654 |  | 
| Jeff Dike | d5c9ffc | 2007-02-10 01:44:08 -0800 | [diff] [blame] | 655 | struct tty_driver *register_lines(struct line_driver *line_driver, | 
 | 656 | 				  const struct tty_operations *ops, | 
 | 657 | 				  struct line *lines, int nlines) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | { | 
 | 659 | 	int i; | 
 | 660 | 	struct tty_driver *driver = alloc_tty_driver(nlines); | 
 | 661 |  | 
 | 662 | 	if (!driver) | 
 | 663 | 		return NULL; | 
 | 664 |  | 
 | 665 | 	driver->driver_name = line_driver->name; | 
 | 666 | 	driver->name = line_driver->device_name; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | 	driver->major = line_driver->major; | 
 | 668 | 	driver->minor_start = line_driver->minor_start; | 
 | 669 | 	driver->type = line_driver->type; | 
 | 670 | 	driver->subtype = line_driver->subtype; | 
 | 671 | 	driver->flags = TTY_DRIVER_REAL_RAW; | 
 | 672 | 	driver->init_termios = tty_std_termios; | 
 | 673 | 	tty_set_operations(driver, ops); | 
 | 674 |  | 
 | 675 | 	if (tty_register_driver(driver)) { | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 676 | 		printk(KERN_ERR "register_lines : can't register %s driver\n", | 
 | 677 | 		       line_driver->name); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | 		put_tty_driver(driver); | 
 | 679 | 		return NULL; | 
 | 680 | 	} | 
 | 681 |  | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 682 | 	for(i = 0; i < nlines; i++) { | 
 | 683 | 		if (!lines[i].valid) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | 			tty_unregister_device(driver, i); | 
 | 685 | 	} | 
 | 686 |  | 
 | 687 | 	mconsole_register_dev(&line_driver->mc); | 
 | 688 | 	return driver; | 
 | 689 | } | 
 | 690 |  | 
| Jeff Dike | 9010772 | 2006-01-06 00:18:54 -0800 | [diff] [blame] | 691 | static DEFINE_SPINLOCK(winch_handler_lock); | 
 | 692 | static LIST_HEAD(winch_handlers); | 
| Paolo 'Blaisorblade' Giarrusso | 605a69a | 2005-07-07 17:56:52 -0700 | [diff] [blame] | 693 |  | 
| Jeff Dike | 1f80171 | 2006-01-06 00:18:55 -0800 | [diff] [blame] | 694 | void lines_init(struct line *lines, int nlines, struct chan_opts *opts) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | { | 
 | 696 | 	struct line *line; | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 697 | 	char *error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | 	int i; | 
 | 699 |  | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 700 | 	for(i = 0; i < nlines; i++) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | 		line = &lines[i]; | 
 | 702 | 		INIT_LIST_HEAD(&line->chan_list); | 
| Jeff Dike | 9010772 | 2006-01-06 00:18:54 -0800 | [diff] [blame] | 703 |  | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 704 | 		if (line->init_str == NULL) | 
| Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 705 | 			continue; | 
 | 706 |  | 
 | 707 | 		line->init_str = kstrdup(line->init_str, GFP_KERNEL); | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 708 | 		if (line->init_str == NULL) | 
 | 709 | 			printk(KERN_ERR "lines_init - kstrdup returned NULL\n"); | 
| Jeff Dike | 1f80171 | 2006-01-06 00:18:55 -0800 | [diff] [blame] | 710 |  | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 711 | 		if (parse_chan_pair(line->init_str, line, i, opts, &error)) { | 
 | 712 | 			printk(KERN_ERR "parse_chan_pair failed for " | 
 | 713 | 			       "device %d : %s\n", i, error); | 
| Jeff Dike | 1f80171 | 2006-01-06 00:18:55 -0800 | [diff] [blame] | 714 | 			line->valid = 0; | 
 | 715 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | 	} | 
 | 717 | } | 
 | 718 |  | 
 | 719 | struct winch { | 
 | 720 | 	struct list_head list; | 
 | 721 | 	int fd; | 
 | 722 | 	int tty_fd; | 
 | 723 | 	int pid; | 
 | 724 | 	struct tty_struct *tty; | 
| Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 725 | 	unsigned long stack; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | }; | 
 | 727 |  | 
| Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 728 | static void free_winch(struct winch *winch, int free_irq_ok) | 
 | 729 | { | 
| Will Newton | 69e83da | 2010-11-24 12:56:55 -0800 | [diff] [blame] | 730 | 	if (free_irq_ok) | 
 | 731 | 		free_irq(WINCH_IRQ, winch); | 
 | 732 |  | 
| Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 733 | 	list_del(&winch->list); | 
 | 734 |  | 
 | 735 | 	if (winch->pid != -1) | 
 | 736 | 		os_kill_process(winch->pid, 1); | 
 | 737 | 	if (winch->fd != -1) | 
 | 738 | 		os_close_file(winch->fd); | 
 | 739 | 	if (winch->stack != 0) | 
 | 740 | 		free_stack(winch->stack, 0); | 
| Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 741 | 	kfree(winch); | 
 | 742 | } | 
 | 743 |  | 
| Al Viro | 7bea96f | 2006-10-08 22:49:34 +0100 | [diff] [blame] | 744 | static irqreturn_t winch_interrupt(int irq, void *data) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | { | 
 | 746 | 	struct winch *winch = data; | 
 | 747 | 	struct tty_struct *tty; | 
 | 748 | 	struct line *line; | 
 | 749 | 	int err; | 
 | 750 | 	char c; | 
 | 751 |  | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 752 | 	if (winch->fd != -1) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | 		err = generic_read(winch->fd, &c, NULL); | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 754 | 		if (err < 0) { | 
 | 755 | 			if (err != -EAGAIN) { | 
 | 756 | 				printk(KERN_ERR "winch_interrupt : " | 
 | 757 | 				       "read failed, errno = %d\n", -err); | 
 | 758 | 				printk(KERN_ERR "fd %d is losing SIGWINCH " | 
 | 759 | 				       "support\n", winch->tty_fd); | 
| Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 760 | 				free_winch(winch, 0); | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 761 | 				return IRQ_HANDLED; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | 			} | 
 | 763 | 			goto out; | 
 | 764 | 		} | 
 | 765 | 	} | 
| Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 766 | 	tty = winch->tty; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | 	if (tty != NULL) { | 
 | 768 | 		line = tty->driver_data; | 
| Jeff Dike | 438ee67 | 2008-02-04 22:31:19 -0800 | [diff] [blame] | 769 | 		if (line != NULL) { | 
 | 770 | 			chan_window_size(&line->chan_list, &tty->winsize.ws_row, | 
 | 771 | 					 &tty->winsize.ws_col); | 
 | 772 | 			kill_pgrp(tty->pgrp, SIGWINCH, 1); | 
 | 773 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | 	} | 
 | 775 |  out: | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 776 | 	if (winch->fd != -1) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | 		reactivate_fd(winch->fd, WINCH_IRQ); | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 778 | 	return IRQ_HANDLED; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | } | 
 | 780 |  | 
| Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 781 | void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty, | 
 | 782 | 			unsigned long stack) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | { | 
 | 784 | 	struct winch *winch; | 
 | 785 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | 	winch = kmalloc(sizeof(*winch), GFP_KERNEL); | 
 | 787 | 	if (winch == NULL) { | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 788 | 		printk(KERN_ERR "register_winch_irq - kmalloc failed\n"); | 
| Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 789 | 		goto cleanup; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | 	} | 
| Paolo 'Blaisorblade' Giarrusso | 605a69a | 2005-07-07 17:56:52 -0700 | [diff] [blame] | 791 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | 	*winch = ((struct winch) { .list  	= LIST_HEAD_INIT(winch->list), | 
 | 793 | 				   .fd  	= fd, | 
 | 794 | 				   .tty_fd 	= tty_fd, | 
 | 795 | 				   .pid  	= pid, | 
| Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 796 | 				   .tty 	= tty, | 
 | 797 | 				   .stack	= stack }); | 
 | 798 |  | 
 | 799 | 	if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt, | 
 | 800 | 			   IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM, | 
 | 801 | 			   "winch", winch) < 0) { | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 802 | 		printk(KERN_ERR "register_winch_irq - failed to register " | 
 | 803 | 		       "IRQ\n"); | 
| Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 804 | 		goto out_free; | 
 | 805 | 	} | 
| Paolo 'Blaisorblade' Giarrusso | 605a69a | 2005-07-07 17:56:52 -0700 | [diff] [blame] | 806 |  | 
 | 807 | 	spin_lock(&winch_handler_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | 	list_add(&winch->list, &winch_handlers); | 
| Paolo 'Blaisorblade' Giarrusso | 605a69a | 2005-07-07 17:56:52 -0700 | [diff] [blame] | 809 | 	spin_unlock(&winch_handler_lock); | 
 | 810 |  | 
| Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 811 | 	return; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 |  | 
| Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 813 |  out_free: | 
| Jeff Dike | e464bf2 | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 814 | 	kfree(winch); | 
| Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 815 |  cleanup: | 
 | 816 | 	os_kill_process(pid, 1); | 
 | 817 | 	os_close_file(fd); | 
 | 818 | 	if (stack != 0) | 
 | 819 | 		free_stack(stack, 0); | 
| Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 820 | } | 
 | 821 |  | 
| Jeff Dike | e464bf2 | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 822 | static void unregister_winch(struct tty_struct *tty) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | { | 
| Will Newton | 48a0b74 | 2011-01-12 16:59:26 -0800 | [diff] [blame] | 824 | 	struct list_head *ele, *next; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | 	struct winch *winch; | 
 | 826 |  | 
| Jeff Dike | e464bf2 | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 827 | 	spin_lock(&winch_handler_lock); | 
 | 828 |  | 
| Will Newton | 48a0b74 | 2011-01-12 16:59:26 -0800 | [diff] [blame] | 829 | 	list_for_each_safe(ele, next, &winch_handlers) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | 		winch = list_entry(ele, struct winch, list); | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 831 | 		if (winch->tty == tty) { | 
| Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 832 | 			free_winch(winch, 1); | 
| Jeff Dike | e464bf2 | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 833 | 			break; | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 834 | 		} | 
 | 835 | 	} | 
| Jeff Dike | e464bf2 | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 836 | 	spin_unlock(&winch_handler_lock); | 
 | 837 | } | 
 | 838 |  | 
 | 839 | static void winch_cleanup(void) | 
 | 840 | { | 
 | 841 | 	struct list_head *ele, *next; | 
 | 842 | 	struct winch *winch; | 
 | 843 |  | 
 | 844 | 	spin_lock(&winch_handler_lock); | 
 | 845 |  | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 846 | 	list_for_each_safe(ele, next, &winch_handlers) { | 
| Jeff Dike | e464bf2 | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 847 | 		winch = list_entry(ele, struct winch, list); | 
| Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 848 | 		free_winch(winch, 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | 	} | 
| Jeff Dike | e464bf2 | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 850 |  | 
 | 851 | 	spin_unlock(&winch_handler_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | } | 
 | 853 | __uml_exitcall(winch_cleanup); | 
 | 854 |  | 
 | 855 | char *add_xterm_umid(char *base) | 
 | 856 | { | 
 | 857 | 	char *umid, *title; | 
 | 858 | 	int len; | 
 | 859 |  | 
| Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 860 | 	umid = get_umid(); | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 861 | 	if (*umid == '\0') | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 862 | 		return base; | 
| Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 863 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | 	len = strlen(base) + strlen(" ()") + strlen(umid) + 1; | 
 | 865 | 	title = kmalloc(len, GFP_KERNEL); | 
| Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 866 | 	if (title == NULL) { | 
 | 867 | 		printk(KERN_ERR "Failed to allocate buffer for xterm title\n"); | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 868 | 		return base; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | 	} | 
 | 870 |  | 
 | 871 | 	snprintf(title, len, "%s (%s)", base, umid); | 
| Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 872 | 	return title; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | } |