1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * USB network interface driver for Samsung Kalmia based LTE USB modem like the
4 * Samsung GT-B3730 and GT-B3710.
6 * Copyright (C) 2011 Marius Bjoernstad Kotsbak <marius@kotsbak.com>
8 * Sponsored by Quicklink Video Distribution Services Ltd.
10 * Based on the cdc_eem module.
13 #include <linux/module.h>
14 #include <linux/netdevice.h>
15 #include <linux/etherdevice.h>
16 #include <linux/ctype.h>
17 #include <linux/ethtool.h>
18 #include <linux/workqueue.h>
19 #include <linux/mii.h>
20 #include <linux/usb.h>
21 #include <linux/crc32.h>
22 #include <linux/usb/cdc.h>
23 #include <linux/usb/usbnet.h>
24 #include <linux/gfp.h>
27 * The Samsung Kalmia based LTE USB modems have a CDC ACM port for modem control
28 * handled by the "option" module and an ethernet data port handled by this
31 * The stick must first be switched into modem mode by usb_modeswitch
32 * or similar tool. Then the modem gets sent two initialization packets by
33 * this module, which gives the MAC address of the device. User space can then
34 * connect the modem using AT commands through the ACM port and then use
35 * DHCP on the network interface exposed by this module. Network packets are
36 * sent to and from the modem in a proprietary format discovered after watching
37 * the behavior of the windows driver for the modem.
39 * More information about the use of the modem is available in usb_modeswitch
40 * forum and the project page:
42 * http://www.draisberghof.de/usb_modeswitch/bb/viewtopic.php?t=465
43 * https://github.com/mkotsbak/Samsung-GT-B3730-linux-driver
49 #define KALMIA_HEADER_LENGTH 6
50 #define KALMIA_ALIGN_SIZE 4
51 #define KALMIA_USB_TIMEOUT 10000
53 /*-------------------------------------------------------------------------*/
56 kalmia_send_init_packet(struct usbnet
*dev
, u8
*init_msg
, u8 init_msg_len
,
57 u8
*buffer
, u8 expected_len
)
62 netdev_dbg(dev
->net
, "Sending init packet");
64 status
= usb_bulk_msg(dev
->udev
, usb_sndbulkpipe(dev
->udev
, 0x02),
65 init_msg
, init_msg_len
, &act_len
, KALMIA_USB_TIMEOUT
);
68 "Error sending init packet. Status %i, length %i\n",
72 else if (act_len
!= init_msg_len
) {
74 "Did not send all of init packet. Bytes sent: %i",
78 netdev_dbg(dev
->net
, "Successfully sent init packet.");
81 status
= usb_bulk_msg(dev
->udev
, usb_rcvbulkpipe(dev
->udev
, 0x81),
82 buffer
, expected_len
, &act_len
, KALMIA_USB_TIMEOUT
);
86 "Error receiving init result. Status %i, length %i\n",
88 else if (act_len
!= expected_len
)
89 netdev_err(dev
->net
, "Unexpected init result length: %i\n",
96 kalmia_init_and_get_ethernet_addr(struct usbnet
*dev
, u8
*ethernet_addr
)
98 static const char init_msg_1
[] =
99 { 0x57, 0x50, 0x04, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00,
101 static const char init_msg_2
[] =
102 { 0x57, 0x50, 0x04, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0xf4,
104 static const int buflen
= 28;
108 usb_buf
= kmalloc(buflen
, GFP_DMA
| GFP_KERNEL
);
112 memcpy(usb_buf
, init_msg_1
, 12);
113 status
= kalmia_send_init_packet(dev
, usb_buf
, ARRAY_SIZE(init_msg_1
),
118 memcpy(usb_buf
, init_msg_2
, 12);
119 status
= kalmia_send_init_packet(dev
, usb_buf
, ARRAY_SIZE(init_msg_2
),
124 memcpy(ethernet_addr
, usb_buf
+ 10, ETH_ALEN
);
131 kalmia_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
134 u8 ethernet_addr
[ETH_ALEN
];
136 /* Don't bind to AT command interface */
137 if (intf
->cur_altsetting
->desc
.bInterfaceClass
!= USB_CLASS_VENDOR_SPEC
)
140 dev
->in
= usb_rcvbulkpipe(dev
->udev
, 0x81 & USB_ENDPOINT_NUMBER_MASK
);
141 dev
->out
= usb_sndbulkpipe(dev
->udev
, 0x02 & USB_ENDPOINT_NUMBER_MASK
);
144 dev
->net
->hard_header_len
+= KALMIA_HEADER_LENGTH
;
145 dev
->hard_mtu
= 1400;
146 dev
->rx_urb_size
= dev
->hard_mtu
* 10; // Found as optimal after testing
148 status
= kalmia_init_and_get_ethernet_addr(dev
, ethernet_addr
);
152 memcpy(dev
->net
->dev_addr
, ethernet_addr
, ETH_ALEN
);
157 static struct sk_buff
*
158 kalmia_tx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
, gfp_t flags
)
160 struct sk_buff
*skb2
= NULL
;
162 unsigned char *header_start
;
163 unsigned char ether_type_1
, ether_type_2
;
164 u8 remainder
, padlen
= 0;
166 if (!skb_cloned(skb
)) {
167 int headroom
= skb_headroom(skb
);
168 int tailroom
= skb_tailroom(skb
);
170 if ((tailroom
>= KALMIA_ALIGN_SIZE
) && (headroom
171 >= KALMIA_HEADER_LENGTH
))
174 if ((headroom
+ tailroom
) > (KALMIA_HEADER_LENGTH
175 + KALMIA_ALIGN_SIZE
)) {
176 skb
->data
= memmove(skb
->head
+ KALMIA_HEADER_LENGTH
,
177 skb
->data
, skb
->len
);
178 skb_set_tail_pointer(skb
, skb
->len
);
183 skb2
= skb_copy_expand(skb
, KALMIA_HEADER_LENGTH
,
184 KALMIA_ALIGN_SIZE
, flags
);
188 dev_kfree_skb_any(skb
);
192 header_start
= skb_push(skb
, KALMIA_HEADER_LENGTH
);
193 ether_type_1
= header_start
[KALMIA_HEADER_LENGTH
+ 12];
194 ether_type_2
= header_start
[KALMIA_HEADER_LENGTH
+ 13];
196 netdev_dbg(dev
->net
, "Sending etherType: %02x%02x", ether_type_1
,
199 /* According to empiric data for data packages */
200 header_start
[0] = 0x57;
201 header_start
[1] = 0x44;
202 content_len
= skb
->len
- KALMIA_HEADER_LENGTH
;
204 put_unaligned_le16(content_len
, &header_start
[2]);
205 header_start
[4] = ether_type_1
;
206 header_start
[5] = ether_type_2
;
208 /* Align to 4 bytes by padding with zeros */
209 remainder
= skb
->len
% KALMIA_ALIGN_SIZE
;
211 padlen
= KALMIA_ALIGN_SIZE
- remainder
;
212 skb_put_zero(skb
, padlen
);
216 "Sending package with length %i and padding %i. Header: %6phC.",
217 content_len
, padlen
, header_start
);
223 kalmia_rx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
)
226 * Our task here is to strip off framing, leaving skb with one
227 * data frame for the usbnet framework code to process.
229 static const u8 HEADER_END_OF_USB_PACKET
[] =
230 { 0x57, 0x5a, 0x00, 0x00, 0x08, 0x00 };
231 static const u8 EXPECTED_UNKNOWN_HEADER_1
[] =
232 { 0x57, 0x43, 0x1e, 0x00, 0x15, 0x02 };
233 static const u8 EXPECTED_UNKNOWN_HEADER_2
[] =
234 { 0x57, 0x50, 0x0e, 0x00, 0x00, 0x00 };
237 /* incomplete header? */
238 if (skb
->len
< KALMIA_HEADER_LENGTH
)
242 struct sk_buff
*skb2
= NULL
;
244 u16 usb_packet_length
, ether_packet_length
;
247 header_start
= skb
->data
;
249 if (unlikely(header_start
[0] != 0x57 || header_start
[1] != 0x44)) {
250 if (!memcmp(header_start
, EXPECTED_UNKNOWN_HEADER_1
,
251 sizeof(EXPECTED_UNKNOWN_HEADER_1
)) || !memcmp(
252 header_start
, EXPECTED_UNKNOWN_HEADER_2
,
253 sizeof(EXPECTED_UNKNOWN_HEADER_2
))) {
255 "Received expected unknown frame header: %6phC. Package length: %i\n",
257 skb
->len
- KALMIA_HEADER_LENGTH
);
261 "Received unknown frame header: %6phC. Package length: %i\n",
263 skb
->len
- KALMIA_HEADER_LENGTH
);
269 "Received header: %6phC. Package length: %i\n",
270 header_start
, skb
->len
- KALMIA_HEADER_LENGTH
);
272 /* subtract start header and end header */
273 usb_packet_length
= skb
->len
- (2 * KALMIA_HEADER_LENGTH
);
274 ether_packet_length
= get_unaligned_le16(&header_start
[2]);
275 skb_pull(skb
, KALMIA_HEADER_LENGTH
);
277 /* Some small packets misses end marker */
278 if (usb_packet_length
< ether_packet_length
) {
279 ether_packet_length
= usb_packet_length
280 + KALMIA_HEADER_LENGTH
;
284 netdev_dbg(dev
->net
, "Correct package length #%i", i
287 is_last
= (memcmp(skb
->data
+ ether_packet_length
,
288 HEADER_END_OF_USB_PACKET
,
289 sizeof(HEADER_END_OF_USB_PACKET
)) == 0);
291 header_start
= skb
->data
+ ether_packet_length
;
293 "End header: %6phC. Package length: %i\n",
295 skb
->len
- KALMIA_HEADER_LENGTH
);
303 skb2
= skb_clone(skb
, GFP_ATOMIC
);
308 skb_trim(skb2
, ether_packet_length
);
314 usbnet_skb_return(dev
, skb2
);
315 skb_pull(skb
, ether_packet_length
);
325 static const struct driver_info kalmia_info
= {
326 .description
= "Samsung Kalmia LTE USB dongle",
329 .rx_fixup
= kalmia_rx_fixup
,
330 .tx_fixup
= kalmia_tx_fixup
333 /*-------------------------------------------------------------------------*/
335 static const struct usb_device_id products
[] = {
336 /* The unswitched USB ID, to get the module auto loaded: */
337 { USB_DEVICE(0x04e8, 0x689a) },
338 /* The stick switched into modem (by e.g. usb_modeswitch): */
339 { USB_DEVICE(0x04e8, 0x6889),
340 .driver_info
= (unsigned long) &kalmia_info
, },
341 { /* EMPTY == end of list */} };
342 MODULE_DEVICE_TABLE( usb
, products
);
344 static struct usb_driver kalmia_driver
= {
346 .id_table
= products
,
347 .probe
= usbnet_probe
,
348 .disconnect
= usbnet_disconnect
,
349 .suspend
= usbnet_suspend
,
350 .resume
= usbnet_resume
,
351 .disable_hub_initiated_lpm
= 1,
354 module_usb_driver(kalmia_driver
);
356 MODULE_AUTHOR("Marius Bjoernstad Kotsbak <marius@kotsbak.com>");
357 MODULE_DESCRIPTION("Samsung Kalmia USB network driver");
358 MODULE_LICENSE("GPL");