comedi_fops.c 80 KB

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