Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 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/utsname.h> |
| 20 | #include <linux/device.h> |
| 21 | |
| 22 | #include "u_serial.h" |
| 23 | #include "u_ether.h" |
| 24 | #include "u_phonet.h" |
| 25 | #include "gadget_chips.h" |
| 26 | |
| 27 | /* Defines */ |
| 28 | |
| 29 | #define NOKIA_VERSION_NUM 0x0211 |
| 30 | #define NOKIA_LONG_NAME "N900 (PC-Suite Mode)" |
| 31 | |
| 32 | /*-------------------------------------------------------------------------*/ |
| 33 | |
| 34 | /* |
| 35 | * Kbuild is not very cooperative with respect to linking separately |
| 36 | * compiled library objects into one module. So for now we won't use |
| 37 | * separate compilation ... ensuring init/exit sections work to shrink |
| 38 | * the runtime footprint, and giving us at least some parts of what |
| 39 | * a "gcc --combine ... part1.c part2.c part3.c ... " build would. |
| 40 | */ |
| 41 | #include "composite.c" |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 42 | |
| 43 | #include "u_serial.c" |
| 44 | #include "f_acm.c" |
| 45 | #include "f_ecm.c" |
| 46 | #include "f_obex.c" |
| 47 | #include "f_serial.c" |
| 48 | #include "f_phonet.c" |
| 49 | #include "u_ether.c" |
| 50 | |
| 51 | /*-------------------------------------------------------------------------*/ |
| 52 | |
| 53 | #define NOKIA_VENDOR_ID 0x0421 /* Nokia */ |
| 54 | #define NOKIA_PRODUCT_ID 0x01c8 /* Nokia Gadget */ |
| 55 | |
| 56 | /* string IDs are assigned dynamically */ |
| 57 | |
| 58 | #define STRING_MANUFACTURER_IDX 0 |
| 59 | #define STRING_PRODUCT_IDX 1 |
| 60 | #define STRING_DESCRIPTION_IDX 2 |
| 61 | |
| 62 | static char manufacturer_nokia[] = "Nokia"; |
| 63 | static const char product_nokia[] = NOKIA_LONG_NAME; |
| 64 | static const char description_nokia[] = "PC-Suite Configuration"; |
| 65 | |
| 66 | static struct usb_string strings_dev[] = { |
| 67 | [STRING_MANUFACTURER_IDX].s = manufacturer_nokia, |
| 68 | [STRING_PRODUCT_IDX].s = NOKIA_LONG_NAME, |
| 69 | [STRING_DESCRIPTION_IDX].s = description_nokia, |
| 70 | { } /* end of list */ |
| 71 | }; |
| 72 | |
| 73 | static struct usb_gadget_strings stringtab_dev = { |
| 74 | .language = 0x0409, /* en-us */ |
| 75 | .strings = strings_dev, |
| 76 | }; |
| 77 | |
| 78 | static struct usb_gadget_strings *dev_strings[] = { |
| 79 | &stringtab_dev, |
| 80 | NULL, |
| 81 | }; |
| 82 | |
| 83 | static struct usb_device_descriptor device_desc = { |
| 84 | .bLength = USB_DT_DEVICE_SIZE, |
| 85 | .bDescriptorType = USB_DT_DEVICE, |
| 86 | .bcdUSB = __constant_cpu_to_le16(0x0200), |
| 87 | .bDeviceClass = USB_CLASS_COMM, |
| 88 | .idVendor = __constant_cpu_to_le16(NOKIA_VENDOR_ID), |
| 89 | .idProduct = __constant_cpu_to_le16(NOKIA_PRODUCT_ID), |
| 90 | /* .iManufacturer = DYNAMIC */ |
| 91 | /* .iProduct = DYNAMIC */ |
| 92 | .bNumConfigurations = 1, |
| 93 | }; |
| 94 | |
| 95 | /*-------------------------------------------------------------------------*/ |
| 96 | |
| 97 | /* Module */ |
| 98 | MODULE_DESCRIPTION("Nokia composite gadget driver for N900"); |
| 99 | MODULE_AUTHOR("Felipe Balbi"); |
| 100 | MODULE_LICENSE("GPL"); |
| 101 | |
| 102 | /*-------------------------------------------------------------------------*/ |
| 103 | |
| 104 | static u8 hostaddr[ETH_ALEN]; |
| 105 | |
| 106 | static int __init nokia_bind_config(struct usb_configuration *c) |
| 107 | { |
| 108 | int status = 0; |
| 109 | |
| 110 | status = phonet_bind_config(c); |
| 111 | if (status) |
| 112 | printk(KERN_DEBUG "could not bind phonet config\n"); |
| 113 | |
| 114 | status = obex_bind_config(c, 0); |
| 115 | if (status) |
| 116 | printk(KERN_DEBUG "could not bind obex config %d\n", 0); |
| 117 | |
| 118 | status = obex_bind_config(c, 1); |
| 119 | if (status) |
| 120 | printk(KERN_DEBUG "could not bind obex config %d\n", 0); |
| 121 | |
| 122 | status = acm_bind_config(c, 2); |
| 123 | if (status) |
| 124 | printk(KERN_DEBUG "could not bind acm config\n"); |
| 125 | |
| 126 | status = ecm_bind_config(c, hostaddr); |
| 127 | if (status) |
| 128 | printk(KERN_DEBUG "could not bind ecm config\n"); |
| 129 | |
| 130 | return status; |
| 131 | } |
| 132 | |
| 133 | static struct usb_configuration nokia_config_500ma_driver = { |
| 134 | .label = "Bus Powered", |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 135 | .bConfigurationValue = 1, |
| 136 | /* .iConfiguration = DYNAMIC */ |
| 137 | .bmAttributes = USB_CONFIG_ATT_ONE, |
| 138 | .bMaxPower = 250, /* 500mA */ |
| 139 | }; |
| 140 | |
| 141 | static struct usb_configuration nokia_config_100ma_driver = { |
| 142 | .label = "Self Powered", |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 143 | .bConfigurationValue = 2, |
| 144 | /* .iConfiguration = DYNAMIC */ |
| 145 | .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, |
| 146 | .bMaxPower = 50, /* 100 mA */ |
| 147 | }; |
| 148 | |
| 149 | static int __init nokia_bind(struct usb_composite_dev *cdev) |
| 150 | { |
| 151 | int gcnum; |
| 152 | struct usb_gadget *gadget = cdev->gadget; |
| 153 | int status; |
| 154 | |
| 155 | status = gphonet_setup(cdev->gadget); |
| 156 | if (status < 0) |
| 157 | goto err_phonet; |
| 158 | |
| 159 | status = gserial_setup(cdev->gadget, 3); |
| 160 | if (status < 0) |
| 161 | goto err_serial; |
| 162 | |
| 163 | status = gether_setup(cdev->gadget, hostaddr); |
| 164 | if (status < 0) |
| 165 | goto err_ether; |
| 166 | |
Sebastian Andrzej Siewior | e1f15cc | 2012-09-06 20:11:16 +0200 | [diff] [blame^] | 167 | status = usb_string_ids_tab(cdev, strings_dev); |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 168 | if (status < 0) |
| 169 | goto err_usb; |
Sebastian Andrzej Siewior | e1f15cc | 2012-09-06 20:11:16 +0200 | [diff] [blame^] | 170 | device_desc.iManufacturer = strings_dev[STRING_MANUFACTURER_IDX].id; |
| 171 | device_desc.iProduct = strings_dev[STRING_PRODUCT_IDX].id; |
| 172 | status = strings_dev[STRING_DESCRIPTION_IDX].id; |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 173 | nokia_config_500ma_driver.iConfiguration = status; |
| 174 | nokia_config_100ma_driver.iConfiguration = status; |
| 175 | |
| 176 | /* set up other descriptors */ |
| 177 | gcnum = usb_gadget_controller_number(gadget); |
| 178 | if (gcnum >= 0) |
| 179 | device_desc.bcdDevice = cpu_to_le16(NOKIA_VERSION_NUM); |
| 180 | else { |
| 181 | /* this should only work with hw that supports altsettings |
| 182 | * and several endpoints, anything else, panic. |
| 183 | */ |
| 184 | pr_err("nokia_bind: controller '%s' not recognized\n", |
| 185 | gadget->name); |
| 186 | goto err_usb; |
| 187 | } |
| 188 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 189 | /* finally register the configuration */ |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 190 | status = usb_add_config(cdev, &nokia_config_500ma_driver, |
| 191 | nokia_bind_config); |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 192 | if (status < 0) |
| 193 | goto err_usb; |
| 194 | |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 195 | status = usb_add_config(cdev, &nokia_config_100ma_driver, |
| 196 | nokia_bind_config); |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 197 | if (status < 0) |
| 198 | goto err_usb; |
| 199 | |
| 200 | dev_info(&gadget->dev, "%s\n", NOKIA_LONG_NAME); |
| 201 | |
| 202 | return 0; |
| 203 | |
| 204 | err_usb: |
| 205 | gether_cleanup(); |
| 206 | err_ether: |
| 207 | gserial_cleanup(); |
| 208 | err_serial: |
| 209 | gphonet_cleanup(); |
| 210 | err_phonet: |
| 211 | return status; |
| 212 | } |
| 213 | |
| 214 | static int __exit nokia_unbind(struct usb_composite_dev *cdev) |
| 215 | { |
| 216 | gphonet_cleanup(); |
| 217 | gserial_cleanup(); |
| 218 | gether_cleanup(); |
| 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
Sebastian Andrzej Siewior | c2ec75c | 2012-09-06 20:11:03 +0200 | [diff] [blame] | 223 | static __refdata struct usb_composite_driver nokia_driver = { |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 224 | .name = "g_nokia", |
| 225 | .dev = &device_desc, |
| 226 | .strings = dev_strings, |
Tatyana Brokhman | 35a0e0b | 2011-06-29 16:41:49 +0300 | [diff] [blame] | 227 | .max_speed = USB_SPEED_HIGH, |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 228 | .bind = nokia_bind, |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 229 | .unbind = __exit_p(nokia_unbind), |
| 230 | }; |
| 231 | |
| 232 | static int __init nokia_init(void) |
| 233 | { |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 234 | return usb_composite_probe(&nokia_driver); |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 235 | } |
| 236 | module_init(nokia_init); |
| 237 | |
| 238 | static void __exit nokia_cleanup(void) |
| 239 | { |
| 240 | usb_composite_unregister(&nokia_driver); |
| 241 | } |
| 242 | module_exit(nokia_cleanup); |
| 243 | |