2 * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
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.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL
);
29 #include <ipxe/usbnet.h>
35 * USB network devices use a variety of packet formats and interface
36 * descriptors, but tend to have several features in common:
38 * - a single interrupt endpoint using the generic refill mechanism
40 * - a single bulk IN endpoint using the generic refill mechanism
42 * - a single bulk OUT endpoint
44 * - optional use of an alternate setting to enable the data interface
49 * Open USB network device
51 * @v usbnet USB network device
52 * @ret rc Return status code
54 int usbnet_open ( struct usbnet_device
*usbnet
) {
55 struct usb_device
*usb
= usbnet
->func
->usb
;
58 /* Open interrupt endpoint */
59 if ( ( rc
= usb_endpoint_open ( &usbnet
->intr
) ) != 0 ) {
60 DBGC ( usbnet
, "USBNET %s could not open interrupt: %s\n",
61 usbnet
->func
->name
, strerror ( rc
) );
65 /* Refill interrupt endpoint */
66 if ( ( rc
= usb_refill ( &usbnet
->intr
) ) != 0 ) {
67 DBGC ( usbnet
, "USBNET %s could not refill interrupt: %s\n",
68 usbnet
->func
->name
, strerror ( rc
) );
72 /* Select alternate setting for data interface, if applicable */
73 if ( usbnet
->alternate
&&
74 ( ( rc
= usb_set_interface ( usb
, usbnet
->data
,
75 usbnet
->alternate
) ) != 0 ) ) {
76 DBGC ( usbnet
, "USBNET %s could not set alternate interface "
77 "%d: %s\n", usbnet
->func
->name
, usbnet
->alternate
,
79 goto err_set_interface
;
82 /* Open bulk IN endpoint */
83 if ( ( rc
= usb_endpoint_open ( &usbnet
->in
) ) != 0 ) {
84 DBGC ( usbnet
, "USBNET %s could not open bulk IN: %s\n",
85 usbnet
->func
->name
, strerror ( rc
) );
89 /* Open bulk OUT endpoint */
90 if ( ( rc
= usb_endpoint_open ( &usbnet
->out
) ) != 0 ) {
91 DBGC ( usbnet
, "USBNET %s could not open bulk OUT: %s\n",
92 usbnet
->func
->name
, strerror ( rc
) );
96 /* Refill bulk IN endpoint */
97 if ( ( rc
= usb_refill ( &usbnet
->in
) ) != 0 ) {
98 DBGC ( usbnet
, "USBNET %s could not refill bulk IN: %s\n",
99 usbnet
->func
->name
, strerror ( rc
) );
106 usb_endpoint_close ( &usbnet
->out
);
108 usb_endpoint_close ( &usbnet
->in
);
110 if ( usbnet
->alternate
)
111 usb_set_interface ( usb
, usbnet
->data
, 0 );
114 usb_endpoint_close ( &usbnet
->intr
);
120 * Close USB network device
122 * @v usbnet USB network device
124 void usbnet_close ( struct usbnet_device
*usbnet
) {
125 struct usb_device
*usb
= usbnet
->func
->usb
;
127 /* Close bulk OUT endpoint */
128 usb_endpoint_close ( &usbnet
->out
);
130 /* Close bulk IN endpoint */
131 usb_endpoint_close ( &usbnet
->in
);
133 /* Reset alternate setting for data interface, if applicable */
134 if ( usbnet
->alternate
)
135 usb_set_interface ( usb
, usbnet
->data
, 0 );
137 /* Close interrupt endpoint */
138 usb_endpoint_close ( &usbnet
->intr
);
142 * Refill USB network device bulk IN and interrupt endpoints
144 * @v usbnet USB network device
145 * @ret rc Return status code
147 int usbnet_refill ( struct usbnet_device
*usbnet
) {
150 /* Refill bulk IN endpoint */
151 if ( ( rc
= usb_refill ( &usbnet
->in
) ) != 0 )
154 /* Refill interrupt endpoint */
155 if ( ( rc
= usb_refill ( &usbnet
->intr
) ) != 0 )
162 * Describe communications interface and interrupt endpoint
164 * @v usbnet USB network device
165 * @v config Configuration descriptor
166 * @ret rc Return status code
168 static int usbnet_comms_describe ( struct usbnet_device
*usbnet
,
169 struct usb_configuration_descriptor
*config
){
170 struct usb_interface_descriptor
*desc
;
175 /* Iterate over all available interfaces */
176 for ( i
= 0 ; i
< usbnet
->func
->count
; i
++ ) {
178 /* Get interface number */
179 comms
= usbnet
->func
->interface
[i
];
181 /* Locate interface descriptor */
182 desc
= usb_interface_descriptor ( config
, comms
, 0 );
186 /* Describe interrupt endpoint */
187 if ( ( rc
= usb_endpoint_described ( &usbnet
->intr
, config
,
188 desc
, USB_INTERRUPT_IN
,
192 /* Record communications interface */
193 usbnet
->comms
= comms
;
194 DBGC ( usbnet
, "USBNET %s found communications interface %d\n",
195 usbnet
->func
->name
, comms
);
199 DBGC ( usbnet
, "USBNET %s found no communications interface\n",
200 usbnet
->func
->name
);
205 * Describe data interface and bulk endpoints
207 * @v usbnet USB network device
208 * @v config Configuration descriptor
209 * @ret rc Return status code
211 static int usbnet_data_describe ( struct usbnet_device
*usbnet
,
212 struct usb_configuration_descriptor
*config
){
213 struct usb_interface_descriptor
*desc
;
219 /* Iterate over all available interfaces */
220 for ( i
= 0 ; i
< usbnet
->func
->count
; i
++ ) {
222 /* Get interface number */
223 data
= usbnet
->func
->interface
[i
];
225 /* Iterate over all existent alternate settings */
226 for ( alt
= 0 ; ; alt
++ ) {
228 /* Locate interface descriptor */
229 desc
= usb_interface_descriptor ( config
, data
, alt
);
233 /* Describe bulk IN endpoint */
234 if ( ( rc
= usb_endpoint_described ( &usbnet
->in
,
240 /* Describe bulk OUT endpoint */
241 if ( ( rc
= usb_endpoint_described ( &usbnet
->out
,
247 /* Record data interface and alternate setting */
249 usbnet
->alternate
= alt
;
250 DBGC ( usbnet
, "USBNET %s found data interface %d",
251 usbnet
->func
->name
, data
);
253 DBGC ( usbnet
, " using alternate %d", alt
);
254 DBGC ( usbnet
, "\n" );
259 DBGC ( usbnet
, "USBNET %s found no data interface\n",
260 usbnet
->func
->name
);
265 * Describe USB network device interfaces
267 * @v usbnet USB network device
268 * @v config Configuration descriptor
269 * @ret rc Return status code
271 int usbnet_describe ( struct usbnet_device
*usbnet
,
272 struct usb_configuration_descriptor
*config
) {
275 /* Describe communications interface */
276 if ( ( rc
= usbnet_comms_describe ( usbnet
, config
) ) != 0 )
279 /* Describe data interface */
280 if ( ( rc
= usbnet_data_describe ( usbnet
, config
) ) != 0 )