nokia.c 9.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * nokia.c -- Nokia Composite Gadget Driver
 *
 * Copyright (C) 2008-2010 Nokia Corporation
 * Contact: Felipe Balbi <felipe.balbi@nokia.com>
 *
 * This gadget driver borrows from serial.c which is:
 *
 * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
 * Copyright (C) 2008 by David Brownell
 * Copyright (C) 2008 by Nokia Corporation
 *
 * This software is distributed under the terms of the GNU General
 * Public License ("GPL") as published by the Free Software Foundation,
 * version 2 of that License.
 */

#include <linux/kernel.h>
19
#include <linux/module.h>
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include <linux/device.h>

#include "u_serial.h"
#include "u_ether.h"
#include "u_phonet.h"
#include "gadget_chips.h"

/* Defines */

#define NOKIA_VERSION_NUM		0x0211
#define NOKIA_LONG_NAME			"N900 (PC-Suite Mode)"

/*-------------------------------------------------------------------------*/

/*
 * Kbuild is not very cooperative with respect to linking separately
 * compiled library objects into one module.  So for now we won't use
 * separate compilation ... ensuring init/exit sections work to shrink
 * the runtime footprint, and giving us at least some parts of what
 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
 */
41
#define USBF_ECM_INCLUDED
42
#include "f_ecm.c"
43
#include "u_ether.h"
44 45

/*-------------------------------------------------------------------------*/
46
USB_GADGET_COMPOSITE_OPTIONS();
47

48 49
USB_ETHERNET_MODULE_PARAMETERS();

50 51 52 53 54
#define NOKIA_VENDOR_ID			0x0421	/* Nokia */
#define NOKIA_PRODUCT_ID		0x01c8	/* Nokia Gadget */

/* string IDs are assigned dynamically */

55
#define STRING_DESCRIPTION_IDX		USB_GADGET_FIRST_AVAIL_IDX
56 57 58 59 60 61

static char manufacturer_nokia[] = "Nokia";
static const char product_nokia[] = NOKIA_LONG_NAME;
static const char description_nokia[] = "PC-Suite Configuration";

static struct usb_string strings_dev[] = {
62 63 64
	[USB_GADGET_MANUFACTURER_IDX].s = manufacturer_nokia,
	[USB_GADGET_PRODUCT_IDX].s = NOKIA_LONG_NAME,
	[USB_GADGET_SERIAL_IDX].s = "",
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
	[STRING_DESCRIPTION_IDX].s = description_nokia,
	{  } /* end of list */
};

static struct usb_gadget_strings stringtab_dev = {
	.language	= 0x0409,	/* en-us */
	.strings	= strings_dev,
};

static struct usb_gadget_strings *dev_strings[] = {
	&stringtab_dev,
	NULL,
};

static struct usb_device_descriptor device_desc = {
	.bLength		= USB_DT_DEVICE_SIZE,
	.bDescriptorType	= USB_DT_DEVICE,
	.bcdUSB			= __constant_cpu_to_le16(0x0200),
	.bDeviceClass		= USB_CLASS_COMM,
	.idVendor		= __constant_cpu_to_le16(NOKIA_VENDOR_ID),
	.idProduct		= __constant_cpu_to_le16(NOKIA_PRODUCT_ID),
86
	.bcdDevice		= cpu_to_le16(NOKIA_VERSION_NUM),
87 88 89 90 91 92 93 94 95 96 97 98 99
	/* .iManufacturer = DYNAMIC */
	/* .iProduct = DYNAMIC */
	.bNumConfigurations =	1,
};

/*-------------------------------------------------------------------------*/

/* Module */
MODULE_DESCRIPTION("Nokia composite gadget driver for N900");
MODULE_AUTHOR("Felipe Balbi");
MODULE_LICENSE("GPL");

/*-------------------------------------------------------------------------*/
100 101
static struct usb_function *f_acm_cfg1;
static struct usb_function *f_acm_cfg2;
102
static u8 host_mac[ETH_ALEN];
103 104 105 106
static struct usb_function *f_obex1_cfg1;
static struct usb_function *f_obex2_cfg1;
static struct usb_function *f_obex1_cfg2;
static struct usb_function *f_obex2_cfg2;
107 108
static struct usb_function *f_phonet_cfg1;
static struct usb_function *f_phonet_cfg2;
109
static struct eth_dev *the_dev;
110

111

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
static struct usb_configuration nokia_config_500ma_driver = {
	.label		= "Bus Powered",
	.bConfigurationValue = 1,
	/* .iConfiguration = DYNAMIC */
	.bmAttributes	= USB_CONFIG_ATT_ONE,
	.MaxPower	= 500,
};

