Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 1 | /* |
Greg Kroah-Hartman | 4efe606 | 2014-11-17 16:55:54 -0800 | [diff] [blame] | 2 | * Greybus Vibrator protocol driver. |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 3 | * |
| 4 | * Copyright 2014 Google Inc. |
Alex Elder | a46e967 | 2014-12-12 12:08:42 -0600 | [diff] [blame] | 5 | * Copyright 2014 Linaro Ltd. |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 6 | * |
| 7 | * Released under the GPLv2 only. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/device.h> |
| 14 | #include <linux/kdev_t.h> |
Greg Kroah-Hartman | 396671b | 2014-11-17 17:12:50 -0800 | [diff] [blame] | 15 | #include <linux/idr.h> |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 16 | #include "greybus.h" |
| 17 | |
| 18 | struct gb_vibrator_device { |
| 19 | struct gb_connection *connection; |
| 20 | struct device *dev; |
Greg Kroah-Hartman | 396671b | 2014-11-17 17:12:50 -0800 | [diff] [blame] | 21 | int minor; /* vibrator minor number */ |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 22 | }; |
| 23 | |
Greg Kroah-Hartman | 42a9466 | 2014-11-19 10:36:23 -0800 | [diff] [blame] | 24 | /* Version of the Greybus vibrator protocol we support */ |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 25 | #define GB_VIBRATOR_VERSION_MAJOR 0x00 |
| 26 | #define GB_VIBRATOR_VERSION_MINOR 0x01 |
| 27 | |
Alex Elder | 6d65337 | 2015-05-07 13:03:52 -0500 | [diff] [blame] | 28 | /* Greybus Vibrator operation types */ |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 29 | #define GB_VIBRATOR_TYPE_ON 0x02 |
| 30 | #define GB_VIBRATOR_TYPE_OFF 0x03 |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 31 | |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 32 | struct gb_vibrator_on_request { |
| 33 | __le16 timeout_ms; |
| 34 | }; |
| 35 | |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 36 | static int turn_on(struct gb_vibrator_device *vib, u16 timeout_ms) |
| 37 | { |
Greg Kroah-Hartman | ac3d249 | 2014-11-23 17:45:20 -0800 | [diff] [blame] | 38 | struct gb_vibrator_on_request request; |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 39 | |
Greg Kroah-Hartman | ac3d249 | 2014-11-23 17:45:20 -0800 | [diff] [blame] | 40 | request.timeout_ms = cpu_to_le16(timeout_ms); |
| 41 | return gb_operation_sync(vib->connection, GB_VIBRATOR_TYPE_ON, |
| 42 | &request, sizeof(request), NULL, 0); |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | static int turn_off(struct gb_vibrator_device *vib) |
| 46 | { |
Greg Kroah-Hartman | ac3d249 | 2014-11-23 17:45:20 -0800 | [diff] [blame] | 47 | return gb_operation_sync(vib->connection, GB_VIBRATOR_TYPE_OFF, |
| 48 | NULL, 0, NULL, 0); |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | static ssize_t timeout_store(struct device *dev, struct device_attribute *attr, |
| 52 | const char *buf, size_t count) |
| 53 | { |
| 54 | struct gb_vibrator_device *vib = dev_get_drvdata(dev); |
| 55 | unsigned long val; |
| 56 | int retval; |
| 57 | |
| 58 | retval = kstrtoul(buf, 10, &val); |
| 59 | if (retval < 0) { |
| 60 | dev_err(dev, "could not parse timeout value %d\n", retval); |
| 61 | return retval; |
| 62 | } |
| 63 | |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 64 | if (val) |
| 65 | retval = turn_on(vib, (u16)val); |
| 66 | else |
| 67 | retval = turn_off(vib); |
| 68 | if (retval) |
| 69 | return retval; |
| 70 | |
| 71 | return count; |
| 72 | } |
| 73 | static DEVICE_ATTR_WO(timeout); |
| 74 | |
| 75 | static struct attribute *vibrator_attrs[] = { |
| 76 | &dev_attr_timeout.attr, |
| 77 | NULL, |
| 78 | }; |
| 79 | ATTRIBUTE_GROUPS(vibrator); |
| 80 | |
| 81 | static struct class vibrator_class = { |
| 82 | .name = "vibrator", |
| 83 | .owner = THIS_MODULE, |
| 84 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0) |
| 85 | .dev_groups = vibrator_groups, |
| 86 | #endif |
| 87 | }; |
| 88 | |
Greg Kroah-Hartman | 6b17492 | 2015-05-14 10:39:35 -0700 | [diff] [blame] | 89 | static DEFINE_IDA(minors); |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 90 | |
| 91 | static int gb_vibrator_connection_init(struct gb_connection *connection) |
| 92 | { |
| 93 | struct gb_vibrator_device *vib; |
| 94 | struct device *dev; |
| 95 | int retval; |
| 96 | |
| 97 | vib = kzalloc(sizeof(*vib), GFP_KERNEL); |
| 98 | if (!vib) |
| 99 | return -ENOMEM; |
| 100 | |
| 101 | vib->connection = connection; |
Alex Elder | 93bbe85 | 2014-12-03 12:27:42 -0600 | [diff] [blame] | 102 | connection->private = vib; |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 103 | |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 104 | /* |
Greg Kroah-Hartman | 396671b | 2014-11-17 17:12:50 -0800 | [diff] [blame] | 105 | * For now we create a device in sysfs for the vibrator, but odds are |
| 106 | * there is a "real" device somewhere in the kernel for this, but I |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 107 | * can't find it at the moment... |
| 108 | */ |
Greg Kroah-Hartman | 6b17492 | 2015-05-14 10:39:35 -0700 | [diff] [blame] | 109 | vib->minor = ida_simple_get(&minors, 0, 0, GFP_KERNEL); |
Greg Kroah-Hartman | 396671b | 2014-11-17 17:12:50 -0800 | [diff] [blame] | 110 | if (vib->minor < 0) { |
| 111 | retval = vib->minor; |
| 112 | goto error; |
| 113 | } |
Greg Kroah-Hartman | 5ae2f55 | 2015-10-14 11:12:06 -0700 | [diff] [blame^] | 114 | dev = device_create(&vibrator_class, &connection->bundle->dev, |
| 115 | MKDEV(0, 0), vib, "vibrator%d", vib->minor); |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 116 | if (IS_ERR(dev)) { |
| 117 | retval = -EINVAL; |
Greg Kroah-Hartman | 6b17492 | 2015-05-14 10:39:35 -0700 | [diff] [blame] | 118 | goto err_ida_remove; |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 119 | } |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 120 | vib->dev = dev; |
| 121 | |
| 122 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(3,11,0) |
| 123 | /* |
Greg Kroah-Hartman | 4efe606 | 2014-11-17 16:55:54 -0800 | [diff] [blame] | 124 | * Newer kernels handle this in a race-free manner, by the dev_groups |
| 125 | * field in the struct class up above. But for older kernels, we need |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 126 | * to "open code this :( |
| 127 | */ |
| 128 | retval = sysfs_create_group(&dev->kobj, vibrator_groups[0]); |
| 129 | if (retval) { |
| 130 | device_unregister(dev); |
Greg Kroah-Hartman | 6b17492 | 2015-05-14 10:39:35 -0700 | [diff] [blame] | 131 | goto err_ida_remove; |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 132 | } |
| 133 | #endif |
| 134 | |
| 135 | return 0; |
| 136 | |
Greg Kroah-Hartman | 6b17492 | 2015-05-14 10:39:35 -0700 | [diff] [blame] | 137 | err_ida_remove: |
| 138 | ida_simple_remove(&minors, vib->minor); |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 139 | error: |
| 140 | kfree(vib); |
| 141 | return retval; |
| 142 | } |
| 143 | |
| 144 | static void gb_vibrator_connection_exit(struct gb_connection *connection) |
| 145 | { |
| 146 | struct gb_vibrator_device *vib = connection->private; |
| 147 | |
| 148 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(3,11,0) |
| 149 | sysfs_remove_group(&vib->dev->kobj, vibrator_groups[0]); |
| 150 | #endif |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 151 | device_unregister(vib->dev); |
Johan Hovold | d7849bf | 2015-09-14 20:19:02 +0200 | [diff] [blame] | 152 | ida_simple_remove(&minors, vib->minor); |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 153 | kfree(vib); |
| 154 | } |
| 155 | |
| 156 | static struct gb_protocol vibrator_protocol = { |
Greg Kroah-Hartman | 7422a1e | 2014-12-24 13:01:45 -0800 | [diff] [blame] | 157 | .name = "vibrator", |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 158 | .id = GREYBUS_PROTOCOL_VIBRATOR, |
Viresh Kumar | f0a1698 | 2015-08-08 10:25:41 +0530 | [diff] [blame] | 159 | .major = GB_VIBRATOR_VERSION_MAJOR, |
| 160 | .minor = GB_VIBRATOR_VERSION_MINOR, |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 161 | .connection_init = gb_vibrator_connection_init, |
| 162 | .connection_exit = gb_vibrator_connection_exit, |
| 163 | .request_recv = NULL, /* no incoming requests */ |
| 164 | }; |
| 165 | |
Greg Kroah-Hartman | 66b676f | 2014-12-24 13:01:41 -0800 | [diff] [blame] | 166 | static __init int protocol_init(void) |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 167 | { |
| 168 | int retval; |
| 169 | |
| 170 | retval = class_register(&vibrator_class); |
| 171 | if (retval) |
| 172 | return retval; |
| 173 | |
Johan Hovold | d4efa68 | 2015-10-13 19:10:27 +0200 | [diff] [blame] | 174 | retval = gb_protocol_register(&vibrator_protocol); |
| 175 | if (retval) |
| 176 | goto err_class_unregister; |
| 177 | |
| 178 | return 0; |
| 179 | |
| 180 | err_class_unregister: |
| 181 | class_unregister(&vibrator_class); |
| 182 | |
| 183 | return retval; |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 184 | } |
Viresh Kumar | 0d34be7 | 2015-03-24 20:14:28 +0530 | [diff] [blame] | 185 | module_init(protocol_init); |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 186 | |
Greg Kroah-Hartman | 66b676f | 2014-12-24 13:01:41 -0800 | [diff] [blame] | 187 | static __exit void protocol_exit(void) |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 188 | { |
| 189 | gb_protocol_deregister(&vibrator_protocol); |
| 190 | class_unregister(&vibrator_class); |
Greg Kroah-Hartman | 5c1ac69 | 2015-07-08 10:44:09 -0700 | [diff] [blame] | 191 | ida_destroy(&minors); |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 192 | } |
Greg Kroah-Hartman | 66b676f | 2014-12-24 13:01:41 -0800 | [diff] [blame] | 193 | module_exit(protocol_exit); |
| 194 | |
| 195 | MODULE_LICENSE("GPL v2"); |