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
37 # define pr_vdebug pr_debug
38 # define ffs_dump_mem(prefix, ptr, len) \
39 print_hex_dump_bytes(pr_fmt(prefix ": "), DUMP_PREFIX_NONE, ptr, len)
41 # define pr_vdebug(...) do { } while (0)
42 # define ffs_dump_mem(prefix, ptr, len) do { } while (0)
45 #define ENTER() pr_vdebug("%s()\n", __func__)
160 #define FFS_SETUP_STATE(ffs) \
161 ((enum ffs_setup_state)cmpxchg(&(ffs)->setup_state, \
162 FFS_SETUP_CANCELED, FFS_NO_SETUP))
177 #define FFS_FL_CALL_CLOSED_CALLBACK 0
178 #define FFS_FL_BOUND 1
248 __ffs_data_got_strings(
struct ffs_data *ffs,
char *data,
size_t len);
262 short *interfaces_nums;
268 static struct ffs_function *ffs_func_from_usb(
struct usb_function *
f)
273 static void ffs_func_free(
struct ffs_function *
func);
275 static void ffs_func_eps_disable(
struct ffs_function *
func);
282 static int ffs_func_set_alt(
struct usb_function *,
unsigned,
unsigned);
290 static int ffs_func_revmap_ep(
struct ffs_function *
func,
u8 num);
291 static int ffs_func_revmap_intf(
struct ffs_function *
func,
u8 intf);
327 static void ffs_epfiles_destroy(
struct ffs_epfile *epfiles,
unsigned count);
332 struct dentry **dentry_p);
337 static int ffs_mutex_lock(
struct mutex *
mutex,
unsigned nonblock)
339 static
char *ffs_prepare_buffer(
const char * __user
buf,
size_t len)
352 static int __ffs_ep0_queue_wait(
struct ffs_data *
ffs,
char *
data,
size_t len)
359 spin_unlock_irq(&ffs->
ev.waitq.lock);
370 req->
buf = (
void *)0xDEADBABE;
380 usb_ep_dequeue(ffs->
gadget->ep0, req);
388 static int __ffs_ep0_stall(
struct ffs_data *ffs)
390 if (ffs->
ev.can_stall) {
392 usb_ep_set_halt(ffs->
gadget->ep0);
402 size_t len, loff_t *
ptr)
420 switch (ffs->
state) {
429 data = ffs_prepare_buffer(buf, len);
438 ret = __ffs_data_got_descs(ffs, data, len);
446 ret = __ffs_data_got_strings(ffs, data, len);
450 ret = ffs_epfiles_create(ffs);
459 ret = functionfs_ready_callback(ffs);
476 spin_lock_irq(&ffs->
ev.waitq.lock);
492 spin_unlock_irq(&ffs->
ev.waitq.lock);
493 ret = __ffs_ep0_stall(ffs);
500 spin_unlock_irq(&ffs->
ev.waitq.lock);
502 data = ffs_prepare_buffer(buf, len);
508 spin_lock_irq(&ffs->
ev.waitq.lock);
525 spin_unlock_irq(&ffs->
ev.waitq.lock);
528 ret = __ffs_ep0_queue_wait(ffs, data, len);
542 static ssize_t __ffs_ep0_read_events(
struct ffs_data *ffs,
char __user *buf,
562 if (n < ffs->ev.count) {
565 ffs->
ev.count *
sizeof *ffs->
ev.types);
570 spin_unlock_irq(&ffs->
ev.waitq.lock);
577 static ssize_t ffs_ep0_read(
struct file *file,
char __user *buf,
578 size_t len, loff_t *ptr)
606 spin_lock_irq(&ffs->
ev.waitq.lock);
631 return __ffs_ep0_read_events(ffs, buf,
632 min(n, (
size_t)ffs->
ev.count));
636 spin_unlock_irq(&ffs->
ev.waitq.lock);
637 ret = __ffs_ep0_stall(ffs);
643 spin_unlock_irq(&ffs->
ev.waitq.lock);
653 spin_lock_irq(&ffs->
ev.waitq.lock);
662 ret = __ffs_ep0_queue_wait(ffs, data, len);
672 spin_unlock_irq(&ffs->
ev.waitq.lock);
679 static int ffs_ep0_open(
struct inode *
inode,
struct file *file)
689 ffs_data_opened(ffs);
694 static int ffs_ep0_release(
struct inode *inode,
struct file *file)
700 ffs_data_closed(ffs);
705 static long ffs_ep0_ioctl(
struct file *file,
unsigned code,
unsigned long value)
714 struct ffs_function *
func = ffs->
func;
715 ret = func ? ffs_func_revmap_intf(func, value) : -
ENODEV;
716 }
else if (gadget && gadget->
ops->ioctl) {
717 ret = gadget->
ops->ioctl(gadget, code, value);
729 .open = ffs_ep0_open,
730 .write = ffs_ep0_write,
731 .read = ffs_ep0_read,
732 .release = ffs_ep0_release,
733 .unlocked_ioctl = ffs_ep0_ioctl,
749 static ssize_t ffs_epfile_io(
struct file *file,
750 char __user *buf,
size_t len,
int read)
760 spin_unlock_irq(&epfile->
ffs->eps_lock);
779 (ep = epfile->
ep))) {
786 halt = !read == !epfile->
in;
787 if (halt && epfile->
isoc) {
793 if (!halt && !data) {
806 ret = ffs_mutex_lock(&epfile->
mutex,
815 spin_lock_irq(&epfile->
ffs->eps_lock);
826 usb_ep_set_halt(ep->
ep);
827 spin_unlock_irq(&epfile->
ffs->eps_lock);
835 req->
complete = ffs_epfile_io_complete;
841 spin_unlock_irq(&epfile->
ffs->eps_lock);
847 usb_ep_dequeue(ep->
ep, req);
850 if (read && ret > 0 &&
863 ffs_epfile_write(
struct file *file,
const char __user *buf,
size_t len,
868 return ffs_epfile_io(file, (
char __user *)buf, len, 0);
872 ffs_epfile_read(
struct file *file,
char __user *buf,
size_t len, loff_t *ptr)
876 return ffs_epfile_io(file, buf, len, 1);
880 ffs_epfile_open(
struct inode *inode,
struct file *file)
890 ffs_data_opened(epfile->
ffs);
896 ffs_epfile_release(
struct inode *inode,
struct file *file)
902 ffs_data_closed(epfile->
ffs);
907 static long ffs_epfile_ioctl(
struct file *file,
unsigned code,
918 spin_lock_irq(&epfile->
ffs->eps_lock);
922 ret = usb_ep_fifo_status(epfile->
ep->ep);
925 usb_ep_fifo_flush(epfile->
ep->ep);
929 ret = usb_ep_clear_halt(epfile->
ep->ep);
932 ret = epfile->
ep->num;
940 spin_unlock_irq(&epfile->
ffs->eps_lock);
949 .open = ffs_epfile_open,
950 .write = ffs_epfile_write,
951 .read = ffs_epfile_read,
952 .release = ffs_epfile_release,
953 .unlocked_ioctl = ffs_epfile_ioctl,
968 struct ffs_file_perms *perms)
980 inode->
i_mode = perms->mode;
981 inode->
i_uid = perms->uid;
982 inode->
i_gid = perms->gid;
997 static struct inode *ffs_sb_create_file(
struct super_block *sb,
998 const char *
name,
void *data,
1000 struct dentry **dentry_p)
1004 struct inode *
inode;
1012 inode = ffs_sb_make_inode(sb, data, fops,
NULL, &ffs->
file_perms);
1018 d_add(dentry, inode);
1032 struct ffs_file_perms perms;
1043 static int ffs_sb_fill(
struct super_block *sb,
void *_data,
int silent)
1046 struct inode *
inode;
1052 ffs = ffs_data_new();
1070 sb->
s_op = &ffs_sb_operations;
1075 inode = ffs_sb_make_inode(sb,
NULL,
1084 if (
unlikely(!ffs_sb_create_file(sb,
"ep0", ffs,
1085 &ffs_ep0_operations,
NULL)))
1098 if (!opts || !*opts)
1102 char *
end, *eq, *comma;
1103 unsigned long value;
1106 comma =
strchr(opts,
',');
1113 pr_err(
"'=' missing in %s\n", opts);
1120 if (
unlikely(*end !=
',' && *end != 0)) {
1121 pr_err(
"%s: invalid value: %s\n", opts, eq + 1);
1126 switch (eq - opts) {
1128 if (!
memcmp(opts,
"rmode", 5))
1130 else if (!
memcmp(opts,
"fmode", 5))
1137 if (!
memcmp(opts,
"mode", 4)) {
1146 if (!
memcmp(opts,
"uid", 3))
1147 data->
perms.uid = value;
1149 data->
perms.gid = value;
1156 pr_err(
"%s: invalid option\n", opts);
1171 static struct dentry *
1173 const char *dev_name,
void *opts)
1189 ret = ffs_fs_parse_opts(&data, opts);
1191 return ERR_PTR(ret);
1193 ffs_dev = functionfs_acquire_dev_callback(dev_name);
1194 if (IS_ERR(ffs_dev))
1203 functionfs_release_dev_callback(data.
ffs_data);
1215 functionfs_release_dev_callback(sb->
s_fs_info);
1222 .name =
"functionfs",
1223 .mount = ffs_fs_mount,
1224 .kill_sb = ffs_fs_kill_sb,
1230 static int functionfs_init(
void)
1238 pr_info(
"file system registered\n");
1240 pr_err(
"failed registering file system (%d)\n", ret);
1245 static void functionfs_cleanup(
void)
1256 static void ffs_data_clear(
struct ffs_data *ffs);
1257 static void ffs_data_reset(
struct ffs_data *ffs);
1259 static void ffs_data_get(
struct ffs_data *ffs)
1266 static void ffs_data_opened(
struct ffs_data *ffs)
1274 static void ffs_data_put(
struct ffs_data *ffs)
1279 pr_info(
"%s(): freeing\n", __func__);
1280 ffs_data_clear(ffs);
1281 BUG_ON(waitqueue_active(&ffs->
ev.waitq) ||
1288 static void ffs_data_closed(
struct ffs_data *ffs)
1294 ffs_data_reset(ffs);
1300 static struct ffs_data *ffs_data_new(
void)
1317 ffs->
ev.can_stall = 1;
1322 static void ffs_data_clear(
struct ffs_data *ffs)
1327 functionfs_closed_callback(ffs);
1339 static void ffs_data_reset(
struct ffs_data *ffs)
1343 ffs_data_clear(ffs);
1385 ffs->
ep0req->complete = ffs_ep0_complete;
1389 for (lang = ffs->
stringtabs; *lang; ++lang) {
1392 for (; str->
s; ++
id, ++
str)
1401 static void functionfs_unbind(
struct ffs_data *ffs)
1414 static int ffs_epfiles_create(
struct ffs_data *ffs)
1422 epfiles = kcalloc(count,
sizeof(*epfiles),
GFP_KERNEL);
1427 for (i = 1; i <=
count; ++
i, ++epfile) {
1433 &ffs_epfile_operations,
1435 ffs_epfiles_destroy(epfiles, i - 1);
1444 static void ffs_epfiles_destroy(
struct ffs_epfile *epfiles,
unsigned count)
1452 waitqueue_active(&epfile->
wait));
1467 struct ffs_function *
func;
1476 func->function.name =
"Function FS Gadget";
1479 func->function.bind = ffs_func_bind;
1480 func->function.unbind = ffs_func_unbind;
1481 func->function.set_alt = ffs_func_set_alt;
1482 func->function.disable = ffs_func_disable;
1483 func->function.setup = ffs_func_setup;
1484 func->function.suspend = ffs_func_suspend;
1485 func->function.resume = ffs_func_resume;
1488 func->gadget = cdev->
gadget;
1494 ffs_func_free(func);
1499 static void ffs_func_free(
struct ffs_function *func)
1501 struct ffs_ep *ep = func->eps;
1502 unsigned count = func->ffs->eps_count;
1503 unsigned long flags;
1510 if (ep->
ep && ep->
req)
1511 usb_ep_free_request(ep->
ep, ep->
req);
1515 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1517 ffs_data_put(func->ffs);
1529 static void ffs_func_eps_disable(
struct ffs_function *func)
1531 struct ffs_ep *ep = func->eps;
1533 unsigned count = func->ffs->eps_count;
1534 unsigned long flags;
1540 usb_ep_disable(ep->
ep);
1546 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1549 static int ffs_func_eps_enable(
struct ffs_function *func)
1552 struct ffs_ep *ep = func->eps;
1555 unsigned long flags;
1563 ep->
ep->driver_data = ep;
1565 ret = usb_ep_enable(ep->
ep);
1568 epfile->
in = usb_endpoint_dir_in(ds);
1569 epfile->
isoc = usb_endpoint_xfer_isoc(ds);
1579 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1603 static int __must_check ffs_do_desc(
char *data,
unsigned len,
1621 pr_vdebug(
"descriptor longer then available data\n");
1625 #define __entity_check_INTERFACE(val) 1
1626 #define __entity_check_STRING(val) (val)
1627 #define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1628 #define __entity(type, val) do { \
1629 pr_vdebug("entity " #type "(%02x)\n", (val)); \
1630 if (unlikely(!__entity_check_ ##type(val))) { \
1631 pr_vdebug("invalid entity's value\n"); \
1634 ret = entity(FFS_ ##type, &val, _ds, priv); \
1635 if (unlikely(ret < 0)) { \
1636 pr_debug("entity " #type "(%02x); ret = %d\n", \
1649 pr_vdebug(
"descriptor reserved for gadget: %d\n",
1656 if (length !=
sizeof *ds)
1688 pr_vdebug(
"interface association descriptor\n");
1689 if (length !=
sizeof *ds)
1711 pr_vdebug(
"invalid length: %d (descriptor %d)\n",
1717 #undef __entity_check_DESCRIPTOR
1718 #undef __entity_check_INTERFACE
1719 #undef __entity_check_STRING
1720 #undef __entity_check_ENDPOINT
1725 static int __must_check ffs_do_descs(
unsigned count,
char *data,
unsigned len,
1728 const unsigned _len = len;
1729 unsigned long num = 0;
1742 pr_debug(
"entity DESCRIPTOR(%02lx); ret = %d\n",
1750 ret = ffs_do_desc(data, len, entity, priv);
1752 pr_debug(
"%s returns %d\n", __func__, ret);
1803 static int __ffs_data_got_descs(
struct ffs_data *ffs,
1804 char *
const _data,
size_t len)
1807 int fs_len, ret = -
EINVAL;
1818 if (!fs_count && !hs_count)
1825 fs_len = ffs_do_descs(fs_count, data, len,
1826 __ffs_data_do_entity, ffs);
1839 ret = ffs_do_descs(hs_count, data, len,
1840 __ffs_data_do_entity, ffs);
1865 static int __ffs_data_got_strings(
struct ffs_data *ffs,
1866 char *
const _data,
size_t len)
1871 const char *data = _data;
1882 if (
unlikely(!str_count != !lang_count))
1887 if (
unlikely(str_count < needed_count))
1894 if (!needed_count) {
1904 struct usb_string strings[lang_count*(needed_count+1)];
1914 stringtabs =
d->stringtabs;
1918 *stringtabs++ = t++;
1922 stringtabs =
d->stringtabs;
1933 unsigned needed = needed_count;
1937 t->
language = get_unaligned_le16(data);
1969 }
while (--str_count);
1975 }
while (--lang_count);
1997 static void __ffs_event_add(
struct ffs_data *ffs,
2039 u8 *ev = ffs->
ev.types, *
out = ev;
2040 unsigned n = ffs->
ev.count;
2041 for (;
n; --
n, ++ev)
2042 if ((*ev == rem_type1 || *ev == rem_type2) == neg)
2046 ffs->
ev.count =
out - ffs->
ev.types;
2050 ffs->
ev.types[ffs->
ev.count++] =
type;
2054 static void ffs_event_add(
struct ffs_data *ffs,
2057 unsigned long flags;
2059 __ffs_event_add(ffs, type);
2060 spin_unlock_irqrestore(&ffs->
ev.waitq.lock, flags);
2071 struct ffs_function *func =
priv;
2078 const int isHS = func->function.hs_descriptors !=
NULL;
2085 func->function.hs_descriptors[(
long)valuep] = desc;
2087 func->function.descriptors[(
long)valuep] = desc;
2093 ffs_ep = func->eps +
idx;
2096 pr_vdebug(
"two %sspeed descriptors for EP %d\n",
2097 isHS ?
"high" :
"full",
2136 struct ffs_function *func =
priv;
2148 if (func->interfaces_nums[idx] < 0) {
2152 func->interfaces_nums[
idx] =
id;
2154 newValue = func->interfaces_nums[
idx];
2159 newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
2176 descs = func->eps[
idx].descs;
2182 pr_vdebug(
"%02x -> %02x\n", *valuep, newValue);
2190 struct ffs_function *func = ffs_func_from_usb(f);
2193 const int full = !!func->ffs->fs_descs_count;
2194 const int high = gadget_is_dualspeed(func->gadget) &&
2195 func->ffs->hs_descs_count;
2223 memset(data->eps, 0,
sizeof data->eps);
2225 memset(data->inums, 0xff,
sizeof data->inums);
2227 data->eps[
ret].num = -1;
2230 func->eps = data->eps;
2231 func->interfaces_nums = data->inums;
2239 func->function.descriptors = data->fs_descs;
2242 sizeof data->raw_descs,
2243 __ffs_func_bind_do_descs, func);
2251 func->function.hs_descriptors = data->hs_descs;
2253 data->raw_descs + ret,
2254 (
sizeof data->raw_descs) - ret,
2255 __ffs_func_bind_do_descs, func);
2265 data->raw_descs,
sizeof data->raw_descs,
2266 __ffs_func_bind_do_nums, func);
2285 struct ffs_function *func = ffs_func_from_usb(f);
2290 if (ffs->
func == func) {
2291 ffs_func_eps_disable(func);
2297 ffs_func_free(func);
2303 struct ffs_function *func = ffs_func_from_usb(f);
2307 if (alt != (
unsigned)-1) {
2308 intf = ffs_func_revmap_intf(func, interface);
2314 ffs_func_eps_disable(ffs->
func);
2319 if (alt == (
unsigned)-1) {
2326 ret = ffs_func_eps_enable(func);
2334 ffs_func_set_alt(f, 0, (
unsigned)-1);
2340 struct ffs_function *func = ffs_func_from_usb(f);
2342 unsigned long flags;
2383 ffs->
ev.setup = *creq;
2386 spin_unlock_irqrestore(&ffs->
ev.waitq.lock, flags);
2406 static int ffs_func_revmap_ep(
struct ffs_function *func,
u8 num)
2409 return num ? num : -
EDOM;
2412 static int ffs_func_revmap_intf(
struct ffs_function *func,
u8 intf)
2414 short *nums = func->interfaces_nums;
2415 unsigned count = func->ffs->interfaces_count;
2418 if (*nums >= 0 && *nums == intf)
2419 return nums - func->interfaces_nums;
2428 static int ffs_mutex_lock(
struct mutex *
mutex,
unsigned nonblock)
2435 static char *ffs_prepare_buffer(
const char * __user buf,
size_t len)