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 ret = netif_rx_ni(skb);
91 if (ret == NET_RX_DROP) {
92 nic->stats.rx_dropped++;
94 nic->stats.rx_packets++;
95 nic->stats.rx_bytes += skb->len + ETH_HLEN;
101 static int gdm_lte_emulate_arp(struct sk_buff *skb_in, u32 nic_type)
103 struct nic *nic = netdev_priv(skb_in->dev);
104 struct sk_buff *skb_out;
106 struct vlan_ethhdr vlan_eth;
107 struct arphdr *arp_in;
108 struct arphdr *arp_out;
115 struct arpdata *arp_data_in;
116 struct arpdata *arp_data_out;
118 void *mac_header_data;
121 /* Format the mac header so that it can be put to skb */
122 if (ntohs(((struct ethhdr *)skb_in->data)->h_proto) == ETH_P_8021Q) {
123 memcpy(&vlan_eth, skb_in->data, sizeof(struct vlan_ethhdr));
124 mac_header_data = &vlan_eth;
125 mac_header_len = VLAN_ETH_HLEN;
127 memcpy(ð, skb_in->data, sizeof(struct ethhdr));
128 mac_header_data = ð
129 mac_header_len = ETH_HLEN;
132 /* Get the pointer of the original request */
133 arp_in = (struct arphdr *)(skb_in->data + mac_header_len);
134 arp_data_in = (struct arpdata *)(skb_in->data + mac_header_len +
135 sizeof(struct arphdr));
137 /* Get the pointer of the outgoing response */
138 arp_out = (struct arphdr *)arp_temp;
139 arp_data_out = (struct arpdata *)(arp_temp + sizeof(struct arphdr));
141 /* Copy the arp header */
142 memcpy(arp_out, arp_in, sizeof(struct arphdr));
143 arp_out->ar_op = htons(ARPOP_REPLY);
145 /* Copy the arp payload: based on 2 bytes of mac and fill the IP */
146 arp_data_out->ar_sha[0] = arp_data_in->ar_sha[0];
147 arp_data_out->ar_sha[1] = arp_data_in->ar_sha[1];
148 memcpy(&arp_data_out->ar_sha[2], &arp_data_in->ar_tip[0], 4);
149 memcpy(&arp_data_out->ar_sip[0], &arp_data_in->ar_tip[0], 4);
150 memcpy(&arp_data_out->ar_tha[0], &arp_data_in->ar_sha[0], 6);
151 memcpy(&arp_data_out->ar_tip[0], &arp_data_in->ar_sip[0], 4);
153 /* Fill the destination mac with source mac of the received packet */
154 memcpy(mac_header_data, mac_header_data + ETH_ALEN, ETH_ALEN);
155 /* Fill the source mac with nic's source mac */
156 memcpy(mac_header_data + ETH_ALEN, nic->src_mac_addr, ETH_ALEN);
158 /* Alloc skb and reserve align */
159 skb_out = dev_alloc_skb(skb_in->len);
162 skb_reserve(skb_out, NET_IP_ALIGN);
164 skb_put_data(skb_out, mac_header_data, mac_header_len);
165 skb_put_data(skb_out, arp_out, sizeof(struct arphdr));
166 skb_put_data(skb_out, arp_data_out, sizeof(struct arpdata));
168 skb_out->protocol = ((struct ethhdr *)mac_header_data)->h_proto;
169 skb_out->dev = skb_in->dev;
170 skb_reset_mac_header(skb_out);
171 skb_pull(skb_out, ETH_HLEN);
173 gdm_lte_rx(skb_out, nic, nic_type);
178 static __sum16 icmp6_checksum(struct ipv6hdr *ipv6, u16 *ptr, int len)
180 unsigned short *w = ptr;
195 memset(&pseudo_header, 0, sizeof(pseudo_header));
196 memcpy(&pseudo_header.ph.ph_src, &ipv6->saddr.in6_u.u6_addr8, 16);
197 memcpy(&pseudo_header.ph.ph_dst, &ipv6->daddr.in6_u.u6_addr8, 16);
198 pseudo_header.ph.ph_len = be16_to_cpu(ipv6->payload_len);
199 pseudo_header.ph.ph_nxt = ipv6->nexthdr;
201 w = (u16 *)&pseudo_header;
202 for (i = 0; i < ARRAY_SIZE(pseudo_header.pa); i++)
203 sum = csum_add(sum, csum_unfold(
204 (__force __sum16)pseudo_header.pa[i]));
208 sum = csum_add(sum, csum_unfold((__force __sum16)*w++));
212 return csum_fold(sum);
215 static int gdm_lte_emulate_ndp(struct sk_buff *skb_in, u32 nic_type)
217 struct nic *nic = netdev_priv(skb_in->dev);
218 struct sk_buff *skb_out;
220 struct vlan_ethhdr vlan_eth;
221 struct neighbour_advertisement {
222 u8 target_address[16];
225 u8 link_layer_address[6];
227 struct neighbour_advertisement na;
228 struct neighbour_solicitation {
229 u8 target_address[16];
231 struct neighbour_solicitation *ns;
232 struct ipv6hdr *ipv6_in;
233 struct ipv6hdr ipv6_out;
234 struct icmp6hdr *icmp6_in;
235 struct icmp6hdr icmp6_out;
237 void *mac_header_data;
240 /* Format the mac header so that it can be put to skb */
241 if (ntohs(((struct ethhdr *)skb_in->data)->h_proto) == ETH_P_8021Q) {
242 memcpy(&vlan_eth, skb_in->data, sizeof(struct vlan_ethhdr));
243 if (ntohs(vlan_eth.h_vlan_encapsulated_proto) != ETH_P_IPV6)
245 mac_header_data = &vlan_eth;
246 mac_header_len = VLAN_ETH_HLEN;
248 memcpy(ð, skb_in->data, sizeof(struct ethhdr));
249 if (ntohs(eth.h_proto) != ETH_P_IPV6)
251 mac_header_data = ð
252 mac_header_len = ETH_HLEN;
255 /* Check if this is IPv6 ICMP packet */
256 ipv6_in = (struct ipv6hdr *)(skb_in->data + mac_header_len);
257 if (ipv6_in->version != 6 || ipv6_in->nexthdr != IPPROTO_ICMPV6)
260 /* Check if this is NDP packet */
261 icmp6_in = (struct icmp6hdr *)(skb_in->data + mac_header_len +
262 sizeof(struct ipv6hdr));
263 if (icmp6_in->icmp6_type == NDISC_ROUTER_SOLICITATION) { /* Check RS */
265 } else if (icmp6_in->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) {
267 u8 icmp_na[sizeof(struct icmp6hdr) +
268 sizeof(struct neighbour_advertisement)];
269 u8 zero_addr8[16] = {0,};
271 if (memcmp(ipv6_in->saddr.in6_u.u6_addr8, zero_addr8, 16) == 0)
272 /* Duplicate Address Detection: Source IP is all zero */
275 icmp6_out.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
276 icmp6_out.icmp6_code = 0;
277 icmp6_out.icmp6_cksum = 0;
279 icmp6_out.icmp6_dataun.un_data32[0] = htonl(0x60000000);
281 ns = (struct neighbour_solicitation *)
282 (skb_in->data + mac_header_len +
283 sizeof(struct ipv6hdr) + sizeof(struct icmp6hdr));
284 memcpy(&na.target_address, ns->target_address, 16);
287 na.link_layer_address[0] = 0x00;
288 na.link_layer_address[1] = 0x0a;
289 na.link_layer_address[2] = 0x3b;
290 na.link_layer_address[3] = 0xaf;
291 na.link_layer_address[4] = 0x63;
292 na.link_layer_address[5] = 0xc7;
294 memcpy(&ipv6_out, ipv6_in, sizeof(struct ipv6hdr));
295 memcpy(ipv6_out.saddr.in6_u.u6_addr8, &na.target_address, 16);
296 memcpy(ipv6_out.daddr.in6_u.u6_addr8,
297 ipv6_in->saddr.in6_u.u6_addr8, 16);
298 ipv6_out.payload_len = htons(sizeof(struct icmp6hdr) +
299 sizeof(struct neighbour_advertisement));
301 memcpy(icmp_na, &icmp6_out, sizeof(struct icmp6hdr));
302 memcpy(icmp_na + sizeof(struct icmp6hdr), &na,
303 sizeof(struct neighbour_advertisement));
305 icmp6_out.icmp6_cksum = icmp6_checksum(&ipv6_out,
306 (u16 *)icmp_na, sizeof(icmp_na));
311 /* Fill the destination mac with source mac of the received packet */
312 memcpy(mac_header_data, mac_header_data + ETH_ALEN, ETH_ALEN);
313 /* Fill the source mac with nic's source mac */
314 memcpy(mac_header_data + ETH_ALEN, nic->src_mac_addr, ETH_ALEN);
316 /* Alloc skb and reserve align */
317 skb_out = dev_alloc_skb(skb_in->len);
320 skb_reserve(skb_out, NET_IP_ALIGN);
322 skb_put_data(skb_out, mac_header_data, mac_header_len);
323 skb_put_data(skb_out, &ipv6_out, sizeof(struct ipv6hdr));
324 skb_put_data(skb_out, &icmp6_out, sizeof(struct icmp6hdr));
325 skb_put_data(skb_out, &na, sizeof(struct neighbour_advertisement));
327 skb_out->protocol = ((struct ethhdr *)mac_header_data)->h_proto;
328 skb_out->dev = skb_in->dev;
329 skb_reset_mac_header(skb_out);
330 skb_pull(skb_out, ETH_HLEN);
332 gdm_lte_rx(skb_out, nic, nic_type);
337 static s32 gdm_lte_tx_nic_type(struct net_device *dev, struct sk_buff *skb)
339 struct nic *nic = netdev_priv(dev);
341 struct vlan_ethhdr *vlan_eth;
343 struct ipv6hdr *ipv6;
348 /* NIC TYPE is based on the nic_id of this net_device */
349 nic_type = 0x00000010 | nic->nic_id;
351 /* Get ethernet protocol */
352 eth = (struct ethhdr *)skb->data;
353 if (ntohs(eth->h_proto) == ETH_P_8021Q) {
354 vlan_eth = (struct vlan_ethhdr *)skb->data;
355 mac_proto = ntohs(vlan_eth->h_vlan_encapsulated_proto);
356 network_data = skb->data + VLAN_ETH_HLEN;
357 nic_type |= NIC_TYPE_F_VLAN;
359 mac_proto = ntohs(eth->h_proto);
360 network_data = skb->data + ETH_HLEN;
363 /* Process packet for nic type */
366 nic_type |= NIC_TYPE_ARP;
369 nic_type |= NIC_TYPE_F_IPV4;
373 if (ip->protocol == IPPROTO_UDP) {
375 network_data + sizeof(struct iphdr);
376 if (ntohs(udp->dest) == 67 || ntohs(udp->dest) == 68)
377 nic_type |= NIC_TYPE_F_DHCP;
381 nic_type |= NIC_TYPE_F_IPV6;
384 if (ipv6->nexthdr == IPPROTO_ICMPV6) /* Check NDP request */ {
385 struct icmp6hdr *icmp6 =
386 network_data + sizeof(struct ipv6hdr);
387 if (icmp6->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
388 nic_type |= NIC_TYPE_ICMPV6;
389 } else if (ipv6->nexthdr == IPPROTO_UDP) /* Check DHCPv6 */ {
391 network_data + sizeof(struct ipv6hdr);
392 if (ntohs(udp->dest) == 546 || ntohs(udp->dest) == 547)
393 nic_type |= NIC_TYPE_F_DHCP;
403 static int gdm_lte_tx(struct sk_buff *skb, struct net_device *dev)
405 struct nic *nic = netdev_priv(dev);
412 nic_type = gdm_lte_tx_nic_type(dev, skb);
414 netdev_err(dev, "tx - invalid nic_type\n");
418 if (nic_type & NIC_TYPE_ARP) {
419 if (gdm_lte_emulate_arp(skb, nic_type) == 0) {
425 if (nic_type & NIC_TYPE_ICMPV6) {
426 if (gdm_lte_emulate_ndp(skb, nic_type) == 0) {
433 * Need byte shift (that is, remove VLAN tag) if there is one
434 * For the case of ARP, this breaks the offset as vlan_ethhdr+4
435 * is treated as ethhdr However, it shouldn't be a problem as
436 * the response starts from arp_hdr and ethhdr is created by this
437 * driver based on the NIC mac
439 if (nic_type & NIC_TYPE_F_VLAN) {
440 struct vlan_ethhdr *vlan_eth = (struct vlan_ethhdr *)skb->data;
442 nic->vlan_id = ntohs(vlan_eth->h_vlan_TCI) & VLAN_VID_MASK;
443 data_buf = skb->data + (VLAN_ETH_HLEN - ETH_HLEN);
444 data_len = skb->len - (VLAN_ETH_HLEN - ETH_HLEN);
447 data_buf = skb->data;
451 /* If it is a ICMPV6 packet, clear all the other bits :
452 * for backward compatibility with the firmware
454 if (nic_type & NIC_TYPE_ICMPV6)
455 nic_type = NIC_TYPE_ICMPV6;
457 /* If it is not a dhcp packet, clear all the flag bits :
458 * original NIC, otherwise the special flag (IPVX | DHCP)
460 if (!(nic_type & NIC_TYPE_F_DHCP))
461 nic_type &= NIC_TYPE_MASK;
463 ret = sscanf(dev->name, "lte%d", &idx);
469 ret = nic->phy_dev->send_sdu_func(nic->phy_dev->priv_dev,
471 nic->pdn_table.dft_eps_id, 0,
472 tx_complete, nic, idx,
475 if (ret == TX_NO_BUFFER || ret == TX_NO_SPC) {
476 netif_stop_queue(dev);
477 if (ret == TX_NO_BUFFER)
481 } else if (ret == TX_NO_DEV) {
485 /* Updates tx stats */
487 nic->stats.tx_dropped++;
489 nic->stats.tx_packets++;
490 nic->stats.tx_bytes += data_len;
497 static struct net_device_stats *gdm_lte_stats(struct net_device *dev)
499 struct nic *nic = netdev_priv(dev);
504 static int gdm_lte_event_send(struct net_device *dev, char *buf, int len)
506 struct nic *nic = netdev_priv(dev);
507 struct hci_packet *hci = (struct hci_packet *)buf;
511 ret = sscanf(dev->name, "lte%d", &idx);
515 return netlink_send(lte_event.sock, idx, 0, buf,
517 nic->phy_dev->get_endian(
518 nic->phy_dev->priv_dev), hci->len)
522 static void gdm_lte_event_rcv(struct net_device *dev, u16 type,
525 struct nic *nic = netdev_priv(dev);
527 nic->phy_dev->send_hci_func(nic->phy_dev->priv_dev, msg, len, NULL,
531 int gdm_lte_event_init(void)
533 if (lte_event.ref_cnt == 0)
534 lte_event.sock = netlink_init(NETLINK_LTE, gdm_lte_event_rcv);
536 if (lte_event.sock) {
541 pr_err("event init failed\n");
545 void gdm_lte_event_exit(void)
547 if (lte_event.sock && --lte_event.ref_cnt == 0) {
548 sock_release(lte_event.sock->sk_socket);
549 lte_event.sock = NULL;
553 static int find_dev_index(u32 nic_type)
557 index = (u8)(nic_type & 0x0000000f);
558 if (index >= MAX_NIC_TYPE)
564 static void gdm_lte_netif_rx(struct net_device *dev, char *buf,
565 int len, int flagged_nic_type)
571 struct vlan_ethhdr vlan_eth;
572 void *mac_header_data;
576 nic_type = flagged_nic_type & NIC_TYPE_MASK;
577 nic = netdev_priv(dev);
579 if (flagged_nic_type & NIC_TYPE_F_DHCP) {
580 /* Change the destination mac address
581 * with the one requested the IP
583 if (flagged_nic_type & NIC_TYPE_F_IPV4) {
585 u8 op; /* BOOTREQUEST or BOOTREPLY */
586 u8 htype; /* hardware address type.
589 u8 hlen; /* hardware address length */
590 u8 hops; /* used by relay agents only */
591 u32 xid; /* unique id */
592 u16 secs; /* elapsed since client began
593 * acquisition/renewal
595 u16 flags; /* only one flag so far: */
596 #define BROADCAST_FLAG 0x8000
597 /* "I need broadcast replies" */
598 u32 ciaddr; /* client IP (if client is in
599 * BOUND, RENEW or REBINDING state)
601 u32 yiaddr; /* 'your' (client) IP address */
602 /* IP address of next server to use in
603 * bootstrap, returned in DHCPOFFER,
607 u32 gateway_nip; /* relay agent IP address */
608 u8 chaddr[16]; /* link-layer client hardware
611 u8 sname[64]; /* server host name (ASCIZ) */
612 u8 file[128]; /* boot file name (ASCIZ) */
613 u32 cookie; /* fixed first four option
614 * bytes (99,130,83,99 dec)
617 int offset = sizeof(struct iphdr) +
618 sizeof(struct udphdr) +
619 offsetof(struct dhcp_packet, chaddr);
620 if (offset + ETH_ALEN > len)
622 ether_addr_copy(nic->dest_mac_addr, buf + offset);
626 if (nic->vlan_id > 0) {
627 mac_header_data = (void *)&vlan_eth;
628 mac_header_len = VLAN_ETH_HLEN;
630 mac_header_data = (void *)ð
631 mac_header_len = ETH_HLEN;
634 /* Format the data so that it can be put to skb */
635 ether_addr_copy(mac_header_data, nic->dest_mac_addr);
636 memcpy(mac_header_data + ETH_ALEN, nic->src_mac_addr, ETH_ALEN);
638 vlan_eth.h_vlan_TCI = htons(nic->vlan_id);
639 vlan_eth.h_vlan_proto = htons(ETH_P_8021Q);
641 if (nic_type == NIC_TYPE_ARP) {
642 /* Should be response: Only happens because
643 * there was a request from the host
645 eth.h_proto = htons(ETH_P_ARP);
646 vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_ARP);
648 ip_version = buf[0] >> 4;
649 if (ip_version == IP_VERSION_4) {
650 eth.h_proto = htons(ETH_P_IP);
651 vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_IP);
652 } else if (ip_version == IP_VERSION_6) {
653 eth.h_proto = htons(ETH_P_IPV6);
654 vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_IPV6);
656 netdev_err(dev, "Unknown IP version %d\n", ip_version);
661 /* Alloc skb and reserve align */
662 skb = dev_alloc_skb(len + mac_header_len + NET_IP_ALIGN);
665 skb_reserve(skb, NET_IP_ALIGN);
667 skb_put_data(skb, mac_header_data, mac_header_len);
668 skb_put_data(skb, buf, len);
670 skb->protocol = ((struct ethhdr *)mac_header_data)->h_proto;
672 skb_reset_mac_header(skb);
673 skb_pull(skb, ETH_HLEN);
675 gdm_lte_rx(skb, nic, nic_type);
678 static void gdm_lte_multi_sdu_pkt(struct phy_dev *phy_dev, char *buf, int len)
680 struct net_device *dev;
681 struct multi_sdu *multi_sdu = (struct multi_sdu *)buf;
682 struct sdu *sdu = NULL;
683 struct gdm_endian *endian = phy_dev->get_endian(phy_dev->priv_dev);
684 u8 *data = (u8 *)multi_sdu->data;
693 hci_len = gdm_dev16_to_cpu(endian, multi_sdu->len);
694 num_packet = gdm_dev16_to_cpu(endian, multi_sdu->num_packet);
696 for (i = 0; i < num_packet; i++) {
697 copied = data - multi_sdu->data;
698 if (len < copied + sizeof(*sdu)) {
699 pr_err("rx prevent buffer overflow");
703 sdu = (struct sdu *)data;
705 cmd_evt = gdm_dev16_to_cpu(endian, sdu->cmd_evt);
706 hci_len = gdm_dev16_to_cpu(endian, sdu->len);
707 nic_type = gdm_dev32_to_cpu(endian, sdu->nic_type);
709 if (cmd_evt != LTE_RX_SDU) {
710 pr_err("rx sdu wrong hci %04x\n", cmd_evt);
714 len < copied + sizeof(*sdu) + (hci_len - 12)) {
715 pr_err("rx sdu invalid len %d\n", hci_len);
719 index = find_dev_index(nic_type);
721 pr_err("rx sdu invalid nic_type :%x\n", nic_type);
724 dev = phy_dev->dev[index];
725 gdm_lte_netif_rx(dev, (char *)sdu->data,
726 (int)(hci_len - 12), nic_type);
728 data += ((hci_len + 3) & 0xfffc) + HCI_HEADER_SIZE;
732 static void gdm_lte_pdn_table(struct net_device *dev, char *buf, int len)
734 struct nic *nic = netdev_priv(dev);
735 struct hci_pdn_table_ind *pdn_table = (struct hci_pdn_table_ind *)buf;
737 if (pdn_table->activate) {
738 nic->pdn_table.activate = pdn_table->activate;
739 nic->pdn_table.dft_eps_id = gdm_dev32_to_cpu(
740 nic->phy_dev->get_endian(
741 nic->phy_dev->priv_dev),
742 pdn_table->dft_eps_id);
743 nic->pdn_table.nic_type = gdm_dev32_to_cpu(
744 nic->phy_dev->get_endian(
745 nic->phy_dev->priv_dev),
746 pdn_table->nic_type);
748 netdev_info(dev, "pdn activated, nic_type=0x%x\n",
749 nic->pdn_table.nic_type);
751 memset(&nic->pdn_table, 0x00, sizeof(struct pdn_table));
752 netdev_info(dev, "pdn deactivated\n");
756 static int gdm_lte_receive_pkt(struct phy_dev *phy_dev, char *buf, int len)
758 struct hci_packet *hci = (struct hci_packet *)buf;
759 struct hci_pdn_table_ind *pdn_table = (struct hci_pdn_table_ind *)buf;
760 struct gdm_endian *endian = phy_dev->get_endian(phy_dev->priv_dev);
762 struct net_device *dev;
771 cmd_evt = gdm_dev16_to_cpu(endian, hci->cmd_evt);
773 dev = phy_dev->dev[0];
779 sdu = (struct sdu *)hci->data;
780 nic_type = gdm_dev32_to_cpu(endian, sdu->nic_type);
781 index = find_dev_index(nic_type);
784 dev = phy_dev->dev[index];
785 gdm_lte_netif_rx(dev, hci->data, len, nic_type);
787 case LTE_RX_MULTI_SDU:
788 gdm_lte_multi_sdu_pkt(phy_dev, buf, len);
790 case LTE_LINK_ON_OFF_INDICATION:
791 netdev_info(dev, "link %s\n",
792 ((struct hci_connect_ind *)buf)->connect
795 case LTE_PDN_TABLE_IND:
796 pdn_table = (struct hci_pdn_table_ind *)buf;
797 nic_type = gdm_dev32_to_cpu(endian, pdn_table->nic_type);
798 index = find_dev_index(nic_type);
801 dev = phy_dev->dev[index];
802 gdm_lte_pdn_table(dev, buf, len);
805 ret = gdm_lte_event_send(dev, buf, len);
812 static int rx_complete(void *arg, void *data, int len, int context)
814 struct phy_dev *phy_dev = arg;
816 return gdm_lte_receive_pkt(phy_dev, data, len);
819 void start_rx_proc(struct phy_dev *phy_dev)
823 for (i = 0; i < MAX_RX_SUBMIT_COUNT; i++)
824 phy_dev->rcv_func(phy_dev->priv_dev,
825 rx_complete, phy_dev, USB_COMPLETE);
828 static const struct net_device_ops gdm_netdev_ops = {
829 .ndo_open = gdm_lte_open,
830 .ndo_stop = gdm_lte_close,
831 .ndo_set_config = gdm_lte_set_config,
832 .ndo_start_xmit = gdm_lte_tx,
833 .ndo_get_stats = gdm_lte_stats,
836 static u8 gdm_lte_macaddr[ETH_ALEN] = {0x00, 0x0a, 0x3b, 0x00, 0x00, 0x00};
838 static void form_mac_address(u8 *dev_addr, u8 *nic_src, u8 *nic_dest,
839 u8 *mac_address, u8 index)
841 /* Form the dev_addr */
843 ether_addr_copy(dev_addr, gdm_lte_macaddr);
845 ether_addr_copy(dev_addr, mac_address);
847 /* The last byte of the mac address
848 * should be less than or equal to 0xFC
850 dev_addr[ETH_ALEN - 1] += index;
852 /* Create random nic src and copy the first
853 * 3 bytes to be the same as dev_addr
855 eth_random_addr(nic_src);
856 memcpy(nic_src, dev_addr, 3);
858 /* Copy the nic_dest from dev_addr*/
859 ether_addr_copy(nic_dest, dev_addr);
862 static void validate_mac_address(u8 *mac_address)
864 /* if zero address or multicast bit set, restore the default value */
865 if (is_zero_ether_addr(mac_address) || (mac_address[0] & 0x01)) {
866 pr_err("MAC invalid, restoring default\n");
867 memcpy(mac_address, gdm_lte_macaddr, 6);
871 int register_lte_device(struct phy_dev *phy_dev,
872 struct device *dev, u8 *mac_address)
875 struct net_device *net;
876 char pdn_dev_name[16];
880 validate_mac_address(mac_address);
882 for (index = 0; index < MAX_NIC_TYPE; index++) {
883 /* Create device name lteXpdnX */
884 sprintf(pdn_dev_name, "lte%%dpdn%d", index);
886 /* Allocate netdev */
887 net = alloc_netdev(sizeof(struct nic), pdn_dev_name,
888 NET_NAME_UNKNOWN, ether_setup);
890 pr_err("alloc_netdev failed\n");
894 net->netdev_ops = &gdm_netdev_ops;
895 net->flags &= ~IFF_MULTICAST;
896 net->mtu = DEFAULT_MTU_SIZE;
898 nic = netdev_priv(net);
899 memset(nic, 0, sizeof(struct nic));
901 nic->phy_dev = phy_dev;
911 SET_NETDEV_DEV(net, dev);
912 SET_NETDEV_DEVTYPE(net, &wwan_type);
914 ret = register_netdev(net);
918 netif_carrier_on(net);
920 phy_dev->dev[index] = net;
926 unregister_lte_device(phy_dev);
931 void unregister_lte_device(struct phy_dev *phy_dev)
933 struct net_device *net;
936 for (index = 0; index < MAX_NIC_TYPE; index++) {
937 net = phy_dev->dev[index];
941 unregister_netdev(net);