diff --git a/LICENSES/GPL-2.0-only.txt b/LICENSES/GPL-2.0-only.txt new file mode 100644 index 0000000000000000000000000000000000000000..d159169d1050894d3ea3b98e1c965c4058208fe1 --- /dev/null +++ b/LICENSES/GPL-2.0-only.txt @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + <one line to give the program's name and a brief idea of what it does.> + Copyright (C) <year> <name of author> + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + <signature of Ty Coon>, 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. diff --git a/recipes-kernel/linux-yocto/linux-yocto/driver-add-hilog-and-hievent-buffer-management-drive.cfg b/recipes-kernel/linux-yocto/linux-yocto/driver-add-hilog-and-hievent-buffer-management-drive.cfg new file mode 100644 index 0000000000000000000000000000000000000000..8352c01bf36007e0f31e703435c35ead63fde50d --- /dev/null +++ b/recipes-kernel/linux-yocto/linux-yocto/driver-add-hilog-and-hievent-buffer-management-drive.cfg @@ -0,0 +1,7 @@ +# SPDX-FileCopyrightText: Huawei Inc. +# +# SPDX-License-Identifier: Apache-2.0 + +CONFIG_STAGING=y +CONFIG_HILOG=y +CONFIG_HIEVENT=y diff --git a/recipes-kernel/linux-yocto/linux-yocto/driver-add-hilog-and-hievent-buffer-management-drive.patch b/recipes-kernel/linux-yocto/linux-yocto/driver-add-hilog-and-hievent-buffer-management-drive.patch new file mode 100644 index 0000000000000000000000000000000000000000..fdd3ea7f5528b9f59a73a9931b3cd7f37a8972ed --- /dev/null +++ b/recipes-kernel/linux-yocto/linux-yocto/driver-add-hilog-and-hievent-buffer-management-drive.patch @@ -0,0 +1,1539 @@ +From dab1259b6d11e2c3fc794cca2d55e7f984828a0f Mon Sep 17 00:00:00 2001 +From: stesen <stesen.ma@huawei.com> +Date: Wed, 21 Jul 2021 15:14:30 +0800 +Subject: [PATCH] driver: add hilog and hievent buffer management drivers +Content-Type: text/plain; charset = "utf-8" +Content-Transfert-Encoding: 8bit + +ohos inclusion +category: feature +feature: dfx drivers +issue: #I3XQ1T + +-------------------------------- + +Hilog driver is used by the hilog feature on small devices. +It manage the log buffer and provide an synced log read and +asynchronous write logic. + +Hievent Driver is similar to the hilog driver, but used by +the hievent feature. + +Signed-off-by: stesen <stesen.ma@huawei.com> +Signed-off-by: Zachery Wu <wuzuoyu@huawei.com> + +Upstream-Status: Inappropriate [ OpenHarmony specific ] +--- + drivers/staging/Kconfig | 4 + + drivers/staging/Makefile | 2 + + drivers/staging/hievent/Kconfig | 4 + + drivers/staging/hievent/Makefile | 3 + + drivers/staging/hievent/hievent_driver.c | 422 +++++++++++++++++++ + drivers/staging/hievent/hievent_driver.h | 32 ++ + drivers/staging/hievent/hiview_hievent.c | 498 +++++++++++++++++++++++ + drivers/staging/hievent/hiview_hievent.h | 44 ++ + drivers/staging/hilog/Kconfig | 21 + + drivers/staging/hilog/Makefile | 5 + + drivers/staging/hilog/hilog.c | 381 +++++++++++++++++ + 11 files changed, 1416 insertions(+) + create mode 100644 drivers/staging/hievent/Kconfig + create mode 100644 drivers/staging/hievent/Makefile + create mode 100644 drivers/staging/hievent/hievent_driver.c + create mode 100644 drivers/staging/hievent/hievent_driver.h + create mode 100644 drivers/staging/hievent/hiview_hievent.c + create mode 100644 drivers/staging/hievent/hiview_hievent.h + create mode 100644 drivers/staging/hilog/Kconfig + create mode 100644 drivers/staging/hilog/Makefile + create mode 100644 drivers/staging/hilog/hilog.c + +diff --git a/drivers/staging/Kconfig b/drivers/staging/Kconfig +index 2d0310448eba..4dbdc87e809c 100644 +--- a/drivers/staging/Kconfig ++++ b/drivers/staging/Kconfig +@@ -118,4 +118,8 @@ source "drivers/staging/wfx/Kconfig" + + source "drivers/staging/hikey9xx/Kconfig" + ++source "drivers/staging/hilog/Kconfig" ++ ++source "drivers/staging/hievent/Kconfig" ++ + endif # STAGING +diff --git a/drivers/staging/Makefile b/drivers/staging/Makefile +index 757a892ab5b9..b33dfbdcce7e 100644 +--- a/drivers/staging/Makefile ++++ b/drivers/staging/Makefile +@@ -49,3 +49,5 @@ obj-$(CONFIG_KPC2000) += kpc2000/ + obj-$(CONFIG_QLGE) += qlge/ + obj-$(CONFIG_WFX) += wfx/ + obj-y += hikey9xx/ ++obj-$(CONFIG_HILOG) += hilog/ ++obj-$(CONFIG_HIEVENT) += hievent/ +diff --git a/drivers/staging/hievent/Kconfig b/drivers/staging/hievent/Kconfig +new file mode 100644 +index 000000000000..39da4c041ba9 +--- /dev/null ++++ b/drivers/staging/hievent/Kconfig +@@ -0,0 +1,4 @@ ++config HIEVENT ++ tristate "Enable hievent" ++ help ++ hievent buffer manager +diff --git a/drivers/staging/hievent/Makefile b/drivers/staging/hievent/Makefile +new file mode 100644 +index 000000000000..530802d59156 +--- /dev/null ++++ b/drivers/staging/hievent/Makefile +@@ -0,0 +1,3 @@ ++ ++obj-$(CONFIG_HIEVENT) += hievent_driver.o ++obj-$(CONFIG_HIEVENT) += hiview_hievent.o +\ No newline at end of file +diff --git a/drivers/staging/hievent/hievent_driver.c b/drivers/staging/hievent/hievent_driver.c +new file mode 100644 +index 000000000000..f6b378766abc +--- /dev/null ++++ b/drivers/staging/hievent/hievent_driver.c +@@ -0,0 +1,422 @@ ++// SPDX-License-Identifier: GPL-2.0 ++/* ++ * Copyright (C) 2021 Huawei Technologies Co., Ltd. All rights reserved. ++ * ++ * This software is licensed under the terms of the GNU General Public ++ * License version 2, as published by the Free Software Foundation, and ++ * may be copied, distributed, and modified under those terms. ++ * ++ * This program is distributed in the hope that it will be useful, ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++ * GNU General Public License for more details. ++ * ++ */ ++ ++#include "hievent_driver.h" ++ ++#include <linux/init.h> ++#include <linux/module.h> ++#include <linux/mutex.h> ++#include <linux/uaccess.h> ++#include <linux/fs.h> ++#include <linux/slab.h> ++#include <linux/cdev.h> ++#include <linux/sched.h> ++#include <linux/time.h> ++#include <linux/timex.h> ++#include <linux/rtc.h> ++#include <linux/version.h> ++#include <linux/uio.h> ++#include <linux/list.h> ++#include <linux/wait.h> ++ ++#ifndef HIEVENTDEV_MAJOR ++#define HIEVENTDEV_MAJOR 241 ++#endif ++ ++#ifndef HIEVENT_NR_DEVS ++#define HIEVENT_NR_DEVS 2 ++#endif ++ ++static int hievent_major = HIEVENTDEV_MAJOR; ++ ++static struct cdev hievent_cdev; ++ ++#define HIEVENT_BUFFER ((size_t)1024) ++#define HIEVENT_DRIVER "/dev/hwlog_exception" ++ ++struct hievent_entry { ++ unsigned short len; ++ unsigned short header_size; ++ int pid; ++ int tid; ++ int sec; ++ int nsec; ++ char msg[0]; ++}; ++ ++struct hievent_char_device { ++ int flag; ++ struct mutex mtx; /* lock to protect read/write buffer */ ++ unsigned char *buffer; ++ wait_queue_head_t wq; ++ size_t write_offset; ++ size_t head_offset; ++ size_t size; ++ size_t count; ++} hievent_dev; ++ ++static inline unsigned char *hievent_buffer_head(void) ++{ ++ if (hievent_dev.head_offset > HIEVENT_BUFFER) ++ hievent_dev.head_offset = ++ hievent_dev.head_offset % HIEVENT_BUFFER; ++ ++ return hievent_dev.buffer + hievent_dev.head_offset; ++} ++ ++static void hievent_buffer_inc(size_t sz) ++{ ++ if (hievent_dev.size + sz <= HIEVENT_BUFFER) { ++ hievent_dev.size += sz; ++ hievent_dev.write_offset += sz; ++ hievent_dev.write_offset %= HIEVENT_BUFFER; ++ hievent_dev.count++; ++ } ++} ++ ++static void hievent_buffer_dec(size_t sz) ++{ ++ if (hievent_dev.size >= sz) { ++ hievent_dev.size -= sz; ++ hievent_dev.head_offset += sz; ++ hievent_dev.head_offset %= HIEVENT_BUFFER; ++ hievent_dev.count--; ++ } ++} ++ ++static int hievent_read_ring_buffer(unsigned char __user *buffer, ++ size_t buf_len) ++{ ++ size_t retval; ++ size_t buf_left = HIEVENT_BUFFER - hievent_dev.head_offset; ++ ++ if (buf_left > buf_len) { ++ retval = copy_to_user(buffer, hievent_buffer_head(), buf_len); ++ } else { ++ size_t mem_len = (buf_len > buf_left) ? buf_left : buf_len; ++ ++ retval = copy_to_user(buffer, hievent_buffer_head(), mem_len); ++ if (retval < 0) ++ return retval; ++ ++ retval = copy_to_user(buffer + buf_left, hievent_dev.buffer, ++ buf_len - buf_left); ++ } ++ return retval; ++} ++ ++static int hievent_read_ring_head_buffer(unsigned char * const buffer, ++ size_t buf_len) ++{ ++ size_t buf_left = HIEVENT_BUFFER - hievent_dev.head_offset; ++ ++ if (buf_left > buf_len) { ++ memcpy(buffer, hievent_buffer_head(), buf_len); ++ } else { ++ size_t mem_len = (buf_len > buf_left) ? buf_left : buf_len; ++ ++ memcpy(buffer, hievent_buffer_head(), mem_len); ++ memcpy(buffer + buf_left, hievent_dev.buffer, ++ buf_len - buf_left); ++ } ++ return 0; ++} ++ ++static ssize_t hievent_read(struct file *file, char __user *user_buf, ++ size_t count, loff_t *ppos) ++{ ++ size_t retval; ++ struct hievent_entry header; ++ ++ (void)file; ++ ++ wait_event_interruptible(hievent_dev.wq, (hievent_dev.size > 0)); ++ ++ (void)mutex_lock(&hievent_dev.mtx); ++ ++ retval = hievent_read_ring_head_buffer((unsigned char *)&header, ++ sizeof(header)); ++ if (retval < 0) { ++ retval = -EINVAL; ++ goto out; ++ } ++ ++ if (count < header.len + sizeof(header)) { ++ retval = -ENOMEM; ++ goto out; ++ } ++ ++ hievent_buffer_dec(sizeof(header)); ++ retval = copy_to_user((unsigned char *)user_buf, ++ (unsigned char *)&header, ++ min(count, sizeof(header))); ++ if (retval < 0) { ++ retval = -EINVAL; ++ goto out; ++ } ++ ++ retval = hievent_read_ring_buffer((unsigned char *)(user_buf + ++ sizeof(header)), header.len); ++ if (retval < 0) { ++ retval = -EINVAL; ++ goto out; ++ } ++ ++ hievent_buffer_dec(header.len); ++ ++ retval = header.len + sizeof(header); ++out: ++ (void)mutex_unlock(&hievent_dev.mtx); ++ ++ return retval; ++} ++ ++static int hievent_write_ring_head_buffer(const unsigned char *buffer, ++ size_t buf_len) ++{ ++ size_t buf_left = HIEVENT_BUFFER - hievent_dev.write_offset; ++ ++ if (buf_len > buf_left) { ++ memcpy(hievent_dev.buffer + hievent_dev.write_offset, ++ buffer, buf_left); ++ memcpy(hievent_dev.buffer, buffer + buf_left, ++ min(HIEVENT_BUFFER, buf_len - buf_left)); ++ } else { ++ memcpy(hievent_dev.buffer + hievent_dev.write_offset, ++ buffer, min(buf_left, buf_len)); ++ } ++ ++ return 0; ++} ++ ++static void hievent_head_init(struct hievent_entry * const header, size_t len) ++{ ++#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0) ++#define NANOSEC_PER_MIRCOSEC 1000 ++ struct timeval now = { 0 }; ++ ++ do_gettimeofday(&now); ++ ++ header->sec = now.tv_sec; ++ header->nsec = now.tv_usec * NANOSEC_PER_MIRCOSEC; ++#else ++ struct timespec64 now = { 0 }; ++ ++ ktime_get_real_ts64(&now); ++ ++ header->sec = now.tv_sec; ++ header->nsec = now.tv_nsec; ++#endif ++ ++ header->len = (unsigned short)len; ++ header->pid = current->pid; ++ header->tid = 0; ++ header->header_size = sizeof(struct hievent_entry); ++} ++ ++static void hievent_cover_old_log(size_t buf_len) ++{ ++ int retval; ++ struct hievent_entry header; ++ size_t total_size = buf_len + sizeof(struct hievent_entry); ++ ++ while (total_size + hievent_dev.size >= HIEVENT_BUFFER) { ++ retval = hievent_read_ring_head_buffer((unsigned char *)&header, ++ sizeof(header)); ++ if (retval < 0) ++ break; ++ ++ /* let count decrease twice */ ++ hievent_buffer_dec(sizeof(header)); ++ hievent_buffer_dec(header.len); ++ } ++} ++ ++int hievent_write_internal(const char *buffer, size_t buf_len) ++{ ++ struct hievent_entry header; ++ int retval; ++ ++ if (buf_len < sizeof(int) || ++ buf_len > HIEVENT_BUFFER - sizeof(struct hievent_entry)) ++ return -EINVAL; ++ ++ (void)mutex_lock(&hievent_dev.mtx); ++ ++ hievent_cover_old_log(buf_len); ++ ++ hievent_head_init(&header, buf_len); ++ ++ retval = hievent_write_ring_head_buffer((unsigned char *)&header, ++ sizeof(header)); ++ if (retval) { ++ retval = -EINVAL; ++ goto out; ++ } ++ hievent_buffer_inc(sizeof(header)); ++ ++ retval = hievent_write_ring_head_buffer((unsigned char *)(buffer), ++ header.len); ++ if (retval) { ++ retval = -EINVAL; ++ goto out; ++ } ++ ++ hievent_buffer_inc(header.len); ++ ++ retval = header.len; ++ ++out: ++ (void)mutex_unlock(&hievent_dev.mtx); ++ if (retval > 0) ++ wake_up_interruptible(&hievent_dev.wq); ++ ++ return retval; ++} ++ ++static unsigned int hievent_poll(struct file *filep, ++ struct poll_table_struct *fds) ++{ ++ (void)filep; ++ (void)fds; ++ ++ wait_event_interruptible(hievent_dev.wq, (hievent_dev.size > 0)); ++ ++ return (POLLOUT | POLLWRNORM); ++} ++ ++static ssize_t hievent_write_iter(struct kiocb *iocb, struct iov_iter *from) ++{ ++ int check_code = 0; ++ unsigned char *temp_buffer = NULL; ++ const struct iovec *iov = from->iov; ++ int retval; ++ int buf_len; ++ ++ (void)iocb; ++ if (from->nr_segs != 3) { /* must contain 3 segments */ ++ retval = -EINVAL; ++ goto out; ++ } ++ ++ retval = copy_from_user(&check_code, iov[0].iov_base, ++ sizeof(check_code)); ++ if (retval || check_code != CHECK_CODE) { ++ retval = -EINVAL; ++ goto out; ++ } ++ ++ /* seg 1 && 2 is head info */ ++ buf_len = iov[1].iov_len + iov[2].iov_len; ++ if (buf_len > HIEVENT_BUFFER - sizeof(struct hievent_entry)) { ++ retval = -ENOMEM; ++ goto out; ++ } ++ ++ temp_buffer = kmalloc(buf_len, GFP_KERNEL); ++ if (!temp_buffer) { ++ retval = -ENOMEM; ++ goto out; ++ } ++ ++ retval = copy_from_user(temp_buffer, iov[1].iov_base, iov[1].iov_len); ++ if (retval) { ++ retval = -EIO; ++ goto free_mem; ++ } ++ ++ /* 1 2 head info */ ++ retval = copy_from_user(temp_buffer + iov[1].iov_len, iov[2].iov_base, ++ iov[2].iov_len); ++ if (retval) { ++ retval = -EIO; ++ goto free_mem; ++ } ++ ++ retval = hievent_write_internal(temp_buffer, buf_len); ++ if (retval) { ++ retval = -EIO; ++ goto free_mem; ++ } ++ ++ retval = buf_len + iov[0].iov_len; ++ ++free_mem: ++ kfree(temp_buffer); ++ ++out: ++ return retval; ++} ++ ++static const struct file_operations hievent_fops = { ++ .read = hievent_read, /* read */ ++ .poll = hievent_poll, /* poll */ ++ .write_iter = hievent_write_iter, /* write_iter */ ++}; ++ ++static void hievent_device_init(void) ++{ ++ hievent_dev.buffer = kmalloc(HIEVENT_BUFFER, GFP_KERNEL); ++ if (!hievent_dev.buffer) ++ return; ++ ++ init_waitqueue_head(&hievent_dev.wq); ++ mutex_init(&hievent_dev.mtx); ++ hievent_dev.write_offset = 0; ++ hievent_dev.head_offset = 0; ++ hievent_dev.size = 0; ++ hievent_dev.count = 0; ++} ++ ++static int __init hieventdev_init(void) ++{ ++ int result; ++ dev_t devno = MKDEV(hievent_major, 0); ++ ++ result = register_chrdev_region(devno, 2, "hwlog_exception"); ++ if (result < 0) ++ return result; ++ ++ cdev_init(&hievent_cdev, &hievent_fops); ++ hievent_cdev.owner = THIS_MODULE; ++ hievent_cdev.ops = &hievent_fops; ++ ++ cdev_add(&hievent_cdev, MKDEV(hievent_major, 0), HIEVENT_NR_DEVS); ++ ++ hievent_device_init(); ++ return 0; ++} ++ ++static void __exit hievent_exit_module(void) ++{ ++ cdev_del(&hievent_cdev); ++ unregister_chrdev_region(MKDEV(hievent_major, 0), HIEVENT_NR_DEVS); ++} ++ ++static int __init hievent_init_module(void) ++{ ++ int state; ++ ++ state = hieventdev_init(); ++ return 0; ++} ++ ++module_init(hievent_init_module); ++module_exit(hievent_exit_module); ++ ++MODULE_AUTHOR("OHOS"); ++MODULE_DESCRIPTION("User mode hievent device interface"); ++MODULE_LICENSE("GPL"); ++MODULE_ALIAS("hievent"); +diff --git a/drivers/staging/hievent/hievent_driver.h b/drivers/staging/hievent/hievent_driver.h +new file mode 100644 +index 000000000000..56eb0c5ea316 +--- /dev/null ++++ b/drivers/staging/hievent/hievent_driver.h +@@ -0,0 +1,32 @@ ++/* SPDX-License-Identifier: GPL-2.0 */ ++/* ++ * Copyright (C) 2021 Huawei Technologies Co., Ltd. All rights reserved. ++ * ++ * This software is licensed under the terms of the GNU General Public ++ * License version 2, as published by the Free Software Foundation, and ++ * may be copied, distributed, and modified under those terms. ++ * ++ * This program is distributed in the hope that it will be useful, ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++ * GNU General Public License for more details. ++ * ++ */ ++ ++#ifndef HIEVENT_DRIVER_H ++#define HIEVENT_DRIVER_H ++ ++#include <linux/types.h> ++ ++#define CHECK_CODE 0x7BCDABCD ++ ++struct idap_header { ++ char level; ++ char category; ++ char log_type; ++ char sn; ++}; ++ ++int hievent_write_internal(const char *buffer, size_t buf_len); ++ ++#endif /* HIEVENT_DRIVER_H */ +diff --git a/drivers/staging/hievent/hiview_hievent.c b/drivers/staging/hievent/hiview_hievent.c +new file mode 100644 +index 000000000000..552965dfacb4 +--- /dev/null ++++ b/drivers/staging/hievent/hiview_hievent.c +@@ -0,0 +1,498 @@ ++// SPDX-License-Identifier: GPL-2.0 ++/* ++ * Copyright (C) 2021 Huawei Technologies Co., Ltd. All rights reserved. ++ * ++ * This software is licensed under the terms of the GNU General Public ++ * License version 2, as published by the Free Software Foundation, and ++ * may be copied, distributed, and modified under those terms. ++ * ++ * This program is distributed in the hope that it will be useful, ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++ * GNU General Public License for more details. ++ * ++ */ ++ ++#include "hiview_hievent.h" ++#include "hievent_driver.h" ++ ++#include <linux/slab.h> ++#include <linux/string.h> ++ ++#define INT_TYPE_MAX_LEN 21 ++ ++#define MAX_PATH_LEN 256 ++#define MAX_STR_LEN (10 * 1024) ++ ++/* 64K is max length of /dev/hwlog_exception */ ++#define EVENT_INFO_BUF_LEN (64 * 1024) ++#define EVENT_INFO_PACK_BUF_LEN (2 * 1024) ++ ++#define BUF_POINTER_FORWARD \ ++do { \ ++ if (tmplen < len) { \ ++ tmp += tmplen; \ ++ len -= tmplen; \ ++ } else { \ ++ tmp += len; \ ++ len = 0; \ ++ } \ ++} while (0) ++ ++struct hievent_payload { ++ char *key; ++ char *value; ++ struct hievent_payload *next; ++}; ++ ++static int hievent_convert_string(struct hiview_hievent *event, char **pbuf); ++ ++static struct hievent_payload *hievent_payload_create(void); ++ ++static void hievent_payload_destroy(struct hievent_payload *p); ++ ++static struct hievent_payload *hievent_get_payload(struct hievent_payload *head, ++ const char *key); ++ ++static void hievent_add_payload(struct hiview_hievent *obj, ++ struct hievent_payload *payload); ++ ++static struct hievent_payload *hievent_payload_create(void) ++{ ++ struct hievent_payload *payload = NULL; ++ ++ payload = kmalloc(sizeof(*payload), GFP_KERNEL); ++ if (!payload) ++ return NULL; ++ ++ payload->key = NULL; ++ payload->value = NULL; ++ payload->next = NULL; ++ ++ return payload; ++} ++ ++static void hievent_payload_destroy(struct hievent_payload *p) ++{ ++ if (!p) ++ return; ++ ++ kfree(p->value); ++ kfree(p->key); ++ kfree(p); ++} ++ ++static struct hievent_payload *hievent_get_payload(struct hievent_payload *head, ++ const char *key) ++{ ++ struct hievent_payload *p = head; ++ ++ while (p) { ++ if (key && p->key) { ++ if (strcmp(p->key, key) == 0) ++ return p; ++ } ++ p = p->next; ++ } ++ ++ return NULL; ++} ++ ++static void hievent_add_payload(struct hiview_hievent *obj, ++ struct hievent_payload *payload) ++{ ++ if (!obj->head) { ++ obj->head = payload; ++ } else { ++ struct hievent_payload *p = obj->head; ++ ++ while (p->next) ++ p = p->next; ++ p->next = payload; ++ } ++} ++ ++struct hiview_hievent *hievent_create(unsigned int eventid) ++{ ++ struct hiview_hievent *event = NULL; ++ ++ /* combined event obj struct */ ++ event = kmalloc(sizeof(*event), GFP_KERNEL); ++ if (!event) ++ return NULL; ++ ++ memset(event, 0, sizeof(*event)); ++ event->eventid = eventid; ++ pr_debug("%s : %u\n", __func__, eventid); ++ ++ return (void *)event; ++} ++ ++int hievent_put_integer(struct hiview_hievent *event, ++ const char *key, long value) ++{ ++ int ret; ++ struct hievent_payload *payload = NULL; ++ ++ if ((!event) || (!key)) { ++ pr_err("Bad input event or key for %s", __func__); ++ return -EINVAL; ++ } ++ ++ payload = hievent_get_payload(event->head, key); ++ if (!payload) { ++ payload = hievent_payload_create(); ++ if (!payload) ++ return -ENOMEM; ++ payload->key = kstrdup(key, GFP_KERNEL); ++ hievent_add_payload(event, payload); ++ } ++ ++ kfree(payload->value); ++ ++ payload->value = kmalloc(INT_TYPE_MAX_LEN, GFP_KERNEL); ++ if (!payload->value) ++ return -ENOMEM; ++ ++ (void)memset(payload->value, 0, INT_TYPE_MAX_LEN); ++ ret = snprintf(payload->value, INT_TYPE_MAX_LEN, "%d", (int)value); ++ if (ret < 0) ++ return -ENOMEM; ++ ++ return 0; ++} ++ ++int hievent_put_string(struct hiview_hievent *event, ++ const char *key, const char *value) ++{ ++ struct hievent_payload *payload = NULL; ++ int len; ++ ++ if ((!event) || (!key) || (!value)) { ++ pr_err("Bad key for %s", __func__); ++ return -EINVAL; ++ } ++ ++ payload = hievent_get_payload(event->head, key); ++ if (!payload) { ++ payload = hievent_payload_create(); ++ if (!payload) ++ return -ENOMEM; ++ ++ payload->key = kstrdup(key, GFP_KERNEL); ++ hievent_add_payload(event, payload); ++ } ++ ++ kfree(payload->value); ++ ++ len = strlen(value); ++ /* prevent length larger than MAX_STR_LEN */ ++ if (len > MAX_STR_LEN) ++ len = MAX_STR_LEN; ++ ++ payload->value = kmalloc(len + 1, GFP_KERNEL); ++ if (!payload->value) ++ return -ENOMEM; ++ ++ (void)memset(payload->value, 0, len + 1); ++ if (strncpy(payload->value, value, len) > 0) ++ payload->value[len] = '\0'; ++ ++ return 0; ++} ++ ++int hievent_set_time(struct hiview_hievent *event, long long seconds) ++{ ++ if ((!event) || (seconds == 0)) { ++ pr_err("Bad input for %s", __func__); ++ return -EINVAL; ++ } ++ event->time = seconds; ++ return 0; ++} ++ ++static int append_array_item(char **pool, int pool_len, const char *path) ++{ ++ int i; ++ ++ if ((!path) || (path[0] == 0)) { ++ pr_err("Bad path %s", __func__); ++ return -EINVAL; ++ } ++ ++ if (strlen(path) > MAX_PATH_LEN) { ++ pr_err("file path over max: %d", MAX_PATH_LEN); ++ return -EINVAL; ++ } ++ ++ for (i = 0; i < pool_len; i++) { ++ if (pool[i] != 0) ++ continue; ++ ++ pool[i] = kstrdup(path, GFP_KERNEL); ++ if (!pool[i]) ++ return -ENOMEM; ++ ++ break; ++ } ++ ++ if (i == MAX_PATH_NUMBER) { ++ pr_err("Too many paths"); ++ return -EINVAL; ++ } ++ ++ return 0; ++} ++ ++int hievent_add_filepath(struct hiview_hievent *event, const char *path) ++{ ++ if (!event) { ++ pr_err("Bad path %s", __func__); ++ return -EINVAL; ++ } ++ return append_array_item(event->file_path, MAX_PATH_NUMBER, path); ++} ++ ++/* make string ":" to "::", ";" to ";;", and remove newline character ++ * for example: "abc:def;ghi" transfer to "abc::def;;ghi" ++ */ ++static char *hievent_make_regular(char *value) ++{ ++ int count = 0; ++ int len = 0; ++ char *temp = value; ++ char *regular = NULL; ++ char *regular_tmp = NULL; ++ size_t regular_len; ++ ++ while (*temp != '\0') { ++ if (*temp == ':') ++ count++; ++ else if (*temp == ';') ++ count++; ++ else if ((*temp == '\n') || (*temp == '\r')) ++ *temp = ' '; ++ ++ temp++; ++ len++; ++ } ++ ++ /* no need to transfer, just return old value */ ++ if (count == 0) ++ return value; ++ ++ regular_len = len + count * 2 + 1; // 2 char in a byte ++ regular = kmalloc(regular_len, GFP_KERNEL); ++ if (!regular) ++ return NULL; ++ ++ (void)memset(regular, 0, regular_len); ++ regular_tmp = regular; ++ temp = value; ++ while (*temp != 0) { ++ if ((*temp == ':') || (*temp == ';')) ++ *regular_tmp++ = *temp; ++ ++ *regular_tmp++ = *temp; ++ temp++; ++ } ++ *regular_tmp = '\0'; ++ ++ return regular; ++} ++ ++int logbuff_to_exception(char category, int level, char log_type, ++ char sn, const char *msg, int msglen) ++{ ++ struct idap_header *hdr = NULL; ++ size_t buf_len = sizeof(int) + sizeof(struct idap_header) + msglen; ++ int ret; ++ int *check_code = NULL; ++ char *buffer = kmalloc(buf_len, GFP_KERNEL); ++ ++ if (!buffer) ++ return -ENOMEM; ++ ++ check_code = (int *)buffer; ++ *check_code = CHECK_CODE; ++ ++ hdr = (struct idap_header *)(buffer + sizeof(int)); ++ hdr->level = level; ++ hdr->category = category; ++ hdr->log_type = log_type; ++ hdr->sn = sn; ++ ++ memcpy(buffer + sizeof(int) + sizeof(struct idap_header), msg, msglen); ++ ++ ret = hievent_write_internal(buffer, buf_len); ++ ++ kfree(buffer); ++ ++ return ret; ++} ++ ++static int hievent_fill_payload(struct hiview_hievent *event, char **pbuf, ++ char *tmp, int length) ++{ ++ struct hievent_payload *p = event->head; ++ int len = length; ++ int tmplen; ++ unsigned int keycount = 0; ++ ++ while (p) { ++ char *value = NULL; ++ char *regular_value = NULL; ++ int need_free = 1; ++ ++ if (!p->value) { ++ p = p->next; ++ continue; ++ } ++ if (keycount == 0) { ++ tmplen = snprintf(tmp, len - 1, " --extra "); ++ BUF_POINTER_FORWARD; ++ } ++ keycount++; ++ ++ /* fill key */ ++ if (p->key) ++ tmplen = snprintf(tmp, len - 1, "%s:", p->key); ++ ++ BUF_POINTER_FORWARD; ++ /* fill value */ ++ tmplen = 0; ++ ++ value = p->value; ++ regular_value = hievent_make_regular(value); ++ if (!regular_value) { ++ regular_value = "NULL"; ++ need_free = 0; ++ } ++ tmplen = snprintf(tmp, len - 1, "%s;", regular_value); ++ if ((value != regular_value) && need_free) ++ kfree(regular_value); ++ ++ BUF_POINTER_FORWARD; ++ p = p->next; ++ } ++ return len; ++} ++ ++static int hievent_convert_string(struct hiview_hievent *event, char **pbuf) ++{ ++ int len; ++ char *tmp = NULL; ++ int tmplen; ++ unsigned int i; ++ ++ char *buf = kmalloc(EVENT_INFO_BUF_LEN, GFP_KERNEL); ++ ++ if (!buf) { ++ *pbuf = NULL; ++ return 0; ++ } ++ ++ (void)memset(buf, 0, EVENT_INFO_BUF_LEN); ++ len = EVENT_INFO_BUF_LEN; ++ tmp = buf; ++ ++ /* fill eventid */ ++ tmplen = snprintf(tmp, len - 1, "eventid %d", event->eventid); ++ BUF_POINTER_FORWARD; ++ ++ /* fill the path */ ++ for (i = 0; i < MAX_PATH_NUMBER; i++) { ++ if (!event->file_path[i]) ++ break; ++ ++ tmplen = snprintf(tmp, len - 1, " -i %s", event->file_path[i]); ++ BUF_POINTER_FORWARD; ++ } ++ ++ /* fill time */ ++ if (event->time) { ++ tmplen = snprintf(tmp, len - 1, " -t %lld", event->time); ++ BUF_POINTER_FORWARD; ++ } ++ ++ /* fill the payload info */ ++ len = hievent_fill_payload(event, pbuf, tmp, len); ++ *pbuf = buf; ++ return (EVENT_INFO_BUF_LEN - len); ++} ++ ++#define IDAP_LOGTYPE_CMD 1 ++static int hievent_write_logexception(char *str, const int strlen) ++{ ++ char tempchr; ++ char *strptr = str; ++ int left_buf_len = strlen + 1; ++ int sent_cnt = 0; ++ ++ while (left_buf_len > 0) { ++ if (left_buf_len > EVENT_INFO_PACK_BUF_LEN) { ++ tempchr = strptr[EVENT_INFO_PACK_BUF_LEN - 1]; ++ strptr[EVENT_INFO_PACK_BUF_LEN - 1] = '\0'; ++ logbuff_to_exception(0, 0, IDAP_LOGTYPE_CMD, 1, strptr, ++ EVENT_INFO_PACK_BUF_LEN); ++ left_buf_len -= (EVENT_INFO_PACK_BUF_LEN - 1); ++ strptr += (EVENT_INFO_PACK_BUF_LEN - 1); ++ strptr[0] = tempchr; ++ sent_cnt++; ++ } else { ++ logbuff_to_exception(0, 0, IDAP_LOGTYPE_CMD, 0, strptr, ++ left_buf_len); ++ sent_cnt++; ++ break; ++ } ++ } ++ ++ return sent_cnt; ++} ++ ++int hievent_report(struct hiview_hievent *obj) ++{ ++ char *str = NULL; ++ int buf_len; ++ int sent_packet; ++ ++ if (!obj) { ++ pr_err("Bad event %s", __func__); ++ return -EINVAL; ++ } ++ ++ buf_len = hievent_convert_string(obj, &str); ++ if (!str) ++ return -EINVAL; ++ ++ sent_packet = hievent_write_logexception(str, buf_len); ++ pr_err("report: %s", str); ++ kfree(str); ++ ++ return sent_packet; ++} ++ ++void hievent_destroy(struct hiview_hievent *event) ++{ ++ int i; ++ struct hievent_payload *p = NULL; ++ ++ if (!event) ++ return; ++ ++ p = event->head; ++ while (p) { ++ struct hievent_payload *del = p; ++ ++ p = p->next; ++ hievent_payload_destroy(del); ++ } ++ ++ event->head = NULL; ++ for (i = 0; i < MAX_PATH_NUMBER; i++) { ++ kfree(event->file_path[i]); ++ event->file_path[i] = NULL; ++ } ++ ++ kfree(event); ++} +diff --git a/drivers/staging/hievent/hiview_hievent.h b/drivers/staging/hievent/hiview_hievent.h +new file mode 100644 +index 000000000000..48d4ae32b536 +--- /dev/null ++++ b/drivers/staging/hievent/hiview_hievent.h +@@ -0,0 +1,44 @@ ++/* SPDX-License-Identifier: GPL-2.0 */ ++/* ++ * Copyright (C) 2021 Huawei Technologies Co., Ltd. All rights reserved. ++ * ++ * This software is licensed under the terms of the GNU General Public ++ * License version 2, as published by the Free Software Foundation, and ++ * may be copied, distributed, and modified under those terms. ++ * ++ * This program is distributed in the hope that it will be useful, ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++ * GNU General Public License for more details. ++ * ++ */ ++ ++#ifndef HIVIEW_HIEVENT_H ++#define HIVIEW_HIEVENT_H ++ ++#define MAX_PATH_NUMBER 10 ++ ++/* hievent struct */ ++struct hiview_hievent { ++ unsigned int eventid; ++ ++ long long time; ++ ++ /* payload linked list */ ++ struct hievent_payload *head; ++ ++ /* file path needs uploaded */ ++ char *file_path[MAX_PATH_NUMBER]; ++}; ++ ++struct hiview_hievent *hievent_create(unsigned int eventid); ++int hievent_put_integer(struct hiview_hievent *event, ++ const char *key, long value); ++int hievent_put_string(struct hiview_hievent *event, ++ const char *key, const char *value); ++int hievent_set_time(struct hiview_hievent *event, long long seconds); ++int hievent_add_filepath(struct hiview_hievent *event, const char *path); ++int hievent_report(struct hiview_hievent *obj); ++void hievent_destroy(struct hiview_hievent *event); ++ ++#endif /* HIVIEW_HIEVENT_H */ +diff --git a/drivers/staging/hilog/Kconfig b/drivers/staging/hilog/Kconfig +new file mode 100644 +index 000000000000..23fe9000b585 +--- /dev/null ++++ b/drivers/staging/hilog/Kconfig +@@ -0,0 +1,21 @@ ++# ++# Sensor device configuration ++# ++ ++config HILOG ++ tristate "Hilog support" ++ default n ++ help ++ hilog buffer manager. ++ ++ Hilog is a simple log manager for OpenHarmonyOS. ++ log string write to /dev/hilog, and the hilog driver copy it ++ to the ring buffer. Ring buffer can be read from userspace. ++ ++ If unsure, say N. ++ ++if HILOG ++config HI_LOG_BUFFER_SIZE ++ int "hi log buffer size" ++ default "2048" ++endif +diff --git a/drivers/staging/hilog/Makefile b/drivers/staging/hilog/Makefile +new file mode 100644 +index 000000000000..e53c86a5dfcd +--- /dev/null ++++ b/drivers/staging/hilog/Makefile +@@ -0,0 +1,5 @@ ++# ++# Makefile for the hi hilog drivers. ++# ++ ++obj-$(CONFIG_HILOG) += hilog.o +diff --git a/drivers/staging/hilog/hilog.c b/drivers/staging/hilog/hilog.c +new file mode 100644 +index 000000000000..dae5ab074cc8 +--- /dev/null ++++ b/drivers/staging/hilog/hilog.c +@@ -0,0 +1,381 @@ ++// SPDX-License-Identifier: GPL-2.0 ++/* ++ * Copyright (C) 2021 Huawei Technologies Co., Ltd. All rights reserved. ++ * ++ * This software is licensed under the terms of the GNU General Public ++ * License version 2, as published by the Free Software Foundation, and ++ * may be copied, distributed, and modified under those terms. ++ * ++ * This program is distributed in the hope that it will be useful, ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++ * GNU General Public License for more details. ++ * ++ */ ++ ++#include <linux/init.h> ++#include <linux/module.h> ++#include <linux/mutex.h> ++#include <linux/uaccess.h> ++#include <linux/fs.h> ++#include <linux/slab.h> ++#include <linux/cdev.h> ++#include <linux/sched.h> ++#include <linux/time.h> ++#include <linux/timex.h> ++#include <linux/rtc.h> ++#include <linux/version.h> ++ ++#ifndef HILOGDEV_MAJOR ++#define HILOGDEV_MAJOR 245 ++#endif ++ ++#ifndef HILOG_NR_DEVS ++#define HILOG_NR_DEVS 2 ++#endif ++ ++#ifndef MEMDEV_SIZE ++#define MEMDEV_SIZE 4096 ++#endif ++ ++static int hilog_major = HILOGDEV_MAJOR; ++ ++module_param(hilog_major, int, 0444); ++ ++struct cdev g_hilog_cdev; ++ ++#define HILOG_BUFFER ((size_t)1024) ++#define HILOG_DRIVER "/dev/hilog" ++ ++struct hilog_entry { ++ unsigned int len; ++ unsigned int header_size; ++ unsigned int pid : 16; ++ unsigned int task_id : 16; ++ unsigned int sec; ++ unsigned int nsec; ++ unsigned int reserved; ++ char msg[0]; ++}; ++ ++static ssize_t hilog_write(struct file *file, ++ const char __user *user_buf, ++ size_t count, loff_t *ppos); ++static ssize_t hilog_read(struct file *file, ++ char __user *user_buf, size_t count, loff_t *ppos); ++ ++static const struct file_operations hilog_fops = { ++ .read = hilog_read, ++ .write = hilog_write, ++}; ++ ++struct hilog_char_device { ++ int flag; ++ struct mutex mtx; /* lock to protect read/write buffer */ ++ unsigned char *buffer; ++ wait_queue_head_t wq; ++ size_t wr_off; ++ size_t hdr_off; ++ size_t size; ++ size_t count; ++} hilog_dev; ++ ++static inline unsigned char *hilog_buffer_head(void) ++{ ++ return hilog_dev.buffer + hilog_dev.hdr_off; ++} ++ ++static void hilog_buffer_inc(size_t sz) ++{ ++ if (hilog_dev.size + sz <= HILOG_BUFFER) { ++ hilog_dev.size += sz; ++ hilog_dev.wr_off += sz; ++ hilog_dev.wr_off %= HILOG_BUFFER; ++ hilog_dev.count++; ++ } ++} ++ ++static void hilog_buffer_dec(size_t sz) ++{ ++ if (hilog_dev.size >= sz) { ++ hilog_dev.size -= sz; ++ hilog_dev.hdr_off += sz; ++ hilog_dev.hdr_off %= HILOG_BUFFER; ++ hilog_dev.count--; ++ } ++} ++ ++static int hilog_read_ring_buff(unsigned char __user *buffer, size_t buf_len) ++{ ++ size_t retval; ++ size_t buf_left = HILOG_BUFFER - hilog_dev.hdr_off; ++ ++ if (buf_left > buf_len) { ++ retval = copy_to_user(buffer, hilog_buffer_head(), buf_len); ++ } else { ++ size_t mem_len = (buf_len > buf_left) ? buf_left : buf_len; ++ ++ retval = copy_to_user(buffer, hilog_buffer_head(), mem_len); ++ if (retval < 0) ++ return retval; ++ ++ retval = copy_to_user(buffer + buf_left, hilog_dev.buffer, ++ buf_len - buf_left); ++ } ++ return retval; ++} ++ ++static int hilog_read_ring_head_buffer(unsigned char *buffer, size_t buf_len) ++{ ++ size_t buf_left = HILOG_BUFFER - hilog_dev.hdr_off; ++ ++ if (buf_left > buf_len) { ++ memcpy(buffer, hilog_buffer_head(), buf_len); ++ } else { ++ size_t mem_len = (buf_len > buf_left) ? buf_left : buf_len; ++ ++ memcpy(buffer, hilog_buffer_head(), mem_len); ++ memcpy(buffer + buf_left, hilog_dev.buffer, buf_len - buf_left); ++ } ++ ++ return 0; ++} ++ ++static ssize_t hilog_read(struct file *file, ++ char __user *user_buf, size_t count, loff_t *ppos) ++{ ++ size_t retval; ++ struct hilog_entry header; ++ ++ (void)file; ++ wait_event_interruptible(hilog_dev.wq, (hilog_dev.size > 0)); ++ ++ (void)mutex_lock(&hilog_dev.mtx); ++ ++ retval = hilog_read_ring_head_buffer((unsigned char *)&header, ++ sizeof(header)); ++ if (retval < 0) { ++ retval = -EINVAL; ++ goto out; ++ } ++ ++ if (count < header.len + sizeof(header)) { ++ pr_err("buffer too small,buf_len=%d, header.len=%d,%d\n", ++ count, header.len, header.header_size); ++ retval = -ENOMEM; ++ goto out; ++ } ++ ++ hilog_buffer_dec(sizeof(header)); ++ retval = copy_to_user((unsigned char *)user_buf, ++ (unsigned char *)&header, ++ min(count, sizeof(header))); ++ ++ if (retval < 0) { ++ retval = -EINVAL; ++ goto out; ++ } ++ ++ retval = hilog_read_ring_buff((unsigned char *) ++ (user_buf + sizeof(header)), ++ header.len); ++ if (retval < 0) { ++ retval = -EINVAL; ++ goto out; ++ } ++ ++ hilog_buffer_dec(header.len); ++ retval = header.len + sizeof(header); ++out: ++ (void)mutex_unlock(&hilog_dev.mtx); ++ ++ return retval; ++} ++ ++static int hilog_write_ring_buffer(unsigned char __user *buffer, size_t buf_len) ++{ ++ int retval; ++ size_t buf_left = HILOG_BUFFER - hilog_dev.wr_off; ++ ++ if (buf_len > buf_left) { ++ retval = copy_from_user(hilog_dev.buffer + hilog_dev.wr_off, ++ buffer, buf_left); ++ if (retval) ++ return -1; ++ retval = copy_from_user(hilog_dev.buffer, buffer + buf_left, ++ min(HILOG_BUFFER, buf_len - buf_left)); ++ } else { ++ retval = copy_from_user(hilog_dev.buffer + hilog_dev.wr_off, ++ buffer, min(buf_left, buf_len)); ++ } ++ ++ if (retval < 0) ++ return -1; ++ ++ return 0; ++} ++ ++static int hilog_write_ring_head_buffer(unsigned char *buffer, size_t buf_len) ++{ ++ size_t buf_left = HILOG_BUFFER - hilog_dev.wr_off; ++ ++ if (buf_len > buf_left) { ++ memcpy(hilog_dev.buffer + hilog_dev.wr_off, ++ buffer, buf_left); ++ memcpy(hilog_dev.buffer, buffer + buf_left, ++ min(HILOG_BUFFER, buf_len - buf_left)); ++ } else { ++ memcpy(hilog_dev.buffer + hilog_dev.wr_off, ++ buffer, min(buf_left, buf_len)); ++ } ++ ++ return 0; ++} ++ ++static void hilog_head_init(struct hilog_entry *header, size_t len) ++{ ++#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0) ++#define NANOSEC_PER_MIRCOSEC 1000 ++ struct timeval now = { 0 }; ++ ++ do_gettimeofday(&now); ++ ++ header->sec = now.tv_sec; ++ header->nsec = now.tv_usec * NANOSEC_PER_MIRCOSEC; ++#else ++ struct timespec64 now = { 0 }; ++ ++ ktime_get_real_ts64(&now); ++ ++ header->sec = now.tv_sec; ++ header->nsec = now.tv_nsec; ++#endif ++ ++ header->len = len; ++ header->pid = current->pid; ++ header->task_id = current->pid; ++ header->header_size = sizeof(struct hilog_entry); ++} ++ ++static void hilog_cover_old_log(size_t buf_len) ++{ ++ int retval; ++ struct hilog_entry header; ++ size_t total_size = buf_len + sizeof(struct hilog_entry); ++ ++ while (total_size + hilog_dev.size >= HILOG_BUFFER) { ++ retval = hilog_read_ring_head_buffer((unsigned char *)&header, ++ sizeof(header)); ++ if (retval < 0) ++ break; ++ ++ hilog_buffer_dec(sizeof(header) + header.len); ++ } ++} ++ ++int hilog_write_internal(const char __user *buffer, size_t buf_len) ++{ ++ struct hilog_entry header; ++ int retval; ++ ++ (void)mutex_lock(&hilog_dev.mtx); ++ hilog_cover_old_log(buf_len); ++ hilog_head_init(&header, buf_len); ++ ++ retval = hilog_write_ring_head_buffer((unsigned char *)&header, ++ sizeof(header)); ++ if (retval) { ++ retval = -ENODATA; ++ goto out; ++ } ++ hilog_buffer_inc(sizeof(header)); ++ ++ retval = hilog_write_ring_buffer((unsigned char *)(buffer), header.len); ++ if (retval) { ++ retval = -ENODATA; ++ goto out; ++ } ++ ++ hilog_buffer_inc(header.len); ++ ++ retval = header.len; ++ ++out: ++ (void)mutex_unlock(&hilog_dev.mtx); ++ if (retval > 0) ++ wake_up_interruptible(&hilog_dev.wq); ++ else if (retval < 0) ++ pr_err("write fail retval=%d\n", retval); ++ ++ return retval; ++} ++ ++static ssize_t hilog_write(struct file *file, ++ const char __user *user_buf, ++ size_t count, loff_t *ppos) ++{ ++ (void)file; ++ if (count + sizeof(struct hilog_entry) > HILOG_BUFFER) { ++ pr_err("input too large\n"); ++ return -ENOMEM; ++ } ++ ++ return hilog_write_internal(user_buf, count); ++} ++ ++static void hilog_device_init(void) ++{ ++ hilog_dev.buffer = kmalloc(HILOG_BUFFER, GFP_KERNEL); ++ if (!hilog_dev.buffer) ++ return; ++ ++ init_waitqueue_head(&hilog_dev.wq); ++ mutex_init(&hilog_dev.mtx); ++ hilog_dev.wr_off = 0; ++ hilog_dev.hdr_off = 0; ++ hilog_dev.size = 0; ++ hilog_dev.count = 0; ++} ++ ++static int __init hilogdev_init(void) ++{ ++ int result; ++ dev_t devno = MKDEV(hilog_major, 0); ++ ++ result = register_chrdev_region(devno, 2, "hilog"); ++ if (result < 0) { ++ pr_emerg("\t register hilog error %d\n", result); ++ return result; ++ } ++ ++ cdev_init(&g_hilog_cdev, &hilog_fops); ++ g_hilog_cdev.owner = THIS_MODULE; ++ g_hilog_cdev.ops = &hilog_fops; ++ ++ cdev_add(&g_hilog_cdev, MKDEV(hilog_major, 0), HILOG_NR_DEVS); ++ ++ hilog_device_init(); ++ return 0; ++} ++ ++static void __exit hilog_exit_module(void) ++{ ++ cdev_del(&g_hilog_cdev); ++ unregister_chrdev_region(MKDEV(hilog_major, 0), HILOG_NR_DEVS); ++} ++ ++static int __init hilog_init_module(void) ++{ ++ int state = hilogdev_init(); ++ ++ pr_info("\t hilog_init Start%d\n", state); ++ return 0; ++} ++ ++module_init(hilog_init_module); ++module_exit(hilog_exit_module); ++ ++MODULE_AUTHOR("OHOS"); ++MODULE_DESCRIPTION("User mode hilog device interface"); ++MODULE_LICENSE("GPL"); ++MODULE_ALIAS("hilog"); +-- +2.32.0 + diff --git a/recipes-kernel/linux-yocto/linux-yocto/driver-add-hilog-and-hievent-buffer-management-drive.patch.license b/recipes-kernel/linux-yocto/linux-yocto/driver-add-hilog-and-hievent-buffer-management-drive.patch.license new file mode 100644 index 0000000000000000000000000000000000000000..51f024686f1fcc065b20ded36e5ad692d0ef3893 --- /dev/null +++ b/recipes-kernel/linux-yocto/linux-yocto/driver-add-hilog-and-hievent-buffer-management-drive.patch.license @@ -0,0 +1,3 @@ +# SPDX-FileCopyrightText: Huawei Inc. +# +# SPDX-License-Identifier: GPL-2.0-only diff --git a/recipes-kernel/linux-yocto/linux-yocto_%.bbappend b/recipes-kernel/linux-yocto/linux-yocto_%.bbappend index 248930c05ca4e58fb7a1624d591ad04b349263f4..284d90b2a9b8d0f252e5a9fc73a21d561c3639a2 100644 --- a/recipes-kernel/linux-yocto/linux-yocto_%.bbappend +++ b/recipes-kernel/linux-yocto/linux-yocto_%.bbappend @@ -5,3 +5,5 @@ FILESEXTRAPATHS:prepend := "${THISDIR}/linux-yocto:" SRC_URI:append:oniro-openharmony-linux = " file://ashmem.cfg" +SRC_URI:append:oniro-openharmony-linux = " file://driver-add-hilog-and-hievent-buffer-management-drive.patch" +SRC_URI:append:oniro-openharmony-linux = " file://driver-add-hilog-and-hievent-buffer-management-drive.cfg"