scr24x_cs.c 8.3 KB

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