usb4bsd: Bring in FreeBSD's libusbhid, usbhidctl and USB kernel code.
[dragonfly.git] / sys / bus / u4b / usb_dev.h
1 /* $FreeBSD$ */
2 /*-
3  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #ifndef _USB_DEV_H_
28 #define _USB_DEV_H_
29
30 #include <sys/file.h>
31 #include <sys/selinfo.h>
32 #include <sys/poll.h>
33 #include <sys/signalvar.h>
34 #include <sys/proc.h>
35
36 struct usb_fifo;
37 struct usb_mbuf;
38
39 struct usb_symlink {
40         TAILQ_ENTRY(usb_symlink) sym_entry;
41         char    src_path[32];           /* Source path - including terminating
42                                          * zero */
43         char    dst_path[32];           /* Destination path - including
44                                          * terminating zero */
45         uint8_t src_len;                /* String length */
46         uint8_t dst_len;                /* String length */
47 };
48
49 /*
50  * Private per-device information.
51  */
52 struct usb_cdev_privdata {
53         struct usb_bus          *bus;
54         struct usb_device       *udev;
55         struct usb_interface    *iface;
56         int                     bus_index;      /* bus index */
57         int                     dev_index;      /* device index */
58         int                     ep_addr;        /* endpoint address */
59         int                     fflags;
60         uint8_t                 fifo_index;     /* FIFO index */
61 };
62
63 /*
64  * The following structure defines a minimum re-implementation of the
65  * ifqueue structure in the kernel.
66  */
67 struct usb_ifqueue {
68         struct usb_mbuf *ifq_head;
69         struct usb_mbuf *ifq_tail;
70
71         usb_size_t ifq_len;
72         usb_size_t ifq_maxlen;
73 };
74
75 /*
76  * Private per-device and per-thread reference information
77  */
78 struct usb_cdev_refdata {
79         struct usb_fifo         *rxfifo;
80         struct usb_fifo         *txfifo;
81         uint8_t                 is_read;        /* location has read access */
82         uint8_t                 is_write;       /* location has write access */
83         uint8_t                 is_uref;        /* USB refcount decr. needed */
84         uint8_t                 is_usbfs;       /* USB-FS is active */
85 };
86
87 struct usb_fs_privdata {
88         int bus_index;
89         int dev_index;
90         int ep_addr;
91         int mode;
92         int fifo_index;
93         struct cdev *cdev;
94
95         LIST_ENTRY(usb_fs_privdata) pd_next;
96 };
97
98 /*
99  * Most of the fields in the "usb_fifo" structure are used by the
100  * generic USB access layer.
101  */
102 struct usb_fifo {
103         struct usb_ifqueue free_q;
104         struct usb_ifqueue used_q;
105         struct selinfo selinfo;
106         struct cv cv_io;
107         struct cv cv_drain;
108         struct usb_fifo_methods *methods;
109         struct usb_symlink *symlink[2];/* our symlinks */
110         struct proc *async_p;           /* process that wants SIGIO */
111         struct usb_fs_endpoint *fs_ep_ptr;
112         struct usb_device *udev;
113         struct usb_xfer *xfer[2];
114         struct usb_xfer **fs_xfer;
115         struct mtx *priv_mtx;           /* client data */
116         /* set if FIFO is opened by a FILE: */
117         struct usb_cdev_privdata *curr_cpd;
118         void   *priv_sc0;               /* client data */
119         void   *priv_sc1;               /* client data */
120         void   *queue_data;
121         usb_timeout_t timeout;          /* timeout in milliseconds */
122         usb_frlength_t bufsize;         /* BULK and INTERRUPT buffer size */
123         usb_frcount_t nframes;          /* for isochronous mode */
124         uint16_t dev_ep_index;          /* our device endpoint index */
125         uint8_t flag_sleeping;          /* set if FIFO is sleeping */
126         uint8_t flag_iscomplete;        /* set if a USB transfer is complete */
127         uint8_t flag_iserror;           /* set if FIFO error happened */
128         uint8_t flag_isselect;          /* set if FIFO is selected */
129         uint8_t flag_flushing;          /* set if FIFO is flushing data */
130         uint8_t flag_short;             /* set if short_ok or force_short
131                                          * transfer flags should be set */
132         uint8_t flag_stall;             /* set if clear stall should be run */
133         uint8_t flag_write_defrag;      /* set to defrag written data */
134         uint8_t flag_have_fragment;     /* set if defragging */
135         uint8_t iface_index;            /* set to the interface we belong to */
136         uint8_t fifo_index;             /* set to the FIFO index in "struct
137                                          * usb_device" */
138         uint8_t fs_ep_max;
139         uint8_t fifo_zlp;               /* zero length packet count */
140         uint8_t refcount;
141 #define USB_FIFO_REF_MAX 0xFF
142 };
143
144 extern struct cdevsw usb_devsw;
145
146 int     usb_fifo_wait(struct usb_fifo *fifo);
147 void    usb_fifo_signal(struct usb_fifo *fifo);
148 uint8_t usb_fifo_opened(struct usb_fifo *fifo);
149 struct usb_symlink *usb_alloc_symlink(const char *target);
150 void    usb_free_symlink(struct usb_symlink *ps);
151 int     usb_read_symlink(uint8_t *user_ptr, uint32_t startentry,
152             uint32_t user_len);
153
154 #endif                                  /* _USB_DEV_H_ */