File: freebsdusb.c

package info (click to toggle)
pilot-link 0.12.5-dfsg-2
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 6,868 kB
  • ctags: 5,811
  • sloc: ansic: 53,153; sh: 10,459; java: 2,584; perl: 2,247; python: 1,044; makefile: 991; yacc: 662; cpp: 551; xml: 39
file content (460 lines) | stat: -rw-r--r-- 12,911 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
 * $Id: freebsdusb.c,v 1.28 2006/10/12 14:21:22 desrod Exp $
 *
 * freebsdusb.c: device IO for FreeBSD usb devices
 *
 * Copyright (c) 1996, 1997, D. Jeff Dionne & Kenneth Albanowski.
 *               2002, Anish Mistry
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Library General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library
 * General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>

#include "pi-debug.h"
#include "pi-source.h"
#include "pi-usb.h"

#ifdef HAVE_SYS_IOCTL_COMPAT_H
#include <sys/ioctl_compat.h>
#endif

#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif

#ifndef O_NONBLOCK
# define O_NONBLOCK 0
#endif

#if defined(__FreeBSD__)
/* freebsd usb header */
#include <dev/usb/usb.h>
#define MAX_BUF 256
#endif

/* Declare prototypes */
static int u_open(pi_socket_t *ps, struct pi_sockaddr *addr, size_t addrlen);
static int u_close(pi_socket_t *ps);
static ssize_t u_write(pi_socket_t *ps, const unsigned char *buf, size_t len, int flags);
static ssize_t u_read(pi_socket_t *ps, pi_buffer_t *buf, size_t len, int flags);
static int u_poll(pi_socket_t *ps, int timeout);
static int u_flush(pi_socket_t *ps, int flags);

void pi_usb_impl_init (struct pi_usb_impl *impl)
{
	impl->open 	= u_open;
	impl->close 	= u_close;
	impl->write 	= u_write;
	impl->read 	= u_read;
	impl->flush	= u_flush;
	impl->poll 	= u_poll;
}


/***********************************************************************
 *
 * Function:    u_open
 *
 * Summary:     Open the usb port and establish a connection for
 *		communicating over the usb port.
 *
 * Parameters:  ps is of type pi_socket_t which will given a copy of the
 *              valid file descriptor that is returned by the function.
 *
 * 		addr of type pi_socketaddr contains the member pi_device
 *              which is a character string of the usb device.
 *
 *		XXX addrlen is of type int contain the size of addr?  I'm
 *		not using this, should I?  Possible buffer overflow because
 *		I'm not checking something?
 *
 * Returns:     The file descriptor
 *
 ***********************************************************************/
static int
u_open(pi_socket_t *ps, struct pi_sockaddr *addr, size_t addrlen)
{
	int 	fd,
		i,
		endpoint_fd;

	struct 	usb_device_info udi;
	/* struct 	usb_ctl_request ur;	*/
	/* unsigned char usbresponse[50];	*/

	char 	*tty 		= addr->pi_device;
	char 	*pEndPoint 	= NULL;

	/* open the usb device */
	if ((fd = open(tty, O_RDWR, 0)) < 0) {
		ps->last_error = PI_ERR_GENERIC_SYSTEM;
		return PI_ERR_GENERIC_SYSTEM;
	}

	/* check for a valid file descriptor */
	if (fd < 0) {
		ps->last_error = PI_ERR_GENERIC_SYSTEM;
		return PI_ERR_GENERIC_SYSTEM;
	}

	/* fill the udi structure with information about the handheld, after
	   this is where you will probably want to do device specific stuff
	   */

	if (ioctl(fd, USB_GET_DEVICEINFO, &udi)) {
		close(fd);
		ps->last_error = PI_ERR_GENERIC_SYSTEM;
		return PI_ERR_GENERIC_SYSTEM;
	}

	LOG((PI_DBG_DEV, PI_DBG_LVL_INFO,
		"DEV USB_GET_DEVICE_INFO USB FreeBSD fd: %d\n", fd));

	/* set the configuration */
	i = 1;
	if (ioctl(fd, USB_SET_CONFIG, &i) < 0) {
		LOG((PI_DBG_DEV, PI_DBG_LVL_ERR,
		 "DEV USB_SET_CONFIG USB FreeBSD fd: %d failed\n", fd));

		close(fd);
		ps->last_error = PI_ERR_GENERIC_SYSTEM;
		return PI_ERR_GENERIC_SYSTEM;
	}

	/* close the main communication pipe since we have initilized
	  everything we needed to
	NOTE: we HAVE to do all this stuff to the main pipe or we will
	 cause a kernel panic when data is sent over the endpoint */
	close(fd);

	/* open endpoint */
	/* allocate data for the usb endpoint string */
	pEndPoint = malloc(strlen(tty)+20);
	if(!pEndPoint)
		return pi_set_error(ps->sd, PI_ERR_GENERIC_MEMORY);

	/* create device endpoint name string */
	sprintf(pEndPoint, "%s.%d", tty, 2);

	/* open the endpoint with read write access */
	endpoint_fd = open(pEndPoint, O_RDWR, 0);
	if(endpoint_fd < 0) {
		/* we failed to open the endpoint */
		free(pEndPoint);
		return pi_set_error(ps->sd, PI_ERR_GENERIC_SYSTEM);
	}

	if ((i = fcntl(endpoint_fd, F_GETFL, 0)) != -1) {
		i &= ~O_NONBLOCK;
		fcntl(endpoint_fd, F_SETFL, i);
	}

	/* set short transfer so that we can allow "short" reads since we
	   will don't know exactly
	what is coming so we can't specify exact byte amounts */
	i = 1;
	if (ioctl(endpoint_fd, USB_SET_SHORT_XFER, &i) < 0) {
		LOG((PI_DBG_DEV, PI_DBG_LVL_WARN,
		 "DEV USB_SET_SHORT_XFER USB FreeBSD fd: %d failed\n",
			endpoint_fd));
	}

	/* 0 timeout value will cause us the wait until the device has data
           available or is disconnected */
	i = 0;
	if (ioctl(endpoint_fd, USB_SET_TIMEOUT, &i) < 0) {
		LOG((PI_DBG_DEV, PI_DBG_LVL_WARN,
		 "DEV USB_SET_TIMEOUT USB FreeBSD fd: %d failed\n",
			endpoint_fd));
	}

	/* save our file descriptor in the pi_socket structure */
	if ((i = pi_socket_setsd(ps, endpoint_fd)) < 0) {
		free(pEndPoint);
		return i;
	}

	/* free endpoint string memory */
	free(pEndPoint);

	LOG((PI_DBG_DEV, PI_DBG_LVL_INFO,
		"DEV OPEN USB FreeBSD fd: %d\n", endpoint_fd));

	/* return our endpoint file descriptor since this is where the
	   reading and writing will be done */
	return endpoint_fd;

}

