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> |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 19 | #include <linux/device.h> |
| 20 | |
| 21 | #include "u_serial.h" |
| 22 | #include "u_ether.h" |
| 23 | #include "u_phonet.h" |
| 24 | #include "gadget_chips.h" |
| 25 | |
| 26 | /* Defines */ |
| 27 | |
| 28 | #define NOKIA_VERSION_NUM 0x0211 |
| 29 | #define NOKIA_LONG_NAME "N900 (PC-Suite Mode)" |
| 30 | |
| 31 | /*-------------------------------------------------------------------------*/ |
| 32 | |
| 33 | /* |
| 34 | * Kbuild is not very cooperative with respect to linking separately |
| 35 | * compiled library objects into one module. So for now we won't use |
| 36 | * separate compilation ... ensuring init/exit sections work to shrink |
| 37 | * the runtime footprint, and giving us at least some parts of what |
| 38 | * a "gcc --combine ... part1.c part2.c part3.c ... " build would. |
| 39 | */ |
Andrzej Pietrasiewicz | 1d8fc25 | 2013-03-21 15:33:42 +0100 | [diff] [blame] | 40 | #define USBF_OBEX_INCLUDED |
Felipe Balbi | 9b192de | 2013-04-03 21:01:44 +0300 | [diff] [blame] | 41 | #include "f_ecm.c" |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 42 | #include "f_obex.c" |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 43 | #include "f_phonet.c" |
| 44 | #include "u_ether.c" |
| 45 | |
| 46 | /*-------------------------------------------------------------------------*/ |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 47 | USB_GADGET_COMPOSITE_OPTIONS(); |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 48 | |
| 49 | #define NOKIA_VENDOR_ID 0x0421 /* Nokia */ |
| 50 | #define NOKIA_PRODUCT_ID 0x01c8 /* Nokia Gadget */ |
| 51 | |
| 52 | /* string IDs are assigned dynamically */ |
| 53 | |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 54 | #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 55 | |
| 56 | static char manufacturer_nokia[] = "Nokia"; |
| 57 | static const char product_nokia[] = NOKIA_LONG_NAME; |
| 58 | static const char description_nokia[] = "PC-Suite Configuration"; |
| 59 | |
| 60 | static struct usb_string strings_dev[] = { |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 61 | [USB_GADGET_MANUFACTURER_IDX].s = manufacturer_nokia, |
| 62 | [USB_GADGET_PRODUCT_IDX].s = NOKIA_LONG_NAME, |
| 63 | [USB_GADGET_SERIAL_IDX].s = "", |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 64 | [STRING_DESCRIPTION_IDX].s = description_nokia, |
| 65 | { } /* end of list */ |
| 66 | }; |
| 67 | |
| 68 | static struct usb_gadget_strings stringtab_dev = { |
| 69 | .language = 0x0409, /* en-us */ |
| 70 | .strings = strings_dev, |
| 71 | }; |
| 72 | |
| 73 | static struct usb_gadget_strings *dev_strings[] = { |
| 74 | &stringtab_dev, |
| 75 | NULL, |
| 76 | }; |
| 77 | |
| 78 | static struct usb_device_descriptor device_desc = { |
| 79 | .bLength = USB_DT_DEVICE_SIZE, |
| 80 | .bDescriptorType = USB_DT_DEVICE, |
| 81 | .bcdUSB = __constant_cpu_to_le16(0x0200), |
| 82 | .bDeviceClass = USB_CLASS_COMM, |
| 83 | .idVendor = __constant_cpu_to_le16(NOKIA_VENDOR_ID), |
| 84 | .idProduct = __constant_cpu_to_le16(NOKIA_PRODUCT_ID), |
Sebastian Andrzej Siewior | ed9cbda | 2012-09-10 09:16:07 +0200 | [diff] [blame] | 85 | .bcdDevice = cpu_to_le16(NOKIA_VERSION_NUM), |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 86 | /* .iManufacturer = DYNAMIC */ |
| 87 | /* .iProduct = DYNAMIC */ |
| 88 | .bNumConfigurations = 1, |
| 89 | }; |
| 90 | |
| 91 | /*-------------------------------------------------------------------------*/ |
| 92 | |
| 93 | /* Module */ |
| 94 | MODULE_DESCRIPTION("Nokia composite gadget driver for N900"); |
| 95 | MODULE_AUTHOR("Felipe Balbi"); |
| 96 | MODULE_LICENSE("GPL"); |
| 97 | |
| 98 | /*-------------------------------------------------------------------------*/ |
Sebastian Andrzej Siewior | 1576182 | 2013-01-25 14:09:17 +0100 | [diff] [blame] | 99 | static struct usb_function *f_acm_cfg1; |
| 100 | static struct usb_function *f_acm_cfg2; |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 101 | static u8 hostaddr[ETH_ALEN]; |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 102 | static struct eth_dev *the_dev; |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 103 | |
Sebastian Andrzej Siewior | 19b10a8 | 2012-12-23 21:10:06 +0100 | [diff] [blame] | 104 | enum { |
| 105 | TTY_PORT_OBEX0, |
| 106 | TTY_PORT_OBEX1, |
Sebastian Andrzej Siewior | 19b10a8 | 2012-12-23 21:10:06 +0100 | [diff] [blame] | 107 | TTY_PORTS_MAX, |
| 108 | }; |
| 109 | |
| 110 | static unsigned char tty_lines[TTY_PORTS_MAX]; |
| 111 | |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 112 | static struct usb_configuration nokia_config_500ma_driver = { |
| 113 | .label = "Bus Powered", |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 114 | .bConfigurationValue = 1, |
| 115 | /* .iConfiguration = DYNAMIC */ |
| 116 | .bmAttributes = USB_CONFIG_ATT_ONE, |
Sebastian Andrzej Siewior | 8f900a9 | 2012-12-03 20:07:05 +0100 | [diff] [blame] | 117 | .MaxPower = 500, |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | static struct usb_configuration nokia_config_100ma_driver = { |
| 121 | .label = "Self Powered", |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 122 | .bConfigurationValue = 2, |
| 123 | /* .iConfiguration = DYNAMIC */ |
| 124 | .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, |
Sebastian Andrzej Siewior | 8f900a9 | 2012-12-03 20:07:05 +0100 | [diff] [blame] | 125 | .MaxPower = 100, |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 126 | }; |
| 127 | |
Sebastian Andrzej Siewior | 1576182 | 2013-01-25 14:09:17 +0100 | [diff] [blame] | 128 | static struct usb_function_instance *fi_acm; |
| 129 | |
| 130 | static int __init nokia_bind_config(struct usb_configuration *c) |
| 131 | { |
| 132 | struct usb_function *f_acm; |
| 133 | int status = 0; |
| 134 | |
| 135 | status = phonet_bind_config(c); |
| 136 | if (status) |
| 137 | printk(KERN_DEBUG "could not bind phonet config\n"); |
| 138 | |
| 139 | status = obex_bind_config(c, tty_lines[TTY_PORT_OBEX0]); |
| 140 | if (status) |
| 141 | printk(KERN_DEBUG "could not bind obex config %d\n", 0); |
| 142 | |
| 143 | status = obex_bind_config(c, tty_lines[TTY_PORT_OBEX1]); |
| 144 | if (status) |
| 145 | printk(KERN_DEBUG "could not bind obex config %d\n", 0); |
| 146 | |
| 147 | f_acm = usb_get_function(fi_acm); |
| 148 | if (IS_ERR(f_acm)) |
| 149 | return PTR_ERR(f_acm); |
| 150 | |
| 151 | status = usb_add_function(c, f_acm); |
| 152 | if (status) |
| 153 | goto err_conf; |
| 154 | |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 155 | status = ecm_bind_config(c, hostaddr, the_dev); |
Sebastian Andrzej Siewior | 1576182 | 2013-01-25 14:09:17 +0100 | [diff] [blame] | 156 | if (status) { |
| 157 | pr_debug("could not bind ecm config %d\n", status); |
| 158 | goto err_ecm; |
| 159 | } |
| 160 | if (c == &nokia_config_500ma_driver) |
| 161 | f_acm_cfg1 = f_acm; |
| 162 | else |
| 163 | f_acm_cfg2 = f_acm; |
| 164 | |
| 165 | return status; |
| 166 | err_ecm: |
| 167 | usb_remove_function(c, f_acm); |
| 168 | err_conf: |
| 169 | usb_put_function(f_acm); |
| 170 | return status; |
| 171 | } |
| 172 | |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 173 | static int __init nokia_bind(struct usb_composite_dev *cdev) |
| 174 | { |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 175 | struct usb_gadget *gadget = cdev->gadget; |
| 176 | int status; |
Sebastian Andrzej Siewior | 19b10a8 | 2012-12-23 21:10:06 +0100 | [diff] [blame] | 177 | int cur_line; |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 178 | |
| 179 | status = gphonet_setup(cdev->gadget); |
| 180 | if (status < 0) |
| 181 | goto err_phonet; |
| 182 | |
Sebastian Andrzej Siewior | 19b10a8 | 2012-12-23 21:10:06 +0100 | [diff] [blame] | 183 | for (cur_line = 0; cur_line < TTY_PORTS_MAX; cur_line++) { |
| 184 | status = gserial_alloc_line(&tty_lines[cur_line]); |
| 185 | if (status) |
| 186 | goto err_ether; |
| 187 | } |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 188 | |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 189 | the_dev = gether_setup(cdev->gadget, hostaddr); |
| 190 | if (IS_ERR(the_dev)) { |
| 191 | status = PTR_ERR(the_dev); |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 192 | goto err_ether; |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 193 | } |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 194 | |
Sebastian Andrzej Siewior | e1f15cc | 2012-09-06 20:11:16 +0200 | [diff] [blame] | 195 | status = usb_string_ids_tab(cdev, strings_dev); |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 196 | if (status < 0) |
| 197 | goto err_usb; |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 198 | device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id; |
| 199 | device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id; |
Sebastian Andrzej Siewior | e1f15cc | 2012-09-06 20:11:16 +0200 | [diff] [blame] | 200 | status = strings_dev[STRING_DESCRIPTION_IDX].id; |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 201 | nokia_config_500ma_driver.iConfiguration = status; |
| 202 | nokia_config_100ma_driver.iConfiguration = status; |
| 203 | |
Sebastian Andrzej Siewior | ed9cbda | 2012-09-10 09:16:07 +0200 | [diff] [blame] | 204 | if (!gadget_supports_altsettings(gadget)) |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 205 | goto err_usb; |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 206 | |
Sebastian Andrzej Siewior | 1576182 | 2013-01-25 14:09:17 +0100 | [diff] [blame] | 207 | fi_acm = usb_get_function_instance("acm"); |
| 208 | if (IS_ERR(fi_acm)) |
| 209 | goto err_usb; |
Sebastian Andrzej Siewior | 1576182 | 2013-01-25 14:09:17 +0100 | [diff] [blame] | 210 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 211 | /* finally register the configuration */ |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 212 | status = usb_add_config(cdev, &nokia_config_500ma_driver, |
| 213 | nokia_bind_config); |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 214 | if (status < 0) |
Sebastian Andrzej Siewior | 1576182 | 2013-01-25 14:09:17 +0100 | [diff] [blame] | 215 | goto err_acm_inst; |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 216 | |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 217 | status = usb_add_config(cdev, &nokia_config_100ma_driver, |
| 218 | nokia_bind_config); |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 219 | if (status < 0) |
Sebastian Andrzej Siewior | 1576182 | 2013-01-25 14:09:17 +0100 | [diff] [blame] | 220 | goto err_put_cfg1; |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 221 | |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 222 | usb_composite_overwrite_options(cdev, &coverwrite); |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 223 | dev_info(&gadget->dev, "%s\n", NOKIA_LONG_NAME); |
| 224 | |
| 225 | return 0; |
| 226 | |
Sebastian Andrzej Siewior | 1576182 | 2013-01-25 14:09:17 +0100 | [diff] [blame] | 227 | err_put_cfg1: |
| 228 | usb_put_function(f_acm_cfg1); |
| 229 | err_acm_inst: |
| 230 | usb_put_function_instance(fi_acm); |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 231 | err_usb: |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 232 | gether_cleanup(the_dev); |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 233 | err_ether: |
Sebastian Andrzej Siewior | 19b10a8 | 2012-12-23 21:10:06 +0100 | [diff] [blame] | 234 | cur_line--; |
| 235 | while (cur_line >= 0) |
| 236 | gserial_free_line(tty_lines[cur_line--]); |
| 237 | |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 238 | gphonet_cleanup(); |
| 239 | err_phonet: |
| 240 | return status; |
| 241 | } |
| 242 | |
| 243 | static int __exit nokia_unbind(struct usb_composite_dev *cdev) |
| 244 | { |
Sebastian Andrzej Siewior | 19b10a8 | 2012-12-23 21:10:06 +0100 | [diff] [blame] | 245 | int i; |
| 246 | |
Sebastian Andrzej Siewior | 1576182 | 2013-01-25 14:09:17 +0100 | [diff] [blame] | 247 | usb_put_function(f_acm_cfg1); |
| 248 | usb_put_function(f_acm_cfg2); |
| 249 | usb_put_function_instance(fi_acm); |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 250 | gphonet_cleanup(); |
Sebastian Andrzej Siewior | 19b10a8 | 2012-12-23 21:10:06 +0100 | [diff] [blame] | 251 | |
| 252 | for (i = 0; i < TTY_PORTS_MAX; i++) |
| 253 | gserial_free_line(tty_lines[i]); |
| 254 | |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 255 | gether_cleanup(the_dev); |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
Sebastian Andrzej Siewior | c2ec75c | 2012-09-06 20:11:03 +0200 | [diff] [blame] | 260 | static __refdata struct usb_composite_driver nokia_driver = { |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 261 | .name = "g_nokia", |
| 262 | .dev = &device_desc, |
| 263 | .strings = dev_strings, |
Tatyana Brokhman | 35a0e0b | 2011-06-29 16:41:49 +0300 | [diff] [blame] | 264 | .max_speed = USB_SPEED_HIGH, |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 265 | .bind = nokia_bind, |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 266 | .unbind = __exit_p(nokia_unbind), |
| 267 | }; |
| 268 | |
| 269 | static int __init nokia_init(void) |
| 270 | { |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 271 | return usb_composite_probe(&nokia_driver); |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 272 | } |
| 273 | module_init(nokia_init); |
| 274 | |
| 275 | static void __exit nokia_cleanup(void) |
| 276 | { |
| 277 | usb_composite_unregister(&nokia_driver); |
| 278 | } |
| 279 | module_exit(nokia_cleanup); |