msm_rmnet_smux.c 22 KB

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