2 * USB network interface driver for Samsung Kalmia based LTE USB modem like the
3 * Samsung GT-B3730 and GT-B3710.
5 * Copyright (C) 2011 Marius Bjoernstad Kotsbak <marius@kotsbak.com>
7 * Sponsored by Quicklink Video Distribution Services Ltd.
9 * Based on the cdc_eem module.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
17 #include <linux/module.h>
18 #include <linux/netdevice.h>
19 #include <linux/etherdevice.h>
20 #include <linux/ctype.h>
21 #include <linux/ethtool.h>
22 #include <linux/workqueue.h>
23 #include <linux/mii.h>
24 #include <linux/usb.h>
25 #include <linux/crc32.h>
26 #include <linux/usb/cdc.h>
27 #include <linux/usb/usbnet.h>
28 #include <linux/gfp.h>
31 * The Samsung Kalmia based LTE USB modems have a CDC ACM port for modem control
32 * handled by the "option" module and an ethernet data port handled by this
35 * The stick must first be switched into modem mode by usb_modeswitch
36 * or similar tool. Then the modem gets sent two initialization packets by
37 * this module, which gives the MAC address of the device. User space can then
38 * connect the modem using AT commands through the ACM port and then use
39 * DHCP on the network interface exposed by this module. Network packets are
40 * sent to and from the modem in a proprietary format discovered after watching
41 * the behavior of the windows driver for the modem.
43 * More information about the use of the modem is available in usb_modeswitch
44 * forum and the project page:
46 * http://www.draisberghof.de/usb_modeswitch/bb/viewtopic.php?t=465
47 * https://github.com/mkotsbak/Samsung-GT-B3730-linux-driver
53 #define KALMIA_HEADER_LENGTH 6
54 #define KALMIA_ALIGN_SIZE 4
55 #define KALMIA_USB_TIMEOUT 10000
57 /*-------------------------------------------------------------------------*/
60 kalmia_send_init_packet(struct usbnet
*dev
, u8
*init_msg
, u8 init_msg_len
,
61 u8
*buffer
, u8 expected_len
)
66 netdev_dbg(dev
->net
, "Sending init packet");
68 status
= usb_bulk_msg(dev
->udev
, usb_sndbulkpipe(dev
->udev
, 0x02),
69 init_msg
, init_msg_len
, &act_len
, KALMIA_USB_TIMEOUT
);
72 "Error sending init packet. Status %i, length %i\n",
76 else if (act_len
!= init_msg_len
) {
78 "Did not send all of init packet. Bytes sent: %i",
82 netdev_dbg(dev
->net
, "Successfully sent init packet.");
85 status
= usb_bulk_msg(dev
->udev
, usb_rcvbulkpipe(dev
->udev
, 0x81),
86 buffer
, expected_len
, &act_len
, KALMIA_USB_TIMEOUT
);
90 "Error receiving init result. Status %i, length %i\n",
92 else if (act_len
!= expected_len
)
93 netdev_err(dev
->net
, "Unexpected init result length: %i\n",
100 kalmia_init_and_get_ethernet_addr(struct usbnet
*dev
, u8
*ethernet_addr
)
102 static const char init_msg_1
[] =
103 { 0x57, 0x50, 0x04, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00,
105 static const char init_msg_2
[] =
106 { 0x57, 0x50, 0x04, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0xf4,
108 static const int buflen
= 28;
112 usb_buf
= kmalloc(buflen
, GFP_DMA
| GFP_KERNEL
);
116 memcpy(usb_buf
, init_msg_1
, 12);
117 status
= kalmia_send_init_packet(dev
, usb_buf
, ARRAY_SIZE(init_msg_1
),
122 memcpy(usb_buf
, init_msg_2
, 12);
123 status
= kalmia_send_init_packet(dev
, usb_buf
, ARRAY_SIZE(init_msg_2
),
128 memcpy(ethernet_addr
, usb_buf
+ 10, ETH_ALEN
);
135 kalmia_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
138 u8 ethernet_addr
[ETH_ALEN
];
140 /* Don't bind to AT command interface */
141 if (intf
->cur_altsetting
->desc
.bInterfaceClass
!= USB_CLASS_VENDOR_SPEC
)
144 dev
->in
= usb_rcvbulkpipe(dev
->udev
, 0x81 & USB_ENDPOINT_NUMBER_MASK
);
145 dev
->out
= usb_sndbulkpipe(dev
->udev
, 0x02 & USB_ENDPOINT_NUMBER_MASK
);
148 dev
->net
->hard_header_len
+= KALMIA_HEADER_LENGTH
;
149 dev
->hard_mtu
= 1400;
150 dev
->rx_urb_size
= dev
->hard_mtu
* 10; // Found as optimal after testing
152 status
= kalmia_init_and_get_ethernet_addr(dev
, ethernet_addr
);
156 memcpy(dev
->net
->dev_addr
, ethernet_addr
, ETH_ALEN
);
161 static struct sk_buff
*
162 kalmia_tx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
, gfp_t flags
)
164 struct sk_buff
*skb2
= NULL
;
166 unsigned char *header_start
;
167 unsigned char ether_type_1
, ether_type_2
;
168 u8 remainder
, padlen
= 0;
170 if (!skb_cloned(skb
)) {
171 int headroom
= skb_headroom(skb
);
172 int tailroom
= skb_tailroom(skb
);
174 if ((tailroom
>= KALMIA_ALIGN_SIZE
) && (headroom
175 >= KALMIA_HEADER_LENGTH
))
178 if ((headroom
+ tailroom
) > (KALMIA_HEADER_LENGTH
179 + KALMIA_ALIGN_SIZE
)) {
180 skb
->data
= memmove(skb
->head
+ KALMIA_HEADER_LENGTH
,
181 skb
->data
, skb
->len
);
182 skb_set_tail_pointer(skb
, skb
->len
);
187 skb2
= skb_copy_expand(skb
, KALMIA_HEADER_LENGTH
,
188 KALMIA_ALIGN_SIZE
, flags
);
192 dev_kfree_skb_any(skb
);
196 header_start
= skb_push(skb
, KALMIA_HEADER_LENGTH
);
197 ether_type_1
= header_start
[KALMIA_HEADER_LENGTH
+ 12];
198 ether_type_2
= header_start
[KALMIA_HEADER_LENGTH
+ 13];
200 netdev_dbg(dev
->net
, "Sending etherType: %02x%02x", ether_type_1
,
203 /* According to empiric data for data packages */
204 header_start
[0] = 0x57;
205 header_start
[1] = 0x44;
206 content_len
= skb
->len
- KALMIA_HEADER_LENGTH
;
208 put_unaligned_le16(content_len
, &header_start
[2]);
209 header_start
[4] = ether_type_1
;
210 header_start
[5] = ether_type_2
;
212 /* Align to 4 bytes by padding with zeros */
213 remainder
= skb
->len
% KALMIA_ALIGN_SIZE
;
215 padlen
= KALMIA_ALIGN_SIZE
- remainder
;
216 skb_put_zero(skb
, padlen
);
220 "Sending package with length %i and padding %i. Header: %6phC.",
221 content_len
, padlen
, header_start
);
227 kalmia_rx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
)
230 * Our task here is to strip off framing, leaving skb with one
231 * data frame for the usbnet framework code to process.
233 static const u8 HEADER_END_OF_USB_PACKET
[] =
234 { 0x57, 0x5a, 0x00, 0x00, 0x08, 0x00 };
235 static const u8 EXPECTED_UNKNOWN_HEADER_1
[] =
236 { 0x57, 0x43, 0x1e, 0x00, 0x15, 0x02 };
237 static const u8 EXPECTED_UNKNOWN_HEADER_2
[] =
238 { 0x57, 0x50, 0x0e, 0x00, 0x00, 0x00 };
241 /* incomplete header? */
242 if (skb
->len
< KALMIA_HEADER_LENGTH
)
246 struct sk_buff
*skb2
= NULL
;
248 u16 usb_packet_length
, ether_packet_length
;
251 header_start
= skb
->data
;
253 if (unlikely(header_start
[0] != 0x57 || header_start
[1] != 0x44)) {
254 if (!memcmp(header_start
, EXPECTED_UNKNOWN_HEADER_1
,
255 sizeof(EXPECTED_UNKNOWN_HEADER_1
)) || !memcmp(
256 header_start
, EXPECTED_UNKNOWN_HEADER_2
,
257 sizeof(EXPECTED_UNKNOWN_HEADER_2
))) {
259 "Received expected unknown frame header: %6phC. Package length: %i\n",
261 skb
->len
- KALMIA_HEADER_LENGTH
);
265 "Received unknown frame header: %6phC. Package length: %i\n",
267 skb
->len
- KALMIA_HEADER_LENGTH
);
273 "Received header: %6phC. Package length: %i\n",
274 header_start
, skb
->len
- KALMIA_HEADER_LENGTH
);
276 /* subtract start header and end header */
277 usb_packet_length
= skb
->len
- (2 * KALMIA_HEADER_LENGTH
);
278 ether_packet_length
= get_unaligned_le16(&header_start
[2]);
279 skb_pull(skb
, KALMIA_HEADER_LENGTH
);
281 /* Some small packets misses end marker */
282 if (usb_packet_length
< ether_packet_length
) {
283 ether_packet_length
= usb_packet_length
284 + KALMIA_HEADER_LENGTH
;
288 netdev_dbg(dev
->net
, "Correct package length #%i", i
291 is_last
= (memcmp(skb
->data
+ ether_packet_length
,
292 HEADER_END_OF_USB_PACKET
,
293 sizeof(HEADER_END_OF_USB_PACKET
)) == 0);
295 header_start
= skb
->data
+ ether_packet_length
;
297 "End header: %6phC. Package length: %i\n",
299 skb
->len
- KALMIA_HEADER_LENGTH
);
307 skb2
= skb_clone(skb
, GFP_ATOMIC
);
312 skb_trim(skb2
, ether_packet_length
);
318 usbnet_skb_return(dev
, skb2
);
319 skb_pull(skb
, ether_packet_length
);
329 static const struct driver_info kalmia_info
= {
330 .description
= "Samsung Kalmia LTE USB dongle",
333 .rx_fixup
= kalmia_rx_fixup
,
334 .tx_fixup
= kalmia_tx_fixup
337 /*-------------------------------------------------------------------------*/
339 static const struct usb_device_id products
[] = {
340 /* The unswitched USB ID, to get the module auto loaded: */
341 { USB_DEVICE(0x04e8, 0x689a) },
342 /* The stick switched into modem (by e.g. usb_modeswitch): */
343 { USB_DEVICE(0x04e8, 0x6889),
344 .driver_info
= (unsigned long) &kalmia_info
, },
345 { /* EMPTY == end of list */} };
346 MODULE_DEVICE_TABLE( usb
, products
);
348 static struct usb_driver kalmia_driver
= {
350 .id_table
= products
,
351 .probe
= usbnet_probe
,
352 .disconnect
= usbnet_disconnect
,
353 .suspend
= usbnet_suspend
,
354 .resume
= usbnet_resume
,
355 .disable_hub_initiated_lpm
= 1,
358 module_usb_driver(kalmia_driver
);
360 MODULE_AUTHOR("Marius Bjoernstad Kotsbak <marius@kotsbak.com>");
361 MODULE_DESCRIPTION("Samsung Kalmia USB network driver");
362 MODULE_LICENSE("GPL");