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 + sizeof(struct arphdr));
136 /* Get the pointer of the outgoing response */
137 arp_out = (struct arphdr *)arp_temp;
138 arp_data_out = (struct arpdata *)(arp_temp + sizeof(struct arphdr));
140 /* Copy the arp header */
141 memcpy(arp_out, arp_in, sizeof(struct arphdr));
142 arp_out->ar_op = htons(ARPOP_REPLY);
144 /* Copy the arp payload: based on 2 bytes of mac and fill the IP */
145 arp_data_out->ar_sha[0] = arp_data_in->ar_sha[0];
146 arp_data_out->ar_sha[1] = arp_data_in->ar_sha[1];
147 memcpy(&arp_data_out->ar_sha[2], &arp_data_in->ar_tip[0], 4);
148 memcpy(&arp_data_out->ar_sip[0], &arp_data_in->ar_tip[0], 4);
149 memcpy(&arp_data_out->ar_tha[0], &arp_data_in->ar_sha[0], 6);
150 memcpy(&arp_data_out->ar_tip[0], &arp_data_in->ar_sip[0], 4);
152 /* Fill the destination mac with source mac of the received packet */
153 memcpy(mac_header_data, mac_header_data + ETH_ALEN, ETH_ALEN);
154 /* Fill the source mac with nic's source mac */
155 memcpy(mac_header_data + ETH_ALEN, nic->src_mac_addr, ETH_ALEN);
157 /* Alloc skb and reserve align */
158 skb_out = dev_alloc_skb(skb_in->len);
161 skb_reserve(skb_out, NET_IP_ALIGN);
163 memcpy(skb_put(skb_out, mac_header_len), mac_header_data, mac_header_len);
164 memcpy(skb_put(skb_out, sizeof(struct arphdr)), arp_out, sizeof(struct arphdr));
165 memcpy(skb_put(skb_out, sizeof(struct arpdata)), arp_data_out, sizeof(struct arpdata));
167 skb_out->protocol = ((struct ethhdr *)mac_header_data)->h_proto;
168 skb_out->dev = skb_in->dev;
169 skb_reset_mac_header(skb_out);
170 skb_pull(skb_out, ETH_HLEN);
172 gdm_lte_rx(skb_out, nic, nic_type);
177 static int icmp6_checksum(struct ipv6hdr *ipv6, u16 *ptr, int len)
179 unsigned short *w = ptr;
194 memset(&pseudo_header, 0, sizeof(pseudo_header));
195 memcpy(&pseudo_header.ph.ph_src, &ipv6->saddr.in6_u.u6_addr8, 16);
196 memcpy(&pseudo_header.ph.ph_dst, &ipv6->daddr.in6_u.u6_addr8, 16);
197 pseudo_header.ph.ph_len = ipv6->payload_len;
198 pseudo_header.ph.ph_nxt = ipv6->nexthdr;
200 w = (u16 *)&pseudo_header;
201 for (i = 0; i < sizeof(pseudo_header.pa) / sizeof(pseudo_header.pa[0]); i++)
202 sum += pseudo_header.pa[i];
210 sum = (sum >> 16) + (sum & 0xFFFF);
217 static int gdm_lte_emulate_ndp(struct sk_buff *skb_in, u32 nic_type)
219 struct nic *nic = netdev_priv(skb_in->dev);
220 struct sk_buff *skb_out;
222 struct vlan_ethhdr vlan_eth;
223 struct neighbour_advertisement {
224 u8 target_address[16];
227 u8 link_layer_address[6];
229 struct neighbour_advertisement na;
230 struct neighbour_solicitation {
231 u8 target_address[16];
233 struct neighbour_solicitation *ns;
234 struct ipv6hdr *ipv6_in;
235 struct ipv6hdr ipv6_out;
236 struct icmp6hdr *icmp6_in;
237 struct icmp6hdr icmp6_out;
239 void *mac_header_data;
242 /* Format the mac header so that it can be put to skb */
243 if (ntohs(((struct ethhdr *)skb_in->data)->h_proto) == ETH_P_8021Q) {
244 memcpy(&vlan_eth, skb_in->data, sizeof(struct vlan_ethhdr));
245 if (ntohs(vlan_eth.h_vlan_encapsulated_proto) != ETH_P_IPV6)
247 mac_header_data = &vlan_eth;
248 mac_header_len = VLAN_ETH_HLEN;
250 memcpy(ð, skb_in->data, sizeof(struct ethhdr));
251 if (ntohs(eth.h_proto) != ETH_P_IPV6)
253 mac_header_data = ð
254 mac_header_len = ETH_HLEN;
257 /* Check if this is IPv6 ICMP packet */
258 ipv6_in = (struct ipv6hdr *)(skb_in->data + mac_header_len);
259 if (ipv6_in->version != 6 || ipv6_in->nexthdr != IPPROTO_ICMPV6)
262 /* Check if this is NDP packet */
263 icmp6_in = (struct icmp6hdr *)(skb_in->data + mac_header_len + sizeof(struct ipv6hdr));
264 if (icmp6_in->icmp6_type == NDISC_ROUTER_SOLICITATION) { /* Check RS */
266 } else if (icmp6_in->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) { /* Check NS */
267 u8 icmp_na[sizeof(struct icmp6hdr) + sizeof(struct neighbour_advertisement)];
268 u8 zero_addr8[16] = {0,};
270 if (memcmp(ipv6_in->saddr.in6_u.u6_addr8, zero_addr8, 16) == 0)
271 /* Duplicate Address Detection: Source IP is all zero */
274 icmp6_out.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
275 icmp6_out.icmp6_code = 0;
276 icmp6_out.icmp6_cksum = 0;
277 icmp6_out.icmp6_dataun.un_data32[0] = htonl(0x60000000); /* R=0, S=1, O=1 */
279 ns = (struct neighbour_solicitation *)(skb_in->data + mac_header_len + sizeof(struct ipv6hdr) + sizeof(struct icmp6hdr));
280 memcpy(&na.target_address, ns->target_address, 16);
283 na.link_layer_address[0] = 0x00;
284 na.link_layer_address[1] = 0x0a;
285 na.link_layer_address[2] = 0x3b;
286 na.link_layer_address[3] = 0xaf;
287 na.link_layer_address[4] = 0x63;
288 na.link_layer_address[5] = 0xc7;
290 memcpy(&ipv6_out, ipv6_in, sizeof(struct ipv6hdr));
291 memcpy(ipv6_out.saddr.in6_u.u6_addr8, &na.target_address, 16);
292 memcpy(ipv6_out.daddr.in6_u.u6_addr8, ipv6_in->saddr.in6_u.u6_addr8, 16);
293 ipv6_out.payload_len = htons(sizeof(struct icmp6hdr) + sizeof(struct neighbour_advertisement));
295 memcpy(icmp_na, &icmp6_out, sizeof(struct icmp6hdr));
296 memcpy(icmp_na + sizeof(struct icmp6hdr), &na, sizeof(struct neighbour_advertisement));
298 icmp6_out.icmp6_cksum = icmp6_checksum(&ipv6_out, (u16 *)icmp_na, sizeof(icmp_na));
303 /* Fill the destination mac with source mac of the received packet */
304 memcpy(mac_header_data, mac_header_data + ETH_ALEN, ETH_ALEN);
305 /* Fill the source mac with nic's source mac */
306 memcpy(mac_header_data + ETH_ALEN, nic->src_mac_addr, ETH_ALEN);
308 /* Alloc skb and reserve align */
309 skb_out = dev_alloc_skb(skb_in->len);
312 skb_reserve(skb_out, NET_IP_ALIGN);
314 memcpy(skb_put(skb_out, mac_header_len), mac_header_data, mac_header_len);
315 memcpy(skb_put(skb_out, sizeof(struct ipv6hdr)), &ipv6_out, sizeof(struct ipv6hdr));
316 memcpy(skb_put(skb_out, sizeof(struct icmp6hdr)), &icmp6_out, sizeof(struct icmp6hdr));
317 memcpy(skb_put(skb_out, sizeof(struct neighbour_advertisement)), &na, sizeof(struct neighbour_advertisement));
319 skb_out->protocol = ((struct ethhdr *)mac_header_data)->h_proto;
320 skb_out->dev = skb_in->dev;
321 skb_reset_mac_header(skb_out);
322 skb_pull(skb_out, ETH_HLEN);
324 gdm_lte_rx(skb_out, nic, nic_type);
329 static s32 gdm_lte_tx_nic_type(struct net_device *dev, struct sk_buff *skb)
331 struct nic *nic = netdev_priv(dev);
333 struct vlan_ethhdr *vlan_eth;
335 struct ipv6hdr *ipv6;
340 /* NIC TYPE is based on the nic_id of this net_device */
341 nic_type = 0x00000010 | nic->nic_id;
343 /* Get ethernet protocol */
344 eth = (struct ethhdr *)skb->data;
345 if (ntohs(eth->h_proto) == ETH_P_8021Q) {
346 vlan_eth = (struct vlan_ethhdr *)skb->data;
347 mac_proto = ntohs(vlan_eth->h_vlan_encapsulated_proto);
348 network_data = skb->data + VLAN_ETH_HLEN;
349 nic_type |= NIC_TYPE_F_VLAN;
351 mac_proto = ntohs(eth->h_proto);
352 network_data = skb->data + ETH_HLEN;
355 /* Process packet for nic type */
358 nic_type |= NIC_TYPE_ARP;
361 nic_type |= NIC_TYPE_F_IPV4;
362 ip = (struct iphdr *)network_data;
365 if (ip->protocol == IPPROTO_UDP) {
366 struct udphdr *udp = (struct udphdr *)(network_data + sizeof(struct iphdr));
367 if (ntohs(udp->dest) == 67 || ntohs(udp->dest) == 68)
368 nic_type |= NIC_TYPE_F_DHCP;
372 nic_type |= NIC_TYPE_F_IPV6;
373 ipv6 = (struct ipv6hdr *)network_data;
375 if (ipv6->nexthdr == IPPROTO_ICMPV6) /* Check NDP request */ {
376 struct icmp6hdr *icmp6 = (struct icmp6hdr *)(network_data + sizeof(struct ipv6hdr));
377 if (/*icmp6->icmp6_type == NDISC_ROUTER_SOLICITATION || */
378 icmp6->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
379 nic_type |= NIC_TYPE_ICMPV6;
380 } else if (ipv6->nexthdr == IPPROTO_UDP) /* Check DHCPv6 */ {
381 struct udphdr *udp = (struct udphdr *)(network_data + sizeof(struct ipv6hdr));
382 if (ntohs(udp->dest) == 546 || ntohs(udp->dest) == 547)
383 nic_type |= NIC_TYPE_F_DHCP;
393 static int gdm_lte_tx(struct sk_buff *skb, struct net_device *dev)
395 struct nic *nic = netdev_priv(dev);
402 nic_type = gdm_lte_tx_nic_type(dev, skb);
404 netdev_err(dev, "tx - invalid nic_type\n");
408 if (nic_type & NIC_TYPE_ARP) {
409 if (gdm_lte_emulate_arp(skb, nic_type) == 0) {
415 if (nic_type & NIC_TYPE_ICMPV6) {
416 if (gdm_lte_emulate_ndp(skb, nic_type) == 0) {
423 Need byte shift (that is, remove VLAN tag) if there is one
424 For the case of ARP, this breaks the offset as vlan_ethhdr+4 is treated as ethhdr
425 However, it shouldn't be a problem as the response starts from arp_hdr and ethhdr
426 is created by this driver based on the NIC mac
428 if (nic_type & NIC_TYPE_F_VLAN) {
429 struct vlan_ethhdr *vlan_eth = (struct vlan_ethhdr *)skb->data;
430 nic->vlan_id = ntohs(vlan_eth->h_vlan_TCI) & VLAN_VID_MASK;
431 data_buf = skb->data + (VLAN_ETH_HLEN - ETH_HLEN);
432 data_len = skb->len - (VLAN_ETH_HLEN - ETH_HLEN);
435 data_buf = skb->data;
439 /* If it is a ICMPV6 packet, clear all the other bits : for backward compatibility with the firmware */
440 if (nic_type & NIC_TYPE_ICMPV6)
441 nic_type = NIC_TYPE_ICMPV6;
443 /* If it is not a dhcp packet, clear all the flag bits : original NIC, otherwise the special flag (IPVX | DHCP) */
444 if (!(nic_type & NIC_TYPE_F_DHCP))
445 nic_type &= NIC_TYPE_MASK;
447 sscanf(dev->name, "lte%d", &idx);
449 ret = nic->phy_dev->send_sdu_func(nic->phy_dev->priv_dev,
451 nic->pdn_table.dft_eps_id, 0,
452 tx_complete, nic, idx,
455 if (ret == TX_NO_BUFFER || ret == TX_NO_SPC) {
456 netif_stop_queue(dev);
457 if (ret == TX_NO_BUFFER)
461 } else if (ret == TX_NO_DEV) {
465 /* Updates tx stats */
467 nic->stats.tx_dropped++;
469 nic->stats.tx_packets++;
470 nic->stats.tx_bytes += data_len;
477 static struct net_device_stats *gdm_lte_stats(struct net_device *dev)
479 struct nic *nic = netdev_priv(dev);
483 static int gdm_lte_event_send(struct net_device *dev, char *buf, int len)
485 struct nic *nic = netdev_priv(dev);
486 struct hci_packet *hci = (struct hci_packet *)buf;
489 sscanf(dev->name, "lte%d", &idx);
491 return netlink_send(lte_event.sock, idx, 0, buf,
493 nic->phy_dev->get_endian(
494 nic->phy_dev->priv_dev), hci->len)
498 static void gdm_lte_event_rcv(struct net_device *dev, u16 type, void *msg, int len)
500 struct nic *nic = netdev_priv(dev);
502 nic->phy_dev->send_hci_func(nic->phy_dev->priv_dev, msg, len, NULL,
506 int gdm_lte_event_init(void)
508 if (lte_event.ref_cnt == 0)
509 lte_event.sock = netlink_init(NETLINK_LTE, gdm_lte_event_rcv);
511 if (lte_event.sock) {
516 pr_err("event init failed\n");
520 void gdm_lte_event_exit(void)
522 if (lte_event.sock && --lte_event.ref_cnt == 0) {
523 netlink_exit(lte_event.sock);
524 lte_event.sock = NULL;
528 static u8 find_dev_index(u32 nic_type)
532 index = (u8)(nic_type & 0x0000000f);
533 if (index > MAX_NIC_TYPE)
539 static void gdm_lte_netif_rx(struct net_device *dev, char *buf, int len, int flagged_nic_type)
545 struct vlan_ethhdr vlan_eth;
546 void *mac_header_data;
550 nic_type = flagged_nic_type & NIC_TYPE_MASK;
551 nic = netdev_priv(dev);
553 if (flagged_nic_type & NIC_TYPE_F_DHCP) {
554 /* Change the destination mac address with the one requested the IP */
555 if (flagged_nic_type & NIC_TYPE_F_IPV4) {
557 u8 op; /* BOOTREQUEST or BOOTREPLY */
558 u8 htype; /* hardware address type. 1 = 10mb ethernet */
559 u8 hlen; /* hardware address length */
560 u8 hops; /* used by relay agents only */
561 u32 xid; /* unique id */
562 u16 secs; /* elapsed since client began acquisition/renewal */
563 u16 flags; /* only one flag so far: */
564 #define BROADCAST_FLAG 0x8000 /* "I need broadcast replies" */
565 u32 ciaddr; /* client IP (if client is in BOUND, RENEW or REBINDING state) */
566 u32 yiaddr; /* 'your' (client) IP address */
567 /* IP address of next server to use in bootstrap, returned in DHCPOFFER, DHCPACK by server */
569 u32 gateway_nip; /* relay agent IP address */
570 u8 chaddr[16]; /* link-layer client hardware address (MAC) */
571 u8 sname[64]; /* server host name (ASCIZ) */
572 u8 file[128]; /* boot file name (ASCIZ) */
573 u32 cookie; /* fixed first four option bytes (99,130,83,99 dec) */
575 void *addr = buf + sizeof(struct iphdr) + sizeof(struct udphdr) + offsetof(struct dhcp_packet, chaddr);
576 memcpy(nic->dest_mac_addr, addr, ETH_ALEN);
580 if (nic->vlan_id > 0) {
581 mac_header_data = (void *)&vlan_eth;
582 mac_header_len = VLAN_ETH_HLEN;
584 mac_header_data = (void *)ð
585 mac_header_len = ETH_HLEN;
588 /* Format the data so that it can be put to skb */
589 memcpy(mac_header_data, nic->dest_mac_addr, ETH_ALEN);
590 memcpy(mac_header_data + ETH_ALEN, nic->src_mac_addr, ETH_ALEN);
592 vlan_eth.h_vlan_TCI = htons(nic->vlan_id);
593 vlan_eth.h_vlan_proto = htons(ETH_P_8021Q);
595 if (nic_type == NIC_TYPE_ARP) {
596 /* Should be response: Only happens because there was a request from the host */
597 eth.h_proto = htons(ETH_P_ARP);
598 vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_ARP);
600 ip_version = buf[0] >> 4;
601 if (ip_version == IP_VERSION_4) {
602 eth.h_proto = htons(ETH_P_IP);
603 vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_IP);
604 } else if (ip_version == IP_VERSION_6) {
605 eth.h_proto = htons(ETH_P_IPV6);
606 vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_IPV6);
608 netdev_err(dev, "Unknown IP version %d\n", ip_version);
613 /* Alloc skb and reserve align */
614 skb = dev_alloc_skb(len + mac_header_len + NET_IP_ALIGN);
617 skb_reserve(skb, NET_IP_ALIGN);
619 memcpy(skb_put(skb, mac_header_len), mac_header_data, mac_header_len);
620 memcpy(skb_put(skb, len), buf, len);
622 skb->protocol = ((struct ethhdr *)mac_header_data)->h_proto;
624 skb_reset_mac_header(skb);
625 skb_pull(skb, ETH_HLEN);
627 gdm_lte_rx(skb, nic, nic_type);
630 static void gdm_lte_multi_sdu_pkt(struct phy_dev *phy_dev, char *buf, int len)
632 struct net_device *dev;
633 struct multi_sdu *multi_sdu = (struct multi_sdu *)buf;
634 struct sdu *sdu = NULL;
635 u8 *data = (u8 *)multi_sdu->data;
643 hci_len = gdm_dev16_to_cpu(phy_dev->get_endian(phy_dev->priv_dev), multi_sdu->len);
644 num_packet = gdm_dev16_to_cpu(phy_dev->get_endian(phy_dev->priv_dev), multi_sdu->num_packet);
646 for (i = 0; i < num_packet; i++) {
647 sdu = (struct sdu *)data;
649 cmd_evt = gdm_dev16_to_cpu(phy_dev->get_endian(phy_dev->priv_dev), sdu->cmd_evt);
650 hci_len = gdm_dev16_to_cpu(phy_dev->get_endian(phy_dev->priv_dev), sdu->len);
651 nic_type = gdm_dev32_to_cpu(phy_dev->get_endian(phy_dev->priv_dev), sdu->nic_type);
653 if (cmd_evt != LTE_RX_SDU) {
654 pr_err("rx sdu wrong hci %04x\n", cmd_evt);
658 pr_err("rx sdu invalid len %d\n", hci_len);
662 index = find_dev_index(nic_type);
663 if (index < MAX_NIC_TYPE) {
664 dev = phy_dev->dev[index];
665 gdm_lte_netif_rx(dev, (char *)sdu->data, (int)(hci_len-12), nic_type);
667 pr_err("rx sdu invalid nic_type :%x\n", nic_type);
670 data += ((hci_len+3) & 0xfffc) + HCI_HEADER_SIZE;
674 static void gdm_lte_pdn_table(struct net_device *dev, char *buf, int len)
676 struct nic *nic = netdev_priv(dev);
677 struct hci_pdn_table_ind *pdn_table = (struct hci_pdn_table_ind *)buf;
679 if (pdn_table->activate) {
680 nic->pdn_table.activate = pdn_table->activate;
681 nic->pdn_table.dft_eps_id = gdm_dev32_to_cpu(
682 nic->phy_dev->get_endian(
683 nic->phy_dev->priv_dev),
684 pdn_table->dft_eps_id);
685 nic->pdn_table.nic_type = gdm_dev32_to_cpu(
686 nic->phy_dev->get_endian(
687 nic->phy_dev->priv_dev),
688 pdn_table->nic_type);
690 netdev_info(dev, "pdn activated, nic_type=0x%x\n",
691 nic->pdn_table.nic_type);
693 memset(&nic->pdn_table, 0x00, sizeof(struct pdn_table));
694 netdev_info(dev, "pdn deactivated\n");
698 static int gdm_lte_receive_pkt(struct phy_dev *phy_dev, char *buf, int len)
700 struct hci_packet *hci = (struct hci_packet *)buf;
701 struct hci_pdn_table_ind *pdn_table = (struct hci_pdn_table_ind *)buf;
703 struct net_device *dev;
712 cmd_evt = gdm_dev16_to_cpu(phy_dev->get_endian(phy_dev->priv_dev), hci->cmd_evt);
714 dev = phy_dev->dev[0];
720 sdu = (struct sdu *)hci->data;
721 nic_type = gdm_dev32_to_cpu(phy_dev->get_endian(phy_dev->priv_dev), sdu->nic_type);
722 index = find_dev_index(nic_type);
723 dev = phy_dev->dev[index];
724 gdm_lte_netif_rx(dev, hci->data, len, nic_type);
726 case LTE_RX_MULTI_SDU:
727 gdm_lte_multi_sdu_pkt(phy_dev, buf, len);
729 case LTE_LINK_ON_OFF_INDICATION:
730 netdev_info(dev, "link %s\n",
731 ((struct hci_connect_ind *)buf)->connect
734 case LTE_PDN_TABLE_IND:
735 pdn_table = (struct hci_pdn_table_ind *)buf;
736 nic_type = gdm_dev32_to_cpu(phy_dev->get_endian(phy_dev->priv_dev), pdn_table->nic_type);
737 index = find_dev_index(nic_type);
738 dev = phy_dev->dev[index];
739 gdm_lte_pdn_table(dev, buf, len);
742 ret = gdm_lte_event_send(dev, buf, len);
749 static int rx_complete(void *arg, void *data, int len, int context)
751 struct phy_dev *phy_dev = (struct phy_dev *)arg;
753 return gdm_lte_receive_pkt(phy_dev, (char *)data, len);
756 void start_rx_proc(struct phy_dev *phy_dev)
760 for (i = 0; i < MAX_RX_SUBMIT_COUNT; i++)
761 phy_dev->rcv_func(phy_dev->priv_dev, rx_complete, phy_dev, USB_COMPLETE);
764 static struct net_device_ops gdm_netdev_ops = {
765 .ndo_open = gdm_lte_open,
766 .ndo_stop = gdm_lte_close,
767 .ndo_set_config = gdm_lte_set_config,
768 .ndo_start_xmit = gdm_lte_tx,
769 .ndo_get_stats = gdm_lte_stats,
772 static u8 gdm_lte_macaddr[ETH_ALEN] = {0x00, 0x0a, 0x3b, 0x00, 0x00, 0x00};
774 static void form_mac_address(u8 *dev_addr, u8 *nic_src, u8 *nic_dest, u8 *mac_address, u8 index)
776 /* Form the dev_addr */
778 memcpy(dev_addr, gdm_lte_macaddr, ETH_ALEN);
780 memcpy(dev_addr, mac_address, ETH_ALEN);
782 /* The last byte of the mac address should be less than or equal to 0xFC */
783 dev_addr[ETH_ALEN-1] += index;
785 /* Create random nic src and copy the first 3 bytes to be the same as dev_addr */
786 random_ether_addr(nic_src);
787 memcpy(nic_src, dev_addr, 3);
789 /* Copy the nic_dest from dev_addr*/
790 memcpy(nic_dest, dev_addr, ETH_ALEN);
793 static void validate_mac_address(u8 *mac_address)
795 /* if zero address or multicast bit set, restore the default value */
796 if (is_zero_ether_addr(mac_address) || (mac_address[0] & 0x01)) {
797 pr_err("MAC invalid, restoring default\n");
798 memcpy(mac_address, gdm_lte_macaddr, 6);
802 int register_lte_device(struct phy_dev *phy_dev, struct device *dev, u8 *mac_address)
805 struct net_device *net;
806 char pdn_dev_name[16];
810 validate_mac_address(mac_address);
812 for (index = 0; index < MAX_NIC_TYPE; index++) {
813 /* Create device name lteXpdnX */
814 sprintf(pdn_dev_name, "lte%%dpdn%d", index);
816 /* Allocate netdev */
817 net = alloc_netdev(sizeof(struct nic), pdn_dev_name, ether_setup);
819 pr_err("alloc_netdev failed\n");
823 net->netdev_ops = &gdm_netdev_ops;
824 net->flags &= ~IFF_MULTICAST;
825 net->mtu = DEFAULT_MTU_SIZE;
827 nic = netdev_priv(net);
828 memset(nic, 0, sizeof(struct nic));
830 nic->phy_dev = phy_dev;
840 SET_NETDEV_DEV(net, dev);
841 SET_NETDEV_DEVTYPE(net, &wwan_type);
843 ret = register_netdev(net);
847 netif_carrier_on(net);
849 phy_dev->dev[index] = net;
855 unregister_lte_device(phy_dev);
860 void unregister_lte_device(struct phy_dev *phy_dev)
862 struct net_device *net;
865 for (index = 0; index < MAX_NIC_TYPE; index++) {
866 net = phy_dev->dev[index];
870 unregister_netdev(net);