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 memcpy(skb_put(skb_out, mac_header_len), mac_header_data,
167 memcpy(skb_put(skb_out, sizeof(struct arphdr)), arp_out,
168 sizeof(struct arphdr));
169 memcpy(skb_put(skb_out, sizeof(struct arpdata)), arp_data_out,
170 sizeof(struct arpdata));
172 skb_out->protocol = ((struct ethhdr *)mac_header_data)->h_proto;
173 skb_out->dev = skb_in->dev;
174 skb_reset_mac_header(skb_out);
175 skb_pull(skb_out, ETH_HLEN);
177 gdm_lte_rx(skb_out, nic, nic_type);
182 static int icmp6_checksum(struct ipv6hdr *ipv6, u16 *ptr, int len)
184 unsigned short *w = ptr;
199 memset(&pseudo_header, 0, sizeof(pseudo_header));
200 memcpy(&pseudo_header.ph.ph_src, &ipv6->saddr.in6_u.u6_addr8, 16);
201 memcpy(&pseudo_header.ph.ph_dst, &ipv6->daddr.in6_u.u6_addr8, 16);
202 pseudo_header.ph.ph_len = ipv6->payload_len;
203 pseudo_header.ph.ph_nxt = ipv6->nexthdr;
205 w = (u16 *)&pseudo_header;
206 for (i = 0; i < ARRAY_SIZE(pseudo_header.pa); i++)
207 sum += pseudo_header.pa[i];
215 sum = (sum >> 16) + (sum & 0xFFFF);
222 static int gdm_lte_emulate_ndp(struct sk_buff *skb_in, u32 nic_type)
224 struct nic *nic = netdev_priv(skb_in->dev);
225 struct sk_buff *skb_out;
227 struct vlan_ethhdr vlan_eth;
228 struct neighbour_advertisement {
229 u8 target_address[16];
232 u8 link_layer_address[6];
234 struct neighbour_advertisement na;
235 struct neighbour_solicitation {
236 u8 target_address[16];
238 struct neighbour_solicitation *ns;
239 struct ipv6hdr *ipv6_in;
240 struct ipv6hdr ipv6_out;
241 struct icmp6hdr *icmp6_in;
242 struct icmp6hdr icmp6_out;
244 void *mac_header_data;
247 /* Format the mac header so that it can be put to skb */
248 if (ntohs(((struct ethhdr *)skb_in->data)->h_proto) == ETH_P_8021Q) {
249 memcpy(&vlan_eth, skb_in->data, sizeof(struct vlan_ethhdr));
250 if (ntohs(vlan_eth.h_vlan_encapsulated_proto) != ETH_P_IPV6)
252 mac_header_data = &vlan_eth;
253 mac_header_len = VLAN_ETH_HLEN;
255 memcpy(ð, skb_in->data, sizeof(struct ethhdr));
256 if (ntohs(eth.h_proto) != ETH_P_IPV6)
258 mac_header_data = ð
259 mac_header_len = ETH_HLEN;
262 /* Check if this is IPv6 ICMP packet */
263 ipv6_in = (struct ipv6hdr *)(skb_in->data + mac_header_len);
264 if (ipv6_in->version != 6 || ipv6_in->nexthdr != IPPROTO_ICMPV6)
267 /* Check if this is NDP packet */
268 icmp6_in = (struct icmp6hdr *)(skb_in->data + mac_header_len +
269 sizeof(struct ipv6hdr));
270 if (icmp6_in->icmp6_type == NDISC_ROUTER_SOLICITATION) { /* Check RS */
272 } else if (icmp6_in->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) {
274 u8 icmp_na[sizeof(struct icmp6hdr) +
275 sizeof(struct neighbour_advertisement)];
276 u8 zero_addr8[16] = {0,};
278 if (memcmp(ipv6_in->saddr.in6_u.u6_addr8, zero_addr8, 16) == 0)
279 /* Duplicate Address Detection: Source IP is all zero */
282 icmp6_out.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
283 icmp6_out.icmp6_code = 0;
284 icmp6_out.icmp6_cksum = 0;
286 icmp6_out.icmp6_dataun.un_data32[0] = htonl(0x60000000);
288 ns = (struct neighbour_solicitation *)
289 (skb_in->data + mac_header_len +
290 sizeof(struct ipv6hdr) + sizeof(struct icmp6hdr));
291 memcpy(&na.target_address, ns->target_address, 16);
294 na.link_layer_address[0] = 0x00;
295 na.link_layer_address[1] = 0x0a;
296 na.link_layer_address[2] = 0x3b;
297 na.link_layer_address[3] = 0xaf;
298 na.link_layer_address[4] = 0x63;
299 na.link_layer_address[5] = 0xc7;
301 memcpy(&ipv6_out, ipv6_in, sizeof(struct ipv6hdr));
302 memcpy(ipv6_out.saddr.in6_u.u6_addr8, &na.target_address, 16);
303 memcpy(ipv6_out.daddr.in6_u.u6_addr8,
304 ipv6_in->saddr.in6_u.u6_addr8, 16);
305 ipv6_out.payload_len = htons(sizeof(struct icmp6hdr) +
306 sizeof(struct neighbour_advertisement));
308 memcpy(icmp_na, &icmp6_out, sizeof(struct icmp6hdr));
309 memcpy(icmp_na + sizeof(struct icmp6hdr), &na,
310 sizeof(struct neighbour_advertisement));
312 icmp6_out.icmp6_cksum = icmp6_checksum(&ipv6_out,
313 (u16 *)icmp_na, sizeof(icmp_na));
318 /* Fill the destination mac with source mac of the received packet */
319 memcpy(mac_header_data, mac_header_data + ETH_ALEN, ETH_ALEN);
320 /* Fill the source mac with nic's source mac */
321 memcpy(mac_header_data + ETH_ALEN, nic->src_mac_addr, ETH_ALEN);
323 /* Alloc skb and reserve align */
324 skb_out = dev_alloc_skb(skb_in->len);
327 skb_reserve(skb_out, NET_IP_ALIGN);
329 memcpy(skb_put(skb_out, mac_header_len), mac_header_data,
331 memcpy(skb_put(skb_out, sizeof(struct ipv6hdr)), &ipv6_out,
332 sizeof(struct ipv6hdr));
333 memcpy(skb_put(skb_out, sizeof(struct icmp6hdr)), &icmp6_out,
334 sizeof(struct icmp6hdr));
335 memcpy(skb_put(skb_out, sizeof(struct neighbour_advertisement)), &na,
336 sizeof(struct neighbour_advertisement));
338 skb_out->protocol = ((struct ethhdr *)mac_header_data)->h_proto;
339 skb_out->dev = skb_in->dev;
340 skb_reset_mac_header(skb_out);
341 skb_pull(skb_out, ETH_HLEN);
343 gdm_lte_rx(skb_out, nic, nic_type);
348 static s32 gdm_lte_tx_nic_type(struct net_device *dev, struct sk_buff *skb)
350 struct nic *nic = netdev_priv(dev);
352 struct vlan_ethhdr *vlan_eth;
354 struct ipv6hdr *ipv6;
359 /* NIC TYPE is based on the nic_id of this net_device */
360 nic_type = 0x00000010 | nic->nic_id;
362 /* Get ethernet protocol */
363 eth = (struct ethhdr *)skb->data;
364 if (ntohs(eth->h_proto) == ETH_P_8021Q) {
365 vlan_eth = (struct vlan_ethhdr *)skb->data;
366 mac_proto = ntohs(vlan_eth->h_vlan_encapsulated_proto);
367 network_data = skb->data + VLAN_ETH_HLEN;
368 nic_type |= NIC_TYPE_F_VLAN;
370 mac_proto = ntohs(eth->h_proto);
371 network_data = skb->data + ETH_HLEN;
374 /* Process packet for nic type */
377 nic_type |= NIC_TYPE_ARP;
380 nic_type |= NIC_TYPE_F_IPV4;
384 if (ip->protocol == IPPROTO_UDP) {
386 network_data + sizeof(struct iphdr);
387 if (ntohs(udp->dest) == 67 || ntohs(udp->dest) == 68)
388 nic_type |= NIC_TYPE_F_DHCP;
392 nic_type |= NIC_TYPE_F_IPV6;
395 if (ipv6->nexthdr == IPPROTO_ICMPV6) /* Check NDP request */ {
396 struct icmp6hdr *icmp6 =
397 network_data + sizeof(struct ipv6hdr);
398 if (icmp6->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
399 nic_type |= NIC_TYPE_ICMPV6;
400 } else if (ipv6->nexthdr == IPPROTO_UDP) /* Check DHCPv6 */ {
402 network_data + sizeof(struct ipv6hdr);
403 if (ntohs(udp->dest) == 546 || ntohs(udp->dest) == 547)
404 nic_type |= NIC_TYPE_F_DHCP;
414 static int gdm_lte_tx(struct sk_buff *skb, struct net_device *dev)
416 struct nic *nic = netdev_priv(dev);
423 nic_type = gdm_lte_tx_nic_type(dev, skb);
425 netdev_err(dev, "tx - invalid nic_type\n");
429 if (nic_type & NIC_TYPE_ARP) {
430 if (gdm_lte_emulate_arp(skb, nic_type) == 0) {
436 if (nic_type & NIC_TYPE_ICMPV6) {
437 if (gdm_lte_emulate_ndp(skb, nic_type) == 0) {
444 * Need byte shift (that is, remove VLAN tag) if there is one
445 * For the case of ARP, this breaks the offset as vlan_ethhdr+4
446 * is treated as ethhdr However, it shouldn't be a problem as
447 * the response starts from arp_hdr and ethhdr is created by this
448 * driver based on the NIC mac
450 if (nic_type & NIC_TYPE_F_VLAN) {
451 struct vlan_ethhdr *vlan_eth = (struct vlan_ethhdr *)skb->data;
453 nic->vlan_id = ntohs(vlan_eth->h_vlan_TCI) & VLAN_VID_MASK;
454 data_buf = skb->data + (VLAN_ETH_HLEN - ETH_HLEN);
455 data_len = skb->len - (VLAN_ETH_HLEN - ETH_HLEN);
458 data_buf = skb->data;
462 /* If it is a ICMPV6 packet, clear all the other bits :
463 * for backward compatibility with the firmware
465 if (nic_type & NIC_TYPE_ICMPV6)
466 nic_type = NIC_TYPE_ICMPV6;
468 /* If it is not a dhcp packet, clear all the flag bits :
469 * original NIC, otherwise the special flag (IPVX | DHCP)
471 if (!(nic_type & NIC_TYPE_F_DHCP))
472 nic_type &= NIC_TYPE_MASK;
474 ret = sscanf(dev->name, "lte%d", &idx);
480 ret = nic->phy_dev->send_sdu_func(nic->phy_dev->priv_dev,
482 nic->pdn_table.dft_eps_id, 0,
483 tx_complete, nic, idx,
486 if (ret == TX_NO_BUFFER || ret == TX_NO_SPC) {
487 netif_stop_queue(dev);
488 if (ret == TX_NO_BUFFER)
492 } else if (ret == TX_NO_DEV) {
496 /* Updates tx stats */
498 nic->stats.tx_dropped++;
500 nic->stats.tx_packets++;
501 nic->stats.tx_bytes += data_len;
508 static struct net_device_stats *gdm_lte_stats(struct net_device *dev)
510 struct nic *nic = netdev_priv(dev);
515 static int gdm_lte_event_send(struct net_device *dev, char *buf, int len)
517 struct nic *nic = netdev_priv(dev);
518 struct hci_packet *hci = (struct hci_packet *)buf;
522 ret = sscanf(dev->name, "lte%d", &idx);
526 return netlink_send(lte_event.sock, idx, 0, buf,
528 nic->phy_dev->get_endian(
529 nic->phy_dev->priv_dev), hci->len)
533 static void gdm_lte_event_rcv(struct net_device *dev, u16 type,
536 struct nic *nic = netdev_priv(dev);
538 nic->phy_dev->send_hci_func(nic->phy_dev->priv_dev, msg, len, NULL,
542 int gdm_lte_event_init(void)
544 if (lte_event.ref_cnt == 0)
545 lte_event.sock = netlink_init(NETLINK_LTE, gdm_lte_event_rcv);
547 if (lte_event.sock) {
552 pr_err("event init failed\n");
556 void gdm_lte_event_exit(void)
558 if (lte_event.sock && --lte_event.ref_cnt == 0) {
559 sock_release(lte_event.sock->sk_socket);
560 lte_event.sock = NULL;
564 static u8 find_dev_index(u32 nic_type)
568 index = (u8)(nic_type & 0x0000000f);
569 if (index > MAX_NIC_TYPE)
575 static void gdm_lte_netif_rx(struct net_device *dev, char *buf,
576 int len, int flagged_nic_type)
582 struct vlan_ethhdr vlan_eth;
583 void *mac_header_data;
587 nic_type = flagged_nic_type & NIC_TYPE_MASK;
588 nic = netdev_priv(dev);
590 if (flagged_nic_type & NIC_TYPE_F_DHCP) {
591 /* Change the destination mac address
592 * with the one requested the IP
594 if (flagged_nic_type & NIC_TYPE_F_IPV4) {
596 u8 op; /* BOOTREQUEST or BOOTREPLY */
597 u8 htype; /* hardware address type.
600 u8 hlen; /* hardware address length */
601 u8 hops; /* used by relay agents only */
602 u32 xid; /* unique id */
603 u16 secs; /* elapsed since client began
604 * acquisition/renewal
606 u16 flags; /* only one flag so far: */
607 #define BROADCAST_FLAG 0x8000
608 /* "I need broadcast replies" */
609 u32 ciaddr; /* client IP (if client is in
610 * BOUND, RENEW or REBINDING state)
612 u32 yiaddr; /* 'your' (client) IP address */
613 /* IP address of next server to use in
614 * bootstrap, returned in DHCPOFFER,
618 u32 gateway_nip; /* relay agent IP address */
619 u8 chaddr[16]; /* link-layer client hardware
622 u8 sname[64]; /* server host name (ASCIZ) */
623 u8 file[128]; /* boot file name (ASCIZ) */
624 u32 cookie; /* fixed first four option
625 * bytes (99,130,83,99 dec)
628 int offset = sizeof(struct iphdr) +
629 sizeof(struct udphdr) +
630 offsetof(struct dhcp_packet, chaddr);
631 if (offset + ETH_ALEN > len)
633 ether_addr_copy(nic->dest_mac_addr, buf + offset);
637 if (nic->vlan_id > 0) {
638 mac_header_data = (void *)&vlan_eth;
639 mac_header_len = VLAN_ETH_HLEN;
641 mac_header_data = (void *)ð
642 mac_header_len = ETH_HLEN;
645 /* Format the data so that it can be put to skb */
646 ether_addr_copy(mac_header_data, nic->dest_mac_addr);
647 memcpy(mac_header_data + ETH_ALEN, nic->src_mac_addr, ETH_ALEN);
649 vlan_eth.h_vlan_TCI = htons(nic->vlan_id);
650 vlan_eth.h_vlan_proto = htons(ETH_P_8021Q);
652 if (nic_type == NIC_TYPE_ARP) {
653 /* Should be response: Only happens because
654 * there was a request from the host
656 eth.h_proto = htons(ETH_P_ARP);
657 vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_ARP);
659 ip_version = buf[0] >> 4;
660 if (ip_version == IP_VERSION_4) {
661 eth.h_proto = htons(ETH_P_IP);
662 vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_IP);
663 } else if (ip_version == IP_VERSION_6) {
664 eth.h_proto = htons(ETH_P_IPV6);
665 vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_IPV6);
667 netdev_err(dev, "Unknown IP version %d\n", ip_version);
672 /* Alloc skb and reserve align */
673 skb = dev_alloc_skb(len + mac_header_len + NET_IP_ALIGN);
676 skb_reserve(skb, NET_IP_ALIGN);
678 memcpy(skb_put(skb, mac_header_len), mac_header_data, mac_header_len);
679 memcpy(skb_put(skb, len), buf, len);
681 skb->protocol = ((struct ethhdr *)mac_header_data)->h_proto;
683 skb_reset_mac_header(skb);
684 skb_pull(skb, ETH_HLEN);
686 gdm_lte_rx(skb, nic, nic_type);
689 static void gdm_lte_multi_sdu_pkt(struct phy_dev *phy_dev, char *buf, int len)
691 struct net_device *dev;
692 struct multi_sdu *multi_sdu = (struct multi_sdu *)buf;
693 struct sdu *sdu = NULL;
694 u8 *data = (u8 *)multi_sdu->data;
703 hci_len = gdm_dev16_to_cpu(phy_dev->get_endian(phy_dev->priv_dev),
705 num_packet = gdm_dev16_to_cpu(phy_dev->get_endian(phy_dev->priv_dev),
706 multi_sdu->num_packet);
708 for (i = 0; i < num_packet; i++) {
709 copied = data - multi_sdu->data;
710 if (len < copied + sizeof(*sdu)) {
711 pr_err("rx prevent buffer overflow");
715 sdu = (struct sdu *)data;
717 cmd_evt = gdm_dev16_to_cpu(phy_dev->
718 get_endian(phy_dev->priv_dev), sdu->cmd_evt);
719 hci_len = gdm_dev16_to_cpu(phy_dev->
720 get_endian(phy_dev->priv_dev), sdu->len);
721 nic_type = gdm_dev32_to_cpu(phy_dev->
722 get_endian(phy_dev->priv_dev), sdu->nic_type);
724 if (cmd_evt != LTE_RX_SDU) {
725 pr_err("rx sdu wrong hci %04x\n", cmd_evt);
729 len < copied + sizeof(*sdu) + (hci_len - 12)) {
730 pr_err("rx sdu invalid len %d\n", hci_len);
734 index = find_dev_index(nic_type);
735 if (index < MAX_NIC_TYPE) {
736 dev = phy_dev->dev[index];
737 gdm_lte_netif_rx(dev, (char *)sdu->data,
738 (int)(hci_len - 12), nic_type);
740 pr_err("rx sdu invalid nic_type :%x\n", nic_type);
743 data += ((hci_len + 3) & 0xfffc) + HCI_HEADER_SIZE;
747 static void gdm_lte_pdn_table(struct net_device *dev, char *buf, int len)
749 struct nic *nic = netdev_priv(dev);
750 struct hci_pdn_table_ind *pdn_table = (struct hci_pdn_table_ind *)buf;
752 if (pdn_table->activate) {
753 nic->pdn_table.activate = pdn_table->activate;
754 nic->pdn_table.dft_eps_id = gdm_dev32_to_cpu(
755 nic->phy_dev->get_endian(
756 nic->phy_dev->priv_dev),
757 pdn_table->dft_eps_id);
758 nic->pdn_table.nic_type = gdm_dev32_to_cpu(
759 nic->phy_dev->get_endian(
760 nic->phy_dev->priv_dev),
761 pdn_table->nic_type);
763 netdev_info(dev, "pdn activated, nic_type=0x%x\n",
764 nic->pdn_table.nic_type);
766 memset(&nic->pdn_table, 0x00, sizeof(struct pdn_table));
767 netdev_info(dev, "pdn deactivated\n");
771 static int gdm_lte_receive_pkt(struct phy_dev *phy_dev, char *buf, int len)
773 struct hci_packet *hci = (struct hci_packet *)buf;
774 struct hci_pdn_table_ind *pdn_table = (struct hci_pdn_table_ind *)buf;
776 struct net_device *dev;
785 cmd_evt = gdm_dev16_to_cpu(phy_dev->get_endian(phy_dev->priv_dev),
788 dev = phy_dev->dev[0];
794 sdu = (struct sdu *)hci->data;
795 nic_type = gdm_dev32_to_cpu(phy_dev->
796 get_endian(phy_dev->priv_dev), sdu->nic_type);
797 index = find_dev_index(nic_type);
798 dev = phy_dev->dev[index];
799 gdm_lte_netif_rx(dev, hci->data, len, nic_type);
801 case LTE_RX_MULTI_SDU:
802 gdm_lte_multi_sdu_pkt(phy_dev, buf, len);
804 case LTE_LINK_ON_OFF_INDICATION:
805 netdev_info(dev, "link %s\n",
806 ((struct hci_connect_ind *)buf)->connect
809 case LTE_PDN_TABLE_IND:
810 pdn_table = (struct hci_pdn_table_ind *)buf;
811 nic_type = gdm_dev32_to_cpu(phy_dev->
812 get_endian(phy_dev->priv_dev),
813 pdn_table->nic_type);
814 index = find_dev_index(nic_type);
815 dev = phy_dev->dev[index];
816 gdm_lte_pdn_table(dev, buf, len);
819 ret = gdm_lte_event_send(dev, buf, len);
826 static int rx_complete(void *arg, void *data, int len, int context)
828 struct phy_dev *phy_dev = arg;
830 return gdm_lte_receive_pkt(phy_dev, data, len);
833 void start_rx_proc(struct phy_dev *phy_dev)
837 for (i = 0; i < MAX_RX_SUBMIT_COUNT; i++)
838 phy_dev->rcv_func(phy_dev->priv_dev,
839 rx_complete, phy_dev, USB_COMPLETE);
842 static const struct net_device_ops gdm_netdev_ops = {
843 .ndo_open = gdm_lte_open,
844 .ndo_stop = gdm_lte_close,
845 .ndo_set_config = gdm_lte_set_config,
846 .ndo_start_xmit = gdm_lte_tx,
847 .ndo_get_stats = gdm_lte_stats,
850 static u8 gdm_lte_macaddr[ETH_ALEN] = {0x00, 0x0a, 0x3b, 0x00, 0x00, 0x00};
852 static void form_mac_address(u8 *dev_addr, u8 *nic_src, u8 *nic_dest,
853 u8 *mac_address, u8 index)
855 /* Form the dev_addr */
857 ether_addr_copy(dev_addr, gdm_lte_macaddr);
859 ether_addr_copy(dev_addr, mac_address);
861 /* The last byte of the mac address
862 * should be less than or equal to 0xFC
864 dev_addr[ETH_ALEN - 1] += index;
866 /* Create random nic src and copy the first
867 * 3 bytes to be the same as dev_addr
869 eth_random_addr(nic_src);
870 memcpy(nic_src, dev_addr, 3);
872 /* Copy the nic_dest from dev_addr*/
873 ether_addr_copy(nic_dest, dev_addr);
876 static void validate_mac_address(u8 *mac_address)
878 /* if zero address or multicast bit set, restore the default value */
879 if (is_zero_ether_addr(mac_address) || (mac_address[0] & 0x01)) {
880 pr_err("MAC invalid, restoring default\n");
881 memcpy(mac_address, gdm_lte_macaddr, 6);
885 int register_lte_device(struct phy_dev *phy_dev,
886 struct device *dev, u8 *mac_address)
889 struct net_device *net;
890 char pdn_dev_name[16];
894 validate_mac_address(mac_address);
896 for (index = 0; index < MAX_NIC_TYPE; index++) {
897 /* Create device name lteXpdnX */
898 sprintf(pdn_dev_name, "lte%%dpdn%d", index);
900 /* Allocate netdev */
901 net = alloc_netdev(sizeof(struct nic), pdn_dev_name,
902 NET_NAME_UNKNOWN, ether_setup);
904 pr_err("alloc_netdev failed\n");
908 net->netdev_ops = &gdm_netdev_ops;
909 net->flags &= ~IFF_MULTICAST;
910 net->mtu = DEFAULT_MTU_SIZE;
912 nic = netdev_priv(net);
913 memset(nic, 0, sizeof(struct nic));
915 nic->phy_dev = phy_dev;
925 SET_NETDEV_DEV(net, dev);
926 SET_NETDEV_DEVTYPE(net, &wwan_type);
928 ret = register_netdev(net);
932 netif_carrier_on(net);
934 phy_dev->dev[index] = net;
940 unregister_lte_device(phy_dev);
945 void unregister_lte_device(struct phy_dev *phy_dev)
947 struct net_device *net;
950 for (index = 0; index < MAX_NIC_TYPE; index++) {
951 net = phy_dev->dev[index];
955 unregister_netdev(net);