1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Copyright (c) 2021 IBM Corp. */
4 #include <linux/delay.h>
5 #include <linux/device.h>
6 #include <linux/errno.h>
7 #include <linux/list.h>
8 #include <linux/module.h>
9 #include <linux/sched/signal.h>
10 #include <linux/serio.h>
11 #include <linux/slab.h>
13 #include "kcs_bmc_client.h"
15 struct kcs_bmc_serio
{
16 struct list_head entry
;
18 struct kcs_bmc_client client
;
24 static inline struct kcs_bmc_serio
*client_to_kcs_bmc_serio(struct kcs_bmc_client
*client
)
26 return container_of(client
, struct kcs_bmc_serio
, client
);
29 static irqreturn_t
kcs_bmc_serio_event(struct kcs_bmc_client
*client
)
31 struct kcs_bmc_serio
*priv
;
32 u8 handled
= IRQ_NONE
;
35 priv
= client_to_kcs_bmc_serio(client
);
37 spin_lock(&priv
->lock
);
39 status
= kcs_bmc_read_status(client
->dev
);
41 if (status
& KCS_BMC_STR_IBF
)
42 handled
= serio_interrupt(priv
->port
, kcs_bmc_read_data(client
->dev
), 0);
44 spin_unlock(&priv
->lock
);
49 static const struct kcs_bmc_client_ops kcs_bmc_serio_client_ops
= {
50 .event
= kcs_bmc_serio_event
,
53 static int kcs_bmc_serio_open(struct serio
*port
)
55 struct kcs_bmc_serio
*priv
= port
->port_data
;
57 return kcs_bmc_enable_device(priv
->client
.dev
, &priv
->client
);
60 static void kcs_bmc_serio_close(struct serio
*port
)
62 struct kcs_bmc_serio
*priv
= port
->port_data
;
64 kcs_bmc_disable_device(priv
->client
.dev
, &priv
->client
);
67 static DEFINE_SPINLOCK(kcs_bmc_serio_instances_lock
);
68 static LIST_HEAD(kcs_bmc_serio_instances
);
70 static int kcs_bmc_serio_add_device(struct kcs_bmc_device
*kcs_bmc
)
72 struct kcs_bmc_serio
*priv
;
75 priv
= devm_kzalloc(kcs_bmc
->dev
, sizeof(*priv
), GFP_KERNEL
);
79 /* Use kzalloc() as the allocation is cleaned up with kfree() via serio_unregister_port() */
80 port
= kzalloc(sizeof(*port
), GFP_KERNEL
);
84 port
->id
.type
= SERIO_8042
;
85 port
->open
= kcs_bmc_serio_open
;
86 port
->close
= kcs_bmc_serio_close
;
87 port
->port_data
= priv
;
88 port
->dev
.parent
= kcs_bmc
->dev
;
90 spin_lock_init(&priv
->lock
);
92 priv
->client
.dev
= kcs_bmc
;
93 priv
->client
.ops
= &kcs_bmc_serio_client_ops
;
95 spin_lock_irq(&kcs_bmc_serio_instances_lock
);
96 list_add(&priv
->entry
, &kcs_bmc_serio_instances
);
97 spin_unlock_irq(&kcs_bmc_serio_instances_lock
);
99 serio_register_port(port
);
101 dev_info(kcs_bmc
->dev
, "Initialised serio client for channel %d", kcs_bmc
->channel
);
106 static int kcs_bmc_serio_remove_device(struct kcs_bmc_device
*kcs_bmc
)
108 struct kcs_bmc_serio
*priv
= NULL
, *pos
;
110 spin_lock_irq(&kcs_bmc_serio_instances_lock
);
111 list_for_each_entry(pos
, &kcs_bmc_serio_instances
, entry
) {
112 if (pos
->client
.dev
== kcs_bmc
) {
114 list_del(&pos
->entry
);
118 spin_unlock_irq(&kcs_bmc_serio_instances_lock
);
123 /* kfree()s priv->port via put_device() */
124 serio_unregister_port(priv
->port
);
126 /* Ensure the IBF IRQ is disabled if we were the active client */
127 kcs_bmc_disable_device(kcs_bmc
, &priv
->client
);
129 devm_kfree(priv
->client
.dev
->dev
, priv
);
134 static const struct kcs_bmc_driver_ops kcs_bmc_serio_driver_ops
= {
135 .add_device
= kcs_bmc_serio_add_device
,
136 .remove_device
= kcs_bmc_serio_remove_device
,
139 static struct kcs_bmc_driver kcs_bmc_serio_driver
= {
140 .ops
= &kcs_bmc_serio_driver_ops
,
143 static int __init
kcs_bmc_serio_init(void)
145 kcs_bmc_register_driver(&kcs_bmc_serio_driver
);
149 module_init(kcs_bmc_serio_init
);
151 static void __exit
kcs_bmc_serio_exit(void)
153 kcs_bmc_unregister_driver(&kcs_bmc_serio_driver
);
155 module_exit(kcs_bmc_serio_exit
);
157 MODULE_LICENSE("GPL v2");
158 MODULE_AUTHOR("Andrew Jeffery <andrew@aj.id.au>");
159 MODULE_DESCRIPTION("Adapter driver for serio access to BMC KCS devices");