Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

char: pcmcia: remove all the drivers

These char PCMCIA drivers are buggy[1] and receive only minimal care. It
was concluded[2], that we should try to remove most pcmcia drivers
completely. Let's start with these char broken one.

Note that I also removed a UAPI header: include/uapi/linux/cm4000_cs.h.
I found only coccinelle tests mentioning some ioctl constants from that
file. But they are not actually used. Anyway, should someone complain,
we may reintroduce the header (or its parts).

[1] https://lore.kernel.org/all/f41c2765-80e0-48bc-b1e4-8cfd3230fd4a@www.fastmail.com/
[2] https://lore.kernel.org/all/c5b39544-a4fb-4796-a046-0b9be9853787@app.fastmail.com/

Signed-off-by: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Cc: "Hyunwoo Kim" <imv4bel@gmail.com>
Cc: Harald Welte <laforge@gnumonks.org>
Cc: Lubomir Rintel <lkundrak@v3.sk>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Acked-by: Dominik Brodowski <linux@dominikbrodowski.net>
Reviewed-by: Arnd Bergmann <arnd@arndb.de>
Link: https://lore.kernel.org/r/20230222092302.6348-2-jirislaby@kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Jiri Slaby and committed by
Greg Kroah-Hartman
9b12f050 3996954f

-7466
-1
Documentation/process/magic-number.rst
··· 72 72 APM_BIOS_MAGIC 0x4101 apm_user ``arch/x86/kernel/apm_32.c`` 73 73 FASYNC_MAGIC 0x4601 fasync_struct ``include/linux/fs.h`` 74 74 SLIP_MAGIC 0x5302 slip ``drivers/net/slip.h`` 75 - MGSLPC_MAGIC 0x5402 mgslpc_info ``drivers/char/pcmcia/synclink_cs.c`` 76 75 BAYCOM_MAGIC 0x19730510 baycom_state ``drivers/net/baycom_epp.c`` 77 76 HDLCDRV_MAGIC 0x5ac6e778 hdlcdrv_state ``include/linux/hdlcdrv.h`` 78 77 KV_MAGIC 0x5f4b565f kernel_vars_s ``arch/mips/include/asm/sn/klkernvars.h``
-1
Documentation/translations/it_IT/process/magic-number.rst
··· 78 78 APM_BIOS_MAGIC 0x4101 apm_user ``arch/x86/kernel/apm_32.c`` 79 79 FASYNC_MAGIC 0x4601 fasync_struct ``include/linux/fs.h`` 80 80 SLIP_MAGIC 0x5302 slip ``drivers/net/slip.h`` 81 - MGSLPC_MAGIC 0x5402 mgslpc_info ``drivers/char/pcmcia/synclink_cs.c`` 82 81 BAYCOM_MAGIC 0x19730510 baycom_state ``drivers/net/baycom_epp.c`` 83 82 HDLCDRV_MAGIC 0x5ac6e778 hdlcdrv_state ``include/linux/hdlcdrv.h`` 84 83 KV_MAGIC 0x5f4b565f kernel_vars_s ``arch/mips/include/asm/sn/klkernvars.h``
-1
Documentation/translations/sp_SP/process/magic-number.rst
··· 77 77 APM_BIOS_MAGIC 0x4101 apm_user ``arch/x86/kernel/apm_32.c`` 78 78 FASYNC_MAGIC 0x4601 fasync_struct ``include/linux/fs.h`` 79 79 SLIP_MAGIC 0x5302 slip ``drivers/net/slip.h`` 80 - MGSLPC_MAGIC 0x5402 mgslpc_info ``drivers/char/pcmcia/synclink_cs.c`` 81 80 BAYCOM_MAGIC 0x19730510 baycom_state ``drivers/net/baycom_epp.c`` 82 81 HDLCDRV_MAGIC 0x5ac6e778 hdlcdrv_state ``include/linux/hdlcdrv.h`` 83 82 KV_MAGIC 0x5f4b565f kernel_vars_s ``arch/mips/include/asm/sn/klkernvars.h``
-1
Documentation/translations/zh_CN/process/magic-number.rst
··· 61 61 APM_BIOS_MAGIC 0x4101 apm_user ``arch/x86/kernel/apm_32.c`` 62 62 FASYNC_MAGIC 0x4601 fasync_struct ``include/linux/fs.h`` 63 63 SLIP_MAGIC 0x5302 slip ``drivers/net/slip.h`` 64 - MGSLPC_MAGIC 0x5402 mgslpc_info ``drivers/char/pcmcia/synclink_cs.c`` 65 64 BAYCOM_MAGIC 0x19730510 baycom_state ``drivers/net/baycom_epp.c`` 66 65 HDLCDRV_MAGIC 0x5ac6e778 hdlcdrv_state ``include/linux/hdlcdrv.h`` 67 66 KV_MAGIC 0x5f4b565f kernel_vars_s ``arch/mips/include/asm/sn/klkernvars.h``
-1
Documentation/translations/zh_TW/process/magic-number.rst
··· 64 64 APM_BIOS_MAGIC 0x4101 apm_user ``arch/x86/kernel/apm_32.c`` 65 65 FASYNC_MAGIC 0x4601 fasync_struct ``include/linux/fs.h`` 66 66 SLIP_MAGIC 0x5302 slip ``drivers/net/slip.h`` 67 - MGSLPC_MAGIC 0x5402 mgslpc_info ``drivers/char/pcmcia/synclink_cs.c`` 68 67 BAYCOM_MAGIC 0x19730510 baycom_state ``drivers/net/baycom_epp.c`` 69 68 HDLCDRV_MAGIC 0x5ac6e778 hdlcdrv_state ``include/linux/hdlcdrv.h`` 70 69 KV_MAGIC 0x5f4b565f kernel_vars_s ``arch/mips/include/asm/sn/klkernvars.h``
-1
Documentation/userspace-api/ioctl/ioctl-number.rst
··· 222 222 'b' 00-FF conflict! bit3 vme host bridge 223 223 <mailto:natalia@nikhefk.nikhef.nl> 224 224 'b' 00-0F linux/dma-buf.h conflict! 225 - 'c' all linux/cm4000_cs.h conflict! 226 225 'c' 00-7F linux/comstats.h conflict! 227 226 'c' 00-7F linux/coda.h conflict! 228 227 'c' 00-1F linux/chio.h conflict!
-17
MAINTAINERS
··· 15322 15322 F: Documentation/filesystems/omfs.rst 15323 15323 F: fs/omfs/ 15324 15324 15325 - OMNIKEY CARDMAN 4000 DRIVER 15326 - M: Harald Welte <laforge@gnumonks.org> 15327 - S: Maintained 15328 - F: drivers/char/pcmcia/cm4000_cs.c 15329 - F: include/linux/cm4000_cs.h 15330 - F: include/uapi/linux/cm4000_cs.h 15331 - 15332 - OMNIKEY CARDMAN 4040 DRIVER 15333 - M: Harald Welte <laforge@gnumonks.org> 15334 - S: Maintained 15335 - F: drivers/char/pcmcia/cm4040_cs.* 15336 - 15337 15325 OMNIVISION OG01A1B SENSOR DRIVER 15338 15326 M: Shawn Tu <shawnx.tu@intel.com> 15339 15327 L: linux-media@vger.kernel.org ··· 18596 18608 F: include/linux/wait.h 18597 18609 F: include/uapi/linux/sched.h 18598 18610 F: kernel/sched/ 18599 - 18600 - SCR24X CHIP CARD INTERFACE DRIVER 18601 - M: Lubomir Rintel <lkundrak@v3.sk> 18602 - S: Supported 18603 - F: drivers/char/pcmcia/scr24x_cs.c 18604 18611 18605 18612 SCSI RDMA PROTOCOL (SRP) INITIATOR 18606 18613 M: Bart Van Assche <bvanassche@acm.org>
-2
arch/powerpc/configs/ppc6xx_defconfig
··· 614 614 CONFIG_HW_RANDOM_VIRTIO=m 615 615 CONFIG_NVRAM=y 616 616 CONFIG_DTLK=m 617 - CONFIG_CARDMAN_4000=m 618 - CONFIG_CARDMAN_4040=m 619 617 CONFIG_IPWIRELESS=m 620 618 CONFIG_I2C_CHARDEV=m 621 619 CONFIG_I2C_HYDRA=m
-2
drivers/char/Kconfig
··· 247 247 To compile this driver as a module, choose M here: the 248 248 module will be called sonypi. 249 249 250 - source "drivers/char/pcmcia/Kconfig" 251 - 252 250 config MWAVE 253 251 tristate "ACP Modem (Mwave) support" 254 252 depends on X86 && TTY
-1
drivers/char/Makefile
··· 35 35 36 36 obj-$(CONFIG_MWAVE) += mwave/ 37 37 obj-y += agp/ 38 - obj-$(CONFIG_PCMCIA) += pcmcia/ 39 38 40 39 obj-$(CONFIG_HANGCHECK_TIMER) += hangcheck-timer.o 41 40 obj-$(CONFIG_TCG_TPM) += tpm/
-59
drivers/char/pcmcia/Kconfig
··· 1 - # SPDX-License-Identifier: GPL-2.0-only 2 - # 3 - # PCMCIA character device configuration 4 - # 5 - 6 - menu "PCMCIA character devices" 7 - depends on PCMCIA!=n 8 - 9 - config SYNCLINK_CS 10 - tristate "SyncLink PC Card support" 11 - depends on PCMCIA && TTY 12 - help 13 - Enable support for the SyncLink PC Card serial adapter, running 14 - asynchronous and HDLC communications up to 512Kbps. The port is 15 - selectable for RS-232, V.35, RS-449, RS-530, and X.21 16 - 17 - This driver may be built as a module ( = code which can be 18 - inserted in and removed from the running kernel whenever you want). 19 - The module will be called synclink_cs. If you want to do that, say M 20 - here. 21 - 22 - config CARDMAN_4000 23 - tristate "Omnikey Cardman 4000 support" 24 - depends on PCMCIA 25 - select BITREVERSE 26 - help 27 - Enable support for the Omnikey Cardman 4000 PCMCIA Smartcard 28 - reader. 29 - 30 - This kernel driver requires additional userspace support, either 31 - by the vendor-provided PC/SC ifd_handler (http://www.omnikey.com/), 32 - or via the cm4000 backend of OpenCT (http://www.opensc-project.org/opensc). 33 - 34 - config CARDMAN_4040 35 - tristate "Omnikey CardMan 4040 support" 36 - depends on PCMCIA 37 - help 38 - Enable support for the Omnikey CardMan 4040 PCMCIA Smartcard 39 - reader. 40 - 41 - This card is basically a USB CCID device connected to a FIFO 42 - in I/O space. To use the kernel driver, you will need either the 43 - PC/SC ifdhandler provided from the Omnikey homepage 44 - (http://www.omnikey.com/), or a current development version of OpenCT 45 - (http://www.opensc-project.org/opensc). 46 - 47 - config SCR24X 48 - tristate "SCR24x Chip Card Interface support" 49 - depends on PCMCIA 50 - help 51 - Enable support for the SCR24x PCMCIA Chip Card Interface. 52 - 53 - To compile this driver as a module, choose M here. 54 - The module will be called scr24x_cs.. 55 - 56 - If unsure say N. 57 - 58 - endmenu 59 -
-11
drivers/char/pcmcia/Makefile
··· 1 - # SPDX-License-Identifier: GPL-2.0-only 2 - # 3 - # drivers/char/pcmcia/Makefile 4 - # 5 - # Makefile for the Linux PCMCIA char device drivers. 6 - # 7 - 8 - obj-$(CONFIG_SYNCLINK_CS) += synclink_cs.o 9 - obj-$(CONFIG_CARDMAN_4000) += cm4000_cs.o 10 - obj-$(CONFIG_CARDMAN_4040) += cm4040_cs.o 11 - obj-$(CONFIG_SCR24X) += scr24x_cs.o
-1912
drivers/char/pcmcia/cm4000_cs.c
··· 1 - /* 2 - * A driver for the PCMCIA Smartcard Reader "Omnikey CardMan Mobile 4000" 3 - * 4 - * cm4000_cs.c support.linux@omnikey.com 5 - * 6 - * Tue Oct 23 11:32:43 GMT 2001 herp - cleaned up header files 7 - * Sun Jan 20 10:11:15 MET 2002 herp - added modversion header files 8 - * Thu Nov 14 16:34:11 GMT 2002 mh - added PPS functionality 9 - * Tue Nov 19 16:36:27 GMT 2002 mh - added SUSPEND/RESUME functionailty 10 - * Wed Jul 28 12:55:01 CEST 2004 mh - kernel 2.6 adjustments 11 - * 12 - * current version: 2.4.0gm4 13 - * 14 - * (C) 2000,2001,2002,2003,2004 Omnikey AG 15 - * 16 - * (C) 2005-2006 Harald Welte <laforge@gnumonks.org> 17 - * - Adhere to Kernel process/coding-style.rst 18 - * - Port to 2.6.13 "new" style PCMCIA 19 - * - Check for copy_{from,to}_user return values 20 - * - Use nonseekable_open() 21 - * - add class interface for udev device creation 22 - * 23 - * All rights reserved. Licensed under dual BSD/GPL license. 24 - */ 25 - 26 - #include <linux/kernel.h> 27 - #include <linux/module.h> 28 - #include <linux/slab.h> 29 - #include <linux/init.h> 30 - #include <linux/fs.h> 31 - #include <linux/delay.h> 32 - #include <linux/bitrev.h> 33 - #include <linux/mutex.h> 34 - #include <linux/uaccess.h> 35 - #include <linux/io.h> 36 - 37 - #include <pcmcia/cistpl.h> 38 - #include <pcmcia/cisreg.h> 39 - #include <pcmcia/ciscode.h> 40 - #include <pcmcia/ds.h> 41 - 42 - #include <linux/cm4000_cs.h> 43 - 44 - /* #define ATR_CSUM */ 45 - 46 - #define reader_to_dev(x) (&x->p_dev->dev) 47 - 48 - /* n (debug level) is ignored */ 49 - /* additional debug output may be enabled by re-compiling with 50 - * CM4000_DEBUG set */ 51 - /* #define CM4000_DEBUG */ 52 - #define DEBUGP(n, rdr, x, args...) do { \ 53 - dev_dbg(reader_to_dev(rdr), "%s:" x, \ 54 - __func__ , ## args); \ 55 - } while (0) 56 - 57 - static DEFINE_MUTEX(cmm_mutex); 58 - 59 - #define T_1SEC (HZ) 60 - #define T_10MSEC msecs_to_jiffies(10) 61 - #define T_20MSEC msecs_to_jiffies(20) 62 - #define T_40MSEC msecs_to_jiffies(40) 63 - #define T_50MSEC msecs_to_jiffies(50) 64 - #define T_100MSEC msecs_to_jiffies(100) 65 - #define T_500MSEC msecs_to_jiffies(500) 66 - 67 - static void cm4000_release(struct pcmcia_device *link); 68 - 69 - static int major; /* major number we get from the kernel */ 70 - 71 - /* note: the first state has to have number 0 always */ 72 - 73 - #define M_FETCH_ATR 0 74 - #define M_TIMEOUT_WAIT 1 75 - #define M_READ_ATR_LEN 2 76 - #define M_READ_ATR 3 77 - #define M_ATR_PRESENT 4 78 - #define M_BAD_CARD 5 79 - #define M_CARDOFF 6 80 - 81 - #define LOCK_IO 0 82 - #define LOCK_MONITOR 1 83 - 84 - #define IS_AUTOPPS_ACT 6 85 - #define IS_PROCBYTE_PRESENT 7 86 - #define IS_INVREV 8 87 - #define IS_ANY_T0 9 88 - #define IS_ANY_T1 10 89 - #define IS_ATR_PRESENT 11 90 - #define IS_ATR_VALID 12 91 - #define IS_CMM_ABSENT 13 92 - #define IS_BAD_LENGTH 14 93 - #define IS_BAD_CSUM 15 94 - #define IS_BAD_CARD 16 95 - 96 - #define REG_FLAGS0(x) (x + 0) 97 - #define REG_FLAGS1(x) (x + 1) 98 - #define REG_NUM_BYTES(x) (x + 2) 99 - #define REG_BUF_ADDR(x) (x + 3) 100 - #define REG_BUF_DATA(x) (x + 4) 101 - #define REG_NUM_SEND(x) (x + 5) 102 - #define REG_BAUDRATE(x) (x + 6) 103 - #define REG_STOPBITS(x) (x + 7) 104 - 105 - struct cm4000_dev { 106 - struct pcmcia_device *p_dev; 107 - 108 - unsigned char atr[MAX_ATR]; 109 - unsigned char rbuf[512]; 110 - unsigned char sbuf[512]; 111 - 112 - wait_queue_head_t devq; /* when removing cardman must not be 113 - zeroed! */ 114 - 115 - wait_queue_head_t ioq; /* if IO is locked, wait on this Q */ 116 - wait_queue_head_t atrq; /* wait for ATR valid */ 117 - wait_queue_head_t readq; /* used by write to wake blk.read */ 118 - 119 - /* warning: do not move this struct group. 120 - * initialising to zero depends on it - see ZERO_DEV below. */ 121 - struct_group(init, 122 - unsigned char atr_csum; 123 - unsigned char atr_len_retry; 124 - unsigned short atr_len; 125 - unsigned short rlen; /* bytes avail. after write */ 126 - unsigned short rpos; /* latest read pos. write zeroes */ 127 - unsigned char procbyte; /* T=0 procedure byte */ 128 - unsigned char mstate; /* state of card monitor */ 129 - unsigned char cwarn; /* slow down warning */ 130 - unsigned char flags0; /* cardman IO-flags 0 */ 131 - unsigned char flags1; /* cardman IO-flags 1 */ 132 - unsigned int mdelay; /* variable monitor speeds, in jiffies */ 133 - 134 - unsigned int baudv; /* baud value for speed */ 135 - unsigned char ta1; 136 - unsigned char proto; /* T=0, T=1, ... */ 137 - unsigned long flags; /* lock+flags (MONITOR,IO,ATR) * for concurrent 138 - access */ 139 - 140 - unsigned char pts[4]; 141 - 142 - struct timer_list timer; /* used to keep monitor running */ 143 - int monitor_running; 144 - ); 145 - }; 146 - 147 - #define ZERO_DEV(dev) memset(&((dev)->init), 0, sizeof((dev)->init)) 148 - 149 - static struct pcmcia_device *dev_table[CM4000_MAX_DEV]; 150 - static struct class *cmm_class; 151 - 152 - /* This table doesn't use spaces after the comma between fields and thus 153 - * violates process/coding-style.rst. However, I don't really think wrapping it around will 154 - * make it any clearer to read -HW */ 155 - static unsigned char fi_di_table[10][14] = { 156 - /*FI 00 01 02 03 04 05 06 07 08 09 10 11 12 13 */ 157 - /*DI */ 158 - /* 0 */ {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11}, 159 - /* 1 */ {0x01,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x91,0x11,0x11,0x11,0x11}, 160 - /* 2 */ {0x02,0x12,0x22,0x32,0x11,0x11,0x11,0x11,0x11,0x92,0xA2,0xB2,0x11,0x11}, 161 - /* 3 */ {0x03,0x13,0x23,0x33,0x43,0x53,0x63,0x11,0x11,0x93,0xA3,0xB3,0xC3,0xD3}, 162 - /* 4 */ {0x04,0x14,0x24,0x34,0x44,0x54,0x64,0x11,0x11,0x94,0xA4,0xB4,0xC4,0xD4}, 163 - /* 5 */ {0x00,0x15,0x25,0x35,0x45,0x55,0x65,0x11,0x11,0x95,0xA5,0xB5,0xC5,0xD5}, 164 - /* 6 */ {0x06,0x16,0x26,0x36,0x46,0x56,0x66,0x11,0x11,0x96,0xA6,0xB6,0xC6,0xD6}, 165 - /* 7 */ {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11}, 166 - /* 8 */ {0x08,0x11,0x28,0x38,0x48,0x58,0x68,0x11,0x11,0x98,0xA8,0xB8,0xC8,0xD8}, 167 - /* 9 */ {0x09,0x19,0x29,0x39,0x49,0x59,0x69,0x11,0x11,0x99,0xA9,0xB9,0xC9,0xD9} 168 - }; 169 - 170 - #ifndef CM4000_DEBUG 171 - #define xoutb outb 172 - #define xinb inb 173 - #else 174 - static inline void xoutb(unsigned char val, unsigned short port) 175 - { 176 - pr_debug("outb(val=%.2x,port=%.4x)\n", val, port); 177 - outb(val, port); 178 - } 179 - static inline unsigned char xinb(unsigned short port) 180 - { 181 - unsigned char val; 182 - 183 - val = inb(port); 184 - pr_debug("%.2x=inb(%.4x)\n", val, port); 185 - 186 - return val; 187 - } 188 - #endif 189 - 190 - static inline unsigned char invert_revert(unsigned char ch) 191 - { 192 - return bitrev8(~ch); 193 - } 194 - 195 - static void str_invert_revert(unsigned char *b, int len) 196 - { 197 - int i; 198 - 199 - for (i = 0; i < len; i++) 200 - b[i] = invert_revert(b[i]); 201 - } 202 - 203 - #define ATRLENCK(dev,pos) \ 204 - if (pos>=dev->atr_len || pos>=MAX_ATR) \ 205 - goto return_0; 206 - 207 - static unsigned int calc_baudv(unsigned char fidi) 208 - { 209 - unsigned int wcrcf, wbrcf, fi_rfu, di_rfu; 210 - 211 - fi_rfu = 372; 212 - di_rfu = 1; 213 - 214 - /* FI */ 215 - switch ((fidi >> 4) & 0x0F) { 216 - case 0x00: 217 - wcrcf = 372; 218 - break; 219 - case 0x01: 220 - wcrcf = 372; 221 - break; 222 - case 0x02: 223 - wcrcf = 558; 224 - break; 225 - case 0x03: 226 - wcrcf = 744; 227 - break; 228 - case 0x04: 229 - wcrcf = 1116; 230 - break; 231 - case 0x05: 232 - wcrcf = 1488; 233 - break; 234 - case 0x06: 235 - wcrcf = 1860; 236 - break; 237 - case 0x07: 238 - wcrcf = fi_rfu; 239 - break; 240 - case 0x08: 241 - wcrcf = fi_rfu; 242 - break; 243 - case 0x09: 244 - wcrcf = 512; 245 - break; 246 - case 0x0A: 247 - wcrcf = 768; 248 - break; 249 - case 0x0B: 250 - wcrcf = 1024; 251 - break; 252 - case 0x0C: 253 - wcrcf = 1536; 254 - break; 255 - case 0x0D: 256 - wcrcf = 2048; 257 - break; 258 - default: 259 - wcrcf = fi_rfu; 260 - break; 261 - } 262 - 263 - /* DI */ 264 - switch (fidi & 0x0F) { 265 - case 0x00: 266 - wbrcf = di_rfu; 267 - break; 268 - case 0x01: 269 - wbrcf = 1; 270 - break; 271 - case 0x02: 272 - wbrcf = 2; 273 - break; 274 - case 0x03: 275 - wbrcf = 4; 276 - break; 277 - case 0x04: 278 - wbrcf = 8; 279 - break; 280 - case 0x05: 281 - wbrcf = 16; 282 - break; 283 - case 0x06: 284 - wbrcf = 32; 285 - break; 286 - case 0x07: 287 - wbrcf = di_rfu; 288 - break; 289 - case 0x08: 290 - wbrcf = 12; 291 - break; 292 - case 0x09: 293 - wbrcf = 20; 294 - break; 295 - default: 296 - wbrcf = di_rfu; 297 - break; 298 - } 299 - 300 - return (wcrcf / wbrcf); 301 - } 302 - 303 - static unsigned short io_read_num_rec_bytes(unsigned int iobase, 304 - unsigned short *s) 305 - { 306 - unsigned short tmp; 307 - 308 - tmp = *s = 0; 309 - do { 310 - *s = tmp; 311 - tmp = inb(REG_NUM_BYTES(iobase)) | 312 - (inb(REG_FLAGS0(iobase)) & 4 ? 0x100 : 0); 313 - } while (tmp != *s); 314 - 315 - return *s; 316 - } 317 - 318 - static int parse_atr(struct cm4000_dev *dev) 319 - { 320 - unsigned char any_t1, any_t0; 321 - unsigned char ch, ifno; 322 - int ix, done; 323 - 324 - DEBUGP(3, dev, "-> parse_atr: dev->atr_len = %i\n", dev->atr_len); 325 - 326 - if (dev->atr_len < 3) { 327 - DEBUGP(5, dev, "parse_atr: atr_len < 3\n"); 328 - return 0; 329 - } 330 - 331 - if (dev->atr[0] == 0x3f) 332 - set_bit(IS_INVREV, &dev->flags); 333 - else 334 - clear_bit(IS_INVREV, &dev->flags); 335 - ix = 1; 336 - ifno = 1; 337 - ch = dev->atr[1]; 338 - dev->proto = 0; /* XXX PROTO */ 339 - any_t1 = any_t0 = done = 0; 340 - dev->ta1 = 0x11; /* defaults to 9600 baud */ 341 - do { 342 - if (ifno == 1 && (ch & 0x10)) { 343 - /* read first interface byte and TA1 is present */ 344 - dev->ta1 = dev->atr[2]; 345 - DEBUGP(5, dev, "Card says FiDi is 0x%.2x\n", dev->ta1); 346 - ifno++; 347 - } else if ((ifno == 2) && (ch & 0x10)) { /* TA(2) */ 348 - dev->ta1 = 0x11; 349 - ifno++; 350 - } 351 - 352 - DEBUGP(5, dev, "Yi=%.2x\n", ch & 0xf0); 353 - ix += ((ch & 0x10) >> 4) /* no of int.face chars */ 354 - +((ch & 0x20) >> 5) 355 - + ((ch & 0x40) >> 6) 356 - + ((ch & 0x80) >> 7); 357 - /* ATRLENCK(dev,ix); */ 358 - if (ch & 0x80) { /* TDi */ 359 - ch = dev->atr[ix]; 360 - if ((ch & 0x0f)) { 361 - any_t1 = 1; 362 - DEBUGP(5, dev, "card is capable of T=1\n"); 363 - } else { 364 - any_t0 = 1; 365 - DEBUGP(5, dev, "card is capable of T=0\n"); 366 - } 367 - } else 368 - done = 1; 369 - } while (!done); 370 - 371 - DEBUGP(5, dev, "ix=%d noHist=%d any_t1=%d\n", 372 - ix, dev->atr[1] & 15, any_t1); 373 - if (ix + 1 + (dev->atr[1] & 0x0f) + any_t1 != dev->atr_len) { 374 - DEBUGP(5, dev, "length error\n"); 375 - return 0; 376 - } 377 - if (any_t0) 378 - set_bit(IS_ANY_T0, &dev->flags); 379 - 380 - if (any_t1) { /* compute csum */ 381 - dev->atr_csum = 0; 382 - #ifdef ATR_CSUM 383 - for (i = 1; i < dev->atr_len; i++) 384 - dev->atr_csum ^= dev->atr[i]; 385 - if (dev->atr_csum) { 386 - set_bit(IS_BAD_CSUM, &dev->flags); 387 - DEBUGP(5, dev, "bad checksum\n"); 388 - goto return_0; 389 - } 390 - #endif 391 - if (any_t0 == 0) 392 - dev->proto = 1; /* XXX PROTO */ 393 - set_bit(IS_ANY_T1, &dev->flags); 394 - } 395 - 396 - return 1; 397 - } 398 - 399 - struct card_fixup { 400 - char atr[12]; 401 - u_int8_t atr_len; 402 - u_int8_t stopbits; 403 - }; 404 - 405 - static struct card_fixup card_fixups[] = { 406 - { /* ACOS */ 407 - .atr = { 0x3b, 0xb3, 0x11, 0x00, 0x00, 0x41, 0x01 }, 408 - .atr_len = 7, 409 - .stopbits = 0x03, 410 - }, 411 - { /* Motorola */ 412 - .atr = {0x3b, 0x76, 0x13, 0x00, 0x00, 0x80, 0x62, 0x07, 413 - 0x41, 0x81, 0x81 }, 414 - .atr_len = 11, 415 - .stopbits = 0x04, 416 - }, 417 - }; 418 - 419 - static void set_cardparameter(struct cm4000_dev *dev) 420 - { 421 - int i; 422 - unsigned int iobase = dev->p_dev->resource[0]->start; 423 - u_int8_t stopbits = 0x02; /* ISO default */ 424 - 425 - DEBUGP(3, dev, "-> set_cardparameter\n"); 426 - 427 - dev->flags1 = dev->flags1 | (((dev->baudv - 1) & 0x0100) >> 8); 428 - xoutb(dev->flags1, REG_FLAGS1(iobase)); 429 - DEBUGP(5, dev, "flags1 = 0x%02x\n", dev->flags1); 430 - 431 - /* set baudrate */ 432 - xoutb((unsigned char)((dev->baudv - 1) & 0xFF), REG_BAUDRATE(iobase)); 433 - 434 - DEBUGP(5, dev, "baudv = %i -> write 0x%02x\n", dev->baudv, 435 - ((dev->baudv - 1) & 0xFF)); 436 - 437 - /* set stopbits */ 438 - for (i = 0; i < ARRAY_SIZE(card_fixups); i++) { 439 - if (!memcmp(dev->atr, card_fixups[i].atr, 440 - card_fixups[i].atr_len)) 441 - stopbits = card_fixups[i].stopbits; 442 - } 443 - xoutb(stopbits, REG_STOPBITS(iobase)); 444 - 445 - DEBUGP(3, dev, "<- set_cardparameter\n"); 446 - } 447 - 448 - static int set_protocol(struct cm4000_dev *dev, struct ptsreq *ptsreq) 449 - { 450 - 451 - unsigned long tmp, i; 452 - unsigned short num_bytes_read; 453 - unsigned char pts_reply[4]; 454 - ssize_t rc; 455 - unsigned int iobase = dev->p_dev->resource[0]->start; 456 - 457 - rc = 0; 458 - 459 - DEBUGP(3, dev, "-> set_protocol\n"); 460 - DEBUGP(5, dev, "ptsreq->Protocol = 0x%.8x, ptsreq->Flags=0x%.8x, " 461 - "ptsreq->pts1=0x%.2x, ptsreq->pts2=0x%.2x, " 462 - "ptsreq->pts3=0x%.2x\n", (unsigned int)ptsreq->protocol, 463 - (unsigned int)ptsreq->flags, ptsreq->pts1, ptsreq->pts2, 464 - ptsreq->pts3); 465 - 466 - /* Fill PTS structure */ 467 - dev->pts[0] = 0xff; 468 - dev->pts[1] = 0x00; 469 - tmp = ptsreq->protocol; 470 - while ((tmp = (tmp >> 1)) > 0) 471 - dev->pts[1]++; 472 - dev->proto = dev->pts[1]; /* Set new protocol */ 473 - dev->pts[1] = (0x01 << 4) | (dev->pts[1]); 474 - 475 - /* Correct Fi/Di according to CM4000 Fi/Di table */ 476 - DEBUGP(5, dev, "Ta(1) from ATR is 0x%.2x\n", dev->ta1); 477 - /* set Fi/Di according to ATR TA(1) */ 478 - dev->pts[2] = fi_di_table[dev->ta1 & 0x0F][(dev->ta1 >> 4) & 0x0F]; 479 - 480 - /* Calculate PCK character */ 481 - dev->pts[3] = dev->pts[0] ^ dev->pts[1] ^ dev->pts[2]; 482 - 483 - DEBUGP(5, dev, "pts0=%.2x, pts1=%.2x, pts2=%.2x, pts3=%.2x\n", 484 - dev->pts[0], dev->pts[1], dev->pts[2], dev->pts[3]); 485 - 486 - /* check card convention */ 487 - if (test_bit(IS_INVREV, &dev->flags)) 488 - str_invert_revert(dev->pts, 4); 489 - 490 - /* reset SM */ 491 - xoutb(0x80, REG_FLAGS0(iobase)); 492 - 493 - /* Enable access to the message buffer */ 494 - DEBUGP(5, dev, "Enable access to the messages buffer\n"); 495 - dev->flags1 = 0x20 /* T_Active */ 496 - | (test_bit(IS_INVREV, &dev->flags) ? 0x02 : 0x00) /* inv parity */ 497 - | ((dev->baudv >> 8) & 0x01); /* MSB-baud */ 498 - xoutb(dev->flags1, REG_FLAGS1(iobase)); 499 - 500 - DEBUGP(5, dev, "Enable message buffer -> flags1 = 0x%.2x\n", 501 - dev->flags1); 502 - 503 - /* write challenge to the buffer */ 504 - DEBUGP(5, dev, "Write challenge to buffer: "); 505 - for (i = 0; i < 4; i++) { 506 - xoutb(i, REG_BUF_ADDR(iobase)); 507 - xoutb(dev->pts[i], REG_BUF_DATA(iobase)); /* buf data */ 508 - #ifdef CM4000_DEBUG 509 - pr_debug("0x%.2x ", dev->pts[i]); 510 - } 511 - pr_debug("\n"); 512 - #else 513 - } 514 - #endif 515 - 516 - /* set number of bytes to write */ 517 - DEBUGP(5, dev, "Set number of bytes to write\n"); 518 - xoutb(0x04, REG_NUM_SEND(iobase)); 519 - 520 - /* Trigger CARDMAN CONTROLLER */ 521 - xoutb(0x50, REG_FLAGS0(iobase)); 522 - 523 - /* Monitor progress */ 524 - /* wait for xmit done */ 525 - DEBUGP(5, dev, "Waiting for NumRecBytes getting valid\n"); 526 - 527 - for (i = 0; i < 100; i++) { 528 - if (inb(REG_FLAGS0(iobase)) & 0x08) { 529 - DEBUGP(5, dev, "NumRecBytes is valid\n"); 530 - break; 531 - } 532 - /* can not sleep as this is in atomic context */ 533 - mdelay(10); 534 - } 535 - if (i == 100) { 536 - DEBUGP(5, dev, "Timeout waiting for NumRecBytes getting " 537 - "valid\n"); 538 - rc = -EIO; 539 - goto exit_setprotocol; 540 - } 541 - 542 - DEBUGP(5, dev, "Reading NumRecBytes\n"); 543 - for (i = 0; i < 100; i++) { 544 - io_read_num_rec_bytes(iobase, &num_bytes_read); 545 - if (num_bytes_read >= 4) { 546 - DEBUGP(2, dev, "NumRecBytes = %i\n", num_bytes_read); 547 - if (num_bytes_read > 4) { 548 - rc = -EIO; 549 - goto exit_setprotocol; 550 - } 551 - break; 552 - } 553 - /* can not sleep as this is in atomic context */ 554 - mdelay(10); 555 - } 556 - 557 - /* check whether it is a short PTS reply? */ 558 - if (num_bytes_read == 3) 559 - i = 0; 560 - 561 - if (i == 100) { 562 - DEBUGP(5, dev, "Timeout reading num_bytes_read\n"); 563 - rc = -EIO; 564 - goto exit_setprotocol; 565 - } 566 - 567 - DEBUGP(5, dev, "Reset the CARDMAN CONTROLLER\n"); 568 - xoutb(0x80, REG_FLAGS0(iobase)); 569 - 570 - /* Read PPS reply */ 571 - DEBUGP(5, dev, "Read PPS reply\n"); 572 - for (i = 0; i < num_bytes_read; i++) { 573 - xoutb(i, REG_BUF_ADDR(iobase)); 574 - pts_reply[i] = inb(REG_BUF_DATA(iobase)); 575 - } 576 - 577 - #ifdef CM4000_DEBUG 578 - DEBUGP(2, dev, "PTSreply: "); 579 - for (i = 0; i < num_bytes_read; i++) { 580 - pr_debug("0x%.2x ", pts_reply[i]); 581 - } 582 - pr_debug("\n"); 583 - #endif /* CM4000_DEBUG */ 584 - 585 - DEBUGP(5, dev, "Clear Tactive in Flags1\n"); 586 - xoutb(0x20, REG_FLAGS1(iobase)); 587 - 588 - /* Compare ptsreq and ptsreply */ 589 - if ((dev->pts[0] == pts_reply[0]) && 590 - (dev->pts[1] == pts_reply[1]) && 591 - (dev->pts[2] == pts_reply[2]) && (dev->pts[3] == pts_reply[3])) { 592 - /* setcardparameter according to PPS */ 593 - dev->baudv = calc_baudv(dev->pts[2]); 594 - set_cardparameter(dev); 595 - } else if ((dev->pts[0] == pts_reply[0]) && 596 - ((dev->pts[1] & 0xef) == pts_reply[1]) && 597 - ((pts_reply[0] ^ pts_reply[1]) == pts_reply[2])) { 598 - /* short PTS reply, set card parameter to default values */ 599 - dev->baudv = calc_baudv(0x11); 600 - set_cardparameter(dev); 601 - } else 602 - rc = -EIO; 603 - 604 - exit_setprotocol: 605 - DEBUGP(3, dev, "<- set_protocol\n"); 606 - return rc; 607 - } 608 - 609 - static int io_detect_cm4000(unsigned int iobase, struct cm4000_dev *dev) 610 - { 611 - 612 - /* note: statemachine is assumed to be reset */ 613 - if (inb(REG_FLAGS0(iobase)) & 8) { 614 - clear_bit(IS_ATR_VALID, &dev->flags); 615 - set_bit(IS_CMM_ABSENT, &dev->flags); 616 - return 0; /* detect CMM = 1 -> failure */ 617 - } 618 - /* xoutb(0x40, REG_FLAGS1(iobase)); detectCMM */ 619 - xoutb(dev->flags1 | 0x40, REG_FLAGS1(iobase)); 620 - if ((inb(REG_FLAGS0(iobase)) & 8) == 0) { 621 - clear_bit(IS_ATR_VALID, &dev->flags); 622 - set_bit(IS_CMM_ABSENT, &dev->flags); 623 - return 0; /* detect CMM=0 -> failure */ 624 - } 625 - /* clear detectCMM again by restoring original flags1 */ 626 - xoutb(dev->flags1, REG_FLAGS1(iobase)); 627 - return 1; 628 - } 629 - 630 - static void terminate_monitor(struct cm4000_dev *dev) 631 - { 632 - 633 - /* tell the monitor to stop and wait until 634 - * it terminates. 635 - */ 636 - DEBUGP(3, dev, "-> terminate_monitor\n"); 637 - wait_event_interruptible(dev->devq, 638 - test_and_set_bit(LOCK_MONITOR, 639 - (void *)&dev->flags)); 640 - 641 - /* now, LOCK_MONITOR has been set. 642 - * allow a last cycle in the monitor. 643 - * the monitor will indicate that it has 644 - * finished by clearing this bit. 645 - */ 646 - DEBUGP(5, dev, "Now allow last cycle of monitor!\n"); 647 - while (test_bit(LOCK_MONITOR, (void *)&dev->flags)) 648 - msleep(25); 649 - 650 - DEBUGP(5, dev, "Delete timer\n"); 651 - del_timer_sync(&dev->timer); 652 - #ifdef CM4000_DEBUG 653 - dev->monitor_running = 0; 654 - #endif 655 - 656 - DEBUGP(3, dev, "<- terminate_monitor\n"); 657 - } 658 - 659 - /* 660 - * monitor the card every 50msec. as a side-effect, retrieve the 661 - * atr once a card is inserted. another side-effect of retrieving the 662 - * atr is that the card will be powered on, so there is no need to 663 - * power on the card explicitly from the application: the driver 664 - * is already doing that for you. 665 - */ 666 - 667 - static void monitor_card(struct timer_list *t) 668 - { 669 - struct cm4000_dev *dev = from_timer(dev, t, timer); 670 - unsigned int iobase = dev->p_dev->resource[0]->start; 671 - unsigned short s; 672 - struct ptsreq ptsreq; 673 - int i, atrc; 674 - 675 - DEBUGP(7, dev, "-> monitor_card\n"); 676 - 677 - /* if someone has set the lock for us: we're done! */ 678 - if (test_and_set_bit(LOCK_MONITOR, &dev->flags)) { 679 - DEBUGP(4, dev, "About to stop monitor\n"); 680 - /* no */ 681 - dev->rlen = 682 - dev->rpos = 683 - dev->atr_csum = dev->atr_len_retry = dev->cwarn = 0; 684 - dev->mstate = M_FETCH_ATR; 685 - clear_bit(LOCK_MONITOR, &dev->flags); 686 - /* close et al. are sleeping on devq, so wake it */ 687 - wake_up_interruptible(&dev->devq); 688 - DEBUGP(2, dev, "<- monitor_card (we are done now)\n"); 689 - return; 690 - } 691 - 692 - /* try to lock io: if it is already locked, just add another timer */ 693 - if (test_and_set_bit(LOCK_IO, (void *)&dev->flags)) { 694 - DEBUGP(4, dev, "Couldn't get IO lock\n"); 695 - goto return_with_timer; 696 - } 697 - 698 - /* is a card/a reader inserted at all ? */ 699 - dev->flags0 = xinb(REG_FLAGS0(iobase)); 700 - DEBUGP(7, dev, "dev->flags0 = 0x%2x\n", dev->flags0); 701 - DEBUGP(7, dev, "smartcard present: %s\n", 702 - dev->flags0 & 1 ? "yes" : "no"); 703 - DEBUGP(7, dev, "cardman present: %s\n", 704 - dev->flags0 == 0xff ? "no" : "yes"); 705 - 706 - if ((dev->flags0 & 1) == 0 /* no smartcard inserted */ 707 - || dev->flags0 == 0xff) { /* no cardman inserted */ 708 - /* no */ 709 - dev->rlen = 710 - dev->rpos = 711 - dev->atr_csum = dev->atr_len_retry = dev->cwarn = 0; 712 - dev->mstate = M_FETCH_ATR; 713 - 714 - dev->flags &= 0x000000ff; /* only keep IO and MONITOR locks */ 715 - 716 - if (dev->flags0 == 0xff) { 717 - DEBUGP(4, dev, "set IS_CMM_ABSENT bit\n"); 718 - set_bit(IS_CMM_ABSENT, &dev->flags); 719 - } else if (test_bit(IS_CMM_ABSENT, &dev->flags)) { 720 - DEBUGP(4, dev, "clear IS_CMM_ABSENT bit " 721 - "(card is removed)\n"); 722 - clear_bit(IS_CMM_ABSENT, &dev->flags); 723 - } 724 - 725 - goto release_io; 726 - } else if ((dev->flags0 & 1) && test_bit(IS_CMM_ABSENT, &dev->flags)) { 727 - /* cardman and card present but cardman was absent before 728 - * (after suspend with inserted card) */ 729 - DEBUGP(4, dev, "clear IS_CMM_ABSENT bit (card is inserted)\n"); 730 - clear_bit(IS_CMM_ABSENT, &dev->flags); 731 - } 732 - 733 - if (test_bit(IS_ATR_VALID, &dev->flags) == 1) { 734 - DEBUGP(7, dev, "believe ATR is already valid (do nothing)\n"); 735 - goto release_io; 736 - } 737 - 738 - switch (dev->mstate) { 739 - case M_CARDOFF: { 740 - unsigned char flags0; 741 - 742 - DEBUGP(4, dev, "M_CARDOFF\n"); 743 - flags0 = inb(REG_FLAGS0(iobase)); 744 - if (flags0 & 0x02) { 745 - /* wait until Flags0 indicate power is off */ 746 - dev->mdelay = T_10MSEC; 747 - } else { 748 - /* Flags0 indicate power off and no card inserted now; 749 - * Reset CARDMAN CONTROLLER */ 750 - xoutb(0x80, REG_FLAGS0(iobase)); 751 - 752 - /* prepare for fetching ATR again: after card off ATR 753 - * is read again automatically */ 754 - dev->rlen = 755 - dev->rpos = 756 - dev->atr_csum = 757 - dev->atr_len_retry = dev->cwarn = 0; 758 - dev->mstate = M_FETCH_ATR; 759 - 760 - /* minimal gap between CARDOFF and read ATR is 50msec */ 761 - dev->mdelay = T_50MSEC; 762 - } 763 - break; 764 - } 765 - case M_FETCH_ATR: 766 - DEBUGP(4, dev, "M_FETCH_ATR\n"); 767 - xoutb(0x80, REG_FLAGS0(iobase)); 768 - DEBUGP(4, dev, "Reset BAUDV to 9600\n"); 769 - dev->baudv = 0x173; /* 9600 */ 770 - xoutb(0x02, REG_STOPBITS(iobase)); /* stopbits=2 */ 771 - xoutb(0x73, REG_BAUDRATE(iobase)); /* baud value */ 772 - xoutb(0x21, REG_FLAGS1(iobase)); /* T_Active=1, baud 773 - value */ 774 - /* warm start vs. power on: */ 775 - xoutb(dev->flags0 & 2 ? 0x46 : 0x44, REG_FLAGS0(iobase)); 776 - dev->mdelay = T_40MSEC; 777 - dev->mstate = M_TIMEOUT_WAIT; 778 - break; 779 - case M_TIMEOUT_WAIT: 780 - DEBUGP(4, dev, "M_TIMEOUT_WAIT\n"); 781 - /* numRecBytes */ 782 - io_read_num_rec_bytes(iobase, &dev->atr_len); 783 - dev->mdelay = T_10MSEC; 784 - dev->mstate = M_READ_ATR_LEN; 785 - break; 786 - case M_READ_ATR_LEN: 787 - DEBUGP(4, dev, "M_READ_ATR_LEN\n"); 788 - /* infinite loop possible, since there is no timeout */ 789 - 790 - #define MAX_ATR_LEN_RETRY 100 791 - 792 - if (dev->atr_len == io_read_num_rec_bytes(iobase, &s)) { 793 - if (dev->atr_len_retry++ >= MAX_ATR_LEN_RETRY) { /* + XX msec */ 794 - dev->mdelay = T_10MSEC; 795 - dev->mstate = M_READ_ATR; 796 - } 797 - } else { 798 - dev->atr_len = s; 799 - dev->atr_len_retry = 0; /* set new timeout */ 800 - } 801 - 802 - DEBUGP(4, dev, "Current ATR_LEN = %i\n", dev->atr_len); 803 - break; 804 - case M_READ_ATR: 805 - DEBUGP(4, dev, "M_READ_ATR\n"); 806 - xoutb(0x80, REG_FLAGS0(iobase)); /* reset SM */ 807 - for (i = 0; i < dev->atr_len; i++) { 808 - xoutb(i, REG_BUF_ADDR(iobase)); 809 - dev->atr[i] = inb(REG_BUF_DATA(iobase)); 810 - } 811 - /* Deactivate T_Active flags */ 812 - DEBUGP(4, dev, "Deactivate T_Active flags\n"); 813 - dev->flags1 = 0x01; 814 - xoutb(dev->flags1, REG_FLAGS1(iobase)); 815 - 816 - /* atr is present (which doesn't mean it's valid) */ 817 - set_bit(IS_ATR_PRESENT, &dev->flags); 818 - if (dev->atr[0] == 0x03) 819 - str_invert_revert(dev->atr, dev->atr_len); 820 - atrc = parse_atr(dev); 821 - if (atrc == 0) { /* atr invalid */ 822 - dev->mdelay = 0; 823 - dev->mstate = M_BAD_CARD; 824 - } else { 825 - dev->mdelay = T_50MSEC; 826 - dev->mstate = M_ATR_PRESENT; 827 - set_bit(IS_ATR_VALID, &dev->flags); 828 - } 829 - 830 - if (test_bit(IS_ATR_VALID, &dev->flags) == 1) { 831 - DEBUGP(4, dev, "monitor_card: ATR valid\n"); 832 - /* if ta1 == 0x11, no PPS necessary (default values) */ 833 - /* do not do PPS with multi protocol cards */ 834 - if ((test_bit(IS_AUTOPPS_ACT, &dev->flags) == 0) && 835 - (dev->ta1 != 0x11) && 836 - !(test_bit(IS_ANY_T0, &dev->flags) && 837 - test_bit(IS_ANY_T1, &dev->flags))) { 838 - DEBUGP(4, dev, "Perform AUTOPPS\n"); 839 - set_bit(IS_AUTOPPS_ACT, &dev->flags); 840 - ptsreq.protocol = (0x01 << dev->proto); 841 - ptsreq.flags = 0x01; 842 - ptsreq.pts1 = 0x00; 843 - ptsreq.pts2 = 0x00; 844 - ptsreq.pts3 = 0x00; 845 - if (set_protocol(dev, &ptsreq) == 0) { 846 - DEBUGP(4, dev, "AUTOPPS ret SUCC\n"); 847 - clear_bit(IS_AUTOPPS_ACT, &dev->flags); 848 - wake_up_interruptible(&dev->atrq); 849 - } else { 850 - DEBUGP(4, dev, "AUTOPPS failed: " 851 - "repower using defaults\n"); 852 - /* prepare for repowering */ 853 - clear_bit(IS_ATR_PRESENT, &dev->flags); 854 - clear_bit(IS_ATR_VALID, &dev->flags); 855 - dev->rlen = 856 - dev->rpos = 857 - dev->atr_csum = 858 - dev->atr_len_retry = dev->cwarn = 0; 859 - dev->mstate = M_FETCH_ATR; 860 - 861 - dev->mdelay = T_50MSEC; 862 - } 863 - } else { 864 - /* for cards which use slightly different 865 - * params (extra guard time) */ 866 - set_cardparameter(dev); 867 - if (test_bit(IS_AUTOPPS_ACT, &dev->flags) == 1) 868 - DEBUGP(4, dev, "AUTOPPS already active " 869 - "2nd try:use default values\n"); 870 - if (dev->ta1 == 0x11) 871 - DEBUGP(4, dev, "No AUTOPPS necessary " 872 - "TA(1)==0x11\n"); 873 - if (test_bit(IS_ANY_T0, &dev->flags) 874 - && test_bit(IS_ANY_T1, &dev->flags)) 875 - DEBUGP(4, dev, "Do NOT perform AUTOPPS " 876 - "with multiprotocol cards\n"); 877 - clear_bit(IS_AUTOPPS_ACT, &dev->flags); 878 - wake_up_interruptible(&dev->atrq); 879 - } 880 - } else { 881 - DEBUGP(4, dev, "ATR invalid\n"); 882 - wake_up_interruptible(&dev->atrq); 883 - } 884 - break; 885 - case M_BAD_CARD: 886 - DEBUGP(4, dev, "M_BAD_CARD\n"); 887 - /* slow down warning, but prompt immediately after insertion */ 888 - if (dev->cwarn == 0 || dev->cwarn == 10) { 889 - set_bit(IS_BAD_CARD, &dev->flags); 890 - dev_warn(&dev->p_dev->dev, MODULE_NAME ": "); 891 - if (test_bit(IS_BAD_CSUM, &dev->flags)) { 892 - DEBUGP(4, dev, "ATR checksum (0x%.2x, should " 893 - "be zero) failed\n", dev->atr_csum); 894 - } 895 - #ifdef CM4000_DEBUG 896 - else if (test_bit(IS_BAD_LENGTH, &dev->flags)) { 897 - DEBUGP(4, dev, "ATR length error\n"); 898 - } else { 899 - DEBUGP(4, dev, "card damaged or wrong way " 900 - "inserted\n"); 901 - } 902 - #endif 903 - dev->cwarn = 0; 904 - wake_up_interruptible(&dev->atrq); /* wake open */ 905 - } 906 - dev->cwarn++; 907 - dev->mdelay = T_100MSEC; 908 - dev->mstate = M_FETCH_ATR; 909 - break; 910 - default: 911 - DEBUGP(7, dev, "Unknown action\n"); 912 - break; /* nothing */ 913 - } 914 - 915 - release_io: 916 - DEBUGP(7, dev, "release_io\n"); 917 - clear_bit(LOCK_IO, &dev->flags); 918 - wake_up_interruptible(&dev->ioq); /* whoever needs IO */ 919 - 920 - return_with_timer: 921 - DEBUGP(7, dev, "<- monitor_card (returns with timer)\n"); 922 - mod_timer(&dev->timer, jiffies + dev->mdelay); 923 - clear_bit(LOCK_MONITOR, &dev->flags); 924 - } 925 - 926 - /* Interface to userland (file_operations) */ 927 - 928 - static ssize_t cmm_read(struct file *filp, __user char *buf, size_t count, 929 - loff_t *ppos) 930 - { 931 - struct cm4000_dev *dev = filp->private_data; 932 - unsigned int iobase = dev->p_dev->resource[0]->start; 933 - ssize_t rc; 934 - int i, j, k; 935 - 936 - DEBUGP(2, dev, "-> cmm_read(%s,%d)\n", current->comm, current->pid); 937 - 938 - if (count == 0) /* according to manpage */ 939 - return 0; 940 - 941 - if (!pcmcia_dev_present(dev->p_dev) || /* device removed */ 942 - test_bit(IS_CMM_ABSENT, &dev->flags)) 943 - return -ENODEV; 944 - 945 - if (test_bit(IS_BAD_CSUM, &dev->flags)) 946 - return -EIO; 947 - 948 - /* also see the note about this in cmm_write */ 949 - if (wait_event_interruptible 950 - (dev->atrq, 951 - ((filp->f_flags & O_NONBLOCK) 952 - || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags) != 0)))) { 953 - if (filp->f_flags & O_NONBLOCK) 954 - return -EAGAIN; 955 - return -ERESTARTSYS; 956 - } 957 - 958 - if (test_bit(IS_ATR_VALID, &dev->flags) == 0) 959 - return -EIO; 960 - 961 - /* this one implements blocking IO */ 962 - if (wait_event_interruptible 963 - (dev->readq, 964 - ((filp->f_flags & O_NONBLOCK) || (dev->rpos < dev->rlen)))) { 965 - if (filp->f_flags & O_NONBLOCK) 966 - return -EAGAIN; 967 - return -ERESTARTSYS; 968 - } 969 - 970 - /* lock io */ 971 - if (wait_event_interruptible 972 - (dev->ioq, 973 - ((filp->f_flags & O_NONBLOCK) 974 - || (test_and_set_bit(LOCK_IO, (void *)&dev->flags) == 0)))) { 975 - if (filp->f_flags & O_NONBLOCK) 976 - return -EAGAIN; 977 - return -ERESTARTSYS; 978 - } 979 - 980 - rc = 0; 981 - dev->flags0 = inb(REG_FLAGS0(iobase)); 982 - if ((dev->flags0 & 1) == 0 /* no smartcard inserted */ 983 - || dev->flags0 == 0xff) { /* no cardman inserted */ 984 - clear_bit(IS_ATR_VALID, &dev->flags); 985 - if (dev->flags0 & 1) { 986 - set_bit(IS_CMM_ABSENT, &dev->flags); 987 - rc = -ENODEV; 988 - } else { 989 - rc = -EIO; 990 - } 991 - goto release_io; 992 - } 993 - 994 - DEBUGP(4, dev, "begin read answer\n"); 995 - j = min(count, (size_t)(dev->rlen - dev->rpos)); 996 - k = dev->rpos; 997 - if (k + j > 255) 998 - j = 256 - k; 999 - DEBUGP(4, dev, "read1 j=%d\n", j); 1000 - for (i = 0; i < j; i++) { 1001 - xoutb(k++, REG_BUF_ADDR(iobase)); 1002 - dev->rbuf[i] = xinb(REG_BUF_DATA(iobase)); 1003 - } 1004 - j = min(count, (size_t)(dev->rlen - dev->rpos)); 1005 - if (k + j > 255) { 1006 - DEBUGP(4, dev, "read2 j=%d\n", j); 1007 - dev->flags1 |= 0x10; /* MSB buf addr set */ 1008 - xoutb(dev->flags1, REG_FLAGS1(iobase)); 1009 - for (; i < j; i++) { 1010 - xoutb(k++, REG_BUF_ADDR(iobase)); 1011 - dev->rbuf[i] = xinb(REG_BUF_DATA(iobase)); 1012 - } 1013 - } 1014 - 1015 - if (dev->proto == 0 && count > dev->rlen - dev->rpos && i) { 1016 - DEBUGP(4, dev, "T=0 and count > buffer\n"); 1017 - dev->rbuf[i] = dev->rbuf[i - 1]; 1018 - dev->rbuf[i - 1] = dev->procbyte; 1019 - j++; 1020 - } 1021 - count = j; 1022 - 1023 - dev->rpos = dev->rlen + 1; 1024 - 1025 - /* Clear T1Active */ 1026 - DEBUGP(4, dev, "Clear T1Active\n"); 1027 - dev->flags1 &= 0xdf; 1028 - xoutb(dev->flags1, REG_FLAGS1(iobase)); 1029 - 1030 - xoutb(0, REG_FLAGS1(iobase)); /* clear detectCMM */ 1031 - /* last check before exit */ 1032 - if (!io_detect_cm4000(iobase, dev)) { 1033 - rc = -ENODEV; 1034 - goto release_io; 1035 - } 1036 - 1037 - if (test_bit(IS_INVREV, &dev->flags) && count > 0) 1038 - str_invert_revert(dev->rbuf, count); 1039 - 1040 - if (copy_to_user(buf, dev->rbuf, count)) 1041 - rc = -EFAULT; 1042 - 1043 - release_io: 1044 - clear_bit(LOCK_IO, &dev->flags); 1045 - wake_up_interruptible(&dev->ioq); 1046 - 1047 - DEBUGP(2, dev, "<- cmm_read returns: rc = %zi\n", 1048 - (rc < 0 ? rc : count)); 1049 - return rc < 0 ? rc : count; 1050 - } 1051 - 1052 - static ssize_t cmm_write(struct file *filp, const char __user *buf, 1053 - size_t count, loff_t *ppos) 1054 - { 1055 - struct cm4000_dev *dev = filp->private_data; 1056 - unsigned int iobase = dev->p_dev->resource[0]->start; 1057 - unsigned short s; 1058 - unsigned char infolen; 1059 - unsigned char sendT0; 1060 - unsigned short nsend; 1061 - unsigned short nr; 1062 - ssize_t rc; 1063 - int i; 1064 - 1065 - DEBUGP(2, dev, "-> cmm_write(%s,%d)\n", current->comm, current->pid); 1066 - 1067 - if (count == 0) /* according to manpage */ 1068 - return 0; 1069 - 1070 - if (dev->proto == 0 && count < 4) { 1071 - /* T0 must have at least 4 bytes */ 1072 - DEBUGP(4, dev, "T0 short write\n"); 1073 - return -EIO; 1074 - } 1075 - 1076 - nr = count & 0x1ff; /* max bytes to write */ 1077 - 1078 - sendT0 = dev->proto ? 0 : nr > 5 ? 0x08 : 0; 1079 - 1080 - if (!pcmcia_dev_present(dev->p_dev) || /* device removed */ 1081 - test_bit(IS_CMM_ABSENT, &dev->flags)) 1082 - return -ENODEV; 1083 - 1084 - if (test_bit(IS_BAD_CSUM, &dev->flags)) { 1085 - DEBUGP(4, dev, "bad csum\n"); 1086 - return -EIO; 1087 - } 1088 - 1089 - /* 1090 - * wait for atr to become valid. 1091 - * note: it is important to lock this code. if we dont, the monitor 1092 - * could be run between test_bit and the call to sleep on the 1093 - * atr-queue. if *then* the monitor detects atr valid, it will wake up 1094 - * any process on the atr-queue, *but* since we have been interrupted, 1095 - * we do not yet sleep on this queue. this would result in a missed 1096 - * wake_up and the calling process would sleep forever (until 1097 - * interrupted). also, do *not* restore_flags before sleep_on, because 1098 - * this could result in the same situation! 1099 - */ 1100 - if (wait_event_interruptible 1101 - (dev->atrq, 1102 - ((filp->f_flags & O_NONBLOCK) 1103 - || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags) != 0)))) { 1104 - if (filp->f_flags & O_NONBLOCK) 1105 - return -EAGAIN; 1106 - return -ERESTARTSYS; 1107 - } 1108 - 1109 - if (test_bit(IS_ATR_VALID, &dev->flags) == 0) { /* invalid atr */ 1110 - DEBUGP(4, dev, "invalid ATR\n"); 1111 - return -EIO; 1112 - } 1113 - 1114 - /* lock io */ 1115 - if (wait_event_interruptible 1116 - (dev->ioq, 1117 - ((filp->f_flags & O_NONBLOCK) 1118 - || (test_and_set_bit(LOCK_IO, (void *)&dev->flags) == 0)))) { 1119 - if (filp->f_flags & O_NONBLOCK) 1120 - return -EAGAIN; 1121 - return -ERESTARTSYS; 1122 - } 1123 - 1124 - if (copy_from_user(dev->sbuf, buf, ((count > 512) ? 512 : count))) 1125 - return -EFAULT; 1126 - 1127 - rc = 0; 1128 - dev->flags0 = inb(REG_FLAGS0(iobase)); 1129 - if ((dev->flags0 & 1) == 0 /* no smartcard inserted */ 1130 - || dev->flags0 == 0xff) { /* no cardman inserted */ 1131 - clear_bit(IS_ATR_VALID, &dev->flags); 1132 - if (dev->flags0 & 1) { 1133 - set_bit(IS_CMM_ABSENT, &dev->flags); 1134 - rc = -ENODEV; 1135 - } else { 1136 - DEBUGP(4, dev, "IO error\n"); 1137 - rc = -EIO; 1138 - } 1139 - goto release_io; 1140 - } 1141 - 1142 - xoutb(0x80, REG_FLAGS0(iobase)); /* reset SM */ 1143 - 1144 - if (!io_detect_cm4000(iobase, dev)) { 1145 - rc = -ENODEV; 1146 - goto release_io; 1147 - } 1148 - 1149 - /* reflect T=0 send/read mode in flags1 */ 1150 - dev->flags1 |= (sendT0); 1151 - 1152 - set_cardparameter(dev); 1153 - 1154 - /* dummy read, reset flag procedure received */ 1155 - inb(REG_FLAGS1(iobase)); 1156 - 1157 - dev->flags1 = 0x20 /* T_Active */ 1158 - | (sendT0) 1159 - | (test_bit(IS_INVREV, &dev->flags) ? 2 : 0)/* inverse parity */ 1160 - | (((dev->baudv - 1) & 0x0100) >> 8); /* MSB-Baud */ 1161 - DEBUGP(1, dev, "set dev->flags1 = 0x%.2x\n", dev->flags1); 1162 - xoutb(dev->flags1, REG_FLAGS1(iobase)); 1163 - 1164 - /* xmit data */ 1165 - DEBUGP(4, dev, "Xmit data\n"); 1166 - for (i = 0; i < nr; i++) { 1167 - if (i >= 256) { 1168 - dev->flags1 = 0x20 /* T_Active */ 1169 - | (sendT0) /* SendT0 */ 1170 - /* inverse parity: */ 1171 - | (test_bit(IS_INVREV, &dev->flags) ? 2 : 0) 1172 - | (((dev->baudv - 1) & 0x0100) >> 8) /* MSB-Baud */ 1173 - | 0x10; /* set address high */ 1174 - DEBUGP(4, dev, "dev->flags = 0x%.2x - set address " 1175 - "high\n", dev->flags1); 1176 - xoutb(dev->flags1, REG_FLAGS1(iobase)); 1177 - } 1178 - if (test_bit(IS_INVREV, &dev->flags)) { 1179 - DEBUGP(4, dev, "Apply inverse convention for 0x%.2x " 1180 - "-> 0x%.2x\n", (unsigned char)dev->sbuf[i], 1181 - invert_revert(dev->sbuf[i])); 1182 - xoutb(i, REG_BUF_ADDR(iobase)); 1183 - xoutb(invert_revert(dev->sbuf[i]), 1184 - REG_BUF_DATA(iobase)); 1185 - } else { 1186 - xoutb(i, REG_BUF_ADDR(iobase)); 1187 - xoutb(dev->sbuf[i], REG_BUF_DATA(iobase)); 1188 - } 1189 - } 1190 - DEBUGP(4, dev, "Xmit done\n"); 1191 - 1192 - if (dev->proto == 0) { 1193 - /* T=0 proto: 0 byte reply */ 1194 - if (nr == 4) { 1195 - DEBUGP(4, dev, "T=0 assumes 0 byte reply\n"); 1196 - xoutb(i, REG_BUF_ADDR(iobase)); 1197 - if (test_bit(IS_INVREV, &dev->flags)) 1198 - xoutb(0xff, REG_BUF_DATA(iobase)); 1199 - else 1200 - xoutb(0x00, REG_BUF_DATA(iobase)); 1201 - } 1202 - 1203 - /* numSendBytes */ 1204 - if (sendT0) 1205 - nsend = nr; 1206 - else { 1207 - if (nr == 4) 1208 - nsend = 5; 1209 - else { 1210 - nsend = 5 + (unsigned char)dev->sbuf[4]; 1211 - if (dev->sbuf[4] == 0) 1212 - nsend += 0x100; 1213 - } 1214 - } 1215 - } else 1216 - nsend = nr; 1217 - 1218 - /* T0: output procedure byte */ 1219 - if (test_bit(IS_INVREV, &dev->flags)) { 1220 - DEBUGP(4, dev, "T=0 set Procedure byte (inverse-reverse) " 1221 - "0x%.2x\n", invert_revert(dev->sbuf[1])); 1222 - xoutb(invert_revert(dev->sbuf[1]), REG_NUM_BYTES(iobase)); 1223 - } else { 1224 - DEBUGP(4, dev, "T=0 set Procedure byte 0x%.2x\n", dev->sbuf[1]); 1225 - xoutb(dev->sbuf[1], REG_NUM_BYTES(iobase)); 1226 - } 1227 - 1228 - DEBUGP(1, dev, "set NumSendBytes = 0x%.2x\n", 1229 - (unsigned char)(nsend & 0xff)); 1230 - xoutb((unsigned char)(nsend & 0xff), REG_NUM_SEND(iobase)); 1231 - 1232 - DEBUGP(1, dev, "Trigger CARDMAN CONTROLLER (0x%.2x)\n", 1233 - 0x40 /* SM_Active */ 1234 - | (dev->flags0 & 2 ? 0 : 4) /* power on if needed */ 1235 - |(dev->proto ? 0x10 : 0x08) /* T=1/T=0 */ 1236 - |(nsend & 0x100) >> 8 /* MSB numSendBytes */ ); 1237 - xoutb(0x40 /* SM_Active */ 1238 - | (dev->flags0 & 2 ? 0 : 4) /* power on if needed */ 1239 - |(dev->proto ? 0x10 : 0x08) /* T=1/T=0 */ 1240 - |(nsend & 0x100) >> 8, /* MSB numSendBytes */ 1241 - REG_FLAGS0(iobase)); 1242 - 1243 - /* wait for xmit done */ 1244 - if (dev->proto == 1) { 1245 - DEBUGP(4, dev, "Wait for xmit done\n"); 1246 - for (i = 0; i < 1000; i++) { 1247 - if (inb(REG_FLAGS0(iobase)) & 0x08) 1248 - break; 1249 - msleep_interruptible(10); 1250 - } 1251 - if (i == 1000) { 1252 - DEBUGP(4, dev, "timeout waiting for xmit done\n"); 1253 - rc = -EIO; 1254 - goto release_io; 1255 - } 1256 - } 1257 - 1258 - /* T=1: wait for infoLen */ 1259 - 1260 - infolen = 0; 1261 - if (dev->proto) { 1262 - /* wait until infoLen is valid */ 1263 - for (i = 0; i < 6000; i++) { /* max waiting time of 1 min */ 1264 - io_read_num_rec_bytes(iobase, &s); 1265 - if (s >= 3) { 1266 - infolen = inb(REG_FLAGS1(iobase)); 1267 - DEBUGP(4, dev, "infolen=%d\n", infolen); 1268 - break; 1269 - } 1270 - msleep_interruptible(10); 1271 - } 1272 - if (i == 6000) { 1273 - DEBUGP(4, dev, "timeout waiting for infoLen\n"); 1274 - rc = -EIO; 1275 - goto release_io; 1276 - } 1277 - } else 1278 - clear_bit(IS_PROCBYTE_PRESENT, &dev->flags); 1279 - 1280 - /* numRecBytes | bit9 of numRecytes */ 1281 - io_read_num_rec_bytes(iobase, &dev->rlen); 1282 - for (i = 0; i < 600; i++) { /* max waiting time of 2 sec */ 1283 - if (dev->proto) { 1284 - if (dev->rlen >= infolen + 4) 1285 - break; 1286 - } 1287 - msleep_interruptible(10); 1288 - /* numRecBytes | bit9 of numRecytes */ 1289 - io_read_num_rec_bytes(iobase, &s); 1290 - if (s > dev->rlen) { 1291 - DEBUGP(1, dev, "NumRecBytes inc (reset timeout)\n"); 1292 - i = 0; /* reset timeout */ 1293 - dev->rlen = s; 1294 - } 1295 - /* T=0: we are done when numRecBytes doesn't 1296 - * increment any more and NoProcedureByte 1297 - * is set and numRecBytes == bytes sent + 6 1298 - * (header bytes + data + 1 for sw2) 1299 - * except when the card replies an error 1300 - * which means, no data will be sent back. 1301 - */ 1302 - else if (dev->proto == 0) { 1303 - if ((inb(REG_BUF_ADDR(iobase)) & 0x80)) { 1304 - /* no procedure byte received since last read */ 1305 - DEBUGP(1, dev, "NoProcedure byte set\n"); 1306 - /* i=0; */ 1307 - } else { 1308 - /* procedure byte received since last read */ 1309 - DEBUGP(1, dev, "NoProcedure byte unset " 1310 - "(reset timeout)\n"); 1311 - dev->procbyte = inb(REG_FLAGS1(iobase)); 1312 - DEBUGP(1, dev, "Read procedure byte 0x%.2x\n", 1313 - dev->procbyte); 1314 - i = 0; /* resettimeout */ 1315 - } 1316 - if (inb(REG_FLAGS0(iobase)) & 0x08) { 1317 - DEBUGP(1, dev, "T0Done flag (read reply)\n"); 1318 - break; 1319 - } 1320 - } 1321 - if (dev->proto) 1322 - infolen = inb(REG_FLAGS1(iobase)); 1323 - } 1324 - if (i == 600) { 1325 - DEBUGP(1, dev, "timeout waiting for numRecBytes\n"); 1326 - rc = -EIO; 1327 - goto release_io; 1328 - } else { 1329 - if (dev->proto == 0) { 1330 - DEBUGP(1, dev, "Wait for T0Done bit to be set\n"); 1331 - for (i = 0; i < 1000; i++) { 1332 - if (inb(REG_FLAGS0(iobase)) & 0x08) 1333 - break; 1334 - msleep_interruptible(10); 1335 - } 1336 - if (i == 1000) { 1337 - DEBUGP(1, dev, "timeout waiting for T0Done\n"); 1338 - rc = -EIO; 1339 - goto release_io; 1340 - } 1341 - 1342 - dev->procbyte = inb(REG_FLAGS1(iobase)); 1343 - DEBUGP(4, dev, "Read procedure byte 0x%.2x\n", 1344 - dev->procbyte); 1345 - 1346 - io_read_num_rec_bytes(iobase, &dev->rlen); 1347 - DEBUGP(4, dev, "Read NumRecBytes = %i\n", dev->rlen); 1348 - 1349 - } 1350 - } 1351 - /* T=1: read offset=zero, T=0: read offset=after challenge */ 1352 - dev->rpos = dev->proto ? 0 : nr == 4 ? 5 : nr > dev->rlen ? 5 : nr; 1353 - DEBUGP(4, dev, "dev->rlen = %i, dev->rpos = %i, nr = %i\n", 1354 - dev->rlen, dev->rpos, nr); 1355 - 1356 - release_io: 1357 - DEBUGP(4, dev, "Reset SM\n"); 1358 - xoutb(0x80, REG_FLAGS0(iobase)); /* reset SM */ 1359 - 1360 - if (rc < 0) { 1361 - DEBUGP(4, dev, "Write failed but clear T_Active\n"); 1362 - dev->flags1 &= 0xdf; 1363 - xoutb(dev->flags1, REG_FLAGS1(iobase)); 1364 - } 1365 - 1366 - clear_bit(LOCK_IO, &dev->flags); 1367 - wake_up_interruptible(&dev->ioq); 1368 - wake_up_interruptible(&dev->readq); /* tell read we have data */ 1369 - 1370 - /* ITSEC E2: clear write buffer */ 1371 - memset((char *)dev->sbuf, 0, 512); 1372 - 1373 - /* return error or actually written bytes */ 1374 - DEBUGP(2, dev, "<- cmm_write\n"); 1375 - return rc < 0 ? rc : nr; 1376 - } 1377 - 1378 - static void start_monitor(struct cm4000_dev *dev) 1379 - { 1380 - DEBUGP(3, dev, "-> start_monitor\n"); 1381 - if (!dev->monitor_running) { 1382 - DEBUGP(5, dev, "create, init and add timer\n"); 1383 - timer_setup(&dev->timer, monitor_card, 0); 1384 - dev->monitor_running = 1; 1385 - mod_timer(&dev->timer, jiffies); 1386 - } else 1387 - DEBUGP(5, dev, "monitor already running\n"); 1388 - DEBUGP(3, dev, "<- start_monitor\n"); 1389 - } 1390 - 1391 - static void stop_monitor(struct cm4000_dev *dev) 1392 - { 1393 - DEBUGP(3, dev, "-> stop_monitor\n"); 1394 - if (dev->monitor_running) { 1395 - DEBUGP(5, dev, "stopping monitor\n"); 1396 - terminate_monitor(dev); 1397 - /* reset monitor SM */ 1398 - clear_bit(IS_ATR_VALID, &dev->flags); 1399 - clear_bit(IS_ATR_PRESENT, &dev->flags); 1400 - } else 1401 - DEBUGP(5, dev, "monitor already stopped\n"); 1402 - DEBUGP(3, dev, "<- stop_monitor\n"); 1403 - } 1404 - 1405 - static long cmm_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 1406 - { 1407 - struct cm4000_dev *dev = filp->private_data; 1408 - unsigned int iobase = dev->p_dev->resource[0]->start; 1409 - struct inode *inode = file_inode(filp); 1410 - struct pcmcia_device *link; 1411 - int rc; 1412 - void __user *argp = (void __user *)arg; 1413 - #ifdef CM4000_DEBUG 1414 - char *ioctl_names[CM_IOC_MAXNR + 1] = { 1415 - [_IOC_NR(CM_IOCGSTATUS)] "CM_IOCGSTATUS", 1416 - [_IOC_NR(CM_IOCGATR)] "CM_IOCGATR", 1417 - [_IOC_NR(CM_IOCARDOFF)] "CM_IOCARDOFF", 1418 - [_IOC_NR(CM_IOCSPTS)] "CM_IOCSPTS", 1419 - [_IOC_NR(CM_IOSDBGLVL)] "CM4000_DBGLVL", 1420 - }; 1421 - DEBUGP(3, dev, "cmm_ioctl(device=%d.%d) %s\n", imajor(inode), 1422 - iminor(inode), ioctl_names[_IOC_NR(cmd)]); 1423 - #endif 1424 - 1425 - mutex_lock(&cmm_mutex); 1426 - rc = -ENODEV; 1427 - link = dev_table[iminor(inode)]; 1428 - if (!pcmcia_dev_present(link)) { 1429 - DEBUGP(4, dev, "DEV_OK false\n"); 1430 - goto out; 1431 - } 1432 - 1433 - if (test_bit(IS_CMM_ABSENT, &dev->flags)) { 1434 - DEBUGP(4, dev, "CMM_ABSENT flag set\n"); 1435 - goto out; 1436 - } 1437 - rc = -EINVAL; 1438 - 1439 - if (_IOC_TYPE(cmd) != CM_IOC_MAGIC) { 1440 - DEBUGP(4, dev, "ioctype mismatch\n"); 1441 - goto out; 1442 - } 1443 - if (_IOC_NR(cmd) > CM_IOC_MAXNR) { 1444 - DEBUGP(4, dev, "iocnr mismatch\n"); 1445 - goto out; 1446 - } 1447 - rc = 0; 1448 - 1449 - switch (cmd) { 1450 - case CM_IOCGSTATUS: 1451 - DEBUGP(4, dev, " ... in CM_IOCGSTATUS\n"); 1452 - { 1453 - int status; 1454 - 1455 - /* clear other bits, but leave inserted & powered as 1456 - * they are */ 1457 - status = dev->flags0 & 3; 1458 - if (test_bit(IS_ATR_PRESENT, &dev->flags)) 1459 - status |= CM_ATR_PRESENT; 1460 - if (test_bit(IS_ATR_VALID, &dev->flags)) 1461 - status |= CM_ATR_VALID; 1462 - if (test_bit(IS_CMM_ABSENT, &dev->flags)) 1463 - status |= CM_NO_READER; 1464 - if (test_bit(IS_BAD_CARD, &dev->flags)) 1465 - status |= CM_BAD_CARD; 1466 - if (copy_to_user(argp, &status, sizeof(int))) 1467 - rc = -EFAULT; 1468 - } 1469 - break; 1470 - case CM_IOCGATR: 1471 - DEBUGP(4, dev, "... in CM_IOCGATR\n"); 1472 - { 1473 - struct atreq __user *atreq = argp; 1474 - int tmp; 1475 - /* allow nonblocking io and being interrupted */ 1476 - if (wait_event_interruptible 1477 - (dev->atrq, 1478 - ((filp->f_flags & O_NONBLOCK) 1479 - || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags) 1480 - != 0)))) { 1481 - if (filp->f_flags & O_NONBLOCK) 1482 - rc = -EAGAIN; 1483 - else 1484 - rc = -ERESTARTSYS; 1485 - break; 1486 - } 1487 - 1488 - rc = -EFAULT; 1489 - if (test_bit(IS_ATR_VALID, &dev->flags) == 0) { 1490 - tmp = -1; 1491 - if (copy_to_user(&(atreq->atr_len), &tmp, 1492 - sizeof(int))) 1493 - break; 1494 - } else { 1495 - if (copy_to_user(atreq->atr, dev->atr, 1496 - dev->atr_len)) 1497 - break; 1498 - 1499 - tmp = dev->atr_len; 1500 - if (copy_to_user(&(atreq->atr_len), &tmp, sizeof(int))) 1501 - break; 1502 - } 1503 - rc = 0; 1504 - break; 1505 - } 1506 - case CM_IOCARDOFF: 1507 - 1508 - #ifdef CM4000_DEBUG 1509 - DEBUGP(4, dev, "... in CM_IOCARDOFF\n"); 1510 - if (dev->flags0 & 0x01) { 1511 - DEBUGP(4, dev, " Card inserted\n"); 1512 - } else { 1513 - DEBUGP(2, dev, " No card inserted\n"); 1514 - } 1515 - if (dev->flags0 & 0x02) { 1516 - DEBUGP(4, dev, " Card powered\n"); 1517 - } else { 1518 - DEBUGP(2, dev, " Card not powered\n"); 1519 - } 1520 - #endif 1521 - 1522 - /* is a card inserted and powered? */ 1523 - if ((dev->flags0 & 0x01) && (dev->flags0 & 0x02)) { 1524 - 1525 - /* get IO lock */ 1526 - if (wait_event_interruptible 1527 - (dev->ioq, 1528 - ((filp->f_flags & O_NONBLOCK) 1529 - || (test_and_set_bit(LOCK_IO, (void *)&dev->flags) 1530 - == 0)))) { 1531 - if (filp->f_flags & O_NONBLOCK) 1532 - rc = -EAGAIN; 1533 - else 1534 - rc = -ERESTARTSYS; 1535 - break; 1536 - } 1537 - /* Set Flags0 = 0x42 */ 1538 - DEBUGP(4, dev, "Set Flags0=0x42 \n"); 1539 - xoutb(0x42, REG_FLAGS0(iobase)); 1540 - clear_bit(IS_ATR_PRESENT, &dev->flags); 1541 - clear_bit(IS_ATR_VALID, &dev->flags); 1542 - dev->mstate = M_CARDOFF; 1543 - clear_bit(LOCK_IO, &dev->flags); 1544 - if (wait_event_interruptible 1545 - (dev->atrq, 1546 - ((filp->f_flags & O_NONBLOCK) 1547 - || (test_bit(IS_ATR_VALID, (void *)&dev->flags) != 1548 - 0)))) { 1549 - if (filp->f_flags & O_NONBLOCK) 1550 - rc = -EAGAIN; 1551 - else 1552 - rc = -ERESTARTSYS; 1553 - break; 1554 - } 1555 - } 1556 - /* release lock */ 1557 - clear_bit(LOCK_IO, &dev->flags); 1558 - wake_up_interruptible(&dev->ioq); 1559 - 1560 - rc = 0; 1561 - break; 1562 - case CM_IOCSPTS: 1563 - { 1564 - struct ptsreq krnptsreq; 1565 - 1566 - if (copy_from_user(&krnptsreq, argp, 1567 - sizeof(struct ptsreq))) { 1568 - rc = -EFAULT; 1569 - break; 1570 - } 1571 - 1572 - rc = 0; 1573 - DEBUGP(4, dev, "... in CM_IOCSPTS\n"); 1574 - /* wait for ATR to get valid */ 1575 - if (wait_event_interruptible 1576 - (dev->atrq, 1577 - ((filp->f_flags & O_NONBLOCK) 1578 - || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags) 1579 - != 0)))) { 1580 - if (filp->f_flags & O_NONBLOCK) 1581 - rc = -EAGAIN; 1582 - else 1583 - rc = -ERESTARTSYS; 1584 - break; 1585 - } 1586 - /* get IO lock */ 1587 - if (wait_event_interruptible 1588 - (dev->ioq, 1589 - ((filp->f_flags & O_NONBLOCK) 1590 - || (test_and_set_bit(LOCK_IO, (void *)&dev->flags) 1591 - == 0)))) { 1592 - if (filp->f_flags & O_NONBLOCK) 1593 - rc = -EAGAIN; 1594 - else 1595 - rc = -ERESTARTSYS; 1596 - break; 1597 - } 1598 - 1599 - if ((rc = set_protocol(dev, &krnptsreq)) != 0) { 1600 - /* auto power_on again */ 1601 - dev->mstate = M_FETCH_ATR; 1602 - clear_bit(IS_ATR_VALID, &dev->flags); 1603 - } 1604 - /* release lock */ 1605 - clear_bit(LOCK_IO, &dev->flags); 1606 - wake_up_interruptible(&dev->ioq); 1607 - 1608 - } 1609 - break; 1610 - #ifdef CM4000_DEBUG 1611 - case CM_IOSDBGLVL: 1612 - rc = -ENOTTY; 1613 - break; 1614 - #endif 1615 - default: 1616 - DEBUGP(4, dev, "... in default (unknown IOCTL code)\n"); 1617 - rc = -ENOTTY; 1618 - } 1619 - out: 1620 - mutex_unlock(&cmm_mutex); 1621 - return rc; 1622 - } 1623 - 1624 - static int cmm_open(struct inode *inode, struct file *filp) 1625 - { 1626 - struct cm4000_dev *dev; 1627 - struct pcmcia_device *link; 1628 - int minor = iminor(inode); 1629 - int ret; 1630 - 1631 - if (minor >= CM4000_MAX_DEV) 1632 - return -ENODEV; 1633 - 1634 - mutex_lock(&cmm_mutex); 1635 - link = dev_table[minor]; 1636 - if (link == NULL || !pcmcia_dev_present(link)) { 1637 - ret = -ENODEV; 1638 - goto out; 1639 - } 1640 - 1641 - if (link->open) { 1642 - ret = -EBUSY; 1643 - goto out; 1644 - } 1645 - 1646 - dev = link->priv; 1647 - filp->private_data = dev; 1648 - 1649 - DEBUGP(2, dev, "-> cmm_open(device=%d.%d process=%s,%d)\n", 1650 - imajor(inode), minor, current->comm, current->pid); 1651 - 1652 - /* init device variables, they may be "polluted" after close 1653 - * or, the device may never have been closed (i.e. open failed) 1654 - */ 1655 - 1656 - ZERO_DEV(dev); 1657 - 1658 - /* opening will always block since the 1659 - * monitor will be started by open, which 1660 - * means we have to wait for ATR becoming 1661 - * valid = block until valid (or card 1662 - * inserted) 1663 - */ 1664 - if (filp->f_flags & O_NONBLOCK) { 1665 - ret = -EAGAIN; 1666 - goto out; 1667 - } 1668 - 1669 - dev->mdelay = T_50MSEC; 1670 - 1671 - /* start monitoring the cardstatus */ 1672 - start_monitor(dev); 1673 - 1674 - link->open = 1; /* only one open per device */ 1675 - 1676 - DEBUGP(2, dev, "<- cmm_open\n"); 1677 - ret = stream_open(inode, filp); 1678 - out: 1679 - mutex_unlock(&cmm_mutex); 1680 - return ret; 1681 - } 1682 - 1683 - static int cmm_close(struct inode *inode, struct file *filp) 1684 - { 1685 - struct cm4000_dev *dev; 1686 - struct pcmcia_device *link; 1687 - int minor = iminor(inode); 1688 - 1689 - if (minor >= CM4000_MAX_DEV) 1690 - return -ENODEV; 1691 - 1692 - link = dev_table[minor]; 1693 - if (link == NULL) 1694 - return -ENODEV; 1695 - 1696 - dev = link->priv; 1697 - 1698 - DEBUGP(2, dev, "-> cmm_close(maj/min=%d.%d)\n", 1699 - imajor(inode), minor); 1700 - 1701 - stop_monitor(dev); 1702 - 1703 - ZERO_DEV(dev); 1704 - 1705 - link->open = 0; /* only one open per device */ 1706 - wake_up(&dev->devq); /* socket removed? */ 1707 - 1708 - DEBUGP(2, dev, "cmm_close\n"); 1709 - return 0; 1710 - } 1711 - 1712 - static void cmm_cm4000_release(struct pcmcia_device * link) 1713 - { 1714 - struct cm4000_dev *dev = link->priv; 1715 - 1716 - /* dont terminate the monitor, rather rely on 1717 - * close doing that for us. 1718 - */ 1719 - DEBUGP(3, dev, "-> cmm_cm4000_release\n"); 1720 - while (link->open) { 1721 - printk(KERN_INFO MODULE_NAME ": delaying release until " 1722 - "process has terminated\n"); 1723 - /* note: don't interrupt us: 1724 - * close the applications which own 1725 - * the devices _first_ ! 1726 - */ 1727 - wait_event(dev->devq, (link->open == 0)); 1728 - } 1729 - /* dev->devq=NULL; this cannot be zeroed earlier */ 1730 - DEBUGP(3, dev, "<- cmm_cm4000_release\n"); 1731 - return; 1732 - } 1733 - 1734 - /*==== Interface to PCMCIA Layer =======================================*/ 1735 - 1736 - static int cm4000_config_check(struct pcmcia_device *p_dev, void *priv_data) 1737 - { 1738 - return pcmcia_request_io(p_dev); 1739 - } 1740 - 1741 - static int cm4000_config(struct pcmcia_device * link, int devno) 1742 - { 1743 - link->config_flags |= CONF_AUTO_SET_IO; 1744 - 1745 - /* read the config-tuples */ 1746 - if (pcmcia_loop_config(link, cm4000_config_check, NULL)) 1747 - goto cs_release; 1748 - 1749 - if (pcmcia_enable_device(link)) 1750 - goto cs_release; 1751 - 1752 - return 0; 1753 - 1754 - cs_release: 1755 - cm4000_release(link); 1756 - return -ENODEV; 1757 - } 1758 - 1759 - static int cm4000_suspend(struct pcmcia_device *link) 1760 - { 1761 - struct cm4000_dev *dev; 1762 - 1763 - dev = link->priv; 1764 - stop_monitor(dev); 1765 - 1766 - return 0; 1767 - } 1768 - 1769 - static int cm4000_resume(struct pcmcia_device *link) 1770 - { 1771 - struct cm4000_dev *dev; 1772 - 1773 - dev = link->priv; 1774 - if (link->open) 1775 - start_monitor(dev); 1776 - 1777 - return 0; 1778 - } 1779 - 1780 - static void cm4000_release(struct pcmcia_device *link) 1781 - { 1782 - cmm_cm4000_release(link); /* delay release until device closed */ 1783 - pcmcia_disable_device(link); 1784 - } 1785 - 1786 - static int cm4000_probe(struct pcmcia_device *link) 1787 - { 1788 - struct cm4000_dev *dev; 1789 - int i, ret; 1790 - 1791 - for (i = 0; i < CM4000_MAX_DEV; i++) 1792 - if (dev_table[i] == NULL) 1793 - break; 1794 - 1795 - if (i == CM4000_MAX_DEV) { 1796 - printk(KERN_NOTICE MODULE_NAME ": all devices in use\n"); 1797 - return -ENODEV; 1798 - } 1799 - 1800 - /* create a new cm4000_cs device */ 1801 - dev = kzalloc(sizeof(struct cm4000_dev), GFP_KERNEL); 1802 - if (dev == NULL) 1803 - return -ENOMEM; 1804 - 1805 - dev->p_dev = link; 1806 - link->priv = dev; 1807 - dev_table[i] = link; 1808 - 1809 - init_waitqueue_head(&dev->devq); 1810 - init_waitqueue_head(&dev->ioq); 1811 - init_waitqueue_head(&dev->atrq); 1812 - init_waitqueue_head(&dev->readq); 1813 - 1814 - ret = cm4000_config(link, i); 1815 - if (ret) { 1816 - dev_table[i] = NULL; 1817 - kfree(dev); 1818 - return ret; 1819 - } 1820 - 1821 - device_create(cmm_class, NULL, MKDEV(major, i), NULL, "cmm%d", i); 1822 - 1823 - return 0; 1824 - } 1825 - 1826 - static void cm4000_detach(struct pcmcia_device *link) 1827 - { 1828 - struct cm4000_dev *dev = link->priv; 1829 - int devno; 1830 - 1831 - /* find device */ 1832 - for (devno = 0; devno < CM4000_MAX_DEV; devno++) 1833 - if (dev_table[devno] == link) 1834 - break; 1835 - if (devno == CM4000_MAX_DEV) 1836 - return; 1837 - 1838 - stop_monitor(dev); 1839 - 1840 - cm4000_release(link); 1841 - 1842 - dev_table[devno] = NULL; 1843 - kfree(dev); 1844 - 1845 - device_destroy(cmm_class, MKDEV(major, devno)); 1846 - 1847 - return; 1848 - } 1849 - 1850 - static const struct file_operations cm4000_fops = { 1851 - .owner = THIS_MODULE, 1852 - .read = cmm_read, 1853 - .write = cmm_write, 1854 - .unlocked_ioctl = cmm_ioctl, 1855 - .open = cmm_open, 1856 - .release= cmm_close, 1857 - .llseek = no_llseek, 1858 - }; 1859 - 1860 - static const struct pcmcia_device_id cm4000_ids[] = { 1861 - PCMCIA_DEVICE_MANF_CARD(0x0223, 0x0002), 1862 - PCMCIA_DEVICE_PROD_ID12("CardMan", "4000", 0x2FB368CA, 0xA2BD8C39), 1863 - PCMCIA_DEVICE_NULL, 1864 - }; 1865 - MODULE_DEVICE_TABLE(pcmcia, cm4000_ids); 1866 - 1867 - static struct pcmcia_driver cm4000_driver = { 1868 - .owner = THIS_MODULE, 1869 - .name = "cm4000_cs", 1870 - .probe = cm4000_probe, 1871 - .remove = cm4000_detach, 1872 - .suspend = cm4000_suspend, 1873 - .resume = cm4000_resume, 1874 - .id_table = cm4000_ids, 1875 - }; 1876 - 1877 - static int __init cmm_init(void) 1878 - { 1879 - int rc; 1880 - 1881 - cmm_class = class_create(THIS_MODULE, "cardman_4000"); 1882 - if (IS_ERR(cmm_class)) 1883 - return PTR_ERR(cmm_class); 1884 - 1885 - major = register_chrdev(0, DEVICE_NAME, &cm4000_fops); 1886 - if (major < 0) { 1887 - printk(KERN_WARNING MODULE_NAME 1888 - ": could not get major number\n"); 1889 - class_destroy(cmm_class); 1890 - return major; 1891 - } 1892 - 1893 - rc = pcmcia_register_driver(&cm4000_driver); 1894 - if (rc < 0) { 1895 - unregister_chrdev(major, DEVICE_NAME); 1896 - class_destroy(cmm_class); 1897 - return rc; 1898 - } 1899 - 1900 - return 0; 1901 - } 1902 - 1903 - static void __exit cmm_exit(void) 1904 - { 1905 - pcmcia_unregister_driver(&cm4000_driver); 1906 - unregister_chrdev(major, DEVICE_NAME); 1907 - class_destroy(cmm_class); 1908 - }; 1909 - 1910 - module_init(cmm_init); 1911 - module_exit(cmm_exit); 1912 - MODULE_LICENSE("Dual BSD/GPL");
-684
drivers/char/pcmcia/cm4040_cs.c
··· 1 - /* 2 - * A driver for the Omnikey PCMCIA smartcard reader CardMan 4040 3 - * 4 - * (c) 2000-2004 Omnikey AG (http://www.omnikey.com/) 5 - * 6 - * (C) 2005-2006 Harald Welte <laforge@gnumonks.org> 7 - * - add support for poll() 8 - * - driver cleanup 9 - * - add waitqueues 10 - * - adhere to linux kernel coding style and policies 11 - * - support 2.6.13 "new style" pcmcia interface 12 - * - add class interface for udev device creation 13 - * 14 - * The device basically is a USB CCID compliant device that has been 15 - * attached to an I/O-Mapped FIFO. 16 - * 17 - * All rights reserved, Dual BSD/GPL Licensed. 18 - */ 19 - 20 - #include <linux/kernel.h> 21 - #include <linux/module.h> 22 - #include <linux/slab.h> 23 - #include <linux/init.h> 24 - #include <linux/fs.h> 25 - #include <linux/delay.h> 26 - #include <linux/poll.h> 27 - #include <linux/mutex.h> 28 - #include <linux/wait.h> 29 - #include <linux/uaccess.h> 30 - #include <asm/io.h> 31 - 32 - #include <pcmcia/cistpl.h> 33 - #include <pcmcia/cisreg.h> 34 - #include <pcmcia/ciscode.h> 35 - #include <pcmcia/ds.h> 36 - 37 - #include "cm4040_cs.h" 38 - 39 - 40 - #define reader_to_dev(x) (&x->p_dev->dev) 41 - 42 - /* n (debug level) is ignored */ 43 - /* additional debug output may be enabled by re-compiling with 44 - * CM4040_DEBUG set */ 45 - /* #define CM4040_DEBUG */ 46 - #define DEBUGP(n, rdr, x, args...) do { \ 47 - dev_dbg(reader_to_dev(rdr), "%s:" x, \ 48 - __func__ , ## args); \ 49 - } while (0) 50 - 51 - static DEFINE_MUTEX(cm4040_mutex); 52 - 53 - #define CCID_DRIVER_BULK_DEFAULT_TIMEOUT (150*HZ) 54 - #define CCID_DRIVER_ASYNC_POWERUP_TIMEOUT (35*HZ) 55 - #define CCID_DRIVER_MINIMUM_TIMEOUT (3*HZ) 56 - #define READ_WRITE_BUFFER_SIZE 512 57 - #define POLL_LOOP_COUNT 1000 58 - 59 - /* how often to poll for fifo status change */ 60 - #define POLL_PERIOD msecs_to_jiffies(10) 61 - 62 - static void reader_release(struct pcmcia_device *link); 63 - 64 - static int major; 65 - static struct class *cmx_class; 66 - 67 - #define BS_READABLE 0x01 68 - #define BS_WRITABLE 0x02 69 - 70 - struct reader_dev { 71 - struct pcmcia_device *p_dev; 72 - wait_queue_head_t devq; 73 - wait_queue_head_t poll_wait; 74 - wait_queue_head_t read_wait; 75 - wait_queue_head_t write_wait; 76 - unsigned long buffer_status; 77 - unsigned long timeout; 78 - unsigned char s_buf[READ_WRITE_BUFFER_SIZE]; 79 - unsigned char r_buf[READ_WRITE_BUFFER_SIZE]; 80 - struct timer_list poll_timer; 81 - }; 82 - 83 - static struct pcmcia_device *dev_table[CM_MAX_DEV]; 84 - 85 - #ifndef CM4040_DEBUG 86 - #define xoutb outb 87 - #define xinb inb 88 - #else 89 - static inline void xoutb(unsigned char val, unsigned short port) 90 - { 91 - pr_debug("outb(val=%.2x,port=%.4x)\n", val, port); 92 - outb(val, port); 93 - } 94 - 95 - static inline unsigned char xinb(unsigned short port) 96 - { 97 - unsigned char val; 98 - 99 - val = inb(port); 100 - pr_debug("%.2x=inb(%.4x)\n", val, port); 101 - return val; 102 - } 103 - #endif 104 - 105 - /* poll the device fifo status register. not to be confused with 106 - * the poll syscall. */ 107 - static void cm4040_do_poll(struct timer_list *t) 108 - { 109 - struct reader_dev *dev = from_timer(dev, t, poll_timer); 110 - unsigned int obs = xinb(dev->p_dev->resource[0]->start 111 - + REG_OFFSET_BUFFER_STATUS); 112 - 113 - if ((obs & BSR_BULK_IN_FULL)) { 114 - set_bit(BS_READABLE, &dev->buffer_status); 115 - DEBUGP(4, dev, "waking up read_wait\n"); 116 - wake_up_interruptible(&dev->read_wait); 117 - } else 118 - clear_bit(BS_READABLE, &dev->buffer_status); 119 - 120 - if (!(obs & BSR_BULK_OUT_FULL)) { 121 - set_bit(BS_WRITABLE, &dev->buffer_status); 122 - DEBUGP(4, dev, "waking up write_wait\n"); 123 - wake_up_interruptible(&dev->write_wait); 124 - } else 125 - clear_bit(BS_WRITABLE, &dev->buffer_status); 126 - 127 - if (dev->buffer_status) 128 - wake_up_interruptible(&dev->poll_wait); 129 - 130 - mod_timer(&dev->poll_timer, jiffies + POLL_PERIOD); 131 - } 132 - 133 - static void cm4040_stop_poll(struct reader_dev *dev) 134 - { 135 - del_timer_sync(&dev->poll_timer); 136 - } 137 - 138 - static int wait_for_bulk_out_ready(struct reader_dev *dev) 139 - { 140 - int i, rc; 141 - int iobase = dev->p_dev->resource[0]->start; 142 - 143 - for (i = 0; i < POLL_LOOP_COUNT; i++) { 144 - if ((xinb(iobase + REG_OFFSET_BUFFER_STATUS) 145 - & BSR_BULK_OUT_FULL) == 0) { 146 - DEBUGP(4, dev, "BulkOut empty (i=%d)\n", i); 147 - return 1; 148 - } 149 - } 150 - 151 - DEBUGP(4, dev, "wait_event_interruptible_timeout(timeout=%ld\n", 152 - dev->timeout); 153 - rc = wait_event_interruptible_timeout(dev->write_wait, 154 - test_and_clear_bit(BS_WRITABLE, 155 - &dev->buffer_status), 156 - dev->timeout); 157 - 158 - if (rc > 0) 159 - DEBUGP(4, dev, "woke up: BulkOut empty\n"); 160 - else if (rc == 0) 161 - DEBUGP(4, dev, "woke up: BulkOut full, returning 0 :(\n"); 162 - else if (rc < 0) 163 - DEBUGP(4, dev, "woke up: signal arrived\n"); 164 - 165 - return rc; 166 - } 167 - 168 - /* Write to Sync Control Register */ 169 - static int write_sync_reg(unsigned char val, struct reader_dev *dev) 170 - { 171 - int iobase = dev->p_dev->resource[0]->start; 172 - int rc; 173 - 174 - rc = wait_for_bulk_out_ready(dev); 175 - if (rc <= 0) 176 - return rc; 177 - 178 - xoutb(val, iobase + REG_OFFSET_SYNC_CONTROL); 179 - rc = wait_for_bulk_out_ready(dev); 180 - if (rc <= 0) 181 - return rc; 182 - 183 - return 1; 184 - } 185 - 186 - static int wait_for_bulk_in_ready(struct reader_dev *dev) 187 - { 188 - int i, rc; 189 - int iobase = dev->p_dev->resource[0]->start; 190 - 191 - for (i = 0; i < POLL_LOOP_COUNT; i++) { 192 - if ((xinb(iobase + REG_OFFSET_BUFFER_STATUS) 193 - & BSR_BULK_IN_FULL) == BSR_BULK_IN_FULL) { 194 - DEBUGP(3, dev, "BulkIn full (i=%d)\n", i); 195 - return 1; 196 - } 197 - } 198 - 199 - DEBUGP(4, dev, "wait_event_interruptible_timeout(timeout=%ld\n", 200 - dev->timeout); 201 - rc = wait_event_interruptible_timeout(dev->read_wait, 202 - test_and_clear_bit(BS_READABLE, 203 - &dev->buffer_status), 204 - dev->timeout); 205 - if (rc > 0) 206 - DEBUGP(4, dev, "woke up: BulkIn full\n"); 207 - else if (rc == 0) 208 - DEBUGP(4, dev, "woke up: BulkIn not full, returning 0 :(\n"); 209 - else if (rc < 0) 210 - DEBUGP(4, dev, "woke up: signal arrived\n"); 211 - 212 - return rc; 213 - } 214 - 215 - static ssize_t cm4040_read(struct file *filp, char __user *buf, 216 - size_t count, loff_t *ppos) 217 - { 218 - struct reader_dev *dev = filp->private_data; 219 - int iobase = dev->p_dev->resource[0]->start; 220 - size_t bytes_to_read; 221 - unsigned long i; 222 - size_t min_bytes_to_read; 223 - int rc; 224 - 225 - DEBUGP(2, dev, "-> cm4040_read(%s,%d)\n", current->comm, current->pid); 226 - 227 - if (count == 0) 228 - return 0; 229 - 230 - if (count < 10) 231 - return -EFAULT; 232 - 233 - if (filp->f_flags & O_NONBLOCK) { 234 - DEBUGP(4, dev, "filep->f_flags O_NONBLOCK set\n"); 235 - DEBUGP(2, dev, "<- cm4040_read (failure)\n"); 236 - return -EAGAIN; 237 - } 238 - 239 - if (!pcmcia_dev_present(dev->p_dev)) 240 - return -ENODEV; 241 - 242 - for (i = 0; i < 5; i++) { 243 - rc = wait_for_bulk_in_ready(dev); 244 - if (rc <= 0) { 245 - DEBUGP(5, dev, "wait_for_bulk_in_ready rc=%.2x\n", rc); 246 - DEBUGP(2, dev, "<- cm4040_read (failed)\n"); 247 - if (rc == -ERESTARTSYS) 248 - return rc; 249 - return -EIO; 250 - } 251 - dev->r_buf[i] = xinb(iobase + REG_OFFSET_BULK_IN); 252 - #ifdef CM4040_DEBUG 253 - pr_debug("%lu:%2x ", i, dev->r_buf[i]); 254 - } 255 - pr_debug("\n"); 256 - #else 257 - } 258 - #endif 259 - 260 - bytes_to_read = 5 + le32_to_cpu(*(__le32 *)&dev->r_buf[1]); 261 - 262 - DEBUGP(6, dev, "BytesToRead=%zu\n", bytes_to_read); 263 - 264 - min_bytes_to_read = min(count, bytes_to_read + 5); 265 - min_bytes_to_read = min_t(size_t, min_bytes_to_read, READ_WRITE_BUFFER_SIZE); 266 - 267 - DEBUGP(6, dev, "Min=%zu\n", min_bytes_to_read); 268 - 269 - for (i = 0; i < (min_bytes_to_read-5); i++) { 270 - rc = wait_for_bulk_in_ready(dev); 271 - if (rc <= 0) { 272 - DEBUGP(5, dev, "wait_for_bulk_in_ready rc=%.2x\n", rc); 273 - DEBUGP(2, dev, "<- cm4040_read (failed)\n"); 274 - if (rc == -ERESTARTSYS) 275 - return rc; 276 - return -EIO; 277 - } 278 - dev->r_buf[i+5] = xinb(iobase + REG_OFFSET_BULK_IN); 279 - #ifdef CM4040_DEBUG 280 - pr_debug("%lu:%2x ", i, dev->r_buf[i]); 281 - } 282 - pr_debug("\n"); 283 - #else 284 - } 285 - #endif 286 - 287 - *ppos = min_bytes_to_read; 288 - if (copy_to_user(buf, dev->r_buf, min_bytes_to_read)) 289 - return -EFAULT; 290 - 291 - rc = wait_for_bulk_in_ready(dev); 292 - if (rc <= 0) { 293 - DEBUGP(5, dev, "wait_for_bulk_in_ready rc=%.2x\n", rc); 294 - DEBUGP(2, dev, "<- cm4040_read (failed)\n"); 295 - if (rc == -ERESTARTSYS) 296 - return rc; 297 - return -EIO; 298 - } 299 - 300 - rc = write_sync_reg(SCR_READER_TO_HOST_DONE, dev); 301 - if (rc <= 0) { 302 - DEBUGP(5, dev, "write_sync_reg c=%.2x\n", rc); 303 - DEBUGP(2, dev, "<- cm4040_read (failed)\n"); 304 - if (rc == -ERESTARTSYS) 305 - return rc; 306 - else 307 - return -EIO; 308 - } 309 - 310 - xinb(iobase + REG_OFFSET_BULK_IN); 311 - 312 - DEBUGP(2, dev, "<- cm4040_read (successfully)\n"); 313 - return min_bytes_to_read; 314 - } 315 - 316 - static ssize_t cm4040_write(struct file *filp, const char __user *buf, 317 - size_t count, loff_t *ppos) 318 - { 319 - struct reader_dev *dev = filp->private_data; 320 - int iobase = dev->p_dev->resource[0]->start; 321 - ssize_t rc; 322 - int i; 323 - unsigned int bytes_to_write; 324 - 325 - DEBUGP(2, dev, "-> cm4040_write(%s,%d)\n", current->comm, current->pid); 326 - 327 - if (count == 0) { 328 - DEBUGP(2, dev, "<- cm4040_write empty read (successfully)\n"); 329 - return 0; 330 - } 331 - 332 - if ((count < 5) || (count > READ_WRITE_BUFFER_SIZE)) { 333 - DEBUGP(2, dev, "<- cm4040_write buffersize=%zd < 5\n", count); 334 - return -EIO; 335 - } 336 - 337 - if (filp->f_flags & O_NONBLOCK) { 338 - DEBUGP(4, dev, "filep->f_flags O_NONBLOCK set\n"); 339 - DEBUGP(4, dev, "<- cm4040_write (failure)\n"); 340 - return -EAGAIN; 341 - } 342 - 343 - if (!pcmcia_dev_present(dev->p_dev)) 344 - return -ENODEV; 345 - 346 - bytes_to_write = count; 347 - if (copy_from_user(dev->s_buf, buf, bytes_to_write)) 348 - return -EFAULT; 349 - 350 - switch (dev->s_buf[0]) { 351 - case CMD_PC_TO_RDR_XFRBLOCK: 352 - case CMD_PC_TO_RDR_SECURE: 353 - case CMD_PC_TO_RDR_TEST_SECURE: 354 - case CMD_PC_TO_RDR_OK_SECURE: 355 - dev->timeout = CCID_DRIVER_BULK_DEFAULT_TIMEOUT; 356 - break; 357 - 358 - case CMD_PC_TO_RDR_ICCPOWERON: 359 - dev->timeout = CCID_DRIVER_ASYNC_POWERUP_TIMEOUT; 360 - break; 361 - 362 - case CMD_PC_TO_RDR_GETSLOTSTATUS: 363 - case CMD_PC_TO_RDR_ICCPOWEROFF: 364 - case CMD_PC_TO_RDR_GETPARAMETERS: 365 - case CMD_PC_TO_RDR_RESETPARAMETERS: 366 - case CMD_PC_TO_RDR_SETPARAMETERS: 367 - case CMD_PC_TO_RDR_ESCAPE: 368 - case CMD_PC_TO_RDR_ICCCLOCK: 369 - default: 370 - dev->timeout = CCID_DRIVER_MINIMUM_TIMEOUT; 371 - break; 372 - } 373 - 374 - rc = write_sync_reg(SCR_HOST_TO_READER_START, dev); 375 - if (rc <= 0) { 376 - DEBUGP(5, dev, "write_sync_reg c=%.2zx\n", rc); 377 - DEBUGP(2, dev, "<- cm4040_write (failed)\n"); 378 - if (rc == -ERESTARTSYS) 379 - return rc; 380 - else 381 - return -EIO; 382 - } 383 - 384 - DEBUGP(4, dev, "start \n"); 385 - 386 - for (i = 0; i < bytes_to_write; i++) { 387 - rc = wait_for_bulk_out_ready(dev); 388 - if (rc <= 0) { 389 - DEBUGP(5, dev, "wait_for_bulk_out_ready rc=%.2zx\n", 390 - rc); 391 - DEBUGP(2, dev, "<- cm4040_write (failed)\n"); 392 - if (rc == -ERESTARTSYS) 393 - return rc; 394 - else 395 - return -EIO; 396 - } 397 - 398 - xoutb(dev->s_buf[i],iobase + REG_OFFSET_BULK_OUT); 399 - } 400 - DEBUGP(4, dev, "end\n"); 401 - 402 - rc = write_sync_reg(SCR_HOST_TO_READER_DONE, dev); 403 - 404 - if (rc <= 0) { 405 - DEBUGP(5, dev, "write_sync_reg c=%.2zx\n", rc); 406 - DEBUGP(2, dev, "<- cm4040_write (failed)\n"); 407 - if (rc == -ERESTARTSYS) 408 - return rc; 409 - else 410 - return -EIO; 411 - } 412 - 413 - DEBUGP(2, dev, "<- cm4040_write (successfully)\n"); 414 - return count; 415 - } 416 - 417 - static __poll_t cm4040_poll(struct file *filp, poll_table *wait) 418 - { 419 - struct reader_dev *dev = filp->private_data; 420 - __poll_t mask = 0; 421 - 422 - poll_wait(filp, &dev->poll_wait, wait); 423 - 424 - if (test_and_clear_bit(BS_READABLE, &dev->buffer_status)) 425 - mask |= EPOLLIN | EPOLLRDNORM; 426 - if (test_and_clear_bit(BS_WRITABLE, &dev->buffer_status)) 427 - mask |= EPOLLOUT | EPOLLWRNORM; 428 - 429 - DEBUGP(2, dev, "<- cm4040_poll(%u)\n", mask); 430 - 431 - return mask; 432 - } 433 - 434 - static int cm4040_open(struct inode *inode, struct file *filp) 435 - { 436 - struct reader_dev *dev; 437 - struct pcmcia_device *link; 438 - int minor = iminor(inode); 439 - int ret; 440 - 441 - if (minor >= CM_MAX_DEV) 442 - return -ENODEV; 443 - 444 - mutex_lock(&cm4040_mutex); 445 - link = dev_table[minor]; 446 - if (link == NULL || !pcmcia_dev_present(link)) { 447 - ret = -ENODEV; 448 - goto out; 449 - } 450 - 451 - if (link->open) { 452 - ret = -EBUSY; 453 - goto out; 454 - } 455 - 456 - dev = link->priv; 457 - filp->private_data = dev; 458 - 459 - if (filp->f_flags & O_NONBLOCK) { 460 - DEBUGP(4, dev, "filep->f_flags O_NONBLOCK set\n"); 461 - ret = -EAGAIN; 462 - goto out; 463 - } 464 - 465 - link->open = 1; 466 - 467 - mod_timer(&dev->poll_timer, jiffies + POLL_PERIOD); 468 - 469 - DEBUGP(2, dev, "<- cm4040_open (successfully)\n"); 470 - ret = nonseekable_open(inode, filp); 471 - out: 472 - mutex_unlock(&cm4040_mutex); 473 - return ret; 474 - } 475 - 476 - static int cm4040_close(struct inode *inode, struct file *filp) 477 - { 478 - struct reader_dev *dev = filp->private_data; 479 - struct pcmcia_device *link; 480 - int minor = iminor(inode); 481 - 482 - DEBUGP(2, dev, "-> cm4040_close(maj/min=%d.%d)\n", imajor(inode), 483 - iminor(inode)); 484 - 485 - if (minor >= CM_MAX_DEV) 486 - return -ENODEV; 487 - 488 - link = dev_table[minor]; 489 - if (link == NULL) 490 - return -ENODEV; 491 - 492 - cm4040_stop_poll(dev); 493 - 494 - link->open = 0; 495 - wake_up(&dev->devq); 496 - 497 - DEBUGP(2, dev, "<- cm4040_close\n"); 498 - return 0; 499 - } 500 - 501 - static void cm4040_reader_release(struct pcmcia_device *link) 502 - { 503 - struct reader_dev *dev = link->priv; 504 - 505 - DEBUGP(3, dev, "-> cm4040_reader_release\n"); 506 - while (link->open) { 507 - DEBUGP(3, dev, MODULE_NAME ": delaying release " 508 - "until process has terminated\n"); 509 - wait_event(dev->devq, (link->open == 0)); 510 - } 511 - DEBUGP(3, dev, "<- cm4040_reader_release\n"); 512 - return; 513 - } 514 - 515 - static int cm4040_config_check(struct pcmcia_device *p_dev, void *priv_data) 516 - { 517 - return pcmcia_request_io(p_dev); 518 - } 519 - 520 - 521 - static int reader_config(struct pcmcia_device *link, int devno) 522 - { 523 - struct reader_dev *dev; 524 - int fail_rc; 525 - 526 - link->config_flags |= CONF_AUTO_SET_IO; 527 - 528 - if (pcmcia_loop_config(link, cm4040_config_check, NULL)) 529 - goto cs_release; 530 - 531 - fail_rc = pcmcia_enable_device(link); 532 - if (fail_rc != 0) { 533 - dev_info(&link->dev, "pcmcia_enable_device failed 0x%x\n", 534 - fail_rc); 535 - goto cs_release; 536 - } 537 - 538 - dev = link->priv; 539 - 540 - DEBUGP(2, dev, "device " DEVICE_NAME "%d at %pR\n", devno, 541 - link->resource[0]); 542 - DEBUGP(2, dev, "<- reader_config (succ)\n"); 543 - 544 - return 0; 545 - 546 - cs_release: 547 - reader_release(link); 548 - return -ENODEV; 549 - } 550 - 551 - static void reader_release(struct pcmcia_device *link) 552 - { 553 - cm4040_reader_release(link); 554 - pcmcia_disable_device(link); 555 - } 556 - 557 - static int reader_probe(struct pcmcia_device *link) 558 - { 559 - struct reader_dev *dev; 560 - int i, ret; 561 - 562 - for (i = 0; i < CM_MAX_DEV; i++) { 563 - if (dev_table[i] == NULL) 564 - break; 565 - } 566 - 567 - if (i == CM_MAX_DEV) 568 - return -ENODEV; 569 - 570 - dev = kzalloc(sizeof(struct reader_dev), GFP_KERNEL); 571 - if (dev == NULL) 572 - return -ENOMEM; 573 - 574 - dev->timeout = CCID_DRIVER_MINIMUM_TIMEOUT; 575 - dev->buffer_status = 0; 576 - 577 - link->priv = dev; 578 - dev->p_dev = link; 579 - 580 - dev_table[i] = link; 581 - 582 - init_waitqueue_head(&dev->devq); 583 - init_waitqueue_head(&dev->poll_wait); 584 - init_waitqueue_head(&dev->read_wait); 585 - init_waitqueue_head(&dev->write_wait); 586 - timer_setup(&dev->poll_timer, cm4040_do_poll, 0); 587 - 588 - ret = reader_config(link, i); 589 - if (ret) { 590 - dev_table[i] = NULL; 591 - kfree(dev); 592 - return ret; 593 - } 594 - 595 - device_create(cmx_class, NULL, MKDEV(major, i), NULL, "cmx%d", i); 596 - 597 - return 0; 598 - } 599 - 600 - static void reader_detach(struct pcmcia_device *link) 601 - { 602 - struct reader_dev *dev = link->priv; 603 - int devno; 604 - 605 - /* find device */ 606 - for (devno = 0; devno < CM_MAX_DEV; devno++) { 607 - if (dev_table[devno] == link) 608 - break; 609 - } 610 - if (devno == CM_MAX_DEV) 611 - return; 612 - 613 - reader_release(link); 614 - 615 - dev_table[devno] = NULL; 616 - kfree(dev); 617 - 618 - device_destroy(cmx_class, MKDEV(major, devno)); 619 - 620 - return; 621 - } 622 - 623 - static const struct file_operations reader_fops = { 624 - .owner = THIS_MODULE, 625 - .read = cm4040_read, 626 - .write = cm4040_write, 627 - .open = cm4040_open, 628 - .release = cm4040_close, 629 - .poll = cm4040_poll, 630 - .llseek = no_llseek, 631 - }; 632 - 633 - static const struct pcmcia_device_id cm4040_ids[] = { 634 - PCMCIA_DEVICE_MANF_CARD(0x0223, 0x0200), 635 - PCMCIA_DEVICE_PROD_ID12("OMNIKEY", "CardMan 4040", 636 - 0xE32CDD8C, 0x8F23318B), 637 - PCMCIA_DEVICE_NULL, 638 - }; 639 - MODULE_DEVICE_TABLE(pcmcia, cm4040_ids); 640 - 641 - static struct pcmcia_driver reader_driver = { 642 - .owner = THIS_MODULE, 643 - .name = "cm4040_cs", 644 - .probe = reader_probe, 645 - .remove = reader_detach, 646 - .id_table = cm4040_ids, 647 - }; 648 - 649 - static int __init cm4040_init(void) 650 - { 651 - int rc; 652 - 653 - cmx_class = class_create(THIS_MODULE, "cardman_4040"); 654 - if (IS_ERR(cmx_class)) 655 - return PTR_ERR(cmx_class); 656 - 657 - major = register_chrdev(0, DEVICE_NAME, &reader_fops); 658 - if (major < 0) { 659 - printk(KERN_WARNING MODULE_NAME 660 - ": could not get major number\n"); 661 - class_destroy(cmx_class); 662 - return major; 663 - } 664 - 665 - rc = pcmcia_register_driver(&reader_driver); 666 - if (rc < 0) { 667 - unregister_chrdev(major, DEVICE_NAME); 668 - class_destroy(cmx_class); 669 - return rc; 670 - } 671 - 672 - return 0; 673 - } 674 - 675 - static void __exit cm4040_exit(void) 676 - { 677 - pcmcia_unregister_driver(&reader_driver); 678 - unregister_chrdev(major, DEVICE_NAME); 679 - class_destroy(cmx_class); 680 - } 681 - 682 - module_init(cm4040_init); 683 - module_exit(cm4040_exit); 684 - MODULE_LICENSE("Dual BSD/GPL");
-48
drivers/char/pcmcia/cm4040_cs.h
··· 1 - /* SPDX-License-Identifier: GPL-2.0 */ 2 - #ifndef _CM4040_H_ 3 - #define _CM4040_H_ 4 - 5 - #define CM_MAX_DEV 4 6 - 7 - #define DEVICE_NAME "cmx" 8 - #define MODULE_NAME "cm4040_cs" 9 - 10 - #define REG_OFFSET_BULK_OUT 0 11 - #define REG_OFFSET_BULK_IN 0 12 - #define REG_OFFSET_BUFFER_STATUS 1 13 - #define REG_OFFSET_SYNC_CONTROL 2 14 - 15 - #define BSR_BULK_IN_FULL 0x02 16 - #define BSR_BULK_OUT_FULL 0x01 17 - 18 - #define SCR_HOST_TO_READER_START 0x80 19 - #define SCR_ABORT 0x40 20 - #define SCR_EN_NOTIFY 0x20 21 - #define SCR_ACK_NOTIFY 0x10 22 - #define SCR_READER_TO_HOST_DONE 0x08 23 - #define SCR_HOST_TO_READER_DONE 0x04 24 - #define SCR_PULSE_INTERRUPT 0x02 25 - #define SCR_POWER_DOWN 0x01 26 - 27 - 28 - #define CMD_PC_TO_RDR_ICCPOWERON 0x62 29 - #define CMD_PC_TO_RDR_GETSLOTSTATUS 0x65 30 - #define CMD_PC_TO_RDR_ICCPOWEROFF 0x63 31 - #define CMD_PC_TO_RDR_SECURE 0x69 32 - #define CMD_PC_TO_RDR_GETPARAMETERS 0x6C 33 - #define CMD_PC_TO_RDR_RESETPARAMETERS 0x6D 34 - #define CMD_PC_TO_RDR_SETPARAMETERS 0x61 35 - #define CMD_PC_TO_RDR_XFRBLOCK 0x6F 36 - #define CMD_PC_TO_RDR_ESCAPE 0x6B 37 - #define CMD_PC_TO_RDR_ICCCLOCK 0x6E 38 - #define CMD_PC_TO_RDR_TEST_SECURE 0x74 39 - #define CMD_PC_TO_RDR_OK_SECURE 0x89 40 - 41 - 42 - #define CMD_RDR_TO_PC_SLOTSTATUS 0x81 43 - #define CMD_RDR_TO_PC_DATABLOCK 0x80 44 - #define CMD_RDR_TO_PC_PARAMETERS 0x82 45 - #define CMD_RDR_TO_PC_ESCAPE 0x83 46 - #define CMD_RDR_TO_PC_OK_SECURE 0x89 47 - 48 - #endif /* _CM4040_H_ */
-359
drivers/char/pcmcia/scr24x_cs.c
··· 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 - 11 - #include <linux/device.h> 12 - #include <linux/module.h> 13 - #include <linux/delay.h> 14 - #include <linux/cdev.h> 15 - #include <linux/slab.h> 16 - #include <linux/fs.h> 17 - #include <linux/io.h> 18 - #include <linux/uaccess.h> 19 - 20 - #include <pcmcia/cistpl.h> 21 - #include <pcmcia/ds.h> 22 - 23 - #define CCID_HEADER_SIZE 10 24 - #define CCID_LENGTH_OFFSET 1 25 - #define CCID_MAX_LEN 271 26 - 27 - #define SCR24X_DATA(n) (1 + n) 28 - #define SCR24X_CMD_STATUS 7 29 - #define CMD_START 0x40 30 - #define CMD_WRITE_BYTE 0x41 31 - #define CMD_READ_BYTE 0x42 32 - #define STATUS_BUSY 0x80 33 - 34 - struct scr24x_dev { 35 - struct device *dev; 36 - struct cdev c_dev; 37 - unsigned char buf[CCID_MAX_LEN]; 38 - int devno; 39 - struct mutex lock; 40 - struct kref refcnt; 41 - u8 __iomem *regs; 42 - }; 43 - 44 - #define SCR24X_DEVS 8 45 - static DECLARE_BITMAP(scr24x_minors, SCR24X_DEVS); 46 - 47 - static struct class *scr24x_class; 48 - static dev_t scr24x_devt; 49 - 50 - static void scr24x_delete(struct kref *kref) 51 - { 52 - struct scr24x_dev *dev = container_of(kref, struct scr24x_dev, 53 - refcnt); 54 - 55 - kfree(dev); 56 - } 57 - 58 - static int scr24x_wait_ready(struct scr24x_dev *dev) 59 - { 60 - u_char status; 61 - int timeout = 100; 62 - 63 - do { 64 - status = ioread8(dev->regs + SCR24X_CMD_STATUS); 65 - if (!(status & STATUS_BUSY)) 66 - return 0; 67 - 68 - msleep(20); 69 - } while (--timeout); 70 - 71 - return -EIO; 72 - } 73 - 74 - static int scr24x_open(struct inode *inode, struct file *filp) 75 - { 76 - struct scr24x_dev *dev = container_of(inode->i_cdev, 77 - struct scr24x_dev, c_dev); 78 - 79 - kref_get(&dev->refcnt); 80 - filp->private_data = dev; 81 - 82 - return stream_open(inode, filp); 83 - } 84 - 85 - static int scr24x_release(struct inode *inode, struct file *filp) 86 - { 87 - struct scr24x_dev *dev = filp->private_data; 88 - 89 - /* We must not take the dev->lock here as scr24x_delete() 90 - * might be called to remove the dev structure altogether. 91 - * We don't need the lock anyway, since after the reference 92 - * acquired in probe() is released in remove() the chrdev 93 - * is already unregistered and noone can possibly acquire 94 - * a reference via open() anymore. */ 95 - kref_put(&dev->refcnt, scr24x_delete); 96 - return 0; 97 - } 98 - 99 - static int read_chunk(struct scr24x_dev *dev, size_t offset, size_t limit) 100 - { 101 - size_t i, y; 102 - int ret; 103 - 104 - for (i = offset; i < limit; i += 5) { 105 - iowrite8(CMD_READ_BYTE, dev->regs + SCR24X_CMD_STATUS); 106 - ret = scr24x_wait_ready(dev); 107 - if (ret < 0) 108 - return ret; 109 - 110 - for (y = 0; y < 5 && i + y < limit; y++) 111 - dev->buf[i + y] = ioread8(dev->regs + SCR24X_DATA(y)); 112 - } 113 - 114 - return 0; 115 - } 116 - 117 - static ssize_t scr24x_read(struct file *filp, char __user *buf, size_t count, 118 - loff_t *ppos) 119 - { 120 - struct scr24x_dev *dev = filp->private_data; 121 - int ret; 122 - int len; 123 - 124 - if (count < CCID_HEADER_SIZE) 125 - return -EINVAL; 126 - 127 - if (mutex_lock_interruptible(&dev->lock)) 128 - return -ERESTARTSYS; 129 - 130 - if (!dev->dev) { 131 - ret = -ENODEV; 132 - goto out; 133 - } 134 - 135 - ret = scr24x_wait_ready(dev); 136 - if (ret < 0) 137 - goto out; 138 - len = CCID_HEADER_SIZE; 139 - ret = read_chunk(dev, 0, len); 140 - if (ret < 0) 141 - goto out; 142 - 143 - len += le32_to_cpu(*(__le32 *)(&dev->buf[CCID_LENGTH_OFFSET])); 144 - if (len > sizeof(dev->buf)) { 145 - ret = -EIO; 146 - goto out; 147 - } 148 - ret = read_chunk(dev, CCID_HEADER_SIZE, len); 149 - if (ret < 0) 150 - goto out; 151 - 152 - if (len < count) 153 - count = len; 154 - 155 - if (copy_to_user(buf, dev->buf, count)) { 156 - ret = -EFAULT; 157 - goto out; 158 - } 159 - 160 - ret = count; 161 - out: 162 - mutex_unlock(&dev->lock); 163 - return ret; 164 - } 165 - 166 - static ssize_t scr24x_write(struct file *filp, const char __user *buf, 167 - size_t count, loff_t *ppos) 168 - { 169 - struct scr24x_dev *dev = filp->private_data; 170 - size_t i, y; 171 - int ret; 172 - 173 - if (mutex_lock_interruptible(&dev->lock)) 174 - return -ERESTARTSYS; 175 - 176 - if (!dev->dev) { 177 - ret = -ENODEV; 178 - goto out; 179 - } 180 - 181 - if (count > sizeof(dev->buf)) { 182 - ret = -EINVAL; 183 - goto out; 184 - } 185 - 186 - if (copy_from_user(dev->buf, buf, count)) { 187 - ret = -EFAULT; 188 - goto out; 189 - } 190 - 191 - ret = scr24x_wait_ready(dev); 192 - if (ret < 0) 193 - goto out; 194 - 195 - iowrite8(CMD_START, dev->regs + SCR24X_CMD_STATUS); 196 - ret = scr24x_wait_ready(dev); 197 - if (ret < 0) 198 - goto out; 199 - 200 - for (i = 0; i < count; i += 5) { 201 - for (y = 0; y < 5 && i + y < count; y++) 202 - iowrite8(dev->buf[i + y], dev->regs + SCR24X_DATA(y)); 203 - 204 - iowrite8(CMD_WRITE_BYTE, dev->regs + SCR24X_CMD_STATUS); 205 - ret = scr24x_wait_ready(dev); 206 - if (ret < 0) 207 - goto out; 208 - } 209 - 210 - ret = count; 211 - out: 212 - mutex_unlock(&dev->lock); 213 - return ret; 214 - } 215 - 216 - static const struct file_operations scr24x_fops = { 217 - .owner = THIS_MODULE, 218 - .read = scr24x_read, 219 - .write = scr24x_write, 220 - .open = scr24x_open, 221 - .release = scr24x_release, 222 - .llseek = no_llseek, 223 - }; 224 - 225 - static int scr24x_config_check(struct pcmcia_device *link, void *priv_data) 226 - { 227 - if (resource_size(link->resource[PCMCIA_IOPORT_0]) != 0x11) 228 - return -ENODEV; 229 - return pcmcia_request_io(link); 230 - } 231 - 232 - static int scr24x_probe(struct pcmcia_device *link) 233 - { 234 - struct scr24x_dev *dev; 235 - int ret; 236 - 237 - dev = kzalloc(sizeof(*dev), GFP_KERNEL); 238 - if (!dev) 239 - return -ENOMEM; 240 - 241 - dev->devno = find_first_zero_bit(scr24x_minors, SCR24X_DEVS); 242 - if (dev->devno >= SCR24X_DEVS) { 243 - ret = -EBUSY; 244 - goto err; 245 - } 246 - 247 - mutex_init(&dev->lock); 248 - kref_init(&dev->refcnt); 249 - 250 - link->priv = dev; 251 - link->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_IO; 252 - 253 - ret = pcmcia_loop_config(link, scr24x_config_check, NULL); 254 - if (ret < 0) 255 - goto err; 256 - 257 - dev->dev = &link->dev; 258 - dev->regs = devm_ioport_map(&link->dev, 259 - link->resource[PCMCIA_IOPORT_0]->start, 260 - resource_size(link->resource[PCMCIA_IOPORT_0])); 261 - if (!dev->regs) { 262 - ret = -EIO; 263 - goto err; 264 - } 265 - 266 - cdev_init(&dev->c_dev, &scr24x_fops); 267 - dev->c_dev.owner = THIS_MODULE; 268 - ret = cdev_add(&dev->c_dev, MKDEV(MAJOR(scr24x_devt), dev->devno), 1); 269 - if (ret < 0) 270 - goto err; 271 - 272 - ret = pcmcia_enable_device(link); 273 - if (ret < 0) { 274 - pcmcia_disable_device(link); 275 - goto err; 276 - } 277 - 278 - device_create(scr24x_class, NULL, MKDEV(MAJOR(scr24x_devt), dev->devno), 279 - NULL, "scr24x%d", dev->devno); 280 - 281 - dev_info(&link->dev, "SCR24x Chip Card Interface\n"); 282 - return 0; 283 - 284 - err: 285 - if (dev->devno < SCR24X_DEVS) 286 - clear_bit(dev->devno, scr24x_minors); 287 - kfree (dev); 288 - return ret; 289 - } 290 - 291 - static void scr24x_remove(struct pcmcia_device *link) 292 - { 293 - struct scr24x_dev *dev = (struct scr24x_dev *)link->priv; 294 - 295 - device_destroy(scr24x_class, MKDEV(MAJOR(scr24x_devt), dev->devno)); 296 - mutex_lock(&dev->lock); 297 - pcmcia_disable_device(link); 298 - cdev_del(&dev->c_dev); 299 - clear_bit(dev->devno, scr24x_minors); 300 - dev->dev = NULL; 301 - mutex_unlock(&dev->lock); 302 - 303 - kref_put(&dev->refcnt, scr24x_delete); 304 - } 305 - 306 - static const struct pcmcia_device_id scr24x_ids[] = { 307 - PCMCIA_DEVICE_PROD_ID12("HP", "PC Card Smart Card Reader", 308 - 0x53cb94f9, 0xbfdf89a5), 309 - PCMCIA_DEVICE_PROD_ID1("SCR241 PCMCIA", 0x6271efa3), 310 - PCMCIA_DEVICE_PROD_ID1("SCR243 PCMCIA", 0x2054e8de), 311 - PCMCIA_DEVICE_PROD_ID1("SCR24x PCMCIA", 0x54a33665), 312 - PCMCIA_DEVICE_NULL 313 - }; 314 - MODULE_DEVICE_TABLE(pcmcia, scr24x_ids); 315 - 316 - static struct pcmcia_driver scr24x_driver = { 317 - .owner = THIS_MODULE, 318 - .name = "scr24x_cs", 319 - .probe = scr24x_probe, 320 - .remove = scr24x_remove, 321 - .id_table = scr24x_ids, 322 - }; 323 - 324 - static int __init scr24x_init(void) 325 - { 326 - int ret; 327 - 328 - scr24x_class = class_create(THIS_MODULE, "scr24x"); 329 - if (IS_ERR(scr24x_class)) 330 - return PTR_ERR(scr24x_class); 331 - 332 - ret = alloc_chrdev_region(&scr24x_devt, 0, SCR24X_DEVS, "scr24x"); 333 - if (ret < 0) { 334 - class_destroy(scr24x_class); 335 - return ret; 336 - } 337 - 338 - ret = pcmcia_register_driver(&scr24x_driver); 339 - if (ret < 0) { 340 - unregister_chrdev_region(scr24x_devt, SCR24X_DEVS); 341 - class_destroy(scr24x_class); 342 - } 343 - 344 - return ret; 345 - } 346 - 347 - static void __exit scr24x_exit(void) 348 - { 349 - pcmcia_unregister_driver(&scr24x_driver); 350 - unregister_chrdev_region(scr24x_devt, SCR24X_DEVS); 351 - class_destroy(scr24x_class); 352 - } 353 - 354 - module_init(scr24x_init); 355 - module_exit(scr24x_exit); 356 - 357 - MODULE_AUTHOR("Lubomir Rintel"); 358 - MODULE_DESCRIPTION("SCR24x PCMCIA Smart Card Reader Driver"); 359 - MODULE_LICENSE("GPL");
-11
include/linux/cm4000_cs.h
··· 1 - /* SPDX-License-Identifier: GPL-2.0 */ 2 - #ifndef _CM4000_H_ 3 - #define _CM4000_H_ 4 - 5 - #include <uapi/linux/cm4000_cs.h> 6 - 7 - 8 - #define DEVICE_NAME "cmm" 9 - #define MODULE_NAME "cm4000_cs" 10 - 11 - #endif /* _CM4000_H_ */
-64
include/uapi/linux/cm4000_cs.h
··· 1 - /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ 2 - #ifndef _UAPI_CM4000_H_ 3 - #define _UAPI_CM4000_H_ 4 - 5 - #include <linux/types.h> 6 - #include <linux/ioctl.h> 7 - 8 - #define MAX_ATR 33 9 - 10 - #define CM4000_MAX_DEV 4 11 - 12 - /* those two structures are passed via ioctl() from/to userspace. They are 13 - * used by existing userspace programs, so I kepth the awkward "bIFSD" naming 14 - * not to break compilation of userspace apps. -HW */ 15 - 16 - typedef struct atreq { 17 - __s32 atr_len; 18 - unsigned char atr[64]; 19 - __s32 power_act; 20 - unsigned char bIFSD; 21 - unsigned char bIFSC; 22 - } atreq_t; 23 - 24 - 25 - /* what is particularly stupid in the original driver is the arch-dependent 26 - * member sizes. This leads to CONFIG_COMPAT breakage, since 32bit userspace 27 - * will lay out the structure members differently than the 64bit kernel. 28 - * 29 - * I've changed "ptsreq.protocol" from "unsigned long" to "__u32". 30 - * On 32bit this will make no difference. With 64bit kernels, it will make 31 - * 32bit apps work, too. 32 - */ 33 - 34 - typedef struct ptsreq { 35 - __u32 protocol; /*T=0: 2^0, T=1: 2^1*/ 36 - unsigned char flags; 37 - unsigned char pts1; 38 - unsigned char pts2; 39 - unsigned char pts3; 40 - } ptsreq_t; 41 - 42 - #define CM_IOC_MAGIC 'c' 43 - #define CM_IOC_MAXNR 255 44 - 45 - #define CM_IOCGSTATUS _IOR (CM_IOC_MAGIC, 0, unsigned char *) 46 - #define CM_IOCGATR _IOWR(CM_IOC_MAGIC, 1, atreq_t *) 47 - #define CM_IOCSPTS _IOW (CM_IOC_MAGIC, 2, ptsreq_t *) 48 - #define CM_IOCSRDR _IO (CM_IOC_MAGIC, 3) 49 - #define CM_IOCARDOFF _IO (CM_IOC_MAGIC, 4) 50 - 51 - #define CM_IOSDBGLVL _IOW(CM_IOC_MAGIC, 250, int*) 52 - 53 - /* card and device states */ 54 - #define CM_CARD_INSERTED 0x01 55 - #define CM_CARD_POWERED 0x02 56 - #define CM_ATR_PRESENT 0x04 57 - #define CM_ATR_VALID 0x08 58 - #define CM_STATE_VALID 0x0f 59 - /* extra info only from CM4000 */ 60 - #define CM_NO_READER 0x10 61 - #define CM_BAD_CARD 0x20 62 - 63 - 64 - #endif /* _UAPI_CM4000_H_ */