2 * Copyright (c) 2012 GCT Semiconductor, Inc. All rights reserved.
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/etherdevice.h>
18 #include <linux/ipv6.h>
19 #include <linux/udp.h>
21 #include <linux/if_arp.h>
22 #include <linux/if_ether.h>
23 #include <linux/if_vlan.h>
24 #include <linux/in6.h>
25 #include <linux/tcp.h>
26 #include <linux/icmp.h>
27 #include <linux/icmpv6.h>
28 #include <linux/uaccess.h>
29 #include <net/ndisc.h>
32 #include "netlink_k.h"
34 #include "hci_packet.h"
35 #include "gdm_endian.h"
38 * Netlink protocol number
40 #define NETLINK_LTE 30
45 #define DEFAULT_MTU_SIZE 1500
47 #define gdm_dev_endian(n) (\
48 n->phy_dev->get_endian(n->phy_dev->priv_dev))
50 #define gdm_lte_hci_send(n, d, l) (\
51 n->phy_dev->send_hci_func(n->phy_dev->priv_dev, d, l, NULL, NULL))
53 #define gdm_lte_sdu_send(n, d, l, c, b, i, t) (\
54 n->phy_dev->send_sdu_func(n->phy_dev->priv_dev, d, l, n->pdn_table.dft_eps_id, 0, c, b, i, t))
56 #define gdm_lte_rcv_with_cb(n, c, b, e) (\
57 n->rcv_func(n->priv_dev, c, b, e))
59 #define IP_VERSION_4 4
60 #define IP_VERSION_6 6
67 static struct device_type wwan_type = {
71 static int gdm_lte_open(struct net_device *dev)
73 netif_start_queue(dev);
77 static int gdm_lte_close(struct net_device *dev)
79 netif_stop_queue(dev);
83 static int gdm_lte_set_config(struct net_device *dev, struct ifmap *map)
85 if (dev->flags & IFF_UP)
90 static void tx_complete(void *arg)
92 struct nic *nic = arg;
94 if (netif_queue_stopped(nic->netdev))
95 netif_wake_queue(nic->netdev);
98 static int gdm_lte_rx(struct sk_buff *skb, struct nic *nic, int nic_type)
102 ret = netif_rx_ni(skb);
103 if (ret == NET_RX_DROP) {
104 nic->stats.rx_dropped++;
106 nic->stats.rx_packets++;
107 nic->stats.rx_bytes += skb->len + ETH_HLEN;
113 int gdm_lte_emulate_arp(struct sk_buff *skb_in, u32 nic_type)
115 struct nic *nic = netdev_priv(skb_in->dev);
116 struct sk_buff *skb_out;
118 struct vlan_ethhdr vlan_eth;
119 struct arphdr *arp_in;
120 struct arphdr *arp_out;
127 struct arpdata *arp_data_in;
128 struct arpdata *arp_data_out;
130 void *mac_header_data;
133 /* Format the mac header so that it can be put to skb */
134 if (ntohs(((struct ethhdr *)skb_in->data)->h_proto) == ETH_P_8021Q) {
135 memcpy(&vlan_eth, skb_in->data, sizeof(struct vlan_ethhdr));
136 mac_header_data = &vlan_eth;
137 mac_header_len = VLAN_ETH_HLEN;
139 memcpy(ð, skb_in->data, sizeof(struct ethhdr));
140 mac_header_data = ð
141 mac_header_len = ETH_HLEN;
144 /* Get the pointer of the original request */
145 arp_in = (struct arphdr *)(skb_in->data + mac_header_len);
146 arp_data_in = (struct arpdata *)(skb_in->data + mac_header_len + sizeof(struct arphdr));
148 /* Get the pointer of the outgoing response */
149 arp_out = (struct arphdr *)arp_temp;
150 arp_data_out = (struct arpdata *)(arp_temp + sizeof(struct arphdr));
152 /* Copy the arp header */
153 memcpy(arp_out, arp_in, sizeof(struct arphdr));
154 arp_out->ar_op = htons(ARPOP_REPLY);
156 /* Copy the arp payload: based on 2 bytes of mac and fill the IP */
157 arp_data_out->ar_sha[0] = arp_data_in->ar_sha[0];
158 arp_data_out->ar_sha[1] = arp_data_in->ar_sha[1];
159 memcpy(&arp_data_out->ar_sha[2], &arp_data_in->ar_tip[0], 4);
160 memcpy(&arp_data_out->ar_sip[0], &arp_data_in->ar_tip[0], 4);
161 memcpy(&arp_data_out->ar_tha[0], &arp_data_in->ar_sha[0], 6);
162 memcpy(&arp_data_out->ar_tip[0], &arp_data_in->ar_sip[0], 4);
164 /* Fill the destination mac with source mac of the received packet */
165 memcpy(mac_header_data, mac_header_data + ETH_ALEN, ETH_ALEN);
166 /* Fill the source mac with nic's source mac */
167 memcpy(mac_header_data + ETH_ALEN, nic->src_mac_addr, ETH_ALEN);
169 /* Alloc skb and reserve align */
170 skb_out = dev_alloc_skb(skb_in->len);
173 skb_reserve(skb_out, NET_IP_ALIGN);
175 memcpy(skb_put(skb_out, mac_header_len), mac_header_data, mac_header_len);
176 memcpy(skb_put(skb_out, sizeof(struct arphdr)), arp_out, sizeof(struct arphdr));
177 memcpy(skb_put(skb_out, sizeof(struct arpdata)), arp_data_out, sizeof(struct arpdata));
179 skb_out->protocol = ((struct ethhdr *)mac_header_data)->h_proto;
180 skb_out->dev = skb_in->dev;
181 skb_reset_mac_header(skb_out);
182 skb_pull(skb_out, ETH_HLEN);
184 gdm_lte_rx(skb_out, nic, nic_type);
189 int icmp6_checksum(struct ipv6hdr *ipv6, u16 *ptr, int len)
191 unsigned short *w = ptr;
206 memset(&pseudo_header, 0, sizeof(pseudo_header));
207 memcpy(&pseudo_header.ph.ph_src, &ipv6->saddr.in6_u.u6_addr8, 16);
208 memcpy(&pseudo_header.ph.ph_dst, &ipv6->daddr.in6_u.u6_addr8, 16);
209 pseudo_header.ph.ph_len = ipv6->payload_len;
210 pseudo_header.ph.ph_nxt = ipv6->nexthdr;
212 w = (u16 *)&pseudo_header;
213 for (i = 0; i < sizeof(pseudo_header.pa) / sizeof(pseudo_header.pa[0]); i++)
214 sum += pseudo_header.pa[i];
222 sum = (sum >> 16) + (sum & 0xFFFF);
229 int gdm_lte_emulate_ndp(struct sk_buff *skb_in, u32 nic_type)
231 struct nic *nic = netdev_priv(skb_in->dev);
232 struct sk_buff *skb_out;
234 struct vlan_ethhdr vlan_eth;
235 struct neighbour_advertisement {
236 u8 target_address[16];
239 u8 link_layer_address[6];
241 struct neighbour_advertisement na;
242 struct neighbour_solicitation {
243 u8 target_address[16];
245 struct neighbour_solicitation *ns;
246 struct ipv6hdr *ipv6_in;
247 struct ipv6hdr ipv6_out;
248 struct icmp6hdr *icmp6_in;
249 struct icmp6hdr icmp6_out;
251 void *mac_header_data;
254 /* Format the mac header so that it can be put to skb */
255 if (ntohs(((struct ethhdr *)skb_in->data)->h_proto) == ETH_P_8021Q) {
256 memcpy(&vlan_eth, skb_in->data, sizeof(struct vlan_ethhdr));
257 if (ntohs(vlan_eth.h_vlan_encapsulated_proto) != ETH_P_IPV6)
259 mac_header_data = &vlan_eth;
260 mac_header_len = VLAN_ETH_HLEN;
262 memcpy(ð, skb_in->data, sizeof(struct ethhdr));
263 if (ntohs(eth.h_proto) != ETH_P_IPV6)
265 mac_header_data = ð
266 mac_header_len = ETH_HLEN;
269 /* Check if this is IPv6 ICMP packet */
270 ipv6_in = (struct ipv6hdr *)(skb_in->data + mac_header_len);
271 if (ipv6_in->version != 6 || ipv6_in->nexthdr != IPPROTO_ICMPV6)
274 /* Check if this is NDP packet */
275 icmp6_in = (struct icmp6hdr *)(skb_in->data + mac_header_len + sizeof(struct ipv6hdr));
276 if (icmp6_in->icmp6_type == NDISC_ROUTER_SOLICITATION) { /* Check RS */
278 } else if (icmp6_in->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) { /* Check NS */
279 u8 icmp_na[sizeof(struct icmp6hdr) + sizeof(struct neighbour_advertisement)];
280 u8 zero_addr8[16] = {0,};
282 if (memcmp(ipv6_in->saddr.in6_u.u6_addr8, zero_addr8, 16) == 0)
283 /* Duplicate Address Detection: Source IP is all zero */
286 icmp6_out.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
287 icmp6_out.icmp6_code = 0;
288 icmp6_out.icmp6_cksum = 0;
289 icmp6_out.icmp6_dataun.un_data32[0] = htonl(0x60000000); /* R=0, S=1, O=1 */
291 ns = (struct neighbour_solicitation *)(skb_in->data + mac_header_len + sizeof(struct ipv6hdr) + sizeof(struct icmp6hdr));
292 memcpy(&na.target_address, ns->target_address, 16);
295 na.link_layer_address[0] = 0x00;
296 na.link_layer_address[1] = 0x0a;
297 na.link_layer_address[2] = 0x3b;
298 na.link_layer_address[3] = 0xaf;
299 na.link_layer_address[4] = 0x63;
300 na.link_layer_address[5] = 0xc7;
302 memcpy(&ipv6_out, ipv6_in, sizeof(struct ipv6hdr));
303 memcpy(ipv6_out.saddr.in6_u.u6_addr8, &na.target_address, 16);
304 memcpy(ipv6_out.daddr.in6_u.u6_addr8, ipv6_in->saddr.in6_u.u6_addr8, 16);
305 ipv6_out.payload_len = htons(sizeof(struct icmp6hdr) + sizeof(struct neighbour_advertisement));
307 memcpy(icmp_na, &icmp6_out, sizeof(struct icmp6hdr));
308 memcpy(icmp_na + sizeof(struct icmp6hdr), &na, sizeof(struct neighbour_advertisement));
310 icmp6_out.icmp6_cksum = icmp6_checksum(&ipv6_out, (u16 *)icmp_na, sizeof(icmp_na));
315 /* Fill the destination mac with source mac of the received packet */
316 memcpy(mac_header_data, mac_header_data + ETH_ALEN, ETH_ALEN);
317 /* Fill the source mac with nic's source mac */
318 memcpy(mac_header_data + ETH_ALEN, nic->src_mac_addr, ETH_ALEN);
320 /* Alloc skb and reserve align */
321 skb_out = dev_alloc_skb(skb_in->len);
324 skb_reserve(skb_out, NET_IP_ALIGN);
326 memcpy(skb_put(skb_out, mac_header_len), mac_header_data, mac_header_len);
327 memcpy(skb_put(skb_out, sizeof(struct ipv6hdr)), &ipv6_out, sizeof(struct ipv6hdr));
328 memcpy(skb_put(skb_out, sizeof(struct icmp6hdr)), &icmp6_out, sizeof(struct icmp6hdr));
329 memcpy(skb_put(skb_out, sizeof(struct neighbour_advertisement)), &na, sizeof(struct neighbour_advertisement));
331 skb_out->protocol = ((struct ethhdr *)mac_header_data)->h_proto;
332 skb_out->dev = skb_in->dev;
333 skb_reset_mac_header(skb_out);
334 skb_pull(skb_out, ETH_HLEN);
336 gdm_lte_rx(skb_out, nic, nic_type);
341 static s32 gdm_lte_tx_nic_type(struct net_device *dev, struct sk_buff *skb)
343 struct nic *nic = netdev_priv(dev);
345 struct vlan_ethhdr *vlan_eth;
347 struct ipv6hdr *ipv6;
352 /* NIC TYPE is based on the nic_id of this net_device */
353 nic_type = 0x00000010 | nic->nic_id;
355 /* Get ethernet protocol */
356 eth = (struct ethhdr *)skb->data;
357 if (ntohs(eth->h_proto) == ETH_P_8021Q) {
358 vlan_eth = (struct vlan_ethhdr *)skb->data;
359 mac_proto = ntohs(vlan_eth->h_vlan_encapsulated_proto);
360 network_data = skb->data + VLAN_ETH_HLEN;
361 nic_type |= NIC_TYPE_F_VLAN;
363 mac_proto = ntohs(eth->h_proto);
364 network_data = skb->data + ETH_HLEN;
367 /* Process packet for nic type */
370 nic_type |= NIC_TYPE_ARP;
373 nic_type |= NIC_TYPE_F_IPV4;
374 ip = (struct iphdr *)network_data;
377 if (ip->protocol == IPPROTO_UDP) {
378 struct udphdr *udp = (struct udphdr *)(network_data + sizeof(struct iphdr));
379 if (ntohs(udp->dest) == 67 || ntohs(udp->dest) == 68)
380 nic_type |= NIC_TYPE_F_DHCP;
384 nic_type |= NIC_TYPE_F_IPV6;
385 ipv6 = (struct ipv6hdr *)network_data;
387 if (ipv6->nexthdr == IPPROTO_ICMPV6) /* Check NDP request */ {
388 struct icmp6hdr *icmp6 = (struct icmp6hdr *)(network_data + sizeof(struct ipv6hdr));
389 if (/*icmp6->icmp6_type == NDISC_ROUTER_SOLICITATION || */
390 icmp6->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
391 nic_type |= NIC_TYPE_ICMPV6;
392 } else if (ipv6->nexthdr == IPPROTO_UDP) /* Check DHCPv6 */ {
393 struct udphdr *udp = (struct udphdr *)(network_data + sizeof(struct ipv6hdr));
394 if (ntohs(udp->dest) == 546 || ntohs(udp->dest) == 547)
395 nic_type |= NIC_TYPE_F_DHCP;
405 static int gdm_lte_tx(struct sk_buff *skb, struct net_device *dev)
407 struct nic *nic = netdev_priv(dev);
414 nic_type = gdm_lte_tx_nic_type(dev, skb);
416 netdev_err(dev, "tx - invalid nic_type\n");
420 if (nic_type & NIC_TYPE_ARP) {
421 if (gdm_lte_emulate_arp(skb, nic_type) == 0) {
427 if (nic_type & NIC_TYPE_ICMPV6) {
428 if (gdm_lte_emulate_ndp(skb, nic_type) == 0) {
435 Need byte shift (that is, remove VLAN tag) if there is one
436 For the case of ARP, this breaks the offset as vlan_ethhdr+4 is treated as ethhdr
437 However, it shouldn't be a problem as the response starts from arp_hdr and ethhdr
438 is created by this driver based on the NIC mac
440 if (nic_type & NIC_TYPE_F_VLAN) {
441 struct vlan_ethhdr *vlan_eth = (struct vlan_ethhdr *)skb->data;
442 nic->vlan_id = ntohs(vlan_eth->h_vlan_TCI) & VLAN_VID_MASK;
443 data_buf = skb->data + (VLAN_ETH_HLEN - ETH_HLEN);
444 data_len = skb->len - (VLAN_ETH_HLEN - ETH_HLEN);
447 data_buf = skb->data;
451 /* If it is a ICMPV6 packet, clear all the other bits : for backward compatibility with the firmware */
452 if (nic_type & NIC_TYPE_ICMPV6)
453 nic_type = NIC_TYPE_ICMPV6;
455 /* If it is not a dhcp packet, clear all the flag bits : original NIC, otherwise the special flag (IPVX | DHCP) */
456 if (!(nic_type & NIC_TYPE_F_DHCP))
457 nic_type &= NIC_TYPE_MASK;
459 sscanf(dev->name, "lte%d", &idx);
461 ret = gdm_lte_sdu_send(nic,
469 if (ret == TX_NO_BUFFER || ret == TX_NO_SPC) {
470 netif_stop_queue(dev);
471 if (ret == TX_NO_BUFFER)
475 } else if (ret == TX_NO_DEV) {
479 /* Updates tx stats */
481 nic->stats.tx_dropped++;
483 nic->stats.tx_packets++;
484 nic->stats.tx_bytes += data_len;
491 static struct net_device_stats *gdm_lte_stats(struct net_device *dev)
493 struct nic *nic = netdev_priv(dev);
497 static int gdm_lte_event_send(struct net_device *dev, char *buf, int len)
499 struct nic *nic = netdev_priv(dev);
500 struct hci_packet *hci = (struct hci_packet *)buf;
503 sscanf(dev->name, "lte%d", &idx);
505 return netlink_send(lte_event.sock, idx, 0, buf,
506 gdm_dev16_to_cpu(gdm_dev_endian(nic), hci->len) + HCI_HEADER_SIZE);
509 static void gdm_lte_event_rcv(struct net_device *dev, u16 type, void *msg, int len)
511 struct nic *nic = netdev_priv(dev);
513 gdm_lte_hci_send(nic, msg, len);
516 int gdm_lte_event_init(void)
518 if (lte_event.ref_cnt == 0)
519 lte_event.sock = netlink_init(NETLINK_LTE, gdm_lte_event_rcv);
521 if (lte_event.sock) {
526 pr_err("event init failed\n");
530 void gdm_lte_event_exit(void)
532 if (lte_event.sock && --lte_event.ref_cnt == 0) {
533 netlink_exit(lte_event.sock);
534 lte_event.sock = NULL;
538 static u8 find_dev_index(u32 nic_type)
542 index = (u8)(nic_type & 0x0000000f);
543 if (index > MAX_NIC_TYPE)
549 static void gdm_lte_netif_rx(struct net_device *dev, char *buf, int len, int flagged_nic_type)
555 struct vlan_ethhdr vlan_eth;
556 void *mac_header_data;
560 nic_type = flagged_nic_type & NIC_TYPE_MASK;
561 nic = netdev_priv(dev);
563 if (flagged_nic_type & NIC_TYPE_F_DHCP) {
564 /* Change the destination mac address with the one requested the IP */
565 if (flagged_nic_type & NIC_TYPE_F_IPV4) {
567 u8 op; /* BOOTREQUEST or BOOTREPLY */
568 u8 htype; /* hardware address type. 1 = 10mb ethernet */
569 u8 hlen; /* hardware address length */
570 u8 hops; /* used by relay agents only */
571 u32 xid; /* unique id */
572 u16 secs; /* elapsed since client began acquisition/renewal */
573 u16 flags; /* only one flag so far: */
574 #define BROADCAST_FLAG 0x8000 /* "I need broadcast replies" */
575 u32 ciaddr; /* client IP (if client is in BOUND, RENEW or REBINDING state) */
576 u32 yiaddr; /* 'your' (client) IP address */
577 /* IP address of next server to use in bootstrap, returned in DHCPOFFER, DHCPACK by server */
579 u32 gateway_nip; /* relay agent IP address */
580 u8 chaddr[16]; /* link-layer client hardware address (MAC) */
581 u8 sname[64]; /* server host name (ASCIZ) */
582 u8 file[128]; /* boot file name (ASCIZ) */
583 u32 cookie; /* fixed first four option bytes (99,130,83,99 dec) */
585 void *addr = buf + sizeof(struct iphdr) + sizeof(struct udphdr) + offsetof(struct dhcp_packet, chaddr);
586 memcpy(nic->dest_mac_addr, addr, ETH_ALEN);
590 if (nic->vlan_id > 0) {
591 mac_header_data = (void *)&vlan_eth;
592 mac_header_len = VLAN_ETH_HLEN;
594 mac_header_data = (void *)ð
595 mac_header_len = ETH_HLEN;
598 /* Format the data so that it can be put to skb */
599 memcpy(mac_header_data, nic->dest_mac_addr, ETH_ALEN);
600 memcpy(mac_header_data + ETH_ALEN, nic->src_mac_addr, ETH_ALEN);
602 vlan_eth.h_vlan_TCI = htons(nic->vlan_id);
603 vlan_eth.h_vlan_proto = htons(ETH_P_8021Q);
605 if (nic_type == NIC_TYPE_ARP) {
606 /* Should be response: Only happens because there was a request from the host */
607 eth.h_proto = htons(ETH_P_ARP);
608 vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_ARP);
610 ip_version = buf[0] >> 4;
611 if (ip_version == IP_VERSION_4) {
612 eth.h_proto = htons(ETH_P_IP);
613 vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_IP);
614 } else if (ip_version == IP_VERSION_6) {
615 eth.h_proto = htons(ETH_P_IPV6);
616 vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_IPV6);
618 netdev_err(dev, "Unknown IP version %d\n", ip_version);
623 /* Alloc skb and reserve align */
624 skb = dev_alloc_skb(len + mac_header_len + NET_IP_ALIGN);
627 skb_reserve(skb, NET_IP_ALIGN);
629 memcpy(skb_put(skb, mac_header_len), mac_header_data, mac_header_len);
630 memcpy(skb_put(skb, len), buf, len);
632 skb->protocol = ((struct ethhdr *)mac_header_data)->h_proto;
634 skb_reset_mac_header(skb);
635 skb_pull(skb, ETH_HLEN);
637 gdm_lte_rx(skb, nic, nic_type);
640 static void gdm_lte_multi_sdu_pkt(struct phy_dev *phy_dev, char *buf, int len)
642 struct net_device *dev;
643 struct multi_sdu *multi_sdu = (struct multi_sdu *)buf;
644 struct sdu *sdu = NULL;
645 u8 *data = (u8 *)multi_sdu->data;
653 hci_len = gdm_dev16_to_cpu(phy_dev->get_endian(phy_dev->priv_dev), multi_sdu->len);
654 num_packet = gdm_dev16_to_cpu(phy_dev->get_endian(phy_dev->priv_dev), multi_sdu->num_packet);
656 for (i = 0; i < num_packet; i++) {
657 sdu = (struct sdu *)data;
659 cmd_evt = gdm_dev16_to_cpu(phy_dev->get_endian(phy_dev->priv_dev), sdu->cmd_evt);
660 hci_len = gdm_dev16_to_cpu(phy_dev->get_endian(phy_dev->priv_dev), sdu->len);
661 nic_type = gdm_dev32_to_cpu(phy_dev->get_endian(phy_dev->priv_dev), sdu->nic_type);
663 if (cmd_evt != LTE_RX_SDU) {
664 pr_err("rx sdu wrong hci %04x\n", cmd_evt);
668 pr_err("rx sdu invalid len %d\n", hci_len);
672 index = find_dev_index(nic_type);
673 if (index < MAX_NIC_TYPE) {
674 dev = phy_dev->dev[index];
675 gdm_lte_netif_rx(dev, (char *)sdu->data, (int)(hci_len-12), nic_type);
677 pr_err("rx sdu invalid nic_type :%x\n", nic_type);
680 data += ((hci_len+3) & 0xfffc) + HCI_HEADER_SIZE;
684 static void gdm_lte_pdn_table(struct net_device *dev, char *buf, int len)
686 struct nic *nic = netdev_priv(dev);
687 struct hci_pdn_table_ind *pdn_table = (struct hci_pdn_table_ind *)buf;
689 if (pdn_table->activate) {
690 nic->pdn_table.activate = pdn_table->activate;
691 nic->pdn_table.dft_eps_id = gdm_dev32_to_cpu(gdm_dev_endian(nic), pdn_table->dft_eps_id);
692 nic->pdn_table.nic_type = gdm_dev32_to_cpu(gdm_dev_endian(nic), pdn_table->nic_type);
694 netdev_info(dev, "pdn activated, nic_type=0x%x\n",
695 nic->pdn_table.nic_type);
697 memset(&nic->pdn_table, 0x00, sizeof(struct pdn_table));
698 netdev_info(dev, "pdn deactivated\n");
702 static int gdm_lte_receive_pkt(struct phy_dev *phy_dev, char *buf, int len)
704 struct hci_packet *hci = (struct hci_packet *)buf;
705 struct hci_pdn_table_ind *pdn_table = (struct hci_pdn_table_ind *)buf;
707 struct net_device *dev;
716 cmd_evt = gdm_dev16_to_cpu(phy_dev->get_endian(phy_dev->priv_dev), hci->cmd_evt);
718 dev = phy_dev->dev[0];
724 sdu = (struct sdu *)hci->data;
725 nic_type = gdm_dev32_to_cpu(phy_dev->get_endian(phy_dev->priv_dev), sdu->nic_type);
726 index = find_dev_index(nic_type);
727 dev = phy_dev->dev[index];
728 gdm_lte_netif_rx(dev, hci->data, len, nic_type);
730 case LTE_RX_MULTI_SDU:
731 gdm_lte_multi_sdu_pkt(phy_dev, buf, len);
733 case LTE_LINK_ON_OFF_INDICATION:
734 netdev_info(dev, "link %s\n",
735 ((struct hci_connect_ind *)buf)->connect
738 case LTE_PDN_TABLE_IND:
739 pdn_table = (struct hci_pdn_table_ind *)buf;
740 nic_type = gdm_dev32_to_cpu(phy_dev->get_endian(phy_dev->priv_dev), pdn_table->nic_type);
741 index = find_dev_index(nic_type);
742 dev = phy_dev->dev[index];
743 gdm_lte_pdn_table(dev, buf, len);
746 ret = gdm_lte_event_send(dev, buf, len);
753 static int rx_complete(void *arg, void *data, int len, int context)
755 struct phy_dev *phy_dev = (struct phy_dev *)arg;
757 return gdm_lte_receive_pkt(phy_dev, (char *)data, len);
760 void start_rx_proc(struct phy_dev *phy_dev)
764 for (i = 0; i < MAX_RX_SUBMIT_COUNT; i++)
765 gdm_lte_rcv_with_cb(phy_dev, rx_complete, phy_dev, USB_COMPLETE);
768 static struct net_device_ops gdm_netdev_ops = {
769 .ndo_open = gdm_lte_open,
770 .ndo_stop = gdm_lte_close,
771 .ndo_set_config = gdm_lte_set_config,
772 .ndo_start_xmit = gdm_lte_tx,
773 .ndo_get_stats = gdm_lte_stats,
776 static u8 gdm_lte_macaddr[ETH_ALEN] = {0x00, 0x0a, 0x3b, 0x00, 0x00, 0x00};
778 static void form_mac_address(u8 *dev_addr, u8 *nic_src, u8 *nic_dest, u8 *mac_address, u8 index)
780 /* Form the dev_addr */
782 memcpy(dev_addr, gdm_lte_macaddr, ETH_ALEN);
784 memcpy(dev_addr, mac_address, ETH_ALEN);
786 /* The last byte of the mac address should be less than or equal to 0xFC */
787 dev_addr[ETH_ALEN-1] += index;
789 /* Create random nic src and copy the first 3 bytes to be the same as dev_addr */
790 random_ether_addr(nic_src);
791 memcpy(nic_src, dev_addr, 3);
793 /* Copy the nic_dest from dev_addr*/
794 memcpy(nic_dest, dev_addr, ETH_ALEN);
797 static void validate_mac_address(u8 *mac_address)
799 /* if zero address or multicast bit set, restore the default value */
800 if (is_zero_ether_addr(mac_address) || (mac_address[0] & 0x01)) {
801 pr_err("MAC invalid, restoring default\n");
802 memcpy(mac_address, gdm_lte_macaddr, 6);
806 int register_lte_device(struct phy_dev *phy_dev, struct device *dev, u8 *mac_address)
809 struct net_device *net;
810 char pdn_dev_name[16];
814 validate_mac_address(mac_address);
816 for (index = 0; index < MAX_NIC_TYPE; index++) {
817 /* Create device name lteXpdnX */
818 sprintf(pdn_dev_name, "lte%%dpdn%d", index);
820 /* Allocate netdev */
821 net = alloc_netdev(sizeof(struct nic), pdn_dev_name, ether_setup);
823 pr_err("alloc_netdev failed\n");
827 net->netdev_ops = &gdm_netdev_ops;
828 net->flags &= ~IFF_MULTICAST;
829 net->mtu = DEFAULT_MTU_SIZE;
831 nic = netdev_priv(net);
832 memset(nic, 0, sizeof(struct nic));
834 nic->phy_dev = phy_dev;
844 SET_NETDEV_DEV(net, dev);
845 SET_NETDEV_DEVTYPE(net, &wwan_type);
847 ret = register_netdev(net);
851 netif_carrier_on(net);
853 phy_dev->dev[index] = net;
859 unregister_lte_device(phy_dev);
864 void unregister_lte_device(struct phy_dev *phy_dev)
866 struct net_device *net;
869 for (index = 0; index < MAX_NIC_TYPE; index++) {
870 net = phy_dev->dev[index];
874 unregister_netdev(net);