github.com/rigado/snapd@v2.42.5-go-mod+incompatible/interfaces/builtin/spi.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2017 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package builtin
    21  
    22  import (
    23  	"fmt"
    24  	"path/filepath"
    25  	"regexp"
    26  	"strings"
    27  
    28  	"github.com/snapcore/snapd/interfaces"
    29  	"github.com/snapcore/snapd/interfaces/apparmor"
    30  	"github.com/snapcore/snapd/interfaces/udev"
    31  	"github.com/snapcore/snapd/snap"
    32  )
    33  
    34  const spiSummary = `allows access to specific spi controller`
    35  
    36  const spiBaseDeclarationSlots = `
    37    spi:
    38      allow-installation:
    39        slot-snap-type:
    40          - core
    41          - gadget
    42      deny-auto-connection: true
    43  `
    44  
    45  type spiInterface struct{}
    46  
    47  func (iface *spiInterface) Name() string {
    48  	return "spi"
    49  }
    50  
    51  func (iface *spiInterface) StaticInfo() interfaces.StaticInfo {
    52  	return interfaces.StaticInfo{
    53  		Summary:              spiSummary,
    54  		BaseDeclarationSlots: spiBaseDeclarationSlots,
    55  	}
    56  }
    57  
    58  var spiDevPattern = regexp.MustCompile(`^/dev/spidev[0-9]+\.[0-9]+$`)
    59  
    60  func (iface *spiInterface) path(slotRef *interfaces.SlotRef, attrs interfaces.Attrer) (string, error) {
    61  	var path string
    62  	if err := attrs.Attr("path", &path); err != nil || path == "" {
    63  		return "", fmt.Errorf("slot %q must have a path attribute", slotRef)
    64  	}
    65  	path = filepath.Clean(path)
    66  	if !spiDevPattern.MatchString(path) {
    67  		return "", fmt.Errorf("%q is not a valid SPI device", path)
    68  	}
    69  	return path, nil
    70  }
    71  
    72  func (iface *spiInterface) BeforePrepareSlot(slot *snap.SlotInfo) error {
    73  	_, err := iface.path(&interfaces.SlotRef{Snap: slot.Snap.InstanceName(), Name: slot.Name}, slot)
    74  	return err
    75  }
    76  
    77  func (iface *spiInterface) AppArmorConnectedPlug(spec *apparmor.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error {
    78  	path, err := iface.path(slot.Ref(), slot)
    79  	if err != nil {
    80  		return nil
    81  	}
    82  	spec.AddSnippet(fmt.Sprintf("%s rw,", path))
    83  	spec.AddSnippet(fmt.Sprintf("/sys/devices/platform/**/**.spi/**/%s/** rw,", strings.TrimPrefix(path, "/dev/")))
    84  	return nil
    85  }
    86  
    87  func (iface *spiInterface) UDevConnectedPlug(spec *udev.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error {
    88  	path, err := iface.path(slot.Ref(), slot)
    89  	if err != nil {
    90  		return nil
    91  	}
    92  	spec.TagDevice(fmt.Sprintf(`KERNEL=="%s"`, strings.TrimPrefix(path, "/dev/")))
    93  	return nil
    94  }
    95  
    96  func (iface *spiInterface) AutoConnect(*snap.PlugInfo, *snap.SlotInfo) bool {
    97  	// Allow what is allowed in the declarations
    98  	return true
    99  }
   100  
   101  func init() {
   102  	registerIface(&spiInterface{})
   103  }