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