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

📄 liftmon_snowcon_rpxcllf.c.txt

📁 这是《嵌入式linux-硬件、软件与接口》一书对应的所有linux方面实例的源代码
💻 TXT
📖 第 1 页 / 共 2 页
字号:
/*
 * liftmon_snowcon_rpx-cllf v1.1 03/24/04
 * www.embeddedlinuxinterfacing.com
 *
 * The original location of this code is
 * http://www.embeddedlinuxinterfacing.com/chapters/09/
 *
 * 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
 */

/*
powerpc-linux-gcc -O2 -D__KERNEL__ -DMODULE -I/usr/src/powerpc-linux/include -c liftmon_snowcon_rpxcllf.c -o /tftpboot/powerpc-rootfs/tmp/liftmon_snowcon_rpxcllf.o
*/

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

/* liftmon_snowcon
 * liftmon_snowcon uses read and write system calls to control
 * an interface circuit connected the RPX-CLLF's bus and expansion ports.
 * 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 */


/* references
 * see section 34.2 Port A MPC860 PowerQUICC User's Manual
 *     section 16.5 General-Purpose Chip-Select Machine (GPCM)
 */


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

#include <asm/8xx_immap.h>


#define MODULE_VERSION "1.0"
#define MODULE_NAME "liftmon_snowcon_cllf"

volatile immap_t *immap;
static void *io_base;


#define EXPANSIONPORT          0x80000000
#define EXPANSIONPORTLEN       16

#define PA0                    0x8000


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

/* 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;

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;

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. A read from io_base gets the AC input
 * status module. The result is 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 int v;
  struct liftmon_snowcon_data_t *liftmon_snowcon_data =
                                 (struct liftmon_snowcon_data_t *)data;

  v = readb(io_base);

/* mask the input value based on the mask. Each mask is different depending
 * which /proc/trailblazer file was read.
 * Electical 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] == '0')
    snowcondata |= liftmon_snowcon_data->mask;

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

  writeb(snowcondata, io_base + 1);
  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)
{
  struct liftmon_snowcon_data_t *liftmon_snowcon_data =
                                 (struct liftmon_snowcon_data_t *)data;

/* mask the snowcondata value based on the mask. Each mask is different
 * depending which /proc/trailblazer file was read. */
  if ( snowcondata & liftmon_snowcon_data->mask )
    page[0] = '0';
  else
    page[0] = '1';

/* return the length */
  return 1;
}

/* init - init_liftmon_snowcon
 * init_liftmon_snowcon creates the /proc entry files and obtains
 * their pointers. For each file, the fields, data, read_proc,

⌨️ 快捷键说明

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