1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * EISA "eeprom" support routines
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2001 Thomas Bogendoerfer <tsbogend at parisc-linux.org>
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <linux/module.h>
9*4882a593Smuzhiyun #include <linux/init.h>
10*4882a593Smuzhiyun #include <linux/kernel.h>
11*4882a593Smuzhiyun #include <linux/miscdevice.h>
12*4882a593Smuzhiyun #include <linux/slab.h>
13*4882a593Smuzhiyun #include <linux/fs.h>
14*4882a593Smuzhiyun #include <asm/io.h>
15*4882a593Smuzhiyun #include <linux/uaccess.h>
16*4882a593Smuzhiyun #include <asm/eisa_eeprom.h>
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #define EISA_EEPROM_MINOR 241
19*4882a593Smuzhiyun
eisa_eeprom_llseek(struct file * file,loff_t offset,int origin)20*4882a593Smuzhiyun static loff_t eisa_eeprom_llseek(struct file *file, loff_t offset, int origin)
21*4882a593Smuzhiyun {
22*4882a593Smuzhiyun return fixed_size_llseek(file, offset, origin, HPEE_MAX_LENGTH);
23*4882a593Smuzhiyun }
24*4882a593Smuzhiyun
eisa_eeprom_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)25*4882a593Smuzhiyun static ssize_t eisa_eeprom_read(struct file * file,
26*4882a593Smuzhiyun char __user *buf, size_t count, loff_t *ppos )
27*4882a593Smuzhiyun {
28*4882a593Smuzhiyun unsigned char *tmp;
29*4882a593Smuzhiyun ssize_t ret;
30*4882a593Smuzhiyun int i;
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun if (*ppos < 0 || *ppos >= HPEE_MAX_LENGTH)
33*4882a593Smuzhiyun return 0;
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun count = *ppos + count < HPEE_MAX_LENGTH ? count : HPEE_MAX_LENGTH - *ppos;
36*4882a593Smuzhiyun tmp = kmalloc(count, GFP_KERNEL);
37*4882a593Smuzhiyun if (tmp) {
38*4882a593Smuzhiyun for (i = 0; i < count; i++)
39*4882a593Smuzhiyun tmp[i] = readb(eisa_eeprom_addr+(*ppos)++);
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun if (copy_to_user (buf, tmp, count))
42*4882a593Smuzhiyun ret = -EFAULT;
43*4882a593Smuzhiyun else
44*4882a593Smuzhiyun ret = count;
45*4882a593Smuzhiyun kfree (tmp);
46*4882a593Smuzhiyun } else
47*4882a593Smuzhiyun ret = -ENOMEM;
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun return ret;
50*4882a593Smuzhiyun }
51*4882a593Smuzhiyun
eisa_eeprom_open(struct inode * inode,struct file * file)52*4882a593Smuzhiyun static int eisa_eeprom_open(struct inode *inode, struct file *file)
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun if (file->f_mode & FMODE_WRITE)
55*4882a593Smuzhiyun return -EINVAL;
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun return 0;
58*4882a593Smuzhiyun }
59*4882a593Smuzhiyun
eisa_eeprom_release(struct inode * inode,struct file * file)60*4882a593Smuzhiyun static int eisa_eeprom_release(struct inode *inode, struct file *file)
61*4882a593Smuzhiyun {
62*4882a593Smuzhiyun return 0;
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun /*
66*4882a593Smuzhiyun * The various file operations we support.
67*4882a593Smuzhiyun */
68*4882a593Smuzhiyun static const struct file_operations eisa_eeprom_fops = {
69*4882a593Smuzhiyun .owner = THIS_MODULE,
70*4882a593Smuzhiyun .llseek = eisa_eeprom_llseek,
71*4882a593Smuzhiyun .read = eisa_eeprom_read,
72*4882a593Smuzhiyun .open = eisa_eeprom_open,
73*4882a593Smuzhiyun .release = eisa_eeprom_release,
74*4882a593Smuzhiyun };
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun static struct miscdevice eisa_eeprom_dev = {
77*4882a593Smuzhiyun EISA_EEPROM_MINOR,
78*4882a593Smuzhiyun "eisa_eeprom",
79*4882a593Smuzhiyun &eisa_eeprom_fops
80*4882a593Smuzhiyun };
81*4882a593Smuzhiyun
eisa_eeprom_init(void)82*4882a593Smuzhiyun static int __init eisa_eeprom_init(void)
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun int retval;
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun if (!eisa_eeprom_addr)
87*4882a593Smuzhiyun return -ENODEV;
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun retval = misc_register(&eisa_eeprom_dev);
90*4882a593Smuzhiyun if (retval < 0) {
91*4882a593Smuzhiyun printk(KERN_ERR "EISA EEPROM: cannot register misc device.\n");
92*4882a593Smuzhiyun return retval;
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun printk(KERN_INFO "EISA EEPROM at 0x%px\n", eisa_eeprom_addr);
96*4882a593Smuzhiyun return 0;
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun MODULE_LICENSE("GPL");
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun module_init(eisa_eeprom_init);
102