1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2012 GCT Semiconductor, Inc. All rights reserved. */
4 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6 #include <linux/etherdevice.h>
8 #include <linux/ipv6.h>
11 #include <linux/if_arp.h>
12 #include <linux/if_ether.h>
13 #include <linux/if_vlan.h>
14 #include <linux/in6.h>
15 #include <linux/tcp.h>
16 #include <linux/icmp.h>
17 #include <linux/icmpv6.h>
18 #include <linux/uaccess.h>
19 #include <linux/errno.h>
20 #include <net/ndisc.h>
23 #include "netlink_k.h"
25 #include "hci_packet.h"
26 #include "gdm_endian.h"
29 * Netlink protocol number
31 #define NETLINK_LTE 30
36 #define DEFAULT_MTU_SIZE 1500
38 #define IP_VERSION_4 4
39 #define IP_VERSION_6 6
46 static struct device_type wwan_type = {
50 static int gdm_lte_open(struct net_device *dev)
52 netif_start_queue(dev);
56 static int gdm_lte_close(struct net_device *dev)
58 netif_stop_queue(dev);
62 static int gdm_lte_set_config(struct net_device *dev, struct ifmap *map)
64 if (dev->flags & IFF_UP)
69 static void tx_complete(void *arg)
71 struct nic *nic = arg;
73 if (netif_queue_stopped(nic->netdev))
74 netif_wake_queue(nic->netdev);
77 static int gdm_lte_rx(struct sk_buff *skb, struct nic *nic, int nic_type)
81 ret = netif_rx_ni(skb);
82 if (ret == NET_RX_DROP) {
83 nic->stats.rx_dropped++;
85 nic->stats.rx_packets++;
86 nic->stats.rx_bytes += skb->len + ETH_HLEN;
92 static int gdm_lte_emulate_arp(struct sk_buff *skb_in, u32 nic_type)
94 struct nic *nic = netdev_priv(skb_in->dev);
95 struct sk_buff *skb_out;
97 struct vlan_ethhdr vlan_eth;
98 struct arphdr *arp_in;
99 struct arphdr *arp_out;
106 struct arpdata *arp_data_in;
107 struct arpdata *arp_data_out;
109 void *mac_header_data;
112 /* Check for skb->len, discard if empty */
113 if (skb_in->len == 0)
116 /* Format the mac header so that it can be put to skb */
117 if (ntohs(((struct ethhdr *)skb_in->data)->h_proto) == ETH_P_8021Q) {
118 memcpy(&vlan_eth, skb_in->data, sizeof(struct vlan_ethhdr));
119 mac_header_data = &vlan_eth;
120 mac_header_len = VLAN_ETH_HLEN;
122 memcpy(ð, skb_in->data, sizeof(struct ethhdr));
123 mac_header_data = ð
124 mac_header_len = ETH_HLEN;
127 /* Get the pointer of the original request */
128 arp_in = (struct arphdr *)(skb_in->data + mac_header_len);
129 arp_data_in = (struct arpdata *)(skb_in->data + mac_header_len +
130 sizeof(struct arphdr));
132 /* Get the pointer of the outgoing response */
133 arp_out = (struct arphdr *)arp_temp;
134 arp_data_out = (struct arpdata *)(arp_temp + sizeof(struct arphdr));
136 /* Copy the arp header */
137 memcpy(arp_out, arp_in, sizeof(struct arphdr));
138 arp_out->ar_op = htons(ARPOP_REPLY);
140 /* Copy the arp payload: based on 2 bytes of mac and fill the IP */
141 arp_data_out->ar_sha[0] = arp_data_in->ar_sha[0];
142 arp_data_out->ar_sha[1] = arp_data_in->ar_sha[1];
143 memcpy(&arp_data_out->ar_sha[2], &arp_data_in->ar_tip[0], 4);
144 memcpy(&arp_data_out->ar_sip[0], &arp_data_in->ar_tip[0], 4);
145 memcpy(&arp_data_out->ar_tha[0], &arp_data_in->ar_sha[0], 6);
146 memcpy(&arp_data_out->ar_tip[0], &arp_data_in->ar_sip[0], 4);
148 /* Fill the destination mac with source mac of the received packet */
149 memcpy(mac_header_data, mac_header_data + ETH_ALEN, ETH_ALEN);
150 /* Fill the source mac with nic's source mac */
151 memcpy(mac_header_data + ETH_ALEN, nic->src_mac_addr, ETH_ALEN);
153 /* Alloc skb and reserve align */
154 skb_out = dev_alloc_skb(skb_in->len);
157 skb_reserve(skb_out, NET_IP_ALIGN);
159 skb_put_data(skb_out, mac_header_data, mac_header_len);
160 skb_put_data(skb_out, arp_out, sizeof(struct arphdr));
161 skb_put_data(skb_out, arp_data_out, sizeof(struct arpdata));
163 skb_out->protocol = ((struct ethhdr *)mac_header_data)->h_proto;
164 skb_out->dev = skb_in->dev;
165 skb_reset_mac_header(skb_out);
166 skb_pull(skb_out, ETH_HLEN);
168 gdm_lte_rx(skb_out, nic, nic_type);
173 static __sum16 icmp6_checksum(struct ipv6hdr *ipv6, u16 *ptr, int len)
191 memset(&pseudo_header, 0, sizeof(pseudo_header));
192 memcpy(&pseudo_header.ph.ph_src, &ipv6->saddr.in6_u.u6_addr8, 16);
193 memcpy(&pseudo_header.ph.ph_dst, &ipv6->daddr.in6_u.u6_addr8, 16);
194 pseudo_header.ph.ph_len = be16_to_cpu(ipv6->payload_len);
195 pseudo_header.ph.ph_nxt = ipv6->nexthdr;
197 w = (u16 *)&pseudo_header;
198 for (i = 0; i < ARRAY_SIZE(pseudo_header.pa); i++) {
199 pa = pseudo_header.pa[i];
200 sum = csum_add(sum, csum_unfold((__force __sum16)pa));
205 sum = csum_add(sum, csum_unfold((__force __sum16)*w++));
209 return csum_fold(sum);
212 static int gdm_lte_emulate_ndp(struct sk_buff *skb_in, u32 nic_type)
214 struct nic *nic = netdev_priv(skb_in->dev);
215 struct sk_buff *skb_out;
217 struct vlan_ethhdr vlan_eth;
218 struct neighbour_advertisement {
219 u8 target_address[16];
222 u8 link_layer_address[6];
224 struct neighbour_advertisement na;
225 struct neighbour_solicitation {
226 u8 target_address[16];
228 struct neighbour_solicitation *ns;
229 struct ipv6hdr *ipv6_in;
230 struct ipv6hdr ipv6_out;
231 struct icmp6hdr *icmp6_in;
232 struct icmp6hdr icmp6_out;
234 void *mac_header_data;
237 /* Format the mac header so that it can be put to skb */
238 if (ntohs(((struct ethhdr *)skb_in->data)->h_proto) == ETH_P_8021Q) {
239 memcpy(&vlan_eth, skb_in->data, sizeof(struct vlan_ethhdr));
240 if (ntohs(vlan_eth.h_vlan_encapsulated_proto) != ETH_P_IPV6)
241 return -EPROTONOSUPPORT;
242 mac_header_data = &vlan_eth;
243 mac_header_len = VLAN_ETH_HLEN;
245 memcpy(ð, skb_in->data, sizeof(struct ethhdr));
246 if (ntohs(eth.h_proto) != ETH_P_IPV6)
247 return -EPROTONOSUPPORT;
248 mac_header_data = ð
249 mac_header_len = ETH_HLEN;
252 /* Check if this is IPv6 ICMP packet */
253 ipv6_in = (struct ipv6hdr *)(skb_in->data + mac_header_len);
254 if (ipv6_in->version != 6 || ipv6_in->nexthdr != IPPROTO_ICMPV6)
255 return -EPROTONOSUPPORT;
257 /* Check if this is NDP packet */
258 icmp6_in = (struct icmp6hdr *)(skb_in->data + mac_header_len +
259 sizeof(struct ipv6hdr));
260 if (icmp6_in->icmp6_type == NDISC_ROUTER_SOLICITATION) { /* Check RS */
261 return -EPROTONOSUPPORT;
262 } else if (icmp6_in->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) {
264 u8 icmp_na[sizeof(struct icmp6hdr) +
265 sizeof(struct neighbour_advertisement)];
266 u8 zero_addr8[16] = {0,};
268 if (memcmp(ipv6_in->saddr.in6_u.u6_addr8, zero_addr8, 16) == 0)
269 /* Duplicate Address Detection: Source IP is all zero */
272 icmp6_out.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
273 icmp6_out.icmp6_code = 0;
274 icmp6_out.icmp6_cksum = 0;
276 icmp6_out.icmp6_dataun.un_data32[0] = htonl(0x60000000);
278 ns = (struct neighbour_solicitation *)
279 (skb_in->data + mac_header_len +
280 sizeof(struct ipv6hdr) + sizeof(struct icmp6hdr));
281 memcpy(&na.target_address, ns->target_address, 16);
284 na.link_layer_address[0] = 0x00;
285 na.link_layer_address[1] = 0x0a;
286 na.link_layer_address[2] = 0x3b;
287 na.link_layer_address[3] = 0xaf;
288 na.link_layer_address[4] = 0x63;
289 na.link_layer_address[5] = 0xc7;
291 memcpy(&ipv6_out, ipv6_in, sizeof(struct ipv6hdr));
292 memcpy(ipv6_out.saddr.in6_u.u6_addr8, &na.target_address, 16);
293 memcpy(ipv6_out.daddr.in6_u.u6_addr8,
294 ipv6_in->saddr.in6_u.u6_addr8, 16);
295 ipv6_out.payload_len = htons(sizeof(struct icmp6hdr) +
296 sizeof(struct neighbour_advertisement));
298 memcpy(icmp_na, &icmp6_out, sizeof(struct icmp6hdr));
299 memcpy(icmp_na + sizeof(struct icmp6hdr), &na,
300 sizeof(struct neighbour_advertisement));
302 icmp6_out.icmp6_cksum = icmp6_checksum(&ipv6_out,
309 /* Fill the destination mac with source mac of the received packet */
310 memcpy(mac_header_data, mac_header_data + ETH_ALEN, ETH_ALEN);
311 /* Fill the source mac with nic's source mac */
312 memcpy(mac_header_data + ETH_ALEN, nic->src_mac_addr, ETH_ALEN);
314 /* Alloc skb and reserve align */
315 skb_out = dev_alloc_skb(skb_in->len);
318 skb_reserve(skb_out, NET_IP_ALIGN);
320 skb_put_data(skb_out, mac_header_data, mac_header_len);
321 skb_put_data(skb_out, &ipv6_out, sizeof(struct ipv6hdr));
322 skb_put_data(skb_out, &icmp6_out, sizeof(struct icmp6hdr));
323 skb_put_data(skb_out, &na, sizeof(struct neighbour_advertisement));
325 skb_out->protocol = ((struct ethhdr *)mac_header_data)->h_proto;
326 skb_out->dev = skb_in->dev;
327 skb_reset_mac_header(skb_out);
328 skb_pull(skb_out, ETH_HLEN);
330 gdm_lte_rx(skb_out, nic, nic_type);
335 static s32 gdm_lte_tx_nic_type(struct net_device *dev, struct sk_buff *skb)
337 struct nic *nic = netdev_priv(dev);
339 struct vlan_ethhdr *vlan_eth;
341 struct ipv6hdr *ipv6;
346 /* NIC TYPE is based on the nic_id of this net_device */
347 nic_type = 0x00000010 | nic->nic_id;
349 /* Get ethernet protocol */
350 eth = (struct ethhdr *)skb->data;
351 if (ntohs(eth->h_proto) == ETH_P_8021Q) {
352 vlan_eth = (struct vlan_ethhdr *)skb->data;
353 mac_proto = ntohs(vlan_eth->h_vlan_encapsulated_proto);
354 network_data = skb->data + VLAN_ETH_HLEN;
355 nic_type |= NIC_TYPE_F_VLAN;
357 mac_proto = ntohs(eth->h_proto);
358 network_data = skb->data + ETH_HLEN;
361 /* Process packet for nic type */
364 nic_type |= NIC_TYPE_ARP;
367 nic_type |= NIC_TYPE_F_IPV4;
371 if (ip->protocol == IPPROTO_UDP) {
373 network_data + sizeof(struct iphdr);
374 if (ntohs(udp->dest) == 67 || ntohs(udp->dest) == 68)
375 nic_type |= NIC_TYPE_F_DHCP;
379 nic_type |= NIC_TYPE_F_IPV6;
382 if (ipv6->nexthdr == IPPROTO_ICMPV6) /* Check NDP request */ {
383 struct icmp6hdr *icmp6 =
384 network_data + sizeof(struct ipv6hdr);
385 if (icmp6->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
386 nic_type |= NIC_TYPE_ICMPV6;
387 } else if (ipv6->nexthdr == IPPROTO_UDP) /* Check DHCPv6 */ {
389 network_data + sizeof(struct ipv6hdr);
390 if (ntohs(udp->dest) == 546 || ntohs(udp->dest) == 547)
391 nic_type |= NIC_TYPE_F_DHCP;
401 static netdev_tx_t gdm_lte_tx(struct sk_buff *skb, struct net_device *dev)
403 struct nic *nic = netdev_priv(dev);
410 nic_type = gdm_lte_tx_nic_type(dev, skb);
412 netdev_err(dev, "tx - invalid nic_type\n");
416 if (nic_type & NIC_TYPE_ARP) {
417 if (gdm_lte_emulate_arp(skb, nic_type) == 0) {
423 if (nic_type & NIC_TYPE_ICMPV6) {
424 if (gdm_lte_emulate_ndp(skb, nic_type) == 0) {
431 * Need byte shift (that is, remove VLAN tag) if there is one
432 * For the case of ARP, this breaks the offset as vlan_ethhdr+4
433 * is treated as ethhdr However, it shouldn't be a problem as
434 * the response starts from arp_hdr and ethhdr is created by this
435 * driver based on the NIC mac
437 if (nic_type & NIC_TYPE_F_VLAN) {
438 struct vlan_ethhdr *vlan_eth = (struct vlan_ethhdr *)skb->data;
440 nic->vlan_id = ntohs(vlan_eth->h_vlan_TCI) & VLAN_VID_MASK;
441 data_buf = skb->data + (VLAN_ETH_HLEN - ETH_HLEN);
442 data_len = skb->len - (VLAN_ETH_HLEN - ETH_HLEN);
445 data_buf = skb->data;
449 /* If it is a ICMPV6 packet, clear all the other bits :
450 * 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 :
456 * original NIC, otherwise the special flag (IPVX | DHCP)
458 if (!(nic_type & NIC_TYPE_F_DHCP))
459 nic_type &= NIC_TYPE_MASK;
461 ret = sscanf(dev->name, "lte%d", &idx);
467 ret = nic->phy_dev->send_sdu_func(nic->phy_dev->priv_dev,
469 nic->pdn_table.dft_eps_id, 0,
470 tx_complete, nic, idx,
473 if (ret == TX_NO_BUFFER || ret == TX_NO_SPC) {
474 netif_stop_queue(dev);
475 if (ret == TX_NO_BUFFER)
479 } else if (ret == TX_NO_DEV) {
483 /* Updates tx stats */
485 nic->stats.tx_dropped++;
487 nic->stats.tx_packets++;
488 nic->stats.tx_bytes += data_len;
495 static struct net_device_stats *gdm_lte_stats(struct net_device *dev)
497 struct nic *nic = netdev_priv(dev);
502 static int gdm_lte_event_send(struct net_device *dev, char *buf, int len)
504 struct phy_dev *phy_dev = ((struct nic *)netdev_priv(dev))->phy_dev;
505 struct hci_packet *hci = (struct hci_packet *)buf;
510 ret = sscanf(dev->name, "lte%d", &idx);
514 length = gdm_dev16_to_cpu(phy_dev->get_endian(phy_dev->priv_dev),
515 hci->len) + HCI_HEADER_SIZE;
516 return netlink_send(lte_event.sock, idx, 0, buf, length, dev);
519 static void gdm_lte_event_rcv(struct net_device *dev, u16 type,
522 struct nic *nic = netdev_priv(dev);
524 nic->phy_dev->send_hci_func(nic->phy_dev->priv_dev, msg, len, NULL,
528 int gdm_lte_event_init(void)
530 if (lte_event.ref_cnt == 0)
531 lte_event.sock = netlink_init(NETLINK_LTE, gdm_lte_event_rcv);
533 if (lte_event.sock) {
538 pr_err("event init failed\n");
542 void gdm_lte_event_exit(void)
544 if (lte_event.sock && --lte_event.ref_cnt == 0) {
545 sock_release(lte_event.sock->sk_socket);
546 lte_event.sock = NULL;
550 static int find_dev_index(u32 nic_type)
554 index = (u8)(nic_type & 0x0000000f);
555 if (index >= MAX_NIC_TYPE)
561 static void gdm_lte_netif_rx(struct net_device *dev, char *buf,
562 int len, int flagged_nic_type)
568 struct vlan_ethhdr vlan_eth;
569 void *mac_header_data;
573 nic_type = flagged_nic_type & NIC_TYPE_MASK;
574 nic = netdev_priv(dev);
576 if (flagged_nic_type & NIC_TYPE_F_DHCP) {
577 /* Change the destination mac address
578 * with the one requested the IP
580 if (flagged_nic_type & NIC_TYPE_F_IPV4) {
582 u8 op; /* BOOTREQUEST or BOOTREPLY */
583 u8 htype; /* hardware address type.
586 u8 hlen; /* hardware address length */
587 u8 hops; /* used by relay agents only */
588 u32 xid; /* unique id */
589 u16 secs; /* elapsed since client began
590 * acquisition/renewal
592 u16 flags; /* only one flag so far: */
593 #define BROADCAST_FLAG 0x8000
594 /* "I need broadcast replies" */
595 u32 ciaddr; /* client IP (if client is in
596 * BOUND, RENEW or REBINDING state)
598 u32 yiaddr; /* 'your' (client) IP address */
599 /* IP address of next server to use in
600 * bootstrap, returned in DHCPOFFER,
604 u32 gateway_nip; /* relay agent IP address */
605 u8 chaddr[16]; /* link-layer client hardware
608 u8 sname[64]; /* server host name (ASCIZ) */
609 u8 file[128]; /* boot file name (ASCIZ) */
610 u32 cookie; /* fixed first four option
611 * bytes (99,130,83,99 dec)
614 int offset = sizeof(struct iphdr) +
615 sizeof(struct udphdr) +
616 offsetof(struct dhcp_packet, chaddr);
617 if (offset + ETH_ALEN > len)
619 ether_addr_copy(nic->dest_mac_addr, buf + offset);
623 if (nic->vlan_id > 0) {
624 mac_header_data = (void *)&vlan_eth;
625 mac_header_len = VLAN_ETH_HLEN;
627 mac_header_data = (void *)ð
628 mac_header_len = ETH_HLEN;
631 /* Format the data so that it can be put to skb */
632 ether_addr_copy(mac_header_data, nic->dest_mac_addr);
633 memcpy(mac_header_data + ETH_ALEN, nic->src_mac_addr, ETH_ALEN);
635 vlan_eth.h_vlan_TCI = htons(nic->vlan_id);
636 vlan_eth.h_vlan_proto = htons(ETH_P_8021Q);
638 if (nic_type == NIC_TYPE_ARP) {
639 /* Should be response: Only happens because
640 * there was a request from the host
642 eth.h_proto = htons(ETH_P_ARP);
643 vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_ARP);
645 ip_version = buf[0] >> 4;
646 if (ip_version == IP_VERSION_4) {
647 eth.h_proto = htons(ETH_P_IP);
648 vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_IP);
649 } else if (ip_version == IP_VERSION_6) {
650 eth.h_proto = htons(ETH_P_IPV6);
651 vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_IPV6);
653 netdev_err(dev, "Unknown IP version %d\n", ip_version);
658 /* Alloc skb and reserve align */
659 skb = dev_alloc_skb(len + mac_header_len + NET_IP_ALIGN);
662 skb_reserve(skb, NET_IP_ALIGN);
664 skb_put_data(skb, mac_header_data, mac_header_len);
665 skb_put_data(skb, buf, len);
667 skb->protocol = ((struct ethhdr *)mac_header_data)->h_proto;
669 skb_reset_mac_header(skb);
670 skb_pull(skb, ETH_HLEN);
672 gdm_lte_rx(skb, nic, nic_type);
675 static void gdm_lte_multi_sdu_pkt(struct phy_dev *phy_dev, char *buf, int len)
677 struct net_device *dev;
678 struct multi_sdu *multi_sdu = (struct multi_sdu *)buf;
679 struct sdu *sdu = NULL;
680 u8 endian = phy_dev->get_endian(phy_dev->priv_dev);
681 u8 *data = (u8 *)multi_sdu->data;
690 num_packet = gdm_dev16_to_cpu(endian, multi_sdu->num_packet);
692 for (i = 0; i < num_packet; i++) {
693 copied = data - multi_sdu->data;
694 if (len < copied + sizeof(*sdu)) {
695 pr_err("rx prevent buffer overflow");
699 sdu = (struct sdu *)data;
701 cmd_evt = gdm_dev16_to_cpu(endian, sdu->cmd_evt);
702 hci_len = gdm_dev16_to_cpu(endian, sdu->len);
703 nic_type = gdm_dev32_to_cpu(endian, sdu->nic_type);
705 if (cmd_evt != LTE_RX_SDU) {
706 pr_err("rx sdu wrong hci %04x\n", cmd_evt);
710 len < copied + sizeof(*sdu) + (hci_len - 12)) {
711 pr_err("rx sdu invalid len %d\n", hci_len);
715 index = find_dev_index(nic_type);
717 pr_err("rx sdu invalid nic_type :%x\n", nic_type);
720 dev = phy_dev->dev[index];
721 gdm_lte_netif_rx(dev, (char *)sdu->data,
722 (int)(hci_len - 12), nic_type);
724 data += ((hci_len + 3) & 0xfffc) + HCI_HEADER_SIZE;
728 static void gdm_lte_pdn_table(struct net_device *dev, char *buf, int len)
730 struct nic *nic = netdev_priv(dev);
731 struct hci_pdn_table_ind *pdn_table = (struct hci_pdn_table_ind *)buf;
732 u8 ed = nic->phy_dev->get_endian(nic->phy_dev->priv_dev);
734 if (!pdn_table->activate) {
735 memset(&nic->pdn_table, 0x00, sizeof(struct pdn_table));
736 netdev_info(dev, "pdn deactivated\n");
741 nic->pdn_table.activate = pdn_table->activate;
742 nic->pdn_table.dft_eps_id = gdm_dev32_to_cpu(ed, pdn_table->dft_eps_id);
743 nic->pdn_table.nic_type = gdm_dev32_to_cpu(ed, pdn_table->nic_type);
745 netdev_info(dev, "pdn activated, nic_type=0x%x\n",
746 nic->pdn_table.nic_type);
749 static int gdm_lte_receive_pkt(struct phy_dev *phy_dev, char *buf, int len)
751 struct hci_packet *hci = (struct hci_packet *)buf;
752 struct hci_pdn_table_ind *pdn_table = (struct hci_pdn_table_ind *)buf;
754 struct net_device *dev;
755 u8 endian = phy_dev->get_endian(phy_dev->priv_dev);
764 cmd_evt = gdm_dev16_to_cpu(endian, hci->cmd_evt);
766 dev = phy_dev->dev[0];
772 sdu = (struct sdu *)hci->data;
773 nic_type = gdm_dev32_to_cpu(endian, sdu->nic_type);
774 index = find_dev_index(nic_type);
777 dev = phy_dev->dev[index];
778 gdm_lte_netif_rx(dev, hci->data, len, nic_type);
780 case LTE_RX_MULTI_SDU:
781 gdm_lte_multi_sdu_pkt(phy_dev, buf, len);
783 case LTE_LINK_ON_OFF_INDICATION:
784 netdev_info(dev, "link %s\n",
785 ((struct hci_connect_ind *)buf)->connect
788 case LTE_PDN_TABLE_IND:
789 pdn_table = (struct hci_pdn_table_ind *)buf;
790 nic_type = gdm_dev32_to_cpu(endian, pdn_table->nic_type);
791 index = find_dev_index(nic_type);
794 dev = phy_dev->dev[index];
795 gdm_lte_pdn_table(dev, buf, len);
798 ret = gdm_lte_event_send(dev, buf, len);
805 static int rx_complete(void *arg, void *data, int len, int context)
807 struct phy_dev *phy_dev = arg;
809 return gdm_lte_receive_pkt(phy_dev, data, len);
812 void start_rx_proc(struct phy_dev *phy_dev)
816 for (i = 0; i < MAX_RX_SUBMIT_COUNT; i++)
817 phy_dev->rcv_func(phy_dev->priv_dev,
818 rx_complete, phy_dev, USB_COMPLETE);
821 static const struct net_device_ops gdm_netdev_ops = {
822 .ndo_open = gdm_lte_open,
823 .ndo_stop = gdm_lte_close,
824 .ndo_set_config = gdm_lte_set_config,
825 .ndo_start_xmit = gdm_lte_tx,
826 .ndo_get_stats = gdm_lte_stats,
829 static u8 gdm_lte_macaddr[ETH_ALEN] = {0x00, 0x0a, 0x3b, 0x00, 0x00, 0x00};
831 static void form_mac_address(u8 *dev_addr, u8 *nic_src, u8 *nic_dest,
832 u8 *mac_address, u8 index)
834 /* Form the dev_addr */
836 ether_addr_copy(dev_addr, gdm_lte_macaddr);
838 ether_addr_copy(dev_addr, mac_address);
840 /* The last byte of the mac address
841 * should be less than or equal to 0xFC
843 dev_addr[ETH_ALEN - 1] += index;
845 /* Create random nic src and copy the first
846 * 3 bytes to be the same as dev_addr
848 eth_random_addr(nic_src);
849 memcpy(nic_src, dev_addr, 3);
851 /* Copy the nic_dest from dev_addr*/
852 ether_addr_copy(nic_dest, dev_addr);
855 static void validate_mac_address(u8 *mac_address)
857 /* if zero address or multicast bit set, restore the default value */
858 if (is_zero_ether_addr(mac_address) || (mac_address[0] & 0x01)) {
859 pr_err("MAC invalid, restoring default\n");
860 memcpy(mac_address, gdm_lte_macaddr, 6);
864 int register_lte_device(struct phy_dev *phy_dev,
865 struct device *dev, u8 *mac_address)
868 struct net_device *net;
869 char pdn_dev_name[16];
873 validate_mac_address(mac_address);
875 for (index = 0; index < MAX_NIC_TYPE; index++) {
876 /* Create device name lteXpdnX */
877 sprintf(pdn_dev_name, "lte%%dpdn%d", index);
879 /* Allocate netdev */
880 net = alloc_netdev(sizeof(struct nic), pdn_dev_name,
881 NET_NAME_UNKNOWN, ether_setup);
886 net->netdev_ops = &gdm_netdev_ops;
887 net->flags &= ~IFF_MULTICAST;
888 net->mtu = DEFAULT_MTU_SIZE;
890 nic = netdev_priv(net);
891 memset(nic, 0, sizeof(struct nic));
893 nic->phy_dev = phy_dev;
896 form_mac_address(net->dev_addr,
902 SET_NETDEV_DEV(net, dev);
903 SET_NETDEV_DEVTYPE(net, &wwan_type);
905 ret = register_netdev(net);
909 netif_carrier_on(net);
911 phy_dev->dev[index] = net;
917 unregister_lte_device(phy_dev);
922 void unregister_lte_device(struct phy_dev *phy_dev)
924 struct net_device *net;
927 for (index = 0; index < MAX_NIC_TYPE; index++) {
928 net = phy_dev->dev[index];
932 unregister_netdev(net);