1 #include <linux/module.h>
2 #include <linux/utsname.h>
6 * kbuild is not very cooperative with respect to linking separately
7 * compiled library objects into one module. So for now we won't use
8 * separate compilation ... ensuring init/exit sections work to shrink
9 * the runtime footprint, and giving us at least some parts of what
10 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
13 #include "composite.c"
14 #include "usbstring.c"
16 #include "epautoconf.c"
18 #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
19 # if defined USB_ETH_RNDIS
22 # ifdef CONFIG_USB_FUNCTIONFS_RNDIS
23 # define USB_ETH_RNDIS y
27 # include "f_subset.c"
34 static u8 gfs_hostaddr[ETH_ALEN];
35 # ifdef CONFIG_USB_FUNCTIONFS_ETH
36 static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]);
39 # define gether_cleanup() do { } while (0)
40 # define gether_setup(gadget, hostaddr) ((int)0)
41 # define gfs_hostaddr NULL
47 #define DRIVER_NAME "g_ffs"
48 #define DRIVER_DESC "USB Function Filesystem"
49 #define DRIVER_VERSION "24 Aug 2004"
51 MODULE_DESCRIPTION(DRIVER_DESC);
52 MODULE_AUTHOR("Michal Nazarewicz");
53 MODULE_LICENSE("GPL");
56 static unsigned short gfs_vendor_id = 0x0525; /* XXX NetChip */
57 static unsigned short gfs_product_id = 0xa4ac; /* XXX */
59 static struct usb_device_descriptor gfs_dev_desc = {
60 .bLength = sizeof gfs_dev_desc,
61 .bDescriptorType = USB_DT_DEVICE,
63 .bcdUSB = cpu_to_le16(0x0200),
64 .bDeviceClass = USB_CLASS_PER_INTERFACE,
66 /* Vendor and product id can be overridden by module parameters. */
67 /* .idVendor = cpu_to_le16(gfs_vendor_id), */
68 /* .idProduct = cpu_to_le16(gfs_product_id), */
69 /* .bcdDevice = f(hardware) */
70 /* .iManufacturer = DYNAMIC */
71 /* .iProduct = DYNAMIC */
72 /* NO SERIAL NUMBER */
73 .bNumConfigurations = 1,
76 #define GFS_MODULE_PARAM_DESC(name, field) \
77 MODULE_PARM_DESC(name, "Value of the " #field " field of the device descriptor sent to the host. Takes effect only prior to the user-space driver registering to the FunctionFS.")
79 module_param_named(usb_class, gfs_dev_desc.bDeviceClass, byte, 0644);
80 GFS_MODULE_PARAM_DESC(usb_class, bDeviceClass);
81 module_param_named(usb_subclass, gfs_dev_desc.bDeviceSubClass, byte, 0644);
82 GFS_MODULE_PARAM_DESC(usb_subclass, bDeviceSubClass);
83 module_param_named(usb_protocol, gfs_dev_desc.bDeviceProtocol, byte, 0644);
84 GFS_MODULE_PARAM_DESC(usb_protocol, bDeviceProtocol);
85 module_param_named(usb_vendor, gfs_vendor_id, ushort, 0644);
86 GFS_MODULE_PARAM_DESC(usb_vendor, idVendor);
87 module_param_named(usb_product, gfs_product_id, ushort, 0644);
88 GFS_MODULE_PARAM_DESC(usb_product, idProduct);
92 static const struct usb_descriptor_header *gfs_otg_desc[] = {
93 (const struct usb_descriptor_header *)
94 &(const struct usb_otg_descriptor) {
95 .bLength = sizeof(struct usb_otg_descriptor),
96 .bDescriptorType = USB_DT_OTG,
98 /* REVISIT SRP-only hardware is possible, although
99 * it would not be called "OTG" ... */
100 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
106 /* string IDs are assigned dynamically */
109 GFS_STRING_MANUFACTURER_IDX,
110 GFS_STRING_PRODUCT_IDX,
111 GFS_STRING_FIRST_CONFIG_IDX,
114 static char gfs_manufacturer[50];
115 static const char gfs_driver_desc[] = DRIVER_DESC;
116 static const char gfs_short_name[] = DRIVER_NAME;
118 static struct usb_string gfs_strings[] = {
119 [GFS_STRING_MANUFACTURER_IDX].s = gfs_manufacturer,
120 [GFS_STRING_PRODUCT_IDX].s = gfs_driver_desc,
121 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
122 { .s = "FunctionFS + RNDIS" },
124 #ifdef CONFIG_USB_FUNCTIONFS_ETH
125 { .s = "FunctionFS + ECM" },
127 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
128 { .s = "FunctionFS" },
130 { } /* end of list */
133 static struct usb_gadget_strings *gfs_dev_strings[] = {
134 &(struct usb_gadget_strings) {
135 .language = 0x0409, /* en-us */
136 .strings = gfs_strings,
143 struct gfs_configuration {
144 struct usb_configuration c;
145 int (*eth)(struct usb_configuration *c, u8 *ethaddr);
146 } gfs_configurations[] = {
147 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
149 .eth = rndis_bind_config,
153 #ifdef CONFIG_USB_FUNCTIONFS_ETH
155 .eth = eth_bind_config,
159 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
166 static int gfs_bind(struct usb_composite_dev *cdev);
167 static int gfs_unbind(struct usb_composite_dev *cdev);
168 static int gfs_do_config(struct usb_configuration *c);
170 static struct usb_composite_driver gfs_driver = {
171 .name = gfs_short_name,
172 .dev = &gfs_dev_desc,
173 .strings = gfs_dev_strings,
175 .unbind = gfs_unbind,
179 static struct ffs_data *gfs_ffs_data;
180 static unsigned long gfs_registered;
183 static int gfs_init(void)
187 return functionfs_init();
189 module_init(gfs_init);
191 static void gfs_exit(void)
195 if (test_and_clear_bit(0, &gfs_registered))
196 usb_composite_unregister(&gfs_driver);
198 functionfs_cleanup();
200 module_exit(gfs_exit);
203 static int functionfs_ready_callback(struct ffs_data *ffs)
209 if (WARN_ON(test_and_set_bit(0, &gfs_registered)))
213 ret = usb_composite_register(&gfs_driver);
214 if (unlikely(ret < 0))
215 clear_bit(0, &gfs_registered);
219 static void functionfs_closed_callback(struct ffs_data *ffs)
223 if (test_and_clear_bit(0, &gfs_registered))
224 usb_composite_unregister(&gfs_driver);
228 static int functionfs_check_dev_callback(const char *dev_name)
235 static int gfs_bind(struct usb_composite_dev *cdev)
241 if (WARN_ON(!gfs_ffs_data))
244 ret = gether_setup(cdev->gadget, gfs_hostaddr);
245 if (unlikely(ret < 0))
248 gfs_dev_desc.idVendor = cpu_to_le16(gfs_vendor_id);
249 gfs_dev_desc.idProduct = cpu_to_le16(gfs_product_id);
251 snprintf(gfs_manufacturer, sizeof gfs_manufacturer, "%s %s with %s",
252 init_utsname()->sysname, init_utsname()->release,
255 ret = usb_string_ids_tab(cdev, gfs_strings);
256 if (unlikely(ret < 0))
259 gfs_dev_desc.iManufacturer = gfs_strings[GFS_STRING_MANUFACTURER_IDX].id;
260 gfs_dev_desc.iProduct = gfs_strings[GFS_STRING_PRODUCT_IDX].id;
262 ret = functionfs_bind(gfs_ffs_data, cdev);
263 if (unlikely(ret < 0))
266 for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) {
267 struct gfs_configuration *c = gfs_configurations + i;
269 ret = GFS_STRING_FIRST_CONFIG_IDX + i;
270 c->c.label = gfs_strings[ret].s;
271 c->c.iConfiguration = gfs_strings[ret].id;
272 c->c.bind = gfs_do_config;
273 c->c.bConfigurationValue = 1 + i;
274 c->c.bmAttributes = USB_CONFIG_ATT_SELFPOWER;
276 ret = usb_add_config(cdev, &c->c);
277 if (unlikely(ret < 0))
284 functionfs_unbind(gfs_ffs_data);
292 static int gfs_unbind(struct usb_composite_dev *cdev)
296 /* We may have been called in an error recovery frem
297 * composite_bind() after gfs_unbind() failure so we need to
298 * check if gfs_ffs_data is not NULL since gfs_bind() handles
299 * all error recovery itself. I'd rather we werent called
300 * from composite on orror recovery, but what you're gonna
305 functionfs_unbind(gfs_ffs_data);
313 static int gfs_do_config(struct usb_configuration *c)
315 struct gfs_configuration *gc =
316 container_of(c, struct gfs_configuration, c);
319 if (WARN_ON(!gfs_ffs_data))
322 if (gadget_is_otg(c->cdev->gadget)) {
323 c->descriptors = gfs_otg_desc;
324 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
328 ret = gc->eth(c, gfs_hostaddr);
329 if (unlikely(ret < 0))
333 ret = functionfs_bind_config(c->cdev, c, gfs_ffs_data);
334 if (unlikely(ret < 0))
337 /* After previous do_configs there may be some invalid
338 * pointers in c->interface array. This happens every time
339 * a user space function with fewer interfaces than a user
340 * space function that was run before the new one is run. The
341 * compasit's set_config() assumes that if there is no more
342 * then MAX_CONFIG_INTERFACES interfaces in a configuration
343 * then there is a NULL pointer after the last interface in
344 * c->interface array. We need to make sure this is true. */
345 if (c->next_interface_id < ARRAY_SIZE(c->interface))
346 c->interface[c->next_interface_id] = NULL;
352 #ifdef CONFIG_USB_FUNCTIONFS_ETH
353 static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
355 return can_support_ecm(c->cdev->gadget)
356 ? ecm_bind_config(c, ethaddr)
357 : geth_bind_config(c, ethaddr);