/***********************************************************************
 *
 * Function:    u_close
 *
 * Summary:     Close the open socket/file descriptor
 *
 * Parameters:  ps is of type pi_socket that contains the member sd which is
 *              the file descriptor that is to be closed.
 *
 * Returns:     0 if sucessful or -1 if it failed and errno set to the error
 *
 ***********************************************************************/
static int
u_close(pi_socket_t *ps)
{
	LOG((PI_DBG_DEV, PI_DBG_LVL_INFO,
		"DEV CLOSE USB FreeBSD fd: %d\n", ps->sd));

	return close(ps->sd);
}

/***********************************************************************
 *
 * Function:    u_poll
 *
 * Summary:     Poll the open socket/file descriptor
 *
 * Parameters:	None
 *
 * Returns:     1 always since we buffer our own data and you can't poll()
 *		FreeBSD USB devices?
 *
 ***********************************************************************/
static int
u_poll(pi_socket_t *ps, int timeout)
{
	/* stub this function and log an error that this should never needed
	   to be called */
	LOG((PI_DBG_DEV, PI_DBG_LVL_WARN,
		"DEV POLL USB FreeBSD Timeout: %d\npoll()"
		" should not be called for FreeBSD USB\n", timeout));

	return 1;
}

/***********************************************************************
 *
 * Function:    u_write
 *
 * Parameters:	ps is of type pi_socket that contains the sd member which is
 *              the file descriptor that the data in buf will be written to.
 *
 *              buf is a unsigned char pointer that points to the data that
 *              is to be written to ps->sd.
 *
 *		len is of type int and indicated the number of bytes to
 *              write to ps->sd.
 *
 *		flags is of type int and contains various write flags.  What
 *              flags and should I respect them, since I am currently not
 *              checking this variable?
 *
 * Parameters:  None
 *
 * Returns:     The number of bytes written from buf or negative to indicate an
 * 		error
 *
 ***********************************************************************/
static ssize_t
u_write(pi_socket_t *ps, const unsigned char *buf, size_t len, int flags)
{
	int 	nwrote,
		total,
		write_len;
	fd_set 	ready;

	total 		= len;
	write_len 	= len;

	/* FIXME: there is no timeout handling in the original freebsdusb code! */

	while (total > 0) {
		FD_ZERO(&ready);
		FD_SET(ps->sd, &ready);

		if (!FD_ISSET(ps->sd, &ready)) {
			ps->state = PI_SOCK_CONN_BREAK;
			return pi_set_error(ps->sd, PI_ERR_SOCK_DISCONNECTED);
		}

		nwrote = write(ps->sd, buf, write_len);
		if (nwrote < 0) {
			ps->state = PI_SOCK_CONN_BREAK;
			return pi_set_error(ps->sd, PI_ERR_SOCK_DISCONNECTED);
		}

		write_len -= nwrote;
		buf += nwrote;
		total -= nwrote;
	}

	LOG((PI_DBG_DEV, PI_DBG_LVL_INFO,
		"DEV TX USB FreeBSD Bytes: %d\n", len));

	return len;
}

