3 * drivers/char/viocons.c
5 * iSeries Virtual Terminal
7 * Authors: Dave Boutcher <boutcher@us.ibm.com>
8 * Ryan Arnold <ryanarn@us.ibm.com>
9 * Colin Devilbiss <devilbis@us.ibm.com>
10 * Stephen Rothwell <sfr@au1.ibm.com>
12 * (C) Copyright 2000, 2001, 2002, 2003, 2004 IBM Corporation
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) anyu later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <linux/kernel.h>
29 #include <linux/proc_fs.h>
30 #include <linux/errno.h>
31 #include <linux/vmalloc.h>
33 #include <linux/console.h>
34 #include <linux/module.h>
35 #include <asm/uaccess.h>
36 #include <linux/init.h>
37 #include <linux/wait.h>
38 #include <linux/spinlock.h>
39 #include <asm/ioctls.h>
41 #include <linux/tty.h>
42 #include <linux/tty_flip.h>
43 #include <linux/sysrq.h>
45 #include <asm/iseries/vio.h>
46 #include <asm/iseries/hv_lp_event.h>
47 #include <asm/iseries/hv_call_event.h>
48 #include <asm/iseries/hv_lp_config.h>
49 #include <asm/iseries/hv_call.h>
52 #error You must turn off CONFIG_VT to use CONFIG_VIOCONS
55 #define VIOTTY_MAGIC (0x0DCB)
58 #define VIOCONS_KERN_WARN KERN_WARNING "viocons: "
59 #define VIOCONS_KERN_INFO KERN_INFO "viocons: "
61 static DEFINE_SPINLOCK(consolelock);
62 static DEFINE_SPINLOCK(consoleloglock);
64 #ifdef CONFIG_MAGIC_SYSRQ
65 static int vio_sysrq_pressed;
66 extern int sysrq_enabled;
69 #define VIOCHAR_NUM_BUF 16
72 * Our port information. We store a pointer to one entry in the
75 static struct port_info {
77 struct tty_struct *tty;
80 u64 seq; /* sequence number of last HV send */
81 u64 ack; /* last ack from HV */
83 * When we get writes faster than we can send it to the partition,
84 * buffer the data here. Note that used is a bit map of used buffers.
85 * It had better have enough bits to hold VIOCHAR_NUM_BUF the bitops assume
86 * it is a multiple of unsigned long
89 u8 *buffer[VIOCHAR_NUM_BUF];
90 int bufferBytes[VIOCHAR_NUM_BUF];
94 } port_info[VTTY_PORTS];
96 #define viochar_is_console(pi) ((pi) == &port_info[0])
97 #define viochar_port(pi) ((pi) - &port_info[0])
99 static void initDataEvent(struct viocharlpevent *viochar, HvLpIndex lp);
101 static struct tty_driver *viotty_driver;
103 static void hvlog(char *fmt, ...)
108 static char buf[256];
110 spin_lock_irqsave(&consoleloglock, flags);
112 i = vscnprintf(buf, sizeof(buf) - 1, fmt, args);
115 HvCall_writeLogBuffer(buf, i);
116 spin_unlock_irqrestore(&consoleloglock, flags);
119 static void hvlogOutput(const char *buf, int count)
124 static const char cr = '\r';
127 spin_lock_irqsave(&consoleloglock, flags);
128 for (index = 0; index < count; index++) {
129 if (buf[index] == '\n') {
131 * Start right after the last '\n' or at the zeroth
132 * array position and output the number of characters
133 * including the newline.
135 HvCall_writeLogBuffer(&buf[begin], index - begin + 1);
137 HvCall_writeLogBuffer(&cr, 1);
140 if ((index - begin) > 0)
141 HvCall_writeLogBuffer(&buf[begin], index - begin);
142 spin_unlock_irqrestore(&consoleloglock, flags);
146 * Make sure we're pointing to a valid port_info structure. Shamelessly
147 * plagerized from serial.c
149 static inline int viotty_paranoia_check(struct port_info *pi,
150 char *name, const char *routine)
152 static const char *bad_pi_addr = VIOCONS_KERN_WARN
153 "warning: bad address for port_info struct (%s) in %s\n";
154 static const char *badmagic = VIOCONS_KERN_WARN
155 "warning: bad magic number for port_info struct (%s) in %s\n";
157 if ((pi < &port_info[0]) || (viochar_port(pi) > VTTY_PORTS)) {
158 printk(bad_pi_addr, name, routine);
161 if (pi->magic != VIOTTY_MAGIC) {
162 printk(badmagic, name, routine);
169 * Add data to our pending-send buffers.
171 * NOTE: Don't use printk in here because it gets nastily recursive.
172 * hvlog can be used to log to the hypervisor buffer
174 static int buffer_add(struct port_info *pi, const char *buf, size_t len)
185 * If there is no space left in the current buffer, we have
186 * filled everything up, so return. If we filled the previous
187 * buffer we would already have moved to the next one.
189 if (pi->bufferBytes[pi->curbuf] == VIOCHAR_MAX_DATA) {
190 hvlog ("\n\rviocons: No overflow buffer available for memcpy().\n");
191 pi->bufferOverflow++;
192 pi->overflowMessage = 1;
197 * Turn on the "used" bit for this buffer. If it's already on,
200 set_bit(pi->curbuf, &pi->used);
203 * See if this buffer has been allocated. If not, allocate it.
205 if (pi->buffer[pi->curbuf] == NULL) {
206 pi->buffer[pi->curbuf] =
207 kmalloc(VIOCHAR_MAX_DATA, GFP_ATOMIC);
208 if (pi->buffer[pi->curbuf] == NULL) {
209 hvlog("\n\rviocons: kmalloc failed allocating spaces for buffer %d.",
215 /* Figure out how much we can copy into this buffer. */
216 if (bleft < (VIOCHAR_MAX_DATA - pi->bufferBytes[pi->curbuf]))
219 curlen = VIOCHAR_MAX_DATA - pi->bufferBytes[pi->curbuf];
221 /* Copy the data into the buffer. */
222 memcpy(pi->buffer[pi->curbuf] + pi->bufferBytes[pi->curbuf],
225 pi->bufferBytes[pi->curbuf] += curlen;
230 * Now see if we've filled this buffer. If not then
231 * we'll try to use it again later. If we've filled it
232 * up then we'll advance the curbuf to the next in the
235 if (pi->bufferBytes[pi->curbuf] == VIOCHAR_MAX_DATA) {
236 nextbuf = (pi->curbuf + 1) % VIOCHAR_NUM_BUF;
238 * Move to the next buffer if it hasn't been used yet
240 if (test_bit(nextbuf, &pi->used) == 0)
241 pi->curbuf = nextbuf;
250 * NOTE: Don't use printk in here because it gets nastily recursive.
251 * hvlog can be used to log to the hypervisor buffer
253 static void send_buffers(struct port_info *pi)
257 struct viocharlpevent *viochar;
260 spin_lock_irqsave(&consolelock, flags);
262 viochar = (struct viocharlpevent *)
263 vio_get_event_buffer(viomajorsubtype_chario);
265 /* Make sure we got a buffer */
266 if (viochar == NULL) {
267 hvlog("\n\rviocons: Can't get viochar buffer in sendBuffers().");
268 spin_unlock_irqrestore(&consolelock, flags);
273 hvlog("\n\rviocons: in sendbuffers(), but no buffers used.\n");
274 vio_free_event_buffer(viomajorsubtype_chario, viochar);
275 spin_unlock_irqrestore(&consolelock, flags);
280 * curbuf points to the buffer we're filling. We want to
281 * start sending AFTER this one.
283 nextbuf = (pi->curbuf + 1) % VIOCHAR_NUM_BUF;
286 * Loop until we find a buffer with the used bit on
288 while (test_bit(nextbuf, &pi->used) == 0)
289 nextbuf = (nextbuf + 1) % VIOCHAR_NUM_BUF;
291 initDataEvent(viochar, pi->lp);
294 * While we have buffers with data, and our send window
297 while ((test_bit(nextbuf, &pi->used)) &&
298 ((pi->seq - pi->ack) < VIOCHAR_WINDOW)) {
299 viochar->len = pi->bufferBytes[nextbuf];
300 viochar->event.xCorrelationToken = pi->seq++;
301 viochar->event.xSizeMinus1 =
302 offsetof(struct viocharlpevent, data) + viochar->len;
304 memcpy(viochar->data, pi->buffer[nextbuf], viochar->len);
306 hvrc = HvCallEvent_signalLpEvent(&viochar->event);
309 * MUST unlock the spinlock before doing a printk
311 vio_free_event_buffer(viomajorsubtype_chario, viochar);
312 spin_unlock_irqrestore(&consolelock, flags);
314 printk(VIOCONS_KERN_WARN
315 "error sending event! return code %d\n",
321 * clear the used bit, zero the number of bytes in
322 * this buffer, and move to the next buffer
324 clear_bit(nextbuf, &pi->used);
325 pi->bufferBytes[nextbuf] = 0;
326 nextbuf = (nextbuf + 1) % VIOCHAR_NUM_BUF;
330 * If we have emptied all the buffers, start at 0 again.
331 * this will re-use any allocated buffers
336 if (pi->overflowMessage)
337 pi->overflowMessage = 0;
344 vio_free_event_buffer(viomajorsubtype_chario, viochar);
345 spin_unlock_irqrestore(&consolelock, flags);
349 * Our internal writer. Gets called both from the console device and
350 * the tty device. the tty pointer will be NULL if called from the console.
351 * Return total number of bytes "written".
353 * NOTE: Don't use printk in here because it gets nastily recursive. hvlog
354 * can be used to log to the hypervisor buffer
356 static int internal_write(struct port_info *pi, const char *buf, size_t len)
363 struct viocharlpevent *viochar;
366 * Write to the hvlog of inbound data are now done prior to
367 * calling internal_write() since internal_write() is only called in
368 * the event that an lp event path is active, which isn't the case for
369 * logging attempts prior to console initialization.
371 * If there is already data queued for this port, send it prior to
372 * attempting to send any new data.
377 spin_lock_irqsave(&consolelock, flags);
379 viochar = vio_get_event_buffer(viomajorsubtype_chario);
380 if (viochar == NULL) {
381 spin_unlock_irqrestore(&consolelock, flags);
382 hvlog("\n\rviocons: Can't get vio buffer in internal_write().");
385 initDataEvent(viochar, pi->lp);
390 while ((bleft > 0) && (pi->used == 0) &&
391 ((pi->seq - pi->ack) < VIOCHAR_WINDOW)) {
392 if (bleft > VIOCHAR_MAX_DATA)
393 curlen = VIOCHAR_MAX_DATA;
397 viochar->event.xCorrelationToken = pi->seq++;
398 memcpy(viochar->data, curbuf, curlen);
399 viochar->len = curlen;
400 viochar->event.xSizeMinus1 =
401 offsetof(struct viocharlpevent, data) + curlen;
403 hvrc = HvCallEvent_signalLpEvent(&viochar->event);
405 hvlog("viocons: error sending event! %d\n", (int)hvrc);
412 /* If we didn't send it all, buffer as much of it as we can. */
414 bleft -= buffer_add(pi, curbuf, bleft);
416 vio_free_event_buffer(viomajorsubtype_chario, viochar);
417 spin_unlock_irqrestore(&consolelock, flags);
421 static struct port_info *get_port_data(struct tty_struct *tty)
424 struct port_info *pi;
426 spin_lock_irqsave(&consolelock, flags);
428 pi = (struct port_info *)tty->driver_data;
429 if (!pi || viotty_paranoia_check(pi, tty->name,
435 * If this is the console device, use the lp from
436 * the first port entry
439 spin_unlock_irqrestore(&consolelock, flags);
444 * Initialize the common fields in a charLpEvent
446 static void initDataEvent(struct viocharlpevent *viochar, HvLpIndex lp)
448 struct HvLpEvent *hev = &viochar->event;
450 memset(viochar, 0, sizeof(struct viocharlpevent));
452 hev->flags = HV_LP_EVENT_VALID | HV_LP_EVENT_DEFERRED_ACK |
454 hev->xType = HvLpEvent_Type_VirtualIo;
455 hev->xSubtype = viomajorsubtype_chario | viochardata;
456 hev->xSourceLp = HvLpConfig_getLpIndex();
458 hev->xSizeMinus1 = sizeof(struct viocharlpevent);
459 hev->xSourceInstanceId = viopath_sourceinst(lp);
460 hev->xTargetInstanceId = viopath_targetinst(lp);
464 * early console device write
466 static void viocons_write_early(struct console *co, const char *s, unsigned count)
468 hvlogOutput(s, count);
472 * console device write
474 static void viocons_write(struct console *co, const char *s, unsigned count)
478 struct port_info *pi;
480 static const char cr = '\r';
483 * Check port data first because the target LP might be valid but
484 * simply not active, in which case we want to hvlog the output.
486 pi = get_port_data(NULL);
488 hvlog("\n\rviocons_write: unable to get port data.");
492 hvlogOutput(s, count);
494 if (!viopath_isactive(pi->lp))
498 * Any newline character found will cause a
499 * carriage return character to be emitted as well.
502 for (index = 0; index < count; index++) {
503 if (s[index] == '\n') {
505 * Newline found. Print everything up to and
506 * including the newline
508 internal_write(pi, &s[begin], index - begin + 1);
510 /* Emit a carriage return as well */
511 internal_write(pi, &cr, 1);
515 /* If any characters left to write, write them now */
516 if ((index - begin) > 0)
517 internal_write(pi, &s[begin], index - begin);
521 * Work out the device associate with this console
523 static struct tty_driver *viocons_device(struct console *c, int *index)
526 return viotty_driver;
530 * console device I/O methods
532 static struct console viocons_early = {
534 .write = viocons_write_early,
535 .flags = CON_PRINTBUFFER,
539 static struct console viocons = {
541 .write = viocons_write,
542 .device = viocons_device,
543 .flags = CON_PRINTBUFFER,
550 static int viotty_open(struct tty_struct *tty, struct file *filp)
554 struct port_info *pi;
558 if ((port < 0) || (port >= VTTY_PORTS))
561 spin_lock_irqsave(&consolelock, flags);
563 pi = &port_info[port];
564 /* If some other TTY is already connected here, reject the open */
565 if ((pi->tty) && (pi->tty != tty)) {
566 spin_unlock_irqrestore(&consolelock, flags);
567 printk(VIOCONS_KERN_WARN
568 "attempt to open device twice from different ttys\n");
571 tty->driver_data = pi;
573 spin_unlock_irqrestore(&consolelock, flags);
581 static void viotty_close(struct tty_struct *tty, struct file *filp)
584 struct port_info *pi;
586 spin_lock_irqsave(&consolelock, flags);
587 pi = (struct port_info *)tty->driver_data;
589 if (!pi || viotty_paranoia_check(pi, tty->name, "viotty_close")) {
590 spin_unlock_irqrestore(&consolelock, flags);
595 spin_unlock_irqrestore(&consolelock, flags);
601 static int viotty_write(struct tty_struct *tty, const unsigned char *buf,
604 struct port_info *pi;
606 pi = get_port_data(tty);
608 hvlog("\n\rviotty_write: no port data.");
612 if (viochar_is_console(pi))
613 hvlogOutput(buf, count);
616 * If the path to this LP is closed, don't bother doing anything more.
617 * just dump the data on the floor and return count. For some reason
618 * some user level programs will attempt to probe available tty's and
619 * they'll attempt a viotty_write on an invalid port which maps to an
620 * invalid target lp. If this is the case then ignore the
621 * viotty_write call and, since the viopath isn't active to this
622 * partition, return count.
624 if (!viopath_isactive(pi->lp))
627 return internal_write(pi, buf, count);
631 * TTY put_char method
633 static void viotty_put_char(struct tty_struct *tty, unsigned char ch)
635 struct port_info *pi;
637 pi = get_port_data(tty);
641 /* This will append '\r' as well if the char is '\n' */
642 if (viochar_is_console(pi))
645 if (viopath_isactive(pi->lp))
646 internal_write(pi, &ch, 1);
650 * TTY write_room method
652 static int viotty_write_room(struct tty_struct *tty)
656 struct port_info *pi;
659 spin_lock_irqsave(&consolelock, flags);
660 pi = (struct port_info *)tty->driver_data;
661 if (!pi || viotty_paranoia_check(pi, tty->name, "viotty_write_room")) {
662 spin_unlock_irqrestore(&consolelock, flags);
666 /* If no buffers are used, return the max size. */
668 spin_unlock_irqrestore(&consolelock, flags);
669 return VIOCHAR_MAX_DATA * VIOCHAR_NUM_BUF;
673 * We retain the spinlock because we want to get an accurate
674 * count and it can change on us between each operation if we
675 * don't hold the spinlock.
677 for (i = 0; ((i < VIOCHAR_NUM_BUF) && (room < VIOCHAR_MAX_DATA)); i++)
678 room += (VIOCHAR_MAX_DATA - pi->bufferBytes[i]);
679 spin_unlock_irqrestore(&consolelock, flags);
681 if (room > VIOCHAR_MAX_DATA)
682 room = VIOCHAR_MAX_DATA;
687 * TTY chars_in_buffer method
689 static int viotty_chars_in_buffer(struct tty_struct *tty)
694 static int viotty_ioctl(struct tty_struct *tty, struct file *file,
695 unsigned int cmd, unsigned long arg)
699 * the ioctls below read/set the flags usually shown in the leds
700 * don't use them - they will go away without warning
704 return put_user(0, (char *)arg);
710 return n_tty_ioctl(tty, file, cmd, arg);
714 * Handle an open charLpEvent. Could be either interrupt or ack
716 static void vioHandleOpenEvent(struct HvLpEvent *event)
719 struct viocharlpevent *cevent = (struct viocharlpevent *)event;
720 u8 port = cevent->virtual_device;
721 struct port_info *pi;
724 if (hvlpevent_is_ack(event)) {
725 if (port >= VTTY_PORTS)
728 spin_lock_irqsave(&consolelock, flags);
729 /* Got the lock, don't cause console output */
731 pi = &port_info[port];
732 if (event->xRc == HvLpEvent_Rc_Good) {
733 pi->seq = pi->ack = 0;
735 * This line allows connections from the primary
736 * partition but once one is connected from the
737 * primary partition nothing short of a reboot
738 * of linux will allow access from the hosting
739 * partition again without a required iSeries fix.
741 pi->lp = event->xTargetLp;
744 spin_unlock_irqrestore(&consolelock, flags);
745 if (event->xRc != HvLpEvent_Rc_Good)
746 printk(VIOCONS_KERN_WARN
747 "handle_open_event: event->xRc == (%d).\n",
750 if (event->xCorrelationToken != 0) {
751 atomic_t *aptr= (atomic_t *)event->xCorrelationToken;
754 printk(VIOCONS_KERN_WARN
755 "weird...got open ack without atomic\n");
759 /* This had better require an ack, otherwise complain */
760 if (!hvlpevent_need_ack(event)) {
761 printk(VIOCONS_KERN_WARN "viocharopen without ack bit!\n");
765 spin_lock_irqsave(&consolelock, flags);
766 /* Got the lock, don't cause console output */
768 /* Make sure this is a good virtual tty */
769 if (port >= VTTY_PORTS) {
770 event->xRc = HvLpEvent_Rc_SubtypeError;
771 cevent->subtype_result_code = viorc_openRejected;
773 * Flag state here since we can't printk while holding
778 pi = &port_info[port];
779 if ((pi->lp != HvLpIndexInvalid) &&
780 (pi->lp != event->xSourceLp)) {
782 * If this is tty is already connected to a different
785 event->xRc = HvLpEvent_Rc_SubtypeError;
786 cevent->subtype_result_code = viorc_openRejected;
789 pi->lp = event->xSourceLp;
790 event->xRc = HvLpEvent_Rc_Good;
791 cevent->subtype_result_code = viorc_good;
792 pi->seq = pi->ack = 0;
797 spin_unlock_irqrestore(&consolelock, flags);
800 printk(VIOCONS_KERN_WARN "open rejected: bad virtual tty.\n");
801 else if (reject == 2)
802 printk(VIOCONS_KERN_WARN
803 "open rejected: console in exclusive use by another partition.\n");
805 /* Return the acknowledgement */
806 HvCallEvent_ackLpEvent(event);
810 * Handle a close charLpEvent. This should ONLY be an Interrupt because the
811 * virtual console should never actually issue a close event to the hypervisor
812 * because the virtual console never goes away. A close event coming from the
813 * hypervisor simply means that there are no client consoles connected to the
816 * Regardless of the number of connections masqueraded on the other side of
817 * the hypervisor ONLY ONE close event should be called to accompany the ONE
818 * open event that is called. The close event should ONLY be called when NO
819 * MORE connections (masqueraded or not) exist on the other side of the
822 static void vioHandleCloseEvent(struct HvLpEvent *event)
825 struct viocharlpevent *cevent = (struct viocharlpevent *)event;
826 u8 port = cevent->virtual_device;
828 if (hvlpevent_is_int(event)) {
829 if (port >= VTTY_PORTS) {
830 printk(VIOCONS_KERN_WARN
831 "close message from invalid virtual device.\n");
835 /* For closes, just mark the console partition invalid */
836 spin_lock_irqsave(&consolelock, flags);
837 /* Got the lock, don't cause console output */
839 if (port_info[port].lp == event->xSourceLp)
840 port_info[port].lp = HvLpIndexInvalid;
842 spin_unlock_irqrestore(&consolelock, flags);
843 printk(VIOCONS_KERN_INFO "close from %d\n", event->xSourceLp);
845 printk(VIOCONS_KERN_WARN
846 "got unexpected close acknowlegement\n");
850 * Handle a config charLpEvent. Could be either interrupt or ack
852 static void vioHandleConfig(struct HvLpEvent *event)
854 struct viocharlpevent *cevent = (struct viocharlpevent *)event;
856 HvCall_writeLogBuffer(cevent->data, cevent->len);
858 if (cevent->data[0] == 0x01)
859 printk(VIOCONS_KERN_INFO "window resized to %d: %d: %d: %d\n",
860 cevent->data[1], cevent->data[2],
861 cevent->data[3], cevent->data[4]);
863 printk(VIOCONS_KERN_WARN "unknown config event\n");
867 * Handle a data charLpEvent.
869 static void vioHandleData(struct HvLpEvent *event)
871 struct tty_struct *tty;
873 struct viocharlpevent *cevent = (struct viocharlpevent *)event;
874 struct port_info *pi;
877 u8 port = cevent->virtual_device;
879 if (port >= VTTY_PORTS) {
880 printk(VIOCONS_KERN_WARN "data on invalid virtual device %d\n",
886 * Hold the spinlock so that we don't take an interrupt that
887 * changes tty between the time we fetch the port_info
888 * pointer and the time we paranoia check.
890 spin_lock_irqsave(&consolelock, flags);
891 pi = &port_info[port];
894 * Change 05/01/2003 - Ryan Arnold: If a partition other than
895 * the current exclusive partition tries to send us data
896 * events then just drop them on the floor because we don't
897 * want his stinking data. He isn't authorized to receive
898 * data because he wasn't the first one to get the console,
899 * therefore he shouldn't be allowed to send data either.
900 * This will work without an iSeries fix.
902 if (pi->lp != event->xSourceLp) {
903 spin_unlock_irqrestore(&consolelock, flags);
909 spin_unlock_irqrestore(&consolelock, flags);
910 printk(VIOCONS_KERN_WARN "no tty for virtual device %d\n",
915 if (tty->magic != TTY_MAGIC) {
916 spin_unlock_irqrestore(&consolelock, flags);
917 printk(VIOCONS_KERN_WARN "tty bad magic\n");
922 * Just to be paranoid, make sure the tty points back to this port
924 pi = (struct port_info *)tty->driver_data;
925 if (!pi || viotty_paranoia_check(pi, tty->name, "vioHandleData")) {
926 spin_unlock_irqrestore(&consolelock, flags);
929 spin_unlock_irqrestore(&consolelock, flags);
932 * Change 07/21/2003 - Ryan Arnold: functionality added to
933 * support sysrq utilizing ^O as the sysrq key. The sysrq
934 * functionality will only work if built into the kernel and
935 * then only if sysrq is enabled through the proc filesystem.
938 for (index = 0; index < cevent->len; index++) {
939 #ifdef CONFIG_MAGIC_SYSRQ
941 /* 0x0f is the ascii character for ^O */
942 if (cevent->data[index] == '\x0f') {
943 vio_sysrq_pressed = 1;
945 * continue because we don't want to add
946 * the sysrq key into the data string.
949 } else if (vio_sysrq_pressed) {
950 handle_sysrq(cevent->data[index], NULL, tty);
951 vio_sysrq_pressed = 0;
953 * continue because we don't want to add
954 * the sysrq sequence into the data string.
961 * The sysrq sequence isn't included in this check if
962 * sysrq is enabled and compiled into the kernel because
963 * the sequence will never get inserted into the buffer.
964 * Don't attempt to copy more data into the buffer than we
965 * have room for because it would fail without indication.
967 if(tty_insert_flip_char(tty, cevent->data[index], TTY_NORMAL) == 0) {
968 printk(VIOCONS_KERN_WARN "input buffer overflow!\n");
975 tty_flip_buffer_push(tty);
979 * Handle an ack charLpEvent.
981 static void vioHandleAck(struct HvLpEvent *event)
983 struct viocharlpevent *cevent = (struct viocharlpevent *)event;
985 u8 port = cevent->virtual_device;
987 if (port >= VTTY_PORTS) {
988 printk(VIOCONS_KERN_WARN "data on invalid virtual device\n");
992 spin_lock_irqsave(&consolelock, flags);
993 port_info[port].ack = event->xCorrelationToken;
994 spin_unlock_irqrestore(&consolelock, flags);
996 if (port_info[port].used)
997 send_buffers(&port_info[port]);
1001 * Handle charLpEvents and route to the appropriate routine
1003 static void vioHandleCharEvent(struct HvLpEvent *event)
1010 charminor = event->xSubtype & VIOMINOR_SUBTYPE_MASK;
1011 switch (charminor) {
1013 vioHandleOpenEvent(event);
1016 vioHandleCloseEvent(event);
1019 vioHandleData(event);
1022 vioHandleAck(event);
1025 vioHandleConfig(event);
1028 if (hvlpevent_is_int(event) && hvlpevent_need_ack(event)) {
1029 event->xRc = HvLpEvent_Rc_InvalidSubtype;
1030 HvCallEvent_ackLpEvent(event);
1036 * Send an open event
1038 static int send_open(HvLpIndex remoteLp, void *sem)
1040 return HvCallEvent_signalLpEventFast(remoteLp,
1041 HvLpEvent_Type_VirtualIo,
1042 viomajorsubtype_chario | viocharopen,
1043 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
1044 viopath_sourceinst(remoteLp),
1045 viopath_targetinst(remoteLp),
1046 (u64)(unsigned long)sem, VIOVERSION << 16,
1050 static struct tty_operations serial_ops = {
1051 .open = viotty_open,
1052 .close = viotty_close,
1053 .write = viotty_write,
1054 .put_char = viotty_put_char,
1055 .write_room = viotty_write_room,
1056 .chars_in_buffer = viotty_chars_in_buffer,
1057 .ioctl = viotty_ioctl,
1060 static int __init viocons_init2(void)
1066 rc = viopath_open(HvLpConfig_getPrimaryLpIndex(),
1067 viomajorsubtype_chario, VIOCHAR_WINDOW + 2);
1069 printk(VIOCONS_KERN_WARN "error opening to primary %d\n", rc);
1071 if (viopath_hostLp == HvLpIndexInvalid)
1075 * And if the primary is not the same as the hosting LP, open to the
1078 if ((viopath_hostLp != HvLpIndexInvalid) &&
1079 (viopath_hostLp != HvLpConfig_getPrimaryLpIndex())) {
1080 printk(VIOCONS_KERN_INFO "open path to hosting (%d)\n",
1082 rc = viopath_open(viopath_hostLp, viomajorsubtype_chario,
1083 VIOCHAR_WINDOW + 2); /* +2 for fudge */
1085 printk(VIOCONS_KERN_WARN
1086 "error opening to partition %d: %d\n",
1087 viopath_hostLp, rc);
1090 if (vio_setHandler(viomajorsubtype_chario, vioHandleCharEvent) < 0)
1091 printk(VIOCONS_KERN_WARN
1092 "error seting handler for console events!\n");
1095 * First, try to open the console to the hosting lp.
1096 * Wait on a semaphore for the response.
1098 atomic_set(&wait_flag, 0);
1099 if ((viopath_isactive(viopath_hostLp)) &&
1100 (send_open(viopath_hostLp, (void *)&wait_flag) == 0)) {
1101 printk(VIOCONS_KERN_INFO "hosting partition %d\n",
1103 while (atomic_read(&wait_flag) == 0)
1105 atomic_set(&wait_flag, 0);
1109 * If we don't have an active console, try the primary
1111 if ((!viopath_isactive(port_info[0].lp)) &&
1112 (viopath_isactive(HvLpConfig_getPrimaryLpIndex())) &&
1113 (send_open(HvLpConfig_getPrimaryLpIndex(), (void *)&wait_flag)
1115 printk(VIOCONS_KERN_INFO "opening console to primary partition\n");
1116 while (atomic_read(&wait_flag) == 0)
1120 /* Initialize the tty_driver structure */
1121 viotty_driver = alloc_tty_driver(VTTY_PORTS);
1122 viotty_driver->owner = THIS_MODULE;
1123 viotty_driver->driver_name = "vioconsole";
1124 viotty_driver->name = "tty";
1125 viotty_driver->name_base = 1;
1126 viotty_driver->major = TTY_MAJOR;
1127 viotty_driver->minor_start = 1;
1128 viotty_driver->type = TTY_DRIVER_TYPE_CONSOLE;
1129 viotty_driver->subtype = 1;
1130 viotty_driver->init_termios = tty_std_termios;
1131 viotty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
1132 tty_set_operations(viotty_driver, &serial_ops);
1134 if (tty_register_driver(viotty_driver)) {
1135 printk(VIOCONS_KERN_WARN "couldn't register console driver\n");
1136 put_tty_driver(viotty_driver);
1137 viotty_driver = NULL;
1140 unregister_console(&viocons_early);
1141 register_console(&viocons);
1146 static int __init viocons_init(void)
1150 printk(VIOCONS_KERN_INFO "registering console\n");
1151 for (i = 0; i < VTTY_PORTS; i++) {
1152 port_info[i].lp = HvLpIndexInvalid;
1153 port_info[i].magic = VIOTTY_MAGIC;
1155 HvCall_setLogBufferFormatAndCodepage(HvCall_LogBuffer_ASCII, 437);
1156 add_preferred_console("viocons", 0, NULL);
1157 register_console(&viocons_early);
1161 console_initcall(viocons_init);
1162 module_init(viocons_init2);