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 len = skb->len + ETH_HLEN;
82 ret = netif_rx_ni(skb);
83 if (ret == NET_RX_DROP) {
84 nic->stats.rx_dropped++;
86 nic->stats.rx_packets++;
87 nic->stats.rx_bytes += len;
93 static int gdm_lte_emulate_arp(struct sk_buff *skb_in, u32 nic_type)
95 struct nic *nic = netdev_priv(skb_in->dev);
96 struct sk_buff *skb_out;
98 struct vlan_ethhdr vlan_eth;
99 struct arphdr *arp_in;
100 struct arphdr *arp_out;
107 struct arpdata *arp_data_in;
108 struct arpdata *arp_data_out;
110 void *mac_header_data;
113 /* Check for skb->len, discard if empty */
114 if (skb_in->len == 0)
117 /* Format the mac header so that it can be put to skb */
118 if (ntohs(((struct ethhdr *)skb_in->data)->h_proto) == ETH_P_8021Q) {
119 memcpy(&vlan_eth, skb_in->data, sizeof(struct vlan_ethhdr));
120 mac_header_data = &vlan_eth;
121 mac_header_len = VLAN_ETH_HLEN;
123 memcpy(ð, skb_in->data, sizeof(struct ethhdr));
124 mac_header_data = ð
125 mac_header_len = ETH_HLEN;
128 /* Get the pointer of the original request */
129 arp_in = (struct arphdr *)(skb_in->data + mac_header_len);
130 arp_data_in = (struct arpdata *)(skb_in->data + mac_header_len +
131 sizeof(struct arphdr));
133 /* Get the pointer of the outgoing response */
134 arp_out = (struct arphdr *)arp_temp;
135 arp_data_out = (struct arpdata *)(arp_temp + sizeof(struct arphdr));
137 /* Copy the arp header */
138 memcpy(arp_out, arp_in, sizeof(struct arphdr));
139 arp_out->ar_op = htons(ARPOP_REPLY);
141 /* Copy the arp payload: based on 2 bytes of mac and fill the IP */
142 arp_data_out->ar_sha[0] = arp_data_in->ar_sha[0];
143 arp_data_out->ar_sha[1] = arp_data_in->ar_sha[1];
144 memcpy(&arp_data_out->ar_sha[2], &arp_data_in->ar_tip[0], 4);
145 memcpy(&arp_data_out->ar_sip[0], &arp_data_in->ar_tip[0], 4);
146 memcpy(&arp_data_out->ar_tha[0], &arp_data_in->ar_sha[0], 6);
147 memcpy(&arp_data_out->ar_tip[0], &arp_data_in->ar_sip[0], 4);
149 /* Fill the destination mac with source mac of the received packet */
150 memcpy(mac_header_data, mac_header_data + ETH_ALEN, ETH_ALEN);
151 /* Fill the source mac with nic's source mac */
152 memcpy(mac_header_data + ETH_ALEN, nic->src_mac_addr, ETH_ALEN);
154 /* Alloc skb and reserve align */
155 skb_out = dev_alloc_skb(skb_in->len);
158 skb_reserve(skb_out, NET_IP_ALIGN);
160 skb_put_data(skb_out, mac_header_data, mac_header_len);
161 skb_put_data(skb_out, arp_out, sizeof(struct arphdr));
162 skb_put_data(skb_out, arp_data_out, sizeof(struct arpdata));
164 skb_out->protocol = ((struct ethhdr *)mac_header_data)->h_proto;
165 skb_out->dev = skb_in->dev;
166 skb_reset_mac_header(skb_out);
167 skb_pull(skb_out, ETH_HLEN);
169 gdm_lte_rx(skb_out, nic, nic_type);
174 static __sum16 icmp6_checksum(struct ipv6hdr *ipv6, u16 *ptr, int len)
192 memset(&pseudo_header, 0, sizeof(pseudo_header));
193 memcpy(&pseudo_header.ph.ph_src, &ipv6->saddr.in6_u.u6_addr8, 16);
194 memcpy(&pseudo_header.ph.ph_dst, &ipv6->daddr.in6_u.u6_addr8, 16);
195 pseudo_header.ph.ph_len = be16_to_cpu(ipv6->payload_len);
196 pseudo_header.ph.ph_nxt = ipv6->nexthdr;
198 w = (u16 *)&pseudo_header;
199 for (i = 0; i < ARRAY_SIZE(pseudo_header.pa); i++) {
200 pa = pseudo_header.pa[i];
201 sum = csum_add(sum, csum_unfold((__force __sum16)pa));
206 sum = csum_add(sum, csum_unfold((__force __sum16)*w++));
210 return csum_fold(sum);
213 static int gdm_lte_emulate_ndp(struct sk_buff *skb_in, u32 nic_type)
215 struct nic *nic = netdev_priv(skb_in->dev);
216 struct sk_buff *skb_out;
218 struct vlan_ethhdr vlan_eth;
219 struct neighbour_advertisement {
220 u8 target_address[16];
223 u8 link_layer_address[6];
225 struct neighbour_advertisement na;
226 struct neighbour_solicitation {
227 u8 target_address[16];
229 struct neighbour_solicitation *ns;
230 struct ipv6hdr *ipv6_in;
231 struct ipv6hdr ipv6_out;
232 struct icmp6hdr *icmp6_in;
233 struct icmp6hdr icmp6_out;
235 void *mac_header_data;
238 /* Format the mac header so that it can be put to skb */
239 if (ntohs(((struct ethhdr *)skb_in->data)->h_proto) == ETH_P_8021Q) {
240 memcpy(&vlan_eth, skb_in->data, sizeof(struct vlan_ethhdr));
241 if (ntohs(vlan_eth.h_vlan_encapsulated_proto) != ETH_P_IPV6)
242 return -EPROTONOSUPPORT;
243 mac_header_data = &vlan_eth;
244 mac_header_len = VLAN_ETH_HLEN;
246 memcpy(ð, skb_in->data, sizeof(struct ethhdr));
247 if (ntohs(eth.h_proto) != ETH_P_IPV6)
248 return -EPROTONOSUPPORT;
249 mac_header_data = ð
250 mac_header_len = ETH_HLEN;
253 /* Check if this is IPv6 ICMP packet */
254 ipv6_in = (struct ipv6hdr *)(skb_in->data + mac_header_len);
255 if (ipv6_in->version != 6 || ipv6_in->nexthdr != IPPROTO_ICMPV6)
256 return -EPROTONOSUPPORT;
258 /* Check if this is NDP packet */
259 icmp6_in = (struct icmp6hdr *)(skb_in->data + mac_header_len +
260 sizeof(struct ipv6hdr));
261 if (icmp6_in->icmp6_type == NDISC_ROUTER_SOLICITATION) { /* Check RS */
262 return -EPROTONOSUPPORT;
263 } else if (icmp6_in->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) {
265 u8 icmp_na[sizeof(struct icmp6hdr) +
266 sizeof(struct neighbour_advertisement)];
267 u8 zero_addr8[16] = {0,};
269 if (memcmp(ipv6_in->saddr.in6_u.u6_addr8, zero_addr8, 16) == 0)
270 /* Duplicate Address Detection: Source IP is all zero */
273 icmp6_out.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
274 icmp6_out.icmp6_code = 0;
275 icmp6_out.icmp6_cksum = 0;
277 icmp6_out.icmp6_dataun.un_data32[0] = htonl(0x60000000);
279 ns = (struct neighbour_solicitation *)
280 (skb_in->data + mac_header_len +
281 sizeof(struct ipv6hdr) + sizeof(struct icmp6hdr));
282 memcpy(&na.target_address, ns->target_address, 16);
285 na.link_layer_address[0] = 0x00;
286 na.link_layer_address[1] = 0x0a;
287 na.link_layer_address[2] = 0x3b;
288 na.link_layer_address[3] = 0xaf;
289 na.link_layer_address[4] = 0x63;
290 na.link_layer_address[5] = 0xc7;
292 memcpy(&ipv6_out, ipv6_in, sizeof(struct ipv6hdr));
293 memcpy(ipv6_out.saddr.in6_u.u6_addr8, &na.target_address, 16);
294 memcpy(ipv6_out.daddr.in6_u.u6_addr8,
295 ipv6_in->saddr.in6_u.u6_addr8, 16);
296 ipv6_out.payload_len = htons(sizeof(struct icmp6hdr) +
297 sizeof(struct neighbour_advertisement));
299 memcpy(icmp_na, &icmp6_out, sizeof(struct icmp6hdr));
300 memcpy(icmp_na + sizeof(struct icmp6hdr), &na,
301 sizeof(struct neighbour_advertisement));
303 icmp6_out.icmp6_cksum = icmp6_checksum(&ipv6_out,
310 /* Fill the destination mac with source mac of the received packet */
311 memcpy(mac_header_data, mac_header_data + ETH_ALEN, ETH_ALEN);
312 /* Fill the source mac with nic's source mac */
313 memcpy(mac_header_data + ETH_ALEN, nic->src_mac_addr, ETH_ALEN);
315 /* Alloc skb and reserve align */
316 skb_out = dev_alloc_skb(skb_in->len);
319 skb_reserve(skb_out, NET_IP_ALIGN);
321 skb_put_data(skb_out, mac_header_data, mac_header_len);
322 skb_put_data(skb_out, &ipv6_out, sizeof(struct ipv6hdr));
323 skb_put_data(skb_out, &icmp6_out, sizeof(struct icmp6hdr));
324 skb_put_data(skb_out, &na, sizeof(struct neighbour_advertisement));
326 skb_out->protocol = ((struct ethhdr *)mac_header_data)->h_proto;
327 skb_out->dev = skb_in->dev;
328 skb_reset_mac_header(skb_out);
329 skb_pull(skb_out, ETH_HLEN);
331 gdm_lte_rx(skb_out, nic, nic_type);
336 static s32 gdm_lte_tx_nic_type(struct net_device *dev, struct sk_buff *skb)
338 struct nic *nic = netdev_priv(dev);
340 struct vlan_ethhdr *vlan_eth;
342 struct ipv6hdr *ipv6;
347 /* NIC TYPE is based on the nic_id of this net_device */
348 nic_type = 0x00000010 | nic->nic_id;
350 /* Get ethernet protocol */
351 eth = (struct ethhdr *)skb->data;
352 if (ntohs(eth->h_proto) == ETH_P_8021Q) {
353 vlan_eth = skb_vlan_eth_hdr(skb);
354 mac_proto = ntohs(vlan_eth->h_vlan_encapsulated_proto);
355 network_data = skb->data + VLAN_ETH_HLEN;
356 nic_type |= NIC_TYPE_F_VLAN;
358 mac_proto = ntohs(eth->h_proto);
359 network_data = skb->data + ETH_HLEN;
362 /* Process packet for nic type */
365 nic_type |= NIC_TYPE_ARP;
368 nic_type |= NIC_TYPE_F_IPV4;
372 if (ip->protocol == IPPROTO_UDP) {
374 network_data + sizeof(struct iphdr);
375 if (ntohs(udp->dest) == 67 || ntohs(udp->dest) == 68)
376 nic_type |= NIC_TYPE_F_DHCP;
380 nic_type |= NIC_TYPE_F_IPV6;
383 if (ipv6->nexthdr == IPPROTO_ICMPV6) /* Check NDP request */ {
384 struct icmp6hdr *icmp6 =
385 network_data + sizeof(struct ipv6hdr);
386 if (icmp6->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
387 nic_type |= NIC_TYPE_ICMPV6;
388 } else if (ipv6->nexthdr == IPPROTO_UDP) /* Check DHCPv6 */ {
390 network_data + sizeof(struct ipv6hdr);
391 if (ntohs(udp->dest) == 546 || ntohs(udp->dest) == 547)
392 nic_type |= NIC_TYPE_F_DHCP;
402 static netdev_tx_t gdm_lte_tx(struct sk_buff *skb, struct net_device *dev)
404 struct nic *nic = netdev_priv(dev);
411 nic_type = gdm_lte_tx_nic_type(dev, skb);
413 netdev_err(dev, "tx - invalid nic_type\n");
417 if (nic_type & NIC_TYPE_ARP) {
418 if (gdm_lte_emulate_arp(skb, nic_type) == 0) {
424 if (nic_type & NIC_TYPE_ICMPV6) {
425 if (gdm_lte_emulate_ndp(skb, nic_type) == 0) {
432 * Need byte shift (that is, remove VLAN tag) if there is one
433 * For the case of ARP, this breaks the offset as vlan_ethhdr+4
434 * is treated as ethhdr However, it shouldn't be a problem as
435 * the response starts from arp_hdr and ethhdr is created by this
436 * driver based on the NIC mac
438 if (nic_type & NIC_TYPE_F_VLAN) {
439 struct vlan_ethhdr *vlan_eth = skb_vlan_eth_hdr(skb);
441 nic->vlan_id = ntohs(vlan_eth->h_vlan_TCI) & VLAN_VID_MASK;
442 data_buf = skb->data + (VLAN_ETH_HLEN - ETH_HLEN);
443 data_len = skb->len - (VLAN_ETH_HLEN - ETH_HLEN);
446 data_buf = skb->data;
450 /* If it is a ICMPV6 packet, clear all the other bits :
451 * for backward compatibility with the firmware
453 if (nic_type & NIC_TYPE_ICMPV6)
454 nic_type = NIC_TYPE_ICMPV6;
456 /* If it is not a dhcp packet, clear all the flag bits :
457 * original NIC, otherwise the special flag (IPVX | DHCP)
459 if (!(nic_type & NIC_TYPE_F_DHCP))
460 nic_type &= NIC_TYPE_MASK;
462 ret = sscanf(dev->name, "lte%d", &idx);
468 ret = nic->phy_dev->send_sdu_func(nic->phy_dev->priv_dev,
470 nic->pdn_table.dft_eps_id, 0,
471 tx_complete, nic, idx,
474 if (ret == TX_NO_BUFFER || ret == TX_NO_SPC) {
475 netif_stop_queue(dev);
476 if (ret == TX_NO_BUFFER)
480 } else if (ret == TX_NO_DEV) {
484 /* Updates tx stats */
486 nic->stats.tx_dropped++;
488 nic->stats.tx_packets++;
489 nic->stats.tx_bytes += data_len;
496 static struct net_device_stats *gdm_lte_stats(struct net_device *dev)
498 struct nic *nic = netdev_priv(dev);
503 static int gdm_lte_event_send(struct net_device *dev, char *buf, int len)
505 struct phy_dev *phy_dev = ((struct nic *)netdev_priv(dev))->phy_dev;
506 struct hci_packet *hci = (struct hci_packet *)buf;
511 ret = sscanf(dev->name, "lte%d", &idx);
515 length = gdm_dev16_to_cpu(phy_dev->get_endian(phy_dev->priv_dev),
516 hci->len) + HCI_HEADER_SIZE;
517 return netlink_send(lte_event.sock, idx, 0, buf, length, dev);
520 static void gdm_lte_event_rcv(struct net_device *dev, u16 type,
523 struct nic *nic = netdev_priv(dev);
525 nic->phy_dev->send_hci_func(nic->phy_dev->priv_dev, msg, len, NULL,
529 int gdm_lte_event_init(void)
531 if (lte_event.ref_cnt == 0)
532 lte_event.sock = netlink_init(NETLINK_LTE, gdm_lte_event_rcv);
534 if (lte_event.sock) {
539 pr_err("event init failed\n");
543 void gdm_lte_event_exit(void)
545 if (lte_event.sock && --lte_event.ref_cnt == 0) {
546 sock_release(lte_event.sock->sk_socket);
547 lte_event.sock = NULL;
551 static int find_dev_index(u32 nic_type)
555 index = (u8)(nic_type & 0x0000000f);
556 if (index >= MAX_NIC_TYPE)
562 static void gdm_lte_netif_rx(struct net_device *dev, char *buf,
563 int len, int flagged_nic_type)
569 struct vlan_ethhdr vlan_eth;
570 void *mac_header_data;
574 nic_type = flagged_nic_type & NIC_TYPE_MASK;
575 nic = netdev_priv(dev);
577 if (flagged_nic_type & NIC_TYPE_F_DHCP) {
578 /* Change the destination mac address
579 * with the one requested the IP
581 if (flagged_nic_type & NIC_TYPE_F_IPV4) {
583 u8 op; /* BOOTREQUEST or BOOTREPLY */
584 u8 htype; /* hardware address type.
587 u8 hlen; /* hardware address length */
588 u8 hops; /* used by relay agents only */
589 u32 xid; /* unique id */
590 u16 secs; /* elapsed since client began
591 * acquisition/renewal
593 u16 flags; /* only one flag so far: */
594 #define BROADCAST_FLAG 0x8000
595 /* "I need broadcast replies" */
596 u32 ciaddr; /* client IP (if client is in
597 * BOUND, RENEW or REBINDING state)
599 u32 yiaddr; /* 'your' (client) IP address */
600 /* IP address of next server to use in
601 * bootstrap, returned in DHCPOFFER,
605 u32 gateway_nip; /* relay agent IP address */
606 u8 chaddr[16]; /* link-layer client hardware
609 u8 sname[64]; /* server host name (ASCIZ) */
610 u8 file[128]; /* boot file name (ASCIZ) */
611 u32 cookie; /* fixed first four option
612 * bytes (99,130,83,99 dec)
615 int offset = sizeof(struct iphdr) +
616 sizeof(struct udphdr) +
617 offsetof(struct dhcp_packet, chaddr);
618 if (offset + ETH_ALEN > len)
620 ether_addr_copy(nic->dest_mac_addr, buf + offset);
624 if (nic->vlan_id > 0) {
625 mac_header_data = (void *)&vlan_eth;
626 mac_header_len = VLAN_ETH_HLEN;
628 mac_header_data = (void *)ð
629 mac_header_len = ETH_HLEN;
632 /* Format the data so that it can be put to skb */
633 ether_addr_copy(mac_header_data, nic->dest_mac_addr);
634 memcpy(mac_header_data + ETH_ALEN, nic->src_mac_addr, ETH_ALEN);
636 vlan_eth.h_vlan_TCI = htons(nic->vlan_id);
637 vlan_eth.h_vlan_proto = htons(ETH_P_8021Q);
639 if (nic_type == NIC_TYPE_ARP) {
640 /* Should be response: Only happens because
641 * there was a request from the host
643 eth.h_proto = htons(ETH_P_ARP);
644 vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_ARP);
646 ip_version = buf[0] >> 4;
647 if (ip_version == IP_VERSION_4) {
648 eth.h_proto = htons(ETH_P_IP);
649 vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_IP);
650 } else if (ip_version == IP_VERSION_6) {
651 eth.h_proto = htons(ETH_P_IPV6);
652 vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_IPV6);
654 netdev_err(dev, "Unknown IP version %d\n", ip_version);
659 /* Alloc skb and reserve align */
660 skb = dev_alloc_skb(len + mac_header_len + NET_IP_ALIGN);
663 skb_reserve(skb, NET_IP_ALIGN);
665 skb_put_data(skb, mac_header_data, mac_header_len);
666 skb_put_data(skb, buf, len);
668 skb->protocol = ((struct ethhdr *)mac_header_data)->h_proto;
670 skb_reset_mac_header(skb);
671 skb_pull(skb, ETH_HLEN);
673 gdm_lte_rx(skb, nic, nic_type);
676 static void gdm_lte_multi_sdu_pkt(struct phy_dev *phy_dev, char *buf, int len)
678 struct net_device *dev;
679 struct multi_sdu *multi_sdu = (struct multi_sdu *)buf;
680 struct sdu *sdu = NULL;
681 u8 endian = phy_dev->get_endian(phy_dev->priv_dev);
682 u8 *data = (u8 *)multi_sdu->data;
691 hci_len = gdm_dev16_to_cpu(endian, multi_sdu->len);
692 num_packet = gdm_dev16_to_cpu(endian, multi_sdu->num_packet);
694 for (i = 0; i < num_packet; i++) {
695 copied = data - multi_sdu->data;
696 if (len < copied + sizeof(*sdu)) {
697 pr_err("rx prevent buffer overflow");
701 sdu = (struct sdu *)data;
703 cmd_evt = gdm_dev16_to_cpu(endian, sdu->cmd_evt);
704 hci_len = gdm_dev16_to_cpu(endian, sdu->len);
705 nic_type = gdm_dev32_to_cpu(endian, sdu->nic_type);
707 if (cmd_evt != LTE_RX_SDU) {
708 pr_err("rx sdu wrong hci %04x\n", cmd_evt);
712 len < copied + sizeof(*sdu) + (hci_len - 12)) {
713 pr_err("rx sdu invalid len %d\n", hci_len);
717 index = find_dev_index(nic_type);
719 pr_err("rx sdu invalid nic_type :%x\n", nic_type);
722 dev = phy_dev->dev[index];
723 gdm_lte_netif_rx(dev, (char *)sdu->data,
724 (int)(hci_len - 12), nic_type);
726 data += ((hci_len + 3) & 0xfffc) + HCI_HEADER_SIZE;
730 static void gdm_lte_pdn_table(struct net_device *dev, char *buf, int len)
732 struct nic *nic = netdev_priv(dev);
733 struct hci_pdn_table_ind *pdn_table = (struct hci_pdn_table_ind *)buf;
734 u8 ed = nic->phy_dev->get_endian(nic->phy_dev->priv_dev);
736 if (!pdn_table->activate) {
737 memset(&nic->pdn_table, 0x00, sizeof(struct pdn_table));
738 netdev_info(dev, "pdn deactivated\n");
743 nic->pdn_table.activate = pdn_table->activate;
744 nic->pdn_table.dft_eps_id = gdm_dev32_to_cpu(ed, pdn_table->dft_eps_id);
745 nic->pdn_table.nic_type = gdm_dev32_to_cpu(ed, pdn_table->nic_type);
747 netdev_info(dev, "pdn activated, nic_type=0x%x\n",
748 nic->pdn_table.nic_type);
751 static int gdm_lte_receive_pkt(struct phy_dev *phy_dev, char *buf, int len)
753 struct hci_packet *hci = (struct hci_packet *)buf;
754 struct hci_pdn_table_ind *pdn_table = (struct hci_pdn_table_ind *)buf;
756 struct net_device *dev;
757 u8 endian = phy_dev->get_endian(phy_dev->priv_dev);
766 cmd_evt = gdm_dev16_to_cpu(endian, hci->cmd_evt);
768 dev = phy_dev->dev[0];
774 sdu = (struct sdu *)hci->data;
775 nic_type = gdm_dev32_to_cpu(endian, sdu->nic_type);
776 index = find_dev_index(nic_type);
779 dev = phy_dev->dev[index];
780 gdm_lte_netif_rx(dev, hci->data, len, nic_type);
782 case LTE_RX_MULTI_SDU:
783 gdm_lte_multi_sdu_pkt(phy_dev, buf, len);
785 case LTE_LINK_ON_OFF_INDICATION:
786 netdev_info(dev, "link %s\n",
787 ((struct hci_connect_ind *)buf)->connect
790 case LTE_PDN_TABLE_IND:
791 pdn_table = (struct hci_pdn_table_ind *)buf;
792 nic_type = gdm_dev32_to_cpu(endian, pdn_table->nic_type);
793 index = find_dev_index(nic_type);
796 dev = phy_dev->dev[index];
797 gdm_lte_pdn_table(dev, buf, len);
800 ret = gdm_lte_event_send(dev, buf, len);
807 static int rx_complete(void *arg, void *data, int len, int context)
809 struct phy_dev *phy_dev = arg;
811 return gdm_lte_receive_pkt(phy_dev, data, len);
814 void start_rx_proc(struct phy_dev *phy_dev)
818 for (i = 0; i < MAX_RX_SUBMIT_COUNT; i++)
819 phy_dev->rcv_func(phy_dev->priv_dev,
820 rx_complete, phy_dev, USB_COMPLETE);
823 static const struct net_device_ops gdm_netdev_ops = {
824 .ndo_open = gdm_lte_open,
825 .ndo_stop = gdm_lte_close,
826 .ndo_set_config = gdm_lte_set_config,
827 .ndo_start_xmit = gdm_lte_tx,
828 .ndo_get_stats = gdm_lte_stats,
831 static u8 gdm_lte_macaddr[ETH_ALEN] = {0x00, 0x0a, 0x3b, 0x00, 0x00, 0x00};
833 static void form_mac_address(u8 *dev_addr, u8 *nic_src, u8 *nic_dest,
834 u8 *mac_address, u8 index)
836 /* Form the dev_addr */
838 ether_addr_copy(dev_addr, gdm_lte_macaddr);
840 ether_addr_copy(dev_addr, mac_address);
842 /* The last byte of the mac address
843 * should be less than or equal to 0xFC
845 dev_addr[ETH_ALEN - 1] += index;
847 /* Create random nic src and copy the first
848 * 3 bytes to be the same as dev_addr
850 eth_random_addr(nic_src);
851 memcpy(nic_src, dev_addr, 3);
853 /* Copy the nic_dest from dev_addr*/
854 ether_addr_copy(nic_dest, dev_addr);
857 static void validate_mac_address(u8 *mac_address)
859 /* if zero address or multicast bit set, restore the default value */
860 if (is_zero_ether_addr(mac_address) || (mac_address[0] & 0x01)) {
861 pr_err("MAC invalid, restoring default\n");
862 memcpy(mac_address, gdm_lte_macaddr, 6);
866 int register_lte_device(struct phy_dev *phy_dev,
867 struct device *dev, u8 *mac_address)
870 struct net_device *net;
871 char pdn_dev_name[16];
875 validate_mac_address(mac_address);
877 for (index = 0; index < MAX_NIC_TYPE; index++) {
878 /* Create device name lteXpdnX */
879 sprintf(pdn_dev_name, "lte%%dpdn%d", index);
881 /* Allocate netdev */
882 net = alloc_netdev(sizeof(struct nic), pdn_dev_name,
883 NET_NAME_UNKNOWN, ether_setup);
888 net->netdev_ops = &gdm_netdev_ops;
889 net->flags &= ~IFF_MULTICAST;
890 net->mtu = DEFAULT_MTU_SIZE;
892 nic = netdev_priv(net);
893 memset(nic, 0, sizeof(struct nic));
895 nic->phy_dev = phy_dev;
898 form_mac_address(net->dev_addr,
904 SET_NETDEV_DEV(net, dev);
905 SET_NETDEV_DEVTYPE(net, &wwan_type);
907 ret = register_netdev(net);
911 netif_carrier_on(net);
913 phy_dev->dev[index] = net;
919 unregister_lte_device(phy_dev);
924 void unregister_lte_device(struct phy_dev *phy_dev)
926 struct net_device *net;
929 for (index = 0; index < MAX_NIC_TYPE; index++) {
930 net = phy_dev->dev[index];
934 unregister_netdev(net);