📄 tqm_led.c
字号:
/*********************************************************************** * * (C) Copyright 1999, 2000 * DENX Software Engineering * Wolfgang Denk, wd@denx.de * All rights reserved. * * * Demo Device Driver for TQM8xxL: play with the free LEDs on the STK8xx * * Can be loaded as module * * $Id: tqm_led.c,v 1.1 2000/01/14 16:28:49 wd Exp $ * ***********************************************************************//* * Standard in kernel modules */#include <linux/kernel.h> /* We're doing kernel work */#include <linux/module.h> /* Specifically, a module */#include <asm/uaccess.h> /* for put_user */#include <asm/init.h> /* for __initfunc */#include <asm/8xx_immap.h>#include <asm/mpc8xx.h>static cpm8xx_t *cpmp = NULL; /* Pointer to comm processor */#undef DEBUG#ifdef DEBUG# define debugk(fmt,args...) printk(fmt ,##args)#else# define debugk(fmt,args...)#endif/* * Deal with CONFIG_MODVERSIONS */#if CONFIG_MODVERSIONS==1/* # define MODVERSIONS */# include <linux/modversions.h>#endif/* * For character devices */#include <linux/fs.h> /* character device definitions */#include <linux/wrapper.h> /* wrapper for compatibility with future versions */#ifndef KERNEL_VERSION# define KERNEL_VERSION(a,b,c) ((a)*65536+(b)*256+(c))#endif#define DEBUG 1#define SUCCESS 0#define LED_MAJOR 42 /* free for demo/sample use *//* * Device Declarations *//* * The name for our device, as it will appear in /proc/devices */#define DEVICE_NAME "tqm_led"/* * prevent concurent access of the same device */static int Device_Open = 0;/* * Prototypes */int led_init (void );static int device_open(struct inode *, struct file *);static int device_release(struct inode *, struct file *);static ssize_t device_read(struct file *, char *, size_t, loff_t *);static ssize_t device_write(struct file *, const char *, size_t, loff_t *);int init_module(void);void cleanup_module(void);struct file_operations led_ops ={ NULL, /* seek */ device_read, device_write, NULL, /* readdir */ NULL, /* select */ NULL, /* ioctl */ NULL, /* mmap */ device_open, NULL, /* flush */ device_release /* close */};static int Major;/* * Initialize the driver - Register the character device */__initfunc( int led_init (void) ){ if (!cpmp) { /* init CPM ptr */ unsigned long immr; asm( "mfspr %0,638": "=r"(immr) : ); immr &= 0xFFFF0000; cpmp = (cpm8xx_t *)&((volatile immap_t *)immr)->im_cpm; } /* * Register the character device */ Major = register_chrdev (LED_MAJOR, DEVICE_NAME, &led_ops); /* Negative values signify an error */ if (Major < 0) { printk ("TQM_LED init_module: failed with %d\n", Major); return Major; } Major = LED_MAJOR; printk ("TQM_LED registred: major = %d\n", Major); return 0;}/* * called whenever a process attempts to open the device */static int device_open (struct inode *inode, struct file *file){ volatile cpm8xx_t *cp = cpmp; debugk ("device_open(%p,%p)\n", inode, file); /* * Get major / minor numbers when needed */ debugk ("Device: %d.%d\n", inode->i_rdev >> 8, inode->i_rdev & 0xFF); /* * exclusive open only */ if (Device_Open) { return -EBUSY; } /* * Be careful here on SMP kernels! */ Device_Open++; /* * Make sure that the module isn't removed while * the file is open by incrementing the usage count */ MOD_INC_USE_COUNT; debugk ("LED_OPEN: pbdir=0x%x pbpar=0x%x pbodr=0x%x pbdat=0x%x\n", cp->cp_pbdir, cp->cp_pbpar, cp->cp_pbodr, cp->cp_pbdat); cp->cp_pbdat |= 0x0000003F; cp->cp_pbdir |= 0x0000003F; debugk ("LED_OPEN: pbdir=0x%x pbpar=0x%x pbodr=0x%x pbdat=0x%x\n", cp->cp_pbdir, cp->cp_pbpar, cp->cp_pbodr, cp->cp_pbdat); return SUCCESS;}/* * Called when a process closes the device. * Doesn't have a return value in version 2.0.x because it can't fail, * but in version 2.2.x it is allowed to fail */static int device_release (struct inode *inode, struct file *file){ volatile cpm8xx_t *cp = cpmp; debugk ("device_release(%p,%p)\n", inode, file); /* We're now ready for our next caller */ Device_Open--; MOD_DEC_USE_COUNT; debugk ("LED_CLOSE: pbdir=0x%x pbpar=0x%x pbodr=0x%x pbdat=0x%x\n", cp->cp_pbdir, cp->cp_pbpar, cp->cp_pbodr, cp->cp_pbdat); cp->cp_pbdir &= 0xFFFFFFC0; cp->cp_pbdat &= 0xFFFFFFC0; debugk ("LED_CLOSE: pbdir=0x%x pbpar=0x%x pbodr=0x%x pbdat=0x%x\n", cp->cp_pbdir, cp->cp_pbpar, cp->cp_pbodr, cp->cp_pbdat); return 0;}/* * read entry point: */static ssize_t device_read (struct file *file, char *buffer, /* The buffer to fill with data */ size_t length, /* The length of the buffer */ loff_t * offset) /* Our offset in the file */{ volatile cpm8xx_t *cp = cpmp; unsigned char c = cp->cp_pbdat & 0x0000003F; put_user (c, buffer); debugk ("Read: value = 0x%02x\n", c); return (1); /* read() always returns exactly one byte */}/* * write entry point: dummy here */static ssize_t device_write (struct file *file, const char *buffer, /* buffer */ size_t length, /* length of buffer */ loff_t * offset) /* offset in the file */{ volatile cpm8xx_t *cp = cpmp; size_t len = length; int error; unsigned char c; while (length-- > 0) { unsigned int pbdat = cp->cp_pbdat & 0xFFFFFFC0; if ((error = get_user (c, buffer++)) != 0) { return (error); } c &= 0x3F; cp->cp_pbdat = pbdat | (unsigned int)c; } return (len);}/****************************** **** Module Declarations ***** **************************** */#ifdef MODULE/* * Initialize the module */int init_module (void){ return led_init();}/* * Cleanup - unregister the appropriate file from /proc */void cleanup_module (void){ int ret; /* * Unregister the device */ ret = module_unregister_chrdev (Major, DEVICE_NAME); /* * If there's an error, report it */ if (ret < 0) { printk ("unregister_chrdev: error %d\n", ret); }}#endif /* MODULE */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -