kalmia.c 9.9 KB

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