2 * SCR24x PCMCIA Smart Card Reader Driver
4 * Copyright (C) 2005-2006 TL Sudheendran
5 * Copyright (C) 2016 Lubomir Rintel
7 * Derived from "scr24x_v4.2.6_Release.tar.gz" driver by TL Sudheendran.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; see the file COPYING. If not, write to
21 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/device.h>
25 #include <linux/module.h>
26 #include <linux/delay.h>
27 #include <linux/cdev.h>
28 #include <linux/slab.h>
31 #include <linux/uaccess.h>
33 #include <pcmcia/cistpl.h>
34 #include <pcmcia/ds.h>
36 #define CCID_HEADER_SIZE 10
37 #define CCID_LENGTH_OFFSET 1
38 #define CCID_MAX_LEN 271
40 #define SCR24X_DATA(n) (1 + n)
41 #define SCR24X_CMD_STATUS 7
42 #define CMD_START 0x40
43 #define CMD_WRITE_BYTE 0x41
44 #define CMD_READ_BYTE 0x42
45 #define STATUS_BUSY 0x80
50 unsigned char buf
[CCID_MAX_LEN
];
58 static DECLARE_BITMAP(scr24x_minors
, SCR24X_DEVS
);
60 static struct class *scr24x_class
;
61 static dev_t scr24x_devt
;
63 static void scr24x_delete(struct kref
*kref
)
65 struct scr24x_dev
*dev
= container_of(kref
, struct scr24x_dev
,
71 static int scr24x_wait_ready(struct scr24x_dev
*dev
)
77 status
= ioread8(dev
->regs
+ SCR24X_CMD_STATUS
);
78 if (!(status
& STATUS_BUSY
))
87 static int scr24x_open(struct inode
*inode
, struct file
*filp
)
89 struct scr24x_dev
*dev
= container_of(inode
->i_cdev
,
90 struct scr24x_dev
, c_dev
);
92 kref_get(&dev
->refcnt
);
93 filp
->private_data
= dev
;
95 return nonseekable_open(inode
, filp
);
98 static int scr24x_release(struct inode
*inode
, struct file
*filp
)
100 struct scr24x_dev
*dev
= filp
->private_data
;
102 /* We must not take the dev->lock here as scr24x_delete()
103 * might be called to remove the dev structure altogether.
104 * We don't need the lock anyway, since after the reference
105 * acquired in probe() is released in remove() the chrdev
106 * is already unregistered and noone can possibly acquire
107 * a reference via open() anymore. */
108 kref_put(&dev
->refcnt
, scr24x_delete
);
112 static int read_chunk(struct scr24x_dev
*dev
, size_t offset
, size_t limit
)
117 for (i
= offset
; i
< limit
; i
+= 5) {
118 iowrite8(CMD_READ_BYTE
, dev
->regs
+ SCR24X_CMD_STATUS
);
119 ret
= scr24x_wait_ready(dev
);
123 for (y
= 0; y
< 5 && i
+ y
< limit
; y
++)
124 dev
->buf
[i
+ y
] = ioread8(dev
->regs
+ SCR24X_DATA(y
));
130 static ssize_t
scr24x_read(struct file
*filp
, char __user
*buf
, size_t count
,
133 struct scr24x_dev
*dev
= filp
->private_data
;
137 if (count
< CCID_HEADER_SIZE
)
140 if (mutex_lock_interruptible(&dev
->lock
))
148 ret
= scr24x_wait_ready(dev
);
151 len
= CCID_HEADER_SIZE
;
152 ret
= read_chunk(dev
, 0, len
);
156 len
+= le32_to_cpu(*(__le32
*)(&dev
->buf
[CCID_LENGTH_OFFSET
]));
157 if (len
> sizeof(dev
->buf
)) {
161 ret
= read_chunk(dev
, CCID_HEADER_SIZE
, len
);
168 if (copy_to_user(buf
, dev
->buf
, count
)) {
175 mutex_unlock(&dev
->lock
);
179 static ssize_t
scr24x_write(struct file
*filp
, const char __user
*buf
,
180 size_t count
, loff_t
*ppos
)
182 struct scr24x_dev
*dev
= filp
->private_data
;
186 if (mutex_lock_interruptible(&dev
->lock
))
194 if (count
> sizeof(dev
->buf
)) {
199 if (copy_from_user(dev
->buf
, buf
, count
)) {
204 ret
= scr24x_wait_ready(dev
);
208 iowrite8(CMD_START
, dev
->regs
+ SCR24X_CMD_STATUS
);
209 ret
= scr24x_wait_ready(dev
);
213 for (i
= 0; i
< count
; i
+= 5) {
214 for (y
= 0; y
< 5 && i
+ y
< count
; y
++)
215 iowrite8(dev
->buf
[i
+ y
], dev
->regs
+ SCR24X_DATA(y
));
217 iowrite8(CMD_WRITE_BYTE
, dev
->regs
+ SCR24X_CMD_STATUS
);
218 ret
= scr24x_wait_ready(dev
);
225 mutex_unlock(&dev
->lock
);
229 static const struct file_operations scr24x_fops
= {
230 .owner
= THIS_MODULE
,
232 .write
= scr24x_write
,
234 .release
= scr24x_release
,
238 static int scr24x_config_check(struct pcmcia_device
*link
, void *priv_data
)
240 if (resource_size(link
->resource
[PCMCIA_IOPORT_0
]) != 0x11)
242 return pcmcia_request_io(link
);
245 static int scr24x_probe(struct pcmcia_device
*link
)
247 struct scr24x_dev
*dev
;
250 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
254 dev
->devno
= find_first_zero_bit(scr24x_minors
, SCR24X_DEVS
);
255 if (dev
->devno
>= SCR24X_DEVS
) {
260 mutex_init(&dev
->lock
);
261 kref_init(&dev
->refcnt
);
264 link
->config_flags
|= CONF_ENABLE_IRQ
| CONF_AUTO_SET_IO
;
266 ret
= pcmcia_loop_config(link
, scr24x_config_check
, NULL
);
270 dev
->dev
= &link
->dev
;
271 dev
->regs
= devm_ioport_map(&link
->dev
,
272 link
->resource
[PCMCIA_IOPORT_0
]->start
,
273 resource_size(link
->resource
[PCMCIA_IOPORT_0
]));
279 cdev_init(&dev
->c_dev
, &scr24x_fops
);
280 dev
->c_dev
.owner
= THIS_MODULE
;
281 dev
->c_dev
.ops
= &scr24x_fops
;
282 ret
= cdev_add(&dev
->c_dev
, MKDEV(MAJOR(scr24x_devt
), dev
->devno
), 1);
286 ret
= pcmcia_enable_device(link
);
288 pcmcia_disable_device(link
);
292 device_create(scr24x_class
, NULL
, MKDEV(MAJOR(scr24x_devt
), dev
->devno
),
293 NULL
, "scr24x%d", dev
->devno
);
295 dev_info(&link
->dev
, "SCR24x Chip Card Interface\n");
299 if (dev
->devno
< SCR24X_DEVS
)
300 clear_bit(dev
->devno
, scr24x_minors
);
305 static void scr24x_remove(struct pcmcia_device
*link
)
307 struct scr24x_dev
*dev
= (struct scr24x_dev
*)link
->priv
;
309 device_destroy(scr24x_class
, MKDEV(MAJOR(scr24x_devt
), dev
->devno
));
310 mutex_lock(&dev
->lock
);
311 pcmcia_disable_device(link
);
312 cdev_del(&dev
->c_dev
);
313 clear_bit(dev
->devno
, scr24x_minors
);
315 mutex_unlock(&dev
->lock
);
317 kref_put(&dev
->refcnt
, scr24x_delete
);
320 static const struct pcmcia_device_id scr24x_ids
[] = {
321 PCMCIA_DEVICE_PROD_ID12("HP", "PC Card Smart Card Reader",
322 0x53cb94f9, 0xbfdf89a5),
323 PCMCIA_DEVICE_PROD_ID1("SCR241 PCMCIA", 0x6271efa3),
324 PCMCIA_DEVICE_PROD_ID1("SCR243 PCMCIA", 0x2054e8de),
325 PCMCIA_DEVICE_PROD_ID1("SCR24x PCMCIA", 0x54a33665),
328 MODULE_DEVICE_TABLE(pcmcia
, scr24x_ids
);
330 static struct pcmcia_driver scr24x_driver
= {
331 .owner
= THIS_MODULE
,
333 .probe
= scr24x_probe
,
334 .remove
= scr24x_remove
,
335 .id_table
= scr24x_ids
,
338 static int __init
scr24x_init(void)
342 scr24x_class
= class_create(THIS_MODULE
, "scr24x");
343 if (IS_ERR(scr24x_class
))
344 return PTR_ERR(scr24x_class
);
346 ret
= alloc_chrdev_region(&scr24x_devt
, 0, SCR24X_DEVS
, "scr24x");
348 class_destroy(scr24x_class
);
352 ret
= pcmcia_register_driver(&scr24x_driver
);
354 unregister_chrdev_region(scr24x_devt
, SCR24X_DEVS
);
355 class_destroy(scr24x_class
);
361 static void __exit
scr24x_exit(void)
363 pcmcia_unregister_driver(&scr24x_driver
);
364 unregister_chrdev_region(scr24x_devt
, SCR24X_DEVS
);
365 class_destroy(scr24x_class
);
368 module_init(scr24x_init
);
369 module_exit(scr24x_exit
);
371 MODULE_AUTHOR("Lubomir Rintel");
372 MODULE_DESCRIPTION("SCR24x PCMCIA Smart Card Reader Driver");
373 MODULE_LICENSE("GPL");