pax_global_header00006660000000000000000000000064145366524220014523gustar00rootroot0000000000000052 comment=78ffb70626f7fba0c886e39de85c5c3ba21ad6f9 usbio-drivers-0~git202312141918.78ffb706/000077500000000000000000000000001453665242200172355ustar00rootroot00000000000000usbio-drivers-0~git202312141918.78ffb706/.gitattributes000066400000000000000000000047261453665242200221410ustar00rootroot00000000000000############################################################################### # Set default behavior to automatically normalize line endings. ############################################################################### * text=auto ############################################################################### # Set default behavior for command prompt diff. # # This is need for earlier builds of msysgit that does not have it on by # default for csharp files. # Note: This is only used by command line ############################################################################### #*.cs diff=csharp ############################################################################### # Set the merge driver for project and solution files # # Merging from the command prompt will add diff markers to the files if there # are conflicts (Merging from VS is not affected by the settings below, in VS # the diff markers are never inserted). Diff markers may cause the following # file extensions to fail to load in VS. An alternative would be to treat # these files as binary and thus will always conflict and require user # intervention with every merge. To do so, just uncomment the entries below ############################################################################### #*.sln merge=binary #*.csproj merge=binary #*.vbproj merge=binary #*.vcxproj merge=binary #*.vcproj merge=binary #*.dbproj merge=binary #*.fsproj merge=binary #*.lsproj merge=binary #*.wixproj merge=binary #*.modelproj merge=binary #*.sqlproj merge=binary #*.wwaproj merge=binary ############################################################################### # behavior for image files # # image files are treated as binary by default. ############################################################################### #*.jpg binary #*.png binary #*.gif binary ############################################################################### # diff behavior for common document formats # # Convert binary document formats to text before diffing them. This feature # is only available from the command line. Turn it on by uncommenting the # entries below. ############################################################################### #*.doc diff=astextplain #*.DOC diff=astextplain #*.docx diff=astextplain #*.DOCX diff=astextplain #*.dot diff=astextplain #*.DOT diff=astextplain #*.pdf diff=astextplain #*.PDF diff=astextplain #*.rtf diff=astextplain #*.RTF diff=astextplain usbio-drivers-0~git202312141918.78ffb706/.gitignore000066400000000000000000000103721453665242200212300ustar00rootroot00000000000000## Ignore Visual Studio temporary files, build results, and ## files generated by popular Visual Studio add-ons. # User-specific files *.suo *.user *.userosscache *.sln.docstates # User-specific files (MonoDevelop/Xamarin Studio) *.userprefs # Build results [Dd]ebug/ [Dd]ebugPublic/ [Rr]elease/ [Rr]eleases/ x64/ x86/ bld/ [Bb]in/ [Oo]bj/ [Ll]og/ # Visual Studio 2015 cache/options directory .vs/ # Klocwork files directory .klocwork/ # Uncomment if you have tasks that create the project's static files in wwwroot #wwwroot/ # MSTest test Results [Tt]est[Rr]esult*/ [Bb]uild[Ll]og.* # NUNIT *.VisualState.xml TestResult.xml # Build Results of an ATL Project [Dd]ebugPS/ [Rr]eleasePS/ dlldata.c # DNX project.lock.json project.fragment.lock.json artifacts/ *_i.c *_p.c *_i.h *.ilk *.meta *.obj *.pch *.pdb *.pgc *.pgd *.rsp *.sbr *.tlb *.tli *.tlh *.tmp *.tmp_proj *.log *.vspscc *.vssscc .builds *.pidb *.svclog *.scc # Chutzpah Test files _Chutzpah* # Visual C++ cache files ipch/ *.aps *.ncb *.opendb *.opensdf *.sdf *.cachefile *.VC.db *.VC.VC.opendb # Visual Studio profiler *.psess *.vsp *.vspx *.sap # TFS 2012 Local Workspace $tf/ # Guidance Automation Toolkit *.gpState # ReSharper is a .NET coding add-in _ReSharper*/ *.[Rr]e[Ss]harper *.DotSettings.user # JustCode is a .NET coding add-in .JustCode # TeamCity is a build add-in _TeamCity* # DotCover is a Code Coverage Tool *.dotCover # NCrunch _NCrunch_* .*crunch*.local.xml nCrunchTemp_* # MightyMoose *.mm.* AutoTest.Net/ # Web workbench (sass) .sass-cache/ # Installshield output folder [Ee]xpress/ # DocProject is a documentation generator add-in DocProject/buildhelp/ DocProject/Help/*.HxT DocProject/Help/*.HxC DocProject/Help/*.hhc DocProject/Help/*.hhk DocProject/Help/*.hhp DocProject/Help/Html2 DocProject/Help/html # Click-Once directory publish/ # Publish Web Output *.[Pp]ublish.xml *.azurePubxml # TODO: Comment the next line if you want to checkin your web deploy settings # but database connection strings (with potential passwords) will be unencrypted #*.pubxml *.publishproj # Microsoft Azure Web App publish settings. Comment the next line if you want to # checkin your Azure Web App publish settings, but sensitive information contained # in these scripts will be unencrypted PublishScripts/ # NuGet Packages *.nupkg # The packages folder can be ignored because of Package Restore **/packages/* # except build/, which is used as an MSBuild target. !**/packages/build/ # Uncomment if necessary however generally it will be regenerated when needed #!**/packages/repositories.config # NuGet v3's project.json files produces more ignoreable files *.nuget.props *.nuget.targets # Microsoft Azure Build Output csx/ *.build.csdef # Microsoft Azure Emulator ecf/ rcf/ # Windows Store app package directories and files AppPackages/ BundleArtifacts/ Package.StoreAssociation.xml _pkginfo.txt # Visual Studio cache files # files ending in .cache can be ignored *.[Cc]ache # but keep track of directories ending in .cache !*.[Cc]ache/ # Others ClientBin/ ~$* *~ *.dbmdl *.dbproj.schemaview *.jfm *.pfx *.publishsettings node_modules/ orleans.codegen.cs # Since there are multiple workflows, uncomment next line to ignore bower_components # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) #bower_components/ # RIA/Silverlight projects Generated_Code/ # Backup & report files from converting an old project file # to a newer Visual Studio version. Backup files are not needed, # because we have git ;-) _UpgradeReport_Files/ Backup*/ UpgradeLog*.XML UpgradeLog*.htm # SQL Server files *.mdf *.ldf # Business Intelligence projects *.rdl.data *.bim.layout *.bim_*.settings # Microsoft Fakes FakesAssemblies/ # GhostDoc plugin setting file *.GhostDoc.xml # Node.js Tools for Visual Studio .ntvs_analysis.dat # Visual Studio 6 build log *.plg # Visual Studio 6 workspace options file *.opt # Visual Studio LightSwitch build output **/*.HTMLClient/GeneratedArtifacts **/*.DesktopClient/GeneratedArtifacts **/*.DesktopClient/ModelManifest.xml **/*.Server/GeneratedArtifacts **/*.Server/ModelManifest.xml _Pvt_Extensions # Paket dependency manager .paket/paket.exe paket-files/ # FAKE - F# Make .fake/ # JetBrains Rider .idea/ *.sln.iml # CodeRush .cr/ # Python Tools for Visual Studio (PTVS) __pycache__/ *.pyc usbio-drivers-0~git202312141918.78ffb706/CODE_OF_CONDUCT.md000066400000000000000000000124761453665242200220460ustar00rootroot00000000000000# Contributor Covenant Code of Conduct ## Our Pledge We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, caste, color, religion, or sexual identity and orientation. We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community. ## Our Standards Examples of behavior that contributes to a positive environment for our community include: * Demonstrating empathy and kindness toward other people * Being respectful of differing opinions, viewpoints, and experiences * Giving and gracefully accepting constructive feedback * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience * Focusing on what is best not just for us as individuals, but for the overall community Examples of unacceptable behavior include: * The use of sexualized language or imagery, and sexual attention or advances of any kind * Trolling, insulting or derogatory comments, and personal or political attacks * Public or private harassment * Publishing others' private information, such as a physical or email address, without their explicit permission * Other conduct which could reasonably be considered inappropriate in a professional setting ## Enforcement Responsibilities Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful. Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate. ## Scope This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. ## Enforcement Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at CommunityCodeOfConduct AT intel DOT com. All complaints will be reviewed and investigated promptly and fairly. All community leaders are obligated to respect the privacy and security of the reporter of any incident. ## Enforcement Guidelines Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct: ### 1. Correction **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community. **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested. ### 2. Warning **Community Impact**: A violation through a single incident or series of actions. **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban. ### 3. Temporary Ban **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior. **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban. ### 4. Permanent Ban **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals. **Consequence**: A permanent ban from any sort of public interaction within the community. ## Attribution This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.1, available at [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1]. Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder][Mozilla CoC]. For answers to common questions about this code of conduct, see the FAQ at [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at [https://www.contributor-covenant.org/translations][translations]. [homepage]: https://www.contributor-covenant.org [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html [Mozilla CoC]: https://github.com/mozilla/diversity [FAQ]: https://www.contributor-covenant.org/faq usbio-drivers-0~git202312141918.78ffb706/CONTRIBUTING.md000066400000000000000000000043711453665242200214730ustar00rootroot00000000000000# Contributing ### License is licensed under the terms in [LICENSE]. By contributing to the project, you agree to the license and copyright terms therein and release your contribution under these terms. ### Sign your work Please use the sign-off line at the end of the patch. Your signature certifies that you wrote the patch or otherwise have the right to pass it on as an open-source patch. The rules are pretty simple: if you can certify the below (from [developercertificate.org](http://developercertificate.org/)): ``` Developer Certificate of Origin Version 1.1 Copyright (C) 2004, 2006 The Linux Foundation and its contributors. 660 York Street, Suite 102, San Francisco, CA 94110 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Developer's Certificate of Origin 1.1 By making a contribution to this project, I certify that: (a) The contribution was created in whole or in part by me and I have the right to submit it under the open source license indicated in the file; or (b) The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate open source license and I have the right under that license to submit that work with modifications, whether created in whole or in part by me, under the same open source license (unless I am permitted to submit under a different license), as indicated in the file; or (c) The contribution was provided directly to me by some other person who certified (a), (b) or (c) and I have not modified it. (d) I understand and agree that this project and the contribution are public and that a record of the contribution (including all personal information I submit with it, including my sign-off) is maintained indefinitely and may be redistributed consistent with this project or the open source license(s) involved. ``` Then you just add a line to every git commit message: Signed-off-by: Joe Smith Use your real name (sorry, no pseudonyms or anonymous contributions.) If you set your `user.name` and `user.email` git configs, you can sign your commit automatically with `git commit -s`. usbio-drivers-0~git202312141918.78ffb706/Makefile000066400000000000000000000012671453665242200207030ustar00rootroot00000000000000# SPDX-License-Identifier: GPL-2.0 # Copyright (c) 2023 Intel Corporation. ifeq ($(KERN_VER),$(EMPTY_STR)) KERNELRELEASE ?= $(shell uname -r) else KERNELRELEASE ?= $(KERN_VER) endif obj-m += usbio.o usbio-y := drivers/mfd/usbio.o obj-m += gpio-usbio.o gpio-usbio-y := drivers/gpio/gpio-usbio.o obj-m += i2c-usbio.o i2c-usbio-y := drivers/i2c/busses/i2c-usbio.o KERNELRELEASE ?= $(shell uname -r) KERNEL_SRC ?= /lib/modules/$(KERNELRELEASE)/build PWD := $(shell pwd) ccflags-y += -I$(src)/include/ all: $(MAKE) -C $(KERNEL_SRC) M=$(PWD) modules modules_install: $(MAKE) INSTALL_MOD_DIR=/updates -C $(KERNEL_SRC) M=$(PWD) modules_install clean: $(MAKE) -C $(KERNEL_SRC) M=$(PWD) clean usbio-drivers-0~git202312141918.78ffb706/README.md000066400000000000000000000014761453665242200205240ustar00rootroot00000000000000# USBIO driver stack This repository supports USBIO Bridge drivers for Intel MeteoLake platform with Lattice AIC ## Build instructions Ways to build the USBIO drivers 1. build out of kernel source tree 2. build with kernel source tree and build with dkms aren't supported yet Build was tested on Ubuntu 22.04 system ### Build out of kernel source tree * Prerequisite: 5.19 (or later), kernel header and build packages on compiling machine. To install ``` $ sudo apt-get install build-essential linux-headers-`uname -r` ``` * To compile the drivers: ``` $ cd usbio-drivers/drivers $ make clean $ make ``` * To install the drivers ``` $ sudo insmod usbio.ko $ sudo insmod i2c-usbio.ko $ sudo insmod gpio-usbio.ko ``` * To uninstall the drivers ``` $ sudo rmmod gpio-usbio $ sudo rmmod i2c-usbio $ sudo rmmod usbio ``` usbio-drivers-0~git202312141918.78ffb706/dkms.conf000066400000000000000000000006421453665242200210440ustar00rootroot00000000000000PACKAGE_NAME="usbio-drivers" PACKAGE_VERSION="0.0.2" MAKE="make KERNELRELEASE=$kernelver KERNEL_SRC=$kernel_source_dir" CLEAN="make KERNELRELEASE=$kernelver KERNEL_SRC=$kernel_source_dir clean" BUILT_MODULE_NAME[0]="usbio" DEST_MODULE_LOCATION[0]="/updates" BUILT_MODULE_NAME[1]="i2c-usbio" DEST_MODULE_LOCATION[1]="/updates" BUILT_MODULE_NAME[2]="gpio-usbio" DEST_MODULE_LOCATION[2]="/updates" AUTOINSTALL="yes" usbio-drivers-0~git202312141918.78ffb706/drivers/000077500000000000000000000000001453665242200207135ustar00rootroot00000000000000usbio-drivers-0~git202312141918.78ffb706/drivers/gpio/000077500000000000000000000000001453665242200216515ustar00rootroot00000000000000usbio-drivers-0~git202312141918.78ffb706/drivers/gpio/gpio-usbio.c000066400000000000000000000320651453665242200241000ustar00rootroot00000000000000// SPDX-License-Identifier: GPL-2.0-only /* * Intel USBIO-GPIO driver * * Copyright (c) 2023, Intel Corporation. */ #include #include #include #include #include #include #include #include #include #include #include /* GPIO commands */ #define GPIO_DEINIT 0 #define GPIO_INIT 1 #define GPIO_READ 2 #define GPIO_WRITE 3 /* Deprecated */ #define GPIO_INT_EVENT 4 #define GPIO_INT_MASK 5 #define GPIO_INT_UNMASK 6 /* PinMode */ #define GPIO_CONF_DISABLED 0 #define GPIO_CONF_INPUT 1 #define GPIO_CONF_OUTPUT 2 /* PinConfig */ #define GPIO_CONF_DEFAULT (0<<2) #define GPIO_CONF_PULLUP (1<<2) #define GPIO_CONF_PULLDOWN (2<<2) #define GPIO_CONF_NONE (3<<2) /* Deprecated */ #define GPIO_CONF_INTERRUPT BIT(6) #define GPIO_INT_TYPE BIT(7) #define GPIO_CONF_EDGE (1 << 7) #define GPIO_CONF_LEVEL (0 << 7) #define IRQ_SUPPORT 0 struct gpio_cfg_packet { u8 bankid; u8 config; u8 pincount; u8 pins; } __packed; struct gpio_rw_packet { u8 bankid; u8 pincount; u8 pins; u32 value; } __packed; struct usbio_gpio_dev { struct platform_device *pdev; struct gpio_chip gc; struct usbio_gpio_info *ctr_info; DECLARE_BITMAP(unmasked_irqs, MAX_GPIO_NUM); DECLARE_BITMAP(enabled_irqs, MAX_GPIO_NUM); DECLARE_BITMAP(reenable_irqs, MAX_GPIO_NUM); u8 *connect_mode; struct mutex irq_lock; struct work_struct work; struct mutex trans_lock; u8 obuf[256]; u8 ibuf[256]; }; static bool usbio_gpio_valid(struct usbio_gpio_dev *usbio_gpio, int gpio_id) { if (gpio_id >= usbio_gpio->ctr_info->num || !test_bit(gpio_id, usbio_gpio->ctr_info->valid_pin_map)) { dev_err(&usbio_gpio->pdev->dev, "invalid gpio_id:%d\n", gpio_id); return false; } return true; } static int gpio_config(struct usbio_gpio_dev *usbio_gpio, u8 gpio_id, u8 config) { struct gpio_cfg_packet *packet = (struct gpio_cfg_packet *)usbio_gpio->obuf; int ret; if (!usbio_gpio_valid(usbio_gpio, gpio_id)) return -EINVAL; mutex_lock(&usbio_gpio->trans_lock); packet->bankid = gpio_id / GPIO_PER_BANK; packet->config = config | usbio_gpio->connect_mode[gpio_id]; packet->pincount = 1; packet->pins = gpio_id % GPIO_PER_BANK; ret = usbio_transfer(usbio_gpio->pdev, GPIO_INIT, packet, sizeof(*packet), NULL, NULL); mutex_unlock(&usbio_gpio->trans_lock); return ret; } static int usbio_gpio_read(struct usbio_gpio_dev *usbio_gpio, u8 gpio_id) { struct gpio_rw_packet *packet = (struct gpio_rw_packet *)usbio_gpio->obuf; struct gpio_rw_packet *ack_packet; int ret; int ibuf_len; if (!usbio_gpio_valid(usbio_gpio, gpio_id)) return -EINVAL; mutex_lock(&usbio_gpio->trans_lock); packet->bankid = gpio_id / GPIO_PER_BANK; packet->pincount = 1; packet->pins = gpio_id % GPIO_PER_BANK; ret = usbio_transfer(usbio_gpio->pdev, GPIO_READ, packet, sizeof(*packet), usbio_gpio->ibuf, &ibuf_len); ack_packet = (struct gpio_rw_packet *)usbio_gpio->ibuf; if (ret || !ibuf_len || ack_packet->pins != packet->pins) { dev_err(&usbio_gpio->pdev->dev, "%s failed gpio_id:%d ret %d %d", __func__, gpio_id, ret, ack_packet->pins); mutex_unlock(&usbio_gpio->trans_lock); return -EIO; } mutex_unlock(&usbio_gpio->trans_lock); return (ack_packet->value & (1 << ack_packet->pins) ? 1 : 0); } static int usbio_gpio_write(struct usbio_gpio_dev *usbio_gpio, u8 gpio_id, int value) { struct gpio_rw_packet *packet = (struct gpio_rw_packet *)usbio_gpio->obuf; int ret; mutex_lock(&usbio_gpio->trans_lock); packet->bankid = gpio_id / GPIO_PER_BANK; packet->pincount = 1; packet->pins = gpio_id % GPIO_PER_BANK; packet->value = value << packet->pins; ret = usbio_transfer(usbio_gpio->pdev, GPIO_WRITE, packet, sizeof(*packet), NULL, NULL); if (ret) { dev_err(&usbio_gpio->pdev->dev, "%s failed gpio_id:%d ret %d\n", __func__, gpio_id, ret); mutex_unlock(&usbio_gpio->trans_lock); return -EIO; } mutex_unlock(&usbio_gpio->trans_lock); return ret; } static int usbio_gpio_get_value(struct gpio_chip *chip, unsigned int offset) { struct usbio_gpio_dev *usbio_gpio = gpiochip_get_data(chip); dev_dbg(chip->parent, "%s: offset %d\n", __func__, offset); return usbio_gpio_read(usbio_gpio, offset); } static void usbio_gpio_set_value(struct gpio_chip *chip, unsigned int offset, int val) { struct usbio_gpio_dev *usbio_gpio = gpiochip_get_data(chip); int ret; dev_dbg(chip->parent, "%s: offset %d val %d\n", __func__, offset, val); ret = usbio_gpio_write(usbio_gpio, offset, val); if (ret) dev_err(chip->parent, "%s offset:%d val:%d set value failed %d\n", __func__, offset, val, ret); } static int usbio_gpio_direction_input(struct gpio_chip *chip, unsigned int offset) { struct usbio_gpio_dev *usbio_gpio = gpiochip_get_data(chip); u8 config = GPIO_CONF_INPUT; return gpio_config(usbio_gpio, offset, config); } static int usbio_gpio_direction_output(struct gpio_chip *chip, unsigned int offset, int val) { struct usbio_gpio_dev *usbio_gpio = gpiochip_get_data(chip); u8 config = GPIO_CONF_OUTPUT; int ret; ret = gpio_config(usbio_gpio, offset, config); if (ret) return ret; usbio_gpio_set_value(chip, offset, val); return 0; } static int usbio_gpio_set_config(struct gpio_chip *chip, unsigned int offset, unsigned long config) { struct usbio_gpio_dev *usbio_gpio = gpiochip_get_data(chip); if (!usbio_gpio_valid(usbio_gpio, offset)) return -EINVAL; usbio_gpio->connect_mode[offset] = 0; switch (pinconf_to_config_param(config)) { case PIN_CONFIG_BIAS_PULL_UP: usbio_gpio->connect_mode[offset] |= GPIO_CONF_PULLUP; break; case PIN_CONFIG_BIAS_PULL_DOWN: usbio_gpio->connect_mode[offset] |= GPIO_CONF_PULLDOWN; break; case PIN_CONFIG_DRIVE_PUSH_PULL: case PIN_CONFIG_PERSIST_STATE: usbio_gpio->connect_mode[offset] |= GPIO_CONF_NONE; break; default: usbio_gpio->connect_mode[offset] |= GPIO_CONF_DEFAULT; break; } return 0; } #if IRQ_SUPPORT static int usbio_enable_irq(struct usbio_gpio_dev *usbio_gpio, int gpio_id, bool enable) { struct gpio_packet *packet = (struct gpio_packet *)usbio_gpio->obuf; int ret; mutex_lock(&usbio_gpio->trans_lock); packet->num = 1; packet->item[0].index = gpio_id; packet->item[0].value = 0; dev_dbg(usbio_gpio->gc.parent, "%s %d", __func__, gpio_id); ret = usbio_transfer(usbio_gpio->pdev, enable == true ? GPIO_INT_UNMASK : GPIO_INT_MASK, packet, sizeof(*packet), NULL, NULL); mutex_unlock(&usbio_gpio->trans_lock); return ret; } static void usbio_gpio_async(struct work_struct *work) { struct usbio_gpio_dev *usbio_gpio = container_of(work, struct usbio_gpio_dev, work); int gpio_id; int unmasked; for_each_set_bit (gpio_id, usbio_gpio->reenable_irqs, usbio_gpio->gc.ngpio) { clear_bit(gpio_id, usbio_gpio->reenable_irqs); unmasked = test_bit(gpio_id, usbio_gpio->unmasked_irqs); if (unmasked) usbio_enable_irq(usbio_gpio, gpio_id, true); } } void usbio_gpio_event_cb(struct platform_device *pdev, u8 cmd, const void *evt_data, int len) { const struct gpio_packet *packet = evt_data; struct usbio_gpio_dev *usbio_gpio = platform_get_drvdata(pdev); int i; int irq; if (cmd != GPIO_INT_EVENT) return; for (i = 0; i < packet->num; i++) { irq = irq_find_mapping(usbio_gpio->gc.irq.domain, packet->item[i].index); if (!irq) { dev_err(usbio_gpio->gc.parent, "gpio_id %d not mapped to IRQ\n", packet->item[i].index); return; } generic_handle_irq(irq); set_bit(packet->item[i].index, usbio_gpio->reenable_irqs); dev_dbg(usbio_gpio->gc.parent, "%s got one interrupt %d %d %d\n", __func__, i, packet->item[i].index, packet->item[i].value); } schedule_work(&usbio_gpio->work); } static void usbio_irq_unmask(struct irq_data *irqd) { struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd); struct usbio_gpio_dev *usbio_gpio = gpiochip_get_data(gc); int gpio_id = irqd_to_hwirq(irqd); dev_dbg(usbio_gpio->gc.parent, "%s %d", __func__, gpio_id); set_bit(gpio_id, usbio_gpio->unmasked_irqs); } static void usbio_irq_mask(struct irq_data *irqd) { struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd); struct usbio_gpio_dev *usbio_gpio = gpiochip_get_data(gc); int gpio_id = irqd_to_hwirq(irqd); dev_dbg(usbio_gpio->gc.parent, "%s %d", __func__, gpio_id); clear_bit(gpio_id, usbio_gpio->unmasked_irqs); } static int usbio_irq_set_type(struct irq_data *irqd, unsigned type) { struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd); struct usbio_gpio_dev *usbio_gpio = gpiochip_get_data(gc); int gpio_id = irqd_to_hwirq(irqd); usbio_gpio->connect_mode[gpio_id] = GPIO_CONF_INTERRUPT; switch (type) { case IRQ_TYPE_LEVEL_HIGH: usbio_gpio->connect_mode[gpio_id] |= GPIO_CONF_LEVEL | GPIO_CONF_PULLUP; break; case IRQ_TYPE_LEVEL_LOW: usbio_gpio->connect_mode[gpio_id] |= GPIO_CONF_LEVEL | GPIO_CONF_PULLDOWN; break; case IRQ_TYPE_EDGE_BOTH: break; case IRQ_TYPE_EDGE_RISING: usbio_gpio->connect_mode[gpio_id] |= GPIO_CONF_EDGE | GPIO_CONF_PULLUP; break; case IRQ_TYPE_EDGE_FALLING: usbio_gpio->connect_mode[gpio_id] |= GPIO_CONF_EDGE | GPIO_CONF_PULLDOWN; break; default: return -EINVAL; } dev_dbg(usbio_gpio->gc.parent, "%s %d %x\n", __func__, gpio_id, usbio_gpio->connect_mode[gpio_id]); return 0; } static void usbio_irq_bus_lock(struct irq_data *irqd) { struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd); struct usbio_gpio_dev *usbio_gpio = gpiochip_get_data(gc); mutex_lock(&usbio_gpio->irq_lock); } static void usbio_irq_bus_unlock(struct irq_data *irqd) { struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd); struct usbio_gpio_dev *usbio_gpio = gpiochip_get_data(gc); int gpio_id = irqd_to_hwirq(irqd); int enabled; int unmasked; enabled = test_bit(gpio_id, usbio_gpio->enabled_irqs); unmasked = test_bit(gpio_id, usbio_gpio->unmasked_irqs); dev_dbg(usbio_gpio->gc.parent, "%s %d %d %d\n", __func__, gpio_id, enabled, unmasked); if (enabled != unmasked) { if (unmasked) { gpio_config(usbio_gpio, gpio_id, 0); usbio_enable_irq(usbio_gpio, gpio_id, true); set_bit(gpio_id, usbio_gpio->enabled_irqs); } else { usbio_enable_irq(usbio_gpio, gpio_id, false); clear_bit(gpio_id, usbio_gpio->enabled_irqs); } } mutex_unlock(&usbio_gpio->irq_lock); } static unsigned int usbio_irq_startup(struct irq_data *irqd) { struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd); return gpiochip_lock_as_irq(gc, irqd_to_hwirq(irqd)); } static void usbio_irq_shutdown(struct irq_data *irqd) { struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd); gpiochip_unlock_as_irq(gc, irqd_to_hwirq(irqd)); } static struct irq_chip usbio_gpio_irqchip = { .name = "ljca-irq", .irq_mask = usbio_irq_mask, .irq_unmask = usbio_irq_unmask, .irq_set_type = usbio_irq_set_type, .irq_bus_lock = usbio_irq_bus_lock, .irq_bus_sync_unlock = usbio_irq_bus_unlock, .irq_startup = usbio_irq_startup, .irq_shutdown = usbio_irq_shutdown, }; #endif static int usbio_gpio_probe(struct platform_device *pdev) { struct usbio_gpio_dev *usbio_gpio; struct usbio_platform_data *pdata = dev_get_platdata(&pdev->dev); #if IRQ_SUPPORT struct gpio_irq_chip *girq = NULL; #endif usbio_gpio = devm_kzalloc(&pdev->dev, sizeof(*usbio_gpio), GFP_KERNEL); if (!usbio_gpio) return -ENOMEM; usbio_gpio->ctr_info = &pdata->gpio_info; usbio_gpio->connect_mode = devm_kcalloc(&pdev->dev, usbio_gpio->ctr_info->num, sizeof(*usbio_gpio->connect_mode), GFP_KERNEL); if (!usbio_gpio->connect_mode) return -ENOMEM; mutex_init(&usbio_gpio->irq_lock); mutex_init(&usbio_gpio->trans_lock); usbio_gpio->pdev = pdev; usbio_gpio->gc.direction_input = usbio_gpio_direction_input; usbio_gpio->gc.direction_output = usbio_gpio_direction_output; usbio_gpio->gc.get = usbio_gpio_get_value; usbio_gpio->gc.set = usbio_gpio_set_value; usbio_gpio->gc.set_config = usbio_gpio_set_config; usbio_gpio->gc.can_sleep = true; usbio_gpio->gc.parent = &pdev->dev; usbio_gpio->gc.base = -1; usbio_gpio->gc.ngpio = usbio_gpio->ctr_info->num; usbio_gpio->gc.label = ACPI_COMPANION(&pdev->dev) ? acpi_dev_name(ACPI_COMPANION(&pdev->dev)) : "usbio-gpio"; usbio_gpio->gc.owner = THIS_MODULE; platform_set_drvdata(pdev, usbio_gpio); #if IRQ_SUPPORT usbio_register_event_cb(pdev, usbio_gpio_event_cb); girq = &usbio_gpio->gc.irq; girq->chip = &usbio_gpio_irqchip; girq->parent_handler = NULL; girq->num_parents = 0; girq->parents = NULL; girq->default_type = IRQ_TYPE_NONE; girq->handler = handle_simple_irq; INIT_WORK(&usbio_gpio->work, usbio_gpio_async); #endif return devm_gpiochip_add_data(&pdev->dev, &usbio_gpio->gc, usbio_gpio); } static int usbio_gpio_remove(struct platform_device *pdev) { return 0; } static struct platform_driver usbio_gpio_driver = { .driver.name = "usbio-gpio", .probe = usbio_gpio_probe, .remove = usbio_gpio_remove, }; module_platform_driver(usbio_gpio_driver); MODULE_AUTHOR("Lifu Wang "); MODULE_AUTHOR("Zhang Lixu "); MODULE_AUTHOR("Israel Cepeda "); MODULE_DESCRIPTION("Intel USBIO-GPIO driver"); MODULE_LICENSE("GPL v2"); MODULE_ALIAS("platform:usbio-gpio"); usbio-drivers-0~git202312141918.78ffb706/drivers/i2c/000077500000000000000000000000001453665242200213705ustar00rootroot00000000000000usbio-drivers-0~git202312141918.78ffb706/drivers/i2c/busses/000077500000000000000000000000001453665242200226745ustar00rootroot00000000000000usbio-drivers-0~git202312141918.78ffb706/drivers/i2c/busses/i2c-usbio.c000066400000000000000000000242061453665242200246400ustar00rootroot00000000000000// SPDX-License-Identifier: GPL-2.0-only /* * Intel USBIO-I2C driver * * Copyright (c) 2023, Intel Corporation. */ #include #include #include #include #include #include /* I2C commands */ enum i2c_cmd { I2C_DEINIT, I2C_INIT, I2C_READ, I2C_WRITE, I2C_XFER }; enum i2c_address_mode { I2C_ADDRESS_MODE_7BIT, I2C_ADDRESS_MODE_10BIT, }; /* Speeds */ #define I2C_400KHZ 400000 /* i2c init flags */ #define I2C_INIT_FLAG_MODE_MASK (0x1 << 0) #define I2C_INIT_FLAG_MODE_POLLING (0x0 << 0) #define I2C_INIT_FLAG_MODE_INTERRUPT (0x1 << 0) #define I2C_FLAG_ADDR_16BIT (0x1 << 0) #define I2C_INIT_FLAG_FREQ_MASK (0x3 << 1) #define I2C_FLAG_FREQ_100K (0x0 << 1) #define I2C_FLAG_FREQ_400K (0x1 << 1) #define I2C_FLAG_FREQ_1M (0x2 << 1) /* I2C init commands: Init/Deinit */ struct i2c_init_packet { u8 id; u16 config; u32 speed; } __packed; /* I2C RW commands: Read/Write */ struct i2c_rw_packet { u8 id; u16 config; u16 len; u8 data[]; } __packed; /* I2C Transfer */ struct i2c_xfer { u8 id; u16 config; u16 wlen; u16 rlen; u8 data[]; } __packed; #define USBIO_I2C_MAX_XFER_SIZE 256 #define USBIO_I2C_BUF_SIZE \ (USBIO_I2C_MAX_XFER_SIZE + sizeof(struct i2c_rw_packet)) struct usbio_i2c_dev { struct platform_device *pdev; struct usbio_i2c_info *ctr_info; struct i2c_adapter adap; u8 obuf[USBIO_I2C_BUF_SIZE]; u8 ibuf[USBIO_I2C_BUF_SIZE]; }; static u16 usbio_i2c_format_slave_addr(u8 slave_addr, enum i2c_address_mode mode) { if (mode == I2C_ADDRESS_MODE_7BIT) return (u16)slave_addr; return 0xFF; } static int usbio_i2c_start(struct usbio_i2c_dev *usbio_i2c, u8 slave_addr) { struct i2c_init_packet *w_packet = (struct i2c_init_packet *)usbio_i2c->obuf; int ret; memset(w_packet, 0, sizeof(*w_packet)); w_packet->id = usbio_i2c->ctr_info->id; /* TODO: Add support for 10Bit address and multiple speeds */ w_packet->config = usbio_i2c_format_slave_addr(slave_addr, I2C_ADDRESS_MODE_7BIT); w_packet->speed = I2C_400KHZ; ret = usbio_transfer_noack(usbio_i2c->pdev, I2C_INIT, w_packet, sizeof(*w_packet)); if (ret) { dev_err(&usbio_i2c->adap.dev, "i2c start failed ret:%d\n", ret); return -EIO; } return 0; } static int usbio_i2c_stop(struct usbio_i2c_dev *usbio_i2c, u8 slave_addr) { struct i2c_init_packet *w_packet = (struct i2c_init_packet *)usbio_i2c->obuf; int ret; memset(w_packet, 0, sizeof(*w_packet)); w_packet->id = usbio_i2c->ctr_info->id; /* TODO: Add support for 10Bit address */ w_packet->config = usbio_i2c_format_slave_addr(slave_addr, I2C_ADDRESS_MODE_7BIT); ret = usbio_transfer_noack(usbio_i2c->pdev, I2C_DEINIT, w_packet, sizeof(*w_packet)); if (ret) { dev_err(&usbio_i2c->adap.dev, "i2c stop failed ret:%d\n", ret); return -EIO; } return 0; } static int usbio_i2c_pure_read(struct usbio_i2c_dev *usbio_i2c, u8 slave_addr, u8 *data, int len) { struct i2c_rw_packet *w_packet = (struct i2c_rw_packet *)usbio_i2c->obuf; struct i2c_rw_packet *r_packet = (struct i2c_rw_packet *)usbio_i2c->ibuf; int ibuf_len; int ret; if (len > USBIO_I2C_MAX_XFER_SIZE) return -EINVAL; memset(w_packet, 0, sizeof(*w_packet)); w_packet->id = usbio_i2c->ctr_info->id; /* TODO: Add support for 10Bit address */ w_packet->config = usbio_i2c_format_slave_addr(slave_addr, I2C_ADDRESS_MODE_7BIT); w_packet->len = cpu_to_le16(len); ret = usbio_transfer(usbio_i2c->pdev, I2C_READ, w_packet, sizeof(*w_packet) + 1, r_packet, &ibuf_len); if (ret) { dev_err(&usbio_i2c->adap.dev, "I2C_READ failed ret:%d\n", ret); return ret; } if (ibuf_len < sizeof(*r_packet)) return -EIO; if ((s16)le16_to_cpu(r_packet->len) != len || r_packet->id != w_packet->id) { dev_err(&usbio_i2c->adap.dev, "i2c raw read failed len:%d id:%d %d\n", (s16)le16_to_cpu(r_packet->len), r_packet->id, w_packet->id); return -EIO; } memcpy(data, r_packet->data, len); return 0; } static int usbio_i2c_read(struct usbio_i2c_dev *usbio_i2c, u8 slave_addr, u8 *data, u8 len) { int ret; ret = usbio_i2c_start(usbio_i2c, slave_addr); if (ret) return ret; ret = usbio_i2c_pure_read(usbio_i2c, slave_addr, data, len); if (ret) { dev_err(&usbio_i2c->adap.dev, "i2c raw read failed ret:%d\n", ret); return ret; } return usbio_i2c_stop(usbio_i2c, slave_addr); } static int usbio_i2c_pure_write(struct usbio_i2c_dev *usbio_i2c, u8 slave_addr, u8 *data, int len) { struct i2c_rw_packet *w_packet = (struct i2c_rw_packet *)usbio_i2c->obuf; struct i2c_rw_packet *r_packet = (struct i2c_rw_packet *)usbio_i2c->ibuf; int ret; int ibuf_len; if (len > USBIO_I2C_MAX_XFER_SIZE) return -EINVAL; memset(w_packet, 0, sizeof(*w_packet)); w_packet->id = usbio_i2c->ctr_info->id; /* TODO: Add support for 10Bit address */ w_packet->config = usbio_i2c_format_slave_addr(slave_addr, I2C_ADDRESS_MODE_7BIT); w_packet->len = cpu_to_le16(len); memcpy(w_packet->data, data, len); ret = usbio_transfer(usbio_i2c->pdev, I2C_WRITE, w_packet, sizeof(*w_packet) + w_packet->len, r_packet, &ibuf_len); if (ret || ibuf_len < sizeof(*r_packet)) return -EIO; if ((s16)le16_to_cpu(r_packet->len) != len || r_packet->id != w_packet->id) { dev_err(&usbio_i2c->adap.dev, "i2c write failed len:%d id:%d/%d\n", (s16)le16_to_cpu(r_packet->len), r_packet->id, w_packet->id); return -EIO; } return 0; } static int usbio_i2c_write(struct usbio_i2c_dev *usbio_i2c, u8 slave_addr, u8 *data, u8 len) { int ret; if (!data) return -EINVAL; ret = usbio_i2c_start(usbio_i2c, slave_addr); if (ret) return ret; ret = usbio_i2c_pure_write(usbio_i2c, slave_addr, data, len); if (ret) return ret; return usbio_i2c_stop(usbio_i2c, slave_addr); } static int usbio_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msg, int num) { struct usbio_i2c_dev *usbio_i2c; struct i2c_msg *cur_msg; int i, ret; usbio_i2c = i2c_get_adapdata(adapter); if (!usbio_i2c) return -EINVAL; for (i = 0; i < num; i++) { cur_msg = &msg[i]; dev_dbg(&adapter->dev, "i:%d msg:(%d %d)\n", i, cur_msg->flags, cur_msg->len); if (cur_msg->flags & I2C_M_RD) ret = usbio_i2c_read(usbio_i2c, cur_msg->addr, cur_msg->buf, cur_msg->len); else ret = usbio_i2c_write(usbio_i2c, cur_msg->addr, cur_msg->buf, cur_msg->len); if (ret) return ret; } return num; } static u32 usbio_i2c_func(struct i2c_adapter *adap) { return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; } static const struct i2c_adapter_quirks usbio_i2c_quirks = { .max_read_len = USBIO_I2C_MAX_XFER_SIZE, .max_write_len = USBIO_I2C_MAX_XFER_SIZE, }; static const struct i2c_algorithm usbio_i2c_algo = { .master_xfer = usbio_i2c_xfer, .functionality = usbio_i2c_func, }; struct match_ids_walk_data { struct acpi_device *adev; const char *hid1; const char *uid2; const char *uid2_v2; }; static int match_device_ids(struct acpi_device *adev, void *data) { struct match_ids_walk_data *wd = data; if (acpi_dev_hid_uid_match(adev, wd->hid1, wd->uid2) || acpi_dev_hid_uid_match(adev, wd->hid1, wd->uid2_v2)) { wd->adev = adev; return 1; } return 0; } static void try_bind_acpi(struct platform_device *pdev, struct usbio_i2c_dev *usbio_i2c) { struct acpi_device *parent; struct acpi_device *cur = ACPI_COMPANION(&pdev->dev); const char *hid1; const char *uid1; char uid2[2] = { 0 }; char uid2_v2[5] = { 0 }; struct match_ids_walk_data wd = { 0 }; if (!cur) return; hid1 = acpi_device_hid(cur); uid1 = acpi_device_uid(cur); snprintf(uid2, sizeof(uid2), "%d", usbio_i2c->ctr_info->id); snprintf(uid2_v2, sizeof(uid2_v2), "VIC%d", usbio_i2c->ctr_info->id); /* * If the pdev is bound to the right acpi device, just forward it to the * adapter. Otherwise, we find that of current adapter manually. */ if (!uid1 || !strcmp(uid1, uid2) || !strcmp(uid1, uid2_v2)) { ACPI_COMPANION_SET(&usbio_i2c->adap.dev, cur); return; } dev_info(&pdev->dev, "hid %s uid %s new uid%s\n", hid1, uid1, uid2); parent = ACPI_COMPANION(pdev->dev.parent); if (!parent) return; wd.hid1 = hid1; wd.uid2 = uid2; wd.uid2_v2 = uid2_v2; #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 0, 0) acpi_dev_for_each_child(parent, match_device_ids, &wd); ACPI_COMPANION_SET(&usbio_i2c->adap.dev, wd.adev); #else list_for_each_entry(wd.adev, &parent->children, node) { if (match_device_ids(wd.adev, &wd)) { ACPI_COMPANION_SET(&usbio_i2c->adap.dev, wd.adev); return; } } #endif } static int usbio_i2c_probe(struct platform_device *pdev) { struct usbio_i2c_dev *usbio_i2c; struct usbio_platform_data *pdata = dev_get_platdata(&pdev->dev); int ret; usbio_i2c = devm_kzalloc(&pdev->dev, sizeof(*usbio_i2c), GFP_KERNEL); if (!usbio_i2c) return -ENOMEM; usbio_i2c->pdev = pdev; usbio_i2c->ctr_info = &pdata->i2c_info; usbio_i2c->adap.owner = THIS_MODULE; usbio_i2c->adap.class = I2C_CLASS_HWMON; usbio_i2c->adap.algo = &usbio_i2c_algo; usbio_i2c->adap.dev.parent = &pdev->dev; try_bind_acpi(pdev, usbio_i2c); usbio_i2c->adap.dev.of_node = pdev->dev.of_node; i2c_set_adapdata(&usbio_i2c->adap, usbio_i2c); snprintf(usbio_i2c->adap.name, sizeof(usbio_i2c->adap.name), "%s-%s-%d", "usbio-i2c", dev_name(pdev->dev.parent), usbio_i2c->ctr_info->id); platform_set_drvdata(pdev, usbio_i2c); ret = i2c_add_adapter(&usbio_i2c->adap); if (ret) return ret; #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 17, 0) if (has_acpi_companion(&usbio_i2c->adap.dev)) acpi_dev_clear_dependencies(ACPI_COMPANION(&usbio_i2c->adap.dev)); #endif return 0; } static int usbio_i2c_remove(struct platform_device *pdev) { struct usbio_i2c_dev *usbio_i2c = platform_get_drvdata(pdev); i2c_del_adapter(&usbio_i2c->adap); return 0; } static struct platform_driver usbio_i2c_driver = { .driver.name = "usbio-i2c", .probe = usbio_i2c_probe, .remove = usbio_i2c_remove, }; module_platform_driver(usbio_i2c_driver); MODULE_AUTHOR("Lifu Wang "); MODULE_AUTHOR("Ye Xiang "); MODULE_AUTHOR("Zhang Lixu "); MODULE_AUTHOR("Israel Cepeda "); MODULE_DESCRIPTION("Intel USBIO-I2C driver"); MODULE_LICENSE("GPL v2"); MODULE_ALIAS("platform:usbio-i2c"); usbio-drivers-0~git202312141918.78ffb706/drivers/mfd/000077500000000000000000000000001453665242200214615ustar00rootroot00000000000000usbio-drivers-0~git202312141918.78ffb706/drivers/mfd/bridge.h000066400000000000000000000060421453665242200230700ustar00rootroot00000000000000/* SPDX-License-Identifier: GPL-2.0 */ #ifndef __USB_IO_BRIDGE_H #define __USB_IO_BRIDGE_H /* stub types */ enum stub_type { CTRL_STUB = 1, DIAG_STUB, GPIO_STUB, I2C_STUB, SPI_STUB, }; /* CTRL stub commands */ enum usbio_mng_cmd { CTRL_PROT_VERSION = 0, CTRL_FW_VERSION, CTRL_RESET_NOTIFY, CTRL_RESET, CTRL_POWER_STATE_CHANGE, CTRL_SET_DFU_MODE, CTRL_ENUM_GPIO = 0x10, CTRL_ENUM_I2C, CTRL_ENUM_SPI, }; /* DIAG commands */ enum diag_cmd { DIAG_GET_STATE = 1, DIAG_GET_STATISTIC, DIAG_SET_TRACE_LEVEL, DIAG_SET_ECHO_MODE, DIAG_GET_FW_LOG, DIAG_GET_FW_COREDUMP, DIAG_TRIGGER_WDT, DIAG_TRIGGER_FAULT, DIAG_FEED_WDT, DIAG_GET_SECURE_STATE, }; /* command Flags */ #define ACK_FLAG BIT(0) #define RESP_FLAG BIT(1) #define CMPL_FLAG BIT(2) #define ERR_FLAG BIT(3) /* Control Transfer Message */ struct usbio_msg { u8 type; u8 cmd; u8 flags; u8 len; u8 data[]; } __packed; /* Bulk Transfer Message */ struct usbio_bmsg { u8 type; u8 cmd; u8 flags; u16 len; u8 data[]; } __packed; struct fw_version { u8 major; u8 minor; u16 patch; u16 build; } __packed; struct usbio_i2c_descriptor { u8 num; struct usbio_i2c_info info[]; } __packed; struct usbio_spi_ctr_info { u8 id; u8 capacity; } __packed; struct usbio_spi_descriptor { u8 num; struct usbio_spi_ctr_info info[]; } __packed; struct usbio_bank_descriptor { u8 bank_id; u8 pin_num; /* 1 bit for each gpio, 1 means valid */ u32 valid_pins; } __packed; struct usbio_gpio_descriptor { u8 pins_per_bank; u8 banks; struct usbio_bank_descriptor bank_desc[]; } __packed; #define MAX_PACKET_SIZE 64 #define MAX_PAYLOAD_SIZE (MAX_PACKET_SIZE - sizeof(struct usbio_msg)) #define USB_WRITE_TIMEOUT 200 #define USB_WRITE_ACK_TIMEOUT 500 #define USB_ENUM_STUB_TIMEOUT 20 struct usbio_event_cb_entry { struct platform_device *pdev; usbio_event_cb_t notify; }; struct usbio_stub_packet { u8 *ibuf; u32 ibuf_len; }; struct usbio_stub { struct list_head list; u8 type; struct usb_interface *intf; spinlock_t event_cb_lock; struct usbio_stub_packet ipacket; /* for identify ack */ bool acked; int cur_cmd; struct usbio_event_cb_entry event_entry; }; static inline void *usbio_priv(const struct usbio_stub *stub) { return (char *)stub + sizeof(struct usbio_stub); } enum bridge_state { BRIDGE_STOPPED, BRIDGE_INITED, BRIDGE_RESET_HANDSHAKE, BRIDGE_RESET_SYNCED, BRIDGE_ENUM_GPIO_COMPLETE, BRIDGE_ENUM_I2C_COMPLETE, BRIDGE_ENUM_SPI_COMPLETE, BRIDGE_SUSPEND, BRIDGE_STARTED, BRIDGE_FAILED, }; struct usbio_dev { struct usb_device *udev; struct usb_interface *intf; u8 ep0; /* the address of the control endpoint */ u8 in_ep; /* the address of the bulk in endpoint */ u8 out_ep; /* the address of the bulk out endpoint */ /* control buffer for xfer */ u16 cbuf_len; unsigned char *cbuf; /* the urb/buffer for read */ u16 ibuf_len; struct urb *in_urb; unsigned char *ibuf; enum bridge_state state; struct list_head stubs_list; /* to wait for an ongoing write ack */ wait_queue_head_t ack_wq; struct mfd_cell *cells; int cell_count; struct mutex mutex; }; #endif usbio-drivers-0~git202312141918.78ffb706/drivers/mfd/usbio.c000066400000000000000000000675641453665242200227700ustar00rootroot00000000000000// SPDX-License-Identifier: GPL-2.0-only /* * Intel USBIO-Bridge driver * * Copyright (c) 2023, Intel Corporation. */ #include #include #include #include #include #include #include #include #include #include #include #include #include "bridge.h" #define USBIO_VERSION "1.1" static char *gpio_hids[] = { "INTC1074", /* TGL */ "INTC1096", /* ADL */ "INTC100B", /* RPL */ "INTC10D1", /* MTL */ }; static struct mfd_cell_acpi_match usbio_acpi_match_gpio; static char *i2c_hids[] = { "INTC1075", /* TGL */ "INTC1097", /* ADL */ "INTC100C", /* RPL */ "INTC10D2", /* MTL */ }; static struct mfd_cell_acpi_match usbio_acpi_match_i2cs; static char *spi_hids[] = { "INTC1091", /* TGL */ "INTC1098", /* ADL */ "INTC100D", /* RPL */ "INTC10D3", /* MTL */ }; static struct mfd_cell_acpi_match usbio_acpi_match_spis; static int try_match_acpi_hid(struct acpi_device *child, struct mfd_cell_acpi_match *match, char **hids, int hids_num) { struct acpi_device_id ids[2] = {}; int i; for (i = 0; i < hids_num; i++) { strlcpy(ids[0].id, hids[i], sizeof(ids[0].id)); if (!acpi_match_device_ids(child, ids)) { match->pnpid = hids[i]; break; } } return 0; } static int match_device_ids(struct acpi_device *adev, void *data) { (void)data; try_match_acpi_hid(adev, &usbio_acpi_match_gpio, gpio_hids, ARRAY_SIZE(gpio_hids)); try_match_acpi_hid(adev, &usbio_acpi_match_i2cs, i2c_hids, ARRAY_SIZE(i2c_hids)); try_match_acpi_hid(adev, &usbio_acpi_match_spis, spi_hids, ARRAY_SIZE(spi_hids)); return 0; } static int precheck_acpi_hid(struct usb_interface *intf) { struct acpi_device *parent; #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 0, 0) struct acpi_device *child; #endif parent = ACPI_COMPANION(&intf->dev); if (!parent) return -ENODEV; #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 0, 0) acpi_dev_for_each_child(parent, match_device_ids, NULL); #else list_for_each_entry (child, &parent->children, node) { match_device_ids(child, NULL); } #endif return 0; } static bool usbio_validate(void *data, u32 data_len) { struct usbio_bmsg *header = (struct usbio_bmsg *)data; return (header->len + sizeof(*header) == data_len); } void usbio_dump(struct usbio_dev *bridge, void *buf, int len) { int i; u8 tmp[256] = { 0 }; int n = 0; if (!len) return; for (i = 0; i < len; i++) n += scnprintf(tmp + n, sizeof(tmp) - n - 1, "%02x ", ((u8 *)buf)[i]); dev_dbg(&bridge->intf->dev, "%s\n", tmp); } static struct usbio_stub *usbio_stub_alloc(struct usbio_dev *bridge, int priv_size) { struct usbio_stub *stub; stub = kzalloc(sizeof(*stub) + priv_size, GFP_KERNEL); if (!stub) return ERR_PTR(-ENOMEM); spin_lock_init(&stub->event_cb_lock); INIT_LIST_HEAD(&stub->list); list_add_tail(&stub->list, &bridge->stubs_list); dev_dbg(&bridge->intf->dev, "enuming a stub success\n"); return stub; } static struct usbio_stub *usbio_stub_find(struct usbio_dev *bridge, u8 type) { struct usbio_stub *stub; list_for_each_entry (stub, &bridge->stubs_list, list) { if (stub->type == type) return stub; } dev_err(&bridge->intf->dev, "usb stub not find, type: %d", type); return ERR_PTR(-ENODEV); } static void usbio_stub_notify(struct usbio_stub *stub, u8 cmd, const void *evt_data, int len) { unsigned long flags; spin_lock_irqsave(&stub->event_cb_lock, flags); if (stub->event_entry.notify && stub->event_entry.pdev) stub->event_entry.notify(stub->event_entry.pdev, cmd, evt_data, len); spin_unlock_irqrestore(&stub->event_cb_lock, flags); } static int usbio_parse(struct usbio_dev *bridge, struct usbio_bmsg *header) { struct usbio_stub *stub; stub = usbio_stub_find(bridge, header->type); if (IS_ERR(stub)) return PTR_ERR(stub); if (!(header->flags & RESP_FLAG)) { usbio_stub_notify(stub, header->cmd, header->data, header->len); return 0; } if (stub->cur_cmd != header->cmd) { dev_err(&bridge->intf->dev, "header->cmd:%x != stub->cur_cmd:%x", header->cmd, stub->cur_cmd); return -EINVAL; } if (header->flags & ERR_FLAG) { dev_err(&bridge->intf->dev, "header->cmd %x flag %x (ERR_FLAG)", header->cmd, header->flags); return -EINVAL; } stub->ipacket.ibuf_len = header->len; if (stub->ipacket.ibuf) memcpy(stub->ipacket.ibuf, header->data, header->len); stub->acked = true; wake_up(&bridge->ack_wq); return 0; } static int usbio_control_xfer(struct usbio_stub *stub, u8 cmd, const void *obuf, int obuf_len, void *ibuf, int *ibuf_len, bool wait_ack, int timeout) { struct usbio_msg *header; struct usbio_dev *bridge = usb_get_intfdata(stub->intf); int actual, ret; u8 flags = CMPL_FLAG; if (bridge->state == BRIDGE_STOPPED) return -ENODEV; if (obuf_len > bridge->cbuf_len) return -EINVAL; if (wait_ack) flags |= ACK_FLAG; stub->ipacket.ibuf_len = 0; actual = sizeof(header) + obuf_len; header = kmalloc(actual, GFP_KERNEL); if (!header) return -ENOMEM; header->type = stub->type; header->cmd = cmd; header->flags = flags; header->len = obuf_len; memcpy(header->data, obuf, obuf_len); dev_dbg(&bridge->intf->dev, "send: type:0x%x cmd:0x%x flags:0x%x len:%d\n", header->type, header->cmd, header->flags, header->len); usbio_dump(bridge, header->data, header->len); mutex_lock(&bridge->mutex); stub->cur_cmd = cmd; stub->ipacket.ibuf = ibuf; stub->acked = false; usb_autopm_get_interface(bridge->intf); ret = usb_control_msg_send(bridge->udev, bridge->ep0, 0, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT, 0, 0, header, actual, timeout, GFP_KERNEL); if (ret) { dev_err(&bridge->intf->dev, "bridge write failed ret:%d total_len:%d\n ", ret, actual); goto error; } kfree(header); header = NULL; if (wait_ack) { actual = bridge->cbuf_len; header = kmalloc(actual, GFP_KERNEL); if (!header) { ret = -ENOMEM; goto error; } ret = usb_control_msg_recv(bridge->udev, bridge->ep0, 0, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, 0, 0, header, actual, timeout, GFP_KERNEL); if (ret) { dev_err(&bridge->intf->dev, "bridge read failed ret:%d total_len:%d\n ", ret, actual); goto error; } } if (ibuf_len && header) { *ibuf_len = header->len; memcpy(ibuf, header->data, *ibuf_len); } stub->ipacket.ibuf = NULL; stub->ipacket.ibuf_len = 0; error: if(header) { kfree(header); header = NULL; } usb_autopm_put_interface(bridge->intf); mutex_unlock(&bridge->mutex); return ret; } static int usbio_bulk_write(struct usbio_stub *stub, u8 cmd, const void *obuf, int obuf_len, void *ibuf, int *ibuf_len, bool wait_ack, int timeout) { struct usbio_bmsg *header; struct usbio_dev *bridge = usb_get_intfdata(stub->intf); int ret; u8 flags = CMPL_FLAG; int actual; if (bridge->state == BRIDGE_STOPPED) return -ENODEV; if (obuf_len > MAX_PAYLOAD_SIZE) return -EINVAL; if (wait_ack) flags |= ACK_FLAG; stub->ipacket.ibuf_len = 0; header = kmalloc(sizeof(*header) + obuf_len, GFP_KERNEL); if (!header) return -ENOMEM; header->type = stub->type; header->cmd = cmd; header->flags = flags; header->len = obuf_len; memcpy(header->data, obuf, obuf_len); dev_dbg(&bridge->intf->dev, "send: type:0x%x cmd:0x%x flags:0x%x len:%d\n", header->type, header->cmd, header->flags, header->len); usbio_dump(bridge, header->data, header->len); mutex_lock(&bridge->mutex); stub->cur_cmd = cmd; stub->ipacket.ibuf = ibuf; stub->acked = false; usb_autopm_get_interface(bridge->intf); ret = usb_bulk_msg(bridge->udev, usb_sndbulkpipe(bridge->udev, bridge->out_ep), header, sizeof(*header) + obuf_len, &actual, USB_WRITE_TIMEOUT); kfree(header); if (ret || actual != sizeof(*header) + obuf_len) { dev_err(&bridge->intf->dev, "bridge write failed ret:%d total_len:%d\n ", ret, actual); goto error; } if (wait_ack) { ret = wait_event_timeout(bridge->ack_wq, stub->acked, msecs_to_jiffies(timeout)); if (!ret || !stub->acked) { dev_err(&bridge->intf->dev, "acked wait timed out ret:%d timeout:%d ack:%d\n", ret, timeout, stub->acked); ret = -ETIMEDOUT; goto error; } } if (ibuf_len) *ibuf_len = stub->ipacket.ibuf_len; stub->ipacket.ibuf = NULL; stub->ipacket.ibuf_len = 0; ret = 0; error: usb_autopm_put_interface(bridge->intf); mutex_unlock(&bridge->mutex); return ret; } static int usbio_transfer_internal(struct platform_device *pdev, u8 cmd, const void *obuf, int obuf_len, void *ibuf, int *ibuf_len, bool wait_ack) { struct usbio_platform_data *usbio_pdata; struct usbio_dev *bridge; struct usbio_stub *stub; if (!pdev) return -EINVAL; bridge = dev_get_drvdata(pdev->dev.parent); usbio_pdata = dev_get_platdata(&pdev->dev); stub = usbio_stub_find(bridge, usbio_pdata->type); if (IS_ERR(stub)) return PTR_ERR(stub); if (stub->type <= GPIO_STUB) return usbio_control_xfer(stub, cmd, obuf, obuf_len, ibuf, ibuf_len, wait_ack, USB_WRITE_ACK_TIMEOUT); else return usbio_bulk_write(stub, cmd, obuf, obuf_len, ibuf, ibuf_len, wait_ack, USB_WRITE_ACK_TIMEOUT); } int usbio_transfer(struct platform_device *pdev, u8 cmd, const void *obuf, int obuf_len, void *ibuf, int *ibuf_len) { return usbio_transfer_internal(pdev, cmd, obuf, obuf_len, ibuf, ibuf_len, true); } EXPORT_SYMBOL_GPL(usbio_transfer); int usbio_transfer_noack(struct platform_device *pdev, u8 cmd, const void *obuf, int obuf_len) { return usbio_transfer_internal(pdev, cmd, obuf, obuf_len, NULL, NULL, false); } EXPORT_SYMBOL_GPL(usbio_transfer_noack); int usbio_register_event_cb(struct platform_device *pdev, usbio_event_cb_t event_cb) { struct usbio_platform_data *usbio_pdata; struct usbio_dev *bridge; struct usbio_stub *stub; unsigned long flags; if (!pdev) return -EINVAL; bridge = dev_get_drvdata(pdev->dev.parent); usbio_pdata = dev_get_platdata(&pdev->dev); stub = usbio_stub_find(bridge, usbio_pdata->type); if (IS_ERR(stub)) return PTR_ERR(stub); spin_lock_irqsave(&stub->event_cb_lock, flags); stub->event_entry.notify = event_cb; stub->event_entry.pdev = pdev; spin_unlock_irqrestore(&stub->event_cb_lock, flags); return 0; } EXPORT_SYMBOL_GPL(usbio_register_event_cb); void usbio_unregister_event_cb(struct platform_device *pdev) { struct usbio_platform_data *usbio_pdata; struct usbio_dev *bridge; struct usbio_stub *stub; unsigned long flags; bridge = dev_get_drvdata(pdev->dev.parent); usbio_pdata = dev_get_platdata(&pdev->dev); stub = usbio_stub_find(bridge, usbio_pdata->type); if (IS_ERR(stub)) return; spin_lock_irqsave(&stub->event_cb_lock, flags); stub->event_entry.notify = NULL; stub->event_entry.pdev = NULL; spin_unlock_irqrestore(&stub->event_cb_lock, flags); } EXPORT_SYMBOL_GPL(usbio_unregister_event_cb); static void usbio_stub_cleanup(struct usbio_dev *bridge) { struct usbio_stub *stub; struct usbio_stub *next; list_for_each_entry_safe (stub, next, &bridge->stubs_list, list) { list_del_init(&stub->list); kfree(stub); } } static void usbio_read_complete(struct urb *urb) { struct usbio_dev *bridge = urb->context; struct usbio_bmsg *header = urb->transfer_buffer; int len = urb->actual_length; int ret; dev_dbg(&bridge->intf->dev, "bulk read urb got message from fw, status:%d data_len:%d\n", urb->status, urb->actual_length); BUG_ON(!bridge); BUG_ON(!header); if (urb->status) { /* sync/async unlink faults aren't errors */ if (urb->status == -ENOENT || urb->status == -ECONNRESET || urb->status == -ESHUTDOWN) return; dev_err(&bridge->intf->dev, "read bulk urb transfer failed: %d\n", urb->status); goto resubmit; } dev_dbg(&bridge->intf->dev, "receive: type:%d cmd:%d flags:%d len:%d\n", header->type, header->cmd, header->flags, header->len); usbio_dump(bridge, header->data, header->len); if (!usbio_validate(header, len)) { dev_err(&bridge->intf->dev, "data not correct header->len:%d payload_len:%d\n ", header->len, len); goto resubmit; } ret = usbio_parse(bridge, header); if (ret) dev_err(&bridge->intf->dev, "failed to parse data: ret:%d type:%d len: %d", ret, header->type, header->len); resubmit: ret = usb_submit_urb(urb, GFP_ATOMIC); if (ret) dev_err(&bridge->intf->dev, "failed submitting read urb, error %d\n", ret); } static int usbio_start(struct usbio_dev *bridge) { int ret; usb_fill_bulk_urb(bridge->in_urb, bridge->udev, usb_rcvbulkpipe(bridge->udev, bridge->in_ep), bridge->ibuf, bridge->ibuf_len, usbio_read_complete, bridge); ret = usb_submit_urb(bridge->in_urb, GFP_KERNEL); if (ret) { dev_err(&bridge->intf->dev, "failed submitting read urb, error %d\n", ret); } return ret; } struct usbio_mng_priv { long reset_id; }; static int usbio_mng_reset_handshake(struct usbio_stub *stub) { int ret; int ilen; ret = usbio_control_xfer(stub, CTRL_RESET_NOTIFY, 0, 0, 0, &ilen, true, USB_WRITE_ACK_TIMEOUT); if (ret) { dev_err(&stub->intf->dev, "CTRL_RESET_NOTIFY failed ret:%d\n", ret); return -EIO; } return 0; } static inline int usbio_mng_reset(struct usbio_stub *stub) { return usbio_control_xfer(stub, CTRL_RESET, NULL, 0, NULL, NULL, true, USB_WRITE_ACK_TIMEOUT); } static int usbio_add_mfd_cell(struct usbio_dev *bridge, struct mfd_cell *cell) { struct mfd_cell *new_cells; /* Enumerate the device even if it does not appear in DSDT */ if (!cell->acpi_match->pnpid) dev_warn(&bridge->intf->dev, "The HID of cell %s does not exist in DSDT\n", cell->name); new_cells = krealloc_array(bridge->cells, (bridge->cell_count + 1), sizeof(struct mfd_cell), GFP_KERNEL); if (!new_cells) return -ENOMEM; memcpy(&new_cells[bridge->cell_count], cell, sizeof(*cell)); bridge->cells = new_cells; bridge->cell_count++; return 0; } static int usbio_gpio_stub_init(struct usbio_dev *bridge, struct usbio_gpio_descriptor *desc) { struct usbio_stub *stub; struct mfd_cell cell = { 0 }; struct usbio_platform_data *pdata; int gpio_num = desc->pins_per_bank * desc->banks; int i; u32 valid_pin[MAX_GPIO_NUM / (sizeof(u32) * BITS_PER_BYTE)]; if (gpio_num > MAX_GPIO_NUM) return -EINVAL; stub = usbio_stub_alloc(bridge, sizeof(*pdata)); if (IS_ERR(stub)) return PTR_ERR(stub); stub->type = GPIO_STUB; stub->intf = bridge->intf; pdata = usbio_priv(stub); pdata->type = stub->type; pdata->gpio_info.num = gpio_num; for (i = 0; i < desc->banks; i++) { valid_pin[i] = desc->bank_desc[i].valid_pins; dev_info(&bridge->intf->dev, "bank:%d map:0x%08x\n", i, valid_pin[i]); } bitmap_from_arr32(pdata->gpio_info.valid_pin_map, valid_pin, gpio_num); cell.name = "usbio-gpio"; cell.platform_data = pdata; cell.pdata_size = sizeof(*pdata); cell.acpi_match = &usbio_acpi_match_gpio; return usbio_add_mfd_cell(bridge, &cell); } static int usbio_mng_enum_gpio(struct usbio_stub *stub) { struct usbio_dev *bridge = usb_get_intfdata(stub->intf); struct usbio_gpio_descriptor *desc; int ret; int len; desc = kzalloc(MAX_PAYLOAD_SIZE, GFP_KERNEL); if (!desc) return -ENOMEM; ret = usbio_control_xfer(stub, CTRL_ENUM_GPIO, NULL, 0, desc->bank_desc, &len, true, USB_ENUM_STUB_TIMEOUT); if (ret || !len || (len % sizeof(*desc->bank_desc))) { dev_err(&stub->intf->dev, "enum gpio failed ret:%d len:%d bank_desc:%ld\n", ret, len, sizeof(*desc->bank_desc)); kfree(desc); return -EIO; } desc->pins_per_bank = GPIO_PER_BANK; desc->banks = len / sizeof(*desc->bank_desc); ret = usbio_gpio_stub_init(bridge, desc); kfree(desc); if (ret) dev_err(&stub->intf->dev, "enum gpio failed ret:%d\n", ret); return ret; } static int usbio_i2c_stub_init(struct usbio_dev *bridge, struct usbio_i2c_descriptor *desc) { struct usbio_stub *stub; struct usbio_platform_data *pdata; int i; int ret; stub = usbio_stub_alloc(bridge, desc->num * sizeof(*pdata)); if (IS_ERR(stub)) return PTR_ERR(stub); stub->type = I2C_STUB; stub->intf = bridge->intf; pdata = usbio_priv(stub); for (i = 0; i < desc->num; i++) { struct mfd_cell cell = { 0 }; pdata[i].type = stub->type; pdata[i].i2c_info.id = desc->info[i].id; pdata[i].i2c_info.capacity = desc->info[i].capacity; cell.name = "usbio-i2c"; cell.platform_data = &pdata[i]; cell.pdata_size = sizeof(pdata[i]); cell.acpi_match = &usbio_acpi_match_i2cs; ret = usbio_add_mfd_cell(bridge, &cell); if (ret) return ret; } return 0; } static int usbio_mng_enum_i2c(struct usbio_stub *stub) { struct usbio_dev *bridge = usb_get_intfdata(stub->intf); struct usbio_i2c_descriptor *desc; int ret; int len; desc = kzalloc(MAX_PAYLOAD_SIZE, GFP_KERNEL); if (!desc) return -ENOMEM; ret = usbio_control_xfer(stub, CTRL_ENUM_I2C, NULL, 0, desc->info, &len, true, USB_ENUM_STUB_TIMEOUT); if (ret || !len || (len % sizeof(*desc->info))) { dev_err(&stub->intf->dev, "CTRL_ENUM_I2C failed ret:%d len:%d num:%d\n", ret, len, desc->num); kfree(desc); return -EIO; } desc->num = len / sizeof(*desc->info); ret = usbio_i2c_stub_init(bridge, desc); kfree(desc); return ret; } static int usbio_spi_stub_init(struct usbio_dev *bridge, struct usbio_spi_descriptor *desc) { struct usbio_stub *stub; struct usbio_platform_data *pdata; int i; int ret; stub = usbio_stub_alloc(bridge, desc->num * sizeof(*pdata)); if (IS_ERR(stub)) return PTR_ERR(stub); stub->type = SPI_STUB; stub->intf = bridge->intf; pdata = usbio_priv(stub); for (i = 0; i < desc->num; i++) { struct mfd_cell cell = { 0 }; pdata[i].type = stub->type; pdata[i].spi_info.id = desc->info[i].id; pdata[i].spi_info.capacity = desc->info[i].capacity; cell.name = "usbio-spi"; cell.platform_data = &pdata[i]; cell.pdata_size = sizeof(pdata[i]); cell.acpi_match = &usbio_acpi_match_spis; ret = usbio_add_mfd_cell(bridge, &cell); if (ret) return ret; } return 0; } static int usbio_mng_enum_spi(struct usbio_stub *stub) { struct usbio_dev *bridge = usb_get_intfdata(stub->intf); struct usbio_spi_descriptor *desc; int ret; int len; desc = kzalloc(MAX_PAYLOAD_SIZE, GFP_KERNEL); if (!desc) return -ENOMEM; ret = usbio_control_xfer(stub, CTRL_ENUM_SPI, NULL, 0, desc, &len, true, USB_ENUM_STUB_TIMEOUT); if (ret) { dev_err(&stub->intf->dev, "CTRL_ENUM_SPI failed ret:%d len:%d num:%d\n", ret, len, desc->num); kfree(desc); return -EIO; } ret = usbio_spi_stub_init(bridge, desc); kfree(desc); return ret; } static int usbio_mng_get_version(struct usbio_stub *stub, char *buf) { struct fw_version version = { 0 }; int ret; int len; if (!buf) return -EINVAL; ret = usbio_control_xfer(stub, CTRL_FW_VERSION, NULL, 0, &version, &len, true, USB_WRITE_ACK_TIMEOUT); if (ret || len < sizeof(struct fw_version)) { dev_err(&stub->intf->dev, "CTRL_GET_VERSION failed ret:%d len:%d\n", ret, len); return ret; } return sysfs_emit(buf, "%d.%d.%d.%d\n", version.major, version.minor, le16_to_cpu(version.patch), le16_to_cpu(version.build)); } static inline int usbio_mng_set_dfu_mode(struct usbio_stub *stub) { return usbio_control_xfer(stub, CTRL_SET_DFU_MODE, NULL, 0, NULL, NULL, true, USB_WRITE_ACK_TIMEOUT); } static int usbio_mng_link(struct usbio_dev *bridge, struct usbio_stub *stub) { int ret; ret = usbio_mng_reset_handshake(stub); if (ret) return ret; bridge->state = BRIDGE_RESET_SYNCED; /* workaround for FW limitation, ignore return value of enum result */ usbio_mng_enum_gpio(stub); bridge->state = BRIDGE_ENUM_GPIO_COMPLETE; usbio_mng_enum_i2c(stub); bridge->state = BRIDGE_ENUM_I2C_COMPLETE; usbio_mng_enum_spi(stub); bridge->state = BRIDGE_ENUM_SPI_COMPLETE; return 0; } static int usbio_mng_init(struct usbio_dev *bridge) { struct usbio_stub *stub; struct usbio_mng_priv *priv; int ret; stub = usbio_stub_alloc(bridge, sizeof(*priv)); if (IS_ERR(stub)) return PTR_ERR(stub); priv = usbio_priv(stub); if (!priv) return -ENOMEM; priv->reset_id = 0; stub->type = CTRL_STUB; stub->intf = bridge->intf; ret = usbio_mng_link(bridge, stub); if (ret) dev_err(&bridge->intf->dev, "mng stub link done ret:%d state:%d\n", ret, bridge->state); return ret; } static inline int usbio_diag_get_fw_log(struct usbio_stub *stub, void *buf) { int ret; int len; if (!buf) return -EINVAL; ret = usbio_control_xfer(stub, DIAG_GET_FW_LOG, NULL, 0, buf, &len, true, USB_WRITE_ACK_TIMEOUT); if (ret) return ret; return len; } static inline int usbio_diag_get_coredump(struct usbio_stub *stub, void *buf) { int ret; int len; if (!buf) return -EINVAL; ret = usbio_control_xfer(stub, DIAG_GET_FW_COREDUMP, NULL, 0, buf, &len, true, USB_WRITE_ACK_TIMEOUT); if (ret) return ret; return len; } static inline int usbio_diag_set_trace_level(struct usbio_stub *stub, u8 level) { return usbio_control_xfer(stub, DIAG_SET_TRACE_LEVEL, &level, sizeof(level), NULL, NULL, true, USB_WRITE_ACK_TIMEOUT); } static int usbio_diag_init(struct usbio_dev *bridge) { struct usbio_stub *stub; stub = usbio_stub_alloc(bridge, 0); if (IS_ERR(stub)) return PTR_ERR(stub); stub->type = DIAG_STUB; stub->intf = bridge->intf; return 0; } static void usbio_delete(struct usbio_dev *bridge) { mutex_destroy(&bridge->mutex); usb_free_urb(bridge->in_urb); usb_put_intf(bridge->intf); usb_put_dev(bridge->udev); kfree(bridge->ibuf); kfree(bridge->cells); kfree(bridge); } static int usbio_init(struct usbio_dev *bridge) { mutex_init(&bridge->mutex); init_waitqueue_head(&bridge->ack_wq); INIT_LIST_HEAD(&bridge->stubs_list); bridge->state = BRIDGE_INITED; return 0; } static void usbio_stop(struct usbio_dev *bridge) { usb_kill_urb(bridge->in_urb); } static ssize_t cmd_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { struct usb_interface *intf = to_usb_interface(dev); struct usbio_dev *bridge = usb_get_intfdata(intf); struct usbio_stub *mng_stub = usbio_stub_find(bridge, CTRL_STUB); struct usbio_stub *diag_stub = usbio_stub_find(bridge, DIAG_STUB); if (sysfs_streq(buf, "dfu")) usbio_mng_set_dfu_mode(mng_stub); else if (sysfs_streq(buf, "reset")) usbio_mng_reset(mng_stub); else if (sysfs_streq(buf, "debug")) usbio_diag_set_trace_level(diag_stub, 3); return count; } static ssize_t cmd_show(struct device *dev, struct device_attribute *attr, char *buf) { return sysfs_emit(buf, "%s\n", "supported cmd: [reset, debug]"); } static DEVICE_ATTR_RW(cmd); static ssize_t version_show(struct device *dev, struct device_attribute *attr, char *buf) { struct usb_interface *intf = to_usb_interface(dev); struct usbio_dev *bridge = usb_get_intfdata(intf); struct usbio_stub *stub = usbio_stub_find(bridge, CTRL_STUB); return usbio_mng_get_version(stub, buf); } static DEVICE_ATTR_RO(version); static ssize_t log_show(struct device *dev, struct device_attribute *attr, char *buf) { struct usb_interface *intf = to_usb_interface(dev); struct usbio_dev *bridge = usb_get_intfdata(intf); struct usbio_stub *diag_stub = usbio_stub_find(bridge, DIAG_STUB); memcpy(buf, "USBIO log: TBD\n", 15); return usbio_diag_get_fw_log(diag_stub, buf); } static DEVICE_ATTR_RO(log); static ssize_t coredump_show(struct device *dev, struct device_attribute *attr, char *buf) { struct usb_interface *intf = to_usb_interface(dev); struct usbio_dev *bridge = usb_get_intfdata(intf); struct usbio_stub *diag_stub = usbio_stub_find(bridge, DIAG_STUB); return usbio_diag_get_coredump(diag_stub, buf); } static DEVICE_ATTR_RO(coredump); static struct attribute *usbio_attrs[] = { &dev_attr_version.attr, &dev_attr_cmd.attr, &dev_attr_log.attr, &dev_attr_coredump.attr, NULL, }; ATTRIBUTE_GROUPS(usbio); static int usbio_probe(struct usb_interface *intf, const struct usb_device_id *id) { struct usbio_dev *bridge; struct usb_endpoint_descriptor *ep0, *bulk_in, *bulk_out; int ret; ret = precheck_acpi_hid(intf); if (ret) return ret; /* allocate memory for our device state and initialize it */ bridge = kzalloc(sizeof(*bridge), GFP_KERNEL); if (!bridge) return -ENOMEM; usbio_init(bridge); bridge->udev = usb_get_dev(interface_to_usbdev(intf)); bridge->intf = usb_get_intf(intf); /* control transfer enpoint information */ if (!&bridge->udev->ep0) { dev_err(&intf->dev, "Could not find control endpoint\n"); goto error; } ep0 = &bridge->udev->ep0.desc; if (!ep0) { dev_err(&intf->dev, "Could not find control endpoint descriptor\n"); goto error; } bridge->ep0 = ep0->bEndpointAddress; bridge->cbuf_len = usb_endpoint_maxp(ep0); bridge->cbuf = kzalloc(bridge->cbuf_len, GFP_KERNEL); if (!bridge->cbuf) { ret = -ENOMEM; goto error; } dev_dbg(&intf->dev, "ep0 addr:%d size:%u\n", bridge->ep0, bridge->cbuf_len); /* set up the endpoint information use only the first bulk-in and bulk-out endpoints */ ret = usb_find_common_endpoints(intf->cur_altsetting, &bulk_in, &bulk_out, NULL, NULL); if (ret) { dev_err(&intf->dev, "Could not find both bulk-in and bulk-out endpoints\n"); goto error; } bridge->in_ep = bulk_in->bEndpointAddress; bridge->ibuf_len = usb_endpoint_maxp(bulk_in); bridge->ibuf = kzalloc(bridge->ibuf_len, GFP_KERNEL); if (!bridge->ibuf) { ret = -ENOMEM; goto error; } bridge->in_urb = usb_alloc_urb(0, GFP_KERNEL); if (!bridge->in_urb) { ret = -ENOMEM; goto error; } bridge->out_ep = bulk_out->bEndpointAddress; dev_dbg(&intf->dev, "bulk_in addr:%d bulk_out addr:%d size:%u\n", bridge->in_ep, bridge->out_ep, bridge->ibuf_len); /* save our data pointer in this intf device */ usb_set_intfdata(intf, bridge); ret = usbio_start(bridge); if (ret) { dev_err(&intf->dev, "bridge read start failed ret %d\n", ret); goto error; } ret = usbio_mng_init(bridge); if (ret) { dev_err(&intf->dev, "register mng stub failed ret %d\n", ret); goto error_stop; } ret = usbio_diag_init(bridge); if (ret) { dev_err(&intf->dev, "register diag stub failed ret %d\n", ret); goto error_stop; } ret = mfd_add_hotplug_devices(&intf->dev, bridge->cells, bridge->cell_count); if (ret) { dev_err(&intf->dev, "failed to add mfd devices to core %d\n", bridge->cell_count); goto error_stop; } bridge->state = BRIDGE_STARTED; dev_info(&intf->dev, "USB Bridge device init success\n"); return 0; error_stop: usbio_stop(bridge); error: dev_err(&intf->dev, "USB Bridge device init failed\n"); /* this frees allocated memory */ usbio_stub_cleanup(bridge); usbio_delete(bridge); return ret; } static void usbio_disconnect(struct usb_interface *intf) { struct usbio_dev *bridge; bridge = usb_get_intfdata(intf); usbio_stop(bridge); bridge->state = BRIDGE_STOPPED; mfd_remove_devices(&intf->dev); usbio_stub_cleanup(bridge); usb_set_intfdata(intf, NULL); usbio_delete(bridge); dev_info(&intf->dev, "USB Bridge disconnected\n"); } static int usbio_suspend(struct usb_interface *intf, pm_message_t message) { struct usbio_dev *bridge = usb_get_intfdata(intf); usbio_stop(bridge); bridge->state = BRIDGE_SUSPEND; dev_dbg(&intf->dev, "USB Bridge suspend\n"); return 0; } static int usbio_resume(struct usb_interface *intf) { struct usbio_dev *bridge = usb_get_intfdata(intf); bridge->state = BRIDGE_STARTED; dev_dbg(&intf->dev, "USB Bridge resume\n"); return usbio_start(bridge); } static const struct usb_device_id usbio_table[] = { {USB_DEVICE(0x2AC1, 0x20C1)}, /* Lattice NX40 */ {USB_DEVICE(0x2AC1, 0x20C9)}, /* Lattice NX33 */ {USB_DEVICE(0x2AC1, 0x20CB)}, /* Lattice NX33U */ {} }; MODULE_DEVICE_TABLE(usb, usbio_table); static struct usb_driver usbbridge_driver = { .name = "usbio-bridge", .probe = usbio_probe, .disconnect = usbio_disconnect, .suspend = usbio_suspend, .resume = usbio_resume, .id_table = usbio_table, .dev_groups = usbio_groups, .supports_autosuspend = 1, }; module_usb_driver(usbbridge_driver); MODULE_AUTHOR("Ye Xiang "); MODULE_AUTHOR("Zhang Lixu "); MODULE_AUTHOR("Israel Cepeda "); MODULE_AUTHOR("Lifu Wang "); MODULE_DESCRIPTION("Intel USBIO Bridge driver"); MODULE_VERSION(USBIO_VERSION); MODULE_LICENSE("GPL v2"); usbio-drivers-0~git202312141918.78ffb706/include/000077500000000000000000000000001453665242200206605ustar00rootroot00000000000000usbio-drivers-0~git202312141918.78ffb706/include/linux/000077500000000000000000000000001453665242200220175ustar00rootroot00000000000000usbio-drivers-0~git202312141918.78ffb706/include/linux/mfd/000077500000000000000000000000001453665242200225655ustar00rootroot00000000000000usbio-drivers-0~git202312141918.78ffb706/include/linux/mfd/usbio.h000066400000000000000000000021321453665242200240550ustar00rootroot00000000000000/* SPDX-License-Identifier: GPL-2.0 */ #ifndef __LINUX_USB_IO_H #define __LINUX_USB_IO_H #include #include #include #define MAX_BANK_NUM 5 #define GPIO_PER_BANK 32 #define MAX_GPIO_NUM (MAX_BANK_NUM * GPIO_PER_BANK) struct usbio_gpio_info { int num; DECLARE_BITMAP(valid_pin_map, MAX_GPIO_NUM); }; struct usbio_i2c_info { u8 id; u8 capacity; }; struct usbio_spi_info { u8 id; u8 capacity; }; struct usbio_platform_data { int type; union { struct usbio_gpio_info gpio_info; struct usbio_i2c_info i2c_info; struct usbio_spi_info spi_info; }; }; typedef void (*usbio_event_cb_t)(struct platform_device *pdev, u8 cmd, const void *evt_data, int len); int usbio_register_event_cb(struct platform_device *pdev, usbio_event_cb_t event_cb); void usbi_unregister_event_cb(struct platform_device *pdev); int usbio_transfer(struct platform_device *pdev, u8 cmd, const void *obuf, int obuf_len, void *ibuf, int *ibuf_len); int usbio_transfer_noack(struct platform_device *pdev, u8 cmd, const void *obuf, int obuf_len); #endif usbio-drivers-0~git202312141918.78ffb706/security.md000066400000000000000000000006261453665242200214320ustar00rootroot00000000000000# Security Policy Intel is committed to rapidly addressing security vulnerabilities affecting our customers and providing clear guidance on the solution, impact, severity and mitigation. ## Reporting a Vulnerability Please report any security vulnerabilities in this project utilizing the guidelines [here](https://www.intel.com/content/www/us/en/security-center/vulnerability-handling-guidelines.html).