18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *    EISA "eeprom" support routines
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *    Copyright (C) 2001 Thomas Bogendoerfer <tsbogend at parisc-linux.org>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/module.h>
98c2ecf20Sopenharmony_ci#include <linux/init.h>
108c2ecf20Sopenharmony_ci#include <linux/kernel.h>
118c2ecf20Sopenharmony_ci#include <linux/miscdevice.h>
128c2ecf20Sopenharmony_ci#include <linux/slab.h>
138c2ecf20Sopenharmony_ci#include <linux/fs.h>
148c2ecf20Sopenharmony_ci#include <asm/io.h>
158c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
168c2ecf20Sopenharmony_ci#include <asm/eisa_eeprom.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#define 	EISA_EEPROM_MINOR 241
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistatic loff_t eisa_eeprom_llseek(struct file *file, loff_t offset, int origin)
218c2ecf20Sopenharmony_ci{
228c2ecf20Sopenharmony_ci	return fixed_size_llseek(file, offset, origin, HPEE_MAX_LENGTH);
238c2ecf20Sopenharmony_ci}
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistatic ssize_t eisa_eeprom_read(struct file * file,
268c2ecf20Sopenharmony_ci			      char __user *buf, size_t count, loff_t *ppos )
278c2ecf20Sopenharmony_ci{
288c2ecf20Sopenharmony_ci	unsigned char *tmp;
298c2ecf20Sopenharmony_ci	ssize_t ret;
308c2ecf20Sopenharmony_ci	int i;
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	if (*ppos < 0 || *ppos >= HPEE_MAX_LENGTH)
338c2ecf20Sopenharmony_ci		return 0;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	count = *ppos + count < HPEE_MAX_LENGTH ? count : HPEE_MAX_LENGTH - *ppos;
368c2ecf20Sopenharmony_ci	tmp = kmalloc(count, GFP_KERNEL);
378c2ecf20Sopenharmony_ci	if (tmp) {
388c2ecf20Sopenharmony_ci		for (i = 0; i < count; i++)
398c2ecf20Sopenharmony_ci			tmp[i] = readb(eisa_eeprom_addr+(*ppos)++);
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci		if (copy_to_user (buf, tmp, count))
428c2ecf20Sopenharmony_ci			ret = -EFAULT;
438c2ecf20Sopenharmony_ci		else
448c2ecf20Sopenharmony_ci			ret = count;
458c2ecf20Sopenharmony_ci		kfree (tmp);
468c2ecf20Sopenharmony_ci	} else
478c2ecf20Sopenharmony_ci		ret = -ENOMEM;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	return ret;
508c2ecf20Sopenharmony_ci}
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic int eisa_eeprom_open(struct inode *inode, struct file *file)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	if (file->f_mode & FMODE_WRITE)
558c2ecf20Sopenharmony_ci		return -EINVAL;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	return 0;
588c2ecf20Sopenharmony_ci}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic int eisa_eeprom_release(struct inode *inode, struct file *file)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	return 0;
638c2ecf20Sopenharmony_ci}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci/*
668c2ecf20Sopenharmony_ci *	The various file operations we support.
678c2ecf20Sopenharmony_ci */
688c2ecf20Sopenharmony_cistatic const struct file_operations eisa_eeprom_fops = {
698c2ecf20Sopenharmony_ci	.owner =	THIS_MODULE,
708c2ecf20Sopenharmony_ci	.llseek =	eisa_eeprom_llseek,
718c2ecf20Sopenharmony_ci	.read =		eisa_eeprom_read,
728c2ecf20Sopenharmony_ci	.open =		eisa_eeprom_open,
738c2ecf20Sopenharmony_ci	.release =	eisa_eeprom_release,
748c2ecf20Sopenharmony_ci};
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistatic struct miscdevice eisa_eeprom_dev = {
778c2ecf20Sopenharmony_ci	EISA_EEPROM_MINOR,
788c2ecf20Sopenharmony_ci	"eisa_eeprom",
798c2ecf20Sopenharmony_ci	&eisa_eeprom_fops
808c2ecf20Sopenharmony_ci};
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistatic int __init eisa_eeprom_init(void)
838c2ecf20Sopenharmony_ci{
848c2ecf20Sopenharmony_ci	int retval;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	if (!eisa_eeprom_addr)
878c2ecf20Sopenharmony_ci		return -ENODEV;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	retval = misc_register(&eisa_eeprom_dev);
908c2ecf20Sopenharmony_ci	if (retval < 0) {
918c2ecf20Sopenharmony_ci		printk(KERN_ERR "EISA EEPROM: cannot register misc device.\n");
928c2ecf20Sopenharmony_ci		return retval;
938c2ecf20Sopenharmony_ci	}
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	printk(KERN_INFO "EISA EEPROM at 0x%px\n", eisa_eeprom_addr);
968c2ecf20Sopenharmony_ci	return 0;
978c2ecf20Sopenharmony_ci}
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_cimodule_init(eisa_eeprom_init);
102