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 void *addr = buf + sizeof(struct iphdr) +
615 sizeof(struct udphdr) +
616 offsetof(struct dhcp_packet, chaddr);
617 ether_addr_copy(nic->dest_mac_addr, addr);
621 if (nic->vlan_id > 0) {
622 mac_header_data = (void *)&vlan_eth;
623 mac_header_len = VLAN_ETH_HLEN;
625 mac_header_data = (void *)ð
626 mac_header_len = ETH_HLEN;
629 /* Format the data so that it can be put to skb */
630 ether_addr_copy(mac_header_data, nic->dest_mac_addr);
631 memcpy(mac_header_data + ETH_ALEN, nic->src_mac_addr, ETH_ALEN);
633 vlan_eth.h_vlan_TCI = htons(nic->vlan_id);
634 vlan_eth.h_vlan_proto = htons(ETH_P_8021Q);
636 if (nic_type == NIC_TYPE_ARP) {
637 /* Should be response: Only happens because
638 * there was a request from the host
640 eth.h_proto = htons(ETH_P_ARP);
641 vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_ARP);
643 ip_version = buf[0] >> 4;
644 if (ip_version == IP_VERSION_4) {
645 eth.h_proto = htons(ETH_P_IP);
646 vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_IP);
647 } else if (ip_version == IP_VERSION_6) {
648 eth.h_proto = htons(ETH_P_IPV6);
649 vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_IPV6);
651 netdev_err(dev, "Unknown IP version %d\n", ip_version);
656 /* Alloc skb and reserve align */
657 skb = dev_alloc_skb(len + mac_header_len + NET_IP_ALIGN);
660 skb_reserve(skb, NET_IP_ALIGN);
662 skb_put_data(skb, mac_header_data, mac_header_len);
663 skb_put_data(skb, buf, len);
665 skb->protocol = ((struct ethhdr *)mac_header_data)->h_proto;
667 skb_reset_mac_header(skb);
668 skb_pull(skb, ETH_HLEN);
670 gdm_lte_rx(skb, nic, nic_type);
673 static void gdm_lte_multi_sdu_pkt(struct phy_dev *phy_dev, char *buf, int len)
675 struct net_device *dev;
676 struct multi_sdu *multi_sdu = (struct multi_sdu *)buf;
677 struct sdu *sdu = NULL;
678 u8 endian = phy_dev->get_endian(phy_dev->priv_dev);
679 u8 *data = (u8 *)multi_sdu->data;
687 hci_len = gdm_dev16_to_cpu(endian, multi_sdu->len);
688 num_packet = gdm_dev16_to_cpu(endian, multi_sdu->num_packet);
690 for (i = 0; i < num_packet; i++) {
691 sdu = (struct sdu *)data;
693 cmd_evt = gdm_dev16_to_cpu(endian, sdu->cmd_evt);
694 hci_len = gdm_dev16_to_cpu(endian, sdu->len);
695 nic_type = gdm_dev32_to_cpu(endian, sdu->nic_type);
697 if (cmd_evt != LTE_RX_SDU) {
698 pr_err("rx sdu wrong hci %04x\n", cmd_evt);
702 pr_err("rx sdu invalid len %d\n", hci_len);
706 index = find_dev_index(nic_type);
708 pr_err("rx sdu invalid nic_type :%x\n", nic_type);
711 dev = phy_dev->dev[index];
712 gdm_lte_netif_rx(dev, (char *)sdu->data,
713 (int)(hci_len - 12), nic_type);
715 data += ((hci_len + 3) & 0xfffc) + HCI_HEADER_SIZE;
719 static void gdm_lte_pdn_table(struct net_device *dev, char *buf, int len)
721 struct nic *nic = netdev_priv(dev);
722 struct hci_pdn_table_ind *pdn_table = (struct hci_pdn_table_ind *)buf;
723 u8 ed = nic->phy_dev->get_endian(nic->phy_dev->priv_dev);
725 if (!pdn_table->activate) {
726 memset(&nic->pdn_table, 0x00, sizeof(struct pdn_table));
727 netdev_info(dev, "pdn deactivated\n");
732 nic->pdn_table.activate = pdn_table->activate;
733 nic->pdn_table.dft_eps_id = gdm_dev32_to_cpu(ed, pdn_table->dft_eps_id);
734 nic->pdn_table.nic_type = gdm_dev32_to_cpu(ed, pdn_table->nic_type);
736 netdev_info(dev, "pdn activated, nic_type=0x%x\n",
737 nic->pdn_table.nic_type);
740 static int gdm_lte_receive_pkt(struct phy_dev *phy_dev, char *buf, int len)
742 struct hci_packet *hci = (struct hci_packet *)buf;
743 struct hci_pdn_table_ind *pdn_table = (struct hci_pdn_table_ind *)buf;
745 struct net_device *dev;
746 u8 endian = phy_dev->get_endian(phy_dev->priv_dev);
755 cmd_evt = gdm_dev16_to_cpu(endian, hci->cmd_evt);
757 dev = phy_dev->dev[0];
763 sdu = (struct sdu *)hci->data;
764 nic_type = gdm_dev32_to_cpu(endian, sdu->nic_type);
765 index = find_dev_index(nic_type);
768 dev = phy_dev->dev[index];
769 gdm_lte_netif_rx(dev, hci->data, len, nic_type);
771 case LTE_RX_MULTI_SDU:
772 gdm_lte_multi_sdu_pkt(phy_dev, buf, len);
774 case LTE_LINK_ON_OFF_INDICATION:
775 netdev_info(dev, "link %s\n",
776 ((struct hci_connect_ind *)buf)->connect
779 case LTE_PDN_TABLE_IND:
780 pdn_table = (struct hci_pdn_table_ind *)buf;
781 nic_type = gdm_dev32_to_cpu(endian, pdn_table->nic_type);
782 index = find_dev_index(nic_type);
785 dev = phy_dev->dev[index];
786 gdm_lte_pdn_table(dev, buf, len);
789 ret = gdm_lte_event_send(dev, buf, len);
796 static int rx_complete(void *arg, void *data, int len, int context)
798 struct phy_dev *phy_dev = arg;
800 return gdm_lte_receive_pkt(phy_dev, data, len);
803 void start_rx_proc(struct phy_dev *phy_dev)
807 for (i = 0; i < MAX_RX_SUBMIT_COUNT; i++)
808 phy_dev->rcv_func(phy_dev->priv_dev,
809 rx_complete, phy_dev, USB_COMPLETE);
812 static const struct net_device_ops gdm_netdev_ops = {
813 .ndo_open = gdm_lte_open,
814 .ndo_stop = gdm_lte_close,
815 .ndo_set_config = gdm_lte_set_config,
816 .ndo_start_xmit = gdm_lte_tx,
817 .ndo_get_stats = gdm_lte_stats,
820 static u8 gdm_lte_macaddr[ETH_ALEN] = {0x00, 0x0a, 0x3b, 0x00, 0x00, 0x00};
822 static void form_mac_address(u8 *dev_addr, u8 *nic_src, u8 *nic_dest,
823 u8 *mac_address, u8 index)
825 /* Form the dev_addr */
827 ether_addr_copy(dev_addr, gdm_lte_macaddr);
829 ether_addr_copy(dev_addr, mac_address);
831 /* The last byte of the mac address
832 * should be less than or equal to 0xFC
834 dev_addr[ETH_ALEN - 1] += index;
836 /* Create random nic src and copy the first
837 * 3 bytes to be the same as dev_addr
839 eth_random_addr(nic_src);
840 memcpy(nic_src, dev_addr, 3);
842 /* Copy the nic_dest from dev_addr*/
843 ether_addr_copy(nic_dest, dev_addr);
846 static void validate_mac_address(u8 *mac_address)
848 /* if zero address or multicast bit set, restore the default value */
849 if (is_zero_ether_addr(mac_address) || (mac_address[0] & 0x01)) {
850 pr_err("MAC invalid, restoring default\n");
851 memcpy(mac_address, gdm_lte_macaddr, 6);
855 int register_lte_device(struct phy_dev *phy_dev,
856 struct device *dev, u8 *mac_address)
859 struct net_device *net;
860 char pdn_dev_name[16];
864 validate_mac_address(mac_address);
866 for (index = 0; index < MAX_NIC_TYPE; index++) {
867 /* Create device name lteXpdnX */
868 sprintf(pdn_dev_name, "lte%%dpdn%d", index);
870 /* Allocate netdev */
871 net = alloc_netdev(sizeof(struct nic), pdn_dev_name,
872 NET_NAME_UNKNOWN, ether_setup);
877 net->netdev_ops = &gdm_netdev_ops;
878 net->flags &= ~IFF_MULTICAST;
879 net->mtu = DEFAULT_MTU_SIZE;
881 nic = netdev_priv(net);
882 memset(nic, 0, sizeof(struct nic));
884 nic->phy_dev = phy_dev;
887 form_mac_address(net->dev_addr,
893 SET_NETDEV_DEV(net, dev);
894 SET_NETDEV_DEVTYPE(net, &wwan_type);
896 ret = register_netdev(net);
900 netif_carrier_on(net);
902 phy_dev->dev[index] = net;
908 unregister_lte_device(phy_dev);
913 void unregister_lte_device(struct phy_dev *phy_dev)
915 struct net_device *net;
918 for (index = 0; index < MAX_NIC_TYPE; index++) {
919 net = phy_dev->dev[index];
923 unregister_netdev(net);