| 1 | /* |
|---|
| 2 | * I/O routines for pcmcia devices |
|---|
| 3 | * |
|---|
| 4 | * Copyright (C) 2003 Olaf Kirch <okir@lst.de> |
|---|
| 5 | * Copyright (C) 2005 Harald Welte <laforge@gnumonks.org> |
|---|
| 6 | */ |
|---|
| 7 | |
|---|
| 8 | #include "internal.h" |
|---|
| 9 | #include <sys/types.h> |
|---|
| 10 | #include <sys/select.h> |
|---|
| 11 | #include <sys/poll.h> |
|---|
| 12 | #include <sys/ioctl.h> |
|---|
| 13 | #include <unistd.h> |
|---|
| 14 | #include <errno.h> |
|---|
| 15 | #include <string.h> |
|---|
| 16 | #include <fcntl.h> |
|---|
| 17 | |
|---|
| 18 | /* |
|---|
| 19 | * Input/output routines |
|---|
| 20 | */ |
|---|
| 21 | static int |
|---|
| 22 | ifd_pcmcia_block_send(ifd_device_t * dev, const unsigned char *buffer, |
|---|
| 23 | size_t len) |
|---|
| 24 | { |
|---|
| 25 | size_t total = len; |
|---|
| 26 | int n; |
|---|
| 27 | |
|---|
| 28 | while (len) { |
|---|
| 29 | n = write(dev->fd, buffer, len); |
|---|
| 30 | if (n < 0) { |
|---|
| 31 | ct_error("Error writing to %s: %m", dev->name); |
|---|
| 32 | return -1; |
|---|
| 33 | } |
|---|
| 34 | buffer += n; |
|---|
| 35 | len -= n; |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | return total; |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | static int |
|---|
| 42 | ifd_pcmcia_block_recv(ifd_device_t * dev, unsigned char *buffer, size_t len, |
|---|
| 43 | long timeout) |
|---|
| 44 | { |
|---|
| 45 | struct timeval begin; |
|---|
| 46 | int n; |
|---|
| 47 | |
|---|
| 48 | struct pollfd pfd; |
|---|
| 49 | long wait; |
|---|
| 50 | |
|---|
| 51 | gettimeofday(&begin, NULL); |
|---|
| 52 | |
|---|
| 53 | if ((wait = timeout - ifd_time_elapsed(&begin)) < 0) |
|---|
| 54 | goto timeout; |
|---|
| 55 | |
|---|
| 56 | pfd.fd = dev->fd; |
|---|
| 57 | pfd.events = POLLIN; |
|---|
| 58 | n = poll(&pfd, 1, wait); |
|---|
| 59 | if (n < 0) { |
|---|
| 60 | ct_error("%s: error while waiting for input: %m", dev->name); |
|---|
| 61 | return -1; |
|---|
| 62 | } |
|---|
| 63 | if (n == 0) |
|---|
| 64 | goto timeout; |
|---|
| 65 | |
|---|
| 66 | n = read(dev->fd, buffer, len); |
|---|
| 67 | if (n < 0) { |
|---|
| 68 | ct_error("%s: failed to read from device: %m", dev->name); |
|---|
| 69 | return -1; |
|---|
| 70 | } |
|---|
| 71 | if (ct_config.debug >= 9) |
|---|
| 72 | ifd_debug(9, "pcmcia recv:%s", ct_hexdump(buffer, n)); |
|---|
| 73 | |
|---|
| 74 | return n; |
|---|
| 75 | |
|---|
| 76 | timeout: /* Timeouts are a little special; they may happen e.g. |
|---|
| 77 | * when trying to obtain the ATR */ |
|---|
| 78 | if (!ct_config.suppress_errors) |
|---|
| 79 | ct_error("%s: timed out while waiting for input", dev->name); |
|---|
| 80 | return IFD_ERROR_TIMEOUT; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | /* |
|---|
| 84 | * Set pcmcia params |
|---|
| 85 | */ |
|---|
| 86 | static int ifd_pcmcia_block_set_params(ifd_device_t * dev, |
|---|
| 87 | const ifd_device_params_t * params) |
|---|
| 88 | { |
|---|
| 89 | /* nothing to do so far */ |
|---|
| 90 | dev->settings = *params; |
|---|
| 91 | return 0; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | /* |
|---|
| 95 | * Close the device |
|---|
| 96 | */ |
|---|
| 97 | static void ifd_pcmcia_block_close(ifd_device_t * dev) |
|---|
| 98 | { |
|---|
| 99 | if (dev->fd >= 0) |
|---|
| 100 | close(dev->fd); |
|---|
| 101 | dev->fd = -1; |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | static struct ifd_device_ops ifd_pcmcia_block_ops; |
|---|
| 105 | |
|---|
| 106 | /* |
|---|
| 107 | * Open serial device |
|---|
| 108 | */ |
|---|
| 109 | ifd_device_t *ifd_open_pcmcia_block(const char *name) |
|---|
| 110 | { |
|---|
| 111 | ifd_device_t *dev; |
|---|
| 112 | int fd; |
|---|
| 113 | |
|---|
| 114 | if ((fd = open(name, O_RDWR)) < 0) { |
|---|
| 115 | ct_error("Unable to open %s: %m", name); |
|---|
| 116 | return NULL; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | ifd_pcmcia_block_ops.send = ifd_pcmcia_block_send; |
|---|
| 120 | ifd_pcmcia_block_ops.recv = ifd_pcmcia_block_recv; |
|---|
| 121 | ifd_pcmcia_block_ops.set_params = ifd_pcmcia_block_set_params; |
|---|
| 122 | ifd_pcmcia_block_ops.close = ifd_pcmcia_block_close; |
|---|
| 123 | |
|---|
| 124 | dev = ifd_device_new(name, &ifd_pcmcia_block_ops, sizeof(*dev)); |
|---|
| 125 | dev->timeout = 1000; /* acceptable? */ |
|---|
| 126 | dev->type = IFD_DEVICE_TYPE_PCMCIA_BLOCK; |
|---|
| 127 | dev->fd = fd; |
|---|
| 128 | |
|---|
| 129 | return dev; |
|---|
| 130 | } |
|---|