comedi_fops.c 87 KB

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