comedi_fops.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * comedi/comedi_fops.c
  4. * comedi kernel module
  5. *
  6. * COMEDI - Linux Control and Measurement Device Interface
  7. * Copyright (C) 1997-2007 David A. Schleef <ds@schleef.org>
  8. * compat ioctls:
  9. * Author: Ian Abbott, MEV Ltd. <abbotti@mev.co.uk>
  10. * Copyright (C) 2007 MEV Ltd. <http://www.mev.co.uk/>
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/module.h>
  14. #include <linux/errno.h>
  15. #include <linux/kernel.h>
  16. #include <linux/sched/signal.h>
  17. #include <linux/fcntl.h>
  18. #include <linux/delay.h>
  19. #include <linux/mm.h>
  20. #include <linux/slab.h>
  21. #include <linux/poll.h>
  22. #include <linux/device.h>
  23. #include <linux/fs.h>
  24. #include <linux/comedi/comedidev.h>
  25. #include <linux/cdev.h>
  26. #include <linux/io.h>
  27. #include <linux/uaccess.h>
  28. #include <linux/compat.h>
  29. #include "comedi_internal.h"
  30. /*
  31. * comedi_subdevice "runflags"
  32. * COMEDI_SRF_RT: DEPRECATED: command is running real-time
  33. * COMEDI_SRF_ERROR: indicates an COMEDI_CB_ERROR event has occurred
  34. * since the last command was started
  35. * COMEDI_SRF_RUNNING: command is running
  36. * COMEDI_SRF_FREE_SPRIV: free s->private on detach
  37. *
  38. * COMEDI_SRF_BUSY_MASK: runflags that indicate the subdevice is "busy"
  39. */
  40. #define COMEDI_SRF_RT BIT(1)
  41. #define COMEDI_SRF_ERROR BIT(2)
  42. #define COMEDI_SRF_RUNNING BIT(27)
  43. #define COMEDI_SRF_FREE_SPRIV BIT(31)
  44. #define COMEDI_SRF_BUSY_MASK (COMEDI_SRF_ERROR | COMEDI_SRF_RUNNING)
  45. /**
  46. * struct comedi_file - Per-file private data for COMEDI device
  47. * @dev: COMEDI device.
  48. * @read_subdev: Current "read" subdevice.
  49. * @write_subdev: Current "write" subdevice.
  50. * @last_detach_count: Last known detach count.
  51. * @last_attached: Last known attached/detached state.
  52. */
  53. struct comedi_file {
  54. struct comedi_device *dev;
  55. struct comedi_subdevice *read_subdev;
  56. struct comedi_subdevice *write_subdev;
  57. unsigned int last_detach_count;
  58. unsigned int last_attached:1;
  59. };
  60. #define COMEDI_NUM_MINORS 0x100
  61. #define COMEDI_NUM_SUBDEVICE_MINORS \
  62. (COMEDI_NUM_MINORS - COMEDI_NUM_BOARD_MINORS)
  63. static unsigned short comedi_num_legacy_minors;
  64. module_param(comedi_num_legacy_minors, ushort, 0444);
  65. MODULE_PARM_DESC(comedi_num_legacy_minors,
  66. "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
  67. );
  68. unsigned int comedi_default_buf_size_kb = CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB;
  69. module_param(comedi_default_buf_size_kb, uint, 0644);
  70. MODULE_PARM_DESC(comedi_default_buf_size_kb,
  71. "default asynchronous buffer size in KiB (default "
  72. __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB) ")");
  73. unsigned int comedi_default_buf_maxsize_kb =
  74. CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB;
  75. module_param(comedi_default_buf_maxsize_kb, uint, 0644);
  76. MODULE_PARM_DESC(comedi_default_buf_maxsize_kb,
  77. "default maximum size of asynchronous buffer in KiB (default "
  78. __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB) ")");
  79. static DEFINE_MUTEX(comedi_board_minor_table_lock);
  80. static struct comedi_device
  81. *comedi_board_minor_table[COMEDI_NUM_BOARD_MINORS];
  82. static DEFINE_MUTEX(comedi_subdevice_minor_table_lock);
  83. /* Note: indexed by minor - COMEDI_NUM_BOARD_MINORS. */
  84. static struct comedi_subdevice
  85. *comedi_subdevice_minor_table[COMEDI_NUM_SUBDEVICE_MINORS];
  86. static struct cdev comedi_cdev;
  87. static void comedi_device_init(struct comedi_device *dev)
  88. {
  89. kref_init(&dev->refcount);
  90. spin_lock_init(&dev->spinlock);
  91. mutex_init(&dev->mutex);
  92. init_rwsem(&dev->attach_lock);
  93. dev->minor = -1;
  94. }
  95. static void comedi_dev_kref_release(struct kref *kref)
  96. {
  97. struct comedi_device *dev =
  98. container_of(kref, struct comedi_device, refcount);
  99. mutex_destroy(&dev->mutex);
  100. put_device(dev->class_dev);
  101. kfree(dev);
  102. }
  103. /**
  104. * comedi_dev_put() - Release a use of a COMEDI device
  105. * @dev: COMEDI device.
  106. *
  107. * Must be called when a user of a COMEDI device is finished with it.
  108. * When the last user of the COMEDI device calls this function, the
  109. * COMEDI device is destroyed.
  110. *
  111. * Return: 1 if the COMEDI device is destroyed by this call or @dev is
  112. * NULL, otherwise return 0. Callers must not assume the COMEDI
  113. * device is still valid if this function returns 0.
  114. */
  115. int comedi_dev_put(struct comedi_device *dev)
  116. {
  117. if (dev)
  118. return kref_put(&dev->refcount, comedi_dev_kref_release);
  119. return 1;
  120. }
  121. EXPORT_SYMBOL_GPL(comedi_dev_put);
  122. static struct comedi_device *comedi_dev_get(struct comedi_device *dev)
  123. {
  124. if (dev)
  125. kref_get(&dev->refcount);
  126. return dev;
  127. }
  128. static void comedi_device_cleanup(struct comedi_device *dev)
  129. {
  130. struct module *driver_module = NULL;
  131. if (!dev)
  132. return;
  133. mutex_lock(&dev->mutex);
  134. if (dev->attached)
  135. driver_module = dev->driver->module;
  136. comedi_device_detach(dev);
  137. if (driver_module && dev->use_count)
  138. module_put(driver_module);
  139. mutex_unlock(&dev->mutex);
  140. }
  141. static bool comedi_clear_board_dev(struct comedi_device *dev)
  142. {
  143. unsigned int i = dev->minor;
  144. bool cleared = false;
  145. lockdep_assert_held(&dev->mutex);
  146. mutex_lock(&comedi_board_minor_table_lock);
  147. if (dev == comedi_board_minor_table[i]) {
  148. comedi_board_minor_table[i] = NULL;
  149. cleared = true;
  150. }
  151. mutex_unlock(&comedi_board_minor_table_lock);
  152. return cleared;
  153. }
  154. static struct comedi_device *comedi_clear_board_minor(unsigned int minor)
  155. {
  156. struct comedi_device *dev;
  157. mutex_lock(&comedi_board_minor_table_lock);
  158. dev = comedi_board_minor_table[minor];
  159. comedi_board_minor_table[minor] = NULL;
  160. mutex_unlock(&comedi_board_minor_table_lock);
  161. return dev;
  162. }
  163. static struct comedi_subdevice *
  164. comedi_subdevice_from_minor(const struct comedi_device *dev, unsigned int minor)
  165. {
  166. struct comedi_subdevice *s;
  167. unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
  168. mutex_lock(&comedi_subdevice_minor_table_lock);
  169. s = comedi_subdevice_minor_table[i];
  170. if (s && s->device != dev)
  171. s = NULL;
  172. mutex_unlock(&comedi_subdevice_minor_table_lock);
  173. return s;
  174. }
  175. static struct comedi_device *comedi_dev_get_from_board_minor(unsigned int minor)
  176. {
  177. struct comedi_device *dev;
  178. mutex_lock(&comedi_board_minor_table_lock);
  179. dev = comedi_dev_get(comedi_board_minor_table[minor]);
  180. mutex_unlock(&comedi_board_minor_table_lock);
  181. return dev;
  182. }
  183. static struct comedi_device *
  184. comedi_dev_get_from_subdevice_minor(unsigned int minor)
  185. {
  186. struct comedi_device *dev;
  187. struct comedi_subdevice *s;
  188. unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
  189. mutex_lock(&comedi_subdevice_minor_table_lock);
  190. s = comedi_subdevice_minor_table[i];
  191. dev = comedi_dev_get(s ? s->device : NULL);
  192. mutex_unlock(&comedi_subdevice_minor_table_lock);
  193. return dev;
  194. }
  195. /**
  196. * comedi_dev_get_from_minor() - Get COMEDI device by minor device number
  197. * @minor: Minor device number.
  198. *
  199. * Finds the COMEDI device associated with the minor device number, if any,
  200. * and increments its reference count. The COMEDI device is prevented from
  201. * being freed until a matching call is made to comedi_dev_put().
  202. *
  203. * Return: A pointer to the COMEDI device if it exists, with its usage
  204. * reference incremented. Return NULL if no COMEDI device exists with the
  205. * specified minor device number.
  206. */
  207. struct comedi_device *comedi_dev_get_from_minor(unsigned int minor)
  208. {
  209. if (minor < COMEDI_NUM_BOARD_MINORS)
  210. return comedi_dev_get_from_board_minor(minor);
  211. return comedi_dev_get_from_subdevice_minor(minor);
  212. }
  213. EXPORT_SYMBOL_GPL(comedi_dev_get_from_minor);
  214. static struct comedi_subdevice *
  215. comedi_read_subdevice(const struct comedi_device *dev, unsigned int minor)
  216. {
  217. struct comedi_subdevice *s;
  218. lockdep_assert_held(&dev->mutex);
  219. if (minor >= COMEDI_NUM_BOARD_MINORS) {
  220. s = comedi_subdevice_from_minor(dev, minor);
  221. if (!s || (s->subdev_flags & SDF_CMD_READ))
  222. return s;
  223. }
  224. return dev->read_subdev;
  225. }
  226. static struct comedi_subdevice *
  227. comedi_write_subdevice(const struct comedi_device *dev, unsigned int minor)
  228. {
  229. struct comedi_subdevice *s;
  230. lockdep_assert_held(&dev->mutex);
  231. if (minor >= COMEDI_NUM_BOARD_MINORS) {
  232. s = comedi_subdevice_from_minor(dev, minor);
  233. if (!s || (s->subdev_flags & SDF_CMD_WRITE))
  234. return s;
  235. }
  236. return dev->write_subdev;
  237. }
  238. static void comedi_file_reset(struct file *file)
  239. {
  240. struct comedi_file *cfp = file->private_data;
  241. struct comedi_device *dev = cfp->dev;
  242. struct comedi_subdevice *s, *read_s, *write_s;
  243. unsigned int minor = iminor(file_inode(file));
  244. read_s = dev->read_subdev;
  245. write_s = dev->write_subdev;
  246. if (minor >= COMEDI_NUM_BOARD_MINORS) {
  247. s = comedi_subdevice_from_minor(dev, minor);
  248. if (!s || s->subdev_flags & SDF_CMD_READ)
  249. read_s = s;
  250. if (!s || s->subdev_flags & SDF_CMD_WRITE)
  251. write_s = s;
  252. }
  253. cfp->last_attached = dev->attached;
  254. cfp->last_detach_count = dev->detach_count;
  255. WRITE_ONCE(cfp->read_subdev, read_s);
  256. WRITE_ONCE(cfp->write_subdev, write_s);
  257. }
  258. static void comedi_file_check(struct file *file)
  259. {
  260. struct comedi_file *cfp = file->private_data;
  261. struct comedi_device *dev = cfp->dev;
  262. if (cfp->last_attached != dev->attached ||
  263. cfp->last_detach_count != dev->detach_count)
  264. comedi_file_reset(file);
  265. }
  266. static struct comedi_subdevice *comedi_file_read_subdevice(struct file *file)
  267. {
  268. struct comedi_file *cfp = file->private_data;
  269. comedi_file_check(file);
  270. return READ_ONCE(cfp->read_subdev);
  271. }
  272. static struct comedi_subdevice *comedi_file_write_subdevice(struct file *file)
  273. {
  274. struct comedi_file *cfp = file->private_data;
  275. comedi_file_check(file);
  276. return READ_ONCE(cfp->write_subdev);
  277. }
  278. static int resize_async_buffer(struct comedi_device *dev,
  279. struct comedi_subdevice *s,
  280. unsigned int new_size)
  281. {
  282. struct comedi_async *async = s->async;
  283. int retval;
  284. lockdep_assert_held(&dev->mutex);
  285. if (new_size > async->max_bufsize)
  286. return -EPERM;
  287. if (s->busy) {
  288. dev_dbg(dev->class_dev,
  289. "subdevice is busy, cannot resize buffer\n");
  290. return -EBUSY;
  291. }
  292. if (comedi_buf_is_mmapped(s)) {
  293. dev_dbg(dev->class_dev,
  294. "subdevice is mmapped, cannot resize buffer\n");
  295. return -EBUSY;
  296. }
  297. /* make sure buffer is an integral number of pages (we round up) */
  298. new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
  299. retval = comedi_buf_alloc(dev, s, new_size);
  300. if (retval < 0)
  301. return retval;
  302. if (s->buf_change) {
  303. retval = s->buf_change(dev, s);
  304. if (retval < 0)
  305. return retval;
  306. }
  307. dev_dbg(dev->class_dev, "subd %d buffer resized to %i bytes\n",
  308. s->index, async->prealloc_bufsz);
  309. return 0;
  310. }
  311. /* sysfs attribute files */
  312. static ssize_t max_read_buffer_kb_show(struct device *csdev,
  313. struct device_attribute *attr, char *buf)
  314. {
  315. unsigned int minor = MINOR(csdev->devt);
  316. struct comedi_device *dev;
  317. struct comedi_subdevice *s;
  318. unsigned int size = 0;
  319. dev = comedi_dev_get_from_minor(minor);
  320. if (!dev)
  321. return -ENODEV;
  322. mutex_lock(&dev->mutex);
  323. s = comedi_read_subdevice(dev, minor);
  324. if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
  325. size = s->async->max_bufsize / 1024;
  326. mutex_unlock(&dev->mutex);
  327. comedi_dev_put(dev);
  328. return sysfs_emit(buf, "%u\n", size);
  329. }
  330. static ssize_t max_read_buffer_kb_store(struct device *csdev,
  331. struct device_attribute *attr,
  332. const char *buf, size_t count)
  333. {
  334. unsigned int minor = MINOR(csdev->devt);
  335. struct comedi_device *dev;
  336. struct comedi_subdevice *s;
  337. unsigned int size;
  338. int err;
  339. err = kstrtouint(buf, 10, &size);
  340. if (err)
  341. return err;
  342. if (size > (UINT_MAX / 1024))
  343. return -EINVAL;
  344. size *= 1024;
  345. dev = comedi_dev_get_from_minor(minor);
  346. if (!dev)
  347. return -ENODEV;
  348. mutex_lock(&dev->mutex);
  349. s = comedi_read_subdevice(dev, minor);
  350. if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
  351. s->async->max_bufsize = size;
  352. else
  353. err = -EINVAL;
  354. mutex_unlock(&dev->mutex);
  355. comedi_dev_put(dev);
  356. return err ? err : count;
  357. }
  358. static DEVICE_ATTR_RW(max_read_buffer_kb);
  359. static ssize_t read_buffer_kb_show(struct device *csdev,
  360. struct device_attribute *attr, char *buf)
  361. {
  362. unsigned int minor = MINOR(csdev->devt);
  363. struct comedi_device *dev;
  364. struct comedi_subdevice *s;
  365. unsigned int size = 0;
  366. dev = comedi_dev_get_from_minor(minor);
  367. if (!dev)
  368. return -ENODEV;
  369. mutex_lock(&dev->mutex);
  370. s = comedi_read_subdevice(dev, minor);
  371. if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
  372. size = s->async->prealloc_bufsz / 1024;
  373. mutex_unlock(&dev->mutex);
  374. comedi_dev_put(dev);
  375. return sysfs_emit(buf, "%u\n", size);
  376. }
  377. static ssize_t read_buffer_kb_store(struct device *csdev,
  378. struct device_attribute *attr,
  379. const char *buf, size_t count)
  380. {
  381. unsigned int minor = MINOR(csdev->devt);
  382. struct comedi_device *dev;
  383. struct comedi_subdevice *s;
  384. unsigned int size;
  385. int err;
  386. err = kstrtouint(buf, 10, &size);
  387. if (err)
  388. return err;
  389. if (size > (UINT_MAX / 1024))
  390. return -EINVAL;
  391. size *= 1024;
  392. dev = comedi_dev_get_from_minor(minor);
  393. if (!dev)
  394. return -ENODEV;
  395. mutex_lock(&dev->mutex);
  396. s = comedi_read_subdevice(dev, minor);
  397. if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
  398. err = resize_async_buffer(dev, s, size);
  399. else
  400. err = -EINVAL;
  401. mutex_unlock(&dev->mutex);
  402. comedi_dev_put(dev);
  403. return err ? err : count;
  404. }
  405. static DEVICE_ATTR_RW(read_buffer_kb);
  406. static ssize_t max_write_buffer_kb_show(struct device *csdev,
  407. struct device_attribute *attr,
  408. char *buf)
  409. {
  410. unsigned int minor = MINOR(csdev->devt);
  411. struct comedi_device *dev;
  412. struct comedi_subdevice *s;
  413. unsigned int size = 0;
  414. dev = comedi_dev_get_from_minor(minor);
  415. if (!dev)
  416. return -ENODEV;
  417. mutex_lock(&dev->mutex);
  418. s = comedi_write_subdevice(dev, minor);
  419. if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
  420. size = s->async->max_bufsize / 1024;
  421. mutex_unlock(&dev->mutex);
  422. comedi_dev_put(dev);
  423. return sysfs_emit(buf, "%u\n", size);
  424. }
  425. static ssize_t max_write_buffer_kb_store(struct device *csdev,
  426. struct device_attribute *attr,
  427. const char *buf, size_t count)
  428. {
  429. unsigned int minor = MINOR(csdev->devt);
  430. struct comedi_device *dev;
  431. struct comedi_subdevice *s;
  432. unsigned int size;
  433. int err;
  434. err = kstrtouint(buf, 10, &size);
  435. if (err)
  436. return err;
  437. if (size > (UINT_MAX / 1024))
  438. return -EINVAL;
  439. size *= 1024;
  440. dev = comedi_dev_get_from_minor(minor);
  441. if (!dev)
  442. return -ENODEV;
  443. mutex_lock(&dev->mutex);
  444. s = comedi_write_subdevice(dev, minor);
  445. if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
  446. s->async->max_bufsize = size;
  447. else
  448. err = -EINVAL;
  449. mutex_unlock(&dev->mutex);
  450. comedi_dev_put(dev);
  451. return err ? err : count;
  452. }
  453. static DEVICE_ATTR_RW(max_write_buffer_kb);
  454. static ssize_t write_buffer_kb_show(struct device *csdev,
  455. struct device_attribute *attr, char *buf)
  456. {
  457. unsigned int minor = MINOR(csdev->devt);
  458. struct comedi_device *dev;
  459. struct comedi_subdevice *s;
  460. unsigned int size = 0;
  461. dev = comedi_dev_get_from_minor(minor);
  462. if (!dev)
  463. return -ENODEV;
  464. mutex_lock(&dev->mutex);
  465. s = comedi_write_subdevice(dev, minor);
  466. if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
  467. size = s->async->prealloc_bufsz / 1024;
  468. mutex_unlock(&dev->mutex);
  469. comedi_dev_put(dev);
  470. return sysfs_emit(buf, "%u\n", size);
  471. }
  472. static ssize_t write_buffer_kb_store(struct device *csdev,
  473. struct device_attribute *attr,
  474. const char *buf, size_t count)
  475. {
  476. unsigned int minor = MINOR(csdev->devt);
  477. struct comedi_device *dev;
  478. struct comedi_subdevice *s;
  479. unsigned int size;
  480. int err;
  481. err = kstrtouint(buf, 10, &size);
  482. if (err)
  483. return err;
  484. if (size > (UINT_MAX / 1024))
  485. return -EINVAL;
  486. size *= 1024;
  487. dev = comedi_dev_get_from_minor(minor);
  488. if (!dev)
  489. return -ENODEV;
  490. mutex_lock(&dev->mutex);
  491. s = comedi_write_subdevice(dev, minor);
  492. if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
  493. err = resize_async_buffer(dev, s, size);
  494. else
  495. err = -EINVAL;
  496. mutex_unlock(&dev->mutex);
  497. comedi_dev_put(dev);
  498. return err ? err : count;
  499. }
  500. static DEVICE_ATTR_RW(write_buffer_kb);
  501. static struct attribute *comedi_dev_attrs[] = {
  502. &dev_attr_max_read_buffer_kb.attr,
  503. &dev_attr_read_buffer_kb.attr,
  504. &dev_attr_max_write_buffer_kb.attr,
  505. &dev_attr_write_buffer_kb.attr,
  506. NULL,
  507. };
  508. ATTRIBUTE_GROUPS(comedi_dev);
  509. static const struct class comedi_class = {
  510. .name = "comedi",
  511. .dev_groups = comedi_dev_groups,
  512. };
  513. static void comedi_free_board_dev(struct comedi_device *dev)
  514. {
  515. if (dev) {
  516. comedi_device_cleanup(dev);
  517. if (dev->class_dev) {
  518. device_destroy(&comedi_class,
  519. MKDEV(COMEDI_MAJOR, dev->minor));
  520. }
  521. comedi_dev_put(dev);
  522. }
  523. }
  524. static void __comedi_clear_subdevice_runflags(struct comedi_subdevice *s,
  525. unsigned int bits)
  526. {
  527. s->runflags &= ~bits;
  528. }
  529. static void __comedi_set_subdevice_runflags(struct comedi_subdevice *s,
  530. unsigned int bits)
  531. {
  532. s->runflags |= bits;
  533. }
  534. static void comedi_update_subdevice_runflags(struct comedi_subdevice *s,
  535. unsigned int mask,
  536. unsigned int bits)
  537. {
  538. unsigned long flags;
  539. spin_lock_irqsave(&s->spin_lock, flags);
  540. __comedi_clear_subdevice_runflags(s, mask);
  541. __comedi_set_subdevice_runflags(s, bits & mask);
  542. spin_unlock_irqrestore(&s->spin_lock, flags);
  543. }
  544. static unsigned int __comedi_get_subdevice_runflags(struct comedi_subdevice *s)
  545. {
  546. return s->runflags;
  547. }
  548. static unsigned int comedi_get_subdevice_runflags(struct comedi_subdevice *s)
  549. {
  550. unsigned long flags;
  551. unsigned int runflags;
  552. spin_lock_irqsave(&s->spin_lock, flags);
  553. runflags = __comedi_get_subdevice_runflags(s);
  554. spin_unlock_irqrestore(&s->spin_lock, flags);
  555. return runflags;
  556. }
  557. static bool comedi_is_runflags_running(unsigned int runflags)
  558. {
  559. return runflags & COMEDI_SRF_RUNNING;
  560. }
  561. static bool comedi_is_runflags_in_error(unsigned int runflags)
  562. {
  563. return runflags & COMEDI_SRF_ERROR;
  564. }
  565. /**
  566. * comedi_is_subdevice_running() - Check if async command running on subdevice
  567. * @s: COMEDI subdevice.
  568. *
  569. * Return: %true if an asynchronous COMEDI command is active on the
  570. * subdevice, else %false.
  571. */
  572. bool comedi_is_subdevice_running(struct comedi_subdevice *s)
  573. {
  574. unsigned int runflags = comedi_get_subdevice_runflags(s);
  575. return comedi_is_runflags_running(runflags);
  576. }
  577. EXPORT_SYMBOL_GPL(comedi_is_subdevice_running);
  578. static bool __comedi_is_subdevice_running(struct comedi_subdevice *s)
  579. {
  580. unsigned int runflags = __comedi_get_subdevice_runflags(s);
  581. return comedi_is_runflags_running(runflags);
  582. }
  583. bool comedi_can_auto_free_spriv(struct comedi_subdevice *s)
  584. {
  585. unsigned int runflags = __comedi_get_subdevice_runflags(s);
  586. return runflags & COMEDI_SRF_FREE_SPRIV;
  587. }
  588. /**
  589. * comedi_set_spriv_auto_free() - Mark subdevice private data as freeable
  590. * @s: COMEDI subdevice.
  591. *
  592. * Mark the subdevice as having a pointer to private data that can be
  593. * automatically freed when the COMEDI device is detached from the low-level
  594. * driver.
  595. */
  596. void comedi_set_spriv_auto_free(struct comedi_subdevice *s)
  597. {
  598. __comedi_set_subdevice_runflags(s, COMEDI_SRF_FREE_SPRIV);
  599. }
  600. EXPORT_SYMBOL_GPL(comedi_set_spriv_auto_free);
  601. /**
  602. * comedi_alloc_spriv - Allocate memory for the subdevice private data
  603. * @s: COMEDI subdevice.
  604. * @size: Size of the memory to allocate.
  605. *
  606. * Allocate memory for the subdevice private data and point @s->private
  607. * to it. The memory will be freed automatically when the COMEDI device
  608. * is detached from the low-level driver.
  609. *
  610. * Return: A pointer to the allocated memory @s->private on success.
  611. * Return NULL on failure.
  612. */
  613. void *comedi_alloc_spriv(struct comedi_subdevice *s, size_t size)
  614. {
  615. s->private = kzalloc(size, GFP_KERNEL);
  616. if (s->private)
  617. comedi_set_spriv_auto_free(s);
  618. return s->private;
  619. }
  620. EXPORT_SYMBOL_GPL(comedi_alloc_spriv);
  621. /*
  622. * This function restores a subdevice to an idle state.
  623. */
  624. static void do_become_nonbusy(struct comedi_device *dev,
  625. struct comedi_subdevice *s)
  626. {
  627. struct comedi_async *async = s->async;
  628. lockdep_assert_held(&dev->mutex);
  629. comedi_update_subdevice_runflags(s, COMEDI_SRF_RUNNING, 0);
  630. if (async) {
  631. comedi_buf_reset(s);
  632. async->inttrig = NULL;
  633. kfree(async->cmd.chanlist);
  634. async->cmd.chanlist = NULL;
  635. s->busy = NULL;
  636. wake_up_interruptible_all(&async->wait_head);
  637. } else {
  638. dev_err(dev->class_dev,
  639. "BUG: (?) %s called with async=NULL\n", __func__);
  640. s->busy = NULL;
  641. }
  642. }
  643. static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
  644. {
  645. int ret = 0;
  646. lockdep_assert_held(&dev->mutex);
  647. if (comedi_is_subdevice_running(s) && s->cancel)
  648. ret = s->cancel(dev, s);
  649. do_become_nonbusy(dev, s);
  650. return ret;
  651. }
  652. void comedi_device_cancel_all(struct comedi_device *dev)
  653. {
  654. struct comedi_subdevice *s;
  655. int i;
  656. lockdep_assert_held(&dev->mutex);
  657. if (!dev->attached)
  658. return;
  659. for (i = 0; i < dev->n_subdevices; i++) {
  660. s = &dev->subdevices[i];
  661. if (s->async)
  662. do_cancel(dev, s);
  663. }
  664. }
  665. static int is_device_busy(struct comedi_device *dev)
  666. {
  667. struct comedi_subdevice *s;
  668. int i;
  669. lockdep_assert_held_write(&dev->attach_lock);
  670. lockdep_assert_held(&dev->mutex);
  671. if (!dev->attached)
  672. return 0;
  673. for (i = 0; i < dev->n_subdevices; i++) {
  674. s = &dev->subdevices[i];
  675. if (s->busy)
  676. return 1;
  677. if (!s->async)
  678. continue;
  679. if (comedi_buf_is_mmapped(s))
  680. return 1;
  681. /*
  682. * There may be tasks still waiting on the subdevice's wait
  683. * queue, although they should already be about to be removed
  684. * from it since the subdevice has no active async command.
  685. */
  686. if (wq_has_sleeper(&s->async->wait_head))
  687. return 1;
  688. }
  689. return 0;
  690. }
  691. /*
  692. * COMEDI_DEVCONFIG ioctl
  693. * attaches (and configures) or detaches a legacy device
  694. *
  695. * arg:
  696. * pointer to comedi_devconfig structure (NULL if detaching)
  697. *
  698. * reads:
  699. * comedi_devconfig structure (if attaching)
  700. *
  701. * writes:
  702. * nothing
  703. */
  704. static int do_devconfig_ioctl(struct comedi_device *dev,
  705. struct comedi_devconfig __user *arg)
  706. {
  707. struct comedi_devconfig it;
  708. lockdep_assert_held(&dev->mutex);
  709. if (!capable(CAP_SYS_ADMIN))
  710. return -EPERM;
  711. if (!arg) {
  712. int rc = 0;
  713. if (dev->attached) {
  714. down_write(&dev->attach_lock);
  715. if (is_device_busy(dev)) {
  716. rc = -EBUSY;
  717. } else {
  718. struct module *driver_module =
  719. dev->driver->module;
  720. comedi_device_detach_locked(dev);
  721. module_put(driver_module);
  722. }
  723. up_write(&dev->attach_lock);
  724. }
  725. return rc;
  726. }
  727. if (copy_from_user(&it, arg, sizeof(it)))
  728. return -EFAULT;
  729. it.board_name[COMEDI_NAMELEN - 1] = 0;
  730. if (it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
  731. dev_warn(dev->class_dev,
  732. "comedi_config --init_data is deprecated\n");
  733. return -EINVAL;
  734. }
  735. if (dev->minor >= comedi_num_legacy_minors)
  736. /* don't re-use dynamically allocated comedi devices */
  737. return -EBUSY;
  738. /* This increments the driver module count on success. */
  739. return comedi_device_attach(dev, &it);
  740. }
  741. /*
  742. * COMEDI_BUFCONFIG ioctl
  743. * buffer configuration
  744. *
  745. * arg:
  746. * pointer to comedi_bufconfig structure
  747. *
  748. * reads:
  749. * comedi_bufconfig structure
  750. *
  751. * writes:
  752. * modified comedi_bufconfig structure
  753. */
  754. static int do_bufconfig_ioctl(struct comedi_device *dev,
  755. struct comedi_bufconfig __user *arg)
  756. {
  757. struct comedi_bufconfig bc;
  758. struct comedi_async *async;
  759. struct comedi_subdevice *s;
  760. int retval = 0;
  761. lockdep_assert_held(&dev->mutex);
  762. if (copy_from_user(&bc, arg, sizeof(bc)))
  763. return -EFAULT;
  764. if (bc.subdevice >= dev->n_subdevices)
  765. return -EINVAL;
  766. s = &dev->subdevices[bc.subdevice];
  767. async = s->async;
  768. if (!async) {
  769. dev_dbg(dev->class_dev,
  770. "subdevice does not have async capability\n");
  771. bc.size = 0;
  772. bc.maximum_size = 0;
  773. goto copyback;
  774. }
  775. if (bc.maximum_size) {
  776. if (!capable(CAP_SYS_ADMIN))
  777. return -EPERM;
  778. async->max_bufsize = bc.maximum_size;
  779. }
  780. if (bc.size) {
  781. retval = resize_async_buffer(dev, s, bc.size);
  782. if (retval < 0)
  783. return retval;
  784. }
  785. bc.size = async->prealloc_bufsz;
  786. bc.maximum_size = async->max_bufsize;
  787. copyback:
  788. if (copy_to_user(arg, &bc, sizeof(bc)))
  789. return -EFAULT;
  790. return 0;
  791. }
  792. /*
  793. * COMEDI_DEVINFO ioctl
  794. * device info
  795. *
  796. * arg:
  797. * pointer to comedi_devinfo structure
  798. *
  799. * reads:
  800. * nothing
  801. *
  802. * writes:
  803. * comedi_devinfo structure
  804. */
  805. static int do_devinfo_ioctl(struct comedi_device *dev,
  806. struct comedi_devinfo __user *arg,
  807. struct file *file)
  808. {
  809. struct comedi_subdevice *s;
  810. struct comedi_devinfo devinfo;
  811. lockdep_assert_held(&dev->mutex);
  812. memset(&devinfo, 0, sizeof(devinfo));
  813. /* fill devinfo structure */
  814. devinfo.version_code = COMEDI_VERSION_CODE;
  815. devinfo.n_subdevs = dev->n_subdevices;
  816. strscpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
  817. strscpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
  818. s = comedi_file_read_subdevice(file);
  819. if (s)
  820. devinfo.read_subdevice = s->index;
  821. else
  822. devinfo.read_subdevice = -1;
  823. s = comedi_file_write_subdevice(file);
  824. if (s)
  825. devinfo.write_subdevice = s->index;
  826. else
  827. devinfo.write_subdevice = -1;
  828. if (copy_to_user(arg, &devinfo, sizeof(devinfo)))
  829. return -EFAULT;
  830. return 0;
  831. }
  832. /*
  833. * COMEDI_SUBDINFO ioctl
  834. * subdevices info
  835. *
  836. * arg:
  837. * pointer to array of comedi_subdinfo structures
  838. *
  839. * reads:
  840. * nothing
  841. *
  842. * writes:
  843. * array of comedi_subdinfo structures
  844. */
  845. static int do_subdinfo_ioctl(struct comedi_device *dev,
  846. struct comedi_subdinfo __user *arg, void *file)
  847. {
  848. int ret, i;
  849. struct comedi_subdinfo *tmp, *us;
  850. struct comedi_subdevice *s;
  851. lockdep_assert_held(&dev->mutex);
  852. tmp = kcalloc(dev->n_subdevices, sizeof(*tmp), GFP_KERNEL);
  853. if (!tmp)
  854. return -ENOMEM;
  855. /* fill subdinfo structs */
  856. for (i = 0; i < dev->n_subdevices; i++) {
  857. s = &dev->subdevices[i];
  858. us = tmp + i;
  859. us->type = s->type;
  860. us->n_chan = s->n_chan;
  861. us->subd_flags = s->subdev_flags;
  862. if (comedi_is_subdevice_running(s))
  863. us->subd_flags |= SDF_RUNNING;
  864. #define TIMER_nanosec 5 /* backwards compatibility */
  865. us->timer_type = TIMER_nanosec;
  866. us->len_chanlist = s->len_chanlist;
  867. us->maxdata = s->maxdata;
  868. if (s->range_table) {
  869. us->range_type =
  870. (i << 24) | (0 << 16) | (s->range_table->length);
  871. } else {
  872. us->range_type = 0; /* XXX */
  873. }
  874. if (s->busy)
  875. us->subd_flags |= SDF_BUSY;
  876. if (s->busy == file)
  877. us->subd_flags |= SDF_BUSY_OWNER;
  878. if (s->lock)
  879. us->subd_flags |= SDF_LOCKED;
  880. if (s->lock == file)
  881. us->subd_flags |= SDF_LOCK_OWNER;
  882. if (!s->maxdata && s->maxdata_list)
  883. us->subd_flags |= SDF_MAXDATA;
  884. if (s->range_table_list)
  885. us->subd_flags |= SDF_RANGETYPE;
  886. if (s->do_cmd)
  887. us->subd_flags |= SDF_CMD;
  888. if (s->insn_bits != &insn_inval)
  889. us->insn_bits_support = COMEDI_SUPPORTED;
  890. else
  891. us->insn_bits_support = COMEDI_UNSUPPORTED;
  892. }
  893. ret = copy_to_user(arg, tmp, dev->n_subdevices * sizeof(*tmp));
  894. kfree(tmp);
  895. return ret ? -EFAULT : 0;
  896. }
  897. /*
  898. * COMEDI_CHANINFO ioctl
  899. * subdevice channel info
  900. *
  901. * arg:
  902. * pointer to comedi_chaninfo structure
  903. *
  904. * reads:
  905. * comedi_chaninfo structure
  906. *
  907. * writes:
  908. * array of maxdata values to chaninfo->maxdata_list if requested
  909. * array of range table lengths to chaninfo->range_table_list if requested
  910. */
  911. static int do_chaninfo_ioctl(struct comedi_device *dev,
  912. struct comedi_chaninfo *it)
  913. {
  914. struct comedi_subdevice *s;
  915. lockdep_assert_held(&dev->mutex);
  916. if (it->subdev >= dev->n_subdevices)
  917. return -EINVAL;
  918. s = &dev->subdevices[it->subdev];
  919. if (it->maxdata_list) {
  920. if (s->maxdata || !s->maxdata_list)
  921. return -EINVAL;
  922. if (copy_to_user(it->maxdata_list, s->maxdata_list,
  923. s->n_chan * sizeof(unsigned int)))
  924. return -EFAULT;
  925. }
  926. if (it->flaglist)
  927. return -EINVAL; /* flaglist not supported */
  928. if (it->rangelist) {
  929. int i;
  930. if (!s->range_table_list)
  931. return -EINVAL;
  932. for (i = 0; i < s->n_chan; i++) {
  933. int x;
  934. x = (dev->minor << 28) | (it->subdev << 24) | (i << 16) |
  935. (s->range_table_list[i]->length);
  936. if (put_user(x, it->rangelist + i))
  937. return -EFAULT;
  938. }
  939. }
  940. return 0;
  941. }
  942. /*
  943. * COMEDI_BUFINFO ioctl
  944. * buffer information
  945. *
  946. * arg:
  947. * pointer to comedi_bufinfo structure
  948. *
  949. * reads:
  950. * comedi_bufinfo structure
  951. *
  952. * writes:
  953. * modified comedi_bufinfo structure
  954. */
  955. static int do_bufinfo_ioctl(struct comedi_device *dev,
  956. struct comedi_bufinfo __user *arg, void *file)
  957. {
  958. struct comedi_bufinfo bi;
  959. struct comedi_subdevice *s;
  960. struct comedi_async *async;
  961. unsigned int runflags;
  962. int retval = 0;
  963. bool become_nonbusy = false;
  964. lockdep_assert_held(&dev->mutex);
  965. if (copy_from_user(&bi, arg, sizeof(bi)))
  966. return -EFAULT;
  967. if (bi.subdevice >= dev->n_subdevices)
  968. return -EINVAL;
  969. s = &dev->subdevices[bi.subdevice];
  970. async = s->async;
  971. if (!async || s->busy != file)
  972. return -EINVAL;
  973. runflags = comedi_get_subdevice_runflags(s);
  974. if (!(async->cmd.flags & CMDF_WRITE)) {
  975. /* command was set up in "read" direction */
  976. if (bi.bytes_read) {
  977. comedi_buf_read_alloc(s, bi.bytes_read);
  978. bi.bytes_read = comedi_buf_read_free(s, bi.bytes_read);
  979. }
  980. /*
  981. * If nothing left to read, and command has stopped, and
  982. * {"read" position not updated or command stopped normally},
  983. * then become non-busy.
  984. */
  985. if (comedi_buf_read_n_available(s) == 0 &&
  986. !comedi_is_runflags_running(runflags) &&
  987. (bi.bytes_read == 0 ||
  988. !comedi_is_runflags_in_error(runflags))) {
  989. become_nonbusy = true;
  990. if (comedi_is_runflags_in_error(runflags))
  991. retval = -EPIPE;
  992. }
  993. bi.bytes_written = 0;
  994. } else {
  995. /* command was set up in "write" direction */
  996. if (!comedi_is_runflags_running(runflags)) {
  997. bi.bytes_written = 0;
  998. become_nonbusy = true;
  999. if (comedi_is_runflags_in_error(runflags))
  1000. retval = -EPIPE;
  1001. } else if (bi.bytes_written) {
  1002. comedi_buf_write_alloc(s, bi.bytes_written);
  1003. bi.bytes_written =
  1004. comedi_buf_write_free(s, bi.bytes_written);
  1005. }
  1006. bi.bytes_read = 0;
  1007. }
  1008. bi.buf_write_count = async->buf_write_count;
  1009. bi.buf_write_ptr = async->buf_write_ptr;
  1010. bi.buf_read_count = async->buf_read_count;
  1011. bi.buf_read_ptr = async->buf_read_ptr;
  1012. if (become_nonbusy)
  1013. do_become_nonbusy(dev, s);
  1014. if (retval)
  1015. return retval;
  1016. if (copy_to_user(arg, &bi, sizeof(bi)))
  1017. return -EFAULT;
  1018. return 0;
  1019. }
  1020. static int check_insn_config_length(struct comedi_insn *insn,
  1021. unsigned int *data)
  1022. {
  1023. if (insn->n < 1)
  1024. return -EINVAL;
  1025. switch (data[0]) {
  1026. case INSN_CONFIG_DIO_OUTPUT:
  1027. case INSN_CONFIG_DIO_INPUT:
  1028. case INSN_CONFIG_DISARM:
  1029. case INSN_CONFIG_RESET:
  1030. if (insn->n == 1)
  1031. return 0;
  1032. break;
  1033. case INSN_CONFIG_ARM:
  1034. case INSN_CONFIG_DIO_QUERY:
  1035. case INSN_CONFIG_BLOCK_SIZE:
  1036. case INSN_CONFIG_FILTER:
  1037. case INSN_CONFIG_SERIAL_CLOCK:
  1038. case INSN_CONFIG_BIDIRECTIONAL_DATA:
  1039. case INSN_CONFIG_ALT_SOURCE:
  1040. case INSN_CONFIG_SET_COUNTER_MODE:
  1041. case INSN_CONFIG_8254_READ_STATUS:
  1042. case INSN_CONFIG_SET_ROUTING:
  1043. case INSN_CONFIG_GET_ROUTING:
  1044. case INSN_CONFIG_GET_PWM_STATUS:
  1045. case INSN_CONFIG_PWM_SET_PERIOD:
  1046. case INSN_CONFIG_PWM_GET_PERIOD:
  1047. if (insn->n == 2)
  1048. return 0;
  1049. break;
  1050. case INSN_CONFIG_SET_GATE_SRC:
  1051. case INSN_CONFIG_GET_GATE_SRC:
  1052. case INSN_CONFIG_SET_CLOCK_SRC:
  1053. case INSN_CONFIG_GET_CLOCK_SRC:
  1054. case INSN_CONFIG_SET_OTHER_SRC:
  1055. case INSN_CONFIG_GET_COUNTER_STATUS:
  1056. case INSN_CONFIG_GET_PWM_OUTPUT:
  1057. case INSN_CONFIG_PWM_SET_H_BRIDGE:
  1058. case INSN_CONFIG_PWM_GET_H_BRIDGE:
  1059. case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
  1060. if (insn->n == 3)
  1061. return 0;
  1062. break;
  1063. case INSN_CONFIG_PWM_OUTPUT:
  1064. case INSN_CONFIG_ANALOG_TRIG:
  1065. case INSN_CONFIG_TIMER_1:
  1066. if (insn->n == 5)
  1067. return 0;
  1068. break;
  1069. case INSN_CONFIG_DIGITAL_TRIG:
  1070. if (insn->n == 6)
  1071. return 0;
  1072. break;
  1073. case INSN_CONFIG_GET_CMD_TIMING_CONSTRAINTS:
  1074. if (insn->n >= 4)
  1075. return 0;
  1076. break;
  1077. /*
  1078. * by default we allow the insn since we don't have checks for
  1079. * all possible cases yet
  1080. */
  1081. default:
  1082. pr_warn("No check for data length of config insn id %i is implemented\n",
  1083. data[0]);
  1084. pr_warn("Add a check to %s in %s\n", __func__, __FILE__);
  1085. pr_warn("Assuming n=%i is correct\n", insn->n);
  1086. return 0;
  1087. }
  1088. return -EINVAL;
  1089. }
  1090. static int check_insn_device_config_length(struct comedi_insn *insn,
  1091. unsigned int *data)
  1092. {
  1093. if (insn->n < 1)
  1094. return -EINVAL;
  1095. switch (data[0]) {
  1096. case INSN_DEVICE_CONFIG_TEST_ROUTE:
  1097. case INSN_DEVICE_CONFIG_CONNECT_ROUTE:
  1098. case INSN_DEVICE_CONFIG_DISCONNECT_ROUTE:
  1099. if (insn->n == 3)
  1100. return 0;
  1101. break;
  1102. case INSN_DEVICE_CONFIG_GET_ROUTES:
  1103. /*
  1104. * Big enough for config_id and the length of the userland
  1105. * memory buffer. Additional length should be in factors of 2
  1106. * to communicate any returned route pairs (source,destination).
  1107. */
  1108. if (insn->n >= 2)
  1109. return 0;
  1110. break;
  1111. }
  1112. return -EINVAL;
  1113. }
  1114. /**
  1115. * get_valid_routes() - Calls low-level driver get_valid_routes function to
  1116. * either return a count of valid routes to user, or copy
  1117. * of list of all valid device routes to buffer in
  1118. * userspace.
  1119. * @dev: comedi device pointer
  1120. * @data: data from user insn call. The length of the data must be >= 2.
  1121. * data[0] must contain the INSN_DEVICE_CONFIG config_id.
  1122. * data[1](input) contains the number of _pairs_ for which memory is
  1123. * allotted from the user. If the user specifies '0', then only
  1124. * the number of pairs available is returned.
  1125. * data[1](output) returns either the number of pairs available (if none
  1126. * where requested) or the number of _pairs_ that are copied back
  1127. * to the user.
  1128. * data[2::2] returns each (source, destination) pair.
  1129. *
  1130. * Return: -EINVAL if low-level driver does not allocate and return routes as
  1131. * expected. Returns 0 otherwise.
  1132. */
  1133. static int get_valid_routes(struct comedi_device *dev, unsigned int *data)
  1134. {
  1135. lockdep_assert_held(&dev->mutex);
  1136. data[1] = dev->get_valid_routes(dev, data[1], data + 2);
  1137. return 0;
  1138. }
  1139. static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
  1140. unsigned int *data, void *file)
  1141. {
  1142. struct comedi_subdevice *s;
  1143. int ret = 0;
  1144. int i;
  1145. lockdep_assert_held(&dev->mutex);
  1146. if (insn->insn & INSN_MASK_SPECIAL) {
  1147. /* a non-subdevice instruction */
  1148. switch (insn->insn) {
  1149. case INSN_GTOD:
  1150. {
  1151. struct timespec64 tv;
  1152. if (insn->n != 2) {
  1153. ret = -EINVAL;
  1154. break;
  1155. }
  1156. ktime_get_real_ts64(&tv);
  1157. /* unsigned data safe until 2106 */
  1158. data[0] = (unsigned int)tv.tv_sec;
  1159. data[1] = tv.tv_nsec / NSEC_PER_USEC;
  1160. ret = 2;
  1161. break;
  1162. }
  1163. case INSN_WAIT:
  1164. if (insn->n != 1 || data[0] >= 100000) {
  1165. ret = -EINVAL;
  1166. break;
  1167. }
  1168. udelay(data[0] / 1000);
  1169. ret = 1;
  1170. break;
  1171. case INSN_INTTRIG:
  1172. if (insn->n != 1) {
  1173. ret = -EINVAL;
  1174. break;
  1175. }
  1176. if (insn->subdev >= dev->n_subdevices) {
  1177. dev_dbg(dev->class_dev,
  1178. "%d not usable subdevice\n",
  1179. insn->subdev);
  1180. ret = -EINVAL;
  1181. break;
  1182. }
  1183. s = &dev->subdevices[insn->subdev];
  1184. if (!s->async) {
  1185. dev_dbg(dev->class_dev, "no async\n");
  1186. ret = -EINVAL;
  1187. break;
  1188. }
  1189. if (!s->async->inttrig) {
  1190. dev_dbg(dev->class_dev, "no inttrig\n");
  1191. ret = -EAGAIN;
  1192. break;
  1193. }
  1194. ret = s->async->inttrig(dev, s, data[0]);
  1195. if (ret >= 0)
  1196. ret = 1;
  1197. break;
  1198. case INSN_DEVICE_CONFIG:
  1199. ret = check_insn_device_config_length(insn, data);
  1200. if (ret)
  1201. break;
  1202. if (data[0] == INSN_DEVICE_CONFIG_GET_ROUTES) {
  1203. /*
  1204. * data[1] should be the number of _pairs_ that
  1205. * the memory can hold.
  1206. */
  1207. data[1] = (insn->n - 2) / 2;
  1208. ret = get_valid_routes(dev, data);
  1209. break;
  1210. }
  1211. /* other global device config instructions. */
  1212. ret = dev->insn_device_config(dev, insn, data);
  1213. break;
  1214. default:
  1215. dev_dbg(dev->class_dev, "invalid insn\n");
  1216. ret = -EINVAL;
  1217. break;
  1218. }
  1219. } else {
  1220. /* a subdevice instruction */
  1221. unsigned int maxdata;
  1222. if (insn->subdev >= dev->n_subdevices) {
  1223. dev_dbg(dev->class_dev, "subdevice %d out of range\n",
  1224. insn->subdev);
  1225. ret = -EINVAL;
  1226. goto out;
  1227. }
  1228. s = &dev->subdevices[insn->subdev];
  1229. if (s->type == COMEDI_SUBD_UNUSED) {
  1230. dev_dbg(dev->class_dev, "%d not usable subdevice\n",
  1231. insn->subdev);
  1232. ret = -EIO;
  1233. goto out;
  1234. }
  1235. /* are we locked? (ioctl lock) */
  1236. if (s->lock && s->lock != file) {
  1237. dev_dbg(dev->class_dev, "device locked\n");
  1238. ret = -EACCES;
  1239. goto out;
  1240. }
  1241. ret = comedi_check_chanlist(s, 1, &insn->chanspec);
  1242. if (ret < 0) {
  1243. ret = -EINVAL;
  1244. dev_dbg(dev->class_dev, "bad chanspec\n");
  1245. goto out;
  1246. }
  1247. if (s->busy) {
  1248. ret = -EBUSY;
  1249. goto out;
  1250. }
  1251. /* This looks arbitrary. It is. */
  1252. s->busy = parse_insn;
  1253. switch (insn->insn) {
  1254. case INSN_READ:
  1255. ret = s->insn_read(dev, s, insn, data);
  1256. if (ret == -ETIMEDOUT) {
  1257. dev_dbg(dev->class_dev,
  1258. "subdevice %d read instruction timed out\n",
  1259. s->index);
  1260. }
  1261. break;
  1262. case INSN_WRITE:
  1263. maxdata = s->maxdata_list
  1264. ? s->maxdata_list[CR_CHAN(insn->chanspec)]
  1265. : s->maxdata;
  1266. for (i = 0; i < insn->n; ++i) {
  1267. if (data[i] > maxdata) {
  1268. ret = -EINVAL;
  1269. dev_dbg(dev->class_dev,
  1270. "bad data value(s)\n");
  1271. break;
  1272. }
  1273. }
  1274. if (ret == 0) {
  1275. ret = s->insn_write(dev, s, insn, data);
  1276. if (ret == -ETIMEDOUT) {
  1277. dev_dbg(dev->class_dev,
  1278. "subdevice %d write instruction timed out\n",
  1279. s->index);
  1280. }
  1281. }
  1282. break;
  1283. case INSN_BITS:
  1284. if (insn->n != 2) {
  1285. ret = -EINVAL;
  1286. } else {
  1287. /*
  1288. * Most drivers ignore the base channel in
  1289. * insn->chanspec. Fix this here if
  1290. * the subdevice has <= 32 channels.
  1291. */
  1292. unsigned int orig_mask = data[0];
  1293. unsigned int shift = 0;
  1294. if (s->n_chan <= 32) {
  1295. shift = CR_CHAN(insn->chanspec);
  1296. if (shift > 0) {
  1297. insn->chanspec = 0;
  1298. data[0] <<= shift;
  1299. data[1] <<= shift;
  1300. }
  1301. }
  1302. ret = s->insn_bits(dev, s, insn, data);
  1303. data[0] = orig_mask;
  1304. if (shift > 0)
  1305. data[1] >>= shift;
  1306. }
  1307. break;
  1308. case INSN_CONFIG:
  1309. ret = check_insn_config_length(insn, data);
  1310. if (ret)
  1311. break;
  1312. ret = s->insn_config(dev, s, insn, data);
  1313. break;
  1314. default:
  1315. ret = -EINVAL;
  1316. break;
  1317. }
  1318. s->busy = NULL;
  1319. }
  1320. out:
  1321. return ret;
  1322. }
  1323. /*
  1324. * COMEDI_INSNLIST ioctl
  1325. * synchronous instruction list
  1326. *
  1327. * arg:
  1328. * pointer to comedi_insnlist structure
  1329. *
  1330. * reads:
  1331. * comedi_insnlist structure
  1332. * array of comedi_insn structures from insnlist->insns pointer
  1333. * data (for writes) from insns[].data pointers
  1334. *
  1335. * writes:
  1336. * data (for reads) to insns[].data pointers
  1337. */
  1338. /* arbitrary limits */
  1339. #define MIN_SAMPLES 16
  1340. #define MAX_SAMPLES 65536
  1341. static int do_insnlist_ioctl(struct comedi_device *dev,
  1342. struct comedi_insn *insns,
  1343. unsigned int n_insns,
  1344. void *file)
  1345. {
  1346. unsigned int *data = NULL;
  1347. unsigned int max_n_data_required = MIN_SAMPLES;
  1348. int i = 0;
  1349. int ret = 0;
  1350. lockdep_assert_held(&dev->mutex);
  1351. /* Determine maximum memory needed for all instructions. */
  1352. for (i = 0; i < n_insns; ++i) {
  1353. if (insns[i].n > MAX_SAMPLES) {
  1354. dev_dbg(dev->class_dev,
  1355. "number of samples too large\n");
  1356. ret = -EINVAL;
  1357. goto error;
  1358. }
  1359. max_n_data_required = max(max_n_data_required, insns[i].n);
  1360. }
  1361. /* Allocate scratch space for all instruction data. */
  1362. data = kmalloc_array(max_n_data_required, sizeof(unsigned int),
  1363. GFP_KERNEL);
  1364. if (!data) {
  1365. ret = -ENOMEM;
  1366. goto error;
  1367. }
  1368. for (i = 0; i < n_insns; ++i) {
  1369. unsigned int n = insns[i].n;
  1370. if (insns[i].insn & INSN_MASK_WRITE) {
  1371. if (copy_from_user(data, insns[i].data,
  1372. n * sizeof(unsigned int))) {
  1373. dev_dbg(dev->class_dev,
  1374. "copy_from_user failed\n");
  1375. ret = -EFAULT;
  1376. goto error;
  1377. }
  1378. if (n < MIN_SAMPLES) {
  1379. memset(&data[n], 0, (MIN_SAMPLES - n) *
  1380. sizeof(unsigned int));
  1381. }
  1382. } else {
  1383. memset(data, 0, max_t(unsigned int, n, MIN_SAMPLES) *
  1384. sizeof(unsigned int));
  1385. }
  1386. ret = parse_insn(dev, insns + i, data, file);
  1387. if (ret < 0)
  1388. goto error;
  1389. if (insns[i].insn & INSN_MASK_READ) {
  1390. if (copy_to_user(insns[i].data, data,
  1391. n * sizeof(unsigned int))) {
  1392. dev_dbg(dev->class_dev,
  1393. "copy_to_user failed\n");
  1394. ret = -EFAULT;
  1395. goto error;
  1396. }
  1397. }
  1398. if (need_resched())
  1399. schedule();
  1400. }
  1401. error:
  1402. kfree(data);
  1403. if (ret < 0)
  1404. return ret;
  1405. return i;
  1406. }
  1407. #define MAX_INSNS MAX_SAMPLES
  1408. static int check_insnlist_len(struct comedi_device *dev, unsigned int n_insns)
  1409. {
  1410. if (n_insns > MAX_INSNS) {
  1411. dev_dbg(dev->class_dev, "insnlist length too large\n");
  1412. return -EINVAL;
  1413. }
  1414. return 0;
  1415. }
  1416. /*
  1417. * COMEDI_INSN ioctl
  1418. * synchronous instruction
  1419. *
  1420. * arg:
  1421. * pointer to comedi_insn structure
  1422. *
  1423. * reads:
  1424. * comedi_insn structure
  1425. * data (for writes) from insn->data pointer
  1426. *
  1427. * writes:
  1428. * data (for reads) to insn->data pointer
  1429. */
  1430. static int do_insn_ioctl(struct comedi_device *dev,
  1431. struct comedi_insn *insn, void *file)
  1432. {
  1433. unsigned int *data = NULL;
  1434. unsigned int n_data = MIN_SAMPLES;
  1435. int ret = 0;
  1436. lockdep_assert_held(&dev->mutex);
  1437. n_data = max(n_data, insn->n);
  1438. /* This is where the behavior of insn and insnlist deviate. */
  1439. if (insn->n > MAX_SAMPLES) {
  1440. insn->n = MAX_SAMPLES;
  1441. n_data = MAX_SAMPLES;
  1442. }
  1443. data = kmalloc_array(n_data, sizeof(unsigned int), GFP_KERNEL);
  1444. if (!data) {
  1445. ret = -ENOMEM;
  1446. goto error;
  1447. }
  1448. if (insn->insn & INSN_MASK_WRITE) {
  1449. if (copy_from_user(data,
  1450. insn->data,
  1451. insn->n * sizeof(unsigned int))) {
  1452. ret = -EFAULT;
  1453. goto error;
  1454. }
  1455. if (insn->n < MIN_SAMPLES) {
  1456. memset(&data[insn->n], 0,
  1457. (MIN_SAMPLES - insn->n) * sizeof(unsigned int));
  1458. }
  1459. } else {
  1460. memset(data, 0, n_data * sizeof(unsigned int));
  1461. }
  1462. ret = parse_insn(dev, insn, data, file);
  1463. if (ret < 0)
  1464. goto error;
  1465. if (insn->insn & INSN_MASK_READ) {
  1466. if (copy_to_user(insn->data,
  1467. data,
  1468. insn->n * sizeof(unsigned int))) {
  1469. ret = -EFAULT;
  1470. goto error;
  1471. }
  1472. }
  1473. ret = insn->n;
  1474. error:
  1475. kfree(data);
  1476. return ret;
  1477. }
  1478. static int __comedi_get_user_cmd(struct comedi_device *dev,
  1479. struct comedi_cmd *cmd)
  1480. {
  1481. struct comedi_subdevice *s;
  1482. lockdep_assert_held(&dev->mutex);
  1483. if (cmd->subdev >= dev->n_subdevices) {
  1484. dev_dbg(dev->class_dev, "%d no such subdevice\n", cmd->subdev);
  1485. return -ENODEV;
  1486. }
  1487. s = &dev->subdevices[cmd->subdev];
  1488. if (s->type == COMEDI_SUBD_UNUSED) {
  1489. dev_dbg(dev->class_dev, "%d not valid subdevice\n",
  1490. cmd->subdev);
  1491. return -EIO;
  1492. }
  1493. if (!s->do_cmd || !s->do_cmdtest || !s->async) {
  1494. dev_dbg(dev->class_dev,
  1495. "subdevice %d does not support commands\n",
  1496. cmd->subdev);
  1497. return -EIO;
  1498. }
  1499. /* make sure channel/gain list isn't too long */
  1500. if (cmd->chanlist_len > s->len_chanlist) {
  1501. dev_dbg(dev->class_dev, "channel/gain list too long %d > %d\n",
  1502. cmd->chanlist_len, s->len_chanlist);
  1503. return -EINVAL;
  1504. }
  1505. /*
  1506. * Set the CMDF_WRITE flag to the correct state if the subdevice
  1507. * supports only "read" commands or only "write" commands.
  1508. */
  1509. switch (s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) {
  1510. case SDF_CMD_READ:
  1511. cmd->flags &= ~CMDF_WRITE;
  1512. break;
  1513. case SDF_CMD_WRITE:
  1514. cmd->flags |= CMDF_WRITE;
  1515. break;
  1516. default:
  1517. break;
  1518. }
  1519. return 0;
  1520. }
  1521. static int __comedi_get_user_chanlist(struct comedi_device *dev,
  1522. struct comedi_subdevice *s,
  1523. unsigned int __user *user_chanlist,
  1524. struct comedi_cmd *cmd)
  1525. {
  1526. unsigned int *chanlist;
  1527. int ret;
  1528. lockdep_assert_held(&dev->mutex);
  1529. cmd->chanlist = NULL;
  1530. chanlist = memdup_array_user(user_chanlist,
  1531. cmd->chanlist_len, sizeof(unsigned int));
  1532. if (IS_ERR(chanlist))
  1533. return PTR_ERR(chanlist);
  1534. /* make sure each element in channel/gain list is valid */
  1535. ret = comedi_check_chanlist(s, cmd->chanlist_len, chanlist);
  1536. if (ret < 0) {
  1537. kfree(chanlist);
  1538. return ret;
  1539. }
  1540. cmd->chanlist = chanlist;
  1541. return 0;
  1542. }
  1543. /*
  1544. * COMEDI_CMD ioctl
  1545. * asynchronous acquisition command set-up
  1546. *
  1547. * arg:
  1548. * pointer to comedi_cmd structure
  1549. *
  1550. * reads:
  1551. * comedi_cmd structure
  1552. * channel/range list from cmd->chanlist pointer
  1553. *
  1554. * writes:
  1555. * possibly modified comedi_cmd structure (when -EAGAIN returned)
  1556. */
  1557. static int do_cmd_ioctl(struct comedi_device *dev,
  1558. struct comedi_cmd *cmd, bool *copy, void *file)
  1559. {
  1560. struct comedi_subdevice *s;
  1561. struct comedi_async *async;
  1562. unsigned int __user *user_chanlist;
  1563. int ret;
  1564. lockdep_assert_held(&dev->mutex);
  1565. /* do some simple cmd validation */
  1566. ret = __comedi_get_user_cmd(dev, cmd);
  1567. if (ret)
  1568. return ret;
  1569. /* save user's chanlist pointer so it can be restored later */
  1570. user_chanlist = (unsigned int __user *)cmd->chanlist;
  1571. s = &dev->subdevices[cmd->subdev];
  1572. async = s->async;
  1573. /* are we locked? (ioctl lock) */
  1574. if (s->lock && s->lock != file) {
  1575. dev_dbg(dev->class_dev, "subdevice locked\n");
  1576. return -EACCES;
  1577. }
  1578. /* are we busy? */
  1579. if (s->busy) {
  1580. dev_dbg(dev->class_dev, "subdevice busy\n");
  1581. return -EBUSY;
  1582. }
  1583. /* make sure channel/gain list isn't too short */
  1584. if (cmd->chanlist_len < 1) {
  1585. dev_dbg(dev->class_dev, "channel/gain list too short %u < 1\n",
  1586. cmd->chanlist_len);
  1587. return -EINVAL;
  1588. }
  1589. async->cmd = *cmd;
  1590. async->cmd.data = NULL;
  1591. /* load channel/gain list */
  1592. ret = __comedi_get_user_chanlist(dev, s, user_chanlist, &async->cmd);
  1593. if (ret)
  1594. goto cleanup;
  1595. ret = s->do_cmdtest(dev, s, &async->cmd);
  1596. if (async->cmd.flags & CMDF_BOGUS || ret) {
  1597. dev_dbg(dev->class_dev, "test returned %d\n", ret);
  1598. *cmd = async->cmd;
  1599. /* restore chanlist pointer before copying back */
  1600. cmd->chanlist = (unsigned int __force *)user_chanlist;
  1601. cmd->data = NULL;
  1602. *copy = true;
  1603. ret = -EAGAIN;
  1604. goto cleanup;
  1605. }
  1606. if (!async->prealloc_bufsz) {
  1607. ret = -ENOMEM;
  1608. dev_dbg(dev->class_dev, "no buffer (?)\n");
  1609. goto cleanup;
  1610. }
  1611. comedi_buf_reset(s);
  1612. async->cb_mask = COMEDI_CB_BLOCK | COMEDI_CB_CANCEL_MASK;
  1613. if (async->cmd.flags & CMDF_WAKE_EOS)
  1614. async->cb_mask |= COMEDI_CB_EOS;
  1615. comedi_update_subdevice_runflags(s, COMEDI_SRF_BUSY_MASK,
  1616. COMEDI_SRF_RUNNING);
  1617. /*
  1618. * Set s->busy _after_ setting COMEDI_SRF_RUNNING flag to avoid
  1619. * race with comedi_read() or comedi_write().
  1620. */
  1621. s->busy = file;
  1622. ret = s->do_cmd(dev, s);
  1623. if (ret == 0)
  1624. return 0;
  1625. cleanup:
  1626. do_become_nonbusy(dev, s);
  1627. return ret;
  1628. }
  1629. /*
  1630. * COMEDI_CMDTEST ioctl
  1631. * asynchronous acquisition command testing
  1632. *
  1633. * arg:
  1634. * pointer to comedi_cmd structure
  1635. *
  1636. * reads:
  1637. * comedi_cmd structure
  1638. * channel/range list from cmd->chanlist pointer
  1639. *
  1640. * writes:
  1641. * possibly modified comedi_cmd structure
  1642. */
  1643. static int do_cmdtest_ioctl(struct comedi_device *dev,
  1644. struct comedi_cmd *cmd, bool *copy, void *file)
  1645. {
  1646. struct comedi_subdevice *s;
  1647. unsigned int __user *user_chanlist;
  1648. int ret;
  1649. lockdep_assert_held(&dev->mutex);
  1650. /* do some simple cmd validation */
  1651. ret = __comedi_get_user_cmd(dev, cmd);
  1652. if (ret)
  1653. return ret;
  1654. /* save user's chanlist pointer so it can be restored later */
  1655. user_chanlist = (unsigned int __user *)cmd->chanlist;
  1656. s = &dev->subdevices[cmd->subdev];
  1657. /* user_chanlist can be NULL for COMEDI_CMDTEST ioctl */
  1658. if (user_chanlist) {
  1659. /* load channel/gain list */
  1660. ret = __comedi_get_user_chanlist(dev, s, user_chanlist, cmd);
  1661. if (ret)
  1662. return ret;
  1663. }
  1664. ret = s->do_cmdtest(dev, s, cmd);
  1665. kfree(cmd->chanlist); /* free kernel copy of user chanlist */
  1666. /* restore chanlist pointer before copying back */
  1667. cmd->chanlist = (unsigned int __force *)user_chanlist;
  1668. *copy = true;
  1669. return ret;
  1670. }
  1671. /*
  1672. * COMEDI_LOCK ioctl
  1673. * lock subdevice
  1674. *
  1675. * arg:
  1676. * subdevice number
  1677. *
  1678. * reads:
  1679. * nothing
  1680. *
  1681. * writes:
  1682. * nothing
  1683. */
  1684. static int do_lock_ioctl(struct comedi_device *dev, unsigned long arg,
  1685. void *file)
  1686. {
  1687. int ret = 0;
  1688. unsigned long flags;
  1689. struct comedi_subdevice *s;
  1690. lockdep_assert_held(&dev->mutex);
  1691. if (arg >= dev->n_subdevices)
  1692. return -EINVAL;
  1693. s = &dev->subdevices[arg];
  1694. spin_lock_irqsave(&s->spin_lock, flags);
  1695. if (s->busy || s->lock)
  1696. ret = -EBUSY;
  1697. else
  1698. s->lock = file;
  1699. spin_unlock_irqrestore(&s->spin_lock, flags);
  1700. return ret;
  1701. }
  1702. /*
  1703. * COMEDI_UNLOCK ioctl
  1704. * unlock subdevice
  1705. *
  1706. * arg:
  1707. * subdevice number
  1708. *
  1709. * reads:
  1710. * nothing
  1711. *
  1712. * writes:
  1713. * nothing
  1714. */
  1715. static int do_unlock_ioctl(struct comedi_device *dev, unsigned long arg,
  1716. void *file)
  1717. {
  1718. struct comedi_subdevice *s;
  1719. lockdep_assert_held(&dev->mutex);
  1720. if (arg >= dev->n_subdevices)
  1721. return -EINVAL;
  1722. s = &dev->subdevices[arg];
  1723. if (s->busy)
  1724. return -EBUSY;
  1725. if (s->lock && s->lock != file)
  1726. return -EACCES;
  1727. if (s->lock == file)
  1728. s->lock = NULL;
  1729. return 0;
  1730. }
  1731. /*
  1732. * COMEDI_CANCEL ioctl
  1733. * cancel asynchronous acquisition
  1734. *
  1735. * arg:
  1736. * subdevice number
  1737. *
  1738. * reads:
  1739. * nothing
  1740. *
  1741. * writes:
  1742. * nothing
  1743. */
  1744. static int do_cancel_ioctl(struct comedi_device *dev, unsigned long arg,
  1745. void *file)
  1746. {
  1747. struct comedi_subdevice *s;
  1748. lockdep_assert_held(&dev->mutex);
  1749. if (arg >= dev->n_subdevices)
  1750. return -EINVAL;
  1751. s = &dev->subdevices[arg];
  1752. if (!s->async)
  1753. return -EINVAL;
  1754. if (!s->busy)
  1755. return 0;
  1756. if (s->busy != file)
  1757. return -EBUSY;
  1758. return do_cancel(dev, s);
  1759. }
  1760. /*
  1761. * COMEDI_POLL ioctl
  1762. * instructs driver to synchronize buffers
  1763. *
  1764. * arg:
  1765. * subdevice number
  1766. *
  1767. * reads:
  1768. * nothing
  1769. *
  1770. * writes:
  1771. * nothing
  1772. */
  1773. static int do_poll_ioctl(struct comedi_device *dev, unsigned long arg,
  1774. void *file)
  1775. {
  1776. struct comedi_subdevice *s;
  1777. lockdep_assert_held(&dev->mutex);
  1778. if (arg >= dev->n_subdevices)
  1779. return -EINVAL;
  1780. s = &dev->subdevices[arg];
  1781. if (!s->busy)
  1782. return 0;
  1783. if (s->busy != file)
  1784. return -EBUSY;
  1785. if (s->poll)
  1786. return s->poll(dev, s);
  1787. return -EINVAL;
  1788. }
  1789. /*
  1790. * COMEDI_SETRSUBD ioctl
  1791. * sets the current "read" subdevice on a per-file basis
  1792. *
  1793. * arg:
  1794. * subdevice number
  1795. *
  1796. * reads:
  1797. * nothing
  1798. *
  1799. * writes:
  1800. * nothing
  1801. */
  1802. static int do_setrsubd_ioctl(struct comedi_device *dev, unsigned long arg,
  1803. struct file *file)
  1804. {
  1805. struct comedi_file *cfp = file->private_data;
  1806. struct comedi_subdevice *s_old, *s_new;
  1807. lockdep_assert_held(&dev->mutex);
  1808. if (arg >= dev->n_subdevices)
  1809. return -EINVAL;
  1810. s_new = &dev->subdevices[arg];
  1811. s_old = comedi_file_read_subdevice(file);
  1812. if (s_old == s_new)
  1813. return 0; /* no change */
  1814. if (!(s_new->subdev_flags & SDF_CMD_READ))
  1815. return -EINVAL;
  1816. /*
  1817. * Check the file isn't still busy handling a "read" command on the
  1818. * old subdevice (if any).
  1819. */
  1820. if (s_old && s_old->busy == file && s_old->async &&
  1821. !(s_old->async->cmd.flags & CMDF_WRITE))
  1822. return -EBUSY;
  1823. WRITE_ONCE(cfp->read_subdev, s_new);
  1824. return 0;
  1825. }
  1826. /*
  1827. * COMEDI_SETWSUBD ioctl
  1828. * sets the current "write" subdevice on a per-file basis
  1829. *
  1830. * arg:
  1831. * subdevice number
  1832. *
  1833. * reads:
  1834. * nothing
  1835. *
  1836. * writes:
  1837. * nothing
  1838. */
  1839. static int do_setwsubd_ioctl(struct comedi_device *dev, unsigned long arg,
  1840. struct file *file)
  1841. {
  1842. struct comedi_file *cfp = file->private_data;
  1843. struct comedi_subdevice *s_old, *s_new;
  1844. lockdep_assert_held(&dev->mutex);
  1845. if (arg >= dev->n_subdevices)
  1846. return -EINVAL;
  1847. s_new = &dev->subdevices[arg];
  1848. s_old = comedi_file_write_subdevice(file);
  1849. if (s_old == s_new)
  1850. return 0; /* no change */
  1851. if (!(s_new->subdev_flags & SDF_CMD_WRITE))
  1852. return -EINVAL;
  1853. /*
  1854. * Check the file isn't still busy handling a "write" command on the
  1855. * old subdevice (if any).
  1856. */
  1857. if (s_old && s_old->busy == file && s_old->async &&
  1858. (s_old->async->cmd.flags & CMDF_WRITE))
  1859. return -EBUSY;
  1860. WRITE_ONCE(cfp->write_subdev, s_new);
  1861. return 0;
  1862. }
  1863. static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
  1864. unsigned long arg)
  1865. {
  1866. unsigned int minor = iminor(file_inode(file));
  1867. struct comedi_file *cfp = file->private_data;
  1868. struct comedi_device *dev = cfp->dev;
  1869. int rc;
  1870. mutex_lock(&dev->mutex);
  1871. /*
  1872. * Device config is special, because it must work on
  1873. * an unconfigured device.
  1874. */
  1875. if (cmd == COMEDI_DEVCONFIG) {
  1876. if (minor >= COMEDI_NUM_BOARD_MINORS) {
  1877. /* Device config not appropriate on non-board minors. */
  1878. rc = -ENOTTY;
  1879. goto done;
  1880. }
  1881. rc = do_devconfig_ioctl(dev,
  1882. (struct comedi_devconfig __user *)arg);
  1883. if (rc == 0) {
  1884. if (arg == 0 &&
  1885. dev->minor >= comedi_num_legacy_minors) {
  1886. /*
  1887. * Successfully unconfigured a dynamically
  1888. * allocated device. Try and remove it.
  1889. */
  1890. if (comedi_clear_board_dev(dev)) {
  1891. mutex_unlock(&dev->mutex);
  1892. comedi_free_board_dev(dev);
  1893. return rc;
  1894. }
  1895. }
  1896. }
  1897. goto done;
  1898. }
  1899. if (!dev->attached) {
  1900. dev_dbg(dev->class_dev, "no driver attached\n");
  1901. rc = -ENODEV;
  1902. goto done;
  1903. }
  1904. switch (cmd) {
  1905. case COMEDI_BUFCONFIG:
  1906. rc = do_bufconfig_ioctl(dev,
  1907. (struct comedi_bufconfig __user *)arg);
  1908. break;
  1909. case COMEDI_DEVINFO:
  1910. rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
  1911. file);
  1912. break;
  1913. case COMEDI_SUBDINFO:
  1914. rc = do_subdinfo_ioctl(dev,
  1915. (struct comedi_subdinfo __user *)arg,
  1916. file);
  1917. break;
  1918. case COMEDI_CHANINFO: {
  1919. struct comedi_chaninfo it;
  1920. if (copy_from_user(&it, (void __user *)arg, sizeof(it)))
  1921. rc = -EFAULT;
  1922. else
  1923. rc = do_chaninfo_ioctl(dev, &it);
  1924. break;
  1925. }
  1926. case COMEDI_RANGEINFO: {
  1927. struct comedi_rangeinfo it;
  1928. if (copy_from_user(&it, (void __user *)arg, sizeof(it)))
  1929. rc = -EFAULT;
  1930. else
  1931. rc = do_rangeinfo_ioctl(dev, &it);
  1932. break;
  1933. }
  1934. case COMEDI_BUFINFO:
  1935. rc = do_bufinfo_ioctl(dev,
  1936. (struct comedi_bufinfo __user *)arg,
  1937. file);
  1938. break;
  1939. case COMEDI_LOCK:
  1940. rc = do_lock_ioctl(dev, arg, file);
  1941. break;
  1942. case COMEDI_UNLOCK:
  1943. rc = do_unlock_ioctl(dev, arg, file);
  1944. break;
  1945. case COMEDI_CANCEL:
  1946. rc = do_cancel_ioctl(dev, arg, file);
  1947. break;
  1948. case COMEDI_CMD: {
  1949. struct comedi_cmd cmd;
  1950. bool copy = false;
  1951. if (copy_from_user(&cmd, (void __user *)arg, sizeof(cmd))) {
  1952. rc = -EFAULT;
  1953. break;
  1954. }
  1955. rc = do_cmd_ioctl(dev, &cmd, &copy, file);
  1956. if (copy && copy_to_user((void __user *)arg, &cmd, sizeof(cmd)))
  1957. rc = -EFAULT;
  1958. break;
  1959. }
  1960. case COMEDI_CMDTEST: {
  1961. struct comedi_cmd cmd;
  1962. bool copy = false;
  1963. if (copy_from_user(&cmd, (void __user *)arg, sizeof(cmd))) {
  1964. rc = -EFAULT;
  1965. break;
  1966. }
  1967. rc = do_cmdtest_ioctl(dev, &cmd, &copy, file);
  1968. if (copy && copy_to_user((void __user *)arg, &cmd, sizeof(cmd)))
  1969. rc = -EFAULT;
  1970. break;
  1971. }
  1972. case COMEDI_INSNLIST: {
  1973. struct comedi_insnlist insnlist;
  1974. struct comedi_insn *insns = NULL;
  1975. if (copy_from_user(&insnlist, (void __user *)arg,
  1976. sizeof(insnlist))) {
  1977. rc = -EFAULT;
  1978. break;
  1979. }
  1980. rc = check_insnlist_len(dev, insnlist.n_insns);
  1981. if (rc)
  1982. break;
  1983. insns = kcalloc(insnlist.n_insns, sizeof(*insns), GFP_KERNEL);
  1984. if (!insns) {
  1985. rc = -ENOMEM;
  1986. break;
  1987. }
  1988. if (copy_from_user(insns, insnlist.insns,
  1989. sizeof(*insns) * insnlist.n_insns)) {
  1990. rc = -EFAULT;
  1991. kfree(insns);
  1992. break;
  1993. }
  1994. rc = do_insnlist_ioctl(dev, insns, insnlist.n_insns, file);
  1995. kfree(insns);
  1996. break;
  1997. }
  1998. case COMEDI_INSN: {
  1999. struct comedi_insn insn;
  2000. if (copy_from_user(&insn, (void __user *)arg, sizeof(insn)))
  2001. rc = -EFAULT;
  2002. else
  2003. rc = do_insn_ioctl(dev, &insn, file);
  2004. break;
  2005. }
  2006. case COMEDI_POLL:
  2007. rc = do_poll_ioctl(dev, arg, file);
  2008. break;
  2009. case COMEDI_SETRSUBD:
  2010. rc = do_setrsubd_ioctl(dev, arg, file);
  2011. break;
  2012. case COMEDI_SETWSUBD:
  2013. rc = do_setwsubd_ioctl(dev, arg, file);
  2014. break;
  2015. default:
  2016. rc = -ENOTTY;
  2017. break;
  2018. }
  2019. done:
  2020. mutex_unlock(&dev->mutex);
  2021. return rc;
  2022. }
  2023. static void comedi_vm_open(struct vm_area_struct *area)
  2024. {
  2025. struct comedi_buf_map *bm;
  2026. bm = area->vm_private_data;
  2027. comedi_buf_map_get(bm);
  2028. }
  2029. static void comedi_vm_close(struct vm_area_struct *area)
  2030. {
  2031. struct comedi_buf_map *bm;
  2032. bm = area->vm_private_data;
  2033. comedi_buf_map_put(bm);
  2034. }
  2035. static int comedi_vm_access(struct vm_area_struct *vma, unsigned long addr,
  2036. void *buf, int len, int write)
  2037. {
  2038. struct comedi_buf_map *bm = vma->vm_private_data;
  2039. unsigned long offset =
  2040. addr - vma->vm_start + (vma->vm_pgoff << PAGE_SHIFT);
  2041. if (len < 0)
  2042. return -EINVAL;
  2043. if (len > vma->vm_end - addr)
  2044. len = vma->vm_end - addr;
  2045. return comedi_buf_map_access(bm, offset, buf, len, write);
  2046. }
  2047. static const struct vm_operations_struct comedi_vm_ops = {
  2048. .open = comedi_vm_open,
  2049. .close = comedi_vm_close,
  2050. .access = comedi_vm_access,
  2051. };
  2052. static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
  2053. {
  2054. struct comedi_file *cfp = file->private_data;
  2055. struct comedi_device *dev = cfp->dev;
  2056. struct comedi_subdevice *s;
  2057. struct comedi_async *async;
  2058. struct comedi_buf_map *bm = NULL;
  2059. struct comedi_buf_page *buf;
  2060. unsigned long start = vma->vm_start;
  2061. unsigned long size;
  2062. int n_pages;
  2063. int i;
  2064. int retval = 0;
  2065. /*
  2066. * 'trylock' avoids circular dependency with current->mm->mmap_lock
  2067. * and down-reading &dev->attach_lock should normally succeed without
  2068. * contention unless the device is in the process of being attached
  2069. * or detached.
  2070. */
  2071. if (!down_read_trylock(&dev->attach_lock))
  2072. return -EAGAIN;
  2073. if (!dev->attached) {
  2074. dev_dbg(dev->class_dev, "no driver attached\n");
  2075. retval = -ENODEV;
  2076. goto done;
  2077. }
  2078. if (vma->vm_flags & VM_WRITE)
  2079. s = comedi_file_write_subdevice(file);
  2080. else
  2081. s = comedi_file_read_subdevice(file);
  2082. if (!s) {
  2083. retval = -EINVAL;
  2084. goto done;
  2085. }
  2086. async = s->async;
  2087. if (!async) {
  2088. retval = -EINVAL;
  2089. goto done;
  2090. }
  2091. if (vma->vm_pgoff != 0) {
  2092. dev_dbg(dev->class_dev, "mmap() offset must be 0.\n");
  2093. retval = -EINVAL;
  2094. goto done;
  2095. }
  2096. size = vma->vm_end - vma->vm_start;
  2097. if (size > async->prealloc_bufsz) {
  2098. retval = -EFAULT;
  2099. goto done;
  2100. }
  2101. if (offset_in_page(size)) {
  2102. retval = -EFAULT;
  2103. goto done;
  2104. }
  2105. n_pages = vma_pages(vma);
  2106. /* get reference to current buf map (if any) */
  2107. bm = comedi_buf_map_from_subdev_get(s);
  2108. if (!bm || n_pages > bm->n_pages) {
  2109. retval = -EINVAL;
  2110. goto done;
  2111. }
  2112. if (bm->dma_dir != DMA_NONE) {
  2113. /*
  2114. * DMA buffer was allocated as a single block.
  2115. * Address is in page_list[0].
  2116. */
  2117. buf = &bm->page_list[0];
  2118. retval = dma_mmap_coherent(bm->dma_hw_dev, vma, buf->virt_addr,
  2119. buf->dma_addr, n_pages * PAGE_SIZE);
  2120. } else {
  2121. for (i = 0; i < n_pages; ++i) {
  2122. unsigned long pfn;
  2123. buf = &bm->page_list[i];
  2124. pfn = page_to_pfn(virt_to_page(buf->virt_addr));
  2125. retval = remap_pfn_range(vma, start, pfn, PAGE_SIZE,
  2126. PAGE_SHARED);
  2127. if (retval)
  2128. break;
  2129. start += PAGE_SIZE;
  2130. }
  2131. #ifdef CONFIG_MMU
  2132. /*
  2133. * Leaving behind a partial mapping of a buffer we're about to
  2134. * drop is unsafe, see remap_pfn_range_notrack().
  2135. * We need to zap the range here ourselves instead of relying
  2136. * on the automatic zapping in remap_pfn_range() because we call
  2137. * remap_pfn_range() in a loop.
  2138. */
  2139. if (retval)
  2140. zap_vma_ptes(vma, vma->vm_start, size);
  2141. #endif
  2142. }
  2143. if (retval == 0) {
  2144. vma->vm_ops = &comedi_vm_ops;
  2145. vma->vm_private_data = bm;
  2146. vma->vm_ops->open(vma);
  2147. }
  2148. done:
  2149. up_read(&dev->attach_lock);
  2150. comedi_buf_map_put(bm); /* put reference to buf map - okay if NULL */
  2151. return retval;
  2152. }
  2153. static __poll_t comedi_poll(struct file *file, poll_table *wait)
  2154. {
  2155. __poll_t mask = 0;
  2156. struct comedi_file *cfp = file->private_data;
  2157. struct comedi_device *dev = cfp->dev;
  2158. struct comedi_subdevice *s, *s_read;
  2159. down_read(&dev->attach_lock);
  2160. if (!dev->attached) {
  2161. dev_dbg(dev->class_dev, "no driver attached\n");
  2162. goto done;
  2163. }
  2164. s = comedi_file_read_subdevice(file);
  2165. s_read = s;
  2166. if (s && s->async) {
  2167. poll_wait(file, &s->async->wait_head, wait);
  2168. if (s->busy != file || !comedi_is_subdevice_running(s) ||
  2169. (s->async->cmd.flags & CMDF_WRITE) ||
  2170. comedi_buf_read_n_available(s) > 0)
  2171. mask |= EPOLLIN | EPOLLRDNORM;
  2172. }
  2173. s = comedi_file_write_subdevice(file);
  2174. if (s && s->async) {
  2175. unsigned int bps = comedi_bytes_per_sample(s);
  2176. if (s != s_read)
  2177. poll_wait(file, &s->async->wait_head, wait);
  2178. if (s->busy != file || !comedi_is_subdevice_running(s) ||
  2179. !(s->async->cmd.flags & CMDF_WRITE) ||
  2180. comedi_buf_write_n_available(s) >= bps)
  2181. mask |= EPOLLOUT | EPOLLWRNORM;
  2182. }
  2183. done:
  2184. up_read(&dev->attach_lock);
  2185. return mask;
  2186. }
  2187. static ssize_t comedi_write(struct file *file, const char __user *buf,
  2188. size_t nbytes, loff_t *offset)
  2189. {
  2190. struct comedi_subdevice *s;
  2191. struct comedi_async *async;
  2192. unsigned int n, m;
  2193. ssize_t count = 0;
  2194. int retval = 0;
  2195. DECLARE_WAITQUEUE(wait, current);
  2196. struct comedi_file *cfp = file->private_data;
  2197. struct comedi_device *dev = cfp->dev;
  2198. bool become_nonbusy = false;
  2199. bool attach_locked;
  2200. unsigned int old_detach_count;
  2201. /* Protect against device detachment during operation. */
  2202. down_read(&dev->attach_lock);
  2203. attach_locked = true;
  2204. old_detach_count = dev->detach_count;
  2205. if (!dev->attached) {
  2206. dev_dbg(dev->class_dev, "no driver attached\n");
  2207. retval = -ENODEV;
  2208. goto out;
  2209. }
  2210. s = comedi_file_write_subdevice(file);
  2211. if (!s || !s->async) {
  2212. retval = -EIO;
  2213. goto out;
  2214. }
  2215. async = s->async;
  2216. if (s->busy != file || !(async->cmd.flags & CMDF_WRITE)) {
  2217. retval = -EINVAL;
  2218. goto out;
  2219. }
  2220. add_wait_queue(&async->wait_head, &wait);
  2221. while (count == 0 && !retval) {
  2222. unsigned int runflags;
  2223. unsigned int wp, n1, n2;
  2224. set_current_state(TASK_INTERRUPTIBLE);
  2225. runflags = comedi_get_subdevice_runflags(s);
  2226. if (!comedi_is_runflags_running(runflags)) {
  2227. if (comedi_is_runflags_in_error(runflags))
  2228. retval = -EPIPE;
  2229. if (retval || nbytes)
  2230. become_nonbusy = true;
  2231. break;
  2232. }
  2233. if (nbytes == 0)
  2234. break;
  2235. /* Allocate all free buffer space. */
  2236. comedi_buf_write_alloc(s, async->prealloc_bufsz);
  2237. m = comedi_buf_write_n_allocated(s);
  2238. n = min_t(size_t, m, nbytes);
  2239. if (n == 0) {
  2240. if (file->f_flags & O_NONBLOCK) {
  2241. retval = -EAGAIN;
  2242. break;
  2243. }
  2244. schedule();
  2245. if (signal_pending(current)) {
  2246. retval = -ERESTARTSYS;
  2247. break;
  2248. }
  2249. if (s->busy != file ||
  2250. !(async->cmd.flags & CMDF_WRITE)) {
  2251. retval = -EINVAL;
  2252. break;
  2253. }
  2254. continue;
  2255. }
  2256. set_current_state(TASK_RUNNING);
  2257. wp = async->buf_write_ptr;
  2258. n1 = min(n, async->prealloc_bufsz - wp);
  2259. n2 = n - n1;
  2260. m = copy_from_user(async->prealloc_buf + wp, buf, n1);
  2261. if (m)
  2262. m += n2;
  2263. else if (n2)
  2264. m = copy_from_user(async->prealloc_buf, buf + n1, n2);
  2265. if (m) {
  2266. n -= m;
  2267. retval = -EFAULT;
  2268. }
  2269. comedi_buf_write_free(s, n);
  2270. count += n;
  2271. nbytes -= n;
  2272. buf += n;
  2273. }
  2274. remove_wait_queue(&async->wait_head, &wait);
  2275. set_current_state(TASK_RUNNING);
  2276. if (become_nonbusy && count == 0) {
  2277. struct comedi_subdevice *new_s;
  2278. /*
  2279. * To avoid deadlock, cannot acquire dev->mutex
  2280. * while dev->attach_lock is held.
  2281. */
  2282. up_read(&dev->attach_lock);
  2283. attach_locked = false;
  2284. mutex_lock(&dev->mutex);
  2285. /*
  2286. * Check device hasn't become detached behind our back.
  2287. * Checking dev->detach_count is unchanged ought to be
  2288. * sufficient (unless there have been 2**32 detaches in the
  2289. * meantime!), but check the subdevice pointer as well just in
  2290. * case.
  2291. *
  2292. * Also check the subdevice is still in a suitable state to
  2293. * become non-busy in case it changed behind our back.
  2294. */
  2295. new_s = comedi_file_write_subdevice(file);
  2296. if (dev->attached && old_detach_count == dev->detach_count &&
  2297. s == new_s && new_s->async == async && s->busy == file &&
  2298. (async->cmd.flags & CMDF_WRITE) &&
  2299. !comedi_is_subdevice_running(s))
  2300. do_become_nonbusy(dev, s);
  2301. mutex_unlock(&dev->mutex);
  2302. }
  2303. out:
  2304. if (attach_locked)
  2305. up_read(&dev->attach_lock);
  2306. return count ? count : retval;
  2307. }
  2308. static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
  2309. loff_t *offset)
  2310. {
  2311. struct comedi_subdevice *s;
  2312. struct comedi_async *async;
  2313. unsigned int n, m;
  2314. ssize_t count = 0;
  2315. int retval = 0;
  2316. DECLARE_WAITQUEUE(wait, current);
  2317. struct comedi_file *cfp = file->private_data;
  2318. struct comedi_device *dev = cfp->dev;
  2319. unsigned int old_detach_count;
  2320. bool become_nonbusy = false;
  2321. bool attach_locked;
  2322. /* Protect against device detachment during operation. */
  2323. down_read(&dev->attach_lock);
  2324. attach_locked = true;
  2325. old_detach_count = dev->detach_count;
  2326. if (!dev->attached) {
  2327. dev_dbg(dev->class_dev, "no driver attached\n");
  2328. retval = -ENODEV;
  2329. goto out;
  2330. }
  2331. s = comedi_file_read_subdevice(file);
  2332. if (!s || !s->async) {
  2333. retval = -EIO;
  2334. goto out;
  2335. }
  2336. async = s->async;
  2337. if (s->busy != file || (async->cmd.flags & CMDF_WRITE)) {
  2338. retval = -EINVAL;
  2339. goto out;
  2340. }
  2341. add_wait_queue(&async->wait_head, &wait);
  2342. while (count == 0 && !retval) {
  2343. unsigned int rp, n1, n2;
  2344. set_current_state(TASK_INTERRUPTIBLE);
  2345. m = comedi_buf_read_n_available(s);
  2346. n = min_t(size_t, m, nbytes);
  2347. if (n == 0) {
  2348. unsigned int runflags =
  2349. comedi_get_subdevice_runflags(s);
  2350. if (!comedi_is_runflags_running(runflags)) {
  2351. if (comedi_is_runflags_in_error(runflags))
  2352. retval = -EPIPE;
  2353. if (retval || nbytes)
  2354. become_nonbusy = true;
  2355. break;
  2356. }
  2357. if (nbytes == 0)
  2358. break;
  2359. if (file->f_flags & O_NONBLOCK) {
  2360. retval = -EAGAIN;
  2361. break;
  2362. }
  2363. schedule();
  2364. if (signal_pending(current)) {
  2365. retval = -ERESTARTSYS;
  2366. break;
  2367. }
  2368. if (s->busy != file ||
  2369. (async->cmd.flags & CMDF_WRITE)) {
  2370. retval = -EINVAL;
  2371. break;
  2372. }
  2373. continue;
  2374. }
  2375. set_current_state(TASK_RUNNING);
  2376. rp = async->buf_read_ptr;
  2377. n1 = min(n, async->prealloc_bufsz - rp);
  2378. n2 = n - n1;
  2379. m = copy_to_user(buf, async->prealloc_buf + rp, n1);
  2380. if (m)
  2381. m += n2;
  2382. else if (n2)
  2383. m = copy_to_user(buf + n1, async->prealloc_buf, n2);
  2384. if (m) {
  2385. n -= m;
  2386. retval = -EFAULT;
  2387. }
  2388. comedi_buf_read_alloc(s, n);
  2389. comedi_buf_read_free(s, n);
  2390. count += n;
  2391. nbytes -= n;
  2392. buf += n;
  2393. }
  2394. remove_wait_queue(&async->wait_head, &wait);
  2395. set_current_state(TASK_RUNNING);
  2396. if (become_nonbusy && count == 0) {
  2397. struct comedi_subdevice *new_s;
  2398. /*
  2399. * To avoid deadlock, cannot acquire dev->mutex
  2400. * while dev->attach_lock is held.
  2401. */
  2402. up_read(&dev->attach_lock);
  2403. attach_locked = false;
  2404. mutex_lock(&dev->mutex);
  2405. /*
  2406. * Check device hasn't become detached behind our back.
  2407. * Checking dev->detach_count is unchanged ought to be
  2408. * sufficient (unless there have been 2**32 detaches in the
  2409. * meantime!), but check the subdevice pointer as well just in
  2410. * case.
  2411. *
  2412. * Also check the subdevice is still in a suitable state to
  2413. * become non-busy in case it changed behind our back.
  2414. */
  2415. new_s = comedi_file_read_subdevice(file);
  2416. if (dev->attached && old_detach_count == dev->detach_count &&
  2417. s == new_s && new_s->async == async && s->busy == file &&
  2418. !(async->cmd.flags & CMDF_WRITE) &&
  2419. !comedi_is_subdevice_running(s) &&
  2420. comedi_buf_read_n_available(s) == 0)
  2421. do_become_nonbusy(dev, s);
  2422. mutex_unlock(&dev->mutex);
  2423. }
  2424. out:
  2425. if (attach_locked)
  2426. up_read(&dev->attach_lock);
  2427. return count ? count : retval;
  2428. }
  2429. static int comedi_open(struct inode *inode, struct file *file)
  2430. {
  2431. const unsigned int minor = iminor(inode);
  2432. struct comedi_file *cfp;
  2433. struct comedi_device *dev = comedi_dev_get_from_minor(minor);
  2434. int rc;
  2435. if (!dev) {
  2436. pr_debug("invalid minor number\n");
  2437. return -ENODEV;
  2438. }
  2439. cfp = kzalloc(sizeof(*cfp), GFP_KERNEL);
  2440. if (!cfp) {
  2441. comedi_dev_put(dev);
  2442. return -ENOMEM;
  2443. }
  2444. cfp->dev = dev;
  2445. mutex_lock(&dev->mutex);
  2446. if (!dev->attached && !capable(CAP_SYS_ADMIN)) {
  2447. dev_dbg(dev->class_dev, "not attached and not CAP_SYS_ADMIN\n");
  2448. rc = -ENODEV;
  2449. goto out;
  2450. }
  2451. if (dev->attached && dev->use_count == 0) {
  2452. if (!try_module_get(dev->driver->module)) {
  2453. rc = -ENXIO;
  2454. goto out;
  2455. }
  2456. if (dev->open) {
  2457. rc = dev->open(dev);
  2458. if (rc < 0) {
  2459. module_put(dev->driver->module);
  2460. goto out;
  2461. }
  2462. }
  2463. }
  2464. dev->use_count++;
  2465. file->private_data = cfp;
  2466. comedi_file_reset(file);
  2467. rc = 0;
  2468. out:
  2469. mutex_unlock(&dev->mutex);
  2470. if (rc) {
  2471. comedi_dev_put(dev);
  2472. kfree(cfp);
  2473. }
  2474. return rc;
  2475. }
  2476. static int comedi_fasync(int fd, struct file *file, int on)
  2477. {
  2478. struct comedi_file *cfp = file->private_data;
  2479. struct comedi_device *dev = cfp->dev;
  2480. return fasync_helper(fd, file, on, &dev->async_queue);
  2481. }
  2482. static int comedi_close(struct inode *inode, struct file *file)
  2483. {
  2484. struct comedi_file *cfp = file->private_data;
  2485. struct comedi_device *dev = cfp->dev;
  2486. struct comedi_subdevice *s = NULL;
  2487. int i;
  2488. mutex_lock(&dev->mutex);
  2489. if (dev->subdevices) {
  2490. for (i = 0; i < dev->n_subdevices; i++) {
  2491. s = &dev->subdevices[i];
  2492. if (s->busy == file)
  2493. do_cancel(dev, s);
  2494. if (s->lock == file)
  2495. s->lock = NULL;
  2496. }
  2497. }
  2498. if (dev->attached && dev->use_count == 1) {
  2499. if (dev->close)
  2500. dev->close(dev);
  2501. module_put(dev->driver->module);
  2502. }
  2503. dev->use_count--;
  2504. mutex_unlock(&dev->mutex);
  2505. comedi_dev_put(dev);
  2506. kfree(cfp);
  2507. return 0;
  2508. }
  2509. #ifdef CONFIG_COMPAT
  2510. #define COMEDI32_CHANINFO _IOR(CIO, 3, struct comedi32_chaninfo_struct)
  2511. #define COMEDI32_RANGEINFO _IOR(CIO, 8, struct comedi32_rangeinfo_struct)
  2512. /*
  2513. * N.B. COMEDI32_CMD and COMEDI_CMD ought to use _IOWR, not _IOR.
  2514. * It's too late to change it now, but it only affects the command number.
  2515. */
  2516. #define COMEDI32_CMD _IOR(CIO, 9, struct comedi32_cmd_struct)
  2517. /*
  2518. * N.B. COMEDI32_CMDTEST and COMEDI_CMDTEST ought to use _IOWR, not _IOR.
  2519. * It's too late to change it now, but it only affects the command number.
  2520. */
  2521. #define COMEDI32_CMDTEST _IOR(CIO, 10, struct comedi32_cmd_struct)
  2522. #define COMEDI32_INSNLIST _IOR(CIO, 11, struct comedi32_insnlist_struct)
  2523. #define COMEDI32_INSN _IOR(CIO, 12, struct comedi32_insn_struct)
  2524. struct comedi32_chaninfo_struct {
  2525. unsigned int subdev;
  2526. compat_uptr_t maxdata_list; /* 32-bit 'unsigned int *' */
  2527. compat_uptr_t flaglist; /* 32-bit 'unsigned int *' */
  2528. compat_uptr_t rangelist; /* 32-bit 'unsigned int *' */
  2529. unsigned int unused[4];
  2530. };
  2531. struct comedi32_rangeinfo_struct {
  2532. unsigned int range_type;
  2533. compat_uptr_t range_ptr; /* 32-bit 'void *' */
  2534. };
  2535. struct comedi32_cmd_struct {
  2536. unsigned int subdev;
  2537. unsigned int flags;
  2538. unsigned int start_src;
  2539. unsigned int start_arg;
  2540. unsigned int scan_begin_src;
  2541. unsigned int scan_begin_arg;
  2542. unsigned int convert_src;
  2543. unsigned int convert_arg;
  2544. unsigned int scan_end_src;
  2545. unsigned int scan_end_arg;
  2546. unsigned int stop_src;
  2547. unsigned int stop_arg;
  2548. compat_uptr_t chanlist; /* 32-bit 'unsigned int *' */
  2549. unsigned int chanlist_len;
  2550. compat_uptr_t data; /* 32-bit 'short *' */
  2551. unsigned int data_len;
  2552. };
  2553. struct comedi32_insn_struct {
  2554. unsigned int insn;
  2555. unsigned int n;
  2556. compat_uptr_t data; /* 32-bit 'unsigned int *' */
  2557. unsigned int subdev;
  2558. unsigned int chanspec;
  2559. unsigned int unused[3];
  2560. };
  2561. struct comedi32_insnlist_struct {
  2562. unsigned int n_insns;
  2563. compat_uptr_t insns; /* 32-bit 'struct comedi_insn *' */
  2564. };
  2565. /* Handle 32-bit COMEDI_CHANINFO ioctl. */
  2566. static int compat_chaninfo(struct file *file, unsigned long arg)
  2567. {
  2568. struct comedi_file *cfp = file->private_data;
  2569. struct comedi_device *dev = cfp->dev;
  2570. struct comedi32_chaninfo_struct chaninfo32;
  2571. struct comedi_chaninfo chaninfo;
  2572. int err;
  2573. if (copy_from_user(&chaninfo32, compat_ptr(arg), sizeof(chaninfo32)))
  2574. return -EFAULT;
  2575. memset(&chaninfo, 0, sizeof(chaninfo));
  2576. chaninfo.subdev = chaninfo32.subdev;
  2577. chaninfo.maxdata_list = compat_ptr(chaninfo32.maxdata_list);
  2578. chaninfo.flaglist = compat_ptr(chaninfo32.flaglist);
  2579. chaninfo.rangelist = compat_ptr(chaninfo32.rangelist);
  2580. mutex_lock(&dev->mutex);
  2581. err = do_chaninfo_ioctl(dev, &chaninfo);
  2582. mutex_unlock(&dev->mutex);
  2583. return err;
  2584. }
  2585. /* Handle 32-bit COMEDI_RANGEINFO ioctl. */
  2586. static int compat_rangeinfo(struct file *file, unsigned long arg)
  2587. {
  2588. struct comedi_file *cfp = file->private_data;
  2589. struct comedi_device *dev = cfp->dev;
  2590. struct comedi32_rangeinfo_struct rangeinfo32;
  2591. struct comedi_rangeinfo rangeinfo;
  2592. int err;
  2593. if (copy_from_user(&rangeinfo32, compat_ptr(arg), sizeof(rangeinfo32)))
  2594. return -EFAULT;
  2595. memset(&rangeinfo, 0, sizeof(rangeinfo));
  2596. rangeinfo.range_type = rangeinfo32.range_type;
  2597. rangeinfo.range_ptr = compat_ptr(rangeinfo32.range_ptr);
  2598. mutex_lock(&dev->mutex);
  2599. err = do_rangeinfo_ioctl(dev, &rangeinfo);
  2600. mutex_unlock(&dev->mutex);
  2601. return err;
  2602. }
  2603. /* Copy 32-bit cmd structure to native cmd structure. */
  2604. static int get_compat_cmd(struct comedi_cmd *cmd,
  2605. struct comedi32_cmd_struct __user *cmd32)
  2606. {
  2607. struct comedi32_cmd_struct v32;
  2608. if (copy_from_user(&v32, cmd32, sizeof(v32)))
  2609. return -EFAULT;
  2610. cmd->subdev = v32.subdev;
  2611. cmd->flags = v32.flags;
  2612. cmd->start_src = v32.start_src;
  2613. cmd->start_arg = v32.start_arg;
  2614. cmd->scan_begin_src = v32.scan_begin_src;
  2615. cmd->scan_begin_arg = v32.scan_begin_arg;
  2616. cmd->convert_src = v32.convert_src;
  2617. cmd->convert_arg = v32.convert_arg;
  2618. cmd->scan_end_src = v32.scan_end_src;
  2619. cmd->scan_end_arg = v32.scan_end_arg;
  2620. cmd->stop_src = v32.stop_src;
  2621. cmd->stop_arg = v32.stop_arg;
  2622. cmd->chanlist = (unsigned int __force *)compat_ptr(v32.chanlist);
  2623. cmd->chanlist_len = v32.chanlist_len;
  2624. cmd->data = compat_ptr(v32.data);
  2625. cmd->data_len = v32.data_len;
  2626. return 0;
  2627. }
  2628. /* Copy native cmd structure to 32-bit cmd structure. */
  2629. static int put_compat_cmd(struct comedi32_cmd_struct __user *cmd32,
  2630. struct comedi_cmd *cmd)
  2631. {
  2632. struct comedi32_cmd_struct v32;
  2633. memset(&v32, 0, sizeof(v32));
  2634. v32.subdev = cmd->subdev;
  2635. v32.flags = cmd->flags;
  2636. v32.start_src = cmd->start_src;
  2637. v32.start_arg = cmd->start_arg;
  2638. v32.scan_begin_src = cmd->scan_begin_src;
  2639. v32.scan_begin_arg = cmd->scan_begin_arg;
  2640. v32.convert_src = cmd->convert_src;
  2641. v32.convert_arg = cmd->convert_arg;
  2642. v32.scan_end_src = cmd->scan_end_src;
  2643. v32.scan_end_arg = cmd->scan_end_arg;
  2644. v32.stop_src = cmd->stop_src;
  2645. v32.stop_arg = cmd->stop_arg;
  2646. /* Assume chanlist pointer is unchanged. */
  2647. v32.chanlist = ptr_to_compat((unsigned int __user *)cmd->chanlist);
  2648. v32.chanlist_len = cmd->chanlist_len;
  2649. v32.data = ptr_to_compat(cmd->data);
  2650. v32.data_len = cmd->data_len;
  2651. if (copy_to_user(cmd32, &v32, sizeof(v32)))
  2652. return -EFAULT;
  2653. return 0;
  2654. }
  2655. /* Handle 32-bit COMEDI_CMD ioctl. */
  2656. static int compat_cmd(struct file *file, unsigned long arg)
  2657. {
  2658. struct comedi_file *cfp = file->private_data;
  2659. struct comedi_device *dev = cfp->dev;
  2660. struct comedi_cmd cmd;
  2661. bool copy = false;
  2662. int rc, err;
  2663. rc = get_compat_cmd(&cmd, compat_ptr(arg));
  2664. if (rc)
  2665. return rc;
  2666. mutex_lock(&dev->mutex);
  2667. rc = do_cmd_ioctl(dev, &cmd, &copy, file);
  2668. mutex_unlock(&dev->mutex);
  2669. if (copy) {
  2670. /* Special case: copy cmd back to user. */
  2671. err = put_compat_cmd(compat_ptr(arg), &cmd);
  2672. if (err)
  2673. rc = err;
  2674. }
  2675. return rc;
  2676. }
  2677. /* Handle 32-bit COMEDI_CMDTEST ioctl. */
  2678. static int compat_cmdtest(struct file *file, unsigned long arg)
  2679. {
  2680. struct comedi_file *cfp = file->private_data;
  2681. struct comedi_device *dev = cfp->dev;
  2682. struct comedi_cmd cmd;
  2683. bool copy = false;
  2684. int rc, err;
  2685. rc = get_compat_cmd(&cmd, compat_ptr(arg));
  2686. if (rc)
  2687. return rc;
  2688. mutex_lock(&dev->mutex);
  2689. rc = do_cmdtest_ioctl(dev, &cmd, &copy, file);
  2690. mutex_unlock(&dev->mutex);
  2691. if (copy) {
  2692. err = put_compat_cmd(compat_ptr(arg), &cmd);
  2693. if (err)
  2694. rc = err;
  2695. }
  2696. return rc;
  2697. }
  2698. /* Copy 32-bit insn structure to native insn structure. */
  2699. static int get_compat_insn(struct comedi_insn *insn,
  2700. struct comedi32_insn_struct __user *insn32)
  2701. {
  2702. struct comedi32_insn_struct v32;
  2703. /* Copy insn structure. Ignore the unused members. */
  2704. if (copy_from_user(&v32, insn32, sizeof(v32)))
  2705. return -EFAULT;
  2706. memset(insn, 0, sizeof(*insn));
  2707. insn->insn = v32.insn;
  2708. insn->n = v32.n;
  2709. insn->data = compat_ptr(v32.data);
  2710. insn->subdev = v32.subdev;
  2711. insn->chanspec = v32.chanspec;
  2712. return 0;
  2713. }
  2714. /* Handle 32-bit COMEDI_INSNLIST ioctl. */
  2715. static int compat_insnlist(struct file *file, unsigned long arg)
  2716. {
  2717. struct comedi_file *cfp = file->private_data;
  2718. struct comedi_device *dev = cfp->dev;
  2719. struct comedi32_insnlist_struct insnlist32;
  2720. struct comedi32_insn_struct __user *insn32;
  2721. struct comedi_insn *insns;
  2722. unsigned int n;
  2723. int rc;
  2724. if (copy_from_user(&insnlist32, compat_ptr(arg), sizeof(insnlist32)))
  2725. return -EFAULT;
  2726. rc = check_insnlist_len(dev, insnlist32.n_insns);
  2727. if (rc)
  2728. return rc;
  2729. insns = kcalloc(insnlist32.n_insns, sizeof(*insns), GFP_KERNEL);
  2730. if (!insns)
  2731. return -ENOMEM;
  2732. /* Copy insn structures. */
  2733. insn32 = compat_ptr(insnlist32.insns);
  2734. for (n = 0; n < insnlist32.n_insns; n++) {
  2735. rc = get_compat_insn(insns + n, insn32 + n);
  2736. if (rc) {
  2737. kfree(insns);
  2738. return rc;
  2739. }
  2740. }
  2741. mutex_lock(&dev->mutex);
  2742. rc = do_insnlist_ioctl(dev, insns, insnlist32.n_insns, file);
  2743. mutex_unlock(&dev->mutex);
  2744. kfree(insns);
  2745. return rc;
  2746. }
  2747. /* Handle 32-bit COMEDI_INSN ioctl. */
  2748. static int compat_insn(struct file *file, unsigned long arg)
  2749. {
  2750. struct comedi_file *cfp = file->private_data;
  2751. struct comedi_device *dev = cfp->dev;
  2752. struct comedi_insn insn;
  2753. int rc;
  2754. rc = get_compat_insn(&insn, (void __user *)arg);
  2755. if (rc)
  2756. return rc;
  2757. mutex_lock(&dev->mutex);
  2758. rc = do_insn_ioctl(dev, &insn, file);
  2759. mutex_unlock(&dev->mutex);
  2760. return rc;
  2761. }
  2762. /*
  2763. * compat_ioctl file operation.
  2764. *
  2765. * Returns -ENOIOCTLCMD for unrecognised ioctl codes.
  2766. */
  2767. static long comedi_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2768. {
  2769. int rc;
  2770. switch (cmd) {
  2771. case COMEDI_DEVCONFIG:
  2772. case COMEDI_DEVINFO:
  2773. case COMEDI_SUBDINFO:
  2774. case COMEDI_BUFCONFIG:
  2775. case COMEDI_BUFINFO:
  2776. /* Just need to translate the pointer argument. */
  2777. arg = (unsigned long)compat_ptr(arg);
  2778. rc = comedi_unlocked_ioctl(file, cmd, arg);
  2779. break;
  2780. case COMEDI_LOCK:
  2781. case COMEDI_UNLOCK:
  2782. case COMEDI_CANCEL:
  2783. case COMEDI_POLL:
  2784. case COMEDI_SETRSUBD:
  2785. case COMEDI_SETWSUBD:
  2786. /* No translation needed. */
  2787. rc = comedi_unlocked_ioctl(file, cmd, arg);
  2788. break;
  2789. case COMEDI32_CHANINFO:
  2790. rc = compat_chaninfo(file, arg);
  2791. break;
  2792. case COMEDI32_RANGEINFO:
  2793. rc = compat_rangeinfo(file, arg);
  2794. break;
  2795. case COMEDI32_CMD:
  2796. rc = compat_cmd(file, arg);
  2797. break;
  2798. case COMEDI32_CMDTEST:
  2799. rc = compat_cmdtest(file, arg);
  2800. break;
  2801. case COMEDI32_INSNLIST:
  2802. rc = compat_insnlist(file, arg);
  2803. break;
  2804. case COMEDI32_INSN:
  2805. rc = compat_insn(file, arg);
  2806. break;
  2807. default:
  2808. rc = -ENOIOCTLCMD;
  2809. break;
  2810. }
  2811. return rc;
  2812. }
  2813. #else
  2814. #define comedi_compat_ioctl NULL
  2815. #endif
  2816. static const struct file_operations comedi_fops = {
  2817. .owner = THIS_MODULE,
  2818. .unlocked_ioctl = comedi_unlocked_ioctl,
  2819. .compat_ioctl = comedi_compat_ioctl,
  2820. .open = comedi_open,
  2821. .release = comedi_close,
  2822. .read = comedi_read,
  2823. .write = comedi_write,
  2824. .mmap = comedi_mmap,
  2825. .poll = comedi_poll,
  2826. .fasync = comedi_fasync,
  2827. .llseek = noop_llseek,
  2828. };
  2829. /**
  2830. * comedi_event() - Handle events for asynchronous COMEDI command
  2831. * @dev: COMEDI device.
  2832. * @s: COMEDI subdevice.
  2833. * Context: in_interrupt() (usually), @s->spin_lock spin-lock not held.
  2834. *
  2835. * If an asynchronous COMEDI command is active on the subdevice, process
  2836. * any %COMEDI_CB_... event flags that have been set, usually by an
  2837. * interrupt handler. These may change the run state of the asynchronous
  2838. * command, wake a task, and/or send a %SIGIO signal.
  2839. */
  2840. void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
  2841. {
  2842. struct comedi_async *async = s->async;
  2843. unsigned int events;
  2844. int si_code = 0;
  2845. unsigned long flags;
  2846. spin_lock_irqsave(&s->spin_lock, flags);
  2847. events = async->events;
  2848. async->events = 0;
  2849. if (!__comedi_is_subdevice_running(s)) {
  2850. spin_unlock_irqrestore(&s->spin_lock, flags);
  2851. return;
  2852. }
  2853. if (events & COMEDI_CB_CANCEL_MASK)
  2854. __comedi_clear_subdevice_runflags(s, COMEDI_SRF_RUNNING);
  2855. /*
  2856. * Remember if an error event has occurred, so an error can be
  2857. * returned the next time the user does a read() or write().
  2858. */
  2859. if (events & COMEDI_CB_ERROR_MASK)
  2860. __comedi_set_subdevice_runflags(s, COMEDI_SRF_ERROR);
  2861. if (async->cb_mask & events) {
  2862. wake_up_interruptible(&async->wait_head);
  2863. si_code = async->cmd.flags & CMDF_WRITE ? POLL_OUT : POLL_IN;
  2864. }
  2865. spin_unlock_irqrestore(&s->spin_lock, flags);
  2866. if (si_code)
  2867. kill_fasync(&dev->async_queue, SIGIO, si_code);
  2868. }
  2869. EXPORT_SYMBOL_GPL(comedi_event);
  2870. /* Note: the ->mutex is pre-locked on successful return */
  2871. struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device)
  2872. {
  2873. struct comedi_device *dev;
  2874. struct device *csdev;
  2875. unsigned int i;
  2876. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  2877. if (!dev)
  2878. return ERR_PTR(-ENOMEM);
  2879. comedi_device_init(dev);
  2880. comedi_set_hw_dev(dev, hardware_device);
  2881. mutex_lock(&dev->mutex);
  2882. mutex_lock(&comedi_board_minor_table_lock);
  2883. for (i = hardware_device ? comedi_num_legacy_minors : 0;
  2884. i < COMEDI_NUM_BOARD_MINORS; ++i) {
  2885. if (!comedi_board_minor_table[i]) {
  2886. comedi_board_minor_table[i] = dev;
  2887. break;
  2888. }
  2889. }
  2890. mutex_unlock(&comedi_board_minor_table_lock);
  2891. if (i == COMEDI_NUM_BOARD_MINORS) {
  2892. mutex_unlock(&dev->mutex);
  2893. comedi_device_cleanup(dev);
  2894. comedi_dev_put(dev);
  2895. dev_err(hardware_device,
  2896. "ran out of minor numbers for board device files\n");
  2897. return ERR_PTR(-EBUSY);
  2898. }
  2899. dev->minor = i;
  2900. csdev = device_create(&comedi_class, hardware_device,
  2901. MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
  2902. if (!IS_ERR(csdev))
  2903. dev->class_dev = get_device(csdev);
  2904. /* Note: dev->mutex needs to be unlocked by the caller. */
  2905. return dev;
  2906. }
  2907. void comedi_release_hardware_device(struct device *hardware_device)
  2908. {
  2909. int minor;
  2910. struct comedi_device *dev;
  2911. for (minor = comedi_num_legacy_minors; minor < COMEDI_NUM_BOARD_MINORS;
  2912. minor++) {
  2913. mutex_lock(&comedi_board_minor_table_lock);
  2914. dev = comedi_board_minor_table[minor];
  2915. if (dev && dev->hw_dev == hardware_device) {
  2916. comedi_board_minor_table[minor] = NULL;
  2917. mutex_unlock(&comedi_board_minor_table_lock);
  2918. comedi_free_board_dev(dev);
  2919. break;
  2920. }
  2921. mutex_unlock(&comedi_board_minor_table_lock);
  2922. }
  2923. }
  2924. int comedi_alloc_subdevice_minor(struct comedi_subdevice *s)
  2925. {
  2926. struct comedi_device *dev = s->device;
  2927. struct device *csdev;
  2928. unsigned int i;
  2929. mutex_lock(&comedi_subdevice_minor_table_lock);
  2930. for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i) {
  2931. if (!comedi_subdevice_minor_table[i]) {
  2932. comedi_subdevice_minor_table[i] = s;
  2933. break;
  2934. }
  2935. }
  2936. mutex_unlock(&comedi_subdevice_minor_table_lock);
  2937. if (i == COMEDI_NUM_SUBDEVICE_MINORS) {
  2938. dev_err(dev->class_dev,
  2939. "ran out of minor numbers for subdevice files\n");
  2940. return -EBUSY;
  2941. }
  2942. i += COMEDI_NUM_BOARD_MINORS;
  2943. s->minor = i;
  2944. csdev = device_create(&comedi_class, dev->class_dev,
  2945. MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
  2946. dev->minor, s->index);
  2947. if (!IS_ERR(csdev))
  2948. s->class_dev = csdev;
  2949. return 0;
  2950. }
  2951. void comedi_free_subdevice_minor(struct comedi_subdevice *s)
  2952. {
  2953. unsigned int i;
  2954. if (!s)
  2955. return;
  2956. if (s->minor < COMEDI_NUM_BOARD_MINORS ||
  2957. s->minor >= COMEDI_NUM_MINORS)
  2958. return;
  2959. i = s->minor - COMEDI_NUM_BOARD_MINORS;
  2960. mutex_lock(&comedi_subdevice_minor_table_lock);
  2961. if (s == comedi_subdevice_minor_table[i])
  2962. comedi_subdevice_minor_table[i] = NULL;
  2963. mutex_unlock(&comedi_subdevice_minor_table_lock);
  2964. if (s->class_dev) {
  2965. device_destroy(&comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
  2966. s->class_dev = NULL;
  2967. }
  2968. }
  2969. static void comedi_cleanup_board_minors(void)
  2970. {
  2971. struct comedi_device *dev;
  2972. unsigned int i;
  2973. for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
  2974. dev = comedi_clear_board_minor(i);
  2975. comedi_free_board_dev(dev);
  2976. }
  2977. }
  2978. static int __init comedi_init(void)
  2979. {
  2980. int i;
  2981. int retval;
  2982. pr_info("version " COMEDI_RELEASE " - http://www.comedi.org\n");
  2983. if (comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
  2984. pr_err("invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
  2985. COMEDI_NUM_BOARD_MINORS);
  2986. return -EINVAL;
  2987. }
  2988. retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
  2989. COMEDI_NUM_MINORS, "comedi");
  2990. if (retval)
  2991. return retval;
  2992. cdev_init(&comedi_cdev, &comedi_fops);
  2993. comedi_cdev.owner = THIS_MODULE;
  2994. retval = kobject_set_name(&comedi_cdev.kobj, "comedi");
  2995. if (retval)
  2996. goto out_unregister_chrdev_region;
  2997. retval = cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0),
  2998. COMEDI_NUM_MINORS);
  2999. if (retval)
  3000. goto out_unregister_chrdev_region;
  3001. retval = class_register(&comedi_class);
  3002. if (retval) {
  3003. pr_err("failed to create class\n");
  3004. goto out_cdev_del;
  3005. }
  3006. /* create devices files for legacy/manual use */
  3007. for (i = 0; i < comedi_num_legacy_minors; i++) {
  3008. struct comedi_device *dev;
  3009. dev = comedi_alloc_board_minor(NULL);
  3010. if (IS_ERR(dev)) {
  3011. retval = PTR_ERR(dev);
  3012. goto out_cleanup_board_minors;
  3013. }
  3014. /* comedi_alloc_board_minor() locked the mutex */
  3015. lockdep_assert_held(&dev->mutex);
  3016. mutex_unlock(&dev->mutex);
  3017. }
  3018. /* XXX requires /proc interface */
  3019. comedi_proc_init();
  3020. return 0;
  3021. out_cleanup_board_minors:
  3022. comedi_cleanup_board_minors();
  3023. class_unregister(&comedi_class);
  3024. out_cdev_del:
  3025. cdev_del(&comedi_cdev);
  3026. out_unregister_chrdev_region:
  3027. unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
  3028. return retval;
  3029. }
  3030. module_init(comedi_init);
  3031. static void __exit comedi_cleanup(void)
  3032. {
  3033. comedi_cleanup_board_minors();
  3034. class_unregister(&comedi_class);
  3035. cdev_del(&comedi_cdev);
  3036. unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
  3037. comedi_proc_cleanup();
  3038. }
  3039. module_exit(comedi_cleanup);
  3040. MODULE_AUTHOR("https://www.comedi.org");
  3041. MODULE_DESCRIPTION("Comedi core module");
  3042. MODULE_LICENSE("GPL");