xref: /linux/drivers/tty/vcc.c (revision 5d171050e28f823aeb040f2830da4d3422b54b63)
155bd2133SJag Raman /* vcc.c: sun4v virtual channel concentrator
255bd2133SJag Raman  *
355bd2133SJag Raman  * Copyright (C) 2017 Oracle. All rights reserved.
455bd2133SJag Raman  */
555bd2133SJag Raman 
6*5d171050SJag Raman #include <linux/delay.h>
7*5d171050SJag Raman #include <linux/interrupt.h>
855bd2133SJag Raman #include <linux/module.h>
9*5d171050SJag Raman #include <linux/slab.h>
10ce808b74SJag Raman #include <linux/tty.h>
11*5d171050SJag Raman #include <asm/vio.h>
12*5d171050SJag Raman #include <asm/ldc.h>
1355bd2133SJag Raman 
14f283ebd5SJag Raman #define DRV_MODULE_NAME		"vcc"
15f283ebd5SJag Raman #define DRV_MODULE_VERSION	"1.1"
16f283ebd5SJag Raman #define DRV_MODULE_RELDATE	"July 1, 2017"
17f283ebd5SJag Raman 
18ce808b74SJag Raman static char version[] =
19ce808b74SJag Raman 	DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
20ce808b74SJag Raman 
21f283ebd5SJag Raman MODULE_DESCRIPTION("Sun LDOM virtual console concentrator driver");
22f283ebd5SJag Raman MODULE_LICENSE("GPL");
23f283ebd5SJag Raman MODULE_VERSION(DRV_MODULE_VERSION);
24f283ebd5SJag Raman 
25*5d171050SJag Raman struct vcc_port {
26*5d171050SJag Raman 	struct vio_driver_state vio;
27*5d171050SJag Raman 
28*5d171050SJag Raman 	spinlock_t lock;
29*5d171050SJag Raman 	char *domain;
30*5d171050SJag Raman 	struct tty_struct *tty;	/* only populated while dev is open */
31*5d171050SJag Raman 	unsigned long index;	/* index into the vcc_table */
32*5d171050SJag Raman 
33*5d171050SJag Raman 	u64 refcnt;
34*5d171050SJag Raman 	bool excl_locked;
35*5d171050SJag Raman 
36*5d171050SJag Raman 	bool removed;
37*5d171050SJag Raman 
38*5d171050SJag Raman 	/* This buffer is required to support the tty write_room interface
39*5d171050SJag Raman 	 * and guarantee that any characters that the driver accepts will
40*5d171050SJag Raman 	 * be eventually sent, either immediately or later.
41*5d171050SJag Raman 	 */
42*5d171050SJag Raman 	int chars_in_buffer;
43*5d171050SJag Raman 	struct vio_vcc buffer;
44*5d171050SJag Raman 
45*5d171050SJag Raman 	struct timer_list rx_timer;
46*5d171050SJag Raman 	struct timer_list tx_timer;
47*5d171050SJag Raman };
48*5d171050SJag Raman 
49*5d171050SJag Raman /* Microseconds that thread will delay waiting for a vcc port ref */
50*5d171050SJag Raman #define VCC_REF_DELAY		100
51*5d171050SJag Raman 
52ce808b74SJag Raman #define VCC_MAX_PORTS		1024
53ce808b74SJag Raman #define VCC_MINOR_START		0	/* must be zero */
54ce808b74SJag Raman 
55ce808b74SJag Raman static const char vcc_driver_name[] = "vcc";
56ce808b74SJag Raman static const char vcc_device_node[] = "vcc";
57ce808b74SJag Raman static struct tty_driver *vcc_tty_driver;
58ce808b74SJag Raman 
59*5d171050SJag Raman static struct vcc_port *vcc_table[VCC_MAX_PORTS];
60*5d171050SJag Raman static DEFINE_SPINLOCK(vcc_table_lock);
61*5d171050SJag Raman 
62f283ebd5SJag Raman int vcc_dbg;
63f283ebd5SJag Raman int vcc_dbg_ldc;
64f283ebd5SJag Raman int vcc_dbg_vio;
65f283ebd5SJag Raman 
66f283ebd5SJag Raman module_param(vcc_dbg, uint, 0664);
67f283ebd5SJag Raman module_param(vcc_dbg_ldc, uint, 0664);
68f283ebd5SJag Raman module_param(vcc_dbg_vio, uint, 0664);
69f283ebd5SJag Raman 
70f283ebd5SJag Raman #define VCC_DBG_DRV	0x1
71f283ebd5SJag Raman #define VCC_DBG_LDC	0x2
72f283ebd5SJag Raman #define VCC_DBG_PKT	0x4
73f283ebd5SJag Raman 
74f283ebd5SJag Raman #define vccdbg(f, a...)						\
75f283ebd5SJag Raman 	do {							\
76f283ebd5SJag Raman 		if (vcc_dbg & VCC_DBG_DRV)			\
77f283ebd5SJag Raman 			pr_info(f, ## a);			\
78f283ebd5SJag Raman 	} while (0)						\
79f283ebd5SJag Raman 
80f283ebd5SJag Raman #define vccdbgl(l)						\
81f283ebd5SJag Raman 	do {							\
82f283ebd5SJag Raman 		if (vcc_dbg & VCC_DBG_LDC)			\
83f283ebd5SJag Raman 			ldc_print(l);				\
84f283ebd5SJag Raman 	} while (0)						\
85f283ebd5SJag Raman 
86f283ebd5SJag Raman #define vccdbgp(pkt)						\
87f283ebd5SJag Raman 	do {							\
88f283ebd5SJag Raman 		if (vcc_dbg & VCC_DBG_PKT) {			\
89f283ebd5SJag Raman 			int i;					\
90f283ebd5SJag Raman 			for (i = 0; i < pkt.tag.stype; i++)	\
91f283ebd5SJag Raman 				pr_info("[%c]", pkt.data[i]);	\
92f283ebd5SJag Raman 		}						\
93f283ebd5SJag Raman 	} while (0)						\
94f283ebd5SJag Raman 
95ce808b74SJag Raman /* Note: Be careful when adding flags to this line discipline.  Don't
96ce808b74SJag Raman  * add anything that will cause echoing or we'll go into recursive
97ce808b74SJag Raman  * loop echoing chars back and forth with the console drivers.
98ce808b74SJag Raman  */
99ce808b74SJag Raman static struct ktermios vcc_tty_termios = {
100ce808b74SJag Raman 	.c_iflag = IGNBRK | IGNPAR,
101ce808b74SJag Raman 	.c_oflag = OPOST,
102ce808b74SJag Raman 	.c_cflag = B38400 | CS8 | CREAD | HUPCL,
103ce808b74SJag Raman 	.c_cc = INIT_C_CC,
104ce808b74SJag Raman 	.c_ispeed = 38400,
105ce808b74SJag Raman 	.c_ospeed = 38400
106ce808b74SJag Raman };
107ce808b74SJag Raman 
108*5d171050SJag Raman /**
109*5d171050SJag Raman  * vcc_table_add() - Add VCC port to the VCC table
110*5d171050SJag Raman  * @port: pointer to the VCC port
111*5d171050SJag Raman  *
112*5d171050SJag Raman  * Return: index of the port in the VCC table on success,
113*5d171050SJag Raman  *	   -1 on failure
114*5d171050SJag Raman  */
115*5d171050SJag Raman static int vcc_table_add(struct vcc_port *port)
116*5d171050SJag Raman {
117*5d171050SJag Raman 	unsigned long flags;
118*5d171050SJag Raman 	int i;
119*5d171050SJag Raman 
120*5d171050SJag Raman 	spin_lock_irqsave(&vcc_table_lock, flags);
121*5d171050SJag Raman 	for (i = VCC_MINOR_START; i < VCC_MAX_PORTS; i++) {
122*5d171050SJag Raman 		if (!vcc_table[i]) {
123*5d171050SJag Raman 			vcc_table[i] = port;
124*5d171050SJag Raman 			break;
125*5d171050SJag Raman 		}
126*5d171050SJag Raman 	}
127*5d171050SJag Raman 	spin_unlock_irqrestore(&vcc_table_lock, flags);
128*5d171050SJag Raman 
129*5d171050SJag Raman 	if (i < VCC_MAX_PORTS)
130*5d171050SJag Raman 		return i;
131*5d171050SJag Raman 	else
132*5d171050SJag Raman 		return -1;
133*5d171050SJag Raman }
134*5d171050SJag Raman 
135*5d171050SJag Raman /**
136*5d171050SJag Raman  * vcc_table_remove() - Removes a VCC port from the VCC table
137*5d171050SJag Raman  * @index: Index into the VCC table
138*5d171050SJag Raman  */
139*5d171050SJag Raman static void vcc_table_remove(unsigned long index)
140*5d171050SJag Raman {
141*5d171050SJag Raman 	unsigned long flags;
142*5d171050SJag Raman 
143*5d171050SJag Raman 	if (WARN_ON(index >= VCC_MAX_PORTS))
144*5d171050SJag Raman 		return;
145*5d171050SJag Raman 
146*5d171050SJag Raman 	spin_lock_irqsave(&vcc_table_lock, flags);
147*5d171050SJag Raman 	vcc_table[index] = NULL;
148*5d171050SJag Raman 	spin_unlock_irqrestore(&vcc_table_lock, flags);
149*5d171050SJag Raman }
150*5d171050SJag Raman 
151*5d171050SJag Raman /**
152*5d171050SJag Raman  * vcc_get() - Gets a reference to VCC port
153*5d171050SJag Raman  * @index: Index into the VCC table
154*5d171050SJag Raman  * @excl: Indicates if an exclusive access is requested
155*5d171050SJag Raman  *
156*5d171050SJag Raman  * Return: reference to the VCC port, if found
157*5d171050SJag Raman  *	   NULL, if port not found
158*5d171050SJag Raman  */
159*5d171050SJag Raman static struct vcc_port *vcc_get(unsigned long index, bool excl)
160*5d171050SJag Raman {
161*5d171050SJag Raman 	struct vcc_port *port;
162*5d171050SJag Raman 	unsigned long flags;
163*5d171050SJag Raman 
164*5d171050SJag Raman try_again:
165*5d171050SJag Raman 	spin_lock_irqsave(&vcc_table_lock, flags);
166*5d171050SJag Raman 
167*5d171050SJag Raman 	port = vcc_table[index];
168*5d171050SJag Raman 	if (!port) {
169*5d171050SJag Raman 		spin_unlock_irqrestore(&vcc_table_lock, flags);
170*5d171050SJag Raman 		return NULL;
171*5d171050SJag Raman 	}
172*5d171050SJag Raman 
173*5d171050SJag Raman 	if (!excl) {
174*5d171050SJag Raman 		if (port->excl_locked) {
175*5d171050SJag Raman 			spin_unlock_irqrestore(&vcc_table_lock, flags);
176*5d171050SJag Raman 			udelay(VCC_REF_DELAY);
177*5d171050SJag Raman 			goto try_again;
178*5d171050SJag Raman 		}
179*5d171050SJag Raman 		port->refcnt++;
180*5d171050SJag Raman 		spin_unlock_irqrestore(&vcc_table_lock, flags);
181*5d171050SJag Raman 		return port;
182*5d171050SJag Raman 	}
183*5d171050SJag Raman 
184*5d171050SJag Raman 	if (port->refcnt) {
185*5d171050SJag Raman 		spin_unlock_irqrestore(&vcc_table_lock, flags);
186*5d171050SJag Raman 		/* Threads wanting exclusive access will wait half the time,
187*5d171050SJag Raman 		 * probably giving them higher priority in the case of
188*5d171050SJag Raman 		 * multiple waiters.
189*5d171050SJag Raman 		 */
190*5d171050SJag Raman 		udelay(VCC_REF_DELAY/2);
191*5d171050SJag Raman 		goto try_again;
192*5d171050SJag Raman 	}
193*5d171050SJag Raman 
194*5d171050SJag Raman 	port->refcnt++;
195*5d171050SJag Raman 	port->excl_locked = true;
196*5d171050SJag Raman 	spin_unlock_irqrestore(&vcc_table_lock, flags);
197*5d171050SJag Raman 
198*5d171050SJag Raman 	return port;
199*5d171050SJag Raman }
200*5d171050SJag Raman 
201*5d171050SJag Raman /**
202*5d171050SJag Raman  * vcc_put() - Returns a reference to VCC port
203*5d171050SJag Raman  * @port: pointer to VCC port
204*5d171050SJag Raman  * @excl: Indicates if the returned reference is an exclusive reference
205*5d171050SJag Raman  *
206*5d171050SJag Raman  * Note: It's the caller's responsibility to ensure the correct value
207*5d171050SJag Raman  *	 for the excl flag
208*5d171050SJag Raman  */
209*5d171050SJag Raman static void vcc_put(struct vcc_port *port, bool excl)
210*5d171050SJag Raman {
211*5d171050SJag Raman 	unsigned long flags;
212*5d171050SJag Raman 
213*5d171050SJag Raman 	if (!port)
214*5d171050SJag Raman 		return;
215*5d171050SJag Raman 
216*5d171050SJag Raman 	spin_lock_irqsave(&vcc_table_lock, flags);
217*5d171050SJag Raman 
218*5d171050SJag Raman 	/* check if caller attempted to put with the wrong flags */
219*5d171050SJag Raman 	if (WARN_ON((excl && !port->excl_locked) ||
220*5d171050SJag Raman 		    (!excl && port->excl_locked)))
221*5d171050SJag Raman 		goto done;
222*5d171050SJag Raman 
223*5d171050SJag Raman 	port->refcnt--;
224*5d171050SJag Raman 
225*5d171050SJag Raman 	if (excl)
226*5d171050SJag Raman 		port->excl_locked = false;
227*5d171050SJag Raman 
228*5d171050SJag Raman done:
229*5d171050SJag Raman 	spin_unlock_irqrestore(&vcc_table_lock, flags);
230*5d171050SJag Raman }
231*5d171050SJag Raman 
232*5d171050SJag Raman /**
233*5d171050SJag Raman  * vcc_get_ne() - Get a non-exclusive reference to VCC port
234*5d171050SJag Raman  * @index: Index into the VCC table
235*5d171050SJag Raman  *
236*5d171050SJag Raman  * Gets a non-exclusive reference to VCC port, if it's not removed
237*5d171050SJag Raman  *
238*5d171050SJag Raman  * Return: pointer to the VCC port, if found
239*5d171050SJag Raman  *	   NULL, if port not found
240*5d171050SJag Raman  */
241*5d171050SJag Raman static struct vcc_port *vcc_get_ne(unsigned long index)
242*5d171050SJag Raman {
243*5d171050SJag Raman 	struct vcc_port *port;
244*5d171050SJag Raman 
245*5d171050SJag Raman 	port = vcc_get(index, false);
246*5d171050SJag Raman 
247*5d171050SJag Raman 	if (port && port->removed) {
248*5d171050SJag Raman 		vcc_put(port, false);
249*5d171050SJag Raman 		return NULL;
250*5d171050SJag Raman 	}
251*5d171050SJag Raman 
252*5d171050SJag Raman 	return port;
253*5d171050SJag Raman }
254*5d171050SJag Raman 
255*5d171050SJag Raman static void vcc_event(void *arg, int event)
256*5d171050SJag Raman {
257*5d171050SJag Raman }
258*5d171050SJag Raman 
259*5d171050SJag Raman static struct ldc_channel_config vcc_ldc_cfg = {
260*5d171050SJag Raman 	.event		= vcc_event,
261*5d171050SJag Raman 	.mtu		= VIO_VCC_MTU_SIZE,
262*5d171050SJag Raman 	.mode		= LDC_MODE_RAW,
263*5d171050SJag Raman 	.debug		= 0,
264*5d171050SJag Raman };
265*5d171050SJag Raman 
266*5d171050SJag Raman /* Ordered from largest major to lowest */
267*5d171050SJag Raman static struct vio_version vcc_versions[] = {
268*5d171050SJag Raman 	{ .major = 1, .minor = 0 },
269*5d171050SJag Raman };
270*5d171050SJag Raman 
271*5d171050SJag Raman /**
272*5d171050SJag Raman  * vcc_probe() - Initialize VCC port
273*5d171050SJag Raman  * @vdev: Pointer to VIO device of the new VCC port
274*5d171050SJag Raman  * @id: VIO device ID
275*5d171050SJag Raman  *
276*5d171050SJag Raman  * Initializes a VCC port to receive serial console data from
277*5d171050SJag Raman  * the guest domain. Sets up a TTY end point on the control
278*5d171050SJag Raman  * domain. Sets up VIO/LDC link between the guest & control
279*5d171050SJag Raman  * domain endpoints.
280*5d171050SJag Raman  *
281*5d171050SJag Raman  * Return: status of the probe
282*5d171050SJag Raman  */
283*5d171050SJag Raman static int vcc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
284*5d171050SJag Raman {
285*5d171050SJag Raman 	struct mdesc_handle *hp;
286*5d171050SJag Raman 	struct vcc_port *port;
287*5d171050SJag Raman 	struct device *dev;
288*5d171050SJag Raman 	const char *domain;
289*5d171050SJag Raman 	char *name;
290*5d171050SJag Raman 	u64 node;
291*5d171050SJag Raman 	int rv;
292*5d171050SJag Raman 
293*5d171050SJag Raman 	vccdbg("VCC: name=%s\n", dev_name(&vdev->dev));
294*5d171050SJag Raman 
295*5d171050SJag Raman 	if (!vcc_tty_driver) {
296*5d171050SJag Raman 		pr_err("VCC: TTY driver not registered\n");
297*5d171050SJag Raman 		return -ENODEV;
298*5d171050SJag Raman 	}
299*5d171050SJag Raman 
300*5d171050SJag Raman 	port = kzalloc(sizeof(struct vcc_port), GFP_KERNEL);
301*5d171050SJag Raman 	if (!port)
302*5d171050SJag Raman 		return -ENOMEM;
303*5d171050SJag Raman 
304*5d171050SJag Raman 	name = kstrdup(dev_name(&vdev->dev), GFP_KERNEL);
305*5d171050SJag Raman 
306*5d171050SJag Raman 	rv = vio_driver_init(&port->vio, vdev, VDEV_CONSOLE_CON, vcc_versions,
307*5d171050SJag Raman 			     ARRAY_SIZE(vcc_versions), NULL, name);
308*5d171050SJag Raman 	if (rv)
309*5d171050SJag Raman 		goto free_port;
310*5d171050SJag Raman 
311*5d171050SJag Raman 	port->vio.debug = vcc_dbg_vio;
312*5d171050SJag Raman 	vcc_ldc_cfg.debug = vcc_dbg_ldc;
313*5d171050SJag Raman 
314*5d171050SJag Raman 	rv = vio_ldc_alloc(&port->vio, &vcc_ldc_cfg, port);
315*5d171050SJag Raman 	if (rv)
316*5d171050SJag Raman 		goto free_port;
317*5d171050SJag Raman 
318*5d171050SJag Raman 	spin_lock_init(&port->lock);
319*5d171050SJag Raman 
320*5d171050SJag Raman 	port->index = vcc_table_add(port);
321*5d171050SJag Raman 	if (port->index == -1) {
322*5d171050SJag Raman 		pr_err("VCC: no more TTY indices left for allocation\n");
323*5d171050SJag Raman 		goto free_ldc;
324*5d171050SJag Raman 	}
325*5d171050SJag Raman 
326*5d171050SJag Raman 	/* Register the device using VCC table index as TTY index */
327*5d171050SJag Raman 	dev = tty_register_device(vcc_tty_driver, port->index, &vdev->dev);
328*5d171050SJag Raman 	if (IS_ERR(dev)) {
329*5d171050SJag Raman 		rv = PTR_ERR(dev);
330*5d171050SJag Raman 		goto free_table;
331*5d171050SJag Raman 	}
332*5d171050SJag Raman 
333*5d171050SJag Raman 	hp = mdesc_grab();
334*5d171050SJag Raman 
335*5d171050SJag Raman 	node = vio_vdev_node(hp, vdev);
336*5d171050SJag Raman 	if (node == MDESC_NODE_NULL) {
337*5d171050SJag Raman 		rv = -ENXIO;
338*5d171050SJag Raman 		mdesc_release(hp);
339*5d171050SJag Raman 		goto unreg_tty;
340*5d171050SJag Raman 	}
341*5d171050SJag Raman 
342*5d171050SJag Raman 	domain = mdesc_get_property(hp, node, "vcc-domain-name", NULL);
343*5d171050SJag Raman 	if (!domain) {
344*5d171050SJag Raman 		rv = -ENXIO;
345*5d171050SJag Raman 		mdesc_release(hp);
346*5d171050SJag Raman 		goto unreg_tty;
347*5d171050SJag Raman 	}
348*5d171050SJag Raman 	port->domain = kstrdup(domain, GFP_KERNEL);
349*5d171050SJag Raman 
350*5d171050SJag Raman 	mdesc_release(hp);
351*5d171050SJag Raman 
352*5d171050SJag Raman 	dev_set_drvdata(&vdev->dev, port);
353*5d171050SJag Raman 
354*5d171050SJag Raman 	/* It's possible to receive IRQs in the middle of vio_port_up. Disable
355*5d171050SJag Raman 	 * IRQs until the port is up.
356*5d171050SJag Raman 	 */
357*5d171050SJag Raman 	disable_irq_nosync(vdev->rx_irq);
358*5d171050SJag Raman 	vio_port_up(&port->vio);
359*5d171050SJag Raman 	enable_irq(vdev->rx_irq);
360*5d171050SJag Raman 
361*5d171050SJag Raman 	return 0;
362*5d171050SJag Raman 
363*5d171050SJag Raman unreg_tty:
364*5d171050SJag Raman 	tty_unregister_device(vcc_tty_driver, port->index);
365*5d171050SJag Raman free_table:
366*5d171050SJag Raman 	vcc_table_remove(port->index);
367*5d171050SJag Raman free_ldc:
368*5d171050SJag Raman 	vio_ldc_free(&port->vio);
369*5d171050SJag Raman free_port:
370*5d171050SJag Raman 	kfree(name);
371*5d171050SJag Raman 	kfree(port);
372*5d171050SJag Raman 
373*5d171050SJag Raman 	return rv;
374*5d171050SJag Raman }
375*5d171050SJag Raman 
376*5d171050SJag Raman /**
377*5d171050SJag Raman  * vcc_remove() - Terminate a VCC port
378*5d171050SJag Raman  * @vdev: Pointer to VIO device of the VCC port
379*5d171050SJag Raman  *
380*5d171050SJag Raman  * Terminates a VCC port. Sets up the teardown of TTY and
381*5d171050SJag Raman  * VIO/LDC link between guest and primary domains.
382*5d171050SJag Raman  *
383*5d171050SJag Raman  * Return: status of removal
384*5d171050SJag Raman  */
385*5d171050SJag Raman static int vcc_remove(struct vio_dev *vdev)
386*5d171050SJag Raman {
387*5d171050SJag Raman 	struct vcc_port *port = dev_get_drvdata(&vdev->dev);
388*5d171050SJag Raman 
389*5d171050SJag Raman 	if (!port)
390*5d171050SJag Raman 		return -ENODEV;
391*5d171050SJag Raman 
392*5d171050SJag Raman 	/* If there's a process with the device open, do a synchronous
393*5d171050SJag Raman 	 * hangup of the TTY. This *may* cause the process to call close
394*5d171050SJag Raman 	 * asynchronously, but it's not guaranteed.
395*5d171050SJag Raman 	 */
396*5d171050SJag Raman 	if (port->tty)
397*5d171050SJag Raman 		tty_vhangup(port->tty);
398*5d171050SJag Raman 
399*5d171050SJag Raman 	/* Get exclusive reference to VCC, ensures that there are no other
400*5d171050SJag Raman 	 * clients to this port
401*5d171050SJag Raman 	 */
402*5d171050SJag Raman 	port = vcc_get(port->index, true);
403*5d171050SJag Raman 
404*5d171050SJag Raman 	if (WARN_ON(!port))
405*5d171050SJag Raman 		return -ENODEV;
406*5d171050SJag Raman 
407*5d171050SJag Raman 	tty_unregister_device(vcc_tty_driver, port->index);
408*5d171050SJag Raman 
409*5d171050SJag Raman 	del_timer_sync(&port->vio.timer);
410*5d171050SJag Raman 	vio_ldc_free(&port->vio);
411*5d171050SJag Raman 	dev_set_drvdata(&vdev->dev, NULL);
412*5d171050SJag Raman 
413*5d171050SJag Raman 	if (port->tty) {
414*5d171050SJag Raman 		port->removed = true;
415*5d171050SJag Raman 		vcc_put(port, true);
416*5d171050SJag Raman 	} else {
417*5d171050SJag Raman 		vcc_table_remove(port->index);
418*5d171050SJag Raman 
419*5d171050SJag Raman 		kfree(port->vio.name);
420*5d171050SJag Raman 		kfree(port->domain);
421*5d171050SJag Raman 		kfree(port);
422*5d171050SJag Raman 	}
423*5d171050SJag Raman 
424*5d171050SJag Raman 	return 0;
425*5d171050SJag Raman }
426*5d171050SJag Raman 
427*5d171050SJag Raman static const struct vio_device_id vcc_match[] = {
428*5d171050SJag Raman 	{
429*5d171050SJag Raman 		.type = "vcc-port",
430*5d171050SJag Raman 	},
431*5d171050SJag Raman 	{},
432*5d171050SJag Raman };
433*5d171050SJag Raman MODULE_DEVICE_TABLE(vio, vcc_match);
434*5d171050SJag Raman 
435*5d171050SJag Raman static struct vio_driver vcc_driver = {
436*5d171050SJag Raman 	.id_table	= vcc_match,
437*5d171050SJag Raman 	.probe		= vcc_probe,
438*5d171050SJag Raman 	.remove		= vcc_remove,
439*5d171050SJag Raman 	.name		= "vcc",
440*5d171050SJag Raman };
441*5d171050SJag Raman 
442ce808b74SJag Raman static const struct tty_operations vcc_ops;
443ce808b74SJag Raman 
444ce808b74SJag Raman #define VCC_TTY_FLAGS   (TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_REAL_RAW)
445ce808b74SJag Raman 
446ce808b74SJag Raman static int vcc_tty_init(void)
447ce808b74SJag Raman {
448ce808b74SJag Raman 	int rv;
449ce808b74SJag Raman 
450ce808b74SJag Raman 	pr_info("VCC: %s\n", version);
451ce808b74SJag Raman 
452ce808b74SJag Raman 	vcc_tty_driver = tty_alloc_driver(VCC_MAX_PORTS, VCC_TTY_FLAGS);
453ce808b74SJag Raman 	if (!vcc_tty_driver) {
454ce808b74SJag Raman 		pr_err("VCC: TTY driver alloc failed\n");
455ce808b74SJag Raman 		return -ENOMEM;
456ce808b74SJag Raman 	}
457ce808b74SJag Raman 
458ce808b74SJag Raman 	vcc_tty_driver->driver_name = vcc_driver_name;
459ce808b74SJag Raman 	vcc_tty_driver->name = vcc_device_node;
460ce808b74SJag Raman 
461ce808b74SJag Raman 	vcc_tty_driver->minor_start = VCC_MINOR_START;
462ce808b74SJag Raman 	vcc_tty_driver->type = TTY_DRIVER_TYPE_SYSTEM;
463ce808b74SJag Raman 	vcc_tty_driver->init_termios = vcc_tty_termios;
464ce808b74SJag Raman 
465ce808b74SJag Raman 	tty_set_operations(vcc_tty_driver, &vcc_ops);
466ce808b74SJag Raman 
467ce808b74SJag Raman 	rv = tty_register_driver(vcc_tty_driver);
468ce808b74SJag Raman 	if (rv) {
469ce808b74SJag Raman 		pr_err("VCC: TTY driver registration failed\n");
470ce808b74SJag Raman 		put_tty_driver(vcc_tty_driver);
471ce808b74SJag Raman 		vcc_tty_driver = NULL;
472ce808b74SJag Raman 		return rv;
473ce808b74SJag Raman 	}
474ce808b74SJag Raman 
475ce808b74SJag Raman 	vccdbg("VCC: TTY driver registered\n");
476ce808b74SJag Raman 
477ce808b74SJag Raman 	return 0;
478ce808b74SJag Raman }
479ce808b74SJag Raman 
480ce808b74SJag Raman static void vcc_tty_exit(void)
481ce808b74SJag Raman {
482ce808b74SJag Raman 	tty_unregister_driver(vcc_tty_driver);
483ce808b74SJag Raman 	put_tty_driver(vcc_tty_driver);
484ce808b74SJag Raman 	vccdbg("VCC: TTY driver unregistered\n");
485ce808b74SJag Raman 
486ce808b74SJag Raman 	vcc_tty_driver = NULL;
487ce808b74SJag Raman }
488ce808b74SJag Raman 
48955bd2133SJag Raman static int __init vcc_init(void)
49055bd2133SJag Raman {
491ce808b74SJag Raman 	int rv;
492ce808b74SJag Raman 
493ce808b74SJag Raman 	rv = vcc_tty_init();
494ce808b74SJag Raman 	if (rv) {
495ce808b74SJag Raman 		pr_err("VCC: TTY init failed\n");
496ce808b74SJag Raman 		return rv;
497ce808b74SJag Raman 	}
498ce808b74SJag Raman 
499*5d171050SJag Raman 	rv = vio_register_driver(&vcc_driver);
500*5d171050SJag Raman 	if (rv) {
501*5d171050SJag Raman 		pr_err("VCC: VIO driver registration failed\n");
502*5d171050SJag Raman 		vcc_tty_exit();
503*5d171050SJag Raman 	} else {
504*5d171050SJag Raman 		vccdbg("VCC: VIO driver registered successfully\n");
505*5d171050SJag Raman 	}
506*5d171050SJag Raman 
507ce808b74SJag Raman 	return rv;
50855bd2133SJag Raman }
50955bd2133SJag Raman 
51055bd2133SJag Raman static void __exit vcc_exit(void)
51155bd2133SJag Raman {
512*5d171050SJag Raman 	vio_unregister_driver(&vcc_driver);
513*5d171050SJag Raman 	vccdbg("VCC: VIO driver unregistered\n");
514ce808b74SJag Raman 	vcc_tty_exit();
515ce808b74SJag Raman 	vccdbg("VCC: TTY driver unregistered\n");
51655bd2133SJag Raman }
51755bd2133SJag Raman 
51855bd2133SJag Raman module_init(vcc_init);
51955bd2133SJag Raman module_exit(vcc_exit);
520