Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2011, Code Aurora Forum. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | /* |
| 15 | * RMNET BAM Module. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/string.h> |
| 21 | #include <linux/delay.h> |
| 22 | #include <linux/errno.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/netdevice.h> |
| 26 | #include <linux/etherdevice.h> |
| 27 | #include <linux/skbuff.h> |
| 28 | #include <linux/wakelock.h> |
| 29 | #include <linux/if_arp.h> |
| 30 | #include <linux/msm_rmnet.h> |
Jeff Hugo | 24dfa0c | 2011-10-07 17:55:06 -0600 | [diff] [blame] | 31 | #include <linux/platform_device.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 32 | |
| 33 | #ifdef CONFIG_HAS_EARLYSUSPEND |
| 34 | #include <linux/earlysuspend.h> |
| 35 | #endif |
| 36 | |
| 37 | #include <mach/bam_dmux.h> |
| 38 | |
| 39 | /* Debug message support */ |
| 40 | static int msm_rmnet_bam_debug_mask; |
| 41 | module_param_named(debug_enable, msm_rmnet_bam_debug_mask, |
| 42 | int, S_IRUGO | S_IWUSR | S_IWGRP); |
| 43 | |
| 44 | #define DEBUG_MASK_LVL0 (1U << 0) |
| 45 | #define DEBUG_MASK_LVL1 (1U << 1) |
| 46 | #define DEBUG_MASK_LVL2 (1U << 2) |
| 47 | |
| 48 | #define DBG(m, x...) do { \ |
| 49 | if (msm_rmnet_bam_debug_mask & m) \ |
| 50 | pr_info(x); \ |
| 51 | } while (0) |
| 52 | #define DBG0(x...) DBG(DEBUG_MASK_LVL0, x) |
| 53 | #define DBG1(x...) DBG(DEBUG_MASK_LVL1, x) |
| 54 | #define DBG2(x...) DBG(DEBUG_MASK_LVL2, x) |
| 55 | |
| 56 | /* Configure device instances */ |
| 57 | #define RMNET_DEVICE_COUNT (8) |
| 58 | |
| 59 | /* allow larger frames */ |
| 60 | #define RMNET_DATA_LEN 2000 |
| 61 | |
| 62 | #define DEVICE_ID_INVALID -1 |
| 63 | |
| 64 | #define DEVICE_INACTIVE 0 |
| 65 | #define DEVICE_ACTIVE 1 |
| 66 | |
| 67 | #define HEADROOM_FOR_BAM 8 /* for mux header */ |
| 68 | #define HEADROOM_FOR_QOS 8 |
| 69 | #define TAILROOM 8 /* for padding by mux layer */ |
| 70 | |
| 71 | struct rmnet_private { |
| 72 | struct net_device_stats stats; |
| 73 | uint32_t ch_id; |
| 74 | #ifdef CONFIG_MSM_RMNET_DEBUG |
| 75 | ktime_t last_packet; |
| 76 | unsigned long wakeups_xmit; |
| 77 | unsigned long wakeups_rcv; |
| 78 | unsigned long timeout_us; |
| 79 | #endif |
| 80 | struct sk_buff *skb; |
| 81 | spinlock_t lock; |
| 82 | struct tasklet_struct tsklt; |
| 83 | u32 operation_mode; /* IOCTL specified mode (protocol, QoS header) */ |
| 84 | uint8_t device_up; |
Jeff Hugo | bac7ea2 | 2011-10-24 10:58:48 -0600 | [diff] [blame] | 85 | uint8_t waiting_for_ul; |
Jeff Hugo | 24dfa0c | 2011-10-07 17:55:06 -0600 | [diff] [blame] | 86 | uint8_t in_reset; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 87 | }; |
| 88 | |
Jeff Hugo | bac7ea2 | 2011-10-24 10:58:48 -0600 | [diff] [blame] | 89 | static uint8_t ul_is_connected; |
| 90 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 91 | #ifdef CONFIG_MSM_RMNET_DEBUG |
| 92 | static unsigned long timeout_us; |
| 93 | |
| 94 | #ifdef CONFIG_HAS_EARLYSUSPEND |
| 95 | /* |
| 96 | * If early suspend is enabled then we specify two timeout values, |
| 97 | * screen on (default), and screen is off. |
| 98 | */ |
| 99 | static unsigned long timeout_suspend_us; |
| 100 | static struct device *rmnet0; |
| 101 | |
| 102 | /* Set timeout in us when the screen is off. */ |
| 103 | static ssize_t timeout_suspend_store(struct device *d, |
| 104 | struct device_attribute *attr, |
| 105 | const char *buf, size_t n) |
| 106 | { |
| 107 | timeout_suspend_us = strict_strtoul(buf, NULL, 10); |
| 108 | return n; |
| 109 | } |
| 110 | |
| 111 | static ssize_t timeout_suspend_show(struct device *d, |
| 112 | struct device_attribute *attr, |
| 113 | char *buf) |
| 114 | { |
| 115 | return sprintf(buf, "%lu\n", (unsigned long) timeout_suspend_us); |
| 116 | } |
| 117 | |
| 118 | static DEVICE_ATTR(timeout_suspend, 0664, timeout_suspend_show, |
| 119 | timeout_suspend_store); |
| 120 | |
| 121 | static void rmnet_early_suspend(struct early_suspend *handler) |
| 122 | { |
| 123 | if (rmnet0) { |
| 124 | struct rmnet_private *p = netdev_priv(to_net_dev(rmnet0)); |
| 125 | p->timeout_us = timeout_suspend_us; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | static void rmnet_late_resume(struct early_suspend *handler) |
| 130 | { |
| 131 | if (rmnet0) { |
| 132 | struct rmnet_private *p = netdev_priv(to_net_dev(rmnet0)); |
| 133 | p->timeout_us = timeout_us; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | static struct early_suspend rmnet_power_suspend = { |
| 138 | .suspend = rmnet_early_suspend, |
| 139 | .resume = rmnet_late_resume, |
| 140 | }; |
| 141 | |
| 142 | static int __init rmnet_late_init(void) |
| 143 | { |
| 144 | register_early_suspend(&rmnet_power_suspend); |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | late_initcall(rmnet_late_init); |
| 149 | #endif |
| 150 | |
| 151 | /* Returns 1 if packet caused rmnet to wakeup, 0 otherwise. */ |
| 152 | static int rmnet_cause_wakeup(struct rmnet_private *p) |
| 153 | { |
| 154 | int ret = 0; |
| 155 | ktime_t now; |
| 156 | if (p->timeout_us == 0) /* Check if disabled */ |
| 157 | return 0; |
| 158 | |
| 159 | /* Use real (wall) time. */ |
| 160 | now = ktime_get_real(); |
| 161 | |
| 162 | if (ktime_us_delta(now, p->last_packet) > p->timeout_us) |
| 163 | ret = 1; |
| 164 | |
| 165 | p->last_packet = now; |
| 166 | return ret; |
| 167 | } |
| 168 | |
| 169 | static ssize_t wakeups_xmit_show(struct device *d, |
| 170 | struct device_attribute *attr, |
| 171 | char *buf) |
| 172 | { |
| 173 | struct rmnet_private *p = netdev_priv(to_net_dev(d)); |
| 174 | return sprintf(buf, "%lu\n", p->wakeups_xmit); |
| 175 | } |
| 176 | |
| 177 | DEVICE_ATTR(wakeups_xmit, 0444, wakeups_xmit_show, NULL); |
| 178 | |
| 179 | static ssize_t wakeups_rcv_show(struct device *d, struct device_attribute *attr, |
| 180 | char *buf) |
| 181 | { |
| 182 | struct rmnet_private *p = netdev_priv(to_net_dev(d)); |
| 183 | return sprintf(buf, "%lu\n", p->wakeups_rcv); |
| 184 | } |
| 185 | |
| 186 | DEVICE_ATTR(wakeups_rcv, 0444, wakeups_rcv_show, NULL); |
| 187 | |
| 188 | /* Set timeout in us. */ |
| 189 | static ssize_t timeout_store(struct device *d, struct device_attribute *attr, |
| 190 | const char *buf, size_t n) |
| 191 | { |
| 192 | #ifndef CONFIG_HAS_EARLYSUSPEND |
| 193 | struct rmnet_private *p = netdev_priv(to_net_dev(d)); |
| 194 | p->timeout_us = timeout_us = strict_strtoul(buf, NULL, 10); |
| 195 | #else |
| 196 | /* If using early suspend/resume hooks do not write the value on store. */ |
| 197 | timeout_us = strict_strtoul(buf, NULL, 10); |
| 198 | #endif |
| 199 | return n; |
| 200 | } |
| 201 | |
| 202 | static ssize_t timeout_show(struct device *d, struct device_attribute *attr, |
| 203 | char *buf) |
| 204 | { |
| 205 | struct rmnet_private *p = netdev_priv(to_net_dev(d)); |
| 206 | p = netdev_priv(to_net_dev(d)); |
| 207 | return sprintf(buf, "%lu\n", timeout_us); |
| 208 | } |
| 209 | |
| 210 | DEVICE_ATTR(timeout, 0664, timeout_show, timeout_store); |
| 211 | #endif |
| 212 | |
| 213 | |
| 214 | /* Forward declaration */ |
| 215 | static int rmnet_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd); |
| 216 | |
| 217 | static __be16 rmnet_ip_type_trans(struct sk_buff *skb, struct net_device *dev) |
| 218 | { |
| 219 | __be16 protocol = 0; |
| 220 | |
| 221 | skb->dev = dev; |
| 222 | |
| 223 | /* Determine L3 protocol */ |
| 224 | switch (skb->data[0] & 0xf0) { |
| 225 | case 0x40: |
| 226 | protocol = htons(ETH_P_IP); |
| 227 | break; |
| 228 | case 0x60: |
| 229 | protocol = htons(ETH_P_IPV6); |
| 230 | break; |
| 231 | default: |
| 232 | pr_err("[%s] rmnet_recv() L3 protocol decode error: 0x%02x", |
| 233 | dev->name, skb->data[0] & 0xf0); |
| 234 | /* skb will be dropped in upper layer for unknown protocol */ |
| 235 | } |
| 236 | return protocol; |
| 237 | } |
| 238 | |
| 239 | static int count_this_packet(void *_hdr, int len) |
| 240 | { |
| 241 | struct ethhdr *hdr = _hdr; |
| 242 | |
| 243 | if (len >= ETH_HLEN && hdr->h_proto == htons(ETH_P_ARP)) |
| 244 | return 0; |
| 245 | |
| 246 | return 1; |
| 247 | } |
| 248 | |
| 249 | /* Rx Callback, Called in Work Queue context */ |
| 250 | static void bam_recv_notify(void *dev, struct sk_buff *skb) |
| 251 | { |
| 252 | struct rmnet_private *p = netdev_priv(dev); |
| 253 | unsigned long flags; |
| 254 | u32 opmode; |
| 255 | |
| 256 | if (skb) { |
| 257 | skb->dev = dev; |
| 258 | /* Handle Rx frame format */ |
| 259 | spin_lock_irqsave(&p->lock, flags); |
| 260 | opmode = p->operation_mode; |
| 261 | spin_unlock_irqrestore(&p->lock, flags); |
| 262 | |
| 263 | if (RMNET_IS_MODE_IP(opmode)) { |
| 264 | /* Driver in IP mode */ |
| 265 | skb->protocol = rmnet_ip_type_trans(skb, dev); |
| 266 | } else { |
| 267 | /* Driver in Ethernet mode */ |
| 268 | skb->protocol = eth_type_trans(skb, dev); |
| 269 | } |
| 270 | if (RMNET_IS_MODE_IP(opmode) || |
| 271 | count_this_packet(skb->data, skb->len)) { |
| 272 | #ifdef CONFIG_MSM_RMNET_DEBUG |
| 273 | p->wakeups_rcv += rmnet_cause_wakeup(p); |
| 274 | #endif |
| 275 | p->stats.rx_packets++; |
| 276 | p->stats.rx_bytes += skb->len; |
| 277 | } |
| 278 | DBG1("[%s] Rx packet #%lu len=%d\n", |
| 279 | ((struct net_device *)dev)->name, |
| 280 | p->stats.rx_packets, skb->len); |
| 281 | |
| 282 | /* Deliver to network stack */ |
| 283 | netif_rx(skb); |
| 284 | } else |
| 285 | pr_err("[%s] %s: No skb received", |
| 286 | ((struct net_device *)dev)->name, __func__); |
| 287 | } |
| 288 | |
| 289 | static int _rmnet_xmit(struct sk_buff *skb, struct net_device *dev) |
| 290 | { |
| 291 | struct rmnet_private *p = netdev_priv(dev); |
| 292 | int bam_ret; |
| 293 | struct QMI_QOS_HDR_S *qmih; |
| 294 | u32 opmode; |
| 295 | unsigned long flags; |
| 296 | |
| 297 | /* For QoS mode, prepend QMI header and assign flow ID from skb->mark */ |
| 298 | spin_lock_irqsave(&p->lock, flags); |
| 299 | opmode = p->operation_mode; |
| 300 | spin_unlock_irqrestore(&p->lock, flags); |
| 301 | |
| 302 | if (RMNET_IS_MODE_QOS(opmode)) { |
| 303 | qmih = (struct QMI_QOS_HDR_S *) |
| 304 | skb_push(skb, sizeof(struct QMI_QOS_HDR_S)); |
| 305 | qmih->version = 1; |
| 306 | qmih->flags = 0; |
| 307 | qmih->flow_id = skb->mark; |
| 308 | } |
| 309 | |
| 310 | dev->trans_start = jiffies; |
| 311 | bam_ret = msm_bam_dmux_write(p->ch_id, skb); |
| 312 | |
| 313 | if (bam_ret != 0) { |
| 314 | pr_err("[%s] %s: write returned error %d", |
| 315 | dev->name, __func__, bam_ret); |
| 316 | goto xmit_out; |
| 317 | } |
| 318 | |
| 319 | if (count_this_packet(skb->data, skb->len)) { |
| 320 | p->stats.tx_packets++; |
| 321 | p->stats.tx_bytes += skb->len; |
| 322 | #ifdef CONFIG_MSM_RMNET_DEBUG |
| 323 | p->wakeups_xmit += rmnet_cause_wakeup(p); |
| 324 | #endif |
| 325 | } |
| 326 | DBG1("[%s] Tx packet #%lu len=%d mark=0x%x\n", |
| 327 | dev->name, p->stats.tx_packets, skb->len, skb->mark); |
| 328 | |
| 329 | return 0; |
| 330 | xmit_out: |
| 331 | /* data xmited, safe to release skb */ |
| 332 | dev_kfree_skb_any(skb); |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | static void bam_write_done(void *dev, struct sk_buff *skb) |
| 337 | { |
| 338 | DBG1("%s: write complete\n", __func__); |
| 339 | dev_kfree_skb_any(skb); |
| 340 | netif_wake_queue(dev); |
| 341 | } |
| 342 | |
Jeff Hugo | 1c4531c | 2011-08-02 14:55:37 -0600 | [diff] [blame] | 343 | static void bam_notify(void *dev, int event, unsigned long data) |
| 344 | { |
Jeff Hugo | bac7ea2 | 2011-10-24 10:58:48 -0600 | [diff] [blame] | 345 | struct rmnet_private *p = netdev_priv(dev); |
| 346 | |
Jeff Hugo | 1c4531c | 2011-08-02 14:55:37 -0600 | [diff] [blame] | 347 | switch (event) { |
| 348 | case BAM_DMUX_RECEIVE: |
| 349 | bam_recv_notify(dev, (struct sk_buff *)(data)); |
| 350 | break; |
| 351 | case BAM_DMUX_WRITE_DONE: |
| 352 | bam_write_done(dev, (struct sk_buff *)(data)); |
| 353 | break; |
Jeff Hugo | bac7ea2 | 2011-10-24 10:58:48 -0600 | [diff] [blame] | 354 | case BAM_DMUX_UL_CONNECTED: |
| 355 | ul_is_connected = 1; |
| 356 | if (p->waiting_for_ul) { |
| 357 | netif_wake_queue(dev); |
| 358 | p->waiting_for_ul = 0; |
| 359 | } |
| 360 | break; |
| 361 | case BAM_DMUX_UL_DISCONNECTED: |
| 362 | ul_is_connected = 0; |
| 363 | break; |
Jeff Hugo | 1c4531c | 2011-08-02 14:55:37 -0600 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 367 | static int __rmnet_open(struct net_device *dev) |
| 368 | { |
| 369 | int r; |
| 370 | struct rmnet_private *p = netdev_priv(dev); |
| 371 | |
| 372 | DBG0("[%s] __rmnet_open()\n", dev->name); |
| 373 | |
| 374 | if (!p->device_up) { |
Jeff Hugo | 1c4531c | 2011-08-02 14:55:37 -0600 | [diff] [blame] | 375 | r = msm_bam_dmux_open(p->ch_id, dev, bam_notify); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 376 | |
| 377 | if (r < 0) |
| 378 | return -ENODEV; |
| 379 | } |
| 380 | |
| 381 | p->device_up = DEVICE_ACTIVE; |
| 382 | return 0; |
| 383 | } |
| 384 | |
| 385 | static int rmnet_open(struct net_device *dev) |
| 386 | { |
| 387 | int rc = 0; |
| 388 | |
| 389 | DBG0("[%s] rmnet_open()\n", dev->name); |
| 390 | |
| 391 | rc = __rmnet_open(dev); |
| 392 | |
| 393 | if (rc == 0) |
| 394 | netif_start_queue(dev); |
| 395 | |
| 396 | return rc; |
| 397 | } |
| 398 | |
| 399 | |
| 400 | static int __rmnet_close(struct net_device *dev) |
| 401 | { |
| 402 | struct rmnet_private *p = netdev_priv(dev); |
| 403 | int rc = 0; |
| 404 | |
| 405 | if (p->device_up) { |
| 406 | /* do not close rmnet port once up, this causes |
| 407 | remote side to hang if tried to open again */ |
| 408 | p->device_up = DEVICE_INACTIVE; |
| 409 | return rc; |
| 410 | } else |
| 411 | return -EBADF; |
| 412 | } |
| 413 | |
| 414 | |
| 415 | static int rmnet_stop(struct net_device *dev) |
| 416 | { |
| 417 | DBG0("[%s] rmnet_stop()\n", dev->name); |
| 418 | |
| 419 | __rmnet_close(dev); |
| 420 | netif_stop_queue(dev); |
| 421 | |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | static int rmnet_change_mtu(struct net_device *dev, int new_mtu) |
| 426 | { |
| 427 | if (0 > new_mtu || RMNET_DATA_LEN < new_mtu) |
| 428 | return -EINVAL; |
| 429 | |
| 430 | DBG0("[%s] MTU change: old=%d new=%d\n", |
| 431 | dev->name, dev->mtu, new_mtu); |
| 432 | dev->mtu = new_mtu; |
| 433 | |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | static int rmnet_xmit(struct sk_buff *skb, struct net_device *dev) |
| 438 | { |
Jeff Hugo | bac7ea2 | 2011-10-24 10:58:48 -0600 | [diff] [blame] | 439 | struct rmnet_private *p = netdev_priv(dev); |
| 440 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 441 | if (netif_queue_stopped(dev)) { |
| 442 | pr_err("[%s]fatal: rmnet_xmit called when " |
| 443 | "netif_queue is stopped", dev->name); |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | netif_stop_queue(dev); |
Jeff Hugo | bac7ea2 | 2011-10-24 10:58:48 -0600 | [diff] [blame] | 448 | if (!ul_is_connected) { |
| 449 | p->waiting_for_ul = 1; |
| 450 | msm_bam_dmux_kickoff_ul_wakeup(); |
| 451 | return NETDEV_TX_BUSY; |
| 452 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 453 | _rmnet_xmit(skb, dev); |
| 454 | |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | static struct net_device_stats *rmnet_get_stats(struct net_device *dev) |
| 459 | { |
| 460 | struct rmnet_private *p = netdev_priv(dev); |
| 461 | return &p->stats; |
| 462 | } |
| 463 | |
| 464 | static void rmnet_set_multicast_list(struct net_device *dev) |
| 465 | { |
| 466 | } |
| 467 | |
| 468 | static void rmnet_tx_timeout(struct net_device *dev) |
| 469 | { |
| 470 | pr_warning("[%s] rmnet_tx_timeout()\n", dev->name); |
| 471 | } |
| 472 | |
| 473 | static const struct net_device_ops rmnet_ops_ether = { |
| 474 | .ndo_open = rmnet_open, |
| 475 | .ndo_stop = rmnet_stop, |
| 476 | .ndo_start_xmit = rmnet_xmit, |
| 477 | .ndo_get_stats = rmnet_get_stats, |
| 478 | .ndo_set_multicast_list = rmnet_set_multicast_list, |
| 479 | .ndo_tx_timeout = rmnet_tx_timeout, |
| 480 | .ndo_do_ioctl = rmnet_ioctl, |
| 481 | .ndo_change_mtu = rmnet_change_mtu, |
| 482 | .ndo_set_mac_address = eth_mac_addr, |
| 483 | .ndo_validate_addr = eth_validate_addr, |
| 484 | }; |
| 485 | |
| 486 | static const struct net_device_ops rmnet_ops_ip = { |
| 487 | .ndo_open = rmnet_open, |
| 488 | .ndo_stop = rmnet_stop, |
| 489 | .ndo_start_xmit = rmnet_xmit, |
| 490 | .ndo_get_stats = rmnet_get_stats, |
| 491 | .ndo_set_multicast_list = rmnet_set_multicast_list, |
| 492 | .ndo_tx_timeout = rmnet_tx_timeout, |
| 493 | .ndo_do_ioctl = rmnet_ioctl, |
| 494 | .ndo_change_mtu = rmnet_change_mtu, |
| 495 | .ndo_set_mac_address = 0, |
| 496 | .ndo_validate_addr = 0, |
| 497 | }; |
| 498 | |
| 499 | static int rmnet_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) |
| 500 | { |
| 501 | struct rmnet_private *p = netdev_priv(dev); |
| 502 | u32 old_opmode = p->operation_mode; |
| 503 | unsigned long flags; |
| 504 | int prev_mtu = dev->mtu; |
| 505 | int rc = 0; |
| 506 | |
| 507 | /* Process IOCTL command */ |
| 508 | switch (cmd) { |
| 509 | case RMNET_IOCTL_SET_LLP_ETHERNET: /* Set Ethernet protocol */ |
| 510 | /* Perform Ethernet config only if in IP mode currently*/ |
| 511 | if (p->operation_mode & RMNET_MODE_LLP_IP) { |
| 512 | ether_setup(dev); |
| 513 | random_ether_addr(dev->dev_addr); |
| 514 | dev->mtu = prev_mtu; |
| 515 | |
| 516 | dev->netdev_ops = &rmnet_ops_ether; |
| 517 | spin_lock_irqsave(&p->lock, flags); |
| 518 | p->operation_mode &= ~RMNET_MODE_LLP_IP; |
| 519 | p->operation_mode |= RMNET_MODE_LLP_ETH; |
| 520 | spin_unlock_irqrestore(&p->lock, flags); |
| 521 | DBG0("[%s] rmnet_ioctl(): " |
| 522 | "set Ethernet protocol mode\n", |
| 523 | dev->name); |
| 524 | } |
| 525 | break; |
| 526 | |
| 527 | case RMNET_IOCTL_SET_LLP_IP: /* Set RAWIP protocol */ |
| 528 | /* Perform IP config only if in Ethernet mode currently*/ |
| 529 | if (p->operation_mode & RMNET_MODE_LLP_ETH) { |
| 530 | |
| 531 | /* Undo config done in ether_setup() */ |
| 532 | dev->header_ops = 0; /* No header */ |
| 533 | dev->type = ARPHRD_RAWIP; |
| 534 | dev->hard_header_len = 0; |
| 535 | dev->mtu = prev_mtu; |
| 536 | dev->addr_len = 0; |
| 537 | dev->flags &= ~(IFF_BROADCAST| |
| 538 | IFF_MULTICAST); |
| 539 | |
| 540 | dev->needed_headroom = HEADROOM_FOR_BAM + |
| 541 | HEADROOM_FOR_QOS; |
| 542 | dev->needed_tailroom = TAILROOM; |
| 543 | dev->netdev_ops = &rmnet_ops_ip; |
| 544 | spin_lock_irqsave(&p->lock, flags); |
| 545 | p->operation_mode &= ~RMNET_MODE_LLP_ETH; |
| 546 | p->operation_mode |= RMNET_MODE_LLP_IP; |
| 547 | spin_unlock_irqrestore(&p->lock, flags); |
| 548 | DBG0("[%s] rmnet_ioctl(): " |
| 549 | "set IP protocol mode\n", |
| 550 | dev->name); |
| 551 | } |
| 552 | break; |
| 553 | |
| 554 | case RMNET_IOCTL_GET_LLP: /* Get link protocol state */ |
| 555 | ifr->ifr_ifru.ifru_data = |
| 556 | (void *)(p->operation_mode & |
| 557 | (RMNET_MODE_LLP_ETH|RMNET_MODE_LLP_IP)); |
| 558 | break; |
| 559 | |
| 560 | case RMNET_IOCTL_SET_QOS_ENABLE: /* Set QoS header enabled */ |
| 561 | spin_lock_irqsave(&p->lock, flags); |
| 562 | p->operation_mode |= RMNET_MODE_QOS; |
| 563 | spin_unlock_irqrestore(&p->lock, flags); |
| 564 | DBG0("[%s] rmnet_ioctl(): set QMI QOS header enable\n", |
| 565 | dev->name); |
| 566 | break; |
| 567 | |
| 568 | case RMNET_IOCTL_SET_QOS_DISABLE: /* Set QoS header disabled */ |
| 569 | spin_lock_irqsave(&p->lock, flags); |
| 570 | p->operation_mode &= ~RMNET_MODE_QOS; |
| 571 | spin_unlock_irqrestore(&p->lock, flags); |
| 572 | DBG0("[%s] rmnet_ioctl(): set QMI QOS header disable\n", |
| 573 | dev->name); |
| 574 | break; |
| 575 | |
| 576 | case RMNET_IOCTL_GET_QOS: /* Get QoS header state */ |
| 577 | ifr->ifr_ifru.ifru_data = |
| 578 | (void *)(p->operation_mode & RMNET_MODE_QOS); |
| 579 | break; |
| 580 | |
| 581 | case RMNET_IOCTL_GET_OPMODE: /* Get operation mode */ |
| 582 | ifr->ifr_ifru.ifru_data = (void *)p->operation_mode; |
| 583 | break; |
| 584 | |
| 585 | case RMNET_IOCTL_OPEN: /* Open transport port */ |
| 586 | rc = __rmnet_open(dev); |
| 587 | DBG0("[%s] rmnet_ioctl(): open transport port\n", |
| 588 | dev->name); |
| 589 | break; |
| 590 | |
| 591 | case RMNET_IOCTL_CLOSE: /* Close transport port */ |
| 592 | rc = __rmnet_close(dev); |
| 593 | DBG0("[%s] rmnet_ioctl(): close transport port\n", |
| 594 | dev->name); |
| 595 | break; |
| 596 | |
| 597 | default: |
| 598 | pr_err("[%s] error: rmnet_ioct called for unsupported cmd[%d]", |
| 599 | dev->name, cmd); |
| 600 | return -EINVAL; |
| 601 | } |
| 602 | |
| 603 | DBG2("[%s] %s: cmd=0x%x opmode old=0x%08x new=0x%08x\n", |
| 604 | dev->name, __func__, cmd, old_opmode, p->operation_mode); |
| 605 | return rc; |
| 606 | } |
| 607 | |
| 608 | static void __init rmnet_setup(struct net_device *dev) |
| 609 | { |
| 610 | /* Using Ethernet mode by default */ |
| 611 | dev->netdev_ops = &rmnet_ops_ether; |
| 612 | ether_setup(dev); |
| 613 | |
| 614 | /* set this after calling ether_setup */ |
| 615 | dev->mtu = RMNET_DATA_LEN; |
| 616 | dev->needed_headroom = HEADROOM_FOR_BAM + HEADROOM_FOR_QOS ; |
| 617 | dev->needed_tailroom = TAILROOM; |
| 618 | random_ether_addr(dev->dev_addr); |
| 619 | |
| 620 | dev->watchdog_timeo = 1000; /* 10 seconds? */ |
| 621 | } |
| 622 | |
Jeff Hugo | 24dfa0c | 2011-10-07 17:55:06 -0600 | [diff] [blame] | 623 | static struct net_device *netdevs[RMNET_DEVICE_COUNT]; |
| 624 | static struct platform_driver bam_rmnet_drivers[RMNET_DEVICE_COUNT]; |
| 625 | |
| 626 | static int bam_rmnet_probe(struct platform_device *pdev) |
| 627 | { |
| 628 | int i; |
| 629 | char name[BAM_DMUX_CH_NAME_MAX_LEN]; |
| 630 | struct rmnet_private *p; |
| 631 | |
| 632 | for (i = 0; i < RMNET_DEVICE_COUNT; ++i) { |
| 633 | scnprintf(name, BAM_DMUX_CH_NAME_MAX_LEN, "bam_dmux_ch_%d", i); |
| 634 | if (!strncmp(pdev->name, name, BAM_DMUX_CH_NAME_MAX_LEN)) |
| 635 | break; |
| 636 | } |
| 637 | |
| 638 | p = netdev_priv(netdevs[i]); |
| 639 | if (p->in_reset) { |
| 640 | p->in_reset = 0; |
| 641 | msm_bam_dmux_open(p->ch_id, netdevs[i], bam_notify); |
| 642 | netif_carrier_on(netdevs[i]); |
| 643 | netif_start_queue(netdevs[i]); |
| 644 | } |
| 645 | |
| 646 | return 0; |
| 647 | } |
| 648 | |
| 649 | static int bam_rmnet_remove(struct platform_device *pdev) |
| 650 | { |
| 651 | int i; |
| 652 | char name[BAM_DMUX_CH_NAME_MAX_LEN]; |
| 653 | struct rmnet_private *p; |
| 654 | |
| 655 | for (i = 0; i < RMNET_DEVICE_COUNT; ++i) { |
| 656 | scnprintf(name, BAM_DMUX_CH_NAME_MAX_LEN, "bam_dmux_ch_%d", i); |
| 657 | if (!strncmp(pdev->name, name, BAM_DMUX_CH_NAME_MAX_LEN)) |
| 658 | break; |
| 659 | } |
| 660 | |
| 661 | p = netdev_priv(netdevs[i]); |
| 662 | p->in_reset = 1; |
| 663 | msm_bam_dmux_close(p->ch_id); |
| 664 | netif_carrier_off(netdevs[i]); |
| 665 | netif_stop_queue(netdevs[i]); |
| 666 | return 0; |
| 667 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 668 | |
| 669 | static int __init rmnet_init(void) |
| 670 | { |
| 671 | int ret; |
| 672 | struct device *d; |
| 673 | struct net_device *dev; |
| 674 | struct rmnet_private *p; |
| 675 | unsigned n; |
Jeff Hugo | 24dfa0c | 2011-10-07 17:55:06 -0600 | [diff] [blame] | 676 | char *tempname; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 677 | |
| 678 | pr_info("%s: BAM devices[%d]\n", __func__, RMNET_DEVICE_COUNT); |
| 679 | |
| 680 | #ifdef CONFIG_MSM_RMNET_DEBUG |
| 681 | timeout_us = 0; |
| 682 | #ifdef CONFIG_HAS_EARLYSUSPEND |
| 683 | timeout_suspend_us = 0; |
| 684 | #endif |
| 685 | #endif |
| 686 | |
| 687 | for (n = 0; n < RMNET_DEVICE_COUNT; n++) { |
| 688 | dev = alloc_netdev(sizeof(struct rmnet_private), |
| 689 | "rmnet%d", rmnet_setup); |
| 690 | |
| 691 | if (!dev) |
| 692 | return -ENOMEM; |
| 693 | |
Jeff Hugo | 24dfa0c | 2011-10-07 17:55:06 -0600 | [diff] [blame] | 694 | netdevs[n] = dev; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 695 | d = &(dev->dev); |
| 696 | p = netdev_priv(dev); |
| 697 | /* Initial config uses Ethernet */ |
| 698 | p->operation_mode = RMNET_MODE_LLP_ETH; |
| 699 | p->ch_id = n; |
Jeff Hugo | bac7ea2 | 2011-10-24 10:58:48 -0600 | [diff] [blame] | 700 | p->waiting_for_ul = 0; |
Jeff Hugo | 24dfa0c | 2011-10-07 17:55:06 -0600 | [diff] [blame] | 701 | p->in_reset = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 702 | spin_lock_init(&p->lock); |
| 703 | #ifdef CONFIG_MSM_RMNET_DEBUG |
| 704 | p->timeout_us = timeout_us; |
| 705 | p->wakeups_xmit = p->wakeups_rcv = 0; |
| 706 | #endif |
| 707 | |
| 708 | ret = register_netdev(dev); |
| 709 | if (ret) { |
| 710 | free_netdev(dev); |
| 711 | return ret; |
| 712 | } |
| 713 | |
| 714 | #ifdef CONFIG_MSM_RMNET_DEBUG |
| 715 | if (device_create_file(d, &dev_attr_timeout)) |
| 716 | continue; |
| 717 | if (device_create_file(d, &dev_attr_wakeups_xmit)) |
| 718 | continue; |
| 719 | if (device_create_file(d, &dev_attr_wakeups_rcv)) |
| 720 | continue; |
| 721 | #ifdef CONFIG_HAS_EARLYSUSPEND |
| 722 | if (device_create_file(d, &dev_attr_timeout_suspend)) |
| 723 | continue; |
| 724 | |
| 725 | /* Only care about rmnet0 for suspend/resume tiemout hooks. */ |
| 726 | if (n == 0) |
| 727 | rmnet0 = d; |
| 728 | #endif |
| 729 | #endif |
Jeff Hugo | 24dfa0c | 2011-10-07 17:55:06 -0600 | [diff] [blame] | 730 | bam_rmnet_drivers[n].probe = bam_rmnet_probe; |
| 731 | bam_rmnet_drivers[n].remove = bam_rmnet_remove; |
| 732 | tempname = kmalloc(BAM_DMUX_CH_NAME_MAX_LEN, GFP_KERNEL); |
| 733 | if (tempname == NULL) |
| 734 | return -ENOMEM; |
| 735 | scnprintf(tempname, BAM_DMUX_CH_NAME_MAX_LEN, "bam_dmux_ch_%d", |
| 736 | n); |
| 737 | bam_rmnet_drivers[n].driver.name = tempname; |
| 738 | bam_rmnet_drivers[n].driver.owner = THIS_MODULE; |
| 739 | ret = platform_driver_register(&bam_rmnet_drivers[n]); |
Eric Holmberg | bd4c384 | 2011-11-08 13:48:19 -0700 | [diff] [blame^] | 740 | if (ret) { |
| 741 | pr_err("%s: registration failed n=%d rc=%d\n", |
| 742 | __func__, n, ret); |
Jeff Hugo | 24dfa0c | 2011-10-07 17:55:06 -0600 | [diff] [blame] | 743 | return ret; |
Eric Holmberg | bd4c384 | 2011-11-08 13:48:19 -0700 | [diff] [blame^] | 744 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 745 | } |
| 746 | return 0; |
| 747 | } |
| 748 | |
| 749 | module_init(rmnet_init); |
| 750 | MODULE_DESCRIPTION("MSM RMNET BAM TRANSPORT"); |
| 751 | MODULE_LICENSE("GPL v2"); |
| 752 | |