blob: 5a3d753659ba8d8e092dfe0e3f19dc752dfac713 [file] [log] [blame]
Ido Shayevitzbadc8ea2013-02-06 14:14:54 +02001/* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
Anna Perela8c991d2012-04-09 16:44:46 +03002 *
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,
Ido Shayevitzbadc8ea2013-02-06 14:14:54 +020069 NCM_NOTIFY_RESPONSE_AVAILABLE,
Anna Perela8c991d2012-04-09 16:44:46 +030070};
71
72struct f_mbim {
Anna Perel557bf722012-09-20 11:16:35 +030073 struct usb_function function;
74 struct usb_composite_dev *cdev;
Anna Perela8c991d2012-04-09 16:44:46 +030075
76 atomic_t online;
77 bool is_open;
78
79 atomic_t open_excl;
80 atomic_t ioctl_excl;
81 atomic_t read_excl;
82 atomic_t write_excl;
83
84 wait_queue_head_t read_wq;
85 wait_queue_head_t write_wq;
86
Lena Salmandf7e7992013-03-15 09:46:27 +020087 enum transport_type xport;
Anna Perela8c991d2012-04-09 16:44:46 +030088 u8 port_num;
89 struct data_port bam_port;
90 struct mbim_notify_port not_port;
91
92 struct mbim_ep_descs fs;
93 struct mbim_ep_descs hs;
94
95 u8 ctrl_id, data_id;
Bar Weinerb1c95f52012-12-23 09:09:13 +020096 u8 data_alt_int;
Anna Perela8c991d2012-04-09 16:44:46 +030097
98 struct ndp_parser_opts *parser_opts;
99
100 spinlock_t lock;
101
102 struct list_head cpkt_req_q;
103 struct list_head cpkt_resp_q;
104
105 u32 ntb_input_size;
106 u16 ntb_max_datagrams;
107
Anna Perela8c991d2012-04-09 16:44:46 +0300108 atomic_t error;
109};
110
111struct mbim_ntb_input_size {
112 u32 ntb_input_size;
113 u16 ntb_max_datagrams;
114 u16 reserved;
115};
116
117/* temporary variable used between mbim_open() and mbim_gadget_bind() */
118static struct f_mbim *_mbim_dev;
119
120static unsigned int nr_mbim_ports;
121
122static struct mbim_ports {
123 struct f_mbim *port;
124 unsigned port_num;
125} mbim_ports[NR_MBIM_PORTS];
126
127static inline struct f_mbim *func_to_mbim(struct usb_function *f)
128{
129 return container_of(f, struct f_mbim, function);
130}
131
132/* peak (theoretical) bulk transfer rate in bits-per-second */
133static inline unsigned mbim_bitrate(struct usb_gadget *g)
134{
135 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
136 return 13 * 512 * 8 * 1000 * 8;
137 else
138 return 19 * 64 * 1 * 1000 * 8;
139}
140
141/*-------------------------------------------------------------------------*/
142
143#define NTB_DEFAULT_IN_SIZE (0x4000)
144#define NTB_OUT_SIZE (0x1000)
145#define NDP_IN_DIVISOR (0x4)
146
Lena Salmandf7e7992013-03-15 09:46:27 +0200147#define NTB_DEFAULT_IN_SIZE_IPA (0x2000)
148#define NTB_OUT_SIZE_IPA (0x2000)
149
Anna Perela8c991d2012-04-09 16:44:46 +0300150#define FORMATS_SUPPORTED USB_CDC_NCM_NTB16_SUPPORTED
151
152static struct usb_cdc_ncm_ntb_parameters ntb_parameters = {
153 .wLength = sizeof ntb_parameters,
154 .bmNtbFormatsSupported = cpu_to_le16(FORMATS_SUPPORTED),
155 .dwNtbInMaxSize = cpu_to_le32(NTB_DEFAULT_IN_SIZE),
156 .wNdpInDivisor = cpu_to_le16(NDP_IN_DIVISOR),
157 .wNdpInPayloadRemainder = cpu_to_le16(0),
158 .wNdpInAlignment = cpu_to_le16(4),
159
160 .dwNtbOutMaxSize = cpu_to_le32(NTB_OUT_SIZE),
161 .wNdpOutDivisor = cpu_to_le16(4),
162 .wNdpOutPayloadRemainder = cpu_to_le16(0),
163 .wNdpOutAlignment = cpu_to_le16(4),
Anna Perelf99cd0c2012-05-03 13:30:26 +0300164 .wNtbOutMaxDatagrams = 0,
Anna Perela8c991d2012-04-09 16:44:46 +0300165};
166
167/*
168 * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
169 * packet, to simplify cancellation; and a big transfer interval, to
170 * waste less bandwidth.
171 */
172
173#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
174#define NCM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
175
176static struct usb_interface_assoc_descriptor mbim_iad_desc = {
177 .bLength = sizeof mbim_iad_desc,
178 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
179
180 /* .bFirstInterface = DYNAMIC, */
181 .bInterfaceCount = 2, /* control + data */
182 .bFunctionClass = 2,
183 .bFunctionSubClass = 0x0e,
184 .bFunctionProtocol = 0,
185 /* .iFunction = DYNAMIC */
186};
187
188/* interface descriptor: */
189static struct usb_interface_descriptor mbim_control_intf = {
190 .bLength = sizeof mbim_control_intf,
191 .bDescriptorType = USB_DT_INTERFACE,
192
193 /* .bInterfaceNumber = DYNAMIC */
194 .bNumEndpoints = 1,
195 .bInterfaceClass = 0x02,
196 .bInterfaceSubClass = 0x0e,
197 .bInterfaceProtocol = 0,
198 /* .iInterface = DYNAMIC */
199};
200
201static struct usb_cdc_header_desc mbim_header_desc = {
202 .bLength = sizeof mbim_header_desc,
203 .bDescriptorType = USB_DT_CS_INTERFACE,
204 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
205
206 .bcdCDC = cpu_to_le16(0x0110),
207};
208
209static struct usb_cdc_union_desc mbim_union_desc = {
210 .bLength = sizeof(mbim_union_desc),
211 .bDescriptorType = USB_DT_CS_INTERFACE,
212 .bDescriptorSubType = USB_CDC_UNION_TYPE,
213 /* .bMasterInterface0 = DYNAMIC */
214 /* .bSlaveInterface0 = DYNAMIC */
215};
216
217static struct usb_cdc_mbb_desc mbb_desc = {
218 .bLength = sizeof mbb_desc,
219 .bDescriptorType = USB_DT_CS_INTERFACE,
220 .bDescriptorSubType = USB_CDC_MBB_TYPE,
221
222 .bcdMbbVersion = cpu_to_le16(0x0100),
223
224 .wMaxControlMessage = cpu_to_le16(0x1000),
Anna Perel3354bdc2012-12-09 12:08:10 +0200225 .bNumberFilters = 0x20,
Anna Perela8c991d2012-04-09 16:44:46 +0300226 .bMaxFilterSize = 0x80,
Anna Perel26ae27c2012-05-23 18:07:31 +0300227 .wMaxSegmentSize = cpu_to_le16(0xfe0),
Anna Perela8c991d2012-04-09 16:44:46 +0300228 .bmNetworkCapabilities = 0x20,
229};
230
Bar Weiner38c3e642013-02-12 10:25:17 +0200231static struct usb_cdc_ext_mbb_desc ext_mbb_desc = {
232 .bLength = sizeof ext_mbb_desc,
233 .bDescriptorType = USB_DT_CS_INTERFACE,
234 .bDescriptorSubType = USB_CDC_EXT_MBB_TYPE,
235
236 .bcdMbbExtendedVersion = cpu_to_le16(0x0100),
237 .bMaxOutstandingCmdMsges = 64,
238 .wMTU = 1500,
239};
240
Anna Perela8c991d2012-04-09 16:44:46 +0300241/* the default data interface has no endpoints ... */
242static struct usb_interface_descriptor mbim_data_nop_intf = {
243 .bLength = sizeof mbim_data_nop_intf,
244 .bDescriptorType = USB_DT_INTERFACE,
245
246 /* .bInterfaceNumber = DYNAMIC */
247 .bAlternateSetting = 0,
248 .bNumEndpoints = 0,
249 .bInterfaceClass = 0x0a,
250 .bInterfaceSubClass = 0,
251 .bInterfaceProtocol = 0x02,
252 /* .iInterface = DYNAMIC */
253};
254
255/* ... but the "real" data interface has two bulk endpoints */
256static struct usb_interface_descriptor mbim_data_intf = {
257 .bLength = sizeof mbim_data_intf,
258 .bDescriptorType = USB_DT_INTERFACE,
259
260 /* .bInterfaceNumber = DYNAMIC */
261 .bAlternateSetting = 1,
262 .bNumEndpoints = 2,
263 .bInterfaceClass = 0x0a,
264 .bInterfaceSubClass = 0,
265 .bInterfaceProtocol = 0x02,
266 /* .iInterface = DYNAMIC */
267};
268
269/* full speed support: */
270
271static struct usb_endpoint_descriptor fs_mbim_notify_desc = {
272 .bLength = USB_DT_ENDPOINT_SIZE,
273 .bDescriptorType = USB_DT_ENDPOINT,
274
275 .bEndpointAddress = USB_DIR_IN,
276 .bmAttributes = USB_ENDPOINT_XFER_INT,
277 .wMaxPacketSize = 4*cpu_to_le16(NCM_STATUS_BYTECOUNT),
278 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
279};
280
281static struct usb_endpoint_descriptor fs_mbim_in_desc = {
282 .bLength = USB_DT_ENDPOINT_SIZE,
283 .bDescriptorType = USB_DT_ENDPOINT,
284
285 .bEndpointAddress = USB_DIR_IN,
286 .bmAttributes = USB_ENDPOINT_XFER_BULK,
287};
288
289static struct usb_endpoint_descriptor fs_mbim_out_desc = {
290 .bLength = USB_DT_ENDPOINT_SIZE,
291 .bDescriptorType = USB_DT_ENDPOINT,
292
293 .bEndpointAddress = USB_DIR_OUT,
294 .bmAttributes = USB_ENDPOINT_XFER_BULK,
295};
296
297static struct usb_descriptor_header *mbim_fs_function[] = {
298 (struct usb_descriptor_header *) &mbim_iad_desc,
299 /* MBIM control descriptors */
300 (struct usb_descriptor_header *) &mbim_control_intf,
301 (struct usb_descriptor_header *) &mbim_header_desc,
Anna Perel555a7b22013-02-26 13:14:32 +0200302 (struct usb_descriptor_header *) &mbim_union_desc,
Anna Perela8c991d2012-04-09 16:44:46 +0300303 (struct usb_descriptor_header *) &mbb_desc,
Bar Weiner38c3e642013-02-12 10:25:17 +0200304 (struct usb_descriptor_header *) &ext_mbb_desc,
Anna Perela8c991d2012-04-09 16:44:46 +0300305 (struct usb_descriptor_header *) &fs_mbim_notify_desc,
306 /* data interface, altsettings 0 and 1 */
307 (struct usb_descriptor_header *) &mbim_data_nop_intf,
308 (struct usb_descriptor_header *) &mbim_data_intf,
309 (struct usb_descriptor_header *) &fs_mbim_in_desc,
310 (struct usb_descriptor_header *) &fs_mbim_out_desc,
311 NULL,
312};
313
314/* high speed support: */
315
316static struct usb_endpoint_descriptor hs_mbim_notify_desc = {
317 .bLength = USB_DT_ENDPOINT_SIZE,
318 .bDescriptorType = USB_DT_ENDPOINT,
319
320 .bEndpointAddress = USB_DIR_IN,
321 .bmAttributes = USB_ENDPOINT_XFER_INT,
322 .wMaxPacketSize = 4*cpu_to_le16(NCM_STATUS_BYTECOUNT),
323 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
324};
325static struct usb_endpoint_descriptor hs_mbim_in_desc = {
326 .bLength = USB_DT_ENDPOINT_SIZE,
327 .bDescriptorType = USB_DT_ENDPOINT,
328
329 .bEndpointAddress = USB_DIR_IN,
330 .bmAttributes = USB_ENDPOINT_XFER_BULK,
331 .wMaxPacketSize = cpu_to_le16(512),
332};
333
334static struct usb_endpoint_descriptor hs_mbim_out_desc = {
335 .bLength = USB_DT_ENDPOINT_SIZE,
336 .bDescriptorType = USB_DT_ENDPOINT,
337
338 .bEndpointAddress = USB_DIR_OUT,
339 .bmAttributes = USB_ENDPOINT_XFER_BULK,
340 .wMaxPacketSize = cpu_to_le16(512),
341};
342
343static struct usb_descriptor_header *mbim_hs_function[] = {
344 (struct usb_descriptor_header *) &mbim_iad_desc,
345 /* MBIM control descriptors */
346 (struct usb_descriptor_header *) &mbim_control_intf,
347 (struct usb_descriptor_header *) &mbim_header_desc,
Amit Blayfa105432013-03-27 15:46:08 +0200348 (struct usb_descriptor_header *) &mbim_union_desc,
Anna Perela8c991d2012-04-09 16:44:46 +0300349 (struct usb_descriptor_header *) &mbb_desc,
Bar Weiner38c3e642013-02-12 10:25:17 +0200350 (struct usb_descriptor_header *) &ext_mbb_desc,
Anna Perela8c991d2012-04-09 16:44:46 +0300351 (struct usb_descriptor_header *) &hs_mbim_notify_desc,
352 /* data interface, altsettings 0 and 1 */
353 (struct usb_descriptor_header *) &mbim_data_nop_intf,
354 (struct usb_descriptor_header *) &mbim_data_intf,
355 (struct usb_descriptor_header *) &hs_mbim_in_desc,
356 (struct usb_descriptor_header *) &hs_mbim_out_desc,
357 NULL,
358};
359
360/* string descriptors: */
361
362#define STRING_CTRL_IDX 0
363#define STRING_DATA_IDX 1
364
365static struct usb_string mbim_string_defs[] = {
366 [STRING_CTRL_IDX].s = "MBIM Control",
367 [STRING_DATA_IDX].s = "MBIM Data",
368 { } /* end of list */
369};
370
371static struct usb_gadget_strings mbim_string_table = {
372 .language = 0x0409, /* en-us */
373 .strings = mbim_string_defs,
374};
375
376static struct usb_gadget_strings *mbim_strings[] = {
377 &mbim_string_table,
378 NULL,
379};
380
Jack Pham2df2f702012-10-11 19:08:24 -0700381/* Microsoft OS Descriptors */
382
383/*
384 * We specify our own bMS_VendorCode byte which Windows will use
385 * as the bRequest value in subsequent device get requests.
386 */
387#define MBIM_VENDOR_CODE 0xA5
388
389/* Microsoft OS String */
390static u8 mbim_os_string[] = {
391 18, /* sizeof(mtp_os_string) */
392 USB_DT_STRING,
393 /* Signature field: "MSFT100" */
394 'M', 0, 'S', 0, 'F', 0, 'T', 0, '1', 0, '0', 0, '0', 0,
395 /* vendor code */
396 MBIM_VENDOR_CODE,
397 /* padding */
398 0
399};
400
401/* Microsoft Extended Configuration Descriptor Header Section */
402struct mbim_ext_config_desc_header {
403 __le32 dwLength;
404 __u16 bcdVersion;
405 __le16 wIndex;
406 __u8 bCount;
407 __u8 reserved[7];
408};
409
410/* Microsoft Extended Configuration Descriptor Function Section */
411struct mbim_ext_config_desc_function {
412 __u8 bFirstInterfaceNumber;
413 __u8 bInterfaceCount;
414 __u8 compatibleID[8];
415 __u8 subCompatibleID[8];
416 __u8 reserved[6];
417};
418
419/* Microsoft Extended Configuration Descriptor */
420static struct {
421 struct mbim_ext_config_desc_header header;
422 struct mbim_ext_config_desc_function function;
423} mbim_ext_config_desc = {
424 .header = {
425 .dwLength = __constant_cpu_to_le32(sizeof mbim_ext_config_desc),
426 .bcdVersion = __constant_cpu_to_le16(0x0100),
427 .wIndex = __constant_cpu_to_le16(4),
428 .bCount = 1,
429 },
430 .function = {
431 .bFirstInterfaceNumber = 0,
432 .bInterfaceCount = 1,
433 .compatibleID = { 'A', 'L', 'T', 'R', 'C', 'F', 'G' },
434 /* .subCompatibleID = DYNAMIC */
435 },
436};
437
Anna Perela8c991d2012-04-09 16:44:46 +0300438/*
439 * Here are options for the Datagram Pointer table (NDP) parser.
440 * There are 2 different formats: NDP16 and NDP32 in the spec (ch. 3),
441 * in NDP16 offsets and sizes fields are 1 16bit word wide,
442 * in NDP32 -- 2 16bit words wide. Also signatures are different.
443 * To make the parser code the same, put the differences in the structure,
444 * and switch pointers to the structures when the format is changed.
445 */
446
447struct ndp_parser_opts {
448 u32 nth_sign;
449 u32 ndp_sign;
450 unsigned nth_size;
451 unsigned ndp_size;
452 unsigned ndplen_align;
453 /* sizes in u16 units */
454 unsigned dgram_item_len; /* index or length */
455 unsigned block_length;
456 unsigned fp_index;
457 unsigned reserved1;
458 unsigned reserved2;
459 unsigned next_fp_index;
460};
461
462#define INIT_NDP16_OPTS { \
463 .nth_sign = USB_CDC_NCM_NTH16_SIGN, \
464 .ndp_sign = USB_CDC_NCM_NDP16_NOCRC_SIGN, \
465 .nth_size = sizeof(struct usb_cdc_ncm_nth16), \
466 .ndp_size = sizeof(struct usb_cdc_ncm_ndp16), \
467 .ndplen_align = 4, \
468 .dgram_item_len = 1, \
469 .block_length = 1, \
470 .fp_index = 1, \
471 .reserved1 = 0, \
472 .reserved2 = 0, \
473 .next_fp_index = 1, \
474}
475
476#define INIT_NDP32_OPTS { \
477 .nth_sign = USB_CDC_NCM_NTH32_SIGN, \
478 .ndp_sign = USB_CDC_NCM_NDP32_NOCRC_SIGN, \
479 .nth_size = sizeof(struct usb_cdc_ncm_nth32), \
480 .ndp_size = sizeof(struct usb_cdc_ncm_ndp32), \
481 .ndplen_align = 8, \
482 .dgram_item_len = 2, \
483 .block_length = 2, \
484 .fp_index = 2, \
485 .reserved1 = 1, \
486 .reserved2 = 2, \
487 .next_fp_index = 2, \
488}
489
490static struct ndp_parser_opts ndp16_opts = INIT_NDP16_OPTS;
491static struct ndp_parser_opts ndp32_opts = INIT_NDP32_OPTS;
492
493static inline int mbim_lock(atomic_t *excl)
494{
495 if (atomic_inc_return(excl) == 1) {
496 return 0;
497 } else {
498 atomic_dec(excl);
499 return -EBUSY;
500 }
501}
502
503static inline void mbim_unlock(atomic_t *excl)
504{
505 atomic_dec(excl);
506}
507
508static struct ctrl_pkt *mbim_alloc_ctrl_pkt(unsigned len, gfp_t flags)
509{
510 struct ctrl_pkt *pkt;
511
512 pkt = kzalloc(sizeof(struct ctrl_pkt), flags);
513 if (!pkt)
514 return ERR_PTR(-ENOMEM);
515
516 pkt->buf = kmalloc(len, flags);
517 if (!pkt->buf) {
518 kfree(pkt);
519 return ERR_PTR(-ENOMEM);
520 }
521 pkt->len = len;
522
523 return pkt;
524}
525
526static void mbim_free_ctrl_pkt(struct ctrl_pkt *pkt)
527{
528 if (pkt) {
529 kfree(pkt->buf);
530 kfree(pkt);
531 }
532}
533
534static struct usb_request *mbim_alloc_req(struct usb_ep *ep, int buffer_size)
535{
536 struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
537 if (!req)
538 return NULL;
539
540 req->buf = kmalloc(buffer_size, GFP_KERNEL);
541 if (!req->buf) {
542 usb_ep_free_request(ep, req);
543 return NULL;
544 }
545 req->length = buffer_size;
546 return req;
547}
548
549void fmbim_free_req(struct usb_ep *ep, struct usb_request *req)
550{
551 if (req) {
552 kfree(req->buf);
553 usb_ep_free_request(ep, req);
554 }
555}
556
557static void fmbim_ctrl_response_available(struct f_mbim *dev)
558{
559 struct usb_request *req = dev->not_port.notify_req;
560 struct usb_cdc_notification *event = NULL;
561 unsigned long flags;
562 int ret;
563
Anna Perel68aeb172012-10-28 09:00:45 +0200564 pr_debug("dev:%p portno#%d\n", dev, dev->port_num);
Anna Perela8c991d2012-04-09 16:44:46 +0300565
566 spin_lock_irqsave(&dev->lock, flags);
567
568 if (!atomic_read(&dev->online)) {
Anna Perel20c91152012-10-30 16:26:44 +0200569 pr_err("dev:%p is not online\n", dev);
Anna Perela8c991d2012-04-09 16:44:46 +0300570 spin_unlock_irqrestore(&dev->lock, flags);
571 return;
572 }
573
574 if (!req) {
Anna Perel20c91152012-10-30 16:26:44 +0200575 pr_err("dev:%p req is NULL\n", dev);
Anna Perela8c991d2012-04-09 16:44:46 +0300576 spin_unlock_irqrestore(&dev->lock, flags);
577 return;
578 }
579
580 if (!req->buf) {
Anna Perel20c91152012-10-30 16:26:44 +0200581 pr_err("dev:%p req->buf is NULL\n", dev);
Anna Perela8c991d2012-04-09 16:44:46 +0300582 spin_unlock_irqrestore(&dev->lock, flags);
583 return;
584 }
585
Anna Perel68aeb172012-10-28 09:00:45 +0200586 if (atomic_inc_return(&dev->not_port.notify_count) != 1) {
587 pr_debug("delay ep_queue: notifications queue is busy[%d]",
588 atomic_read(&dev->not_port.notify_count));
589 spin_unlock_irqrestore(&dev->lock, flags);
590 return;
591 }
Anna Perela8c991d2012-04-09 16:44:46 +0300592
Ido Shayevitzbadc8ea2013-02-06 14:14:54 +0200593 req->length = sizeof *event;
Anna Perela8c991d2012-04-09 16:44:46 +0300594 event = req->buf;
595 event->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS
596 | USB_RECIP_INTERFACE;
597 event->bNotificationType = USB_CDC_NOTIFY_RESPONSE_AVAILABLE;
598 event->wValue = cpu_to_le16(0);
599 event->wIndex = cpu_to_le16(dev->ctrl_id);
600 event->wLength = cpu_to_le16(0);
601 spin_unlock_irqrestore(&dev->lock, flags);
602
Anna Perela8c991d2012-04-09 16:44:46 +0300603 ret = usb_ep_queue(dev->not_port.notify,
Ido Shayevitzbadc8ea2013-02-06 14:14:54 +0200604 req, GFP_ATOMIC);
Anna Perela8c991d2012-04-09 16:44:46 +0300605 if (ret) {
606 atomic_dec(&dev->not_port.notify_count);
607 pr_err("ep enqueue error %d\n", ret);
608 }
609
Anna Perel68aeb172012-10-28 09:00:45 +0200610 pr_debug("Successful Exit");
Anna Perela8c991d2012-04-09 16:44:46 +0300611}
612
613static int
614fmbim_send_cpkt_response(struct f_mbim *gr, struct ctrl_pkt *cpkt)
615{
616 struct f_mbim *dev = gr;
617 unsigned long flags;
618
619 if (!gr || !cpkt) {
620 pr_err("Invalid cpkt, dev:%p cpkt:%p\n",
621 gr, cpkt);
622 return -ENODEV;
623 }
624
Anna Perel20c91152012-10-30 16:26:44 +0200625 pr_debug("dev:%p port_num#%d\n", dev, dev->port_num);
Anna Perela8c991d2012-04-09 16:44:46 +0300626
627 if (!atomic_read(&dev->online)) {
Anna Perel20c91152012-10-30 16:26:44 +0200628 pr_err("dev:%p is not connected\n", dev);
Anna Perela8c991d2012-04-09 16:44:46 +0300629 mbim_free_ctrl_pkt(cpkt);
630 return 0;
631 }
632
Ido Shayevitzbadc8ea2013-02-06 14:14:54 +0200633 if (dev->not_port.notify_state != NCM_NOTIFY_RESPONSE_AVAILABLE) {
634 pr_err("dev:%p state=%d, recover!!\n", dev,
635 dev->not_port.notify_state);
636 mbim_free_ctrl_pkt(cpkt);
637 return 0;
638 }
639
Anna Perela8c991d2012-04-09 16:44:46 +0300640 spin_lock_irqsave(&dev->lock, flags);
Anna Perel40c550c2012-04-11 14:09:27 +0300641 list_add_tail(&cpkt->list, &dev->cpkt_resp_q);
Anna Perela8c991d2012-04-09 16:44:46 +0300642 spin_unlock_irqrestore(&dev->lock, flags);
643
644 fmbim_ctrl_response_available(dev);
645
646 return 0;
647}
648
649/* ---------------------------- BAM INTERFACE ----------------------------- */
650
651static int mbim_bam_setup(int no_ports)
652{
653 int ret;
654
655 pr_info("no_ports:%d\n", no_ports);
656
657 ret = bam_data_setup(no_ports);
658 if (ret) {
659 pr_err("bam_data_setup failed err: %d\n", ret);
660 return ret;
661 }
662
663 pr_info("Initialized %d ports\n", no_ports);
664 return 0;
665}
666
Lena Salmandf7e7992013-03-15 09:46:27 +0200667int mbim_configure_params(void)
668{
669 struct teth_aggr_params aggr_params;
670 int ret = 0;
671
672 aggr_params.dl.aggr_prot = TETH_AGGR_PROTOCOL_MBIM;
673 aggr_params.dl.max_datagrams = ntb_parameters.wNtbOutMaxDatagrams;
674 aggr_params.dl.max_transfer_size_byte = ntb_parameters.dwNtbInMaxSize;
675
676 aggr_params.ul.aggr_prot = TETH_AGGR_PROTOCOL_MBIM;
677 aggr_params.ul.max_datagrams = ntb_parameters.wNtbOutMaxDatagrams;
678 aggr_params.ul.max_transfer_size_byte = ntb_parameters.dwNtbOutMaxSize;
679
680 ret = teth_bridge_set_aggr_params(&aggr_params);
681 if (ret)
682 pr_err("%s: teth_bridge_set_aggr_params failed\n", __func__);
683
684 return ret;
685}
686
Anna Perela8c991d2012-04-09 16:44:46 +0300687static int mbim_bam_connect(struct f_mbim *dev)
688{
689 int ret;
Shimrit Malichidbf43d72013-03-16 03:32:27 +0200690 u8 src_connection_idx, dst_connection_idx;
691 struct usb_gadget *gadget = dev->cdev->gadget;
Lena Salmandf7e7992013-03-15 09:46:27 +0200692 enum peer_bam bam_name = (dev->xport == USB_GADGET_XPORT_BAM2BAM_IPA) ?
693 IPA_P_BAM : A2_P_BAM;
Anna Perela8c991d2012-04-09 16:44:46 +0300694
695 pr_info("dev:%p portno:%d\n", dev, dev->port_num);
696
Lena Salmandf7e7992013-03-15 09:46:27 +0200697 src_connection_idx = usb_bam_get_connection_idx(gadget->name, bam_name,
698 USB_TO_PEER_PERIPHERAL, dev->port_num);
699 dst_connection_idx = usb_bam_get_connection_idx(gadget->name, bam_name,
700 PEER_PERIPHERAL_TO_USB, dev->port_num);
Shimrit Malichidbf43d72013-03-16 03:32:27 +0200701 if (src_connection_idx < 0 || dst_connection_idx < 0) {
702 pr_err("%s: usb_bam_get_connection_idx failed\n", __func__);
703 return ret;
704 }
705
Amit Blayf9b352b2013-03-04 15:01:40 +0200706 ret = bam_data_connect(&dev->bam_port, dev->port_num,
Lena Salmandf7e7992013-03-15 09:46:27 +0200707 dev->xport, src_connection_idx, dst_connection_idx,
708 USB_FUNC_MBIM);
709
Anna Perela8c991d2012-04-09 16:44:46 +0300710 if (ret) {
711 pr_err("bam_data_setup failed: err:%d\n",
712 ret);
713 return ret;
Anna Perela8c991d2012-04-09 16:44:46 +0300714 }
715
Shimrit Malichidbf43d72013-03-16 03:32:27 +0200716 pr_info("mbim bam connected\n");
Anna Perela8c991d2012-04-09 16:44:46 +0300717 return 0;
718}
719
720static int mbim_bam_disconnect(struct f_mbim *dev)
721{
722 pr_info("dev:%p port:%d. Do nothing.\n",
723 dev, dev->port_num);
724
Amit Blay51bebe92012-12-25 18:48:10 +0200725 bam_data_disconnect(&dev->bam_port, dev->port_num);
Anna Perela8c991d2012-04-09 16:44:46 +0300726
727 return 0;
728}
729
730/* -------------------------------------------------------------------------*/
731
732static inline void mbim_reset_values(struct f_mbim *mbim)
733{
734 mbim->parser_opts = &ndp16_opts;
735
736 mbim->ntb_input_size = NTB_DEFAULT_IN_SIZE;
737
Anna Perela8c991d2012-04-09 16:44:46 +0300738 atomic_set(&mbim->online, 0);
739}
740
Anna Perel86ea7c92012-04-24 14:31:29 +0300741static void mbim_reset_function_queue(struct f_mbim *dev)
742{
743 struct ctrl_pkt *cpkt = NULL;
744
745 pr_debug("Queue empty packet for QBI");
746
747 spin_lock(&dev->lock);
748 if (!dev->is_open) {
749 pr_err("%s: mbim file handler %p is not open", __func__, dev);
750 spin_unlock(&dev->lock);
751 return;
752 }
753
754 cpkt = mbim_alloc_ctrl_pkt(0, GFP_ATOMIC);
755 if (!cpkt) {
756 pr_err("%s: Unable to allocate reset function pkt\n", __func__);
757 spin_unlock(&dev->lock);
758 return;
759 }
760
761 list_add_tail(&cpkt->list, &dev->cpkt_req_q);
762 spin_unlock(&dev->lock);
763
764 pr_debug("%s: Wake up read queue", __func__);
765 wake_up(&dev->read_wq);
766}
767
768static void fmbim_reset_cmd_complete(struct usb_ep *ep, struct usb_request *req)
769{
770 struct f_mbim *dev = req->context;
771
772 mbim_reset_function_queue(dev);
773}
774
775static void mbim_clear_queues(struct f_mbim *mbim)
776{
777 struct ctrl_pkt *cpkt = NULL;
778 struct list_head *act, *tmp;
779
780 spin_lock(&mbim->lock);
781 list_for_each_safe(act, tmp, &mbim->cpkt_req_q) {
782 cpkt = list_entry(act, struct ctrl_pkt, list);
783 list_del(&cpkt->list);
784 mbim_free_ctrl_pkt(cpkt);
785 }
786 list_for_each_safe(act, tmp, &mbim->cpkt_resp_q) {
787 cpkt = list_entry(act, struct ctrl_pkt, list);
788 list_del(&cpkt->list);
789 mbim_free_ctrl_pkt(cpkt);
790 }
791 spin_unlock(&mbim->lock);
792}
793
Anna Perela8c991d2012-04-09 16:44:46 +0300794/*
795 * Context: mbim->lock held
796 */
797static void mbim_do_notify(struct f_mbim *mbim)
798{
799 struct usb_request *req = mbim->not_port.notify_req;
800 struct usb_cdc_notification *event;
801 struct usb_composite_dev *cdev = mbim->cdev;
802 __le32 *data;
803 int status;
804
Anna Perel20c91152012-10-30 16:26:44 +0200805 pr_debug("notify_state: %d", mbim->not_port.notify_state);
Anna Perela8c991d2012-04-09 16:44:46 +0300806
807 if (!req)
808 return;
809
810 event = req->buf;
811
812 switch (mbim->not_port.notify_state) {
813
814 case NCM_NOTIFY_NONE:
Ido Shayevitzbadc8ea2013-02-06 14:14:54 +0200815 if (atomic_read(&mbim->not_port.notify_count) > 0)
816 pr_err("Pending notifications in NCM_NOTIFY_NONE\n");
817 else
818 pr_debug("No pending notifications\n");
819
820 return;
821
822 case NCM_NOTIFY_RESPONSE_AVAILABLE:
Anna Perel68aeb172012-10-28 09:00:45 +0200823 pr_debug("Notification %02x sent\n", event->bNotificationType);
824
825 if (atomic_read(&mbim->not_port.notify_count) <= 0) {
Ido Shayevitzbadc8ea2013-02-06 14:14:54 +0200826 pr_debug("notify_response_avaliable: done");
Anna Perel68aeb172012-10-28 09:00:45 +0200827 return;
828 }
829
830 spin_unlock(&mbim->lock);
831 status = usb_ep_queue(mbim->not_port.notify, req, GFP_ATOMIC);
832 spin_lock(&mbim->lock);
833 if (status) {
834 atomic_dec(&mbim->not_port.notify_count);
835 pr_err("Queue notify request failed, err: %d", status);
836 }
837
Anna Perela8c991d2012-04-09 16:44:46 +0300838 return;
839
840 case NCM_NOTIFY_CONNECT:
841 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
842 if (mbim->is_open)
843 event->wValue = cpu_to_le16(1);
844 else
845 event->wValue = cpu_to_le16(0);
846 event->wLength = 0;
847 req->length = sizeof *event;
848
849 pr_info("notify connect %s\n",
850 mbim->is_open ? "true" : "false");
Ido Shayevitzbadc8ea2013-02-06 14:14:54 +0200851 mbim->not_port.notify_state = NCM_NOTIFY_RESPONSE_AVAILABLE;
Anna Perela8c991d2012-04-09 16:44:46 +0300852 break;
853
854 case NCM_NOTIFY_SPEED:
855 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
856 event->wValue = cpu_to_le16(0);
857 event->wLength = cpu_to_le16(8);
858 req->length = NCM_STATUS_BYTECOUNT;
859
860 /* SPEED_CHANGE data is up/down speeds in bits/sec */
861 data = req->buf + sizeof *event;
862 data[0] = cpu_to_le32(mbim_bitrate(cdev->gadget));
863 data[1] = data[0];
864
865 pr_info("notify speed %d\n",
866 mbim_bitrate(cdev->gadget));
867 mbim->not_port.notify_state = NCM_NOTIFY_CONNECT;
868 break;
869 }
Anna Perel68aeb172012-10-28 09:00:45 +0200870
Anna Perela8c991d2012-04-09 16:44:46 +0300871 event->bmRequestType = 0xA1;
872 event->wIndex = cpu_to_le16(mbim->ctrl_id);
873
Anna Perela8c991d2012-04-09 16:44:46 +0300874 /*
875 * In double buffering if there is a space in FIFO,
876 * completion callback can be called right after the call,
877 * so unlocking
878 */
Anna Perel68aeb172012-10-28 09:00:45 +0200879 atomic_inc(&mbim->not_port.notify_count);
880 pr_debug("queue request: notify_count = %d",
881 atomic_read(&mbim->not_port.notify_count));
Anna Perela8c991d2012-04-09 16:44:46 +0300882 spin_unlock(&mbim->lock);
883 status = usb_ep_queue(mbim->not_port.notify, req, GFP_ATOMIC);
884 spin_lock(&mbim->lock);
Anna Perel68aeb172012-10-28 09:00:45 +0200885 if (status) {
Anna Perela8c991d2012-04-09 16:44:46 +0300886 atomic_dec(&mbim->not_port.notify_count);
887 pr_err("usb_ep_queue failed, err: %d", status);
888 }
889}
890
891/*
892 * Context: mbim->lock held
893 */
894static void mbim_notify(struct f_mbim *mbim)
895{
896 /*
897 * If mbim_notify() is called before the second (CONNECT)
898 * notification is sent, then it will reset to send the SPEED
899 * notificaion again (and again, and again), but it's not a problem
900 */
Anna Perel20c91152012-10-30 16:26:44 +0200901 pr_debug("dev:%p\n", mbim);
Anna Perela8c991d2012-04-09 16:44:46 +0300902
Ido Shayevitzf1510c02013-02-07 11:48:49 +0200903 mbim->not_port.notify_state = NCM_NOTIFY_RESPONSE_AVAILABLE;
Anna Perela8c991d2012-04-09 16:44:46 +0300904 mbim_do_notify(mbim);
905}
906
907static void mbim_notify_complete(struct usb_ep *ep, struct usb_request *req)
908{
909 struct f_mbim *mbim = req->context;
910 struct usb_cdc_notification *event = req->buf;
911
Anna Perel68aeb172012-10-28 09:00:45 +0200912 pr_debug("dev:%p\n", mbim);
Anna Perela8c991d2012-04-09 16:44:46 +0300913
914 spin_lock(&mbim->lock);
915 switch (req->status) {
916 case 0:
Anna Perel68aeb172012-10-28 09:00:45 +0200917 atomic_dec(&mbim->not_port.notify_count);
918 pr_debug("notify_count = %d",
919 atomic_read(&mbim->not_port.notify_count));
Anna Perela8c991d2012-04-09 16:44:46 +0300920 break;
921
922 case -ECONNRESET:
923 case -ESHUTDOWN:
924 /* connection gone */
925 mbim->not_port.notify_state = NCM_NOTIFY_NONE;
926 atomic_set(&mbim->not_port.notify_count, 0);
927 pr_info("ESHUTDOWN/ECONNRESET, connection gone");
Anna Perel86ea7c92012-04-24 14:31:29 +0300928 spin_unlock(&mbim->lock);
929 mbim_clear_queues(mbim);
930 mbim_reset_function_queue(mbim);
Anna Perel89ad1212012-06-13 17:17:24 +0300931 spin_lock(&mbim->lock);
Anna Perela8c991d2012-04-09 16:44:46 +0300932 break;
933 default:
934 pr_err("Unknown event %02x --> %d\n",
935 event->bNotificationType, req->status);
936 break;
937 }
938
Anna Perela8c991d2012-04-09 16:44:46 +0300939 mbim_do_notify(mbim);
Anna Perela8c991d2012-04-09 16:44:46 +0300940 spin_unlock(&mbim->lock);
941
Anna Perel20c91152012-10-30 16:26:44 +0200942 pr_debug("dev:%p Exit\n", mbim);
Anna Perela8c991d2012-04-09 16:44:46 +0300943}
944
945static void mbim_ep0out_complete(struct usb_ep *ep, struct usb_request *req)
946{
947 /* now for SET_NTB_INPUT_SIZE only */
948 unsigned in_size = 0;
949 struct usb_function *f = req->context;
950 struct f_mbim *mbim = func_to_mbim(f);
951 struct mbim_ntb_input_size *ntb = NULL;
952
Anna Perel20c91152012-10-30 16:26:44 +0200953 pr_debug("dev:%p\n", mbim);
Anna Perela8c991d2012-04-09 16:44:46 +0300954
955 req->context = NULL;
956 if (req->status || req->actual != req->length) {
957 pr_err("Bad control-OUT transfer\n");
958 goto invalid;
959 }
960
961 if (req->length == 4) {
962 in_size = get_unaligned_le32(req->buf);
963 if (in_size < USB_CDC_NCM_NTB_MIN_IN_SIZE ||
964 in_size > le32_to_cpu(ntb_parameters.dwNtbInMaxSize)) {
965 pr_err("Illegal INPUT SIZE (%d) from host\n", in_size);
966 goto invalid;
967 }
968 } else if (req->length == 8) {
969 ntb = (struct mbim_ntb_input_size *)req->buf;
970 in_size = get_unaligned_le32(&(ntb->ntb_input_size));
971 if (in_size < USB_CDC_NCM_NTB_MIN_IN_SIZE ||
972 in_size > le32_to_cpu(ntb_parameters.dwNtbInMaxSize)) {
973 pr_err("Illegal INPUT SIZE (%d) from host\n", in_size);
974 goto invalid;
975 }
976 mbim->ntb_max_datagrams =
977 get_unaligned_le16(&(ntb->ntb_max_datagrams));
978 } else {
979 pr_err("Illegal NTB length %d\n", in_size);
980 goto invalid;
981 }
982
Anna Perel20c91152012-10-30 16:26:44 +0200983 pr_debug("Set NTB INPUT SIZE %d\n", in_size);
Anna Perela8c991d2012-04-09 16:44:46 +0300984
985 mbim->ntb_input_size = in_size;
986 return;
987
988invalid:
989 usb_ep_set_halt(ep);
990
991 pr_err("dev:%p Failed\n", mbim);
992
993 return;
994}
995
996static void
997fmbim_cmd_complete(struct usb_ep *ep, struct usb_request *req)
998{
999 struct f_mbim *dev = req->context;
1000 struct ctrl_pkt *cpkt = NULL;
1001 int len = req->actual;
1002
1003 if (!dev) {
1004 pr_err("mbim dev is null\n");
1005 return;
1006 }
1007
1008 if (req->status < 0) {
1009 pr_err("mbim command error %d\n", req->status);
1010 return;
1011 }
1012
Anna Perel20c91152012-10-30 16:26:44 +02001013 pr_debug("dev:%p port#%d\n", dev, dev->port_num);
Anna Perela8c991d2012-04-09 16:44:46 +03001014
Anna Perela8c991d2012-04-09 16:44:46 +03001015 cpkt = mbim_alloc_ctrl_pkt(len, GFP_ATOMIC);
1016 if (!cpkt) {
1017 pr_err("Unable to allocate ctrl pkt\n");
Anna Perela8c991d2012-04-09 16:44:46 +03001018 return;
1019 }
1020
Anna Perel20c91152012-10-30 16:26:44 +02001021 pr_debug("Add to cpkt_req_q packet with len = %d\n", len);
Anna Perela8c991d2012-04-09 16:44:46 +03001022 memcpy(cpkt->buf, req->buf, len);
Anna Perel20c91152012-10-30 16:26:44 +02001023
Anna Perel182ab572012-11-18 10:10:12 +02001024 spin_lock(&dev->lock);
1025 if (!dev->is_open) {
1026 pr_err("mbim file handler %p is not open", dev);
1027 spin_unlock(&dev->lock);
1028 mbim_free_ctrl_pkt(cpkt);
1029 return;
1030 }
1031
Anna Perela8c991d2012-04-09 16:44:46 +03001032 list_add_tail(&cpkt->list, &dev->cpkt_req_q);
1033 spin_unlock(&dev->lock);
1034
1035 /* wakeup read thread */
Anna Perel20c91152012-10-30 16:26:44 +02001036 pr_debug("Wake up read queue");
Anna Perela8c991d2012-04-09 16:44:46 +03001037 wake_up(&dev->read_wq);
1038
1039 return;
1040}
1041
1042static int
1043mbim_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
1044{
1045 struct f_mbim *mbim = func_to_mbim(f);
1046 struct usb_composite_dev *cdev = mbim->cdev;
1047 struct usb_request *req = cdev->req;
1048 struct ctrl_pkt *cpkt = NULL;
1049 int value = -EOPNOTSUPP;
1050 u16 w_index = le16_to_cpu(ctrl->wIndex);
1051 u16 w_value = le16_to_cpu(ctrl->wValue);
1052 u16 w_length = le16_to_cpu(ctrl->wLength);
1053
1054 /*
1055 * composite driver infrastructure handles everything except
1056 * CDC class messages; interface activation uses set_alt().
1057 */
1058
1059 if (!atomic_read(&mbim->online)) {
1060 pr_info("usb cable is not connected\n");
1061 return -ENOTCONN;
1062 }
1063
1064 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
1065 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
1066 | USB_CDC_RESET_FUNCTION:
1067
Anna Perel20c91152012-10-30 16:26:44 +02001068 pr_debug("USB_CDC_RESET_FUNCTION");
Anna Perela8c991d2012-04-09 16:44:46 +03001069 value = 0;
Anna Perel86ea7c92012-04-24 14:31:29 +03001070 req->complete = fmbim_reset_cmd_complete;
1071 req->context = mbim;
Anna Perela8c991d2012-04-09 16:44:46 +03001072 break;
1073
1074 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
1075 | USB_CDC_SEND_ENCAPSULATED_COMMAND:
1076
Anna Perel20c91152012-10-30 16:26:44 +02001077 pr_debug("USB_CDC_SEND_ENCAPSULATED_COMMAND");
Anna Perela8c991d2012-04-09 16:44:46 +03001078
1079 if (w_length > req->length) {
Anna Perel20c91152012-10-30 16:26:44 +02001080 pr_debug("w_length > req->length: %d > %d",
Anna Perela8c991d2012-04-09 16:44:46 +03001081 w_length, req->length);
1082 }
1083 value = w_length;
1084 req->complete = fmbim_cmd_complete;
1085 req->context = mbim;
1086 break;
1087
1088 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
1089 | USB_CDC_GET_ENCAPSULATED_RESPONSE:
1090
Anna Perel20c91152012-10-30 16:26:44 +02001091 pr_debug("USB_CDC_GET_ENCAPSULATED_RESPONSE");
Anna Perela8c991d2012-04-09 16:44:46 +03001092
1093 if (w_value) {
1094 pr_err("w_length > 0: %d", w_length);
1095 break;
1096 }
1097
Anna Perel20c91152012-10-30 16:26:44 +02001098 pr_debug("req%02x.%02x v%04x i%04x l%d\n",
Anna Perela8c991d2012-04-09 16:44:46 +03001099 ctrl->bRequestType, ctrl->bRequest,
1100 w_value, w_index, w_length);
1101
1102 spin_lock(&mbim->lock);
1103 if (list_empty(&mbim->cpkt_resp_q)) {
1104 pr_err("ctrl resp queue empty\n");
1105 spin_unlock(&mbim->lock);
1106 break;
1107 }
1108
1109 cpkt = list_first_entry(&mbim->cpkt_resp_q,
1110 struct ctrl_pkt, list);
1111 list_del(&cpkt->list);
1112 spin_unlock(&mbim->lock);
1113
1114 value = min_t(unsigned, w_length, cpkt->len);
1115 memcpy(req->buf, cpkt->buf, value);
1116 mbim_free_ctrl_pkt(cpkt);
1117
Anna Perel20c91152012-10-30 16:26:44 +02001118 pr_debug("copied encapsulated_response %d bytes",
Anna Perela8c991d2012-04-09 16:44:46 +03001119 value);
1120
1121 break;
1122
1123 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
1124 | USB_CDC_GET_NTB_PARAMETERS:
1125
Anna Perel20c91152012-10-30 16:26:44 +02001126 pr_debug("USB_CDC_GET_NTB_PARAMETERS");
Anna Perela8c991d2012-04-09 16:44:46 +03001127
1128 if (w_length == 0 || w_value != 0 || w_index != mbim->ctrl_id)
1129 break;
1130
1131 value = w_length > sizeof ntb_parameters ?
1132 sizeof ntb_parameters : w_length;
1133 memcpy(req->buf, &ntb_parameters, value);
1134 break;
1135
1136 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
1137 | USB_CDC_GET_NTB_INPUT_SIZE:
1138
Anna Perel20c91152012-10-30 16:26:44 +02001139 pr_debug("USB_CDC_GET_NTB_INPUT_SIZE");
Anna Perela8c991d2012-04-09 16:44:46 +03001140
1141 if (w_length < 4 || w_value != 0 || w_index != mbim->ctrl_id)
1142 break;
1143
1144 put_unaligned_le32(mbim->ntb_input_size, req->buf);
1145 value = 4;
Anna Perel20c91152012-10-30 16:26:44 +02001146 pr_debug("Reply to host INPUT SIZE %d\n",
Anna Perela8c991d2012-04-09 16:44:46 +03001147 mbim->ntb_input_size);
1148 break;
1149
1150 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
1151 | USB_CDC_SET_NTB_INPUT_SIZE:
1152
Anna Perel20c91152012-10-30 16:26:44 +02001153 pr_debug("USB_CDC_SET_NTB_INPUT_SIZE");
Anna Perela8c991d2012-04-09 16:44:46 +03001154
1155 if (w_length != 4 && w_length != 8) {
1156 pr_err("wrong NTB length %d", w_length);
1157 break;
1158 }
1159
1160 if (w_value != 0 || w_index != mbim->ctrl_id)
1161 break;
1162
1163 req->complete = mbim_ep0out_complete;
1164 req->length = w_length;
1165 req->context = f;
1166
1167 value = req->length;
1168 break;
1169
1170 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
1171 | USB_CDC_GET_NTB_FORMAT:
1172 {
1173 uint16_t format;
1174
Anna Perel20c91152012-10-30 16:26:44 +02001175 pr_debug("USB_CDC_GET_NTB_FORMAT");
Anna Perela8c991d2012-04-09 16:44:46 +03001176
1177 if (w_length < 2 || w_value != 0 || w_index != mbim->ctrl_id)
1178 break;
1179
1180 format = (mbim->parser_opts == &ndp16_opts) ? 0x0000 : 0x0001;
1181 put_unaligned_le16(format, req->buf);
1182 value = 2;
Anna Perel20c91152012-10-30 16:26:44 +02001183 pr_debug("NTB FORMAT: sending %d\n", format);
Anna Perela8c991d2012-04-09 16:44:46 +03001184 break;
1185 }
1186
1187 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
1188 | USB_CDC_SET_NTB_FORMAT:
1189 {
Anna Perel20c91152012-10-30 16:26:44 +02001190 pr_debug("USB_CDC_SET_NTB_FORMAT");
Anna Perela8c991d2012-04-09 16:44:46 +03001191
1192 if (w_length != 0 || w_index != mbim->ctrl_id)
1193 break;
1194 switch (w_value) {
1195 case 0x0000:
1196 mbim->parser_opts = &ndp16_opts;
Anna Perel20c91152012-10-30 16:26:44 +02001197 pr_debug("NCM16 selected\n");
Anna Perela8c991d2012-04-09 16:44:46 +03001198 break;
1199 case 0x0001:
1200 mbim->parser_opts = &ndp32_opts;
Anna Perel20c91152012-10-30 16:26:44 +02001201 pr_debug("NCM32 selected\n");
Anna Perela8c991d2012-04-09 16:44:46 +03001202 break;
1203 default:
1204 break;
1205 }
1206 value = 0;
1207 break;
1208 }
1209
1210 /* optional in mbim descriptor: */
1211 /* case USB_CDC_GET_MAX_DATAGRAM_SIZE: */
1212 /* case USB_CDC_SET_MAX_DATAGRAM_SIZE: */
1213
1214 default:
1215 pr_err("invalid control req: %02x.%02x v%04x i%04x l%d\n",
1216 ctrl->bRequestType, ctrl->bRequest,
1217 w_value, w_index, w_length);
1218 }
1219
1220 /* respond with data transfer or status phase? */
1221 if (value >= 0) {
Anna Perel20c91152012-10-30 16:26:44 +02001222 pr_debug("control request: %02x.%02x v%04x i%04x l%d\n",
Anna Perela8c991d2012-04-09 16:44:46 +03001223 ctrl->bRequestType, ctrl->bRequest,
1224 w_value, w_index, w_length);
Anna Perel2dfcaca2012-04-18 17:25:59 +03001225 req->zero = (value < w_length);
Anna Perela8c991d2012-04-09 16:44:46 +03001226 req->length = value;
1227 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1228 if (value < 0) {
1229 pr_err("queueing req failed: %02x.%02x, err %d\n",
1230 ctrl->bRequestType,
1231 ctrl->bRequest, value);
1232 }
1233 } else {
1234 pr_err("ctrl req err %d: %02x.%02x v%04x i%04x l%d\n",
1235 value, ctrl->bRequestType, ctrl->bRequest,
1236 w_value, w_index, w_length);
1237 }
1238
1239 /* device either stalls (value < 0) or reports success */
1240 return value;
1241}
1242
Jack Pham2df2f702012-10-11 19:08:24 -07001243/*
1244 * This function handles the Microsoft-specific OS descriptor control
1245 * requests that are issued by Windows host drivers to determine the
1246 * configuration containing the MBIM function.
1247 *
1248 * Unlike mbim_setup() this function handles two specific device requests,
1249 * and only when a configuration has not yet been selected.
1250 */
1251static int mbim_ctrlrequest(struct usb_composite_dev *cdev,
1252 const struct usb_ctrlrequest *ctrl)
1253{
1254 int value = -EOPNOTSUPP;
1255 u16 w_index = le16_to_cpu(ctrl->wIndex);
1256 u16 w_value = le16_to_cpu(ctrl->wValue);
1257 u16 w_length = le16_to_cpu(ctrl->wLength);
1258
1259 /* only respond to OS desciptors when no configuration selected */
1260 if (cdev->config || !mbim_ext_config_desc.function.subCompatibleID[0])
1261 return value;
1262
1263 pr_debug("%02x.%02x v%04x i%04x l%u",
1264 ctrl->bRequestType, ctrl->bRequest,
1265 w_value, w_index, w_length);
1266
1267 /* Handle MSFT OS string */
1268 if (ctrl->bRequestType ==
1269 (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1270 && ctrl->bRequest == USB_REQ_GET_DESCRIPTOR
1271 && (w_value >> 8) == USB_DT_STRING
1272 && (w_value & 0xFF) == MBIM_OS_STRING_ID) {
1273
1274 value = (w_length < sizeof(mbim_os_string) ?
1275 w_length : sizeof(mbim_os_string));
1276 memcpy(cdev->req->buf, mbim_os_string, value);
1277
1278 } else if (ctrl->bRequestType ==
1279 (USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE)
1280 && ctrl->bRequest == MBIM_VENDOR_CODE && w_index == 4) {
1281
1282 /* Handle Extended OS descriptor */
1283 value = (w_length < sizeof(mbim_ext_config_desc) ?
1284 w_length : sizeof(mbim_ext_config_desc));
1285 memcpy(cdev->req->buf, &mbim_ext_config_desc, value);
1286 }
1287
1288 /* respond with data transfer or status phase? */
1289 if (value >= 0) {
1290 int rc;
1291 cdev->req->zero = value < w_length;
1292 cdev->req->length = value;
1293 rc = usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
1294 if (rc < 0)
1295 pr_err("response queue error: %d", rc);
1296 }
1297 return value;
1298}
1299
Anna Perela8c991d2012-04-09 16:44:46 +03001300static int mbim_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
1301{
1302 struct f_mbim *mbim = func_to_mbim(f);
1303 struct usb_composite_dev *cdev = mbim->cdev;
1304 int ret = 0;
1305
1306 /* Control interface has only altsetting 0 */
1307 if (intf == mbim->ctrl_id) {
1308
1309 pr_info("CONTROL_INTERFACE");
1310
1311 if (alt != 0)
1312 goto fail;
1313
1314 if (mbim->not_port.notify->driver_data) {
1315 pr_info("reset mbim control %d\n", intf);
1316 usb_ep_disable(mbim->not_port.notify);
1317 }
1318
1319 ret = config_ep_by_speed(cdev->gadget, f,
1320 mbim->not_port.notify);
1321 if (ret) {
1322 mbim->not_port.notify->desc = NULL;
1323 pr_err("Failed configuring notify ep %s: err %d\n",
1324 mbim->not_port.notify->name, ret);
1325 return ret;
1326 }
1327
1328 ret = usb_ep_enable(mbim->not_port.notify);
1329 if (ret) {
1330 pr_err("usb ep#%s enable failed, err#%d\n",
1331 mbim->not_port.notify->name, ret);
1332 return ret;
1333 }
1334 mbim->not_port.notify->driver_data = mbim;
1335
1336 /* Data interface has two altsettings, 0 and 1 */
1337 } else if (intf == mbim->data_id) {
1338
1339 pr_info("DATA_INTERFACE");
1340
1341 if (alt > 1)
1342 goto fail;
1343
1344 if (mbim->bam_port.in->driver_data) {
1345 pr_info("reset mbim\n");
1346 mbim_reset_values(mbim);
Anna Perela8c991d2012-04-09 16:44:46 +03001347 }
1348
1349 /*
1350 * CDC Network only sends data in non-default altsettings.
1351 * Changing altsettings resets filters, statistics, etc.
1352 */
1353 if (alt == 1) {
1354 pr_info("Alt set 1, initialize ports");
1355
1356 if (!mbim->bam_port.in->desc) {
1357
1358 pr_info("Choose endpoints");
1359
1360 ret = config_ep_by_speed(cdev->gadget, f,
1361 mbim->bam_port.in);
1362 if (ret) {
1363 mbim->bam_port.in->desc = NULL;
1364 pr_err("IN ep %s failed: %d\n",
1365 mbim->bam_port.in->name, ret);
1366 return ret;
1367 }
1368
1369 pr_info("Set mbim port in_desc = 0x%p",
1370 mbim->bam_port.in->desc);
1371
1372 ret = config_ep_by_speed(cdev->gadget, f,
1373 mbim->bam_port.out);
1374 if (ret) {
1375 mbim->bam_port.out->desc = NULL;
1376 pr_err("OUT ep %s failed: %d\n",
1377 mbim->bam_port.out->name, ret);
1378 return ret;
1379 }
1380
1381 pr_info("Set mbim port out_desc = 0x%p",
1382 mbim->bam_port.out->desc);
Anna Perel6637bd72012-10-23 10:53:32 +02001383
1384 pr_debug("Activate mbim\n");
1385 mbim_bam_connect(mbim);
1386
Anna Perela8c991d2012-04-09 16:44:46 +03001387 } else {
1388 pr_info("PORTS already SET");
1389 }
Anna Perela8c991d2012-04-09 16:44:46 +03001390 }
1391
Bar Weinerb1c95f52012-12-23 09:09:13 +02001392 mbim->data_alt_int = alt;
Anna Perela8c991d2012-04-09 16:44:46 +03001393 spin_lock(&mbim->lock);
1394 mbim_notify(mbim);
1395 spin_unlock(&mbim->lock);
1396 } else {
1397 goto fail;
1398 }
1399
1400 atomic_set(&mbim->online, 1);
1401
1402 pr_info("SET DEVICE ONLINE");
1403
1404 /* wakeup file threads */
1405 wake_up(&mbim->read_wq);
1406 wake_up(&mbim->write_wq);
1407
1408 return 0;
1409
1410fail:
1411 pr_err("ERROR: Illegal Interface");
1412 return -EINVAL;
1413}
1414
1415/*
1416 * Because the data interface supports multiple altsettings,
1417 * this MBIM function *MUST* implement a get_alt() method.
1418 */
1419static int mbim_get_alt(struct usb_function *f, unsigned intf)
1420{
1421 struct f_mbim *mbim = func_to_mbim(f);
1422
1423 if (intf == mbim->ctrl_id)
1424 return 0;
Bar Weinerb1c95f52012-12-23 09:09:13 +02001425 else if (intf == mbim->data_id)
1426 return mbim->data_alt_int;
1427
1428 return -EINVAL;
Anna Perela8c991d2012-04-09 16:44:46 +03001429}
1430
1431static void mbim_disable(struct usb_function *f)
1432{
1433 struct f_mbim *mbim = func_to_mbim(f);
1434
1435 pr_info("SET DEVICE OFFLINE");
1436 atomic_set(&mbim->online, 0);
1437
Ido Shayevitzbadc8ea2013-02-06 14:14:54 +02001438 mbim->not_port.notify_state = NCM_NOTIFY_NONE;
1439
Anna Perel86ea7c92012-04-24 14:31:29 +03001440 mbim_clear_queues(mbim);
1441 mbim_reset_function_queue(mbim);
Anna Perela8c991d2012-04-09 16:44:46 +03001442
1443 mbim_bam_disconnect(mbim);
1444
1445 if (mbim->not_port.notify->driver_data) {
1446 usb_ep_disable(mbim->not_port.notify);
1447 mbim->not_port.notify->driver_data = NULL;
1448 }
1449
Anna Perel68aeb172012-10-28 09:00:45 +02001450 atomic_set(&mbim->not_port.notify_count, 0);
1451
Anna Perela8c991d2012-04-09 16:44:46 +03001452 pr_info("mbim deactivated\n");
1453}
1454
Anna Perel557bf722012-09-20 11:16:35 +03001455#define MBIM_ACTIVE_PORT 0
1456
1457static void mbim_suspend(struct usb_function *f)
1458{
1459 pr_info("mbim suspended\n");
1460 bam_data_suspend(MBIM_ACTIVE_PORT);
1461}
1462
1463static void mbim_resume(struct usb_function *f)
1464{
1465 pr_info("mbim resumed\n");
1466 bam_data_resume(MBIM_ACTIVE_PORT);
1467}
1468
Anna Perela8c991d2012-04-09 16:44:46 +03001469/*---------------------- function driver setup/binding ---------------------*/
1470
1471static int
1472mbim_bind(struct usb_configuration *c, struct usb_function *f)
1473{
1474 struct usb_composite_dev *cdev = c->cdev;
1475 struct f_mbim *mbim = func_to_mbim(f);
1476 int status;
1477 struct usb_ep *ep;
1478
1479 pr_info("Enter");
1480
1481 mbim->cdev = cdev;
1482
1483 /* allocate instance-specific interface IDs */
1484 status = usb_interface_id(c, f);
1485 if (status < 0)
1486 goto fail;
1487 mbim->ctrl_id = status;
1488 mbim_iad_desc.bFirstInterface = status;
1489
1490 mbim_control_intf.bInterfaceNumber = status;
1491 mbim_union_desc.bMasterInterface0 = status;
1492
1493 status = usb_interface_id(c, f);
1494 if (status < 0)
1495 goto fail;
1496 mbim->data_id = status;
Bar Weinerb1c95f52012-12-23 09:09:13 +02001497 mbim->data_alt_int = 0;
Anna Perela8c991d2012-04-09 16:44:46 +03001498
1499 mbim_data_nop_intf.bInterfaceNumber = status;
1500 mbim_data_intf.bInterfaceNumber = status;
1501 mbim_union_desc.bSlaveInterface0 = status;
1502
Anna Perel557bf722012-09-20 11:16:35 +03001503 mbim->bam_port.cdev = cdev;
1504
Anna Perela8c991d2012-04-09 16:44:46 +03001505 status = -ENODEV;
1506
1507 /* allocate instance-specific endpoints */
1508 ep = usb_ep_autoconfig(cdev->gadget, &fs_mbim_in_desc);
1509 if (!ep) {
1510 pr_err("usb epin autoconfig failed\n");
1511 goto fail;
1512 }
1513 pr_info("usb epin autoconfig succeeded\n");
1514 ep->driver_data = cdev; /* claim */
1515 mbim->bam_port.in = ep;
1516
1517 ep = usb_ep_autoconfig(cdev->gadget, &fs_mbim_out_desc);
1518 if (!ep) {
1519 pr_err("usb epout autoconfig failed\n");
1520 goto fail;
1521 }
1522 pr_info("usb epout autoconfig succeeded\n");
1523 ep->driver_data = cdev; /* claim */
1524 mbim->bam_port.out = ep;
1525
1526 ep = usb_ep_autoconfig(cdev->gadget, &fs_mbim_notify_desc);
1527 if (!ep) {
1528 pr_err("usb notify ep autoconfig failed\n");
1529 goto fail;
1530 }
1531 pr_info("usb notify ep autoconfig succeeded\n");
1532 mbim->not_port.notify = ep;
1533 ep->driver_data = cdev; /* claim */
1534
1535 status = -ENOMEM;
1536
1537 /* allocate notification request and buffer */
1538 mbim->not_port.notify_req = mbim_alloc_req(ep, NCM_STATUS_BYTECOUNT);
1539 if (!mbim->not_port.notify_req) {
1540 pr_info("failed to allocate notify request\n");
1541 goto fail;
1542 }
1543 pr_info("allocated notify ep request & request buffer\n");
1544
1545 mbim->not_port.notify_req->context = mbim;
1546 mbim->not_port.notify_req->complete = mbim_notify_complete;
1547
1548 /* copy descriptors, and track endpoint copies */
1549 f->descriptors = usb_copy_descriptors(mbim_fs_function);
1550 if (!f->descriptors)
1551 goto fail;
1552
1553 /*
1554 * support all relevant hardware speeds... we expect that when
1555 * hardware is dual speed, all bulk-capable endpoints work at
1556 * both speeds
1557 */
1558 if (gadget_is_dualspeed(c->cdev->gadget)) {
1559 hs_mbim_in_desc.bEndpointAddress =
1560 fs_mbim_in_desc.bEndpointAddress;
1561 hs_mbim_out_desc.bEndpointAddress =
1562 fs_mbim_out_desc.bEndpointAddress;
1563 hs_mbim_notify_desc.bEndpointAddress =
1564 fs_mbim_notify_desc.bEndpointAddress;
1565
1566 /* copy descriptors, and track endpoint copies */
1567 f->hs_descriptors = usb_copy_descriptors(mbim_hs_function);
1568 if (!f->hs_descriptors)
1569 goto fail;
1570 }
1571
Jack Pham2df2f702012-10-11 19:08:24 -07001572 /*
1573 * If MBIM is bound in a config other than the first, tell Windows
1574 * about it by returning the num as a string in the OS descriptor's
1575 * subCompatibleID field. Windows only supports up to config #4.
1576 */
1577 if (c->bConfigurationValue >= 2 && c->bConfigurationValue <= 4) {
1578 pr_debug("MBIM in configuration %d", c->bConfigurationValue);
1579 mbim_ext_config_desc.function.subCompatibleID[0] =
1580 c->bConfigurationValue + '0';
1581 }
1582
Anna Perela8c991d2012-04-09 16:44:46 +03001583 pr_info("mbim(%d): %s speed IN/%s OUT/%s NOTIFY/%s\n",
1584 mbim->port_num,
1585 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
1586 mbim->bam_port.in->name, mbim->bam_port.out->name,
1587 mbim->not_port.notify->name);
1588
1589 return 0;
1590
1591fail:
1592 pr_err("%s failed to bind, err %d\n", f->name, status);
1593
1594 if (f->descriptors)
1595 usb_free_descriptors(f->descriptors);
1596
1597 if (mbim->not_port.notify_req) {
1598 kfree(mbim->not_port.notify_req->buf);
1599 usb_ep_free_request(mbim->not_port.notify,
1600 mbim->not_port.notify_req);
1601 }
1602
1603 /* we might as well release our claims on endpoints */
1604 if (mbim->not_port.notify)
1605 mbim->not_port.notify->driver_data = NULL;
1606 if (mbim->bam_port.out)
1607 mbim->bam_port.out->driver_data = NULL;
1608 if (mbim->bam_port.in)
1609 mbim->bam_port.in->driver_data = NULL;
1610
1611 return status;
1612}
1613
1614static void mbim_unbind(struct usb_configuration *c, struct usb_function *f)
1615{
1616 struct f_mbim *mbim = func_to_mbim(f);
1617
1618 if (gadget_is_dualspeed(c->cdev->gadget))
1619 usb_free_descriptors(f->hs_descriptors);
1620 usb_free_descriptors(f->descriptors);
1621
1622 kfree(mbim->not_port.notify_req->buf);
1623 usb_ep_free_request(mbim->not_port.notify, mbim->not_port.notify_req);
Jack Pham2df2f702012-10-11 19:08:24 -07001624
1625 mbim_ext_config_desc.function.subCompatibleID[0] = 0;
Anna Perela8c991d2012-04-09 16:44:46 +03001626}
1627
1628/**
1629 * mbim_bind_config - add MBIM link to a configuration
1630 * @c: the configuration to support the network link
1631 * Context: single threaded during gadget setup
1632 * Returns zero on success, else negative errno.
1633 */
Lena Salmandf7e7992013-03-15 09:46:27 +02001634int mbim_bind_config(struct usb_configuration *c, unsigned portno,
1635 char *xport_name)
Anna Perela8c991d2012-04-09 16:44:46 +03001636{
1637 struct f_mbim *mbim = NULL;
1638 int status = 0;
1639
1640 pr_info("port number %u", portno);
1641
1642 if (portno >= nr_mbim_ports) {
1643 pr_err("Can not add port %u. Max ports = %d",
1644 portno, nr_mbim_ports);
1645 return -ENODEV;
1646 }
1647
1648 status = mbim_bam_setup(nr_mbim_ports);
1649 if (status) {
1650 pr_err("bam setup failed");
1651 return status;
1652 }
1653
1654 /* maybe allocate device-global string IDs */
1655 if (mbim_string_defs[0].id == 0) {
1656
1657 /* control interface label */
1658 status = usb_string_id(c->cdev);
1659 if (status < 0)
1660 return status;
1661 mbim_string_defs[STRING_CTRL_IDX].id = status;
1662 mbim_control_intf.iInterface = status;
1663
1664 /* data interface label */
1665 status = usb_string_id(c->cdev);
1666 if (status < 0)
1667 return status;
1668 mbim_string_defs[STRING_DATA_IDX].id = status;
1669 mbim_data_nop_intf.iInterface = status;
1670 mbim_data_intf.iInterface = status;
1671 }
1672
1673 /* allocate and initialize one new instance */
1674 mbim = mbim_ports[0].port;
1675 if (!mbim) {
1676 pr_info("mbim struct not allocated");
1677 return -ENOMEM;
1678 }
1679
1680 mbim->cdev = c->cdev;
1681
Anna Perela8c991d2012-04-09 16:44:46 +03001682 mbim_reset_values(mbim);
1683
1684 mbim->function.name = "usb_mbim";
1685 mbim->function.strings = mbim_strings;
1686 mbim->function.bind = mbim_bind;
1687 mbim->function.unbind = mbim_unbind;
1688 mbim->function.set_alt = mbim_set_alt;
1689 mbim->function.get_alt = mbim_get_alt;
1690 mbim->function.setup = mbim_setup;
1691 mbim->function.disable = mbim_disable;
Anna Perel557bf722012-09-20 11:16:35 +03001692 mbim->function.suspend = mbim_suspend;
1693 mbim->function.resume = mbim_resume;
Lena Salmandf7e7992013-03-15 09:46:27 +02001694 mbim->xport = str_to_xport(xport_name);
1695
1696 if (mbim->xport != USB_GADGET_XPORT_BAM2BAM_IPA) {
1697 /* Use BAM2BAM by default if not IPA */
1698 mbim->xport = USB_GADGET_XPORT_BAM2BAM;
1699 } else {
1700 /* For IPA we use limit of 16 */
1701 ntb_parameters.wNtbOutMaxDatagrams = 16;
1702 /* For IPA this is proven to give maximum throughput */
1703 ntb_parameters.dwNtbInMaxSize =
1704 cpu_to_le32(NTB_DEFAULT_IN_SIZE_IPA);
1705 ntb_parameters.dwNtbOutMaxSize = cpu_to_le32(NTB_OUT_SIZE_IPA);
1706 }
Anna Perela8c991d2012-04-09 16:44:46 +03001707
1708 INIT_LIST_HEAD(&mbim->cpkt_req_q);
1709 INIT_LIST_HEAD(&mbim->cpkt_resp_q);
1710
1711 status = usb_add_function(c, &mbim->function);
1712
1713 pr_info("Exit status %d", status);
1714
1715 return status;
1716}
1717
1718/* ------------ MBIM DRIVER File Operations API for USER SPACE ------------ */
1719
1720static ssize_t
1721mbim_read(struct file *fp, char __user *buf, size_t count, loff_t *pos)
1722{
1723 struct f_mbim *dev = fp->private_data;
1724 struct ctrl_pkt *cpkt = NULL;
1725 int ret = 0;
1726
1727 pr_debug("Enter(%d)\n", count);
1728
1729 if (!dev) {
1730 pr_err("Received NULL mbim pointer\n");
1731 return -ENODEV;
1732 }
1733
1734 if (count > MBIM_BULK_BUFFER_SIZE) {
1735 pr_err("Buffer size is too big %d, should be at most %d\n",
1736 count, MBIM_BULK_BUFFER_SIZE);
1737 return -EINVAL;
1738 }
1739
1740 if (mbim_lock(&dev->read_excl)) {
1741 pr_err("Previous reading is not finished yet\n");
1742 return -EBUSY;
1743 }
1744
1745 /* block until mbim online */
1746 while (!(atomic_read(&dev->online) || atomic_read(&dev->error))) {
1747 pr_err("USB cable not connected. Wait.\n");
1748 ret = wait_event_interruptible(dev->read_wq,
1749 (atomic_read(&dev->online) ||
1750 atomic_read(&dev->error)));
1751 if (ret < 0) {
1752 mbim_unlock(&dev->read_excl);
Anna Perel96eea9d2012-12-09 14:08:04 +02001753 return -ERESTARTSYS;
Anna Perela8c991d2012-04-09 16:44:46 +03001754 }
1755 }
1756
1757 if (atomic_read(&dev->error)) {
1758 mbim_unlock(&dev->read_excl);
1759 return -EIO;
1760 }
1761
1762 while (list_empty(&dev->cpkt_req_q)) {
Anna Perel3747fdae2013-02-17 15:19:04 +02001763 pr_debug("Requests list is empty. Wait.\n");
Anna Perela8c991d2012-04-09 16:44:46 +03001764 ret = wait_event_interruptible(dev->read_wq,
1765 !list_empty(&dev->cpkt_req_q));
1766 if (ret < 0) {
1767 pr_err("Waiting failed\n");
1768 mbim_unlock(&dev->read_excl);
Anna Perel96eea9d2012-12-09 14:08:04 +02001769 return -ERESTARTSYS;
Anna Perela8c991d2012-04-09 16:44:46 +03001770 }
1771 pr_debug("Received request packet\n");
1772 }
1773
1774 cpkt = list_first_entry(&dev->cpkt_req_q, struct ctrl_pkt,
1775 list);
1776 if (cpkt->len > count) {
1777 mbim_unlock(&dev->read_excl);
1778 pr_err("cpkt size too big:%d > buf size:%d\n",
1779 cpkt->len, count);
1780 return -ENOMEM;
1781 }
1782
1783 pr_debug("cpkt size:%d\n", cpkt->len);
1784
1785 list_del(&cpkt->list);
1786 mbim_unlock(&dev->read_excl);
1787
1788 ret = copy_to_user(buf, cpkt->buf, cpkt->len);
1789 if (ret) {
1790 pr_err("copy_to_user failed: err %d\n", ret);
Anna Perel96eea9d2012-12-09 14:08:04 +02001791 ret = -ENOMEM;
Anna Perela8c991d2012-04-09 16:44:46 +03001792 } else {
1793 pr_debug("copied %d bytes to user\n", cpkt->len);
1794 ret = cpkt->len;
1795 }
1796
1797 mbim_free_ctrl_pkt(cpkt);
1798
1799 return ret;
1800}
1801
1802static ssize_t
1803mbim_write(struct file *fp, const char __user *buf, size_t count, loff_t *pos)
1804{
1805 struct f_mbim *dev = fp->private_data;
1806 struct ctrl_pkt *cpkt = NULL;
1807 int ret = 0;
1808
1809 pr_debug("Enter(%d)", count);
1810
1811 if (!dev) {
1812 pr_err("Received NULL mbim pointer\n");
1813 return -ENODEV;
1814 }
1815
1816 if (!count) {
1817 pr_err("zero length ctrl pkt\n");
1818 return -ENODEV;
1819 }
1820
1821 if (count > MAX_CTRL_PKT_SIZE) {
1822 pr_err("given pkt size too big:%d > max_pkt_size:%d\n",
1823 count, MAX_CTRL_PKT_SIZE);
1824 return -ENOMEM;
1825 }
1826
1827 if (mbim_lock(&dev->write_excl)) {
1828 pr_err("Previous writing not finished yet\n");
1829 return -EBUSY;
1830 }
1831
1832 if (!atomic_read(&dev->online)) {
1833 pr_err("USB cable not connected\n");
1834 mbim_unlock(&dev->write_excl);
1835 return -EPIPE;
1836 }
1837
1838 cpkt = mbim_alloc_ctrl_pkt(count, GFP_KERNEL);
1839 if (!cpkt) {
1840 pr_err("failed to allocate ctrl pkt\n");
1841 mbim_unlock(&dev->write_excl);
1842 return -ENOMEM;
1843 }
1844
1845 ret = copy_from_user(cpkt->buf, buf, count);
1846 if (ret) {
1847 pr_err("copy_from_user failed err:%d\n", ret);
1848 mbim_free_ctrl_pkt(cpkt);
1849 mbim_unlock(&dev->write_excl);
1850 return 0;
1851 }
1852
1853 fmbim_send_cpkt_response(dev, cpkt);
1854
1855 mbim_unlock(&dev->write_excl);
1856
1857 pr_debug("Exit(%d)", count);
1858
1859 return count;
Anna Perel89ad1212012-06-13 17:17:24 +03001860
Anna Perela8c991d2012-04-09 16:44:46 +03001861}
1862
1863static int mbim_open(struct inode *ip, struct file *fp)
1864{
1865 pr_info("Open mbim driver\n");
1866
1867 while (!_mbim_dev) {
1868 pr_err("mbim_dev not created yet\n");
1869 return -ENODEV;
1870 }
1871
1872 if (mbim_lock(&_mbim_dev->open_excl)) {
1873 pr_err("Already opened\n");
1874 return -EBUSY;
1875 }
1876
1877 pr_info("Lock mbim_dev->open_excl for open\n");
1878
1879 if (!atomic_read(&_mbim_dev->online))
1880 pr_err("USB cable not connected\n");
1881
Anna Perela8c991d2012-04-09 16:44:46 +03001882 fp->private_data = _mbim_dev;
1883
1884 atomic_set(&_mbim_dev->error, 0);
1885
1886 spin_lock(&_mbim_dev->lock);
1887 _mbim_dev->is_open = true;
Anna Perela8c991d2012-04-09 16:44:46 +03001888 spin_unlock(&_mbim_dev->lock);
1889
1890 pr_info("Exit, mbim file opened\n");
1891
1892 return 0;
1893}
1894
1895static int mbim_release(struct inode *ip, struct file *fp)
1896{
1897 struct f_mbim *mbim = fp->private_data;
1898
1899 pr_info("Close mbim file");
1900
1901 spin_lock(&mbim->lock);
1902 mbim->is_open = false;
Anna Perela8c991d2012-04-09 16:44:46 +03001903 spin_unlock(&mbim->lock);
1904
Anna Perela8c991d2012-04-09 16:44:46 +03001905 mbim_unlock(&_mbim_dev->open_excl);
1906
1907 return 0;
1908}
1909
1910static long mbim_ioctl(struct file *fp, unsigned cmd, unsigned long arg)
1911{
1912 struct f_mbim *mbim = fp->private_data;
1913 int ret = 0;
1914
Anna Perel20c91152012-10-30 16:26:44 +02001915 pr_debug("Received command %d", cmd);
Anna Perela8c991d2012-04-09 16:44:46 +03001916
1917 if (mbim_lock(&mbim->ioctl_excl))
1918 return -EBUSY;
1919
1920 switch (cmd) {
1921 case MBIM_GET_NTB_SIZE:
1922 ret = copy_to_user((void __user *)arg,
1923 &mbim->ntb_input_size, sizeof(mbim->ntb_input_size));
1924 if (ret) {
1925 pr_err("copying to user space failed");
1926 ret = -EFAULT;
1927 }
1928 pr_info("Sent NTB size %d", mbim->ntb_input_size);
1929 break;
1930 case MBIM_GET_DATAGRAM_COUNT:
1931 ret = copy_to_user((void __user *)arg,
1932 &mbim->ntb_max_datagrams,
1933 sizeof(mbim->ntb_max_datagrams));
1934 if (ret) {
1935 pr_err("copying to user space failed");
1936 ret = -EFAULT;
1937 }
1938 pr_info("Sent NTB datagrams count %d",
1939 mbim->ntb_max_datagrams);
1940 break;
1941 default:
1942 pr_err("wrong parameter");
1943 ret = -EINVAL;
1944 }
1945
1946 mbim_unlock(&mbim->ioctl_excl);
1947
1948 return ret;
1949}
1950
1951/* file operations for MBIM device /dev/android_mbim */
1952static const struct file_operations mbim_fops = {
1953 .owner = THIS_MODULE,
1954 .open = mbim_open,
1955 .release = mbim_release,
1956 .read = mbim_read,
1957 .write = mbim_write,
1958 .unlocked_ioctl = mbim_ioctl,
1959};
1960
1961static struct miscdevice mbim_device = {
1962 .minor = MISC_DYNAMIC_MINOR,
1963 .name = "android_mbim",
1964 .fops = &mbim_fops,
1965};
1966
1967static int mbim_init(int instances)
1968{
1969 int i;
1970 struct f_mbim *dev = NULL;
1971 int ret;
1972
1973 pr_info("initialize %d instances\n", instances);
1974
1975 if (instances > NR_MBIM_PORTS) {
1976 pr_err("Max-%d instances supported\n", NR_MBIM_PORTS);
1977 return -EINVAL;
1978 }
1979
1980 for (i = 0; i < instances; i++) {
1981 dev = kzalloc(sizeof(struct f_mbim), GFP_KERNEL);
1982 if (!dev) {
1983 pr_err("Failed to allocate mbim dev\n");
1984 ret = -ENOMEM;
1985 goto fail_probe;
1986 }
1987
1988 dev->port_num = i;
1989 spin_lock_init(&dev->lock);
1990 INIT_LIST_HEAD(&dev->cpkt_req_q);
1991 INIT_LIST_HEAD(&dev->cpkt_resp_q);
1992
1993 mbim_ports[i].port = dev;
1994 mbim_ports[i].port_num = i;
1995
1996 init_waitqueue_head(&dev->read_wq);
1997 init_waitqueue_head(&dev->write_wq);
1998
1999 atomic_set(&dev->open_excl, 0);
2000 atomic_set(&dev->ioctl_excl, 0);
2001 atomic_set(&dev->read_excl, 0);
2002 atomic_set(&dev->write_excl, 0);
2003
2004 nr_mbim_ports++;
2005
2006 }
2007
2008 _mbim_dev = dev;
2009 ret = misc_register(&mbim_device);
2010 if (ret) {
2011 pr_err("mbim driver failed to register");
2012 goto fail_probe;
2013 }
2014
2015 pr_info("Initialized %d ports\n", nr_mbim_ports);
2016
2017 return ret;
2018
2019fail_probe:
2020 pr_err("Failed");
2021 for (i = 0; i < nr_mbim_ports; i++) {
2022 kfree(mbim_ports[i].port);
2023 mbim_ports[i].port = NULL;
2024 }
2025
2026 return ret;
2027}
2028
2029static void fmbim_cleanup(void)
2030{
2031 int i = 0;
2032
2033 pr_info("Enter");
2034
2035 for (i = 0; i < nr_mbim_ports; i++) {
2036 kfree(mbim_ports[i].port);
2037 mbim_ports[i].port = NULL;
2038 }
2039 nr_mbim_ports = 0;
2040
2041 misc_deregister(&mbim_device);
2042
2043 _mbim_dev = NULL;
2044}
2045