| 1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
/* |
| 3 |
* USB network interface driver for Samsung Kalmia based LTE USB modem like the |
| 4 |
* Samsung GT-B3730 and GT-B3710. |
| 5 |
* |
| 6 |
* Copyright (C) 2011 Marius Bjoernstad Kotsbak <[email protected]> |
| 7 |
* |
| 8 |
* Sponsored by Quicklink Video Distribution Services Ltd. |
| 9 |
* |
| 10 |
* Based on the cdc_eem module. |
| 11 |
*/ |
| 12 |
|
| 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> |
| 25 |
|
| 26 |
/* |
| 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 |
| 29 |
* module. |
| 30 |
* |
| 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. |
| 38 |
* |
| 39 |
* More information about the use of the modem is available in usb_modeswitch |
| 40 |
* forum and the project page: |
| 41 |
* |
| 42 |
* http://www.draisberghof.de/usb_modeswitch/bb/viewtopic.php?t=465 |
| 43 |
* https://github.com/mkotsbak/Samsung-GT-B3730-linux-driver |
| 44 |
*/ |
| 45 |
|
| 46 |
/* #define DEBUG */ |
| 47 |
/* #define VERBOSE */ |
| 48 |
|
| 49 |
#define KALMIA_HEADER_LENGTH 6 |
| 50 |
#define KALMIA_ALIGN_SIZE 4 |
| 51 |
#define KALMIA_USB_TIMEOUT 10000 |
| 52 |
|
| 53 |
/*-------------------------------------------------------------------------*/ |
| 54 |
|
| 55 |
static int |
| 56 |
kalmia_send_init_packet(struct usbnet *dev, u8 *init_msg, u8 init_msg_len, |
| 57 |
u8 *buffer, u8 expected_len) |
| 58 |
{ |
| 59 |
int act_len; |
| 60 |
int status; |
| 61 |
|
| 62 |
netdev_dbg(dev->net, "Sending init packet"); |
| 63 |
|
| 64 |
status = usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, 0x02), |
| 65 |
init_msg, init_msg_len, &act_len, KALMIA_USB_TIMEOUT); |
| 66 |
if (status != 0) { |
| 67 |
netdev_err(dev->net, |
| 68 |
"Error sending init packet. Status %i\n", |
| 69 |
status); |
| 70 |
return status; |
| 71 |
} |
| 72 |
else if (act_len != init_msg_len) { |
| 73 |
netdev_err(dev->net, |
| 74 |
"Did not send all of init packet. Bytes sent: %i", |
| 75 |
act_len); |
| 76 |
} |
| 77 |
else { |
| 78 |
netdev_dbg(dev->net, "Successfully sent init packet."); |
| 79 |
} |
| 80 |
|
| 81 |
status = usb_bulk_msg(dev->udev, usb_rcvbulkpipe(dev->udev, 0x81), |
| 82 |
buffer, expected_len, &act_len, KALMIA_USB_TIMEOUT); |
| 83 |
|
| 84 |
if (status != 0) |
| 85 |
netdev_err(dev->net, |
| 86 |
"Error receiving init result. Status %i\n", |
| 87 |
status); |
| 88 |
else if (act_len != expected_len) |
| 89 |
netdev_err(dev->net, "Unexpected init result length: %i\n", |
| 90 |
act_len); |
| 91 |
|
| 92 |
return status; |
| 93 |
} |
| 94 |
|
| 95 |
static int |
| 96 |
kalmia_init_and_get_ethernet_addr(struct usbnet *dev, u8 *ethernet_addr) |
| 97 |
{ |
| 98 |
static const char init_msg_1[] = |
| 99 |
{ 0x57, 0x50, 0x04, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, |
| 100 |
0x00, 0x00 }; |
| 101 |
static const char init_msg_2[] = |
| 102 |
{ 0x57, 0x50, 0x04, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0xf4, |
| 103 |
0x00, 0x00 }; |
| 104 |
static const int buflen = 28; |
| 105 |
char *usb_buf; |
| 106 |
int status; |
| 107 |
|
| 108 |
usb_buf = kmalloc(buflen, GFP_DMA | GFP_KERNEL); |
| 109 |
if (!usb_buf) |
| 110 |
return -ENOMEM; |
| 111 |
|
| 112 |
memcpy(usb_buf, init_msg_1, 12); |
| 113 |
status = kalmia_send_init_packet(dev, usb_buf, ARRAY_SIZE(init_msg_1), |
| 114 |
usb_buf, 24); |
| 115 |
if (status != 0) |
| 116 |
goto out; |
| 117 |
|
| 118 |
memcpy(usb_buf, init_msg_2, 12); |
| 119 |
status = kalmia_send_init_packet(dev, usb_buf, ARRAY_SIZE(init_msg_2), |
| 120 |
usb_buf, 28); |
| 121 |
if (status != 0) |
| 122 |
goto out; |
| 123 |
|
| 124 |
memcpy(ethernet_addr, usb_buf + 10, ETH_ALEN); |
| 125 |
out: |
| 126 |
kfree(usb_buf); |
| 127 |
return status; |
| 128 |
} |
| 129 |
|
| 130 |
static int |
| 131 |
kalmia_bind(struct usbnet *dev, struct usb_interface *intf) |
| 132 |
{ |
| 133 |
int status; |
| 134 |
u8 ethernet_addr[ETH_ALEN]; |
| 135 |
static const u8 ep_addr[] = { |
| 136 |
1 | USB_DIR_IN, |
| 137 |
2 | USB_DIR_OUT, |
| 138 |
0}; |
| 139 |
|
| 140 |
/* Don't bind to AT command interface */ |
| 141 |
if (intf->cur_altsetting->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC) |
| 142 |
return -EINVAL; |
| 143 |
|
| 144 |
if (!usb_check_bulk_endpoints(intf, ep_addr)) |
| 145 |
return -ENODEV; |
| 146 |
|
| 147 |
dev->in = usb_rcvbulkpipe(dev->udev, 0x81 & USB_ENDPOINT_NUMBER_MASK); |
| 148 |
dev->out = usb_sndbulkpipe(dev->udev, 0x02 & USB_ENDPOINT_NUMBER_MASK); |
| 149 |
dev->status = NULL; |
| 150 |
|
| 151 |
dev->net->hard_header_len += KALMIA_HEADER_LENGTH; |
| 152 |
dev->hard_mtu = 1400; |
| 153 |
dev->rx_urb_size = dev->hard_mtu * 10; // Found as optimal after testing |
| 154 |
|
| 155 |
status = kalmia_init_and_get_ethernet_addr(dev, ethernet_addr); |
| 156 |
if (status) |
| 157 |
return status; |
| 158 |
|
| 159 |
eth_hw_addr_set(dev->net, ethernet_addr); |
| 160 |
|
| 161 |
return status; |
| 162 |
} |
| 163 |
|
| 164 |
static struct sk_buff * |
| 165 |
kalmia_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags) |
| 166 |
{ |
| 167 |
struct sk_buff *skb2 = NULL; |
| 168 |
u16 content_len; |
| 169 |
unsigned char *header_start; |
| 170 |
unsigned char ether_type_1, ether_type_2; |
| 171 |
u8 remainder, padlen = 0; |
| 172 |
|
| 173 |
if (!skb_cloned(skb)) { |
| 174 |
int headroom = skb_headroom(skb); |
| 175 |
int tailroom = skb_tailroom(skb); |
| 176 |
|
| 177 |
if ((tailroom >= KALMIA_ALIGN_SIZE) && (headroom |
| 178 |
>= KALMIA_HEADER_LENGTH)) |
| 179 |
goto done; |
| 180 |
|
| 181 |
if ((headroom + tailroom) > (KALMIA_HEADER_LENGTH |
| 182 |
+ KALMIA_ALIGN_SIZE)) { |
| 183 |
skb->data = memmove(skb->head + KALMIA_HEADER_LENGTH, |
| 184 |
skb->data, skb->len); |
| 185 |
skb_set_tail_pointer(skb, skb->len); |
| 186 |
goto done; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
skb2 = skb_copy_expand(skb, KALMIA_HEADER_LENGTH, |
| 191 |
KALMIA_ALIGN_SIZE, flags); |
| 192 |
if (!skb2) |
| 193 |
return NULL; |
| 194 |
|
| 195 |
dev_kfree_skb_any(skb); |
| 196 |
skb = skb2; |
| 197 |
|
| 198 |
done: |
| 199 |
header_start = skb_push(skb, KALMIA_HEADER_LENGTH); |
| 200 |
ether_type_1 = header_start[KALMIA_HEADER_LENGTH + 12]; |
| 201 |
ether_type_2 = header_start[KALMIA_HEADER_LENGTH + 13]; |
| 202 |
|
| 203 |
netdev_dbg(dev->net, "Sending etherType: %02x%02x", ether_type_1, |
| 204 |
ether_type_2); |
| 205 |
|
| 206 |
/* According to empiric data for data packages */ |
| 207 |
header_start[0] = 0x57; |
| 208 |
header_start[1] = 0x44; |
| 209 |
content_len = skb->len - KALMIA_HEADER_LENGTH; |
| 210 |
|
| 211 |
put_unaligned_le16(content_len, &header_start[2]); |
| 212 |
header_start[4] = ether_type_1; |
| 213 |
header_start[5] = ether_type_2; |
| 214 |
|
| 215 |
/* Align to 4 bytes by padding with zeros */ |
| 216 |
remainder = skb->len % KALMIA_ALIGN_SIZE; |
| 217 |
if (remainder > 0) { |
| 218 |
padlen = KALMIA_ALIGN_SIZE - remainder; |
| 219 |
skb_put_zero(skb, padlen); |
| 220 |
} |
| 221 |
|
| 222 |
netdev_dbg(dev->net, |
| 223 |
"Sending package with length %i and padding %i. Header: %6phC.", |
| 224 |
content_len, padlen, header_start); |
| 225 |
|
| 226 |
return skb; |
| 227 |
} |
| 228 |
|
| 229 |
static int |
| 230 |
kalmia_rx_fixup(struct usbnet *dev, struct sk_buff *skb) |
| 231 |
{ |
| 232 |
/* |
| 233 |
* Our task here is to strip off framing, leaving skb with one |
| 234 |
* data frame for the usbnet framework code to process. |
| 235 |
*/ |
| 236 |
static const u8 HEADER_END_OF_USB_PACKET[] = |
| 237 |
{ 0x57, 0x5a, 0x00, 0x00, 0x08, 0x00 }; |
| 238 |
static const u8 EXPECTED_UNKNOWN_HEADER_1[] = |
| 239 |
{ 0x57, 0x43, 0x1e, 0x00, 0x15, 0x02 }; |
| 240 |
static const u8 EXPECTED_UNKNOWN_HEADER_2[] = |
| 241 |
{ 0x57, 0x50, 0x0e, 0x00, 0x00, 0x00 }; |
| 242 |
int i = 0; |
| 243 |
|
| 244 |
/* incomplete header? */ |
| 245 |
if (skb->len < KALMIA_HEADER_LENGTH) |
| 246 |
return 0; |
| 247 |
|
| 248 |
do { |
| 249 |
struct sk_buff *skb2 = NULL; |
| 250 |
u8 *header_start; |
| 251 |
u16 usb_packet_length, ether_packet_length; |
| 252 |
int is_last; |
| 253 |
|
| 254 |
header_start = skb->data; |
| 255 |
|
| 256 |
if (unlikely(header_start[0] != 0x57 || header_start[1] != 0x44)) { |
| 257 |
if (!memcmp(header_start, EXPECTED_UNKNOWN_HEADER_1, |
| 258 |
sizeof(EXPECTED_UNKNOWN_HEADER_1)) || !memcmp( |
| 259 |
header_start, EXPECTED_UNKNOWN_HEADER_2, |
| 260 |
sizeof(EXPECTED_UNKNOWN_HEADER_2))) { |
| 261 |
netdev_dbg(dev->net, |
| 262 |
"Received expected unknown frame header: %6phC. Package length: %i\n", |
| 263 |
header_start, |
| 264 |
skb->len - KALMIA_HEADER_LENGTH); |
| 265 |
} |
| 266 |
else { |
| 267 |
netdev_err(dev->net, |
| 268 |
"Received unknown frame header: %6phC. Package length: %i\n", |
| 269 |
header_start, |
| 270 |
skb->len - KALMIA_HEADER_LENGTH); |
| 271 |
return 0; |
| 272 |
} |
| 273 |
} |
| 274 |
else |
| 275 |
netdev_dbg(dev->net, |
| 276 |
"Received header: %6phC. Package length: %i\n", |
| 277 |
header_start, skb->len - KALMIA_HEADER_LENGTH); |
| 278 |
|
| 279 |
/* subtract start header and end header */ |
| 280 |
usb_packet_length = skb->len - (2 * KALMIA_HEADER_LENGTH); |
| 281 |
ether_packet_length = get_unaligned_le16(&header_start[2]); |
| 282 |
skb_pull(skb, KALMIA_HEADER_LENGTH); |
| 283 |
|
| 284 |
/* Some small packets misses end marker */ |
| 285 |
if (usb_packet_length < ether_packet_length) { |
| 286 |
ether_packet_length = usb_packet_length |
| 287 |
+ KALMIA_HEADER_LENGTH; |
| 288 |
is_last = true; |
| 289 |
} |
| 290 |
else { |
| 291 |
netdev_dbg(dev->net, "Correct package length #%i", i |
| 292 |
+ 1); |
| 293 |
|
| 294 |
is_last = (memcmp(skb->data + ether_packet_length, |
| 295 |
HEADER_END_OF_USB_PACKET, |
| 296 |
sizeof(HEADER_END_OF_USB_PACKET)) == 0); |
| 297 |
if (!is_last) { |
| 298 |
header_start = skb->data + ether_packet_length; |
| 299 |
netdev_dbg(dev->net, |
| 300 |
"End header: %6phC. Package length: %i\n", |
| 301 |
header_start, |
| 302 |
skb->len - KALMIA_HEADER_LENGTH); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
if (is_last) { |
| 307 |
skb2 = skb; |
| 308 |
} |
| 309 |
else { |
| 310 |
skb2 = skb_clone(skb, GFP_ATOMIC); |
| 311 |
if (unlikely(!skb2)) |
| 312 |
return 0; |
| 313 |
} |
| 314 |
|
| 315 |
skb_trim(skb2, ether_packet_length); |
| 316 |
|
| 317 |
if (is_last) { |
| 318 |
return 1; |
| 319 |
} |
| 320 |
else { |
| 321 |
usbnet_skb_return(dev, skb2); |
| 322 |
skb_pull(skb, ether_packet_length); |
| 323 |
} |
| 324 |
|
| 325 |
i++; |
| 326 |
} |
| 327 |
while (skb->len); |
| 328 |
|
| 329 |
return 1; |
| 330 |
} |
| 331 |
|
| 332 |
static const struct driver_info kalmia_info = { |
| 333 |
.description = "Samsung Kalmia LTE USB dongle", |
| 334 |
.flags = FLAG_WWAN, |
| 335 |
.bind = kalmia_bind, |
| 336 |
.rx_fixup = kalmia_rx_fixup, |
| 337 |
.tx_fixup = kalmia_tx_fixup |
| 338 |
}; |
| 339 |
|
| 340 |
/*-------------------------------------------------------------------------*/ |
| 341 |
|
| 342 |
static const struct usb_device_id products[] = { |
| 343 |
/* The unswitched USB ID, to get the module auto loaded: */ |
| 344 |
{ USB_DEVICE(0x04e8, 0x689a) }, |
| 345 |
/* The stick switched into modem (by e.g. usb_modeswitch): */ |
| 346 |
{ USB_DEVICE(0x04e8, 0x6889), |
| 347 |
.driver_info = (unsigned long) &kalmia_info, }, |
| 348 |
{ /* EMPTY == end of list */} }; |
| 349 |
MODULE_DEVICE_TABLE( usb, products); |
| 350 |
|
| 351 |
static struct usb_driver kalmia_driver = { |
| 352 |
.name = "kalmia", |
| 353 |
.id_table = products, |
| 354 |
.probe = usbnet_probe, |
| 355 |
.disconnect = usbnet_disconnect, |
| 356 |
.suspend = usbnet_suspend, |
| 357 |
.resume = usbnet_resume, |
| 358 |
.disable_hub_initiated_lpm = 1, |
| 359 |
}; |
| 360 |
|
| 361 |
module_usb_driver(kalmia_driver); |
| 362 |
|
| 363 |
MODULE_AUTHOR("Marius Bjoernstad Kotsbak <[email protected]>"); |
| 364 |
MODULE_DESCRIPTION("Samsung Kalmia USB network driver"); |
| 365 |
MODULE_LICENSE("GPL"); |