1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695 |
- /*
- * f_fs.c -- user mode file system API for USB composite function controllers
- *
- * Copyright (C) 2010 Samsung Electronics
- * Author: Michal Nazarewicz <mina86@mina86.com>
- *
- * Based on inode.c (GadgetFS) which was:
- * Copyright (C) 2003-2004 David Brownell
- * Copyright (C) 2003 Agilent Technologies
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- */
- /* #define DEBUG */
- /* #define VERBOSE_DEBUG */
- #include <linux/blkdev.h>
- #include <linux/pagemap.h>
- #include <linux/export.h>
- #include <linux/hid.h>
- #include <linux/module.h>
- #include <asm/unaligned.h>
- #include <linux/usb/composite.h>
- #include <linux/usb/functionfs.h>
- #include "u_fs.h"
- #include "configfs.h"
- #define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */
- /* Variable Length Array Macros **********************************************/
- #define vla_group(groupname) size_t groupname##__next = 0
- #define vla_group_size(groupname) groupname##__next
- #define vla_item(groupname, type, name, n) \
- size_t groupname##_##name##__offset = ({ \
- size_t align_mask = __alignof__(type) - 1; \
- size_t offset = (groupname##__next + align_mask) & ~align_mask;\
- size_t size = (n) * sizeof(type); \
- groupname##__next = offset + size; \
- offset; \
- })
- #define vla_item_with_sz(groupname, type, name, n) \
- size_t groupname##_##name##__sz = (n) * sizeof(type); \
- size_t groupname##_##name##__offset = ({ \
- size_t align_mask = __alignof__(type) - 1; \
- size_t offset = (groupname##__next + align_mask) & ~align_mask;\
- size_t size = groupname##_##name##__sz; \
- groupname##__next = offset + size; \
- offset; \
- })
- #define vla_ptr(ptr, groupname, name) \
- ((void *) ((char *)ptr + groupname##_##name##__offset))
- /* Reference counter handling */
- static void ffs_data_get(struct ffs_data *ffs);
- static void ffs_data_put(struct ffs_data *ffs);
- /* Creates new ffs_data object. */
- static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc));
- /* Opened counter handling. */
- static void ffs_data_opened(struct ffs_data *ffs);
- static void ffs_data_closed(struct ffs_data *ffs);
- /* Called with ffs->mutex held; take over ownership of data. */
- static int __must_check
- __ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len);
- static int __must_check
- __ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len);
- /* The function structure ***************************************************/
- struct ffs_ep;
- struct ffs_function {
- struct usb_configuration *conf;
- struct usb_gadget *gadget;
- struct ffs_data *ffs;
- struct ffs_ep *eps;
- u8 eps_revmap[16];
- short *interfaces_nums;
- struct usb_function function;
- };
- static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
- {
- return container_of(f, struct ffs_function, function);
- }
- static void ffs_func_eps_disable(struct ffs_function *func);
- static int __must_check ffs_func_eps_enable(struct ffs_function *func);
- static int ffs_func_bind(struct usb_configuration *,
- struct usb_function *);
- static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
- static void ffs_func_disable(struct usb_function *);
- static int ffs_func_setup(struct usb_function *,
- const struct usb_ctrlrequest *);
- static void ffs_func_suspend(struct usb_function *);
- static void ffs_func_resume(struct usb_function *);
- static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
- static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
- /* The endpoints structures *************************************************/
- struct ffs_ep {
- struct usb_ep *ep; /* P: ffs->eps_lock */
- struct usb_request *req; /* P: epfile->mutex */
- /* [0]: full speed, [1]: high speed */
- struct usb_endpoint_descriptor *descs[2];
- u8 num;
- int status; /* P: epfile->mutex */
- };
- struct ffs_epfile {
- /* Protects ep->ep and ep->req. */
- struct mutex mutex;
- wait_queue_head_t wait;
- struct ffs_data *ffs;
- struct ffs_ep *ep; /* P: ffs->eps_lock */
- struct dentry *dentry;
- char name[5];
- unsigned char in; /* P: ffs->eps_lock */
- unsigned char isoc; /* P: ffs->eps_lock */
- unsigned char _pad;
- };
- static int __must_check ffs_epfiles_create(struct ffs_data *ffs);
- static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
- static struct inode *__must_check
- ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
- const struct file_operations *fops,
- struct dentry **dentry_p);
- /* Devices management *******************************************************/
- DEFINE_MUTEX(ffs_lock);
- EXPORT_SYMBOL(ffs_lock);
- static struct ffs_dev *ffs_find_dev(const char *name);
- static int _ffs_name_dev(struct ffs_dev *dev, const char *name);
- static void *ffs_acquire_dev(const char *dev_name);
- static void ffs_release_dev(struct ffs_data *ffs_data);
- static int ffs_ready(struct ffs_data *ffs);
- static void ffs_closed(struct ffs_data *ffs);
- /* Misc helper functions ****************************************************/
- static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
- __attribute__((warn_unused_result, nonnull));
- static char *ffs_prepare_buffer(const char __user *buf, size_t len)
- __attribute__((warn_unused_result, nonnull));
- /* Control file aka ep0 *****************************************************/
- static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
- {
- struct ffs_data *ffs = req->context;
- complete_all(&ffs->ep0req_completion);
- }
- static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
- {
- struct usb_request *req = ffs->ep0req;
- int ret;
- req->zero = len < le16_to_cpu(ffs->ev.setup.wLength);
- spin_unlock_irq(&ffs->ev.waitq.lock);
- req->buf = data;
- req->length = len;
- /*
- * UDC layer requires to provide a buffer even for ZLP, but should
- * not use it at all. Let's provide some poisoned pointer to catch
- * possible bug in the driver.
- */
- if (req->buf == NULL)
- req->buf = (void *)0xDEADBABE;
- reinit_completion(&ffs->ep0req_completion);
- ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
- if (unlikely(ret < 0))
- return ret;
- ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
- if (unlikely(ret)) {
- usb_ep_dequeue(ffs->gadget->ep0, req);
- return -EINTR;
- }
- ffs->setup_state = FFS_NO_SETUP;
- return ffs->ep0req_status;
- }
- static int __ffs_ep0_stall(struct ffs_data *ffs)
- {
- if (ffs->ev.can_stall) {
- pr_vdebug("ep0 stall\n");
- usb_ep_set_halt(ffs->gadget->ep0);
- ffs->setup_state = FFS_NO_SETUP;
- return -EL2HLT;
- } else {
- pr_debug("bogus ep0 stall!\n");
- return -ESRCH;
- }
- }
- static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
- size_t len, loff_t *ptr)
- {
- struct ffs_data *ffs = file->private_data;
- ssize_t ret;
- char *data;
- ENTER();
- /* Fast check if setup was canceled */
- if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
- return -EIDRM;
- /* Acquire mutex */
- ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
- if (unlikely(ret < 0))
- return ret;
- /* Check state */
- switch (ffs->state) {
- case FFS_READ_DESCRIPTORS:
- case FFS_READ_STRINGS:
- /* Copy data */
- if (unlikely(len < 16)) {
- ret = -EINVAL;
- break;
- }
- data = ffs_prepare_buffer(buf, len);
- if (IS_ERR(data)) {
- ret = PTR_ERR(data);
- break;
- }
- /* Handle data */
- if (ffs->state == FFS_READ_DESCRIPTORS) {
- pr_info("read descriptors\n");
- ret = __ffs_data_got_descs(ffs, data, len);
- if (unlikely(ret < 0))
- break;
- ffs->state = FFS_READ_STRINGS;
- ret = len;
- } else {
- pr_info("read strings\n");
- ret = __ffs_data_got_strings(ffs, data, len);
- if (unlikely(ret < 0))
- break;
- ret = ffs_epfiles_create(ffs);
- if (unlikely(ret)) {
- ffs->state = FFS_CLOSING;
- break;
- }
- ffs->state = FFS_ACTIVE;
- mutex_unlock(&ffs->mutex);
- ret = ffs_ready(ffs);
- if (unlikely(ret < 0)) {
- ffs->state = FFS_CLOSING;
- return ret;
- }
- set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
- return len;
- }
- break;
- case FFS_ACTIVE:
- data = NULL;
- /*
- * We're called from user space, we can use _irq
- * rather then _irqsave
- */
- spin_lock_irq(&ffs->ev.waitq.lock);
- switch (FFS_SETUP_STATE(ffs)) {
- case FFS_SETUP_CANCELED:
- ret = -EIDRM;
- goto done_spin;
- case FFS_NO_SETUP:
- ret = -ESRCH;
- goto done_spin;
- case FFS_SETUP_PENDING:
- break;
- }
- /* FFS_SETUP_PENDING */
- if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
- spin_unlock_irq(&ffs->ev.waitq.lock);
- ret = __ffs_ep0_stall(ffs);
- break;
- }
- /* FFS_SETUP_PENDING and not stall */
- len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
- spin_unlock_irq(&ffs->ev.waitq.lock);
- data = ffs_prepare_buffer(buf, len);
- if (IS_ERR(data)) {
- ret = PTR_ERR(data);
- break;
- }
- spin_lock_irq(&ffs->ev.waitq.lock);
- /*
- * We are guaranteed to be still in FFS_ACTIVE state
- * but the state of setup could have changed from
- * FFS_SETUP_PENDING to FFS_SETUP_CANCELED so we need
- * to check for that. If that happened we copied data
- * from user space in vain but it's unlikely.
- *
- * For sure we are not in FFS_NO_SETUP since this is
- * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
- * transition can be performed and it's protected by
- * mutex.
- */
- if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
- ret = -EIDRM;
- done_spin:
- spin_unlock_irq(&ffs->ev.waitq.lock);
- } else {
- /* unlocks spinlock */
- ret = __ffs_ep0_queue_wait(ffs, data, len);
- }
- kfree(data);
- break;
- default:
- ret = -EBADFD;
- break;
- }
- mutex_unlock(&ffs->mutex);
- return ret;
- }
- static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
- size_t n)
- {
- /*
- * We are holding ffs->ev.waitq.lock and ffs->mutex and we need
- * to release them.
- */
- struct usb_functionfs_event events[n];
- unsigned i = 0;
- memset(events, 0, sizeof events);
- do {
- events[i].type = ffs->ev.types[i];
- if (events[i].type == FUNCTIONFS_SETUP) {
- events[i].u.setup = ffs->ev.setup;
- ffs->setup_state = FFS_SETUP_PENDING;
- }
- } while (++i < n);
- if (n < ffs->ev.count) {
- ffs->ev.count -= n;
- memmove(ffs->ev.types, ffs->ev.types + n,
- ffs->ev.count * sizeof *ffs->ev.types);
- } else {
- ffs->ev.count = 0;
- }
- spin_unlock_irq(&ffs->ev.waitq.lock);
- mutex_unlock(&ffs->mutex);
- return unlikely(__copy_to_user(buf, events, sizeof events))
- ? -EFAULT : sizeof events;
- }
- static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
- size_t len, loff_t *ptr)
- {
- struct ffs_data *ffs = file->private_data;
- char *data = NULL;
- size_t n;
- int ret;
- ENTER();
- /* Fast check if setup was canceled */
- if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
- return -EIDRM;
- /* Acquire mutex */
- ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
- if (unlikely(ret < 0))
- return ret;
- /* Check state */
- if (ffs->state != FFS_ACTIVE) {
- ret = -EBADFD;
- goto done_mutex;
- }
- /*
- * We're called from user space, we can use _irq rather then
- * _irqsave
- */
- spin_lock_irq(&ffs->ev.waitq.lock);
- switch (FFS_SETUP_STATE(ffs)) {
- case FFS_SETUP_CANCELED:
- ret = -EIDRM;
- break;
- case FFS_NO_SETUP:
- n = len / sizeof(struct usb_functionfs_event);
- if (unlikely(!n)) {
- ret = -EINVAL;
- break;
- }
- if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
- ret = -EAGAIN;
- break;
- }
- if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
- ffs->ev.count)) {
- ret = -EINTR;
- break;
- }
- return __ffs_ep0_read_events(ffs, buf,
- min(n, (size_t)ffs->ev.count));
- case FFS_SETUP_PENDING:
- if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
- spin_unlock_irq(&ffs->ev.waitq.lock);
- ret = __ffs_ep0_stall(ffs);
- goto done_mutex;
- }
- len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
- spin_unlock_irq(&ffs->ev.waitq.lock);
- if (likely(len)) {
- data = kmalloc(len, GFP_KERNEL);
- if (unlikely(!data)) {
- ret = -ENOMEM;
- goto done_mutex;
- }
- }
- spin_lock_irq(&ffs->ev.waitq.lock);
- /* See ffs_ep0_write() */
- if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
- ret = -EIDRM;
- break;
- }
- /* unlocks spinlock */
- ret = __ffs_ep0_queue_wait(ffs, data, len);
- if (likely(ret > 0) && unlikely(__copy_to_user(buf, data, len)))
- ret = -EFAULT;
- goto done_mutex;
- default:
- ret = -EBADFD;
- break;
- }
- spin_unlock_irq(&ffs->ev.waitq.lock);
- done_mutex:
- mutex_unlock(&ffs->mutex);
- kfree(data);
- return ret;
- }
- static int ffs_ep0_open(struct inode *inode, struct file *file)
- {
- struct ffs_data *ffs = inode->i_private;
- ENTER();
- if (unlikely(ffs->state == FFS_CLOSING))
- return -EBUSY;
- file->private_data = ffs;
- ffs_data_opened(ffs);
- return 0;
- }
- static int ffs_ep0_release(struct inode *inode, struct file *file)
- {
- struct ffs_data *ffs = file->private_data;
- ENTER();
- ffs_data_closed(ffs);
- return 0;
- }
- static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
- {
- struct ffs_data *ffs = file->private_data;
- struct usb_gadget *gadget = ffs->gadget;
- long ret;
- ENTER();
- if (code == FUNCTIONFS_INTERFACE_REVMAP) {
- struct ffs_function *func = ffs->func;
- ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
- } else if (gadget && gadget->ops->ioctl) {
- ret = gadget->ops->ioctl(gadget, code, value);
- } else {
- ret = -ENOTTY;
- }
- return ret;
- }
- static const struct file_operations ffs_ep0_operations = {
- .llseek = no_llseek,
- .open = ffs_ep0_open,
- .write = ffs_ep0_write,
- .read = ffs_ep0_read,
- .release = ffs_ep0_release,
- .unlocked_ioctl = ffs_ep0_ioctl,
- };
- /* "Normal" endpoints operations ********************************************/
- static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
- {
- ENTER();
- if (likely(req->context)) {
- struct ffs_ep *ep = _ep->driver_data;
- ep->status = req->status ? req->status : req->actual;
- complete(req->context);
- }
- }
- static ssize_t ffs_epfile_io(struct file *file,
- char __user *buf, size_t len, int read)
- {
- struct ffs_epfile *epfile = file->private_data;
- struct ffs_ep *ep;
- char *data = NULL;
- ssize_t ret, data_len;
- int halt;
- /* Are we still active? */
- if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) {
- ret = -ENODEV;
- goto error;
- }
- /* Wait for endpoint to be enabled */
- ep = epfile->ep;
- if (!ep) {
- if (file->f_flags & O_NONBLOCK) {
- ret = -EAGAIN;
- goto error;
- }
- ret = wait_event_interruptible(epfile->wait, (ep = epfile->ep));
- if (ret) {
- ret = -EINTR;
- goto error;
- }
- }
- /* Do we halt? */
- halt = !read == !epfile->in;
- if (halt && epfile->isoc) {
- ret = -EINVAL;
- goto error;
- }
- /* Allocate & copy */
- if (!halt) {
- /*
- * if we _do_ wait above, the epfile->ffs->gadget might be NULL
- * before the waiting completes, so do not assign to 'gadget' earlier
- */
- struct usb_gadget *gadget = epfile->ffs->gadget;
- /*
- * Controller may require buffer size to be aligned to
- * maxpacketsize of an out endpoint.
- */
- data_len = read ? usb_ep_align_maybe(gadget, ep->ep, len) : len;
- data = kmalloc(data_len, GFP_KERNEL);
- if (unlikely(!data))
- return -ENOMEM;
- if (!read && unlikely(copy_from_user(data, buf, len))) {
- ret = -EFAULT;
- goto error;
- }
- }
- /* We will be using request */
- ret = ffs_mutex_lock(&epfile->mutex, file->f_flags & O_NONBLOCK);
- if (unlikely(ret))
- goto error;
- spin_lock_irq(&epfile->ffs->eps_lock);
- if (epfile->ep != ep) {
- /* In the meantime, endpoint got disabled or changed. */
- ret = -ESHUTDOWN;
- spin_unlock_irq(&epfile->ffs->eps_lock);
- } else if (halt) {
- /* Halt */
- if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
- usb_ep_set_halt(ep->ep);
- spin_unlock_irq(&epfile->ffs->eps_lock);
- ret = -EBADMSG;
- } else {
- /* Fire the request */
- DECLARE_COMPLETION_ONSTACK(done);
- struct usb_request *req = ep->req;
- req->context = &done;
- req->complete = ffs_epfile_io_complete;
- req->buf = data;
- req->length = data_len;
- ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
- spin_unlock_irq(&epfile->ffs->eps_lock);
- if (unlikely(ret < 0)) {
- /* nop */
- } else if (unlikely(wait_for_completion_interruptible(&done))) {
- ret = -EINTR;
- usb_ep_dequeue(ep->ep, req);
- } else {
- /*
- * XXX We may end up silently droping data here.
- * Since data_len (i.e. req->length) may be bigger
- * than len (after being rounded up to maxpacketsize),
- * we may end up with more data then user space has
- * space for.
- */
- ret = ep->status;
- if (read && ret > 0 &&
- unlikely(copy_to_user(buf, data,
- min_t(size_t, ret, len))))
- ret = -EFAULT;
- }
- }
- mutex_unlock(&epfile->mutex);
- error:
- kfree(data);
- return ret;
- }
- static ssize_t
- ffs_epfile_write(struct file *file, const char __user *buf, size_t len,
- loff_t *ptr)
- {
- ENTER();
- return ffs_epfile_io(file, (char __user *)buf, len, 0);
- }
- static ssize_t
- ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr)
- {
- ENTER();
- return ffs_epfile_io(file, buf, len, 1);
- }
- static int
- ffs_epfile_open(struct inode *inode, struct file *file)
- {
- struct ffs_epfile *epfile = inode->i_private;
- ENTER();
- if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
- return -ENODEV;
- file->private_data = epfile;
- ffs_data_opened(epfile->ffs);
- return 0;
- }
- static int
- ffs_epfile_release(struct inode *inode, struct file *file)
- {
- struct ffs_epfile *epfile = inode->i_private;
- ENTER();
- ffs_data_closed(epfile->ffs);
- return 0;
- }
- static long ffs_epfile_ioctl(struct file *file, unsigned code,
- unsigned long value)
- {
- struct ffs_epfile *epfile = file->private_data;
- int ret;
- ENTER();
- if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
- return -ENODEV;
- spin_lock_irq(&epfile->ffs->eps_lock);
- if (likely(epfile->ep)) {
- switch (code) {
- case FUNCTIONFS_FIFO_STATUS:
- ret = usb_ep_fifo_status(epfile->ep->ep);
- break;
- case FUNCTIONFS_FIFO_FLUSH:
- usb_ep_fifo_flush(epfile->ep->ep);
- ret = 0;
- break;
- case FUNCTIONFS_CLEAR_HALT:
- ret = usb_ep_clear_halt(epfile->ep->ep);
- break;
- case FUNCTIONFS_ENDPOINT_REVMAP:
- ret = epfile->ep->num;
- break;
- default:
- ret = -ENOTTY;
- }
- } else {
- ret = -ENODEV;
- }
- spin_unlock_irq(&epfile->ffs->eps_lock);
- return ret;
- }
- static const struct file_operations ffs_epfile_operations = {
- .llseek = no_llseek,
- .open = ffs_epfile_open,
- .write = ffs_epfile_write,
- .read = ffs_epfile_read,
- .release = ffs_epfile_release,
- .unlocked_ioctl = ffs_epfile_ioctl,
- };
- /* File system and super block operations ***********************************/
- /*
- * Mounting the file system creates a controller file, used first for
- * function configuration then later for event monitoring.
- */
- static struct inode *__must_check
- ffs_sb_make_inode(struct super_block *sb, void *data,
- const struct file_operations *fops,
- const struct inode_operations *iops,
- struct ffs_file_perms *perms)
- {
- struct inode *inode;
- ENTER();
- inode = new_inode(sb);
- if (likely(inode)) {
- struct timespec current_time = CURRENT_TIME;
- inode->i_ino = get_next_ino();
- inode->i_mode = perms->mode;
- inode->i_uid = perms->uid;
- inode->i_gid = perms->gid;
- inode->i_atime = current_time;
- inode->i_mtime = current_time;
- inode->i_ctime = current_time;
- inode->i_private = data;
- if (fops)
- inode->i_fop = fops;
- if (iops)
- inode->i_op = iops;
- }
- return inode;
- }
- /* Create "regular" file */
- static struct inode *ffs_sb_create_file(struct super_block *sb,
- const char *name, void *data,
- const struct file_operations *fops,
- struct dentry **dentry_p)
- {
- struct ffs_data *ffs = sb->s_fs_info;
- struct dentry *dentry;
- struct inode *inode;
- ENTER();
- dentry = d_alloc_name(sb->s_root, name);
- if (unlikely(!dentry))
- return NULL;
- inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
- if (unlikely(!inode)) {
- dput(dentry);
- return NULL;
- }
- d_add(dentry, inode);
- if (dentry_p)
- *dentry_p = dentry;
- return inode;
- }
- /* Super block */
- static const struct super_operations ffs_sb_operations = {
- .statfs = simple_statfs,
- .drop_inode = generic_delete_inode,
- };
- struct ffs_sb_fill_data {
- struct ffs_file_perms perms;
- umode_t root_mode;
- const char *dev_name;
- struct ffs_data *ffs_data;
- };
- static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
- {
- struct ffs_sb_fill_data *data = _data;
- struct inode *inode;
- struct ffs_data *ffs = data->ffs_data;
- ENTER();
- ffs->sb = sb;
- data->ffs_data = NULL;
- sb->s_fs_info = ffs;
- sb->s_blocksize = PAGE_CACHE_SIZE;
- sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
- sb->s_magic = FUNCTIONFS_MAGIC;
- sb->s_op = &ffs_sb_operations;
- sb->s_time_gran = 1;
- /* Root inode */
- data->perms.mode = data->root_mode;
- inode = ffs_sb_make_inode(sb, NULL,
- &simple_dir_operations,
- &simple_dir_inode_operations,
- &data->perms);
- sb->s_root = d_make_root(inode);
- if (unlikely(!sb->s_root))
- return -ENOMEM;
- /* EP0 file */
- if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
- &ffs_ep0_operations, NULL)))
- return -ENOMEM;
- return 0;
- }
- static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
- {
- ENTER();
- if (!opts || !*opts)
- return 0;
- for (;;) {
- unsigned long value;
- char *eq, *comma;
- /* Option limit */
- comma = strchr(opts, ',');
- if (comma)
- *comma = 0;
- /* Value limit */
- eq = strchr(opts, '=');
- if (unlikely(!eq)) {
- pr_err("'=' missing in %s\n", opts);
- return -EINVAL;
- }
- *eq = 0;
- /* Parse value */
- if (kstrtoul(eq + 1, 0, &value)) {
- pr_err("%s: invalid value: %s\n", opts, eq + 1);
- return -EINVAL;
- }
- /* Interpret option */
- switch (eq - opts) {
- case 5:
- if (!memcmp(opts, "rmode", 5))
- data->root_mode = (value & 0555) | S_IFDIR;
- else if (!memcmp(opts, "fmode", 5))
- data->perms.mode = (value & 0666) | S_IFREG;
- else
- goto invalid;
- break;
- case 4:
- if (!memcmp(opts, "mode", 4)) {
- data->root_mode = (value & 0555) | S_IFDIR;
- data->perms.mode = (value & 0666) | S_IFREG;
- } else {
- goto invalid;
- }
- break;
- case 3:
- if (!memcmp(opts, "uid", 3)) {
- data->perms.uid = make_kuid(current_user_ns(), value);
- if (!uid_valid(data->perms.uid)) {
- pr_err("%s: unmapped value: %lu\n", opts, value);
- return -EINVAL;
- }
- } else if (!memcmp(opts, "gid", 3)) {
- data->perms.gid = make_kgid(current_user_ns(), value);
- if (!gid_valid(data->perms.gid)) {
- pr_err("%s: unmapped value: %lu\n", opts, value);
- return -EINVAL;
- }
- } else {
- goto invalid;
- }
- break;
- default:
- invalid:
- pr_err("%s: invalid option\n", opts);
- return -EINVAL;
- }
- /* Next iteration */
- if (!comma)
- break;
- opts = comma + 1;
- }
- return 0;
- }
- /* "mount -t functionfs dev_name /dev/function" ends up here */
- static struct dentry *
- ffs_fs_mount(struct file_system_type *t, int flags,
- const char *dev_name, void *opts)
- {
- struct ffs_sb_fill_data data = {
- .perms = {
- .mode = S_IFREG | 0600,
- .uid = GLOBAL_ROOT_UID,
- .gid = GLOBAL_ROOT_GID,
- },
- .root_mode = S_IFDIR | 0500,
- };
- struct dentry *rv;
- int ret;
- void *ffs_dev;
- struct ffs_data *ffs;
- ENTER();
- ret = ffs_fs_parse_opts(&data, opts);
- if (unlikely(ret < 0))
- return ERR_PTR(ret);
- ffs = ffs_data_new();
- if (unlikely(!ffs))
- return ERR_PTR(-ENOMEM);
- ffs->file_perms = data.perms;
- ffs->dev_name = kstrdup(dev_name, GFP_KERNEL);
- if (unlikely(!ffs->dev_name)) {
- ffs_data_put(ffs);
- return ERR_PTR(-ENOMEM);
- }
- ffs_dev = ffs_acquire_dev(dev_name);
- if (IS_ERR(ffs_dev)) {
- ffs_data_put(ffs);
- return ERR_CAST(ffs_dev);
- }
- ffs->private_data = ffs_dev;
- data.ffs_data = ffs;
- rv = mount_nodev(t, flags, &data, ffs_sb_fill);
- if (IS_ERR(rv) && data.ffs_data) {
- ffs_release_dev(data.ffs_data);
- ffs_data_put(data.ffs_data);
- }
- return rv;
- }
- static void
- ffs_fs_kill_sb(struct super_block *sb)
- {
- ENTER();
- kill_litter_super(sb);
- if (sb->s_fs_info) {
- ffs_release_dev(sb->s_fs_info);
- ffs_data_put(sb->s_fs_info);
- }
- }
- static struct file_system_type ffs_fs_type = {
- .owner = THIS_MODULE,
- .name = "functionfs",
- .mount = ffs_fs_mount,
- .kill_sb = ffs_fs_kill_sb,
- };
- MODULE_ALIAS_FS("functionfs");
- /* Driver's main init/cleanup functions *************************************/
- static int functionfs_init(void)
- {
- int ret;
- ENTER();
- ret = register_filesystem(&ffs_fs_type);
- if (likely(!ret))
- pr_info("file system registered\n");
- else
- pr_err("failed registering file system (%d)\n", ret);
- return ret;
- }
- static void functionfs_cleanup(void)
- {
- ENTER();
- pr_info("unloading\n");
- unregister_filesystem(&ffs_fs_type);
- }
- /* ffs_data and ffs_function construction and destruction code **************/
- static void ffs_data_clear(struct ffs_data *ffs);
- static void ffs_data_reset(struct ffs_data *ffs);
- static void ffs_data_get(struct ffs_data *ffs)
- {
- ENTER();
- atomic_inc(&ffs->ref);
- }
- static void ffs_data_opened(struct ffs_data *ffs)
- {
- ENTER();
- atomic_inc(&ffs->ref);
- atomic_inc(&ffs->opened);
- }
- static void ffs_data_put(struct ffs_data *ffs)
- {
- ENTER();
- if (unlikely(atomic_dec_and_test(&ffs->ref))) {
- pr_info("%s(): freeing\n", __func__);
- ffs_data_clear(ffs);
- BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
- waitqueue_active(&ffs->ep0req_completion.wait));
- kfree(ffs->dev_name);
- kfree(ffs);
- }
- }
- static void ffs_data_closed(struct ffs_data *ffs)
- {
- ENTER();
- if (atomic_dec_and_test(&ffs->opened)) {
- ffs->state = FFS_CLOSING;
- ffs_data_reset(ffs);
- }
- ffs_data_put(ffs);
- }
- static struct ffs_data *ffs_data_new(void)
- {
- struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
- if (unlikely(!ffs))
- return NULL;
- ENTER();
- atomic_set(&ffs->ref, 1);
- atomic_set(&ffs->opened, 0);
- ffs->state = FFS_READ_DESCRIPTORS;
- mutex_init(&ffs->mutex);
- spin_lock_init(&ffs->eps_lock);
- init_waitqueue_head(&ffs->ev.waitq);
- init_completion(&ffs->ep0req_completion);
- /* XXX REVISIT need to update it in some places, or do we? */
- ffs->ev.can_stall = 1;
- return ffs;
- }
- static void ffs_data_clear(struct ffs_data *ffs)
- {
- ENTER();
- if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags))
- ffs_closed(ffs);
- BUG_ON(ffs->gadget);
- if (ffs->epfiles)
- ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
- kfree(ffs->raw_descs);
- kfree(ffs->raw_strings);
- kfree(ffs->stringtabs);
- }
- static void ffs_data_reset(struct ffs_data *ffs)
- {
- ENTER();
- ffs_data_clear(ffs);
- ffs->epfiles = NULL;
- ffs->raw_descs = NULL;
- ffs->raw_strings = NULL;
- ffs->stringtabs = NULL;
- ffs->raw_descs_length = 0;
- ffs->raw_fs_descs_length = 0;
- ffs->fs_descs_count = 0;
- ffs->hs_descs_count = 0;
- ffs->strings_count = 0;
- ffs->interfaces_count = 0;
- ffs->eps_count = 0;
- ffs->ev.count = 0;
- ffs->state = FFS_READ_DESCRIPTORS;
- ffs->setup_state = FFS_NO_SETUP;
- ffs->flags = 0;
- }
- static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
- {
- struct usb_gadget_strings **lang;
- int first_id;
- ENTER();
- if (WARN_ON(ffs->state != FFS_ACTIVE
- || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
- return -EBADFD;
- first_id = usb_string_ids_n(cdev, ffs->strings_count);
- if (unlikely(first_id < 0))
- return first_id;
- ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
- if (unlikely(!ffs->ep0req))
- return -ENOMEM;
- ffs->ep0req->complete = ffs_ep0_complete;
- ffs->ep0req->context = ffs;
- lang = ffs->stringtabs;
- for (lang = ffs->stringtabs; *lang; ++lang) {
- struct usb_string *str = (*lang)->strings;
- int id = first_id;
- for (; str->s; ++id, ++str)
- str->id = id;
- }
- ffs->gadget = cdev->gadget;
- ffs_data_get(ffs);
- return 0;
- }
- static void functionfs_unbind(struct ffs_data *ffs)
- {
- ENTER();
- if (!WARN_ON(!ffs->gadget)) {
- usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
- ffs->ep0req = NULL;
- ffs->gadget = NULL;
- clear_bit(FFS_FL_BOUND, &ffs->flags);
- ffs_data_put(ffs);
- }
- }
- static int ffs_epfiles_create(struct ffs_data *ffs)
- {
- struct ffs_epfile *epfile, *epfiles;
- unsigned i, count;
- ENTER();
- count = ffs->eps_count;
- epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL);
- if (!epfiles)
- return -ENOMEM;
- epfile = epfiles;
- for (i = 1; i <= count; ++i, ++epfile) {
- epfile->ffs = ffs;
- mutex_init(&epfile->mutex);
- init_waitqueue_head(&epfile->wait);
- sprintf(epfiles->name, "ep%u", i);
- if (!unlikely(ffs_sb_create_file(ffs->sb, epfiles->name, epfile,
- &ffs_epfile_operations,
- &epfile->dentry))) {
- ffs_epfiles_destroy(epfiles, i - 1);
- return -ENOMEM;
- }
- }
- ffs->epfiles = epfiles;
- return 0;
- }
- static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
- {
- struct ffs_epfile *epfile = epfiles;
- ENTER();
- for (; count; --count, ++epfile) {
- BUG_ON(mutex_is_locked(&epfile->mutex) ||
- waitqueue_active(&epfile->wait));
- if (epfile->dentry) {
- d_delete(epfile->dentry);
- dput(epfile->dentry);
- epfile->dentry = NULL;
- }
- }
- kfree(epfiles);
- }
- static void ffs_func_eps_disable(struct ffs_function *func)
- {
- struct ffs_ep *ep = func->eps;
- struct ffs_epfile *epfile = func->ffs->epfiles;
- unsigned count = func->ffs->eps_count;
- unsigned long flags;
- spin_lock_irqsave(&func->ffs->eps_lock, flags);
- do {
- /* pending requests get nuked */
- if (likely(ep->ep))
- usb_ep_disable(ep->ep);
- epfile->ep = NULL;
- ++ep;
- ++epfile;
- } while (--count);
- spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
- }
- static int ffs_func_eps_enable(struct ffs_function *func)
- {
- struct ffs_data *ffs = func->ffs;
- struct ffs_ep *ep = func->eps;
- struct ffs_epfile *epfile = ffs->epfiles;
- unsigned count = ffs->eps_count;
- unsigned long flags;
- int ret = 0;
- spin_lock_irqsave(&func->ffs->eps_lock, flags);
- do {
- struct usb_endpoint_descriptor *ds;
- ds = ep->descs[ep->descs[1] ? 1 : 0];
- ep->ep->driver_data = ep;
- ep->ep->desc = ds;
- ret = usb_ep_enable(ep->ep);
- if (likely(!ret)) {
- epfile->ep = ep;
- epfile->in = usb_endpoint_dir_in(ds);
- epfile->isoc = usb_endpoint_xfer_isoc(ds);
- } else {
- break;
- }
- wake_up(&epfile->wait);
- ++ep;
- ++epfile;
- } while (--count);
- spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
- return ret;
- }
- /* Parsing and building descriptors and strings *****************************/
- /*
- * This validates if data pointed by data is a valid USB descriptor as
- * well as record how many interfaces, endpoints and strings are
- * required by given configuration. Returns address after the
- * descriptor or NULL if data is invalid.
- */
- enum ffs_entity_type {
- FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
- };
- typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
- u8 *valuep,
- struct usb_descriptor_header *desc,
- void *priv);
- static int __must_check ffs_do_desc(char *data, unsigned len,
- ffs_entity_callback entity, void *priv)
- {
- struct usb_descriptor_header *_ds = (void *)data;
- u8 length;
- int ret;
- ENTER();
- /* At least two bytes are required: length and type */
- if (len < 2) {
- pr_vdebug("descriptor too short\n");
- return -EINVAL;
- }
- /* If we have at least as many bytes as the descriptor takes? */
- length = _ds->bLength;
- if (len < length) {
- pr_vdebug("descriptor longer then available data\n");
- return -EINVAL;
- }
- #define __entity_check_INTERFACE(val) 1
- #define __entity_check_STRING(val) (val)
- #define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
- #define __entity(type, val) do { \
- pr_vdebug("entity " #type "(%02x)\n", (val)); \
- if (unlikely(!__entity_check_ ##type(val))) { \
- pr_vdebug("invalid entity's value\n"); \
- return -EINVAL; \
- } \
- ret = entity(FFS_ ##type, &val, _ds, priv); \
- if (unlikely(ret < 0)) { \
- pr_debug("entity " #type "(%02x); ret = %d\n", \
- (val), ret); \
- return ret; \
- } \
- } while (0)
- /* Parse descriptor depending on type. */
- switch (_ds->bDescriptorType) {
- case USB_DT_DEVICE:
- case USB_DT_CONFIG:
- case USB_DT_STRING:
- case USB_DT_DEVICE_QUALIFIER:
- /* function can't have any of those */
- pr_vdebug("descriptor reserved for gadget: %d\n",
- _ds->bDescriptorType);
- return -EINVAL;
- case USB_DT_INTERFACE: {
- struct usb_interface_descriptor *ds = (void *)_ds;
- pr_vdebug("interface descriptor\n");
- if (length != sizeof *ds)
- goto inv_length;
- __entity(INTERFACE, ds->bInterfaceNumber);
- if (ds->iInterface)
- __entity(STRING, ds->iInterface);
- }
- break;
- case USB_DT_ENDPOINT: {
- struct usb_endpoint_descriptor *ds = (void *)_ds;
- pr_vdebug("endpoint descriptor\n");
- if (length != USB_DT_ENDPOINT_SIZE &&
- length != USB_DT_ENDPOINT_AUDIO_SIZE)
- goto inv_length;
- __entity(ENDPOINT, ds->bEndpointAddress);
- }
- break;
- case HID_DT_HID:
- pr_vdebug("hid descriptor\n");
- if (length != sizeof(struct hid_descriptor))
- goto inv_length;
- break;
- case USB_DT_OTG:
- if (length != sizeof(struct usb_otg_descriptor))
- goto inv_length;
- break;
- case USB_DT_INTERFACE_ASSOCIATION: {
- struct usb_interface_assoc_descriptor *ds = (void *)_ds;
- pr_vdebug("interface association descriptor\n");
- if (length != sizeof *ds)
- goto inv_length;
- if (ds->iFunction)
- __entity(STRING, ds->iFunction);
- }
- break;
- case USB_DT_OTHER_SPEED_CONFIG:
- case USB_DT_INTERFACE_POWER:
- case USB_DT_DEBUG:
- case USB_DT_SECURITY:
- case USB_DT_CS_RADIO_CONTROL:
- /* TODO */
- pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
- return -EINVAL;
- default:
- /* We should never be here */
- pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
- return -EINVAL;
- inv_length:
- pr_vdebug("invalid length: %d (descriptor %d)\n",
- _ds->bLength, _ds->bDescriptorType);
- return -EINVAL;
- }
- #undef __entity
- #undef __entity_check_DESCRIPTOR
- #undef __entity_check_INTERFACE
- #undef __entity_check_STRING
- #undef __entity_check_ENDPOINT
- return length;
- }
- static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
- ffs_entity_callback entity, void *priv)
- {
- const unsigned _len = len;
- unsigned long num = 0;
- ENTER();
- for (;;) {
- int ret;
- if (num == count)
- data = NULL;
- /* Record "descriptor" entity */
- ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
- if (unlikely(ret < 0)) {
- pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
- num, ret);
- return ret;
- }
- if (!data)
- return _len - len;
- ret = ffs_do_desc(data, len, entity, priv);
- if (unlikely(ret < 0)) {
- pr_debug("%s returns %d\n", __func__, ret);
- return ret;
- }
- len -= ret;
- data += ret;
- ++num;
- }
- }
- static int __ffs_data_do_entity(enum ffs_entity_type type,
- u8 *valuep, struct usb_descriptor_header *desc,
- void *priv)
- {
- struct ffs_data *ffs = priv;
- ENTER();
- switch (type) {
- case FFS_DESCRIPTOR:
- break;
- case FFS_INTERFACE:
- /*
- * Interfaces are indexed from zero so if we
- * encountered interface "n" then there are at least
- * "n+1" interfaces.
- */
- if (*valuep >= ffs->interfaces_count)
- ffs->interfaces_count = *valuep + 1;
- break;
- case FFS_STRING:
- /*
- * Strings are indexed from 1 (0 is magic ;) reserved
- * for languages list or some such)
- */
- if (*valuep > ffs->strings_count)
- ffs->strings_count = *valuep;
- break;
- case FFS_ENDPOINT:
- /* Endpoints are indexed from 1 as well. */
- if ((*valuep & USB_ENDPOINT_NUMBER_MASK) > ffs->eps_count)
- ffs->eps_count = (*valuep & USB_ENDPOINT_NUMBER_MASK);
- break;
- }
- return 0;
- }
- static int __ffs_data_got_descs(struct ffs_data *ffs,
- char *const _data, size_t len)
- {
- unsigned fs_count, hs_count;
- int fs_len, ret = -EINVAL;
- char *data = _data;
- ENTER();
- if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_DESCRIPTORS_MAGIC ||
- get_unaligned_le32(data + 4) != len))
- goto error;
- fs_count = get_unaligned_le32(data + 8);
- hs_count = get_unaligned_le32(data + 12);
- if (!fs_count && !hs_count)
- goto einval;
- data += 16;
- len -= 16;
- if (likely(fs_count)) {
- fs_len = ffs_do_descs(fs_count, data, len,
- __ffs_data_do_entity, ffs);
- if (unlikely(fs_len < 0)) {
- ret = fs_len;
- goto error;
- }
- data += fs_len;
- len -= fs_len;
- } else {
- fs_len = 0;
- }
- if (likely(hs_count)) {
- ret = ffs_do_descs(hs_count, data, len,
- __ffs_data_do_entity, ffs);
- if (unlikely(ret < 0))
- goto error;
- } else {
- ret = 0;
- }
- if (unlikely(len != ret))
- goto einval;
- ffs->raw_fs_descs_length = fs_len;
- ffs->raw_descs_length = fs_len + ret;
- ffs->raw_descs = _data;
- ffs->fs_descs_count = fs_count;
- ffs->hs_descs_count = hs_count;
- return 0;
- einval:
- ret = -EINVAL;
- error:
- kfree(_data);
- return ret;
- }
- static int __ffs_data_got_strings(struct ffs_data *ffs,
- char *const _data, size_t len)
- {
- u32 str_count, needed_count, lang_count;
- struct usb_gadget_strings **stringtabs, *t;
- struct usb_string *strings, *s;
- const char *data = _data;
- ENTER();
- if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
- get_unaligned_le32(data + 4) != len))
- goto error;
- str_count = get_unaligned_le32(data + 8);
- lang_count = get_unaligned_le32(data + 12);
- /* if one is zero the other must be zero */
- if (unlikely(!str_count != !lang_count))
- goto error;
- /* Do we have at least as many strings as descriptors need? */
- needed_count = ffs->strings_count;
- if (unlikely(str_count < needed_count))
- goto error;
- /*
- * If we don't need any strings just return and free all
- * memory.
- */
- if (!needed_count) {
- kfree(_data);
- return 0;
- }
- /* Allocate everything in one chunk so there's less maintenance. */
- {
- unsigned i = 0;
- vla_group(d);
- vla_item(d, struct usb_gadget_strings *, stringtabs,
- lang_count + 1);
- vla_item(d, struct usb_gadget_strings, stringtab, lang_count);
- vla_item(d, struct usb_string, strings,
- lang_count*(needed_count+1));
- char *vlabuf = kmalloc(vla_group_size(d), GFP_KERNEL);
- if (unlikely(!vlabuf)) {
- kfree(_data);
- return -ENOMEM;
- }
- /* Initialize the VLA pointers */
- stringtabs = vla_ptr(vlabuf, d, stringtabs);
- t = vla_ptr(vlabuf, d, stringtab);
- i = lang_count;
- do {
- *stringtabs++ = t++;
- } while (--i);
- *stringtabs = NULL;
- /* stringtabs = vlabuf = d_stringtabs for later kfree */
- stringtabs = vla_ptr(vlabuf, d, stringtabs);
- t = vla_ptr(vlabuf, d, stringtab);
- s = vla_ptr(vlabuf, d, strings);
- strings = s;
- }
- /* For each language */
- data += 16;
- len -= 16;
- do { /* lang_count > 0 so we can use do-while */
- unsigned needed = needed_count;
- if (unlikely(len < 3))
- goto error_free;
- t->language = get_unaligned_le16(data);
- t->strings = s;
- ++t;
- data += 2;
- len -= 2;
- /* For each string */
- do { /* str_count > 0 so we can use do-while */
- size_t length = strnlen(data, len);
- if (unlikely(length == len))
- goto error_free;
- /*
- * User may provide more strings then we need,
- * if that's the case we simply ignore the
- * rest
- */
- if (likely(needed)) {
- /*
- * s->id will be set while adding
- * function to configuration so for
- * now just leave garbage here.
- */
- s->s = data;
- --needed;
- ++s;
- }
- data += length + 1;
- len -= length + 1;
- } while (--str_count);
- s->id = 0; /* terminator */
- s->s = NULL;
- ++s;
- } while (--lang_count);
- /* Some garbage left? */
- if (unlikely(len))
- goto error_free;
- /* Done! */
- ffs->stringtabs = stringtabs;
- ffs->raw_strings = _data;
- return 0;
- error_free:
- kfree(stringtabs);
- error:
- kfree(_data);
- return -EINVAL;
- }
- /* Events handling and management *******************************************/
- static void __ffs_event_add(struct ffs_data *ffs,
- enum usb_functionfs_event_type type)
- {
- enum usb_functionfs_event_type rem_type1, rem_type2 = type;
- int neg = 0;
- /*
- * Abort any unhandled setup
- *
- * We do not need to worry about some cmpxchg() changing value
- * of ffs->setup_state without holding the lock because when
- * state is FFS_SETUP_PENDING cmpxchg() in several places in
- * the source does nothing.
- */
- if (ffs->setup_state == FFS_SETUP_PENDING)
- ffs->setup_state = FFS_SETUP_CANCELED;
- switch (type) {
- case FUNCTIONFS_RESUME:
- rem_type2 = FUNCTIONFS_SUSPEND;
- /* FALL THROUGH */
- case FUNCTIONFS_SUSPEND:
- case FUNCTIONFS_SETUP:
- rem_type1 = type;
- /* Discard all similar events */
- break;
- case FUNCTIONFS_BIND:
- case FUNCTIONFS_UNBIND:
- case FUNCTIONFS_DISABLE:
- case FUNCTIONFS_ENABLE:
- /* Discard everything other then power management. */
- rem_type1 = FUNCTIONFS_SUSPEND;
- rem_type2 = FUNCTIONFS_RESUME;
- neg = 1;
- break;
- default:
- BUG();
- }
- {
- u8 *ev = ffs->ev.types, *out = ev;
- unsigned n = ffs->ev.count;
- for (; n; --n, ++ev)
- if ((*ev == rem_type1 || *ev == rem_type2) == neg)
- *out++ = *ev;
- else
- pr_vdebug("purging event %d\n", *ev);
- ffs->ev.count = out - ffs->ev.types;
- }
- pr_vdebug("adding event %d\n", type);
- ffs->ev.types[ffs->ev.count++] = type;
- wake_up_locked(&ffs->ev.waitq);
- }
- static void ffs_event_add(struct ffs_data *ffs,
- enum usb_functionfs_event_type type)
- {
- unsigned long flags;
- spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
- __ffs_event_add(ffs, type);
- spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
- }
- /* Bind/unbind USB function hooks *******************************************/
- static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
- struct usb_descriptor_header *desc,
- void *priv)
- {
- struct usb_endpoint_descriptor *ds = (void *)desc;
- struct ffs_function *func = priv;
- struct ffs_ep *ffs_ep;
- /*
- * If hs_descriptors is not NULL then we are reading hs
- * descriptors now
- */
- const int isHS = func->function.hs_descriptors != NULL;
- unsigned idx;
- if (type != FFS_DESCRIPTOR)
- return 0;
- if (isHS)
- func->function.hs_descriptors[(long)valuep] = desc;
- else
- func->function.fs_descriptors[(long)valuep] = desc;
- if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
- return 0;
- idx = (ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) - 1;
- ffs_ep = func->eps + idx;
- if (unlikely(ffs_ep->descs[isHS])) {
- pr_vdebug("two %sspeed descriptors for EP %d\n",
- isHS ? "high" : "full",
- ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
- return -EINVAL;
- }
- ffs_ep->descs[isHS] = ds;
- ffs_dump_mem(": Original ep desc", ds, ds->bLength);
- if (ffs_ep->ep) {
- ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
- if (!ds->wMaxPacketSize)
- ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
- } else {
- struct usb_request *req;
- struct usb_ep *ep;
- pr_vdebug("autoconfig\n");
- ep = usb_ep_autoconfig(func->gadget, ds);
- if (unlikely(!ep))
- return -ENOTSUPP;
- ep->driver_data = func->eps + idx;
- req = usb_ep_alloc_request(ep, GFP_KERNEL);
- if (unlikely(!req))
- return -ENOMEM;
- ffs_ep->ep = ep;
- ffs_ep->req = req;
- func->eps_revmap[ds->bEndpointAddress &
- USB_ENDPOINT_NUMBER_MASK] = idx + 1;
- }
- ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
- return 0;
- }
- static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
- struct usb_descriptor_header *desc,
- void *priv)
- {
- struct ffs_function *func = priv;
- unsigned idx;
- u8 newValue;
- switch (type) {
- default:
- case FFS_DESCRIPTOR:
- /* Handled in previous pass by __ffs_func_bind_do_descs() */
- return 0;
- case FFS_INTERFACE:
- idx = *valuep;
- if (func->interfaces_nums[idx] < 0) {
- int id = usb_interface_id(func->conf, &func->function);
- if (unlikely(id < 0))
- return id;
- func->interfaces_nums[idx] = id;
- }
- newValue = func->interfaces_nums[idx];
- break;
- case FFS_STRING:
- /* String' IDs are allocated when fsf_data is bound to cdev */
- newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
- break;
- case FFS_ENDPOINT:
- /*
- * USB_DT_ENDPOINT are handled in
- * __ffs_func_bind_do_descs().
- */
- if (desc->bDescriptorType == USB_DT_ENDPOINT)
- return 0;
- idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
- if (unlikely(!func->eps[idx].ep))
- return -EINVAL;
- {
- struct usb_endpoint_descriptor **descs;
- descs = func->eps[idx].descs;
- newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
- }
- break;
- }
- pr_vdebug("%02x -> %02x\n", *valuep, newValue);
- *valuep = newValue;
- return 0;
- }
- static inline struct f_fs_opts *ffs_do_functionfs_bind(struct usb_function *f,
- struct usb_configuration *c)
- {
- struct ffs_function *func = ffs_func_from_usb(f);
- struct f_fs_opts *ffs_opts =
- container_of(f->fi, struct f_fs_opts, func_inst);
- int ret;
- ENTER();
- /*
- * Legacy gadget triggers binding in functionfs_ready_callback,
- * which already uses locking; taking the same lock here would
- * cause a deadlock.
- *
- * Configfs-enabled gadgets however do need ffs_dev_lock.
- */
- if (!ffs_opts->no_configfs)
- ffs_dev_lock();
- ret = ffs_opts->dev->desc_ready ? 0 : -ENODEV;
- func->ffs = ffs_opts->dev->ffs_data;
- if (!ffs_opts->no_configfs)
- ffs_dev_unlock();
- if (ret)
- return ERR_PTR(ret);
- func->conf = c;
- func->gadget = c->cdev->gadget;
- ffs_data_get(func->ffs);
- /*
- * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
- * configurations are bound in sequence with list_for_each_entry,
- * in each configuration its functions are bound in sequence
- * with list_for_each_entry, so we assume no race condition
- * with regard to ffs_opts->bound access
- */
- if (!ffs_opts->refcnt) {
- ret = functionfs_bind(func->ffs, c->cdev);
- if (ret)
- return ERR_PTR(ret);
- }
- ffs_opts->refcnt++;
- func->function.strings = func->ffs->stringtabs;
- return ffs_opts;
- }
- static int _ffs_func_bind(struct usb_configuration *c,
- struct usb_function *f)
- {
- struct ffs_function *func = ffs_func_from_usb(f);
- struct ffs_data *ffs = func->ffs;
- const int full = !!func->ffs->fs_descs_count;
- const int high = gadget_is_dualspeed(func->gadget) &&
- func->ffs->hs_descs_count;
- int ret;
- /* Make it a single chunk, less management later on */
- vla_group(d);
- vla_item_with_sz(d, struct ffs_ep, eps, ffs->eps_count);
- vla_item_with_sz(d, struct usb_descriptor_header *, fs_descs,
- full ? ffs->fs_descs_count + 1 : 0);
- vla_item_with_sz(d, struct usb_descriptor_header *, hs_descs,
- high ? ffs->hs_descs_count + 1 : 0);
- vla_item_with_sz(d, short, inums, ffs->interfaces_count);
- vla_item_with_sz(d, char, raw_descs,
- high ? ffs->raw_descs_length : ffs->raw_fs_descs_length);
- char *vlabuf;
- ENTER();
- /* Only high speed but not supported by gadget? */
- if (unlikely(!(full | high)))
- return -ENOTSUPP;
- /* Allocate a single chunk, less management later on */
- vlabuf = kmalloc(vla_group_size(d), GFP_KERNEL);
- if (unlikely(!vlabuf))
- return -ENOMEM;
- /* Zero */
- memset(vla_ptr(vlabuf, d, eps), 0, d_eps__sz);
- memcpy(vla_ptr(vlabuf, d, raw_descs), ffs->raw_descs + 16,
- d_raw_descs__sz);
- memset(vla_ptr(vlabuf, d, inums), 0xff, d_inums__sz);
- for (ret = ffs->eps_count; ret; --ret) {
- struct ffs_ep *ptr;
- ptr = vla_ptr(vlabuf, d, eps);
- ptr[ret].num = -1;
- }
- /* Save pointers
- * d_eps == vlabuf, func->eps used to kfree vlabuf later
- */
- func->eps = vla_ptr(vlabuf, d, eps);
- func->interfaces_nums = vla_ptr(vlabuf, d, inums);
- /*
- * Go through all the endpoint descriptors and allocate
- * endpoints first, so that later we can rewrite the endpoint
- * numbers without worrying that it may be described later on.
- */
- if (likely(full)) {
- func->function.fs_descriptors = vla_ptr(vlabuf, d, fs_descs);
- ret = ffs_do_descs(ffs->fs_descs_count,
- vla_ptr(vlabuf, d, raw_descs),
- d_raw_descs__sz,
- __ffs_func_bind_do_descs, func);
- if (unlikely(ret < 0))
- goto error;
- } else {
- ret = 0;
- }
- if (likely(high)) {
- func->function.hs_descriptors = vla_ptr(vlabuf, d, hs_descs);
- ret = ffs_do_descs(ffs->hs_descs_count,
- vla_ptr(vlabuf, d, raw_descs) + ret,
- d_raw_descs__sz - ret,
- __ffs_func_bind_do_descs, func);
- if (unlikely(ret < 0))
- goto error;
- }
- /*
- * Now handle interface numbers allocation and interface and
- * endpoint numbers rewriting. We can do that in one go
- * now.
- */
- ret = ffs_do_descs(ffs->fs_descs_count +
- (high ? ffs->hs_descs_count : 0),
- vla_ptr(vlabuf, d, raw_descs), d_raw_descs__sz,
- __ffs_func_bind_do_nums, func);
- if (unlikely(ret < 0))
- goto error;
- /* And we're done */
- ffs_event_add(ffs, FUNCTIONFS_BIND);
- return 0;
- error:
- /* XXX Do we need to release all claimed endpoints here? */
- return ret;
- }
- static int ffs_func_bind(struct usb_configuration *c,
- struct usb_function *f)
- {
- struct f_fs_opts *ffs_opts = ffs_do_functionfs_bind(f, c);
- if (IS_ERR(ffs_opts))
- return PTR_ERR(ffs_opts);
- return _ffs_func_bind(c, f);
- }
- /* Other USB function hooks *************************************************/
- static int ffs_func_set_alt(struct usb_function *f,
- unsigned interface, unsigned alt)
- {
- struct ffs_function *func = ffs_func_from_usb(f);
- struct ffs_data *ffs = func->ffs;
- int ret = 0, intf;
- if (alt != (unsigned)-1) {
- intf = ffs_func_revmap_intf(func, interface);
- if (unlikely(intf < 0))
- return intf;
- }
- if (ffs->func)
- ffs_func_eps_disable(ffs->func);
- if (ffs->state != FFS_ACTIVE)
- return -ENODEV;
- if (alt == (unsigned)-1) {
- ffs->func = NULL;
- ffs_event_add(ffs, FUNCTIONFS_DISABLE);
- return 0;
- }
- ffs->func = func;
- ret = ffs_func_eps_enable(func);
- if (likely(ret >= 0))
- ffs_event_add(ffs, FUNCTIONFS_ENABLE);
- return ret;
- }
- static void ffs_func_disable(struct usb_function *f)
- {
- ffs_func_set_alt(f, 0, (unsigned)-1);
- }
- static int ffs_func_setup(struct usb_function *f,
- const struct usb_ctrlrequest *creq)
- {
- struct ffs_function *func = ffs_func_from_usb(f);
- struct ffs_data *ffs = func->ffs;
- unsigned long flags;
- int ret;
- ENTER();
- pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
- pr_vdebug("creq->bRequest = %02x\n", creq->bRequest);
- pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue));
- pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex));
- pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength));
- /*
- * Most requests directed to interface go through here
- * (notable exceptions are set/get interface) so we need to
- * handle them. All other either handled by composite or
- * passed to usb_configuration->setup() (if one is set). No
- * matter, we will handle requests directed to endpoint here
- * as well (as it's straightforward) but what to do with any
- * other request?
- */
- if (ffs->state != FFS_ACTIVE)
- return -ENODEV;
- switch (creq->bRequestType & USB_RECIP_MASK) {
- case USB_RECIP_INTERFACE:
- ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
- if (unlikely(ret < 0))
- return ret;
- break;
- case USB_RECIP_ENDPOINT:
- ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
- if (unlikely(ret < 0))
- return ret;
- break;
- default:
- return -EOPNOTSUPP;
- }
- spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
- ffs->ev.setup = *creq;
- ffs->ev.setup.wIndex = cpu_to_le16(ret);
- __ffs_event_add(ffs, FUNCTIONFS_SETUP);
- spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
- return 0;
- }
- static void ffs_func_suspend(struct usb_function *f)
- {
- ENTER();
- ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
- }
- static void ffs_func_resume(struct usb_function *f)
- {
- ENTER();
- ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
- }
- /* Endpoint and interface numbers reverse mapping ***************************/
- static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
- {
- num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
- return num ? num : -EDOM;
- }
- static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
- {
- short *nums = func->interfaces_nums;
- unsigned count = func->ffs->interfaces_count;
- for (; count; --count, ++nums) {
- if (*nums >= 0 && *nums == intf)
- return nums - func->interfaces_nums;
- }
- return -EDOM;
- }
- /* Devices management *******************************************************/
- static LIST_HEAD(ffs_devices);
- static struct ffs_dev *_ffs_find_dev(const char *name)
- {
- struct ffs_dev *dev;
- list_for_each_entry(dev, &ffs_devices, entry) {
- if (!dev->name || !name)
- continue;
- if (strcmp(dev->name, name) == 0)
- return dev;
- }
- return NULL;
- }
- /*
- * ffs_lock must be taken by the caller of this function
- */
- static struct ffs_dev *ffs_get_single_dev(void)
- {
- struct ffs_dev *dev;
- if (list_is_singular(&ffs_devices)) {
- dev = list_first_entry(&ffs_devices, struct ffs_dev, entry);
- if (dev->single)
- return dev;
- }
- return NULL;
- }
- /*
- * ffs_lock must be taken by the caller of this function
- */
- static struct ffs_dev *ffs_find_dev(const char *name)
- {
- struct ffs_dev *dev;
- dev = ffs_get_single_dev();
- if (dev)
- return dev;
- return _ffs_find_dev(name);
- }
- /* Configfs support *********************************************************/
- static inline struct f_fs_opts *to_ffs_opts(struct config_item *item)
- {
- return container_of(to_config_group(item), struct f_fs_opts,
- func_inst.group);
- }
- static void ffs_attr_release(struct config_item *item)
- {
- struct f_fs_opts *opts = to_ffs_opts(item);
- usb_put_function_instance(&opts->func_inst);
- }
- static struct configfs_item_operations ffs_item_ops = {
- .release = ffs_attr_release,
- };
- static struct config_item_type ffs_func_type = {
- .ct_item_ops = &ffs_item_ops,
- .ct_owner = THIS_MODULE,
- };
- /* Function registration interface ******************************************/
- static void ffs_free_inst(struct usb_function_instance *f)
- {
- struct f_fs_opts *opts;
- opts = to_f_fs_opts(f);
- ffs_dev_lock();
- ffs_free_dev(opts->dev);
- ffs_dev_unlock();
- kfree(opts);
- }
- #define MAX_INST_NAME_LEN 40
- static int ffs_set_inst_name(struct usb_function_instance *fi, const char *name)
- {
- struct f_fs_opts *opts;
- char *ptr;
- const char *tmp;
- int name_len, ret;
- name_len = strlen(name) + 1;
- if (name_len > MAX_INST_NAME_LEN)
- return -ENAMETOOLONG;
- ptr = kstrndup(name, name_len, GFP_KERNEL);
- if (!ptr)
- return -ENOMEM;
- opts = to_f_fs_opts(fi);
- tmp = NULL;
- ffs_dev_lock();
- tmp = opts->dev->name_allocated ? opts->dev->name : NULL;
- ret = _ffs_name_dev(opts->dev, ptr);
- if (ret) {
- kfree(ptr);
- ffs_dev_unlock();
- return ret;
- }
- opts->dev->name_allocated = true;
- ffs_dev_unlock();
- kfree(tmp);
- return 0;
- }
- static struct usb_function_instance *ffs_alloc_inst(void)
- {
- struct f_fs_opts *opts;
- struct ffs_dev *dev;
- opts = kzalloc(sizeof(*opts), GFP_KERNEL);
- if (!opts)
- return ERR_PTR(-ENOMEM);
- opts->func_inst.set_inst_name = ffs_set_inst_name;
- opts->func_inst.free_func_inst = ffs_free_inst;
- ffs_dev_lock();
- dev = ffs_alloc_dev();
- ffs_dev_unlock();
- if (IS_ERR(dev)) {
- kfree(opts);
- return ERR_CAST(dev);
- }
- opts->dev = dev;
- dev->opts = opts;
- config_group_init_type_name(&opts->func_inst.group, "",
- &ffs_func_type);
- return &opts->func_inst;
- }
- static void ffs_free(struct usb_function *f)
- {
- kfree(ffs_func_from_usb(f));
- }
- static void ffs_func_unbind(struct usb_configuration *c,
- struct usb_function *f)
- {
- struct ffs_function *func = ffs_func_from_usb(f);
- struct ffs_data *ffs = func->ffs;
- struct f_fs_opts *opts =
- container_of(f->fi, struct f_fs_opts, func_inst);
- struct ffs_ep *ep = func->eps;
- unsigned count = ffs->eps_count;
- unsigned long flags;
- ENTER();
- if (ffs->func == func) {
- ffs_func_eps_disable(func);
- ffs->func = NULL;
- }
- if (!--opts->refcnt)
- functionfs_unbind(ffs);
- /* cleanup after autoconfig */
- spin_lock_irqsave(&func->ffs->eps_lock, flags);
- do {
- if (ep->ep && ep->req)
- usb_ep_free_request(ep->ep, ep->req);
- ep->req = NULL;
- ++ep;
- } while (--count);
- spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
- kfree(func->eps);
- func->eps = NULL;
- /*
- * eps, descriptors and interfaces_nums are allocated in the
- * same chunk so only one free is required.
- */
- func->function.fs_descriptors = NULL;
- func->function.hs_descriptors = NULL;
- func->interfaces_nums = NULL;
- ffs_event_add(ffs, FUNCTIONFS_UNBIND);
- }
- static struct usb_function *ffs_alloc(struct usb_function_instance *fi)
- {
- struct ffs_function *func;
- ENTER();
- func = kzalloc(sizeof(*func), GFP_KERNEL);
- if (unlikely(!func))
- return ERR_PTR(-ENOMEM);
- func->function.name = "Function FS Gadget";
- func->function.bind = ffs_func_bind;
- func->function.unbind = ffs_func_unbind;
- func->function.set_alt = ffs_func_set_alt;
- func->function.disable = ffs_func_disable;
- func->function.setup = ffs_func_setup;
- func->function.suspend = ffs_func_suspend;
- func->function.resume = ffs_func_resume;
- func->function.free_func = ffs_free;
- return &func->function;
- }
- /*
- * ffs_lock must be taken by the caller of this function
- */
- struct ffs_dev *ffs_alloc_dev(void)
- {
- struct ffs_dev *dev;
- int ret;
- if (ffs_get_single_dev())
- return ERR_PTR(-EBUSY);
- dev = kzalloc(sizeof(*dev), GFP_KERNEL);
- if (!dev)
- return ERR_PTR(-ENOMEM);
- if (list_empty(&ffs_devices)) {
- ret = functionfs_init();
- if (ret) {
- kfree(dev);
- return ERR_PTR(ret);
- }
- }
- list_add(&dev->entry, &ffs_devices);
- return dev;
- }
- /*
- * ffs_lock must be taken by the caller of this function
- * The caller is responsible for "name" being available whenever f_fs needs it
- */
- static int _ffs_name_dev(struct ffs_dev *dev, const char *name)
- {
- struct ffs_dev *existing;
- existing = _ffs_find_dev(name);
- if (existing)
- return -EBUSY;
-
- dev->name = name;
- return 0;
- }
- /*
- * The caller is responsible for "name" being available whenever f_fs needs it
- */
- int ffs_name_dev(struct ffs_dev *dev, const char *name)
- {
- int ret;
- ffs_dev_lock();
- ret = _ffs_name_dev(dev, name);
- ffs_dev_unlock();
- return ret;
- }
- EXPORT_SYMBOL(ffs_name_dev);
- int ffs_single_dev(struct ffs_dev *dev)
- {
- int ret;
- ret = 0;
- ffs_dev_lock();
- if (!list_is_singular(&ffs_devices))
- ret = -EBUSY;
- else
- dev->single = true;
- ffs_dev_unlock();
- return ret;
- }
- EXPORT_SYMBOL(ffs_single_dev);
- /*
- * ffs_lock must be taken by the caller of this function
- */
- void ffs_free_dev(struct ffs_dev *dev)
- {
- list_del(&dev->entry);
- if (dev->name_allocated)
- kfree(dev->name);
- kfree(dev);
- if (list_empty(&ffs_devices))
- functionfs_cleanup();
- }
- static void *ffs_acquire_dev(const char *dev_name)
- {
- struct ffs_dev *ffs_dev;
- ENTER();
- ffs_dev_lock();
- ffs_dev = ffs_find_dev(dev_name);
- if (!ffs_dev)
- ffs_dev = ERR_PTR(-ENODEV);
- else if (ffs_dev->mounted)
- ffs_dev = ERR_PTR(-EBUSY);
- else if (ffs_dev->ffs_acquire_dev_callback &&
- ffs_dev->ffs_acquire_dev_callback(ffs_dev))
- ffs_dev = ERR_PTR(-ENODEV);
- else
- ffs_dev->mounted = true;
- ffs_dev_unlock();
- return ffs_dev;
- }
- static void ffs_release_dev(struct ffs_data *ffs_data)
- {
- struct ffs_dev *ffs_dev;
- ENTER();
- ffs_dev_lock();
- ffs_dev = ffs_data->private_data;
- if (ffs_dev)
- ffs_dev->mounted = false;
-
- if (ffs_dev->ffs_release_dev_callback)
- ffs_dev->ffs_release_dev_callback(ffs_dev);
- ffs_dev_unlock();
- }
- static int ffs_ready(struct ffs_data *ffs)
- {
- struct ffs_dev *ffs_obj;
- int ret = 0;
- ENTER();
- ffs_dev_lock();
- ffs_obj = ffs->private_data;
- if (!ffs_obj) {
- ret = -EINVAL;
- goto done;
- }
- if (WARN_ON(ffs_obj->desc_ready)) {
- ret = -EBUSY;
- goto done;
- }
- ffs_obj->desc_ready = true;
- ffs_obj->ffs_data = ffs;
- if (ffs_obj->ffs_ready_callback)
- ret = ffs_obj->ffs_ready_callback(ffs);
- done:
- ffs_dev_unlock();
- return ret;
- }
- static void ffs_closed(struct ffs_data *ffs)
- {
- struct ffs_dev *ffs_obj;
- ENTER();
- ffs_dev_lock();
- ffs_obj = ffs->private_data;
- if (!ffs_obj)
- goto done;
- ffs_obj->desc_ready = false;
- if (ffs_obj->ffs_closed_callback)
- ffs_obj->ffs_closed_callback(ffs);
- if (!ffs_obj->opts || ffs_obj->opts->no_configfs
- || !ffs_obj->opts->func_inst.group.cg_item.ci_parent)
- goto done;
- unregister_gadget_item(ffs_obj->opts->
- func_inst.group.cg_item.ci_parent->ci_parent);
- done:
- ffs_dev_unlock();
- }
- /* Misc helper functions ****************************************************/
- static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
- {
- return nonblock
- ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
- : mutex_lock_interruptible(mutex);
- }
- static char *ffs_prepare_buffer(const char __user *buf, size_t len)
- {
- char *data;
- if (unlikely(!len))
- return NULL;
- data = kmalloc(len, GFP_KERNEL);
- if (unlikely(!data))
- return ERR_PTR(-ENOMEM);
- if (unlikely(__copy_from_user(data, buf, len))) {
- kfree(data);
- return ERR_PTR(-EFAULT);
- }
- pr_vdebug("Buffer from user space:\n");
- ffs_dump_mem("", data, len);
- return data;
- }
- DECLARE_USB_FUNCTION_INIT(ffs, ffs_alloc_inst, ffs_alloc);
- MODULE_LICENSE("GPL");
- MODULE_AUTHOR("Michal Nazarewicz");
|