blob: cc95941148fbfe2d824200413f58f08fcb3baafb [file] [log] [blame]
Milton Milleracad9552005-07-07 17:56:24 -07001/*
2 * vio driver interface to hvc_console.c
3 *
4 * This code was moved here to allow the remaing code to be reused as a
5 * generic polling mode with semi-reliable transport driver core to the
6 * console and tty subsystems.
7 *
8 *
9 * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
10 * Copyright (C) 2001 Paul Mackerras <paulus@au.ibm.com>, IBM
11 * Copyright (C) 2004 Benjamin Herrenschmidt <benh@kernel.crashing.org>, IBM Corp.
12 * Copyright (C) 2004 IBM Corporation
13 *
14 * Additional Author(s):
15 * Ryan S. Arnold <rsa@us.ibm.com>
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 */
31
32#include <linux/types.h>
33#include <linux/init.h>
Ryan S. Arnold45d607e2006-03-27 21:25:16 +020034
Milton Milleracad9552005-07-07 17:56:24 -070035#include <asm/hvconsole.h>
36#include <asm/vio.h>
37#include <asm/prom.h>
38
Ryan S. Arnold45d607e2006-03-27 21:25:16 +020039#include "hvc_console.h"
40
Milton Milleracad9552005-07-07 17:56:24 -070041char hvc_driver_name[] = "hvc_console";
42
43static struct vio_device_id hvc_driver_table[] __devinitdata = {
44 {"serial", "hvterm1"},
Stephen Rothwellfb120da2005-08-17 16:42:59 +100045 { "", "" }
Milton Milleracad9552005-07-07 17:56:24 -070046};
47MODULE_DEVICE_TABLE(vio, hvc_driver_table);
48
Milton Miller70b234a2005-07-07 17:56:26 -070049static int filtered_get_chars(uint32_t vtermno, char *buf, int count)
50{
51 unsigned long got;
52 int i;
53
Ryan S. Arnold45d607e2006-03-27 21:25:16 +020054 /*
55 * Vio firmware will read up to SIZE_VIO_GET_CHARS at its own discretion
56 * so we play safe and avoid the situation where got > count which could
57 * overload the flip buffer.
58 */
59 if (count < SIZE_VIO_GET_CHARS)
60 return -EAGAIN;
61
Milton Miller70b234a2005-07-07 17:56:26 -070062 got = hvc_get_chars(vtermno, buf, count);
63
64 /*
65 * Work around a HV bug where it gives us a null
66 * after every \r. -- paulus
67 */
68 for (i = 1; i < got; ++i) {
69 if (buf[i] == 0 && buf[i-1] == '\r') {
70 --got;
71 if (i < got)
72 memmove(&buf[i], &buf[i+1],
73 got - i);
74 }
75 }
76 return got;
77}
78
Milton Miller030ffad2005-07-07 17:56:25 -070079static struct hv_ops hvc_get_put_ops = {
Milton Miller70b234a2005-07-07 17:56:26 -070080 .get_chars = filtered_get_chars,
Milton Miller030ffad2005-07-07 17:56:25 -070081 .put_chars = hvc_put_chars,
82};
83
Milton Milleracad9552005-07-07 17:56:24 -070084static int __devinit hvc_vio_probe(struct vio_dev *vdev,
85 const struct vio_device_id *id)
86{
87 struct hvc_struct *hp;
88
89 /* probed with invalid parameters. */
90 if (!vdev || !id)
91 return -EPERM;
92
Stephen Rothwell4e9e95a2006-07-13 18:53:32 +100093 hp = hvc_alloc(vdev->unit_address, vdev->irq, &hvc_get_put_ops,
94 MAX_VIO_PUT_CHARS);
Milton Milleracad9552005-07-07 17:56:24 -070095 if (IS_ERR(hp))
96 return PTR_ERR(hp);
97 dev_set_drvdata(&vdev->dev, hp);
98
99 return 0;
100}
101
102static int __devexit hvc_vio_remove(struct vio_dev *vdev)
103{
104 struct hvc_struct *hp = dev_get_drvdata(&vdev->dev);
105
106 return hvc_remove(hp);
107}
108
109static struct vio_driver hvc_vio_driver = {
Milton Milleracad9552005-07-07 17:56:24 -0700110 .id_table = hvc_driver_table,
111 .probe = hvc_vio_probe,
112 .remove = hvc_vio_remove,
113 .driver = {
Stephen Rothwell6fdf5392005-10-24 14:53:21 +1000114 .name = hvc_driver_name,
Milton Milleracad9552005-07-07 17:56:24 -0700115 .owner = THIS_MODULE,
116 }
117};
118
119static int hvc_vio_init(void)
120{
121 int rc;
122
123 /* Register as a vio device to receive callbacks */
124 rc = vio_register_driver(&hvc_vio_driver);
125
126 return rc;
127}
128module_init(hvc_vio_init); /* after drivers/char/hvc_console.c */
129
130static void hvc_vio_exit(void)
131{
132 vio_unregister_driver(&hvc_vio_driver);
133}
134module_exit(hvc_vio_exit);
135
136/* the device tree order defines our numbering */
137static int hvc_find_vtys(void)
138{
139 struct device_node *vty;
140 int num_found = 0;
141
142 for (vty = of_find_node_by_name(NULL, "vty"); vty != NULL;
143 vty = of_find_node_by_name(vty, "vty")) {
Jeremy Kerr954a46e2006-07-12 15:39:43 +1000144 const uint32_t *vtermno;
Milton Milleracad9552005-07-07 17:56:24 -0700145
146 /* We have statically defined space for only a certain number
147 * of console adapters.
148 */
149 if (num_found >= MAX_NR_HVC_CONSOLES)
150 break;
151
Jeremy Kerr954a46e2006-07-12 15:39:43 +1000152 vtermno = get_property(vty, "reg", NULL);
Milton Milleracad9552005-07-07 17:56:24 -0700153 if (!vtermno)
154 continue;
155
156 if (device_is_compatible(vty, "hvterm1")) {
Milton Miller030ffad2005-07-07 17:56:25 -0700157 hvc_instantiate(*vtermno, num_found, &hvc_get_put_ops);
Milton Milleracad9552005-07-07 17:56:24 -0700158 ++num_found;
159 }
160 }
161
162 return num_found;
163}
164console_initcall(hvc_find_vtys);