comedi_fops.c 67 KB

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