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 | */ |
Sebastian Andrzej Siewior | ff47f59 | 2012-12-23 21:10:07 +0100 | [diff] [blame] | 40 | #define USB_FACM_INCLUDED |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 41 | #include "f_acm.c" |
| 42 | #include "f_ecm.c" |
| 43 | #include "f_obex.c" |
| 44 | #include "f_serial.c" |
| 45 | #include "f_phonet.c" |
| 46 | #include "u_ether.c" |
| 47 | |
| 48 | /*-------------------------------------------------------------------------*/ |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 49 | USB_GADGET_COMPOSITE_OPTIONS(); |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 50 | |
| 51 | #define NOKIA_VENDOR_ID 0x0421 /* Nokia */ |
| 52 | #define NOKIA_PRODUCT_ID 0x01c8 /* Nokia Gadget */ |
| 53 | |
| 54 | /* string IDs are assigned dynamically */ |
| 55 | |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 56 | #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 57 | |
| 58 | static char manufacturer_nokia[] = "Nokia"; |
| 59 | static const char product_nokia[] = NOKIA_LONG_NAME; |
| 60 | static const char description_nokia[] = "PC-Suite Configuration"; |
| 61 | |
| 62 | static struct usb_string strings_dev[] = { |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 63 | [USB_GADGET_MANUFACTURER_IDX].s = manufacturer_nokia, |
| 64 | [USB_GADGET_PRODUCT_IDX].s = NOKIA_LONG_NAME, |
| 65 | [USB_GADGET_SERIAL_IDX].s = "", |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 66 | [STRING_DESCRIPTION_IDX].s = description_nokia, |
| 67 | { } /* end of list */ |
| 68 | }; |
| 69 | |
| 70 | static struct usb_gadget_strings stringtab_dev = { |
| 71 | .language = 0x0409, /* en-us */ |
| 72 | .strings = strings_dev, |
| 73 | }; |
| 74 | |
| 75 | static struct usb_gadget_strings *dev_strings[] = { |
| 76 | &stringtab_dev, |
| 77 | NULL, |
| 78 | }; |
| 79 | |
| 80 | static struct usb_device_descriptor device_desc = { |
| 81 | .bLength = USB_DT_DEVICE_SIZE, |
| 82 | .bDescriptorType = USB_DT_DEVICE, |
| 83 | .bcdUSB = __constant_cpu_to_le16(0x0200), |
| 84 | .bDeviceClass = USB_CLASS_COMM, |
| 85 | .idVendor = __constant_cpu_to_le16(NOKIA_VENDOR_ID), |
| 86 | .idProduct = __constant_cpu_to_le16(NOKIA_PRODUCT_ID), |
Sebastian Andrzej Siewior | ed9cbda | 2012-09-10 09:16:07 +0200 | [diff] [blame] | 87 | .bcdDevice = cpu_to_le16(NOKIA_VERSION_NUM), |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 88 | /* .iManufacturer = DYNAMIC */ |
| 89 | /* .iProduct = DYNAMIC */ |
| 90 | .bNumConfigurations = 1, |
| 91 | }; |
| 92 | |
| 93 | /*-------------------------------------------------------------------------*/ |
| 94 | |
| 95 | /* Module */ |
| 96 | MODULE_DESCRIPTION("Nokia composite gadget driver for N900"); |
| 97 | MODULE_AUTHOR("Felipe Balbi"); |
| 98 | MODULE_LICENSE("GPL"); |
| 99 | |
| 100 | /*-------------------------------------------------------------------------*/ |
| 101 | |
| 102 | static u8 hostaddr[ETH_ALEN]; |
| 103 | |
Sebastian Andrzej Siewior | 19b10a8 | 2012-12-23 21:10:06 +0100 | [diff] [blame] | 104 | enum { |
| 105 | TTY_PORT_OBEX0, |
| 106 | TTY_PORT_OBEX1, |
| 107 | TTY_PORT_ACM, |
| 108 | TTY_PORTS_MAX, |
| 109 | }; |
| 110 | |
| 111 | static unsigned char tty_lines[TTY_PORTS_MAX]; |
| 112 | |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 113 | static int __init nokia_bind_config(struct usb_configuration *c) |
| 114 | { |
| 115 | int status = 0; |
| 116 | |
| 117 | status = phonet_bind_config(c); |
| 118 | if (status) |
| 119 | printk(KERN_DEBUG "could not bind phonet config\n"); |
| 120 | |
Sebastian Andrzej Siewior | 19b10a8 | 2012-12-23 21:10:06 +0100 | [diff] [blame] | 121 | status = obex_bind_config(c, tty_lines[TTY_PORT_OBEX0]); |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 122 | if (status) |
| 123 | printk(KERN_DEBUG "could not bind obex config %d\n", 0); |
| 124 | |
Sebastian Andrzej Siewior | 19b10a8 | 2012-12-23 21:10:06 +0100 | [diff] [blame] | 125 | status = obex_bind_config(c, tty_lines[TTY_PORT_OBEX1]); |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 126 | if (status) |
| 127 | printk(KERN_DEBUG "could not bind obex config %d\n", 0); |
| 128 | |
Sebastian Andrzej Siewior | 19b10a8 | 2012-12-23 21:10:06 +0100 | [diff] [blame] | 129 | status = acm_bind_config(c, tty_lines[TTY_PORT_ACM]); |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 130 | if (status) |
| 131 | printk(KERN_DEBUG "could not bind acm config\n"); |
| 132 | |
| 133 | status = ecm_bind_config(c, hostaddr); |
| 134 | if (status) |
| 135 | printk(KERN_DEBUG "could not bind ecm config\n"); |
| 136 | |
| 137 | return status; |
| 138 | } |
| 139 | |
| 140 | static struct usb_configuration nokia_config_500ma_driver = { |
| 141 | .label = "Bus Powered", |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 142 | .bConfigurationValue = 1, |
| 143 | /* .iConfiguration = DYNAMIC */ |
| 144 | .bmAttributes = USB_CONFIG_ATT_ONE, |
Sebastian Andrzej Siewior | 8f900a9 | 2012-12-03 20:07:05 +0100 | [diff] [blame] | 145 | .MaxPower = 500, |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | static struct usb_configuration nokia_config_100ma_driver = { |
| 149 | .label = "Self Powered", |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 150 | .bConfigurationValue = 2, |
| 151 | /* .iConfiguration = DYNAMIC */ |
| 152 | .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, |
Sebastian Andrzej Siewior | 8f900a9 | 2012-12-03 20:07:05 +0100 | [diff] [blame] | 153 | .MaxPower = 100, |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | static int __init nokia_bind(struct usb_composite_dev *cdev) |
| 157 | { |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 158 | struct usb_gadget *gadget = cdev->gadget; |
| 159 | int status; |
Sebastian Andrzej Siewior | 19b10a8 | 2012-12-23 21:10:06 +0100 | [diff] [blame] | 160 | int cur_line; |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 161 | |
| 162 | status = gphonet_setup(cdev->gadget); |
| 163 | if (status < 0) |
| 164 | goto err_phonet; |
| 165 | |
Sebastian Andrzej Siewior | 19b10a8 | 2012-12-23 21:10:06 +0100 | [diff] [blame] | 166 | for (cur_line = 0; cur_line < TTY_PORTS_MAX; cur_line++) { |
| 167 | status = gserial_alloc_line(&tty_lines[cur_line]); |
| 168 | if (status) |
| 169 | goto err_ether; |
| 170 | } |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 171 | |
| 172 | status = gether_setup(cdev->gadget, hostaddr); |
| 173 | if (status < 0) |
| 174 | goto err_ether; |
| 175 | |
Sebastian Andrzej Siewior | e1f15cc | 2012-09-06 20:11:16 +0200 | [diff] [blame] | 176 | status = usb_string_ids_tab(cdev, strings_dev); |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 177 | if (status < 0) |
| 178 | goto err_usb; |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 179 | device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id; |
| 180 | device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id; |
Sebastian Andrzej Siewior | e1f15cc | 2012-09-06 20:11:16 +0200 | [diff] [blame] | 181 | status = strings_dev[STRING_DESCRIPTION_IDX].id; |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 182 | nokia_config_500ma_driver.iConfiguration = status; |
| 183 | nokia_config_100ma_driver.iConfiguration = status; |
| 184 | |
Sebastian Andrzej Siewior | ed9cbda | 2012-09-10 09:16:07 +0200 | [diff] [blame] | 185 | if (!gadget_supports_altsettings(gadget)) |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 186 | goto err_usb; |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 187 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 188 | /* finally register the configuration */ |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 189 | status = usb_add_config(cdev, &nokia_config_500ma_driver, |
| 190 | nokia_bind_config); |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 191 | if (status < 0) |
| 192 | goto err_usb; |
| 193 | |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 194 | status = usb_add_config(cdev, &nokia_config_100ma_driver, |
| 195 | nokia_bind_config); |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 196 | if (status < 0) |
| 197 | goto err_usb; |
| 198 | |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 199 | usb_composite_overwrite_options(cdev, &coverwrite); |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 200 | dev_info(&gadget->dev, "%s\n", NOKIA_LONG_NAME); |
| 201 | |
| 202 | return 0; |
| 203 | |
| 204 | err_usb: |
| 205 | gether_cleanup(); |
| 206 | err_ether: |
Sebastian Andrzej Siewior | 19b10a8 | 2012-12-23 21:10:06 +0100 | [diff] [blame] | 207 | cur_line--; |
| 208 | while (cur_line >= 0) |
| 209 | gserial_free_line(tty_lines[cur_line--]); |
| 210 | |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 211 | gphonet_cleanup(); |
| 212 | err_phonet: |
| 213 | return status; |
| 214 | } |
| 215 | |
| 216 | static int __exit nokia_unbind(struct usb_composite_dev *cdev) |
| 217 | { |
Sebastian Andrzej Siewior | 19b10a8 | 2012-12-23 21:10:06 +0100 | [diff] [blame] | 218 | int i; |
| 219 | |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 220 | gphonet_cleanup(); |
Sebastian Andrzej Siewior | 19b10a8 | 2012-12-23 21:10:06 +0100 | [diff] [blame] | 221 | |
| 222 | for (i = 0; i < TTY_PORTS_MAX; i++) |
| 223 | gserial_free_line(tty_lines[i]); |
| 224 | |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 225 | gether_cleanup(); |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
Sebastian Andrzej Siewior | c2ec75c | 2012-09-06 20:11:03 +0200 | [diff] [blame] | 230 | static __refdata struct usb_composite_driver nokia_driver = { |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 231 | .name = "g_nokia", |
| 232 | .dev = &device_desc, |
| 233 | .strings = dev_strings, |
Tatyana Brokhman | 35a0e0b | 2011-06-29 16:41:49 +0300 | [diff] [blame] | 234 | .max_speed = USB_SPEED_HIGH, |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 235 | .bind = nokia_bind, |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 236 | .unbind = __exit_p(nokia_unbind), |
| 237 | }; |
| 238 | |
| 239 | static int __init nokia_init(void) |
| 240 | { |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 241 | return usb_composite_probe(&nokia_driver); |
Felipe Balbi | f358f5b | 2010-01-05 16:10:13 +0200 | [diff] [blame] | 242 | } |
| 243 | module_init(nokia_init); |
| 244 | |
| 245 | static void __exit nokia_cleanup(void) |
| 246 | { |
| 247 | usb_composite_unregister(&nokia_driver); |
| 248 | } |
| 249 | module_exit(nokia_cleanup); |
| 250 | |