Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 1 | /* |
| 2 | * HID class driver for the Greybus. |
| 3 | * |
| 4 | * Copyright 2014 Google Inc. |
| 5 | * Copyright 2014 Linaro Ltd. |
| 6 | * |
| 7 | * Released under the GPLv2 only. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/bitops.h> |
| 11 | #include <linux/hid.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/mutex.h> |
| 15 | #include <linux/slab.h> |
| 16 | |
| 17 | #include "greybus.h" |
| 18 | |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 19 | /* Greybus HID device's structure */ |
| 20 | struct gb_hid { |
| 21 | struct gb_connection *connection; |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 22 | |
| 23 | struct hid_device *hid; |
| 24 | struct gb_hid_desc_response hdesc; |
| 25 | |
| 26 | unsigned long flags; |
| 27 | #define GB_HID_STARTED 0x01 |
| 28 | #define GB_HID_READ_PENDING 0x04 |
| 29 | |
| 30 | unsigned int bufsize; |
| 31 | char *inbuf; |
| 32 | }; |
| 33 | |
| 34 | static DEFINE_MUTEX(gb_hid_open_mutex); |
| 35 | |
Viresh Kumar | d79ae49 | 2015-08-08 10:25:32 +0530 | [diff] [blame] | 36 | /* Routines to get controller's information over greybus */ |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 37 | |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 38 | /* Operations performed on greybus */ |
| 39 | static int gb_hid_get_desc(struct gb_hid *ghid) |
| 40 | { |
| 41 | return gb_operation_sync(ghid->connection, GB_HID_TYPE_GET_DESC, NULL, |
| 42 | 0, &ghid->hdesc, sizeof(ghid->hdesc)); |
| 43 | } |
| 44 | |
| 45 | static int gb_hid_get_report_desc(struct gb_hid *ghid, char *rdesc) |
| 46 | { |
| 47 | return gb_operation_sync(ghid->connection, GB_HID_TYPE_GET_REPORT_DESC, |
| 48 | NULL, 0, rdesc, |
| 49 | le16_to_cpu(ghid->hdesc.wReportDescLength)); |
| 50 | } |
| 51 | |
| 52 | static int gb_hid_set_power(struct gb_hid *ghid, int type) |
| 53 | { |
| 54 | return gb_operation_sync(ghid->connection, type, NULL, 0, NULL, 0); |
| 55 | } |
| 56 | |
| 57 | static int gb_hid_get_report(struct gb_hid *ghid, u8 report_type, u8 report_id, |
| 58 | unsigned char *buf, int len) |
| 59 | { |
| 60 | struct gb_hid_get_report_request request; |
| 61 | |
| 62 | request.report_type = report_type; |
| 63 | request.report_id = report_id; |
| 64 | |
| 65 | return gb_operation_sync(ghid->connection, GB_HID_TYPE_GET_REPORT, |
| 66 | &request, sizeof(request), buf, len); |
| 67 | } |
| 68 | |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 69 | static int gb_hid_set_report(struct gb_hid *ghid, u8 report_type, u8 report_id, |
Viresh Kumar | 292cca9 | 2015-03-18 11:41:39 +0530 | [diff] [blame] | 70 | unsigned char *buf, int len) |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 71 | { |
| 72 | struct gb_hid_set_report_request *request; |
| 73 | struct gb_operation *operation; |
| 74 | int ret, size = sizeof(*request) + len - 1; |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 75 | |
Viresh Kumar | 292cca9 | 2015-03-18 11:41:39 +0530 | [diff] [blame] | 76 | operation = gb_operation_create(ghid->connection, |
Johan Hovold | e420721 | 2015-07-01 12:37:22 +0200 | [diff] [blame] | 77 | GB_HID_TYPE_SET_REPORT, size, 0, |
| 78 | GFP_KERNEL); |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 79 | if (!operation) |
| 80 | return -ENOMEM; |
| 81 | |
| 82 | request = operation->request->payload; |
| 83 | request->report_type = report_type; |
| 84 | request->report_id = report_id; |
| 85 | memcpy(request->report, buf, len); |
| 86 | |
| 87 | ret = gb_operation_request_send_sync(operation); |
Johan Hovold | b67f2d1 | 2015-03-27 12:45:44 +0100 | [diff] [blame] | 88 | if (ret) { |
Greg Kroah-Hartman | b2a637d | 2015-10-14 11:16:24 -0700 | [diff] [blame] | 89 | dev_err(&operation->connection->bundle->dev, |
Johan Hovold | b67f2d1 | 2015-03-27 12:45:44 +0100 | [diff] [blame] | 90 | "failed to set report: %d\n", ret); |
| 91 | } else { |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 92 | ret = len; |
Johan Hovold | b67f2d1 | 2015-03-27 12:45:44 +0100 | [diff] [blame] | 93 | } |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 94 | |
Johan Hovold | 6ab1ce4 | 2015-09-26 17:59:15 -0700 | [diff] [blame] | 95 | gb_operation_put(operation); |
| 96 | |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 97 | return ret; |
| 98 | } |
| 99 | |
Johan Hovold | 4324282d | 2016-01-21 17:34:23 +0100 | [diff] [blame] | 100 | static int gb_hid_request_handler(struct gb_operation *op) |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 101 | { |
| 102 | struct gb_connection *connection = op->connection; |
| 103 | struct gb_hid *ghid = connection->private; |
| 104 | struct gb_hid_input_report_request *request = op->request->payload; |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 105 | |
Johan Hovold | 4324282d | 2016-01-21 17:34:23 +0100 | [diff] [blame] | 106 | if (op->type != GB_HID_TYPE_IRQ_EVENT) { |
Greg Kroah-Hartman | b2a637d | 2015-10-14 11:16:24 -0700 | [diff] [blame] | 107 | dev_err(&connection->bundle->dev, |
Johan Hovold | b67f2d1 | 2015-03-27 12:45:44 +0100 | [diff] [blame] | 108 | "unsupported unsolicited request\n"); |
Johan Hovold | 973ccfd | 2015-03-27 12:45:49 +0100 | [diff] [blame] | 109 | return -EINVAL; |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 110 | } |
| 111 | |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 112 | if (test_bit(GB_HID_STARTED, &ghid->flags)) |
| 113 | hid_input_report(ghid->hid, HID_INPUT_REPORT, |
Viresh Kumar | 581baac | 2015-04-03 12:00:48 +0530 | [diff] [blame] | 114 | request->report, op->request->payload_size, 1); |
Johan Hovold | ecf47ab | 2015-03-27 12:45:45 +0100 | [diff] [blame] | 115 | |
Johan Hovold | 973ccfd | 2015-03-27 12:45:49 +0100 | [diff] [blame] | 116 | return 0; |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | |
| 120 | static int gb_hid_report_len(struct hid_report *report) |
| 121 | { |
| 122 | return ((report->size - 1) >> 3) + 1 + |
| 123 | report->device->report_enum[report->type].numbered; |
| 124 | } |
| 125 | |
| 126 | static void gb_hid_find_max_report(struct hid_device *hid, unsigned int type, |
| 127 | unsigned int *max) |
| 128 | { |
| 129 | struct hid_report *report; |
| 130 | unsigned int size; |
| 131 | |
| 132 | list_for_each_entry(report, &hid->report_enum[type].report_list, list) { |
| 133 | size = gb_hid_report_len(report); |
| 134 | if (*max < size) |
| 135 | *max = size; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | static void gb_hid_free_buffers(struct gb_hid *ghid) |
| 140 | { |
| 141 | kfree(ghid->inbuf); |
| 142 | ghid->inbuf = NULL; |
| 143 | ghid->bufsize = 0; |
| 144 | } |
| 145 | |
| 146 | static int gb_hid_alloc_buffers(struct gb_hid *ghid, size_t bufsize) |
| 147 | { |
| 148 | ghid->inbuf = kzalloc(bufsize, GFP_KERNEL); |
| 149 | if (!ghid->inbuf) |
| 150 | return -ENOMEM; |
| 151 | |
| 152 | ghid->bufsize = bufsize; |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | /* Routines dealing with reports */ |
| 158 | static void gb_hid_init_report(struct gb_hid *ghid, struct hid_report *report) |
| 159 | { |
| 160 | unsigned int size; |
| 161 | |
| 162 | size = gb_hid_report_len(report); |
| 163 | if (gb_hid_get_report(ghid, report->type, report->id, ghid->inbuf, |
| 164 | size)) |
| 165 | return; |
| 166 | |
| 167 | /* |
| 168 | * hid->driver_lock is held as we are in probe function, |
| 169 | * we just need to setup the input fields, so using |
| 170 | * hid_report_raw_event is safe. |
| 171 | */ |
| 172 | hid_report_raw_event(ghid->hid, report->type, ghid->inbuf, size, 1); |
| 173 | } |
| 174 | |
| 175 | static void gb_hid_init_reports(struct gb_hid *ghid) |
| 176 | { |
| 177 | struct hid_device *hid = ghid->hid; |
| 178 | struct hid_report *report; |
| 179 | |
| 180 | list_for_each_entry(report, |
| 181 | &hid->report_enum[HID_INPUT_REPORT].report_list, list) |
| 182 | gb_hid_init_report(ghid, report); |
| 183 | |
| 184 | list_for_each_entry(report, |
| 185 | &hid->report_enum[HID_FEATURE_REPORT].report_list, list) |
| 186 | gb_hid_init_report(ghid, report); |
| 187 | } |
| 188 | |
| 189 | static int __gb_hid_get_raw_report(struct hid_device *hid, |
| 190 | unsigned char report_number, __u8 *buf, size_t count, |
| 191 | unsigned char report_type) |
| 192 | { |
| 193 | struct gb_hid *ghid = hid->driver_data; |
| 194 | int ret; |
| 195 | |
| 196 | if (report_type == HID_OUTPUT_REPORT) |
| 197 | return -EINVAL; |
| 198 | |
| 199 | ret = gb_hid_get_report(ghid, report_type, report_number, buf, count); |
| 200 | if (!ret) |
| 201 | ret = count; |
| 202 | |
| 203 | return ret; |
| 204 | } |
| 205 | |
| 206 | static int __gb_hid_output_raw_report(struct hid_device *hid, __u8 *buf, |
Viresh Kumar | 292cca9 | 2015-03-18 11:41:39 +0530 | [diff] [blame] | 207 | size_t len, unsigned char report_type) |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 208 | { |
| 209 | struct gb_hid *ghid = hid->driver_data; |
| 210 | int report_id = buf[0]; |
| 211 | int ret; |
| 212 | |
| 213 | if (report_type == HID_INPUT_REPORT) |
| 214 | return -EINVAL; |
| 215 | |
| 216 | if (report_id) { |
| 217 | buf++; |
| 218 | len--; |
| 219 | } |
| 220 | |
Viresh Kumar | 292cca9 | 2015-03-18 11:41:39 +0530 | [diff] [blame] | 221 | ret = gb_hid_set_report(ghid, report_type, report_id, buf, len); |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 222 | if (report_id && ret >= 0) |
| 223 | ret++; /* add report_id to the number of transfered bytes */ |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | static int gb_hid_raw_request(struct hid_device *hid, unsigned char reportnum, |
| 229 | __u8 *buf, size_t len, unsigned char rtype, |
| 230 | int reqtype) |
| 231 | { |
| 232 | switch (reqtype) { |
| 233 | case HID_REQ_GET_REPORT: |
| 234 | return __gb_hid_get_raw_report(hid, reportnum, buf, len, rtype); |
| 235 | case HID_REQ_SET_REPORT: |
| 236 | if (buf[0] != reportnum) |
| 237 | return -EINVAL; |
Viresh Kumar | 292cca9 | 2015-03-18 11:41:39 +0530 | [diff] [blame] | 238 | return __gb_hid_output_raw_report(hid, buf, len, rtype); |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 239 | default: |
| 240 | return -EIO; |
| 241 | } |
| 242 | } |
| 243 | |
Viresh Kumar | 292cca9 | 2015-03-18 11:41:39 +0530 | [diff] [blame] | 244 | #if LINUX_VERSION_CODE < KERNEL_VERSION(3,15,0) |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 245 | static int gb_hid_get_raw_report(struct hid_device *hid, |
| 246 | unsigned char reportnum, __u8 *buf, |
| 247 | size_t len, unsigned char rtype) |
| 248 | { |
| 249 | return gb_hid_raw_request(hid, reportnum, buf, len, rtype, |
| 250 | HID_REQ_GET_REPORT); |
| 251 | } |
| 252 | |
| 253 | static int gb_hid_output_raw_report(struct hid_device *hid, __u8 *buf, |
| 254 | size_t len, unsigned char rtype) |
| 255 | { |
| 256 | return gb_hid_raw_request(hid, buf[0], buf, len, rtype, |
| 257 | HID_REQ_SET_REPORT); |
| 258 | } |
| 259 | #endif |
| 260 | |
| 261 | /* HID Callbacks */ |
| 262 | static int gb_hid_parse(struct hid_device *hid) |
| 263 | { |
| 264 | struct gb_hid *ghid = hid->driver_data; |
| 265 | unsigned int rsize; |
| 266 | char *rdesc; |
| 267 | int ret; |
| 268 | |
| 269 | rsize = le16_to_cpu(ghid->hdesc.wReportDescLength); |
| 270 | if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) { |
| 271 | dbg_hid("weird size of report descriptor (%u)\n", rsize); |
| 272 | return -EINVAL; |
| 273 | } |
| 274 | |
| 275 | rdesc = kzalloc(rsize, GFP_KERNEL); |
| 276 | if (!rdesc) { |
| 277 | dbg_hid("couldn't allocate rdesc memory\n"); |
| 278 | return -ENOMEM; |
| 279 | } |
| 280 | |
| 281 | ret = gb_hid_get_report_desc(ghid, rdesc); |
| 282 | if (ret) { |
| 283 | hid_err(hid, "reading report descriptor failed\n"); |
| 284 | goto free_rdesc; |
| 285 | } |
| 286 | |
| 287 | ret = hid_parse_report(hid, rdesc, rsize); |
| 288 | if (ret) |
| 289 | dbg_hid("parsing report descriptor failed\n"); |
| 290 | |
| 291 | free_rdesc: |
| 292 | kfree(rdesc); |
| 293 | |
| 294 | return ret; |
| 295 | } |
| 296 | |
| 297 | static int gb_hid_start(struct hid_device *hid) |
| 298 | { |
| 299 | struct gb_hid *ghid = hid->driver_data; |
| 300 | unsigned int bufsize = HID_MIN_BUFFER_SIZE; |
| 301 | int ret; |
| 302 | |
| 303 | gb_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize); |
| 304 | gb_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize); |
| 305 | gb_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize); |
| 306 | |
| 307 | if (bufsize > HID_MAX_BUFFER_SIZE) |
| 308 | bufsize = HID_MAX_BUFFER_SIZE; |
| 309 | |
| 310 | ret = gb_hid_alloc_buffers(ghid, bufsize); |
| 311 | if (ret) |
| 312 | return ret; |
| 313 | |
| 314 | if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS)) |
| 315 | gb_hid_init_reports(ghid); |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | static void gb_hid_stop(struct hid_device *hid) |
| 321 | { |
| 322 | struct gb_hid *ghid = hid->driver_data; |
| 323 | |
| 324 | gb_hid_free_buffers(ghid); |
| 325 | } |
| 326 | |
| 327 | static int gb_hid_open(struct hid_device *hid) |
| 328 | { |
| 329 | struct gb_hid *ghid = hid->driver_data; |
| 330 | int ret = 0; |
| 331 | |
| 332 | mutex_lock(&gb_hid_open_mutex); |
| 333 | if (!hid->open++) { |
| 334 | ret = gb_hid_set_power(ghid, GB_HID_TYPE_PWR_ON); |
| 335 | if (ret < 0) |
| 336 | hid->open--; |
| 337 | else |
| 338 | set_bit(GB_HID_STARTED, &ghid->flags); |
| 339 | } |
| 340 | mutex_unlock(&gb_hid_open_mutex); |
| 341 | |
| 342 | return ret; |
| 343 | } |
| 344 | |
| 345 | static void gb_hid_close(struct hid_device *hid) |
| 346 | { |
| 347 | struct gb_hid *ghid = hid->driver_data; |
| 348 | |
| 349 | /* |
| 350 | * Protecting hid->open to make sure we don't restart data acquistion |
| 351 | * due to a resumption we no longer care about.. |
| 352 | */ |
| 353 | mutex_lock(&gb_hid_open_mutex); |
| 354 | if (!--hid->open) { |
| 355 | clear_bit(GB_HID_STARTED, &ghid->flags); |
| 356 | |
| 357 | /* Save some power */ |
| 358 | WARN_ON(gb_hid_set_power(ghid, GB_HID_TYPE_PWR_OFF)); |
| 359 | } |
| 360 | mutex_unlock(&gb_hid_open_mutex); |
| 361 | } |
| 362 | |
| 363 | static int gb_hid_power(struct hid_device *hid, int lvl) |
| 364 | { |
| 365 | struct gb_hid *ghid = hid->driver_data; |
| 366 | |
| 367 | switch (lvl) { |
| 368 | case PM_HINT_FULLON: |
| 369 | return gb_hid_set_power(ghid, GB_HID_TYPE_PWR_ON); |
| 370 | case PM_HINT_NORMAL: |
| 371 | return gb_hid_set_power(ghid, GB_HID_TYPE_PWR_OFF); |
| 372 | } |
| 373 | |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | /* HID structure to pass callbacks */ |
| 378 | static struct hid_ll_driver gb_hid_ll_driver = { |
| 379 | .parse = gb_hid_parse, |
| 380 | .start = gb_hid_start, |
| 381 | .stop = gb_hid_stop, |
| 382 | .open = gb_hid_open, |
| 383 | .close = gb_hid_close, |
| 384 | .power = gb_hid_power, |
| 385 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,15,0) |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 386 | .raw_request = gb_hid_raw_request, |
| 387 | #endif |
| 388 | }; |
| 389 | |
| 390 | static int gb_hid_init(struct gb_hid *ghid) |
| 391 | { |
| 392 | struct hid_device *hid = ghid->hid; |
| 393 | int ret; |
| 394 | |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 395 | ret = gb_hid_get_desc(ghid); |
| 396 | if (ret) |
| 397 | return ret; |
| 398 | |
| 399 | hid->version = le16_to_cpu(ghid->hdesc.bcdHID); |
| 400 | hid->vendor = le16_to_cpu(ghid->hdesc.wVendorID); |
| 401 | hid->product = le16_to_cpu(ghid->hdesc.wProductID); |
| 402 | hid->country = ghid->hdesc.bCountryCode; |
| 403 | |
| 404 | hid->driver_data = ghid; |
| 405 | hid->ll_driver = &gb_hid_ll_driver; |
Greg Kroah-Hartman | b2a637d | 2015-10-14 11:16:24 -0700 | [diff] [blame] | 406 | hid->dev.parent = &ghid->connection->bundle->dev; |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 407 | #if LINUX_VERSION_CODE < KERNEL_VERSION(3,15,0) |
| 408 | hid->hid_get_raw_report = gb_hid_get_raw_report; |
| 409 | hid->hid_output_raw_report = gb_hid_output_raw_report; |
| 410 | #endif |
| 411 | // hid->bus = BUS_GREYBUS; /* Need a bustype for GREYBUS in <linux/input.h> */ |
| 412 | |
| 413 | /* Set HID device's name */ |
Viresh Kumar | 2f3db92 | 2015-12-04 21:30:09 +0530 | [diff] [blame] | 414 | snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X", |
Greg Kroah-Hartman | b2a637d | 2015-10-14 11:16:24 -0700 | [diff] [blame] | 415 | dev_name(&ghid->connection->bundle->dev), |
| 416 | hid->vendor, hid->product); |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 417 | |
| 418 | return 0; |
| 419 | } |
| 420 | |
Johan Hovold | 4324282d | 2016-01-21 17:34:23 +0100 | [diff] [blame] | 421 | static int gb_hid_probe(struct gb_bundle *bundle, |
| 422 | const struct greybus_bundle_id *id) |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 423 | { |
Johan Hovold | 4324282d | 2016-01-21 17:34:23 +0100 | [diff] [blame] | 424 | struct greybus_descriptor_cport *cport_desc; |
| 425 | struct gb_connection *connection; |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 426 | struct hid_device *hid; |
| 427 | struct gb_hid *ghid; |
| 428 | int ret; |
| 429 | |
Johan Hovold | 4324282d | 2016-01-21 17:34:23 +0100 | [diff] [blame] | 430 | if (bundle->num_cports != 1) |
| 431 | return -ENODEV; |
| 432 | |
| 433 | cport_desc = &bundle->cport_desc[0]; |
| 434 | if (cport_desc->protocol_id != GREYBUS_PROTOCOL_HID) |
| 435 | return -ENODEV; |
| 436 | |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 437 | ghid = kzalloc(sizeof(*ghid), GFP_KERNEL); |
| 438 | if (!ghid) |
| 439 | return -ENOMEM; |
| 440 | |
Johan Hovold | 4324282d | 2016-01-21 17:34:23 +0100 | [diff] [blame] | 441 | connection = gb_connection_create(bundle, le16_to_cpu(cport_desc->id), |
| 442 | gb_hid_request_handler); |
| 443 | if (IS_ERR(connection)) { |
| 444 | ret = PTR_ERR(connection); |
Johan Hovold | 5dd8cc5 | 2016-01-21 17:34:22 +0100 | [diff] [blame] | 445 | goto err_free_ghid; |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | connection->private = ghid; |
| 449 | ghid->connection = connection; |
Johan Hovold | 4324282d | 2016-01-21 17:34:23 +0100 | [diff] [blame] | 450 | |
| 451 | hid = hid_allocate_device(); |
| 452 | if (IS_ERR(hid)) { |
| 453 | ret = PTR_ERR(hid); |
| 454 | goto err_connection_destroy; |
| 455 | } |
| 456 | |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 457 | ghid->hid = hid; |
| 458 | |
Johan Hovold | 4324282d | 2016-01-21 17:34:23 +0100 | [diff] [blame] | 459 | greybus_set_drvdata(bundle, ghid); |
| 460 | |
| 461 | ret = gb_connection_enable(connection); |
| 462 | if (ret) |
| 463 | goto err_destroy_hid; |
| 464 | |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 465 | ret = gb_hid_init(ghid); |
| 466 | if (ret) |
Johan Hovold | 4324282d | 2016-01-21 17:34:23 +0100 | [diff] [blame] | 467 | goto err_connection_disable; |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 468 | |
| 469 | ret = hid_add_device(hid); |
Johan Hovold | 5dd8cc5 | 2016-01-21 17:34:22 +0100 | [diff] [blame] | 470 | if (ret) { |
| 471 | hid_err(hid, "can't add hid device: %d\n", ret); |
Johan Hovold | 4324282d | 2016-01-21 17:34:23 +0100 | [diff] [blame] | 472 | goto err_connection_disable; |
Johan Hovold | 5dd8cc5 | 2016-01-21 17:34:22 +0100 | [diff] [blame] | 473 | } |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 474 | |
Johan Hovold | 5dd8cc5 | 2016-01-21 17:34:22 +0100 | [diff] [blame] | 475 | return 0; |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 476 | |
Johan Hovold | 4324282d | 2016-01-21 17:34:23 +0100 | [diff] [blame] | 477 | err_connection_disable: |
| 478 | gb_connection_disable(connection); |
Johan Hovold | 5dd8cc5 | 2016-01-21 17:34:22 +0100 | [diff] [blame] | 479 | err_destroy_hid: |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 480 | hid_destroy_device(hid); |
Johan Hovold | 4324282d | 2016-01-21 17:34:23 +0100 | [diff] [blame] | 481 | err_connection_destroy: |
| 482 | gb_connection_destroy(connection); |
Johan Hovold | 5dd8cc5 | 2016-01-21 17:34:22 +0100 | [diff] [blame] | 483 | err_free_ghid: |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 484 | kfree(ghid); |
| 485 | |
| 486 | return ret; |
| 487 | } |
| 488 | |
Johan Hovold | 4324282d | 2016-01-21 17:34:23 +0100 | [diff] [blame] | 489 | static void gb_hid_disconnect(struct gb_bundle *bundle) |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 490 | { |
Johan Hovold | 4324282d | 2016-01-21 17:34:23 +0100 | [diff] [blame] | 491 | struct gb_hid *ghid = greybus_get_drvdata(bundle); |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 492 | |
Johan Hovold | 4324282d | 2016-01-21 17:34:23 +0100 | [diff] [blame] | 493 | gb_connection_disable(ghid->connection); |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 494 | hid_destroy_device(ghid->hid); |
Johan Hovold | 4324282d | 2016-01-21 17:34:23 +0100 | [diff] [blame] | 495 | gb_connection_destroy(ghid->connection); |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 496 | kfree(ghid); |
| 497 | } |
| 498 | |
Johan Hovold | 4324282d | 2016-01-21 17:34:23 +0100 | [diff] [blame] | 499 | static const struct greybus_bundle_id gb_hid_id_table[] = { |
| 500 | { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_HID) }, |
| 501 | { } |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 502 | }; |
Johan Hovold | 4324282d | 2016-01-21 17:34:23 +0100 | [diff] [blame] | 503 | MODULE_DEVICE_TABLE(greybus, gb_hid_id_table); |
Viresh Kumar | 96eab77 | 2015-03-16 16:49:37 +0530 | [diff] [blame] | 504 | |
Johan Hovold | 4324282d | 2016-01-21 17:34:23 +0100 | [diff] [blame] | 505 | static struct greybus_driver gb_hid_driver = { |
| 506 | .name = "hid", |
| 507 | .probe = gb_hid_probe, |
| 508 | .disconnect = gb_hid_disconnect, |
| 509 | .id_table = gb_hid_id_table, |
| 510 | }; |
| 511 | module_greybus_driver(gb_hid_driver); |
Greg Kroah-Hartman | c9e9de2 | 2015-11-04 20:46:14 -0800 | [diff] [blame] | 512 | |
| 513 | MODULE_LICENSE("GPL v2"); |