source: trunk/src/ifd/pcmcia.c @ 1132

Revision 1132, 2.7 KB checked in by alonbl, 3 years ago (diff)

Revert 1131

Line 
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 */
21static int ifd_pcmcia_send(ifd_device_t * dev, const unsigned char *buffer,
22                           size_t len)
23{
24        size_t total = len;
25        int n;
26
27        while (len) {
28                n = write(dev->fd, buffer, len);
29                if (n < 0) {
30                        ct_error("Error writing to %s: %m", dev->name);
31                        return -1;
32                }
33                buffer += n;
34                len -= n;
35        }
36
37        return total;
38}
39
40static int ifd_pcmcia_recv(ifd_device_t * dev, unsigned char *buffer,
41                           size_t len, long timeout)
42{
43        size_t total = len, to_read;
44        struct timeval begin;
45        int n;
46
47        gettimeofday(&begin, NULL);
48
49        while (len) {
50                struct pollfd pfd;
51                long wait;
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",
61                                 dev->name);
62                        return -1;
63                }
64                if (n == 0)
65                        continue;
66
67                to_read = len;
68
69                n = read(dev->fd, buffer, to_read);
70                if (n < 0) {
71                        ct_error("%s: failed to read from device: %m",
72                                 dev->name);
73                        return -1;
74                }
75                if (ct_config.debug >= 9)
76                        ifd_debug(9, "pcmcia recv:%s", ct_hexdump(buffer, n));
77                buffer += n;
78                len -= n;
79        }
80
81        return total;
82
83      timeout:                  /* Timeouts are a little special; they may happen e.g.
84                                 * when trying to obtain the ATR */
85        if (!ct_config.suppress_errors)
86                ct_error("%s: timed out while waiting for input", dev->name);
87        ifd_debug(9, "(%u bytes received so far)", total - len);
88        return IFD_ERROR_TIMEOUT;
89}
90
91/*
92 * Set pcmcia params
93 */
94static int ifd_pcmcia_set_params(ifd_device_t * dev,
95                                 const ifd_device_params_t * params)
96{
97        /* nothing to do so far */
98        dev->settings = *params;
99        return 0;
100}
101
102/*
103 * Close the device
104 */
105static void ifd_pcmcia_close(ifd_device_t * dev)
106{
107        if (dev->fd >= 0)
108                close(dev->fd);
109        dev->fd = -1;
110}
111
112static struct ifd_device_ops ifd_pcmcia_ops;
113
114/*
115 * Open serial device
116 */
117ifd_device_t *ifd_open_pcmcia(const char *name)
118{
119        ifd_device_t *dev;
120        int fd;
121
122        if ((fd = open(name, O_RDWR)) < 0) {
123                ct_error("Unable to open %s: %m", name);
124                return NULL;
125        }
126
127        ifd_pcmcia_ops.send = ifd_pcmcia_send;
128        ifd_pcmcia_ops.recv = ifd_pcmcia_recv;
129        ifd_pcmcia_ops.set_params = ifd_pcmcia_set_params;
130        ifd_pcmcia_ops.close = ifd_pcmcia_close;
131
132        dev = ifd_device_new(name, &ifd_pcmcia_ops, sizeof(*dev));
133        dev->timeout = 1000;    /* acceptable? */
134        dev->type = IFD_DEVICE_TYPE_PCMCIA;
135        dev->fd = fd;
136
137        return dev;
138}
Note: See TracBrowser for help on using the repository browser.