uvc_configfs.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * uvc_configfs.c
  4. *
  5. * Configfs support for the uvc function.
  6. *
  7. * Copyright (c) 2014 Samsung Electronics Co., Ltd.
  8. * http://www.samsung.com
  9. *
  10. * Author: Andrzej Pietrasiewicz <[email protected]>
  11. */
  12. #include "uvc_configfs.h"
  13. #include <linux/sort.h>
  14. /* -----------------------------------------------------------------------------
  15. * Global Utility Structures and Macros
  16. */
  17. #define UVC_ATTR(prefix, cname, aname) \
  18. static struct configfs_attribute prefix##attr_##cname = { \
  19. .ca_name = __stringify(aname), \
  20. .ca_mode = S_IRUGO | S_IWUGO, \
  21. .ca_owner = THIS_MODULE, \
  22. .show = prefix##cname##_show, \
  23. .store = prefix##cname##_store, \
  24. }
  25. #define UVC_ATTR_RO(prefix, cname, aname) \
  26. static struct configfs_attribute prefix##attr_##cname = { \
  27. .ca_name = __stringify(aname), \
  28. .ca_mode = S_IRUGO, \
  29. .ca_owner = THIS_MODULE, \
  30. .show = prefix##cname##_show, \
  31. }
  32. #define le8_to_cpu(x) (x)
  33. #define cpu_to_le8(x) (x)
  34. static int uvcg_config_compare_u32(const void *l, const void *r)
  35. {
  36. u32 li = *(const u32 *)l;
  37. u32 ri = *(const u32 *)r;
  38. return li < ri ? -1 : li == ri ? 0 : 1;
  39. }
  40. struct uvcg_config_group_type {
  41. struct config_item_type type;
  42. const char *name;
  43. const struct uvcg_config_group_type **children;
  44. int (*create_children)(struct config_group *group);
  45. };
  46. static void uvcg_config_item_release(struct config_item *item)
  47. {
  48. struct config_group *group = to_config_group(item);
  49. kfree(group);
  50. }
  51. static struct configfs_item_operations uvcg_config_item_ops = {
  52. .release = uvcg_config_item_release,
  53. };
  54. static int uvcg_config_create_group(struct config_group *parent,
  55. const struct uvcg_config_group_type *type);
  56. static int uvcg_config_create_children(struct config_group *group,
  57. const struct uvcg_config_group_type *type)
  58. {
  59. const struct uvcg_config_group_type **child;
  60. int ret;
  61. if (type->create_children)
  62. return type->create_children(group);
  63. for (child = type->children; child && *child; ++child) {
  64. ret = uvcg_config_create_group(group, *child);
  65. if (ret < 0)
  66. return ret;
  67. }
  68. return 0;
  69. }
  70. static int uvcg_config_create_group(struct config_group *parent,
  71. const struct uvcg_config_group_type *type)
  72. {
  73. struct config_group *group;
  74. group = kzalloc(sizeof(*group), GFP_KERNEL);
  75. if (!group)
  76. return -ENOMEM;
  77. config_group_init_type_name(group, type->name, &type->type);
  78. configfs_add_default_group(group, parent);
  79. return uvcg_config_create_children(group, type);
  80. }
  81. static void uvcg_config_remove_children(struct config_group *group)
  82. {
  83. struct config_group *child, *n;
  84. list_for_each_entry_safe(child, n, &group->default_groups, group_entry) {
  85. list_del(&child->group_entry);
  86. uvcg_config_remove_children(child);
  87. config_item_put(&child->cg_item);
  88. }
  89. }
  90. /* -----------------------------------------------------------------------------
  91. * control/header/<NAME>
  92. * control/header
  93. */
  94. #define UVCG_CTRL_HDR_ATTR(cname, aname, bits, limit) \
  95. static ssize_t uvcg_control_header_##cname##_show( \
  96. struct config_item *item, char *page) \
  97. { \
  98. struct uvcg_control_header *ch = to_uvcg_control_header(item); \
  99. struct f_uvc_opts *opts; \
  100. struct config_item *opts_item; \
  101. struct mutex *su_mutex = &ch->item.ci_group->cg_subsys->su_mutex;\
  102. int result; \
  103. \
  104. mutex_lock(su_mutex); /* for navigating configfs hierarchy */ \
  105. \
  106. opts_item = ch->item.ci_parent->ci_parent->ci_parent; \
  107. opts = to_f_uvc_opts(opts_item); \
  108. \
  109. mutex_lock(&opts->lock); \
  110. result = sprintf(page, "%u\n", le##bits##_to_cpu(ch->desc.aname));\
  111. mutex_unlock(&opts->lock); \
  112. \
  113. mutex_unlock(su_mutex); \
  114. return result; \
  115. } \
  116. \
  117. static ssize_t \
  118. uvcg_control_header_##cname##_store(struct config_item *item, \
  119. const char *page, size_t len) \
  120. { \
  121. struct uvcg_control_header *ch = to_uvcg_control_header(item); \
  122. struct f_uvc_opts *opts; \
  123. struct config_item *opts_item; \
  124. struct mutex *su_mutex = &ch->item.ci_group->cg_subsys->su_mutex;\
  125. int ret; \
  126. u##bits num; \
  127. \
  128. mutex_lock(su_mutex); /* for navigating configfs hierarchy */ \
  129. \
  130. opts_item = ch->item.ci_parent->ci_parent->ci_parent; \
  131. opts = to_f_uvc_opts(opts_item); \
  132. \
  133. mutex_lock(&opts->lock); \
  134. if (ch->linked || opts->refcnt) { \
  135. ret = -EBUSY; \
  136. goto end; \
  137. } \
  138. \
  139. ret = kstrtou##bits(page, 0, &num); \
  140. if (ret) \
  141. goto end; \
  142. \
  143. if (num > limit) { \
  144. ret = -EINVAL; \
  145. goto end; \
  146. } \
  147. ch->desc.aname = cpu_to_le##bits(num); \
  148. ret = len; \
  149. end: \
  150. mutex_unlock(&opts->lock); \
  151. mutex_unlock(su_mutex); \
  152. return ret; \
  153. } \
  154. \
  155. UVC_ATTR(uvcg_control_header_, cname, aname)
  156. UVCG_CTRL_HDR_ATTR(bcd_uvc, bcdUVC, 16, 0xffff);
  157. UVCG_CTRL_HDR_ATTR(dw_clock_frequency, dwClockFrequency, 32, 0x7fffffff);
  158. #undef UVCG_CTRL_HDR_ATTR
  159. static struct configfs_attribute *uvcg_control_header_attrs[] = {
  160. &uvcg_control_header_attr_bcd_uvc,
  161. &uvcg_control_header_attr_dw_clock_frequency,
  162. NULL,
  163. };
  164. static const struct config_item_type uvcg_control_header_type = {
  165. .ct_item_ops = &uvcg_config_item_ops,
  166. .ct_attrs = uvcg_control_header_attrs,
  167. .ct_owner = THIS_MODULE,
  168. };
  169. static struct config_item *uvcg_control_header_make(struct config_group *group,
  170. const char *name)
  171. {
  172. struct uvcg_control_header *h;
  173. h = kzalloc(sizeof(*h), GFP_KERNEL);
  174. if (!h)
  175. return ERR_PTR(-ENOMEM);
  176. h->desc.bLength = UVC_DT_HEADER_SIZE(1);
  177. h->desc.bDescriptorType = USB_DT_CS_INTERFACE;
  178. h->desc.bDescriptorSubType = UVC_VC_HEADER;
  179. h->desc.bcdUVC = cpu_to_le16(0x0110);
  180. h->desc.dwClockFrequency = cpu_to_le32(48000000);
  181. config_item_init_type_name(&h->item, name, &uvcg_control_header_type);
  182. return &h->item;
  183. }
  184. static struct configfs_group_operations uvcg_control_header_grp_ops = {
  185. .make_item = uvcg_control_header_make,
  186. };
  187. static const struct uvcg_config_group_type uvcg_control_header_grp_type = {
  188. .type = {
  189. .ct_item_ops = &uvcg_config_item_ops,
  190. .ct_group_ops = &uvcg_control_header_grp_ops,
  191. .ct_owner = THIS_MODULE,
  192. },
  193. .name = "header",
  194. };
  195. /* -----------------------------------------------------------------------------
  196. * control/processing/default
  197. */
  198. #define UVCG_DEFAULT_PROCESSING_ATTR(cname, aname, bits) \
  199. static ssize_t uvcg_default_processing_##cname##_show( \
  200. struct config_item *item, char *page) \
  201. { \
  202. struct config_group *group = to_config_group(item); \
  203. struct f_uvc_opts *opts; \
  204. struct config_item *opts_item; \
  205. struct mutex *su_mutex = &group->cg_subsys->su_mutex; \
  206. struct uvc_processing_unit_descriptor *pd; \
  207. int result; \
  208. \
  209. mutex_lock(su_mutex); /* for navigating configfs hierarchy */ \
  210. \
  211. opts_item = group->cg_item.ci_parent->ci_parent->ci_parent; \
  212. opts = to_f_uvc_opts(opts_item); \
  213. pd = &opts->uvc_processing; \
  214. \
  215. mutex_lock(&opts->lock); \
  216. result = sprintf(page, "%u\n", le##bits##_to_cpu(pd->aname)); \
  217. mutex_unlock(&opts->lock); \
  218. \
  219. mutex_unlock(su_mutex); \
  220. return result; \
  221. } \
  222. \
  223. UVC_ATTR_RO(uvcg_default_processing_, cname, aname)
  224. UVCG_DEFAULT_PROCESSING_ATTR(b_unit_id, bUnitID, 8);
  225. UVCG_DEFAULT_PROCESSING_ATTR(b_source_id, bSourceID, 8);
  226. UVCG_DEFAULT_PROCESSING_ATTR(w_max_multiplier, wMaxMultiplier, 16);
  227. UVCG_DEFAULT_PROCESSING_ATTR(i_processing, iProcessing, 8);
  228. #undef UVCG_DEFAULT_PROCESSING_ATTR
  229. static ssize_t uvcg_default_processing_bm_controls_show(
  230. struct config_item *item, char *page)
  231. {
  232. struct config_group *group = to_config_group(item);
  233. struct f_uvc_opts *opts;
  234. struct config_item *opts_item;
  235. struct mutex *su_mutex = &group->cg_subsys->su_mutex;
  236. struct uvc_processing_unit_descriptor *pd;
  237. int result, i;
  238. char *pg = page;
  239. mutex_lock(su_mutex); /* for navigating configfs hierarchy */
  240. opts_item = group->cg_item.ci_parent->ci_parent->ci_parent;
  241. opts = to_f_uvc_opts(opts_item);
  242. pd = &opts->uvc_processing;
  243. mutex_lock(&opts->lock);
  244. for (result = 0, i = 0; i < pd->bControlSize; ++i) {
  245. result += sprintf(pg, "%u\n", pd->bmControls[i]);
  246. pg = page + result;
  247. }
  248. mutex_unlock(&opts->lock);
  249. mutex_unlock(su_mutex);
  250. return result;
  251. }
  252. UVC_ATTR_RO(uvcg_default_processing_, bm_controls, bmControls);
  253. static struct configfs_attribute *uvcg_default_processing_attrs[] = {
  254. &uvcg_default_processing_attr_b_unit_id,
  255. &uvcg_default_processing_attr_b_source_id,
  256. &uvcg_default_processing_attr_w_max_multiplier,
  257. &uvcg_default_processing_attr_bm_controls,
  258. &uvcg_default_processing_attr_i_processing,
  259. NULL,
  260. };
  261. static const struct uvcg_config_group_type uvcg_default_processing_type = {
  262. .type = {
  263. .ct_item_ops = &uvcg_config_item_ops,
  264. .ct_attrs = uvcg_default_processing_attrs,
  265. .ct_owner = THIS_MODULE,
  266. },
  267. .name = "default",
  268. };
  269. /* -----------------------------------------------------------------------------
  270. * control/processing
  271. */
  272. static const struct uvcg_config_group_type uvcg_processing_grp_type = {
  273. .type = {
  274. .ct_item_ops = &uvcg_config_item_ops,
  275. .ct_owner = THIS_MODULE,
  276. },
  277. .name = "processing",
  278. .children = (const struct uvcg_config_group_type*[]) {
  279. &uvcg_default_processing_type,
  280. NULL,
  281. },
  282. };
  283. /* -----------------------------------------------------------------------------
  284. * control/terminal/camera/default
  285. */
  286. #define UVCG_DEFAULT_CAMERA_ATTR(cname, aname, bits) \
  287. static ssize_t uvcg_default_camera_##cname##_show( \
  288. struct config_item *item, char *page) \
  289. { \
  290. struct config_group *group = to_config_group(item); \
  291. struct f_uvc_opts *opts; \
  292. struct config_item *opts_item; \
  293. struct mutex *su_mutex = &group->cg_subsys->su_mutex; \
  294. struct uvc_camera_terminal_descriptor *cd; \
  295. int result; \
  296. \
  297. mutex_lock(su_mutex); /* for navigating configfs hierarchy */ \
  298. \
  299. opts_item = group->cg_item.ci_parent->ci_parent->ci_parent-> \
  300. ci_parent; \
  301. opts = to_f_uvc_opts(opts_item); \
  302. cd = &opts->uvc_camera_terminal; \
  303. \
  304. mutex_lock(&opts->lock); \
  305. result = sprintf(page, "%u\n", le##bits##_to_cpu(cd->aname)); \
  306. mutex_unlock(&opts->lock); \
  307. \
  308. mutex_unlock(su_mutex); \
  309. \
  310. return result; \
  311. } \
  312. \
  313. UVC_ATTR_RO(uvcg_default_camera_, cname, aname)
  314. UVCG_DEFAULT_CAMERA_ATTR(b_terminal_id, bTerminalID, 8);
  315. UVCG_DEFAULT_CAMERA_ATTR(w_terminal_type, wTerminalType, 16);
  316. UVCG_DEFAULT_CAMERA_ATTR(b_assoc_terminal, bAssocTerminal, 8);
  317. UVCG_DEFAULT_CAMERA_ATTR(i_terminal, iTerminal, 8);
  318. UVCG_DEFAULT_CAMERA_ATTR(w_objective_focal_length_min, wObjectiveFocalLengthMin,
  319. 16);
  320. UVCG_DEFAULT_CAMERA_ATTR(w_objective_focal_length_max, wObjectiveFocalLengthMax,
  321. 16);
  322. UVCG_DEFAULT_CAMERA_ATTR(w_ocular_focal_length, wOcularFocalLength,
  323. 16);
  324. #undef UVCG_DEFAULT_CAMERA_ATTR
  325. static ssize_t uvcg_default_camera_bm_controls_show(
  326. struct config_item *item, char *page)
  327. {
  328. struct config_group *group = to_config_group(item);
  329. struct f_uvc_opts *opts;
  330. struct config_item *opts_item;
  331. struct mutex *su_mutex = &group->cg_subsys->su_mutex;
  332. struct uvc_camera_terminal_descriptor *cd;
  333. int result, i;
  334. char *pg = page;
  335. mutex_lock(su_mutex); /* for navigating configfs hierarchy */
  336. opts_item = group->cg_item.ci_parent->ci_parent->ci_parent->
  337. ci_parent;
  338. opts = to_f_uvc_opts(opts_item);
  339. cd = &opts->uvc_camera_terminal;
  340. mutex_lock(&opts->lock);
  341. for (result = 0, i = 0; i < cd->bControlSize; ++i) {
  342. result += sprintf(pg, "%u\n", cd->bmControls[i]);
  343. pg = page + result;
  344. }
  345. mutex_unlock(&opts->lock);
  346. mutex_unlock(su_mutex);
  347. return result;
  348. }
  349. UVC_ATTR_RO(uvcg_default_camera_, bm_controls, bmControls);
  350. static struct configfs_attribute *uvcg_default_camera_attrs[] = {
  351. &uvcg_default_camera_attr_b_terminal_id,
  352. &uvcg_default_camera_attr_w_terminal_type,
  353. &uvcg_default_camera_attr_b_assoc_terminal,
  354. &uvcg_default_camera_attr_i_terminal,
  355. &uvcg_default_camera_attr_w_objective_focal_length_min,
  356. &uvcg_default_camera_attr_w_objective_focal_length_max,
  357. &uvcg_default_camera_attr_w_ocular_focal_length,
  358. &uvcg_default_camera_attr_bm_controls,
  359. NULL,
  360. };
  361. static const struct uvcg_config_group_type uvcg_default_camera_type = {
  362. .type = {
  363. .ct_item_ops = &uvcg_config_item_ops,
  364. .ct_attrs = uvcg_default_camera_attrs,
  365. .ct_owner = THIS_MODULE,
  366. },
  367. .name = "default",
  368. };
  369. /* -----------------------------------------------------------------------------
  370. * control/terminal/camera
  371. */
  372. static const struct uvcg_config_group_type uvcg_camera_grp_type = {
  373. .type = {
  374. .ct_item_ops = &uvcg_config_item_ops,
  375. .ct_owner = THIS_MODULE,
  376. },
  377. .name = "camera",
  378. .children = (const struct uvcg_config_group_type*[]) {
  379. &uvcg_default_camera_type,
  380. NULL,
  381. },
  382. };
  383. /* -----------------------------------------------------------------------------
  384. * control/terminal/output/default
  385. */
  386. #define UVCG_DEFAULT_OUTPUT_ATTR(cname, aname, bits) \
  387. static ssize_t uvcg_default_output_##cname##_show( \
  388. struct config_item *item, char *page) \
  389. { \
  390. struct config_group *group = to_config_group(item); \
  391. struct f_uvc_opts *opts; \
  392. struct config_item *opts_item; \
  393. struct mutex *su_mutex = &group->cg_subsys->su_mutex; \
  394. struct uvc_output_terminal_descriptor *cd; \
  395. int result; \
  396. \
  397. mutex_lock(su_mutex); /* for navigating configfs hierarchy */ \
  398. \
  399. opts_item = group->cg_item.ci_parent->ci_parent-> \
  400. ci_parent->ci_parent; \
  401. opts = to_f_uvc_opts(opts_item); \
  402. cd = &opts->uvc_output_terminal; \
  403. \
  404. mutex_lock(&opts->lock); \
  405. result = sprintf(page, "%u\n", le##bits##_to_cpu(cd->aname)); \
  406. mutex_unlock(&opts->lock); \
  407. \
  408. mutex_unlock(su_mutex); \
  409. \
  410. return result; \
  411. } \
  412. \
  413. UVC_ATTR_RO(uvcg_default_output_, cname, aname)
  414. UVCG_DEFAULT_OUTPUT_ATTR(b_terminal_id, bTerminalID, 8);
  415. UVCG_DEFAULT_OUTPUT_ATTR(w_terminal_type, wTerminalType, 16);
  416. UVCG_DEFAULT_OUTPUT_ATTR(b_assoc_terminal, bAssocTerminal, 8);
  417. UVCG_DEFAULT_OUTPUT_ATTR(i_terminal, iTerminal, 8);
  418. #undef UVCG_DEFAULT_OUTPUT_ATTR
  419. static ssize_t uvcg_default_output_b_source_id_show(struct config_item *item,
  420. char *page)
  421. {
  422. struct config_group *group = to_config_group(item);
  423. struct f_uvc_opts *opts;
  424. struct config_item *opts_item;
  425. struct mutex *su_mutex = &group->cg_subsys->su_mutex;
  426. struct uvc_output_terminal_descriptor *cd;
  427. int result;
  428. mutex_lock(su_mutex); /* for navigating configfs hierarchy */
  429. opts_item = group->cg_item.ci_parent->ci_parent->
  430. ci_parent->ci_parent;
  431. opts = to_f_uvc_opts(opts_item);
  432. cd = &opts->uvc_output_terminal;
  433. mutex_lock(&opts->lock);
  434. result = sprintf(page, "%u\n", le8_to_cpu(cd->bSourceID));
  435. mutex_unlock(&opts->lock);
  436. mutex_unlock(su_mutex);
  437. return result;
  438. }
  439. static ssize_t uvcg_default_output_b_source_id_store(struct config_item *item,
  440. const char *page, size_t len)
  441. {
  442. struct config_group *group = to_config_group(item);
  443. struct f_uvc_opts *opts;
  444. struct config_item *opts_item;
  445. struct mutex *su_mutex = &group->cg_subsys->su_mutex;
  446. struct uvc_output_terminal_descriptor *cd;
  447. int result;
  448. u8 num;
  449. result = kstrtou8(page, 0, &num);
  450. if (result)
  451. return result;
  452. mutex_lock(su_mutex); /* for navigating configfs hierarchy */
  453. opts_item = group->cg_item.ci_parent->ci_parent->
  454. ci_parent->ci_parent;
  455. opts = to_f_uvc_opts(opts_item);
  456. cd = &opts->uvc_output_terminal;
  457. mutex_lock(&opts->lock);
  458. cd->bSourceID = num;
  459. mutex_unlock(&opts->lock);
  460. mutex_unlock(su_mutex);
  461. return len;
  462. }
  463. UVC_ATTR(uvcg_default_output_, b_source_id, bSourceID);
  464. static struct configfs_attribute *uvcg_default_output_attrs[] = {
  465. &uvcg_default_output_attr_b_terminal_id,
  466. &uvcg_default_output_attr_w_terminal_type,
  467. &uvcg_default_output_attr_b_assoc_terminal,
  468. &uvcg_default_output_attr_b_source_id,
  469. &uvcg_default_output_attr_i_terminal,
  470. NULL,
  471. };
  472. static const struct uvcg_config_group_type uvcg_default_output_type = {
  473. .type = {
  474. .ct_item_ops = &uvcg_config_item_ops,
  475. .ct_attrs = uvcg_default_output_attrs,
  476. .ct_owner = THIS_MODULE,
  477. },
  478. .name = "default",
  479. };
  480. /* -----------------------------------------------------------------------------
  481. * control/terminal/output
  482. */
  483. static const struct uvcg_config_group_type uvcg_output_grp_type = {
  484. .type = {
  485. .ct_item_ops = &uvcg_config_item_ops,
  486. .ct_owner = THIS_MODULE,
  487. },
  488. .name = "output",
  489. .children = (const struct uvcg_config_group_type*[]) {
  490. &uvcg_default_output_type,
  491. NULL,
  492. },
  493. };
  494. /* -----------------------------------------------------------------------------
  495. * control/terminal
  496. */
  497. static const struct uvcg_config_group_type uvcg_terminal_grp_type = {
  498. .type = {
  499. .ct_item_ops = &uvcg_config_item_ops,
  500. .ct_owner = THIS_MODULE,
  501. },
  502. .name = "terminal",
  503. .children = (const struct uvcg_config_group_type*[]) {
  504. &uvcg_camera_grp_type,
  505. &uvcg_output_grp_type,
  506. NULL,
  507. },
  508. };
  509. /* -----------------------------------------------------------------------------
  510. * control/class/{fs|ss}
  511. */
  512. struct uvcg_control_class_group {
  513. struct config_group group;
  514. const char *name;
  515. };
  516. static inline struct uvc_descriptor_header
  517. **uvcg_get_ctl_class_arr(struct config_item *i, struct f_uvc_opts *o)
  518. {
  519. struct uvcg_control_class_group *group =
  520. container_of(i, struct uvcg_control_class_group,
  521. group.cg_item);
  522. if (!strcmp(group->name, "fs"))
  523. return o->uvc_fs_control_cls;
  524. if (!strcmp(group->name, "ss"))
  525. return o->uvc_ss_control_cls;
  526. return NULL;
  527. }
  528. static int uvcg_control_class_allow_link(struct config_item *src,
  529. struct config_item *target)
  530. {
  531. struct config_item *control, *header;
  532. struct f_uvc_opts *opts;
  533. struct mutex *su_mutex = &src->ci_group->cg_subsys->su_mutex;
  534. struct uvc_descriptor_header **class_array;
  535. struct uvcg_control_header *target_hdr;
  536. int ret = -EINVAL;
  537. mutex_lock(su_mutex); /* for navigating configfs hierarchy */
  538. control = src->ci_parent->ci_parent;
  539. header = config_group_find_item(to_config_group(control), "header");
  540. if (!header || target->ci_parent != header)
  541. goto out;
  542. opts = to_f_uvc_opts(control->ci_parent);
  543. mutex_lock(&opts->lock);
  544. class_array = uvcg_get_ctl_class_arr(src, opts);
  545. if (!class_array)
  546. goto unlock;
  547. if (opts->refcnt || class_array[0]) {
  548. ret = -EBUSY;
  549. goto unlock;
  550. }
  551. target_hdr = to_uvcg_control_header(target);
  552. ++target_hdr->linked;
  553. class_array[0] = (struct uvc_descriptor_header *)&target_hdr->desc;
  554. ret = 0;
  555. unlock:
  556. mutex_unlock(&opts->lock);
  557. out:
  558. config_item_put(header);
  559. mutex_unlock(su_mutex);
  560. return ret;
  561. }
  562. static void uvcg_control_class_drop_link(struct config_item *src,
  563. struct config_item *target)
  564. {
  565. struct config_item *control, *header;
  566. struct f_uvc_opts *opts;
  567. struct mutex *su_mutex = &src->ci_group->cg_subsys->su_mutex;
  568. struct uvc_descriptor_header **class_array;
  569. struct uvcg_control_header *target_hdr;
  570. mutex_lock(su_mutex); /* for navigating configfs hierarchy */
  571. control = src->ci_parent->ci_parent;
  572. header = config_group_find_item(to_config_group(control), "header");
  573. if (!header || target->ci_parent != header)
  574. goto out;
  575. opts = to_f_uvc_opts(control->ci_parent);
  576. mutex_lock(&opts->lock);
  577. class_array = uvcg_get_ctl_class_arr(src, opts);
  578. if (!class_array || opts->refcnt)
  579. goto unlock;
  580. target_hdr = to_uvcg_control_header(target);
  581. --target_hdr->linked;
  582. class_array[0] = NULL;
  583. unlock:
  584. mutex_unlock(&opts->lock);
  585. out:
  586. config_item_put(header);
  587. mutex_unlock(su_mutex);
  588. }
  589. static struct configfs_item_operations uvcg_control_class_item_ops = {
  590. .release = uvcg_config_item_release,
  591. .allow_link = uvcg_control_class_allow_link,
  592. .drop_link = uvcg_control_class_drop_link,
  593. };
  594. static const struct config_item_type uvcg_control_class_type = {
  595. .ct_item_ops = &uvcg_control_class_item_ops,
  596. .ct_owner = THIS_MODULE,
  597. };
  598. /* -----------------------------------------------------------------------------
  599. * control/class
  600. */
  601. static int uvcg_control_class_create_children(struct config_group *parent)
  602. {
  603. static const char * const names[] = { "fs", "ss" };
  604. unsigned int i;
  605. for (i = 0; i < ARRAY_SIZE(names); ++i) {
  606. struct uvcg_control_class_group *group;
  607. group = kzalloc(sizeof(*group), GFP_KERNEL);
  608. if (!group)
  609. return -ENOMEM;
  610. group->name = names[i];
  611. config_group_init_type_name(&group->group, group->name,
  612. &uvcg_control_class_type);
  613. configfs_add_default_group(&group->group, parent);
  614. }
  615. return 0;
  616. }
  617. static const struct uvcg_config_group_type uvcg_control_class_grp_type = {
  618. .type = {
  619. .ct_item_ops = &uvcg_config_item_ops,
  620. .ct_owner = THIS_MODULE,
  621. },
  622. .name = "class",
  623. .create_children = uvcg_control_class_create_children,
  624. };
  625. /* -----------------------------------------------------------------------------
  626. * control
  627. */
  628. static ssize_t uvcg_default_control_b_interface_number_show(
  629. struct config_item *item, char *page)
  630. {
  631. struct config_group *group = to_config_group(item);
  632. struct mutex *su_mutex = &group->cg_subsys->su_mutex;
  633. struct config_item *opts_item;
  634. struct f_uvc_opts *opts;
  635. int result = 0;
  636. mutex_lock(su_mutex); /* for navigating configfs hierarchy */
  637. opts_item = item->ci_parent;
  638. opts = to_f_uvc_opts(opts_item);
  639. mutex_lock(&opts->lock);
  640. result += sprintf(page, "%u\n", opts->control_interface);
  641. mutex_unlock(&opts->lock);
  642. mutex_unlock(su_mutex);
  643. return result;
  644. }
  645. UVC_ATTR_RO(uvcg_default_control_, b_interface_number, bInterfaceNumber);
  646. static struct configfs_attribute *uvcg_default_control_attrs[] = {
  647. &uvcg_default_control_attr_b_interface_number,
  648. NULL,
  649. };
  650. static const struct uvcg_config_group_type uvcg_control_grp_type = {
  651. .type = {
  652. .ct_item_ops = &uvcg_config_item_ops,
  653. .ct_attrs = uvcg_default_control_attrs,
  654. .ct_owner = THIS_MODULE,
  655. },
  656. .name = "control",
  657. .children = (const struct uvcg_config_group_type*[]) {
  658. &uvcg_control_header_grp_type,
  659. &uvcg_processing_grp_type,
  660. &uvcg_terminal_grp_type,
  661. &uvcg_control_class_grp_type,
  662. NULL,
  663. },
  664. };
  665. /* -----------------------------------------------------------------------------
  666. * streaming/uncompressed
  667. * streaming/mjpeg
  668. */
  669. static const char * const uvcg_format_names[] = {
  670. "uncompressed",
  671. "mjpeg",
  672. };
  673. static ssize_t uvcg_format_bma_controls_show(struct uvcg_format *f, char *page)
  674. {
  675. struct f_uvc_opts *opts;
  676. struct config_item *opts_item;
  677. struct mutex *su_mutex = &f->group.cg_subsys->su_mutex;
  678. int result, i;
  679. char *pg = page;
  680. mutex_lock(su_mutex); /* for navigating configfs hierarchy */
  681. opts_item = f->group.cg_item.ci_parent->ci_parent->ci_parent;
  682. opts = to_f_uvc_opts(opts_item);
  683. mutex_lock(&opts->lock);
  684. result = sprintf(pg, "0x");
  685. pg += result;
  686. for (i = 0; i < UVCG_STREAMING_CONTROL_SIZE; ++i) {
  687. result += sprintf(pg, "%x\n", f->bmaControls[i]);
  688. pg = page + result;
  689. }
  690. mutex_unlock(&opts->lock);
  691. mutex_unlock(su_mutex);
  692. return result;
  693. }
  694. static ssize_t uvcg_format_bma_controls_store(struct uvcg_format *ch,
  695. const char *page, size_t len)
  696. {
  697. struct f_uvc_opts *opts;
  698. struct config_item *opts_item;
  699. struct mutex *su_mutex = &ch->group.cg_subsys->su_mutex;
  700. int ret = -EINVAL;
  701. mutex_lock(su_mutex); /* for navigating configfs hierarchy */
  702. opts_item = ch->group.cg_item.ci_parent->ci_parent->ci_parent;
  703. opts = to_f_uvc_opts(opts_item);
  704. mutex_lock(&opts->lock);
  705. if (ch->linked || opts->refcnt) {
  706. ret = -EBUSY;
  707. goto end;
  708. }
  709. if (len < 4 || *page != '0' ||
  710. (*(page + 1) != 'x' && *(page + 1) != 'X'))
  711. goto end;
  712. ret = hex2bin(ch->bmaControls, page + 2, 1);
  713. if (ret < 0)
  714. goto end;
  715. ret = len;
  716. end:
  717. mutex_unlock(&opts->lock);
  718. mutex_unlock(su_mutex);
  719. return ret;
  720. }
  721. /* -----------------------------------------------------------------------------
  722. * streaming/header/<NAME>
  723. * streaming/header
  724. */
  725. static void uvcg_format_set_indices(struct config_group *fmt);
  726. static int uvcg_streaming_header_allow_link(struct config_item *src,
  727. struct config_item *target)
  728. {
  729. struct mutex *su_mutex = &src->ci_group->cg_subsys->su_mutex;
  730. struct config_item *opts_item;
  731. struct f_uvc_opts *opts;
  732. struct uvcg_streaming_header *src_hdr;
  733. struct uvcg_format *target_fmt = NULL;
  734. struct uvcg_format_ptr *format_ptr;
  735. int i, ret = -EINVAL;
  736. src_hdr = to_uvcg_streaming_header(src);
  737. mutex_lock(su_mutex); /* for navigating configfs hierarchy */
  738. opts_item = src->ci_parent->ci_parent->ci_parent;
  739. opts = to_f_uvc_opts(opts_item);
  740. mutex_lock(&opts->lock);
  741. if (src_hdr->linked) {
  742. ret = -EBUSY;
  743. goto out;
  744. }
  745. /*
  746. * Linking is only allowed to direct children of the format nodes
  747. * (streaming/uncompressed or streaming/mjpeg nodes). First check that
  748. * the grand-parent of the target matches the grand-parent of the source
  749. * (the streaming node), and then verify that the target parent is a
  750. * format node.
  751. */
  752. if (src->ci_parent->ci_parent != target->ci_parent->ci_parent)
  753. goto out;
  754. for (i = 0; i < ARRAY_SIZE(uvcg_format_names); ++i) {
  755. if (!strcmp(target->ci_parent->ci_name, uvcg_format_names[i]))
  756. break;
  757. }
  758. if (i == ARRAY_SIZE(uvcg_format_names))
  759. goto out;
  760. target_fmt = container_of(to_config_group(target), struct uvcg_format,
  761. group);
  762. uvcg_format_set_indices(to_config_group(target));
  763. format_ptr = kzalloc(sizeof(*format_ptr), GFP_KERNEL);
  764. if (!format_ptr) {
  765. ret = -ENOMEM;
  766. goto out;
  767. }
  768. ret = 0;
  769. format_ptr->fmt = target_fmt;
  770. list_add_tail(&format_ptr->entry, &src_hdr->formats);
  771. ++src_hdr->num_fmt;
  772. ++target_fmt->linked;
  773. out:
  774. mutex_unlock(&opts->lock);
  775. mutex_unlock(su_mutex);
  776. return ret;
  777. }
  778. static void uvcg_streaming_header_drop_link(struct config_item *src,
  779. struct config_item *target)
  780. {
  781. struct mutex *su_mutex = &src->ci_group->cg_subsys->su_mutex;
  782. struct config_item *opts_item;
  783. struct f_uvc_opts *opts;
  784. struct uvcg_streaming_header *src_hdr;
  785. struct uvcg_format *target_fmt = NULL;
  786. struct uvcg_format_ptr *format_ptr, *tmp;
  787. src_hdr = to_uvcg_streaming_header(src);
  788. mutex_lock(su_mutex); /* for navigating configfs hierarchy */
  789. opts_item = src->ci_parent->ci_parent->ci_parent;
  790. opts = to_f_uvc_opts(opts_item);
  791. mutex_lock(&opts->lock);
  792. target_fmt = container_of(to_config_group(target), struct uvcg_format,
  793. group);
  794. list_for_each_entry_safe(format_ptr, tmp, &src_hdr->formats, entry)
  795. if (format_ptr->fmt == target_fmt) {
  796. list_del(&format_ptr->entry);
  797. kfree(format_ptr);
  798. --src_hdr->num_fmt;
  799. break;
  800. }
  801. --target_fmt->linked;
  802. mutex_unlock(&opts->lock);
  803. mutex_unlock(su_mutex);
  804. }
  805. static struct configfs_item_operations uvcg_streaming_header_item_ops = {
  806. .release = uvcg_config_item_release,
  807. .allow_link = uvcg_streaming_header_allow_link,
  808. .drop_link = uvcg_streaming_header_drop_link,
  809. };
  810. #define UVCG_STREAMING_HEADER_ATTR(cname, aname, bits) \
  811. static ssize_t uvcg_streaming_header_##cname##_show( \
  812. struct config_item *item, char *page) \
  813. { \
  814. struct uvcg_streaming_header *sh = to_uvcg_streaming_header(item); \
  815. struct f_uvc_opts *opts; \
  816. struct config_item *opts_item; \
  817. struct mutex *su_mutex = &sh->item.ci_group->cg_subsys->su_mutex;\
  818. int result; \
  819. \
  820. mutex_lock(su_mutex); /* for navigating configfs hierarchy */ \
  821. \
  822. opts_item = sh->item.ci_parent->ci_parent->ci_parent; \
  823. opts = to_f_uvc_opts(opts_item); \
  824. \
  825. mutex_lock(&opts->lock); \
  826. result = sprintf(page, "%u\n", le##bits##_to_cpu(sh->desc.aname));\
  827. mutex_unlock(&opts->lock); \
  828. \
  829. mutex_unlock(su_mutex); \
  830. return result; \
  831. } \
  832. \
  833. UVC_ATTR_RO(uvcg_streaming_header_, cname, aname)
  834. UVCG_STREAMING_HEADER_ATTR(bm_info, bmInfo, 8);
  835. UVCG_STREAMING_HEADER_ATTR(b_terminal_link, bTerminalLink, 8);
  836. UVCG_STREAMING_HEADER_ATTR(b_still_capture_method, bStillCaptureMethod, 8);
  837. UVCG_STREAMING_HEADER_ATTR(b_trigger_support, bTriggerSupport, 8);
  838. UVCG_STREAMING_HEADER_ATTR(b_trigger_usage, bTriggerUsage, 8);
  839. #undef UVCG_STREAMING_HEADER_ATTR
  840. static struct configfs_attribute *uvcg_streaming_header_attrs[] = {
  841. &uvcg_streaming_header_attr_bm_info,
  842. &uvcg_streaming_header_attr_b_terminal_link,
  843. &uvcg_streaming_header_attr_b_still_capture_method,
  844. &uvcg_streaming_header_attr_b_trigger_support,
  845. &uvcg_streaming_header_attr_b_trigger_usage,
  846. NULL,
  847. };
  848. static const struct config_item_type uvcg_streaming_header_type = {
  849. .ct_item_ops = &uvcg_streaming_header_item_ops,
  850. .ct_attrs = uvcg_streaming_header_attrs,
  851. .ct_owner = THIS_MODULE,
  852. };
  853. static struct config_item
  854. *uvcg_streaming_header_make(struct config_group *group, const char *name)
  855. {
  856. struct uvcg_streaming_header *h;
  857. h = kzalloc(sizeof(*h), GFP_KERNEL);
  858. if (!h)
  859. return ERR_PTR(-ENOMEM);
  860. INIT_LIST_HEAD(&h->formats);
  861. h->desc.bDescriptorType = USB_DT_CS_INTERFACE;
  862. h->desc.bDescriptorSubType = UVC_VS_INPUT_HEADER;
  863. h->desc.bTerminalLink = 3;
  864. h->desc.bControlSize = UVCG_STREAMING_CONTROL_SIZE;
  865. config_item_init_type_name(&h->item, name, &uvcg_streaming_header_type);
  866. return &h->item;
  867. }
  868. static struct configfs_group_operations uvcg_streaming_header_grp_ops = {
  869. .make_item = uvcg_streaming_header_make,
  870. };
  871. static const struct uvcg_config_group_type uvcg_streaming_header_grp_type = {
  872. .type = {
  873. .ct_item_ops = &uvcg_config_item_ops,
  874. .ct_group_ops = &uvcg_streaming_header_grp_ops,
  875. .ct_owner = THIS_MODULE,
  876. },
  877. .name = "header",
  878. };
  879. /* -----------------------------------------------------------------------------
  880. * streaming/<mode>/<format>/<NAME>
  881. */
  882. #define UVCG_FRAME_ATTR(cname, aname, bits) \
  883. static ssize_t uvcg_frame_##cname##_show(struct config_item *item, char *page)\
  884. { \
  885. struct uvcg_frame *f = to_uvcg_frame(item); \
  886. struct f_uvc_opts *opts; \
  887. struct config_item *opts_item; \
  888. struct mutex *su_mutex = &f->item.ci_group->cg_subsys->su_mutex;\
  889. int result; \
  890. \
  891. mutex_lock(su_mutex); /* for navigating configfs hierarchy */ \
  892. \
  893. opts_item = f->item.ci_parent->ci_parent->ci_parent->ci_parent; \
  894. opts = to_f_uvc_opts(opts_item); \
  895. \
  896. mutex_lock(&opts->lock); \
  897. result = sprintf(page, "%u\n", f->frame.cname); \
  898. mutex_unlock(&opts->lock); \
  899. \
  900. mutex_unlock(su_mutex); \
  901. return result; \
  902. } \
  903. \
  904. static ssize_t uvcg_frame_##cname##_store(struct config_item *item, \
  905. const char *page, size_t len)\
  906. { \
  907. struct uvcg_frame *f = to_uvcg_frame(item); \
  908. struct f_uvc_opts *opts; \
  909. struct config_item *opts_item; \
  910. struct uvcg_format *fmt; \
  911. struct mutex *su_mutex = &f->item.ci_group->cg_subsys->su_mutex;\
  912. typeof(f->frame.cname) num; \
  913. int ret; \
  914. \
  915. ret = kstrtou##bits(page, 0, &num); \
  916. if (ret) \
  917. return ret; \
  918. \
  919. mutex_lock(su_mutex); /* for navigating configfs hierarchy */ \
  920. \
  921. opts_item = f->item.ci_parent->ci_parent->ci_parent->ci_parent; \
  922. opts = to_f_uvc_opts(opts_item); \
  923. fmt = to_uvcg_format(f->item.ci_parent); \
  924. \
  925. mutex_lock(&opts->lock); \
  926. if (fmt->linked || opts->refcnt) { \
  927. ret = -EBUSY; \
  928. goto end; \
  929. } \
  930. \
  931. f->frame.cname = num; \
  932. ret = len; \
  933. end: \
  934. mutex_unlock(&opts->lock); \
  935. mutex_unlock(su_mutex); \
  936. return ret; \
  937. } \
  938. \
  939. UVC_ATTR(uvcg_frame_, cname, aname);
  940. static ssize_t uvcg_frame_b_frame_index_show(struct config_item *item,
  941. char *page)
  942. {
  943. struct uvcg_frame *f = to_uvcg_frame(item);
  944. struct uvcg_format *fmt;
  945. struct f_uvc_opts *opts;
  946. struct config_item *opts_item;
  947. struct config_item *fmt_item;
  948. struct mutex *su_mutex = &f->item.ci_group->cg_subsys->su_mutex;
  949. int result;
  950. mutex_lock(su_mutex); /* for navigating configfs hierarchy */
  951. fmt_item = f->item.ci_parent;
  952. fmt = to_uvcg_format(fmt_item);
  953. if (!fmt->linked) {
  954. result = -EBUSY;
  955. goto out;
  956. }
  957. opts_item = fmt_item->ci_parent->ci_parent->ci_parent;
  958. opts = to_f_uvc_opts(opts_item);
  959. mutex_lock(&opts->lock);
  960. result = sprintf(page, "%u\n", f->frame.b_frame_index);
  961. mutex_unlock(&opts->lock);
  962. out:
  963. mutex_unlock(su_mutex);
  964. return result;
  965. }
  966. UVC_ATTR_RO(uvcg_frame_, b_frame_index, bFrameIndex);
  967. UVCG_FRAME_ATTR(bm_capabilities, bmCapabilities, 8);
  968. UVCG_FRAME_ATTR(w_width, wWidth, 16);
  969. UVCG_FRAME_ATTR(w_height, wHeight, 16);
  970. UVCG_FRAME_ATTR(dw_min_bit_rate, dwMinBitRate, 32);
  971. UVCG_FRAME_ATTR(dw_max_bit_rate, dwMaxBitRate, 32);
  972. UVCG_FRAME_ATTR(dw_max_video_frame_buffer_size, dwMaxVideoFrameBufferSize, 32);
  973. UVCG_FRAME_ATTR(dw_default_frame_interval, dwDefaultFrameInterval, 32);
  974. #undef UVCG_FRAME_ATTR
  975. static ssize_t uvcg_frame_dw_frame_interval_show(struct config_item *item,
  976. char *page)
  977. {
  978. struct uvcg_frame *frm = to_uvcg_frame(item);
  979. struct f_uvc_opts *opts;
  980. struct config_item *opts_item;
  981. struct mutex *su_mutex = &frm->item.ci_group->cg_subsys->su_mutex;
  982. int result, i;
  983. char *pg = page;
  984. mutex_lock(su_mutex); /* for navigating configfs hierarchy */
  985. opts_item = frm->item.ci_parent->ci_parent->ci_parent->ci_parent;
  986. opts = to_f_uvc_opts(opts_item);
  987. mutex_lock(&opts->lock);
  988. for (result = 0, i = 0; i < frm->frame.b_frame_interval_type; ++i) {
  989. result += sprintf(pg, "%u\n", frm->dw_frame_interval[i]);
  990. pg = page + result;
  991. }
  992. mutex_unlock(&opts->lock);
  993. mutex_unlock(su_mutex);
  994. return result;
  995. }
  996. static inline int __uvcg_count_frm_intrv(char *buf, void *priv)
  997. {
  998. ++*((int *)priv);
  999. return 0;
  1000. }
  1001. static inline int __uvcg_fill_frm_intrv(char *buf, void *priv)
  1002. {
  1003. u32 num, **interv;
  1004. int ret;
  1005. ret = kstrtou32(buf, 0, &num);
  1006. if (ret)
  1007. return ret;
  1008. interv = priv;
  1009. **interv = num;
  1010. ++*interv;
  1011. return 0;
  1012. }
  1013. static int __uvcg_iter_frm_intrv(const char *page, size_t len,
  1014. int (*fun)(char *, void *), void *priv)
  1015. {
  1016. /* sign, base 2 representation, newline, terminator */
  1017. char buf[1 + sizeof(u32) * 8 + 1 + 1];
  1018. const char *pg = page;
  1019. int i, ret;
  1020. if (!fun)
  1021. return -EINVAL;
  1022. while (pg - page < len) {
  1023. i = 0;
  1024. while (i < sizeof(buf) && (pg - page < len) &&
  1025. *pg != '\0' && *pg != '\n')
  1026. buf[i++] = *pg++;
  1027. if (i == sizeof(buf))
  1028. return -EINVAL;
  1029. while ((pg - page < len) && (*pg == '\0' || *pg == '\n'))
  1030. ++pg;
  1031. buf[i] = '\0';
  1032. ret = fun(buf, priv);
  1033. if (ret)
  1034. return ret;
  1035. }
  1036. return 0;
  1037. }
  1038. static ssize_t uvcg_frame_dw_frame_interval_store(struct config_item *item,
  1039. const char *page, size_t len)
  1040. {
  1041. struct uvcg_frame *ch = to_uvcg_frame(item);
  1042. struct f_uvc_opts *opts;
  1043. struct config_item *opts_item;
  1044. struct uvcg_format *fmt;
  1045. struct mutex *su_mutex = &ch->item.ci_group->cg_subsys->su_mutex;
  1046. int ret = 0, n = 0;
  1047. u32 *frm_intrv, *tmp;
  1048. mutex_lock(su_mutex); /* for navigating configfs hierarchy */
  1049. opts_item = ch->item.ci_parent->ci_parent->ci_parent->ci_parent;
  1050. opts = to_f_uvc_opts(opts_item);
  1051. fmt = to_uvcg_format(ch->item.ci_parent);
  1052. mutex_lock(&opts->lock);
  1053. if (fmt->linked || opts->refcnt) {
  1054. ret = -EBUSY;
  1055. goto end;
  1056. }
  1057. ret = __uvcg_iter_frm_intrv(page, len, __uvcg_count_frm_intrv, &n);
  1058. if (ret)
  1059. goto end;
  1060. tmp = frm_intrv = kcalloc(n, sizeof(u32), GFP_KERNEL);
  1061. if (!frm_intrv) {
  1062. ret = -ENOMEM;
  1063. goto end;
  1064. }
  1065. ret = __uvcg_iter_frm_intrv(page, len, __uvcg_fill_frm_intrv, &tmp);
  1066. if (ret) {
  1067. kfree(frm_intrv);
  1068. goto end;
  1069. }
  1070. kfree(ch->dw_frame_interval);
  1071. ch->dw_frame_interval = frm_intrv;
  1072. ch->frame.b_frame_interval_type = n;
  1073. sort(ch->dw_frame_interval, n, sizeof(*ch->dw_frame_interval),
  1074. uvcg_config_compare_u32, NULL);
  1075. ret = len;
  1076. end:
  1077. mutex_unlock(&opts->lock);
  1078. mutex_unlock(su_mutex);
  1079. return ret;
  1080. }
  1081. UVC_ATTR(uvcg_frame_, dw_frame_interval, dwFrameInterval);
  1082. static struct configfs_attribute *uvcg_frame_attrs[] = {
  1083. &uvcg_frame_attr_b_frame_index,
  1084. &uvcg_frame_attr_bm_capabilities,
  1085. &uvcg_frame_attr_w_width,
  1086. &uvcg_frame_attr_w_height,
  1087. &uvcg_frame_attr_dw_min_bit_rate,
  1088. &uvcg_frame_attr_dw_max_bit_rate,
  1089. &uvcg_frame_attr_dw_max_video_frame_buffer_size,
  1090. &uvcg_frame_attr_dw_default_frame_interval,
  1091. &uvcg_frame_attr_dw_frame_interval,
  1092. NULL,
  1093. };
  1094. static const struct config_item_type uvcg_frame_type = {
  1095. .ct_item_ops = &uvcg_config_item_ops,
  1096. .ct_attrs = uvcg_frame_attrs,
  1097. .ct_owner = THIS_MODULE,
  1098. };
  1099. static struct config_item *uvcg_frame_make(struct config_group *group,
  1100. const char *name)
  1101. {
  1102. struct uvcg_frame *h;
  1103. struct uvcg_format *fmt;
  1104. struct f_uvc_opts *opts;
  1105. struct config_item *opts_item;
  1106. struct uvcg_frame_ptr *frame_ptr;
  1107. h = kzalloc(sizeof(*h), GFP_KERNEL);
  1108. if (!h)
  1109. return ERR_PTR(-ENOMEM);
  1110. h->frame.b_descriptor_type = USB_DT_CS_INTERFACE;
  1111. h->frame.b_frame_index = 1;
  1112. h->frame.w_width = 640;
  1113. h->frame.w_height = 360;
  1114. h->frame.dw_min_bit_rate = 18432000;
  1115. h->frame.dw_max_bit_rate = 55296000;
  1116. h->frame.dw_max_video_frame_buffer_size = 460800;
  1117. h->frame.dw_default_frame_interval = 666666;
  1118. opts_item = group->cg_item.ci_parent->ci_parent->ci_parent;
  1119. opts = to_f_uvc_opts(opts_item);
  1120. mutex_lock(&opts->lock);
  1121. fmt = to_uvcg_format(&group->cg_item);
  1122. if (fmt->type == UVCG_UNCOMPRESSED) {
  1123. h->frame.b_descriptor_subtype = UVC_VS_FRAME_UNCOMPRESSED;
  1124. h->fmt_type = UVCG_UNCOMPRESSED;
  1125. } else if (fmt->type == UVCG_MJPEG) {
  1126. h->frame.b_descriptor_subtype = UVC_VS_FRAME_MJPEG;
  1127. h->fmt_type = UVCG_MJPEG;
  1128. } else {
  1129. mutex_unlock(&opts->lock);
  1130. kfree(h);
  1131. return ERR_PTR(-EINVAL);
  1132. }
  1133. frame_ptr = kzalloc(sizeof(*frame_ptr), GFP_KERNEL);
  1134. if (!frame_ptr) {
  1135. mutex_unlock(&opts->lock);
  1136. kfree(h);
  1137. return ERR_PTR(-ENOMEM);
  1138. }
  1139. frame_ptr->frm = h;
  1140. list_add_tail(&frame_ptr->entry, &fmt->frames);
  1141. ++fmt->num_frames;
  1142. mutex_unlock(&opts->lock);
  1143. config_item_init_type_name(&h->item, name, &uvcg_frame_type);
  1144. return &h->item;
  1145. }
  1146. static void uvcg_frame_drop(struct config_group *group, struct config_item *item)
  1147. {
  1148. struct uvcg_format *fmt;
  1149. struct f_uvc_opts *opts;
  1150. struct config_item *opts_item;
  1151. struct uvcg_frame *target_frm = NULL;
  1152. struct uvcg_frame_ptr *frame_ptr, *tmp;
  1153. opts_item = group->cg_item.ci_parent->ci_parent->ci_parent;
  1154. opts = to_f_uvc_opts(opts_item);
  1155. mutex_lock(&opts->lock);
  1156. target_frm = container_of(item, struct uvcg_frame, item);
  1157. fmt = to_uvcg_format(&group->cg_item);
  1158. list_for_each_entry_safe(frame_ptr, tmp, &fmt->frames, entry)
  1159. if (frame_ptr->frm == target_frm) {
  1160. list_del(&frame_ptr->entry);
  1161. kfree(frame_ptr);
  1162. --fmt->num_frames;
  1163. break;
  1164. }
  1165. mutex_unlock(&opts->lock);
  1166. config_item_put(item);
  1167. }
  1168. static void uvcg_format_set_indices(struct config_group *fmt)
  1169. {
  1170. struct config_item *ci;
  1171. unsigned int i = 1;
  1172. list_for_each_entry(ci, &fmt->cg_children, ci_entry) {
  1173. struct uvcg_frame *frm;
  1174. if (ci->ci_type != &uvcg_frame_type)
  1175. continue;
  1176. frm = to_uvcg_frame(ci);
  1177. frm->frame.b_frame_index = i++;
  1178. }
  1179. }
  1180. /* -----------------------------------------------------------------------------
  1181. * streaming/uncompressed/<NAME>
  1182. */
  1183. static struct configfs_group_operations uvcg_uncompressed_group_ops = {
  1184. .make_item = uvcg_frame_make,
  1185. .drop_item = uvcg_frame_drop,
  1186. };
  1187. static ssize_t uvcg_uncompressed_guid_format_show(struct config_item *item,
  1188. char *page)
  1189. {
  1190. struct uvcg_uncompressed *ch = to_uvcg_uncompressed(item);
  1191. struct f_uvc_opts *opts;
  1192. struct config_item *opts_item;
  1193. struct mutex *su_mutex = &ch->fmt.group.cg_subsys->su_mutex;
  1194. mutex_lock(su_mutex); /* for navigating configfs hierarchy */
  1195. opts_item = ch->fmt.group.cg_item.ci_parent->ci_parent->ci_parent;
  1196. opts = to_f_uvc_opts(opts_item);
  1197. mutex_lock(&opts->lock);
  1198. memcpy(page, ch->desc.guidFormat, sizeof(ch->desc.guidFormat));
  1199. mutex_unlock(&opts->lock);
  1200. mutex_unlock(su_mutex);
  1201. return sizeof(ch->desc.guidFormat);
  1202. }
  1203. static ssize_t uvcg_uncompressed_guid_format_store(struct config_item *item,
  1204. const char *page, size_t len)
  1205. {
  1206. struct uvcg_uncompressed *ch = to_uvcg_uncompressed(item);
  1207. struct f_uvc_opts *opts;
  1208. struct config_item *opts_item;
  1209. struct mutex *su_mutex = &ch->fmt.group.cg_subsys->su_mutex;
  1210. int ret;
  1211. mutex_lock(su_mutex); /* for navigating configfs hierarchy */
  1212. opts_item = ch->fmt.group.cg_item.ci_parent->ci_parent->ci_parent;
  1213. opts = to_f_uvc_opts(opts_item);
  1214. mutex_lock(&opts->lock);
  1215. if (ch->fmt.linked || opts->refcnt) {
  1216. ret = -EBUSY;
  1217. goto end;
  1218. }
  1219. memcpy(ch->desc.guidFormat, page,
  1220. min(sizeof(ch->desc.guidFormat), len));
  1221. ret = sizeof(ch->desc.guidFormat);
  1222. end:
  1223. mutex_unlock(&opts->lock);
  1224. mutex_unlock(su_mutex);
  1225. return ret;
  1226. }
  1227. UVC_ATTR(uvcg_uncompressed_, guid_format, guidFormat);
  1228. #define UVCG_UNCOMPRESSED_ATTR_RO(cname, aname, bits) \
  1229. static ssize_t uvcg_uncompressed_##cname##_show( \
  1230. struct config_item *item, char *page) \
  1231. { \
  1232. struct uvcg_uncompressed *u = to_uvcg_uncompressed(item); \
  1233. struct f_uvc_opts *opts; \
  1234. struct config_item *opts_item; \
  1235. struct mutex *su_mutex = &u->fmt.group.cg_subsys->su_mutex; \
  1236. int result; \
  1237. \
  1238. mutex_lock(su_mutex); /* for navigating configfs hierarchy */ \
  1239. \
  1240. opts_item = u->fmt.group.cg_item.ci_parent->ci_parent->ci_parent;\
  1241. opts = to_f_uvc_opts(opts_item); \
  1242. \
  1243. mutex_lock(&opts->lock); \
  1244. result = sprintf(page, "%u\n", le##bits##_to_cpu(u->desc.aname));\
  1245. mutex_unlock(&opts->lock); \
  1246. \
  1247. mutex_unlock(su_mutex); \
  1248. return result; \
  1249. } \
  1250. \
  1251. UVC_ATTR_RO(uvcg_uncompressed_, cname, aname);
  1252. #define UVCG_UNCOMPRESSED_ATTR(cname, aname, bits) \
  1253. static ssize_t uvcg_uncompressed_##cname##_show( \
  1254. struct config_item *item, char *page) \
  1255. { \
  1256. struct uvcg_uncompressed *u = to_uvcg_uncompressed(item); \
  1257. struct f_uvc_opts *opts; \
  1258. struct config_item *opts_item; \
  1259. struct mutex *su_mutex = &u->fmt.group.cg_subsys->su_mutex; \
  1260. int result; \
  1261. \
  1262. mutex_lock(su_mutex); /* for navigating configfs hierarchy */ \
  1263. \
  1264. opts_item = u->fmt.group.cg_item.ci_parent->ci_parent->ci_parent;\
  1265. opts = to_f_uvc_opts(opts_item); \
  1266. \
  1267. mutex_lock(&opts->lock); \
  1268. result = sprintf(page, "%u\n", le##bits##_to_cpu(u->desc.aname));\
  1269. mutex_unlock(&opts->lock); \
  1270. \
  1271. mutex_unlock(su_mutex); \
  1272. return result; \
  1273. } \
  1274. \
  1275. static ssize_t \
  1276. uvcg_uncompressed_##cname##_store(struct config_item *item, \
  1277. const char *page, size_t len) \
  1278. { \
  1279. struct uvcg_uncompressed *u = to_uvcg_uncompressed(item); \
  1280. struct f_uvc_opts *opts; \
  1281. struct config_item *opts_item; \
  1282. struct mutex *su_mutex = &u->fmt.group.cg_subsys->su_mutex; \
  1283. int ret; \
  1284. u8 num; \
  1285. \
  1286. mutex_lock(su_mutex); /* for navigating configfs hierarchy */ \
  1287. \
  1288. opts_item = u->fmt.group.cg_item.ci_parent->ci_parent->ci_parent;\
  1289. opts = to_f_uvc_opts(opts_item); \
  1290. \
  1291. mutex_lock(&opts->lock); \
  1292. if (u->fmt.linked || opts->refcnt) { \
  1293. ret = -EBUSY; \
  1294. goto end; \
  1295. } \
  1296. \
  1297. ret = kstrtou8(page, 0, &num); \
  1298. if (ret) \
  1299. goto end; \
  1300. \
  1301. /* index values in uvc are never 0 */ \
  1302. if (!num) { \
  1303. ret = -EINVAL; \
  1304. goto end; \
  1305. } \
  1306. \
  1307. u->desc.aname = num; \
  1308. ret = len; \
  1309. end: \
  1310. mutex_unlock(&opts->lock); \
  1311. mutex_unlock(su_mutex); \
  1312. return ret; \
  1313. } \
  1314. \
  1315. UVC_ATTR(uvcg_uncompressed_, cname, aname);
  1316. UVCG_UNCOMPRESSED_ATTR_RO(b_format_index, bFormatIndex, 8);
  1317. UVCG_UNCOMPRESSED_ATTR(b_bits_per_pixel, bBitsPerPixel, 8);
  1318. UVCG_UNCOMPRESSED_ATTR(b_default_frame_index, bDefaultFrameIndex, 8);
  1319. UVCG_UNCOMPRESSED_ATTR_RO(b_aspect_ratio_x, bAspectRatioX, 8);
  1320. UVCG_UNCOMPRESSED_ATTR_RO(b_aspect_ratio_y, bAspectRatioY, 8);
  1321. UVCG_UNCOMPRESSED_ATTR_RO(bm_interlace_flags, bmInterlaceFlags, 8);
  1322. #undef UVCG_UNCOMPRESSED_ATTR
  1323. #undef UVCG_UNCOMPRESSED_ATTR_RO
  1324. static inline ssize_t
  1325. uvcg_uncompressed_bma_controls_show(struct config_item *item, char *page)
  1326. {
  1327. struct uvcg_uncompressed *unc = to_uvcg_uncompressed(item);
  1328. return uvcg_format_bma_controls_show(&unc->fmt, page);
  1329. }
  1330. static inline ssize_t
  1331. uvcg_uncompressed_bma_controls_store(struct config_item *item,
  1332. const char *page, size_t len)
  1333. {
  1334. struct uvcg_uncompressed *unc = to_uvcg_uncompressed(item);
  1335. return uvcg_format_bma_controls_store(&unc->fmt, page, len);
  1336. }
  1337. UVC_ATTR(uvcg_uncompressed_, bma_controls, bmaControls);
  1338. static struct configfs_attribute *uvcg_uncompressed_attrs[] = {
  1339. &uvcg_uncompressed_attr_b_format_index,
  1340. &uvcg_uncompressed_attr_guid_format,
  1341. &uvcg_uncompressed_attr_b_bits_per_pixel,
  1342. &uvcg_uncompressed_attr_b_default_frame_index,
  1343. &uvcg_uncompressed_attr_b_aspect_ratio_x,
  1344. &uvcg_uncompressed_attr_b_aspect_ratio_y,
  1345. &uvcg_uncompressed_attr_bm_interlace_flags,
  1346. &uvcg_uncompressed_attr_bma_controls,
  1347. NULL,
  1348. };
  1349. static const struct config_item_type uvcg_uncompressed_type = {
  1350. .ct_item_ops = &uvcg_config_item_ops,
  1351. .ct_group_ops = &uvcg_uncompressed_group_ops,
  1352. .ct_attrs = uvcg_uncompressed_attrs,
  1353. .ct_owner = THIS_MODULE,
  1354. };
  1355. static struct config_group *uvcg_uncompressed_make(struct config_group *group,
  1356. const char *name)
  1357. {
  1358. static char guid[] = {
  1359. 'Y', 'U', 'Y', '2', 0x00, 0x00, 0x10, 0x00,
  1360. 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71
  1361. };
  1362. struct uvcg_uncompressed *h;
  1363. h = kzalloc(sizeof(*h), GFP_KERNEL);
  1364. if (!h)
  1365. return ERR_PTR(-ENOMEM);
  1366. h->desc.bLength = UVC_DT_FORMAT_UNCOMPRESSED_SIZE;
  1367. h->desc.bDescriptorType = USB_DT_CS_INTERFACE;
  1368. h->desc.bDescriptorSubType = UVC_VS_FORMAT_UNCOMPRESSED;
  1369. memcpy(h->desc.guidFormat, guid, sizeof(guid));
  1370. h->desc.bBitsPerPixel = 16;
  1371. h->desc.bDefaultFrameIndex = 1;
  1372. h->desc.bAspectRatioX = 0;
  1373. h->desc.bAspectRatioY = 0;
  1374. h->desc.bmInterlaceFlags = 0;
  1375. h->desc.bCopyProtect = 0;
  1376. INIT_LIST_HEAD(&h->fmt.frames);
  1377. h->fmt.type = UVCG_UNCOMPRESSED;
  1378. config_group_init_type_name(&h->fmt.group, name,
  1379. &uvcg_uncompressed_type);
  1380. return &h->fmt.group;
  1381. }
  1382. static struct configfs_group_operations uvcg_uncompressed_grp_ops = {
  1383. .make_group = uvcg_uncompressed_make,
  1384. };
  1385. static const struct uvcg_config_group_type uvcg_uncompressed_grp_type = {
  1386. .type = {
  1387. .ct_item_ops = &uvcg_config_item_ops,
  1388. .ct_group_ops = &uvcg_uncompressed_grp_ops,
  1389. .ct_owner = THIS_MODULE,
  1390. },
  1391. .name = "uncompressed",
  1392. };
  1393. /* -----------------------------------------------------------------------------
  1394. * streaming/mjpeg/<NAME>
  1395. */
  1396. static struct configfs_group_operations uvcg_mjpeg_group_ops = {
  1397. .make_item = uvcg_frame_make,
  1398. .drop_item = uvcg_frame_drop,
  1399. };
  1400. #define UVCG_MJPEG_ATTR_RO(cname, aname, bits) \
  1401. static ssize_t uvcg_mjpeg_##cname##_show(struct config_item *item, char *page)\
  1402. { \
  1403. struct uvcg_mjpeg *u = to_uvcg_mjpeg(item); \
  1404. struct f_uvc_opts *opts; \
  1405. struct config_item *opts_item; \
  1406. struct mutex *su_mutex = &u->fmt.group.cg_subsys->su_mutex; \
  1407. int result; \
  1408. \
  1409. mutex_lock(su_mutex); /* for navigating configfs hierarchy */ \
  1410. \
  1411. opts_item = u->fmt.group.cg_item.ci_parent->ci_parent->ci_parent;\
  1412. opts = to_f_uvc_opts(opts_item); \
  1413. \
  1414. mutex_lock(&opts->lock); \
  1415. result = sprintf(page, "%u\n", le##bits##_to_cpu(u->desc.aname));\
  1416. mutex_unlock(&opts->lock); \
  1417. \
  1418. mutex_unlock(su_mutex); \
  1419. return result; \
  1420. } \
  1421. \
  1422. UVC_ATTR_RO(uvcg_mjpeg_, cname, aname)
  1423. #define UVCG_MJPEG_ATTR(cname, aname, bits) \
  1424. static ssize_t uvcg_mjpeg_##cname##_show(struct config_item *item, char *page)\
  1425. { \
  1426. struct uvcg_mjpeg *u = to_uvcg_mjpeg(item); \
  1427. struct f_uvc_opts *opts; \
  1428. struct config_item *opts_item; \
  1429. struct mutex *su_mutex = &u->fmt.group.cg_subsys->su_mutex; \
  1430. int result; \
  1431. \
  1432. mutex_lock(su_mutex); /* for navigating configfs hierarchy */ \
  1433. \
  1434. opts_item = u->fmt.group.cg_item.ci_parent->ci_parent->ci_parent;\
  1435. opts = to_f_uvc_opts(opts_item); \
  1436. \
  1437. mutex_lock(&opts->lock); \
  1438. result = sprintf(page, "%u\n", le##bits##_to_cpu(u->desc.aname));\
  1439. mutex_unlock(&opts->lock); \
  1440. \
  1441. mutex_unlock(su_mutex); \
  1442. return result; \
  1443. } \
  1444. \
  1445. static ssize_t \
  1446. uvcg_mjpeg_##cname##_store(struct config_item *item, \
  1447. const char *page, size_t len) \
  1448. { \
  1449. struct uvcg_mjpeg *u = to_uvcg_mjpeg(item); \
  1450. struct f_uvc_opts *opts; \
  1451. struct config_item *opts_item; \
  1452. struct mutex *su_mutex = &u->fmt.group.cg_subsys->su_mutex; \
  1453. int ret; \
  1454. u8 num; \
  1455. \
  1456. mutex_lock(su_mutex); /* for navigating configfs hierarchy */ \
  1457. \
  1458. opts_item = u->fmt.group.cg_item.ci_parent->ci_parent->ci_parent;\
  1459. opts = to_f_uvc_opts(opts_item); \
  1460. \
  1461. mutex_lock(&opts->lock); \
  1462. if (u->fmt.linked || opts->refcnt) { \
  1463. ret = -EBUSY; \
  1464. goto end; \
  1465. } \
  1466. \
  1467. ret = kstrtou8(page, 0, &num); \
  1468. if (ret) \
  1469. goto end; \
  1470. \
  1471. /* index values in uvc are never 0 */ \
  1472. if (!num) { \
  1473. ret = -EINVAL; \
  1474. goto end; \
  1475. } \
  1476. \
  1477. u->desc.aname = num; \
  1478. ret = len; \
  1479. end: \
  1480. mutex_unlock(&opts->lock); \
  1481. mutex_unlock(su_mutex); \
  1482. return ret; \
  1483. } \
  1484. \
  1485. UVC_ATTR(uvcg_mjpeg_, cname, aname)
  1486. UVCG_MJPEG_ATTR_RO(b_format_index, bFormatIndex, 8);
  1487. UVCG_MJPEG_ATTR(b_default_frame_index, bDefaultFrameIndex, 8);
  1488. UVCG_MJPEG_ATTR_RO(bm_flags, bmFlags, 8);
  1489. UVCG_MJPEG_ATTR_RO(b_aspect_ratio_x, bAspectRatioX, 8);
  1490. UVCG_MJPEG_ATTR_RO(b_aspect_ratio_y, bAspectRatioY, 8);
  1491. UVCG_MJPEG_ATTR_RO(bm_interlace_flags, bmInterlaceFlags, 8);
  1492. #undef UVCG_MJPEG_ATTR
  1493. #undef UVCG_MJPEG_ATTR_RO
  1494. static inline ssize_t
  1495. uvcg_mjpeg_bma_controls_show(struct config_item *item, char *page)
  1496. {
  1497. struct uvcg_mjpeg *u = to_uvcg_mjpeg(item);
  1498. return uvcg_format_bma_controls_show(&u->fmt, page);
  1499. }
  1500. static inline ssize_t
  1501. uvcg_mjpeg_bma_controls_store(struct config_item *item,
  1502. const char *page, size_t len)
  1503. {
  1504. struct uvcg_mjpeg *u = to_uvcg_mjpeg(item);
  1505. return uvcg_format_bma_controls_store(&u->fmt, page, len);
  1506. }
  1507. UVC_ATTR(uvcg_mjpeg_, bma_controls, bmaControls);
  1508. static struct configfs_attribute *uvcg_mjpeg_attrs[] = {
  1509. &uvcg_mjpeg_attr_b_format_index,
  1510. &uvcg_mjpeg_attr_b_default_frame_index,
  1511. &uvcg_mjpeg_attr_bm_flags,
  1512. &uvcg_mjpeg_attr_b_aspect_ratio_x,
  1513. &uvcg_mjpeg_attr_b_aspect_ratio_y,
  1514. &uvcg_mjpeg_attr_bm_interlace_flags,
  1515. &uvcg_mjpeg_attr_bma_controls,
  1516. NULL,
  1517. };
  1518. static const struct config_item_type uvcg_mjpeg_type = {
  1519. .ct_item_ops = &uvcg_config_item_ops,
  1520. .ct_group_ops = &uvcg_mjpeg_group_ops,
  1521. .ct_attrs = uvcg_mjpeg_attrs,
  1522. .ct_owner = THIS_MODULE,
  1523. };
  1524. static struct config_group *uvcg_mjpeg_make(struct config_group *group,
  1525. const char *name)
  1526. {
  1527. struct uvcg_mjpeg *h;
  1528. h = kzalloc(sizeof(*h), GFP_KERNEL);
  1529. if (!h)
  1530. return ERR_PTR(-ENOMEM);
  1531. h->desc.bLength = UVC_DT_FORMAT_MJPEG_SIZE;
  1532. h->desc.bDescriptorType = USB_DT_CS_INTERFACE;
  1533. h->desc.bDescriptorSubType = UVC_VS_FORMAT_MJPEG;
  1534. h->desc.bDefaultFrameIndex = 1;
  1535. h->desc.bAspectRatioX = 0;
  1536. h->desc.bAspectRatioY = 0;
  1537. h->desc.bmInterlaceFlags = 0;
  1538. h->desc.bCopyProtect = 0;
  1539. INIT_LIST_HEAD(&h->fmt.frames);
  1540. h->fmt.type = UVCG_MJPEG;
  1541. config_group_init_type_name(&h->fmt.group, name,
  1542. &uvcg_mjpeg_type);
  1543. return &h->fmt.group;
  1544. }
  1545. static struct configfs_group_operations uvcg_mjpeg_grp_ops = {
  1546. .make_group = uvcg_mjpeg_make,
  1547. };
  1548. static const struct uvcg_config_group_type uvcg_mjpeg_grp_type = {
  1549. .type = {
  1550. .ct_item_ops = &uvcg_config_item_ops,
  1551. .ct_group_ops = &uvcg_mjpeg_grp_ops,
  1552. .ct_owner = THIS_MODULE,
  1553. },
  1554. .name = "mjpeg",
  1555. };
  1556. /* -----------------------------------------------------------------------------
  1557. * streaming/color_matching/default
  1558. */
  1559. #define UVCG_DEFAULT_COLOR_MATCHING_ATTR(cname, aname, bits) \
  1560. static ssize_t uvcg_default_color_matching_##cname##_show( \
  1561. struct config_item *item, char *page) \
  1562. { \
  1563. struct config_group *group = to_config_group(item); \
  1564. struct f_uvc_opts *opts; \
  1565. struct config_item *opts_item; \
  1566. struct mutex *su_mutex = &group->cg_subsys->su_mutex; \
  1567. struct uvc_color_matching_descriptor *cd; \
  1568. int result; \
  1569. \
  1570. mutex_lock(su_mutex); /* for navigating configfs hierarchy */ \
  1571. \
  1572. opts_item = group->cg_item.ci_parent->ci_parent->ci_parent; \
  1573. opts = to_f_uvc_opts(opts_item); \
  1574. cd = &opts->uvc_color_matching; \
  1575. \
  1576. mutex_lock(&opts->lock); \
  1577. result = sprintf(page, "%u\n", le##bits##_to_cpu(cd->aname)); \
  1578. mutex_unlock(&opts->lock); \
  1579. \
  1580. mutex_unlock(su_mutex); \
  1581. return result; \
  1582. } \
  1583. \
  1584. UVC_ATTR_RO(uvcg_default_color_matching_, cname, aname)
  1585. UVCG_DEFAULT_COLOR_MATCHING_ATTR(b_color_primaries, bColorPrimaries, 8);
  1586. UVCG_DEFAULT_COLOR_MATCHING_ATTR(b_transfer_characteristics,
  1587. bTransferCharacteristics, 8);
  1588. UVCG_DEFAULT_COLOR_MATCHING_ATTR(b_matrix_coefficients, bMatrixCoefficients, 8);
  1589. #undef UVCG_DEFAULT_COLOR_MATCHING_ATTR
  1590. static struct configfs_attribute *uvcg_default_color_matching_attrs[] = {
  1591. &uvcg_default_color_matching_attr_b_color_primaries,
  1592. &uvcg_default_color_matching_attr_b_transfer_characteristics,
  1593. &uvcg_default_color_matching_attr_b_matrix_coefficients,
  1594. NULL,
  1595. };
  1596. static const struct uvcg_config_group_type uvcg_default_color_matching_type = {
  1597. .type = {
  1598. .ct_item_ops = &uvcg_config_item_ops,
  1599. .ct_attrs = uvcg_default_color_matching_attrs,
  1600. .ct_owner = THIS_MODULE,
  1601. },
  1602. .name = "default",
  1603. };
  1604. /* -----------------------------------------------------------------------------
  1605. * streaming/color_matching
  1606. */
  1607. static const struct uvcg_config_group_type uvcg_color_matching_grp_type = {
  1608. .type = {
  1609. .ct_item_ops = &uvcg_config_item_ops,
  1610. .ct_owner = THIS_MODULE,
  1611. },
  1612. .name = "color_matching",
  1613. .children = (const struct uvcg_config_group_type*[]) {
  1614. &uvcg_default_color_matching_type,
  1615. NULL,
  1616. },
  1617. };
  1618. /* -----------------------------------------------------------------------------
  1619. * streaming/class/{fs|hs|ss}
  1620. */
  1621. struct uvcg_streaming_class_group {
  1622. struct config_group group;
  1623. const char *name;
  1624. };
  1625. static inline struct uvc_descriptor_header
  1626. ***__uvcg_get_stream_class_arr(struct config_item *i, struct f_uvc_opts *o)
  1627. {
  1628. struct uvcg_streaming_class_group *group =
  1629. container_of(i, struct uvcg_streaming_class_group,
  1630. group.cg_item);
  1631. if (!strcmp(group->name, "fs"))
  1632. return &o->uvc_fs_streaming_cls;
  1633. if (!strcmp(group->name, "hs"))
  1634. return &o->uvc_hs_streaming_cls;
  1635. if (!strcmp(group->name, "ss"))
  1636. return &o->uvc_ss_streaming_cls;
  1637. return NULL;
  1638. }
  1639. enum uvcg_strm_type {
  1640. UVCG_HEADER = 0,
  1641. UVCG_FORMAT,
  1642. UVCG_FRAME
  1643. };
  1644. /*
  1645. * Iterate over a hierarchy of streaming descriptors' config items.
  1646. * The items are created by the user with configfs.
  1647. *
  1648. * It "processes" the header pointed to by @priv1, then for each format
  1649. * that follows the header "processes" the format itself and then for
  1650. * each frame inside a format "processes" the frame.
  1651. *
  1652. * As a "processing" function the @fun is used.
  1653. *
  1654. * __uvcg_iter_strm_cls() is used in two context: first, to calculate
  1655. * the amount of memory needed for an array of streaming descriptors
  1656. * and second, to actually fill the array.
  1657. *
  1658. * @h: streaming header pointer
  1659. * @priv2: an "inout" parameter (the caller might want to see the changes to it)
  1660. * @priv3: an "inout" parameter (the caller might want to see the changes to it)
  1661. * @fun: callback function for processing each level of the hierarchy
  1662. */
  1663. static int __uvcg_iter_strm_cls(struct uvcg_streaming_header *h,
  1664. void *priv2, void *priv3,
  1665. int (*fun)(void *, void *, void *, int, enum uvcg_strm_type type))
  1666. {
  1667. struct uvcg_format_ptr *f;
  1668. struct config_group *grp;
  1669. struct config_item *item;
  1670. struct uvcg_frame *frm;
  1671. int ret, i, j;
  1672. if (!fun)
  1673. return -EINVAL;
  1674. i = j = 0;
  1675. ret = fun(h, priv2, priv3, 0, UVCG_HEADER);
  1676. if (ret)
  1677. return ret;
  1678. list_for_each_entry(f, &h->formats, entry) {
  1679. ret = fun(f->fmt, priv2, priv3, i++, UVCG_FORMAT);
  1680. if (ret)
  1681. return ret;
  1682. grp = &f->fmt->group;
  1683. list_for_each_entry(item, &grp->cg_children, ci_entry) {
  1684. frm = to_uvcg_frame(item);
  1685. ret = fun(frm, priv2, priv3, j++, UVCG_FRAME);
  1686. if (ret)
  1687. return ret;
  1688. }
  1689. }
  1690. return ret;
  1691. }
  1692. /*
  1693. * Count how many bytes are needed for an array of streaming descriptors.
  1694. *
  1695. * @priv1: pointer to a header, format or frame
  1696. * @priv2: inout parameter, accumulated size of the array
  1697. * @priv3: inout parameter, accumulated number of the array elements
  1698. * @n: unused, this function's prototype must match @fun in __uvcg_iter_strm_cls
  1699. */
  1700. static int __uvcg_cnt_strm(void *priv1, void *priv2, void *priv3, int n,
  1701. enum uvcg_strm_type type)
  1702. {
  1703. size_t *size = priv2;
  1704. size_t *count = priv3;
  1705. switch (type) {
  1706. case UVCG_HEADER: {
  1707. struct uvcg_streaming_header *h = priv1;
  1708. *size += sizeof(h->desc);
  1709. /* bmaControls */
  1710. *size += h->num_fmt * UVCG_STREAMING_CONTROL_SIZE;
  1711. }
  1712. break;
  1713. case UVCG_FORMAT: {
  1714. struct uvcg_format *fmt = priv1;
  1715. if (fmt->type == UVCG_UNCOMPRESSED) {
  1716. struct uvcg_uncompressed *u =
  1717. container_of(fmt, struct uvcg_uncompressed,
  1718. fmt);
  1719. *size += sizeof(u->desc);
  1720. } else if (fmt->type == UVCG_MJPEG) {
  1721. struct uvcg_mjpeg *m =
  1722. container_of(fmt, struct uvcg_mjpeg, fmt);
  1723. *size += sizeof(m->desc);
  1724. } else {
  1725. return -EINVAL;
  1726. }
  1727. }
  1728. break;
  1729. case UVCG_FRAME: {
  1730. struct uvcg_frame *frm = priv1;
  1731. int sz = sizeof(frm->dw_frame_interval);
  1732. *size += sizeof(frm->frame);
  1733. *size += frm->frame.b_frame_interval_type * sz;
  1734. }
  1735. break;
  1736. }
  1737. ++*count;
  1738. return 0;
  1739. }
  1740. /*
  1741. * Fill an array of streaming descriptors.
  1742. *
  1743. * @priv1: pointer to a header, format or frame
  1744. * @priv2: inout parameter, pointer into a block of memory
  1745. * @priv3: inout parameter, pointer to a 2-dimensional array
  1746. */
  1747. static int __uvcg_fill_strm(void *priv1, void *priv2, void *priv3, int n,
  1748. enum uvcg_strm_type type)
  1749. {
  1750. void **dest = priv2;
  1751. struct uvc_descriptor_header ***array = priv3;
  1752. size_t sz;
  1753. **array = *dest;
  1754. ++*array;
  1755. switch (type) {
  1756. case UVCG_HEADER: {
  1757. struct uvc_input_header_descriptor *ihdr = *dest;
  1758. struct uvcg_streaming_header *h = priv1;
  1759. struct uvcg_format_ptr *f;
  1760. memcpy(*dest, &h->desc, sizeof(h->desc));
  1761. *dest += sizeof(h->desc);
  1762. sz = UVCG_STREAMING_CONTROL_SIZE;
  1763. list_for_each_entry(f, &h->formats, entry) {
  1764. memcpy(*dest, f->fmt->bmaControls, sz);
  1765. *dest += sz;
  1766. }
  1767. ihdr->bLength = sizeof(h->desc) + h->num_fmt * sz;
  1768. ihdr->bNumFormats = h->num_fmt;
  1769. }
  1770. break;
  1771. case UVCG_FORMAT: {
  1772. struct uvcg_format *fmt = priv1;
  1773. if (fmt->type == UVCG_UNCOMPRESSED) {
  1774. struct uvcg_uncompressed *u =
  1775. container_of(fmt, struct uvcg_uncompressed,
  1776. fmt);
  1777. u->desc.bFormatIndex = n + 1;
  1778. u->desc.bNumFrameDescriptors = fmt->num_frames;
  1779. memcpy(*dest, &u->desc, sizeof(u->desc));
  1780. *dest += sizeof(u->desc);
  1781. } else if (fmt->type == UVCG_MJPEG) {
  1782. struct uvcg_mjpeg *m =
  1783. container_of(fmt, struct uvcg_mjpeg, fmt);
  1784. m->desc.bFormatIndex = n + 1;
  1785. m->desc.bNumFrameDescriptors = fmt->num_frames;
  1786. memcpy(*dest, &m->desc, sizeof(m->desc));
  1787. *dest += sizeof(m->desc);
  1788. } else {
  1789. return -EINVAL;
  1790. }
  1791. }
  1792. break;
  1793. case UVCG_FRAME: {
  1794. struct uvcg_frame *frm = priv1;
  1795. struct uvc_descriptor_header *h = *dest;
  1796. sz = sizeof(frm->frame);
  1797. memcpy(*dest, &frm->frame, sz);
  1798. *dest += sz;
  1799. sz = frm->frame.b_frame_interval_type *
  1800. sizeof(*frm->dw_frame_interval);
  1801. memcpy(*dest, frm->dw_frame_interval, sz);
  1802. *dest += sz;
  1803. if (frm->fmt_type == UVCG_UNCOMPRESSED)
  1804. h->bLength = UVC_DT_FRAME_UNCOMPRESSED_SIZE(
  1805. frm->frame.b_frame_interval_type);
  1806. else if (frm->fmt_type == UVCG_MJPEG)
  1807. h->bLength = UVC_DT_FRAME_MJPEG_SIZE(
  1808. frm->frame.b_frame_interval_type);
  1809. }
  1810. break;
  1811. }
  1812. return 0;
  1813. }
  1814. static int uvcg_streaming_class_allow_link(struct config_item *src,
  1815. struct config_item *target)
  1816. {
  1817. struct config_item *streaming, *header;
  1818. struct f_uvc_opts *opts;
  1819. struct mutex *su_mutex = &src->ci_group->cg_subsys->su_mutex;
  1820. struct uvc_descriptor_header ***class_array, **cl_arr;
  1821. struct uvcg_streaming_header *target_hdr;
  1822. void *data, *data_save;
  1823. size_t size = 0, count = 0;
  1824. int ret = -EINVAL;
  1825. mutex_lock(su_mutex); /* for navigating configfs hierarchy */
  1826. streaming = src->ci_parent->ci_parent;
  1827. header = config_group_find_item(to_config_group(streaming), "header");
  1828. if (!header || target->ci_parent != header)
  1829. goto out;
  1830. opts = to_f_uvc_opts(streaming->ci_parent);
  1831. mutex_lock(&opts->lock);
  1832. class_array = __uvcg_get_stream_class_arr(src, opts);
  1833. if (!class_array || *class_array || opts->refcnt) {
  1834. ret = -EBUSY;
  1835. goto unlock;
  1836. }
  1837. target_hdr = to_uvcg_streaming_header(target);
  1838. ret = __uvcg_iter_strm_cls(target_hdr, &size, &count, __uvcg_cnt_strm);
  1839. if (ret)
  1840. goto unlock;
  1841. count += 2; /* color_matching, NULL */
  1842. *class_array = kcalloc(count, sizeof(void *), GFP_KERNEL);
  1843. if (!*class_array) {
  1844. ret = -ENOMEM;
  1845. goto unlock;
  1846. }
  1847. data = data_save = kzalloc(size, GFP_KERNEL);
  1848. if (!data) {
  1849. kfree(*class_array);
  1850. *class_array = NULL;
  1851. ret = -ENOMEM;
  1852. goto unlock;
  1853. }
  1854. cl_arr = *class_array;
  1855. ret = __uvcg_iter_strm_cls(target_hdr, &data, &cl_arr,
  1856. __uvcg_fill_strm);
  1857. if (ret) {
  1858. kfree(*class_array);
  1859. *class_array = NULL;
  1860. /*
  1861. * __uvcg_fill_strm() called from __uvcg_iter_stream_cls()
  1862. * might have advanced the "data", so use a backup copy
  1863. */
  1864. kfree(data_save);
  1865. goto unlock;
  1866. }
  1867. *cl_arr = (struct uvc_descriptor_header *)&opts->uvc_color_matching;
  1868. ++target_hdr->linked;
  1869. ret = 0;
  1870. unlock:
  1871. mutex_unlock(&opts->lock);
  1872. out:
  1873. config_item_put(header);
  1874. mutex_unlock(su_mutex);
  1875. return ret;
  1876. }
  1877. static void uvcg_streaming_class_drop_link(struct config_item *src,
  1878. struct config_item *target)
  1879. {
  1880. struct config_item *streaming, *header;
  1881. struct f_uvc_opts *opts;
  1882. struct mutex *su_mutex = &src->ci_group->cg_subsys->su_mutex;
  1883. struct uvc_descriptor_header ***class_array;
  1884. struct uvcg_streaming_header *target_hdr;
  1885. mutex_lock(su_mutex); /* for navigating configfs hierarchy */
  1886. streaming = src->ci_parent->ci_parent;
  1887. header = config_group_find_item(to_config_group(streaming), "header");
  1888. if (!header || target->ci_parent != header)
  1889. goto out;
  1890. opts = to_f_uvc_opts(streaming->ci_parent);
  1891. mutex_lock(&opts->lock);
  1892. class_array = __uvcg_get_stream_class_arr(src, opts);
  1893. if (!class_array || !*class_array)
  1894. goto unlock;
  1895. if (opts->refcnt)
  1896. goto unlock;
  1897. target_hdr = to_uvcg_streaming_header(target);
  1898. --target_hdr->linked;
  1899. kfree(**class_array);
  1900. kfree(*class_array);
  1901. *class_array = NULL;
  1902. unlock:
  1903. mutex_unlock(&opts->lock);
  1904. out:
  1905. config_item_put(header);
  1906. mutex_unlock(su_mutex);
  1907. }
  1908. static struct configfs_item_operations uvcg_streaming_class_item_ops = {
  1909. .release = uvcg_config_item_release,
  1910. .allow_link = uvcg_streaming_class_allow_link,
  1911. .drop_link = uvcg_streaming_class_drop_link,
  1912. };
  1913. static const struct config_item_type uvcg_streaming_class_type = {
  1914. .ct_item_ops = &uvcg_streaming_class_item_ops,
  1915. .ct_owner = THIS_MODULE,
  1916. };
  1917. /* -----------------------------------------------------------------------------
  1918. * streaming/class
  1919. */
  1920. static int uvcg_streaming_class_create_children(struct config_group *parent)
  1921. {
  1922. static const char * const names[] = { "fs", "hs", "ss" };
  1923. unsigned int i;
  1924. for (i = 0; i < ARRAY_SIZE(names); ++i) {
  1925. struct uvcg_streaming_class_group *group;
  1926. group = kzalloc(sizeof(*group), GFP_KERNEL);
  1927. if (!group)
  1928. return -ENOMEM;
  1929. group->name = names[i];
  1930. config_group_init_type_name(&group->group, group->name,
  1931. &uvcg_streaming_class_type);
  1932. configfs_add_default_group(&group->group, parent);
  1933. }
  1934. return 0;
  1935. }
  1936. static const struct uvcg_config_group_type uvcg_streaming_class_grp_type = {
  1937. .type = {
  1938. .ct_item_ops = &uvcg_config_item_ops,
  1939. .ct_owner = THIS_MODULE,
  1940. },
  1941. .name = "class",
  1942. .create_children = uvcg_streaming_class_create_children,
  1943. };
  1944. /* -----------------------------------------------------------------------------
  1945. * streaming
  1946. */
  1947. static ssize_t uvcg_default_streaming_b_interface_number_show(
  1948. struct config_item *item, char *page)
  1949. {
  1950. struct config_group *group = to_config_group(item);
  1951. struct mutex *su_mutex = &group->cg_subsys->su_mutex;
  1952. struct config_item *opts_item;
  1953. struct f_uvc_opts *opts;
  1954. int result = 0;
  1955. mutex_lock(su_mutex); /* for navigating configfs hierarchy */
  1956. opts_item = item->ci_parent;
  1957. opts = to_f_uvc_opts(opts_item);
  1958. mutex_lock(&opts->lock);
  1959. result += sprintf(page, "%u\n", opts->streaming_interface);
  1960. mutex_unlock(&opts->lock);
  1961. mutex_unlock(su_mutex);
  1962. return result;
  1963. }
  1964. UVC_ATTR_RO(uvcg_default_streaming_, b_interface_number, bInterfaceNumber);
  1965. static struct configfs_attribute *uvcg_default_streaming_attrs[] = {
  1966. &uvcg_default_streaming_attr_b_interface_number,
  1967. NULL,
  1968. };
  1969. static const struct uvcg_config_group_type uvcg_streaming_grp_type = {
  1970. .type = {
  1971. .ct_item_ops = &uvcg_config_item_ops,
  1972. .ct_attrs = uvcg_default_streaming_attrs,
  1973. .ct_owner = THIS_MODULE,
  1974. },
  1975. .name = "streaming",
  1976. .children = (const struct uvcg_config_group_type*[]) {
  1977. &uvcg_streaming_header_grp_type,
  1978. &uvcg_uncompressed_grp_type,
  1979. &uvcg_mjpeg_grp_type,
  1980. &uvcg_color_matching_grp_type,
  1981. &uvcg_streaming_class_grp_type,
  1982. NULL,
  1983. },
  1984. };
  1985. /* -----------------------------------------------------------------------------
  1986. * UVC function
  1987. */
  1988. static void uvc_func_item_release(struct config_item *item)
  1989. {
  1990. struct f_uvc_opts *opts = to_f_uvc_opts(item);
  1991. uvcg_config_remove_children(to_config_group(item));
  1992. usb_put_function_instance(&opts->func_inst);
  1993. }
  1994. static struct configfs_item_operations uvc_func_item_ops = {
  1995. .release = uvc_func_item_release,
  1996. };
  1997. #define UVCG_OPTS_ATTR(cname, aname, limit) \
  1998. static ssize_t f_uvc_opts_##cname##_show( \
  1999. struct config_item *item, char *page) \
  2000. { \
  2001. struct f_uvc_opts *opts = to_f_uvc_opts(item); \
  2002. int result; \
  2003. \
  2004. mutex_lock(&opts->lock); \
  2005. result = sprintf(page, "%u\n", opts->cname); \
  2006. mutex_unlock(&opts->lock); \
  2007. \
  2008. return result; \
  2009. } \
  2010. \
  2011. static ssize_t \
  2012. f_uvc_opts_##cname##_store(struct config_item *item, \
  2013. const char *page, size_t len) \
  2014. { \
  2015. struct f_uvc_opts *opts = to_f_uvc_opts(item); \
  2016. unsigned int num; \
  2017. int ret; \
  2018. \
  2019. mutex_lock(&opts->lock); \
  2020. if (opts->refcnt) { \
  2021. ret = -EBUSY; \
  2022. goto end; \
  2023. } \
  2024. \
  2025. ret = kstrtouint(page, 0, &num); \
  2026. if (ret) \
  2027. goto end; \
  2028. \
  2029. if (num > limit) { \
  2030. ret = -EINVAL; \
  2031. goto end; \
  2032. } \
  2033. opts->cname = num; \
  2034. ret = len; \
  2035. end: \
  2036. mutex_unlock(&opts->lock); \
  2037. return ret; \
  2038. } \
  2039. \
  2040. UVC_ATTR(f_uvc_opts_, cname, cname)
  2041. UVCG_OPTS_ATTR(streaming_interval, streaming_interval, 16);
  2042. UVCG_OPTS_ATTR(streaming_maxpacket, streaming_maxpacket, 3072);
  2043. UVCG_OPTS_ATTR(streaming_maxburst, streaming_maxburst, 15);
  2044. #undef UVCG_OPTS_ATTR
  2045. #define UVCG_OPTS_STRING_ATTR(cname, aname) \
  2046. static ssize_t f_uvc_opts_string_##cname##_show(struct config_item *item,\
  2047. char *page) \
  2048. { \
  2049. struct f_uvc_opts *opts = to_f_uvc_opts(item); \
  2050. int result; \
  2051. \
  2052. mutex_lock(&opts->lock); \
  2053. result = snprintf(page, sizeof(opts->aname), "%s", opts->aname);\
  2054. mutex_unlock(&opts->lock); \
  2055. \
  2056. return result; \
  2057. } \
  2058. \
  2059. static ssize_t f_uvc_opts_string_##cname##_store(struct config_item *item,\
  2060. const char *page, size_t len) \
  2061. { \
  2062. struct f_uvc_opts *opts = to_f_uvc_opts(item); \
  2063. int size = min(sizeof(opts->aname), len + 1); \
  2064. int ret = 0; \
  2065. \
  2066. mutex_lock(&opts->lock); \
  2067. if (opts->refcnt) { \
  2068. ret = -EBUSY; \
  2069. goto end; \
  2070. } \
  2071. \
  2072. ret = strscpy(opts->aname, page, size); \
  2073. if (ret == -E2BIG) \
  2074. ret = size - 1; \
  2075. \
  2076. end: \
  2077. mutex_unlock(&opts->lock); \
  2078. return ret; \
  2079. } \
  2080. \
  2081. UVC_ATTR(f_uvc_opts_string_, cname, aname)
  2082. UVCG_OPTS_STRING_ATTR(function_name, function_name);
  2083. #undef UVCG_OPTS_STRING_ATTR
  2084. static struct configfs_attribute *uvc_attrs[] = {
  2085. &f_uvc_opts_attr_streaming_interval,
  2086. &f_uvc_opts_attr_streaming_maxpacket,
  2087. &f_uvc_opts_attr_streaming_maxburst,
  2088. &f_uvc_opts_string_attr_function_name,
  2089. NULL,
  2090. };
  2091. static const struct uvcg_config_group_type uvc_func_type = {
  2092. .type = {
  2093. .ct_item_ops = &uvc_func_item_ops,
  2094. .ct_attrs = uvc_attrs,
  2095. .ct_owner = THIS_MODULE,
  2096. },
  2097. .name = "",
  2098. .children = (const struct uvcg_config_group_type*[]) {
  2099. &uvcg_control_grp_type,
  2100. &uvcg_streaming_grp_type,
  2101. NULL,
  2102. },
  2103. };
  2104. int uvcg_attach_configfs(struct f_uvc_opts *opts)
  2105. {
  2106. int ret;
  2107. config_group_init_type_name(&opts->func_inst.group, uvc_func_type.name,
  2108. &uvc_func_type.type);
  2109. ret = uvcg_config_create_children(&opts->func_inst.group,
  2110. &uvc_func_type);
  2111. if (ret < 0)
  2112. config_group_put(&opts->func_inst.group);
  2113. return ret;
  2114. }