[pci] Add support for PCI MSI-X interrupts
[ipxe.git] / src / drivers / usb / usbhid.c
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 }