⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 liftmon_snowcon.c.txt

📁 这是《嵌入式linux-硬件、软件与接口》一书对应的所有linux方面实例的源代码
💻 TXT
📖 第 1 页 / 共 2 页
字号:
/*
 * liftmon_snowcon v1.1 03/23/04 
 * www.embeddedlinuxinterfacing.com
 *
 * The original location of this code is
 * http://www.embeddedlinuxinterfacing.com/chapters/07/liftmon_snowcon.c
 *
 * Copyright (C) 2001 by Craig Hollabaugh
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

/*
 * liftmon_snowcon.c is based on procfs_example.c by Erik Mouw.
 * For more information, please see The Linux Kernel Procfs Guide,
 * http://kernelnewbies.org/documents/kdoc/procfs-guide/lkprocfsguide.html
 */


/* liftmon_snowcon
 * liftmon_snowcon uses inb and outp port I/O system calls to control
 * an interface circuit connected the PC parallel printer port. The port's
 * control port drives the interface circuit's output latch signals and
 * input buffer. This module performs all bit operations for the data bus.
 * Bash script need only read and write to /proc entry files to determine
 * status or control equipment. In addition, the module's init code asserts
 * the OUTPUT_ENABLE signal and its exit code deasserts the OUTPUT_ENABLE
 * signal.
 *
 * This module creates these /proc entries:
 * Trailblazer directory  /proc/trailblazer
 * Lift Monitoring
 *  AC Mains              /proc/trailblazer/liftacmains
 *  Motor Controller      /proc/trailblazer/liftmotorcontroller
 *  Low Speed Operation   /proc/trailblazer/liftslowspeed
 *  High Speed Operation  /proc/trailblazer/lifthighspeed
 *  Operator Switch Base  /proc/trailblazer/liftoperatorswitchbase
 *  Operator Switch Top   /proc/trailblazer/liftoperatorswitchtop
 * Snow-Making Control
 *  Water Value 1         /proc/trailblazer/snowwatervalve1
 *  Water Value 2         /proc/trailblazer/snowwatervalve2
 *  Water Value 3         /proc/trailblazer/snowwatervalve3
 *  Heater                /proc/trailblazer/snowheater1
 */

/* v1.1 fixed the removal of proc directory entries in init_liftmon_snowcon */

/*
gcc -O2 -D__KERNEL__ -DMODULE -I/usr/src/linux/include -c liftmon_snowcon.c -o /tftpboot/i386-rootfs/liftmon_snowcon.o
*/

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/sched.h>
#include <asm/uaccess.h>

#include <asm/io.h>

#define MODULE_VERSION "1.0"
#define MODULE_NAME    "liftmon_snowcon"

/* Standard Parallel Port (SPP) definitions */
#define SPPDATAPORT     0x378
#define SPPSTATUSPORT  (SPPDATAPORT + 1)
#define SPPCONTROLPORT (SPPDATAPORT + 2)

/* SPP control port bit definitions */
#define OUTPUTENABLE           0x02
#define OUTPUTLATCH            0x04
#define INPUTENABLE            0x08
#define SPPPORTREAD            0x20

/* SPP input bit definitions */
#define LIFTACMAINS            0x01
#define LIFTMOTORCONTROLLER    0x02
#define LIFTSLOWSPEED          0x04
#define LIFTHIGHSPEED          0x08
#define LIFTOPERATORSWITCHBASE 0x10
#define LIFTOPERATORSWITCHTOP  0x20

/* SPP output bit definitions */
#define SNOWWATERVALVE1        0x01
#define SNOWWATERVALVE2        0x02
#define SNOWWATERVALVE3        0x04
#define SNOWHEATER1            0x08

/* define a bitmask, each *_file uses this to determine who it is */
struct liftmon_snowcon_data_t {
        unsigned char mask;
};

/* snowcondata is the output latch value stored internally. Control
   changes made by user scripts writing to /proc/trailblazer entries
   result in bits being either cleared or set in snowcondata. We
   write snowcondata to the output latch every time a control change
   occurs */
unsigned char snowcondata;

/* this are the data structures that hold the mask. When a /proc
   file is read or written to, the read_proc or write_proc routine
   receives a pointer to this structure */
struct liftmon_snowcon_data_t liftacmains_data,
                              liftmotorcontroller_data,
                              liftslowspeed_data,
                              lifthighspeed_data,
                              liftoperatorswitchbase_data,
                              liftoperatorswitchtop_data,
                              snowwatervalve1_data,
                              snowwatervalve2_data,
                              snowwatervalve3_data,
                              snowheater1_data;

/* These are the pointers to the /proc directory entries */
static struct proc_dir_entry  *tb_dir,
                              *liftacmains_file,
                              *liftmotorcontroller_file,
                              *liftslowspeed_file,
                              *lifthighspeed_file,
                              *liftoperatorswitchbase_file,
                              *liftoperatorswitchtop_file,
                              *snowwatervalve1_file,
                              *snowwatervalve2_file,
                              *snowwatervalve3_file,
                              *snowheater1_file;

