xref: /openbmc/linux/drivers/tty/ehv_bytechan.c (revision fb8d6c8d)
1 // SPDX-License-Identifier: GPL-2.0
2 /* ePAPR hypervisor byte channel device driver
3  *
4  * Copyright 2009-2011 Freescale Semiconductor, Inc.
5  *
6  * Author: Timur Tabi <timur@freescale.com>
7  *
8  * This driver support three distinct interfaces, all of which are related to
9  * ePAPR hypervisor byte channels.
10  *
11  * 1) An early-console (udbg) driver.  This provides early console output
12  * through a byte channel.  The byte channel handle must be specified in a
13  * Kconfig option.
14  *
15  * 2) A normal console driver.  Output is sent to the byte channel designated
16  * for stdout in the device tree.  The console driver is for handling kernel
17  * printk calls.
18  *
19  * 3) A tty driver, which is used to handle user-space input and output.  The
20  * byte channel used for the console is designated as the default tty.
21  */
22 
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/err.h>
26 #include <linux/interrupt.h>
27 #include <linux/fs.h>
28 #include <linux/poll.h>
29 #include <asm/epapr_hcalls.h>
30 #include <linux/of.h>
31 #include <linux/of_irq.h>
32 #include <linux/platform_device.h>
33 #include <linux/cdev.h>
34 #include <linux/console.h>
35 #include <linux/tty.h>
36 #include <linux/tty_flip.h>
37 #include <linux/circ_buf.h>
38 #include <asm/udbg.h>
39 
40 /* The size of the transmit circular buffer.  This must be a power of two. */
41 #define BUF_SIZE	2048
42 
43 /* Per-byte channel private data */
44 struct ehv_bc_data {
45 	struct device *dev;
46 	struct tty_port port;
47 	uint32_t handle;
48 	unsigned int rx_irq;
49 	unsigned int tx_irq;
50 
51 	spinlock_t lock;	/* lock for transmit buffer */
52 	unsigned char buf[BUF_SIZE];	/* transmit circular buffer */
53 	unsigned int head;	/* circular buffer head */
54 	unsigned int tail;	/* circular buffer tail */
55 
56 	int tx_irq_enabled;	/* true == TX interrupt is enabled */
57 };
58 
59 /* Array of byte channel objects */
60 static struct ehv_bc_data *bcs;
61 
62 /* Byte channel handle for stdout (and stdin), taken from device tree */
63 static unsigned int stdout_bc;
64 
65 /* Virtual IRQ for the byte channel handle for stdin, taken from device tree */
66 static unsigned int stdout_irq;
67 
68 /**************************** SUPPORT FUNCTIONS ****************************/
69 
70 /*
71  * Enable the transmit interrupt
72  *
73  * Unlike a serial device, byte channels have no mechanism for disabling their
74  * own receive or transmit interrupts.  To emulate that feature, we toggle
75  * the IRQ in the kernel.
76  *
77  * We cannot just blindly call enable_irq() or disable_irq(), because these
78  * calls are reference counted.  This means that we cannot call enable_irq()
79  * if interrupts are already enabled.  This can happen in two situations:
80  *
81  * 1. The tty layer makes two back-to-back calls to ehv_bc_tty_write()
82  * 2. A transmit interrupt occurs while executing ehv_bc_tx_dequeue()
83  *
84  * To work around this, we keep a flag to tell us if the IRQ is enabled or not.
85  */
86 static void enable_tx_interrupt(struct ehv_bc_data *bc)
87 {
88 	if (!bc->tx_irq_enabled) {
89 		enable_irq(bc->tx_irq);
90 		bc->tx_irq_enabled = 1;
91 	}
92 }
93 
94 static void disable_tx_interrupt(struct ehv_bc_data *bc)
95 {
96 	if (bc->tx_irq_enabled) {
97 		disable_irq_nosync(bc->tx_irq);
98 		bc->tx_irq_enabled = 0;
99 	}
100 }
101 
102 /*
103  * find the byte channel handle to use for the console
104  *
105  * The byte channel to be used for the console is specified via a "stdout"
106  * property in the /chosen node.
107  */
108 static int find_console_handle(void)
109 {
110 	struct device_node *np = of_stdout;
111 	const uint32_t *iprop;
112 
113 	/* We don't care what the aliased node is actually called.  We only
114 	 * care if it's compatible with "epapr,hv-byte-channel", because that
115 	 * indicates that it's a byte channel node.
116 	 */
117 	if (!np || !of_device_is_compatible(np, "epapr,hv-byte-channel"))
118 		return 0;
119 
120 	stdout_irq = irq_of_parse_and_map(np, 0);
121 	if (stdout_irq == NO_IRQ) {
122 		pr_err("ehv-bc: no 'interrupts' property in %pOF node\n", np);
123 		return 0;
124 	}
125 
126 	/*
127 	 * The 'hv-handle' property contains the handle for this byte channel.
128 	 */
129 	iprop = of_get_property(np, "hv-handle", NULL);
130 	if (!iprop) {
131 		pr_err("ehv-bc: no 'hv-handle' property in %pOFn node\n",
132 		       np);
133 		return 0;
134 	}
135 	stdout_bc = be32_to_cpu(*iprop);
136 	return 1;
137 }
138 
139 /*************************** EARLY CONSOLE DRIVER ***************************/
140 
141 #ifdef CONFIG_PPC_EARLY_DEBUG_EHV_BC
142 
143 /*
144  * send a byte to a byte channel, wait if necessary
145  *
146  * This function sends a byte to a byte channel, and it waits and
147  * retries if the byte channel is full.  It returns if the character
148  * has been sent, or if some error has occurred.
149  *
150  */
151 static void byte_channel_spin_send(const char data)
152 {
153 	int ret, count;
154 
155 	do {
156 		count = 1;
157 		ret = ev_byte_channel_send(CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE,
158 					   &count, &data);
159 	} while (ret == EV_EAGAIN);
160 }
161 
162 /*
163  * The udbg subsystem calls this function to display a single character.
164  * We convert CR to a CR/LF.
165  */
166 static void ehv_bc_udbg_putc(char c)
167 {
168 	if (c == '\n')
169 		byte_channel_spin_send('\r');
170 
171 	byte_channel_spin_send(c);
172 }
173 
174 /*
175  * early console initialization
176  *
177  * PowerPC kernels support an early printk console, also known as udbg.
178  * This function must be called via the ppc_md.init_early function pointer.
179  * At this point, the device tree has been unflattened, so we can obtain the
180  * byte channel handle for stdout.
181  *
182  * We only support displaying of characters (putc).  We do not support
183  * keyboard input.
184  */
185 void __init udbg_init_ehv_bc(void)
186 {
187 	unsigned int rx_count, tx_count;
188 	unsigned int ret;
189 
190 	/* Verify the byte channel handle */
191 	ret = ev_byte_channel_poll(CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE,
192 				   &rx_count, &tx_count);
193 	if (ret)
194 		return;
195 
196 	udbg_putc = ehv_bc_udbg_putc;
197 	register_early_udbg_console();
198 
199 	udbg_printf("ehv-bc: early console using byte channel handle %u\n",
200 		    CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE);
201 }
202 
203 #endif
204 
205 /****************************** CONSOLE DRIVER ******************************/
206 
207 static struct tty_driver *ehv_bc_driver;
208 
209 /*
210  * Byte channel console sending worker function.
211  *
212  * For consoles, if the output buffer is full, we should just spin until it
213  * clears.
214  */
215 static int ehv_bc_console_byte_channel_send(unsigned int handle, const char *s,
216 			     unsigned int count)
217 {
218 	unsigned int len;
219 	int ret = 0;
220 
221 	while (count) {
222 		len = min_t(unsigned int, count, EV_BYTE_CHANNEL_MAX_BYTES);
223 		do {
224 			ret = ev_byte_channel_send(handle, &len, s);
225 		} while (ret == EV_EAGAIN);
226 		count -= len;
227 		s += len;
228 	}
229 
230 	return ret;
231 }
232 
233 /*
234  * write a string to the console
235  *
236  * This function gets called to write a string from the kernel, typically from
237  * a printk().  This function spins until all data is written.
238  *
239  * We copy the data to a temporary buffer because we need to insert a \r in
240  * front of every \n.  It's more efficient to copy the data to the buffer than
241  * it is to make multiple hcalls for each character or each newline.
242  */
243 static void ehv_bc_console_write(struct console *co, const char *s,
244 				 unsigned int count)
245 {
246 	char s2[EV_BYTE_CHANNEL_MAX_BYTES];
247 	unsigned int i, j = 0;
248 	char c;
249 
250 	for (i = 0; i < count; i++) {
251 		c = *s++;
252 
253 		if (c == '\n')
254 			s2[j++] = '\r';
255 
256 		s2[j++] = c;
257 		if (j >= (EV_BYTE_CHANNEL_MAX_BYTES - 1)) {
258 			if (ehv_bc_console_byte_channel_send(stdout_bc, s2, j))
259 				return;
260 			j = 0;
261 		}
262 	}
263 
264 	if (j)
265 		ehv_bc_console_byte_channel_send(stdout_bc, s2, j);
266 }
267 
268 /*
269  * When /dev/console is opened, the kernel iterates the console list looking
270  * for one with ->device and then calls that method. On success, it expects
271  * the passed-in int* to contain the minor number to use.
272  */
273 static struct tty_driver *ehv_bc_console_device(struct console *co, int *index)
274 {
275 	*index = co->index;
276 
277 	return ehv_bc_driver;
278 }
279 
280 static struct console ehv_bc_console = {
281 	.name		= "ttyEHV",
282 	.write		= ehv_bc_console_write,
283 	.device		= ehv_bc_console_device,
284 	.flags		= CON_PRINTBUFFER | CON_ENABLED,
285 };
286 
287 /*
288  * Console initialization
289  *
290  * This is the first function that is called after the device tree is
291  * available, so here is where we determine the byte channel handle and IRQ for
292  * stdout/stdin, even though that information is used by the tty and character
293  * drivers.
294  */
295 static int __init ehv_bc_console_init(void)
296 {
297 	if (!find_console_handle()) {
298 		pr_debug("ehv-bc: stdout is not a byte channel\n");
299 		return -ENODEV;
300 	}
301 
302 #ifdef CONFIG_PPC_EARLY_DEBUG_EHV_BC
303 	/* Print a friendly warning if the user chose the wrong byte channel
304 	 * handle for udbg.
305 	 */
306 	if (stdout_bc != CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE)
307 		pr_warn("ehv-bc: udbg handle %u is not the stdout handle\n",
308 			CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE);
309 #endif
310 
311 	/* add_preferred_console() must be called before register_console(),
312 	   otherwise it won't work.  However, we don't want to enumerate all the
313 	   byte channels here, either, since we only care about one. */
314 
315 	add_preferred_console(ehv_bc_console.name, ehv_bc_console.index, NULL);
316 	register_console(&ehv_bc_console);
317 
318 	pr_info("ehv-bc: registered console driver for byte channel %u\n",
319 		stdout_bc);
320 
321 	return 0;
322 }
323 console_initcall(ehv_bc_console_init);
324 
325 /******************************** TTY DRIVER ********************************/
326 
327 /*
328  * byte channel receive interrupt handler
329  *
330  * This ISR is called whenever data is available on a byte channel.
331  */
332 static irqreturn_t ehv_bc_tty_rx_isr(int irq, void *data)
333 {
334 	struct ehv_bc_data *bc = data;
335 	unsigned int rx_count, tx_count, len;
336 	int count;
337 	char buffer[EV_BYTE_CHANNEL_MAX_BYTES];
338 	int ret;
339 
340 	/* Find out how much data needs to be read, and then ask the TTY layer
341 	 * if it can handle that much.  We want to ensure that every byte we
342 	 * read from the byte channel will be accepted by the TTY layer.
343 	 */
344 	ev_byte_channel_poll(bc->handle, &rx_count, &tx_count);
345 	count = tty_buffer_request_room(&bc->port, rx_count);
346 
347 	/* 'count' is the maximum amount of data the TTY layer can accept at
348 	 * this time.  However, during testing, I was never able to get 'count'
349 	 * to be less than 'rx_count'.  I'm not sure whether I'm calling it
350 	 * correctly.
351 	 */
352 
353 	while (count > 0) {
354 		len = min_t(unsigned int, count, sizeof(buffer));
355 
356 		/* Read some data from the byte channel.  This function will
357 		 * never return more than EV_BYTE_CHANNEL_MAX_BYTES bytes.
358 		 */
359 		ev_byte_channel_receive(bc->handle, &len, buffer);
360 
361 		/* 'len' is now the amount of data that's been received. 'len'
362 		 * can't be zero, and most likely it's equal to one.
363 		 */
364 
365 		/* Pass the received data to the tty layer. */
366 		ret = tty_insert_flip_string(&bc->port, buffer, len);
367 
368 		/* 'ret' is the number of bytes that the TTY layer accepted.
369 		 * If it's not equal to 'len', then it means the buffer is
370 		 * full, which should never happen.  If it does happen, we can
371 		 * exit gracefully, but we drop the last 'len - ret' characters
372 		 * that we read from the byte channel.
373 		 */
374 		if (ret != len)
375 			break;
376 
377 		count -= len;
378 	}
379 
380 	/* Tell the tty layer that we're done. */
381 	tty_flip_buffer_push(&bc->port);
382 
383 	return IRQ_HANDLED;
384 }
385 
386 /*
387  * dequeue the transmit buffer to the hypervisor
388  *
389  * This function, which can be called in interrupt context, dequeues as much
390  * data as possible from the transmit buffer to the byte channel.
391  */
392 static void ehv_bc_tx_dequeue(struct ehv_bc_data *bc)
393 {
394 	unsigned int count;
395 	unsigned int len, ret;
396 	unsigned long flags;
397 
398 	do {
399 		spin_lock_irqsave(&bc->lock, flags);
400 		len = min_t(unsigned int,
401 			    CIRC_CNT_TO_END(bc->head, bc->tail, BUF_SIZE),
402 			    EV_BYTE_CHANNEL_MAX_BYTES);
403 
404 		ret = ev_byte_channel_send(bc->handle, &len, bc->buf + bc->tail);
405 
406 		/* 'len' is valid only if the return code is 0 or EV_EAGAIN */
407 		if (!ret || (ret == EV_EAGAIN))
408 			bc->tail = (bc->tail + len) & (BUF_SIZE - 1);
409 
410 		count = CIRC_CNT(bc->head, bc->tail, BUF_SIZE);
411 		spin_unlock_irqrestore(&bc->lock, flags);
412 	} while (count && !ret);
413 
414 	spin_lock_irqsave(&bc->lock, flags);
415 	if (CIRC_CNT(bc->head, bc->tail, BUF_SIZE))
416 		/*
417 		 * If we haven't emptied the buffer, then enable the TX IRQ.
418 		 * We'll get an interrupt when there's more room in the
419 		 * hypervisor's output buffer.
420 		 */
421 		enable_tx_interrupt(bc);
422 	else
423 		disable_tx_interrupt(bc);
424 	spin_unlock_irqrestore(&bc->lock, flags);
425 }
426 
427 /*
428  * byte channel transmit interrupt handler
429  *
430  * This ISR is called whenever space becomes available for transmitting
431  * characters on a byte channel.
432  */
433 static irqreturn_t ehv_bc_tty_tx_isr(int irq, void *data)
434 {
435 	struct ehv_bc_data *bc = data;
436 
437 	ehv_bc_tx_dequeue(bc);
438 	tty_port_tty_wakeup(&bc->port);
439 
440 	return IRQ_HANDLED;
441 }
442 
443 /*
444  * This function is called when the tty layer has data for us send.  We store
445  * the data first in a circular buffer, and then dequeue as much of that data
446  * as possible.
447  *
448  * We don't need to worry about whether there is enough room in the buffer for
449  * all the data.  The purpose of ehv_bc_tty_write_room() is to tell the tty
450  * layer how much data it can safely send to us.  We guarantee that
451  * ehv_bc_tty_write_room() will never lie, so the tty layer will never send us
452  * too much data.
453  */
454 static int ehv_bc_tty_write(struct tty_struct *ttys, const unsigned char *s,
455 			    int count)
456 {
457 	struct ehv_bc_data *bc = ttys->driver_data;
458 	unsigned long flags;
459 	unsigned int len;
460 	unsigned int written = 0;
461 
462 	while (1) {
463 		spin_lock_irqsave(&bc->lock, flags);
464 		len = CIRC_SPACE_TO_END(bc->head, bc->tail, BUF_SIZE);
465 		if (count < len)
466 			len = count;
467 		if (len) {
468 			memcpy(bc->buf + bc->head, s, len);
469 			bc->head = (bc->head + len) & (BUF_SIZE - 1);
470 		}
471 		spin_unlock_irqrestore(&bc->lock, flags);
472 		if (!len)
473 			break;
474 
475 		s += len;
476 		count -= len;
477 		written += len;
478 	}
479 
480 	ehv_bc_tx_dequeue(bc);
481 
482 	return written;
483 }
484 
485 /*
486  * This function can be called multiple times for a given tty_struct, which is
487  * why we initialize bc->ttys in ehv_bc_tty_port_activate() instead.
488  *
489  * The tty layer will still call this function even if the device was not
490  * registered (i.e. tty_register_device() was not called).  This happens
491  * because tty_register_device() is optional and some legacy drivers don't
492  * use it.  So we need to check for that.
493  */
494 static int ehv_bc_tty_open(struct tty_struct *ttys, struct file *filp)
495 {
496 	struct ehv_bc_data *bc = &bcs[ttys->index];
497 
498 	if (!bc->dev)
499 		return -ENODEV;
500 
501 	return tty_port_open(&bc->port, ttys, filp);
502 }
503 
504 /*
505  * Amazingly, if ehv_bc_tty_open() returns an error code, the tty layer will
506  * still call this function to close the tty device.  So we can't assume that
507  * the tty port has been initialized.
508  */
509 static void ehv_bc_tty_close(struct tty_struct *ttys, struct file *filp)
510 {
511 	struct ehv_bc_data *bc = &bcs[ttys->index];
512 
513 	if (bc->dev)
514 		tty_port_close(&bc->port, ttys, filp);
515 }
516 
517 /*
518  * Return the amount of space in the output buffer
519  *
520  * This is actually a contract between the driver and the tty layer outlining
521  * how much write room the driver can guarantee will be sent OR BUFFERED.  This
522  * driver MUST honor the return value.
523  */
524 static int ehv_bc_tty_write_room(struct tty_struct *ttys)
525 {
526 	struct ehv_bc_data *bc = ttys->driver_data;
527 	unsigned long flags;
528 	int count;
529 
530 	spin_lock_irqsave(&bc->lock, flags);
531 	count = CIRC_SPACE(bc->head, bc->tail, BUF_SIZE);
532 	spin_unlock_irqrestore(&bc->lock, flags);
533 
534 	return count;
535 }
536 
537 /*
538  * Stop sending data to the tty layer
539  *
540  * This function is called when the tty layer's input buffers are getting full,
541  * so the driver should stop sending it data.  The easiest way to do this is to
542  * disable the RX IRQ, which will prevent ehv_bc_tty_rx_isr() from being
543  * called.
544  *
545  * The hypervisor will continue to queue up any incoming data.  If there is any
546  * data in the queue when the RX interrupt is enabled, we'll immediately get an
547  * RX interrupt.
548  */
549 static void ehv_bc_tty_throttle(struct tty_struct *ttys)
550 {
551 	struct ehv_bc_data *bc = ttys->driver_data;
552 
553 	disable_irq(bc->rx_irq);
554 }
555 
556 /*
557  * Resume sending data to the tty layer
558  *
559  * This function is called after previously calling ehv_bc_tty_throttle().  The
560  * tty layer's input buffers now have more room, so the driver can resume
561  * sending it data.
562  */
563 static void ehv_bc_tty_unthrottle(struct tty_struct *ttys)
564 {
565 	struct ehv_bc_data *bc = ttys->driver_data;
566 
567 	/* If there is any data in the queue when the RX interrupt is enabled,
568 	 * we'll immediately get an RX interrupt.
569 	 */
570 	enable_irq(bc->rx_irq);
571 }
572 
573 static void ehv_bc_tty_hangup(struct tty_struct *ttys)
574 {
575 	struct ehv_bc_data *bc = ttys->driver_data;
576 
577 	ehv_bc_tx_dequeue(bc);
578 	tty_port_hangup(&bc->port);
579 }
580 
581 /*
582  * TTY driver operations
583  *
584  * If we could ask the hypervisor how much data is still in the TX buffer, or
585  * at least how big the TX buffers are, then we could implement the
586  * .wait_until_sent and .chars_in_buffer functions.
587  */
588 static const struct tty_operations ehv_bc_ops = {
589 	.open		= ehv_bc_tty_open,
590 	.close		= ehv_bc_tty_close,
591 	.write		= ehv_bc_tty_write,
592 	.write_room	= ehv_bc_tty_write_room,
593 	.throttle	= ehv_bc_tty_throttle,
594 	.unthrottle	= ehv_bc_tty_unthrottle,
595 	.hangup		= ehv_bc_tty_hangup,
596 };
597 
598 /*
599  * initialize the TTY port
600  *
601  * This function will only be called once, no matter how many times
602  * ehv_bc_tty_open() is called.  That's why we register the ISR here, and also
603  * why we initialize tty_struct-related variables here.
604  */
605 static int ehv_bc_tty_port_activate(struct tty_port *port,
606 				    struct tty_struct *ttys)
607 {
608 	struct ehv_bc_data *bc = container_of(port, struct ehv_bc_data, port);
609 	int ret;
610 
611 	ttys->driver_data = bc;
612 
613 	ret = request_irq(bc->rx_irq, ehv_bc_tty_rx_isr, 0, "ehv-bc", bc);
614 	if (ret < 0) {
615 		dev_err(bc->dev, "could not request rx irq %u (ret=%i)\n",
616 		       bc->rx_irq, ret);
617 		return ret;
618 	}
619 
620 	/* request_irq also enables the IRQ */
621 	bc->tx_irq_enabled = 1;
622 
623 	ret = request_irq(bc->tx_irq, ehv_bc_tty_tx_isr, 0, "ehv-bc", bc);
624 	if (ret < 0) {
625 		dev_err(bc->dev, "could not request tx irq %u (ret=%i)\n",
626 		       bc->tx_irq, ret);
627 		free_irq(bc->rx_irq, bc);
628 		return ret;
629 	}
630 
631 	/* The TX IRQ is enabled only when we can't write all the data to the
632 	 * byte channel at once, so by default it's disabled.
633 	 */
634 	disable_tx_interrupt(bc);
635 
636 	return 0;
637 }
638 
639 static void ehv_bc_tty_port_shutdown(struct tty_port *port)
640 {
641 	struct ehv_bc_data *bc = container_of(port, struct ehv_bc_data, port);
642 
643 	free_irq(bc->tx_irq, bc);
644 	free_irq(bc->rx_irq, bc);
645 }
646 
647 static const struct tty_port_operations ehv_bc_tty_port_ops = {
648 	.activate = ehv_bc_tty_port_activate,
649 	.shutdown = ehv_bc_tty_port_shutdown,
650 };
651 
652 static int ehv_bc_tty_probe(struct platform_device *pdev)
653 {
654 	struct device_node *np = pdev->dev.of_node;
655 	struct ehv_bc_data *bc;
656 	const uint32_t *iprop;
657 	unsigned int handle;
658 	int ret;
659 	static unsigned int index = 1;
660 	unsigned int i;
661 
662 	iprop = of_get_property(np, "hv-handle", NULL);
663 	if (!iprop) {
664 		dev_err(&pdev->dev, "no 'hv-handle' property in %pOFn node\n",
665 			np);
666 		return -ENODEV;
667 	}
668 
669 	/* We already told the console layer that the index for the console
670 	 * device is zero, so we need to make sure that we use that index when
671 	 * we probe the console byte channel node.
672 	 */
673 	handle = be32_to_cpu(*iprop);
674 	i = (handle == stdout_bc) ? 0 : index++;
675 	bc = &bcs[i];
676 
677 	bc->handle = handle;
678 	bc->head = 0;
679 	bc->tail = 0;
680 	spin_lock_init(&bc->lock);
681 
682 	bc->rx_irq = irq_of_parse_and_map(np, 0);
683 	bc->tx_irq = irq_of_parse_and_map(np, 1);
684 	if ((bc->rx_irq == NO_IRQ) || (bc->tx_irq == NO_IRQ)) {
685 		dev_err(&pdev->dev, "no 'interrupts' property in %pOFn node\n",
686 			np);
687 		ret = -ENODEV;
688 		goto error;
689 	}
690 
691 	tty_port_init(&bc->port);
692 	bc->port.ops = &ehv_bc_tty_port_ops;
693 
694 	bc->dev = tty_port_register_device(&bc->port, ehv_bc_driver, i,
695 			&pdev->dev);
696 	if (IS_ERR(bc->dev)) {
697 		ret = PTR_ERR(bc->dev);
698 		dev_err(&pdev->dev, "could not register tty (ret=%i)\n", ret);
699 		goto error;
700 	}
701 
702 	dev_set_drvdata(&pdev->dev, bc);
703 
704 	dev_info(&pdev->dev, "registered /dev/%s%u for byte channel %u\n",
705 		ehv_bc_driver->name, i, bc->handle);
706 
707 	return 0;
708 
709 error:
710 	tty_port_destroy(&bc->port);
711 	irq_dispose_mapping(bc->tx_irq);
712 	irq_dispose_mapping(bc->rx_irq);
713 
714 	memset(bc, 0, sizeof(struct ehv_bc_data));
715 	return ret;
716 }
717 
718 static const struct of_device_id ehv_bc_tty_of_ids[] = {
719 	{ .compatible = "epapr,hv-byte-channel" },
720 	{}
721 };
722 
723 static struct platform_driver ehv_bc_tty_driver = {
724 	.driver = {
725 		.name = "ehv-bc",
726 		.of_match_table = ehv_bc_tty_of_ids,
727 		.suppress_bind_attrs = true,
728 	},
729 	.probe		= ehv_bc_tty_probe,
730 };
731 
732 /**
733  * ehv_bc_init - ePAPR hypervisor byte channel driver initialization
734  *
735  * This function is called when this driver is loaded.
736  */
737 static int __init ehv_bc_init(void)
738 {
739 	struct device_node *np;
740 	unsigned int count = 0; /* Number of elements in bcs[] */
741 	int ret;
742 
743 	pr_info("ePAPR hypervisor byte channel driver\n");
744 
745 	/* Count the number of byte channels */
746 	for_each_compatible_node(np, NULL, "epapr,hv-byte-channel")
747 		count++;
748 
749 	if (!count)
750 		return -ENODEV;
751 
752 	/* The array index of an element in bcs[] is the same as the tty index
753 	 * for that element.  If you know the address of an element in the
754 	 * array, then you can use pointer math (e.g. "bc - bcs") to get its
755 	 * tty index.
756 	 */
757 	bcs = kcalloc(count, sizeof(struct ehv_bc_data), GFP_KERNEL);
758 	if (!bcs)
759 		return -ENOMEM;
760 
761 	ehv_bc_driver = alloc_tty_driver(count);
762 	if (!ehv_bc_driver) {
763 		ret = -ENOMEM;
764 		goto err_free_bcs;
765 	}
766 
767 	ehv_bc_driver->driver_name = "ehv-bc";
768 	ehv_bc_driver->name = ehv_bc_console.name;
769 	ehv_bc_driver->type = TTY_DRIVER_TYPE_CONSOLE;
770 	ehv_bc_driver->subtype = SYSTEM_TYPE_CONSOLE;
771 	ehv_bc_driver->init_termios = tty_std_termios;
772 	ehv_bc_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
773 	tty_set_operations(ehv_bc_driver, &ehv_bc_ops);
774 
775 	ret = tty_register_driver(ehv_bc_driver);
776 	if (ret) {
777 		pr_err("ehv-bc: could not register tty driver (ret=%i)\n", ret);
778 		goto err_put_tty_driver;
779 	}
780 
781 	ret = platform_driver_register(&ehv_bc_tty_driver);
782 	if (ret) {
783 		pr_err("ehv-bc: could not register platform driver (ret=%i)\n",
784 		       ret);
785 		goto err_deregister_tty_driver;
786 	}
787 
788 	return 0;
789 
790 err_deregister_tty_driver:
791 	tty_unregister_driver(ehv_bc_driver);
792 err_put_tty_driver:
793 	put_tty_driver(ehv_bc_driver);
794 err_free_bcs:
795 	kfree(bcs);
796 
797 	return ret;
798 }
799 device_initcall(ehv_bc_init);
800