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