/* proc_read - proc_read_liftmon
 * proc_read_liftmon is the callback function that the kernel calls when
 * there's a read file operation on these /proc/trailblazer files: 
 * liftacmains, lifthighspeed, liftmotorcontroller, liftoperatorswitchbase
 * liftoperatorswitchtop, and liftslowspeed. The file's data pointer is
 * passed in the data parameter. You first cast it to the
 * liftmon_snowcon_data_t structure. The input module rack values are
 * read by configuring SPP data for input and asserting the input buffer.
 * This places the state of the input modules on the SPP data port. Using
 * inb, the bit values are read then anded with the bitmask value to
 * to determine if the particular input module is on or off. Which
 * particular input module is defined by which /proc/trailblazer/ file
 * is read.
 */
static int proc_read_liftmon(char *page, char **start, off_t off, int count,
                             int *eof, void *data)
{
  unsigned char v;
  struct liftmon_snowcon_data_t *liftmon_snowcon_data =
                                 (struct liftmon_snowcon_data_t *)data;

  outb(OUTPUTENABLE, SPPCONTROLPORT);
/* this asserts three items.
 * 1. SSPPORTREAD, this configures the bidirectional data port as input
 * 2. INPUTENABLE, this enables the input buffer to drive the data bus
 * 3. OUTPUTENABLE, enable the output latch, driving the output rack
 *                  if you don't assert this, the output modules will
 *                  turn off
 */
  outb(SPPPORTREAD | INPUTENABLE  | OUTPUTENABLE, SPPCONTROLPORT);

/* The input buffer is now driving the bus, so do a read */
  v = inb(SPPDATAPORT);

/* Deassert SPPORTREAD and INPUTENABLE.
 * Use OUTPUTENABLE to keep output latch enabled
 */
  outb(OUTPUTENABLE, SPPCONTROLPORT);

/* mask the input value based on the mask. Each mask is different depending
 * which /proc/trailblazer file was read.
 * Electrical note: returning an inverted value because AC power to an input
 * module pulls outputs a low and the input buffer, 74244, doesn't invert
 */
  if (v & liftmon_snowcon_data->mask)
    page[0] = '0';
  else
    page[0] = '1';

/* return 1 which is the length of page */
  return 1;
}

/* proc_write - proc_write_snowcondata
 * proc_write_snowcondata is the callback function that the kernel calls 
 * when there's a write file operation on these /proc/trailblazer files: 
 * snowheater1, snowwatervalve1, snowwatervalve2 and snowwatervalve3.
 * The file's data pointer is passed in the data parameter. You first 
 * cast it to the liftmon_snowcon_data_t structure. The buffer parameter 
 * points to the incoming data. If the incoming data is a 1 or a 0, 
 * a bit in snowcondata is set or cleared. Which bit is defined by which
 * /proc/trailblazer file is written to. snowcondata is then written to
 * the output latch. 
 */
static int proc_write_snowcondata(struct file *file, const char *buffer, 
                                  unsigned long count, void *data)
{
  struct liftmon_snowcon_data_t *liftmon_snowcon_data = 
                                 (struct liftmon_snowcon_data_t *)data;

/* check if the user wrote a 1 or a 0 the /proc/trailblazer file.
   if so, set or clear a bit in snowcondata */
  if (buffer[0] == '1')
    snowcondata |= liftmon_snowcon_data->mask;

  if (buffer[0] == '0')
    snowcondata &= ~liftmon_snowcon_data->mask;


/* Use OUTPUTENABLE to keep output latch enabled */
  outb(OUTPUTENABLE, SPPCONTROLPORT);

/* put snowcondata on the data bus */
  outb(snowcondata , SPPDATAPORT);

/* assert OUTPUTLATCH, this latches the data bus to the latch's 
 * outputs, driving the output module rack */
  outb(OUTPUTENABLE | OUTPUTLATCH, SPPCONTROLPORT);

/* Use OUTPUTENABLE to keep output latch enabled */
  outb(OUTPUTENABLE,SPPCONTROLPORT);

  return 1;
}

/* proc_read - proc_read_snowcondata
 * proc_read_snowcondata is the callback function that the kernel calls 
 * when there's a read file operation on these /proc/trailblazer files: 
 * snowheater1, snowwatervalve1, snowwatervalve2 and snowwatervalve3.
 * The file's data pointer is passed in the data parameter. You first 
 * cast it to the liftmon_snowcon_data_t structure.  Use snowcondata
 * anded with the bitmask value to determine if the particular output 
 * module is on or off. Which particular output module is defined by 
 * which /proc/trailblazer/ file is read.
 */
static int proc_read_snowcondata(char *page, char **start, off_t off, 
                                 int count, int *eof, void *data)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -