uvc_configfs.c 66 KB

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