]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/staging/gdm724x/gdm_lte.c
[media] usb: Make use of media_bus_format enum
[mirror_ubuntu-artful-kernel.git] / drivers / staging / gdm724x / gdm_lte.c
CommitLineData
61e12104
WK
1/*
2 * Copyright (c) 2012 GCT Semiconductor, Inc. All rights reserved.
3 *
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.
7 *
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.
12 */
13
0ec473b5
JP
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
61e12104
WK
16#include <linux/etherdevice.h>
17#include <linux/ip.h>
18#include <linux/ipv6.h>
19#include <linux/udp.h>
20#include <linux/in.h>
21#include <linux/if_arp.h>
22#include <linux/if_ether.h>
23#include <linux/if_vlan.h>
61e12104 24#include <linux/in6.h>
61e12104
WK
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>
30
31#include "gdm_lte.h"
32#include "netlink_k.h"
33#include "hci.h"
34#include "hci_packet.h"
35#include "gdm_endian.h"
61e12104
WK
36
37/*
38 * Netlink protocol number
39 */
40#define NETLINK_LTE 30
41
42/*
43 * Default MTU Size
44 */
45#define DEFAULT_MTU_SIZE 1500
46
61e12104
WK
47#define IP_VERSION_4 4
48#define IP_VERSION_6 6
49
50static struct {
51 int ref_cnt;
52 struct sock *sock;
53} lte_event;
54
55static struct device_type wwan_type = {
56 .name = "wwan",
57};
58
59static int gdm_lte_open(struct net_device *dev)
60{
61 netif_start_queue(dev);
62 return 0;
63}
64
65static int gdm_lte_close(struct net_device *dev)
66{
67 netif_stop_queue(dev);
68 return 0;
69}
70
71static int gdm_lte_set_config(struct net_device *dev, struct ifmap *map)
72{
73 if (dev->flags & IFF_UP)
74 return -EBUSY;
75 return 0;
76}
77
78static void tx_complete(void *arg)
79{
80 struct nic *nic = arg;
81
82 if (netif_queue_stopped(nic->netdev))
83 netif_wake_queue(nic->netdev);
84}
85
86static int gdm_lte_rx(struct sk_buff *skb, struct nic *nic, int nic_type)
87{
88 int ret;
89
90 ret = netif_rx_ni(skb);
91 if (ret == NET_RX_DROP) {
61e12104
WK
92 nic->stats.rx_dropped++;
93 } else {
94 nic->stats.rx_packets++;
95 nic->stats.rx_bytes += skb->len + ETH_HLEN;
96 }
97
98 return 0;
99}
100
ff52b8fe 101static int gdm_lte_emulate_arp(struct sk_buff *skb_in, u32 nic_type)
61e12104
WK
102{
103 struct nic *nic = netdev_priv(skb_in->dev);
104 struct sk_buff *skb_out;
105 struct ethhdr eth;
106 struct vlan_ethhdr vlan_eth;
107 struct arphdr *arp_in;
108 struct arphdr *arp_out;
109 struct arpdata {
110 u8 ar_sha[ETH_ALEN];
111 u8 ar_sip[4];
112 u8 ar_tha[ETH_ALEN];
113 u8 ar_tip[4];
114 };
115 struct arpdata *arp_data_in;
116 struct arpdata *arp_data_out;
117 u8 arp_temp[60];
118 void *mac_header_data;
119 u32 mac_header_len;
120
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;
126 } else {
127 memcpy(&eth, skb_in->data, sizeof(struct ethhdr));
128 mac_header_data = &eth;
129 mac_header_len = ETH_HLEN;
130 }
131
132 /* Get the pointer of the original request */
133 arp_in = (struct arphdr *)(skb_in->data + mac_header_len);
097b4d8c
GK
134 arp_data_in = (struct arpdata *)(skb_in->data + mac_header_len +
135 sizeof(struct arphdr));
61e12104
WK
136
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));
140
141 /* Copy the arp header */
142 memcpy(arp_out, arp_in, sizeof(struct arphdr));
143 arp_out->ar_op = htons(ARPOP_REPLY);
144
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);
152
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);
157
158 /* Alloc skb and reserve align */
159 skb_out = dev_alloc_skb(skb_in->len);
160 if (!skb_out)
161 return -ENOMEM;
162 skb_reserve(skb_out, NET_IP_ALIGN);
163
097b4d8c
GK
164 memcpy(skb_put(skb_out, mac_header_len), mac_header_data,
165 mac_header_len);
166 memcpy(skb_put(skb_out, sizeof(struct arphdr)), arp_out,
167 sizeof(struct arphdr));
168 memcpy(skb_put(skb_out, sizeof(struct arpdata)), arp_data_out,
169 sizeof(struct arpdata));
61e12104
WK
170
171 skb_out->protocol = ((struct ethhdr *)mac_header_data)->h_proto;
172 skb_out->dev = skb_in->dev;
173 skb_reset_mac_header(skb_out);
174 skb_pull(skb_out, ETH_HLEN);
175
176 gdm_lte_rx(skb_out, nic, nic_type);
177
178 return 0;
179}
180
ff52b8fe 181static int icmp6_checksum(struct ipv6hdr *ipv6, u16 *ptr, int len)
61e12104
WK
182{
183 unsigned short *w = ptr;
184 int sum = 0;
185 int i;
186
187 union {
188 struct {
189 u8 ph_src[16];
190 u8 ph_dst[16];
191 u32 ph_len;
192 u8 ph_zero[3];
193 u8 ph_nxt;
194 } ph __packed;
195 u16 pa[20];
196 } pseudo_header;
197
198 memset(&pseudo_header, 0, sizeof(pseudo_header));
199 memcpy(&pseudo_header.ph.ph_src, &ipv6->saddr.in6_u.u6_addr8, 16);
200 memcpy(&pseudo_header.ph.ph_dst, &ipv6->daddr.in6_u.u6_addr8, 16);
201 pseudo_header.ph.ph_len = ipv6->payload_len;
202 pseudo_header.ph.ph_nxt = ipv6->nexthdr;
203
204 w = (u16 *)&pseudo_header;
097b4d8c 205 for (i = 0; i < ARRAY_SIZE(pseudo_header.pa); i++)
61e12104
WK
206 sum += pseudo_header.pa[i];
207
208 w = ptr;
209 while (len > 1) {
210 sum += *w++;
211 len -= 2;
212 }
213
214 sum = (sum >> 16) + (sum & 0xFFFF);
215 sum += (sum >> 16);
216 sum = ~sum & 0xffff;
217
218 return sum;
219}
220
ff52b8fe 221static int gdm_lte_emulate_ndp(struct sk_buff *skb_in, u32 nic_type)
61e12104
WK
222{
223 struct nic *nic = netdev_priv(skb_in->dev);
224 struct sk_buff *skb_out;
225 struct ethhdr eth;
226 struct vlan_ethhdr vlan_eth;
227 struct neighbour_advertisement {
228 u8 target_address[16];
229 u8 type;
230 u8 length;
231 u8 link_layer_address[6];
232 };
233 struct neighbour_advertisement na;
234 struct neighbour_solicitation {
235 u8 target_address[16];
236 };
237 struct neighbour_solicitation *ns;
238 struct ipv6hdr *ipv6_in;
239 struct ipv6hdr ipv6_out;
240 struct icmp6hdr *icmp6_in;
241 struct icmp6hdr icmp6_out;
242
243 void *mac_header_data;
244 u32 mac_header_len;
245
246 /* Format the mac header so that it can be put to skb */
247 if (ntohs(((struct ethhdr *)skb_in->data)->h_proto) == ETH_P_8021Q) {
248 memcpy(&vlan_eth, skb_in->data, sizeof(struct vlan_ethhdr));
249 if (ntohs(vlan_eth.h_vlan_encapsulated_proto) != ETH_P_IPV6)
250 return -1;
251 mac_header_data = &vlan_eth;
252 mac_header_len = VLAN_ETH_HLEN;
253 } else {
254 memcpy(&eth, skb_in->data, sizeof(struct ethhdr));
255 if (ntohs(eth.h_proto) != ETH_P_IPV6)
256 return -1;
257 mac_header_data = &eth;
258 mac_header_len = ETH_HLEN;
259 }
260
261 /* Check if this is IPv6 ICMP packet */
262 ipv6_in = (struct ipv6hdr *)(skb_in->data + mac_header_len);
263 if (ipv6_in->version != 6 || ipv6_in->nexthdr != IPPROTO_ICMPV6)
264 return -1;
265
266 /* Check if this is NDP packet */
097b4d8c
GK
267 icmp6_in = (struct icmp6hdr *)(skb_in->data + mac_header_len +
268 sizeof(struct ipv6hdr));
61e12104
WK
269 if (icmp6_in->icmp6_type == NDISC_ROUTER_SOLICITATION) { /* Check RS */
270 return -1;
097b4d8c
GK
271 } else if (icmp6_in->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) {
272 /* Check NS */
273 u8 icmp_na[sizeof(struct icmp6hdr) +
274 sizeof(struct neighbour_advertisement)];
61e12104
WK
275 u8 zero_addr8[16] = {0,};
276
277 if (memcmp(ipv6_in->saddr.in6_u.u6_addr8, zero_addr8, 16) == 0)
278 /* Duplicate Address Detection: Source IP is all zero */
279 return 0;
280
281 icmp6_out.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
282 icmp6_out.icmp6_code = 0;
283 icmp6_out.icmp6_cksum = 0;
284 icmp6_out.icmp6_dataun.un_data32[0] = htonl(0x60000000); /* R=0, S=1, O=1 */
285
097b4d8c
GK
286 ns = (struct neighbour_solicitation *)
287 (skb_in->data + mac_header_len +
288 sizeof(struct ipv6hdr) + sizeof(struct icmp6hdr));
61e12104
WK
289 memcpy(&na.target_address, ns->target_address, 16);
290 na.type = 0x02;
291 na.length = 1;
292 na.link_layer_address[0] = 0x00;
293 na.link_layer_address[1] = 0x0a;
294 na.link_layer_address[2] = 0x3b;
295 na.link_layer_address[3] = 0xaf;
296 na.link_layer_address[4] = 0x63;
297 na.link_layer_address[5] = 0xc7;
298
299 memcpy(&ipv6_out, ipv6_in, sizeof(struct ipv6hdr));
300 memcpy(ipv6_out.saddr.in6_u.u6_addr8, &na.target_address, 16);
097b4d8c
GK
301 memcpy(ipv6_out.daddr.in6_u.u6_addr8,
302 ipv6_in->saddr.in6_u.u6_addr8, 16);
303 ipv6_out.payload_len = htons(sizeof(struct icmp6hdr) +
304 sizeof(struct neighbour_advertisement));
61e12104
WK
305
306 memcpy(icmp_na, &icmp6_out, sizeof(struct icmp6hdr));
097b4d8c
GK
307 memcpy(icmp_na + sizeof(struct icmp6hdr), &na,
308 sizeof(struct neighbour_advertisement));
61e12104 309
097b4d8c
GK
310 icmp6_out.icmp6_cksum = icmp6_checksum(&ipv6_out,
311 (u16 *)icmp_na, sizeof(icmp_na));
61e12104
WK
312 } else {
313 return -1;
314 }
315
316 /* Fill the destination mac with source mac of the received packet */
317 memcpy(mac_header_data, mac_header_data + ETH_ALEN, ETH_ALEN);
318 /* Fill the source mac with nic's source mac */
319 memcpy(mac_header_data + ETH_ALEN, nic->src_mac_addr, ETH_ALEN);
320
321 /* Alloc skb and reserve align */
322 skb_out = dev_alloc_skb(skb_in->len);
323 if (!skb_out)
324 return -ENOMEM;
325 skb_reserve(skb_out, NET_IP_ALIGN);
326
097b4d8c
GK
327 memcpy(skb_put(skb_out, mac_header_len), mac_header_data,
328 mac_header_len);
329 memcpy(skb_put(skb_out, sizeof(struct ipv6hdr)), &ipv6_out,
330 sizeof(struct ipv6hdr));
331 memcpy(skb_put(skb_out, sizeof(struct icmp6hdr)), &icmp6_out,
332 sizeof(struct icmp6hdr));
333 memcpy(skb_put(skb_out, sizeof(struct neighbour_advertisement)), &na,
334 sizeof(struct neighbour_advertisement));
61e12104
WK
335
336 skb_out->protocol = ((struct ethhdr *)mac_header_data)->h_proto;
337 skb_out->dev = skb_in->dev;
338 skb_reset_mac_header(skb_out);
339 skb_pull(skb_out, ETH_HLEN);
340
341 gdm_lte_rx(skb_out, nic, nic_type);
342
343 return 0;
344}
345
346static s32 gdm_lte_tx_nic_type(struct net_device *dev, struct sk_buff *skb)
347{
348 struct nic *nic = netdev_priv(dev);
349 struct ethhdr *eth;
350 struct vlan_ethhdr *vlan_eth;
351 struct iphdr *ip;
352 struct ipv6hdr *ipv6;
353 int mac_proto;
354 void *network_data;
355 u32 nic_type = 0;
356
357 /* NIC TYPE is based on the nic_id of this net_device */
358 nic_type = 0x00000010 | nic->nic_id;
359
360 /* Get ethernet protocol */
361 eth = (struct ethhdr *)skb->data;
362 if (ntohs(eth->h_proto) == ETH_P_8021Q) {
363 vlan_eth = (struct vlan_ethhdr *)skb->data;
364 mac_proto = ntohs(vlan_eth->h_vlan_encapsulated_proto);
365 network_data = skb->data + VLAN_ETH_HLEN;
366 nic_type |= NIC_TYPE_F_VLAN;
367 } else {
368 mac_proto = ntohs(eth->h_proto);
369 network_data = skb->data + ETH_HLEN;
370 }
371
372 /* Process packet for nic type */
373 switch (mac_proto) {
374 case ETH_P_ARP:
375 nic_type |= NIC_TYPE_ARP;
376 break;
377 case ETH_P_IP:
378 nic_type |= NIC_TYPE_F_IPV4;
379 ip = (struct iphdr *)network_data;
380
381 /* Check DHCPv4 */
382 if (ip->protocol == IPPROTO_UDP) {
097b4d8c
GK
383 struct udphdr *udp = (struct udphdr *)
384 (network_data + sizeof(struct iphdr));
61e12104
WK
385 if (ntohs(udp->dest) == 67 || ntohs(udp->dest) == 68)
386 nic_type |= NIC_TYPE_F_DHCP;
387 }
388 break;
389 case ETH_P_IPV6:
390 nic_type |= NIC_TYPE_F_IPV6;
391 ipv6 = (struct ipv6hdr *)network_data;
392
393 if (ipv6->nexthdr == IPPROTO_ICMPV6) /* Check NDP request */ {
097b4d8c
GK
394 struct icmp6hdr *icmp6 = (struct icmp6hdr *)
395 (network_data + sizeof(struct ipv6hdr));
396 if (icmp6->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
61e12104
WK
397 nic_type |= NIC_TYPE_ICMPV6;
398 } else if (ipv6->nexthdr == IPPROTO_UDP) /* Check DHCPv6 */ {
097b4d8c
GK
399 struct udphdr *udp = (struct udphdr *)
400 (network_data + sizeof(struct ipv6hdr));
61e12104
WK
401 if (ntohs(udp->dest) == 546 || ntohs(udp->dest) == 547)
402 nic_type |= NIC_TYPE_F_DHCP;
403 }
404 break;
405 default:
406 break;
407 }
408
409 return nic_type;
410}
411
412static int gdm_lte_tx(struct sk_buff *skb, struct net_device *dev)
413{
414 struct nic *nic = netdev_priv(dev);
415 u32 nic_type;
416 void *data_buf;
417 int data_len;
418 int idx;
419 int ret = 0;
420
421 nic_type = gdm_lte_tx_nic_type(dev, skb);
422 if (nic_type == 0) {
0ec473b5 423 netdev_err(dev, "tx - invalid nic_type\n");
61e12104
WK
424 return -1;
425 }
426
427 if (nic_type & NIC_TYPE_ARP) {
428 if (gdm_lte_emulate_arp(skb, nic_type) == 0) {
429 dev_kfree_skb(skb);
430 return 0;
431 }
432 }
433
434 if (nic_type & NIC_TYPE_ICMPV6) {
435 if (gdm_lte_emulate_ndp(skb, nic_type) == 0) {
436 dev_kfree_skb(skb);
437 return 0;
438 }
439 }
440
441 /*
097b4d8c
GK
442 * Need byte shift (that is, remove VLAN tag) if there is one
443 * For the case of ARP, this breaks the offset as vlan_ethhdr+4
444 * is treated as ethhdr However, it shouldn't be a problem as
445 * the response starts from arp_hdr and ethhdr is created by this
446 * driver based on the NIC mac
447 */
61e12104
WK
448 if (nic_type & NIC_TYPE_F_VLAN) {
449 struct vlan_ethhdr *vlan_eth = (struct vlan_ethhdr *)skb->data;
4e13d410 450
61e12104
WK
451 nic->vlan_id = ntohs(vlan_eth->h_vlan_TCI) & VLAN_VID_MASK;
452 data_buf = skb->data + (VLAN_ETH_HLEN - ETH_HLEN);
453 data_len = skb->len - (VLAN_ETH_HLEN - ETH_HLEN);
454 } else {
455 nic->vlan_id = 0;
456 data_buf = skb->data;
457 data_len = skb->len;
458 }
459
097b4d8c
GK
460 /* If it is a ICMPV6 packet, clear all the other bits :
461 * for backward compatibility with the firmware
462 */
61e12104
WK
463 if (nic_type & NIC_TYPE_ICMPV6)
464 nic_type = NIC_TYPE_ICMPV6;
465
097b4d8c
GK
466 /* If it is not a dhcp packet, clear all the flag bits :
467 * original NIC, otherwise the special flag (IPVX | DHCP)
468 */
61e12104
WK
469 if (!(nic_type & NIC_TYPE_F_DHCP))
470 nic_type &= NIC_TYPE_MASK;
471
a28bfd11
MA
472 ret = sscanf(dev->name, "lte%d", &idx);
473 if (ret != 1) {
474 dev_kfree_skb(skb);
475 return -EINVAL;
476 }
61e12104 477
a23bb460
RK
478 ret = nic->phy_dev->send_sdu_func(nic->phy_dev->priv_dev,
479 data_buf, data_len,
480 nic->pdn_table.dft_eps_id, 0,
481 tx_complete, nic, idx,
482 nic_type);
61e12104
WK
483
484 if (ret == TX_NO_BUFFER || ret == TX_NO_SPC) {
485 netif_stop_queue(dev);
486 if (ret == TX_NO_BUFFER)
487 ret = 0;
488 else
489 ret = -ENOSPC;
490 } else if (ret == TX_NO_DEV) {
491 ret = -ENODEV;
492 }
493
494 /* Updates tx stats */
495 if (ret) {
496 nic->stats.tx_dropped++;
497 } else {
498 nic->stats.tx_packets++;
499 nic->stats.tx_bytes += data_len;
500 }
501 dev_kfree_skb(skb);
502
503 return 0;
504}
505
506static struct net_device_stats *gdm_lte_stats(struct net_device *dev)
507{
508 struct nic *nic = netdev_priv(dev);
4e13d410 509
61e12104
WK
510 return &nic->stats;
511}
512
61e12104
WK
513static int gdm_lte_event_send(struct net_device *dev, char *buf, int len)
514{
515 struct nic *nic = netdev_priv(dev);
516 struct hci_packet *hci = (struct hci_packet *)buf;
517 int idx;
a28bfd11 518 int ret;
61e12104 519
a28bfd11
MA
520 ret = sscanf(dev->name, "lte%d", &idx);
521 if (ret != 1)
522 return -EINVAL;
61e12104
WK
523
524 return netlink_send(lte_event.sock, idx, 0, buf,
04db9c6a
RK
525 gdm_dev16_to_cpu(
526 nic->phy_dev->get_endian(
527 nic->phy_dev->priv_dev), hci->len)
528 + HCI_HEADER_SIZE);
61e12104
WK
529}
530
097b4d8c
GK
531static void gdm_lte_event_rcv(struct net_device *dev, u16 type,
532 void *msg, int len)
61e12104
WK
533{
534 struct nic *nic = netdev_priv(dev);
535
abb40c11
RK
536 nic->phy_dev->send_hci_func(nic->phy_dev->priv_dev, msg, len, NULL,
537 NULL);
61e12104
WK
538}
539
540int gdm_lte_event_init(void)
541{
542 if (lte_event.ref_cnt == 0)
543 lte_event.sock = netlink_init(NETLINK_LTE, gdm_lte_event_rcv);
544
545 if (lte_event.sock) {
546 lte_event.ref_cnt++;
547 return 0;
548 }
549
0ec473b5 550 pr_err("event init failed\n");
61e12104
WK
551 return -1;
552}
553
554void gdm_lte_event_exit(void)
555{
556 if (lte_event.sock && --lte_event.ref_cnt == 0) {
557 netlink_exit(lte_event.sock);
558 lte_event.sock = NULL;
559 }
560}
561
562static u8 find_dev_index(u32 nic_type)
563{
564 u8 index;
565
566 index = (u8)(nic_type & 0x0000000f);
567 if (index > MAX_NIC_TYPE)
568 index = 0;
569
570 return index;
571}
572
097b4d8c
GK
573static void gdm_lte_netif_rx(struct net_device *dev, char *buf,
574 int len, int flagged_nic_type)
61e12104
WK
575{
576 u32 nic_type;
577 struct nic *nic;
578 struct sk_buff *skb;
579 struct ethhdr eth;
580 struct vlan_ethhdr vlan_eth;
581 void *mac_header_data;
582 u32 mac_header_len;
583 char ip_version = 0;
584
585 nic_type = flagged_nic_type & NIC_TYPE_MASK;
586 nic = netdev_priv(dev);
587
588 if (flagged_nic_type & NIC_TYPE_F_DHCP) {
097b4d8c
GK
589 /* Change the destination mac address
590 * with the one requested the IP
591 */
61e12104
WK
592 if (flagged_nic_type & NIC_TYPE_F_IPV4) {
593 struct dhcp_packet {
594 u8 op; /* BOOTREQUEST or BOOTREPLY */
097b4d8c
GK
595 u8 htype; /* hardware address type.
596 * 1 = 10mb ethernet
597 */
61e12104
WK
598 u8 hlen; /* hardware address length */
599 u8 hops; /* used by relay agents only */
600 u32 xid; /* unique id */
097b4d8c
GK
601 u16 secs; /* elapsed since client began
602 * acquisition/renewal
603 */
61e12104 604 u16 flags; /* only one flag so far: */
097b4d8c
GK
605 #define BROADCAST_FLAG 0x8000
606 /* "I need broadcast replies" */
607 u32 ciaddr; /* client IP (if client is in
608 * BOUND, RENEW or REBINDING state)
609 */
61e12104 610 u32 yiaddr; /* 'your' (client) IP address */
097b4d8c
GK
611 /* IP address of next server to use in
612 * bootstrap, returned in DHCPOFFER,
613 * DHCPACK by server
614 */
61e12104
WK
615 u32 siaddr_nip;
616 u32 gateway_nip; /* relay agent IP address */
097b4d8c
GK
617 u8 chaddr[16]; /* link-layer client hardware
618 * address (MAC)
619 */
61e12104
WK
620 u8 sname[64]; /* server host name (ASCIZ) */
621 u8 file[128]; /* boot file name (ASCIZ) */
097b4d8c
GK
622 u32 cookie; /* fixed first four option
623 * bytes (99,130,83,99 dec)
624 */
61e12104 625 } __packed;
097b4d8c
GK
626 void *addr = buf + sizeof(struct iphdr) +
627 sizeof(struct udphdr) +
628 offsetof(struct dhcp_packet, chaddr);
61e12104
WK
629 memcpy(nic->dest_mac_addr, addr, ETH_ALEN);
630 }
631 }
632
633 if (nic->vlan_id > 0) {
634 mac_header_data = (void *)&vlan_eth;
635 mac_header_len = VLAN_ETH_HLEN;
636 } else {
637 mac_header_data = (void *)&eth;
638 mac_header_len = ETH_HLEN;
639 }
640
641 /* Format the data so that it can be put to skb */
642 memcpy(mac_header_data, nic->dest_mac_addr, ETH_ALEN);
643 memcpy(mac_header_data + ETH_ALEN, nic->src_mac_addr, ETH_ALEN);
644
645 vlan_eth.h_vlan_TCI = htons(nic->vlan_id);
646 vlan_eth.h_vlan_proto = htons(ETH_P_8021Q);
647
648 if (nic_type == NIC_TYPE_ARP) {
097b4d8c
GK
649 /* Should be response: Only happens because
650 * there was a request from the host
651 */
61e12104
WK
652 eth.h_proto = htons(ETH_P_ARP);
653 vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_ARP);
654 } else {
655 ip_version = buf[0] >> 4;
656 if (ip_version == IP_VERSION_4) {
657 eth.h_proto = htons(ETH_P_IP);
658 vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_IP);
659 } else if (ip_version == IP_VERSION_6) {
660 eth.h_proto = htons(ETH_P_IPV6);
661 vlan_eth.h_vlan_encapsulated_proto = htons(ETH_P_IPV6);
662 } else {
0ec473b5 663 netdev_err(dev, "Unknown IP version %d\n", ip_version);
61e12104
WK
664 return;
665 }
666 }
667
668 /* Alloc skb and reserve align */
669 skb = dev_alloc_skb(len + mac_header_len + NET_IP_ALIGN);
670 if (!skb)
671 return;
672 skb_reserve(skb, NET_IP_ALIGN);
673
674 memcpy(skb_put(skb, mac_header_len), mac_header_data, mac_header_len);
675 memcpy(skb_put(skb, len), buf, len);
676
677 skb->protocol = ((struct ethhdr *)mac_header_data)->h_proto;
678 skb->dev = dev;
679 skb_reset_mac_header(skb);
680 skb_pull(skb, ETH_HLEN);
681
682 gdm_lte_rx(skb, nic, nic_type);
683}
684
685static void gdm_lte_multi_sdu_pkt(struct phy_dev *phy_dev, char *buf, int len)
686{
687 struct net_device *dev;
688 struct multi_sdu *multi_sdu = (struct multi_sdu *)buf;
689 struct sdu *sdu = NULL;
690 u8 *data = (u8 *)multi_sdu->data;
691 u16 i = 0;
692 u16 num_packet;
693 u16 hci_len;
694 u16 cmd_evt;
695 u32 nic_type;
696 u8 index;
697
097b4d8c
GK
698 hci_len = gdm_dev16_to_cpu(phy_dev->get_endian(phy_dev->priv_dev),
699 multi_sdu->len);
700 num_packet = gdm_dev16_to_cpu(phy_dev->get_endian(phy_dev->priv_dev),
701 multi_sdu->num_packet);
61e12104
WK
702
703 for (i = 0; i < num_packet; i++) {
704 sdu = (struct sdu *)data;
705
097b4d8c
GK
706 cmd_evt = gdm_dev16_to_cpu(phy_dev->
707 get_endian(phy_dev->priv_dev), sdu->cmd_evt);
708 hci_len = gdm_dev16_to_cpu(phy_dev->
709 get_endian(phy_dev->priv_dev), sdu->len);
710 nic_type = gdm_dev32_to_cpu(phy_dev->
711 get_endian(phy_dev->priv_dev), sdu->nic_type);
61e12104
WK
712
713 if (cmd_evt != LTE_RX_SDU) {
0ec473b5 714 pr_err("rx sdu wrong hci %04x\n", cmd_evt);
61e12104
WK
715 return;
716 }
717 if (hci_len < 12) {
0ec473b5 718 pr_err("rx sdu invalid len %d\n", hci_len);
61e12104
WK
719 return;
720 }
721
722 index = find_dev_index(nic_type);
723 if (index < MAX_NIC_TYPE) {
724 dev = phy_dev->dev[index];
097b4d8c
GK
725 gdm_lte_netif_rx(dev, (char *)sdu->data,
726 (int)(hci_len-12), nic_type);
61e12104 727 } else {
0ec473b5 728 pr_err("rx sdu invalid nic_type :%x\n", nic_type);
61e12104
WK
729 }
730
731 data += ((hci_len+3) & 0xfffc) + HCI_HEADER_SIZE;
732 }
733}
734
735static void gdm_lte_pdn_table(struct net_device *dev, char *buf, int len)
736{
737 struct nic *nic = netdev_priv(dev);
738 struct hci_pdn_table_ind *pdn_table = (struct hci_pdn_table_ind *)buf;
739
740 if (pdn_table->activate) {
741 nic->pdn_table.activate = pdn_table->activate;
04db9c6a
RK
742 nic->pdn_table.dft_eps_id = gdm_dev32_to_cpu(
743 nic->phy_dev->get_endian(
744 nic->phy_dev->priv_dev),
745 pdn_table->dft_eps_id);
746 nic->pdn_table.nic_type = gdm_dev32_to_cpu(
747 nic->phy_dev->get_endian(
748 nic->phy_dev->priv_dev),
749 pdn_table->nic_type);
61e12104 750
0ec473b5
JP
751 netdev_info(dev, "pdn activated, nic_type=0x%x\n",
752 nic->pdn_table.nic_type);
61e12104
WK
753 } else {
754 memset(&nic->pdn_table, 0x00, sizeof(struct pdn_table));
0ec473b5 755 netdev_info(dev, "pdn deactivated\n");
61e12104
WK
756 }
757}
758
759static int gdm_lte_receive_pkt(struct phy_dev *phy_dev, char *buf, int len)
760{
761 struct hci_packet *hci = (struct hci_packet *)buf;
762 struct hci_pdn_table_ind *pdn_table = (struct hci_pdn_table_ind *)buf;
763 struct sdu *sdu;
764 struct net_device *dev;
765 int ret = 0;
766 u16 cmd_evt;
767 u32 nic_type;
768 u8 index;
769
770 if (!len)
771 return ret;
772
097b4d8c
GK
773 cmd_evt = gdm_dev16_to_cpu(phy_dev->get_endian(phy_dev->priv_dev),
774 hci->cmd_evt);
61e12104
WK
775
776 dev = phy_dev->dev[0];
777 if (dev == NULL)
778 return 0;
779
780 switch (cmd_evt) {
781 case LTE_RX_SDU:
782 sdu = (struct sdu *)hci->data;
097b4d8c
GK
783 nic_type = gdm_dev32_to_cpu(phy_dev->
784 get_endian(phy_dev->priv_dev), sdu->nic_type);
61e12104
WK
785 index = find_dev_index(nic_type);
786 dev = phy_dev->dev[index];
787 gdm_lte_netif_rx(dev, hci->data, len, nic_type);
788 break;
789 case LTE_RX_MULTI_SDU:
790 gdm_lte_multi_sdu_pkt(phy_dev, buf, len);
791 break;
792 case LTE_LINK_ON_OFF_INDICATION:
0ec473b5
JP
793 netdev_info(dev, "link %s\n",
794 ((struct hci_connect_ind *)buf)->connect
795 ? "on" : "off");
61e12104
WK
796 break;
797 case LTE_PDN_TABLE_IND:
798 pdn_table = (struct hci_pdn_table_ind *)buf;
097b4d8c
GK
799 nic_type = gdm_dev32_to_cpu(phy_dev->
800 get_endian(phy_dev->priv_dev),
801 pdn_table->nic_type);
61e12104
WK
802 index = find_dev_index(nic_type);
803 dev = phy_dev->dev[index];
804 gdm_lte_pdn_table(dev, buf, len);
805 /* Fall through */
806 default:
807 ret = gdm_lte_event_send(dev, buf, len);
808 break;
809 }
810
811 return ret;
812}
813
814static int rx_complete(void *arg, void *data, int len, int context)
815{
816 struct phy_dev *phy_dev = (struct phy_dev *)arg;
817
818 return gdm_lte_receive_pkt(phy_dev, (char *)data, len);
819}
820
821void start_rx_proc(struct phy_dev *phy_dev)
822{
823 int i;
824
825 for (i = 0; i < MAX_RX_SUBMIT_COUNT; i++)
097b4d8c
GK
826 phy_dev->rcv_func(phy_dev->priv_dev,
827 rx_complete, phy_dev, USB_COMPLETE);
61e12104
WK
828}
829
830static struct net_device_ops gdm_netdev_ops = {
831 .ndo_open = gdm_lte_open,
832 .ndo_stop = gdm_lte_close,
833 .ndo_set_config = gdm_lte_set_config,
834 .ndo_start_xmit = gdm_lte_tx,
835 .ndo_get_stats = gdm_lte_stats,
61e12104
WK
836};
837
838static u8 gdm_lte_macaddr[ETH_ALEN] = {0x00, 0x0a, 0x3b, 0x00, 0x00, 0x00};
839
097b4d8c
GK
840static void form_mac_address(u8 *dev_addr, u8 *nic_src, u8 *nic_dest,
841 u8 *mac_address, u8 index)
61e12104
WK
842{
843 /* Form the dev_addr */
844 if (!mac_address)
845 memcpy(dev_addr, gdm_lte_macaddr, ETH_ALEN);
846 else
847 memcpy(dev_addr, mac_address, ETH_ALEN);
848
097b4d8c
GK
849 /* The last byte of the mac address
850 * should be less than or equal to 0xFC
851 */
61e12104
WK
852 dev_addr[ETH_ALEN-1] += index;
853
097b4d8c
GK
854 /* Create random nic src and copy the first
855 * 3 bytes to be the same as dev_addr
856 */
61e12104
WK
857 random_ether_addr(nic_src);
858 memcpy(nic_src, dev_addr, 3);
859
860 /* Copy the nic_dest from dev_addr*/
861 memcpy(nic_dest, dev_addr, ETH_ALEN);
862}
863
864static void validate_mac_address(u8 *mac_address)
865{
866 /* if zero address or multicast bit set, restore the default value */
867 if (is_zero_ether_addr(mac_address) || (mac_address[0] & 0x01)) {
0ec473b5 868 pr_err("MAC invalid, restoring default\n");
61e12104
WK
869 memcpy(mac_address, gdm_lte_macaddr, 6);
870 }
871}
872
097b4d8c
GK
873int register_lte_device(struct phy_dev *phy_dev,
874 struct device *dev, u8 *mac_address)
61e12104
WK
875{
876 struct nic *nic;
877 struct net_device *net;
878 char pdn_dev_name[16];
879 int ret = 0;
880 u8 index;
881
882 validate_mac_address(mac_address);
883
884 for (index = 0; index < MAX_NIC_TYPE; index++) {
885 /* Create device name lteXpdnX */
886 sprintf(pdn_dev_name, "lte%%dpdn%d", index);
887
888 /* Allocate netdev */
097b4d8c 889 net = alloc_netdev(sizeof(struct nic), pdn_dev_name,
c835a677 890 NET_NAME_UNKNOWN, ether_setup);
61e12104 891 if (net == NULL) {
0ec473b5 892 pr_err("alloc_netdev failed\n");
61e12104
WK
893 ret = -ENOMEM;
894 goto err;
895 }
896 net->netdev_ops = &gdm_netdev_ops;
897 net->flags &= ~IFF_MULTICAST;
898 net->mtu = DEFAULT_MTU_SIZE;
899
900 nic = netdev_priv(net);
901 memset(nic, 0, sizeof(struct nic));
902 nic->netdev = net;
903 nic->phy_dev = phy_dev;
904 nic->nic_id = index;
905
906 form_mac_address(
907 net->dev_addr,
908 nic->src_mac_addr,
909 nic->dest_mac_addr,
910 mac_address,
911 index);
912
913 SET_NETDEV_DEV(net, dev);
914 SET_NETDEV_DEVTYPE(net, &wwan_type);
915
916 ret = register_netdev(net);
917 if (ret)
918 goto err;
919
920 netif_carrier_on(net);
921
922 phy_dev->dev[index] = net;
923 }
924
925 return 0;
926
927err:
928 unregister_lte_device(phy_dev);
929
930 return ret;
931}
932
933void unregister_lte_device(struct phy_dev *phy_dev)
934{
935 struct net_device *net;
936 int index;
937
938 for (index = 0; index < MAX_NIC_TYPE; index++) {
939 net = phy_dev->dev[index];
940 if (net == NULL)
941 continue;
942
943 unregister_netdev(net);
944 free_netdev(net);
945 }
946}