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