gpiolib-cdev.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/anon_inodes.h>
  3. #include <linux/atomic.h>
  4. #include <linux/bitmap.h>
  5. #include <linux/build_bug.h>
  6. #include <linux/cdev.h>
  7. #include <linux/compat.h>
  8. #include <linux/compiler.h>
  9. #include <linux/device.h>
  10. #include <linux/err.h>
  11. #include <linux/file.h>
  12. #include <linux/gpio.h>
  13. #include <linux/gpio/driver.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/irqreturn.h>
  16. #include <linux/kernel.h>
  17. #include <linux/kfifo.h>
  18. #include <linux/module.h>
  19. #include <linux/mutex.h>
  20. #include <linux/pinctrl/consumer.h>
  21. #include <linux/poll.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/timekeeping.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/workqueue.h>
  26. #include <linux/hte.h>
  27. #include <uapi/linux/gpio.h>
  28. #include "gpiolib.h"
  29. #include "gpiolib-cdev.h"
  30. /*
  31. * Array sizes must ensure 64-bit alignment and not create holes in the
  32. * struct packing.
  33. */
  34. static_assert(IS_ALIGNED(GPIO_V2_LINES_MAX, 2));
  35. static_assert(IS_ALIGNED(GPIO_MAX_NAME_SIZE, 8));
  36. /*
  37. * Check that uAPI structs are 64-bit aligned for 32/64-bit compatibility
  38. */
  39. static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_attribute), 8));
  40. static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config_attribute), 8));
  41. static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_config), 8));
  42. static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_request), 8));
  43. static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info), 8));
  44. static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_info_changed), 8));
  45. static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_event), 8));
  46. static_assert(IS_ALIGNED(sizeof(struct gpio_v2_line_values), 8));
  47. /* Character device interface to GPIO.
  48. *
  49. * The GPIO character device, /dev/gpiochipN, provides userspace an
  50. * interface to gpiolib GPIOs via ioctl()s.
  51. */
  52. typedef __poll_t (*poll_fn)(struct file *, struct poll_table_struct *);
  53. typedef long (*ioctl_fn)(struct file *, unsigned int, unsigned long);
  54. typedef ssize_t (*read_fn)(struct file *, char __user *,
  55. size_t count, loff_t *);
  56. static __poll_t call_poll_locked(struct file *file,
  57. struct poll_table_struct *wait,
  58. struct gpio_device *gdev, poll_fn func)
  59. {
  60. __poll_t ret;
  61. down_read(&gdev->sem);
  62. ret = func(file, wait);
  63. up_read(&gdev->sem);
  64. return ret;
  65. }
  66. static long call_ioctl_locked(struct file *file, unsigned int cmd,
  67. unsigned long arg, struct gpio_device *gdev,
  68. ioctl_fn func)
  69. {
  70. long ret;
  71. down_read(&gdev->sem);
  72. ret = func(file, cmd, arg);
  73. up_read(&gdev->sem);
  74. return ret;
  75. }
  76. static ssize_t call_read_locked(struct file *file, char __user *buf,
  77. size_t count, loff_t *f_ps,
  78. struct gpio_device *gdev, read_fn func)
  79. {
  80. ssize_t ret;
  81. down_read(&gdev->sem);
  82. ret = func(file, buf, count, f_ps);
  83. up_read(&gdev->sem);
  84. return ret;
  85. }
  86. /*
  87. * GPIO line handle management
  88. */
  89. #ifdef CONFIG_GPIO_CDEV_V1
  90. /**
  91. * struct linehandle_state - contains the state of a userspace handle
  92. * @gdev: the GPIO device the handle pertains to
  93. * @label: consumer label used to tag descriptors
  94. * @descs: the GPIO descriptors held by this handle
  95. * @num_descs: the number of descriptors held in the descs array
  96. */
  97. struct linehandle_state {
  98. struct gpio_device *gdev;
  99. const char *label;
  100. struct gpio_desc *descs[GPIOHANDLES_MAX];
  101. u32 num_descs;
  102. };
  103. #define GPIOHANDLE_REQUEST_VALID_FLAGS \
  104. (GPIOHANDLE_REQUEST_INPUT | \
  105. GPIOHANDLE_REQUEST_OUTPUT | \
  106. GPIOHANDLE_REQUEST_ACTIVE_LOW | \
  107. GPIOHANDLE_REQUEST_BIAS_PULL_UP | \
  108. GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \
  109. GPIOHANDLE_REQUEST_BIAS_DISABLE | \
  110. GPIOHANDLE_REQUEST_OPEN_DRAIN | \
  111. GPIOHANDLE_REQUEST_OPEN_SOURCE)
  112. static int linehandle_validate_flags(u32 flags)
  113. {
  114. /* Return an error if an unknown flag is set */
  115. if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
  116. return -EINVAL;
  117. /*
  118. * Do not allow both INPUT & OUTPUT flags to be set as they are
  119. * contradictory.
  120. */
  121. if ((flags & GPIOHANDLE_REQUEST_INPUT) &&
  122. (flags & GPIOHANDLE_REQUEST_OUTPUT))
  123. return -EINVAL;
  124. /*
  125. * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
  126. * the hardware actually supports enabling both at the same time the
  127. * electrical result would be disastrous.
  128. */
  129. if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
  130. (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
  131. return -EINVAL;
  132. /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
  133. if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) &&
  134. ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
  135. (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
  136. return -EINVAL;
  137. /* Bias flags only allowed for input or output mode. */
  138. if (!((flags & GPIOHANDLE_REQUEST_INPUT) ||
  139. (flags & GPIOHANDLE_REQUEST_OUTPUT)) &&
  140. ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) ||
  141. (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) ||
  142. (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)))
  143. return -EINVAL;
  144. /* Only one bias flag can be set. */
  145. if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
  146. (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
  147. GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
  148. ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
  149. (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
  150. return -EINVAL;
  151. return 0;
  152. }
  153. static void linehandle_flags_to_desc_flags(u32 lflags, unsigned long *flagsp)
  154. {
  155. assign_bit(FLAG_ACTIVE_LOW, flagsp,
  156. lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW);
  157. assign_bit(FLAG_OPEN_DRAIN, flagsp,
  158. lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN);
  159. assign_bit(FLAG_OPEN_SOURCE, flagsp,
  160. lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE);
  161. assign_bit(FLAG_PULL_UP, flagsp,
  162. lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP);
  163. assign_bit(FLAG_PULL_DOWN, flagsp,
  164. lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN);
  165. assign_bit(FLAG_BIAS_DISABLE, flagsp,
  166. lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE);
  167. }
  168. static long linehandle_set_config(struct linehandle_state *lh,
  169. void __user *ip)
  170. {
  171. struct gpiohandle_config gcnf;
  172. struct gpio_desc *desc;
  173. int i, ret;
  174. u32 lflags;
  175. if (copy_from_user(&gcnf, ip, sizeof(gcnf)))
  176. return -EFAULT;
  177. lflags = gcnf.flags;
  178. ret = linehandle_validate_flags(lflags);
  179. if (ret)
  180. return ret;
  181. for (i = 0; i < lh->num_descs; i++) {
  182. desc = lh->descs[i];
  183. linehandle_flags_to_desc_flags(gcnf.flags, &desc->flags);
  184. /*
  185. * Lines have to be requested explicitly for input
  186. * or output, else the line will be treated "as is".
  187. */
  188. if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
  189. int val = !!gcnf.default_values[i];
  190. ret = gpiod_direction_output(desc, val);
  191. if (ret)
  192. return ret;
  193. } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
  194. ret = gpiod_direction_input(desc);
  195. if (ret)
  196. return ret;
  197. }
  198. blocking_notifier_call_chain(&desc->gdev->notifier,
  199. GPIO_V2_LINE_CHANGED_CONFIG,
  200. desc);
  201. }
  202. return 0;
  203. }
  204. static long linehandle_ioctl_unlocked(struct file *file, unsigned int cmd,
  205. unsigned long arg)
  206. {
  207. struct linehandle_state *lh = file->private_data;
  208. void __user *ip = (void __user *)arg;
  209. struct gpiohandle_data ghd;
  210. DECLARE_BITMAP(vals, GPIOHANDLES_MAX);
  211. unsigned int i;
  212. int ret;
  213. if (!lh->gdev->chip)
  214. return -ENODEV;
  215. switch (cmd) {
  216. case GPIOHANDLE_GET_LINE_VALUES_IOCTL:
  217. /* NOTE: It's okay to read values of output lines */
  218. ret = gpiod_get_array_value_complex(false, true,
  219. lh->num_descs, lh->descs,
  220. NULL, vals);
  221. if (ret)
  222. return ret;
  223. memset(&ghd, 0, sizeof(ghd));
  224. for (i = 0; i < lh->num_descs; i++)
  225. ghd.values[i] = test_bit(i, vals);
  226. if (copy_to_user(ip, &ghd, sizeof(ghd)))
  227. return -EFAULT;
  228. return 0;
  229. case GPIOHANDLE_SET_LINE_VALUES_IOCTL:
  230. /*
  231. * All line descriptors were created at once with the same
  232. * flags so just check if the first one is really output.
  233. */
  234. if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags))
  235. return -EPERM;
  236. if (copy_from_user(&ghd, ip, sizeof(ghd)))
  237. return -EFAULT;
  238. /* Clamp all values to [0,1] */
  239. for (i = 0; i < lh->num_descs; i++)
  240. __assign_bit(i, vals, ghd.values[i]);
  241. /* Reuse the array setting function */
  242. return gpiod_set_array_value_complex(false,
  243. true,
  244. lh->num_descs,
  245. lh->descs,
  246. NULL,
  247. vals);
  248. case GPIOHANDLE_SET_CONFIG_IOCTL:
  249. return linehandle_set_config(lh, ip);
  250. default:
  251. return -EINVAL;
  252. }
  253. }
  254. static long linehandle_ioctl(struct file *file, unsigned int cmd,
  255. unsigned long arg)
  256. {
  257. struct linehandle_state *lh = file->private_data;
  258. return call_ioctl_locked(file, cmd, arg, lh->gdev,
  259. linehandle_ioctl_unlocked);
  260. }
  261. #ifdef CONFIG_COMPAT
  262. static long linehandle_ioctl_compat(struct file *file, unsigned int cmd,
  263. unsigned long arg)
  264. {
  265. return linehandle_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  266. }
  267. #endif
  268. static void linehandle_free(struct linehandle_state *lh)
  269. {
  270. int i;
  271. for (i = 0; i < lh->num_descs; i++)
  272. if (lh->descs[i])
  273. gpiod_free(lh->descs[i]);
  274. kfree(lh->label);
  275. put_device(&lh->gdev->dev);
  276. kfree(lh);
  277. }
  278. static int linehandle_release(struct inode *inode, struct file *file)
  279. {
  280. linehandle_free(file->private_data);
  281. return 0;
  282. }
  283. static const struct file_operations linehandle_fileops = {
  284. .release = linehandle_release,
  285. .owner = THIS_MODULE,
  286. .llseek = noop_llseek,
  287. .unlocked_ioctl = linehandle_ioctl,
  288. #ifdef CONFIG_COMPAT
  289. .compat_ioctl = linehandle_ioctl_compat,
  290. #endif
  291. };
  292. static int linehandle_create(struct gpio_device *gdev, void __user *ip)
  293. {
  294. struct gpiohandle_request handlereq;
  295. struct linehandle_state *lh;
  296. struct file *file;
  297. int fd, i, ret;
  298. u32 lflags;
  299. if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
  300. return -EFAULT;
  301. if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
  302. return -EINVAL;
  303. lflags = handlereq.flags;
  304. ret = linehandle_validate_flags(lflags);
  305. if (ret)
  306. return ret;
  307. lh = kzalloc(sizeof(*lh), GFP_KERNEL);
  308. if (!lh)
  309. return -ENOMEM;
  310. lh->gdev = gdev;
  311. get_device(&gdev->dev);
  312. if (handlereq.consumer_label[0] != '\0') {
  313. /* label is only initialized if consumer_label is set */
  314. lh->label = kstrndup(handlereq.consumer_label,
  315. sizeof(handlereq.consumer_label) - 1,
  316. GFP_KERNEL);
  317. if (!lh->label) {
  318. ret = -ENOMEM;
  319. goto out_free_lh;
  320. }
  321. }
  322. lh->num_descs = handlereq.lines;
  323. /* Request each GPIO */
  324. for (i = 0; i < handlereq.lines; i++) {
  325. u32 offset = handlereq.lineoffsets[i];
  326. struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
  327. if (IS_ERR(desc)) {
  328. ret = PTR_ERR(desc);
  329. goto out_free_lh;
  330. }
  331. ret = gpiod_request_user(desc, lh->label);
  332. if (ret)
  333. goto out_free_lh;
  334. lh->descs[i] = desc;
  335. linehandle_flags_to_desc_flags(handlereq.flags, &desc->flags);
  336. ret = gpiod_set_transitory(desc, false);
  337. if (ret < 0)
  338. goto out_free_lh;
  339. /*
  340. * Lines have to be requested explicitly for input
  341. * or output, else the line will be treated "as is".
  342. */
  343. if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
  344. int val = !!handlereq.default_values[i];
  345. ret = gpiod_direction_output(desc, val);
  346. if (ret)
  347. goto out_free_lh;
  348. } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
  349. ret = gpiod_direction_input(desc);
  350. if (ret)
  351. goto out_free_lh;
  352. }
  353. blocking_notifier_call_chain(&desc->gdev->notifier,
  354. GPIO_V2_LINE_CHANGED_REQUESTED, desc);
  355. dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
  356. offset);
  357. }
  358. fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
  359. if (fd < 0) {
  360. ret = fd;
  361. goto out_free_lh;
  362. }
  363. file = anon_inode_getfile("gpio-linehandle",
  364. &linehandle_fileops,
  365. lh,
  366. O_RDONLY | O_CLOEXEC);
  367. if (IS_ERR(file)) {
  368. ret = PTR_ERR(file);
  369. goto out_put_unused_fd;
  370. }
  371. handlereq.fd = fd;
  372. if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
  373. /*
  374. * fput() will trigger the release() callback, so do not go onto
  375. * the regular error cleanup path here.
  376. */
  377. fput(file);
  378. put_unused_fd(fd);
  379. return -EFAULT;
  380. }
  381. fd_install(fd, file);
  382. dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
  383. lh->num_descs);
  384. return 0;
  385. out_put_unused_fd:
  386. put_unused_fd(fd);
  387. out_free_lh:
  388. linehandle_free(lh);
  389. return ret;
  390. }
  391. #endif /* CONFIG_GPIO_CDEV_V1 */
  392. /**
  393. * struct line - contains the state of a requested line
  394. * @desc: the GPIO descriptor for this line.
  395. * @req: the corresponding line request
  396. * @irq: the interrupt triggered in response to events on this GPIO
  397. * @eflags: the edge flags, GPIO_V2_LINE_FLAG_EDGE_RISING and/or
  398. * GPIO_V2_LINE_FLAG_EDGE_FALLING, indicating the edge detection applied
  399. * @timestamp_ns: cache for the timestamp storing it between hardirq and
  400. * IRQ thread, used to bring the timestamp close to the actual event
  401. * @req_seqno: the seqno for the current edge event in the sequence of
  402. * events for the corresponding line request. This is drawn from the @req.
  403. * @line_seqno: the seqno for the current edge event in the sequence of
  404. * events for this line.
  405. * @work: the worker that implements software debouncing
  406. * @sw_debounced: flag indicating if the software debouncer is active
  407. * @level: the current debounced physical level of the line
  408. * @hdesc: the Hardware Timestamp Engine (HTE) descriptor
  409. * @raw_level: the line level at the time of event
  410. * @total_discard_seq: the running counter of the discarded events
  411. * @last_seqno: the last sequence number before debounce period expires
  412. */
  413. struct line {
  414. struct gpio_desc *desc;
  415. /*
  416. * -- edge detector specific fields --
  417. */
  418. struct linereq *req;
  419. unsigned int irq;
  420. /*
  421. * The flags for the active edge detector configuration.
  422. *
  423. * edflags is set by linereq_create(), linereq_free(), and
  424. * linereq_set_config_unlocked(), which are themselves mutually
  425. * exclusive, and is accessed by edge_irq_thread(),
  426. * process_hw_ts_thread() and debounce_work_func(),
  427. * which can all live with a slightly stale value.
  428. */
  429. u64 edflags;
  430. /*
  431. * timestamp_ns and req_seqno are accessed only by
  432. * edge_irq_handler() and edge_irq_thread(), which are themselves
  433. * mutually exclusive, so no additional protection is necessary.
  434. */
  435. u64 timestamp_ns;
  436. u32 req_seqno;
  437. /*
  438. * line_seqno is accessed by either edge_irq_thread() or
  439. * debounce_work_func(), which are themselves mutually exclusive,
  440. * so no additional protection is necessary.
  441. */
  442. u32 line_seqno;
  443. /*
  444. * -- debouncer specific fields --
  445. */
  446. struct delayed_work work;
  447. /*
  448. * sw_debounce is accessed by linereq_set_config(), which is the
  449. * only setter, and linereq_get_values(), which can live with a
  450. * slightly stale value.
  451. */
  452. unsigned int sw_debounced;
  453. /*
  454. * level is accessed by debounce_work_func(), which is the only
  455. * setter, and linereq_get_values() which can live with a slightly
  456. * stale value.
  457. */
  458. unsigned int level;
  459. #ifdef CONFIG_HTE
  460. struct hte_ts_desc hdesc;
  461. /*
  462. * HTE provider sets line level at the time of event. The valid
  463. * value is 0 or 1 and negative value for an error.
  464. */
  465. int raw_level;
  466. /*
  467. * when sw_debounce is set on HTE enabled line, this is running
  468. * counter of the discarded events.
  469. */
  470. u32 total_discard_seq;
  471. /*
  472. * when sw_debounce is set on HTE enabled line, this variable records
  473. * last sequence number before debounce period expires.
  474. */
  475. u32 last_seqno;
  476. #endif /* CONFIG_HTE */
  477. };
  478. /**
  479. * struct linereq - contains the state of a userspace line request
  480. * @gdev: the GPIO device the line request pertains to
  481. * @label: consumer label used to tag GPIO descriptors
  482. * @num_lines: the number of lines in the lines array
  483. * @wait: wait queue that handles blocking reads of events
  484. * @event_buffer_size: the number of elements allocated in @events
  485. * @events: KFIFO for the GPIO events
  486. * @seqno: the sequence number for edge events generated on all lines in
  487. * this line request. Note that this is not used when @num_lines is 1, as
  488. * the line_seqno is then the same and is cheaper to calculate.
  489. * @config_mutex: mutex for serializing ioctl() calls to ensure consistency
  490. * of configuration, particularly multi-step accesses to desc flags.
  491. * @lines: the lines held by this line request, with @num_lines elements.
  492. */
  493. struct linereq {
  494. struct gpio_device *gdev;
  495. const char *label;
  496. u32 num_lines;
  497. wait_queue_head_t wait;
  498. u32 event_buffer_size;
  499. DECLARE_KFIFO_PTR(events, struct gpio_v2_line_event);
  500. atomic_t seqno;
  501. struct mutex config_mutex;
  502. struct line lines[];
  503. };
  504. #define GPIO_V2_LINE_BIAS_FLAGS \
  505. (GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \
  506. GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN | \
  507. GPIO_V2_LINE_FLAG_BIAS_DISABLED)
  508. #define GPIO_V2_LINE_DIRECTION_FLAGS \
  509. (GPIO_V2_LINE_FLAG_INPUT | \
  510. GPIO_V2_LINE_FLAG_OUTPUT)
  511. #define GPIO_V2_LINE_DRIVE_FLAGS \
  512. (GPIO_V2_LINE_FLAG_OPEN_DRAIN | \
  513. GPIO_V2_LINE_FLAG_OPEN_SOURCE)
  514. #define GPIO_V2_LINE_EDGE_FLAGS \
  515. (GPIO_V2_LINE_FLAG_EDGE_RISING | \
  516. GPIO_V2_LINE_FLAG_EDGE_FALLING)
  517. #define GPIO_V2_LINE_FLAG_EDGE_BOTH GPIO_V2_LINE_EDGE_FLAGS
  518. #define GPIO_V2_LINE_VALID_FLAGS \
  519. (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \
  520. GPIO_V2_LINE_DIRECTION_FLAGS | \
  521. GPIO_V2_LINE_DRIVE_FLAGS | \
  522. GPIO_V2_LINE_EDGE_FLAGS | \
  523. GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME | \
  524. GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \
  525. GPIO_V2_LINE_BIAS_FLAGS)
  526. /* subset of flags relevant for edge detector configuration */
  527. #define GPIO_V2_LINE_EDGE_DETECTOR_FLAGS \
  528. (GPIO_V2_LINE_FLAG_ACTIVE_LOW | \
  529. GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE | \
  530. GPIO_V2_LINE_EDGE_FLAGS)
  531. static void linereq_put_event(struct linereq *lr,
  532. struct gpio_v2_line_event *le)
  533. {
  534. bool overflow = false;
  535. spin_lock(&lr->wait.lock);
  536. if (kfifo_is_full(&lr->events)) {
  537. overflow = true;
  538. kfifo_skip(&lr->events);
  539. }
  540. kfifo_in(&lr->events, le, 1);
  541. spin_unlock(&lr->wait.lock);
  542. if (!overflow)
  543. wake_up_poll(&lr->wait, EPOLLIN);
  544. else
  545. pr_debug_ratelimited("event FIFO is full - event dropped\n");
  546. }
  547. static u64 line_event_timestamp(struct line *line)
  548. {
  549. if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &line->desc->flags))
  550. return ktime_get_real_ns();
  551. else if (IS_ENABLED(CONFIG_HTE) &&
  552. test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags))
  553. return line->timestamp_ns;
  554. return ktime_get_ns();
  555. }
  556. static u32 line_event_id(int level)
  557. {
  558. return level ? GPIO_V2_LINE_EVENT_RISING_EDGE :
  559. GPIO_V2_LINE_EVENT_FALLING_EDGE;
  560. }
  561. #ifdef CONFIG_HTE
  562. static enum hte_return process_hw_ts_thread(void *p)
  563. {
  564. struct line *line;
  565. struct linereq *lr;
  566. struct gpio_v2_line_event le;
  567. u64 edflags;
  568. int level;
  569. if (!p)
  570. return HTE_CB_HANDLED;
  571. line = p;
  572. lr = line->req;
  573. memset(&le, 0, sizeof(le));
  574. le.timestamp_ns = line->timestamp_ns;
  575. edflags = READ_ONCE(line->edflags);
  576. switch (edflags & GPIO_V2_LINE_EDGE_FLAGS) {
  577. case GPIO_V2_LINE_FLAG_EDGE_BOTH:
  578. level = (line->raw_level >= 0) ?
  579. line->raw_level :
  580. gpiod_get_raw_value_cansleep(line->desc);
  581. if (edflags & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
  582. level = !level;
  583. le.id = line_event_id(level);
  584. break;
  585. case GPIO_V2_LINE_FLAG_EDGE_RISING:
  586. le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
  587. break;
  588. case GPIO_V2_LINE_FLAG_EDGE_FALLING:
  589. le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
  590. break;
  591. default:
  592. return HTE_CB_HANDLED;
  593. }
  594. le.line_seqno = line->line_seqno;
  595. le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
  596. le.offset = gpio_chip_hwgpio(line->desc);
  597. linereq_put_event(lr, &le);
  598. return HTE_CB_HANDLED;
  599. }
  600. static enum hte_return process_hw_ts(struct hte_ts_data *ts, void *p)
  601. {
  602. struct line *line;
  603. struct linereq *lr;
  604. int diff_seqno = 0;
  605. if (!ts || !p)
  606. return HTE_CB_HANDLED;
  607. line = p;
  608. line->timestamp_ns = ts->tsc;
  609. line->raw_level = ts->raw_level;
  610. lr = line->req;
  611. if (READ_ONCE(line->sw_debounced)) {
  612. line->total_discard_seq++;
  613. line->last_seqno = ts->seq;
  614. mod_delayed_work(system_wq, &line->work,
  615. usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us)));
  616. } else {
  617. if (unlikely(ts->seq < line->line_seqno))
  618. return HTE_CB_HANDLED;
  619. diff_seqno = ts->seq - line->line_seqno;
  620. line->line_seqno = ts->seq;
  621. if (lr->num_lines != 1)
  622. line->req_seqno = atomic_add_return(diff_seqno,
  623. &lr->seqno);
  624. return HTE_RUN_SECOND_CB;
  625. }
  626. return HTE_CB_HANDLED;
  627. }
  628. static int hte_edge_setup(struct line *line, u64 eflags)
  629. {
  630. int ret;
  631. unsigned long flags = 0;
  632. struct hte_ts_desc *hdesc = &line->hdesc;
  633. if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
  634. flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
  635. HTE_FALLING_EDGE_TS :
  636. HTE_RISING_EDGE_TS;
  637. if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
  638. flags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
  639. HTE_RISING_EDGE_TS :
  640. HTE_FALLING_EDGE_TS;
  641. line->total_discard_seq = 0;
  642. hte_init_line_attr(hdesc, desc_to_gpio(line->desc), flags, NULL,
  643. line->desc);
  644. ret = hte_ts_get(NULL, hdesc, 0);
  645. if (ret)
  646. return ret;
  647. return hte_request_ts_ns(hdesc, process_hw_ts, process_hw_ts_thread,
  648. line);
  649. }
  650. #else
  651. static int hte_edge_setup(struct line *line, u64 eflags)
  652. {
  653. return 0;
  654. }
  655. #endif /* CONFIG_HTE */
  656. static irqreturn_t edge_irq_thread(int irq, void *p)
  657. {
  658. struct line *line = p;
  659. struct linereq *lr = line->req;
  660. struct gpio_v2_line_event le;
  661. /* Do not leak kernel stack to userspace */
  662. memset(&le, 0, sizeof(le));
  663. if (line->timestamp_ns) {
  664. le.timestamp_ns = line->timestamp_ns;
  665. } else {
  666. /*
  667. * We may be running from a nested threaded interrupt in
  668. * which case we didn't get the timestamp from
  669. * edge_irq_handler().
  670. */
  671. le.timestamp_ns = line_event_timestamp(line);
  672. if (lr->num_lines != 1)
  673. line->req_seqno = atomic_inc_return(&lr->seqno);
  674. }
  675. line->timestamp_ns = 0;
  676. switch (READ_ONCE(line->edflags) & GPIO_V2_LINE_EDGE_FLAGS) {
  677. case GPIO_V2_LINE_FLAG_EDGE_BOTH:
  678. le.id = line_event_id(gpiod_get_value_cansleep(line->desc));
  679. break;
  680. case GPIO_V2_LINE_FLAG_EDGE_RISING:
  681. le.id = GPIO_V2_LINE_EVENT_RISING_EDGE;
  682. break;
  683. case GPIO_V2_LINE_FLAG_EDGE_FALLING:
  684. le.id = GPIO_V2_LINE_EVENT_FALLING_EDGE;
  685. break;
  686. default:
  687. return IRQ_NONE;
  688. }
  689. line->line_seqno++;
  690. le.line_seqno = line->line_seqno;
  691. le.seqno = (lr->num_lines == 1) ? le.line_seqno : line->req_seqno;
  692. le.offset = gpio_chip_hwgpio(line->desc);
  693. linereq_put_event(lr, &le);
  694. return IRQ_HANDLED;
  695. }
  696. static irqreturn_t edge_irq_handler(int irq, void *p)
  697. {
  698. struct line *line = p;
  699. struct linereq *lr = line->req;
  700. /*
  701. * Just store the timestamp in hardirq context so we get it as
  702. * close in time as possible to the actual event.
  703. */
  704. line->timestamp_ns = line_event_timestamp(line);
  705. if (lr->num_lines != 1)
  706. line->req_seqno = atomic_inc_return(&lr->seqno);
  707. return IRQ_WAKE_THREAD;
  708. }
  709. /*
  710. * returns the current debounced logical value.
  711. */
  712. static bool debounced_value(struct line *line)
  713. {
  714. bool value;
  715. /*
  716. * minor race - debouncer may be stopped here, so edge_detector_stop()
  717. * must leave the value unchanged so the following will read the level
  718. * from when the debouncer was last running.
  719. */
  720. value = READ_ONCE(line->level);
  721. if (test_bit(FLAG_ACTIVE_LOW, &line->desc->flags))
  722. value = !value;
  723. return value;
  724. }
  725. static irqreturn_t debounce_irq_handler(int irq, void *p)
  726. {
  727. struct line *line = p;
  728. mod_delayed_work(system_wq, &line->work,
  729. usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us)));
  730. return IRQ_HANDLED;
  731. }
  732. static void debounce_work_func(struct work_struct *work)
  733. {
  734. struct gpio_v2_line_event le;
  735. struct line *line = container_of(work, struct line, work.work);
  736. struct linereq *lr;
  737. u64 eflags, edflags = READ_ONCE(line->edflags);
  738. int level = -1;
  739. #ifdef CONFIG_HTE
  740. int diff_seqno;
  741. if (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)
  742. level = line->raw_level;
  743. #endif
  744. if (level < 0)
  745. level = gpiod_get_raw_value_cansleep(line->desc);
  746. if (level < 0) {
  747. pr_debug_ratelimited("debouncer failed to read line value\n");
  748. return;
  749. }
  750. if (READ_ONCE(line->level) == level)
  751. return;
  752. WRITE_ONCE(line->level, level);
  753. /* -- edge detection -- */
  754. eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS;
  755. if (!eflags)
  756. return;
  757. /* switch from physical level to logical - if they differ */
  758. if (edflags & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
  759. level = !level;
  760. /* ignore edges that are not being monitored */
  761. if (((eflags == GPIO_V2_LINE_FLAG_EDGE_RISING) && !level) ||
  762. ((eflags == GPIO_V2_LINE_FLAG_EDGE_FALLING) && level))
  763. return;
  764. /* Do not leak kernel stack to userspace */
  765. memset(&le, 0, sizeof(le));
  766. lr = line->req;
  767. le.timestamp_ns = line_event_timestamp(line);
  768. le.offset = gpio_chip_hwgpio(line->desc);
  769. #ifdef CONFIG_HTE
  770. if (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) {
  771. /* discard events except the last one */
  772. line->total_discard_seq -= 1;
  773. diff_seqno = line->last_seqno - line->total_discard_seq -
  774. line->line_seqno;
  775. line->line_seqno = line->last_seqno - line->total_discard_seq;
  776. le.line_seqno = line->line_seqno;
  777. le.seqno = (lr->num_lines == 1) ?
  778. le.line_seqno : atomic_add_return(diff_seqno, &lr->seqno);
  779. } else
  780. #endif /* CONFIG_HTE */
  781. {
  782. line->line_seqno++;
  783. le.line_seqno = line->line_seqno;
  784. le.seqno = (lr->num_lines == 1) ?
  785. le.line_seqno : atomic_inc_return(&lr->seqno);
  786. }
  787. le.id = line_event_id(level);
  788. linereq_put_event(lr, &le);
  789. }
  790. static int debounce_setup(struct line *line, unsigned int debounce_period_us)
  791. {
  792. unsigned long irqflags;
  793. int ret, level, irq;
  794. /* try hardware */
  795. ret = gpiod_set_debounce(line->desc, debounce_period_us);
  796. if (!ret) {
  797. WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
  798. return ret;
  799. }
  800. if (ret != -ENOTSUPP)
  801. return ret;
  802. if (debounce_period_us) {
  803. /* setup software debounce */
  804. level = gpiod_get_raw_value_cansleep(line->desc);
  805. if (level < 0)
  806. return level;
  807. if (!(IS_ENABLED(CONFIG_HTE) &&
  808. test_bit(FLAG_EVENT_CLOCK_HTE, &line->desc->flags))) {
  809. irq = gpiod_to_irq(line->desc);
  810. if (irq < 0)
  811. return -ENXIO;
  812. irqflags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING;
  813. ret = request_irq(irq, debounce_irq_handler, irqflags,
  814. line->req->label, line);
  815. if (ret)
  816. return ret;
  817. line->irq = irq;
  818. } else {
  819. ret = hte_edge_setup(line, GPIO_V2_LINE_FLAG_EDGE_BOTH);
  820. if (ret)
  821. return ret;
  822. }
  823. WRITE_ONCE(line->level, level);
  824. WRITE_ONCE(line->sw_debounced, 1);
  825. }
  826. return 0;
  827. }
  828. static bool gpio_v2_line_config_debounced(struct gpio_v2_line_config *lc,
  829. unsigned int line_idx)
  830. {
  831. unsigned int i;
  832. u64 mask = BIT_ULL(line_idx);
  833. for (i = 0; i < lc->num_attrs; i++) {
  834. if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
  835. (lc->attrs[i].mask & mask))
  836. return true;
  837. }
  838. return false;
  839. }
  840. static u32 gpio_v2_line_config_debounce_period(struct gpio_v2_line_config *lc,
  841. unsigned int line_idx)
  842. {
  843. unsigned int i;
  844. u64 mask = BIT_ULL(line_idx);
  845. for (i = 0; i < lc->num_attrs; i++) {
  846. if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) &&
  847. (lc->attrs[i].mask & mask))
  848. return lc->attrs[i].attr.debounce_period_us;
  849. }
  850. return 0;
  851. }
  852. static void edge_detector_stop(struct line *line)
  853. {
  854. if (line->irq) {
  855. free_irq(line->irq, line);
  856. line->irq = 0;
  857. }
  858. #ifdef CONFIG_HTE
  859. if (READ_ONCE(line->edflags) & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE)
  860. hte_ts_put(&line->hdesc);
  861. #endif
  862. cancel_delayed_work_sync(&line->work);
  863. WRITE_ONCE(line->sw_debounced, 0);
  864. WRITE_ONCE(line->edflags, 0);
  865. if (line->desc)
  866. WRITE_ONCE(line->desc->debounce_period_us, 0);
  867. /* do not change line->level - see comment in debounced_value() */
  868. }
  869. static int edge_detector_setup(struct line *line,
  870. struct gpio_v2_line_config *lc,
  871. unsigned int line_idx, u64 edflags)
  872. {
  873. u32 debounce_period_us;
  874. unsigned long irqflags = 0;
  875. u64 eflags;
  876. int irq, ret;
  877. eflags = edflags & GPIO_V2_LINE_EDGE_FLAGS;
  878. if (eflags && !kfifo_initialized(&line->req->events)) {
  879. ret = kfifo_alloc(&line->req->events,
  880. line->req->event_buffer_size, GFP_KERNEL);
  881. if (ret)
  882. return ret;
  883. }
  884. if (gpio_v2_line_config_debounced(lc, line_idx)) {
  885. debounce_period_us = gpio_v2_line_config_debounce_period(lc, line_idx);
  886. ret = debounce_setup(line, debounce_period_us);
  887. if (ret)
  888. return ret;
  889. WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
  890. }
  891. /* detection disabled or sw debouncer will provide edge detection */
  892. if (!eflags || READ_ONCE(line->sw_debounced))
  893. return 0;
  894. if (IS_ENABLED(CONFIG_HTE) &&
  895. (edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE))
  896. return hte_edge_setup(line, edflags);
  897. irq = gpiod_to_irq(line->desc);
  898. if (irq < 0)
  899. return -ENXIO;
  900. if (eflags & GPIO_V2_LINE_FLAG_EDGE_RISING)
  901. irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
  902. IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
  903. if (eflags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
  904. irqflags |= test_bit(FLAG_ACTIVE_LOW, &line->desc->flags) ?
  905. IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
  906. irqflags |= IRQF_ONESHOT;
  907. /* Request a thread to read the events */
  908. ret = request_threaded_irq(irq, edge_irq_handler, edge_irq_thread,
  909. irqflags, line->req->label, line);
  910. if (ret)
  911. return ret;
  912. line->irq = irq;
  913. return 0;
  914. }
  915. static int edge_detector_update(struct line *line,
  916. struct gpio_v2_line_config *lc,
  917. unsigned int line_idx, u64 edflags)
  918. {
  919. u64 active_edflags = READ_ONCE(line->edflags);
  920. unsigned int debounce_period_us =
  921. gpio_v2_line_config_debounce_period(lc, line_idx);
  922. if ((active_edflags == edflags) &&
  923. (READ_ONCE(line->desc->debounce_period_us) == debounce_period_us))
  924. return 0;
  925. /* sw debounced and still will be...*/
  926. if (debounce_period_us && READ_ONCE(line->sw_debounced)) {
  927. WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us);
  928. return 0;
  929. }
  930. /* reconfiguring edge detection or sw debounce being disabled */
  931. if ((line->irq && !READ_ONCE(line->sw_debounced)) ||
  932. (active_edflags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE) ||
  933. (!debounce_period_us && READ_ONCE(line->sw_debounced)))
  934. edge_detector_stop(line);
  935. return edge_detector_setup(line, lc, line_idx, edflags);
  936. }
  937. static u64 gpio_v2_line_config_flags(struct gpio_v2_line_config *lc,
  938. unsigned int line_idx)
  939. {
  940. unsigned int i;
  941. u64 mask = BIT_ULL(line_idx);
  942. for (i = 0; i < lc->num_attrs; i++) {
  943. if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_FLAGS) &&
  944. (lc->attrs[i].mask & mask))
  945. return lc->attrs[i].attr.flags;
  946. }
  947. return lc->flags;
  948. }
  949. static int gpio_v2_line_config_output_value(struct gpio_v2_line_config *lc,
  950. unsigned int line_idx)
  951. {
  952. unsigned int i;
  953. u64 mask = BIT_ULL(line_idx);
  954. for (i = 0; i < lc->num_attrs; i++) {
  955. if ((lc->attrs[i].attr.id == GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES) &&
  956. (lc->attrs[i].mask & mask))
  957. return !!(lc->attrs[i].attr.values & mask);
  958. }
  959. return 0;
  960. }
  961. static int gpio_v2_line_flags_validate(u64 flags)
  962. {
  963. /* Return an error if an unknown flag is set */
  964. if (flags & ~GPIO_V2_LINE_VALID_FLAGS)
  965. return -EINVAL;
  966. if (!IS_ENABLED(CONFIG_HTE) &&
  967. (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE))
  968. return -EOPNOTSUPP;
  969. /*
  970. * Do not allow both INPUT and OUTPUT flags to be set as they are
  971. * contradictory.
  972. */
  973. if ((flags & GPIO_V2_LINE_FLAG_INPUT) &&
  974. (flags & GPIO_V2_LINE_FLAG_OUTPUT))
  975. return -EINVAL;
  976. /* Only allow one event clock source */
  977. if (IS_ENABLED(CONFIG_HTE) &&
  978. (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME) &&
  979. (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE))
  980. return -EINVAL;
  981. /* Edge detection requires explicit input. */
  982. if ((flags & GPIO_V2_LINE_EDGE_FLAGS) &&
  983. !(flags & GPIO_V2_LINE_FLAG_INPUT))
  984. return -EINVAL;
  985. /*
  986. * Do not allow OPEN_SOURCE and OPEN_DRAIN flags in a single
  987. * request. If the hardware actually supports enabling both at the
  988. * same time the electrical result would be disastrous.
  989. */
  990. if ((flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN) &&
  991. (flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE))
  992. return -EINVAL;
  993. /* Drive requires explicit output direction. */
  994. if ((flags & GPIO_V2_LINE_DRIVE_FLAGS) &&
  995. !(flags & GPIO_V2_LINE_FLAG_OUTPUT))
  996. return -EINVAL;
  997. /* Bias requires explicit direction. */
  998. if ((flags & GPIO_V2_LINE_BIAS_FLAGS) &&
  999. !(flags & GPIO_V2_LINE_DIRECTION_FLAGS))
  1000. return -EINVAL;
  1001. /* Only one bias flag can be set. */
  1002. if (((flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED) &&
  1003. (flags & (GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN |
  1004. GPIO_V2_LINE_FLAG_BIAS_PULL_UP))) ||
  1005. ((flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) &&
  1006. (flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)))
  1007. return -EINVAL;
  1008. return 0;
  1009. }
  1010. static int gpio_v2_line_config_validate(struct gpio_v2_line_config *lc,
  1011. unsigned int num_lines)
  1012. {
  1013. unsigned int i;
  1014. u64 flags;
  1015. int ret;
  1016. if (lc->num_attrs > GPIO_V2_LINE_NUM_ATTRS_MAX)
  1017. return -EINVAL;
  1018. if (memchr_inv(lc->padding, 0, sizeof(lc->padding)))
  1019. return -EINVAL;
  1020. for (i = 0; i < num_lines; i++) {
  1021. flags = gpio_v2_line_config_flags(lc, i);
  1022. ret = gpio_v2_line_flags_validate(flags);
  1023. if (ret)
  1024. return ret;
  1025. /* debounce requires explicit input */
  1026. if (gpio_v2_line_config_debounced(lc, i) &&
  1027. !(flags & GPIO_V2_LINE_FLAG_INPUT))
  1028. return -EINVAL;
  1029. }
  1030. return 0;
  1031. }
  1032. static void gpio_v2_line_config_flags_to_desc_flags(u64 flags,
  1033. unsigned long *flagsp)
  1034. {
  1035. assign_bit(FLAG_ACTIVE_LOW, flagsp,
  1036. flags & GPIO_V2_LINE_FLAG_ACTIVE_LOW);
  1037. if (flags & GPIO_V2_LINE_FLAG_OUTPUT)
  1038. set_bit(FLAG_IS_OUT, flagsp);
  1039. else if (flags & GPIO_V2_LINE_FLAG_INPUT)
  1040. clear_bit(FLAG_IS_OUT, flagsp);
  1041. assign_bit(FLAG_EDGE_RISING, flagsp,
  1042. flags & GPIO_V2_LINE_FLAG_EDGE_RISING);
  1043. assign_bit(FLAG_EDGE_FALLING, flagsp,
  1044. flags & GPIO_V2_LINE_FLAG_EDGE_FALLING);
  1045. assign_bit(FLAG_OPEN_DRAIN, flagsp,
  1046. flags & GPIO_V2_LINE_FLAG_OPEN_DRAIN);
  1047. assign_bit(FLAG_OPEN_SOURCE, flagsp,
  1048. flags & GPIO_V2_LINE_FLAG_OPEN_SOURCE);
  1049. assign_bit(FLAG_PULL_UP, flagsp,
  1050. flags & GPIO_V2_LINE_FLAG_BIAS_PULL_UP);
  1051. assign_bit(FLAG_PULL_DOWN, flagsp,
  1052. flags & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN);
  1053. assign_bit(FLAG_BIAS_DISABLE, flagsp,
  1054. flags & GPIO_V2_LINE_FLAG_BIAS_DISABLED);
  1055. assign_bit(FLAG_EVENT_CLOCK_REALTIME, flagsp,
  1056. flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME);
  1057. assign_bit(FLAG_EVENT_CLOCK_HTE, flagsp,
  1058. flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE);
  1059. }
  1060. static long linereq_get_values(struct linereq *lr, void __user *ip)
  1061. {
  1062. struct gpio_v2_line_values lv;
  1063. DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
  1064. struct gpio_desc **descs;
  1065. unsigned int i, didx, num_get;
  1066. bool val;
  1067. int ret;
  1068. /* NOTE: It's ok to read values of output lines. */
  1069. if (copy_from_user(&lv, ip, sizeof(lv)))
  1070. return -EFAULT;
  1071. for (num_get = 0, i = 0; i < lr->num_lines; i++) {
  1072. if (lv.mask & BIT_ULL(i)) {
  1073. num_get++;
  1074. descs = &lr->lines[i].desc;
  1075. }
  1076. }
  1077. if (num_get == 0)
  1078. return -EINVAL;
  1079. if (num_get != 1) {
  1080. descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL);
  1081. if (!descs)
  1082. return -ENOMEM;
  1083. for (didx = 0, i = 0; i < lr->num_lines; i++) {
  1084. if (lv.mask & BIT_ULL(i)) {
  1085. descs[didx] = lr->lines[i].desc;
  1086. didx++;
  1087. }
  1088. }
  1089. }
  1090. ret = gpiod_get_array_value_complex(false, true, num_get,
  1091. descs, NULL, vals);
  1092. if (num_get != 1)
  1093. kfree(descs);
  1094. if (ret)
  1095. return ret;
  1096. lv.bits = 0;
  1097. for (didx = 0, i = 0; i < lr->num_lines; i++) {
  1098. if (lv.mask & BIT_ULL(i)) {
  1099. if (lr->lines[i].sw_debounced)
  1100. val = debounced_value(&lr->lines[i]);
  1101. else
  1102. val = test_bit(didx, vals);
  1103. if (val)
  1104. lv.bits |= BIT_ULL(i);
  1105. didx++;
  1106. }
  1107. }
  1108. if (copy_to_user(ip, &lv, sizeof(lv)))
  1109. return -EFAULT;
  1110. return 0;
  1111. }
  1112. static long linereq_set_values_unlocked(struct linereq *lr,
  1113. struct gpio_v2_line_values *lv)
  1114. {
  1115. DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX);
  1116. struct gpio_desc **descs;
  1117. unsigned int i, didx, num_set;
  1118. int ret;
  1119. bitmap_zero(vals, GPIO_V2_LINES_MAX);
  1120. for (num_set = 0, i = 0; i < lr->num_lines; i++) {
  1121. if (lv->mask & BIT_ULL(i)) {
  1122. if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags))
  1123. return -EPERM;
  1124. if (lv->bits & BIT_ULL(i))
  1125. __set_bit(num_set, vals);
  1126. num_set++;
  1127. descs = &lr->lines[i].desc;
  1128. }
  1129. }
  1130. if (num_set == 0)
  1131. return -EINVAL;
  1132. if (num_set != 1) {
  1133. /* build compacted desc array and values */
  1134. descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL);
  1135. if (!descs)
  1136. return -ENOMEM;
  1137. for (didx = 0, i = 0; i < lr->num_lines; i++) {
  1138. if (lv->mask & BIT_ULL(i)) {
  1139. descs[didx] = lr->lines[i].desc;
  1140. didx++;
  1141. }
  1142. }
  1143. }
  1144. ret = gpiod_set_array_value_complex(false, true, num_set,
  1145. descs, NULL, vals);
  1146. if (num_set != 1)
  1147. kfree(descs);
  1148. return ret;
  1149. }
  1150. static long linereq_set_values(struct linereq *lr, void __user *ip)
  1151. {
  1152. struct gpio_v2_line_values lv;
  1153. int ret;
  1154. if (copy_from_user(&lv, ip, sizeof(lv)))
  1155. return -EFAULT;
  1156. mutex_lock(&lr->config_mutex);
  1157. ret = linereq_set_values_unlocked(lr, &lv);
  1158. mutex_unlock(&lr->config_mutex);
  1159. return ret;
  1160. }
  1161. static long linereq_set_config_unlocked(struct linereq *lr,
  1162. struct gpio_v2_line_config *lc)
  1163. {
  1164. struct gpio_desc *desc;
  1165. struct line *line;
  1166. unsigned int i;
  1167. u64 flags, edflags;
  1168. int ret;
  1169. for (i = 0; i < lr->num_lines; i++) {
  1170. line = &lr->lines[i];
  1171. desc = lr->lines[i].desc;
  1172. flags = gpio_v2_line_config_flags(lc, i);
  1173. gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
  1174. edflags = flags & GPIO_V2_LINE_EDGE_DETECTOR_FLAGS;
  1175. /*
  1176. * Lines have to be requested explicitly for input
  1177. * or output, else the line will be treated "as is".
  1178. */
  1179. if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
  1180. int val = gpio_v2_line_config_output_value(lc, i);
  1181. edge_detector_stop(line);
  1182. ret = gpiod_direction_output(desc, val);
  1183. if (ret)
  1184. return ret;
  1185. } else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
  1186. ret = gpiod_direction_input(desc);
  1187. if (ret)
  1188. return ret;
  1189. ret = edge_detector_update(line, lc, i, edflags);
  1190. if (ret)
  1191. return ret;
  1192. }
  1193. WRITE_ONCE(line->edflags, edflags);
  1194. blocking_notifier_call_chain(&desc->gdev->notifier,
  1195. GPIO_V2_LINE_CHANGED_CONFIG,
  1196. desc);
  1197. }
  1198. return 0;
  1199. }
  1200. static long linereq_set_config(struct linereq *lr, void __user *ip)
  1201. {
  1202. struct gpio_v2_line_config lc;
  1203. int ret;
  1204. if (copy_from_user(&lc, ip, sizeof(lc)))
  1205. return -EFAULT;
  1206. ret = gpio_v2_line_config_validate(&lc, lr->num_lines);
  1207. if (ret)
  1208. return ret;
  1209. mutex_lock(&lr->config_mutex);
  1210. ret = linereq_set_config_unlocked(lr, &lc);
  1211. mutex_unlock(&lr->config_mutex);
  1212. return ret;
  1213. }
  1214. static long linereq_ioctl_unlocked(struct file *file, unsigned int cmd,
  1215. unsigned long arg)
  1216. {
  1217. struct linereq *lr = file->private_data;
  1218. void __user *ip = (void __user *)arg;
  1219. if (!lr->gdev->chip)
  1220. return -ENODEV;
  1221. switch (cmd) {
  1222. case GPIO_V2_LINE_GET_VALUES_IOCTL:
  1223. return linereq_get_values(lr, ip);
  1224. case GPIO_V2_LINE_SET_VALUES_IOCTL:
  1225. return linereq_set_values(lr, ip);
  1226. case GPIO_V2_LINE_SET_CONFIG_IOCTL:
  1227. return linereq_set_config(lr, ip);
  1228. default:
  1229. return -EINVAL;
  1230. }
  1231. }
  1232. static long linereq_ioctl(struct file *file, unsigned int cmd,
  1233. unsigned long arg)
  1234. {
  1235. struct linereq *lr = file->private_data;
  1236. return call_ioctl_locked(file, cmd, arg, lr->gdev,
  1237. linereq_ioctl_unlocked);
  1238. }
  1239. #ifdef CONFIG_COMPAT
  1240. static long linereq_ioctl_compat(struct file *file, unsigned int cmd,
  1241. unsigned long arg)
  1242. {
  1243. return linereq_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  1244. }
  1245. #endif
  1246. static __poll_t linereq_poll_unlocked(struct file *file,
  1247. struct poll_table_struct *wait)
  1248. {
  1249. struct linereq *lr = file->private_data;
  1250. __poll_t events = 0;
  1251. if (!lr->gdev->chip)
  1252. return EPOLLHUP | EPOLLERR;
  1253. poll_wait(file, &lr->wait, wait);
  1254. if (!kfifo_is_empty_spinlocked_noirqsave(&lr->events,
  1255. &lr->wait.lock))
  1256. events = EPOLLIN | EPOLLRDNORM;
  1257. return events;
  1258. }
  1259. static __poll_t linereq_poll(struct file *file,
  1260. struct poll_table_struct *wait)
  1261. {
  1262. struct linereq *lr = file->private_data;
  1263. return call_poll_locked(file, wait, lr->gdev, linereq_poll_unlocked);
  1264. }
  1265. static ssize_t linereq_read_unlocked(struct file *file, char __user *buf,
  1266. size_t count, loff_t *f_ps)
  1267. {
  1268. struct linereq *lr = file->private_data;
  1269. struct gpio_v2_line_event le;
  1270. ssize_t bytes_read = 0;
  1271. int ret;
  1272. if (!lr->gdev->chip)
  1273. return -ENODEV;
  1274. if (count < sizeof(le))
  1275. return -EINVAL;
  1276. do {
  1277. spin_lock(&lr->wait.lock);
  1278. if (kfifo_is_empty(&lr->events)) {
  1279. if (bytes_read) {
  1280. spin_unlock(&lr->wait.lock);
  1281. return bytes_read;
  1282. }
  1283. if (file->f_flags & O_NONBLOCK) {
  1284. spin_unlock(&lr->wait.lock);
  1285. return -EAGAIN;
  1286. }
  1287. ret = wait_event_interruptible_locked(lr->wait,
  1288. !kfifo_is_empty(&lr->events));
  1289. if (ret) {
  1290. spin_unlock(&lr->wait.lock);
  1291. return ret;
  1292. }
  1293. }
  1294. ret = kfifo_out(&lr->events, &le, 1);
  1295. spin_unlock(&lr->wait.lock);
  1296. if (ret != 1) {
  1297. /*
  1298. * This should never happen - we were holding the
  1299. * lock from the moment we learned the fifo is no
  1300. * longer empty until now.
  1301. */
  1302. ret = -EIO;
  1303. break;
  1304. }
  1305. if (copy_to_user(buf + bytes_read, &le, sizeof(le)))
  1306. return -EFAULT;
  1307. bytes_read += sizeof(le);
  1308. } while (count >= bytes_read + sizeof(le));
  1309. return bytes_read;
  1310. }
  1311. static ssize_t linereq_read(struct file *file, char __user *buf,
  1312. size_t count, loff_t *f_ps)
  1313. {
  1314. struct linereq *lr = file->private_data;
  1315. return call_read_locked(file, buf, count, f_ps, lr->gdev,
  1316. linereq_read_unlocked);
  1317. }
  1318. static void linereq_free(struct linereq *lr)
  1319. {
  1320. unsigned int i;
  1321. for (i = 0; i < lr->num_lines; i++) {
  1322. if (lr->lines[i].desc) {
  1323. edge_detector_stop(&lr->lines[i]);
  1324. gpiod_free(lr->lines[i].desc);
  1325. }
  1326. }
  1327. kfifo_free(&lr->events);
  1328. kfree(lr->label);
  1329. put_device(&lr->gdev->dev);
  1330. kfree(lr);
  1331. }
  1332. static int linereq_release(struct inode *inode, struct file *file)
  1333. {
  1334. struct linereq *lr = file->private_data;
  1335. linereq_free(lr);
  1336. return 0;
  1337. }
  1338. #ifdef CONFIG_PROC_FS
  1339. static void linereq_show_fdinfo(struct seq_file *out, struct file *file)
  1340. {
  1341. struct linereq *lr = file->private_data;
  1342. struct device *dev = &lr->gdev->dev;
  1343. u16 i;
  1344. seq_printf(out, "gpio-chip:\t%s\n", dev_name(dev));
  1345. for (i = 0; i < lr->num_lines; i++)
  1346. seq_printf(out, "gpio-line:\t%d\n",
  1347. gpio_chip_hwgpio(lr->lines[i].desc));
  1348. }
  1349. #endif
  1350. static const struct file_operations line_fileops = {
  1351. .release = linereq_release,
  1352. .read = linereq_read,
  1353. .poll = linereq_poll,
  1354. .owner = THIS_MODULE,
  1355. .llseek = noop_llseek,
  1356. .unlocked_ioctl = linereq_ioctl,
  1357. #ifdef CONFIG_COMPAT
  1358. .compat_ioctl = linereq_ioctl_compat,
  1359. #endif
  1360. #ifdef CONFIG_PROC_FS
  1361. .show_fdinfo = linereq_show_fdinfo,
  1362. #endif
  1363. };
  1364. static int linereq_create(struct gpio_device *gdev, void __user *ip)
  1365. {
  1366. struct gpio_v2_line_request ulr;
  1367. struct gpio_v2_line_config *lc;
  1368. struct linereq *lr;
  1369. struct file *file;
  1370. u64 flags, edflags;
  1371. unsigned int i;
  1372. int fd, ret;
  1373. if (copy_from_user(&ulr, ip, sizeof(ulr)))
  1374. return -EFAULT;
  1375. if ((ulr.num_lines == 0) || (ulr.num_lines > GPIO_V2_LINES_MAX))
  1376. return -EINVAL;
  1377. if (memchr_inv(ulr.padding, 0, sizeof(ulr.padding)))
  1378. return -EINVAL;
  1379. lc = &ulr.config;
  1380. ret = gpio_v2_line_config_validate(lc, ulr.num_lines);
  1381. if (ret)
  1382. return ret;
  1383. lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL);
  1384. if (!lr)
  1385. return -ENOMEM;
  1386. lr->gdev = gdev;
  1387. get_device(&gdev->dev);
  1388. for (i = 0; i < ulr.num_lines; i++) {
  1389. lr->lines[i].req = lr;
  1390. WRITE_ONCE(lr->lines[i].sw_debounced, 0);
  1391. INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func);
  1392. }
  1393. if (ulr.consumer[0] != '\0') {
  1394. /* label is only initialized if consumer is set */
  1395. lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1,
  1396. GFP_KERNEL);
  1397. if (!lr->label) {
  1398. ret = -ENOMEM;
  1399. goto out_free_linereq;
  1400. }
  1401. }
  1402. mutex_init(&lr->config_mutex);
  1403. init_waitqueue_head(&lr->wait);
  1404. lr->event_buffer_size = ulr.event_buffer_size;
  1405. if (lr->event_buffer_size == 0)
  1406. lr->event_buffer_size = ulr.num_lines * 16;
  1407. else if (lr->event_buffer_size > GPIO_V2_LINES_MAX * 16)
  1408. lr->event_buffer_size = GPIO_V2_LINES_MAX * 16;
  1409. atomic_set(&lr->seqno, 0);
  1410. lr->num_lines = ulr.num_lines;
  1411. /* Request each GPIO */
  1412. for (i = 0; i < ulr.num_lines; i++) {
  1413. u32 offset = ulr.offsets[i];
  1414. struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
  1415. if (IS_ERR(desc)) {
  1416. ret = PTR_ERR(desc);
  1417. goto out_free_linereq;
  1418. }
  1419. ret = gpiod_request_user(desc, lr->label);
  1420. if (ret)
  1421. goto out_free_linereq;
  1422. lr->lines[i].desc = desc;
  1423. flags = gpio_v2_line_config_flags(lc, i);
  1424. gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags);
  1425. ret = gpiod_set_transitory(desc, false);
  1426. if (ret < 0)
  1427. goto out_free_linereq;
  1428. edflags = flags & GPIO_V2_LINE_EDGE_DETECTOR_FLAGS;
  1429. /*
  1430. * Lines have to be requested explicitly for input
  1431. * or output, else the line will be treated "as is".
  1432. */
  1433. if (flags & GPIO_V2_LINE_FLAG_OUTPUT) {
  1434. int val = gpio_v2_line_config_output_value(lc, i);
  1435. ret = gpiod_direction_output(desc, val);
  1436. if (ret)
  1437. goto out_free_linereq;
  1438. } else if (flags & GPIO_V2_LINE_FLAG_INPUT) {
  1439. ret = gpiod_direction_input(desc);
  1440. if (ret)
  1441. goto out_free_linereq;
  1442. ret = edge_detector_setup(&lr->lines[i], lc, i,
  1443. edflags);
  1444. if (ret)
  1445. goto out_free_linereq;
  1446. }
  1447. lr->lines[i].edflags = edflags;
  1448. blocking_notifier_call_chain(&desc->gdev->notifier,
  1449. GPIO_V2_LINE_CHANGED_REQUESTED, desc);
  1450. dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
  1451. offset);
  1452. }
  1453. fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
  1454. if (fd < 0) {
  1455. ret = fd;
  1456. goto out_free_linereq;
  1457. }
  1458. file = anon_inode_getfile("gpio-line", &line_fileops, lr,
  1459. O_RDONLY | O_CLOEXEC);
  1460. if (IS_ERR(file)) {
  1461. ret = PTR_ERR(file);
  1462. goto out_put_unused_fd;
  1463. }
  1464. ulr.fd = fd;
  1465. if (copy_to_user(ip, &ulr, sizeof(ulr))) {
  1466. /*
  1467. * fput() will trigger the release() callback, so do not go onto
  1468. * the regular error cleanup path here.
  1469. */
  1470. fput(file);
  1471. put_unused_fd(fd);
  1472. return -EFAULT;
  1473. }
  1474. fd_install(fd, file);
  1475. dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
  1476. lr->num_lines);
  1477. return 0;
  1478. out_put_unused_fd:
  1479. put_unused_fd(fd);
  1480. out_free_linereq:
  1481. linereq_free(lr);
  1482. return ret;
  1483. }
  1484. #ifdef CONFIG_GPIO_CDEV_V1
  1485. /*
  1486. * GPIO line event management
  1487. */
  1488. /**
  1489. * struct lineevent_state - contains the state of a userspace event
  1490. * @gdev: the GPIO device the event pertains to
  1491. * @label: consumer label used to tag descriptors
  1492. * @desc: the GPIO descriptor held by this event
  1493. * @eflags: the event flags this line was requested with
  1494. * @irq: the interrupt that trigger in response to events on this GPIO
  1495. * @wait: wait queue that handles blocking reads of events
  1496. * @events: KFIFO for the GPIO events
  1497. * @timestamp: cache for the timestamp storing it between hardirq
  1498. * and IRQ thread, used to bring the timestamp close to the actual
  1499. * event
  1500. */
  1501. struct lineevent_state {
  1502. struct gpio_device *gdev;
  1503. const char *label;
  1504. struct gpio_desc *desc;
  1505. u32 eflags;
  1506. int irq;
  1507. wait_queue_head_t wait;
  1508. DECLARE_KFIFO(events, struct gpioevent_data, 16);
  1509. u64 timestamp;
  1510. };
  1511. #define GPIOEVENT_REQUEST_VALID_FLAGS \
  1512. (GPIOEVENT_REQUEST_RISING_EDGE | \
  1513. GPIOEVENT_REQUEST_FALLING_EDGE)
  1514. static __poll_t lineevent_poll_unlocked(struct file *file,
  1515. struct poll_table_struct *wait)
  1516. {
  1517. struct lineevent_state *le = file->private_data;
  1518. __poll_t events = 0;
  1519. if (!le->gdev->chip)
  1520. return EPOLLHUP | EPOLLERR;
  1521. poll_wait(file, &le->wait, wait);
  1522. if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock))
  1523. events = EPOLLIN | EPOLLRDNORM;
  1524. return events;
  1525. }
  1526. static __poll_t lineevent_poll(struct file *file,
  1527. struct poll_table_struct *wait)
  1528. {
  1529. struct lineevent_state *le = file->private_data;
  1530. return call_poll_locked(file, wait, le->gdev, lineevent_poll_unlocked);
  1531. }
  1532. struct compat_gpioeevent_data {
  1533. compat_u64 timestamp;
  1534. u32 id;
  1535. };
  1536. static ssize_t lineevent_read_unlocked(struct file *file, char __user *buf,
  1537. size_t count, loff_t *f_ps)
  1538. {
  1539. struct lineevent_state *le = file->private_data;
  1540. struct gpioevent_data ge;
  1541. ssize_t bytes_read = 0;
  1542. ssize_t ge_size;
  1543. int ret;
  1544. if (!le->gdev->chip)
  1545. return -ENODEV;
  1546. /*
  1547. * When compatible system call is being used the struct gpioevent_data,
  1548. * in case of at least ia32, has different size due to the alignment
  1549. * differences. Because we have first member 64 bits followed by one of
  1550. * 32 bits there is no gap between them. The only difference is the
  1551. * padding at the end of the data structure. Hence, we calculate the
  1552. * actual sizeof() and pass this as an argument to copy_to_user() to
  1553. * drop unneeded bytes from the output.
  1554. */
  1555. if (compat_need_64bit_alignment_fixup())
  1556. ge_size = sizeof(struct compat_gpioeevent_data);
  1557. else
  1558. ge_size = sizeof(struct gpioevent_data);
  1559. if (count < ge_size)
  1560. return -EINVAL;
  1561. do {
  1562. spin_lock(&le->wait.lock);
  1563. if (kfifo_is_empty(&le->events)) {
  1564. if (bytes_read) {
  1565. spin_unlock(&le->wait.lock);
  1566. return bytes_read;
  1567. }
  1568. if (file->f_flags & O_NONBLOCK) {
  1569. spin_unlock(&le->wait.lock);
  1570. return -EAGAIN;
  1571. }
  1572. ret = wait_event_interruptible_locked(le->wait,
  1573. !kfifo_is_empty(&le->events));
  1574. if (ret) {
  1575. spin_unlock(&le->wait.lock);
  1576. return ret;
  1577. }
  1578. }
  1579. ret = kfifo_out(&le->events, &ge, 1);
  1580. spin_unlock(&le->wait.lock);
  1581. if (ret != 1) {
  1582. /*
  1583. * This should never happen - we were holding the lock
  1584. * from the moment we learned the fifo is no longer
  1585. * empty until now.
  1586. */
  1587. ret = -EIO;
  1588. break;
  1589. }
  1590. if (copy_to_user(buf + bytes_read, &ge, ge_size))
  1591. return -EFAULT;
  1592. bytes_read += ge_size;
  1593. } while (count >= bytes_read + ge_size);
  1594. return bytes_read;
  1595. }
  1596. static ssize_t lineevent_read(struct file *file, char __user *buf,
  1597. size_t count, loff_t *f_ps)
  1598. {
  1599. struct lineevent_state *le = file->private_data;
  1600. return call_read_locked(file, buf, count, f_ps, le->gdev,
  1601. lineevent_read_unlocked);
  1602. }
  1603. static void lineevent_free(struct lineevent_state *le)
  1604. {
  1605. if (le->irq)
  1606. free_irq(le->irq, le);
  1607. if (le->desc)
  1608. gpiod_free(le->desc);
  1609. kfree(le->label);
  1610. put_device(&le->gdev->dev);
  1611. kfree(le);
  1612. }
  1613. static int lineevent_release(struct inode *inode, struct file *file)
  1614. {
  1615. lineevent_free(file->private_data);
  1616. return 0;
  1617. }
  1618. static long lineevent_ioctl_unlocked(struct file *file, unsigned int cmd,
  1619. unsigned long arg)
  1620. {
  1621. struct lineevent_state *le = file->private_data;
  1622. void __user *ip = (void __user *)arg;
  1623. struct gpiohandle_data ghd;
  1624. if (!le->gdev->chip)
  1625. return -ENODEV;
  1626. /*
  1627. * We can get the value for an event line but not set it,
  1628. * because it is input by definition.
  1629. */
  1630. if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
  1631. int val;
  1632. memset(&ghd, 0, sizeof(ghd));
  1633. val = gpiod_get_value_cansleep(le->desc);
  1634. if (val < 0)
  1635. return val;
  1636. ghd.values[0] = val;
  1637. if (copy_to_user(ip, &ghd, sizeof(ghd)))
  1638. return -EFAULT;
  1639. return 0;
  1640. }
  1641. return -EINVAL;
  1642. }
  1643. static long lineevent_ioctl(struct file *file, unsigned int cmd,
  1644. unsigned long arg)
  1645. {
  1646. struct lineevent_state *le = file->private_data;
  1647. return call_ioctl_locked(file, cmd, arg, le->gdev,
  1648. lineevent_ioctl_unlocked);
  1649. }
  1650. #ifdef CONFIG_COMPAT
  1651. static long lineevent_ioctl_compat(struct file *file, unsigned int cmd,
  1652. unsigned long arg)
  1653. {
  1654. return lineevent_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  1655. }
  1656. #endif
  1657. static const struct file_operations lineevent_fileops = {
  1658. .release = lineevent_release,
  1659. .read = lineevent_read,
  1660. .poll = lineevent_poll,
  1661. .owner = THIS_MODULE,
  1662. .llseek = noop_llseek,
  1663. .unlocked_ioctl = lineevent_ioctl,
  1664. #ifdef CONFIG_COMPAT
  1665. .compat_ioctl = lineevent_ioctl_compat,
  1666. #endif
  1667. };
  1668. static irqreturn_t lineevent_irq_thread(int irq, void *p)
  1669. {
  1670. struct lineevent_state *le = p;
  1671. struct gpioevent_data ge;
  1672. int ret;
  1673. /* Do not leak kernel stack to userspace */
  1674. memset(&ge, 0, sizeof(ge));
  1675. /*
  1676. * We may be running from a nested threaded interrupt in which case
  1677. * we didn't get the timestamp from lineevent_irq_handler().
  1678. */
  1679. if (!le->timestamp)
  1680. ge.timestamp = ktime_get_ns();
  1681. else
  1682. ge.timestamp = le->timestamp;
  1683. if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
  1684. && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
  1685. int level = gpiod_get_value_cansleep(le->desc);
  1686. if (level)
  1687. /* Emit low-to-high event */
  1688. ge.id = GPIOEVENT_EVENT_RISING_EDGE;
  1689. else
  1690. /* Emit high-to-low event */
  1691. ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
  1692. } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
  1693. /* Emit low-to-high event */
  1694. ge.id = GPIOEVENT_EVENT_RISING_EDGE;
  1695. } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
  1696. /* Emit high-to-low event */
  1697. ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
  1698. } else {
  1699. return IRQ_NONE;
  1700. }
  1701. ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge,
  1702. 1, &le->wait.lock);
  1703. if (ret)
  1704. wake_up_poll(&le->wait, EPOLLIN);
  1705. else
  1706. pr_debug_ratelimited("event FIFO is full - event dropped\n");
  1707. return IRQ_HANDLED;
  1708. }
  1709. static irqreturn_t lineevent_irq_handler(int irq, void *p)
  1710. {
  1711. struct lineevent_state *le = p;
  1712. /*
  1713. * Just store the timestamp in hardirq context so we get it as
  1714. * close in time as possible to the actual event.
  1715. */
  1716. le->timestamp = ktime_get_ns();
  1717. return IRQ_WAKE_THREAD;
  1718. }
  1719. static int lineevent_create(struct gpio_device *gdev, void __user *ip)
  1720. {
  1721. struct gpioevent_request eventreq;
  1722. struct lineevent_state *le;
  1723. struct gpio_desc *desc;
  1724. struct file *file;
  1725. u32 offset;
  1726. u32 lflags;
  1727. u32 eflags;
  1728. int fd;
  1729. int ret;
  1730. int irq, irqflags = 0;
  1731. if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
  1732. return -EFAULT;
  1733. offset = eventreq.lineoffset;
  1734. lflags = eventreq.handleflags;
  1735. eflags = eventreq.eventflags;
  1736. desc = gpiochip_get_desc(gdev->chip, offset);
  1737. if (IS_ERR(desc))
  1738. return PTR_ERR(desc);
  1739. /* Return an error if a unknown flag is set */
  1740. if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
  1741. (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
  1742. return -EINVAL;
  1743. /* This is just wrong: we don't look for events on output lines */
  1744. if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
  1745. (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
  1746. (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
  1747. return -EINVAL;
  1748. /* Only one bias flag can be set. */
  1749. if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
  1750. (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
  1751. GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
  1752. ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
  1753. (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
  1754. return -EINVAL;
  1755. le = kzalloc(sizeof(*le), GFP_KERNEL);
  1756. if (!le)
  1757. return -ENOMEM;
  1758. le->gdev = gdev;
  1759. get_device(&gdev->dev);
  1760. if (eventreq.consumer_label[0] != '\0') {
  1761. /* label is only initialized if consumer_label is set */
  1762. le->label = kstrndup(eventreq.consumer_label,
  1763. sizeof(eventreq.consumer_label) - 1,
  1764. GFP_KERNEL);
  1765. if (!le->label) {
  1766. ret = -ENOMEM;
  1767. goto out_free_le;
  1768. }
  1769. }
  1770. ret = gpiod_request_user(desc, le->label);
  1771. if (ret)
  1772. goto out_free_le;
  1773. le->desc = desc;
  1774. le->eflags = eflags;
  1775. linehandle_flags_to_desc_flags(lflags, &desc->flags);
  1776. ret = gpiod_direction_input(desc);
  1777. if (ret)
  1778. goto out_free_le;
  1779. blocking_notifier_call_chain(&desc->gdev->notifier,
  1780. GPIO_V2_LINE_CHANGED_REQUESTED, desc);
  1781. irq = gpiod_to_irq(desc);
  1782. if (irq <= 0) {
  1783. ret = -ENODEV;
  1784. goto out_free_le;
  1785. }
  1786. if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
  1787. irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
  1788. IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
  1789. if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
  1790. irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
  1791. IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
  1792. irqflags |= IRQF_ONESHOT;
  1793. INIT_KFIFO(le->events);
  1794. init_waitqueue_head(&le->wait);
  1795. /* Request a thread to read the events */
  1796. ret = request_threaded_irq(irq,
  1797. lineevent_irq_handler,
  1798. lineevent_irq_thread,
  1799. irqflags,
  1800. le->label,
  1801. le);
  1802. if (ret)
  1803. goto out_free_le;
  1804. le->irq = irq;
  1805. fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
  1806. if (fd < 0) {
  1807. ret = fd;
  1808. goto out_free_le;
  1809. }
  1810. file = anon_inode_getfile("gpio-event",
  1811. &lineevent_fileops,
  1812. le,
  1813. O_RDONLY | O_CLOEXEC);
  1814. if (IS_ERR(file)) {
  1815. ret = PTR_ERR(file);
  1816. goto out_put_unused_fd;
  1817. }
  1818. eventreq.fd = fd;
  1819. if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
  1820. /*
  1821. * fput() will trigger the release() callback, so do not go onto
  1822. * the regular error cleanup path here.
  1823. */
  1824. fput(file);
  1825. put_unused_fd(fd);
  1826. return -EFAULT;
  1827. }
  1828. fd_install(fd, file);
  1829. return 0;
  1830. out_put_unused_fd:
  1831. put_unused_fd(fd);
  1832. out_free_le:
  1833. lineevent_free(le);
  1834. return ret;
  1835. }
  1836. static void gpio_v2_line_info_to_v1(struct gpio_v2_line_info *info_v2,
  1837. struct gpioline_info *info_v1)
  1838. {
  1839. u64 flagsv2 = info_v2->flags;
  1840. memcpy(info_v1->name, info_v2->name, sizeof(info_v1->name));
  1841. memcpy(info_v1->consumer, info_v2->consumer, sizeof(info_v1->consumer));
  1842. info_v1->line_offset = info_v2->offset;
  1843. info_v1->flags = 0;
  1844. if (flagsv2 & GPIO_V2_LINE_FLAG_USED)
  1845. info_v1->flags |= GPIOLINE_FLAG_KERNEL;
  1846. if (flagsv2 & GPIO_V2_LINE_FLAG_OUTPUT)
  1847. info_v1->flags |= GPIOLINE_FLAG_IS_OUT;
  1848. if (flagsv2 & GPIO_V2_LINE_FLAG_ACTIVE_LOW)
  1849. info_v1->flags |= GPIOLINE_FLAG_ACTIVE_LOW;
  1850. if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_DRAIN)
  1851. info_v1->flags |= GPIOLINE_FLAG_OPEN_DRAIN;
  1852. if (flagsv2 & GPIO_V2_LINE_FLAG_OPEN_SOURCE)
  1853. info_v1->flags |= GPIOLINE_FLAG_OPEN_SOURCE;
  1854. if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_UP)
  1855. info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
  1856. if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN)
  1857. info_v1->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
  1858. if (flagsv2 & GPIO_V2_LINE_FLAG_BIAS_DISABLED)
  1859. info_v1->flags |= GPIOLINE_FLAG_BIAS_DISABLE;
  1860. }
  1861. static void gpio_v2_line_info_changed_to_v1(
  1862. struct gpio_v2_line_info_changed *lic_v2,
  1863. struct gpioline_info_changed *lic_v1)
  1864. {
  1865. memset(lic_v1, 0, sizeof(*lic_v1));
  1866. gpio_v2_line_info_to_v1(&lic_v2->info, &lic_v1->info);
  1867. lic_v1->timestamp = lic_v2->timestamp_ns;
  1868. lic_v1->event_type = lic_v2->event_type;
  1869. }
  1870. #endif /* CONFIG_GPIO_CDEV_V1 */
  1871. static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
  1872. struct gpio_v2_line_info *info)
  1873. {
  1874. struct gpio_chip *gc = desc->gdev->chip;
  1875. bool ok_for_pinctrl;
  1876. unsigned long flags;
  1877. u32 debounce_period_us;
  1878. unsigned int num_attrs = 0;
  1879. memset(info, 0, sizeof(*info));
  1880. info->offset = gpio_chip_hwgpio(desc);
  1881. /*
  1882. * This function takes a mutex so we must check this before taking
  1883. * the spinlock.
  1884. *
  1885. * FIXME: find a non-racy way to retrieve this information. Maybe a
  1886. * lock common to both frameworks?
  1887. */
  1888. ok_for_pinctrl =
  1889. pinctrl_gpio_can_use_line(gc->base + info->offset);
  1890. spin_lock_irqsave(&gpio_lock, flags);
  1891. if (desc->name)
  1892. strscpy(info->name, desc->name, sizeof(info->name));
  1893. if (desc->label)
  1894. strscpy(info->consumer, desc->label, sizeof(info->consumer));
  1895. /*
  1896. * Userspace only need to know that the kernel is using this GPIO so
  1897. * it can't use it.
  1898. */
  1899. info->flags = 0;
  1900. if (test_bit(FLAG_REQUESTED, &desc->flags) ||
  1901. test_bit(FLAG_IS_HOGGED, &desc->flags) ||
  1902. test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
  1903. test_bit(FLAG_EXPORT, &desc->flags) ||
  1904. test_bit(FLAG_SYSFS, &desc->flags) ||
  1905. !gpiochip_line_is_valid(gc, info->offset) ||
  1906. !ok_for_pinctrl)
  1907. info->flags |= GPIO_V2_LINE_FLAG_USED;
  1908. if (test_bit(FLAG_IS_OUT, &desc->flags))
  1909. info->flags |= GPIO_V2_LINE_FLAG_OUTPUT;
  1910. else
  1911. info->flags |= GPIO_V2_LINE_FLAG_INPUT;
  1912. if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
  1913. info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW;
  1914. if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
  1915. info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN;
  1916. if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
  1917. info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE;
  1918. if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
  1919. info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED;
  1920. if (test_bit(FLAG_PULL_DOWN, &desc->flags))
  1921. info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN;
  1922. if (test_bit(FLAG_PULL_UP, &desc->flags))
  1923. info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP;
  1924. if (test_bit(FLAG_EDGE_RISING, &desc->flags))
  1925. info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING;
  1926. if (test_bit(FLAG_EDGE_FALLING, &desc->flags))
  1927. info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING;
  1928. if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &desc->flags))
  1929. info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME;
  1930. else if (test_bit(FLAG_EVENT_CLOCK_HTE, &desc->flags))
  1931. info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE;
  1932. debounce_period_us = READ_ONCE(desc->debounce_period_us);
  1933. if (debounce_period_us) {
  1934. info->attrs[num_attrs].id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE;
  1935. info->attrs[num_attrs].debounce_period_us = debounce_period_us;
  1936. num_attrs++;
  1937. }
  1938. info->num_attrs = num_attrs;
  1939. spin_unlock_irqrestore(&gpio_lock, flags);
  1940. }
  1941. struct gpio_chardev_data {
  1942. struct gpio_device *gdev;
  1943. wait_queue_head_t wait;
  1944. DECLARE_KFIFO(events, struct gpio_v2_line_info_changed, 32);
  1945. struct notifier_block lineinfo_changed_nb;
  1946. unsigned long *watched_lines;
  1947. #ifdef CONFIG_GPIO_CDEV_V1
  1948. atomic_t watch_abi_version;
  1949. #endif
  1950. };
  1951. static int chipinfo_get(struct gpio_chardev_data *cdev, void __user *ip)
  1952. {
  1953. struct gpio_device *gdev = cdev->gdev;
  1954. struct gpiochip_info chipinfo;
  1955. memset(&chipinfo, 0, sizeof(chipinfo));
  1956. strscpy(chipinfo.name, dev_name(&gdev->dev), sizeof(chipinfo.name));
  1957. strscpy(chipinfo.label, gdev->label, sizeof(chipinfo.label));
  1958. chipinfo.lines = gdev->ngpio;
  1959. if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
  1960. return -EFAULT;
  1961. return 0;
  1962. }
  1963. #ifdef CONFIG_GPIO_CDEV_V1
  1964. /*
  1965. * returns 0 if the versions match, else the previously selected ABI version
  1966. */
  1967. static int lineinfo_ensure_abi_version(struct gpio_chardev_data *cdata,
  1968. unsigned int version)
  1969. {
  1970. int abiv = atomic_cmpxchg(&cdata->watch_abi_version, 0, version);
  1971. if (abiv == version)
  1972. return 0;
  1973. return abiv;
  1974. }
  1975. static int lineinfo_get_v1(struct gpio_chardev_data *cdev, void __user *ip,
  1976. bool watch)
  1977. {
  1978. struct gpio_desc *desc;
  1979. struct gpioline_info lineinfo;
  1980. struct gpio_v2_line_info lineinfo_v2;
  1981. if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
  1982. return -EFAULT;
  1983. /* this doubles as a range check on line_offset */
  1984. desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.line_offset);
  1985. if (IS_ERR(desc))
  1986. return PTR_ERR(desc);
  1987. if (watch) {
  1988. if (lineinfo_ensure_abi_version(cdev, 1))
  1989. return -EPERM;
  1990. if (test_and_set_bit(lineinfo.line_offset, cdev->watched_lines))
  1991. return -EBUSY;
  1992. }
  1993. gpio_desc_to_lineinfo(desc, &lineinfo_v2);
  1994. gpio_v2_line_info_to_v1(&lineinfo_v2, &lineinfo);
  1995. if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
  1996. if (watch)
  1997. clear_bit(lineinfo.line_offset, cdev->watched_lines);
  1998. return -EFAULT;
  1999. }
  2000. return 0;
  2001. }
  2002. #endif
  2003. static int lineinfo_get(struct gpio_chardev_data *cdev, void __user *ip,
  2004. bool watch)
  2005. {
  2006. struct gpio_desc *desc;
  2007. struct gpio_v2_line_info lineinfo;
  2008. if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
  2009. return -EFAULT;
  2010. if (memchr_inv(lineinfo.padding, 0, sizeof(lineinfo.padding)))
  2011. return -EINVAL;
  2012. desc = gpiochip_get_desc(cdev->gdev->chip, lineinfo.offset);
  2013. if (IS_ERR(desc))
  2014. return PTR_ERR(desc);
  2015. if (watch) {
  2016. #ifdef CONFIG_GPIO_CDEV_V1
  2017. if (lineinfo_ensure_abi_version(cdev, 2))
  2018. return -EPERM;
  2019. #endif
  2020. if (test_and_set_bit(lineinfo.offset, cdev->watched_lines))
  2021. return -EBUSY;
  2022. }
  2023. gpio_desc_to_lineinfo(desc, &lineinfo);
  2024. if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) {
  2025. if (watch)
  2026. clear_bit(lineinfo.offset, cdev->watched_lines);
  2027. return -EFAULT;
  2028. }
  2029. return 0;
  2030. }
  2031. static int lineinfo_unwatch(struct gpio_chardev_data *cdev, void __user *ip)
  2032. {
  2033. __u32 offset;
  2034. if (copy_from_user(&offset, ip, sizeof(offset)))
  2035. return -EFAULT;
  2036. if (offset >= cdev->gdev->ngpio)
  2037. return -EINVAL;
  2038. if (!test_and_clear_bit(offset, cdev->watched_lines))
  2039. return -EBUSY;
  2040. return 0;
  2041. }
  2042. /*
  2043. * gpio_ioctl() - ioctl handler for the GPIO chardev
  2044. */
  2045. static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2046. {
  2047. struct gpio_chardev_data *cdev = file->private_data;
  2048. struct gpio_device *gdev = cdev->gdev;
  2049. void __user *ip = (void __user *)arg;
  2050. /* We fail any subsequent ioctl():s when the chip is gone */
  2051. if (!gdev->chip)
  2052. return -ENODEV;
  2053. /* Fill in the struct and pass to userspace */
  2054. switch (cmd) {
  2055. case GPIO_GET_CHIPINFO_IOCTL:
  2056. return chipinfo_get(cdev, ip);
  2057. #ifdef CONFIG_GPIO_CDEV_V1
  2058. case GPIO_GET_LINEHANDLE_IOCTL:
  2059. return linehandle_create(gdev, ip);
  2060. case GPIO_GET_LINEEVENT_IOCTL:
  2061. return lineevent_create(gdev, ip);
  2062. case GPIO_GET_LINEINFO_IOCTL:
  2063. return lineinfo_get_v1(cdev, ip, false);
  2064. case GPIO_GET_LINEINFO_WATCH_IOCTL:
  2065. return lineinfo_get_v1(cdev, ip, true);
  2066. #endif /* CONFIG_GPIO_CDEV_V1 */
  2067. case GPIO_V2_GET_LINEINFO_IOCTL:
  2068. return lineinfo_get(cdev, ip, false);
  2069. case GPIO_V2_GET_LINEINFO_WATCH_IOCTL:
  2070. return lineinfo_get(cdev, ip, true);
  2071. case GPIO_V2_GET_LINE_IOCTL:
  2072. return linereq_create(gdev, ip);
  2073. case GPIO_GET_LINEINFO_UNWATCH_IOCTL:
  2074. return lineinfo_unwatch(cdev, ip);
  2075. default:
  2076. return -EINVAL;
  2077. }
  2078. }
  2079. #ifdef CONFIG_COMPAT
  2080. static long gpio_ioctl_compat(struct file *file, unsigned int cmd,
  2081. unsigned long arg)
  2082. {
  2083. return gpio_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  2084. }
  2085. #endif
  2086. static struct gpio_chardev_data *
  2087. to_gpio_chardev_data(struct notifier_block *nb)
  2088. {
  2089. return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb);
  2090. }
  2091. static int lineinfo_changed_notify(struct notifier_block *nb,
  2092. unsigned long action, void *data)
  2093. {
  2094. struct gpio_chardev_data *cdev = to_gpio_chardev_data(nb);
  2095. struct gpio_v2_line_info_changed chg;
  2096. struct gpio_desc *desc = data;
  2097. int ret;
  2098. if (!test_bit(gpio_chip_hwgpio(desc), cdev->watched_lines))
  2099. return NOTIFY_DONE;
  2100. memset(&chg, 0, sizeof(chg));
  2101. chg.event_type = action;
  2102. chg.timestamp_ns = ktime_get_ns();
  2103. gpio_desc_to_lineinfo(desc, &chg.info);
  2104. ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock);
  2105. if (ret)
  2106. wake_up_poll(&cdev->wait, EPOLLIN);
  2107. else
  2108. pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n");
  2109. return NOTIFY_OK;
  2110. }
  2111. static __poll_t lineinfo_watch_poll_unlocked(struct file *file,
  2112. struct poll_table_struct *pollt)
  2113. {
  2114. struct gpio_chardev_data *cdev = file->private_data;
  2115. __poll_t events = 0;
  2116. if (!cdev->gdev->chip)
  2117. return EPOLLHUP | EPOLLERR;
  2118. poll_wait(file, &cdev->wait, pollt);
  2119. if (!kfifo_is_empty_spinlocked_noirqsave(&cdev->events,
  2120. &cdev->wait.lock))
  2121. events = EPOLLIN | EPOLLRDNORM;
  2122. return events;
  2123. }
  2124. static __poll_t lineinfo_watch_poll(struct file *file,
  2125. struct poll_table_struct *pollt)
  2126. {
  2127. struct gpio_chardev_data *cdev = file->private_data;
  2128. return call_poll_locked(file, pollt, cdev->gdev,
  2129. lineinfo_watch_poll_unlocked);
  2130. }
  2131. static ssize_t lineinfo_watch_read_unlocked(struct file *file, char __user *buf,
  2132. size_t count, loff_t *off)
  2133. {
  2134. struct gpio_chardev_data *cdev = file->private_data;
  2135. struct gpio_v2_line_info_changed event;
  2136. ssize_t bytes_read = 0;
  2137. int ret;
  2138. size_t event_size;
  2139. if (!cdev->gdev->chip)
  2140. return -ENODEV;
  2141. #ifndef CONFIG_GPIO_CDEV_V1
  2142. event_size = sizeof(struct gpio_v2_line_info_changed);
  2143. if (count < event_size)
  2144. return -EINVAL;
  2145. #endif
  2146. do {
  2147. spin_lock(&cdev->wait.lock);
  2148. if (kfifo_is_empty(&cdev->events)) {
  2149. if (bytes_read) {
  2150. spin_unlock(&cdev->wait.lock);
  2151. return bytes_read;
  2152. }
  2153. if (file->f_flags & O_NONBLOCK) {
  2154. spin_unlock(&cdev->wait.lock);
  2155. return -EAGAIN;
  2156. }
  2157. ret = wait_event_interruptible_locked(cdev->wait,
  2158. !kfifo_is_empty(&cdev->events));
  2159. if (ret) {
  2160. spin_unlock(&cdev->wait.lock);
  2161. return ret;
  2162. }
  2163. }
  2164. #ifdef CONFIG_GPIO_CDEV_V1
  2165. /* must be after kfifo check so watch_abi_version is set */
  2166. if (atomic_read(&cdev->watch_abi_version) == 2)
  2167. event_size = sizeof(struct gpio_v2_line_info_changed);
  2168. else
  2169. event_size = sizeof(struct gpioline_info_changed);
  2170. if (count < event_size) {
  2171. spin_unlock(&cdev->wait.lock);
  2172. return -EINVAL;
  2173. }
  2174. #endif
  2175. ret = kfifo_out(&cdev->events, &event, 1);
  2176. spin_unlock(&cdev->wait.lock);
  2177. if (ret != 1) {
  2178. ret = -EIO;
  2179. break;
  2180. /* We should never get here. See lineevent_read(). */
  2181. }
  2182. #ifdef CONFIG_GPIO_CDEV_V1
  2183. if (event_size == sizeof(struct gpio_v2_line_info_changed)) {
  2184. if (copy_to_user(buf + bytes_read, &event, event_size))
  2185. return -EFAULT;
  2186. } else {
  2187. struct gpioline_info_changed event_v1;
  2188. gpio_v2_line_info_changed_to_v1(&event, &event_v1);
  2189. if (copy_to_user(buf + bytes_read, &event_v1,
  2190. event_size))
  2191. return -EFAULT;
  2192. }
  2193. #else
  2194. if (copy_to_user(buf + bytes_read, &event, event_size))
  2195. return -EFAULT;
  2196. #endif
  2197. bytes_read += event_size;
  2198. } while (count >= bytes_read + sizeof(event));
  2199. return bytes_read;
  2200. }
  2201. static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,
  2202. size_t count, loff_t *off)
  2203. {
  2204. struct gpio_chardev_data *cdev = file->private_data;
  2205. return call_read_locked(file, buf, count, off, cdev->gdev,
  2206. lineinfo_watch_read_unlocked);
  2207. }
  2208. /**
  2209. * gpio_chrdev_open() - open the chardev for ioctl operations
  2210. * @inode: inode for this chardev
  2211. * @file: file struct for storing private data
  2212. * Returns 0 on success
  2213. */
  2214. static int gpio_chrdev_open(struct inode *inode, struct file *file)
  2215. {
  2216. struct gpio_device *gdev = container_of(inode->i_cdev,
  2217. struct gpio_device, chrdev);
  2218. struct gpio_chardev_data *cdev;
  2219. int ret = -ENOMEM;
  2220. down_read(&gdev->sem);
  2221. /* Fail on open if the backing gpiochip is gone */
  2222. if (!gdev->chip) {
  2223. ret = -ENODEV;
  2224. goto out_unlock;
  2225. }
  2226. cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
  2227. if (!cdev)
  2228. goto out_unlock;
  2229. cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL);
  2230. if (!cdev->watched_lines)
  2231. goto out_free_cdev;
  2232. init_waitqueue_head(&cdev->wait);
  2233. INIT_KFIFO(cdev->events);
  2234. cdev->gdev = gdev;
  2235. cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify;
  2236. ret = blocking_notifier_chain_register(&gdev->notifier,
  2237. &cdev->lineinfo_changed_nb);
  2238. if (ret)
  2239. goto out_free_bitmap;
  2240. get_device(&gdev->dev);
  2241. file->private_data = cdev;
  2242. ret = nonseekable_open(inode, file);
  2243. if (ret)
  2244. goto out_unregister_notifier;
  2245. up_read(&gdev->sem);
  2246. return ret;
  2247. out_unregister_notifier:
  2248. blocking_notifier_chain_unregister(&gdev->notifier,
  2249. &cdev->lineinfo_changed_nb);
  2250. out_free_bitmap:
  2251. bitmap_free(cdev->watched_lines);
  2252. out_free_cdev:
  2253. kfree(cdev);
  2254. out_unlock:
  2255. up_read(&gdev->sem);
  2256. return ret;
  2257. }
  2258. /**
  2259. * gpio_chrdev_release() - close chardev after ioctl operations
  2260. * @inode: inode for this chardev
  2261. * @file: file struct for storing private data
  2262. * Returns 0 on success
  2263. */
  2264. static int gpio_chrdev_release(struct inode *inode, struct file *file)
  2265. {
  2266. struct gpio_chardev_data *cdev = file->private_data;
  2267. struct gpio_device *gdev = cdev->gdev;
  2268. bitmap_free(cdev->watched_lines);
  2269. blocking_notifier_chain_unregister(&gdev->notifier,
  2270. &cdev->lineinfo_changed_nb);
  2271. put_device(&gdev->dev);
  2272. kfree(cdev);
  2273. return 0;
  2274. }
  2275. static const struct file_operations gpio_fileops = {
  2276. .release = gpio_chrdev_release,
  2277. .open = gpio_chrdev_open,
  2278. .poll = lineinfo_watch_poll,
  2279. .read = lineinfo_watch_read,
  2280. .owner = THIS_MODULE,
  2281. .llseek = no_llseek,
  2282. .unlocked_ioctl = gpio_ioctl,
  2283. #ifdef CONFIG_COMPAT
  2284. .compat_ioctl = gpio_ioctl_compat,
  2285. #endif
  2286. };
  2287. int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt)
  2288. {
  2289. int ret;
  2290. cdev_init(&gdev->chrdev, &gpio_fileops);
  2291. gdev->chrdev.owner = THIS_MODULE;
  2292. gdev->dev.devt = MKDEV(MAJOR(devt), gdev->id);
  2293. ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
  2294. if (ret)
  2295. return ret;
  2296. chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
  2297. MAJOR(devt), gdev->id);
  2298. return 0;
  2299. }
  2300. void gpiolib_cdev_unregister(struct gpio_device *gdev)
  2301. {
  2302. cdev_device_del(&gdev->chrdev, &gdev->dev);
  2303. }