1
2
3
4
5
6#include <linux/delay.h>
7#include <linux/interrupt.h>
8#include <linux/module.h>
9#include <linux/slab.h>
10#include <linux/sysfs.h>
11#include <linux/tty.h>
12#include <linux/tty_flip.h>
13#include <asm/vio.h>
14#include <asm/ldc.h>
15
16#define DRV_MODULE_NAME "vcc"
17#define DRV_MODULE_VERSION "1.1"
18#define DRV_MODULE_RELDATE "July 1, 2017"
19
20static char version[] =
21 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
22
23MODULE_DESCRIPTION("Sun LDOM virtual console concentrator driver");
24MODULE_LICENSE("GPL");
25MODULE_VERSION(DRV_MODULE_VERSION);
26
27struct vcc_port {
28 struct vio_driver_state vio;
29
30 spinlock_t lock;
31 char *domain;
32 struct tty_struct *tty;
33 unsigned long index;
34
35 u64 refcnt;
36 bool excl_locked;
37
38 bool removed;
39
40
41
42
43
44 int chars_in_buffer;
45 struct vio_vcc buffer;
46
47 struct timer_list rx_timer;
48 struct timer_list tx_timer;
49};
50
51
52#define VCC_REF_DELAY 100
53
54#define VCC_MAX_PORTS 1024
55#define VCC_MINOR_START 0
56#define VCC_BUFF_LEN VIO_VCC_MTU_SIZE
57
58#define VCC_CTL_BREAK -1
59#define VCC_CTL_HUP -2
60
61static const char vcc_driver_name[] = "vcc";
62static const char vcc_device_node[] = "vcc";
63static struct tty_driver *vcc_tty_driver;
64
65static struct vcc_port *vcc_table[VCC_MAX_PORTS];
66static DEFINE_SPINLOCK(vcc_table_lock);
67
68int vcc_dbg;
69int vcc_dbg_ldc;
70int vcc_dbg_vio;
71
72module_param(vcc_dbg, uint, 0664);
73module_param(vcc_dbg_ldc, uint, 0664);
74module_param(vcc_dbg_vio, uint, 0664);
75
76#define VCC_DBG_DRV 0x1
77#define VCC_DBG_LDC 0x2
78#define VCC_DBG_PKT 0x4
79
80#define vccdbg(f, a...) \
81 do { \
82 if (vcc_dbg & VCC_DBG_DRV) \
83 pr_info(f, ## a); \
84 } while (0) \
85
86#define vccdbgl(l) \
87 do { \
88 if (vcc_dbg & VCC_DBG_LDC) \
89 ldc_print(l); \
90 } while (0) \
91
92#define vccdbgp(pkt) \
93 do { \
94 if (vcc_dbg & VCC_DBG_PKT) { \
95 int i; \
96 for (i = 0; i < pkt.tag.stype; i++) \
97 pr_info("[%c]", pkt.data[i]); \
98 } \
99 } while (0) \
100
101
102
103
104
105static const struct ktermios vcc_tty_termios = {
106 .c_iflag = IGNBRK | IGNPAR,
107 .c_oflag = OPOST,
108 .c_cflag = B38400 | CS8 | CREAD | HUPCL,
109 .c_cc = INIT_C_CC,
110 .c_ispeed = 38400,
111 .c_ospeed = 38400
112};
113
114
115
116
117
118
119
120
121static int vcc_table_add(struct vcc_port *port)
122{
123 unsigned long flags;
124 int i;
125
126 spin_lock_irqsave(&vcc_table_lock, flags);
127 for (i = VCC_MINOR_START; i < VCC_MAX_PORTS; i++) {
128 if (!vcc_table[i]) {
129 vcc_table[i] = port;
130 break;
131 }
132 }
133 spin_unlock_irqrestore(&vcc_table_lock, flags);
134
135 if (i < VCC_MAX_PORTS)
136 return i;
137 else
138 return -1;
139}
140
141
142
143
144
145static void vcc_table_remove(unsigned long index)
146{
147 unsigned long flags;
148
149 if (WARN_ON(index >= VCC_MAX_PORTS))
150 return;
151
152 spin_lock_irqsave(&vcc_table_lock, flags);
153 vcc_table[index] = NULL;
154 spin_unlock_irqrestore(&vcc_table_lock, flags);
155}
156
157
158
159
160
161
162
163
164
165static struct vcc_port *vcc_get(unsigned long index, bool excl)
166{
167 struct vcc_port *port;
168 unsigned long flags;
169
170try_again:
171 spin_lock_irqsave(&vcc_table_lock, flags);
172
173 port = vcc_table[index];
174 if (!port) {
175 spin_unlock_irqrestore(&vcc_table_lock, flags);
176 return NULL;
177 }
178
179 if (!excl) {
180 if (port->excl_locked) {
181 spin_unlock_irqrestore(&vcc_table_lock, flags);
182 udelay(VCC_REF_DELAY);
183 goto try_again;
184 }
185 port->refcnt++;
186 spin_unlock_irqrestore(&vcc_table_lock, flags);
187 return port;
188 }
189
190 if (port->refcnt) {
191 spin_unlock_irqrestore(&vcc_table_lock, flags);
192
193
194
195
196 udelay(VCC_REF_DELAY/2);
197 goto try_again;
198 }
199
200 port->refcnt++;
201 port->excl_locked = true;
202 spin_unlock_irqrestore(&vcc_table_lock, flags);
203
204 return port;
205}
206
207
208
209
210
211
212
213
214
215static void vcc_put(struct vcc_port *port, bool excl)
216{
217 unsigned long flags;
218
219 if (!port)
220 return;
221
222 spin_lock_irqsave(&vcc_table_lock, flags);
223
224
225 if (WARN_ON((excl && !port->excl_locked) ||
226 (!excl && port->excl_locked)))
227 goto done;
228
229 port->refcnt--;
230
231 if (excl)
232 port->excl_locked = false;
233
234done:
235 spin_unlock_irqrestore(&vcc_table_lock, flags);
236}
237
238
239
240
241
242
243
244
245
246
247static struct vcc_port *vcc_get_ne(unsigned long index)
248{
249 struct vcc_port *port;
250
251 port = vcc_get(index, false);
252
253 if (port && port->removed) {
254 vcc_put(port, false);
255 return NULL;
256 }
257
258 return port;
259}
260
261static void vcc_kick_rx(struct vcc_port *port)
262{
263 struct vio_driver_state *vio = &port->vio;
264
265 assert_spin_locked(&port->lock);
266
267 if (!timer_pending(&port->rx_timer) && !port->removed) {
268 disable_irq_nosync(vio->vdev->rx_irq);
269 port->rx_timer.expires = (jiffies + 1);
270 add_timer(&port->rx_timer);
271 }
272}
273
274static void vcc_kick_tx(struct vcc_port *port)
275{
276 assert_spin_locked(&port->lock);
277
278 if (!timer_pending(&port->tx_timer) && !port->removed) {
279 port->tx_timer.expires = (jiffies + 1);
280 add_timer(&port->tx_timer);
281 }
282}
283
284static int vcc_rx_check(struct tty_struct *tty, int size)
285{
286 if (WARN_ON(!tty || !tty->port))
287 return 1;
288
289
290
291
292 if (test_bit(TTY_THROTTLED, &tty->flags) ||
293 (tty_buffer_request_room(tty->port, VCC_BUFF_LEN) < VCC_BUFF_LEN))
294 return 0;
295
296 return 1;
297}
298
299static int vcc_rx(struct tty_struct *tty, char *buf, int size)
300{
301 int len = 0;
302
303 if (WARN_ON(!tty || !tty->port))
304 return len;
305
306 len = tty_insert_flip_string(tty->port, buf, size);
307 if (len)
308 tty_flip_buffer_push(tty->port);
309
310 return len;
311}
312
313static int vcc_ldc_read(struct vcc_port *port)
314{
315 struct vio_driver_state *vio = &port->vio;
316 struct tty_struct *tty;
317 struct vio_vcc pkt;
318 int rv = 0;
319
320 tty = port->tty;
321 if (!tty) {
322 rv = ldc_rx_reset(vio->lp);
323 vccdbg("VCC: reset rx q: rv=%d\n", rv);
324 goto done;
325 }
326
327
328 while (1) {
329 if (!vcc_rx_check(tty, VIO_VCC_MTU_SIZE)) {
330 vcc_kick_rx(port);
331 break;
332 }
333
334 vccdbgl(vio->lp);
335
336 rv = ldc_read(vio->lp, &pkt, sizeof(pkt));
337 if (rv <= 0)
338 break;
339
340 vccdbg("VCC: ldc_read()=%d\n", rv);
341 vccdbg("TAG [%02x:%02x:%04x:%08x]\n",
342 pkt.tag.type, pkt.tag.stype,
343 pkt.tag.stype_env, pkt.tag.sid);
344
345 if (pkt.tag.type == VIO_TYPE_DATA) {
346 vccdbgp(pkt);
347
348 vcc_rx(tty, pkt.data, pkt.tag.stype);
349 } else {
350 pr_err("VCC: unknown msg [%02x:%02x:%04x:%08x]\n",
351 pkt.tag.type, pkt.tag.stype,
352 pkt.tag.stype_env, pkt.tag.sid);
353 rv = -ECONNRESET;
354 break;
355 }
356
357 WARN_ON(rv != LDC_PACKET_SIZE);
358 }
359
360done:
361 return rv;
362}
363
364static void vcc_rx_timer(unsigned long index)
365{
366 struct vio_driver_state *vio;
367 struct vcc_port *port;
368 unsigned long flags;
369 int rv;
370
371 port = vcc_get_ne(index);
372 if (!port)
373 return;
374
375 spin_lock_irqsave(&port->lock, flags);
376 port->rx_timer.expires = 0;
377
378 vio = &port->vio;
379
380 enable_irq(vio->vdev->rx_irq);
381
382 if (!port->tty || port->removed)
383 goto done;
384
385 rv = vcc_ldc_read(port);
386 if (rv == -ECONNRESET)
387 vio_conn_reset(vio);
388
389done:
390 spin_unlock_irqrestore(&port->lock, flags);
391 vcc_put(port, false);
392}
393
394static void vcc_tx_timer(unsigned long index)
395{
396 struct vcc_port *port;
397 struct vio_vcc *pkt;
398 unsigned long flags;
399 int tosend = 0;
400 int rv;
401
402 port = vcc_get_ne(index);
403 if (!port)
404 return;
405
406 spin_lock_irqsave(&port->lock, flags);
407 port->tx_timer.expires = 0;
408
409 if (!port->tty || port->removed)
410 goto done;
411
412 tosend = min(VCC_BUFF_LEN, port->chars_in_buffer);
413 if (!tosend)
414 goto done;
415
416 pkt = &port->buffer;
417 pkt->tag.type = VIO_TYPE_DATA;
418 pkt->tag.stype = tosend;
419 vccdbgl(port->vio.lp);
420
421 rv = ldc_write(port->vio.lp, pkt, (VIO_TAG_SIZE + tosend));
422 WARN_ON(!rv);
423
424 if (rv < 0) {
425 vccdbg("VCC: ldc_write()=%d\n", rv);
426 vcc_kick_tx(port);
427 } else {
428 struct tty_struct *tty = port->tty;
429
430 port->chars_in_buffer = 0;
431 if (tty)
432 tty_wakeup(tty);
433 }
434
435done:
436 spin_unlock_irqrestore(&port->lock, flags);
437 vcc_put(port, false);
438}
439
440
441
442
443
444
445
446
447static void vcc_event(void *arg, int event)
448{
449 struct vio_driver_state *vio;
450 struct vcc_port *port;
451 unsigned long flags;
452 int rv;
453
454 port = arg;
455 vio = &port->vio;
456
457 spin_lock_irqsave(&port->lock, flags);
458
459 switch (event) {
460 case LDC_EVENT_RESET:
461 case LDC_EVENT_UP:
462 vio_link_state_change(vio, event);
463 break;
464
465 case LDC_EVENT_DATA_READY:
466 rv = vcc_ldc_read(port);
467 if (rv == -ECONNRESET)
468 vio_conn_reset(vio);
469 break;
470
471 default:
472 pr_err("VCC: unexpected LDC event(%d)\n", event);
473 }
474
475 spin_unlock_irqrestore(&port->lock, flags);
476}
477
478static struct ldc_channel_config vcc_ldc_cfg = {
479 .event = vcc_event,
480 .mtu = VIO_VCC_MTU_SIZE,
481 .mode = LDC_MODE_RAW,
482 .debug = 0,
483};
484
485
486static struct vio_version vcc_versions[] = {
487 { .major = 1, .minor = 0 },
488};
489
490static struct tty_port_operations vcc_port_ops = { 0 };
491
492static ssize_t vcc_sysfs_domain_show(struct device *dev,
493 struct device_attribute *attr,
494 char *buf)
495{
496 struct vcc_port *port;
497 int rv;
498
499 port = dev_get_drvdata(dev);
500 if (!port)
501 return -ENODEV;
502
503 rv = scnprintf(buf, PAGE_SIZE, "%s\n", port->domain);
504
505 return rv;
506}
507
508static int vcc_send_ctl(struct vcc_port *port, int ctl)
509{
510 struct vio_vcc pkt;
511 int rv;
512
513 pkt.tag.type = VIO_TYPE_CTRL;
514 pkt.tag.sid = ctl;
515 pkt.tag.stype = 0;
516
517 rv = ldc_write(port->vio.lp, &pkt, sizeof(pkt.tag));
518 WARN_ON(!rv);
519 vccdbg("VCC: ldc_write(%ld)=%d\n", sizeof(pkt.tag), rv);
520
521 return rv;
522}
523
524static ssize_t vcc_sysfs_break_store(struct device *dev,
525 struct device_attribute *attr,
526 const char *buf, size_t count)
527{
528 struct vcc_port *port;
529 unsigned long flags;
530 int rv = count;
531 int brk;
532
533 port = dev_get_drvdata(dev);
534 if (!port)
535 return -ENODEV;
536
537 spin_lock_irqsave(&port->lock, flags);
538
539 if (sscanf(buf, "%ud", &brk) != 1 || brk != 1)
540 rv = -EINVAL;
541 else if (vcc_send_ctl(port, VCC_CTL_BREAK) < 0)
542 vcc_kick_tx(port);
543
544 spin_unlock_irqrestore(&port->lock, flags);
545
546 return rv;
547}
548
549static DEVICE_ATTR(domain, 0400, vcc_sysfs_domain_show, NULL);
550static DEVICE_ATTR(break, 0200, NULL, vcc_sysfs_break_store);
551
552static struct attribute *vcc_sysfs_entries[] = {
553 &dev_attr_domain.attr,
554 &dev_attr_break.attr,
555 NULL
556};
557
558static struct attribute_group vcc_attribute_group = {
559 .name = NULL,
560 .attrs = vcc_sysfs_entries,
561};
562
563
564
565
566
567
568
569
570
571
572
573
574
575static int vcc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
576{
577 struct mdesc_handle *hp;
578 struct vcc_port *port;
579 struct device *dev;
580 const char *domain;
581 char *name;
582 u64 node;
583 int rv;
584
585 vccdbg("VCC: name=%s\n", dev_name(&vdev->dev));
586
587 if (!vcc_tty_driver) {
588 pr_err("VCC: TTY driver not registered\n");
589 return -ENODEV;
590 }
591
592 port = kzalloc(sizeof(struct vcc_port), GFP_KERNEL);
593 if (!port)
594 return -ENOMEM;
595
596 name = kstrdup(dev_name(&vdev->dev), GFP_KERNEL);
597
598 rv = vio_driver_init(&port->vio, vdev, VDEV_CONSOLE_CON, vcc_versions,
599 ARRAY_SIZE(vcc_versions), NULL, name);
600 if (rv)
601 goto free_port;
602
603 port->vio.debug = vcc_dbg_vio;
604 vcc_ldc_cfg.debug = vcc_dbg_ldc;
605
606 rv = vio_ldc_alloc(&port->vio, &vcc_ldc_cfg, port);
607 if (rv)
608 goto free_port;
609
610 spin_lock_init(&port->lock);
611
612 port->index = vcc_table_add(port);
613 if (port->index == -1) {
614 pr_err("VCC: no more TTY indices left for allocation\n");
615 goto free_ldc;
616 }
617
618
619 dev = tty_register_device(vcc_tty_driver, port->index, &vdev->dev);
620 if (IS_ERR(dev)) {
621 rv = PTR_ERR(dev);
622 goto free_table;
623 }
624
625 hp = mdesc_grab();
626
627 node = vio_vdev_node(hp, vdev);
628 if (node == MDESC_NODE_NULL) {
629 rv = -ENXIO;
630 mdesc_release(hp);
631 goto unreg_tty;
632 }
633
634 domain = mdesc_get_property(hp, node, "vcc-domain-name", NULL);
635 if (!domain) {
636 rv = -ENXIO;
637 mdesc_release(hp);
638 goto unreg_tty;
639 }
640 port->domain = kstrdup(domain, GFP_KERNEL);
641
642 mdesc_release(hp);
643
644 rv = sysfs_create_group(&vdev->dev.kobj, &vcc_attribute_group);
645 if (rv)
646 goto free_domain;
647
648 init_timer(&port->rx_timer);
649 port->rx_timer.function = vcc_rx_timer;
650 port->rx_timer.data = port->index;
651
652 init_timer(&port->tx_timer);
653 port->tx_timer.function = vcc_tx_timer;
654 port->tx_timer.data = port->index;
655
656 dev_set_drvdata(&vdev->dev, port);
657
658
659
660
661 disable_irq_nosync(vdev->rx_irq);
662 vio_port_up(&port->vio);
663 enable_irq(vdev->rx_irq);
664
665 return 0;
666
667free_domain:
668 kfree(port->domain);
669unreg_tty:
670 tty_unregister_device(vcc_tty_driver, port->index);
671free_table:
672 vcc_table_remove(port->index);
673free_ldc:
674 vio_ldc_free(&port->vio);
675free_port:
676 kfree(name);
677 kfree(port);
678
679 return rv;
680}
681
682
683
684
685
686
687
688
689
690
691static int vcc_remove(struct vio_dev *vdev)
692{
693 struct vcc_port *port = dev_get_drvdata(&vdev->dev);
694
695 if (!port)
696 return -ENODEV;
697
698 del_timer_sync(&port->rx_timer);
699 del_timer_sync(&port->tx_timer);
700
701
702
703
704
705 if (port->tty)
706 tty_vhangup(port->tty);
707
708
709
710
711 port = vcc_get(port->index, true);
712
713 if (WARN_ON(!port))
714 return -ENODEV;
715
716 tty_unregister_device(vcc_tty_driver, port->index);
717
718 del_timer_sync(&port->vio.timer);
719 vio_ldc_free(&port->vio);
720 sysfs_remove_group(&vdev->dev.kobj, &vcc_attribute_group);
721 dev_set_drvdata(&vdev->dev, NULL);
722 if (port->tty) {
723 port->removed = true;
724 vcc_put(port, true);
725 } else {
726 vcc_table_remove(port->index);
727
728 kfree(port->vio.name);
729 kfree(port->domain);
730 kfree(port);
731 }
732
733 return 0;
734}
735
736static const struct vio_device_id vcc_match[] = {
737 {
738 .type = "vcc-port",
739 },
740 {},
741};
742MODULE_DEVICE_TABLE(vio, vcc_match);
743
744static struct vio_driver vcc_driver = {
745 .id_table = vcc_match,
746 .probe = vcc_probe,
747 .remove = vcc_remove,
748 .name = "vcc",
749};
750
751static int vcc_open(struct tty_struct *tty, struct file *vcc_file)
752{
753 struct vcc_port *port;
754
755 if (unlikely(!tty)) {
756 pr_err("VCC: open: Invalid TTY handle\n");
757 return -ENXIO;
758 }
759
760 if (tty->count > 1)
761 return -EBUSY;
762
763 port = vcc_get_ne(tty->index);
764 if (unlikely(!port)) {
765 pr_err("VCC: open: Failed to find VCC port\n");
766 return -ENODEV;
767 }
768
769 if (unlikely(!port->vio.lp)) {
770 pr_err("VCC: open: LDC channel not configured\n");
771 vcc_put(port, false);
772 return -EPIPE;
773 }
774 vccdbgl(port->vio.lp);
775
776 vcc_put(port, false);
777
778 if (unlikely(!tty->port)) {
779 pr_err("VCC: open: TTY port not found\n");
780 return -ENXIO;
781 }
782
783 if (unlikely(!tty->port->ops)) {
784 pr_err("VCC: open: TTY ops not defined\n");
785 return -ENXIO;
786 }
787
788 return tty_port_open(tty->port, tty, vcc_file);
789}
790
791static void vcc_close(struct tty_struct *tty, struct file *vcc_file)
792{
793 if (unlikely(!tty)) {
794 pr_err("VCC: close: Invalid TTY handle\n");
795 return;
796 }
797
798 if (unlikely(tty->count > 1))
799 return;
800
801 if (unlikely(!tty->port)) {
802 pr_err("VCC: close: TTY port not found\n");
803 return;
804 }
805
806 tty_port_close(tty->port, tty, vcc_file);
807}
808
809static void vcc_ldc_hup(struct vcc_port *port)
810{
811 unsigned long flags;
812
813 spin_lock_irqsave(&port->lock, flags);
814
815 if (vcc_send_ctl(port, VCC_CTL_HUP) < 0)
816 vcc_kick_tx(port);
817
818 spin_unlock_irqrestore(&port->lock, flags);
819}
820
821static void vcc_hangup(struct tty_struct *tty)
822{
823 struct vcc_port *port;
824
825 if (unlikely(!tty)) {
826 pr_err("VCC: hangup: Invalid TTY handle\n");
827 return;
828 }
829
830 port = vcc_get_ne(tty->index);
831 if (unlikely(!port)) {
832 pr_err("VCC: hangup: Failed to find VCC port\n");
833 return;
834 }
835
836 if (unlikely(!tty->port)) {
837 pr_err("VCC: hangup: TTY port not found\n");
838 vcc_put(port, false);
839 return;
840 }
841
842 vcc_ldc_hup(port);
843
844 vcc_put(port, false);
845
846 tty_port_hangup(tty->port);
847}
848
849static int vcc_write(struct tty_struct *tty, const unsigned char *buf,
850 int count)
851{
852 struct vcc_port *port;
853 struct vio_vcc *pkt;
854 unsigned long flags;
855 int total_sent = 0;
856 int tosend = 0;
857 int rv = -EINVAL;
858
859 if (unlikely(!tty)) {
860 pr_err("VCC: write: Invalid TTY handle\n");
861 return -ENXIO;
862 }
863
864 port = vcc_get_ne(tty->index);
865 if (unlikely(!port)) {
866 pr_err("VCC: write: Failed to find VCC port");
867 return -ENODEV;
868 }
869
870 spin_lock_irqsave(&port->lock, flags);
871
872 pkt = &port->buffer;
873 pkt->tag.type = VIO_TYPE_DATA;
874
875 while (count > 0) {
876
877 tosend = min(count, (VCC_BUFF_LEN - port->chars_in_buffer));
878
879 if (!tosend)
880 break;
881
882 memcpy(&pkt->data[port->chars_in_buffer], &buf[total_sent],
883 tosend);
884 port->chars_in_buffer += tosend;
885 pkt->tag.stype = tosend;
886
887 vccdbg("TAG [%02x:%02x:%04x:%08x]\n", pkt->tag.type,
888 pkt->tag.stype, pkt->tag.stype_env, pkt->tag.sid);
889 vccdbg("DATA [%s]\n", pkt->data);
890 vccdbgl(port->vio.lp);
891
892
893
894
895
896 rv = ldc_write(port->vio.lp, pkt, (VIO_TAG_SIZE + tosend));
897 vccdbg("VCC: write: ldc_write(%d)=%d\n",
898 (VIO_TAG_SIZE + tosend), rv);
899
900 total_sent += tosend;
901 count -= tosend;
902 if (rv < 0) {
903 vcc_kick_tx(port);
904 break;
905 }
906
907 port->chars_in_buffer = 0;
908 }
909
910 spin_unlock_irqrestore(&port->lock, flags);
911
912 vcc_put(port, false);
913
914 vccdbg("VCC: write: total=%d rv=%d", total_sent, rv);
915
916 return total_sent ? total_sent : rv;
917}
918
919static int vcc_write_room(struct tty_struct *tty)
920{
921 struct vcc_port *port;
922 u64 num;
923
924 if (unlikely(!tty)) {
925 pr_err("VCC: write_room: Invalid TTY handle\n");
926 return -ENXIO;
927 }
928
929 port = vcc_get_ne(tty->index);
930 if (unlikely(!port)) {
931 pr_err("VCC: write_room: Failed to find VCC port\n");
932 return -ENODEV;
933 }
934
935 num = VCC_BUFF_LEN - port->chars_in_buffer;
936
937 vcc_put(port, false);
938
939 return num;
940}
941
942static int vcc_chars_in_buffer(struct tty_struct *tty)
943{
944 struct vcc_port *port;
945 u64 num;
946
947 if (unlikely(!tty)) {
948 pr_err("VCC: chars_in_buffer: Invalid TTY handle\n");
949 return -ENXIO;
950 }
951
952 port = vcc_get_ne(tty->index);
953 if (unlikely(!port)) {
954 pr_err("VCC: chars_in_buffer: Failed to find VCC port\n");
955 return -ENODEV;
956 }
957
958 num = port->chars_in_buffer;
959
960 vcc_put(port, false);
961
962 return num;
963}
964
965static int vcc_break_ctl(struct tty_struct *tty, int state)
966{
967 struct vcc_port *port;
968 unsigned long flags;
969
970 if (unlikely(!tty)) {
971 pr_err("VCC: break_ctl: Invalid TTY handle\n");
972 return -ENXIO;
973 }
974
975 port = vcc_get_ne(tty->index);
976 if (unlikely(!port)) {
977 pr_err("VCC: break_ctl: Failed to find VCC port\n");
978 return -ENODEV;
979 }
980
981
982 if (state == 0) {
983 vcc_put(port, false);
984 return 0;
985 }
986
987 spin_lock_irqsave(&port->lock, flags);
988
989 if (vcc_send_ctl(port, VCC_CTL_BREAK) < 0)
990 vcc_kick_tx(port);
991
992 spin_unlock_irqrestore(&port->lock, flags);
993
994 vcc_put(port, false);
995
996 return 0;
997}
998
999static int vcc_install(struct tty_driver *driver, struct tty_struct *tty)
1000{
1001 struct vcc_port *port_vcc;
1002 struct tty_port *port_tty;
1003 int ret;
1004
1005 if (unlikely(!tty)) {
1006 pr_err("VCC: install: Invalid TTY handle\n");
1007 return -ENXIO;
1008 }
1009
1010 if (tty->index >= VCC_MAX_PORTS)
1011 return -EINVAL;
1012
1013 ret = tty_standard_install(driver, tty);
1014 if (ret)
1015 return ret;
1016
1017 port_tty = kzalloc(sizeof(struct tty_port), GFP_KERNEL);
1018 if (!port_tty)
1019 return -ENOMEM;
1020
1021 port_vcc = vcc_get(tty->index, true);
1022 if (!port_vcc) {
1023 pr_err("VCC: install: Failed to find VCC port\n");
1024 tty->port = NULL;
1025 kfree(port_tty);
1026 return -ENODEV;
1027 }
1028
1029 tty_port_init(port_tty);
1030 port_tty->ops = &vcc_port_ops;
1031 tty->port = port_tty;
1032
1033 port_vcc->tty = tty;
1034
1035 vcc_put(port_vcc, true);
1036
1037 return 0;
1038}
1039
1040static void vcc_cleanup(struct tty_struct *tty)
1041{
1042 struct vcc_port *port;
1043
1044 if (unlikely(!tty)) {
1045 pr_err("VCC: cleanup: Invalid TTY handle\n");
1046 return;
1047 }
1048
1049 port = vcc_get(tty->index, true);
1050 if (port) {
1051 port->tty = NULL;
1052
1053 if (port->removed) {
1054 vcc_table_remove(tty->index);
1055 kfree(port->vio.name);
1056 kfree(port->domain);
1057 kfree(port);
1058 } else {
1059 vcc_put(port, true);
1060 }
1061 }
1062
1063 tty_port_destroy(tty->port);
1064 kfree(tty->port);
1065 tty->port = NULL;
1066}
1067
1068static const struct tty_operations vcc_ops = {
1069 .open = vcc_open,
1070 .close = vcc_close,
1071 .hangup = vcc_hangup,
1072 .write = vcc_write,
1073 .write_room = vcc_write_room,
1074 .chars_in_buffer = vcc_chars_in_buffer,
1075 .break_ctl = vcc_break_ctl,
1076 .install = vcc_install,
1077 .cleanup = vcc_cleanup,
1078};
1079
1080#define VCC_TTY_FLAGS (TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_REAL_RAW)
1081
1082static int vcc_tty_init(void)
1083{
1084 int rv;
1085
1086 pr_info("VCC: %s\n", version);
1087
1088 vcc_tty_driver = tty_alloc_driver(VCC_MAX_PORTS, VCC_TTY_FLAGS);
1089 if (IS_ERR(vcc_tty_driver)) {
1090 pr_err("VCC: TTY driver alloc failed\n");
1091 return PTR_ERR(vcc_tty_driver);
1092 }
1093
1094 vcc_tty_driver->driver_name = vcc_driver_name;
1095 vcc_tty_driver->name = vcc_device_node;
1096
1097 vcc_tty_driver->minor_start = VCC_MINOR_START;
1098 vcc_tty_driver->type = TTY_DRIVER_TYPE_SYSTEM;
1099 vcc_tty_driver->init_termios = vcc_tty_termios;
1100
1101 tty_set_operations(vcc_tty_driver, &vcc_ops);
1102
1103 rv = tty_register_driver(vcc_tty_driver);
1104 if (rv) {
1105 pr_err("VCC: TTY driver registration failed\n");
1106 put_tty_driver(vcc_tty_driver);
1107 vcc_tty_driver = NULL;
1108 return rv;
1109 }
1110
1111 vccdbg("VCC: TTY driver registered\n");
1112
1113 return 0;
1114}
1115
1116static void vcc_tty_exit(void)
1117{
1118 tty_unregister_driver(vcc_tty_driver);
1119 put_tty_driver(vcc_tty_driver);
1120 vccdbg("VCC: TTY driver unregistered\n");
1121
1122 vcc_tty_driver = NULL;
1123}
1124
1125static int __init vcc_init(void)
1126{
1127 int rv;
1128
1129 rv = vcc_tty_init();
1130 if (rv) {
1131 pr_err("VCC: TTY init failed\n");
1132 return rv;
1133 }
1134
1135 rv = vio_register_driver(&vcc_driver);
1136 if (rv) {
1137 pr_err("VCC: VIO driver registration failed\n");
1138 vcc_tty_exit();
1139 } else {
1140 vccdbg("VCC: VIO driver registered successfully\n");
1141 }
1142
1143 return rv;
1144}
1145
1146static void __exit vcc_exit(void)
1147{
1148 vio_unregister_driver(&vcc_driver);
1149 vccdbg("VCC: VIO driver unregistered\n");
1150 vcc_tty_exit();
1151 vccdbg("VCC: TTY driver unregistered\n");
1152}
1153
1154module_init(vcc_init);
1155module_exit(vcc_exit);
1156