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 IP_VERSION_4 4
48 #define IP_VERSION_6 6
55 static struct device_type wwan_type = {
59 static int gdm_lte_open(struct net_device *dev)
61 netif_start_queue(dev);
65 static int gdm_lte_close(struct net_device *dev)
67 netif_stop_queue(dev);
71 static int gdm_lte_set_config(struct net_device *dev, struct ifmap *map)
73 if (dev->flags & IFF_UP)
78 static void tx_complete(void *arg)
80 struct nic *nic = arg;
82 if (netif_queue_stopped(nic->netdev))
83 netif_wake_queue(nic->netdev);
86 static int gdm_lte_rx(struct sk_buff *skb, struct nic *nic, int nic_type)
90 len = skb->len + ETH_HLEN;
91 ret = netif_rx_ni(skb);
92 if (ret == NET_RX_DROP) {
93 nic->stats.rx_dropped++;
95 nic->stats.rx_packets++;
96 nic->stats.rx_bytes += len;
102 static int gdm_lte_emulate_arp(struct sk_buff *skb_in, u32 nic_type)
104 struct nic *nic = netdev_priv(skb_in->dev);
105 struct sk_buff *skb_out;
107 struct vlan_ethhdr vlan_eth;
108 struct arphdr *arp_in;
109 struct arphdr *arp_out;
116 struct arpdata *arp_data_in;
117 struct arpdata *arp_data_out;
119 void *mac_header_data;
122 /* Format the mac header so that it can be put to skb */
123 if (ntohs(((struct ethhdr *)skb_in->data)->h_proto) == ETH_P_8021Q) {
124 memcpy(&vlan_eth, skb_in->data, sizeof(struct vlan_ethhdr));
125 mac_header_data = &vlan_eth;
126 mac_header_len = VLAN_ETH_HLEN;
128 memcpy(ð, skb_in->data, sizeof(struct ethhdr));
129 mac_header_data = ð
130 mac_header_len = ETH_HLEN;
133 /* Get the pointer of the original request */
134 arp_in = (struct arphdr *)(skb_in->data + mac_header_len);
135 arp_data_in = (struct arpdata *)(skb_in->data + mac_header_len +
136 sizeof(struct arphdr));
138 /* Get the pointer of the outgoing response */
139 arp_out = (struct arphdr *)arp_temp;
140 arp_data_out = (struct arpdata *)(arp_temp + sizeof(struct arphdr));
142 /* Copy the arp header */
143 memcpy(arp_out, arp_in, sizeof(struct arphdr));
144 arp_out->ar_op = htons(ARPOP_REPLY);
146 /* Copy the arp payload: based on 2 bytes of mac and fill the IP */
147 arp_data_out->ar_sha[0] = arp_data_in->ar_sha[0];
148 arp_data_out->ar_sha[1] = arp_data_in->ar_sha[1];
149 memcpy(&arp_data_out->ar_sha[2], &arp_data_in->ar_tip[0], 4);
150 memcpy(&arp_data_out->ar_sip[0], &arp_data_in->ar_tip[0], 4);
151 memcpy(&arp_data_out->ar_tha[0], &arp_data_in->ar_sha[0], 6);
152 memcpy(&arp_data_out->ar_tip[0], &arp_data_in->ar_sip[0], 4);
154 /* Fill the destination mac with source mac of the received packet */
155 memcpy(mac_header_data, mac_header_data + ETH_ALEN, ETH_ALEN);
156 /* Fill the source mac with nic's source mac */
157 memcpy(mac_header_data + ETH_ALEN, nic->src_mac_addr, ETH_ALEN);
159 /* Alloc skb and reserve align */
160 skb_out = dev_alloc_skb(skb_in->len);
163 skb_reserve(skb_out, NET_IP_ALIGN);
165 skb_put_data(skb_out, mac_header_data, mac_header_len);
166 skb_put_data(skb_out, arp_out, sizeof(struct arphdr));
167 skb_put_data(skb_out, arp_data_out, sizeof(struct arpdata));
169 skb_out->protocol = ((struct ethhdr *)mac_header_data)->h_proto;
170 skb_out->dev = skb_in->dev;
171 skb_reset_mac_header(skb_out);
172 skb_pull(skb_out, ETH_HLEN);
174 gdm_lte_rx(skb_out, nic, nic_type);
179 static __sum16 icmp6_checksum(struct ipv6hdr *ipv6, u16 *ptr, int len)
181 unsigned short *w = ptr;
196 memset(&pseudo_header, 0, sizeof(pseudo_header));
197 memcpy(&pseudo_header.ph.ph_src, &ipv6->saddr.in6_u.u6_addr8, 16);
198 memcpy(&pseudo_header.ph.ph_dst, &ipv6->daddr.in6_u.u6_addr8, 16);
199 pseudo_header.ph.ph_len = be16_to_cpu(ipv6->payload_len);
200 pseudo_header.ph.ph_nxt = ipv6->nexthdr;
202 w = (u16 *)&pseudo_header;
203 for (i = 0; i < ARRAY_SIZE(pseudo_header.pa); i++)
204 sum = csum_add(sum, csum_unfold(
205 (__force __sum16)pseudo_header.pa[i]));
209 sum = csum_add(sum, csum_unfold((__force __sum16)*w++));
213 return csum_fold(sum);
216 static int gdm_lte_emulate_ndp(struct sk_buff *skb_in, u32 nic_type)
218 struct nic *nic = netdev_priv(skb_in->dev);
219 struct sk_buff *skb_out;
221 struct vlan_ethhdr vlan_eth;
222 struct neighbour_advertisement {
223 u8 target_address[16];
226 u8 link_layer_address[6];
228 struct neighbour_advertisement na;
229 struct neighbour_solicitation {
230 u8 target_address[16];
232 struct neighbour_solicitation *ns;
233 struct ipv6hdr *ipv6_in;
234 struct ipv6hdr ipv6_out;
235 struct icmp6hdr *icmp6_in;
236 struct icmp6hdr icmp6_out;
238 void *mac_header_data;
241 /* Format the mac header so that it can be put to skb */
242 if (ntohs(((struct ethhdr *)skb_in->data)->h_proto) == ETH_P_8021Q) {
243 memcpy(&vlan_eth, skb_in->data, sizeof(struct vlan_ethhdr));
244 if (ntohs(vlan_eth.h_vlan_encapsulated_proto) != ETH_P_IPV6)
246 mac_header_data = &vlan_eth;
247 mac_header_len = VLAN_ETH_HLEN;
249 memcpy(ð, skb_in->data, sizeof(struct ethhdr));
250 if (ntohs(eth.h_proto) != ETH_P_IPV6)
252 mac_header_data = ð
253 mac_header_len = ETH_HLEN;
256 /* Check if this is IPv6 ICMP packet */
257 ipv6_in = (struct ipv6hdr *)(skb_in->data + mac_header_len);
258 if (ipv6_in->version != 6 || ipv6_in->nexthdr != IPPROTO_ICMPV6)
261 /* Check if this is NDP packet */
262 icmp6_in = (struct icmp6hdr *)(skb_in->data + mac_header_len +
263 sizeof(struct ipv6hdr));
264 if (icmp6_in->icmp6_type == NDISC_ROUTER_SOLICITATION) { /* Check RS */
266 } else if (icmp6_in->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) {
268 u8 icmp_na[sizeof(struct icmp6hdr) +
269 sizeof(struct neighbour_advertisement)];
270 u8 zero_addr8[16] = {0,};
272 if (memcmp(ipv6_in->saddr.in6_u.u6_addr8, zero_addr8, 16) == 0)
273 /* Duplicate Address Detection: Source IP is all zero */
276 icmp6_out.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
277 icmp6_out.icmp6_code = 0;
278 icmp6_out.icmp6_cksum = 0;
280 icmp6_out.icmp6_dataun.un_data32[0] = htonl(0x60000000);
282 ns = (struct neighbour_solicitation *)
283 (skb_in->data + mac_header_len +
284 sizeof(struct ipv6hdr) + sizeof(struct icmp6hdr));
285 memcpy(&na.target_address, ns->target_address, 16);
288 na.link_layer_address[0] = 0x00;
289 na.link_layer_address[1] = 0x0a;
290 na.link_layer_address[2] = 0x3b;
291 na.link_layer_address[3] = 0xaf;
292 na.link_layer_address[4] = 0x63;
293 na.link_layer_address[5] = 0xc7;
295 memcpy(&ipv6_out, ipv6_in, sizeof(struct ipv6hdr));
296 memcpy(ipv6_out.saddr.in6_u.u6_addr8, &na.target_address, 16);
297 memcpy(ipv6_out.daddr.in6_u.u6_addr8,
298 ipv6_in->saddr.in6_u.u6_addr8, 16);
299 ipv6_out.payload_len = htons(sizeof(struct icmp6hdr) +
300 sizeof(struct neighbour_advertisement));
302 memcpy(icmp_na, &icmp6_out, sizeof(struct icmp6hdr));
303 memcpy(icmp_na + sizeof(struct icmp6hdr), &na,
304 sizeof(struct neighbour_advertisement));
306 icmp6_out.icmp6_cksum = icmp6_checksum(&ipv6_out,
307 (u16 *)icmp_na, sizeof(icmp_na));
312 /* Fill the destination mac with source mac of the received packet */
313 memcpy(mac_header_data, mac_header_data + ETH_ALEN, ETH_ALEN);
314 /* Fill the source mac with nic's source mac */
315 memcpy(mac_header_data + ETH_ALEN, nic->src_mac_addr, ETH_ALEN);
317 /* Alloc skb and reserve align */
318 skb_out = dev_alloc_skb(skb_in->len);
321 skb_reserve(skb_out, NET_IP_ALIGN);
323 skb_put_data(skb_out, mac_header_data, mac_header_len);
324 skb_put_data(skb_out, &ipv6_out, sizeof(struct ipv6hdr));
325 skb_put_data(skb_out, &icmp6_out, sizeof(struct icmp6hdr));
326 skb_put_data(skb_out, &na, sizeof(struct neighbour_advertisement));
328 skb_out->protocol = ((struct ethhdr *)mac_header_data)->h_proto;
329 skb_out->dev = skb_in->dev;
330 skb_reset_mac_header(skb_out);
331 skb_pull(skb_out, ETH_HLEN);
333 gdm_lte_rx(skb_out, nic, nic_type);
338 static s32 gdm_lte_tx_nic_type(struct net_device *dev, struct sk_buff *skb)
340 struct nic *nic = netdev_priv(dev);
342 struct vlan_ethhdr *vlan_eth;
344 struct ipv6hdr *ipv6;
349 /* NIC TYPE is based on the nic_id of this net_device */
350 nic_type = 0x00000010 | nic->nic_id;
352 /* Get ethernet protocol */
353 eth = (struct ethhdr *)skb->data;
354 if (ntohs(eth->h_proto) == ETH_P_8021Q) {
355 vlan_eth = (struct vlan_ethhdr *)skb->data;
356 mac_proto = ntohs(vlan_eth->h_vlan_encapsulated_proto);
357 network_data = skb->data + VLAN_ETH_HLEN;
358 nic_type |= NIC_TYPE_F_VLAN;
360 mac_proto = ntohs(eth->h_proto);
361 network_data = skb->data + ETH_HLEN;
364 /* Process packet for nic type */
367 nic_type |= NIC_TYPE_ARP;
370 nic_type |= NIC_TYPE_F_IPV4;
374 if (ip->protocol == IPPROTO_UDP) {
376 network_data + sizeof(struct iphdr);
377 if (ntohs(udp->dest) == 67 || ntohs(udp->dest) == 68)
378 nic_type |= NIC_TYPE_F_DHCP;
382 nic_type |= NIC_TYPE_F_IPV6;
385 if (ipv6->nexthdr == IPPROTO_ICMPV6) /* Check NDP request */ {
386 struct icmp6hdr *icmp6 =
387 network_data + sizeof(struct ipv6hdr);
388 if (icmp6->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
389 nic_type |= NIC_TYPE_ICMPV6;
390 } else if (ipv6->nexthdr == IPPROTO_UDP) /* Check DHCPv6 */ {
392 network_data + sizeof(struct ipv6hdr);
393 if (ntohs(udp->dest) == 546 || ntohs(udp->dest) == 547)
394 nic_type |= NIC_TYPE_F_DHCP;
404 static int gdm_lte_tx(struct sk_buff *skb, struct net_device *dev)
406 struct nic *nic = netdev_priv(dev);
413 nic_type = gdm_lte_tx_nic_type(dev, skb);
415 netdev_err(dev, "tx - invalid nic_type\n");
419 if (nic_type & NIC_TYPE_ARP) {
420 if (gdm_lte_emulate_arp(skb, nic_type) == 0) {
426 if (nic_type & NIC_TYPE_ICMPV6) {
427 if (gdm_lte_emulate_ndp(skb, nic_type) == 0) {
434 * Need byte shift (that is, remove VLAN tag) if there is one
435 * For the case of ARP, this breaks the offset as vlan_ethhdr+4
436 * is treated as ethhdr However, it shouldn't be a problem as
437 * the response starts from arp_hdr and ethhdr is created by this
438 * 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;
443 nic->vlan_id = ntohs(vlan_eth->h_vlan_TCI) & VLAN_VID_MASK;
444 data_buf = skb->data + (VLAN_ETH_HLEN - ETH_HLEN);
445 data_len = skb->len - (VLAN_ETH_HLEN - ETH_HLEN);
448 data_buf = skb->data;
452 /* If it is a ICMPV6 packet, clear all the other bits :
453 * for backward compatibility with the firmware
455 if (nic_type & NIC_TYPE_ICMPV6)
456 nic_type = NIC_TYPE_ICMPV6;
458 /* If it is not a dhcp packet, clear all the flag bits :
459 * original NIC, otherwise the special flag (IPVX | DHCP)
461 if (!(nic_type & NIC_TYPE_F_DHCP))
462 nic_type &= NIC_TYPE_MASK;
464 ret = sscanf(dev->name, "lte%d", &idx);
470 ret = nic->phy_dev->send_sdu_func(nic->phy_dev->priv_dev,
472 nic->pdn_table.dft_eps_id, 0,
473 tx_complete, nic, idx,
476 if (ret == TX_NO_BUFFER || ret == TX_NO_SPC) {
477 netif_stop_queue(dev);
478 if (ret == TX_NO_BUFFER)
482 } else if (ret == TX_NO_DEV) {
486 /* Updates tx stats */
488 nic->stats.tx_dropped++;
490 nic->stats.tx_packets++;
491 nic->stats.tx_bytes += data_len;
498 static struct net_device_stats *gdm_lte_stats(struct net_device *dev)
500 struct nic *nic = netdev_priv(dev);
505 static int gdm_lte_event_send(struct net_device *dev, char *buf, int len)
507 struct nic *nic = netdev_priv(dev);
508 struct hci_packet *hci = (struct hci_packet *)buf;
512 ret = sscanf(dev->name, "lte%d", &idx);
516 return netlink_send(lte_event.sock, idx, 0, buf,
518 nic->phy_dev->get_endian(
519 nic->phy_dev->priv_dev), hci->len)
523 static void gdm_lte_event_rcv(struct net_device *dev, u16 type,
526 struct nic *nic = netdev_priv(dev);
528 nic->phy_dev->send_hci_func(nic->phy_dev->priv_dev, msg, len, NULL,
532 int gdm_lte_event_init(void)
534 if (lte_event.ref_cnt == 0)
535 lte_event.sock = netlink_init(NETLINK_LTE, gdm_lte_event_rcv);
537 if (lte_event.sock) {
542 pr_err("event init failed\n");
546 void gdm_lte_event_exit(void)
548 if (lte_event.sock && --lte_event.ref_cnt == 0) {
549 sock_release(lte_event.sock->sk_socket);
550 lte_event.sock = NULL;
554 static int find_dev_index(u32 nic_type)
558 index = (u8)(nic_type & 0x0000000f);
559 if (index >= MAX_NIC_TYPE)
565 static void gdm_lte_netif_rx(struct net_device *dev, char *buf,
566 int len, int flagged_nic_type)
572 struct vlan_ethhdr vlan_eth;
573 void *mac_header_data;
577 nic_type = flagged_nic_type & NIC_TYPE_MASK;
578 nic = netdev_priv(dev);
580 if (flagged_nic_type & NIC_TYPE_F_DHCP) {
581 /* Change the destination mac address
582 * with the one requested the IP
584 if (flagged_nic_type & NIC_TYPE_F_IPV4) {
586 u8 op; /* BOOTREQUEST or BOOTREPLY */
587 u8 htype; /* hardware address type.
590 u8 hlen; /* hardware address length */
591 u8 hops; /* used by relay agents only */
592 u32 xid; /* unique id */
593 u16 secs; /* elapsed since client began
594 * acquisition/renewal
596 u16 flags; /* only one flag so far: */
597 #define BROADCAST_FLAG 0x8000
598 /* "I need broadcast replies" */
599 u32 ciaddr; /* client IP (if client is in
600 * BOUND, RENEW or REBINDING state)
602 u32 yiaddr; /* 'your' (client) IP address */
603 /* IP address of next server to use in
604 * bootstrap, returned in DHCPOFFER,
608 u32 gateway_nip; /* relay agent IP address */
609 u8 chaddr[16]; /* link-layer client hardware
612 u8 sname[64]; /* server host name (ASCIZ) */
613 u8 file[128]; /* boot file name (ASCIZ) */
614 u32 cookie; /* fixed first four option
615 * bytes (99,130,83,99 dec)
618 int offset = sizeof(struct iphdr) +
619 sizeof(struct udphdr) +
620 offsetof(struct dhcp_packet, chaddr);
621 if (offset + ETH_ALEN > len)
623 ether_addr_copy(nic->dest_mac_addr, buf + offset);
627 if (nic->vlan_id > 0) {
628 mac_header_data = (void *)&vlan_eth;
629 mac_header_len = VLAN_ETH_HLEN;
631 mac_header_data = (void *)ð
632 mac_header_len = ETH_HLEN;
635 /* Format the data so that it can be put to skb */
636 ether_addr_copy(mac_header_data, nic->dest_mac_addr);
637 memcpy(mac_header_data + ETH_ALEN, nic->src_mac_addr, ETH_ALEN);
639 vlan_eth.h_vlan_TCI = htons(nic->vlan_id);
640 vlan_eth.h_vlan_proto = htons(ETH_P_8021Q);
642 if (nic_type == NIC_TYPE_ARP) {
643 /* Should be response: Only happens because
644 * there was a request from the host
646 eth.h_proto = htons(ETH_P_ARP);
647 vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_ARP);
649 ip_version = buf[0] >> 4;
650 if (ip_version == IP_VERSION_4) {
651 eth.h_proto = htons(ETH_P_IP);
652 vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_IP);
653 } else if (ip_version == IP_VERSION_6) {
654 eth.h_proto = htons(ETH_P_IPV6);
655 vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_IPV6);
657 netdev_err(dev, "Unknown IP version %d\n", ip_version);
662 /* Alloc skb and reserve align */
663 skb = dev_alloc_skb(len + mac_header_len + NET_IP_ALIGN);
666 skb_reserve(skb, NET_IP_ALIGN);
668 skb_put_data(skb, mac_header_data, mac_header_len);
669 skb_put_data(skb, buf, len);
671 skb->protocol = ((struct ethhdr *)mac_header_data)->h_proto;
673 skb_reset_mac_header(skb);
674 skb_pull(skb, ETH_HLEN);
676 gdm_lte_rx(skb, nic, nic_type);
679 static void gdm_lte_multi_sdu_pkt(struct phy_dev *phy_dev, char *buf, int len)
681 struct net_device *dev;
682 struct multi_sdu *multi_sdu = (struct multi_sdu *)buf;
683 struct sdu *sdu = NULL;
684 struct gdm_endian *endian = phy_dev->get_endian(phy_dev->priv_dev);
685 u8 *data = (u8 *)multi_sdu->data;
694 hci_len = gdm_dev16_to_cpu(endian, multi_sdu->len);
695 num_packet = gdm_dev16_to_cpu(endian, multi_sdu->num_packet);
697 for (i = 0; i < num_packet; i++) {
698 copied = data - multi_sdu->data;
699 if (len < copied + sizeof(*sdu)) {
700 pr_err("rx prevent buffer overflow");
704 sdu = (struct sdu *)data;
706 cmd_evt = gdm_dev16_to_cpu(endian, sdu->cmd_evt);
707 hci_len = gdm_dev16_to_cpu(endian, sdu->len);
708 nic_type = gdm_dev32_to_cpu(endian, sdu->nic_type);
710 if (cmd_evt != LTE_RX_SDU) {
711 pr_err("rx sdu wrong hci %04x\n", cmd_evt);
715 len < copied + sizeof(*sdu) + (hci_len - 12)) {
716 pr_err("rx sdu invalid len %d\n", hci_len);
720 index = find_dev_index(nic_type);
722 pr_err("rx sdu invalid nic_type :%x\n", nic_type);
725 dev = phy_dev->dev[index];
726 gdm_lte_netif_rx(dev, (char *)sdu->data,
727 (int)(hci_len - 12), nic_type);
729 data += ((hci_len + 3) & 0xfffc) + HCI_HEADER_SIZE;
733 static void gdm_lte_pdn_table(struct net_device *dev, char *buf, int len)
735 struct nic *nic = netdev_priv(dev);
736 struct hci_pdn_table_ind *pdn_table = (struct hci_pdn_table_ind *)buf;
738 if (pdn_table->activate) {
739 nic->pdn_table.activate = pdn_table->activate;
740 nic->pdn_table.dft_eps_id = gdm_dev32_to_cpu(
741 nic->phy_dev->get_endian(
742 nic->phy_dev->priv_dev),
743 pdn_table->dft_eps_id);
744 nic->pdn_table.nic_type = gdm_dev32_to_cpu(
745 nic->phy_dev->get_endian(
746 nic->phy_dev->priv_dev),
747 pdn_table->nic_type);
749 netdev_info(dev, "pdn activated, nic_type=0x%x\n",
750 nic->pdn_table.nic_type);
752 memset(&nic->pdn_table, 0x00, sizeof(struct pdn_table));
753 netdev_info(dev, "pdn deactivated\n");
757 static int gdm_lte_receive_pkt(struct phy_dev *phy_dev, char *buf, int len)
759 struct hci_packet *hci = (struct hci_packet *)buf;
760 struct hci_pdn_table_ind *pdn_table = (struct hci_pdn_table_ind *)buf;
761 struct gdm_endian *endian = phy_dev->get_endian(phy_dev->priv_dev);
763 struct net_device *dev;
772 cmd_evt = gdm_dev16_to_cpu(endian, hci->cmd_evt);
774 dev = phy_dev->dev[0];
780 sdu = (struct sdu *)hci->data;
781 nic_type = gdm_dev32_to_cpu(endian, sdu->nic_type);
782 index = find_dev_index(nic_type);
785 dev = phy_dev->dev[index];
786 gdm_lte_netif_rx(dev, hci->data, len, nic_type);
788 case LTE_RX_MULTI_SDU:
789 gdm_lte_multi_sdu_pkt(phy_dev, buf, len);
791 case LTE_LINK_ON_OFF_INDICATION:
792 netdev_info(dev, "link %s\n",
793 ((struct hci_connect_ind *)buf)->connect
796 case LTE_PDN_TABLE_IND:
797 pdn_table = (struct hci_pdn_table_ind *)buf;
798 nic_type = gdm_dev32_to_cpu(endian, pdn_table->nic_type);
799 index = find_dev_index(nic_type);
802 dev = phy_dev->dev[index];
803 gdm_lte_pdn_table(dev, buf, len);
806 ret = gdm_lte_event_send(dev, buf, len);
813 static int rx_complete(void *arg, void *data, int len, int context)
815 struct phy_dev *phy_dev = arg;
817 return gdm_lte_receive_pkt(phy_dev, data, len);
820 void start_rx_proc(struct phy_dev *phy_dev)
824 for (i = 0; i < MAX_RX_SUBMIT_COUNT; i++)
825 phy_dev->rcv_func(phy_dev->priv_dev,
826 rx_complete, phy_dev, USB_COMPLETE);
829 static const struct net_device_ops gdm_netdev_ops = {
830 .ndo_open = gdm_lte_open,
831 .ndo_stop = gdm_lte_close,
832 .ndo_set_config = gdm_lte_set_config,
833 .ndo_start_xmit = gdm_lte_tx,
834 .ndo_get_stats = gdm_lte_stats,
837 static u8 gdm_lte_macaddr[ETH_ALEN] = {0x00, 0x0a, 0x3b, 0x00, 0x00, 0x00};
839 static void form_mac_address(u8 *dev_addr, u8 *nic_src, u8 *nic_dest,
840 u8 *mac_address, u8 index)
842 /* Form the dev_addr */
844 ether_addr_copy(dev_addr, gdm_lte_macaddr);
846 ether_addr_copy(dev_addr, mac_address);
848 /* The last byte of the mac address
849 * should be less than or equal to 0xFC
851 dev_addr[ETH_ALEN - 1] += index;
853 /* Create random nic src and copy the first
854 * 3 bytes to be the same as dev_addr
856 eth_random_addr(nic_src);
857 memcpy(nic_src, dev_addr, 3);
859 /* Copy the nic_dest from dev_addr*/
860 ether_addr_copy(nic_dest, dev_addr);
863 static void validate_mac_address(u8 *mac_address)
865 /* if zero address or multicast bit set, restore the default value */
866 if (is_zero_ether_addr(mac_address) || (mac_address[0] & 0x01)) {
867 pr_err("MAC invalid, restoring default\n");
868 memcpy(mac_address, gdm_lte_macaddr, 6);
872 int register_lte_device(struct phy_dev *phy_dev,
873 struct device *dev, u8 *mac_address)
876 struct net_device *net;
877 char pdn_dev_name[16];
881 validate_mac_address(mac_address);
883 for (index = 0; index < MAX_NIC_TYPE; index++) {
884 /* Create device name lteXpdnX */
885 sprintf(pdn_dev_name, "lte%%dpdn%d", index);
887 /* Allocate netdev */
888 net = alloc_netdev(sizeof(struct nic), pdn_dev_name,
889 NET_NAME_UNKNOWN, ether_setup);
891 pr_err("alloc_netdev failed\n");
895 net->netdev_ops = &gdm_netdev_ops;
896 net->flags &= ~IFF_MULTICAST;
897 net->mtu = DEFAULT_MTU_SIZE;
899 nic = netdev_priv(net);
900 memset(nic, 0, sizeof(struct nic));
902 nic->phy_dev = phy_dev;
912 SET_NETDEV_DEV(net, dev);
913 SET_NETDEV_DEVTYPE(net, &wwan_type);
915 ret = register_netdev(net);
919 netif_carrier_on(net);
921 phy_dev->dev[index] = net;
927 unregister_lte_device(phy_dev);
932 void unregister_lte_device(struct phy_dev *phy_dev)
934 struct net_device *net;
937 for (index = 0; index < MAX_NIC_TYPE; index++) {
938 net = phy_dev->dev[index];
942 unregister_netdev(net);