| 1 | /* |
|---|
| 2 | * driver for Rainbow iKey 2032 devices |
|---|
| 3 | * |
|---|
| 4 | * Copyright (C) 2003, Andreas Jellinghaus <aj@dungeon.inka.de> |
|---|
| 5 | * Copyright (C) 2003, Olaf Kirch <okir@suse.de> |
|---|
| 6 | */ |
|---|
| 7 | |
|---|
| 8 | #include "internal.h" |
|---|
| 9 | #include <stdlib.h> |
|---|
| 10 | #include <string.h> |
|---|
| 11 | |
|---|
| 12 | /* |
|---|
| 13 | * Initialize the device |
|---|
| 14 | */ |
|---|
| 15 | static int ikey2k_open(ifd_reader_t * reader, const char *device_name) |
|---|
| 16 | { |
|---|
| 17 | ifd_device_t *dev; |
|---|
| 18 | ifd_device_params_t params; |
|---|
| 19 | |
|---|
| 20 | reader->name = "Rainbow iKey 2032"; |
|---|
| 21 | reader->nslots = 1; |
|---|
| 22 | if (!(dev = ifd_device_open(device_name))) |
|---|
| 23 | return -1; |
|---|
| 24 | if (ifd_device_type(dev) != IFD_DEVICE_TYPE_USB) { |
|---|
| 25 | ct_error("ikey2k: device %s is not a USB device", device_name); |
|---|
| 26 | ifd_device_close(dev); |
|---|
| 27 | return -1; |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | params = dev->settings; |
|---|
| 31 | params.usb.interface = 0; |
|---|
| 32 | if (ifd_device_set_parameters(dev, ¶ms) < 0) { |
|---|
| 33 | ct_error("ikey2k: setting parameters failed", device_name); |
|---|
| 34 | ifd_device_close(dev); |
|---|
| 35 | return -1; |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | reader->device = dev; |
|---|
| 39 | |
|---|
| 40 | return 0; |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | /* |
|---|
| 44 | * Power up the reader |
|---|
| 45 | */ |
|---|
| 46 | static int ikey2k_activate(ifd_reader_t * reader) |
|---|
| 47 | { |
|---|
| 48 | return 0; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | static int ikey2k_deactivate(ifd_reader_t * reader) |
|---|
| 52 | { |
|---|
| 53 | return -1; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | /* |
|---|
| 57 | * Card status - always present |
|---|
| 58 | */ |
|---|
| 59 | static int ikey2k_card_status(ifd_reader_t * reader, int slot, int *status) |
|---|
| 60 | { |
|---|
| 61 | *status = IFD_CARD_PRESENT; |
|---|
| 62 | return 0; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | /* |
|---|
| 66 | * Reset - nothing to be done? |
|---|
| 67 | * We should do something to make it come back with all state zapped |
|---|
| 68 | */ |
|---|
| 69 | static int ikey2k_card_reset(ifd_reader_t * reader, int slot, void *atr, |
|---|
| 70 | size_t size) |
|---|
| 71 | { |
|---|
| 72 | ifd_device_t *dev = reader->device; |
|---|
| 73 | unsigned char buffer[256]; |
|---|
| 74 | int rc, atrlen; |
|---|
| 75 | |
|---|
| 76 | static unsigned char expect5[] = |
|---|
| 77 | { 0x0d, 0x63, 0x00, 0x00, 0x2d, 0x2d, 0xc0, 0x80, |
|---|
| 78 | 0x80, 0x60, 0x80, 0x01, 0x19 |
|---|
| 79 | }; |
|---|
| 80 | |
|---|
| 81 | if (ifd_usb_control(dev, 0xc1, 0x00, 0, 0, buffer, 0x40, -1) != 13) |
|---|
| 82 | goto failed; |
|---|
| 83 | |
|---|
| 84 | /* we've seen values of 0x03, 0x05 and 0x06 in position 3, and |
|---|
| 85 | * since we don't know what they mean, we mask them here. */ |
|---|
| 86 | buffer[3] &= 0xF0; |
|---|
| 87 | |
|---|
| 88 | if (memcmp(buffer, expect5, sizeof(expect5)) != 0 |
|---|
| 89 | || ifd_usb_control(dev, 0x41, 0x16, 0, 0, buffer, 00, -1) != 0 |
|---|
| 90 | || ifd_usb_control(dev, 0xc1, 0x01, 0, 0, buffer, 02, -1) != 1 |
|---|
| 91 | || buffer[0] != 00) |
|---|
| 92 | goto failed; |
|---|
| 93 | |
|---|
| 94 | rc = ifd_usb_control(dev, 0x41, 0x16, 0x1901, 0, buffer, 0, 1000); |
|---|
| 95 | if (rc < 0) |
|---|
| 96 | goto failed; |
|---|
| 97 | |
|---|
| 98 | rc = ifd_usb_control(dev, 0xc1, 0x01, 0, 0, buffer, 0x19, 1000); |
|---|
| 99 | if (rc != 25) |
|---|
| 100 | goto failed; |
|---|
| 101 | |
|---|
| 102 | rc = ifd_usb_control(dev, 0x41, 0x16, 0, 0, buffer, 0x0, 1000); |
|---|
| 103 | if (rc != 0) |
|---|
| 104 | goto failed; |
|---|
| 105 | |
|---|
| 106 | rc = ifd_usb_control(dev, 0xc1, 0x01, 0, 0, buffer, 0x02, 1000); |
|---|
| 107 | if (rc != 1 || buffer[0] != 0) |
|---|
| 108 | goto failed; |
|---|
| 109 | |
|---|
| 110 | rc = ifd_usb_control(dev, 0x41, 0x16, 0, 0, buffer, 0x0, 1000); |
|---|
| 111 | if (rc != 0) |
|---|
| 112 | goto failed; |
|---|
| 113 | |
|---|
| 114 | rc = ifd_usb_control(dev, 0xc1, 0x01, 0, 0, buffer, 0x20, 1000); |
|---|
| 115 | if (rc != 1 || buffer[0] != 0) |
|---|
| 116 | goto failed; |
|---|
| 117 | |
|---|
| 118 | rc = ifd_usb_control(dev, 0x41, 0x16, 0x1901, 0, buffer, 0, 1000); |
|---|
| 119 | if (rc < 0) |
|---|
| 120 | goto failed; |
|---|
| 121 | |
|---|
| 122 | rc = ifd_usb_control(dev, 0xc1, 0x01, 0, 0, buffer, 0x19, 1000); |
|---|
| 123 | if (rc != 25) |
|---|
| 124 | goto failed; |
|---|
| 125 | |
|---|
| 126 | /* yes, this is _currently_ dead code, as IFD_MAX_ATR_LEN |
|---|
| 127 | * is higher than 25 ... */ |
|---|
| 128 | if (rc > IFD_MAX_ATR_LEN) |
|---|
| 129 | goto failed; |
|---|
| 130 | |
|---|
| 131 | atrlen = rc; |
|---|
| 132 | memcpy(atr, buffer, atrlen); |
|---|
| 133 | |
|---|
| 134 | return atrlen; |
|---|
| 135 | |
|---|
| 136 | failed: |
|---|
| 137 | ct_error("ikey2k: failed to activate token"); |
|---|
| 138 | return -1; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | /* |
|---|
| 142 | * Select a protocol. We override this function to be able to set the T=1 IFSC |
|---|
| 143 | */ |
|---|
| 144 | static int ikey2k_set_protocol(ifd_reader_t * reader, int nslot, int proto) |
|---|
| 145 | { |
|---|
| 146 | ifd_slot_t *slot = &reader->slot[nslot]; |
|---|
| 147 | int r; |
|---|
| 148 | |
|---|
| 149 | if (!(slot->proto = ifd_protocol_new(proto, reader, slot->dad))) |
|---|
| 150 | return -1; |
|---|
| 151 | |
|---|
| 152 | if (proto == IFD_PROTOCOL_T1) { |
|---|
| 153 | r = ifd_protocol_set_parameter(slot->proto, |
|---|
| 154 | IFD_PROTOCOL_T1_IFSC, 256); |
|---|
| 155 | if (r < 0) |
|---|
| 156 | return r; |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | return 0; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | /* |
|---|
| 163 | * Send/receive routines |
|---|
| 164 | */ |
|---|
| 165 | static int ikey2k_send(ifd_reader_t * reader, unsigned int dad, |
|---|
| 166 | const unsigned char *buffer, size_t len) |
|---|
| 167 | { |
|---|
| 168 | int value, idx; |
|---|
| 169 | value = buffer[1] << 8 | buffer[0]; |
|---|
| 170 | idx = buffer[3] << 8 | buffer[2]; |
|---|
| 171 | |
|---|
| 172 | return ifd_usb_control(reader->device, 0x41, 0x17, value, idx, |
|---|
| 173 | (void *)&buffer[4], len - 4, -1); |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | static int ikey2k_recv(ifd_reader_t * reader, unsigned int dad, |
|---|
| 177 | unsigned char *buffer, size_t len, long timeout) |
|---|
| 178 | { |
|---|
| 179 | return ifd_usb_control(reader->device, 0xc1, 0x01, 0, 0, |
|---|
| 180 | buffer, 255, timeout); |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | /* |
|---|
| 184 | * Driver operations |
|---|
| 185 | */ |
|---|
| 186 | static struct ifd_driver_ops ikey2k_driver; |
|---|
| 187 | |
|---|
| 188 | /* |
|---|
| 189 | * Initialize this module |
|---|
| 190 | */ |
|---|
| 191 | void ifd_ikey2k_register(void) |
|---|
| 192 | { |
|---|
| 193 | ikey2k_driver.open = ikey2k_open; |
|---|
| 194 | ikey2k_driver.activate = ikey2k_activate; |
|---|
| 195 | ikey2k_driver.deactivate = ikey2k_deactivate; |
|---|
| 196 | ikey2k_driver.card_status = ikey2k_card_status; |
|---|
| 197 | ikey2k_driver.card_reset = ikey2k_card_reset; |
|---|
| 198 | ikey2k_driver.set_protocol = ikey2k_set_protocol; |
|---|
| 199 | ikey2k_driver.send = ikey2k_send; |
|---|
| 200 | ikey2k_driver.recv = ikey2k_recv; |
|---|
| 201 | |
|---|
| 202 | ifd_driver_register("ikey2k", &ikey2k_driver); |
|---|
| 203 | } |
|---|