1 /* 2 * nokia.c -- Nokia Composite Gadget Driver 3 * 4 * Copyright (C) 2008-2010 Nokia Corporation 5 * Contact: Felipe Balbi <felipe.balbi@nokia.com> 6 * 7 * This gadget driver borrows from serial.c which is: 8 * 9 * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com) 10 * Copyright (C) 2008 by David Brownell 11 * Copyright (C) 2008 by Nokia Corporation 12 * 13 * This software is distributed under the terms of the GNU General 14 * Public License ("GPL") as published by the Free Software Foundation, 15 * version 2 of that License. 16 */ 17 18 #include <linux/kernel.h> 19 #include <linux/module.h> 20 #include <linux/device.h> 21 22 #include "u_serial.h" 23 #include "u_ether.h" 24 #include "u_phonet.h" 25 #include "u_ecm.h" 26 #include "f_mass_storage.h" 27 28 /* Defines */ 29 30 #define NOKIA_VERSION_NUM 0x0211 31 #define NOKIA_LONG_NAME "N900 (PC-Suite Mode)" 32 33 USB_GADGET_COMPOSITE_OPTIONS(); 34 35 USB_ETHERNET_MODULE_PARAMETERS(); 36 37 static struct fsg_module_parameters fsg_mod_data = { 38 .stall = 0, 39 .luns = 2, 40 .removable_count = 2, 41 .removable = { 1, 1, }, 42 }; 43 44 #ifdef CONFIG_USB_GADGET_DEBUG_FILES 45 46 static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS; 47 48 #else 49 50 /* 51 * Number of buffers we will use. 52 * 2 is usually enough for good buffering pipeline 53 */ 54 #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS 55 56 #endif /* CONFIG_USB_DEBUG */ 57 58 FSG_MODULE_PARAMETERS(/* no prefix */, fsg_mod_data); 59 60 #define NOKIA_VENDOR_ID 0x0421 /* Nokia */ 61 #define NOKIA_PRODUCT_ID 0x01c8 /* Nokia Gadget */ 62 63 /* string IDs are assigned dynamically */ 64 65 #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX 66 67 static char manufacturer_nokia[] = "Nokia"; 68 static const char product_nokia[] = NOKIA_LONG_NAME; 69 static const char description_nokia[] = "PC-Suite Configuration"; 70 71 static struct usb_string strings_dev[] = { 72 [USB_GADGET_MANUFACTURER_IDX].s = manufacturer_nokia, 73 [USB_GADGET_PRODUCT_IDX].s = NOKIA_LONG_NAME, 74 [USB_GADGET_SERIAL_IDX].s = "", 75 [STRING_DESCRIPTION_IDX].s = description_nokia, 76 { } /* end of list */ 77 }; 78 79 static struct usb_gadget_strings stringtab_dev = { 80 .language = 0x0409, /* en-us */ 81 .strings = strings_dev, 82 }; 83 84 static struct usb_gadget_strings *dev_strings[] = { 85 &stringtab_dev, 86 NULL, 87 }; 88 89 static struct usb_device_descriptor device_desc = { 90 .bLength = USB_DT_DEVICE_SIZE, 91 .bDescriptorType = USB_DT_DEVICE, 92 .bcdUSB = cpu_to_le16(0x0200), 93 .bDeviceClass = USB_CLASS_COMM, 94 .idVendor = cpu_to_le16(NOKIA_VENDOR_ID), 95 .idProduct = cpu_to_le16(NOKIA_PRODUCT_ID), 96 .bcdDevice = cpu_to_le16(NOKIA_VERSION_NUM), 97 /* .iManufacturer = DYNAMIC */ 98 /* .iProduct = DYNAMIC */ 99 .bNumConfigurations = 1, 100 }; 101 102 /*-------------------------------------------------------------------------*/ 103 104 /* Module */ 105 MODULE_DESCRIPTION("Nokia composite gadget driver for N900"); 106 MODULE_AUTHOR("Felipe Balbi"); 107 MODULE_LICENSE("GPL"); 108 109 /*-------------------------------------------------------------------------*/ 110 static struct usb_function *f_acm_cfg1; 111 static struct usb_function *f_acm_cfg2; 112 static struct usb_function *f_ecm_cfg1; 113 static struct usb_function *f_ecm_cfg2; 114 static struct usb_function *f_obex1_cfg1; 115 static struct usb_function *f_obex2_cfg1; 116 static struct usb_function *f_obex1_cfg2; 117 static struct usb_function *f_obex2_cfg2; 118 static struct usb_function *f_phonet_cfg1; 119 static struct usb_function *f_phonet_cfg2; 120 static struct usb_function *f_msg_cfg1; 121 static struct usb_function *f_msg_cfg2; 122 123 124 static struct usb_configuration nokia_config_500ma_driver = { 125 .label = "Bus Powered", 126 .bConfigurationValue = 1, 127 /* .iConfiguration = DYNAMIC */ 128 .bmAttributes = USB_CONFIG_ATT_ONE, 129 .MaxPower = 500, 130 }; 131 132 static struct usb_configuration nokia_config_100ma_driver = { 133 .label = "Self Powered", 134 .bConfigurationValue = 2, 135 /* .iConfiguration = DYNAMIC */ 136 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, 137 .MaxPower = 100, 138 }; 139 140 static struct usb_function_instance *fi_acm; 141 static struct usb_function_instance *fi_ecm; 142 static struct usb_function_instance *fi_obex1; 143 static struct usb_function_instance *fi_obex2; 144 static struct usb_function_instance *fi_phonet; 145 static struct usb_function_instance *fi_msg; 146 147 static int nokia_bind_config(struct usb_configuration *c) 148 { 149 struct usb_function *f_acm; 150 struct usb_function *f_phonet = NULL; 151 struct usb_function *f_obex1 = NULL; 152 struct usb_function *f_ecm; 153 struct usb_function *f_obex2 = NULL; 154 struct usb_function *f_msg; 155 struct fsg_opts *fsg_opts; 156 int status = 0; 157 int obex1_stat = -1; 158 int obex2_stat = -1; 159 int phonet_stat = -1; 160 161 if (!IS_ERR(fi_phonet)) { 162 f_phonet = usb_get_function(fi_phonet); 163 if (IS_ERR(f_phonet)) 164 pr_debug("could not get phonet function\n"); 165 } 166 167 if (!IS_ERR(fi_obex1)) { 168 f_obex1 = usb_get_function(fi_obex1); 169 if (IS_ERR(f_obex1)) 170 pr_debug("could not get obex function 0\n"); 171 } 172 173 if (!IS_ERR(fi_obex2)) { 174 f_obex2 = usb_get_function(fi_obex2); 175 if (IS_ERR(f_obex2)) 176 pr_debug("could not get obex function 1\n"); 177 } 178 179 f_acm = usb_get_function(fi_acm); 180 if (IS_ERR(f_acm)) { 181 status = PTR_ERR(f_acm); 182 goto err_get_acm; 183 } 184 185 f_ecm = usb_get_function(fi_ecm); 186 if (IS_ERR(f_ecm)) { 187 status = PTR_ERR(f_ecm); 188 goto err_get_ecm; 189 } 190 191 f_msg = usb_get_function(fi_msg); 192 if (IS_ERR(f_msg)) { 193 status = PTR_ERR(f_msg); 194 goto err_get_msg; 195 } 196 197 if (!IS_ERR_OR_NULL(f_phonet)) { 198 phonet_stat = usb_add_function(c, f_phonet); 199 if (phonet_stat) 200 pr_debug("could not add phonet function\n"); 201 } 202 203 if (!IS_ERR_OR_NULL(f_obex1)) { 204 obex1_stat = usb_add_function(c, f_obex1); 205 if (obex1_stat) 206 pr_debug("could not add obex function 0\n"); 207 } 208 209 if (!IS_ERR_OR_NULL(f_obex2)) { 210 obex2_stat = usb_add_function(c, f_obex2); 211 if (obex2_stat) 212 pr_debug("could not add obex function 1\n"); 213 } 214 215 status = usb_add_function(c, f_acm); 216 if (status) 217 goto err_conf; 218 219 status = usb_add_function(c, f_ecm); 220 if (status) { 221 pr_debug("could not bind ecm config %d\n", status); 222 goto err_ecm; 223 } 224 225 fsg_opts = fsg_opts_from_func_inst(fi_msg); 226 227 status = fsg_common_run_thread(fsg_opts->common); 228 if (status) 229 goto err_msg; 230 231 status = usb_add_function(c, f_msg); 232 if (status) 233 goto err_msg; 234 235 if (c == &nokia_config_500ma_driver) { 236 f_acm_cfg1 = f_acm; 237 f_ecm_cfg1 = f_ecm; 238 f_phonet_cfg1 = f_phonet; 239 f_obex1_cfg1 = f_obex1; 240 f_obex2_cfg1 = f_obex2; 241 f_msg_cfg1 = f_msg; 242 } else { 243 f_acm_cfg2 = f_acm; 244 f_ecm_cfg2 = f_ecm; 245 f_phonet_cfg2 = f_phonet; 246 f_obex1_cfg2 = f_obex1; 247 f_obex2_cfg2 = f_obex2; 248 f_msg_cfg2 = f_msg; 249 } 250 251 return status; 252 err_msg: 253 usb_remove_function(c, f_ecm); 254 err_ecm: 255 usb_remove_function(c, f_acm); 256 err_conf: 257 if (!obex2_stat) 258 usb_remove_function(c, f_obex2); 259 if (!obex1_stat) 260 usb_remove_function(c, f_obex1); 261 if (!phonet_stat) 262 usb_remove_function(c, f_phonet); 263 usb_put_function(f_msg); 264 err_get_msg: 265 usb_put_function(f_ecm); 266 err_get_ecm: 267 usb_put_function(f_acm); 268 err_get_acm: 269 if (!IS_ERR_OR_NULL(f_obex2)) 270 usb_put_function(f_obex2); 271 if (!IS_ERR_OR_NULL(f_obex1)) 272 usb_put_function(f_obex1); 273 if (!IS_ERR_OR_NULL(f_phonet)) 274 usb_put_function(f_phonet); 275 return status; 276 } 277 278 static int nokia_bind(struct usb_composite_dev *cdev) 279 { 280 struct usb_gadget *gadget = cdev->gadget; 281 struct fsg_opts *fsg_opts; 282 struct fsg_config fsg_config; 283 int status; 284 285 status = usb_string_ids_tab(cdev, strings_dev); 286 if (status < 0) 287 goto err_usb; 288 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id; 289 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id; 290 status = strings_dev[STRING_DESCRIPTION_IDX].id; 291 nokia_config_500ma_driver.iConfiguration = status; 292 nokia_config_100ma_driver.iConfiguration = status; 293 294 if (!gadget_is_altset_supported(gadget)) { 295 status = -ENODEV; 296 goto err_usb; 297 } 298 299 fi_phonet = usb_get_function_instance("phonet"); 300 if (IS_ERR(fi_phonet)) 301 pr_debug("could not find phonet function\n"); 302 303 fi_obex1 = usb_get_function_instance("obex"); 304 if (IS_ERR(fi_obex1)) 305 pr_debug("could not find obex function 1\n"); 306 307 fi_obex2 = usb_get_function_instance("obex"); 308 if (IS_ERR(fi_obex2)) 309 pr_debug("could not find obex function 2\n"); 310 311 fi_acm = usb_get_function_instance("acm"); 312 if (IS_ERR(fi_acm)) { 313 status = PTR_ERR(fi_acm); 314 goto err_obex2_inst; 315 } 316 317 fi_ecm = usb_get_function_instance("ecm"); 318 if (IS_ERR(fi_ecm)) { 319 status = PTR_ERR(fi_ecm); 320 goto err_acm_inst; 321 } 322 323 fi_msg = usb_get_function_instance("mass_storage"); 324 if (IS_ERR(fi_msg)) { 325 status = PTR_ERR(fi_msg); 326 goto err_ecm_inst; 327 } 328 329 /* set up mass storage function */ 330 fsg_config_from_params(&fsg_config, &fsg_mod_data, fsg_num_buffers); 331 fsg_config.vendor_name = "Nokia"; 332 fsg_config.product_name = "N900"; 333 334 fsg_opts = fsg_opts_from_func_inst(fi_msg); 335 fsg_opts->no_configfs = true; 336 337 status = fsg_common_set_num_buffers(fsg_opts->common, fsg_num_buffers); 338 if (status) 339 goto err_msg_inst; 340 341 status = fsg_common_set_cdev(fsg_opts->common, cdev, fsg_config.can_stall); 342 if (status) 343 goto err_msg_buf; 344 345 fsg_common_set_sysfs(fsg_opts->common, true); 346 347 status = fsg_common_create_luns(fsg_opts->common, &fsg_config); 348 if (status) 349 goto err_msg_buf; 350 351 fsg_common_set_inquiry_string(fsg_opts->common, fsg_config.vendor_name, 352 fsg_config.product_name); 353 354 /* finally register the configuration */ 355 status = usb_add_config(cdev, &nokia_config_500ma_driver, 356 nokia_bind_config); 357 if (status < 0) 358 goto err_msg_luns; 359 360 status = usb_add_config(cdev, &nokia_config_100ma_driver, 361 nokia_bind_config); 362 if (status < 0) 363 goto err_put_cfg1; 364 365 usb_composite_overwrite_options(cdev, &coverwrite); 366 dev_info(&gadget->dev, "%s\n", NOKIA_LONG_NAME); 367 368 return 0; 369 370 err_put_cfg1: 371 usb_put_function(f_acm_cfg1); 372 if (!IS_ERR_OR_NULL(f_obex1_cfg1)) 373 usb_put_function(f_obex1_cfg1); 374 if (!IS_ERR_OR_NULL(f_obex2_cfg1)) 375 usb_put_function(f_obex2_cfg1); 376 if (!IS_ERR_OR_NULL(f_phonet_cfg1)) 377 usb_put_function(f_phonet_cfg1); 378 usb_put_function(f_ecm_cfg1); 379 err_msg_luns: 380 fsg_common_remove_luns(fsg_opts->common); 381 err_msg_buf: 382 fsg_common_free_buffers(fsg_opts->common); 383 err_msg_inst: 384 usb_put_function_instance(fi_msg); 385 err_ecm_inst: 386 usb_put_function_instance(fi_ecm); 387 err_acm_inst: 388 usb_put_function_instance(fi_acm); 389 err_obex2_inst: 390 if (!IS_ERR(fi_obex2)) 391 usb_put_function_instance(fi_obex2); 392 if (!IS_ERR(fi_obex1)) 393 usb_put_function_instance(fi_obex1); 394 if (!IS_ERR(fi_phonet)) 395 usb_put_function_instance(fi_phonet); 396 err_usb: 397 return status; 398 } 399 400 static int nokia_unbind(struct usb_composite_dev *cdev) 401 { 402 if (!IS_ERR_OR_NULL(f_obex1_cfg2)) 403 usb_put_function(f_obex1_cfg2); 404 if (!IS_ERR_OR_NULL(f_obex2_cfg2)) 405 usb_put_function(f_obex2_cfg2); 406 if (!IS_ERR_OR_NULL(f_obex1_cfg1)) 407 usb_put_function(f_obex1_cfg1); 408 if (!IS_ERR_OR_NULL(f_obex2_cfg1)) 409 usb_put_function(f_obex2_cfg1); 410 if (!IS_ERR_OR_NULL(f_phonet_cfg1)) 411 usb_put_function(f_phonet_cfg1); 412 if (!IS_ERR_OR_NULL(f_phonet_cfg2)) 413 usb_put_function(f_phonet_cfg2); 414 usb_put_function(f_acm_cfg1); 415 usb_put_function(f_acm_cfg2); 416 usb_put_function(f_ecm_cfg1); 417 usb_put_function(f_ecm_cfg2); 418 usb_put_function(f_msg_cfg1); 419 usb_put_function(f_msg_cfg2); 420 421 usb_put_function_instance(fi_msg); 422 usb_put_function_instance(fi_ecm); 423 if (!IS_ERR(fi_obex2)) 424 usb_put_function_instance(fi_obex2); 425 if (!IS_ERR(fi_obex1)) 426 usb_put_function_instance(fi_obex1); 427 if (!IS_ERR(fi_phonet)) 428 usb_put_function_instance(fi_phonet); 429 usb_put_function_instance(fi_acm); 430 431 return 0; 432 } 433 434 static struct usb_composite_driver nokia_driver = { 435 .name = "g_nokia", 436 .dev = &device_desc, 437 .strings = dev_strings, 438 .max_speed = USB_SPEED_HIGH, 439 .bind = nokia_bind, 440 .unbind = nokia_unbind, 441 }; 442 443 module_usb_composite_driver(nokia_driver); 444