| 1 | /* |
|---|
| 2 | * driver for Rainbow iKey 3000 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 ikey3k_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 3000"; |
|---|
| 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("ikey3k: 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("ikey3k: 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 ikey3k_activate(ifd_reader_t * reader) |
|---|
| 47 | { |
|---|
| 48 | return 0; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | static int ikey3k_deactivate(ifd_reader_t * reader) |
|---|
| 52 | { |
|---|
| 53 | return -1; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | /* |
|---|
| 57 | * Card status - always present |
|---|
| 58 | */ |
|---|
| 59 | static int ikey3k_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 ikey3k_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, n, atrlen; |
|---|
| 75 | |
|---|
| 76 | unsigned char expect5[] = |
|---|
| 77 | { 0x0a, 0x61, 0x00, 0x07, 0x2d, 0x2d, 0xc0, 0x80, 0x80, 0x60 }; |
|---|
| 78 | unsigned char expect11[] = { 0xff, 0x11, 0x11, 0xff }; |
|---|
| 79 | |
|---|
| 80 | if (ifd_usb_control(dev, 0xc1, 0x00, 0, 0, buffer, 0x40, -1) != 10 |
|---|
| 81 | || memcmp(buffer, expect5, sizeof(expect5)) != 0 |
|---|
| 82 | || ifd_usb_control(dev, 0x41, 0x16, 0, 0, buffer, 00, -1) != 0 |
|---|
| 83 | || ifd_usb_control(dev, 0xc1, 0x01, 0, 0, buffer, 02, -1) != 1 |
|---|
| 84 | || buffer[0] != 00) |
|---|
| 85 | goto failed; |
|---|
| 86 | |
|---|
| 87 | rc = ifd_usb_control(dev, 0x41, 0x16, 0x2005, 0, buffer, 0, 1000); |
|---|
| 88 | if (rc < 0) |
|---|
| 89 | goto failed; |
|---|
| 90 | |
|---|
| 91 | rc = ifd_usb_control(dev, 0xc1, 0x01, 0, 0, buffer, 0x20, 1000); |
|---|
| 92 | if (rc <= 0) |
|---|
| 93 | goto failed; |
|---|
| 94 | |
|---|
| 95 | n = buffer[0]; |
|---|
| 96 | if (n + 1 > rc) |
|---|
| 97 | goto failed; |
|---|
| 98 | if (n > IFD_MAX_ATR_LEN) |
|---|
| 99 | goto failed; |
|---|
| 100 | |
|---|
| 101 | if (n > size) |
|---|
| 102 | n = size; |
|---|
| 103 | atrlen = n; |
|---|
| 104 | memcpy(atr, buffer + 1, atrlen); |
|---|
| 105 | |
|---|
| 106 | if (ifd_usb_control(dev, 0x41, 0x16, 0x0002, 0, buffer, 0, -1) != 0 |
|---|
| 107 | || ifd_usb_control(dev, 0xc1, 0x01, 0, 0, buffer, 04, -1) != 4 |
|---|
| 108 | || memcmp(buffer, expect11, sizeof(expect11)) != 0) |
|---|
| 109 | goto failed; |
|---|
| 110 | |
|---|
| 111 | return atrlen; |
|---|
| 112 | |
|---|
| 113 | failed: |
|---|
| 114 | ct_error("ikey3k: failed to activate token"); |
|---|
| 115 | return -1; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | /* |
|---|
| 119 | * Select a protocol. We override this function to be able to set the T=1 IFSC |
|---|
| 120 | */ |
|---|
| 121 | static int ikey3k_set_protocol(ifd_reader_t * reader, int nslot, int proto) |
|---|
| 122 | { |
|---|
| 123 | ifd_slot_t *slot = &reader->slot[nslot]; |
|---|
| 124 | int r; |
|---|
| 125 | |
|---|
| 126 | if (!(slot->proto = ifd_protocol_new(proto, reader, slot->dad))) |
|---|
| 127 | return -1; |
|---|
| 128 | |
|---|
| 129 | if (proto == IFD_PROTOCOL_T1) { |
|---|
| 130 | r = ifd_protocol_set_parameter(slot->proto, |
|---|
| 131 | IFD_PROTOCOL_T1_IFSC, 256); |
|---|
| 132 | if (r < 0) |
|---|
| 133 | return r; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | return 0; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | /* |
|---|
| 140 | * Send/receive routines |
|---|
| 141 | */ |
|---|
| 142 | static int ikey3k_send(ifd_reader_t * reader, unsigned int dad, |
|---|
| 143 | const unsigned char *buffer, size_t len) |
|---|
| 144 | { |
|---|
| 145 | int value, idx; |
|---|
| 146 | value = buffer[1] << 8 | buffer[0]; |
|---|
| 147 | idx = buffer[3] << 8 | buffer[2]; |
|---|
| 148 | |
|---|
| 149 | return ifd_usb_control(reader->device, 0x41, 0x17, value, idx, |
|---|
| 150 | (void *)&buffer[4], len - 4, -1); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | static int ikey3k_recv(ifd_reader_t * reader, unsigned int dad, |
|---|
| 154 | unsigned char *buffer, size_t len, long timeout) |
|---|
| 155 | { |
|---|
| 156 | return ifd_usb_control(reader->device, 0xc1, 0x01, 0, 0, |
|---|
| 157 | buffer, 255, timeout); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | /* |
|---|
| 161 | * Driver operations |
|---|
| 162 | */ |
|---|
| 163 | static struct ifd_driver_ops ikey3k_driver; |
|---|
| 164 | |
|---|
| 165 | /* |
|---|
| 166 | * Initialize this module |
|---|
| 167 | */ |
|---|
| 168 | void ifd_ikey3k_register(void) |
|---|
| 169 | { |
|---|
| 170 | ikey3k_driver.open = ikey3k_open; |
|---|
| 171 | ikey3k_driver.activate = ikey3k_activate; |
|---|
| 172 | ikey3k_driver.deactivate = ikey3k_deactivate; |
|---|
| 173 | ikey3k_driver.card_status = ikey3k_card_status; |
|---|
| 174 | ikey3k_driver.card_reset = ikey3k_card_reset; |
|---|
| 175 | ikey3k_driver.set_protocol = ikey3k_set_protocol; |
|---|
| 176 | ikey3k_driver.send = ikey3k_send; |
|---|
| 177 | ikey3k_driver.recv = ikey3k_recv; |
|---|
| 178 | |
|---|
| 179 | ifd_driver_register("ikey3k", &ikey3k_driver); |
|---|
| 180 | } |
|---|