1 /* huawei_cdc_ncm.c - handles Huawei devices using the CDC NCM protocol as
3 * Copyright (C) 2013 Enrico Mioso <mrkiko.rs@gmail.com>
7 * This driver handles devices resembling the CDC NCM standard, but
8 * encapsulating another protocol inside it. An example are some Huawei 3G
9 * devices, exposing an embedded AT channel where you can set up the NCM
11 * This code has been heavily inspired by the cdc_mbim.c driver, which is
12 * Copyright (c) 2012 Smith Micro Software, Inc.
13 * Copyright (c) 2012 Bjørn Mork <bjorn@mork.no>
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * version 2 as published by the Free Software Foundation.
20 #include <linux/module.h>
21 #include <linux/netdevice.h>
22 #include <linux/ethtool.h>
23 #include <linux/if_vlan.h>
25 #include <linux/mii.h>
26 #include <linux/usb.h>
27 #include <linux/usb/cdc.h>
28 #include <linux/usb/usbnet.h>
29 #include <linux/usb/cdc-wdm.h>
30 #include <linux/usb/cdc_ncm.h>
33 struct huawei_cdc_ncm_state
{
34 struct cdc_ncm_ctx
*ctx
;
36 struct usb_driver
*subdriver
;
37 struct usb_interface
*control
;
38 struct usb_interface
*data
;
41 static int huawei_cdc_ncm_manage_power(struct usbnet
*usbnet_dev
, int on
)
43 struct huawei_cdc_ncm_state
*drvstate
= (void *)&usbnet_dev
->data
;
46 if ((on
&& atomic_add_return(1, &drvstate
->pmcount
) == 1) ||
47 (!on
&& atomic_dec_and_test(&drvstate
->pmcount
))) {
48 rv
= usb_autopm_get_interface(usbnet_dev
->intf
);
49 usbnet_dev
->intf
->needs_remote_wakeup
= on
;
51 usb_autopm_put_interface(usbnet_dev
->intf
);
56 static int huawei_cdc_ncm_wdm_manage_power(struct usb_interface
*intf
,
59 struct usbnet
*usbnet_dev
= usb_get_intfdata(intf
);
61 /* can be called while disconnecting */
65 return huawei_cdc_ncm_manage_power(usbnet_dev
, status
);
69 static int huawei_cdc_ncm_bind(struct usbnet
*usbnet_dev
,
70 struct usb_interface
*intf
)
72 struct cdc_ncm_ctx
*ctx
;
73 struct usb_driver
*subdriver
= ERR_PTR(-ENODEV
);
75 struct huawei_cdc_ncm_state
*drvstate
= (void *)&usbnet_dev
->data
;
77 /* altsetting should always be 1 for NCM devices - so we hard-coded
80 ret
= cdc_ncm_bind_common(usbnet_dev
, intf
, 1);
86 if (usbnet_dev
->status
)
87 /* CDC-WMC r1.1 requires wMaxCommand to be "at least 256
90 subdriver
= usb_cdc_wdm_register(ctx
->control
,
91 &usbnet_dev
->status
->desc
,
92 256, /* wMaxCommand */
93 huawei_cdc_ncm_wdm_manage_power
);
94 if (IS_ERR(subdriver
)) {
95 ret
= PTR_ERR(subdriver
);
96 cdc_ncm_unbind(usbnet_dev
, intf
);
100 /* Prevent usbnet from using the status descriptor */
101 usbnet_dev
->status
= NULL
;
103 drvstate
->subdriver
= subdriver
;
109 static void huawei_cdc_ncm_unbind(struct usbnet
*usbnet_dev
,
110 struct usb_interface
*intf
)
112 struct huawei_cdc_ncm_state
*drvstate
= (void *)&usbnet_dev
->data
;
113 struct cdc_ncm_ctx
*ctx
= drvstate
->ctx
;
115 if (drvstate
->subdriver
&& drvstate
->subdriver
->disconnect
)
116 drvstate
->subdriver
->disconnect(ctx
->control
);
117 drvstate
->subdriver
= NULL
;
119 cdc_ncm_unbind(usbnet_dev
, intf
);
122 static int huawei_cdc_ncm_suspend(struct usb_interface
*intf
,
123 pm_message_t message
)
126 struct usbnet
*usbnet_dev
= usb_get_intfdata(intf
);
127 struct huawei_cdc_ncm_state
*drvstate
= (void *)&usbnet_dev
->data
;
128 struct cdc_ncm_ctx
*ctx
= drvstate
->ctx
;
135 ret
= usbnet_suspend(intf
, message
);
139 if (intf
== ctx
->control
&&
140 drvstate
->subdriver
&&
141 drvstate
->subdriver
->suspend
)
142 ret
= drvstate
->subdriver
->suspend(intf
, message
);
150 static int huawei_cdc_ncm_resume(struct usb_interface
*intf
)
153 struct usbnet
*usbnet_dev
= usb_get_intfdata(intf
);
154 struct huawei_cdc_ncm_state
*drvstate
= (void *)&usbnet_dev
->data
;
156 struct cdc_ncm_ctx
*ctx
= drvstate
->ctx
;
158 /* should we call subdriver's resume function? */
160 (intf
== ctx
->control
&&
161 drvstate
->subdriver
&&
162 drvstate
->subdriver
->resume
);
165 ret
= drvstate
->subdriver
->resume(intf
);
168 ret
= usbnet_resume(intf
);
169 if (ret
< 0 && callsub
)
170 drvstate
->subdriver
->suspend(intf
, PMSG_SUSPEND
);
175 static int huawei_cdc_ncm_check_connect(struct usbnet
*usbnet_dev
)
177 struct cdc_ncm_ctx
*ctx
;
179 ctx
= (struct cdc_ncm_ctx
*)usbnet_dev
->data
[0];
182 return 1; /* disconnected */
184 return !ctx
->connected
;
187 static const struct driver_info huawei_cdc_ncm_info
= {
188 .description
= "Huawei CDC NCM device",
189 .flags
= FLAG_NO_SETINT
| FLAG_MULTI_PACKET
| FLAG_WWAN
,
190 .bind
= huawei_cdc_ncm_bind
,
191 .unbind
= huawei_cdc_ncm_unbind
,
192 .check_connect
= huawei_cdc_ncm_check_connect
,
193 .manage_power
= huawei_cdc_ncm_manage_power
,
194 .rx_fixup
= cdc_ncm_rx_fixup
,
195 .tx_fixup
= cdc_ncm_tx_fixup
,
198 static const struct usb_device_id huawei_cdc_ncm_devs
[] = {
199 /* Huawei NCM devices disguised as vendor specific */
200 { USB_VENDOR_AND_INTERFACE_INFO(0x12d1, 0xff, 0x02, 0x16),
201 .driver_info
= (unsigned long)&huawei_cdc_ncm_info
,
203 { USB_VENDOR_AND_INTERFACE_INFO(0x12d1, 0xff, 0x02, 0x46),
204 .driver_info
= (unsigned long)&huawei_cdc_ncm_info
,
206 { USB_VENDOR_AND_INTERFACE_INFO(0x12d1, 0xff, 0x02, 0x76),
207 .driver_info
= (unsigned long)&huawei_cdc_ncm_info
,
210 /* Terminating entry */
214 MODULE_DEVICE_TABLE(usb
, huawei_cdc_ncm_devs
);
216 static struct usb_driver huawei_cdc_ncm_driver
= {
217 .name
= "huawei_cdc_ncm",
218 .id_table
= huawei_cdc_ncm_devs
,
219 .probe
= usbnet_probe
,
220 .disconnect
= usbnet_disconnect
,
221 .suspend
= huawei_cdc_ncm_suspend
,
222 .resume
= huawei_cdc_ncm_resume
,
223 .reset_resume
= huawei_cdc_ncm_resume
,
224 .supports_autosuspend
= 1,
225 .disable_hub_initiated_lpm
= 1,
227 module_usb_driver(huawei_cdc_ncm_driver
);
228 MODULE_AUTHOR("Enrico Mioso <mrkiko.rs@gmail.com>");
229 MODULE_DESCRIPTION("USB CDC NCM host driver with encapsulated protocol support");
230 MODULE_LICENSE("GPL");