static struct usb_configuration nokia_config_100ma_driver = {
	.label		= "Self Powered",
	.bConfigurationValue = 2,
	/* .iConfiguration = DYNAMIC */
	.bmAttributes	= USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
	.MaxPower	= 100,
};

static struct usb_function_instance *fi_acm;
129 130
static struct usb_function_instance *fi_obex1;
static struct usb_function_instance *fi_obex2;
131
static struct usb_function_instance *fi_phonet;
132

133 134
static int __init nokia_bind_config(struct usb_configuration *c)
{
135
	struct usb_function *f_acm;
136
	struct usb_function *f_phonet = NULL;
137 138
	struct usb_function *f_obex1 = NULL;
	struct usb_function *f_obex2 = NULL;
139
	int status = 0;
140 141
	int obex1_stat = 0;
	int obex2_stat = 0;
142
	int phonet_stat = 0;
143

144 145 146 147 148
	if (!IS_ERR(fi_phonet)) {
		f_phonet = usb_get_function(fi_phonet);
		if (IS_ERR(f_phonet))
			pr_debug("could not get phonet function\n");
	}
149

150 151 152 153 154
	if (!IS_ERR(fi_obex1)) {
		f_obex1 = usb_get_function(fi_obex1);
		if (IS_ERR(f_obex1))
			pr_debug("could not get obex function 0\n");
	}
155

156 157 158 159 160
	if (!IS_ERR(fi_obex2)) {
		f_obex2 = usb_get_function(fi_obex2);
		if (IS_ERR(f_obex2))
			pr_debug("could not get obex function 1\n");
	}
161

162
	f_acm = usb_get_function(fi_acm);
163 164 165 166 167
	if (IS_ERR(f_acm)) {
		status = PTR_ERR(f_acm);
		goto err_get_acm;
	}

168 169 170 171 172 173
	if (!IS_ERR_OR_NULL(f_phonet)) {
		phonet_stat = usb_add_function(c, f_phonet);
		if (phonet_stat)
			pr_debug("could not add phonet function\n");
	}

174 175 176 177 178 179 180 181 182 183 184
	if (!IS_ERR_OR_NULL(f_obex1)) {
		obex1_stat = usb_add_function(c, f_obex1);
		if (obex1_stat)
			pr_debug("could not add obex function 0\n");
	}

	if (!IS_ERR_OR_NULL(f_obex2)) {
		obex2_stat = usb_add_function(c, f_obex2);
		if (obex2_stat)
			pr_debug("could not add obex function 1\n");
	}
185 186

	status = usb_add_function(c, f_acm);
187
	if (status)
188
		goto err_conf;
189

190
	status = ecm_bind_config(c, host_mac, the_dev);
191 192 193 194
	if (status) {
		pr_debug("could not bind ecm config %d\n", status);
		goto err_ecm;
	}
195
	if (c == &nokia_config_500ma_driver) {
196
		f_acm_cfg1 = f_acm;
197
		f_phonet_cfg1 = f_phonet;
198 199 200
		f_obex1_cfg1 = f_obex1;
		f_obex2_cfg1 = f_obex2;
	} else {
201
		f_acm_cfg2 = f_acm;
202
		f_phonet_cfg2 = f_phonet;
203 204 205
		f_obex1_cfg2 = f_obex1;
		f_obex2_cfg2 = f_obex2;
	}
206 207

	return status;
208 209 210
err_ecm:
	usb_remove_function(c, f_acm);
err_conf:
211 212 213 214
	if (!obex2_stat)
		usb_remove_function(c, f_obex2);
	if (!obex1_stat)
		usb_remove_function(c, f_obex1);
215 216
	if (!phonet_stat)
		usb_remove_function(c, f_phonet);
217
	usb_put_function(f_acm);
218 219 220 221 222
err_get_acm:
	if (!IS_ERR_OR_NULL(f_obex2))
		usb_put_function(f_obex2);
	if (!IS_ERR_OR_NULL(f_obex1))
		usb_put_function(f_obex1);
223 224
	if (!IS_ERR_OR_NULL(f_phonet))
		usb_put_function(f_phonet);
225
	return status;
226 227 228 229 230 231 232
}

