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