Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / drivers / tty / ehv_bytechan.c
blobf733718bf8e74eba8d206a1206f2d0c10c6596f9
1 /* ePAPR hypervisor byte channel device driver
3 * Copyright 2009-2011 Freescale Semiconductor, Inc.
5 * Author: Timur Tabi <timur@freescale.com>
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.
11 * This driver support three distinct interfaces, all of which are related to
12 * ePAPR hypervisor byte channels.
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.
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.
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.
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>
43 /* The size of the transmit circular buffer. This must be a power of two. */
44 #define BUF_SIZE 2048
46 /* Per-byte channel private data */
47 struct 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;
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 */
59 int tx_irq_enabled; /* true == TX interrupt is enabled */
62 /* Array of byte channel objects */
63 static struct ehv_bc_data *bcs;
65 /* Byte channel handle for stdout (and stdin), taken from device tree */
66 static unsigned int stdout_bc;
68 /* Virtual IRQ for the byte channel handle for stdin, taken from device tree */
69 static unsigned int stdout_irq;
71 /**************************** SUPPORT FUNCTIONS ****************************/
74 * Enable the transmit interrupt
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.
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:
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()
87 * To work around this, we keep a flag to tell us if the IRQ is enabled or not.
89 static void enable_tx_interrupt(struct ehv_bc_data *bc)
91 if (!bc->tx_irq_enabled) {
92 enable_irq(bc->tx_irq);
93 bc->tx_irq_enabled = 1;
97 static void disable_tx_interrupt(struct ehv_bc_data *bc)
99 if (bc->tx_irq_enabled) {
100 disable_irq_nosync(bc->tx_irq);
101 bc->tx_irq_enabled = 0;
106 * find the byte channel handle to use for the console
108 * The byte channel to be used for the console is specified via a "stdout"
109 * property in the /chosen node.
111 * For compatible with legacy device trees, we also look for a "stdout" alias.
113 static int find_console_handle(void)
115 struct device_node *np, *np2;
116 const char *sprop = NULL;
117 const uint32_t *iprop;
119 np = of_find_node_by_path("/chosen");
120 if (np)
121 sprop = of_get_property(np, "stdout-path", NULL);
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);
130 if (!sprop) {
131 of_node_put(np);
132 return 0;
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'.
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;
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;
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;
163 * The 'hv-handle' property contains the handle for this byte channel.
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;
172 stdout_bc = be32_to_cpu(*iprop);
174 of_node_put(np);
175 return 1;
178 /*************************** EARLY CONSOLE DRIVER ***************************/
180 #ifdef CONFIG_PPC_EARLY_DEBUG_EHV_BC
183 * send a byte to a byte channel, wait if necessary
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.
190 static void byte_channel_spin_send(const char data)
192 int ret, count;
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);
202 * The udbg subsystem calls this function to display a single character.
203 * We convert CR to a CR/LF.
205 static void ehv_bc_udbg_putc(char c)
207 if (c == '\n')
208 byte_channel_spin_send('\r');
210 byte_channel_spin_send(c);
214 * early console initialization
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.
221 * We only support displaying of characters (putc). We do not support
222 * keyboard input.
224 void __init udbg_init_ehv_bc(void)
226 unsigned int rx_count, tx_count;
227 unsigned int ret;
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;
235 udbg_putc = ehv_bc_udbg_putc;
236 register_early_udbg_console();
238 udbg_printf("ehv-bc: early console using byte channel handle %u\n",
239 CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE);
242 #endif
244 /****************************** CONSOLE DRIVER ******************************/
246 static struct tty_driver *ehv_bc_driver;
249 * Byte channel console sending worker function.
251 * For consoles, if the output buffer is full, we should just spin until it
252 * clears.
254 static int ehv_bc_console_byte_channel_send(unsigned int handle, const char *s,
255 unsigned int count)
257 unsigned int len;
258 int ret = 0;
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;
269 return ret;
273 * write a string to the console
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.
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.
282 static void ehv_bc_console_write(struct console *co, const char *s,
283 unsigned int count)
285 unsigned int handle = (uintptr_t)co->data;
286 char s2[EV_BYTE_CHANNEL_MAX_BYTES];
287 unsigned int i, j = 0;
288 char c;
290 for (i = 0; i < count; i++) {
291 c = *s++;
293 if (c == '\n')
294 s2[j++] = '\r';
296 s2[j++] = c;
297 if (j >= (EV_BYTE_CHANNEL_MAX_BYTES - 1)) {
298 if (ehv_bc_console_byte_channel_send(handle, s2, j))
299 return;
300 j = 0;
304 if (j)
305 ehv_bc_console_byte_channel_send(handle, s2, j);
309 * When /dev/console is opened, the kernel iterates the console list looking
310 * for one with ->device and then calls that method. On success, it expects
311 * the passed-in int* to contain the minor number to use.
313 static struct tty_driver *ehv_bc_console_device(struct console *co, int *index)
315 *index = co->index;
317 return ehv_bc_driver;
320 static struct console ehv_bc_console = {
321 .name = "ttyEHV",
322 .write = ehv_bc_console_write,
323 .device = ehv_bc_console_device,
324 .flags = CON_PRINTBUFFER | CON_ENABLED,
328 * Console initialization
330 * This is the first function that is called after the device tree is
331 * available, so here is where we determine the byte channel handle and IRQ for
332 * stdout/stdin, even though that information is used by the tty and character
333 * drivers.
335 static int __init ehv_bc_console_init(void)
337 if (!find_console_handle()) {
338 pr_debug("ehv-bc: stdout is not a byte channel\n");
339 return -ENODEV;
342 #ifdef CONFIG_PPC_EARLY_DEBUG_EHV_BC
343 /* Print a friendly warning if the user chose the wrong byte channel
344 * handle for udbg.
346 if (stdout_bc != CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE)
347 pr_warning("ehv-bc: udbg handle %u is not the stdout handle\n",
348 CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE);
349 #endif
351 ehv_bc_console.data = (void *)(uintptr_t)stdout_bc;
353 /* add_preferred_console() must be called before register_console(),
354 otherwise it won't work. However, we don't want to enumerate all the
355 byte channels here, either, since we only care about one. */
357 add_preferred_console(ehv_bc_console.name, ehv_bc_console.index, NULL);
358 register_console(&ehv_bc_console);
360 pr_info("ehv-bc: registered console driver for byte channel %u\n",
361 stdout_bc);
363 return 0;
365 console_initcall(ehv_bc_console_init);
367 /******************************** TTY DRIVER ********************************/
370 * byte channel receive interupt handler
372 * This ISR is called whenever data is available on a byte channel.
374 static irqreturn_t ehv_bc_tty_rx_isr(int irq, void *data)
376 struct ehv_bc_data *bc = data;
377 struct tty_struct *ttys = tty_port_tty_get(&bc->port);
378 unsigned int rx_count, tx_count, len;
379 int count;
380 char buffer[EV_BYTE_CHANNEL_MAX_BYTES];
381 int ret;
383 /* ttys could be NULL during a hangup */
384 if (!ttys)
385 return IRQ_HANDLED;
387 /* Find out how much data needs to be read, and then ask the TTY layer
388 * if it can handle that much. We want to ensure that every byte we
389 * read from the byte channel will be accepted by the TTY layer.
391 ev_byte_channel_poll(bc->handle, &rx_count, &tx_count);
392 count = tty_buffer_request_room(ttys, rx_count);
394 /* 'count' is the maximum amount of data the TTY layer can accept at
395 * this time. However, during testing, I was never able to get 'count'
396 * to be less than 'rx_count'. I'm not sure whether I'm calling it
397 * correctly.
400 while (count > 0) {
401 len = min_t(unsigned int, count, sizeof(buffer));
403 /* Read some data from the byte channel. This function will
404 * never return more than EV_BYTE_CHANNEL_MAX_BYTES bytes.
406 ev_byte_channel_receive(bc->handle, &len, buffer);
408 /* 'len' is now the amount of data that's been received. 'len'
409 * can't be zero, and most likely it's equal to one.
412 /* Pass the received data to the tty layer. */
413 ret = tty_insert_flip_string(ttys, buffer, len);
415 /* 'ret' is the number of bytes that the TTY layer accepted.
416 * If it's not equal to 'len', then it means the buffer is
417 * full, which should never happen. If it does happen, we can
418 * exit gracefully, but we drop the last 'len - ret' characters
419 * that we read from the byte channel.
421 if (ret != len)
422 break;
424 count -= len;
427 /* Tell the tty layer that we're done. */
428 tty_flip_buffer_push(ttys);
430 tty_kref_put(ttys);
432 return IRQ_HANDLED;
436 * dequeue the transmit buffer to the hypervisor
438 * This function, which can be called in interrupt context, dequeues as much
439 * data as possible from the transmit buffer to the byte channel.
441 static void ehv_bc_tx_dequeue(struct ehv_bc_data *bc)
443 unsigned int count;
444 unsigned int len, ret;
445 unsigned long flags;
447 do {
448 spin_lock_irqsave(&bc->lock, flags);
449 len = min_t(unsigned int,
450 CIRC_CNT_TO_END(bc->head, bc->tail, BUF_SIZE),
451 EV_BYTE_CHANNEL_MAX_BYTES);
453 ret = ev_byte_channel_send(bc->handle, &len, bc->buf + bc->tail);
455 /* 'len' is valid only if the return code is 0 or EV_EAGAIN */
456 if (!ret || (ret == EV_EAGAIN))
457 bc->tail = (bc->tail + len) & (BUF_SIZE - 1);
459 count = CIRC_CNT(bc->head, bc->tail, BUF_SIZE);
460 spin_unlock_irqrestore(&bc->lock, flags);
461 } while (count && !ret);
463 spin_lock_irqsave(&bc->lock, flags);
464 if (CIRC_CNT(bc->head, bc->tail, BUF_SIZE))
466 * If we haven't emptied the buffer, then enable the TX IRQ.
467 * We'll get an interrupt when there's more room in the
468 * hypervisor's output buffer.
470 enable_tx_interrupt(bc);
471 else
472 disable_tx_interrupt(bc);
473 spin_unlock_irqrestore(&bc->lock, flags);
477 * byte channel transmit interupt handler
479 * This ISR is called whenever space becomes available for transmitting
480 * characters on a byte channel.
482 static irqreturn_t ehv_bc_tty_tx_isr(int irq, void *data)
484 struct ehv_bc_data *bc = data;
485 struct tty_struct *ttys = tty_port_tty_get(&bc->port);
487 ehv_bc_tx_dequeue(bc);
488 if (ttys) {
489 tty_wakeup(ttys);
490 tty_kref_put(ttys);
493 return IRQ_HANDLED;
497 * This function is called when the tty layer has data for us send. We store
498 * the data first in a circular buffer, and then dequeue as much of that data
499 * as possible.
501 * We don't need to worry about whether there is enough room in the buffer for
502 * all the data. The purpose of ehv_bc_tty_write_room() is to tell the tty
503 * layer how much data it can safely send to us. We guarantee that
504 * ehv_bc_tty_write_room() will never lie, so the tty layer will never send us
505 * too much data.
507 static int ehv_bc_tty_write(struct tty_struct *ttys, const unsigned char *s,
508 int count)
510 struct ehv_bc_data *bc = ttys->driver_data;
511 unsigned long flags;
512 unsigned int len;
513 unsigned int written = 0;
515 while (1) {
516 spin_lock_irqsave(&bc->lock, flags);
517 len = CIRC_SPACE_TO_END(bc->head, bc->tail, BUF_SIZE);
518 if (count < len)
519 len = count;
520 if (len) {
521 memcpy(bc->buf + bc->head, s, len);
522 bc->head = (bc->head + len) & (BUF_SIZE - 1);
524 spin_unlock_irqrestore(&bc->lock, flags);
525 if (!len)
526 break;
528 s += len;
529 count -= len;
530 written += len;
533 ehv_bc_tx_dequeue(bc);
535 return written;
539 * This function can be called multiple times for a given tty_struct, which is
540 * why we initialize bc->ttys in ehv_bc_tty_port_activate() instead.
542 * The tty layer will still call this function even if the device was not
543 * registered (i.e. tty_register_device() was not called). This happens
544 * because tty_register_device() is optional and some legacy drivers don't
545 * use it. So we need to check for that.
547 static int ehv_bc_tty_open(struct tty_struct *ttys, struct file *filp)
549 struct ehv_bc_data *bc = &bcs[ttys->index];
551 if (!bc->dev)
552 return -ENODEV;
554 return tty_port_open(&bc->port, ttys, filp);
558 * Amazingly, if ehv_bc_tty_open() returns an error code, the tty layer will
559 * still call this function to close the tty device. So we can't assume that
560 * the tty port has been initialized.
562 static void ehv_bc_tty_close(struct tty_struct *ttys, struct file *filp)
564 struct ehv_bc_data *bc = &bcs[ttys->index];
566 if (bc->dev)
567 tty_port_close(&bc->port, ttys, filp);
571 * Return the amount of space in the output buffer
573 * This is actually a contract between the driver and the tty layer outlining
574 * how much write room the driver can guarantee will be sent OR BUFFERED. This
575 * driver MUST honor the return value.
577 static int ehv_bc_tty_write_room(struct tty_struct *ttys)
579 struct ehv_bc_data *bc = ttys->driver_data;
580 unsigned long flags;
581 int count;
583 spin_lock_irqsave(&bc->lock, flags);
584 count = CIRC_SPACE(bc->head, bc->tail, BUF_SIZE);
585 spin_unlock_irqrestore(&bc->lock, flags);
587 return count;
591 * Stop sending data to the tty layer
593 * This function is called when the tty layer's input buffers are getting full,
594 * so the driver should stop sending it data. The easiest way to do this is to
595 * disable the RX IRQ, which will prevent ehv_bc_tty_rx_isr() from being
596 * called.
598 * The hypervisor will continue to queue up any incoming data. If there is any
599 * data in the queue when the RX interrupt is enabled, we'll immediately get an
600 * RX interrupt.
602 static void ehv_bc_tty_throttle(struct tty_struct *ttys)
604 struct ehv_bc_data *bc = ttys->driver_data;
606 disable_irq(bc->rx_irq);
610 * Resume sending data to the tty layer
612 * This function is called after previously calling ehv_bc_tty_throttle(). The
613 * tty layer's input buffers now have more room, so the driver can resume
614 * sending it data.
616 static void ehv_bc_tty_unthrottle(struct tty_struct *ttys)
618 struct ehv_bc_data *bc = ttys->driver_data;
620 /* If there is any data in the queue when the RX interrupt is enabled,
621 * we'll immediately get an RX interrupt.
623 enable_irq(bc->rx_irq);
626 static void ehv_bc_tty_hangup(struct tty_struct *ttys)
628 struct ehv_bc_data *bc = ttys->driver_data;
630 ehv_bc_tx_dequeue(bc);
631 tty_port_hangup(&bc->port);
635 * TTY driver operations
637 * If we could ask the hypervisor how much data is still in the TX buffer, or
638 * at least how big the TX buffers are, then we could implement the
639 * .wait_until_sent and .chars_in_buffer functions.
641 static const struct tty_operations ehv_bc_ops = {
642 .open = ehv_bc_tty_open,
643 .close = ehv_bc_tty_close,
644 .write = ehv_bc_tty_write,
645 .write_room = ehv_bc_tty_write_room,
646 .throttle = ehv_bc_tty_throttle,
647 .unthrottle = ehv_bc_tty_unthrottle,
648 .hangup = ehv_bc_tty_hangup,
652 * initialize the TTY port
654 * This function will only be called once, no matter how many times
655 * ehv_bc_tty_open() is called. That's why we register the ISR here, and also
656 * why we initialize tty_struct-related variables here.
658 static int ehv_bc_tty_port_activate(struct tty_port *port,
659 struct tty_struct *ttys)
661 struct ehv_bc_data *bc = container_of(port, struct ehv_bc_data, port);
662 int ret;
664 ttys->driver_data = bc;
666 ret = request_irq(bc->rx_irq, ehv_bc_tty_rx_isr, 0, "ehv-bc", bc);
667 if (ret < 0) {
668 dev_err(bc->dev, "could not request rx irq %u (ret=%i)\n",
669 bc->rx_irq, ret);
670 return ret;
673 /* request_irq also enables the IRQ */
674 bc->tx_irq_enabled = 1;
676 ret = request_irq(bc->tx_irq, ehv_bc_tty_tx_isr, 0, "ehv-bc", bc);
677 if (ret < 0) {
678 dev_err(bc->dev, "could not request tx irq %u (ret=%i)\n",
679 bc->tx_irq, ret);
680 free_irq(bc->rx_irq, bc);
681 return ret;
684 /* The TX IRQ is enabled only when we can't write all the data to the
685 * byte channel at once, so by default it's disabled.
687 disable_tx_interrupt(bc);
689 return 0;
692 static void ehv_bc_tty_port_shutdown(struct tty_port *port)
694 struct ehv_bc_data *bc = container_of(port, struct ehv_bc_data, port);
696 free_irq(bc->tx_irq, bc);
697 free_irq(bc->rx_irq, bc);
700 static const struct tty_port_operations ehv_bc_tty_port_ops = {
701 .activate = ehv_bc_tty_port_activate,
702 .shutdown = ehv_bc_tty_port_shutdown,
705 static int __devinit ehv_bc_tty_probe(struct platform_device *pdev)
707 struct device_node *np = pdev->dev.of_node;
708 struct ehv_bc_data *bc;
709 const uint32_t *iprop;
710 unsigned int handle;
711 int ret;
712 static unsigned int index = 1;
713 unsigned int i;
715 iprop = of_get_property(np, "hv-handle", NULL);
716 if (!iprop) {
717 dev_err(&pdev->dev, "no 'hv-handle' property in %s node\n",
718 np->name);
719 return -ENODEV;
722 /* We already told the console layer that the index for the console
723 * device is zero, so we need to make sure that we use that index when
724 * we probe the console byte channel node.
726 handle = be32_to_cpu(*iprop);
727 i = (handle == stdout_bc) ? 0 : index++;
728 bc = &bcs[i];
730 bc->handle = handle;
731 bc->head = 0;
732 bc->tail = 0;
733 spin_lock_init(&bc->lock);
735 bc->rx_irq = irq_of_parse_and_map(np, 0);
736 bc->tx_irq = irq_of_parse_and_map(np, 1);
737 if ((bc->rx_irq == NO_IRQ) || (bc->tx_irq == NO_IRQ)) {
738 dev_err(&pdev->dev, "no 'interrupts' property in %s node\n",
739 np->name);
740 ret = -ENODEV;
741 goto error;
744 bc->dev = tty_register_device(ehv_bc_driver, i, &pdev->dev);
745 if (IS_ERR(bc->dev)) {
746 ret = PTR_ERR(bc->dev);
747 dev_err(&pdev->dev, "could not register tty (ret=%i)\n", ret);
748 goto error;
751 tty_port_init(&bc->port);
752 bc->port.ops = &ehv_bc_tty_port_ops;
754 dev_set_drvdata(&pdev->dev, bc);
756 dev_info(&pdev->dev, "registered /dev/%s%u for byte channel %u\n",
757 ehv_bc_driver->name, i, bc->handle);
759 return 0;
761 error:
762 irq_dispose_mapping(bc->tx_irq);
763 irq_dispose_mapping(bc->rx_irq);
765 memset(bc, 0, sizeof(struct ehv_bc_data));
766 return ret;
769 static int ehv_bc_tty_remove(struct platform_device *pdev)
771 struct ehv_bc_data *bc = dev_get_drvdata(&pdev->dev);
773 tty_unregister_device(ehv_bc_driver, bc - bcs);
775 irq_dispose_mapping(bc->tx_irq);
776 irq_dispose_mapping(bc->rx_irq);
778 return 0;
781 static const struct of_device_id ehv_bc_tty_of_ids[] = {
782 { .compatible = "epapr,hv-byte-channel" },
786 static struct platform_driver ehv_bc_tty_driver = {
787 .driver = {
788 .owner = THIS_MODULE,
789 .name = "ehv-bc",
790 .of_match_table = ehv_bc_tty_of_ids,
792 .probe = ehv_bc_tty_probe,
793 .remove = ehv_bc_tty_remove,
797 * ehv_bc_init - ePAPR hypervisor byte channel driver initialization
799 * This function is called when this module is loaded.
801 static int __init ehv_bc_init(void)
803 struct device_node *np;
804 unsigned int count = 0; /* Number of elements in bcs[] */
805 int ret;
807 pr_info("ePAPR hypervisor byte channel driver\n");
809 /* Count the number of byte channels */
810 for_each_compatible_node(np, NULL, "epapr,hv-byte-channel")
811 count++;
813 if (!count)
814 return -ENODEV;
816 /* The array index of an element in bcs[] is the same as the tty index
817 * for that element. If you know the address of an element in the
818 * array, then you can use pointer math (e.g. "bc - bcs") to get its
819 * tty index.
821 bcs = kzalloc(count * sizeof(struct ehv_bc_data), GFP_KERNEL);
822 if (!bcs)
823 return -ENOMEM;
825 ehv_bc_driver = alloc_tty_driver(count);
826 if (!ehv_bc_driver) {
827 ret = -ENOMEM;
828 goto error;
831 ehv_bc_driver->owner = THIS_MODULE;
832 ehv_bc_driver->driver_name = "ehv-bc";
833 ehv_bc_driver->name = ehv_bc_console.name;
834 ehv_bc_driver->type = TTY_DRIVER_TYPE_CONSOLE;
835 ehv_bc_driver->subtype = SYSTEM_TYPE_CONSOLE;
836 ehv_bc_driver->init_termios = tty_std_termios;
837 ehv_bc_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
838 tty_set_operations(ehv_bc_driver, &ehv_bc_ops);
840 ret = tty_register_driver(ehv_bc_driver);
841 if (ret) {
842 pr_err("ehv-bc: could not register tty driver (ret=%i)\n", ret);
843 goto error;
846 ret = platform_driver_register(&ehv_bc_tty_driver);
847 if (ret) {
848 pr_err("ehv-bc: could not register platform driver (ret=%i)\n",
849 ret);
850 goto error;
853 return 0;
855 error:
856 if (ehv_bc_driver) {
857 tty_unregister_driver(ehv_bc_driver);
858 put_tty_driver(ehv_bc_driver);
861 kfree(bcs);
863 return ret;
868 * ehv_bc_exit - ePAPR hypervisor byte channel driver termination
870 * This function is called when this driver is unloaded.
872 static void __exit ehv_bc_exit(void)
874 tty_unregister_driver(ehv_bc_driver);
875 put_tty_driver(ehv_bc_driver);
876 kfree(bcs);
879 module_init(ehv_bc_init);
880 module_exit(ehv_bc_exit);
882 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
883 MODULE_DESCRIPTION("ePAPR hypervisor byte channel driver");
884 MODULE_LICENSE("GPL v2");