blob: 6324c1728784c70050c2056a8e64ece82b72272b [file] [log] [blame]
Anna Perela8c991d2012-04-09 16:44:46 +03001/* Copyright (c) 2012, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13
14#define pr_fmt(fmt) "%s: " fmt, __func__
15
16#include <linux/kernel.h>
17#include <linux/device.h>
18
19#include <linux/usb/cdc.h>
20
21#include <linux/usb/composite.h>
Anna Perela8c991d2012-04-09 16:44:46 +030022#include <linux/platform_device.h>
23
24#include <linux/spinlock.h>
25
26/*
27 * This function is a "Mobile Broadband Interface Model" (MBIM) link.
28 * MBIM is intended to be used with high-speed network attachments.
29 *
30 * Note that MBIM requires the use of "alternate settings" for its data
31 * interface. This means that the set_alt() method has real work to do,
32 * and also means that a get_alt() method is required.
33 */
34
35#define MBIM_BULK_BUFFER_SIZE 4096
36
37#define MBIM_IOCTL_MAGIC 'o'
38#define MBIM_GET_NTB_SIZE _IOR(MBIM_IOCTL_MAGIC, 2, u32)
39#define MBIM_GET_DATAGRAM_COUNT _IOR(MBIM_IOCTL_MAGIC, 3, u16)
40
41#define NR_MBIM_PORTS 1
42
Jack Pham2df2f702012-10-11 19:08:24 -070043/* ID for Microsoft OS String */
44#define MBIM_OS_STRING_ID 0xEE
45
Anna Perela8c991d2012-04-09 16:44:46 +030046struct ctrl_pkt {
47 void *buf;
48 int len;
49 struct list_head list;
50};
51
52struct mbim_ep_descs {
53 struct usb_endpoint_descriptor *in;
54 struct usb_endpoint_descriptor *out;
55 struct usb_endpoint_descriptor *notify;
56};
57
58struct mbim_notify_port {
59 struct usb_ep *notify;
60 struct usb_request *notify_req;
61 u8 notify_state;
62 atomic_t notify_count;
63};
64
65enum mbim_notify_state {
66 NCM_NOTIFY_NONE,
67 NCM_NOTIFY_CONNECT,
68 NCM_NOTIFY_SPEED,
69};
70
71struct f_mbim {
Anna Perel557bf722012-09-20 11:16:35 +030072 struct usb_function function;
73 struct usb_composite_dev *cdev;
Anna Perela8c991d2012-04-09 16:44:46 +030074
75 atomic_t online;
76 bool is_open;
77
78 atomic_t open_excl;
79 atomic_t ioctl_excl;
80 atomic_t read_excl;
81 atomic_t write_excl;
82
83 wait_queue_head_t read_wq;
84 wait_queue_head_t write_wq;
85
86 u8 port_num;
87 struct data_port bam_port;
88 struct mbim_notify_port not_port;
89
90 struct mbim_ep_descs fs;
91 struct mbim_ep_descs hs;
92
93 u8 ctrl_id, data_id;
94
95 struct ndp_parser_opts *parser_opts;
96
97 spinlock_t lock;
98
99 struct list_head cpkt_req_q;
100 struct list_head cpkt_resp_q;
101
102 u32 ntb_input_size;
103 u16 ntb_max_datagrams;
104
Anna Perela8c991d2012-04-09 16:44:46 +0300105 atomic_t error;
106};
107
108struct mbim_ntb_input_size {
109 u32 ntb_input_size;
110 u16 ntb_max_datagrams;
111 u16 reserved;
112};
113
114/* temporary variable used between mbim_open() and mbim_gadget_bind() */
115static struct f_mbim *_mbim_dev;
116
117static unsigned int nr_mbim_ports;
118
119static struct mbim_ports {
120 struct f_mbim *port;
121 unsigned port_num;
122} mbim_ports[NR_MBIM_PORTS];
123
124static inline struct f_mbim *func_to_mbim(struct usb_function *f)
125{
126 return container_of(f, struct f_mbim, function);
127}
128
129/* peak (theoretical) bulk transfer rate in bits-per-second */
130static inline unsigned mbim_bitrate(struct usb_gadget *g)
131{
132 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
133 return 13 * 512 * 8 * 1000 * 8;
134 else
135 return 19 * 64 * 1 * 1000 * 8;
136}
137
138/*-------------------------------------------------------------------------*/
139
140#define NTB_DEFAULT_IN_SIZE (0x4000)
141#define NTB_OUT_SIZE (0x1000)
142#define NDP_IN_DIVISOR (0x4)
143
144#define FORMATS_SUPPORTED USB_CDC_NCM_NTB16_SUPPORTED
145
146static struct usb_cdc_ncm_ntb_parameters ntb_parameters = {
147 .wLength = sizeof ntb_parameters,
148 .bmNtbFormatsSupported = cpu_to_le16(FORMATS_SUPPORTED),
149 .dwNtbInMaxSize = cpu_to_le32(NTB_DEFAULT_IN_SIZE),
150 .wNdpInDivisor = cpu_to_le16(NDP_IN_DIVISOR),
151 .wNdpInPayloadRemainder = cpu_to_le16(0),
152 .wNdpInAlignment = cpu_to_le16(4),
153
154 .dwNtbOutMaxSize = cpu_to_le32(NTB_OUT_SIZE),
155 .wNdpOutDivisor = cpu_to_le16(4),
156 .wNdpOutPayloadRemainder = cpu_to_le16(0),
157 .wNdpOutAlignment = cpu_to_le16(4),
Anna Perelf99cd0c2012-05-03 13:30:26 +0300158 .wNtbOutMaxDatagrams = 0,
Anna Perela8c991d2012-04-09 16:44:46 +0300159};
160
161/*
162 * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
163 * packet, to simplify cancellation; and a big transfer interval, to
164 * waste less bandwidth.
165 */
166
167#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
168#define NCM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
169
170static struct usb_interface_assoc_descriptor mbim_iad_desc = {
171 .bLength = sizeof mbim_iad_desc,
172 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
173
174 /* .bFirstInterface = DYNAMIC, */
175 .bInterfaceCount = 2, /* control + data */
176 .bFunctionClass = 2,
177 .bFunctionSubClass = 0x0e,
178 .bFunctionProtocol = 0,
179 /* .iFunction = DYNAMIC */
180};
181
182/* interface descriptor: */
183static struct usb_interface_descriptor mbim_control_intf = {
184 .bLength = sizeof mbim_control_intf,
185 .bDescriptorType = USB_DT_INTERFACE,
186
187 /* .bInterfaceNumber = DYNAMIC */
188 .bNumEndpoints = 1,
189 .bInterfaceClass = 0x02,
190 .bInterfaceSubClass = 0x0e,
191 .bInterfaceProtocol = 0,
192 /* .iInterface = DYNAMIC */
193};
194
195static struct usb_cdc_header_desc mbim_header_desc = {
196 .bLength = sizeof mbim_header_desc,
197 .bDescriptorType = USB_DT_CS_INTERFACE,
198 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
199
200 .bcdCDC = cpu_to_le16(0x0110),
201};
202
203static struct usb_cdc_union_desc mbim_union_desc = {
204 .bLength = sizeof(mbim_union_desc),
205 .bDescriptorType = USB_DT_CS_INTERFACE,
206 .bDescriptorSubType = USB_CDC_UNION_TYPE,
207 /* .bMasterInterface0 = DYNAMIC */
208 /* .bSlaveInterface0 = DYNAMIC */
209};
210
211static struct usb_cdc_mbb_desc mbb_desc = {
212 .bLength = sizeof mbb_desc,
213 .bDescriptorType = USB_DT_CS_INTERFACE,
214 .bDescriptorSubType = USB_CDC_MBB_TYPE,
215
216 .bcdMbbVersion = cpu_to_le16(0x0100),
217
218 .wMaxControlMessage = cpu_to_le16(0x1000),
219 .bNumberFilters = 0x10,
220 .bMaxFilterSize = 0x80,
Anna Perel26ae27c2012-05-23 18:07:31 +0300221 .wMaxSegmentSize = cpu_to_le16(0xfe0),
Anna Perela8c991d2012-04-09 16:44:46 +0300222 .bmNetworkCapabilities = 0x20,
223};
224
225/* the default data interface has no endpoints ... */
226static struct usb_interface_descriptor mbim_data_nop_intf = {
227 .bLength = sizeof mbim_data_nop_intf,
228 .bDescriptorType = USB_DT_INTERFACE,
229
230 /* .bInterfaceNumber = DYNAMIC */
231 .bAlternateSetting = 0,
232 .bNumEndpoints = 0,
233 .bInterfaceClass = 0x0a,
234 .bInterfaceSubClass = 0,
235 .bInterfaceProtocol = 0x02,
236 /* .iInterface = DYNAMIC */
237};
238
239/* ... but the "real" data interface has two bulk endpoints */
240static struct usb_interface_descriptor mbim_data_intf = {
241 .bLength = sizeof mbim_data_intf,
242 .bDescriptorType = USB_DT_INTERFACE,
243
244 /* .bInterfaceNumber = DYNAMIC */
245 .bAlternateSetting = 1,
246 .bNumEndpoints = 2,
247 .bInterfaceClass = 0x0a,
248 .bInterfaceSubClass = 0,
249 .bInterfaceProtocol = 0x02,
250 /* .iInterface = DYNAMIC */
251};
252
253/* full speed support: */
254
255static struct usb_endpoint_descriptor fs_mbim_notify_desc = {
256 .bLength = USB_DT_ENDPOINT_SIZE,
257 .bDescriptorType = USB_DT_ENDPOINT,
258
259 .bEndpointAddress = USB_DIR_IN,
260 .bmAttributes = USB_ENDPOINT_XFER_INT,
261 .wMaxPacketSize = 4*cpu_to_le16(NCM_STATUS_BYTECOUNT),
262 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
263};
264
265static struct usb_endpoint_descriptor fs_mbim_in_desc = {
266 .bLength = USB_DT_ENDPOINT_SIZE,
267 .bDescriptorType = USB_DT_ENDPOINT,
268
269 .bEndpointAddress = USB_DIR_IN,
270 .bmAttributes = USB_ENDPOINT_XFER_BULK,
271};
272
273static struct usb_endpoint_descriptor fs_mbim_out_desc = {
274 .bLength = USB_DT_ENDPOINT_SIZE,
275 .bDescriptorType = USB_DT_ENDPOINT,
276
277 .bEndpointAddress = USB_DIR_OUT,
278 .bmAttributes = USB_ENDPOINT_XFER_BULK,
279};
280
281static struct usb_descriptor_header *mbim_fs_function[] = {
282 (struct usb_descriptor_header *) &mbim_iad_desc,
283 /* MBIM control descriptors */
284 (struct usb_descriptor_header *) &mbim_control_intf,
285 (struct usb_descriptor_header *) &mbim_header_desc,
286 (struct usb_descriptor_header *) &mbb_desc,
287 (struct usb_descriptor_header *) &fs_mbim_notify_desc,
288 /* data interface, altsettings 0 and 1 */
289 (struct usb_descriptor_header *) &mbim_data_nop_intf,
290 (struct usb_descriptor_header *) &mbim_data_intf,
291 (struct usb_descriptor_header *) &fs_mbim_in_desc,
292 (struct usb_descriptor_header *) &fs_mbim_out_desc,
293 NULL,
294};
295
296/* high speed support: */
297
298static struct usb_endpoint_descriptor hs_mbim_notify_desc = {
299 .bLength = USB_DT_ENDPOINT_SIZE,
300 .bDescriptorType = USB_DT_ENDPOINT,
301
302 .bEndpointAddress = USB_DIR_IN,
303 .bmAttributes = USB_ENDPOINT_XFER_INT,
304 .wMaxPacketSize = 4*cpu_to_le16(NCM_STATUS_BYTECOUNT),
305 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
306};
307static struct usb_endpoint_descriptor hs_mbim_in_desc = {
308 .bLength = USB_DT_ENDPOINT_SIZE,
309 .bDescriptorType = USB_DT_ENDPOINT,
310
311 .bEndpointAddress = USB_DIR_IN,
312 .bmAttributes = USB_ENDPOINT_XFER_BULK,
313 .wMaxPacketSize = cpu_to_le16(512),
314};
315
316static struct usb_endpoint_descriptor hs_mbim_out_desc = {
317 .bLength = USB_DT_ENDPOINT_SIZE,
318 .bDescriptorType = USB_DT_ENDPOINT,
319
320 .bEndpointAddress = USB_DIR_OUT,
321 .bmAttributes = USB_ENDPOINT_XFER_BULK,
322 .wMaxPacketSize = cpu_to_le16(512),
323};
324
325static struct usb_descriptor_header *mbim_hs_function[] = {
326 (struct usb_descriptor_header *) &mbim_iad_desc,
327 /* MBIM control descriptors */
328 (struct usb_descriptor_header *) &mbim_control_intf,
329 (struct usb_descriptor_header *) &mbim_header_desc,
330 (struct usb_descriptor_header *) &mbb_desc,
331 (struct usb_descriptor_header *) &hs_mbim_notify_desc,
332 /* data interface, altsettings 0 and 1 */
333 (struct usb_descriptor_header *) &mbim_data_nop_intf,
334 (struct usb_descriptor_header *) &mbim_data_intf,
335 (struct usb_descriptor_header *) &hs_mbim_in_desc,
336 (struct usb_descriptor_header *) &hs_mbim_out_desc,
337 NULL,
338};
339
340/* string descriptors: */
341
342#define STRING_CTRL_IDX 0
343#define STRING_DATA_IDX 1
344
345static struct usb_string mbim_string_defs[] = {
346 [STRING_CTRL_IDX].s = "MBIM Control",
347 [STRING_DATA_IDX].s = "MBIM Data",
348 { } /* end of list */
349};
350
351static struct usb_gadget_strings mbim_string_table = {
352 .language = 0x0409, /* en-us */
353 .strings = mbim_string_defs,
354};
355
356static struct usb_gadget_strings *mbim_strings[] = {
357 &mbim_string_table,
358 NULL,
359};
360
Jack Pham2df2f702012-10-11 19:08:24 -0700361/* Microsoft OS Descriptors */
362
363/*
364 * We specify our own bMS_VendorCode byte which Windows will use
365 * as the bRequest value in subsequent device get requests.
366 */
367#define MBIM_VENDOR_CODE 0xA5
368
369/* Microsoft OS String */
370static u8 mbim_os_string[] = {
371 18, /* sizeof(mtp_os_string) */
372 USB_DT_STRING,
373 /* Signature field: "MSFT100" */
374 'M', 0, 'S', 0, 'F', 0, 'T', 0, '1', 0, '0', 0, '0', 0,
375 /* vendor code */
376 MBIM_VENDOR_CODE,
377 /* padding */
378 0
379};
380
381/* Microsoft Extended Configuration Descriptor Header Section */
382struct mbim_ext_config_desc_header {
383 __le32 dwLength;
384 __u16 bcdVersion;
385 __le16 wIndex;
386 __u8 bCount;
387 __u8 reserved[7];
388};
389
390/* Microsoft Extended Configuration Descriptor Function Section */
391struct mbim_ext_config_desc_function {
392 __u8 bFirstInterfaceNumber;
393 __u8 bInterfaceCount;
394 __u8 compatibleID[8];
395 __u8 subCompatibleID[8];
396 __u8 reserved[6];
397};
398
399/* Microsoft Extended Configuration Descriptor */
400static struct {
401 struct mbim_ext_config_desc_header header;
402 struct mbim_ext_config_desc_function function;
403} mbim_ext_config_desc = {
404 .header = {
405 .dwLength = __constant_cpu_to_le32(sizeof mbim_ext_config_desc),
406 .bcdVersion = __constant_cpu_to_le16(0x0100),
407 .wIndex = __constant_cpu_to_le16(4),
408 .bCount = 1,
409 },
410 .function = {
411 .bFirstInterfaceNumber = 0,
412 .bInterfaceCount = 1,
413 .compatibleID = { 'A', 'L', 'T', 'R', 'C', 'F', 'G' },
414 /* .subCompatibleID = DYNAMIC */
415 },
416};
417
Anna Perela8c991d2012-04-09 16:44:46 +0300418/*
419 * Here are options for the Datagram Pointer table (NDP) parser.
420 * There are 2 different formats: NDP16 and NDP32 in the spec (ch. 3),
421 * in NDP16 offsets and sizes fields are 1 16bit word wide,
422 * in NDP32 -- 2 16bit words wide. Also signatures are different.
423 * To make the parser code the same, put the differences in the structure,
424 * and switch pointers to the structures when the format is changed.
425 */
426
427struct ndp_parser_opts {
428 u32 nth_sign;
429 u32 ndp_sign;
430 unsigned nth_size;
431 unsigned ndp_size;
432 unsigned ndplen_align;
433 /* sizes in u16 units */
434 unsigned dgram_item_len; /* index or length */
435 unsigned block_length;
436 unsigned fp_index;
437 unsigned reserved1;
438 unsigned reserved2;
439 unsigned next_fp_index;
440};
441
442#define INIT_NDP16_OPTS { \
443 .nth_sign = USB_CDC_NCM_NTH16_SIGN, \
444 .ndp_sign = USB_CDC_NCM_NDP16_NOCRC_SIGN, \
445 .nth_size = sizeof(struct usb_cdc_ncm_nth16), \
446 .ndp_size = sizeof(struct usb_cdc_ncm_ndp16), \
447 .ndplen_align = 4, \
448 .dgram_item_len = 1, \
449 .block_length = 1, \
450 .fp_index = 1, \
451 .reserved1 = 0, \
452 .reserved2 = 0, \
453 .next_fp_index = 1, \
454}
455
456#define INIT_NDP32_OPTS { \
457 .nth_sign = USB_CDC_NCM_NTH32_SIGN, \
458 .ndp_sign = USB_CDC_NCM_NDP32_NOCRC_SIGN, \
459 .nth_size = sizeof(struct usb_cdc_ncm_nth32), \
460 .ndp_size = sizeof(struct usb_cdc_ncm_ndp32), \
461 .ndplen_align = 8, \
462 .dgram_item_len = 2, \
463 .block_length = 2, \
464 .fp_index = 2, \
465 .reserved1 = 1, \
466 .reserved2 = 2, \
467 .next_fp_index = 2, \
468}
469
470static struct ndp_parser_opts ndp16_opts = INIT_NDP16_OPTS;
471static struct ndp_parser_opts ndp32_opts = INIT_NDP32_OPTS;
472
473static inline int mbim_lock(atomic_t *excl)
474{
475 if (atomic_inc_return(excl) == 1) {
476 return 0;
477 } else {
478 atomic_dec(excl);
479 return -EBUSY;
480 }
481}
482
483static inline void mbim_unlock(atomic_t *excl)
484{
485 atomic_dec(excl);
486}
487
488static struct ctrl_pkt *mbim_alloc_ctrl_pkt(unsigned len, gfp_t flags)
489{
490 struct ctrl_pkt *pkt;
491
492 pkt = kzalloc(sizeof(struct ctrl_pkt), flags);
493 if (!pkt)
494 return ERR_PTR(-ENOMEM);
495
496 pkt->buf = kmalloc(len, flags);
497 if (!pkt->buf) {
498 kfree(pkt);
499 return ERR_PTR(-ENOMEM);
500 }
501 pkt->len = len;
502
503 return pkt;
504}
505
506static void mbim_free_ctrl_pkt(struct ctrl_pkt *pkt)
507{
508 if (pkt) {
509 kfree(pkt->buf);
510 kfree(pkt);
511 }
512}
513
514static struct usb_request *mbim_alloc_req(struct usb_ep *ep, int buffer_size)
515{
516 struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
517 if (!req)
518 return NULL;
519
520 req->buf = kmalloc(buffer_size, GFP_KERNEL);
521 if (!req->buf) {
522 usb_ep_free_request(ep, req);
523 return NULL;
524 }
525 req->length = buffer_size;
526 return req;
527}
528
529void fmbim_free_req(struct usb_ep *ep, struct usb_request *req)
530{
531 if (req) {
532 kfree(req->buf);
533 usb_ep_free_request(ep, req);
534 }
535}
536
537static void fmbim_ctrl_response_available(struct f_mbim *dev)
538{
539 struct usb_request *req = dev->not_port.notify_req;
540 struct usb_cdc_notification *event = NULL;
541 unsigned long flags;
542 int ret;
543
Anna Perel68aeb172012-10-28 09:00:45 +0200544 pr_debug("dev:%p portno#%d\n", dev, dev->port_num);
Anna Perela8c991d2012-04-09 16:44:46 +0300545
546 spin_lock_irqsave(&dev->lock, flags);
547
548 if (!atomic_read(&dev->online)) {
549 pr_info("dev:%p is not online\n", dev);
550 spin_unlock_irqrestore(&dev->lock, flags);
551 return;
552 }
553
554 if (!req) {
555 pr_info("dev:%p req is NULL\n", dev);
556 spin_unlock_irqrestore(&dev->lock, flags);
557 return;
558 }
559
560 if (!req->buf) {
561 pr_info("dev:%p req->buf is NULL\n", dev);
562 spin_unlock_irqrestore(&dev->lock, flags);
563 return;
564 }
565
Anna Perel68aeb172012-10-28 09:00:45 +0200566 if (atomic_inc_return(&dev->not_port.notify_count) != 1) {
567 pr_debug("delay ep_queue: notifications queue is busy[%d]",
568 atomic_read(&dev->not_port.notify_count));
569 spin_unlock_irqrestore(&dev->lock, flags);
570 return;
571 }
Anna Perela8c991d2012-04-09 16:44:46 +0300572
573 event = req->buf;
574 event->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS
575 | USB_RECIP_INTERFACE;
576 event->bNotificationType = USB_CDC_NOTIFY_RESPONSE_AVAILABLE;
577 event->wValue = cpu_to_le16(0);
578 event->wIndex = cpu_to_le16(dev->ctrl_id);
579 event->wLength = cpu_to_le16(0);
580 spin_unlock_irqrestore(&dev->lock, flags);
581
Anna Perela8c991d2012-04-09 16:44:46 +0300582 ret = usb_ep_queue(dev->not_port.notify,
583 dev->not_port.notify_req, GFP_ATOMIC);
584 if (ret) {
585 atomic_dec(&dev->not_port.notify_count);
586 pr_err("ep enqueue error %d\n", ret);
587 }
588
Anna Perel68aeb172012-10-28 09:00:45 +0200589 pr_debug("Successful Exit");
Anna Perela8c991d2012-04-09 16:44:46 +0300590}
591
592static int
593fmbim_send_cpkt_response(struct f_mbim *gr, struct ctrl_pkt *cpkt)
594{
595 struct f_mbim *dev = gr;
596 unsigned long flags;
597
598 if (!gr || !cpkt) {
599 pr_err("Invalid cpkt, dev:%p cpkt:%p\n",
600 gr, cpkt);
601 return -ENODEV;
602 }
603
604 pr_info("dev:%p port_num#%d\n", dev, dev->port_num);
605
606 if (!atomic_read(&dev->online)) {
607 pr_info("dev:%p is not connected\n", dev);
608 mbim_free_ctrl_pkt(cpkt);
609 return 0;
610 }
611
612 spin_lock_irqsave(&dev->lock, flags);
Anna Perel40c550c2012-04-11 14:09:27 +0300613 list_add_tail(&cpkt->list, &dev->cpkt_resp_q);
Anna Perela8c991d2012-04-09 16:44:46 +0300614 spin_unlock_irqrestore(&dev->lock, flags);
615
616 fmbim_ctrl_response_available(dev);
617
618 return 0;
619}
620
621/* ---------------------------- BAM INTERFACE ----------------------------- */
622
623static int mbim_bam_setup(int no_ports)
624{
625 int ret;
626
627 pr_info("no_ports:%d\n", no_ports);
628
629 ret = bam_data_setup(no_ports);
630 if (ret) {
631 pr_err("bam_data_setup failed err: %d\n", ret);
632 return ret;
633 }
634
635 pr_info("Initialized %d ports\n", no_ports);
636 return 0;
637}
638
639static int mbim_bam_connect(struct f_mbim *dev)
640{
641 int ret;
642
643 pr_info("dev:%p portno:%d\n", dev, dev->port_num);
644
645 ret = bam_data_connect(&dev->bam_port, dev->port_num, dev->port_num);
646 if (ret) {
647 pr_err("bam_data_setup failed: err:%d\n",
648 ret);
649 return ret;
650 } else {
651 pr_info("mbim bam connected\n");
652 }
653
654 return 0;
655}
656
657static int mbim_bam_disconnect(struct f_mbim *dev)
658{
659 pr_info("dev:%p port:%d. Do nothing.\n",
660 dev, dev->port_num);
661
662 /* bam_data_disconnect(&dev->bam_port, dev->port_num); */
663
664 return 0;
665}
666
667/* -------------------------------------------------------------------------*/
668
669static inline void mbim_reset_values(struct f_mbim *mbim)
670{
671 mbim->parser_opts = &ndp16_opts;
672
673 mbim->ntb_input_size = NTB_DEFAULT_IN_SIZE;
674
Anna Perela8c991d2012-04-09 16:44:46 +0300675 atomic_set(&mbim->online, 0);
676}
677
Anna Perel86ea7c92012-04-24 14:31:29 +0300678static void mbim_reset_function_queue(struct f_mbim *dev)
679{
680 struct ctrl_pkt *cpkt = NULL;
681
682 pr_debug("Queue empty packet for QBI");
683
684 spin_lock(&dev->lock);
685 if (!dev->is_open) {
686 pr_err("%s: mbim file handler %p is not open", __func__, dev);
687 spin_unlock(&dev->lock);
688 return;
689 }
690
691 cpkt = mbim_alloc_ctrl_pkt(0, GFP_ATOMIC);
692 if (!cpkt) {
693 pr_err("%s: Unable to allocate reset function pkt\n", __func__);
694 spin_unlock(&dev->lock);
695 return;
696 }
697
698 list_add_tail(&cpkt->list, &dev->cpkt_req_q);
699 spin_unlock(&dev->lock);
700
701 pr_debug("%s: Wake up read queue", __func__);
702 wake_up(&dev->read_wq);
703}
704
705static void fmbim_reset_cmd_complete(struct usb_ep *ep, struct usb_request *req)
706{
707 struct f_mbim *dev = req->context;
708
709 mbim_reset_function_queue(dev);
710}
711
712static void mbim_clear_queues(struct f_mbim *mbim)
713{
714 struct ctrl_pkt *cpkt = NULL;
715 struct list_head *act, *tmp;
716
717 spin_lock(&mbim->lock);
718 list_for_each_safe(act, tmp, &mbim->cpkt_req_q) {
719 cpkt = list_entry(act, struct ctrl_pkt, list);
720 list_del(&cpkt->list);
721 mbim_free_ctrl_pkt(cpkt);
722 }
723 list_for_each_safe(act, tmp, &mbim->cpkt_resp_q) {
724 cpkt = list_entry(act, struct ctrl_pkt, list);
725 list_del(&cpkt->list);
726 mbim_free_ctrl_pkt(cpkt);
727 }
728 spin_unlock(&mbim->lock);
729}
730
Anna Perela8c991d2012-04-09 16:44:46 +0300731/*
732 * Context: mbim->lock held
733 */
734static void mbim_do_notify(struct f_mbim *mbim)
735{
736 struct usb_request *req = mbim->not_port.notify_req;
737 struct usb_cdc_notification *event;
738 struct usb_composite_dev *cdev = mbim->cdev;
739 __le32 *data;
740 int status;
741
742 pr_info("notify_state: %d", mbim->not_port.notify_state);
743
744 if (!req)
745 return;
746
747 event = req->buf;
748
749 switch (mbim->not_port.notify_state) {
750
751 case NCM_NOTIFY_NONE:
Anna Perel68aeb172012-10-28 09:00:45 +0200752 pr_debug("Notification %02x sent\n", event->bNotificationType);
753
754 if (atomic_read(&mbim->not_port.notify_count) <= 0) {
755 pr_debug("notify_none: done");
756 return;
757 }
758
759 spin_unlock(&mbim->lock);
760 status = usb_ep_queue(mbim->not_port.notify, req, GFP_ATOMIC);
761 spin_lock(&mbim->lock);
762 if (status) {
763 atomic_dec(&mbim->not_port.notify_count);
764 pr_err("Queue notify request failed, err: %d", status);
765 }
766
Anna Perela8c991d2012-04-09 16:44:46 +0300767 return;
768
769 case NCM_NOTIFY_CONNECT:
770 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
771 if (mbim->is_open)
772 event->wValue = cpu_to_le16(1);
773 else
774 event->wValue = cpu_to_le16(0);
775 event->wLength = 0;
776 req->length = sizeof *event;
777
778 pr_info("notify connect %s\n",
779 mbim->is_open ? "true" : "false");
780 mbim->not_port.notify_state = NCM_NOTIFY_NONE;
781 break;
782
783 case NCM_NOTIFY_SPEED:
784 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
785 event->wValue = cpu_to_le16(0);
786 event->wLength = cpu_to_le16(8);
787 req->length = NCM_STATUS_BYTECOUNT;
788
789 /* SPEED_CHANGE data is up/down speeds in bits/sec */
790 data = req->buf + sizeof *event;
791 data[0] = cpu_to_le32(mbim_bitrate(cdev->gadget));
792 data[1] = data[0];
793
794 pr_info("notify speed %d\n",
795 mbim_bitrate(cdev->gadget));
796 mbim->not_port.notify_state = NCM_NOTIFY_CONNECT;
797 break;
798 }
Anna Perel68aeb172012-10-28 09:00:45 +0200799
Anna Perela8c991d2012-04-09 16:44:46 +0300800 event->bmRequestType = 0xA1;
801 event->wIndex = cpu_to_le16(mbim->ctrl_id);
802
Anna Perela8c991d2012-04-09 16:44:46 +0300803 /*
804 * In double buffering if there is a space in FIFO,
805 * completion callback can be called right after the call,
806 * so unlocking
807 */
Anna Perel68aeb172012-10-28 09:00:45 +0200808 atomic_inc(&mbim->not_port.notify_count);
809 pr_debug("queue request: notify_count = %d",
810 atomic_read(&mbim->not_port.notify_count));
Anna Perela8c991d2012-04-09 16:44:46 +0300811 spin_unlock(&mbim->lock);
812 status = usb_ep_queue(mbim->not_port.notify, req, GFP_ATOMIC);
813 spin_lock(&mbim->lock);
Anna Perel68aeb172012-10-28 09:00:45 +0200814 if (status) {
Anna Perela8c991d2012-04-09 16:44:46 +0300815 atomic_dec(&mbim->not_port.notify_count);
816 pr_err("usb_ep_queue failed, err: %d", status);
817 }
818}
819
820/*
821 * Context: mbim->lock held
822 */
823static void mbim_notify(struct f_mbim *mbim)
824{
825 /*
826 * If mbim_notify() is called before the second (CONNECT)
827 * notification is sent, then it will reset to send the SPEED
828 * notificaion again (and again, and again), but it's not a problem
829 */
830 pr_info("dev:%p\n", mbim);
831
832 mbim->not_port.notify_state = NCM_NOTIFY_SPEED;
833 mbim_do_notify(mbim);
834}
835
836static void mbim_notify_complete(struct usb_ep *ep, struct usb_request *req)
837{
838 struct f_mbim *mbim = req->context;
839 struct usb_cdc_notification *event = req->buf;
840
Anna Perel68aeb172012-10-28 09:00:45 +0200841 pr_debug("dev:%p\n", mbim);
Anna Perela8c991d2012-04-09 16:44:46 +0300842
843 spin_lock(&mbim->lock);
844 switch (req->status) {
845 case 0:
Anna Perel68aeb172012-10-28 09:00:45 +0200846 atomic_dec(&mbim->not_port.notify_count);
847 pr_debug("notify_count = %d",
848 atomic_read(&mbim->not_port.notify_count));
Anna Perela8c991d2012-04-09 16:44:46 +0300849 break;
850
851 case -ECONNRESET:
852 case -ESHUTDOWN:
853 /* connection gone */
854 mbim->not_port.notify_state = NCM_NOTIFY_NONE;
855 atomic_set(&mbim->not_port.notify_count, 0);
856 pr_info("ESHUTDOWN/ECONNRESET, connection gone");
Anna Perel86ea7c92012-04-24 14:31:29 +0300857 spin_unlock(&mbim->lock);
858 mbim_clear_queues(mbim);
859 mbim_reset_function_queue(mbim);
Anna Perel89ad1212012-06-13 17:17:24 +0300860 spin_lock(&mbim->lock);
Anna Perela8c991d2012-04-09 16:44:46 +0300861 break;
862 default:
863 pr_err("Unknown event %02x --> %d\n",
864 event->bNotificationType, req->status);
865 break;
866 }
867
Anna Perela8c991d2012-04-09 16:44:46 +0300868 mbim_do_notify(mbim);
Anna Perela8c991d2012-04-09 16:44:46 +0300869 spin_unlock(&mbim->lock);
870
871 pr_info("dev:%p Exit\n", mbim);
872}
873
874static void mbim_ep0out_complete(struct usb_ep *ep, struct usb_request *req)
875{
876 /* now for SET_NTB_INPUT_SIZE only */
877 unsigned in_size = 0;
878 struct usb_function *f = req->context;
879 struct f_mbim *mbim = func_to_mbim(f);
880 struct mbim_ntb_input_size *ntb = NULL;
881
882 pr_info("dev:%p\n", mbim);
883
884 req->context = NULL;
885 if (req->status || req->actual != req->length) {
886 pr_err("Bad control-OUT transfer\n");
887 goto invalid;
888 }
889
890 if (req->length == 4) {
891 in_size = get_unaligned_le32(req->buf);
892 if (in_size < USB_CDC_NCM_NTB_MIN_IN_SIZE ||
893 in_size > le32_to_cpu(ntb_parameters.dwNtbInMaxSize)) {
894 pr_err("Illegal INPUT SIZE (%d) from host\n", in_size);
895 goto invalid;
896 }
897 } else if (req->length == 8) {
898 ntb = (struct mbim_ntb_input_size *)req->buf;
899 in_size = get_unaligned_le32(&(ntb->ntb_input_size));
900 if (in_size < USB_CDC_NCM_NTB_MIN_IN_SIZE ||
901 in_size > le32_to_cpu(ntb_parameters.dwNtbInMaxSize)) {
902 pr_err("Illegal INPUT SIZE (%d) from host\n", in_size);
903 goto invalid;
904 }
905 mbim->ntb_max_datagrams =
906 get_unaligned_le16(&(ntb->ntb_max_datagrams));
907 } else {
908 pr_err("Illegal NTB length %d\n", in_size);
909 goto invalid;
910 }
911
912 pr_info("Set NTB INPUT SIZE %d\n", in_size);
913
914 mbim->ntb_input_size = in_size;
915 return;
916
917invalid:
918 usb_ep_set_halt(ep);
919
920 pr_err("dev:%p Failed\n", mbim);
921
922 return;
923}
924
925static void
926fmbim_cmd_complete(struct usb_ep *ep, struct usb_request *req)
927{
928 struct f_mbim *dev = req->context;
929 struct ctrl_pkt *cpkt = NULL;
930 int len = req->actual;
931
932 if (!dev) {
933 pr_err("mbim dev is null\n");
934 return;
935 }
936
937 if (req->status < 0) {
938 pr_err("mbim command error %d\n", req->status);
939 return;
940 }
941
942 pr_info("dev:%p port#%d\n", dev, dev->port_num);
943
944 spin_lock(&dev->lock);
945 if (!dev->is_open) {
946 pr_err("mbim file handler %p is not open", dev);
947 spin_unlock(&dev->lock);
948 return;
949 }
950
951 cpkt = mbim_alloc_ctrl_pkt(len, GFP_ATOMIC);
952 if (!cpkt) {
953 pr_err("Unable to allocate ctrl pkt\n");
954 spin_unlock(&dev->lock);
955 return;
956 }
957
958 pr_info("Add to cpkt_req_q packet with len = %d\n", len);
959 memcpy(cpkt->buf, req->buf, len);
960 list_add_tail(&cpkt->list, &dev->cpkt_req_q);
961 spin_unlock(&dev->lock);
962
963 /* wakeup read thread */
964 pr_info("Wake up read queue");
965 wake_up(&dev->read_wq);
966
967 return;
968}
969
970static int
971mbim_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
972{
973 struct f_mbim *mbim = func_to_mbim(f);
974 struct usb_composite_dev *cdev = mbim->cdev;
975 struct usb_request *req = cdev->req;
976 struct ctrl_pkt *cpkt = NULL;
977 int value = -EOPNOTSUPP;
978 u16 w_index = le16_to_cpu(ctrl->wIndex);
979 u16 w_value = le16_to_cpu(ctrl->wValue);
980 u16 w_length = le16_to_cpu(ctrl->wLength);
981
982 /*
983 * composite driver infrastructure handles everything except
984 * CDC class messages; interface activation uses set_alt().
985 */
986
987 if (!atomic_read(&mbim->online)) {
988 pr_info("usb cable is not connected\n");
989 return -ENOTCONN;
990 }
991
992 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
993 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
994 | USB_CDC_RESET_FUNCTION:
995
996 pr_info("USB_CDC_RESET_FUNCTION");
997 value = 0;
Anna Perel86ea7c92012-04-24 14:31:29 +0300998 req->complete = fmbim_reset_cmd_complete;
999 req->context = mbim;
Anna Perela8c991d2012-04-09 16:44:46 +03001000 break;
1001
1002 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
1003 | USB_CDC_SEND_ENCAPSULATED_COMMAND:
1004
1005 pr_info("USB_CDC_SEND_ENCAPSULATED_COMMAND");
1006
1007 if (w_length > req->length) {
1008 pr_err("w_length > req->length: %d > %d",
1009 w_length, req->length);
1010 }
1011 value = w_length;
1012 req->complete = fmbim_cmd_complete;
1013 req->context = mbim;
1014 break;
1015
1016 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
1017 | USB_CDC_GET_ENCAPSULATED_RESPONSE:
1018
1019 pr_info("USB_CDC_GET_ENCAPSULATED_RESPONSE");
1020
1021 if (w_value) {
1022 pr_err("w_length > 0: %d", w_length);
1023 break;
1024 }
1025
1026 pr_info("req%02x.%02x v%04x i%04x l%d\n",
1027 ctrl->bRequestType, ctrl->bRequest,
1028 w_value, w_index, w_length);
1029
1030 spin_lock(&mbim->lock);
1031 if (list_empty(&mbim->cpkt_resp_q)) {
1032 pr_err("ctrl resp queue empty\n");
1033 spin_unlock(&mbim->lock);
1034 break;
1035 }
1036
1037 cpkt = list_first_entry(&mbim->cpkt_resp_q,
1038 struct ctrl_pkt, list);
1039 list_del(&cpkt->list);
1040 spin_unlock(&mbim->lock);
1041
1042 value = min_t(unsigned, w_length, cpkt->len);
1043 memcpy(req->buf, cpkt->buf, value);
1044 mbim_free_ctrl_pkt(cpkt);
1045
1046 pr_info("copied encapsulated_response %d bytes",
1047 value);
1048
1049 break;
1050
1051 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
1052 | USB_CDC_GET_NTB_PARAMETERS:
1053
1054 pr_info("USB_CDC_GET_NTB_PARAMETERS");
1055
1056 if (w_length == 0 || w_value != 0 || w_index != mbim->ctrl_id)
1057 break;
1058
1059 value = w_length > sizeof ntb_parameters ?
1060 sizeof ntb_parameters : w_length;
1061 memcpy(req->buf, &ntb_parameters, value);
1062 break;
1063
1064 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
1065 | USB_CDC_GET_NTB_INPUT_SIZE:
1066
1067 pr_info("USB_CDC_GET_NTB_INPUT_SIZE");
1068
1069 if (w_length < 4 || w_value != 0 || w_index != mbim->ctrl_id)
1070 break;
1071
1072 put_unaligned_le32(mbim->ntb_input_size, req->buf);
1073 value = 4;
1074 pr_info("Reply to host INPUT SIZE %d\n",
1075 mbim->ntb_input_size);
1076 break;
1077
1078 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
1079 | USB_CDC_SET_NTB_INPUT_SIZE:
1080
1081 pr_info("USB_CDC_SET_NTB_INPUT_SIZE");
1082
1083 if (w_length != 4 && w_length != 8) {
1084 pr_err("wrong NTB length %d", w_length);
1085 break;
1086 }
1087
1088 if (w_value != 0 || w_index != mbim->ctrl_id)
1089 break;
1090
1091 req->complete = mbim_ep0out_complete;
1092 req->length = w_length;
1093 req->context = f;
1094
1095 value = req->length;
1096 break;
1097
1098 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
1099 | USB_CDC_GET_NTB_FORMAT:
1100 {
1101 uint16_t format;
1102
1103 pr_info("USB_CDC_GET_NTB_FORMAT");
1104
1105 if (w_length < 2 || w_value != 0 || w_index != mbim->ctrl_id)
1106 break;
1107
1108 format = (mbim->parser_opts == &ndp16_opts) ? 0x0000 : 0x0001;
1109 put_unaligned_le16(format, req->buf);
1110 value = 2;
1111 pr_info("NTB FORMAT: sending %d\n", format);
1112 break;
1113 }
1114
1115 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
1116 | USB_CDC_SET_NTB_FORMAT:
1117 {
1118 pr_info("USB_CDC_SET_NTB_FORMAT");
1119
1120 if (w_length != 0 || w_index != mbim->ctrl_id)
1121 break;
1122 switch (w_value) {
1123 case 0x0000:
1124 mbim->parser_opts = &ndp16_opts;
1125 pr_info("NCM16 selected\n");
1126 break;
1127 case 0x0001:
1128 mbim->parser_opts = &ndp32_opts;
1129 pr_info("NCM32 selected\n");
1130 break;
1131 default:
1132 break;
1133 }
1134 value = 0;
1135 break;
1136 }
1137
1138 /* optional in mbim descriptor: */
1139 /* case USB_CDC_GET_MAX_DATAGRAM_SIZE: */
1140 /* case USB_CDC_SET_MAX_DATAGRAM_SIZE: */
1141
1142 default:
1143 pr_err("invalid control req: %02x.%02x v%04x i%04x l%d\n",
1144 ctrl->bRequestType, ctrl->bRequest,
1145 w_value, w_index, w_length);
1146 }
1147
1148 /* respond with data transfer or status phase? */
1149 if (value >= 0) {
1150 pr_info("control request: %02x.%02x v%04x i%04x l%d\n",
1151 ctrl->bRequestType, ctrl->bRequest,
1152 w_value, w_index, w_length);
Anna Perel2dfcaca2012-04-18 17:25:59 +03001153 req->zero = (value < w_length);
Anna Perela8c991d2012-04-09 16:44:46 +03001154 req->length = value;
1155 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1156 if (value < 0) {
1157 pr_err("queueing req failed: %02x.%02x, err %d\n",
1158 ctrl->bRequestType,
1159 ctrl->bRequest, value);
1160 }
1161 } else {
1162 pr_err("ctrl req err %d: %02x.%02x v%04x i%04x l%d\n",
1163 value, ctrl->bRequestType, ctrl->bRequest,
1164 w_value, w_index, w_length);
1165 }
1166
1167 /* device either stalls (value < 0) or reports success */
1168 return value;
1169}
1170
Jack Pham2df2f702012-10-11 19:08:24 -07001171/*
1172 * This function handles the Microsoft-specific OS descriptor control
1173 * requests that are issued by Windows host drivers to determine the
1174 * configuration containing the MBIM function.
1175 *
1176 * Unlike mbim_setup() this function handles two specific device requests,
1177 * and only when a configuration has not yet been selected.
1178 */
1179static int mbim_ctrlrequest(struct usb_composite_dev *cdev,
1180 const struct usb_ctrlrequest *ctrl)
1181{
1182 int value = -EOPNOTSUPP;
1183 u16 w_index = le16_to_cpu(ctrl->wIndex);
1184 u16 w_value = le16_to_cpu(ctrl->wValue);
1185 u16 w_length = le16_to_cpu(ctrl->wLength);
1186
1187 /* only respond to OS desciptors when no configuration selected */
1188 if (cdev->config || !mbim_ext_config_desc.function.subCompatibleID[0])
1189 return value;
1190
1191 pr_debug("%02x.%02x v%04x i%04x l%u",
1192 ctrl->bRequestType, ctrl->bRequest,
1193 w_value, w_index, w_length);
1194
1195 /* Handle MSFT OS string */
1196 if (ctrl->bRequestType ==
1197 (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1198 && ctrl->bRequest == USB_REQ_GET_DESCRIPTOR
1199 && (w_value >> 8) == USB_DT_STRING
1200 && (w_value & 0xFF) == MBIM_OS_STRING_ID) {
1201
1202 value = (w_length < sizeof(mbim_os_string) ?
1203 w_length : sizeof(mbim_os_string));
1204 memcpy(cdev->req->buf, mbim_os_string, value);
1205
1206 } else if (ctrl->bRequestType ==
1207 (USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE)
1208 && ctrl->bRequest == MBIM_VENDOR_CODE && w_index == 4) {
1209
1210 /* Handle Extended OS descriptor */
1211 value = (w_length < sizeof(mbim_ext_config_desc) ?
1212 w_length : sizeof(mbim_ext_config_desc));
1213 memcpy(cdev->req->buf, &mbim_ext_config_desc, value);
1214 }
1215
1216 /* respond with data transfer or status phase? */
1217 if (value >= 0) {
1218 int rc;
1219 cdev->req->zero = value < w_length;
1220 cdev->req->length = value;
1221 rc = usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
1222 if (rc < 0)
1223 pr_err("response queue error: %d", rc);
1224 }
1225 return value;
1226}
1227
Anna Perela8c991d2012-04-09 16:44:46 +03001228static int mbim_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
1229{
1230 struct f_mbim *mbim = func_to_mbim(f);
1231 struct usb_composite_dev *cdev = mbim->cdev;
1232 int ret = 0;
1233
1234 /* Control interface has only altsetting 0 */
1235 if (intf == mbim->ctrl_id) {
1236
1237 pr_info("CONTROL_INTERFACE");
1238
1239 if (alt != 0)
1240 goto fail;
1241
1242 if (mbim->not_port.notify->driver_data) {
1243 pr_info("reset mbim control %d\n", intf);
1244 usb_ep_disable(mbim->not_port.notify);
1245 }
1246
1247 ret = config_ep_by_speed(cdev->gadget, f,
1248 mbim->not_port.notify);
1249 if (ret) {
1250 mbim->not_port.notify->desc = NULL;
1251 pr_err("Failed configuring notify ep %s: err %d\n",
1252 mbim->not_port.notify->name, ret);
1253 return ret;
1254 }
1255
1256 ret = usb_ep_enable(mbim->not_port.notify);
1257 if (ret) {
1258 pr_err("usb ep#%s enable failed, err#%d\n",
1259 mbim->not_port.notify->name, ret);
1260 return ret;
1261 }
1262 mbim->not_port.notify->driver_data = mbim;
1263
1264 /* Data interface has two altsettings, 0 and 1 */
1265 } else if (intf == mbim->data_id) {
1266
1267 pr_info("DATA_INTERFACE");
1268
1269 if (alt > 1)
1270 goto fail;
1271
1272 if (mbim->bam_port.in->driver_data) {
1273 pr_info("reset mbim\n");
1274 mbim_reset_values(mbim);
1275 mbim_bam_disconnect(mbim);
1276 }
1277
1278 /*
1279 * CDC Network only sends data in non-default altsettings.
1280 * Changing altsettings resets filters, statistics, etc.
1281 */
1282 if (alt == 1) {
1283 pr_info("Alt set 1, initialize ports");
1284
1285 if (!mbim->bam_port.in->desc) {
1286
1287 pr_info("Choose endpoints");
1288
1289 ret = config_ep_by_speed(cdev->gadget, f,
1290 mbim->bam_port.in);
1291 if (ret) {
1292 mbim->bam_port.in->desc = NULL;
1293 pr_err("IN ep %s failed: %d\n",
1294 mbim->bam_port.in->name, ret);
1295 return ret;
1296 }
1297
1298 pr_info("Set mbim port in_desc = 0x%p",
1299 mbim->bam_port.in->desc);
1300
1301 ret = config_ep_by_speed(cdev->gadget, f,
1302 mbim->bam_port.out);
1303 if (ret) {
1304 mbim->bam_port.out->desc = NULL;
1305 pr_err("OUT ep %s failed: %d\n",
1306 mbim->bam_port.out->name, ret);
1307 return ret;
1308 }
1309
1310 pr_info("Set mbim port out_desc = 0x%p",
1311 mbim->bam_port.out->desc);
1312 } else {
1313 pr_info("PORTS already SET");
1314 }
1315
1316 pr_info("Activate mbim\n");
1317 mbim_bam_connect(mbim);
1318 }
1319
1320 spin_lock(&mbim->lock);
1321 mbim_notify(mbim);
1322 spin_unlock(&mbim->lock);
1323 } else {
1324 goto fail;
1325 }
1326
1327 atomic_set(&mbim->online, 1);
1328
1329 pr_info("SET DEVICE ONLINE");
1330
1331 /* wakeup file threads */
1332 wake_up(&mbim->read_wq);
1333 wake_up(&mbim->write_wq);
1334
1335 return 0;
1336
1337fail:
1338 pr_err("ERROR: Illegal Interface");
1339 return -EINVAL;
1340}
1341
1342/*
1343 * Because the data interface supports multiple altsettings,
1344 * this MBIM function *MUST* implement a get_alt() method.
1345 */
1346static int mbim_get_alt(struct usb_function *f, unsigned intf)
1347{
1348 struct f_mbim *mbim = func_to_mbim(f);
1349
1350 if (intf == mbim->ctrl_id)
1351 return 0;
1352 return mbim->bam_port.in->driver_data ? 1 : 0;
1353}
1354
1355static void mbim_disable(struct usb_function *f)
1356{
1357 struct f_mbim *mbim = func_to_mbim(f);
1358
1359 pr_info("SET DEVICE OFFLINE");
1360 atomic_set(&mbim->online, 0);
1361
Anna Perel86ea7c92012-04-24 14:31:29 +03001362 mbim_clear_queues(mbim);
1363 mbim_reset_function_queue(mbim);
Anna Perela8c991d2012-04-09 16:44:46 +03001364
1365 mbim_bam_disconnect(mbim);
1366
1367 if (mbim->not_port.notify->driver_data) {
1368 usb_ep_disable(mbim->not_port.notify);
1369 mbim->not_port.notify->driver_data = NULL;
1370 }
1371
Anna Perel68aeb172012-10-28 09:00:45 +02001372 atomic_set(&mbim->not_port.notify_count, 0);
1373
Anna Perela8c991d2012-04-09 16:44:46 +03001374 pr_info("mbim deactivated\n");
1375}
1376
Anna Perel557bf722012-09-20 11:16:35 +03001377#define MBIM_ACTIVE_PORT 0
1378
1379static void mbim_suspend(struct usb_function *f)
1380{
1381 pr_info("mbim suspended\n");
1382 bam_data_suspend(MBIM_ACTIVE_PORT);
1383}
1384
1385static void mbim_resume(struct usb_function *f)
1386{
1387 pr_info("mbim resumed\n");
1388 bam_data_resume(MBIM_ACTIVE_PORT);
1389}
1390
Anna Perela8c991d2012-04-09 16:44:46 +03001391/*---------------------- function driver setup/binding ---------------------*/
1392
1393static int
1394mbim_bind(struct usb_configuration *c, struct usb_function *f)
1395{
1396 struct usb_composite_dev *cdev = c->cdev;
1397 struct f_mbim *mbim = func_to_mbim(f);
1398 int status;
1399 struct usb_ep *ep;
1400
1401 pr_info("Enter");
1402
1403 mbim->cdev = cdev;
1404
1405 /* allocate instance-specific interface IDs */
1406 status = usb_interface_id(c, f);
1407 if (status < 0)
1408 goto fail;
1409 mbim->ctrl_id = status;
1410 mbim_iad_desc.bFirstInterface = status;
1411
1412 mbim_control_intf.bInterfaceNumber = status;
1413 mbim_union_desc.bMasterInterface0 = status;
1414
1415 status = usb_interface_id(c, f);
1416 if (status < 0)
1417 goto fail;
1418 mbim->data_id = status;
1419
1420 mbim_data_nop_intf.bInterfaceNumber = status;
1421 mbim_data_intf.bInterfaceNumber = status;
1422 mbim_union_desc.bSlaveInterface0 = status;
1423
Anna Perel557bf722012-09-20 11:16:35 +03001424 mbim->bam_port.cdev = cdev;
1425
Anna Perela8c991d2012-04-09 16:44:46 +03001426 status = -ENODEV;
1427
1428 /* allocate instance-specific endpoints */
1429 ep = usb_ep_autoconfig(cdev->gadget, &fs_mbim_in_desc);
1430 if (!ep) {
1431 pr_err("usb epin autoconfig failed\n");
1432 goto fail;
1433 }
1434 pr_info("usb epin autoconfig succeeded\n");
1435 ep->driver_data = cdev; /* claim */
1436 mbim->bam_port.in = ep;
1437
1438 ep = usb_ep_autoconfig(cdev->gadget, &fs_mbim_out_desc);
1439 if (!ep) {
1440 pr_err("usb epout autoconfig failed\n");
1441 goto fail;
1442 }
1443 pr_info("usb epout autoconfig succeeded\n");
1444 ep->driver_data = cdev; /* claim */
1445 mbim->bam_port.out = ep;
1446
1447 ep = usb_ep_autoconfig(cdev->gadget, &fs_mbim_notify_desc);
1448 if (!ep) {
1449 pr_err("usb notify ep autoconfig failed\n");
1450 goto fail;
1451 }
1452 pr_info("usb notify ep autoconfig succeeded\n");
1453 mbim->not_port.notify = ep;
1454 ep->driver_data = cdev; /* claim */
1455
1456 status = -ENOMEM;
1457
1458 /* allocate notification request and buffer */
1459 mbim->not_port.notify_req = mbim_alloc_req(ep, NCM_STATUS_BYTECOUNT);
1460 if (!mbim->not_port.notify_req) {
1461 pr_info("failed to allocate notify request\n");
1462 goto fail;
1463 }
1464 pr_info("allocated notify ep request & request buffer\n");
1465
1466 mbim->not_port.notify_req->context = mbim;
1467 mbim->not_port.notify_req->complete = mbim_notify_complete;
1468
1469 /* copy descriptors, and track endpoint copies */
1470 f->descriptors = usb_copy_descriptors(mbim_fs_function);
1471 if (!f->descriptors)
1472 goto fail;
1473
1474 /*
1475 * support all relevant hardware speeds... we expect that when
1476 * hardware is dual speed, all bulk-capable endpoints work at
1477 * both speeds
1478 */
1479 if (gadget_is_dualspeed(c->cdev->gadget)) {
1480 hs_mbim_in_desc.bEndpointAddress =
1481 fs_mbim_in_desc.bEndpointAddress;
1482 hs_mbim_out_desc.bEndpointAddress =
1483 fs_mbim_out_desc.bEndpointAddress;
1484 hs_mbim_notify_desc.bEndpointAddress =
1485 fs_mbim_notify_desc.bEndpointAddress;
1486
1487 /* copy descriptors, and track endpoint copies */
1488 f->hs_descriptors = usb_copy_descriptors(mbim_hs_function);
1489 if (!f->hs_descriptors)
1490 goto fail;
1491 }
1492
Jack Pham2df2f702012-10-11 19:08:24 -07001493 /*
1494 * If MBIM is bound in a config other than the first, tell Windows
1495 * about it by returning the num as a string in the OS descriptor's
1496 * subCompatibleID field. Windows only supports up to config #4.
1497 */
1498 if (c->bConfigurationValue >= 2 && c->bConfigurationValue <= 4) {
1499 pr_debug("MBIM in configuration %d", c->bConfigurationValue);
1500 mbim_ext_config_desc.function.subCompatibleID[0] =
1501 c->bConfigurationValue + '0';
1502 }
1503
Anna Perela8c991d2012-04-09 16:44:46 +03001504 pr_info("mbim(%d): %s speed IN/%s OUT/%s NOTIFY/%s\n",
1505 mbim->port_num,
1506 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
1507 mbim->bam_port.in->name, mbim->bam_port.out->name,
1508 mbim->not_port.notify->name);
1509
1510 return 0;
1511
1512fail:
1513 pr_err("%s failed to bind, err %d\n", f->name, status);
1514
1515 if (f->descriptors)
1516 usb_free_descriptors(f->descriptors);
1517
1518 if (mbim->not_port.notify_req) {
1519 kfree(mbim->not_port.notify_req->buf);
1520 usb_ep_free_request(mbim->not_port.notify,
1521 mbim->not_port.notify_req);
1522 }
1523
1524 /* we might as well release our claims on endpoints */
1525 if (mbim->not_port.notify)
1526 mbim->not_port.notify->driver_data = NULL;
1527 if (mbim->bam_port.out)
1528 mbim->bam_port.out->driver_data = NULL;
1529 if (mbim->bam_port.in)
1530 mbim->bam_port.in->driver_data = NULL;
1531
1532 return status;
1533}
1534
1535static void mbim_unbind(struct usb_configuration *c, struct usb_function *f)
1536{
1537 struct f_mbim *mbim = func_to_mbim(f);
1538
1539 if (gadget_is_dualspeed(c->cdev->gadget))
1540 usb_free_descriptors(f->hs_descriptors);
1541 usb_free_descriptors(f->descriptors);
1542
1543 kfree(mbim->not_port.notify_req->buf);
1544 usb_ep_free_request(mbim->not_port.notify, mbim->not_port.notify_req);
Jack Pham2df2f702012-10-11 19:08:24 -07001545
1546 mbim_ext_config_desc.function.subCompatibleID[0] = 0;
Anna Perela8c991d2012-04-09 16:44:46 +03001547}
1548
1549/**
1550 * mbim_bind_config - add MBIM link to a configuration
1551 * @c: the configuration to support the network link
1552 * Context: single threaded during gadget setup
1553 * Returns zero on success, else negative errno.
1554 */
1555int mbim_bind_config(struct usb_configuration *c, unsigned portno)
1556{
1557 struct f_mbim *mbim = NULL;
1558 int status = 0;
1559
1560 pr_info("port number %u", portno);
1561
1562 if (portno >= nr_mbim_ports) {
1563 pr_err("Can not add port %u. Max ports = %d",
1564 portno, nr_mbim_ports);
1565 return -ENODEV;
1566 }
1567
1568 status = mbim_bam_setup(nr_mbim_ports);
1569 if (status) {
1570 pr_err("bam setup failed");
1571 return status;
1572 }
1573
1574 /* maybe allocate device-global string IDs */
1575 if (mbim_string_defs[0].id == 0) {
1576
1577 /* control interface label */
1578 status = usb_string_id(c->cdev);
1579 if (status < 0)
1580 return status;
1581 mbim_string_defs[STRING_CTRL_IDX].id = status;
1582 mbim_control_intf.iInterface = status;
1583
1584 /* data interface label */
1585 status = usb_string_id(c->cdev);
1586 if (status < 0)
1587 return status;
1588 mbim_string_defs[STRING_DATA_IDX].id = status;
1589 mbim_data_nop_intf.iInterface = status;
1590 mbim_data_intf.iInterface = status;
1591 }
1592
1593 /* allocate and initialize one new instance */
1594 mbim = mbim_ports[0].port;
1595 if (!mbim) {
1596 pr_info("mbim struct not allocated");
1597 return -ENOMEM;
1598 }
1599
1600 mbim->cdev = c->cdev;
1601
Anna Perela8c991d2012-04-09 16:44:46 +03001602 mbim_reset_values(mbim);
1603
1604 mbim->function.name = "usb_mbim";
1605 mbim->function.strings = mbim_strings;
1606 mbim->function.bind = mbim_bind;
1607 mbim->function.unbind = mbim_unbind;
1608 mbim->function.set_alt = mbim_set_alt;
1609 mbim->function.get_alt = mbim_get_alt;
1610 mbim->function.setup = mbim_setup;
1611 mbim->function.disable = mbim_disable;
Anna Perel557bf722012-09-20 11:16:35 +03001612 mbim->function.suspend = mbim_suspend;
1613 mbim->function.resume = mbim_resume;
Anna Perela8c991d2012-04-09 16:44:46 +03001614
1615 INIT_LIST_HEAD(&mbim->cpkt_req_q);
1616 INIT_LIST_HEAD(&mbim->cpkt_resp_q);
1617
1618 status = usb_add_function(c, &mbim->function);
1619
1620 pr_info("Exit status %d", status);
1621
1622 return status;
1623}
1624
1625/* ------------ MBIM DRIVER File Operations API for USER SPACE ------------ */
1626
1627static ssize_t
1628mbim_read(struct file *fp, char __user *buf, size_t count, loff_t *pos)
1629{
1630 struct f_mbim *dev = fp->private_data;
1631 struct ctrl_pkt *cpkt = NULL;
1632 int ret = 0;
1633
1634 pr_debug("Enter(%d)\n", count);
1635
1636 if (!dev) {
1637 pr_err("Received NULL mbim pointer\n");
1638 return -ENODEV;
1639 }
1640
1641 if (count > MBIM_BULK_BUFFER_SIZE) {
1642 pr_err("Buffer size is too big %d, should be at most %d\n",
1643 count, MBIM_BULK_BUFFER_SIZE);
1644 return -EINVAL;
1645 }
1646
1647 if (mbim_lock(&dev->read_excl)) {
1648 pr_err("Previous reading is not finished yet\n");
1649 return -EBUSY;
1650 }
1651
1652 /* block until mbim online */
1653 while (!(atomic_read(&dev->online) || atomic_read(&dev->error))) {
1654 pr_err("USB cable not connected. Wait.\n");
1655 ret = wait_event_interruptible(dev->read_wq,
1656 (atomic_read(&dev->online) ||
1657 atomic_read(&dev->error)));
1658 if (ret < 0) {
1659 mbim_unlock(&dev->read_excl);
1660 return 0;
1661 }
1662 }
1663
1664 if (atomic_read(&dev->error)) {
1665 mbim_unlock(&dev->read_excl);
1666 return -EIO;
1667 }
1668
1669 while (list_empty(&dev->cpkt_req_q)) {
1670 pr_err("Requests list is empty. Wait.\n");
1671 ret = wait_event_interruptible(dev->read_wq,
1672 !list_empty(&dev->cpkt_req_q));
1673 if (ret < 0) {
1674 pr_err("Waiting failed\n");
1675 mbim_unlock(&dev->read_excl);
1676 return 0;
1677 }
1678 pr_debug("Received request packet\n");
1679 }
1680
1681 cpkt = list_first_entry(&dev->cpkt_req_q, struct ctrl_pkt,
1682 list);
1683 if (cpkt->len > count) {
1684 mbim_unlock(&dev->read_excl);
1685 pr_err("cpkt size too big:%d > buf size:%d\n",
1686 cpkt->len, count);
1687 return -ENOMEM;
1688 }
1689
1690 pr_debug("cpkt size:%d\n", cpkt->len);
1691
1692 list_del(&cpkt->list);
1693 mbim_unlock(&dev->read_excl);
1694
1695 ret = copy_to_user(buf, cpkt->buf, cpkt->len);
1696 if (ret) {
1697 pr_err("copy_to_user failed: err %d\n", ret);
1698 ret = 0;
1699 } else {
1700 pr_debug("copied %d bytes to user\n", cpkt->len);
1701 ret = cpkt->len;
1702 }
1703
1704 mbim_free_ctrl_pkt(cpkt);
1705
1706 return ret;
1707}
1708
1709static ssize_t
1710mbim_write(struct file *fp, const char __user *buf, size_t count, loff_t *pos)
1711{
1712 struct f_mbim *dev = fp->private_data;
1713 struct ctrl_pkt *cpkt = NULL;
1714 int ret = 0;
1715
1716 pr_debug("Enter(%d)", count);
1717
1718 if (!dev) {
1719 pr_err("Received NULL mbim pointer\n");
1720 return -ENODEV;
1721 }
1722
1723 if (!count) {
1724 pr_err("zero length ctrl pkt\n");
1725 return -ENODEV;
1726 }
1727
1728 if (count > MAX_CTRL_PKT_SIZE) {
1729 pr_err("given pkt size too big:%d > max_pkt_size:%d\n",
1730 count, MAX_CTRL_PKT_SIZE);
1731 return -ENOMEM;
1732 }
1733
1734 if (mbim_lock(&dev->write_excl)) {
1735 pr_err("Previous writing not finished yet\n");
1736 return -EBUSY;
1737 }
1738
1739 if (!atomic_read(&dev->online)) {
1740 pr_err("USB cable not connected\n");
1741 mbim_unlock(&dev->write_excl);
1742 return -EPIPE;
1743 }
1744
1745 cpkt = mbim_alloc_ctrl_pkt(count, GFP_KERNEL);
1746 if (!cpkt) {
1747 pr_err("failed to allocate ctrl pkt\n");
1748 mbim_unlock(&dev->write_excl);
1749 return -ENOMEM;
1750 }
1751
1752 ret = copy_from_user(cpkt->buf, buf, count);
1753 if (ret) {
1754 pr_err("copy_from_user failed err:%d\n", ret);
1755 mbim_free_ctrl_pkt(cpkt);
1756 mbim_unlock(&dev->write_excl);
1757 return 0;
1758 }
1759
1760 fmbim_send_cpkt_response(dev, cpkt);
1761
1762 mbim_unlock(&dev->write_excl);
1763
1764 pr_debug("Exit(%d)", count);
1765
1766 return count;
Anna Perel89ad1212012-06-13 17:17:24 +03001767
Anna Perela8c991d2012-04-09 16:44:46 +03001768}
1769
1770static int mbim_open(struct inode *ip, struct file *fp)
1771{
1772 pr_info("Open mbim driver\n");
1773
1774 while (!_mbim_dev) {
1775 pr_err("mbim_dev not created yet\n");
1776 return -ENODEV;
1777 }
1778
1779 if (mbim_lock(&_mbim_dev->open_excl)) {
1780 pr_err("Already opened\n");
1781 return -EBUSY;
1782 }
1783
1784 pr_info("Lock mbim_dev->open_excl for open\n");
1785
1786 if (!atomic_read(&_mbim_dev->online))
1787 pr_err("USB cable not connected\n");
1788
Anna Perela8c991d2012-04-09 16:44:46 +03001789 fp->private_data = _mbim_dev;
1790
1791 atomic_set(&_mbim_dev->error, 0);
1792
1793 spin_lock(&_mbim_dev->lock);
1794 _mbim_dev->is_open = true;
Anna Perela8c991d2012-04-09 16:44:46 +03001795 spin_unlock(&_mbim_dev->lock);
1796
1797 pr_info("Exit, mbim file opened\n");
1798
1799 return 0;
1800}
1801
1802static int mbim_release(struct inode *ip, struct file *fp)
1803{
1804 struct f_mbim *mbim = fp->private_data;
1805
1806 pr_info("Close mbim file");
1807
1808 spin_lock(&mbim->lock);
1809 mbim->is_open = false;
Anna Perela8c991d2012-04-09 16:44:46 +03001810 spin_unlock(&mbim->lock);
1811
Anna Perela8c991d2012-04-09 16:44:46 +03001812 mbim_unlock(&_mbim_dev->open_excl);
1813
1814 return 0;
1815}
1816
1817static long mbim_ioctl(struct file *fp, unsigned cmd, unsigned long arg)
1818{
1819 struct f_mbim *mbim = fp->private_data;
1820 int ret = 0;
1821
1822 pr_info("Received command %d", cmd);
1823
1824 if (mbim_lock(&mbim->ioctl_excl))
1825 return -EBUSY;
1826
1827 switch (cmd) {
1828 case MBIM_GET_NTB_SIZE:
1829 ret = copy_to_user((void __user *)arg,
1830 &mbim->ntb_input_size, sizeof(mbim->ntb_input_size));
1831 if (ret) {
1832 pr_err("copying to user space failed");
1833 ret = -EFAULT;
1834 }
1835 pr_info("Sent NTB size %d", mbim->ntb_input_size);
1836 break;
1837 case MBIM_GET_DATAGRAM_COUNT:
1838 ret = copy_to_user((void __user *)arg,
1839 &mbim->ntb_max_datagrams,
1840 sizeof(mbim->ntb_max_datagrams));
1841 if (ret) {
1842 pr_err("copying to user space failed");
1843 ret = -EFAULT;
1844 }
1845 pr_info("Sent NTB datagrams count %d",
1846 mbim->ntb_max_datagrams);
1847 break;
1848 default:
1849 pr_err("wrong parameter");
1850 ret = -EINVAL;
1851 }
1852
1853 mbim_unlock(&mbim->ioctl_excl);
1854
1855 return ret;
1856}
1857
1858/* file operations for MBIM device /dev/android_mbim */
1859static const struct file_operations mbim_fops = {
1860 .owner = THIS_MODULE,
1861 .open = mbim_open,
1862 .release = mbim_release,
1863 .read = mbim_read,
1864 .write = mbim_write,
1865 .unlocked_ioctl = mbim_ioctl,
1866};
1867
1868static struct miscdevice mbim_device = {
1869 .minor = MISC_DYNAMIC_MINOR,
1870 .name = "android_mbim",
1871 .fops = &mbim_fops,
1872};
1873
1874static int mbim_init(int instances)
1875{
1876 int i;
1877 struct f_mbim *dev = NULL;
1878 int ret;
1879
1880 pr_info("initialize %d instances\n", instances);
1881
1882 if (instances > NR_MBIM_PORTS) {
1883 pr_err("Max-%d instances supported\n", NR_MBIM_PORTS);
1884 return -EINVAL;
1885 }
1886
1887 for (i = 0; i < instances; i++) {
1888 dev = kzalloc(sizeof(struct f_mbim), GFP_KERNEL);
1889 if (!dev) {
1890 pr_err("Failed to allocate mbim dev\n");
1891 ret = -ENOMEM;
1892 goto fail_probe;
1893 }
1894
1895 dev->port_num = i;
1896 spin_lock_init(&dev->lock);
1897 INIT_LIST_HEAD(&dev->cpkt_req_q);
1898 INIT_LIST_HEAD(&dev->cpkt_resp_q);
1899
1900 mbim_ports[i].port = dev;
1901 mbim_ports[i].port_num = i;
1902
1903 init_waitqueue_head(&dev->read_wq);
1904 init_waitqueue_head(&dev->write_wq);
1905
1906 atomic_set(&dev->open_excl, 0);
1907 atomic_set(&dev->ioctl_excl, 0);
1908 atomic_set(&dev->read_excl, 0);
1909 atomic_set(&dev->write_excl, 0);
1910
1911 nr_mbim_ports++;
1912
1913 }
1914
1915 _mbim_dev = dev;
1916 ret = misc_register(&mbim_device);
1917 if (ret) {
1918 pr_err("mbim driver failed to register");
1919 goto fail_probe;
1920 }
1921
1922 pr_info("Initialized %d ports\n", nr_mbim_ports);
1923
1924 return ret;
1925
1926fail_probe:
1927 pr_err("Failed");
1928 for (i = 0; i < nr_mbim_ports; i++) {
1929 kfree(mbim_ports[i].port);
1930 mbim_ports[i].port = NULL;
1931 }
1932
1933 return ret;
1934}
1935
1936static void fmbim_cleanup(void)
1937{
1938 int i = 0;
1939
1940 pr_info("Enter");
1941
1942 for (i = 0; i < nr_mbim_ports; i++) {
1943 kfree(mbim_ports[i].port);
1944 mbim_ports[i].port = NULL;
1945 }
1946 nr_mbim_ports = 0;
1947
1948 misc_deregister(&mbim_device);
1949
1950 _mbim_dev = NULL;
1951}
1952