kalmia.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 <marius@kotsbak.com>
  7. *
  8. * Sponsored by Quicklink Video Distribution Services Ltd.
  9. *
  10. * Based on the cdc_eem module.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/etherdevice.h>
  15. #include <linux/ctype.h>
  16. #include <linux/ethtool.h>
  17. #include <linux/workqueue.h>
  18. #include <linux/mii.h>
  19. #include <linux/usb.h>
  20. #include <linux/crc32.h>
  21. #include <linux/usb/cdc.h>
  22. #include <linux/usb/usbnet.h>
  23. #include <linux/gfp.h>
  24. /*
  25. * The Samsung Kalmia based LTE USB modems have a CDC ACM port for modem control
  26. * handled by the "option" module and an ethernet data port handled by this
  27. * module.
  28. *
  29. * The stick must first be switched into modem mode by usb_modeswitch
  30. * or similar tool. Then the modem gets sent two initialization packets by
  31. * this module, which gives the MAC address of the device. User space can then
  32. * connect the modem using AT commands through the ACM port and then use
  33. * DHCP on the network interface exposed by this module. Network packets are
  34. * sent to and from the modem in a proprietary format discovered after watching
  35. * the behavior of the windows driver for the modem.
  36. *
  37. * More information about the use of the modem is available in usb_modeswitch
  38. * forum and the project page:
  39. *
  40. * http://www.draisberghof.de/usb_modeswitch/bb/viewtopic.php?t=465
  41. * https://github.com/mkotsbak/Samsung-GT-B3730-linux-driver
  42. */
  43. /* #define DEBUG */
  44. /* #define VERBOSE */
  45. #define KALMIA_HEADER_LENGTH 6
  46. #define KALMIA_ALIGN_SIZE 4
  47. #define KALMIA_USB_TIMEOUT 10000
  48. /*-------------------------------------------------------------------------*/
  49. static int
  50. kalmia_send_init_packet(struct usbnet *dev, u8 *init_msg, u8 init_msg_len,
  51. u8 *buffer, u8 expected_len)
  52. {
  53. int act_len;
  54. int status;
  55. netdev_dbg(dev->net, "Sending init packet");
  56. status = usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, 0x02),
  57. init_msg, init_msg_len, &act_len, KALMIA_USB_TIMEOUT);
  58. if (status != 0) {
  59. netdev_err(dev->net,
  60. "Error sending init packet. Status %i, length %i\n",
  61. status, act_len);
  62. return status;
  63. }
  64. else if (act_len != init_msg_len) {
  65. netdev_err(dev->net,
  66. "Did not send all of init packet. Bytes sent: %i",
  67. act_len);
  68. }
  69. else {
  70. netdev_dbg(dev->net, "Successfully sent init packet.");
  71. }
  72. status = usb_bulk_msg(dev->udev, usb_rcvbulkpipe(dev->udev, 0x81),
  73. buffer, expected_len, &act_len, KALMIA_USB_TIMEOUT);
  74. if (status != 0)
  75. netdev_err(dev->net,
  76. "Error receiving init result. Status %i, length %i\n",
  77. status, act_len);
  78. else if (act_len != expected_len)
  79. netdev_err(dev->net, "Unexpected init result length: %i\n",
  80. act_len);
  81. return status;
  82. }
  83. static int
  84. kalmia_init_and_get_ethernet_addr(struct usbnet *dev, u8 *ethernet_addr)
  85. {
  86. static const char init_msg_1[] =
  87. { 0x57, 0x50, 0x04, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00,
  88. 0x00, 0x00 };
  89. static const char init_msg_2[] =
  90. { 0x57, 0x50, 0x04, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0xf4,
  91. 0x00, 0x00 };
  92. static const int buflen = 28;
  93. char *usb_buf;
  94. int status;
  95. usb_buf = kmalloc(buflen, GFP_DMA | GFP_KERNEL);
  96. if (!usb_buf)
  97. return -ENOMEM;
  98. memcpy(usb_buf, init_msg_1, 12);
  99. status = kalmia_send_init_packet(dev, usb_buf, ARRAY_SIZE(init_msg_1),
  100. usb_buf, 24);
  101. if (status != 0)
  102. goto out;
  103. memcpy(usb_buf, init_msg_2, 12);
  104. status = kalmia_send_init_packet(dev, usb_buf, ARRAY_SIZE(init_msg_2),
  105. usb_buf, 28);
  106. if (status != 0)
  107. goto out;
  108. memcpy(ethernet_addr, usb_buf + 10, ETH_ALEN);
  109. out:
  110. kfree(usb_buf);
  111. return status;
  112. }
  113. static int
  114. kalmia_bind(struct usbnet *dev, struct usb_interface *intf)
  115. {
  116. int status;
  117. u8 ethernet_addr[ETH_ALEN];
  118. /* Don't bind to AT command interface */
  119. if (intf->cur_altsetting->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC)
  120. return -EINVAL;
  121. dev->in = usb_rcvbulkpipe(dev->udev, 0x81 & USB_ENDPOINT_NUMBER_MASK);
  122. dev->out = usb_sndbulkpipe(dev->udev, 0x02 & USB_ENDPOINT_NUMBER_MASK);
  123. dev->status = NULL;
  124. dev->net->hard_header_len += KALMIA_HEADER_LENGTH;
  125. dev->hard_mtu = 1400;
  126. dev->rx_urb_size = dev->hard_mtu * 10; // Found as optimal after testing
  127. status = kalmia_init_and_get_ethernet_addr(dev, ethernet_addr);
  128. if (status)
  129. return status;
  130. memcpy(dev->net->dev_addr, ethernet_addr, ETH_ALEN);
  131. return status;
  132. }
  133. static struct sk_buff *
  134. kalmia_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
  135. {
  136. struct sk_buff *skb2 = NULL;
  137. u16 content_len;
  138. unsigned char *header_start;
  139. unsigned char ether_type_1, ether_type_2;
  140. u8 remainder, padlen = 0;
  141. if (!skb_cloned(skb)) {
  142. int headroom = skb_headroom(skb);
  143. int tailroom = skb_tailroom(skb);
  144. if ((tailroom >= KALMIA_ALIGN_SIZE) && (headroom
  145. >= KALMIA_HEADER_LENGTH))
  146. goto done;
  147. if ((headroom + tailroom) > (KALMIA_HEADER_LENGTH
  148. + KALMIA_ALIGN_SIZE)) {
  149. skb->data = memmove(skb->head + KALMIA_HEADER_LENGTH,
  150. skb->data, skb->len);
  151. skb_set_tail_pointer(skb, skb->len);
  152. goto done;
  153. }
  154. }
  155. skb2 = skb_copy_expand(skb, KALMIA_HEADER_LENGTH,
  156. KALMIA_ALIGN_SIZE, flags);
  157. if (!skb2)
  158. return NULL;
  159. dev_kfree_skb_any(skb);
  160. skb = skb2;
  161. done:
  162. header_start = skb_push(skb, KALMIA_HEADER_LENGTH);
  163. ether_type_1 = header_start[KALMIA_HEADER_LENGTH + 12];
  164. ether_type_2 = header_start[KALMIA_HEADER_LENGTH + 13];
  165. netdev_dbg(dev->net, "Sending etherType: %02x%02x", ether_type_1,
  166. ether_type_2);
  167. /* According to empiric data for data packages */
  168. header_start[0] = 0x57;
  169. header_start[1] = 0x44;
  170. content_len = skb->len - KALMIA_HEADER_LENGTH;
  171. put_unaligned_le16(content_len, &header_start[2]);
  172. header_start[4] = ether_type_1;
  173. header_start[5] = ether_type_2;
  174. /* Align to 4 bytes by padding with zeros */
  175. remainder = skb->len % KALMIA_ALIGN_SIZE;
  176. if (remainder > 0) {
  177. padlen = KALMIA_ALIGN_SIZE - remainder;
  178. skb_put_zero(skb, padlen);
  179. }
  180. netdev_dbg(dev->net,
  181. "Sending package with length %i and padding %i. Header: %6phC.",
  182. content_len, padlen, header_start);
  183. return skb;
  184. }
  185. static int
  186. kalmia_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  187. {
  188. /*
  189. * Our task here is to strip off framing, leaving skb with one
  190. * data frame for the usbnet framework code to process.
  191. */
  192. static const u8 HEADER_END_OF_USB_PACKET[] =
  193. { 0x57, 0x5a, 0x00, 0x00, 0x08, 0x00 };
  194. static const u8 EXPECTED_UNKNOWN_HEADER_1[] =
  195. { 0x57, 0x43, 0x1e, 0x00, 0x15, 0x02 };
  196. static const u8 EXPECTED_UNKNOWN_HEADER_2[] =
  197. { 0x57, 0x50, 0x0e, 0x00, 0x00, 0x00 };
  198. int i = 0;
  199. /* incomplete header? */
  200. if (skb->len < KALMIA_HEADER_LENGTH)
  201. return 0;
  202. do {
  203. struct sk_buff *skb2 = NULL;
  204. u8 *header_start;
  205. u16 usb_packet_length, ether_packet_length;
  206. int is_last;
  207. header_start = skb->data;
  208. if (unlikely(header_start[0] != 0x57 || header_start[1] != 0x44)) {
  209. if (!memcmp(header_start, EXPECTED_UNKNOWN_HEADER_1,
  210. sizeof(EXPECTED_UNKNOWN_HEADER_1)) || !memcmp(
  211. header_start, EXPECTED_UNKNOWN_HEADER_2,
  212. sizeof(EXPECTED_UNKNOWN_HEADER_2))) {
  213. netdev_dbg(dev->net,
  214. "Received expected unknown frame header: %6phC. Package length: %i\n",
  215. header_start,
  216. skb->len - KALMIA_HEADER_LENGTH);
  217. }
  218. else {
  219. netdev_err(dev->net,
  220. "Received unknown frame header: %6phC. Package length: %i\n",
  221. header_start,
  222. skb->len - KALMIA_HEADER_LENGTH);
  223. return 0;
  224. }
  225. }
  226. else
  227. netdev_dbg(dev->net,
  228. "Received header: %6phC. Package length: %i\n",
  229. header_start, skb->len - KALMIA_HEADER_LENGTH);
  230. /* subtract start header and end header */
  231. usb_packet_length = skb->len - (2 * KALMIA_HEADER_LENGTH);
  232. ether_packet_length = get_unaligned_le16(&header_start[2]);
  233. skb_pull(skb, KALMIA_HEADER_LENGTH);
  234. /* Some small packets misses end marker */
  235. if (usb_packet_length < ether_packet_length) {
  236. ether_packet_length = usb_packet_length
  237. + KALMIA_HEADER_LENGTH;
  238. is_last = true;
  239. }
  240. else {
  241. netdev_dbg(dev->net, "Correct package length #%i", i
  242. + 1);
  243. is_last = (memcmp(skb->data + ether_packet_length,
  244. HEADER_END_OF_USB_PACKET,
  245. sizeof(HEADER_END_OF_USB_PACKET)) == 0);
  246. if (!is_last) {
  247. header_start = skb->data + ether_packet_length;
  248. netdev_dbg(dev->net,
  249. "End header: %6phC. Package length: %i\n",
  250. header_start,
  251. skb->len - KALMIA_HEADER_LENGTH);
  252. }
  253. }
  254. if (is_last) {
  255. skb2 = skb;
  256. }
  257. else {
  258. skb2 = skb_clone(skb, GFP_ATOMIC);
  259. if (unlikely(!skb2))
  260. return 0;
  261. }
  262. skb_trim(skb2, ether_packet_length);
  263. if (is_last) {
  264. return 1;
  265. }
  266. else {
  267. usbnet_skb_return(dev, skb2);
  268. skb_pull(skb, ether_packet_length);
  269. }
  270. i++;
  271. }
  272. while (skb->len);
  273. return 1;
  274. }
  275. static const struct driver_info kalmia_info = {
  276. .description = "Samsung Kalmia LTE USB dongle",
  277. .flags = FLAG_WWAN,
  278. .bind = kalmia_bind,
  279. .rx_fixup = kalmia_rx_fixup,
  280. .tx_fixup = kalmia_tx_fixup
  281. };
  282. /*-------------------------------------------------------------------------*/
  283. static const struct usb_device_id products[] = {
  284. /* The unswitched USB ID, to get the module auto loaded: */
  285. { USB_DEVICE(0x04e8, 0x689a) },
  286. /* The stick switched into modem (by e.g. usb_modeswitch): */
  287. { USB_DEVICE(0x04e8, 0x6889),
  288. .driver_info = (unsigned long) &kalmia_info, },
  289. { /* EMPTY == end of list */} };
  290. MODULE_DEVICE_TABLE( usb, products);
  291. static struct usb_driver kalmia_driver = {
  292. .name = "kalmia",
  293. .id_table = products,
  294. .probe = usbnet_probe,
  295. .disconnect = usbnet_disconnect,
  296. .suspend = usbnet_suspend,
  297. .resume = usbnet_resume,
  298. .disable_hub_initiated_lpm = 1,
  299. };
  300. module_usb_driver(kalmia_driver);
  301. MODULE_AUTHOR("Marius Bjoernstad Kotsbak <marius@kotsbak.com>");
  302. MODULE_DESCRIPTION("Samsung Kalmia USB network driver");
  303. MODULE_LICENSE("GPL");