23 #include <linux/export.h>
24 #include <linux/hid.h>
25 #include <asm/unaligned.h>
28 #include <linux/usb/functionfs.h>
31 #define FUNCTIONFS_MAGIC 0xa647361
38 # define pr_vdebug pr_debug
40 # define ffs_dump_mem(prefix, ptr, len) \
41 print_hex_dump_bytes(pr_fmt(prefix ": "), DUMP_PREFIX_NONE, ptr, len)
44 # define pr_vdebug(...) do { } while (0)
46 # define ffs_dump_mem(prefix, ptr, len) do { } while (0)
49 #define ENTER() pr_vdebug("%s()\n", __func__)
164 #define FFS_SETUP_STATE(ffs) \
165 ((enum ffs_setup_state)cmpxchg(&(ffs)->setup_state, \
166 FFS_SETUP_CANCELED, FFS_NO_SETUP))
171 unsigned short count;
181 #define FFS_FL_CALL_CLOSED_CALLBACK 0
182 #define FFS_FL_BOUND 1
185 struct ffs_function *
func;
211 unsigned short _pad1;
225 struct ffs_file_perms {
252 __ffs_data_got_strings(
struct ffs_data *ffs,
char *data,
size_t len);
266 short *interfaces_nums;
272 static struct ffs_function *ffs_func_from_usb(
struct usb_function *
f)
277 static void ffs_func_free(
struct ffs_function *
func);
279 static void ffs_func_eps_disable(
struct ffs_function *
func);
286 static int ffs_func_set_alt(
struct usb_function *,
unsigned,
unsigned);
294 static int ffs_func_revmap_ep(
struct ffs_function *
func,
u8 num);
295 static int ffs_func_revmap_intf(
struct ffs_function *
func,
u8 intf);
331 static void ffs_epfiles_destroy(
struct ffs_epfile *epfiles,
unsigned count);
336 struct dentry **dentry_p);
341 static int ffs_mutex_lock(
struct mutex *
mutex,
unsigned nonblock)
343 static
char *ffs_prepare_buffer(
const char __user *
buf,
size_t len)
356 static int __ffs_ep0_queue_wait(
struct ffs_data *
ffs,
char *
data,
size_t len)
363 spin_unlock_irq(&ffs->
ev.waitq.lock);
374 req->
buf = (
void *)0xDEADBABE;
384 usb_ep_dequeue(ffs->
gadget->ep0, req);
392 static int __ffs_ep0_stall(
struct ffs_data *ffs)
394 if (ffs->
ev.can_stall) {
396 usb_ep_set_halt(ffs->
gadget->ep0);
406 size_t len, loff_t *
ptr)
424 switch (ffs->
state) {
433 data = ffs_prepare_buffer(buf, len);
442 ret = __ffs_data_got_descs(ffs, data, len);
450 ret = __ffs_data_got_strings(ffs, data, len);
454 ret = ffs_epfiles_create(ffs);
463 ret = functionfs_ready_callback(ffs);
480 spin_lock_irq(&ffs->
ev.waitq.lock);
496 spin_unlock_irq(&ffs->
ev.waitq.lock);
497 ret = __ffs_ep0_stall(ffs);
504 spin_unlock_irq(&ffs->
ev.waitq.lock);
506 data = ffs_prepare_buffer(buf, len);
512 spin_lock_irq(&ffs->
ev.waitq.lock);
529 spin_unlock_irq(&ffs->
ev.waitq.lock);
532 ret = __ffs_ep0_queue_wait(ffs, data, len);
546 static ssize_t __ffs_ep0_read_events(
struct ffs_data *ffs,
char __user *buf,
566 if (n < ffs->ev.count) {
569 ffs->
ev.count *
sizeof *ffs->
ev.types);
574 spin_unlock_irq(&ffs->
ev.waitq.lock);
581 static ssize_t ffs_ep0_read(
struct file *file,
char __user *buf,
582 size_t len, loff_t *ptr)
610 spin_lock_irq(&ffs->
ev.waitq.lock);
635 return __ffs_ep0_read_events(ffs, buf,
636 min(n, (
size_t)ffs->
ev.count));
640 spin_unlock_irq(&ffs->
ev.waitq.lock);
641 ret = __ffs_ep0_stall(ffs);
647 spin_unlock_irq(&ffs->
ev.waitq.lock);
657 spin_lock_irq(&ffs->
ev.waitq.lock);
666 ret = __ffs_ep0_queue_wait(ffs, data, len);
676 spin_unlock_irq(&ffs->
ev.waitq.lock);
683 static int ffs_ep0_open(
struct inode *
inode,
struct file *file)
693 ffs_data_opened(ffs);
698 static int ffs_ep0_release(
struct inode *inode,
struct file *file)
704 ffs_data_closed(ffs);
709 static long ffs_ep0_ioctl(
struct file *file,
unsigned code,
unsigned long value)
718 struct ffs_function *
func = ffs->
func;
719 ret = func ? ffs_func_revmap_intf(func, value) : -
ENODEV;
720 }
else if (gadget && gadget->
ops->ioctl) {
721 ret = gadget->
ops->ioctl(gadget, code, value);
733 .open = ffs_ep0_open,
734 .write = ffs_ep0_write,
735 .read = ffs_ep0_read,
736 .release = ffs_ep0_release,
737 .unlocked_ioctl = ffs_ep0_ioctl,
753 static ssize_t ffs_epfile_io(
struct file *file,
754 char __user *buf,
size_t len,
int read)
764 spin_unlock_irq(&epfile->
ffs->eps_lock);
783 (ep = epfile->
ep))) {
790 halt = !read == !epfile->
in;
791 if (halt && epfile->
isoc) {
797 if (!halt && !data) {
810 ret = ffs_mutex_lock(&epfile->
mutex,
819 spin_lock_irq(&epfile->
ffs->eps_lock);
830 usb_ep_set_halt(ep->
ep);
831 spin_unlock_irq(&epfile->
ffs->eps_lock);
839 req->
complete = ffs_epfile_io_complete;
845 spin_unlock_irq(&epfile->
ffs->eps_lock);
851 usb_ep_dequeue(ep->
ep, req);
854 if (read && ret > 0 &&
867 ffs_epfile_write(
struct file *file,
const char __user *buf,
size_t len,
872 return ffs_epfile_io(file, (
char __user *)buf, len, 0);
876 ffs_epfile_read(
struct file *file,
char __user *buf,
size_t len, loff_t *ptr)
880 return ffs_epfile_io(file, buf, len, 1);
884 ffs_epfile_open(
struct inode *inode,
struct file *file)
894 ffs_data_opened(epfile->
ffs);
900 ffs_epfile_release(
struct inode *inode,
struct file *file)
906 ffs_data_closed(epfile->
ffs);
911 static long ffs_epfile_ioctl(
struct file *file,
unsigned code,
922 spin_lock_irq(&epfile->
ffs->eps_lock);
926 ret = usb_ep_fifo_status(epfile->
ep->ep);
929 usb_ep_fifo_flush(epfile->
ep->ep);
933 ret = usb_ep_clear_halt(epfile->
ep->ep);
936 ret = epfile->
ep->num;
944 spin_unlock_irq(&epfile->
ffs->eps_lock);
953 .open = ffs_epfile_open,
954 .write = ffs_epfile_write,
955 .read = ffs_epfile_read,
956 .release = ffs_epfile_release,
957 .unlocked_ioctl = ffs_epfile_ioctl,
972 struct ffs_file_perms *perms)
984 inode->
i_mode = perms->mode;
985 inode->
i_uid = perms->uid;
986 inode->
i_gid = perms->gid;
1001 static struct inode *ffs_sb_create_file(
struct super_block *sb,
1002 const char *
name,
void *data,
1004 struct dentry **dentry_p)
1008 struct inode *
inode;
1016 inode = ffs_sb_make_inode(sb, data, fops,
NULL, &ffs->
file_perms);
1022 d_add(dentry, inode);
1036 struct ffs_file_perms perms;
1047 static int ffs_sb_fill(
struct super_block *sb,
void *_data,
int silent)
1050 struct inode *
inode;
1056 ffs = ffs_data_new();
1074 sb->
s_op = &ffs_sb_operations;
1079 inode = ffs_sb_make_inode(sb,
NULL,
1088 if (
unlikely(!ffs_sb_create_file(sb,
"ep0", ffs,
1089 &ffs_ep0_operations,
NULL)))
1102 if (!opts || !*opts)
1106 char *
end, *eq, *comma;
1107 unsigned long value;
1110 comma =
strchr(opts,
',');
1117 pr_err(
"'=' missing in %s\n", opts);
1124 if (
unlikely(*end !=
',' && *end != 0)) {
1125 pr_err(
"%s: invalid value: %s\n", opts, eq + 1);
1130 switch (eq - opts) {
1132 if (!
memcmp(opts,
"rmode", 5))
1134 else if (!
memcmp(opts,
"fmode", 5))
1141 if (!
memcmp(opts,
"mode", 4)) {
1150 if (!
memcmp(opts,
"uid", 3)) {
1152 if (!uid_valid(data->
perms.uid)) {
1153 pr_err(
"%s: unmapped value: %lu\n", opts, value);
1157 else if (!
memcmp(opts,
"gid", 3))
1159 if (!gid_valid(data->
perms.gid)) {
1160 pr_err(
"%s: unmapped value: %lu\n", opts, value);
1169 pr_err(
"%s: invalid option\n", opts);
1184 static struct dentry *
1186 const char *dev_name,
void *opts)
1202 ret = ffs_fs_parse_opts(&data, opts);
1204 return ERR_PTR(ret);
1206 ffs_dev = functionfs_acquire_dev_callback(dev_name);
1207 if (IS_ERR(ffs_dev))
1216 functionfs_release_dev_callback(data.
ffs_data);
1228 functionfs_release_dev_callback(sb->
s_fs_info);
1235 .name =
"functionfs",
1236 .mount = ffs_fs_mount,
1237 .kill_sb = ffs_fs_kill_sb,
1243 static int functionfs_init(
void)
1251 pr_info(
"file system registered\n");
1253 pr_err(
"failed registering file system (%d)\n", ret);
1258 static void functionfs_cleanup(
void)
1269 static void ffs_data_clear(
struct ffs_data *ffs);
1270 static void ffs_data_reset(
struct ffs_data *ffs);
1272 static void ffs_data_get(
struct ffs_data *ffs)
1279 static void ffs_data_opened(
struct ffs_data *ffs)
1287 static void ffs_data_put(
struct ffs_data *ffs)
1292 pr_info(
"%s(): freeing\n", __func__);
1293 ffs_data_clear(ffs);
1294 BUG_ON(waitqueue_active(&ffs->
ev.waitq) ||
1301 static void ffs_data_closed(
struct ffs_data *ffs)
1307 ffs_data_reset(ffs);
1313 static struct ffs_data *ffs_data_new(
void)
1330 ffs->
ev.can_stall = 1;
1335 static void ffs_data_clear(
struct ffs_data *ffs)
1340 functionfs_closed_callback(ffs);
1352 static void ffs_data_reset(
struct ffs_data *ffs)
1356 ffs_data_clear(ffs);
1398 ffs->
ep0req->complete = ffs_ep0_complete;
1402 for (lang = ffs->
stringtabs; *lang; ++lang) {
1405 for (; str->
s; ++
id, ++
str)
1414 static void functionfs_unbind(
struct ffs_data *ffs)
1427 static int ffs_epfiles_create(
struct ffs_data *ffs)
1435 epfiles = kcalloc(count,
sizeof(*epfiles),
GFP_KERNEL);
1440 for (i = 1; i <=
count; ++
i, ++epfile) {
1446 &ffs_epfile_operations,
1448 ffs_epfiles_destroy(epfiles, i - 1);
1457 static void ffs_epfiles_destroy(
struct ffs_epfile *epfiles,
unsigned count)
1465 waitqueue_active(&epfile->
wait));
1480 struct ffs_function *
func;
1489 func->function.name =
"Function FS Gadget";
1492 func->function.bind = ffs_func_bind;
1493 func->function.unbind = ffs_func_unbind;
1494 func->function.set_alt = ffs_func_set_alt;
1495 func->function.disable = ffs_func_disable;
1496 func->function.setup = ffs_func_setup;
1497 func->function.suspend = ffs_func_suspend;
1498 func->function.resume = ffs_func_resume;
1501 func->gadget = cdev->
gadget;
1507 ffs_func_free(func);
1512 static void ffs_func_free(
struct ffs_function *func)
1514 struct ffs_ep *ep = func->eps;
1515 unsigned count = func->ffs->eps_count;
1516 unsigned long flags;
1523 if (ep->
ep && ep->
req)
1524 usb_ep_free_request(ep->
ep, ep->
req);
1528 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1530 ffs_data_put(func->ffs);
1542 static void ffs_func_eps_disable(
struct ffs_function *func)
1544 struct ffs_ep *ep = func->eps;
1546 unsigned count = func->ffs->eps_count;
1547 unsigned long flags;
1553 usb_ep_disable(ep->
ep);
1559 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1562 static int ffs_func_eps_enable(
struct ffs_function *func)
1565 struct ffs_ep *ep = func->eps;
1568 unsigned long flags;
1576 ep->
ep->driver_data = ep;
1578 ret = usb_ep_enable(ep->
ep);
1581 epfile->
in = usb_endpoint_dir_in(ds);
1582 epfile->
isoc = usb_endpoint_xfer_isoc(ds);
1592 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1616 static int __must_check ffs_do_desc(
char *data,
unsigned len,
1634 pr_vdebug(
"descriptor longer then available data\n");
1638 #define __entity_check_INTERFACE(val) 1
1639 #define __entity_check_STRING(val) (val)
1640 #define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1641 #define __entity(type, val) do { \
1642 pr_vdebug("entity " #type "(%02x)\n", (val)); \
1643 if (unlikely(!__entity_check_ ##type(val))) { \
1644 pr_vdebug("invalid entity's value\n"); \
1647 ret = entity(FFS_ ##type, &val, _ds, priv); \
1648 if (unlikely(ret < 0)) { \
1649 pr_debug("entity " #type "(%02x); ret = %d\n", \
1662 pr_vdebug(
"descriptor reserved for gadget: %d\n",
1669 if (length !=
sizeof *ds)
1701 pr_vdebug(
"interface association descriptor\n");
1702 if (length !=
sizeof *ds)
1724 pr_vdebug(
"invalid length: %d (descriptor %d)\n",
1730 #undef __entity_check_DESCRIPTOR
1731 #undef __entity_check_INTERFACE
1732 #undef __entity_check_STRING
1733 #undef __entity_check_ENDPOINT
1738 static int __must_check ffs_do_descs(
unsigned count,
char *data,
unsigned len,
1741 const unsigned _len = len;
1742 unsigned long num = 0;
1755 pr_debug(
"entity DESCRIPTOR(%02lx); ret = %d\n",
1763 ret = ffs_do_desc(data, len, entity, priv);
1765 pr_debug(
"%s returns %d\n", __func__, ret);
1816 static int __ffs_data_got_descs(
struct ffs_data *ffs,
1817 char *
const _data,
size_t len)
1820 int fs_len, ret = -
EINVAL;
1831 if (!fs_count && !hs_count)
1838 fs_len = ffs_do_descs(fs_count, data, len,
1839 __ffs_data_do_entity, ffs);
1852 ret = ffs_do_descs(hs_count, data, len,
1853 __ffs_data_do_entity, ffs);
1878 static int __ffs_data_got_strings(
struct ffs_data *ffs,
1879 char *
const _data,
size_t len)
1884 const char *data = _data;
1895 if (
unlikely(!str_count != !lang_count))
1900 if (
unlikely(str_count < needed_count))
1907 if (!needed_count) {
1917 struct usb_string strings[lang_count*(needed_count+1)];
1927 stringtabs =
d->stringtabs;
1931 *stringtabs++ = t++;
1935 stringtabs =
d->stringtabs;
1946 unsigned needed = needed_count;
1950 t->
language = get_unaligned_le16(data);
1982 }
while (--str_count);
1988 }
while (--lang_count);
2010 static void __ffs_event_add(
struct ffs_data *ffs,
2052 u8 *ev = ffs->
ev.types, *
out = ev;
2053 unsigned n = ffs->
ev.count;
2054 for (;
n; --
n, ++ev)
2055 if ((*ev == rem_type1 || *ev == rem_type2) == neg)
2059 ffs->
ev.count =
out - ffs->
ev.types;
2063 ffs->
ev.types[ffs->
ev.count++] =
type;
2067 static void ffs_event_add(
struct ffs_data *ffs,
2070 unsigned long flags;
2072 __ffs_event_add(ffs, type);
2073 spin_unlock_irqrestore(&ffs->
ev.waitq.lock, flags);
2084 struct ffs_function *func =
priv;
2091 const int isHS = func->function.hs_descriptors !=
NULL;
2098 func->function.hs_descriptors[(
long)valuep] = desc;
2100 func->function.descriptors[(
long)valuep] = desc;
2106 ffs_ep = func->eps +
idx;
2109 pr_vdebug(
"two %sspeed descriptors for EP %d\n",
2110 isHS ?
"high" :
"full",
2149 struct ffs_function *func =
priv;
2161 if (func->interfaces_nums[idx] < 0) {
2165 func->interfaces_nums[
idx] =
id;
2167 newValue = func->interfaces_nums[
idx];
2172 newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
2189 descs = func->eps[
idx].descs;
2195 pr_vdebug(
"%02x -> %02x\n", *valuep, newValue);
2203 struct ffs_function *func = ffs_func_from_usb(f);
2206 const int full = !!func->ffs->fs_descs_count;
2207 const int high = gadget_is_dualspeed(func->gadget) &&
2208 func->ffs->hs_descs_count;
2236 memset(data->eps, 0,
sizeof data->eps);
2238 memset(data->inums, 0xff,
sizeof data->inums);
2240 data->eps[
ret].num = -1;
2243 func->eps = data->eps;
2244 func->interfaces_nums = data->inums;
2252 func->function.descriptors = data->fs_descs;
2255 sizeof data->raw_descs,
2256 __ffs_func_bind_do_descs, func);
2264 func->function.hs_descriptors = data->hs_descs;
2266 data->raw_descs + ret,
2267 (
sizeof data->raw_descs) - ret,
2268 __ffs_func_bind_do_descs, func);
2278 data->raw_descs,
sizeof data->raw_descs,
2279 __ffs_func_bind_do_nums, func);
2298 struct ffs_function *func = ffs_func_from_usb(f);
2303 if (ffs->
func == func) {
2304 ffs_func_eps_disable(func);
2310 ffs_func_free(func);
2316 struct ffs_function *func = ffs_func_from_usb(f);
2320 if (alt != (
unsigned)-1) {
2321 intf = ffs_func_revmap_intf(func, interface);
2327 ffs_func_eps_disable(ffs->
func);
2332 if (alt == (
unsigned)-1) {
2339 ret = ffs_func_eps_enable(func);
2347 ffs_func_set_alt(f, 0, (
unsigned)-1);
2353 struct ffs_function *func = ffs_func_from_usb(f);
2355 unsigned long flags;
2396 ffs->
ev.setup = *creq;
2399 spin_unlock_irqrestore(&ffs->
ev.waitq.lock, flags);
2419 static int ffs_func_revmap_ep(
struct ffs_function *func,
u8 num)
2422 return num ? num : -
EDOM;
2425 static int ffs_func_revmap_intf(
struct ffs_function *func,
u8 intf)
2427 short *nums = func->interfaces_nums;
2428 unsigned count = func->ffs->interfaces_count;
2431 if (*nums >= 0 && *nums == intf)
2432 return nums - func->interfaces_nums;
2441 static int ffs_mutex_lock(
struct mutex *
mutex,
unsigned nonblock)
2448 static char *ffs_prepare_buffer(
const char __user *buf,
size_t len)