Blame view

kernel/linux-imx6_3.14.28/drivers/input/keyboard/hookswitch.c 5.65 KB
6b13f685e   김민수   BSP 최초 추가
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
  /*
   * Keypad driver for Cottonwood Creek Technologies Hook Switch
   *
   * Copyright 2014 Boundary Devices
   *
   * Licensed under the BSD or GPL-2 or later.
   */
  
  #include <linux/init.h>
  #include <linux/module.h>
  #include <linux/kernel.h>
  #include <linux/i2c.h>
  #include <linux/kthread.h>
  #include <linux/delay.h>
  #include <linux/input.h>
  
  #define CWC_HOOKSWITCH_I2C_BUS         2
  #define CWC_HOOKSWITCH_I2C_ADDR        0x20
  #define CWC_HOOKSWITCH_I2C_BITMASK     0x40
  #define CWC_HOOKSWITCH_POLL_INTERVAL   250
  #define CWC_HOOKSWITCH_KEY_INTERVAL    50
  
  static struct task_struct *thread;
  static int previous_offhook_state;
  static int previous_led_state;
  struct i2c_client *myclient;
  static struct input_dev *fake_keyboard_dev;
  
  static const char fake_input_name[] = "cwc_hookswitch";
  
  MODULE_DEVICE_TABLE(i2c, cwc_hookswitch_id);
  
  /*
   * ATTRIBUTES:
   *
   * /sys/devices/platform/cwc_hookswitch/offhook [ro]
   * /sys/devices/platform/cwc_hookswitch/leds [rw]
   */
  static ssize_t hookswitch_show_offhook
  	(struct device *dev,
  	 struct device_attribute *attr,
  	 char *buf)
  {
  	return sprintf(buf, "%d
  ", previous_offhook_state);
  }
  
  static ssize_t hookswitch_store_offhook
  	(struct device *dev,
  	 struct device_attribute *attr,
  	 const char *buf,
  	 size_t count)
  {
  	return -EINVAL;
  }
  
  static DEVICE_ATTR(offhook, S_IRUGO,
  		   hookswitch_show_offhook,
  		   hookswitch_store_offhook);
  
  static ssize_t hookswitch_show_leds
  	(struct device *dev,
  	 struct device_attribute *attr,
  	 char *buf)
  {
  	return sprintf(buf, "%d
  ", previous_led_state);
  }
  
  static ssize_t hookswitch_store_leds
          (struct device *dev,
           struct device_attribute *attr,
           const char *buf,
           size_t count)
  {
  	int state;
  	if (sscanf(buf, "%u", &state) == 1) {
  		if (2 > (unsigned)state) {
  			if (state != previous_led_state) {
  				i2c_smbus_write_byte_data
  					(myclient, 1, state ? 0x20 : 0);
  				previous_led_state = state;
  			}
  			return count;
  		}
  	}
  	return -EINVAL;
  }
  
  static DEVICE_ATTR(leds, S_IWUGO | S_IRUGO,
  		   hookswitch_show_leds,
  		   hookswitch_store_leds);
  
  static struct attribute *hookswitch_attrs[] = {
  	&dev_attr_offhook.attr,
  	&dev_attr_leds.attr,
  	NULL,
  };
  
  static struct attribute_group hookswitch_attr_group = {
  	.attrs = hookswitch_attrs,
  };
  
  static void cwc_read_hookstate(void)
  {
  	int data;
  	int offhook;
  	unsigned int key;
  
  	if (myclient != NULL) {
  		data = i2c_smbus_read_byte_data(myclient, 0);
  		offhook = ((data & CWC_HOOKSWITCH_I2C_BITMASK) == 0) ? 0 : 1;
  		if (previous_offhook_state >= 0) {
  			if (offhook != previous_offhook_state) {
  				pr_debug("hookswitch: raw: 0x%02x  offhook: %d
  ",
  					 data, offhook);
  
  				if (offhook)
  					key = KEY_KPLEFTPAREN;
  				else
  					key = KEY_KPRIGHTPAREN;
  
  				input_report_key(fake_keyboard_dev,
  						 KEY_LEFTCTRL, 1);
  				input_report_key(fake_keyboard_dev,
  						 KEY_LEFTALT, 1);
  				input_report_key(fake_keyboard_dev,
  						 key, 1);
  				input_sync(fake_keyboard_dev);
  				msleep(CWC_HOOKSWITCH_KEY_INTERVAL);
  				input_report_key(fake_keyboard_dev,
  						 KEY_LEFTCTRL, 0);
  				input_report_key(fake_keyboard_dev,
  						 KEY_LEFTALT, 0);
  				input_report_key(fake_keyboard_dev,
  						 key, 0);
  				input_sync(fake_keyboard_dev);
  			}
  		}
  		previous_offhook_state = offhook;
  	}
  }
  
  static int cwc_poll_thread(void *data)
  {
  	int ret = 0;
  
  	allow_signal(SIGINT);
  	while (!kthread_should_stop()) {
  		if (ret == -ERESTARTSYS) {
  			pr_info("%s: interrupted
  ", __func__);
  			return -EINTR;
  		}
  
  		cwc_read_hookstate();
  		msleep(CWC_HOOKSWITCH_POLL_INTERVAL);
  	}
  	return ret;
  }
  
  static int cwc_hookswitch_probe(struct i2c_client *client,
  				const struct i2c_device_id *id)
  {
  	int ret = 0;
  
  	myclient = client;
  	previous_offhook_state = -1;
  
  	i2c_smbus_write_byte_data(myclient, 3, 0xd0);
  	i2c_smbus_write_byte_data(myclient, 1, 0x0);
  	fake_keyboard_dev = input_allocate_device();
  	if (!fake_keyboard_dev) {
  		pr_err("%s() : cannot allocate fake keyboard: ", __func__);
  		return -ENOMEM;
  	}
  
  	set_bit(EV_KEY, fake_keyboard_dev->evbit);
  	set_bit(KEY_LEFTCTRL, fake_keyboard_dev->keybit);
  	set_bit(KEY_LEFTALT, fake_keyboard_dev->keybit);
  	set_bit(KEY_KPLEFTPAREN, fake_keyboard_dev->keybit);
  	set_bit(KEY_KPRIGHTPAREN, fake_keyboard_dev->keybit);
  
  	fake_keyboard_dev->name = fake_input_name;
  	fake_keyboard_dev->id.bustype = BUS_USB;
  	fake_keyboard_dev->id.vendor = 0x0525;
  	fake_keyboard_dev->id.product = 0xa4a0;
  	fake_keyboard_dev->id.version = 0x0001;
  
  	ret = input_register_device(fake_keyboard_dev);
  	if (ret)
  		pr_err(KERN_ERR "%s() : failed to register device
  ", __func__);
  
  	thread = kthread_run(cwc_poll_thread, NULL, "%s", KBUILD_MODNAME);
  	if (IS_ERR(thread))
  		ret = PTR_ERR(thread);
  
  	ret = sysfs_create_group(&fake_keyboard_dev->dev.kobj, &hookswitch_attr_group);
  	if (ret)
  		dev_err(&fake_keyboard_dev->dev, "sysfs export error: %d
  ", ret);
  
  	return ret;
  }
  
  static int cwc_hookswitch_remove(struct i2c_client *client)
  {
  	sysfs_remove_group(&fake_keyboard_dev->dev.kobj, &hookswitch_attr_group);
  	input_unregister_device(fake_keyboard_dev);
  
  	if (!IS_ERR_OR_NULL(thread))
  		kthread_stop(thread);
  
  	return 0;
  }
  
  static const struct i2c_device_id cwc_hookswitch_id[] = {
  	{"cwc_hookswitch", 0},
  	{"cwc_xx", 0},
  	{},
  };
  
  static struct i2c_driver cwc_hookswitch_i2c_driver = {
  	.driver = {
  		.owner = THIS_MODULE,
  		.name  = "cwc_hookswitch",
  	},
  	.probe  = cwc_hookswitch_probe,
  	.remove = cwc_hookswitch_remove,
  	.id_table = cwc_hookswitch_id,
  };
  
  static int cwc_hookswitch_init(void)
  {
  	return i2c_add_driver(&cwc_hookswitch_i2c_driver);
  }
  
  static void cwc_hookswitch_exit(void)
  {
  	i2c_del_driver(&cwc_hookswitch_i2c_driver);
  }
  
  module_init(cwc_hookswitch_init);
  module_exit(cwc_hookswitch_exit);
  
  MODULE_DESCRIPTION("CWC Tech Hook Switch");
  MODULE_LICENSE("Dual BSD/GPL");
  MODULE_ALIAS("platform:cwctech-keys");