static int __init nokia_bind(struct usb_composite_dev *cdev)
{
	struct usb_gadget	*gadget = cdev->gadget;
	int			status;

233 234
	the_dev = gether_setup(cdev->gadget, dev_addr, host_addr, host_mac,
			       qmult);
235 236
	if (IS_ERR(the_dev)) {
		status = PTR_ERR(the_dev);
237
		goto err_ether;
238
	}
239

240
	status = usb_string_ids_tab(cdev, strings_dev);
241 242
	if (status < 0)
		goto err_usb;
243 244
	device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
	device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
245
	status = strings_dev[STRING_DESCRIPTION_IDX].id;
246 247 248
	nokia_config_500ma_driver.iConfiguration = status;
	nokia_config_100ma_driver.iConfiguration = status;

249
	if (!gadget_supports_altsettings(gadget))
250 251
		goto err_usb;

252 253 254 255
	fi_phonet = usb_get_function_instance("phonet");
	if (IS_ERR(fi_phonet))
		pr_debug("could not find phonet function\n");

256 257 258 259 260 261 262 263
	fi_obex1 = usb_get_function_instance("obex");
	if (IS_ERR(fi_obex1))
		pr_debug("could not find obex function 1\n");

	fi_obex2 = usb_get_function_instance("obex");
	if (IS_ERR(fi_obex2))
		pr_debug("could not find obex function 2\n");

264 265
	fi_acm = usb_get_function_instance("acm");
	if (IS_ERR(fi_acm))
266
		goto err_obex2_inst;
267

L
Lucas De Marchi 已提交
268
	/* finally register the configuration */
269 270
	status = usb_add_config(cdev, &nokia_config_500ma_driver,
			nokia_bind_config);
271
	if (status < 0)
272
		goto err_acm_inst;
273

274 275
	status = usb_add_config(cdev, &nokia_config_100ma_driver,
			nokia_bind_config);
276
	if (status < 0)
277
		goto err_put_cfg1;
278

279
	usb_composite_overwrite_options(cdev, &coverwrite);
280 281 282 283
	dev_info(&gadget->dev, "%s\n", NOKIA_LONG_NAME);

	return 0;

284 285
err_put_cfg1:
	usb_put_function(f_acm_cfg1);
286 287 288 289
	if (!IS_ERR_OR_NULL(f_obex1_cfg1))
		usb_put_function(f_obex1_cfg1);
	if (!IS_ERR_OR_NULL(f_obex2_cfg1))
		usb_put_function(f_obex2_cfg1);
290 291
	if (!IS_ERR_OR_NULL(f_phonet_cfg1))
		usb_put_function(f_phonet_cfg1);
292 293
err_acm_inst:
	usb_put_function_instance(fi_acm);
294 295 296 297 298
err_obex2_inst:
	if (!IS_ERR(fi_obex2))
		usb_put_function_instance(fi_obex2);
	if (!IS_ERR(fi_obex1))
		usb_put_function_instance(fi_obex1);
299 300
	if (!IS_ERR(fi_phonet))
		usb_put_function_instance(fi_phonet);
301
err_usb:
302
	gether_cleanup(the_dev);
303 304 305 306 307 308
err_ether:
	return status;
}

static int __exit nokia_unbind(struct usb_composite_dev *cdev)
{
309 310 311 312 313 314 315 316
	if (!IS_ERR_OR_NULL(f_obex1_cfg2))
		usb_put_function(f_obex1_cfg2);
	if (!IS_ERR_OR_NULL(f_obex2_cfg2))
		usb_put_function(f_obex2_cfg2);
	if (!IS_ERR_OR_NULL(f_obex1_cfg1))
		usb_put_function(f_obex1_cfg1);
	if (!IS_ERR_OR_NULL(f_obex2_cfg1))
		usb_put_function(f_obex2_cfg1);
317 318 319 320
	if (!IS_ERR_OR_NULL(f_phonet_cfg1))
		usb_put_function(f_phonet_cfg1);
	if (!IS_ERR_OR_NULL(f_phonet_cfg2))
		usb_put_function(f_phonet_cfg2);
321 322
	usb_put_function(f_acm_cfg1);
	usb_put_function(f_acm_cfg2);
323

324 325
	if (!IS_ERR(fi_obex2))
		usb_put_function_instance(fi_obex2);
326 327 328 329
	if (!IS_ERR(fi_obex1))
		usb_put_function_instance(fi_obex1);
	if (!IS_ERR(fi_phonet))
		usb_put_function_instance(fi_phonet);
330
	usb_put_function_instance(fi_acm);
331

332
	gether_cleanup(the_dev);
333 334 335 336

	return 0;
}

337
static __refdata struct usb_composite_driver nokia_driver = {
338 339 340
	.name		= "g_nokia",
	.dev		= &device_desc,
	.strings	= dev_strings,
341
	.max_speed	= USB_SPEED_HIGH,
342
	.bind		= nokia_bind,
343 344 345 346 347
	.unbind		= __exit_p(nokia_unbind),
};

static int __init nokia_init(void)
{
348
	return usb_composite_probe(&nokia_driver);
349 350 351 352 353 354 355 356
}
module_init(nokia_init);

static void __exit nokia_cleanup(void)
{
	usb_composite_unregister(&nokia_driver);
}
module_exit(nokia_cleanup);