/***********************************************************************
 *
 * Function:    u_read
 *
 * Summary:	Read incoming data from the socket/file descriptor and
 *              buffer any extra data that is read in the process of
 *              receiving data from the device.
 *
 * Parameters:	ps is of type pi_socket that contains the sd member which is
 *              the file descriptor that the data in buf will be read. It
 *              also contains the read buffer.
 *
 *		buf is a pointer to an unsigned character string that will
 *              receive the data the is read from the device.
 *
 *		len is of type int that is the the number of bytes to read
 *              from the device/buffer into buf.
 *
 *		flags is of type in that currently only indicated if you
 *              want to peek data from the device/buffer .
 *
 * Returns:	The number of bytes that was read from the device or from
 *              the buffer and copied to buf.
 *
 ***********************************************************************/
static ssize_t
u_read(pi_socket_t *ps, pi_buffer_t *buf, size_t len, int flags)
{
	struct pi_usb_data *data = (struct pi_usb_data *) ps->device->data;
	ssize_t	rlen;
	int bytes_read = 0;
	fd_set ready;
	struct timeval t;

	if (flags == PI_MSG_PEEK && len > 256)
		len = 256;

	if (pi_buffer_expect (buf, len) == NULL) {
		errno = ENOMEM;
		return pi_set_error(ps->sd, PI_ERR_GENERIC_MEMORY);
	}

	/* first extract anything we had in the "peek" buffer */
	if (data->buf_size > 0) {
		bytes_read = len > data->buf_size ? data->buf_size : len;
		len -= bytes_read;
		pi_buffer_append(buf, data->buf, bytes_read);
		if (flags != PI_MSG_PEEK) {
			data->buf_size -= bytes_read;
			if (data->buf_size > 0)
				memmove(data->buf, data->buf + bytes_read, data->buf_size);
		}
		if (!len)
			return bytes_read;
	}

	/* check to see if device is ready for read */
	FD_ZERO(&ready);
	FD_SET(ps->sd, &ready);

	/* If timeout == 0, wait forever for packet, otherwise wait till
	   timeout milliseconds */
	if (data->timeout == 0)
		select(ps->sd + 1, &ready, 0, 0, 0);
	else {
		t.tv_sec 	= data->timeout / 1000;
		t.tv_usec 	= (data->timeout % 1000) * 1000;
		if (select(ps->sd + 1, &ready, 0, 0, &t) == 0)
			return pi_set_error(ps->sd, PI_ERR_SOCK_TIMEOUT);
	}

	/* If data is available in time, read it */
	if (!FD_ISSET(ps->sd, &ready)) {
		LOG((PI_DBG_DEV, PI_DBG_LVL_WARN,
			 "DEV RX USB FreeBSD timeout\n"));
		errno = ETIMEDOUT;
		return pi_set_error(ps->sd, PI_ERR_SOCK_TIMEOUT);
	}

	/* read data to pre-sized buffer */
	rlen = read(ps->sd, &buf->data[buf->used], len);
	if (rlen > 0) {
		if (flags == PI_MSG_PEEK) {
			memcpy(data->buf, buf->data + buf->used, rlen);
			data->buf_size = rlen;
		}
		buf->used += rlen;
		bytes_read += rlen;

		LOG((PI_DBG_DEV, PI_DBG_LVL_INFO,
			"DEV RX USB FreeBSD Bytes: %d:%d\n", bytes_read,
			bytes_read + data->buf_size));

	}
	else
		bytes_read = PI_ERR_SOCK_IO;

	return bytes_read;
}

/***********************************************************************
 *
 * Function:    u_flush
 *
 * Summary:	Flush incoming and/or outgoing data from the socket/file
 *		descriptor
 *
 * Parameters:	ps is of type pi_socket that contains the sd member which is
 *              the file descriptor that the data in buf will be read. It
 *              also contains the read buffer.
 *
 *		flags is of type int and can be a combination of
 *		PI_FLUSH_INPUT and PI_FLUSH_OUTPUT
 *
 * Returns:	0
 *
 ***********************************************************************/
static int
u_flush(pi_socket_t *ps, int flags)
{
	char buf[256];
	int fl;
	struct pi_usb_data *data = (struct pi_usb_data *) ps->device->data;

	if (flags & PI_FLUSH_INPUT) {
		/* clear internal buffer */
		data->buf_size = 0;

		/* flush pending data */
		if ((fl = fcntl(ps->sd, F_GETFL, 0)) != -1) {
			fcntl(ps->sd, F_SETFL, fl | O_NONBLOCK);
			while (read(ps->sd, buf, sizeof(buf)) > 0)
				;
			fcntl(ps->sd, F_SETFL, fl);
		}
	}
	return 0;
}

/* vi: set ts=8 sw=4 sts=4 noexpandtab: cin */
/* ex: set tabstop=4 expandtab: */
/* Local Variables: */
/* indent-tabs-mode: t */
/* c-basic-offset: 8 */
/* End: */