Kaynağa Gözat

[usb] Add generic USB human interface device (HID) framework

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 9 yıl önce
ebeveyn
işleme
a8e4187c45
3 değiştirilmiş dosya ile 258 ekleme ve 0 silme
  1. 151
    0
      src/drivers/usb/usbhid.c
  2. 1
    0
      src/include/ipxe/errfile.h
  3. 106
    0
      src/include/ipxe/usbhid.h

+ 151
- 0
src/drivers/usb/usbhid.c Dosyayı Görüntüle

@@ -0,0 +1,151 @@
1
+/*
2
+ * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
3
+ *
4
+ * This program is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU General Public License as
6
+ * published by the Free Software Foundation; either version 2 of the
7
+ * License, or (at your option) any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful, but
10
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+ * General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License
15
+ * along with this program; if not, write to the Free Software
16
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17
+ * 02110-1301, USA.
18
+ *
19
+ * You can also choose to distribute this program under the terms of
20
+ * the Unmodified Binary Distribution Licence (as given in the file
21
+ * COPYING.UBDL), provided that you have satisfied its requirements.
22
+ */
23
+
24
+FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
+
26
+#include <string.h>
27
+#include <errno.h>
28
+#include <ipxe/usb.h>
29
+#include <ipxe/usbhid.h>
30
+
31
+/** @file
32
+ *
33
+ * USB human interface devices (HID)
34
+ *
35
+ */
36
+
37
+/**
38
+ * Open USB human interface device
39
+ *
40
+ * @v hid		USB human interface device
41
+ * @ret rc		Return status code
42
+ */
43
+int usbhid_open ( struct usb_hid *hid ) {
44
+	int rc;
45
+
46
+	/* Open interrupt IN endpoint */
47
+	if ( ( rc = usb_endpoint_open ( &hid->in ) ) != 0 ) {
48
+		DBGC ( hid, "HID %s could not open interrupt IN: %s\n",
49
+		       hid->func->name, strerror ( rc ) );
50
+		goto err_open_in;
51
+	}
52
+
53
+	/* Refill interrupt IN endpoint */
54
+	if ( ( rc = usb_refill ( &hid->in ) ) != 0 ) {
55
+		DBGC ( hid, "HID %s could not refill interrupt IN: %s\n",
56
+		       hid->func->name, strerror ( rc ) );
57
+		goto err_refill_in;
58
+	}
59
+
60
+	/* Open interrupt OUT endpoint, if applicable */
61
+	if ( hid->out.usb &&
62
+	     ( ( rc = usb_endpoint_open ( &hid->out ) ) != 0 ) ) {
63
+		DBGC ( hid, "HID %s could not open interrupt OUT: %s\n",
64
+		       hid->func->name, strerror ( rc ) );
65
+		goto err_open_out;
66
+	}
67
+
68
+	return 0;
69
+
70
+	usb_endpoint_close ( &hid->out );
71
+ err_open_out:
72
+ err_refill_in:
73
+	usb_endpoint_close ( &hid->in );
74
+ err_open_in:
75
+	return rc;
76
+}
77
+
78
+/**
79
+ * Close USB human interface device
80
+ *
81
+ * @v hid		USB human interface device
82
+ */
83
+void usbhid_close ( struct usb_hid *hid ) {
84
+
85
+	/* Close interrupt OUT endpoint, if applicable */
86
+	if ( hid->out.usb )
87
+		usb_endpoint_close ( &hid->out );
88
+
89
+	/* Close interrupt IN endpoint */
90
+	usb_endpoint_close ( &hid->in );
91
+}
92
+
93
+/**
94
+ * Refill USB human interface device endpoints
95
+ *
96
+ * @v hid		USB human interface device
97
+ * @ret rc		Return status code
98
+ */
99
+int usbhid_refill ( struct usb_hid *hid ) {
100
+	int rc;
101
+
102
+	/* Refill interrupt IN endpoint */
103
+	if ( ( rc = usb_refill ( &hid->in ) ) != 0 )
104
+		return rc;
105
+
106
+	/* Refill interrupt OUT endpoint, if applicable */
107
+	if ( hid->out.usb && ( ( rc = usb_refill ( &hid->out ) ) != 0 ) )
108
+		return rc;
109
+
110
+	return 0;
111
+}
112
+
113
+/**
114
+ * Describe USB human interface device
115
+ *
116
+ * @v hid		USB human interface device
117
+ * @v config		Configuration descriptor
118
+ * @ret rc		Return status code
119
+ */
120
+int usbhid_describe ( struct usb_hid *hid,
121
+		      struct usb_configuration_descriptor *config ) {
122
+	struct usb_interface_descriptor *desc;
123
+	int rc;
124
+
125
+	/* Locate interface descriptor */
126
+	desc = usb_interface_descriptor ( config, hid->func->interface[0], 0 );
127
+	if ( ! desc ) {
128
+		DBGC ( hid, "HID %s has no interface descriptor\n",
129
+		       hid->func->name );
130
+		return -EINVAL;
131
+	}
132
+
133
+	/* Describe interrupt IN endpoint */
134
+	if ( ( rc = usb_endpoint_described ( &hid->in, config, desc,
135
+					     USB_INTERRUPT_IN, 0 ) ) != 0 ) {
136
+		DBGC ( hid, "HID %s could not describe interrupt IN: %s\n",
137
+		       hid->func->name, strerror ( rc ) );
138
+		return rc;
139
+	}
140
+
141
+	/* Describe interrupt OUT endpoint, if applicable */
142
+	if ( hid->out.usb &&
143
+	     ( ( rc = usb_endpoint_described ( &hid->out, config, desc,
144
+					       USB_INTERRUPT_OUT, 0 ) ) != 0 )){
145
+		DBGC ( hid, "HID %s could not describe interrupt OUT: %s\n",
146
+		       hid->func->name, strerror ( rc ) );
147
+		return rc;
148
+	}
149
+
150
+	return 0;
151
+}

