From 5418201ea44add21ec30858e9b2229f448c100b9 Mon Sep 17 00:00:00 2001 From: Yevgen Kovalyov Date: Tue, 28 Jan 2020 17:15:57 +0200 Subject: [PATCH 01/11] homework: task for hardware detector Signed-off-by: Yevgen Kovalyov --- README.md | 4 ++++ hwdetect/README.md | 10 ++++++++++ 2 files changed, 14 insertions(+) create mode 100644 hwdetect/README.md diff --git a/README.md b/README.md index 2313e55..ae55282 100644 --- a/README.md +++ b/README.md @@ -4,4 +4,8 @@ GlobalLogic Kharkiv, 2020 This repository is aimed for sharing our exercises and your solutions in scope of the course. +Homeworks: +- hwdetect - detect attached hardware using user level script file + + Please refer to wiki for details. diff --git a/hwdetect/README.md b/hwdetect/README.md new file mode 100644 index 0000000..7f93fd6 --- /dev/null +++ b/hwdetect/README.md @@ -0,0 +1,10 @@ +# Home work hot plugged hardware detector + +Create the script 'hwdetect.sh' which detects connected hardware. +For monitor hardware from user level please use /dev folder, +output from lsusb and i2detect (optional). +Most interesting devices are: + - USB to TTL convertors, + - flash drives, + - SD cards, + - i2c devices. From 4e96f17b0201f8586d3de2316cbb6ed17fc08c48 Mon Sep 17 00:00:00 2001 From: Kyryl Krynychnyi Date: Sun, 2 Feb 2020 22:14:02 +0200 Subject: [PATCH 02/11] hwdetect: Add an empty bash script Signed-off-by: Kyryl Krynychnyi --- hwdetect/hwdetect.sh | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100755 hwdetect/hwdetect.sh diff --git a/hwdetect/hwdetect.sh b/hwdetect/hwdetect.sh new file mode 100755 index 0000000..04f7a15 --- /dev/null +++ b/hwdetect/hwdetect.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +# USB to TTL convertors + +# flash drives + +# SD cards + +# i2c devices From ad2786fb4cda493ca2b77d24c6d29ece01f444a0 Mon Sep 17 00:00:00 2001 From: Kyryl Krynychnyi Date: Sun, 2 Feb 2020 23:52:04 +0200 Subject: [PATCH 03/11] hwdetect: Add USB to TTL detector Create two tmp files. Redirect an output of ls /dev to these files and compare them. If the script is interrupted or exited, delete tmp files Signed-off-by: Kyryl Krynychnyi --- hwdetect/hwdetect.sh | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/hwdetect/hwdetect.sh b/hwdetect/hwdetect.sh index 04f7a15..3540454 100755 --- a/hwdetect/hwdetect.sh +++ b/hwdetect/hwdetect.sh @@ -1,9 +1,29 @@ #!/bin/bash -# USB to TTL convertors +dev0=$(mktemp) +ls /dev > $dev0 +dev1=$(mktemp) + +while true; do + ls /dev > $dev1 + usb_to_ttl=$(diff $dev0 $dev1 | grep ttyUSB* ) + if [[ -n "$usb_to_ttl" ]]; then + if [[ "$(diff $dev0 $dev1 | grep ttyUSB* | head -c1)" = ">" ]]; then + echo "connected" + else + echo "disconnected" + fi + echo "$usb_to_ttl" + echo + ls /dev > $dev0 + fi +done + +trap "rm -f $dev0" 0 2 3 15 +trap "rm -f $dev1" 0 2 3 15 # flash drives # SD cards -# i2c devices +# i2c devices \ No newline at end of file From fe4c487ac5348406496919f0203a1156f3f4ffc2 Mon Sep 17 00:00:00 2001 From: Kyryl Krynychnyi Date: Tue, 4 Feb 2020 22:49:13 +0200 Subject: [PATCH 04/11] hwdetect: Add USB flash drives detector USB flash detector is a result of comparison of outputs of ls /dev/sd[a-z]. Change output format and add delay. Signed-off-by: Kyryl Krynychnyi --- hwdetect/hwdetect.sh | 105 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 95 insertions(+), 10 deletions(-) diff --git a/hwdetect/hwdetect.sh b/hwdetect/hwdetect.sh index 3540454..25806c0 100755 --- a/hwdetect/hwdetect.sh +++ b/hwdetect/hwdetect.sh @@ -1,28 +1,113 @@ #!/bin/bash -dev0=$(mktemp) -ls /dev > $dev0 -dev1=$(mktemp) +usb0=$(mktemp) +flash0=$(mktemp) +sd0=$(mktemp) +usb1=$(mktemp) +flash1=$(mktemp) +sd1=$(mktemp) + +echo "Hardware that is detected from start" +echo "USB to ttl:" +ls /dev | grep ttyUSB* > $usb0 +if [[ -n $(cat $usb0) ]]; then + cat $usb0 +else + echo "none" +fi + +echo "USB flash drives that are connected" +ls /dev/sd[a-z] > $flash0 +if [[ -n $(cat $flash0) ]]; then + cat $flash0 +else + echo "none" +fi + +echo +echo "Active detector" while true; do - ls /dev > $dev1 - usb_to_ttl=$(diff $dev0 $dev1 | grep ttyUSB* ) + ls /dev | grep ttyUSB* > $usb1 + usb_to_ttl=$(diff --new-line-format=$'>connected> %l\n' --old-line-format=$' $flash1 + flash_drives=$(diff --new-line-format=$'>connected> %l\n' --old-line-format=$' $usb0 + fi + + if [[ -n "$flash_drives" ]]; then + for j in $(echo "$flash_drives"); do + echo "$j" + done + echo + ls /dev/sd[a-z] > $flash0 | 2>/dev/null + fi + + sleep 1 +done + +trap "rm -f $usb0" 0 2 3 15 +trap "rm -f $usb1" 0 2 3 15 +trap "rm -f $flash0" 0 2 3 15 +trap "rm -f $flash1" 0 2 3 15 + + + +# SD cards + +# i2c devicesecho "USB flash drives that are connected:" +ls /dev/sd[a-z] > $flash0 +if [[ -n $(cat $flash0) ]]; then + cat $flash0 +else + echo "none" +fi + +echo +echo "Active detector" +while true; do + ls /dev | grep ttyUSB* > $usb1 + usb_to_ttl=$(diff $usb0 $usb1 ) + + ls /dev/sd[a-z] > $flash1 + flash_drives=$(diff $flash0 $flash1) + + if [[ -n "$usb_to_ttl" ]]; then + if [[ "$(diff $usb0 $usb1 | head -c1)" = ">" ]]; then echo "connected" else echo "disconnected" fi echo "$usb_to_ttl" echo - ls /dev > $dev0 + ls /dev > $usb0 + fi + + if [[ -n "$flash_drives" ]]; then + if [[ "$( diff $flash0 $flash1 | head -c1)" = ">" ]]; then + echo "connected" + echo "$flash_drives" + elif [[ "$(echo "$flash_drives" | head -c1)" = "<" ]]; then + echo "disconnected" + echo "$flash_drives" + fi + cat $flash1 > $flash0 fi done -trap "rm -f $dev0" 0 2 3 15 -trap "rm -f $dev1" 0 2 3 15 +trap "rm -f $usb0" 0 2 3 15 +trap "rm -f $usb1" 0 2 3 15 +trap "rm -f $flash0" 0 2 3 15 +trap "rm -f $flash1" 0 2 3 15 + -# flash drives # SD cards From 509969feb786f8eb379a2e2879ba701634a8bddd Mon Sep 17 00:00:00 2001 From: Kyryl Krynychnyi Date: Tue, 4 Feb 2020 23:37:39 +0200 Subject: [PATCH 05/11] hwdetect: Add i2c and SD card detector i2c detector checks the output of i2cdetect -l SD card detector checks the output of ls /dev Signed-off-by: Kyryl Krynychnyi --- hwdetect/hwdetect.sh | 106 ++++++++++++++++++++----------------------- 1 file changed, 49 insertions(+), 57 deletions(-) diff --git a/hwdetect/hwdetect.sh b/hwdetect/hwdetect.sh index 25806c0..9acfa10 100755 --- a/hwdetect/hwdetect.sh +++ b/hwdetect/hwdetect.sh @@ -3,12 +3,14 @@ usb0=$(mktemp) flash0=$(mktemp) sd0=$(mktemp) +i2c0=$(mktemp) usb1=$(mktemp) flash1=$(mktemp) sd1=$(mktemp) +i2c1=$(mktemp) -echo "Hardware that is detected from start" +echo -e "Hardware that is detected from start\n" echo "USB to ttl:" ls /dev | grep ttyUSB* > $usb0 if [[ -n $(cat $usb0) ]]; then @@ -16,16 +18,35 @@ if [[ -n $(cat $usb0) ]]; then else echo "none" fi +echo -echo "USB flash drives that are connected" +echo "USB flash drives:" ls /dev/sd[a-z] > $flash0 if [[ -n $(cat $flash0) ]]; then cat $flash0 else echo "none" fi +echo + +echo "I2C devices: " +i2cdetect -l > $i2c0 +if [[ -n $(cat $i2c0) ]]; then + cat $i2c0 +else + echo "none" +fi +echo + +echo "SD cards: " +ls /dev | grep -E "mmcblk[0-9]+$" > $sd0 +if [[ -n $(cat $sd0) ]]; then + cat $sd0 +else + echo "none" +fi +echo -echo echo "Active detector" while true; do ls /dev | grep ttyUSB* > $usb1 @@ -34,12 +55,18 @@ while true; do ls /dev/sd[a-z] > $flash1 flash_drives=$(diff --new-line-format=$'>connected> %l\n' --old-line-format=$' $i2c1 + i2c_devices=$(diff --new-line-format=$'>connected> %l\n' --old-line-format=$' $sd1 + sd_cards=$(diff --new-line-format=$'>connected> %l\n' --old-line-format=$' $usb0 + cat $usb1 > $usb0 fi if [[ -n "$flash_drives" ]]; then @@ -47,68 +74,33 @@ while true; do echo "$j" done echo - ls /dev/sd[a-z] > $flash0 | 2>/dev/null + cat $flash1 > $flash0 | 2>/dev/null fi - sleep 1 -done - -trap "rm -f $usb0" 0 2 3 15 -trap "rm -f $usb1" 0 2 3 15 -trap "rm -f $flash0" 0 2 3 15 -trap "rm -f $flash1" 0 2 3 15 - - - -# SD cards - -# i2c devicesecho "USB flash drives that are connected:" -ls /dev/sd[a-z] > $flash0 -if [[ -n $(cat $flash0) ]]; then - cat $flash0 -else - echo "none" -fi - -echo -echo "Active detector" -while true; do - ls /dev | grep ttyUSB* > $usb1 - usb_to_ttl=$(diff $usb0 $usb1 ) - - ls /dev/sd[a-z] > $flash1 - flash_drives=$(diff $flash0 $flash1) - - if [[ -n "$usb_to_ttl" ]]; then - if [[ "$(diff $usb0 $usb1 | head -c1)" = ">" ]]; then - echo "connected" - else - echo "disconnected" - fi - echo "$usb_to_ttl" + if [[ -n "$i2c_devices" ]]; then + for k in $(echo "$i2c_devices"); do + echo "$k" + done echo - ls /dev > $usb0 + cat $i2c1 > $i2c0 fi - if [[ -n "$flash_drives" ]]; then - if [[ "$( diff $flash0 $flash1 | head -c1)" = ">" ]]; then - echo "connected" - echo "$flash_drives" - elif [[ "$(echo "$flash_drives" | head -c1)" = "<" ]]; then - echo "disconnected" - echo "$flash_drives" - fi - cat $flash1 > $flash0 + if [[ -n "$sd_cards" ]]; then + for l in $(echo "$sd_cards"); do + echo "$l" + done + echo + cat $sd1 > $sd0 fi + + sleep 1 done trap "rm -f $usb0" 0 2 3 15 trap "rm -f $usb1" 0 2 3 15 trap "rm -f $flash0" 0 2 3 15 trap "rm -f $flash1" 0 2 3 15 - - - -# SD cards - -# i2c devices \ No newline at end of file +trap "rm -f $i2c0" 0 2 3 15 +trap "rm -f $i2c1" 0 2 3 15 +trap "rm -f $sd0" 0 2 3 15 +trap "rm -f $sd1" 0 2 3 15 \ No newline at end of file From 46d1df2267e9f1d50fa60d27fb649d1e73108666 Mon Sep 17 00:00:00 2001 From: Kyryl-code <57329207+Kyryl-code@users.noreply.github.com> Date: Tue, 4 Feb 2020 23:58:27 +0200 Subject: [PATCH 06/11] hwdetect: Create example.md --- example.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 example.md diff --git a/example.md b/example.md new file mode 100644 index 0000000..c2e77bc --- /dev/null +++ b/example.md @@ -0,0 +1,37 @@ +Hardware that is detected from start + +USB to ttl: +ttyUSB0 + +USB flash drives: +/dev/sda + +I2C devices: +i2c-3 unknown i915 gmbus dpd N/A +i2c-1 unknown i915 gmbus dpc N/A +i2c-4 unknown DPDDC-A N/A +i2c-2 unknown i915 gmbus dpb N/A +i2c-0 unknown SMBus I801 adapter at f040 N/A +i2c-5 unknown DPDDC-C N/A + +SD cards: +mmcblk0 + +Active detector +connected> +ttyUSB0 + +connected> +mmcblk0 + +>connected> +/dev/sdb + + Date: Wed, 5 Feb 2020 00:08:46 +0200 Subject: [PATCH 07/11] hwdetect: Move example.md to hwdetect folder Signed-off-by: Kyryl Krynychnyi --- example.md => hwdetect/example.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename example.md => hwdetect/example.md (100%) diff --git a/example.md b/hwdetect/example.md similarity index 100% rename from example.md rename to hwdetect/example.md From b4d991ee8e499c7bc83eb7061f767f0864dafb11 Mon Sep 17 00:00:00 2001 From: Kyryl Krynychnyi Date: Wed, 5 Feb 2020 22:36:53 +0200 Subject: [PATCH 08/11] hwdetect: Fix deleting of tmp files after script termination Signed-off-by: Kyryl Krynychnyi --- hwdetect/hwdetect.sh | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/hwdetect/hwdetect.sh b/hwdetect/hwdetect.sh index 9acfa10..953d8e8 100755 --- a/hwdetect/hwdetect.sh +++ b/hwdetect/hwdetect.sh @@ -94,13 +94,5 @@ while true; do fi sleep 1 -done - -trap "rm -f $usb0" 0 2 3 15 -trap "rm -f $usb1" 0 2 3 15 -trap "rm -f $flash0" 0 2 3 15 -trap "rm -f $flash1" 0 2 3 15 -trap "rm -f $i2c0" 0 2 3 15 -trap "rm -f $i2c1" 0 2 3 15 -trap "rm -f $sd0" 0 2 3 15 -trap "rm -f $sd1" 0 2 3 15 \ No newline at end of file + trap 'rm -f $usb0 $usb1 $flash0 $flash1 $i2c0 $i2c1 $sd0 $sd1 && echo -e "\n***********Detection ended***********" && exit' 2 3 15 +done \ No newline at end of file From 16119fa4ff304bec30551664318a7652c49536bf Mon Sep 17 00:00:00 2001 From: Kyryl Krynychnyi Date: Thu, 6 Feb 2020 21:45:56 +0200 Subject: [PATCH 09/11] hwdetect: Fix declaration of trap Signed-off-by: Kyryl Krynychnyi --- hwdetect/hwdetect.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hwdetect/hwdetect.sh b/hwdetect/hwdetect.sh index 953d8e8..a7b8882 100755 --- a/hwdetect/hwdetect.sh +++ b/hwdetect/hwdetect.sh @@ -47,6 +47,8 @@ else fi echo +trap 'rm -f $usb0 $usb1 $flash0 $flash1 $i2c0 $i2c1 $sd0 $sd1 && echo -e "\n***********Detection ended***********" && exit' 2 3 15 + echo "Active detector" while true; do ls /dev | grep ttyUSB* > $usb1 @@ -94,5 +96,4 @@ while true; do fi sleep 1 - trap 'rm -f $usb0 $usb1 $flash0 $flash1 $i2c0 $i2c1 $sd0 $sd1 && echo -e "\n***********Detection ended***********" && exit' 2 3 15 done \ No newline at end of file From 3310c1e70da0ef915d5a196857cdbb583c24b896 Mon Sep 17 00:00:00 2001 From: Kyryl Krynychnyi Date: Sun, 9 Feb 2020 22:57:11 +0200 Subject: [PATCH 10/11] hwdetect: Format code according to Linux kernel coding style Signed-off-by: Kyryl Krynychnyi --- hwdetect/hwdetect.sh | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/hwdetect/hwdetect.sh b/hwdetect/hwdetect.sh index a7b8882..0a4b861 100755 --- a/hwdetect/hwdetect.sh +++ b/hwdetect/hwdetect.sh @@ -10,6 +10,14 @@ flash1=$(mktemp) sd1=$(mktemp) i2c1=$(mktemp) +ALL_TMP_FILES="$usb0 $usb1 $flash0 $flash1 $i2c0 $i2c1 $sd0 $sd1" + +CON_MSG=">connected> %l" + +DISCON_MSG=" $usb0 @@ -47,21 +55,21 @@ else fi echo -trap 'rm -f $usb0 $usb1 $flash0 $flash1 $i2c0 $i2c1 $sd0 $sd1 && echo -e "\n***********Detection ended***********" && exit' 2 3 15 +trap 'rm -f $ALL_TMP_FILES && echo -e "\n$END_MSG" && exit' 2 3 15 echo "Active detector" while true; do ls /dev | grep ttyUSB* > $usb1 - usb_to_ttl=$(diff --new-line-format=$'>connected> %l\n' --old-line-format=$' $flash1 - flash_drives=$(diff --new-line-format=$'>connected> %l\n' --old-line-format=$' $i2c1 - i2c_devices=$(diff --new-line-format=$'>connected> %l\n' --old-line-format=$' $sd1 - sd_cards=$(diff --new-line-format=$'>connected> %l\n' --old-line-format=$' Date: Mon, 10 Feb 2020 16:07:21 +0200 Subject: [PATCH 11/11] device_tree: Add basic overlay file that adds i2c0 device Signed-off-by: Kyryl Krynychnyi --- device_tree/i2c0.dts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 device_tree/i2c0.dts diff --git a/device_tree/i2c0.dts b/device_tree/i2c0.dts new file mode 100644 index 0000000..6173423 --- /dev/null +++ b/device_tree/i2c0.dts @@ -0,0 +1,20 @@ +/dts-v1/; +/plugin/; + +/ { + compatible = "allwinner,sun8i-h3"; + + fragment@0 { + target-path = "/aliases"; + __overlay__ { + i2c0 = "/soc/i2c@01c2ac00"; + }; + }; + + fragment@1 { + target = <&i2c0>; + __overlay__ { + status = "okay"; + }; + }; +}; \ No newline at end of file