Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * SCR24x PCMCIA Smart Card Reader Driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2005-2006 TL Sudheendran
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (C) 2016 Lubomir Rintel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Derived from "scr24x_v4.2.6_Release.tar.gz" driver by TL Sudheendran.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/cdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <pcmcia/cistpl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <pcmcia/ds.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define CCID_HEADER_SIZE	10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define CCID_LENGTH_OFFSET	1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define CCID_MAX_LEN		271
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define SCR24X_DATA(n)		(1 + n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define SCR24X_CMD_STATUS	7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define CMD_START		0x40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define CMD_WRITE_BYTE		0x41
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define CMD_READ_BYTE		0x42
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define STATUS_BUSY		0x80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) struct scr24x_dev {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	struct cdev c_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	unsigned char buf[CCID_MAX_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	int devno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	struct mutex lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	struct kref refcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	u8 __iomem *regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define SCR24X_DEVS 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) static DECLARE_BITMAP(scr24x_minors, SCR24X_DEVS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) static struct class *scr24x_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) static dev_t scr24x_devt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) static void scr24x_delete(struct kref *kref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	struct scr24x_dev *dev = container_of(kref, struct scr24x_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 								refcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	kfree(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) static int scr24x_wait_ready(struct scr24x_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	u_char status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	int timeout = 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		status = ioread8(dev->regs + SCR24X_CMD_STATUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		if (!(status & STATUS_BUSY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		msleep(20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	} while (--timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) static int scr24x_open(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	struct scr24x_dev *dev = container_of(inode->i_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 				struct scr24x_dev, c_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	kref_get(&dev->refcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	filp->private_data = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	return stream_open(inode, filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) static int scr24x_release(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	struct scr24x_dev *dev = filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	/* We must not take the dev->lock here as scr24x_delete()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	 * might be called to remove the dev structure altogether.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	 * We don't need the lock anyway, since after the reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	 * acquired in probe() is released in remove() the chrdev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	 * is already unregistered and noone can possibly acquire
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	 * a reference via open() anymore. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	kref_put(&dev->refcnt, scr24x_delete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) static int read_chunk(struct scr24x_dev *dev, size_t offset, size_t limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	size_t i, y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	for (i = offset; i < limit; i += 5) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		iowrite8(CMD_READ_BYTE, dev->regs + SCR24X_CMD_STATUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		ret = scr24x_wait_ready(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		for (y = 0; y < 5 && i + y < limit; y++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 			dev->buf[i + y] = ioread8(dev->regs + SCR24X_DATA(y));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static ssize_t scr24x_read(struct file *filp, char __user *buf, size_t count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 								loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	struct scr24x_dev *dev = filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	if (count < CCID_HEADER_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	if (mutex_lock_interruptible(&dev->lock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		return -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	if (!dev->dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	ret = scr24x_wait_ready(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	len = CCID_HEADER_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	ret = read_chunk(dev, 0, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	len += le32_to_cpu(*(__le32 *)(&dev->buf[CCID_LENGTH_OFFSET]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	if (len > sizeof(dev->buf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	ret = read_chunk(dev, CCID_HEADER_SIZE, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	if (len < count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		count = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	if (copy_to_user(buf, dev->buf, count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	mutex_unlock(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static ssize_t scr24x_write(struct file *filp, const char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 					size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	struct scr24x_dev *dev = filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	size_t i, y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	if (mutex_lock_interruptible(&dev->lock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		return -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	if (!dev->dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	if (count > sizeof(dev->buf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	if (copy_from_user(dev->buf, buf, count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	ret = scr24x_wait_ready(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	iowrite8(CMD_START, dev->regs + SCR24X_CMD_STATUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	ret = scr24x_wait_ready(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	for (i = 0; i < count; i += 5) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		for (y = 0; y < 5 && i + y < count; y++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 			iowrite8(dev->buf[i + y], dev->regs + SCR24X_DATA(y));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		iowrite8(CMD_WRITE_BYTE, dev->regs + SCR24X_CMD_STATUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		ret = scr24x_wait_ready(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	mutex_unlock(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) static const struct file_operations scr24x_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	.read		= scr24x_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	.write		= scr24x_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	.open		= scr24x_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	.release	= scr24x_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	.llseek		= no_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) static int scr24x_config_check(struct pcmcia_device *link, void *priv_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	if (resource_size(link->resource[PCMCIA_IOPORT_0]) != 0x11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	return pcmcia_request_io(link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) static int scr24x_probe(struct pcmcia_device *link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	struct scr24x_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	dev->devno = find_first_zero_bit(scr24x_minors, SCR24X_DEVS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	if (dev->devno >= SCR24X_DEVS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	mutex_init(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	kref_init(&dev->refcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	link->priv = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	link->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_IO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	ret = pcmcia_loop_config(link, scr24x_config_check, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	dev->dev = &link->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	dev->regs = devm_ioport_map(&link->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 				link->resource[PCMCIA_IOPORT_0]->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 				resource_size(link->resource[PCMCIA_IOPORT_0]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	if (!dev->regs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	cdev_init(&dev->c_dev, &scr24x_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	dev->c_dev.owner = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	dev->c_dev.ops = &scr24x_fops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	ret = cdev_add(&dev->c_dev, MKDEV(MAJOR(scr24x_devt), dev->devno), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	ret = pcmcia_enable_device(link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		pcmcia_disable_device(link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	device_create(scr24x_class, NULL, MKDEV(MAJOR(scr24x_devt), dev->devno),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		      NULL, "scr24x%d", dev->devno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	dev_info(&link->dev, "SCR24x Chip Card Interface\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	if (dev->devno < SCR24X_DEVS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		clear_bit(dev->devno, scr24x_minors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	kfree (dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) static void scr24x_remove(struct pcmcia_device *link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	struct scr24x_dev *dev = (struct scr24x_dev *)link->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	device_destroy(scr24x_class, MKDEV(MAJOR(scr24x_devt), dev->devno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	mutex_lock(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	pcmcia_disable_device(link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	cdev_del(&dev->c_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	clear_bit(dev->devno, scr24x_minors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	dev->dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	mutex_unlock(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	kref_put(&dev->refcnt, scr24x_delete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) static const struct pcmcia_device_id scr24x_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	PCMCIA_DEVICE_PROD_ID12("HP", "PC Card Smart Card Reader",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 					0x53cb94f9, 0xbfdf89a5),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	PCMCIA_DEVICE_PROD_ID1("SCR241 PCMCIA", 0x6271efa3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	PCMCIA_DEVICE_PROD_ID1("SCR243 PCMCIA", 0x2054e8de),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	PCMCIA_DEVICE_PROD_ID1("SCR24x PCMCIA", 0x54a33665),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	PCMCIA_DEVICE_NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) MODULE_DEVICE_TABLE(pcmcia, scr24x_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) static struct pcmcia_driver scr24x_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	.name		= "scr24x_cs",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	.probe		= scr24x_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	.remove		= scr24x_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	.id_table	= scr24x_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) static int __init scr24x_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	scr24x_class = class_create(THIS_MODULE, "scr24x");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	if (IS_ERR(scr24x_class))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		return PTR_ERR(scr24x_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	ret = alloc_chrdev_region(&scr24x_devt, 0, SCR24X_DEVS, "scr24x");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	if (ret < 0)  {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		class_destroy(scr24x_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	ret = pcmcia_register_driver(&scr24x_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		unregister_chrdev_region(scr24x_devt, SCR24X_DEVS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		class_destroy(scr24x_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) static void __exit scr24x_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	pcmcia_unregister_driver(&scr24x_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	unregister_chrdev_region(scr24x_devt, SCR24X_DEVS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	class_destroy(scr24x_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) module_init(scr24x_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) module_exit(scr24x_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) MODULE_AUTHOR("Lubomir Rintel");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) MODULE_DESCRIPTION("SCR24x PCMCIA Smart Card Reader Driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) MODULE_LICENSE("GPL");