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