blob: e6eea1485244b6e4e52a845f48522b6f8e5f7599 [file] [log] [blame]
Milton Milleracad9552005-07-07 17:56:24 -07001/*
2 * vio driver interface to hvc_console.c
3 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004 * This code was moved here to allow the remaining code to be reused as a
Milton Milleracad9552005-07-07 17:56:24 -07005 * 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>
Stephen Rothwellde0138d2006-09-25 13:30:51 +100038#include <asm/firmware.h>
Milton Milleracad9552005-07-07 17:56:24 -070039
Ryan S. Arnold45d607e2006-03-27 21:25:16 +020040#include "hvc_console.h"
41
Joe Perches5a71c612010-09-13 09:47:42 +000042static const char hvc_driver_name[] = "hvc_console";
Milton Milleracad9552005-07-07 17:56:24 -070043
44static struct vio_device_id hvc_driver_table[] __devinitdata = {
45 {"serial", "hvterm1"},
Stephen Rothwellfb120da2005-08-17 16:42:59 +100046 { "", "" }
Milton Milleracad9552005-07-07 17:56:24 -070047};
48MODULE_DEVICE_TABLE(vio, hvc_driver_table);
49
Milton Miller70b234a2005-07-07 17:56:26 -070050static int filtered_get_chars(uint32_t vtermno, char *buf, int count)
51{
52 unsigned long got;
53 int i;
54
Ryan S. Arnold45d607e2006-03-27 21:25:16 +020055 /*
56 * Vio firmware will read up to SIZE_VIO_GET_CHARS at its own discretion
57 * so we play safe and avoid the situation where got > count which could
58 * overload the flip buffer.
59 */
60 if (count < SIZE_VIO_GET_CHARS)
61 return -EAGAIN;
62
Milton Miller70b234a2005-07-07 17:56:26 -070063 got = hvc_get_chars(vtermno, buf, count);
64
65 /*
66 * Work around a HV bug where it gives us a null
67 * after every \r. -- paulus
68 */
69 for (i = 1; i < got; ++i) {
70 if (buf[i] == 0 && buf[i-1] == '\r') {
71 --got;
72 if (i < got)
73 memmove(&buf[i], &buf[i+1],
74 got - i);
75 }
76 }
77 return got;
78}
79
Rusty Russell1dff3992009-11-28 12:20:26 +053080static const struct hv_ops hvc_get_put_ops = {
Milton Miller70b234a2005-07-07 17:56:26 -070081 .get_chars = filtered_get_chars,
Milton Miller030ffad2005-07-07 17:56:25 -070082 .put_chars = hvc_put_chars,
Christian Borntraeger611e0972008-06-20 15:24:08 +020083 .notifier_add = notifier_add_irq,
84 .notifier_del = notifier_del_irq,
Hendrik Bruecknerfc362e22008-10-13 23:12:48 +000085 .notifier_hangup = notifier_hangup_irq,
Milton Miller030ffad2005-07-07 17:56:25 -070086};
87
Milton Milleracad9552005-07-07 17:56:24 -070088static int __devinit hvc_vio_probe(struct vio_dev *vdev,
89 const struct vio_device_id *id)
90{
91 struct hvc_struct *hp;
92
93 /* probed with invalid parameters. */
94 if (!vdev || !id)
95 return -EPERM;
96
Stephen Rothwell4e9e95a2006-07-13 18:53:32 +100097 hp = hvc_alloc(vdev->unit_address, vdev->irq, &hvc_get_put_ops,
98 MAX_VIO_PUT_CHARS);
Milton Milleracad9552005-07-07 17:56:24 -070099 if (IS_ERR(hp))
100 return PTR_ERR(hp);
101 dev_set_drvdata(&vdev->dev, hp);
102
103 return 0;
104}
105
106static int __devexit hvc_vio_remove(struct vio_dev *vdev)
107{
108 struct hvc_struct *hp = dev_get_drvdata(&vdev->dev);
109
110 return hvc_remove(hp);
111}
112
113static struct vio_driver hvc_vio_driver = {
Milton Milleracad9552005-07-07 17:56:24 -0700114 .id_table = hvc_driver_table,
115 .probe = hvc_vio_probe,
Mike Frysingere364ca92009-06-10 09:42:30 +0000116 .remove = __devexit_p(hvc_vio_remove),
Milton Milleracad9552005-07-07 17:56:24 -0700117 .driver = {
Stephen Rothwell6fdf5392005-10-24 14:53:21 +1000118 .name = hvc_driver_name,
Milton Milleracad9552005-07-07 17:56:24 -0700119 .owner = THIS_MODULE,
120 }
121};
122
Peter Huewe948c28f2009-08-20 11:14:06 +0000123static int __init hvc_vio_init(void)
Milton Milleracad9552005-07-07 17:56:24 -0700124{
125 int rc;
126
Stephen Rothwellde0138d2006-09-25 13:30:51 +1000127 if (firmware_has_feature(FW_FEATURE_ISERIES))
128 return -EIO;
129
Milton Milleracad9552005-07-07 17:56:24 -0700130 /* Register as a vio device to receive callbacks */
131 rc = vio_register_driver(&hvc_vio_driver);
132
133 return rc;
134}
135module_init(hvc_vio_init); /* after drivers/char/hvc_console.c */
136
Peter Huewe948c28f2009-08-20 11:14:06 +0000137static void __exit hvc_vio_exit(void)
Milton Milleracad9552005-07-07 17:56:24 -0700138{
139 vio_unregister_driver(&hvc_vio_driver);
140}
141module_exit(hvc_vio_exit);
142
143/* the device tree order defines our numbering */
144static int hvc_find_vtys(void)
145{
146 struct device_node *vty;
147 int num_found = 0;
148
149 for (vty = of_find_node_by_name(NULL, "vty"); vty != NULL;
150 vty = of_find_node_by_name(vty, "vty")) {
Jeremy Kerr954a46e2006-07-12 15:39:43 +1000151 const uint32_t *vtermno;
Milton Milleracad9552005-07-07 17:56:24 -0700152
153 /* We have statically defined space for only a certain number
154 * of console adapters.
155 */
Nicolas Palixdc421492008-12-02 03:38:55 +0000156 if (num_found >= MAX_NR_HVC_CONSOLES) {
157 of_node_put(vty);
Milton Milleracad9552005-07-07 17:56:24 -0700158 break;
Nicolas Palixdc421492008-12-02 03:38:55 +0000159 }
Milton Milleracad9552005-07-07 17:56:24 -0700160
Stephen Rothwell01b27262007-04-27 13:41:15 +1000161 vtermno = of_get_property(vty, "reg", NULL);
Milton Milleracad9552005-07-07 17:56:24 -0700162 if (!vtermno)
163 continue;
164
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000165 if (of_device_is_compatible(vty, "hvterm1")) {
Milton Miller030ffad2005-07-07 17:56:25 -0700166 hvc_instantiate(*vtermno, num_found, &hvc_get_put_ops);
Milton Milleracad9552005-07-07 17:56:24 -0700167 ++num_found;
168 }
169 }
170
171 return num_found;
172}
173console_initcall(hvc_find_vtys);