if_umb.c revision 1.4.4.4 1 1.4.4.4 pgoyette /* $NetBSD: if_umb.c,v 1.4.4.4 2018/11/26 01:52:47 pgoyette Exp $ */
2 1.4.4.3 pgoyette /* $OpenBSD: if_umb.c,v 1.20 2018/09/10 17:00:45 gerhard Exp $ */
3 1.4.4.2 pgoyette
4 1.4.4.2 pgoyette /*
5 1.4.4.2 pgoyette * Copyright (c) 2016 genua mbH
6 1.4.4.2 pgoyette * All rights reserved.
7 1.4.4.2 pgoyette *
8 1.4.4.2 pgoyette * Permission to use, copy, modify, and distribute this software for any
9 1.4.4.2 pgoyette * purpose with or without fee is hereby granted, provided that the above
10 1.4.4.2 pgoyette * copyright notice and this permission notice appear in all copies.
11 1.4.4.2 pgoyette *
12 1.4.4.2 pgoyette * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 1.4.4.2 pgoyette * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 1.4.4.2 pgoyette * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 1.4.4.2 pgoyette * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 1.4.4.2 pgoyette * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 1.4.4.2 pgoyette * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 1.4.4.2 pgoyette * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 1.4.4.2 pgoyette */
20 1.4.4.2 pgoyette
21 1.4.4.2 pgoyette /*
22 1.4.4.2 pgoyette * Mobile Broadband Interface Model specification:
23 1.4.4.2 pgoyette * http://www.usb.org/developers/docs/devclass_docs/MBIM10Errata1_073013.zip
24 1.4.4.2 pgoyette * Compliance testing guide
25 1.4.4.2 pgoyette * http://www.usb.org/developers/docs/devclass_docs/MBIM-Compliance-1.0.pdf
26 1.4.4.2 pgoyette */
27 1.4.4.2 pgoyette
28 1.4.4.2 pgoyette #include <sys/cdefs.h>
29 1.4.4.4 pgoyette __KERNEL_RCSID(0, "$NetBSD: if_umb.c,v 1.4.4.4 2018/11/26 01:52:47 pgoyette Exp $");
30 1.4.4.2 pgoyette
31 1.4.4.2 pgoyette #ifdef _KERNEL_OPT
32 1.4.4.2 pgoyette #include "opt_inet.h"
33 1.4.4.2 pgoyette #endif
34 1.4.4.2 pgoyette
35 1.4.4.2 pgoyette #include <sys/param.h>
36 1.4.4.2 pgoyette #include <sys/device.h>
37 1.4.4.2 pgoyette #include <sys/endian.h>
38 1.4.4.2 pgoyette #include <sys/kauth.h>
39 1.4.4.2 pgoyette #include <sys/kernel.h>
40 1.4.4.2 pgoyette #include <sys/kmem.h>
41 1.4.4.2 pgoyette #include <sys/mbuf.h>
42 1.4.4.2 pgoyette #include <sys/rndsource.h>
43 1.4.4.2 pgoyette #include <sys/socket.h>
44 1.4.4.2 pgoyette #include <sys/syslog.h>
45 1.4.4.2 pgoyette #include <sys/systm.h>
46 1.4.4.2 pgoyette
47 1.4.4.2 pgoyette #include <net/bpf.h>
48 1.4.4.2 pgoyette #include <net/if.h>
49 1.4.4.2 pgoyette #include <net/if_media.h>
50 1.4.4.2 pgoyette #include <net/if_types.h>
51 1.4.4.2 pgoyette
52 1.4.4.2 pgoyette #ifdef INET
53 1.4.4.2 pgoyette #include <netinet/in.h>
54 1.4.4.2 pgoyette #include <netinet/if_inarp.h>
55 1.4.4.2 pgoyette #include <netinet/in_var.h>
56 1.4.4.2 pgoyette #include <netinet/ip.h>
57 1.4.4.2 pgoyette #endif
58 1.4.4.2 pgoyette
59 1.4.4.2 pgoyette #include <dev/usb/usb.h>
60 1.4.4.2 pgoyette #include <dev/usb/usbdi.h>
61 1.4.4.2 pgoyette #include <dev/usb/usbdivar.h>
62 1.4.4.2 pgoyette #include <dev/usb/usbdi_util.h>
63 1.4.4.2 pgoyette #include <dev/usb/usbdevs.h>
64 1.4.4.2 pgoyette #include <dev/usb/usbcdc.h>
65 1.4.4.2 pgoyette
66 1.4.4.2 pgoyette #include <dev/usb/mbim.h>
67 1.4.4.2 pgoyette #include <dev/usb/if_umbreg.h>
68 1.4.4.2 pgoyette
69 1.4.4.2 pgoyette #ifdef UMB_DEBUG
70 1.4.4.2 pgoyette #define DPRINTF(x...) \
71 1.4.4.2 pgoyette do { if (umb_debug) log(LOG_DEBUG, x); } while (0)
72 1.4.4.2 pgoyette
73 1.4.4.2 pgoyette #define DPRINTFN(n, x...) \
74 1.4.4.2 pgoyette do { if (umb_debug >= (n)) log(LOG_DEBUG, x); } while (0)
75 1.4.4.2 pgoyette
76 1.4.4.2 pgoyette #define DDUMPN(n, b, l) \
77 1.4.4.2 pgoyette do { \
78 1.4.4.2 pgoyette if (umb_debug >= (n)) \
79 1.4.4.2 pgoyette umb_dump((b), (l)); \
80 1.4.4.2 pgoyette } while (0)
81 1.4.4.2 pgoyette
82 1.4.4.2 pgoyette int umb_debug = 0;
83 1.4.4.2 pgoyette Static char *umb_uuid2str(uint8_t [MBIM_UUID_LEN]);
84 1.4.4.2 pgoyette Static void umb_dump(void *, int);
85 1.4.4.2 pgoyette
86 1.4.4.2 pgoyette #else
87 1.4.4.2 pgoyette #define DPRINTF(x...) do { } while (0)
88 1.4.4.2 pgoyette #define DPRINTFN(n, x...) do { } while (0)
89 1.4.4.2 pgoyette #define DDUMPN(n, b, l) do { } while (0)
90 1.4.4.2 pgoyette #endif
91 1.4.4.2 pgoyette
92 1.4.4.2 pgoyette #define DEVNAM(sc) device_xname((sc)->sc_dev)
93 1.4.4.2 pgoyette
94 1.4.4.2 pgoyette /*
95 1.4.4.2 pgoyette * State change timeout
96 1.4.4.2 pgoyette */
97 1.4.4.2 pgoyette #define UMB_STATE_CHANGE_TIMEOUT 30
98 1.4.4.2 pgoyette
99 1.4.4.2 pgoyette /*
100 1.4.4.2 pgoyette * State change flags
101 1.4.4.2 pgoyette */
102 1.4.4.2 pgoyette #define UMB_NS_DONT_DROP 0x0001 /* do not drop below current state */
103 1.4.4.2 pgoyette #define UMB_NS_DONT_RAISE 0x0002 /* do not raise below current state */
104 1.4.4.2 pgoyette
105 1.4.4.2 pgoyette /*
106 1.4.4.2 pgoyette * Diagnostic macros
107 1.4.4.2 pgoyette */
108 1.4.4.2 pgoyette const struct umb_valdescr umb_regstates[] = MBIM_REGSTATE_DESCRIPTIONS;
109 1.4.4.2 pgoyette const struct umb_valdescr umb_dataclasses[] = MBIM_DATACLASS_DESCRIPTIONS;
110 1.4.4.2 pgoyette const struct umb_valdescr umb_simstate[] = MBIM_SIMSTATE_DESCRIPTIONS;
111 1.4.4.2 pgoyette const struct umb_valdescr umb_messages[] = MBIM_MESSAGES_DESCRIPTIONS;
112 1.4.4.2 pgoyette const struct umb_valdescr umb_status[] = MBIM_STATUS_DESCRIPTIONS;
113 1.4.4.2 pgoyette const struct umb_valdescr umb_cids[] = MBIM_CID_DESCRIPTIONS;
114 1.4.4.2 pgoyette const struct umb_valdescr umb_pktstate[] = MBIM_PKTSRV_STATE_DESCRIPTIONS;
115 1.4.4.2 pgoyette const struct umb_valdescr umb_actstate[] = MBIM_ACTIVATION_STATE_DESCRIPTIONS;
116 1.4.4.2 pgoyette const struct umb_valdescr umb_error[] = MBIM_ERROR_DESCRIPTIONS;
117 1.4.4.2 pgoyette const struct umb_valdescr umb_pintype[] = MBIM_PINTYPE_DESCRIPTIONS;
118 1.4.4.2 pgoyette const struct umb_valdescr umb_istate[] = UMB_INTERNAL_STATE_DESCRIPTIONS;
119 1.4.4.2 pgoyette
120 1.4.4.2 pgoyette #define umb_regstate(c) umb_val2descr(umb_regstates, (c))
121 1.4.4.2 pgoyette #define umb_dataclass(c) umb_val2descr(umb_dataclasses, (c))
122 1.4.4.2 pgoyette #define umb_simstate(s) umb_val2descr(umb_simstate, (s))
123 1.4.4.2 pgoyette #define umb_request2str(m) umb_val2descr(umb_messages, (m))
124 1.4.4.2 pgoyette #define umb_status2str(s) umb_val2descr(umb_status, (s))
125 1.4.4.2 pgoyette #define umb_cid2str(c) umb_val2descr(umb_cids, (c))
126 1.4.4.2 pgoyette #define umb_packet_state(s) umb_val2descr(umb_pktstate, (s))
127 1.4.4.2 pgoyette #define umb_activation(s) umb_val2descr(umb_actstate, (s))
128 1.4.4.2 pgoyette #define umb_error2str(e) umb_val2descr(umb_error, (e))
129 1.4.4.2 pgoyette #define umb_pin_type(t) umb_val2descr(umb_pintype, (t))
130 1.4.4.2 pgoyette #define umb_istate(s) umb_val2descr(umb_istate, (s))
131 1.4.4.2 pgoyette
132 1.4.4.2 pgoyette Static int umb_match(device_t, cfdata_t, void *);
133 1.4.4.2 pgoyette Static void umb_attach(device_t, device_t, void *);
134 1.4.4.2 pgoyette Static int umb_detach(device_t, int);
135 1.4.4.2 pgoyette Static int umb_activate(device_t, enum devact);
136 1.4.4.2 pgoyette Static void umb_ncm_setup(struct umb_softc *);
137 1.4.4.2 pgoyette Static int umb_alloc_xfers(struct umb_softc *);
138 1.4.4.2 pgoyette Static void umb_free_xfers(struct umb_softc *);
139 1.4.4.2 pgoyette Static int umb_alloc_bulkpipes(struct umb_softc *);
140 1.4.4.2 pgoyette Static void umb_close_bulkpipes(struct umb_softc *);
141 1.4.4.2 pgoyette Static int umb_ioctl(struct ifnet *, u_long, void *);
142 1.4.4.2 pgoyette Static int umb_output(struct ifnet *, struct mbuf *,
143 1.4.4.2 pgoyette const struct sockaddr *, const struct rtentry *);
144 1.4.4.2 pgoyette Static void umb_input(struct ifnet *, struct mbuf *);
145 1.4.4.2 pgoyette Static void umb_start(struct ifnet *);
146 1.4.4.2 pgoyette Static void umb_watchdog(struct ifnet *);
147 1.4.4.2 pgoyette Static void umb_statechg_timeout(void *);
148 1.4.4.2 pgoyette
149 1.4.4.2 pgoyette Static int umb_mediachange(struct ifnet *);
150 1.4.4.2 pgoyette Static void umb_mediastatus(struct ifnet *, struct ifmediareq *);
151 1.4.4.2 pgoyette
152 1.4.4.2 pgoyette Static void umb_newstate(struct umb_softc *, enum umb_state, int);
153 1.4.4.2 pgoyette Static void umb_state_task(void *);
154 1.4.4.2 pgoyette Static void umb_up(struct umb_softc *);
155 1.4.4.2 pgoyette Static void umb_down(struct umb_softc *, int);
156 1.4.4.2 pgoyette
157 1.4.4.2 pgoyette Static void umb_get_response_task(void *);
158 1.4.4.2 pgoyette
159 1.4.4.2 pgoyette Static void umb_decode_response(struct umb_softc *, void *, int);
160 1.4.4.2 pgoyette Static void umb_handle_indicate_status_msg(struct umb_softc *, void *,
161 1.4.4.2 pgoyette int);
162 1.4.4.2 pgoyette Static void umb_handle_opendone_msg(struct umb_softc *, void *, int);
163 1.4.4.2 pgoyette Static void umb_handle_closedone_msg(struct umb_softc *, void *, int);
164 1.4.4.2 pgoyette Static int umb_decode_register_state(struct umb_softc *, void *, int);
165 1.4.4.2 pgoyette Static int umb_decode_devices_caps(struct umb_softc *, void *, int);
166 1.4.4.2 pgoyette Static int umb_decode_subscriber_status(struct umb_softc *, void *, int);
167 1.4.4.2 pgoyette Static int umb_decode_radio_state(struct umb_softc *, void *, int);
168 1.4.4.2 pgoyette Static int umb_decode_pin(struct umb_softc *, void *, int);
169 1.4.4.2 pgoyette Static int umb_decode_packet_service(struct umb_softc *, void *, int);
170 1.4.4.2 pgoyette Static int umb_decode_signal_state(struct umb_softc *, void *, int);
171 1.4.4.2 pgoyette Static int umb_decode_connect_info(struct umb_softc *, void *, int);
172 1.4.4.2 pgoyette Static int umb_decode_ip_configuration(struct umb_softc *, void *, int);
173 1.4.4.2 pgoyette Static void umb_rx(struct umb_softc *);
174 1.4.4.2 pgoyette Static void umb_rxeof(struct usbd_xfer *, void *, usbd_status);
175 1.4.4.2 pgoyette Static int umb_encap(struct umb_softc *, struct mbuf *);
176 1.4.4.2 pgoyette Static void umb_txeof(struct usbd_xfer *, void *, usbd_status);
177 1.4.4.2 pgoyette Static void umb_decap(struct umb_softc *, struct usbd_xfer *);
178 1.4.4.2 pgoyette
179 1.4.4.2 pgoyette Static usbd_status umb_send_encap_command(struct umb_softc *, void *, int);
180 1.4.4.2 pgoyette Static int umb_get_encap_response(struct umb_softc *, void *, int *);
181 1.4.4.2 pgoyette Static void umb_ctrl_msg(struct umb_softc *, uint32_t, void *, int);
182 1.4.4.2 pgoyette
183 1.4.4.2 pgoyette Static void umb_open(struct umb_softc *);
184 1.4.4.2 pgoyette Static void umb_close(struct umb_softc *);
185 1.4.4.2 pgoyette
186 1.4.4.2 pgoyette Static int umb_setpin(struct umb_softc *, int, int, void *, int, void *,
187 1.4.4.2 pgoyette int);
188 1.4.4.2 pgoyette Static void umb_setdataclass(struct umb_softc *);
189 1.4.4.2 pgoyette Static void umb_radio(struct umb_softc *, int);
190 1.4.4.2 pgoyette Static void umb_allocate_cid(struct umb_softc *);
191 1.4.4.2 pgoyette Static void umb_send_fcc_auth(struct umb_softc *);
192 1.4.4.2 pgoyette Static void umb_packet_service(struct umb_softc *, int);
193 1.4.4.2 pgoyette Static void umb_connect(struct umb_softc *);
194 1.4.4.2 pgoyette Static void umb_disconnect(struct umb_softc *);
195 1.4.4.2 pgoyette Static void umb_send_connect(struct umb_softc *, int);
196 1.4.4.2 pgoyette
197 1.4.4.2 pgoyette Static void umb_qry_ipconfig(struct umb_softc *);
198 1.4.4.2 pgoyette Static void umb_cmd(struct umb_softc *, int, int, const void *, int);
199 1.4.4.2 pgoyette Static void umb_cmd1(struct umb_softc *, int, int, const void *, int, uint8_t *);
200 1.4.4.2 pgoyette Static void umb_command_done(struct umb_softc *, void *, int);
201 1.4.4.2 pgoyette Static void umb_decode_cid(struct umb_softc *, uint32_t, void *, int);
202 1.4.4.2 pgoyette Static void umb_decode_qmi(struct umb_softc *, uint8_t *, int);
203 1.4.4.2 pgoyette
204 1.4.4.2 pgoyette Static void umb_intr(struct usbd_xfer *, void *, usbd_status);
205 1.4.4.2 pgoyette
206 1.4.4.2 pgoyette Static char *umb_ntop(struct sockaddr *);
207 1.4.4.2 pgoyette
208 1.4.4.2 pgoyette Static const char *
209 1.4.4.2 pgoyette inet_ntop(int af, const void *src, char *dst, socklen_t size);
210 1.4.4.2 pgoyette static const char *inet_ntop4(const u_char *src, char *dst, size_t size);
211 1.4.4.2 pgoyette #ifdef INET6
212 1.4.4.2 pgoyette static const char *inet_ntop6(const u_char *src, char *dst, size_t size);
213 1.4.4.2 pgoyette #endif /* INET6 */
214 1.4.4.2 pgoyette
215 1.4.4.2 pgoyette Static int umb_xfer_tout = USBD_DEFAULT_TIMEOUT;
216 1.4.4.2 pgoyette
217 1.4.4.2 pgoyette Static uint8_t umb_uuid_basic_connect[] = MBIM_UUID_BASIC_CONNECT;
218 1.4.4.2 pgoyette Static uint8_t umb_uuid_context_internet[] = MBIM_UUID_CONTEXT_INTERNET;
219 1.4.4.2 pgoyette Static uint8_t umb_uuid_qmi_mbim[] = MBIM_UUID_QMI_MBIM;
220 1.4.4.2 pgoyette Static uint32_t umb_session_id = 0;
221 1.4.4.2 pgoyette
222 1.4.4.2 pgoyette CFATTACH_DECL_NEW(umb, sizeof(struct umb_softc), umb_match, umb_attach,
223 1.4.4.2 pgoyette umb_detach, umb_activate);
224 1.4.4.2 pgoyette
225 1.4.4.2 pgoyette const int umb_delay = 4000;
226 1.4.4.2 pgoyette
227 1.4.4.2 pgoyette /*
228 1.4.4.2 pgoyette * These devices require an "FCC Authentication" command.
229 1.4.4.2 pgoyette */
230 1.4.4.2 pgoyette const struct usb_devno umb_fccauth_devs[] = {
231 1.4.4.2 pgoyette { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_EM7455 },
232 1.4.4.2 pgoyette };
233 1.4.4.2 pgoyette
234 1.4.4.2 pgoyette Static const uint8_t umb_qmi_alloc_cid[] = {
235 1.4.4.2 pgoyette 0x01,
236 1.4.4.2 pgoyette 0x0f, 0x00, /* len */
237 1.4.4.2 pgoyette 0x00, /* QMUX flags */
238 1.4.4.2 pgoyette 0x00, /* service "ctl" */
239 1.4.4.2 pgoyette 0x00, /* CID */
240 1.4.4.2 pgoyette 0x00, /* QMI flags */
241 1.4.4.2 pgoyette 0x01, /* transaction */
242 1.4.4.2 pgoyette 0x22, 0x00, /* msg "Allocate CID" */
243 1.4.4.2 pgoyette 0x04, 0x00, /* TLV len */
244 1.4.4.2 pgoyette 0x01, 0x01, 0x00, 0x02 /* TLV */
245 1.4.4.2 pgoyette };
246 1.4.4.2 pgoyette
247 1.4.4.2 pgoyette Static const uint8_t umb_qmi_fcc_auth[] = {
248 1.4.4.2 pgoyette 0x01,
249 1.4.4.2 pgoyette 0x0c, 0x00, /* len */
250 1.4.4.2 pgoyette 0x00, /* QMUX flags */
251 1.4.4.2 pgoyette 0x02, /* service "dms" */
252 1.4.4.2 pgoyette #define UMB_QMI_CID_OFFS 5
253 1.4.4.2 pgoyette 0x00, /* CID (filled in later) */
254 1.4.4.2 pgoyette 0x00, /* QMI flags */
255 1.4.4.2 pgoyette 0x01, 0x00, /* transaction */
256 1.4.4.2 pgoyette 0x5f, 0x55, /* msg "Send FCC Authentication" */
257 1.4.4.2 pgoyette 0x00, 0x00 /* TLV len */
258 1.4.4.2 pgoyette };
259 1.4.4.2 pgoyette
260 1.4.4.2 pgoyette Static int
261 1.4.4.2 pgoyette umb_match(device_t parent, cfdata_t match, void *aux)
262 1.4.4.2 pgoyette {
263 1.4.4.2 pgoyette struct usbif_attach_arg *uiaa = aux;
264 1.4.4.2 pgoyette usb_interface_descriptor_t *id;
265 1.4.4.2 pgoyette
266 1.4.4.2 pgoyette if (!uiaa->uiaa_iface)
267 1.4.4.2 pgoyette return UMATCH_NONE;
268 1.4.4.2 pgoyette if ((id = usbd_get_interface_descriptor(uiaa->uiaa_iface)) == NULL)
269 1.4.4.2 pgoyette return UMATCH_NONE;
270 1.4.4.2 pgoyette
271 1.4.4.2 pgoyette /*
272 1.4.4.2 pgoyette * If this function implements NCM, check if alternate setting
273 1.4.4.2 pgoyette * 1 implements MBIM.
274 1.4.4.2 pgoyette */
275 1.4.4.2 pgoyette if (id->bInterfaceClass == UICLASS_CDC &&
276 1.4.4.2 pgoyette id->bInterfaceSubClass ==
277 1.4.4.2 pgoyette UISUBCLASS_NETWORK_CONTROL_MODEL)
278 1.4.4.2 pgoyette id = usbd_find_idesc(uiaa->uiaa_device->ud_cdesc, uiaa->uiaa_iface->ui_index, 1);
279 1.4.4.2 pgoyette if (id == NULL)
280 1.4.4.2 pgoyette return UMATCH_NONE;
281 1.4.4.2 pgoyette
282 1.4.4.2 pgoyette if (id->bInterfaceClass == UICLASS_CDC &&
283 1.4.4.2 pgoyette id->bInterfaceSubClass ==
284 1.4.4.2 pgoyette UISUBCLASS_MOBILE_BROADBAND_INTERFACE_MODEL &&
285 1.4.4.2 pgoyette id->bInterfaceProtocol == 0)
286 1.4.4.2 pgoyette return UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO;
287 1.4.4.2 pgoyette
288 1.4.4.2 pgoyette return UMATCH_NONE;
289 1.4.4.2 pgoyette }
290 1.4.4.2 pgoyette
291 1.4.4.2 pgoyette Static void
292 1.4.4.2 pgoyette umb_attach(device_t parent, device_t self, void *aux)
293 1.4.4.2 pgoyette {
294 1.4.4.2 pgoyette struct umb_softc *sc = device_private(self);
295 1.4.4.2 pgoyette struct usbif_attach_arg *uiaa = aux;
296 1.4.4.2 pgoyette char *devinfop;
297 1.4.4.2 pgoyette usbd_status status;
298 1.4.4.2 pgoyette usbd_desc_iter_t iter;
299 1.4.4.2 pgoyette const usb_descriptor_t *desc;
300 1.4.4.2 pgoyette int v;
301 1.4.4.2 pgoyette const usb_cdc_union_descriptor_t *ud;
302 1.4.4.2 pgoyette const struct mbim_descriptor *md;
303 1.4.4.2 pgoyette int i;
304 1.4.4.2 pgoyette int ctrl_ep;
305 1.4.4.2 pgoyette const usb_interface_descriptor_t *id;
306 1.4.4.2 pgoyette usb_config_descriptor_t *cd;
307 1.4.4.2 pgoyette usb_endpoint_descriptor_t *ed;
308 1.4.4.2 pgoyette const usb_interface_assoc_descriptor_t *ad;
309 1.4.4.2 pgoyette int current_ifaceno = -1;
310 1.4.4.2 pgoyette int data_ifaceno = -1;
311 1.4.4.2 pgoyette int altnum;
312 1.4.4.2 pgoyette int s;
313 1.4.4.2 pgoyette struct ifnet *ifp;
314 1.4.4.2 pgoyette int rv;
315 1.4.4.2 pgoyette
316 1.4.4.2 pgoyette sc->sc_dev = self;
317 1.4.4.2 pgoyette sc->sc_udev = uiaa->uiaa_device;
318 1.4.4.2 pgoyette
319 1.4.4.2 pgoyette aprint_naive("\n");
320 1.4.4.2 pgoyette aprint_normal("\n");
321 1.4.4.2 pgoyette
322 1.4.4.2 pgoyette devinfop = usbd_devinfo_alloc(sc->sc_udev, 0);
323 1.4.4.2 pgoyette aprint_normal_dev(self, "%s\n", devinfop);
324 1.4.4.2 pgoyette usbd_devinfo_free(devinfop);
325 1.4.4.2 pgoyette
326 1.4.4.2 pgoyette sc->sc_ctrl_ifaceno = uiaa->uiaa_ifaceno;
327 1.4.4.2 pgoyette
328 1.4.4.2 pgoyette /*
329 1.4.4.2 pgoyette * Some MBIM hardware does not provide the mandatory CDC Union
330 1.4.4.2 pgoyette * Descriptor, so we also look at matching Interface
331 1.4.4.2 pgoyette * Association Descriptors to find out the MBIM Data Interface
332 1.4.4.2 pgoyette * number.
333 1.4.4.2 pgoyette */
334 1.4.4.2 pgoyette sc->sc_ver_maj = sc->sc_ver_min = -1;
335 1.4.4.2 pgoyette sc->sc_maxpktlen = MBIM_MAXSEGSZ_MINVAL;
336 1.4.4.2 pgoyette usb_desc_iter_init(sc->sc_udev, &iter);
337 1.4.4.2 pgoyette while ((desc = usb_desc_iter_next(&iter))) {
338 1.4.4.2 pgoyette if (desc->bDescriptorType == UDESC_INTERFACE_ASSOC) {
339 1.4.4.2 pgoyette ad = (const usb_interface_assoc_descriptor_t *)desc;
340 1.4.4.2 pgoyette if (ad->bFirstInterface == uiaa->uiaa_ifaceno &&
341 1.4.4.2 pgoyette ad->bInterfaceCount > 1)
342 1.4.4.2 pgoyette data_ifaceno = uiaa->uiaa_ifaceno + 1;
343 1.4.4.2 pgoyette continue;
344 1.4.4.2 pgoyette }
345 1.4.4.2 pgoyette if (desc->bDescriptorType == UDESC_INTERFACE) {
346 1.4.4.2 pgoyette id = (const usb_interface_descriptor_t *)desc;
347 1.4.4.2 pgoyette current_ifaceno = id->bInterfaceNumber;
348 1.4.4.2 pgoyette continue;
349 1.4.4.2 pgoyette }
350 1.4.4.2 pgoyette if (current_ifaceno != uiaa->uiaa_ifaceno)
351 1.4.4.2 pgoyette continue;
352 1.4.4.2 pgoyette if (desc->bDescriptorType != UDESC_CS_INTERFACE)
353 1.4.4.2 pgoyette continue;
354 1.4.4.2 pgoyette switch (desc->bDescriptorSubtype) {
355 1.4.4.2 pgoyette case UDESCSUB_CDC_UNION:
356 1.4.4.2 pgoyette ud = (const usb_cdc_union_descriptor_t *)desc;
357 1.4.4.2 pgoyette data_ifaceno = ud->bSlaveInterface[0];
358 1.4.4.2 pgoyette break;
359 1.4.4.2 pgoyette case UDESCSUB_MBIM:
360 1.4.4.2 pgoyette md = (const struct mbim_descriptor *)desc;
361 1.4.4.2 pgoyette v = UGETW(md->bcdMBIMVersion);
362 1.4.4.2 pgoyette sc->sc_ver_maj = MBIM_VER_MAJOR(v);
363 1.4.4.2 pgoyette sc->sc_ver_min = MBIM_VER_MINOR(v);
364 1.4.4.2 pgoyette sc->sc_ctrl_len = UGETW(md->wMaxControlMessage);
365 1.4.4.2 pgoyette /* Never trust a USB device! Could try to exploit us */
366 1.4.4.2 pgoyette if (sc->sc_ctrl_len < MBIM_CTRLMSG_MINLEN ||
367 1.4.4.2 pgoyette sc->sc_ctrl_len > MBIM_CTRLMSG_MAXLEN) {
368 1.4.4.2 pgoyette DPRINTF("%s: control message len %d out of "
369 1.4.4.2 pgoyette "bounds [%d .. %d]\n", DEVNAM(sc),
370 1.4.4.2 pgoyette sc->sc_ctrl_len, MBIM_CTRLMSG_MINLEN,
371 1.4.4.2 pgoyette MBIM_CTRLMSG_MAXLEN);
372 1.4.4.2 pgoyette /* cont. anyway */
373 1.4.4.2 pgoyette }
374 1.4.4.2 pgoyette sc->sc_maxpktlen = UGETW(md->wMaxSegmentSize);
375 1.4.4.2 pgoyette DPRINTFN(2, "%s: ctrl_len=%d, maxpktlen=%d, cap=0x%x\n",
376 1.4.4.2 pgoyette DEVNAM(sc), sc->sc_ctrl_len, sc->sc_maxpktlen,
377 1.4.4.2 pgoyette md->bmNetworkCapabilities);
378 1.4.4.2 pgoyette break;
379 1.4.4.2 pgoyette default:
380 1.4.4.2 pgoyette break;
381 1.4.4.2 pgoyette }
382 1.4.4.2 pgoyette }
383 1.4.4.2 pgoyette if (sc->sc_ver_maj < 0) {
384 1.4.4.2 pgoyette aprint_error_dev(self, "missing MBIM descriptor\n");
385 1.4.4.2 pgoyette goto fail;
386 1.4.4.2 pgoyette }
387 1.4.4.2 pgoyette
388 1.4.4.2 pgoyette aprint_normal_dev(self, "version %d.%d\n", sc->sc_ver_maj,
389 1.4.4.2 pgoyette sc->sc_ver_min);
390 1.4.4.2 pgoyette
391 1.4.4.2 pgoyette if (usb_lookup(umb_fccauth_devs, uiaa->uiaa_vendor, uiaa->uiaa_product)) {
392 1.4.4.2 pgoyette sc->sc_flags |= UMBFLG_FCC_AUTH_REQUIRED;
393 1.4.4.2 pgoyette sc->sc_cid = -1;
394 1.4.4.2 pgoyette }
395 1.4.4.2 pgoyette
396 1.4.4.2 pgoyette for (i = 0; i < uiaa->uiaa_nifaces; i++) {
397 1.4.4.2 pgoyette id = usbd_get_interface_descriptor(uiaa->uiaa_ifaces[i]);
398 1.4.4.2 pgoyette if (id != NULL && id->bInterfaceNumber == data_ifaceno) {
399 1.4.4.2 pgoyette sc->sc_data_iface = uiaa->uiaa_ifaces[i];
400 1.4.4.2 pgoyette }
401 1.4.4.2 pgoyette }
402 1.4.4.2 pgoyette if (sc->sc_data_iface == NULL) {
403 1.4.4.2 pgoyette aprint_error_dev(self, "no data interface found\n");
404 1.4.4.2 pgoyette goto fail;
405 1.4.4.2 pgoyette }
406 1.4.4.2 pgoyette
407 1.4.4.2 pgoyette /*
408 1.4.4.2 pgoyette * If this is a combined NCM/MBIM function, switch to
409 1.4.4.2 pgoyette * alternate setting one to enable MBIM.
410 1.4.4.2 pgoyette */
411 1.4.4.2 pgoyette id = usbd_get_interface_descriptor(uiaa->uiaa_iface);
412 1.4.4.2 pgoyette if (id->bInterfaceClass == UICLASS_CDC &&
413 1.4.4.2 pgoyette id->bInterfaceSubClass ==
414 1.4.4.2 pgoyette UISUBCLASS_NETWORK_CONTROL_MODEL)
415 1.4.4.2 pgoyette usbd_set_interface(uiaa->uiaa_iface, 1);
416 1.4.4.2 pgoyette
417 1.4.4.2 pgoyette id = usbd_get_interface_descriptor(uiaa->uiaa_iface);
418 1.4.4.2 pgoyette ctrl_ep = -1;
419 1.4.4.2 pgoyette for (i = 0; i < id->bNumEndpoints && ctrl_ep == -1; i++) {
420 1.4.4.2 pgoyette ed = usbd_interface2endpoint_descriptor(uiaa->uiaa_iface, i);
421 1.4.4.2 pgoyette if (ed == NULL)
422 1.4.4.2 pgoyette break;
423 1.4.4.2 pgoyette if (UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT &&
424 1.4.4.2 pgoyette UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
425 1.4.4.2 pgoyette ctrl_ep = ed->bEndpointAddress;
426 1.4.4.2 pgoyette }
427 1.4.4.2 pgoyette if (ctrl_ep == -1) {
428 1.4.4.2 pgoyette aprint_error_dev(self, "missing interrupt endpoint\n");
429 1.4.4.2 pgoyette goto fail;
430 1.4.4.2 pgoyette }
431 1.4.4.2 pgoyette
432 1.4.4.2 pgoyette /*
433 1.4.4.2 pgoyette * For the MBIM Data Interface, select the appropriate
434 1.4.4.2 pgoyette * alternate setting by looking for a matching descriptor that
435 1.4.4.2 pgoyette * has two endpoints.
436 1.4.4.2 pgoyette */
437 1.4.4.2 pgoyette cd = usbd_get_config_descriptor(sc->sc_udev);
438 1.4.4.2 pgoyette altnum = usbd_get_no_alts(cd, data_ifaceno);
439 1.4.4.2 pgoyette for (i = 0; i < altnum; i++) {
440 1.4.4.2 pgoyette id = usbd_find_idesc(cd, sc->sc_data_iface->ui_index, i);
441 1.4.4.2 pgoyette if (id == NULL)
442 1.4.4.2 pgoyette continue;
443 1.4.4.2 pgoyette if (id->bInterfaceClass == UICLASS_CDC_DATA &&
444 1.4.4.2 pgoyette id->bInterfaceSubClass == UISUBCLASS_DATA &&
445 1.4.4.2 pgoyette id->bInterfaceProtocol == UIPROTO_DATA_MBIM &&
446 1.4.4.2 pgoyette id->bNumEndpoints == 2)
447 1.4.4.2 pgoyette break;
448 1.4.4.2 pgoyette }
449 1.4.4.2 pgoyette if (i == altnum || id == NULL) {
450 1.4.4.2 pgoyette aprint_error_dev(self, "missing alt setting for interface #%d\n",
451 1.4.4.2 pgoyette data_ifaceno);
452 1.4.4.2 pgoyette goto fail;
453 1.4.4.2 pgoyette }
454 1.4.4.2 pgoyette status = usbd_set_interface(sc->sc_data_iface, i);
455 1.4.4.2 pgoyette if (status) {
456 1.4.4.2 pgoyette aprint_error_dev(self, "select alt setting %d for interface #%d "
457 1.4.4.2 pgoyette "failed: %s\n", i, data_ifaceno, usbd_errstr(status));
458 1.4.4.2 pgoyette goto fail;
459 1.4.4.2 pgoyette }
460 1.4.4.2 pgoyette
461 1.4.4.2 pgoyette id = usbd_get_interface_descriptor(sc->sc_data_iface);
462 1.4.4.2 pgoyette sc->sc_rx_ep = sc->sc_tx_ep = -1;
463 1.4.4.2 pgoyette for (i = 0; i < id->bNumEndpoints; i++) {
464 1.4.4.2 pgoyette if ((ed = usbd_interface2endpoint_descriptor(sc->sc_data_iface,
465 1.4.4.2 pgoyette i)) == NULL)
466 1.4.4.2 pgoyette break;
467 1.4.4.2 pgoyette if (UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK &&
468 1.4.4.2 pgoyette UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
469 1.4.4.2 pgoyette sc->sc_rx_ep = ed->bEndpointAddress;
470 1.4.4.2 pgoyette else if (UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK &&
471 1.4.4.2 pgoyette UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT)
472 1.4.4.2 pgoyette sc->sc_tx_ep = ed->bEndpointAddress;
473 1.4.4.2 pgoyette }
474 1.4.4.2 pgoyette if (sc->sc_rx_ep == -1 || sc->sc_tx_ep == -1) {
475 1.4.4.2 pgoyette aprint_error_dev(self, "missing bulk endpoints\n");
476 1.4.4.2 pgoyette goto fail;
477 1.4.4.2 pgoyette }
478 1.4.4.2 pgoyette
479 1.4.4.2 pgoyette DPRINTFN(2, "%s: ctrl-ifno#%d: ep-ctrl=%d, data-ifno#%d: ep-rx=%d, "
480 1.4.4.2 pgoyette "ep-tx=%d\n", DEVNAM(sc), sc->sc_ctrl_ifaceno,
481 1.4.4.2 pgoyette UE_GET_ADDR(ctrl_ep), data_ifaceno,
482 1.4.4.2 pgoyette UE_GET_ADDR(sc->sc_rx_ep), UE_GET_ADDR(sc->sc_tx_ep));
483 1.4.4.2 pgoyette
484 1.4.4.2 pgoyette usb_init_task(&sc->sc_umb_task, umb_state_task, sc,
485 1.4.4.2 pgoyette 0);
486 1.4.4.2 pgoyette usb_init_task(&sc->sc_get_response_task, umb_get_response_task, sc,
487 1.4.4.2 pgoyette 0);
488 1.4.4.2 pgoyette callout_init(&sc->sc_statechg_timer, 0);
489 1.4.4.2 pgoyette callout_setfunc(&sc->sc_statechg_timer, umb_statechg_timeout, sc);
490 1.4.4.2 pgoyette
491 1.4.4.2 pgoyette if (usbd_open_pipe_intr(uiaa->uiaa_iface, ctrl_ep, USBD_SHORT_XFER_OK,
492 1.4.4.2 pgoyette &sc->sc_ctrl_pipe, sc, &sc->sc_intr_msg, sizeof(sc->sc_intr_msg),
493 1.4.4.2 pgoyette umb_intr, USBD_DEFAULT_INTERVAL)) {
494 1.4.4.2 pgoyette aprint_error_dev(self, "failed to open control pipe\n");
495 1.4.4.2 pgoyette goto fail;
496 1.4.4.2 pgoyette }
497 1.4.4.2 pgoyette
498 1.4.4.2 pgoyette sc->sc_resp_buf = kmem_alloc(sc->sc_ctrl_len, KM_SLEEP);
499 1.4.4.2 pgoyette sc->sc_ctrl_msg = kmem_alloc(sc->sc_ctrl_len, KM_SLEEP);
500 1.4.4.2 pgoyette
501 1.4.4.2 pgoyette sc->sc_info.regstate = MBIM_REGSTATE_UNKNOWN;
502 1.4.4.2 pgoyette sc->sc_info.pin_attempts_left = UMB_VALUE_UNKNOWN;
503 1.4.4.2 pgoyette sc->sc_info.rssi = UMB_VALUE_UNKNOWN;
504 1.4.4.2 pgoyette sc->sc_info.ber = UMB_VALUE_UNKNOWN;
505 1.4.4.2 pgoyette
506 1.4.4.2 pgoyette umb_ncm_setup(sc);
507 1.4.4.2 pgoyette DPRINTFN(2, "%s: rx/tx size %d/%d\n", DEVNAM(sc),
508 1.4.4.2 pgoyette sc->sc_rx_bufsz, sc->sc_tx_bufsz);
509 1.4.4.2 pgoyette
510 1.4.4.2 pgoyette s = splnet();
511 1.4.4.2 pgoyette
512 1.4.4.2 pgoyette /* initialize the interface */
513 1.4.4.2 pgoyette ifp = GET_IFP(sc);
514 1.4.4.2 pgoyette ifp->if_softc = sc;
515 1.4.4.2 pgoyette ifp->if_flags = IFF_SIMPLEX | IFF_MULTICAST | IFF_POINTOPOINT;
516 1.4.4.2 pgoyette ifp->if_ioctl = umb_ioctl;
517 1.4.4.2 pgoyette ifp->if_start = umb_start;
518 1.4.4.2 pgoyette
519 1.4.4.2 pgoyette ifp->if_watchdog = umb_watchdog;
520 1.4.4.2 pgoyette strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
521 1.4.4.2 pgoyette ifp->if_link_state = LINK_STATE_DOWN;
522 1.4.4.2 pgoyette ifmedia_init(&sc->sc_im, 0, umb_mediachange, umb_mediastatus);
523 1.4.4.2 pgoyette
524 1.4.4.2 pgoyette ifp->if_type = IFT_MBIM;
525 1.4.4.2 pgoyette ifp->if_addrlen = 0;
526 1.4.4.2 pgoyette ifp->if_hdrlen = sizeof(struct ncm_header16) +
527 1.4.4.2 pgoyette sizeof(struct ncm_pointer16);
528 1.4.4.2 pgoyette ifp->if_mtu = 1500; /* use a common default */
529 1.4.4.2 pgoyette ifp->if_mtu = sc->sc_maxpktlen;
530 1.4.4.2 pgoyette ifp->if_output = umb_output;
531 1.4.4.2 pgoyette ifp->_if_input = umb_input;
532 1.4.4.2 pgoyette IFQ_SET_READY(&ifp->if_snd);
533 1.4.4.2 pgoyette
534 1.4.4.2 pgoyette /* attach the interface */
535 1.4.4.2 pgoyette rv = if_initialize(ifp);
536 1.4.4.2 pgoyette if (rv != 0) {
537 1.4.4.2 pgoyette aprint_error_dev(self, "if_initialize failed(%d)\n", rv);
538 1.4.4.2 pgoyette splx(s);
539 1.4.4.2 pgoyette return;
540 1.4.4.2 pgoyette }
541 1.4.4.2 pgoyette if_register(ifp);
542 1.4.4.2 pgoyette if_alloc_sadl(ifp);
543 1.4.4.2 pgoyette
544 1.4.4.2 pgoyette bpf_attach(ifp, DLT_RAW, 0);
545 1.4.4.2 pgoyette rnd_attach_source(&sc->sc_rnd_source, device_xname(sc->sc_dev),
546 1.4.4.2 pgoyette RND_TYPE_NET, RND_FLAG_DEFAULT);
547 1.4.4.2 pgoyette
548 1.4.4.2 pgoyette /*
549 1.4.4.2 pgoyette * Open the device now so that we are able to query device information.
550 1.4.4.2 pgoyette * XXX maybe close when done?
551 1.4.4.2 pgoyette */
552 1.4.4.2 pgoyette umb_open(sc);
553 1.4.4.2 pgoyette
554 1.4.4.2 pgoyette sc->sc_attached = 1;
555 1.4.4.2 pgoyette splx(s);
556 1.4.4.2 pgoyette
557 1.4.4.2 pgoyette usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
558 1.4.4.2 pgoyette
559 1.4.4.2 pgoyette if (!pmf_device_register(self, NULL, NULL))
560 1.4.4.2 pgoyette aprint_error_dev(self, "couldn't establish power handler\n");
561 1.4.4.2 pgoyette
562 1.4.4.2 pgoyette return;
563 1.4.4.2 pgoyette
564 1.4.4.2 pgoyette fail:
565 1.4.4.2 pgoyette umb_activate(sc->sc_dev, DVACT_DEACTIVATE);
566 1.4.4.2 pgoyette return;
567 1.4.4.2 pgoyette }
568 1.4.4.2 pgoyette
569 1.4.4.2 pgoyette Static int
570 1.4.4.2 pgoyette umb_detach(device_t self, int flags)
571 1.4.4.2 pgoyette {
572 1.4.4.2 pgoyette struct umb_softc *sc = (struct umb_softc *)self;
573 1.4.4.2 pgoyette struct ifnet *ifp = GET_IFP(sc);
574 1.4.4.2 pgoyette int s;
575 1.4.4.2 pgoyette
576 1.4.4.2 pgoyette pmf_device_deregister(self);
577 1.4.4.2 pgoyette
578 1.4.4.2 pgoyette s = splnet();
579 1.4.4.2 pgoyette if (ifp->if_flags & IFF_RUNNING)
580 1.4.4.2 pgoyette umb_down(sc, 1);
581 1.4.4.2 pgoyette umb_close(sc);
582 1.4.4.2 pgoyette
583 1.4.4.3 pgoyette usb_rem_task_wait(sc->sc_udev, &sc->sc_get_response_task,
584 1.4.4.3 pgoyette USB_TASKQ_DRIVER, NULL);
585 1.4.4.2 pgoyette sc->sc_nresp = 0;
586 1.4.4.2 pgoyette if (sc->sc_rx_ep != -1 && sc->sc_tx_ep != -1) {
587 1.4.4.2 pgoyette callout_destroy(&sc->sc_statechg_timer);
588 1.4.4.3 pgoyette usb_rem_task_wait(sc->sc_udev, &sc->sc_umb_task,
589 1.4.4.3 pgoyette USB_TASKQ_DRIVER, NULL);
590 1.4.4.2 pgoyette }
591 1.4.4.2 pgoyette if (sc->sc_ctrl_pipe) {
592 1.4.4.2 pgoyette usbd_close_pipe(sc->sc_ctrl_pipe);
593 1.4.4.2 pgoyette sc->sc_ctrl_pipe = NULL;
594 1.4.4.2 pgoyette }
595 1.4.4.2 pgoyette if (sc->sc_ctrl_msg) {
596 1.4.4.2 pgoyette kmem_free(sc->sc_ctrl_msg, sc->sc_ctrl_len);
597 1.4.4.2 pgoyette sc->sc_ctrl_msg = NULL;
598 1.4.4.2 pgoyette }
599 1.4.4.2 pgoyette if (sc->sc_resp_buf) {
600 1.4.4.2 pgoyette kmem_free(sc->sc_resp_buf, sc->sc_ctrl_len);
601 1.4.4.2 pgoyette sc->sc_resp_buf = NULL;
602 1.4.4.2 pgoyette }
603 1.4.4.2 pgoyette if (ifp->if_softc) {
604 1.4.4.2 pgoyette ifmedia_delete_instance(&sc->sc_im, IFM_INST_ANY);
605 1.4.4.2 pgoyette }
606 1.4.4.2 pgoyette if (sc->sc_attached) {
607 1.4.4.2 pgoyette rnd_detach_source(&sc->sc_rnd_source);
608 1.4.4.2 pgoyette bpf_detach(ifp);
609 1.4.4.2 pgoyette if_detach(ifp);
610 1.4.4.2 pgoyette }
611 1.4.4.2 pgoyette
612 1.4.4.2 pgoyette sc->sc_attached = 0;
613 1.4.4.2 pgoyette splx(s);
614 1.4.4.2 pgoyette return 0;
615 1.4.4.2 pgoyette }
616 1.4.4.2 pgoyette
617 1.4.4.2 pgoyette Static int
618 1.4.4.2 pgoyette umb_activate(device_t self, enum devact act)
619 1.4.4.2 pgoyette {
620 1.4.4.2 pgoyette struct umb_softc *sc = device_private(self);
621 1.4.4.2 pgoyette
622 1.4.4.2 pgoyette switch (act) {
623 1.4.4.2 pgoyette case DVACT_DEACTIVATE:
624 1.4.4.2 pgoyette if_deactivate(GET_IFP(sc));
625 1.4.4.2 pgoyette sc->sc_dying = 1;
626 1.4.4.2 pgoyette return 0;
627 1.4.4.2 pgoyette default:
628 1.4.4.2 pgoyette return EOPNOTSUPP;
629 1.4.4.2 pgoyette }
630 1.4.4.2 pgoyette }
631 1.4.4.2 pgoyette
632 1.4.4.2 pgoyette Static void
633 1.4.4.2 pgoyette umb_ncm_setup(struct umb_softc *sc)
634 1.4.4.2 pgoyette {
635 1.4.4.2 pgoyette usb_device_request_t req;
636 1.4.4.2 pgoyette struct ncm_ntb_parameters np;
637 1.4.4.2 pgoyette
638 1.4.4.2 pgoyette /* Query NTB tranfers sizes */
639 1.4.4.2 pgoyette req.bmRequestType = UT_READ_CLASS_INTERFACE;
640 1.4.4.2 pgoyette req.bRequest = NCM_GET_NTB_PARAMETERS;
641 1.4.4.2 pgoyette USETW(req.wValue, 0);
642 1.4.4.2 pgoyette USETW(req.wIndex, sc->sc_ctrl_ifaceno);
643 1.4.4.2 pgoyette USETW(req.wLength, sizeof(np));
644 1.4.4.2 pgoyette if (usbd_do_request(sc->sc_udev, &req, &np) == USBD_NORMAL_COMPLETION &&
645 1.4.4.2 pgoyette UGETW(np.wLength) == sizeof(np)) {
646 1.4.4.2 pgoyette sc->sc_rx_bufsz = UGETDW(np.dwNtbInMaxSize);
647 1.4.4.2 pgoyette sc->sc_tx_bufsz = UGETDW(np.dwNtbOutMaxSize);
648 1.4.4.2 pgoyette } else
649 1.4.4.2 pgoyette sc->sc_rx_bufsz = sc->sc_tx_bufsz = 8 * 1024;
650 1.4.4.2 pgoyette }
651 1.4.4.2 pgoyette
652 1.4.4.2 pgoyette Static int
653 1.4.4.2 pgoyette umb_alloc_xfers(struct umb_softc *sc)
654 1.4.4.2 pgoyette {
655 1.4.4.2 pgoyette int err = 0;
656 1.4.4.2 pgoyette
657 1.4.4.2 pgoyette if (!sc->sc_rx_xfer) {
658 1.4.4.2 pgoyette err |= usbd_create_xfer(sc->sc_rx_pipe,
659 1.4.4.2 pgoyette sc->sc_rx_bufsz,
660 1.4.4.2 pgoyette 0, 0, &sc->sc_rx_xfer);
661 1.4.4.2 pgoyette }
662 1.4.4.2 pgoyette if (!sc->sc_tx_xfer) {
663 1.4.4.2 pgoyette err |= usbd_create_xfer(sc->sc_tx_pipe,
664 1.4.4.2 pgoyette sc->sc_tx_bufsz,
665 1.4.4.2 pgoyette 0, 0, &sc->sc_tx_xfer);
666 1.4.4.2 pgoyette }
667 1.4.4.2 pgoyette if (err)
668 1.4.4.2 pgoyette return err;
669 1.4.4.2 pgoyette
670 1.4.4.2 pgoyette sc->sc_rx_buf = usbd_get_buffer(sc->sc_rx_xfer);
671 1.4.4.2 pgoyette sc->sc_tx_buf = usbd_get_buffer(sc->sc_tx_xfer);
672 1.4.4.2 pgoyette
673 1.4.4.2 pgoyette return 0;
674 1.4.4.2 pgoyette }
675 1.4.4.2 pgoyette
676 1.4.4.2 pgoyette Static void
677 1.4.4.2 pgoyette umb_free_xfers(struct umb_softc *sc)
678 1.4.4.2 pgoyette {
679 1.4.4.2 pgoyette if (sc->sc_rx_xfer) {
680 1.4.4.2 pgoyette /* implicit usbd_free_buffer() */
681 1.4.4.2 pgoyette usbd_destroy_xfer(sc->sc_rx_xfer);
682 1.4.4.2 pgoyette sc->sc_rx_xfer = NULL;
683 1.4.4.2 pgoyette sc->sc_rx_buf = NULL;
684 1.4.4.2 pgoyette }
685 1.4.4.2 pgoyette if (sc->sc_tx_xfer) {
686 1.4.4.2 pgoyette usbd_destroy_xfer(sc->sc_tx_xfer);
687 1.4.4.2 pgoyette sc->sc_tx_xfer = NULL;
688 1.4.4.2 pgoyette sc->sc_tx_buf = NULL;
689 1.4.4.2 pgoyette }
690 1.4.4.2 pgoyette if (sc->sc_tx_m) {
691 1.4.4.2 pgoyette m_freem(sc->sc_tx_m);
692 1.4.4.2 pgoyette sc->sc_tx_m = NULL;
693 1.4.4.2 pgoyette }
694 1.4.4.2 pgoyette }
695 1.4.4.2 pgoyette
696 1.4.4.2 pgoyette Static int
697 1.4.4.2 pgoyette umb_alloc_bulkpipes(struct umb_softc *sc)
698 1.4.4.2 pgoyette {
699 1.4.4.2 pgoyette struct ifnet *ifp = GET_IFP(sc);
700 1.4.4.2 pgoyette int rv;
701 1.4.4.2 pgoyette
702 1.4.4.2 pgoyette if (!(ifp->if_flags & IFF_RUNNING)) {
703 1.4.4.2 pgoyette if ((rv = usbd_open_pipe(sc->sc_data_iface, sc->sc_rx_ep,
704 1.4.4.2 pgoyette USBD_EXCLUSIVE_USE, &sc->sc_rx_pipe))) {
705 1.4.4.2 pgoyette DPRINTFN(4, "usbd_open_pipe() failed (RX) %d\n", rv);
706 1.4.4.2 pgoyette return 0;
707 1.4.4.2 pgoyette }
708 1.4.4.2 pgoyette if ((rv = usbd_open_pipe(sc->sc_data_iface, sc->sc_tx_ep,
709 1.4.4.2 pgoyette USBD_EXCLUSIVE_USE, &sc->sc_tx_pipe))) {
710 1.4.4.2 pgoyette DPRINTFN(4, "usbd_open_pipe() failed (TX) %d\n", rv);
711 1.4.4.2 pgoyette return 0;
712 1.4.4.2 pgoyette }
713 1.4.4.2 pgoyette
714 1.4.4.2 pgoyette if ((rv = umb_alloc_xfers(sc)) != 0) {
715 1.4.4.2 pgoyette DPRINTFN(4, "umb_alloc_xfers() failed %d\n", rv);
716 1.4.4.2 pgoyette return 0;
717 1.4.4.2 pgoyette }
718 1.4.4.2 pgoyette
719 1.4.4.2 pgoyette ifp->if_flags |= IFF_RUNNING;
720 1.4.4.2 pgoyette ifp->if_flags &= ~IFF_OACTIVE;
721 1.4.4.2 pgoyette umb_rx(sc);
722 1.4.4.2 pgoyette }
723 1.4.4.2 pgoyette return 1;
724 1.4.4.2 pgoyette }
725 1.4.4.2 pgoyette
726 1.4.4.2 pgoyette Static void
727 1.4.4.2 pgoyette umb_close_bulkpipes(struct umb_softc *sc)
728 1.4.4.2 pgoyette {
729 1.4.4.2 pgoyette struct ifnet *ifp = GET_IFP(sc);
730 1.4.4.2 pgoyette
731 1.4.4.2 pgoyette ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
732 1.4.4.2 pgoyette ifp->if_timer = 0;
733 1.4.4.2 pgoyette if (sc->sc_rx_pipe) {
734 1.4.4.2 pgoyette usbd_close_pipe(sc->sc_rx_pipe);
735 1.4.4.2 pgoyette sc->sc_rx_pipe = NULL;
736 1.4.4.2 pgoyette }
737 1.4.4.2 pgoyette if (sc->sc_tx_pipe) {
738 1.4.4.2 pgoyette usbd_close_pipe(sc->sc_tx_pipe);
739 1.4.4.2 pgoyette sc->sc_tx_pipe = NULL;
740 1.4.4.2 pgoyette }
741 1.4.4.2 pgoyette }
742 1.4.4.2 pgoyette
743 1.4.4.2 pgoyette Static int
744 1.4.4.2 pgoyette umb_ioctl(struct ifnet *ifp, u_long cmd, void *data)
745 1.4.4.2 pgoyette {
746 1.4.4.2 pgoyette struct umb_softc *sc = ifp->if_softc;
747 1.4.4.2 pgoyette struct ifaddr *ifa = (struct ifaddr *)data;
748 1.4.4.2 pgoyette struct ifreq *ifr = (struct ifreq *)data;
749 1.4.4.2 pgoyette int s, error = 0;
750 1.4.4.2 pgoyette struct umb_parameter mp;
751 1.4.4.2 pgoyette
752 1.4.4.2 pgoyette if (sc->sc_dying)
753 1.4.4.2 pgoyette return EIO;
754 1.4.4.2 pgoyette
755 1.4.4.2 pgoyette s = splnet();
756 1.4.4.2 pgoyette switch (cmd) {
757 1.4.4.2 pgoyette case SIOCINITIFADDR:
758 1.4.4.2 pgoyette ifp->if_flags |= IFF_UP;
759 1.4.4.2 pgoyette usb_add_task(sc->sc_udev, &sc->sc_umb_task, USB_TASKQ_DRIVER);
760 1.4.4.2 pgoyette switch (ifa->ifa_addr->sa_family) {
761 1.4.4.2 pgoyette #ifdef INET
762 1.4.4.2 pgoyette case AF_INET:
763 1.4.4.2 pgoyette break;
764 1.4.4.2 pgoyette #endif /* INET */
765 1.4.4.2 pgoyette #ifdef INET6
766 1.4.4.2 pgoyette case AF_INET6:
767 1.4.4.2 pgoyette break;
768 1.4.4.2 pgoyette #endif /* INET6 */
769 1.4.4.2 pgoyette default:
770 1.4.4.2 pgoyette error = EAFNOSUPPORT;
771 1.4.4.2 pgoyette break;
772 1.4.4.2 pgoyette }
773 1.4.4.2 pgoyette ifa->ifa_rtrequest = p2p_rtrequest;
774 1.4.4.2 pgoyette break;
775 1.4.4.2 pgoyette case SIOCSIFFLAGS:
776 1.4.4.2 pgoyette error = ifioctl_common(ifp, cmd, data);
777 1.4.4.2 pgoyette if (error)
778 1.4.4.2 pgoyette break;
779 1.4.4.2 pgoyette usb_add_task(sc->sc_udev, &sc->sc_umb_task, USB_TASKQ_DRIVER);
780 1.4.4.2 pgoyette break;
781 1.4.4.2 pgoyette case SIOCGUMBINFO:
782 1.4.4.2 pgoyette error = copyout(&sc->sc_info, ifr->ifr_data,
783 1.4.4.2 pgoyette sizeof(sc->sc_info));
784 1.4.4.2 pgoyette break;
785 1.4.4.2 pgoyette case SIOCSUMBPARAM:
786 1.4.4.2 pgoyette error = kauth_authorize_network(curlwp->l_cred,
787 1.4.4.2 pgoyette KAUTH_NETWORK_INTERFACE,
788 1.4.4.2 pgoyette KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, KAUTH_ARG(cmd),
789 1.4.4.2 pgoyette NULL);
790 1.4.4.2 pgoyette if (error)
791 1.4.4.2 pgoyette break;
792 1.4.4.2 pgoyette
793 1.4.4.2 pgoyette if ((error = copyin(ifr->ifr_data, &mp, sizeof(mp))) != 0)
794 1.4.4.2 pgoyette break;
795 1.4.4.2 pgoyette
796 1.4.4.2 pgoyette if ((error = umb_setpin(sc, mp.op, mp.is_puk, mp.pin, mp.pinlen,
797 1.4.4.2 pgoyette mp.newpin, mp.newpinlen)) != 0)
798 1.4.4.2 pgoyette break;
799 1.4.4.2 pgoyette
800 1.4.4.2 pgoyette if (mp.apnlen < 0 || mp.apnlen > sizeof(sc->sc_info.apn)) {
801 1.4.4.2 pgoyette error = EINVAL;
802 1.4.4.2 pgoyette break;
803 1.4.4.2 pgoyette }
804 1.4.4.2 pgoyette sc->sc_roaming = mp.roaming ? 1 : 0;
805 1.4.4.2 pgoyette memset(sc->sc_info.apn, 0, sizeof(sc->sc_info.apn));
806 1.4.4.2 pgoyette memcpy(sc->sc_info.apn, mp.apn, mp.apnlen);
807 1.4.4.2 pgoyette sc->sc_info.apnlen = mp.apnlen;
808 1.4.4.2 pgoyette memset(sc->sc_info.username, 0, sizeof(sc->sc_info.username));
809 1.4.4.2 pgoyette memcpy(sc->sc_info.username, mp.username, mp.usernamelen);
810 1.4.4.2 pgoyette sc->sc_info.usernamelen = mp.usernamelen;
811 1.4.4.2 pgoyette memset(sc->sc_info.password, 0, sizeof(sc->sc_info.password));
812 1.4.4.2 pgoyette memcpy(sc->sc_info.password, mp.password, mp.passwordlen);
813 1.4.4.2 pgoyette sc->sc_info.passwordlen = mp.passwordlen;
814 1.4.4.2 pgoyette sc->sc_info.preferredclasses = mp.preferredclasses;
815 1.4.4.2 pgoyette umb_setdataclass(sc);
816 1.4.4.2 pgoyette break;
817 1.4.4.2 pgoyette case SIOCGUMBPARAM:
818 1.4.4.2 pgoyette memset(&mp, 0, sizeof(mp));
819 1.4.4.2 pgoyette memcpy(mp.apn, sc->sc_info.apn, sc->sc_info.apnlen);
820 1.4.4.2 pgoyette mp.apnlen = sc->sc_info.apnlen;
821 1.4.4.2 pgoyette mp.roaming = sc->sc_roaming;
822 1.4.4.2 pgoyette mp.preferredclasses = sc->sc_info.preferredclasses;
823 1.4.4.2 pgoyette error = copyout(&mp, ifr->ifr_data, sizeof(mp));
824 1.4.4.2 pgoyette break;
825 1.4.4.2 pgoyette case SIOCSIFMTU:
826 1.4.4.2 pgoyette /* Does this include the NCM headers and tail? */
827 1.4.4.2 pgoyette if (ifr->ifr_mtu > ifp->if_mtu) {
828 1.4.4.2 pgoyette error = EINVAL;
829 1.4.4.2 pgoyette break;
830 1.4.4.2 pgoyette }
831 1.4.4.2 pgoyette ifp->if_mtu = ifr->ifr_mtu;
832 1.4.4.2 pgoyette break;
833 1.4.4.2 pgoyette case SIOCSIFADDR:
834 1.4.4.2 pgoyette case SIOCAIFADDR:
835 1.4.4.2 pgoyette case SIOCSIFDSTADDR:
836 1.4.4.2 pgoyette case SIOCADDMULTI:
837 1.4.4.2 pgoyette case SIOCDELMULTI:
838 1.4.4.2 pgoyette break;
839 1.4.4.2 pgoyette case SIOCGIFMEDIA:
840 1.4.4.2 pgoyette error = ifmedia_ioctl(ifp, ifr, &sc->sc_im, cmd);
841 1.4.4.2 pgoyette break;
842 1.4.4.2 pgoyette default:
843 1.4.4.2 pgoyette error = ifioctl_common(ifp, cmd, data);
844 1.4.4.2 pgoyette break;
845 1.4.4.2 pgoyette }
846 1.4.4.2 pgoyette splx(s);
847 1.4.4.2 pgoyette return error;
848 1.4.4.2 pgoyette }
849 1.4.4.2 pgoyette
850 1.4.4.2 pgoyette Static int
851 1.4.4.2 pgoyette umb_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
852 1.4.4.2 pgoyette const struct rtentry *rtp)
853 1.4.4.2 pgoyette {
854 1.4.4.2 pgoyette int error;
855 1.4.4.2 pgoyette
856 1.4.4.2 pgoyette DPRINTFN(10, "%s: %s: enter\n",
857 1.4.4.2 pgoyette device_xname(((struct umb_softc *)ifp->if_softc)->sc_dev),
858 1.4.4.2 pgoyette __func__);
859 1.4.4.2 pgoyette
860 1.4.4.2 pgoyette /*
861 1.4.4.2 pgoyette * if the queueing discipline needs packet classification,
862 1.4.4.2 pgoyette * do it now.
863 1.4.4.2 pgoyette */
864 1.4.4.2 pgoyette IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family);
865 1.4.4.2 pgoyette
866 1.4.4.2 pgoyette /*
867 1.4.4.2 pgoyette * Queue message on interface, and start output if interface
868 1.4.4.2 pgoyette * not yet active.
869 1.4.4.2 pgoyette */
870 1.4.4.2 pgoyette error = if_transmit_lock(ifp, m);
871 1.4.4.2 pgoyette
872 1.4.4.2 pgoyette return error;
873 1.4.4.2 pgoyette }
874 1.4.4.2 pgoyette
875 1.4.4.2 pgoyette Static void
876 1.4.4.2 pgoyette umb_input(struct ifnet *ifp, struct mbuf *m)
877 1.4.4.2 pgoyette {
878 1.4.4.2 pgoyette size_t pktlen = m->m_len;
879 1.4.4.2 pgoyette int s;
880 1.4.4.2 pgoyette
881 1.4.4.2 pgoyette if ((ifp->if_flags & IFF_UP) == 0) {
882 1.4.4.2 pgoyette m_freem(m);
883 1.4.4.2 pgoyette return;
884 1.4.4.2 pgoyette }
885 1.4.4.2 pgoyette if (pktlen < sizeof(struct ip)) {
886 1.4.4.2 pgoyette ifp->if_ierrors++;
887 1.4.4.2 pgoyette DPRINTFN(4, "%s: dropping short packet (len %zd)\n", __func__,
888 1.4.4.2 pgoyette pktlen);
889 1.4.4.2 pgoyette m_freem(m);
890 1.4.4.2 pgoyette return;
891 1.4.4.2 pgoyette }
892 1.4.4.2 pgoyette s = splnet();
893 1.4.4.2 pgoyette if (__predict_false(!pktq_enqueue(ip_pktq, m, 0))) {
894 1.4.4.2 pgoyette ifp->if_iqdrops++;
895 1.4.4.2 pgoyette m_freem(m);
896 1.4.4.2 pgoyette } else {
897 1.4.4.2 pgoyette ifp->if_ipackets++;
898 1.4.4.2 pgoyette ifp->if_ibytes += pktlen;
899 1.4.4.2 pgoyette }
900 1.4.4.2 pgoyette splx(s);
901 1.4.4.2 pgoyette }
902 1.4.4.2 pgoyette
903 1.4.4.2 pgoyette Static void
904 1.4.4.2 pgoyette umb_start(struct ifnet *ifp)
905 1.4.4.2 pgoyette {
906 1.4.4.2 pgoyette struct umb_softc *sc = ifp->if_softc;
907 1.4.4.2 pgoyette struct mbuf *m_head = NULL;
908 1.4.4.2 pgoyette
909 1.4.4.2 pgoyette if (sc->sc_dying || (ifp->if_flags & IFF_OACTIVE))
910 1.4.4.2 pgoyette return;
911 1.4.4.2 pgoyette
912 1.4.4.2 pgoyette IFQ_POLL(&ifp->if_snd, m_head);
913 1.4.4.2 pgoyette if (m_head == NULL)
914 1.4.4.2 pgoyette return;
915 1.4.4.2 pgoyette
916 1.4.4.2 pgoyette if (!umb_encap(sc, m_head)) {
917 1.4.4.2 pgoyette ifp->if_flags |= IFF_OACTIVE;
918 1.4.4.2 pgoyette return;
919 1.4.4.2 pgoyette }
920 1.4.4.2 pgoyette IFQ_DEQUEUE(&ifp->if_snd, m_head);
921 1.4.4.2 pgoyette
922 1.4.4.2 pgoyette bpf_mtap(ifp, m_head, BPF_D_OUT);
923 1.4.4.2 pgoyette
924 1.4.4.2 pgoyette ifp->if_flags |= IFF_OACTIVE;
925 1.4.4.2 pgoyette ifp->if_timer = (2 * umb_xfer_tout) / 1000;
926 1.4.4.2 pgoyette }
927 1.4.4.2 pgoyette
928 1.4.4.2 pgoyette Static void
929 1.4.4.2 pgoyette umb_watchdog(struct ifnet *ifp)
930 1.4.4.2 pgoyette {
931 1.4.4.2 pgoyette struct umb_softc *sc = ifp->if_softc;
932 1.4.4.2 pgoyette
933 1.4.4.2 pgoyette if (sc->sc_dying)
934 1.4.4.2 pgoyette return;
935 1.4.4.2 pgoyette
936 1.4.4.2 pgoyette ifp->if_oerrors++;
937 1.4.4.2 pgoyette printf("%s: watchdog timeout\n", DEVNAM(sc));
938 1.4.4.2 pgoyette usbd_abort_pipe(sc->sc_tx_pipe);
939 1.4.4.2 pgoyette return;
940 1.4.4.2 pgoyette }
941 1.4.4.2 pgoyette
942 1.4.4.2 pgoyette Static void
943 1.4.4.2 pgoyette umb_statechg_timeout(void *arg)
944 1.4.4.2 pgoyette {
945 1.4.4.2 pgoyette struct umb_softc *sc = arg;
946 1.4.4.2 pgoyette
947 1.4.4.3 pgoyette if (sc->sc_info.regstate != MBIM_REGSTATE_ROAMING || sc->sc_roaming)
948 1.4.4.2 pgoyette printf("%s: state change timeout\n",DEVNAM(sc));
949 1.4.4.2 pgoyette usb_add_task(sc->sc_udev, &sc->sc_umb_task, USB_TASKQ_DRIVER);
950 1.4.4.2 pgoyette }
951 1.4.4.2 pgoyette
952 1.4.4.2 pgoyette Static int
953 1.4.4.2 pgoyette umb_mediachange(struct ifnet * ifp)
954 1.4.4.2 pgoyette {
955 1.4.4.2 pgoyette return 0;
956 1.4.4.2 pgoyette }
957 1.4.4.2 pgoyette
958 1.4.4.2 pgoyette Static void
959 1.4.4.2 pgoyette umb_mediastatus(struct ifnet * ifp, struct ifmediareq * imr)
960 1.4.4.2 pgoyette {
961 1.4.4.2 pgoyette switch (ifp->if_link_state) {
962 1.4.4.2 pgoyette case LINK_STATE_UP:
963 1.4.4.2 pgoyette imr->ifm_status = IFM_AVALID | IFM_ACTIVE;
964 1.4.4.2 pgoyette break;
965 1.4.4.2 pgoyette case LINK_STATE_DOWN:
966 1.4.4.2 pgoyette imr->ifm_status = IFM_AVALID;
967 1.4.4.2 pgoyette break;
968 1.4.4.2 pgoyette default:
969 1.4.4.2 pgoyette imr->ifm_status = 0;
970 1.4.4.2 pgoyette break;
971 1.4.4.2 pgoyette }
972 1.4.4.2 pgoyette }
973 1.4.4.2 pgoyette
974 1.4.4.2 pgoyette Static void
975 1.4.4.2 pgoyette umb_newstate(struct umb_softc *sc, enum umb_state newstate, int flags)
976 1.4.4.2 pgoyette {
977 1.4.4.2 pgoyette struct ifnet *ifp = GET_IFP(sc);
978 1.4.4.2 pgoyette
979 1.4.4.2 pgoyette if (newstate == sc->sc_state)
980 1.4.4.2 pgoyette return;
981 1.4.4.2 pgoyette if (((flags & UMB_NS_DONT_DROP) && newstate < sc->sc_state) ||
982 1.4.4.2 pgoyette ((flags & UMB_NS_DONT_RAISE) && newstate > sc->sc_state))
983 1.4.4.2 pgoyette return;
984 1.4.4.2 pgoyette if (ifp->if_flags & IFF_DEBUG)
985 1.4.4.2 pgoyette log(LOG_DEBUG, "%s: state going %s from '%s' to '%s'\n",
986 1.4.4.2 pgoyette DEVNAM(sc), newstate > sc->sc_state ? "up" : "down",
987 1.4.4.2 pgoyette umb_istate(sc->sc_state), umb_istate(newstate));
988 1.4.4.2 pgoyette sc->sc_state = newstate;
989 1.4.4.2 pgoyette usb_add_task(sc->sc_udev, &sc->sc_umb_task, USB_TASKQ_DRIVER);
990 1.4.4.2 pgoyette }
991 1.4.4.2 pgoyette
992 1.4.4.2 pgoyette Static void
993 1.4.4.2 pgoyette umb_state_task(void *arg)
994 1.4.4.2 pgoyette {
995 1.4.4.2 pgoyette struct umb_softc *sc = arg;
996 1.4.4.2 pgoyette struct ifnet *ifp = GET_IFP(sc);
997 1.4.4.2 pgoyette struct ifreq ifr;
998 1.4.4.2 pgoyette int s;
999 1.4.4.2 pgoyette int state;
1000 1.4.4.2 pgoyette
1001 1.4.4.3 pgoyette if (sc->sc_info.regstate == MBIM_REGSTATE_ROAMING && !sc->sc_roaming) {
1002 1.4.4.3 pgoyette /*
1003 1.4.4.3 pgoyette * Query the registration state until we're with the home
1004 1.4.4.3 pgoyette * network again.
1005 1.4.4.3 pgoyette */
1006 1.4.4.3 pgoyette umb_cmd(sc, MBIM_CID_REGISTER_STATE, MBIM_CMDOP_QRY, NULL, 0);
1007 1.4.4.3 pgoyette return;
1008 1.4.4.3 pgoyette }
1009 1.4.4.3 pgoyette
1010 1.4.4.2 pgoyette s = splnet();
1011 1.4.4.2 pgoyette if (ifp->if_flags & IFF_UP)
1012 1.4.4.2 pgoyette umb_up(sc);
1013 1.4.4.2 pgoyette else
1014 1.4.4.2 pgoyette umb_down(sc, 0);
1015 1.4.4.2 pgoyette
1016 1.4.4.2 pgoyette state = sc->sc_state == UMB_S_UP ? LINK_STATE_UP : LINK_STATE_DOWN;
1017 1.4.4.2 pgoyette if (ifp->if_link_state != state) {
1018 1.4.4.2 pgoyette if (ifp->if_flags & IFF_DEBUG)
1019 1.4.4.2 pgoyette log(LOG_DEBUG, "%s: link state changed from %s to %s\n",
1020 1.4.4.2 pgoyette DEVNAM(sc),
1021 1.4.4.2 pgoyette (ifp->if_link_state == LINK_STATE_UP)
1022 1.4.4.2 pgoyette ? "up" : "down",
1023 1.4.4.2 pgoyette (state == LINK_STATE_UP) ? "up" : "down");
1024 1.4.4.2 pgoyette ifp->if_link_state = state;
1025 1.4.4.2 pgoyette if (state != LINK_STATE_UP) {
1026 1.4.4.2 pgoyette /*
1027 1.4.4.2 pgoyette * Purge any existing addresses
1028 1.4.4.2 pgoyette */
1029 1.4.4.2 pgoyette memset(sc->sc_info.ipv4dns, 0,
1030 1.4.4.2 pgoyette sizeof(sc->sc_info.ipv4dns));
1031 1.4.4.2 pgoyette if (in_control(NULL, SIOCGIFADDR, &ifr, ifp) == 0 &&
1032 1.4.4.2 pgoyette satosin(&ifr.ifr_addr)->sin_addr.s_addr !=
1033 1.4.4.2 pgoyette INADDR_ANY) {
1034 1.4.4.2 pgoyette in_control(NULL, SIOCDIFADDR, &ifr, ifp);
1035 1.4.4.2 pgoyette }
1036 1.4.4.2 pgoyette }
1037 1.4.4.2 pgoyette if_link_state_change(ifp, state);
1038 1.4.4.2 pgoyette }
1039 1.4.4.2 pgoyette splx(s);
1040 1.4.4.2 pgoyette }
1041 1.4.4.2 pgoyette
1042 1.4.4.2 pgoyette Static void
1043 1.4.4.2 pgoyette umb_up(struct umb_softc *sc)
1044 1.4.4.2 pgoyette {
1045 1.4.4.2 pgoyette switch (sc->sc_state) {
1046 1.4.4.2 pgoyette case UMB_S_DOWN:
1047 1.4.4.2 pgoyette DPRINTF("%s: init: opening ...\n", DEVNAM(sc));
1048 1.4.4.2 pgoyette umb_open(sc);
1049 1.4.4.2 pgoyette break;
1050 1.4.4.2 pgoyette case UMB_S_OPEN:
1051 1.4.4.2 pgoyette if (sc->sc_flags & UMBFLG_FCC_AUTH_REQUIRED) {
1052 1.4.4.2 pgoyette if (sc->sc_cid == -1) {
1053 1.4.4.2 pgoyette DPRINTF("%s: init: allocating CID ...\n",
1054 1.4.4.2 pgoyette DEVNAM(sc));
1055 1.4.4.2 pgoyette umb_allocate_cid(sc);
1056 1.4.4.2 pgoyette break;
1057 1.4.4.2 pgoyette } else
1058 1.4.4.2 pgoyette umb_newstate(sc, UMB_S_CID, UMB_NS_DONT_DROP);
1059 1.4.4.2 pgoyette } else {
1060 1.4.4.2 pgoyette DPRINTF("%s: init: turning radio on ...\n", DEVNAM(sc));
1061 1.4.4.2 pgoyette umb_radio(sc, 1);
1062 1.4.4.2 pgoyette break;
1063 1.4.4.2 pgoyette }
1064 1.4.4.2 pgoyette /*FALLTHROUGH*/
1065 1.4.4.2 pgoyette case UMB_S_CID:
1066 1.4.4.2 pgoyette DPRINTF("%s: init: sending FCC auth ...\n", DEVNAM(sc));
1067 1.4.4.2 pgoyette umb_send_fcc_auth(sc);
1068 1.4.4.2 pgoyette break;
1069 1.4.4.2 pgoyette case UMB_S_RADIO:
1070 1.4.4.2 pgoyette DPRINTF("%s: init: checking SIM state ...\n", DEVNAM(sc));
1071 1.4.4.2 pgoyette umb_cmd(sc, MBIM_CID_SUBSCRIBER_READY_STATUS, MBIM_CMDOP_QRY,
1072 1.4.4.2 pgoyette NULL, 0);
1073 1.4.4.2 pgoyette break;
1074 1.4.4.2 pgoyette case UMB_S_SIMREADY:
1075 1.4.4.2 pgoyette DPRINTF("%s: init: attaching ...\n", DEVNAM(sc));
1076 1.4.4.2 pgoyette umb_packet_service(sc, 1);
1077 1.4.4.2 pgoyette break;
1078 1.4.4.2 pgoyette case UMB_S_ATTACHED:
1079 1.4.4.2 pgoyette sc->sc_tx_seq = 0;
1080 1.4.4.2 pgoyette DPRINTF("%s: init: connecting ...\n", DEVNAM(sc));
1081 1.4.4.2 pgoyette umb_connect(sc);
1082 1.4.4.2 pgoyette break;
1083 1.4.4.2 pgoyette case UMB_S_CONNECTED:
1084 1.4.4.2 pgoyette DPRINTF("%s: init: getting IP config ...\n", DEVNAM(sc));
1085 1.4.4.2 pgoyette umb_qry_ipconfig(sc);
1086 1.4.4.2 pgoyette break;
1087 1.4.4.2 pgoyette case UMB_S_UP:
1088 1.4.4.2 pgoyette DPRINTF("%s: init: reached state UP\n", DEVNAM(sc));
1089 1.4.4.2 pgoyette if (!umb_alloc_bulkpipes(sc)) {
1090 1.4.4.2 pgoyette printf("%s: opening bulk pipes failed\n", DEVNAM(sc));
1091 1.4.4.2 pgoyette umb_down(sc, 1);
1092 1.4.4.2 pgoyette }
1093 1.4.4.2 pgoyette break;
1094 1.4.4.2 pgoyette }
1095 1.4.4.2 pgoyette if (sc->sc_state < UMB_S_UP)
1096 1.4.4.2 pgoyette callout_schedule(&sc->sc_statechg_timer,
1097 1.4.4.2 pgoyette UMB_STATE_CHANGE_TIMEOUT * hz);
1098 1.4.4.2 pgoyette else
1099 1.4.4.2 pgoyette callout_stop(&sc->sc_statechg_timer);
1100 1.4.4.2 pgoyette return;
1101 1.4.4.2 pgoyette }
1102 1.4.4.2 pgoyette
1103 1.4.4.2 pgoyette Static void
1104 1.4.4.2 pgoyette umb_down(struct umb_softc *sc, int force)
1105 1.4.4.2 pgoyette {
1106 1.4.4.2 pgoyette umb_close_bulkpipes(sc);
1107 1.4.4.2 pgoyette if (sc->sc_state < UMB_S_CONNECTED)
1108 1.4.4.2 pgoyette umb_free_xfers(sc);
1109 1.4.4.2 pgoyette
1110 1.4.4.2 pgoyette switch (sc->sc_state) {
1111 1.4.4.2 pgoyette case UMB_S_UP:
1112 1.4.4.2 pgoyette case UMB_S_CONNECTED:
1113 1.4.4.2 pgoyette DPRINTF("%s: stop: disconnecting ...\n", DEVNAM(sc));
1114 1.4.4.2 pgoyette umb_disconnect(sc);
1115 1.4.4.2 pgoyette if (!force)
1116 1.4.4.2 pgoyette break;
1117 1.4.4.2 pgoyette /*FALLTHROUGH*/
1118 1.4.4.2 pgoyette case UMB_S_ATTACHED:
1119 1.4.4.2 pgoyette DPRINTF("%s: stop: detaching ...\n", DEVNAM(sc));
1120 1.4.4.2 pgoyette umb_packet_service(sc, 0);
1121 1.4.4.2 pgoyette if (!force)
1122 1.4.4.2 pgoyette break;
1123 1.4.4.2 pgoyette /*FALLTHROUGH*/
1124 1.4.4.2 pgoyette case UMB_S_SIMREADY:
1125 1.4.4.2 pgoyette case UMB_S_RADIO:
1126 1.4.4.2 pgoyette DPRINTF("%s: stop: turning radio off ...\n", DEVNAM(sc));
1127 1.4.4.2 pgoyette umb_radio(sc, 0);
1128 1.4.4.2 pgoyette if (!force)
1129 1.4.4.2 pgoyette break;
1130 1.4.4.2 pgoyette /*FALLTHROUGH*/
1131 1.4.4.2 pgoyette case UMB_S_CID:
1132 1.4.4.2 pgoyette case UMB_S_OPEN:
1133 1.4.4.2 pgoyette case UMB_S_DOWN:
1134 1.4.4.2 pgoyette /* Do not close the device */
1135 1.4.4.2 pgoyette DPRINTF("%s: stop: reached state DOWN\n", DEVNAM(sc));
1136 1.4.4.2 pgoyette break;
1137 1.4.4.2 pgoyette }
1138 1.4.4.2 pgoyette if (force)
1139 1.4.4.2 pgoyette sc->sc_state = UMB_S_OPEN;
1140 1.4.4.2 pgoyette
1141 1.4.4.2 pgoyette if (sc->sc_state > UMB_S_OPEN)
1142 1.4.4.2 pgoyette callout_schedule(&sc->sc_statechg_timer,
1143 1.4.4.2 pgoyette UMB_STATE_CHANGE_TIMEOUT * hz);
1144 1.4.4.2 pgoyette else
1145 1.4.4.2 pgoyette callout_stop(&sc->sc_statechg_timer);
1146 1.4.4.2 pgoyette }
1147 1.4.4.2 pgoyette
1148 1.4.4.2 pgoyette Static void
1149 1.4.4.2 pgoyette umb_get_response_task(void *arg)
1150 1.4.4.2 pgoyette {
1151 1.4.4.2 pgoyette struct umb_softc *sc = arg;
1152 1.4.4.2 pgoyette int len;
1153 1.4.4.2 pgoyette int s;
1154 1.4.4.2 pgoyette
1155 1.4.4.2 pgoyette /*
1156 1.4.4.2 pgoyette * Function is required to send on RESPONSE_AVAILABLE notification for
1157 1.4.4.2 pgoyette * each encapsulated response that is to be processed by the host.
1158 1.4.4.2 pgoyette * But of course, we can receive multiple notifications before the
1159 1.4.4.2 pgoyette * response task is run.
1160 1.4.4.2 pgoyette */
1161 1.4.4.2 pgoyette s = splusb();
1162 1.4.4.2 pgoyette while (sc->sc_nresp > 0) {
1163 1.4.4.2 pgoyette --sc->sc_nresp;
1164 1.4.4.2 pgoyette len = sc->sc_ctrl_len;
1165 1.4.4.2 pgoyette if (umb_get_encap_response(sc, sc->sc_resp_buf, &len))
1166 1.4.4.2 pgoyette umb_decode_response(sc, sc->sc_resp_buf, len);
1167 1.4.4.2 pgoyette }
1168 1.4.4.2 pgoyette splx(s);
1169 1.4.4.2 pgoyette }
1170 1.4.4.2 pgoyette
1171 1.4.4.2 pgoyette Static void
1172 1.4.4.2 pgoyette umb_decode_response(struct umb_softc *sc, void *response, int len)
1173 1.4.4.2 pgoyette {
1174 1.4.4.2 pgoyette struct mbim_msghdr *hdr = response;
1175 1.4.4.2 pgoyette struct mbim_fragmented_msg_hdr *fraghdr;
1176 1.4.4.2 pgoyette uint32_t type;
1177 1.4.4.2 pgoyette
1178 1.4.4.2 pgoyette DPRINTFN(3, "%s: got response: len %d\n", DEVNAM(sc), len);
1179 1.4.4.2 pgoyette DDUMPN(4, response, len);
1180 1.4.4.2 pgoyette
1181 1.4.4.2 pgoyette if (len < sizeof(*hdr) || le32toh(hdr->len) != len) {
1182 1.4.4.2 pgoyette /*
1183 1.4.4.2 pgoyette * We should probably cancel a transaction, but since the
1184 1.4.4.2 pgoyette * message is too short, we cannot decode the transaction
1185 1.4.4.2 pgoyette * id (tid) and hence don't know, whom to cancel. Must wait
1186 1.4.4.2 pgoyette * for the timeout.
1187 1.4.4.2 pgoyette */
1188 1.4.4.2 pgoyette DPRINTF("%s: received short response (len %d)\n",
1189 1.4.4.2 pgoyette DEVNAM(sc), len);
1190 1.4.4.2 pgoyette return;
1191 1.4.4.2 pgoyette }
1192 1.4.4.2 pgoyette
1193 1.4.4.2 pgoyette /*
1194 1.4.4.2 pgoyette * XXX FIXME: if message is fragmented, store it until last frag
1195 1.4.4.2 pgoyette * is received and then re-assemble all fragments.
1196 1.4.4.2 pgoyette */
1197 1.4.4.2 pgoyette type = le32toh(hdr->type);
1198 1.4.4.2 pgoyette switch (type) {
1199 1.4.4.2 pgoyette case MBIM_INDICATE_STATUS_MSG:
1200 1.4.4.2 pgoyette case MBIM_COMMAND_DONE:
1201 1.4.4.2 pgoyette fraghdr = response;
1202 1.4.4.2 pgoyette if (le32toh(fraghdr->frag.nfrag) != 1) {
1203 1.4.4.2 pgoyette DPRINTF("%s: discarding fragmented messages\n",
1204 1.4.4.2 pgoyette DEVNAM(sc));
1205 1.4.4.2 pgoyette return;
1206 1.4.4.2 pgoyette }
1207 1.4.4.2 pgoyette break;
1208 1.4.4.2 pgoyette default:
1209 1.4.4.2 pgoyette break;
1210 1.4.4.2 pgoyette }
1211 1.4.4.2 pgoyette
1212 1.4.4.2 pgoyette DPRINTF("%s: <- rcv %s (tid %u)\n", DEVNAM(sc), umb_request2str(type),
1213 1.4.4.2 pgoyette le32toh(hdr->tid));
1214 1.4.4.2 pgoyette switch (type) {
1215 1.4.4.2 pgoyette case MBIM_FUNCTION_ERROR_MSG:
1216 1.4.4.2 pgoyette case MBIM_HOST_ERROR_MSG:
1217 1.4.4.2 pgoyette {
1218 1.4.4.2 pgoyette struct mbim_f2h_hosterr *e;
1219 1.4.4.2 pgoyette int err;
1220 1.4.4.2 pgoyette
1221 1.4.4.2 pgoyette if (len >= sizeof(*e)) {
1222 1.4.4.2 pgoyette e = response;
1223 1.4.4.2 pgoyette err = le32toh(e->err);
1224 1.4.4.2 pgoyette
1225 1.4.4.2 pgoyette DPRINTF("%s: %s message, error %s (tid %u)\n",
1226 1.4.4.2 pgoyette DEVNAM(sc), umb_request2str(type),
1227 1.4.4.2 pgoyette umb_error2str(err), le32toh(hdr->tid));
1228 1.4.4.2 pgoyette if (err == MBIM_ERROR_NOT_OPENED)
1229 1.4.4.2 pgoyette umb_newstate(sc, UMB_S_DOWN, 0);
1230 1.4.4.2 pgoyette }
1231 1.4.4.2 pgoyette break;
1232 1.4.4.2 pgoyette }
1233 1.4.4.2 pgoyette case MBIM_INDICATE_STATUS_MSG:
1234 1.4.4.2 pgoyette umb_handle_indicate_status_msg(sc, response, len);
1235 1.4.4.2 pgoyette break;
1236 1.4.4.2 pgoyette case MBIM_OPEN_DONE:
1237 1.4.4.2 pgoyette umb_handle_opendone_msg(sc, response, len);
1238 1.4.4.2 pgoyette break;
1239 1.4.4.2 pgoyette case MBIM_CLOSE_DONE:
1240 1.4.4.2 pgoyette umb_handle_closedone_msg(sc, response, len);
1241 1.4.4.2 pgoyette break;
1242 1.4.4.2 pgoyette case MBIM_COMMAND_DONE:
1243 1.4.4.2 pgoyette umb_command_done(sc, response, len);
1244 1.4.4.2 pgoyette break;
1245 1.4.4.2 pgoyette default:
1246 1.4.4.2 pgoyette DPRINTF("%s: discard messsage %s\n", DEVNAM(sc),
1247 1.4.4.2 pgoyette umb_request2str(type));
1248 1.4.4.2 pgoyette break;
1249 1.4.4.2 pgoyette }
1250 1.4.4.2 pgoyette }
1251 1.4.4.2 pgoyette
1252 1.4.4.2 pgoyette Static void
1253 1.4.4.2 pgoyette umb_handle_indicate_status_msg(struct umb_softc *sc, void *data, int len)
1254 1.4.4.2 pgoyette {
1255 1.4.4.2 pgoyette struct mbim_f2h_indicate_status *m = data;
1256 1.4.4.2 pgoyette uint32_t infolen;
1257 1.4.4.2 pgoyette uint32_t cid;
1258 1.4.4.2 pgoyette
1259 1.4.4.2 pgoyette if (len < sizeof(*m)) {
1260 1.4.4.2 pgoyette DPRINTF("%s: discard short %s messsage\n", DEVNAM(sc),
1261 1.4.4.2 pgoyette umb_request2str(le32toh(m->hdr.type)));
1262 1.4.4.2 pgoyette return;
1263 1.4.4.2 pgoyette }
1264 1.4.4.2 pgoyette if (memcmp(m->devid, umb_uuid_basic_connect, sizeof(m->devid))) {
1265 1.4.4.2 pgoyette DPRINTF("%s: discard %s messsage for other UUID '%s'\n",
1266 1.4.4.2 pgoyette DEVNAM(sc), umb_request2str(le32toh(m->hdr.type)),
1267 1.4.4.2 pgoyette umb_uuid2str(m->devid));
1268 1.4.4.2 pgoyette return;
1269 1.4.4.2 pgoyette }
1270 1.4.4.2 pgoyette infolen = le32toh(m->infolen);
1271 1.4.4.2 pgoyette if (len < sizeof(*m) + infolen) {
1272 1.4.4.2 pgoyette DPRINTF("%s: discard truncated %s messsage (want %d, got %d)\n",
1273 1.4.4.2 pgoyette DEVNAM(sc), umb_request2str(le32toh(m->hdr.type)),
1274 1.4.4.2 pgoyette (int)sizeof(*m) + infolen, len);
1275 1.4.4.2 pgoyette return;
1276 1.4.4.2 pgoyette }
1277 1.4.4.2 pgoyette
1278 1.4.4.2 pgoyette cid = le32toh(m->cid);
1279 1.4.4.2 pgoyette DPRINTF("%s: indicate %s status\n", DEVNAM(sc), umb_cid2str(cid));
1280 1.4.4.2 pgoyette umb_decode_cid(sc, cid, m->info, infolen);
1281 1.4.4.2 pgoyette }
1282 1.4.4.2 pgoyette
1283 1.4.4.2 pgoyette Static void
1284 1.4.4.2 pgoyette umb_handle_opendone_msg(struct umb_softc *sc, void *data, int len)
1285 1.4.4.2 pgoyette {
1286 1.4.4.2 pgoyette struct mbim_f2h_openclosedone *resp = data;
1287 1.4.4.2 pgoyette struct ifnet *ifp = GET_IFP(sc);
1288 1.4.4.2 pgoyette uint32_t status;
1289 1.4.4.2 pgoyette
1290 1.4.4.2 pgoyette status = le32toh(resp->status);
1291 1.4.4.2 pgoyette if (status == MBIM_STATUS_SUCCESS) {
1292 1.4.4.2 pgoyette if (sc->sc_maxsessions == 0) {
1293 1.4.4.2 pgoyette umb_cmd(sc, MBIM_CID_DEVICE_CAPS, MBIM_CMDOP_QRY, NULL,
1294 1.4.4.2 pgoyette 0);
1295 1.4.4.2 pgoyette umb_cmd(sc, MBIM_CID_PIN, MBIM_CMDOP_QRY, NULL, 0);
1296 1.4.4.2 pgoyette umb_cmd(sc, MBIM_CID_REGISTER_STATE, MBIM_CMDOP_QRY,
1297 1.4.4.2 pgoyette NULL, 0);
1298 1.4.4.2 pgoyette }
1299 1.4.4.2 pgoyette umb_newstate(sc, UMB_S_OPEN, UMB_NS_DONT_DROP);
1300 1.4.4.2 pgoyette } else if (ifp->if_flags & IFF_DEBUG)
1301 1.4.4.2 pgoyette log(LOG_ERR, "%s: open error: %s\n", DEVNAM(sc),
1302 1.4.4.2 pgoyette umb_status2str(status));
1303 1.4.4.2 pgoyette return;
1304 1.4.4.2 pgoyette }
1305 1.4.4.2 pgoyette
1306 1.4.4.2 pgoyette Static void
1307 1.4.4.2 pgoyette umb_handle_closedone_msg(struct umb_softc *sc, void *data, int len)
1308 1.4.4.2 pgoyette {
1309 1.4.4.2 pgoyette struct mbim_f2h_openclosedone *resp = data;
1310 1.4.4.2 pgoyette uint32_t status;
1311 1.4.4.2 pgoyette
1312 1.4.4.2 pgoyette status = le32toh(resp->status);
1313 1.4.4.2 pgoyette if (status == MBIM_STATUS_SUCCESS)
1314 1.4.4.2 pgoyette umb_newstate(sc, UMB_S_DOWN, 0);
1315 1.4.4.2 pgoyette else
1316 1.4.4.2 pgoyette DPRINTF("%s: close error: %s\n", DEVNAM(sc),
1317 1.4.4.2 pgoyette umb_status2str(status));
1318 1.4.4.2 pgoyette return;
1319 1.4.4.2 pgoyette }
1320 1.4.4.2 pgoyette
1321 1.4.4.2 pgoyette static inline void
1322 1.4.4.2 pgoyette umb_getinfobuf(char *in, int inlen, uint32_t offs, uint32_t sz,
1323 1.4.4.2 pgoyette void *out, size_t outlen)
1324 1.4.4.2 pgoyette {
1325 1.4.4.2 pgoyette offs = le32toh(offs);
1326 1.4.4.2 pgoyette sz = le32toh(sz);
1327 1.4.4.2 pgoyette if (inlen >= offs + sz) {
1328 1.4.4.2 pgoyette memset(out, 0, outlen);
1329 1.4.4.2 pgoyette memcpy(out, in + offs, MIN(sz, outlen));
1330 1.4.4.2 pgoyette }
1331 1.4.4.2 pgoyette }
1332 1.4.4.2 pgoyette
1333 1.4.4.2 pgoyette static inline int
1334 1.4.4.2 pgoyette umb_padding(void *data, int len, size_t sz)
1335 1.4.4.2 pgoyette {
1336 1.4.4.2 pgoyette char *p = data;
1337 1.4.4.2 pgoyette int np = 0;
1338 1.4.4.2 pgoyette
1339 1.4.4.2 pgoyette while (len < sz && (len % 4) != 0) {
1340 1.4.4.2 pgoyette *p++ = '\0';
1341 1.4.4.2 pgoyette len++;
1342 1.4.4.2 pgoyette np++;
1343 1.4.4.2 pgoyette }
1344 1.4.4.2 pgoyette return np;
1345 1.4.4.2 pgoyette }
1346 1.4.4.2 pgoyette
1347 1.4.4.2 pgoyette static inline int
1348 1.4.4.2 pgoyette umb_addstr(void *buf, size_t bufsz, int *offs, void *str, int slen,
1349 1.4.4.2 pgoyette uint32_t *offsmember, uint32_t *sizemember)
1350 1.4.4.2 pgoyette {
1351 1.4.4.2 pgoyette if (*offs + slen > bufsz)
1352 1.4.4.2 pgoyette return 0;
1353 1.4.4.2 pgoyette
1354 1.4.4.2 pgoyette *sizemember = htole32((uint32_t)slen);
1355 1.4.4.2 pgoyette if (slen && str) {
1356 1.4.4.2 pgoyette *offsmember = htole32((uint32_t)*offs);
1357 1.4.4.2 pgoyette memcpy((char *)buf + *offs, str, slen);
1358 1.4.4.2 pgoyette *offs += slen;
1359 1.4.4.2 pgoyette *offs += umb_padding(buf, *offs, bufsz);
1360 1.4.4.2 pgoyette } else
1361 1.4.4.2 pgoyette *offsmember = htole32(0);
1362 1.4.4.2 pgoyette return 1;
1363 1.4.4.2 pgoyette }
1364 1.4.4.2 pgoyette
1365 1.4.4.2 pgoyette static void
1366 1.4.4.2 pgoyette umb_in_len2mask(struct in_addr *mask, int len)
1367 1.4.4.2 pgoyette {
1368 1.4.4.2 pgoyette int i;
1369 1.4.4.2 pgoyette u_char *p;
1370 1.4.4.2 pgoyette
1371 1.4.4.2 pgoyette p = (u_char *)mask;
1372 1.4.4.2 pgoyette memset(mask, 0, sizeof(*mask));
1373 1.4.4.2 pgoyette for (i = 0; i < len / 8; i++)
1374 1.4.4.2 pgoyette p[i] = 0xff;
1375 1.4.4.2 pgoyette if (len % 8)
1376 1.4.4.2 pgoyette p[i] = (0xff00 >> (len % 8)) & 0xff;
1377 1.4.4.2 pgoyette }
1378 1.4.4.2 pgoyette
1379 1.4.4.2 pgoyette Static int
1380 1.4.4.2 pgoyette umb_decode_register_state(struct umb_softc *sc, void *data, int len)
1381 1.4.4.2 pgoyette {
1382 1.4.4.2 pgoyette struct mbim_cid_registration_state_info *rs = data;
1383 1.4.4.2 pgoyette struct ifnet *ifp = GET_IFP(sc);
1384 1.4.4.2 pgoyette
1385 1.4.4.2 pgoyette if (len < sizeof(*rs))
1386 1.4.4.2 pgoyette return 0;
1387 1.4.4.2 pgoyette sc->sc_info.nwerror = le32toh(rs->nwerror);
1388 1.4.4.2 pgoyette sc->sc_info.regstate = le32toh(rs->regstate);
1389 1.4.4.2 pgoyette sc->sc_info.regmode = le32toh(rs->regmode);
1390 1.4.4.2 pgoyette sc->sc_info.cellclass = le32toh(rs->curcellclass);
1391 1.4.4.2 pgoyette
1392 1.4.4.2 pgoyette /* XXX should we remember the provider_id? */
1393 1.4.4.2 pgoyette umb_getinfobuf(data, len, rs->provname_offs, rs->provname_size,
1394 1.4.4.2 pgoyette sc->sc_info.provider, sizeof(sc->sc_info.provider));
1395 1.4.4.2 pgoyette umb_getinfobuf(data, len, rs->roamingtxt_offs, rs->roamingtxt_size,
1396 1.4.4.2 pgoyette sc->sc_info.roamingtxt, sizeof(sc->sc_info.roamingtxt));
1397 1.4.4.2 pgoyette
1398 1.4.4.2 pgoyette DPRINTFN(2, "%s: %s, availclass 0x%x, class 0x%x, regmode %d\n",
1399 1.4.4.2 pgoyette DEVNAM(sc), umb_regstate(sc->sc_info.regstate),
1400 1.4.4.2 pgoyette le32toh(rs->availclasses), sc->sc_info.cellclass,
1401 1.4.4.2 pgoyette sc->sc_info.regmode);
1402 1.4.4.2 pgoyette
1403 1.4.4.2 pgoyette if (sc->sc_info.regstate == MBIM_REGSTATE_ROAMING &&
1404 1.4.4.2 pgoyette !sc->sc_roaming &&
1405 1.4.4.2 pgoyette sc->sc_info.activation == MBIM_ACTIVATION_STATE_ACTIVATED) {
1406 1.4.4.2 pgoyette if (ifp->if_flags & IFF_DEBUG)
1407 1.4.4.2 pgoyette log(LOG_INFO,
1408 1.4.4.2 pgoyette "%s: disconnecting from roaming network\n",
1409 1.4.4.2 pgoyette DEVNAM(sc));
1410 1.4.4.2 pgoyette umb_disconnect(sc);
1411 1.4.4.2 pgoyette }
1412 1.4.4.2 pgoyette return 1;
1413 1.4.4.2 pgoyette }
1414 1.4.4.2 pgoyette
1415 1.4.4.2 pgoyette Static int
1416 1.4.4.2 pgoyette umb_decode_devices_caps(struct umb_softc *sc, void *data, int len)
1417 1.4.4.2 pgoyette {
1418 1.4.4.2 pgoyette struct mbim_cid_device_caps *dc = data;
1419 1.4.4.2 pgoyette
1420 1.4.4.2 pgoyette if (len < sizeof(*dc))
1421 1.4.4.2 pgoyette return 0;
1422 1.4.4.2 pgoyette sc->sc_maxsessions = le32toh(dc->max_sessions);
1423 1.4.4.2 pgoyette sc->sc_info.supportedclasses = le32toh(dc->dataclass);
1424 1.4.4.2 pgoyette umb_getinfobuf(data, len, dc->devid_offs, dc->devid_size,
1425 1.4.4.2 pgoyette sc->sc_info.devid, sizeof(sc->sc_info.devid));
1426 1.4.4.2 pgoyette umb_getinfobuf(data, len, dc->fwinfo_offs, dc->fwinfo_size,
1427 1.4.4.2 pgoyette sc->sc_info.fwinfo, sizeof(sc->sc_info.fwinfo));
1428 1.4.4.2 pgoyette umb_getinfobuf(data, len, dc->hwinfo_offs, dc->hwinfo_size,
1429 1.4.4.2 pgoyette sc->sc_info.hwinfo, sizeof(sc->sc_info.hwinfo));
1430 1.4.4.2 pgoyette DPRINTFN(2, "%s: max sessions %d, supported classes 0x%x\n",
1431 1.4.4.2 pgoyette DEVNAM(sc), sc->sc_maxsessions, sc->sc_info.supportedclasses);
1432 1.4.4.2 pgoyette return 1;
1433 1.4.4.2 pgoyette }
1434 1.4.4.2 pgoyette
1435 1.4.4.2 pgoyette Static int
1436 1.4.4.2 pgoyette umb_decode_subscriber_status(struct umb_softc *sc, void *data, int len)
1437 1.4.4.2 pgoyette {
1438 1.4.4.2 pgoyette struct mbim_cid_subscriber_ready_info *si = data;
1439 1.4.4.2 pgoyette struct ifnet *ifp = GET_IFP(sc);
1440 1.4.4.2 pgoyette int npn;
1441 1.4.4.2 pgoyette
1442 1.4.4.2 pgoyette if (len < sizeof(*si))
1443 1.4.4.2 pgoyette return 0;
1444 1.4.4.2 pgoyette sc->sc_info.sim_state = le32toh(si->ready);
1445 1.4.4.2 pgoyette
1446 1.4.4.2 pgoyette umb_getinfobuf(data, len, si->sid_offs, si->sid_size,
1447 1.4.4.2 pgoyette sc->sc_info.sid, sizeof(sc->sc_info.sid));
1448 1.4.4.2 pgoyette umb_getinfobuf(data, len, si->icc_offs, si->icc_size,
1449 1.4.4.2 pgoyette sc->sc_info.iccid, sizeof(sc->sc_info.iccid));
1450 1.4.4.2 pgoyette
1451 1.4.4.2 pgoyette npn = le32toh(si->no_pn);
1452 1.4.4.2 pgoyette if (npn > 0)
1453 1.4.4.2 pgoyette umb_getinfobuf(data, len, si->pn[0].offs, si->pn[0].size,
1454 1.4.4.2 pgoyette sc->sc_info.pn, sizeof(sc->sc_info.pn));
1455 1.4.4.2 pgoyette else
1456 1.4.4.2 pgoyette memset(sc->sc_info.pn, 0, sizeof(sc->sc_info.pn));
1457 1.4.4.2 pgoyette
1458 1.4.4.2 pgoyette if (sc->sc_info.sim_state == MBIM_SIMSTATE_LOCKED)
1459 1.4.4.2 pgoyette sc->sc_info.pin_state = UMB_PUK_REQUIRED;
1460 1.4.4.2 pgoyette if (ifp->if_flags & IFF_DEBUG)
1461 1.4.4.2 pgoyette log(LOG_INFO, "%s: SIM %s\n", DEVNAM(sc),
1462 1.4.4.2 pgoyette umb_simstate(sc->sc_info.sim_state));
1463 1.4.4.2 pgoyette if (sc->sc_info.sim_state == MBIM_SIMSTATE_INITIALIZED)
1464 1.4.4.2 pgoyette umb_newstate(sc, UMB_S_SIMREADY, UMB_NS_DONT_DROP);
1465 1.4.4.2 pgoyette return 1;
1466 1.4.4.2 pgoyette }
1467 1.4.4.2 pgoyette
1468 1.4.4.2 pgoyette Static int
1469 1.4.4.2 pgoyette umb_decode_radio_state(struct umb_softc *sc, void *data, int len)
1470 1.4.4.2 pgoyette {
1471 1.4.4.2 pgoyette struct mbim_cid_radio_state_info *rs = data;
1472 1.4.4.2 pgoyette struct ifnet *ifp = GET_IFP(sc);
1473 1.4.4.2 pgoyette
1474 1.4.4.2 pgoyette if (len < sizeof(*rs))
1475 1.4.4.2 pgoyette return 0;
1476 1.4.4.2 pgoyette
1477 1.4.4.2 pgoyette sc->sc_info.hw_radio_on =
1478 1.4.4.2 pgoyette (le32toh(rs->hw_state) == MBIM_RADIO_STATE_ON) ? 1 : 0;
1479 1.4.4.2 pgoyette sc->sc_info.sw_radio_on =
1480 1.4.4.2 pgoyette (le32toh(rs->sw_state) == MBIM_RADIO_STATE_ON) ? 1 : 0;
1481 1.4.4.2 pgoyette if (!sc->sc_info.hw_radio_on) {
1482 1.4.4.2 pgoyette printf("%s: radio is disabled by hardware switch\n",
1483 1.4.4.2 pgoyette DEVNAM(sc));
1484 1.4.4.2 pgoyette /*
1485 1.4.4.2 pgoyette * XXX do we need a time to poll the state of the rfkill switch
1486 1.4.4.2 pgoyette * or will the device send an unsolicited notification
1487 1.4.4.2 pgoyette * in case the state changes?
1488 1.4.4.2 pgoyette */
1489 1.4.4.2 pgoyette umb_newstate(sc, UMB_S_OPEN, 0);
1490 1.4.4.2 pgoyette } else if (!sc->sc_info.sw_radio_on) {
1491 1.4.4.2 pgoyette if (ifp->if_flags & IFF_DEBUG)
1492 1.4.4.2 pgoyette log(LOG_INFO, "%s: radio is off\n", DEVNAM(sc));
1493 1.4.4.2 pgoyette umb_newstate(sc, UMB_S_OPEN, 0);
1494 1.4.4.2 pgoyette } else
1495 1.4.4.2 pgoyette umb_newstate(sc, UMB_S_RADIO, UMB_NS_DONT_DROP);
1496 1.4.4.2 pgoyette return 1;
1497 1.4.4.2 pgoyette }
1498 1.4.4.2 pgoyette
1499 1.4.4.2 pgoyette Static int
1500 1.4.4.2 pgoyette umb_decode_pin(struct umb_softc *sc, void *data, int len)
1501 1.4.4.2 pgoyette {
1502 1.4.4.2 pgoyette struct mbim_cid_pin_info *pi = data;
1503 1.4.4.2 pgoyette struct ifnet *ifp = GET_IFP(sc);
1504 1.4.4.2 pgoyette uint32_t attempts_left;
1505 1.4.4.2 pgoyette
1506 1.4.4.2 pgoyette if (len < sizeof(*pi))
1507 1.4.4.2 pgoyette return 0;
1508 1.4.4.2 pgoyette
1509 1.4.4.2 pgoyette attempts_left = le32toh(pi->remaining_attempts);
1510 1.4.4.2 pgoyette if (attempts_left != 0xffffffff)
1511 1.4.4.2 pgoyette sc->sc_info.pin_attempts_left = attempts_left;
1512 1.4.4.2 pgoyette
1513 1.4.4.2 pgoyette switch (le32toh(pi->state)) {
1514 1.4.4.2 pgoyette case MBIM_PIN_STATE_UNLOCKED:
1515 1.4.4.2 pgoyette sc->sc_info.pin_state = UMB_PIN_UNLOCKED;
1516 1.4.4.2 pgoyette break;
1517 1.4.4.2 pgoyette case MBIM_PIN_STATE_LOCKED:
1518 1.4.4.2 pgoyette switch (le32toh(pi->type)) {
1519 1.4.4.2 pgoyette case MBIM_PIN_TYPE_PIN1:
1520 1.4.4.2 pgoyette sc->sc_info.pin_state = UMB_PIN_REQUIRED;
1521 1.4.4.2 pgoyette break;
1522 1.4.4.2 pgoyette case MBIM_PIN_TYPE_PUK1:
1523 1.4.4.2 pgoyette sc->sc_info.pin_state = UMB_PUK_REQUIRED;
1524 1.4.4.2 pgoyette break;
1525 1.4.4.2 pgoyette case MBIM_PIN_TYPE_PIN2:
1526 1.4.4.2 pgoyette case MBIM_PIN_TYPE_PUK2:
1527 1.4.4.2 pgoyette /* Assume that PIN1 was accepted */
1528 1.4.4.2 pgoyette sc->sc_info.pin_state = UMB_PIN_UNLOCKED;
1529 1.4.4.2 pgoyette break;
1530 1.4.4.2 pgoyette }
1531 1.4.4.2 pgoyette break;
1532 1.4.4.2 pgoyette }
1533 1.4.4.2 pgoyette if (ifp->if_flags & IFF_DEBUG)
1534 1.4.4.2 pgoyette log(LOG_INFO, "%s: %s state %s (%d attempts left)\n",
1535 1.4.4.2 pgoyette DEVNAM(sc), umb_pin_type(le32toh(pi->type)),
1536 1.4.4.2 pgoyette (le32toh(pi->state) == MBIM_PIN_STATE_UNLOCKED) ?
1537 1.4.4.2 pgoyette "unlocked" : "locked",
1538 1.4.4.2 pgoyette le32toh(pi->remaining_attempts));
1539 1.4.4.2 pgoyette
1540 1.4.4.2 pgoyette /*
1541 1.4.4.2 pgoyette * In case the PIN was set after IFF_UP, retrigger the state machine
1542 1.4.4.2 pgoyette */
1543 1.4.4.2 pgoyette usb_add_task(sc->sc_udev, &sc->sc_umb_task, USB_TASKQ_DRIVER);
1544 1.4.4.2 pgoyette return 1;
1545 1.4.4.2 pgoyette }
1546 1.4.4.2 pgoyette
1547 1.4.4.2 pgoyette Static int
1548 1.4.4.2 pgoyette umb_decode_packet_service(struct umb_softc *sc, void *data, int len)
1549 1.4.4.2 pgoyette {
1550 1.4.4.2 pgoyette struct mbim_cid_packet_service_info *psi = data;
1551 1.4.4.2 pgoyette int state, highestclass;
1552 1.4.4.2 pgoyette uint64_t up_speed, down_speed;
1553 1.4.4.2 pgoyette struct ifnet *ifp = GET_IFP(sc);
1554 1.4.4.2 pgoyette
1555 1.4.4.2 pgoyette if (len < sizeof(*psi))
1556 1.4.4.2 pgoyette return 0;
1557 1.4.4.2 pgoyette
1558 1.4.4.2 pgoyette sc->sc_info.nwerror = le32toh(psi->nwerror);
1559 1.4.4.2 pgoyette state = le32toh(psi->state);
1560 1.4.4.2 pgoyette highestclass = le32toh(psi->highest_dataclass);
1561 1.4.4.2 pgoyette up_speed = le64toh(psi->uplink_speed);
1562 1.4.4.2 pgoyette down_speed = le64toh(psi->downlink_speed);
1563 1.4.4.2 pgoyette if (sc->sc_info.packetstate != state ||
1564 1.4.4.2 pgoyette sc->sc_info.uplink_speed != up_speed ||
1565 1.4.4.2 pgoyette sc->sc_info.downlink_speed != down_speed) {
1566 1.4.4.2 pgoyette if (ifp->if_flags & IFF_DEBUG) {
1567 1.4.4.2 pgoyette log(LOG_INFO, "%s: packet service ", DEVNAM(sc));
1568 1.4.4.2 pgoyette if (sc->sc_info.packetstate != state)
1569 1.4.4.2 pgoyette addlog("changed from %s to ",
1570 1.4.4.2 pgoyette umb_packet_state(sc->sc_info.packetstate));
1571 1.4.4.2 pgoyette addlog("%s, class %s, speed: %" PRIu64 " up / %" PRIu64 " down\n",
1572 1.4.4.2 pgoyette umb_packet_state(state),
1573 1.4.4.2 pgoyette umb_dataclass(highestclass), up_speed, down_speed);
1574 1.4.4.2 pgoyette }
1575 1.4.4.2 pgoyette }
1576 1.4.4.2 pgoyette sc->sc_info.packetstate = state;
1577 1.4.4.2 pgoyette sc->sc_info.highestclass = highestclass;
1578 1.4.4.2 pgoyette sc->sc_info.uplink_speed = up_speed;
1579 1.4.4.2 pgoyette sc->sc_info.downlink_speed = down_speed;
1580 1.4.4.2 pgoyette
1581 1.4.4.2 pgoyette if (sc->sc_info.regmode == MBIM_REGMODE_AUTOMATIC) {
1582 1.4.4.2 pgoyette /*
1583 1.4.4.2 pgoyette * For devices using automatic registration mode, just proceed,
1584 1.4.4.2 pgoyette * once registration has completed.
1585 1.4.4.2 pgoyette */
1586 1.4.4.2 pgoyette if (ifp->if_flags & IFF_UP) {
1587 1.4.4.2 pgoyette switch (sc->sc_info.regstate) {
1588 1.4.4.2 pgoyette case MBIM_REGSTATE_HOME:
1589 1.4.4.2 pgoyette case MBIM_REGSTATE_ROAMING:
1590 1.4.4.2 pgoyette case MBIM_REGSTATE_PARTNER:
1591 1.4.4.2 pgoyette umb_newstate(sc, UMB_S_ATTACHED,
1592 1.4.4.2 pgoyette UMB_NS_DONT_DROP);
1593 1.4.4.2 pgoyette break;
1594 1.4.4.2 pgoyette default:
1595 1.4.4.2 pgoyette break;
1596 1.4.4.2 pgoyette }
1597 1.4.4.2 pgoyette } else
1598 1.4.4.2 pgoyette umb_newstate(sc, UMB_S_SIMREADY, UMB_NS_DONT_RAISE);
1599 1.4.4.2 pgoyette } else switch (sc->sc_info.packetstate) {
1600 1.4.4.2 pgoyette case MBIM_PKTSERVICE_STATE_ATTACHED:
1601 1.4.4.2 pgoyette umb_newstate(sc, UMB_S_ATTACHED, UMB_NS_DONT_DROP);
1602 1.4.4.2 pgoyette break;
1603 1.4.4.2 pgoyette case MBIM_PKTSERVICE_STATE_DETACHED:
1604 1.4.4.2 pgoyette umb_newstate(sc, UMB_S_SIMREADY, UMB_NS_DONT_RAISE);
1605 1.4.4.2 pgoyette break;
1606 1.4.4.2 pgoyette }
1607 1.4.4.2 pgoyette return 1;
1608 1.4.4.2 pgoyette }
1609 1.4.4.2 pgoyette
1610 1.4.4.2 pgoyette Static int
1611 1.4.4.2 pgoyette umb_decode_signal_state(struct umb_softc *sc, void *data, int len)
1612 1.4.4.2 pgoyette {
1613 1.4.4.2 pgoyette struct mbim_cid_signal_state *ss = data;
1614 1.4.4.2 pgoyette struct ifnet *ifp = GET_IFP(sc);
1615 1.4.4.2 pgoyette int rssi;
1616 1.4.4.2 pgoyette
1617 1.4.4.2 pgoyette if (len < sizeof(*ss))
1618 1.4.4.2 pgoyette return 0;
1619 1.4.4.2 pgoyette
1620 1.4.4.2 pgoyette if (le32toh(ss->rssi) == 99)
1621 1.4.4.2 pgoyette rssi = UMB_VALUE_UNKNOWN;
1622 1.4.4.2 pgoyette else {
1623 1.4.4.2 pgoyette rssi = -113 + 2 * le32toh(ss->rssi);
1624 1.4.4.2 pgoyette if ((ifp->if_flags & IFF_DEBUG) && sc->sc_info.rssi != rssi &&
1625 1.4.4.2 pgoyette sc->sc_state >= UMB_S_CONNECTED)
1626 1.4.4.2 pgoyette log(LOG_INFO, "%s: rssi %d dBm\n", DEVNAM(sc), rssi);
1627 1.4.4.2 pgoyette }
1628 1.4.4.2 pgoyette sc->sc_info.rssi = rssi;
1629 1.4.4.2 pgoyette sc->sc_info.ber = le32toh(ss->err_rate);
1630 1.4.4.2 pgoyette if (sc->sc_info.ber == -99)
1631 1.4.4.2 pgoyette sc->sc_info.ber = UMB_VALUE_UNKNOWN;
1632 1.4.4.2 pgoyette return 1;
1633 1.4.4.2 pgoyette }
1634 1.4.4.2 pgoyette
1635 1.4.4.2 pgoyette Static int
1636 1.4.4.2 pgoyette umb_decode_connect_info(struct umb_softc *sc, void *data, int len)
1637 1.4.4.2 pgoyette {
1638 1.4.4.2 pgoyette struct mbim_cid_connect_info *ci = data;
1639 1.4.4.2 pgoyette struct ifnet *ifp = GET_IFP(sc);
1640 1.4.4.2 pgoyette int act;
1641 1.4.4.2 pgoyette
1642 1.4.4.2 pgoyette if (len < sizeof(*ci))
1643 1.4.4.2 pgoyette return 0;
1644 1.4.4.2 pgoyette
1645 1.4.4.2 pgoyette if (le32toh(ci->sessionid) != umb_session_id) {
1646 1.4.4.2 pgoyette DPRINTF("%s: discard connection info for session %u\n",
1647 1.4.4.2 pgoyette DEVNAM(sc), le32toh(ci->sessionid));
1648 1.4.4.2 pgoyette return 1;
1649 1.4.4.2 pgoyette }
1650 1.4.4.2 pgoyette if (memcmp(ci->context, umb_uuid_context_internet,
1651 1.4.4.2 pgoyette sizeof(ci->context))) {
1652 1.4.4.2 pgoyette DPRINTF("%s: discard connection info for other context\n",
1653 1.4.4.2 pgoyette DEVNAM(sc));
1654 1.4.4.2 pgoyette return 1;
1655 1.4.4.2 pgoyette }
1656 1.4.4.2 pgoyette act = le32toh(ci->activation);
1657 1.4.4.2 pgoyette if (sc->sc_info.activation != act) {
1658 1.4.4.2 pgoyette if (ifp->if_flags & IFF_DEBUG)
1659 1.4.4.2 pgoyette log(LOG_INFO, "%s: connection %s\n", DEVNAM(sc),
1660 1.4.4.2 pgoyette umb_activation(act));
1661 1.4.4.2 pgoyette if ((ifp->if_flags & IFF_DEBUG) &&
1662 1.4.4.2 pgoyette le32toh(ci->iptype) != MBIM_CONTEXT_IPTYPE_DEFAULT &&
1663 1.4.4.2 pgoyette le32toh(ci->iptype) != MBIM_CONTEXT_IPTYPE_IPV4)
1664 1.4.4.2 pgoyette log(LOG_DEBUG, "%s: got iptype %d connection\n",
1665 1.4.4.2 pgoyette DEVNAM(sc), le32toh(ci->iptype));
1666 1.4.4.2 pgoyette
1667 1.4.4.2 pgoyette sc->sc_info.activation = act;
1668 1.4.4.2 pgoyette sc->sc_info.nwerror = le32toh(ci->nwerror);
1669 1.4.4.2 pgoyette
1670 1.4.4.2 pgoyette if (sc->sc_info.activation == MBIM_ACTIVATION_STATE_ACTIVATED)
1671 1.4.4.2 pgoyette umb_newstate(sc, UMB_S_CONNECTED, UMB_NS_DONT_DROP);
1672 1.4.4.2 pgoyette else if (sc->sc_info.activation ==
1673 1.4.4.2 pgoyette MBIM_ACTIVATION_STATE_DEACTIVATED)
1674 1.4.4.2 pgoyette umb_newstate(sc, UMB_S_ATTACHED, 0);
1675 1.4.4.2 pgoyette /* else: other states are purely transitional */
1676 1.4.4.2 pgoyette }
1677 1.4.4.2 pgoyette return 1;
1678 1.4.4.2 pgoyette }
1679 1.4.4.2 pgoyette
1680 1.4.4.2 pgoyette Static int
1681 1.4.4.2 pgoyette umb_decode_ip_configuration(struct umb_softc *sc, void *data, int len)
1682 1.4.4.2 pgoyette {
1683 1.4.4.2 pgoyette struct mbim_cid_ip_configuration_info *ic = data;
1684 1.4.4.2 pgoyette struct ifnet *ifp = GET_IFP(sc);
1685 1.4.4.2 pgoyette int s;
1686 1.4.4.2 pgoyette uint32_t avail;
1687 1.4.4.2 pgoyette uint32_t val;
1688 1.4.4.2 pgoyette int n, i;
1689 1.4.4.2 pgoyette int off;
1690 1.4.4.2 pgoyette struct mbim_cid_ipv4_element ipv4elem;
1691 1.4.4.2 pgoyette struct in_aliasreq ifra;
1692 1.4.4.2 pgoyette struct sockaddr_in *sin;
1693 1.4.4.2 pgoyette int state = -1;
1694 1.4.4.2 pgoyette int rv;
1695 1.4.4.2 pgoyette
1696 1.4.4.2 pgoyette if (len < sizeof(*ic))
1697 1.4.4.2 pgoyette return 0;
1698 1.4.4.2 pgoyette if (le32toh(ic->sessionid) != umb_session_id) {
1699 1.4.4.2 pgoyette DPRINTF("%s: ignore IP configuration for session id %d\n",
1700 1.4.4.2 pgoyette DEVNAM(sc), le32toh(ic->sessionid));
1701 1.4.4.2 pgoyette return 0;
1702 1.4.4.2 pgoyette }
1703 1.4.4.2 pgoyette s = splnet();
1704 1.4.4.2 pgoyette
1705 1.4.4.2 pgoyette /*
1706 1.4.4.2 pgoyette * IPv4 configuation
1707 1.4.4.2 pgoyette */
1708 1.4.4.2 pgoyette avail = le32toh(ic->ipv4_available);
1709 1.4.4.2 pgoyette if (avail & MBIM_IPCONF_HAS_ADDRINFO) {
1710 1.4.4.2 pgoyette n = le32toh(ic->ipv4_naddr);
1711 1.4.4.2 pgoyette off = le32toh(ic->ipv4_addroffs);
1712 1.4.4.2 pgoyette
1713 1.4.4.2 pgoyette if (n == 0 || off + sizeof(ipv4elem) > len)
1714 1.4.4.2 pgoyette goto done;
1715 1.4.4.2 pgoyette
1716 1.4.4.2 pgoyette /* Only pick the first one */
1717 1.4.4.2 pgoyette memcpy(&ipv4elem, (char *)data + off, sizeof(ipv4elem));
1718 1.4.4.2 pgoyette ipv4elem.prefixlen = le32toh(ipv4elem.prefixlen);
1719 1.4.4.2 pgoyette
1720 1.4.4.2 pgoyette memset(&ifra, 0, sizeof(ifra));
1721 1.4.4.2 pgoyette sin = (struct sockaddr_in *)&ifra.ifra_addr;
1722 1.4.4.2 pgoyette sin->sin_family = AF_INET;
1723 1.4.4.2 pgoyette sin->sin_len = sizeof(ifra.ifra_addr);
1724 1.4.4.2 pgoyette sin->sin_addr.s_addr = ipv4elem.addr;
1725 1.4.4.2 pgoyette
1726 1.4.4.2 pgoyette sin = (struct sockaddr_in *)&ifra.ifra_dstaddr;
1727 1.4.4.2 pgoyette sin->sin_family = AF_INET;
1728 1.4.4.2 pgoyette sin->sin_len = sizeof(ifra.ifra_dstaddr);
1729 1.4.4.2 pgoyette if (avail & MBIM_IPCONF_HAS_GWINFO) {
1730 1.4.4.2 pgoyette off = le32toh(ic->ipv4_gwoffs);
1731 1.4.4.2 pgoyette sin->sin_addr.s_addr = *((uint32_t *)((char *)data + off));
1732 1.4.4.2 pgoyette }
1733 1.4.4.2 pgoyette
1734 1.4.4.2 pgoyette sin = (struct sockaddr_in *)&ifra.ifra_mask;
1735 1.4.4.2 pgoyette sin->sin_family = AF_INET;
1736 1.4.4.2 pgoyette sin->sin_len = sizeof(ifra.ifra_mask);
1737 1.4.4.2 pgoyette umb_in_len2mask(&sin->sin_addr, ipv4elem.prefixlen);
1738 1.4.4.2 pgoyette
1739 1.4.4.2 pgoyette rv = in_control(NULL, SIOCAIFADDR, &ifra, ifp);
1740 1.4.4.2 pgoyette if (rv == 0) {
1741 1.4.4.2 pgoyette if (ifp->if_flags & IFF_DEBUG)
1742 1.4.4.2 pgoyette log(LOG_INFO, "%s: IPv4 addr %s, mask %s, "
1743 1.4.4.2 pgoyette "gateway %s\n", device_xname(sc->sc_dev),
1744 1.4.4.2 pgoyette umb_ntop(sintosa(&ifra.ifra_addr)),
1745 1.4.4.2 pgoyette umb_ntop(sintosa(&ifra.ifra_mask)),
1746 1.4.4.2 pgoyette umb_ntop(sintosa(&ifra.ifra_dstaddr)));
1747 1.4.4.2 pgoyette state = UMB_S_UP;
1748 1.4.4.2 pgoyette } else
1749 1.4.4.2 pgoyette printf("%s: unable to set IPv4 address, error %d\n",
1750 1.4.4.2 pgoyette device_xname(sc->sc_dev), rv);
1751 1.4.4.2 pgoyette }
1752 1.4.4.2 pgoyette
1753 1.4.4.2 pgoyette memset(sc->sc_info.ipv4dns, 0, sizeof(sc->sc_info.ipv4dns));
1754 1.4.4.2 pgoyette if (avail & MBIM_IPCONF_HAS_DNSINFO) {
1755 1.4.4.2 pgoyette n = le32toh(ic->ipv4_ndnssrv);
1756 1.4.4.2 pgoyette off = le32toh(ic->ipv4_dnssrvoffs);
1757 1.4.4.2 pgoyette i = 0;
1758 1.4.4.2 pgoyette while (n-- > 0) {
1759 1.4.4.2 pgoyette if (off + sizeof(uint32_t) > len)
1760 1.4.4.2 pgoyette break;
1761 1.4.4.2 pgoyette val = *((uint32_t *)((char *)data + off));
1762 1.4.4.2 pgoyette if (i < UMB_MAX_DNSSRV)
1763 1.4.4.2 pgoyette sc->sc_info.ipv4dns[i++] = val;
1764 1.4.4.2 pgoyette off += sizeof(uint32_t);
1765 1.4.4.2 pgoyette }
1766 1.4.4.2 pgoyette }
1767 1.4.4.2 pgoyette
1768 1.4.4.2 pgoyette if ((avail & MBIM_IPCONF_HAS_MTUINFO)) {
1769 1.4.4.2 pgoyette val = le32toh(ic->ipv4_mtu);
1770 1.4.4.2 pgoyette if (ifp->if_mtu != val && val <= sc->sc_maxpktlen) {
1771 1.4.4.2 pgoyette ifp->if_mtu = val;
1772 1.4.4.2 pgoyette if (ifp->if_mtu > val)
1773 1.4.4.2 pgoyette ifp->if_mtu = val;
1774 1.4.4.2 pgoyette if (ifp->if_flags & IFF_DEBUG)
1775 1.4.4.2 pgoyette log(LOG_INFO, "%s: MTU %d\n", DEVNAM(sc), val);
1776 1.4.4.2 pgoyette }
1777 1.4.4.2 pgoyette }
1778 1.4.4.2 pgoyette
1779 1.4.4.2 pgoyette avail = le32toh(ic->ipv6_available);
1780 1.4.4.2 pgoyette if ((ifp->if_flags & IFF_DEBUG) && avail & MBIM_IPCONF_HAS_ADDRINFO) {
1781 1.4.4.2 pgoyette /* XXX FIXME: IPv6 configuration missing */
1782 1.4.4.2 pgoyette log(LOG_INFO, "%s: ignoring IPv6 configuration\n", DEVNAM(sc));
1783 1.4.4.2 pgoyette }
1784 1.4.4.2 pgoyette if (state != -1)
1785 1.4.4.2 pgoyette umb_newstate(sc, state, 0);
1786 1.4.4.2 pgoyette
1787 1.4.4.2 pgoyette done:
1788 1.4.4.2 pgoyette splx(s);
1789 1.4.4.2 pgoyette return 1;
1790 1.4.4.2 pgoyette }
1791 1.4.4.2 pgoyette
1792 1.4.4.2 pgoyette Static void
1793 1.4.4.2 pgoyette umb_rx(struct umb_softc *sc)
1794 1.4.4.2 pgoyette {
1795 1.4.4.2 pgoyette usbd_setup_xfer(sc->sc_rx_xfer, sc, sc->sc_rx_buf,
1796 1.4.4.2 pgoyette sc->sc_rx_bufsz, USBD_SHORT_XFER_OK,
1797 1.4.4.2 pgoyette USBD_NO_TIMEOUT, umb_rxeof);
1798 1.4.4.2 pgoyette usbd_transfer(sc->sc_rx_xfer);
1799 1.4.4.2 pgoyette }
1800 1.4.4.2 pgoyette
1801 1.4.4.2 pgoyette Static void
1802 1.4.4.2 pgoyette umb_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
1803 1.4.4.2 pgoyette {
1804 1.4.4.2 pgoyette struct umb_softc *sc = priv;
1805 1.4.4.2 pgoyette struct ifnet *ifp = GET_IFP(sc);
1806 1.4.4.2 pgoyette
1807 1.4.4.2 pgoyette if (sc->sc_dying || !(ifp->if_flags & IFF_RUNNING))
1808 1.4.4.2 pgoyette return;
1809 1.4.4.2 pgoyette
1810 1.4.4.2 pgoyette if (status != USBD_NORMAL_COMPLETION) {
1811 1.4.4.2 pgoyette if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1812 1.4.4.2 pgoyette return;
1813 1.4.4.2 pgoyette DPRINTF("%s: rx error: %s\n", DEVNAM(sc), usbd_errstr(status));
1814 1.4.4.2 pgoyette if (status == USBD_STALLED)
1815 1.4.4.2 pgoyette usbd_clear_endpoint_stall_async(sc->sc_rx_pipe);
1816 1.4.4.2 pgoyette if (++sc->sc_rx_nerr > 100) {
1817 1.4.4.2 pgoyette log(LOG_ERR, "%s: too many rx errors, disabling\n",
1818 1.4.4.2 pgoyette DEVNAM(sc));
1819 1.4.4.2 pgoyette umb_activate(sc->sc_dev, DVACT_DEACTIVATE);
1820 1.4.4.2 pgoyette }
1821 1.4.4.2 pgoyette } else {
1822 1.4.4.2 pgoyette sc->sc_rx_nerr = 0;
1823 1.4.4.2 pgoyette umb_decap(sc, xfer);
1824 1.4.4.2 pgoyette }
1825 1.4.4.2 pgoyette
1826 1.4.4.2 pgoyette umb_rx(sc);
1827 1.4.4.2 pgoyette return;
1828 1.4.4.2 pgoyette }
1829 1.4.4.2 pgoyette
1830 1.4.4.2 pgoyette Static int
1831 1.4.4.2 pgoyette umb_encap(struct umb_softc *sc, struct mbuf *m)
1832 1.4.4.2 pgoyette {
1833 1.4.4.2 pgoyette struct ncm_header16 *hdr;
1834 1.4.4.2 pgoyette struct ncm_pointer16 *ptr;
1835 1.4.4.2 pgoyette usbd_status err;
1836 1.4.4.2 pgoyette int len;
1837 1.4.4.2 pgoyette
1838 1.4.4.2 pgoyette /* All size constraints have been validated by the caller! */
1839 1.4.4.2 pgoyette hdr = (struct ncm_header16 *)sc->sc_tx_buf;
1840 1.4.4.2 pgoyette ptr = (struct ncm_pointer16 *)(hdr + 1);
1841 1.4.4.2 pgoyette USETDW(hdr->dwSignature, NCM_HDR16_SIG);
1842 1.4.4.2 pgoyette USETW(hdr->wHeaderLength, sizeof(*hdr));
1843 1.4.4.2 pgoyette USETW(hdr->wSequence, sc->sc_tx_seq);
1844 1.4.4.2 pgoyette sc->sc_tx_seq++;
1845 1.4.4.2 pgoyette
1846 1.4.4.2 pgoyette len = m->m_pkthdr.len;
1847 1.4.4.2 pgoyette
1848 1.4.4.2 pgoyette USETDW(ptr->dwSignature, MBIM_NCM_NTH16_SIG(umb_session_id));
1849 1.4.4.2 pgoyette USETW(ptr->wLength, sizeof(*ptr));
1850 1.4.4.2 pgoyette USETW(ptr->wNextNdpIndex, 0);
1851 1.4.4.2 pgoyette USETW(ptr->dgram[0].wDatagramIndex, MBIM_HDR16_LEN);
1852 1.4.4.2 pgoyette USETW(ptr->dgram[0].wDatagramLen, len);
1853 1.4.4.2 pgoyette USETW(ptr->dgram[1].wDatagramIndex, 0);
1854 1.4.4.2 pgoyette USETW(ptr->dgram[1].wDatagramLen, 0);
1855 1.4.4.2 pgoyette
1856 1.4.4.2 pgoyette KASSERT(len <= sc->sc_tx_bufsz - sizeof(*hdr) - sizeof(*ptr));
1857 1.4.4.2 pgoyette m_copydata(m, 0, len, ptr + 1);
1858 1.4.4.2 pgoyette sc->sc_tx_m = m;
1859 1.4.4.2 pgoyette len += MBIM_HDR16_LEN;
1860 1.4.4.2 pgoyette USETW(hdr->wBlockLength, len);
1861 1.4.4.2 pgoyette
1862 1.4.4.2 pgoyette DPRINTFN(3, "%s: encap %d bytes\n", DEVNAM(sc), len);
1863 1.4.4.2 pgoyette DDUMPN(5, sc->sc_tx_buf, len);
1864 1.4.4.2 pgoyette usbd_setup_xfer(sc->sc_tx_xfer, sc, sc->sc_tx_buf, len,
1865 1.4.4.2 pgoyette USBD_FORCE_SHORT_XFER, umb_xfer_tout, umb_txeof);
1866 1.4.4.2 pgoyette err = usbd_transfer(sc->sc_tx_xfer);
1867 1.4.4.2 pgoyette if (err != USBD_IN_PROGRESS) {
1868 1.4.4.2 pgoyette DPRINTF("%s: start tx error: %s\n", DEVNAM(sc),
1869 1.4.4.2 pgoyette usbd_errstr(err));
1870 1.4.4.2 pgoyette return 0;
1871 1.4.4.2 pgoyette }
1872 1.4.4.2 pgoyette return 1;
1873 1.4.4.2 pgoyette }
1874 1.4.4.2 pgoyette
1875 1.4.4.2 pgoyette Static void
1876 1.4.4.2 pgoyette umb_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
1877 1.4.4.2 pgoyette {
1878 1.4.4.2 pgoyette struct umb_softc *sc = priv;
1879 1.4.4.2 pgoyette struct ifnet *ifp = GET_IFP(sc);
1880 1.4.4.2 pgoyette int s;
1881 1.4.4.2 pgoyette
1882 1.4.4.2 pgoyette s = splnet();
1883 1.4.4.2 pgoyette ifp->if_flags &= ~IFF_OACTIVE;
1884 1.4.4.2 pgoyette ifp->if_timer = 0;
1885 1.4.4.2 pgoyette
1886 1.4.4.2 pgoyette m_freem(sc->sc_tx_m);
1887 1.4.4.2 pgoyette sc->sc_tx_m = NULL;
1888 1.4.4.2 pgoyette
1889 1.4.4.2 pgoyette if (status != USBD_NORMAL_COMPLETION) {
1890 1.4.4.2 pgoyette if (status != USBD_NOT_STARTED && status != USBD_CANCELLED) {
1891 1.4.4.2 pgoyette ifp->if_oerrors++;
1892 1.4.4.2 pgoyette DPRINTF("%s: tx error: %s\n", DEVNAM(sc),
1893 1.4.4.2 pgoyette usbd_errstr(status));
1894 1.4.4.2 pgoyette if (status == USBD_STALLED)
1895 1.4.4.2 pgoyette usbd_clear_endpoint_stall_async(sc->sc_tx_pipe);
1896 1.4.4.2 pgoyette }
1897 1.4.4.2 pgoyette }
1898 1.4.4.2 pgoyette if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1899 1.4.4.2 pgoyette umb_start(ifp);
1900 1.4.4.2 pgoyette
1901 1.4.4.2 pgoyette splx(s);
1902 1.4.4.2 pgoyette }
1903 1.4.4.2 pgoyette
1904 1.4.4.2 pgoyette Static void
1905 1.4.4.2 pgoyette umb_decap(struct umb_softc *sc, struct usbd_xfer *xfer)
1906 1.4.4.2 pgoyette {
1907 1.4.4.2 pgoyette struct ifnet *ifp = GET_IFP(sc);
1908 1.4.4.2 pgoyette int s;
1909 1.4.4.2 pgoyette char *buf;
1910 1.4.4.2 pgoyette uint32_t len;
1911 1.4.4.2 pgoyette char *dp;
1912 1.4.4.2 pgoyette struct ncm_header16 *hdr16;
1913 1.4.4.2 pgoyette struct ncm_header32 *hdr32;
1914 1.4.4.2 pgoyette struct ncm_pointer16 *ptr16;
1915 1.4.4.2 pgoyette struct ncm_pointer16_dgram *dgram16;
1916 1.4.4.2 pgoyette struct ncm_pointer32_dgram *dgram32;
1917 1.4.4.2 pgoyette uint32_t hsig, psig;
1918 1.4.4.2 pgoyette int hlen, blen;
1919 1.4.4.2 pgoyette int ptrlen, ptroff, dgentryoff;
1920 1.4.4.2 pgoyette uint32_t doff, dlen;
1921 1.4.4.2 pgoyette struct mbuf *m;
1922 1.4.4.2 pgoyette
1923 1.4.4.2 pgoyette usbd_get_xfer_status(xfer, NULL, (void **)&buf, &len, NULL);
1924 1.4.4.2 pgoyette DPRINTFN(4, "%s: recv %d bytes\n", DEVNAM(sc), len);
1925 1.4.4.2 pgoyette DDUMPN(5, buf, len);
1926 1.4.4.2 pgoyette s = splnet();
1927 1.4.4.2 pgoyette if (len < sizeof(*hdr16))
1928 1.4.4.2 pgoyette goto toosmall;
1929 1.4.4.2 pgoyette
1930 1.4.4.2 pgoyette hdr16 = (struct ncm_header16 *)buf;
1931 1.4.4.2 pgoyette hsig = UGETDW(hdr16->dwSignature);
1932 1.4.4.2 pgoyette hlen = UGETW(hdr16->wHeaderLength);
1933 1.4.4.2 pgoyette if (len < hlen)
1934 1.4.4.2 pgoyette goto toosmall;
1935 1.4.4.2 pgoyette if (len > sc->sc_rx_bufsz) {
1936 1.4.4.2 pgoyette DPRINTF("%s: packet too large (%d)\n", DEVNAM(sc), len);
1937 1.4.4.2 pgoyette goto fail;
1938 1.4.4.2 pgoyette }
1939 1.4.4.2 pgoyette switch (hsig) {
1940 1.4.4.2 pgoyette case NCM_HDR16_SIG:
1941 1.4.4.2 pgoyette blen = UGETW(hdr16->wBlockLength);
1942 1.4.4.2 pgoyette ptroff = UGETW(hdr16->wNdpIndex);
1943 1.4.4.2 pgoyette if (hlen != sizeof(*hdr16)) {
1944 1.4.4.2 pgoyette DPRINTF("%s: bad header len %d for NTH16 (exp %zu)\n",
1945 1.4.4.2 pgoyette DEVNAM(sc), hlen, sizeof(*hdr16));
1946 1.4.4.2 pgoyette goto fail;
1947 1.4.4.2 pgoyette }
1948 1.4.4.2 pgoyette break;
1949 1.4.4.2 pgoyette case NCM_HDR32_SIG:
1950 1.4.4.2 pgoyette hdr32 = (struct ncm_header32 *)hdr16;
1951 1.4.4.2 pgoyette blen = UGETDW(hdr32->dwBlockLength);
1952 1.4.4.2 pgoyette ptroff = UGETDW(hdr32->dwNdpIndex);
1953 1.4.4.2 pgoyette if (hlen != sizeof(*hdr32)) {
1954 1.4.4.2 pgoyette DPRINTF("%s: bad header len %d for NTH32 (exp %zu)\n",
1955 1.4.4.2 pgoyette DEVNAM(sc), hlen, sizeof(*hdr32));
1956 1.4.4.2 pgoyette goto fail;
1957 1.4.4.2 pgoyette }
1958 1.4.4.2 pgoyette break;
1959 1.4.4.2 pgoyette default:
1960 1.4.4.2 pgoyette DPRINTF("%s: unsupported NCM header signature (0x%08x)\n",
1961 1.4.4.2 pgoyette DEVNAM(sc), hsig);
1962 1.4.4.2 pgoyette goto fail;
1963 1.4.4.2 pgoyette }
1964 1.4.4.2 pgoyette if (len < blen) {
1965 1.4.4.2 pgoyette DPRINTF("%s: bad NTB len (%d) for %d bytes of data\n",
1966 1.4.4.2 pgoyette DEVNAM(sc), blen, len);
1967 1.4.4.2 pgoyette goto fail;
1968 1.4.4.2 pgoyette }
1969 1.4.4.2 pgoyette
1970 1.4.4.2 pgoyette ptr16 = (struct ncm_pointer16 *)(buf + ptroff);
1971 1.4.4.2 pgoyette psig = UGETDW(ptr16->dwSignature);
1972 1.4.4.2 pgoyette ptrlen = UGETW(ptr16->wLength);
1973 1.4.4.2 pgoyette if (len < ptrlen + ptroff)
1974 1.4.4.2 pgoyette goto toosmall;
1975 1.4.4.2 pgoyette if (!MBIM_NCM_NTH16_ISISG(psig) && !MBIM_NCM_NTH32_ISISG(psig)) {
1976 1.4.4.2 pgoyette DPRINTF("%s: unsupported NCM pointer signature (0x%08x)\n",
1977 1.4.4.2 pgoyette DEVNAM(sc), psig);
1978 1.4.4.2 pgoyette goto fail;
1979 1.4.4.2 pgoyette }
1980 1.4.4.2 pgoyette
1981 1.4.4.2 pgoyette switch (hsig) {
1982 1.4.4.2 pgoyette case NCM_HDR16_SIG:
1983 1.4.4.2 pgoyette dgentryoff = offsetof(struct ncm_pointer16, dgram);
1984 1.4.4.2 pgoyette break;
1985 1.4.4.2 pgoyette case NCM_HDR32_SIG:
1986 1.4.4.2 pgoyette dgentryoff = offsetof(struct ncm_pointer32, dgram);
1987 1.4.4.2 pgoyette break;
1988 1.4.4.2 pgoyette default:
1989 1.4.4.2 pgoyette goto fail;
1990 1.4.4.2 pgoyette }
1991 1.4.4.2 pgoyette
1992 1.4.4.2 pgoyette while (dgentryoff < ptrlen) {
1993 1.4.4.2 pgoyette switch (hsig) {
1994 1.4.4.2 pgoyette case NCM_HDR16_SIG:
1995 1.4.4.2 pgoyette if (ptroff + dgentryoff < sizeof(*dgram16))
1996 1.4.4.2 pgoyette goto done;
1997 1.4.4.2 pgoyette dgram16 = (struct ncm_pointer16_dgram *)
1998 1.4.4.2 pgoyette (buf + ptroff + dgentryoff);
1999 1.4.4.2 pgoyette dgentryoff += sizeof(*dgram16);
2000 1.4.4.2 pgoyette dlen = UGETW(dgram16->wDatagramLen);
2001 1.4.4.2 pgoyette doff = UGETW(dgram16->wDatagramIndex);
2002 1.4.4.2 pgoyette break;
2003 1.4.4.2 pgoyette case NCM_HDR32_SIG:
2004 1.4.4.2 pgoyette if (ptroff + dgentryoff < sizeof(*dgram32))
2005 1.4.4.2 pgoyette goto done;
2006 1.4.4.2 pgoyette dgram32 = (struct ncm_pointer32_dgram *)
2007 1.4.4.2 pgoyette (buf + ptroff + dgentryoff);
2008 1.4.4.2 pgoyette dgentryoff += sizeof(*dgram32);
2009 1.4.4.2 pgoyette dlen = UGETDW(dgram32->dwDatagramLen);
2010 1.4.4.2 pgoyette doff = UGETDW(dgram32->dwDatagramIndex);
2011 1.4.4.2 pgoyette break;
2012 1.4.4.2 pgoyette default:
2013 1.4.4.2 pgoyette ifp->if_ierrors++;
2014 1.4.4.2 pgoyette goto done;
2015 1.4.4.2 pgoyette }
2016 1.4.4.2 pgoyette
2017 1.4.4.2 pgoyette /* Terminating zero entry */
2018 1.4.4.2 pgoyette if (dlen == 0 || doff == 0)
2019 1.4.4.2 pgoyette break;
2020 1.4.4.2 pgoyette if (len < dlen + doff) {
2021 1.4.4.2 pgoyette /* Skip giant datagram but continue processing */
2022 1.4.4.2 pgoyette DPRINTF("%s: datagram too large (%d @ off %d)\n",
2023 1.4.4.2 pgoyette DEVNAM(sc), dlen, doff);
2024 1.4.4.2 pgoyette continue;
2025 1.4.4.2 pgoyette }
2026 1.4.4.2 pgoyette
2027 1.4.4.2 pgoyette dp = buf + doff;
2028 1.4.4.2 pgoyette DPRINTFN(3, "%s: decap %d bytes\n", DEVNAM(sc), dlen);
2029 1.4.4.4 pgoyette m = m_devget(dp, dlen, 0, ifp);
2030 1.4.4.2 pgoyette if (m == NULL) {
2031 1.4.4.2 pgoyette ifp->if_iqdrops++;
2032 1.4.4.2 pgoyette continue;
2033 1.4.4.2 pgoyette }
2034 1.4.4.2 pgoyette
2035 1.4.4.2 pgoyette if_percpuq_enqueue((ifp)->if_percpuq, (m));
2036 1.4.4.2 pgoyette }
2037 1.4.4.2 pgoyette done:
2038 1.4.4.2 pgoyette splx(s);
2039 1.4.4.2 pgoyette return;
2040 1.4.4.2 pgoyette toosmall:
2041 1.4.4.2 pgoyette DPRINTF("%s: packet too small (%d)\n", DEVNAM(sc), len);
2042 1.4.4.2 pgoyette fail:
2043 1.4.4.2 pgoyette ifp->if_ierrors++;
2044 1.4.4.2 pgoyette splx(s);
2045 1.4.4.2 pgoyette }
2046 1.4.4.2 pgoyette
2047 1.4.4.2 pgoyette Static usbd_status
2048 1.4.4.2 pgoyette umb_send_encap_command(struct umb_softc *sc, void *data, int len)
2049 1.4.4.2 pgoyette {
2050 1.4.4.2 pgoyette struct usbd_xfer *xfer;
2051 1.4.4.2 pgoyette usb_device_request_t req;
2052 1.4.4.2 pgoyette char *buf;
2053 1.4.4.2 pgoyette
2054 1.4.4.2 pgoyette if (len > sc->sc_ctrl_len)
2055 1.4.4.2 pgoyette return USBD_INVAL;
2056 1.4.4.2 pgoyette
2057 1.4.4.2 pgoyette if (usbd_create_xfer(sc->sc_udev->ud_pipe0, len, 0, 0, &xfer) != 0)
2058 1.4.4.2 pgoyette return USBD_NOMEM;
2059 1.4.4.2 pgoyette buf = usbd_get_buffer(xfer);
2060 1.4.4.2 pgoyette memcpy(buf, data, len);
2061 1.4.4.2 pgoyette
2062 1.4.4.2 pgoyette /* XXX FIXME: if (total len > sc->sc_ctrl_len) => must fragment */
2063 1.4.4.2 pgoyette req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
2064 1.4.4.2 pgoyette req.bRequest = UCDC_SEND_ENCAPSULATED_COMMAND;
2065 1.4.4.2 pgoyette USETW(req.wValue, 0);
2066 1.4.4.2 pgoyette USETW(req.wIndex, sc->sc_ctrl_ifaceno);
2067 1.4.4.2 pgoyette USETW(req.wLength, len);
2068 1.4.4.2 pgoyette DELAY(umb_delay);
2069 1.4.4.2 pgoyette return usbd_request_async(sc->sc_udev, xfer, &req, NULL, NULL);
2070 1.4.4.2 pgoyette }
2071 1.4.4.2 pgoyette
2072 1.4.4.2 pgoyette Static int
2073 1.4.4.2 pgoyette umb_get_encap_response(struct umb_softc *sc, void *buf, int *len)
2074 1.4.4.2 pgoyette {
2075 1.4.4.2 pgoyette usb_device_request_t req;
2076 1.4.4.2 pgoyette usbd_status err;
2077 1.4.4.2 pgoyette
2078 1.4.4.2 pgoyette req.bmRequestType = UT_READ_CLASS_INTERFACE;
2079 1.4.4.2 pgoyette req.bRequest = UCDC_GET_ENCAPSULATED_RESPONSE;
2080 1.4.4.2 pgoyette USETW(req.wValue, 0);
2081 1.4.4.2 pgoyette USETW(req.wIndex, sc->sc_ctrl_ifaceno);
2082 1.4.4.2 pgoyette USETW(req.wLength, *len);
2083 1.4.4.2 pgoyette /* XXX FIXME: re-assemble fragments */
2084 1.4.4.2 pgoyette
2085 1.4.4.2 pgoyette DELAY(umb_delay);
2086 1.4.4.2 pgoyette err = usbd_do_request_flags(sc->sc_udev, &req, buf, USBD_SHORT_XFER_OK,
2087 1.4.4.2 pgoyette len, umb_xfer_tout);
2088 1.4.4.2 pgoyette if (err == USBD_NORMAL_COMPLETION)
2089 1.4.4.2 pgoyette return 1;
2090 1.4.4.2 pgoyette DPRINTF("%s: ctrl recv: %s\n", DEVNAM(sc), usbd_errstr(err));
2091 1.4.4.2 pgoyette return 0;
2092 1.4.4.2 pgoyette }
2093 1.4.4.2 pgoyette
2094 1.4.4.2 pgoyette Static void
2095 1.4.4.2 pgoyette umb_ctrl_msg(struct umb_softc *sc, uint32_t req, void *data, int len)
2096 1.4.4.2 pgoyette {
2097 1.4.4.2 pgoyette struct ifnet *ifp = GET_IFP(sc);
2098 1.4.4.2 pgoyette uint32_t tid;
2099 1.4.4.2 pgoyette struct mbim_msghdr *hdr = data;
2100 1.4.4.2 pgoyette usbd_status err;
2101 1.4.4.2 pgoyette int s;
2102 1.4.4.2 pgoyette
2103 1.4.4.2 pgoyette if (sc->sc_dying)
2104 1.4.4.2 pgoyette return;
2105 1.4.4.2 pgoyette if (len < sizeof(*hdr))
2106 1.4.4.2 pgoyette return;
2107 1.4.4.2 pgoyette tid = ++sc->sc_tid;
2108 1.4.4.2 pgoyette
2109 1.4.4.2 pgoyette hdr->type = htole32(req);
2110 1.4.4.2 pgoyette hdr->len = htole32(len);
2111 1.4.4.2 pgoyette hdr->tid = htole32(tid);
2112 1.4.4.2 pgoyette
2113 1.4.4.2 pgoyette #ifdef UMB_DEBUG
2114 1.4.4.2 pgoyette if (umb_debug) {
2115 1.4.4.2 pgoyette const char *op, *str;
2116 1.4.4.2 pgoyette if (req == MBIM_COMMAND_MSG) {
2117 1.4.4.2 pgoyette struct mbim_h2f_cmd *c = data;
2118 1.4.4.2 pgoyette if (le32toh(c->op) == MBIM_CMDOP_SET)
2119 1.4.4.2 pgoyette op = "set";
2120 1.4.4.2 pgoyette else
2121 1.4.4.2 pgoyette op = "qry";
2122 1.4.4.2 pgoyette str = umb_cid2str(le32toh(c->cid));
2123 1.4.4.2 pgoyette } else {
2124 1.4.4.2 pgoyette op = "snd";
2125 1.4.4.2 pgoyette str = umb_request2str(req);
2126 1.4.4.2 pgoyette }
2127 1.4.4.2 pgoyette DPRINTF("%s: -> %s %s (tid %u)\n", DEVNAM(sc), op, str, tid);
2128 1.4.4.2 pgoyette }
2129 1.4.4.2 pgoyette #endif
2130 1.4.4.2 pgoyette s = splusb();
2131 1.4.4.2 pgoyette err = umb_send_encap_command(sc, data, len);
2132 1.4.4.2 pgoyette splx(s);
2133 1.4.4.2 pgoyette if (err != USBD_NORMAL_COMPLETION) {
2134 1.4.4.2 pgoyette if (ifp->if_flags & IFF_DEBUG)
2135 1.4.4.2 pgoyette log(LOG_ERR, "%s: send %s msg (tid %u) failed: %s\n",
2136 1.4.4.2 pgoyette DEVNAM(sc), umb_request2str(req), tid,
2137 1.4.4.2 pgoyette usbd_errstr(err));
2138 1.4.4.2 pgoyette
2139 1.4.4.2 pgoyette /* will affect other transactions, too */
2140 1.4.4.2 pgoyette usbd_abort_pipe(sc->sc_udev->ud_pipe0);
2141 1.4.4.2 pgoyette } else {
2142 1.4.4.2 pgoyette DPRINTFN(2, "%s: sent %s (tid %u)\n", DEVNAM(sc),
2143 1.4.4.2 pgoyette umb_request2str(req), tid);
2144 1.4.4.2 pgoyette DDUMPN(3, data, len);
2145 1.4.4.2 pgoyette }
2146 1.4.4.2 pgoyette return;
2147 1.4.4.2 pgoyette }
2148 1.4.4.2 pgoyette
2149 1.4.4.2 pgoyette Static void
2150 1.4.4.2 pgoyette umb_open(struct umb_softc *sc)
2151 1.4.4.2 pgoyette {
2152 1.4.4.2 pgoyette struct mbim_h2f_openmsg msg;
2153 1.4.4.2 pgoyette
2154 1.4.4.2 pgoyette memset(&msg, 0, sizeof(msg));
2155 1.4.4.2 pgoyette msg.maxlen = htole32(sc->sc_ctrl_len);
2156 1.4.4.2 pgoyette umb_ctrl_msg(sc, MBIM_OPEN_MSG, &msg, sizeof(msg));
2157 1.4.4.2 pgoyette return;
2158 1.4.4.2 pgoyette }
2159 1.4.4.2 pgoyette
2160 1.4.4.2 pgoyette Static void
2161 1.4.4.2 pgoyette umb_close(struct umb_softc *sc)
2162 1.4.4.2 pgoyette {
2163 1.4.4.2 pgoyette struct mbim_h2f_closemsg msg;
2164 1.4.4.2 pgoyette
2165 1.4.4.2 pgoyette memset(&msg, 0, sizeof(msg));
2166 1.4.4.2 pgoyette umb_ctrl_msg(sc, MBIM_CLOSE_MSG, &msg, sizeof(msg));
2167 1.4.4.2 pgoyette }
2168 1.4.4.2 pgoyette
2169 1.4.4.2 pgoyette Static int
2170 1.4.4.2 pgoyette umb_setpin(struct umb_softc *sc, int op, int is_puk, void *pin, int pinlen,
2171 1.4.4.2 pgoyette void *newpin, int newpinlen)
2172 1.4.4.2 pgoyette {
2173 1.4.4.2 pgoyette struct mbim_cid_pin cp;
2174 1.4.4.2 pgoyette int off;
2175 1.4.4.2 pgoyette
2176 1.4.4.2 pgoyette if (pinlen == 0)
2177 1.4.4.2 pgoyette return 0;
2178 1.4.4.2 pgoyette if (pinlen < 0 || pinlen > MBIM_PIN_MAXLEN ||
2179 1.4.4.2 pgoyette newpinlen < 0 || newpinlen > MBIM_PIN_MAXLEN ||
2180 1.4.4.2 pgoyette op < 0 || op > MBIM_PIN_OP_CHANGE ||
2181 1.4.4.2 pgoyette (is_puk && op != MBIM_PIN_OP_ENTER))
2182 1.4.4.2 pgoyette return EINVAL;
2183 1.4.4.2 pgoyette
2184 1.4.4.2 pgoyette memset(&cp, 0, sizeof(cp));
2185 1.4.4.2 pgoyette cp.type = htole32(is_puk ? MBIM_PIN_TYPE_PUK1 : MBIM_PIN_TYPE_PIN1);
2186 1.4.4.2 pgoyette
2187 1.4.4.2 pgoyette off = offsetof(struct mbim_cid_pin, data);
2188 1.4.4.2 pgoyette if (!umb_addstr(&cp, sizeof(cp), &off, pin, pinlen,
2189 1.4.4.2 pgoyette &cp.pin_offs, &cp.pin_size))
2190 1.4.4.2 pgoyette return EINVAL;
2191 1.4.4.2 pgoyette
2192 1.4.4.2 pgoyette cp.op = htole32(op);
2193 1.4.4.2 pgoyette if (newpinlen) {
2194 1.4.4.2 pgoyette if (!umb_addstr(&cp, sizeof(cp), &off, newpin, newpinlen,
2195 1.4.4.2 pgoyette &cp.newpin_offs, &cp.newpin_size))
2196 1.4.4.2 pgoyette return EINVAL;
2197 1.4.4.2 pgoyette } else {
2198 1.4.4.2 pgoyette if ((op == MBIM_PIN_OP_CHANGE) || is_puk)
2199 1.4.4.2 pgoyette return EINVAL;
2200 1.4.4.2 pgoyette if (!umb_addstr(&cp, sizeof(cp), &off, NULL, 0,
2201 1.4.4.2 pgoyette &cp.newpin_offs, &cp.newpin_size))
2202 1.4.4.2 pgoyette return EINVAL;
2203 1.4.4.2 pgoyette }
2204 1.4.4.2 pgoyette umb_cmd(sc, MBIM_CID_PIN, MBIM_CMDOP_SET, &cp, off);
2205 1.4.4.2 pgoyette return 0;
2206 1.4.4.2 pgoyette }
2207 1.4.4.2 pgoyette
2208 1.4.4.2 pgoyette Static void
2209 1.4.4.2 pgoyette umb_setdataclass(struct umb_softc *sc)
2210 1.4.4.2 pgoyette {
2211 1.4.4.2 pgoyette struct mbim_cid_registration_state rs;
2212 1.4.4.2 pgoyette uint32_t classes;
2213 1.4.4.2 pgoyette
2214 1.4.4.2 pgoyette if (sc->sc_info.supportedclasses == MBIM_DATACLASS_NONE)
2215 1.4.4.2 pgoyette return;
2216 1.4.4.2 pgoyette
2217 1.4.4.2 pgoyette memset(&rs, 0, sizeof(rs));
2218 1.4.4.2 pgoyette rs.regaction = htole32(MBIM_REGACTION_AUTOMATIC);
2219 1.4.4.2 pgoyette classes = sc->sc_info.supportedclasses;
2220 1.4.4.2 pgoyette if (sc->sc_info.preferredclasses != MBIM_DATACLASS_NONE)
2221 1.4.4.2 pgoyette classes &= sc->sc_info.preferredclasses;
2222 1.4.4.2 pgoyette rs.data_class = htole32(classes);
2223 1.4.4.2 pgoyette umb_cmd(sc, MBIM_CID_REGISTER_STATE, MBIM_CMDOP_SET, &rs, sizeof(rs));
2224 1.4.4.2 pgoyette }
2225 1.4.4.2 pgoyette
2226 1.4.4.2 pgoyette Static void
2227 1.4.4.2 pgoyette umb_radio(struct umb_softc *sc, int on)
2228 1.4.4.2 pgoyette {
2229 1.4.4.2 pgoyette struct mbim_cid_radio_state s;
2230 1.4.4.2 pgoyette
2231 1.4.4.2 pgoyette DPRINTF("%s: set radio %s\n", DEVNAM(sc), on ? "on" : "off");
2232 1.4.4.2 pgoyette memset(&s, 0, sizeof(s));
2233 1.4.4.2 pgoyette s.state = htole32(on ? MBIM_RADIO_STATE_ON : MBIM_RADIO_STATE_OFF);
2234 1.4.4.2 pgoyette umb_cmd(sc, MBIM_CID_RADIO_STATE, MBIM_CMDOP_SET, &s, sizeof(s));
2235 1.4.4.2 pgoyette }
2236 1.4.4.2 pgoyette
2237 1.4.4.2 pgoyette Static void
2238 1.4.4.2 pgoyette umb_allocate_cid(struct umb_softc *sc)
2239 1.4.4.2 pgoyette {
2240 1.4.4.2 pgoyette umb_cmd1(sc, MBIM_CID_DEVICE_CAPS, MBIM_CMDOP_SET,
2241 1.4.4.2 pgoyette umb_qmi_alloc_cid, sizeof(umb_qmi_alloc_cid), umb_uuid_qmi_mbim);
2242 1.4.4.2 pgoyette }
2243 1.4.4.2 pgoyette
2244 1.4.4.2 pgoyette Static void
2245 1.4.4.2 pgoyette umb_send_fcc_auth(struct umb_softc *sc)
2246 1.4.4.2 pgoyette {
2247 1.4.4.2 pgoyette uint8_t fccauth[sizeof(umb_qmi_fcc_auth)];
2248 1.4.4.2 pgoyette
2249 1.4.4.2 pgoyette if (sc->sc_cid == -1) {
2250 1.4.4.2 pgoyette DPRINTF("%s: missing CID, cannot send FCC auth\n", DEVNAM(sc));
2251 1.4.4.2 pgoyette umb_allocate_cid(sc);
2252 1.4.4.2 pgoyette return;
2253 1.4.4.2 pgoyette }
2254 1.4.4.2 pgoyette memcpy(fccauth, umb_qmi_fcc_auth, sizeof(fccauth));
2255 1.4.4.2 pgoyette fccauth[UMB_QMI_CID_OFFS] = sc->sc_cid;
2256 1.4.4.2 pgoyette umb_cmd1(sc, MBIM_CID_DEVICE_CAPS, MBIM_CMDOP_SET,
2257 1.4.4.2 pgoyette fccauth, sizeof(fccauth), umb_uuid_qmi_mbim);
2258 1.4.4.2 pgoyette }
2259 1.4.4.2 pgoyette
2260 1.4.4.2 pgoyette Static void
2261 1.4.4.2 pgoyette umb_packet_service(struct umb_softc *sc, int attach)
2262 1.4.4.2 pgoyette {
2263 1.4.4.2 pgoyette struct mbim_cid_packet_service s;
2264 1.4.4.2 pgoyette
2265 1.4.4.2 pgoyette DPRINTF("%s: %s packet service\n", DEVNAM(sc),
2266 1.4.4.2 pgoyette attach ? "attach" : "detach");
2267 1.4.4.2 pgoyette memset(&s, 0, sizeof(s));
2268 1.4.4.2 pgoyette s.action = htole32(attach ?
2269 1.4.4.2 pgoyette MBIM_PKTSERVICE_ACTION_ATTACH : MBIM_PKTSERVICE_ACTION_DETACH);
2270 1.4.4.2 pgoyette umb_cmd(sc, MBIM_CID_PACKET_SERVICE, MBIM_CMDOP_SET, &s, sizeof(s));
2271 1.4.4.2 pgoyette }
2272 1.4.4.2 pgoyette
2273 1.4.4.2 pgoyette Static void
2274 1.4.4.2 pgoyette umb_connect(struct umb_softc *sc)
2275 1.4.4.2 pgoyette {
2276 1.4.4.2 pgoyette struct ifnet *ifp = GET_IFP(sc);
2277 1.4.4.2 pgoyette
2278 1.4.4.2 pgoyette if (sc->sc_info.regstate == MBIM_REGSTATE_ROAMING && !sc->sc_roaming) {
2279 1.4.4.2 pgoyette log(LOG_INFO, "%s: connection disabled in roaming network\n",
2280 1.4.4.2 pgoyette DEVNAM(sc));
2281 1.4.4.2 pgoyette return;
2282 1.4.4.2 pgoyette }
2283 1.4.4.2 pgoyette if (ifp->if_flags & IFF_DEBUG)
2284 1.4.4.2 pgoyette log(LOG_DEBUG, "%s: connecting ...\n", DEVNAM(sc));
2285 1.4.4.2 pgoyette umb_send_connect(sc, MBIM_CONNECT_ACTIVATE);
2286 1.4.4.2 pgoyette }
2287 1.4.4.2 pgoyette
2288 1.4.4.2 pgoyette Static void
2289 1.4.4.2 pgoyette umb_disconnect(struct umb_softc *sc)
2290 1.4.4.2 pgoyette {
2291 1.4.4.2 pgoyette struct ifnet *ifp = GET_IFP(sc);
2292 1.4.4.2 pgoyette
2293 1.4.4.2 pgoyette if (ifp->if_flags & IFF_DEBUG)
2294 1.4.4.2 pgoyette log(LOG_DEBUG, "%s: disconnecting ...\n", DEVNAM(sc));
2295 1.4.4.2 pgoyette umb_send_connect(sc, MBIM_CONNECT_DEACTIVATE);
2296 1.4.4.2 pgoyette }
2297 1.4.4.2 pgoyette
2298 1.4.4.2 pgoyette Static void
2299 1.4.4.2 pgoyette umb_send_connect(struct umb_softc *sc, int command)
2300 1.4.4.2 pgoyette {
2301 1.4.4.2 pgoyette struct mbim_cid_connect *c;
2302 1.4.4.2 pgoyette int off;
2303 1.4.4.2 pgoyette
2304 1.4.4.2 pgoyette /* Too large or the stack */
2305 1.4.4.2 pgoyette c = kmem_zalloc(sizeof(*c), KM_SLEEP);
2306 1.4.4.2 pgoyette c->sessionid = htole32(umb_session_id);
2307 1.4.4.2 pgoyette c->command = htole32(command);
2308 1.4.4.2 pgoyette off = offsetof(struct mbim_cid_connect, data);
2309 1.4.4.2 pgoyette if (!umb_addstr(c, sizeof(*c), &off, sc->sc_info.apn,
2310 1.4.4.2 pgoyette sc->sc_info.apnlen, &c->access_offs, &c->access_size))
2311 1.4.4.2 pgoyette goto done;
2312 1.4.4.2 pgoyette if (!umb_addstr(c, sizeof(*c), &off, sc->sc_info.username,
2313 1.4.4.2 pgoyette sc->sc_info.usernamelen, &c->user_offs, &c->user_size))
2314 1.4.4.2 pgoyette goto done;
2315 1.4.4.2 pgoyette if (!umb_addstr(c, sizeof(*c), &off, sc->sc_info.password,
2316 1.4.4.2 pgoyette sc->sc_info.passwordlen, &c->passwd_offs, &c->passwd_size))
2317 1.4.4.2 pgoyette goto done;
2318 1.4.4.2 pgoyette c->authprot = htole32(MBIM_AUTHPROT_NONE);
2319 1.4.4.2 pgoyette c->compression = htole32(MBIM_COMPRESSION_NONE);
2320 1.4.4.2 pgoyette c->iptype = htole32(MBIM_CONTEXT_IPTYPE_IPV4);
2321 1.4.4.2 pgoyette memcpy(c->context, umb_uuid_context_internet, sizeof(c->context));
2322 1.4.4.2 pgoyette umb_cmd(sc, MBIM_CID_CONNECT, MBIM_CMDOP_SET, c, off);
2323 1.4.4.2 pgoyette done:
2324 1.4.4.2 pgoyette kmem_free(c, sizeof(*c));
2325 1.4.4.2 pgoyette return;
2326 1.4.4.2 pgoyette }
2327 1.4.4.2 pgoyette
2328 1.4.4.2 pgoyette Static void
2329 1.4.4.2 pgoyette umb_qry_ipconfig(struct umb_softc *sc)
2330 1.4.4.2 pgoyette {
2331 1.4.4.2 pgoyette struct mbim_cid_ip_configuration_info ipc;
2332 1.4.4.2 pgoyette
2333 1.4.4.2 pgoyette memset(&ipc, 0, sizeof(ipc));
2334 1.4.4.2 pgoyette ipc.sessionid = htole32(umb_session_id);
2335 1.4.4.2 pgoyette umb_cmd(sc, MBIM_CID_IP_CONFIGURATION, MBIM_CMDOP_QRY,
2336 1.4.4.2 pgoyette &ipc, sizeof(ipc));
2337 1.4.4.2 pgoyette }
2338 1.4.4.2 pgoyette
2339 1.4.4.2 pgoyette Static void
2340 1.4.4.2 pgoyette umb_cmd(struct umb_softc *sc, int cid, int op, const void *data, int len)
2341 1.4.4.2 pgoyette {
2342 1.4.4.2 pgoyette umb_cmd1(sc, cid, op, data, len, umb_uuid_basic_connect);
2343 1.4.4.2 pgoyette }
2344 1.4.4.2 pgoyette
2345 1.4.4.2 pgoyette Static void
2346 1.4.4.2 pgoyette umb_cmd1(struct umb_softc *sc, int cid, int op, const void *data, int len,
2347 1.4.4.2 pgoyette uint8_t *uuid)
2348 1.4.4.2 pgoyette {
2349 1.4.4.2 pgoyette struct mbim_h2f_cmd *cmd;
2350 1.4.4.2 pgoyette int totlen;
2351 1.4.4.2 pgoyette
2352 1.4.4.2 pgoyette /* XXX FIXME support sending fragments */
2353 1.4.4.2 pgoyette if (sizeof(*cmd) + len > sc->sc_ctrl_len) {
2354 1.4.4.2 pgoyette DPRINTF("%s: set %s msg too long: cannot send\n",
2355 1.4.4.2 pgoyette DEVNAM(sc), umb_cid2str(cid));
2356 1.4.4.2 pgoyette return;
2357 1.4.4.2 pgoyette }
2358 1.4.4.2 pgoyette cmd = sc->sc_ctrl_msg;
2359 1.4.4.2 pgoyette memset(cmd, 0, sizeof(*cmd));
2360 1.4.4.2 pgoyette cmd->frag.nfrag = htole32(1);
2361 1.4.4.2 pgoyette memcpy(cmd->devid, uuid, sizeof(cmd->devid));
2362 1.4.4.2 pgoyette cmd->cid = htole32(cid);
2363 1.4.4.2 pgoyette cmd->op = htole32(op);
2364 1.4.4.2 pgoyette cmd->infolen = htole32(len);
2365 1.4.4.2 pgoyette totlen = sizeof(*cmd);
2366 1.4.4.2 pgoyette if (len > 0) {
2367 1.4.4.2 pgoyette memcpy(cmd + 1, data, len);
2368 1.4.4.2 pgoyette totlen += len;
2369 1.4.4.2 pgoyette }
2370 1.4.4.2 pgoyette umb_ctrl_msg(sc, MBIM_COMMAND_MSG, cmd, totlen);
2371 1.4.4.2 pgoyette }
2372 1.4.4.2 pgoyette
2373 1.4.4.2 pgoyette Static void
2374 1.4.4.2 pgoyette umb_command_done(struct umb_softc *sc, void *data, int len)
2375 1.4.4.2 pgoyette {
2376 1.4.4.2 pgoyette struct mbim_f2h_cmddone *cmd = data;
2377 1.4.4.2 pgoyette struct ifnet *ifp = GET_IFP(sc);
2378 1.4.4.2 pgoyette uint32_t status;
2379 1.4.4.2 pgoyette uint32_t cid;
2380 1.4.4.2 pgoyette uint32_t infolen;
2381 1.4.4.2 pgoyette int qmimsg = 0;
2382 1.4.4.2 pgoyette
2383 1.4.4.2 pgoyette if (len < sizeof(*cmd)) {
2384 1.4.4.2 pgoyette DPRINTF("%s: discard short %s messsage\n", DEVNAM(sc),
2385 1.4.4.2 pgoyette umb_request2str(le32toh(cmd->hdr.type)));
2386 1.4.4.2 pgoyette return;
2387 1.4.4.2 pgoyette }
2388 1.4.4.2 pgoyette cid = le32toh(cmd->cid);
2389 1.4.4.2 pgoyette if (memcmp(cmd->devid, umb_uuid_basic_connect, sizeof(cmd->devid))) {
2390 1.4.4.2 pgoyette if (memcmp(cmd->devid, umb_uuid_qmi_mbim,
2391 1.4.4.2 pgoyette sizeof(cmd->devid))) {
2392 1.4.4.2 pgoyette DPRINTF("%s: discard %s messsage for other UUID '%s'\n",
2393 1.4.4.2 pgoyette DEVNAM(sc), umb_request2str(le32toh(cmd->hdr.type)),
2394 1.4.4.2 pgoyette umb_uuid2str(cmd->devid));
2395 1.4.4.2 pgoyette return;
2396 1.4.4.2 pgoyette } else
2397 1.4.4.2 pgoyette qmimsg = 1;
2398 1.4.4.2 pgoyette }
2399 1.4.4.2 pgoyette
2400 1.4.4.2 pgoyette status = le32toh(cmd->status);
2401 1.4.4.2 pgoyette switch (status) {
2402 1.4.4.2 pgoyette case MBIM_STATUS_SUCCESS:
2403 1.4.4.2 pgoyette break;
2404 1.4.4.2 pgoyette case MBIM_STATUS_NOT_INITIALIZED:
2405 1.4.4.2 pgoyette if (ifp->if_flags & IFF_DEBUG)
2406 1.4.4.2 pgoyette log(LOG_ERR, "%s: SIM not initialized (PIN missing)\n",
2407 1.4.4.2 pgoyette DEVNAM(sc));
2408 1.4.4.2 pgoyette return;
2409 1.4.4.2 pgoyette case MBIM_STATUS_PIN_REQUIRED:
2410 1.4.4.2 pgoyette sc->sc_info.pin_state = UMB_PIN_REQUIRED;
2411 1.4.4.2 pgoyette /*FALLTHROUGH*/
2412 1.4.4.2 pgoyette default:
2413 1.4.4.2 pgoyette if (ifp->if_flags & IFF_DEBUG)
2414 1.4.4.2 pgoyette log(LOG_ERR, "%s: set/qry %s failed: %s\n", DEVNAM(sc),
2415 1.4.4.2 pgoyette umb_cid2str(cid), umb_status2str(status));
2416 1.4.4.2 pgoyette return;
2417 1.4.4.2 pgoyette }
2418 1.4.4.2 pgoyette
2419 1.4.4.2 pgoyette infolen = le32toh(cmd->infolen);
2420 1.4.4.2 pgoyette if (len < sizeof(*cmd) + infolen) {
2421 1.4.4.2 pgoyette DPRINTF("%s: discard truncated %s messsage (want %d, got %d)\n",
2422 1.4.4.2 pgoyette DEVNAM(sc), umb_cid2str(cid),
2423 1.4.4.2 pgoyette (int)sizeof(*cmd) + infolen, len);
2424 1.4.4.2 pgoyette return;
2425 1.4.4.2 pgoyette }
2426 1.4.4.2 pgoyette if (qmimsg) {
2427 1.4.4.2 pgoyette if (sc->sc_flags & UMBFLG_FCC_AUTH_REQUIRED)
2428 1.4.4.2 pgoyette umb_decode_qmi(sc, cmd->info, infolen);
2429 1.4.4.2 pgoyette } else {
2430 1.4.4.2 pgoyette DPRINTFN(2, "%s: set/qry %s done\n", DEVNAM(sc),
2431 1.4.4.2 pgoyette umb_cid2str(cid));
2432 1.4.4.2 pgoyette umb_decode_cid(sc, cid, cmd->info, infolen);
2433 1.4.4.2 pgoyette }
2434 1.4.4.2 pgoyette }
2435 1.4.4.2 pgoyette
2436 1.4.4.2 pgoyette Static void
2437 1.4.4.2 pgoyette umb_decode_cid(struct umb_softc *sc, uint32_t cid, void *data, int len)
2438 1.4.4.2 pgoyette {
2439 1.4.4.2 pgoyette int ok = 1;
2440 1.4.4.2 pgoyette
2441 1.4.4.2 pgoyette switch (cid) {
2442 1.4.4.2 pgoyette case MBIM_CID_DEVICE_CAPS:
2443 1.4.4.2 pgoyette ok = umb_decode_devices_caps(sc, data, len);
2444 1.4.4.2 pgoyette break;
2445 1.4.4.2 pgoyette case MBIM_CID_SUBSCRIBER_READY_STATUS:
2446 1.4.4.2 pgoyette ok = umb_decode_subscriber_status(sc, data, len);
2447 1.4.4.2 pgoyette break;
2448 1.4.4.2 pgoyette case MBIM_CID_RADIO_STATE:
2449 1.4.4.2 pgoyette ok = umb_decode_radio_state(sc, data, len);
2450 1.4.4.2 pgoyette break;
2451 1.4.4.2 pgoyette case MBIM_CID_PIN:
2452 1.4.4.2 pgoyette ok = umb_decode_pin(sc, data, len);
2453 1.4.4.2 pgoyette break;
2454 1.4.4.2 pgoyette case MBIM_CID_REGISTER_STATE:
2455 1.4.4.2 pgoyette ok = umb_decode_register_state(sc, data, len);
2456 1.4.4.2 pgoyette break;
2457 1.4.4.2 pgoyette case MBIM_CID_PACKET_SERVICE:
2458 1.4.4.2 pgoyette ok = umb_decode_packet_service(sc, data, len);
2459 1.4.4.2 pgoyette break;
2460 1.4.4.2 pgoyette case MBIM_CID_SIGNAL_STATE:
2461 1.4.4.2 pgoyette ok = umb_decode_signal_state(sc, data, len);
2462 1.4.4.2 pgoyette break;
2463 1.4.4.2 pgoyette case MBIM_CID_CONNECT:
2464 1.4.4.2 pgoyette ok = umb_decode_connect_info(sc, data, len);
2465 1.4.4.2 pgoyette break;
2466 1.4.4.2 pgoyette case MBIM_CID_IP_CONFIGURATION:
2467 1.4.4.2 pgoyette ok = umb_decode_ip_configuration(sc, data, len);
2468 1.4.4.2 pgoyette break;
2469 1.4.4.2 pgoyette default:
2470 1.4.4.2 pgoyette /*
2471 1.4.4.2 pgoyette * Note: the above list is incomplete and only contains
2472 1.4.4.2 pgoyette * mandatory CIDs from the BASIC_CONNECT set.
2473 1.4.4.2 pgoyette * So alternate values are not unusual.
2474 1.4.4.2 pgoyette */
2475 1.4.4.2 pgoyette DPRINTFN(4, "%s: ignore %s\n", DEVNAM(sc), umb_cid2str(cid));
2476 1.4.4.2 pgoyette break;
2477 1.4.4.2 pgoyette }
2478 1.4.4.2 pgoyette if (!ok)
2479 1.4.4.2 pgoyette DPRINTF("%s: discard %s with bad info length %d\n",
2480 1.4.4.2 pgoyette DEVNAM(sc), umb_cid2str(cid), len);
2481 1.4.4.2 pgoyette return;
2482 1.4.4.2 pgoyette }
2483 1.4.4.2 pgoyette
2484 1.4.4.2 pgoyette Static void
2485 1.4.4.2 pgoyette umb_decode_qmi(struct umb_softc *sc, uint8_t *data, int len)
2486 1.4.4.2 pgoyette {
2487 1.4.4.2 pgoyette uint8_t srv;
2488 1.4.4.2 pgoyette uint16_t msg, tlvlen;
2489 1.4.4.2 pgoyette uint32_t val;
2490 1.4.4.2 pgoyette
2491 1.4.4.2 pgoyette #define UMB_QMI_QMUXLEN 6
2492 1.4.4.2 pgoyette if (len < UMB_QMI_QMUXLEN)
2493 1.4.4.2 pgoyette goto tooshort;
2494 1.4.4.2 pgoyette
2495 1.4.4.2 pgoyette srv = data[4];
2496 1.4.4.2 pgoyette data += UMB_QMI_QMUXLEN;
2497 1.4.4.2 pgoyette len -= UMB_QMI_QMUXLEN;
2498 1.4.4.2 pgoyette
2499 1.4.4.2 pgoyette #define UMB_GET16(p) ((uint16_t)*p | (uint16_t)*(p + 1) << 8)
2500 1.4.4.2 pgoyette #define UMB_GET32(p) ((uint32_t)*p | (uint32_t)*(p + 1) << 8 | \
2501 1.4.4.2 pgoyette (uint32_t)*(p + 2) << 16 |(uint32_t)*(p + 3) << 24)
2502 1.4.4.2 pgoyette switch (srv) {
2503 1.4.4.2 pgoyette case 0: /* ctl */
2504 1.4.4.2 pgoyette #define UMB_QMI_CTLLEN 6
2505 1.4.4.2 pgoyette if (len < UMB_QMI_CTLLEN)
2506 1.4.4.2 pgoyette goto tooshort;
2507 1.4.4.2 pgoyette msg = UMB_GET16(&data[2]);
2508 1.4.4.2 pgoyette tlvlen = UMB_GET16(&data[4]);
2509 1.4.4.2 pgoyette data += UMB_QMI_CTLLEN;
2510 1.4.4.2 pgoyette len -= UMB_QMI_CTLLEN;
2511 1.4.4.2 pgoyette break;
2512 1.4.4.2 pgoyette case 2: /* dms */
2513 1.4.4.2 pgoyette #define UMB_QMI_DMSLEN 7
2514 1.4.4.2 pgoyette if (len < UMB_QMI_DMSLEN)
2515 1.4.4.2 pgoyette goto tooshort;
2516 1.4.4.2 pgoyette msg = UMB_GET16(&data[3]);
2517 1.4.4.2 pgoyette tlvlen = UMB_GET16(&data[5]);
2518 1.4.4.2 pgoyette data += UMB_QMI_DMSLEN;
2519 1.4.4.2 pgoyette len -= UMB_QMI_DMSLEN;
2520 1.4.4.2 pgoyette break;
2521 1.4.4.2 pgoyette default:
2522 1.4.4.2 pgoyette DPRINTF("%s: discard QMI message for unknown service type %d\n",
2523 1.4.4.2 pgoyette DEVNAM(sc), srv);
2524 1.4.4.2 pgoyette return;
2525 1.4.4.2 pgoyette }
2526 1.4.4.2 pgoyette
2527 1.4.4.2 pgoyette if (len < tlvlen)
2528 1.4.4.2 pgoyette goto tooshort;
2529 1.4.4.2 pgoyette
2530 1.4.4.2 pgoyette #define UMB_QMI_TLVLEN 3
2531 1.4.4.2 pgoyette while (len > 0) {
2532 1.4.4.2 pgoyette if (len < UMB_QMI_TLVLEN)
2533 1.4.4.2 pgoyette goto tooshort;
2534 1.4.4.2 pgoyette tlvlen = UMB_GET16(&data[1]);
2535 1.4.4.2 pgoyette if (len < UMB_QMI_TLVLEN + tlvlen)
2536 1.4.4.2 pgoyette goto tooshort;
2537 1.4.4.2 pgoyette switch (data[0]) {
2538 1.4.4.2 pgoyette case 1: /* allocation info */
2539 1.4.4.2 pgoyette if (msg == 0x0022) { /* Allocate CID */
2540 1.4.4.2 pgoyette if (tlvlen != 2 || data[3] != 2) /* dms */
2541 1.4.4.2 pgoyette break;
2542 1.4.4.2 pgoyette sc->sc_cid = data[4];
2543 1.4.4.2 pgoyette DPRINTF("%s: QMI CID %d allocated\n",
2544 1.4.4.2 pgoyette DEVNAM(sc), sc->sc_cid);
2545 1.4.4.2 pgoyette umb_newstate(sc, UMB_S_CID, UMB_NS_DONT_DROP);
2546 1.4.4.2 pgoyette }
2547 1.4.4.2 pgoyette break;
2548 1.4.4.2 pgoyette case 2: /* response */
2549 1.4.4.2 pgoyette if (tlvlen != sizeof(val))
2550 1.4.4.2 pgoyette break;
2551 1.4.4.2 pgoyette val = UMB_GET32(&data[3]);
2552 1.4.4.2 pgoyette switch (msg) {
2553 1.4.4.2 pgoyette case 0x0022: /* Allocate CID */
2554 1.4.4.2 pgoyette if (val != 0) {
2555 1.4.4.2 pgoyette log(LOG_ERR, "%s: allocation of QMI CID"
2556 1.4.4.2 pgoyette " failed, error 0x%x\n", DEVNAM(sc),
2557 1.4.4.2 pgoyette val);
2558 1.4.4.2 pgoyette /* XXX how to proceed? */
2559 1.4.4.2 pgoyette return;
2560 1.4.4.2 pgoyette }
2561 1.4.4.2 pgoyette break;
2562 1.4.4.2 pgoyette case 0x555f: /* Send FCC Authentication */
2563 1.4.4.2 pgoyette if (val == 0)
2564 1.4.4.2 pgoyette log(LOG_INFO, "%s: send FCC "
2565 1.4.4.2 pgoyette "Authentication succeeded\n",
2566 1.4.4.2 pgoyette DEVNAM(sc));
2567 1.4.4.2 pgoyette else if (val == 0x001a0001)
2568 1.4.4.2 pgoyette log(LOG_INFO, "%s: FCC Authentication "
2569 1.4.4.2 pgoyette "not required\n", DEVNAM(sc));
2570 1.4.4.2 pgoyette else
2571 1.4.4.2 pgoyette log(LOG_INFO, "%s: send FCC "
2572 1.4.4.2 pgoyette "Authentication failed, "
2573 1.4.4.2 pgoyette "error 0x%x\n", DEVNAM(sc), val);
2574 1.4.4.2 pgoyette
2575 1.4.4.2 pgoyette /* FCC Auth is needed only once after power-on*/
2576 1.4.4.2 pgoyette sc->sc_flags &= ~UMBFLG_FCC_AUTH_REQUIRED;
2577 1.4.4.2 pgoyette
2578 1.4.4.2 pgoyette /* Try to proceed anyway */
2579 1.4.4.2 pgoyette DPRINTF("%s: init: turning radio on ...\n",
2580 1.4.4.2 pgoyette DEVNAM(sc));
2581 1.4.4.2 pgoyette umb_radio(sc, 1);
2582 1.4.4.2 pgoyette break;
2583 1.4.4.2 pgoyette default:
2584 1.4.4.2 pgoyette break;
2585 1.4.4.2 pgoyette }
2586 1.4.4.2 pgoyette break;
2587 1.4.4.2 pgoyette default:
2588 1.4.4.2 pgoyette break;
2589 1.4.4.2 pgoyette }
2590 1.4.4.2 pgoyette data += UMB_QMI_TLVLEN + tlvlen;
2591 1.4.4.2 pgoyette len -= UMB_QMI_TLVLEN + tlvlen;
2592 1.4.4.2 pgoyette }
2593 1.4.4.2 pgoyette return;
2594 1.4.4.2 pgoyette
2595 1.4.4.2 pgoyette tooshort:
2596 1.4.4.2 pgoyette DPRINTF("%s: discard short QMI message\n", DEVNAM(sc));
2597 1.4.4.2 pgoyette return;
2598 1.4.4.2 pgoyette }
2599 1.4.4.2 pgoyette
2600 1.4.4.2 pgoyette Static void
2601 1.4.4.2 pgoyette umb_intr(struct usbd_xfer *xfer, void *priv, usbd_status status)
2602 1.4.4.2 pgoyette {
2603 1.4.4.2 pgoyette struct umb_softc *sc = priv;
2604 1.4.4.2 pgoyette struct ifnet *ifp = GET_IFP(sc);
2605 1.4.4.2 pgoyette int total_len;
2606 1.4.4.2 pgoyette
2607 1.4.4.2 pgoyette if (status != USBD_NORMAL_COMPLETION) {
2608 1.4.4.2 pgoyette DPRINTF("%s: notification error: %s\n", DEVNAM(sc),
2609 1.4.4.2 pgoyette usbd_errstr(status));
2610 1.4.4.2 pgoyette if (status == USBD_STALLED)
2611 1.4.4.2 pgoyette usbd_clear_endpoint_stall_async(sc->sc_ctrl_pipe);
2612 1.4.4.2 pgoyette return;
2613 1.4.4.2 pgoyette }
2614 1.4.4.2 pgoyette usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
2615 1.4.4.2 pgoyette if (total_len < UCDC_NOTIFICATION_LENGTH) {
2616 1.4.4.2 pgoyette DPRINTF("%s: short notification (%d<%d)\n", DEVNAM(sc),
2617 1.4.4.2 pgoyette total_len, UCDC_NOTIFICATION_LENGTH);
2618 1.4.4.2 pgoyette return;
2619 1.4.4.2 pgoyette }
2620 1.4.4.2 pgoyette if (sc->sc_intr_msg.bmRequestType != UCDC_NOTIFICATION) {
2621 1.4.4.2 pgoyette DPRINTF("%s: unexpected notification (type=0x%02x)\n",
2622 1.4.4.2 pgoyette DEVNAM(sc), sc->sc_intr_msg.bmRequestType);
2623 1.4.4.2 pgoyette return;
2624 1.4.4.2 pgoyette }
2625 1.4.4.2 pgoyette
2626 1.4.4.2 pgoyette switch (sc->sc_intr_msg.bNotification) {
2627 1.4.4.2 pgoyette case UCDC_N_NETWORK_CONNECTION:
2628 1.4.4.2 pgoyette if (ifp->if_flags & IFF_DEBUG)
2629 1.4.4.2 pgoyette log(LOG_DEBUG, "%s: network %sconnected\n", DEVNAM(sc),
2630 1.4.4.2 pgoyette UGETW(sc->sc_intr_msg.wValue) ? "" : "dis");
2631 1.4.4.2 pgoyette break;
2632 1.4.4.2 pgoyette case UCDC_N_RESPONSE_AVAILABLE:
2633 1.4.4.2 pgoyette DPRINTFN(2, "%s: umb_intr: response available\n", DEVNAM(sc));
2634 1.4.4.2 pgoyette ++sc->sc_nresp;
2635 1.4.4.2 pgoyette usb_add_task(sc->sc_udev, &sc->sc_get_response_task, USB_TASKQ_DRIVER);
2636 1.4.4.2 pgoyette break;
2637 1.4.4.2 pgoyette case UCDC_N_CONNECTION_SPEED_CHANGE:
2638 1.4.4.2 pgoyette DPRINTFN(2, "%s: umb_intr: connection speed changed\n",
2639 1.4.4.2 pgoyette DEVNAM(sc));
2640 1.4.4.2 pgoyette break;
2641 1.4.4.2 pgoyette default:
2642 1.4.4.2 pgoyette DPRINTF("%s: unexpected notifiation (0x%02x)\n",
2643 1.4.4.2 pgoyette DEVNAM(sc), sc->sc_intr_msg.bNotification);
2644 1.4.4.2 pgoyette break;
2645 1.4.4.2 pgoyette }
2646 1.4.4.2 pgoyette }
2647 1.4.4.2 pgoyette
2648 1.4.4.2 pgoyette /*
2649 1.4.4.2 pgoyette * Diagnostic routines
2650 1.4.4.2 pgoyette */
2651 1.4.4.2 pgoyette Static char *
2652 1.4.4.2 pgoyette umb_ntop(struct sockaddr *sa)
2653 1.4.4.2 pgoyette {
2654 1.4.4.2 pgoyette #define NUMBUFS 4
2655 1.4.4.2 pgoyette static char astr[NUMBUFS][INET_ADDRSTRLEN];
2656 1.4.4.2 pgoyette static unsigned nbuf = 0;
2657 1.4.4.2 pgoyette char *s;
2658 1.4.4.2 pgoyette
2659 1.4.4.2 pgoyette s = astr[nbuf++];
2660 1.4.4.2 pgoyette if (nbuf >= NUMBUFS)
2661 1.4.4.2 pgoyette nbuf = 0;
2662 1.4.4.2 pgoyette
2663 1.4.4.2 pgoyette switch (sa->sa_family) {
2664 1.4.4.2 pgoyette case AF_INET:
2665 1.4.4.2 pgoyette default:
2666 1.4.4.2 pgoyette inet_ntop(AF_INET, &satosin(sa)->sin_addr, s, sizeof(astr[0]));
2667 1.4.4.2 pgoyette break;
2668 1.4.4.2 pgoyette case AF_INET6:
2669 1.4.4.2 pgoyette inet_ntop(AF_INET6, &satosin6(sa)->sin6_addr, s,
2670 1.4.4.2 pgoyette sizeof(astr[0]));
2671 1.4.4.2 pgoyette break;
2672 1.4.4.2 pgoyette }
2673 1.4.4.2 pgoyette return s;
2674 1.4.4.2 pgoyette }
2675 1.4.4.2 pgoyette
2676 1.4.4.2 pgoyette #ifdef UMB_DEBUG
2677 1.4.4.2 pgoyette Static char *
2678 1.4.4.2 pgoyette umb_uuid2str(uint8_t uuid[MBIM_UUID_LEN])
2679 1.4.4.2 pgoyette {
2680 1.4.4.2 pgoyette static char uuidstr[2 * MBIM_UUID_LEN + 5];
2681 1.4.4.2 pgoyette
2682 1.4.4.2 pgoyette #define UUID_BFMT "%02X"
2683 1.4.4.2 pgoyette #define UUID_SEP "-"
2684 1.4.4.2 pgoyette snprintf(uuidstr, sizeof(uuidstr),
2685 1.4.4.2 pgoyette UUID_BFMT UUID_BFMT UUID_BFMT UUID_BFMT UUID_SEP
2686 1.4.4.2 pgoyette UUID_BFMT UUID_BFMT UUID_SEP
2687 1.4.4.2 pgoyette UUID_BFMT UUID_BFMT UUID_SEP
2688 1.4.4.2 pgoyette UUID_BFMT UUID_BFMT UUID_SEP
2689 1.4.4.2 pgoyette UUID_BFMT UUID_BFMT UUID_BFMT UUID_BFMT UUID_BFMT UUID_BFMT,
2690 1.4.4.2 pgoyette uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5],
2691 1.4.4.2 pgoyette uuid[6], uuid[7], uuid[8], uuid[9], uuid[10], uuid[11],
2692 1.4.4.2 pgoyette uuid[12], uuid[13], uuid[14], uuid[15]);
2693 1.4.4.2 pgoyette return uuidstr;
2694 1.4.4.2 pgoyette }
2695 1.4.4.2 pgoyette
2696 1.4.4.2 pgoyette Static void
2697 1.4.4.2 pgoyette umb_dump(void *buf, int len)
2698 1.4.4.2 pgoyette {
2699 1.4.4.2 pgoyette int i = 0;
2700 1.4.4.2 pgoyette uint8_t *c = buf;
2701 1.4.4.2 pgoyette
2702 1.4.4.2 pgoyette if (len == 0)
2703 1.4.4.2 pgoyette return;
2704 1.4.4.2 pgoyette while (i < len) {
2705 1.4.4.2 pgoyette if ((i % 16) == 0) {
2706 1.4.4.2 pgoyette if (i > 0)
2707 1.4.4.2 pgoyette addlog("\n");
2708 1.4.4.2 pgoyette log(LOG_DEBUG, "%4d: ", i);
2709 1.4.4.2 pgoyette }
2710 1.4.4.2 pgoyette addlog(" %02x", *c);
2711 1.4.4.2 pgoyette c++;
2712 1.4.4.2 pgoyette i++;
2713 1.4.4.2 pgoyette }
2714 1.4.4.2 pgoyette addlog("\n");
2715 1.4.4.2 pgoyette }
2716 1.4.4.2 pgoyette #endif /* UMB_DEBUG */
2717 1.4.4.2 pgoyette
2718 1.4.4.2 pgoyette /* char *
2719 1.4.4.2 pgoyette * inet_ntop(af, src, dst, size)
2720 1.4.4.2 pgoyette * convert a network format address to presentation format.
2721 1.4.4.2 pgoyette * return:
2722 1.4.4.2 pgoyette * pointer to presentation format address (`dst'), or NULL (see errno).
2723 1.4.4.2 pgoyette * author:
2724 1.4.4.2 pgoyette * Paul Vixie, 1996.
2725 1.4.4.2 pgoyette */
2726 1.4.4.2 pgoyette Static const char *
2727 1.4.4.2 pgoyette inet_ntop(int af, const void *src, char *dst, socklen_t size)
2728 1.4.4.2 pgoyette {
2729 1.4.4.2 pgoyette switch (af) {
2730 1.4.4.2 pgoyette case AF_INET:
2731 1.4.4.2 pgoyette return inet_ntop4(src, dst, (size_t)size);
2732 1.4.4.2 pgoyette #ifdef INET6
2733 1.4.4.2 pgoyette case AF_INET6:
2734 1.4.4.2 pgoyette return inet_ntop6(src, dst, (size_t)size);
2735 1.4.4.2 pgoyette #endif /* INET6 */
2736 1.4.4.2 pgoyette default:
2737 1.4.4.2 pgoyette return NULL;
2738 1.4.4.2 pgoyette }
2739 1.4.4.2 pgoyette /* NOTREACHED */
2740 1.4.4.2 pgoyette }
2741 1.4.4.2 pgoyette
2742 1.4.4.2 pgoyette /* const char *
2743 1.4.4.2 pgoyette * inet_ntop4(src, dst, size)
2744 1.4.4.2 pgoyette * format an IPv4 address, more or less like inet_ntoa()
2745 1.4.4.2 pgoyette * return:
2746 1.4.4.2 pgoyette * `dst' (as a const)
2747 1.4.4.2 pgoyette * notes:
2748 1.4.4.2 pgoyette * (1) uses no statics
2749 1.4.4.2 pgoyette * (2) takes a u_char* not an in_addr as input
2750 1.4.4.2 pgoyette * author:
2751 1.4.4.2 pgoyette * Paul Vixie, 1996.
2752 1.4.4.2 pgoyette */
2753 1.4.4.2 pgoyette Static const char *
2754 1.4.4.2 pgoyette inet_ntop4(const u_char *src, char *dst, size_t size)
2755 1.4.4.2 pgoyette {
2756 1.4.4.2 pgoyette char tmp[sizeof "255.255.255.255"];
2757 1.4.4.2 pgoyette int l;
2758 1.4.4.2 pgoyette
2759 1.4.4.2 pgoyette l = snprintf(tmp, sizeof(tmp), "%u.%u.%u.%u",
2760 1.4.4.2 pgoyette src[0], src[1], src[2], src[3]);
2761 1.4.4.2 pgoyette if (l <= 0 || l >= size) {
2762 1.4.4.2 pgoyette return NULL;
2763 1.4.4.2 pgoyette }
2764 1.4.4.2 pgoyette strlcpy(dst, tmp, size);
2765 1.4.4.2 pgoyette return dst;
2766 1.4.4.2 pgoyette }
2767 1.4.4.2 pgoyette
2768 1.4.4.2 pgoyette #ifdef INET6
2769 1.4.4.2 pgoyette /* const char *
2770 1.4.4.2 pgoyette * inet_ntop6(src, dst, size)
2771 1.4.4.2 pgoyette * convert IPv6 binary address into presentation (printable) format
2772 1.4.4.2 pgoyette * author:
2773 1.4.4.2 pgoyette * Paul Vixie, 1996.
2774 1.4.4.2 pgoyette */
2775 1.4.4.2 pgoyette Static const char *
2776 1.4.4.2 pgoyette inet_ntop6(const u_char *src, char *dst, size_t size)
2777 1.4.4.2 pgoyette {
2778 1.4.4.2 pgoyette /*
2779 1.4.4.2 pgoyette * Note that int32_t and int16_t need only be "at least" large enough
2780 1.4.4.2 pgoyette * to contain a value of the specified size. On some systems, like
2781 1.4.4.2 pgoyette * Crays, there is no such thing as an integer variable with 16 bits.
2782 1.4.4.2 pgoyette * Keep this in mind if you think this function should have been coded
2783 1.4.4.2 pgoyette * to use pointer overlays. All the world's not a VAX.
2784 1.4.4.2 pgoyette */
2785 1.4.4.2 pgoyette char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
2786 1.4.4.2 pgoyette char *tp, *ep;
2787 1.4.4.2 pgoyette struct { int base, len; } best, cur;
2788 1.4.4.2 pgoyette #define IN6ADDRSZ 16
2789 1.4.4.2 pgoyette #define INT16SZ 2
2790 1.4.4.2 pgoyette u_int words[IN6ADDRSZ / INT16SZ];
2791 1.4.4.2 pgoyette int i;
2792 1.4.4.2 pgoyette int advance;
2793 1.4.4.2 pgoyette
2794 1.4.4.2 pgoyette /*
2795 1.4.4.2 pgoyette * Preprocess:
2796 1.4.4.2 pgoyette * Copy the input (bytewise) array into a wordwise array.
2797 1.4.4.2 pgoyette * Find the longest run of 0x00's in src[] for :: shorthanding.
2798 1.4.4.2 pgoyette */
2799 1.4.4.2 pgoyette memset(words, '\0', sizeof words);
2800 1.4.4.2 pgoyette for (i = 0; i < IN6ADDRSZ; i++)
2801 1.4.4.2 pgoyette words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
2802 1.4.4.2 pgoyette best.base = -1;
2803 1.4.4.2 pgoyette best.len = 0;
2804 1.4.4.2 pgoyette cur.base = -1;
2805 1.4.4.2 pgoyette cur.len = 0;
2806 1.4.4.2 pgoyette for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
2807 1.4.4.2 pgoyette if (words[i] == 0) {
2808 1.4.4.2 pgoyette if (cur.base == -1)
2809 1.4.4.2 pgoyette cur.base = i, cur.len = 1;
2810 1.4.4.2 pgoyette else
2811 1.4.4.2 pgoyette cur.len++;
2812 1.4.4.2 pgoyette } else {
2813 1.4.4.2 pgoyette if (cur.base != -1) {
2814 1.4.4.2 pgoyette if (best.base == -1 || cur.len > best.len)
2815 1.4.4.2 pgoyette best = cur;
2816 1.4.4.2 pgoyette cur.base = -1;
2817 1.4.4.2 pgoyette }
2818 1.4.4.2 pgoyette }
2819 1.4.4.2 pgoyette }
2820 1.4.4.2 pgoyette if (cur.base != -1) {
2821 1.4.4.2 pgoyette if (best.base == -1 || cur.len > best.len)
2822 1.4.4.2 pgoyette best = cur;
2823 1.4.4.2 pgoyette }
2824 1.4.4.2 pgoyette if (best.base != -1 && best.len < 2)
2825 1.4.4.2 pgoyette best.base = -1;
2826 1.4.4.2 pgoyette
2827 1.4.4.2 pgoyette /*
2828 1.4.4.2 pgoyette * Format the result.
2829 1.4.4.2 pgoyette */
2830 1.4.4.2 pgoyette tp = tmp;
2831 1.4.4.2 pgoyette ep = tmp + sizeof(tmp);
2832 1.4.4.2 pgoyette for (i = 0; i < (IN6ADDRSZ / INT16SZ) && tp < ep; i++) {
2833 1.4.4.2 pgoyette /* Are we inside the best run of 0x00's? */
2834 1.4.4.2 pgoyette if (best.base != -1 && i >= best.base &&
2835 1.4.4.2 pgoyette i < (best.base + best.len)) {
2836 1.4.4.2 pgoyette if (i == best.base) {
2837 1.4.4.2 pgoyette if (tp + 1 >= ep)
2838 1.4.4.2 pgoyette return NULL;
2839 1.4.4.2 pgoyette *tp++ = ':';
2840 1.4.4.2 pgoyette }
2841 1.4.4.2 pgoyette continue;
2842 1.4.4.2 pgoyette }
2843 1.4.4.2 pgoyette /* Are we following an initial run of 0x00s or any real hex? */
2844 1.4.4.2 pgoyette if (i != 0) {
2845 1.4.4.2 pgoyette if (tp + 1 >= ep)
2846 1.4.4.2 pgoyette return NULL;
2847 1.4.4.2 pgoyette *tp++ = ':';
2848 1.4.4.2 pgoyette }
2849 1.4.4.2 pgoyette /* Is this address an encapsulated IPv4? */
2850 1.4.4.2 pgoyette if (i == 6 && best.base == 0 &&
2851 1.4.4.2 pgoyette (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
2852 1.4.4.2 pgoyette if (!inet_ntop4(src+12, tp, (size_t)(ep - tp)))
2853 1.4.4.2 pgoyette return NULL;
2854 1.4.4.2 pgoyette tp += strlen(tp);
2855 1.4.4.2 pgoyette break;
2856 1.4.4.2 pgoyette }
2857 1.4.4.2 pgoyette advance = snprintf(tp, ep - tp, "%x", words[i]);
2858 1.4.4.2 pgoyette if (advance <= 0 || advance >= ep - tp)
2859 1.4.4.2 pgoyette return NULL;
2860 1.4.4.2 pgoyette tp += advance;
2861 1.4.4.2 pgoyette }
2862 1.4.4.2 pgoyette /* Was it a trailing run of 0x00's? */
2863 1.4.4.2 pgoyette if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ)) {
2864 1.4.4.2 pgoyette if (tp + 1 >= ep)
2865 1.4.4.2 pgoyette return NULL;
2866 1.4.4.2 pgoyette *tp++ = ':';
2867 1.4.4.2 pgoyette }
2868 1.4.4.2 pgoyette if (tp + 1 >= ep)
2869 1.4.4.2 pgoyette return NULL;
2870 1.4.4.2 pgoyette *tp++ = '\0';
2871 1.4.4.2 pgoyette
2872 1.4.4.2 pgoyette /*
2873 1.4.4.2 pgoyette * Check for overflow, copy, and we're done.
2874 1.4.4.2 pgoyette */
2875 1.4.4.2 pgoyette if ((size_t)(tp - tmp) > size) {
2876 1.4.4.2 pgoyette return NULL;
2877 1.4.4.2 pgoyette }
2878 1.4.4.2 pgoyette strlcpy(dst, tmp, size);
2879 1.4.4.2 pgoyette return dst;
2880 1.4.4.2 pgoyette }
2881 1.4.4.2 pgoyette #endif /* INET6 */
2882