hid-core.c 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * HID support for Linux
  4. *
  5. * Copyright (c) 1999 Andreas Gal
  6. * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
  7. * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
  8. * Copyright (c) 2006-2012 Jiri Kosina
  9. */
  10. /*
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/init.h>
  16. #include <linux/kernel.h>
  17. #include <linux/list.h>
  18. #include <linux/mm.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/unaligned.h>
  21. #include <asm/byteorder.h>
  22. #include <linux/input.h>
  23. #include <linux/wait.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/sched.h>
  26. #include <linux/semaphore.h>
  27. #include <linux/hid.h>
  28. #include <linux/hiddev.h>
  29. #include <linux/hid-debug.h>
  30. #include <linux/hidraw.h>
  31. #include "hid-ids.h"
  32. /*
  33. * Version Information
  34. */
  35. #define DRIVER_DESC "HID core driver"
  36. static int hid_ignore_special_drivers = 0;
  37. module_param_named(ignore_special_drivers, hid_ignore_special_drivers, int, 0600);
  38. MODULE_PARM_DESC(ignore_special_drivers, "Ignore any special drivers and handle all devices by generic driver");
  39. /*
  40. * Convert a signed n-bit integer to signed 32-bit integer.
  41. */
  42. static s32 snto32(__u32 value, unsigned int n)
  43. {
  44. if (!value || !n)
  45. return 0;
  46. if (n > 32)
  47. n = 32;
  48. return sign_extend32(value, n - 1);
  49. }
  50. /*
  51. * Convert a signed 32-bit integer to a signed n-bit integer.
  52. */
  53. static u32 s32ton(__s32 value, unsigned int n)
  54. {
  55. s32 a;
  56. if (!value || !n)
  57. return 0;
  58. a = value >> (n - 1);
  59. if (a && a != -1)
  60. return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
  61. return value & ((1 << n) - 1);
  62. }
  63. /*
  64. * Register a new report for a device.
  65. */
  66. struct hid_report *hid_register_report(struct hid_device *device,
  67. enum hid_report_type type, unsigned int id,
  68. unsigned int application)
  69. {
  70. struct hid_report_enum *report_enum = device->report_enum + type;
  71. struct hid_report *report;
  72. if (id >= HID_MAX_IDS)
  73. return NULL;
  74. if (report_enum->report_id_hash[id])
  75. return report_enum->report_id_hash[id];
  76. report = kzalloc(sizeof(struct hid_report), GFP_KERNEL);
  77. if (!report)
  78. return NULL;
  79. if (id != 0)
  80. report_enum->numbered = 1;
  81. report->id = id;
  82. report->type = type;
  83. report->size = 0;
  84. report->device = device;
  85. report->application = application;
  86. report_enum->report_id_hash[id] = report;
  87. list_add_tail(&report->list, &report_enum->report_list);
  88. INIT_LIST_HEAD(&report->field_entry_list);
  89. return report;
  90. }
  91. EXPORT_SYMBOL_GPL(hid_register_report);
  92. /*
  93. * Register a new field for this report.
  94. */
  95. static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages)
  96. {
  97. struct hid_field *field;
  98. if (report->maxfield == HID_MAX_FIELDS) {
  99. hid_err(report->device, "too many fields in report\n");
  100. return NULL;
  101. }
  102. field = kvzalloc((sizeof(struct hid_field) +
  103. usages * sizeof(struct hid_usage) +
  104. 3 * usages * sizeof(unsigned int)), GFP_KERNEL);
  105. if (!field)
  106. return NULL;
  107. field->index = report->maxfield++;
  108. report->field[field->index] = field;
  109. field->usage = (struct hid_usage *)(field + 1);
  110. field->value = (s32 *)(field->usage + usages);
  111. field->new_value = (s32 *)(field->value + usages);
  112. field->usages_priorities = (s32 *)(field->new_value + usages);
  113. field->report = report;
  114. return field;
  115. }
  116. /*
  117. * Open a collection. The type/usage is pushed on the stack.
  118. */
  119. static int open_collection(struct hid_parser *parser, unsigned type)
  120. {
  121. struct hid_collection *collection;
  122. unsigned usage;
  123. int collection_index;
  124. usage = parser->local.usage[0];
  125. if (parser->collection_stack_ptr == parser->collection_stack_size) {
  126. unsigned int *collection_stack;
  127. unsigned int new_size = parser->collection_stack_size +
  128. HID_COLLECTION_STACK_SIZE;
  129. collection_stack = krealloc(parser->collection_stack,
  130. new_size * sizeof(unsigned int),
  131. GFP_KERNEL);
  132. if (!collection_stack)
  133. return -ENOMEM;
  134. parser->collection_stack = collection_stack;
  135. parser->collection_stack_size = new_size;
  136. }
  137. if (parser->device->maxcollection == parser->device->collection_size) {
  138. collection = kmalloc(
  139. array3_size(sizeof(struct hid_collection),
  140. parser->device->collection_size,
  141. 2),
  142. GFP_KERNEL);
  143. if (collection == NULL) {
  144. hid_err(parser->device, "failed to reallocate collection array\n");
  145. return -ENOMEM;
  146. }
  147. memcpy(collection, parser->device->collection,
  148. sizeof(struct hid_collection) *
  149. parser->device->collection_size);
  150. memset(collection + parser->device->collection_size, 0,
  151. sizeof(struct hid_collection) *
  152. parser->device->collection_size);
  153. kfree(parser->device->collection);
  154. parser->device->collection = collection;
  155. parser->device->collection_size *= 2;
  156. }
  157. parser->collection_stack[parser->collection_stack_ptr++] =
  158. parser->device->maxcollection;
  159. collection_index = parser->device->maxcollection++;
  160. collection = parser->device->collection + collection_index;
  161. collection->type = type;
  162. collection->usage = usage;
  163. collection->level = parser->collection_stack_ptr - 1;
  164. collection->parent_idx = (collection->level == 0) ? -1 :
  165. parser->collection_stack[collection->level - 1];
  166. if (type == HID_COLLECTION_APPLICATION)
  167. parser->device->maxapplication++;
  168. return 0;
  169. }
  170. /*
  171. * Close a collection.
  172. */
  173. static int close_collection(struct hid_parser *parser)
  174. {
  175. if (!parser->collection_stack_ptr) {
  176. hid_err(parser->device, "collection stack underflow\n");
  177. return -EINVAL;
  178. }
  179. parser->collection_stack_ptr--;
  180. return 0;
  181. }
  182. /*
  183. * Climb up the stack, search for the specified collection type
  184. * and return the usage.
  185. */
  186. static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
  187. {
  188. struct hid_collection *collection = parser->device->collection;
  189. int n;
  190. for (n = parser->collection_stack_ptr - 1; n >= 0; n--) {
  191. unsigned index = parser->collection_stack[n];
  192. if (collection[index].type == type)
  193. return collection[index].usage;
  194. }
  195. return 0; /* we know nothing about this usage type */
  196. }
  197. /*
  198. * Concatenate usage which defines 16 bits or less with the
  199. * currently defined usage page to form a 32 bit usage
  200. */
  201. static void complete_usage(struct hid_parser *parser, unsigned int index)
  202. {
  203. parser->local.usage[index] &= 0xFFFF;
  204. parser->local.usage[index] |=
  205. (parser->global.usage_page & 0xFFFF) << 16;
  206. }
  207. /*
  208. * Add a usage to the temporary parser table.
  209. */
  210. static int hid_add_usage(struct hid_parser *parser, unsigned usage, u8 size)
  211. {
  212. if (parser->local.usage_index >= HID_MAX_USAGES) {
  213. hid_err(parser->device, "usage index exceeded\n");
  214. return -1;
  215. }
  216. parser->local.usage[parser->local.usage_index] = usage;
  217. /*
  218. * If Usage item only includes usage id, concatenate it with
  219. * currently defined usage page
  220. */
  221. if (size <= 2)
  222. complete_usage(parser, parser->local.usage_index);
  223. parser->local.usage_size[parser->local.usage_index] = size;
  224. parser->local.collection_index[parser->local.usage_index] =
  225. parser->collection_stack_ptr ?
  226. parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
  227. parser->local.usage_index++;
  228. return 0;
  229. }
  230. /*
  231. * Register a new field for this report.
  232. */
  233. static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
  234. {
  235. struct hid_report *report;
  236. struct hid_field *field;
  237. unsigned int max_buffer_size = HID_MAX_BUFFER_SIZE;
  238. unsigned int usages;
  239. unsigned int offset;
  240. unsigned int i;
  241. unsigned int application;
  242. application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
  243. report = hid_register_report(parser->device, report_type,
  244. parser->global.report_id, application);
  245. if (!report) {
  246. hid_err(parser->device, "hid_register_report failed\n");
  247. return -1;
  248. }
  249. /* Handle both signed and unsigned cases properly */
  250. if ((parser->global.logical_minimum < 0 &&
  251. parser->global.logical_maximum <
  252. parser->global.logical_minimum) ||
  253. (parser->global.logical_minimum >= 0 &&
  254. (__u32)parser->global.logical_maximum <
  255. (__u32)parser->global.logical_minimum)) {
  256. dbg_hid("logical range invalid 0x%x 0x%x\n",
  257. parser->global.logical_minimum,
  258. parser->global.logical_maximum);
  259. return -1;
  260. }
  261. offset = report->size;
  262. report->size += parser->global.report_size * parser->global.report_count;
  263. if (parser->device->ll_driver->max_buffer_size)
  264. max_buffer_size = parser->device->ll_driver->max_buffer_size;
  265. /* Total size check: Allow for possible report index byte */
  266. if (report->size > (max_buffer_size - 1) << 3) {
  267. hid_err(parser->device, "report is too long\n");
  268. return -1;
  269. }
  270. if (!parser->local.usage_index) /* Ignore padding fields */
  271. return 0;
  272. usages = max_t(unsigned, parser->local.usage_index,
  273. parser->global.report_count);
  274. field = hid_register_field(report, usages);
  275. if (!field)
  276. return 0;
  277. field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
  278. field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
  279. field->application = application;
  280. for (i = 0; i < usages; i++) {
  281. unsigned j = i;
  282. /* Duplicate the last usage we parsed if we have excess values */
  283. if (i >= parser->local.usage_index)
  284. j = parser->local.usage_index - 1;
  285. field->usage[i].hid = parser->local.usage[j];
  286. field->usage[i].collection_index =
  287. parser->local.collection_index[j];
  288. field->usage[i].usage_index = i;
  289. field->usage[i].resolution_multiplier = 1;
  290. }
  291. field->maxusage = usages;
  292. field->flags = flags;
  293. field->report_offset = offset;
  294. field->report_type = report_type;
  295. field->report_size = parser->global.report_size;
  296. field->report_count = parser->global.report_count;
  297. field->logical_minimum = parser->global.logical_minimum;
  298. field->logical_maximum = parser->global.logical_maximum;
  299. field->physical_minimum = parser->global.physical_minimum;
  300. field->physical_maximum = parser->global.physical_maximum;
  301. field->unit_exponent = parser->global.unit_exponent;
  302. field->unit = parser->global.unit;
  303. return 0;
  304. }
  305. /*
  306. * Read data value from item.
  307. */
  308. static u32 item_udata(struct hid_item *item)
  309. {
  310. switch (item->size) {
  311. case 1: return item->data.u8;
  312. case 2: return item->data.u16;
  313. case 4: return item->data.u32;
  314. }
  315. return 0;
  316. }
  317. static s32 item_sdata(struct hid_item *item)
  318. {
  319. switch (item->size) {
  320. case 1: return item->data.s8;
  321. case 2: return item->data.s16;
  322. case 4: return item->data.s32;
  323. }
  324. return 0;
  325. }
  326. /*
  327. * Process a global item.
  328. */
  329. static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
  330. {
  331. __s32 raw_value;
  332. switch (item->tag) {
  333. case HID_GLOBAL_ITEM_TAG_PUSH:
  334. if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
  335. hid_err(parser->device, "global environment stack overflow\n");
  336. return -1;
  337. }
  338. memcpy(parser->global_stack + parser->global_stack_ptr++,
  339. &parser->global, sizeof(struct hid_global));
  340. return 0;
  341. case HID_GLOBAL_ITEM_TAG_POP:
  342. if (!parser->global_stack_ptr) {
  343. hid_err(parser->device, "global environment stack underflow\n");
  344. return -1;
  345. }
  346. memcpy(&parser->global, parser->global_stack +
  347. --parser->global_stack_ptr, sizeof(struct hid_global));
  348. return 0;
  349. case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
  350. parser->global.usage_page = item_udata(item);
  351. return 0;
  352. case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
  353. parser->global.logical_minimum = item_sdata(item);
  354. return 0;
  355. case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
  356. if (parser->global.logical_minimum < 0)
  357. parser->global.logical_maximum = item_sdata(item);
  358. else
  359. parser->global.logical_maximum = item_udata(item);
  360. return 0;
  361. case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
  362. parser->global.physical_minimum = item_sdata(item);
  363. return 0;
  364. case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
  365. if (parser->global.physical_minimum < 0)
  366. parser->global.physical_maximum = item_sdata(item);
  367. else
  368. parser->global.physical_maximum = item_udata(item);
  369. return 0;
  370. case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
  371. /* Many devices provide unit exponent as a two's complement
  372. * nibble due to the common misunderstanding of HID
  373. * specification 1.11, 6.2.2.7 Global Items. Attempt to handle
  374. * both this and the standard encoding. */
  375. raw_value = item_sdata(item);
  376. if (!(raw_value & 0xfffffff0))
  377. parser->global.unit_exponent = snto32(raw_value, 4);
  378. else
  379. parser->global.unit_exponent = raw_value;
  380. return 0;
  381. case HID_GLOBAL_ITEM_TAG_UNIT:
  382. parser->global.unit = item_udata(item);
  383. return 0;
  384. case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
  385. parser->global.report_size = item_udata(item);
  386. if (parser->global.report_size > 256) {
  387. hid_err(parser->device, "invalid report_size %d\n",
  388. parser->global.report_size);
  389. return -1;
  390. }
  391. return 0;
  392. case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
  393. parser->global.report_count = item_udata(item);
  394. if (parser->global.report_count > HID_MAX_USAGES) {
  395. hid_err(parser->device, "invalid report_count %d\n",
  396. parser->global.report_count);
  397. return -1;
  398. }
  399. return 0;
  400. case HID_GLOBAL_ITEM_TAG_REPORT_ID:
  401. parser->global.report_id = item_udata(item);
  402. if (parser->global.report_id == 0 ||
  403. parser->global.report_id >= HID_MAX_IDS) {
  404. hid_err(parser->device, "report_id %u is invalid\n",
  405. parser->global.report_id);
  406. return -1;
  407. }
  408. return 0;
  409. default:
  410. hid_err(parser->device, "unknown global tag 0x%x\n", item->tag);
  411. return -1;
  412. }
  413. }
  414. /*
  415. * Process a local item.
  416. */
  417. static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
  418. {
  419. __u32 data;
  420. unsigned n;
  421. __u32 count;
  422. data = item_udata(item);
  423. switch (item->tag) {
  424. case HID_LOCAL_ITEM_TAG_DELIMITER:
  425. if (data) {
  426. /*
  427. * We treat items before the first delimiter
  428. * as global to all usage sets (branch 0).
  429. * In the moment we process only these global
  430. * items and the first delimiter set.
  431. */
  432. if (parser->local.delimiter_depth != 0) {
  433. hid_err(parser->device, "nested delimiters\n");
  434. return -1;
  435. }
  436. parser->local.delimiter_depth++;
  437. parser->local.delimiter_branch++;
  438. } else {
  439. if (parser->local.delimiter_depth < 1) {
  440. hid_err(parser->device, "bogus close delimiter\n");
  441. return -1;
  442. }
  443. parser->local.delimiter_depth--;
  444. }
  445. return 0;
  446. case HID_LOCAL_ITEM_TAG_USAGE:
  447. if (parser->local.delimiter_branch > 1) {
  448. dbg_hid("alternative usage ignored\n");
  449. return 0;
  450. }
  451. return hid_add_usage(parser, data, item->size);
  452. case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
  453. if (parser->local.delimiter_branch > 1) {
  454. dbg_hid("alternative usage ignored\n");
  455. return 0;
  456. }
  457. parser->local.usage_minimum = data;
  458. return 0;
  459. case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
  460. if (parser->local.delimiter_branch > 1) {
  461. dbg_hid("alternative usage ignored\n");
  462. return 0;
  463. }
  464. count = data - parser->local.usage_minimum;
  465. if (count + parser->local.usage_index >= HID_MAX_USAGES) {
  466. /*
  467. * We do not warn if the name is not set, we are
  468. * actually pre-scanning the device.
  469. */
  470. if (dev_name(&parser->device->dev))
  471. hid_warn(parser->device,
  472. "ignoring exceeding usage max\n");
  473. data = HID_MAX_USAGES - parser->local.usage_index +
  474. parser->local.usage_minimum - 1;
  475. if (data <= 0) {
  476. hid_err(parser->device,
  477. "no more usage index available\n");
  478. return -1;
  479. }
  480. }
  481. for (n = parser->local.usage_minimum; n <= data; n++)
  482. if (hid_add_usage(parser, n, item->size)) {
  483. dbg_hid("hid_add_usage failed\n");
  484. return -1;
  485. }
  486. return 0;
  487. default:
  488. dbg_hid("unknown local item tag 0x%x\n", item->tag);
  489. return 0;
  490. }
  491. return 0;
  492. }
  493. /*
  494. * Concatenate Usage Pages into Usages where relevant:
  495. * As per specification, 6.2.2.8: "When the parser encounters a main item it
  496. * concatenates the last declared Usage Page with a Usage to form a complete
  497. * usage value."
  498. */
  499. static void hid_concatenate_last_usage_page(struct hid_parser *parser)
  500. {
  501. int i;
  502. unsigned int usage_page;
  503. unsigned int current_page;
  504. if (!parser->local.usage_index)
  505. return;
  506. usage_page = parser->global.usage_page;
  507. /*
  508. * Concatenate usage page again only if last declared Usage Page
  509. * has not been already used in previous usages concatenation
  510. */
  511. for (i = parser->local.usage_index - 1; i >= 0; i--) {
  512. if (parser->local.usage_size[i] > 2)
  513. /* Ignore extended usages */
  514. continue;
  515. current_page = parser->local.usage[i] >> 16;
  516. if (current_page == usage_page)
  517. break;
  518. complete_usage(parser, i);
  519. }
  520. }
  521. /*
  522. * Process a main item.
  523. */
  524. static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
  525. {
  526. __u32 data;
  527. int ret;
  528. hid_concatenate_last_usage_page(parser);
  529. data = item_udata(item);
  530. switch (item->tag) {
  531. case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
  532. ret = open_collection(parser, data & 0xff);
  533. break;
  534. case HID_MAIN_ITEM_TAG_END_COLLECTION:
  535. ret = close_collection(parser);
  536. break;
  537. case HID_MAIN_ITEM_TAG_INPUT:
  538. ret = hid_add_field(parser, HID_INPUT_REPORT, data);
  539. break;
  540. case HID_MAIN_ITEM_TAG_OUTPUT:
  541. ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
  542. break;
  543. case HID_MAIN_ITEM_TAG_FEATURE:
  544. ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
  545. break;
  546. default:
  547. hid_warn(parser->device, "unknown main item tag 0x%x\n", item->tag);
  548. ret = 0;
  549. }
  550. memset(&parser->local, 0, sizeof(parser->local)); /* Reset the local parser environment */
  551. return ret;
  552. }
  553. /*
  554. * Process a reserved item.
  555. */
  556. static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
  557. {
  558. dbg_hid("reserved item type, tag 0x%x\n", item->tag);
  559. return 0;
  560. }
  561. /*
  562. * Free a report and all registered fields. The field->usage and
  563. * field->value table's are allocated behind the field, so we need
  564. * only to free(field) itself.
  565. */
  566. static void hid_free_report(struct hid_report *report)
  567. {
  568. unsigned n;
  569. kfree(report->field_entries);
  570. for (n = 0; n < report->maxfield; n++)
  571. kvfree(report->field[n]);
  572. kfree(report);
  573. }
  574. /*
  575. * Close report. This function returns the device
  576. * state to the point prior to hid_open_report().
  577. */
  578. static void hid_close_report(struct hid_device *device)
  579. {
  580. unsigned i, j;
  581. for (i = 0; i < HID_REPORT_TYPES; i++) {
  582. struct hid_report_enum *report_enum = device->report_enum + i;
  583. for (j = 0; j < HID_MAX_IDS; j++) {
  584. struct hid_report *report = report_enum->report_id_hash[j];
  585. if (report)
  586. hid_free_report(report);
  587. }
  588. memset(report_enum, 0, sizeof(*report_enum));
  589. INIT_LIST_HEAD(&report_enum->report_list);
  590. }
  591. kfree(device->rdesc);
  592. device->rdesc = NULL;
  593. device->rsize = 0;
  594. kfree(device->collection);
  595. device->collection = NULL;
  596. device->collection_size = 0;
  597. device->maxcollection = 0;
  598. device->maxapplication = 0;
  599. device->status &= ~HID_STAT_PARSED;
  600. }
  601. /*
  602. * Free a device structure, all reports, and all fields.
  603. */
  604. void hiddev_free(struct kref *ref)
  605. {
  606. struct hid_device *hid = container_of(ref, struct hid_device, ref);
  607. hid_close_report(hid);
  608. kfree(hid->dev_rdesc);
  609. kfree(hid);
  610. }
  611. static void hid_device_release(struct device *dev)
  612. {
  613. struct hid_device *hid = to_hid_device(dev);
  614. kref_put(&hid->ref, hiddev_free);
  615. }
  616. /*
  617. * Fetch a report description item from the data stream. We support long
  618. * items, though they are not used yet.
  619. */
  620. static const u8 *fetch_item(const __u8 *start, const __u8 *end, struct hid_item *item)
  621. {
  622. u8 b;
  623. if ((end - start) <= 0)
  624. return NULL;
  625. b = *start++;
  626. item->type = (b >> 2) & 3;
  627. item->tag = (b >> 4) & 15;
  628. if (item->tag == HID_ITEM_TAG_LONG) {
  629. item->format = HID_ITEM_FORMAT_LONG;
  630. if ((end - start) < 2)
  631. return NULL;
  632. item->size = *start++;
  633. item->tag = *start++;
  634. if ((end - start) < item->size)
  635. return NULL;
  636. item->data.longdata = start;
  637. start += item->size;
  638. return start;
  639. }
  640. item->format = HID_ITEM_FORMAT_SHORT;
  641. item->size = b & 3;
  642. switch (item->size) {
  643. case 0:
  644. return start;
  645. case 1:
  646. if ((end - start) < 1)
  647. return NULL;
  648. item->data.u8 = *start++;
  649. return start;
  650. case 2:
  651. if ((end - start) < 2)
  652. return NULL;
  653. item->data.u16 = get_unaligned_le16(start);
  654. start = (__u8 *)((__le16 *)start + 1);
  655. return start;
  656. case 3:
  657. item->size++;
  658. if ((end - start) < 4)
  659. return NULL;
  660. item->data.u32 = get_unaligned_le32(start);
  661. start = (__u8 *)((__le32 *)start + 1);
  662. return start;
  663. }
  664. return NULL;
  665. }
  666. static void hid_scan_input_usage(struct hid_parser *parser, u32 usage)
  667. {
  668. struct hid_device *hid = parser->device;
  669. if (usage == HID_DG_CONTACTID)
  670. hid->group = HID_GROUP_MULTITOUCH;
  671. }
  672. static void hid_scan_feature_usage(struct hid_parser *parser, u32 usage)
  673. {
  674. if (usage == 0xff0000c5 && parser->global.report_count == 256 &&
  675. parser->global.report_size == 8)
  676. parser->scan_flags |= HID_SCAN_FLAG_MT_WIN_8;
  677. if (usage == 0xff0000c6 && parser->global.report_count == 1 &&
  678. parser->global.report_size == 8)
  679. parser->scan_flags |= HID_SCAN_FLAG_MT_WIN_8;
  680. }
  681. static void hid_scan_collection(struct hid_parser *parser, unsigned type)
  682. {
  683. struct hid_device *hid = parser->device;
  684. int i;
  685. if (((parser->global.usage_page << 16) == HID_UP_SENSOR) &&
  686. (type == HID_COLLECTION_PHYSICAL ||
  687. type == HID_COLLECTION_APPLICATION))
  688. hid->group = HID_GROUP_SENSOR_HUB;
  689. if (hid->vendor == USB_VENDOR_ID_MICROSOFT &&
  690. hid->product == USB_DEVICE_ID_MS_POWER_COVER &&
  691. hid->group == HID_GROUP_MULTITOUCH)
  692. hid->group = HID_GROUP_GENERIC;
  693. if ((parser->global.usage_page << 16) == HID_UP_GENDESK)
  694. for (i = 0; i < parser->local.usage_index; i++)
  695. if (parser->local.usage[i] == HID_GD_POINTER)
  696. parser->scan_flags |= HID_SCAN_FLAG_GD_POINTER;
  697. if ((parser->global.usage_page << 16) >= HID_UP_MSVENDOR)
  698. parser->scan_flags |= HID_SCAN_FLAG_VENDOR_SPECIFIC;
  699. if ((parser->global.usage_page << 16) == HID_UP_GOOGLEVENDOR)
  700. for (i = 0; i < parser->local.usage_index; i++)
  701. if (parser->local.usage[i] ==
  702. (HID_UP_GOOGLEVENDOR | 0x0001))
  703. parser->device->group =
  704. HID_GROUP_VIVALDI;
  705. }
  706. static int hid_scan_main(struct hid_parser *parser, struct hid_item *item)
  707. {
  708. __u32 data;
  709. int i;
  710. hid_concatenate_last_usage_page(parser);
  711. data = item_udata(item);
  712. switch (item->tag) {
  713. case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
  714. hid_scan_collection(parser, data & 0xff);
  715. break;
  716. case HID_MAIN_ITEM_TAG_END_COLLECTION:
  717. break;
  718. case HID_MAIN_ITEM_TAG_INPUT:
  719. /* ignore constant inputs, they will be ignored by hid-input */
  720. if (data & HID_MAIN_ITEM_CONSTANT)
  721. break;
  722. for (i = 0; i < parser->local.usage_index; i++)
  723. hid_scan_input_usage(parser, parser->local.usage[i]);
  724. break;
  725. case HID_MAIN_ITEM_TAG_OUTPUT:
  726. break;
  727. case HID_MAIN_ITEM_TAG_FEATURE:
  728. for (i = 0; i < parser->local.usage_index; i++)
  729. hid_scan_feature_usage(parser, parser->local.usage[i]);
  730. break;
  731. }
  732. /* Reset the local parser environment */
  733. memset(&parser->local, 0, sizeof(parser->local));
  734. return 0;
  735. }
  736. /*
  737. * Scan a report descriptor before the device is added to the bus.
  738. * Sets device groups and other properties that determine what driver
  739. * to load.
  740. */
  741. static int hid_scan_report(struct hid_device *hid)
  742. {
  743. struct hid_parser *parser;
  744. struct hid_item item;
  745. const __u8 *start = hid->dev_rdesc;
  746. const __u8 *end = start + hid->dev_rsize;
  747. static int (*dispatch_type[])(struct hid_parser *parser,
  748. struct hid_item *item) = {
  749. hid_scan_main,
  750. hid_parser_global,
  751. hid_parser_local,
  752. hid_parser_reserved
  753. };
  754. parser = vzalloc(sizeof(struct hid_parser));
  755. if (!parser)
  756. return -ENOMEM;
  757. parser->device = hid;
  758. hid->group = HID_GROUP_GENERIC;
  759. /*
  760. * The parsing is simpler than the one in hid_open_report() as we should
  761. * be robust against hid errors. Those errors will be raised by
  762. * hid_open_report() anyway.
  763. */
  764. while ((start = fetch_item(start, end, &item)) != NULL)
  765. dispatch_type[item.type](parser, &item);
  766. /*
  767. * Handle special flags set during scanning.
  768. */
  769. if ((parser->scan_flags & HID_SCAN_FLAG_MT_WIN_8) &&
  770. (hid->group == HID_GROUP_MULTITOUCH))
  771. hid->group = HID_GROUP_MULTITOUCH_WIN_8;
  772. /*
  773. * Vendor specific handlings
  774. */
  775. switch (hid->vendor) {
  776. case USB_VENDOR_ID_WACOM:
  777. hid->group = HID_GROUP_WACOM;
  778. break;
  779. case USB_VENDOR_ID_SYNAPTICS:
  780. if (hid->group == HID_GROUP_GENERIC)
  781. if ((parser->scan_flags & HID_SCAN_FLAG_VENDOR_SPECIFIC)
  782. && (parser->scan_flags & HID_SCAN_FLAG_GD_POINTER))
  783. /*
  784. * hid-rmi should take care of them,
  785. * not hid-generic
  786. */
  787. hid->group = HID_GROUP_RMI;
  788. break;
  789. }
  790. kfree(parser->collection_stack);
  791. vfree(parser);
  792. return 0;
  793. }
  794. /**
  795. * hid_parse_report - parse device report
  796. *
  797. * @hid: hid device
  798. * @start: report start
  799. * @size: report size
  800. *
  801. * Allocate the device report as read by the bus driver. This function should
  802. * only be called from parse() in ll drivers.
  803. */
  804. int hid_parse_report(struct hid_device *hid, const __u8 *start, unsigned size)
  805. {
  806. hid->dev_rdesc = kmemdup(start, size, GFP_KERNEL);
  807. if (!hid->dev_rdesc)
  808. return -ENOMEM;
  809. hid->dev_rsize = size;
  810. return 0;
  811. }
  812. EXPORT_SYMBOL_GPL(hid_parse_report);
  813. static const char * const hid_report_names[] = {
  814. "HID_INPUT_REPORT",
  815. "HID_OUTPUT_REPORT",
  816. "HID_FEATURE_REPORT",
  817. };
  818. /**
  819. * hid_validate_values - validate existing device report's value indexes
  820. *
  821. * @hid: hid device
  822. * @type: which report type to examine
  823. * @id: which report ID to examine (0 for first)
  824. * @field_index: which report field to examine
  825. * @report_counts: expected number of values
  826. *
  827. * Validate the number of values in a given field of a given report, after
  828. * parsing.
  829. */
  830. struct hid_report *hid_validate_values(struct hid_device *hid,
  831. enum hid_report_type type, unsigned int id,
  832. unsigned int field_index,
  833. unsigned int report_counts)
  834. {
  835. struct hid_report *report;
  836. if (type > HID_FEATURE_REPORT) {
  837. hid_err(hid, "invalid HID report type %u\n", type);
  838. return NULL;
  839. }
  840. if (id >= HID_MAX_IDS) {
  841. hid_err(hid, "invalid HID report id %u\n", id);
  842. return NULL;
  843. }
  844. /*
  845. * Explicitly not using hid_get_report() here since it depends on
  846. * ->numbered being checked, which may not always be the case when
  847. * drivers go to access report values.
  848. */
  849. if (id == 0) {
  850. /*
  851. * Validating on id 0 means we should examine the first
  852. * report in the list.
  853. */
  854. report = list_first_entry_or_null(
  855. &hid->report_enum[type].report_list,
  856. struct hid_report, list);
  857. } else {
  858. report = hid->report_enum[type].report_id_hash[id];
  859. }
  860. if (!report) {
  861. hid_err(hid, "missing %s %u\n", hid_report_names[type], id);
  862. return NULL;
  863. }
  864. if (report->maxfield <= field_index) {
  865. hid_err(hid, "not enough fields in %s %u\n",
  866. hid_report_names[type], id);
  867. return NULL;
  868. }
  869. if (report->field[field_index]->report_count < report_counts) {
  870. hid_err(hid, "not enough values in %s %u field %u\n",
  871. hid_report_names[type], id, field_index);
  872. return NULL;
  873. }
  874. return report;
  875. }
  876. EXPORT_SYMBOL_GPL(hid_validate_values);
  877. static int hid_calculate_multiplier(struct hid_device *hid,
  878. struct hid_field *multiplier)
  879. {
  880. int m;
  881. __s32 v = *multiplier->value;
  882. __s32 lmin = multiplier->logical_minimum;
  883. __s32 lmax = multiplier->logical_maximum;
  884. __s32 pmin = multiplier->physical_minimum;
  885. __s32 pmax = multiplier->physical_maximum;
  886. /*
  887. * "Because OS implementations will generally divide the control's
  888. * reported count by the Effective Resolution Multiplier, designers
  889. * should take care not to establish a potential Effective
  890. * Resolution Multiplier of zero."
  891. * HID Usage Table, v1.12, Section 4.3.1, p31
  892. */
  893. if (lmax - lmin == 0)
  894. return 1;
  895. /*
  896. * Handling the unit exponent is left as an exercise to whoever
  897. * finds a device where that exponent is not 0.
  898. */
  899. m = ((v - lmin)/(lmax - lmin) * (pmax - pmin) + pmin);
  900. if (unlikely(multiplier->unit_exponent != 0)) {
  901. hid_warn(hid,
  902. "unsupported Resolution Multiplier unit exponent %d\n",
  903. multiplier->unit_exponent);
  904. }
  905. /* There are no devices with an effective multiplier > 255 */
  906. if (unlikely(m == 0 || m > 255 || m < -255)) {
  907. hid_warn(hid, "unsupported Resolution Multiplier %d\n", m);
  908. m = 1;
  909. }
  910. return m;
  911. }
  912. static void hid_apply_multiplier_to_field(struct hid_device *hid,
  913. struct hid_field *field,
  914. struct hid_collection *multiplier_collection,
  915. int effective_multiplier)
  916. {
  917. struct hid_collection *collection;
  918. struct hid_usage *usage;
  919. int i;
  920. /*
  921. * If multiplier_collection is NULL, the multiplier applies
  922. * to all fields in the report.
  923. * Otherwise, it is the Logical Collection the multiplier applies to
  924. * but our field may be in a subcollection of that collection.
  925. */
  926. for (i = 0; i < field->maxusage; i++) {
  927. usage = &field->usage[i];
  928. collection = &hid->collection[usage->collection_index];
  929. while (collection->parent_idx != -1 &&
  930. collection != multiplier_collection)
  931. collection = &hid->collection[collection->parent_idx];
  932. if (collection->parent_idx != -1 ||
  933. multiplier_collection == NULL)
  934. usage->resolution_multiplier = effective_multiplier;
  935. }
  936. }
  937. static void hid_apply_multiplier(struct hid_device *hid,
  938. struct hid_field *multiplier)
  939. {
  940. struct hid_report_enum *rep_enum;
  941. struct hid_report *rep;
  942. struct hid_field *field;
  943. struct hid_collection *multiplier_collection;
  944. int effective_multiplier;
  945. int i;
  946. /*
  947. * "The Resolution Multiplier control must be contained in the same
  948. * Logical Collection as the control(s) to which it is to be applied.
  949. * If no Resolution Multiplier is defined, then the Resolution
  950. * Multiplier defaults to 1. If more than one control exists in a
  951. * Logical Collection, the Resolution Multiplier is associated with
  952. * all controls in the collection. If no Logical Collection is
  953. * defined, the Resolution Multiplier is associated with all
  954. * controls in the report."
  955. * HID Usage Table, v1.12, Section 4.3.1, p30
  956. *
  957. * Thus, search from the current collection upwards until we find a
  958. * logical collection. Then search all fields for that same parent
  959. * collection. Those are the fields the multiplier applies to.
  960. *
  961. * If we have more than one multiplier, it will overwrite the
  962. * applicable fields later.
  963. */
  964. multiplier_collection = &hid->collection[multiplier->usage->collection_index];
  965. while (multiplier_collection->parent_idx != -1 &&
  966. multiplier_collection->type != HID_COLLECTION_LOGICAL)
  967. multiplier_collection = &hid->collection[multiplier_collection->parent_idx];
  968. if (multiplier_collection->type != HID_COLLECTION_LOGICAL)
  969. multiplier_collection = NULL;
  970. effective_multiplier = hid_calculate_multiplier(hid, multiplier);
  971. rep_enum = &hid->report_enum[HID_INPUT_REPORT];
  972. list_for_each_entry(rep, &rep_enum->report_list, list) {
  973. for (i = 0; i < rep->maxfield; i++) {
  974. field = rep->field[i];
  975. hid_apply_multiplier_to_field(hid, field,
  976. multiplier_collection,
  977. effective_multiplier);
  978. }
  979. }
  980. }
  981. /*
  982. * hid_setup_resolution_multiplier - set up all resolution multipliers
  983. *
  984. * @device: hid device
  985. *
  986. * Search for all Resolution Multiplier Feature Reports and apply their
  987. * value to all matching Input items. This only updates the internal struct
  988. * fields.
  989. *
  990. * The Resolution Multiplier is applied by the hardware. If the multiplier
  991. * is anything other than 1, the hardware will send pre-multiplied events
  992. * so that the same physical interaction generates an accumulated
  993. * accumulated_value = value * * multiplier
  994. * This may be achieved by sending
  995. * - "value * multiplier" for each event, or
  996. * - "value" but "multiplier" times as frequently, or
  997. * - a combination of the above
  998. * The only guarantee is that the same physical interaction always generates
  999. * an accumulated 'value * multiplier'.
  1000. *
  1001. * This function must be called before any event processing and after
  1002. * any SetRequest to the Resolution Multiplier.
  1003. */
  1004. void hid_setup_resolution_multiplier(struct hid_device *hid)
  1005. {
  1006. struct hid_report_enum *rep_enum;
  1007. struct hid_report *rep;
  1008. struct hid_usage *usage;
  1009. int i, j;
  1010. rep_enum = &hid->report_enum[HID_FEATURE_REPORT];
  1011. list_for_each_entry(rep, &rep_enum->report_list, list) {
  1012. for (i = 0; i < rep->maxfield; i++) {
  1013. /* Ignore if report count is out of bounds. */
  1014. if (rep->field[i]->report_count < 1)
  1015. continue;
  1016. for (j = 0; j < rep->field[i]->maxusage; j++) {
  1017. usage = &rep->field[i]->usage[j];
  1018. if (usage->hid == HID_GD_RESOLUTION_MULTIPLIER)
  1019. hid_apply_multiplier(hid,
  1020. rep->field[i]);
  1021. }
  1022. }
  1023. }
  1024. }
  1025. EXPORT_SYMBOL_GPL(hid_setup_resolution_multiplier);
  1026. /**
  1027. * hid_open_report - open a driver-specific device report
  1028. *
  1029. * @device: hid device
  1030. *
  1031. * Parse a report description into a hid_device structure. Reports are
  1032. * enumerated, fields are attached to these reports.
  1033. * 0 returned on success, otherwise nonzero error value.
  1034. *
  1035. * This function (or the equivalent hid_parse() macro) should only be
  1036. * called from probe() in drivers, before starting the device.
  1037. */
  1038. int hid_open_report(struct hid_device *device)
  1039. {
  1040. struct hid_parser *parser;
  1041. struct hid_item item;
  1042. unsigned int size;
  1043. const __u8 *start;
  1044. __u8 *buf;
  1045. const __u8 *end;
  1046. const __u8 *next;
  1047. int ret;
  1048. int i;
  1049. static int (*dispatch_type[])(struct hid_parser *parser,
  1050. struct hid_item *item) = {
  1051. hid_parser_main,
  1052. hid_parser_global,
  1053. hid_parser_local,
  1054. hid_parser_reserved
  1055. };
  1056. if (WARN_ON(device->status & HID_STAT_PARSED))
  1057. return -EBUSY;
  1058. start = device->dev_rdesc;
  1059. if (WARN_ON(!start))
  1060. return -ENODEV;
  1061. size = device->dev_rsize;
  1062. /* call_hid_bpf_rdesc_fixup() ensures we work on a copy of rdesc */
  1063. buf = call_hid_bpf_rdesc_fixup(device, start, &size);
  1064. if (buf == NULL)
  1065. return -ENOMEM;
  1066. if (device->driver->report_fixup)
  1067. start = device->driver->report_fixup(device, buf, &size);
  1068. else
  1069. start = buf;
  1070. start = kmemdup(start, size, GFP_KERNEL);
  1071. kfree(buf);
  1072. if (start == NULL)
  1073. return -ENOMEM;
  1074. device->rdesc = start;
  1075. device->rsize = size;
  1076. parser = vzalloc(sizeof(struct hid_parser));
  1077. if (!parser) {
  1078. ret = -ENOMEM;
  1079. goto alloc_err;
  1080. }
  1081. parser->device = device;
  1082. end = start + size;
  1083. device->collection = kcalloc(HID_DEFAULT_NUM_COLLECTIONS,
  1084. sizeof(struct hid_collection), GFP_KERNEL);
  1085. if (!device->collection) {
  1086. ret = -ENOMEM;
  1087. goto err;
  1088. }
  1089. device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
  1090. for (i = 0; i < HID_DEFAULT_NUM_COLLECTIONS; i++)
  1091. device->collection[i].parent_idx = -1;
  1092. ret = -EINVAL;
  1093. while ((next = fetch_item(start, end, &item)) != NULL) {
  1094. start = next;
  1095. if (item.format != HID_ITEM_FORMAT_SHORT) {
  1096. hid_err(device, "unexpected long global item\n");
  1097. goto err;
  1098. }
  1099. if (dispatch_type[item.type](parser, &item)) {
  1100. hid_err(device, "item %u %u %u %u parsing failed\n",
  1101. item.format, (unsigned)item.size,
  1102. (unsigned)item.type, (unsigned)item.tag);
  1103. goto err;
  1104. }
  1105. if (start == end) {
  1106. if (parser->collection_stack_ptr) {
  1107. hid_err(device, "unbalanced collection at end of report description\n");
  1108. goto err;
  1109. }
  1110. if (parser->local.delimiter_depth) {
  1111. hid_err(device, "unbalanced delimiter at end of report description\n");
  1112. goto err;
  1113. }
  1114. /*
  1115. * fetch initial values in case the device's
  1116. * default multiplier isn't the recommended 1
  1117. */
  1118. hid_setup_resolution_multiplier(device);
  1119. kfree(parser->collection_stack);
  1120. vfree(parser);
  1121. device->status |= HID_STAT_PARSED;
  1122. return 0;
  1123. }
  1124. }
  1125. hid_err(device, "item fetching failed at offset %u/%u\n",
  1126. size - (unsigned int)(end - start), size);
  1127. err:
  1128. kfree(parser->collection_stack);
  1129. alloc_err:
  1130. vfree(parser);
  1131. hid_close_report(device);
  1132. return ret;
  1133. }
  1134. EXPORT_SYMBOL_GPL(hid_open_report);
  1135. /*
  1136. * Extract/implement a data field from/to a little endian report (bit array).
  1137. *
  1138. * Code sort-of follows HID spec:
  1139. * http://www.usb.org/developers/hidpage/HID1_11.pdf
  1140. *
  1141. * While the USB HID spec allows unlimited length bit fields in "report
  1142. * descriptors", most devices never use more than 16 bits.
  1143. * One model of UPS is claimed to report "LINEV" as a 32-bit field.
  1144. * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
  1145. */
  1146. static u32 __extract(u8 *report, unsigned offset, int n)
  1147. {
  1148. unsigned int idx = offset / 8;
  1149. unsigned int bit_nr = 0;
  1150. unsigned int bit_shift = offset % 8;
  1151. int bits_to_copy = 8 - bit_shift;
  1152. u32 value = 0;
  1153. u32 mask = n < 32 ? (1U << n) - 1 : ~0U;
  1154. while (n > 0) {
  1155. value |= ((u32)report[idx] >> bit_shift) << bit_nr;
  1156. n -= bits_to_copy;
  1157. bit_nr += bits_to_copy;
  1158. bits_to_copy = 8;
  1159. bit_shift = 0;
  1160. idx++;
  1161. }
  1162. return value & mask;
  1163. }
  1164. u32 hid_field_extract(const struct hid_device *hid, u8 *report,
  1165. unsigned offset, unsigned n)
  1166. {
  1167. if (n > 32) {
  1168. hid_warn_once(hid, "%s() called with n (%d) > 32! (%s)\n",
  1169. __func__, n, current->comm);
  1170. n = 32;
  1171. }
  1172. return __extract(report, offset, n);
  1173. }
  1174. EXPORT_SYMBOL_GPL(hid_field_extract);
  1175. /*
  1176. * "implement" : set bits in a little endian bit stream.
  1177. * Same concepts as "extract" (see comments above).
  1178. * The data mangled in the bit stream remains in little endian
  1179. * order the whole time. It make more sense to talk about
  1180. * endianness of register values by considering a register
  1181. * a "cached" copy of the little endian bit stream.
  1182. */
  1183. static void __implement(u8 *report, unsigned offset, int n, u32 value)
  1184. {
  1185. unsigned int idx = offset / 8;
  1186. unsigned int bit_shift = offset % 8;
  1187. int bits_to_set = 8 - bit_shift;
  1188. while (n - bits_to_set >= 0) {
  1189. report[idx] &= ~(0xff << bit_shift);
  1190. report[idx] |= value << bit_shift;
  1191. value >>= bits_to_set;
  1192. n -= bits_to_set;
  1193. bits_to_set = 8;
  1194. bit_shift = 0;
  1195. idx++;
  1196. }
  1197. /* last nibble */
  1198. if (n) {
  1199. u8 bit_mask = ((1U << n) - 1);
  1200. report[idx] &= ~(bit_mask << bit_shift);
  1201. report[idx] |= value << bit_shift;
  1202. }
  1203. }
  1204. static void implement(const struct hid_device *hid, u8 *report,
  1205. unsigned offset, unsigned n, u32 value)
  1206. {
  1207. if (unlikely(n > 32)) {
  1208. hid_warn(hid, "%s() called with n (%d) > 32! (%s)\n",
  1209. __func__, n, current->comm);
  1210. n = 32;
  1211. } else if (n < 32) {
  1212. u32 m = (1U << n) - 1;
  1213. if (unlikely(value > m)) {
  1214. hid_warn(hid,
  1215. "%s() called with too large value %d (n: %d)! (%s)\n",
  1216. __func__, value, n, current->comm);
  1217. value &= m;
  1218. }
  1219. }
  1220. __implement(report, offset, n, value);
  1221. }
  1222. /*
  1223. * Search an array for a value.
  1224. */
  1225. static int search(__s32 *array, __s32 value, unsigned n)
  1226. {
  1227. while (n--) {
  1228. if (*array++ == value)
  1229. return 0;
  1230. }
  1231. return -1;
  1232. }
  1233. /**
  1234. * hid_match_report - check if driver's raw_event should be called
  1235. *
  1236. * @hid: hid device
  1237. * @report: hid report to match against
  1238. *
  1239. * compare hid->driver->report_table->report_type to report->type
  1240. */
  1241. static int hid_match_report(struct hid_device *hid, struct hid_report *report)
  1242. {
  1243. const struct hid_report_id *id = hid->driver->report_table;
  1244. if (!id) /* NULL means all */
  1245. return 1;
  1246. for (; id->report_type != HID_TERMINATOR; id++)
  1247. if (id->report_type == HID_ANY_ID ||
  1248. id->report_type == report->type)
  1249. return 1;
  1250. return 0;
  1251. }
  1252. /**
  1253. * hid_match_usage - check if driver's event should be called
  1254. *
  1255. * @hid: hid device
  1256. * @usage: usage to match against
  1257. *
  1258. * compare hid->driver->usage_table->usage_{type,code} to
  1259. * usage->usage_{type,code}
  1260. */
  1261. static int hid_match_usage(struct hid_device *hid, struct hid_usage *usage)
  1262. {
  1263. const struct hid_usage_id *id = hid->driver->usage_table;
  1264. if (!id) /* NULL means all */
  1265. return 1;
  1266. for (; id->usage_type != HID_ANY_ID - 1; id++)
  1267. if ((id->usage_hid == HID_ANY_ID ||
  1268. id->usage_hid == usage->hid) &&
  1269. (id->usage_type == HID_ANY_ID ||
  1270. id->usage_type == usage->type) &&
  1271. (id->usage_code == HID_ANY_ID ||
  1272. id->usage_code == usage->code))
  1273. return 1;
  1274. return 0;
  1275. }
  1276. static void hid_process_event(struct hid_device *hid, struct hid_field *field,
  1277. struct hid_usage *usage, __s32 value, int interrupt)
  1278. {
  1279. struct hid_driver *hdrv = hid->driver;
  1280. int ret;
  1281. if (!list_empty(&hid->debug_list))
  1282. hid_dump_input(hid, usage, value);
  1283. if (hdrv && hdrv->event && hid_match_usage(hid, usage)) {
  1284. ret = hdrv->event(hid, field, usage, value);
  1285. if (ret != 0) {
  1286. if (ret < 0)
  1287. hid_err(hid, "%s's event failed with %d\n",
  1288. hdrv->name, ret);
  1289. return;
  1290. }
  1291. }
  1292. if (hid->claimed & HID_CLAIMED_INPUT)
  1293. hidinput_hid_event(hid, field, usage, value);
  1294. if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event)
  1295. hid->hiddev_hid_event(hid, field, usage, value);
  1296. }
  1297. /*
  1298. * Checks if the given value is valid within this field
  1299. */
  1300. static inline int hid_array_value_is_valid(struct hid_field *field,
  1301. __s32 value)
  1302. {
  1303. __s32 min = field->logical_minimum;
  1304. /*
  1305. * Value needs to be between logical min and max, and
  1306. * (value - min) is used as an index in the usage array.
  1307. * This array is of size field->maxusage
  1308. */
  1309. return value >= min &&
  1310. value <= field->logical_maximum &&
  1311. value - min < field->maxusage;
  1312. }
  1313. /*
  1314. * Fetch the field from the data. The field content is stored for next
  1315. * report processing (we do differential reporting to the layer).
  1316. */
  1317. static void hid_input_fetch_field(struct hid_device *hid,
  1318. struct hid_field *field,
  1319. __u8 *data)
  1320. {
  1321. unsigned n;
  1322. unsigned count = field->report_count;
  1323. unsigned offset = field->report_offset;
  1324. unsigned size = field->report_size;
  1325. __s32 min = field->logical_minimum;
  1326. __s32 *value;
  1327. value = field->new_value;
  1328. memset(value, 0, count * sizeof(__s32));
  1329. field->ignored = false;
  1330. for (n = 0; n < count; n++) {
  1331. value[n] = min < 0 ?
  1332. snto32(hid_field_extract(hid, data, offset + n * size,
  1333. size), size) :
  1334. hid_field_extract(hid, data, offset + n * size, size);
  1335. /* Ignore report if ErrorRollOver */
  1336. if (!(field->flags & HID_MAIN_ITEM_VARIABLE) &&
  1337. hid_array_value_is_valid(field, value[n]) &&
  1338. field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1) {
  1339. field->ignored = true;
  1340. return;
  1341. }
  1342. }
  1343. }
  1344. /*
  1345. * Process a received variable field.
  1346. */
  1347. static void hid_input_var_field(struct hid_device *hid,
  1348. struct hid_field *field,
  1349. int interrupt)
  1350. {
  1351. unsigned int count = field->report_count;
  1352. __s32 *value = field->new_value;
  1353. unsigned int n;
  1354. for (n = 0; n < count; n++)
  1355. hid_process_event(hid,
  1356. field,
  1357. &field->usage[n],
  1358. value[n],
  1359. interrupt);
  1360. memcpy(field->value, value, count * sizeof(__s32));
  1361. }
  1362. /*
  1363. * Process a received array field. The field content is stored for
  1364. * next report processing (we do differential reporting to the layer).
  1365. */
  1366. static void hid_input_array_field(struct hid_device *hid,
  1367. struct hid_field *field,
  1368. int interrupt)
  1369. {
  1370. unsigned int n;
  1371. unsigned int count = field->report_count;
  1372. __s32 min = field->logical_minimum;
  1373. __s32 *value;
  1374. value = field->new_value;
  1375. /* ErrorRollOver */
  1376. if (field->ignored)
  1377. return;
  1378. for (n = 0; n < count; n++) {
  1379. if (hid_array_value_is_valid(field, field->value[n]) &&
  1380. search(value, field->value[n], count))
  1381. hid_process_event(hid,
  1382. field,
  1383. &field->usage[field->value[n] - min],
  1384. 0,
  1385. interrupt);
  1386. if (hid_array_value_is_valid(field, value[n]) &&
  1387. search(field->value, value[n], count))
  1388. hid_process_event(hid,
  1389. field,
  1390. &field->usage[value[n] - min],
  1391. 1,
  1392. interrupt);
  1393. }
  1394. memcpy(field->value, value, count * sizeof(__s32));
  1395. }
  1396. /*
  1397. * Analyse a received report, and fetch the data from it. The field
  1398. * content is stored for next report processing (we do differential
  1399. * reporting to the layer).
  1400. */
  1401. static void hid_process_report(struct hid_device *hid,
  1402. struct hid_report *report,
  1403. __u8 *data,
  1404. int interrupt)
  1405. {
  1406. unsigned int a;
  1407. struct hid_field_entry *entry;
  1408. struct hid_field *field;
  1409. /* first retrieve all incoming values in data */
  1410. for (a = 0; a < report->maxfield; a++)
  1411. hid_input_fetch_field(hid, report->field[a], data);
  1412. if (!list_empty(&report->field_entry_list)) {
  1413. /* INPUT_REPORT, we have a priority list of fields */
  1414. list_for_each_entry(entry,
  1415. &report->field_entry_list,
  1416. list) {
  1417. field = entry->field;
  1418. if (field->flags & HID_MAIN_ITEM_VARIABLE)
  1419. hid_process_event(hid,
  1420. field,
  1421. &field->usage[entry->index],
  1422. field->new_value[entry->index],
  1423. interrupt);
  1424. else
  1425. hid_input_array_field(hid, field, interrupt);
  1426. }
  1427. /* we need to do the memcpy at the end for var items */
  1428. for (a = 0; a < report->maxfield; a++) {
  1429. field = report->field[a];
  1430. if (field->flags & HID_MAIN_ITEM_VARIABLE)
  1431. memcpy(field->value, field->new_value,
  1432. field->report_count * sizeof(__s32));
  1433. }
  1434. } else {
  1435. /* FEATURE_REPORT, regular processing */
  1436. for (a = 0; a < report->maxfield; a++) {
  1437. field = report->field[a];
  1438. if (field->flags & HID_MAIN_ITEM_VARIABLE)
  1439. hid_input_var_field(hid, field, interrupt);
  1440. else
  1441. hid_input_array_field(hid, field, interrupt);
  1442. }
  1443. }
  1444. }
  1445. /*
  1446. * Insert a given usage_index in a field in the list
  1447. * of processed usages in the report.
  1448. *
  1449. * The elements of lower priority score are processed
  1450. * first.
  1451. */
  1452. static void __hid_insert_field_entry(struct hid_device *hid,
  1453. struct hid_report *report,
  1454. struct hid_field_entry *entry,
  1455. struct hid_field *field,
  1456. unsigned int usage_index)
  1457. {
  1458. struct hid_field_entry *next;
  1459. entry->field = field;
  1460. entry->index = usage_index;
  1461. entry->priority = field->usages_priorities[usage_index];
  1462. /* insert the element at the correct position */
  1463. list_for_each_entry(next,
  1464. &report->field_entry_list,
  1465. list) {
  1466. /*
  1467. * the priority of our element is strictly higher
  1468. * than the next one, insert it before
  1469. */
  1470. if (entry->priority > next->priority) {
  1471. list_add_tail(&entry->list, &next->list);
  1472. return;
  1473. }
  1474. }
  1475. /* lowest priority score: insert at the end */
  1476. list_add_tail(&entry->list, &report->field_entry_list);
  1477. }
  1478. static void hid_report_process_ordering(struct hid_device *hid,
  1479. struct hid_report *report)
  1480. {
  1481. struct hid_field *field;
  1482. struct hid_field_entry *entries;
  1483. unsigned int a, u, usages;
  1484. unsigned int count = 0;
  1485. /* count the number of individual fields in the report */
  1486. for (a = 0; a < report->maxfield; a++) {
  1487. field = report->field[a];
  1488. if (field->flags & HID_MAIN_ITEM_VARIABLE)
  1489. count += field->report_count;
  1490. else
  1491. count++;
  1492. }
  1493. /* allocate the memory to process the fields */
  1494. entries = kcalloc(count, sizeof(*entries), GFP_KERNEL);
  1495. if (!entries)
  1496. return;
  1497. report->field_entries = entries;
  1498. /*
  1499. * walk through all fields in the report and
  1500. * store them by priority order in report->field_entry_list
  1501. *
  1502. * - Var elements are individualized (field + usage_index)
  1503. * - Arrays are taken as one, we can not chose an order for them
  1504. */
  1505. usages = 0;
  1506. for (a = 0; a < report->maxfield; a++) {
  1507. field = report->field[a];
  1508. if (field->flags & HID_MAIN_ITEM_VARIABLE) {
  1509. for (u = 0; u < field->report_count; u++) {
  1510. __hid_insert_field_entry(hid, report,
  1511. &entries[usages],
  1512. field, u);
  1513. usages++;
  1514. }
  1515. } else {
  1516. __hid_insert_field_entry(hid, report, &entries[usages],
  1517. field, 0);
  1518. usages++;
  1519. }
  1520. }
  1521. }
  1522. static void hid_process_ordering(struct hid_device *hid)
  1523. {
  1524. struct hid_report *report;
  1525. struct hid_report_enum *report_enum = &hid->report_enum[HID_INPUT_REPORT];
  1526. list_for_each_entry(report, &report_enum->report_list, list)
  1527. hid_report_process_ordering(hid, report);
  1528. }
  1529. /*
  1530. * Output the field into the report.
  1531. */
  1532. static void hid_output_field(const struct hid_device *hid,
  1533. struct hid_field *field, __u8 *data)
  1534. {
  1535. unsigned count = field->report_count;
  1536. unsigned offset = field->report_offset;
  1537. unsigned size = field->report_size;
  1538. unsigned n;
  1539. for (n = 0; n < count; n++) {
  1540. if (field->logical_minimum < 0) /* signed values */
  1541. implement(hid, data, offset + n * size, size,
  1542. s32ton(field->value[n], size));
  1543. else /* unsigned values */
  1544. implement(hid, data, offset + n * size, size,
  1545. field->value[n]);
  1546. }
  1547. }
  1548. /*
  1549. * Compute the size of a report.
  1550. */
  1551. static size_t hid_compute_report_size(struct hid_report *report)
  1552. {
  1553. if (report->size)
  1554. return ((report->size - 1) >> 3) + 1;
  1555. return 0;
  1556. }
  1557. /*
  1558. * Create a report. 'data' has to be allocated using
  1559. * hid_alloc_report_buf() so that it has proper size.
  1560. */
  1561. void hid_output_report(struct hid_report *report, __u8 *data)
  1562. {
  1563. unsigned n;
  1564. if (report->id > 0)
  1565. *data++ = report->id;
  1566. memset(data, 0, hid_compute_report_size(report));
  1567. for (n = 0; n < report->maxfield; n++)
  1568. hid_output_field(report->device, report->field[n], data);
  1569. }
  1570. EXPORT_SYMBOL_GPL(hid_output_report);
  1571. /*
  1572. * Allocator for buffer that is going to be passed to hid_output_report()
  1573. */
  1574. u8 *hid_alloc_report_buf(struct hid_report *report, gfp_t flags)
  1575. {
  1576. /*
  1577. * 7 extra bytes are necessary to achieve proper functionality
  1578. * of implement() working on 8 byte chunks
  1579. * 1 extra byte for the report ID if it is null (not used) so
  1580. * we can reserve that extra byte in the first position of the buffer
  1581. * when sending it to .raw_request()
  1582. */
  1583. u32 len = hid_report_len(report) + 7 + (report->id == 0);
  1584. return kzalloc(len, flags);
  1585. }
  1586. EXPORT_SYMBOL_GPL(hid_alloc_report_buf);
  1587. /*
  1588. * Set a field value. The report this field belongs to has to be
  1589. * created and transferred to the device, to set this value in the
  1590. * device.
  1591. */
  1592. int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
  1593. {
  1594. unsigned size;
  1595. if (!field)
  1596. return -1;
  1597. size = field->report_size;
  1598. hid_dump_input(field->report->device, field->usage + offset, value);
  1599. if (offset >= field->report_count) {
  1600. hid_err(field->report->device, "offset (%d) exceeds report_count (%d)\n",
  1601. offset, field->report_count);
  1602. return -1;
  1603. }
  1604. if (field->logical_minimum < 0) {
  1605. if (value != snto32(s32ton(value, size), size)) {
  1606. hid_err(field->report->device, "value %d is out of range\n", value);
  1607. return -1;
  1608. }
  1609. }
  1610. field->value[offset] = value;
  1611. return 0;
  1612. }
  1613. EXPORT_SYMBOL_GPL(hid_set_field);
  1614. struct hid_field *hid_find_field(struct hid_device *hdev, unsigned int report_type,
  1615. unsigned int application, unsigned int usage)
  1616. {
  1617. struct list_head *report_list = &hdev->report_enum[report_type].report_list;
  1618. struct hid_report *report;
  1619. int i, j;
  1620. list_for_each_entry(report, report_list, list) {
  1621. if (report->application != application)
  1622. continue;
  1623. for (i = 0; i < report->maxfield; i++) {
  1624. struct hid_field *field = report->field[i];
  1625. for (j = 0; j < field->maxusage; j++) {
  1626. if (field->usage[j].hid == usage)
  1627. return field;
  1628. }
  1629. }
  1630. }
  1631. return NULL;
  1632. }
  1633. EXPORT_SYMBOL_GPL(hid_find_field);
  1634. static struct hid_report *hid_get_report(struct hid_report_enum *report_enum,
  1635. const u8 *data)
  1636. {
  1637. struct hid_report *report;
  1638. unsigned int n = 0; /* Normally report number is 0 */
  1639. /* Device uses numbered reports, data[0] is report number */
  1640. if (report_enum->numbered)
  1641. n = *data;
  1642. report = report_enum->report_id_hash[n];
  1643. if (report == NULL)
  1644. dbg_hid("undefined report_id %u received\n", n);
  1645. return report;
  1646. }
  1647. /*
  1648. * Implement a generic .request() callback, using .raw_request()
  1649. * DO NOT USE in hid drivers directly, but through hid_hw_request instead.
  1650. */
  1651. int __hid_request(struct hid_device *hid, struct hid_report *report,
  1652. enum hid_class_request reqtype)
  1653. {
  1654. char *buf, *data_buf;
  1655. int ret;
  1656. u32 len;
  1657. buf = hid_alloc_report_buf(report, GFP_KERNEL);
  1658. if (!buf)
  1659. return -ENOMEM;
  1660. data_buf = buf;
  1661. len = hid_report_len(report);
  1662. if (report->id == 0) {
  1663. /* reserve the first byte for the report ID */
  1664. data_buf++;
  1665. len++;
  1666. }
  1667. if (reqtype == HID_REQ_SET_REPORT)
  1668. hid_output_report(report, data_buf);
  1669. ret = hid_hw_raw_request(hid, report->id, buf, len, report->type, reqtype);
  1670. if (ret < 0) {
  1671. dbg_hid("unable to complete request: %d\n", ret);
  1672. goto out;
  1673. }
  1674. if (reqtype == HID_REQ_GET_REPORT)
  1675. hid_input_report(hid, report->type, buf, ret, 0);
  1676. ret = 0;
  1677. out:
  1678. kfree(buf);
  1679. return ret;
  1680. }
  1681. EXPORT_SYMBOL_GPL(__hid_request);
  1682. int hid_report_raw_event(struct hid_device *hid, enum hid_report_type type, u8 *data, u32 size,
  1683. int interrupt)
  1684. {
  1685. struct hid_report_enum *report_enum = hid->report_enum + type;
  1686. struct hid_report *report;
  1687. struct hid_driver *hdrv;
  1688. int max_buffer_size = HID_MAX_BUFFER_SIZE;
  1689. u32 rsize, csize = size;
  1690. u8 *cdata = data;
  1691. int ret = 0;
  1692. report = hid_get_report(report_enum, data);
  1693. if (!report)
  1694. goto out;
  1695. if (report_enum->numbered) {
  1696. cdata++;
  1697. csize--;
  1698. }
  1699. rsize = hid_compute_report_size(report);
  1700. if (hid->ll_driver->max_buffer_size)
  1701. max_buffer_size = hid->ll_driver->max_buffer_size;
  1702. if (report_enum->numbered && rsize >= max_buffer_size)
  1703. rsize = max_buffer_size - 1;
  1704. else if (rsize > max_buffer_size)
  1705. rsize = max_buffer_size;
  1706. if (csize < rsize) {
  1707. dbg_hid("report %d is too short, (%d < %d)\n", report->id,
  1708. csize, rsize);
  1709. memset(cdata + csize, 0, rsize - csize);
  1710. }
  1711. if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
  1712. hid->hiddev_report_event(hid, report);
  1713. if (hid->claimed & HID_CLAIMED_HIDRAW) {
  1714. ret = hidraw_report_event(hid, data, size);
  1715. if (ret)
  1716. goto out;
  1717. }
  1718. if (hid->claimed != HID_CLAIMED_HIDRAW && report->maxfield) {
  1719. hid_process_report(hid, report, cdata, interrupt);
  1720. hdrv = hid->driver;
  1721. if (hdrv && hdrv->report)
  1722. hdrv->report(hid, report);
  1723. }
  1724. if (hid->claimed & HID_CLAIMED_INPUT)
  1725. hidinput_report_event(hid, report);
  1726. out:
  1727. return ret;
  1728. }
  1729. EXPORT_SYMBOL_GPL(hid_report_raw_event);
  1730. static int __hid_input_report(struct hid_device *hid, enum hid_report_type type,
  1731. u8 *data, u32 size, int interrupt, u64 source, bool from_bpf,
  1732. bool lock_already_taken)
  1733. {
  1734. struct hid_report_enum *report_enum;
  1735. struct hid_driver *hdrv;
  1736. struct hid_report *report;
  1737. int ret = 0;
  1738. if (!hid)
  1739. return -ENODEV;
  1740. ret = down_trylock(&hid->driver_input_lock);
  1741. if (lock_already_taken && !ret) {
  1742. up(&hid->driver_input_lock);
  1743. return -EINVAL;
  1744. } else if (!lock_already_taken && ret) {
  1745. return -EBUSY;
  1746. }
  1747. if (!hid->driver) {
  1748. ret = -ENODEV;
  1749. goto unlock;
  1750. }
  1751. report_enum = hid->report_enum + type;
  1752. hdrv = hid->driver;
  1753. data = dispatch_hid_bpf_device_event(hid, type, data, &size, interrupt, source, from_bpf);
  1754. if (IS_ERR(data)) {
  1755. ret = PTR_ERR(data);
  1756. goto unlock;
  1757. }
  1758. if (!size) {
  1759. dbg_hid("empty report\n");
  1760. ret = -1;
  1761. goto unlock;
  1762. }
  1763. /* Avoid unnecessary overhead if debugfs is disabled */
  1764. if (!list_empty(&hid->debug_list))
  1765. hid_dump_report(hid, type, data, size);
  1766. report = hid_get_report(report_enum, data);
  1767. if (!report) {
  1768. ret = -1;
  1769. goto unlock;
  1770. }
  1771. if (hdrv && hdrv->raw_event && hid_match_report(hid, report)) {
  1772. ret = hdrv->raw_event(hid, report, data, size);
  1773. if (ret < 0)
  1774. goto unlock;
  1775. }
  1776. ret = hid_report_raw_event(hid, type, data, size, interrupt);
  1777. unlock:
  1778. if (!lock_already_taken)
  1779. up(&hid->driver_input_lock);
  1780. return ret;
  1781. }
  1782. /**
  1783. * hid_input_report - report data from lower layer (usb, bt...)
  1784. *
  1785. * @hid: hid device
  1786. * @type: HID report type (HID_*_REPORT)
  1787. * @data: report contents
  1788. * @size: size of data parameter
  1789. * @interrupt: distinguish between interrupt and control transfers
  1790. *
  1791. * This is data entry for lower layers.
  1792. */
  1793. int hid_input_report(struct hid_device *hid, enum hid_report_type type, u8 *data, u32 size,
  1794. int interrupt)
  1795. {
  1796. return __hid_input_report(hid, type, data, size, interrupt, 0,
  1797. false, /* from_bpf */
  1798. false /* lock_already_taken */);
  1799. }
  1800. EXPORT_SYMBOL_GPL(hid_input_report);
  1801. bool hid_match_one_id(const struct hid_device *hdev,
  1802. const struct hid_device_id *id)
  1803. {
  1804. return (id->bus == HID_BUS_ANY || id->bus == hdev->bus) &&
  1805. (id->group == HID_GROUP_ANY || id->group == hdev->group) &&
  1806. (id->vendor == HID_ANY_ID || id->vendor == hdev->vendor) &&
  1807. (id->product == HID_ANY_ID || id->product == hdev->product);
  1808. }
  1809. const struct hid_device_id *hid_match_id(const struct hid_device *hdev,
  1810. const struct hid_device_id *id)
  1811. {
  1812. for (; id->bus; id++)
  1813. if (hid_match_one_id(hdev, id))
  1814. return id;
  1815. return NULL;
  1816. }
  1817. EXPORT_SYMBOL_GPL(hid_match_id);
  1818. static const struct hid_device_id hid_hiddev_list[] = {
  1819. { HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS) },
  1820. { HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1) },
  1821. { }
  1822. };
  1823. static bool hid_hiddev(struct hid_device *hdev)
  1824. {
  1825. return !!hid_match_id(hdev, hid_hiddev_list);
  1826. }
  1827. static ssize_t
  1828. read_report_descriptor(struct file *filp, struct kobject *kobj,
  1829. struct bin_attribute *attr,
  1830. char *buf, loff_t off, size_t count)
  1831. {
  1832. struct device *dev = kobj_to_dev(kobj);
  1833. struct hid_device *hdev = to_hid_device(dev);
  1834. if (off >= hdev->rsize)
  1835. return 0;
  1836. if (off + count > hdev->rsize)
  1837. count = hdev->rsize - off;
  1838. memcpy(buf, hdev->rdesc + off, count);
  1839. return count;
  1840. }
  1841. static ssize_t
  1842. show_country(struct device *dev, struct device_attribute *attr,
  1843. char *buf)
  1844. {
  1845. struct hid_device *hdev = to_hid_device(dev);
  1846. return sprintf(buf, "%02x\n", hdev->country & 0xff);
  1847. }
  1848. static struct bin_attribute dev_bin_attr_report_desc = {
  1849. .attr = { .name = "report_descriptor", .mode = 0444 },
  1850. .read = read_report_descriptor,
  1851. .size = HID_MAX_DESCRIPTOR_SIZE,
  1852. };
  1853. static const struct device_attribute dev_attr_country = {
  1854. .attr = { .name = "country", .mode = 0444 },
  1855. .show = show_country,
  1856. };
  1857. int hid_connect(struct hid_device *hdev, unsigned int connect_mask)
  1858. {
  1859. static const char *types[] = { "Device", "Pointer", "Mouse", "Device",
  1860. "Joystick", "Gamepad", "Keyboard", "Keypad",
  1861. "Multi-Axis Controller"
  1862. };
  1863. const char *type, *bus;
  1864. char buf[64] = "";
  1865. unsigned int i;
  1866. int len;
  1867. int ret;
  1868. ret = hid_bpf_connect_device(hdev);
  1869. if (ret)
  1870. return ret;
  1871. if (hdev->quirks & HID_QUIRK_HIDDEV_FORCE)
  1872. connect_mask |= (HID_CONNECT_HIDDEV_FORCE | HID_CONNECT_HIDDEV);
  1873. if (hdev->quirks & HID_QUIRK_HIDINPUT_FORCE)
  1874. connect_mask |= HID_CONNECT_HIDINPUT_FORCE;
  1875. if (hdev->bus != BUS_USB)
  1876. connect_mask &= ~HID_CONNECT_HIDDEV;
  1877. if (hid_hiddev(hdev))
  1878. connect_mask |= HID_CONNECT_HIDDEV_FORCE;
  1879. if ((connect_mask & HID_CONNECT_HIDINPUT) && !hidinput_connect(hdev,
  1880. connect_mask & HID_CONNECT_HIDINPUT_FORCE))
  1881. hdev->claimed |= HID_CLAIMED_INPUT;
  1882. if ((connect_mask & HID_CONNECT_HIDDEV) && hdev->hiddev_connect &&
  1883. !hdev->hiddev_connect(hdev,
  1884. connect_mask & HID_CONNECT_HIDDEV_FORCE))
  1885. hdev->claimed |= HID_CLAIMED_HIDDEV;
  1886. if ((connect_mask & HID_CONNECT_HIDRAW) && !hidraw_connect(hdev))
  1887. hdev->claimed |= HID_CLAIMED_HIDRAW;
  1888. if (connect_mask & HID_CONNECT_DRIVER)
  1889. hdev->claimed |= HID_CLAIMED_DRIVER;
  1890. /* Drivers with the ->raw_event callback set are not required to connect
  1891. * to any other listener. */
  1892. if (!hdev->claimed && !hdev->driver->raw_event) {
  1893. hid_err(hdev, "device has no listeners, quitting\n");
  1894. return -ENODEV;
  1895. }
  1896. hid_process_ordering(hdev);
  1897. if ((hdev->claimed & HID_CLAIMED_INPUT) &&
  1898. (connect_mask & HID_CONNECT_FF) && hdev->ff_init)
  1899. hdev->ff_init(hdev);
  1900. len = 0;
  1901. if (hdev->claimed & HID_CLAIMED_INPUT)
  1902. len += sprintf(buf + len, "input");
  1903. if (hdev->claimed & HID_CLAIMED_HIDDEV)
  1904. len += sprintf(buf + len, "%shiddev%d", len ? "," : "",
  1905. ((struct hiddev *)hdev->hiddev)->minor);
  1906. if (hdev->claimed & HID_CLAIMED_HIDRAW)
  1907. len += sprintf(buf + len, "%shidraw%d", len ? "," : "",
  1908. ((struct hidraw *)hdev->hidraw)->minor);
  1909. type = "Device";
  1910. for (i = 0; i < hdev->maxcollection; i++) {
  1911. struct hid_collection *col = &hdev->collection[i];
  1912. if (col->type == HID_COLLECTION_APPLICATION &&
  1913. (col->usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
  1914. (col->usage & 0xffff) < ARRAY_SIZE(types)) {
  1915. type = types[col->usage & 0xffff];
  1916. break;
  1917. }
  1918. }
  1919. switch (hdev->bus) {
  1920. case BUS_USB:
  1921. bus = "USB";
  1922. break;
  1923. case BUS_BLUETOOTH:
  1924. bus = "BLUETOOTH";
  1925. break;
  1926. case BUS_I2C:
  1927. bus = "I2C";
  1928. break;
  1929. case BUS_VIRTUAL:
  1930. bus = "VIRTUAL";
  1931. break;
  1932. case BUS_INTEL_ISHTP:
  1933. case BUS_AMD_SFH:
  1934. bus = "SENSOR HUB";
  1935. break;
  1936. default:
  1937. bus = "<UNKNOWN>";
  1938. }
  1939. ret = device_create_file(&hdev->dev, &dev_attr_country);
  1940. if (ret)
  1941. hid_warn(hdev,
  1942. "can't create sysfs country code attribute err: %d\n", ret);
  1943. hid_info(hdev, "%s: %s HID v%x.%02x %s [%s] on %s\n",
  1944. buf, bus, hdev->version >> 8, hdev->version & 0xff,
  1945. type, hdev->name, hdev->phys);
  1946. return 0;
  1947. }
  1948. EXPORT_SYMBOL_GPL(hid_connect);
  1949. void hid_disconnect(struct hid_device *hdev)
  1950. {
  1951. device_remove_file(&hdev->dev, &dev_attr_country);
  1952. if (hdev->claimed & HID_CLAIMED_INPUT)
  1953. hidinput_disconnect(hdev);
  1954. if (hdev->claimed & HID_CLAIMED_HIDDEV)
  1955. hdev->hiddev_disconnect(hdev);
  1956. if (hdev->claimed & HID_CLAIMED_HIDRAW)
  1957. hidraw_disconnect(hdev);
  1958. hdev->claimed = 0;
  1959. hid_bpf_disconnect_device(hdev);
  1960. }
  1961. EXPORT_SYMBOL_GPL(hid_disconnect);
  1962. /**
  1963. * hid_hw_start - start underlying HW
  1964. * @hdev: hid device
  1965. * @connect_mask: which outputs to connect, see HID_CONNECT_*
  1966. *
  1967. * Call this in probe function *after* hid_parse. This will setup HW
  1968. * buffers and start the device (if not defeirred to device open).
  1969. * hid_hw_stop must be called if this was successful.
  1970. */
  1971. int hid_hw_start(struct hid_device *hdev, unsigned int connect_mask)
  1972. {
  1973. int error;
  1974. error = hdev->ll_driver->start(hdev);
  1975. if (error)
  1976. return error;
  1977. if (connect_mask) {
  1978. error = hid_connect(hdev, connect_mask);
  1979. if (error) {
  1980. hdev->ll_driver->stop(hdev);
  1981. return error;
  1982. }
  1983. }
  1984. return 0;
  1985. }
  1986. EXPORT_SYMBOL_GPL(hid_hw_start);
  1987. /**
  1988. * hid_hw_stop - stop underlying HW
  1989. * @hdev: hid device
  1990. *
  1991. * This is usually called from remove function or from probe when something
  1992. * failed and hid_hw_start was called already.
  1993. */
  1994. void hid_hw_stop(struct hid_device *hdev)
  1995. {
  1996. hid_disconnect(hdev);
  1997. hdev->ll_driver->stop(hdev);
  1998. }
  1999. EXPORT_SYMBOL_GPL(hid_hw_stop);
  2000. /**
  2001. * hid_hw_open - signal underlying HW to start delivering events
  2002. * @hdev: hid device
  2003. *
  2004. * Tell underlying HW to start delivering events from the device.
  2005. * This function should be called sometime after successful call
  2006. * to hid_hw_start().
  2007. */
  2008. int hid_hw_open(struct hid_device *hdev)
  2009. {
  2010. int ret;
  2011. ret = mutex_lock_killable(&hdev->ll_open_lock);
  2012. if (ret)
  2013. return ret;
  2014. if (!hdev->ll_open_count++) {
  2015. ret = hdev->ll_driver->open(hdev);
  2016. if (ret)
  2017. hdev->ll_open_count--;
  2018. }
  2019. mutex_unlock(&hdev->ll_open_lock);
  2020. return ret;
  2021. }
  2022. EXPORT_SYMBOL_GPL(hid_hw_open);
  2023. /**
  2024. * hid_hw_close - signal underlaying HW to stop delivering events
  2025. *
  2026. * @hdev: hid device
  2027. *
  2028. * This function indicates that we are not interested in the events
  2029. * from this device anymore. Delivery of events may or may not stop,
  2030. * depending on the number of users still outstanding.
  2031. */
  2032. void hid_hw_close(struct hid_device *hdev)
  2033. {
  2034. mutex_lock(&hdev->ll_open_lock);
  2035. if (!--hdev->ll_open_count)
  2036. hdev->ll_driver->close(hdev);
  2037. mutex_unlock(&hdev->ll_open_lock);
  2038. }
  2039. EXPORT_SYMBOL_GPL(hid_hw_close);
  2040. /**
  2041. * hid_hw_request - send report request to device
  2042. *
  2043. * @hdev: hid device
  2044. * @report: report to send
  2045. * @reqtype: hid request type
  2046. */
  2047. void hid_hw_request(struct hid_device *hdev,
  2048. struct hid_report *report, enum hid_class_request reqtype)
  2049. {
  2050. if (hdev->ll_driver->request)
  2051. return hdev->ll_driver->request(hdev, report, reqtype);
  2052. __hid_request(hdev, report, reqtype);
  2053. }
  2054. EXPORT_SYMBOL_GPL(hid_hw_request);
  2055. int __hid_hw_raw_request(struct hid_device *hdev,
  2056. unsigned char reportnum, __u8 *buf,
  2057. size_t len, enum hid_report_type rtype,
  2058. enum hid_class_request reqtype,
  2059. u64 source, bool from_bpf)
  2060. {
  2061. unsigned int max_buffer_size = HID_MAX_BUFFER_SIZE;
  2062. int ret;
  2063. if (hdev->ll_driver->max_buffer_size)
  2064. max_buffer_size = hdev->ll_driver->max_buffer_size;
  2065. if (len < 1 || len > max_buffer_size || !buf)
  2066. return -EINVAL;
  2067. ret = dispatch_hid_bpf_raw_requests(hdev, reportnum, buf, len, rtype,
  2068. reqtype, source, from_bpf);
  2069. if (ret)
  2070. return ret;
  2071. return hdev->ll_driver->raw_request(hdev, reportnum, buf, len,
  2072. rtype, reqtype);
  2073. }
  2074. /**
  2075. * hid_hw_raw_request - send report request to device
  2076. *
  2077. * @hdev: hid device
  2078. * @reportnum: report ID
  2079. * @buf: in/out data to transfer
  2080. * @len: length of buf
  2081. * @rtype: HID report type
  2082. * @reqtype: HID_REQ_GET_REPORT or HID_REQ_SET_REPORT
  2083. *
  2084. * Return: count of data transferred, negative if error
  2085. *
  2086. * Same behavior as hid_hw_request, but with raw buffers instead.
  2087. */
  2088. int hid_hw_raw_request(struct hid_device *hdev,
  2089. unsigned char reportnum, __u8 *buf,
  2090. size_t len, enum hid_report_type rtype, enum hid_class_request reqtype)
  2091. {
  2092. return __hid_hw_raw_request(hdev, reportnum, buf, len, rtype, reqtype, 0, false);
  2093. }
  2094. EXPORT_SYMBOL_GPL(hid_hw_raw_request);
  2095. int __hid_hw_output_report(struct hid_device *hdev, __u8 *buf, size_t len, u64 source,
  2096. bool from_bpf)
  2097. {
  2098. unsigned int max_buffer_size = HID_MAX_BUFFER_SIZE;
  2099. int ret;
  2100. if (hdev->ll_driver->max_buffer_size)
  2101. max_buffer_size = hdev->ll_driver->max_buffer_size;
  2102. if (len < 1 || len > max_buffer_size || !buf)
  2103. return -EINVAL;
  2104. ret = dispatch_hid_bpf_output_report(hdev, buf, len, source, from_bpf);
  2105. if (ret)
  2106. return ret;
  2107. if (hdev->ll_driver->output_report)
  2108. return hdev->ll_driver->output_report(hdev, buf, len);
  2109. return -ENOSYS;
  2110. }
  2111. /**
  2112. * hid_hw_output_report - send output report to device
  2113. *
  2114. * @hdev: hid device
  2115. * @buf: raw data to transfer
  2116. * @len: length of buf
  2117. *
  2118. * Return: count of data transferred, negative if error
  2119. */
  2120. int hid_hw_output_report(struct hid_device *hdev, __u8 *buf, size_t len)
  2121. {
  2122. return __hid_hw_output_report(hdev, buf, len, 0, false);
  2123. }
  2124. EXPORT_SYMBOL_GPL(hid_hw_output_report);
  2125. #ifdef CONFIG_PM
  2126. int hid_driver_suspend(struct hid_device *hdev, pm_message_t state)
  2127. {
  2128. if (hdev->driver && hdev->driver->suspend)
  2129. return hdev->driver->suspend(hdev, state);
  2130. return 0;
  2131. }
  2132. EXPORT_SYMBOL_GPL(hid_driver_suspend);
  2133. int hid_driver_reset_resume(struct hid_device *hdev)
  2134. {
  2135. if (hdev->driver && hdev->driver->reset_resume)
  2136. return hdev->driver->reset_resume(hdev);
  2137. return 0;
  2138. }
  2139. EXPORT_SYMBOL_GPL(hid_driver_reset_resume);
  2140. int hid_driver_resume(struct hid_device *hdev)
  2141. {
  2142. if (hdev->driver && hdev->driver->resume)
  2143. return hdev->driver->resume(hdev);
  2144. return 0;
  2145. }
  2146. EXPORT_SYMBOL_GPL(hid_driver_resume);
  2147. #endif /* CONFIG_PM */
  2148. struct hid_dynid {
  2149. struct list_head list;
  2150. struct hid_device_id id;
  2151. };
  2152. /**
  2153. * new_id_store - add a new HID device ID to this driver and re-probe devices
  2154. * @drv: target device driver
  2155. * @buf: buffer for scanning device ID data
  2156. * @count: input size
  2157. *
  2158. * Adds a new dynamic hid device ID to this driver,
  2159. * and causes the driver to probe for all devices again.
  2160. */
  2161. static ssize_t new_id_store(struct device_driver *drv, const char *buf,
  2162. size_t count)
  2163. {
  2164. struct hid_driver *hdrv = to_hid_driver(drv);
  2165. struct hid_dynid *dynid;
  2166. __u32 bus, vendor, product;
  2167. unsigned long driver_data = 0;
  2168. int ret;
  2169. ret = sscanf(buf, "%x %x %x %lx",
  2170. &bus, &vendor, &product, &driver_data);
  2171. if (ret < 3)
  2172. return -EINVAL;
  2173. dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
  2174. if (!dynid)
  2175. return -ENOMEM;
  2176. dynid->id.bus = bus;
  2177. dynid->id.group = HID_GROUP_ANY;
  2178. dynid->id.vendor = vendor;
  2179. dynid->id.product = product;
  2180. dynid->id.driver_data = driver_data;
  2181. spin_lock(&hdrv->dyn_lock);
  2182. list_add_tail(&dynid->list, &hdrv->dyn_list);
  2183. spin_unlock(&hdrv->dyn_lock);
  2184. ret = driver_attach(&hdrv->driver);
  2185. return ret ? : count;
  2186. }
  2187. static DRIVER_ATTR_WO(new_id);
  2188. static struct attribute *hid_drv_attrs[] = {
  2189. &driver_attr_new_id.attr,
  2190. NULL,
  2191. };
  2192. ATTRIBUTE_GROUPS(hid_drv);
  2193. static void hid_free_dynids(struct hid_driver *hdrv)
  2194. {
  2195. struct hid_dynid *dynid, *n;
  2196. spin_lock(&hdrv->dyn_lock);
  2197. list_for_each_entry_safe(dynid, n, &hdrv->dyn_list, list) {
  2198. list_del(&dynid->list);
  2199. kfree(dynid);
  2200. }
  2201. spin_unlock(&hdrv->dyn_lock);
  2202. }
  2203. const struct hid_device_id *hid_match_device(struct hid_device *hdev,
  2204. struct hid_driver *hdrv)
  2205. {
  2206. struct hid_dynid *dynid;
  2207. spin_lock(&hdrv->dyn_lock);
  2208. list_for_each_entry(dynid, &hdrv->dyn_list, list) {
  2209. if (hid_match_one_id(hdev, &dynid->id)) {
  2210. spin_unlock(&hdrv->dyn_lock);
  2211. return &dynid->id;
  2212. }
  2213. }
  2214. spin_unlock(&hdrv->dyn_lock);
  2215. return hid_match_id(hdev, hdrv->id_table);
  2216. }
  2217. EXPORT_SYMBOL_GPL(hid_match_device);
  2218. static int hid_bus_match(struct device *dev, const struct device_driver *drv)
  2219. {
  2220. struct hid_driver *hdrv = to_hid_driver(drv);
  2221. struct hid_device *hdev = to_hid_device(dev);
  2222. return hid_match_device(hdev, hdrv) != NULL;
  2223. }
  2224. /**
  2225. * hid_compare_device_paths - check if both devices share the same path
  2226. * @hdev_a: hid device
  2227. * @hdev_b: hid device
  2228. * @separator: char to use as separator
  2229. *
  2230. * Check if two devices share the same path up to the last occurrence of
  2231. * the separator char. Both paths must exist (i.e., zero-length paths
  2232. * don't match).
  2233. */
  2234. bool hid_compare_device_paths(struct hid_device *hdev_a,
  2235. struct hid_device *hdev_b, char separator)
  2236. {
  2237. int n1 = strrchr(hdev_a->phys, separator) - hdev_a->phys;
  2238. int n2 = strrchr(hdev_b->phys, separator) - hdev_b->phys;
  2239. if (n1 != n2 || n1 <= 0 || n2 <= 0)
  2240. return false;
  2241. return !strncmp(hdev_a->phys, hdev_b->phys, n1);
  2242. }
  2243. EXPORT_SYMBOL_GPL(hid_compare_device_paths);
  2244. static bool hid_check_device_match(struct hid_device *hdev,
  2245. struct hid_driver *hdrv,
  2246. const struct hid_device_id **id)
  2247. {
  2248. *id = hid_match_device(hdev, hdrv);
  2249. if (!*id)
  2250. return false;
  2251. if (hdrv->match)
  2252. return hdrv->match(hdev, hid_ignore_special_drivers);
  2253. /*
  2254. * hid-generic implements .match(), so we must be dealing with a
  2255. * different HID driver here, and can simply check if
  2256. * hid_ignore_special_drivers or HID_QUIRK_IGNORE_SPECIAL_DRIVER
  2257. * are set or not.
  2258. */
  2259. return !hid_ignore_special_drivers && !(hdev->quirks & HID_QUIRK_IGNORE_SPECIAL_DRIVER);
  2260. }
  2261. static int __hid_device_probe(struct hid_device *hdev, struct hid_driver *hdrv)
  2262. {
  2263. const struct hid_device_id *id;
  2264. int ret;
  2265. if (!hid_check_device_match(hdev, hdrv, &id))
  2266. return -ENODEV;
  2267. hdev->devres_group_id = devres_open_group(&hdev->dev, NULL, GFP_KERNEL);
  2268. if (!hdev->devres_group_id)
  2269. return -ENOMEM;
  2270. /* reset the quirks that has been previously set */
  2271. hdev->quirks = hid_lookup_quirk(hdev);
  2272. hdev->driver = hdrv;
  2273. if (hdrv->probe) {
  2274. ret = hdrv->probe(hdev, id);
  2275. } else { /* default probe */
  2276. ret = hid_open_report(hdev);
  2277. if (!ret)
  2278. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  2279. }
  2280. /*
  2281. * Note that we are not closing the devres group opened above so
  2282. * even resources that were attached to the device after probe is
  2283. * run are released when hid_device_remove() is executed. This is
  2284. * needed as some drivers would allocate additional resources,
  2285. * for example when updating firmware.
  2286. */
  2287. if (ret) {
  2288. devres_release_group(&hdev->dev, hdev->devres_group_id);
  2289. hid_close_report(hdev);
  2290. hdev->driver = NULL;
  2291. }
  2292. return ret;
  2293. }
  2294. static int hid_device_probe(struct device *dev)
  2295. {
  2296. struct hid_device *hdev = to_hid_device(dev);
  2297. struct hid_driver *hdrv = to_hid_driver(dev->driver);
  2298. int ret = 0;
  2299. if (down_interruptible(&hdev->driver_input_lock))
  2300. return -EINTR;
  2301. hdev->io_started = false;
  2302. clear_bit(ffs(HID_STAT_REPROBED), &hdev->status);
  2303. if (!hdev->driver)
  2304. ret = __hid_device_probe(hdev, hdrv);
  2305. if (!hdev->io_started)
  2306. up(&hdev->driver_input_lock);
  2307. return ret;
  2308. }
  2309. static void hid_device_remove(struct device *dev)
  2310. {
  2311. struct hid_device *hdev = to_hid_device(dev);
  2312. struct hid_driver *hdrv;
  2313. down(&hdev->driver_input_lock);
  2314. hdev->io_started = false;
  2315. hdrv = hdev->driver;
  2316. if (hdrv) {
  2317. if (hdrv->remove)
  2318. hdrv->remove(hdev);
  2319. else /* default remove */
  2320. hid_hw_stop(hdev);
  2321. /* Release all devres resources allocated by the driver */
  2322. devres_release_group(&hdev->dev, hdev->devres_group_id);
  2323. hid_close_report(hdev);
  2324. hdev->driver = NULL;
  2325. }
  2326. if (!hdev->io_started)
  2327. up(&hdev->driver_input_lock);
  2328. }
  2329. static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
  2330. char *buf)
  2331. {
  2332. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  2333. return scnprintf(buf, PAGE_SIZE, "hid:b%04Xg%04Xv%08Xp%08X\n",
  2334. hdev->bus, hdev->group, hdev->vendor, hdev->product);
  2335. }
  2336. static DEVICE_ATTR_RO(modalias);
  2337. static struct attribute *hid_dev_attrs[] = {
  2338. &dev_attr_modalias.attr,
  2339. NULL,
  2340. };
  2341. static struct bin_attribute *hid_dev_bin_attrs[] = {
  2342. &dev_bin_attr_report_desc,
  2343. NULL
  2344. };
  2345. static const struct attribute_group hid_dev_group = {
  2346. .attrs = hid_dev_attrs,
  2347. .bin_attrs = hid_dev_bin_attrs,
  2348. };
  2349. __ATTRIBUTE_GROUPS(hid_dev);
  2350. static int hid_uevent(const struct device *dev, struct kobj_uevent_env *env)
  2351. {
  2352. const struct hid_device *hdev = to_hid_device(dev);
  2353. if (add_uevent_var(env, "HID_ID=%04X:%08X:%08X",
  2354. hdev->bus, hdev->vendor, hdev->product))
  2355. return -ENOMEM;
  2356. if (add_uevent_var(env, "HID_NAME=%s", hdev->name))
  2357. return -ENOMEM;
  2358. if (add_uevent_var(env, "HID_PHYS=%s", hdev->phys))
  2359. return -ENOMEM;
  2360. if (add_uevent_var(env, "HID_UNIQ=%s", hdev->uniq))
  2361. return -ENOMEM;
  2362. if (add_uevent_var(env, "MODALIAS=hid:b%04Xg%04Xv%08Xp%08X",
  2363. hdev->bus, hdev->group, hdev->vendor, hdev->product))
  2364. return -ENOMEM;
  2365. return 0;
  2366. }
  2367. const struct bus_type hid_bus_type = {
  2368. .name = "hid",
  2369. .dev_groups = hid_dev_groups,
  2370. .drv_groups = hid_drv_groups,
  2371. .match = hid_bus_match,
  2372. .probe = hid_device_probe,
  2373. .remove = hid_device_remove,
  2374. .uevent = hid_uevent,
  2375. };
  2376. EXPORT_SYMBOL(hid_bus_type);
  2377. int hid_add_device(struct hid_device *hdev)
  2378. {
  2379. static atomic_t id = ATOMIC_INIT(0);
  2380. int ret;
  2381. if (WARN_ON(hdev->status & HID_STAT_ADDED))
  2382. return -EBUSY;
  2383. hdev->quirks = hid_lookup_quirk(hdev);
  2384. /* we need to kill them here, otherwise they will stay allocated to
  2385. * wait for coming driver */
  2386. if (hid_ignore(hdev))
  2387. return -ENODEV;
  2388. /*
  2389. * Check for the mandatory transport channel.
  2390. */
  2391. if (!hdev->ll_driver->raw_request) {
  2392. hid_err(hdev, "transport driver missing .raw_request()\n");
  2393. return -EINVAL;
  2394. }
  2395. /*
  2396. * Read the device report descriptor once and use as template
  2397. * for the driver-specific modifications.
  2398. */
  2399. ret = hdev->ll_driver->parse(hdev);
  2400. if (ret)
  2401. return ret;
  2402. if (!hdev->dev_rdesc)
  2403. return -ENODEV;
  2404. /*
  2405. * Scan generic devices for group information
  2406. */
  2407. if (hid_ignore_special_drivers) {
  2408. hdev->group = HID_GROUP_GENERIC;
  2409. } else if (!hdev->group &&
  2410. !(hdev->quirks & HID_QUIRK_HAVE_SPECIAL_DRIVER)) {
  2411. ret = hid_scan_report(hdev);
  2412. if (ret)
  2413. hid_warn(hdev, "bad device descriptor (%d)\n", ret);
  2414. }
  2415. hdev->id = atomic_inc_return(&id);
  2416. /* XXX hack, any other cleaner solution after the driver core
  2417. * is converted to allow more than 20 bytes as the device name? */
  2418. dev_set_name(&hdev->dev, "%04X:%04X:%04X.%04X", hdev->bus,
  2419. hdev->vendor, hdev->product, hdev->id);
  2420. hid_debug_register(hdev, dev_name(&hdev->dev));
  2421. ret = device_add(&hdev->dev);
  2422. if (!ret)
  2423. hdev->status |= HID_STAT_ADDED;
  2424. else
  2425. hid_debug_unregister(hdev);
  2426. return ret;
  2427. }
  2428. EXPORT_SYMBOL_GPL(hid_add_device);
  2429. /**
  2430. * hid_allocate_device - allocate new hid device descriptor
  2431. *
  2432. * Allocate and initialize hid device, so that hid_destroy_device might be
  2433. * used to free it.
  2434. *
  2435. * New hid_device pointer is returned on success, otherwise ERR_PTR encoded
  2436. * error value.
  2437. */
  2438. struct hid_device *hid_allocate_device(void)
  2439. {
  2440. struct hid_device *hdev;
  2441. int ret = -ENOMEM;
  2442. hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
  2443. if (hdev == NULL)
  2444. return ERR_PTR(ret);
  2445. device_initialize(&hdev->dev);
  2446. hdev->dev.release = hid_device_release;
  2447. hdev->dev.bus = &hid_bus_type;
  2448. device_enable_async_suspend(&hdev->dev);
  2449. hid_close_report(hdev);
  2450. init_waitqueue_head(&hdev->debug_wait);
  2451. INIT_LIST_HEAD(&hdev->debug_list);
  2452. spin_lock_init(&hdev->debug_list_lock);
  2453. sema_init(&hdev->driver_input_lock, 1);
  2454. mutex_init(&hdev->ll_open_lock);
  2455. kref_init(&hdev->ref);
  2456. ret = hid_bpf_device_init(hdev);
  2457. if (ret)
  2458. goto out_err;
  2459. return hdev;
  2460. out_err:
  2461. hid_destroy_device(hdev);
  2462. return ERR_PTR(ret);
  2463. }
  2464. EXPORT_SYMBOL_GPL(hid_allocate_device);
  2465. static void hid_remove_device(struct hid_device *hdev)
  2466. {
  2467. if (hdev->status & HID_STAT_ADDED) {
  2468. device_del(&hdev->dev);
  2469. hid_debug_unregister(hdev);
  2470. hdev->status &= ~HID_STAT_ADDED;
  2471. }
  2472. kfree(hdev->dev_rdesc);
  2473. hdev->dev_rdesc = NULL;
  2474. hdev->dev_rsize = 0;
  2475. }
  2476. /**
  2477. * hid_destroy_device - free previously allocated device
  2478. *
  2479. * @hdev: hid device
  2480. *
  2481. * If you allocate hid_device through hid_allocate_device, you should ever
  2482. * free by this function.
  2483. */
  2484. void hid_destroy_device(struct hid_device *hdev)
  2485. {
  2486. hid_bpf_destroy_device(hdev);
  2487. hid_remove_device(hdev);
  2488. put_device(&hdev->dev);
  2489. }
  2490. EXPORT_SYMBOL_GPL(hid_destroy_device);
  2491. static int __hid_bus_reprobe_drivers(struct device *dev, void *data)
  2492. {
  2493. struct hid_driver *hdrv = data;
  2494. struct hid_device *hdev = to_hid_device(dev);
  2495. if (hdev->driver == hdrv &&
  2496. !hdrv->match(hdev, hid_ignore_special_drivers) &&
  2497. !test_and_set_bit(ffs(HID_STAT_REPROBED), &hdev->status))
  2498. return device_reprobe(dev);
  2499. return 0;
  2500. }
  2501. static int __hid_bus_driver_added(struct device_driver *drv, void *data)
  2502. {
  2503. struct hid_driver *hdrv = to_hid_driver(drv);
  2504. if (hdrv->match) {
  2505. bus_for_each_dev(&hid_bus_type, NULL, hdrv,
  2506. __hid_bus_reprobe_drivers);
  2507. }
  2508. return 0;
  2509. }
  2510. static int __bus_removed_driver(struct device_driver *drv, void *data)
  2511. {
  2512. return bus_rescan_devices(&hid_bus_type);
  2513. }
  2514. int __hid_register_driver(struct hid_driver *hdrv, struct module *owner,
  2515. const char *mod_name)
  2516. {
  2517. int ret;
  2518. hdrv->driver.name = hdrv->name;
  2519. hdrv->driver.bus = &hid_bus_type;
  2520. hdrv->driver.owner = owner;
  2521. hdrv->driver.mod_name = mod_name;
  2522. INIT_LIST_HEAD(&hdrv->dyn_list);
  2523. spin_lock_init(&hdrv->dyn_lock);
  2524. ret = driver_register(&hdrv->driver);
  2525. if (ret == 0)
  2526. bus_for_each_drv(&hid_bus_type, NULL, NULL,
  2527. __hid_bus_driver_added);
  2528. return ret;
  2529. }
  2530. EXPORT_SYMBOL_GPL(__hid_register_driver);
  2531. void hid_unregister_driver(struct hid_driver *hdrv)
  2532. {
  2533. driver_unregister(&hdrv->driver);
  2534. hid_free_dynids(hdrv);
  2535. bus_for_each_drv(&hid_bus_type, NULL, hdrv, __bus_removed_driver);
  2536. }
  2537. EXPORT_SYMBOL_GPL(hid_unregister_driver);
  2538. int hid_check_keys_pressed(struct hid_device *hid)
  2539. {
  2540. struct hid_input *hidinput;
  2541. int i;
  2542. if (!(hid->claimed & HID_CLAIMED_INPUT))
  2543. return 0;
  2544. list_for_each_entry(hidinput, &hid->inputs, list) {
  2545. for (i = 0; i < BITS_TO_LONGS(KEY_MAX); i++)
  2546. if (hidinput->input->key[i])
  2547. return 1;
  2548. }
  2549. return 0;
  2550. }
  2551. EXPORT_SYMBOL_GPL(hid_check_keys_pressed);
  2552. #ifdef CONFIG_HID_BPF
  2553. static struct hid_ops __hid_ops = {
  2554. .hid_get_report = hid_get_report,
  2555. .hid_hw_raw_request = __hid_hw_raw_request,
  2556. .hid_hw_output_report = __hid_hw_output_report,
  2557. .hid_input_report = __hid_input_report,
  2558. .owner = THIS_MODULE,
  2559. .bus_type = &hid_bus_type,
  2560. };
  2561. #endif
  2562. static int __init hid_init(void)
  2563. {
  2564. int ret;
  2565. ret = bus_register(&hid_bus_type);
  2566. if (ret) {
  2567. pr_err("can't register hid bus\n");
  2568. goto err;
  2569. }
  2570. #ifdef CONFIG_HID_BPF
  2571. hid_ops = &__hid_ops;
  2572. #endif
  2573. ret = hidraw_init();
  2574. if (ret)
  2575. goto err_bus;
  2576. hid_debug_init();
  2577. return 0;
  2578. err_bus:
  2579. bus_unregister(&hid_bus_type);
  2580. err:
  2581. return ret;
  2582. }
  2583. static void __exit hid_exit(void)
  2584. {
  2585. #ifdef CONFIG_HID_BPF
  2586. hid_ops = NULL;
  2587. #endif
  2588. hid_debug_exit();
  2589. hidraw_exit();
  2590. bus_unregister(&hid_bus_type);
  2591. hid_quirks_exit(HID_BUS_ANY);
  2592. }
  2593. module_init(hid_init);
  2594. module_exit(hid_exit);
  2595. MODULE_AUTHOR("Andreas Gal");
  2596. MODULE_AUTHOR("Vojtech Pavlik");
  2597. MODULE_AUTHOR("Jiri Kosina");
  2598. MODULE_DESCRIPTION("HID support for Linux");
  2599. MODULE_LICENSE("GPL");