blob: fa0cd82d77d473409670b954a6771f40a936b786 [file] [log] [blame]
Hemant Kumar37c35e42011-09-14 23:44:19 -07001/* 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#include <linux/mii.h>
14#include <linux/if_arp.h>
15#include <linux/etherdevice.h>
16#include <linux/usb.h>
17#include <linux/usb/usbnet.h>
18#include <linux/msm_rmnet.h>
19
20#include "rmnet_usb_ctrl.h"
21
22#define RMNET_DATA_LEN 2000
23#define HEADROOM_FOR_QOS 8
24
Hemant Kumar7f236832011-12-16 12:13:21 -080025#define FIRST_RMNET_USB_INTERFACE 5
Hemant Kumar37c35e42011-09-14 23:44:19 -070026#define NUM_EMBEDDED_RMNET_IFACE 4
27
28static int data_msg_dbg_mask;
29
30enum {
31 DEBUG_MASK_LVL0 = 1U << 0,
32 DEBUG_MASK_LVL1 = 1U << 1,
33 DEBUG_MASK_LVL2 = 1U << 2,
34};
35
36#define DBG(m, x...) do { \
37 if (data_msg_dbg_mask & m) \
38 pr_info(x); \
39} while (0)
40
41/*echo dbg_mask > /sys/class/net/rmnet_usbx/dbg_mask*/
42static ssize_t dbg_mask_store(struct device *d,
43 struct device_attribute *attr,
44 const char *buf, size_t n)
45{
46 unsigned int dbg_mask;
47 struct net_device *dev = to_net_dev(d);
48 struct usbnet *unet = netdev_priv(dev);
49
50 if (!dev)
51 return -ENODEV;
52
53 sscanf(buf, "%u", &dbg_mask);
54 /*enable dbg msgs for data driver*/
55 data_msg_dbg_mask = dbg_mask;
56
57 /*set default msg level*/
58 unet->msg_enable = NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK;
59
60 /*enable netif_xxx msgs*/
61 if (dbg_mask & DEBUG_MASK_LVL0)
62 unet->msg_enable |= NETIF_MSG_IFUP | NETIF_MSG_IFDOWN;
63 if (dbg_mask & DEBUG_MASK_LVL1)
64 unet->msg_enable |= NETIF_MSG_TX_ERR | NETIF_MSG_RX_ERR
65 | NETIF_MSG_TX_QUEUED | NETIF_MSG_TX_DONE
66 | NETIF_MSG_RX_STATUS;
67
68 return n;
69}
70
71static ssize_t dbg_mask_show(struct device *d,
72 struct device_attribute *attr, char *buf)
73{
74 return snprintf(buf, PAGE_SIZE, "%d\n", data_msg_dbg_mask);
75}
76
77static DEVICE_ATTR(dbg_mask, 0644, dbg_mask_show, dbg_mask_store);
78
79#define DBG0(x...) DBG(DEBUG_MASK_LVL0, x)
80#define DBG1(x...) DBG(DEBUG_MASK_LVL1, x)
81#define DBG2(x...) DBG(DEBUG_MASK_LVL2, x)
82
83static void rmnet_usb_setup(struct net_device *);
84static int rmnet_ioctl(struct net_device *, struct ifreq *, int);
85
86static int rmnet_usb_suspend(struct usb_interface *iface, pm_message_t message)
87{
88 struct usbnet *unet;
89 struct rmnet_ctrl_dev *dev;
90 int time = 0;
91 int retval = 0;
92
93 unet = usb_get_intfdata(iface);
94 if (!unet) {
95 pr_err("%s:data device not found\n", __func__);
96 retval = -ENODEV;
97 goto fail;
98 }
99
100 dev = (struct rmnet_ctrl_dev *)unet->data[1];
101 if (!dev) {
102 dev_err(&unet->udev->dev, "%s: ctrl device not found\n",
103 __func__);
104 retval = -ENODEV;
105 goto fail;
106 }
107
108 retval = usbnet_suspend(iface, message);
109 if (!retval) {
110 if (message.event & PM_EVENT_SUSPEND) {
111 time = usb_wait_anchor_empty_timeout(&dev->tx_submitted,
112 1000);
113 if (!time)
114 usb_kill_anchored_urbs(&dev->tx_submitted);
115
116 retval = rmnet_usb_ctrl_stop_rx(dev);
117 iface->dev.power.power_state.event = message.event;
118 }
119 /* TBD : do we need to set/clear usbnet->udev->reset_resume*/
120 } else
121 dev_dbg(&unet->udev->dev,
122 "%s: device is busy can not suspend\n", __func__);
123
124fail:
125 return retval;
126}
127
128static int rmnet_usb_resume(struct usb_interface *iface)
129{
130 int retval = 0;
131 int oldstate;
132 struct usbnet *unet;
133 struct rmnet_ctrl_dev *dev;
134
135 unet = usb_get_intfdata(iface);
136 if (!unet) {
137 pr_err("%s:data device not found\n", __func__);
138 retval = -ENODEV;
139 goto fail;
140 }
141
142 dev = (struct rmnet_ctrl_dev *)unet->data[1];
143 if (!dev) {
144 dev_err(&unet->udev->dev, "%s: ctrl device not found\n",
145 __func__);
146 retval = -ENODEV;
147 goto fail;
148 }
149 oldstate = iface->dev.power.power_state.event;
150 iface->dev.power.power_state.event = PM_EVENT_ON;
151
152 retval = usbnet_resume(iface);
153 if (!retval) {
154
155 if (oldstate & PM_EVENT_SUSPEND)
156 retval = rmnet_usb_ctrl_start(dev);
157 }
158fail:
159 return retval;
160}
161
162static int rmnet_usb_bind(struct usbnet *usbnet, struct usb_interface *iface)
163{
164 struct usb_host_endpoint *endpoint = NULL;
165 struct usb_host_endpoint *bulk_in = NULL;
166 struct usb_host_endpoint *bulk_out = NULL;
167 struct usb_host_endpoint *int_in = NULL;
168 struct usb_device *udev;
169 int status = 0;
170 int i;
171 int numends;
172
173 udev = interface_to_usbdev(iface);
174 numends = iface->cur_altsetting->desc.bNumEndpoints;
175 for (i = 0; i < numends; i++) {
176 endpoint = iface->cur_altsetting->endpoint + i;
177 if (!endpoint) {
178 dev_err(&udev->dev, "%s: invalid endpoint %u\n",
179 __func__, i);
180 status = -EINVAL;
181 goto out;
182 }
183 if (usb_endpoint_is_bulk_in(&endpoint->desc))
184 bulk_in = endpoint;
185 else if (usb_endpoint_is_bulk_out(&endpoint->desc))
186 bulk_out = endpoint;
187 else if (usb_endpoint_is_int_in(&endpoint->desc))
188 int_in = endpoint;
189 }
190
191 if (!bulk_in || !bulk_out || !int_in) {
192 dev_err(&udev->dev, "%s: invalid endpoints\n", __func__);
193 status = -EINVAL;
194 goto out;
195 }
196 usbnet->in = usb_rcvbulkpipe(usbnet->udev,
197 bulk_in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
198 usbnet->out = usb_sndbulkpipe(usbnet->udev,
199 bulk_out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
200 usbnet->status = int_in;
201
202 /*change name of net device to rmnet_usbx here*/
203 strlcpy(usbnet->net->name, "rmnet_usb%d", IFNAMSIZ);
204
205 /*TBD: update rx_urb_size, curently set to eth frame len by usbnet*/
206out:
207 return status;
208}
209
210static struct sk_buff *rmnet_usb_tx_fixup(struct usbnet *dev,
211 struct sk_buff *skb, gfp_t flags)
212{
213 struct QMI_QOS_HDR_S *qmih;
214
215 if (test_bit(RMNET_MODE_QOS, &dev->data[0])) {
216 qmih = (struct QMI_QOS_HDR_S *)
217 skb_push(skb, sizeof(struct QMI_QOS_HDR_S));
218 qmih->version = 1;
219 qmih->flags = 0;
220 qmih->flow_id = skb->mark;
221 }
222
223 DBG1("[%s] Tx packet #%lu len=%d mark=0x%x\n",
224 dev->net->name, dev->net->stats.tx_packets, skb->len, skb->mark);
225
226 return skb;
227}
228
229static __be16 rmnet_ip_type_trans(struct sk_buff *skb,
230 struct net_device *dev)
231{
232 __be16 protocol = 0;
233
234 skb->dev = dev;
235
236 switch (skb->data[0] & 0xf0) {
237 case 0x40:
238 protocol = htons(ETH_P_IP);
239 break;
240 case 0x60:
241 protocol = htons(ETH_P_IPV6);
242 break;
243 default:
244 pr_err("[%s] rmnet_recv() L3 protocol decode error: 0x%02x",
245 dev->name, skb->data[0] & 0xf0);
246 }
247
248 return protocol;
249}
250
251static int rmnet_usb_rx_fixup(struct usbnet *dev,
252 struct sk_buff *skb)
253{
254
255 if (test_bit(RMNET_MODE_LLP_IP, &dev->data[0]))
256 skb->protocol = rmnet_ip_type_trans(skb, dev->net);
257 else /*set zero for eth mode*/
258 skb->protocol = 0;
259
260 DBG1("[%s] Rx packet #%lu len=%d\n",
261 dev->net->name, dev->net->stats.rx_packets, skb->len);
262
263 return 1;
264}
265
266static int rmnet_change_mtu(struct net_device *dev, int new_mtu)
267{
268 if (0 > new_mtu || RMNET_DATA_LEN < new_mtu)
269 return -EINVAL;
270
271 DBG0("[%s] MTU change: old=%d new=%d\n", dev->name, dev->mtu, new_mtu);
272
273 dev->mtu = new_mtu;
274
275 return 0;
276}
277
278static struct net_device_stats *rmnet_get_stats(struct net_device *dev)
279{
280 return &dev->stats;
281}
282
283static const struct net_device_ops rmnet_usb_ops_ether = {
284 .ndo_open = usbnet_open,
285 .ndo_stop = usbnet_stop,
286 .ndo_start_xmit = usbnet_start_xmit,
287 .ndo_get_stats = rmnet_get_stats,
288 /*.ndo_set_multicast_list = rmnet_set_multicast_list,*/
289 .ndo_tx_timeout = usbnet_tx_timeout,
290 .ndo_do_ioctl = rmnet_ioctl,
291 .ndo_change_mtu = usbnet_change_mtu,
292 .ndo_set_mac_address = eth_mac_addr,
293 .ndo_validate_addr = eth_validate_addr,
294};
295
296static const struct net_device_ops rmnet_usb_ops_ip = {
297 .ndo_open = usbnet_open,
298 .ndo_stop = usbnet_stop,
299 .ndo_start_xmit = usbnet_start_xmit,
300 .ndo_get_stats = rmnet_get_stats,
301 /*.ndo_set_multicast_list = rmnet_set_multicast_list,*/
302 .ndo_tx_timeout = usbnet_tx_timeout,
303 .ndo_do_ioctl = rmnet_ioctl,
304 .ndo_change_mtu = rmnet_change_mtu,
305 .ndo_set_mac_address = 0,
306 .ndo_validate_addr = 0,
307};
308
309
310static int rmnet_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
311{
312 struct usbnet *unet = netdev_priv(dev);
313 u32 old_opmode;
314 int prev_mtu = dev->mtu;
315 int rc = 0;
316
317 old_opmode = unet->data[0]; /*data[0] saves operation mode*/
318 /* Process IOCTL command */
319 switch (cmd) {
320 case RMNET_IOCTL_SET_LLP_ETHERNET: /*Set Ethernet protocol*/
321 /* Perform Ethernet config only if in IP mode currently*/
322 if (test_bit(RMNET_MODE_LLP_IP, &unet->data[0])) {
323 ether_setup(dev);
324 random_ether_addr(dev->dev_addr);
325 dev->mtu = prev_mtu;
326 dev->netdev_ops = &rmnet_usb_ops_ether;
327 clear_bit(RMNET_MODE_LLP_IP, &unet->data[0]);
328 set_bit(RMNET_MODE_LLP_ETH, &unet->data[0]);
329 DBG0("[%s] rmnet_ioctl(): set Ethernet protocol mode\n",
330 dev->name);
331 }
332 break;
333
334 case RMNET_IOCTL_SET_LLP_IP: /* Set RAWIP protocol*/
335 /* Perform IP config only if in Ethernet mode currently*/
336 if (test_bit(RMNET_MODE_LLP_ETH, &unet->data[0])) {
337
338 /* Undo config done in ether_setup() */
339 dev->header_ops = 0; /* No header */
340 dev->type = ARPHRD_RAWIP;
341 dev->hard_header_len = 0;
342 dev->mtu = prev_mtu;
343 dev->addr_len = 0;
344 dev->flags &= ~(IFF_BROADCAST | IFF_MULTICAST);
345 dev->needed_headroom = HEADROOM_FOR_QOS;
346 dev->netdev_ops = &rmnet_usb_ops_ip;
347 clear_bit(RMNET_MODE_LLP_ETH, &unet->data[0]);
348 set_bit(RMNET_MODE_LLP_IP, &unet->data[0]);
349 DBG0("[%s] rmnet_ioctl(): set IP protocol mode\n",
350 dev->name);
351 }
352 break;
353
354 case RMNET_IOCTL_GET_LLP: /* Get link protocol state */
355 ifr->ifr_ifru.ifru_data = (void *)(unet->data[0]
356 & (RMNET_MODE_LLP_ETH
357 | RMNET_MODE_LLP_IP));
358 break;
359
360 case RMNET_IOCTL_SET_QOS_ENABLE: /* Set QoS header enabled*/
361 set_bit(RMNET_MODE_QOS, &unet->data[0]);
362 DBG0("[%s] rmnet_ioctl(): set QMI QOS header enable\n",
363 dev->name);
364 break;
365
366 case RMNET_IOCTL_SET_QOS_DISABLE: /* Set QoS header disabled */
367 clear_bit(RMNET_MODE_QOS, &unet->data[0]);
368 DBG0("[%s] rmnet_ioctl(): set QMI QOS header disable\n",
369 dev->name);
370 break;
371
372 case RMNET_IOCTL_GET_QOS: /* Get QoS header state */
373 ifr->ifr_ifru.ifru_data = (void *)(unet->data[0]
374 & RMNET_MODE_QOS);
375 break;
376
377 case RMNET_IOCTL_GET_OPMODE: /* Get operation mode*/
378 ifr->ifr_ifru.ifru_data = (void *)unet->data[0];
379 break;
380
381 case RMNET_IOCTL_OPEN: /* Open transport port */
382 rc = usbnet_open(dev);
383 DBG0("[%s] rmnet_ioctl(): open transport port\n", dev->name);
384 break;
385
386 case RMNET_IOCTL_CLOSE: /* Close transport port*/
387 rc = usbnet_stop(dev);
388 DBG0("[%s] rmnet_ioctl(): close transport port\n", dev->name);
389 break;
390
391 default:
392 dev_err(&unet->udev->dev, "[%s] error: "
393 "rmnet_ioct called for unsupported cmd[%d]",
394 dev->name, cmd);
395 return -EINVAL;
396 }
397
398 DBG2("[%s] %s: cmd=0x%x opmode old=0x%08x new=0x%08lx\n",
399 dev->name, __func__, cmd, old_opmode, unet->data[0]);
400
401 return rc;
402}
403
404static void rmnet_usb_setup(struct net_device *dev)
405{
406 /* Using Ethernet mode by default */
407 dev->netdev_ops = &rmnet_usb_ops_ether;
408
409 /* set this after calling ether_setup */
410 dev->mtu = RMNET_DATA_LEN;
411
412 dev->needed_headroom = HEADROOM_FOR_QOS;
413 random_ether_addr(dev->dev_addr);
414 dev->watchdog_timeo = 1000; /* 10 seconds? */
415}
416
417static int rmnet_usb_probe(struct usb_interface *iface,
418 const struct usb_device_id *prod)
419{
420 struct usbnet *unet;
421 struct usb_device *udev;
422 int iface_num;
423 int last_rmnet_iface_num;
424 int status = -ENODEV;
425
426 udev = interface_to_usbdev(iface);
427 iface_num = iface->cur_altsetting->desc.bInterfaceNumber;
428 if (iface->num_altsetting != 1) {
429 dev_err(&udev->dev, "%s invalid num_altsetting %u\n",
430 __func__, iface->num_altsetting);
431 status = -EINVAL;
432 goto out;
433 }
434
435 last_rmnet_iface_num = (FIRST_RMNET_USB_INTERFACE +
436 NUM_EMBEDDED_RMNET_IFACE - 1);
437
438 if (iface_num >= FIRST_RMNET_USB_INTERFACE &&
439 iface_num <= last_rmnet_iface_num) {
440 status = usbnet_probe(iface, prod);
441 if (status < 0) {
442 dev_err(&udev->dev, "usbnet_probe failed %d\n",
443 status);
444 goto out;
445 }
446 unet = usb_get_intfdata(iface);
447
448 /*set rmnet operation mode to eth by default*/
449 set_bit(RMNET_MODE_LLP_ETH, &unet->data[0]);
450
451 /*update net device*/
452 rmnet_usb_setup(unet->net);
453
454 /*create /sys/class/net/rmnet_usbx/dbg_mask*/
455 status = device_create_file(&unet->net->dev,
456 &dev_attr_dbg_mask);
457 if (status)
458 goto out;
459
460 /*save control device intstance */
461 unet->data[1] = (unsigned long)ctrl_dev \
462 [iface_num - FIRST_RMNET_USB_INTERFACE];
463
464 status = rmnet_usb_ctrl_probe(iface, unet->status,
465 (struct rmnet_ctrl_dev *)unet->data[1]);
466 }
467out:
468 return status;
469}
470
471static void rmnet_usb_disconnect(struct usb_interface *intf)
472{
473 struct usbnet *unet;
474 struct usb_device *udev;
475 struct rmnet_ctrl_dev *dev;
476 int iface_num;
477 int last_rmnet_iface_num;
478
479 udev = interface_to_usbdev(intf);
480 iface_num = intf->cur_altsetting->desc.bInterfaceNumber;
481
482 last_rmnet_iface_num = (FIRST_RMNET_USB_INTERFACE +
483 NUM_EMBEDDED_RMNET_IFACE - 1);
484
485 if (iface_num >= FIRST_RMNET_USB_INTERFACE &&
486 iface_num <= last_rmnet_iface_num) {
487 unet = usb_get_intfdata(intf);
488 if (!unet) {
489 dev_err(&udev->dev, "%s:data device not found\n",
490 __func__);
491 return;
492 }
493 dev = (struct rmnet_ctrl_dev *)unet->data[1];
494 if (!dev) {
495 dev_err(&udev->dev, "%s:ctrl device not found\n",
496 __func__);
497 return;
498 }
499 unet->data[0] = 0;
500 unet->data[1] = 0;
501 rmnet_usb_ctrl_disconnect(dev);
502 device_remove_file(&unet->net->dev, &dev_attr_dbg_mask);
503 usbnet_disconnect(intf);
504 }
505}
506
507static const struct driver_info rmnet_info = {
508 .description = "RmNET net device",
509 .bind = rmnet_usb_bind,
510 .tx_fixup = rmnet_usb_tx_fixup,
511 .rx_fixup = rmnet_usb_rx_fixup,
512 .data = 0,
513};
514
515static const struct usb_device_id vidpids[] = {
516 {
Hemant Kumar7f236832011-12-16 12:13:21 -0800517 USB_DEVICE(0x05c6, 0x9048), /* MDM9x15*/
Hemant Kumar37c35e42011-09-14 23:44:19 -0700518 .driver_info = (unsigned long)&rmnet_info,
519 },
520 { },
521};
522
523MODULE_DEVICE_TABLE(usb, vidpids);
524
525static struct usb_driver rmnet_usb = {
526 .name = "rmnet_usb",
527 .id_table = vidpids,
528 .probe = rmnet_usb_probe,
529 .disconnect = rmnet_usb_disconnect,
530 .suspend = rmnet_usb_suspend,
531 .resume = rmnet_usb_resume,
532 .supports_autosuspend = true,
533};
534
535static int __init rmnet_usb_init(void)
536{
537 int retval;
538
539 retval = usb_register(&rmnet_usb);
540 if (retval) {
541 err("usb_register failed: %d", retval);
542 return retval;
543 }
544 /* initialize rmnet ctrl device here*/
545 retval = rmnet_usb_ctrl_init();
546 if (retval) {
547 usb_deregister(&rmnet_usb);
548 err("rmnet_usb_cmux_init failed: %d", retval);
549 return retval;
550 }
551
552 return 0;
553}
554module_init(rmnet_usb_init);
555
556static void __exit rmnet_usb_exit(void)
557{
558 rmnet_usb_ctrl_exit();
559 usb_deregister(&rmnet_usb);
560}
561module_exit(rmnet_usb_exit);
562
563MODULE_DESCRIPTION("msm rmnet usb device");
564MODULE_LICENSE("GPL v2");