16
16
* https://www.silabs.com/documents/public/application-notes/an495-cp2112-interface-specification.pdf
17
17
*/
18
18
19
- #include <linux/gpio/consumer.h>
20
- #include <linux/gpio/machine.h>
19
+ #include <linux/bitops.h>
21
20
#include <linux/gpio/driver.h>
22
21
#include <linux/hid.h>
23
22
#include <linux/hidraw.h>
24
23
#include <linux/i2c.h>
25
24
#include <linux/module.h>
26
25
#include <linux/nls.h>
26
+ #include <linux/string_choices.h>
27
27
#include <linux/usb/ch9.h>
28
28
#include "hid-ids.h"
29
29
30
30
#define CP2112_REPORT_MAX_LENGTH 64
31
31
#define CP2112_GPIO_CONFIG_LENGTH 5
32
32
#define CP2112_GPIO_GET_LENGTH 2
33
33
#define CP2112_GPIO_SET_LENGTH 3
34
+ #define CP2112_GPIO_MAX_GPIO 8
35
+ #define CP2112_GPIO_ALL_GPIO_MASK GENMASK(7, 0)
34
36
35
37
enum {
36
38
CP2112_GPIO_CONFIG = 0x02 ,
@@ -163,19 +165,17 @@ struct cp2112_device {
163
165
atomic_t read_avail ;
164
166
atomic_t xfer_avail ;
165
167
struct gpio_chip gc ;
166
- struct irq_chip irq ;
167
168
u8 * in_out_buffer ;
168
169
struct mutex lock ;
169
170
170
- struct gpio_desc * desc [8 ];
171
171
bool gpio_poll ;
172
172
struct delayed_work gpio_poll_worker ;
173
173
unsigned long irq_mask ;
174
174
u8 gpio_prev_state ;
175
175
};
176
176
177
- static int gpio_push_pull = 0xFF ;
178
- module_param (gpio_push_pull , int , S_IRUGO | S_IWUSR );
177
+ static int gpio_push_pull = CP2112_GPIO_ALL_GPIO_MASK ;
178
+ module_param (gpio_push_pull , int , 0644 );
179
179
MODULE_PARM_DESC (gpio_push_pull , "GPIO push-pull configuration bitmask" );
180
180
181
181
static int cp2112_gpio_direction_input (struct gpio_chip * chip , unsigned offset )
@@ -197,7 +197,7 @@ static int cp2112_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
197
197
goto exit ;
198
198
}
199
199
200
- buf [1 ] &= ~( 1 << offset );
200
+ buf [1 ] &= ~BIT ( offset );
201
201
buf [2 ] = gpio_push_pull ;
202
202
203
203
ret = hid_hw_raw_request (hdev , CP2112_GPIO_CONFIG , buf ,
@@ -227,8 +227,8 @@ static void cp2112_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
227
227
mutex_lock (& dev -> lock );
228
228
229
229
buf [0 ] = CP2112_GPIO_SET ;
230
- buf [1 ] = value ? 0xff : 0 ;
231
- buf [2 ] = 1 << offset ;
230
+ buf [1 ] = value ? CP2112_GPIO_ALL_GPIO_MASK : 0 ;
231
+ buf [2 ] = BIT ( offset ) ;
232
232
233
233
ret = hid_hw_raw_request (hdev , CP2112_GPIO_SET , buf ,
234
234
CP2112_GPIO_SET_LENGTH , HID_FEATURE_REPORT ,
@@ -532,15 +532,13 @@ static int cp2112_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
532
532
hid_dbg (hdev , "I2C %d messages\n" , num );
533
533
534
534
if (num == 1 ) {
535
+ hid_dbg (hdev , "I2C %s %#04x len %d\n" ,
536
+ str_read_write (msgs -> flags & I2C_M_RD ), msgs -> addr , msgs -> len );
535
537
if (msgs -> flags & I2C_M_RD ) {
536
- hid_dbg (hdev , "I2C read %#04x len %d\n" ,
537
- msgs -> addr , msgs -> len );
538
538
read_length = msgs -> len ;
539
539
read_buf = msgs -> buf ;
540
540
count = cp2112_read_req (buf , msgs -> addr , msgs -> len );
541
541
} else {
542
- hid_dbg (hdev , "I2C write %#04x len %d\n" ,
543
- msgs -> addr , msgs -> len );
544
542
count = cp2112_i2c_write_req (buf , msgs -> addr ,
545
543
msgs -> buf , msgs -> len );
546
544
}
@@ -648,7 +646,7 @@ static int cp2112_xfer(struct i2c_adapter *adap, u16 addr,
648
646
int ret ;
649
647
650
648
hid_dbg (hdev , "%s addr 0x%x flags 0x%x cmd 0x%x size %d\n" ,
651
- read_write == I2C_SMBUS_WRITE ? "write" : "read" ,
649
+ str_write_read ( read_write == I2C_SMBUS_WRITE ) ,
652
650
addr , flags , command , size );
653
651
654
652
switch (size ) {
@@ -895,7 +893,7 @@ static ssize_t name##_show(struct device *kdev, \
895
893
int ret = cp2112_get_usb_config(hdev, &cfg); \
896
894
if (ret) \
897
895
return ret; \
898
- return scnprintf (buf, PAGE_SIZE , format, ##__VA_ARGS__); \
896
+ return sysfs_emit (buf, format, ##__VA_ARGS__); \
899
897
} \
900
898
static DEVICE_ATTR_RW(name);
901
899
@@ -946,26 +944,18 @@ CP2112_CONFIG_ATTR(release_version, ({
946
944
947
945
#undef CP2112_CONFIG_ATTR
948
946
949
- struct cp2112_pstring_attribute {
950
- struct device_attribute attr ;
951
- unsigned char report ;
952
- };
953
-
954
- static ssize_t pstr_store (struct device * kdev ,
955
- struct device_attribute * kattr , const char * buf ,
956
- size_t count )
947
+ static ssize_t pstr_store (struct device * kdev , struct device_attribute * kattr ,
948
+ const char * buf , size_t count , int number )
957
949
{
958
950
struct hid_device * hdev = to_hid_device (kdev );
959
- struct cp2112_pstring_attribute * attr =
960
- container_of (kattr , struct cp2112_pstring_attribute , attr );
961
951
struct cp2112_string_report report ;
962
952
int ret ;
963
953
964
954
memset (& report , 0 , sizeof (report ));
965
955
966
956
ret = utf8s_to_utf16s (buf , count , UTF16_LITTLE_ENDIAN ,
967
957
report .string , ARRAY_SIZE (report .string ));
968
- report .report = attr -> report ;
958
+ report .report = number ;
969
959
report .length = ret * sizeof (report .string [0 ]) + 2 ;
970
960
report .type = USB_DT_STRING ;
971
961
@@ -983,17 +973,15 @@ static ssize_t pstr_store(struct device *kdev,
983
973
return count ;
984
974
}
985
975
986
- static ssize_t pstr_show (struct device * kdev ,
987
- struct device_attribute * kattr , char * buf )
976
+ static ssize_t pstr_show (struct device * kdev , struct device_attribute * kattr ,
977
+ char * buf , int number )
988
978
{
989
979
struct hid_device * hdev = to_hid_device (kdev );
990
- struct cp2112_pstring_attribute * attr =
991
- container_of (kattr , struct cp2112_pstring_attribute , attr );
992
980
struct cp2112_string_report report ;
993
981
u8 length ;
994
982
int ret ;
995
983
996
- ret = cp2112_hid_get (hdev , attr -> report , (u8 * )& report .contents ,
984
+ ret = cp2112_hid_get (hdev , number , (u8 * )& report .contents ,
997
985
sizeof (report .contents ), HID_FEATURE_REPORT );
998
986
if (ret < 3 ) {
999
987
hid_err (hdev , "error reading %s string: %d\n" , kattr -> attr .name ,
@@ -1018,10 +1006,16 @@ static ssize_t pstr_show(struct device *kdev,
1018
1006
}
1019
1007
1020
1008
#define CP2112_PSTR_ATTR (name , _report ) \
1021
- static struct cp2112_pstring_attribute dev_attr_##name = { \
1022
- .attr = __ATTR(name, (S_IWUSR | S_IRUGO), pstr_show, pstr_store), \
1023
- .report = _report, \
1024
- };
1009
+ static ssize_t name##_store(struct device *kdev, struct device_attribute *kattr, \
1010
+ const char *buf, size_t count) \
1011
+ { \
1012
+ return pstr_store(kdev, kattr, buf, count, _report); \
1013
+ } \
1014
+ static ssize_t name##_show(struct device *kdev, struct device_attribute *kattr, char *buf) \
1015
+ { \
1016
+ return pstr_show(kdev, kattr, buf, _report); \
1017
+ } \
1018
+ static DEVICE_ATTR_RW(name);
1025
1019
1026
1020
CP2112_PSTR_ATTR (manufacturer , CP2112_MANUFACTURER_STRING );
1027
1021
CP2112_PSTR_ATTR (product , CP2112_PRODUCT_STRING );
@@ -1036,9 +1030,9 @@ static const struct attribute_group cp2112_attr_group = {
1036
1030
& dev_attr_max_power .attr ,
1037
1031
& dev_attr_power_mode .attr ,
1038
1032
& dev_attr_release_version .attr ,
1039
- & dev_attr_manufacturer .attr . attr ,
1040
- & dev_attr_product .attr . attr ,
1041
- & dev_attr_serial .attr . attr ,
1033
+ & dev_attr_manufacturer .attr ,
1034
+ & dev_attr_product .attr ,
1035
+ & dev_attr_serial .attr ,
1042
1036
NULL
1043
1037
}
1044
1038
};
@@ -1063,7 +1057,7 @@ static void chmod_sysfs_attrs(struct hid_device *hdev)
1063
1057
}
1064
1058
1065
1059
for (attr = cp2112_attr_group .attrs ; * attr ; ++ attr ) {
1066
- umode_t mode = (buf [1 ] & 1 ) ? S_IWUSR | S_IRUGO : S_IRUGO ;
1060
+ umode_t mode = (buf [1 ] & 1 ) ? 0644 : 0444 ;
1067
1061
ret = sysfs_chmod_file (& hdev -> dev .kobj , * attr , mode );
1068
1062
if (ret < 0 )
1069
1063
hid_err (hdev , "error chmoding sysfs file %s\n" ,
@@ -1080,16 +1074,20 @@ static void cp2112_gpio_irq_mask(struct irq_data *d)
1080
1074
{
1081
1075
struct gpio_chip * gc = irq_data_get_irq_chip_data (d );
1082
1076
struct cp2112_device * dev = gpiochip_get_data (gc );
1077
+ irq_hw_number_t hwirq = irqd_to_hwirq (d );
1083
1078
1084
- __clear_bit (d -> hwirq , & dev -> irq_mask );
1079
+ __clear_bit (hwirq , & dev -> irq_mask );
1080
+ gpiochip_disable_irq (gc , hwirq );
1085
1081
}
1086
1082
1087
1083
static void cp2112_gpio_irq_unmask (struct irq_data * d )
1088
1084
{
1089
1085
struct gpio_chip * gc = irq_data_get_irq_chip_data (d );
1090
1086
struct cp2112_device * dev = gpiochip_get_data (gc );
1087
+ irq_hw_number_t hwirq = irqd_to_hwirq (d );
1091
1088
1092
- __set_bit (d -> hwirq , & dev -> irq_mask );
1089
+ gpiochip_enable_irq (gc , hwirq );
1090
+ __set_bit (hwirq , & dev -> irq_mask );
1093
1091
}
1094
1092
1095
1093
static void cp2112_gpio_poll_callback (struct work_struct * work )
@@ -1098,7 +1096,6 @@ static void cp2112_gpio_poll_callback(struct work_struct *work)
1098
1096
gpio_poll_worker .work );
1099
1097
struct irq_data * d ;
1100
1098
u8 gpio_mask ;
1101
- u8 virqs = (u8 )dev -> irq_mask ;
1102
1099
u32 irq_type ;
1103
1100
int irq , virq , ret ;
1104
1101
@@ -1109,15 +1106,10 @@ static void cp2112_gpio_poll_callback(struct work_struct *work)
1109
1106
goto exit ;
1110
1107
1111
1108
gpio_mask = ret ;
1112
-
1113
- while (virqs ) {
1114
- virq = ffs (virqs ) - 1 ;
1115
- virqs &= ~BIT (virq );
1116
-
1117
- if (!dev -> gc .to_irq )
1118
- break ;
1119
-
1120
- irq = dev -> gc .to_irq (& dev -> gc , virq );
1109
+ for_each_set_bit (virq , & dev -> irq_mask , CP2112_GPIO_MAX_GPIO ) {
1110
+ irq = irq_find_mapping (dev -> gc .irq .domain , virq );
1111
+ if (!irq )
1112
+ continue ;
1121
1113
1122
1114
d = irq_get_irq_data (irq );
1123
1115
if (!d )
@@ -1175,6 +1167,7 @@ static void cp2112_gpio_irq_shutdown(struct irq_data *d)
1175
1167
struct gpio_chip * gc = irq_data_get_irq_chip_data (d );
1176
1168
struct cp2112_device * dev = gpiochip_get_data (gc );
1177
1169
1170
+ cp2112_gpio_irq_mask (d );
1178
1171
cancel_delayed_work_sync (& dev -> gpio_poll_worker );
1179
1172
}
1180
1173
@@ -1183,50 +1176,17 @@ static int cp2112_gpio_irq_type(struct irq_data *d, unsigned int type)
1183
1176
return 0 ;
1184
1177
}
1185
1178
1186
- static int __maybe_unused cp2112_allocate_irq (struct cp2112_device * dev ,
1187
- int pin )
1188
- {
1189
- int ret ;
1190
-
1191
- if (dev -> desc [pin ])
1192
- return - EINVAL ;
1193
-
1194
- dev -> desc [pin ] = gpiochip_request_own_desc (& dev -> gc , pin ,
1195
- "HID/I2C:Event" ,
1196
- GPIO_ACTIVE_HIGH ,
1197
- GPIOD_IN );
1198
- if (IS_ERR (dev -> desc [pin ])) {
1199
- dev_err (dev -> gc .parent , "Failed to request GPIO\n" );
1200
- return PTR_ERR (dev -> desc [pin ]);
1201
- }
1202
-
1203
- ret = cp2112_gpio_direction_input (& dev -> gc , pin );
1204
- if (ret < 0 ) {
1205
- dev_err (dev -> gc .parent , "Failed to set GPIO to input dir\n" );
1206
- goto err_desc ;
1207
- }
1208
-
1209
- ret = gpiochip_lock_as_irq (& dev -> gc , pin );
1210
- if (ret ) {
1211
- dev_err (dev -> gc .parent , "Failed to lock GPIO as interrupt\n" );
1212
- goto err_desc ;
1213
- }
1214
-
1215
- ret = gpiod_to_irq (dev -> desc [pin ]);
1216
- if (ret < 0 ) {
1217
- dev_err (dev -> gc .parent , "Failed to translate GPIO to IRQ\n" );
1218
- goto err_lock ;
1219
- }
1220
-
1221
- return ret ;
1222
-
1223
- err_lock :
1224
- gpiochip_unlock_as_irq (& dev -> gc , pin );
1225
- err_desc :
1226
- gpiochip_free_own_desc (dev -> desc [pin ]);
1227
- dev -> desc [pin ] = NULL ;
1228
- return ret ;
1229
- }
1179
+ static const struct irq_chip cp2112_gpio_irqchip = {
1180
+ .name = "cp2112-gpio" ,
1181
+ .irq_startup = cp2112_gpio_irq_startup ,
1182
+ .irq_shutdown = cp2112_gpio_irq_shutdown ,
1183
+ .irq_ack = cp2112_gpio_irq_ack ,
1184
+ .irq_mask = cp2112_gpio_irq_mask ,
1185
+ .irq_unmask = cp2112_gpio_irq_unmask ,
1186
+ .irq_set_type = cp2112_gpio_irq_type ,
1187
+ .flags = IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_IMMUTABLE ,
1188
+ GPIOCHIP_IRQ_RESOURCE_HELPERS ,
1189
+ };
1230
1190
1231
1191
static int cp2112_probe (struct hid_device * hdev , const struct hid_device_id * id )
1232
1192
{
@@ -1333,21 +1293,12 @@ static int cp2112_probe(struct hid_device *hdev, const struct hid_device_id *id)
1333
1293
dev -> gc .set = cp2112_gpio_set ;
1334
1294
dev -> gc .get = cp2112_gpio_get ;
1335
1295
dev -> gc .base = -1 ;
1336
- dev -> gc .ngpio = 8 ;
1296
+ dev -> gc .ngpio = CP2112_GPIO_MAX_GPIO ;
1337
1297
dev -> gc .can_sleep = 1 ;
1338
1298
dev -> gc .parent = & hdev -> dev ;
1339
1299
1340
- dev -> irq .name = "cp2112-gpio" ;
1341
- dev -> irq .irq_startup = cp2112_gpio_irq_startup ;
1342
- dev -> irq .irq_shutdown = cp2112_gpio_irq_shutdown ;
1343
- dev -> irq .irq_ack = cp2112_gpio_irq_ack ;
1344
- dev -> irq .irq_mask = cp2112_gpio_irq_mask ;
1345
- dev -> irq .irq_unmask = cp2112_gpio_irq_unmask ;
1346
- dev -> irq .irq_set_type = cp2112_gpio_irq_type ;
1347
- dev -> irq .flags = IRQCHIP_MASK_ON_SUSPEND ;
1348
-
1349
1300
girq = & dev -> gc .irq ;
1350
- girq -> chip = & dev -> irq ;
1301
+ gpio_irq_chip_set_chip ( girq , & cp2112_gpio_irqchip ) ;
1351
1302
/* The event comes from the outside so no parent handler */
1352
1303
girq -> parent_handler = NULL ;
1353
1304
girq -> num_parents = 0 ;
@@ -1389,7 +1340,6 @@ static int cp2112_probe(struct hid_device *hdev, const struct hid_device_id *id)
1389
1340
static void cp2112_remove (struct hid_device * hdev )
1390
1341
{
1391
1342
struct cp2112_device * dev = hid_get_drvdata (hdev );
1392
- int i ;
1393
1343
1394
1344
sysfs_remove_group (& hdev -> dev .kobj , & cp2112_attr_group );
1395
1345
i2c_del_adapter (& dev -> adap );
@@ -1399,11 +1349,6 @@ static void cp2112_remove(struct hid_device *hdev)
1399
1349
cancel_delayed_work_sync (& dev -> gpio_poll_worker );
1400
1350
}
1401
1351
1402
- for (i = 0 ; i < ARRAY_SIZE (dev -> desc ); i ++ ) {
1403
- gpiochip_unlock_as_irq (& dev -> gc , i );
1404
- gpiochip_free_own_desc (dev -> desc [i ]);
1405
- }
1406
-
1407
1352
gpiochip_remove (& dev -> gc );
1408
1353
/* i2c_del_adapter has finished removing all i2c devices from our
1409
1354
* adapter. Well behaved devices should no longer call our cp2112_xfer
0 commit comments