Linux Audio

Check our new training course

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