213 lines
5 KiB
C
213 lines
5 KiB
C
#include <linux/device.h>
|
|
|
|
#include <linux/notifier.h>
|
|
#include <linux/muic/muic.h>
|
|
#include <linux/muic/muic_notifier.h>
|
|
#include <linux/sec_class.h>
|
|
|
|
/*
|
|
* The src & dest addresses of the noti.
|
|
* keep the same value defined in ccic_notifier.h
|
|
* b'0001 : CCIC
|
|
* b'0010 : MUIC
|
|
* b'1111 : Broadcasting
|
|
*/
|
|
#define NOTI_ADDR_SRC (1 << 1)
|
|
#define NOTI_ADDR_DST (0xf)
|
|
|
|
/* ATTACH Noti. ID */
|
|
#define NOTI_ID_ATTACH (1)
|
|
|
|
|
|
#define SET_MUIC_NOTIFIER_BLOCK(nb, fn, dev) do { \
|
|
(nb)->notifier_call = (fn); \
|
|
(nb)->priority = (dev); \
|
|
} while (0)
|
|
|
|
#define DESTROY_MUIC_NOTIFIER_BLOCK(nb) \
|
|
SET_MUIC_NOTIFIER_BLOCK(nb, NULL, -1)
|
|
|
|
static struct muic_notifier_struct muic_notifier;
|
|
|
|
struct device *switch_device;
|
|
|
|
static int muic_uses_new_noti;
|
|
|
|
void muic_notifier_set_new_noti(bool flag)
|
|
{
|
|
muic_uses_new_noti = flag ? 1: 0;
|
|
}
|
|
|
|
static void __set_noti_cxt(int attach, int type)
|
|
{
|
|
if (type < 0) {
|
|
muic_notifier.cmd = attach;
|
|
muic_notifier.cxt.attach = attach;
|
|
return;
|
|
}
|
|
|
|
/* Old Interface */
|
|
muic_notifier.cmd = attach;
|
|
muic_notifier.attached_dev = type;
|
|
|
|
/* New Interface */
|
|
muic_notifier.cxt.src = NOTI_ADDR_SRC;
|
|
muic_notifier.cxt.dest = NOTI_ADDR_DST;
|
|
muic_notifier.cxt.id = NOTI_ID_ATTACH;
|
|
muic_notifier.cxt.attach = attach;
|
|
muic_notifier.cxt.cable_type = type;
|
|
muic_notifier.cxt.reserved = 0;
|
|
}
|
|
|
|
int muic_notifier_register(struct notifier_block *nb, notifier_fn_t notifier,
|
|
muic_notifier_device_t listener)
|
|
{
|
|
int ret = 0;
|
|
void *pcxt;;
|
|
|
|
pr_info("%s: listener=%d register\n", __func__, listener);
|
|
|
|
SET_MUIC_NOTIFIER_BLOCK(nb, notifier, listener);
|
|
ret = blocking_notifier_chain_register(&(muic_notifier.notifier_call_chain), nb);
|
|
if (ret < 0)
|
|
pr_err("%s: blocking_notifier_chain_register error(%d)\n",
|
|
__func__, ret);
|
|
|
|
pcxt = muic_uses_new_noti ? &(muic_notifier.cxt) : \
|
|
(void *)&(muic_notifier.attached_dev);
|
|
|
|
/* current muic's attached_device status notify */
|
|
nb->notifier_call(nb, muic_notifier.cxt.attach, pcxt);
|
|
|
|
return ret;
|
|
}
|
|
|
|
int muic_notifier_unregister(struct notifier_block *nb)
|
|
{
|
|
int ret = 0;
|
|
|
|
pr_info("%s: listener=%d unregister\n", __func__, nb->priority);
|
|
|
|
ret = blocking_notifier_chain_unregister(&(muic_notifier.notifier_call_chain), nb);
|
|
if (ret < 0)
|
|
pr_err("%s: blocking_notifier_chain_unregister error(%d)\n",
|
|
__func__, ret);
|
|
DESTROY_MUIC_NOTIFIER_BLOCK(nb);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static int muic_notifier_notify(void)
|
|
{
|
|
int ret = 0;
|
|
void *pcxt;
|
|
|
|
pr_info("%s: CMD=%d, DATA=%d\n", __func__, muic_notifier.cxt.attach,
|
|
muic_notifier.cxt.cable_type);
|
|
|
|
pcxt = muic_uses_new_noti ? &(muic_notifier.cxt) : \
|
|
(void *)&(muic_notifier.attached_dev);
|
|
|
|
ret = blocking_notifier_call_chain(&(muic_notifier.notifier_call_chain),
|
|
muic_notifier.cxt.attach, pcxt);
|
|
|
|
switch (ret) {
|
|
case NOTIFY_STOP_MASK:
|
|
case NOTIFY_BAD:
|
|
pr_err("%s: notify error occur(0x%x)\n", __func__, ret);
|
|
break;
|
|
case NOTIFY_DONE:
|
|
case NOTIFY_OK:
|
|
pr_info("%s: notify done(0x%x)\n", __func__, ret);
|
|
break;
|
|
default:
|
|
pr_info("%s: notify status unknown(0x%x)\n", __func__, ret);
|
|
break;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
void muic_notifier_attach_attached_dev(muic_attached_dev_t new_dev)
|
|
{
|
|
pr_info("%s: (%d)\n", __func__, new_dev);
|
|
|
|
__set_noti_cxt(MUIC_NOTIFY_CMD_ATTACH, new_dev);
|
|
|
|
/* muic's attached_device attach broadcast */
|
|
muic_notifier_notify();
|
|
}
|
|
|
|
void muic_notifier_detach_attached_dev(muic_attached_dev_t cur_dev)
|
|
{
|
|
pr_info("%s: (%d)\n", __func__, cur_dev);
|
|
|
|
__set_noti_cxt(MUIC_NOTIFY_CMD_DETACH, -1);
|
|
|
|
if (muic_notifier.cxt.cable_type != cur_dev)
|
|
pr_warn("%s: attached_dev of muic_notifier(%d) != muic_data(%d)\n",
|
|
__func__, muic_notifier.cxt.cable_type, cur_dev);
|
|
|
|
if (muic_notifier.cxt.cable_type != ATTACHED_DEV_NONE_MUIC) {
|
|
/* muic's attached_device detach broadcast */
|
|
muic_notifier_notify();
|
|
}
|
|
|
|
__set_noti_cxt(0, ATTACHED_DEV_NONE_MUIC);
|
|
}
|
|
|
|
void muic_notifier_logically_attach_attached_dev(muic_attached_dev_t new_dev)
|
|
{
|
|
pr_info("%s: (%d)\n", __func__, new_dev);
|
|
|
|
__set_noti_cxt(MUIC_NOTIFY_CMD_ATTACH, new_dev);
|
|
|
|
/* muic's attached_device attach broadcast */
|
|
muic_notifier_notify();
|
|
}
|
|
|
|
void muic_notifier_logically_detach_attached_dev(muic_attached_dev_t cur_dev)
|
|
{
|
|
pr_info("%s: (%d)\n", __func__, cur_dev);
|
|
|
|
__set_noti_cxt(MUIC_NOTIFY_CMD_DETACH, cur_dev);
|
|
|
|
/* muic's attached_device detach broadcast */
|
|
muic_notifier_notify();
|
|
|
|
__set_noti_cxt(0, ATTACHED_DEV_NONE_MUIC);
|
|
}
|
|
|
|
static int __init muic_notifier_init(void)
|
|
{
|
|
int ret = 0;
|
|
|
|
pr_info("%s\n", __func__);
|
|
#if defined(CONFIG_MUIC_SUPPORT_CCIC) && \
|
|
defined(CONFIG_CCIC_NOTIFIER)
|
|
muic_uses_new_noti = 1;
|
|
|
|
#endif
|
|
switch_device = device_create(sec_class, NULL, 0, NULL, "switch");
|
|
if (IS_ERR(switch_device)) {
|
|
pr_err("(%s): failed to created device (switch_device)!\n",
|
|
__func__);
|
|
return -ENODEV;
|
|
}
|
|
#if 0
|
|
ret = switch_dev_register(&switch_dock);
|
|
if (ret < 0) {
|
|
pr_err("Failed to register dock switch. %d\n",
|
|
ret);
|
|
goto out;
|
|
}
|
|
#endif
|
|
BLOCKING_INIT_NOTIFIER_HEAD(&(muic_notifier.notifier_call_chain));
|
|
__set_noti_cxt(0 ,ATTACHED_DEV_UNKNOWN_MUIC);
|
|
#if 0
|
|
out:
|
|
#endif
|
|
return ret;
|
|
}
|
|
device_initcall(muic_notifier_init);
|
|
|