| 1 | /* |
|---|
| 2 | * OMNIKEY CardMan Mobile PCMCIA 4000 Driver |
|---|
| 3 | * |
|---|
| 4 | * This driver is not yet complete, but at least it |
|---|
| 5 | * spits out the ATR already. |
|---|
| 6 | * |
|---|
| 7 | * Copyright (C) 2005, Harald Welte <laforge@gnumonks.org> |
|---|
| 8 | * |
|---|
| 9 | * Based on information from the cm4000 driver by Omnikey AG. |
|---|
| 10 | */ |
|---|
| 11 | |
|---|
| 12 | /* only available on linux */ |
|---|
| 13 | #ifdef linux |
|---|
| 14 | |
|---|
| 15 | #include "internal.h" |
|---|
| 16 | #include "cardman.h" |
|---|
| 17 | #include <stdlib.h> |
|---|
| 18 | #include <string.h> |
|---|
| 19 | #include <unistd.h> |
|---|
| 20 | #include <errno.h> |
|---|
| 21 | #include <sys/ioctl.h> |
|---|
| 22 | |
|---|
| 23 | /* |
|---|
| 24 | * Initialize the device |
|---|
| 25 | */ |
|---|
| 26 | static int cm_open(ifd_reader_t * reader, const char *device_name) |
|---|
| 27 | { |
|---|
| 28 | ifd_device_t *dev; |
|---|
| 29 | ifd_device_params_t params; |
|---|
| 30 | |
|---|
| 31 | reader->name = "OMNIKEY CardMan 4000"; |
|---|
| 32 | reader->nslots = 1; |
|---|
| 33 | if (!(dev = ifd_device_open(device_name))) |
|---|
| 34 | return -1; |
|---|
| 35 | if (ifd_device_type(dev) != IFD_DEVICE_TYPE_PCMCIA) { |
|---|
| 36 | ct_error("cm4000: device %s is not a PCMCIA device", |
|---|
| 37 | device_name); |
|---|
| 38 | ifd_device_close(dev); |
|---|
| 39 | return -1; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | reader->driver_data = NULL; |
|---|
| 43 | reader->device = dev; |
|---|
| 44 | dev->timeout = 2000; |
|---|
| 45 | |
|---|
| 46 | params = dev->settings; |
|---|
| 47 | params.usb.interface = 0; |
|---|
| 48 | if (ifd_device_set_parameters(dev, ¶ms) < 0) { |
|---|
| 49 | ct_error("cm4000: setting parameters failed", device_name); |
|---|
| 50 | ifd_device_close(dev); |
|---|
| 51 | return -1; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | return 0; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | /* |
|---|
| 58 | * Power up the card slot |
|---|
| 59 | */ |
|---|
| 60 | static int cm_activate(ifd_reader_t * reader) |
|---|
| 61 | { |
|---|
| 62 | ifd_debug(1, "called."); |
|---|
| 63 | return 0; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | static int cm_deactivate(ifd_reader_t * reader) |
|---|
| 67 | { |
|---|
| 68 | ifd_debug(1, "called."); |
|---|
| 69 | return 0; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | /* |
|---|
| 73 | * Card status |
|---|
| 74 | */ |
|---|
| 75 | static int cm_card_status(ifd_reader_t * reader, int slot, int *status) |
|---|
| 76 | { |
|---|
| 77 | ifd_device_t *dev = reader->device; |
|---|
| 78 | unsigned int cm_status = 0; |
|---|
| 79 | int rc; |
|---|
| 80 | |
|---|
| 81 | *status = 0; |
|---|
| 82 | |
|---|
| 83 | ifd_debug(1, "called."); |
|---|
| 84 | rc = ioctl(dev->fd, CM_IOCGSTATUS, &cm_status); |
|---|
| 85 | if (rc != 0) { |
|---|
| 86 | ifd_debug(1, "error during ioctl(CM_IOCGSTATUS): %d=%s", |
|---|
| 87 | rc, strerror(errno)); |
|---|
| 88 | return -1; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | if (cm_status & CM_ATR_PRESENT) |
|---|
| 92 | *status = IFD_CARD_PRESENT; |
|---|
| 93 | |
|---|
| 94 | /* Hardware doesn't tell us about status change */ |
|---|
| 95 | |
|---|
| 96 | ifd_debug(1, "card %spresent", *status ? "" : "not "); |
|---|
| 97 | return 0; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | /* |
|---|
| 101 | * Reset |
|---|
| 102 | */ |
|---|
| 103 | static int cm_card_reset(ifd_reader_t * reader, int slot, void *atr, |
|---|
| 104 | size_t size) |
|---|
| 105 | { |
|---|
| 106 | ifd_device_t *dev = reader->device; |
|---|
| 107 | struct atreq cmatr; |
|---|
| 108 | int len; |
|---|
| 109 | |
|---|
| 110 | ioctl(dev->fd, 0x6304, 1); |
|---|
| 111 | /* propriatary driver doesn't check return value here, too */ |
|---|
| 112 | |
|---|
| 113 | /* CM_IOCGATR */ |
|---|
| 114 | if (ioctl(dev->fd, CM_IOCGATR, &cmatr) != 0) { |
|---|
| 115 | ifd_debug(1, "error during ioctl(CM_IOCGATR)\n"); |
|---|
| 116 | return -1; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | if (cmatr.atr_len == -1) { |
|---|
| 120 | ifd_debug(1, "atr_len == -1\n"); |
|---|
| 121 | return -1; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | len = cmatr.atr_len; |
|---|
| 125 | if ((size_t) len > size) |
|---|
| 126 | len = size; |
|---|
| 127 | |
|---|
| 128 | memcpy(atr, &cmatr.atr, len); |
|---|
| 129 | |
|---|
| 130 | return len; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | static int cm_send(ifd_reader_t * reader, unsigned int dad, |
|---|
| 134 | const unsigned char *buffer, size_t len) |
|---|
| 135 | { |
|---|
| 136 | ifd_device_t *dev = reader->device; |
|---|
| 137 | |
|---|
| 138 | return write(dev->fd, buffer, len); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | static int cm_recv(ifd_reader_t * reader, unsigned int dad, |
|---|
| 142 | unsigned char *buffer, size_t len, long timeout) |
|---|
| 143 | { |
|---|
| 144 | ifd_device_t *dev = reader->device; |
|---|
| 145 | |
|---|
| 146 | return read(dev->fd, buffer, len); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | /* |
|---|
| 150 | * Driver operations |
|---|
| 151 | */ |
|---|
| 152 | static struct ifd_driver_ops cm4000_driver; |
|---|
| 153 | |
|---|
| 154 | /* |
|---|
| 155 | * Initialize this module |
|---|
| 156 | */ |
|---|
| 157 | void ifd_cm4000_register(void) |
|---|
| 158 | { |
|---|
| 159 | cm4000_driver.open = cm_open; |
|---|
| 160 | cm4000_driver.activate = cm_activate; |
|---|
| 161 | cm4000_driver.deactivate = cm_deactivate; |
|---|
| 162 | cm4000_driver.card_reset = cm_card_reset; |
|---|
| 163 | cm4000_driver.card_status = cm_card_status; |
|---|
| 164 | cm4000_driver.send = cm_send; |
|---|
| 165 | cm4000_driver.recv = cm_recv; |
|---|
| 166 | |
|---|
| 167 | ifd_driver_register("cm4000", &cm4000_driver); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | #else |
|---|
| 171 | |
|---|
| 172 | /* |
|---|
| 173 | * Initialize this module |
|---|
| 174 | */ |
|---|
| 175 | void ifd_cm4000_register(void) |
|---|
| 176 | { |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | #endif |
|---|