// SPDX-License-Identifier: GPL-2.0-only /* * Generic ADC thermal driver * * Copyright (C) 2016 NVIDIA CORPORATION. All rights reserved. * * Author: Laxman Dewangan <ldewangan@nvidia.com> */ #include <linux/iio/consumer.h> #include <linux/kernel.h> #include <linux/module.h> #include <linux/platform_device.h> #include <linux/slab.h> #include <linux/thermal.h> struct gadc_thermal_info { struct device *dev; struct thermal_zone_device *tz_dev; struct iio_channel *channel; s32 *lookup_table; int nlookup_table; }; static int gadc_thermal_adc_to_temp(struct gadc_thermal_info *gti, int val) { int temp, temp_hi, temp_lo, adc_hi, adc_lo; int i; if (!gti->lookup_table) return val; for (i = 0; i < gti->nlookup_table; i++) { if (val >= gti->lookup_table[2 * i + 1]) break; } if (i == 0) { temp = gti->lookup_table[0]; } else if (i >= gti->nlookup_table) { temp = gti->lookup_table[2 * (gti->nlookup_table - 1)]; } else { adc_hi = gti->lookup_table[2 * i - 1]; adc_lo = gti->lookup_table[2 * i + 1]; temp_hi = gti->lookup_table[2 * i - 2]; temp_lo = gti->lookup_table[2 * i]; temp = temp_hi + mult_frac(temp_lo - temp_hi, val - adc_hi, adc_lo - adc_hi); } return temp; } static int gadc_thermal_get_temp(void *data, int *temp) { struct gadc_thermal_info *gti = data; int val; int ret; ret = iio_read_channel_processed(gti->channel, &val); if (ret < 0) { dev_err(gti->dev, "IIO channel read failed %d\n", ret); return ret; } *temp = gadc_thermal_adc_to_temp(gti, val); return 0; } static const struct thermal_zone_of_device_ops gadc_thermal_ops = { .get_temp = gadc_thermal_get_temppre { line-height: 125%; } td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; } span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; } td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; } span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; } .highlight .hll { background-color: #ffffcc } .highlight .c { color: #888888 } /* Comment */ .highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */ .highlight .k { color: #008800; font-weight: bold } /* Keyword */ .highlight .ch { color: #888888 } /* Comment.Hashbang */ .highlight .cm { color: #888888 } /* Comment.Multiline */ .highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */ .highlight .cpf { color: #888888 } /* Comment.PreprocFile */ .highlight .c1 { color: #888888 } /* Comment.Single */ .highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */use anyhow::Context as _; use std::collections::HashMap; use std::io; use rpassword; use sequoia_openpgp as openpgp; use crate::openpgp::types::SymmetricAlgorithm; use crate::openpgp::fmt::hex; use crate::openpgp::crypto::{self, SessionKey}; use crate::openpgp::{Fingerprint, Cert, KeyID, Result}; use crate::openpgp::packet; use crate::openpgp::packet::prelude::*; use crate::openpgp::parse::{ Parse, PacketParser, PacketParserResult, }; use crate::openpgp::parse::stream::{ VerificationHelper, DecryptionHelper, DecryptorBuilder, MessageStructure, }; use crate::{ Config, commands::{ dump::PacketDumper, VHelper, }, }; struct Helper<'a> { vhelper: VHelper<'a>, secret_keys: HashMap<KeyID, Key<key::SecretParts, key::UnspecifiedRole>>, key_identities: HashMap<KeyID, Fingerprint>, key_hints: HashMap<KeyID, String>, dump_session_key: bool, dumper: Option<PacketDumper>, } impl<'a> Helper<'a> { fn new(config: &Config<'a>, signatures: usize, certs: Vec<Cert>, secrets: Vec<Cert>, dump_session_key: bool, dump: bool) -> Self { let mut keys = HashMap::new(); let mut identities: HashMap<KeyID, Fingerprint> = HashMap::new(); let mut hints: HashMap<KeyID, String> = HashMap::new(); for tsk in secrets { let hint = match tsk.with_policy(&config.policy, None) .and_then(|valid_cert| valid_cert.primary_userid()).ok() { Some(uid) => format!("{} ({})", uid.userid(), KeyID::from(tsk.fingerprint())), None => format!("{}", KeyID::from(tsk.fingerprint())), }; for ka in tsk.keys() // XXX: Should use the message's creation time that we do not know. .with_policy(&config.policy, None) .for_transport_encryption().for_storage_encryption() .secret() { let id: KeyID = ka.key().fingerprint().into(); keys.insert(id.clone(), ka.key().clone()); identities.insert(id.clone(), tsk.fingerprint()); hints.insert(id, hint.clone()); } } Helper { vhelper: VHelper::new(config, signatures, certs), secret_keys: keys, key_identities: identities, key_hints: hints, dump_session_key: dump_session_key, dumper: if dump { let width = term_size::dimensions_stdout().map(|(w, _)| w) .unwrap_or(80); Some(PacketDumper::new(width, false)) } else { None }, } } /// Tries to decrypt the given PKESK packet with `keypair` and try /// to decrypt the packet parser using `decrypt`. fn try_decrypt<D>(&self, pkesk: &PKESK, sym_algo: Option<SymmetricAlgorithm>, keypair: &mut dyn crypto::Decryptor, decrypt: &mut D) -> Option<Option<Fingerprint>> where D: FnMut(SymmetricAlgorithm, &SessionKey) -> bool { let keyid = keypair.public().fingerprint().into(); match pkesk.decrypt(keypair, sym_algo) .and_then(|(algo, sk)| { if decrypt(algo, &sk) { Some(sk) } else { None } }) { Some(sk) => { if self.dump_session_key { eprintln!("Session key: {}", hex::encode(&sk)); } Some(self.key_identities.get(&keyid).map(|fp| fp.clone())) }, None => None, } } } impl<'a> VerificationHelper for Helper<'a> { fn inspect(&mut self, pp: &PacketParser) -> Result<()> { if let Some(dumper) = self.dumper.as_mut() { dumper.packet(&mut io::stderr(), pp.recursion_depth() as usize, pp.header().clone(), pp.packet.clone(), pp.map().map(|m| m.clone()), None)?; } Ok(()) } fn get_certs(&mut self, ids: &[openpgp::KeyHandle]) -> Result<Vec<Cert>> { self.vhelper.get_certs(ids) } fn check(&mut self, structure: MessageStructure) -> Result<()> { self.vhelper.check(structure) } } impl<'a> DecryptionHelper for Helper<'a> { fn decrypt<D>(&mut self, pkesks: &[PKESK], skesks: &[SKESK], sym_algo: Option<SymmetricAlgorithm>, mut decrypt: D) -> openpgp::Result<Option<Fingerprint>> where D: FnMut(SymmetricAlgorithm, &SessionKey) -> bool { // First, we try those keys that we can use without prompting // for a password. for pkesk in pkesks { let keyid = pkesk.recipient(); if let Some(key) = self.secret_keys.get(&keyid) { if ! key.secret().is_encrypted() { if let Some(fp) = key.clone().into_keypair().ok() .and_then(|mut k| self.try_decrypt(pkesk, sym_algo, &mut k, &mut decrypt
use anyhow::Context as _; use std::collections::HashMap; use std::io; use rpassword; use sequoia_openpgp as openpgp; use crate::openpgp::types::SymmetricAlgorithm; use crate::openpgp::fmt::hex; use crate::openpgp::crypto::{self, SessionKey}; use crate::openpgp::{Fingerprint, Cert, KeyID, Result}; use crate::openpgp::packet; use crate::openpgp::packet::prelude::*; use crate::openpgp::parse::{ Parse, PacketParser, PacketParserResult, }; use crate::openpgp::parse::stream::{ VerificationHelper, DecryptionHelper, DecryptorBuilder, MessageStructure, }; use crate::{ Config, commands::{ dump::PacketDumper, VHelper, }, }; struct Helper<'a> { vhelper: VHelper<'a>, secret_keys: HashMap<KeyID, Key<key::SecretParts, key::UnspecifiedRole>>, key_identities: HashMap<KeyID, Fingerprint>, key_hints: HashMap<KeyID, String>, dump_session_key: bool, dumper: Option<PacketDumper>, } impl<'a> Helper<'a> { fn new(config: &Config<'a>, signatures: usize, certs: Vec<Cert>, secrets: Vec<Cert>, dump_session_key: bool, dump: bool) -> Self { let mut keys = HashMap::new(); let mut identities: HashMap<KeyID, Fingerprint> = HashMap::new(); let mut hints: HashMap<KeyID, String> = HashMap::new(); for tsk in secrets { let hint = match tsk.with_policy(&config.policy, None) .and_then(|valid_cert| valid_cert.primary_userid()).ok() { Some(uid) => format!("{} ({})", uid.userid(), KeyID::from(tsk.fingerprint())), None => format!("{}", KeyID::from(tsk.fingerprint())), }; for ka in tsk.keys() // XXX: Should use the message's creation time that we do not know. .with_policy(&config.policy, None) .for_transport_encryption().for_storage_encryption() .secret() { let id: KeyID = ka.key().fingerprint().into(); keys.insert(id.clone(), ka.key().clone()); identities.insert(id.clone(), tsk.fingerprint()); hints.insert(id, hint.clone()); } } Helper { vhelper: VHelper::new(config, signatures, certs), secret_keys: keys, key_identities: identities, key_hints: hints, dump_session_key: dump_session_key, dumper: if dump { let width = term_size::dimensions_stdout().map(|(w, _)| w) .unwrap_or(80); Some(PacketDumper::new(width, false)) } else { None }, } } /// Tries to decrypt the given PKESK packet with `keypair` and try /// to decrypt the packet parser using `decrypt`. fn try_decrypt<D>(&self, pkesk: &PKESK, sym_algo: Option<SymmetricAlgorithm>, keypair: &mut dyn crypto::Decryptor, decrypt: &mut D) -> Option<Option<Fingerprint>> where D: FnMut(SymmetricAlgorithm, &SessionKey) -> bool { let keyid = keypair.public().fingerprint().into(); match pkesk.decrypt(keypair, sym_algo) .and_then(|(algo, sk)| { if decrypt(algo, &sk) { Some(sk) } else { None } }) { Some(sk) => { if self.dump_session_key { eprintln!("Session key: {}", hex::encode(&sk)); } Some(self.key_identities.get(&keyid).map(|fp| fp.clone())) }, None => None, } } } impl<'a> VerificationHelper for Helper<'a> { fn inspect(&mut self, pp: &PacketParser) -> Result<()> { if let Some(dumper) = self.dumper.as_mut() { dumper.packet(&mut io::stderr(), pp.recursion_depth() as usize, pp.header().clone(), pp.packet.clone(), pp.map().map(|m| m.clone()), None)?; } Ok(()) } fn get_certs(&mut self, ids: &[openpgp::KeyHandle]) -> Result<Vec<Cert>> { self.vhelper.get_certs(ids) } fn check(&mut self, structure: MessageStructure) -> Result<()> { self.vhelper.check(structure) } } impl<'a> DecryptionHelper for Helper<'a> { fn decrypt<D>(&mut self, pkesks: &[PKESK], skesks: &[SKESK], sym_algo: Option<SymmetricAlgorithm>, mut decrypt: D) -> openpgp::Result<Option<Fingerprint>> where D: FnMut(SymmetricAlgorithm, &SessionKey) -> bool { // First, we try those keys that we can use without prompting // for a password. for pkesk in pkesks { let keyid = pkesk.recipient(); if let Some(key) = self.secret_keys.get(&keyid) { if ! key.secret().is_encrypted() { if let Some(fp) = key.clone().into_keypair().ok() .and_then(|mut k| self.try_decrypt(pkesk, sym_algo, &mut k, &mut decrypt