scr24x_cs.c 7.6 KB

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