platform/chrome: Add ChromeOS EC USB driver
Subsystems (2)
PATCH v2 1/1 platform/chrome: Add ChromeOS EC USB driver
Use USB to talk to the ChromeOS EC. The protocol is defined by the EC and is fairly simple, with a length byte, checksum, command byte and version byte in the header.
Use vendor defined usb interface with in/out endpoints to transfer requests and responses. Also use one interrupt in endpoint which signals readiness of response and pending events on the EC side.
---
V1 -> V2:
- Initialize work before potential cancel_work_sync call
- Return errors in .post_reset and .resume callbacks
- Inline expected_response_size function
- Use "imperative mood" in the commit message
- Use u8 instead of uin8_t
- Use 100 columns
- Add some blank line
- Move some code and functions
- Adjust function and variables names
- Some additional formatting
drivers/platform/chrome/Kconfig | 11 + drivers/platform/chrome/Makefile | 1 + drivers/platform/chrome/cros_ec_usb.c | 582 ++++++++++++++++++++++++++ 3 files changed, 594 insertions(+) create mode 100644 drivers/platform/chrome/cros_ec_usb.c
drivers/platform/chrome/Kconfig +11 -0
@@ -316,6 +316,17 @@ config CROS_TYPEC_SWITCH To compile this driver as a module, choose M here: the module will be called cros_typec_switch. +config CROS_EC_USB + tristate "ChromeOS Embedded Controller (USB)" + depends on CROS_EC && USB + help + If you say Y here, you get support for talking to the ChromeOS EC + through a USB. The driver uses vendor defined interface and is capable + of signaling events from EC. + + To compile this driver as a module, choose M here: the + module will be called cros_ec_usb. + source "drivers/platform/chrome/wilco_ec/Kconfig" # Kunit test cases
drivers/platform/chrome/Makefile +1 -0
@@ -38,6 +38,7 @@ obj-$(CONFIG_CROS_EC_SYSFS) += cros_ec_sysfs.o obj-$(CONFIG_CROS_HPS_I2C) += cros_hps_i2c.o obj-$(CONFIG_CROS_USBPD_LOGGER) += cros_usbpd_logger.o obj-$(CONFIG_CROS_USBPD_NOTIFY) += cros_usbpd_notify.o +obj-$(CONFIG_CROS_EC_USB) += cros_ec_usb.o obj-$(CONFIG_WILCO_EC) += wilco_ec/
drivers/platform/chrome/cros_ec_usb.c new file +582 -0
582 lines not on this page · raw message
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
kernel test robot
build botkernel test robot noticed the following build warnings:
Hi Dawid,
kernel test robot noticed the following build warnings:
[auto build test WARNING on chrome-platform/for-next]
[also build test WARNING on chrome-platform/for-firmware-next linus/master v6.16-rc4 next-20250701] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Dawid-Niedzwiecki/platform-chrome-Add-ChromeOS-EC-USB-driver/20250630-202610 base: https://git.kernel.org/pub/scm/linux/kernel/git/chrome-platform/linux.git for-next patch link: https://lore.kernel.org/r/20250630122155.2060589-1-dawidn%40google.com patch subject: [PATCH v2] platform/chrome: Add ChromeOS EC USB driver config: i386-randconfig-r122-20250702 (https://download.01.org/0day-ci/archive/20250702/[email protected]/config) compiler: gcc-12 (Debian 12.2.0-14+deb12u1) 12.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250702/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
sparse warnings: (new ones prefixed by >>)
>> drivers/platform/chrome/cros_ec_usb.c:345:50: sparse: sparse: incorrect type in initializer (different base types) @@ expected unsigned short const [usertype] idProduct @@ got restricted __le16 [usertype] idProduct @@
drivers/platform/chrome/cros_ec_usb.c:345:50: sparse: expected unsigned short const [usertype] idProduct
drivers/platform/chrome/cros_ec_usb.c:345:50: sparse: got restricted __le16 [usertype] idProduct
vim +345 drivers/platform/chrome/cros_ec_usb.c
338
339 static int cros_ec_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
340 {
341 struct usb_device *usb_dev = interface_to_usbdev(intf);
342 struct usb_endpoint_descriptor *bulk_in, *bulk_out, *int_in;
343 struct device *if_dev = &intf->dev;
344 struct cros_ec_device *ec_dev;
> 345 const u16 idProduct = usb_dev->descriptor.idProduct;
346 struct cros_ec_usb *ec_usb = cros_ec_usb_get_registered(idProduct);
347 const bool is_registered = !!ec_usb;
348 int ret;
349
350 /*
351 * Do not register the same EC device twice. The probing is performed every reboot, sysjump,
352 * crash etc. Recreating the /dev/cros_X file every time would force all application to
353 * reopen the file, which is not a case for other cros_ec_x divers. Instead, keep
354 * the cros_ec_device and cros_ec_usb structures constant and replace USB related structures
355 * for the same EC that is reprobed.
356 *
357 * The driver doesn't support handling two devices with the same idProduct, but it will
358 * never be a real usecase.
359 */
360 if (!is_registered) {
361 ec_usb = kzalloc(sizeof(*ec_usb), GFP_KERNEL);
362 if (!ec_usb)
363 return -ENOMEM;
364
365 INIT_WORK(&ec_usb->work_ec_evt, usb_evt_handler);
366
367 ec_dev = kzalloc(sizeof(*ec_dev), GFP_KERNEL);
368 if (!ec_dev) {
369 ret = -ENOMEM;
370 goto error;
371 }
372
373 ec_usb->ec_dev = ec_dev;
374 mutex_init(&ec_usb->io_mutex);
375 init_waitqueue_head(&ec_usb->resp_ready_wait);
376
377 ec_dev->priv = ec_usb;
378 /* EC uses int endpoint to signal events. */
379 ec_dev->irq = 0;
380 ec_dev->cmd_xfer = NULL;
381 ec_dev->pkt_xfer = do_cros_ec_pkt_xfer_usb;
382 ec_dev->din_size = sizeof(struct ec_host_response) +
383 sizeof(struct ec_response_get_protocol_info);
384 ec_dev->dout_size = sizeof(struct ec_host_request) +
385 sizeof(struct ec_params_rwsig_action);
386 } else {
387 ec_dev = ec_usb->ec_dev;
388
389 /*
390 * We need to allocate dout and din buffers, because cros_ec_register
391 * won't be called. These buffers were freed once previous usb device was
392 * disconnected. Use buffer sizes from the last query.
393 * The EC_HOST_EVENT_INTERFACE_READY event will be triggered at the end
394 * of a boot, which calls cros_ec_query_all function, that reallocates
395 * buffers.
396 */
397 ec_dev->din = devm_kzalloc(ec_dev->dev, ec_dev->din_size, GFP_KERNEL);
398 if (!ec_dev->din) {
399 ret = -ENOMEM;
400 dev_err(if_dev, "Failed to allocate din buffer\n");
401 goto error;
402 }
403 ec_dev->dout = devm_kzalloc(ec_dev->dev, ec_dev->dout_size, GFP_KERNEL);
404 if (!ec_dev->dout) {
405 ret = -ENOMEM;
406 dev_err(if_dev, "Failed to allocate dout buffer\n");
407 goto error;
408 }
409 }
410
411 ec_dev->dev = if_dev;
412 ec_dev->phys_name = dev_name(if_dev);
413 usb_set_intfdata(intf, ec_dev);
414 /* Allow EC to do remote wake-up - host sends SET_FEATURE(remote wake-up) before suspend. */
415 device_init_wakeup(&usb_dev->dev, true);
416
417 ec_usb->udev = usb_get_dev(usb_dev);
418 ec_usb->interface = usb_get_intf(intf);
419
420 /* Use first bulk-in/out endpoints + int-in endpoint */
421 ret = usb_find_common_endpoints(intf->cur_altsetting, &bulk_in, &bulk_out, &int_in, NULL);
422 if (ret) {
423 dev_err(if_dev,
424 "Could not find bulk-in, bulk-out or int-in endpoint\n");
425 goto error;
426 }
427 /* Bulk endpoints have to be capable of sending headers in one transfer. */
428 if ((usb_endpoint_maxp(bulk_out) < sizeof(struct ec_host_request)) ||
429 (usb_endpoint_maxp(bulk_in) < sizeof(struct ec_host_response)) || 430 (usb_endpoint_maxp(int_in)) < sizeof(struct int_msg)) {
431 ret = -ENOSPC;
432 dev_err(if_dev, "Incorrect max packet size\n");
433 goto error;
434 }
435
436 ec_usb->bulk_out_pipe = usb_sndbulkpipe(ec_usb->udev, bulk_out->bEndpointAddress);
437 ec_usb->bulk_in_size = usb_endpoint_maxp(bulk_in);
438 ec_usb->bulk_in_pipe = usb_rcvbulkpipe(ec_usb->udev, bulk_in->bEndpointAddress);
439 ec_usb->bulk_in_buffer = kmalloc(ec_usb->bulk_in_size, GFP_KERNEL);
440 if (!ec_usb->bulk_in_buffer) {
441 dev_err(if_dev, "Failed to allocate bulk in buffer\n");
442 ret = -ENOMEM;
443 goto error;
444 }
445
446 ec_usb->int_in_size = usb_endpoint_maxp(int_in);
447 ec_usb->int_in_pipe = usb_rcvintpipe(ec_usb->udev, int_in->bEndpointAddress);
448 ec_usb->int_in_interval = int_in->bInterval;
449 ec_usb->int_in_buffer = kmalloc(ec_usb->int_in_size, GFP_KERNEL);
450 if (!ec_usb->int_in_buffer) {
451 dev_err(if_dev, "Failed to allocate int in buffer\n");
452 ret = -ENOMEM;
453 goto error;
454 }
455 ec_usb->int_in_urb = usb_alloc_urb(0, GFP_KERNEL);
456 if (!ec_usb->int_in_urb) {
457 dev_err(if_dev, "Failed to allocate int in urb\n");
458 ret = -ENOMEM;
459 goto error;
460 }
461
462 /* Use URB for the int endpoint. */
463 ret = submit_int_urb(ec_dev);
464 if (ret) {
465 dev_err(if_dev, "Failed to sumbit int urb: %d\n", ret);
466 goto error;
467 }
468
469 mutex_lock(&ec_usb->io_mutex);
470 ec_usb->disconnected = false;
471 mutex_unlock(&ec_usb->io_mutex);
472
473 if (!is_registered) {
474 ret = cros_ec_register(ec_dev);
475 if (ret) {
476 dev_err(if_dev, "Cannot register EC\n");
477 goto error;
478 }
479 ret = cros_ec_usb_register(idProduct, ec_usb);
480 if (ret) {
481 cros_ec_unregister(ec_dev);
482 goto error;
483 }
484 ec_usb->registered = true;
485 }
486
487 /* Handle potential events that haven't been handled before registration */
488 schedule_work(&ec_usb->work_ec_evt);
489
490 return 0;
491
492 error:
493 /* Free allocated memory */
494 cros_ec_usb_delete(ec_usb);
495
496 return ret;
497 }
498
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
I'll fix the build problem in V3.
I've also found a bug, introduced in V2:
Quoted 1 line
+
ec_dev->din=devm_kzalloc(ec_dev->dev, ec_dev->din_size, GFP_KERNEL);
ec_dev->dev is not updated at this moment. I'll fix that in V3 as well