comedi_fops.c 80 KB

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