blob: db55c8c8c743d9370132eddac23c6fc82d7ff2cd [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
544 int notif_c = 0;
545
546 pr_info("dev:%p portno#%d\n", dev, dev->port_num);
547
548 spin_lock_irqsave(&dev->lock, flags);
549
550 if (!atomic_read(&dev->online)) {
551 pr_info("dev:%p is not online\n", dev);
552 spin_unlock_irqrestore(&dev->lock, flags);
553 return;
554 }
555
556 if (!req) {
557 pr_info("dev:%p req is NULL\n", dev);
558 spin_unlock_irqrestore(&dev->lock, flags);
559 return;
560 }
561
562 if (!req->buf) {
563 pr_info("dev:%p req->buf is NULL\n", dev);
564 spin_unlock_irqrestore(&dev->lock, flags);
565 return;
566 }
567
568 notif_c = atomic_inc_return(&dev->not_port.notify_count);
569 pr_info("atomic_inc_return[notif_c] = %d", notif_c);
570
571 event = req->buf;
572 event->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS
573 | USB_RECIP_INTERFACE;
574 event->bNotificationType = USB_CDC_NOTIFY_RESPONSE_AVAILABLE;
575 event->wValue = cpu_to_le16(0);
576 event->wIndex = cpu_to_le16(dev->ctrl_id);
577 event->wLength = cpu_to_le16(0);
578 spin_unlock_irqrestore(&dev->lock, flags);
579
580 pr_info("Call usb_ep_queue");
581
582 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
589 pr_info("Succcessfull Exit");
590}
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
675 atomic_set(&mbim->not_port.notify_count, 0);
676 atomic_set(&mbim->online, 0);
677}
678
Anna Perel86ea7c92012-04-24 14:31:29 +0300679static void mbim_reset_function_queue(struct f_mbim *dev)
680{
681 struct ctrl_pkt *cpkt = NULL;
682
683 pr_debug("Queue empty packet for QBI");
684
685 spin_lock(&dev->lock);
686 if (!dev->is_open) {
687 pr_err("%s: mbim file handler %p is not open", __func__, dev);
688 spin_unlock(&dev->lock);
689 return;
690 }
691
692 cpkt = mbim_alloc_ctrl_pkt(0, GFP_ATOMIC);
693 if (!cpkt) {
694 pr_err("%s: Unable to allocate reset function pkt\n", __func__);
695 spin_unlock(&dev->lock);
696 return;
697 }
698
699 list_add_tail(&cpkt->list, &dev->cpkt_req_q);
700 spin_unlock(&dev->lock);
701
702 pr_debug("%s: Wake up read queue", __func__);
703 wake_up(&dev->read_wq);
704}
705
706static void fmbim_reset_cmd_complete(struct usb_ep *ep, struct usb_request *req)
707{
708 struct f_mbim *dev = req->context;
709
710 mbim_reset_function_queue(dev);
711}
712
713static void mbim_clear_queues(struct f_mbim *mbim)
714{
715 struct ctrl_pkt *cpkt = NULL;
716 struct list_head *act, *tmp;
717
718 spin_lock(&mbim->lock);
719 list_for_each_safe(act, tmp, &mbim->cpkt_req_q) {
720 cpkt = list_entry(act, struct ctrl_pkt, list);
721 list_del(&cpkt->list);
722 mbim_free_ctrl_pkt(cpkt);
723 }
724 list_for_each_safe(act, tmp, &mbim->cpkt_resp_q) {
725 cpkt = list_entry(act, struct ctrl_pkt, list);
726 list_del(&cpkt->list);
727 mbim_free_ctrl_pkt(cpkt);
728 }
729 spin_unlock(&mbim->lock);
730}
731
Anna Perela8c991d2012-04-09 16:44:46 +0300732/*
733 * Context: mbim->lock held
734 */
735static void mbim_do_notify(struct f_mbim *mbim)
736{
737 struct usb_request *req = mbim->not_port.notify_req;
738 struct usb_cdc_notification *event;
739 struct usb_composite_dev *cdev = mbim->cdev;
740 __le32 *data;
741 int status;
742
743 pr_info("notify_state: %d", mbim->not_port.notify_state);
744
745 if (!req)
746 return;
747
748 event = req->buf;
749
750 switch (mbim->not_port.notify_state) {
751
752 case NCM_NOTIFY_NONE:
753 return;
754
755 case NCM_NOTIFY_CONNECT:
756 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
757 if (mbim->is_open)
758 event->wValue = cpu_to_le16(1);
759 else
760 event->wValue = cpu_to_le16(0);
761 event->wLength = 0;
762 req->length = sizeof *event;
763
764 pr_info("notify connect %s\n",
765 mbim->is_open ? "true" : "false");
766 mbim->not_port.notify_state = NCM_NOTIFY_NONE;
767 break;
768
769 case NCM_NOTIFY_SPEED:
770 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
771 event->wValue = cpu_to_le16(0);
772 event->wLength = cpu_to_le16(8);
773 req->length = NCM_STATUS_BYTECOUNT;
774
775 /* SPEED_CHANGE data is up/down speeds in bits/sec */
776 data = req->buf + sizeof *event;
777 data[0] = cpu_to_le32(mbim_bitrate(cdev->gadget));
778 data[1] = data[0];
779
780 pr_info("notify speed %d\n",
781 mbim_bitrate(cdev->gadget));
782 mbim->not_port.notify_state = NCM_NOTIFY_CONNECT;
783 break;
784 }
785 event->bmRequestType = 0xA1;
786 event->wIndex = cpu_to_le16(mbim->ctrl_id);
787
788 mbim->not_port.notify_req = NULL;
789 /*
790 * In double buffering if there is a space in FIFO,
791 * completion callback can be called right after the call,
792 * so unlocking
793 */
794 spin_unlock(&mbim->lock);
795 status = usb_ep_queue(mbim->not_port.notify, req, GFP_ATOMIC);
796 spin_lock(&mbim->lock);
797 if (status < 0) {
798 mbim->not_port.notify_req = req;
799 atomic_dec(&mbim->not_port.notify_count);
800 pr_err("usb_ep_queue failed, err: %d", status);
801 }
802}
803
804/*
805 * Context: mbim->lock held
806 */
807static void mbim_notify(struct f_mbim *mbim)
808{
809 /*
810 * If mbim_notify() is called before the second (CONNECT)
811 * notification is sent, then it will reset to send the SPEED
812 * notificaion again (and again, and again), but it's not a problem
813 */
814 pr_info("dev:%p\n", mbim);
815
816 mbim->not_port.notify_state = NCM_NOTIFY_SPEED;
817 mbim_do_notify(mbim);
818}
819
820static void mbim_notify_complete(struct usb_ep *ep, struct usb_request *req)
821{
822 struct f_mbim *mbim = req->context;
823 struct usb_cdc_notification *event = req->buf;
824
825 int notif_c = 0;
826
827 pr_info("dev:%p\n", mbim);
828
829 spin_lock(&mbim->lock);
830 switch (req->status) {
831 case 0:
832 pr_info("Notification %02x sent\n",
833 event->bNotificationType);
834
835 notif_c = atomic_dec_return(&mbim->not_port.notify_count);
836
837 if (notif_c != 0) {
838 pr_info("Continue to mbim_do_notify()");
839 break;
840 } else {
841 pr_info("notify_count decreased to 0. Do not notify");
842 spin_unlock(&mbim->lock);
843 return;
844 }
845
846 break;
847
848 case -ECONNRESET:
849 case -ESHUTDOWN:
850 /* connection gone */
851 mbim->not_port.notify_state = NCM_NOTIFY_NONE;
852 atomic_set(&mbim->not_port.notify_count, 0);
853 pr_info("ESHUTDOWN/ECONNRESET, connection gone");
Anna Perel86ea7c92012-04-24 14:31:29 +0300854 spin_unlock(&mbim->lock);
855 mbim_clear_queues(mbim);
856 mbim_reset_function_queue(mbim);
Anna Perel89ad1212012-06-13 17:17:24 +0300857 spin_lock(&mbim->lock);
Anna Perela8c991d2012-04-09 16:44:46 +0300858 break;
859 default:
860 pr_err("Unknown event %02x --> %d\n",
861 event->bNotificationType, req->status);
862 break;
863 }
864
865 mbim->not_port.notify_req = req;
866 mbim_do_notify(mbim);
867
868 spin_unlock(&mbim->lock);
869
870 pr_info("dev:%p Exit\n", mbim);
871}
872
873static void mbim_ep0out_complete(struct usb_ep *ep, struct usb_request *req)
874{
875 /* now for SET_NTB_INPUT_SIZE only */
876 unsigned in_size = 0;
877 struct usb_function *f = req->context;
878 struct f_mbim *mbim = func_to_mbim(f);
879 struct mbim_ntb_input_size *ntb = NULL;
880
881 pr_info("dev:%p\n", mbim);
882
883 req->context = NULL;
884 if (req->status || req->actual != req->length) {
885 pr_err("Bad control-OUT transfer\n");
886 goto invalid;
887 }
888
889 if (req->length == 4) {
890 in_size = get_unaligned_le32(req->buf);
891 if (in_size < USB_CDC_NCM_NTB_MIN_IN_SIZE ||
892 in_size > le32_to_cpu(ntb_parameters.dwNtbInMaxSize)) {
893 pr_err("Illegal INPUT SIZE (%d) from host\n", in_size);
894 goto invalid;
895 }
896 } else if (req->length == 8) {
897 ntb = (struct mbim_ntb_input_size *)req->buf;
898 in_size = get_unaligned_le32(&(ntb->ntb_input_size));
899 if (in_size < USB_CDC_NCM_NTB_MIN_IN_SIZE ||
900 in_size > le32_to_cpu(ntb_parameters.dwNtbInMaxSize)) {
901 pr_err("Illegal INPUT SIZE (%d) from host\n", in_size);
902 goto invalid;
903 }
904 mbim->ntb_max_datagrams =
905 get_unaligned_le16(&(ntb->ntb_max_datagrams));
906 } else {
907 pr_err("Illegal NTB length %d\n", in_size);
908 goto invalid;
909 }
910
911 pr_info("Set NTB INPUT SIZE %d\n", in_size);
912
913 mbim->ntb_input_size = in_size;
914 return;
915
916invalid:
917 usb_ep_set_halt(ep);
918
919 pr_err("dev:%p Failed\n", mbim);
920
921 return;
922}
923
924static void
925fmbim_cmd_complete(struct usb_ep *ep, struct usb_request *req)
926{
927 struct f_mbim *dev = req->context;
928 struct ctrl_pkt *cpkt = NULL;
929 int len = req->actual;
930
931 if (!dev) {
932 pr_err("mbim dev is null\n");
933 return;
934 }
935
936 if (req->status < 0) {
937 pr_err("mbim command error %d\n", req->status);
938 return;
939 }
940
941 pr_info("dev:%p port#%d\n", dev, dev->port_num);
942
943 spin_lock(&dev->lock);
944 if (!dev->is_open) {
945 pr_err("mbim file handler %p is not open", dev);
946 spin_unlock(&dev->lock);
947 return;
948 }
949
950 cpkt = mbim_alloc_ctrl_pkt(len, GFP_ATOMIC);
951 if (!cpkt) {
952 pr_err("Unable to allocate ctrl pkt\n");
953 spin_unlock(&dev->lock);
954 return;
955 }
956
957 pr_info("Add to cpkt_req_q packet with len = %d\n", len);
958 memcpy(cpkt->buf, req->buf, len);
959 list_add_tail(&cpkt->list, &dev->cpkt_req_q);
960 spin_unlock(&dev->lock);
961
962 /* wakeup read thread */
963 pr_info("Wake up read queue");
964 wake_up(&dev->read_wq);
965
966 return;
967}
968
969static int
970mbim_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
971{
972 struct f_mbim *mbim = func_to_mbim(f);
973 struct usb_composite_dev *cdev = mbim->cdev;
974 struct usb_request *req = cdev->req;
975 struct ctrl_pkt *cpkt = NULL;
976 int value = -EOPNOTSUPP;
977 u16 w_index = le16_to_cpu(ctrl->wIndex);
978 u16 w_value = le16_to_cpu(ctrl->wValue);
979 u16 w_length = le16_to_cpu(ctrl->wLength);
980
981 /*
982 * composite driver infrastructure handles everything except
983 * CDC class messages; interface activation uses set_alt().
984 */
985
986 if (!atomic_read(&mbim->online)) {
987 pr_info("usb cable is not connected\n");
988 return -ENOTCONN;
989 }
990
991 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
992 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
993 | USB_CDC_RESET_FUNCTION:
994
995 pr_info("USB_CDC_RESET_FUNCTION");
996 value = 0;
Anna Perel86ea7c92012-04-24 14:31:29 +0300997 req->complete = fmbim_reset_cmd_complete;
998 req->context = mbim;
Anna Perela8c991d2012-04-09 16:44:46 +0300999 break;
1000
1001 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
1002 | USB_CDC_SEND_ENCAPSULATED_COMMAND:
1003
1004 pr_info("USB_CDC_SEND_ENCAPSULATED_COMMAND");
1005
1006 if (w_length > req->length) {
1007 pr_err("w_length > req->length: %d > %d",
1008 w_length, req->length);
1009 }
1010 value = w_length;
1011 req->complete = fmbim_cmd_complete;
1012 req->context = mbim;
1013 break;
1014
1015 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
1016 | USB_CDC_GET_ENCAPSULATED_RESPONSE:
1017
1018 pr_info("USB_CDC_GET_ENCAPSULATED_RESPONSE");
1019
1020 if (w_value) {
1021 pr_err("w_length > 0: %d", w_length);
1022 break;
1023 }
1024
1025 pr_info("req%02x.%02x v%04x i%04x l%d\n",
1026 ctrl->bRequestType, ctrl->bRequest,
1027 w_value, w_index, w_length);
1028
1029 spin_lock(&mbim->lock);
1030 if (list_empty(&mbim->cpkt_resp_q)) {
1031 pr_err("ctrl resp queue empty\n");
1032 spin_unlock(&mbim->lock);
1033 break;
1034 }
1035
1036 cpkt = list_first_entry(&mbim->cpkt_resp_q,
1037 struct ctrl_pkt, list);
1038 list_del(&cpkt->list);
1039 spin_unlock(&mbim->lock);
1040
1041 value = min_t(unsigned, w_length, cpkt->len);
1042 memcpy(req->buf, cpkt->buf, value);
1043 mbim_free_ctrl_pkt(cpkt);
1044
1045 pr_info("copied encapsulated_response %d bytes",
1046 value);
1047
1048 break;
1049
1050 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
1051 | USB_CDC_GET_NTB_PARAMETERS:
1052
1053 pr_info("USB_CDC_GET_NTB_PARAMETERS");
1054
1055 if (w_length == 0 || w_value != 0 || w_index != mbim->ctrl_id)
1056 break;
1057
1058 value = w_length > sizeof ntb_parameters ?
1059 sizeof ntb_parameters : w_length;
1060 memcpy(req->buf, &ntb_parameters, value);
1061 break;
1062
1063 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
1064 | USB_CDC_GET_NTB_INPUT_SIZE:
1065
1066 pr_info("USB_CDC_GET_NTB_INPUT_SIZE");
1067
1068 if (w_length < 4 || w_value != 0 || w_index != mbim->ctrl_id)
1069 break;
1070
1071 put_unaligned_le32(mbim->ntb_input_size, req->buf);
1072 value = 4;
1073 pr_info("Reply to host INPUT SIZE %d\n",
1074 mbim->ntb_input_size);
1075 break;
1076
1077 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
1078 | USB_CDC_SET_NTB_INPUT_SIZE:
1079
1080 pr_info("USB_CDC_SET_NTB_INPUT_SIZE");
1081
1082 if (w_length != 4 && w_length != 8) {
1083 pr_err("wrong NTB length %d", w_length);
1084 break;
1085 }
1086
1087 if (w_value != 0 || w_index != mbim->ctrl_id)
1088 break;
1089
1090 req->complete = mbim_ep0out_complete;
1091 req->length = w_length;
1092 req->context = f;
1093
1094 value = req->length;
1095 break;
1096
1097 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
1098 | USB_CDC_GET_NTB_FORMAT:
1099 {
1100 uint16_t format;
1101
1102 pr_info("USB_CDC_GET_NTB_FORMAT");
1103
1104 if (w_length < 2 || w_value != 0 || w_index != mbim->ctrl_id)
1105 break;
1106
1107 format = (mbim->parser_opts == &ndp16_opts) ? 0x0000 : 0x0001;
1108 put_unaligned_le16(format, req->buf);
1109 value = 2;
1110 pr_info("NTB FORMAT: sending %d\n", format);
1111 break;
1112 }
1113
1114 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
1115 | USB_CDC_SET_NTB_FORMAT:
1116 {
1117 pr_info("USB_CDC_SET_NTB_FORMAT");
1118
1119 if (w_length != 0 || w_index != mbim->ctrl_id)
1120 break;
1121 switch (w_value) {
1122 case 0x0000:
1123 mbim->parser_opts = &ndp16_opts;
1124 pr_info("NCM16 selected\n");
1125 break;
1126 case 0x0001:
1127 mbim->parser_opts = &ndp32_opts;
1128 pr_info("NCM32 selected\n");
1129 break;
1130 default:
1131 break;
1132 }
1133 value = 0;
1134 break;
1135 }
1136
1137 /* optional in mbim descriptor: */
1138 /* case USB_CDC_GET_MAX_DATAGRAM_SIZE: */
1139 /* case USB_CDC_SET_MAX_DATAGRAM_SIZE: */
1140
1141 default:
1142 pr_err("invalid control req: %02x.%02x v%04x i%04x l%d\n",
1143 ctrl->bRequestType, ctrl->bRequest,
1144 w_value, w_index, w_length);
1145 }
1146
1147 /* respond with data transfer or status phase? */
1148 if (value >= 0) {
1149 pr_info("control request: %02x.%02x v%04x i%04x l%d\n",
1150 ctrl->bRequestType, ctrl->bRequest,
1151 w_value, w_index, w_length);
Anna Perel2dfcaca2012-04-18 17:25:59 +03001152 req->zero = (value < w_length);
Anna Perela8c991d2012-04-09 16:44:46 +03001153 req->length = value;
1154 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1155 if (value < 0) {
1156 pr_err("queueing req failed: %02x.%02x, err %d\n",
1157 ctrl->bRequestType,
1158 ctrl->bRequest, value);
1159 }
1160 } else {
1161 pr_err("ctrl req err %d: %02x.%02x v%04x i%04x l%d\n",
1162 value, ctrl->bRequestType, ctrl->bRequest,
1163 w_value, w_index, w_length);
1164 }
1165
1166 /* device either stalls (value < 0) or reports success */
1167 return value;
1168}
1169
Jack Pham2df2f702012-10-11 19:08:24 -07001170/*
1171 * This function handles the Microsoft-specific OS descriptor control
1172 * requests that are issued by Windows host drivers to determine the
1173 * configuration containing the MBIM function.
1174 *
1175 * Unlike mbim_setup() this function handles two specific device requests,
1176 * and only when a configuration has not yet been selected.
1177 */
1178static int mbim_ctrlrequest(struct usb_composite_dev *cdev,
1179 const struct usb_ctrlrequest *ctrl)
1180{
1181 int value = -EOPNOTSUPP;
1182 u16 w_index = le16_to_cpu(ctrl->wIndex);
1183 u16 w_value = le16_to_cpu(ctrl->wValue);
1184 u16 w_length = le16_to_cpu(ctrl->wLength);
1185
1186 /* only respond to OS desciptors when no configuration selected */
1187 if (cdev->config || !mbim_ext_config_desc.function.subCompatibleID[0])
1188 return value;
1189
1190 pr_debug("%02x.%02x v%04x i%04x l%u",
1191 ctrl->bRequestType, ctrl->bRequest,
1192 w_value, w_index, w_length);
1193
1194 /* Handle MSFT OS string */
1195 if (ctrl->bRequestType ==
1196 (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1197 && ctrl->bRequest == USB_REQ_GET_DESCRIPTOR
1198 && (w_value >> 8) == USB_DT_STRING
1199 && (w_value & 0xFF) == MBIM_OS_STRING_ID) {
1200
1201 value = (w_length < sizeof(mbim_os_string) ?
1202 w_length : sizeof(mbim_os_string));
1203 memcpy(cdev->req->buf, mbim_os_string, value);
1204
1205 } else if (ctrl->bRequestType ==
1206 (USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE)
1207 && ctrl->bRequest == MBIM_VENDOR_CODE && w_index == 4) {
1208
1209 /* Handle Extended OS descriptor */
1210 value = (w_length < sizeof(mbim_ext_config_desc) ?
1211 w_length : sizeof(mbim_ext_config_desc));
1212 memcpy(cdev->req->buf, &mbim_ext_config_desc, value);
1213 }
1214
1215 /* respond with data transfer or status phase? */
1216 if (value >= 0) {
1217 int rc;
1218 cdev->req->zero = value < w_length;
1219 cdev->req->length = value;
1220 rc = usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
1221 if (rc < 0)
1222 pr_err("response queue error: %d", rc);
1223 }
1224 return value;
1225}
1226
Anna Perela8c991d2012-04-09 16:44:46 +03001227static int mbim_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
1228{
1229 struct f_mbim *mbim = func_to_mbim(f);
1230 struct usb_composite_dev *cdev = mbim->cdev;
1231 int ret = 0;
1232
1233 /* Control interface has only altsetting 0 */
1234 if (intf == mbim->ctrl_id) {
1235
1236 pr_info("CONTROL_INTERFACE");
1237
1238 if (alt != 0)
1239 goto fail;
1240
1241 if (mbim->not_port.notify->driver_data) {
1242 pr_info("reset mbim control %d\n", intf);
1243 usb_ep_disable(mbim->not_port.notify);
1244 }
1245
1246 ret = config_ep_by_speed(cdev->gadget, f,
1247 mbim->not_port.notify);
1248 if (ret) {
1249 mbim->not_port.notify->desc = NULL;
1250 pr_err("Failed configuring notify ep %s: err %d\n",
1251 mbim->not_port.notify->name, ret);
1252 return ret;
1253 }
1254
1255 ret = usb_ep_enable(mbim->not_port.notify);
1256 if (ret) {
1257 pr_err("usb ep#%s enable failed, err#%d\n",
1258 mbim->not_port.notify->name, ret);
1259 return ret;
1260 }
1261 mbim->not_port.notify->driver_data = mbim;
1262
1263 /* Data interface has two altsettings, 0 and 1 */
1264 } else if (intf == mbim->data_id) {
1265
1266 pr_info("DATA_INTERFACE");
1267
1268 if (alt > 1)
1269 goto fail;
1270
1271 if (mbim->bam_port.in->driver_data) {
1272 pr_info("reset mbim\n");
1273 mbim_reset_values(mbim);
1274 mbim_bam_disconnect(mbim);
1275 }
1276
1277 /*
1278 * CDC Network only sends data in non-default altsettings.
1279 * Changing altsettings resets filters, statistics, etc.
1280 */
1281 if (alt == 1) {
1282 pr_info("Alt set 1, initialize ports");
1283
1284 if (!mbim->bam_port.in->desc) {
1285
1286 pr_info("Choose endpoints");
1287
1288 ret = config_ep_by_speed(cdev->gadget, f,
1289 mbim->bam_port.in);
1290 if (ret) {
1291 mbim->bam_port.in->desc = NULL;
1292 pr_err("IN ep %s failed: %d\n",
1293 mbim->bam_port.in->name, ret);
1294 return ret;
1295 }
1296
1297 pr_info("Set mbim port in_desc = 0x%p",
1298 mbim->bam_port.in->desc);
1299
1300 ret = config_ep_by_speed(cdev->gadget, f,
1301 mbim->bam_port.out);
1302 if (ret) {
1303 mbim->bam_port.out->desc = NULL;
1304 pr_err("OUT ep %s failed: %d\n",
1305 mbim->bam_port.out->name, ret);
1306 return ret;
1307 }
1308
1309 pr_info("Set mbim port out_desc = 0x%p",
1310 mbim->bam_port.out->desc);
Anna Perel6637bd72012-10-23 10:53:32 +02001311
1312 pr_debug("Activate mbim\n");
1313 mbim_bam_connect(mbim);
1314
Anna Perela8c991d2012-04-09 16:44:46 +03001315 } else {
1316 pr_info("PORTS already SET");
1317 }
Anna Perela8c991d2012-04-09 16:44:46 +03001318 }
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
1372 pr_info("mbim deactivated\n");
1373}
1374
Anna Perel557bf722012-09-20 11:16:35 +03001375#define MBIM_ACTIVE_PORT 0
1376
1377static void mbim_suspend(struct usb_function *f)
1378{
1379 pr_info("mbim suspended\n");
1380 bam_data_suspend(MBIM_ACTIVE_PORT);
1381}
1382
1383static void mbim_resume(struct usb_function *f)
1384{
1385 pr_info("mbim resumed\n");
1386 bam_data_resume(MBIM_ACTIVE_PORT);
1387}
1388
Anna Perela8c991d2012-04-09 16:44:46 +03001389/*---------------------- function driver setup/binding ---------------------*/
1390
1391static int
1392mbim_bind(struct usb_configuration *c, struct usb_function *f)
1393{
1394 struct usb_composite_dev *cdev = c->cdev;
1395 struct f_mbim *mbim = func_to_mbim(f);
1396 int status;
1397 struct usb_ep *ep;
1398
1399 pr_info("Enter");
1400
1401 mbim->cdev = cdev;
1402
1403 /* allocate instance-specific interface IDs */
1404 status = usb_interface_id(c, f);
1405 if (status < 0)
1406 goto fail;
1407 mbim->ctrl_id = status;
1408 mbim_iad_desc.bFirstInterface = status;
1409
1410 mbim_control_intf.bInterfaceNumber = status;
1411 mbim_union_desc.bMasterInterface0 = status;
1412
1413 status = usb_interface_id(c, f);
1414 if (status < 0)
1415 goto fail;
1416 mbim->data_id = status;
1417
1418 mbim_data_nop_intf.bInterfaceNumber = status;
1419 mbim_data_intf.bInterfaceNumber = status;
1420 mbim_union_desc.bSlaveInterface0 = status;
1421
Anna Perel557bf722012-09-20 11:16:35 +03001422 mbim->bam_port.cdev = cdev;
1423
Anna Perela8c991d2012-04-09 16:44:46 +03001424 status = -ENODEV;
1425
1426 /* allocate instance-specific endpoints */
1427 ep = usb_ep_autoconfig(cdev->gadget, &fs_mbim_in_desc);
1428 if (!ep) {
1429 pr_err("usb epin autoconfig failed\n");
1430 goto fail;
1431 }
1432 pr_info("usb epin autoconfig succeeded\n");
1433 ep->driver_data = cdev; /* claim */
1434 mbim->bam_port.in = ep;
1435
1436 ep = usb_ep_autoconfig(cdev->gadget, &fs_mbim_out_desc);
1437 if (!ep) {
1438 pr_err("usb epout autoconfig failed\n");
1439 goto fail;
1440 }
1441 pr_info("usb epout autoconfig succeeded\n");
1442 ep->driver_data = cdev; /* claim */
1443 mbim->bam_port.out = ep;
1444
1445 ep = usb_ep_autoconfig(cdev->gadget, &fs_mbim_notify_desc);
1446 if (!ep) {
1447 pr_err("usb notify ep autoconfig failed\n");
1448 goto fail;
1449 }
1450 pr_info("usb notify ep autoconfig succeeded\n");
1451 mbim->not_port.notify = ep;
1452 ep->driver_data = cdev; /* claim */
1453
1454 status = -ENOMEM;
1455
1456 /* allocate notification request and buffer */
1457 mbim->not_port.notify_req = mbim_alloc_req(ep, NCM_STATUS_BYTECOUNT);
1458 if (!mbim->not_port.notify_req) {
1459 pr_info("failed to allocate notify request\n");
1460 goto fail;
1461 }
1462 pr_info("allocated notify ep request & request buffer\n");
1463
1464 mbim->not_port.notify_req->context = mbim;
1465 mbim->not_port.notify_req->complete = mbim_notify_complete;
1466
1467 /* copy descriptors, and track endpoint copies */
1468 f->descriptors = usb_copy_descriptors(mbim_fs_function);
1469 if (!f->descriptors)
1470 goto fail;
1471
1472 /*
1473 * support all relevant hardware speeds... we expect that when
1474 * hardware is dual speed, all bulk-capable endpoints work at
1475 * both speeds
1476 */
1477 if (gadget_is_dualspeed(c->cdev->gadget)) {
1478 hs_mbim_in_desc.bEndpointAddress =
1479 fs_mbim_in_desc.bEndpointAddress;
1480 hs_mbim_out_desc.bEndpointAddress =
1481 fs_mbim_out_desc.bEndpointAddress;
1482 hs_mbim_notify_desc.bEndpointAddress =
1483 fs_mbim_notify_desc.bEndpointAddress;
1484
1485 /* copy descriptors, and track endpoint copies */
1486 f->hs_descriptors = usb_copy_descriptors(mbim_hs_function);
1487 if (!f->hs_descriptors)
1488 goto fail;
1489 }
1490
Jack Pham2df2f702012-10-11 19:08:24 -07001491 /*
1492 * If MBIM is bound in a config other than the first, tell Windows
1493 * about it by returning the num as a string in the OS descriptor's
1494 * subCompatibleID field. Windows only supports up to config #4.
1495 */
1496 if (c->bConfigurationValue >= 2 && c->bConfigurationValue <= 4) {
1497 pr_debug("MBIM in configuration %d", c->bConfigurationValue);
1498 mbim_ext_config_desc.function.subCompatibleID[0] =
1499 c->bConfigurationValue + '0';
1500 }
1501
Anna Perela8c991d2012-04-09 16:44:46 +03001502 pr_info("mbim(%d): %s speed IN/%s OUT/%s NOTIFY/%s\n",
1503 mbim->port_num,
1504 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
1505 mbim->bam_port.in->name, mbim->bam_port.out->name,
1506 mbim->not_port.notify->name);
1507
1508 return 0;
1509
1510fail:
1511 pr_err("%s failed to bind, err %d\n", f->name, status);
1512
1513 if (f->descriptors)
1514 usb_free_descriptors(f->descriptors);
1515
1516 if (mbim->not_port.notify_req) {
1517 kfree(mbim->not_port.notify_req->buf);
1518 usb_ep_free_request(mbim->not_port.notify,
1519 mbim->not_port.notify_req);
1520 }
1521
1522 /* we might as well release our claims on endpoints */
1523 if (mbim->not_port.notify)
1524 mbim->not_port.notify->driver_data = NULL;
1525 if (mbim->bam_port.out)
1526 mbim->bam_port.out->driver_data = NULL;
1527 if (mbim->bam_port.in)
1528 mbim->bam_port.in->driver_data = NULL;
1529
1530 return status;
1531}
1532
1533static void mbim_unbind(struct usb_configuration *c, struct usb_function *f)
1534{
1535 struct f_mbim *mbim = func_to_mbim(f);
1536
1537 if (gadget_is_dualspeed(c->cdev->gadget))
1538 usb_free_descriptors(f->hs_descriptors);
1539 usb_free_descriptors(f->descriptors);
1540
1541 kfree(mbim->not_port.notify_req->buf);
1542 usb_ep_free_request(mbim->not_port.notify, mbim->not_port.notify_req);
Jack Pham2df2f702012-10-11 19:08:24 -07001543
1544 mbim_ext_config_desc.function.subCompatibleID[0] = 0;
Anna Perela8c991d2012-04-09 16:44:46 +03001545}
1546
1547/**
1548 * mbim_bind_config - add MBIM link to a configuration
1549 * @c: the configuration to support the network link
1550 * Context: single threaded during gadget setup
1551 * Returns zero on success, else negative errno.
1552 */
1553int mbim_bind_config(struct usb_configuration *c, unsigned portno)
1554{
1555 struct f_mbim *mbim = NULL;
1556 int status = 0;
1557
1558 pr_info("port number %u", portno);
1559
1560 if (portno >= nr_mbim_ports) {
1561 pr_err("Can not add port %u. Max ports = %d",
1562 portno, nr_mbim_ports);
1563 return -ENODEV;
1564 }
1565
1566 status = mbim_bam_setup(nr_mbim_ports);
1567 if (status) {
1568 pr_err("bam setup failed");
1569 return status;
1570 }
1571
1572 /* maybe allocate device-global string IDs */
1573 if (mbim_string_defs[0].id == 0) {
1574
1575 /* control interface label */
1576 status = usb_string_id(c->cdev);
1577 if (status < 0)
1578 return status;
1579 mbim_string_defs[STRING_CTRL_IDX].id = status;
1580 mbim_control_intf.iInterface = status;
1581
1582 /* data interface label */
1583 status = usb_string_id(c->cdev);
1584 if (status < 0)
1585 return status;
1586 mbim_string_defs[STRING_DATA_IDX].id = status;
1587 mbim_data_nop_intf.iInterface = status;
1588 mbim_data_intf.iInterface = status;
1589 }
1590
1591 /* allocate and initialize one new instance */
1592 mbim = mbim_ports[0].port;
1593 if (!mbim) {
1594 pr_info("mbim struct not allocated");
1595 return -ENOMEM;
1596 }
1597
1598 mbim->cdev = c->cdev;
1599
Anna Perela8c991d2012-04-09 16:44:46 +03001600 mbim_reset_values(mbim);
1601
1602 mbim->function.name = "usb_mbim";
1603 mbim->function.strings = mbim_strings;
1604 mbim->function.bind = mbim_bind;
1605 mbim->function.unbind = mbim_unbind;
1606 mbim->function.set_alt = mbim_set_alt;
1607 mbim->function.get_alt = mbim_get_alt;
1608 mbim->function.setup = mbim_setup;
1609 mbim->function.disable = mbim_disable;
Anna Perel557bf722012-09-20 11:16:35 +03001610 mbim->function.suspend = mbim_suspend;
1611 mbim->function.resume = mbim_resume;
Anna Perela8c991d2012-04-09 16:44:46 +03001612
1613 INIT_LIST_HEAD(&mbim->cpkt_req_q);
1614 INIT_LIST_HEAD(&mbim->cpkt_resp_q);
1615
1616 status = usb_add_function(c, &mbim->function);
1617
1618 pr_info("Exit status %d", status);
1619
1620 return status;
1621}
1622
1623/* ------------ MBIM DRIVER File Operations API for USER SPACE ------------ */
1624
1625static ssize_t
1626mbim_read(struct file *fp, char __user *buf, size_t count, loff_t *pos)
1627{
1628 struct f_mbim *dev = fp->private_data;
1629 struct ctrl_pkt *cpkt = NULL;
1630 int ret = 0;
1631
1632 pr_debug("Enter(%d)\n", count);
1633
1634 if (!dev) {
1635 pr_err("Received NULL mbim pointer\n");
1636 return -ENODEV;
1637 }
1638
1639 if (count > MBIM_BULK_BUFFER_SIZE) {
1640 pr_err("Buffer size is too big %d, should be at most %d\n",
1641 count, MBIM_BULK_BUFFER_SIZE);
1642 return -EINVAL;
1643 }
1644
1645 if (mbim_lock(&dev->read_excl)) {
1646 pr_err("Previous reading is not finished yet\n");
1647 return -EBUSY;
1648 }
1649
1650 /* block until mbim online */
1651 while (!(atomic_read(&dev->online) || atomic_read(&dev->error))) {
1652 pr_err("USB cable not connected. Wait.\n");
1653 ret = wait_event_interruptible(dev->read_wq,
1654 (atomic_read(&dev->online) ||
1655 atomic_read(&dev->error)));
1656 if (ret < 0) {
1657 mbim_unlock(&dev->read_excl);
1658 return 0;
1659 }
1660 }
1661
1662 if (atomic_read(&dev->error)) {
1663 mbim_unlock(&dev->read_excl);
1664 return -EIO;
1665 }
1666
1667 while (list_empty(&dev->cpkt_req_q)) {
1668 pr_err("Requests list is empty. Wait.\n");
1669 ret = wait_event_interruptible(dev->read_wq,
1670 !list_empty(&dev->cpkt_req_q));
1671 if (ret < 0) {
1672 pr_err("Waiting failed\n");
1673 mbim_unlock(&dev->read_excl);
1674 return 0;
1675 }
1676 pr_debug("Received request packet\n");
1677 }
1678
1679 cpkt = list_first_entry(&dev->cpkt_req_q, struct ctrl_pkt,
1680 list);
1681 if (cpkt->len > count) {
1682 mbim_unlock(&dev->read_excl);
1683 pr_err("cpkt size too big:%d > buf size:%d\n",
1684 cpkt->len, count);
1685 return -ENOMEM;
1686 }
1687
1688 pr_debug("cpkt size:%d\n", cpkt->len);
1689
1690 list_del(&cpkt->list);
1691 mbim_unlock(&dev->read_excl);
1692
1693 ret = copy_to_user(buf, cpkt->buf, cpkt->len);
1694 if (ret) {
1695 pr_err("copy_to_user failed: err %d\n", ret);
1696 ret = 0;
1697 } else {
1698 pr_debug("copied %d bytes to user\n", cpkt->len);
1699 ret = cpkt->len;
1700 }
1701
1702 mbim_free_ctrl_pkt(cpkt);
1703
1704 return ret;
1705}
1706
1707static ssize_t
1708mbim_write(struct file *fp, const char __user *buf, size_t count, loff_t *pos)
1709{
1710 struct f_mbim *dev = fp->private_data;
1711 struct ctrl_pkt *cpkt = NULL;
1712 int ret = 0;
1713
1714 pr_debug("Enter(%d)", count);
1715
1716 if (!dev) {
1717 pr_err("Received NULL mbim pointer\n");
1718 return -ENODEV;
1719 }
1720
1721 if (!count) {
1722 pr_err("zero length ctrl pkt\n");
1723 return -ENODEV;
1724 }
1725
1726 if (count > MAX_CTRL_PKT_SIZE) {
1727 pr_err("given pkt size too big:%d > max_pkt_size:%d\n",
1728 count, MAX_CTRL_PKT_SIZE);
1729 return -ENOMEM;
1730 }
1731
1732 if (mbim_lock(&dev->write_excl)) {
1733 pr_err("Previous writing not finished yet\n");
1734 return -EBUSY;
1735 }
1736
1737 if (!atomic_read(&dev->online)) {
1738 pr_err("USB cable not connected\n");
1739 mbim_unlock(&dev->write_excl);
1740 return -EPIPE;
1741 }
1742
1743 cpkt = mbim_alloc_ctrl_pkt(count, GFP_KERNEL);
1744 if (!cpkt) {
1745 pr_err("failed to allocate ctrl pkt\n");
1746 mbim_unlock(&dev->write_excl);
1747 return -ENOMEM;
1748 }
1749
1750 ret = copy_from_user(cpkt->buf, buf, count);
1751 if (ret) {
1752 pr_err("copy_from_user failed err:%d\n", ret);
1753 mbim_free_ctrl_pkt(cpkt);
1754 mbim_unlock(&dev->write_excl);
1755 return 0;
1756 }
1757
1758 fmbim_send_cpkt_response(dev, cpkt);
1759
1760 mbim_unlock(&dev->write_excl);
1761
1762 pr_debug("Exit(%d)", count);
1763
1764 return count;
Anna Perel89ad1212012-06-13 17:17:24 +03001765
Anna Perela8c991d2012-04-09 16:44:46 +03001766}
1767
1768static int mbim_open(struct inode *ip, struct file *fp)
1769{
1770 pr_info("Open mbim driver\n");
1771
1772 while (!_mbim_dev) {
1773 pr_err("mbim_dev not created yet\n");
1774 return -ENODEV;
1775 }
1776
1777 if (mbim_lock(&_mbim_dev->open_excl)) {
1778 pr_err("Already opened\n");
1779 return -EBUSY;
1780 }
1781
1782 pr_info("Lock mbim_dev->open_excl for open\n");
1783
1784 if (!atomic_read(&_mbim_dev->online))
1785 pr_err("USB cable not connected\n");
1786
Anna Perela8c991d2012-04-09 16:44:46 +03001787 fp->private_data = _mbim_dev;
1788
1789 atomic_set(&_mbim_dev->error, 0);
1790
1791 spin_lock(&_mbim_dev->lock);
1792 _mbim_dev->is_open = true;
1793 mbim_notify(_mbim_dev);
1794 spin_unlock(&_mbim_dev->lock);
1795
1796 pr_info("Exit, mbim file opened\n");
1797
1798 return 0;
1799}
1800
1801static int mbim_release(struct inode *ip, struct file *fp)
1802{
1803 struct f_mbim *mbim = fp->private_data;
1804
1805 pr_info("Close mbim file");
1806
1807 spin_lock(&mbim->lock);
1808 mbim->is_open = false;
1809 mbim_notify(mbim);
1810 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