gpiolib-cdev.c 59 KB

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