kalmia.c 11 KB

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