rmnet_usb_data.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. /* Copyright (c) 2011-2014, 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. #include <linux/module.h>
  13. #include <linux/mii.h>
  14. #include <linux/if_arp.h>
  15. #include <linux/etherdevice.h>
  16. #include <linux/debugfs.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/usb.h>
  19. #include <linux/ratelimit.h>
  20. #include <linux/usb/usbnet.h>
  21. #include <linux/msm_rmnet.h>
  22. #include "rmnet_usb.h"
  23. #define RMNET_DATA_LEN 2000
  24. #define RMNET_HEADROOM_W_MUX (sizeof(struct mux_hdr) + \
  25. sizeof(struct QMI_QOS_HDR_S))
  26. #define RMNET_HEADROOM sizeof(struct QMI_QOS_HDR_S)
  27. #define RMNET_TAILROOM MAX_PAD_BYTES(4);
  28. static unsigned int no_rmnet_devs = 1;
  29. module_param(no_rmnet_devs, uint, S_IRUGO | S_IWUSR);
  30. unsigned int no_rmnet_insts_per_dev = 4;
  31. module_param(no_rmnet_insts_per_dev, uint, S_IRUGO | S_IWUSR);
  32. /*
  33. * To support mux on multiple devices, bit position represents device
  34. * and value represnts if mux is enabled or disabled.
  35. * e.g. bit 0: mdm over HSIC, bit1: mdm over hsusb
  36. */
  37. static unsigned long mux_enabled;
  38. module_param(mux_enabled, ulong, S_IRUGO | S_IWUSR);
  39. static unsigned int no_fwd_rmnet_links;
  40. module_param(no_fwd_rmnet_links, uint, S_IRUGO | S_IWUSR);
  41. struct usbnet *unet_list[TOTAL_RMNET_DEV_COUNT];
  42. /* net device name prefixes, indexed by driver_info->data */
  43. static const char * const rmnet_names[] = {
  44. "rmnet_usb%d",
  45. "rmnet2_usb%d",
  46. };
  47. /* net device reverse link name prefixes, indexed by driver_info->data */
  48. static const char * const rev_rmnet_names[] = {
  49. "rev_rmnet_usb%d",
  50. "rev_rmnet2_usb%d",
  51. };
  52. static int data_msg_dbg_mask;
  53. enum {
  54. DEBUG_MASK_LVL0 = 1U << 0,
  55. DEBUG_MASK_LVL1 = 1U << 1,
  56. DEBUG_MASK_LVL2 = 1U << 2,
  57. };
  58. #define DBG(m, x...) do { \
  59. if (data_msg_dbg_mask & m) \
  60. pr_info(x); \
  61. } while (0)
  62. /*echo dbg_mask > /sys/class/net/rmnet_usbx/dbg_mask*/
  63. static ssize_t dbg_mask_store(struct device *d,
  64. struct device_attribute *attr,
  65. const char *buf, size_t n)
  66. {
  67. unsigned int dbg_mask;
  68. struct net_device *dev = to_net_dev(d);
  69. struct usbnet *unet = netdev_priv(dev);
  70. if (!dev)
  71. return -ENODEV;
  72. sscanf(buf, "%u", &dbg_mask);
  73. /*enable dbg msgs for data driver*/
  74. data_msg_dbg_mask = dbg_mask;
  75. /*set default msg level*/
  76. unet->msg_enable = NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK;
  77. /*enable netif_xxx msgs*/
  78. if (dbg_mask & DEBUG_MASK_LVL0)
  79. unet->msg_enable |= NETIF_MSG_IFUP | NETIF_MSG_IFDOWN;
  80. if (dbg_mask & DEBUG_MASK_LVL1)
  81. unet->msg_enable |= NETIF_MSG_TX_ERR | NETIF_MSG_RX_ERR
  82. | NETIF_MSG_TX_QUEUED | NETIF_MSG_TX_DONE
  83. | NETIF_MSG_RX_STATUS;
  84. return n;
  85. }
  86. static ssize_t dbg_mask_show(struct device *d,
  87. struct device_attribute *attr, char *buf)
  88. {
  89. return snprintf(buf, PAGE_SIZE, "%d\n", data_msg_dbg_mask);
  90. }
  91. static DEVICE_ATTR(dbg_mask, 0644, dbg_mask_show, dbg_mask_store);
  92. #define DBG0(x...) DBG(DEBUG_MASK_LVL0, x)
  93. #define DBG1(x...) DBG(DEBUG_MASK_LVL1, x)
  94. #define DBG2(x...) DBG(DEBUG_MASK_LVL2, x)
  95. static int rmnet_data_start(void);
  96. static bool rmnet_data_init;
  97. static int rmnet_init(const char *val, const struct kernel_param *kp)
  98. {
  99. int ret = 0;
  100. if (rmnet_data_init) {
  101. pr_err("dynamic setting rmnet params currently unsupported\n");
  102. return -EINVAL;
  103. }
  104. ret = param_set_bool(val, kp);
  105. if (ret)
  106. return ret;
  107. rmnet_data_start();
  108. return ret;
  109. }
  110. static struct kernel_param_ops rmnet_init_ops = {
  111. .set = rmnet_init,
  112. .get = param_get_bool,
  113. };
  114. module_param_cb(rmnet_data_init, &rmnet_init_ops, &rmnet_data_init,
  115. S_IRUGO | S_IWUSR);
  116. static void rmnet_usb_setup(struct net_device *, int mux_enabled);
  117. static int rmnet_ioctl(struct net_device *, struct ifreq *, int);
  118. static int rmnet_usb_suspend(struct usb_interface *iface, pm_message_t message)
  119. {
  120. struct usbnet *unet = usb_get_intfdata(iface);
  121. struct rmnet_ctrl_dev *dev;
  122. int i, n, rdev_cnt, unet_id;
  123. int retval = 0;
  124. rdev_cnt = unet->data[4] ? no_rmnet_insts_per_dev : 1;
  125. for (n = 0; n < rdev_cnt; n++) {
  126. unet_id = n + unet->driver_info->data * no_rmnet_insts_per_dev;
  127. unet =
  128. unet->data[4] ? unet_list[unet_id] : usb_get_intfdata(iface);
  129. dev = (struct rmnet_ctrl_dev *)unet->data[1];
  130. spin_lock_irq(&unet->txq.lock);
  131. if (work_busy(&dev->get_encap_work) || unet->txq.qlen) {
  132. spin_unlock_irq(&unet->txq.lock);
  133. retval = -EBUSY;
  134. goto abort_suspend;
  135. }
  136. set_bit(EVENT_DEV_ASLEEP, &unet->flags);
  137. spin_unlock_irq(&unet->txq.lock);
  138. usb_kill_anchored_urbs(&dev->rx_submitted);
  139. if (work_busy(&dev->get_encap_work)) {
  140. spin_lock_irq(&unet->txq.lock);
  141. clear_bit(EVENT_DEV_ASLEEP, &unet->flags);
  142. spin_unlock_irq(&unet->txq.lock);
  143. retval = -EBUSY;
  144. goto abort_suspend;
  145. }
  146. }
  147. for (n = 0; n < rdev_cnt; n++) {
  148. unet_id = n + unet->driver_info->data * no_rmnet_insts_per_dev;
  149. unet =
  150. unet->data[4] ? unet_list[unet_id] : usb_get_intfdata(iface);
  151. dev = (struct rmnet_ctrl_dev *)unet->data[1];
  152. netif_device_detach(unet->net);
  153. usbnet_terminate_urbs(unet);
  154. netif_device_attach(unet->net);
  155. }
  156. return 0;
  157. abort_suspend:
  158. for (i = 0; i < n; i++) {
  159. unet_id = i + unet->driver_info->data * no_rmnet_insts_per_dev;
  160. unet =
  161. unet->data[4] ? unet_list[unet_id] : usb_get_intfdata(iface);
  162. dev = (struct rmnet_ctrl_dev *)unet->data[1];
  163. rmnet_usb_ctrl_start_rx(dev);
  164. spin_lock_irq(&unet->txq.lock);
  165. clear_bit(EVENT_DEV_ASLEEP, &unet->flags);
  166. spin_unlock_irq(&unet->txq.lock);
  167. }
  168. return retval;
  169. }
  170. static int rmnet_usb_resume(struct usb_interface *iface)
  171. {
  172. struct usbnet *unet = usb_get_intfdata(iface);
  173. struct rmnet_ctrl_dev *dev;
  174. int n, rdev_cnt, unet_id;
  175. rdev_cnt = unet->data[4] ? no_rmnet_insts_per_dev : 1;
  176. for (n = 0; n < rdev_cnt; n++) {
  177. unet_id = n + unet->driver_info->data * no_rmnet_insts_per_dev;
  178. unet =
  179. unet->data[4] ? unet_list[unet_id] : usb_get_intfdata(iface);
  180. dev = (struct rmnet_ctrl_dev *)unet->data[1];
  181. rmnet_usb_ctrl_start_rx(dev);
  182. usb_set_intfdata(iface, unet);
  183. unet->suspend_count = 1;
  184. usbnet_resume(iface);
  185. }
  186. return 0;
  187. }
  188. static int rmnet_usb_bind(struct usbnet *usbnet, struct usb_interface *iface)
  189. {
  190. struct usb_host_endpoint *endpoint = NULL;
  191. struct usb_host_endpoint *bulk_in = NULL;
  192. struct usb_host_endpoint *bulk_out = NULL;
  193. struct usb_host_endpoint *int_in = NULL;
  194. struct driver_info *info = usbnet->driver_info;
  195. int status = 0;
  196. int i;
  197. int numends;
  198. bool mux;
  199. mux = test_bit(info->data, &mux_enabled);
  200. numends = iface->cur_altsetting->desc.bNumEndpoints;
  201. for (i = 0; i < numends; i++) {
  202. endpoint = iface->cur_altsetting->endpoint + i;
  203. if (!endpoint) {
  204. dev_err(&iface->dev, "%s: invalid endpoint %u\n",
  205. __func__, i);
  206. status = -EINVAL;
  207. goto out;
  208. }
  209. if (usb_endpoint_is_bulk_in(&endpoint->desc))
  210. bulk_in = endpoint;
  211. else if (usb_endpoint_is_bulk_out(&endpoint->desc))
  212. bulk_out = endpoint;
  213. else if (usb_endpoint_is_int_in(&endpoint->desc))
  214. int_in = endpoint;
  215. }
  216. if (!bulk_in || !bulk_out || !int_in) {
  217. dev_err(&iface->dev, "%s: invalid endpoints\n", __func__);
  218. status = -EINVAL;
  219. goto out;
  220. }
  221. usbnet->in = usb_rcvbulkpipe(usbnet->udev,
  222. bulk_in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  223. usbnet->out = usb_sndbulkpipe(usbnet->udev,
  224. bulk_out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  225. usbnet->status = int_in;
  226. /*change name of net device to rmnet_usbx here*/
  227. if (mux && (info->in > no_fwd_rmnet_links))
  228. strlcpy(usbnet->net->name, rev_rmnet_names[info->data],
  229. IFNAMSIZ);
  230. else
  231. strlcpy(usbnet->net->name, rmnet_names[info->data],
  232. IFNAMSIZ);
  233. if (mux)
  234. usbnet->rx_urb_size = usbnet->hard_mtu + sizeof(struct mux_hdr)
  235. + MAX_PAD_BYTES(4);
  236. out:
  237. return status;
  238. }
  239. static int rmnet_usb_data_dmux(struct sk_buff *skb, struct urb *rx_urb)
  240. {
  241. struct mux_hdr *hdr;
  242. size_t pad_len;
  243. size_t total_len;
  244. unsigned int mux_id;
  245. hdr = (struct mux_hdr *)skb->data;
  246. mux_id = hdr->mux_id;
  247. if (!mux_id || mux_id > no_rmnet_insts_per_dev) {
  248. pr_err_ratelimited("%s: Invalid data channel id %u.\n",
  249. __func__, mux_id);
  250. return -EINVAL;
  251. }
  252. pad_len = hdr->padding_info >> MUX_PAD_SHIFT;
  253. if (pad_len > MAX_PAD_BYTES(4)) {
  254. pr_err_ratelimited("%s: Invalid pad len %d\n",
  255. __func__, pad_len);
  256. return -EINVAL;
  257. }
  258. total_len = le16_to_cpu(hdr->pkt_len_w_padding);
  259. if (!total_len || !(total_len - pad_len)) {
  260. pr_err_ratelimited("%s: Invalid pkt length %d\n", __func__,
  261. total_len);
  262. return -EINVAL;
  263. }
  264. skb->data = (unsigned char *)(hdr + 1);
  265. skb_reset_tail_pointer(skb);
  266. rx_urb->actual_length = total_len - pad_len;
  267. return mux_id - 1;
  268. }
  269. static void rmnet_usb_data_mux(struct sk_buff *skb, unsigned int id)
  270. {
  271. struct mux_hdr *hdr;
  272. size_t len;
  273. hdr = (struct mux_hdr *)skb_push(skb, sizeof(struct mux_hdr));
  274. hdr->mux_id = id + 1;
  275. len = skb->len - sizeof(struct mux_hdr);
  276. /*add padding if len is not 4 byte aligned*/
  277. skb_put(skb, ALIGN(len, 4) - len);
  278. hdr->pkt_len_w_padding = cpu_to_le16(skb->len - sizeof(struct mux_hdr));
  279. hdr->padding_info = (ALIGN(len, 4) - len) << MUX_PAD_SHIFT;
  280. }
  281. static struct sk_buff *rmnet_usb_tx_fixup(struct usbnet *dev,
  282. struct sk_buff *skb, gfp_t flags)
  283. {
  284. struct QMI_QOS_HDR_S *qmih;
  285. if (test_bit(RMNET_MODE_QOS, &dev->data[0])) {
  286. qmih = (struct QMI_QOS_HDR_S *)
  287. skb_push(skb, sizeof(struct QMI_QOS_HDR_S));
  288. qmih->version = 1;
  289. qmih->flags = 0;
  290. qmih->flow_id = skb->mark;
  291. }
  292. if (dev->data[4])
  293. rmnet_usb_data_mux(skb, dev->data[3]);
  294. DBG1("[%s] Tx packet #%lu len=%d mark=0x%x\n",
  295. dev->net->name, dev->net->stats.tx_packets, skb->len, skb->mark);
  296. return skb;
  297. }
  298. static __be16 rmnet_ip_type_trans(struct sk_buff *skb)
  299. {
  300. __be16 protocol = 0;
  301. switch (skb->data[0] & 0xf0) {
  302. case 0x40:
  303. protocol = htons(ETH_P_IP);
  304. break;
  305. case 0x60:
  306. protocol = htons(ETH_P_IPV6);
  307. break;
  308. default:
  309. pr_err("[%s] rmnet_recv() L3 protocol decode error: 0x%02x",
  310. dev->name, skb->data[0] & 0xf0);
  311. }
  312. return protocol;
  313. }
  314. static void rmnet_usb_rx_complete(struct urb *rx_urb)
  315. {
  316. struct sk_buff *skb = (struct sk_buff *) rx_urb->context;
  317. struct skb_data *entry = (struct skb_data *) skb->cb;
  318. struct usbnet *dev = entry->dev;
  319. unsigned int unet_offset;
  320. unsigned int unet_id;
  321. int mux_id;
  322. unet_offset = dev->driver_info->data * no_rmnet_insts_per_dev;
  323. if (!rx_urb->status && dev->data[4]) {
  324. mux_id = rmnet_usb_data_dmux(skb, rx_urb);
  325. if (mux_id < 0) {
  326. /*resubmit urb and free skb in rx_complete*/
  327. rx_urb->status = -EINVAL;
  328. } else {
  329. /*map urb to actual network iface based on mux id*/
  330. unet_id = unet_offset + mux_id;
  331. skb->dev = unet_list[unet_id]->net;
  332. }
  333. }
  334. rx_complete(rx_urb);
  335. }
  336. static int rmnet_usb_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  337. {
  338. if (test_bit(RMNET_MODE_LLP_IP, &dev->data[0]))
  339. skb->protocol = rmnet_ip_type_trans(skb);
  340. else /*set zero for eth mode*/
  341. skb->protocol = 0;
  342. DBG1("[%s] Rx packet #%lu len=%d\n",
  343. dev->net->name, dev->net->stats.rx_packets, skb->len);
  344. return 1;
  345. }
  346. static int rmnet_usb_manage_power(struct usbnet *dev, int on)
  347. {
  348. dev->intf->needs_remote_wakeup = on;
  349. return 0;
  350. }
  351. static int rmnet_change_mtu(struct net_device *dev, int new_mtu)
  352. {
  353. if (0 > new_mtu || RMNET_DATA_LEN < new_mtu)
  354. return -EINVAL;
  355. DBG0("[%s] MTU change: old=%d new=%d\n", dev->name, dev->mtu, new_mtu);
  356. dev->mtu = new_mtu;
  357. return 0;
  358. }
  359. static struct net_device_stats *rmnet_get_stats(struct net_device *dev)
  360. {
  361. return &dev->stats;
  362. }
  363. static const struct net_device_ops rmnet_usb_ops_ether = {
  364. .ndo_open = usbnet_open,
  365. .ndo_stop = usbnet_stop,
  366. .ndo_start_xmit = usbnet_start_xmit,
  367. .ndo_get_stats = rmnet_get_stats,
  368. /*.ndo_set_multicast_list = rmnet_set_multicast_list,*/
  369. .ndo_tx_timeout = usbnet_tx_timeout,
  370. .ndo_do_ioctl = rmnet_ioctl,
  371. .ndo_change_mtu = usbnet_change_mtu,
  372. .ndo_set_mac_address = eth_mac_addr,
  373. .ndo_validate_addr = eth_validate_addr,
  374. };
  375. static const struct net_device_ops rmnet_usb_ops_ip = {
  376. .ndo_open = usbnet_open,
  377. .ndo_stop = usbnet_stop,
  378. .ndo_start_xmit = usbnet_start_xmit,
  379. .ndo_get_stats = rmnet_get_stats,
  380. /*.ndo_set_multicast_list = rmnet_set_multicast_list,*/
  381. .ndo_tx_timeout = usbnet_tx_timeout,
  382. .ndo_do_ioctl = rmnet_ioctl,
  383. .ndo_change_mtu = rmnet_change_mtu,
  384. .ndo_set_mac_address = 0,
  385. .ndo_validate_addr = 0,
  386. };
  387. static int rmnet_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  388. {
  389. struct usbnet *unet = netdev_priv(dev);
  390. u32 old_opmode;
  391. int prev_mtu = dev->mtu;
  392. int rc = 0;
  393. old_opmode = unet->data[0]; /*data[0] saves operation mode*/
  394. /* Process IOCTL command */
  395. switch (cmd) {
  396. case RMNET_IOCTL_SET_LLP_ETHERNET: /*Set Ethernet protocol*/
  397. /* Perform Ethernet config only if in IP mode currently*/
  398. if (test_bit(RMNET_MODE_LLP_IP, &unet->data[0])) {
  399. ether_setup(dev);
  400. random_ether_addr(dev->dev_addr);
  401. dev->mtu = prev_mtu;
  402. dev->netdev_ops = &rmnet_usb_ops_ether;
  403. clear_bit(RMNET_MODE_LLP_IP, &unet->data[0]);
  404. set_bit(RMNET_MODE_LLP_ETH, &unet->data[0]);
  405. DBG0("[%s] rmnet_ioctl(): set Ethernet protocol mode\n",
  406. dev->name);
  407. }
  408. break;
  409. case RMNET_IOCTL_SET_LLP_IP: /* Set RAWIP protocol*/
  410. /* Perform IP config only if in Ethernet mode currently*/
  411. if (test_bit(RMNET_MODE_LLP_ETH, &unet->data[0])) {
  412. /* Undo config done in ether_setup() */
  413. dev->header_ops = 0; /* No header */
  414. dev->type = ARPHRD_RAWIP;
  415. dev->hard_header_len = 0;
  416. dev->mtu = prev_mtu;
  417. dev->addr_len = 0;
  418. dev->flags &= ~(IFF_BROADCAST | IFF_MULTICAST);
  419. dev->netdev_ops = &rmnet_usb_ops_ip;
  420. clear_bit(RMNET_MODE_LLP_ETH, &unet->data[0]);
  421. set_bit(RMNET_MODE_LLP_IP, &unet->data[0]);
  422. DBG0("[%s] rmnet_ioctl(): set IP protocol mode\n",
  423. dev->name);
  424. }
  425. break;
  426. case RMNET_IOCTL_GET_LLP: /* Get link protocol state */
  427. ifr->ifr_ifru.ifru_data = (void *)(unet->data[0]
  428. & (RMNET_MODE_LLP_ETH
  429. | RMNET_MODE_LLP_IP));
  430. break;
  431. case RMNET_IOCTL_SET_QOS_ENABLE: /* Set QoS header enabled*/
  432. set_bit(RMNET_MODE_QOS, &unet->data[0]);
  433. DBG0("[%s] rmnet_ioctl(): set QMI QOS header enable\n",
  434. dev->name);
  435. break;
  436. case RMNET_IOCTL_SET_QOS_DISABLE: /* Set QoS header disabled */
  437. clear_bit(RMNET_MODE_QOS, &unet->data[0]);
  438. DBG0("[%s] rmnet_ioctl(): set QMI QOS header disable\n",
  439. dev->name);
  440. break;
  441. case RMNET_IOCTL_GET_QOS: /* Get QoS header state */
  442. ifr->ifr_ifru.ifru_data = (void *)(unet->data[0]
  443. & RMNET_MODE_QOS);
  444. break;
  445. case RMNET_IOCTL_GET_OPMODE: /* Get operation mode*/
  446. ifr->ifr_ifru.ifru_data = (void *)unet->data[0];
  447. break;
  448. case RMNET_IOCTL_OPEN: /* Open transport port */
  449. rc = usbnet_open(dev);
  450. DBG0("[%s] rmnet_ioctl(): open transport port\n", dev->name);
  451. break;
  452. case RMNET_IOCTL_CLOSE: /* Close transport port*/
  453. rc = usbnet_stop(dev);
  454. DBG0("[%s] rmnet_ioctl(): close transport port\n", dev->name);
  455. break;
  456. default:
  457. dev_dbg(&unet->intf->dev, "[%s] error: rmnet_ioctl called for unsupported cmd[0x%x]\n",
  458. dev->name, cmd);
  459. return -EINVAL;
  460. }
  461. DBG2("[%s] %s: cmd=0x%x opmode old=0x%08x new=0x%08lx\n",
  462. dev->name, __func__, cmd, old_opmode, unet->data[0]);
  463. return rc;
  464. }
  465. static void rmnet_usb_setup(struct net_device *dev, int mux_enabled)
  466. {
  467. /* Using Ethernet mode by default */
  468. dev->netdev_ops = &rmnet_usb_ops_ether;
  469. /* set this after calling ether_setup */
  470. dev->mtu = RMNET_DATA_LEN;
  471. if (mux_enabled) {
  472. dev->needed_headroom = RMNET_HEADROOM_W_MUX;
  473. /*max pad bytes for 4 byte alignment*/
  474. dev->needed_tailroom = RMNET_TAILROOM;
  475. } else {
  476. dev->needed_headroom = RMNET_HEADROOM;
  477. }
  478. random_ether_addr(dev->dev_addr);
  479. dev->watchdog_timeo = 1000; /* 10 seconds? */
  480. }
  481. static int rmnet_usb_data_status(struct seq_file *s, void *unused)
  482. {
  483. struct usbnet *unet = s->private;
  484. seq_printf(s, "RMNET_MODE_LLP_IP: %d\n",
  485. test_bit(RMNET_MODE_LLP_IP, &unet->data[0]));
  486. seq_printf(s, "RMNET_MODE_LLP_ETH: %d\n",
  487. test_bit(RMNET_MODE_LLP_ETH, &unet->data[0]));
  488. seq_printf(s, "RMNET_MODE_QOS: %d\n",
  489. test_bit(RMNET_MODE_QOS, &unet->data[0]));
  490. seq_printf(s, "Net MTU: %u\n", unet->net->mtu);
  491. seq_printf(s, "rx_urb_size: %u\n", unet->rx_urb_size);
  492. seq_printf(s, "rx skb q len: %u\n", unet->rxq.qlen);
  493. seq_printf(s, "rx skb done q len: %u\n", unet->done.qlen);
  494. seq_printf(s, "rx errors: %lu\n", unet->net->stats.rx_errors);
  495. seq_printf(s, "rx over errors: %lu\n",
  496. unet->net->stats.rx_over_errors);
  497. seq_printf(s, "rx length errors: %lu\n",
  498. unet->net->stats.rx_length_errors);
  499. seq_printf(s, "rx packets: %lu\n", unet->net->stats.rx_packets);
  500. seq_printf(s, "rx bytes: %lu\n", unet->net->stats.rx_bytes);
  501. seq_printf(s, "tx skb q len: %u\n", unet->txq.qlen);
  502. seq_printf(s, "tx errors: %lu\n", unet->net->stats.tx_errors);
  503. seq_printf(s, "tx packets: %lu\n", unet->net->stats.tx_packets);
  504. seq_printf(s, "tx bytes: %lu\n", unet->net->stats.tx_bytes);
  505. seq_printf(s, "EVENT_DEV_OPEN: %d\n",
  506. test_bit(EVENT_DEV_OPEN, &unet->flags));
  507. seq_printf(s, "EVENT_TX_HALT: %d\n",
  508. test_bit(EVENT_TX_HALT, &unet->flags));
  509. seq_printf(s, "EVENT_RX_HALT: %d\n",
  510. test_bit(EVENT_RX_HALT, &unet->flags));
  511. seq_printf(s, "EVENT_RX_MEMORY: %d\n",
  512. test_bit(EVENT_RX_MEMORY, &unet->flags));
  513. seq_printf(s, "EVENT_DEV_ASLEEP: %d\n",
  514. test_bit(EVENT_DEV_ASLEEP, &unet->flags));
  515. return 0;
  516. }
  517. static int rmnet_usb_data_status_open(struct inode *inode, struct file *file)
  518. {
  519. return single_open(file, rmnet_usb_data_status, inode->i_private);
  520. }
  521. const struct file_operations rmnet_usb_data_fops = {
  522. .open = rmnet_usb_data_status_open,
  523. .read = seq_read,
  524. .llseek = seq_lseek,
  525. .release = single_release,
  526. };
  527. static int rmnet_usb_data_debugfs_init(struct usbnet *unet)
  528. {
  529. struct dentry *rmnet_usb_data_dbg_root;
  530. struct dentry *rmnet_usb_data_dentry;
  531. rmnet_usb_data_dbg_root = debugfs_create_dir(unet->net->name, NULL);
  532. if (!rmnet_usb_data_dbg_root || IS_ERR(rmnet_usb_data_dbg_root))
  533. return -ENODEV;
  534. rmnet_usb_data_dentry = debugfs_create_file("status",
  535. S_IRUGO | S_IWUSR,
  536. rmnet_usb_data_dbg_root, unet,
  537. &rmnet_usb_data_fops);
  538. if (!rmnet_usb_data_dentry) {
  539. debugfs_remove_recursive(rmnet_usb_data_dbg_root);
  540. return -ENODEV;
  541. }
  542. unet->data[2] = (unsigned long)rmnet_usb_data_dbg_root;
  543. return 0;
  544. }
  545. static void rmnet_usb_data_debugfs_cleanup(struct usbnet *unet)
  546. {
  547. struct dentry *root = (struct dentry *)unet->data[2];
  548. if (root) {
  549. debugfs_remove_recursive(root);
  550. unet->data[2] = 0;
  551. }
  552. }
  553. static int rmnet_usb_probe(struct usb_interface *iface,
  554. const struct usb_device_id *prod)
  555. {
  556. struct usbnet *unet;
  557. struct driver_info *info = (struct driver_info *)prod->driver_info;
  558. struct usb_device *udev;
  559. int status = 0;
  560. unsigned int i, unet_id, rdev_cnt, n = 0;
  561. bool mux;
  562. struct rmnet_ctrl_dev *dev;
  563. udev = interface_to_usbdev(iface);
  564. if (iface->num_altsetting != 1) {
  565. dev_err(&iface->dev, "%s invalid num_altsetting %u\n",
  566. __func__, iface->num_altsetting);
  567. status = -EINVAL;
  568. goto out;
  569. }
  570. mux = test_bit(info->data, &mux_enabled);
  571. rdev_cnt = mux ? no_rmnet_insts_per_dev : 1;
  572. info->in = 0;
  573. for (n = 0; n < rdev_cnt; n++) {
  574. /* Use this filed to increment device count this will be
  575. * used by bind to determin the forward link and reverse
  576. * link network interface names.
  577. */
  578. info->in++;
  579. status = usbnet_probe(iface, prod);
  580. if (status < 0) {
  581. dev_err(&iface->dev, "usbnet_probe failed %d\n",
  582. status);
  583. goto out;
  584. }
  585. unet_id = n + info->data * no_rmnet_insts_per_dev;
  586. unet_list[unet_id] = unet = usb_get_intfdata(iface);
  587. /*store mux id for later access*/
  588. unet->data[3] = n;
  589. /*save mux info for control and usbnet devices*/
  590. unet->data[1] = unet->data[4] = mux;
  591. /*set rmnet operation mode to eth by default*/
  592. set_bit(RMNET_MODE_LLP_ETH, &unet->data[0]);
  593. /*update net device*/
  594. rmnet_usb_setup(unet->net, mux);
  595. /*create /sys/class/net/rmnet_usbx/dbg_mask*/
  596. status = device_create_file(&unet->net->dev,
  597. &dev_attr_dbg_mask);
  598. if (status) {
  599. usbnet_disconnect(iface);
  600. goto out;
  601. }
  602. status = rmnet_usb_ctrl_probe(iface, unet->status, info->data,
  603. &unet->data[1]);
  604. if (status) {
  605. device_remove_file(&unet->net->dev, &dev_attr_dbg_mask);
  606. usbnet_disconnect(iface);
  607. goto out;
  608. }
  609. status = rmnet_usb_data_debugfs_init(unet);
  610. if (status)
  611. dev_dbg(&iface->dev,
  612. "mode debugfs file is not available\n");
  613. }
  614. usb_enable_autosuspend(udev);
  615. if (udev->parent && !udev->parent->parent) {
  616. /* allow modem and roothub to wake up suspended system */
  617. device_set_wakeup_enable(&udev->dev, 1);
  618. device_set_wakeup_enable(&udev->parent->dev, 1);
  619. }
  620. return 0;
  621. out:
  622. for (i = 0; i < n; i++) {
  623. /* This cleanup happens only for MUX case */
  624. unet_id = i + info->data * no_rmnet_insts_per_dev;
  625. unet = unet_list[unet_id];
  626. dev = (struct rmnet_ctrl_dev *)unet->data[1];
  627. rmnet_usb_data_debugfs_cleanup(unet);
  628. rmnet_usb_ctrl_disconnect(dev);
  629. device_remove_file(&unet->net->dev, &dev_attr_dbg_mask);
  630. usb_set_intfdata(iface, unet_list[unet_id]);
  631. usbnet_disconnect(iface);
  632. unet_list[unet_id] = NULL;
  633. }
  634. return status;
  635. }
  636. static void rmnet_usb_disconnect(struct usb_interface *intf)
  637. {
  638. struct usbnet *unet = usb_get_intfdata(intf);
  639. struct rmnet_ctrl_dev *dev;
  640. unsigned int n, rdev_cnt, unet_id;
  641. rdev_cnt = unet->data[4] ? no_rmnet_insts_per_dev : 1;
  642. device_set_wakeup_enable(&unet->udev->dev, 0);
  643. for (n = 0; n < rdev_cnt; n++) {
  644. unet_id = n + unet->driver_info->data * no_rmnet_insts_per_dev;
  645. unet =
  646. unet->data[4] ? unet_list[unet_id] : usb_get_intfdata(intf);
  647. device_remove_file(&unet->net->dev, &dev_attr_dbg_mask);
  648. dev = (struct rmnet_ctrl_dev *)unet->data[1];
  649. rmnet_usb_ctrl_disconnect(dev);
  650. unet->data[0] = 0;
  651. unet->data[1] = 0;
  652. rmnet_usb_data_debugfs_cleanup(unet);
  653. usb_set_intfdata(intf, unet);
  654. usbnet_disconnect(intf);
  655. unet_list[unet_id] = NULL;
  656. }
  657. }
  658. static struct driver_info rmnet_info = {
  659. .description = "RmNET net device",
  660. .flags = FLAG_SEND_ZLP,
  661. .bind = rmnet_usb_bind,
  662. .tx_fixup = rmnet_usb_tx_fixup,
  663. .rx_fixup = rmnet_usb_rx_fixup,
  664. .rx_complete = rmnet_usb_rx_complete,
  665. .manage_power = rmnet_usb_manage_power,
  666. .data = 0,
  667. };
  668. static struct driver_info rmnet_usb_info = {
  669. .description = "RmNET net device",
  670. .flags = FLAG_SEND_ZLP,
  671. .bind = rmnet_usb_bind,
  672. .tx_fixup = rmnet_usb_tx_fixup,
  673. .rx_fixup = rmnet_usb_rx_fixup,
  674. .rx_complete = rmnet_usb_rx_complete,
  675. .manage_power = rmnet_usb_manage_power,
  676. .data = 1,
  677. };
  678. static const struct usb_device_id vidpids[] = {
  679. { USB_DEVICE_INTERFACE_NUMBER(0x05c6, 0x9034, 4),
  680. .driver_info = (unsigned long)&rmnet_info,
  681. },
  682. { USB_DEVICE_INTERFACE_NUMBER(0x05c6, 0x9034, 5),
  683. .driver_info = (unsigned long)&rmnet_info,
  684. },
  685. { USB_DEVICE_INTERFACE_NUMBER(0x05c6, 0x9034, 6),
  686. .driver_info = (unsigned long)&rmnet_info,
  687. },
  688. { USB_DEVICE_INTERFACE_NUMBER(0x05c6, 0x9034, 7),
  689. .driver_info = (unsigned long)&rmnet_info,
  690. },
  691. { USB_DEVICE_INTERFACE_NUMBER(0x05c6, 0x9048, 5),
  692. .driver_info = (unsigned long)&rmnet_info,
  693. },
  694. { USB_DEVICE_INTERFACE_NUMBER(0x05c6, 0x9048, 6),
  695. .driver_info = (unsigned long)&rmnet_info,
  696. },
  697. { USB_DEVICE_INTERFACE_NUMBER(0x05c6, 0x9048, 7),
  698. .driver_info = (unsigned long)&rmnet_info,
  699. },
  700. { USB_DEVICE_INTERFACE_NUMBER(0x05c6, 0x9048, 8),
  701. .driver_info = (unsigned long)&rmnet_info,
  702. },
  703. { USB_DEVICE_INTERFACE_NUMBER(0x05c6, 0x904c, 6),
  704. .driver_info = (unsigned long)&rmnet_info,
  705. },
  706. { USB_DEVICE_INTERFACE_NUMBER(0x05c6, 0x904c, 7),
  707. .driver_info = (unsigned long)&rmnet_info,
  708. },
  709. { USB_DEVICE_INTERFACE_NUMBER(0x05c6, 0x904c, 8),
  710. .driver_info = (unsigned long)&rmnet_info,
  711. },
  712. { USB_DEVICE_INTERFACE_NUMBER(0x05c6, 0x9075, 6), /*mux over hsic mdm*/
  713. .driver_info = (unsigned long)&rmnet_info,
  714. },
  715. { USB_DEVICE_INTERFACE_NUMBER(0x05c6, 0x9079, 5),
  716. .driver_info = (unsigned long)&rmnet_usb_info,
  717. },
  718. { USB_DEVICE_INTERFACE_NUMBER(0x05c6, 0x9079, 6),
  719. .driver_info = (unsigned long)&rmnet_usb_info,
  720. },
  721. { USB_DEVICE_INTERFACE_NUMBER(0x05c6, 0x9079, 7),
  722. .driver_info = (unsigned long)&rmnet_usb_info,
  723. },
  724. { USB_DEVICE_INTERFACE_NUMBER(0x05c6, 0x9079, 8),
  725. .driver_info = (unsigned long)&rmnet_usb_info,
  726. },
  727. { }, /* Terminating entry */
  728. };
  729. MODULE_DEVICE_TABLE(usb, vidpids);
  730. static struct usb_driver rmnet_usb = {
  731. .name = "rmnet_usb",
  732. .id_table = vidpids,
  733. .probe = rmnet_usb_probe,
  734. .disconnect = rmnet_usb_disconnect,
  735. .suspend = rmnet_usb_suspend,
  736. .resume = rmnet_usb_resume,
  737. .supports_autosuspend = true,
  738. };
  739. static int rmnet_data_start(void)
  740. {
  741. int retval;
  742. if (no_rmnet_devs > MAX_RMNET_DEVS) {
  743. pr_err("ERROR:%s: param no_rmnet_devs(%d) > than maximum(%d)",
  744. __func__, no_rmnet_devs, MAX_RMNET_DEVS);
  745. return -EINVAL;
  746. }
  747. /* initialize ctrl devices */
  748. retval = rmnet_usb_ctrl_init(no_rmnet_devs, no_rmnet_insts_per_dev);
  749. if (retval) {
  750. err("rmnet_usb_cmux_init failed: %d", retval);
  751. return retval;
  752. }
  753. retval = usb_register(&rmnet_usb);
  754. if (retval) {
  755. err("usb_register failed: %d", retval);
  756. return retval;
  757. }
  758. return retval;
  759. }
  760. static void __exit rmnet_usb_exit(void)
  761. {
  762. usb_deregister(&rmnet_usb);
  763. rmnet_usb_ctrl_exit(no_rmnet_devs, no_rmnet_insts_per_dev);
  764. }
  765. module_exit(rmnet_usb_exit);
  766. MODULE_DESCRIPTION("msm rmnet usb device");
  767. MODULE_LICENSE("GPL v2");