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");
55 #define GFS_VENDOR_ID 0x1d6b /* Linux Foundation */
56 #define GFS_PRODUCT_ID 0x0105 /* FunctionFS Gadget */
58 static struct usb_device_descriptor gfs_dev_desc
= {
59 .bLength
= sizeof gfs_dev_desc
,
60 .bDescriptorType
= USB_DT_DEVICE
,
62 .bcdUSB
= cpu_to_le16(0x0200),
63 .bDeviceClass
= USB_CLASS_PER_INTERFACE
,
65 .idVendor
= cpu_to_le16(GFS_VENDOR_ID
),
66 .idProduct
= cpu_to_le16(GFS_PRODUCT_ID
),
69 module_param_named(bDeviceClass
, gfs_dev_desc
.bDeviceClass
, byte
, 0644);
70 MODULE_PARM_DESC(bDeviceClass
, "USB Device class");
71 module_param_named(bDeviceSubClass
, gfs_dev_desc
.bDeviceSubClass
, byte
, 0644);
72 MODULE_PARM_DESC(bDeviceSubClass
, "USB Device subclass");
73 module_param_named(bDeviceProtocol
, gfs_dev_desc
.bDeviceProtocol
, byte
, 0644);
74 MODULE_PARM_DESC(bDeviceProtocol
, "USB Device protocol");
78 static const struct usb_descriptor_header
*gfs_otg_desc
[] = {
79 (const struct usb_descriptor_header
*)
80 &(const struct usb_otg_descriptor
) {
81 .bLength
= sizeof(struct usb_otg_descriptor
),
82 .bDescriptorType
= USB_DT_OTG
,
85 * REVISIT SRP-only hardware is possible, although
86 * it would not be called "OTG" ...
88 .bmAttributes
= USB_OTG_SRP
| USB_OTG_HNP
,
94 /* string IDs are assigned dynamically */
96 static struct usb_string gfs_strings
[] = {
97 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
98 { .s
= "FunctionFS + RNDIS" },
100 #ifdef CONFIG_USB_FUNCTIONFS_ETH
101 { .s
= "FunctionFS + ECM" },
103 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
104 { .s
= "FunctionFS" },
106 { } /* end of list */
109 static struct usb_gadget_strings
*gfs_dev_strings
[] = {
110 &(struct usb_gadget_strings
) {
111 .language
= 0x0409, /* en-us */
112 .strings
= gfs_strings
,
119 struct gfs_configuration
{
120 struct usb_configuration c
;
121 int (*eth
)(struct usb_configuration
*c
, u8
*ethaddr
);
122 } gfs_configurations
[] = {
123 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
125 .eth
= rndis_bind_config
,
129 #ifdef CONFIG_USB_FUNCTIONFS_ETH
131 .eth
= eth_bind_config
,
135 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
142 static int gfs_bind(struct usb_composite_dev
*cdev
);
143 static int gfs_unbind(struct usb_composite_dev
*cdev
);
144 static int gfs_do_config(struct usb_configuration
*c
);
146 static struct usb_composite_driver gfs_driver
= {
148 .dev
= &gfs_dev_desc
,
149 .strings
= gfs_dev_strings
,
151 .unbind
= gfs_unbind
,
152 .iProduct
= DRIVER_DESC
,
156 static struct ffs_data
*gfs_ffs_data
;
157 static unsigned long gfs_registered
;
160 static int gfs_init(void)
164 return functionfs_init();
166 module_init(gfs_init
);
168 static void gfs_exit(void)
172 if (test_and_clear_bit(0, &gfs_registered
))
173 usb_composite_unregister(&gfs_driver
);
175 functionfs_cleanup();
177 module_exit(gfs_exit
);
180 static int functionfs_ready_callback(struct ffs_data
*ffs
)
186 if (WARN_ON(test_and_set_bit(0, &gfs_registered
)))
190 ret
= usb_composite_register(&gfs_driver
);
191 if (unlikely(ret
< 0))
192 clear_bit(0, &gfs_registered
);
196 static void functionfs_closed_callback(struct ffs_data
*ffs
)
200 if (test_and_clear_bit(0, &gfs_registered
))
201 usb_composite_unregister(&gfs_driver
);
205 static int functionfs_check_dev_callback(const char *dev_name
)
212 static int gfs_bind(struct usb_composite_dev
*cdev
)
218 if (WARN_ON(!gfs_ffs_data
))
221 ret
= gether_setup(cdev
->gadget
, gfs_hostaddr
);
222 if (unlikely(ret
< 0))
225 ret
= usb_string_ids_tab(cdev
, gfs_strings
);
226 if (unlikely(ret
< 0))
229 ret
= functionfs_bind(gfs_ffs_data
, cdev
);
230 if (unlikely(ret
< 0))
233 for (i
= 0; i
< ARRAY_SIZE(gfs_configurations
); ++i
) {
234 struct gfs_configuration
*c
= gfs_configurations
+ i
;
236 c
->c
.label
= gfs_strings
[i
].s
;
237 c
->c
.iConfiguration
= gfs_strings
[i
].id
;
238 c
->c
.bind
= gfs_do_config
;
239 c
->c
.bConfigurationValue
= 1 + i
;
240 c
->c
.bmAttributes
= USB_CONFIG_ATT_SELFPOWER
;
242 ret
= usb_add_config(cdev
, &c
->c
);
243 if (unlikely(ret
< 0))
250 functionfs_unbind(gfs_ffs_data
);
258 static int gfs_unbind(struct usb_composite_dev
*cdev
)
263 * We may have been called in an error recovery from
264 * composite_bind() after gfs_unbind() failure so we need to
265 * check if gfs_ffs_data is not NULL since gfs_bind() handles
266 * all error recovery itself. I'd rather we werent called
267 * from composite on orror recovery, but what you're gonna
272 functionfs_unbind(gfs_ffs_data
);
280 static int gfs_do_config(struct usb_configuration
*c
)
282 struct gfs_configuration
*gc
=
283 container_of(c
, struct gfs_configuration
, c
);
286 if (WARN_ON(!gfs_ffs_data
))
289 if (gadget_is_otg(c
->cdev
->gadget
)) {
290 c
->descriptors
= gfs_otg_desc
;
291 c
->bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
295 ret
= gc
->eth(c
, gfs_hostaddr
);
296 if (unlikely(ret
< 0))
300 ret
= functionfs_bind_config(c
->cdev
, c
, gfs_ffs_data
);
301 if (unlikely(ret
< 0))
305 * After previous do_configs there may be some invalid
306 * pointers in c->interface array. This happens every time
307 * a user space function with fewer interfaces than a user
308 * space function that was run before the new one is run. The
309 * compasit's set_config() assumes that if there is no more
310 * then MAX_CONFIG_INTERFACES interfaces in a configuration
311 * then there is a NULL pointer after the last interface in
312 * c->interface array. We need to make sure this is true.
314 if (c
->next_interface_id
< ARRAY_SIZE(c
->interface
))
315 c
->interface
[c
->next_interface_id
] = NULL
;
321 #ifdef CONFIG_USB_FUNCTIONFS_ETH
323 static int eth_bind_config(struct usb_configuration
*c
, u8 ethaddr
[ETH_ALEN
])
325 return can_support_ecm(c
->cdev
->gadget
)
326 ? ecm_bind_config(c
, ethaddr
)
327 : geth_bind_config(c
, ethaddr
);