+ 1
- 0
src/include/ipxe/errfile.h Dosyayı Görüntüle

@@ -81,6 +81,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
81 81
 #define ERRFILE_xhci		     ( ERRFILE_DRIVER | 0x00090000 )
82 82
 #define ERRFILE_ehci		     ( ERRFILE_DRIVER | 0x000a0000 )
83 83
 #define ERRFILE_uhci		     ( ERRFILE_DRIVER | 0x000b0000 )
84
+#define ERRFILE_usbhid		     ( ERRFILE_DRIVER | 0x000c0000 )
84 85
 
85 86
 #define ERRFILE_nvs		     ( ERRFILE_DRIVER | 0x00100000 )
86 87
 #define ERRFILE_spi		     ( ERRFILE_DRIVER | 0x00110000 )

+ 106
- 0
src/include/ipxe/usbhid.h Dosyayı Görüntüle

@@ -0,0 +1,106 @@
1
+#ifndef _IPXE_USBHID_H
2
+#define _IPXE_USBHID_H
3
+
4
+/** @file
5
+ *
6
+ * USB human interface devices (HID)
7
+ *
8
+ */
9
+
10
+FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11
+
12
+#include <ipxe/usb.h>
13
+
14
+/** Class code for human interface devices */
15
+#define USB_CLASS_HID 3
16
+
17
+/** Subclass code for boot devices */
18
+#define USB_SUBCLASS_HID_BOOT 1
19
+
20
+/** Set protocol */
21
+#define USBHID_SET_PROTOCOL						\
22
+	( USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE |		\
23
+	  USB_REQUEST_TYPE ( 0x0b ) )
24
+
25
+/** Boot protocol */
26
+#define USBHID_PROTOCOL_BOOT 0
27
+
28
+/** Report protocol */
29
+#define USBHID_PROTOCOL_REPORT 1
30
+
31
+/** Set idle time */
32
+#define USBHID_SET_IDLE							\
33
+	( USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE |		\
34
+	  USB_REQUEST_TYPE ( 0x0a ) )
35
+
36
+/** A USB human interface device */
37
+struct usb_hid {
38
+	/** USB function */
39
+	struct usb_function *func;
40
+	/** Interrupt IN endpoint */
41
+	struct usb_endpoint in;
42
+	/** Interrupt OUT endpoint (optional) */
43
+	struct usb_endpoint out;
44
+};
45
+
46
+/**
47
+ * Initialise USB human interface device
48
+ *
49
+ * @v hid		USB human interface device
50
+ * @v func		USB function
51
+ * @v in		Interrupt IN endpoint operations
52
+ * @v out		Interrupt OUT endpoint operations (or NULL)
53
+ */
54
+static inline __attribute__ (( always_inline )) void
55
+usbhid_init ( struct usb_hid *hid, struct usb_function *func,
56
+	      struct usb_endpoint_driver_operations *in,
57
+	      struct usb_endpoint_driver_operations *out ) {
58
+	struct usb_device *usb = func->usb;
59
+
60
+	hid->func = func;
61
+	usb_endpoint_init ( &hid->in, usb, in );
62
+	if ( out )
63
+		usb_endpoint_init ( &hid->out, usb, out );
64
+}
65
+
66
+/**
67
+ * Set protocol
68
+ *
69
+ * @v usb		USB device
70
+ * @v interface		Interface number
71
+ * @v protocol		HID protocol
72
+ * @ret rc		Return status code
73
+ */
74
+static inline __attribute__ (( always_inline )) int
75
+usbhid_set_protocol ( struct usb_device *usb, unsigned int interface,
76
+		      unsigned int protocol ) {
77
+
78
+	return usb_control ( usb, USBHID_SET_PROTOCOL, protocol, interface,
79
+			     NULL, 0 );
80
+}
81
+
82
+/**
83
+ * Set idle time
84
+ *
85
+ * @v usb		USB device
86
+ * @v interface		Interface number
87
+ * @v report		Report ID
88
+ * @v duration		Duration (in 4ms units)
89
+ * @ret rc		Return status code
90
+ */
91
+static inline __attribute__ (( always_inline )) int
92
+usbhid_set_idle ( struct usb_device *usb, unsigned int interface,
93
+		  unsigned int report, unsigned int duration ) {
94
+
95
+	return usb_control ( usb, USBHID_SET_IDLE,
96
+			     ( ( duration << 8 ) | report ),
97
+			     interface, NULL, 0 );
98
+}
99
+
100
+extern int usbhid_open ( struct usb_hid *hid );
101
+extern void usbhid_close ( struct usb_hid *hid );
102
+extern int usbhid_refill ( struct usb_hid *hid );
103
+extern int usbhid_describe ( struct usb_hid *hid,
104
+			     struct usb_configuration_descriptor *config );
105
+
106
+#endif /* _IPXE_USBHID_H */

Loading…
İptal
Kaydet